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 | |
| 20 | #include "RenderTask.h" |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 21 | |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 22 | #include "CacheManager.h" |
Fedor Kudasov | 85d6f10 | 2019-07-31 15:58:16 +0100 | [diff] [blame^] | 23 | #include "ProfileDataContainer.h" |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 24 | #include "TimeLord.h" |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 25 | #include "WebViewFunctorManager.h" |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 26 | #include "thread/ThreadBase.h" |
John Reck | 3c0f563 | 2019-03-15 16:36:01 -0700 | [diff] [blame] | 27 | #include "utils/TimeUtils.h" |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 28 | |
Derek Sollenberger | 98f75d5 | 2016-10-25 10:25:45 -0400 | [diff] [blame] | 29 | #include <GrContext.h> |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 30 | #include <SkBitmap.h> |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 31 | #include <cutils/compiler.h> |
John Reck | b36016c | 2015-03-11 08:50:53 -0700 | [diff] [blame] | 32 | #include <ui/DisplayInfo.h> |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 33 | #include <utils/Looper.h> |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 34 | #include <utils/Thread.h> |
| 35 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 36 | #include <thread/ThreadBase.h> |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 37 | #include <memory> |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 38 | #include <mutex> |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 39 | #include <set> |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -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; |
Stan Iliev | ee3754a | 2019-03-15 12:07:35 -0400 | [diff] [blame] | 44 | class AutoBackendTextureRelease; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 45 | |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 46 | namespace uirenderer { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 47 | |
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 | |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 56 | namespace renderthread { |
| 57 | |
John Reck | 443a714 | 2014-09-04 17:40:05 -0700 | [diff] [blame] | 58 | class CanvasContext; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 59 | class EglManager; |
| 60 | class RenderProxy; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 61 | class VulkanManager; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 62 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 63 | // Mimics android.view.Choreographer.FrameCallback |
| 64 | class IFrameCallback { |
| 65 | public: |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 66 | virtual void doFrame() = 0; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 67 | |
| 68 | protected: |
Greg Kaiser | a95435b | 2018-08-23 16:58:18 -0700 | [diff] [blame] | 69 | virtual ~IFrameCallback() {} |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 70 | }; |
| 71 | |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 72 | struct VsyncSource { |
| 73 | virtual void requestNextVsync() = 0; |
| 74 | virtual nsecs_t latestVsyncEvent() = 0; |
| 75 | virtual ~VsyncSource() {} |
| 76 | }; |
| 77 | |
| 78 | class DummyVsyncSource; |
| 79 | |
Stan Iliev | 978d532 | 2019-02-06 12:02:28 -0500 | [diff] [blame] | 80 | typedef void (*JVMAttachHook)(const char* name); |
Stan Iliev | 80dbc35 | 2019-02-05 15:31:28 -0500 | [diff] [blame] | 81 | |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 82 | class RenderThread : private ThreadBase { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 83 | PREVENT_COPY_AND_ASSIGN(RenderThread); |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 84 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 85 | public: |
John Reck | 8785ceb | 2018-10-29 16:45:58 -0700 | [diff] [blame] | 86 | // Sets a callback that fires before any RenderThread setup has occurred. |
Stan Iliev | 80dbc35 | 2019-02-05 15:31:28 -0500 | [diff] [blame] | 87 | ANDROID_API static void setOnStartHook(JVMAttachHook onStartHook); |
| 88 | static JVMAttachHook getOnStartHook(); |
John Reck | 259b25a | 2017-12-01 16:18:53 -0800 | [diff] [blame] | 89 | |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 90 | WorkQueue& queue() { return ThreadBase::queue(); } |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 91 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 92 | // Mimics android.view.Choreographer |
| 93 | void postFrameCallback(IFrameCallback* callback); |
John Reck | 01a5ea3 | 2014-12-03 13:01:07 -0800 | [diff] [blame] | 94 | bool removeFrameCallback(IFrameCallback* callback); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 95 | // If the callback is currently registered, it will be pushed back until |
| 96 | // the next vsync. If it is not currently registered this does nothing. |
| 97 | void pushBackFrameCallback(IFrameCallback* callback); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 98 | |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 99 | TimeLord& timeLord() { return mTimeLord; } |
Derek Sollenberger | daf7229 | 2016-10-25 12:09:18 -0400 | [diff] [blame] | 100 | RenderState& renderState() const { return *mRenderState; } |
| 101 | EglManager& eglManager() const { return *mEglManager; } |
John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 102 | ProfileDataContainer& globalProfileData() { return mGlobalProfileData; } |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 103 | Readback& readback(); |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 104 | |
Derek Sollenberger | 98f75d5 | 2016-10-25 10:25:45 -0400 | [diff] [blame] | 105 | GrContext* getGrContext() const { return mGrContext.get(); } |
Greg Daniel | 660d6ec | 2017-12-08 11:44:27 -0500 | [diff] [blame] | 106 | void setGrContext(sk_sp<GrContext> cxt); |
Derek Sollenberger | 98f75d5 | 2016-10-25 10:25:45 -0400 | [diff] [blame] | 107 | |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 108 | CacheManager& cacheManager() { return *mCacheManager; } |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 109 | VulkanManager& vulkanManager() { return *mVkManager; } |
| 110 | |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 111 | sk_sp<Bitmap> allocateHardwareBitmap(SkBitmap& skBitmap); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 112 | void dumpGraphicsMemory(int fd); |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 113 | |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 114 | void requireGlContext(); |
Stan Iliev | 981afe7 | 2019-02-13 14:24:33 -0500 | [diff] [blame] | 115 | void requireVkContext(); |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 116 | void destroyRenderingContext(); |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 117 | |
Stan Iliev | 898123b | 2019-02-14 14:57:44 -0500 | [diff] [blame] | 118 | void preload(); |
| 119 | |
Stan Iliev | 6b894d7 | 2017-08-23 12:41:41 -0400 | [diff] [blame] | 120 | /** |
| 121 | * isCurrent provides a way to query, if the caller is running on |
| 122 | * the render thread. |
| 123 | * |
| 124 | * @return true only if isCurrent is invoked from the render thread. |
| 125 | */ |
| 126 | static bool isCurrent(); |
| 127 | |
Stan Iliev | 981afe7 | 2019-02-13 14:24:33 -0500 | [diff] [blame] | 128 | static void initGrContextOptions(GrContextOptions& options); |
| 129 | |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 130 | protected: |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 131 | virtual bool threadLoop() override; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 132 | |
| 133 | private: |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 134 | friend class DispatchFrameCallbacks; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 135 | friend class RenderProxy; |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 136 | friend class DummyVsyncSource; |
Stan Iliev | ee3754a | 2019-03-15 12:07:35 -0400 | [diff] [blame] | 137 | friend class android::AutoBackendTextureRelease; |
Chris Craik | 0a24b14 | 2015-10-19 17:10:19 -0700 | [diff] [blame] | 138 | friend class android::uirenderer::TestUtils; |
Bo Liu | 1b0278c | 2019-01-03 16:36:24 -0800 | [diff] [blame] | 139 | friend class android::uirenderer::WebViewFunctor; |
Bo Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 140 | friend class android::uirenderer::skiapipeline::VkFunctorDrawHandler; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 141 | |
| 142 | RenderThread(); |
| 143 | virtual ~RenderThread(); |
| 144 | |
John Reck | 6b50780 | 2015-11-03 10:09:59 -0800 | [diff] [blame] | 145 | static bool hasInstance(); |
| 146 | static RenderThread& getInstance(); |
| 147 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 148 | void initThreadLocals(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 149 | void initializeDisplayEventReceiver(); |
John Reck | cf185f5 | 2019-04-11 16:11:24 -0700 | [diff] [blame] | 150 | void setupFrameInterval(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 151 | static int displayEventReceiverCallback(int fd, int events, void* data); |
John Reck | a733f89 | 2014-12-19 11:37:21 -0800 | [diff] [blame] | 152 | void drainDisplayEventQueue(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 153 | void dispatchFrameCallbacks(); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 154 | void requestVsync(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 155 | |
John Reck | 5642847 | 2018-03-16 17:27:17 -0700 | [diff] [blame] | 156 | VsyncSource* mVsyncSource; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 157 | bool mVsyncRequested; |
| 158 | std::set<IFrameCallback*> mFrameCallbacks; |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 159 | // We defer the actual registration of these callbacks until |
| 160 | // both mQueue *and* mDisplayEventReceiver have been drained off all |
| 161 | // immediate events. This makes sure that we catch the next vsync, not |
| 162 | // the previous one |
| 163 | std::set<IFrameCallback*> mPendingRegistrationFrameCallbacks; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 164 | bool mFrameCallbackTaskPending; |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 165 | |
| 166 | TimeLord mTimeLord; |
John Reck | 3c0f563 | 2019-03-15 16:36:01 -0700 | [diff] [blame] | 167 | nsecs_t mDispatchFrameDelay = 4_ms; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 168 | RenderState* mRenderState; |
| 169 | EglManager* mEglManager; |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 170 | WebViewFunctorManager& mFunctorManager; |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 171 | |
John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 172 | ProfileDataContainer mGlobalProfileData; |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 173 | Readback* mReadback = nullptr; |
Derek Sollenberger | 98f75d5 | 2016-10-25 10:25:45 -0400 | [diff] [blame] | 174 | |
| 175 | sk_sp<GrContext> mGrContext; |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 176 | CacheManager* mCacheManager; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 177 | VulkanManager* mVkManager; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | } /* namespace renderthread */ |
| 181 | } /* namespace uirenderer */ |
| 182 | } /* namespace android */ |
| 183 | #endif /* RENDERTHREAD_H_ */ |