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