blob: 59843736f048690f00e9e93a4f75cb81cfa4e8b4 [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>
John Reck73b7a4d2014-07-23 14:54:04 -070026#include <utils/Condition.h>
John Reckcec24ae2013-11-05 13:27:50 -080027#include <utils/Looper.h>
28#include <utils/Mutex.h>
29#include <utils/Singleton.h>
30#include <utils/Thread.h>
31
John Reck18f16e62014-05-02 16:46:41 -070032#include "TimeLord.h"
33
John Reckcec24ae2013-11-05 13:27:50 -080034namespace android {
John Reck3b202512014-06-23 13:13:08 -070035
John Recke45b1fd2014-04-15 09:50:16 -070036class DisplayEventReceiver;
37
John Reckcec24ae2013-11-05 13:27:50 -080038namespace uirenderer {
John Reck3b202512014-06-23 13:13:08 -070039
40class RenderState;
41
John Reckcec24ae2013-11-05 13:27:50 -080042namespace renderthread {
43
John Recke45b1fd2014-04-15 09:50:16 -070044class DispatchFrameCallbacks;
John Reck3b202512014-06-23 13:13:08 -070045class EglManager;
46class RenderProxy;
John Recke45b1fd2014-04-15 09:50:16 -070047
John Reck4f02bf42014-01-03 18:09:17 -080048class TaskQueue {
49public:
50 TaskQueue();
51
52 RenderTask* next();
53 void queue(RenderTask* task);
John Recka5dda642014-05-22 15:43:54 -070054 void queueAtFront(RenderTask* task);
John Reck4f02bf42014-01-03 18:09:17 -080055 RenderTask* peek();
56 void remove(RenderTask* task);
57
58private:
59 RenderTask* mHead;
60 RenderTask* mTail;
61};
62
John Recke45b1fd2014-04-15 09:50:16 -070063// Mimics android.view.Choreographer.FrameCallback
64class IFrameCallback {
65public:
John Reck18f16e62014-05-02 16:46:41 -070066 virtual void doFrame() = 0;
John Recke45b1fd2014-04-15 09:50:16 -070067
68protected:
69 ~IFrameCallback() {}
70};
71
John Reck3b202512014-06-23 13:13:08 -070072class ANDROID_API RenderThread : public Thread, protected Singleton<RenderThread> {
John Reckcec24ae2013-11-05 13:27:50 -080073public:
74 // RenderThread takes complete ownership of tasks that are queued
75 // and will delete them after they are run
76 ANDROID_API void queue(RenderTask* task);
John Reck73b7a4d2014-07-23 14:54:04 -070077 void queueAndWait(RenderTask* task, Condition& signal, Mutex& lock);
John Recka5dda642014-05-22 15:43:54 -070078 ANDROID_API void queueAtFront(RenderTask* task);
John Reck4f02bf42014-01-03 18:09:17 -080079 void queueDelayed(RenderTask* task, int delayMs);
80 void remove(RenderTask* task);
John Reckcec24ae2013-11-05 13:27:50 -080081
John Recke45b1fd2014-04-15 09:50:16 -070082 // Mimics android.view.Choreographer
83 void postFrameCallback(IFrameCallback* callback);
84 void removeFrameCallback(IFrameCallback* callback);
John Recka5dda642014-05-22 15:43:54 -070085 // If the callback is currently registered, it will be pushed back until
86 // the next vsync. If it is not currently registered this does nothing.
87 void pushBackFrameCallback(IFrameCallback* callback);
John Recke45b1fd2014-04-15 09:50:16 -070088
John Reck18f16e62014-05-02 16:46:41 -070089 TimeLord& timeLord() { return mTimeLord; }
John Reck3b202512014-06-23 13:13:08 -070090 RenderState& renderState() { return *mRenderState; }
91 EglManager& eglManager() { return *mEglManager; }
John Reck18f16e62014-05-02 16:46:41 -070092
John Reckcec24ae2013-11-05 13:27:50 -080093protected:
94 virtual bool threadLoop();
95
96private:
97 friend class Singleton<RenderThread>;
John Recke45b1fd2014-04-15 09:50:16 -070098 friend class DispatchFrameCallbacks;
John Reck3b202512014-06-23 13:13:08 -070099 friend class RenderProxy;
John Reckcec24ae2013-11-05 13:27:50 -0800100
101 RenderThread();
102 virtual ~RenderThread();
103
John Reck3b202512014-06-23 13:13:08 -0700104 void initThreadLocals();
John Recke45b1fd2014-04-15 09:50:16 -0700105 void initializeDisplayEventReceiver();
106 static int displayEventReceiverCallback(int fd, int events, void* data);
John Recka5dda642014-05-22 15:43:54 -0700107 void drainDisplayEventQueue(bool skipCallbacks = false);
John Recke45b1fd2014-04-15 09:50:16 -0700108 void dispatchFrameCallbacks();
John Recka5dda642014-05-22 15:43:54 -0700109 void requestVsync();
John Recke45b1fd2014-04-15 09:50:16 -0700110
John Reck73b7a4d2014-07-23 14:54:04 -0700111 // VERY DANGEROUS HANDLE WITH EXTREME CARE
112 void nukeFromOrbit();
113
John Reck4f02bf42014-01-03 18:09:17 -0800114 // Returns the next task to be run. If this returns NULL nextWakeup is set
115 // to the time to requery for the nextTask to run. mNextWakeup is also
116 // set to this time
117 RenderTask* nextTask(nsecs_t* nextWakeup);
John Reckcec24ae2013-11-05 13:27:50 -0800118
John Reck73b7a4d2014-07-23 14:54:04 -0700119 pthread_t mThreadId;
John Reckcec24ae2013-11-05 13:27:50 -0800120 sp<Looper> mLooper;
121 Mutex mLock;
122
John Reck4f02bf42014-01-03 18:09:17 -0800123 nsecs_t mNextWakeup;
124 TaskQueue mQueue;
John Recke45b1fd2014-04-15 09:50:16 -0700125
126 DisplayEventReceiver* mDisplayEventReceiver;
127 bool mVsyncRequested;
128 std::set<IFrameCallback*> mFrameCallbacks;
John Recka5dda642014-05-22 15:43:54 -0700129 // We defer the actual registration of these callbacks until
130 // both mQueue *and* mDisplayEventReceiver have been drained off all
131 // immediate events. This makes sure that we catch the next vsync, not
132 // the previous one
133 std::set<IFrameCallback*> mPendingRegistrationFrameCallbacks;
John Recke45b1fd2014-04-15 09:50:16 -0700134 bool mFrameCallbackTaskPending;
135 DispatchFrameCallbacks* mFrameCallbackTask;
John Reck18f16e62014-05-02 16:46:41 -0700136
137 TimeLord mTimeLord;
John Reck3b202512014-06-23 13:13:08 -0700138 RenderState* mRenderState;
139 EglManager* mEglManager;
John Reckcec24ae2013-11-05 13:27:50 -0800140};
141
142} /* namespace renderthread */
143} /* namespace uirenderer */
144} /* namespace android */
145#endif /* RENDERTHREAD_H_ */