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); |
| 47 | RenderTask* peek(); |
| 48 | void remove(RenderTask* task); |
| 49 | |
| 50 | private: |
| 51 | RenderTask* mHead; |
| 52 | RenderTask* mTail; |
| 53 | }; |
| 54 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 55 | // Mimics android.view.Choreographer.FrameCallback |
| 56 | class IFrameCallback { |
| 57 | public: |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 58 | virtual void doFrame() = 0; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 59 | |
| 60 | protected: |
| 61 | ~IFrameCallback() {} |
| 62 | }; |
| 63 | |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 64 | class ANDROID_API RenderThread : public Thread, public Singleton<RenderThread> { |
| 65 | public: |
| 66 | // RenderThread takes complete ownership of tasks that are queued |
| 67 | // and will delete them after they are run |
| 68 | ANDROID_API void queue(RenderTask* task); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 69 | void queueDelayed(RenderTask* task, int delayMs); |
| 70 | void remove(RenderTask* task); |
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); |
| 74 | void removeFrameCallback(IFrameCallback* callback); |
| 75 | |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 76 | TimeLord& timeLord() { return mTimeLord; } |
| 77 | |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 78 | protected: |
| 79 | virtual bool threadLoop(); |
| 80 | |
| 81 | private: |
| 82 | friend class Singleton<RenderThread>; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 83 | friend class DispatchFrameCallbacks; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 84 | |
| 85 | RenderThread(); |
| 86 | virtual ~RenderThread(); |
| 87 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 88 | void initializeDisplayEventReceiver(); |
| 89 | static int displayEventReceiverCallback(int fd, int events, void* data); |
| 90 | void drainDisplayEventQueue(); |
| 91 | void dispatchFrameCallbacks(); |
| 92 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 93 | // Returns the next task to be run. If this returns NULL nextWakeup is set |
| 94 | // to the time to requery for the nextTask to run. mNextWakeup is also |
| 95 | // set to this time |
| 96 | RenderTask* nextTask(nsecs_t* nextWakeup); |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 97 | |
| 98 | sp<Looper> mLooper; |
| 99 | Mutex mLock; |
| 100 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 101 | nsecs_t mNextWakeup; |
| 102 | TaskQueue mQueue; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 103 | |
| 104 | DisplayEventReceiver* mDisplayEventReceiver; |
| 105 | bool mVsyncRequested; |
| 106 | std::set<IFrameCallback*> mFrameCallbacks; |
| 107 | bool mFrameCallbackTaskPending; |
| 108 | DispatchFrameCallbacks* mFrameCallbackTask; |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 109 | |
| 110 | TimeLord mTimeLord; |
John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | } /* namespace renderthread */ |
| 114 | } /* namespace uirenderer */ |
| 115 | } /* namespace android */ |
| 116 | #endif /* RENDERTHREAD_H_ */ |