blob: 34542c6021ec6873674837af847640b41c2e8dc0 [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"
23#include "TimeLord.h"
John Recke45b1fd2014-04-15 09:50:16 -070024
Derek Sollenberger98f75d52016-10-25 10:25:45 -040025#include <GrContext.h>
John Reckcec24ae2013-11-05 13:27:50 -080026#include <cutils/compiler.h>
John Reckb36016c2015-03-11 08:50:53 -070027#include <ui/DisplayInfo.h>
John Reckcec24ae2013-11-05 13:27:50 -080028#include <utils/Looper.h>
John Reckcec24ae2013-11-05 13:27:50 -080029#include <utils/Thread.h>
30
John Reckba6adf62015-02-19 14:36:50 -080031#include <memory>
32#include <set>
John Reck18f16e62014-05-02 16:46:41 -070033
John Reckcec24ae2013-11-05 13:27:50 -080034namespace android {
John Reck3b202512014-06-23 13:13:08 -070035
Stan Ilievb33013f2017-05-05 19:41:36 -040036class Bitmap;
John Recke45b1fd2014-04-15 09:50:16 -070037class DisplayEventReceiver;
38
John Reckcec24ae2013-11-05 13:27:50 -080039namespace uirenderer {
John Reck3b202512014-06-23 13:13:08 -070040
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050041class Readback;
John Reck3b202512014-06-23 13:13:08 -070042class RenderState;
Chris Craik0a24b142015-10-19 17:10:19 -070043class TestUtils;
John Reck3b202512014-06-23 13:13:08 -070044
John Reckcec24ae2013-11-05 13:27:50 -080045namespace renderthread {
46
John Reck443a7142014-09-04 17:40:05 -070047class CanvasContext;
John Recke45b1fd2014-04-15 09:50:16 -070048class DispatchFrameCallbacks;
John Reck3b202512014-06-23 13:13:08 -070049class EglManager;
50class RenderProxy;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050051class VulkanManager;
John Recke45b1fd2014-04-15 09:50:16 -070052
John Reck4f02bf42014-01-03 18:09:17 -080053class TaskQueue {
54public:
55 TaskQueue();
56
57 RenderTask* next();
58 void queue(RenderTask* task);
John Recka5dda642014-05-22 15:43:54 -070059 void queueAtFront(RenderTask* task);
John Reck4f02bf42014-01-03 18:09:17 -080060 RenderTask* peek();
61 void remove(RenderTask* task);
62
63private:
64 RenderTask* mHead;
65 RenderTask* mTail;
66};
67
John Recke45b1fd2014-04-15 09:50:16 -070068// Mimics android.view.Choreographer.FrameCallback
69class IFrameCallback {
70public:
John Reck18f16e62014-05-02 16:46:41 -070071 virtual void doFrame() = 0;
John Recke45b1fd2014-04-15 09:50:16 -070072
73protected:
74 ~IFrameCallback() {}
75};
76
John Reck6b507802015-11-03 10:09:59 -080077class ANDROID_API RenderThread : public Thread {
John Reckdf1742e2017-01-19 15:56:21 -080078 PREVENT_COPY_AND_ASSIGN(RenderThread);
John Reckcec24ae2013-11-05 13:27:50 -080079public:
80 // RenderThread takes complete ownership of tasks that are queued
81 // and will delete them after they are run
82 ANDROID_API void queue(RenderTask* task);
Chris Craik0a24b142015-10-19 17:10:19 -070083 ANDROID_API void queueAndWait(RenderTask* task);
John Recka5dda642014-05-22 15:43:54 -070084 ANDROID_API void queueAtFront(RenderTask* task);
John Recka733f892014-12-19 11:37:21 -080085 void queueAt(RenderTask* task, nsecs_t runAtNs);
John Reck4f02bf42014-01-03 18:09:17 -080086 void remove(RenderTask* task);
John Reckcec24ae2013-11-05 13:27:50 -080087
John Recke45b1fd2014-04-15 09:50:16 -070088 // Mimics android.view.Choreographer
89 void postFrameCallback(IFrameCallback* callback);
John Reck01a5ea32014-12-03 13:01:07 -080090 bool removeFrameCallback(IFrameCallback* callback);
John Recka5dda642014-05-22 15:43:54 -070091 // If the callback is currently registered, it will be pushed back until
92 // the next vsync. If it is not currently registered this does nothing.
93 void pushBackFrameCallback(IFrameCallback* callback);
John Recke45b1fd2014-04-15 09:50:16 -070094
John Reck18f16e62014-05-02 16:46:41 -070095 TimeLord& timeLord() { return mTimeLord; }
Derek Sollenbergerdaf72292016-10-25 12:09:18 -040096 RenderState& renderState() const { return *mRenderState; }
97 EglManager& eglManager() const { return *mEglManager; }
John Reckba6adf62015-02-19 14:36:50 -080098 JankTracker& jankTracker() { return *mJankTracker; }
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050099 Readback& readback();
John Reck18f16e62014-05-02 16:46:41 -0700100
John Reckb36016c2015-03-11 08:50:53 -0700101 const DisplayInfo& mainDisplayInfo() { return mDisplayInfo; }
102
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400103 GrContext* getGrContext() const { return mGrContext.get(); }
104 void setGrContext(GrContext* cxt) { mGrContext.reset(cxt); }
105
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500106 VulkanManager& vulkanManager() { return *mVkManager; }
107
Stan Ilievb33013f2017-05-05 19:41:36 -0400108 sk_sp<SkImage> makeTextureImage(Bitmap* bitmap);
109
John Reckcec24ae2013-11-05 13:27:50 -0800110protected:
Chris Craikd41c4d82015-01-05 15:51:13 -0800111 virtual bool threadLoop() override;
John Reckcec24ae2013-11-05 13:27:50 -0800112
113private:
John Recke45b1fd2014-04-15 09:50:16 -0700114 friend class DispatchFrameCallbacks;
John Reck3b202512014-06-23 13:13:08 -0700115 friend class RenderProxy;
Chris Craik0a24b142015-10-19 17:10:19 -0700116 friend class android::uirenderer::TestUtils;
John Reckcec24ae2013-11-05 13:27:50 -0800117
118 RenderThread();
119 virtual ~RenderThread();
120
John Reck6b507802015-11-03 10:09:59 -0800121 static bool hasInstance();
122 static RenderThread& getInstance();
123
John Reck3b202512014-06-23 13:13:08 -0700124 void initThreadLocals();
John Recke45b1fd2014-04-15 09:50:16 -0700125 void initializeDisplayEventReceiver();
126 static int displayEventReceiverCallback(int fd, int events, void* data);
John Recka733f892014-12-19 11:37:21 -0800127 void drainDisplayEventQueue();
John Recke45b1fd2014-04-15 09:50:16 -0700128 void dispatchFrameCallbacks();
John Recka5dda642014-05-22 15:43:54 -0700129 void requestVsync();
John Recke45b1fd2014-04-15 09:50:16 -0700130
John Reck4f02bf42014-01-03 18:09:17 -0800131 // Returns the next task to be run. If this returns NULL nextWakeup is set
132 // to the time to requery for the nextTask to run. mNextWakeup is also
133 // set to this time
134 RenderTask* nextTask(nsecs_t* nextWakeup);
John Reckcec24ae2013-11-05 13:27:50 -0800135
136 sp<Looper> mLooper;
137 Mutex mLock;
138
John Reck4f02bf42014-01-03 18:09:17 -0800139 nsecs_t mNextWakeup;
140 TaskQueue mQueue;
John Recke45b1fd2014-04-15 09:50:16 -0700141
John Reckb36016c2015-03-11 08:50:53 -0700142 DisplayInfo mDisplayInfo;
143
John Recke45b1fd2014-04-15 09:50:16 -0700144 DisplayEventReceiver* mDisplayEventReceiver;
145 bool mVsyncRequested;
146 std::set<IFrameCallback*> mFrameCallbacks;
John Recka5dda642014-05-22 15:43:54 -0700147 // We defer the actual registration of these callbacks until
148 // both mQueue *and* mDisplayEventReceiver have been drained off all
149 // immediate events. This makes sure that we catch the next vsync, not
150 // the previous one
151 std::set<IFrameCallback*> mPendingRegistrationFrameCallbacks;
John Recke45b1fd2014-04-15 09:50:16 -0700152 bool mFrameCallbackTaskPending;
153 DispatchFrameCallbacks* mFrameCallbackTask;
John Reck18f16e62014-05-02 16:46:41 -0700154
155 TimeLord mTimeLord;
John Reck3b202512014-06-23 13:13:08 -0700156 RenderState* mRenderState;
157 EglManager* mEglManager;
John Reckba6adf62015-02-19 14:36:50 -0800158
159 JankTracker* mJankTracker = nullptr;
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500160 Readback* mReadback = nullptr;
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400161
162 sk_sp<GrContext> mGrContext;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500163 VulkanManager* mVkManager;
John Reckcec24ae2013-11-05 13:27:50 -0800164};
165
166} /* namespace renderthread */
167} /* namespace uirenderer */
168} /* namespace android */
169#endif /* RENDERTHREAD_H_ */