CanvasContext.cpp revision 98787e6c9b2c10b1ab7820bdac168686025b924a
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
19d04794a9a3f9edc8b7ca336175d66eb81a8f55faJohn Reck#include "AnimationContext.h"
20d04794a9a3f9edc8b7ca336175d66eb81a8f55faJohn Reck#include "Caches.h"
21d04794a9a3f9edc8b7ca336175d66eb81a8f55faJohn Reck#include "DeferredLayerUpdater.h"
223b20251a355c88193c439f928a84ae69483fb488John Reck#include "EglManager.h"
230b7e8245db728d127ada698be63d78b33fc6e4daChris Craik#include "LayerUpdateQueue.h"
24d04794a9a3f9edc8b7ca336175d66eb81a8f55faJohn Reck#include "LayerRenderer.h"
25d04794a9a3f9edc8b7ca336175d66eb81a8f55faJohn Reck#include "OpenGLRenderer.h"
26d04794a9a3f9edc8b7ca336175d66eb81a8f55faJohn Reck#include "Properties.h"
274f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck#include "RenderThread.h"
28d04794a9a3f9edc8b7ca336175d66eb81a8f55faJohn Reck#include "renderstate/RenderState.h"
29d04794a9a3f9edc8b7ca336175d66eb81a8f55faJohn Reck#include "renderstate/Stencil.h"
30e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck#include "protos/hwui.pb.h"
31e486d932ca5a10446a3c98d6d065213913277268John Reck#include "utils/TimeUtils.h"
3223b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
33b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik#if HWUI_NEW_OPS
34b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik#include "OpReorderer.h"
35b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik#endif
36b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
37240ff6246a29602539fd0295274e1c769e743a2eJohn Reck#include <cutils/properties.h>
38e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck#include <google/protobuf/io/zero_copy_stream_impl.h>
3965fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik#include <private/hwui/DrawGlInfo.h>
40e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck#include <strings.h>
41e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck
42e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck#include <algorithm>
43e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck#include <fcntl.h>
44e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck#include <sys/stat.h>
4565fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik
46290b23a1e11d532b39098bb58693ef97ba98a622Colin Cross#include <cstdlib>
47290b23a1e11d532b39098bb58693ef97ba98a622Colin Cross
48f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck#define TRIM_MEMORY_COMPLETE 80
49f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck#define TRIM_MEMORY_UI_HIDDEN 20
50f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck
51e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck#define ENABLE_RENDERNODE_SERIALIZATION false
52e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck
53149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck#define LOG_FRAMETIME_MMA 0
54149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck
55149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck#if LOG_FRAMETIME_MMA
56149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reckstatic float sBenchMma = 0;
57149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reckstatic int sFrameCount = 0;
58149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reckstatic const float NANOS_PER_MILLIS_F = 1000000.0f;
59149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck#endif
60149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck
6123b797ab5151eb2474f3bdd679f2f07bfd723042John Recknamespace android {
6223b797ab5151eb2474f3bdd679f2f07bfd723042John Recknamespace uirenderer {
6323b797ab5151eb2474f3bdd679f2f07bfd723042John Recknamespace renderthread {
6423b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
65119907cd2575c56b1ebf66348b52e67aaf6a88d8John ReckCanvasContext::CanvasContext(RenderThread& thread, bool translucent,
66119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck        RenderNode* rootRenderNode, IContextFactory* contextFactory)
673b20251a355c88193c439f928a84ae69483fb488John Reck        : mRenderThread(thread)
683b20251a355c88193c439f928a84ae69483fb488John Reck        , mEglManager(thread.eglManager())
694f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck        , mOpaque(!translucent)
7051d6a3db97bdd5315f1a17a4b447d10a92217b98Chris Craik        , mAnimationContext(contextFactory->createAnimationContext(mRenderThread.timeLord()))
714c9e59d03c2bca38001225b79d01740b8999adfbJohn Reck        , mJankTracker(thread.timeLord().frameIntervalNanos())
72ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne        , mProfiler(mFrames)
73b816087962aba0019b022303330f03b897b580edSkuhne        , mContentDrawBounds(0, 0, 0, 0) {
74ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    mRenderNodes.emplace_back(rootRenderNode);
75443a714fa7c0dd07fee3527cc5bc3d3ca1fb7d44John Reck    mRenderThread.renderState().registerCanvasContext(this);
76b36016c65f1d1b5846dba0349aab491dbd3a746aJohn Reck    mProfiler.setDensity(mRenderThread.mainDisplayInfo().density);
7723b797ab5151eb2474f3bdd679f2f07bfd723042John Reck}
7823b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
7923b797ab5151eb2474f3bdd679f2f07bfd723042John ReckCanvasContext::~CanvasContext() {
8017035b0211a3c9d45ea46a99217a6acbe76e8fbeJohn Reck    destroy();
81443a714fa7c0dd07fee3527cc5bc3d3ca1fb7d44John Reck    mRenderThread.renderState().unregisterCanvasContext(this);
824f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
834f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
8417035b0211a3c9d45ea46a99217a6acbe76e8fbeJohn Reckvoid CanvasContext::destroy() {
8517035b0211a3c9d45ea46a99217a6acbe76e8fbeJohn Reck    stopDrawing();
86d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    setSurface(nullptr);
8717035b0211a3c9d45ea46a99217a6acbe76e8fbeJohn Reck    freePrefetechedLayers();
8817035b0211a3c9d45ea46a99217a6acbe76e8fbeJohn Reck    destroyHardwareResources();
89e2478d45ccbe5b6abb360ac9d44771b5f4a50bdeJohn Reck    mAnimationContext->destroy();
904f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    if (mCanvas) {
914f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck        delete mCanvas;
92d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik        mCanvas = nullptr;
934f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    }
9423b797ab5151eb2474f3bdd679f2f07bfd723042John Reck}
9523b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
96a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reckvoid CanvasContext::setSurface(ANativeWindow* window) {
97fbc8df03e498baf47ff1a5e05e182f1bcd60c770John Reck    ATRACE_CALL();
98fbc8df03e498baf47ff1a5e05e182f1bcd60c770John Reck
99a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    mNativeWindow = window;
100a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck
10123b797ab5151eb2474f3bdd679f2f07bfd723042John Reck    if (mEglSurface != EGL_NO_SURFACE) {
1023b20251a355c88193c439f928a84ae69483fb488John Reck        mEglManager.destroySurface(mEglSurface);
10323b797ab5151eb2474f3bdd679f2f07bfd723042John Reck        mEglSurface = EGL_NO_SURFACE;
10423b797ab5151eb2474f3bdd679f2f07bfd723042John Reck    }
10523b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
10623b797ab5151eb2474f3bdd679f2f07bfd723042John Reck    if (window) {
1073b20251a355c88193c439f928a84ae69483fb488John Reck        mEglSurface = mEglManager.createSurface(window);
10823b797ab5151eb2474f3bdd679f2f07bfd723042John Reck    }
10923b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
11023b797ab5151eb2474f3bdd679f2f07bfd723042John Reck    if (mEglSurface != EGL_NO_SURFACE) {
1111125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck        const bool preserveBuffer = (mSwapBehavior != kSwap_discardBuffer);
1121125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck        mBufferPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer);
1134f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck        mHaveNewSurface = true;
114e486d932ca5a10446a3c98d6d065213913277268John Reck        mSwapHistory.clear();
115dbc9a86d05e5e835051de22f6cb30ec1921e9705John Reck        makeCurrent();
116368cdd85268999997fb495cf90c4417221797de0John Reck    } else {
117368cdd85268999997fb495cf90c4417221797de0John Reck        mRenderThread.removeFrameCallback(this);
1184f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    }
1194f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
1204f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
121f7d9c1dc84671d4e99657ef071d275700d85bb11John Reckvoid CanvasContext::requireSurface() {
122f7d9c1dc84671d4e99657ef071d275700d85bb11John Reck    LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
123f7d9c1dc84671d4e99657ef071d275700d85bb11John Reck            "requireSurface() called but no surface set!");
124dbc9a86d05e5e835051de22f6cb30ec1921e9705John Reck    makeCurrent();
1254f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
1264f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
1271125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reckvoid CanvasContext::setSwapBehavior(SwapBehavior swapBehavior) {
1281125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck    mSwapBehavior = swapBehavior;
1291125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck}
1301125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck
131a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reckbool CanvasContext::initialize(ANativeWindow* window) {
1324f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    setSurface(window);
133b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik#if !HWUI_NEW_OPS
1342cdbc7d2283aae3d77b12c8fdbba8ca4bd3db5eaJohn Reck    if (mCanvas) return false;
1353b20251a355c88193c439f928a84ae69483fb488John Reck    mCanvas = new OpenGLRenderer(mRenderThread.renderState());
1364f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    mCanvas->initProperties();
137b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik#endif
1384f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    return true;
1394f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
1404f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
141a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reckvoid CanvasContext::updateSurface(ANativeWindow* window) {
1424f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    setSurface(window);
143f7d9c1dc84671d4e99657ef071d275700d85bb11John Reck}
144f7d9c1dc84671d4e99657ef071d275700d85bb11John Reck
1459fb42f07784ac9e1ab29fa7d5bcda6c3081d238fJohn Reckbool CanvasContext::pauseSurface(ANativeWindow* window) {
14601a5ea35fbba4c5bb1d7790ae1677a2fa752e042John Reck    return mRenderThread.removeFrameCallback(this);
1474f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
1484f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
149284b24358410cb0200e525a5ba36994090c83f20Chris Craik// TODO: don't pass viewport size, it's automatic via EGL
15050210d912925aef14e4ce69be82e4949122a3cd9Alan Viverettevoid CanvasContext::setup(int width, int height, float lightRadius,
15164bb413a664001c95c8439cf097dc3033f4ed733Andreas Gampe        uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
15298787e6c9b2c10b1ab7820bdac168686025b924aChris Craik#if HWUI_NEW_OPS
15398787e6c9b2c10b1ab7820bdac168686025b924aChris Craik    mLightInfo.lightRadius = lightRadius;
15498787e6c9b2c10b1ab7820bdac168686025b924aChris Craik    mLightInfo.ambientShadowAlpha = ambientShadowAlpha;
15598787e6c9b2c10b1ab7820bdac168686025b924aChris Craik    mLightInfo.spotShadowAlpha = spotShadowAlpha;
15698787e6c9b2c10b1ab7820bdac168686025b924aChris Craik#else
1574f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    if (!mCanvas) return;
15850210d912925aef14e4ce69be82e4949122a3cd9Alan Viverette    mCanvas->initLight(lightRadius, ambientShadowAlpha, spotShadowAlpha);
15998787e6c9b2c10b1ab7820bdac168686025b924aChris Craik#endif
16050210d912925aef14e4ce69be82e4949122a3cd9Alan Viverette}
16150210d912925aef14e4ce69be82e4949122a3cd9Alan Viverette
16250210d912925aef14e4ce69be82e4949122a3cd9Alan Viverettevoid CanvasContext::setLightCenter(const Vector3& lightCenter) {
16398787e6c9b2c10b1ab7820bdac168686025b924aChris Craik#if HWUI_NEW_OPS
16498787e6c9b2c10b1ab7820bdac168686025b924aChris Craik    mLightCenter = lightCenter;
16598787e6c9b2c10b1ab7820bdac168686025b924aChris Craik#else
16650210d912925aef14e4ce69be82e4949122a3cd9Alan Viverette    if (!mCanvas) return;
16750210d912925aef14e4ce69be82e4949122a3cd9Alan Viverette    mCanvas->setLightCenter(lightCenter);
16898787e6c9b2c10b1ab7820bdac168686025b924aChris Craik#endif
1694f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
1704f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
17163a06673253914510bbeebd500655008682dade1John Reckvoid CanvasContext::setOpaque(bool opaque) {
17263a06673253914510bbeebd500655008682dade1John Reck    mOpaque = opaque;
17363a06673253914510bbeebd500655008682dade1John Reck}
17463a06673253914510bbeebd500655008682dade1John Reck
175860d155f866cc15a725e7ce03763280987f24901John Reckvoid CanvasContext::makeCurrent() {
176dbc9a86d05e5e835051de22f6cb30ec1921e9705John Reck    // TODO: Figure out why this workaround is needed, see b/13913604
177dbc9a86d05e5e835051de22f6cb30ec1921e9705John Reck    // In the meantime this matches the behavior of GLRenderer, so it is not a regression
178f2dcc2aecb94e726096256c47b913ed0a57ae7e2John Reck    EGLint error = 0;
179f2dcc2aecb94e726096256c47b913ed0a57ae7e2John Reck    mHaveNewSurface |= mEglManager.makeCurrent(mEglSurface, &error);
180f2dcc2aecb94e726096256c47b913ed0a57ae7e2John Reck    if (error) {
181f2dcc2aecb94e726096256c47b913ed0a57ae7e2John Reck        setSurface(nullptr);
182f2dcc2aecb94e726096256c47b913ed0a57ae7e2John Reck    }
183860d155f866cc15a725e7ce03763280987f24901John Reck}
184860d155f866cc15a725e7ce03763280987f24901John Reck
18568bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reckvoid CanvasContext::processLayerUpdate(DeferredLayerUpdater* layerUpdater) {
186b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik#if !HWUI_NEW_OPS
18768bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck    bool success = layerUpdater->apply();
188d72e0a339b54af0c4e731513bbad120dff694723John Reck    LOG_ALWAYS_FATAL_IF(!success, "Failed to update layer!");
189d72e0a339b54af0c4e731513bbad120dff694723John Reck    if (layerUpdater->backingLayer()->deferredUpdateScheduled) {
190d72e0a339b54af0c4e731513bbad120dff694723John Reck        mCanvas->pushLayerUpdate(layerUpdater->backingLayer());
19119b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck    }
192b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik#endif
19319b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck}
19419b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck
195bf3c602284f9a344faf185c3a5e94a264ba44c4fJohn Reckstatic bool wasSkipped(FrameInfo* info) {
1961b54fb27ac48495ed0b33868fda5776fb49fe0f3Chris Craik    return info && ((*info)[FrameInfoIndex::Flags] & FrameInfoFlags::SkippedFrame);
197bf3c602284f9a344faf185c3a5e94a264ba44c4fJohn Reck}
198bf3c602284f9a344faf185c3a5e94a264ba44c4fJohn Reck
199ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhnevoid CanvasContext::prepareTree(TreeInfo& info, int64_t* uiFrameInfo,
200ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne        int64_t syncQueued, RenderNode* target) {
201f9be77940e365036fecd8cc0e491e8545c34e79bJohn Reck    mRenderThread.removeFrameCallback(this);
20218f16e6fba74eda173e1e7c869e6e2e2acc073ffJohn Reck
203bf3c602284f9a344faf185c3a5e94a264ba44c4fJohn Reck    // If the previous frame was dropped we don't need to hold onto it, so
204bf3c602284f9a344faf185c3a5e94a264ba44c4fJohn Reck    // just keep using the previous frame's structure instead
205bf3c602284f9a344faf185c3a5e94a264ba44c4fJohn Reck    if (!wasSkipped(mCurrentFrameInfo)) {
206bf3c602284f9a344faf185c3a5e94a264ba44c4fJohn Reck        mCurrentFrameInfo = &mFrames.next();
207bf3c602284f9a344faf185c3a5e94a264ba44c4fJohn Reck    }
208ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    mCurrentFrameInfo->importUiThreadInfo(uiFrameInfo);
209be3fba05e823f740f65b2679929347dc3dd282adJohn Reck    mCurrentFrameInfo->set(FrameInfoIndex::SyncQueued) = syncQueued;
210ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    mCurrentFrameInfo->markSyncStart();
211ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck
212e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck    info.damageAccumulator = &mDamageAccumulator;
2130b7e8245db728d127ada698be63d78b33fc6e4daChris Craik#if HWUI_NEW_OPS
2140b7e8245db728d127ada698be63d78b33fc6e4daChris Craik    info.layerUpdateQueue = &mLayerUpdateQueue;
2150b7e8245db728d127ada698be63d78b33fc6e4daChris Craik#else
21625fbb3fa1138675379102a44405852555cefccbdJohn Reck    info.renderer = mCanvas;
2170b7e8245db728d127ada698be63d78b33fc6e4daChris Craik#endif
21800e79c9947b741194ff6c0d08ede9b3befbf9c9dJohn Reck
219ec845a215e343cdb3b2e4c7b6aff7b24beb0236bJohn Reck    mAnimationContext->startFrame(info.mode);
220ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    for (const sp<RenderNode>& node : mRenderNodes) {
221ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne        // Only the primary target node will be drawn full - all other nodes would get drawn in
222ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne        // real time mode. In case of a window, the primary node is the window content and the other
223ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne        // node(s) are non client / filler nodes.
224ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne        info.mode = (node.get() == target ? TreeInfo::MODE_FULL : TreeInfo::MODE_RT_ONLY);
225ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne        node->prepareTree(info);
226ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    }
227119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    mAnimationContext->runRemainingAnimations(info);
228e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck
22900e79c9947b741194ff6c0d08ede9b3befbf9c9dJohn Reck    freePrefetechedLayers();
230998a6d81896df8b662cc10ddeb35087b78b38d72John Reck
231aa95a88327d9a3ac8a4a00b065b78ac0f28b3a19John Reck    if (CC_UNLIKELY(!mNativeWindow.get())) {
2321b54fb27ac48495ed0b33868fda5776fb49fe0f3Chris Craik        mCurrentFrameInfo->addFlag(FrameInfoFlags::SkippedFrame);
233aa95a88327d9a3ac8a4a00b065b78ac0f28b3a19John Reck        info.out.canDrawThisFrame = false;
234aa95a88327d9a3ac8a4a00b065b78ac0f28b3a19John Reck        return;
235aa95a88327d9a3ac8a4a00b065b78ac0f28b3a19John Reck    }
236aa95a88327d9a3ac8a4a00b065b78ac0f28b3a19John Reck
237e486d932ca5a10446a3c98d6d065213913277268John Reck    if (CC_LIKELY(mSwapHistory.size())) {
238e486d932ca5a10446a3c98d6d065213913277268John Reck        nsecs_t latestVsync = mRenderThread.timeLord().latestVsync();
239e486d932ca5a10446a3c98d6d065213913277268John Reck        const SwapHistory& lastSwap = mSwapHistory.back();
240e486d932ca5a10446a3c98d6d065213913277268John Reck        int vsyncDelta = std::abs(lastSwap.vsyncTime - latestVsync);
241e486d932ca5a10446a3c98d6d065213913277268John Reck        // The slight fudge-factor is to deal with cases where
242e486d932ca5a10446a3c98d6d065213913277268John Reck        // the vsync was estimated due to being slow handling the signal.
243e486d932ca5a10446a3c98d6d065213913277268John Reck        // See the logic in TimeLord#computeFrameTimeNanos or in
244e486d932ca5a10446a3c98d6d065213913277268John Reck        // Choreographer.java for details on when this happens
245e486d932ca5a10446a3c98d6d065213913277268John Reck        if (vsyncDelta < 2_ms) {
246e486d932ca5a10446a3c98d6d065213913277268John Reck            // Already drew for this vsync pulse, UI draw request missed
247e486d932ca5a10446a3c98d6d065213913277268John Reck            // the deadline for RT animations
248e486d932ca5a10446a3c98d6d065213913277268John Reck            info.out.canDrawThisFrame = false;
249e486d932ca5a10446a3c98d6d065213913277268John Reck        } else if (lastSwap.swapTime < latestVsync) {
250e486d932ca5a10446a3c98d6d065213913277268John Reck            info.out.canDrawThisFrame = true;
251e486d932ca5a10446a3c98d6d065213913277268John Reck        } else {
252e486d932ca5a10446a3c98d6d065213913277268John Reck            // We're maybe behind? Find out for sure
253e486d932ca5a10446a3c98d6d065213913277268John Reck            int runningBehind = 0;
254e486d932ca5a10446a3c98d6d065213913277268John Reck            mNativeWindow->query(mNativeWindow.get(),
255e486d932ca5a10446a3c98d6d065213913277268John Reck                    NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &runningBehind);
256e486d932ca5a10446a3c98d6d065213913277268John Reck            info.out.canDrawThisFrame = !runningBehind;
257e486d932ca5a10446a3c98d6d065213913277268John Reck        }
258e486d932ca5a10446a3c98d6d065213913277268John Reck    } else {
259e486d932ca5a10446a3c98d6d065213913277268John Reck        info.out.canDrawThisFrame = true;
260e486d932ca5a10446a3c98d6d065213913277268John Reck    }
261a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck
262aef9dc8d186bd5f78068ab2d5240b5e9c8ab44b6John Reck    if (!info.out.canDrawThisFrame) {
2631b54fb27ac48495ed0b33868fda5776fb49fe0f3Chris Craik        mCurrentFrameInfo->addFlag(FrameInfoFlags::SkippedFrame);
264aef9dc8d186bd5f78068ab2d5240b5e9c8ab44b6John Reck    }
265aef9dc8d186bd5f78068ab2d5240b5e9c8ab44b6John Reck
266a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    if (info.out.hasAnimations || !info.out.canDrawThisFrame) {
267cd028f336e36b22dbe8cf623eb5bd2361314495cJohn Reck        if (!info.out.requiresUiRedraw) {
268f9be77940e365036fecd8cc0e491e8545c34e79bJohn Reck            // If animationsNeedsRedraw is set don't bother posting for an RT anim
269f9be77940e365036fecd8cc0e491e8545c34e79bJohn Reck            // as we will just end up fighting the UI thread.
270f9be77940e365036fecd8cc0e491e8545c34e79bJohn Reck            mRenderThread.postFrameCallback(this);
271f9be77940e365036fecd8cc0e491e8545c34e79bJohn Reck        }
272e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck    }
273e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck}
274e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck
275f47a594f5250b1914c36423ee6b371f0b8db09d0John Reckvoid CanvasContext::stopDrawing() {
276f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck    mRenderThread.removeFrameCallback(this);
277f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck}
278f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck
279a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reckvoid CanvasContext::notifyFramePending() {
280a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    ATRACE_CALL();
281a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    mRenderThread.pushBackFrameCallback(this);
282a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck}
283a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck
284e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reckvoid CanvasContext::draw() {
285b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik#if !HWUI_NEW_OPS
2864f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    LOG_ALWAYS_FATAL_IF(!mCanvas || mEglSurface == EGL_NO_SURFACE,
287a7090e0cfd7c719a6d4c03aae34f5db98754cbddChris Craik            "drawRenderNode called on a context with no canvas or surface!");
288b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik#endif
2894f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
290e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck    SkRect dirty;
291e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck    mDamageAccumulator.finish(&dirty);
292e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck
2936d4d0db312fa8a9fce629dd92aa133c6f8249cf8John Reck    // TODO: Re-enable after figuring out cause of b/22592975
2946d4d0db312fa8a9fce629dd92aa133c6f8249cf8John Reck//    if (dirty.isEmpty() && Properties::skipEmptyFrames) {
2956d4d0db312fa8a9fce629dd92aa133c6f8249cf8John Reck//        mCurrentFrameInfo->addFlag(FrameInfoFlags::SkippedFrame);
2966d4d0db312fa8a9fce629dd92aa133c6f8249cf8John Reck//        return;
2976d4d0db312fa8a9fce629dd92aa133c6f8249cf8John Reck//    }
298240ff6246a29602539fd0295274e1c769e743a2eJohn Reck
299240ff6246a29602539fd0295274e1c769e743a2eJohn Reck    mCurrentFrameInfo->markIssueDrawCommandsStart();
300240ff6246a29602539fd0295274e1c769e743a2eJohn Reck
301149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    Frame frame = mEglManager.beginFrame(mEglSurface);
302b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
30377c40109cf25d29f85ee6c13aeb96e22e55f33abJohn Reck    if (frame.width() != mLastFrameWidth || frame.height() != mLastFrameHeight) {
30464e445bf74bee2098781d608cedfd723d8cc88d3Chris Craik        // can't rely on prior content of window if viewport size changes
305e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck        dirty.setEmpty();
30677c40109cf25d29f85ee6c13aeb96e22e55f33abJohn Reck        mLastFrameWidth = frame.width();
30777c40109cf25d29f85ee6c13aeb96e22e55f33abJohn Reck        mLastFrameHeight = frame.height();
308149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    } else if (mHaveNewSurface || frame.bufferAge() == 0) {
309149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck        // New surface needs a full draw
310e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck        dirty.setEmpty();
311fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck    } else {
312149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck        if (!dirty.isEmpty() && !dirty.intersect(0, 0, frame.width(), frame.height())) {
3130a97330b98dd633b58dcfff405d94476c89e867dJohn Reck            ALOGW("Dirty " RECT_STRING " doesn't intersect with 0 0 %d %d ?",
314149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck                    SK_RECT_ARGS(dirty), frame.width(), frame.height());
3150a97330b98dd633b58dcfff405d94476c89e867dJohn Reck            dirty.setEmpty();
3160a97330b98dd633b58dcfff405d94476c89e867dJohn Reck        }
317e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck        profiler().unionDirty(&dirty);
3184f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    }
3194f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
320149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    if (dirty.isEmpty()) {
321149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck        dirty.set(0, 0, frame.width(), frame.height());
32213d1b4ab10fbee5e81a2ba1ac59cfae1e51d3ef0Season Li    }
32313d1b4ab10fbee5e81a2ba1ac59cfae1e51d3ef0Season Li
324149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    // At this point dirty is the area of the screen to update. However,
325149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    // the area of the frame we need to repaint is potentially different, so
326149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    // stash the screen area for later
327149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    SkRect screenDirty(dirty);
328149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck
329149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    // If the buffer age is 0 we do a full-screen repaint (handled above)
330149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    // If the buffer age is 1 the buffer contents are the same as they were
331149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    // last frame so there's nothing to union() against
332149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    // Therefore we only care about the > 1 case.
333149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    if (frame.bufferAge() > 1) {
334e486d932ca5a10446a3c98d6d065213913277268John Reck        if (frame.bufferAge() > (int) mSwapHistory.size()) {
335149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck            // We don't have enough history to handle this old of a buffer
336149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck            // Just do a full-draw
337149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck            dirty.set(0, 0, frame.width(), frame.height());
338149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck        } else {
339149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck            // At this point we haven't yet added the latest frame
340149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck            // to the damage history (happens below)
341149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck            // So we need to damage
342e486d932ca5a10446a3c98d6d065213913277268John Reck            for (int i = mSwapHistory.size() - 1;
343e486d932ca5a10446a3c98d6d065213913277268John Reck                    i > ((int) mSwapHistory.size()) - frame.bufferAge(); i--) {
344e486d932ca5a10446a3c98d6d065213913277268John Reck                dirty.join(mSwapHistory[i].damage);
345149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck            }
346149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck        }
3474f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    }
3484f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
349149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    mEglManager.damageFrame(frame, dirty);
350ddf2215d9807b641dbcb304779ef6b530f876ac7Chris Craik
351ddf2215d9807b641dbcb304779ef6b530f876ac7Chris Craik#if HWUI_NEW_OPS
35298787e6c9b2c10b1ab7820bdac168686025b924aChris Craik    OpReorderer reorderer(mLayerUpdateQueue, dirty, frame.width(), frame.height(),
35398787e6c9b2c10b1ab7820bdac168686025b924aChris Craik            mRenderNodes, mLightCenter);
3540b7e8245db728d127ada698be63d78b33fc6e4daChris Craik    mLayerUpdateQueue.clear();
35598787e6c9b2c10b1ab7820bdac168686025b924aChris Craik    BakedOpRenderer renderer(Caches::getInstance(), mRenderThread.renderState(),
35698787e6c9b2c10b1ab7820bdac168686025b924aChris Craik            mOpaque, mLightInfo);
357ddf2215d9807b641dbcb304779ef6b530f876ac7Chris Craik    // TODO: profiler().draw(mCanvas);
3585854b34881b1a747ac80b5077869ef270a92b1f4Chris Craik    reorderer.replayBakedOps<BakedOpDispatcher>(renderer);
359ddf2215d9807b641dbcb304779ef6b530f876ac7Chris Craik
3605854b34881b1a747ac80b5077869ef270a92b1f4Chris Craik    bool drew = renderer.didDraw();
361ddf2215d9807b641dbcb304779ef6b530f876ac7Chris Craik
362ddf2215d9807b641dbcb304779ef6b530f876ac7Chris Craik#else
36364e445bf74bee2098781d608cedfd723d8cc88d3Chris Craik    mCanvas->prepareDirty(frame.width(), frame.height(),
36464e445bf74bee2098781d608cedfd723d8cc88d3Chris Craik            dirty.fLeft, dirty.fTop, dirty.fRight, dirty.fBottom, mOpaque);
365149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck
3664f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    Rect outBounds;
367b6c2624c8d9ee71d1c8d0aaf8082f92bb84b1418Robert Carr    // It there are multiple render nodes, they are laid out as follows:
368b6c2624c8d9ee71d1c8d0aaf8082f92bb84b1418Robert Carr    // #0 - backdrop (content + caption)
369b816087962aba0019b022303330f03b897b580edSkuhne    // #1 - content (positioned at (0,0) and clipped to - its bounds mContentDrawBounds)
370b6c2624c8d9ee71d1c8d0aaf8082f92bb84b1418Robert Carr    // #2 - additional overlay nodes
371ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    // Usually the backdrop cannot be seen since it will be entirely covered by the content. While
372ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    // resizing however it might become partially visible. The following render loop will crop the
373b6c2624c8d9ee71d1c8d0aaf8082f92bb84b1418Robert Carr    // backdrop against the content and draw the remaining part of it. It will then draw the content
374b6c2624c8d9ee71d1c8d0aaf8082f92bb84b1418Robert Carr    // cropped to the backdrop (since that indicates a shrinking of the window).
375b6c2624c8d9ee71d1c8d0aaf8082f92bb84b1418Robert Carr    //
376b6c2624c8d9ee71d1c8d0aaf8082f92bb84b1418Robert Carr    // Additional nodes will be drawn on top with no particular clipping semantics.
377b6c2624c8d9ee71d1c8d0aaf8082f92bb84b1418Robert Carr
378ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    // The bounds of the backdrop against which the content should be clipped.
379b816087962aba0019b022303330f03b897b580edSkuhne    Rect backdropBounds = mContentDrawBounds;
380b816087962aba0019b022303330f03b897b580edSkuhne    // Usually the contents bounds should be mContentDrawBounds - however - we will
381b816087962aba0019b022303330f03b897b580edSkuhne    // move it towards the fixed edge to give it a more stable appearance (for the moment).
382b816087962aba0019b022303330f03b897b580edSkuhne    Rect contentBounds;
383ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    // If there is no content bounds we ignore the layering as stated above and start with 2.
384b6c2624c8d9ee71d1c8d0aaf8082f92bb84b1418Robert Carr    int layer = (mContentDrawBounds.isEmpty() || mRenderNodes.size() == 1) ? 2 : 0;
385ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    // Draw all render nodes. Note that
386ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    for (const sp<RenderNode>& node : mRenderNodes) {
387ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne        if (layer == 0) { // Backdrop.
388b816087962aba0019b022303330f03b897b580edSkuhne            // Draw the backdrop clipped to the inverse content bounds, but assume that the content
389b816087962aba0019b022303330f03b897b580edSkuhne            // was moved to the upper left corner.
390ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            const RenderProperties& properties = node->properties();
391ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            Rect targetBounds(properties.getLeft(), properties.getTop(),
392ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                              properties.getRight(), properties.getBottom());
393b816087962aba0019b022303330f03b897b580edSkuhne            // Move the content bounds towards the fixed corner of the backdrop.
394b816087962aba0019b022303330f03b897b580edSkuhne            const int x = targetBounds.left;
395b816087962aba0019b022303330f03b897b580edSkuhne            const int y = targetBounds.top;
396b816087962aba0019b022303330f03b897b580edSkuhne            contentBounds.set(x, y, x + mContentDrawBounds.getWidth(),
397b816087962aba0019b022303330f03b897b580edSkuhne                                    y + mContentDrawBounds.getHeight());
398ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            // Remember the intersection of the target bounds and the intersection bounds against
399ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            // which we have to crop the content.
400b816087962aba0019b022303330f03b897b580edSkuhne            backdropBounds.set(x, y, x + backdropBounds.getWidth(), y + backdropBounds.getHeight());
401ac02eb9035a13a3d09c2def9ed63d04225eb2509Chris Craik            backdropBounds.doIntersect(targetBounds);
402ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            // Check if we have to draw something on the left side ...
403b816087962aba0019b022303330f03b897b580edSkuhne            if (targetBounds.left < contentBounds.left) {
404ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                mCanvas->save(SkCanvas::kClip_SaveFlag);
405ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                if (mCanvas->clipRect(targetBounds.left, targetBounds.top,
406b816087962aba0019b022303330f03b897b580edSkuhne                                      contentBounds.left, targetBounds.bottom,
407ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                                      SkRegion::kIntersect_Op)) {
408ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                    mCanvas->drawRenderNode(node.get(), outBounds);
409ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                }
410ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                // Reduce the target area by the area we have just painted.
411b816087962aba0019b022303330f03b897b580edSkuhne                targetBounds.left = std::min(contentBounds.left, targetBounds.right);
412ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                mCanvas->restore();
413ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            }
414ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            // ... or on the right side ...
415b816087962aba0019b022303330f03b897b580edSkuhne            if (targetBounds.right > contentBounds.right &&
416ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                !targetBounds.isEmpty()) {
417ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                mCanvas->save(SkCanvas::kClip_SaveFlag);
418b816087962aba0019b022303330f03b897b580edSkuhne                if (mCanvas->clipRect(contentBounds.right, targetBounds.top,
419ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                                      targetBounds.right, targetBounds.bottom,
420ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                                      SkRegion::kIntersect_Op)) {
421ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                    mCanvas->drawRenderNode(node.get(), outBounds);
422ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                }
423ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                // Reduce the target area by the area we have just painted.
424b816087962aba0019b022303330f03b897b580edSkuhne                targetBounds.right = std::max(targetBounds.left, contentBounds.right);
425ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                mCanvas->restore();
426ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            }
427ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            // ... or at the top ...
428b816087962aba0019b022303330f03b897b580edSkuhne            if (targetBounds.top < contentBounds.top &&
429ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                !targetBounds.isEmpty()) {
430ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                mCanvas->save(SkCanvas::kClip_SaveFlag);
431ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                if (mCanvas->clipRect(targetBounds.left, targetBounds.top, targetBounds.right,
432b816087962aba0019b022303330f03b897b580edSkuhne                                      contentBounds.top,
433ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                                      SkRegion::kIntersect_Op)) {
434ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                    mCanvas->drawRenderNode(node.get(), outBounds);
435ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                }
436ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                // Reduce the target area by the area we have just painted.
437b816087962aba0019b022303330f03b897b580edSkuhne                targetBounds.top = std::min(contentBounds.top, targetBounds.bottom);
438ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                mCanvas->restore();
439ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            }
440ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            // ... or at the bottom.
441b816087962aba0019b022303330f03b897b580edSkuhne            if (targetBounds.bottom > contentBounds.bottom &&
442ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                !targetBounds.isEmpty()) {
443ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                mCanvas->save(SkCanvas::kClip_SaveFlag);
444b816087962aba0019b022303330f03b897b580edSkuhne                if (mCanvas->clipRect(targetBounds.left, contentBounds.bottom, targetBounds.right,
445ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                                      targetBounds.bottom, SkRegion::kIntersect_Op)) {
446ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                    mCanvas->drawRenderNode(node.get(), outBounds);
447ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                }
448ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                mCanvas->restore();
449ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            }
450ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne        } else if (layer == 1) { // Content
451ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            // It gets cropped against the bounds of the backdrop to stay inside.
452b816087962aba0019b022303330f03b897b580edSkuhne            mCanvas->save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
453b816087962aba0019b022303330f03b897b580edSkuhne
454b816087962aba0019b022303330f03b897b580edSkuhne            // We shift and clip the content to match its final location in the window.
455b816087962aba0019b022303330f03b897b580edSkuhne            const float left = mContentDrawBounds.left;
456b816087962aba0019b022303330f03b897b580edSkuhne            const float top = mContentDrawBounds.top;
457b816087962aba0019b022303330f03b897b580edSkuhne            const float dx = backdropBounds.left - left;
458b816087962aba0019b022303330f03b897b580edSkuhne            const float dy = backdropBounds.top - top;
459b816087962aba0019b022303330f03b897b580edSkuhne            const float width = backdropBounds.getWidth();
460b816087962aba0019b022303330f03b897b580edSkuhne            const float height = backdropBounds.getHeight();
461b6c2624c8d9ee71d1c8d0aaf8082f92bb84b1418Robert Carr
462b816087962aba0019b022303330f03b897b580edSkuhne            mCanvas->translate(dx, dy);
463b816087962aba0019b022303330f03b897b580edSkuhne            if (mCanvas->clipRect(left, top, left + width, top + height, SkRegion::kIntersect_Op)) {
464ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                mCanvas->drawRenderNode(node.get(), outBounds);
465ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            }
466ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            mCanvas->restore();
467ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne        } else { // draw the rest on top at will!
468ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            mCanvas->drawRenderNode(node.get(), outBounds);
469ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne        }
470ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne        layer++;
471ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    }
4724f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
473fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck    profiler().draw(mCanvas);
4744f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
475107843de4507b3511006cb9c77b8d0364374385aTom Hudson    bool drew = mCanvas->finish();
476ddf2215d9807b641dbcb304779ef6b530f876ac7Chris Craik#endif
477ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    // Even if we decided to cancel the frame, from the perspective of jank
478ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    // metrics the frame was swapped at this point
479ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    mCurrentFrameInfo->markSwapBuffers();
480ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck
481107843de4507b3511006cb9c77b8d0364374385aTom Hudson    if (drew) {
482149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck        if (CC_UNLIKELY(!mEglManager.swapBuffers(frame, screenDirty))) {
483149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck            setSurface(nullptr);
484149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck        }
485e486d932ca5a10446a3c98d6d065213913277268John Reck        SwapHistory& swap = mSwapHistory.next();
486e486d932ca5a10446a3c98d6d065213913277268John Reck        swap.damage = screenDirty;
487e486d932ca5a10446a3c98d6d065213913277268John Reck        swap.swapTime = systemTime(CLOCK_MONOTONIC);
488e486d932ca5a10446a3c98d6d065213913277268John Reck        swap.vsyncTime = mRenderThread.timeLord().latestVsync();
489149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck        mHaveNewSurface = false;
49023b797ab5151eb2474f3bdd679f2f07bfd723042John Reck    }
491fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck
492ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    // TODO: Use a fence for real completion?
493ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    mCurrentFrameInfo->markFrameCompleted();
494149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck
495149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck#if LOG_FRAMETIME_MMA
496149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    float thisFrame = mCurrentFrameInfo->duration(
497149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck            FrameInfoIndex::IssueDrawCommandsStart,
498149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck            FrameInfoIndex::FrameCompleted) / NANOS_PER_MILLIS_F;
499149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    if (sFrameCount) {
500149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck        sBenchMma = ((9 * sBenchMma) + thisFrame) / 10;
501149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    } else {
502149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck        sBenchMma = thisFrame;
503149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    }
504149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    if (++sFrameCount == 10) {
505149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck        sFrameCount = 1;
506149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck        ALOGD("Average frame time: %.4f", sBenchMma);
507149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    }
508149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck#endif
509149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck
510edc524c90506d80e0fc5fb67e8de7b8f3ef53439John Reck    mJankTracker.addFrame(*mCurrentFrameInfo);
511ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    mRenderThread.jankTracker().addFrame(*mCurrentFrameInfo);
51223b797ab5151eb2474f3bdd679f2f07bfd723042John Reck}
51323b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
514e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck// Called by choreographer to do an RT-driven animation
51518f16e6fba74eda173e1e7c869e6e2e2acc073ffJohn Reckvoid CanvasContext::doFrame() {
516368cdd85268999997fb495cf90c4417221797de0John Reck    if (CC_UNLIKELY(!mCanvas || mEglSurface == EGL_NO_SURFACE)) {
517368cdd85268999997fb495cf90c4417221797de0John Reck        return;
518368cdd85268999997fb495cf90c4417221797de0John Reck    }
519ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    prepareAndDraw(nullptr);
520ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne}
521368cdd85268999997fb495cf90c4417221797de0John Reck
522ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhnevoid CanvasContext::prepareAndDraw(RenderNode* node) {
523e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck    ATRACE_CALL();
524e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck
525ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    int64_t frameInfo[UI_THREAD_FRAME_INFO_SIZE];
526ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    UiFrameInfoBuilder(frameInfo)
5271b54fb27ac48495ed0b33868fda5776fb49fe0f3Chris Craik        .addFlag(FrameInfoFlags::RTAnimation)
528ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck        .setVsync(mRenderThread.timeLord().computeFrameTimeNanos(),
529ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck                mRenderThread.timeLord().latestVsync());
530fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck
531e2e53a7079733694bd52dbce665e9ceff21e9727Chris Craik    TreeInfo info(TreeInfo::MODE_RT_ONLY, *this);
532ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    prepareTree(info, frameInfo, systemTime(CLOCK_MONOTONIC), node);
533a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    if (info.out.canDrawThisFrame) {
534e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck        draw();
535a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    }
536e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck}
537e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck
5383b20251a355c88193c439f928a84ae69483fb488John Reckvoid CanvasContext::invokeFunctor(RenderThread& thread, Functor* functor) {
539d3d8dafc2f61fb118c060720b52684c59303f3dbJohn Reck    ATRACE_CALL();
5400d1f634f4b5e1bb37aa51777efb6a68619488d01John Reck    DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
5413b20251a355c88193c439f928a84ae69483fb488John Reck    if (thread.eglManager().hasEglContext()) {
5420d1f634f4b5e1bb37aa51777efb6a68619488d01John Reck        mode = DrawGlInfo::kModeProcess;
5430d1f634f4b5e1bb37aa51777efb6a68619488d01John Reck    }
5446f07a0dc875a9eac67312085a8e0133b9e2f4771John Reck
545d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    thread.renderState().invokeFunctor(functor, mode, nullptr);
54623b797ab5151eb2474f3bdd679f2f07bfd723042John Reck}
54723b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
548998a6d81896df8b662cc10ddeb35087b78b38d72John Reckvoid CanvasContext::markLayerInUse(RenderNode* node) {
549998a6d81896df8b662cc10ddeb35087b78b38d72John Reck    if (mPrefetechedLayers.erase(node)) {
550d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik        node->decStrong(nullptr);
551998a6d81896df8b662cc10ddeb35087b78b38d72John Reck    }
552998a6d81896df8b662cc10ddeb35087b78b38d72John Reck}
553998a6d81896df8b662cc10ddeb35087b78b38d72John Reck
554998a6d81896df8b662cc10ddeb35087b78b38d72John Reckstatic void destroyPrefetechedNode(RenderNode* node) {
555998a6d81896df8b662cc10ddeb35087b78b38d72John Reck    ALOGW("Incorrectly called buildLayer on View: %s, destroying layer...", node->getName());
556998a6d81896df8b662cc10ddeb35087b78b38d72John Reck    node->destroyHardwareResources();
557d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    node->decStrong(nullptr);
558998a6d81896df8b662cc10ddeb35087b78b38d72John Reck}
559998a6d81896df8b662cc10ddeb35087b78b38d72John Reck
560998a6d81896df8b662cc10ddeb35087b78b38d72John Reckvoid CanvasContext::freePrefetechedLayers() {
561998a6d81896df8b662cc10ddeb35087b78b38d72John Reck    if (mPrefetechedLayers.size()) {
562998a6d81896df8b662cc10ddeb35087b78b38d72John Reck        std::for_each(mPrefetechedLayers.begin(), mPrefetechedLayers.end(), destroyPrefetechedNode);
563998a6d81896df8b662cc10ddeb35087b78b38d72John Reck        mPrefetechedLayers.clear();
564998a6d81896df8b662cc10ddeb35087b78b38d72John Reck    }
565998a6d81896df8b662cc10ddeb35087b78b38d72John Reck}
566998a6d81896df8b662cc10ddeb35087b78b38d72John Reck
5673e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reckvoid CanvasContext::buildLayer(RenderNode* node) {
5683e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    ATRACE_CALL();
5693e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    if (!mEglManager.hasEglContext() || !mCanvas) {
5703e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck        return;
5713e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    }
5723e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    // buildLayer() will leave the tree in an unknown state, so we must stop drawing
5733e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    stopDrawing();
5743e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck
575e2e53a7079733694bd52dbce665e9ceff21e9727Chris Craik    TreeInfo info(TreeInfo::MODE_FULL, *this);
5763e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    info.damageAccumulator = &mDamageAccumulator;
5770b7e8245db728d127ada698be63d78b33fc6e4daChris Craik#if HWUI_NEW_OPS
5780b7e8245db728d127ada698be63d78b33fc6e4daChris Craik    info.layerUpdateQueue = &mLayerUpdateQueue;
5790b7e8245db728d127ada698be63d78b33fc6e4daChris Craik#else
5803e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    info.renderer = mCanvas;
5810b7e8245db728d127ada698be63d78b33fc6e4daChris Craik#endif
5829eb9f6f8cbbbd87d45da8071aa54cb066a797723John Reck    info.runAnimations = false;
5833e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    node->prepareTree(info);
5843e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    SkRect ignore;
5853e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    mDamageAccumulator.finish(&ignore);
5863e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    // Tickle the GENERIC property on node to mark it as dirty for damaging
5873e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    // purposes when the frame is actually drawn
5883e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    node->setPropertyFieldsDirty(RenderNode::GENERIC);
5893e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck
59098787e6c9b2c10b1ab7820bdac168686025b924aChris Craik#if HWUI_NEW_OPS
59198787e6c9b2c10b1ab7820bdac168686025b924aChris Craik    LOG_ALWAYS_FATAL("unsupported");
59298787e6c9b2c10b1ab7820bdac168686025b924aChris Craik#else
593443a714fa7c0dd07fee3527cc5bc3d3ca1fb7d44John Reck    mCanvas->markLayersAsBuildLayers();
5943e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    mCanvas->flushLayerUpdates();
59598787e6c9b2c10b1ab7820bdac168686025b924aChris Craik#endif
596998a6d81896df8b662cc10ddeb35087b78b38d72John Reck
597d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    node->incStrong(nullptr);
598998a6d81896df8b662cc10ddeb35087b78b38d72John Reck    mPrefetechedLayers.insert(node);
5993e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck}
6003e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck
60119b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reckbool CanvasContext::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
60268bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck    layer->apply();
6033b20251a355c88193c439f928a84ae69483fb488John Reck    return LayerRenderer::copyLayer(mRenderThread.renderState(), layer->backingLayer(), bitmap);
60419b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck}
60519b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck
606f47a594f5250b1914c36423ee6b371f0b8db09d0John Reckvoid CanvasContext::destroyHardwareResources() {
607f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck    stopDrawing();
6083b20251a355c88193c439f928a84ae69483fb488John Reck    if (mEglManager.hasEglContext()) {
609dff9957cc22a1174a4cf91de6609c50934d29434John Reck        freePrefetechedLayers();
610ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne        for (const sp<RenderNode>& node : mRenderNodes) {
611ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            node->destroyHardwareResources();
612ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne        }
61300e79c9947b741194ff6c0d08ede9b3befbf9c9dJohn Reck        Caches& caches = Caches::getInstance();
61400e79c9947b741194ff6c0d08ede9b3befbf9c9dJohn Reck        // Make sure to release all the textures we were owning as there won't
61500e79c9947b741194ff6c0d08ede9b3befbf9c9dJohn Reck        // be another draw
61600e79c9947b741194ff6c0d08ede9b3befbf9c9dJohn Reck        caches.textureCache.resetMarkInUse(this);
6179fded232a9548a304e0145011df8849fba0dcda7Chris Craik        mRenderThread.renderState().flush(Caches::FlushMode::Layers);
618f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck    }
619f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck}
620f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck
621f47a594f5250b1914c36423ee6b371f0b8db09d0John Reckvoid CanvasContext::trimMemory(RenderThread& thread, int level) {
622f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck    // No context means nothing to free
623f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck    if (!thread.eglManager().hasEglContext()) return;
624f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck
625786afcb3eec18315ec54987a08814ff28f13d09fJorim Jaggi    ATRACE_CALL();
626f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck    if (level >= TRIM_MEMORY_COMPLETE) {
6279fded232a9548a304e0145011df8849fba0dcda7Chris Craik        thread.renderState().flush(Caches::FlushMode::Full);
628f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck        thread.eglManager().destroy();
629f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck    } else if (level >= TRIM_MEMORY_UI_HIDDEN) {
6309fded232a9548a304e0145011df8849fba0dcda7Chris Craik        thread.renderState().flush(Caches::FlushMode::Moderate);
631e1628b7c6fc3822fa83cf02028ce8ad67abb0afeJohn Reck    }
632e1628b7c6fc3822fa83cf02028ce8ad67abb0afeJohn Reck}
633e1628b7c6fc3822fa83cf02028ce8ad67abb0afeJohn Reck
634fc53ef27793a39e9effd829e9cae02a9ca14147eJohn Reckvoid CanvasContext::runWithGlContext(RenderTask* task) {
635d7db4d767246b41d44995acb93d03d220b53c748John Reck    LOG_ALWAYS_FATAL_IF(!mEglManager.hasEglContext(),
636d7db4d767246b41d44995acb93d03d220b53c748John Reck            "GL context not initialized!");
63719b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck    task->run();
63819b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck}
63919b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck
6401949e7928eeec22cd3f74b5f763a4eb433238453John ReckLayer* CanvasContext::createTextureLayer() {
641f7d9c1dc84671d4e99657ef071d275700d85bb11John Reck    requireSurface();
6423b20251a355c88193c439f928a84ae69483fb488John Reck    return LayerRenderer::createTextureLayer(mRenderThread.renderState());
6431949e7928eeec22cd3f74b5f763a4eb433238453John Reck}
6441949e7928eeec22cd3f74b5f763a4eb433238453John Reck
6453b20251a355c88193c439f928a84ae69483fb488John Reckvoid CanvasContext::setTextureAtlas(RenderThread& thread,
6463b20251a355c88193c439f928a84ae69483fb488John Reck        const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize) {
6473b20251a355c88193c439f928a84ae69483fb488John Reck    thread.eglManager().setTextureAtlas(buffer, map, mapSize);
64866f0be65a1046f54ddce0498b242c1fa0776b1eaJohn Reck}
64966f0be65a1046f54ddce0498b242c1fa0776b1eaJohn Reck
650ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reckvoid CanvasContext::dumpFrames(int fd) {
651ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    FILE* file = fdopen(fd, "a");
6524db3d17debef68f72d23999d69ae68b75f59dda3John Reck    fprintf(file, "\n\n---PROFILEDATA---\n");
6531b54fb27ac48495ed0b33868fda5776fb49fe0f3Chris Craik    for (size_t i = 0; i < static_cast<size_t>(FrameInfoIndex::NumIndexes); i++) {
6542a8bb05a31ddd0d44d8513cba9fbd9b4ef9b97f6John Reck        fprintf(file, "%s", FrameInfoNames[i].c_str());
6554db3d17debef68f72d23999d69ae68b75f59dda3John Reck        fprintf(file, ",");
6564db3d17debef68f72d23999d69ae68b75f59dda3John Reck    }
657ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    for (size_t i = 0; i < mFrames.size(); i++) {
658ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck        FrameInfo& frame = mFrames[i];
6591b54fb27ac48495ed0b33868fda5776fb49fe0f3Chris Craik        if (frame[FrameInfoIndex::SyncStart] == 0) {
660ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck            continue;
661ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck        }
662ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck        fprintf(file, "\n");
6631b54fb27ac48495ed0b33868fda5776fb49fe0f3Chris Craik        for (int i = 0; i < static_cast<int>(FrameInfoIndex::NumIndexes); i++) {
664ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck            fprintf(file, "%" PRId64 ",", frame[i]);
665ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck        }
666ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    }
667ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    fprintf(file, "\n---PROFILEDATA---\n\n");
668ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    fflush(file);
669ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck}
670ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck
671ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reckvoid CanvasContext::resetFrameStats() {
672ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    mFrames.clear();
673ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    mRenderThread.jankTracker().reset();
674ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck}
675ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck
676e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reckvoid CanvasContext::serializeDisplayListTree() {
677e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck#if ENABLE_RENDERNODE_SERIALIZATION
678e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    using namespace google::protobuf::io;
679e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    char package[128];
680e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    // Check whether tracing is enabled for this process.
681e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    FILE * file = fopen("/proc/self/cmdline", "r");
682e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    if (file) {
683e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck        if (!fgets(package, 128, file)) {
684e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck            ALOGE("Error reading cmdline: %s (%d)", strerror(errno), errno);
685e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck            fclose(file);
686e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck            return;
687e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck        }
688e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck        fclose(file);
689e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    } else {
690e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck        ALOGE("Error opening /proc/self/cmdline: %s (%d)", strerror(errno),
691e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck                errno);
692e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck        return;
693e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    }
694e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    char path[1024];
695e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    snprintf(path, 1024, "/data/data/%s/cache/rendertree_dump", package);
696e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    int fd = open(path, O_CREAT | O_WRONLY, S_IRWXU | S_IRGRP | S_IROTH);
697e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    if (fd == -1) {
698e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck        ALOGD("Failed to open '%s'", path);
699e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck        return;
700e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    }
701e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    proto::RenderNode tree;
702e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    // TODO: Streaming writes?
703e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    mRootRenderNode->copyTo(&tree);
704e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    std::string data = tree.SerializeAsString();
705e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    write(fd, data.c_str(), data.length());
706e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    close(fd);
707e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck#endif
708e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck}
709e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck
71023b797ab5151eb2474f3bdd679f2f07bfd723042John Reck} /* namespace renderthread */
71123b797ab5151eb2474f3bdd679f2f07bfd723042John Reck} /* namespace uirenderer */
71223b797ab5151eb2474f3bdd679f2f07bfd723042John Reck} /* namespace android */
713