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