blob: f1694243b5285c353143d0084c5b8e3887a77d3e [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
John Reckba6adf62015-02-19 14:36:50 -080022#include "../JankTracker.h"
23#include "TimeLord.h"
John Recke45b1fd2014-04-15 09:50:16 -070024
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 Reckba6adf62015-02-19 14:36:50 -080031#include <memory>
32#include <set>
John Reck18f16e62014-05-02 16:46:41 -070033
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 Reck443a7142014-09-04 17:40:05 -070044class CanvasContext;
John Recke45b1fd2014-04-15 09:50:16 -070045class DispatchFrameCallbacks;
John Reck3b202512014-06-23 13:13:08 -070046class EglManager;
47class RenderProxy;
John Recke45b1fd2014-04-15 09:50:16 -070048
John Reck4f02bf42014-01-03 18:09:17 -080049class TaskQueue {
50public:
51 TaskQueue();
52
53 RenderTask* next();
54 void queue(RenderTask* task);
John Recka5dda642014-05-22 15:43:54 -070055 void queueAtFront(RenderTask* task);
John Reck4f02bf42014-01-03 18:09:17 -080056 RenderTask* peek();
57 void remove(RenderTask* task);
58
59private:
60 RenderTask* mHead;
61 RenderTask* mTail;
62};
63
John Recke45b1fd2014-04-15 09:50:16 -070064// Mimics android.view.Choreographer.FrameCallback
65class IFrameCallback {
66public:
John Reck18f16e62014-05-02 16:46:41 -070067 virtual void doFrame() = 0;
John Recke45b1fd2014-04-15 09:50:16 -070068
69protected:
70 ~IFrameCallback() {}
71};
72
John Reck3b202512014-06-23 13:13:08 -070073class ANDROID_API RenderThread : public Thread, protected Singleton<RenderThread> {
John Reckcec24ae2013-11-05 13:27:50 -080074public:
75 // RenderThread takes complete ownership of tasks that are queued
76 // and will delete them after they are run
77 ANDROID_API void queue(RenderTask* task);
John Recka5dda642014-05-22 15:43:54 -070078 ANDROID_API void queueAtFront(RenderTask* task);
John Recka733f892014-12-19 11:37:21 -080079 void queueAt(RenderTask* task, nsecs_t runAtNs);
John Reck4f02bf42014-01-03 18:09:17 -080080 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);
John Reck01a5ea32014-12-03 13:01:07 -080084 bool 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 Reckba6adf62015-02-19 14:36:50 -080089 void setFrameInterval(nsecs_t frameInterval);
90
John Reck18f16e62014-05-02 16:46:41 -070091 TimeLord& timeLord() { return mTimeLord; }
John Reck3b202512014-06-23 13:13:08 -070092 RenderState& renderState() { return *mRenderState; }
93 EglManager& eglManager() { return *mEglManager; }
John Reckba6adf62015-02-19 14:36:50 -080094 JankTracker& jankTracker() { return *mJankTracker; }
John Reck18f16e62014-05-02 16:46:41 -070095
John Reckcec24ae2013-11-05 13:27:50 -080096protected:
Chris Craikd41c4d82015-01-05 15:51:13 -080097 virtual bool threadLoop() override;
John Reckcec24ae2013-11-05 13:27:50 -080098
99private:
100 friend class Singleton<RenderThread>;
John Recke45b1fd2014-04-15 09:50:16 -0700101 friend class DispatchFrameCallbacks;
John Reck3b202512014-06-23 13:13:08 -0700102 friend class RenderProxy;
John Reckcec24ae2013-11-05 13:27:50 -0800103
104 RenderThread();
105 virtual ~RenderThread();
106
John Reck3b202512014-06-23 13:13:08 -0700107 void initThreadLocals();
John Recke45b1fd2014-04-15 09:50:16 -0700108 void initializeDisplayEventReceiver();
109 static int displayEventReceiverCallback(int fd, int events, void* data);
John Recka733f892014-12-19 11:37:21 -0800110 void drainDisplayEventQueue();
John Recke45b1fd2014-04-15 09:50:16 -0700111 void dispatchFrameCallbacks();
John Recka5dda642014-05-22 15:43:54 -0700112 void requestVsync();
John Recke45b1fd2014-04-15 09:50:16 -0700113
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
119 sp<Looper> mLooper;
120 Mutex mLock;
121
John Reck4f02bf42014-01-03 18:09:17 -0800122 nsecs_t mNextWakeup;
123 TaskQueue mQueue;
John Recke45b1fd2014-04-15 09:50:16 -0700124
125 DisplayEventReceiver* mDisplayEventReceiver;
126 bool mVsyncRequested;
127 std::set<IFrameCallback*> mFrameCallbacks;
John Recka5dda642014-05-22 15:43:54 -0700128 // We defer the actual registration of these callbacks until
129 // both mQueue *and* mDisplayEventReceiver have been drained off all
130 // immediate events. This makes sure that we catch the next vsync, not
131 // the previous one
132 std::set<IFrameCallback*> mPendingRegistrationFrameCallbacks;
John Recke45b1fd2014-04-15 09:50:16 -0700133 bool mFrameCallbackTaskPending;
134 DispatchFrameCallbacks* mFrameCallbackTask;
John Reck18f16e62014-05-02 16:46:41 -0700135
136 TimeLord mTimeLord;
John Reck3b202512014-06-23 13:13:08 -0700137 RenderState* mRenderState;
138 EglManager* mEglManager;
John Reckba6adf62015-02-19 14:36:50 -0800139
140 JankTracker* mJankTracker = nullptr;
John Reckcec24ae2013-11-05 13:27:50 -0800141};
142
143} /* namespace renderthread */
144} /* namespace uirenderer */
145} /* namespace android */
146#endif /* RENDERTHREAD_H_ */