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 <GrDirectContext.h> 21 #include <SkBitmap.h> 22 #include <cutils/compiler.h> 23 #include <private/android/choreographer.h> 24 #include <surface_control_private.h> 25 #include <thread/ThreadBase.h> 26 #include <utils/Looper.h> 27 #include <utils/Thread.h> 28 29 #include <memory> 30 #include <mutex> 31 #include <set> 32 33 #include "CacheManager.h" 34 #include "MemoryPolicy.h" 35 #include "ProfileDataContainer.h" 36 #include "RenderTask.h" 37 #include "TimeLord.h" 38 #include "WebViewFunctorManager.h" 39 #include "thread/ThreadBase.h" 40 #include "utils/TimeUtils.h" 41 42 namespace android { 43 44 class Bitmap; 45 46 namespace uirenderer { 47 48 class AutoBackendTextureRelease; 49 class Readback; 50 class RenderState; 51 class TestUtils; 52 53 namespace skiapipeline { 54 class VkFunctorDrawHandler; 55 } 56 57 namespace VectorDrawable { 58 class Tree; 59 } 60 61 namespace renderthread { 62 63 class CanvasContext; 64 class EglManager; 65 class RenderProxy; 66 class VulkanManager; 67 68 // Mimics android.view.Choreographer.FrameCallback 69 class IFrameCallback { 70 public: 71 virtual void doFrame() = 0; 72 73 protected: ~IFrameCallback()74 virtual ~IFrameCallback() {} 75 }; 76 77 struct VsyncSource { 78 virtual void requestNextVsync() = 0; 79 virtual void drainPendingEvents() = 0; ~VsyncSourceVsyncSource80 virtual ~VsyncSource() {} 81 }; 82 83 typedef ASurfaceControl* (*ASC_create)(ASurfaceControl* parent, const char* debug_name); 84 typedef void (*ASC_acquire)(ASurfaceControl* control); 85 typedef void (*ASC_release)(ASurfaceControl* control); 86 87 typedef void (*ASC_registerSurfaceStatsListener)(ASurfaceControl* control, int32_t id, 88 void* context, 89 ASurfaceControl_SurfaceStatsListener func); 90 typedef void (*ASC_unregisterSurfaceStatsListener)(void* context, 91 ASurfaceControl_SurfaceStatsListener func); 92 93 typedef int64_t (*ASCStats_getAcquireTime)(ASurfaceControlStats* stats); 94 typedef uint64_t (*ASCStats_getFrameNumber)(ASurfaceControlStats* stats); 95 96 typedef ASurfaceTransaction* (*AST_create)(); 97 typedef void (*AST_delete)(ASurfaceTransaction* transaction); 98 typedef void (*AST_apply)(ASurfaceTransaction* transaction); 99 typedef void (*AST_reparent)(ASurfaceTransaction* aSurfaceTransaction, 100 ASurfaceControl* aSurfaceControl, 101 ASurfaceControl* newParentASurfaceControl); 102 typedef void (*AST_setVisibility)(ASurfaceTransaction* transaction, 103 ASurfaceControl* surface_control, int8_t visibility); 104 typedef void (*AST_setZOrder)(ASurfaceTransaction* transaction, ASurfaceControl* surface_control, 105 int32_t z_order); 106 107 struct ASurfaceControlFunctions { 108 ASurfaceControlFunctions(); 109 110 ASC_create createFunc; 111 ASC_acquire acquireFunc; 112 ASC_release releaseFunc; 113 ASC_registerSurfaceStatsListener registerListenerFunc; 114 ASC_unregisterSurfaceStatsListener unregisterListenerFunc; 115 ASCStats_getAcquireTime getAcquireTimeFunc; 116 ASCStats_getFrameNumber getFrameNumberFunc; 117 118 AST_create transactionCreateFunc; 119 AST_delete transactionDeleteFunc; 120 AST_apply transactionApplyFunc; 121 AST_reparent transactionReparentFunc; 122 AST_setVisibility transactionSetVisibilityFunc; 123 AST_setZOrder transactionSetZOrderFunc; 124 }; 125 126 class ChoreographerSource; 127 class DummyVsyncSource; 128 129 typedef void (*JVMAttachHook)(const char* name); 130 131 class RenderThread : private ThreadBase { 132 PREVENT_COPY_AND_ASSIGN(RenderThread); 133 134 public: 135 // Sets a callback that fires before any RenderThread setup has occurred. 136 static void setOnStartHook(JVMAttachHook onStartHook); 137 static JVMAttachHook getOnStartHook(); 138 queue()139 WorkQueue& queue() { return ThreadBase::queue(); } 140 141 // Mimics android.view.Choreographer 142 void postFrameCallback(IFrameCallback* callback); 143 bool removeFrameCallback(IFrameCallback* callback); 144 // If the callback is currently registered, it will be pushed back until 145 // the next vsync. If it is not currently registered this does nothing. 146 void pushBackFrameCallback(IFrameCallback* callback); 147 timeLord()148 TimeLord& timeLord() { return mTimeLord; } renderState()149 RenderState& renderState() const { return *mRenderState; } eglManager()150 EglManager& eglManager() const { return *mEglManager; } globalProfileData()151 ProfileDataContainer& globalProfileData() { return mGlobalProfileData; } getJankDataMutex()152 std::mutex& getJankDataMutex() { return mJankDataMutex; } 153 Readback& readback(); 154 getGrContext()155 GrDirectContext* getGrContext() const { return mGrContext.get(); } 156 void setGrContext(sk_sp<GrDirectContext> cxt); 157 sk_sp<GrDirectContext> requireGrContext(); 158 cacheManager()159 CacheManager& cacheManager() { return *mCacheManager; } 160 VulkanManager& vulkanManager(); 161 162 sk_sp<Bitmap> allocateHardwareBitmap(SkBitmap& skBitmap); 163 void dumpGraphicsMemory(int fd, bool includeProfileData); 164 void getMemoryUsage(size_t* cpuUsage, size_t* gpuUsage); 165 166 void requireGlContext(); 167 void requireVkContext(); 168 void destroyRenderingContext(); 169 170 void preload(); 171 getASurfaceControlFunctions()172 const ASurfaceControlFunctions& getASurfaceControlFunctions() { 173 return mASurfaceControlFunctions; 174 } 175 176 void trimMemory(TrimLevel level); 177 void trimCaches(CacheTrimLevel level); 178 179 /** 180 * isCurrent provides a way to query, if the caller is running on 181 * the render thread. 182 * 183 * @return true only if isCurrent is invoked from the render thread. 184 */ 185 static bool isCurrent(); 186 187 static void initGrContextOptions(GrContextOptions& options); 188 189 protected: 190 virtual bool threadLoop() override; 191 192 private: 193 friend class DispatchFrameCallbacks; 194 friend class RenderProxy; 195 friend class DummyVsyncSource; 196 friend class ChoreographerSource; 197 friend class android::uirenderer::AutoBackendTextureRelease; 198 friend class android::uirenderer::TestUtils; 199 friend class android::uirenderer::WebViewFunctor; 200 friend class android::uirenderer::skiapipeline::VkFunctorDrawHandler; 201 friend class android::uirenderer::VectorDrawable::Tree; 202 friend class sp<RenderThread>; 203 204 RenderThread(); 205 virtual ~RenderThread(); 206 207 static bool hasInstance(); 208 static RenderThread& getInstance(); 209 210 void initThreadLocals(); 211 void initializeChoreographer(); 212 void setupFrameInterval(); 213 // Callbacks for choreographer events: 214 // choreographerCallback will call AChoreograper_handleEvent to call the 215 // corresponding callbacks for each display event type 216 static int choreographerCallback(int fd, int events, void* data); 217 // Callback that will be run on vsync ticks. 218 static void extendedFrameCallback(const AChoreographerFrameCallbackData* cbData, void* data); 219 void frameCallback(int64_t vsyncId, int64_t frameDeadline, int64_t frameTimeNanos, 220 int64_t frameInterval); 221 // Callback that will be run whenver there is a refresh rate change. 222 static void refreshRateCallback(int64_t vsyncPeriod, void* data); 223 void drainDisplayEventQueue(); 224 void dispatchFrameCallbacks(); 225 void requestVsync(); 226 227 AChoreographer* mChoreographer; 228 VsyncSource* mVsyncSource; 229 bool mVsyncRequested; 230 std::set<IFrameCallback*> mFrameCallbacks; 231 // We defer the actual registration of these callbacks until 232 // both mQueue *and* mDisplayEventReceiver have been drained off all 233 // immediate events. This makes sure that we catch the next vsync, not 234 // the previous one 235 std::set<IFrameCallback*> mPendingRegistrationFrameCallbacks; 236 bool mFrameCallbackTaskPending; 237 238 TimeLord mTimeLord; 239 RenderState* mRenderState; 240 EglManager* mEglManager; 241 WebViewFunctorManager& mFunctorManager; 242 243 ProfileDataContainer mGlobalProfileData; 244 Readback* mReadback = nullptr; 245 246 sk_sp<GrDirectContext> mGrContext; 247 CacheManager* mCacheManager; 248 sp<VulkanManager> mVkManager; 249 250 ASurfaceControlFunctions mASurfaceControlFunctions; 251 std::mutex mJankDataMutex; 252 }; 253 254 } /* namespace renderthread */ 255 } /* namespace uirenderer */ 256 } /* namespace android */ 257 #endif /* RENDERTHREAD_H_ */ 258