blob: fb1c8962328be754a1b6d1b233459bd10b817d0d [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"
21#include "LayerRenderer.h"
John Reck10dd0582016-03-31 16:36:16 -070022#include "Readback.h"
John Reckba6adf62015-02-19 14:36:50 -080023#include "Rect.h"
24#include "renderthread/CanvasContext.h"
John Reck43871902016-08-01 14:39:24 -070025#include "renderthread/EglManager.h"
John Reckba6adf62015-02-19 14:36:50 -080026#include "renderthread/RenderTask.h"
27#include "renderthread/RenderThread.h"
28#include "utils/Macros.h"
John Reck43871902016-08-01 14:39:24 -070029#include "utils/TimeUtils.h"
John Reck4f02bf42014-01-03 18:09:17 -080030
31namespace android {
32namespace uirenderer {
33namespace renderthread {
34
35#define ARGS(method) method ## Args
36
John Reck19b6bcf2014-02-14 20:03:38 -080037#define CREATE_BRIDGE0(name) CREATE_BRIDGE(name,,,,,,,,)
John Reck4f02bf42014-01-03 18:09:17 -080038#define CREATE_BRIDGE1(name, a1) CREATE_BRIDGE(name, a1,,,,,,,)
39#define CREATE_BRIDGE2(name, a1, a2) CREATE_BRIDGE(name, a1,a2,,,,,,)
40#define CREATE_BRIDGE3(name, a1, a2, a3) CREATE_BRIDGE(name, a1,a2,a3,,,,,)
41#define CREATE_BRIDGE4(name, a1, a2, a3, a4) CREATE_BRIDGE(name, a1,a2,a3,a4,,,,)
Chris Craik797b95b2014-05-20 18:10:25 -070042#define CREATE_BRIDGE5(name, a1, a2, a3, a4, a5) CREATE_BRIDGE(name, a1,a2,a3,a4,a5,,,)
Chris Craik058fc642014-07-23 18:19:28 -070043#define CREATE_BRIDGE6(name, a1, a2, a3, a4, a5, a6) CREATE_BRIDGE(name, a1,a2,a3,a4,a5,a6,,)
44#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 -080045#define CREATE_BRIDGE(name, a1, a2, a3, a4, a5, a6, a7, a8) \
46 typedef struct { \
47 a1; a2; a3; a4; a5; a6; a7; a8; \
48 } ARGS(name); \
John Reck43871902016-08-01 14:39:24 -070049 static_assert(std::is_trivially_destructible<ARGS(name)>::value, \
50 "Error, ARGS must be trivially destructible!"); \
John Reck4f02bf42014-01-03 18:09:17 -080051 static void* Bridge_ ## name(ARGS(name)* args)
52
53#define SETUP_TASK(method) \
54 LOG_ALWAYS_FATAL_IF( METHOD_INVOKE_PAYLOAD_SIZE < sizeof(ARGS(method)), \
Mark Salyzyn546f3532014-06-10 12:29:14 -070055 "METHOD_INVOKE_PAYLOAD_SIZE %zu is smaller than sizeof(" #method "Args) %zu", \
John Reck4f02bf42014-01-03 18:09:17 -080056 METHOD_INVOKE_PAYLOAD_SIZE, sizeof(ARGS(method))); \
John Recke2c45522014-04-07 17:31:44 -070057 MethodInvokeRenderTask* task = new MethodInvokeRenderTask((RunnableMethod) Bridge_ ## method); \
John Reck4f02bf42014-01-03 18:09:17 -080058 ARGS(method) *args = (ARGS(method) *) task->payload()
59
John Reck119907c2014-08-14 09:02:01 -070060CREATE_BRIDGE4(createContext, RenderThread* thread, bool translucent,
61 RenderNode* rootRenderNode, IContextFactory* contextFactory) {
62 return new CanvasContext(*args->thread, args->translucent,
63 args->rootRenderNode, args->contextFactory);
John Reck4f02bf42014-01-03 18:09:17 -080064}
65
John Reck119907c2014-08-14 09:02:01 -070066RenderProxy::RenderProxy(bool translucent, RenderNode* rootRenderNode, IContextFactory* contextFactory)
John Reck4f02bf42014-01-03 18:09:17 -080067 : mRenderThread(RenderThread::getInstance())
Chris Craikd41c4d82015-01-05 15:51:13 -080068 , mContext(nullptr) {
John Reck4f02bf42014-01-03 18:09:17 -080069 SETUP_TASK(createContext);
70 args->translucent = translucent;
John Recke45b1fd2014-04-15 09:50:16 -070071 args->rootRenderNode = rootRenderNode;
John Reck3b202512014-06-23 13:13:08 -070072 args->thread = &mRenderThread;
John Reck119907c2014-08-14 09:02:01 -070073 args->contextFactory = contextFactory;
John Reck4f02bf42014-01-03 18:09:17 -080074 mContext = (CanvasContext*) postAndWait(task);
Skuhneea7a7fb2015-08-28 07:10:31 -070075 mDrawFrameTask.setContext(&mRenderThread, mContext, rootRenderNode);
John Reck4f02bf42014-01-03 18:09:17 -080076}
77
78RenderProxy::~RenderProxy() {
79 destroyContext();
80}
81
82CREATE_BRIDGE1(destroyContext, CanvasContext* context) {
83 delete args->context;
Chris Craikd41c4d82015-01-05 15:51:13 -080084 return nullptr;
John Reck4f02bf42014-01-03 18:09:17 -080085}
86
87void RenderProxy::destroyContext() {
88 if (mContext) {
89 SETUP_TASK(destroyContext);
90 args->context = mContext;
Chris Craikd41c4d82015-01-05 15:51:13 -080091 mContext = nullptr;
Skuhneea7a7fb2015-08-28 07:10:31 -070092 mDrawFrameTask.setContext(nullptr, nullptr, nullptr);
John Reck668f0e32014-03-26 15:10:40 -070093 // This is also a fence as we need to be certain that there are no
94 // outstanding mDrawFrame tasks posted before it is destroyed
95 postAndWait(task);
John Reck4f02bf42014-01-03 18:09:17 -080096 }
97}
98
John Reck1125d1f2014-10-23 11:02:19 -070099CREATE_BRIDGE2(setSwapBehavior, CanvasContext* context, SwapBehavior swapBehavior) {
100 args->context->setSwapBehavior(args->swapBehavior);
Chris Craikd41c4d82015-01-05 15:51:13 -0800101 return nullptr;
John Reck1125d1f2014-10-23 11:02:19 -0700102}
103
104void RenderProxy::setSwapBehavior(SwapBehavior swapBehavior) {
105 SETUP_TASK(setSwapBehavior);
106 args->context = mContext;
107 args->swapBehavior = swapBehavior;
108 post(task);
109}
110
John Reckfe5e7b72014-05-23 17:42:28 -0700111CREATE_BRIDGE1(loadSystemProperties, CanvasContext* context) {
John Recke4280ba2014-05-05 16:39:37 -0700112 bool needsRedraw = false;
113 if (Caches::hasInstance()) {
Chris Craik2507c342015-05-04 14:36:49 -0700114 needsRedraw = Properties::load();
John Recke4280ba2014-05-05 16:39:37 -0700115 }
Chris Craik2507c342015-05-04 14:36:49 -0700116 if (args->context->profiler().consumeProperties()) {
John Reckfe5e7b72014-05-23 17:42:28 -0700117 needsRedraw = true;
118 }
John Recke4280ba2014-05-05 16:39:37 -0700119 return (void*) needsRedraw;
120}
121
122bool RenderProxy::loadSystemProperties() {
123 SETUP_TASK(loadSystemProperties);
John Reckfe5e7b72014-05-23 17:42:28 -0700124 args->context = mContext;
John Recke4280ba2014-05-05 16:39:37 -0700125 return (bool) postAndWait(task);
126}
127
John Reckb36016c2015-03-11 08:50:53 -0700128CREATE_BRIDGE2(setName, CanvasContext* context, const char* name) {
129 args->context->setName(std::string(args->name));
130 return nullptr;
131}
132
133void RenderProxy::setName(const char* name) {
134 SETUP_TASK(setName);
135 args->context = mContext;
136 args->name = name;
Chris Craik2507c342015-05-04 14:36:49 -0700137 postAndWait(task); // block since name/value pointers owned by caller
John Reckb36016c2015-03-11 08:50:53 -0700138}
139
John Reckf6481082016-02-02 15:18:23 -0800140CREATE_BRIDGE2(initialize, CanvasContext* context, Surface* surface) {
141 args->context->initialize(args->surface);
Thomas Buhot0bcd0cb2015-12-04 12:18:03 +0100142 return nullptr;
John Reck4f02bf42014-01-03 18:09:17 -0800143}
144
John Reckf6481082016-02-02 15:18:23 -0800145void RenderProxy::initialize(const sp<Surface>& surface) {
John Reck4f02bf42014-01-03 18:09:17 -0800146 SETUP_TASK(initialize);
147 args->context = mContext;
John Reckf6481082016-02-02 15:18:23 -0800148 args->surface = surface.get();
Thomas Buhot0bcd0cb2015-12-04 12:18:03 +0100149 post(task);
John Reck4f02bf42014-01-03 18:09:17 -0800150}
151
John Reckf6481082016-02-02 15:18:23 -0800152CREATE_BRIDGE2(updateSurface, CanvasContext* context, Surface* surface) {
153 args->context->updateSurface(args->surface);
Chris Craikd41c4d82015-01-05 15:51:13 -0800154 return nullptr;
John Reck4f02bf42014-01-03 18:09:17 -0800155}
156
John Reckf6481082016-02-02 15:18:23 -0800157void RenderProxy::updateSurface(const sp<Surface>& surface) {
John Reck4f02bf42014-01-03 18:09:17 -0800158 SETUP_TASK(updateSurface);
159 args->context = mContext;
John Reckf6481082016-02-02 15:18:23 -0800160 args->surface = surface.get();
John Reckf7d9c1d2014-04-09 10:01:03 -0700161 postAndWait(task);
162}
163
John Reckf6481082016-02-02 15:18:23 -0800164CREATE_BRIDGE2(pauseSurface, CanvasContext* context, Surface* surface) {
165 return (void*) args->context->pauseSurface(args->surface);
John Reckf7d9c1d2014-04-09 10:01:03 -0700166}
167
John Reckf6481082016-02-02 15:18:23 -0800168bool RenderProxy::pauseSurface(const sp<Surface>& surface) {
John Reckf7d9c1d2014-04-09 10:01:03 -0700169 SETUP_TASK(pauseSurface);
170 args->context = mContext;
John Reckf6481082016-02-02 15:18:23 -0800171 args->surface = surface.get();
John Reck01a5ea32014-12-03 13:01:07 -0800172 return (bool) postAndWait(task);
John Reck4f02bf42014-01-03 18:09:17 -0800173}
174
John Reck8afcc762016-04-13 10:24:06 -0700175CREATE_BRIDGE2(setStopped, CanvasContext* context, bool stopped) {
176 args->context->setStopped(args->stopped);
177 return nullptr;
178}
179
180void RenderProxy::setStopped(bool stopped) {
181 SETUP_TASK(setStopped);
182 args->context = mContext;
183 args->stopped = stopped;
184 postAndWait(task);
185}
186
Alan Viverette50210d92015-05-14 18:05:36 -0700187CREATE_BRIDGE6(setup, CanvasContext* context, int width, int height,
188 float lightRadius, uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
189 args->context->setup(args->width, args->height, args->lightRadius,
Chris Craik058fc642014-07-23 18:19:28 -0700190 args->ambientShadowAlpha, args->spotShadowAlpha);
Chris Craikd41c4d82015-01-05 15:51:13 -0800191 return nullptr;
John Reck4f02bf42014-01-03 18:09:17 -0800192}
193
Alan Viverette50210d92015-05-14 18:05:36 -0700194void RenderProxy::setup(int width, int height, float lightRadius,
John Reckb36016c2015-03-11 08:50:53 -0700195 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
John Reck4f02bf42014-01-03 18:09:17 -0800196 SETUP_TASK(setup);
197 args->context = mContext;
198 args->width = width;
199 args->height = height;
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
John Reckba6adf62015-02-19 14:36:50 -0800230int64_t* RenderProxy::frameInfo() {
231 return mDrawFrameTask.frameInfo();
232}
233
John Reck51f2d602016-04-06 07:50:47 -0700234int RenderProxy::syncAndDrawFrame(TreeObserver* observer) {
235 return mDrawFrameTask.drawFrame(observer);
John Reck4f02bf42014-01-03 18:09:17 -0800236}
237
John Reck51f2d602016-04-06 07:50:47 -0700238CREATE_BRIDGE2(destroy, CanvasContext* context, TreeObserver* observer) {
239 args->context->destroy(args->observer);
Chris Craikd41c4d82015-01-05 15:51:13 -0800240 return nullptr;
John Reck4f02bf42014-01-03 18:09:17 -0800241}
242
John Reck51f2d602016-04-06 07:50:47 -0700243void RenderProxy::destroy(TreeObserver* observer) {
John Reck17035b02014-09-03 07:39:53 -0700244 SETUP_TASK(destroy);
John Reck4f02bf42014-01-03 18:09:17 -0800245 args->context = mContext;
John Reck51f2d602016-04-06 07:50:47 -0700246 args->observer = observer;
John Reckfae904d2014-04-14 11:01:57 -0700247 // destroyCanvasAndSurface() needs a fence as when it returns the
248 // underlying BufferQueue is going to be released from under
249 // the render thread.
250 postAndWait(task);
John Reck4f02bf42014-01-03 18:09:17 -0800251}
252
John Reck3b202512014-06-23 13:13:08 -0700253CREATE_BRIDGE2(invokeFunctor, RenderThread* thread, Functor* functor) {
254 CanvasContext::invokeFunctor(*args->thread, args->functor);
Chris Craikd41c4d82015-01-05 15:51:13 -0800255 return nullptr;
John Reck0d1f6342014-03-28 20:30:27 -0700256}
257
258void RenderProxy::invokeFunctor(Functor* functor, bool waitForCompletion) {
John Reckd3d8daf2014-04-10 15:00:13 -0700259 ATRACE_CALL();
John Reck3b202512014-06-23 13:13:08 -0700260 RenderThread& thread = RenderThread::getInstance();
John Reck0d1f6342014-03-28 20:30:27 -0700261 SETUP_TASK(invokeFunctor);
John Reck3b202512014-06-23 13:13:08 -0700262 args->thread = &thread;
John Reck0d1f6342014-03-28 20:30:27 -0700263 args->functor = functor;
264 if (waitForCompletion) {
John Reck3b202512014-06-23 13:13:08 -0700265 // waitForCompletion = true is expected to be fairly rare and only
266 // happen in destruction. Thus it should be fine to temporarily
267 // create a Mutex
John Reck0e89e2b2014-10-31 14:49:06 -0700268 staticPostAndWait(task);
John Reck0d1f6342014-03-28 20:30:27 -0700269 } else {
John Reck3b202512014-06-23 13:13:08 -0700270 thread.queue(task);
John Reck0d1f6342014-03-28 20:30:27 -0700271 }
272}
273
John Reckfc53ef272014-02-11 10:40:25 -0800274CREATE_BRIDGE2(runWithGlContext, CanvasContext* context, RenderTask* task) {
275 args->context->runWithGlContext(args->task);
Chris Craikd41c4d82015-01-05 15:51:13 -0800276 return nullptr;
John Reckfc53ef272014-02-11 10:40:25 -0800277}
278
279void RenderProxy::runWithGlContext(RenderTask* gltask) {
280 SETUP_TASK(runWithGlContext);
281 args->context = mContext;
282 args->task = gltask;
283 postAndWait(task);
284}
285
John Reckc36df952015-07-29 10:09:36 -0700286CREATE_BRIDGE1(createTextureLayer, CanvasContext* context) {
John Reck1949e792014-04-08 15:18:56 -0700287 Layer* layer = args->context->createTextureLayer();
Chris Craikd41c4d82015-01-05 15:51:13 -0800288 if (!layer) return nullptr;
John Reckc36df952015-07-29 10:09:36 -0700289 return new DeferredLayerUpdater(layer);
John Reck19b6bcf2014-02-14 20:03:38 -0800290}
291
292DeferredLayerUpdater* RenderProxy::createTextureLayer() {
293 SETUP_TASK(createTextureLayer);
John Reck1949e792014-04-08 15:18:56 -0700294 args->context = mContext;
John Reck19b6bcf2014-02-14 20:03:38 -0800295 void* retval = postAndWait(task);
296 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval);
John Reck19b6bcf2014-02-14 20:03:38 -0800297 return layer;
298}
299
John Reck51f2d602016-04-06 07:50:47 -0700300CREATE_BRIDGE3(buildLayer, CanvasContext* context, RenderNode* node, TreeObserver* observer) {
301 args->context->buildLayer(args->node, args->observer);
Chris Craikd41c4d82015-01-05 15:51:13 -0800302 return nullptr;
John Reck3e824952014-08-20 10:08:39 -0700303}
304
John Reck51f2d602016-04-06 07:50:47 -0700305void RenderProxy::buildLayer(RenderNode* node, TreeObserver* observer) {
John Reck3e824952014-08-20 10:08:39 -0700306 SETUP_TASK(buildLayer);
307 args->context = mContext;
308 args->node = node;
John Reck51f2d602016-04-06 07:50:47 -0700309 args->observer = observer;
John Reck3e824952014-08-20 10:08:39 -0700310 postAndWait(task);
311}
312
John Reck19b6bcf2014-02-14 20:03:38 -0800313CREATE_BRIDGE3(copyLayerInto, CanvasContext* context, DeferredLayerUpdater* layer,
314 SkBitmap* bitmap) {
315 bool success = args->context->copyLayerInto(args->layer, args->bitmap);
316 return (void*) success;
317}
318
John Reck3731dc22015-04-13 15:20:29 -0700319bool RenderProxy::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap& bitmap) {
John Reck19b6bcf2014-02-14 20:03:38 -0800320 SETUP_TASK(copyLayerInto);
321 args->context = mContext;
322 args->layer = layer;
John Reck3731dc22015-04-13 15:20:29 -0700323 args->bitmap = &bitmap;
John Reck19b6bcf2014-02-14 20:03:38 -0800324 return (bool) postAndWait(task);
325}
326
John Reckd72e0a32014-05-29 18:56:11 -0700327void RenderProxy::pushLayerUpdate(DeferredLayerUpdater* layer) {
328 mDrawFrameTask.pushLayerUpdate(layer);
329}
330
331void RenderProxy::cancelLayerUpdate(DeferredLayerUpdater* layer) {
332 mDrawFrameTask.removeLayerUpdate(layer);
John Reck19b6bcf2014-02-14 20:03:38 -0800333}
334
John Reck918ad522014-06-27 14:45:25 -0700335CREATE_BRIDGE1(detachSurfaceTexture, DeferredLayerUpdater* layer) {
336 args->layer->detachSurfaceTexture();
Chris Craikd41c4d82015-01-05 15:51:13 -0800337 return nullptr;
John Reck918ad522014-06-27 14:45:25 -0700338}
339
340void RenderProxy::detachSurfaceTexture(DeferredLayerUpdater* layer) {
341 SETUP_TASK(detachSurfaceTexture);
342 args->layer = layer;
343 postAndWait(task);
344}
345
John Reck51f2d602016-04-06 07:50:47 -0700346CREATE_BRIDGE2(destroyHardwareResources, CanvasContext* context, TreeObserver* observer) {
347 args->context->destroyHardwareResources(args->observer);
Chris Craikd41c4d82015-01-05 15:51:13 -0800348 return nullptr;
John Recke1628b72014-05-23 15:11:19 -0700349}
350
John Reck51f2d602016-04-06 07:50:47 -0700351void RenderProxy::destroyHardwareResources(TreeObserver* observer) {
John Reckf47a5942014-06-30 16:20:04 -0700352 SETUP_TASK(destroyHardwareResources);
John Recke1628b72014-05-23 15:11:19 -0700353 args->context = mContext;
John Reck51f2d602016-04-06 07:50:47 -0700354 args->observer = observer;
355 postAndWait(task);
John Recke1628b72014-05-23 15:11:19 -0700356}
357
Chris Craik2507c342015-05-04 14:36:49 -0700358CREATE_BRIDGE2(trimMemory, RenderThread* thread, int level) {
John Reckf47a5942014-06-30 16:20:04 -0700359 CanvasContext::trimMemory(*args->thread, args->level);
Chris Craikd41c4d82015-01-05 15:51:13 -0800360 return nullptr;
John Reckf47a5942014-06-30 16:20:04 -0700361}
362
363void RenderProxy::trimMemory(int level) {
John Reckcd3a22c2014-08-06 13:33:59 -0700364 // Avoid creating a RenderThread to do a trimMemory.
365 if (RenderThread::hasInstance()) {
366 RenderThread& thread = RenderThread::getInstance();
Chris Craik2507c342015-05-04 14:36:49 -0700367 SETUP_TASK(trimMemory);
John Reckcd3a22c2014-08-06 13:33:59 -0700368 args->thread = &thread;
369 args->level = level;
370 thread.queue(task);
371 }
John Reckf47a5942014-06-30 16:20:04 -0700372}
373
Chris Craik2507c342015-05-04 14:36:49 -0700374CREATE_BRIDGE2(overrideProperty, const char* name, const char* value) {
375 Properties::overrideProperty(args->name, args->value);
376 return nullptr;
377}
378
379void RenderProxy::overrideProperty(const char* name, const char* value) {
Chris Craik2507c342015-05-04 14:36:49 -0700380 SETUP_TASK(overrideProperty);
381 args->name = name;
382 args->value = value;
383 staticPostAndWait(task); // expensive, but block here since name/value pointers owned by caller
384}
385
John Reck28ad7b52014-04-07 16:59:25 -0700386CREATE_BRIDGE0(fence) {
387 // Intentionally empty
Chris Craikd41c4d82015-01-05 15:51:13 -0800388 return nullptr;
John Reck28ad7b52014-04-07 16:59:25 -0700389}
390
Andreas Gampe64bb4132014-11-22 00:35:09 +0000391template <typename T>
392void UNUSED(T t) {}
393
John Reck28ad7b52014-04-07 16:59:25 -0700394void RenderProxy::fence() {
395 SETUP_TASK(fence);
Andreas Gampe1e196742014-11-10 15:23:43 -0800396 UNUSED(args);
John Reck28ad7b52014-04-07 16:59:25 -0700397 postAndWait(task);
398}
399
Thomas Buhotc0a0e1a2016-01-18 10:31:58 +0100400void RenderProxy::staticFence() {
401 SETUP_TASK(fence);
402 UNUSED(args);
403 staticPostAndWait(task);
404}
405
John Reckf47a5942014-06-30 16:20:04 -0700406CREATE_BRIDGE1(stopDrawing, CanvasContext* context) {
407 args->context->stopDrawing();
Chris Craikd41c4d82015-01-05 15:51:13 -0800408 return nullptr;
John Reckf47a5942014-06-30 16:20:04 -0700409}
410
411void RenderProxy::stopDrawing() {
412 SETUP_TASK(stopDrawing);
413 args->context = mContext;
414 postAndWait(task);
415}
416
John Recka5dda642014-05-22 15:43:54 -0700417CREATE_BRIDGE1(notifyFramePending, CanvasContext* context) {
418 args->context->notifyFramePending();
Chris Craikd41c4d82015-01-05 15:51:13 -0800419 return nullptr;
John Recka5dda642014-05-22 15:43:54 -0700420}
421
422void RenderProxy::notifyFramePending() {
423 SETUP_TASK(notifyFramePending);
424 args->context = mContext;
425 mRenderThread.queueAtFront(task);
426}
427
John Reck7f2e5e32015-05-05 11:00:53 -0700428CREATE_BRIDGE4(dumpProfileInfo, CanvasContext* context, RenderThread* thread,
429 int fd, int dumpFlags) {
John Reckfe5e7b72014-05-23 17:42:28 -0700430 args->context->profiler().dumpData(args->fd);
Chris Craik53e51e42015-06-01 10:35:35 -0700431 if (args->dumpFlags & DumpFlags::FrameStats) {
John Reckba6adf62015-02-19 14:36:50 -0800432 args->context->dumpFrames(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 }
John Recka41f2442016-04-07 16:36:57 -0700437 if (args->dumpFlags & DumpFlags::JankStats) {
438 args->thread->jankTracker().dump(args->fd);
439 }
Chris Craikd41c4d82015-01-05 15:51:13 -0800440 return nullptr;
John Reckfe5e7b72014-05-23 17:42:28 -0700441}
442
John Reckba6adf62015-02-19 14:36:50 -0800443void RenderProxy::dumpProfileInfo(int fd, int dumpFlags) {
John Reckfe5e7b72014-05-23 17:42:28 -0700444 SETUP_TASK(dumpProfileInfo);
445 args->context = mContext;
John Reck7f2e5e32015-05-05 11:00:53 -0700446 args->thread = &mRenderThread;
John Reckfe5e7b72014-05-23 17:42:28 -0700447 args->fd = fd;
John Reckba6adf62015-02-19 14:36:50 -0800448 args->dumpFlags = dumpFlags;
John Reckfe5e7b72014-05-23 17:42:28 -0700449 postAndWait(task);
450}
451
John Reck7f2e5e32015-05-05 11:00:53 -0700452CREATE_BRIDGE1(resetProfileInfo, CanvasContext* context) {
453 args->context->resetFrameStats();
454 return nullptr;
455}
456
457void RenderProxy::resetProfileInfo() {
458 SETUP_TASK(resetProfileInfo);
459 args->context = mContext;
460 postAndWait(task);
461}
462
John Reckba6adf62015-02-19 14:36:50 -0800463CREATE_BRIDGE2(dumpGraphicsMemory, int fd, RenderThread* thread) {
464 args->thread->jankTracker().dump(args->fd);
465
Chris Craik2ae07332015-01-21 14:22:39 -0800466 FILE *file = fdopen(args->fd, "a");
467 if (Caches::hasInstance()) {
468 String8 cachesLog;
469 Caches::getInstance().dumpMemoryUsage(cachesLog);
470 fprintf(file, "\nCaches:\n%s\n", cachesLog.string());
471 } else {
472 fprintf(file, "\nNo caches instance.\n");
473 }
Chris Craikff3edce2016-01-14 10:04:08 -0800474#if HWUI_NEW_OPS
475 fprintf(file, "\nPipeline=FrameBuilder\n");
476#else
477 fprintf(file, "\nPipeline=OpenGLRenderer\n");
478#endif
Chris Craik2ae07332015-01-21 14:22:39 -0800479 fflush(file);
Chris Craikd41c4d82015-01-05 15:51:13 -0800480 return nullptr;
John Reck0e89e2b2014-10-31 14:49:06 -0700481}
482
Chris Craik2ae07332015-01-21 14:22:39 -0800483void RenderProxy::dumpGraphicsMemory(int fd) {
youngmin0822.leec80c9ad2015-03-20 21:22:32 +0900484 if (!RenderThread::hasInstance()) return;
Chris Craik2ae07332015-01-21 14:22:39 -0800485 SETUP_TASK(dumpGraphicsMemory);
John Reck0e89e2b2014-10-31 14:49:06 -0700486 args->fd = fd;
John Reckba6adf62015-02-19 14:36:50 -0800487 args->thread = &RenderThread::getInstance();
John Reck0e89e2b2014-10-31 14:49:06 -0700488 staticPostAndWait(task);
489}
490
Skuhneea7a7fb2015-08-28 07:10:31 -0700491CREATE_BRIDGE4(setTextureAtlas, RenderThread* thread, GraphicBuffer* buffer, int64_t* map,
492 size_t size) {
John Reck3b202512014-06-23 13:13:08 -0700493 CanvasContext::setTextureAtlas(*args->thread, args->buffer, args->map, args->size);
Chris Craikd41c4d82015-01-05 15:51:13 -0800494 args->buffer->decStrong(nullptr);
495 return nullptr;
John Reck3b202512014-06-23 13:13:08 -0700496}
497
498void RenderProxy::setTextureAtlas(const sp<GraphicBuffer>& buffer, int64_t* map, size_t size) {
499 SETUP_TASK(setTextureAtlas);
500 args->thread = &mRenderThread;
501 args->buffer = buffer.get();
Chris Craikd41c4d82015-01-05 15:51:13 -0800502 args->buffer->incStrong(nullptr);
John Reck3b202512014-06-23 13:13:08 -0700503 args->map = map;
504 args->size = size;
505 post(task);
506}
507
John Reckedc524c2015-03-18 15:24:33 -0700508CREATE_BRIDGE2(setProcessStatsBuffer, RenderThread* thread, int fd) {
509 args->thread->jankTracker().switchStorageToAshmem(args->fd);
510 close(args->fd);
511 return nullptr;
512}
513
514void RenderProxy::setProcessStatsBuffer(int fd) {
515 SETUP_TASK(setProcessStatsBuffer);
516 args->thread = &mRenderThread;
517 args->fd = dup(fd);
518 post(task);
519}
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 Reck10dd0582016-03-31 16:36:16 -0700628CREATE_BRIDGE3(copySurfaceInto, RenderThread* thread,
629 Surface* surface, SkBitmap* bitmap) {
630 return (void*) Readback::copySurfaceInto(*args->thread,
631 *args->surface, args->bitmap);
632}
633
John Recke94cbc72016-04-25 13:03:44 -0700634int RenderProxy::copySurfaceInto(sp<Surface>& surface, SkBitmap* bitmap) {
John Reck10dd0582016-03-31 16:36:16 -0700635 SETUP_TASK(copySurfaceInto);
636 args->bitmap = bitmap;
637 args->surface = surface.get();
638 args->thread = &RenderThread::getInstance();
John Recke94cbc72016-04-25 13:03:44 -0700639 return static_cast<int>(
640 reinterpret_cast<intptr_t>( staticPostAndWait(task) ));
John Reck10dd0582016-03-31 16:36:16 -0700641}
642
John Reck43871902016-08-01 14:39:24 -0700643CREATE_BRIDGE2(prepareToDraw, RenderThread* thread, SkBitmap* bitmap) {
644 if (Caches::hasInstance() && args->thread->eglManager().hasEglContext()) {
645 ATRACE_NAME("Bitmap#prepareToDraw task");
646 Caches::getInstance().textureCache.prefetch(args->bitmap);
647 }
648 delete args->bitmap;
649 args->bitmap = nullptr;
650 return nullptr;
651}
652
653void RenderProxy::prepareToDraw(const SkBitmap& bitmap) {
654 // If we haven't spun up a hardware accelerated window yet, there's no
655 // point in precaching these bitmaps as it can't impact jank.
656 // We also don't know if we even will spin up a hardware-accelerated
657 // window or not.
658 if (!RenderThread::hasInstance()) return;
659 RenderThread* renderThread = &RenderThread::getInstance();
660 SETUP_TASK(prepareToDraw);
661 args->thread = renderThread;
662 args->bitmap = new SkBitmap(bitmap);
663 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
John Reck4f02bf42014-01-03 18:09:17 -0800678void RenderProxy::post(RenderTask* task) {
679 mRenderThread.queue(task);
680}
681
682void* RenderProxy::postAndWait(MethodInvokeRenderTask* task) {
683 void* retval;
684 task->setReturnPtr(&retval);
John Reckcba287b2015-11-10 12:52:44 -0800685 SignalingRenderTask syncTask(task, &mSyncMutex, &mSyncCondition);
686 AutoMutex _lock(mSyncMutex);
687 mRenderThread.queue(&syncTask);
688 mSyncCondition.wait(mSyncMutex);
John Reck4f02bf42014-01-03 18:09:17 -0800689 return retval;
690}
691
John Reck0e89e2b2014-10-31 14:49:06 -0700692void* RenderProxy::staticPostAndWait(MethodInvokeRenderTask* task) {
693 RenderThread& thread = RenderThread::getInstance();
694 void* retval;
695 task->setReturnPtr(&retval);
Chris Craik0a24b142015-10-19 17:10:19 -0700696 thread.queueAndWait(task);
John Reck0e89e2b2014-10-31 14:49:06 -0700697 return retval;
698}
699
John Reck4f02bf42014-01-03 18:09:17 -0800700} /* namespace renderthread */
701} /* namespace uirenderer */
702} /* namespace android */