RenderThread.h revision d5a4a1aac980c304d6f46f07f15bfc5c94d8f9d0
1/* 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" 21 22#include "../JankTracker.h" 23#include "TimeLord.h" 24 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> 30 31#include <memory> 32#include <set> 33 34namespace android { 35 36class DisplayEventReceiver; 37 38namespace uirenderer { 39 40class RenderState; 41 42namespace renderthread { 43 44class CanvasContext; 45class DispatchFrameCallbacks; 46class EglManager; 47class RenderProxy; 48 49class TaskQueue { 50public: 51 TaskQueue(); 52 53 RenderTask* next(); 54 void queue(RenderTask* task); 55 void queueAtFront(RenderTask* task); 56 RenderTask* peek(); 57 void remove(RenderTask* task); 58 59private: 60 RenderTask* mHead; 61 RenderTask* mTail; 62}; 63 64// Mimics android.view.Choreographer.FrameCallback 65class IFrameCallback { 66public: 67 virtual void doFrame() = 0; 68 69protected: 70 ~IFrameCallback() {} 71}; 72 73class ANDROID_API RenderThread : public Thread, protected Singleton<RenderThread> { 74public: 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); 78 ANDROID_API void queueAtFront(RenderTask* task); 79 void queueAt(RenderTask* task, nsecs_t runAtNs); 80 void remove(RenderTask* task); 81 82 // Mimics android.view.Choreographer 83 void postFrameCallback(IFrameCallback* callback); 84 bool removeFrameCallback(IFrameCallback* callback); 85 // 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); 88 89 void setFrameInterval(nsecs_t frameInterval); 90 91 TimeLord& timeLord() { return mTimeLord; } 92 RenderState& renderState() { return *mRenderState; } 93 EglManager& eglManager() { return *mEglManager; } 94 JankTracker& jankTracker() { return *mJankTracker; } 95 96protected: 97 virtual bool threadLoop(); 98 99private: 100 friend class Singleton<RenderThread>; 101 friend class DispatchFrameCallbacks; 102 friend class RenderProxy; 103 104 RenderThread(); 105 virtual ~RenderThread(); 106 107 void initThreadLocals(); 108 void initializeDisplayEventReceiver(); 109 static int displayEventReceiverCallback(int fd, int events, void* data); 110 void drainDisplayEventQueue(); 111 void dispatchFrameCallbacks(); 112 void requestVsync(); 113 114 // 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); 118 119 sp<Looper> mLooper; 120 Mutex mLock; 121 122 nsecs_t mNextWakeup; 123 TaskQueue mQueue; 124 125 DisplayEventReceiver* mDisplayEventReceiver; 126 bool mVsyncRequested; 127 std::set<IFrameCallback*> mFrameCallbacks; 128 // 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; 133 bool mFrameCallbackTaskPending; 134 DispatchFrameCallbacks* mFrameCallbackTask; 135 136 TimeLord mTimeLord; 137 RenderState* mRenderState; 138 EglManager* mEglManager; 139 140 JankTracker* mJankTracker; 141}; 142 143} /* namespace renderthread */ 144} /* namespace uirenderer */ 145} /* namespace android */ 146#endif /* RENDERTHREAD_H_ */ 147