RenderProxy.cpp revision 8de65a8e05285df52a1e6f0c1d5616dd233298a7
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_BRIDGE(name, a1, a2, a3, a4, a5, a6, a7, a8) \ 42 typedef struct { \ 43 a1; a2; a3; a4; a5; a6; a7; a8; \ 44 } ARGS(name); \ 45 static void* Bridge_ ## name(ARGS(name)* args) 46 47#define SETUP_TASK(method) \ 48 LOG_ALWAYS_FATAL_IF( METHOD_INVOKE_PAYLOAD_SIZE < sizeof(ARGS(method)), \ 49 "METHOD_INVOKE_PAYLOAD_SIZE %d is smaller than sizeof(" #method "Args) %d", \ 50 METHOD_INVOKE_PAYLOAD_SIZE, sizeof(ARGS(method))); \ 51 MethodInvokeRenderTask* task = new MethodInvokeRenderTask((RunnableMethod) Bridge_ ## method); \ 52 ARGS(method) *args = (ARGS(method) *) task->payload() 53 54CREATE_BRIDGE1(createContext, bool translucent) { 55 return new CanvasContext(args->translucent); 56} 57 58RenderProxy::RenderProxy(bool translucent) 59 : mRenderThread(RenderThread::getInstance()) 60 , mContext(0) { 61 SETUP_TASK(createContext); 62 args->translucent = translucent; 63 mContext = (CanvasContext*) postAndWait(task); 64 mDrawFrameTask.setContext(mContext); 65} 66 67RenderProxy::~RenderProxy() { 68 destroyContext(); 69} 70 71CREATE_BRIDGE1(destroyContext, CanvasContext* context) { 72 delete args->context; 73 return NULL; 74} 75 76void RenderProxy::destroyContext() { 77 if (mContext) { 78 // Flush any pending changes to ensure all garbage is destroyed 79 mDrawFrameTask.flushStateChanges(&mRenderThread); 80 81 SETUP_TASK(destroyContext); 82 args->context = mContext; 83 mContext = 0; 84 mDrawFrameTask.setContext(0); 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(initialize, CanvasContext* context, EGLNativeWindowType window) { 92 return (void*) args->context->initialize(args->window); 93} 94 95bool RenderProxy::initialize(EGLNativeWindowType window) { 96 SETUP_TASK(initialize); 97 args->context = mContext; 98 args->window = window; 99 return (bool) postAndWait(task); 100} 101 102CREATE_BRIDGE2(updateSurface, CanvasContext* context, EGLNativeWindowType window) { 103 args->context->updateSurface(args->window); 104 return NULL; 105} 106 107void RenderProxy::updateSurface(EGLNativeWindowType window) { 108 SETUP_TASK(updateSurface); 109 args->context = mContext; 110 args->window = window; 111 post(task); 112} 113 114CREATE_BRIDGE3(setup, CanvasContext* context, int width, int height) { 115 args->context->setup(args->width, args->height); 116 return NULL; 117} 118 119void RenderProxy::setup(int width, int height) { 120 SETUP_TASK(setup); 121 args->context = mContext; 122 args->width = width; 123 args->height = height; 124 post(task); 125} 126 127void RenderProxy::drawDisplayList(RenderNode* displayList, 128 int dirtyLeft, int dirtyTop, int dirtyRight, int dirtyBottom) { 129 mDrawFrameTask.setRenderNode(displayList); 130 mDrawFrameTask.setDirty(dirtyLeft, dirtyTop, dirtyRight, dirtyBottom); 131 mDrawFrameTask.drawFrame(&mRenderThread); 132} 133 134CREATE_BRIDGE1(destroyCanvas, CanvasContext* context) { 135 args->context->destroyCanvas(); 136 return NULL; 137} 138 139void RenderProxy::destroyCanvas() { 140 // If the canvas is being destroyed we won't be drawing again anytime soon 141 // So flush any pending state changes to allow for resource cleanup. 142 mDrawFrameTask.flushStateChanges(&mRenderThread); 143 144 SETUP_TASK(destroyCanvas); 145 args->context = mContext; 146 post(task); 147} 148 149CREATE_BRIDGE2(attachFunctor, CanvasContext* context, Functor* functor) { 150 args->context->attachFunctor(args->functor); 151 return NULL; 152} 153 154void RenderProxy::attachFunctor(Functor* functor) { 155 SETUP_TASK(attachFunctor); 156 args->context = mContext; 157 args->functor = functor; 158 post(task); 159} 160 161CREATE_BRIDGE2(detachFunctor, CanvasContext* context, Functor* functor) { 162 args->context->detachFunctor(args->functor); 163 return NULL; 164} 165 166void RenderProxy::detachFunctor(Functor* functor) { 167 SETUP_TASK(detachFunctor); 168 args->context = mContext; 169 args->functor = functor; 170 post(task); 171} 172 173CREATE_BRIDGE2(invokeFunctor, CanvasContext* context, Functor* functor) { 174 args->context->invokeFunctor(args->functor); 175 return NULL; 176} 177 178void RenderProxy::invokeFunctor(Functor* functor, bool waitForCompletion) { 179 SETUP_TASK(invokeFunctor); 180 args->context = mContext; 181 args->functor = functor; 182 if (waitForCompletion) { 183 postAndWait(task); 184 } else { 185 post(task); 186 } 187} 188 189CREATE_BRIDGE2(runWithGlContext, CanvasContext* context, RenderTask* task) { 190 args->context->runWithGlContext(args->task); 191 return NULL; 192} 193 194void RenderProxy::runWithGlContext(RenderTask* gltask) { 195 SETUP_TASK(runWithGlContext); 196 args->context = mContext; 197 args->task = gltask; 198 postAndWait(task); 199} 200 201CREATE_BRIDGE3(createDisplayListLayer, CanvasContext* context, int width, int height) { 202 Layer* layer = args->context->createRenderLayer(args->width, args->height); 203 if (!layer) return 0; 204 return new DeferredLayerUpdater(layer); 205} 206 207DeferredLayerUpdater* RenderProxy::createDisplayListLayer(int width, int height) { 208 SETUP_TASK(createDisplayListLayer); 209 args->width = width; 210 args->height = height; 211 args->context = mContext; 212 void* retval = postAndWait(task); 213 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval); 214 mDrawFrameTask.addLayer(layer); 215 return layer; 216} 217 218CREATE_BRIDGE1(createTextureLayer, CanvasContext* context) { 219 Layer* layer = args->context->createTextureLayer(); 220 if (!layer) return 0; 221 return new DeferredLayerUpdater(layer); 222} 223 224DeferredLayerUpdater* RenderProxy::createTextureLayer() { 225 SETUP_TASK(createTextureLayer); 226 args->context = mContext; 227 void* retval = postAndWait(task); 228 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval); 229 mDrawFrameTask.addLayer(layer); 230 return layer; 231} 232 233CREATE_BRIDGE1(destroyLayer, Layer* layer) { 234 LayerRenderer::destroyLayer(args->layer); 235 return NULL; 236} 237 238CREATE_BRIDGE3(copyLayerInto, CanvasContext* context, DeferredLayerUpdater* layer, 239 SkBitmap* bitmap) { 240 bool success = args->context->copyLayerInto(args->layer, args->bitmap); 241 return (void*) success; 242} 243 244bool RenderProxy::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) { 245 SETUP_TASK(copyLayerInto); 246 args->context = mContext; 247 args->layer = layer; 248 args->bitmap = bitmap; 249 return (bool) postAndWait(task); 250} 251 252void RenderProxy::destroyLayer(DeferredLayerUpdater* layer) { 253 mDrawFrameTask.removeLayer(layer); 254 SETUP_TASK(destroyLayer); 255 args->layer = layer->detachBackingLayer(); 256 post(task); 257} 258 259CREATE_BRIDGE0(fence) { 260 // Intentionally empty 261 return NULL; 262} 263 264void RenderProxy::fence() { 265 SETUP_TASK(fence); 266 postAndWait(task); 267} 268 269void RenderProxy::post(RenderTask* task) { 270 mRenderThread.queue(task); 271} 272 273void* RenderProxy::postAndWait(MethodInvokeRenderTask* task) { 274 void* retval; 275 task->setReturnPtr(&retval); 276 SignalingRenderTask syncTask(task, &mSyncMutex, &mSyncCondition); 277 AutoMutex _lock(mSyncMutex); 278 mRenderThread.queue(&syncTask); 279 mSyncCondition.wait(mSyncMutex); 280 return retval; 281} 282 283} /* namespace renderthread */ 284} /* namespace uirenderer */ 285} /* namespace android */ 286