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