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