blob: 405ce242657d8b0665644599370a43d869f55062 [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
John Reck4f02bf42014-01-03 18:09:17 -080017#include "RenderProxy.h"
18
19#include "CanvasContext.h"
20#include "RenderTask.h"
21#include "RenderThread.h"
22
John Reck19b6bcf2014-02-14 20:03:38 -080023#include "../DeferredLayerUpdater.h"
John Reck4f02bf42014-01-03 18:09:17 -080024#include "../DisplayList.h"
John Reck19b6bcf2014-02-14 20:03:38 -080025#include "../LayerRenderer.h"
John Reck4f02bf42014-01-03 18:09:17 -080026#include "../Rect.h"
27
28namespace android {
29namespace uirenderer {
30namespace renderthread {
31
32#define ARGS(method) method ## Args
33
John Reck19b6bcf2014-02-14 20:03:38 -080034#define CREATE_BRIDGE0(name) CREATE_BRIDGE(name,,,,,,,,)
John Reck4f02bf42014-01-03 18:09:17 -080035#define CREATE_BRIDGE1(name, a1) CREATE_BRIDGE(name, a1,,,,,,,)
36#define CREATE_BRIDGE2(name, a1, a2) CREATE_BRIDGE(name, a1,a2,,,,,,)
37#define CREATE_BRIDGE3(name, a1, a2, a3) CREATE_BRIDGE(name, a1,a2,a3,,,,,)
38#define CREATE_BRIDGE4(name, a1, a2, a3, a4) CREATE_BRIDGE(name, a1,a2,a3,a4,,,,)
Chris Craik797b95b2014-05-20 18:10:25 -070039#define CREATE_BRIDGE5(name, a1, a2, a3, a4, a5) CREATE_BRIDGE(name, a1,a2,a3,a4,a5,,,)
Chris Craik058fc642014-07-23 18:19:28 -070040#define CREATE_BRIDGE6(name, a1, a2, a3, a4, a5, a6) CREATE_BRIDGE(name, a1,a2,a3,a4,a5,a6,,)
41#define CREATE_BRIDGE7(name, a1, a2, a3, a4, a5, a6, a7) CREATE_BRIDGE(name, a1,a2,a3,a4,a5,a6,a7,)
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)), \
Mark Salyzyn546f3532014-06-10 12:29:14 -070050 "METHOD_INVOKE_PAYLOAD_SIZE %zu is smaller than sizeof(" #method "Args) %zu", \
John Reck4f02bf42014-01-03 18:09:17 -080051 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 Reck3b202512014-06-23 13:13:08 -070055CREATE_BRIDGE3(createContext, RenderThread* thread, bool translucent, RenderNode* rootRenderNode) {
56 return new CanvasContext(*args->thread, 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 Reck3b202512014-06-23 13:13:08 -070065 args->thread = &mRenderThread;
John Reck4f02bf42014-01-03 18:09:17 -080066 mContext = (CanvasContext*) postAndWait(task);
John Reck18f16e62014-05-02 16:46:41 -070067 mDrawFrameTask.setContext(&mRenderThread, mContext);
John Reck4f02bf42014-01-03 18:09:17 -080068}
69
70RenderProxy::~RenderProxy() {
71 destroyContext();
72}
73
74CREATE_BRIDGE1(destroyContext, CanvasContext* context) {
75 delete args->context;
76 return NULL;
77}
78
79void RenderProxy::destroyContext() {
80 if (mContext) {
81 SETUP_TASK(destroyContext);
82 args->context = mContext;
83 mContext = 0;
John Reck18f16e62014-05-02 16:46:41 -070084 mDrawFrameTask.setContext(NULL, NULL);
John Reck668f0e32014-03-26 15:10:40 -070085 // 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
John Reck18f16e62014-05-02 16:46:41 -070091CREATE_BRIDGE2(setFrameInterval, RenderThread* thread, nsecs_t frameIntervalNanos) {
92 args->thread->timeLord().setFrameInterval(args->frameIntervalNanos);
93 return NULL;
94}
95
96void RenderProxy::setFrameInterval(nsecs_t frameIntervalNanos) {
97 SETUP_TASK(setFrameInterval);
98 args->thread = &mRenderThread;
99 args->frameIntervalNanos = frameIntervalNanos;
100 post(task);
101}
102
John Reckfe5e7b72014-05-23 17:42:28 -0700103CREATE_BRIDGE1(loadSystemProperties, CanvasContext* context) {
John Recke4280ba2014-05-05 16:39:37 -0700104 bool needsRedraw = false;
105 if (Caches::hasInstance()) {
106 needsRedraw = Caches::getInstance().initProperties();
107 }
John Reckfe5e7b72014-05-23 17:42:28 -0700108 if (args->context->profiler().loadSystemProperties()) {
109 needsRedraw = true;
110 }
John Recke4280ba2014-05-05 16:39:37 -0700111 return (void*) needsRedraw;
112}
113
114bool RenderProxy::loadSystemProperties() {
115 SETUP_TASK(loadSystemProperties);
John Reckfe5e7b72014-05-23 17:42:28 -0700116 args->context = mContext;
John Recke4280ba2014-05-05 16:39:37 -0700117 return (bool) postAndWait(task);
118}
119
John Recka5dda642014-05-22 15:43:54 -0700120CREATE_BRIDGE2(initialize, CanvasContext* context, ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -0800121 return (void*) args->context->initialize(args->window);
122}
123
John Reckf7d9c1d2014-04-09 10:01:03 -0700124bool RenderProxy::initialize(const sp<ANativeWindow>& window) {
John Reck4f02bf42014-01-03 18:09:17 -0800125 SETUP_TASK(initialize);
126 args->context = mContext;
John Reckf7d9c1d2014-04-09 10:01:03 -0700127 args->window = window.get();
John Reck4f02bf42014-01-03 18:09:17 -0800128 return (bool) postAndWait(task);
129}
130
John Recka5dda642014-05-22 15:43:54 -0700131CREATE_BRIDGE2(updateSurface, CanvasContext* context, ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -0800132 args->context->updateSurface(args->window);
133 return NULL;
134}
135
John Reckf7d9c1d2014-04-09 10:01:03 -0700136void RenderProxy::updateSurface(const sp<ANativeWindow>& window) {
John Reck4f02bf42014-01-03 18:09:17 -0800137 SETUP_TASK(updateSurface);
138 args->context = mContext;
John Reckf7d9c1d2014-04-09 10:01:03 -0700139 args->window = window.get();
140 postAndWait(task);
141}
142
John Recka5dda642014-05-22 15:43:54 -0700143CREATE_BRIDGE2(pauseSurface, CanvasContext* context, ANativeWindow* window) {
John Reckf7d9c1d2014-04-09 10:01:03 -0700144 args->context->pauseSurface(args->window);
145 return NULL;
146}
147
148void RenderProxy::pauseSurface(const sp<ANativeWindow>& window) {
149 SETUP_TASK(pauseSurface);
150 args->context = mContext;
151 args->window = window.get();
152 postAndWait(task);
John Reck4f02bf42014-01-03 18:09:17 -0800153}
154
Chris Craik058fc642014-07-23 18:19:28 -0700155CREATE_BRIDGE7(setup, CanvasContext* context, int width, int height,
156 Vector3 lightCenter, float lightRadius,
157 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
158 args->context->setup(args->width, args->height, args->lightCenter, args->lightRadius,
159 args->ambientShadowAlpha, args->spotShadowAlpha);
John Reck4f02bf42014-01-03 18:09:17 -0800160 return NULL;
161}
162
Chris Craik058fc642014-07-23 18:19:28 -0700163void RenderProxy::setup(int width, int height, const Vector3& lightCenter, float lightRadius,
164 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
John Reck4f02bf42014-01-03 18:09:17 -0800165 SETUP_TASK(setup);
166 args->context = mContext;
167 args->width = width;
168 args->height = height;
Chris Craik797b95b2014-05-20 18:10:25 -0700169 args->lightCenter = lightCenter;
170 args->lightRadius = lightRadius;
Chris Craik058fc642014-07-23 18:19:28 -0700171 args->ambientShadowAlpha = ambientShadowAlpha;
172 args->spotShadowAlpha = spotShadowAlpha;
John Reck4f02bf42014-01-03 18:09:17 -0800173 post(task);
174}
175
John Reck63a06672014-05-07 13:45:54 -0700176CREATE_BRIDGE2(setOpaque, CanvasContext* context, bool opaque) {
177 args->context->setOpaque(args->opaque);
178 return NULL;
179}
180
181void RenderProxy::setOpaque(bool opaque) {
182 SETUP_TASK(setOpaque);
183 args->context = mContext;
184 args->opaque = opaque;
185 post(task);
186}
187
John Reckfe5e7b72014-05-23 17:42:28 -0700188int RenderProxy::syncAndDrawFrame(nsecs_t frameTimeNanos, nsecs_t recordDurationNanos,
John Recke4267ea2014-06-03 15:53:15 -0700189 float density) {
John Reckfe5e7b72014-05-23 17:42:28 -0700190 mDrawFrameTask.setDensity(density);
191 return mDrawFrameTask.drawFrame(frameTimeNanos, recordDurationNanos);
John Reck4f02bf42014-01-03 18:09:17 -0800192}
193
John Reckfae904d2014-04-14 11:01:57 -0700194CREATE_BRIDGE1(destroyCanvasAndSurface, CanvasContext* context) {
195 args->context->destroyCanvasAndSurface();
John Reck4f02bf42014-01-03 18:09:17 -0800196 return NULL;
197}
198
John Reckfae904d2014-04-14 11:01:57 -0700199void RenderProxy::destroyCanvasAndSurface() {
200 SETUP_TASK(destroyCanvasAndSurface);
John Reck4f02bf42014-01-03 18:09:17 -0800201 args->context = mContext;
John Reckfae904d2014-04-14 11:01:57 -0700202 // destroyCanvasAndSurface() needs a fence as when it returns the
203 // underlying BufferQueue is going to be released from under
204 // the render thread.
205 postAndWait(task);
John Reck4f02bf42014-01-03 18:09:17 -0800206}
207
John Reck3b202512014-06-23 13:13:08 -0700208CREATE_BRIDGE2(invokeFunctor, RenderThread* thread, Functor* functor) {
209 CanvasContext::invokeFunctor(*args->thread, args->functor);
John Reck0d1f6342014-03-28 20:30:27 -0700210 return NULL;
211}
212
213void RenderProxy::invokeFunctor(Functor* functor, bool waitForCompletion) {
John Reckd3d8daf2014-04-10 15:00:13 -0700214 ATRACE_CALL();
John Reck3b202512014-06-23 13:13:08 -0700215 RenderThread& thread = RenderThread::getInstance();
John Reck0d1f6342014-03-28 20:30:27 -0700216 SETUP_TASK(invokeFunctor);
John Reck3b202512014-06-23 13:13:08 -0700217 args->thread = &thread;
John Reck0d1f6342014-03-28 20:30:27 -0700218 args->functor = functor;
219 if (waitForCompletion) {
John Reck3b202512014-06-23 13:13:08 -0700220 // waitForCompletion = true is expected to be fairly rare and only
221 // happen in destruction. Thus it should be fine to temporarily
222 // create a Mutex
223 Mutex mutex;
224 Condition condition;
225 SignalingRenderTask syncTask(task, &mutex, &condition);
226 AutoMutex _lock(mutex);
227 thread.queue(&syncTask);
228 condition.wait(mutex);
John Reck0d1f6342014-03-28 20:30:27 -0700229 } else {
John Reck3b202512014-06-23 13:13:08 -0700230 thread.queue(task);
John Reck0d1f6342014-03-28 20:30:27 -0700231 }
232}
233
John Reckfc53ef272014-02-11 10:40:25 -0800234CREATE_BRIDGE2(runWithGlContext, CanvasContext* context, RenderTask* task) {
235 args->context->runWithGlContext(args->task);
236 return NULL;
237}
238
239void RenderProxy::runWithGlContext(RenderTask* gltask) {
240 SETUP_TASK(runWithGlContext);
241 args->context = mContext;
242 args->task = gltask;
243 postAndWait(task);
244}
245
John Reckd72e0a32014-05-29 18:56:11 -0700246CREATE_BRIDGE1(destroyLayer, Layer* layer) {
247 LayerRenderer::destroyLayer(args->layer);
248 return NULL;
249}
250
John Reck3b202512014-06-23 13:13:08 -0700251void RenderProxy::enqueueDestroyLayer(Layer* layer) {
John Reckd72e0a32014-05-29 18:56:11 -0700252 SETUP_TASK(destroyLayer);
253 args->layer = layer;
254 RenderThread::getInstance().queue(task);
255}
256
John Reck1949e792014-04-08 15:18:56 -0700257CREATE_BRIDGE3(createDisplayListLayer, CanvasContext* context, int width, int height) {
258 Layer* layer = args->context->createRenderLayer(args->width, args->height);
John Reck19b6bcf2014-02-14 20:03:38 -0800259 if (!layer) return 0;
John Reck3b202512014-06-23 13:13:08 -0700260 return new DeferredLayerUpdater(layer, RenderProxy::enqueueDestroyLayer);
John Reck19b6bcf2014-02-14 20:03:38 -0800261}
262
263DeferredLayerUpdater* RenderProxy::createDisplayListLayer(int width, int height) {
264 SETUP_TASK(createDisplayListLayer);
265 args->width = width;
266 args->height = height;
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 Reck1949e792014-04-08 15:18:56 -0700273CREATE_BRIDGE1(createTextureLayer, CanvasContext* context) {
274 Layer* layer = args->context->createTextureLayer();
John Reck19b6bcf2014-02-14 20:03:38 -0800275 if (!layer) return 0;
John Reck3b202512014-06-23 13:13:08 -0700276 return new DeferredLayerUpdater(layer, RenderProxy::enqueueDestroyLayer);
John Reck19b6bcf2014-02-14 20:03:38 -0800277}
278
279DeferredLayerUpdater* RenderProxy::createTextureLayer() {
280 SETUP_TASK(createTextureLayer);
John Reck1949e792014-04-08 15:18:56 -0700281 args->context = mContext;
John Reck19b6bcf2014-02-14 20:03:38 -0800282 void* retval = postAndWait(task);
283 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval);
John Reck19b6bcf2014-02-14 20:03:38 -0800284 return layer;
285}
286
John Reck3e824952014-08-20 10:08:39 -0700287CREATE_BRIDGE2(buildLayer, CanvasContext* context, RenderNode* node) {
288 args->context->buildLayer(args->node);
289 return NULL;
290}
291
292void RenderProxy::buildLayer(RenderNode* node) {
293 SETUP_TASK(buildLayer);
294 args->context = mContext;
295 args->node = node;
296 postAndWait(task);
297}
298
John Reck19b6bcf2014-02-14 20:03:38 -0800299CREATE_BRIDGE3(copyLayerInto, CanvasContext* context, DeferredLayerUpdater* layer,
300 SkBitmap* bitmap) {
301 bool success = args->context->copyLayerInto(args->layer, args->bitmap);
302 return (void*) success;
303}
304
305bool RenderProxy::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
306 SETUP_TASK(copyLayerInto);
307 args->context = mContext;
308 args->layer = layer;
309 args->bitmap = bitmap;
310 return (bool) postAndWait(task);
311}
312
John Reckd72e0a32014-05-29 18:56:11 -0700313void RenderProxy::pushLayerUpdate(DeferredLayerUpdater* layer) {
314 mDrawFrameTask.pushLayerUpdate(layer);
315}
316
317void RenderProxy::cancelLayerUpdate(DeferredLayerUpdater* layer) {
318 mDrawFrameTask.removeLayerUpdate(layer);
John Reck19b6bcf2014-02-14 20:03:38 -0800319}
320
John Reck918ad522014-06-27 14:45:25 -0700321CREATE_BRIDGE1(detachSurfaceTexture, DeferredLayerUpdater* layer) {
322 args->layer->detachSurfaceTexture();
323 return NULL;
324}
325
326void RenderProxy::detachSurfaceTexture(DeferredLayerUpdater* layer) {
327 SETUP_TASK(detachSurfaceTexture);
328 args->layer = layer;
329 postAndWait(task);
330}
331
John Reckf47a5942014-06-30 16:20:04 -0700332CREATE_BRIDGE1(destroyHardwareResources, CanvasContext* context) {
333 args->context->destroyHardwareResources();
John Recke1628b72014-05-23 15:11:19 -0700334 return NULL;
335}
336
John Reckf47a5942014-06-30 16:20:04 -0700337void RenderProxy::destroyHardwareResources() {
338 SETUP_TASK(destroyHardwareResources);
John Recke1628b72014-05-23 15:11:19 -0700339 args->context = mContext;
John Recke1628b72014-05-23 15:11:19 -0700340 post(task);
341}
342
John Reckf47a5942014-06-30 16:20:04 -0700343CREATE_BRIDGE2(timMemory, RenderThread* thread, int level) {
344 CanvasContext::trimMemory(*args->thread, args->level);
345 return NULL;
346}
347
348void RenderProxy::trimMemory(int level) {
John Reckcd3a22c2014-08-06 13:33:59 -0700349 // Avoid creating a RenderThread to do a trimMemory.
350 if (RenderThread::hasInstance()) {
351 RenderThread& thread = RenderThread::getInstance();
352 SETUP_TASK(timMemory);
353 args->thread = &thread;
354 args->level = level;
355 thread.queue(task);
356 }
John Reckf47a5942014-06-30 16:20:04 -0700357}
358
John Reck28ad7b52014-04-07 16:59:25 -0700359CREATE_BRIDGE0(fence) {
360 // Intentionally empty
361 return NULL;
362}
363
364void RenderProxy::fence() {
365 SETUP_TASK(fence);
366 postAndWait(task);
367}
368
John Reckf47a5942014-06-30 16:20:04 -0700369CREATE_BRIDGE1(stopDrawing, CanvasContext* context) {
370 args->context->stopDrawing();
371 return NULL;
372}
373
374void RenderProxy::stopDrawing() {
375 SETUP_TASK(stopDrawing);
376 args->context = mContext;
377 postAndWait(task);
378}
379
John Recka5dda642014-05-22 15:43:54 -0700380CREATE_BRIDGE1(notifyFramePending, CanvasContext* context) {
381 args->context->notifyFramePending();
382 return NULL;
383}
384
385void RenderProxy::notifyFramePending() {
386 SETUP_TASK(notifyFramePending);
387 args->context = mContext;
388 mRenderThread.queueAtFront(task);
389}
390
John Reckfe5e7b72014-05-23 17:42:28 -0700391CREATE_BRIDGE2(dumpProfileInfo, CanvasContext* context, int fd) {
392 args->context->profiler().dumpData(args->fd);
393 return NULL;
394}
395
396void RenderProxy::dumpProfileInfo(int fd) {
397 SETUP_TASK(dumpProfileInfo);
398 args->context = mContext;
399 args->fd = fd;
400 postAndWait(task);
401}
402
John Reck3b202512014-06-23 13:13:08 -0700403CREATE_BRIDGE4(setTextureAtlas, RenderThread* thread, GraphicBuffer* buffer, int64_t* map, size_t size) {
404 CanvasContext::setTextureAtlas(*args->thread, args->buffer, args->map, args->size);
405 args->buffer->decStrong(0);
406 return NULL;
407}
408
409void RenderProxy::setTextureAtlas(const sp<GraphicBuffer>& buffer, int64_t* map, size_t size) {
410 SETUP_TASK(setTextureAtlas);
411 args->thread = &mRenderThread;
412 args->buffer = buffer.get();
413 args->buffer->incStrong(0);
414 args->map = map;
415 args->size = size;
416 post(task);
417}
418
John Reck4f02bf42014-01-03 18:09:17 -0800419void RenderProxy::post(RenderTask* task) {
420 mRenderThread.queue(task);
421}
422
423void* RenderProxy::postAndWait(MethodInvokeRenderTask* task) {
424 void* retval;
425 task->setReturnPtr(&retval);
426 SignalingRenderTask syncTask(task, &mSyncMutex, &mSyncCondition);
427 AutoMutex _lock(mSyncMutex);
Chris Craik738ec3a2014-07-25 18:25:02 +0000428 mRenderThread.queue(&syncTask);
429 mSyncCondition.wait(mSyncMutex);
John Reck4f02bf42014-01-03 18:09:17 -0800430 return retval;
431}
432
433} /* namespace renderthread */
434} /* namespace uirenderer */
435} /* namespace android */