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 | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 25 | |
Derek Sollenberger | 98f75d5 | 2016-10-25 10:25:45 -0400 | [diff] [blame] | 26 | #include <GrContext.h> |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 27 | #include <cutils/compiler.h> |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 28 | #include <SkBitmap.h> |
John Reck | b36016c | 2015-03-11 08:50:53 -0700 | [diff] [blame] | 29 | #include <ui/DisplayInfo.h> |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 30 | #include <utils/Looper.h> |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 31 | #include <utils/Thread.h> |
| 32 | |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 33 | #include <memory> |
| 34 | #include <set> |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 35 | |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 36 | namespace android { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 37 | |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 38 | class Bitmap; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 39 | class DisplayEventReceiver; |
| 40 | |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 41 | namespace uirenderer { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 42 | |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 43 | class Readback; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 44 | class RenderState; |
Chris Craik | 0a24b14 | 2015-10-19 17:10:19 -0700 | [diff] [blame] | 45 | class TestUtils; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 46 | |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 47 | namespace renderthread { |
| 48 | |
John Reck | 443a714 | 2014-09-04 17:40:05 -0700 | [diff] [blame] | 49 | class CanvasContext; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 50 | class DispatchFrameCallbacks; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 51 | class EglManager; |
| 52 | class RenderProxy; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 53 | class VulkanManager; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 54 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 55 | class TaskQueue { |
| 56 | public: |
| 57 | TaskQueue(); |
| 58 | |
| 59 | RenderTask* next(); |
| 60 | void queue(RenderTask* task); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 61 | void queueAtFront(RenderTask* task); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 62 | RenderTask* peek(); |
| 63 | void remove(RenderTask* task); |
| 64 | |
| 65 | private: |
| 66 | RenderTask* mHead; |
| 67 | RenderTask* mTail; |
| 68 | }; |
| 69 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 70 | // Mimics android.view.Choreographer.FrameCallback |
| 71 | class IFrameCallback { |
| 72 | public: |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 73 | virtual void doFrame() = 0; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 74 | |
| 75 | protected: |
| 76 | ~IFrameCallback() {} |
| 77 | }; |
| 78 | |
John Reck | 6b50780 | 2015-11-03 10:09:59 -0800 | [diff] [blame] | 79 | class ANDROID_API RenderThread : public Thread { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 80 | PREVENT_COPY_AND_ASSIGN(RenderThread); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 81 | public: |
| 82 | // RenderThread takes complete ownership of tasks that are queued |
| 83 | // and will delete them after they are run |
| 84 | ANDROID_API void queue(RenderTask* task); |
Chris Craik | 0a24b14 | 2015-10-19 17:10:19 -0700 | [diff] [blame] | 85 | ANDROID_API void queueAndWait(RenderTask* task); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 86 | ANDROID_API void queueAtFront(RenderTask* task); |
John Reck | a733f89 | 2014-12-19 11:37:21 -0800 | [diff] [blame] | 87 | void queueAt(RenderTask* task, nsecs_t runAtNs); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 88 | void remove(RenderTask* task); |
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 | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 100 | JankTracker& jankTracker() { return *mJankTracker; } |
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(); } |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 106 | void setGrContext(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 | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 114 | protected: |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 115 | virtual bool threadLoop() override; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 116 | |
| 117 | private: |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 118 | friend class DispatchFrameCallbacks; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 119 | friend class RenderProxy; |
Chris Craik | 0a24b14 | 2015-10-19 17:10:19 -0700 | [diff] [blame] | 120 | friend class android::uirenderer::TestUtils; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 121 | |
| 122 | RenderThread(); |
| 123 | virtual ~RenderThread(); |
| 124 | |
John Reck | 6b50780 | 2015-11-03 10:09:59 -0800 | [diff] [blame] | 125 | static bool hasInstance(); |
| 126 | static RenderThread& getInstance(); |
| 127 | |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 128 | void initThreadLocals(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 129 | void initializeDisplayEventReceiver(); |
| 130 | static int displayEventReceiverCallback(int fd, int events, void* data); |
John Reck | a733f89 | 2014-12-19 11:37:21 -0800 | [diff] [blame] | 131 | void drainDisplayEventQueue(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 132 | void dispatchFrameCallbacks(); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 133 | void requestVsync(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 134 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 135 | // Returns the next task to be run. If this returns NULL nextWakeup is set |
| 136 | // to the time to requery for the nextTask to run. mNextWakeup is also |
| 137 | // set to this time |
| 138 | RenderTask* nextTask(nsecs_t* nextWakeup); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 139 | |
| 140 | sp<Looper> mLooper; |
| 141 | Mutex mLock; |
| 142 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 143 | nsecs_t mNextWakeup; |
| 144 | TaskQueue mQueue; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 145 | |
John Reck | b36016c | 2015-03-11 08:50:53 -0700 | [diff] [blame] | 146 | DisplayInfo mDisplayInfo; |
| 147 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 148 | DisplayEventReceiver* mDisplayEventReceiver; |
| 149 | bool mVsyncRequested; |
| 150 | std::set<IFrameCallback*> mFrameCallbacks; |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 151 | // We defer the actual registration of these callbacks until |
| 152 | // both mQueue *and* mDisplayEventReceiver have been drained off all |
| 153 | // immediate events. This makes sure that we catch the next vsync, not |
| 154 | // the previous one |
| 155 | std::set<IFrameCallback*> mPendingRegistrationFrameCallbacks; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 156 | bool mFrameCallbackTaskPending; |
| 157 | DispatchFrameCallbacks* mFrameCallbackTask; |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 158 | |
| 159 | TimeLord mTimeLord; |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 160 | RenderState* mRenderState; |
| 161 | EglManager* mEglManager; |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 162 | |
| 163 | JankTracker* mJankTracker = nullptr; |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 164 | Readback* mReadback = nullptr; |
Derek Sollenberger | 98f75d5 | 2016-10-25 10:25:45 -0400 | [diff] [blame] | 165 | |
| 166 | sk_sp<GrContext> mGrContext; |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 167 | CacheManager* mCacheManager; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 168 | VulkanManager* mVkManager; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | } /* namespace renderthread */ |
| 172 | } /* namespace uirenderer */ |
| 173 | } /* namespace android */ |
| 174 | #endif /* RENDERTHREAD_H_ */ |