John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 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 | |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 25 | #include "../DeferredLayerUpdater.h" |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 26 | #include "../DisplayList.h" |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 27 | #include "../LayerRenderer.h" |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 28 | #include "../Rect.h" |
| 29 | |
| 30 | namespace android { |
| 31 | namespace uirenderer { |
| 32 | namespace renderthread { |
| 33 | |
| 34 | #define ARGS(method) method ## Args |
| 35 | |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 36 | #define CREATE_BRIDGE0(name) CREATE_BRIDGE(name,,,,,,,,) |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 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 | |
| 54 | CREATE_BRIDGE1(createContext, bool translucent) { |
| 55 | return new CanvasContext(args->translucent); |
| 56 | } |
| 57 | |
| 58 | RenderProxy::RenderProxy(bool translucent) |
| 59 | : mRenderThread(RenderThread::getInstance()) |
| 60 | , mContext(0) { |
| 61 | SETUP_TASK(createContext); |
| 62 | args->translucent = translucent; |
| 63 | mContext = (CanvasContext*) postAndWait(task); |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 64 | mDrawFrameTask.setContext(mContext); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | RenderProxy::~RenderProxy() { |
| 68 | destroyContext(); |
| 69 | } |
| 70 | |
| 71 | CREATE_BRIDGE1(destroyContext, CanvasContext* context) { |
| 72 | delete args->context; |
| 73 | return NULL; |
| 74 | } |
| 75 | |
| 76 | void RenderProxy::destroyContext() { |
| 77 | if (mContext) { |
| 78 | SETUP_TASK(destroyContext); |
| 79 | args->context = mContext; |
| 80 | mContext = 0; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 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); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
| 88 | CREATE_BRIDGE2(initialize, CanvasContext* context, EGLNativeWindowType window) { |
| 89 | return (void*) args->context->initialize(args->window); |
| 90 | } |
| 91 | |
| 92 | bool RenderProxy::initialize(EGLNativeWindowType window) { |
| 93 | SETUP_TASK(initialize); |
| 94 | args->context = mContext; |
| 95 | args->window = window; |
| 96 | return (bool) postAndWait(task); |
| 97 | } |
| 98 | |
| 99 | CREATE_BRIDGE2(updateSurface, CanvasContext* context, EGLNativeWindowType window) { |
| 100 | args->context->updateSurface(args->window); |
| 101 | return NULL; |
| 102 | } |
| 103 | |
| 104 | void RenderProxy::updateSurface(EGLNativeWindowType window) { |
| 105 | SETUP_TASK(updateSurface); |
| 106 | args->context = mContext; |
| 107 | args->window = window; |
| 108 | post(task); |
| 109 | } |
| 110 | |
| 111 | CREATE_BRIDGE3(setup, CanvasContext* context, int width, int height) { |
| 112 | args->context->setup(args->width, args->height); |
| 113 | return NULL; |
| 114 | } |
| 115 | |
| 116 | void 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 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 124 | void RenderProxy::setDisplayListData(RenderNode* renderNode, DisplayListData* newData) { |
| 125 | mDrawFrameTask.setDisplayListData(renderNode, newData); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 126 | } |
| 127 | |
John Reck | e18264b | 2014-03-12 13:56:30 -0700 | [diff] [blame] | 128 | void RenderProxy::drawDisplayList(RenderNode* displayList, |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 129 | int dirtyLeft, int dirtyTop, int dirtyRight, int dirtyBottom) { |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 130 | mDrawFrameTask.setRenderNode(displayList); |
| 131 | mDrawFrameTask.setDirty(dirtyLeft, dirtyTop, dirtyRight, dirtyBottom); |
| 132 | mDrawFrameTask.drawFrame(&mRenderThread); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | CREATE_BRIDGE1(destroyCanvas, CanvasContext* context) { |
| 136 | args->context->destroyCanvas(); |
| 137 | return NULL; |
| 138 | } |
| 139 | |
| 140 | void RenderProxy::destroyCanvas() { |
| 141 | SETUP_TASK(destroyCanvas); |
| 142 | args->context = mContext; |
| 143 | post(task); |
| 144 | } |
| 145 | |
| 146 | CREATE_BRIDGE2(attachFunctor, CanvasContext* context, Functor* functor) { |
| 147 | args->context->attachFunctor(args->functor); |
| 148 | return NULL; |
| 149 | } |
| 150 | |
| 151 | void RenderProxy::attachFunctor(Functor* functor) { |
| 152 | SETUP_TASK(attachFunctor); |
| 153 | args->context = mContext; |
| 154 | args->functor = functor; |
| 155 | post(task); |
| 156 | } |
| 157 | |
| 158 | CREATE_BRIDGE2(detachFunctor, CanvasContext* context, Functor* functor) { |
| 159 | args->context->detachFunctor(args->functor); |
| 160 | return NULL; |
| 161 | } |
| 162 | |
| 163 | void RenderProxy::detachFunctor(Functor* functor) { |
| 164 | SETUP_TASK(detachFunctor); |
| 165 | args->context = mContext; |
| 166 | args->functor = functor; |
| 167 | post(task); |
| 168 | } |
| 169 | |
John Reck | fc53ef27 | 2014-02-11 10:40:25 -0800 | [diff] [blame] | 170 | CREATE_BRIDGE2(runWithGlContext, CanvasContext* context, RenderTask* task) { |
| 171 | args->context->runWithGlContext(args->task); |
| 172 | return NULL; |
| 173 | } |
| 174 | |
| 175 | void RenderProxy::runWithGlContext(RenderTask* gltask) { |
| 176 | SETUP_TASK(runWithGlContext); |
| 177 | args->context = mContext; |
| 178 | args->task = gltask; |
| 179 | postAndWait(task); |
| 180 | } |
| 181 | |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 182 | CREATE_BRIDGE2(createDisplayListLayer, int width, int height) { |
| 183 | Layer* layer = LayerRenderer::createRenderLayer(args->width, args->height); |
| 184 | if (!layer) return 0; |
| 185 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 186 | return new DeferredLayerUpdater(layer); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | DeferredLayerUpdater* RenderProxy::createDisplayListLayer(int width, int height) { |
| 190 | SETUP_TASK(createDisplayListLayer); |
| 191 | args->width = width; |
| 192 | args->height = height; |
| 193 | void* retval = postAndWait(task); |
| 194 | DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval); |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 195 | mDrawFrameTask.addLayer(layer); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 196 | return layer; |
| 197 | } |
| 198 | |
| 199 | CREATE_BRIDGE0(createTextureLayer) { |
| 200 | Layer* layer = LayerRenderer::createTextureLayer(); |
| 201 | if (!layer) return 0; |
| 202 | return new DeferredLayerUpdater(layer); |
| 203 | } |
| 204 | |
| 205 | DeferredLayerUpdater* RenderProxy::createTextureLayer() { |
| 206 | SETUP_TASK(createTextureLayer); |
| 207 | void* retval = postAndWait(task); |
| 208 | DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval); |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 209 | mDrawFrameTask.addLayer(layer); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 210 | return layer; |
| 211 | } |
| 212 | |
| 213 | CREATE_BRIDGE1(destroyLayer, Layer* layer) { |
| 214 | LayerRenderer::destroyLayer(args->layer); |
| 215 | return NULL; |
| 216 | } |
| 217 | |
| 218 | CREATE_BRIDGE3(copyLayerInto, CanvasContext* context, DeferredLayerUpdater* layer, |
| 219 | SkBitmap* bitmap) { |
| 220 | bool success = args->context->copyLayerInto(args->layer, args->bitmap); |
| 221 | return (void*) success; |
| 222 | } |
| 223 | |
| 224 | bool RenderProxy::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) { |
| 225 | SETUP_TASK(copyLayerInto); |
| 226 | args->context = mContext; |
| 227 | args->layer = layer; |
| 228 | args->bitmap = bitmap; |
| 229 | return (bool) postAndWait(task); |
| 230 | } |
| 231 | |
| 232 | void RenderProxy::destroyLayer(DeferredLayerUpdater* layer) { |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 233 | mDrawFrameTask.removeLayer(layer); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 234 | SETUP_TASK(destroyLayer); |
| 235 | args->layer = layer->detachBackingLayer(); |
| 236 | post(task); |
| 237 | } |
| 238 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 239 | MethodInvokeRenderTask* RenderProxy::createTask(RunnableMethod method) { |
| 240 | // TODO: Consider having a small pool of these to avoid alloc churn |
| 241 | return new MethodInvokeRenderTask(method); |
| 242 | } |
| 243 | |
| 244 | void RenderProxy::post(RenderTask* task) { |
| 245 | mRenderThread.queue(task); |
| 246 | } |
| 247 | |
| 248 | void* RenderProxy::postAndWait(MethodInvokeRenderTask* task) { |
| 249 | void* retval; |
| 250 | task->setReturnPtr(&retval); |
| 251 | SignalingRenderTask syncTask(task, &mSyncMutex, &mSyncCondition); |
| 252 | AutoMutex _lock(mSyncMutex); |
| 253 | mRenderThread.queue(&syncTask); |
| 254 | mSyncCondition.wait(mSyncMutex); |
| 255 | return retval; |
| 256 | } |
| 257 | |
| 258 | } /* namespace renderthread */ |
| 259 | } /* namespace uirenderer */ |
| 260 | } /* namespace android */ |