blob: 9528874b92d6e344b7a3c3ae49cf1faa0a5cb29c [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 Reckfe5e7b72014-05-23 17:42:28 -0700106CREATE_BRIDGE1(loadSystemProperties, CanvasContext* context) {
John Recke4280ba2014-05-05 16:39:37 -0700107 bool needsRedraw = false;
108 if (Caches::hasInstance()) {
109 needsRedraw = Caches::getInstance().initProperties();
110 }
John Reckfe5e7b72014-05-23 17:42:28 -0700111 if (args->context->profiler().loadSystemProperties()) {
112 needsRedraw = true;
113 }
John Recke4280ba2014-05-05 16:39:37 -0700114 return (void*) needsRedraw;
115}
116
117bool RenderProxy::loadSystemProperties() {
118 SETUP_TASK(loadSystemProperties);
John Reckfe5e7b72014-05-23 17:42:28 -0700119 args->context = mContext;
John Recke4280ba2014-05-05 16:39:37 -0700120 return (bool) postAndWait(task);
121}
122
John Recka5dda642014-05-22 15:43:54 -0700123CREATE_BRIDGE2(initialize, CanvasContext* context, ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -0800124 return (void*) args->context->initialize(args->window);
125}
126
John Reckf7d9c1d2014-04-09 10:01:03 -0700127bool RenderProxy::initialize(const sp<ANativeWindow>& window) {
John Reck4f02bf42014-01-03 18:09:17 -0800128 SETUP_TASK(initialize);
129 args->context = mContext;
John Reckf7d9c1d2014-04-09 10:01:03 -0700130 args->window = window.get();
John Reck4f02bf42014-01-03 18:09:17 -0800131 return (bool) postAndWait(task);
132}
133
John Recka5dda642014-05-22 15:43:54 -0700134CREATE_BRIDGE2(updateSurface, CanvasContext* context, ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -0800135 args->context->updateSurface(args->window);
136 return NULL;
137}
138
John Reckf7d9c1d2014-04-09 10:01:03 -0700139void RenderProxy::updateSurface(const sp<ANativeWindow>& window) {
John Reck4f02bf42014-01-03 18:09:17 -0800140 SETUP_TASK(updateSurface);
141 args->context = mContext;
John Reckf7d9c1d2014-04-09 10:01:03 -0700142 args->window = window.get();
143 postAndWait(task);
144}
145
John Recka5dda642014-05-22 15:43:54 -0700146CREATE_BRIDGE2(pauseSurface, CanvasContext* context, ANativeWindow* window) {
John Reckf7d9c1d2014-04-09 10:01:03 -0700147 args->context->pauseSurface(args->window);
148 return NULL;
149}
150
151void RenderProxy::pauseSurface(const sp<ANativeWindow>& window) {
152 SETUP_TASK(pauseSurface);
153 args->context = mContext;
154 args->window = window.get();
155 postAndWait(task);
John Reck4f02bf42014-01-03 18:09:17 -0800156}
157
Chris Craik058fc642014-07-23 18:19:28 -0700158CREATE_BRIDGE7(setup, CanvasContext* context, int width, int height,
159 Vector3 lightCenter, float lightRadius,
160 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
161 args->context->setup(args->width, args->height, args->lightCenter, args->lightRadius,
162 args->ambientShadowAlpha, args->spotShadowAlpha);
John Reck4f02bf42014-01-03 18:09:17 -0800163 return NULL;
164}
165
Chris Craik058fc642014-07-23 18:19:28 -0700166void RenderProxy::setup(int width, int height, const Vector3& lightCenter, float lightRadius,
167 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
John Reck4f02bf42014-01-03 18:09:17 -0800168 SETUP_TASK(setup);
169 args->context = mContext;
170 args->width = width;
171 args->height = height;
Chris Craik797b95b2014-05-20 18:10:25 -0700172 args->lightCenter = lightCenter;
173 args->lightRadius = lightRadius;
Chris Craik058fc642014-07-23 18:19:28 -0700174 args->ambientShadowAlpha = ambientShadowAlpha;
175 args->spotShadowAlpha = spotShadowAlpha;
John Reck4f02bf42014-01-03 18:09:17 -0800176 post(task);
177}
178
John Reck63a06672014-05-07 13:45:54 -0700179CREATE_BRIDGE2(setOpaque, CanvasContext* context, bool opaque) {
180 args->context->setOpaque(args->opaque);
181 return NULL;
182}
183
184void RenderProxy::setOpaque(bool opaque) {
185 SETUP_TASK(setOpaque);
186 args->context = mContext;
187 args->opaque = opaque;
188 post(task);
189}
190
John Reckfe5e7b72014-05-23 17:42:28 -0700191int RenderProxy::syncAndDrawFrame(nsecs_t frameTimeNanos, nsecs_t recordDurationNanos,
John Recke4267ea2014-06-03 15:53:15 -0700192 float density) {
John Reckfe5e7b72014-05-23 17:42:28 -0700193 mDrawFrameTask.setDensity(density);
194 return mDrawFrameTask.drawFrame(frameTimeNanos, recordDurationNanos);
John Reck4f02bf42014-01-03 18:09:17 -0800195}
196
John Reck17035b02014-09-03 07:39:53 -0700197CREATE_BRIDGE1(destroy, CanvasContext* context) {
198 args->context->destroy();
John Reck4f02bf42014-01-03 18:09:17 -0800199 return NULL;
200}
201
John Reck17035b02014-09-03 07:39:53 -0700202void RenderProxy::destroy() {
203 SETUP_TASK(destroy);
John Reck4f02bf42014-01-03 18:09:17 -0800204 args->context = mContext;
John Reckfae904d2014-04-14 11:01:57 -0700205 // destroyCanvasAndSurface() needs a fence as when it returns the
206 // underlying BufferQueue is going to be released from under
207 // the render thread.
208 postAndWait(task);
John Reck4f02bf42014-01-03 18:09:17 -0800209}
210
John Reck3b202512014-06-23 13:13:08 -0700211CREATE_BRIDGE2(invokeFunctor, RenderThread* thread, Functor* functor) {
212 CanvasContext::invokeFunctor(*args->thread, args->functor);
John Reck0d1f6342014-03-28 20:30:27 -0700213 return NULL;
214}
215
216void RenderProxy::invokeFunctor(Functor* functor, bool waitForCompletion) {
John Reckd3d8daf2014-04-10 15:00:13 -0700217 ATRACE_CALL();
John Reck3b202512014-06-23 13:13:08 -0700218 RenderThread& thread = RenderThread::getInstance();
John Reck0d1f6342014-03-28 20:30:27 -0700219 SETUP_TASK(invokeFunctor);
John Reck3b202512014-06-23 13:13:08 -0700220 args->thread = &thread;
John Reck0d1f6342014-03-28 20:30:27 -0700221 args->functor = functor;
222 if (waitForCompletion) {
John Reck3b202512014-06-23 13:13:08 -0700223 // waitForCompletion = true is expected to be fairly rare and only
224 // happen in destruction. Thus it should be fine to temporarily
225 // create a Mutex
226 Mutex mutex;
227 Condition condition;
228 SignalingRenderTask syncTask(task, &mutex, &condition);
229 AutoMutex _lock(mutex);
230 thread.queue(&syncTask);
231 condition.wait(mutex);
John Reck0d1f6342014-03-28 20:30:27 -0700232 } else {
John Reck3b202512014-06-23 13:13:08 -0700233 thread.queue(task);
John Reck0d1f6342014-03-28 20:30:27 -0700234 }
235}
236
John Reckfc53ef272014-02-11 10:40:25 -0800237CREATE_BRIDGE2(runWithGlContext, CanvasContext* context, RenderTask* task) {
238 args->context->runWithGlContext(args->task);
239 return NULL;
240}
241
242void RenderProxy::runWithGlContext(RenderTask* gltask) {
243 SETUP_TASK(runWithGlContext);
244 args->context = mContext;
245 args->task = gltask;
246 postAndWait(task);
247}
248
John Reckd72e0a32014-05-29 18:56:11 -0700249CREATE_BRIDGE1(destroyLayer, Layer* layer) {
250 LayerRenderer::destroyLayer(args->layer);
251 return NULL;
252}
253
John Reck3b202512014-06-23 13:13:08 -0700254void RenderProxy::enqueueDestroyLayer(Layer* layer) {
John Reckd72e0a32014-05-29 18:56:11 -0700255 SETUP_TASK(destroyLayer);
256 args->layer = layer;
257 RenderThread::getInstance().queue(task);
258}
259
John Reck1949e792014-04-08 15:18:56 -0700260CREATE_BRIDGE3(createDisplayListLayer, CanvasContext* context, int width, int height) {
261 Layer* layer = args->context->createRenderLayer(args->width, args->height);
John Reck19b6bcf2014-02-14 20:03:38 -0800262 if (!layer) return 0;
John Reck3b202512014-06-23 13:13:08 -0700263 return new DeferredLayerUpdater(layer, RenderProxy::enqueueDestroyLayer);
John Reck19b6bcf2014-02-14 20:03:38 -0800264}
265
266DeferredLayerUpdater* RenderProxy::createDisplayListLayer(int width, int height) {
267 SETUP_TASK(createDisplayListLayer);
268 args->width = width;
269 args->height = height;
John Reck1949e792014-04-08 15:18:56 -0700270 args->context = mContext;
John Reck19b6bcf2014-02-14 20:03:38 -0800271 void* retval = postAndWait(task);
272 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval);
John Reck19b6bcf2014-02-14 20:03:38 -0800273 return layer;
274}
275
John Reck1949e792014-04-08 15:18:56 -0700276CREATE_BRIDGE1(createTextureLayer, CanvasContext* context) {
277 Layer* layer = args->context->createTextureLayer();
John Reck19b6bcf2014-02-14 20:03:38 -0800278 if (!layer) return 0;
John Reck3b202512014-06-23 13:13:08 -0700279 return new DeferredLayerUpdater(layer, RenderProxy::enqueueDestroyLayer);
John Reck19b6bcf2014-02-14 20:03:38 -0800280}
281
282DeferredLayerUpdater* RenderProxy::createTextureLayer() {
283 SETUP_TASK(createTextureLayer);
John Reck1949e792014-04-08 15:18:56 -0700284 args->context = mContext;
John Reck19b6bcf2014-02-14 20:03:38 -0800285 void* retval = postAndWait(task);
286 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval);
John Reck19b6bcf2014-02-14 20:03:38 -0800287 return layer;
288}
289
John Reck3e824952014-08-20 10:08:39 -0700290CREATE_BRIDGE2(buildLayer, CanvasContext* context, RenderNode* node) {
291 args->context->buildLayer(args->node);
292 return NULL;
293}
294
295void RenderProxy::buildLayer(RenderNode* node) {
296 SETUP_TASK(buildLayer);
297 args->context = mContext;
298 args->node = node;
299 postAndWait(task);
300}
301
John Reck19b6bcf2014-02-14 20:03:38 -0800302CREATE_BRIDGE3(copyLayerInto, CanvasContext* context, DeferredLayerUpdater* layer,
303 SkBitmap* bitmap) {
304 bool success = args->context->copyLayerInto(args->layer, args->bitmap);
305 return (void*) success;
306}
307
308bool RenderProxy::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
309 SETUP_TASK(copyLayerInto);
310 args->context = mContext;
311 args->layer = layer;
312 args->bitmap = bitmap;
313 return (bool) postAndWait(task);
314}
315
John Reckd72e0a32014-05-29 18:56:11 -0700316void RenderProxy::pushLayerUpdate(DeferredLayerUpdater* layer) {
317 mDrawFrameTask.pushLayerUpdate(layer);
318}
319
320void RenderProxy::cancelLayerUpdate(DeferredLayerUpdater* layer) {
321 mDrawFrameTask.removeLayerUpdate(layer);
John Reck19b6bcf2014-02-14 20:03:38 -0800322}
323
John Reck918ad522014-06-27 14:45:25 -0700324CREATE_BRIDGE1(detachSurfaceTexture, DeferredLayerUpdater* layer) {
325 args->layer->detachSurfaceTexture();
326 return NULL;
327}
328
329void RenderProxy::detachSurfaceTexture(DeferredLayerUpdater* layer) {
330 SETUP_TASK(detachSurfaceTexture);
331 args->layer = layer;
332 postAndWait(task);
333}
334
John Reckf47a5942014-06-30 16:20:04 -0700335CREATE_BRIDGE1(destroyHardwareResources, CanvasContext* context) {
336 args->context->destroyHardwareResources();
John Recke1628b72014-05-23 15:11:19 -0700337 return NULL;
338}
339
John Reckf47a5942014-06-30 16:20:04 -0700340void RenderProxy::destroyHardwareResources() {
341 SETUP_TASK(destroyHardwareResources);
John Recke1628b72014-05-23 15:11:19 -0700342 args->context = mContext;
John Recke1628b72014-05-23 15:11:19 -0700343 post(task);
344}
345
John Reckf47a5942014-06-30 16:20:04 -0700346CREATE_BRIDGE2(timMemory, RenderThread* thread, int level) {
347 CanvasContext::trimMemory(*args->thread, args->level);
348 return NULL;
349}
350
351void RenderProxy::trimMemory(int level) {
John Reckcd3a22c2014-08-06 13:33:59 -0700352 // Avoid creating a RenderThread to do a trimMemory.
353 if (RenderThread::hasInstance()) {
354 RenderThread& thread = RenderThread::getInstance();
355 SETUP_TASK(timMemory);
356 args->thread = &thread;
357 args->level = level;
358 thread.queue(task);
359 }
John Reckf47a5942014-06-30 16:20:04 -0700360}
361
John Reck28ad7b52014-04-07 16:59:25 -0700362CREATE_BRIDGE0(fence) {
363 // Intentionally empty
364 return NULL;
365}
366
367void RenderProxy::fence() {
368 SETUP_TASK(fence);
369 postAndWait(task);
370}
371
John Reckf47a5942014-06-30 16:20:04 -0700372CREATE_BRIDGE1(stopDrawing, CanvasContext* context) {
373 args->context->stopDrawing();
374 return NULL;
375}
376
377void RenderProxy::stopDrawing() {
378 SETUP_TASK(stopDrawing);
379 args->context = mContext;
380 postAndWait(task);
381}
382
John Recka5dda642014-05-22 15:43:54 -0700383CREATE_BRIDGE1(notifyFramePending, CanvasContext* context) {
384 args->context->notifyFramePending();
385 return NULL;
386}
387
388void RenderProxy::notifyFramePending() {
389 SETUP_TASK(notifyFramePending);
390 args->context = mContext;
391 mRenderThread.queueAtFront(task);
392}
393
John Reckfe5e7b72014-05-23 17:42:28 -0700394CREATE_BRIDGE2(dumpProfileInfo, CanvasContext* context, int fd) {
395 args->context->profiler().dumpData(args->fd);
396 return NULL;
397}
398
399void RenderProxy::dumpProfileInfo(int fd) {
400 SETUP_TASK(dumpProfileInfo);
401 args->context = mContext;
402 args->fd = fd;
403 postAndWait(task);
404}
405
John Reck3b202512014-06-23 13:13:08 -0700406CREATE_BRIDGE4(setTextureAtlas, RenderThread* thread, GraphicBuffer* buffer, int64_t* map, size_t size) {
407 CanvasContext::setTextureAtlas(*args->thread, args->buffer, args->map, args->size);
408 args->buffer->decStrong(0);
409 return NULL;
410}
411
412void RenderProxy::setTextureAtlas(const sp<GraphicBuffer>& buffer, int64_t* map, size_t size) {
413 SETUP_TASK(setTextureAtlas);
414 args->thread = &mRenderThread;
415 args->buffer = buffer.get();
416 args->buffer->incStrong(0);
417 args->map = map;
418 args->size = size;
419 post(task);
420}
421
John Reck4f02bf42014-01-03 18:09:17 -0800422void RenderProxy::post(RenderTask* task) {
423 mRenderThread.queue(task);
424}
425
426void* RenderProxy::postAndWait(MethodInvokeRenderTask* task) {
427 void* retval;
428 task->setReturnPtr(&retval);
429 SignalingRenderTask syncTask(task, &mSyncMutex, &mSyncCondition);
430 AutoMutex _lock(mSyncMutex);
Chris Craik738ec3a2014-07-25 18:25:02 +0000431 mRenderThread.queue(&syncTask);
432 mSyncCondition.wait(mSyncMutex);
John Reck4f02bf42014-01-03 18:09:17 -0800433 return retval;
434}
435
436} /* namespace renderthread */
437} /* namespace uirenderer */
438} /* namespace android */