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 | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 26 | |
Derek Sollenberger | 98f75d5 | 2016-10-25 10:25:45 -0400 | [diff] [blame] | 27 | #include <GrContext.h> |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 28 | #include <SkBitmap.h> |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 29 | #include <cutils/compiler.h> |
John Reck | b36016c | 2015-03-11 08:50:53 -0700 | [diff] [blame] | 30 | #include <ui/DisplayInfo.h> |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 31 | #include <utils/Looper.h> |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 32 | #include <utils/Thread.h> |
| 33 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 34 | #include <thread/ThreadBase.h> |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 35 | #include <memory> |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 36 | #include <mutex> |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 37 | #include <set> |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 38 | |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 39 | namespace android { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 40 | |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 41 | class Bitmap; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 42 | class DisplayEventReceiver; |
| 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: |
| 63 | ~IFrameCallback() {} |
| 64 | }; |
| 65 | |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 66 | class RenderThread : private ThreadBase { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 67 | PREVENT_COPY_AND_ASSIGN(RenderThread); |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 68 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 69 | public: |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 70 | WorkQueue& queue() { return ThreadBase::queue(); } |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 71 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 72 | // Mimics android.view.Choreographer |
| 73 | void postFrameCallback(IFrameCallback* callback); |
John Reck | 01a5ea3 | 2014-12-03 13:01:07 -0800 | [diff] [blame] | 74 | bool removeFrameCallback(IFrameCallback* callback); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 75 | // If the callback is currently registered, it will be pushed back until |
| 76 | // the next vsync. If it is not currently registered this does nothing. |
| 77 | void pushBackFrameCallback(IFrameCallback* callback); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 78 | |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 79 | TimeLord& timeLord() { return mTimeLord; } |
Derek Sollenberger | daf7229 | 2016-10-25 12:09:18 -0400 | [diff] [blame] | 80 | RenderState& renderState() const { return *mRenderState; } |
| 81 | EglManager& eglManager() const { return *mEglManager; } |
John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 82 | ProfileDataContainer& globalProfileData() { return mGlobalProfileData; } |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 83 | Readback& readback(); |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 84 | |
John Reck | b36016c | 2015-03-11 08:50:53 -0700 | [diff] [blame] | 85 | const DisplayInfo& mainDisplayInfo() { return mDisplayInfo; } |
| 86 | |
Derek Sollenberger | 98f75d5 | 2016-10-25 10:25:45 -0400 | [diff] [blame] | 87 | GrContext* getGrContext() const { return mGrContext.get(); } |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 88 | void setGrContext(GrContext* cxt); |
Derek Sollenberger | 98f75d5 | 2016-10-25 10:25:45 -0400 | [diff] [blame] | 89 | |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 90 | CacheManager& cacheManager() { return *mCacheManager; } |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 91 | VulkanManager& vulkanManager() { return *mVkManager; } |
| 92 | |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 93 | sk_sp<Bitmap> allocateHardwareBitmap(SkBitmap& skBitmap); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 94 | void dumpGraphicsMemory(int fd); |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 95 | |
Stan Iliev | 6b894d7 | 2017-08-23 12:41:41 -0400 | [diff] [blame] | 96 | /** |
| 97 | * isCurrent provides a way to query, if the caller is running on |
| 98 | * the render thread. |
| 99 | * |
| 100 | * @return true only if isCurrent is invoked from the render thread. |
| 101 | */ |
| 102 | static bool isCurrent(); |
| 103 | |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 104 | protected: |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 105 | virtual bool threadLoop() override; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 106 | |
| 107 | private: |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 108 | friend class DispatchFrameCallbacks; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 109 | friend class RenderProxy; |
Chris Craik | 0a24b14 | 2015-10-19 17:10:19 -0700 | [diff] [blame] | 110 | friend class android::uirenderer::TestUtils; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 111 | |
| 112 | RenderThread(); |
| 113 | virtual ~RenderThread(); |
| 114 | |
John Reck | 6b50780 | 2015-11-03 10:09:59 -0800 | [diff] [blame] | 115 | static bool hasInstance(); |
| 116 | static RenderThread& getInstance(); |
| 117 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 118 | void initThreadLocals(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 119 | void initializeDisplayEventReceiver(); |
| 120 | static int displayEventReceiverCallback(int fd, int events, void* data); |
John Reck | a733f89 | 2014-12-19 11:37:21 -0800 | [diff] [blame] | 121 | void drainDisplayEventQueue(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 122 | void dispatchFrameCallbacks(); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 123 | void requestVsync(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 124 | |
John Reck | b36016c | 2015-03-11 08:50:53 -0700 | [diff] [blame] | 125 | DisplayInfo mDisplayInfo; |
| 126 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 127 | DisplayEventReceiver* mDisplayEventReceiver; |
| 128 | bool mVsyncRequested; |
| 129 | std::set<IFrameCallback*> mFrameCallbacks; |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 130 | // We defer the actual registration of these callbacks until |
| 131 | // both mQueue *and* mDisplayEventReceiver have been drained off all |
| 132 | // immediate events. This makes sure that we catch the next vsync, not |
| 133 | // the previous one |
| 134 | std::set<IFrameCallback*> mPendingRegistrationFrameCallbacks; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 135 | bool mFrameCallbackTaskPending; |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 136 | |
| 137 | TimeLord mTimeLord; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 138 | RenderState* mRenderState; |
| 139 | EglManager* mEglManager; |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 140 | |
John Reck | 34781b2 | 2017-07-05 16:39:36 -0700 | [diff] [blame] | 141 | ProfileDataContainer mGlobalProfileData; |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 142 | Readback* mReadback = nullptr; |
Derek Sollenberger | 98f75d5 | 2016-10-25 10:25:45 -0400 | [diff] [blame] | 143 | |
| 144 | sk_sp<GrContext> mGrContext; |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 145 | CacheManager* mCacheManager; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 146 | VulkanManager* mVkManager; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | } /* namespace renderthread */ |
| 150 | } /* namespace uirenderer */ |
| 151 | } /* namespace android */ |
| 152 | #endif /* RENDERTHREAD_H_ */ |