CanvasContext.cpp revision 107843de4507b3511006cb9c77b8d0364374385a
123b797ab5151eb2474f3bdd679f2f07bfd723042John Reck/*
223b797ab5151eb2474f3bdd679f2f07bfd723042John Reck * Copyright (C) 2014 The Android Open Source Project
323b797ab5151eb2474f3bdd679f2f07bfd723042John Reck *
423b797ab5151eb2474f3bdd679f2f07bfd723042John Reck * Licensed under the Apache License, Version 2.0 (the "License");
523b797ab5151eb2474f3bdd679f2f07bfd723042John Reck * you may not use this file except in compliance with the License.
623b797ab5151eb2474f3bdd679f2f07bfd723042John Reck * You may obtain a copy of the License at
723b797ab5151eb2474f3bdd679f2f07bfd723042John Reck *
823b797ab5151eb2474f3bdd679f2f07bfd723042John Reck *      http://www.apache.org/licenses/LICENSE-2.0
923b797ab5151eb2474f3bdd679f2f07bfd723042John Reck *
1023b797ab5151eb2474f3bdd679f2f07bfd723042John Reck * Unless required by applicable law or agreed to in writing, software
1123b797ab5151eb2474f3bdd679f2f07bfd723042John Reck * distributed under the License is distributed on an "AS IS" BASIS,
1223b797ab5151eb2474f3bdd679f2f07bfd723042John Reck * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1323b797ab5151eb2474f3bdd679f2f07bfd723042John Reck * See the License for the specific language governing permissions and
1423b797ab5151eb2474f3bdd679f2f07bfd723042John Reck * limitations under the License.
1523b797ab5151eb2474f3bdd679f2f07bfd723042John Reck */
1623b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
1723b797ab5151eb2474f3bdd679f2f07bfd723042John Reck#include "CanvasContext.h"
1823b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
19998a6d81896df8b662cc10ddeb35087b78b38d72John Reck#include <algorithm>
204f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck#include <private/hwui/DrawGlInfo.h>
2123b797ab5151eb2474f3bdd679f2f07bfd723042John Reck#include <strings.h>
2223b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
233b20251a355c88193c439f928a84ae69483fb488John Reck#include "EglManager.h"
244f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck#include "RenderThread.h"
25119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck#include "../AnimationContext.h"
2623b797ab5151eb2474f3bdd679f2f07bfd723042John Reck#include "../Caches.h"
2719b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck#include "../DeferredLayerUpdater.h"
283b20251a355c88193c439f928a84ae69483fb488John Reck#include "../RenderState.h"
2919b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck#include "../LayerRenderer.h"
304f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck#include "../OpenGLRenderer.h"
3123b797ab5151eb2474f3bdd679f2f07bfd723042John Reck#include "../Stencil.h"
3223b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
33f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck#define TRIM_MEMORY_COMPLETE 80
34f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck#define TRIM_MEMORY_UI_HIDDEN 20
35f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck
3623b797ab5151eb2474f3bdd679f2f07bfd723042John Recknamespace android {
3723b797ab5151eb2474f3bdd679f2f07bfd723042John Recknamespace uirenderer {
3823b797ab5151eb2474f3bdd679f2f07bfd723042John Recknamespace renderthread {
3923b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
40119907cd2575c56b1ebf66348b52e67aaf6a88d8John ReckCanvasContext::CanvasContext(RenderThread& thread, bool translucent,
41119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck        RenderNode* rootRenderNode, IContextFactory* contextFactory)
423b20251a355c88193c439f928a84ae69483fb488John Reck        : mRenderThread(thread)
433b20251a355c88193c439f928a84ae69483fb488John Reck        , mEglManager(thread.eglManager())
444f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck        , mEglSurface(EGL_NO_SURFACE)
451125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck        , mBufferPreserved(false)
461125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck        , mSwapBehavior(kSwap_default)
474f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck        , mOpaque(!translucent)
483b20251a355c88193c439f928a84ae69483fb488John Reck        , mCanvas(NULL)
49e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck        , mHaveNewSurface(false)
50e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck        , mRootRenderNode(rootRenderNode) {
51119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    mAnimationContext = contextFactory->createAnimationContext(mRenderThread.timeLord());
52443a714fa7c0dd07fee3527cc5bc3d3ca1fb7d44John Reck    mRenderThread.renderState().registerCanvasContext(this);
5323b797ab5151eb2474f3bdd679f2f07bfd723042John Reck}
5423b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
5523b797ab5151eb2474f3bdd679f2f07bfd723042John ReckCanvasContext::~CanvasContext() {
5617035b0211a3c9d45ea46a99217a6acbe76e8fbeJohn Reck    destroy();
57119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    delete mAnimationContext;
58443a714fa7c0dd07fee3527cc5bc3d3ca1fb7d44John Reck    mRenderThread.renderState().unregisterCanvasContext(this);
594f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
604f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
6117035b0211a3c9d45ea46a99217a6acbe76e8fbeJohn Reckvoid CanvasContext::destroy() {
6217035b0211a3c9d45ea46a99217a6acbe76e8fbeJohn Reck    stopDrawing();
6317035b0211a3c9d45ea46a99217a6acbe76e8fbeJohn Reck    freePrefetechedLayers();
6417035b0211a3c9d45ea46a99217a6acbe76e8fbeJohn Reck    destroyHardwareResources();
65e2478d45ccbe5b6abb360ac9d44771b5f4a50bdeJohn Reck    mAnimationContext->destroy();
664f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    if (mCanvas) {
674f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck        delete mCanvas;
684f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck        mCanvas = 0;
694f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    }
7023b797ab5151eb2474f3bdd679f2f07bfd723042John Reck    setSurface(NULL);
7123b797ab5151eb2474f3bdd679f2f07bfd723042John Reck}
7223b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
73a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reckvoid CanvasContext::setSurface(ANativeWindow* window) {
74a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    mNativeWindow = window;
75a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck
7623b797ab5151eb2474f3bdd679f2f07bfd723042John Reck    if (mEglSurface != EGL_NO_SURFACE) {
773b20251a355c88193c439f928a84ae69483fb488John Reck        mEglManager.destroySurface(mEglSurface);
7823b797ab5151eb2474f3bdd679f2f07bfd723042John Reck        mEglSurface = EGL_NO_SURFACE;
7923b797ab5151eb2474f3bdd679f2f07bfd723042John Reck    }
8023b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
8123b797ab5151eb2474f3bdd679f2f07bfd723042John Reck    if (window) {
823b20251a355c88193c439f928a84ae69483fb488John Reck        mEglSurface = mEglManager.createSurface(window);
8323b797ab5151eb2474f3bdd679f2f07bfd723042John Reck    }
8423b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
8523b797ab5151eb2474f3bdd679f2f07bfd723042John Reck    if (mEglSurface != EGL_NO_SURFACE) {
861125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck        const bool preserveBuffer = (mSwapBehavior != kSwap_discardBuffer);
871125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck        mBufferPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer);
884f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck        mHaveNewSurface = true;
89dbc9a86d05e5e835051de22f6cb30ec1921e9705John Reck        makeCurrent();
90368cdd85268999997fb495cf90c4417221797de0John Reck    } else {
91368cdd85268999997fb495cf90c4417221797de0John Reck        mRenderThread.removeFrameCallback(this);
924f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    }
934f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
944f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
954f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reckvoid CanvasContext::swapBuffers() {
962cdbc7d2283aae3d77b12c8fdbba8ca4bd3db5eaJohn Reck    if (CC_UNLIKELY(!mEglManager.swapBuffers(mEglSurface))) {
972cdbc7d2283aae3d77b12c8fdbba8ca4bd3db5eaJohn Reck        setSurface(NULL);
982cdbc7d2283aae3d77b12c8fdbba8ca4bd3db5eaJohn Reck    }
994f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    mHaveNewSurface = false;
1004f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
1014f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
102f7d9c1dc84671d4e99657ef071d275700d85bb11John Reckvoid CanvasContext::requireSurface() {
103f7d9c1dc84671d4e99657ef071d275700d85bb11John Reck    LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
104f7d9c1dc84671d4e99657ef071d275700d85bb11John Reck            "requireSurface() called but no surface set!");
105dbc9a86d05e5e835051de22f6cb30ec1921e9705John Reck    makeCurrent();
1064f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
1074f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
1081125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reckvoid CanvasContext::setSwapBehavior(SwapBehavior swapBehavior) {
1091125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck    mSwapBehavior = swapBehavior;
1101125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck}
1111125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck
112a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reckbool CanvasContext::initialize(ANativeWindow* window) {
1134f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    setSurface(window);
1142cdbc7d2283aae3d77b12c8fdbba8ca4bd3db5eaJohn Reck    if (mCanvas) return false;
1153b20251a355c88193c439f928a84ae69483fb488John Reck    mCanvas = new OpenGLRenderer(mRenderThread.renderState());
1164f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    mCanvas->initProperties();
1174f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    return true;
1184f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
1194f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
120a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reckvoid CanvasContext::updateSurface(ANativeWindow* window) {
1214f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    setSurface(window);
122f7d9c1dc84671d4e99657ef071d275700d85bb11John Reck}
123f7d9c1dc84671d4e99657ef071d275700d85bb11John Reck
124a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reckvoid CanvasContext::pauseSurface(ANativeWindow* window) {
1251661715d4066a557ab0877271d62762579a38fa9John Reck    stopDrawing();
1264f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
1274f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
128284b24358410cb0200e525a5ba36994090c83f20Chris Craik// TODO: don't pass viewport size, it's automatic via EGL
129058fc640017c90120c599d378a4cbc55668b05b7Chris Craikvoid CanvasContext::setup(int width, int height, const Vector3& lightCenter, float lightRadius,
130058fc640017c90120c599d378a4cbc55668b05b7Chris Craik        uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
1314f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    if (!mCanvas) return;
132058fc640017c90120c599d378a4cbc55668b05b7Chris Craik    mCanvas->initLight(lightCenter, lightRadius, ambientShadowAlpha, spotShadowAlpha);
1334f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
1344f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
13563a06673253914510bbeebd500655008682dade1John Reckvoid CanvasContext::setOpaque(bool opaque) {
13663a06673253914510bbeebd500655008682dade1John Reck    mOpaque = opaque;
13763a06673253914510bbeebd500655008682dade1John Reck}
13863a06673253914510bbeebd500655008682dade1John Reck
139860d155f866cc15a725e7ce03763280987f24901John Reckvoid CanvasContext::makeCurrent() {
140dbc9a86d05e5e835051de22f6cb30ec1921e9705John Reck    // TODO: Figure out why this workaround is needed, see b/13913604
141dbc9a86d05e5e835051de22f6cb30ec1921e9705John Reck    // In the meantime this matches the behavior of GLRenderer, so it is not a regression
1423b20251a355c88193c439f928a84ae69483fb488John Reck    mHaveNewSurface |= mEglManager.makeCurrent(mEglSurface);
143860d155f866cc15a725e7ce03763280987f24901John Reck}
144860d155f866cc15a725e7ce03763280987f24901John Reck
14568bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reckvoid CanvasContext::processLayerUpdate(DeferredLayerUpdater* layerUpdater) {
14668bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck    bool success = layerUpdater->apply();
147d72e0a339b54af0c4e731513bbad120dff694723John Reck    LOG_ALWAYS_FATAL_IF(!success, "Failed to update layer!");
148d72e0a339b54af0c4e731513bbad120dff694723John Reck    if (layerUpdater->backingLayer()->deferredUpdateScheduled) {
149d72e0a339b54af0c4e731513bbad120dff694723John Reck        mCanvas->pushLayerUpdate(layerUpdater->backingLayer());
15019b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck    }
15119b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck}
15219b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck
153e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reckvoid CanvasContext::prepareTree(TreeInfo& info) {
154f9be77940e365036fecd8cc0e491e8545c34e79bJohn Reck    mRenderThread.removeFrameCallback(this);
15518f16e6fba74eda173e1e7c869e6e2e2acc073ffJohn Reck
156e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck    info.damageAccumulator = &mDamageAccumulator;
15725fbb3fa1138675379102a44405852555cefccbdJohn Reck    info.renderer = mCanvas;
158998a6d81896df8b662cc10ddeb35087b78b38d72John Reck    if (mPrefetechedLayers.size() && info.mode == TreeInfo::MODE_FULL) {
159998a6d81896df8b662cc10ddeb35087b78b38d72John Reck        info.canvasContext = this;
160998a6d81896df8b662cc10ddeb35087b78b38d72John Reck    }
161ec845a215e343cdb3b2e4c7b6aff7b24beb0236bJohn Reck    mAnimationContext->startFrame(info.mode);
162e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck    mRootRenderNode->prepareTree(info);
163119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    mAnimationContext->runRemainingAnimations(info);
164e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck
165998a6d81896df8b662cc10ddeb35087b78b38d72John Reck    if (info.canvasContext) {
166998a6d81896df8b662cc10ddeb35087b78b38d72John Reck        freePrefetechedLayers();
167998a6d81896df8b662cc10ddeb35087b78b38d72John Reck    }
168998a6d81896df8b662cc10ddeb35087b78b38d72John Reck
169a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    int runningBehind = 0;
170a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    // TODO: This query is moderately expensive, investigate adding some sort
171a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    // of fast-path based off when we last called eglSwapBuffers() as well as
172a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    // last vsync time. Or something.
173a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    mNativeWindow->query(mNativeWindow.get(),
174a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck            NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &runningBehind);
175a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    info.out.canDrawThisFrame = !runningBehind;
176a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck
177a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    if (info.out.hasAnimations || !info.out.canDrawThisFrame) {
178cd028f336e36b22dbe8cf623eb5bd2361314495cJohn Reck        if (!info.out.requiresUiRedraw) {
179f9be77940e365036fecd8cc0e491e8545c34e79bJohn Reck            // If animationsNeedsRedraw is set don't bother posting for an RT anim
180f9be77940e365036fecd8cc0e491e8545c34e79bJohn Reck            // as we will just end up fighting the UI thread.
181f9be77940e365036fecd8cc0e491e8545c34e79bJohn Reck            mRenderThread.postFrameCallback(this);
182f9be77940e365036fecd8cc0e491e8545c34e79bJohn Reck        }
183e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck    }
184e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck}
185e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck
186f47a594f5250b1914c36423ee6b371f0b8db09d0John Reckvoid CanvasContext::stopDrawing() {
187f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck    mRenderThread.removeFrameCallback(this);
188f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck}
189f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck
190a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reckvoid CanvasContext::notifyFramePending() {
191a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    ATRACE_CALL();
192a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    mRenderThread.pushBackFrameCallback(this);
193a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck}
194a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck
195e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reckvoid CanvasContext::draw() {
1964f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    LOG_ALWAYS_FATAL_IF(!mCanvas || mEglSurface == EGL_NO_SURFACE,
197a7090e0cfd7c719a6d4c03aae34f5db98754cbddChris Craik            "drawRenderNode called on a context with no canvas or surface!");
1984f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
199fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck    profiler().markPlaybackStart();
200fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck
201e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck    SkRect dirty;
202e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck    mDamageAccumulator.finish(&dirty);
203e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck
2044f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    EGLint width, height;
2053b20251a355c88193c439f928a84ae69483fb488John Reck    mEglManager.beginFrame(mEglSurface, &width, &height);
2064f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    if (width != mCanvas->getViewportWidth() || height != mCanvas->getViewportHeight()) {
2074f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck        mCanvas->setViewport(width, height);
208e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck        dirty.setEmpty();
2091125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck    } else if (!mBufferPreserved || mHaveNewSurface) {
210e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck        dirty.setEmpty();
211fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck    } else {
2125cdb8f998c58a2226112b36e4c391866346e5e17John Reck        if (!dirty.isEmpty() && !dirty.intersect(0, 0, width, height)) {
2130a97330b98dd633b58dcfff405d94476c89e867dJohn Reck            ALOGW("Dirty " RECT_STRING " doesn't intersect with 0 0 %d %d ?",
2140a97330b98dd633b58dcfff405d94476c89e867dJohn Reck                    SK_RECT_ARGS(dirty), width, height);
2150a97330b98dd633b58dcfff405d94476c89e867dJohn Reck            dirty.setEmpty();
2160a97330b98dd633b58dcfff405d94476c89e867dJohn Reck        }
217e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck        profiler().unionDirty(&dirty);
2184f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    }
2194f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
220e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck    if (!dirty.isEmpty()) {
221107843de4507b3511006cb9c77b8d0364374385aTom Hudson        mCanvas->prepareDirty(dirty.fLeft, dirty.fTop,
222e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck                dirty.fRight, dirty.fBottom, mOpaque);
2234f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    } else {
224107843de4507b3511006cb9c77b8d0364374385aTom Hudson        mCanvas->prepare(mOpaque);
2254f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    }
2264f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
2274f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    Rect outBounds;
228107843de4507b3511006cb9c77b8d0364374385aTom Hudson    mCanvas->drawRenderNode(mRootRenderNode.get(), outBounds);
2294f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
230fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck    profiler().draw(mCanvas);
2314f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
232107843de4507b3511006cb9c77b8d0364374385aTom Hudson    bool drew = mCanvas->finish();
2334f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
234fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck    profiler().markPlaybackEnd();
235fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck
236107843de4507b3511006cb9c77b8d0364374385aTom Hudson    if (drew) {
2374f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck        swapBuffers();
2380e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck    } else {
2390e89e2b7bcb2c035e8cee77f93120e7c5617f8d2John Reck        mEglManager.cancelFrame();
24023b797ab5151eb2474f3bdd679f2f07bfd723042John Reck    }
241fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck
242fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck    profiler().finishFrame();
24323b797ab5151eb2474f3bdd679f2f07bfd723042John Reck}
24423b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
245e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck// Called by choreographer to do an RT-driven animation
24618f16e6fba74eda173e1e7c869e6e2e2acc073ffJohn Reckvoid CanvasContext::doFrame() {
247368cdd85268999997fb495cf90c4417221797de0John Reck    if (CC_UNLIKELY(!mCanvas || mEglSurface == EGL_NO_SURFACE)) {
248368cdd85268999997fb495cf90c4417221797de0John Reck        return;
249368cdd85268999997fb495cf90c4417221797de0John Reck    }
250368cdd85268999997fb495cf90c4417221797de0John Reck
251e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck    ATRACE_CALL();
252e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck
253fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck    profiler().startFrame();
254fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck
2553b20251a355c88193c439f928a84ae69483fb488John Reck    TreeInfo info(TreeInfo::MODE_RT_ONLY, mRenderThread.renderState());
256e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck    prepareTree(info);
257a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    if (info.out.canDrawThisFrame) {
258e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck        draw();
259a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    }
260e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck}
261e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck
2623b20251a355c88193c439f928a84ae69483fb488John Reckvoid CanvasContext::invokeFunctor(RenderThread& thread, Functor* functor) {
263d3d8dafc2f61fb118c060720b52684c59303f3dbJohn Reck    ATRACE_CALL();
2640d1f634f4b5e1bb37aa51777efb6a68619488d01John Reck    DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
2653b20251a355c88193c439f928a84ae69483fb488John Reck    if (thread.eglManager().hasEglContext()) {
2663b20251a355c88193c439f928a84ae69483fb488John Reck        thread.eglManager().requireGlContext();
2670d1f634f4b5e1bb37aa51777efb6a68619488d01John Reck        mode = DrawGlInfo::kModeProcess;
2680d1f634f4b5e1bb37aa51777efb6a68619488d01John Reck    }
2696f07a0dc875a9eac67312085a8e0133b9e2f4771John Reck
2703b20251a355c88193c439f928a84ae69483fb488John Reck    thread.renderState().invokeFunctor(functor, mode, NULL);
27123b797ab5151eb2474f3bdd679f2f07bfd723042John Reck}
27223b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
273998a6d81896df8b662cc10ddeb35087b78b38d72John Reckvoid CanvasContext::markLayerInUse(RenderNode* node) {
274998a6d81896df8b662cc10ddeb35087b78b38d72John Reck    if (mPrefetechedLayers.erase(node)) {
275998a6d81896df8b662cc10ddeb35087b78b38d72John Reck        node->decStrong(0);
276998a6d81896df8b662cc10ddeb35087b78b38d72John Reck    }
277998a6d81896df8b662cc10ddeb35087b78b38d72John Reck}
278998a6d81896df8b662cc10ddeb35087b78b38d72John Reck
279998a6d81896df8b662cc10ddeb35087b78b38d72John Reckstatic void destroyPrefetechedNode(RenderNode* node) {
280998a6d81896df8b662cc10ddeb35087b78b38d72John Reck    ALOGW("Incorrectly called buildLayer on View: %s, destroying layer...", node->getName());
281998a6d81896df8b662cc10ddeb35087b78b38d72John Reck    node->destroyHardwareResources();
282998a6d81896df8b662cc10ddeb35087b78b38d72John Reck    node->decStrong(0);
283998a6d81896df8b662cc10ddeb35087b78b38d72John Reck}
284998a6d81896df8b662cc10ddeb35087b78b38d72John Reck
285998a6d81896df8b662cc10ddeb35087b78b38d72John Reckvoid CanvasContext::freePrefetechedLayers() {
286998a6d81896df8b662cc10ddeb35087b78b38d72John Reck    if (mPrefetechedLayers.size()) {
287998a6d81896df8b662cc10ddeb35087b78b38d72John Reck        requireGlContext();
288998a6d81896df8b662cc10ddeb35087b78b38d72John Reck        std::for_each(mPrefetechedLayers.begin(), mPrefetechedLayers.end(), destroyPrefetechedNode);
289998a6d81896df8b662cc10ddeb35087b78b38d72John Reck        mPrefetechedLayers.clear();
290998a6d81896df8b662cc10ddeb35087b78b38d72John Reck    }
291998a6d81896df8b662cc10ddeb35087b78b38d72John Reck}
292998a6d81896df8b662cc10ddeb35087b78b38d72John Reck
2933e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reckvoid CanvasContext::buildLayer(RenderNode* node) {
2943e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    ATRACE_CALL();
2953e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    if (!mEglManager.hasEglContext() || !mCanvas) {
2963e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck        return;
2973e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    }
2983e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    requireGlContext();
2993e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    // buildLayer() will leave the tree in an unknown state, so we must stop drawing
3003e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    stopDrawing();
3013e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck
3023e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    TreeInfo info(TreeInfo::MODE_FULL, mRenderThread.renderState());
3033e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    info.damageAccumulator = &mDamageAccumulator;
3043e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    info.renderer = mCanvas;
3059eb9f6f8cbbbd87d45da8071aa54cb066a797723John Reck    info.runAnimations = false;
3063e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    node->prepareTree(info);
3073e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    SkRect ignore;
3083e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    mDamageAccumulator.finish(&ignore);
3093e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    // Tickle the GENERIC property on node to mark it as dirty for damaging
3103e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    // purposes when the frame is actually drawn
3113e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    node->setPropertyFieldsDirty(RenderNode::GENERIC);
3123e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck
313443a714fa7c0dd07fee3527cc5bc3d3ca1fb7d44John Reck    mCanvas->markLayersAsBuildLayers();
3143e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    mCanvas->flushLayerUpdates();
315998a6d81896df8b662cc10ddeb35087b78b38d72John Reck
316998a6d81896df8b662cc10ddeb35087b78b38d72John Reck    node->incStrong(0);
317998a6d81896df8b662cc10ddeb35087b78b38d72John Reck    mPrefetechedLayers.insert(node);
3183e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck}
3193e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck
32019b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reckbool CanvasContext::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
32119b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck    requireGlContext();
32268bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck    layer->apply();
3233b20251a355c88193c439f928a84ae69483fb488John Reck    return LayerRenderer::copyLayer(mRenderThread.renderState(), layer->backingLayer(), bitmap);
32419b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck}
32519b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck
326f47a594f5250b1914c36423ee6b371f0b8db09d0John Reckvoid CanvasContext::destroyHardwareResources() {
327f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck    stopDrawing();
3283b20251a355c88193c439f928a84ae69483fb488John Reck    if (mEglManager.hasEglContext()) {
329e1628b7c6fc3822fa83cf02028ce8ad67abb0afeJohn Reck        requireGlContext();
330dff9957cc22a1174a4cf91de6609c50934d29434John Reck        freePrefetechedLayers();
331dcba6725e8b9d3eba9ad7a01258d6aa974feafbaJohn Reck        mRootRenderNode->destroyHardwareResources();
332f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck        Caches::getInstance().flush(Caches::kFlushMode_Layers);
333f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck    }
334f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck}
335f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck
336f47a594f5250b1914c36423ee6b371f0b8db09d0John Reckvoid CanvasContext::trimMemory(RenderThread& thread, int level) {
337f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck    // No context means nothing to free
338f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck    if (!thread.eglManager().hasEglContext()) return;
339f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck
340786afcb3eec18315ec54987a08814ff28f13d09fJorim Jaggi    ATRACE_CALL();
3413c2b7fa8c584c5ed56f1bd6ad53f2e87f0a6eb44John Reck    thread.eglManager().requireGlContext();
342f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck    if (level >= TRIM_MEMORY_COMPLETE) {
343f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck        Caches::getInstance().flush(Caches::kFlushMode_Full);
344f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck        thread.eglManager().destroy();
345f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck    } else if (level >= TRIM_MEMORY_UI_HIDDEN) {
346f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck        Caches::getInstance().flush(Caches::kFlushMode_Moderate);
347e1628b7c6fc3822fa83cf02028ce8ad67abb0afeJohn Reck    }
348e1628b7c6fc3822fa83cf02028ce8ad67abb0afeJohn Reck}
349e1628b7c6fc3822fa83cf02028ce8ad67abb0afeJohn Reck
350fc53ef27793a39e9effd829e9cae02a9ca14147eJohn Reckvoid CanvasContext::runWithGlContext(RenderTask* task) {
35119b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck    requireGlContext();
35219b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck    task->run();
35319b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck}
35419b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck
3551949e7928eeec22cd3f74b5f763a4eb433238453John ReckLayer* CanvasContext::createTextureLayer() {
356f7d9c1dc84671d4e99657ef071d275700d85bb11John Reck    requireSurface();
3573b20251a355c88193c439f928a84ae69483fb488John Reck    return LayerRenderer::createTextureLayer(mRenderThread.renderState());
3581949e7928eeec22cd3f74b5f763a4eb433238453John Reck}
3591949e7928eeec22cd3f74b5f763a4eb433238453John Reck
36019b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reckvoid CanvasContext::requireGlContext() {
361f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck    mEglManager.requireGlContext();
362fc53ef27793a39e9effd829e9cae02a9ca14147eJohn Reck}
363fc53ef27793a39e9effd829e9cae02a9ca14147eJohn Reck
3643b20251a355c88193c439f928a84ae69483fb488John Reckvoid CanvasContext::setTextureAtlas(RenderThread& thread,
3653b20251a355c88193c439f928a84ae69483fb488John Reck        const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize) {
3663b20251a355c88193c439f928a84ae69483fb488John Reck    thread.eglManager().setTextureAtlas(buffer, map, mapSize);
36766f0be65a1046f54ddce0498b242c1fa0776b1eaJohn Reck}
36866f0be65a1046f54ddce0498b242c1fa0776b1eaJohn Reck
36923b797ab5151eb2474f3bdd679f2f07bfd723042John Reck} /* namespace renderthread */
37023b797ab5151eb2474f3bdd679f2f07bfd723042John Reck} /* namespace uirenderer */
37123b797ab5151eb2474f3bdd679f2f07bfd723042John Reck} /* namespace android */
372