blob: 9048bd14b35c676560b58ac40a4395310d0ba5e3 [file] [log] [blame]
John Reck4f02bf42014-01-03 18:09:17 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
John Reck4f02bf42014-01-03 18:09:17 -080017#include "RenderProxy.h"
18
John Reckba6adf62015-02-19 14:36:50 -080019#include "DeferredLayerUpdater.h"
20#include "DisplayList.h"
John Recka8963062017-06-14 10:47:50 -070021#include "Properties.h"
John Reck10dd0582016-03-31 16:36:16 -070022#include "Readback.h"
John Reckba6adf62015-02-19 14:36:50 -080023#include "Rect.h"
Stan Iliev3310fb12017-03-23 16:56:51 -040024#include "pipeline/skia/VectorDrawableAtlas.h"
John Reckba6adf62015-02-19 14:36:50 -080025#include "renderthread/CanvasContext.h"
John Reck43871902016-08-01 14:39:24 -070026#include "renderthread/EglManager.h"
John Reckba6adf62015-02-19 14:36:50 -080027#include "renderthread/RenderTask.h"
28#include "renderthread/RenderThread.h"
John Reck9a814872017-05-22 15:04:21 -070029#include "renderstate/RenderState.h"
John Reckba6adf62015-02-19 14:36:50 -080030#include "utils/Macros.h"
John Reck43871902016-08-01 14:39:24 -070031#include "utils/TimeUtils.h"
John Reck4f02bf42014-01-03 18:09:17 -080032
sergeyv59eecb522016-11-17 17:54:57 -080033#include <ui/GraphicBuffer.h>
34
John Reck4f02bf42014-01-03 18:09:17 -080035namespace android {
36namespace uirenderer {
37namespace renderthread {
38
39#define ARGS(method) method ## Args
40
John Reck19b6bcf2014-02-14 20:03:38 -080041#define CREATE_BRIDGE0(name) CREATE_BRIDGE(name,,,,,,,,)
John Reck4f02bf42014-01-03 18:09:17 -080042#define CREATE_BRIDGE1(name, a1) CREATE_BRIDGE(name, a1,,,,,,,)
43#define CREATE_BRIDGE2(name, a1, a2) CREATE_BRIDGE(name, a1,a2,,,,,,)
44#define CREATE_BRIDGE3(name, a1, a2, a3) CREATE_BRIDGE(name, a1,a2,a3,,,,,)
45#define CREATE_BRIDGE4(name, a1, a2, a3, a4) CREATE_BRIDGE(name, a1,a2,a3,a4,,,,)
Chris Craik797b95b2014-05-20 18:10:25 -070046#define CREATE_BRIDGE5(name, a1, a2, a3, a4, a5) CREATE_BRIDGE(name, a1,a2,a3,a4,a5,,,)
Chris Craik058fc642014-07-23 18:19:28 -070047#define CREATE_BRIDGE6(name, a1, a2, a3, a4, a5, a6) CREATE_BRIDGE(name, a1,a2,a3,a4,a5,a6,,)
48#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 -080049#define CREATE_BRIDGE(name, a1, a2, a3, a4, a5, a6, a7, a8) \
50 typedef struct { \
51 a1; a2; a3; a4; a5; a6; a7; a8; \
52 } ARGS(name); \
John Reck43871902016-08-01 14:39:24 -070053 static_assert(std::is_trivially_destructible<ARGS(name)>::value, \
54 "Error, ARGS must be trivially destructible!"); \
John Reck4f02bf42014-01-03 18:09:17 -080055 static void* Bridge_ ## name(ARGS(name)* args)
56
57#define SETUP_TASK(method) \
58 LOG_ALWAYS_FATAL_IF( METHOD_INVOKE_PAYLOAD_SIZE < sizeof(ARGS(method)), \
Mark Salyzyn546f3532014-06-10 12:29:14 -070059 "METHOD_INVOKE_PAYLOAD_SIZE %zu is smaller than sizeof(" #method "Args) %zu", \
John Reck4f02bf42014-01-03 18:09:17 -080060 METHOD_INVOKE_PAYLOAD_SIZE, sizeof(ARGS(method))); \
John Recke2c45522014-04-07 17:31:44 -070061 MethodInvokeRenderTask* task = new MethodInvokeRenderTask((RunnableMethod) Bridge_ ## method); \
John Reck4f02bf42014-01-03 18:09:17 -080062 ARGS(method) *args = (ARGS(method) *) task->payload()
63
John Reck119907c2014-08-14 09:02:01 -070064CREATE_BRIDGE4(createContext, RenderThread* thread, bool translucent,
65 RenderNode* rootRenderNode, IContextFactory* contextFactory) {
Stan Iliev03de0742016-07-07 12:35:54 -040066 return CanvasContext::create(*args->thread, args->translucent,
John Reck119907c2014-08-14 09:02:01 -070067 args->rootRenderNode, args->contextFactory);
John Reck4f02bf42014-01-03 18:09:17 -080068}
69
John Reck119907c2014-08-14 09:02:01 -070070RenderProxy::RenderProxy(bool translucent, RenderNode* rootRenderNode, IContextFactory* contextFactory)
John Reck4f02bf42014-01-03 18:09:17 -080071 : mRenderThread(RenderThread::getInstance())
Chris Craikd41c4d82015-01-05 15:51:13 -080072 , mContext(nullptr) {
John Reck4f02bf42014-01-03 18:09:17 -080073 SETUP_TASK(createContext);
74 args->translucent = translucent;
John Recke45b1fd2014-04-15 09:50:16 -070075 args->rootRenderNode = rootRenderNode;
John Reck3b202512014-06-23 13:13:08 -070076 args->thread = &mRenderThread;
John Reck119907c2014-08-14 09:02:01 -070077 args->contextFactory = contextFactory;
John Reck4f02bf42014-01-03 18:09:17 -080078 mContext = (CanvasContext*) postAndWait(task);
Skuhneea7a7fb2015-08-28 07:10:31 -070079 mDrawFrameTask.setContext(&mRenderThread, mContext, rootRenderNode);
John Reck4f02bf42014-01-03 18:09:17 -080080}
81
82RenderProxy::~RenderProxy() {
83 destroyContext();
84}
85
86CREATE_BRIDGE1(destroyContext, CanvasContext* context) {
87 delete args->context;
Chris Craikd41c4d82015-01-05 15:51:13 -080088 return nullptr;
John Reck4f02bf42014-01-03 18:09:17 -080089}
90
91void RenderProxy::destroyContext() {
92 if (mContext) {
93 SETUP_TASK(destroyContext);
94 args->context = mContext;
Chris Craikd41c4d82015-01-05 15:51:13 -080095 mContext = nullptr;
Skuhneea7a7fb2015-08-28 07:10:31 -070096 mDrawFrameTask.setContext(nullptr, nullptr, nullptr);
John Reck668f0e32014-03-26 15:10:40 -070097 // This is also a fence as we need to be certain that there are no
98 // outstanding mDrawFrame tasks posted before it is destroyed
99 postAndWait(task);
John Reck4f02bf42014-01-03 18:09:17 -0800100 }
101}
102
John Reck1125d1f2014-10-23 11:02:19 -0700103CREATE_BRIDGE2(setSwapBehavior, CanvasContext* context, SwapBehavior swapBehavior) {
104 args->context->setSwapBehavior(args->swapBehavior);
Chris Craikd41c4d82015-01-05 15:51:13 -0800105 return nullptr;
John Reck1125d1f2014-10-23 11:02:19 -0700106}
107
108void RenderProxy::setSwapBehavior(SwapBehavior swapBehavior) {
109 SETUP_TASK(setSwapBehavior);
110 args->context = mContext;
111 args->swapBehavior = swapBehavior;
112 post(task);
113}
114
John Reckfe5e7b72014-05-23 17:42:28 -0700115CREATE_BRIDGE1(loadSystemProperties, CanvasContext* context) {
John Recke4280ba2014-05-05 16:39:37 -0700116 bool needsRedraw = false;
117 if (Caches::hasInstance()) {
Chris Craik2507c342015-05-04 14:36:49 -0700118 needsRedraw = Properties::load();
John Recke4280ba2014-05-05 16:39:37 -0700119 }
Chris Craik2507c342015-05-04 14:36:49 -0700120 if (args->context->profiler().consumeProperties()) {
John Reckfe5e7b72014-05-23 17:42:28 -0700121 needsRedraw = true;
122 }
John Recke4280ba2014-05-05 16:39:37 -0700123 return (void*) needsRedraw;
124}
125
126bool RenderProxy::loadSystemProperties() {
127 SETUP_TASK(loadSystemProperties);
John Reckfe5e7b72014-05-23 17:42:28 -0700128 args->context = mContext;
John Recke4280ba2014-05-05 16:39:37 -0700129 return (bool) postAndWait(task);
130}
131
John Reckb36016c2015-03-11 08:50:53 -0700132CREATE_BRIDGE2(setName, CanvasContext* context, const char* name) {
133 args->context->setName(std::string(args->name));
134 return nullptr;
135}
136
137void RenderProxy::setName(const char* name) {
138 SETUP_TASK(setName);
139 args->context = mContext;
140 args->name = name;
Chris Craik2507c342015-05-04 14:36:49 -0700141 postAndWait(task); // block since name/value pointers owned by caller
John Reckb36016c2015-03-11 08:50:53 -0700142}
143
John Reckf6481082016-02-02 15:18:23 -0800144CREATE_BRIDGE2(initialize, CanvasContext* context, Surface* surface) {
145 args->context->initialize(args->surface);
Thomas Buhot0bcd0cb2015-12-04 12:18:03 +0100146 return nullptr;
John Reck4f02bf42014-01-03 18:09:17 -0800147}
148
John Reckf6481082016-02-02 15:18:23 -0800149void RenderProxy::initialize(const sp<Surface>& surface) {
John Reck4f02bf42014-01-03 18:09:17 -0800150 SETUP_TASK(initialize);
151 args->context = mContext;
John Reckf6481082016-02-02 15:18:23 -0800152 args->surface = surface.get();
Thomas Buhot0bcd0cb2015-12-04 12:18:03 +0100153 post(task);
John Reck4f02bf42014-01-03 18:09:17 -0800154}
155
John Reckf6481082016-02-02 15:18:23 -0800156CREATE_BRIDGE2(updateSurface, CanvasContext* context, Surface* surface) {
157 args->context->updateSurface(args->surface);
Chris Craikd41c4d82015-01-05 15:51:13 -0800158 return nullptr;
John Reck4f02bf42014-01-03 18:09:17 -0800159}
160
John Reckf6481082016-02-02 15:18:23 -0800161void RenderProxy::updateSurface(const sp<Surface>& surface) {
John Reck4f02bf42014-01-03 18:09:17 -0800162 SETUP_TASK(updateSurface);
163 args->context = mContext;
John Reckf6481082016-02-02 15:18:23 -0800164 args->surface = surface.get();
John Reckcd682122016-08-09 12:09:03 -0700165 post(task);
John Reckf7d9c1d2014-04-09 10:01:03 -0700166}
167
John Reckf6481082016-02-02 15:18:23 -0800168CREATE_BRIDGE2(pauseSurface, CanvasContext* context, Surface* surface) {
169 return (void*) args->context->pauseSurface(args->surface);
John Reckf7d9c1d2014-04-09 10:01:03 -0700170}
171
John Reckf6481082016-02-02 15:18:23 -0800172bool RenderProxy::pauseSurface(const sp<Surface>& surface) {
John Reckf7d9c1d2014-04-09 10:01:03 -0700173 SETUP_TASK(pauseSurface);
174 args->context = mContext;
John Reckf6481082016-02-02 15:18:23 -0800175 args->surface = surface.get();
John Reck01a5ea32014-12-03 13:01:07 -0800176 return (bool) postAndWait(task);
John Reck4f02bf42014-01-03 18:09:17 -0800177}
178
John Reck8afcc762016-04-13 10:24:06 -0700179CREATE_BRIDGE2(setStopped, CanvasContext* context, bool stopped) {
180 args->context->setStopped(args->stopped);
181 return nullptr;
182}
183
184void RenderProxy::setStopped(bool stopped) {
185 SETUP_TASK(setStopped);
186 args->context = mContext;
187 args->stopped = stopped;
188 postAndWait(task);
189}
190
John Reckab1080c2016-06-21 16:24:20 -0700191CREATE_BRIDGE4(setup, CanvasContext* context,
Alan Viverette50210d92015-05-14 18:05:36 -0700192 float lightRadius, uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
John Reckab1080c2016-06-21 16:24:20 -0700193 args->context->setup(args->lightRadius,
Chris Craik058fc642014-07-23 18:19:28 -0700194 args->ambientShadowAlpha, args->spotShadowAlpha);
Chris Craikd41c4d82015-01-05 15:51:13 -0800195 return nullptr;
John Reck4f02bf42014-01-03 18:09:17 -0800196}
197
John Reckab1080c2016-06-21 16:24:20 -0700198void RenderProxy::setup(float lightRadius,
John Reckb36016c2015-03-11 08:50:53 -0700199 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
John Reck4f02bf42014-01-03 18:09:17 -0800200 SETUP_TASK(setup);
201 args->context = mContext;
Chris Craik797b95b2014-05-20 18:10:25 -0700202 args->lightRadius = lightRadius;
Chris Craik058fc642014-07-23 18:19:28 -0700203 args->ambientShadowAlpha = ambientShadowAlpha;
204 args->spotShadowAlpha = spotShadowAlpha;
John Reck4f02bf42014-01-03 18:09:17 -0800205 post(task);
206}
207
Alan Viverette50210d92015-05-14 18:05:36 -0700208CREATE_BRIDGE2(setLightCenter, CanvasContext* context, Vector3 lightCenter) {
209 args->context->setLightCenter(args->lightCenter);
210 return nullptr;
211}
212
213void RenderProxy::setLightCenter(const Vector3& lightCenter) {
214 SETUP_TASK(setLightCenter);
215 args->context = mContext;
216 args->lightCenter = lightCenter;
217 post(task);
218}
219
John Reck63a06672014-05-07 13:45:54 -0700220CREATE_BRIDGE2(setOpaque, CanvasContext* context, bool opaque) {
221 args->context->setOpaque(args->opaque);
Chris Craikd41c4d82015-01-05 15:51:13 -0800222 return nullptr;
John Reck63a06672014-05-07 13:45:54 -0700223}
224
225void RenderProxy::setOpaque(bool opaque) {
226 SETUP_TASK(setOpaque);
227 args->context = mContext;
228 args->opaque = opaque;
229 post(task);
230}
231
Romain Guy26a2b972017-04-17 09:39:51 -0700232CREATE_BRIDGE2(setWideGamut, CanvasContext* context, bool wideGamut) {
233 args->context->setWideGamut(args->wideGamut);
234 return nullptr;
235}
236
237void RenderProxy::setWideGamut(bool wideGamut) {
238 SETUP_TASK(setWideGamut);
239 args->context = mContext;
240 args->wideGamut = wideGamut;
241 post(task);
242}
243
John Reckba6adf62015-02-19 14:36:50 -0800244int64_t* RenderProxy::frameInfo() {
245 return mDrawFrameTask.frameInfo();
246}
247
John Reck2de950d2017-01-25 10:58:30 -0800248int RenderProxy::syncAndDrawFrame() {
249 return mDrawFrameTask.drawFrame();
John Reck4f02bf42014-01-03 18:09:17 -0800250}
251
John Reck2de950d2017-01-25 10:58:30 -0800252CREATE_BRIDGE1(destroy, CanvasContext* context) {
253 args->context->destroy();
Chris Craikd41c4d82015-01-05 15:51:13 -0800254 return nullptr;
John Reck4f02bf42014-01-03 18:09:17 -0800255}
256
John Reck2de950d2017-01-25 10:58:30 -0800257void RenderProxy::destroy() {
John Reck17035b02014-09-03 07:39:53 -0700258 SETUP_TASK(destroy);
John Reck4f02bf42014-01-03 18:09:17 -0800259 args->context = mContext;
John Reckfae904d2014-04-14 11:01:57 -0700260 // destroyCanvasAndSurface() needs a fence as when it returns the
261 // underlying BufferQueue is going to be released from under
262 // the render thread.
263 postAndWait(task);
John Reck4f02bf42014-01-03 18:09:17 -0800264}
265
John Reck3b202512014-06-23 13:13:08 -0700266CREATE_BRIDGE2(invokeFunctor, RenderThread* thread, Functor* functor) {
267 CanvasContext::invokeFunctor(*args->thread, args->functor);
Chris Craikd41c4d82015-01-05 15:51:13 -0800268 return nullptr;
John Reck0d1f6342014-03-28 20:30:27 -0700269}
270
271void RenderProxy::invokeFunctor(Functor* functor, bool waitForCompletion) {
John Reckd3d8daf2014-04-10 15:00:13 -0700272 ATRACE_CALL();
John Reck3b202512014-06-23 13:13:08 -0700273 RenderThread& thread = RenderThread::getInstance();
John Reck0d1f6342014-03-28 20:30:27 -0700274 SETUP_TASK(invokeFunctor);
John Reck3b202512014-06-23 13:13:08 -0700275 args->thread = &thread;
John Reck0d1f6342014-03-28 20:30:27 -0700276 args->functor = functor;
277 if (waitForCompletion) {
John Reck3b202512014-06-23 13:13:08 -0700278 // waitForCompletion = true is expected to be fairly rare and only
279 // happen in destruction. Thus it should be fine to temporarily
280 // create a Mutex
John Reck0e89e2b2014-10-31 14:49:06 -0700281 staticPostAndWait(task);
John Reck0d1f6342014-03-28 20:30:27 -0700282 } else {
John Reck3b202512014-06-23 13:13:08 -0700283 thread.queue(task);
John Reck0d1f6342014-03-28 20:30:27 -0700284 }
285}
286
John Reckc36df952015-07-29 10:09:36 -0700287CREATE_BRIDGE1(createTextureLayer, CanvasContext* context) {
Derek Sollenberger56ad6ec2016-07-22 12:13:32 -0400288 return args->context->createTextureLayer();
John Reck19b6bcf2014-02-14 20:03:38 -0800289}
290
291DeferredLayerUpdater* RenderProxy::createTextureLayer() {
292 SETUP_TASK(createTextureLayer);
John Reck1949e792014-04-08 15:18:56 -0700293 args->context = mContext;
John Reck19b6bcf2014-02-14 20:03:38 -0800294 void* retval = postAndWait(task);
295 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval);
John Reck19b6bcf2014-02-14 20:03:38 -0800296 return layer;
297}
298
John Reck2de950d2017-01-25 10:58:30 -0800299CREATE_BRIDGE2(buildLayer, CanvasContext* context, RenderNode* node) {
300 args->context->buildLayer(args->node);
Chris Craikd41c4d82015-01-05 15:51:13 -0800301 return nullptr;
John Reck3e824952014-08-20 10:08:39 -0700302}
303
John Reck2de950d2017-01-25 10:58:30 -0800304void RenderProxy::buildLayer(RenderNode* node) {
John Reck3e824952014-08-20 10:08:39 -0700305 SETUP_TASK(buildLayer);
306 args->context = mContext;
307 args->node = node;
308 postAndWait(task);
309}
310
John Reck19b6bcf2014-02-14 20:03:38 -0800311CREATE_BRIDGE3(copyLayerInto, CanvasContext* context, DeferredLayerUpdater* layer,
312 SkBitmap* bitmap) {
313 bool success = args->context->copyLayerInto(args->layer, args->bitmap);
314 return (void*) success;
315}
316
John Reck3731dc22015-04-13 15:20:29 -0700317bool RenderProxy::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap& bitmap) {
John Reck19b6bcf2014-02-14 20:03:38 -0800318 SETUP_TASK(copyLayerInto);
319 args->context = mContext;
320 args->layer = layer;
John Reck3731dc22015-04-13 15:20:29 -0700321 args->bitmap = &bitmap;
John Reck19b6bcf2014-02-14 20:03:38 -0800322 return (bool) postAndWait(task);
323}
324
John Reckd72e0a32014-05-29 18:56:11 -0700325void RenderProxy::pushLayerUpdate(DeferredLayerUpdater* layer) {
326 mDrawFrameTask.pushLayerUpdate(layer);
327}
328
329void RenderProxy::cancelLayerUpdate(DeferredLayerUpdater* layer) {
330 mDrawFrameTask.removeLayerUpdate(layer);
John Reck19b6bcf2014-02-14 20:03:38 -0800331}
332
John Reck918ad522014-06-27 14:45:25 -0700333CREATE_BRIDGE1(detachSurfaceTexture, DeferredLayerUpdater* layer) {
334 args->layer->detachSurfaceTexture();
Chris Craikd41c4d82015-01-05 15:51:13 -0800335 return nullptr;
John Reck918ad522014-06-27 14:45:25 -0700336}
337
338void RenderProxy::detachSurfaceTexture(DeferredLayerUpdater* layer) {
339 SETUP_TASK(detachSurfaceTexture);
340 args->layer = layer;
341 postAndWait(task);
342}
343
John Reck2de950d2017-01-25 10:58:30 -0800344CREATE_BRIDGE1(destroyHardwareResources, CanvasContext* context) {
345 args->context->destroyHardwareResources();
Chris Craikd41c4d82015-01-05 15:51:13 -0800346 return nullptr;
John Recke1628b72014-05-23 15:11:19 -0700347}
348
John Reck2de950d2017-01-25 10:58:30 -0800349void RenderProxy::destroyHardwareResources() {
John Reckf47a5942014-06-30 16:20:04 -0700350 SETUP_TASK(destroyHardwareResources);
John Recke1628b72014-05-23 15:11:19 -0700351 args->context = mContext;
John Reck51f2d602016-04-06 07:50:47 -0700352 postAndWait(task);
John Recke1628b72014-05-23 15:11:19 -0700353}
354
Chris Craik2507c342015-05-04 14:36:49 -0700355CREATE_BRIDGE2(trimMemory, RenderThread* thread, int level) {
John Reckf47a5942014-06-30 16:20:04 -0700356 CanvasContext::trimMemory(*args->thread, args->level);
Chris Craikd41c4d82015-01-05 15:51:13 -0800357 return nullptr;
John Reckf47a5942014-06-30 16:20:04 -0700358}
359
360void RenderProxy::trimMemory(int level) {
John Reckcd3a22c2014-08-06 13:33:59 -0700361 // Avoid creating a RenderThread to do a trimMemory.
362 if (RenderThread::hasInstance()) {
363 RenderThread& thread = RenderThread::getInstance();
Chris Craik2507c342015-05-04 14:36:49 -0700364 SETUP_TASK(trimMemory);
John Reckcd3a22c2014-08-06 13:33:59 -0700365 args->thread = &thread;
366 args->level = level;
367 thread.queue(task);
368 }
John Reckf47a5942014-06-30 16:20:04 -0700369}
370
Chris Craik2507c342015-05-04 14:36:49 -0700371CREATE_BRIDGE2(overrideProperty, const char* name, const char* value) {
372 Properties::overrideProperty(args->name, args->value);
373 return nullptr;
374}
375
376void RenderProxy::overrideProperty(const char* name, const char* value) {
Chris Craik2507c342015-05-04 14:36:49 -0700377 SETUP_TASK(overrideProperty);
378 args->name = name;
379 args->value = value;
380 staticPostAndWait(task); // expensive, but block here since name/value pointers owned by caller
381}
382
John Reck28ad7b52014-04-07 16:59:25 -0700383CREATE_BRIDGE0(fence) {
384 // Intentionally empty
Chris Craikd41c4d82015-01-05 15:51:13 -0800385 return nullptr;
John Reck28ad7b52014-04-07 16:59:25 -0700386}
387
Andreas Gampe64bb4132014-11-22 00:35:09 +0000388template <typename T>
389void UNUSED(T t) {}
390
John Reck28ad7b52014-04-07 16:59:25 -0700391void RenderProxy::fence() {
392 SETUP_TASK(fence);
Andreas Gampe1e196742014-11-10 15:23:43 -0800393 UNUSED(args);
John Reck28ad7b52014-04-07 16:59:25 -0700394 postAndWait(task);
395}
396
Thomas Buhotc0a0e1a2016-01-18 10:31:58 +0100397void RenderProxy::staticFence() {
398 SETUP_TASK(fence);
399 UNUSED(args);
400 staticPostAndWait(task);
401}
402
John Reckf47a5942014-06-30 16:20:04 -0700403CREATE_BRIDGE1(stopDrawing, CanvasContext* context) {
404 args->context->stopDrawing();
Chris Craikd41c4d82015-01-05 15:51:13 -0800405 return nullptr;
John Reckf47a5942014-06-30 16:20:04 -0700406}
407
408void RenderProxy::stopDrawing() {
409 SETUP_TASK(stopDrawing);
410 args->context = mContext;
411 postAndWait(task);
412}
413
John Recka5dda642014-05-22 15:43:54 -0700414CREATE_BRIDGE1(notifyFramePending, CanvasContext* context) {
415 args->context->notifyFramePending();
Chris Craikd41c4d82015-01-05 15:51:13 -0800416 return nullptr;
John Recka5dda642014-05-22 15:43:54 -0700417}
418
419void RenderProxy::notifyFramePending() {
420 SETUP_TASK(notifyFramePending);
421 args->context = mContext;
422 mRenderThread.queueAtFront(task);
423}
424
John Reck7f2e5e32015-05-05 11:00:53 -0700425CREATE_BRIDGE4(dumpProfileInfo, CanvasContext* context, RenderThread* thread,
426 int fd, int dumpFlags) {
John Reckfe5e7b72014-05-23 17:42:28 -0700427 args->context->profiler().dumpData(args->fd);
Chris Craik53e51e42015-06-01 10:35:35 -0700428 if (args->dumpFlags & DumpFlags::FrameStats) {
John Reckba6adf62015-02-19 14:36:50 -0800429 args->context->dumpFrames(args->fd);
430 }
John Reck34781b22017-07-05 16:39:36 -0700431 if (args->dumpFlags & DumpFlags::JankStats) {
432 args->thread->globalProfileData()->dump(args->fd);
433 }
Chris Craik53e51e42015-06-01 10:35:35 -0700434 if (args->dumpFlags & DumpFlags::Reset) {
John Reckba6adf62015-02-19 14:36:50 -0800435 args->context->resetFrameStats();
436 }
Chris Craikd41c4d82015-01-05 15:51:13 -0800437 return nullptr;
John Reckfe5e7b72014-05-23 17:42:28 -0700438}
439
John Reckba6adf62015-02-19 14:36:50 -0800440void RenderProxy::dumpProfileInfo(int fd, int dumpFlags) {
John Reckfe5e7b72014-05-23 17:42:28 -0700441 SETUP_TASK(dumpProfileInfo);
442 args->context = mContext;
John Reck7f2e5e32015-05-05 11:00:53 -0700443 args->thread = &mRenderThread;
John Reckfe5e7b72014-05-23 17:42:28 -0700444 args->fd = fd;
John Reckba6adf62015-02-19 14:36:50 -0800445 args->dumpFlags = dumpFlags;
John Reckfe5e7b72014-05-23 17:42:28 -0700446 postAndWait(task);
447}
448
John Reck7f2e5e32015-05-05 11:00:53 -0700449CREATE_BRIDGE1(resetProfileInfo, CanvasContext* context) {
450 args->context->resetFrameStats();
451 return nullptr;
452}
453
454void RenderProxy::resetProfileInfo() {
455 SETUP_TASK(resetProfileInfo);
456 args->context = mContext;
457 postAndWait(task);
458}
459
John Reckf1480762016-07-03 18:28:25 -0700460CREATE_BRIDGE2(frameTimePercentile, RenderThread* thread, int percentile) {
461 return reinterpret_cast<void*>(static_cast<uintptr_t>(
John Reck34781b22017-07-05 16:39:36 -0700462 args->thread->globalProfileData()->findPercentile(args->percentile)));
John Reckf1480762016-07-03 18:28:25 -0700463}
464
465uint32_t RenderProxy::frameTimePercentile(int p) {
466 SETUP_TASK(frameTimePercentile);
467 args->thread = &mRenderThread;
468 args->percentile = p;
469 return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
470 postAndWait(task)));
471}
472
John Reckba6adf62015-02-19 14:36:50 -0800473CREATE_BRIDGE2(dumpGraphicsMemory, int fd, RenderThread* thread) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400474 args->thread->dumpGraphicsMemory(args->fd);
Chris Craikd41c4d82015-01-05 15:51:13 -0800475 return nullptr;
John Reck0e89e2b2014-10-31 14:49:06 -0700476}
477
Chris Craik2ae07332015-01-21 14:22:39 -0800478void RenderProxy::dumpGraphicsMemory(int fd) {
youngmin0822.leec80c9ad2015-03-20 21:22:32 +0900479 if (!RenderThread::hasInstance()) return;
Chris Craik2ae07332015-01-21 14:22:39 -0800480 SETUP_TASK(dumpGraphicsMemory);
John Reck0e89e2b2014-10-31 14:49:06 -0700481 args->fd = fd;
John Reckba6adf62015-02-19 14:36:50 -0800482 args->thread = &RenderThread::getInstance();
John Reck0e89e2b2014-10-31 14:49:06 -0700483 staticPostAndWait(task);
484}
485
John Reckedc524c2015-03-18 15:24:33 -0700486CREATE_BRIDGE2(setProcessStatsBuffer, RenderThread* thread, int fd) {
John Reck34781b22017-07-05 16:39:36 -0700487 args->thread->globalProfileData().switchStorageToAshmem(args->fd);
John Reckedc524c2015-03-18 15:24:33 -0700488 close(args->fd);
489 return nullptr;
490}
491
492void RenderProxy::setProcessStatsBuffer(int fd) {
493 SETUP_TASK(setProcessStatsBuffer);
John Reckdf1742e2017-01-19 15:56:21 -0800494 auto& rt = RenderThread::getInstance();
495 args->thread = &rt;
John Reckedc524c2015-03-18 15:24:33 -0700496 args->fd = dup(fd);
John Reckdf1742e2017-01-19 15:56:21 -0800497 rt.queue(task);
498}
499
500CREATE_BRIDGE1(rotateProcessStatsBuffer, RenderThread* thread) {
John Reck34781b22017-07-05 16:39:36 -0700501 args->thread->globalProfileData().rotateStorage();
John Reckdf1742e2017-01-19 15:56:21 -0800502 return nullptr;
503}
504
505void RenderProxy::rotateProcessStatsBuffer() {
506 SETUP_TASK(rotateProcessStatsBuffer);
507 auto& rt = RenderThread::getInstance();
508 args->thread = &rt;
509 rt.queue(task);
John Reckedc524c2015-03-18 15:24:33 -0700510}
511
Tim Murray33eb07f2016-06-10 10:03:20 -0700512int RenderProxy::getRenderThreadTid() {
513 return mRenderThread.getTid();
514}
515
Skuhneea7a7fb2015-08-28 07:10:31 -0700516CREATE_BRIDGE3(addRenderNode, CanvasContext* context, RenderNode* node, bool placeFront) {
517 args->context->addRenderNode(args->node, args->placeFront);
518 return nullptr;
519}
520
521void RenderProxy::addRenderNode(RenderNode* node, bool placeFront) {
522 SETUP_TASK(addRenderNode);
523 args->context = mContext;
524 args->node = node;
525 args->placeFront = placeFront;
526 post(task);
527}
528
529CREATE_BRIDGE2(removeRenderNode, CanvasContext* context, RenderNode* node) {
530 args->context->removeRenderNode(args->node);
531 return nullptr;
532}
533
534void RenderProxy::removeRenderNode(RenderNode* node) {
535 SETUP_TASK(removeRenderNode);
536 args->context = mContext;
537 args->node = node;
538 post(task);
539}
540
541CREATE_BRIDGE2(drawRenderNode, CanvasContext* context, RenderNode* node) {
542 args->context->prepareAndDraw(args->node);
543 return nullptr;
544}
545
546void RenderProxy::drawRenderNode(RenderNode* node) {
547 SETUP_TASK(drawRenderNode);
548 args->context = mContext;
549 args->node = node;
550 // Be pseudo-thread-safe and don't use any member variables
551 staticPostAndWait(task);
552}
553
Skuhneb8160872015-09-22 09:51:39 -0700554void RenderProxy::setContentDrawBounds(int left, int top, int right, int bottom) {
John Reckf138b172017-09-08 11:00:42 -0700555 mDrawFrameTask.setContentDrawBounds(left, top, right, bottom);
Skuhneea7a7fb2015-08-28 07:10:31 -0700556}
557
John Recke248bd12015-08-05 13:53:53 -0700558CREATE_BRIDGE1(serializeDisplayListTree, CanvasContext* context) {
559 args->context->serializeDisplayListTree();
560 return nullptr;
561}
562
563void RenderProxy::serializeDisplayListTree() {
564 SETUP_TASK(serializeDisplayListTree);
565 args->context = mContext;
566 post(task);
567}
568
Andres Morales910beb82016-02-02 16:19:40 -0800569CREATE_BRIDGE2(addFrameMetricsObserver, CanvasContext* context,
570 FrameMetricsObserver* frameStatsObserver) {
571 args->context->addFrameMetricsObserver(args->frameStatsObserver);
Andres Morales06f5bc72015-12-15 15:21:31 -0800572 if (args->frameStatsObserver != nullptr) {
573 args->frameStatsObserver->decStrong(args->context);
574 }
575 return nullptr;
576}
577
Andres Morales910beb82016-02-02 16:19:40 -0800578void RenderProxy::addFrameMetricsObserver(FrameMetricsObserver* observer) {
579 SETUP_TASK(addFrameMetricsObserver);
Andres Morales06f5bc72015-12-15 15:21:31 -0800580 args->context = mContext;
581 args->frameStatsObserver = observer;
582 if (observer != nullptr) {
583 observer->incStrong(mContext);
584 }
585 post(task);
586}
587
Andres Morales910beb82016-02-02 16:19:40 -0800588CREATE_BRIDGE2(removeFrameMetricsObserver, CanvasContext* context,
589 FrameMetricsObserver* frameStatsObserver) {
590 args->context->removeFrameMetricsObserver(args->frameStatsObserver);
Andres Morales06f5bc72015-12-15 15:21:31 -0800591 if (args->frameStatsObserver != nullptr) {
592 args->frameStatsObserver->decStrong(args->context);
593 }
594 return nullptr;
595}
596
Andres Morales910beb82016-02-02 16:19:40 -0800597void RenderProxy::removeFrameMetricsObserver(FrameMetricsObserver* observer) {
598 SETUP_TASK(removeFrameMetricsObserver);
Andres Morales06f5bc72015-12-15 15:21:31 -0800599 args->context = mContext;
600 args->frameStatsObserver = observer;
601 if (observer != nullptr) {
602 observer->incStrong(mContext);
603 }
604 post(task);
605}
606
John Reck95801462016-09-01 09:44:09 -0700607CREATE_BRIDGE4(copySurfaceInto, RenderThread* thread,
608 Surface* surface, Rect srcRect, SkBitmap* bitmap) {
Derek Sollenbergerc4fbada2016-11-07 16:05:41 -0500609 return (void*)args->thread->readback().copySurfaceInto(*args->surface,
610 args->srcRect, args->bitmap);
John Reck10dd0582016-03-31 16:36:16 -0700611}
612
John Reck95801462016-09-01 09:44:09 -0700613int RenderProxy::copySurfaceInto(sp<Surface>& surface, int left, int top,
614 int right, int bottom, SkBitmap* bitmap) {
John Reck10dd0582016-03-31 16:36:16 -0700615 SETUP_TASK(copySurfaceInto);
616 args->bitmap = bitmap;
617 args->surface = surface.get();
618 args->thread = &RenderThread::getInstance();
John Reck95801462016-09-01 09:44:09 -0700619 args->srcRect.set(left, top, right, bottom);
John Recke94cbc72016-04-25 13:03:44 -0700620 return static_cast<int>(
621 reinterpret_cast<intptr_t>( staticPostAndWait(task) ));
John Reck10dd0582016-03-31 16:36:16 -0700622}
623
sergeyvec4a4b12016-10-20 18:39:04 -0700624CREATE_BRIDGE2(prepareToDraw, RenderThread* thread, Bitmap* bitmap) {
Derek Sollenbergerdaf72292016-10-25 12:09:18 -0400625 CanvasContext::prepareToDraw(*args->thread, args->bitmap);
sergeyvec4a4b12016-10-20 18:39:04 -0700626 args->bitmap->unref();
John Reck43871902016-08-01 14:39:24 -0700627 args->bitmap = nullptr;
628 return nullptr;
629}
630
sergeyvec4a4b12016-10-20 18:39:04 -0700631void RenderProxy::prepareToDraw(Bitmap& bitmap) {
John Reck43871902016-08-01 14:39:24 -0700632 // If we haven't spun up a hardware accelerated window yet, there's no
633 // point in precaching these bitmaps as it can't impact jank.
634 // We also don't know if we even will spin up a hardware-accelerated
635 // window or not.
636 if (!RenderThread::hasInstance()) return;
637 RenderThread* renderThread = &RenderThread::getInstance();
638 SETUP_TASK(prepareToDraw);
639 args->thread = renderThread;
sergeyvec4a4b12016-10-20 18:39:04 -0700640 bitmap.ref();
641 args->bitmap = &bitmap;
John Reck43871902016-08-01 14:39:24 -0700642 nsecs_t lastVsync = renderThread->timeLord().latestVsync();
643 nsecs_t estimatedNextVsync = lastVsync + renderThread->timeLord().frameIntervalNanos();
644 nsecs_t timeToNextVsync = estimatedNextVsync - systemTime(CLOCK_MONOTONIC);
645 // We expect the UI thread to take 4ms and for RT to be active from VSYNC+4ms to
646 // VSYNC+12ms or so, so aim for the gap during which RT is expected to
647 // be idle
648 // TODO: Make this concept a first-class supported thing? RT could use
649 // knowledge of pending draws to better schedule this task
650 if (timeToNextVsync > -6_ms && timeToNextVsync < 1_ms) {
651 renderThread->queueAt(task, estimatedNextVsync + 8_ms);
652 } else {
653 renderThread->queue(task);
654 }
655}
656
sergeyv694d4992016-10-27 10:23:13 -0700657CREATE_BRIDGE2(allocateHardwareBitmap, RenderThread* thread, SkBitmap* bitmap) {
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400658 sk_sp<Bitmap> hardwareBitmap = args->thread->allocateHardwareBitmap(*args->bitmap);
sergeyv694d4992016-10-27 10:23:13 -0700659 return hardwareBitmap.release();
660}
661
662sk_sp<Bitmap> RenderProxy::allocateHardwareBitmap(SkBitmap& bitmap) {
663 SETUP_TASK(allocateHardwareBitmap);
664 args->bitmap = &bitmap;
665 args->thread = &RenderThread::getInstance();
666 sk_sp<Bitmap> hardwareBitmap(reinterpret_cast<Bitmap*>(staticPostAndWait(task)));
667 return hardwareBitmap;
668}
669
sergeyv59eecb522016-11-17 17:54:57 -0800670CREATE_BRIDGE3(copyGraphicBufferInto, RenderThread* thread, GraphicBuffer* buffer, SkBitmap* bitmap) {
671 return (void*) args->thread->readback().copyGraphicBufferInto(args->buffer, args->bitmap);
672}
673
674int RenderProxy::copyGraphicBufferInto(GraphicBuffer* buffer, SkBitmap* bitmap) {
Stan Iliev6983bc42017-02-02 14:11:53 -0500675 RenderThread& thread = RenderThread::getInstance();
676 if (Properties::isSkiaEnabled() && gettid() == thread.getTid()) {
677 //TODO: fix everything that hits this. We should never be triggering a readback ourselves.
678 return (int) thread.readback().copyGraphicBufferInto(buffer, bitmap);
679 } else {
680 SETUP_TASK(copyGraphicBufferInto);
681 args->thread = &thread;
682 args->bitmap = bitmap;
683 args->buffer = buffer;
684 return static_cast<int>(reinterpret_cast<intptr_t>(staticPostAndWait(task)));
685 }
sergeyv59eecb522016-11-17 17:54:57 -0800686}
687
John Reck9a814872017-05-22 15:04:21 -0700688CREATE_BRIDGE2(onBitmapDestroyed, RenderThread* thread, uint32_t pixelRefId) {
689 args->thread->renderState().onBitmapDestroyed(args->pixelRefId);
690 return nullptr;
691}
692
693void RenderProxy::onBitmapDestroyed(uint32_t pixelRefId) {
694 if (!RenderThread::hasInstance()) return;
695 SETUP_TASK(onBitmapDestroyed);
696 RenderThread& thread = RenderThread::getInstance();
697 args->thread = &thread;
698 args->pixelRefId = pixelRefId;
699 thread.queue(task);
700}
701
John Recka8963062017-06-14 10:47:50 -0700702void RenderProxy::disableVsync() {
703 Properties::disableVsync = true;
704}
705
John Reck4f02bf42014-01-03 18:09:17 -0800706void RenderProxy::post(RenderTask* task) {
707 mRenderThread.queue(task);
708}
709
Stan Iliev3310fb12017-03-23 16:56:51 -0400710CREATE_BRIDGE1(repackVectorDrawableAtlas, RenderThread* thread) {
711 args->thread->cacheManager().acquireVectorDrawableAtlas()->repackIfNeeded(
712 args->thread->getGrContext());
713 return nullptr;
714}
715
716void RenderProxy::repackVectorDrawableAtlas() {
717 RenderThread& thread = RenderThread::getInstance();
718 SETUP_TASK(repackVectorDrawableAtlas);
719 args->thread = &thread;
720 thread.queue(task);
721}
722
John Reck4f02bf42014-01-03 18:09:17 -0800723void* RenderProxy::postAndWait(MethodInvokeRenderTask* task) {
724 void* retval;
725 task->setReturnPtr(&retval);
John Reckcba287b2015-11-10 12:52:44 -0800726 SignalingRenderTask syncTask(task, &mSyncMutex, &mSyncCondition);
727 AutoMutex _lock(mSyncMutex);
728 mRenderThread.queue(&syncTask);
Tom Cherry298a1462017-02-28 14:07:09 -0800729 while (!syncTask.hasRun()) {
730 mSyncCondition.wait(mSyncMutex);
731 }
John Reck4f02bf42014-01-03 18:09:17 -0800732 return retval;
733}
734
John Reck0e89e2b2014-10-31 14:49:06 -0700735void* RenderProxy::staticPostAndWait(MethodInvokeRenderTask* task) {
736 RenderThread& thread = RenderThread::getInstance();
Stan Iliev6983bc42017-02-02 14:11:53 -0500737 LOG_ALWAYS_FATAL_IF(gettid() == thread.getTid());
John Reck0e89e2b2014-10-31 14:49:06 -0700738 void* retval;
739 task->setReturnPtr(&retval);
Chris Craik0a24b142015-10-19 17:10:19 -0700740 thread.queueAndWait(task);
John Reck0e89e2b2014-10-31 14:49:06 -0700741 return retval;
742}
743
John Reck4f02bf42014-01-03 18:09:17 -0800744} /* namespace renderthread */
745} /* namespace uirenderer */
746} /* namespace android */