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 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 54 | CREATE_BRIDGE2(createContext, bool translucent, RenderNode* rootRenderNode) { |
| 55 | return new CanvasContext(args->translucent, args->rootRenderNode); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 56 | } |
| 57 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 58 | RenderProxy::RenderProxy(bool translucent, RenderNode* rootRenderNode) |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 59 | : mRenderThread(RenderThread::getInstance()) |
| 60 | , mContext(0) { |
| 61 | SETUP_TASK(createContext); |
| 62 | args->translucent = translucent; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 63 | args->rootRenderNode = rootRenderNode; |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 64 | mContext = (CanvasContext*) postAndWait(task); |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 65 | mDrawFrameTask.setContext(&mRenderThread, mContext); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | RenderProxy::~RenderProxy() { |
| 69 | destroyContext(); |
| 70 | } |
| 71 | |
| 72 | CREATE_BRIDGE1(destroyContext, CanvasContext* context) { |
| 73 | delete args->context; |
| 74 | return NULL; |
| 75 | } |
| 76 | |
| 77 | void RenderProxy::destroyContext() { |
| 78 | if (mContext) { |
| 79 | SETUP_TASK(destroyContext); |
| 80 | args->context = mContext; |
| 81 | mContext = 0; |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 82 | mDrawFrameTask.setContext(NULL, NULL); |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 83 | // This is also a fence as we need to be certain that there are no |
| 84 | // outstanding mDrawFrame tasks posted before it is destroyed |
| 85 | postAndWait(task); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 89 | CREATE_BRIDGE2(setFrameInterval, RenderThread* thread, nsecs_t frameIntervalNanos) { |
| 90 | args->thread->timeLord().setFrameInterval(args->frameIntervalNanos); |
| 91 | return NULL; |
| 92 | } |
| 93 | |
| 94 | void RenderProxy::setFrameInterval(nsecs_t frameIntervalNanos) { |
| 95 | SETUP_TASK(setFrameInterval); |
| 96 | args->thread = &mRenderThread; |
| 97 | args->frameIntervalNanos = frameIntervalNanos; |
| 98 | post(task); |
| 99 | } |
| 100 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 101 | CREATE_BRIDGE2(initialize, CanvasContext* context, EGLNativeWindowType window) { |
| 102 | return (void*) args->context->initialize(args->window); |
| 103 | } |
| 104 | |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 105 | bool RenderProxy::initialize(const sp<ANativeWindow>& window) { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 106 | SETUP_TASK(initialize); |
| 107 | args->context = mContext; |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 108 | args->window = window.get(); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 109 | return (bool) postAndWait(task); |
| 110 | } |
| 111 | |
| 112 | CREATE_BRIDGE2(updateSurface, CanvasContext* context, EGLNativeWindowType window) { |
| 113 | args->context->updateSurface(args->window); |
| 114 | return NULL; |
| 115 | } |
| 116 | |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 117 | void RenderProxy::updateSurface(const sp<ANativeWindow>& window) { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 118 | SETUP_TASK(updateSurface); |
| 119 | args->context = mContext; |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 120 | args->window = window.get(); |
| 121 | postAndWait(task); |
| 122 | } |
| 123 | |
| 124 | CREATE_BRIDGE2(pauseSurface, CanvasContext* context, EGLNativeWindowType window) { |
| 125 | args->context->pauseSurface(args->window); |
| 126 | return NULL; |
| 127 | } |
| 128 | |
| 129 | void RenderProxy::pauseSurface(const sp<ANativeWindow>& window) { |
| 130 | SETUP_TASK(pauseSurface); |
| 131 | args->context = mContext; |
| 132 | args->window = window.get(); |
| 133 | postAndWait(task); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | CREATE_BRIDGE3(setup, CanvasContext* context, int width, int height) { |
| 137 | args->context->setup(args->width, args->height); |
| 138 | return NULL; |
| 139 | } |
| 140 | |
| 141 | void RenderProxy::setup(int width, int height) { |
| 142 | SETUP_TASK(setup); |
| 143 | args->context = mContext; |
| 144 | args->width = width; |
| 145 | args->height = height; |
| 146 | post(task); |
| 147 | } |
| 148 | |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame^] | 149 | int RenderProxy::syncAndDrawFrame(nsecs_t frameTimeNanos, |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 150 | int dirtyLeft, int dirtyTop, int dirtyRight, int dirtyBottom) { |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 151 | mDrawFrameTask.setDirty(dirtyLeft, dirtyTop, dirtyRight, dirtyBottom); |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame^] | 152 | return mDrawFrameTask.drawFrame(frameTimeNanos); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 153 | } |
| 154 | |
John Reck | fae904d | 2014-04-14 11:01:57 -0700 | [diff] [blame] | 155 | CREATE_BRIDGE1(destroyCanvasAndSurface, CanvasContext* context) { |
| 156 | args->context->destroyCanvasAndSurface(); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 157 | return NULL; |
| 158 | } |
| 159 | |
John Reck | fae904d | 2014-04-14 11:01:57 -0700 | [diff] [blame] | 160 | void RenderProxy::destroyCanvasAndSurface() { |
| 161 | SETUP_TASK(destroyCanvasAndSurface); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 162 | args->context = mContext; |
John Reck | fae904d | 2014-04-14 11:01:57 -0700 | [diff] [blame] | 163 | // destroyCanvasAndSurface() needs a fence as when it returns the |
| 164 | // underlying BufferQueue is going to be released from under |
| 165 | // the render thread. |
| 166 | postAndWait(task); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 167 | } |
| 168 | |
John Reck | 0d1f634 | 2014-03-28 20:30:27 -0700 | [diff] [blame] | 169 | CREATE_BRIDGE2(invokeFunctor, CanvasContext* context, Functor* functor) { |
| 170 | args->context->invokeFunctor(args->functor); |
| 171 | return NULL; |
| 172 | } |
| 173 | |
| 174 | void RenderProxy::invokeFunctor(Functor* functor, bool waitForCompletion) { |
John Reck | d3d8daf | 2014-04-10 15:00:13 -0700 | [diff] [blame] | 175 | ATRACE_CALL(); |
John Reck | 0d1f634 | 2014-03-28 20:30:27 -0700 | [diff] [blame] | 176 | SETUP_TASK(invokeFunctor); |
| 177 | args->context = mContext; |
| 178 | args->functor = functor; |
| 179 | if (waitForCompletion) { |
| 180 | postAndWait(task); |
| 181 | } else { |
| 182 | post(task); |
| 183 | } |
| 184 | } |
| 185 | |
John Reck | fc53ef27 | 2014-02-11 10:40:25 -0800 | [diff] [blame] | 186 | CREATE_BRIDGE2(runWithGlContext, CanvasContext* context, RenderTask* task) { |
| 187 | args->context->runWithGlContext(args->task); |
| 188 | return NULL; |
| 189 | } |
| 190 | |
| 191 | void RenderProxy::runWithGlContext(RenderTask* gltask) { |
| 192 | SETUP_TASK(runWithGlContext); |
| 193 | args->context = mContext; |
| 194 | args->task = gltask; |
| 195 | postAndWait(task); |
| 196 | } |
| 197 | |
John Reck | 1949e79 | 2014-04-08 15:18:56 -0700 | [diff] [blame] | 198 | CREATE_BRIDGE3(createDisplayListLayer, CanvasContext* context, int width, int height) { |
| 199 | Layer* layer = args->context->createRenderLayer(args->width, args->height); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 200 | if (!layer) return 0; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 201 | return new DeferredLayerUpdater(layer); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | DeferredLayerUpdater* RenderProxy::createDisplayListLayer(int width, int height) { |
| 205 | SETUP_TASK(createDisplayListLayer); |
| 206 | args->width = width; |
| 207 | args->height = height; |
John Reck | 1949e79 | 2014-04-08 15:18:56 -0700 | [diff] [blame] | 208 | args->context = mContext; |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 209 | void* retval = postAndWait(task); |
| 210 | DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval); |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 211 | mDrawFrameTask.addLayer(layer); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 212 | return layer; |
| 213 | } |
| 214 | |
John Reck | 1949e79 | 2014-04-08 15:18:56 -0700 | [diff] [blame] | 215 | CREATE_BRIDGE1(createTextureLayer, CanvasContext* context) { |
| 216 | Layer* layer = args->context->createTextureLayer(); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 217 | if (!layer) return 0; |
| 218 | return new DeferredLayerUpdater(layer); |
| 219 | } |
| 220 | |
| 221 | DeferredLayerUpdater* RenderProxy::createTextureLayer() { |
| 222 | SETUP_TASK(createTextureLayer); |
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 | |
| 230 | CREATE_BRIDGE1(destroyLayer, Layer* layer) { |
| 231 | LayerRenderer::destroyLayer(args->layer); |
| 232 | return NULL; |
| 233 | } |
| 234 | |
| 235 | CREATE_BRIDGE3(copyLayerInto, CanvasContext* context, DeferredLayerUpdater* layer, |
| 236 | SkBitmap* bitmap) { |
| 237 | bool success = args->context->copyLayerInto(args->layer, args->bitmap); |
| 238 | return (void*) success; |
| 239 | } |
| 240 | |
| 241 | bool RenderProxy::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) { |
| 242 | SETUP_TASK(copyLayerInto); |
| 243 | args->context = mContext; |
| 244 | args->layer = layer; |
| 245 | args->bitmap = bitmap; |
| 246 | return (bool) postAndWait(task); |
| 247 | } |
| 248 | |
| 249 | void RenderProxy::destroyLayer(DeferredLayerUpdater* layer) { |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 250 | mDrawFrameTask.removeLayer(layer); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 251 | SETUP_TASK(destroyLayer); |
| 252 | args->layer = layer->detachBackingLayer(); |
| 253 | post(task); |
| 254 | } |
| 255 | |
John Reck | 28ad7b5 | 2014-04-07 16:59:25 -0700 | [diff] [blame] | 256 | CREATE_BRIDGE0(fence) { |
| 257 | // Intentionally empty |
| 258 | return NULL; |
| 259 | } |
| 260 | |
| 261 | void RenderProxy::fence() { |
| 262 | SETUP_TASK(fence); |
| 263 | postAndWait(task); |
| 264 | } |
| 265 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 266 | void RenderProxy::post(RenderTask* task) { |
| 267 | mRenderThread.queue(task); |
| 268 | } |
| 269 | |
| 270 | void* RenderProxy::postAndWait(MethodInvokeRenderTask* task) { |
| 271 | void* retval; |
| 272 | task->setReturnPtr(&retval); |
| 273 | SignalingRenderTask syncTask(task, &mSyncMutex, &mSyncCondition); |
| 274 | AutoMutex _lock(mSyncMutex); |
| 275 | mRenderThread.queue(&syncTask); |
| 276 | mSyncCondition.wait(mSyncMutex); |
| 277 | return retval; |
| 278 | } |
| 279 | |
| 280 | } /* namespace renderthread */ |
| 281 | } /* namespace uirenderer */ |
| 282 | } /* namespace android */ |