RenderProxy.cpp revision 797b95b26bbb7557678af78b9a2a61830158920f
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_BRIDGE5(name, a1, a2, a3, a4, a5) CREATE_BRIDGE(name, a1,a2,a3,a4,a5,,,)
42#define CREATE_BRIDGE(name, a1, a2, a3, a4, a5, a6, a7, a8) \
43    typedef struct { \
44        a1; a2; a3; a4; a5; a6; a7; a8; \
45    } ARGS(name); \
46    static void* Bridge_ ## name(ARGS(name)* args)
47
48#define SETUP_TASK(method) \
49    LOG_ALWAYS_FATAL_IF( METHOD_INVOKE_PAYLOAD_SIZE < sizeof(ARGS(method)), \
50        "METHOD_INVOKE_PAYLOAD_SIZE %d is smaller than sizeof(" #method "Args) %d", \
51                METHOD_INVOKE_PAYLOAD_SIZE, sizeof(ARGS(method))); \
52    MethodInvokeRenderTask* task = new MethodInvokeRenderTask((RunnableMethod) Bridge_ ## method); \
53    ARGS(method) *args = (ARGS(method) *) task->payload()
54
55CREATE_BRIDGE2(createContext, bool translucent, RenderNode* rootRenderNode) {
56    return new CanvasContext(args->translucent, args->rootRenderNode);
57}
58
59RenderProxy::RenderProxy(bool translucent, RenderNode* rootRenderNode)
60        : mRenderThread(RenderThread::getInstance())
61        , mContext(0) {
62    SETUP_TASK(createContext);
63    args->translucent = translucent;
64    args->rootRenderNode = rootRenderNode;
65    mContext = (CanvasContext*) postAndWait(task);
66    mDrawFrameTask.setContext(&mRenderThread, mContext);
67}
68
69RenderProxy::~RenderProxy() {
70    destroyContext();
71}
72
73CREATE_BRIDGE1(destroyContext, CanvasContext* context) {
74    delete args->context;
75    return NULL;
76}
77
78void RenderProxy::destroyContext() {
79    if (mContext) {
80        SETUP_TASK(destroyContext);
81        args->context = mContext;
82        mContext = 0;
83        mDrawFrameTask.setContext(NULL, NULL);
84        // This is also a fence as we need to be certain that there are no
85        // outstanding mDrawFrame tasks posted before it is destroyed
86        postAndWait(task);
87    }
88}
89
90CREATE_BRIDGE2(setFrameInterval, RenderThread* thread, nsecs_t frameIntervalNanos) {
91    args->thread->timeLord().setFrameInterval(args->frameIntervalNanos);
92    return NULL;
93}
94
95void RenderProxy::setFrameInterval(nsecs_t frameIntervalNanos) {
96    SETUP_TASK(setFrameInterval);
97    args->thread = &mRenderThread;
98    args->frameIntervalNanos = frameIntervalNanos;
99    post(task);
100}
101
102CREATE_BRIDGE0(loadSystemProperties) {
103    bool needsRedraw = false;
104    if (Caches::hasInstance()) {
105        needsRedraw = Caches::getInstance().initProperties();
106    }
107    return (void*) needsRedraw;
108}
109
110bool RenderProxy::loadSystemProperties() {
111    SETUP_TASK(loadSystemProperties);
112    return (bool) postAndWait(task);
113}
114
115CREATE_BRIDGE2(initialize, CanvasContext* context, EGLNativeWindowType window) {
116    return (void*) args->context->initialize(args->window);
117}
118
119bool RenderProxy::initialize(const sp<ANativeWindow>& window) {
120    SETUP_TASK(initialize);
121    args->context = mContext;
122    args->window = window.get();
123    return (bool) postAndWait(task);
124}
125
126CREATE_BRIDGE2(updateSurface, CanvasContext* context, EGLNativeWindowType window) {
127    args->context->updateSurface(args->window);
128    return NULL;
129}
130
131void RenderProxy::updateSurface(const sp<ANativeWindow>& window) {
132    SETUP_TASK(updateSurface);
133    args->context = mContext;
134    args->window = window.get();
135    postAndWait(task);
136}
137
138CREATE_BRIDGE2(pauseSurface, CanvasContext* context, EGLNativeWindowType window) {
139    args->context->pauseSurface(args->window);
140    return NULL;
141}
142
143void RenderProxy::pauseSurface(const sp<ANativeWindow>& window) {
144    SETUP_TASK(pauseSurface);
145    args->context = mContext;
146    args->window = window.get();
147    postAndWait(task);
148}
149
150CREATE_BRIDGE5(setup, CanvasContext* context, int width, int height,
151        Vector3 lightCenter, int lightRadius) {
152    args->context->setup(args->width, args->height, args->lightCenter, args->lightRadius);
153    return NULL;
154}
155
156void RenderProxy::setup(int width, int height, const Vector3& lightCenter, float lightRadius) {
157    SETUP_TASK(setup);
158    args->context = mContext;
159    args->width = width;
160    args->height = height;
161    args->lightCenter = lightCenter;
162    args->lightRadius = lightRadius;
163    post(task);
164}
165
166CREATE_BRIDGE2(setOpaque, CanvasContext* context, bool opaque) {
167    args->context->setOpaque(args->opaque);
168    return NULL;
169}
170
171void RenderProxy::setOpaque(bool opaque) {
172    SETUP_TASK(setOpaque);
173    args->context = mContext;
174    args->opaque = opaque;
175    post(task);
176}
177
178int RenderProxy::syncAndDrawFrame(nsecs_t frameTimeNanos,
179        int dirtyLeft, int dirtyTop, int dirtyRight, int dirtyBottom) {
180    mDrawFrameTask.setDirty(dirtyLeft, dirtyTop, dirtyRight, dirtyBottom);
181    return mDrawFrameTask.drawFrame(frameTimeNanos);
182}
183
184CREATE_BRIDGE1(destroyCanvasAndSurface, CanvasContext* context) {
185    args->context->destroyCanvasAndSurface();
186    return NULL;
187}
188
189void RenderProxy::destroyCanvasAndSurface() {
190    SETUP_TASK(destroyCanvasAndSurface);
191    args->context = mContext;
192    // destroyCanvasAndSurface() needs a fence as when it returns the
193    // underlying BufferQueue is going to be released from under
194    // the render thread.
195    postAndWait(task);
196}
197
198CREATE_BRIDGE2(invokeFunctor, CanvasContext* context, Functor* functor) {
199    args->context->invokeFunctor(args->functor);
200    return NULL;
201}
202
203void RenderProxy::invokeFunctor(Functor* functor, bool waitForCompletion) {
204    ATRACE_CALL();
205    SETUP_TASK(invokeFunctor);
206    args->context = mContext;
207    args->functor = functor;
208    if (waitForCompletion) {
209        postAndWait(task);
210    } else {
211        post(task);
212    }
213}
214
215CREATE_BRIDGE2(runWithGlContext, CanvasContext* context, RenderTask* task) {
216    args->context->runWithGlContext(args->task);
217    return NULL;
218}
219
220void RenderProxy::runWithGlContext(RenderTask* gltask) {
221    SETUP_TASK(runWithGlContext);
222    args->context = mContext;
223    args->task = gltask;
224    postAndWait(task);
225}
226
227CREATE_BRIDGE3(createDisplayListLayer, CanvasContext* context, int width, int height) {
228    Layer* layer = args->context->createRenderLayer(args->width, args->height);
229    if (!layer) return 0;
230    return new DeferredLayerUpdater(layer);
231}
232
233DeferredLayerUpdater* RenderProxy::createDisplayListLayer(int width, int height) {
234    SETUP_TASK(createDisplayListLayer);
235    args->width = width;
236    args->height = height;
237    args->context = mContext;
238    void* retval = postAndWait(task);
239    DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval);
240    mDrawFrameTask.addLayer(layer);
241    return layer;
242}
243
244CREATE_BRIDGE1(createTextureLayer, CanvasContext* context) {
245    Layer* layer = args->context->createTextureLayer();
246    if (!layer) return 0;
247    return new DeferredLayerUpdater(layer);
248}
249
250DeferredLayerUpdater* RenderProxy::createTextureLayer() {
251    SETUP_TASK(createTextureLayer);
252    args->context = mContext;
253    void* retval = postAndWait(task);
254    DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval);
255    mDrawFrameTask.addLayer(layer);
256    return layer;
257}
258
259CREATE_BRIDGE1(destroyLayer, Layer* layer) {
260    LayerRenderer::destroyLayer(args->layer);
261    return NULL;
262}
263
264CREATE_BRIDGE3(copyLayerInto, CanvasContext* context, DeferredLayerUpdater* layer,
265        SkBitmap* bitmap) {
266    bool success = args->context->copyLayerInto(args->layer, args->bitmap);
267    return (void*) success;
268}
269
270bool RenderProxy::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
271    SETUP_TASK(copyLayerInto);
272    args->context = mContext;
273    args->layer = layer;
274    args->bitmap = bitmap;
275    return (bool) postAndWait(task);
276}
277
278void RenderProxy::destroyLayer(DeferredLayerUpdater* layer) {
279    mDrawFrameTask.removeLayer(layer);
280    SETUP_TASK(destroyLayer);
281    args->layer = layer->detachBackingLayer();
282    post(task);
283}
284
285CREATE_BRIDGE0(fence) {
286    // Intentionally empty
287    return NULL;
288}
289
290void RenderProxy::fence() {
291    SETUP_TASK(fence);
292    postAndWait(task);
293}
294
295void RenderProxy::post(RenderTask* task) {
296    mRenderThread.queue(task);
297}
298
299void* RenderProxy::postAndWait(MethodInvokeRenderTask* task) {
300    void* retval;
301    task->setReturnPtr(&retval);
302    SignalingRenderTask syncTask(task, &mSyncMutex, &mSyncCondition);
303    AutoMutex _lock(mSyncMutex);
304    mRenderThread.queue(&syncTask);
305    mSyncCondition.wait(mSyncMutex);
306    return retval;
307}
308
309} /* namespace renderthread */
310} /* namespace uirenderer */
311} /* namespace android */
312