John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 1 | /* |
| 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 Jaggi | 71db889 | 2021-02-03 23:19:29 +0100 | [diff] [blame] | 20 | #include <surface_control_private.h> |
Adlai Holler | f8c434e | 2020-07-27 11:42:45 -0400 | [diff] [blame] | 21 | #include <GrDirectContext.h> |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 22 | #include <SkBitmap.h> |
| 23 | #include <cutils/compiler.h> |
Steven Thomas | 6fabb5a | 2020-08-21 16:56:08 -0700 | [diff] [blame] | 24 | #include <private/android/choreographer.h> |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 25 | #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 Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 32 | |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 33 | #include "CacheManager.h" |
Fedor Kudasov | 85d6f10 | 2019-07-31 15:58:16 +0100 | [diff] [blame] | 34 | #include "ProfileDataContainer.h" |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 35 | #include "RenderTask.h" |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 36 | #include "TimeLord.h" |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 37 | #include "WebViewFunctorManager.h" |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 38 | #include "thread/ThreadBase.h" |
John Reck | 3c0f563 | 2019-03-15 16:36:01 -0700 | [diff] [blame] | 39 | #include "utils/TimeUtils.h" |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 40 | |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 41 | namespace android { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 42 | |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 43 | class Bitmap; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 44 | |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 45 | namespace uirenderer { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 46 | |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 47 | class AutoBackendTextureRelease; |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 48 | class Readback; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 49 | class RenderState; |
Chris Craik | 0a24b14 | 2015-10-19 17:10:19 -0700 | [diff] [blame] | 50 | class TestUtils; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 51 | |
Bo Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 52 | namespace skiapipeline { |
| 53 | class VkFunctorDrawHandler; |
| 54 | } |
| 55 | |
Stan Iliev | 52e4392 | 2019-08-06 15:59:44 -0400 | [diff] [blame] | 56 | namespace VectorDrawable { |
| 57 | class Tree; |
| 58 | } |
| 59 | |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 60 | namespace renderthread { |
| 61 | |
John Reck | 443a714 | 2014-09-04 17:40:05 -0700 | [diff] [blame] | 62 | class CanvasContext; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 63 | class EglManager; |
| 64 | class RenderProxy; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 65 | class VulkanManager; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 66 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 67 | // Mimics android.view.Choreographer.FrameCallback |
| 68 | class IFrameCallback { |
| 69 | public: |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 70 | virtual void doFrame() = 0; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 71 | |
| 72 | protected: |
Greg Kaiser | a95435b | 2018-08-23 16:58:18 -0700 | [diff] [blame] | 73 | virtual ~IFrameCallback() {} |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 74 | }; |
| 75 | |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 76 | struct VsyncSource { |
| 77 | virtual void requestNextVsync() = 0; |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 78 | virtual void drainPendingEvents() = 0; |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 79 | virtual ~VsyncSource() {} |
| 80 | }; |
| 81 | |
Huihong Luo | 5fdf7b8 | 2021-01-15 14:27:06 -0800 | [diff] [blame] | 82 | typedef void (*ASC_acquire)(ASurfaceControl* control); |
| 83 | typedef void (*ASC_release)(ASurfaceControl* control); |
| 84 | |
Jorim Jaggi | 71db889 | 2021-02-03 23:19:29 +0100 | [diff] [blame] | 85 | typedef void (*ASC_registerSurfaceStatsListener)(ASurfaceControl* control, void* context, |
| 86 | ASurfaceControl_SurfaceStatsListener func); |
| 87 | typedef void (*ASC_unregisterSurfaceStatsListener)(void* context, |
| 88 | ASurfaceControl_SurfaceStatsListener func); |
| 89 | |
| 90 | typedef int64_t (*ASCStats_getAcquireTime)(ASurfaceControlStats* stats); |
| 91 | typedef uint64_t (*ASCStats_getFrameNumber)(ASurfaceControlStats* stats); |
| 92 | |
Huihong Luo | 5fdf7b8 | 2021-01-15 14:27:06 -0800 | [diff] [blame] | 93 | struct ASurfaceControlFunctions { |
| 94 | ASurfaceControlFunctions(); |
Huihong Luo | 5fdf7b8 | 2021-01-15 14:27:06 -0800 | [diff] [blame] | 95 | ASC_acquire acquireFunc; |
| 96 | ASC_release releaseFunc; |
Jorim Jaggi | 71db889 | 2021-02-03 23:19:29 +0100 | [diff] [blame] | 97 | ASC_registerSurfaceStatsListener registerListenerFunc; |
| 98 | ASC_unregisterSurfaceStatsListener unregisterListenerFunc; |
| 99 | ASCStats_getAcquireTime getAcquireTimeFunc; |
| 100 | ASCStats_getFrameNumber getFrameNumberFunc; |
Huihong Luo | 5fdf7b8 | 2021-01-15 14:27:06 -0800 | [diff] [blame] | 101 | }; |
| 102 | |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 103 | class ChoreographerSource; |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 104 | class DummyVsyncSource; |
| 105 | |
Stan Iliev | 978d532 | 2019-02-06 12:02:28 -0500 | [diff] [blame] | 106 | typedef void (*JVMAttachHook)(const char* name); |
Stan Iliev | 80dbc35 | 2019-02-05 15:31:28 -0500 | [diff] [blame] | 107 | |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 108 | class RenderThread : private ThreadBase { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 109 | PREVENT_COPY_AND_ASSIGN(RenderThread); |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 110 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 111 | public: |
John Reck | 8785ceb | 2018-10-29 16:45:58 -0700 | [diff] [blame] | 112 | // Sets a callback that fires before any RenderThread setup has occurred. |
Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 113 | static void setOnStartHook(JVMAttachHook onStartHook); |
Stan Iliev | 80dbc35 | 2019-02-05 15:31:28 -0500 | [diff] [blame] | 114 | static JVMAttachHook getOnStartHook(); |
John Reck | 259b25a | 2017-12-01 16:18:53 -0800 | [diff] [blame] | 115 | |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 116 | WorkQueue& queue() { return ThreadBase::queue(); } |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 117 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 118 | // Mimics android.view.Choreographer |
| 119 | void postFrameCallback(IFrameCallback* callback); |
John Reck | 01a5ea3 | 2014-12-03 13:01:07 -0800 | [diff] [blame] | 120 | bool removeFrameCallback(IFrameCallback* callback); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 121 | // If the callback is currently registered, it will be pushed back until |
| 122 | // the next vsync. If it is not currently registered this does nothing. |
| 123 | void pushBackFrameCallback(IFrameCallback* callback); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 124 | |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 125 | TimeLord& timeLord() { return mTimeLord; } |
Derek Sollenberger | daf7229 | 2016-10-25 12:09:18 -0400 | [diff] [blame] | 126 | RenderState& renderState() const { return *mRenderState; } |
| 127 | EglManager& eglManager() const { return *mEglManager; } |
John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 128 | ProfileDataContainer& globalProfileData() { return mGlobalProfileData; } |
Jorim Jaggi | 71db889 | 2021-02-03 23:19:29 +0100 | [diff] [blame] | 129 | std::mutex& getJankDataMutex() { return mJankDataMutex; } |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 130 | Readback& readback(); |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 131 | |
Adlai Holler | f8c434e | 2020-07-27 11:42:45 -0400 | [diff] [blame] | 132 | GrDirectContext* getGrContext() const { return mGrContext.get(); } |
| 133 | void setGrContext(sk_sp<GrDirectContext> cxt); |
Derek Sollenberger | 98f75d5 | 2016-10-25 10:25:45 -0400 | [diff] [blame] | 134 | |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 135 | CacheManager& cacheManager() { return *mCacheManager; } |
Derek Sollenberger | 802fefa | 2020-08-13 16:53:30 -0400 | [diff] [blame] | 136 | VulkanManager& vulkanManager(); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 137 | |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 138 | sk_sp<Bitmap> allocateHardwareBitmap(SkBitmap& skBitmap); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 139 | void dumpGraphicsMemory(int fd); |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 140 | |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 141 | void requireGlContext(); |
Stan Iliev | 981afe7 | 2019-02-13 14:24:33 -0500 | [diff] [blame] | 142 | void requireVkContext(); |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 143 | void destroyRenderingContext(); |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 144 | |
Stan Iliev | 898123b | 2019-02-14 14:57:44 -0500 | [diff] [blame] | 145 | void preload(); |
| 146 | |
Huihong Luo | 5fdf7b8 | 2021-01-15 14:27:06 -0800 | [diff] [blame] | 147 | const ASurfaceControlFunctions& getASurfaceControlFunctions() { |
| 148 | return mASurfaceControlFunctions; |
| 149 | } |
| 150 | |
Stan Iliev | 6b894d7 | 2017-08-23 12:41:41 -0400 | [diff] [blame] | 151 | /** |
| 152 | * isCurrent provides a way to query, if the caller is running on |
| 153 | * the render thread. |
| 154 | * |
| 155 | * @return true only if isCurrent is invoked from the render thread. |
| 156 | */ |
| 157 | static bool isCurrent(); |
| 158 | |
Stan Iliev | 981afe7 | 2019-02-13 14:24:33 -0500 | [diff] [blame] | 159 | static void initGrContextOptions(GrContextOptions& options); |
| 160 | |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 161 | protected: |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 162 | virtual bool threadLoop() override; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 163 | |
| 164 | private: |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 165 | friend class DispatchFrameCallbacks; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 166 | friend class RenderProxy; |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 167 | friend class DummyVsyncSource; |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 168 | friend class ChoreographerSource; |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 169 | friend class android::uirenderer::AutoBackendTextureRelease; |
Chris Craik | 0a24b14 | 2015-10-19 17:10:19 -0700 | [diff] [blame] | 170 | friend class android::uirenderer::TestUtils; |
Bo Liu | 1b0278c | 2019-01-03 16:36:24 -0800 | [diff] [blame] | 171 | friend class android::uirenderer::WebViewFunctor; |
Bo Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 172 | friend class android::uirenderer::skiapipeline::VkFunctorDrawHandler; |
Stan Iliev | 52e4392 | 2019-08-06 15:59:44 -0400 | [diff] [blame] | 173 | friend class android::uirenderer::VectorDrawable::Tree; |
Steven Moreland | 76ec382 | 2021-04-02 16:26:03 +0000 | [diff] [blame] | 174 | friend class sp<RenderThread>; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 175 | |
| 176 | RenderThread(); |
| 177 | virtual ~RenderThread(); |
| 178 | |
John Reck | 6b50780 | 2015-11-03 10:09:59 -0800 | [diff] [blame] | 179 | static bool hasInstance(); |
| 180 | static RenderThread& getInstance(); |
| 181 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 182 | void initThreadLocals(); |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 183 | void initializeChoreographer(); |
John Reck | cf185f5 | 2019-04-11 16:11:24 -0700 | [diff] [blame] | 184 | void setupFrameInterval(); |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 185 | // Callbacks for choreographer events: |
| 186 | // choreographerCallback will call AChoreograper_handleEvent to call the |
| 187 | // corresponding callbacks for each display event type |
| 188 | static int choreographerCallback(int fd, int events, void* data); |
| 189 | // Callback that will be run on vsync ticks. |
| 190 | static void frameCallback(int64_t frameTimeNanos, void* data); |
| 191 | // Callback that will be run whenver there is a refresh rate change. |
| 192 | static void refreshRateCallback(int64_t vsyncPeriod, void* data); |
John Reck | a733f89 | 2014-12-19 11:37:21 -0800 | [diff] [blame] | 193 | void drainDisplayEventQueue(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 194 | void dispatchFrameCallbacks(); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 195 | void requestVsync(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 196 | |
Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 197 | AChoreographer* mChoreographer; |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 198 | VsyncSource* mVsyncSource; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 199 | bool mVsyncRequested; |
| 200 | std::set<IFrameCallback*> mFrameCallbacks; |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 201 | // We defer the actual registration of these callbacks until |
| 202 | // both mQueue *and* mDisplayEventReceiver have been drained off all |
| 203 | // immediate events. This makes sure that we catch the next vsync, not |
| 204 | // the previous one |
| 205 | std::set<IFrameCallback*> mPendingRegistrationFrameCallbacks; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 206 | bool mFrameCallbackTaskPending; |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 207 | |
| 208 | TimeLord mTimeLord; |
John Reck | 3c0f563 | 2019-03-15 16:36:01 -0700 | [diff] [blame] | 209 | nsecs_t mDispatchFrameDelay = 4_ms; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 210 | RenderState* mRenderState; |
| 211 | EglManager* mEglManager; |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 212 | WebViewFunctorManager& mFunctorManager; |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 213 | |
John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 214 | ProfileDataContainer mGlobalProfileData; |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 215 | Readback* mReadback = nullptr; |
Derek Sollenberger | 98f75d5 | 2016-10-25 10:25:45 -0400 | [diff] [blame] | 216 | |
Adlai Holler | f8c434e | 2020-07-27 11:42:45 -0400 | [diff] [blame] | 217 | sk_sp<GrDirectContext> mGrContext; |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 218 | CacheManager* mCacheManager; |
Derek Sollenberger | 802fefa | 2020-08-13 16:53:30 -0400 | [diff] [blame] | 219 | sp<VulkanManager> mVkManager; |
Huihong Luo | 5fdf7b8 | 2021-01-15 14:27:06 -0800 | [diff] [blame] | 220 | |
| 221 | ASurfaceControlFunctions mASurfaceControlFunctions; |
Jorim Jaggi | 71db889 | 2021-02-03 23:19:29 +0100 | [diff] [blame] | 222 | std::mutex mJankDataMutex; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 223 | }; |
| 224 | |
| 225 | } /* namespace renderthread */ |
| 226 | } /* namespace uirenderer */ |
| 227 | } /* namespace android */ |
| 228 | #endif /* RENDERTHREAD_H_ */ |