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