blob: d842be9e7d6e17b73b1d5a51d70d54942248eb94 [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"
John Recka8963062017-06-14 10:47:50 -070021#include "Properties.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"
John Reck43871902016-08-01 14:39:24 -070025#include "renderthread/EglManager.h"
John Reckba6adf62015-02-19 14:36:50 -080026#include "renderthread/RenderTask.h"
27#include "renderthread/RenderThread.h"
John Reck9a814872017-05-22 15:04:21 -070028#include "renderstate/RenderState.h"
John Reckba6adf62015-02-19 14:36:50 -080029#include "utils/Macros.h"
John Reck43871902016-08-01 14:39:24 -070030#include "utils/TimeUtils.h"
John Reck4f02bf42014-01-03 18:09:17 -080031
sergeyv59eecb522016-11-17 17:54:57 -080032#include <ui/GraphicBuffer.h>
33
John Reck4f02bf42014-01-03 18:09:17 -080034namespace android {
35namespace uirenderer {
36namespace renderthread {
37
38#define ARGS(method) method ## Args
39
John Reck19b6bcf2014-02-14 20:03:38 -080040#define CREATE_BRIDGE0(name) CREATE_BRIDGE(name,,,,,,,,)
John Reck4f02bf42014-01-03 18:09:17 -080041#define CREATE_BRIDGE1(name, a1) CREATE_BRIDGE(name, a1,,,,,,,)
42#define CREATE_BRIDGE2(name, a1, a2) CREATE_BRIDGE(name, a1,a2,,,,,,)
43#define CREATE_BRIDGE3(name, a1, a2, a3) CREATE_BRIDGE(name, a1,a2,a3,,,,,)
44#define CREATE_BRIDGE4(name, a1, a2, a3, a4) CREATE_BRIDGE(name, a1,a2,a3,a4,,,,)
Chris Craik797b95b2014-05-20 18:10:25 -070045#define CREATE_BRIDGE5(name, a1, a2, a3, a4, a5) CREATE_BRIDGE(name, a1,a2,a3,a4,a5,,,)
Chris Craik058fc642014-07-23 18:19:28 -070046#define CREATE_BRIDGE6(name, a1, a2, a3, a4, a5, a6) CREATE_BRIDGE(name, a1,a2,a3,a4,a5,a6,,)
47#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 -080048#define CREATE_BRIDGE(name, a1, a2, a3, a4, a5, a6, a7, a8) \
49 typedef struct { \
50 a1; a2; a3; a4; a5; a6; a7; a8; \
51 } ARGS(name); \
John Reck43871902016-08-01 14:39:24 -070052 static_assert(std::is_trivially_destructible<ARGS(name)>::value, \
53 "Error, ARGS must be trivially destructible!"); \
John Reck4f02bf42014-01-03 18:09:17 -080054 static void* Bridge_ ## name(ARGS(name)* args)
55
56#define SETUP_TASK(method) \
57 LOG_ALWAYS_FATAL_IF( METHOD_INVOKE_PAYLOAD_SIZE < sizeof(ARGS(method)), \
Mark Salyzyn546f3532014-06-10 12:29:14 -070058 "METHOD_INVOKE_PAYLOAD_SIZE %zu is smaller than sizeof(" #method "Args) %zu", \
John Reck4f02bf42014-01-03 18:09:17 -080059 METHOD_INVOKE_PAYLOAD_SIZE, sizeof(ARGS(method))); \
John Recke2c45522014-04-07 17:31:44 -070060 MethodInvokeRenderTask* task = new MethodInvokeRenderTask((RunnableMethod) Bridge_ ## method); \
John Reck4f02bf42014-01-03 18:09:17 -080061 ARGS(method) *args = (ARGS(method) *) task->payload()
62
John Reck119907c2014-08-14 09:02:01 -070063CREATE_BRIDGE4(createContext, RenderThread* thread, bool translucent,
64 RenderNode* rootRenderNode, IContextFactory* contextFactory) {
Stan Iliev03de0742016-07-07 12:35:54 -040065 return CanvasContext::create(*args->thread, args->translucent,
John Reck119907c2014-08-14 09:02:01 -070066 args->rootRenderNode, args->contextFactory);
John Reck4f02bf42014-01-03 18:09:17 -080067}
68
John Reck119907c2014-08-14 09:02:01 -070069RenderProxy::RenderProxy(bool translucent, RenderNode* rootRenderNode, IContextFactory* contextFactory)
John Reck4f02bf42014-01-03 18:09:17 -080070 : mRenderThread(RenderThread::getInstance())
Chris Craikd41c4d82015-01-05 15:51:13 -080071 , mContext(nullptr) {
John Reck4f02bf42014-01-03 18:09:17 -080072 SETUP_TASK(createContext);
73 args->translucent = translucent;
John Recke45b1fd2014-04-15 09:50:16 -070074 args->rootRenderNode = rootRenderNode;
John Reck3b202512014-06-23 13:13:08 -070075 args->thread = &mRenderThread;
John Reck119907c2014-08-14 09:02:01 -070076 args->contextFactory = contextFactory;
John Reck4f02bf42014-01-03 18:09:17 -080077 mContext = (CanvasContext*) postAndWait(task);
Skuhneea7a7fb2015-08-28 07:10:31 -070078 mDrawFrameTask.setContext(&mRenderThread, mContext, rootRenderNode);
John Reck4f02bf42014-01-03 18:09:17 -080079}
80
81RenderProxy::~RenderProxy() {
82 destroyContext();
83}
84
85CREATE_BRIDGE1(destroyContext, CanvasContext* context) {
86 delete args->context;
Chris Craikd41c4d82015-01-05 15:51:13 -080087 return nullptr;
John Reck4f02bf42014-01-03 18:09:17 -080088}
89
90void RenderProxy::destroyContext() {
91 if (mContext) {
92 SETUP_TASK(destroyContext);
93 args->context = mContext;
Chris Craikd41c4d82015-01-05 15:51:13 -080094 mContext = nullptr;
Skuhneea7a7fb2015-08-28 07:10:31 -070095 mDrawFrameTask.setContext(nullptr, nullptr, nullptr);
John Reck668f0e32014-03-26 15:10:40 -070096 // This is also a fence as we need to be certain that there are no
97 // outstanding mDrawFrame tasks posted before it is destroyed
98 postAndWait(task);
John Reck4f02bf42014-01-03 18:09:17 -080099 }
100}
101
John Reck1125d1f2014-10-23 11:02:19 -0700102CREATE_BRIDGE2(setSwapBehavior, CanvasContext* context, SwapBehavior swapBehavior) {
103 args->context->setSwapBehavior(args->swapBehavior);
Chris Craikd41c4d82015-01-05 15:51:13 -0800104 return nullptr;
John Reck1125d1f2014-10-23 11:02:19 -0700105}
106
107void RenderProxy::setSwapBehavior(SwapBehavior swapBehavior) {
108 SETUP_TASK(setSwapBehavior);
109 args->context = mContext;
110 args->swapBehavior = swapBehavior;
111 post(task);
112}
113
John Reckfe5e7b72014-05-23 17:42:28 -0700114CREATE_BRIDGE1(loadSystemProperties, CanvasContext* context) {
John Recke4280ba2014-05-05 16:39:37 -0700115 bool needsRedraw = false;
116 if (Caches::hasInstance()) {
Chris Craik2507c342015-05-04 14:36:49 -0700117 needsRedraw = Properties::load();
John Recke4280ba2014-05-05 16:39:37 -0700118 }
Chris Craik2507c342015-05-04 14:36:49 -0700119 if (args->context->profiler().consumeProperties()) {
John Reckfe5e7b72014-05-23 17:42:28 -0700120 needsRedraw = true;
121 }
John Recke4280ba2014-05-05 16:39:37 -0700122 return (void*) needsRedraw;
123}
124
125bool RenderProxy::loadSystemProperties() {
126 SETUP_TASK(loadSystemProperties);
John Reckfe5e7b72014-05-23 17:42:28 -0700127 args->context = mContext;
John Recke4280ba2014-05-05 16:39:37 -0700128 return (bool) postAndWait(task);
129}
130
John Reckb36016c2015-03-11 08:50:53 -0700131CREATE_BRIDGE2(setName, CanvasContext* context, const char* name) {
132 args->context->setName(std::string(args->name));
133 return nullptr;
134}
135
136void RenderProxy::setName(const char* name) {
137 SETUP_TASK(setName);
138 args->context = mContext;
139 args->name = name;
Chris Craik2507c342015-05-04 14:36:49 -0700140 postAndWait(task); // block since name/value pointers owned by caller
John Reckb36016c2015-03-11 08:50:53 -0700141}
142
John Reckf6481082016-02-02 15:18:23 -0800143CREATE_BRIDGE2(initialize, CanvasContext* context, Surface* surface) {
144 args->context->initialize(args->surface);
Thomas Buhot0bcd0cb2015-12-04 12:18:03 +0100145 return nullptr;
John Reck4f02bf42014-01-03 18:09:17 -0800146}
147
John Reckf6481082016-02-02 15:18:23 -0800148void RenderProxy::initialize(const sp<Surface>& surface) {
John Reck4f02bf42014-01-03 18:09:17 -0800149 SETUP_TASK(initialize);
150 args->context = mContext;
John Reckf6481082016-02-02 15:18:23 -0800151 args->surface = surface.get();
Thomas Buhot0bcd0cb2015-12-04 12:18:03 +0100152 post(task);
John Reck4f02bf42014-01-03 18:09:17 -0800153}
154
John Reckf6481082016-02-02 15:18:23 -0800155CREATE_BRIDGE2(updateSurface, CanvasContext* context, Surface* surface) {
156 args->context->updateSurface(args->surface);
Chris Craikd41c4d82015-01-05 15:51:13 -0800157 return nullptr;
John Reck4f02bf42014-01-03 18:09:17 -0800158}
159
John Reckf6481082016-02-02 15:18:23 -0800160void RenderProxy::updateSurface(const sp<Surface>& surface) {
John Reck4f02bf42014-01-03 18:09:17 -0800161 SETUP_TASK(updateSurface);
162 args->context = mContext;
John Reckf6481082016-02-02 15:18:23 -0800163 args->surface = surface.get();
John Reckcd682122016-08-09 12:09:03 -0700164 post(task);
John Reckf7d9c1d2014-04-09 10:01:03 -0700165}
166
John Reckf6481082016-02-02 15:18:23 -0800167CREATE_BRIDGE2(pauseSurface, CanvasContext* context, Surface* surface) {
168 return (void*) args->context->pauseSurface(args->surface);
John Reckf7d9c1d2014-04-09 10:01:03 -0700169}
170
John Reckf6481082016-02-02 15:18:23 -0800171bool RenderProxy::pauseSurface(const sp<Surface>& surface) {
John Reckf7d9c1d2014-04-09 10:01:03 -0700172 SETUP_TASK(pauseSurface);
173 args->context = mContext;
John Reckf6481082016-02-02 15:18:23 -0800174 args->surface = surface.get();
John Reck01a5ea32014-12-03 13:01:07 -0800175 return (bool) postAndWait(task);
John Reck4f02bf42014-01-03 18:09:17 -0800176}
177
John Reck8afcc762016-04-13 10:24:06 -0700178CREATE_BRIDGE2(setStopped, CanvasContext* context, bool stopped) {
179 args->context->setStopped(args->stopped);
180 return nullptr;
181}
182
183void RenderProxy::setStopped(bool stopped) {
184 SETUP_TASK(setStopped);
185 args->context = mContext;
186 args->stopped = stopped;
187 postAndWait(task);
188}
189
John Reckab1080c2016-06-21 16:24:20 -0700190CREATE_BRIDGE4(setup, CanvasContext* context,
Alan Viverette50210d92015-05-14 18:05:36 -0700191 float lightRadius, uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
John Reckab1080c2016-06-21 16:24:20 -0700192 args->context->setup(args->lightRadius,
Chris Craik058fc642014-07-23 18:19:28 -0700193 args->ambientShadowAlpha, args->spotShadowAlpha);
Chris Craikd41c4d82015-01-05 15:51:13 -0800194 return nullptr;
John Reck4f02bf42014-01-03 18:09:17 -0800195}
196
John Reckab1080c2016-06-21 16:24:20 -0700197void RenderProxy::setup(float lightRadius,
John Reckb36016c2015-03-11 08:50:53 -0700198 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
John Reck4f02bf42014-01-03 18:09:17 -0800199 SETUP_TASK(setup);
200 args->context = mContext;
Chris Craik797b95b2014-05-20 18:10:25 -0700201 args->lightRadius = lightRadius;
Chris Craik058fc642014-07-23 18:19:28 -0700202 args->ambientShadowAlpha = ambientShadowAlpha;
203 args->spotShadowAlpha = spotShadowAlpha;
John Reck4f02bf42014-01-03 18:09:17 -0800204 post(task);
205}
206
Alan Viverette50210d92015-05-14 18:05:36 -0700207CREATE_BRIDGE2(setLightCenter, CanvasContext* context, Vector3 lightCenter) {
208 args->context->setLightCenter(args->lightCenter);
209 return nullptr;
210}
211
212void RenderProxy::setLightCenter(const Vector3& lightCenter) {
213 SETUP_TASK(setLightCenter);
214 args->context = mContext;
215 args->lightCenter = lightCenter;
216 post(task);
217}
218
John Reck63a06672014-05-07 13:45:54 -0700219CREATE_BRIDGE2(setOpaque, CanvasContext* context, bool opaque) {
220 args->context->setOpaque(args->opaque);
Chris Craikd41c4d82015-01-05 15:51:13 -0800221 return nullptr;
John Reck63a06672014-05-07 13:45:54 -0700222}
223
224void RenderProxy::setOpaque(bool opaque) {
225 SETUP_TASK(setOpaque);
226 args->context = mContext;
227 args->opaque = opaque;
228 post(task);
229}
230
John Reckba6adf62015-02-19 14:36:50 -0800231int64_t* RenderProxy::frameInfo() {
232 return mDrawFrameTask.frameInfo();
233}
234
John Reck2de950d2017-01-25 10:58:30 -0800235int RenderProxy::syncAndDrawFrame() {
236 return mDrawFrameTask.drawFrame();
John Reck4f02bf42014-01-03 18:09:17 -0800237}
238
John Reck2de950d2017-01-25 10:58:30 -0800239CREATE_BRIDGE1(destroy, CanvasContext* context) {
240 args->context->destroy();
Chris Craikd41c4d82015-01-05 15:51:13 -0800241 return nullptr;
John Reck4f02bf42014-01-03 18:09:17 -0800242}
243
John Reck2de950d2017-01-25 10:58:30 -0800244void RenderProxy::destroy() {
John Reck17035b02014-09-03 07:39:53 -0700245 SETUP_TASK(destroy);
John Reck4f02bf42014-01-03 18:09:17 -0800246 args->context = mContext;
John Reckfae904d2014-04-14 11:01:57 -0700247 // destroyCanvasAndSurface() needs a fence as when it returns the
248 // underlying BufferQueue is going to be released from under
249 // the render thread.
250 postAndWait(task);
John Reck4f02bf42014-01-03 18:09:17 -0800251}
252
John Reck3b202512014-06-23 13:13:08 -0700253CREATE_BRIDGE2(invokeFunctor, RenderThread* thread, Functor* functor) {
254 CanvasContext::invokeFunctor(*args->thread, args->functor);
Chris Craikd41c4d82015-01-05 15:51:13 -0800255 return nullptr;
John Reck0d1f6342014-03-28 20:30:27 -0700256}
257
258void RenderProxy::invokeFunctor(Functor* functor, bool waitForCompletion) {
John Reckd3d8daf2014-04-10 15:00:13 -0700259 ATRACE_CALL();
John Reck3b202512014-06-23 13:13:08 -0700260 RenderThread& thread = RenderThread::getInstance();
John Reck0d1f6342014-03-28 20:30:27 -0700261 SETUP_TASK(invokeFunctor);
John Reck3b202512014-06-23 13:13:08 -0700262 args->thread = &thread;
John Reck0d1f6342014-03-28 20:30:27 -0700263 args->functor = functor;
264 if (waitForCompletion) {
John Reck3b202512014-06-23 13:13:08 -0700265 // waitForCompletion = true is expected to be fairly rare and only
266 // happen in destruction. Thus it should be fine to temporarily
267 // create a Mutex
John Reck0e89e2b2014-10-31 14:49:06 -0700268 staticPostAndWait(task);
John Reck0d1f6342014-03-28 20:30:27 -0700269 } else {
John Reck3b202512014-06-23 13:13:08 -0700270 thread.queue(task);
John Reck0d1f6342014-03-28 20:30:27 -0700271 }
272}
273
John Reckc36df952015-07-29 10:09:36 -0700274CREATE_BRIDGE1(createTextureLayer, CanvasContext* context) {
Derek Sollenberger56ad6ec2016-07-22 12:13:32 -0400275 return args->context->createTextureLayer();
John Reck19b6bcf2014-02-14 20:03:38 -0800276}
277
278DeferredLayerUpdater* RenderProxy::createTextureLayer() {
279 SETUP_TASK(createTextureLayer);
John Reck1949e792014-04-08 15:18:56 -0700280 args->context = mContext;
John Reck19b6bcf2014-02-14 20:03:38 -0800281 void* retval = postAndWait(task);
282 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval);
John Reck19b6bcf2014-02-14 20:03:38 -0800283 return layer;
284}
285
John Reck2de950d2017-01-25 10:58:30 -0800286CREATE_BRIDGE2(buildLayer, CanvasContext* context, RenderNode* node) {
287 args->context->buildLayer(args->node);
Chris Craikd41c4d82015-01-05 15:51:13 -0800288 return nullptr;
John Reck3e824952014-08-20 10:08:39 -0700289}
290
John Reck2de950d2017-01-25 10:58:30 -0800291void RenderProxy::buildLayer(RenderNode* node) {
John Reck3e824952014-08-20 10:08:39 -0700292 SETUP_TASK(buildLayer);
293 args->context = mContext;
294 args->node = node;
295 postAndWait(task);
296}
297
John Reck19b6bcf2014-02-14 20:03:38 -0800298CREATE_BRIDGE3(copyLayerInto, CanvasContext* context, DeferredLayerUpdater* layer,
299 SkBitmap* bitmap) {
300 bool success = args->context->copyLayerInto(args->layer, args->bitmap);
301 return (void*) success;
302}
303
John Reck3731dc22015-04-13 15:20:29 -0700304bool RenderProxy::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap& bitmap) {
John Reck19b6bcf2014-02-14 20:03:38 -0800305 SETUP_TASK(copyLayerInto);
306 args->context = mContext;
307 args->layer = layer;
John Reck3731dc22015-04-13 15:20:29 -0700308 args->bitmap = &bitmap;
John Reck19b6bcf2014-02-14 20:03:38 -0800309 return (bool) postAndWait(task);
310}
311
John Reckd72e0a32014-05-29 18:56:11 -0700312void RenderProxy::pushLayerUpdate(DeferredLayerUpdater* layer) {
313 mDrawFrameTask.pushLayerUpdate(layer);
314}
315
316void RenderProxy::cancelLayerUpdate(DeferredLayerUpdater* layer) {
317 mDrawFrameTask.removeLayerUpdate(layer);
John Reck19b6bcf2014-02-14 20:03:38 -0800318}
319
John Reck918ad522014-06-27 14:45:25 -0700320CREATE_BRIDGE1(detachSurfaceTexture, DeferredLayerUpdater* layer) {
321 args->layer->detachSurfaceTexture();
Chris Craikd41c4d82015-01-05 15:51:13 -0800322 return nullptr;
John Reck918ad522014-06-27 14:45:25 -0700323}
324
325void RenderProxy::detachSurfaceTexture(DeferredLayerUpdater* layer) {
326 SETUP_TASK(detachSurfaceTexture);
327 args->layer = layer;
328 postAndWait(task);
329}
330
John Reck2de950d2017-01-25 10:58:30 -0800331CREATE_BRIDGE1(destroyHardwareResources, CanvasContext* context) {
332 args->context->destroyHardwareResources();
Chris Craikd41c4d82015-01-05 15:51:13 -0800333 return nullptr;
John Recke1628b72014-05-23 15:11:19 -0700334}
335
John Reck2de950d2017-01-25 10:58:30 -0800336void RenderProxy::destroyHardwareResources() {
John Reckf47a5942014-06-30 16:20:04 -0700337 SETUP_TASK(destroyHardwareResources);
John Recke1628b72014-05-23 15:11:19 -0700338 args->context = mContext;
John Reck51f2d602016-04-06 07:50:47 -0700339 postAndWait(task);
John Recke1628b72014-05-23 15:11:19 -0700340}
341
Chris Craik2507c342015-05-04 14:36:49 -0700342CREATE_BRIDGE2(trimMemory, RenderThread* thread, int level) {
John Reckf47a5942014-06-30 16:20:04 -0700343 CanvasContext::trimMemory(*args->thread, args->level);
Chris Craikd41c4d82015-01-05 15:51:13 -0800344 return nullptr;
John Reckf47a5942014-06-30 16:20:04 -0700345}
346
347void RenderProxy::trimMemory(int level) {
John Reckcd3a22c2014-08-06 13:33:59 -0700348 // Avoid creating a RenderThread to do a trimMemory.
349 if (RenderThread::hasInstance()) {
350 RenderThread& thread = RenderThread::getInstance();
Chris Craik2507c342015-05-04 14:36:49 -0700351 SETUP_TASK(trimMemory);
John Reckcd3a22c2014-08-06 13:33:59 -0700352 args->thread = &thread;
353 args->level = level;
354 thread.queue(task);
355 }
John Reckf47a5942014-06-30 16:20:04 -0700356}
357
Chris Craik2507c342015-05-04 14:36:49 -0700358CREATE_BRIDGE2(overrideProperty, const char* name, const char* value) {
359 Properties::overrideProperty(args->name, args->value);
360 return nullptr;
361}
362
363void RenderProxy::overrideProperty(const char* name, const char* value) {
Chris Craik2507c342015-05-04 14:36:49 -0700364 SETUP_TASK(overrideProperty);
365 args->name = name;
366 args->value = value;
367 staticPostAndWait(task); // expensive, but block here since name/value pointers owned by caller
368}
369
John Reck28ad7b52014-04-07 16:59:25 -0700370CREATE_BRIDGE0(fence) {
371 // Intentionally empty
Chris Craikd41c4d82015-01-05 15:51:13 -0800372 return nullptr;
John Reck28ad7b52014-04-07 16:59:25 -0700373}
374
Andreas Gampe64bb4132014-11-22 00:35:09 +0000375template <typename T>
376void UNUSED(T t) {}
377
John Reck28ad7b52014-04-07 16:59:25 -0700378void RenderProxy::fence() {
379 SETUP_TASK(fence);
Andreas Gampe1e196742014-11-10 15:23:43 -0800380 UNUSED(args);
John Reck28ad7b52014-04-07 16:59:25 -0700381 postAndWait(task);
382}
383
Thomas Buhotc0a0e1a2016-01-18 10:31:58 +0100384void RenderProxy::staticFence() {
385 SETUP_TASK(fence);
386 UNUSED(args);
387 staticPostAndWait(task);
388}
389
John Reckf47a5942014-06-30 16:20:04 -0700390CREATE_BRIDGE1(stopDrawing, CanvasContext* context) {
391 args->context->stopDrawing();
Chris Craikd41c4d82015-01-05 15:51:13 -0800392 return nullptr;
John Reckf47a5942014-06-30 16:20:04 -0700393}
394
395void RenderProxy::stopDrawing() {
396 SETUP_TASK(stopDrawing);
397 args->context = mContext;
398 postAndWait(task);
399}
400
John Recka5dda642014-05-22 15:43:54 -0700401CREATE_BRIDGE1(notifyFramePending, CanvasContext* context) {
402 args->context->notifyFramePending();
Chris Craikd41c4d82015-01-05 15:51:13 -0800403 return nullptr;
John Recka5dda642014-05-22 15:43:54 -0700404}
405
406void RenderProxy::notifyFramePending() {
407 SETUP_TASK(notifyFramePending);
408 args->context = mContext;
409 mRenderThread.queueAtFront(task);
410}
411
John Reck7f2e5e32015-05-05 11:00:53 -0700412CREATE_BRIDGE4(dumpProfileInfo, CanvasContext* context, RenderThread* thread,
413 int fd, int dumpFlags) {
John Reckfe5e7b72014-05-23 17:42:28 -0700414 args->context->profiler().dumpData(args->fd);
Chris Craik53e51e42015-06-01 10:35:35 -0700415 if (args->dumpFlags & DumpFlags::FrameStats) {
John Reckba6adf62015-02-19 14:36:50 -0800416 args->context->dumpFrames(args->fd);
417 }
Chris Craik53e51e42015-06-01 10:35:35 -0700418 if (args->dumpFlags & DumpFlags::Reset) {
John Reckba6adf62015-02-19 14:36:50 -0800419 args->context->resetFrameStats();
420 }
John Recka41f2442016-04-07 16:36:57 -0700421 if (args->dumpFlags & DumpFlags::JankStats) {
422 args->thread->jankTracker().dump(args->fd);
423 }
Chris Craikd41c4d82015-01-05 15:51:13 -0800424 return nullptr;
John Reckfe5e7b72014-05-23 17:42:28 -0700425}
426
John Reckba6adf62015-02-19 14:36:50 -0800427void RenderProxy::dumpProfileInfo(int fd, int dumpFlags) {
John Reckfe5e7b72014-05-23 17:42:28 -0700428 SETUP_TASK(dumpProfileInfo);
429 args->context = mContext;
John Reck7f2e5e32015-05-05 11:00:53 -0700430 args->thread = &mRenderThread;
John Reckfe5e7b72014-05-23 17:42:28 -0700431 args->fd = fd;
John Reckba6adf62015-02-19 14:36:50 -0800432 args->dumpFlags = dumpFlags;
John Reckfe5e7b72014-05-23 17:42:28 -0700433 postAndWait(task);
434}
435
John Reck7f2e5e32015-05-05 11:00:53 -0700436CREATE_BRIDGE1(resetProfileInfo, CanvasContext* context) {
437 args->context->resetFrameStats();
438 return nullptr;
439}
440
441void RenderProxy::resetProfileInfo() {
442 SETUP_TASK(resetProfileInfo);
443 args->context = mContext;
444 postAndWait(task);
445}
446
John Reckf1480762016-07-03 18:28:25 -0700447CREATE_BRIDGE2(frameTimePercentile, RenderThread* thread, int percentile) {
448 return reinterpret_cast<void*>(static_cast<uintptr_t>(
449 args->thread->jankTracker().findPercentile(args->percentile)));
450}
451
452uint32_t RenderProxy::frameTimePercentile(int p) {
453 SETUP_TASK(frameTimePercentile);
454 args->thread = &mRenderThread;
455 args->percentile = p;
456 return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
457 postAndWait(task)));
458}
459
John Reckba6adf62015-02-19 14:36:50 -0800460CREATE_BRIDGE2(dumpGraphicsMemory, int fd, RenderThread* thread) {
461 args->thread->jankTracker().dump(args->fd);
462
Chris Craik2ae07332015-01-21 14:22:39 -0800463 FILE *file = fdopen(args->fd, "a");
464 if (Caches::hasInstance()) {
465 String8 cachesLog;
466 Caches::getInstance().dumpMemoryUsage(cachesLog);
467 fprintf(file, "\nCaches:\n%s\n", cachesLog.string());
468 } else {
469 fprintf(file, "\nNo caches instance.\n");
470 }
Chris Craikff3edce2016-01-14 10:04:08 -0800471 fprintf(file, "\nPipeline=FrameBuilder\n");
Chris Craik2ae07332015-01-21 14:22:39 -0800472 fflush(file);
Chris Craikd41c4d82015-01-05 15:51:13 -0800473 return nullptr;
John Reck0e89e2b2014-10-31 14:49:06 -0700474}
475
Chris Craik2ae07332015-01-21 14:22:39 -0800476void RenderProxy::dumpGraphicsMemory(int fd) {
youngmin0822.leec80c9ad2015-03-20 21:22:32 +0900477 if (!RenderThread::hasInstance()) return;
Chris Craik2ae07332015-01-21 14:22:39 -0800478 SETUP_TASK(dumpGraphicsMemory);
John Reck0e89e2b2014-10-31 14:49:06 -0700479 args->fd = fd;
John Reckba6adf62015-02-19 14:36:50 -0800480 args->thread = &RenderThread::getInstance();
John Reck0e89e2b2014-10-31 14:49:06 -0700481 staticPostAndWait(task);
482}
483
John Reckedc524c2015-03-18 15:24:33 -0700484CREATE_BRIDGE2(setProcessStatsBuffer, RenderThread* thread, int fd) {
485 args->thread->jankTracker().switchStorageToAshmem(args->fd);
486 close(args->fd);
487 return nullptr;
488}
489
490void RenderProxy::setProcessStatsBuffer(int fd) {
491 SETUP_TASK(setProcessStatsBuffer);
John Reckdf1742e2017-01-19 15:56:21 -0800492 auto& rt = RenderThread::getInstance();
493 args->thread = &rt;
John Reckedc524c2015-03-18 15:24:33 -0700494 args->fd = dup(fd);
John Reckdf1742e2017-01-19 15:56:21 -0800495 rt.queue(task);
496}
497
498CREATE_BRIDGE1(rotateProcessStatsBuffer, RenderThread* thread) {
499 args->thread->jankTracker().rotateStorage();
500 return nullptr;
501}
502
503void RenderProxy::rotateProcessStatsBuffer() {
504 SETUP_TASK(rotateProcessStatsBuffer);
505 auto& rt = RenderThread::getInstance();
506 args->thread = &rt;
507 rt.queue(task);
John Reckedc524c2015-03-18 15:24:33 -0700508}
509
Tim Murray33eb07f2016-06-10 10:03:20 -0700510int RenderProxy::getRenderThreadTid() {
511 return mRenderThread.getTid();
512}
513
Skuhneea7a7fb2015-08-28 07:10:31 -0700514CREATE_BRIDGE3(addRenderNode, CanvasContext* context, RenderNode* node, bool placeFront) {
515 args->context->addRenderNode(args->node, args->placeFront);
516 return nullptr;
517}
518
519void RenderProxy::addRenderNode(RenderNode* node, bool placeFront) {
520 SETUP_TASK(addRenderNode);
521 args->context = mContext;
522 args->node = node;
523 args->placeFront = placeFront;
524 post(task);
525}
526
527CREATE_BRIDGE2(removeRenderNode, CanvasContext* context, RenderNode* node) {
528 args->context->removeRenderNode(args->node);
529 return nullptr;
530}
531
532void RenderProxy::removeRenderNode(RenderNode* node) {
533 SETUP_TASK(removeRenderNode);
534 args->context = mContext;
535 args->node = node;
536 post(task);
537}
538
539CREATE_BRIDGE2(drawRenderNode, CanvasContext* context, RenderNode* node) {
540 args->context->prepareAndDraw(args->node);
541 return nullptr;
542}
543
544void RenderProxy::drawRenderNode(RenderNode* node) {
545 SETUP_TASK(drawRenderNode);
546 args->context = mContext;
547 args->node = node;
548 // Be pseudo-thread-safe and don't use any member variables
549 staticPostAndWait(task);
550}
551
Skuhneb8160872015-09-22 09:51:39 -0700552CREATE_BRIDGE5(setContentDrawBounds, CanvasContext* context, int left, int top,
Skuhneea7a7fb2015-08-28 07:10:31 -0700553 int right, int bottom) {
Skuhneb8160872015-09-22 09:51:39 -0700554 args->context->setContentDrawBounds(args->left, args->top, args->right, args->bottom);
Skuhneea7a7fb2015-08-28 07:10:31 -0700555 return nullptr;
556}
557
Skuhneb8160872015-09-22 09:51:39 -0700558void RenderProxy::setContentDrawBounds(int left, int top, int right, int bottom) {
559 SETUP_TASK(setContentDrawBounds);
Skuhneea7a7fb2015-08-28 07:10:31 -0700560 args->context = mContext;
561 args->left = left;
562 args->top = top;
563 args->right = right;
564 args->bottom = bottom;
565 staticPostAndWait(task);
566}
567
John Recke248bd12015-08-05 13:53:53 -0700568CREATE_BRIDGE1(serializeDisplayListTree, CanvasContext* context) {
569 args->context->serializeDisplayListTree();
570 return nullptr;
571}
572
573void RenderProxy::serializeDisplayListTree() {
574 SETUP_TASK(serializeDisplayListTree);
575 args->context = mContext;
576 post(task);
577}
578
Andres Morales910beb82016-02-02 16:19:40 -0800579CREATE_BRIDGE2(addFrameMetricsObserver, CanvasContext* context,
580 FrameMetricsObserver* frameStatsObserver) {
581 args->context->addFrameMetricsObserver(args->frameStatsObserver);
Andres Morales06f5bc72015-12-15 15:21:31 -0800582 if (args->frameStatsObserver != nullptr) {
583 args->frameStatsObserver->decStrong(args->context);
584 }
585 return nullptr;
586}
587
Andres Morales910beb82016-02-02 16:19:40 -0800588void RenderProxy::addFrameMetricsObserver(FrameMetricsObserver* observer) {
589 SETUP_TASK(addFrameMetricsObserver);
Andres Morales06f5bc72015-12-15 15:21:31 -0800590 args->context = mContext;
591 args->frameStatsObserver = observer;
592 if (observer != nullptr) {
593 observer->incStrong(mContext);
594 }
595 post(task);
596}
597
Andres Morales910beb82016-02-02 16:19:40 -0800598CREATE_BRIDGE2(removeFrameMetricsObserver, CanvasContext* context,
599 FrameMetricsObserver* frameStatsObserver) {
600 args->context->removeFrameMetricsObserver(args->frameStatsObserver);
Andres Morales06f5bc72015-12-15 15:21:31 -0800601 if (args->frameStatsObserver != nullptr) {
602 args->frameStatsObserver->decStrong(args->context);
603 }
604 return nullptr;
605}
606
Andres Morales910beb82016-02-02 16:19:40 -0800607void RenderProxy::removeFrameMetricsObserver(FrameMetricsObserver* observer) {
608 SETUP_TASK(removeFrameMetricsObserver);
Andres Morales06f5bc72015-12-15 15:21:31 -0800609 args->context = mContext;
610 args->frameStatsObserver = observer;
611 if (observer != nullptr) {
612 observer->incStrong(mContext);
613 }
614 post(task);
615}
616
John Reck95801462016-09-01 09:44:09 -0700617CREATE_BRIDGE4(copySurfaceInto, RenderThread* thread,
618 Surface* surface, Rect srcRect, SkBitmap* bitmap) {
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500619 return (void*)args->thread->readback().copySurfaceInto(*args->surface,
620 args->srcRect, args->bitmap);
John Reck10dd0582016-03-31 16:36:16 -0700621}
622
John Reck95801462016-09-01 09:44:09 -0700623int RenderProxy::copySurfaceInto(sp<Surface>& surface, int left, int top,
624 int right, int bottom, SkBitmap* bitmap) {
John Reck10dd0582016-03-31 16:36:16 -0700625 SETUP_TASK(copySurfaceInto);
626 args->bitmap = bitmap;
627 args->surface = surface.get();
628 args->thread = &RenderThread::getInstance();
John Reck95801462016-09-01 09:44:09 -0700629 args->srcRect.set(left, top, right, bottom);
John Recke94cbc72016-04-25 13:03:44 -0700630 return static_cast<int>(
631 reinterpret_cast<intptr_t>( staticPostAndWait(task) ));
John Reck10dd0582016-03-31 16:36:16 -0700632}
633
sergeyvec4a4b12016-10-20 18:39:04 -0700634CREATE_BRIDGE2(prepareToDraw, RenderThread* thread, Bitmap* bitmap) {
Derek Sollenbergerdaf72292016-10-25 12:09:18 -0400635 CanvasContext::prepareToDraw(*args->thread, args->bitmap);
sergeyvec4a4b12016-10-20 18:39:04 -0700636 args->bitmap->unref();
John Reck43871902016-08-01 14:39:24 -0700637 args->bitmap = nullptr;
638 return nullptr;
639}
640
sergeyvec4a4b12016-10-20 18:39:04 -0700641void RenderProxy::prepareToDraw(Bitmap& bitmap) {
John Reck43871902016-08-01 14:39:24 -0700642 // If we haven't spun up a hardware accelerated window yet, there's no
643 // point in precaching these bitmaps as it can't impact jank.
644 // We also don't know if we even will spin up a hardware-accelerated
645 // window or not.
646 if (!RenderThread::hasInstance()) return;
647 RenderThread* renderThread = &RenderThread::getInstance();
648 SETUP_TASK(prepareToDraw);
649 args->thread = renderThread;
sergeyvec4a4b12016-10-20 18:39:04 -0700650 bitmap.ref();
651 args->bitmap = &bitmap;
John Reck43871902016-08-01 14:39:24 -0700652 nsecs_t lastVsync = renderThread->timeLord().latestVsync();
653 nsecs_t estimatedNextVsync = lastVsync + renderThread->timeLord().frameIntervalNanos();
654 nsecs_t timeToNextVsync = estimatedNextVsync - systemTime(CLOCK_MONOTONIC);
655 // We expect the UI thread to take 4ms and for RT to be active from VSYNC+4ms to
656 // VSYNC+12ms or so, so aim for the gap during which RT is expected to
657 // be idle
658 // TODO: Make this concept a first-class supported thing? RT could use
659 // knowledge of pending draws to better schedule this task
660 if (timeToNextVsync > -6_ms && timeToNextVsync < 1_ms) {
661 renderThread->queueAt(task, estimatedNextVsync + 8_ms);
662 } else {
663 renderThread->queue(task);
664 }
665}
666
sergeyv694d4992016-10-27 10:23:13 -0700667CREATE_BRIDGE2(allocateHardwareBitmap, RenderThread* thread, SkBitmap* bitmap) {
668 sk_sp<Bitmap> hardwareBitmap = Bitmap::allocateHardwareBitmap(*args->thread, *args->bitmap);
669 return hardwareBitmap.release();
670}
671
672sk_sp<Bitmap> RenderProxy::allocateHardwareBitmap(SkBitmap& bitmap) {
673 SETUP_TASK(allocateHardwareBitmap);
674 args->bitmap = &bitmap;
675 args->thread = &RenderThread::getInstance();
676 sk_sp<Bitmap> hardwareBitmap(reinterpret_cast<Bitmap*>(staticPostAndWait(task)));
677 return hardwareBitmap;
678}
679
sergeyv59eecb522016-11-17 17:54:57 -0800680CREATE_BRIDGE3(copyGraphicBufferInto, RenderThread* thread, GraphicBuffer* buffer, SkBitmap* bitmap) {
681 return (void*) args->thread->readback().copyGraphicBufferInto(args->buffer, args->bitmap);
682}
683
684int RenderProxy::copyGraphicBufferInto(GraphicBuffer* buffer, SkBitmap* bitmap) {
Stan Iliev6983bc42017-02-02 14:11:53 -0500685 RenderThread& thread = RenderThread::getInstance();
686 if (Properties::isSkiaEnabled() && gettid() == thread.getTid()) {
687 //TODO: fix everything that hits this. We should never be triggering a readback ourselves.
688 return (int) thread.readback().copyGraphicBufferInto(buffer, bitmap);
689 } else {
690 SETUP_TASK(copyGraphicBufferInto);
691 args->thread = &thread;
692 args->bitmap = bitmap;
693 args->buffer = buffer;
694 return static_cast<int>(reinterpret_cast<intptr_t>(staticPostAndWait(task)));
695 }
sergeyv59eecb522016-11-17 17:54:57 -0800696}
697
John Reck9a814872017-05-22 15:04:21 -0700698CREATE_BRIDGE2(onBitmapDestroyed, RenderThread* thread, uint32_t pixelRefId) {
699 args->thread->renderState().onBitmapDestroyed(args->pixelRefId);
700 return nullptr;
701}
702
703void RenderProxy::onBitmapDestroyed(uint32_t pixelRefId) {
704 if (!RenderThread::hasInstance()) return;
705 SETUP_TASK(onBitmapDestroyed);
706 RenderThread& thread = RenderThread::getInstance();
707 args->thread = &thread;
708 args->pixelRefId = pixelRefId;
709 thread.queue(task);
710}
711
John Recka8963062017-06-14 10:47:50 -0700712void RenderProxy::disableVsync() {
713 Properties::disableVsync = true;
714}
715
John Reck4f02bf42014-01-03 18:09:17 -0800716void RenderProxy::post(RenderTask* task) {
717 mRenderThread.queue(task);
718}
719
720void* RenderProxy::postAndWait(MethodInvokeRenderTask* task) {
721 void* retval;
722 task->setReturnPtr(&retval);
John Reckcba287b2015-11-10 12:52:44 -0800723 SignalingRenderTask syncTask(task, &mSyncMutex, &mSyncCondition);
724 AutoMutex _lock(mSyncMutex);
725 mRenderThread.queue(&syncTask);
Tom Cherry298a1462017-02-28 14:07:09 -0800726 while (!syncTask.hasRun()) {
727 mSyncCondition.wait(mSyncMutex);
728 }
John Reck4f02bf42014-01-03 18:09:17 -0800729 return retval;
730}
731
John Reck0e89e2b2014-10-31 14:49:06 -0700732void* RenderProxy::staticPostAndWait(MethodInvokeRenderTask* task) {
733 RenderThread& thread = RenderThread::getInstance();
Stan Iliev6983bc42017-02-02 14:11:53 -0500734 LOG_ALWAYS_FATAL_IF(gettid() == thread.getTid());
John Reck0e89e2b2014-10-31 14:49:06 -0700735 void* retval;
736 task->setReturnPtr(&retval);
Chris Craik0a24b142015-10-19 17:10:19 -0700737 thread.queueAndWait(task);
John Reck0e89e2b2014-10-31 14:49:06 -0700738 return retval;
739}
740
John Reck4f02bf42014-01-03 18:09:17 -0800741} /* namespace renderthread */
742} /* namespace uirenderer */
743} /* namespace android */