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