blob: dcbc980763eccddede11ec9f0432417fc11795fd [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
30namespace android {
31namespace uirenderer {
32namespace renderthread {
33
34#define ARGS(method) method ## Args
35
John Reck19b6bcf2014-02-14 20:03:38 -080036#define CREATE_BRIDGE0(name) CREATE_BRIDGE(name,,,,,,,,)
John Reck4f02bf42014-01-03 18:09:17 -080037#define CREATE_BRIDGE1(name, a1) CREATE_BRIDGE(name, a1,,,,,,,)
38#define CREATE_BRIDGE2(name, a1, a2) CREATE_BRIDGE(name, a1,a2,,,,,,)
39#define CREATE_BRIDGE3(name, a1, a2, a3) CREATE_BRIDGE(name, a1,a2,a3,,,,,)
40#define CREATE_BRIDGE4(name, a1, a2, a3, a4) CREATE_BRIDGE(name, a1,a2,a3,a4,,,,)
Chris Craik797b95b2014-05-20 18:10:25 -070041#define CREATE_BRIDGE5(name, a1, a2, a3, a4, a5) CREATE_BRIDGE(name, a1,a2,a3,a4,a5,,,)
Chris Craik058fc642014-07-23 18:19:28 -070042#define CREATE_BRIDGE6(name, a1, a2, a3, a4, a5, a6) CREATE_BRIDGE(name, a1,a2,a3,a4,a5,a6,,)
43#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 -080044#define CREATE_BRIDGE(name, a1, a2, a3, a4, a5, a6, a7, a8) \
45 typedef struct { \
46 a1; a2; a3; a4; a5; a6; a7; a8; \
47 } ARGS(name); \
John Reck43871902016-08-01 14:39:24 -070048 static_assert(std::is_trivially_destructible<ARGS(name)>::value, \
49 "Error, ARGS must be trivially destructible!"); \
John Reck4f02bf42014-01-03 18:09:17 -080050 static void* Bridge_ ## name(ARGS(name)* args)
51
52#define SETUP_TASK(method) \
53 LOG_ALWAYS_FATAL_IF( METHOD_INVOKE_PAYLOAD_SIZE < sizeof(ARGS(method)), \
Mark Salyzyn546f3532014-06-10 12:29:14 -070054 "METHOD_INVOKE_PAYLOAD_SIZE %zu is smaller than sizeof(" #method "Args) %zu", \
John Reck4f02bf42014-01-03 18:09:17 -080055 METHOD_INVOKE_PAYLOAD_SIZE, sizeof(ARGS(method))); \
John Recke2c45522014-04-07 17:31:44 -070056 MethodInvokeRenderTask* task = new MethodInvokeRenderTask((RunnableMethod) Bridge_ ## method); \
John Reck4f02bf42014-01-03 18:09:17 -080057 ARGS(method) *args = (ARGS(method) *) task->payload()
58
John Reck119907c2014-08-14 09:02:01 -070059CREATE_BRIDGE4(createContext, RenderThread* thread, bool translucent,
60 RenderNode* rootRenderNode, IContextFactory* contextFactory) {
Stan Iliev03de0742016-07-07 12:35:54 -040061 return CanvasContext::create(*args->thread, args->translucent,
John Reck119907c2014-08-14 09:02:01 -070062 args->rootRenderNode, args->contextFactory);
John Reck4f02bf42014-01-03 18:09:17 -080063}
64
John Reck119907c2014-08-14 09:02:01 -070065RenderProxy::RenderProxy(bool translucent, RenderNode* rootRenderNode, IContextFactory* contextFactory)
John Reck4f02bf42014-01-03 18:09:17 -080066 : mRenderThread(RenderThread::getInstance())
Chris Craikd41c4d82015-01-05 15:51:13 -080067 , mContext(nullptr) {
John Reck4f02bf42014-01-03 18:09:17 -080068 SETUP_TASK(createContext);
69 args->translucent = translucent;
John Recke45b1fd2014-04-15 09:50:16 -070070 args->rootRenderNode = rootRenderNode;
John Reck3b202512014-06-23 13:13:08 -070071 args->thread = &mRenderThread;
John Reck119907c2014-08-14 09:02:01 -070072 args->contextFactory = contextFactory;
John Reck4f02bf42014-01-03 18:09:17 -080073 mContext = (CanvasContext*) postAndWait(task);
Skuhneea7a7fb2015-08-28 07:10:31 -070074 mDrawFrameTask.setContext(&mRenderThread, mContext, rootRenderNode);
John Reck4f02bf42014-01-03 18:09:17 -080075}
76
77RenderProxy::~RenderProxy() {
78 destroyContext();
79}
80
81CREATE_BRIDGE1(destroyContext, CanvasContext* context) {
82 delete args->context;
Chris Craikd41c4d82015-01-05 15:51:13 -080083 return nullptr;
John Reck4f02bf42014-01-03 18:09:17 -080084}
85
86void RenderProxy::destroyContext() {
87 if (mContext) {
88 SETUP_TASK(destroyContext);
89 args->context = mContext;
Chris Craikd41c4d82015-01-05 15:51:13 -080090 mContext = nullptr;
Skuhneea7a7fb2015-08-28 07:10:31 -070091 mDrawFrameTask.setContext(nullptr, nullptr, nullptr);
John Reck668f0e32014-03-26 15:10:40 -070092 // This is also a fence as we need to be certain that there are no
93 // outstanding mDrawFrame tasks posted before it is destroyed
94 postAndWait(task);
John Reck4f02bf42014-01-03 18:09:17 -080095 }
96}
97
John Reck1125d1f2014-10-23 11:02:19 -070098CREATE_BRIDGE2(setSwapBehavior, CanvasContext* context, SwapBehavior swapBehavior) {
99 args->context->setSwapBehavior(args->swapBehavior);
Chris Craikd41c4d82015-01-05 15:51:13 -0800100 return nullptr;
John Reck1125d1f2014-10-23 11:02:19 -0700101}
102
103void RenderProxy::setSwapBehavior(SwapBehavior swapBehavior) {
104 SETUP_TASK(setSwapBehavior);
105 args->context = mContext;
106 args->swapBehavior = swapBehavior;
107 post(task);
108}
109
John Reckfe5e7b72014-05-23 17:42:28 -0700110CREATE_BRIDGE1(loadSystemProperties, CanvasContext* context) {
John Recke4280ba2014-05-05 16:39:37 -0700111 bool needsRedraw = false;
112 if (Caches::hasInstance()) {
Chris Craik2507c342015-05-04 14:36:49 -0700113 needsRedraw = Properties::load();
John Recke4280ba2014-05-05 16:39:37 -0700114 }
Chris Craik2507c342015-05-04 14:36:49 -0700115 if (args->context->profiler().consumeProperties()) {
John Reckfe5e7b72014-05-23 17:42:28 -0700116 needsRedraw = true;
117 }
John Recke4280ba2014-05-05 16:39:37 -0700118 return (void*) needsRedraw;
119}
120
121bool RenderProxy::loadSystemProperties() {
122 SETUP_TASK(loadSystemProperties);
John Reckfe5e7b72014-05-23 17:42:28 -0700123 args->context = mContext;
John Recke4280ba2014-05-05 16:39:37 -0700124 return (bool) postAndWait(task);
125}
126
John Reckb36016c2015-03-11 08:50:53 -0700127CREATE_BRIDGE2(setName, CanvasContext* context, const char* name) {
128 args->context->setName(std::string(args->name));
129 return nullptr;
130}
131
132void RenderProxy::setName(const char* name) {
133 SETUP_TASK(setName);
134 args->context = mContext;
135 args->name = name;
Chris Craik2507c342015-05-04 14:36:49 -0700136 postAndWait(task); // block since name/value pointers owned by caller
John Reckb36016c2015-03-11 08:50:53 -0700137}
138
John Reckf6481082016-02-02 15:18:23 -0800139CREATE_BRIDGE2(initialize, CanvasContext* context, Surface* surface) {
140 args->context->initialize(args->surface);
Thomas Buhot0bcd0cb2015-12-04 12:18:03 +0100141 return nullptr;
John Reck4f02bf42014-01-03 18:09:17 -0800142}
143
John Reckf6481082016-02-02 15:18:23 -0800144void RenderProxy::initialize(const sp<Surface>& surface) {
John Reck4f02bf42014-01-03 18:09:17 -0800145 SETUP_TASK(initialize);
146 args->context = mContext;
John Reckf6481082016-02-02 15:18:23 -0800147 args->surface = surface.get();
Thomas Buhot0bcd0cb2015-12-04 12:18:03 +0100148 post(task);
John Reck4f02bf42014-01-03 18:09:17 -0800149}
150
John Reckf6481082016-02-02 15:18:23 -0800151CREATE_BRIDGE2(updateSurface, CanvasContext* context, Surface* surface) {
152 args->context->updateSurface(args->surface);
Chris Craikd41c4d82015-01-05 15:51:13 -0800153 return nullptr;
John Reck4f02bf42014-01-03 18:09:17 -0800154}
155
John Reckf6481082016-02-02 15:18:23 -0800156void RenderProxy::updateSurface(const sp<Surface>& surface) {
John Reck4f02bf42014-01-03 18:09:17 -0800157 SETUP_TASK(updateSurface);
158 args->context = mContext;
John Reckf6481082016-02-02 15:18:23 -0800159 args->surface = surface.get();
John Reckcd682122016-08-09 12:09:03 -0700160 post(task);
John Reckf7d9c1d2014-04-09 10:01:03 -0700161}
162
John Reckf6481082016-02-02 15:18:23 -0800163CREATE_BRIDGE2(pauseSurface, CanvasContext* context, Surface* surface) {
164 return (void*) args->context->pauseSurface(args->surface);
John Reckf7d9c1d2014-04-09 10:01:03 -0700165}
166
John Reckf6481082016-02-02 15:18:23 -0800167bool RenderProxy::pauseSurface(const sp<Surface>& surface) {
John Reckf7d9c1d2014-04-09 10:01:03 -0700168 SETUP_TASK(pauseSurface);
169 args->context = mContext;
John Reckf6481082016-02-02 15:18:23 -0800170 args->surface = surface.get();
John Reck01a5ea32014-12-03 13:01:07 -0800171 return (bool) postAndWait(task);
John Reck4f02bf42014-01-03 18:09:17 -0800172}
173
John Reck8afcc762016-04-13 10:24:06 -0700174CREATE_BRIDGE2(setStopped, CanvasContext* context, bool stopped) {
175 args->context->setStopped(args->stopped);
176 return nullptr;
177}
178
179void RenderProxy::setStopped(bool stopped) {
180 SETUP_TASK(setStopped);
181 args->context = mContext;
182 args->stopped = stopped;
183 postAndWait(task);
184}
185
John Reckab1080c2016-06-21 16:24:20 -0700186CREATE_BRIDGE4(setup, CanvasContext* context,
Alan Viverette50210d92015-05-14 18:05:36 -0700187 float lightRadius, uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
John Reckab1080c2016-06-21 16:24:20 -0700188 args->context->setup(args->lightRadius,
Chris Craik058fc642014-07-23 18:19:28 -0700189 args->ambientShadowAlpha, args->spotShadowAlpha);
Chris Craikd41c4d82015-01-05 15:51:13 -0800190 return nullptr;
John Reck4f02bf42014-01-03 18:09:17 -0800191}
192
John Reckab1080c2016-06-21 16:24:20 -0700193void RenderProxy::setup(float lightRadius,
John Reckb36016c2015-03-11 08:50:53 -0700194 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
John Reck4f02bf42014-01-03 18:09:17 -0800195 SETUP_TASK(setup);
196 args->context = mContext;
Chris Craik797b95b2014-05-20 18:10:25 -0700197 args->lightRadius = lightRadius;
Chris Craik058fc642014-07-23 18:19:28 -0700198 args->ambientShadowAlpha = ambientShadowAlpha;
199 args->spotShadowAlpha = spotShadowAlpha;
John Reck4f02bf42014-01-03 18:09:17 -0800200 post(task);
201}
202
Alan Viverette50210d92015-05-14 18:05:36 -0700203CREATE_BRIDGE2(setLightCenter, CanvasContext* context, Vector3 lightCenter) {
204 args->context->setLightCenter(args->lightCenter);
205 return nullptr;
206}
207
208void RenderProxy::setLightCenter(const Vector3& lightCenter) {
209 SETUP_TASK(setLightCenter);
210 args->context = mContext;
211 args->lightCenter = lightCenter;
212 post(task);
213}
214
John Reck63a06672014-05-07 13:45:54 -0700215CREATE_BRIDGE2(setOpaque, CanvasContext* context, bool opaque) {
216 args->context->setOpaque(args->opaque);
Chris Craikd41c4d82015-01-05 15:51:13 -0800217 return nullptr;
John Reck63a06672014-05-07 13:45:54 -0700218}
219
220void RenderProxy::setOpaque(bool opaque) {
221 SETUP_TASK(setOpaque);
222 args->context = mContext;
223 args->opaque = opaque;
224 post(task);
225}
226
John Reckba6adf62015-02-19 14:36:50 -0800227int64_t* RenderProxy::frameInfo() {
228 return mDrawFrameTask.frameInfo();
229}
230
John Reck51f2d602016-04-06 07:50:47 -0700231int RenderProxy::syncAndDrawFrame(TreeObserver* observer) {
232 return mDrawFrameTask.drawFrame(observer);
John Reck4f02bf42014-01-03 18:09:17 -0800233}
234
John Reck51f2d602016-04-06 07:50:47 -0700235CREATE_BRIDGE2(destroy, CanvasContext* context, TreeObserver* observer) {
236 args->context->destroy(args->observer);
Chris Craikd41c4d82015-01-05 15:51:13 -0800237 return nullptr;
John Reck4f02bf42014-01-03 18:09:17 -0800238}
239
John Reck51f2d602016-04-06 07:50:47 -0700240void RenderProxy::destroy(TreeObserver* observer) {
John Reck17035b02014-09-03 07:39:53 -0700241 SETUP_TASK(destroy);
John Reck4f02bf42014-01-03 18:09:17 -0800242 args->context = mContext;
John Reck51f2d602016-04-06 07:50:47 -0700243 args->observer = observer;
John Reckfae904d2014-04-14 11:01:57 -0700244 // destroyCanvasAndSurface() needs a fence as when it returns the
245 // underlying BufferQueue is going to be released from under
246 // the render thread.
247 postAndWait(task);
John Reck4f02bf42014-01-03 18:09:17 -0800248}
249
John Reck3b202512014-06-23 13:13:08 -0700250CREATE_BRIDGE2(invokeFunctor, RenderThread* thread, Functor* functor) {
251 CanvasContext::invokeFunctor(*args->thread, args->functor);
Chris Craikd41c4d82015-01-05 15:51:13 -0800252 return nullptr;
John Reck0d1f6342014-03-28 20:30:27 -0700253}
254
255void RenderProxy::invokeFunctor(Functor* functor, bool waitForCompletion) {
John Reckd3d8daf2014-04-10 15:00:13 -0700256 ATRACE_CALL();
John Reck3b202512014-06-23 13:13:08 -0700257 RenderThread& thread = RenderThread::getInstance();
John Reck0d1f6342014-03-28 20:30:27 -0700258 SETUP_TASK(invokeFunctor);
John Reck3b202512014-06-23 13:13:08 -0700259 args->thread = &thread;
John Reck0d1f6342014-03-28 20:30:27 -0700260 args->functor = functor;
261 if (waitForCompletion) {
John Reck3b202512014-06-23 13:13:08 -0700262 // waitForCompletion = true is expected to be fairly rare and only
263 // happen in destruction. Thus it should be fine to temporarily
264 // create a Mutex
John Reck0e89e2b2014-10-31 14:49:06 -0700265 staticPostAndWait(task);
John Reck0d1f6342014-03-28 20:30:27 -0700266 } else {
John Reck3b202512014-06-23 13:13:08 -0700267 thread.queue(task);
John Reck0d1f6342014-03-28 20:30:27 -0700268 }
269}
270
John Reckc36df952015-07-29 10:09:36 -0700271CREATE_BRIDGE1(createTextureLayer, CanvasContext* context) {
Derek Sollenberger56ad6ec2016-07-22 12:13:32 -0400272 return args->context->createTextureLayer();
John Reck19b6bcf2014-02-14 20:03:38 -0800273}
274
275DeferredLayerUpdater* RenderProxy::createTextureLayer() {
276 SETUP_TASK(createTextureLayer);
John Reck1949e792014-04-08 15:18:56 -0700277 args->context = mContext;
John Reck19b6bcf2014-02-14 20:03:38 -0800278 void* retval = postAndWait(task);
279 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval);
John Reck19b6bcf2014-02-14 20:03:38 -0800280 return layer;
281}
282
John Reck51f2d602016-04-06 07:50:47 -0700283CREATE_BRIDGE3(buildLayer, CanvasContext* context, RenderNode* node, TreeObserver* observer) {
284 args->context->buildLayer(args->node, args->observer);
Chris Craikd41c4d82015-01-05 15:51:13 -0800285 return nullptr;
John Reck3e824952014-08-20 10:08:39 -0700286}
287
John Reck51f2d602016-04-06 07:50:47 -0700288void RenderProxy::buildLayer(RenderNode* node, TreeObserver* observer) {
John Reck3e824952014-08-20 10:08:39 -0700289 SETUP_TASK(buildLayer);
290 args->context = mContext;
291 args->node = node;
John Reck51f2d602016-04-06 07:50:47 -0700292 args->observer = observer;
John Reck3e824952014-08-20 10:08:39 -0700293 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 Reck51f2d602016-04-06 07:50:47 -0700329CREATE_BRIDGE2(destroyHardwareResources, CanvasContext* context, TreeObserver* observer) {
330 args->context->destroyHardwareResources(args->observer);
Chris Craikd41c4d82015-01-05 15:51:13 -0800331 return nullptr;
John Recke1628b72014-05-23 15:11:19 -0700332}
333
John Reck51f2d602016-04-06 07:50:47 -0700334void RenderProxy::destroyHardwareResources(TreeObserver* observer) {
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 args->observer = observer;
338 postAndWait(task);
John Recke1628b72014-05-23 15:11:19 -0700339}
340
Chris Craik2507c342015-05-04 14:36:49 -0700341CREATE_BRIDGE2(trimMemory, RenderThread* thread, int level) {
John Reckf47a5942014-06-30 16:20:04 -0700342 CanvasContext::trimMemory(*args->thread, args->level);
Chris Craikd41c4d82015-01-05 15:51:13 -0800343 return nullptr;
John Reckf47a5942014-06-30 16:20:04 -0700344}
345
346void RenderProxy::trimMemory(int level) {
John Reckcd3a22c2014-08-06 13:33:59 -0700347 // Avoid creating a RenderThread to do a trimMemory.
348 if (RenderThread::hasInstance()) {
349 RenderThread& thread = RenderThread::getInstance();
Chris Craik2507c342015-05-04 14:36:49 -0700350 SETUP_TASK(trimMemory);
John Reckcd3a22c2014-08-06 13:33:59 -0700351 args->thread = &thread;
352 args->level = level;
353 thread.queue(task);
354 }
John Reckf47a5942014-06-30 16:20:04 -0700355}
356
Chris Craik2507c342015-05-04 14:36:49 -0700357CREATE_BRIDGE2(overrideProperty, const char* name, const char* value) {
358 Properties::overrideProperty(args->name, args->value);
359 return nullptr;
360}
361
362void RenderProxy::overrideProperty(const char* name, const char* value) {
Chris Craik2507c342015-05-04 14:36:49 -0700363 SETUP_TASK(overrideProperty);
364 args->name = name;
365 args->value = value;
366 staticPostAndWait(task); // expensive, but block here since name/value pointers owned by caller
367}
368
John Reck28ad7b52014-04-07 16:59:25 -0700369CREATE_BRIDGE0(fence) {
370 // Intentionally empty
Chris Craikd41c4d82015-01-05 15:51:13 -0800371 return nullptr;
John Reck28ad7b52014-04-07 16:59:25 -0700372}
373
Andreas Gampe64bb4132014-11-22 00:35:09 +0000374template <typename T>
375void UNUSED(T t) {}
376
John Reck28ad7b52014-04-07 16:59:25 -0700377void RenderProxy::fence() {
378 SETUP_TASK(fence);
Andreas Gampe1e196742014-11-10 15:23:43 -0800379 UNUSED(args);
John Reck28ad7b52014-04-07 16:59:25 -0700380 postAndWait(task);
381}
382
Thomas Buhotc0a0e1a2016-01-18 10:31:58 +0100383void RenderProxy::staticFence() {
384 SETUP_TASK(fence);
385 UNUSED(args);
386 staticPostAndWait(task);
387}
388
John Reckf47a5942014-06-30 16:20:04 -0700389CREATE_BRIDGE1(stopDrawing, CanvasContext* context) {
390 args->context->stopDrawing();
Chris Craikd41c4d82015-01-05 15:51:13 -0800391 return nullptr;
John Reckf47a5942014-06-30 16:20:04 -0700392}
393
394void RenderProxy::stopDrawing() {
395 SETUP_TASK(stopDrawing);
396 args->context = mContext;
397 postAndWait(task);
398}
399
John Recka5dda642014-05-22 15:43:54 -0700400CREATE_BRIDGE1(notifyFramePending, CanvasContext* context) {
401 args->context->notifyFramePending();
Chris Craikd41c4d82015-01-05 15:51:13 -0800402 return nullptr;
John Recka5dda642014-05-22 15:43:54 -0700403}
404
405void RenderProxy::notifyFramePending() {
406 SETUP_TASK(notifyFramePending);
407 args->context = mContext;
408 mRenderThread.queueAtFront(task);
409}
410
John Reck7f2e5e32015-05-05 11:00:53 -0700411CREATE_BRIDGE4(dumpProfileInfo, CanvasContext* context, RenderThread* thread,
412 int fd, int dumpFlags) {
John Reckfe5e7b72014-05-23 17:42:28 -0700413 args->context->profiler().dumpData(args->fd);
Chris Craik53e51e42015-06-01 10:35:35 -0700414 if (args->dumpFlags & DumpFlags::FrameStats) {
John Reckba6adf62015-02-19 14:36:50 -0800415 args->context->dumpFrames(args->fd);
416 }
Chris Craik53e51e42015-06-01 10:35:35 -0700417 if (args->dumpFlags & DumpFlags::Reset) {
John Reckba6adf62015-02-19 14:36:50 -0800418 args->context->resetFrameStats();
419 }
John Recka41f2442016-04-07 16:36:57 -0700420 if (args->dumpFlags & DumpFlags::JankStats) {
421 args->thread->jankTracker().dump(args->fd);
422 }
Chris Craikd41c4d82015-01-05 15:51:13 -0800423 return nullptr;
John Reckfe5e7b72014-05-23 17:42:28 -0700424}
425
John Reckba6adf62015-02-19 14:36:50 -0800426void RenderProxy::dumpProfileInfo(int fd, int dumpFlags) {
John Reckfe5e7b72014-05-23 17:42:28 -0700427 SETUP_TASK(dumpProfileInfo);
428 args->context = mContext;
John Reck7f2e5e32015-05-05 11:00:53 -0700429 args->thread = &mRenderThread;
John Reckfe5e7b72014-05-23 17:42:28 -0700430 args->fd = fd;
John Reckba6adf62015-02-19 14:36:50 -0800431 args->dumpFlags = dumpFlags;
John Reckfe5e7b72014-05-23 17:42:28 -0700432 postAndWait(task);
433}
434
John Reck7f2e5e32015-05-05 11:00:53 -0700435CREATE_BRIDGE1(resetProfileInfo, CanvasContext* context) {
436 args->context->resetFrameStats();
437 return nullptr;
438}
439
440void RenderProxy::resetProfileInfo() {
441 SETUP_TASK(resetProfileInfo);
442 args->context = mContext;
443 postAndWait(task);
444}
445
John Reckf1480762016-07-03 18:28:25 -0700446CREATE_BRIDGE2(frameTimePercentile, RenderThread* thread, int percentile) {
447 return reinterpret_cast<void*>(static_cast<uintptr_t>(
448 args->thread->jankTracker().findPercentile(args->percentile)));
449}
450
451uint32_t RenderProxy::frameTimePercentile(int p) {
452 SETUP_TASK(frameTimePercentile);
453 args->thread = &mRenderThread;
454 args->percentile = p;
455 return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
456 postAndWait(task)));
457}
458
John Reckba6adf62015-02-19 14:36:50 -0800459CREATE_BRIDGE2(dumpGraphicsMemory, int fd, RenderThread* thread) {
460 args->thread->jankTracker().dump(args->fd);
461
Chris Craik2ae07332015-01-21 14:22:39 -0800462 FILE *file = fdopen(args->fd, "a");
463 if (Caches::hasInstance()) {
464 String8 cachesLog;
465 Caches::getInstance().dumpMemoryUsage(cachesLog);
466 fprintf(file, "\nCaches:\n%s\n", cachesLog.string());
467 } else {
468 fprintf(file, "\nNo caches instance.\n");
469 }
Chris Craikff3edce2016-01-14 10:04:08 -0800470 fprintf(file, "\nPipeline=FrameBuilder\n");
Chris Craik2ae07332015-01-21 14:22:39 -0800471 fflush(file);
Chris Craikd41c4d82015-01-05 15:51:13 -0800472 return nullptr;
John Reck0e89e2b2014-10-31 14:49:06 -0700473}
474
Chris Craik2ae07332015-01-21 14:22:39 -0800475void RenderProxy::dumpGraphicsMemory(int fd) {
youngmin0822.leec80c9ad2015-03-20 21:22:32 +0900476 if (!RenderThread::hasInstance()) return;
Chris Craik2ae07332015-01-21 14:22:39 -0800477 SETUP_TASK(dumpGraphicsMemory);
John Reck0e89e2b2014-10-31 14:49:06 -0700478 args->fd = fd;
John Reckba6adf62015-02-19 14:36:50 -0800479 args->thread = &RenderThread::getInstance();
John Reck0e89e2b2014-10-31 14:49:06 -0700480 staticPostAndWait(task);
481}
482
Skuhneea7a7fb2015-08-28 07:10:31 -0700483CREATE_BRIDGE4(setTextureAtlas, RenderThread* thread, GraphicBuffer* buffer, int64_t* map,
484 size_t size) {
John Reck3b202512014-06-23 13:13:08 -0700485 CanvasContext::setTextureAtlas(*args->thread, args->buffer, args->map, args->size);
Chris Craikd41c4d82015-01-05 15:51:13 -0800486 args->buffer->decStrong(nullptr);
487 return nullptr;
John Reck3b202512014-06-23 13:13:08 -0700488}
489
490void RenderProxy::setTextureAtlas(const sp<GraphicBuffer>& buffer, int64_t* map, size_t size) {
491 SETUP_TASK(setTextureAtlas);
492 args->thread = &mRenderThread;
493 args->buffer = buffer.get();
Chris Craikd41c4d82015-01-05 15:51:13 -0800494 args->buffer->incStrong(nullptr);
John Reck3b202512014-06-23 13:13:08 -0700495 args->map = map;
496 args->size = size;
497 post(task);
498}
499
John Reckedc524c2015-03-18 15:24:33 -0700500CREATE_BRIDGE2(setProcessStatsBuffer, RenderThread* thread, int fd) {
501 args->thread->jankTracker().switchStorageToAshmem(args->fd);
502 close(args->fd);
503 return nullptr;
504}
505
506void RenderProxy::setProcessStatsBuffer(int fd) {
507 SETUP_TASK(setProcessStatsBuffer);
508 args->thread = &mRenderThread;
509 args->fd = dup(fd);
510 post(task);
511}
512
Tim Murray33eb07f2016-06-10 10:03:20 -0700513int RenderProxy::getRenderThreadTid() {
514 return mRenderThread.getTid();
515}
516
Skuhneea7a7fb2015-08-28 07:10:31 -0700517CREATE_BRIDGE3(addRenderNode, CanvasContext* context, RenderNode* node, bool placeFront) {
518 args->context->addRenderNode(args->node, args->placeFront);
519 return nullptr;
520}
521
522void RenderProxy::addRenderNode(RenderNode* node, bool placeFront) {
523 SETUP_TASK(addRenderNode);
524 args->context = mContext;
525 args->node = node;
526 args->placeFront = placeFront;
527 post(task);
528}
529
530CREATE_BRIDGE2(removeRenderNode, CanvasContext* context, RenderNode* node) {
531 args->context->removeRenderNode(args->node);
532 return nullptr;
533}
534
535void RenderProxy::removeRenderNode(RenderNode* node) {
536 SETUP_TASK(removeRenderNode);
537 args->context = mContext;
538 args->node = node;
539 post(task);
540}
541
542CREATE_BRIDGE2(drawRenderNode, CanvasContext* context, RenderNode* node) {
543 args->context->prepareAndDraw(args->node);
544 return nullptr;
545}
546
547void RenderProxy::drawRenderNode(RenderNode* node) {
548 SETUP_TASK(drawRenderNode);
549 args->context = mContext;
550 args->node = node;
551 // Be pseudo-thread-safe and don't use any member variables
552 staticPostAndWait(task);
553}
554
Skuhneb8160872015-09-22 09:51:39 -0700555CREATE_BRIDGE5(setContentDrawBounds, CanvasContext* context, int left, int top,
Skuhneea7a7fb2015-08-28 07:10:31 -0700556 int right, int bottom) {
Skuhneb8160872015-09-22 09:51:39 -0700557 args->context->setContentDrawBounds(args->left, args->top, args->right, args->bottom);
Skuhneea7a7fb2015-08-28 07:10:31 -0700558 return nullptr;
559}
560
Skuhneb8160872015-09-22 09:51:39 -0700561void RenderProxy::setContentDrawBounds(int left, int top, int right, int bottom) {
562 SETUP_TASK(setContentDrawBounds);
Skuhneea7a7fb2015-08-28 07:10:31 -0700563 args->context = mContext;
564 args->left = left;
565 args->top = top;
566 args->right = right;
567 args->bottom = bottom;
568 staticPostAndWait(task);
569}
570
John Recke248bd12015-08-05 13:53:53 -0700571CREATE_BRIDGE1(serializeDisplayListTree, CanvasContext* context) {
572 args->context->serializeDisplayListTree();
573 return nullptr;
574}
575
576void RenderProxy::serializeDisplayListTree() {
577 SETUP_TASK(serializeDisplayListTree);
578 args->context = mContext;
579 post(task);
580}
581
Andres Morales910beb82016-02-02 16:19:40 -0800582CREATE_BRIDGE2(addFrameMetricsObserver, CanvasContext* context,
583 FrameMetricsObserver* frameStatsObserver) {
584 args->context->addFrameMetricsObserver(args->frameStatsObserver);
Andres Morales06f5bc72015-12-15 15:21:31 -0800585 if (args->frameStatsObserver != nullptr) {
586 args->frameStatsObserver->decStrong(args->context);
587 }
588 return nullptr;
589}
590
Andres Morales910beb82016-02-02 16:19:40 -0800591void RenderProxy::addFrameMetricsObserver(FrameMetricsObserver* observer) {
592 SETUP_TASK(addFrameMetricsObserver);
Andres Morales06f5bc72015-12-15 15:21:31 -0800593 args->context = mContext;
594 args->frameStatsObserver = observer;
595 if (observer != nullptr) {
596 observer->incStrong(mContext);
597 }
598 post(task);
599}
600
Andres Morales910beb82016-02-02 16:19:40 -0800601CREATE_BRIDGE2(removeFrameMetricsObserver, CanvasContext* context,
602 FrameMetricsObserver* frameStatsObserver) {
603 args->context->removeFrameMetricsObserver(args->frameStatsObserver);
Andres Morales06f5bc72015-12-15 15:21:31 -0800604 if (args->frameStatsObserver != nullptr) {
605 args->frameStatsObserver->decStrong(args->context);
606 }
607 return nullptr;
608}
609
Andres Morales910beb82016-02-02 16:19:40 -0800610void RenderProxy::removeFrameMetricsObserver(FrameMetricsObserver* observer) {
611 SETUP_TASK(removeFrameMetricsObserver);
Andres Morales06f5bc72015-12-15 15:21:31 -0800612 args->context = mContext;
613 args->frameStatsObserver = observer;
614 if (observer != nullptr) {
615 observer->incStrong(mContext);
616 }
617 post(task);
618}
619
John Reck95801462016-09-01 09:44:09 -0700620CREATE_BRIDGE4(copySurfaceInto, RenderThread* thread,
621 Surface* surface, Rect srcRect, SkBitmap* bitmap) {
John Reck10dd0582016-03-31 16:36:16 -0700622 return (void*) Readback::copySurfaceInto(*args->thread,
John Reck95801462016-09-01 09:44:09 -0700623 *args->surface, args->srcRect, args->bitmap);
John Reck10dd0582016-03-31 16:36:16 -0700624}
625
John Reck95801462016-09-01 09:44:09 -0700626int RenderProxy::copySurfaceInto(sp<Surface>& surface, int left, int top,
627 int right, int bottom, SkBitmap* bitmap) {
John Reck10dd0582016-03-31 16:36:16 -0700628 SETUP_TASK(copySurfaceInto);
629 args->bitmap = bitmap;
630 args->surface = surface.get();
631 args->thread = &RenderThread::getInstance();
John Reck95801462016-09-01 09:44:09 -0700632 args->srcRect.set(left, top, right, bottom);
John Recke94cbc72016-04-25 13:03:44 -0700633 return static_cast<int>(
634 reinterpret_cast<intptr_t>( staticPostAndWait(task) ));
John Reck10dd0582016-03-31 16:36:16 -0700635}
636
John Reck43871902016-08-01 14:39:24 -0700637CREATE_BRIDGE2(prepareToDraw, RenderThread* thread, SkBitmap* bitmap) {
638 if (Caches::hasInstance() && args->thread->eglManager().hasEglContext()) {
639 ATRACE_NAME("Bitmap#prepareToDraw task");
640 Caches::getInstance().textureCache.prefetch(args->bitmap);
641 }
642 delete args->bitmap;
643 args->bitmap = nullptr;
644 return nullptr;
645}
646
647void RenderProxy::prepareToDraw(const SkBitmap& bitmap) {
648 // If we haven't spun up a hardware accelerated window yet, there's no
649 // point in precaching these bitmaps as it can't impact jank.
650 // We also don't know if we even will spin up a hardware-accelerated
651 // window or not.
652 if (!RenderThread::hasInstance()) return;
653 RenderThread* renderThread = &RenderThread::getInstance();
654 SETUP_TASK(prepareToDraw);
655 args->thread = renderThread;
656 args->bitmap = new SkBitmap(bitmap);
657 nsecs_t lastVsync = renderThread->timeLord().latestVsync();
658 nsecs_t estimatedNextVsync = lastVsync + renderThread->timeLord().frameIntervalNanos();
659 nsecs_t timeToNextVsync = estimatedNextVsync - systemTime(CLOCK_MONOTONIC);
660 // We expect the UI thread to take 4ms and for RT to be active from VSYNC+4ms to
661 // VSYNC+12ms or so, so aim for the gap during which RT is expected to
662 // be idle
663 // TODO: Make this concept a first-class supported thing? RT could use
664 // knowledge of pending draws to better schedule this task
665 if (timeToNextVsync > -6_ms && timeToNextVsync < 1_ms) {
666 renderThread->queueAt(task, estimatedNextVsync + 8_ms);
667 } else {
668 renderThread->queue(task);
669 }
670}
671
John Reck4f02bf42014-01-03 18:09:17 -0800672void RenderProxy::post(RenderTask* task) {
673 mRenderThread.queue(task);
674}
675
676void* RenderProxy::postAndWait(MethodInvokeRenderTask* task) {
677 void* retval;
678 task->setReturnPtr(&retval);
John Reckcba287b2015-11-10 12:52:44 -0800679 SignalingRenderTask syncTask(task, &mSyncMutex, &mSyncCondition);
680 AutoMutex _lock(mSyncMutex);
681 mRenderThread.queue(&syncTask);
682 mSyncCondition.wait(mSyncMutex);
John Reck4f02bf42014-01-03 18:09:17 -0800683 return retval;
684}
685
John Reck0e89e2b2014-10-31 14:49:06 -0700686void* RenderProxy::staticPostAndWait(MethodInvokeRenderTask* task) {
687 RenderThread& thread = RenderThread::getInstance();
688 void* retval;
689 task->setReturnPtr(&retval);
Chris Craik0a24b142015-10-19 17:10:19 -0700690 thread.queueAndWait(task);
John Reck0e89e2b2014-10-31 14:49:06 -0700691 return retval;
692}
693
John Reck4f02bf42014-01-03 18:09:17 -0800694} /* namespace renderthread */
695} /* namespace uirenderer */
696} /* namespace android */