blob: 82a2dbc67e4c1a4b8c90b745c8d60dde64b89a65 [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
John Recke45b1fd2014-04-15 09:50:16 -070054CREATE_BRIDGE2(createContext, bool translucent, RenderNode* rootRenderNode) {
55 return new CanvasContext(args->translucent, args->rootRenderNode);
John Reck4f02bf42014-01-03 18:09:17 -080056}
57
John Recke45b1fd2014-04-15 09:50:16 -070058RenderProxy::RenderProxy(bool translucent, RenderNode* rootRenderNode)
John Reck4f02bf42014-01-03 18:09:17 -080059 : mRenderThread(RenderThread::getInstance())
60 , mContext(0) {
61 SETUP_TASK(createContext);
62 args->translucent = translucent;
John Recke45b1fd2014-04-15 09:50:16 -070063 args->rootRenderNode = rootRenderNode;
John Reck4f02bf42014-01-03 18:09:17 -080064 mContext = (CanvasContext*) postAndWait(task);
John Reck18f16e62014-05-02 16:46:41 -070065 mDrawFrameTask.setContext(&mRenderThread, mContext);
John Reck4f02bf42014-01-03 18:09:17 -080066}
67
68RenderProxy::~RenderProxy() {
69 destroyContext();
70}
71
72CREATE_BRIDGE1(destroyContext, CanvasContext* context) {
73 delete args->context;
74 return NULL;
75}
76
77void RenderProxy::destroyContext() {
78 if (mContext) {
79 SETUP_TASK(destroyContext);
80 args->context = mContext;
81 mContext = 0;
John Reck18f16e62014-05-02 16:46:41 -070082 mDrawFrameTask.setContext(NULL, NULL);
John Reck668f0e32014-03-26 15:10:40 -070083 // 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 Reck4f02bf42014-01-03 18:09:17 -080086 }
87}
88
John Reck18f16e62014-05-02 16:46:41 -070089CREATE_BRIDGE2(setFrameInterval, RenderThread* thread, nsecs_t frameIntervalNanos) {
90 args->thread->timeLord().setFrameInterval(args->frameIntervalNanos);
91 return NULL;
92}
93
94void RenderProxy::setFrameInterval(nsecs_t frameIntervalNanos) {
95 SETUP_TASK(setFrameInterval);
96 args->thread = &mRenderThread;
97 args->frameIntervalNanos = frameIntervalNanos;
98 post(task);
99}
100
John Recke4280ba2014-05-05 16:39:37 -0700101CREATE_BRIDGE0(loadSystemProperties) {
102 bool needsRedraw = false;
103 if (Caches::hasInstance()) {
104 needsRedraw = Caches::getInstance().initProperties();
105 }
106 return (void*) needsRedraw;
107}
108
109bool RenderProxy::loadSystemProperties() {
110 SETUP_TASK(loadSystemProperties);
111 return (bool) postAndWait(task);
112}
113
John Reck4f02bf42014-01-03 18:09:17 -0800114CREATE_BRIDGE2(initialize, CanvasContext* context, EGLNativeWindowType window) {
115 return (void*) args->context->initialize(args->window);
116}
117
John Reckf7d9c1d2014-04-09 10:01:03 -0700118bool RenderProxy::initialize(const sp<ANativeWindow>& window) {
John Reck4f02bf42014-01-03 18:09:17 -0800119 SETUP_TASK(initialize);
120 args->context = mContext;
John Reckf7d9c1d2014-04-09 10:01:03 -0700121 args->window = window.get();
John Reck4f02bf42014-01-03 18:09:17 -0800122 return (bool) postAndWait(task);
123}
124
125CREATE_BRIDGE2(updateSurface, CanvasContext* context, EGLNativeWindowType window) {
126 args->context->updateSurface(args->window);
127 return NULL;
128}
129
John Reckf7d9c1d2014-04-09 10:01:03 -0700130void RenderProxy::updateSurface(const sp<ANativeWindow>& window) {
John Reck4f02bf42014-01-03 18:09:17 -0800131 SETUP_TASK(updateSurface);
132 args->context = mContext;
John Reckf7d9c1d2014-04-09 10:01:03 -0700133 args->window = window.get();
134 postAndWait(task);
135}
136
137CREATE_BRIDGE2(pauseSurface, CanvasContext* context, EGLNativeWindowType window) {
138 args->context->pauseSurface(args->window);
139 return NULL;
140}
141
142void RenderProxy::pauseSurface(const sp<ANativeWindow>& window) {
143 SETUP_TASK(pauseSurface);
144 args->context = mContext;
145 args->window = window.get();
146 postAndWait(task);
John Reck4f02bf42014-01-03 18:09:17 -0800147}
148
149CREATE_BRIDGE3(setup, CanvasContext* context, int width, int height) {
150 args->context->setup(args->width, args->height);
151 return NULL;
152}
153
154void RenderProxy::setup(int width, int height) {
155 SETUP_TASK(setup);
156 args->context = mContext;
157 args->width = width;
158 args->height = height;
159 post(task);
160}
161
John Reck63a06672014-05-07 13:45:54 -0700162CREATE_BRIDGE2(setOpaque, CanvasContext* context, bool opaque) {
163 args->context->setOpaque(args->opaque);
164 return NULL;
165}
166
167void RenderProxy::setOpaque(bool opaque) {
168 SETUP_TASK(setOpaque);
169 args->context = mContext;
170 args->opaque = opaque;
171 post(task);
172}
173
John Reckf9be7792014-05-02 18:21:16 -0700174int RenderProxy::syncAndDrawFrame(nsecs_t frameTimeNanos,
John Reck4f02bf42014-01-03 18:09:17 -0800175 int dirtyLeft, int dirtyTop, int dirtyRight, int dirtyBottom) {
John Reck668f0e32014-03-26 15:10:40 -0700176 mDrawFrameTask.setDirty(dirtyLeft, dirtyTop, dirtyRight, dirtyBottom);
John Reckf9be7792014-05-02 18:21:16 -0700177 return mDrawFrameTask.drawFrame(frameTimeNanos);
John Reck4f02bf42014-01-03 18:09:17 -0800178}
179
John Reckfae904d2014-04-14 11:01:57 -0700180CREATE_BRIDGE1(destroyCanvasAndSurface, CanvasContext* context) {
181 args->context->destroyCanvasAndSurface();
John Reck4f02bf42014-01-03 18:09:17 -0800182 return NULL;
183}
184
John Reckfae904d2014-04-14 11:01:57 -0700185void RenderProxy::destroyCanvasAndSurface() {
186 SETUP_TASK(destroyCanvasAndSurface);
John Reck4f02bf42014-01-03 18:09:17 -0800187 args->context = mContext;
John Reckfae904d2014-04-14 11:01:57 -0700188 // destroyCanvasAndSurface() needs a fence as when it returns the
189 // underlying BufferQueue is going to be released from under
190 // the render thread.
191 postAndWait(task);
John Reck4f02bf42014-01-03 18:09:17 -0800192}
193
John Reck0d1f6342014-03-28 20:30:27 -0700194CREATE_BRIDGE2(invokeFunctor, CanvasContext* context, Functor* functor) {
195 args->context->invokeFunctor(args->functor);
196 return NULL;
197}
198
199void RenderProxy::invokeFunctor(Functor* functor, bool waitForCompletion) {
John Reckd3d8daf2014-04-10 15:00:13 -0700200 ATRACE_CALL();
John Reck0d1f6342014-03-28 20:30:27 -0700201 SETUP_TASK(invokeFunctor);
202 args->context = mContext;
203 args->functor = functor;
204 if (waitForCompletion) {
205 postAndWait(task);
206 } else {
207 post(task);
208 }
209}
210
John Reckfc53ef272014-02-11 10:40:25 -0800211CREATE_BRIDGE2(runWithGlContext, CanvasContext* context, RenderTask* task) {
212 args->context->runWithGlContext(args->task);
213 return NULL;
214}
215
216void RenderProxy::runWithGlContext(RenderTask* gltask) {
217 SETUP_TASK(runWithGlContext);
218 args->context = mContext;
219 args->task = gltask;
220 postAndWait(task);
221}
222
John Reck1949e792014-04-08 15:18:56 -0700223CREATE_BRIDGE3(createDisplayListLayer, CanvasContext* context, int width, int height) {
224 Layer* layer = args->context->createRenderLayer(args->width, args->height);
John Reck19b6bcf2014-02-14 20:03:38 -0800225 if (!layer) return 0;
John Reck668f0e32014-03-26 15:10:40 -0700226 return new DeferredLayerUpdater(layer);
John Reck19b6bcf2014-02-14 20:03:38 -0800227}
228
229DeferredLayerUpdater* RenderProxy::createDisplayListLayer(int width, int height) {
230 SETUP_TASK(createDisplayListLayer);
231 args->width = width;
232 args->height = height;
John Reck1949e792014-04-08 15:18:56 -0700233 args->context = mContext;
John Reck19b6bcf2014-02-14 20:03:38 -0800234 void* retval = postAndWait(task);
235 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval);
John Reck668f0e32014-03-26 15:10:40 -0700236 mDrawFrameTask.addLayer(layer);
John Reck19b6bcf2014-02-14 20:03:38 -0800237 return layer;
238}
239
John Reck1949e792014-04-08 15:18:56 -0700240CREATE_BRIDGE1(createTextureLayer, CanvasContext* context) {
241 Layer* layer = args->context->createTextureLayer();
John Reck19b6bcf2014-02-14 20:03:38 -0800242 if (!layer) return 0;
243 return new DeferredLayerUpdater(layer);
244}
245
246DeferredLayerUpdater* RenderProxy::createTextureLayer() {
247 SETUP_TASK(createTextureLayer);
John Reck1949e792014-04-08 15:18:56 -0700248 args->context = mContext;
John Reck19b6bcf2014-02-14 20:03:38 -0800249 void* retval = postAndWait(task);
250 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval);
John Reck668f0e32014-03-26 15:10:40 -0700251 mDrawFrameTask.addLayer(layer);
John Reck19b6bcf2014-02-14 20:03:38 -0800252 return layer;
253}
254
255CREATE_BRIDGE1(destroyLayer, Layer* layer) {
256 LayerRenderer::destroyLayer(args->layer);
257 return NULL;
258}
259
260CREATE_BRIDGE3(copyLayerInto, CanvasContext* context, DeferredLayerUpdater* layer,
261 SkBitmap* bitmap) {
262 bool success = args->context->copyLayerInto(args->layer, args->bitmap);
263 return (void*) success;
264}
265
266bool RenderProxy::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
267 SETUP_TASK(copyLayerInto);
268 args->context = mContext;
269 args->layer = layer;
270 args->bitmap = bitmap;
271 return (bool) postAndWait(task);
272}
273
274void RenderProxy::destroyLayer(DeferredLayerUpdater* layer) {
John Reck668f0e32014-03-26 15:10:40 -0700275 mDrawFrameTask.removeLayer(layer);
John Reck19b6bcf2014-02-14 20:03:38 -0800276 SETUP_TASK(destroyLayer);
277 args->layer = layer->detachBackingLayer();
278 post(task);
279}
280
John Reck28ad7b52014-04-07 16:59:25 -0700281CREATE_BRIDGE0(fence) {
282 // Intentionally empty
283 return NULL;
284}
285
286void RenderProxy::fence() {
287 SETUP_TASK(fence);
288 postAndWait(task);
289}
290
John Reck4f02bf42014-01-03 18:09:17 -0800291void RenderProxy::post(RenderTask* task) {
292 mRenderThread.queue(task);
293}
294
295void* RenderProxy::postAndWait(MethodInvokeRenderTask* task) {
296 void* retval;
297 task->setReturnPtr(&retval);
298 SignalingRenderTask syncTask(task, &mSyncMutex, &mSyncCondition);
299 AutoMutex _lock(mSyncMutex);
300 mRenderThread.queue(&syncTask);
301 mSyncCondition.wait(mSyncMutex);
302 return retval;
303}
304
305} /* namespace renderthread */
306} /* namespace uirenderer */
307} /* namespace android */