blob: e1b8b85fc9dfe0cb3d848308684651fb1f0ded83 [file] [log] [blame]
John Reck4f02bf42014-01-03 18:09:17 -08001/*
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 Reck19b6bcf2014-02-14 20:03:38 -080025#include "../DeferredLayerUpdater.h"
John Reck4f02bf42014-01-03 18:09:17 -080026#include "../DisplayList.h"
John Reck19b6bcf2014-02-14 20:03:38 -080027#include "../LayerRenderer.h"
John Reck4f02bf42014-01-03 18:09:17 -080028#include "../Rect.h"
29
30namespace android {
31namespace uirenderer {
32namespace renderthread {
33
34#define ARGS(method) method ## Args
35
John Reck19b6bcf2014-02-14 20:03:38 -080036#define CREATE_BRIDGE0(name) CREATE_BRIDGE(name,,,,,,,,)
John Reck4f02bf42014-01-03 18:09:17 -080037#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 Recke2c45522014-04-07 17:31:44 -070051 MethodInvokeRenderTask* task = new MethodInvokeRenderTask((RunnableMethod) Bridge_ ## method); \
John Reck4f02bf42014-01-03 18:09:17 -080052 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);
John Reck668f0e32014-03-26 15:10:40 -070064 mDrawFrameTask.setContext(mContext);
John Reck4f02bf42014-01-03 18:09:17 -080065}
66
67RenderProxy::~RenderProxy() {
68 destroyContext();
69}
70
71CREATE_BRIDGE1(destroyContext, CanvasContext* context) {
72 delete args->context;
73 return NULL;
74}
75
76void RenderProxy::destroyContext() {
77 if (mContext) {
John Reck087bc0c2014-04-04 16:20:08 -070078 // Flush any pending changes to ensure all garbage is destroyed
79 mDrawFrameTask.flushStateChanges(&mRenderThread);
80
John Reck4f02bf42014-01-03 18:09:17 -080081 SETUP_TASK(destroyContext);
82 args->context = mContext;
83 mContext = 0;
John Reck668f0e32014-03-26 15:10:40 -070084 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 Reck4f02bf42014-01-03 18:09:17 -080088 }
89}
90
91CREATE_BRIDGE2(initialize, CanvasContext* context, EGLNativeWindowType window) {
92 return (void*) args->context->initialize(args->window);
93}
94
John Reckf7d9c1d2014-04-09 10:01:03 -070095bool RenderProxy::initialize(const sp<ANativeWindow>& window) {
John Reck4f02bf42014-01-03 18:09:17 -080096 SETUP_TASK(initialize);
97 args->context = mContext;
John Reckf7d9c1d2014-04-09 10:01:03 -070098 args->window = window.get();
John Reck4f02bf42014-01-03 18:09:17 -080099 return (bool) postAndWait(task);
100}
101
102CREATE_BRIDGE2(updateSurface, CanvasContext* context, EGLNativeWindowType window) {
103 args->context->updateSurface(args->window);
104 return NULL;
105}
106
John Reckf7d9c1d2014-04-09 10:01:03 -0700107void RenderProxy::updateSurface(const sp<ANativeWindow>& window) {
John Reck4f02bf42014-01-03 18:09:17 -0800108 SETUP_TASK(updateSurface);
109 args->context = mContext;
John Reckf7d9c1d2014-04-09 10:01:03 -0700110 args->window = window.get();
111 postAndWait(task);
112}
113
114CREATE_BRIDGE2(pauseSurface, CanvasContext* context, EGLNativeWindowType window) {
115 args->context->pauseSurface(args->window);
116 return NULL;
117}
118
119void RenderProxy::pauseSurface(const sp<ANativeWindow>& window) {
120 SETUP_TASK(pauseSurface);
121 args->context = mContext;
122 args->window = window.get();
123 postAndWait(task);
John Reck4f02bf42014-01-03 18:09:17 -0800124}
125
126CREATE_BRIDGE3(setup, CanvasContext* context, int width, int height) {
127 args->context->setup(args->width, args->height);
128 return NULL;
129}
130
131void 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 Recke18264b2014-03-12 13:56:30 -0700139void RenderProxy::drawDisplayList(RenderNode* displayList,
John Reck4f02bf42014-01-03 18:09:17 -0800140 int dirtyLeft, int dirtyTop, int dirtyRight, int dirtyBottom) {
John Reck668f0e32014-03-26 15:10:40 -0700141 mDrawFrameTask.setRenderNode(displayList);
142 mDrawFrameTask.setDirty(dirtyLeft, dirtyTop, dirtyRight, dirtyBottom);
143 mDrawFrameTask.drawFrame(&mRenderThread);
John Reck4f02bf42014-01-03 18:09:17 -0800144}
145
146CREATE_BRIDGE1(destroyCanvas, CanvasContext* context) {
147 args->context->destroyCanvas();
148 return NULL;
149}
150
151void RenderProxy::destroyCanvas() {
John Reck087bc0c2014-04-04 16:20:08 -0700152 // 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 Reck4f02bf42014-01-03 18:09:17 -0800156 SETUP_TASK(destroyCanvas);
157 args->context = mContext;
158 post(task);
159}
160
John Reck0d1f6342014-03-28 20:30:27 -0700161CREATE_BRIDGE2(invokeFunctor, CanvasContext* context, Functor* functor) {
162 args->context->invokeFunctor(args->functor);
163 return NULL;
164}
165
166void RenderProxy::invokeFunctor(Functor* functor, bool waitForCompletion) {
John Reckd3d8daf2014-04-10 15:00:13 -0700167 ATRACE_CALL();
John Reck0d1f6342014-03-28 20:30:27 -0700168 SETUP_TASK(invokeFunctor);
169 args->context = mContext;
170 args->functor = functor;
171 if (waitForCompletion) {
172 postAndWait(task);
173 } else {
174 post(task);
175 }
176}
177
John Reckfc53ef272014-02-11 10:40:25 -0800178CREATE_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
John Reck1949e792014-04-08 15:18:56 -0700190CREATE_BRIDGE3(createDisplayListLayer, CanvasContext* context, int width, int height) {
191 Layer* layer = args->context->createRenderLayer(args->width, args->height);
John Reck19b6bcf2014-02-14 20:03:38 -0800192 if (!layer) return 0;
John Reck668f0e32014-03-26 15:10:40 -0700193 return new DeferredLayerUpdater(layer);
John Reck19b6bcf2014-02-14 20:03:38 -0800194}
195
196DeferredLayerUpdater* RenderProxy::createDisplayListLayer(int width, int height) {
197 SETUP_TASK(createDisplayListLayer);
198 args->width = width;
199 args->height = height;
John Reck1949e792014-04-08 15:18:56 -0700200 args->context = mContext;
John Reck19b6bcf2014-02-14 20:03:38 -0800201 void* retval = postAndWait(task);
202 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval);
John Reck668f0e32014-03-26 15:10:40 -0700203 mDrawFrameTask.addLayer(layer);
John Reck19b6bcf2014-02-14 20:03:38 -0800204 return layer;
205}
206
John Reck1949e792014-04-08 15:18:56 -0700207CREATE_BRIDGE1(createTextureLayer, CanvasContext* context) {
208 Layer* layer = args->context->createTextureLayer();
John Reck19b6bcf2014-02-14 20:03:38 -0800209 if (!layer) return 0;
210 return new DeferredLayerUpdater(layer);
211}
212
213DeferredLayerUpdater* RenderProxy::createTextureLayer() {
214 SETUP_TASK(createTextureLayer);
John Reck1949e792014-04-08 15:18:56 -0700215 args->context = mContext;
John Reck19b6bcf2014-02-14 20:03:38 -0800216 void* retval = postAndWait(task);
217 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval);
John Reck668f0e32014-03-26 15:10:40 -0700218 mDrawFrameTask.addLayer(layer);
John Reck19b6bcf2014-02-14 20:03:38 -0800219 return layer;
220}
221
222CREATE_BRIDGE1(destroyLayer, Layer* layer) {
223 LayerRenderer::destroyLayer(args->layer);
224 return NULL;
225}
226
227CREATE_BRIDGE3(copyLayerInto, CanvasContext* context, DeferredLayerUpdater* layer,
228 SkBitmap* bitmap) {
229 bool success = args->context->copyLayerInto(args->layer, args->bitmap);
230 return (void*) success;
231}
232
233bool RenderProxy::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
234 SETUP_TASK(copyLayerInto);
235 args->context = mContext;
236 args->layer = layer;
237 args->bitmap = bitmap;
238 return (bool) postAndWait(task);
239}
240
241void RenderProxy::destroyLayer(DeferredLayerUpdater* layer) {
John Reck668f0e32014-03-26 15:10:40 -0700242 mDrawFrameTask.removeLayer(layer);
John Reck19b6bcf2014-02-14 20:03:38 -0800243 SETUP_TASK(destroyLayer);
244 args->layer = layer->detachBackingLayer();
245 post(task);
246}
247
John Reck28ad7b52014-04-07 16:59:25 -0700248CREATE_BRIDGE0(fence) {
249 // Intentionally empty
250 return NULL;
251}
252
253void RenderProxy::fence() {
254 SETUP_TASK(fence);
255 postAndWait(task);
256}
257
John Reck4f02bf42014-01-03 18:09:17 -0800258void RenderProxy::post(RenderTask* task) {
259 mRenderThread.queue(task);
260}
261
262void* RenderProxy::postAndWait(MethodInvokeRenderTask* task) {
263 void* retval;
264 task->setReturnPtr(&retval);
265 SignalingRenderTask syncTask(task, &mSyncMutex, &mSyncCondition);
266 AutoMutex _lock(mSyncMutex);
267 mRenderThread.queue(&syncTask);
268 mSyncCondition.wait(mSyncMutex);
269 return retval;
270}
271
272} /* namespace renderthread */
273} /* namespace uirenderer */
274} /* namespace android */