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