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