blob: 30884b571b940cfd4df5fc0506766f88904e7e93 [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 Recke45b1fd2014-04-15 09:50:16 -070025
Derek Sollenberger98f75d52016-10-25 10:25:45 -040026#include <GrContext.h>
John Reckcec24ae2013-11-05 13:27:50 -080027#include <cutils/compiler.h>
Stan Iliev7bc3bc62017-05-24 13:28:36 -040028#include <SkBitmap.h>
John Reckb36016c2015-03-11 08:50:53 -070029#include <ui/DisplayInfo.h>
John Reckcec24ae2013-11-05 13:27:50 -080030#include <utils/Looper.h>
John Reckcec24ae2013-11-05 13:27:50 -080031#include <utils/Thread.h>
32
John Reckba6adf62015-02-19 14:36:50 -080033#include <memory>
34#include <set>
John Reck18f16e62014-05-02 16:46:41 -070035
John Reckcec24ae2013-11-05 13:27:50 -080036namespace android {
John Reck3b202512014-06-23 13:13:08 -070037
Stan Iliev7bc3bc62017-05-24 13:28:36 -040038class Bitmap;
John Recke45b1fd2014-04-15 09:50:16 -070039class DisplayEventReceiver;
40
John Reckcec24ae2013-11-05 13:27:50 -080041namespace uirenderer {
John Reck3b202512014-06-23 13:13:08 -070042
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050043class Readback;
John Reck3b202512014-06-23 13:13:08 -070044class RenderState;
Chris Craik0a24b142015-10-19 17:10:19 -070045class TestUtils;
John Reck3b202512014-06-23 13:13:08 -070046
John Reckcec24ae2013-11-05 13:27:50 -080047namespace renderthread {
48
John Reck443a7142014-09-04 17:40:05 -070049class CanvasContext;
John Recke45b1fd2014-04-15 09:50:16 -070050class DispatchFrameCallbacks;
John Reck3b202512014-06-23 13:13:08 -070051class EglManager;
52class RenderProxy;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050053class VulkanManager;
John Recke45b1fd2014-04-15 09:50:16 -070054
John Reck4f02bf42014-01-03 18:09:17 -080055class TaskQueue {
56public:
57 TaskQueue();
58
59 RenderTask* next();
60 void queue(RenderTask* task);
John Recka5dda642014-05-22 15:43:54 -070061 void queueAtFront(RenderTask* task);
John Reck4f02bf42014-01-03 18:09:17 -080062 RenderTask* peek();
63 void remove(RenderTask* task);
64
65private:
66 RenderTask* mHead;
67 RenderTask* mTail;
68};
69
John Recke45b1fd2014-04-15 09:50:16 -070070// Mimics android.view.Choreographer.FrameCallback
71class IFrameCallback {
72public:
John Reck18f16e62014-05-02 16:46:41 -070073 virtual void doFrame() = 0;
John Recke45b1fd2014-04-15 09:50:16 -070074
75protected:
76 ~IFrameCallback() {}
77};
78
John Reck6b507802015-11-03 10:09:59 -080079class ANDROID_API RenderThread : public Thread {
John Reckdf1742e2017-01-19 15:56:21 -080080 PREVENT_COPY_AND_ASSIGN(RenderThread);
John Reckcec24ae2013-11-05 13:27:50 -080081public:
82 // RenderThread takes complete ownership of tasks that are queued
83 // and will delete them after they are run
84 ANDROID_API void queue(RenderTask* task);
Chris Craik0a24b142015-10-19 17:10:19 -070085 ANDROID_API void queueAndWait(RenderTask* task);
John Recka5dda642014-05-22 15:43:54 -070086 ANDROID_API void queueAtFront(RenderTask* task);
John Recka733f892014-12-19 11:37:21 -080087 void queueAt(RenderTask* task, nsecs_t runAtNs);
John Reck4f02bf42014-01-03 18:09:17 -080088 void remove(RenderTask* task);
John Reckcec24ae2013-11-05 13:27:50 -080089
John Recke45b1fd2014-04-15 09:50:16 -070090 // Mimics android.view.Choreographer
91 void postFrameCallback(IFrameCallback* callback);
John Reck01a5ea32014-12-03 13:01:07 -080092 bool removeFrameCallback(IFrameCallback* callback);
John Recka5dda642014-05-22 15:43:54 -070093 // If the callback is currently registered, it will be pushed back until
94 // the next vsync. If it is not currently registered this does nothing.
95 void pushBackFrameCallback(IFrameCallback* callback);
John Recke45b1fd2014-04-15 09:50:16 -070096
John Reck18f16e62014-05-02 16:46:41 -070097 TimeLord& timeLord() { return mTimeLord; }
Derek Sollenbergerdaf72292016-10-25 12:09:18 -040098 RenderState& renderState() const { return *mRenderState; }
99 EglManager& eglManager() const { return *mEglManager; }
John Reck34781b22017-07-05 16:39:36 -0700100 ProfileDataContainer& globalProfileData() { return mGlobalProfileData; }
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500101 Readback& readback();
John Reck18f16e62014-05-02 16:46:41 -0700102
John Reckb36016c2015-03-11 08:50:53 -0700103 const DisplayInfo& mainDisplayInfo() { return mDisplayInfo; }
104
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400105 GrContext* getGrContext() const { return mGrContext.get(); }
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400106 void setGrContext(GrContext* cxt);
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400107
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400108 CacheManager& cacheManager() { return *mCacheManager; }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500109 VulkanManager& vulkanManager() { return *mVkManager; }
110
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400111 sk_sp<Bitmap> allocateHardwareBitmap(SkBitmap& skBitmap);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400112 void dumpGraphicsMemory(int fd);
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400113
Stan Iliev6b894d72017-08-23 12:41:41 -0400114 /**
115 * isCurrent provides a way to query, if the caller is running on
116 * the render thread.
117 *
118 * @return true only if isCurrent is invoked from the render thread.
119 */
120 static bool isCurrent();
121
John Reckcec24ae2013-11-05 13:27:50 -0800122protected:
Chris Craikd41c4d82015-01-05 15:51:13 -0800123 virtual bool threadLoop() override;
John Reckcec24ae2013-11-05 13:27:50 -0800124
125private:
John Recke45b1fd2014-04-15 09:50:16 -0700126 friend class DispatchFrameCallbacks;
John Reck3b202512014-06-23 13:13:08 -0700127 friend class RenderProxy;
Chris Craik0a24b142015-10-19 17:10:19 -0700128 friend class android::uirenderer::TestUtils;
John Reckcec24ae2013-11-05 13:27:50 -0800129
130 RenderThread();
131 virtual ~RenderThread();
132
John Reck6b507802015-11-03 10:09:59 -0800133 static bool hasInstance();
134 static RenderThread& getInstance();
135
John Reck3b202512014-06-23 13:13:08 -0700136 void initThreadLocals();
John Recke45b1fd2014-04-15 09:50:16 -0700137 void initializeDisplayEventReceiver();
138 static int displayEventReceiverCallback(int fd, int events, void* data);
John Recka733f892014-12-19 11:37:21 -0800139 void drainDisplayEventQueue();
John Recke45b1fd2014-04-15 09:50:16 -0700140 void dispatchFrameCallbacks();
John Recka5dda642014-05-22 15:43:54 -0700141 void requestVsync();
John Recke45b1fd2014-04-15 09:50:16 -0700142
John Reck4f02bf42014-01-03 18:09:17 -0800143 // Returns the next task to be run. If this returns NULL nextWakeup is set
144 // to the time to requery for the nextTask to run. mNextWakeup is also
145 // set to this time
146 RenderTask* nextTask(nsecs_t* nextWakeup);
John Reckcec24ae2013-11-05 13:27:50 -0800147
148 sp<Looper> mLooper;
149 Mutex mLock;
150
John Reck4f02bf42014-01-03 18:09:17 -0800151 nsecs_t mNextWakeup;
152 TaskQueue mQueue;
John Recke45b1fd2014-04-15 09:50:16 -0700153
John Reckb36016c2015-03-11 08:50:53 -0700154 DisplayInfo mDisplayInfo;
155
John Recke45b1fd2014-04-15 09:50:16 -0700156 DisplayEventReceiver* mDisplayEventReceiver;
157 bool mVsyncRequested;
158 std::set<IFrameCallback*> mFrameCallbacks;
John Recka5dda642014-05-22 15:43:54 -0700159 // We defer the actual registration of these callbacks until
160 // both mQueue *and* mDisplayEventReceiver have been drained off all
161 // immediate events. This makes sure that we catch the next vsync, not
162 // the previous one
163 std::set<IFrameCallback*> mPendingRegistrationFrameCallbacks;
John Recke45b1fd2014-04-15 09:50:16 -0700164 bool mFrameCallbackTaskPending;
165 DispatchFrameCallbacks* mFrameCallbackTask;
John Reck18f16e62014-05-02 16:46:41 -0700166
167 TimeLord mTimeLord;
John Reck3b202512014-06-23 13:13:08 -0700168 RenderState* mRenderState;
169 EglManager* mEglManager;
John Reckba6adf62015-02-19 14:36:50 -0800170
John Reck34781b22017-07-05 16:39:36 -0700171 ProfileDataContainer mGlobalProfileData;
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500172 Readback* mReadback = nullptr;
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400173
174 sk_sp<GrContext> mGrContext;
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400175 CacheManager* mCacheManager;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500176 VulkanManager* mVkManager;
John Reckcec24ae2013-11-05 13:27:50 -0800177};
178
179} /* namespace renderthread */
180} /* namespace uirenderer */
181} /* namespace android */
182#endif /* RENDERTHREAD_H_ */