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