blob: 80960999ef539f69c1eafe69a58fcf136129ac6e [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>
John Reckb36016c2015-03-11 08:50:53 -070026#include <ui/DisplayInfo.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 Reckba6adf62015-02-19 14:36:50 -080032#include <memory>
33#include <set>
John Reck18f16e62014-05-02 16:46:41 -070034
John Reckcec24ae2013-11-05 13:27:50 -080035namespace android {
John Reck3b202512014-06-23 13:13:08 -070036
John Recke45b1fd2014-04-15 09:50:16 -070037class DisplayEventReceiver;
38
John Reckcec24ae2013-11-05 13:27:50 -080039namespace uirenderer {
John Reck3b202512014-06-23 13:13:08 -070040
41class RenderState;
42
John Reckcec24ae2013-11-05 13:27:50 -080043namespace renderthread {
44
John Reck443a7142014-09-04 17:40:05 -070045class CanvasContext;
John Recke45b1fd2014-04-15 09:50:16 -070046class DispatchFrameCallbacks;
John Reck3b202512014-06-23 13:13:08 -070047class EglManager;
48class RenderProxy;
John Recke45b1fd2014-04-15 09:50:16 -070049
John Reck4f02bf42014-01-03 18:09:17 -080050class TaskQueue {
51public:
52 TaskQueue();
53
54 RenderTask* next();
55 void queue(RenderTask* task);
John Recka5dda642014-05-22 15:43:54 -070056 void queueAtFront(RenderTask* task);
John Reck4f02bf42014-01-03 18:09:17 -080057 RenderTask* peek();
58 void remove(RenderTask* task);
59
60private:
61 RenderTask* mHead;
62 RenderTask* mTail;
63};
64
John Recke45b1fd2014-04-15 09:50:16 -070065// Mimics android.view.Choreographer.FrameCallback
66class IFrameCallback {
67public:
John Reck18f16e62014-05-02 16:46:41 -070068 virtual void doFrame() = 0;
John Recke45b1fd2014-04-15 09:50:16 -070069
70protected:
71 ~IFrameCallback() {}
72};
73
John Reck3b202512014-06-23 13:13:08 -070074class ANDROID_API RenderThread : public Thread, protected Singleton<RenderThread> {
John Reckcec24ae2013-11-05 13:27:50 -080075public:
76 // RenderThread takes complete ownership of tasks that are queued
77 // and will delete them after they are run
78 ANDROID_API void queue(RenderTask* task);
John Recka5dda642014-05-22 15:43:54 -070079 ANDROID_API void queueAtFront(RenderTask* task);
John Recka733f892014-12-19 11:37:21 -080080 void queueAt(RenderTask* task, nsecs_t runAtNs);
John Reck4f02bf42014-01-03 18:09:17 -080081 void remove(RenderTask* task);
John Reckcec24ae2013-11-05 13:27:50 -080082
John Recke45b1fd2014-04-15 09:50:16 -070083 // Mimics android.view.Choreographer
84 void postFrameCallback(IFrameCallback* callback);
John Reck01a5ea32014-12-03 13:01:07 -080085 bool removeFrameCallback(IFrameCallback* callback);
John Recka5dda642014-05-22 15:43:54 -070086 // If the callback is currently registered, it will be pushed back until
87 // the next vsync. If it is not currently registered this does nothing.
88 void pushBackFrameCallback(IFrameCallback* callback);
John Recke45b1fd2014-04-15 09:50:16 -070089
John Reck18f16e62014-05-02 16:46:41 -070090 TimeLord& timeLord() { return mTimeLord; }
John Reck3b202512014-06-23 13:13:08 -070091 RenderState& renderState() { return *mRenderState; }
92 EglManager& eglManager() { return *mEglManager; }
John Reckba6adf62015-02-19 14:36:50 -080093 JankTracker& jankTracker() { return *mJankTracker; }
John Reck18f16e62014-05-02 16:46:41 -070094
John Reckb36016c2015-03-11 08:50:53 -070095 const DisplayInfo& mainDisplayInfo() { return mDisplayInfo; }
96
John Reckcec24ae2013-11-05 13:27:50 -080097protected:
Chris Craikd41c4d82015-01-05 15:51:13 -080098 virtual bool threadLoop() override;
John Reckcec24ae2013-11-05 13:27:50 -080099
100private:
101 friend class Singleton<RenderThread>;
John Recke45b1fd2014-04-15 09:50:16 -0700102 friend class DispatchFrameCallbacks;
John Reck3b202512014-06-23 13:13:08 -0700103 friend class RenderProxy;
John Reckcec24ae2013-11-05 13:27:50 -0800104
105 RenderThread();
106 virtual ~RenderThread();
107
John Reck3b202512014-06-23 13:13:08 -0700108 void initThreadLocals();
John Recke45b1fd2014-04-15 09:50:16 -0700109 void initializeDisplayEventReceiver();
110 static int displayEventReceiverCallback(int fd, int events, void* data);
John Recka733f892014-12-19 11:37:21 -0800111 void drainDisplayEventQueue();
John Recke45b1fd2014-04-15 09:50:16 -0700112 void dispatchFrameCallbacks();
John Recka5dda642014-05-22 15:43:54 -0700113 void requestVsync();
John Recke45b1fd2014-04-15 09:50:16 -0700114
John Reck4f02bf42014-01-03 18:09:17 -0800115 // Returns the next task to be run. If this returns NULL nextWakeup is set
116 // to the time to requery for the nextTask to run. mNextWakeup is also
117 // set to this time
118 RenderTask* nextTask(nsecs_t* nextWakeup);
John Reckcec24ae2013-11-05 13:27:50 -0800119
120 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
John Reckb36016c2015-03-11 08:50:53 -0700126 DisplayInfo mDisplayInfo;
127
John Recke45b1fd2014-04-15 09:50:16 -0700128 DisplayEventReceiver* mDisplayEventReceiver;
129 bool mVsyncRequested;
130 std::set<IFrameCallback*> mFrameCallbacks;
John Recka5dda642014-05-22 15:43:54 -0700131 // We defer the actual registration of these callbacks until
132 // both mQueue *and* mDisplayEventReceiver have been drained off all
133 // immediate events. This makes sure that we catch the next vsync, not
134 // the previous one
135 std::set<IFrameCallback*> mPendingRegistrationFrameCallbacks;
John Recke45b1fd2014-04-15 09:50:16 -0700136 bool mFrameCallbackTaskPending;
137 DispatchFrameCallbacks* mFrameCallbackTask;
John Reck18f16e62014-05-02 16:46:41 -0700138
139 TimeLord mTimeLord;
John Reck3b202512014-06-23 13:13:08 -0700140 RenderState* mRenderState;
141 EglManager* mEglManager;
John Reckba6adf62015-02-19 14:36:50 -0800142
143 JankTracker* mJankTracker = nullptr;
John Reckcec24ae2013-11-05 13:27:50 -0800144};
145
146} /* namespace renderthread */
147} /* namespace uirenderer */
148} /* namespace android */
149#endif /* RENDERTHREAD_H_ */