blob: 215d29413f681f01b8b341471a7c152bf5fc67dd [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
John Reck18f16e62014-05-02 16:46:41 -070031#include "TimeLord.h"
32
John Reckcec24ae2013-11-05 13:27:50 -080033namespace android {
John Recke45b1fd2014-04-15 09:50:16 -070034class DisplayEventReceiver;
35
John Reckcec24ae2013-11-05 13:27:50 -080036namespace uirenderer {
37namespace renderthread {
38
John Recke45b1fd2014-04-15 09:50:16 -070039class DispatchFrameCallbacks;
40
John Reck4f02bf42014-01-03 18:09:17 -080041class TaskQueue {
42public:
43 TaskQueue();
44
45 RenderTask* next();
46 void queue(RenderTask* task);
47 RenderTask* peek();
48 void remove(RenderTask* task);
49
50private:
51 RenderTask* mHead;
52 RenderTask* mTail;
53};
54
John Recke45b1fd2014-04-15 09:50:16 -070055// Mimics android.view.Choreographer.FrameCallback
56class IFrameCallback {
57public:
John Reck18f16e62014-05-02 16:46:41 -070058 virtual void doFrame() = 0;
John Recke45b1fd2014-04-15 09:50:16 -070059
60protected:
61 ~IFrameCallback() {}
62};
63
John Reckcec24ae2013-11-05 13:27:50 -080064class ANDROID_API RenderThread : public Thread, public Singleton<RenderThread> {
65public:
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 Reck4f02bf42014-01-03 18:09:17 -080069 void queueDelayed(RenderTask* task, int delayMs);
70 void remove(RenderTask* task);
John Reckcec24ae2013-11-05 13:27:50 -080071
John Recke45b1fd2014-04-15 09:50:16 -070072 // Mimics android.view.Choreographer
73 void postFrameCallback(IFrameCallback* callback);
74 void removeFrameCallback(IFrameCallback* callback);
75
John Reck18f16e62014-05-02 16:46:41 -070076 TimeLord& timeLord() { return mTimeLord; }
77
John Reckcec24ae2013-11-05 13:27:50 -080078protected:
79 virtual bool threadLoop();
80
81private:
82 friend class Singleton<RenderThread>;
John Recke45b1fd2014-04-15 09:50:16 -070083 friend class DispatchFrameCallbacks;
John Reckcec24ae2013-11-05 13:27:50 -080084
85 RenderThread();
86 virtual ~RenderThread();
87
John Recke45b1fd2014-04-15 09:50:16 -070088 void initializeDisplayEventReceiver();
89 static int displayEventReceiverCallback(int fd, int events, void* data);
90 void drainDisplayEventQueue();
91 void dispatchFrameCallbacks();
92
John Reck4f02bf42014-01-03 18:09:17 -080093 // 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 Reckcec24ae2013-11-05 13:27:50 -080097
98 sp<Looper> mLooper;
99 Mutex mLock;
100
John Reck4f02bf42014-01-03 18:09:17 -0800101 nsecs_t mNextWakeup;
102 TaskQueue mQueue;
John Recke45b1fd2014-04-15 09:50:16 -0700103
104 DisplayEventReceiver* mDisplayEventReceiver;
105 bool mVsyncRequested;
106 std::set<IFrameCallback*> mFrameCallbacks;
107 bool mFrameCallbackTaskPending;
108 DispatchFrameCallbacks* mFrameCallbackTask;
John Reck18f16e62014-05-02 16:46:41 -0700109
110 TimeLord mTimeLord;
John Reckcec24ae2013-11-05 13:27:50 -0800111};
112
113} /* namespace renderthread */
114} /* namespace uirenderer */
115} /* namespace android */
116#endif /* RENDERTHREAD_H_ */