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