| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#ifndef RENDERTHREAD_H_ |
| 18 |
#define RENDERTHREAD_H_ |
| 19 |
|
| 20 |
#include <surface_control_private.h> |
| 21 |
#include <GrDirectContext.h> |
| 22 |
#include <SkBitmap.h> |
| 23 |
#include <cutils/compiler.h> |
| 24 |
#include <private/android/choreographer.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 "ProfileDataContainer.h" |
| 35 |
#include "RenderTask.h" |
| 36 |
#include "TimeLord.h" |
| 37 |
#include "WebViewFunctorManager.h" |
| 38 |
#include "thread/ThreadBase.h" |
| 39 |
#include "utils/TimeUtils.h" |
| 40 |
|
| 41 |
namespace android { |
| 42 |
|
| 43 |
class Bitmap; |
| 44 |
|
| 45 |
namespace uirenderer { |
| 46 |
|
| 47 |
class AutoBackendTextureRelease; |
| 48 |
class Readback; |
| 49 |
class RenderState; |
| 50 |
class TestUtils; |
| 51 |
|
| 52 |
namespace skiapipeline { |
| 53 |
class VkFunctorDrawHandler; |
| 54 |
} |
| 55 |
|
| 56 |
namespace VectorDrawable { |
| 57 |
class Tree; |
| 58 |
} |
| 59 |
|
| 60 |
namespace renderthread { |
| 61 |
|
| 62 |
class CanvasContext; |
| 63 |
class EglManager; |
| 64 |
class RenderProxy; |
| 65 |
class VulkanManager; |
| 66 |
|
| 67 |
|
| 68 |
class IFrameCallback { |
| 69 |
public: |
| 70 |
virtual void doFrame() = 0; |
| 71 |
|
| 72 |
protected: |
| 73 |
virtual ~IFrameCallback() {} |
| 74 |
}; |
| 75 |
|
| 76 |
struct VsyncSource { |
| 77 |
virtual void requestNextVsync() = 0; |
| 78 |
virtual void drainPendingEvents() = 0; |
| 79 |
virtual ~VsyncSource() {} |
| 80 |
}; |
| 81 |
|
| 82 |
typedef ASurfaceControl* (*ASC_create)(ASurfaceControl* parent, const char* debug_name); |
| 83 |
typedef void (*ASC_acquire)(ASurfaceControl* control); |
| 84 |
typedef void (*ASC_release)(ASurfaceControl* control); |
| 85 |
|
| 86 |
typedef void (*ASC_registerSurfaceStatsListener)(ASurfaceControl* control, int32_t id, |
| 87 |
void* context, |
| 88 |
ASurfaceControl_SurfaceStatsListener func); |
| 89 |
typedef void (*ASC_unregisterSurfaceStatsListener)(void* context, |
| 90 |
ASurfaceControl_SurfaceStatsListener func); |
| 91 |
|
| 92 |
typedef int64_t (*ASCStats_getAcquireTime)(ASurfaceControlStats* stats); |
| 93 |
typedef uint64_t (*ASCStats_getFrameNumber)(ASurfaceControlStats* stats); |
| 94 |
|
| 95 |
typedef ASurfaceTransaction* (*AST_create)(); |
| 96 |
typedef void (*AST_delete)(ASurfaceTransaction* transaction); |
| 97 |
typedef void (*AST_apply)(ASurfaceTransaction* transaction); |
| 98 |
typedef void (*AST_reparent)(ASurfaceTransaction* aSurfaceTransaction, |
| 99 |
ASurfaceControl* aSurfaceControl, |
| 100 |
ASurfaceControl* newParentASurfaceControl); |
| 101 |
typedef void (*AST_setVisibility)(ASurfaceTransaction* transaction, |
| 102 |
ASurfaceControl* surface_control, int8_t visibility); |
| 103 |
typedef void (*AST_setZOrder)(ASurfaceTransaction* transaction, ASurfaceControl* surface_control, |
| 104 |
int32_t z_order); |
| 105 |
|
| 106 |
struct ASurfaceControlFunctions { |
| 107 |
ASurfaceControlFunctions(); |
| 108 |
|
| 109 |
ASC_create createFunc; |
| 110 |
ASC_acquire acquireFunc; |
| 111 |
ASC_release releaseFunc; |
| 112 |
ASC_registerSurfaceStatsListener registerListenerFunc; |
| 113 |
ASC_unregisterSurfaceStatsListener unregisterListenerFunc; |
| 114 |
ASCStats_getAcquireTime getAcquireTimeFunc; |
| 115 |
ASCStats_getFrameNumber getFrameNumberFunc; |
| 116 |
|
| 117 |
AST_create transactionCreateFunc; |
| 118 |
AST_delete transactionDeleteFunc; |
| 119 |
AST_apply transactionApplyFunc; |
| 120 |
AST_reparent transactionReparentFunc; |
| 121 |
AST_setVisibility transactionSetVisibilityFunc; |
| 122 |
AST_setZOrder transactionSetZOrderFunc; |
| 123 |
}; |
| 124 |
|
| 125 |
class ChoreographerSource; |
| 126 |
class DummyVsyncSource; |
| 127 |
|
| 128 |
typedef void (*JVMAttachHook)(const char* name); |
| 129 |
|
| 130 |
class RenderThread : private ThreadBase { |
| 131 |
PREVENT_COPY_AND_ASSIGN(RenderThread); |
| 132 |
|
| 133 |
public: |
| 134 |
|
| 135 |
static void setOnStartHook(JVMAttachHook onStartHook); |
| 136 |
static JVMAttachHook getOnStartHook(); |
| 137 |
|
| 138 |
WorkQueue& queue() { return ThreadBase::queue(); } |
| 139 |
|
| 140 |
|
| 141 |
void postFrameCallback(IFrameCallback* callback); |
| 142 |
bool removeFrameCallback(IFrameCallback* callback); |
| 143 |
|
| 144 |
|
| 145 |
void pushBackFrameCallback(IFrameCallback* callback); |
| 146 |
|
| 147 |
TimeLord& timeLord() { return mTimeLord; } |
| 148 |
RenderState& renderState() const { return *mRenderState; } |
| 149 |
EglManager& eglManager() const { return *mEglManager; } |
| 150 |
ProfileDataContainer& globalProfileData() { return mGlobalProfileData; } |
| 151 |
std::mutex& getJankDataMutex() { return mJankDataMutex; } |
| 152 |
Readback& readback(); |
| 153 |
|
| 154 |
GrDirectContext* getGrContext() const { return mGrContext.get(); } |
| 155 |
void setGrContext(sk_sp<GrDirectContext> cxt); |
| 156 |
sk_sp<GrDirectContext> requireGrContext(); |
| 157 |
|
| 158 |
CacheManager& cacheManager() { return *mCacheManager; } |
| 159 |
VulkanManager& vulkanManager(); |
| 160 |
|
| 161 |
sk_sp<Bitmap> allocateHardwareBitmap(SkBitmap& skBitmap); |
| 162 |
void dumpGraphicsMemory(int fd, bool includeProfileData); |
| 163 |
void getMemoryUsage(size_t* cpuUsage, size_t* gpuUsage); |
| 164 |
|
| 165 |
void requireGlContext(); |
| 166 |
void requireVkContext(); |
| 167 |
void destroyRenderingContext(); |
| 168 |
|
| 169 |
void preload(); |
| 170 |
|
| 171 |
const ASurfaceControlFunctions& getASurfaceControlFunctions() { |
| 172 |
return mASurfaceControlFunctions; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
static bool isCurrent(); |
| 182 |
|
| 183 |
static void initGrContextOptions(GrContextOptions& options); |
| 184 |
|
| 185 |
protected: |
| 186 |
virtual bool threadLoop() override; |
| 187 |
|
| 188 |
private: |
| 189 |
friend class DispatchFrameCallbacks; |
| 190 |
friend class RenderProxy; |
| 191 |
friend class DummyVsyncSource; |
| 192 |
friend class ChoreographerSource; |
| 193 |
friend class android::uirenderer::AutoBackendTextureRelease; |
| 194 |
friend class android::uirenderer::TestUtils; |
| 195 |
friend class android::uirenderer::WebViewFunctor; |
| 196 |
friend class android::uirenderer::skiapipeline::VkFunctorDrawHandler; |
| 197 |
friend class android::uirenderer::VectorDrawable::Tree; |
| 198 |
friend class sp<RenderThread>; |
| 199 |
|
| 200 |
RenderThread(); |
| 201 |
virtual ~RenderThread(); |
| 202 |
|
| 203 |
static bool hasInstance(); |
| 204 |
static RenderThread& getInstance(); |
| 205 |
|
| 206 |
void initThreadLocals(); |
| 207 |
void initializeChoreographer(); |
| 208 |
void setupFrameInterval(); |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
static int choreographerCallback(int fd, int events, void* data); |
| 213 |
|
| 214 |
static void extendedFrameCallback(const AChoreographerFrameCallbackData* cbData, void* data); |
| 215 |
void frameCallback(int64_t vsyncId, int64_t frameDeadline, int64_t frameTimeNanos, |
| 216 |
int64_t frameInterval); |
| 217 |
|
| 218 |
static void refreshRateCallback(int64_t vsyncPeriod, void* data); |
| 219 |
void drainDisplayEventQueue(); |
| 220 |
void dispatchFrameCallbacks(); |
| 221 |
void requestVsync(); |
| 222 |
|
| 223 |
AChoreographer* mChoreographer; |
| 224 |
VsyncSource* mVsyncSource; |
| 225 |
bool mVsyncRequested; |
| 226 |
std::set<IFrameCallback*> mFrameCallbacks; |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
std::set<IFrameCallback*> mPendingRegistrationFrameCallbacks; |
| 232 |
bool mFrameCallbackTaskPending; |
| 233 |
|
| 234 |
TimeLord mTimeLord; |
| 235 |
nsecs_t mDispatchFrameDelay = 4_ms; |
| 236 |
RenderState* mRenderState; |
| 237 |
EglManager* mEglManager; |
| 238 |
WebViewFunctorManager& mFunctorManager; |
| 239 |
|
| 240 |
ProfileDataContainer mGlobalProfileData; |
| 241 |
Readback* mReadback = nullptr; |
| 242 |
|
| 243 |
sk_sp<GrDirectContext> mGrContext; |
| 244 |
CacheManager* mCacheManager; |
| 245 |
sp<VulkanManager> mVkManager; |
| 246 |
|
| 247 |
ASurfaceControlFunctions mASurfaceControlFunctions; |
| 248 |
std::mutex mJankDataMutex; |
| 249 |
}; |
| 250 |
|
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
#endif |