blob: 5d55ea62af1f632470fe8ff0d981f77b70d53d58 [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 Reck119907c2014-08-14 09:02:01 -070055CREATE_BRIDGE4(createContext, RenderThread* thread, bool translucent,
56 RenderNode* rootRenderNode, IContextFactory* contextFactory) {
57 return new CanvasContext(*args->thread, args->translucent,
58 args->rootRenderNode, args->contextFactory);
John Reck4f02bf42014-01-03 18:09:17 -080059}
60
John Reck119907c2014-08-14 09:02:01 -070061RenderProxy::RenderProxy(bool translucent, RenderNode* rootRenderNode, IContextFactory* contextFactory)
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 Reck119907c2014-08-14 09:02:01 -070068 args->contextFactory = contextFactory;
John Reck4f02bf42014-01-03 18:09:17 -080069 mContext = (CanvasContext*) postAndWait(task);
John Reck18f16e62014-05-02 16:46:41 -070070 mDrawFrameTask.setContext(&mRenderThread, mContext);
John Reck4f02bf42014-01-03 18:09:17 -080071}
72
73RenderProxy::~RenderProxy() {
74 destroyContext();
75}
76
77CREATE_BRIDGE1(destroyContext, CanvasContext* context) {
78 delete args->context;
79 return NULL;
80}
81
82void RenderProxy::destroyContext() {
83 if (mContext) {
84 SETUP_TASK(destroyContext);
85 args->context = mContext;
86 mContext = 0;
John Reck18f16e62014-05-02 16:46:41 -070087 mDrawFrameTask.setContext(NULL, NULL);
John Reck668f0e32014-03-26 15:10:40 -070088 // This is also a fence as we need to be certain that there are no
89 // outstanding mDrawFrame tasks posted before it is destroyed
90 postAndWait(task);
John Reck4f02bf42014-01-03 18:09:17 -080091 }
92}
93
John Reck18f16e62014-05-02 16:46:41 -070094CREATE_BRIDGE2(setFrameInterval, RenderThread* thread, nsecs_t frameIntervalNanos) {
95 args->thread->timeLord().setFrameInterval(args->frameIntervalNanos);
96 return NULL;
97}
98
99void RenderProxy::setFrameInterval(nsecs_t frameIntervalNanos) {
100 SETUP_TASK(setFrameInterval);
101 args->thread = &mRenderThread;
102 args->frameIntervalNanos = frameIntervalNanos;
103 post(task);
104}
105
John Reck1125d1f2014-10-23 11:02:19 -0700106CREATE_BRIDGE2(setSwapBehavior, CanvasContext* context, SwapBehavior swapBehavior) {
107 args->context->setSwapBehavior(args->swapBehavior);
108 return NULL;
109}
110
111void RenderProxy::setSwapBehavior(SwapBehavior swapBehavior) {
112 SETUP_TASK(setSwapBehavior);
113 args->context = mContext;
114 args->swapBehavior = swapBehavior;
115 post(task);
116}
117
John Reckfe5e7b72014-05-23 17:42:28 -0700118CREATE_BRIDGE1(loadSystemProperties, CanvasContext* context) {
John Recke4280ba2014-05-05 16:39:37 -0700119 bool needsRedraw = false;
120 if (Caches::hasInstance()) {
121 needsRedraw = Caches::getInstance().initProperties();
122 }
John Reckfe5e7b72014-05-23 17:42:28 -0700123 if (args->context->profiler().loadSystemProperties()) {
124 needsRedraw = true;
125 }
John Recke4280ba2014-05-05 16:39:37 -0700126 return (void*) needsRedraw;
127}
128
129bool RenderProxy::loadSystemProperties() {
130 SETUP_TASK(loadSystemProperties);
John Reckfe5e7b72014-05-23 17:42:28 -0700131 args->context = mContext;
John Recke4280ba2014-05-05 16:39:37 -0700132 return (bool) postAndWait(task);
133}
134
John Recka5dda642014-05-22 15:43:54 -0700135CREATE_BRIDGE2(initialize, CanvasContext* context, ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -0800136 return (void*) args->context->initialize(args->window);
137}
138
John Reckf7d9c1d2014-04-09 10:01:03 -0700139bool RenderProxy::initialize(const sp<ANativeWindow>& window) {
John Reck4f02bf42014-01-03 18:09:17 -0800140 SETUP_TASK(initialize);
141 args->context = mContext;
John Reckf7d9c1d2014-04-09 10:01:03 -0700142 args->window = window.get();
John Reck4f02bf42014-01-03 18:09:17 -0800143 return (bool) postAndWait(task);
144}
145
John Recka5dda642014-05-22 15:43:54 -0700146CREATE_BRIDGE2(updateSurface, CanvasContext* context, ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -0800147 args->context->updateSurface(args->window);
148 return NULL;
149}
150
John Reckf7d9c1d2014-04-09 10:01:03 -0700151void RenderProxy::updateSurface(const sp<ANativeWindow>& window) {
John Reck4f02bf42014-01-03 18:09:17 -0800152 SETUP_TASK(updateSurface);
153 args->context = mContext;
John Reckf7d9c1d2014-04-09 10:01:03 -0700154 args->window = window.get();
155 postAndWait(task);
156}
157
John Recka5dda642014-05-22 15:43:54 -0700158CREATE_BRIDGE2(pauseSurface, CanvasContext* context, ANativeWindow* window) {
John Reckf7d9c1d2014-04-09 10:01:03 -0700159 args->context->pauseSurface(args->window);
160 return NULL;
161}
162
163void RenderProxy::pauseSurface(const sp<ANativeWindow>& window) {
164 SETUP_TASK(pauseSurface);
165 args->context = mContext;
166 args->window = window.get();
167 postAndWait(task);
John Reck4f02bf42014-01-03 18:09:17 -0800168}
169
Chris Craik058fc642014-07-23 18:19:28 -0700170CREATE_BRIDGE7(setup, CanvasContext* context, int width, int height,
171 Vector3 lightCenter, float lightRadius,
172 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
173 args->context->setup(args->width, args->height, args->lightCenter, args->lightRadius,
174 args->ambientShadowAlpha, args->spotShadowAlpha);
John Reck4f02bf42014-01-03 18:09:17 -0800175 return NULL;
176}
177
Chris Craik058fc642014-07-23 18:19:28 -0700178void RenderProxy::setup(int width, int height, const Vector3& lightCenter, float lightRadius,
179 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
John Reck4f02bf42014-01-03 18:09:17 -0800180 SETUP_TASK(setup);
181 args->context = mContext;
182 args->width = width;
183 args->height = height;
Chris Craik797b95b2014-05-20 18:10:25 -0700184 args->lightCenter = lightCenter;
185 args->lightRadius = lightRadius;
Chris Craik058fc642014-07-23 18:19:28 -0700186 args->ambientShadowAlpha = ambientShadowAlpha;
187 args->spotShadowAlpha = spotShadowAlpha;
John Reck4f02bf42014-01-03 18:09:17 -0800188 post(task);
189}
190
John Reck63a06672014-05-07 13:45:54 -0700191CREATE_BRIDGE2(setOpaque, CanvasContext* context, bool opaque) {
192 args->context->setOpaque(args->opaque);
193 return NULL;
194}
195
196void RenderProxy::setOpaque(bool opaque) {
197 SETUP_TASK(setOpaque);
198 args->context = mContext;
199 args->opaque = opaque;
200 post(task);
201}
202
John Reckfe5e7b72014-05-23 17:42:28 -0700203int RenderProxy::syncAndDrawFrame(nsecs_t frameTimeNanos, nsecs_t recordDurationNanos,
John Recke4267ea2014-06-03 15:53:15 -0700204 float density) {
John Reckfe5e7b72014-05-23 17:42:28 -0700205 mDrawFrameTask.setDensity(density);
206 return mDrawFrameTask.drawFrame(frameTimeNanos, recordDurationNanos);
John Reck4f02bf42014-01-03 18:09:17 -0800207}
208
John Reck17035b02014-09-03 07:39:53 -0700209CREATE_BRIDGE1(destroy, CanvasContext* context) {
210 args->context->destroy();
John Reck4f02bf42014-01-03 18:09:17 -0800211 return NULL;
212}
213
John Reck17035b02014-09-03 07:39:53 -0700214void RenderProxy::destroy() {
215 SETUP_TASK(destroy);
John Reck4f02bf42014-01-03 18:09:17 -0800216 args->context = mContext;
John Reckfae904d2014-04-14 11:01:57 -0700217 // destroyCanvasAndSurface() needs a fence as when it returns the
218 // underlying BufferQueue is going to be released from under
219 // the render thread.
220 postAndWait(task);
John Reck4f02bf42014-01-03 18:09:17 -0800221}
222
John Reck3b202512014-06-23 13:13:08 -0700223CREATE_BRIDGE2(invokeFunctor, RenderThread* thread, Functor* functor) {
224 CanvasContext::invokeFunctor(*args->thread, args->functor);
John Reck0d1f6342014-03-28 20:30:27 -0700225 return NULL;
226}
227
228void RenderProxy::invokeFunctor(Functor* functor, bool waitForCompletion) {
John Reckd3d8daf2014-04-10 15:00:13 -0700229 ATRACE_CALL();
John Reck3b202512014-06-23 13:13:08 -0700230 RenderThread& thread = RenderThread::getInstance();
John Reck0d1f6342014-03-28 20:30:27 -0700231 SETUP_TASK(invokeFunctor);
John Reck3b202512014-06-23 13:13:08 -0700232 args->thread = &thread;
John Reck0d1f6342014-03-28 20:30:27 -0700233 args->functor = functor;
234 if (waitForCompletion) {
John Reck3b202512014-06-23 13:13:08 -0700235 // waitForCompletion = true is expected to be fairly rare and only
236 // happen in destruction. Thus it should be fine to temporarily
237 // create a Mutex
John Reck0e89e2b2014-10-31 14:49:06 -0700238 staticPostAndWait(task);
John Reck0d1f6342014-03-28 20:30:27 -0700239 } else {
John Reck3b202512014-06-23 13:13:08 -0700240 thread.queue(task);
John Reck0d1f6342014-03-28 20:30:27 -0700241 }
242}
243
John Reckfc53ef272014-02-11 10:40:25 -0800244CREATE_BRIDGE2(runWithGlContext, CanvasContext* context, RenderTask* task) {
245 args->context->runWithGlContext(args->task);
246 return NULL;
247}
248
249void RenderProxy::runWithGlContext(RenderTask* gltask) {
250 SETUP_TASK(runWithGlContext);
251 args->context = mContext;
252 args->task = gltask;
253 postAndWait(task);
254}
255
John Reck749906b2014-10-03 15:02:19 -0700256CREATE_BRIDGE2(createTextureLayer, RenderThread* thread, CanvasContext* context) {
John Reck1949e792014-04-08 15:18:56 -0700257 Layer* layer = args->context->createTextureLayer();
John Reck19b6bcf2014-02-14 20:03:38 -0800258 if (!layer) return 0;
John Reck749906b2014-10-03 15:02:19 -0700259 return new DeferredLayerUpdater(*args->thread, layer);
John Reck19b6bcf2014-02-14 20:03:38 -0800260}
261
262DeferredLayerUpdater* RenderProxy::createTextureLayer() {
263 SETUP_TASK(createTextureLayer);
John Reck1949e792014-04-08 15:18:56 -0700264 args->context = mContext;
John Reck749906b2014-10-03 15:02:19 -0700265 args->thread = &mRenderThread;
John Reck19b6bcf2014-02-14 20:03:38 -0800266 void* retval = postAndWait(task);
267 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval);
John Reck19b6bcf2014-02-14 20:03:38 -0800268 return layer;
269}
270
John Reck3e824952014-08-20 10:08:39 -0700271CREATE_BRIDGE2(buildLayer, CanvasContext* context, RenderNode* node) {
272 args->context->buildLayer(args->node);
273 return NULL;
274}
275
276void RenderProxy::buildLayer(RenderNode* node) {
277 SETUP_TASK(buildLayer);
278 args->context = mContext;
279 args->node = node;
280 postAndWait(task);
281}
282
John Reck19b6bcf2014-02-14 20:03:38 -0800283CREATE_BRIDGE3(copyLayerInto, CanvasContext* context, DeferredLayerUpdater* layer,
284 SkBitmap* bitmap) {
285 bool success = args->context->copyLayerInto(args->layer, args->bitmap);
286 return (void*) success;
287}
288
289bool RenderProxy::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
290 SETUP_TASK(copyLayerInto);
291 args->context = mContext;
292 args->layer = layer;
293 args->bitmap = bitmap;
294 return (bool) postAndWait(task);
295}
296
John Reckd72e0a32014-05-29 18:56:11 -0700297void RenderProxy::pushLayerUpdate(DeferredLayerUpdater* layer) {
298 mDrawFrameTask.pushLayerUpdate(layer);
299}
300
301void RenderProxy::cancelLayerUpdate(DeferredLayerUpdater* layer) {
302 mDrawFrameTask.removeLayerUpdate(layer);
John Reck19b6bcf2014-02-14 20:03:38 -0800303}
304
John Reck918ad522014-06-27 14:45:25 -0700305CREATE_BRIDGE1(detachSurfaceTexture, DeferredLayerUpdater* layer) {
306 args->layer->detachSurfaceTexture();
307 return NULL;
308}
309
310void RenderProxy::detachSurfaceTexture(DeferredLayerUpdater* layer) {
311 SETUP_TASK(detachSurfaceTexture);
312 args->layer = layer;
313 postAndWait(task);
314}
315
John Reckf47a5942014-06-30 16:20:04 -0700316CREATE_BRIDGE1(destroyHardwareResources, CanvasContext* context) {
317 args->context->destroyHardwareResources();
John Recke1628b72014-05-23 15:11:19 -0700318 return NULL;
319}
320
John Reckf47a5942014-06-30 16:20:04 -0700321void RenderProxy::destroyHardwareResources() {
322 SETUP_TASK(destroyHardwareResources);
John Recke1628b72014-05-23 15:11:19 -0700323 args->context = mContext;
John Recke1628b72014-05-23 15:11:19 -0700324 post(task);
325}
326
John Reckf47a5942014-06-30 16:20:04 -0700327CREATE_BRIDGE2(timMemory, RenderThread* thread, int level) {
328 CanvasContext::trimMemory(*args->thread, args->level);
329 return NULL;
330}
331
332void RenderProxy::trimMemory(int level) {
John Reckcd3a22c2014-08-06 13:33:59 -0700333 // Avoid creating a RenderThread to do a trimMemory.
334 if (RenderThread::hasInstance()) {
335 RenderThread& thread = RenderThread::getInstance();
336 SETUP_TASK(timMemory);
337 args->thread = &thread;
338 args->level = level;
339 thread.queue(task);
340 }
John Reckf47a5942014-06-30 16:20:04 -0700341}
342
John Reck28ad7b52014-04-07 16:59:25 -0700343CREATE_BRIDGE0(fence) {
344 // Intentionally empty
345 return NULL;
346}
347
348void RenderProxy::fence() {
349 SETUP_TASK(fence);
350 postAndWait(task);
351}
352
John Reckf47a5942014-06-30 16:20:04 -0700353CREATE_BRIDGE1(stopDrawing, CanvasContext* context) {
354 args->context->stopDrawing();
355 return NULL;
356}
357
358void RenderProxy::stopDrawing() {
359 SETUP_TASK(stopDrawing);
360 args->context = mContext;
361 postAndWait(task);
362}
363
John Recka5dda642014-05-22 15:43:54 -0700364CREATE_BRIDGE1(notifyFramePending, CanvasContext* context) {
365 args->context->notifyFramePending();
366 return NULL;
367}
368
369void RenderProxy::notifyFramePending() {
370 SETUP_TASK(notifyFramePending);
371 args->context = mContext;
372 mRenderThread.queueAtFront(task);
373}
374
John Reckfe5e7b72014-05-23 17:42:28 -0700375CREATE_BRIDGE2(dumpProfileInfo, CanvasContext* context, int fd) {
376 args->context->profiler().dumpData(args->fd);
377 return NULL;
378}
379
380void RenderProxy::dumpProfileInfo(int fd) {
381 SETUP_TASK(dumpProfileInfo);
382 args->context = mContext;
383 args->fd = fd;
384 postAndWait(task);
385}
386
John Reck0e89e2b2014-10-31 14:49:06 -0700387CREATE_BRIDGE1(outputLogBuffer, int fd) {
388 RenderNode::outputLogBuffer(args->fd);
389 return NULL;
390}
391
392void RenderProxy::outputLogBuffer(int fd) {
393 SETUP_TASK(outputLogBuffer);
394 args->fd = fd;
395 staticPostAndWait(task);
396}
397
John Reck3b202512014-06-23 13:13:08 -0700398CREATE_BRIDGE4(setTextureAtlas, RenderThread* thread, GraphicBuffer* buffer, int64_t* map, size_t size) {
399 CanvasContext::setTextureAtlas(*args->thread, args->buffer, args->map, args->size);
400 args->buffer->decStrong(0);
401 return NULL;
402}
403
404void RenderProxy::setTextureAtlas(const sp<GraphicBuffer>& buffer, int64_t* map, size_t size) {
405 SETUP_TASK(setTextureAtlas);
406 args->thread = &mRenderThread;
407 args->buffer = buffer.get();
408 args->buffer->incStrong(0);
409 args->map = map;
410 args->size = size;
411 post(task);
412}
413
John Reck4f02bf42014-01-03 18:09:17 -0800414void RenderProxy::post(RenderTask* task) {
415 mRenderThread.queue(task);
416}
417
418void* RenderProxy::postAndWait(MethodInvokeRenderTask* task) {
419 void* retval;
420 task->setReturnPtr(&retval);
421 SignalingRenderTask syncTask(task, &mSyncMutex, &mSyncCondition);
422 AutoMutex _lock(mSyncMutex);
Chris Craik738ec3a2014-07-25 18:25:02 +0000423 mRenderThread.queue(&syncTask);
424 mSyncCondition.wait(mSyncMutex);
John Reck4f02bf42014-01-03 18:09:17 -0800425 return retval;
426}
427
John Reck0e89e2b2014-10-31 14:49:06 -0700428void* RenderProxy::staticPostAndWait(MethodInvokeRenderTask* task) {
429 RenderThread& thread = RenderThread::getInstance();
430 void* retval;
431 task->setReturnPtr(&retval);
432 Mutex mutex;
433 Condition condition;
434 SignalingRenderTask syncTask(task, &mutex, &condition);
435 AutoMutex _lock(mutex);
436 thread.queue(&syncTask);
437 condition.wait(mutex);
438 return retval;
439}
440
John Reck4f02bf42014-01-03 18:09:17 -0800441} /* namespace renderthread */
442} /* namespace uirenderer */
443} /* namespace android */