CanvasContext.cpp revision e19093cad8e67cf1db98157d0246d0141b19f808
1/*
2 * Copyright (C) 2014 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#include "CanvasContext.h"
18
19#include <algorithm>
20#include <private/hwui/DrawGlInfo.h>
21#include <strings.h>
22
23#include "EglManager.h"
24#include "RenderThread.h"
25#include "../AnimationContext.h"
26#include "../Caches.h"
27#include "../DeferredLayerUpdater.h"
28#include "../RenderState.h"
29#include "../LayerRenderer.h"
30#include "../OpenGLRenderer.h"
31#include "../Stencil.h"
32
33#define TRIM_MEMORY_COMPLETE 80
34#define TRIM_MEMORY_UI_HIDDEN 20
35
36namespace android {
37namespace uirenderer {
38namespace renderthread {
39
40CanvasContext::CanvasContext(RenderThread& thread, bool translucent,
41        RenderNode* rootRenderNode, IContextFactory* contextFactory)
42        : mRenderThread(thread)
43        , mEglManager(thread.eglManager())
44        , mEglSurface(EGL_NO_SURFACE)
45        , mBufferPreserved(false)
46        , mSwapBehavior(kSwap_default)
47        , mOpaque(!translucent)
48        , mCanvas(NULL)
49        , mHaveNewSurface(false)
50        , mRootRenderNode(rootRenderNode) {
51    mAnimationContext = contextFactory->createAnimationContext(mRenderThread.timeLord());
52    mRenderThread.renderState().registerCanvasContext(this);
53}
54
55CanvasContext::~CanvasContext() {
56    destroy();
57    delete mAnimationContext;
58    mRenderThread.renderState().unregisterCanvasContext(this);
59}
60
61void CanvasContext::destroy() {
62    stopDrawing();
63    freePrefetechedLayers();
64    destroyHardwareResources();
65    mAnimationContext->destroy();
66    if (mCanvas) {
67        delete mCanvas;
68        mCanvas = 0;
69    }
70    setSurface(NULL);
71}
72
73void CanvasContext::setSurface(ANativeWindow* window) {
74    mNativeWindow = window;
75
76    if (mEglSurface != EGL_NO_SURFACE) {
77        mEglManager.destroySurface(mEglSurface);
78        mEglSurface = EGL_NO_SURFACE;
79    }
80
81    if (window) {
82        mEglSurface = mEglManager.createSurface(window);
83    }
84
85    if (mEglSurface != EGL_NO_SURFACE) {
86        const bool preserveBuffer = (mSwapBehavior != kSwap_discardBuffer);
87        mBufferPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer);
88        mHaveNewSurface = true;
89        makeCurrent();
90    } else {
91        mRenderThread.removeFrameCallback(this);
92    }
93}
94
95void CanvasContext::swapBuffers() {
96    if (CC_UNLIKELY(!mEglManager.swapBuffers(mEglSurface))) {
97        setSurface(NULL);
98    }
99    mHaveNewSurface = false;
100}
101
102void CanvasContext::requireSurface() {
103    LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
104            "requireSurface() called but no surface set!");
105    makeCurrent();
106}
107
108void CanvasContext::setSwapBehavior(SwapBehavior swapBehavior) {
109    mSwapBehavior = swapBehavior;
110}
111
112bool CanvasContext::initialize(ANativeWindow* window) {
113    setSurface(window);
114    if (mCanvas) return false;
115    mCanvas = new OpenGLRenderer(mRenderThread.renderState());
116    mCanvas->initProperties();
117    return true;
118}
119
120void CanvasContext::updateSurface(ANativeWindow* window) {
121    setSurface(window);
122}
123
124void CanvasContext::pauseSurface(ANativeWindow* window) {
125    stopDrawing();
126}
127
128// TODO: don't pass viewport size, it's automatic via EGL
129void CanvasContext::setup(int width, int height, const Vector3& lightCenter, float lightRadius,
130        uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
131    if (!mCanvas) return;
132    mCanvas->initLight(lightCenter, lightRadius, ambientShadowAlpha, spotShadowAlpha);
133}
134
135void CanvasContext::setOpaque(bool opaque) {
136    mOpaque = opaque;
137}
138
139void CanvasContext::makeCurrent() {
140    // TODO: Figure out why this workaround is needed, see b/13913604
141    // In the meantime this matches the behavior of GLRenderer, so it is not a regression
142    mHaveNewSurface |= mEglManager.makeCurrent(mEglSurface);
143}
144
145void CanvasContext::processLayerUpdate(DeferredLayerUpdater* layerUpdater) {
146    bool success = layerUpdater->apply();
147    LOG_ALWAYS_FATAL_IF(!success, "Failed to update layer!");
148    if (layerUpdater->backingLayer()->deferredUpdateScheduled) {
149        mCanvas->pushLayerUpdate(layerUpdater->backingLayer());
150    }
151}
152
153void CanvasContext::prepareTree(TreeInfo& info) {
154    mRenderThread.removeFrameCallback(this);
155
156    info.damageAccumulator = &mDamageAccumulator;
157    info.renderer = mCanvas;
158    if (mPrefetechedLayers.size() && info.mode == TreeInfo::MODE_FULL) {
159        info.canvasContext = this;
160    }
161    mAnimationContext->startFrame(info.mode);
162    mRootRenderNode->prepareTree(info);
163    mAnimationContext->runRemainingAnimations(info);
164
165    if (info.canvasContext) {
166        freePrefetechedLayers();
167    }
168
169    if (CC_UNLIKELY(!mNativeWindow.get())) {
170        info.out.canDrawThisFrame = false;
171        return;
172    }
173
174    int runningBehind = 0;
175    // TODO: This query is moderately expensive, investigate adding some sort
176    // of fast-path based off when we last called eglSwapBuffers() as well as
177    // last vsync time. Or something.
178    mNativeWindow->query(mNativeWindow.get(),
179            NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &runningBehind);
180    info.out.canDrawThisFrame = !runningBehind;
181
182    if (info.out.hasAnimations || !info.out.canDrawThisFrame) {
183        if (!info.out.requiresUiRedraw) {
184            // If animationsNeedsRedraw is set don't bother posting for an RT anim
185            // as we will just end up fighting the UI thread.
186            mRenderThread.postFrameCallback(this);
187        }
188    }
189}
190
191void CanvasContext::stopDrawing() {
192    mRenderThread.removeFrameCallback(this);
193}
194
195void CanvasContext::notifyFramePending() {
196    ATRACE_CALL();
197    mRenderThread.pushBackFrameCallback(this);
198}
199
200void CanvasContext::draw() {
201    LOG_ALWAYS_FATAL_IF(!mCanvas || mEglSurface == EGL_NO_SURFACE,
202            "drawRenderNode called on a context with no canvas or surface!");
203
204    profiler().markPlaybackStart();
205
206    SkRect dirty;
207    mDamageAccumulator.finish(&dirty);
208
209    EGLint width, height;
210    mEglManager.beginFrame(mEglSurface, &width, &height);
211    if (width != mCanvas->getViewportWidth() || height != mCanvas->getViewportHeight()) {
212        mCanvas->setViewport(width, height);
213        dirty.setEmpty();
214    } else if (!mBufferPreserved || mHaveNewSurface) {
215        dirty.setEmpty();
216    } else {
217        if (!dirty.isEmpty() && !dirty.intersect(0, 0, width, height)) {
218            ALOGW("Dirty " RECT_STRING " doesn't intersect with 0 0 %d %d ?",
219                    SK_RECT_ARGS(dirty), width, height);
220            dirty.setEmpty();
221        }
222        profiler().unionDirty(&dirty);
223    }
224
225    if (!dirty.isEmpty()) {
226        mCanvas->prepareDirty(dirty.fLeft, dirty.fTop,
227                dirty.fRight, dirty.fBottom, mOpaque);
228    } else {
229        mCanvas->prepare(mOpaque);
230    }
231
232    Rect outBounds;
233    mCanvas->drawRenderNode(mRootRenderNode.get(), outBounds);
234
235    profiler().draw(mCanvas);
236
237    bool drew = mCanvas->finish();
238
239    profiler().markPlaybackEnd();
240
241    if (drew) {
242        swapBuffers();
243    } else {
244        mEglManager.cancelFrame();
245    }
246
247    profiler().finishFrame();
248}
249
250// Called by choreographer to do an RT-driven animation
251void CanvasContext::doFrame() {
252    if (CC_UNLIKELY(!mCanvas || mEglSurface == EGL_NO_SURFACE)) {
253        return;
254    }
255
256    ATRACE_CALL();
257
258    profiler().startFrame();
259
260    TreeInfo info(TreeInfo::MODE_RT_ONLY, mRenderThread.renderState());
261    prepareTree(info);
262    if (info.out.canDrawThisFrame) {
263        draw();
264    }
265}
266
267void CanvasContext::invokeFunctor(RenderThread& thread, Functor* functor) {
268    ATRACE_CALL();
269    DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
270    if (thread.eglManager().hasEglContext()) {
271        thread.eglManager().requireGlContext();
272        mode = DrawGlInfo::kModeProcess;
273    }
274
275    thread.renderState().invokeFunctor(functor, mode, NULL);
276}
277
278void CanvasContext::markLayerInUse(RenderNode* node) {
279    if (mPrefetechedLayers.erase(node)) {
280        node->decStrong(0);
281    }
282}
283
284static void destroyPrefetechedNode(RenderNode* node) {
285    ALOGW("Incorrectly called buildLayer on View: %s, destroying layer...", node->getName());
286    node->destroyHardwareResources();
287    node->decStrong(0);
288}
289
290void CanvasContext::freePrefetechedLayers() {
291    if (mPrefetechedLayers.size()) {
292        requireGlContext();
293        std::for_each(mPrefetechedLayers.begin(), mPrefetechedLayers.end(), destroyPrefetechedNode);
294        mPrefetechedLayers.clear();
295    }
296}
297
298void CanvasContext::buildLayer(RenderNode* node) {
299    ATRACE_CALL();
300    if (!mEglManager.hasEglContext() || !mCanvas) {
301        return;
302    }
303    requireGlContext();
304    // buildLayer() will leave the tree in an unknown state, so we must stop drawing
305    stopDrawing();
306
307    TreeInfo info(TreeInfo::MODE_FULL, mRenderThread.renderState());
308    info.damageAccumulator = &mDamageAccumulator;
309    info.renderer = mCanvas;
310    info.runAnimations = false;
311    node->prepareTree(info);
312    SkRect ignore;
313    mDamageAccumulator.finish(&ignore);
314    // Tickle the GENERIC property on node to mark it as dirty for damaging
315    // purposes when the frame is actually drawn
316    node->setPropertyFieldsDirty(RenderNode::GENERIC);
317
318    mCanvas->markLayersAsBuildLayers();
319    mCanvas->flushLayerUpdates();
320
321    node->incStrong(0);
322    mPrefetechedLayers.insert(node);
323}
324
325bool CanvasContext::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
326    requireGlContext();
327    layer->apply();
328    return LayerRenderer::copyLayer(mRenderThread.renderState(), layer->backingLayer(), bitmap);
329}
330
331void CanvasContext::destroyHardwareResources() {
332    stopDrawing();
333    if (mEglManager.hasEglContext()) {
334        requireGlContext();
335        freePrefetechedLayers();
336        mRootRenderNode->destroyHardwareResources();
337        Caches::getInstance().flush(Caches::kFlushMode_Layers);
338    }
339}
340
341void CanvasContext::trimMemory(RenderThread& thread, int level) {
342    // No context means nothing to free
343    if (!thread.eglManager().hasEglContext()) return;
344
345    ATRACE_CALL();
346    thread.eglManager().requireGlContext();
347    if (level >= TRIM_MEMORY_COMPLETE) {
348        Caches::getInstance().flush(Caches::kFlushMode_Full);
349        thread.eglManager().destroy();
350    } else if (level >= TRIM_MEMORY_UI_HIDDEN) {
351        Caches::getInstance().flush(Caches::kFlushMode_Moderate);
352    }
353}
354
355void CanvasContext::runWithGlContext(RenderTask* task) {
356    requireGlContext();
357    task->run();
358}
359
360Layer* CanvasContext::createTextureLayer() {
361    requireSurface();
362    return LayerRenderer::createTextureLayer(mRenderThread.renderState());
363}
364
365void CanvasContext::requireGlContext() {
366    mEglManager.requireGlContext();
367}
368
369void CanvasContext::setTextureAtlas(RenderThread& thread,
370        const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize) {
371    thread.eglManager().setTextureAtlas(buffer, map, mapSize);
372}
373
374} /* namespace renderthread */
375} /* namespace uirenderer */
376} /* namespace android */
377