blob: d9842572d7cd3bea7c94b137fe66a8b891bffcf8 [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 Reckba6adf62015-02-19 14:36:50 -0800100 JankTracker& jankTracker() { return *mJankTracker; }
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
John Reckcec24ae2013-11-05 13:27:50 -0800114protected:
Chris Craikd41c4d82015-01-05 15:51:13 -0800115 virtual bool threadLoop() override;
John Reckcec24ae2013-11-05 13:27:50 -0800116
117private:
John Recke45b1fd2014-04-15 09:50:16 -0700118 friend class DispatchFrameCallbacks;
John Reck3b202512014-06-23 13:13:08 -0700119 friend class RenderProxy;
Chris Craik0a24b142015-10-19 17:10:19 -0700120 friend class android::uirenderer::TestUtils;
John Reckcec24ae2013-11-05 13:27:50 -0800121
122 RenderThread();
123 virtual ~RenderThread();
124
John Reck6b507802015-11-03 10:09:59 -0800125 static bool hasInstance();
126 static RenderThread& getInstance();
127
John Reck3b202512014-06-23 13:13:08 -0700128 void initThreadLocals();
John Recke45b1fd2014-04-15 09:50:16 -0700129 void initializeDisplayEventReceiver();
130 static int displayEventReceiverCallback(int fd, int events, void* data);
John Recka733f892014-12-19 11:37:21 -0800131 void drainDisplayEventQueue();
John Recke45b1fd2014-04-15 09:50:16 -0700132 void dispatchFrameCallbacks();
John Recka5dda642014-05-22 15:43:54 -0700133 void requestVsync();
John Recke45b1fd2014-04-15 09:50:16 -0700134
John Reck4f02bf42014-01-03 18:09:17 -0800135 // Returns the next task to be run. If this returns NULL nextWakeup is set
136 // to the time to requery for the nextTask to run. mNextWakeup is also
137 // set to this time
138 RenderTask* nextTask(nsecs_t* nextWakeup);
John Reckcec24ae2013-11-05 13:27:50 -0800139
140 sp<Looper> mLooper;
141 Mutex mLock;
142
John Reck4f02bf42014-01-03 18:09:17 -0800143 nsecs_t mNextWakeup;
144 TaskQueue mQueue;
John Recke45b1fd2014-04-15 09:50:16 -0700145
John Reckb36016c2015-03-11 08:50:53 -0700146 DisplayInfo mDisplayInfo;
147
John Recke45b1fd2014-04-15 09:50:16 -0700148 DisplayEventReceiver* mDisplayEventReceiver;
149 bool mVsyncRequested;
150 std::set<IFrameCallback*> mFrameCallbacks;
John Recka5dda642014-05-22 15:43:54 -0700151 // We defer the actual registration of these callbacks until
152 // both mQueue *and* mDisplayEventReceiver have been drained off all
153 // immediate events. This makes sure that we catch the next vsync, not
154 // the previous one
155 std::set<IFrameCallback*> mPendingRegistrationFrameCallbacks;
John Recke45b1fd2014-04-15 09:50:16 -0700156 bool mFrameCallbackTaskPending;
157 DispatchFrameCallbacks* mFrameCallbackTask;
John Reck18f16e62014-05-02 16:46:41 -0700158
159 TimeLord mTimeLord;
John Reck3b202512014-06-23 13:13:08 -0700160 RenderState* mRenderState;
161 EglManager* mEglManager;
John Reckba6adf62015-02-19 14:36:50 -0800162
163 JankTracker* mJankTracker = nullptr;
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500164 Readback* mReadback = nullptr;
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400165
166 sk_sp<GrContext> mGrContext;
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400167 CacheManager* mCacheManager;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500168 VulkanManager* mVkManager;
John Reckcec24ae2013-11-05 13:27:50 -0800169};
170
171} /* namespace renderthread */
172} /* namespace uirenderer */
173} /* namespace android */
174#endif /* RENDERTHREAD_H_ */