blob: f4a4773b29372ec8b3aa68cfac66388c826ec5fd [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 Reck10dd0582016-03-31 16:36:16 -070021#include "Readback.h"
John Reckba6adf62015-02-19 14:36:50 -080022#include "Rect.h"
23#include "renderthread/CanvasContext.h"
John Reck43871902016-08-01 14:39:24 -070024#include "renderthread/EglManager.h"
John Reckba6adf62015-02-19 14:36:50 -080025#include "renderthread/RenderTask.h"
26#include "renderthread/RenderThread.h"
27#include "utils/Macros.h"
John Reck43871902016-08-01 14:39:24 -070028#include "utils/TimeUtils.h"
John Reck4f02bf42014-01-03 18:09:17 -080029
sergeyv59eecb522016-11-17 17:54:57 -080030#include <ui/GraphicBuffer.h>
31
John Reck4f02bf42014-01-03 18:09:17 -080032namespace android {
33namespace uirenderer {
34namespace renderthread {
35
36#define ARGS(method) method ## Args
37
John Reck19b6bcf2014-02-14 20:03:38 -080038#define CREATE_BRIDGE0(name) CREATE_BRIDGE(name,,,,,,,,)
John Reck4f02bf42014-01-03 18:09:17 -080039#define CREATE_BRIDGE1(name, a1) CREATE_BRIDGE(name, a1,,,,,,,)
40#define CREATE_BRIDGE2(name, a1, a2) CREATE_BRIDGE(name, a1,a2,,,,,,)
41#define CREATE_BRIDGE3(name, a1, a2, a3) CREATE_BRIDGE(name, a1,a2,a3,,,,,)
42#define CREATE_BRIDGE4(name, a1, a2, a3, a4) CREATE_BRIDGE(name, a1,a2,a3,a4,,,,)
Chris Craik797b95b2014-05-20 18:10:25 -070043#define CREATE_BRIDGE5(name, a1, a2, a3, a4, a5) CREATE_BRIDGE(name, a1,a2,a3,a4,a5,,,)
Chris Craik058fc642014-07-23 18:19:28 -070044#define CREATE_BRIDGE6(name, a1, a2, a3, a4, a5, a6) CREATE_BRIDGE(name, a1,a2,a3,a4,a5,a6,,)
45#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 -080046#define CREATE_BRIDGE(name, a1, a2, a3, a4, a5, a6, a7, a8) \
47 typedef struct { \
48 a1; a2; a3; a4; a5; a6; a7; a8; \
49 } ARGS(name); \
John Reck43871902016-08-01 14:39:24 -070050 static_assert(std::is_trivially_destructible<ARGS(name)>::value, \
51 "Error, ARGS must be trivially destructible!"); \
John Reck4f02bf42014-01-03 18:09:17 -080052 static void* Bridge_ ## name(ARGS(name)* args)
53
54#define SETUP_TASK(method) \
55 LOG_ALWAYS_FATAL_IF( METHOD_INVOKE_PAYLOAD_SIZE < sizeof(ARGS(method)), \
Mark Salyzyn546f3532014-06-10 12:29:14 -070056 "METHOD_INVOKE_PAYLOAD_SIZE %zu is smaller than sizeof(" #method "Args) %zu", \
John Reck4f02bf42014-01-03 18:09:17 -080057 METHOD_INVOKE_PAYLOAD_SIZE, sizeof(ARGS(method))); \
John Recke2c45522014-04-07 17:31:44 -070058 MethodInvokeRenderTask* task = new MethodInvokeRenderTask((RunnableMethod) Bridge_ ## method); \
John Reck4f02bf42014-01-03 18:09:17 -080059 ARGS(method) *args = (ARGS(method) *) task->payload()
60
John Reck119907c2014-08-14 09:02:01 -070061CREATE_BRIDGE4(createContext, RenderThread* thread, bool translucent,
62 RenderNode* rootRenderNode, IContextFactory* contextFactory) {
Stan Iliev03de0742016-07-07 12:35:54 -040063 return CanvasContext::create(*args->thread, args->translucent,
John Reck119907c2014-08-14 09:02:01 -070064 args->rootRenderNode, args->contextFactory);
John Reck4f02bf42014-01-03 18:09:17 -080065}
66
John Reck119907c2014-08-14 09:02:01 -070067RenderProxy::RenderProxy(bool translucent, RenderNode* rootRenderNode, IContextFactory* contextFactory)
John Reck4f02bf42014-01-03 18:09:17 -080068 : mRenderThread(RenderThread::getInstance())
Chris Craikd41c4d82015-01-05 15:51:13 -080069 , mContext(nullptr) {
John Reck4f02bf42014-01-03 18:09:17 -080070 SETUP_TASK(createContext);
71 args->translucent = translucent;
John Recke45b1fd2014-04-15 09:50:16 -070072 args->rootRenderNode = rootRenderNode;
John Reck3b202512014-06-23 13:13:08 -070073 args->thread = &mRenderThread;
John Reck119907c2014-08-14 09:02:01 -070074 args->contextFactory = contextFactory;
John Reck4f02bf42014-01-03 18:09:17 -080075 mContext = (CanvasContext*) postAndWait(task);
Skuhneea7a7fb2015-08-28 07:10:31 -070076 mDrawFrameTask.setContext(&mRenderThread, mContext, rootRenderNode);
John Reck4f02bf42014-01-03 18:09:17 -080077}
78
79RenderProxy::~RenderProxy() {
80 destroyContext();
81}
82
83CREATE_BRIDGE1(destroyContext, CanvasContext* context) {
84 delete args->context;
Chris Craikd41c4d82015-01-05 15:51:13 -080085 return nullptr;
John Reck4f02bf42014-01-03 18:09:17 -080086}
87
88void RenderProxy::destroyContext() {
89 if (mContext) {
90 SETUP_TASK(destroyContext);
91 args->context = mContext;
Chris Craikd41c4d82015-01-05 15:51:13 -080092 mContext = nullptr;
Skuhneea7a7fb2015-08-28 07:10:31 -070093 mDrawFrameTask.setContext(nullptr, nullptr, nullptr);
John Reck668f0e32014-03-26 15:10:40 -070094 // This is also a fence as we need to be certain that there are no
95 // outstanding mDrawFrame tasks posted before it is destroyed
96 postAndWait(task);
John Reck4f02bf42014-01-03 18:09:17 -080097 }
98}
99
John Reck1125d1f2014-10-23 11:02:19 -0700100CREATE_BRIDGE2(setSwapBehavior, CanvasContext* context, SwapBehavior swapBehavior) {
101 args->context->setSwapBehavior(args->swapBehavior);
Chris Craikd41c4d82015-01-05 15:51:13 -0800102 return nullptr;
John Reck1125d1f2014-10-23 11:02:19 -0700103}
104
105void RenderProxy::setSwapBehavior(SwapBehavior swapBehavior) {
106 SETUP_TASK(setSwapBehavior);
107 args->context = mContext;
108 args->swapBehavior = swapBehavior;
109 post(task);
110}
111
John Reckfe5e7b72014-05-23 17:42:28 -0700112CREATE_BRIDGE1(loadSystemProperties, CanvasContext* context) {
John Recke4280ba2014-05-05 16:39:37 -0700113 bool needsRedraw = false;
114 if (Caches::hasInstance()) {
Chris Craik2507c342015-05-04 14:36:49 -0700115 needsRedraw = Properties::load();
John Recke4280ba2014-05-05 16:39:37 -0700116 }
Chris Craik2507c342015-05-04 14:36:49 -0700117 if (args->context->profiler().consumeProperties()) {
John Reckfe5e7b72014-05-23 17:42:28 -0700118 needsRedraw = true;
119 }
John Recke4280ba2014-05-05 16:39:37 -0700120 return (void*) needsRedraw;
121}
122
123bool RenderProxy::loadSystemProperties() {
124 SETUP_TASK(loadSystemProperties);
John Reckfe5e7b72014-05-23 17:42:28 -0700125 args->context = mContext;
John Recke4280ba2014-05-05 16:39:37 -0700126 return (bool) postAndWait(task);
127}
128
John Reckb36016c2015-03-11 08:50:53 -0700129CREATE_BRIDGE2(setName, CanvasContext* context, const char* name) {
130 args->context->setName(std::string(args->name));
131 return nullptr;
132}
133
134void RenderProxy::setName(const char* name) {
135 SETUP_TASK(setName);
136 args->context = mContext;
137 args->name = name;
Chris Craik2507c342015-05-04 14:36:49 -0700138 postAndWait(task); // block since name/value pointers owned by caller
John Reckb36016c2015-03-11 08:50:53 -0700139}
140
John Reckf6481082016-02-02 15:18:23 -0800141CREATE_BRIDGE2(initialize, CanvasContext* context, Surface* surface) {
142 args->context->initialize(args->surface);
Thomas Buhot0bcd0cb2015-12-04 12:18:03 +0100143 return nullptr;
John Reck4f02bf42014-01-03 18:09:17 -0800144}
145
John Reckf6481082016-02-02 15:18:23 -0800146void RenderProxy::initialize(const sp<Surface>& surface) {
John Reck4f02bf42014-01-03 18:09:17 -0800147 SETUP_TASK(initialize);
148 args->context = mContext;
John Reckf6481082016-02-02 15:18:23 -0800149 args->surface = surface.get();
Thomas Buhot0bcd0cb2015-12-04 12:18:03 +0100150 post(task);
John Reck4f02bf42014-01-03 18:09:17 -0800151}
152
John Reckf6481082016-02-02 15:18:23 -0800153CREATE_BRIDGE2(updateSurface, CanvasContext* context, Surface* surface) {
154 args->context->updateSurface(args->surface);
Chris Craikd41c4d82015-01-05 15:51:13 -0800155 return nullptr;
John Reck4f02bf42014-01-03 18:09:17 -0800156}
157
John Reckf6481082016-02-02 15:18:23 -0800158void RenderProxy::updateSurface(const sp<Surface>& surface) {
John Reck4f02bf42014-01-03 18:09:17 -0800159 SETUP_TASK(updateSurface);
160 args->context = mContext;
John Reckf6481082016-02-02 15:18:23 -0800161 args->surface = surface.get();
John Reckcd682122016-08-09 12:09:03 -0700162 post(task);
John Reckf7d9c1d2014-04-09 10:01:03 -0700163}
164
John Reckf6481082016-02-02 15:18:23 -0800165CREATE_BRIDGE2(pauseSurface, CanvasContext* context, Surface* surface) {
166 return (void*) args->context->pauseSurface(args->surface);
John Reckf7d9c1d2014-04-09 10:01:03 -0700167}
168
John Reckf6481082016-02-02 15:18:23 -0800169bool RenderProxy::pauseSurface(const sp<Surface>& surface) {
John Reckf7d9c1d2014-04-09 10:01:03 -0700170 SETUP_TASK(pauseSurface);
171 args->context = mContext;
John Reckf6481082016-02-02 15:18:23 -0800172 args->surface = surface.get();
John Reck01a5ea32014-12-03 13:01:07 -0800173 return (bool) postAndWait(task);
John Reck4f02bf42014-01-03 18:09:17 -0800174}
175
John Reck8afcc762016-04-13 10:24:06 -0700176CREATE_BRIDGE2(setStopped, CanvasContext* context, bool stopped) {
177 args->context->setStopped(args->stopped);
178 return nullptr;
179}
180
181void RenderProxy::setStopped(bool stopped) {
182 SETUP_TASK(setStopped);
183 args->context = mContext;
184 args->stopped = stopped;
185 postAndWait(task);
186}
187
John Reckab1080c2016-06-21 16:24:20 -0700188CREATE_BRIDGE4(setup, CanvasContext* context,
Alan Viverette50210d92015-05-14 18:05:36 -0700189 float lightRadius, uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
John Reckab1080c2016-06-21 16:24:20 -0700190 args->context->setup(args->lightRadius,
Chris Craik058fc642014-07-23 18:19:28 -0700191 args->ambientShadowAlpha, args->spotShadowAlpha);
Chris Craikd41c4d82015-01-05 15:51:13 -0800192 return nullptr;
John Reck4f02bf42014-01-03 18:09:17 -0800193}
194
John Reckab1080c2016-06-21 16:24:20 -0700195void RenderProxy::setup(float lightRadius,
John Reckb36016c2015-03-11 08:50:53 -0700196 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
John Reck4f02bf42014-01-03 18:09:17 -0800197 SETUP_TASK(setup);
198 args->context = mContext;
Chris Craik797b95b2014-05-20 18:10:25 -0700199 args->lightRadius = lightRadius;
Chris Craik058fc642014-07-23 18:19:28 -0700200 args->ambientShadowAlpha = ambientShadowAlpha;
201 args->spotShadowAlpha = spotShadowAlpha;
John Reck4f02bf42014-01-03 18:09:17 -0800202 post(task);
203}
204
Alan Viverette50210d92015-05-14 18:05:36 -0700205CREATE_BRIDGE2(setLightCenter, CanvasContext* context, Vector3 lightCenter) {
206 args->context->setLightCenter(args->lightCenter);
207 return nullptr;
208}
209
210void RenderProxy::setLightCenter(const Vector3& lightCenter) {
211 SETUP_TASK(setLightCenter);
212 args->context = mContext;
213 args->lightCenter = lightCenter;
214 post(task);
215}
216
John Reck63a06672014-05-07 13:45:54 -0700217CREATE_BRIDGE2(setOpaque, CanvasContext* context, bool opaque) {
218 args->context->setOpaque(args->opaque);
Chris Craikd41c4d82015-01-05 15:51:13 -0800219 return nullptr;
John Reck63a06672014-05-07 13:45:54 -0700220}
221
222void RenderProxy::setOpaque(bool opaque) {
223 SETUP_TASK(setOpaque);
224 args->context = mContext;
225 args->opaque = opaque;
226 post(task);
227}
228
John Reckba6adf62015-02-19 14:36:50 -0800229int64_t* RenderProxy::frameInfo() {
230 return mDrawFrameTask.frameInfo();
231}
232
John Reck2de950d2017-01-25 10:58:30 -0800233int RenderProxy::syncAndDrawFrame() {
234 return mDrawFrameTask.drawFrame();
John Reck4f02bf42014-01-03 18:09:17 -0800235}
236
John Reck2de950d2017-01-25 10:58:30 -0800237CREATE_BRIDGE1(destroy, CanvasContext* context) {
238 args->context->destroy();
Chris Craikd41c4d82015-01-05 15:51:13 -0800239 return nullptr;
John Reck4f02bf42014-01-03 18:09:17 -0800240}
241
John Reck2de950d2017-01-25 10:58:30 -0800242void RenderProxy::destroy() {
John Reck17035b02014-09-03 07:39:53 -0700243 SETUP_TASK(destroy);
John Reck4f02bf42014-01-03 18:09:17 -0800244 args->context = mContext;
John Reckfae904d2014-04-14 11:01:57 -0700245 // destroyCanvasAndSurface() needs a fence as when it returns the
246 // underlying BufferQueue is going to be released from under
247 // the render thread.
248 postAndWait(task);
John Reck4f02bf42014-01-03 18:09:17 -0800249}
250
John Reck3b202512014-06-23 13:13:08 -0700251CREATE_BRIDGE2(invokeFunctor, RenderThread* thread, Functor* functor) {
252 CanvasContext::invokeFunctor(*args->thread, args->functor);
Chris Craikd41c4d82015-01-05 15:51:13 -0800253 return nullptr;
John Reck0d1f6342014-03-28 20:30:27 -0700254}
255
256void RenderProxy::invokeFunctor(Functor* functor, bool waitForCompletion) {
John Reckd3d8daf2014-04-10 15:00:13 -0700257 ATRACE_CALL();
John Reck3b202512014-06-23 13:13:08 -0700258 RenderThread& thread = RenderThread::getInstance();
John Reck0d1f6342014-03-28 20:30:27 -0700259 SETUP_TASK(invokeFunctor);
John Reck3b202512014-06-23 13:13:08 -0700260 args->thread = &thread;
John Reck0d1f6342014-03-28 20:30:27 -0700261 args->functor = functor;
262 if (waitForCompletion) {
John Reck3b202512014-06-23 13:13:08 -0700263 // waitForCompletion = true is expected to be fairly rare and only
264 // happen in destruction. Thus it should be fine to temporarily
265 // create a Mutex
John Reck0e89e2b2014-10-31 14:49:06 -0700266 staticPostAndWait(task);
John Reck0d1f6342014-03-28 20:30:27 -0700267 } else {
John Reck3b202512014-06-23 13:13:08 -0700268 thread.queue(task);
John Reck0d1f6342014-03-28 20:30:27 -0700269 }
270}
271
John Reckc36df952015-07-29 10:09:36 -0700272CREATE_BRIDGE1(createTextureLayer, CanvasContext* context) {
Derek Sollenberger56ad6ec2016-07-22 12:13:32 -0400273 return args->context->createTextureLayer();
John Reck19b6bcf2014-02-14 20:03:38 -0800274}
275
276DeferredLayerUpdater* RenderProxy::createTextureLayer() {
277 SETUP_TASK(createTextureLayer);
John Reck1949e792014-04-08 15:18:56 -0700278 args->context = mContext;
John Reck19b6bcf2014-02-14 20:03:38 -0800279 void* retval = postAndWait(task);
280 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval);
John Reck19b6bcf2014-02-14 20:03:38 -0800281 return layer;
282}
283
John Reck2de950d2017-01-25 10:58:30 -0800284CREATE_BRIDGE2(buildLayer, CanvasContext* context, RenderNode* node) {
285 args->context->buildLayer(args->node);
Chris Craikd41c4d82015-01-05 15:51:13 -0800286 return nullptr;
John Reck3e824952014-08-20 10:08:39 -0700287}
288
John Reck2de950d2017-01-25 10:58:30 -0800289void RenderProxy::buildLayer(RenderNode* node) {
John Reck3e824952014-08-20 10:08:39 -0700290 SETUP_TASK(buildLayer);
291 args->context = mContext;
292 args->node = node;
293 postAndWait(task);
294}
295
John Reck19b6bcf2014-02-14 20:03:38 -0800296CREATE_BRIDGE3(copyLayerInto, CanvasContext* context, DeferredLayerUpdater* layer,
297 SkBitmap* bitmap) {
298 bool success = args->context->copyLayerInto(args->layer, args->bitmap);
299 return (void*) success;
300}
301
John Reck3731dc22015-04-13 15:20:29 -0700302bool RenderProxy::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap& bitmap) {
John Reck19b6bcf2014-02-14 20:03:38 -0800303 SETUP_TASK(copyLayerInto);
304 args->context = mContext;
305 args->layer = layer;
John Reck3731dc22015-04-13 15:20:29 -0700306 args->bitmap = &bitmap;
John Reck19b6bcf2014-02-14 20:03:38 -0800307 return (bool) postAndWait(task);
308}
309
John Reckd72e0a32014-05-29 18:56:11 -0700310void RenderProxy::pushLayerUpdate(DeferredLayerUpdater* layer) {
311 mDrawFrameTask.pushLayerUpdate(layer);
312}
313
314void RenderProxy::cancelLayerUpdate(DeferredLayerUpdater* layer) {
315 mDrawFrameTask.removeLayerUpdate(layer);
John Reck19b6bcf2014-02-14 20:03:38 -0800316}
317
John Reck918ad522014-06-27 14:45:25 -0700318CREATE_BRIDGE1(detachSurfaceTexture, DeferredLayerUpdater* layer) {
319 args->layer->detachSurfaceTexture();
Chris Craikd41c4d82015-01-05 15:51:13 -0800320 return nullptr;
John Reck918ad522014-06-27 14:45:25 -0700321}
322
323void RenderProxy::detachSurfaceTexture(DeferredLayerUpdater* layer) {
324 SETUP_TASK(detachSurfaceTexture);
325 args->layer = layer;
326 postAndWait(task);
327}
328
John Reck2de950d2017-01-25 10:58:30 -0800329CREATE_BRIDGE1(destroyHardwareResources, CanvasContext* context) {
330 args->context->destroyHardwareResources();
Chris Craikd41c4d82015-01-05 15:51:13 -0800331 return nullptr;
John Recke1628b72014-05-23 15:11:19 -0700332}
333
John Reck2de950d2017-01-25 10:58:30 -0800334void RenderProxy::destroyHardwareResources() {
John Reckf47a5942014-06-30 16:20:04 -0700335 SETUP_TASK(destroyHardwareResources);
John Recke1628b72014-05-23 15:11:19 -0700336 args->context = mContext;
John Reck51f2d602016-04-06 07:50:47 -0700337 postAndWait(task);
John Recke1628b72014-05-23 15:11:19 -0700338}
339
Chris Craik2507c342015-05-04 14:36:49 -0700340CREATE_BRIDGE2(trimMemory, RenderThread* thread, int level) {
John Reckf47a5942014-06-30 16:20:04 -0700341 CanvasContext::trimMemory(*args->thread, args->level);
Chris Craikd41c4d82015-01-05 15:51:13 -0800342 return nullptr;
John Reckf47a5942014-06-30 16:20:04 -0700343}
344
345void RenderProxy::trimMemory(int level) {
John Reckcd3a22c2014-08-06 13:33:59 -0700346 // Avoid creating a RenderThread to do a trimMemory.
347 if (RenderThread::hasInstance()) {
348 RenderThread& thread = RenderThread::getInstance();
Chris Craik2507c342015-05-04 14:36:49 -0700349 SETUP_TASK(trimMemory);
John Reckcd3a22c2014-08-06 13:33:59 -0700350 args->thread = &thread;
351 args->level = level;
352 thread.queue(task);
353 }
John Reckf47a5942014-06-30 16:20:04 -0700354}
355
Chris Craik2507c342015-05-04 14:36:49 -0700356CREATE_BRIDGE2(overrideProperty, const char* name, const char* value) {
357 Properties::overrideProperty(args->name, args->value);
358 return nullptr;
359}
360
361void RenderProxy::overrideProperty(const char* name, const char* value) {
Chris Craik2507c342015-05-04 14:36:49 -0700362 SETUP_TASK(overrideProperty);
363 args->name = name;
364 args->value = value;
365 staticPostAndWait(task); // expensive, but block here since name/value pointers owned by caller
366}
367
John Reck28ad7b52014-04-07 16:59:25 -0700368CREATE_BRIDGE0(fence) {
369 // Intentionally empty
Chris Craikd41c4d82015-01-05 15:51:13 -0800370 return nullptr;
John Reck28ad7b52014-04-07 16:59:25 -0700371}
372
Andreas Gampe64bb4132014-11-22 00:35:09 +0000373template <typename T>
374void UNUSED(T t) {}
375
John Reck28ad7b52014-04-07 16:59:25 -0700376void RenderProxy::fence() {
377 SETUP_TASK(fence);
Andreas Gampe1e196742014-11-10 15:23:43 -0800378 UNUSED(args);
John Reck28ad7b52014-04-07 16:59:25 -0700379 postAndWait(task);
380}
381
Thomas Buhotc0a0e1a2016-01-18 10:31:58 +0100382void RenderProxy::staticFence() {
383 SETUP_TASK(fence);
384 UNUSED(args);
385 staticPostAndWait(task);
386}
387
John Reckf47a5942014-06-30 16:20:04 -0700388CREATE_BRIDGE1(stopDrawing, CanvasContext* context) {
389 args->context->stopDrawing();
Chris Craikd41c4d82015-01-05 15:51:13 -0800390 return nullptr;
John Reckf47a5942014-06-30 16:20:04 -0700391}
392
393void RenderProxy::stopDrawing() {
394 SETUP_TASK(stopDrawing);
395 args->context = mContext;
396 postAndWait(task);
397}
398
John Recka5dda642014-05-22 15:43:54 -0700399CREATE_BRIDGE1(notifyFramePending, CanvasContext* context) {
400 args->context->notifyFramePending();
Chris Craikd41c4d82015-01-05 15:51:13 -0800401 return nullptr;
John Recka5dda642014-05-22 15:43:54 -0700402}
403
404void RenderProxy::notifyFramePending() {
405 SETUP_TASK(notifyFramePending);
406 args->context = mContext;
407 mRenderThread.queueAtFront(task);
408}
409
John Reck7f2e5e32015-05-05 11:00:53 -0700410CREATE_BRIDGE4(dumpProfileInfo, CanvasContext* context, RenderThread* thread,
411 int fd, int dumpFlags) {
John Reckfe5e7b72014-05-23 17:42:28 -0700412 args->context->profiler().dumpData(args->fd);
Chris Craik53e51e42015-06-01 10:35:35 -0700413 if (args->dumpFlags & DumpFlags::FrameStats) {
John Reckba6adf62015-02-19 14:36:50 -0800414 args->context->dumpFrames(args->fd);
415 }
Chris Craik53e51e42015-06-01 10:35:35 -0700416 if (args->dumpFlags & DumpFlags::Reset) {
John Reckba6adf62015-02-19 14:36:50 -0800417 args->context->resetFrameStats();
418 }
John Recka41f2442016-04-07 16:36:57 -0700419 if (args->dumpFlags & DumpFlags::JankStats) {
420 args->thread->jankTracker().dump(args->fd);
421 }
Chris Craikd41c4d82015-01-05 15:51:13 -0800422 return nullptr;
John Reckfe5e7b72014-05-23 17:42:28 -0700423}
424
John Reckba6adf62015-02-19 14:36:50 -0800425void RenderProxy::dumpProfileInfo(int fd, int dumpFlags) {
John Reckfe5e7b72014-05-23 17:42:28 -0700426 SETUP_TASK(dumpProfileInfo);
427 args->context = mContext;
John Reck7f2e5e32015-05-05 11:00:53 -0700428 args->thread = &mRenderThread;
John Reckfe5e7b72014-05-23 17:42:28 -0700429 args->fd = fd;
John Reckba6adf62015-02-19 14:36:50 -0800430 args->dumpFlags = dumpFlags;
John Reckfe5e7b72014-05-23 17:42:28 -0700431 postAndWait(task);
432}
433
John Reck7f2e5e32015-05-05 11:00:53 -0700434CREATE_BRIDGE1(resetProfileInfo, CanvasContext* context) {
435 args->context->resetFrameStats();
436 return nullptr;
437}
438
439void RenderProxy::resetProfileInfo() {
440 SETUP_TASK(resetProfileInfo);
441 args->context = mContext;
442 postAndWait(task);
443}
444
John Reckf1480762016-07-03 18:28:25 -0700445CREATE_BRIDGE2(frameTimePercentile, RenderThread* thread, int percentile) {
446 return reinterpret_cast<void*>(static_cast<uintptr_t>(
447 args->thread->jankTracker().findPercentile(args->percentile)));
448}
449
450uint32_t RenderProxy::frameTimePercentile(int p) {
451 SETUP_TASK(frameTimePercentile);
452 args->thread = &mRenderThread;
453 args->percentile = p;
454 return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
455 postAndWait(task)));
456}
457
John Reckba6adf62015-02-19 14:36:50 -0800458CREATE_BRIDGE2(dumpGraphicsMemory, int fd, RenderThread* thread) {
459 args->thread->jankTracker().dump(args->fd);
460
Chris Craik2ae07332015-01-21 14:22:39 -0800461 FILE *file = fdopen(args->fd, "a");
462 if (Caches::hasInstance()) {
463 String8 cachesLog;
464 Caches::getInstance().dumpMemoryUsage(cachesLog);
465 fprintf(file, "\nCaches:\n%s\n", cachesLog.string());
466 } else {
467 fprintf(file, "\nNo caches instance.\n");
468 }
Chris Craikff3edce2016-01-14 10:04:08 -0800469 fprintf(file, "\nPipeline=FrameBuilder\n");
Chris Craik2ae07332015-01-21 14:22:39 -0800470 fflush(file);
Chris Craikd41c4d82015-01-05 15:51:13 -0800471 return nullptr;
John Reck0e89e2b2014-10-31 14:49:06 -0700472}
473
Chris Craik2ae07332015-01-21 14:22:39 -0800474void RenderProxy::dumpGraphicsMemory(int fd) {
youngmin0822.leec80c9ad2015-03-20 21:22:32 +0900475 if (!RenderThread::hasInstance()) return;
Chris Craik2ae07332015-01-21 14:22:39 -0800476 SETUP_TASK(dumpGraphicsMemory);
John Reck0e89e2b2014-10-31 14:49:06 -0700477 args->fd = fd;
John Reckba6adf62015-02-19 14:36:50 -0800478 args->thread = &RenderThread::getInstance();
John Reck0e89e2b2014-10-31 14:49:06 -0700479 staticPostAndWait(task);
480}
481
John Reckedc524c2015-03-18 15:24:33 -0700482CREATE_BRIDGE2(setProcessStatsBuffer, RenderThread* thread, int fd) {
483 args->thread->jankTracker().switchStorageToAshmem(args->fd);
484 close(args->fd);
485 return nullptr;
486}
487
488void RenderProxy::setProcessStatsBuffer(int fd) {
489 SETUP_TASK(setProcessStatsBuffer);
John Reckdf1742e2017-01-19 15:56:21 -0800490 auto& rt = RenderThread::getInstance();
491 args->thread = &rt;
John Reckedc524c2015-03-18 15:24:33 -0700492 args->fd = dup(fd);
John Reckdf1742e2017-01-19 15:56:21 -0800493 rt.queue(task);
494}
495
496CREATE_BRIDGE1(rotateProcessStatsBuffer, RenderThread* thread) {
497 args->thread->jankTracker().rotateStorage();
498 return nullptr;
499}
500
501void RenderProxy::rotateProcessStatsBuffer() {
502 SETUP_TASK(rotateProcessStatsBuffer);
503 auto& rt = RenderThread::getInstance();
504 args->thread = &rt;
505 rt.queue(task);
John Reckedc524c2015-03-18 15:24:33 -0700506}
507
Tim Murray33eb07f2016-06-10 10:03:20 -0700508int RenderProxy::getRenderThreadTid() {
509 return mRenderThread.getTid();
510}
511
Skuhneea7a7fb2015-08-28 07:10:31 -0700512CREATE_BRIDGE3(addRenderNode, CanvasContext* context, RenderNode* node, bool placeFront) {
513 args->context->addRenderNode(args->node, args->placeFront);
514 return nullptr;
515}
516
517void RenderProxy::addRenderNode(RenderNode* node, bool placeFront) {
518 SETUP_TASK(addRenderNode);
519 args->context = mContext;
520 args->node = node;
521 args->placeFront = placeFront;
522 post(task);
523}
524
525CREATE_BRIDGE2(removeRenderNode, CanvasContext* context, RenderNode* node) {
526 args->context->removeRenderNode(args->node);
527 return nullptr;
528}
529
530void RenderProxy::removeRenderNode(RenderNode* node) {
531 SETUP_TASK(removeRenderNode);
532 args->context = mContext;
533 args->node = node;
534 post(task);
535}
536
537CREATE_BRIDGE2(drawRenderNode, CanvasContext* context, RenderNode* node) {
538 args->context->prepareAndDraw(args->node);
539 return nullptr;
540}
541
542void RenderProxy::drawRenderNode(RenderNode* node) {
543 SETUP_TASK(drawRenderNode);
544 args->context = mContext;
545 args->node = node;
546 // Be pseudo-thread-safe and don't use any member variables
547 staticPostAndWait(task);
548}
549
Skuhneb8160872015-09-22 09:51:39 -0700550CREATE_BRIDGE5(setContentDrawBounds, CanvasContext* context, int left, int top,
Skuhneea7a7fb2015-08-28 07:10:31 -0700551 int right, int bottom) {
Skuhneb8160872015-09-22 09:51:39 -0700552 args->context->setContentDrawBounds(args->left, args->top, args->right, args->bottom);
Skuhneea7a7fb2015-08-28 07:10:31 -0700553 return nullptr;
554}
555
Skuhneb8160872015-09-22 09:51:39 -0700556void RenderProxy::setContentDrawBounds(int left, int top, int right, int bottom) {
557 SETUP_TASK(setContentDrawBounds);
Skuhneea7a7fb2015-08-28 07:10:31 -0700558 args->context = mContext;
559 args->left = left;
560 args->top = top;
561 args->right = right;
562 args->bottom = bottom;
563 staticPostAndWait(task);
564}
565
John Recke248bd12015-08-05 13:53:53 -0700566CREATE_BRIDGE1(serializeDisplayListTree, CanvasContext* context) {
567 args->context->serializeDisplayListTree();
568 return nullptr;
569}
570
571void RenderProxy::serializeDisplayListTree() {
572 SETUP_TASK(serializeDisplayListTree);
573 args->context = mContext;
574 post(task);
575}
576
Andres Morales910beb82016-02-02 16:19:40 -0800577CREATE_BRIDGE2(addFrameMetricsObserver, CanvasContext* context,
578 FrameMetricsObserver* frameStatsObserver) {
579 args->context->addFrameMetricsObserver(args->frameStatsObserver);
Andres Morales06f5bc72015-12-15 15:21:31 -0800580 if (args->frameStatsObserver != nullptr) {
581 args->frameStatsObserver->decStrong(args->context);
582 }
583 return nullptr;
584}
585
Andres Morales910beb82016-02-02 16:19:40 -0800586void RenderProxy::addFrameMetricsObserver(FrameMetricsObserver* observer) {
587 SETUP_TASK(addFrameMetricsObserver);
Andres Morales06f5bc72015-12-15 15:21:31 -0800588 args->context = mContext;
589 args->frameStatsObserver = observer;
590 if (observer != nullptr) {
591 observer->incStrong(mContext);
592 }
593 post(task);
594}
595
Andres Morales910beb82016-02-02 16:19:40 -0800596CREATE_BRIDGE2(removeFrameMetricsObserver, CanvasContext* context,
597 FrameMetricsObserver* frameStatsObserver) {
598 args->context->removeFrameMetricsObserver(args->frameStatsObserver);
Andres Morales06f5bc72015-12-15 15:21:31 -0800599 if (args->frameStatsObserver != nullptr) {
600 args->frameStatsObserver->decStrong(args->context);
601 }
602 return nullptr;
603}
604
Andres Morales910beb82016-02-02 16:19:40 -0800605void RenderProxy::removeFrameMetricsObserver(FrameMetricsObserver* observer) {
606 SETUP_TASK(removeFrameMetricsObserver);
Andres Morales06f5bc72015-12-15 15:21:31 -0800607 args->context = mContext;
608 args->frameStatsObserver = observer;
609 if (observer != nullptr) {
610 observer->incStrong(mContext);
611 }
612 post(task);
613}
614
John Reck95801462016-09-01 09:44:09 -0700615CREATE_BRIDGE4(copySurfaceInto, RenderThread* thread,
616 Surface* surface, Rect srcRect, SkBitmap* bitmap) {
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500617 return (void*)args->thread->readback().copySurfaceInto(*args->surface,
618 args->srcRect, args->bitmap);
John Reck10dd0582016-03-31 16:36:16 -0700619}
620
John Reck95801462016-09-01 09:44:09 -0700621int RenderProxy::copySurfaceInto(sp<Surface>& surface, int left, int top,
622 int right, int bottom, SkBitmap* bitmap) {
John Reck10dd0582016-03-31 16:36:16 -0700623 SETUP_TASK(copySurfaceInto);
624 args->bitmap = bitmap;
625 args->surface = surface.get();
626 args->thread = &RenderThread::getInstance();
John Reck95801462016-09-01 09:44:09 -0700627 args->srcRect.set(left, top, right, bottom);
John Recke94cbc72016-04-25 13:03:44 -0700628 return static_cast<int>(
629 reinterpret_cast<intptr_t>( staticPostAndWait(task) ));
John Reck10dd0582016-03-31 16:36:16 -0700630}
631
sergeyvec4a4b12016-10-20 18:39:04 -0700632CREATE_BRIDGE2(prepareToDraw, RenderThread* thread, Bitmap* bitmap) {
Derek Sollenbergerdaf72292016-10-25 12:09:18 -0400633 CanvasContext::prepareToDraw(*args->thread, args->bitmap);
sergeyvec4a4b12016-10-20 18:39:04 -0700634 args->bitmap->unref();
John Reck43871902016-08-01 14:39:24 -0700635 args->bitmap = nullptr;
636 return nullptr;
637}
638
sergeyvec4a4b12016-10-20 18:39:04 -0700639void RenderProxy::prepareToDraw(Bitmap& bitmap) {
John Reck43871902016-08-01 14:39:24 -0700640 // If we haven't spun up a hardware accelerated window yet, there's no
641 // point in precaching these bitmaps as it can't impact jank.
642 // We also don't know if we even will spin up a hardware-accelerated
643 // window or not.
644 if (!RenderThread::hasInstance()) return;
645 RenderThread* renderThread = &RenderThread::getInstance();
646 SETUP_TASK(prepareToDraw);
647 args->thread = renderThread;
sergeyvec4a4b12016-10-20 18:39:04 -0700648 bitmap.ref();
649 args->bitmap = &bitmap;
John Reck43871902016-08-01 14:39:24 -0700650 nsecs_t lastVsync = renderThread->timeLord().latestVsync();
651 nsecs_t estimatedNextVsync = lastVsync + renderThread->timeLord().frameIntervalNanos();
652 nsecs_t timeToNextVsync = estimatedNextVsync - systemTime(CLOCK_MONOTONIC);
653 // We expect the UI thread to take 4ms and for RT to be active from VSYNC+4ms to
654 // VSYNC+12ms or so, so aim for the gap during which RT is expected to
655 // be idle
656 // TODO: Make this concept a first-class supported thing? RT could use
657 // knowledge of pending draws to better schedule this task
658 if (timeToNextVsync > -6_ms && timeToNextVsync < 1_ms) {
659 renderThread->queueAt(task, estimatedNextVsync + 8_ms);
660 } else {
661 renderThread->queue(task);
662 }
663}
664
sergeyv694d4992016-10-27 10:23:13 -0700665CREATE_BRIDGE2(allocateHardwareBitmap, RenderThread* thread, SkBitmap* bitmap) {
666 sk_sp<Bitmap> hardwareBitmap = Bitmap::allocateHardwareBitmap(*args->thread, *args->bitmap);
667 return hardwareBitmap.release();
668}
669
670sk_sp<Bitmap> RenderProxy::allocateHardwareBitmap(SkBitmap& bitmap) {
671 SETUP_TASK(allocateHardwareBitmap);
672 args->bitmap = &bitmap;
673 args->thread = &RenderThread::getInstance();
674 sk_sp<Bitmap> hardwareBitmap(reinterpret_cast<Bitmap*>(staticPostAndWait(task)));
675 return hardwareBitmap;
676}
677
sergeyv59eecb522016-11-17 17:54:57 -0800678CREATE_BRIDGE3(copyGraphicBufferInto, RenderThread* thread, GraphicBuffer* buffer, SkBitmap* bitmap) {
679 return (void*) args->thread->readback().copyGraphicBufferInto(args->buffer, args->bitmap);
680}
681
682int RenderProxy::copyGraphicBufferInto(GraphicBuffer* buffer, SkBitmap* bitmap) {
Stan Iliev6983bc42017-02-02 14:11:53 -0500683 RenderThread& thread = RenderThread::getInstance();
684 if (Properties::isSkiaEnabled() && gettid() == thread.getTid()) {
685 //TODO: fix everything that hits this. We should never be triggering a readback ourselves.
686 return (int) thread.readback().copyGraphicBufferInto(buffer, bitmap);
687 } else {
688 SETUP_TASK(copyGraphicBufferInto);
689 args->thread = &thread;
690 args->bitmap = bitmap;
691 args->buffer = buffer;
692 return static_cast<int>(reinterpret_cast<intptr_t>(staticPostAndWait(task)));
693 }
sergeyv59eecb522016-11-17 17:54:57 -0800694}
695
John Reck4f02bf42014-01-03 18:09:17 -0800696void RenderProxy::post(RenderTask* task) {
697 mRenderThread.queue(task);
698}
699
700void* RenderProxy::postAndWait(MethodInvokeRenderTask* task) {
701 void* retval;
702 task->setReturnPtr(&retval);
John Reckcba287b2015-11-10 12:52:44 -0800703 SignalingRenderTask syncTask(task, &mSyncMutex, &mSyncCondition);
704 AutoMutex _lock(mSyncMutex);
705 mRenderThread.queue(&syncTask);
706 mSyncCondition.wait(mSyncMutex);
John Reck4f02bf42014-01-03 18:09:17 -0800707 return retval;
708}
709
John Reck0e89e2b2014-10-31 14:49:06 -0700710void* RenderProxy::staticPostAndWait(MethodInvokeRenderTask* task) {
711 RenderThread& thread = RenderThread::getInstance();
Stan Iliev6983bc42017-02-02 14:11:53 -0500712 LOG_ALWAYS_FATAL_IF(gettid() == thread.getTid());
John Reck0e89e2b2014-10-31 14:49:06 -0700713 void* retval;
714 task->setReturnPtr(&retval);
Chris Craik0a24b142015-10-19 17:10:19 -0700715 thread.queueAndWait(task);
John Reck0e89e2b2014-10-31 14:49:06 -0700716 return retval;
717}
718
John Reck4f02bf42014-01-03 18:09:17 -0800719} /* namespace renderthread */
720} /* namespace uirenderer */
721} /* namespace android */