14f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck/*
24f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck * Copyright (C) 2013 The Android Open Source Project
34f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck *
44f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck * Licensed under the Apache License, Version 2.0 (the "License");
54f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck * you may not use this file except in compliance with the License.
64f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck * You may obtain a copy of the License at
74f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck *
84f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck *      http://www.apache.org/licenses/LICENSE-2.0
94f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck *
104f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck * Unless required by applicable law or agreed to in writing, software
114f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck * distributed under the License is distributed on an "AS IS" BASIS,
124f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck * See the License for the specific language governing permissions and
144f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck * limitations under the License.
154f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck */
164f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
174f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck#include "RenderProxy.h"
184f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
19ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck#include "DeferredLayerUpdater.h"
20ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck#include "DisplayList.h"
21a896306c38c4d11c43b967a21db6d74c251b2520John Reck#include "Properties.h"
2210dd0585c11dcedb5a271d54e645594f1d215d5cJohn Reck#include "Readback.h"
23ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck#include "Rect.h"
24ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck#include "renderthread/CanvasContext.h"
254387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck#include "renderthread/EglManager.h"
26ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck#include "renderthread/RenderTask.h"
27ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck#include "renderthread/RenderThread.h"
289a814875c4e3a98fea99dae623f22268a9afa38aJohn Reck#include "renderstate/RenderState.h"
29ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck#include "utils/Macros.h"
304387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck#include "utils/TimeUtils.h"
314f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
3259eecb526adc5bd7041e7b6147bfcc40dd2c200esergeyv#include <ui/GraphicBuffer.h>
3359eecb526adc5bd7041e7b6147bfcc40dd2c200esergeyv
344f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Recknamespace android {
354f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Recknamespace uirenderer {
364f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Recknamespace renderthread {
374f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
384f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck#define ARGS(method) method ## Args
394f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
4019b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck#define CREATE_BRIDGE0(name) CREATE_BRIDGE(name,,,,,,,,)
414f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck#define CREATE_BRIDGE1(name, a1) CREATE_BRIDGE(name, a1,,,,,,,)
424f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck#define CREATE_BRIDGE2(name, a1, a2) CREATE_BRIDGE(name, a1,a2,,,,,,)
434f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck#define CREATE_BRIDGE3(name, a1, a2, a3) CREATE_BRIDGE(name, a1,a2,a3,,,,,)
444f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck#define CREATE_BRIDGE4(name, a1, a2, a3, a4) CREATE_BRIDGE(name, a1,a2,a3,a4,,,,)
45797b95b26bbb7557678af78b9a2a61830158920fChris Craik#define CREATE_BRIDGE5(name, a1, a2, a3, a4, a5) CREATE_BRIDGE(name, a1,a2,a3,a4,a5,,,)
46058fc640017c90120c599d378a4cbc55668b05b7Chris Craik#define CREATE_BRIDGE6(name, a1, a2, a3, a4, a5, a6) CREATE_BRIDGE(name, a1,a2,a3,a4,a5,a6,,)
47058fc640017c90120c599d378a4cbc55668b05b7Chris Craik#define CREATE_BRIDGE7(name, a1, a2, a3, a4, a5, a6, a7) CREATE_BRIDGE(name, a1,a2,a3,a4,a5,a6,a7,)
484f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck#define CREATE_BRIDGE(name, a1, a2, a3, a4, a5, a6, a7, a8) \
494f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    typedef struct { \
504f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck        a1; a2; a3; a4; a5; a6; a7; a8; \
514f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    } ARGS(name); \
524387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck    static_assert(std::is_trivially_destructible<ARGS(name)>::value, \
534387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck            "Error, ARGS must be trivially destructible!"); \
544f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    static void* Bridge_ ## name(ARGS(name)* args)
554f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
564f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck#define SETUP_TASK(method) \
574f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    LOG_ALWAYS_FATAL_IF( METHOD_INVOKE_PAYLOAD_SIZE < sizeof(ARGS(method)), \
58546f353e7f562fdbcf59980bcb7dc11567658aa3Mark Salyzyn        "METHOD_INVOKE_PAYLOAD_SIZE %zu is smaller than sizeof(" #method "Args) %zu", \
594f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck                METHOD_INVOKE_PAYLOAD_SIZE, sizeof(ARGS(method))); \
60e2c455264351964bf1ae78da2256c17258f0d3eaJohn Reck    MethodInvokeRenderTask* task = new MethodInvokeRenderTask((RunnableMethod) Bridge_ ## method); \
614f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    ARGS(method) *args = (ARGS(method) *) task->payload()
624f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
63119907cd2575c56b1ebf66348b52e67aaf6a88d8John ReckCREATE_BRIDGE4(createContext, RenderThread* thread, bool translucent,
64119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck        RenderNode* rootRenderNode, IContextFactory* contextFactory) {
6503de074d05108fa9fb07c6b847c7163ada5776daStan Iliev    return CanvasContext::create(*args->thread, args->translucent,
66119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck            args->rootRenderNode, args->contextFactory);
674f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
684f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
69119907cd2575c56b1ebf66348b52e67aaf6a88d8John ReckRenderProxy::RenderProxy(bool translucent, RenderNode* rootRenderNode, IContextFactory* contextFactory)
704f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck        : mRenderThread(RenderThread::getInstance())
71d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik        , mContext(nullptr) {
724f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    SETUP_TASK(createContext);
734f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    args->translucent = translucent;
74e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck    args->rootRenderNode = rootRenderNode;
753b20251a355c88193c439f928a84ae69483fb488John Reck    args->thread = &mRenderThread;
76119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    args->contextFactory = contextFactory;
774f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    mContext = (CanvasContext*) postAndWait(task);
78ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    mDrawFrameTask.setContext(&mRenderThread, mContext, rootRenderNode);
794f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
804f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
814f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John ReckRenderProxy::~RenderProxy() {
824f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    destroyContext();
834f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
844f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
854f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John ReckCREATE_BRIDGE1(destroyContext, CanvasContext* context) {
864f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    delete args->context;
87d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    return nullptr;
884f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
894f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
904f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reckvoid RenderProxy::destroyContext() {
914f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    if (mContext) {
924f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck        SETUP_TASK(destroyContext);
934f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck        args->context = mContext;
94d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik        mContext = nullptr;
95ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne        mDrawFrameTask.setContext(nullptr, nullptr, nullptr);
96668f0e38ef0277d55d3118af37e17b8c435df85cJohn Reck        // This is also a fence as we need to be certain that there are no
97668f0e38ef0277d55d3118af37e17b8c435df85cJohn Reck        // outstanding mDrawFrame tasks posted before it is destroyed
98668f0e38ef0277d55d3118af37e17b8c435df85cJohn Reck        postAndWait(task);
994f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    }
1004f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
1014f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
1021125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn ReckCREATE_BRIDGE2(setSwapBehavior, CanvasContext* context, SwapBehavior swapBehavior) {
1031125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck    args->context->setSwapBehavior(args->swapBehavior);
104d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    return nullptr;
1051125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck}
1061125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck
1071125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reckvoid RenderProxy::setSwapBehavior(SwapBehavior swapBehavior) {
1081125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck    SETUP_TASK(setSwapBehavior);
1091125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck    args->context = mContext;
1101125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck    args->swapBehavior = swapBehavior;
1111125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck    post(task);
1121125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck}
1131125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck
114fe5e7b7346a54537b980796ceeca66bfdbd05561John ReckCREATE_BRIDGE1(loadSystemProperties, CanvasContext* context) {
115e4280baaa709c74d86cf6a389a4674ca665f5af6John Reck    bool needsRedraw = false;
116e4280baaa709c74d86cf6a389a4674ca665f5af6John Reck    if (Caches::hasInstance()) {
1172507c34d91bb0d722b6012e85cb47387b2aa6873Chris Craik        needsRedraw = Properties::load();
118e4280baaa709c74d86cf6a389a4674ca665f5af6John Reck    }
1192507c34d91bb0d722b6012e85cb47387b2aa6873Chris Craik    if (args->context->profiler().consumeProperties()) {
120fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck        needsRedraw = true;
121fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck    }
122e4280baaa709c74d86cf6a389a4674ca665f5af6John Reck    return (void*) needsRedraw;
123e4280baaa709c74d86cf6a389a4674ca665f5af6John Reck}
124e4280baaa709c74d86cf6a389a4674ca665f5af6John Reck
125e4280baaa709c74d86cf6a389a4674ca665f5af6John Reckbool RenderProxy::loadSystemProperties() {
126e4280baaa709c74d86cf6a389a4674ca665f5af6John Reck    SETUP_TASK(loadSystemProperties);
127fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck    args->context = mContext;
128e4280baaa709c74d86cf6a389a4674ca665f5af6John Reck    return (bool) postAndWait(task);
129e4280baaa709c74d86cf6a389a4674ca665f5af6John Reck}
130e4280baaa709c74d86cf6a389a4674ca665f5af6John Reck
131b36016c65f1d1b5846dba0349aab491dbd3a746aJohn ReckCREATE_BRIDGE2(setName, CanvasContext* context, const char* name) {
132b36016c65f1d1b5846dba0349aab491dbd3a746aJohn Reck    args->context->setName(std::string(args->name));
133b36016c65f1d1b5846dba0349aab491dbd3a746aJohn Reck    return nullptr;
134b36016c65f1d1b5846dba0349aab491dbd3a746aJohn Reck}
135b36016c65f1d1b5846dba0349aab491dbd3a746aJohn Reck
136b36016c65f1d1b5846dba0349aab491dbd3a746aJohn Reckvoid RenderProxy::setName(const char* name) {
137b36016c65f1d1b5846dba0349aab491dbd3a746aJohn Reck    SETUP_TASK(setName);
138b36016c65f1d1b5846dba0349aab491dbd3a746aJohn Reck    args->context = mContext;
139b36016c65f1d1b5846dba0349aab491dbd3a746aJohn Reck    args->name = name;
1402507c34d91bb0d722b6012e85cb47387b2aa6873Chris Craik    postAndWait(task); // block since name/value pointers owned by caller
141b36016c65f1d1b5846dba0349aab491dbd3a746aJohn Reck}
142b36016c65f1d1b5846dba0349aab491dbd3a746aJohn Reck
143f648108f83d4e74811919e9811efb8fcc184b8a3John ReckCREATE_BRIDGE2(initialize, CanvasContext* context, Surface* surface) {
144f648108f83d4e74811919e9811efb8fcc184b8a3John Reck    args->context->initialize(args->surface);
1450bcd0cb6b1193168fa2840855195347488daab9eThomas Buhot    return nullptr;
1464f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
1474f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
148f648108f83d4e74811919e9811efb8fcc184b8a3John Reckvoid RenderProxy::initialize(const sp<Surface>& surface) {
1494f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    SETUP_TASK(initialize);
1504f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    args->context = mContext;
151f648108f83d4e74811919e9811efb8fcc184b8a3John Reck    args->surface = surface.get();
1520bcd0cb6b1193168fa2840855195347488daab9eThomas Buhot    post(task);
1534f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
1544f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
155f648108f83d4e74811919e9811efb8fcc184b8a3John ReckCREATE_BRIDGE2(updateSurface, CanvasContext* context, Surface* surface) {
156f648108f83d4e74811919e9811efb8fcc184b8a3John Reck    args->context->updateSurface(args->surface);
157d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    return nullptr;
1584f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
1594f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
160f648108f83d4e74811919e9811efb8fcc184b8a3John Reckvoid RenderProxy::updateSurface(const sp<Surface>& surface) {
1614f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    SETUP_TASK(updateSurface);
1624f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    args->context = mContext;
163f648108f83d4e74811919e9811efb8fcc184b8a3John Reck    args->surface = surface.get();
164cd68212f0821e75081375a5af5ef5fce6e45e167John Reck    post(task);
165f7d9c1dc84671d4e99657ef071d275700d85bb11John Reck}
166f7d9c1dc84671d4e99657ef071d275700d85bb11John Reck
167f648108f83d4e74811919e9811efb8fcc184b8a3John ReckCREATE_BRIDGE2(pauseSurface, CanvasContext* context, Surface* surface) {
168f648108f83d4e74811919e9811efb8fcc184b8a3John Reck    return (void*) args->context->pauseSurface(args->surface);
169f7d9c1dc84671d4e99657ef071d275700d85bb11John Reck}
170f7d9c1dc84671d4e99657ef071d275700d85bb11John Reck
171f648108f83d4e74811919e9811efb8fcc184b8a3John Reckbool RenderProxy::pauseSurface(const sp<Surface>& surface) {
172f7d9c1dc84671d4e99657ef071d275700d85bb11John Reck    SETUP_TASK(pauseSurface);
173f7d9c1dc84671d4e99657ef071d275700d85bb11John Reck    args->context = mContext;
174f648108f83d4e74811919e9811efb8fcc184b8a3John Reck    args->surface = surface.get();
17501a5ea35fbba4c5bb1d7790ae1677a2fa752e042John Reck    return (bool) postAndWait(task);
1764f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
1774f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
1788afcc76920499d0a384dba1470c5a377f80ed768John ReckCREATE_BRIDGE2(setStopped, CanvasContext* context, bool stopped) {
1798afcc76920499d0a384dba1470c5a377f80ed768John Reck    args->context->setStopped(args->stopped);
1808afcc76920499d0a384dba1470c5a377f80ed768John Reck    return nullptr;
1818afcc76920499d0a384dba1470c5a377f80ed768John Reck}
1828afcc76920499d0a384dba1470c5a377f80ed768John Reck
1838afcc76920499d0a384dba1470c5a377f80ed768John Reckvoid RenderProxy::setStopped(bool stopped) {
1848afcc76920499d0a384dba1470c5a377f80ed768John Reck    SETUP_TASK(setStopped);
1858afcc76920499d0a384dba1470c5a377f80ed768John Reck    args->context = mContext;
1868afcc76920499d0a384dba1470c5a377f80ed768John Reck    args->stopped = stopped;
1878afcc76920499d0a384dba1470c5a377f80ed768John Reck    postAndWait(task);
1888afcc76920499d0a384dba1470c5a377f80ed768John Reck}
1898afcc76920499d0a384dba1470c5a377f80ed768John Reck
190ab1080c4d075b008cebdd9a2031ebbd51f9c2729John ReckCREATE_BRIDGE4(setup, CanvasContext* context,
19150210d912925aef14e4ce69be82e4949122a3cd9Alan Viverette        float lightRadius, uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
192ab1080c4d075b008cebdd9a2031ebbd51f9c2729John Reck    args->context->setup(args->lightRadius,
193058fc640017c90120c599d378a4cbc55668b05b7Chris Craik            args->ambientShadowAlpha, args->spotShadowAlpha);
194d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    return nullptr;
1954f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
1964f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
197ab1080c4d075b008cebdd9a2031ebbd51f9c2729John Reckvoid RenderProxy::setup(float lightRadius,
198b36016c65f1d1b5846dba0349aab491dbd3a746aJohn Reck        uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
1994f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    SETUP_TASK(setup);
2004f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    args->context = mContext;
201797b95b26bbb7557678af78b9a2a61830158920fChris Craik    args->lightRadius = lightRadius;
202058fc640017c90120c599d378a4cbc55668b05b7Chris Craik    args->ambientShadowAlpha = ambientShadowAlpha;
203058fc640017c90120c599d378a4cbc55668b05b7Chris Craik    args->spotShadowAlpha = spotShadowAlpha;
2044f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    post(task);
2054f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
2064f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
20750210d912925aef14e4ce69be82e4949122a3cd9Alan ViveretteCREATE_BRIDGE2(setLightCenter, CanvasContext* context, Vector3 lightCenter) {
20850210d912925aef14e4ce69be82e4949122a3cd9Alan Viverette    args->context->setLightCenter(args->lightCenter);
20950210d912925aef14e4ce69be82e4949122a3cd9Alan Viverette    return nullptr;
21050210d912925aef14e4ce69be82e4949122a3cd9Alan Viverette}
21150210d912925aef14e4ce69be82e4949122a3cd9Alan Viverette
21250210d912925aef14e4ce69be82e4949122a3cd9Alan Viverettevoid RenderProxy::setLightCenter(const Vector3& lightCenter) {
21350210d912925aef14e4ce69be82e4949122a3cd9Alan Viverette    SETUP_TASK(setLightCenter);
21450210d912925aef14e4ce69be82e4949122a3cd9Alan Viverette    args->context = mContext;
21550210d912925aef14e4ce69be82e4949122a3cd9Alan Viverette    args->lightCenter = lightCenter;
21650210d912925aef14e4ce69be82e4949122a3cd9Alan Viverette    post(task);
21750210d912925aef14e4ce69be82e4949122a3cd9Alan Viverette}
21850210d912925aef14e4ce69be82e4949122a3cd9Alan Viverette
21963a06673253914510bbeebd500655008682dade1John ReckCREATE_BRIDGE2(setOpaque, CanvasContext* context, bool opaque) {
22063a06673253914510bbeebd500655008682dade1John Reck    args->context->setOpaque(args->opaque);
221d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    return nullptr;
22263a06673253914510bbeebd500655008682dade1John Reck}
22363a06673253914510bbeebd500655008682dade1John Reck
22463a06673253914510bbeebd500655008682dade1John Reckvoid RenderProxy::setOpaque(bool opaque) {
22563a06673253914510bbeebd500655008682dade1John Reck    SETUP_TASK(setOpaque);
22663a06673253914510bbeebd500655008682dade1John Reck    args->context = mContext;
22763a06673253914510bbeebd500655008682dade1John Reck    args->opaque = opaque;
22863a06673253914510bbeebd500655008682dade1John Reck    post(task);
22963a06673253914510bbeebd500655008682dade1John Reck}
23063a06673253914510bbeebd500655008682dade1John Reck
231ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reckint64_t* RenderProxy::frameInfo() {
232ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    return mDrawFrameTask.frameInfo();
233ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck}
234ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck
2352de950d5a8b47c7b4648ada1b1260ce4b7342798John Reckint RenderProxy::syncAndDrawFrame() {
2362de950d5a8b47c7b4648ada1b1260ce4b7342798John Reck    return mDrawFrameTask.drawFrame();
2374f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
2384f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
2392de950d5a8b47c7b4648ada1b1260ce4b7342798John ReckCREATE_BRIDGE1(destroy, CanvasContext* context) {
2402de950d5a8b47c7b4648ada1b1260ce4b7342798John Reck    args->context->destroy();
241d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    return nullptr;
2424f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
2434f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
2442de950d5a8b47c7b4648ada1b1260ce4b7342798John Reckvoid RenderProxy::destroy() {
24517035b0211a3c9d45ea46a99217a6acbe76e8fbeJohn Reck    SETUP_TASK(destroy);
2464f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    args->context = mContext;
247fae904d63947fe1687d1d44be29234cc3d538f24John Reck    // destroyCanvasAndSurface() needs a fence as when it returns the
248fae904d63947fe1687d1d44be29234cc3d538f24John Reck    // underlying BufferQueue is going to be released from under
249fae904d63947fe1687d1d44be29234cc3d538f24John Reck    // the render thread.
250fae904d63947fe1687d1d44be29234cc3d538f24John Reck    postAndWait(task);
2514f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
2524f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
2533b20251a355c88193c439f928a84ae69483fb488John ReckCREATE_BRIDGE2(invokeFunctor, RenderThread* thread, Functor* functor) {
2543b20251a355c88193c439f928a84ae69483fb488John Reck    CanvasContext::invokeFunctor(*args->thread, args->functor);
255d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    return nullptr;
2560d1f634f4b5e1bb37aa51777efb6a68619488d01John Reck}
2570d1f634f4b5e1bb37aa51777efb6a68619488d01John Reck
2580d1f634f4b5e1bb37aa51777efb6a68619488d01John Reckvoid RenderProxy::invokeFunctor(Functor* functor, bool waitForCompletion) {
259d3d8dafc2f61fb118c060720b52684c59303f3dbJohn Reck    ATRACE_CALL();
2603b20251a355c88193c439f928a84ae69483fb488John Reck    RenderThread& thread = RenderThread::getInstance();
2610d1f634f4b5e1bb37aa51777efb6a68619488d01John Reck    SETUP_TASK(invokeFunctor);
2623b20251a355c88193c439f928a84ae69483fb488John Reck    args->thread = &thread;
2630d1f634f4b5e1bb37aa51777efb6a68619488d01John Reck    args->functor = functor;
2640d1f634f4b5e1bb37aa51777efb6a68619488d01John Reck    if (waitForCompletion) {
2653b20251a355c88193c439f928a84ae69483fb488John Reck        // waitForCompletion = true is expected to be fairly rare and only
2663b20251a355c88193c439f928a84ae69483fb488John Reck        // happen in destruction. Thus it should be fine to temporarily
2673b20251a355c88193c439f928a84ae69483fb488John Reck        // create a Mutex
2680e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck        staticPostAndWait(task);
2690d1f634f4b5e1bb37aa51777efb6a68619488d01John Reck    } else {
2703b20251a355c88193c439f928a84ae69483fb488John Reck        thread.queue(task);
2710d1f634f4b5e1bb37aa51777efb6a68619488d01John Reck    }
2720d1f634f4b5e1bb37aa51777efb6a68619488d01John Reck}
2730d1f634f4b5e1bb37aa51777efb6a68619488d01John Reck
274c36df952292b69920d4764a8a37361073fcf4f2cJohn ReckCREATE_BRIDGE1(createTextureLayer, CanvasContext* context) {
27556ad6ec42f814e9e61030ff819cac4e5d31def8bDerek Sollenberger    return args->context->createTextureLayer();
27619b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck}
27719b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck
27819b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John ReckDeferredLayerUpdater* RenderProxy::createTextureLayer() {
27919b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck    SETUP_TASK(createTextureLayer);
2801949e7928eeec22cd3f74b5f763a4eb433238453John Reck    args->context = mContext;
28119b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck    void* retval = postAndWait(task);
28219b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck    DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval);
28319b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck    return layer;
28419b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck}
28519b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck
2862de950d5a8b47c7b4648ada1b1260ce4b7342798John ReckCREATE_BRIDGE2(buildLayer, CanvasContext* context, RenderNode* node) {
2872de950d5a8b47c7b4648ada1b1260ce4b7342798John Reck    args->context->buildLayer(args->node);
288d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    return nullptr;
2893e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck}
2903e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck
2912de950d5a8b47c7b4648ada1b1260ce4b7342798John Reckvoid RenderProxy::buildLayer(RenderNode* node) {
2923e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    SETUP_TASK(buildLayer);
2933e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    args->context = mContext;
2943e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    args->node = node;
2953e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    postAndWait(task);
2963e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck}
2973e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck
29819b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John ReckCREATE_BRIDGE3(copyLayerInto, CanvasContext* context, DeferredLayerUpdater* layer,
29919b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck        SkBitmap* bitmap) {
30019b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck    bool success = args->context->copyLayerInto(args->layer, args->bitmap);
30119b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck    return (void*) success;
30219b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck}
30319b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck
3043731dc220ed457e0f1e99d7ec2589e0a43872b59John Reckbool RenderProxy::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap& bitmap) {
30519b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck    SETUP_TASK(copyLayerInto);
30619b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck    args->context = mContext;
30719b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck    args->layer = layer;
3083731dc220ed457e0f1e99d7ec2589e0a43872b59John Reck    args->bitmap = &bitmap;
30919b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck    return (bool) postAndWait(task);
31019b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck}
31119b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck
312d72e0a339b54af0c4e731513bbad120dff694723John Reckvoid RenderProxy::pushLayerUpdate(DeferredLayerUpdater* layer) {
313d72e0a339b54af0c4e731513bbad120dff694723John Reck    mDrawFrameTask.pushLayerUpdate(layer);
314d72e0a339b54af0c4e731513bbad120dff694723John Reck}
315d72e0a339b54af0c4e731513bbad120dff694723John Reck
316d72e0a339b54af0c4e731513bbad120dff694723John Reckvoid RenderProxy::cancelLayerUpdate(DeferredLayerUpdater* layer) {
317d72e0a339b54af0c4e731513bbad120dff694723John Reck    mDrawFrameTask.removeLayerUpdate(layer);
318918ad523b2780e0c893f3d2a32d4ec13f2a7e921John Reck}
319918ad523b2780e0c893f3d2a32d4ec13f2a7e921John Reck
320918ad523b2780e0c893f3d2a32d4ec13f2a7e921John ReckCREATE_BRIDGE1(detachSurfaceTexture, DeferredLayerUpdater* layer) {
321918ad523b2780e0c893f3d2a32d4ec13f2a7e921John Reck    args->layer->detachSurfaceTexture();
322d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    return nullptr;
323918ad523b2780e0c893f3d2a32d4ec13f2a7e921John Reck}
324918ad523b2780e0c893f3d2a32d4ec13f2a7e921John Reck
325918ad523b2780e0c893f3d2a32d4ec13f2a7e921John Reckvoid RenderProxy::detachSurfaceTexture(DeferredLayerUpdater* layer) {
326918ad523b2780e0c893f3d2a32d4ec13f2a7e921John Reck    SETUP_TASK(detachSurfaceTexture);
327918ad523b2780e0c893f3d2a32d4ec13f2a7e921John Reck    args->layer = layer;
328918ad523b2780e0c893f3d2a32d4ec13f2a7e921John Reck    postAndWait(task);
32919b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck}
33019b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck
3312de950d5a8b47c7b4648ada1b1260ce4b7342798John ReckCREATE_BRIDGE1(destroyHardwareResources, CanvasContext* context) {
3322de950d5a8b47c7b4648ada1b1260ce4b7342798John Reck    args->context->destroyHardwareResources();
333d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    return nullptr;
334e1628b7c6fc3822fa83cf02028ce8ad67abb0afeJohn Reck}
335e1628b7c6fc3822fa83cf02028ce8ad67abb0afeJohn Reck
3362de950d5a8b47c7b4648ada1b1260ce4b7342798John Reckvoid RenderProxy::destroyHardwareResources() {
337f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck    SETUP_TASK(destroyHardwareResources);
338e1628b7c6fc3822fa83cf02028ce8ad67abb0afeJohn Reck    args->context = mContext;
33951f2d606dcbfba3cc5b03dfea37c1304b91c232fJohn Reck    postAndWait(task);
340e1628b7c6fc3822fa83cf02028ce8ad67abb0afeJohn Reck}
341e1628b7c6fc3822fa83cf02028ce8ad67abb0afeJohn Reck
3422507c34d91bb0d722b6012e85cb47387b2aa6873Chris CraikCREATE_BRIDGE2(trimMemory, RenderThread* thread, int level) {
343f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck    CanvasContext::trimMemory(*args->thread, args->level);
344d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    return nullptr;
345f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck}
346f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck
347f47a594f5250b1914c36423ee6b371f0b8db09d0John Reckvoid RenderProxy::trimMemory(int level) {
348cd3a22cfec09c065d0667dd044c0788912e82465John Reck    // Avoid creating a RenderThread to do a trimMemory.
349cd3a22cfec09c065d0667dd044c0788912e82465John Reck    if (RenderThread::hasInstance()) {
350cd3a22cfec09c065d0667dd044c0788912e82465John Reck        RenderThread& thread = RenderThread::getInstance();
3512507c34d91bb0d722b6012e85cb47387b2aa6873Chris Craik        SETUP_TASK(trimMemory);
352cd3a22cfec09c065d0667dd044c0788912e82465John Reck        args->thread = &thread;
353cd3a22cfec09c065d0667dd044c0788912e82465John Reck        args->level = level;
354cd3a22cfec09c065d0667dd044c0788912e82465John Reck        thread.queue(task);
355cd3a22cfec09c065d0667dd044c0788912e82465John Reck    }
356f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck}
357f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck
3582507c34d91bb0d722b6012e85cb47387b2aa6873Chris CraikCREATE_BRIDGE2(overrideProperty, const char* name, const char* value) {
3592507c34d91bb0d722b6012e85cb47387b2aa6873Chris Craik    Properties::overrideProperty(args->name, args->value);
3602507c34d91bb0d722b6012e85cb47387b2aa6873Chris Craik    return nullptr;
3612507c34d91bb0d722b6012e85cb47387b2aa6873Chris Craik}
3622507c34d91bb0d722b6012e85cb47387b2aa6873Chris Craik
3632507c34d91bb0d722b6012e85cb47387b2aa6873Chris Craikvoid RenderProxy::overrideProperty(const char* name, const char* value) {
3642507c34d91bb0d722b6012e85cb47387b2aa6873Chris Craik    SETUP_TASK(overrideProperty);
3652507c34d91bb0d722b6012e85cb47387b2aa6873Chris Craik    args->name = name;
3662507c34d91bb0d722b6012e85cb47387b2aa6873Chris Craik    args->value = value;
3672507c34d91bb0d722b6012e85cb47387b2aa6873Chris Craik    staticPostAndWait(task); // expensive, but block here since name/value pointers owned by caller
3682507c34d91bb0d722b6012e85cb47387b2aa6873Chris Craik}
3692507c34d91bb0d722b6012e85cb47387b2aa6873Chris Craik
37028ad7b52e038ef0cdd89f753d9839444a434b299John ReckCREATE_BRIDGE0(fence) {
37128ad7b52e038ef0cdd89f753d9839444a434b299John Reck    // Intentionally empty
372d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    return nullptr;
37328ad7b52e038ef0cdd89f753d9839444a434b299John Reck}
37428ad7b52e038ef0cdd89f753d9839444a434b299John Reck
37564bb413a664001c95c8439cf097dc3033f4ed733Andreas Gampetemplate <typename T>
37664bb413a664001c95c8439cf097dc3033f4ed733Andreas Gampevoid UNUSED(T t) {}
37764bb413a664001c95c8439cf097dc3033f4ed733Andreas Gampe
37828ad7b52e038ef0cdd89f753d9839444a434b299John Reckvoid RenderProxy::fence() {
37928ad7b52e038ef0cdd89f753d9839444a434b299John Reck    SETUP_TASK(fence);
3801e19674107e1aa2224c2b8c7d12bfa057efe80eaAndreas Gampe    UNUSED(args);
38128ad7b52e038ef0cdd89f753d9839444a434b299John Reck    postAndWait(task);
38228ad7b52e038ef0cdd89f753d9839444a434b299John Reck}
38328ad7b52e038ef0cdd89f753d9839444a434b299John Reck
384c0a0e1a66da20a18045d59451b59ec32685bcf18Thomas Buhotvoid RenderProxy::staticFence() {
385c0a0e1a66da20a18045d59451b59ec32685bcf18Thomas Buhot    SETUP_TASK(fence);
386c0a0e1a66da20a18045d59451b59ec32685bcf18Thomas Buhot    UNUSED(args);
387c0a0e1a66da20a18045d59451b59ec32685bcf18Thomas Buhot    staticPostAndWait(task);
388c0a0e1a66da20a18045d59451b59ec32685bcf18Thomas Buhot}
389c0a0e1a66da20a18045d59451b59ec32685bcf18Thomas Buhot
390f47a594f5250b1914c36423ee6b371f0b8db09d0John ReckCREATE_BRIDGE1(stopDrawing, CanvasContext* context) {
391f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck    args->context->stopDrawing();
392d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    return nullptr;
393f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck}
394f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck
395f47a594f5250b1914c36423ee6b371f0b8db09d0John Reckvoid RenderProxy::stopDrawing() {
396f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck    SETUP_TASK(stopDrawing);
397f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck    args->context = mContext;
398f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck    postAndWait(task);
399f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck}
400f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck
401a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn ReckCREATE_BRIDGE1(notifyFramePending, CanvasContext* context) {
402a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    args->context->notifyFramePending();
403d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    return nullptr;
404a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck}
405a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck
406a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reckvoid RenderProxy::notifyFramePending() {
407a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    SETUP_TASK(notifyFramePending);
408a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    args->context = mContext;
409a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    mRenderThread.queueAtFront(task);
410a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck}
411a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck
4127f2e5e3cea6af1f1dff35842aa13d46c47315b91John ReckCREATE_BRIDGE4(dumpProfileInfo, CanvasContext* context, RenderThread* thread,
4137f2e5e3cea6af1f1dff35842aa13d46c47315b91John Reck        int fd, int dumpFlags) {
414fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck    args->context->profiler().dumpData(args->fd);
41553e51e4aa933f9603587e1780f446c18816bf9beChris Craik    if (args->dumpFlags & DumpFlags::FrameStats) {
416ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck        args->context->dumpFrames(args->fd);
417ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    }
41853e51e4aa933f9603587e1780f446c18816bf9beChris Craik    if (args->dumpFlags & DumpFlags::Reset) {
419ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck        args->context->resetFrameStats();
420ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    }
421a41f2445156fe0bcfde6bd4d813dfc43796a526dJohn Reck    if (args->dumpFlags & DumpFlags::JankStats) {
422a41f2445156fe0bcfde6bd4d813dfc43796a526dJohn Reck        args->thread->jankTracker().dump(args->fd);
423a41f2445156fe0bcfde6bd4d813dfc43796a526dJohn Reck    }
424d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    return nullptr;
425fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck}
426fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck
427ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reckvoid RenderProxy::dumpProfileInfo(int fd, int dumpFlags) {
428fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck    SETUP_TASK(dumpProfileInfo);
429fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck    args->context = mContext;
4307f2e5e3cea6af1f1dff35842aa13d46c47315b91John Reck    args->thread = &mRenderThread;
431fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck    args->fd = fd;
432ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    args->dumpFlags = dumpFlags;
433fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck    postAndWait(task);
434fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck}
435fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck
4367f2e5e3cea6af1f1dff35842aa13d46c47315b91John ReckCREATE_BRIDGE1(resetProfileInfo, CanvasContext* context) {
4377f2e5e3cea6af1f1dff35842aa13d46c47315b91John Reck    args->context->resetFrameStats();
4387f2e5e3cea6af1f1dff35842aa13d46c47315b91John Reck    return nullptr;
4397f2e5e3cea6af1f1dff35842aa13d46c47315b91John Reck}
4407f2e5e3cea6af1f1dff35842aa13d46c47315b91John Reck
4417f2e5e3cea6af1f1dff35842aa13d46c47315b91John Reckvoid RenderProxy::resetProfileInfo() {
4427f2e5e3cea6af1f1dff35842aa13d46c47315b91John Reck    SETUP_TASK(resetProfileInfo);
4437f2e5e3cea6af1f1dff35842aa13d46c47315b91John Reck    args->context = mContext;
4447f2e5e3cea6af1f1dff35842aa13d46c47315b91John Reck    postAndWait(task);
4457f2e5e3cea6af1f1dff35842aa13d46c47315b91John Reck}
4467f2e5e3cea6af1f1dff35842aa13d46c47315b91John Reck
447f1480761c1a83aecd09cdd473ec797a41d1a2f3fJohn ReckCREATE_BRIDGE2(frameTimePercentile, RenderThread* thread, int percentile) {
448f1480761c1a83aecd09cdd473ec797a41d1a2f3fJohn Reck    return reinterpret_cast<void*>(static_cast<uintptr_t>(
449f1480761c1a83aecd09cdd473ec797a41d1a2f3fJohn Reck        args->thread->jankTracker().findPercentile(args->percentile)));
450f1480761c1a83aecd09cdd473ec797a41d1a2f3fJohn Reck}
451f1480761c1a83aecd09cdd473ec797a41d1a2f3fJohn Reck
452f1480761c1a83aecd09cdd473ec797a41d1a2f3fJohn Reckuint32_t RenderProxy::frameTimePercentile(int p) {
453f1480761c1a83aecd09cdd473ec797a41d1a2f3fJohn Reck    SETUP_TASK(frameTimePercentile);
454f1480761c1a83aecd09cdd473ec797a41d1a2f3fJohn Reck    args->thread = &mRenderThread;
455f1480761c1a83aecd09cdd473ec797a41d1a2f3fJohn Reck    args->percentile = p;
456f1480761c1a83aecd09cdd473ec797a41d1a2f3fJohn Reck    return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
457f1480761c1a83aecd09cdd473ec797a41d1a2f3fJohn Reck        postAndWait(task)));
458f1480761c1a83aecd09cdd473ec797a41d1a2f3fJohn Reck}
459f1480761c1a83aecd09cdd473ec797a41d1a2f3fJohn Reck
460ba6adf66d3c44c0aa2fd8a224862ff1901d64300John ReckCREATE_BRIDGE2(dumpGraphicsMemory, int fd, RenderThread* thread) {
461ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    args->thread->jankTracker().dump(args->fd);
462ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck
4632ae07339824efc50468f971ff8920ff0b894cad4Chris Craik    FILE *file = fdopen(args->fd, "a");
4642ae07339824efc50468f971ff8920ff0b894cad4Chris Craik    if (Caches::hasInstance()) {
4652ae07339824efc50468f971ff8920ff0b894cad4Chris Craik        String8 cachesLog;
4662ae07339824efc50468f971ff8920ff0b894cad4Chris Craik        Caches::getInstance().dumpMemoryUsage(cachesLog);
4672ae07339824efc50468f971ff8920ff0b894cad4Chris Craik        fprintf(file, "\nCaches:\n%s\n", cachesLog.string());
4682ae07339824efc50468f971ff8920ff0b894cad4Chris Craik    } else {
4692ae07339824efc50468f971ff8920ff0b894cad4Chris Craik        fprintf(file, "\nNo caches instance.\n");
4702ae07339824efc50468f971ff8920ff0b894cad4Chris Craik    }
471ff3edce12d3081517e9a724cc18a0de58af5187aChris Craik    fprintf(file, "\nPipeline=FrameBuilder\n");
4722ae07339824efc50468f971ff8920ff0b894cad4Chris Craik    fflush(file);
473d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    return nullptr;
4740e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck}
4750e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck
4762ae07339824efc50468f971ff8920ff0b894cad4Chris Craikvoid RenderProxy::dumpGraphicsMemory(int fd) {
477c80c9ad188ac6c421e70c906104d4394504878fbyoungmin    if (!RenderThread::hasInstance()) return;
4782ae07339824efc50468f971ff8920ff0b894cad4Chris Craik    SETUP_TASK(dumpGraphicsMemory);
4790e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck    args->fd = fd;
480ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    args->thread = &RenderThread::getInstance();
4810e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck    staticPostAndWait(task);
4820e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck}
4830e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck
484edc524c90506d80e0fc5fb67e8de7b8f3ef53439John ReckCREATE_BRIDGE2(setProcessStatsBuffer, RenderThread* thread, int fd) {
485edc524c90506d80e0fc5fb67e8de7b8f3ef53439John Reck    args->thread->jankTracker().switchStorageToAshmem(args->fd);
486edc524c90506d80e0fc5fb67e8de7b8f3ef53439John Reck    close(args->fd);
487edc524c90506d80e0fc5fb67e8de7b8f3ef53439John Reck    return nullptr;
488edc524c90506d80e0fc5fb67e8de7b8f3ef53439John Reck}
489edc524c90506d80e0fc5fb67e8de7b8f3ef53439John Reck
490edc524c90506d80e0fc5fb67e8de7b8f3ef53439John Reckvoid RenderProxy::setProcessStatsBuffer(int fd) {
491edc524c90506d80e0fc5fb67e8de7b8f3ef53439John Reck    SETUP_TASK(setProcessStatsBuffer);
492df1742ed47da1e9b61afeae16fa448d5302a8aa0John Reck    auto& rt = RenderThread::getInstance();
493df1742ed47da1e9b61afeae16fa448d5302a8aa0John Reck    args->thread = &rt;
494edc524c90506d80e0fc5fb67e8de7b8f3ef53439John Reck    args->fd = dup(fd);
495df1742ed47da1e9b61afeae16fa448d5302a8aa0John Reck    rt.queue(task);
496df1742ed47da1e9b61afeae16fa448d5302a8aa0John Reck}
497df1742ed47da1e9b61afeae16fa448d5302a8aa0John Reck
498df1742ed47da1e9b61afeae16fa448d5302a8aa0John ReckCREATE_BRIDGE1(rotateProcessStatsBuffer, RenderThread* thread) {
499df1742ed47da1e9b61afeae16fa448d5302a8aa0John Reck    args->thread->jankTracker().rotateStorage();
500df1742ed47da1e9b61afeae16fa448d5302a8aa0John Reck    return nullptr;
501df1742ed47da1e9b61afeae16fa448d5302a8aa0John Reck}
502df1742ed47da1e9b61afeae16fa448d5302a8aa0John Reck
503df1742ed47da1e9b61afeae16fa448d5302a8aa0John Reckvoid RenderProxy::rotateProcessStatsBuffer() {
504df1742ed47da1e9b61afeae16fa448d5302a8aa0John Reck    SETUP_TASK(rotateProcessStatsBuffer);
505df1742ed47da1e9b61afeae16fa448d5302a8aa0John Reck    auto& rt = RenderThread::getInstance();
506df1742ed47da1e9b61afeae16fa448d5302a8aa0John Reck    args->thread = &rt;
507df1742ed47da1e9b61afeae16fa448d5302a8aa0John Reck    rt.queue(task);
508edc524c90506d80e0fc5fb67e8de7b8f3ef53439John Reck}
509edc524c90506d80e0fc5fb67e8de7b8f3ef53439John Reck
51033eb07f5759b85a5617f8057d8a335019c7d24ddTim Murrayint RenderProxy::getRenderThreadTid() {
51133eb07f5759b85a5617f8057d8a335019c7d24ddTim Murray    return mRenderThread.getTid();
51233eb07f5759b85a5617f8057d8a335019c7d24ddTim Murray}
51333eb07f5759b85a5617f8057d8a335019c7d24ddTim Murray
514ea7a7fb75acb7305eb774ca7bc7e96103bd49323SkuhneCREATE_BRIDGE3(addRenderNode, CanvasContext* context, RenderNode* node, bool placeFront) {
515ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    args->context->addRenderNode(args->node, args->placeFront);
516ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    return nullptr;
517ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne}
518ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne
519ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhnevoid RenderProxy::addRenderNode(RenderNode* node, bool placeFront) {
520ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    SETUP_TASK(addRenderNode);
521ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    args->context = mContext;
522ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    args->node = node;
523ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    args->placeFront = placeFront;
524ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    post(task);
525ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne}
526ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne
527ea7a7fb75acb7305eb774ca7bc7e96103bd49323SkuhneCREATE_BRIDGE2(removeRenderNode, CanvasContext* context, RenderNode* node) {
528ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    args->context->removeRenderNode(args->node);
529ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    return nullptr;
530ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne}
531ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne
532ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhnevoid RenderProxy::removeRenderNode(RenderNode* node) {
533ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    SETUP_TASK(removeRenderNode);
534ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    args->context = mContext;
535ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    args->node = node;
536ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    post(task);
537ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne}
538ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne
539ea7a7fb75acb7305eb774ca7bc7e96103bd49323SkuhneCREATE_BRIDGE2(drawRenderNode, CanvasContext* context, RenderNode* node) {
540ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    args->context->prepareAndDraw(args->node);
541ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    return nullptr;
542ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne}
543ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne
544ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhnevoid RenderProxy::drawRenderNode(RenderNode* node) {
545ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    SETUP_TASK(drawRenderNode);
546ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    args->context = mContext;
547ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    args->node = node;
548ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    // Be pseudo-thread-safe and don't use any member variables
549ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    staticPostAndWait(task);
550ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne}
551ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne
552b816087962aba0019b022303330f03b897b580edSkuhneCREATE_BRIDGE5(setContentDrawBounds, CanvasContext* context, int left, int top,
553ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne        int right, int bottom) {
554b816087962aba0019b022303330f03b897b580edSkuhne    args->context->setContentDrawBounds(args->left, args->top, args->right, args->bottom);
555ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    return nullptr;
556ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne}
557ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne
558b816087962aba0019b022303330f03b897b580edSkuhnevoid RenderProxy::setContentDrawBounds(int left, int top, int right, int bottom) {
559b816087962aba0019b022303330f03b897b580edSkuhne    SETUP_TASK(setContentDrawBounds);
560ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    args->context = mContext;
561ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    args->left = left;
562ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    args->top = top;
563ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    args->right = right;
564ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    args->bottom = bottom;
565ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    staticPostAndWait(task);
566ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne}
567ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne
568e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn ReckCREATE_BRIDGE1(serializeDisplayListTree, CanvasContext* context) {
569e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    args->context->serializeDisplayListTree();
570e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    return nullptr;
571e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck}
572e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck
573e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reckvoid RenderProxy::serializeDisplayListTree() {
574e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    SETUP_TASK(serializeDisplayListTree);
575e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    args->context = mContext;
576e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    post(task);
577e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck}
578e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck
579910beb8f5d9042163e2ad0dbb744d9f147db9604Andres MoralesCREATE_BRIDGE2(addFrameMetricsObserver, CanvasContext* context,
580910beb8f5d9042163e2ad0dbb744d9f147db9604Andres Morales        FrameMetricsObserver* frameStatsObserver) {
581910beb8f5d9042163e2ad0dbb744d9f147db9604Andres Morales   args->context->addFrameMetricsObserver(args->frameStatsObserver);
58206f5bc70a667a02b14e31d3f53f91d3661e30666Andres Morales   if (args->frameStatsObserver != nullptr) {
58306f5bc70a667a02b14e31d3f53f91d3661e30666Andres Morales       args->frameStatsObserver->decStrong(args->context);
58406f5bc70a667a02b14e31d3f53f91d3661e30666Andres Morales   }
58506f5bc70a667a02b14e31d3f53f91d3661e30666Andres Morales   return nullptr;
58606f5bc70a667a02b14e31d3f53f91d3661e30666Andres Morales}
58706f5bc70a667a02b14e31d3f53f91d3661e30666Andres Morales
588910beb8f5d9042163e2ad0dbb744d9f147db9604Andres Moralesvoid RenderProxy::addFrameMetricsObserver(FrameMetricsObserver* observer) {
589910beb8f5d9042163e2ad0dbb744d9f147db9604Andres Morales    SETUP_TASK(addFrameMetricsObserver);
59006f5bc70a667a02b14e31d3f53f91d3661e30666Andres Morales    args->context = mContext;
59106f5bc70a667a02b14e31d3f53f91d3661e30666Andres Morales    args->frameStatsObserver = observer;
59206f5bc70a667a02b14e31d3f53f91d3661e30666Andres Morales    if (observer != nullptr) {
59306f5bc70a667a02b14e31d3f53f91d3661e30666Andres Morales        observer->incStrong(mContext);
59406f5bc70a667a02b14e31d3f53f91d3661e30666Andres Morales    }
59506f5bc70a667a02b14e31d3f53f91d3661e30666Andres Morales    post(task);
59606f5bc70a667a02b14e31d3f53f91d3661e30666Andres Morales}
59706f5bc70a667a02b14e31d3f53f91d3661e30666Andres Morales
598910beb8f5d9042163e2ad0dbb744d9f147db9604Andres MoralesCREATE_BRIDGE2(removeFrameMetricsObserver, CanvasContext* context,
599910beb8f5d9042163e2ad0dbb744d9f147db9604Andres Morales        FrameMetricsObserver* frameStatsObserver) {
600910beb8f5d9042163e2ad0dbb744d9f147db9604Andres Morales   args->context->removeFrameMetricsObserver(args->frameStatsObserver);
60106f5bc70a667a02b14e31d3f53f91d3661e30666Andres Morales   if (args->frameStatsObserver != nullptr) {
60206f5bc70a667a02b14e31d3f53f91d3661e30666Andres Morales       args->frameStatsObserver->decStrong(args->context);
60306f5bc70a667a02b14e31d3f53f91d3661e30666Andres Morales   }
60406f5bc70a667a02b14e31d3f53f91d3661e30666Andres Morales   return nullptr;
60506f5bc70a667a02b14e31d3f53f91d3661e30666Andres Morales}
60606f5bc70a667a02b14e31d3f53f91d3661e30666Andres Morales
607910beb8f5d9042163e2ad0dbb744d9f147db9604Andres Moralesvoid RenderProxy::removeFrameMetricsObserver(FrameMetricsObserver* observer) {
608910beb8f5d9042163e2ad0dbb744d9f147db9604Andres Morales    SETUP_TASK(removeFrameMetricsObserver);
60906f5bc70a667a02b14e31d3f53f91d3661e30666Andres Morales    args->context = mContext;
61006f5bc70a667a02b14e31d3f53f91d3661e30666Andres Morales    args->frameStatsObserver = observer;
61106f5bc70a667a02b14e31d3f53f91d3661e30666Andres Morales    if (observer != nullptr) {
61206f5bc70a667a02b14e31d3f53f91d3661e30666Andres Morales        observer->incStrong(mContext);
61306f5bc70a667a02b14e31d3f53f91d3661e30666Andres Morales    }
61406f5bc70a667a02b14e31d3f53f91d3661e30666Andres Morales    post(task);
61506f5bc70a667a02b14e31d3f53f91d3661e30666Andres Morales}
61606f5bc70a667a02b14e31d3f53f91d3661e30666Andres Morales
6179580146f5076aaa7c498f86bd3d724c00599f6f4John ReckCREATE_BRIDGE4(copySurfaceInto, RenderThread* thread,
6189580146f5076aaa7c498f86bd3d724c00599f6f4John Reck        Surface* surface, Rect srcRect, SkBitmap* bitmap) {
619c4fbada76aa840105553b2c2bce2204e673d2983Derek Sollenberger    return (void*)args->thread->readback().copySurfaceInto(*args->surface,
620c4fbada76aa840105553b2c2bce2204e673d2983Derek Sollenberger            args->srcRect, args->bitmap);
62110dd0585c11dcedb5a271d54e645594f1d215d5cJohn Reck}
62210dd0585c11dcedb5a271d54e645594f1d215d5cJohn Reck
6239580146f5076aaa7c498f86bd3d724c00599f6f4John Reckint RenderProxy::copySurfaceInto(sp<Surface>& surface, int left, int top,
6249580146f5076aaa7c498f86bd3d724c00599f6f4John Reck        int right, int bottom,  SkBitmap* bitmap) {
62510dd0585c11dcedb5a271d54e645594f1d215d5cJohn Reck    SETUP_TASK(copySurfaceInto);
62610dd0585c11dcedb5a271d54e645594f1d215d5cJohn Reck    args->bitmap = bitmap;
62710dd0585c11dcedb5a271d54e645594f1d215d5cJohn Reck    args->surface = surface.get();
62810dd0585c11dcedb5a271d54e645594f1d215d5cJohn Reck    args->thread = &RenderThread::getInstance();
6299580146f5076aaa7c498f86bd3d724c00599f6f4John Reck    args->srcRect.set(left, top, right, bottom);
630e94cbc76d560a157c0a0d47181b4ed2a0aadbeb1John Reck    return static_cast<int>(
631e94cbc76d560a157c0a0d47181b4ed2a0aadbeb1John Reck            reinterpret_cast<intptr_t>( staticPostAndWait(task) ));
63210dd0585c11dcedb5a271d54e645594f1d215d5cJohn Reck}
63310dd0585c11dcedb5a271d54e645594f1d215d5cJohn Reck
634ec4a4b13eae2241d1613890c1c1c096bed891845sergeyvCREATE_BRIDGE2(prepareToDraw, RenderThread* thread, Bitmap* bitmap) {
635daf7229047c44947b9b02ee187fe5b13f30ebd4bDerek Sollenberger    CanvasContext::prepareToDraw(*args->thread, args->bitmap);
636ec4a4b13eae2241d1613890c1c1c096bed891845sergeyv    args->bitmap->unref();
6374387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck    args->bitmap = nullptr;
6384387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck    return nullptr;
6394387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck}
6404387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck
641ec4a4b13eae2241d1613890c1c1c096bed891845sergeyvvoid RenderProxy::prepareToDraw(Bitmap& bitmap) {
6424387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck    // If we haven't spun up a hardware accelerated window yet, there's no
6434387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck    // point in precaching these bitmaps as it can't impact jank.
6444387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck    // We also don't know if we even will spin up a hardware-accelerated
6454387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck    // window or not.
6464387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck    if (!RenderThread::hasInstance()) return;
6474387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck    RenderThread* renderThread = &RenderThread::getInstance();
6484387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck    SETUP_TASK(prepareToDraw);
6494387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck    args->thread = renderThread;
650ec4a4b13eae2241d1613890c1c1c096bed891845sergeyv    bitmap.ref();
651ec4a4b13eae2241d1613890c1c1c096bed891845sergeyv    args->bitmap = &bitmap;
6524387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck    nsecs_t lastVsync = renderThread->timeLord().latestVsync();
6534387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck    nsecs_t estimatedNextVsync = lastVsync + renderThread->timeLord().frameIntervalNanos();
6544387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck    nsecs_t timeToNextVsync = estimatedNextVsync - systemTime(CLOCK_MONOTONIC);
6554387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck    // We expect the UI thread to take 4ms and for RT to be active from VSYNC+4ms to
6564387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck    // VSYNC+12ms or so, so aim for the gap during which RT is expected to
6574387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck    // be idle
6584387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck    // TODO: Make this concept a first-class supported thing? RT could use
6594387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck    // knowledge of pending draws to better schedule this task
6604387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck    if (timeToNextVsync > -6_ms && timeToNextVsync < 1_ms) {
6614387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck        renderThread->queueAt(task, estimatedNextVsync + 8_ms);
6624387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck    } else {
6634387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck        renderThread->queue(task);
6644387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck    }
6654387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck}
6664387190d8ec9fe4e953fcfeb093a644b82cf85edJohn Reck
667694d499662838123f474f41b31dea84ec5d563f0sergeyvCREATE_BRIDGE2(allocateHardwareBitmap, RenderThread* thread, SkBitmap* bitmap) {
668694d499662838123f474f41b31dea84ec5d563f0sergeyv    sk_sp<Bitmap> hardwareBitmap = Bitmap::allocateHardwareBitmap(*args->thread, *args->bitmap);
669694d499662838123f474f41b31dea84ec5d563f0sergeyv    return hardwareBitmap.release();
670694d499662838123f474f41b31dea84ec5d563f0sergeyv}
671694d499662838123f474f41b31dea84ec5d563f0sergeyv
672694d499662838123f474f41b31dea84ec5d563f0sergeyvsk_sp<Bitmap> RenderProxy::allocateHardwareBitmap(SkBitmap& bitmap) {
673694d499662838123f474f41b31dea84ec5d563f0sergeyv    SETUP_TASK(allocateHardwareBitmap);
674694d499662838123f474f41b31dea84ec5d563f0sergeyv    args->bitmap = &bitmap;
675694d499662838123f474f41b31dea84ec5d563f0sergeyv    args->thread = &RenderThread::getInstance();
676694d499662838123f474f41b31dea84ec5d563f0sergeyv    sk_sp<Bitmap> hardwareBitmap(reinterpret_cast<Bitmap*>(staticPostAndWait(task)));
677694d499662838123f474f41b31dea84ec5d563f0sergeyv    return hardwareBitmap;
678694d499662838123f474f41b31dea84ec5d563f0sergeyv}
679694d499662838123f474f41b31dea84ec5d563f0sergeyv
68059eecb526adc5bd7041e7b6147bfcc40dd2c200esergeyvCREATE_BRIDGE3(copyGraphicBufferInto, RenderThread* thread, GraphicBuffer* buffer, SkBitmap* bitmap) {
68159eecb526adc5bd7041e7b6147bfcc40dd2c200esergeyv    return (void*) args->thread->readback().copyGraphicBufferInto(args->buffer, args->bitmap);
68259eecb526adc5bd7041e7b6147bfcc40dd2c200esergeyv}
68359eecb526adc5bd7041e7b6147bfcc40dd2c200esergeyv
68459eecb526adc5bd7041e7b6147bfcc40dd2c200esergeyvint RenderProxy::copyGraphicBufferInto(GraphicBuffer* buffer, SkBitmap* bitmap) {
6856983bc40d72acbf06cd04818877cb3f5fea22886Stan Iliev    RenderThread& thread = RenderThread::getInstance();
6866983bc40d72acbf06cd04818877cb3f5fea22886Stan Iliev    if (Properties::isSkiaEnabled() && gettid() == thread.getTid()) {
6876983bc40d72acbf06cd04818877cb3f5fea22886Stan Iliev        //TODO: fix everything that hits this. We should never be triggering a readback ourselves.
6886983bc40d72acbf06cd04818877cb3f5fea22886Stan Iliev        return (int) thread.readback().copyGraphicBufferInto(buffer, bitmap);
6896983bc40d72acbf06cd04818877cb3f5fea22886Stan Iliev    } else {
6906983bc40d72acbf06cd04818877cb3f5fea22886Stan Iliev        SETUP_TASK(copyGraphicBufferInto);
6916983bc40d72acbf06cd04818877cb3f5fea22886Stan Iliev        args->thread = &thread;
6926983bc40d72acbf06cd04818877cb3f5fea22886Stan Iliev        args->bitmap = bitmap;
6936983bc40d72acbf06cd04818877cb3f5fea22886Stan Iliev        args->buffer = buffer;
6946983bc40d72acbf06cd04818877cb3f5fea22886Stan Iliev        return static_cast<int>(reinterpret_cast<intptr_t>(staticPostAndWait(task)));
6956983bc40d72acbf06cd04818877cb3f5fea22886Stan Iliev    }
69659eecb526adc5bd7041e7b6147bfcc40dd2c200esergeyv}
69759eecb526adc5bd7041e7b6147bfcc40dd2c200esergeyv
6989a814875c4e3a98fea99dae623f22268a9afa38aJohn ReckCREATE_BRIDGE2(onBitmapDestroyed, RenderThread* thread, uint32_t pixelRefId) {
6999a814875c4e3a98fea99dae623f22268a9afa38aJohn Reck    args->thread->renderState().onBitmapDestroyed(args->pixelRefId);
7009a814875c4e3a98fea99dae623f22268a9afa38aJohn Reck    return nullptr;
7019a814875c4e3a98fea99dae623f22268a9afa38aJohn Reck}
7029a814875c4e3a98fea99dae623f22268a9afa38aJohn Reck
7039a814875c4e3a98fea99dae623f22268a9afa38aJohn Reckvoid RenderProxy::onBitmapDestroyed(uint32_t pixelRefId) {
7049a814875c4e3a98fea99dae623f22268a9afa38aJohn Reck    if (!RenderThread::hasInstance()) return;
7059a814875c4e3a98fea99dae623f22268a9afa38aJohn Reck    SETUP_TASK(onBitmapDestroyed);
7069a814875c4e3a98fea99dae623f22268a9afa38aJohn Reck    RenderThread& thread = RenderThread::getInstance();
7079a814875c4e3a98fea99dae623f22268a9afa38aJohn Reck    args->thread = &thread;
7089a814875c4e3a98fea99dae623f22268a9afa38aJohn Reck    args->pixelRefId = pixelRefId;
7099a814875c4e3a98fea99dae623f22268a9afa38aJohn Reck    thread.queue(task);
7109a814875c4e3a98fea99dae623f22268a9afa38aJohn Reck}
7119a814875c4e3a98fea99dae623f22268a9afa38aJohn Reck
712a896306c38c4d11c43b967a21db6d74c251b2520John Reckvoid RenderProxy::disableVsync() {
713a896306c38c4d11c43b967a21db6d74c251b2520John Reck    Properties::disableVsync = true;
714a896306c38c4d11c43b967a21db6d74c251b2520John Reck}
715a896306c38c4d11c43b967a21db6d74c251b2520John Reck
7164f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reckvoid RenderProxy::post(RenderTask* task) {
7174f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    mRenderThread.queue(task);
7184f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
7194f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
7204f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reckvoid* RenderProxy::postAndWait(MethodInvokeRenderTask* task) {
7214f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    void* retval;
7224f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    task->setReturnPtr(&retval);
723cba287b9716155183faf21865a6c28ba49ffe486John Reck    SignalingRenderTask syncTask(task, &mSyncMutex, &mSyncCondition);
724cba287b9716155183faf21865a6c28ba49ffe486John Reck    AutoMutex _lock(mSyncMutex);
725cba287b9716155183faf21865a6c28ba49ffe486John Reck    mRenderThread.queue(&syncTask);
726298a146754e35cbc650aa991ebff1a41eefdbe80Tom Cherry    while (!syncTask.hasRun()) {
727298a146754e35cbc650aa991ebff1a41eefdbe80Tom Cherry        mSyncCondition.wait(mSyncMutex);
728298a146754e35cbc650aa991ebff1a41eefdbe80Tom Cherry    }
7294f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    return retval;
7304f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
7314f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
7320e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reckvoid* RenderProxy::staticPostAndWait(MethodInvokeRenderTask* task) {
7330e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck    RenderThread& thread = RenderThread::getInstance();
7346983bc40d72acbf06cd04818877cb3f5fea22886Stan Iliev    LOG_ALWAYS_FATAL_IF(gettid() == thread.getTid());
7350e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck    void* retval;
7360e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck    task->setReturnPtr(&retval);
7370a24b146cd3dacf372ce98424044423a5b2fbf2aChris Craik    thread.queueAndWait(task);
7380e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck    return retval;
7390e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck}
7400e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck
7414f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck} /* namespace renderthread */
7424f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck} /* namespace uirenderer */
7434f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck} /* namespace android */
744