RenderProxy.cpp revision 8ca3eecc2b7fe507d3482745efc4cd2567ad15a1
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 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(const sp<ANativeWindow>& window) { 93 SETUP_TASK(initialize); 94 args->context = mContext; 95 args->window = window.get(); 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(const sp<ANativeWindow>& window) { 105 SETUP_TASK(updateSurface); 106 args->context = mContext; 107 args->window = window.get(); 108 postAndWait(task); 109} 110 111CREATE_BRIDGE2(pauseSurface, CanvasContext* context, EGLNativeWindowType window) { 112 args->context->pauseSurface(args->window); 113 return NULL; 114} 115 116void RenderProxy::pauseSurface(const sp<ANativeWindow>& window) { 117 SETUP_TASK(pauseSurface); 118 args->context = mContext; 119 args->window = window.get(); 120 postAndWait(task); 121} 122 123CREATE_BRIDGE3(setup, CanvasContext* context, int width, int height) { 124 args->context->setup(args->width, args->height); 125 return NULL; 126} 127 128void RenderProxy::setup(int width, int height) { 129 SETUP_TASK(setup); 130 args->context = mContext; 131 args->width = width; 132 args->height = height; 133 post(task); 134} 135 136void RenderProxy::drawDisplayList(RenderNode* displayList, 137 int dirtyLeft, int dirtyTop, int dirtyRight, int dirtyBottom) { 138 mDrawFrameTask.setRenderNode(displayList); 139 mDrawFrameTask.setDirty(dirtyLeft, dirtyTop, dirtyRight, dirtyBottom); 140 mDrawFrameTask.drawFrame(&mRenderThread); 141} 142 143CREATE_BRIDGE1(destroyCanvas, CanvasContext* context) { 144 args->context->destroyCanvas(); 145 return NULL; 146} 147 148void RenderProxy::destroyCanvas() { 149 SETUP_TASK(destroyCanvas); 150 args->context = mContext; 151 post(task); 152} 153 154CREATE_BRIDGE2(attachFunctor, CanvasContext* context, Functor* functor) { 155 args->context->attachFunctor(args->functor); 156 return NULL; 157} 158 159void RenderProxy::attachFunctor(Functor* functor) { 160 SETUP_TASK(attachFunctor); 161 args->context = mContext; 162 args->functor = functor; 163 post(task); 164} 165 166CREATE_BRIDGE2(detachFunctor, CanvasContext* context, Functor* functor) { 167 args->context->detachFunctor(args->functor); 168 return NULL; 169} 170 171void RenderProxy::detachFunctor(Functor* functor) { 172 SETUP_TASK(detachFunctor); 173 args->context = mContext; 174 args->functor = functor; 175 post(task); 176} 177 178CREATE_BRIDGE2(invokeFunctor, CanvasContext* context, Functor* functor) { 179 args->context->invokeFunctor(args->functor); 180 return NULL; 181} 182 183void RenderProxy::invokeFunctor(Functor* functor, bool waitForCompletion) { 184 SETUP_TASK(invokeFunctor); 185 args->context = mContext; 186 args->functor = functor; 187 if (waitForCompletion) { 188 postAndWait(task); 189 } else { 190 post(task); 191 } 192} 193 194CREATE_BRIDGE2(runWithGlContext, CanvasContext* context, RenderTask* task) { 195 args->context->runWithGlContext(args->task); 196 return NULL; 197} 198 199void RenderProxy::runWithGlContext(RenderTask* gltask) { 200 SETUP_TASK(runWithGlContext); 201 args->context = mContext; 202 args->task = gltask; 203 postAndWait(task); 204} 205 206CREATE_BRIDGE3(createDisplayListLayer, CanvasContext* context, int width, int height) { 207 Layer* layer = args->context->createRenderLayer(args->width, args->height); 208 if (!layer) return 0; 209 return new DeferredLayerUpdater(layer); 210} 211 212DeferredLayerUpdater* RenderProxy::createDisplayListLayer(int width, int height) { 213 SETUP_TASK(createDisplayListLayer); 214 args->width = width; 215 args->height = height; 216 args->context = mContext; 217 void* retval = postAndWait(task); 218 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval); 219 mDrawFrameTask.addLayer(layer); 220 return layer; 221} 222 223CREATE_BRIDGE1(createTextureLayer, CanvasContext* context) { 224 Layer* layer = args->context->createTextureLayer(); 225 if (!layer) return 0; 226 return new DeferredLayerUpdater(layer); 227} 228 229DeferredLayerUpdater* RenderProxy::createTextureLayer() { 230 SETUP_TASK(createTextureLayer); 231 args->context = mContext; 232 void* retval = postAndWait(task); 233 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval); 234 mDrawFrameTask.addLayer(layer); 235 return layer; 236} 237 238CREATE_BRIDGE1(destroyLayer, Layer* layer) { 239 LayerRenderer::destroyLayer(args->layer); 240 return NULL; 241} 242 243CREATE_BRIDGE3(copyLayerInto, CanvasContext* context, DeferredLayerUpdater* layer, 244 SkBitmap* bitmap) { 245 bool success = args->context->copyLayerInto(args->layer, args->bitmap); 246 return (void*) success; 247} 248 249bool RenderProxy::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) { 250 SETUP_TASK(copyLayerInto); 251 args->context = mContext; 252 args->layer = layer; 253 args->bitmap = bitmap; 254 return (bool) postAndWait(task); 255} 256 257void RenderProxy::destroyLayer(DeferredLayerUpdater* layer) { 258 mDrawFrameTask.removeLayer(layer); 259 SETUP_TASK(destroyLayer); 260 args->layer = layer->detachBackingLayer(); 261 post(task); 262} 263 264CREATE_BRIDGE0(fence) { 265 // Intentionally empty 266 return NULL; 267} 268 269void RenderProxy::fence() { 270 SETUP_TASK(fence); 271 postAndWait(task); 272} 273 274void RenderProxy::post(RenderTask* task) { 275 mRenderThread.queue(task); 276} 277 278void* RenderProxy::postAndWait(MethodInvokeRenderTask* task) { 279 void* retval; 280 task->setReturnPtr(&retval); 281 SignalingRenderTask syncTask(task, &mSyncMutex, &mSyncCondition); 282 AutoMutex _lock(mSyncMutex); 283 mRenderThread.queue(&syncTask); 284 mSyncCondition.wait(mSyncMutex); 285 return retval; 286} 287 288} /* namespace renderthread */ 289} /* namespace uirenderer */ 290} /* namespace android */ 291