blob: 970537b69fad192e5ffda0c4f881b6f6f1b9e6f7 [file] [log] [blame]
John Reckcec24ae2013-11-05 13:27:50 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef RENDERTHREAD_H_
18#define RENDERTHREAD_H_
19
20#include "RenderTask.h"
John Recke45b1fd2014-04-15 09:50:16 -070021
John Reckba6adf62015-02-19 14:36:50 -080022#include "../JankTracker.h"
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040023#include "CacheManager.h"
John Reckba6adf62015-02-19 14:36:50 -080024#include "TimeLord.h"
John Reckf8441e62017-10-23 13:10:41 -070025#include "thread/ThreadBase.h"
John Recke45b1fd2014-04-15 09:50:16 -070026
Derek Sollenberger98f75d52016-10-25 10:25:45 -040027#include <GrContext.h>
Stan Iliev7bc3bc62017-05-24 13:28:36 -040028#include <SkBitmap.h>
John Reck1bcacfd2017-11-03 10:12:19 -070029#include <cutils/compiler.h>
John Reckb36016c2015-03-11 08:50:53 -070030#include <ui/DisplayInfo.h>
John Reckcec24ae2013-11-05 13:27:50 -080031#include <utils/Looper.h>
John Reckcec24ae2013-11-05 13:27:50 -080032#include <utils/Thread.h>
33
John Reck1bcacfd2017-11-03 10:12:19 -070034#include <thread/ThreadBase.h>
John Reckba6adf62015-02-19 14:36:50 -080035#include <memory>
John Reckf8441e62017-10-23 13:10:41 -070036#include <mutex>
John Reckba6adf62015-02-19 14:36:50 -080037#include <set>
John Reck18f16e62014-05-02 16:46:41 -070038
John Reckcec24ae2013-11-05 13:27:50 -080039namespace android {
John Reck3b202512014-06-23 13:13:08 -070040
Stan Iliev7bc3bc62017-05-24 13:28:36 -040041class Bitmap;
John Recke45b1fd2014-04-15 09:50:16 -070042class DisplayEventReceiver;
43
John Reckcec24ae2013-11-05 13:27:50 -080044namespace uirenderer {
John Reck3b202512014-06-23 13:13:08 -070045
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050046class Readback;
John Reck3b202512014-06-23 13:13:08 -070047class RenderState;
Chris Craik0a24b142015-10-19 17:10:19 -070048class TestUtils;
John Reck3b202512014-06-23 13:13:08 -070049
John Reckcec24ae2013-11-05 13:27:50 -080050namespace renderthread {
51
John Reck443a7142014-09-04 17:40:05 -070052class CanvasContext;
John Reck3b202512014-06-23 13:13:08 -070053class EglManager;
54class RenderProxy;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050055class VulkanManager;
John Recke45b1fd2014-04-15 09:50:16 -070056
John Recke45b1fd2014-04-15 09:50:16 -070057// Mimics android.view.Choreographer.FrameCallback
58class IFrameCallback {
59public:
John Reck18f16e62014-05-02 16:46:41 -070060 virtual void doFrame() = 0;
John Recke45b1fd2014-04-15 09:50:16 -070061
62protected:
63 ~IFrameCallback() {}
64};
65
John Reckf8441e62017-10-23 13:10:41 -070066class RenderThread : private ThreadBase {
John Reckdf1742e2017-01-19 15:56:21 -080067 PREVENT_COPY_AND_ASSIGN(RenderThread);
John Reckf8441e62017-10-23 13:10:41 -070068
John Reck1bcacfd2017-11-03 10:12:19 -070069public:
John Reck259b25a2017-12-01 16:18:53 -080070 // Sets a callback that fires before any RenderThread setup has occured.
71 ANDROID_API static void setOnStartHook(void (*onStartHook)());
72
John Reckf8441e62017-10-23 13:10:41 -070073 WorkQueue& queue() { return ThreadBase::queue(); }
John Reckcec24ae2013-11-05 13:27:50 -080074
John Recke45b1fd2014-04-15 09:50:16 -070075 // Mimics android.view.Choreographer
76 void postFrameCallback(IFrameCallback* callback);
John Reck01a5ea32014-12-03 13:01:07 -080077 bool removeFrameCallback(IFrameCallback* callback);
John Recka5dda642014-05-22 15:43:54 -070078 // If the callback is currently registered, it will be pushed back until
79 // the next vsync. If it is not currently registered this does nothing.
80 void pushBackFrameCallback(IFrameCallback* callback);
John Recke45b1fd2014-04-15 09:50:16 -070081
John Reck18f16e62014-05-02 16:46:41 -070082 TimeLord& timeLord() { return mTimeLord; }
Derek Sollenbergerdaf72292016-10-25 12:09:18 -040083 RenderState& renderState() const { return *mRenderState; }
84 EglManager& eglManager() const { return *mEglManager; }
John Reck34781b22017-07-05 16:39:36 -070085 ProfileDataContainer& globalProfileData() { return mGlobalProfileData; }
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050086 Readback& readback();
John Reck18f16e62014-05-02 16:46:41 -070087
John Reckb36016c2015-03-11 08:50:53 -070088 const DisplayInfo& mainDisplayInfo() { return mDisplayInfo; }
89
Derek Sollenberger98f75d52016-10-25 10:25:45 -040090 GrContext* getGrContext() const { return mGrContext.get(); }
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040091 void setGrContext(GrContext* cxt);
Derek Sollenberger98f75d52016-10-25 10:25:45 -040092
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040093 CacheManager& cacheManager() { return *mCacheManager; }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050094 VulkanManager& vulkanManager() { return *mVkManager; }
95
Stan Iliev7bc3bc62017-05-24 13:28:36 -040096 sk_sp<Bitmap> allocateHardwareBitmap(SkBitmap& skBitmap);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040097 void dumpGraphicsMemory(int fd);
Stan Iliev7bc3bc62017-05-24 13:28:36 -040098
Stan Iliev6b894d72017-08-23 12:41:41 -040099 /**
100 * isCurrent provides a way to query, if the caller is running on
101 * the render thread.
102 *
103 * @return true only if isCurrent is invoked from the render thread.
104 */
105 static bool isCurrent();
106
John Reckcec24ae2013-11-05 13:27:50 -0800107protected:
Chris Craikd41c4d82015-01-05 15:51:13 -0800108 virtual bool threadLoop() override;
John Reckcec24ae2013-11-05 13:27:50 -0800109
110private:
John Recke45b1fd2014-04-15 09:50:16 -0700111 friend class DispatchFrameCallbacks;
John Reck3b202512014-06-23 13:13:08 -0700112 friend class RenderProxy;
Chris Craik0a24b142015-10-19 17:10:19 -0700113 friend class android::uirenderer::TestUtils;
John Reckcec24ae2013-11-05 13:27:50 -0800114
115 RenderThread();
116 virtual ~RenderThread();
117
John Reck6b507802015-11-03 10:09:59 -0800118 static bool hasInstance();
119 static RenderThread& getInstance();
120
John Reck3b202512014-06-23 13:13:08 -0700121 void initThreadLocals();
John Recke45b1fd2014-04-15 09:50:16 -0700122 void initializeDisplayEventReceiver();
123 static int displayEventReceiverCallback(int fd, int events, void* data);
John Recka733f892014-12-19 11:37:21 -0800124 void drainDisplayEventQueue();
John Recke45b1fd2014-04-15 09:50:16 -0700125 void dispatchFrameCallbacks();
John Recka5dda642014-05-22 15:43:54 -0700126 void requestVsync();
John Recke45b1fd2014-04-15 09:50:16 -0700127
John Reckb36016c2015-03-11 08:50:53 -0700128 DisplayInfo mDisplayInfo;
129
John Recke45b1fd2014-04-15 09:50:16 -0700130 DisplayEventReceiver* mDisplayEventReceiver;
131 bool mVsyncRequested;
132 std::set<IFrameCallback*> mFrameCallbacks;
John Recka5dda642014-05-22 15:43:54 -0700133 // We defer the actual registration of these callbacks until
134 // both mQueue *and* mDisplayEventReceiver have been drained off all
135 // immediate events. This makes sure that we catch the next vsync, not
136 // the previous one
137 std::set<IFrameCallback*> mPendingRegistrationFrameCallbacks;
John Recke45b1fd2014-04-15 09:50:16 -0700138 bool mFrameCallbackTaskPending;
John Reck18f16e62014-05-02 16:46:41 -0700139
140 TimeLord mTimeLord;
John Reck3b202512014-06-23 13:13:08 -0700141 RenderState* mRenderState;
142 EglManager* mEglManager;
John Reckba6adf62015-02-19 14:36:50 -0800143
John Reck34781b22017-07-05 16:39:36 -0700144 ProfileDataContainer mGlobalProfileData;
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500145 Readback* mReadback = nullptr;
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400146
147 sk_sp<GrContext> mGrContext;
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400148 CacheManager* mCacheManager;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500149 VulkanManager* mVkManager;
John Reckcec24ae2013-11-05 13:27:50 -0800150};
151
152} /* namespace renderthread */
153} /* namespace uirenderer */
154} /* namespace android */
155#endif /* RENDERTHREAD_H_ */