| 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 |  | 
| Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 20 | #include <GrContext.h> | 
|  | 21 | #include <SkBitmap.h> | 
| Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 22 | #include <apex/choreographer.h> | 
| Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 23 | #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 Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 31 |  | 
| Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 32 | #include "CacheManager.h" | 
| Fedor Kudasov | 85d6f10 | 2019-07-31 15:58:16 +0100 | [diff] [blame] | 33 | #include "ProfileDataContainer.h" | 
| Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 34 | #include "RenderTask.h" | 
| John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 35 | #include "TimeLord.h" | 
| John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 36 | #include "WebViewFunctorManager.h" | 
| John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 37 | #include "thread/ThreadBase.h" | 
| John Reck | 3c0f563 | 2019-03-15 16:36:01 -0700 | [diff] [blame] | 38 | #include "utils/TimeUtils.h" | 
| John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 39 |  | 
| John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 40 | namespace android { | 
| John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 41 |  | 
| Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 42 | class Bitmap; | 
| John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 43 |  | 
| John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 44 | namespace uirenderer { | 
| John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 45 |  | 
| Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 46 | class AutoBackendTextureRelease; | 
| Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 47 | class Readback; | 
| John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 48 | class RenderState; | 
| Chris Craik | 0a24b14 | 2015-10-19 17:10:19 -0700 | [diff] [blame] | 49 | class TestUtils; | 
| John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 50 |  | 
| Bo Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 51 | namespace skiapipeline { | 
|  | 52 | class VkFunctorDrawHandler; | 
|  | 53 | } | 
|  | 54 |  | 
| Stan Iliev | 52e4392 | 2019-08-06 15:59:44 -0400 | [diff] [blame] | 55 | namespace VectorDrawable { | 
|  | 56 | class Tree; | 
|  | 57 | } | 
|  | 58 |  | 
| John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 59 | namespace renderthread { | 
|  | 60 |  | 
| John Reck | 443a714 | 2014-09-04 17:40:05 -0700 | [diff] [blame] | 61 | class CanvasContext; | 
| John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 62 | class EglManager; | 
|  | 63 | class RenderProxy; | 
| Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 64 | class VulkanManager; | 
| John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 65 |  | 
| John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 66 | // Mimics android.view.Choreographer.FrameCallback | 
|  | 67 | class IFrameCallback { | 
|  | 68 | public: | 
| John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 69 | virtual void doFrame() = 0; | 
| John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 70 |  | 
|  | 71 | protected: | 
| Greg Kaiser | a95435b | 2018-08-23 16:58:18 -0700 | [diff] [blame] | 72 | virtual ~IFrameCallback() {} | 
| John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 73 | }; | 
|  | 74 |  | 
| John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 75 | struct VsyncSource { | 
|  | 76 | virtual void requestNextVsync() = 0; | 
| Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 77 | virtual void drainPendingEvents() = 0; | 
| John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 78 | virtual ~VsyncSource() {} | 
|  | 79 | }; | 
|  | 80 |  | 
| Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 81 | class ChoreographerSource; | 
| John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 82 | class DummyVsyncSource; | 
|  | 83 |  | 
| Stan Iliev | 978d532 | 2019-02-06 12:02:28 -0500 | [diff] [blame] | 84 | typedef void (*JVMAttachHook)(const char* name); | 
| Stan Iliev | 80dbc35 | 2019-02-05 15:31:28 -0500 | [diff] [blame] | 85 |  | 
| John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 86 | class RenderThread : private ThreadBase { | 
| John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 87 | PREVENT_COPY_AND_ASSIGN(RenderThread); | 
| John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 88 |  | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 89 | public: | 
| John Reck | 8785ceb | 2018-10-29 16:45:58 -0700 | [diff] [blame] | 90 | // Sets a callback that fires before any RenderThread setup has occurred. | 
| Stan Iliev | 80dbc35 | 2019-02-05 15:31:28 -0500 | [diff] [blame] | 91 | ANDROID_API static void setOnStartHook(JVMAttachHook onStartHook); | 
|  | 92 | static JVMAttachHook getOnStartHook(); | 
| John Reck | 259b25a | 2017-12-01 16:18:53 -0800 | [diff] [blame] | 93 |  | 
| John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 94 | WorkQueue& queue() { return ThreadBase::queue(); } | 
| John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 95 |  | 
| John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 96 | // Mimics android.view.Choreographer | 
|  | 97 | void postFrameCallback(IFrameCallback* callback); | 
| John Reck | 01a5ea3 | 2014-12-03 13:01:07 -0800 | [diff] [blame] | 98 | bool removeFrameCallback(IFrameCallback* callback); | 
| John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 99 | // 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 Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 102 |  | 
| John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 103 | TimeLord& timeLord() { return mTimeLord; } | 
| Derek Sollenberger | daf7229 | 2016-10-25 12:09:18 -0400 | [diff] [blame] | 104 | RenderState& renderState() const { return *mRenderState; } | 
|  | 105 | EglManager& eglManager() const { return *mEglManager; } | 
| John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 106 | ProfileDataContainer& globalProfileData() { return mGlobalProfileData; } | 
| Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 107 | Readback& readback(); | 
| John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 108 |  | 
| Derek Sollenberger | 98f75d5 | 2016-10-25 10:25:45 -0400 | [diff] [blame] | 109 | GrContext* getGrContext() const { return mGrContext.get(); } | 
| Greg Daniel | 660d6ec | 2017-12-08 11:44:27 -0500 | [diff] [blame] | 110 | void setGrContext(sk_sp<GrContext> cxt); | 
| Derek Sollenberger | 98f75d5 | 2016-10-25 10:25:45 -0400 | [diff] [blame] | 111 |  | 
| Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 112 | CacheManager& cacheManager() { return *mCacheManager; } | 
| Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 113 | VulkanManager& vulkanManager() { return *mVkManager; } | 
|  | 114 |  | 
| Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 115 | sk_sp<Bitmap> allocateHardwareBitmap(SkBitmap& skBitmap); | 
| Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 116 | void dumpGraphicsMemory(int fd); | 
| Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 117 |  | 
| John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 118 | void requireGlContext(); | 
| Stan Iliev | 981afe7 | 2019-02-13 14:24:33 -0500 | [diff] [blame] | 119 | void requireVkContext(); | 
| John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 120 | void destroyRenderingContext(); | 
| John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 121 |  | 
| Stan Iliev | 898123b | 2019-02-14 14:57:44 -0500 | [diff] [blame] | 122 | void preload(); | 
|  | 123 |  | 
| Stan Iliev | 6b894d7 | 2017-08-23 12:41:41 -0400 | [diff] [blame] | 124 | /** | 
|  | 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 Iliev | 981afe7 | 2019-02-13 14:24:33 -0500 | [diff] [blame] | 132 | static void initGrContextOptions(GrContextOptions& options); | 
|  | 133 |  | 
| John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 134 | protected: | 
| Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 135 | virtual bool threadLoop() override; | 
| John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 136 |  | 
|  | 137 | private: | 
| John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 138 | friend class DispatchFrameCallbacks; | 
| John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 139 | friend class RenderProxy; | 
| John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 140 | friend class DummyVsyncSource; | 
| Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 141 | friend class ChoreographerSource; | 
| Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 142 | friend class android::uirenderer::AutoBackendTextureRelease; | 
| Chris Craik | 0a24b14 | 2015-10-19 17:10:19 -0700 | [diff] [blame] | 143 | friend class android::uirenderer::TestUtils; | 
| Bo Liu | 1b0278c | 2019-01-03 16:36:24 -0800 | [diff] [blame] | 144 | friend class android::uirenderer::WebViewFunctor; | 
| Bo Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 145 | friend class android::uirenderer::skiapipeline::VkFunctorDrawHandler; | 
| Stan Iliev | 52e4392 | 2019-08-06 15:59:44 -0400 | [diff] [blame] | 146 | friend class android::uirenderer::VectorDrawable::Tree; | 
| John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 147 |  | 
|  | 148 | RenderThread(); | 
|  | 149 | virtual ~RenderThread(); | 
|  | 150 |  | 
| John Reck | 6b50780 | 2015-11-03 10:09:59 -0800 | [diff] [blame] | 151 | static bool hasInstance(); | 
|  | 152 | static RenderThread& getInstance(); | 
|  | 153 |  | 
| John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 154 | void initThreadLocals(); | 
| Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 155 | void initializeChoreographer(); | 
| John Reck | cf185f5 | 2019-04-11 16:11:24 -0700 | [diff] [blame] | 156 | void setupFrameInterval(); | 
| Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 157 | // 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 Reck | a733f89 | 2014-12-19 11:37:21 -0800 | [diff] [blame] | 165 | void drainDisplayEventQueue(); | 
| John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 166 | void dispatchFrameCallbacks(); | 
| John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 167 | void requestVsync(); | 
| John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 168 |  | 
| Alec Mouri | 4a818f1 | 2019-11-19 16:17:53 -0800 | [diff] [blame] | 169 | AChoreographer* mChoreographer; | 
| John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 170 | VsyncSource* mVsyncSource; | 
| John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 171 | bool mVsyncRequested; | 
|  | 172 | std::set<IFrameCallback*> mFrameCallbacks; | 
| John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 173 | // 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 Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 178 | bool mFrameCallbackTaskPending; | 
| John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 179 |  | 
|  | 180 | TimeLord mTimeLord; | 
| John Reck | 3c0f563 | 2019-03-15 16:36:01 -0700 | [diff] [blame] | 181 | nsecs_t mDispatchFrameDelay = 4_ms; | 
| John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 182 | RenderState* mRenderState; | 
|  | 183 | EglManager* mEglManager; | 
| John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 184 | WebViewFunctorManager& mFunctorManager; | 
| John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 185 |  | 
| John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 186 | ProfileDataContainer mGlobalProfileData; | 
| Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 187 | Readback* mReadback = nullptr; | 
| Derek Sollenberger | 98f75d5 | 2016-10-25 10:25:45 -0400 | [diff] [blame] | 188 |  | 
|  | 189 | sk_sp<GrContext> mGrContext; | 
| Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 190 | CacheManager* mCacheManager; | 
| Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 191 | VulkanManager* mVkManager; | 
| John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 192 | }; | 
|  | 193 |  | 
|  | 194 | } /* namespace renderthread */ | 
|  | 195 | } /* namespace uirenderer */ | 
|  | 196 | } /* namespace android */ | 
|  | 197 | #endif /* RENDERTHREAD_H_ */ |