blob: 2c295bcd8105aa8af64947438171757ad6967ac7 [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
Stan Ilievaaa9e832019-09-17 14:07:23 -040020#include <GrContext.h>
21#include <SkBitmap.h>
Alec Mouri4a818f12019-11-19 16:17:53 -080022#include <apex/choreographer.h>
Stan Ilievaaa9e832019-09-17 14:07:23 -040023#include <cutils/compiler.h>
24#include <thread/ThreadBase.h>
25#include <utils/Looper.h>
26#include <utils/Thread.h>
27
28#include <memory>
29#include <mutex>
30#include <set>
John Recke45b1fd2014-04-15 09:50:16 -070031
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040032#include "CacheManager.h"
Fedor Kudasov85d6f102019-07-31 15:58:16 +010033#include "ProfileDataContainer.h"
Stan Ilievaaa9e832019-09-17 14:07:23 -040034#include "RenderTask.h"
John Reckba6adf62015-02-19 14:36:50 -080035#include "TimeLord.h"
John Reck283bb462018-12-13 16:40:14 -080036#include "WebViewFunctorManager.h"
John Reck0fa0cbc2019-04-05 16:57:46 -070037#include "thread/ThreadBase.h"
John Reck3c0f5632019-03-15 16:36:01 -070038#include "utils/TimeUtils.h"
John Recke45b1fd2014-04-15 09:50:16 -070039
John Reckcec24ae2013-11-05 13:27:50 -080040namespace android {
John Reck3b202512014-06-23 13:13:08 -070041
Stan Iliev7bc3bc62017-05-24 13:28:36 -040042class Bitmap;
John Recke45b1fd2014-04-15 09:50:16 -070043
John Reckcec24ae2013-11-05 13:27:50 -080044namespace uirenderer {
John Reck3b202512014-06-23 13:13:08 -070045
Stan Ilievaaa9e832019-09-17 14:07:23 -040046class AutoBackendTextureRelease;
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050047class Readback;
John Reck3b202512014-06-23 13:13:08 -070048class RenderState;
Chris Craik0a24b142015-10-19 17:10:19 -070049class TestUtils;
John Reck3b202512014-06-23 13:13:08 -070050
Bo Liu7b8c1eb2019-01-08 20:17:55 -080051namespace skiapipeline {
52class VkFunctorDrawHandler;
53}
54
Stan Iliev52e43922019-08-06 15:59:44 -040055namespace VectorDrawable {
56class Tree;
57}
58
John Reckcec24ae2013-11-05 13:27:50 -080059namespace renderthread {
60
John Reck443a7142014-09-04 17:40:05 -070061class CanvasContext;
John Reck3b202512014-06-23 13:13:08 -070062class EglManager;
63class RenderProxy;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050064class VulkanManager;
John Recke45b1fd2014-04-15 09:50:16 -070065
John Recke45b1fd2014-04-15 09:50:16 -070066// Mimics android.view.Choreographer.FrameCallback
67class IFrameCallback {
68public:
John Reck18f16e62014-05-02 16:46:41 -070069 virtual void doFrame() = 0;
John Recke45b1fd2014-04-15 09:50:16 -070070
71protected:
Greg Kaisera95435b2018-08-23 16:58:18 -070072 virtual ~IFrameCallback() {}
John Recke45b1fd2014-04-15 09:50:16 -070073};
74
John Reck56428472018-03-16 17:27:17 -070075struct VsyncSource {
76 virtual void requestNextVsync() = 0;
Alec Mouri4a818f12019-11-19 16:17:53 -080077 virtual void drainPendingEvents() = 0;
John Reck56428472018-03-16 17:27:17 -070078 virtual ~VsyncSource() {}
79};
80
Alec Mouri4a818f12019-11-19 16:17:53 -080081class ChoreographerSource;
John Reck56428472018-03-16 17:27:17 -070082class DummyVsyncSource;
83
Stan Iliev978d5322019-02-06 12:02:28 -050084typedef void (*JVMAttachHook)(const char* name);
Stan Iliev80dbc352019-02-05 15:31:28 -050085
John Reckf8441e62017-10-23 13:10:41 -070086class RenderThread : private ThreadBase {
John Reckdf1742e2017-01-19 15:56:21 -080087 PREVENT_COPY_AND_ASSIGN(RenderThread);
John Reckf8441e62017-10-23 13:10:41 -070088
John Reck1bcacfd2017-11-03 10:12:19 -070089public:
John Reck8785ceb2018-10-29 16:45:58 -070090 // Sets a callback that fires before any RenderThread setup has occurred.
Derek Sollenberger3fedf5a2020-02-21 13:07:28 -050091 static void setOnStartHook(JVMAttachHook onStartHook);
Stan Iliev80dbc352019-02-05 15:31:28 -050092 static JVMAttachHook getOnStartHook();
John Reck259b25a2017-12-01 16:18:53 -080093
John Reckf8441e62017-10-23 13:10:41 -070094 WorkQueue& queue() { return ThreadBase::queue(); }
John Reckcec24ae2013-11-05 13:27:50 -080095
John Recke45b1fd2014-04-15 09:50:16 -070096 // Mimics android.view.Choreographer
97 void postFrameCallback(IFrameCallback* callback);
John Reck01a5ea32014-12-03 13:01:07 -080098 bool removeFrameCallback(IFrameCallback* callback);
John Recka5dda642014-05-22 15:43:54 -070099 // If the callback is currently registered, it will be pushed back until
100 // the next vsync. If it is not currently registered this does nothing.
101 void pushBackFrameCallback(IFrameCallback* callback);
John Recke45b1fd2014-04-15 09:50:16 -0700102
John Reck18f16e62014-05-02 16:46:41 -0700103 TimeLord& timeLord() { return mTimeLord; }
Derek Sollenbergerdaf72292016-10-25 12:09:18 -0400104 RenderState& renderState() const { return *mRenderState; }
105 EglManager& eglManager() const { return *mEglManager; }
John Reck34781b22017-07-05 16:39:36 -0700106 ProfileDataContainer& globalProfileData() { return mGlobalProfileData; }
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500107 Readback& readback();
John Reck18f16e62014-05-02 16:46:41 -0700108
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400109 GrContext* getGrContext() const { return mGrContext.get(); }
Greg Daniel660d6ec2017-12-08 11:44:27 -0500110 void setGrContext(sk_sp<GrContext> cxt);
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400111
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400112 CacheManager& cacheManager() { return *mCacheManager; }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500113 VulkanManager& vulkanManager() { return *mVkManager; }
114
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400115 sk_sp<Bitmap> allocateHardwareBitmap(SkBitmap& skBitmap);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400116 void dumpGraphicsMemory(int fd);
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400117
John Reck1e510712018-04-23 08:15:03 -0700118 void requireGlContext();
Stan Iliev981afe72019-02-13 14:24:33 -0500119 void requireVkContext();
John Reck283bb462018-12-13 16:40:14 -0800120 void destroyRenderingContext();
John Reck1e510712018-04-23 08:15:03 -0700121
Stan Iliev898123b2019-02-14 14:57:44 -0500122 void preload();
123
Stan Iliev6b894d72017-08-23 12:41:41 -0400124 /**
125 * isCurrent provides a way to query, if the caller is running on
126 * the render thread.
127 *
128 * @return true only if isCurrent is invoked from the render thread.
129 */
130 static bool isCurrent();
131
Stan Iliev981afe72019-02-13 14:24:33 -0500132 static void initGrContextOptions(GrContextOptions& options);
133
John Reckcec24ae2013-11-05 13:27:50 -0800134protected:
Chris Craikd41c4d82015-01-05 15:51:13 -0800135 virtual bool threadLoop() override;
John Reckcec24ae2013-11-05 13:27:50 -0800136
137private:
John Recke45b1fd2014-04-15 09:50:16 -0700138 friend class DispatchFrameCallbacks;
John Reck3b202512014-06-23 13:13:08 -0700139 friend class RenderProxy;
John Reck56428472018-03-16 17:27:17 -0700140 friend class DummyVsyncSource;
Alec Mouri4a818f12019-11-19 16:17:53 -0800141 friend class ChoreographerSource;
Stan Ilievaaa9e832019-09-17 14:07:23 -0400142 friend class android::uirenderer::AutoBackendTextureRelease;
Chris Craik0a24b142015-10-19 17:10:19 -0700143 friend class android::uirenderer::TestUtils;
Bo Liu1b0278c2019-01-03 16:36:24 -0800144 friend class android::uirenderer::WebViewFunctor;
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800145 friend class android::uirenderer::skiapipeline::VkFunctorDrawHandler;
Stan Iliev52e43922019-08-06 15:59:44 -0400146 friend class android::uirenderer::VectorDrawable::Tree;
John Reckcec24ae2013-11-05 13:27:50 -0800147
148 RenderThread();
149 virtual ~RenderThread();
150
John Reck6b507802015-11-03 10:09:59 -0800151 static bool hasInstance();
152 static RenderThread& getInstance();
153
John Reck3b202512014-06-23 13:13:08 -0700154 void initThreadLocals();
Alec Mouri4a818f12019-11-19 16:17:53 -0800155 void initializeChoreographer();
John Reckcf185f52019-04-11 16:11:24 -0700156 void setupFrameInterval();
Alec Mouri4a818f12019-11-19 16:17:53 -0800157 // Callbacks for choreographer events:
158 // choreographerCallback will call AChoreograper_handleEvent to call the
159 // corresponding callbacks for each display event type
160 static int choreographerCallback(int fd, int events, void* data);
161 // Callback that will be run on vsync ticks.
162 static void frameCallback(int64_t frameTimeNanos, void* data);
163 // Callback that will be run whenver there is a refresh rate change.
164 static void refreshRateCallback(int64_t vsyncPeriod, void* data);
John Recka733f892014-12-19 11:37:21 -0800165 void drainDisplayEventQueue();
John Recke45b1fd2014-04-15 09:50:16 -0700166 void dispatchFrameCallbacks();
John Recka5dda642014-05-22 15:43:54 -0700167 void requestVsync();
John Recke45b1fd2014-04-15 09:50:16 -0700168
Alec Mouri4a818f12019-11-19 16:17:53 -0800169 AChoreographer* mChoreographer;
John Reck56428472018-03-16 17:27:17 -0700170 VsyncSource* mVsyncSource;
John Recke45b1fd2014-04-15 09:50:16 -0700171 bool mVsyncRequested;
172 std::set<IFrameCallback*> mFrameCallbacks;
John Recka5dda642014-05-22 15:43:54 -0700173 // We defer the actual registration of these callbacks until
174 // both mQueue *and* mDisplayEventReceiver have been drained off all
175 // immediate events. This makes sure that we catch the next vsync, not
176 // the previous one
177 std::set<IFrameCallback*> mPendingRegistrationFrameCallbacks;
John Recke45b1fd2014-04-15 09:50:16 -0700178 bool mFrameCallbackTaskPending;
John Reck18f16e62014-05-02 16:46:41 -0700179
180 TimeLord mTimeLord;
John Reck3c0f5632019-03-15 16:36:01 -0700181 nsecs_t mDispatchFrameDelay = 4_ms;
John Reck3b202512014-06-23 13:13:08 -0700182 RenderState* mRenderState;
183 EglManager* mEglManager;
John Reck283bb462018-12-13 16:40:14 -0800184 WebViewFunctorManager& mFunctorManager;
John Reckba6adf62015-02-19 14:36:50 -0800185
John Reck34781b22017-07-05 16:39:36 -0700186 ProfileDataContainer mGlobalProfileData;
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500187 Readback* mReadback = nullptr;
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400188
189 sk_sp<GrContext> mGrContext;
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400190 CacheManager* mCacheManager;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500191 VulkanManager* mVkManager;
John Reckcec24ae2013-11-05 13:27:50 -0800192};
193
194} /* namespace renderthread */
195} /* namespace uirenderer */
196} /* namespace android */
197#endif /* RENDERTHREAD_H_ */