blob: dd060097ddf79938e2fadaed149fce8e7a5b62c9 [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
Jorim Jaggi71db8892021-02-03 23:19:29 +010020#include <surface_control_private.h>
Adlai Hollerf8c434e2020-07-27 11:42:45 -040021#include <GrDirectContext.h>
Stan Ilievaaa9e832019-09-17 14:07:23 -040022#include <SkBitmap.h>
23#include <cutils/compiler.h>
Steven Thomas6fabb5a2020-08-21 16:56:08 -070024#include <private/android/choreographer.h>
Stan Ilievaaa9e832019-09-17 14:07:23 -040025#include <thread/ThreadBase.h>
26#include <utils/Looper.h>
27#include <utils/Thread.h>
28
29#include <memory>
30#include <mutex>
31#include <set>
John Recke45b1fd2014-04-15 09:50:16 -070032
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040033#include "CacheManager.h"
Fedor Kudasov85d6f102019-07-31 15:58:16 +010034#include "ProfileDataContainer.h"
Stan Ilievaaa9e832019-09-17 14:07:23 -040035#include "RenderTask.h"
John Reckba6adf62015-02-19 14:36:50 -080036#include "TimeLord.h"
John Reck283bb462018-12-13 16:40:14 -080037#include "WebViewFunctorManager.h"
John Reck0fa0cbc2019-04-05 16:57:46 -070038#include "thread/ThreadBase.h"
John Reck3c0f5632019-03-15 16:36:01 -070039#include "utils/TimeUtils.h"
John Recke45b1fd2014-04-15 09:50:16 -070040
John Reckcec24ae2013-11-05 13:27:50 -080041namespace android {
John Reck3b202512014-06-23 13:13:08 -070042
Stan Iliev7bc3bc62017-05-24 13:28:36 -040043class Bitmap;
John Recke45b1fd2014-04-15 09:50:16 -070044
John Reckcec24ae2013-11-05 13:27:50 -080045namespace uirenderer {
John Reck3b202512014-06-23 13:13:08 -070046
Stan Ilievaaa9e832019-09-17 14:07:23 -040047class AutoBackendTextureRelease;
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -050048class Readback;
John Reck3b202512014-06-23 13:13:08 -070049class RenderState;
Chris Craik0a24b142015-10-19 17:10:19 -070050class TestUtils;
John Reck3b202512014-06-23 13:13:08 -070051
Bo Liu7b8c1eb2019-01-08 20:17:55 -080052namespace skiapipeline {
53class VkFunctorDrawHandler;
54}
55
Stan Iliev52e43922019-08-06 15:59:44 -040056namespace VectorDrawable {
57class Tree;
58}
59
John Reckcec24ae2013-11-05 13:27:50 -080060namespace renderthread {
61
John Reck443a7142014-09-04 17:40:05 -070062class CanvasContext;
John Reck3b202512014-06-23 13:13:08 -070063class EglManager;
64class RenderProxy;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050065class VulkanManager;
John Recke45b1fd2014-04-15 09:50:16 -070066
John Recke45b1fd2014-04-15 09:50:16 -070067// Mimics android.view.Choreographer.FrameCallback
68class IFrameCallback {
69public:
John Reck18f16e62014-05-02 16:46:41 -070070 virtual void doFrame() = 0;
John Recke45b1fd2014-04-15 09:50:16 -070071
72protected:
Greg Kaisera95435b2018-08-23 16:58:18 -070073 virtual ~IFrameCallback() {}
John Recke45b1fd2014-04-15 09:50:16 -070074};
75
John Reck56428472018-03-16 17:27:17 -070076struct VsyncSource {
77 virtual void requestNextVsync() = 0;
Alec Mouri4a818f12019-11-19 16:17:53 -080078 virtual void drainPendingEvents() = 0;
John Reck56428472018-03-16 17:27:17 -070079 virtual ~VsyncSource() {}
80};
81
Huihong Luo054b8d32021-02-24 18:48:12 -080082typedef ASurfaceControl* (*ASC_create)(ASurfaceControl* parent, const char* debug_name);
Huihong Luo5fdf7b82021-01-15 14:27:06 -080083typedef void (*ASC_acquire)(ASurfaceControl* control);
84typedef void (*ASC_release)(ASurfaceControl* control);
85
Jorim Jaggi71db8892021-02-03 23:19:29 +010086typedef void (*ASC_registerSurfaceStatsListener)(ASurfaceControl* control, void* context,
87 ASurfaceControl_SurfaceStatsListener func);
88typedef void (*ASC_unregisterSurfaceStatsListener)(void* context,
Huihong Luo054b8d32021-02-24 18:48:12 -080089 ASurfaceControl_SurfaceStatsListener func);
Jorim Jaggi71db8892021-02-03 23:19:29 +010090
91typedef int64_t (*ASCStats_getAcquireTime)(ASurfaceControlStats* stats);
92typedef uint64_t (*ASCStats_getFrameNumber)(ASurfaceControlStats* stats);
93
Huihong Luo054b8d32021-02-24 18:48:12 -080094typedef ASurfaceTransaction* (*AST_create)();
95typedef void (*AST_delete)(ASurfaceTransaction* transaction);
96typedef void (*AST_apply)(ASurfaceTransaction* transaction);
97typedef void (*AST_setVisibility)(ASurfaceTransaction* transaction,
98 ASurfaceControl* surface_control, int8_t visibility);
99
Huihong Luo5fdf7b82021-01-15 14:27:06 -0800100struct ASurfaceControlFunctions {
101 ASurfaceControlFunctions();
Huihong Luo054b8d32021-02-24 18:48:12 -0800102
103 ASC_create createFunc;
Huihong Luo5fdf7b82021-01-15 14:27:06 -0800104 ASC_acquire acquireFunc;
105 ASC_release releaseFunc;
Jorim Jaggi71db8892021-02-03 23:19:29 +0100106 ASC_registerSurfaceStatsListener registerListenerFunc;
107 ASC_unregisterSurfaceStatsListener unregisterListenerFunc;
108 ASCStats_getAcquireTime getAcquireTimeFunc;
109 ASCStats_getFrameNumber getFrameNumberFunc;
Huihong Luo054b8d32021-02-24 18:48:12 -0800110
111 AST_create transactionCreateFunc;
112 AST_delete transactionDeleteFunc;
113 AST_apply transactionApplyFunc;
114 AST_setVisibility transactionSetVisibilityFunc;
Huihong Luo5fdf7b82021-01-15 14:27:06 -0800115};
116
Alec Mouri4a818f12019-11-19 16:17:53 -0800117class ChoreographerSource;
John Reck56428472018-03-16 17:27:17 -0700118class DummyVsyncSource;
119
Stan Iliev978d5322019-02-06 12:02:28 -0500120typedef void (*JVMAttachHook)(const char* name);
Stan Iliev80dbc352019-02-05 15:31:28 -0500121
John Reckf8441e62017-10-23 13:10:41 -0700122class RenderThread : private ThreadBase {
John Reckdf1742e2017-01-19 15:56:21 -0800123 PREVENT_COPY_AND_ASSIGN(RenderThread);
John Reckf8441e62017-10-23 13:10:41 -0700124
John Reck1bcacfd2017-11-03 10:12:19 -0700125public:
John Reck8785ceb2018-10-29 16:45:58 -0700126 // Sets a callback that fires before any RenderThread setup has occurred.
Derek Sollenberger3fedf5a2020-02-21 13:07:28 -0500127 static void setOnStartHook(JVMAttachHook onStartHook);
Stan Iliev80dbc352019-02-05 15:31:28 -0500128 static JVMAttachHook getOnStartHook();
John Reck259b25a2017-12-01 16:18:53 -0800129
John Reckf8441e62017-10-23 13:10:41 -0700130 WorkQueue& queue() { return ThreadBase::queue(); }
John Reckcec24ae2013-11-05 13:27:50 -0800131
John Recke45b1fd2014-04-15 09:50:16 -0700132 // Mimics android.view.Choreographer
133 void postFrameCallback(IFrameCallback* callback);
John Reck01a5ea32014-12-03 13:01:07 -0800134 bool removeFrameCallback(IFrameCallback* callback);
John Recka5dda642014-05-22 15:43:54 -0700135 // If the callback is currently registered, it will be pushed back until
136 // the next vsync. If it is not currently registered this does nothing.
137 void pushBackFrameCallback(IFrameCallback* callback);
John Recke45b1fd2014-04-15 09:50:16 -0700138
John Reck18f16e62014-05-02 16:46:41 -0700139 TimeLord& timeLord() { return mTimeLord; }
Derek Sollenbergerdaf72292016-10-25 12:09:18 -0400140 RenderState& renderState() const { return *mRenderState; }
141 EglManager& eglManager() const { return *mEglManager; }
John Reck34781b22017-07-05 16:39:36 -0700142 ProfileDataContainer& globalProfileData() { return mGlobalProfileData; }
Jorim Jaggi71db8892021-02-03 23:19:29 +0100143 std::mutex& getJankDataMutex() { return mJankDataMutex; }
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500144 Readback& readback();
John Reck18f16e62014-05-02 16:46:41 -0700145
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400146 GrDirectContext* getGrContext() const { return mGrContext.get(); }
147 void setGrContext(sk_sp<GrDirectContext> cxt);
John Reck2abc5492021-05-18 00:34:26 -0400148 sk_sp<GrDirectContext> requireGrContext();
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400149
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400150 CacheManager& cacheManager() { return *mCacheManager; }
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400151 VulkanManager& vulkanManager();
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500152
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400153 sk_sp<Bitmap> allocateHardwareBitmap(SkBitmap& skBitmap);
John Reck66e06d42021-05-11 17:04:54 -0400154 void dumpGraphicsMemory(int fd, bool includeProfileData);
John Reck39207682021-05-12 19:10:47 -0400155 void getMemoryUsage(size_t* cpuUsage, size_t* gpuUsage);
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400156
John Reck1e510712018-04-23 08:15:03 -0700157 void requireGlContext();
Stan Iliev981afe72019-02-13 14:24:33 -0500158 void requireVkContext();
John Reck283bb462018-12-13 16:40:14 -0800159 void destroyRenderingContext();
John Reck1e510712018-04-23 08:15:03 -0700160
Stan Iliev898123b2019-02-14 14:57:44 -0500161 void preload();
162
Huihong Luo5fdf7b82021-01-15 14:27:06 -0800163 const ASurfaceControlFunctions& getASurfaceControlFunctions() {
164 return mASurfaceControlFunctions;
165 }
166
Stan Iliev6b894d72017-08-23 12:41:41 -0400167 /**
168 * isCurrent provides a way to query, if the caller is running on
169 * the render thread.
170 *
171 * @return true only if isCurrent is invoked from the render thread.
172 */
173 static bool isCurrent();
174
Stan Iliev981afe72019-02-13 14:24:33 -0500175 static void initGrContextOptions(GrContextOptions& options);
176
John Reckcec24ae2013-11-05 13:27:50 -0800177protected:
Chris Craikd41c4d82015-01-05 15:51:13 -0800178 virtual bool threadLoop() override;
John Reckcec24ae2013-11-05 13:27:50 -0800179
180private:
John Recke45b1fd2014-04-15 09:50:16 -0700181 friend class DispatchFrameCallbacks;
John Reck3b202512014-06-23 13:13:08 -0700182 friend class RenderProxy;
John Reck56428472018-03-16 17:27:17 -0700183 friend class DummyVsyncSource;
Alec Mouri4a818f12019-11-19 16:17:53 -0800184 friend class ChoreographerSource;
Stan Ilievaaa9e832019-09-17 14:07:23 -0400185 friend class android::uirenderer::AutoBackendTextureRelease;
Chris Craik0a24b142015-10-19 17:10:19 -0700186 friend class android::uirenderer::TestUtils;
Bo Liu1b0278c2019-01-03 16:36:24 -0800187 friend class android::uirenderer::WebViewFunctor;
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800188 friend class android::uirenderer::skiapipeline::VkFunctorDrawHandler;
Stan Iliev52e43922019-08-06 15:59:44 -0400189 friend class android::uirenderer::VectorDrawable::Tree;
Steven Moreland76ec3822021-04-02 16:26:03 +0000190 friend class sp<RenderThread>;
John Reckcec24ae2013-11-05 13:27:50 -0800191
192 RenderThread();
193 virtual ~RenderThread();
194
John Reck6b507802015-11-03 10:09:59 -0800195 static bool hasInstance();
196 static RenderThread& getInstance();
197
John Reck3b202512014-06-23 13:13:08 -0700198 void initThreadLocals();
Alec Mouri4a818f12019-11-19 16:17:53 -0800199 void initializeChoreographer();
John Reckcf185f52019-04-11 16:11:24 -0700200 void setupFrameInterval();
Alec Mouri4a818f12019-11-19 16:17:53 -0800201 // Callbacks for choreographer events:
202 // choreographerCallback will call AChoreograper_handleEvent to call the
203 // corresponding callbacks for each display event type
204 static int choreographerCallback(int fd, int events, void* data);
205 // Callback that will be run on vsync ticks.
206 static void frameCallback(int64_t frameTimeNanos, void* data);
207 // Callback that will be run whenver there is a refresh rate change.
208 static void refreshRateCallback(int64_t vsyncPeriod, void* data);
John Recka733f892014-12-19 11:37:21 -0800209 void drainDisplayEventQueue();
John Recke45b1fd2014-04-15 09:50:16 -0700210 void dispatchFrameCallbacks();
John Recka5dda642014-05-22 15:43:54 -0700211 void requestVsync();
John Recke45b1fd2014-04-15 09:50:16 -0700212
Alec Mouri4a818f12019-11-19 16:17:53 -0800213 AChoreographer* mChoreographer;
John Reck56428472018-03-16 17:27:17 -0700214 VsyncSource* mVsyncSource;
John Recke45b1fd2014-04-15 09:50:16 -0700215 bool mVsyncRequested;
216 std::set<IFrameCallback*> mFrameCallbacks;
John Recka5dda642014-05-22 15:43:54 -0700217 // We defer the actual registration of these callbacks until
218 // both mQueue *and* mDisplayEventReceiver have been drained off all
219 // immediate events. This makes sure that we catch the next vsync, not
220 // the previous one
221 std::set<IFrameCallback*> mPendingRegistrationFrameCallbacks;
John Recke45b1fd2014-04-15 09:50:16 -0700222 bool mFrameCallbackTaskPending;
John Reck18f16e62014-05-02 16:46:41 -0700223
224 TimeLord mTimeLord;
John Reck3c0f5632019-03-15 16:36:01 -0700225 nsecs_t mDispatchFrameDelay = 4_ms;
John Reck3b202512014-06-23 13:13:08 -0700226 RenderState* mRenderState;
227 EglManager* mEglManager;
John Reck283bb462018-12-13 16:40:14 -0800228 WebViewFunctorManager& mFunctorManager;
John Reckba6adf62015-02-19 14:36:50 -0800229
John Reck34781b22017-07-05 16:39:36 -0700230 ProfileDataContainer mGlobalProfileData;
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500231 Readback* mReadback = nullptr;
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400232
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400233 sk_sp<GrDirectContext> mGrContext;
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400234 CacheManager* mCacheManager;
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400235 sp<VulkanManager> mVkManager;
Huihong Luo5fdf7b82021-01-15 14:27:06 -0800236
237 ASurfaceControlFunctions mASurfaceControlFunctions;
Jorim Jaggi71db8892021-02-03 23:19:29 +0100238 std::mutex mJankDataMutex;
John Reckcec24ae2013-11-05 13:27:50 -0800239};
240
241} /* namespace renderthread */
242} /* namespace uirenderer */
243} /* namespace android */
244#endif /* RENDERTHREAD_H_ */