RenderProxy.cpp revision 50210d912925aef14e4ce69be82e4949122a3cd9
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(setSwapBehavior, CanvasContext* context, SwapBehavior swapBehavior) { 101 args->context->setSwapBehavior(args->swapBehavior); 102 return nullptr; 103} 104 105void RenderProxy::setSwapBehavior(SwapBehavior swapBehavior) { 106 SETUP_TASK(setSwapBehavior); 107 args->context = mContext; 108 args->swapBehavior = swapBehavior; 109 post(task); 110} 111 112CREATE_BRIDGE1(loadSystemProperties, CanvasContext* context) { 113 bool needsRedraw = false; 114 if (Caches::hasInstance()) { 115 needsRedraw = Properties::load(); 116 } 117 if (args->context->profiler().consumeProperties()) { 118 needsRedraw = true; 119 } 120 return (void*) needsRedraw; 121} 122 123bool RenderProxy::loadSystemProperties() { 124 SETUP_TASK(loadSystemProperties); 125 args->context = mContext; 126 return (bool) postAndWait(task); 127} 128 129CREATE_BRIDGE2(setName, CanvasContext* context, const char* name) { 130 args->context->setName(std::string(args->name)); 131 return nullptr; 132} 133 134void RenderProxy::setName(const char* name) { 135 SETUP_TASK(setName); 136 args->context = mContext; 137 args->name = name; 138 postAndWait(task); // block since name/value pointers owned by caller 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_BRIDGE6(setup, CanvasContext* context, int width, int height, 176 float lightRadius, uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) { 177 args->context->setup(args->width, args->height, args->lightRadius, 178 args->ambientShadowAlpha, args->spotShadowAlpha); 179 return nullptr; 180} 181 182void RenderProxy::setup(int width, int height, float lightRadius, 183 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) { 184 SETUP_TASK(setup); 185 args->context = mContext; 186 args->width = width; 187 args->height = height; 188 args->lightRadius = lightRadius; 189 args->ambientShadowAlpha = ambientShadowAlpha; 190 args->spotShadowAlpha = spotShadowAlpha; 191 post(task); 192} 193 194CREATE_BRIDGE2(setLightCenter, CanvasContext* context, Vector3 lightCenter) { 195 args->context->setLightCenter(args->lightCenter); 196 return nullptr; 197} 198 199void RenderProxy::setLightCenter(const Vector3& lightCenter) { 200 SETUP_TASK(setLightCenter); 201 args->context = mContext; 202 args->lightCenter = lightCenter; 203 post(task); 204} 205 206CREATE_BRIDGE2(setOpaque, CanvasContext* context, bool opaque) { 207 args->context->setOpaque(args->opaque); 208 return nullptr; 209} 210 211void RenderProxy::setOpaque(bool opaque) { 212 SETUP_TASK(setOpaque); 213 args->context = mContext; 214 args->opaque = opaque; 215 post(task); 216} 217 218int64_t* RenderProxy::frameInfo() { 219 return mDrawFrameTask.frameInfo(); 220} 221 222int RenderProxy::syncAndDrawFrame() { 223 return mDrawFrameTask.drawFrame(); 224} 225 226CREATE_BRIDGE1(destroy, CanvasContext* context) { 227 args->context->destroy(); 228 return nullptr; 229} 230 231void RenderProxy::destroy() { 232 SETUP_TASK(destroy); 233 args->context = mContext; 234 // destroyCanvasAndSurface() needs a fence as when it returns the 235 // underlying BufferQueue is going to be released from under 236 // the render thread. 237 postAndWait(task); 238} 239 240CREATE_BRIDGE2(invokeFunctor, RenderThread* thread, Functor* functor) { 241 CanvasContext::invokeFunctor(*args->thread, args->functor); 242 return nullptr; 243} 244 245void RenderProxy::invokeFunctor(Functor* functor, bool waitForCompletion) { 246 ATRACE_CALL(); 247 RenderThread& thread = RenderThread::getInstance(); 248 SETUP_TASK(invokeFunctor); 249 args->thread = &thread; 250 args->functor = functor; 251 if (waitForCompletion) { 252 // waitForCompletion = true is expected to be fairly rare and only 253 // happen in destruction. Thus it should be fine to temporarily 254 // create a Mutex 255 staticPostAndWait(task); 256 } else { 257 thread.queue(task); 258 } 259} 260 261CREATE_BRIDGE2(runWithGlContext, CanvasContext* context, RenderTask* task) { 262 args->context->runWithGlContext(args->task); 263 return nullptr; 264} 265 266void RenderProxy::runWithGlContext(RenderTask* gltask) { 267 SETUP_TASK(runWithGlContext); 268 args->context = mContext; 269 args->task = gltask; 270 postAndWait(task); 271} 272 273CREATE_BRIDGE2(createTextureLayer, RenderThread* thread, CanvasContext* context) { 274 Layer* layer = args->context->createTextureLayer(); 275 if (!layer) return nullptr; 276 return new DeferredLayerUpdater(*args->thread, layer); 277} 278 279DeferredLayerUpdater* RenderProxy::createTextureLayer() { 280 SETUP_TASK(createTextureLayer); 281 args->context = mContext; 282 args->thread = &mRenderThread; 283 void* retval = postAndWait(task); 284 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval); 285 return layer; 286} 287 288CREATE_BRIDGE2(buildLayer, CanvasContext* context, RenderNode* node) { 289 args->context->buildLayer(args->node); 290 return nullptr; 291} 292 293void RenderProxy::buildLayer(RenderNode* node) { 294 SETUP_TASK(buildLayer); 295 args->context = mContext; 296 args->node = node; 297 postAndWait(task); 298} 299 300CREATE_BRIDGE3(copyLayerInto, CanvasContext* context, DeferredLayerUpdater* layer, 301 SkBitmap* bitmap) { 302 bool success = args->context->copyLayerInto(args->layer, args->bitmap); 303 return (void*) success; 304} 305 306bool RenderProxy::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap& bitmap) { 307 SETUP_TASK(copyLayerInto); 308 args->context = mContext; 309 args->layer = layer; 310 args->bitmap = &bitmap; 311 return (bool) postAndWait(task); 312} 313 314void RenderProxy::pushLayerUpdate(DeferredLayerUpdater* layer) { 315 mDrawFrameTask.pushLayerUpdate(layer); 316} 317 318void RenderProxy::cancelLayerUpdate(DeferredLayerUpdater* layer) { 319 mDrawFrameTask.removeLayerUpdate(layer); 320} 321 322CREATE_BRIDGE1(detachSurfaceTexture, DeferredLayerUpdater* layer) { 323 args->layer->detachSurfaceTexture(); 324 return nullptr; 325} 326 327void RenderProxy::detachSurfaceTexture(DeferredLayerUpdater* layer) { 328 SETUP_TASK(detachSurfaceTexture); 329 args->layer = layer; 330 postAndWait(task); 331} 332 333CREATE_BRIDGE1(destroyHardwareResources, CanvasContext* context) { 334 args->context->destroyHardwareResources(); 335 return nullptr; 336} 337 338void RenderProxy::destroyHardwareResources() { 339 SETUP_TASK(destroyHardwareResources); 340 args->context = mContext; 341 post(task); 342} 343 344CREATE_BRIDGE2(trimMemory, RenderThread* thread, int level) { 345 CanvasContext::trimMemory(*args->thread, args->level); 346 return nullptr; 347} 348 349void RenderProxy::trimMemory(int level) { 350 // Avoid creating a RenderThread to do a trimMemory. 351 if (RenderThread::hasInstance()) { 352 RenderThread& thread = RenderThread::getInstance(); 353 SETUP_TASK(trimMemory); 354 args->thread = &thread; 355 args->level = level; 356 thread.queue(task); 357 } 358} 359 360CREATE_BRIDGE2(overrideProperty, const char* name, const char* value) { 361 Properties::overrideProperty(args->name, args->value); 362 return nullptr; 363} 364 365void RenderProxy::overrideProperty(const char* name, const char* value) { 366 SETUP_TASK(overrideProperty); 367 args->name = name; 368 args->value = value; 369 staticPostAndWait(task); // expensive, but block here since name/value pointers owned by caller 370} 371 372CREATE_BRIDGE0(fence) { 373 // Intentionally empty 374 return nullptr; 375} 376 377template <typename T> 378void UNUSED(T t) {} 379 380void RenderProxy::fence() { 381 SETUP_TASK(fence); 382 UNUSED(args); 383 postAndWait(task); 384} 385 386CREATE_BRIDGE1(stopDrawing, CanvasContext* context) { 387 args->context->stopDrawing(); 388 return nullptr; 389} 390 391void RenderProxy::stopDrawing() { 392 SETUP_TASK(stopDrawing); 393 args->context = mContext; 394 postAndWait(task); 395} 396 397CREATE_BRIDGE1(notifyFramePending, CanvasContext* context) { 398 args->context->notifyFramePending(); 399 return nullptr; 400} 401 402void RenderProxy::notifyFramePending() { 403 SETUP_TASK(notifyFramePending); 404 args->context = mContext; 405 mRenderThread.queueAtFront(task); 406} 407 408CREATE_BRIDGE4(dumpProfileInfo, CanvasContext* context, RenderThread* thread, 409 int fd, int dumpFlags) { 410 args->context->profiler().dumpData(args->fd); 411 args->thread->jankTracker().dump(args->fd); 412 if (args->dumpFlags & DumpFlags::kFrameStats) { 413 args->context->dumpFrames(args->fd); 414 } 415 if (args->dumpFlags & DumpFlags::kReset) { 416 args->context->resetFrameStats(); 417 } 418 return nullptr; 419} 420 421void RenderProxy::dumpProfileInfo(int fd, int dumpFlags) { 422 SETUP_TASK(dumpProfileInfo); 423 args->context = mContext; 424 args->thread = &mRenderThread; 425 args->fd = fd; 426 args->dumpFlags = dumpFlags; 427 postAndWait(task); 428} 429 430CREATE_BRIDGE1(resetProfileInfo, CanvasContext* context) { 431 args->context->resetFrameStats(); 432 return nullptr; 433} 434 435void RenderProxy::resetProfileInfo() { 436 SETUP_TASK(resetProfileInfo); 437 args->context = mContext; 438 postAndWait(task); 439} 440 441CREATE_BRIDGE2(dumpGraphicsMemory, int fd, RenderThread* thread) { 442 args->thread->jankTracker().dump(args->fd); 443 444 FILE *file = fdopen(args->fd, "a"); 445 if (Caches::hasInstance()) { 446 String8 cachesLog; 447 Caches::getInstance().dumpMemoryUsage(cachesLog); 448 fprintf(file, "\nCaches:\n%s\n", cachesLog.string()); 449 } else { 450 fprintf(file, "\nNo caches instance.\n"); 451 } 452 fflush(file); 453 return nullptr; 454} 455 456void RenderProxy::dumpGraphicsMemory(int fd) { 457 if (!RenderThread::hasInstance()) return; 458 SETUP_TASK(dumpGraphicsMemory); 459 args->fd = fd; 460 args->thread = &RenderThread::getInstance(); 461 staticPostAndWait(task); 462} 463 464CREATE_BRIDGE4(setTextureAtlas, RenderThread* thread, GraphicBuffer* buffer, int64_t* map, size_t size) { 465 CanvasContext::setTextureAtlas(*args->thread, args->buffer, args->map, args->size); 466 args->buffer->decStrong(nullptr); 467 return nullptr; 468} 469 470void RenderProxy::setTextureAtlas(const sp<GraphicBuffer>& buffer, int64_t* map, size_t size) { 471 SETUP_TASK(setTextureAtlas); 472 args->thread = &mRenderThread; 473 args->buffer = buffer.get(); 474 args->buffer->incStrong(nullptr); 475 args->map = map; 476 args->size = size; 477 post(task); 478} 479 480CREATE_BRIDGE2(setProcessStatsBuffer, RenderThread* thread, int fd) { 481 args->thread->jankTracker().switchStorageToAshmem(args->fd); 482 close(args->fd); 483 return nullptr; 484} 485 486void RenderProxy::setProcessStatsBuffer(int fd) { 487 SETUP_TASK(setProcessStatsBuffer); 488 args->thread = &mRenderThread; 489 args->fd = dup(fd); 490 post(task); 491} 492 493void RenderProxy::post(RenderTask* task) { 494 mRenderThread.queue(task); 495} 496 497void* RenderProxy::postAndWait(MethodInvokeRenderTask* task) { 498 void* retval; 499 task->setReturnPtr(&retval); 500 SignalingRenderTask syncTask(task, &mSyncMutex, &mSyncCondition); 501 AutoMutex _lock(mSyncMutex); 502 mRenderThread.queue(&syncTask); 503 mSyncCondition.wait(mSyncMutex); 504 return retval; 505} 506 507void* RenderProxy::staticPostAndWait(MethodInvokeRenderTask* task) { 508 RenderThread& thread = RenderThread::getInstance(); 509 void* retval; 510 task->setReturnPtr(&retval); 511 Mutex mutex; 512 Condition condition; 513 SignalingRenderTask syncTask(task, &mutex, &condition); 514 AutoMutex _lock(mutex); 515 thread.queue(&syncTask); 516 condition.wait(mutex); 517 return retval; 518} 519 520} /* namespace renderthread */ 521} /* namespace uirenderer */ 522} /* namespace android */ 523