blob: 370cf525fa58f09fdfc8cacc169536a131b2cbf9 [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
Romain Guy26a2b972017-04-17 09:39:51 -0700231CREATE_BRIDGE2(setWideGamut, CanvasContext* context, bool wideGamut) {
232 args->context->setWideGamut(args->wideGamut);
233 return nullptr;
234}
235
236void RenderProxy::setWideGamut(bool wideGamut) {
237 SETUP_TASK(setWideGamut);
238 args->context = mContext;
239 args->wideGamut = wideGamut;
240 post(task);
241}
242
John Reckba6adf62015-02-19 14:36:50 -0800243int64_t* RenderProxy::frameInfo() {
244 return mDrawFrameTask.frameInfo();
245}
246
John Reck2de950d2017-01-25 10:58:30 -0800247int RenderProxy::syncAndDrawFrame() {
248 return mDrawFrameTask.drawFrame();
John Reck4f02bf42014-01-03 18:09:17 -0800249}
250
John Reck2de950d2017-01-25 10:58:30 -0800251CREATE_BRIDGE1(destroy, CanvasContext* context) {
252 args->context->destroy();
Chris Craikd41c4d82015-01-05 15:51:13 -0800253 return nullptr;
John Reck4f02bf42014-01-03 18:09:17 -0800254}
255
John Reck2de950d2017-01-25 10:58:30 -0800256void RenderProxy::destroy() {
John Reck17035b02014-09-03 07:39:53 -0700257 SETUP_TASK(destroy);
John Reck4f02bf42014-01-03 18:09:17 -0800258 args->context = mContext;
John Reckfae904d2014-04-14 11:01:57 -0700259 // destroyCanvasAndSurface() needs a fence as when it returns the
260 // underlying BufferQueue is going to be released from under
261 // the render thread.
262 postAndWait(task);
John Reck4f02bf42014-01-03 18:09:17 -0800263}
264
John Reck3b202512014-06-23 13:13:08 -0700265CREATE_BRIDGE2(invokeFunctor, RenderThread* thread, Functor* functor) {
266 CanvasContext::invokeFunctor(*args->thread, args->functor);
Chris Craikd41c4d82015-01-05 15:51:13 -0800267 return nullptr;
John Reck0d1f6342014-03-28 20:30:27 -0700268}
269
270void RenderProxy::invokeFunctor(Functor* functor, bool waitForCompletion) {
John Reckd3d8daf2014-04-10 15:00:13 -0700271 ATRACE_CALL();
John Reck3b202512014-06-23 13:13:08 -0700272 RenderThread& thread = RenderThread::getInstance();
John Reck0d1f6342014-03-28 20:30:27 -0700273 SETUP_TASK(invokeFunctor);
John Reck3b202512014-06-23 13:13:08 -0700274 args->thread = &thread;
John Reck0d1f6342014-03-28 20:30:27 -0700275 args->functor = functor;
276 if (waitForCompletion) {
John Reck3b202512014-06-23 13:13:08 -0700277 // waitForCompletion = true is expected to be fairly rare and only
278 // happen in destruction. Thus it should be fine to temporarily
279 // create a Mutex
John Reck0e89e2b2014-10-31 14:49:06 -0700280 staticPostAndWait(task);
John Reck0d1f6342014-03-28 20:30:27 -0700281 } else {
John Reck3b202512014-06-23 13:13:08 -0700282 thread.queue(task);
John Reck0d1f6342014-03-28 20:30:27 -0700283 }
284}
285
John Reckc36df952015-07-29 10:09:36 -0700286CREATE_BRIDGE1(createTextureLayer, CanvasContext* context) {
Derek Sollenberger56ad6ec2016-07-22 12:13:32 -0400287 return args->context->createTextureLayer();
John Reck19b6bcf2014-02-14 20:03:38 -0800288}
289
290DeferredLayerUpdater* RenderProxy::createTextureLayer() {
291 SETUP_TASK(createTextureLayer);
John Reck1949e792014-04-08 15:18:56 -0700292 args->context = mContext;
John Reck19b6bcf2014-02-14 20:03:38 -0800293 void* retval = postAndWait(task);
294 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval);
John Reck19b6bcf2014-02-14 20:03:38 -0800295 return layer;
296}
297
John Reck2de950d2017-01-25 10:58:30 -0800298CREATE_BRIDGE2(buildLayer, CanvasContext* context, RenderNode* node) {
299 args->context->buildLayer(args->node);
Chris Craikd41c4d82015-01-05 15:51:13 -0800300 return nullptr;
John Reck3e824952014-08-20 10:08:39 -0700301}
302
John Reck2de950d2017-01-25 10:58:30 -0800303void RenderProxy::buildLayer(RenderNode* node) {
John Reck3e824952014-08-20 10:08:39 -0700304 SETUP_TASK(buildLayer);
305 args->context = mContext;
306 args->node = node;
307 postAndWait(task);
308}
309
John Reck19b6bcf2014-02-14 20:03:38 -0800310CREATE_BRIDGE3(copyLayerInto, CanvasContext* context, DeferredLayerUpdater* layer,
311 SkBitmap* bitmap) {
312 bool success = args->context->copyLayerInto(args->layer, args->bitmap);
313 return (void*) success;
314}
315
John Reck3731dc22015-04-13 15:20:29 -0700316bool RenderProxy::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap& bitmap) {
John Reck19b6bcf2014-02-14 20:03:38 -0800317 SETUP_TASK(copyLayerInto);
318 args->context = mContext;
319 args->layer = layer;
John Reck3731dc22015-04-13 15:20:29 -0700320 args->bitmap = &bitmap;
John Reck19b6bcf2014-02-14 20:03:38 -0800321 return (bool) postAndWait(task);
322}
323
John Reckd72e0a32014-05-29 18:56:11 -0700324void RenderProxy::pushLayerUpdate(DeferredLayerUpdater* layer) {
325 mDrawFrameTask.pushLayerUpdate(layer);
326}
327
328void RenderProxy::cancelLayerUpdate(DeferredLayerUpdater* layer) {
329 mDrawFrameTask.removeLayerUpdate(layer);
John Reck19b6bcf2014-02-14 20:03:38 -0800330}
331
John Reck918ad522014-06-27 14:45:25 -0700332CREATE_BRIDGE1(detachSurfaceTexture, DeferredLayerUpdater* layer) {
333 args->layer->detachSurfaceTexture();
Chris Craikd41c4d82015-01-05 15:51:13 -0800334 return nullptr;
John Reck918ad522014-06-27 14:45:25 -0700335}
336
337void RenderProxy::detachSurfaceTexture(DeferredLayerUpdater* layer) {
338 SETUP_TASK(detachSurfaceTexture);
339 args->layer = layer;
340 postAndWait(task);
341}
342
John Reck2de950d2017-01-25 10:58:30 -0800343CREATE_BRIDGE1(destroyHardwareResources, CanvasContext* context) {
344 args->context->destroyHardwareResources();
Chris Craikd41c4d82015-01-05 15:51:13 -0800345 return nullptr;
John Recke1628b72014-05-23 15:11:19 -0700346}
347
John Reck2de950d2017-01-25 10:58:30 -0800348void RenderProxy::destroyHardwareResources() {
John Reckf47a5942014-06-30 16:20:04 -0700349 SETUP_TASK(destroyHardwareResources);
John Recke1628b72014-05-23 15:11:19 -0700350 args->context = mContext;
John Reck51f2d602016-04-06 07:50:47 -0700351 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 }
John Reck34781b22017-07-05 16:39:36 -0700430 if (args->dumpFlags & DumpFlags::JankStats) {
431 args->thread->globalProfileData()->dump(args->fd);
432 }
Chris Craik53e51e42015-06-01 10:35:35 -0700433 if (args->dumpFlags & DumpFlags::Reset) {
John Reckba6adf62015-02-19 14:36:50 -0800434 args->context->resetFrameStats();
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 Reckf1480762016-07-03 18:28:25 -0700459CREATE_BRIDGE2(frameTimePercentile, RenderThread* thread, int percentile) {
460 return reinterpret_cast<void*>(static_cast<uintptr_t>(
John Reck34781b22017-07-05 16:39:36 -0700461 args->thread->globalProfileData()->findPercentile(args->percentile)));
John Reckf1480762016-07-03 18:28:25 -0700462}
463
464uint32_t RenderProxy::frameTimePercentile(int p) {
465 SETUP_TASK(frameTimePercentile);
466 args->thread = &mRenderThread;
467 args->percentile = p;
468 return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
469 postAndWait(task)));
470}
471
John Reckba6adf62015-02-19 14:36:50 -0800472CREATE_BRIDGE2(dumpGraphicsMemory, int fd, RenderThread* thread) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400473 args->thread->dumpGraphicsMemory(args->fd);
Chris Craikd41c4d82015-01-05 15:51:13 -0800474 return nullptr;
John Reck0e89e2b2014-10-31 14:49:06 -0700475}
476
Chris Craik2ae07332015-01-21 14:22:39 -0800477void RenderProxy::dumpGraphicsMemory(int fd) {
youngmin0822.leec80c9ad2015-03-20 21:22:32 +0900478 if (!RenderThread::hasInstance()) return;
Chris Craik2ae07332015-01-21 14:22:39 -0800479 SETUP_TASK(dumpGraphicsMemory);
John Reck0e89e2b2014-10-31 14:49:06 -0700480 args->fd = fd;
John Reckba6adf62015-02-19 14:36:50 -0800481 args->thread = &RenderThread::getInstance();
John Reck0e89e2b2014-10-31 14:49:06 -0700482 staticPostAndWait(task);
483}
484
John Reckedc524c2015-03-18 15:24:33 -0700485CREATE_BRIDGE2(setProcessStatsBuffer, RenderThread* thread, int fd) {
John Reck34781b22017-07-05 16:39:36 -0700486 args->thread->globalProfileData().switchStorageToAshmem(args->fd);
John Reckedc524c2015-03-18 15:24:33 -0700487 close(args->fd);
488 return nullptr;
489}
490
491void RenderProxy::setProcessStatsBuffer(int fd) {
492 SETUP_TASK(setProcessStatsBuffer);
John Reckdf1742e2017-01-19 15:56:21 -0800493 auto& rt = RenderThread::getInstance();
494 args->thread = &rt;
John Reckedc524c2015-03-18 15:24:33 -0700495 args->fd = dup(fd);
John Reckdf1742e2017-01-19 15:56:21 -0800496 rt.queue(task);
497}
498
499CREATE_BRIDGE1(rotateProcessStatsBuffer, RenderThread* thread) {
John Reck34781b22017-07-05 16:39:36 -0700500 args->thread->globalProfileData().rotateStorage();
John Reckdf1742e2017-01-19 15:56:21 -0800501 return nullptr;
502}
503
504void RenderProxy::rotateProcessStatsBuffer() {
505 SETUP_TASK(rotateProcessStatsBuffer);
506 auto& rt = RenderThread::getInstance();
507 args->thread = &rt;
508 rt.queue(task);
John Reckedc524c2015-03-18 15:24:33 -0700509}
510
Tim Murray33eb07f2016-06-10 10:03:20 -0700511int RenderProxy::getRenderThreadTid() {
512 return mRenderThread.getTid();
513}
514
Skuhneea7a7fb2015-08-28 07:10:31 -0700515CREATE_BRIDGE3(addRenderNode, CanvasContext* context, RenderNode* node, bool placeFront) {
516 args->context->addRenderNode(args->node, args->placeFront);
517 return nullptr;
518}
519
520void RenderProxy::addRenderNode(RenderNode* node, bool placeFront) {
521 SETUP_TASK(addRenderNode);
522 args->context = mContext;
523 args->node = node;
524 args->placeFront = placeFront;
525 post(task);
526}
527
528CREATE_BRIDGE2(removeRenderNode, CanvasContext* context, RenderNode* node) {
529 args->context->removeRenderNode(args->node);
530 return nullptr;
531}
532
533void RenderProxy::removeRenderNode(RenderNode* node) {
534 SETUP_TASK(removeRenderNode);
535 args->context = mContext;
536 args->node = node;
537 post(task);
538}
539
540CREATE_BRIDGE2(drawRenderNode, CanvasContext* context, RenderNode* node) {
541 args->context->prepareAndDraw(args->node);
542 return nullptr;
543}
544
545void RenderProxy::drawRenderNode(RenderNode* node) {
546 SETUP_TASK(drawRenderNode);
547 args->context = mContext;
548 args->node = node;
549 // Be pseudo-thread-safe and don't use any member variables
550 staticPostAndWait(task);
551}
552
Skuhneb8160872015-09-22 09:51:39 -0700553CREATE_BRIDGE5(setContentDrawBounds, CanvasContext* context, int left, int top,
Skuhneea7a7fb2015-08-28 07:10:31 -0700554 int right, int bottom) {
Skuhneb8160872015-09-22 09:51:39 -0700555 args->context->setContentDrawBounds(args->left, args->top, args->right, args->bottom);
Skuhneea7a7fb2015-08-28 07:10:31 -0700556 return nullptr;
557}
558
Skuhneb8160872015-09-22 09:51:39 -0700559void RenderProxy::setContentDrawBounds(int left, int top, int right, int bottom) {
560 SETUP_TASK(setContentDrawBounds);
Skuhneea7a7fb2015-08-28 07:10:31 -0700561 args->context = mContext;
562 args->left = left;
563 args->top = top;
564 args->right = right;
565 args->bottom = bottom;
566 staticPostAndWait(task);
567}
568
John Recke248bd12015-08-05 13:53:53 -0700569CREATE_BRIDGE1(serializeDisplayListTree, CanvasContext* context) {
570 args->context->serializeDisplayListTree();
571 return nullptr;
572}
573
574void RenderProxy::serializeDisplayListTree() {
575 SETUP_TASK(serializeDisplayListTree);
576 args->context = mContext;
577 post(task);
578}
579
Andres Morales910beb82016-02-02 16:19:40 -0800580CREATE_BRIDGE2(addFrameMetricsObserver, CanvasContext* context,
581 FrameMetricsObserver* frameStatsObserver) {
582 args->context->addFrameMetricsObserver(args->frameStatsObserver);
Andres Morales06f5bc72015-12-15 15:21:31 -0800583 if (args->frameStatsObserver != nullptr) {
584 args->frameStatsObserver->decStrong(args->context);
585 }
586 return nullptr;
587}
588
Andres Morales910beb82016-02-02 16:19:40 -0800589void RenderProxy::addFrameMetricsObserver(FrameMetricsObserver* observer) {
590 SETUP_TASK(addFrameMetricsObserver);
Andres Morales06f5bc72015-12-15 15:21:31 -0800591 args->context = mContext;
592 args->frameStatsObserver = observer;
593 if (observer != nullptr) {
594 observer->incStrong(mContext);
595 }
596 post(task);
597}
598
Andres Morales910beb82016-02-02 16:19:40 -0800599CREATE_BRIDGE2(removeFrameMetricsObserver, CanvasContext* context,
600 FrameMetricsObserver* frameStatsObserver) {
601 args->context->removeFrameMetricsObserver(args->frameStatsObserver);
Andres Morales06f5bc72015-12-15 15:21:31 -0800602 if (args->frameStatsObserver != nullptr) {
603 args->frameStatsObserver->decStrong(args->context);
604 }
605 return nullptr;
606}
607
Andres Morales910beb82016-02-02 16:19:40 -0800608void RenderProxy::removeFrameMetricsObserver(FrameMetricsObserver* observer) {
609 SETUP_TASK(removeFrameMetricsObserver);
Andres Morales06f5bc72015-12-15 15:21:31 -0800610 args->context = mContext;
611 args->frameStatsObserver = observer;
612 if (observer != nullptr) {
613 observer->incStrong(mContext);
614 }
615 post(task);
616}
617
John Reck95801462016-09-01 09:44:09 -0700618CREATE_BRIDGE4(copySurfaceInto, RenderThread* thread,
619 Surface* surface, Rect srcRect, SkBitmap* bitmap) {
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500620 return (void*)args->thread->readback().copySurfaceInto(*args->surface,
621 args->srcRect, args->bitmap);
John Reck10dd0582016-03-31 16:36:16 -0700622}
623
John Reck95801462016-09-01 09:44:09 -0700624int RenderProxy::copySurfaceInto(sp<Surface>& surface, int left, int top,
625 int right, int bottom, SkBitmap* bitmap) {
John Reck10dd0582016-03-31 16:36:16 -0700626 SETUP_TASK(copySurfaceInto);
627 args->bitmap = bitmap;
628 args->surface = surface.get();
629 args->thread = &RenderThread::getInstance();
John Reck95801462016-09-01 09:44:09 -0700630 args->srcRect.set(left, top, right, bottom);
John Recke94cbc72016-04-25 13:03:44 -0700631 return static_cast<int>(
632 reinterpret_cast<intptr_t>( staticPostAndWait(task) ));
John Reck10dd0582016-03-31 16:36:16 -0700633}
634
sergeyvec4a4b12016-10-20 18:39:04 -0700635CREATE_BRIDGE2(prepareToDraw, RenderThread* thread, Bitmap* bitmap) {
Derek Sollenbergerdaf72292016-10-25 12:09:18 -0400636 CanvasContext::prepareToDraw(*args->thread, args->bitmap);
sergeyvec4a4b12016-10-20 18:39:04 -0700637 args->bitmap->unref();
John Reck43871902016-08-01 14:39:24 -0700638 args->bitmap = nullptr;
639 return nullptr;
640}
641
sergeyvec4a4b12016-10-20 18:39:04 -0700642void RenderProxy::prepareToDraw(Bitmap& bitmap) {
John Reck43871902016-08-01 14:39:24 -0700643 // If we haven't spun up a hardware accelerated window yet, there's no
644 // point in precaching these bitmaps as it can't impact jank.
645 // We also don't know if we even will spin up a hardware-accelerated
646 // window or not.
647 if (!RenderThread::hasInstance()) return;
648 RenderThread* renderThread = &RenderThread::getInstance();
649 SETUP_TASK(prepareToDraw);
650 args->thread = renderThread;
sergeyvec4a4b12016-10-20 18:39:04 -0700651 bitmap.ref();
652 args->bitmap = &bitmap;
John Reck43871902016-08-01 14:39:24 -0700653 nsecs_t lastVsync = renderThread->timeLord().latestVsync();
654 nsecs_t estimatedNextVsync = lastVsync + renderThread->timeLord().frameIntervalNanos();
655 nsecs_t timeToNextVsync = estimatedNextVsync - systemTime(CLOCK_MONOTONIC);
656 // We expect the UI thread to take 4ms and for RT to be active from VSYNC+4ms to
657 // VSYNC+12ms or so, so aim for the gap during which RT is expected to
658 // be idle
659 // TODO: Make this concept a first-class supported thing? RT could use
660 // knowledge of pending draws to better schedule this task
661 if (timeToNextVsync > -6_ms && timeToNextVsync < 1_ms) {
662 renderThread->queueAt(task, estimatedNextVsync + 8_ms);
663 } else {
664 renderThread->queue(task);
665 }
666}
667
sergeyv694d4992016-10-27 10:23:13 -0700668CREATE_BRIDGE2(allocateHardwareBitmap, RenderThread* thread, SkBitmap* bitmap) {
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400669 sk_sp<Bitmap> hardwareBitmap = args->thread->allocateHardwareBitmap(*args->bitmap);
sergeyv694d4992016-10-27 10:23:13 -0700670 return hardwareBitmap.release();
671}
672
673sk_sp<Bitmap> RenderProxy::allocateHardwareBitmap(SkBitmap& bitmap) {
674 SETUP_TASK(allocateHardwareBitmap);
675 args->bitmap = &bitmap;
676 args->thread = &RenderThread::getInstance();
677 sk_sp<Bitmap> hardwareBitmap(reinterpret_cast<Bitmap*>(staticPostAndWait(task)));
678 return hardwareBitmap;
679}
680
sergeyv59eecb522016-11-17 17:54:57 -0800681CREATE_BRIDGE3(copyGraphicBufferInto, RenderThread* thread, GraphicBuffer* buffer, SkBitmap* bitmap) {
682 return (void*) args->thread->readback().copyGraphicBufferInto(args->buffer, args->bitmap);
683}
684
685int RenderProxy::copyGraphicBufferInto(GraphicBuffer* buffer, SkBitmap* bitmap) {
Stan Iliev6983bc42017-02-02 14:11:53 -0500686 RenderThread& thread = RenderThread::getInstance();
687 if (Properties::isSkiaEnabled() && gettid() == thread.getTid()) {
688 //TODO: fix everything that hits this. We should never be triggering a readback ourselves.
689 return (int) thread.readback().copyGraphicBufferInto(buffer, bitmap);
690 } else {
691 SETUP_TASK(copyGraphicBufferInto);
692 args->thread = &thread;
693 args->bitmap = bitmap;
694 args->buffer = buffer;
695 return static_cast<int>(reinterpret_cast<intptr_t>(staticPostAndWait(task)));
696 }
sergeyv59eecb522016-11-17 17:54:57 -0800697}
698
John Reck9a814872017-05-22 15:04:21 -0700699CREATE_BRIDGE2(onBitmapDestroyed, RenderThread* thread, uint32_t pixelRefId) {
700 args->thread->renderState().onBitmapDestroyed(args->pixelRefId);
701 return nullptr;
702}
703
704void RenderProxy::onBitmapDestroyed(uint32_t pixelRefId) {
705 if (!RenderThread::hasInstance()) return;
706 SETUP_TASK(onBitmapDestroyed);
707 RenderThread& thread = RenderThread::getInstance();
708 args->thread = &thread;
709 args->pixelRefId = pixelRefId;
710 thread.queue(task);
711}
712
John Recka8963062017-06-14 10:47:50 -0700713void RenderProxy::disableVsync() {
714 Properties::disableVsync = true;
715}
716
John Reck4f02bf42014-01-03 18:09:17 -0800717void RenderProxy::post(RenderTask* task) {
718 mRenderThread.queue(task);
719}
720
721void* RenderProxy::postAndWait(MethodInvokeRenderTask* task) {
722 void* retval;
723 task->setReturnPtr(&retval);
John Reckcba287b2015-11-10 12:52:44 -0800724 SignalingRenderTask syncTask(task, &mSyncMutex, &mSyncCondition);
725 AutoMutex _lock(mSyncMutex);
726 mRenderThread.queue(&syncTask);
Tom Cherry298a1462017-02-28 14:07:09 -0800727 while (!syncTask.hasRun()) {
728 mSyncCondition.wait(mSyncMutex);
729 }
John Reck4f02bf42014-01-03 18:09:17 -0800730 return retval;
731}
732
John Reck0e89e2b2014-10-31 14:49:06 -0700733void* RenderProxy::staticPostAndWait(MethodInvokeRenderTask* task) {
734 RenderThread& thread = RenderThread::getInstance();
Stan Iliev6983bc42017-02-02 14:11:53 -0500735 LOG_ALWAYS_FATAL_IF(gettid() == thread.getTid());
John Reck0e89e2b2014-10-31 14:49:06 -0700736 void* retval;
737 task->setReturnPtr(&retval);
Chris Craik0a24b142015-10-19 17:10:19 -0700738 thread.queueAndWait(task);
John Reck0e89e2b2014-10-31 14:49:06 -0700739 return retval;
740}
741
John Reck4f02bf42014-01-03 18:09:17 -0800742} /* namespace renderthread */
743} /* namespace uirenderer */
744} /* namespace android */