blob: 022e871315a9293d4c70f9858edca01fd3bfc185 [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 Reck51f2d602016-04-06 07:50:47 -0700233int RenderProxy::syncAndDrawFrame(TreeObserver* observer) {
234 return mDrawFrameTask.drawFrame(observer);
John Reck4f02bf42014-01-03 18:09:17 -0800235}
236
John Reck51f2d602016-04-06 07:50:47 -0700237CREATE_BRIDGE2(destroy, CanvasContext* context, TreeObserver* observer) {
238 args->context->destroy(args->observer);
Chris Craikd41c4d82015-01-05 15:51:13 -0800239 return nullptr;
John Reck4f02bf42014-01-03 18:09:17 -0800240}
241
John Reck51f2d602016-04-06 07:50:47 -0700242void RenderProxy::destroy(TreeObserver* observer) {
John Reck17035b02014-09-03 07:39:53 -0700243 SETUP_TASK(destroy);
John Reck4f02bf42014-01-03 18:09:17 -0800244 args->context = mContext;
John Reck51f2d602016-04-06 07:50:47 -0700245 args->observer = observer;
John Reckfae904d2014-04-14 11:01:57 -0700246 // destroyCanvasAndSurface() needs a fence as when it returns the
247 // underlying BufferQueue is going to be released from under
248 // the render thread.
249 postAndWait(task);
John Reck4f02bf42014-01-03 18:09:17 -0800250}
251
John Reck3b202512014-06-23 13:13:08 -0700252CREATE_BRIDGE2(invokeFunctor, RenderThread* thread, Functor* functor) {
253 CanvasContext::invokeFunctor(*args->thread, args->functor);
Chris Craikd41c4d82015-01-05 15:51:13 -0800254 return nullptr;
John Reck0d1f6342014-03-28 20:30:27 -0700255}
256
257void RenderProxy::invokeFunctor(Functor* functor, bool waitForCompletion) {
John Reckd3d8daf2014-04-10 15:00:13 -0700258 ATRACE_CALL();
John Reck3b202512014-06-23 13:13:08 -0700259 RenderThread& thread = RenderThread::getInstance();
John Reck0d1f6342014-03-28 20:30:27 -0700260 SETUP_TASK(invokeFunctor);
John Reck3b202512014-06-23 13:13:08 -0700261 args->thread = &thread;
John Reck0d1f6342014-03-28 20:30:27 -0700262 args->functor = functor;
263 if (waitForCompletion) {
John Reck3b202512014-06-23 13:13:08 -0700264 // waitForCompletion = true is expected to be fairly rare and only
265 // happen in destruction. Thus it should be fine to temporarily
266 // create a Mutex
John Reck0e89e2b2014-10-31 14:49:06 -0700267 staticPostAndWait(task);
John Reck0d1f6342014-03-28 20:30:27 -0700268 } else {
John Reck3b202512014-06-23 13:13:08 -0700269 thread.queue(task);
John Reck0d1f6342014-03-28 20:30:27 -0700270 }
271}
272
John Reckc36df952015-07-29 10:09:36 -0700273CREATE_BRIDGE1(createTextureLayer, CanvasContext* context) {
Derek Sollenberger56ad6ec2016-07-22 12:13:32 -0400274 return args->context->createTextureLayer();
John Reck19b6bcf2014-02-14 20:03:38 -0800275}
276
277DeferredLayerUpdater* RenderProxy::createTextureLayer() {
278 SETUP_TASK(createTextureLayer);
John Reck1949e792014-04-08 15:18:56 -0700279 args->context = mContext;
John Reck19b6bcf2014-02-14 20:03:38 -0800280 void* retval = postAndWait(task);
281 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval);
John Reck19b6bcf2014-02-14 20:03:38 -0800282 return layer;
283}
284
John Reck51f2d602016-04-06 07:50:47 -0700285CREATE_BRIDGE3(buildLayer, CanvasContext* context, RenderNode* node, TreeObserver* observer) {
286 args->context->buildLayer(args->node, args->observer);
Chris Craikd41c4d82015-01-05 15:51:13 -0800287 return nullptr;
John Reck3e824952014-08-20 10:08:39 -0700288}
289
John Reck51f2d602016-04-06 07:50:47 -0700290void RenderProxy::buildLayer(RenderNode* node, TreeObserver* observer) {
John Reck3e824952014-08-20 10:08:39 -0700291 SETUP_TASK(buildLayer);
292 args->context = mContext;
293 args->node = node;
John Reck51f2d602016-04-06 07:50:47 -0700294 args->observer = observer;
John Reck3e824952014-08-20 10:08:39 -0700295 postAndWait(task);
296}
297
John Reck19b6bcf2014-02-14 20:03:38 -0800298CREATE_BRIDGE3(copyLayerInto, CanvasContext* context, DeferredLayerUpdater* layer,
299 SkBitmap* bitmap) {
300 bool success = args->context->copyLayerInto(args->layer, args->bitmap);
301 return (void*) success;
302}
303
John Reck3731dc22015-04-13 15:20:29 -0700304bool RenderProxy::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap& bitmap) {
John Reck19b6bcf2014-02-14 20:03:38 -0800305 SETUP_TASK(copyLayerInto);
306 args->context = mContext;
307 args->layer = layer;
John Reck3731dc22015-04-13 15:20:29 -0700308 args->bitmap = &bitmap;
John Reck19b6bcf2014-02-14 20:03:38 -0800309 return (bool) postAndWait(task);
310}
311
John Reckd72e0a32014-05-29 18:56:11 -0700312void RenderProxy::pushLayerUpdate(DeferredLayerUpdater* layer) {
313 mDrawFrameTask.pushLayerUpdate(layer);
314}
315
316void RenderProxy::cancelLayerUpdate(DeferredLayerUpdater* layer) {
317 mDrawFrameTask.removeLayerUpdate(layer);
John Reck19b6bcf2014-02-14 20:03:38 -0800318}
319
John Reck918ad522014-06-27 14:45:25 -0700320CREATE_BRIDGE1(detachSurfaceTexture, DeferredLayerUpdater* layer) {
321 args->layer->detachSurfaceTexture();
Chris Craikd41c4d82015-01-05 15:51:13 -0800322 return nullptr;
John Reck918ad522014-06-27 14:45:25 -0700323}
324
325void RenderProxy::detachSurfaceTexture(DeferredLayerUpdater* layer) {
326 SETUP_TASK(detachSurfaceTexture);
327 args->layer = layer;
328 postAndWait(task);
329}
330
John Reck51f2d602016-04-06 07:50:47 -0700331CREATE_BRIDGE2(destroyHardwareResources, CanvasContext* context, TreeObserver* observer) {
332 args->context->destroyHardwareResources(args->observer);
Chris Craikd41c4d82015-01-05 15:51:13 -0800333 return nullptr;
John Recke1628b72014-05-23 15:11:19 -0700334}
335
John Reck51f2d602016-04-06 07:50:47 -0700336void RenderProxy::destroyHardwareResources(TreeObserver* observer) {
John Reckf47a5942014-06-30 16:20:04 -0700337 SETUP_TASK(destroyHardwareResources);
John Recke1628b72014-05-23 15:11:19 -0700338 args->context = mContext;
John Reck51f2d602016-04-06 07:50:47 -0700339 args->observer = observer;
340 postAndWait(task);
John Recke1628b72014-05-23 15:11:19 -0700341}
342
Chris Craik2507c342015-05-04 14:36:49 -0700343CREATE_BRIDGE2(trimMemory, RenderThread* thread, int level) {
John Reckf47a5942014-06-30 16:20:04 -0700344 CanvasContext::trimMemory(*args->thread, args->level);
Chris Craikd41c4d82015-01-05 15:51:13 -0800345 return nullptr;
John Reckf47a5942014-06-30 16:20:04 -0700346}
347
348void RenderProxy::trimMemory(int level) {
John Reckcd3a22c2014-08-06 13:33:59 -0700349 // Avoid creating a RenderThread to do a trimMemory.
350 if (RenderThread::hasInstance()) {
351 RenderThread& thread = RenderThread::getInstance();
Chris Craik2507c342015-05-04 14:36:49 -0700352 SETUP_TASK(trimMemory);
John Reckcd3a22c2014-08-06 13:33:59 -0700353 args->thread = &thread;
354 args->level = level;
355 thread.queue(task);
356 }
John Reckf47a5942014-06-30 16:20:04 -0700357}
358
Chris Craik2507c342015-05-04 14:36:49 -0700359CREATE_BRIDGE2(overrideProperty, const char* name, const char* value) {
360 Properties::overrideProperty(args->name, args->value);
361 return nullptr;
362}
363
364void RenderProxy::overrideProperty(const char* name, const char* value) {
Chris Craik2507c342015-05-04 14:36:49 -0700365 SETUP_TASK(overrideProperty);
366 args->name = name;
367 args->value = value;
368 staticPostAndWait(task); // expensive, but block here since name/value pointers owned by caller
369}
370
John Reck28ad7b52014-04-07 16:59:25 -0700371CREATE_BRIDGE0(fence) {
372 // Intentionally empty
Chris Craikd41c4d82015-01-05 15:51:13 -0800373 return nullptr;
John Reck28ad7b52014-04-07 16:59:25 -0700374}
375
Andreas Gampe64bb4132014-11-22 00:35:09 +0000376template <typename T>
377void UNUSED(T t) {}
378
John Reck28ad7b52014-04-07 16:59:25 -0700379void RenderProxy::fence() {
380 SETUP_TASK(fence);
Andreas Gampe1e196742014-11-10 15:23:43 -0800381 UNUSED(args);
John Reck28ad7b52014-04-07 16:59:25 -0700382 postAndWait(task);
383}
384
Thomas Buhotc0a0e1a2016-01-18 10:31:58 +0100385void RenderProxy::staticFence() {
386 SETUP_TASK(fence);
387 UNUSED(args);
388 staticPostAndWait(task);
389}
390
John Reckf47a5942014-06-30 16:20:04 -0700391CREATE_BRIDGE1(stopDrawing, CanvasContext* context) {
392 args->context->stopDrawing();
Chris Craikd41c4d82015-01-05 15:51:13 -0800393 return nullptr;
John Reckf47a5942014-06-30 16:20:04 -0700394}
395
396void RenderProxy::stopDrawing() {
397 SETUP_TASK(stopDrawing);
398 args->context = mContext;
399 postAndWait(task);
400}
401
John Recka5dda642014-05-22 15:43:54 -0700402CREATE_BRIDGE1(notifyFramePending, CanvasContext* context) {
403 args->context->notifyFramePending();
Chris Craikd41c4d82015-01-05 15:51:13 -0800404 return nullptr;
John Recka5dda642014-05-22 15:43:54 -0700405}
406
407void RenderProxy::notifyFramePending() {
408 SETUP_TASK(notifyFramePending);
409 args->context = mContext;
410 mRenderThread.queueAtFront(task);
411}
412
John Reck7f2e5e32015-05-05 11:00:53 -0700413CREATE_BRIDGE4(dumpProfileInfo, CanvasContext* context, RenderThread* thread,
414 int fd, int dumpFlags) {
John Reckfe5e7b72014-05-23 17:42:28 -0700415 args->context->profiler().dumpData(args->fd);
Chris Craik53e51e42015-06-01 10:35:35 -0700416 if (args->dumpFlags & DumpFlags::FrameStats) {
John Reckba6adf62015-02-19 14:36:50 -0800417 args->context->dumpFrames(args->fd);
418 }
Chris Craik53e51e42015-06-01 10:35:35 -0700419 if (args->dumpFlags & DumpFlags::Reset) {
John Reckba6adf62015-02-19 14:36:50 -0800420 args->context->resetFrameStats();
421 }
John Recka41f2442016-04-07 16:36:57 -0700422 if (args->dumpFlags & DumpFlags::JankStats) {
423 args->thread->jankTracker().dump(args->fd);
424 }
Chris Craikd41c4d82015-01-05 15:51:13 -0800425 return nullptr;
John Reckfe5e7b72014-05-23 17:42:28 -0700426}
427
John Reckba6adf62015-02-19 14:36:50 -0800428void RenderProxy::dumpProfileInfo(int fd, int dumpFlags) {
John Reckfe5e7b72014-05-23 17:42:28 -0700429 SETUP_TASK(dumpProfileInfo);
430 args->context = mContext;
John Reck7f2e5e32015-05-05 11:00:53 -0700431 args->thread = &mRenderThread;
John Reckfe5e7b72014-05-23 17:42:28 -0700432 args->fd = fd;
John Reckba6adf62015-02-19 14:36:50 -0800433 args->dumpFlags = dumpFlags;
John Reckfe5e7b72014-05-23 17:42:28 -0700434 postAndWait(task);
435}
436
John Reck7f2e5e32015-05-05 11:00:53 -0700437CREATE_BRIDGE1(resetProfileInfo, CanvasContext* context) {
438 args->context->resetFrameStats();
439 return nullptr;
440}
441
442void RenderProxy::resetProfileInfo() {
443 SETUP_TASK(resetProfileInfo);
444 args->context = mContext;
445 postAndWait(task);
446}
447
John Reckf1480762016-07-03 18:28:25 -0700448CREATE_BRIDGE2(frameTimePercentile, RenderThread* thread, int percentile) {
449 return reinterpret_cast<void*>(static_cast<uintptr_t>(
450 args->thread->jankTracker().findPercentile(args->percentile)));
451}
452
453uint32_t RenderProxy::frameTimePercentile(int p) {
454 SETUP_TASK(frameTimePercentile);
455 args->thread = &mRenderThread;
456 args->percentile = p;
457 return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
458 postAndWait(task)));
459}
460
John Reckba6adf62015-02-19 14:36:50 -0800461CREATE_BRIDGE2(dumpGraphicsMemory, int fd, RenderThread* thread) {
462 args->thread->jankTracker().dump(args->fd);
463
Chris Craik2ae07332015-01-21 14:22:39 -0800464 FILE *file = fdopen(args->fd, "a");
465 if (Caches::hasInstance()) {
466 String8 cachesLog;
467 Caches::getInstance().dumpMemoryUsage(cachesLog);
468 fprintf(file, "\nCaches:\n%s\n", cachesLog.string());
469 } else {
470 fprintf(file, "\nNo caches instance.\n");
471 }
Chris Craikff3edce2016-01-14 10:04:08 -0800472 fprintf(file, "\nPipeline=FrameBuilder\n");
Chris Craik2ae07332015-01-21 14:22:39 -0800473 fflush(file);
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) {
486 args->thread->jankTracker().switchStorageToAshmem(args->fd);
487 close(args->fd);
488 return nullptr;
489}
490
491void RenderProxy::setProcessStatsBuffer(int fd) {
492 SETUP_TASK(setProcessStatsBuffer);
493 args->thread = &mRenderThread;
494 args->fd = dup(fd);
495 post(task);
496}
497
Tim Murray33eb07f2016-06-10 10:03:20 -0700498int RenderProxy::getRenderThreadTid() {
499 return mRenderThread.getTid();
500}
501
Skuhneea7a7fb2015-08-28 07:10:31 -0700502CREATE_BRIDGE3(addRenderNode, CanvasContext* context, RenderNode* node, bool placeFront) {
503 args->context->addRenderNode(args->node, args->placeFront);
504 return nullptr;
505}
506
507void RenderProxy::addRenderNode(RenderNode* node, bool placeFront) {
508 SETUP_TASK(addRenderNode);
509 args->context = mContext;
510 args->node = node;
511 args->placeFront = placeFront;
512 post(task);
513}
514
515CREATE_BRIDGE2(removeRenderNode, CanvasContext* context, RenderNode* node) {
516 args->context->removeRenderNode(args->node);
517 return nullptr;
518}
519
520void RenderProxy::removeRenderNode(RenderNode* node) {
521 SETUP_TASK(removeRenderNode);
522 args->context = mContext;
523 args->node = node;
524 post(task);
525}
526
527CREATE_BRIDGE2(drawRenderNode, CanvasContext* context, RenderNode* node) {
528 args->context->prepareAndDraw(args->node);
529 return nullptr;
530}
531
532void RenderProxy::drawRenderNode(RenderNode* node) {
533 SETUP_TASK(drawRenderNode);
534 args->context = mContext;
535 args->node = node;
536 // Be pseudo-thread-safe and don't use any member variables
537 staticPostAndWait(task);
538}
539
Skuhneb8160872015-09-22 09:51:39 -0700540CREATE_BRIDGE5(setContentDrawBounds, CanvasContext* context, int left, int top,
Skuhneea7a7fb2015-08-28 07:10:31 -0700541 int right, int bottom) {
Skuhneb8160872015-09-22 09:51:39 -0700542 args->context->setContentDrawBounds(args->left, args->top, args->right, args->bottom);
Skuhneea7a7fb2015-08-28 07:10:31 -0700543 return nullptr;
544}
545
Skuhneb8160872015-09-22 09:51:39 -0700546void RenderProxy::setContentDrawBounds(int left, int top, int right, int bottom) {
547 SETUP_TASK(setContentDrawBounds);
Skuhneea7a7fb2015-08-28 07:10:31 -0700548 args->context = mContext;
549 args->left = left;
550 args->top = top;
551 args->right = right;
552 args->bottom = bottom;
553 staticPostAndWait(task);
554}
555
John Recke248bd12015-08-05 13:53:53 -0700556CREATE_BRIDGE1(serializeDisplayListTree, CanvasContext* context) {
557 args->context->serializeDisplayListTree();
558 return nullptr;
559}
560
561void RenderProxy::serializeDisplayListTree() {
562 SETUP_TASK(serializeDisplayListTree);
563 args->context = mContext;
564 post(task);
565}
566
Andres Morales910beb82016-02-02 16:19:40 -0800567CREATE_BRIDGE2(addFrameMetricsObserver, CanvasContext* context,
568 FrameMetricsObserver* frameStatsObserver) {
569 args->context->addFrameMetricsObserver(args->frameStatsObserver);
Andres Morales06f5bc72015-12-15 15:21:31 -0800570 if (args->frameStatsObserver != nullptr) {
571 args->frameStatsObserver->decStrong(args->context);
572 }
573 return nullptr;
574}
575
Andres Morales910beb82016-02-02 16:19:40 -0800576void RenderProxy::addFrameMetricsObserver(FrameMetricsObserver* observer) {
577 SETUP_TASK(addFrameMetricsObserver);
Andres Morales06f5bc72015-12-15 15:21:31 -0800578 args->context = mContext;
579 args->frameStatsObserver = observer;
580 if (observer != nullptr) {
581 observer->incStrong(mContext);
582 }
583 post(task);
584}
585
Andres Morales910beb82016-02-02 16:19:40 -0800586CREATE_BRIDGE2(removeFrameMetricsObserver, CanvasContext* context,
587 FrameMetricsObserver* frameStatsObserver) {
588 args->context->removeFrameMetricsObserver(args->frameStatsObserver);
Andres Morales06f5bc72015-12-15 15:21:31 -0800589 if (args->frameStatsObserver != nullptr) {
590 args->frameStatsObserver->decStrong(args->context);
591 }
592 return nullptr;
593}
594
Andres Morales910beb82016-02-02 16:19:40 -0800595void RenderProxy::removeFrameMetricsObserver(FrameMetricsObserver* observer) {
596 SETUP_TASK(removeFrameMetricsObserver);
Andres Morales06f5bc72015-12-15 15:21:31 -0800597 args->context = mContext;
598 args->frameStatsObserver = observer;
599 if (observer != nullptr) {
600 observer->incStrong(mContext);
601 }
602 post(task);
603}
604
John Reck95801462016-09-01 09:44:09 -0700605CREATE_BRIDGE4(copySurfaceInto, RenderThread* thread,
606 Surface* surface, Rect srcRect, SkBitmap* bitmap) {
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500607 return (void*)args->thread->readback().copySurfaceInto(*args->surface,
608 args->srcRect, args->bitmap);
John Reck10dd0582016-03-31 16:36:16 -0700609}
610
John Reck95801462016-09-01 09:44:09 -0700611int RenderProxy::copySurfaceInto(sp<Surface>& surface, int left, int top,
612 int right, int bottom, SkBitmap* bitmap) {
John Reck10dd0582016-03-31 16:36:16 -0700613 SETUP_TASK(copySurfaceInto);
614 args->bitmap = bitmap;
615 args->surface = surface.get();
616 args->thread = &RenderThread::getInstance();
John Reck95801462016-09-01 09:44:09 -0700617 args->srcRect.set(left, top, right, bottom);
John Recke94cbc72016-04-25 13:03:44 -0700618 return static_cast<int>(
619 reinterpret_cast<intptr_t>( staticPostAndWait(task) ));
John Reck10dd0582016-03-31 16:36:16 -0700620}
621
sergeyvec4a4b12016-10-20 18:39:04 -0700622CREATE_BRIDGE2(prepareToDraw, RenderThread* thread, Bitmap* bitmap) {
Derek Sollenbergerdaf72292016-10-25 12:09:18 -0400623 CanvasContext::prepareToDraw(*args->thread, args->bitmap);
sergeyvec4a4b12016-10-20 18:39:04 -0700624 args->bitmap->unref();
John Reck43871902016-08-01 14:39:24 -0700625 args->bitmap = nullptr;
626 return nullptr;
627}
628
sergeyvec4a4b12016-10-20 18:39:04 -0700629void RenderProxy::prepareToDraw(Bitmap& bitmap) {
John Reck43871902016-08-01 14:39:24 -0700630 // If we haven't spun up a hardware accelerated window yet, there's no
631 // point in precaching these bitmaps as it can't impact jank.
632 // We also don't know if we even will spin up a hardware-accelerated
633 // window or not.
634 if (!RenderThread::hasInstance()) return;
635 RenderThread* renderThread = &RenderThread::getInstance();
636 SETUP_TASK(prepareToDraw);
637 args->thread = renderThread;
sergeyvec4a4b12016-10-20 18:39:04 -0700638 bitmap.ref();
639 args->bitmap = &bitmap;
John Reck43871902016-08-01 14:39:24 -0700640 nsecs_t lastVsync = renderThread->timeLord().latestVsync();
641 nsecs_t estimatedNextVsync = lastVsync + renderThread->timeLord().frameIntervalNanos();
642 nsecs_t timeToNextVsync = estimatedNextVsync - systemTime(CLOCK_MONOTONIC);
643 // We expect the UI thread to take 4ms and for RT to be active from VSYNC+4ms to
644 // VSYNC+12ms or so, so aim for the gap during which RT is expected to
645 // be idle
646 // TODO: Make this concept a first-class supported thing? RT could use
647 // knowledge of pending draws to better schedule this task
648 if (timeToNextVsync > -6_ms && timeToNextVsync < 1_ms) {
649 renderThread->queueAt(task, estimatedNextVsync + 8_ms);
650 } else {
651 renderThread->queue(task);
652 }
653}
654
sergeyv694d4992016-10-27 10:23:13 -0700655CREATE_BRIDGE2(allocateHardwareBitmap, RenderThread* thread, SkBitmap* bitmap) {
656 sk_sp<Bitmap> hardwareBitmap = Bitmap::allocateHardwareBitmap(*args->thread, *args->bitmap);
657 return hardwareBitmap.release();
658}
659
660sk_sp<Bitmap> RenderProxy::allocateHardwareBitmap(SkBitmap& bitmap) {
661 SETUP_TASK(allocateHardwareBitmap);
662 args->bitmap = &bitmap;
663 args->thread = &RenderThread::getInstance();
664 sk_sp<Bitmap> hardwareBitmap(reinterpret_cast<Bitmap*>(staticPostAndWait(task)));
665 return hardwareBitmap;
666}
667
sergeyv59eecb522016-11-17 17:54:57 -0800668CREATE_BRIDGE3(copyGraphicBufferInto, RenderThread* thread, GraphicBuffer* buffer, SkBitmap* bitmap) {
669 return (void*) args->thread->readback().copyGraphicBufferInto(args->buffer, args->bitmap);
670}
671
672int RenderProxy::copyGraphicBufferInto(GraphicBuffer* buffer, SkBitmap* bitmap) {
673 SETUP_TASK(copyGraphicBufferInto);
674 args->thread = &RenderThread::getInstance();
675 args->bitmap = bitmap;
676 args->buffer = buffer;
677 return static_cast<int>(reinterpret_cast<intptr_t>(staticPostAndWait(task)));
678}
679
John Reck4f02bf42014-01-03 18:09:17 -0800680void RenderProxy::post(RenderTask* task) {
681 mRenderThread.queue(task);
682}
683
684void* RenderProxy::postAndWait(MethodInvokeRenderTask* task) {
685 void* retval;
686 task->setReturnPtr(&retval);
John Reckcba287b2015-11-10 12:52:44 -0800687 SignalingRenderTask syncTask(task, &mSyncMutex, &mSyncCondition);
688 AutoMutex _lock(mSyncMutex);
689 mRenderThread.queue(&syncTask);
690 mSyncCondition.wait(mSyncMutex);
John Reck4f02bf42014-01-03 18:09:17 -0800691 return retval;
692}
693
John Reck0e89e2b2014-10-31 14:49:06 -0700694void* RenderProxy::staticPostAndWait(MethodInvokeRenderTask* task) {
695 RenderThread& thread = RenderThread::getInstance();
696 void* retval;
697 task->setReturnPtr(&retval);
Chris Craik0a24b142015-10-19 17:10:19 -0700698 thread.queueAndWait(task);
John Reck0e89e2b2014-10-31 14:49:06 -0700699 return retval;
700}
701
John Reck4f02bf42014-01-03 18:09:17 -0800702} /* namespace renderthread */
703} /* namespace uirenderer */
704} /* namespace android */