blob: 986e80805a004c890959fd07b33f989b4f65c34e [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,,,)
Chris Craik058fc642014-07-23 18:19:28 -070042#define CREATE_BRIDGE6(name, a1, a2, a3, a4, a5, a6) CREATE_BRIDGE(name, a1,a2,a3,a4,a5,a6,,)
43#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 -080044#define CREATE_BRIDGE(name, a1, a2, a3, a4, a5, a6, a7, a8) \
45 typedef struct { \
46 a1; a2; a3; a4; a5; a6; a7; a8; \
47 } ARGS(name); \
48 static void* Bridge_ ## name(ARGS(name)* args)
49
50#define SETUP_TASK(method) \
51 LOG_ALWAYS_FATAL_IF( METHOD_INVOKE_PAYLOAD_SIZE < sizeof(ARGS(method)), \
Mark Salyzyn546f3532014-06-10 12:29:14 -070052 "METHOD_INVOKE_PAYLOAD_SIZE %zu is smaller than sizeof(" #method "Args) %zu", \
John Reck4f02bf42014-01-03 18:09:17 -080053 METHOD_INVOKE_PAYLOAD_SIZE, sizeof(ARGS(method))); \
John Recke2c45522014-04-07 17:31:44 -070054 MethodInvokeRenderTask* task = new MethodInvokeRenderTask((RunnableMethod) Bridge_ ## method); \
John Reck4f02bf42014-01-03 18:09:17 -080055 ARGS(method) *args = (ARGS(method) *) task->payload()
56
John Reck3b202512014-06-23 13:13:08 -070057CREATE_BRIDGE3(createContext, RenderThread* thread, bool translucent, RenderNode* rootRenderNode) {
58 return new CanvasContext(*args->thread, args->translucent, args->rootRenderNode);
John Reck4f02bf42014-01-03 18:09:17 -080059}
60
John Recke45b1fd2014-04-15 09:50:16 -070061RenderProxy::RenderProxy(bool translucent, RenderNode* rootRenderNode)
John Reck4f02bf42014-01-03 18:09:17 -080062 : mRenderThread(RenderThread::getInstance())
63 , mContext(0) {
64 SETUP_TASK(createContext);
65 args->translucent = translucent;
John Recke45b1fd2014-04-15 09:50:16 -070066 args->rootRenderNode = rootRenderNode;
John Reck3b202512014-06-23 13:13:08 -070067 args->thread = &mRenderThread;
John Reck4f02bf42014-01-03 18:09:17 -080068 mContext = (CanvasContext*) postAndWait(task);
John Reck18f16e62014-05-02 16:46:41 -070069 mDrawFrameTask.setContext(&mRenderThread, mContext);
John Reck4f02bf42014-01-03 18:09:17 -080070}
71
72RenderProxy::~RenderProxy() {
73 destroyContext();
74}
75
76CREATE_BRIDGE1(destroyContext, CanvasContext* context) {
77 delete args->context;
78 return NULL;
79}
80
81void RenderProxy::destroyContext() {
82 if (mContext) {
83 SETUP_TASK(destroyContext);
84 args->context = mContext;
85 mContext = 0;
John Reck18f16e62014-05-02 16:46:41 -070086 mDrawFrameTask.setContext(NULL, NULL);
John Reck668f0e32014-03-26 15:10:40 -070087 // This is also a fence as we need to be certain that there are no
88 // outstanding mDrawFrame tasks posted before it is destroyed
89 postAndWait(task);
John Reck4f02bf42014-01-03 18:09:17 -080090 }
91}
92
John Reck18f16e62014-05-02 16:46:41 -070093CREATE_BRIDGE2(setFrameInterval, RenderThread* thread, nsecs_t frameIntervalNanos) {
94 args->thread->timeLord().setFrameInterval(args->frameIntervalNanos);
95 return NULL;
96}
97
98void RenderProxy::setFrameInterval(nsecs_t frameIntervalNanos) {
99 SETUP_TASK(setFrameInterval);
100 args->thread = &mRenderThread;
101 args->frameIntervalNanos = frameIntervalNanos;
102 post(task);
103}
104
John Reckfe5e7b72014-05-23 17:42:28 -0700105CREATE_BRIDGE1(loadSystemProperties, CanvasContext* context) {
John Recke4280ba2014-05-05 16:39:37 -0700106 bool needsRedraw = false;
107 if (Caches::hasInstance()) {
108 needsRedraw = Caches::getInstance().initProperties();
109 }
John Reckfe5e7b72014-05-23 17:42:28 -0700110 if (args->context->profiler().loadSystemProperties()) {
111 needsRedraw = true;
112 }
John Recke4280ba2014-05-05 16:39:37 -0700113 return (void*) needsRedraw;
114}
115
116bool RenderProxy::loadSystemProperties() {
117 SETUP_TASK(loadSystemProperties);
John Reckfe5e7b72014-05-23 17:42:28 -0700118 args->context = mContext;
John Recke4280ba2014-05-05 16:39:37 -0700119 return (bool) postAndWait(task);
120}
121
John Recka5dda642014-05-22 15:43:54 -0700122CREATE_BRIDGE2(initialize, CanvasContext* context, ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -0800123 return (void*) args->context->initialize(args->window);
124}
125
John Reckf7d9c1d2014-04-09 10:01:03 -0700126bool RenderProxy::initialize(const sp<ANativeWindow>& window) {
John Reck4f02bf42014-01-03 18:09:17 -0800127 SETUP_TASK(initialize);
128 args->context = mContext;
John Reckf7d9c1d2014-04-09 10:01:03 -0700129 args->window = window.get();
John Reck4f02bf42014-01-03 18:09:17 -0800130 return (bool) postAndWait(task);
131}
132
John Recka5dda642014-05-22 15:43:54 -0700133CREATE_BRIDGE2(updateSurface, CanvasContext* context, ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -0800134 args->context->updateSurface(args->window);
135 return NULL;
136}
137
John Reckf7d9c1d2014-04-09 10:01:03 -0700138void RenderProxy::updateSurface(const sp<ANativeWindow>& window) {
John Reck4f02bf42014-01-03 18:09:17 -0800139 SETUP_TASK(updateSurface);
140 args->context = mContext;
John Reckf7d9c1d2014-04-09 10:01:03 -0700141 args->window = window.get();
142 postAndWait(task);
143}
144
John Recka5dda642014-05-22 15:43:54 -0700145CREATE_BRIDGE2(pauseSurface, CanvasContext* context, ANativeWindow* window) {
John Reckf7d9c1d2014-04-09 10:01:03 -0700146 args->context->pauseSurface(args->window);
147 return NULL;
148}
149
150void RenderProxy::pauseSurface(const sp<ANativeWindow>& window) {
151 SETUP_TASK(pauseSurface);
152 args->context = mContext;
153 args->window = window.get();
154 postAndWait(task);
John Reck4f02bf42014-01-03 18:09:17 -0800155}
156
Chris Craik058fc642014-07-23 18:19:28 -0700157CREATE_BRIDGE7(setup, CanvasContext* context, int width, int height,
158 Vector3 lightCenter, float lightRadius,
159 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
160 args->context->setup(args->width, args->height, args->lightCenter, args->lightRadius,
161 args->ambientShadowAlpha, args->spotShadowAlpha);
John Reck4f02bf42014-01-03 18:09:17 -0800162 return NULL;
163}
164
Chris Craik058fc642014-07-23 18:19:28 -0700165void RenderProxy::setup(int width, int height, const Vector3& lightCenter, float lightRadius,
166 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
John Reck4f02bf42014-01-03 18:09:17 -0800167 SETUP_TASK(setup);
168 args->context = mContext;
169 args->width = width;
170 args->height = height;
Chris Craik797b95b2014-05-20 18:10:25 -0700171 args->lightCenter = lightCenter;
172 args->lightRadius = lightRadius;
Chris Craik058fc642014-07-23 18:19:28 -0700173 args->ambientShadowAlpha = ambientShadowAlpha;
174 args->spotShadowAlpha = spotShadowAlpha;
John Reck4f02bf42014-01-03 18:09:17 -0800175 post(task);
176}
177
John Reck63a06672014-05-07 13:45:54 -0700178CREATE_BRIDGE2(setOpaque, CanvasContext* context, bool opaque) {
179 args->context->setOpaque(args->opaque);
180 return NULL;
181}
182
183void RenderProxy::setOpaque(bool opaque) {
184 SETUP_TASK(setOpaque);
185 args->context = mContext;
186 args->opaque = opaque;
187 post(task);
188}
189
John Reckfe5e7b72014-05-23 17:42:28 -0700190int RenderProxy::syncAndDrawFrame(nsecs_t frameTimeNanos, nsecs_t recordDurationNanos,
John Recke4267ea2014-06-03 15:53:15 -0700191 float density) {
John Reckfe5e7b72014-05-23 17:42:28 -0700192 mDrawFrameTask.setDensity(density);
193 return mDrawFrameTask.drawFrame(frameTimeNanos, recordDurationNanos);
John Reck4f02bf42014-01-03 18:09:17 -0800194}
195
John Reckfae904d2014-04-14 11:01:57 -0700196CREATE_BRIDGE1(destroyCanvasAndSurface, CanvasContext* context) {
197 args->context->destroyCanvasAndSurface();
John Reck4f02bf42014-01-03 18:09:17 -0800198 return NULL;
199}
200
John Reckfae904d2014-04-14 11:01:57 -0700201void RenderProxy::destroyCanvasAndSurface() {
202 SETUP_TASK(destroyCanvasAndSurface);
John Reck4f02bf42014-01-03 18:09:17 -0800203 args->context = mContext;
John Reckfae904d2014-04-14 11:01:57 -0700204 // destroyCanvasAndSurface() needs a fence as when it returns the
205 // underlying BufferQueue is going to be released from under
206 // the render thread.
207 postAndWait(task);
John Reck4f02bf42014-01-03 18:09:17 -0800208}
209
John Reck3b202512014-06-23 13:13:08 -0700210CREATE_BRIDGE2(invokeFunctor, RenderThread* thread, Functor* functor) {
211 CanvasContext::invokeFunctor(*args->thread, args->functor);
John Reck0d1f6342014-03-28 20:30:27 -0700212 return NULL;
213}
214
215void RenderProxy::invokeFunctor(Functor* functor, bool waitForCompletion) {
John Reckd3d8daf2014-04-10 15:00:13 -0700216 ATRACE_CALL();
John Reck3b202512014-06-23 13:13:08 -0700217 RenderThread& thread = RenderThread::getInstance();
John Reck0d1f6342014-03-28 20:30:27 -0700218 SETUP_TASK(invokeFunctor);
John Reck3b202512014-06-23 13:13:08 -0700219 args->thread = &thread;
John Reck0d1f6342014-03-28 20:30:27 -0700220 args->functor = functor;
221 if (waitForCompletion) {
John Reck3b202512014-06-23 13:13:08 -0700222 // waitForCompletion = true is expected to be fairly rare and only
223 // happen in destruction. Thus it should be fine to temporarily
224 // create a Mutex
225 Mutex mutex;
226 Condition condition;
227 SignalingRenderTask syncTask(task, &mutex, &condition);
228 AutoMutex _lock(mutex);
229 thread.queue(&syncTask);
230 condition.wait(mutex);
John Reck0d1f6342014-03-28 20:30:27 -0700231 } else {
John Reck3b202512014-06-23 13:13:08 -0700232 thread.queue(task);
John Reck0d1f6342014-03-28 20:30:27 -0700233 }
234}
235
John Reckfc53ef272014-02-11 10:40:25 -0800236CREATE_BRIDGE2(runWithGlContext, CanvasContext* context, RenderTask* task) {
237 args->context->runWithGlContext(args->task);
238 return NULL;
239}
240
241void RenderProxy::runWithGlContext(RenderTask* gltask) {
242 SETUP_TASK(runWithGlContext);
243 args->context = mContext;
244 args->task = gltask;
245 postAndWait(task);
246}
247
John Reckd72e0a32014-05-29 18:56:11 -0700248CREATE_BRIDGE1(destroyLayer, Layer* layer) {
249 LayerRenderer::destroyLayer(args->layer);
250 return NULL;
251}
252
John Reck3b202512014-06-23 13:13:08 -0700253void RenderProxy::enqueueDestroyLayer(Layer* layer) {
John Reckd72e0a32014-05-29 18:56:11 -0700254 SETUP_TASK(destroyLayer);
255 args->layer = layer;
256 RenderThread::getInstance().queue(task);
257}
258
John Reck1949e792014-04-08 15:18:56 -0700259CREATE_BRIDGE3(createDisplayListLayer, CanvasContext* context, int width, int height) {
260 Layer* layer = args->context->createRenderLayer(args->width, args->height);
John Reck19b6bcf2014-02-14 20:03:38 -0800261 if (!layer) return 0;
John Reck3b202512014-06-23 13:13:08 -0700262 return new DeferredLayerUpdater(layer, RenderProxy::enqueueDestroyLayer);
John Reck19b6bcf2014-02-14 20:03:38 -0800263}
264
265DeferredLayerUpdater* RenderProxy::createDisplayListLayer(int width, int height) {
266 SETUP_TASK(createDisplayListLayer);
267 args->width = width;
268 args->height = height;
John Reck1949e792014-04-08 15:18:56 -0700269 args->context = mContext;
John Reck19b6bcf2014-02-14 20:03:38 -0800270 void* retval = postAndWait(task);
271 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval);
John Reck19b6bcf2014-02-14 20:03:38 -0800272 return layer;
273}
274
John Reck1949e792014-04-08 15:18:56 -0700275CREATE_BRIDGE1(createTextureLayer, CanvasContext* context) {
276 Layer* layer = args->context->createTextureLayer();
John Reck19b6bcf2014-02-14 20:03:38 -0800277 if (!layer) return 0;
John Reck3b202512014-06-23 13:13:08 -0700278 return new DeferredLayerUpdater(layer, RenderProxy::enqueueDestroyLayer);
John Reck19b6bcf2014-02-14 20:03:38 -0800279}
280
281DeferredLayerUpdater* RenderProxy::createTextureLayer() {
282 SETUP_TASK(createTextureLayer);
John Reck1949e792014-04-08 15:18:56 -0700283 args->context = mContext;
John Reck19b6bcf2014-02-14 20:03:38 -0800284 void* retval = postAndWait(task);
285 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval);
John Reck19b6bcf2014-02-14 20:03:38 -0800286 return layer;
287}
288
John Reck19b6bcf2014-02-14 20:03:38 -0800289CREATE_BRIDGE3(copyLayerInto, CanvasContext* context, DeferredLayerUpdater* layer,
290 SkBitmap* bitmap) {
291 bool success = args->context->copyLayerInto(args->layer, args->bitmap);
292 return (void*) success;
293}
294
295bool RenderProxy::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
296 SETUP_TASK(copyLayerInto);
297 args->context = mContext;
298 args->layer = layer;
299 args->bitmap = bitmap;
300 return (bool) postAndWait(task);
301}
302
John Reckd72e0a32014-05-29 18:56:11 -0700303void RenderProxy::pushLayerUpdate(DeferredLayerUpdater* layer) {
304 mDrawFrameTask.pushLayerUpdate(layer);
305}
306
307void RenderProxy::cancelLayerUpdate(DeferredLayerUpdater* layer) {
308 mDrawFrameTask.removeLayerUpdate(layer);
John Reck19b6bcf2014-02-14 20:03:38 -0800309}
310
John Reck918ad522014-06-27 14:45:25 -0700311CREATE_BRIDGE1(detachSurfaceTexture, DeferredLayerUpdater* layer) {
312 args->layer->detachSurfaceTexture();
313 return NULL;
314}
315
316void RenderProxy::detachSurfaceTexture(DeferredLayerUpdater* layer) {
317 SETUP_TASK(detachSurfaceTexture);
318 args->layer = layer;
319 postAndWait(task);
320}
321
John Reckf47a5942014-06-30 16:20:04 -0700322CREATE_BRIDGE1(destroyHardwareResources, CanvasContext* context) {
323 args->context->destroyHardwareResources();
John Recke1628b72014-05-23 15:11:19 -0700324 return NULL;
325}
326
John Reckf47a5942014-06-30 16:20:04 -0700327void RenderProxy::destroyHardwareResources() {
328 SETUP_TASK(destroyHardwareResources);
John Recke1628b72014-05-23 15:11:19 -0700329 args->context = mContext;
John Recke1628b72014-05-23 15:11:19 -0700330 post(task);
331}
332
John Reckf47a5942014-06-30 16:20:04 -0700333CREATE_BRIDGE2(timMemory, RenderThread* thread, int level) {
334 CanvasContext::trimMemory(*args->thread, args->level);
335 return NULL;
336}
337
338void RenderProxy::trimMemory(int level) {
John Reckcd3a22c2014-08-06 13:33:59 -0700339 // Avoid creating a RenderThread to do a trimMemory.
340 if (RenderThread::hasInstance()) {
341 RenderThread& thread = RenderThread::getInstance();
342 SETUP_TASK(timMemory);
343 args->thread = &thread;
344 args->level = level;
345 thread.queue(task);
346 }
John Reckf47a5942014-06-30 16:20:04 -0700347}
348
John Reck28ad7b52014-04-07 16:59:25 -0700349CREATE_BRIDGE0(fence) {
350 // Intentionally empty
351 return NULL;
352}
353
354void RenderProxy::fence() {
355 SETUP_TASK(fence);
356 postAndWait(task);
357}
358
John Reckf47a5942014-06-30 16:20:04 -0700359CREATE_BRIDGE1(stopDrawing, CanvasContext* context) {
360 args->context->stopDrawing();
361 return NULL;
362}
363
364void RenderProxy::stopDrawing() {
365 SETUP_TASK(stopDrawing);
366 args->context = mContext;
367 postAndWait(task);
368}
369
John Recka5dda642014-05-22 15:43:54 -0700370CREATE_BRIDGE1(notifyFramePending, CanvasContext* context) {
371 args->context->notifyFramePending();
372 return NULL;
373}
374
375void RenderProxy::notifyFramePending() {
376 SETUP_TASK(notifyFramePending);
377 args->context = mContext;
378 mRenderThread.queueAtFront(task);
379}
380
John Reckfe5e7b72014-05-23 17:42:28 -0700381CREATE_BRIDGE2(dumpProfileInfo, CanvasContext* context, int fd) {
382 args->context->profiler().dumpData(args->fd);
383 return NULL;
384}
385
386void RenderProxy::dumpProfileInfo(int fd) {
387 SETUP_TASK(dumpProfileInfo);
388 args->context = mContext;
389 args->fd = fd;
390 postAndWait(task);
391}
392
John Reck3b202512014-06-23 13:13:08 -0700393CREATE_BRIDGE4(setTextureAtlas, RenderThread* thread, GraphicBuffer* buffer, int64_t* map, size_t size) {
394 CanvasContext::setTextureAtlas(*args->thread, args->buffer, args->map, args->size);
395 args->buffer->decStrong(0);
396 return NULL;
397}
398
399void RenderProxy::setTextureAtlas(const sp<GraphicBuffer>& buffer, int64_t* map, size_t size) {
400 SETUP_TASK(setTextureAtlas);
401 args->thread = &mRenderThread;
402 args->buffer = buffer.get();
403 args->buffer->incStrong(0);
404 args->map = map;
405 args->size = size;
406 post(task);
407}
408
John Reck4f02bf42014-01-03 18:09:17 -0800409void RenderProxy::post(RenderTask* task) {
410 mRenderThread.queue(task);
411}
412
413void* RenderProxy::postAndWait(MethodInvokeRenderTask* task) {
414 void* retval;
415 task->setReturnPtr(&retval);
416 SignalingRenderTask syncTask(task, &mSyncMutex, &mSyncCondition);
417 AutoMutex _lock(mSyncMutex);
Chris Craik738ec3a2014-07-25 18:25:02 +0000418 mRenderThread.queue(&syncTask);
419 mSyncCondition.wait(mSyncMutex);
John Reck4f02bf42014-01-03 18:09:17 -0800420 return retval;
421}
422
423} /* namespace renderthread */
424} /* namespace uirenderer */
425} /* namespace android */