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