RenderProxy.cpp revision 19b6bcfd83eb7fb92ebd06d2fec89e308311f1d0
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} 65 66RenderProxy::~RenderProxy() { 67 destroyContext(); 68} 69 70CREATE_BRIDGE1(destroyContext, CanvasContext* context) { 71 delete args->context; 72 return NULL; 73} 74 75void RenderProxy::destroyContext() { 76 if (mContext) { 77 SETUP_TASK(destroyContext); 78 args->context = mContext; 79 mContext = 0; 80 post(task); 81 } 82} 83 84CREATE_BRIDGE2(initialize, CanvasContext* context, EGLNativeWindowType window) { 85 return (void*) args->context->initialize(args->window); 86} 87 88bool RenderProxy::initialize(EGLNativeWindowType window) { 89 SETUP_TASK(initialize); 90 args->context = mContext; 91 args->window = window; 92 return (bool) postAndWait(task); 93} 94 95CREATE_BRIDGE2(updateSurface, CanvasContext* context, EGLNativeWindowType window) { 96 args->context->updateSurface(args->window); 97 return NULL; 98} 99 100void RenderProxy::updateSurface(EGLNativeWindowType window) { 101 SETUP_TASK(updateSurface); 102 args->context = mContext; 103 args->window = window; 104 post(task); 105} 106 107CREATE_BRIDGE3(setup, CanvasContext* context, int width, int height) { 108 args->context->setup(args->width, args->height); 109 return NULL; 110} 111 112void RenderProxy::setup(int width, int height) { 113 SETUP_TASK(setup); 114 args->context = mContext; 115 args->width = width; 116 args->height = height; 117 post(task); 118} 119 120CREATE_BRIDGE4(drawDisplayList, CanvasContext* context, DisplayList* displayList, 121 Rect dirty, const Vector<DeferredLayerUpdater*>* layerUpdates) { 122 Rect* dirty = &args->dirty; 123 if (dirty->bottom == -1 && dirty->left == -1 && 124 dirty->top == -1 && dirty->right == -1) { 125 dirty = 0; 126 } 127 args->context->processLayerUpdates(args->layerUpdates); 128 args->context->drawDisplayList(args->displayList, dirty); 129 return NULL; 130} 131 132void RenderProxy::drawDisplayList(DisplayList* displayList, 133 int dirtyLeft, int dirtyTop, int dirtyRight, int dirtyBottom) { 134 SETUP_TASK(drawDisplayList); 135 args->context = mContext; 136 args->displayList = displayList; 137 args->dirty.set(dirtyLeft, dirtyTop, dirtyRight, dirtyBottom); 138 args->layerUpdates = &mLayers; 139 // TODO: Switch to post() once some form of thread safety strategy is in place 140 postAndWait(task); 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(runWithGlContext, CanvasContext* context, RenderTask* task) { 179 args->context->runWithGlContext(args->task); 180 return NULL; 181} 182 183void RenderProxy::runWithGlContext(RenderTask* gltask) { 184 SETUP_TASK(runWithGlContext); 185 args->context = mContext; 186 args->task = gltask; 187 postAndWait(task); 188} 189 190CREATE_BRIDGE2(createDisplayListLayer, int width, int height) { 191 Layer* layer = LayerRenderer::createRenderLayer(args->width, args->height); 192 if (!layer) return 0; 193 194 OpenGLRenderer* renderer = new LayerRenderer(layer); 195 renderer->initProperties(); 196 return new DeferredLayerUpdater(layer, renderer); 197} 198 199DeferredLayerUpdater* RenderProxy::createDisplayListLayer(int width, int height) { 200 SETUP_TASK(createDisplayListLayer); 201 args->width = width; 202 args->height = height; 203 void* retval = postAndWait(task); 204 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval); 205 mLayers.push(layer); 206 return layer; 207} 208 209CREATE_BRIDGE0(createTextureLayer) { 210 Layer* layer = LayerRenderer::createTextureLayer(); 211 if (!layer) return 0; 212 return new DeferredLayerUpdater(layer); 213} 214 215DeferredLayerUpdater* RenderProxy::createTextureLayer() { 216 SETUP_TASK(createTextureLayer); 217 void* retval = postAndWait(task); 218 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval); 219 mLayers.push(layer); 220 return layer; 221} 222 223CREATE_BRIDGE1(destroyLayer, Layer* layer) { 224 LayerRenderer::destroyLayer(args->layer); 225 return NULL; 226} 227 228CREATE_BRIDGE3(copyLayerInto, CanvasContext* context, DeferredLayerUpdater* layer, 229 SkBitmap* bitmap) { 230 bool success = args->context->copyLayerInto(args->layer, args->bitmap); 231 return (void*) success; 232} 233 234bool RenderProxy::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) { 235 SETUP_TASK(copyLayerInto); 236 args->context = mContext; 237 args->layer = layer; 238 args->bitmap = bitmap; 239 return (bool) postAndWait(task); 240} 241 242void RenderProxy::destroyLayer(DeferredLayerUpdater* layer) { 243 for (size_t i = 0; i < mLayers.size(); i++) { 244 if (mLayers[i] == layer) { 245 mLayers.removeAt(i); 246 break; 247 } 248 } 249 SETUP_TASK(destroyLayer); 250 args->layer = layer->detachBackingLayer(); 251 post(task); 252} 253 254MethodInvokeRenderTask* RenderProxy::createTask(RunnableMethod method) { 255 // TODO: Consider having a small pool of these to avoid alloc churn 256 return new MethodInvokeRenderTask(method); 257} 258 259void RenderProxy::post(RenderTask* task) { 260 mRenderThread.queue(task); 261} 262 263void* RenderProxy::postAndWait(MethodInvokeRenderTask* task) { 264 void* retval; 265 task->setReturnPtr(&retval); 266 SignalingRenderTask syncTask(task, &mSyncMutex, &mSyncCondition); 267 AutoMutex _lock(mSyncMutex); 268 mRenderThread.queue(&syncTask); 269 mSyncCondition.wait(mSyncMutex); 270 return retval; 271} 272 273} /* namespace renderthread */ 274} /* namespace uirenderer */ 275} /* namespace android */ 276