RenderProxy.cpp revision be34f2f3b340196426bdf558b28951359a4d84fa
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#define LOG_TAG "RenderProxy"
18
19#include "RenderProxy.h"
20
21#include "CanvasContext.h"
22#include "RenderTask.h"
23#include "RenderThread.h"
24
25#include "../DeferredLayerUpdater.h"
26#include "../DisplayList.h"
27#include "../LayerRenderer.h"
28#include "../Rect.h"
29
30namespace android {
31namespace uirenderer {
32namespace renderthread {
33
34#define ARGS(method) method ## Args
35
36#define CREATE_BRIDGE0(name) CREATE_BRIDGE(name,,,,,,,,)
37#define CREATE_BRIDGE1(name, a1) CREATE_BRIDGE(name, a1,,,,,,,)
38#define CREATE_BRIDGE2(name, a1, a2) CREATE_BRIDGE(name, a1,a2,,,,,,)
39#define CREATE_BRIDGE3(name, a1, a2, a3) CREATE_BRIDGE(name, a1,a2,a3,,,,,)
40#define CREATE_BRIDGE4(name, a1, a2, a3, a4) CREATE_BRIDGE(name, a1,a2,a3,a4,,,,)
41#define CREATE_BRIDGE(name, a1, a2, a3, a4, a5, a6, a7, a8) \
42    typedef struct { \
43        a1; a2; a3; a4; a5; a6; a7; a8; \
44    } ARGS(name); \
45    static void* Bridge_ ## name(ARGS(name)* args)
46
47#define SETUP_TASK(method) \
48    LOG_ALWAYS_FATAL_IF( METHOD_INVOKE_PAYLOAD_SIZE < sizeof(ARGS(method)), \
49        "METHOD_INVOKE_PAYLOAD_SIZE %d is smaller than sizeof(" #method "Args) %d", \
50                METHOD_INVOKE_PAYLOAD_SIZE, sizeof(ARGS(method))); \
51    MethodInvokeRenderTask* task = createTask((RunnableMethod) Bridge_ ## method); \
52    ARGS(method) *args = (ARGS(method) *) task->payload()
53
54CREATE_BRIDGE1(createContext, bool translucent) {
55    return new CanvasContext(args->translucent);
56}
57
58RenderProxy::RenderProxy(bool translucent)
59        : mRenderThread(RenderThread::getInstance())
60        , mContext(0) {
61    SETUP_TASK(createContext);
62    args->translucent = translucent;
63    mContext = (CanvasContext*) postAndWait(task);
64}
65
66RenderProxy::~RenderProxy() {
67    destroyContext();
68}
69
70CREATE_BRIDGE1(destroyContext, CanvasContext* context) {
71    delete args->context;
72    return NULL;
73}
74
75void RenderProxy::destroyContext() {
76    if (mContext) {
77        SETUP_TASK(destroyContext);
78        args->context = mContext;
79        mContext = 0;
80        post(task);
81    }
82}
83
84CREATE_BRIDGE2(initialize, CanvasContext* context, EGLNativeWindowType window) {
85    return (void*) args->context->initialize(args->window);
86}
87
88bool RenderProxy::initialize(EGLNativeWindowType window) {
89    SETUP_TASK(initialize);
90    args->context = mContext;
91    args->window = window;
92    return (bool) postAndWait(task);
93}
94
95CREATE_BRIDGE2(updateSurface, CanvasContext* context, EGLNativeWindowType window) {
96    args->context->updateSurface(args->window);
97    return NULL;
98}
99
100void RenderProxy::updateSurface(EGLNativeWindowType window) {
101    SETUP_TASK(updateSurface);
102    args->context = mContext;
103    args->window = window;
104    post(task);
105}
106
107CREATE_BRIDGE3(setup, CanvasContext* context, int width, int height) {
108    args->context->setup(args->width, args->height);
109    return NULL;
110}
111
112void RenderProxy::setup(int width, int height) {
113    SETUP_TASK(setup);
114    args->context = mContext;
115    args->width = width;
116    args->height = height;
117    post(task);
118}
119
120CREATE_BRIDGE3(setDisplayListData, CanvasContext* context, DisplayList* displayList,
121        DisplayListData* newData) {
122    args->context->setDisplayListData(args->displayList, args->newData);
123    return NULL;
124}
125
126void RenderProxy::setDisplayListData(DisplayList* displayList, DisplayListData* newData) {
127    SETUP_TASK(setDisplayListData);
128    args->context = mContext;
129    args->displayList = displayList;
130    args->newData = newData;
131    post(task);
132}
133
134CREATE_BRIDGE4(drawDisplayList, CanvasContext* context, DisplayList* displayList,
135        Rect dirty, const Vector<DeferredLayerUpdater*>* layerUpdates) {
136    Rect* dirty = &args->dirty;
137    if (dirty->bottom == -1 && dirty->left == -1 &&
138            dirty->top == -1 && dirty->right == -1) {
139        dirty = 0;
140    }
141    args->context->processLayerUpdates(args->layerUpdates);
142    args->context->drawDisplayList(args->displayList, dirty);
143    return NULL;
144}
145
146void RenderProxy::drawDisplayList(DisplayList* displayList,
147        int dirtyLeft, int dirtyTop, int dirtyRight, int dirtyBottom) {
148    SETUP_TASK(drawDisplayList);
149    args->context = mContext;
150    args->displayList = displayList;
151    args->dirty.set(dirtyLeft, dirtyTop, dirtyRight, dirtyBottom);
152    args->layerUpdates = &mLayers;
153    // TODO: Switch to post() once some form of thread safety strategy is in place
154    postAndWait(task);
155}
156
157CREATE_BRIDGE1(destroyCanvas, CanvasContext* context) {
158    args->context->destroyCanvas();
159    return NULL;
160}
161
162void RenderProxy::destroyCanvas() {
163    SETUP_TASK(destroyCanvas);
164    args->context = mContext;
165    post(task);
166}
167
168CREATE_BRIDGE2(attachFunctor, CanvasContext* context, Functor* functor) {
169    args->context->attachFunctor(args->functor);
170    return NULL;
171}
172
173void RenderProxy::attachFunctor(Functor* functor) {
174    SETUP_TASK(attachFunctor);
175    args->context = mContext;
176    args->functor = functor;
177    post(task);
178}
179
180CREATE_BRIDGE2(detachFunctor, CanvasContext* context, Functor* functor) {
181    args->context->detachFunctor(args->functor);
182    return NULL;
183}
184
185void RenderProxy::detachFunctor(Functor* functor) {
186    SETUP_TASK(detachFunctor);
187    args->context = mContext;
188    args->functor = functor;
189    post(task);
190}
191
192CREATE_BRIDGE2(runWithGlContext, CanvasContext* context, RenderTask* task) {
193    args->context->runWithGlContext(args->task);
194    return NULL;
195}
196
197void RenderProxy::runWithGlContext(RenderTask* gltask) {
198    SETUP_TASK(runWithGlContext);
199    args->context = mContext;
200    args->task = gltask;
201    postAndWait(task);
202}
203
204CREATE_BRIDGE2(createDisplayListLayer, int width, int height) {
205    Layer* layer = LayerRenderer::createRenderLayer(args->width, args->height);
206    if (!layer) return 0;
207
208    OpenGLRenderer* renderer = new LayerRenderer(layer);
209    renderer->initProperties();
210    return new DeferredLayerUpdater(layer, renderer);
211}
212
213DeferredLayerUpdater* RenderProxy::createDisplayListLayer(int width, int height) {
214    SETUP_TASK(createDisplayListLayer);
215    args->width = width;
216    args->height = height;
217    void* retval = postAndWait(task);
218    DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval);
219    mLayers.push(layer);
220    return layer;
221}
222
223CREATE_BRIDGE0(createTextureLayer) {
224    Layer* layer = LayerRenderer::createTextureLayer();
225    if (!layer) return 0;
226    return new DeferredLayerUpdater(layer);
227}
228
229DeferredLayerUpdater* RenderProxy::createTextureLayer() {
230    SETUP_TASK(createTextureLayer);
231    void* retval = postAndWait(task);
232    DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval);
233    mLayers.push(layer);
234    return layer;
235}
236
237CREATE_BRIDGE1(destroyLayer, Layer* layer) {
238    LayerRenderer::destroyLayer(args->layer);
239    return NULL;
240}
241
242CREATE_BRIDGE3(copyLayerInto, CanvasContext* context, DeferredLayerUpdater* layer,
243        SkBitmap* bitmap) {
244    bool success = args->context->copyLayerInto(args->layer, args->bitmap);
245    return (void*) success;
246}
247
248bool RenderProxy::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
249    SETUP_TASK(copyLayerInto);
250    args->context = mContext;
251    args->layer = layer;
252    args->bitmap = bitmap;
253    return (bool) postAndWait(task);
254}
255
256void RenderProxy::destroyLayer(DeferredLayerUpdater* layer) {
257    for (size_t i = 0; i < mLayers.size(); i++) {
258        if (mLayers[i] == layer) {
259            mLayers.removeAt(i);
260            break;
261        }
262    }
263    SETUP_TASK(destroyLayer);
264    args->layer = layer->detachBackingLayer();
265    post(task);
266}
267
268MethodInvokeRenderTask* RenderProxy::createTask(RunnableMethod method) {
269    // TODO: Consider having a small pool of these to avoid alloc churn
270    return new MethodInvokeRenderTask(method);
271}
272
273void RenderProxy::post(RenderTask* task) {
274    mRenderThread.queue(task);
275}
276
277void* RenderProxy::postAndWait(MethodInvokeRenderTask* task) {
278    void* retval;
279    task->setReturnPtr(&retval);
280    SignalingRenderTask syncTask(task, &mSyncMutex, &mSyncCondition);
281    AutoMutex _lock(mSyncMutex);
282    mRenderThread.queue(&syncTask);
283    mSyncCondition.wait(mSyncMutex);
284    return retval;
285}
286
287} /* namespace renderthread */
288} /* namespace uirenderer */
289} /* namespace android */
290