RenderProxy.cpp revision fe5e7b7346a54537b980796ceeca66bfdbd05561
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_BRIDGE1(loadSystemProperties, CanvasContext* context) {
103    bool needsRedraw = false;
104    if (Caches::hasInstance()) {
105        needsRedraw = Caches::getInstance().initProperties();
106    }
107    if (args->context->profiler().loadSystemProperties()) {
108        needsRedraw = true;
109    }
110    return (void*) needsRedraw;
111}
112
113bool RenderProxy::loadSystemProperties() {
114    SETUP_TASK(loadSystemProperties);
115    args->context = mContext;
116    return (bool) postAndWait(task);
117}
118
119CREATE_BRIDGE2(initialize, CanvasContext* context, ANativeWindow* window) {
120    return (void*) args->context->initialize(args->window);
121}
122
123bool RenderProxy::initialize(const sp<ANativeWindow>& window) {
124    SETUP_TASK(initialize);
125    args->context = mContext;
126    args->window = window.get();
127    return (bool) postAndWait(task);
128}
129
130CREATE_BRIDGE2(updateSurface, CanvasContext* context, ANativeWindow* window) {
131    args->context->updateSurface(args->window);
132    return NULL;
133}
134
135void RenderProxy::updateSurface(const sp<ANativeWindow>& window) {
136    SETUP_TASK(updateSurface);
137    args->context = mContext;
138    args->window = window.get();
139    postAndWait(task);
140}
141
142CREATE_BRIDGE2(pauseSurface, CanvasContext* context, ANativeWindow* window) {
143    args->context->pauseSurface(args->window);
144    return NULL;
145}
146
147void RenderProxy::pauseSurface(const sp<ANativeWindow>& window) {
148    SETUP_TASK(pauseSurface);
149    args->context = mContext;
150    args->window = window.get();
151    postAndWait(task);
152}
153
154CREATE_BRIDGE5(setup, CanvasContext* context, int width, int height,
155        Vector3 lightCenter, int lightRadius) {
156    args->context->setup(args->width, args->height, args->lightCenter, args->lightRadius);
157    return NULL;
158}
159
160void RenderProxy::setup(int width, int height, const Vector3& lightCenter, float lightRadius) {
161    SETUP_TASK(setup);
162    args->context = mContext;
163    args->width = width;
164    args->height = height;
165    args->lightCenter = lightCenter;
166    args->lightRadius = lightRadius;
167    post(task);
168}
169
170CREATE_BRIDGE2(setOpaque, CanvasContext* context, bool opaque) {
171    args->context->setOpaque(args->opaque);
172    return NULL;
173}
174
175void RenderProxy::setOpaque(bool opaque) {
176    SETUP_TASK(setOpaque);
177    args->context = mContext;
178    args->opaque = opaque;
179    post(task);
180}
181
182int RenderProxy::syncAndDrawFrame(nsecs_t frameTimeNanos, nsecs_t recordDurationNanos,
183        float density, int dirtyLeft, int dirtyTop, int dirtyRight, int dirtyBottom) {
184    mDrawFrameTask.setDirty(dirtyLeft, dirtyTop, dirtyRight, dirtyBottom);
185    mDrawFrameTask.setDensity(density);
186    return mDrawFrameTask.drawFrame(frameTimeNanos, recordDurationNanos);
187}
188
189CREATE_BRIDGE1(destroyCanvasAndSurface, CanvasContext* context) {
190    args->context->destroyCanvasAndSurface();
191    return NULL;
192}
193
194void RenderProxy::destroyCanvasAndSurface() {
195    SETUP_TASK(destroyCanvasAndSurface);
196    args->context = mContext;
197    // destroyCanvasAndSurface() needs a fence as when it returns the
198    // underlying BufferQueue is going to be released from under
199    // the render thread.
200    postAndWait(task);
201}
202
203CREATE_BRIDGE2(invokeFunctor, CanvasContext* context, Functor* functor) {
204    args->context->invokeFunctor(args->functor);
205    return NULL;
206}
207
208void RenderProxy::invokeFunctor(Functor* functor, bool waitForCompletion) {
209    ATRACE_CALL();
210    SETUP_TASK(invokeFunctor);
211    args->context = mContext;
212    args->functor = functor;
213    if (waitForCompletion) {
214        postAndWait(task);
215    } else {
216        post(task);
217    }
218}
219
220CREATE_BRIDGE2(runWithGlContext, CanvasContext* context, RenderTask* task) {
221    args->context->runWithGlContext(args->task);
222    return NULL;
223}
224
225void RenderProxy::runWithGlContext(RenderTask* gltask) {
226    SETUP_TASK(runWithGlContext);
227    args->context = mContext;
228    args->task = gltask;
229    postAndWait(task);
230}
231
232CREATE_BRIDGE3(createDisplayListLayer, CanvasContext* context, int width, int height) {
233    Layer* layer = args->context->createRenderLayer(args->width, args->height);
234    if (!layer) return 0;
235    return new DeferredLayerUpdater(layer);
236}
237
238DeferredLayerUpdater* RenderProxy::createDisplayListLayer(int width, int height) {
239    SETUP_TASK(createDisplayListLayer);
240    args->width = width;
241    args->height = height;
242    args->context = mContext;
243    void* retval = postAndWait(task);
244    DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval);
245    mDrawFrameTask.addLayer(layer);
246    return layer;
247}
248
249CREATE_BRIDGE1(createTextureLayer, CanvasContext* context) {
250    Layer* layer = args->context->createTextureLayer();
251    if (!layer) return 0;
252    return new DeferredLayerUpdater(layer);
253}
254
255DeferredLayerUpdater* RenderProxy::createTextureLayer() {
256    SETUP_TASK(createTextureLayer);
257    args->context = mContext;
258    void* retval = postAndWait(task);
259    DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(retval);
260    mDrawFrameTask.addLayer(layer);
261    return layer;
262}
263
264CREATE_BRIDGE1(destroyLayer, Layer* layer) {
265    LayerRenderer::destroyLayer(args->layer);
266    return NULL;
267}
268
269CREATE_BRIDGE3(copyLayerInto, CanvasContext* context, DeferredLayerUpdater* layer,
270        SkBitmap* bitmap) {
271    bool success = args->context->copyLayerInto(args->layer, args->bitmap);
272    return (void*) success;
273}
274
275bool RenderProxy::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
276    SETUP_TASK(copyLayerInto);
277    args->context = mContext;
278    args->layer = layer;
279    args->bitmap = bitmap;
280    return (bool) postAndWait(task);
281}
282
283void RenderProxy::destroyLayer(DeferredLayerUpdater* layer) {
284    mDrawFrameTask.removeLayer(layer);
285    SETUP_TASK(destroyLayer);
286    args->layer = layer->detachBackingLayer();
287    post(task);
288}
289
290CREATE_BRIDGE2(flushCaches, CanvasContext* context, Caches::FlushMode flushMode) {
291    args->context->flushCaches(args->flushMode);
292    return NULL;
293}
294
295void RenderProxy::flushCaches(Caches::FlushMode flushMode) {
296    SETUP_TASK(flushCaches);
297    args->context = mContext;
298    args->flushMode = flushMode;
299    post(task);
300}
301
302CREATE_BRIDGE0(fence) {
303    // Intentionally empty
304    return NULL;
305}
306
307void RenderProxy::fence() {
308    SETUP_TASK(fence);
309    postAndWait(task);
310}
311
312CREATE_BRIDGE1(notifyFramePending, CanvasContext* context) {
313    args->context->notifyFramePending();
314    return NULL;
315}
316
317void RenderProxy::notifyFramePending() {
318    SETUP_TASK(notifyFramePending);
319    args->context = mContext;
320    mRenderThread.queueAtFront(task);
321}
322
323CREATE_BRIDGE2(dumpProfileInfo, CanvasContext* context, int fd) {
324    args->context->profiler().dumpData(args->fd);
325    return NULL;
326}
327
328void RenderProxy::dumpProfileInfo(int fd) {
329    SETUP_TASK(dumpProfileInfo);
330    args->context = mContext;
331    args->fd = fd;
332    postAndWait(task);
333}
334
335void RenderProxy::post(RenderTask* task) {
336    mRenderThread.queue(task);
337}
338
339void* RenderProxy::postAndWait(MethodInvokeRenderTask* task) {
340    void* retval;
341    task->setReturnPtr(&retval);
342    SignalingRenderTask syncTask(task, &mSyncMutex, &mSyncCondition);
343    AutoMutex _lock(mSyncMutex);
344    mRenderThread.queue(&syncTask);
345    mSyncCondition.wait(mSyncMutex);
346    return retval;
347}
348
349} /* namespace renderthread */
350} /* namespace uirenderer */
351} /* namespace android */
352