blob: 09019637693fe66bc8868ad752c0223e73c4462f [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,,,,)
Chris Craik797b95b2014-05-20 18:10:25 -070041#define CREATE_BRIDGE5(name, a1, a2, a3, a4, a5) CREATE_BRIDGE(name, a1,a2,a3,a4,a5,,,)
John Reck4f02bf42014-01-03 18:09:17 -080042#define CREATE_BRIDGE(name, a1, a2, a3, a4, a5, a6, a7, a8) \
43 typedef struct { \
44 a1; a2; a3; a4; a5; a6; a7; a8; \
45 } ARGS(name); \
46 static void* Bridge_ ## name(ARGS(name)* args)
47
48#define SETUP_TASK(method) \
49 LOG_ALWAYS_FATAL_IF( METHOD_INVOKE_PAYLOAD_SIZE < sizeof(ARGS(method)), \
50 "METHOD_INVOKE_PAYLOAD_SIZE %d is smaller than sizeof(" #method "Args) %d", \
51 METHOD_INVOKE_PAYLOAD_SIZE, sizeof(ARGS(method))); \
John Recke2c45522014-04-07 17:31:44 -070052 MethodInvokeRenderTask* task = new MethodInvokeRenderTask((RunnableMethod) Bridge_ ## method); \
John Reck4f02bf42014-01-03 18:09:17 -080053 ARGS(method) *args = (ARGS(method) *) task->payload()
54
John Recke45b1fd2014-04-15 09:50:16 -070055CREATE_BRIDGE2(createContext, bool translucent, RenderNode* rootRenderNode) {
56 return new CanvasContext(args->translucent, args->rootRenderNode);
John Reck4f02bf42014-01-03 18:09:17 -080057}
58
John Recke45b1fd2014-04-15 09:50:16 -070059RenderProxy::RenderProxy(bool translucent, RenderNode* rootRenderNode)
John Reck4f02bf42014-01-03 18:09:17 -080060 : mRenderThread(RenderThread::getInstance())
61 , mContext(0) {
62 SETUP_TASK(createContext);
63 args->translucent = translucent;
John Recke45b1fd2014-04-15 09:50:16 -070064 args->rootRenderNode = rootRenderNode;
John Reck4f02bf42014-01-03 18:09:17 -080065 mContext = (CanvasContext*) postAndWait(task);
John Reck18f16e62014-05-02 16:46:41 -070066 mDrawFrameTask.setContext(&mRenderThread, mContext);
John Reck4f02bf42014-01-03 18:09:17 -080067}
68
69RenderProxy::~RenderProxy() {
70 destroyContext();
71}
72
73CREATE_BRIDGE1(destroyContext, CanvasContext* context) {
74 delete args->context;
75 return NULL;
76}
77
78void RenderProxy::destroyContext() {
79 if (mContext) {
80 SETUP_TASK(destroyContext);
81 args->context = mContext;
82 mContext = 0;
John Reck18f16e62014-05-02 16:46:41 -070083 mDrawFrameTask.setContext(NULL, NULL);
John Reck668f0e32014-03-26 15:10:40 -070084 // This is also a fence as we need to be certain that there are no
85 // outstanding mDrawFrame tasks posted before it is destroyed
86 postAndWait(task);
John Reck4f02bf42014-01-03 18:09:17 -080087 }
88}
89
John Reck18f16e62014-05-02 16:46:41 -070090CREATE_BRIDGE2(setFrameInterval, RenderThread* thread, nsecs_t frameIntervalNanos) {
91 args->thread->timeLord().setFrameInterval(args->frameIntervalNanos);
92 return NULL;
93}
94
95void RenderProxy::setFrameInterval(nsecs_t frameIntervalNanos) {
96 SETUP_TASK(setFrameInterval);
97 args->thread = &mRenderThread;
98 args->frameIntervalNanos = frameIntervalNanos;
99 post(task);
100}
101
John Reckfe5e7b72014-05-23 17:42:28 -0700102CREATE_BRIDGE1(loadSystemProperties, CanvasContext* context) {
John Recke4280ba2014-05-05 16:39:37 -0700103 bool needsRedraw = false;
104 if (Caches::hasInstance()) {
105 needsRedraw = Caches::getInstance().initProperties();
106 }
John Reckfe5e7b72014-05-23 17:42:28 -0700107 if (args->context->profiler().loadSystemProperties()) {
108 needsRedraw = true;
109 }
John Recke4280ba2014-05-05 16:39:37 -0700110 return (void*) needsRedraw;
111}
112
113bool RenderProxy::loadSystemProperties() {
114 SETUP_TASK(loadSystemProperties);
John Reckfe5e7b72014-05-23 17:42:28 -0700115 args->context = mContext;
John Recke4280ba2014-05-05 16:39:37 -0700116 return (bool) postAndWait(task);
117}
118
John Recka5dda642014-05-22 15:43:54 -0700119CREATE_BRIDGE2(initialize, CanvasContext* context, ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -0800120 return (void*) args->context->initialize(args->window);
121}
122
John Reckf7d9c1d2014-04-09 10:01:03 -0700123bool RenderProxy::initialize(const sp<ANativeWindow>& window) {
John Reck4f02bf42014-01-03 18:09:17 -0800124 SETUP_TASK(initialize);
125 args->context = mContext;
John Reckf7d9c1d2014-04-09 10:01:03 -0700126 args->window = window.get();
John Reck4f02bf42014-01-03 18:09:17 -0800127 return (bool) postAndWait(task);
128}
129
John Recka5dda642014-05-22 15:43:54 -0700130CREATE_BRIDGE2(updateSurface, CanvasContext* context, ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -0800131 args->context->updateSurface(args->window);
132 return NULL;
133}
134
John Reckf7d9c1d2014-04-09 10:01:03 -0700135void RenderProxy::updateSurface(const sp<ANativeWindow>& window) {
John Reck4f02bf42014-01-03 18:09:17 -0800136 SETUP_TASK(updateSurface);
137 args->context = mContext;
John Reckf7d9c1d2014-04-09 10:01:03 -0700138 args->window = window.get();
139 postAndWait(task);
140}
141
John Recka5dda642014-05-22 15:43:54 -0700142CREATE_BRIDGE2(pauseSurface, CanvasContext* context, ANativeWindow* window) {
John Reckf7d9c1d2014-04-09 10:01:03 -0700143 args->context->pauseSurface(args->window);
144 return NULL;
145}
146
147void RenderProxy::pauseSurface(const sp<ANativeWindow>& window) {
148 SETUP_TASK(pauseSurface);
149 args->context = mContext;
150 args->window = window.get();
151 postAndWait(task);
John Reck4f02bf42014-01-03 18:09:17 -0800152}
153
Chris Craik797b95b2014-05-20 18:10:25 -0700154CREATE_BRIDGE5(setup, CanvasContext* context, int width, int height,
155 Vector3 lightCenter, int lightRadius) {
156 args->context->setup(args->width, args->height, args->lightCenter, args->lightRadius);
John Reck4f02bf42014-01-03 18:09:17 -0800157 return NULL;
158}
159
Chris Craik797b95b2014-05-20 18:10:25 -0700160void RenderProxy::setup(int width, int height, const Vector3& lightCenter, float lightRadius) {
John Reck4f02bf42014-01-03 18:09:17 -0800161 SETUP_TASK(setup);
162 args->context = mContext;
163 args->width = width;
164 args->height = height;
Chris Craik797b95b2014-05-20 18:10:25 -0700165 args->lightCenter = lightCenter;
166 args->lightRadius = lightRadius;
John Reck4f02bf42014-01-03 18:09:17 -0800167 post(task);
168}
169
John Reck63a06672014-05-07 13:45:54 -0700170CREATE_BRIDGE2(setOpaque, CanvasContext* context, bool opaque) {
171 args->context->setOpaque(args->opaque);
172 return NULL;
173}
174
175void RenderProxy::setOpaque(bool opaque) {
176 SETUP_TASK(setOpaque);
177 args->context = mContext;
178 args->opaque = opaque;
179 post(task);
180}
181
John Reckfe5e7b72014-05-23 17:42:28 -0700182int RenderProxy::syncAndDrawFrame(nsecs_t frameTimeNanos, nsecs_t recordDurationNanos,
183 float density, int dirtyLeft, int dirtyTop, int dirtyRight, int dirtyBottom) {
John Reck668f0e32014-03-26 15:10:40 -0700184 mDrawFrameTask.setDirty(dirtyLeft, dirtyTop, dirtyRight, dirtyBottom);
John Reckfe5e7b72014-05-23 17:42:28 -0700185 mDrawFrameTask.setDensity(density);
186 return mDrawFrameTask.drawFrame(frameTimeNanos, recordDurationNanos);
John Reck4f02bf42014-01-03 18:09:17 -0800187}
188
John Reckfae904d2014-04-14 11:01:57 -0700189CREATE_BRIDGE1(destroyCanvasAndSurface, CanvasContext* context) {
190 args->context->destroyCanvasAndSurface();
John Reck4f02bf42014-01-03 18:09:17 -0800191 return NULL;
192}
193
John Reckfae904d2014-04-14 11:01:57 -0700194void RenderProxy::destroyCanvasAndSurface() {
195 SETUP_TASK(destroyCanvasAndSurface);
John Reck4f02bf42014-01-03 18:09:17 -0800196 args->context = mContext;
John Reckfae904d2014-04-14 11:01:57 -0700197 // destroyCanvasAndSurface() needs a fence as when it returns the
198 // underlying BufferQueue is going to be released from under
199 // the render thread.
200 postAndWait(task);
John Reck4f02bf42014-01-03 18:09:17 -0800201}
202
John Reck0d1f6342014-03-28 20:30:27 -0700203CREATE_BRIDGE2(invokeFunctor, CanvasContext* context, Functor* functor) {
204 args->context->invokeFunctor(args->functor);
205 return NULL;
206}
207
208void RenderProxy::invokeFunctor(Functor* functor, bool waitForCompletion) {
John Reckd3d8daf2014-04-10 15:00:13 -0700209 ATRACE_CALL();
John Reck0d1f6342014-03-28 20:30:27 -0700210 SETUP_TASK(invokeFunctor);
211 args->context = mContext;
212 args->functor = functor;
213 if (waitForCompletion) {
214 postAndWait(task);
215 } else {
216 post(task);
217 }
218}
219
John Reckfc53ef272014-02-11 10:40:25 -0800220CREATE_BRIDGE2(runWithGlContext, CanvasContext* context, RenderTask* task) {
221 args->context->runWithGlContext(args->task);
222 return NULL;
223}
224
225void RenderProxy::runWithGlContext(RenderTask* gltask) {
226 SETUP_TASK(runWithGlContext);
227 args->context = mContext;
228 args->task = gltask;
229 postAndWait(task);
230}
231
John Reckd72e0a32014-05-29 18:56:11 -0700232CREATE_BRIDGE1(destroyLayer, Layer* layer) {
233 LayerRenderer::destroyLayer(args->layer);
234 return NULL;
235}
236
237static void enqueueDestroyLayer(Layer* layer) {
238 SETUP_TASK(destroyLayer);
239 args->layer = layer;
240 RenderThread::getInstance().queue(task);
241}
242
John Reck1949e792014-04-08 15:18:56 -0700243CREATE_BRIDGE3(createDisplayListLayer, CanvasContext* context, int width, int height) {
244 Layer* layer = args->context->createRenderLayer(args->width, args->height);
John Reck19b6bcf2014-02-14 20:03:38 -0800245 if (!layer) return 0;
John Reckd72e0a32014-05-29 18:56:11 -0700246 return new DeferredLayerUpdater(layer, enqueueDestroyLayer);
John Reck19b6bcf2014-02-14 20:03:38 -0800247}
248
249DeferredLayerUpdater* RenderProxy::createDisplayListLayer(int width, int height) {
250 SETUP_TASK(createDisplayListLayer);
251 args->width = width;
252 args->height = height;
John Reck1949e792014-04-08 15:18:56 -0700253 args->context = mContext;
John Reck19b6bcf2014-02-14 20:03:38 -0800254 void* retval = postAndWait(task);
255 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval);
John Reck19b6bcf2014-02-14 20:03:38 -0800256 return layer;
257}
258
John Reck1949e792014-04-08 15:18:56 -0700259CREATE_BRIDGE1(createTextureLayer, CanvasContext* context) {
260 Layer* layer = args->context->createTextureLayer();
John Reck19b6bcf2014-02-14 20:03:38 -0800261 if (!layer) return 0;
John Reckd72e0a32014-05-29 18:56:11 -0700262 return new DeferredLayerUpdater(layer, enqueueDestroyLayer);
John Reck19b6bcf2014-02-14 20:03:38 -0800263}
264
265DeferredLayerUpdater* RenderProxy::createTextureLayer() {
266 SETUP_TASK(createTextureLayer);
John Reck1949e792014-04-08 15:18:56 -0700267 args->context = mContext;
John Reck19b6bcf2014-02-14 20:03:38 -0800268 void* retval = postAndWait(task);
269 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval);
John Reck19b6bcf2014-02-14 20:03:38 -0800270 return layer;
271}
272
John Reck19b6bcf2014-02-14 20:03:38 -0800273CREATE_BRIDGE3(copyLayerInto, CanvasContext* context, DeferredLayerUpdater* layer,
274 SkBitmap* bitmap) {
275 bool success = args->context->copyLayerInto(args->layer, args->bitmap);
276 return (void*) success;
277}
278
279bool RenderProxy::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
280 SETUP_TASK(copyLayerInto);
281 args->context = mContext;
282 args->layer = layer;
283 args->bitmap = bitmap;
284 return (bool) postAndWait(task);
285}
286
John Reckd72e0a32014-05-29 18:56:11 -0700287void RenderProxy::pushLayerUpdate(DeferredLayerUpdater* layer) {
288 mDrawFrameTask.pushLayerUpdate(layer);
289}
290
291void RenderProxy::cancelLayerUpdate(DeferredLayerUpdater* layer) {
292 mDrawFrameTask.removeLayerUpdate(layer);
John Reck19b6bcf2014-02-14 20:03:38 -0800293}
294
John Recke1628b72014-05-23 15:11:19 -0700295CREATE_BRIDGE2(flushCaches, CanvasContext* context, Caches::FlushMode flushMode) {
296 args->context->flushCaches(args->flushMode);
297 return NULL;
298}
299
300void RenderProxy::flushCaches(Caches::FlushMode flushMode) {
301 SETUP_TASK(flushCaches);
302 args->context = mContext;
303 args->flushMode = flushMode;
304 post(task);
305}
306
John Reck28ad7b52014-04-07 16:59:25 -0700307CREATE_BRIDGE0(fence) {
308 // Intentionally empty
309 return NULL;
310}
311
312void RenderProxy::fence() {
313 SETUP_TASK(fence);
314 postAndWait(task);
315}
316
John Recka5dda642014-05-22 15:43:54 -0700317CREATE_BRIDGE1(notifyFramePending, CanvasContext* context) {
318 args->context->notifyFramePending();
319 return NULL;
320}
321
322void RenderProxy::notifyFramePending() {
323 SETUP_TASK(notifyFramePending);
324 args->context = mContext;
325 mRenderThread.queueAtFront(task);
326}
327
John Reckfe5e7b72014-05-23 17:42:28 -0700328CREATE_BRIDGE2(dumpProfileInfo, CanvasContext* context, int fd) {
329 args->context->profiler().dumpData(args->fd);
330 return NULL;
331}
332
333void RenderProxy::dumpProfileInfo(int fd) {
334 SETUP_TASK(dumpProfileInfo);
335 args->context = mContext;
336 args->fd = fd;
337 postAndWait(task);
338}
339
John Reck4f02bf42014-01-03 18:09:17 -0800340void RenderProxy::post(RenderTask* task) {
341 mRenderThread.queue(task);
342}
343
344void* RenderProxy::postAndWait(MethodInvokeRenderTask* task) {
345 void* retval;
346 task->setReturnPtr(&retval);
347 SignalingRenderTask syncTask(task, &mSyncMutex, &mSyncCondition);
348 AutoMutex _lock(mSyncMutex);
349 mRenderThread.queue(&syncTask);
350 mSyncCondition.wait(mSyncMutex);
351 return retval;
352}
353
354} /* namespace renderthread */
355} /* namespace uirenderer */
356} /* namespace android */