blob: 4126d021a281404cd548f53a68ded1d28ea614c2 [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 Reck3b202512014-06-23 13:13:08 -070034
John Recke45b1fd2014-04-15 09:50:16 -070035class DisplayEventReceiver;
36
John Reckcec24ae2013-11-05 13:27:50 -080037namespace uirenderer {
John Reck3b202512014-06-23 13:13:08 -070038
39class RenderState;
40
John Reckcec24ae2013-11-05 13:27:50 -080041namespace renderthread {
42
John Reck443a7142014-09-04 17:40:05 -070043class CanvasContext;
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 Recka5dda642014-05-22 15:43:54 -070077 ANDROID_API void queueAtFront(RenderTask* task);
John Reck4f02bf42014-01-03 18:09:17 -080078 void queueDelayed(RenderTask* task, int delayMs);
79 void remove(RenderTask* task);
John Reckcec24ae2013-11-05 13:27:50 -080080
John Recke45b1fd2014-04-15 09:50:16 -070081 // Mimics android.view.Choreographer
82 void postFrameCallback(IFrameCallback* callback);
John Reck01a5ea32014-12-03 13:01:07 -080083 bool removeFrameCallback(IFrameCallback* callback);
John Recka5dda642014-05-22 15:43:54 -070084 // If the callback is currently registered, it will be pushed back until
85 // the next vsync. If it is not currently registered this does nothing.
86 void pushBackFrameCallback(IFrameCallback* callback);
John Recke45b1fd2014-04-15 09:50:16 -070087
John Reck18f16e62014-05-02 16:46:41 -070088 TimeLord& timeLord() { return mTimeLord; }
John Reck3b202512014-06-23 13:13:08 -070089 RenderState& renderState() { return *mRenderState; }
90 EglManager& eglManager() { return *mEglManager; }
John Reck18f16e62014-05-02 16:46:41 -070091
John Reckcec24ae2013-11-05 13:27:50 -080092protected:
93 virtual bool threadLoop();
94
95private:
96 friend class Singleton<RenderThread>;
John Recke45b1fd2014-04-15 09:50:16 -070097 friend class DispatchFrameCallbacks;
John Reck3b202512014-06-23 13:13:08 -070098 friend class RenderProxy;
John Reckcec24ae2013-11-05 13:27:50 -080099
100 RenderThread();
101 virtual ~RenderThread();
102
John Reck3b202512014-06-23 13:13:08 -0700103 void initThreadLocals();
John Recke45b1fd2014-04-15 09:50:16 -0700104 void initializeDisplayEventReceiver();
105 static int displayEventReceiverCallback(int fd, int events, void* data);
John Recka5dda642014-05-22 15:43:54 -0700106 void drainDisplayEventQueue(bool skipCallbacks = false);
John Recke45b1fd2014-04-15 09:50:16 -0700107 void dispatchFrameCallbacks();
John Recka5dda642014-05-22 15:43:54 -0700108 void requestVsync();
John Recke45b1fd2014-04-15 09:50:16 -0700109
John Reck4f02bf42014-01-03 18:09:17 -0800110 // Returns the next task to be run. If this returns NULL nextWakeup is set
111 // to the time to requery for the nextTask to run. mNextWakeup is also
112 // set to this time
113 RenderTask* nextTask(nsecs_t* nextWakeup);
John Reckcec24ae2013-11-05 13:27:50 -0800114
115 sp<Looper> mLooper;
116 Mutex mLock;
117
John Reck4f02bf42014-01-03 18:09:17 -0800118 nsecs_t mNextWakeup;
119 TaskQueue mQueue;
John Recke45b1fd2014-04-15 09:50:16 -0700120
121 DisplayEventReceiver* mDisplayEventReceiver;
122 bool mVsyncRequested;
123 std::set<IFrameCallback*> mFrameCallbacks;
John Recka5dda642014-05-22 15:43:54 -0700124 // We defer the actual registration of these callbacks until
125 // both mQueue *and* mDisplayEventReceiver have been drained off all
126 // immediate events. This makes sure that we catch the next vsync, not
127 // the previous one
128 std::set<IFrameCallback*> mPendingRegistrationFrameCallbacks;
John Recke45b1fd2014-04-15 09:50:16 -0700129 bool mFrameCallbackTaskPending;
130 DispatchFrameCallbacks* mFrameCallbackTask;
John Reck18f16e62014-05-02 16:46:41 -0700131
132 TimeLord mTimeLord;
John Reck3b202512014-06-23 13:13:08 -0700133 RenderState* mRenderState;
134 EglManager* mEglManager;
John Reckcec24ae2013-11-05 13:27:50 -0800135};
136
137} /* namespace renderthread */
138} /* namespace uirenderer */
139} /* namespace android */
140#endif /* RENDERTHREAD_H_ */