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))); \ |
John Reck | e2c4552 | 2014-04-07 17:31:44 -0700 | [diff] [blame] | 51 | MethodInvokeRenderTask* task = new MethodInvokeRenderTask((RunnableMethod) Bridge_ ## method); \ |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 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) { |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 78 | // Flush any pending changes to ensure all garbage is destroyed |
| 79 | mDrawFrameTask.flushStateChanges(&mRenderThread); |
| 80 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 81 | SETUP_TASK(destroyContext); |
| 82 | args->context = mContext; |
| 83 | mContext = 0; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 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); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
| 91 | CREATE_BRIDGE2(initialize, CanvasContext* context, EGLNativeWindowType window) { |
| 92 | return (void*) args->context->initialize(args->window); |
| 93 | } |
| 94 | |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 95 | bool RenderProxy::initialize(const sp<ANativeWindow>& window) { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 96 | SETUP_TASK(initialize); |
| 97 | args->context = mContext; |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 98 | args->window = window.get(); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 99 | return (bool) postAndWait(task); |
| 100 | } |
| 101 | |
| 102 | CREATE_BRIDGE2(updateSurface, CanvasContext* context, EGLNativeWindowType window) { |
| 103 | args->context->updateSurface(args->window); |
| 104 | return NULL; |
| 105 | } |
| 106 | |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 107 | void RenderProxy::updateSurface(const sp<ANativeWindow>& window) { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 108 | SETUP_TASK(updateSurface); |
| 109 | args->context = mContext; |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 110 | args->window = window.get(); |
| 111 | postAndWait(task); |
| 112 | } |
| 113 | |
| 114 | CREATE_BRIDGE2(pauseSurface, CanvasContext* context, EGLNativeWindowType window) { |
| 115 | args->context->pauseSurface(args->window); |
| 116 | return NULL; |
| 117 | } |
| 118 | |
| 119 | void RenderProxy::pauseSurface(const sp<ANativeWindow>& window) { |
| 120 | SETUP_TASK(pauseSurface); |
| 121 | args->context = mContext; |
| 122 | args->window = window.get(); |
| 123 | postAndWait(task); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | CREATE_BRIDGE3(setup, CanvasContext* context, int width, int height) { |
| 127 | args->context->setup(args->width, args->height); |
| 128 | return NULL; |
| 129 | } |
| 130 | |
| 131 | void RenderProxy::setup(int width, int height) { |
| 132 | SETUP_TASK(setup); |
| 133 | args->context = mContext; |
| 134 | args->width = width; |
| 135 | args->height = height; |
| 136 | post(task); |
| 137 | } |
| 138 | |
John Reck | e18264b | 2014-03-12 13:56:30 -0700 | [diff] [blame] | 139 | void RenderProxy::drawDisplayList(RenderNode* displayList, |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 140 | int dirtyLeft, int dirtyTop, int dirtyRight, int dirtyBottom) { |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 141 | mDrawFrameTask.setRenderNode(displayList); |
| 142 | mDrawFrameTask.setDirty(dirtyLeft, dirtyTop, dirtyRight, dirtyBottom); |
| 143 | mDrawFrameTask.drawFrame(&mRenderThread); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | CREATE_BRIDGE1(destroyCanvas, CanvasContext* context) { |
| 147 | args->context->destroyCanvas(); |
| 148 | return NULL; |
| 149 | } |
| 150 | |
| 151 | void RenderProxy::destroyCanvas() { |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 152 | // If the canvas is being destroyed we won't be drawing again anytime soon |
| 153 | // So flush any pending state changes to allow for resource cleanup. |
| 154 | mDrawFrameTask.flushStateChanges(&mRenderThread); |
| 155 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 156 | SETUP_TASK(destroyCanvas); |
| 157 | args->context = mContext; |
| 158 | post(task); |
| 159 | } |
| 160 | |
| 161 | CREATE_BRIDGE2(attachFunctor, CanvasContext* context, Functor* functor) { |
| 162 | args->context->attachFunctor(args->functor); |
| 163 | return NULL; |
| 164 | } |
| 165 | |
| 166 | void RenderProxy::attachFunctor(Functor* functor) { |
| 167 | SETUP_TASK(attachFunctor); |
| 168 | args->context = mContext; |
| 169 | args->functor = functor; |
| 170 | post(task); |
| 171 | } |
| 172 | |
| 173 | CREATE_BRIDGE2(detachFunctor, CanvasContext* context, Functor* functor) { |
| 174 | args->context->detachFunctor(args->functor); |
| 175 | return NULL; |
| 176 | } |
| 177 | |
| 178 | void RenderProxy::detachFunctor(Functor* functor) { |
| 179 | SETUP_TASK(detachFunctor); |
| 180 | args->context = mContext; |
| 181 | args->functor = functor; |
| 182 | post(task); |
| 183 | } |
| 184 | |
John Reck | 0d1f634 | 2014-03-28 20:30:27 -0700 | [diff] [blame] | 185 | CREATE_BRIDGE2(invokeFunctor, CanvasContext* context, Functor* functor) { |
| 186 | args->context->invokeFunctor(args->functor); |
| 187 | return NULL; |
| 188 | } |
| 189 | |
| 190 | void RenderProxy::invokeFunctor(Functor* functor, bool waitForCompletion) { |
| 191 | SETUP_TASK(invokeFunctor); |
| 192 | args->context = mContext; |
| 193 | args->functor = functor; |
| 194 | if (waitForCompletion) { |
| 195 | postAndWait(task); |
| 196 | } else { |
| 197 | post(task); |
| 198 | } |
| 199 | } |
| 200 | |
John Reck | fc53ef27 | 2014-02-11 10:40:25 -0800 | [diff] [blame] | 201 | CREATE_BRIDGE2(runWithGlContext, CanvasContext* context, RenderTask* task) { |
| 202 | args->context->runWithGlContext(args->task); |
| 203 | return NULL; |
| 204 | } |
| 205 | |
| 206 | void RenderProxy::runWithGlContext(RenderTask* gltask) { |
| 207 | SETUP_TASK(runWithGlContext); |
| 208 | args->context = mContext; |
| 209 | args->task = gltask; |
| 210 | postAndWait(task); |
| 211 | } |
| 212 | |
John Reck | 1949e79 | 2014-04-08 15:18:56 -0700 | [diff] [blame] | 213 | CREATE_BRIDGE3(createDisplayListLayer, CanvasContext* context, int width, int height) { |
| 214 | Layer* layer = args->context->createRenderLayer(args->width, args->height); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 215 | if (!layer) return 0; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 216 | return new DeferredLayerUpdater(layer); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | DeferredLayerUpdater* RenderProxy::createDisplayListLayer(int width, int height) { |
| 220 | SETUP_TASK(createDisplayListLayer); |
| 221 | args->width = width; |
| 222 | args->height = height; |
John Reck | 1949e79 | 2014-04-08 15:18:56 -0700 | [diff] [blame] | 223 | args->context = mContext; |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 224 | void* retval = postAndWait(task); |
| 225 | DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval); |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 226 | mDrawFrameTask.addLayer(layer); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 227 | return layer; |
| 228 | } |
| 229 | |
John Reck | 1949e79 | 2014-04-08 15:18:56 -0700 | [diff] [blame] | 230 | CREATE_BRIDGE1(createTextureLayer, CanvasContext* context) { |
| 231 | Layer* layer = args->context->createTextureLayer(); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 232 | if (!layer) return 0; |
| 233 | return new DeferredLayerUpdater(layer); |
| 234 | } |
| 235 | |
| 236 | DeferredLayerUpdater* RenderProxy::createTextureLayer() { |
| 237 | SETUP_TASK(createTextureLayer); |
John Reck | 1949e79 | 2014-04-08 15:18:56 -0700 | [diff] [blame] | 238 | args->context = mContext; |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 239 | void* retval = postAndWait(task); |
| 240 | DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval); |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 241 | mDrawFrameTask.addLayer(layer); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 242 | return layer; |
| 243 | } |
| 244 | |
| 245 | CREATE_BRIDGE1(destroyLayer, Layer* layer) { |
| 246 | LayerRenderer::destroyLayer(args->layer); |
| 247 | return NULL; |
| 248 | } |
| 249 | |
| 250 | CREATE_BRIDGE3(copyLayerInto, CanvasContext* context, DeferredLayerUpdater* layer, |
| 251 | SkBitmap* bitmap) { |
| 252 | bool success = args->context->copyLayerInto(args->layer, args->bitmap); |
| 253 | return (void*) success; |
| 254 | } |
| 255 | |
| 256 | bool RenderProxy::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) { |
| 257 | SETUP_TASK(copyLayerInto); |
| 258 | args->context = mContext; |
| 259 | args->layer = layer; |
| 260 | args->bitmap = bitmap; |
| 261 | return (bool) postAndWait(task); |
| 262 | } |
| 263 | |
| 264 | void RenderProxy::destroyLayer(DeferredLayerUpdater* layer) { |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 265 | mDrawFrameTask.removeLayer(layer); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 266 | SETUP_TASK(destroyLayer); |
| 267 | args->layer = layer->detachBackingLayer(); |
| 268 | post(task); |
| 269 | } |
| 270 | |
John Reck | 28ad7b5 | 2014-04-07 16:59:25 -0700 | [diff] [blame] | 271 | CREATE_BRIDGE0(fence) { |
| 272 | // Intentionally empty |
| 273 | return NULL; |
| 274 | } |
| 275 | |
| 276 | void RenderProxy::fence() { |
| 277 | SETUP_TASK(fence); |
| 278 | postAndWait(task); |
| 279 | } |
| 280 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 281 | void RenderProxy::post(RenderTask* task) { |
| 282 | mRenderThread.queue(task); |
| 283 | } |
| 284 | |
| 285 | void* RenderProxy::postAndWait(MethodInvokeRenderTask* task) { |
| 286 | void* retval; |
| 287 | task->setReturnPtr(&retval); |
| 288 | SignalingRenderTask syncTask(task, &mSyncMutex, &mSyncCondition); |
| 289 | AutoMutex _lock(mSyncMutex); |
| 290 | mRenderThread.queue(&syncTask); |
| 291 | mSyncCondition.wait(mSyncMutex); |
| 292 | return retval; |
| 293 | } |
| 294 | |
| 295 | } /* namespace renderthread */ |
| 296 | } /* namespace uirenderer */ |
| 297 | } /* namespace android */ |