RenderProxy.cpp revision 4d7094dc998b4765cb73c6ec26e61a6cc0df030f
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 "Readback.h"
22#include "Rect.h"
23#include "renderthread/CanvasContext.h"
24#include "renderthread/EglManager.h"
25#include "renderthread/RenderTask.h"
26#include "renderthread/RenderThread.h"
27#include "utils/Macros.h"
28#include "utils/TimeUtils.h"
29
30#include <ui/GraphicBuffer.h>
31
32namespace android {
33namespace uirenderer {
34namespace renderthread {
35
36#define ARGS(method) method ## Args
37
38#define CREATE_BRIDGE0(name) CREATE_BRIDGE(name,,,,,,,,)
39#define CREATE_BRIDGE1(name, a1) CREATE_BRIDGE(name, a1,,,,,,,)
40#define CREATE_BRIDGE2(name, a1, a2) CREATE_BRIDGE(name, a1,a2,,,,,,)
41#define CREATE_BRIDGE3(name, a1, a2, a3) CREATE_BRIDGE(name, a1,a2,a3,,,,,)
42#define CREATE_BRIDGE4(name, a1, a2, a3, a4) CREATE_BRIDGE(name, a1,a2,a3,a4,,,,)
43#define CREATE_BRIDGE5(name, a1, a2, a3, a4, a5) CREATE_BRIDGE(name, a1,a2,a3,a4,a5,,,)
44#define CREATE_BRIDGE6(name, a1, a2, a3, a4, a5, a6) CREATE_BRIDGE(name, a1,a2,a3,a4,a5,a6,,)
45#define CREATE_BRIDGE7(name, a1, a2, a3, a4, a5, a6, a7) CREATE_BRIDGE(name, a1,a2,a3,a4,a5,a6,a7,)
46#define CREATE_BRIDGE(name, a1, a2, a3, a4, a5, a6, a7, a8) \
47    typedef struct { \
48        a1; a2; a3; a4; a5; a6; a7; a8; \
49    } ARGS(name); \
50    static_assert(std::is_trivially_destructible<ARGS(name)>::value, \
51            "Error, ARGS must be trivially destructible!"); \
52    static void* Bridge_ ## name(ARGS(name)* args)
53
54#define SETUP_TASK(method) \
55    LOG_ALWAYS_FATAL_IF( METHOD_INVOKE_PAYLOAD_SIZE < sizeof(ARGS(method)), \
56        "METHOD_INVOKE_PAYLOAD_SIZE %zu is smaller than sizeof(" #method "Args) %zu", \
57                METHOD_INVOKE_PAYLOAD_SIZE, sizeof(ARGS(method))); \
58    MethodInvokeRenderTask* task = new MethodInvokeRenderTask((RunnableMethod) Bridge_ ## method); \
59    ARGS(method) *args = (ARGS(method) *) task->payload()
60
61CREATE_BRIDGE4(createContext, RenderThread* thread, bool translucent,
62        RenderNode* rootRenderNode, IContextFactory* contextFactory) {
63    return CanvasContext::create(*args->thread, args->translucent,
64            args->rootRenderNode, args->contextFactory);
65}
66
67RenderProxy::RenderProxy(bool translucent, RenderNode* rootRenderNode, IContextFactory* contextFactory)
68        : mRenderThread(RenderThread::getInstance())
69        , mContext(nullptr) {
70    SETUP_TASK(createContext);
71    args->translucent = translucent;
72    args->rootRenderNode = rootRenderNode;
73    args->thread = &mRenderThread;
74    args->contextFactory = contextFactory;
75    mContext = (CanvasContext*) postAndWait(task);
76    mDrawFrameTask.setContext(&mRenderThread, mContext, rootRenderNode);
77}
78
79RenderProxy::~RenderProxy() {
80    destroyContext();
81}
82
83CREATE_BRIDGE1(destroyContext, CanvasContext* context) {
84    delete args->context;
85    return nullptr;
86}
87
88void RenderProxy::destroyContext() {
89    if (mContext) {
90        SETUP_TASK(destroyContext);
91        args->context = mContext;
92        mContext = nullptr;
93        mDrawFrameTask.setContext(nullptr, nullptr, nullptr);
94        // This is also a fence as we need to be certain that there are no
95        // outstanding mDrawFrame tasks posted before it is destroyed
96        postAndWait(task);
97    }
98}
99
100CREATE_BRIDGE2(setSwapBehavior, CanvasContext* context, SwapBehavior swapBehavior) {
101    args->context->setSwapBehavior(args->swapBehavior);
102    return nullptr;
103}
104
105void RenderProxy::setSwapBehavior(SwapBehavior swapBehavior) {
106    SETUP_TASK(setSwapBehavior);
107    args->context = mContext;
108    args->swapBehavior = swapBehavior;
109    post(task);
110}
111
112CREATE_BRIDGE1(loadSystemProperties, CanvasContext* context) {
113    bool needsRedraw = false;
114    if (Caches::hasInstance()) {
115        needsRedraw = Properties::load();
116    }
117    if (args->context->profiler().consumeProperties()) {
118        needsRedraw = true;
119    }
120    return (void*) needsRedraw;
121}
122
123bool RenderProxy::loadSystemProperties() {
124    SETUP_TASK(loadSystemProperties);
125    args->context = mContext;
126    return (bool) postAndWait(task);
127}
128
129CREATE_BRIDGE2(setName, CanvasContext* context, const char* name) {
130    args->context->setName(std::string(args->name));
131    return nullptr;
132}
133
134void RenderProxy::setName(const char* name) {
135    SETUP_TASK(setName);
136    args->context = mContext;
137    args->name = name;
138    postAndWait(task); // block since name/value pointers owned by caller
139}
140
141CREATE_BRIDGE2(initialize, CanvasContext* context, Surface* surface) {
142    args->context->initialize(args->surface);
143    return nullptr;
144}
145
146void RenderProxy::initialize(const sp<Surface>& surface) {
147    SETUP_TASK(initialize);
148    args->context = mContext;
149    args->surface = surface.get();
150    post(task);
151}
152
153CREATE_BRIDGE2(updateSurface, CanvasContext* context, Surface* surface) {
154    args->context->updateSurface(args->surface);
155    return nullptr;
156}
157
158void RenderProxy::updateSurface(const sp<Surface>& surface) {
159    SETUP_TASK(updateSurface);
160    args->context = mContext;
161    args->surface = surface.get();
162    post(task);
163}
164
165CREATE_BRIDGE2(pauseSurface, CanvasContext* context, Surface* surface) {
166    return (void*) args->context->pauseSurface(args->surface);
167}
168
169bool RenderProxy::pauseSurface(const sp<Surface>& surface) {
170    SETUP_TASK(pauseSurface);
171    args->context = mContext;
172    args->surface = surface.get();
173    return (bool) postAndWait(task);
174}
175
176CREATE_BRIDGE2(setStopped, CanvasContext* context, bool stopped) {
177    args->context->setStopped(args->stopped);
178    return nullptr;
179}
180
181void RenderProxy::setStopped(bool stopped) {
182    SETUP_TASK(setStopped);
183    args->context = mContext;
184    args->stopped = stopped;
185    postAndWait(task);
186}
187
188CREATE_BRIDGE4(setup, CanvasContext* context,
189        float lightRadius, uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
190    args->context->setup(args->lightRadius,
191            args->ambientShadowAlpha, args->spotShadowAlpha);
192    return nullptr;
193}
194
195void RenderProxy::setup(float lightRadius,
196        uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
197    SETUP_TASK(setup);
198    args->context = mContext;
199    args->lightRadius = lightRadius;
200    args->ambientShadowAlpha = ambientShadowAlpha;
201    args->spotShadowAlpha = spotShadowAlpha;
202    post(task);
203}
204
205CREATE_BRIDGE2(setLightCenter, CanvasContext* context, Vector3 lightCenter) {
206    args->context->setLightCenter(args->lightCenter);
207    return nullptr;
208}
209
210void RenderProxy::setLightCenter(const Vector3& lightCenter) {
211    SETUP_TASK(setLightCenter);
212    args->context = mContext;
213    args->lightCenter = lightCenter;
214    post(task);
215}
216
217CREATE_BRIDGE2(setOpaque, CanvasContext* context, bool opaque) {
218    args->context->setOpaque(args->opaque);
219    return nullptr;
220}
221
222void RenderProxy::setOpaque(bool opaque) {
223    SETUP_TASK(setOpaque);
224    args->context = mContext;
225    args->opaque = opaque;
226    post(task);
227}
228
229int64_t* RenderProxy::frameInfo() {
230    return mDrawFrameTask.frameInfo();
231}
232
233int RenderProxy::syncAndDrawFrame() {
234    return mDrawFrameTask.drawFrame();
235}
236
237CREATE_BRIDGE1(destroy, CanvasContext* context) {
238    args->context->destroy();
239    return nullptr;
240}
241
242void RenderProxy::destroy() {
243    SETUP_TASK(destroy);
244    args->context = mContext;
245    // destroyCanvasAndSurface() needs a fence as when it returns the
246    // underlying BufferQueue is going to be released from under
247    // the render thread.
248    postAndWait(task);
249}
250
251CREATE_BRIDGE2(invokeFunctor, RenderThread* thread, Functor* functor) {
252    CanvasContext::invokeFunctor(*args->thread, args->functor);
253    return nullptr;
254}
255
256void RenderProxy::invokeFunctor(Functor* functor, bool waitForCompletion) {
257    ATRACE_CALL();
258    RenderThread& thread = RenderThread::getInstance();
259    SETUP_TASK(invokeFunctor);
260    args->thread = &thread;
261    args->functor = functor;
262    if (waitForCompletion) {
263        // waitForCompletion = true is expected to be fairly rare and only
264        // happen in destruction. Thus it should be fine to temporarily
265        // create a Mutex
266        staticPostAndWait(task);
267    } else {
268        thread.queue(task);
269    }
270}
271
272CREATE_BRIDGE1(createTextureLayer, CanvasContext* context) {
273    return args->context->createTextureLayer();
274}
275
276DeferredLayerUpdater* RenderProxy::createTextureLayer() {
277    SETUP_TASK(createTextureLayer);
278    args->context = mContext;
279    void* retval = postAndWait(task);
280    DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval);
281    return layer;
282}
283
284CREATE_BRIDGE2(buildLayer, CanvasContext* context, RenderNode* node) {
285    args->context->buildLayer(args->node);
286    return nullptr;
287}
288
289void RenderProxy::buildLayer(RenderNode* node) {
290    SETUP_TASK(buildLayer);
291    args->context = mContext;
292    args->node = node;
293    postAndWait(task);
294}
295
296CREATE_BRIDGE3(copyLayerInto, CanvasContext* context, DeferredLayerUpdater* layer,
297        SkBitmap* bitmap) {
298    bool success = args->context->copyLayerInto(args->layer, args->bitmap);
299    return (void*) success;
300}
301
302bool RenderProxy::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap& bitmap) {
303    SETUP_TASK(copyLayerInto);
304    args->context = mContext;
305    args->layer = layer;
306    args->bitmap = &bitmap;
307    return (bool) postAndWait(task);
308}
309
310void RenderProxy::pushLayerUpdate(DeferredLayerUpdater* layer) {
311    mDrawFrameTask.pushLayerUpdate(layer);
312}
313
314void RenderProxy::cancelLayerUpdate(DeferredLayerUpdater* layer) {
315    mDrawFrameTask.removeLayerUpdate(layer);
316}
317
318CREATE_BRIDGE1(detachSurfaceTexture, DeferredLayerUpdater* layer) {
319    args->layer->detachSurfaceTexture();
320    return nullptr;
321}
322
323void RenderProxy::detachSurfaceTexture(DeferredLayerUpdater* layer) {
324    SETUP_TASK(detachSurfaceTexture);
325    args->layer = layer;
326    postAndWait(task);
327}
328
329CREATE_BRIDGE1(destroyHardwareResources, CanvasContext* context) {
330    args->context->destroyHardwareResources();
331    return nullptr;
332}
333
334void RenderProxy::destroyHardwareResources() {
335    SETUP_TASK(destroyHardwareResources);
336    args->context = mContext;
337    postAndWait(task);
338}
339
340CREATE_BRIDGE2(trimMemory, RenderThread* thread, int level) {
341    CanvasContext::trimMemory(*args->thread, args->level);
342    return nullptr;
343}
344
345void RenderProxy::trimMemory(int level) {
346    // Avoid creating a RenderThread to do a trimMemory.
347    if (RenderThread::hasInstance()) {
348        RenderThread& thread = RenderThread::getInstance();
349        SETUP_TASK(trimMemory);
350        args->thread = &thread;
351        args->level = level;
352        thread.queue(task);
353    }
354}
355
356CREATE_BRIDGE2(overrideProperty, const char* name, const char* value) {
357    Properties::overrideProperty(args->name, args->value);
358    return nullptr;
359}
360
361void RenderProxy::overrideProperty(const char* name, const char* value) {
362    SETUP_TASK(overrideProperty);
363    args->name = name;
364    args->value = value;
365    staticPostAndWait(task); // expensive, but block here since name/value pointers owned by caller
366}
367
368CREATE_BRIDGE0(fence) {
369    // Intentionally empty
370    return nullptr;
371}
372
373template <typename T>
374void UNUSED(T t) {}
375
376void RenderProxy::fence() {
377    SETUP_TASK(fence);
378    UNUSED(args);
379    postAndWait(task);
380}
381
382void RenderProxy::staticFence() {
383    SETUP_TASK(fence);
384    UNUSED(args);
385    staticPostAndWait(task);
386}
387
388CREATE_BRIDGE1(stopDrawing, CanvasContext* context) {
389    args->context->stopDrawing();
390    return nullptr;
391}
392
393void RenderProxy::stopDrawing() {
394    SETUP_TASK(stopDrawing);
395    args->context = mContext;
396    postAndWait(task);
397}
398
399CREATE_BRIDGE1(notifyFramePending, CanvasContext* context) {
400    args->context->notifyFramePending();
401    return nullptr;
402}
403
404void RenderProxy::notifyFramePending() {
405    SETUP_TASK(notifyFramePending);
406    args->context = mContext;
407    mRenderThread.queueAtFront(task);
408}
409
410CREATE_BRIDGE4(dumpProfileInfo, CanvasContext* context, RenderThread* thread,
411        int fd, int dumpFlags) {
412    args->context->profiler().dumpData(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    if (args->dumpFlags & DumpFlags::JankStats) {
420        args->thread->jankTracker().dump(args->fd);
421    }
422    return nullptr;
423}
424
425void RenderProxy::dumpProfileInfo(int fd, int dumpFlags) {
426    SETUP_TASK(dumpProfileInfo);
427    args->context = mContext;
428    args->thread = &mRenderThread;
429    args->fd = fd;
430    args->dumpFlags = dumpFlags;
431    postAndWait(task);
432}
433
434CREATE_BRIDGE1(resetProfileInfo, CanvasContext* context) {
435    args->context->resetFrameStats();
436    return nullptr;
437}
438
439void RenderProxy::resetProfileInfo() {
440    SETUP_TASK(resetProfileInfo);
441    args->context = mContext;
442    postAndWait(task);
443}
444
445CREATE_BRIDGE2(frameTimePercentile, RenderThread* thread, int percentile) {
446    return reinterpret_cast<void*>(static_cast<uintptr_t>(
447        args->thread->jankTracker().findPercentile(args->percentile)));
448}
449
450uint32_t RenderProxy::frameTimePercentile(int p) {
451    SETUP_TASK(frameTimePercentile);
452    args->thread = &mRenderThread;
453    args->percentile = p;
454    return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
455        postAndWait(task)));
456}
457
458CREATE_BRIDGE2(dumpGraphicsMemory, int fd, RenderThread* thread) {
459    args->thread->jankTracker().dump(args->fd);
460
461    FILE *file = fdopen(args->fd, "a");
462    if (Caches::hasInstance()) {
463        String8 cachesLog;
464        Caches::getInstance().dumpMemoryUsage(cachesLog);
465        fprintf(file, "\nCaches:\n%s\n", cachesLog.string());
466    } else {
467        fprintf(file, "\nNo caches instance.\n");
468    }
469    fprintf(file, "\nPipeline=FrameBuilder\n");
470    fflush(file);
471    return nullptr;
472}
473
474void RenderProxy::dumpGraphicsMemory(int fd) {
475    if (!RenderThread::hasInstance()) return;
476    SETUP_TASK(dumpGraphicsMemory);
477    args->fd = fd;
478    args->thread = &RenderThread::getInstance();
479    staticPostAndWait(task);
480}
481
482CREATE_BRIDGE2(setProcessStatsBuffer, RenderThread* thread, int fd) {
483    args->thread->jankTracker().switchStorageToAshmem(args->fd);
484    close(args->fd);
485    return nullptr;
486}
487
488void RenderProxy::setProcessStatsBuffer(int fd) {
489    SETUP_TASK(setProcessStatsBuffer);
490    auto& rt = RenderThread::getInstance();
491    args->thread = &rt;
492    args->fd = dup(fd);
493    rt.queue(task);
494}
495
496CREATE_BRIDGE1(rotateProcessStatsBuffer, RenderThread* thread) {
497    args->thread->jankTracker().rotateStorage();
498    return nullptr;
499}
500
501void RenderProxy::rotateProcessStatsBuffer() {
502    SETUP_TASK(rotateProcessStatsBuffer);
503    auto& rt = RenderThread::getInstance();
504    args->thread = &rt;
505    rt.queue(task);
506}
507
508int RenderProxy::getRenderThreadTid() {
509    return mRenderThread.getTid();
510}
511
512CREATE_BRIDGE3(addRenderNode, CanvasContext* context, RenderNode* node, bool placeFront) {
513    args->context->addRenderNode(args->node, args->placeFront);
514    return nullptr;
515}
516
517void RenderProxy::addRenderNode(RenderNode* node, bool placeFront) {
518    SETUP_TASK(addRenderNode);
519    args->context = mContext;
520    args->node = node;
521    args->placeFront = placeFront;
522    post(task);
523}
524
525CREATE_BRIDGE2(removeRenderNode, CanvasContext* context, RenderNode* node) {
526    args->context->removeRenderNode(args->node);
527    return nullptr;
528}
529
530void RenderProxy::removeRenderNode(RenderNode* node) {
531    SETUP_TASK(removeRenderNode);
532    args->context = mContext;
533    args->node = node;
534    post(task);
535}
536
537CREATE_BRIDGE2(drawRenderNode, CanvasContext* context, RenderNode* node) {
538    args->context->prepareAndDraw(args->node);
539    return nullptr;
540}
541
542void RenderProxy::drawRenderNode(RenderNode* node) {
543    SETUP_TASK(drawRenderNode);
544    args->context = mContext;
545    args->node = node;
546    // Be pseudo-thread-safe and don't use any member variables
547    staticPostAndWait(task);
548}
549
550CREATE_BRIDGE5(setContentDrawBounds, CanvasContext* context, int left, int top,
551        int right, int bottom) {
552    args->context->setContentDrawBounds(args->left, args->top, args->right, args->bottom);
553    return nullptr;
554}
555
556void RenderProxy::setContentDrawBounds(int left, int top, int right, int bottom) {
557    SETUP_TASK(setContentDrawBounds);
558    args->context = mContext;
559    args->left = left;
560    args->top = top;
561    args->right = right;
562    args->bottom = bottom;
563    staticPostAndWait(task);
564}
565
566CREATE_BRIDGE1(serializeDisplayListTree, CanvasContext* context) {
567    args->context->serializeDisplayListTree();
568    return nullptr;
569}
570
571void RenderProxy::serializeDisplayListTree() {
572    SETUP_TASK(serializeDisplayListTree);
573    args->context = mContext;
574    post(task);
575}
576
577CREATE_BRIDGE2(addFrameMetricsObserver, CanvasContext* context,
578        FrameMetricsObserver* frameStatsObserver) {
579   args->context->addFrameMetricsObserver(args->frameStatsObserver);
580   if (args->frameStatsObserver != nullptr) {
581       args->frameStatsObserver->decStrong(args->context);
582   }
583   return nullptr;
584}
585
586void RenderProxy::addFrameMetricsObserver(FrameMetricsObserver* observer) {
587    SETUP_TASK(addFrameMetricsObserver);
588    args->context = mContext;
589    args->frameStatsObserver = observer;
590    if (observer != nullptr) {
591        observer->incStrong(mContext);
592    }
593    post(task);
594}
595
596CREATE_BRIDGE2(removeFrameMetricsObserver, CanvasContext* context,
597        FrameMetricsObserver* frameStatsObserver) {
598   args->context->removeFrameMetricsObserver(args->frameStatsObserver);
599   if (args->frameStatsObserver != nullptr) {
600       args->frameStatsObserver->decStrong(args->context);
601   }
602   return nullptr;
603}
604
605void RenderProxy::removeFrameMetricsObserver(FrameMetricsObserver* observer) {
606    SETUP_TASK(removeFrameMetricsObserver);
607    args->context = mContext;
608    args->frameStatsObserver = observer;
609    if (observer != nullptr) {
610        observer->incStrong(mContext);
611    }
612    post(task);
613}
614
615CREATE_BRIDGE4(copySurfaceInto, RenderThread* thread,
616        Surface* surface, Rect srcRect, SkBitmap* bitmap) {
617    return (void*)args->thread->readback().copySurfaceInto(*args->surface,
618            args->srcRect, args->bitmap);
619}
620
621int RenderProxy::copySurfaceInto(sp<Surface>& surface, int left, int top,
622        int right, int bottom,  SkBitmap* bitmap) {
623    SETUP_TASK(copySurfaceInto);
624    args->bitmap = bitmap;
625    args->surface = surface.get();
626    args->thread = &RenderThread::getInstance();
627    args->srcRect.set(left, top, right, bottom);
628    return static_cast<int>(
629            reinterpret_cast<intptr_t>( staticPostAndWait(task) ));
630}
631
632CREATE_BRIDGE2(prepareToDraw, RenderThread* thread, Bitmap* bitmap) {
633    CanvasContext::prepareToDraw(*args->thread, args->bitmap);
634    args->bitmap->unref();
635    args->bitmap = nullptr;
636    return nullptr;
637}
638
639void RenderProxy::prepareToDraw(Bitmap& bitmap) {
640    // If we haven't spun up a hardware accelerated window yet, there's no
641    // point in precaching these bitmaps as it can't impact jank.
642    // We also don't know if we even will spin up a hardware-accelerated
643    // window or not.
644    if (!RenderThread::hasInstance()) return;
645    RenderThread* renderThread = &RenderThread::getInstance();
646    SETUP_TASK(prepareToDraw);
647    args->thread = renderThread;
648    bitmap.ref();
649    args->bitmap = &bitmap;
650    nsecs_t lastVsync = renderThread->timeLord().latestVsync();
651    nsecs_t estimatedNextVsync = lastVsync + renderThread->timeLord().frameIntervalNanos();
652    nsecs_t timeToNextVsync = estimatedNextVsync - systemTime(CLOCK_MONOTONIC);
653    // We expect the UI thread to take 4ms and for RT to be active from VSYNC+4ms to
654    // VSYNC+12ms or so, so aim for the gap during which RT is expected to
655    // be idle
656    // TODO: Make this concept a first-class supported thing? RT could use
657    // knowledge of pending draws to better schedule this task
658    if (timeToNextVsync > -6_ms && timeToNextVsync < 1_ms) {
659        renderThread->queueAt(task, estimatedNextVsync + 8_ms);
660    } else {
661        renderThread->queue(task);
662    }
663}
664
665CREATE_BRIDGE2(allocateHardwareBitmap, RenderThread* thread, SkBitmap* bitmap) {
666    sk_sp<Bitmap> hardwareBitmap = Bitmap::allocateHardwareBitmap(*args->thread, *args->bitmap);
667    return hardwareBitmap.release();
668}
669
670sk_sp<Bitmap> RenderProxy::allocateHardwareBitmap(SkBitmap& bitmap) {
671    SETUP_TASK(allocateHardwareBitmap);
672    args->bitmap = &bitmap;
673    args->thread = &RenderThread::getInstance();
674    sk_sp<Bitmap> hardwareBitmap(reinterpret_cast<Bitmap*>(staticPostAndWait(task)));
675    return hardwareBitmap;
676}
677
678CREATE_BRIDGE3(copyGraphicBufferInto, RenderThread* thread, GraphicBuffer* buffer, SkBitmap* bitmap) {
679    return (void*) args->thread->readback().copyGraphicBufferInto(args->buffer, args->bitmap);
680}
681
682int RenderProxy::copyGraphicBufferInto(GraphicBuffer* buffer, SkBitmap* bitmap) {
683    RenderThread& thread = RenderThread::getInstance();
684    if (Properties::isSkiaEnabled() && gettid() == thread.getTid()) {
685        //TODO: fix everything that hits this. We should never be triggering a readback ourselves.
686        return (int) thread.readback().copyGraphicBufferInto(buffer, bitmap);
687    } else {
688        SETUP_TASK(copyGraphicBufferInto);
689        args->thread = &thread;
690        args->bitmap = bitmap;
691        args->buffer = buffer;
692        return static_cast<int>(reinterpret_cast<intptr_t>(staticPostAndWait(task)));
693    }
694}
695
696void RenderProxy::post(RenderTask* task) {
697    mRenderThread.queue(task);
698}
699
700void* RenderProxy::postAndWait(MethodInvokeRenderTask* task) {
701    void* retval;
702    task->setReturnPtr(&retval);
703    SignalingRenderTask syncTask(task, &mSyncMutex, &mSyncCondition);
704    AutoMutex _lock(mSyncMutex);
705    mRenderThread.queue(&syncTask);
706    while (!syncTask.hasRun()) {
707        mSyncCondition.wait(mSyncMutex);
708    }
709    return retval;
710}
711
712void* RenderProxy::staticPostAndWait(MethodInvokeRenderTask* task) {
713    RenderThread& thread = RenderThread::getInstance();
714    LOG_ALWAYS_FATAL_IF(gettid() == thread.getTid());
715    void* retval;
716    task->setReturnPtr(&retval);
717    thread.queueAndWait(task);
718    return retval;
719}
720
721} /* namespace renderthread */
722} /* namespace uirenderer */
723} /* namespace android */
724