RenderProxy.cpp revision 28ad7b52e038ef0cdd89f753d9839444a434b299
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 = createTask((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 SETUP_TASK(destroyContext); 79 args->context = mContext; 80 mContext = 0; 81 mDrawFrameTask.setContext(0); 82 // This is also a fence as we need to be certain that there are no 83 // outstanding mDrawFrame tasks posted before it is destroyed 84 postAndWait(task); 85 } 86} 87 88CREATE_BRIDGE2(initialize, CanvasContext* context, EGLNativeWindowType window) { 89 return (void*) args->context->initialize(args->window); 90} 91 92bool RenderProxy::initialize(EGLNativeWindowType window) { 93 SETUP_TASK(initialize); 94 args->context = mContext; 95 args->window = window; 96 return (bool) postAndWait(task); 97} 98 99CREATE_BRIDGE2(updateSurface, CanvasContext* context, EGLNativeWindowType window) { 100 args->context->updateSurface(args->window); 101 return NULL; 102} 103 104void RenderProxy::updateSurface(EGLNativeWindowType window) { 105 SETUP_TASK(updateSurface); 106 args->context = mContext; 107 args->window = window; 108 post(task); 109} 110 111CREATE_BRIDGE3(setup, CanvasContext* context, int width, int height) { 112 args->context->setup(args->width, args->height); 113 return NULL; 114} 115 116void RenderProxy::setup(int width, int height) { 117 SETUP_TASK(setup); 118 args->context = mContext; 119 args->width = width; 120 args->height = height; 121 post(task); 122} 123 124void RenderProxy::setDisplayListData(RenderNode* renderNode, DisplayListData* newData) { 125 mDrawFrameTask.setDisplayListData(renderNode, newData); 126} 127 128void RenderProxy::drawDisplayList(RenderNode* displayList, 129 int dirtyLeft, int dirtyTop, int dirtyRight, int dirtyBottom) { 130 mDrawFrameTask.setRenderNode(displayList); 131 mDrawFrameTask.setDirty(dirtyLeft, dirtyTop, dirtyRight, dirtyBottom); 132 mDrawFrameTask.drawFrame(&mRenderThread); 133} 134 135CREATE_BRIDGE1(destroyCanvas, CanvasContext* context) { 136 args->context->destroyCanvas(); 137 return NULL; 138} 139 140void RenderProxy::destroyCanvas() { 141 SETUP_TASK(destroyCanvas); 142 args->context = mContext; 143 post(task); 144} 145 146CREATE_BRIDGE2(attachFunctor, CanvasContext* context, Functor* functor) { 147 args->context->attachFunctor(args->functor); 148 return NULL; 149} 150 151void RenderProxy::attachFunctor(Functor* functor) { 152 SETUP_TASK(attachFunctor); 153 args->context = mContext; 154 args->functor = functor; 155 post(task); 156} 157 158CREATE_BRIDGE2(detachFunctor, CanvasContext* context, Functor* functor) { 159 args->context->detachFunctor(args->functor); 160 return NULL; 161} 162 163void RenderProxy::detachFunctor(Functor* functor) { 164 SETUP_TASK(detachFunctor); 165 args->context = mContext; 166 args->functor = functor; 167 post(task); 168} 169 170CREATE_BRIDGE2(invokeFunctor, CanvasContext* context, Functor* functor) { 171 args->context->invokeFunctor(args->functor); 172 return NULL; 173} 174 175void RenderProxy::invokeFunctor(Functor* functor, bool waitForCompletion) { 176 SETUP_TASK(invokeFunctor); 177 args->context = mContext; 178 args->functor = functor; 179 if (waitForCompletion) { 180 postAndWait(task); 181 } else { 182 post(task); 183 } 184} 185 186CREATE_BRIDGE2(runWithGlContext, CanvasContext* context, RenderTask* task) { 187 args->context->runWithGlContext(args->task); 188 return NULL; 189} 190 191void RenderProxy::runWithGlContext(RenderTask* gltask) { 192 SETUP_TASK(runWithGlContext); 193 args->context = mContext; 194 args->task = gltask; 195 postAndWait(task); 196} 197 198CREATE_BRIDGE2(createDisplayListLayer, int width, int height) { 199 Layer* layer = LayerRenderer::createRenderLayer(args->width, args->height); 200 if (!layer) return 0; 201 202 return new DeferredLayerUpdater(layer); 203} 204 205DeferredLayerUpdater* RenderProxy::createDisplayListLayer(int width, int height) { 206 SETUP_TASK(createDisplayListLayer); 207 args->width = width; 208 args->height = height; 209 void* retval = postAndWait(task); 210 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval); 211 mDrawFrameTask.addLayer(layer); 212 return layer; 213} 214 215CREATE_BRIDGE0(createTextureLayer) { 216 Layer* layer = LayerRenderer::createTextureLayer(); 217 if (!layer) return 0; 218 return new DeferredLayerUpdater(layer); 219} 220 221DeferredLayerUpdater* RenderProxy::createTextureLayer() { 222 SETUP_TASK(createTextureLayer); 223 void* retval = postAndWait(task); 224 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval); 225 mDrawFrameTask.addLayer(layer); 226 return layer; 227} 228 229CREATE_BRIDGE1(destroyLayer, Layer* layer) { 230 LayerRenderer::destroyLayer(args->layer); 231 return NULL; 232} 233 234CREATE_BRIDGE3(copyLayerInto, CanvasContext* context, DeferredLayerUpdater* layer, 235 SkBitmap* bitmap) { 236 bool success = args->context->copyLayerInto(args->layer, args->bitmap); 237 return (void*) success; 238} 239 240bool RenderProxy::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) { 241 SETUP_TASK(copyLayerInto); 242 args->context = mContext; 243 args->layer = layer; 244 args->bitmap = bitmap; 245 return (bool) postAndWait(task); 246} 247 248void RenderProxy::destroyLayer(DeferredLayerUpdater* layer) { 249 mDrawFrameTask.removeLayer(layer); 250 SETUP_TASK(destroyLayer); 251 args->layer = layer->detachBackingLayer(); 252 post(task); 253} 254 255CREATE_BRIDGE0(fence) { 256 // Intentionally empty 257 return NULL; 258} 259 260void RenderProxy::fence() { 261 SETUP_TASK(fence); 262 postAndWait(task); 263} 264 265MethodInvokeRenderTask* RenderProxy::createTask(RunnableMethod method) { 266 // TODO: Consider having a small pool of these to avoid alloc churn 267 return new MethodInvokeRenderTask(method); 268} 269 270void RenderProxy::post(RenderTask* task) { 271 mRenderThread.queue(task); 272} 273 274void* RenderProxy::postAndWait(MethodInvokeRenderTask* task) { 275 void* retval; 276 task->setReturnPtr(&retval); 277 SignalingRenderTask syncTask(task, &mSyncMutex, &mSyncCondition); 278 AutoMutex _lock(mSyncMutex); 279 mRenderThread.queue(&syncTask); 280 mSyncCondition.wait(mSyncMutex); 281 return retval; 282} 283 284} /* namespace renderthread */ 285} /* namespace uirenderer */ 286} /* namespace android */ 287