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); |
| 64 | } |
| 65 | |
| 66 | RenderProxy::~RenderProxy() { |
| 67 | destroyContext(); |
| 68 | } |
| 69 | |
| 70 | CREATE_BRIDGE1(destroyContext, CanvasContext* context) { |
| 71 | delete args->context; |
| 72 | return NULL; |
| 73 | } |
| 74 | |
| 75 | void RenderProxy::destroyContext() { |
| 76 | if (mContext) { |
| 77 | SETUP_TASK(destroyContext); |
| 78 | args->context = mContext; |
| 79 | mContext = 0; |
| 80 | post(task); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | CREATE_BRIDGE2(initialize, CanvasContext* context, EGLNativeWindowType window) { |
| 85 | return (void*) args->context->initialize(args->window); |
| 86 | } |
| 87 | |
| 88 | bool RenderProxy::initialize(EGLNativeWindowType window) { |
| 89 | SETUP_TASK(initialize); |
| 90 | args->context = mContext; |
| 91 | args->window = window; |
| 92 | return (bool) postAndWait(task); |
| 93 | } |
| 94 | |
| 95 | CREATE_BRIDGE2(updateSurface, CanvasContext* context, EGLNativeWindowType window) { |
| 96 | args->context->updateSurface(args->window); |
| 97 | return NULL; |
| 98 | } |
| 99 | |
| 100 | void RenderProxy::updateSurface(EGLNativeWindowType window) { |
| 101 | SETUP_TASK(updateSurface); |
| 102 | args->context = mContext; |
| 103 | args->window = window; |
| 104 | post(task); |
| 105 | } |
| 106 | |
| 107 | CREATE_BRIDGE3(setup, CanvasContext* context, int width, int height) { |
| 108 | args->context->setup(args->width, args->height); |
| 109 | return NULL; |
| 110 | } |
| 111 | |
| 112 | void 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 | |
John Reck | be34f2f | 2014-03-10 08:58:44 -0700 | [diff] [blame] | 120 | CREATE_BRIDGE3(setDisplayListData, CanvasContext* context, DisplayList* displayList, |
John Reck | 44fd8d2 | 2014-02-26 11:00:11 -0800 | [diff] [blame] | 121 | DisplayListData* newData) { |
John Reck | be34f2f | 2014-03-10 08:58:44 -0700 | [diff] [blame] | 122 | args->context->setDisplayListData(args->displayList, args->newData); |
John Reck | 44fd8d2 | 2014-02-26 11:00:11 -0800 | [diff] [blame] | 123 | return NULL; |
| 124 | } |
| 125 | |
John Reck | be34f2f | 2014-03-10 08:58:44 -0700 | [diff] [blame] | 126 | void RenderProxy::setDisplayListData(DisplayList* displayList, DisplayListData* newData) { |
| 127 | SETUP_TASK(setDisplayListData); |
John Reck | 44fd8d2 | 2014-02-26 11:00:11 -0800 | [diff] [blame] | 128 | args->context = mContext; |
| 129 | args->displayList = displayList; |
| 130 | args->newData = newData; |
| 131 | post(task); |
| 132 | } |
| 133 | |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 134 | CREATE_BRIDGE4(drawDisplayList, CanvasContext* context, DisplayList* displayList, |
| 135 | Rect dirty, const Vector<DeferredLayerUpdater*>* layerUpdates) { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 136 | Rect* dirty = &args->dirty; |
| 137 | if (dirty->bottom == -1 && dirty->left == -1 && |
| 138 | dirty->top == -1 && dirty->right == -1) { |
| 139 | dirty = 0; |
| 140 | } |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 141 | args->context->processLayerUpdates(args->layerUpdates); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 142 | args->context->drawDisplayList(args->displayList, dirty); |
| 143 | return NULL; |
| 144 | } |
| 145 | |
| 146 | void RenderProxy::drawDisplayList(DisplayList* displayList, |
| 147 | int dirtyLeft, int dirtyTop, int dirtyRight, int dirtyBottom) { |
| 148 | SETUP_TASK(drawDisplayList); |
| 149 | args->context = mContext; |
| 150 | args->displayList = displayList; |
| 151 | args->dirty.set(dirtyLeft, dirtyTop, dirtyRight, dirtyBottom); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 152 | args->layerUpdates = &mLayers; |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 153 | // TODO: Switch to post() once some form of thread safety strategy is in place |
| 154 | postAndWait(task); |
| 155 | } |
| 156 | |
| 157 | CREATE_BRIDGE1(destroyCanvas, CanvasContext* context) { |
| 158 | args->context->destroyCanvas(); |
| 159 | return NULL; |
| 160 | } |
| 161 | |
| 162 | void RenderProxy::destroyCanvas() { |
| 163 | SETUP_TASK(destroyCanvas); |
| 164 | args->context = mContext; |
| 165 | post(task); |
| 166 | } |
| 167 | |
| 168 | CREATE_BRIDGE2(attachFunctor, CanvasContext* context, Functor* functor) { |
| 169 | args->context->attachFunctor(args->functor); |
| 170 | return NULL; |
| 171 | } |
| 172 | |
| 173 | void RenderProxy::attachFunctor(Functor* functor) { |
| 174 | SETUP_TASK(attachFunctor); |
| 175 | args->context = mContext; |
| 176 | args->functor = functor; |
| 177 | post(task); |
| 178 | } |
| 179 | |
| 180 | CREATE_BRIDGE2(detachFunctor, CanvasContext* context, Functor* functor) { |
| 181 | args->context->detachFunctor(args->functor); |
| 182 | return NULL; |
| 183 | } |
| 184 | |
| 185 | void RenderProxy::detachFunctor(Functor* functor) { |
| 186 | SETUP_TASK(detachFunctor); |
| 187 | args->context = mContext; |
| 188 | args->functor = functor; |
| 189 | post(task); |
| 190 | } |
| 191 | |
John Reck | fc53ef27 | 2014-02-11 10:40:25 -0800 | [diff] [blame] | 192 | CREATE_BRIDGE2(runWithGlContext, CanvasContext* context, RenderTask* task) { |
| 193 | args->context->runWithGlContext(args->task); |
| 194 | return NULL; |
| 195 | } |
| 196 | |
| 197 | void RenderProxy::runWithGlContext(RenderTask* gltask) { |
| 198 | SETUP_TASK(runWithGlContext); |
| 199 | args->context = mContext; |
| 200 | args->task = gltask; |
| 201 | postAndWait(task); |
| 202 | } |
| 203 | |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 204 | CREATE_BRIDGE2(createDisplayListLayer, int width, int height) { |
| 205 | Layer* layer = LayerRenderer::createRenderLayer(args->width, args->height); |
| 206 | if (!layer) return 0; |
| 207 | |
| 208 | OpenGLRenderer* renderer = new LayerRenderer(layer); |
| 209 | renderer->initProperties(); |
| 210 | return new DeferredLayerUpdater(layer, renderer); |
| 211 | } |
| 212 | |
| 213 | DeferredLayerUpdater* RenderProxy::createDisplayListLayer(int width, int height) { |
| 214 | SETUP_TASK(createDisplayListLayer); |
| 215 | args->width = width; |
| 216 | args->height = height; |
| 217 | void* retval = postAndWait(task); |
| 218 | DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval); |
| 219 | mLayers.push(layer); |
| 220 | return layer; |
| 221 | } |
| 222 | |
| 223 | CREATE_BRIDGE0(createTextureLayer) { |
| 224 | Layer* layer = LayerRenderer::createTextureLayer(); |
| 225 | if (!layer) return 0; |
| 226 | return new DeferredLayerUpdater(layer); |
| 227 | } |
| 228 | |
| 229 | DeferredLayerUpdater* RenderProxy::createTextureLayer() { |
| 230 | SETUP_TASK(createTextureLayer); |
| 231 | void* retval = postAndWait(task); |
| 232 | DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval); |
| 233 | mLayers.push(layer); |
| 234 | return layer; |
| 235 | } |
| 236 | |
| 237 | CREATE_BRIDGE1(destroyLayer, Layer* layer) { |
| 238 | LayerRenderer::destroyLayer(args->layer); |
| 239 | return NULL; |
| 240 | } |
| 241 | |
| 242 | CREATE_BRIDGE3(copyLayerInto, CanvasContext* context, DeferredLayerUpdater* layer, |
| 243 | SkBitmap* bitmap) { |
| 244 | bool success = args->context->copyLayerInto(args->layer, args->bitmap); |
| 245 | return (void*) success; |
| 246 | } |
| 247 | |
| 248 | bool RenderProxy::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) { |
| 249 | SETUP_TASK(copyLayerInto); |
| 250 | args->context = mContext; |
| 251 | args->layer = layer; |
| 252 | args->bitmap = bitmap; |
| 253 | return (bool) postAndWait(task); |
| 254 | } |
| 255 | |
| 256 | void RenderProxy::destroyLayer(DeferredLayerUpdater* layer) { |
| 257 | for (size_t i = 0; i < mLayers.size(); i++) { |
| 258 | if (mLayers[i] == layer) { |
| 259 | mLayers.removeAt(i); |
| 260 | break; |
| 261 | } |
| 262 | } |
| 263 | SETUP_TASK(destroyLayer); |
| 264 | args->layer = layer->detachBackingLayer(); |
| 265 | post(task); |
| 266 | } |
| 267 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 268 | MethodInvokeRenderTask* RenderProxy::createTask(RunnableMethod method) { |
| 269 | // TODO: Consider having a small pool of these to avoid alloc churn |
| 270 | return new MethodInvokeRenderTask(method); |
| 271 | } |
| 272 | |
| 273 | void RenderProxy::post(RenderTask* task) { |
| 274 | mRenderThread.queue(task); |
| 275 | } |
| 276 | |
| 277 | void* RenderProxy::postAndWait(MethodInvokeRenderTask* task) { |
| 278 | void* retval; |
| 279 | task->setReturnPtr(&retval); |
| 280 | SignalingRenderTask syncTask(task, &mSyncMutex, &mSyncCondition); |
| 281 | AutoMutex _lock(mSyncMutex); |
| 282 | mRenderThread.queue(&syncTask); |
| 283 | mSyncCondition.wait(mSyncMutex); |
| 284 | return retval; |
| 285 | } |
| 286 | |
| 287 | } /* namespace renderthread */ |
| 288 | } /* namespace uirenderer */ |
| 289 | } /* namespace android */ |