• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 #include "RenderProxy.h"
18 
19 #include "DeferredLayerUpdater.h"
20 #include "DisplayList.h"
21 #include "LayerRenderer.h"
22 #include "Rect.h"
23 #include "renderthread/CanvasContext.h"
24 #include "renderthread/RenderTask.h"
25 #include "renderthread/RenderThread.h"
26 #include "utils/Macros.h"
27 
28 namespace android {
29 namespace uirenderer {
30 namespace renderthread {
31 
32 #define ARGS(method) method ## Args
33 
34 #define CREATE_BRIDGE0(name) CREATE_BRIDGE(name,,,,,,,,)
35 #define CREATE_BRIDGE1(name, a1) CREATE_BRIDGE(name, a1,,,,,,,)
36 #define CREATE_BRIDGE2(name, a1, a2) CREATE_BRIDGE(name, a1,a2,,,,,,)
37 #define CREATE_BRIDGE3(name, a1, a2, a3) CREATE_BRIDGE(name, a1,a2,a3,,,,,)
38 #define CREATE_BRIDGE4(name, a1, a2, a3, a4) CREATE_BRIDGE(name, a1,a2,a3,a4,,,,)
39 #define CREATE_BRIDGE5(name, a1, a2, a3, a4, a5) CREATE_BRIDGE(name, a1,a2,a3,a4,a5,,,)
40 #define CREATE_BRIDGE6(name, a1, a2, a3, a4, a5, a6) CREATE_BRIDGE(name, a1,a2,a3,a4,a5,a6,,)
41 #define CREATE_BRIDGE7(name, a1, a2, a3, a4, a5, a6, a7) CREATE_BRIDGE(name, a1,a2,a3,a4,a5,a6,a7,)
42 #define CREATE_BRIDGE(name, a1, a2, a3, a4, a5, a6, a7, a8) \
43     typedef struct { \
44         a1; a2; a3; a4; a5; a6; a7; a8; \
45     } ARGS(name); \
46     static void* Bridge_ ## name(ARGS(name)* args)
47 
48 #define SETUP_TASK(method) \
49     LOG_ALWAYS_FATAL_IF( METHOD_INVOKE_PAYLOAD_SIZE < sizeof(ARGS(method)), \
50         "METHOD_INVOKE_PAYLOAD_SIZE %zu is smaller than sizeof(" #method "Args) %zu", \
51                 METHOD_INVOKE_PAYLOAD_SIZE, sizeof(ARGS(method))); \
52     MethodInvokeRenderTask* task = new MethodInvokeRenderTask((RunnableMethod) Bridge_ ## method); \
53     ARGS(method) *args = (ARGS(method) *) task->payload()
54 
55 namespace DumpFlags {
56     enum {
57         FrameStats = 1 << 0,
58         Reset      = 1 << 1,
59     };
60 };
61 
CREATE_BRIDGE4(createContext,RenderThread * thread,bool translucent,RenderNode * rootRenderNode,IContextFactory * contextFactory)62 CREATE_BRIDGE4(createContext, RenderThread* thread, bool translucent,
63         RenderNode* rootRenderNode, IContextFactory* contextFactory) {
64     return new CanvasContext(*args->thread, args->translucent,
65             args->rootRenderNode, args->contextFactory);
66 }
67 
RenderProxy(bool translucent,RenderNode * rootRenderNode,IContextFactory * contextFactory)68 RenderProxy::RenderProxy(bool translucent, RenderNode* rootRenderNode, IContextFactory* contextFactory)
69         : mRenderThread(RenderThread::getInstance())
70         , mContext(nullptr) {
71     SETUP_TASK(createContext);
72     args->translucent = translucent;
73     args->rootRenderNode = rootRenderNode;
74     args->thread = &mRenderThread;
75     args->contextFactory = contextFactory;
76     mContext = (CanvasContext*) postAndWait(task);
77     mDrawFrameTask.setContext(&mRenderThread, mContext);
78 }
79 
~RenderProxy()80 RenderProxy::~RenderProxy() {
81     destroyContext();
82 }
83 
CREATE_BRIDGE1(destroyContext,CanvasContext * context)84 CREATE_BRIDGE1(destroyContext, CanvasContext* context) {
85     delete args->context;
86     return nullptr;
87 }
88 
destroyContext()89 void RenderProxy::destroyContext() {
90     if (mContext) {
91         SETUP_TASK(destroyContext);
92         args->context = mContext;
93         mContext = nullptr;
94         mDrawFrameTask.setContext(nullptr, nullptr);
95         // 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);
98     }
99 }
100 
CREATE_BRIDGE2(setSwapBehavior,CanvasContext * context,SwapBehavior swapBehavior)101 CREATE_BRIDGE2(setSwapBehavior, CanvasContext* context, SwapBehavior swapBehavior) {
102     args->context->setSwapBehavior(args->swapBehavior);
103     return nullptr;
104 }
105 
setSwapBehavior(SwapBehavior swapBehavior)106 void RenderProxy::setSwapBehavior(SwapBehavior swapBehavior) {
107     SETUP_TASK(setSwapBehavior);
108     args->context = mContext;
109     args->swapBehavior = swapBehavior;
110     post(task);
111 }
112 
CREATE_BRIDGE1(loadSystemProperties,CanvasContext * context)113 CREATE_BRIDGE1(loadSystemProperties, CanvasContext* context) {
114     bool needsRedraw = false;
115     if (Caches::hasInstance()) {
116         needsRedraw = Properties::load();
117     }
118     if (args->context->profiler().consumeProperties()) {
119         needsRedraw = true;
120     }
121     return (void*) needsRedraw;
122 }
123 
loadSystemProperties()124 bool RenderProxy::loadSystemProperties() {
125     SETUP_TASK(loadSystemProperties);
126     args->context = mContext;
127     return (bool) postAndWait(task);
128 }
129 
CREATE_BRIDGE2(setName,CanvasContext * context,const char * name)130 CREATE_BRIDGE2(setName, CanvasContext* context, const char* name) {
131     args->context->setName(std::string(args->name));
132     return nullptr;
133 }
134 
setName(const char * name)135 void RenderProxy::setName(const char* name) {
136     SETUP_TASK(setName);
137     args->context = mContext;
138     args->name = name;
139     postAndWait(task); // block since name/value pointers owned by caller
140 }
141 
CREATE_BRIDGE2(initialize,CanvasContext * context,ANativeWindow * window)142 CREATE_BRIDGE2(initialize, CanvasContext* context, ANativeWindow* window) {
143     return (void*) args->context->initialize(args->window);
144 }
145 
initialize(const sp<ANativeWindow> & window)146 bool RenderProxy::initialize(const sp<ANativeWindow>& window) {
147     SETUP_TASK(initialize);
148     args->context = mContext;
149     args->window = window.get();
150     return (bool) postAndWait(task);
151 }
152 
CREATE_BRIDGE2(updateSurface,CanvasContext * context,ANativeWindow * window)153 CREATE_BRIDGE2(updateSurface, CanvasContext* context, ANativeWindow* window) {
154     args->context->updateSurface(args->window);
155     return nullptr;
156 }
157 
updateSurface(const sp<ANativeWindow> & window)158 void RenderProxy::updateSurface(const sp<ANativeWindow>& window) {
159     SETUP_TASK(updateSurface);
160     args->context = mContext;
161     args->window = window.get();
162     postAndWait(task);
163 }
164 
CREATE_BRIDGE2(pauseSurface,CanvasContext * context,ANativeWindow * window)165 CREATE_BRIDGE2(pauseSurface, CanvasContext* context, ANativeWindow* window) {
166     return (void*) args->context->pauseSurface(args->window);
167 }
168 
pauseSurface(const sp<ANativeWindow> & window)169 bool RenderProxy::pauseSurface(const sp<ANativeWindow>& window) {
170     SETUP_TASK(pauseSurface);
171     args->context = mContext;
172     args->window = window.get();
173     return (bool) postAndWait(task);
174 }
175 
CREATE_BRIDGE6(setup,CanvasContext * context,int width,int height,float lightRadius,uint8_t ambientShadowAlpha,uint8_t spotShadowAlpha)176 CREATE_BRIDGE6(setup, CanvasContext* context, int width, int height,
177         float lightRadius, uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
178     args->context->setup(args->width, args->height, args->lightRadius,
179             args->ambientShadowAlpha, args->spotShadowAlpha);
180     return nullptr;
181 }
182 
setup(int width,int height,float lightRadius,uint8_t ambientShadowAlpha,uint8_t spotShadowAlpha)183 void RenderProxy::setup(int width, int height, float lightRadius,
184         uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
185     SETUP_TASK(setup);
186     args->context = mContext;
187     args->width = width;
188     args->height = height;
189     args->lightRadius = lightRadius;
190     args->ambientShadowAlpha = ambientShadowAlpha;
191     args->spotShadowAlpha = spotShadowAlpha;
192     post(task);
193 }
194 
CREATE_BRIDGE2(setLightCenter,CanvasContext * context,Vector3 lightCenter)195 CREATE_BRIDGE2(setLightCenter, CanvasContext* context, Vector3 lightCenter) {
196     args->context->setLightCenter(args->lightCenter);
197     return nullptr;
198 }
199 
setLightCenter(const Vector3 & lightCenter)200 void RenderProxy::setLightCenter(const Vector3& lightCenter) {
201     SETUP_TASK(setLightCenter);
202     args->context = mContext;
203     args->lightCenter = lightCenter;
204     post(task);
205 }
206 
CREATE_BRIDGE2(setOpaque,CanvasContext * context,bool opaque)207 CREATE_BRIDGE2(setOpaque, CanvasContext* context, bool opaque) {
208     args->context->setOpaque(args->opaque);
209     return nullptr;
210 }
211 
setOpaque(bool opaque)212 void RenderProxy::setOpaque(bool opaque) {
213     SETUP_TASK(setOpaque);
214     args->context = mContext;
215     args->opaque = opaque;
216     post(task);
217 }
218 
frameInfo()219 int64_t* RenderProxy::frameInfo() {
220     return mDrawFrameTask.frameInfo();
221 }
222 
syncAndDrawFrame()223 int RenderProxy::syncAndDrawFrame() {
224     return mDrawFrameTask.drawFrame();
225 }
226 
CREATE_BRIDGE1(destroy,CanvasContext * context)227 CREATE_BRIDGE1(destroy, CanvasContext* context) {
228     args->context->destroy();
229     return nullptr;
230 }
231 
destroy()232 void RenderProxy::destroy() {
233     SETUP_TASK(destroy);
234     args->context = mContext;
235     // destroyCanvasAndSurface() needs a fence as when it returns the
236     // underlying BufferQueue is going to be released from under
237     // the render thread.
238     postAndWait(task);
239 }
240 
CREATE_BRIDGE2(invokeFunctor,RenderThread * thread,Functor * functor)241 CREATE_BRIDGE2(invokeFunctor, RenderThread* thread, Functor* functor) {
242     CanvasContext::invokeFunctor(*args->thread, args->functor);
243     return nullptr;
244 }
245 
invokeFunctor(Functor * functor,bool waitForCompletion)246 void RenderProxy::invokeFunctor(Functor* functor, bool waitForCompletion) {
247     ATRACE_CALL();
248     RenderThread& thread = RenderThread::getInstance();
249     SETUP_TASK(invokeFunctor);
250     args->thread = &thread;
251     args->functor = functor;
252     if (waitForCompletion) {
253         // waitForCompletion = true is expected to be fairly rare and only
254         // happen in destruction. Thus it should be fine to temporarily
255         // create a Mutex
256         staticPostAndWait(task);
257     } else {
258         thread.queue(task);
259     }
260 }
261 
CREATE_BRIDGE2(runWithGlContext,CanvasContext * context,RenderTask * task)262 CREATE_BRIDGE2(runWithGlContext, CanvasContext* context, RenderTask* task) {
263     args->context->runWithGlContext(args->task);
264     return nullptr;
265 }
266 
runWithGlContext(RenderTask * gltask)267 void RenderProxy::runWithGlContext(RenderTask* gltask) {
268     SETUP_TASK(runWithGlContext);
269     args->context = mContext;
270     args->task = gltask;
271     postAndWait(task);
272 }
273 
CREATE_BRIDGE2(createTextureLayer,RenderThread * thread,CanvasContext * context)274 CREATE_BRIDGE2(createTextureLayer, RenderThread* thread, CanvasContext* context) {
275     Layer* layer = args->context->createTextureLayer();
276     if (!layer) return nullptr;
277     return new DeferredLayerUpdater(*args->thread, layer);
278 }
279 
createTextureLayer()280 DeferredLayerUpdater* RenderProxy::createTextureLayer() {
281     SETUP_TASK(createTextureLayer);
282     args->context = mContext;
283     args->thread = &mRenderThread;
284     void* retval = postAndWait(task);
285     DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval);
286     return layer;
287 }
288 
CREATE_BRIDGE2(buildLayer,CanvasContext * context,RenderNode * node)289 CREATE_BRIDGE2(buildLayer, CanvasContext* context, RenderNode* node) {
290     args->context->buildLayer(args->node);
291     return nullptr;
292 }
293 
buildLayer(RenderNode * node)294 void RenderProxy::buildLayer(RenderNode* node) {
295     SETUP_TASK(buildLayer);
296     args->context = mContext;
297     args->node = node;
298     postAndWait(task);
299 }
300 
CREATE_BRIDGE3(copyLayerInto,CanvasContext * context,DeferredLayerUpdater * layer,SkBitmap * bitmap)301 CREATE_BRIDGE3(copyLayerInto, CanvasContext* context, DeferredLayerUpdater* layer,
302         SkBitmap* bitmap) {
303     bool success = args->context->copyLayerInto(args->layer, args->bitmap);
304     return (void*) success;
305 }
306 
copyLayerInto(DeferredLayerUpdater * layer,SkBitmap & bitmap)307 bool RenderProxy::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap& bitmap) {
308     SETUP_TASK(copyLayerInto);
309     args->context = mContext;
310     args->layer = layer;
311     args->bitmap = &bitmap;
312     return (bool) postAndWait(task);
313 }
314 
pushLayerUpdate(DeferredLayerUpdater * layer)315 void RenderProxy::pushLayerUpdate(DeferredLayerUpdater* layer) {
316     mDrawFrameTask.pushLayerUpdate(layer);
317 }
318 
cancelLayerUpdate(DeferredLayerUpdater * layer)319 void RenderProxy::cancelLayerUpdate(DeferredLayerUpdater* layer) {
320     mDrawFrameTask.removeLayerUpdate(layer);
321 }
322 
CREATE_BRIDGE1(detachSurfaceTexture,DeferredLayerUpdater * layer)323 CREATE_BRIDGE1(detachSurfaceTexture, DeferredLayerUpdater* layer) {
324     args->layer->detachSurfaceTexture();
325     return nullptr;
326 }
327 
detachSurfaceTexture(DeferredLayerUpdater * layer)328 void RenderProxy::detachSurfaceTexture(DeferredLayerUpdater* layer) {
329     SETUP_TASK(detachSurfaceTexture);
330     args->layer = layer;
331     postAndWait(task);
332 }
333 
CREATE_BRIDGE1(destroyHardwareResources,CanvasContext * context)334 CREATE_BRIDGE1(destroyHardwareResources, CanvasContext* context) {
335     args->context->destroyHardwareResources();
336     return nullptr;
337 }
338 
destroyHardwareResources()339 void RenderProxy::destroyHardwareResources() {
340     SETUP_TASK(destroyHardwareResources);
341     args->context = mContext;
342     post(task);
343 }
344 
CREATE_BRIDGE2(trimMemory,RenderThread * thread,int level)345 CREATE_BRIDGE2(trimMemory, RenderThread* thread, int level) {
346     CanvasContext::trimMemory(*args->thread, args->level);
347     return nullptr;
348 }
349 
trimMemory(int level)350 void RenderProxy::trimMemory(int level) {
351     // Avoid creating a RenderThread to do a trimMemory.
352     if (RenderThread::hasInstance()) {
353         RenderThread& thread = RenderThread::getInstance();
354         SETUP_TASK(trimMemory);
355         args->thread = &thread;
356         args->level = level;
357         thread.queue(task);
358     }
359 }
360 
CREATE_BRIDGE2(overrideProperty,const char * name,const char * value)361 CREATE_BRIDGE2(overrideProperty, const char* name, const char* value) {
362     Properties::overrideProperty(args->name, args->value);
363     return nullptr;
364 }
365 
overrideProperty(const char * name,const char * value)366 void RenderProxy::overrideProperty(const char* name, const char* value) {
367     SETUP_TASK(overrideProperty);
368     args->name = name;
369     args->value = value;
370     staticPostAndWait(task); // expensive, but block here since name/value pointers owned by caller
371 }
372 
CREATE_BRIDGE0(fence)373 CREATE_BRIDGE0(fence) {
374     // Intentionally empty
375     return nullptr;
376 }
377 
378 template <typename T>
UNUSED(T t)379 void UNUSED(T t) {}
380 
fence()381 void RenderProxy::fence() {
382     SETUP_TASK(fence);
383     UNUSED(args);
384     postAndWait(task);
385 }
386 
CREATE_BRIDGE1(stopDrawing,CanvasContext * context)387 CREATE_BRIDGE1(stopDrawing, CanvasContext* context) {
388     args->context->stopDrawing();
389     return nullptr;
390 }
391 
stopDrawing()392 void RenderProxy::stopDrawing() {
393     SETUP_TASK(stopDrawing);
394     args->context = mContext;
395     postAndWait(task);
396 }
397 
CREATE_BRIDGE1(notifyFramePending,CanvasContext * context)398 CREATE_BRIDGE1(notifyFramePending, CanvasContext* context) {
399     args->context->notifyFramePending();
400     return nullptr;
401 }
402 
notifyFramePending()403 void RenderProxy::notifyFramePending() {
404     SETUP_TASK(notifyFramePending);
405     args->context = mContext;
406     mRenderThread.queueAtFront(task);
407 }
408 
CREATE_BRIDGE4(dumpProfileInfo,CanvasContext * context,RenderThread * thread,int fd,int dumpFlags)409 CREATE_BRIDGE4(dumpProfileInfo, CanvasContext* context, RenderThread* thread,
410         int fd, int dumpFlags) {
411     args->context->profiler().dumpData(args->fd);
412     args->thread->jankTracker().dump(args->fd);
413     if (args->dumpFlags & DumpFlags::FrameStats) {
414         args->context->dumpFrames(args->fd);
415     }
416     if (args->dumpFlags & DumpFlags::Reset) {
417         args->context->resetFrameStats();
418     }
419     return nullptr;
420 }
421 
dumpProfileInfo(int fd,int dumpFlags)422 void RenderProxy::dumpProfileInfo(int fd, int dumpFlags) {
423     SETUP_TASK(dumpProfileInfo);
424     args->context = mContext;
425     args->thread = &mRenderThread;
426     args->fd = fd;
427     args->dumpFlags = dumpFlags;
428     postAndWait(task);
429 }
430 
CREATE_BRIDGE1(resetProfileInfo,CanvasContext * context)431 CREATE_BRIDGE1(resetProfileInfo, CanvasContext* context) {
432     args->context->resetFrameStats();
433     return nullptr;
434 }
435 
resetProfileInfo()436 void RenderProxy::resetProfileInfo() {
437     SETUP_TASK(resetProfileInfo);
438     args->context = mContext;
439     postAndWait(task);
440 }
441 
CREATE_BRIDGE2(dumpGraphicsMemory,int fd,RenderThread * thread)442 CREATE_BRIDGE2(dumpGraphicsMemory, int fd, RenderThread* thread) {
443     args->thread->jankTracker().dump(args->fd);
444 
445     FILE *file = fdopen(args->fd, "a");
446     if (Caches::hasInstance()) {
447         String8 cachesLog;
448         Caches::getInstance().dumpMemoryUsage(cachesLog);
449         fprintf(file, "\nCaches:\n%s\n", cachesLog.string());
450     } else {
451         fprintf(file, "\nNo caches instance.\n");
452     }
453     fflush(file);
454     return nullptr;
455 }
456 
dumpGraphicsMemory(int fd)457 void RenderProxy::dumpGraphicsMemory(int fd) {
458     if (!RenderThread::hasInstance()) return;
459     SETUP_TASK(dumpGraphicsMemory);
460     args->fd = fd;
461     args->thread = &RenderThread::getInstance();
462     staticPostAndWait(task);
463 }
464 
CREATE_BRIDGE4(setTextureAtlas,RenderThread * thread,GraphicBuffer * buffer,int64_t * map,size_t size)465 CREATE_BRIDGE4(setTextureAtlas, RenderThread* thread, GraphicBuffer* buffer, int64_t* map, size_t size) {
466     CanvasContext::setTextureAtlas(*args->thread, args->buffer, args->map, args->size);
467     args->buffer->decStrong(nullptr);
468     return nullptr;
469 }
470 
setTextureAtlas(const sp<GraphicBuffer> & buffer,int64_t * map,size_t size)471 void RenderProxy::setTextureAtlas(const sp<GraphicBuffer>& buffer, int64_t* map, size_t size) {
472     SETUP_TASK(setTextureAtlas);
473     args->thread = &mRenderThread;
474     args->buffer = buffer.get();
475     args->buffer->incStrong(nullptr);
476     args->map = map;
477     args->size = size;
478     post(task);
479 }
480 
CREATE_BRIDGE2(setProcessStatsBuffer,RenderThread * thread,int fd)481 CREATE_BRIDGE2(setProcessStatsBuffer, RenderThread* thread, int fd) {
482     args->thread->jankTracker().switchStorageToAshmem(args->fd);
483     close(args->fd);
484     return nullptr;
485 }
486 
setProcessStatsBuffer(int fd)487 void RenderProxy::setProcessStatsBuffer(int fd) {
488     SETUP_TASK(setProcessStatsBuffer);
489     args->thread = &mRenderThread;
490     args->fd = dup(fd);
491     post(task);
492 }
493 
post(RenderTask * task)494 void RenderProxy::post(RenderTask* task) {
495     mRenderThread.queue(task);
496 }
497 
postAndWait(MethodInvokeRenderTask * task)498 void* RenderProxy::postAndWait(MethodInvokeRenderTask* task) {
499     void* retval;
500     task->setReturnPtr(&retval);
501     SignalingRenderTask syncTask(task, &mSyncMutex, &mSyncCondition);
502     AutoMutex _lock(mSyncMutex);
503     mRenderThread.queue(&syncTask);
504     mSyncCondition.wait(mSyncMutex);
505     return retval;
506 }
507 
staticPostAndWait(MethodInvokeRenderTask * task)508 void* RenderProxy::staticPostAndWait(MethodInvokeRenderTask* task) {
509     RenderThread& thread = RenderThread::getInstance();
510     void* retval;
511     task->setReturnPtr(&retval);
512     Mutex mutex;
513     Condition condition;
514     SignalingRenderTask syncTask(task, &mutex, &condition);
515     AutoMutex _lock(mutex);
516     thread.queue(&syncTask);
517     condition.wait(mutex);
518     return retval;
519 }
520 
521 } /* namespace renderthread */
522 } /* namespace uirenderer */
523 } /* namespace android */
524