blob: b93dfd65fb342f8114212e7054ad875ebad02f00 [file] [log] [blame]
John Reckcec24ae2013-11-05 13:27:50 -08001/*
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 Recke45b1fd2014-04-15 09:50:16 -070021
22#include <memory>
23#include <set>
24
John Reckcec24ae2013-11-05 13:27:50 -080025#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
31namespace android {
John Recke45b1fd2014-04-15 09:50:16 -070032class DisplayEventReceiver;
33
John Reckcec24ae2013-11-05 13:27:50 -080034namespace uirenderer {
35namespace renderthread {
36
John Recke45b1fd2014-04-15 09:50:16 -070037class DispatchFrameCallbacks;
38
John Reck4f02bf42014-01-03 18:09:17 -080039class TaskQueue {
40public:
41 TaskQueue();
42
43 RenderTask* next();
44 void queue(RenderTask* task);
45 RenderTask* peek();
46 void remove(RenderTask* task);
47
48private:
49 RenderTask* mHead;
50 RenderTask* mTail;
51};
52
John Recke45b1fd2014-04-15 09:50:16 -070053// Mimics android.view.Choreographer.FrameCallback
54class IFrameCallback {
55public:
56 virtual void doFrame(nsecs_t frameTimeNanos) = 0;
57
58protected:
59 ~IFrameCallback() {}
60};
61
John Reckcec24ae2013-11-05 13:27:50 -080062class ANDROID_API RenderThread : public Thread, public Singleton<RenderThread> {
63public:
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 Reck4f02bf42014-01-03 18:09:17 -080067 void queueDelayed(RenderTask* task, int delayMs);
68 void remove(RenderTask* task);
John Reckcec24ae2013-11-05 13:27:50 -080069
John Recke45b1fd2014-04-15 09:50:16 -070070 // Mimics android.view.Choreographer
71 void postFrameCallback(IFrameCallback* callback);
72 void removeFrameCallback(IFrameCallback* callback);
73
John Reckcec24ae2013-11-05 13:27:50 -080074protected:
75 virtual bool threadLoop();
76
77private:
78 friend class Singleton<RenderThread>;
John Recke45b1fd2014-04-15 09:50:16 -070079 friend class DispatchFrameCallbacks;
John Reckcec24ae2013-11-05 13:27:50 -080080
81 RenderThread();
82 virtual ~RenderThread();
83
John Recke45b1fd2014-04-15 09:50:16 -070084 void initializeDisplayEventReceiver();
85 static int displayEventReceiverCallback(int fd, int events, void* data);
86 void drainDisplayEventQueue();
87 void dispatchFrameCallbacks();
88
John Reck4f02bf42014-01-03 18:09:17 -080089 // 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 Reckcec24ae2013-11-05 13:27:50 -080093
94 sp<Looper> mLooper;
95 Mutex mLock;
96
John Reck4f02bf42014-01-03 18:09:17 -080097 nsecs_t mNextWakeup;
98 TaskQueue mQueue;
John Recke45b1fd2014-04-15 09:50:16 -070099
100 DisplayEventReceiver* mDisplayEventReceiver;
101 bool mVsyncRequested;
102 std::set<IFrameCallback*> mFrameCallbacks;
103 bool mFrameCallbackTaskPending;
104 DispatchFrameCallbacks* mFrameCallbackTask;
105 nsecs_t mFrameTime;
John Reckcec24ae2013-11-05 13:27:50 -0800106};
107
108} /* namespace renderthread */
109} /* namespace uirenderer */
110} /* namespace android */
111#endif /* RENDERTHREAD_H_ */