blob: 83b3e3a3ab1e8c968b19ceeebe91c1f51c3cfbd1 [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"
John Reck9a814872017-05-22 15:04:21 -070027#include "renderstate/RenderState.h"
John Reckba6adf62015-02-19 14:36:50 -080028#include "utils/Macros.h"
John Reck43871902016-08-01 14:39:24 -070029#include "utils/TimeUtils.h"
John Reck4f02bf42014-01-03 18:09:17 -080030
sergeyv59eecb522016-11-17 17:54:57 -080031#include <ui/GraphicBuffer.h>
32
John Reck4f02bf42014-01-03 18:09:17 -080033namespace android {
34namespace uirenderer {
35namespace renderthread {
36
37#define ARGS(method) method ## Args
38
John Reck19b6bcf2014-02-14 20:03:38 -080039#define CREATE_BRIDGE0(name) CREATE_BRIDGE(name,,,,,,,,)
John Reck4f02bf42014-01-03 18:09:17 -080040#define CREATE_BRIDGE1(name, a1) CREATE_BRIDGE(name, a1,,,,,,,)
41#define CREATE_BRIDGE2(name, a1, a2) CREATE_BRIDGE(name, a1,a2,,,,,,)
42#define CREATE_BRIDGE3(name, a1, a2, a3) CREATE_BRIDGE(name, a1,a2,a3,,,,,)
43#define CREATE_BRIDGE4(name, a1, a2, a3, a4) CREATE_BRIDGE(name, a1,a2,a3,a4,,,,)
Chris Craik797b95b2014-05-20 18:10:25 -070044#define CREATE_BRIDGE5(name, a1, a2, a3, a4, a5) CREATE_BRIDGE(name, a1,a2,a3,a4,a5,,,)
Chris Craik058fc642014-07-23 18:19:28 -070045#define CREATE_BRIDGE6(name, a1, a2, a3, a4, a5, a6) CREATE_BRIDGE(name, a1,a2,a3,a4,a5,a6,,)
46#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 -080047#define CREATE_BRIDGE(name, a1, a2, a3, a4, a5, a6, a7, a8) \
48 typedef struct { \
49 a1; a2; a3; a4; a5; a6; a7; a8; \
50 } ARGS(name); \
John Reck43871902016-08-01 14:39:24 -070051 static_assert(std::is_trivially_destructible<ARGS(name)>::value, \
52 "Error, ARGS must be trivially destructible!"); \
John Reck4f02bf42014-01-03 18:09:17 -080053 static void* Bridge_ ## name(ARGS(name)* args)
54
55#define SETUP_TASK(method) \
56 LOG_ALWAYS_FATAL_IF( METHOD_INVOKE_PAYLOAD_SIZE < sizeof(ARGS(method)), \
Mark Salyzyn546f3532014-06-10 12:29:14 -070057 "METHOD_INVOKE_PAYLOAD_SIZE %zu is smaller than sizeof(" #method "Args) %zu", \
John Reck4f02bf42014-01-03 18:09:17 -080058 METHOD_INVOKE_PAYLOAD_SIZE, sizeof(ARGS(method))); \
John Recke2c45522014-04-07 17:31:44 -070059 MethodInvokeRenderTask* task = new MethodInvokeRenderTask((RunnableMethod) Bridge_ ## method); \
John Reck4f02bf42014-01-03 18:09:17 -080060 ARGS(method) *args = (ARGS(method) *) task->payload()
61
John Reck119907c2014-08-14 09:02:01 -070062CREATE_BRIDGE4(createContext, RenderThread* thread, bool translucent,
63 RenderNode* rootRenderNode, IContextFactory* contextFactory) {
Stan Iliev03de0742016-07-07 12:35:54 -040064 return CanvasContext::create(*args->thread, args->translucent,
John Reck119907c2014-08-14 09:02:01 -070065 args->rootRenderNode, args->contextFactory);
John Reck4f02bf42014-01-03 18:09:17 -080066}
67
John Reck119907c2014-08-14 09:02:01 -070068RenderProxy::RenderProxy(bool translucent, RenderNode* rootRenderNode, IContextFactory* contextFactory)
John Reck4f02bf42014-01-03 18:09:17 -080069 : mRenderThread(RenderThread::getInstance())
Chris Craikd41c4d82015-01-05 15:51:13 -080070 , mContext(nullptr) {
John Reck4f02bf42014-01-03 18:09:17 -080071 SETUP_TASK(createContext);
72 args->translucent = translucent;
John Recke45b1fd2014-04-15 09:50:16 -070073 args->rootRenderNode = rootRenderNode;
John Reck3b202512014-06-23 13:13:08 -070074 args->thread = &mRenderThread;
John Reck119907c2014-08-14 09:02:01 -070075 args->contextFactory = contextFactory;
John Reck4f02bf42014-01-03 18:09:17 -080076 mContext = (CanvasContext*) postAndWait(task);
Skuhneea7a7fb2015-08-28 07:10:31 -070077 mDrawFrameTask.setContext(&mRenderThread, mContext, rootRenderNode);
John Reck4f02bf42014-01-03 18:09:17 -080078}
79
80RenderProxy::~RenderProxy() {
81 destroyContext();
82}
83
84CREATE_BRIDGE1(destroyContext, CanvasContext* context) {
85 delete args->context;
Chris Craikd41c4d82015-01-05 15:51:13 -080086 return nullptr;
John Reck4f02bf42014-01-03 18:09:17 -080087}
88
89void RenderProxy::destroyContext() {
90 if (mContext) {
91 SETUP_TASK(destroyContext);
92 args->context = mContext;
Chris Craikd41c4d82015-01-05 15:51:13 -080093 mContext = nullptr;
Skuhneea7a7fb2015-08-28 07:10:31 -070094 mDrawFrameTask.setContext(nullptr, nullptr, nullptr);
John Reck668f0e32014-03-26 15:10:40 -070095 // This is also a fence as we need to be certain that there are no
96 // outstanding mDrawFrame tasks posted before it is destroyed
97 postAndWait(task);
John Reck4f02bf42014-01-03 18:09:17 -080098 }
99}
100
John Reck1125d1f2014-10-23 11:02:19 -0700101CREATE_BRIDGE2(setSwapBehavior, CanvasContext* context, SwapBehavior swapBehavior) {
102 args->context->setSwapBehavior(args->swapBehavior);
Chris Craikd41c4d82015-01-05 15:51:13 -0800103 return nullptr;
John Reck1125d1f2014-10-23 11:02:19 -0700104}
105
106void RenderProxy::setSwapBehavior(SwapBehavior swapBehavior) {
107 SETUP_TASK(setSwapBehavior);
108 args->context = mContext;
109 args->swapBehavior = swapBehavior;
110 post(task);
111}
112
John Reckfe5e7b72014-05-23 17:42:28 -0700113CREATE_BRIDGE1(loadSystemProperties, CanvasContext* context) {
John Recke4280ba2014-05-05 16:39:37 -0700114 bool needsRedraw = false;
115 if (Caches::hasInstance()) {
Chris Craik2507c342015-05-04 14:36:49 -0700116 needsRedraw = Properties::load();
John Recke4280ba2014-05-05 16:39:37 -0700117 }
Chris Craik2507c342015-05-04 14:36:49 -0700118 if (args->context->profiler().consumeProperties()) {
John Reckfe5e7b72014-05-23 17:42:28 -0700119 needsRedraw = true;
120 }
John Recke4280ba2014-05-05 16:39:37 -0700121 return (void*) needsRedraw;
122}
123
124bool RenderProxy::loadSystemProperties() {
125 SETUP_TASK(loadSystemProperties);
John Reckfe5e7b72014-05-23 17:42:28 -0700126 args->context = mContext;
John Recke4280ba2014-05-05 16:39:37 -0700127 return (bool) postAndWait(task);
128}
129
John Reckb36016c2015-03-11 08:50:53 -0700130CREATE_BRIDGE2(setName, CanvasContext* context, const char* name) {
131 args->context->setName(std::string(args->name));
132 return nullptr;
133}
134
135void RenderProxy::setName(const char* name) {
136 SETUP_TASK(setName);
137 args->context = mContext;
138 args->name = name;
Chris Craik2507c342015-05-04 14:36:49 -0700139 postAndWait(task); // block since name/value pointers owned by caller
John Reckb36016c2015-03-11 08:50:53 -0700140}
141
John Reckf6481082016-02-02 15:18:23 -0800142CREATE_BRIDGE2(initialize, CanvasContext* context, Surface* surface) {
143 args->context->initialize(args->surface);
Thomas Buhot0bcd0cb2015-12-04 12:18:03 +0100144 return nullptr;
John Reck4f02bf42014-01-03 18:09:17 -0800145}
146
John Reckf6481082016-02-02 15:18:23 -0800147void RenderProxy::initialize(const sp<Surface>& surface) {
John Reck4f02bf42014-01-03 18:09:17 -0800148 SETUP_TASK(initialize);
149 args->context = mContext;
John Reckf6481082016-02-02 15:18:23 -0800150 args->surface = surface.get();
Thomas Buhot0bcd0cb2015-12-04 12:18:03 +0100151 post(task);
John Reck4f02bf42014-01-03 18:09:17 -0800152}
153
John Reckf6481082016-02-02 15:18:23 -0800154CREATE_BRIDGE2(updateSurface, CanvasContext* context, Surface* surface) {
155 args->context->updateSurface(args->surface);
Chris Craikd41c4d82015-01-05 15:51:13 -0800156 return nullptr;
John Reck4f02bf42014-01-03 18:09:17 -0800157}
158
John Reckf6481082016-02-02 15:18:23 -0800159void RenderProxy::updateSurface(const sp<Surface>& surface) {
John Reck4f02bf42014-01-03 18:09:17 -0800160 SETUP_TASK(updateSurface);
161 args->context = mContext;
John Reckf6481082016-02-02 15:18:23 -0800162 args->surface = surface.get();
John Reckcd682122016-08-09 12:09:03 -0700163 post(task);
John Reckf7d9c1d2014-04-09 10:01:03 -0700164}
165
John Reckf6481082016-02-02 15:18:23 -0800166CREATE_BRIDGE2(pauseSurface, CanvasContext* context, Surface* surface) {
167 return (void*) args->context->pauseSurface(args->surface);
John Reckf7d9c1d2014-04-09 10:01:03 -0700168}
169
John Reckf6481082016-02-02 15:18:23 -0800170bool RenderProxy::pauseSurface(const sp<Surface>& surface) {
John Reckf7d9c1d2014-04-09 10:01:03 -0700171 SETUP_TASK(pauseSurface);
172 args->context = mContext;
John Reckf6481082016-02-02 15:18:23 -0800173 args->surface = surface.get();
John Reck01a5ea32014-12-03 13:01:07 -0800174 return (bool) postAndWait(task);
John Reck4f02bf42014-01-03 18:09:17 -0800175}
176
John Reck8afcc762016-04-13 10:24:06 -0700177CREATE_BRIDGE2(setStopped, CanvasContext* context, bool stopped) {
178 args->context->setStopped(args->stopped);
179 return nullptr;
180}
181
182void RenderProxy::setStopped(bool stopped) {
183 SETUP_TASK(setStopped);
184 args->context = mContext;
185 args->stopped = stopped;
186 postAndWait(task);
187}
188
John Reckab1080c2016-06-21 16:24:20 -0700189CREATE_BRIDGE4(setup, CanvasContext* context,
Alan Viverette50210d92015-05-14 18:05:36 -0700190 float lightRadius, uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
John Reckab1080c2016-06-21 16:24:20 -0700191 args->context->setup(args->lightRadius,
Chris Craik058fc642014-07-23 18:19:28 -0700192 args->ambientShadowAlpha, args->spotShadowAlpha);
Chris Craikd41c4d82015-01-05 15:51:13 -0800193 return nullptr;
John Reck4f02bf42014-01-03 18:09:17 -0800194}
195
John Reckab1080c2016-06-21 16:24:20 -0700196void RenderProxy::setup(float lightRadius,
John Reckb36016c2015-03-11 08:50:53 -0700197 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
John Reck4f02bf42014-01-03 18:09:17 -0800198 SETUP_TASK(setup);
199 args->context = mContext;
Chris Craik797b95b2014-05-20 18:10:25 -0700200 args->lightRadius = lightRadius;
Chris Craik058fc642014-07-23 18:19:28 -0700201 args->ambientShadowAlpha = ambientShadowAlpha;
202 args->spotShadowAlpha = spotShadowAlpha;
John Reck4f02bf42014-01-03 18:09:17 -0800203 post(task);
204}
205
Alan Viverette50210d92015-05-14 18:05:36 -0700206CREATE_BRIDGE2(setLightCenter, CanvasContext* context, Vector3 lightCenter) {
207 args->context->setLightCenter(args->lightCenter);
208 return nullptr;
209}
210
211void RenderProxy::setLightCenter(const Vector3& lightCenter) {
212 SETUP_TASK(setLightCenter);
213 args->context = mContext;
214 args->lightCenter = lightCenter;
215 post(task);
216}
217
John Reck63a06672014-05-07 13:45:54 -0700218CREATE_BRIDGE2(setOpaque, CanvasContext* context, bool opaque) {
219 args->context->setOpaque(args->opaque);
Chris Craikd41c4d82015-01-05 15:51:13 -0800220 return nullptr;
John Reck63a06672014-05-07 13:45:54 -0700221}
222
223void RenderProxy::setOpaque(bool opaque) {
224 SETUP_TASK(setOpaque);
225 args->context = mContext;
226 args->opaque = opaque;
227 post(task);
228}
229
Romain Guy26a2b972017-04-17 09:39:51 -0700230CREATE_BRIDGE2(setWideGamut, CanvasContext* context, bool wideGamut) {
231 args->context->setWideGamut(args->wideGamut);
232 return nullptr;
233}
234
235void RenderProxy::setWideGamut(bool wideGamut) {
236 SETUP_TASK(setWideGamut);
237 args->context = mContext;
238 args->wideGamut = wideGamut;
239 post(task);
240}
241
John Reckba6adf62015-02-19 14:36:50 -0800242int64_t* RenderProxy::frameInfo() {
243 return mDrawFrameTask.frameInfo();
244}
245
John Reck2de950d2017-01-25 10:58:30 -0800246int RenderProxy::syncAndDrawFrame() {
247 return mDrawFrameTask.drawFrame();
John Reck4f02bf42014-01-03 18:09:17 -0800248}
249
John Reck2de950d2017-01-25 10:58:30 -0800250CREATE_BRIDGE1(destroy, CanvasContext* context) {
251 args->context->destroy();
Chris Craikd41c4d82015-01-05 15:51:13 -0800252 return nullptr;
John Reck4f02bf42014-01-03 18:09:17 -0800253}
254
John Reck2de950d2017-01-25 10:58:30 -0800255void RenderProxy::destroy() {
John Reck17035b02014-09-03 07:39:53 -0700256 SETUP_TASK(destroy);
John Reck4f02bf42014-01-03 18:09:17 -0800257 args->context = mContext;
John Reckfae904d2014-04-14 11:01:57 -0700258 // destroyCanvasAndSurface() needs a fence as when it returns the
259 // underlying BufferQueue is going to be released from under
260 // the render thread.
261 postAndWait(task);
John Reck4f02bf42014-01-03 18:09:17 -0800262}
263
John Reck3b202512014-06-23 13:13:08 -0700264CREATE_BRIDGE2(invokeFunctor, RenderThread* thread, Functor* functor) {
265 CanvasContext::invokeFunctor(*args->thread, args->functor);
Chris Craikd41c4d82015-01-05 15:51:13 -0800266 return nullptr;
John Reck0d1f6342014-03-28 20:30:27 -0700267}
268
269void RenderProxy::invokeFunctor(Functor* functor, bool waitForCompletion) {
John Reckd3d8daf2014-04-10 15:00:13 -0700270 ATRACE_CALL();
John Reck3b202512014-06-23 13:13:08 -0700271 RenderThread& thread = RenderThread::getInstance();
John Reck0d1f6342014-03-28 20:30:27 -0700272 SETUP_TASK(invokeFunctor);
John Reck3b202512014-06-23 13:13:08 -0700273 args->thread = &thread;
John Reck0d1f6342014-03-28 20:30:27 -0700274 args->functor = functor;
275 if (waitForCompletion) {
John Reck3b202512014-06-23 13:13:08 -0700276 // waitForCompletion = true is expected to be fairly rare and only
277 // happen in destruction. Thus it should be fine to temporarily
278 // create a Mutex
John Reck0e89e2b2014-10-31 14:49:06 -0700279 staticPostAndWait(task);
John Reck0d1f6342014-03-28 20:30:27 -0700280 } else {
John Reck3b202512014-06-23 13:13:08 -0700281 thread.queue(task);
John Reck0d1f6342014-03-28 20:30:27 -0700282 }
283}
284
John Reckc36df952015-07-29 10:09:36 -0700285CREATE_BRIDGE1(createTextureLayer, CanvasContext* context) {
Derek Sollenberger56ad6ec2016-07-22 12:13:32 -0400286 return args->context->createTextureLayer();
John Reck19b6bcf2014-02-14 20:03:38 -0800287}
288
289DeferredLayerUpdater* RenderProxy::createTextureLayer() {
290 SETUP_TASK(createTextureLayer);
John Reck1949e792014-04-08 15:18:56 -0700291 args->context = mContext;
John Reck19b6bcf2014-02-14 20:03:38 -0800292 void* retval = postAndWait(task);
293 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval);
John Reck19b6bcf2014-02-14 20:03:38 -0800294 return layer;
295}
296
John Reck2de950d2017-01-25 10:58:30 -0800297CREATE_BRIDGE2(buildLayer, CanvasContext* context, RenderNode* node) {
298 args->context->buildLayer(args->node);
Chris Craikd41c4d82015-01-05 15:51:13 -0800299 return nullptr;
John Reck3e824952014-08-20 10:08:39 -0700300}
301
John Reck2de950d2017-01-25 10:58:30 -0800302void RenderProxy::buildLayer(RenderNode* node) {
John Reck3e824952014-08-20 10:08:39 -0700303 SETUP_TASK(buildLayer);
304 args->context = mContext;
305 args->node = node;
306 postAndWait(task);
307}
308
John Reck19b6bcf2014-02-14 20:03:38 -0800309CREATE_BRIDGE3(copyLayerInto, CanvasContext* context, DeferredLayerUpdater* layer,
310 SkBitmap* bitmap) {
311 bool success = args->context->copyLayerInto(args->layer, args->bitmap);
312 return (void*) success;
313}
314
John Reck3731dc22015-04-13 15:20:29 -0700315bool RenderProxy::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap& bitmap) {
John Reck19b6bcf2014-02-14 20:03:38 -0800316 SETUP_TASK(copyLayerInto);
317 args->context = mContext;
318 args->layer = layer;
John Reck3731dc22015-04-13 15:20:29 -0700319 args->bitmap = &bitmap;
John Reck19b6bcf2014-02-14 20:03:38 -0800320 return (bool) postAndWait(task);
321}
322
John Reckd72e0a32014-05-29 18:56:11 -0700323void RenderProxy::pushLayerUpdate(DeferredLayerUpdater* layer) {
324 mDrawFrameTask.pushLayerUpdate(layer);
325}
326
327void RenderProxy::cancelLayerUpdate(DeferredLayerUpdater* layer) {
328 mDrawFrameTask.removeLayerUpdate(layer);
John Reck19b6bcf2014-02-14 20:03:38 -0800329}
330
John Reck918ad522014-06-27 14:45:25 -0700331CREATE_BRIDGE1(detachSurfaceTexture, DeferredLayerUpdater* layer) {
332 args->layer->detachSurfaceTexture();
Chris Craikd41c4d82015-01-05 15:51:13 -0800333 return nullptr;
John Reck918ad522014-06-27 14:45:25 -0700334}
335
336void RenderProxy::detachSurfaceTexture(DeferredLayerUpdater* layer) {
337 SETUP_TASK(detachSurfaceTexture);
338 args->layer = layer;
339 postAndWait(task);
340}
341
John Reck2de950d2017-01-25 10:58:30 -0800342CREATE_BRIDGE1(destroyHardwareResources, CanvasContext* context) {
343 args->context->destroyHardwareResources();
Chris Craikd41c4d82015-01-05 15:51:13 -0800344 return nullptr;
John Recke1628b72014-05-23 15:11:19 -0700345}
346
John Reck2de950d2017-01-25 10:58:30 -0800347void RenderProxy::destroyHardwareResources() {
John Reckf47a5942014-06-30 16:20:04 -0700348 SETUP_TASK(destroyHardwareResources);
John Recke1628b72014-05-23 15:11:19 -0700349 args->context = mContext;
John Reck51f2d602016-04-06 07:50:47 -0700350 postAndWait(task);
John Recke1628b72014-05-23 15:11:19 -0700351}
352
Chris Craik2507c342015-05-04 14:36:49 -0700353CREATE_BRIDGE2(trimMemory, RenderThread* thread, int level) {
John Reckf47a5942014-06-30 16:20:04 -0700354 CanvasContext::trimMemory(*args->thread, args->level);
Chris Craikd41c4d82015-01-05 15:51:13 -0800355 return nullptr;
John Reckf47a5942014-06-30 16:20:04 -0700356}
357
358void RenderProxy::trimMemory(int level) {
John Reckcd3a22c2014-08-06 13:33:59 -0700359 // Avoid creating a RenderThread to do a trimMemory.
360 if (RenderThread::hasInstance()) {
361 RenderThread& thread = RenderThread::getInstance();
Chris Craik2507c342015-05-04 14:36:49 -0700362 SETUP_TASK(trimMemory);
John Reckcd3a22c2014-08-06 13:33:59 -0700363 args->thread = &thread;
364 args->level = level;
365 thread.queue(task);
366 }
John Reckf47a5942014-06-30 16:20:04 -0700367}
368
Chris Craik2507c342015-05-04 14:36:49 -0700369CREATE_BRIDGE2(overrideProperty, const char* name, const char* value) {
370 Properties::overrideProperty(args->name, args->value);
371 return nullptr;
372}
373
374void RenderProxy::overrideProperty(const char* name, const char* value) {
Chris Craik2507c342015-05-04 14:36:49 -0700375 SETUP_TASK(overrideProperty);
376 args->name = name;
377 args->value = value;
378 staticPostAndWait(task); // expensive, but block here since name/value pointers owned by caller
379}
380
John Reck28ad7b52014-04-07 16:59:25 -0700381CREATE_BRIDGE0(fence) {
382 // Intentionally empty
Chris Craikd41c4d82015-01-05 15:51:13 -0800383 return nullptr;
John Reck28ad7b52014-04-07 16:59:25 -0700384}
385
Andreas Gampe64bb4132014-11-22 00:35:09 +0000386template <typename T>
387void UNUSED(T t) {}
388
John Reck28ad7b52014-04-07 16:59:25 -0700389void RenderProxy::fence() {
390 SETUP_TASK(fence);
Andreas Gampe1e196742014-11-10 15:23:43 -0800391 UNUSED(args);
John Reck28ad7b52014-04-07 16:59:25 -0700392 postAndWait(task);
393}
394
Thomas Buhotc0a0e1a2016-01-18 10:31:58 +0100395void RenderProxy::staticFence() {
396 SETUP_TASK(fence);
397 UNUSED(args);
398 staticPostAndWait(task);
399}
400
John Reckf47a5942014-06-30 16:20:04 -0700401CREATE_BRIDGE1(stopDrawing, CanvasContext* context) {
402 args->context->stopDrawing();
Chris Craikd41c4d82015-01-05 15:51:13 -0800403 return nullptr;
John Reckf47a5942014-06-30 16:20:04 -0700404}
405
406void RenderProxy::stopDrawing() {
407 SETUP_TASK(stopDrawing);
408 args->context = mContext;
409 postAndWait(task);
410}
411
John Recka5dda642014-05-22 15:43:54 -0700412CREATE_BRIDGE1(notifyFramePending, CanvasContext* context) {
413 args->context->notifyFramePending();
Chris Craikd41c4d82015-01-05 15:51:13 -0800414 return nullptr;
John Recka5dda642014-05-22 15:43:54 -0700415}
416
417void RenderProxy::notifyFramePending() {
418 SETUP_TASK(notifyFramePending);
419 args->context = mContext;
420 mRenderThread.queueAtFront(task);
421}
422
John Reck7f2e5e32015-05-05 11:00:53 -0700423CREATE_BRIDGE4(dumpProfileInfo, CanvasContext* context, RenderThread* thread,
424 int fd, int dumpFlags) {
John Reckfe5e7b72014-05-23 17:42:28 -0700425 args->context->profiler().dumpData(args->fd);
Chris Craik53e51e42015-06-01 10:35:35 -0700426 if (args->dumpFlags & DumpFlags::FrameStats) {
John Reckba6adf62015-02-19 14:36:50 -0800427 args->context->dumpFrames(args->fd);
428 }
Chris Craik53e51e42015-06-01 10:35:35 -0700429 if (args->dumpFlags & DumpFlags::Reset) {
John Reckba6adf62015-02-19 14:36:50 -0800430 args->context->resetFrameStats();
431 }
John Recka41f2442016-04-07 16:36:57 -0700432 if (args->dumpFlags & DumpFlags::JankStats) {
433 args->thread->jankTracker().dump(args->fd);
434 }
Chris Craikd41c4d82015-01-05 15:51:13 -0800435 return nullptr;
John Reckfe5e7b72014-05-23 17:42:28 -0700436}
437
John Reckba6adf62015-02-19 14:36:50 -0800438void RenderProxy::dumpProfileInfo(int fd, int dumpFlags) {
John Reckfe5e7b72014-05-23 17:42:28 -0700439 SETUP_TASK(dumpProfileInfo);
440 args->context = mContext;
John Reck7f2e5e32015-05-05 11:00:53 -0700441 args->thread = &mRenderThread;
John Reckfe5e7b72014-05-23 17:42:28 -0700442 args->fd = fd;
John Reckba6adf62015-02-19 14:36:50 -0800443 args->dumpFlags = dumpFlags;
John Reckfe5e7b72014-05-23 17:42:28 -0700444 postAndWait(task);
445}
446
John Reck7f2e5e32015-05-05 11:00:53 -0700447CREATE_BRIDGE1(resetProfileInfo, CanvasContext* context) {
448 args->context->resetFrameStats();
449 return nullptr;
450}
451
452void RenderProxy::resetProfileInfo() {
453 SETUP_TASK(resetProfileInfo);
454 args->context = mContext;
455 postAndWait(task);
456}
457
John Reckf1480762016-07-03 18:28:25 -0700458CREATE_BRIDGE2(frameTimePercentile, RenderThread* thread, int percentile) {
459 return reinterpret_cast<void*>(static_cast<uintptr_t>(
460 args->thread->jankTracker().findPercentile(args->percentile)));
461}
462
463uint32_t RenderProxy::frameTimePercentile(int p) {
464 SETUP_TASK(frameTimePercentile);
465 args->thread = &mRenderThread;
466 args->percentile = p;
467 return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
468 postAndWait(task)));
469}
470
John Reckba6adf62015-02-19 14:36:50 -0800471CREATE_BRIDGE2(dumpGraphicsMemory, int fd, RenderThread* thread) {
472 args->thread->jankTracker().dump(args->fd);
473
Chris Craik2ae07332015-01-21 14:22:39 -0800474 FILE *file = fdopen(args->fd, "a");
475 if (Caches::hasInstance()) {
476 String8 cachesLog;
477 Caches::getInstance().dumpMemoryUsage(cachesLog);
478 fprintf(file, "\nCaches:\n%s\n", cachesLog.string());
479 } else {
480 fprintf(file, "\nNo caches instance.\n");
481 }
Chris Craikff3edce2016-01-14 10:04:08 -0800482 fprintf(file, "\nPipeline=FrameBuilder\n");
Chris Craik2ae07332015-01-21 14:22:39 -0800483 fflush(file);
Chris Craikd41c4d82015-01-05 15:51:13 -0800484 return nullptr;
John Reck0e89e2b2014-10-31 14:49:06 -0700485}
486
Chris Craik2ae07332015-01-21 14:22:39 -0800487void RenderProxy::dumpGraphicsMemory(int fd) {
youngmin0822.leec80c9ad2015-03-20 21:22:32 +0900488 if (!RenderThread::hasInstance()) return;
Chris Craik2ae07332015-01-21 14:22:39 -0800489 SETUP_TASK(dumpGraphicsMemory);
John Reck0e89e2b2014-10-31 14:49:06 -0700490 args->fd = fd;
John Reckba6adf62015-02-19 14:36:50 -0800491 args->thread = &RenderThread::getInstance();
John Reck0e89e2b2014-10-31 14:49:06 -0700492 staticPostAndWait(task);
493}
494
John Reckedc524c2015-03-18 15:24:33 -0700495CREATE_BRIDGE2(setProcessStatsBuffer, RenderThread* thread, int fd) {
496 args->thread->jankTracker().switchStorageToAshmem(args->fd);
497 close(args->fd);
498 return nullptr;
499}
500
501void RenderProxy::setProcessStatsBuffer(int fd) {
502 SETUP_TASK(setProcessStatsBuffer);
John Reckdf1742e2017-01-19 15:56:21 -0800503 auto& rt = RenderThread::getInstance();
504 args->thread = &rt;
John Reckedc524c2015-03-18 15:24:33 -0700505 args->fd = dup(fd);
John Reckdf1742e2017-01-19 15:56:21 -0800506 rt.queue(task);
507}
508
509CREATE_BRIDGE1(rotateProcessStatsBuffer, RenderThread* thread) {
510 args->thread->jankTracker().rotateStorage();
511 return nullptr;
512}
513
514void RenderProxy::rotateProcessStatsBuffer() {
515 SETUP_TASK(rotateProcessStatsBuffer);
516 auto& rt = RenderThread::getInstance();
517 args->thread = &rt;
518 rt.queue(task);
John Reckedc524c2015-03-18 15:24:33 -0700519}
520
Tim Murray33eb07f2016-06-10 10:03:20 -0700521int RenderProxy::getRenderThreadTid() {
522 return mRenderThread.getTid();
523}
524
Skuhneea7a7fb2015-08-28 07:10:31 -0700525CREATE_BRIDGE3(addRenderNode, CanvasContext* context, RenderNode* node, bool placeFront) {
526 args->context->addRenderNode(args->node, args->placeFront);
527 return nullptr;
528}
529
530void RenderProxy::addRenderNode(RenderNode* node, bool placeFront) {
531 SETUP_TASK(addRenderNode);
532 args->context = mContext;
533 args->node = node;
534 args->placeFront = placeFront;
535 post(task);
536}
537
538CREATE_BRIDGE2(removeRenderNode, CanvasContext* context, RenderNode* node) {
539 args->context->removeRenderNode(args->node);
540 return nullptr;
541}
542
543void RenderProxy::removeRenderNode(RenderNode* node) {
544 SETUP_TASK(removeRenderNode);
545 args->context = mContext;
546 args->node = node;
547 post(task);
548}
549
550CREATE_BRIDGE2(drawRenderNode, CanvasContext* context, RenderNode* node) {
551 args->context->prepareAndDraw(args->node);
552 return nullptr;
553}
554
555void RenderProxy::drawRenderNode(RenderNode* node) {
556 SETUP_TASK(drawRenderNode);
557 args->context = mContext;
558 args->node = node;
559 // Be pseudo-thread-safe and don't use any member variables
560 staticPostAndWait(task);
561}
562
Skuhneb8160872015-09-22 09:51:39 -0700563CREATE_BRIDGE5(setContentDrawBounds, CanvasContext* context, int left, int top,
Skuhneea7a7fb2015-08-28 07:10:31 -0700564 int right, int bottom) {
Skuhneb8160872015-09-22 09:51:39 -0700565 args->context->setContentDrawBounds(args->left, args->top, args->right, args->bottom);
Skuhneea7a7fb2015-08-28 07:10:31 -0700566 return nullptr;
567}
568
Skuhneb8160872015-09-22 09:51:39 -0700569void RenderProxy::setContentDrawBounds(int left, int top, int right, int bottom) {
570 SETUP_TASK(setContentDrawBounds);
Skuhneea7a7fb2015-08-28 07:10:31 -0700571 args->context = mContext;
572 args->left = left;
573 args->top = top;
574 args->right = right;
575 args->bottom = bottom;
576 staticPostAndWait(task);
577}
578
John Recke248bd12015-08-05 13:53:53 -0700579CREATE_BRIDGE1(serializeDisplayListTree, CanvasContext* context) {
580 args->context->serializeDisplayListTree();
581 return nullptr;
582}
583
584void RenderProxy::serializeDisplayListTree() {
585 SETUP_TASK(serializeDisplayListTree);
586 args->context = mContext;
587 post(task);
588}
589
Andres Morales910beb82016-02-02 16:19:40 -0800590CREATE_BRIDGE2(addFrameMetricsObserver, CanvasContext* context,
591 FrameMetricsObserver* frameStatsObserver) {
592 args->context->addFrameMetricsObserver(args->frameStatsObserver);
Andres Morales06f5bc72015-12-15 15:21:31 -0800593 if (args->frameStatsObserver != nullptr) {
594 args->frameStatsObserver->decStrong(args->context);
595 }
596 return nullptr;
597}
598
Andres Morales910beb82016-02-02 16:19:40 -0800599void RenderProxy::addFrameMetricsObserver(FrameMetricsObserver* observer) {
600 SETUP_TASK(addFrameMetricsObserver);
Andres Morales06f5bc72015-12-15 15:21:31 -0800601 args->context = mContext;
602 args->frameStatsObserver = observer;
603 if (observer != nullptr) {
604 observer->incStrong(mContext);
605 }
606 post(task);
607}
608
Andres Morales910beb82016-02-02 16:19:40 -0800609CREATE_BRIDGE2(removeFrameMetricsObserver, CanvasContext* context,
610 FrameMetricsObserver* frameStatsObserver) {
611 args->context->removeFrameMetricsObserver(args->frameStatsObserver);
Andres Morales06f5bc72015-12-15 15:21:31 -0800612 if (args->frameStatsObserver != nullptr) {
613 args->frameStatsObserver->decStrong(args->context);
614 }
615 return nullptr;
616}
617
Andres Morales910beb82016-02-02 16:19:40 -0800618void RenderProxy::removeFrameMetricsObserver(FrameMetricsObserver* observer) {
619 SETUP_TASK(removeFrameMetricsObserver);
Andres Morales06f5bc72015-12-15 15:21:31 -0800620 args->context = mContext;
621 args->frameStatsObserver = observer;
622 if (observer != nullptr) {
623 observer->incStrong(mContext);
624 }
625 post(task);
626}
627
John Reck95801462016-09-01 09:44:09 -0700628CREATE_BRIDGE4(copySurfaceInto, RenderThread* thread,
629 Surface* surface, Rect srcRect, SkBitmap* bitmap) {
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500630 return (void*)args->thread->readback().copySurfaceInto(*args->surface,
631 args->srcRect, args->bitmap);
John Reck10dd0582016-03-31 16:36:16 -0700632}
633
John Reck95801462016-09-01 09:44:09 -0700634int RenderProxy::copySurfaceInto(sp<Surface>& surface, int left, int top,
635 int right, int bottom, SkBitmap* bitmap) {
John Reck10dd0582016-03-31 16:36:16 -0700636 SETUP_TASK(copySurfaceInto);
637 args->bitmap = bitmap;
638 args->surface = surface.get();
639 args->thread = &RenderThread::getInstance();
John Reck95801462016-09-01 09:44:09 -0700640 args->srcRect.set(left, top, right, bottom);
John Recke94cbc72016-04-25 13:03:44 -0700641 return static_cast<int>(
642 reinterpret_cast<intptr_t>( staticPostAndWait(task) ));
John Reck10dd0582016-03-31 16:36:16 -0700643}
644
sergeyvec4a4b12016-10-20 18:39:04 -0700645CREATE_BRIDGE2(prepareToDraw, RenderThread* thread, Bitmap* bitmap) {
Derek Sollenbergerdaf72292016-10-25 12:09:18 -0400646 CanvasContext::prepareToDraw(*args->thread, args->bitmap);
sergeyvec4a4b12016-10-20 18:39:04 -0700647 args->bitmap->unref();
John Reck43871902016-08-01 14:39:24 -0700648 args->bitmap = nullptr;
649 return nullptr;
650}
651
sergeyvec4a4b12016-10-20 18:39:04 -0700652void RenderProxy::prepareToDraw(Bitmap& bitmap) {
John Reck43871902016-08-01 14:39:24 -0700653 // If we haven't spun up a hardware accelerated window yet, there's no
654 // point in precaching these bitmaps as it can't impact jank.
655 // We also don't know if we even will spin up a hardware-accelerated
656 // window or not.
657 if (!RenderThread::hasInstance()) return;
658 RenderThread* renderThread = &RenderThread::getInstance();
659 SETUP_TASK(prepareToDraw);
660 args->thread = renderThread;
sergeyvec4a4b12016-10-20 18:39:04 -0700661 bitmap.ref();
662 args->bitmap = &bitmap;
John Reck43871902016-08-01 14:39:24 -0700663 nsecs_t lastVsync = renderThread->timeLord().latestVsync();
664 nsecs_t estimatedNextVsync = lastVsync + renderThread->timeLord().frameIntervalNanos();
665 nsecs_t timeToNextVsync = estimatedNextVsync - systemTime(CLOCK_MONOTONIC);
666 // We expect the UI thread to take 4ms and for RT to be active from VSYNC+4ms to
667 // VSYNC+12ms or so, so aim for the gap during which RT is expected to
668 // be idle
669 // TODO: Make this concept a first-class supported thing? RT could use
670 // knowledge of pending draws to better schedule this task
671 if (timeToNextVsync > -6_ms && timeToNextVsync < 1_ms) {
672 renderThread->queueAt(task, estimatedNextVsync + 8_ms);
673 } else {
674 renderThread->queue(task);
675 }
676}
677
sergeyv694d4992016-10-27 10:23:13 -0700678CREATE_BRIDGE2(allocateHardwareBitmap, RenderThread* thread, SkBitmap* bitmap) {
679 sk_sp<Bitmap> hardwareBitmap = Bitmap::allocateHardwareBitmap(*args->thread, *args->bitmap);
680 return hardwareBitmap.release();
681}
682
683sk_sp<Bitmap> RenderProxy::allocateHardwareBitmap(SkBitmap& bitmap) {
684 SETUP_TASK(allocateHardwareBitmap);
685 args->bitmap = &bitmap;
686 args->thread = &RenderThread::getInstance();
687 sk_sp<Bitmap> hardwareBitmap(reinterpret_cast<Bitmap*>(staticPostAndWait(task)));
688 return hardwareBitmap;
689}
690
sergeyv59eecb522016-11-17 17:54:57 -0800691CREATE_BRIDGE3(copyGraphicBufferInto, RenderThread* thread, GraphicBuffer* buffer, SkBitmap* bitmap) {
692 return (void*) args->thread->readback().copyGraphicBufferInto(args->buffer, args->bitmap);
693}
694
695int RenderProxy::copyGraphicBufferInto(GraphicBuffer* buffer, SkBitmap* bitmap) {
Stan Iliev6983bc42017-02-02 14:11:53 -0500696 RenderThread& thread = RenderThread::getInstance();
697 if (Properties::isSkiaEnabled() && gettid() == thread.getTid()) {
698 //TODO: fix everything that hits this. We should never be triggering a readback ourselves.
699 return (int) thread.readback().copyGraphicBufferInto(buffer, bitmap);
700 } else {
701 SETUP_TASK(copyGraphicBufferInto);
702 args->thread = &thread;
703 args->bitmap = bitmap;
704 args->buffer = buffer;
705 return static_cast<int>(reinterpret_cast<intptr_t>(staticPostAndWait(task)));
706 }
sergeyv59eecb522016-11-17 17:54:57 -0800707}
708
John Reck9a814872017-05-22 15:04:21 -0700709CREATE_BRIDGE2(onBitmapDestroyed, RenderThread* thread, uint32_t pixelRefId) {
710 args->thread->renderState().onBitmapDestroyed(args->pixelRefId);
711 return nullptr;
712}
713
714void RenderProxy::onBitmapDestroyed(uint32_t pixelRefId) {
715 if (!RenderThread::hasInstance()) return;
716 SETUP_TASK(onBitmapDestroyed);
717 RenderThread& thread = RenderThread::getInstance();
718 args->thread = &thread;
719 args->pixelRefId = pixelRefId;
720 thread.queue(task);
721}
722
John Reck4f02bf42014-01-03 18:09:17 -0800723void RenderProxy::post(RenderTask* task) {
724 mRenderThread.queue(task);
725}
726
727void* RenderProxy::postAndWait(MethodInvokeRenderTask* task) {
728 void* retval;
729 task->setReturnPtr(&retval);
John Reckcba287b2015-11-10 12:52:44 -0800730 SignalingRenderTask syncTask(task, &mSyncMutex, &mSyncCondition);
731 AutoMutex _lock(mSyncMutex);
732 mRenderThread.queue(&syncTask);
Tom Cherry298a1462017-02-28 14:07:09 -0800733 while (!syncTask.hasRun()) {
734 mSyncCondition.wait(mSyncMutex);
735 }
John Reck4f02bf42014-01-03 18:09:17 -0800736 return retval;
737}
738
John Reck0e89e2b2014-10-31 14:49:06 -0700739void* RenderProxy::staticPostAndWait(MethodInvokeRenderTask* task) {
740 RenderThread& thread = RenderThread::getInstance();
Stan Iliev6983bc42017-02-02 14:11:53 -0500741 LOG_ALWAYS_FATAL_IF(gettid() == thread.getTid());
John Reck0e89e2b2014-10-31 14:49:06 -0700742 void* retval;
743 task->setReturnPtr(&retval);
Chris Craik0a24b142015-10-19 17:10:19 -0700744 thread.queueAndWait(task);
John Reck0e89e2b2014-10-31 14:49:06 -0700745 return retval;
746}
747
John Reck4f02bf42014-01-03 18:09:17 -0800748} /* namespace renderthread */
749} /* namespace uirenderer */
750} /* namespace android */