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