blob: 076e3d43a2c9b4204caeb74d444c0240f9cf5330 [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 Reckba6adf62015-02-19 14:36:50 -080030#include <memory>
31#include <set>
John Reck18f16e62014-05-02 16:46:41 -070032
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;
Chris Craik0a24b142015-10-19 17:10:19 -070040class TestUtils;
John Reck3b202512014-06-23 13:13:08 -070041
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 Reck6b507802015-11-03 10:09:59 -080073class ANDROID_API RenderThread : public Thread {
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);
Chris Craik0a24b142015-10-19 17:10:19 -070078 ANDROID_API void queueAndWait(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:
John Recke45b1fd2014-04-15 09:50:16 -0700101 friend class DispatchFrameCallbacks;
John Reck3b202512014-06-23 13:13:08 -0700102 friend class RenderProxy;
Chris Craik0a24b142015-10-19 17:10:19 -0700103 friend class android::uirenderer::TestUtils;
John Reckcec24ae2013-11-05 13:27:50 -0800104
105 RenderThread();
106 virtual ~RenderThread();
107
John Reck6b507802015-11-03 10:09:59 -0800108 static bool hasInstance();
109 static RenderThread& getInstance();
110
John Reck3b202512014-06-23 13:13:08 -0700111 void initThreadLocals();
John Recke45b1fd2014-04-15 09:50:16 -0700112 void initializeDisplayEventReceiver();
113 static int displayEventReceiverCallback(int fd, int events, void* data);
John Recka733f892014-12-19 11:37:21 -0800114 void drainDisplayEventQueue();
John Recke45b1fd2014-04-15 09:50:16 -0700115 void dispatchFrameCallbacks();
John Recka5dda642014-05-22 15:43:54 -0700116 void requestVsync();
John Recke45b1fd2014-04-15 09:50:16 -0700117
John Reck4f02bf42014-01-03 18:09:17 -0800118 // Returns the next task to be run. If this returns NULL nextWakeup is set
119 // to the time to requery for the nextTask to run. mNextWakeup is also
120 // set to this time
121 RenderTask* nextTask(nsecs_t* nextWakeup);
John Reckcec24ae2013-11-05 13:27:50 -0800122
123 sp<Looper> mLooper;
124 Mutex mLock;
125
John Reck4f02bf42014-01-03 18:09:17 -0800126 nsecs_t mNextWakeup;
127 TaskQueue mQueue;
John Recke45b1fd2014-04-15 09:50:16 -0700128
John Reckb36016c2015-03-11 08:50:53 -0700129 DisplayInfo mDisplayInfo;
130
John Recke45b1fd2014-04-15 09:50:16 -0700131 DisplayEventReceiver* mDisplayEventReceiver;
132 bool mVsyncRequested;
133 std::set<IFrameCallback*> mFrameCallbacks;
John Recka5dda642014-05-22 15:43:54 -0700134 // We defer the actual registration of these callbacks until
135 // both mQueue *and* mDisplayEventReceiver have been drained off all
136 // immediate events. This makes sure that we catch the next vsync, not
137 // the previous one
138 std::set<IFrameCallback*> mPendingRegistrationFrameCallbacks;
John Recke45b1fd2014-04-15 09:50:16 -0700139 bool mFrameCallbackTaskPending;
140 DispatchFrameCallbacks* mFrameCallbackTask;
John Reck18f16e62014-05-02 16:46:41 -0700141
142 TimeLord mTimeLord;
John Reck3b202512014-06-23 13:13:08 -0700143 RenderState* mRenderState;
144 EglManager* mEglManager;
John Reckba6adf62015-02-19 14:36:50 -0800145
146 JankTracker* mJankTracker = nullptr;
John Reckcec24ae2013-11-05 13:27:50 -0800147};
148
149} /* namespace renderthread */
150} /* namespace uirenderer */
151} /* namespace android */
152#endif /* RENDERTHREAD_H_ */