blob: 045d26f1d3293684c6303fd3a83626cf006a5a13 [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
Adlai Hollerf8c434e2020-07-27 11:42:45 -040020#include <GrDirectContext.h>
Stan Ilievaaa9e832019-09-17 14:07:23 -040021#include <SkBitmap.h>
22#include <cutils/compiler.h>
John Reck5f66fb82022-09-23 17:49:23 -040023#include <surface_control_private.h>
Stan Ilievaaa9e832019-09-17 14:07:23 -040024#include <utils/Thread.h>
25
26#include <memory>
27#include <mutex>
28#include <set>
John Recke45b1fd2014-04-15 09:50:16 -070029
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040030#include "CacheManager.h"
John Reck5f66fb82022-09-23 17:49:23 -040031#include "MemoryPolicy.h"
Fedor Kudasov85d6f102019-07-31 15:58:16 +010032#include "ProfileDataContainer.h"
Stan Ilievaaa9e832019-09-17 14:07:23 -040033#include "RenderTask.h"
John Reckba6adf62015-02-19 14:36:50 -080034#include "TimeLord.h"
John Reck283bb462018-12-13 16:40:14 -080035#include "WebViewFunctorManager.h"
John Reck0fa0cbc2019-04-05 16:57:46 -070036#include "thread/ThreadBase.h"
John Reck3c0f5632019-03-15 16:36:01 -070037#include "utils/TimeUtils.h"
John Recke45b1fd2014-04-15 09:50:16 -070038
John Reckcec24ae2013-11-05 13:27:50 -080039namespace android {
John Reck3b202512014-06-23 13:13:08 -070040
Stan Iliev7bc3bc62017-05-24 13:28:36 -040041class Bitmap;
John Recke45b1fd2014-04-15 09:50:16 -070042
John Reckcec24ae2013-11-05 13:27:50 -080043namespace uirenderer {
John Reck3b202512014-06-23 13:13:08 -070044
Stan Ilievaaa9e832019-09-17 14:07:23 -040045class AutoBackendTextureRelease;
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050046class Readback;
John Reck3b202512014-06-23 13:13:08 -070047class RenderState;
Chris Craik0a24b142015-10-19 17:10:19 -070048class TestUtils;
John Reck3b202512014-06-23 13:13:08 -070049
Bo Liu7b8c1eb2019-01-08 20:17:55 -080050namespace skiapipeline {
51class VkFunctorDrawHandler;
52}
53
Stan Iliev52e43922019-08-06 15:59:44 -040054namespace VectorDrawable {
55class Tree;
56}
57
John Reckcec24ae2013-11-05 13:27:50 -080058namespace renderthread {
59
John Reck443a7142014-09-04 17:40:05 -070060class CanvasContext;
John Reck3b202512014-06-23 13:13:08 -070061class EglManager;
62class RenderProxy;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050063class VulkanManager;
John Recke45b1fd2014-04-15 09:50:16 -070064
John Recke45b1fd2014-04-15 09:50:16 -070065// Mimics android.view.Choreographer.FrameCallback
66class IFrameCallback {
67public:
John Reck18f16e62014-05-02 16:46:41 -070068 virtual void doFrame() = 0;
John Recke45b1fd2014-04-15 09:50:16 -070069
70protected:
Greg Kaisera95435b2018-08-23 16:58:18 -070071 virtual ~IFrameCallback() {}
John Recke45b1fd2014-04-15 09:50:16 -070072};
73
John Reck56428472018-03-16 17:27:17 -070074struct VsyncSource {
75 virtual void requestNextVsync() = 0;
Alec Mouri4a818f12019-11-19 16:17:53 -080076 virtual void drainPendingEvents() = 0;
John Reck56428472018-03-16 17:27:17 -070077 virtual ~VsyncSource() {}
78};
79
Huihong Luo054b8d32021-02-24 18:48:12 -080080typedef ASurfaceControl* (*ASC_create)(ASurfaceControl* parent, const char* debug_name);
Huihong Luo5fdf7b82021-01-15 14:27:06 -080081typedef void (*ASC_acquire)(ASurfaceControl* control);
82typedef void (*ASC_release)(ASurfaceControl* control);
83
Pablo Gamitobc9e5292021-08-23 17:12:29 +020084typedef void (*ASC_registerSurfaceStatsListener)(ASurfaceControl* control, int32_t id,
85 void* context,
86 ASurfaceControl_SurfaceStatsListener func);
Jorim Jaggi71db8892021-02-03 23:19:29 +010087typedef void (*ASC_unregisterSurfaceStatsListener)(void* context,
Huihong Luo054b8d32021-02-24 18:48:12 -080088 ASurfaceControl_SurfaceStatsListener func);
Jorim Jaggi71db8892021-02-03 23:19:29 +010089
90typedef int64_t (*ASCStats_getAcquireTime)(ASurfaceControlStats* stats);
91typedef uint64_t (*ASCStats_getFrameNumber)(ASurfaceControlStats* stats);
92
Huihong Luo054b8d32021-02-24 18:48:12 -080093typedef ASurfaceTransaction* (*AST_create)();
94typedef void (*AST_delete)(ASurfaceTransaction* transaction);
95typedef void (*AST_apply)(ASurfaceTransaction* transaction);
Huihong Luo540fdf82021-06-25 13:59:39 -070096typedef void (*AST_reparent)(ASurfaceTransaction* aSurfaceTransaction,
97 ASurfaceControl* aSurfaceControl,
98 ASurfaceControl* newParentASurfaceControl);
Huihong Luo054b8d32021-02-24 18:48:12 -080099typedef void (*AST_setVisibility)(ASurfaceTransaction* transaction,
100 ASurfaceControl* surface_control, int8_t visibility);
Huihong Luo34f42fd2021-05-03 14:47:36 -0700101typedef void (*AST_setZOrder)(ASurfaceTransaction* transaction, ASurfaceControl* surface_control,
102 int32_t z_order);
Huihong Luo054b8d32021-02-24 18:48:12 -0800103
Huihong Luo5fdf7b82021-01-15 14:27:06 -0800104struct ASurfaceControlFunctions {
105 ASurfaceControlFunctions();
Huihong Luo054b8d32021-02-24 18:48:12 -0800106
107 ASC_create createFunc;
Huihong Luo5fdf7b82021-01-15 14:27:06 -0800108 ASC_acquire acquireFunc;
109 ASC_release releaseFunc;
Jorim Jaggi71db8892021-02-03 23:19:29 +0100110 ASC_registerSurfaceStatsListener registerListenerFunc;
111 ASC_unregisterSurfaceStatsListener unregisterListenerFunc;
112 ASCStats_getAcquireTime getAcquireTimeFunc;
113 ASCStats_getFrameNumber getFrameNumberFunc;
Huihong Luo054b8d32021-02-24 18:48:12 -0800114
115 AST_create transactionCreateFunc;
116 AST_delete transactionDeleteFunc;
117 AST_apply transactionApplyFunc;
Huihong Luo540fdf82021-06-25 13:59:39 -0700118 AST_reparent transactionReparentFunc;
Huihong Luo054b8d32021-02-24 18:48:12 -0800119 AST_setVisibility transactionSetVisibilityFunc;
Huihong Luo34f42fd2021-05-03 14:47:36 -0700120 AST_setZOrder transactionSetZOrderFunc;
Huihong Luo5fdf7b82021-01-15 14:27:06 -0800121};
122
Alec Mouri4a818f12019-11-19 16:17:53 -0800123class ChoreographerSource;
John Reck56428472018-03-16 17:27:17 -0700124class DummyVsyncSource;
125
Stan Iliev978d5322019-02-06 12:02:28 -0500126typedef void (*JVMAttachHook)(const char* name);
Stan Iliev80dbc352019-02-05 15:31:28 -0500127
John Reckf8441e62017-10-23 13:10:41 -0700128class RenderThread : private ThreadBase {
John Reckdf1742e2017-01-19 15:56:21 -0800129 PREVENT_COPY_AND_ASSIGN(RenderThread);
John Reckf8441e62017-10-23 13:10:41 -0700130
John Reck1bcacfd2017-11-03 10:12:19 -0700131public:
John Reck8785ceb2018-10-29 16:45:58 -0700132 // Sets a callback that fires before any RenderThread setup has occurred.
Derek Sollenberger3fedf5a2020-02-21 13:07:28 -0500133 static void setOnStartHook(JVMAttachHook onStartHook);
Stan Iliev80dbc352019-02-05 15:31:28 -0500134 static JVMAttachHook getOnStartHook();
John Reck259b25a2017-12-01 16:18:53 -0800135
John Reckf8441e62017-10-23 13:10:41 -0700136 WorkQueue& queue() { return ThreadBase::queue(); }
John Reckcec24ae2013-11-05 13:27:50 -0800137
John Recke45b1fd2014-04-15 09:50:16 -0700138 // Mimics android.view.Choreographer
139 void postFrameCallback(IFrameCallback* callback);
John Reck01a5ea32014-12-03 13:01:07 -0800140 bool removeFrameCallback(IFrameCallback* callback);
John Recka5dda642014-05-22 15:43:54 -0700141 // If the callback is currently registered, it will be pushed back until
142 // the next vsync. If it is not currently registered this does nothing.
143 void pushBackFrameCallback(IFrameCallback* callback);
John Recke45b1fd2014-04-15 09:50:16 -0700144
John Reck18f16e62014-05-02 16:46:41 -0700145 TimeLord& timeLord() { return mTimeLord; }
Derek Sollenbergerdaf72292016-10-25 12:09:18 -0400146 RenderState& renderState() const { return *mRenderState; }
147 EglManager& eglManager() const { return *mEglManager; }
John Reck34781b22017-07-05 16:39:36 -0700148 ProfileDataContainer& globalProfileData() { return mGlobalProfileData; }
Jorim Jaggi71db8892021-02-03 23:19:29 +0100149 std::mutex& getJankDataMutex() { return mJankDataMutex; }
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500150 Readback& readback();
John Reck18f16e62014-05-02 16:46:41 -0700151
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400152 GrDirectContext* getGrContext() const { return mGrContext.get(); }
153 void setGrContext(sk_sp<GrDirectContext> cxt);
John Reck2abc5492021-05-18 00:34:26 -0400154 sk_sp<GrDirectContext> requireGrContext();
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400155
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400156 CacheManager& cacheManager() { return *mCacheManager; }
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400157 VulkanManager& vulkanManager();
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500158
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400159 sk_sp<Bitmap> allocateHardwareBitmap(SkBitmap& skBitmap);
John Reck66e06d42021-05-11 17:04:54 -0400160 void dumpGraphicsMemory(int fd, bool includeProfileData);
John Reck39207682021-05-12 19:10:47 -0400161 void getMemoryUsage(size_t* cpuUsage, size_t* gpuUsage);
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400162
John Reck1e510712018-04-23 08:15:03 -0700163 void requireGlContext();
Stan Iliev981afe72019-02-13 14:24:33 -0500164 void requireVkContext();
John Reck283bb462018-12-13 16:40:14 -0800165 void destroyRenderingContext();
John Reck1e510712018-04-23 08:15:03 -0700166
Stan Iliev898123b2019-02-14 14:57:44 -0500167 void preload();
168
Huihong Luo5fdf7b82021-01-15 14:27:06 -0800169 const ASurfaceControlFunctions& getASurfaceControlFunctions() {
170 return mASurfaceControlFunctions;
171 }
172
John Reck5f66fb82022-09-23 17:49:23 -0400173 void trimMemory(TrimLevel level);
Jernej Virag44db0402023-05-09 20:24:48 +0200174 void trimCaches(CacheTrimLevel level);
John Reck5f66fb82022-09-23 17:49:23 -0400175
Stan Iliev6b894d72017-08-23 12:41:41 -0400176 /**
177 * isCurrent provides a way to query, if the caller is running on
178 * the render thread.
179 *
180 * @return true only if isCurrent is invoked from the render thread.
181 */
182 static bool isCurrent();
183
Stan Iliev981afe72019-02-13 14:24:33 -0500184 static void initGrContextOptions(GrContextOptions& options);
185
John Reckcec24ae2013-11-05 13:27:50 -0800186protected:
Chris Craikd41c4d82015-01-05 15:51:13 -0800187 virtual bool threadLoop() override;
John Reckcec24ae2013-11-05 13:27:50 -0800188
189private:
John Recke45b1fd2014-04-15 09:50:16 -0700190 friend class DispatchFrameCallbacks;
John Reck3b202512014-06-23 13:13:08 -0700191 friend class RenderProxy;
John Reck56428472018-03-16 17:27:17 -0700192 friend class DummyVsyncSource;
Alec Mouri4a818f12019-11-19 16:17:53 -0800193 friend class ChoreographerSource;
Stan Ilievaaa9e832019-09-17 14:07:23 -0400194 friend class android::uirenderer::AutoBackendTextureRelease;
Chris Craik0a24b142015-10-19 17:10:19 -0700195 friend class android::uirenderer::TestUtils;
Bo Liu1b0278c2019-01-03 16:36:24 -0800196 friend class android::uirenderer::WebViewFunctor;
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800197 friend class android::uirenderer::skiapipeline::VkFunctorDrawHandler;
Stan Iliev52e43922019-08-06 15:59:44 -0400198 friend class android::uirenderer::VectorDrawable::Tree;
Steven Moreland76ec3822021-04-02 16:26:03 +0000199 friend class sp<RenderThread>;
John Reckcec24ae2013-11-05 13:27:50 -0800200
201 RenderThread();
202 virtual ~RenderThread();
203
John Reck6b507802015-11-03 10:09:59 -0800204 static bool hasInstance();
205 static RenderThread& getInstance();
206
John Reck3b202512014-06-23 13:13:08 -0700207 void initThreadLocals();
Alec Mouri4a818f12019-11-19 16:17:53 -0800208 void initializeChoreographer();
John Reckcf185f52019-04-11 16:11:24 -0700209 void setupFrameInterval();
Alec Mouri4a818f12019-11-19 16:17:53 -0800210 // Callbacks for choreographer events:
211 // choreographerCallback will call AChoreograper_handleEvent to call the
212 // corresponding callbacks for each display event type
213 static int choreographerCallback(int fd, int events, void* data);
214 // Callback that will be run on vsync ticks.
Rachel Lee432b6912021-09-01 13:21:44 -0700215 static void extendedFrameCallback(const AChoreographerFrameCallbackData* cbData, void* data);
216 void frameCallback(int64_t vsyncId, int64_t frameDeadline, int64_t frameTimeNanos,
217 int64_t frameInterval);
Alec Mouri4a818f12019-11-19 16:17:53 -0800218 // Callback that will be run whenver there is a refresh rate change.
219 static void refreshRateCallback(int64_t vsyncPeriod, void* data);
John Recka733f892014-12-19 11:37:21 -0800220 void drainDisplayEventQueue();
John Recke45b1fd2014-04-15 09:50:16 -0700221 void dispatchFrameCallbacks();
John Recka5dda642014-05-22 15:43:54 -0700222 void requestVsync();
John Recke45b1fd2014-04-15 09:50:16 -0700223
Alec Mouri4a818f12019-11-19 16:17:53 -0800224 AChoreographer* mChoreographer;
John Reck56428472018-03-16 17:27:17 -0700225 VsyncSource* mVsyncSource;
John Recke45b1fd2014-04-15 09:50:16 -0700226 bool mVsyncRequested;
227 std::set<IFrameCallback*> mFrameCallbacks;
John Recka5dda642014-05-22 15:43:54 -0700228 // We defer the actual registration of these callbacks until
229 // both mQueue *and* mDisplayEventReceiver have been drained off all
230 // immediate events. This makes sure that we catch the next vsync, not
231 // the previous one
232 std::set<IFrameCallback*> mPendingRegistrationFrameCallbacks;
John Recke45b1fd2014-04-15 09:50:16 -0700233 bool mFrameCallbackTaskPending;
John Reck18f16e62014-05-02 16:46:41 -0700234
235 TimeLord mTimeLord;
John Reck3b202512014-06-23 13:13:08 -0700236 RenderState* mRenderState;
237 EglManager* mEglManager;
John Reck283bb462018-12-13 16:40:14 -0800238 WebViewFunctorManager& mFunctorManager;
John Reckba6adf62015-02-19 14:36:50 -0800239
John Reck34781b22017-07-05 16:39:36 -0700240 ProfileDataContainer mGlobalProfileData;
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500241 Readback* mReadback = nullptr;
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400242
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400243 sk_sp<GrDirectContext> mGrContext;
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400244 CacheManager* mCacheManager;
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400245 sp<VulkanManager> mVkManager;
Huihong Luo5fdf7b82021-01-15 14:27:06 -0800246
247 ASurfaceControlFunctions mASurfaceControlFunctions;
Jorim Jaggi71db8892021-02-03 23:19:29 +0100248 std::mutex mJankDataMutex;
John Reckcec24ae2013-11-05 13:27:50 -0800249};
250
251} /* namespace renderthread */
252} /* namespace uirenderer */
253} /* namespace android */
254#endif /* RENDERTHREAD_H_ */