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