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 "CacheManager.h" 24 #include "TimeLord.h" 25 #include "thread/ThreadBase.h" 26 27 #include <GrContext.h> 28 #include <SkBitmap.h> 29 #include <cutils/compiler.h> 30 #include <ui/DisplayInfo.h> 31 #include <utils/Looper.h> 32 #include <utils/Thread.h> 33 34 #include <thread/ThreadBase.h> 35 #include <memory> 36 #include <mutex> 37 #include <set> 38 39 namespace android { 40 41 class Bitmap; 42 43 namespace uirenderer { 44 45 class Readback; 46 class RenderState; 47 class TestUtils; 48 49 namespace renderthread { 50 51 class CanvasContext; 52 class EglManager; 53 class RenderProxy; 54 class VulkanManager; 55 56 // Mimics android.view.Choreographer.FrameCallback 57 class IFrameCallback { 58 public: 59 virtual void doFrame() = 0; 60 61 protected: ~IFrameCallback()62 ~IFrameCallback() {} 63 }; 64 65 struct VsyncSource { 66 virtual void requestNextVsync() = 0; 67 virtual nsecs_t latestVsyncEvent() = 0; ~VsyncSourceVsyncSource68 virtual ~VsyncSource() {} 69 }; 70 71 class DummyVsyncSource; 72 73 class RenderThread : private ThreadBase { 74 PREVENT_COPY_AND_ASSIGN(RenderThread); 75 76 public: 77 // Sets a callback that fires before any RenderThread setup has occured. 78 ANDROID_API static void setOnStartHook(void (*onStartHook)()); 79 queue()80 WorkQueue& queue() { return ThreadBase::queue(); } 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 timeLord()89 TimeLord& timeLord() { return mTimeLord; } renderState()90 RenderState& renderState() const { return *mRenderState; } eglManager()91 EglManager& eglManager() const { return *mEglManager; } globalProfileData()92 ProfileDataContainer& globalProfileData() { return mGlobalProfileData; } 93 Readback& readback(); 94 mainDisplayInfo()95 const DisplayInfo& mainDisplayInfo() { return mDisplayInfo; } 96 getGrContext()97 GrContext* getGrContext() const { return mGrContext.get(); } 98 void setGrContext(sk_sp<GrContext> cxt); 99 cacheManager()100 CacheManager& cacheManager() { return *mCacheManager; } vulkanManager()101 VulkanManager& vulkanManager() { return *mVkManager; } 102 103 sk_sp<Bitmap> allocateHardwareBitmap(SkBitmap& skBitmap); 104 void dumpGraphicsMemory(int fd); 105 106 /** 107 * isCurrent provides a way to query, if the caller is running on 108 * the render thread. 109 * 110 * @return true only if isCurrent is invoked from the render thread. 111 */ 112 static bool isCurrent(); 113 114 protected: 115 virtual bool threadLoop() override; 116 117 private: 118 friend class DispatchFrameCallbacks; 119 friend class RenderProxy; 120 friend class DummyVsyncSource; 121 friend class android::uirenderer::TestUtils; 122 123 RenderThread(); 124 virtual ~RenderThread(); 125 126 static bool hasInstance(); 127 static RenderThread& getInstance(); 128 129 void initThreadLocals(); 130 void initializeDisplayEventReceiver(); 131 static int displayEventReceiverCallback(int fd, int events, void* data); 132 void drainDisplayEventQueue(); 133 void dispatchFrameCallbacks(); 134 void requestVsync(); 135 136 DisplayInfo mDisplayInfo; 137 138 VsyncSource* mVsyncSource; 139 bool mVsyncRequested; 140 std::set<IFrameCallback*> mFrameCallbacks; 141 // We defer the actual registration of these callbacks until 142 // both mQueue *and* mDisplayEventReceiver have been drained off all 143 // immediate events. This makes sure that we catch the next vsync, not 144 // the previous one 145 std::set<IFrameCallback*> mPendingRegistrationFrameCallbacks; 146 bool mFrameCallbackTaskPending; 147 148 TimeLord mTimeLord; 149 RenderState* mRenderState; 150 EglManager* mEglManager; 151 152 ProfileDataContainer mGlobalProfileData; 153 Readback* mReadback = nullptr; 154 155 sk_sp<GrContext> mGrContext; 156 CacheManager* mCacheManager; 157 VulkanManager* mVkManager; 158 }; 159 160 } /* namespace renderthread */ 161 } /* namespace uirenderer */ 162 } /* namespace android */ 163 #endif /* RENDERTHREAD_H_ */ 164