blob: 0b91e9dd97aad43d65ee27e9224e032833f5dec6 [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 Recke45b1fd2014-04-15 09:50:16 -070043class DispatchFrameCallbacks;
John Reck3b202512014-06-23 13:13:08 -070044class EglManager;
45class RenderProxy;
John Recke45b1fd2014-04-15 09:50:16 -070046
John Reck4f02bf42014-01-03 18:09:17 -080047class TaskQueue {
48public:
49 TaskQueue();
50
51 RenderTask* next();
52 void queue(RenderTask* task);
John Recka5dda642014-05-22 15:43:54 -070053 void queueAtFront(RenderTask* task);
John Reck4f02bf42014-01-03 18:09:17 -080054 RenderTask* peek();
55 void remove(RenderTask* task);
56
57private:
58 RenderTask* mHead;
59 RenderTask* mTail;
60};
61
John Recke45b1fd2014-04-15 09:50:16 -070062// Mimics android.view.Choreographer.FrameCallback
63class IFrameCallback {
64public:
John Reck18f16e62014-05-02 16:46:41 -070065 virtual void doFrame() = 0;
John Recke45b1fd2014-04-15 09:50:16 -070066
67protected:
68 ~IFrameCallback() {}
69};
70
John Reck3b202512014-06-23 13:13:08 -070071class ANDROID_API RenderThread : public Thread, protected Singleton<RenderThread> {
John Reckcec24ae2013-11-05 13:27:50 -080072public:
73 // RenderThread takes complete ownership of tasks that are queued
74 // and will delete them after they are run
75 ANDROID_API void queue(RenderTask* task);
John Recka5dda642014-05-22 15:43:54 -070076 ANDROID_API void queueAtFront(RenderTask* task);
John Reck4f02bf42014-01-03 18:09:17 -080077 void queueDelayed(RenderTask* task, int delayMs);
78 void remove(RenderTask* task);
John Reckcec24ae2013-11-05 13:27:50 -080079
John Recke45b1fd2014-04-15 09:50:16 -070080 // Mimics android.view.Choreographer
81 void postFrameCallback(IFrameCallback* callback);
82 void removeFrameCallback(IFrameCallback* callback);
John Recka5dda642014-05-22 15:43:54 -070083 // If the callback is currently registered, it will be pushed back until
84 // the next vsync. If it is not currently registered this does nothing.
85 void pushBackFrameCallback(IFrameCallback* callback);
John Recke45b1fd2014-04-15 09:50:16 -070086
John Reck18f16e62014-05-02 16:46:41 -070087 TimeLord& timeLord() { return mTimeLord; }
John Reck3b202512014-06-23 13:13:08 -070088 RenderState& renderState() { return *mRenderState; }
89 EglManager& eglManager() { return *mEglManager; }
John Reck18f16e62014-05-02 16:46:41 -070090
John Reckcec24ae2013-11-05 13:27:50 -080091protected:
92 virtual bool threadLoop();
93
94private:
95 friend class Singleton<RenderThread>;
John Recke45b1fd2014-04-15 09:50:16 -070096 friend class DispatchFrameCallbacks;
John Reck3b202512014-06-23 13:13:08 -070097 friend class RenderProxy;
John Reckcec24ae2013-11-05 13:27:50 -080098
99 RenderThread();
100 virtual ~RenderThread();
101
John Reck3b202512014-06-23 13:13:08 -0700102 void initThreadLocals();
John Recke45b1fd2014-04-15 09:50:16 -0700103 void initializeDisplayEventReceiver();
104 static int displayEventReceiverCallback(int fd, int events, void* data);
John Recka5dda642014-05-22 15:43:54 -0700105 void drainDisplayEventQueue(bool skipCallbacks = false);
John Recke45b1fd2014-04-15 09:50:16 -0700106 void dispatchFrameCallbacks();
John Recka5dda642014-05-22 15:43:54 -0700107 void requestVsync();
John Recke45b1fd2014-04-15 09:50:16 -0700108
John Reck4f02bf42014-01-03 18:09:17 -0800109 // Returns the next task to be run. If this returns NULL nextWakeup is set
110 // to the time to requery for the nextTask to run. mNextWakeup is also
111 // set to this time
112 RenderTask* nextTask(nsecs_t* nextWakeup);
John Reckcec24ae2013-11-05 13:27:50 -0800113
114 sp<Looper> mLooper;
115 Mutex mLock;
116
John Reck4f02bf42014-01-03 18:09:17 -0800117 nsecs_t mNextWakeup;
118 TaskQueue mQueue;
John Recke45b1fd2014-04-15 09:50:16 -0700119
120 DisplayEventReceiver* mDisplayEventReceiver;
121 bool mVsyncRequested;
122 std::set<IFrameCallback*> mFrameCallbacks;
John Recka5dda642014-05-22 15:43:54 -0700123 // We defer the actual registration of these callbacks until
124 // both mQueue *and* mDisplayEventReceiver have been drained off all
125 // immediate events. This makes sure that we catch the next vsync, not
126 // the previous one
127 std::set<IFrameCallback*> mPendingRegistrationFrameCallbacks;
John Recke45b1fd2014-04-15 09:50:16 -0700128 bool mFrameCallbackTaskPending;
129 DispatchFrameCallbacks* mFrameCallbackTask;
John Reck18f16e62014-05-02 16:46:41 -0700130
131 TimeLord mTimeLord;
John Reck3b202512014-06-23 13:13:08 -0700132 RenderState* mRenderState;
133 EglManager* mEglManager;
John Reckcec24ae2013-11-05 13:27:50 -0800134};
135
136} /* namespace renderthread */
137} /* namespace uirenderer */
138} /* namespace android */
139#endif /* RENDERTHREAD_H_ */