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