blob: d8c7e61f34eb0f42cba058359dc0a0cea2ec0b9b [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>
John Reckcec24ae2013-11-05 13:27:50 -080028#include <utils/Thread.h>
29
John Reck6b507802015-11-03 10:09:59 -080030#include <condition_variable>
John Reckba6adf62015-02-19 14:36:50 -080031#include <memory>
John Reck6b507802015-11-03 10:09:59 -080032#include <mutex>
John Reckba6adf62015-02-19 14:36:50 -080033#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;
Chris Craik0a24b142015-10-19 17:10:19 -070042class TestUtils;
John Reck3b202512014-06-23 13:13:08 -070043
John Reckcec24ae2013-11-05 13:27:50 -080044namespace renderthread {
45
John Reck443a7142014-09-04 17:40:05 -070046class CanvasContext;
John Recke45b1fd2014-04-15 09:50:16 -070047class DispatchFrameCallbacks;
John Reck3b202512014-06-23 13:13:08 -070048class EglManager;
49class RenderProxy;
John Recke45b1fd2014-04-15 09:50:16 -070050
John Reck4f02bf42014-01-03 18:09:17 -080051class TaskQueue {
52public:
53 TaskQueue();
54
55 RenderTask* next();
56 void queue(RenderTask* task);
John Recka5dda642014-05-22 15:43:54 -070057 void queueAtFront(RenderTask* task);
John Reck4f02bf42014-01-03 18:09:17 -080058 RenderTask* peek();
59 void remove(RenderTask* task);
60
61private:
62 RenderTask* mHead;
63 RenderTask* mTail;
64};
65
John Recke45b1fd2014-04-15 09:50:16 -070066// Mimics android.view.Choreographer.FrameCallback
67class IFrameCallback {
68public:
John Reck18f16e62014-05-02 16:46:41 -070069 virtual void doFrame() = 0;
John Recke45b1fd2014-04-15 09:50:16 -070070
71protected:
72 ~IFrameCallback() {}
73};
74
John Reck6b507802015-11-03 10:09:59 -080075class ANDROID_API RenderThread : public Thread {
John Reckcec24ae2013-11-05 13:27:50 -080076public:
77 // RenderThread takes complete ownership of tasks that are queued
78 // and will delete them after they are run
79 ANDROID_API void queue(RenderTask* task);
Chris Craik0a24b142015-10-19 17:10:19 -070080 ANDROID_API void queueAndWait(RenderTask* task);
John Recka5dda642014-05-22 15:43:54 -070081 ANDROID_API void queueAtFront(RenderTask* task);
John Recka733f892014-12-19 11:37:21 -080082 void queueAt(RenderTask* task, nsecs_t runAtNs);
John Reck4f02bf42014-01-03 18:09:17 -080083 void remove(RenderTask* task);
John Reckcec24ae2013-11-05 13:27:50 -080084
John Recke45b1fd2014-04-15 09:50:16 -070085 // Mimics android.view.Choreographer
86 void postFrameCallback(IFrameCallback* callback);
John Reck01a5ea32014-12-03 13:01:07 -080087 bool removeFrameCallback(IFrameCallback* callback);
John Recka5dda642014-05-22 15:43:54 -070088 // If the callback is currently registered, it will be pushed back until
89 // the next vsync. If it is not currently registered this does nothing.
90 void pushBackFrameCallback(IFrameCallback* callback);
John Recke45b1fd2014-04-15 09:50:16 -070091
John Reck18f16e62014-05-02 16:46:41 -070092 TimeLord& timeLord() { return mTimeLord; }
John Reck3b202512014-06-23 13:13:08 -070093 RenderState& renderState() { return *mRenderState; }
94 EglManager& eglManager() { return *mEglManager; }
John Reckba6adf62015-02-19 14:36:50 -080095 JankTracker& jankTracker() { return *mJankTracker; }
John Reck18f16e62014-05-02 16:46:41 -070096
John Reckb36016c2015-03-11 08:50:53 -070097 const DisplayInfo& mainDisplayInfo() { return mDisplayInfo; }
98
John Reckcec24ae2013-11-05 13:27:50 -080099protected:
Chris Craikd41c4d82015-01-05 15:51:13 -0800100 virtual bool threadLoop() override;
John Reckcec24ae2013-11-05 13:27:50 -0800101
102private:
John Recke45b1fd2014-04-15 09:50:16 -0700103 friend class DispatchFrameCallbacks;
John Reck3b202512014-06-23 13:13:08 -0700104 friend class RenderProxy;
Chris Craik0a24b142015-10-19 17:10:19 -0700105 friend class android::uirenderer::TestUtils;
John Reckcec24ae2013-11-05 13:27:50 -0800106
107 RenderThread();
108 virtual ~RenderThread();
109
John Reck6b507802015-11-03 10:09:59 -0800110 static bool hasInstance();
111 static RenderThread& getInstance();
112
John Reck3b202512014-06-23 13:13:08 -0700113 void initThreadLocals();
John Recke45b1fd2014-04-15 09:50:16 -0700114 void initializeDisplayEventReceiver();
115 static int displayEventReceiverCallback(int fd, int events, void* data);
John Recka733f892014-12-19 11:37:21 -0800116 void drainDisplayEventQueue();
John Recke45b1fd2014-04-15 09:50:16 -0700117 void dispatchFrameCallbacks();
John Recka5dda642014-05-22 15:43:54 -0700118 void requestVsync();
John Recke45b1fd2014-04-15 09:50:16 -0700119
John Reck4f02bf42014-01-03 18:09:17 -0800120 // Returns the next task to be run. If this returns NULL nextWakeup is set
121 // to the time to requery for the nextTask to run. mNextWakeup is also
122 // set to this time
123 RenderTask* nextTask(nsecs_t* nextWakeup);
John Reckcec24ae2013-11-05 13:27:50 -0800124
125 sp<Looper> mLooper;
126 Mutex mLock;
127
John Reck4f02bf42014-01-03 18:09:17 -0800128 nsecs_t mNextWakeup;
129 TaskQueue mQueue;
John Reck6b507802015-11-03 10:09:59 -0800130 Mutex mSyncMutex;
131 Condition mSyncCondition;
John Recke45b1fd2014-04-15 09:50:16 -0700132
John Reckb36016c2015-03-11 08:50:53 -0700133 DisplayInfo mDisplayInfo;
134
John Recke45b1fd2014-04-15 09:50:16 -0700135 DisplayEventReceiver* mDisplayEventReceiver;
136 bool mVsyncRequested;
137 std::set<IFrameCallback*> mFrameCallbacks;
John Recka5dda642014-05-22 15:43:54 -0700138 // We defer the actual registration of these callbacks until
139 // both mQueue *and* mDisplayEventReceiver have been drained off all
140 // immediate events. This makes sure that we catch the next vsync, not
141 // the previous one
142 std::set<IFrameCallback*> mPendingRegistrationFrameCallbacks;
John Recke45b1fd2014-04-15 09:50:16 -0700143 bool mFrameCallbackTaskPending;
144 DispatchFrameCallbacks* mFrameCallbackTask;
John Reck18f16e62014-05-02 16:46:41 -0700145
146 TimeLord mTimeLord;
John Reck3b202512014-06-23 13:13:08 -0700147 RenderState* mRenderState;
148 EglManager* mEglManager;
John Reckba6adf62015-02-19 14:36:50 -0800149
150 JankTracker* mJankTracker = nullptr;
John Reckcec24ae2013-11-05 13:27:50 -0800151};
152
153} /* namespace renderthread */
154} /* namespace uirenderer */
155} /* namespace android */
156#endif /* RENDERTHREAD_H_ */