2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef RENDERTHREAD_H_
18 #define RENDERTHREAD_H_
20 #include "RenderTask.h"
25 #include <cutils/compiler.h>
26 #include <utils/Looper.h>
27 #include <utils/Mutex.h>
28 #include <utils/Singleton.h>
29 #include <utils/Thread.h>
35 class DisplayEventReceiver;
37 namespace uirenderer {
41 namespace renderthread {
44 class DispatchFrameCallbacks;
53 void queue(RenderTask* task);
54 void queueAtFront(RenderTask* task);
56 void remove(RenderTask* task);
63 // Mimics android.view.Choreographer.FrameCallback
64 class IFrameCallback {
66 virtual void doFrame() = 0;
72 class ANDROID_API RenderThread : public Thread, protected Singleton<RenderThread> {
74 // RenderThread takes complete ownership of tasks that are queued
75 // and will delete them after they are run
76 ANDROID_API void queue(RenderTask* task);
77 ANDROID_API void queueAtFront(RenderTask* task);
78 void queueAt(RenderTask* task, nsecs_t runAtNs);
79 void remove(RenderTask* task);
81 // Mimics android.view.Choreographer
82 void postFrameCallback(IFrameCallback* callback);
83 bool removeFrameCallback(IFrameCallback* callback);
84 // If the callback is currently registered, it will be pushed back until
85 // the next vsync. If it is not currently registered this does nothing.
86 void pushBackFrameCallback(IFrameCallback* callback);
88 TimeLord& timeLord() { return mTimeLord; }
89 RenderState& renderState() { return *mRenderState; }
90 EglManager& eglManager() { return *mEglManager; }
93 virtual bool threadLoop();
96 friend class Singleton<RenderThread>;
97 friend class DispatchFrameCallbacks;
98 friend class RenderProxy;
101 virtual ~RenderThread();
103 void initThreadLocals();
104 void initializeDisplayEventReceiver();
105 static int displayEventReceiverCallback(int fd, int events, void* data);
106 void drainDisplayEventQueue();
107 void dispatchFrameCallbacks();
110 // Returns the next task to be run. If this returns NULL nextWakeup is set
111 // to the time to requery for the nextTask to run. mNextWakeup is also
113 RenderTask* nextTask(nsecs_t* nextWakeup);
121 DisplayEventReceiver* mDisplayEventReceiver;
122 bool mVsyncRequested;
123 std::set<IFrameCallback*> mFrameCallbacks;
124 // We defer the actual registration of these callbacks until
125 // both mQueue *and* mDisplayEventReceiver have been drained off all
126 // immediate events. This makes sure that we catch the next vsync, not
128 std::set<IFrameCallback*> mPendingRegistrationFrameCallbacks;
129 bool mFrameCallbackTaskPending;
130 DispatchFrameCallbacks* mFrameCallbackTask;
133 RenderState* mRenderState;
134 EglManager* mEglManager;
137 } /* namespace renderthread */
138 } /* namespace uirenderer */
139 } /* namespace android */
140 #endif /* RENDERTHREAD_H_ */