blob: 5e37856ecce9ada05b34a4a459ec9265ed570fbe [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
John Reckba6adf62015-02-19 14:36:50 -080019#include "DeferredLayerUpdater.h"
20#include "DisplayList.h"
21#include "LayerRenderer.h"
John Reck10dd0582016-03-31 16:36:16 -070022#include "Readback.h"
John Reckba6adf62015-02-19 14:36:50 -080023#include "Rect.h"
24#include "renderthread/CanvasContext.h"
25#include "renderthread/RenderTask.h"
26#include "renderthread/RenderThread.h"
27#include "utils/Macros.h"
John Reck4f02bf42014-01-03 18:09:17 -080028
29namespace android {
30namespace uirenderer {
31namespace renderthread {
32
33#define ARGS(method) method ## Args
34
John Reck19b6bcf2014-02-14 20:03:38 -080035#define CREATE_BRIDGE0(name) CREATE_BRIDGE(name,,,,,,,,)
John Reck4f02bf42014-01-03 18:09:17 -080036#define CREATE_BRIDGE1(name, a1) CREATE_BRIDGE(name, a1,,,,,,,)
37#define CREATE_BRIDGE2(name, a1, a2) CREATE_BRIDGE(name, a1,a2,,,,,,)
38#define CREATE_BRIDGE3(name, a1, a2, a3) CREATE_BRIDGE(name, a1,a2,a3,,,,,)
39#define CREATE_BRIDGE4(name, a1, a2, a3, a4) CREATE_BRIDGE(name, a1,a2,a3,a4,,,,)
Chris Craik797b95b2014-05-20 18:10:25 -070040#define CREATE_BRIDGE5(name, a1, a2, a3, a4, a5) CREATE_BRIDGE(name, a1,a2,a3,a4,a5,,,)
Chris Craik058fc642014-07-23 18:19:28 -070041#define CREATE_BRIDGE6(name, a1, a2, a3, a4, a5, a6) CREATE_BRIDGE(name, a1,a2,a3,a4,a5,a6,,)
42#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 -080043#define CREATE_BRIDGE(name, a1, a2, a3, a4, a5, a6, a7, a8) \
44 typedef struct { \
45 a1; a2; a3; a4; a5; a6; a7; a8; \
46 } ARGS(name); \
47 static void* Bridge_ ## name(ARGS(name)* args)
48
49#define SETUP_TASK(method) \
50 LOG_ALWAYS_FATAL_IF( METHOD_INVOKE_PAYLOAD_SIZE < sizeof(ARGS(method)), \
Mark Salyzyn546f3532014-06-10 12:29:14 -070051 "METHOD_INVOKE_PAYLOAD_SIZE %zu is smaller than sizeof(" #method "Args) %zu", \
John Reck4f02bf42014-01-03 18:09:17 -080052 METHOD_INVOKE_PAYLOAD_SIZE, sizeof(ARGS(method))); \
John Recke2c45522014-04-07 17:31:44 -070053 MethodInvokeRenderTask* task = new MethodInvokeRenderTask((RunnableMethod) Bridge_ ## method); \
John Reck4f02bf42014-01-03 18:09:17 -080054 ARGS(method) *args = (ARGS(method) *) task->payload()
55
John Reck119907c2014-08-14 09:02:01 -070056CREATE_BRIDGE4(createContext, RenderThread* thread, bool translucent,
57 RenderNode* rootRenderNode, IContextFactory* contextFactory) {
58 return new CanvasContext(*args->thread, args->translucent,
59 args->rootRenderNode, args->contextFactory);
John Reck4f02bf42014-01-03 18:09:17 -080060}
61
John Reck119907c2014-08-14 09:02:01 -070062RenderProxy::RenderProxy(bool translucent, RenderNode* rootRenderNode, IContextFactory* contextFactory)
John Reck4f02bf42014-01-03 18:09:17 -080063 : mRenderThread(RenderThread::getInstance())
Chris Craikd41c4d82015-01-05 15:51:13 -080064 , mContext(nullptr) {
John Reck4f02bf42014-01-03 18:09:17 -080065 SETUP_TASK(createContext);
66 args->translucent = translucent;
John Recke45b1fd2014-04-15 09:50:16 -070067 args->rootRenderNode = rootRenderNode;
John Reck3b202512014-06-23 13:13:08 -070068 args->thread = &mRenderThread;
John Reck119907c2014-08-14 09:02:01 -070069 args->contextFactory = contextFactory;
John Reck4f02bf42014-01-03 18:09:17 -080070 mContext = (CanvasContext*) postAndWait(task);
Skuhneea7a7fb2015-08-28 07:10:31 -070071 mDrawFrameTask.setContext(&mRenderThread, mContext, rootRenderNode);
John Reck4f02bf42014-01-03 18:09:17 -080072}
73
74RenderProxy::~RenderProxy() {
75 destroyContext();
76}
77
78CREATE_BRIDGE1(destroyContext, CanvasContext* context) {
79 delete args->context;
Chris Craikd41c4d82015-01-05 15:51:13 -080080 return nullptr;
John Reck4f02bf42014-01-03 18:09:17 -080081}
82
83void RenderProxy::destroyContext() {
84 if (mContext) {
85 SETUP_TASK(destroyContext);
86 args->context = mContext;
Chris Craikd41c4d82015-01-05 15:51:13 -080087 mContext = nullptr;
Skuhneea7a7fb2015-08-28 07:10:31 -070088 mDrawFrameTask.setContext(nullptr, nullptr, nullptr);
John Reck668f0e32014-03-26 15:10:40 -070089 // This is also a fence as we need to be certain that there are no
90 // outstanding mDrawFrame tasks posted before it is destroyed
91 postAndWait(task);
John Reck4f02bf42014-01-03 18:09:17 -080092 }
93}
94
John Reck1125d1f2014-10-23 11:02:19 -070095CREATE_BRIDGE2(setSwapBehavior, CanvasContext* context, SwapBehavior swapBehavior) {
96 args->context->setSwapBehavior(args->swapBehavior);
Chris Craikd41c4d82015-01-05 15:51:13 -080097 return nullptr;
John Reck1125d1f2014-10-23 11:02:19 -070098}
99
100void RenderProxy::setSwapBehavior(SwapBehavior swapBehavior) {
101 SETUP_TASK(setSwapBehavior);
102 args->context = mContext;
103 args->swapBehavior = swapBehavior;
104 post(task);
105}
106
John Reckfe5e7b72014-05-23 17:42:28 -0700107CREATE_BRIDGE1(loadSystemProperties, CanvasContext* context) {
John Recke4280ba2014-05-05 16:39:37 -0700108 bool needsRedraw = false;
109 if (Caches::hasInstance()) {
Chris Craik2507c342015-05-04 14:36:49 -0700110 needsRedraw = Properties::load();
John Recke4280ba2014-05-05 16:39:37 -0700111 }
Chris Craik2507c342015-05-04 14:36:49 -0700112 if (args->context->profiler().consumeProperties()) {
John Reckfe5e7b72014-05-23 17:42:28 -0700113 needsRedraw = true;
114 }
John Recke4280ba2014-05-05 16:39:37 -0700115 return (void*) needsRedraw;
116}
117
118bool RenderProxy::loadSystemProperties() {
119 SETUP_TASK(loadSystemProperties);
John Reckfe5e7b72014-05-23 17:42:28 -0700120 args->context = mContext;
John Recke4280ba2014-05-05 16:39:37 -0700121 return (bool) postAndWait(task);
122}
123
John Reckb36016c2015-03-11 08:50:53 -0700124CREATE_BRIDGE2(setName, CanvasContext* context, const char* name) {
125 args->context->setName(std::string(args->name));
126 return nullptr;
127}
128
129void RenderProxy::setName(const char* name) {
130 SETUP_TASK(setName);
131 args->context = mContext;
132 args->name = name;
Chris Craik2507c342015-05-04 14:36:49 -0700133 postAndWait(task); // block since name/value pointers owned by caller
John Reckb36016c2015-03-11 08:50:53 -0700134}
135
John Reckf6481082016-02-02 15:18:23 -0800136CREATE_BRIDGE2(initialize, CanvasContext* context, Surface* surface) {
137 args->context->initialize(args->surface);
Thomas Buhot0bcd0cb2015-12-04 12:18:03 +0100138 return nullptr;
John Reck4f02bf42014-01-03 18:09:17 -0800139}
140
John Reckf6481082016-02-02 15:18:23 -0800141void RenderProxy::initialize(const sp<Surface>& surface) {
John Reck4f02bf42014-01-03 18:09:17 -0800142 SETUP_TASK(initialize);
143 args->context = mContext;
John Reckf6481082016-02-02 15:18:23 -0800144 args->surface = surface.get();
Thomas Buhot0bcd0cb2015-12-04 12:18:03 +0100145 post(task);
John Reck4f02bf42014-01-03 18:09:17 -0800146}
147
John Reckf6481082016-02-02 15:18:23 -0800148CREATE_BRIDGE2(updateSurface, CanvasContext* context, Surface* surface) {
149 args->context->updateSurface(args->surface);
Chris Craikd41c4d82015-01-05 15:51:13 -0800150 return nullptr;
John Reck4f02bf42014-01-03 18:09:17 -0800151}
152
John Reckf6481082016-02-02 15:18:23 -0800153void RenderProxy::updateSurface(const sp<Surface>& surface) {
John Reck4f02bf42014-01-03 18:09:17 -0800154 SETUP_TASK(updateSurface);
155 args->context = mContext;
John Reckf6481082016-02-02 15:18:23 -0800156 args->surface = surface.get();
John Reckf7d9c1d2014-04-09 10:01:03 -0700157 postAndWait(task);
158}
159
John Reckf6481082016-02-02 15:18:23 -0800160CREATE_BRIDGE2(pauseSurface, CanvasContext* context, Surface* surface) {
161 return (void*) args->context->pauseSurface(args->surface);
John Reckf7d9c1d2014-04-09 10:01:03 -0700162}
163
John Reckf6481082016-02-02 15:18:23 -0800164bool RenderProxy::pauseSurface(const sp<Surface>& surface) {
John Reckf7d9c1d2014-04-09 10:01:03 -0700165 SETUP_TASK(pauseSurface);
166 args->context = mContext;
John Reckf6481082016-02-02 15:18:23 -0800167 args->surface = surface.get();
John Reck01a5ea32014-12-03 13:01:07 -0800168 return (bool) postAndWait(task);
John Reck4f02bf42014-01-03 18:09:17 -0800169}
170
John Reck8afcc762016-04-13 10:24:06 -0700171CREATE_BRIDGE2(setStopped, CanvasContext* context, bool stopped) {
172 args->context->setStopped(args->stopped);
173 return nullptr;
174}
175
176void RenderProxy::setStopped(bool stopped) {
177 SETUP_TASK(setStopped);
178 args->context = mContext;
179 args->stopped = stopped;
180 postAndWait(task);
181}
182
Alan Viverette50210d92015-05-14 18:05:36 -0700183CREATE_BRIDGE6(setup, CanvasContext* context, int width, int height,
184 float lightRadius, uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
185 args->context->setup(args->width, args->height, args->lightRadius,
Chris Craik058fc642014-07-23 18:19:28 -0700186 args->ambientShadowAlpha, args->spotShadowAlpha);
Chris Craikd41c4d82015-01-05 15:51:13 -0800187 return nullptr;
John Reck4f02bf42014-01-03 18:09:17 -0800188}
189
Alan Viverette50210d92015-05-14 18:05:36 -0700190void RenderProxy::setup(int width, int height, float lightRadius,
John Reckb36016c2015-03-11 08:50:53 -0700191 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
John Reck4f02bf42014-01-03 18:09:17 -0800192 SETUP_TASK(setup);
193 args->context = mContext;
194 args->width = width;
195 args->height = height;
Chris Craik797b95b2014-05-20 18:10:25 -0700196 args->lightRadius = lightRadius;
Chris Craik058fc642014-07-23 18:19:28 -0700197 args->ambientShadowAlpha = ambientShadowAlpha;
198 args->spotShadowAlpha = spotShadowAlpha;
John Reck4f02bf42014-01-03 18:09:17 -0800199 post(task);
200}
201
Alan Viverette50210d92015-05-14 18:05:36 -0700202CREATE_BRIDGE2(setLightCenter, CanvasContext* context, Vector3 lightCenter) {
203 args->context->setLightCenter(args->lightCenter);
204 return nullptr;
205}
206
207void RenderProxy::setLightCenter(const Vector3& lightCenter) {
208 SETUP_TASK(setLightCenter);
209 args->context = mContext;
210 args->lightCenter = lightCenter;
211 post(task);
212}
213
John Reck63a06672014-05-07 13:45:54 -0700214CREATE_BRIDGE2(setOpaque, CanvasContext* context, bool opaque) {
215 args->context->setOpaque(args->opaque);
Chris Craikd41c4d82015-01-05 15:51:13 -0800216 return nullptr;
John Reck63a06672014-05-07 13:45:54 -0700217}
218
219void RenderProxy::setOpaque(bool opaque) {
220 SETUP_TASK(setOpaque);
221 args->context = mContext;
222 args->opaque = opaque;
223 post(task);
224}
225
John Reckba6adf62015-02-19 14:36:50 -0800226int64_t* RenderProxy::frameInfo() {
227 return mDrawFrameTask.frameInfo();
228}
229
John Reck51f2d602016-04-06 07:50:47 -0700230int RenderProxy::syncAndDrawFrame(TreeObserver* observer) {
231 return mDrawFrameTask.drawFrame(observer);
John Reck4f02bf42014-01-03 18:09:17 -0800232}
233
John Reck51f2d602016-04-06 07:50:47 -0700234CREATE_BRIDGE2(destroy, CanvasContext* context, TreeObserver* observer) {
235 args->context->destroy(args->observer);
Chris Craikd41c4d82015-01-05 15:51:13 -0800236 return nullptr;
John Reck4f02bf42014-01-03 18:09:17 -0800237}
238
John Reck51f2d602016-04-06 07:50:47 -0700239void RenderProxy::destroy(TreeObserver* observer) {
John Reck17035b02014-09-03 07:39:53 -0700240 SETUP_TASK(destroy);
John Reck4f02bf42014-01-03 18:09:17 -0800241 args->context = mContext;
John Reck51f2d602016-04-06 07:50:47 -0700242 args->observer = observer;
John Reckfae904d2014-04-14 11:01:57 -0700243 // destroyCanvasAndSurface() needs a fence as when it returns the
244 // underlying BufferQueue is going to be released from under
245 // the render thread.
246 postAndWait(task);
John Reck4f02bf42014-01-03 18:09:17 -0800247}
248
John Reck3b202512014-06-23 13:13:08 -0700249CREATE_BRIDGE2(invokeFunctor, RenderThread* thread, Functor* functor) {
250 CanvasContext::invokeFunctor(*args->thread, args->functor);
Chris Craikd41c4d82015-01-05 15:51:13 -0800251 return nullptr;
John Reck0d1f6342014-03-28 20:30:27 -0700252}
253
254void RenderProxy::invokeFunctor(Functor* functor, bool waitForCompletion) {
John Reckd3d8daf2014-04-10 15:00:13 -0700255 ATRACE_CALL();
John Reck3b202512014-06-23 13:13:08 -0700256 RenderThread& thread = RenderThread::getInstance();
John Reck0d1f6342014-03-28 20:30:27 -0700257 SETUP_TASK(invokeFunctor);
John Reck3b202512014-06-23 13:13:08 -0700258 args->thread = &thread;
John Reck0d1f6342014-03-28 20:30:27 -0700259 args->functor = functor;
260 if (waitForCompletion) {
John Reck3b202512014-06-23 13:13:08 -0700261 // waitForCompletion = true is expected to be fairly rare and only
262 // happen in destruction. Thus it should be fine to temporarily
263 // create a Mutex
John Reck0e89e2b2014-10-31 14:49:06 -0700264 staticPostAndWait(task);
John Reck0d1f6342014-03-28 20:30:27 -0700265 } else {
John Reck3b202512014-06-23 13:13:08 -0700266 thread.queue(task);
John Reck0d1f6342014-03-28 20:30:27 -0700267 }
268}
269
John Reckfc53ef272014-02-11 10:40:25 -0800270CREATE_BRIDGE2(runWithGlContext, CanvasContext* context, RenderTask* task) {
271 args->context->runWithGlContext(args->task);
Chris Craikd41c4d82015-01-05 15:51:13 -0800272 return nullptr;
John Reckfc53ef272014-02-11 10:40:25 -0800273}
274
275void RenderProxy::runWithGlContext(RenderTask* gltask) {
276 SETUP_TASK(runWithGlContext);
277 args->context = mContext;
278 args->task = gltask;
279 postAndWait(task);
280}
281
John Reckc36df952015-07-29 10:09:36 -0700282CREATE_BRIDGE1(createTextureLayer, CanvasContext* context) {
John Reck1949e792014-04-08 15:18:56 -0700283 Layer* layer = args->context->createTextureLayer();
Chris Craikd41c4d82015-01-05 15:51:13 -0800284 if (!layer) return nullptr;
John Reckc36df952015-07-29 10:09:36 -0700285 return new DeferredLayerUpdater(layer);
John Reck19b6bcf2014-02-14 20:03:38 -0800286}
287
288DeferredLayerUpdater* RenderProxy::createTextureLayer() {
289 SETUP_TASK(createTextureLayer);
John Reck1949e792014-04-08 15:18:56 -0700290 args->context = mContext;
John Reck19b6bcf2014-02-14 20:03:38 -0800291 void* retval = postAndWait(task);
292 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval);
John Reck19b6bcf2014-02-14 20:03:38 -0800293 return layer;
294}
295
John Reck51f2d602016-04-06 07:50:47 -0700296CREATE_BRIDGE3(buildLayer, CanvasContext* context, RenderNode* node, TreeObserver* observer) {
297 args->context->buildLayer(args->node, args->observer);
Chris Craikd41c4d82015-01-05 15:51:13 -0800298 return nullptr;
John Reck3e824952014-08-20 10:08:39 -0700299}
300
John Reck51f2d602016-04-06 07:50:47 -0700301void RenderProxy::buildLayer(RenderNode* node, TreeObserver* observer) {
John Reck3e824952014-08-20 10:08:39 -0700302 SETUP_TASK(buildLayer);
303 args->context = mContext;
304 args->node = node;
John Reck51f2d602016-04-06 07:50:47 -0700305 args->observer = observer;
John Reck3e824952014-08-20 10:08:39 -0700306 postAndWait(task);
307}
308
John Reck19b6bcf2014-02-14 20:03:38 -0800309CREATE_BRIDGE3(copyLayerInto, CanvasContext* context, DeferredLayerUpdater* layer,
310 SkBitmap* bitmap) {
311 bool success = args->context->copyLayerInto(args->layer, args->bitmap);
312 return (void*) success;
313}
314
John Reck3731dc22015-04-13 15:20:29 -0700315bool RenderProxy::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap& bitmap) {
John Reck19b6bcf2014-02-14 20:03:38 -0800316 SETUP_TASK(copyLayerInto);
317 args->context = mContext;
318 args->layer = layer;
John Reck3731dc22015-04-13 15:20:29 -0700319 args->bitmap = &bitmap;
John Reck19b6bcf2014-02-14 20:03:38 -0800320 return (bool) postAndWait(task);
321}
322
John Reckd72e0a32014-05-29 18:56:11 -0700323void RenderProxy::pushLayerUpdate(DeferredLayerUpdater* layer) {
324 mDrawFrameTask.pushLayerUpdate(layer);
325}
326
327void RenderProxy::cancelLayerUpdate(DeferredLayerUpdater* layer) {
328 mDrawFrameTask.removeLayerUpdate(layer);
John Reck19b6bcf2014-02-14 20:03:38 -0800329}
330
John Reck918ad522014-06-27 14:45:25 -0700331CREATE_BRIDGE1(detachSurfaceTexture, DeferredLayerUpdater* layer) {
332 args->layer->detachSurfaceTexture();
Chris Craikd41c4d82015-01-05 15:51:13 -0800333 return nullptr;
John Reck918ad522014-06-27 14:45:25 -0700334}
335
336void RenderProxy::detachSurfaceTexture(DeferredLayerUpdater* layer) {
337 SETUP_TASK(detachSurfaceTexture);
338 args->layer = layer;
339 postAndWait(task);
340}
341
John Reck51f2d602016-04-06 07:50:47 -0700342CREATE_BRIDGE2(destroyHardwareResources, CanvasContext* context, TreeObserver* observer) {
343 args->context->destroyHardwareResources(args->observer);
Chris Craikd41c4d82015-01-05 15:51:13 -0800344 return nullptr;
John Recke1628b72014-05-23 15:11:19 -0700345}
346
John Reck51f2d602016-04-06 07:50:47 -0700347void RenderProxy::destroyHardwareResources(TreeObserver* observer) {
John Reckf47a5942014-06-30 16:20:04 -0700348 SETUP_TASK(destroyHardwareResources);
John Recke1628b72014-05-23 15:11:19 -0700349 args->context = mContext;
John Reck51f2d602016-04-06 07:50:47 -0700350 args->observer = observer;
351 postAndWait(task);
John Recke1628b72014-05-23 15:11:19 -0700352}
353
Chris Craik2507c342015-05-04 14:36:49 -0700354CREATE_BRIDGE2(trimMemory, RenderThread* thread, int level) {
John Reckf47a5942014-06-30 16:20:04 -0700355 CanvasContext::trimMemory(*args->thread, args->level);
Chris Craikd41c4d82015-01-05 15:51:13 -0800356 return nullptr;
John Reckf47a5942014-06-30 16:20:04 -0700357}
358
359void RenderProxy::trimMemory(int level) {
John Reckcd3a22c2014-08-06 13:33:59 -0700360 // Avoid creating a RenderThread to do a trimMemory.
361 if (RenderThread::hasInstance()) {
362 RenderThread& thread = RenderThread::getInstance();
Chris Craik2507c342015-05-04 14:36:49 -0700363 SETUP_TASK(trimMemory);
John Reckcd3a22c2014-08-06 13:33:59 -0700364 args->thread = &thread;
365 args->level = level;
366 thread.queue(task);
367 }
John Reckf47a5942014-06-30 16:20:04 -0700368}
369
Chris Craik2507c342015-05-04 14:36:49 -0700370CREATE_BRIDGE2(overrideProperty, const char* name, const char* value) {
371 Properties::overrideProperty(args->name, args->value);
372 return nullptr;
373}
374
375void RenderProxy::overrideProperty(const char* name, const char* value) {
Chris Craik2507c342015-05-04 14:36:49 -0700376 SETUP_TASK(overrideProperty);
377 args->name = name;
378 args->value = value;
379 staticPostAndWait(task); // expensive, but block here since name/value pointers owned by caller
380}
381
John Reck28ad7b52014-04-07 16:59:25 -0700382CREATE_BRIDGE0(fence) {
383 // Intentionally empty
Chris Craikd41c4d82015-01-05 15:51:13 -0800384 return nullptr;
John Reck28ad7b52014-04-07 16:59:25 -0700385}
386
Andreas Gampe64bb4132014-11-22 00:35:09 +0000387template <typename T>
388void UNUSED(T t) {}
389
John Reck28ad7b52014-04-07 16:59:25 -0700390void RenderProxy::fence() {
391 SETUP_TASK(fence);
Andreas Gampe1e196742014-11-10 15:23:43 -0800392 UNUSED(args);
John Reck28ad7b52014-04-07 16:59:25 -0700393 postAndWait(task);
394}
395
Thomas Buhotc0a0e1a2016-01-18 10:31:58 +0100396void RenderProxy::staticFence() {
397 SETUP_TASK(fence);
398 UNUSED(args);
399 staticPostAndWait(task);
400}
401
John Reckf47a5942014-06-30 16:20:04 -0700402CREATE_BRIDGE1(stopDrawing, CanvasContext* context) {
403 args->context->stopDrawing();
Chris Craikd41c4d82015-01-05 15:51:13 -0800404 return nullptr;
John Reckf47a5942014-06-30 16:20:04 -0700405}
406
407void RenderProxy::stopDrawing() {
408 SETUP_TASK(stopDrawing);
409 args->context = mContext;
410 postAndWait(task);
411}
412
John Recka5dda642014-05-22 15:43:54 -0700413CREATE_BRIDGE1(notifyFramePending, CanvasContext* context) {
414 args->context->notifyFramePending();
Chris Craikd41c4d82015-01-05 15:51:13 -0800415 return nullptr;
John Recka5dda642014-05-22 15:43:54 -0700416}
417
418void RenderProxy::notifyFramePending() {
419 SETUP_TASK(notifyFramePending);
420 args->context = mContext;
421 mRenderThread.queueAtFront(task);
422}
423
John Reck7f2e5e32015-05-05 11:00:53 -0700424CREATE_BRIDGE4(dumpProfileInfo, CanvasContext* context, RenderThread* thread,
425 int fd, int dumpFlags) {
John Reckfe5e7b72014-05-23 17:42:28 -0700426 args->context->profiler().dumpData(args->fd);
Chris Craik53e51e42015-06-01 10:35:35 -0700427 if (args->dumpFlags & DumpFlags::FrameStats) {
John Reckba6adf62015-02-19 14:36:50 -0800428 args->context->dumpFrames(args->fd);
429 }
Chris Craik53e51e42015-06-01 10:35:35 -0700430 if (args->dumpFlags & DumpFlags::Reset) {
John Reckba6adf62015-02-19 14:36:50 -0800431 args->context->resetFrameStats();
432 }
John Recka41f2442016-04-07 16:36:57 -0700433 if (args->dumpFlags & DumpFlags::JankStats) {
434 args->thread->jankTracker().dump(args->fd);
435 }
Chris Craikd41c4d82015-01-05 15:51:13 -0800436 return nullptr;
John Reckfe5e7b72014-05-23 17:42:28 -0700437}
438
John Reckba6adf62015-02-19 14:36:50 -0800439void RenderProxy::dumpProfileInfo(int fd, int dumpFlags) {
John Reckfe5e7b72014-05-23 17:42:28 -0700440 SETUP_TASK(dumpProfileInfo);
441 args->context = mContext;
John Reck7f2e5e32015-05-05 11:00:53 -0700442 args->thread = &mRenderThread;
John Reckfe5e7b72014-05-23 17:42:28 -0700443 args->fd = fd;
John Reckba6adf62015-02-19 14:36:50 -0800444 args->dumpFlags = dumpFlags;
John Reckfe5e7b72014-05-23 17:42:28 -0700445 postAndWait(task);
446}
447
John Reck7f2e5e32015-05-05 11:00:53 -0700448CREATE_BRIDGE1(resetProfileInfo, CanvasContext* context) {
449 args->context->resetFrameStats();
450 return nullptr;
451}
452
453void RenderProxy::resetProfileInfo() {
454 SETUP_TASK(resetProfileInfo);
455 args->context = mContext;
456 postAndWait(task);
457}
458
John Reckba6adf62015-02-19 14:36:50 -0800459CREATE_BRIDGE2(dumpGraphicsMemory, int fd, RenderThread* thread) {
460 args->thread->jankTracker().dump(args->fd);
461
Chris Craik2ae07332015-01-21 14:22:39 -0800462 FILE *file = fdopen(args->fd, "a");
463 if (Caches::hasInstance()) {
464 String8 cachesLog;
465 Caches::getInstance().dumpMemoryUsage(cachesLog);
466 fprintf(file, "\nCaches:\n%s\n", cachesLog.string());
467 } else {
468 fprintf(file, "\nNo caches instance.\n");
469 }
Chris Craikff3edce2016-01-14 10:04:08 -0800470#if HWUI_NEW_OPS
471 fprintf(file, "\nPipeline=FrameBuilder\n");
472#else
473 fprintf(file, "\nPipeline=OpenGLRenderer\n");
474#endif
Chris Craik2ae07332015-01-21 14:22:39 -0800475 fflush(file);
Chris Craikd41c4d82015-01-05 15:51:13 -0800476 return nullptr;
John Reck0e89e2b2014-10-31 14:49:06 -0700477}
478
Chris Craik2ae07332015-01-21 14:22:39 -0800479void RenderProxy::dumpGraphicsMemory(int fd) {
youngmin0822.leec80c9ad2015-03-20 21:22:32 +0900480 if (!RenderThread::hasInstance()) return;
Chris Craik2ae07332015-01-21 14:22:39 -0800481 SETUP_TASK(dumpGraphicsMemory);
John Reck0e89e2b2014-10-31 14:49:06 -0700482 args->fd = fd;
John Reckba6adf62015-02-19 14:36:50 -0800483 args->thread = &RenderThread::getInstance();
John Reck0e89e2b2014-10-31 14:49:06 -0700484 staticPostAndWait(task);
485}
486
Skuhneea7a7fb2015-08-28 07:10:31 -0700487CREATE_BRIDGE4(setTextureAtlas, RenderThread* thread, GraphicBuffer* buffer, int64_t* map,
488 size_t size) {
John Reck3b202512014-06-23 13:13:08 -0700489 CanvasContext::setTextureAtlas(*args->thread, args->buffer, args->map, args->size);
Chris Craikd41c4d82015-01-05 15:51:13 -0800490 args->buffer->decStrong(nullptr);
491 return nullptr;
John Reck3b202512014-06-23 13:13:08 -0700492}
493
494void RenderProxy::setTextureAtlas(const sp<GraphicBuffer>& buffer, int64_t* map, size_t size) {
495 SETUP_TASK(setTextureAtlas);
496 args->thread = &mRenderThread;
497 args->buffer = buffer.get();
Chris Craikd41c4d82015-01-05 15:51:13 -0800498 args->buffer->incStrong(nullptr);
John Reck3b202512014-06-23 13:13:08 -0700499 args->map = map;
500 args->size = size;
501 post(task);
502}
503
John Reckedc524c2015-03-18 15:24:33 -0700504CREATE_BRIDGE2(setProcessStatsBuffer, RenderThread* thread, int fd) {
505 args->thread->jankTracker().switchStorageToAshmem(args->fd);
506 close(args->fd);
507 return nullptr;
508}
509
510void RenderProxy::setProcessStatsBuffer(int fd) {
511 SETUP_TASK(setProcessStatsBuffer);
512 args->thread = &mRenderThread;
513 args->fd = dup(fd);
514 post(task);
515}
516
Skuhneea7a7fb2015-08-28 07:10:31 -0700517CREATE_BRIDGE3(addRenderNode, CanvasContext* context, RenderNode* node, bool placeFront) {
518 args->context->addRenderNode(args->node, args->placeFront);
519 return nullptr;
520}
521
522void RenderProxy::addRenderNode(RenderNode* node, bool placeFront) {
523 SETUP_TASK(addRenderNode);
524 args->context = mContext;
525 args->node = node;
526 args->placeFront = placeFront;
527 post(task);
528}
529
530CREATE_BRIDGE2(removeRenderNode, CanvasContext* context, RenderNode* node) {
531 args->context->removeRenderNode(args->node);
532 return nullptr;
533}
534
535void RenderProxy::removeRenderNode(RenderNode* node) {
536 SETUP_TASK(removeRenderNode);
537 args->context = mContext;
538 args->node = node;
539 post(task);
540}
541
542CREATE_BRIDGE2(drawRenderNode, CanvasContext* context, RenderNode* node) {
543 args->context->prepareAndDraw(args->node);
544 return nullptr;
545}
546
547void RenderProxy::drawRenderNode(RenderNode* node) {
548 SETUP_TASK(drawRenderNode);
549 args->context = mContext;
550 args->node = node;
551 // Be pseudo-thread-safe and don't use any member variables
552 staticPostAndWait(task);
553}
554
Skuhneb8160872015-09-22 09:51:39 -0700555CREATE_BRIDGE5(setContentDrawBounds, CanvasContext* context, int left, int top,
Skuhneea7a7fb2015-08-28 07:10:31 -0700556 int right, int bottom) {
Skuhneb8160872015-09-22 09:51:39 -0700557 args->context->setContentDrawBounds(args->left, args->top, args->right, args->bottom);
Skuhneea7a7fb2015-08-28 07:10:31 -0700558 return nullptr;
559}
560
Skuhneb8160872015-09-22 09:51:39 -0700561void RenderProxy::setContentDrawBounds(int left, int top, int right, int bottom) {
562 SETUP_TASK(setContentDrawBounds);
Skuhneea7a7fb2015-08-28 07:10:31 -0700563 args->context = mContext;
564 args->left = left;
565 args->top = top;
566 args->right = right;
567 args->bottom = bottom;
568 staticPostAndWait(task);
569}
570
John Recke248bd12015-08-05 13:53:53 -0700571CREATE_BRIDGE1(serializeDisplayListTree, CanvasContext* context) {
572 args->context->serializeDisplayListTree();
573 return nullptr;
574}
575
576void RenderProxy::serializeDisplayListTree() {
577 SETUP_TASK(serializeDisplayListTree);
578 args->context = mContext;
579 post(task);
580}
581
Andres Morales910beb82016-02-02 16:19:40 -0800582CREATE_BRIDGE2(addFrameMetricsObserver, CanvasContext* context,
583 FrameMetricsObserver* frameStatsObserver) {
584 args->context->addFrameMetricsObserver(args->frameStatsObserver);
Andres Morales06f5bc72015-12-15 15:21:31 -0800585 if (args->frameStatsObserver != nullptr) {
586 args->frameStatsObserver->decStrong(args->context);
587 }
588 return nullptr;
589}
590
Andres Morales910beb82016-02-02 16:19:40 -0800591void RenderProxy::addFrameMetricsObserver(FrameMetricsObserver* observer) {
592 SETUP_TASK(addFrameMetricsObserver);
Andres Morales06f5bc72015-12-15 15:21:31 -0800593 args->context = mContext;
594 args->frameStatsObserver = observer;
595 if (observer != nullptr) {
596 observer->incStrong(mContext);
597 }
598 post(task);
599}
600
Andres Morales910beb82016-02-02 16:19:40 -0800601CREATE_BRIDGE2(removeFrameMetricsObserver, CanvasContext* context,
602 FrameMetricsObserver* frameStatsObserver) {
603 args->context->removeFrameMetricsObserver(args->frameStatsObserver);
Andres Morales06f5bc72015-12-15 15:21:31 -0800604 if (args->frameStatsObserver != nullptr) {
605 args->frameStatsObserver->decStrong(args->context);
606 }
607 return nullptr;
608}
609
Andres Morales910beb82016-02-02 16:19:40 -0800610void RenderProxy::removeFrameMetricsObserver(FrameMetricsObserver* observer) {
611 SETUP_TASK(removeFrameMetricsObserver);
Andres Morales06f5bc72015-12-15 15:21:31 -0800612 args->context = mContext;
613 args->frameStatsObserver = observer;
614 if (observer != nullptr) {
615 observer->incStrong(mContext);
616 }
617 post(task);
618}
619
John Reck10dd0582016-03-31 16:36:16 -0700620CREATE_BRIDGE3(copySurfaceInto, RenderThread* thread,
621 Surface* surface, SkBitmap* bitmap) {
622 return (void*) Readback::copySurfaceInto(*args->thread,
623 *args->surface, args->bitmap);
624}
625
626bool RenderProxy::copySurfaceInto(sp<Surface>& surface, SkBitmap* bitmap) {
627 SETUP_TASK(copySurfaceInto);
628 args->bitmap = bitmap;
629 args->surface = surface.get();
630 args->thread = &RenderThread::getInstance();
631 return (bool) staticPostAndWait(task);
632}
633
John Reck4f02bf42014-01-03 18:09:17 -0800634void RenderProxy::post(RenderTask* task) {
635 mRenderThread.queue(task);
636}
637
638void* RenderProxy::postAndWait(MethodInvokeRenderTask* task) {
639 void* retval;
640 task->setReturnPtr(&retval);
John Reckcba287b2015-11-10 12:52:44 -0800641 SignalingRenderTask syncTask(task, &mSyncMutex, &mSyncCondition);
642 AutoMutex _lock(mSyncMutex);
643 mRenderThread.queue(&syncTask);
644 mSyncCondition.wait(mSyncMutex);
John Reck4f02bf42014-01-03 18:09:17 -0800645 return retval;
646}
647
John Reck0e89e2b2014-10-31 14:49:06 -0700648void* RenderProxy::staticPostAndWait(MethodInvokeRenderTask* task) {
649 RenderThread& thread = RenderThread::getInstance();
650 void* retval;
651 task->setReturnPtr(&retval);
Chris Craik0a24b142015-10-19 17:10:19 -0700652 thread.queueAndWait(task);
John Reck0e89e2b2014-10-31 14:49:06 -0700653 return retval;
654}
655
John Reck4f02bf42014-01-03 18:09:17 -0800656} /* namespace renderthread */
657} /* namespace uirenderer */
658} /* namespace android */