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