blob: 4b5601c5abc43328a9cca84efbbdc7744f1b8241 [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>
Stan Iliev7bc3bc62017-05-24 13:28:36 -040027#include <SkBitmap.h>
John Reckb36016c2015-03-11 08:50:53 -070028#include <ui/DisplayInfo.h>
John Reckcec24ae2013-11-05 13:27:50 -080029#include <utils/Looper.h>
John Reckcec24ae2013-11-05 13:27:50 -080030#include <utils/Thread.h>
31
John Reckba6adf62015-02-19 14:36:50 -080032#include <memory>
33#include <set>
John Reck18f16e62014-05-02 16:46:41 -070034
John Reckcec24ae2013-11-05 13:27:50 -080035namespace android {
John Reck3b202512014-06-23 13:13:08 -070036
Stan Iliev7bc3bc62017-05-24 13:28:36 -040037class Bitmap;
John Recke45b1fd2014-04-15 09:50:16 -070038class DisplayEventReceiver;
39
John Reckcec24ae2013-11-05 13:27:50 -080040namespace uirenderer {
John Reck3b202512014-06-23 13:13:08 -070041
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050042class Readback;
John Reck3b202512014-06-23 13:13:08 -070043class RenderState;
Chris Craik0a24b142015-10-19 17:10:19 -070044class TestUtils;
John Reck3b202512014-06-23 13:13:08 -070045
John Reckcec24ae2013-11-05 13:27:50 -080046namespace renderthread {
47
John Reck443a7142014-09-04 17:40:05 -070048class CanvasContext;
John Recke45b1fd2014-04-15 09:50:16 -070049class DispatchFrameCallbacks;
John Reck3b202512014-06-23 13:13:08 -070050class EglManager;
51class RenderProxy;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050052class VulkanManager;
John Recke45b1fd2014-04-15 09:50:16 -070053
John Reck4f02bf42014-01-03 18:09:17 -080054class TaskQueue {
55public:
56 TaskQueue();
57
58 RenderTask* next();
59 void queue(RenderTask* task);
John Recka5dda642014-05-22 15:43:54 -070060 void queueAtFront(RenderTask* task);
John Reck4f02bf42014-01-03 18:09:17 -080061 RenderTask* peek();
62 void remove(RenderTask* task);
63
64private:
65 RenderTask* mHead;
66 RenderTask* mTail;
67};
68
John Recke45b1fd2014-04-15 09:50:16 -070069// Mimics android.view.Choreographer.FrameCallback
70class IFrameCallback {
71public:
John Reck18f16e62014-05-02 16:46:41 -070072 virtual void doFrame() = 0;
John Recke45b1fd2014-04-15 09:50:16 -070073
74protected:
75 ~IFrameCallback() {}
76};
77
John Reck6b507802015-11-03 10:09:59 -080078class ANDROID_API RenderThread : public Thread {
John Reckdf1742e2017-01-19 15:56:21 -080079 PREVENT_COPY_AND_ASSIGN(RenderThread);
John Reckcec24ae2013-11-05 13:27:50 -080080public:
81 // RenderThread takes complete ownership of tasks that are queued
82 // and will delete them after they are run
83 ANDROID_API void queue(RenderTask* task);
Chris Craik0a24b142015-10-19 17:10:19 -070084 ANDROID_API void queueAndWait(RenderTask* task);
John Recka5dda642014-05-22 15:43:54 -070085 ANDROID_API void queueAtFront(RenderTask* task);
John Recka733f892014-12-19 11:37:21 -080086 void queueAt(RenderTask* task, nsecs_t runAtNs);
John Reck4f02bf42014-01-03 18:09:17 -080087 void remove(RenderTask* task);
John Reckcec24ae2013-11-05 13:27:50 -080088
John Recke45b1fd2014-04-15 09:50:16 -070089 // Mimics android.view.Choreographer
90 void postFrameCallback(IFrameCallback* callback);
John Reck01a5ea32014-12-03 13:01:07 -080091 bool removeFrameCallback(IFrameCallback* callback);
John Recka5dda642014-05-22 15:43:54 -070092 // If the callback is currently registered, it will be pushed back until
93 // the next vsync. If it is not currently registered this does nothing.
94 void pushBackFrameCallback(IFrameCallback* callback);
John Recke45b1fd2014-04-15 09:50:16 -070095
John Reck18f16e62014-05-02 16:46:41 -070096 TimeLord& timeLord() { return mTimeLord; }
Derek Sollenbergerdaf72292016-10-25 12:09:18 -040097 RenderState& renderState() const { return *mRenderState; }
98 EglManager& eglManager() const { return *mEglManager; }
John Reckba6adf62015-02-19 14:36:50 -080099 JankTracker& jankTracker() { return *mJankTracker; }
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500100 Readback& readback();
John Reck18f16e62014-05-02 16:46:41 -0700101
John Reckb36016c2015-03-11 08:50:53 -0700102 const DisplayInfo& mainDisplayInfo() { return mDisplayInfo; }
103
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400104 GrContext* getGrContext() const { return mGrContext.get(); }
105 void setGrContext(GrContext* cxt) { mGrContext.reset(cxt); }
106
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500107 VulkanManager& vulkanManager() { return *mVkManager; }
108
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400109 sk_sp<Bitmap> allocateHardwareBitmap(SkBitmap& skBitmap);
110
John Reckcec24ae2013-11-05 13:27:50 -0800111protected:
Chris Craikd41c4d82015-01-05 15:51:13 -0800112 virtual bool threadLoop() override;
John Reckcec24ae2013-11-05 13:27:50 -0800113
114private:
John Recke45b1fd2014-04-15 09:50:16 -0700115 friend class DispatchFrameCallbacks;
John Reck3b202512014-06-23 13:13:08 -0700116 friend class RenderProxy;
Chris Craik0a24b142015-10-19 17:10:19 -0700117 friend class android::uirenderer::TestUtils;
John Reckcec24ae2013-11-05 13:27:50 -0800118
119 RenderThread();
120 virtual ~RenderThread();
121
John Reck6b507802015-11-03 10:09:59 -0800122 static bool hasInstance();
123 static RenderThread& getInstance();
124
John Reck3b202512014-06-23 13:13:08 -0700125 void initThreadLocals();
John Recke45b1fd2014-04-15 09:50:16 -0700126 void initializeDisplayEventReceiver();
127 static int displayEventReceiverCallback(int fd, int events, void* data);
John Recka733f892014-12-19 11:37:21 -0800128 void drainDisplayEventQueue();
John Recke45b1fd2014-04-15 09:50:16 -0700129 void dispatchFrameCallbacks();
John Recka5dda642014-05-22 15:43:54 -0700130 void requestVsync();
John Recke45b1fd2014-04-15 09:50:16 -0700131
John Reck4f02bf42014-01-03 18:09:17 -0800132 // Returns the next task to be run. If this returns NULL nextWakeup is set
133 // to the time to requery for the nextTask to run. mNextWakeup is also
134 // set to this time
135 RenderTask* nextTask(nsecs_t* nextWakeup);
John Reckcec24ae2013-11-05 13:27:50 -0800136
137 sp<Looper> mLooper;
138 Mutex mLock;
139
John Reck4f02bf42014-01-03 18:09:17 -0800140 nsecs_t mNextWakeup;
141 TaskQueue mQueue;
John Recke45b1fd2014-04-15 09:50:16 -0700142
John Reckb36016c2015-03-11 08:50:53 -0700143 DisplayInfo mDisplayInfo;
144
John Recke45b1fd2014-04-15 09:50:16 -0700145 DisplayEventReceiver* mDisplayEventReceiver;
146 bool mVsyncRequested;
147 std::set<IFrameCallback*> mFrameCallbacks;
John Recka5dda642014-05-22 15:43:54 -0700148 // We defer the actual registration of these callbacks until
149 // both mQueue *and* mDisplayEventReceiver have been drained off all
150 // immediate events. This makes sure that we catch the next vsync, not
151 // the previous one
152 std::set<IFrameCallback*> mPendingRegistrationFrameCallbacks;
John Recke45b1fd2014-04-15 09:50:16 -0700153 bool mFrameCallbackTaskPending;
154 DispatchFrameCallbacks* mFrameCallbackTask;
John Reck18f16e62014-05-02 16:46:41 -0700155
156 TimeLord mTimeLord;
John Reck3b202512014-06-23 13:13:08 -0700157 RenderState* mRenderState;
158 EglManager* mEglManager;
John Reckba6adf62015-02-19 14:36:50 -0800159
160 JankTracker* mJankTracker = nullptr;
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500161 Readback* mReadback = nullptr;
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400162
163 sk_sp<GrContext> mGrContext;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500164 VulkanManager* mVkManager;
John Reckcec24ae2013-11-05 13:27:50 -0800165};
166
167} /* namespace renderthread */
168} /* namespace uirenderer */
169} /* namespace android */
170#endif /* RENDERTHREAD_H_ */