blob: ce490f1cde6ab31772c46ebd4be6a30aa06d242a [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) {
78 SETUP_TASK(destroyContext);
79 args->context = mContext;
80 mContext = 0;
John Reck668f0e32014-03-26 15:10:40 -070081 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 Reck4f02bf42014-01-03 18:09:17 -080085 }
86}
87
88CREATE_BRIDGE2(initialize, CanvasContext* context, EGLNativeWindowType window) {
89 return (void*) args->context->initialize(args->window);
90}
91
John Reckf7d9c1d2014-04-09 10:01:03 -070092bool RenderProxy::initialize(const sp<ANativeWindow>& window) {
John Reck4f02bf42014-01-03 18:09:17 -080093 SETUP_TASK(initialize);
94 args->context = mContext;
John Reckf7d9c1d2014-04-09 10:01:03 -070095 args->window = window.get();
John Reck4f02bf42014-01-03 18:09:17 -080096 return (bool) postAndWait(task);
97}
98
99CREATE_BRIDGE2(updateSurface, CanvasContext* context, EGLNativeWindowType window) {
100 args->context->updateSurface(args->window);
101 return NULL;
102}
103
John Reckf7d9c1d2014-04-09 10:01:03 -0700104void RenderProxy::updateSurface(const sp<ANativeWindow>& window) {
John Reck4f02bf42014-01-03 18:09:17 -0800105 SETUP_TASK(updateSurface);
106 args->context = mContext;
John Reckf7d9c1d2014-04-09 10:01:03 -0700107 args->window = window.get();
108 postAndWait(task);
109}
110
111CREATE_BRIDGE2(pauseSurface, CanvasContext* context, EGLNativeWindowType window) {
112 args->context->pauseSurface(args->window);
113 return NULL;
114}
115
116void RenderProxy::pauseSurface(const sp<ANativeWindow>& window) {
117 SETUP_TASK(pauseSurface);
118 args->context = mContext;
119 args->window = window.get();
120 postAndWait(task);
John Reck4f02bf42014-01-03 18:09:17 -0800121}
122
123CREATE_BRIDGE3(setup, CanvasContext* context, int width, int height) {
124 args->context->setup(args->width, args->height);
125 return NULL;
126}
127
128void RenderProxy::setup(int width, int height) {
129 SETUP_TASK(setup);
130 args->context = mContext;
131 args->width = width;
132 args->height = height;
133 post(task);
134}
135
John Recke18264b2014-03-12 13:56:30 -0700136void RenderProxy::drawDisplayList(RenderNode* displayList,
John Reck4f02bf42014-01-03 18:09:17 -0800137 int dirtyLeft, int dirtyTop, int dirtyRight, int dirtyBottom) {
John Reck668f0e32014-03-26 15:10:40 -0700138 mDrawFrameTask.setRenderNode(displayList);
139 mDrawFrameTask.setDirty(dirtyLeft, dirtyTop, dirtyRight, dirtyBottom);
140 mDrawFrameTask.drawFrame(&mRenderThread);
John Reck4f02bf42014-01-03 18:09:17 -0800141}
142
John Reckfae904d2014-04-14 11:01:57 -0700143CREATE_BRIDGE1(destroyCanvasAndSurface, CanvasContext* context) {
144 args->context->destroyCanvasAndSurface();
John Reck4f02bf42014-01-03 18:09:17 -0800145 return NULL;
146}
147
John Reckfae904d2014-04-14 11:01:57 -0700148void RenderProxy::destroyCanvasAndSurface() {
149 SETUP_TASK(destroyCanvasAndSurface);
John Reck4f02bf42014-01-03 18:09:17 -0800150 args->context = mContext;
John Reckfae904d2014-04-14 11:01:57 -0700151 // destroyCanvasAndSurface() needs a fence as when it returns the
152 // underlying BufferQueue is going to be released from under
153 // the render thread.
154 postAndWait(task);
John Reck4f02bf42014-01-03 18:09:17 -0800155}
156
John Reck0d1f6342014-03-28 20:30:27 -0700157CREATE_BRIDGE2(invokeFunctor, CanvasContext* context, Functor* functor) {
158 args->context->invokeFunctor(args->functor);
159 return NULL;
160}
161
162void RenderProxy::invokeFunctor(Functor* functor, bool waitForCompletion) {
John Reckd3d8daf2014-04-10 15:00:13 -0700163 ATRACE_CALL();
John Reck0d1f6342014-03-28 20:30:27 -0700164 SETUP_TASK(invokeFunctor);
165 args->context = mContext;
166 args->functor = functor;
167 if (waitForCompletion) {
168 postAndWait(task);
169 } else {
170 post(task);
171 }
172}
173
John Reckfc53ef272014-02-11 10:40:25 -0800174CREATE_BRIDGE2(runWithGlContext, CanvasContext* context, RenderTask* task) {
175 args->context->runWithGlContext(args->task);
176 return NULL;
177}
178
179void RenderProxy::runWithGlContext(RenderTask* gltask) {
180 SETUP_TASK(runWithGlContext);
181 args->context = mContext;
182 args->task = gltask;
183 postAndWait(task);
184}
185
John Reck1949e792014-04-08 15:18:56 -0700186CREATE_BRIDGE3(createDisplayListLayer, CanvasContext* context, int width, int height) {
187 Layer* layer = args->context->createRenderLayer(args->width, args->height);
John Reck19b6bcf2014-02-14 20:03:38 -0800188 if (!layer) return 0;
John Reck668f0e32014-03-26 15:10:40 -0700189 return new DeferredLayerUpdater(layer);
John Reck19b6bcf2014-02-14 20:03:38 -0800190}
191
192DeferredLayerUpdater* RenderProxy::createDisplayListLayer(int width, int height) {
193 SETUP_TASK(createDisplayListLayer);
194 args->width = width;
195 args->height = height;
John Reck1949e792014-04-08 15:18:56 -0700196 args->context = mContext;
John Reck19b6bcf2014-02-14 20:03:38 -0800197 void* retval = postAndWait(task);
198 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval);
John Reck668f0e32014-03-26 15:10:40 -0700199 mDrawFrameTask.addLayer(layer);
John Reck19b6bcf2014-02-14 20:03:38 -0800200 return layer;
201}
202
John Reck1949e792014-04-08 15:18:56 -0700203CREATE_BRIDGE1(createTextureLayer, CanvasContext* context) {
204 Layer* layer = args->context->createTextureLayer();
John Reck19b6bcf2014-02-14 20:03:38 -0800205 if (!layer) return 0;
206 return new DeferredLayerUpdater(layer);
207}
208
209DeferredLayerUpdater* RenderProxy::createTextureLayer() {
210 SETUP_TASK(createTextureLayer);
John Reck1949e792014-04-08 15:18:56 -0700211 args->context = mContext;
John Reck19b6bcf2014-02-14 20:03:38 -0800212 void* retval = postAndWait(task);
213 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval);
John Reck668f0e32014-03-26 15:10:40 -0700214 mDrawFrameTask.addLayer(layer);
John Reck19b6bcf2014-02-14 20:03:38 -0800215 return layer;
216}
217
218CREATE_BRIDGE1(destroyLayer, Layer* layer) {
219 LayerRenderer::destroyLayer(args->layer);
220 return NULL;
221}
222
223CREATE_BRIDGE3(copyLayerInto, CanvasContext* context, DeferredLayerUpdater* layer,
224 SkBitmap* bitmap) {
225 bool success = args->context->copyLayerInto(args->layer, args->bitmap);
226 return (void*) success;
227}
228
229bool RenderProxy::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
230 SETUP_TASK(copyLayerInto);
231 args->context = mContext;
232 args->layer = layer;
233 args->bitmap = bitmap;
234 return (bool) postAndWait(task);
235}
236
237void RenderProxy::destroyLayer(DeferredLayerUpdater* layer) {
John Reck668f0e32014-03-26 15:10:40 -0700238 mDrawFrameTask.removeLayer(layer);
John Reck19b6bcf2014-02-14 20:03:38 -0800239 SETUP_TASK(destroyLayer);
240 args->layer = layer->detachBackingLayer();
241 post(task);
242}
243
John Reck28ad7b52014-04-07 16:59:25 -0700244CREATE_BRIDGE0(fence) {
245 // Intentionally empty
246 return NULL;
247}
248
249void RenderProxy::fence() {
250 SETUP_TASK(fence);
251 postAndWait(task);
252}
253
John Reck4f02bf42014-01-03 18:09:17 -0800254void RenderProxy::post(RenderTask* task) {
255 mRenderThread.queue(task);
256}
257
258void* RenderProxy::postAndWait(MethodInvokeRenderTask* task) {
259 void* retval;
260 task->setReturnPtr(&retval);
261 SignalingRenderTask syncTask(task, &mSyncMutex, &mSyncCondition);
262 AutoMutex _lock(mSyncMutex);
263 mRenderThread.queue(&syncTask);
264 mSyncCondition.wait(mSyncMutex);
265 return retval;
266}
267
268} /* namespace renderthread */
269} /* namespace uirenderer */
270} /* namespace android */