RenderProxy.cpp revision c87be99c6ead0720a8918ea38ce3b25e5c49e1c6
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#include "RenderProxy.h" 18 19#include "DeferredLayerUpdater.h" 20#include "DisplayList.h" 21#include "LayerRenderer.h" 22#include "Rect.h" 23#include "renderthread/CanvasContext.h" 24#include "renderthread/RenderTask.h" 25#include "renderthread/RenderThread.h" 26#include "utils/Macros.h" 27 28namespace android { 29namespace uirenderer { 30namespace renderthread { 31 32#define ARGS(method) method ## Args 33 34#define CREATE_BRIDGE0(name) CREATE_BRIDGE(name,,,,,,,,) 35#define CREATE_BRIDGE1(name, a1) CREATE_BRIDGE(name, a1,,,,,,,) 36#define CREATE_BRIDGE2(name, a1, a2) CREATE_BRIDGE(name, a1,a2,,,,,,) 37#define CREATE_BRIDGE3(name, a1, a2, a3) CREATE_BRIDGE(name, a1,a2,a3,,,,,) 38#define CREATE_BRIDGE4(name, a1, a2, a3, a4) CREATE_BRIDGE(name, a1,a2,a3,a4,,,,) 39#define CREATE_BRIDGE5(name, a1, a2, a3, a4, a5) CREATE_BRIDGE(name, a1,a2,a3,a4,a5,,,) 40#define CREATE_BRIDGE6(name, a1, a2, a3, a4, a5, a6) CREATE_BRIDGE(name, a1,a2,a3,a4,a5,a6,,) 41#define CREATE_BRIDGE7(name, a1, a2, a3, a4, a5, a6, a7) CREATE_BRIDGE(name, a1,a2,a3,a4,a5,a6,a7,) 42#define CREATE_BRIDGE(name, a1, a2, a3, a4, a5, a6, a7, a8) \ 43 typedef struct { \ 44 a1; a2; a3; a4; a5; a6; a7; a8; \ 45 } ARGS(name); \ 46 static void* Bridge_ ## name(ARGS(name)* args) 47 48#define SETUP_TASK(method) \ 49 LOG_ALWAYS_FATAL_IF( METHOD_INVOKE_PAYLOAD_SIZE < sizeof(ARGS(method)), \ 50 "METHOD_INVOKE_PAYLOAD_SIZE %zu is smaller than sizeof(" #method "Args) %zu", \ 51 METHOD_INVOKE_PAYLOAD_SIZE, sizeof(ARGS(method))); \ 52 MethodInvokeRenderTask* task = new MethodInvokeRenderTask((RunnableMethod) Bridge_ ## method); \ 53 ARGS(method) *args = (ARGS(method) *) task->payload() 54 55enum class DumpFlags { 56 kFrameStats = 1 << 0, 57 kReset = 1 << 1, 58}; 59MAKE_FLAGS_ENUM(DumpFlags) 60 61CREATE_BRIDGE4(createContext, RenderThread* thread, bool translucent, 62 RenderNode* rootRenderNode, IContextFactory* contextFactory) { 63 return new CanvasContext(*args->thread, args->translucent, 64 args->rootRenderNode, args->contextFactory); 65} 66 67RenderProxy::RenderProxy(bool translucent, RenderNode* rootRenderNode, IContextFactory* contextFactory) 68 : mRenderThread(RenderThread::getInstance()) 69 , mContext(nullptr) { 70 SETUP_TASK(createContext); 71 args->translucent = translucent; 72 args->rootRenderNode = rootRenderNode; 73 args->thread = &mRenderThread; 74 args->contextFactory = contextFactory; 75 mContext = (CanvasContext*) postAndWait(task); 76 mDrawFrameTask.setContext(&mRenderThread, mContext); 77} 78 79RenderProxy::~RenderProxy() { 80 destroyContext(); 81} 82 83CREATE_BRIDGE1(destroyContext, CanvasContext* context) { 84 delete args->context; 85 return nullptr; 86} 87 88void RenderProxy::destroyContext() { 89 if (mContext) { 90 SETUP_TASK(destroyContext); 91 args->context = mContext; 92 mContext = nullptr; 93 mDrawFrameTask.setContext(nullptr, nullptr); 94 // This is also a fence as we need to be certain that there are no 95 // outstanding mDrawFrame tasks posted before it is destroyed 96 postAndWait(task); 97 } 98} 99 100CREATE_BRIDGE2(setFrameInterval, RenderThread* thread, nsecs_t frameIntervalNanos) { 101 args->thread->setFrameInterval(args->frameIntervalNanos); 102 return nullptr; 103} 104 105void RenderProxy::setFrameInterval(nsecs_t frameIntervalNanos) { 106 SETUP_TASK(setFrameInterval); 107 args->thread = &mRenderThread; 108 args->frameIntervalNanos = frameIntervalNanos; 109 post(task); 110} 111 112CREATE_BRIDGE2(setSwapBehavior, CanvasContext* context, SwapBehavior swapBehavior) { 113 args->context->setSwapBehavior(args->swapBehavior); 114 return nullptr; 115} 116 117void RenderProxy::setSwapBehavior(SwapBehavior swapBehavior) { 118 SETUP_TASK(setSwapBehavior); 119 args->context = mContext; 120 args->swapBehavior = swapBehavior; 121 post(task); 122} 123 124CREATE_BRIDGE1(loadSystemProperties, CanvasContext* context) { 125 bool needsRedraw = false; 126 if (Caches::hasInstance()) { 127 needsRedraw = Caches::getInstance().initProperties(); 128 } 129 if (args->context->profiler().loadSystemProperties()) { 130 needsRedraw = true; 131 } 132 return (void*) needsRedraw; 133} 134 135bool RenderProxy::loadSystemProperties() { 136 SETUP_TASK(loadSystemProperties); 137 args->context = mContext; 138 return (bool) postAndWait(task); 139} 140 141CREATE_BRIDGE2(initialize, CanvasContext* context, ANativeWindow* window) { 142 return (void*) args->context->initialize(args->window); 143} 144 145bool RenderProxy::initialize(const sp<ANativeWindow>& window) { 146 SETUP_TASK(initialize); 147 args->context = mContext; 148 args->window = window.get(); 149 return (bool) postAndWait(task); 150} 151 152CREATE_BRIDGE2(updateSurface, CanvasContext* context, ANativeWindow* window) { 153 args->context->updateSurface(args->window); 154 return nullptr; 155} 156 157void RenderProxy::updateSurface(const sp<ANativeWindow>& window) { 158 SETUP_TASK(updateSurface); 159 args->context = mContext; 160 args->window = window.get(); 161 postAndWait(task); 162} 163 164CREATE_BRIDGE2(pauseSurface, CanvasContext* context, ANativeWindow* window) { 165 return (void*) args->context->pauseSurface(args->window); 166} 167 168bool RenderProxy::pauseSurface(const sp<ANativeWindow>& window) { 169 SETUP_TASK(pauseSurface); 170 args->context = mContext; 171 args->window = window.get(); 172 return (bool) postAndWait(task); 173} 174 175CREATE_BRIDGE7(setup, CanvasContext* context, int width, int height, 176 Vector3 lightCenter, float lightRadius, 177 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) { 178 args->context->setup(args->width, args->height, args->lightCenter, args->lightRadius, 179 args->ambientShadowAlpha, args->spotShadowAlpha); 180 return nullptr; 181} 182 183void RenderProxy::setup(int width, int height, const Vector3& lightCenter, float lightRadius, 184 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha, float density) { 185 mDrawFrameTask.setDensity(density); 186 SETUP_TASK(setup); 187 args->context = mContext; 188 args->width = width; 189 args->height = height; 190 args->lightCenter = lightCenter; 191 args->lightRadius = lightRadius; 192 args->ambientShadowAlpha = ambientShadowAlpha; 193 args->spotShadowAlpha = spotShadowAlpha; 194 post(task); 195} 196 197CREATE_BRIDGE2(setOpaque, CanvasContext* context, bool opaque) { 198 args->context->setOpaque(args->opaque); 199 return nullptr; 200} 201 202void RenderProxy::setOpaque(bool opaque) { 203 SETUP_TASK(setOpaque); 204 args->context = mContext; 205 args->opaque = opaque; 206 post(task); 207} 208 209int64_t* RenderProxy::frameInfo() { 210 return mDrawFrameTask.frameInfo(); 211} 212 213int RenderProxy::syncAndDrawFrame() { 214 return mDrawFrameTask.drawFrame(); 215} 216 217CREATE_BRIDGE1(destroy, CanvasContext* context) { 218 args->context->destroy(); 219 return nullptr; 220} 221 222void RenderProxy::destroy() { 223 SETUP_TASK(destroy); 224 args->context = mContext; 225 // destroyCanvasAndSurface() needs a fence as when it returns the 226 // underlying BufferQueue is going to be released from under 227 // the render thread. 228 postAndWait(task); 229} 230 231CREATE_BRIDGE2(invokeFunctor, RenderThread* thread, Functor* functor) { 232 CanvasContext::invokeFunctor(*args->thread, args->functor); 233 return nullptr; 234} 235 236void RenderProxy::invokeFunctor(Functor* functor, bool waitForCompletion) { 237 ATRACE_CALL(); 238 RenderThread& thread = RenderThread::getInstance(); 239 SETUP_TASK(invokeFunctor); 240 args->thread = &thread; 241 args->functor = functor; 242 if (waitForCompletion) { 243 // waitForCompletion = true is expected to be fairly rare and only 244 // happen in destruction. Thus it should be fine to temporarily 245 // create a Mutex 246 staticPostAndWait(task); 247 } else { 248 thread.queue(task); 249 } 250} 251 252CREATE_BRIDGE2(runWithGlContext, CanvasContext* context, RenderTask* task) { 253 args->context->runWithGlContext(args->task); 254 return nullptr; 255} 256 257void RenderProxy::runWithGlContext(RenderTask* gltask) { 258 SETUP_TASK(runWithGlContext); 259 args->context = mContext; 260 args->task = gltask; 261 postAndWait(task); 262} 263 264CREATE_BRIDGE2(createTextureLayer, RenderThread* thread, CanvasContext* context) { 265 Layer* layer = args->context->createTextureLayer(); 266 if (!layer) return nullptr; 267 return new DeferredLayerUpdater(*args->thread, layer); 268} 269 270DeferredLayerUpdater* RenderProxy::createTextureLayer() { 271 SETUP_TASK(createTextureLayer); 272 args->context = mContext; 273 args->thread = &mRenderThread; 274 void* retval = postAndWait(task); 275 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval); 276 return layer; 277} 278 279CREATE_BRIDGE2(buildLayer, CanvasContext* context, RenderNode* node) { 280 args->context->buildLayer(args->node); 281 return nullptr; 282} 283 284void RenderProxy::buildLayer(RenderNode* node) { 285 SETUP_TASK(buildLayer); 286 args->context = mContext; 287 args->node = node; 288 postAndWait(task); 289} 290 291CREATE_BRIDGE3(copyLayerInto, CanvasContext* context, DeferredLayerUpdater* layer, 292 SkBitmap* bitmap) { 293 bool success = args->context->copyLayerInto(args->layer, args->bitmap); 294 return (void*) success; 295} 296 297bool RenderProxy::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) { 298 SETUP_TASK(copyLayerInto); 299 args->context = mContext; 300 args->layer = layer; 301 args->bitmap = bitmap; 302 return (bool) postAndWait(task); 303} 304 305void RenderProxy::pushLayerUpdate(DeferredLayerUpdater* layer) { 306 mDrawFrameTask.pushLayerUpdate(layer); 307} 308 309void RenderProxy::cancelLayerUpdate(DeferredLayerUpdater* layer) { 310 mDrawFrameTask.removeLayerUpdate(layer); 311} 312 313CREATE_BRIDGE1(detachSurfaceTexture, DeferredLayerUpdater* layer) { 314 args->layer->detachSurfaceTexture(); 315 return nullptr; 316} 317 318void RenderProxy::detachSurfaceTexture(DeferredLayerUpdater* layer) { 319 SETUP_TASK(detachSurfaceTexture); 320 args->layer = layer; 321 postAndWait(task); 322} 323 324CREATE_BRIDGE1(destroyHardwareResources, CanvasContext* context) { 325 args->context->destroyHardwareResources(); 326 return nullptr; 327} 328 329void RenderProxy::destroyHardwareResources() { 330 SETUP_TASK(destroyHardwareResources); 331 args->context = mContext; 332 post(task); 333} 334 335CREATE_BRIDGE2(timMemory, RenderThread* thread, int level) { 336 CanvasContext::trimMemory(*args->thread, args->level); 337 return nullptr; 338} 339 340void RenderProxy::trimMemory(int level) { 341 // Avoid creating a RenderThread to do a trimMemory. 342 if (RenderThread::hasInstance()) { 343 RenderThread& thread = RenderThread::getInstance(); 344 SETUP_TASK(timMemory); 345 args->thread = &thread; 346 args->level = level; 347 thread.queue(task); 348 } 349} 350 351CREATE_BRIDGE0(fence) { 352 // Intentionally empty 353 return nullptr; 354} 355 356template <typename T> 357void UNUSED(T t) {} 358 359void RenderProxy::fence() { 360 SETUP_TASK(fence); 361 UNUSED(args); 362 postAndWait(task); 363} 364 365CREATE_BRIDGE1(stopDrawing, CanvasContext* context) { 366 args->context->stopDrawing(); 367 return nullptr; 368} 369 370void RenderProxy::stopDrawing() { 371 SETUP_TASK(stopDrawing); 372 args->context = mContext; 373 postAndWait(task); 374} 375 376CREATE_BRIDGE1(notifyFramePending, CanvasContext* context) { 377 args->context->notifyFramePending(); 378 return nullptr; 379} 380 381void RenderProxy::notifyFramePending() { 382 SETUP_TASK(notifyFramePending); 383 args->context = mContext; 384 mRenderThread.queueAtFront(task); 385} 386 387CREATE_BRIDGE3(dumpProfileInfo, CanvasContext* context, int fd, int dumpFlags) { 388 args->context->profiler().dumpData(args->fd); 389 if (args->dumpFlags & DumpFlags::kFrameStats) { 390 args->context->dumpFrames(args->fd); 391 } 392 if (args->dumpFlags & DumpFlags::kReset) { 393 args->context->resetFrameStats(); 394 } 395 return nullptr; 396} 397 398void RenderProxy::dumpProfileInfo(int fd, int dumpFlags) { 399 SETUP_TASK(dumpProfileInfo); 400 args->context = mContext; 401 args->fd = fd; 402 args->dumpFlags = dumpFlags; 403 postAndWait(task); 404} 405 406CREATE_BRIDGE2(dumpGraphicsMemory, int fd, RenderThread* thread) { 407 args->thread->jankTracker().dump(args->fd); 408 409 FILE *file = fdopen(args->fd, "a"); 410 if (Caches::hasInstance()) { 411 String8 cachesLog; 412 Caches::getInstance().dumpMemoryUsage(cachesLog); 413 fprintf(file, "\nCaches:\n%s\n", cachesLog.string()); 414 } else { 415 fprintf(file, "\nNo caches instance.\n"); 416 } 417 fflush(file); 418 return nullptr; 419} 420 421void RenderProxy::dumpGraphicsMemory(int fd) { 422 SETUP_TASK(dumpGraphicsMemory); 423 args->fd = fd; 424 args->thread = &RenderThread::getInstance(); 425 staticPostAndWait(task); 426} 427 428CREATE_BRIDGE4(setTextureAtlas, RenderThread* thread, GraphicBuffer* buffer, int64_t* map, size_t size) { 429 CanvasContext::setTextureAtlas(*args->thread, args->buffer, args->map, args->size); 430 args->buffer->decStrong(nullptr); 431 return nullptr; 432} 433 434void RenderProxy::setTextureAtlas(const sp<GraphicBuffer>& buffer, int64_t* map, size_t size) { 435 SETUP_TASK(setTextureAtlas); 436 args->thread = &mRenderThread; 437 args->buffer = buffer.get(); 438 args->buffer->incStrong(nullptr); 439 args->map = map; 440 args->size = size; 441 post(task); 442} 443 444void RenderProxy::post(RenderTask* task) { 445 mRenderThread.queue(task); 446} 447 448void* RenderProxy::postAndWait(MethodInvokeRenderTask* task) { 449 void* retval; 450 task->setReturnPtr(&retval); 451 SignalingRenderTask syncTask(task, &mSyncMutex, &mSyncCondition); 452 AutoMutex _lock(mSyncMutex); 453 mRenderThread.queue(&syncTask); 454 mSyncCondition.wait(mSyncMutex); 455 return retval; 456} 457 458void* RenderProxy::staticPostAndWait(MethodInvokeRenderTask* task) { 459 RenderThread& thread = RenderThread::getInstance(); 460 void* retval; 461 task->setReturnPtr(&retval); 462 Mutex mutex; 463 Condition condition; 464 SignalingRenderTask syncTask(task, &mutex, &condition); 465 AutoMutex _lock(mutex); 466 thread.queue(&syncTask); 467 condition.wait(mutex); 468 return retval; 469} 470 471} /* namespace renderthread */ 472} /* namespace uirenderer */ 473} /* namespace android */ 474