CanvasContext.cpp revision 6e068c0182f6f85bccb855a647510724d1c65a13
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
1738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck#include <GpuMemoryTracker.h>
1823b797ab5151eb2474f3bdd679f2f07bfd723042John Reck#include "CanvasContext.h"
1923b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
20d04794a9a3f9edc8b7ca336175d66eb81a8f55faJohn Reck#include "AnimationContext.h"
21d04794a9a3f9edc8b7ca336175d66eb81a8f55faJohn Reck#include "Caches.h"
22eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita#include "Canvas.h"
23d04794a9a3f9edc8b7ca336175d66eb81a8f55faJohn Reck#include "DeferredLayerUpdater.h"
243b20251a355c88193c439f928a84ae69483fb488John Reck#include "EglManager.h"
250b7e8245db728d127ada698be63d78b33fc6e4daChris Craik#include "LayerUpdateQueue.h"
26d04794a9a3f9edc8b7ca336175d66eb81a8f55faJohn Reck#include "LayerRenderer.h"
27d04794a9a3f9edc8b7ca336175d66eb81a8f55faJohn Reck#include "OpenGLRenderer.h"
28d04794a9a3f9edc8b7ca336175d66eb81a8f55faJohn Reck#include "Properties.h"
294f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck#include "RenderThread.h"
30d04794a9a3f9edc8b7ca336175d66eb81a8f55faJohn Reck#include "renderstate/RenderState.h"
31d04794a9a3f9edc8b7ca336175d66eb81a8f55faJohn Reck#include "renderstate/Stencil.h"
32e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck#include "protos/hwui.pb.h"
339372ac3621848085e77b867f220c0b5ffce4010dJohn Reck#include "utils/GLUtils.h"
34e486d932ca5a10446a3c98d6d065213913277268John Reck#include "utils/TimeUtils.h"
3523b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
36240ff6246a29602539fd0295274e1c769e743a2eJohn Reck#include <cutils/properties.h>
37e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck#include <google/protobuf/io/zero_copy_stream_impl.h>
3865fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik#include <private/hwui/DrawGlInfo.h>
39e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck#include <strings.h>
40e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck
41e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck#include <algorithm>
42e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck#include <fcntl.h>
43e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck#include <sys/stat.h>
4465fe5eeb19e2e15c8b1ee91e8a2dcf0c25e48ca6Chris Craik
45290b23a1e11d532b39098bb58693ef97ba98a622Colin Cross#include <cstdlib>
46290b23a1e11d532b39098bb58693ef97ba98a622Colin Cross
47f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck#define TRIM_MEMORY_COMPLETE 80
48f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck#define TRIM_MEMORY_UI_HIDDEN 20
49f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck
50e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck#define ENABLE_RENDERNODE_SERIALIZATION false
51e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck
52149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck#define LOG_FRAMETIME_MMA 0
53149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck
54149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck#if LOG_FRAMETIME_MMA
55149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reckstatic float sBenchMma = 0;
56149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reckstatic int sFrameCount = 0;
57149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reckstatic const float NANOS_PER_MILLIS_F = 1000000.0f;
58149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck#endif
59149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck
6023b797ab5151eb2474f3bdd679f2f07bfd723042John Recknamespace android {
6123b797ab5151eb2474f3bdd679f2f07bfd723042John Recknamespace uirenderer {
6223b797ab5151eb2474f3bdd679f2f07bfd723042John Recknamespace renderthread {
6323b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
64119907cd2575c56b1ebf66348b52e67aaf6a88d8John ReckCanvasContext::CanvasContext(RenderThread& thread, bool translucent,
65119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck        RenderNode* rootRenderNode, IContextFactory* contextFactory)
663b20251a355c88193c439f928a84ae69483fb488John Reck        : mRenderThread(thread)
673b20251a355c88193c439f928a84ae69483fb488John Reck        , mEglManager(thread.eglManager())
684f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck        , mOpaque(!translucent)
6951d6a3db97bdd5315f1a17a4b447d10a92217b98Chris Craik        , mAnimationContext(contextFactory->createAnimationContext(mRenderThread.timeLord()))
704c9e59d03c2bca38001225b79d01740b8999adfbJohn Reck        , mJankTracker(thread.timeLord().frameIntervalNanos())
71ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne        , mProfiler(mFrames)
72b816087962aba0019b022303330f03b897b580edSkuhne        , mContentDrawBounds(0, 0, 0, 0) {
73ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    mRenderNodes.emplace_back(rootRenderNode);
74443a714fa7c0dd07fee3527cc5bc3d3ca1fb7d44John Reck    mRenderThread.renderState().registerCanvasContext(this);
75b36016c65f1d1b5846dba0349aab491dbd3a746aJohn Reck    mProfiler.setDensity(mRenderThread.mainDisplayInfo().density);
7623b797ab5151eb2474f3bdd679f2f07bfd723042John Reck}
7723b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
7823b797ab5151eb2474f3bdd679f2f07bfd723042John ReckCanvasContext::~CanvasContext() {
7917035b0211a3c9d45ea46a99217a6acbe76e8fbeJohn Reck    destroy();
80443a714fa7c0dd07fee3527cc5bc3d3ca1fb7d44John Reck    mRenderThread.renderState().unregisterCanvasContext(this);
814f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
824f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
8317035b0211a3c9d45ea46a99217a6acbe76e8fbeJohn Reckvoid CanvasContext::destroy() {
8417035b0211a3c9d45ea46a99217a6acbe76e8fbeJohn Reck    stopDrawing();
85d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    setSurface(nullptr);
8617035b0211a3c9d45ea46a99217a6acbe76e8fbeJohn Reck    freePrefetechedLayers();
8717035b0211a3c9d45ea46a99217a6acbe76e8fbeJohn Reck    destroyHardwareResources();
88e2478d45ccbe5b6abb360ac9d44771b5f4a50bdeJohn Reck    mAnimationContext->destroy();
894f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    if (mCanvas) {
904f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck        delete mCanvas;
91d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik        mCanvas = nullptr;
924f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    }
9323b797ab5151eb2474f3bdd679f2f07bfd723042John Reck}
9423b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
95a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reckvoid CanvasContext::setSurface(ANativeWindow* window) {
96fbc8df03e498baf47ff1a5e05e182f1bcd60c770John Reck    ATRACE_CALL();
97fbc8df03e498baf47ff1a5e05e182f1bcd60c770John Reck
98a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    mNativeWindow = window;
99a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck
10023b797ab5151eb2474f3bdd679f2f07bfd723042John Reck    if (mEglSurface != EGL_NO_SURFACE) {
1013b20251a355c88193c439f928a84ae69483fb488John Reck        mEglManager.destroySurface(mEglSurface);
10223b797ab5151eb2474f3bdd679f2f07bfd723042John Reck        mEglSurface = EGL_NO_SURFACE;
10323b797ab5151eb2474f3bdd679f2f07bfd723042John Reck    }
10423b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
10523b797ab5151eb2474f3bdd679f2f07bfd723042John Reck    if (window) {
1063b20251a355c88193c439f928a84ae69483fb488John Reck        mEglSurface = mEglManager.createSurface(window);
10723b797ab5151eb2474f3bdd679f2f07bfd723042John Reck    }
10823b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
10923b797ab5151eb2474f3bdd679f2f07bfd723042John Reck    if (mEglSurface != EGL_NO_SURFACE) {
1101125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck        const bool preserveBuffer = (mSwapBehavior != kSwap_discardBuffer);
1111125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck        mBufferPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer);
1124f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck        mHaveNewSurface = true;
113e486d932ca5a10446a3c98d6d065213913277268John Reck        mSwapHistory.clear();
114dbc9a86d05e5e835051de22f6cb30ec1921e9705John Reck        makeCurrent();
115368cdd85268999997fb495cf90c4417221797de0John Reck    } else {
116368cdd85268999997fb495cf90c4417221797de0John Reck        mRenderThread.removeFrameCallback(this);
1174f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    }
1184f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
1194f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
120f7d9c1dc84671d4e99657ef071d275700d85bb11John Reckvoid CanvasContext::requireSurface() {
121f7d9c1dc84671d4e99657ef071d275700d85bb11John Reck    LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
122f7d9c1dc84671d4e99657ef071d275700d85bb11John Reck            "requireSurface() called but no surface set!");
123dbc9a86d05e5e835051de22f6cb30ec1921e9705John Reck    makeCurrent();
1244f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
1254f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
1261125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reckvoid CanvasContext::setSwapBehavior(SwapBehavior swapBehavior) {
1271125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck    mSwapBehavior = swapBehavior;
1281125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck}
1291125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck
1300bcd0cb6b1193168fa2840855195347488daab9eThomas Buhotvoid CanvasContext::initialize(ANativeWindow* window) {
1314f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    setSurface(window);
132b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik#if !HWUI_NEW_OPS
1330bcd0cb6b1193168fa2840855195347488daab9eThomas Buhot    if (mCanvas) return;
1343b20251a355c88193c439f928a84ae69483fb488John Reck    mCanvas = new OpenGLRenderer(mRenderThread.renderState());
1354f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    mCanvas->initProperties();
136b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik#endif
1374f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
1384f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
139a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reckvoid CanvasContext::updateSurface(ANativeWindow* window) {
1404f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    setSurface(window);
141f7d9c1dc84671d4e99657ef071d275700d85bb11John Reck}
142f7d9c1dc84671d4e99657ef071d275700d85bb11John Reck
1439fb42f07784ac9e1ab29fa7d5bcda6c3081d238fJohn Reckbool CanvasContext::pauseSurface(ANativeWindow* window) {
14401a5ea35fbba4c5bb1d7790ae1677a2fa752e042John Reck    return mRenderThread.removeFrameCallback(this);
1454f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
1464f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
147284b24358410cb0200e525a5ba36994090c83f20Chris Craik// TODO: don't pass viewport size, it's automatic via EGL
14850210d912925aef14e4ce69be82e4949122a3cd9Alan Viverettevoid CanvasContext::setup(int width, int height, float lightRadius,
14964bb413a664001c95c8439cf097dc3033f4ed733Andreas Gampe        uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
15098787e6c9b2c10b1ab7820bdac168686025b924aChris Craik#if HWUI_NEW_OPS
1516e068c0182f6f85bccb855a647510724d1c65a13Chris Craik    mLightGeometry.radius = lightRadius;
15298787e6c9b2c10b1ab7820bdac168686025b924aChris Craik    mLightInfo.ambientShadowAlpha = ambientShadowAlpha;
15398787e6c9b2c10b1ab7820bdac168686025b924aChris Craik    mLightInfo.spotShadowAlpha = spotShadowAlpha;
15498787e6c9b2c10b1ab7820bdac168686025b924aChris Craik#else
1554f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    if (!mCanvas) return;
15650210d912925aef14e4ce69be82e4949122a3cd9Alan Viverette    mCanvas->initLight(lightRadius, ambientShadowAlpha, spotShadowAlpha);
15798787e6c9b2c10b1ab7820bdac168686025b924aChris Craik#endif
15850210d912925aef14e4ce69be82e4949122a3cd9Alan Viverette}
15950210d912925aef14e4ce69be82e4949122a3cd9Alan Viverette
16050210d912925aef14e4ce69be82e4949122a3cd9Alan Viverettevoid CanvasContext::setLightCenter(const Vector3& lightCenter) {
16198787e6c9b2c10b1ab7820bdac168686025b924aChris Craik#if HWUI_NEW_OPS
1626e068c0182f6f85bccb855a647510724d1c65a13Chris Craik    mLightGeometry.center = lightCenter;
16398787e6c9b2c10b1ab7820bdac168686025b924aChris Craik#else
16450210d912925aef14e4ce69be82e4949122a3cd9Alan Viverette    if (!mCanvas) return;
16550210d912925aef14e4ce69be82e4949122a3cd9Alan Viverette    mCanvas->setLightCenter(lightCenter);
16698787e6c9b2c10b1ab7820bdac168686025b924aChris Craik#endif
1674f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
1684f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
16963a06673253914510bbeebd500655008682dade1John Reckvoid CanvasContext::setOpaque(bool opaque) {
17063a06673253914510bbeebd500655008682dade1John Reck    mOpaque = opaque;
17163a06673253914510bbeebd500655008682dade1John Reck}
17263a06673253914510bbeebd500655008682dade1John Reck
173860d155f866cc15a725e7ce03763280987f24901John Reckvoid CanvasContext::makeCurrent() {
174dbc9a86d05e5e835051de22f6cb30ec1921e9705John Reck    // TODO: Figure out why this workaround is needed, see b/13913604
175dbc9a86d05e5e835051de22f6cb30ec1921e9705John Reck    // In the meantime this matches the behavior of GLRenderer, so it is not a regression
176f2dcc2aecb94e726096256c47b913ed0a57ae7e2John Reck    EGLint error = 0;
177f2dcc2aecb94e726096256c47b913ed0a57ae7e2John Reck    mHaveNewSurface |= mEglManager.makeCurrent(mEglSurface, &error);
178f2dcc2aecb94e726096256c47b913ed0a57ae7e2John Reck    if (error) {
179f2dcc2aecb94e726096256c47b913ed0a57ae7e2John Reck        setSurface(nullptr);
180f2dcc2aecb94e726096256c47b913ed0a57ae7e2John Reck    }
181860d155f866cc15a725e7ce03763280987f24901John Reck}
182860d155f866cc15a725e7ce03763280987f24901John Reck
183bf3c602284f9a344faf185c3a5e94a264ba44c4fJohn Reckstatic bool wasSkipped(FrameInfo* info) {
1841b54fb27ac48495ed0b33868fda5776fb49fe0f3Chris Craik    return info && ((*info)[FrameInfoIndex::Flags] & FrameInfoFlags::SkippedFrame);
185bf3c602284f9a344faf185c3a5e94a264ba44c4fJohn Reck}
186bf3c602284f9a344faf185c3a5e94a264ba44c4fJohn Reck
187ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhnevoid CanvasContext::prepareTree(TreeInfo& info, int64_t* uiFrameInfo,
188ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne        int64_t syncQueued, RenderNode* target) {
189f9be77940e365036fecd8cc0e491e8545c34e79bJohn Reck    mRenderThread.removeFrameCallback(this);
19018f16e6fba74eda173e1e7c869e6e2e2acc073ffJohn Reck
191bf3c602284f9a344faf185c3a5e94a264ba44c4fJohn Reck    // If the previous frame was dropped we don't need to hold onto it, so
192bf3c602284f9a344faf185c3a5e94a264ba44c4fJohn Reck    // just keep using the previous frame's structure instead
193bf3c602284f9a344faf185c3a5e94a264ba44c4fJohn Reck    if (!wasSkipped(mCurrentFrameInfo)) {
194bf3c602284f9a344faf185c3a5e94a264ba44c4fJohn Reck        mCurrentFrameInfo = &mFrames.next();
195bf3c602284f9a344faf185c3a5e94a264ba44c4fJohn Reck    }
196ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    mCurrentFrameInfo->importUiThreadInfo(uiFrameInfo);
197be3fba05e823f740f65b2679929347dc3dd282adJohn Reck    mCurrentFrameInfo->set(FrameInfoIndex::SyncQueued) = syncQueued;
198ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    mCurrentFrameInfo->markSyncStart();
199ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck
200e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck    info.damageAccumulator = &mDamageAccumulator;
2010b7e8245db728d127ada698be63d78b33fc6e4daChris Craik#if HWUI_NEW_OPS
2020b7e8245db728d127ada698be63d78b33fc6e4daChris Craik    info.layerUpdateQueue = &mLayerUpdateQueue;
2030b7e8245db728d127ada698be63d78b33fc6e4daChris Craik#else
20425fbb3fa1138675379102a44405852555cefccbdJohn Reck    info.renderer = mCanvas;
2050b7e8245db728d127ada698be63d78b33fc6e4daChris Craik#endif
20600e79c9947b741194ff6c0d08ede9b3befbf9c9dJohn Reck
207ec845a215e343cdb3b2e4c7b6aff7b24beb0236bJohn Reck    mAnimationContext->startFrame(info.mode);
208ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    for (const sp<RenderNode>& node : mRenderNodes) {
209ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne        // Only the primary target node will be drawn full - all other nodes would get drawn in
210ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne        // real time mode. In case of a window, the primary node is the window content and the other
211ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne        // node(s) are non client / filler nodes.
212ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne        info.mode = (node.get() == target ? TreeInfo::MODE_FULL : TreeInfo::MODE_RT_ONLY);
213ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne        node->prepareTree(info);
214975591a7af883d866d86ab819e164c6004694744John Reck        GL_CHECKPOINT(MODERATE);
215ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    }
216119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    mAnimationContext->runRemainingAnimations(info);
217975591a7af883d866d86ab819e164c6004694744John Reck    GL_CHECKPOINT(MODERATE);
218e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck
21900e79c9947b741194ff6c0d08ede9b3befbf9c9dJohn Reck    freePrefetechedLayers();
220975591a7af883d866d86ab819e164c6004694744John Reck    GL_CHECKPOINT(MODERATE);
221998a6d81896df8b662cc10ddeb35087b78b38d72John Reck
222aa95a88327d9a3ac8a4a00b065b78ac0f28b3a19John Reck    if (CC_UNLIKELY(!mNativeWindow.get())) {
2231b54fb27ac48495ed0b33868fda5776fb49fe0f3Chris Craik        mCurrentFrameInfo->addFlag(FrameInfoFlags::SkippedFrame);
224aa95a88327d9a3ac8a4a00b065b78ac0f28b3a19John Reck        info.out.canDrawThisFrame = false;
225aa95a88327d9a3ac8a4a00b065b78ac0f28b3a19John Reck        return;
226aa95a88327d9a3ac8a4a00b065b78ac0f28b3a19John Reck    }
227aa95a88327d9a3ac8a4a00b065b78ac0f28b3a19John Reck
228e486d932ca5a10446a3c98d6d065213913277268John Reck    if (CC_LIKELY(mSwapHistory.size())) {
229e486d932ca5a10446a3c98d6d065213913277268John Reck        nsecs_t latestVsync = mRenderThread.timeLord().latestVsync();
230e486d932ca5a10446a3c98d6d065213913277268John Reck        const SwapHistory& lastSwap = mSwapHistory.back();
23152b783f76af58a948dad4d3b4d1b7ad7979347ceJohn Reck        nsecs_t vsyncDelta = std::abs(lastSwap.vsyncTime - latestVsync);
232e486d932ca5a10446a3c98d6d065213913277268John Reck        // The slight fudge-factor is to deal with cases where
233e486d932ca5a10446a3c98d6d065213913277268John Reck        // the vsync was estimated due to being slow handling the signal.
234e486d932ca5a10446a3c98d6d065213913277268John Reck        // See the logic in TimeLord#computeFrameTimeNanos or in
235e486d932ca5a10446a3c98d6d065213913277268John Reck        // Choreographer.java for details on when this happens
236e486d932ca5a10446a3c98d6d065213913277268John Reck        if (vsyncDelta < 2_ms) {
237e486d932ca5a10446a3c98d6d065213913277268John Reck            // Already drew for this vsync pulse, UI draw request missed
238e486d932ca5a10446a3c98d6d065213913277268John Reck            // the deadline for RT animations
239e486d932ca5a10446a3c98d6d065213913277268John Reck            info.out.canDrawThisFrame = false;
240e486d932ca5a10446a3c98d6d065213913277268John Reck        } else if (lastSwap.swapTime < latestVsync) {
241e486d932ca5a10446a3c98d6d065213913277268John Reck            info.out.canDrawThisFrame = true;
242e486d932ca5a10446a3c98d6d065213913277268John Reck        } else {
243e486d932ca5a10446a3c98d6d065213913277268John Reck            // We're maybe behind? Find out for sure
244e486d932ca5a10446a3c98d6d065213913277268John Reck            int runningBehind = 0;
245e486d932ca5a10446a3c98d6d065213913277268John Reck            mNativeWindow->query(mNativeWindow.get(),
246e486d932ca5a10446a3c98d6d065213913277268John Reck                    NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &runningBehind);
247e486d932ca5a10446a3c98d6d065213913277268John Reck            info.out.canDrawThisFrame = !runningBehind;
248e486d932ca5a10446a3c98d6d065213913277268John Reck        }
249e486d932ca5a10446a3c98d6d065213913277268John Reck    } else {
250e486d932ca5a10446a3c98d6d065213913277268John Reck        info.out.canDrawThisFrame = true;
251e486d932ca5a10446a3c98d6d065213913277268John Reck    }
252a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck
253aef9dc8d186bd5f78068ab2d5240b5e9c8ab44b6John Reck    if (!info.out.canDrawThisFrame) {
2541b54fb27ac48495ed0b33868fda5776fb49fe0f3Chris Craik        mCurrentFrameInfo->addFlag(FrameInfoFlags::SkippedFrame);
255aef9dc8d186bd5f78068ab2d5240b5e9c8ab44b6John Reck    }
256aef9dc8d186bd5f78068ab2d5240b5e9c8ab44b6John Reck
257a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    if (info.out.hasAnimations || !info.out.canDrawThisFrame) {
258cd028f336e36b22dbe8cf623eb5bd2361314495cJohn Reck        if (!info.out.requiresUiRedraw) {
259f9be77940e365036fecd8cc0e491e8545c34e79bJohn Reck            // If animationsNeedsRedraw is set don't bother posting for an RT anim
260f9be77940e365036fecd8cc0e491e8545c34e79bJohn Reck            // as we will just end up fighting the UI thread.
261f9be77940e365036fecd8cc0e491e8545c34e79bJohn Reck            mRenderThread.postFrameCallback(this);
262f9be77940e365036fecd8cc0e491e8545c34e79bJohn Reck        }
263e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck    }
264e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck}
265e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck
266f47a594f5250b1914c36423ee6b371f0b8db09d0John Reckvoid CanvasContext::stopDrawing() {
267f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck    mRenderThread.removeFrameCallback(this);
268f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck}
269f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck
270a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reckvoid CanvasContext::notifyFramePending() {
271a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    ATRACE_CALL();
272a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    mRenderThread.pushBackFrameCallback(this);
273a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck}
274a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck
275e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reckvoid CanvasContext::draw() {
276b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik#if !HWUI_NEW_OPS
2774f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    LOG_ALWAYS_FATAL_IF(!mCanvas || mEglSurface == EGL_NO_SURFACE,
278a7090e0cfd7c719a6d4c03aae34f5db98754cbddChris Craik            "drawRenderNode called on a context with no canvas or surface!");
279b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik#endif
2804f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
281e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck    SkRect dirty;
282e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck    mDamageAccumulator.finish(&dirty);
283e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck
2846d4d0db312fa8a9fce629dd92aa133c6f8249cf8John Reck    // TODO: Re-enable after figuring out cause of b/22592975
2856d4d0db312fa8a9fce629dd92aa133c6f8249cf8John Reck//    if (dirty.isEmpty() && Properties::skipEmptyFrames) {
2866d4d0db312fa8a9fce629dd92aa133c6f8249cf8John Reck//        mCurrentFrameInfo->addFlag(FrameInfoFlags::SkippedFrame);
2876d4d0db312fa8a9fce629dd92aa133c6f8249cf8John Reck//        return;
2886d4d0db312fa8a9fce629dd92aa133c6f8249cf8John Reck//    }
289240ff6246a29602539fd0295274e1c769e743a2eJohn Reck
290240ff6246a29602539fd0295274e1c769e743a2eJohn Reck    mCurrentFrameInfo->markIssueDrawCommandsStart();
291240ff6246a29602539fd0295274e1c769e743a2eJohn Reck
292149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    Frame frame = mEglManager.beginFrame(mEglSurface);
293b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
29477c40109cf25d29f85ee6c13aeb96e22e55f33abJohn Reck    if (frame.width() != mLastFrameWidth || frame.height() != mLastFrameHeight) {
29564e445bf74bee2098781d608cedfd723d8cc88d3Chris Craik        // can't rely on prior content of window if viewport size changes
296e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck        dirty.setEmpty();
29777c40109cf25d29f85ee6c13aeb96e22e55f33abJohn Reck        mLastFrameWidth = frame.width();
29877c40109cf25d29f85ee6c13aeb96e22e55f33abJohn Reck        mLastFrameHeight = frame.height();
299149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    } else if (mHaveNewSurface || frame.bufferAge() == 0) {
300149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck        // New surface needs a full draw
301e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck        dirty.setEmpty();
302fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck    } else {
303149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck        if (!dirty.isEmpty() && !dirty.intersect(0, 0, frame.width(), frame.height())) {
3040a97330b98dd633b58dcfff405d94476c89e867dJohn Reck            ALOGW("Dirty " RECT_STRING " doesn't intersect with 0 0 %d %d ?",
305149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck                    SK_RECT_ARGS(dirty), frame.width(), frame.height());
3060a97330b98dd633b58dcfff405d94476c89e867dJohn Reck            dirty.setEmpty();
3070a97330b98dd633b58dcfff405d94476c89e867dJohn Reck        }
308e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck        profiler().unionDirty(&dirty);
3094f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    }
3104f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
311149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    if (dirty.isEmpty()) {
312149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck        dirty.set(0, 0, frame.width(), frame.height());
31313d1b4ab10fbee5e81a2ba1ac59cfae1e51d3ef0Season Li    }
31413d1b4ab10fbee5e81a2ba1ac59cfae1e51d3ef0Season Li
315149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    // At this point dirty is the area of the screen to update. However,
316149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    // the area of the frame we need to repaint is potentially different, so
317149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    // stash the screen area for later
318149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    SkRect screenDirty(dirty);
319149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck
320149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    // If the buffer age is 0 we do a full-screen repaint (handled above)
321149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    // If the buffer age is 1 the buffer contents are the same as they were
322149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    // last frame so there's nothing to union() against
323149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    // Therefore we only care about the > 1 case.
324149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    if (frame.bufferAge() > 1) {
325e486d932ca5a10446a3c98d6d065213913277268John Reck        if (frame.bufferAge() > (int) mSwapHistory.size()) {
326149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck            // We don't have enough history to handle this old of a buffer
327149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck            // Just do a full-draw
328149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck            dirty.set(0, 0, frame.width(), frame.height());
329149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck        } else {
330149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck            // At this point we haven't yet added the latest frame
331149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck            // to the damage history (happens below)
332149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck            // So we need to damage
333e486d932ca5a10446a3c98d6d065213913277268John Reck            for (int i = mSwapHistory.size() - 1;
334e486d932ca5a10446a3c98d6d065213913277268John Reck                    i > ((int) mSwapHistory.size()) - frame.bufferAge(); i--) {
335e486d932ca5a10446a3c98d6d065213913277268John Reck                dirty.join(mSwapHistory[i].damage);
336149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck            }
337149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck        }
3384f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    }
3394f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
340149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    mEglManager.damageFrame(frame, dirty);
341ddf2215d9807b641dbcb304779ef6b530f876ac7Chris Craik
342ddf2215d9807b641dbcb304779ef6b530f876ac7Chris Craik#if HWUI_NEW_OPS
343f158b49c888f722194afe5a80539a2b020c130bcChris Craik    FrameBuilder frameBuilder(mLayerUpdateQueue, dirty, frame.width(), frame.height(),
3446e068c0182f6f85bccb855a647510724d1c65a13Chris Craik            mRenderNodes, mLightGeometry, mContentDrawBounds, &Caches::getInstance());
3450b7e8245db728d127ada698be63d78b33fc6e4daChris Craik    mLayerUpdateQueue.clear();
34698787e6c9b2c10b1ab7820bdac168686025b924aChris Craik    BakedOpRenderer renderer(Caches::getInstance(), mRenderThread.renderState(),
34798787e6c9b2c10b1ab7820bdac168686025b924aChris Craik            mOpaque, mLightInfo);
348ddf2215d9807b641dbcb304779ef6b530f876ac7Chris Craik    // TODO: profiler().draw(mCanvas);
349f158b49c888f722194afe5a80539a2b020c130bcChris Craik    frameBuilder.replayBakedOps<BakedOpDispatcher>(renderer);
3505854b34881b1a747ac80b5077869ef270a92b1f4Chris Craik    bool drew = renderer.didDraw();
351ddf2215d9807b641dbcb304779ef6b530f876ac7Chris Craik
352ddf2215d9807b641dbcb304779ef6b530f876ac7Chris Craik#else
35364e445bf74bee2098781d608cedfd723d8cc88d3Chris Craik    mCanvas->prepareDirty(frame.width(), frame.height(),
35464e445bf74bee2098781d608cedfd723d8cc88d3Chris Craik            dirty.fLeft, dirty.fTop, dirty.fRight, dirty.fBottom, mOpaque);
355149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck
3564f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    Rect outBounds;
357b6c2624c8d9ee71d1c8d0aaf8082f92bb84b1418Robert Carr    // It there are multiple render nodes, they are laid out as follows:
358b6c2624c8d9ee71d1c8d0aaf8082f92bb84b1418Robert Carr    // #0 - backdrop (content + caption)
359b816087962aba0019b022303330f03b897b580edSkuhne    // #1 - content (positioned at (0,0) and clipped to - its bounds mContentDrawBounds)
360b6c2624c8d9ee71d1c8d0aaf8082f92bb84b1418Robert Carr    // #2 - additional overlay nodes
361ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    // Usually the backdrop cannot be seen since it will be entirely covered by the content. While
362ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    // resizing however it might become partially visible. The following render loop will crop the
363b6c2624c8d9ee71d1c8d0aaf8082f92bb84b1418Robert Carr    // backdrop against the content and draw the remaining part of it. It will then draw the content
364b6c2624c8d9ee71d1c8d0aaf8082f92bb84b1418Robert Carr    // cropped to the backdrop (since that indicates a shrinking of the window).
365b6c2624c8d9ee71d1c8d0aaf8082f92bb84b1418Robert Carr    //
366b6c2624c8d9ee71d1c8d0aaf8082f92bb84b1418Robert Carr    // Additional nodes will be drawn on top with no particular clipping semantics.
367b6c2624c8d9ee71d1c8d0aaf8082f92bb84b1418Robert Carr
368ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    // The bounds of the backdrop against which the content should be clipped.
369b816087962aba0019b022303330f03b897b580edSkuhne    Rect backdropBounds = mContentDrawBounds;
370b816087962aba0019b022303330f03b897b580edSkuhne    // Usually the contents bounds should be mContentDrawBounds - however - we will
371b816087962aba0019b022303330f03b897b580edSkuhne    // move it towards the fixed edge to give it a more stable appearance (for the moment).
372b816087962aba0019b022303330f03b897b580edSkuhne    Rect contentBounds;
373ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    // If there is no content bounds we ignore the layering as stated above and start with 2.
374b6c2624c8d9ee71d1c8d0aaf8082f92bb84b1418Robert Carr    int layer = (mContentDrawBounds.isEmpty() || mRenderNodes.size() == 1) ? 2 : 0;
375ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    // Draw all render nodes. Note that
376ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    for (const sp<RenderNode>& node : mRenderNodes) {
377ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne        if (layer == 0) { // Backdrop.
378b816087962aba0019b022303330f03b897b580edSkuhne            // Draw the backdrop clipped to the inverse content bounds, but assume that the content
379b816087962aba0019b022303330f03b897b580edSkuhne            // was moved to the upper left corner.
380ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            const RenderProperties& properties = node->properties();
381ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            Rect targetBounds(properties.getLeft(), properties.getTop(),
382ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                              properties.getRight(), properties.getBottom());
383b816087962aba0019b022303330f03b897b580edSkuhne            // Move the content bounds towards the fixed corner of the backdrop.
384b816087962aba0019b022303330f03b897b580edSkuhne            const int x = targetBounds.left;
385b816087962aba0019b022303330f03b897b580edSkuhne            const int y = targetBounds.top;
386b816087962aba0019b022303330f03b897b580edSkuhne            contentBounds.set(x, y, x + mContentDrawBounds.getWidth(),
387b816087962aba0019b022303330f03b897b580edSkuhne                                    y + mContentDrawBounds.getHeight());
388ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            // Remember the intersection of the target bounds and the intersection bounds against
389ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            // which we have to crop the content.
390b816087962aba0019b022303330f03b897b580edSkuhne            backdropBounds.set(x, y, x + backdropBounds.getWidth(), y + backdropBounds.getHeight());
391ac02eb9035a13a3d09c2def9ed63d04225eb2509Chris Craik            backdropBounds.doIntersect(targetBounds);
392ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            // Check if we have to draw something on the left side ...
393b816087962aba0019b022303330f03b897b580edSkuhne            if (targetBounds.left < contentBounds.left) {
394eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita                mCanvas->save(SaveFlags::Clip);
395ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                if (mCanvas->clipRect(targetBounds.left, targetBounds.top,
396b816087962aba0019b022303330f03b897b580edSkuhne                                      contentBounds.left, targetBounds.bottom,
397ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                                      SkRegion::kIntersect_Op)) {
398ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                    mCanvas->drawRenderNode(node.get(), outBounds);
399ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                }
400ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                // Reduce the target area by the area we have just painted.
401b816087962aba0019b022303330f03b897b580edSkuhne                targetBounds.left = std::min(contentBounds.left, targetBounds.right);
402ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                mCanvas->restore();
403ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            }
404ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            // ... or on the right side ...
405b816087962aba0019b022303330f03b897b580edSkuhne            if (targetBounds.right > contentBounds.right &&
406ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                !targetBounds.isEmpty()) {
407eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita                mCanvas->save(SaveFlags::Clip);
408b816087962aba0019b022303330f03b897b580edSkuhne                if (mCanvas->clipRect(contentBounds.right, targetBounds.top,
409ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                                      targetBounds.right, targetBounds.bottom,
410ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                                      SkRegion::kIntersect_Op)) {
411ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                    mCanvas->drawRenderNode(node.get(), outBounds);
412ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                }
413ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                // Reduce the target area by the area we have just painted.
414b816087962aba0019b022303330f03b897b580edSkuhne                targetBounds.right = std::max(targetBounds.left, contentBounds.right);
415ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                mCanvas->restore();
416ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            }
417ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            // ... or at the top ...
418b816087962aba0019b022303330f03b897b580edSkuhne            if (targetBounds.top < contentBounds.top &&
419ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                !targetBounds.isEmpty()) {
420eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita                mCanvas->save(SaveFlags::Clip);
421ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                if (mCanvas->clipRect(targetBounds.left, targetBounds.top, targetBounds.right,
422b816087962aba0019b022303330f03b897b580edSkuhne                                      contentBounds.top,
423ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                                      SkRegion::kIntersect_Op)) {
424ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                    mCanvas->drawRenderNode(node.get(), outBounds);
425ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                }
426ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                // Reduce the target area by the area we have just painted.
427b816087962aba0019b022303330f03b897b580edSkuhne                targetBounds.top = std::min(contentBounds.top, targetBounds.bottom);
428ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                mCanvas->restore();
429ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            }
430ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            // ... or at the bottom.
431b816087962aba0019b022303330f03b897b580edSkuhne            if (targetBounds.bottom > contentBounds.bottom &&
432ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                !targetBounds.isEmpty()) {
433eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita                mCanvas->save(SaveFlags::Clip);
434b816087962aba0019b022303330f03b897b580edSkuhne                if (mCanvas->clipRect(targetBounds.left, contentBounds.bottom, targetBounds.right,
435ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                                      targetBounds.bottom, SkRegion::kIntersect_Op)) {
436ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                    mCanvas->drawRenderNode(node.get(), outBounds);
437ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                }
438ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                mCanvas->restore();
439ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            }
440ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne        } else if (layer == 1) { // Content
441ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            // It gets cropped against the bounds of the backdrop to stay inside.
442eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita            mCanvas->save(SaveFlags::MatrixClip);
443b816087962aba0019b022303330f03b897b580edSkuhne
444b816087962aba0019b022303330f03b897b580edSkuhne            // We shift and clip the content to match its final location in the window.
445b816087962aba0019b022303330f03b897b580edSkuhne            const float left = mContentDrawBounds.left;
446b816087962aba0019b022303330f03b897b580edSkuhne            const float top = mContentDrawBounds.top;
447b816087962aba0019b022303330f03b897b580edSkuhne            const float dx = backdropBounds.left - left;
448b816087962aba0019b022303330f03b897b580edSkuhne            const float dy = backdropBounds.top - top;
449b816087962aba0019b022303330f03b897b580edSkuhne            const float width = backdropBounds.getWidth();
450b816087962aba0019b022303330f03b897b580edSkuhne            const float height = backdropBounds.getHeight();
451b6c2624c8d9ee71d1c8d0aaf8082f92bb84b1418Robert Carr
452b816087962aba0019b022303330f03b897b580edSkuhne            mCanvas->translate(dx, dy);
453b816087962aba0019b022303330f03b897b580edSkuhne            if (mCanvas->clipRect(left, top, left + width, top + height, SkRegion::kIntersect_Op)) {
454ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                mCanvas->drawRenderNode(node.get(), outBounds);
455ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            }
456ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            mCanvas->restore();
457ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne        } else { // draw the rest on top at will!
458ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            mCanvas->drawRenderNode(node.get(), outBounds);
459ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne        }
460ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne        layer++;
461ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    }
4624f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
463fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck    profiler().draw(mCanvas);
4644f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
465107843de4507b3511006cb9c77b8d0364374385aTom Hudson    bool drew = mCanvas->finish();
466ddf2215d9807b641dbcb304779ef6b530f876ac7Chris Craik#endif
4679372ac3621848085e77b867f220c0b5ffce4010dJohn Reck
468975591a7af883d866d86ab819e164c6004694744John Reck    GL_CHECKPOINT(LOW);
4699372ac3621848085e77b867f220c0b5ffce4010dJohn Reck
470ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    // Even if we decided to cancel the frame, from the perspective of jank
471ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    // metrics the frame was swapped at this point
472ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    mCurrentFrameInfo->markSwapBuffers();
473ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck
474107843de4507b3511006cb9c77b8d0364374385aTom Hudson    if (drew) {
475149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck        if (CC_UNLIKELY(!mEglManager.swapBuffers(frame, screenDirty))) {
476149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck            setSurface(nullptr);
477149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck        }
478e486d932ca5a10446a3c98d6d065213913277268John Reck        SwapHistory& swap = mSwapHistory.next();
479e486d932ca5a10446a3c98d6d065213913277268John Reck        swap.damage = screenDirty;
480e486d932ca5a10446a3c98d6d065213913277268John Reck        swap.swapTime = systemTime(CLOCK_MONOTONIC);
481e486d932ca5a10446a3c98d6d065213913277268John Reck        swap.vsyncTime = mRenderThread.timeLord().latestVsync();
482149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck        mHaveNewSurface = false;
48323b797ab5151eb2474f3bdd679f2f07bfd723042John Reck    }
484fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck
485ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    // TODO: Use a fence for real completion?
486ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    mCurrentFrameInfo->markFrameCompleted();
487149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck
488149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck#if LOG_FRAMETIME_MMA
489149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    float thisFrame = mCurrentFrameInfo->duration(
490149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck            FrameInfoIndex::IssueDrawCommandsStart,
491149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck            FrameInfoIndex::FrameCompleted) / NANOS_PER_MILLIS_F;
492149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    if (sFrameCount) {
493149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck        sBenchMma = ((9 * sBenchMma) + thisFrame) / 10;
494149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    } else {
495149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck        sBenchMma = thisFrame;
496149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    }
497149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    if (++sFrameCount == 10) {
498149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck        sFrameCount = 1;
499149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck        ALOGD("Average frame time: %.4f", sBenchMma);
500149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    }
501149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck#endif
502149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck
503edc524c90506d80e0fc5fb67e8de7b8f3ef53439John Reck    mJankTracker.addFrame(*mCurrentFrameInfo);
504ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    mRenderThread.jankTracker().addFrame(*mCurrentFrameInfo);
50506f5bc70a667a02b14e31d3f53f91d3661e30666Andres Morales    if (CC_UNLIKELY(mFrameStatsReporter.get() != nullptr)) {
50606f5bc70a667a02b14e31d3f53f91d3661e30666Andres Morales        mFrameStatsReporter->reportFrameStats(mCurrentFrameInfo->data());
50706f5bc70a667a02b14e31d3f53f91d3661e30666Andres Morales    }
50838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck
50938e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    GpuMemoryTracker::onFrameCompleted();
51023b797ab5151eb2474f3bdd679f2f07bfd723042John Reck}
51123b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
512e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck// Called by choreographer to do an RT-driven animation
51318f16e6fba74eda173e1e7c869e6e2e2acc073ffJohn Reckvoid CanvasContext::doFrame() {
514adfeec94560c6661c5e6600b10c05f34a40f6454Chris Craik#if HWUI_NEW_OPS
515adfeec94560c6661c5e6600b10c05f34a40f6454Chris Craik    if (CC_UNLIKELY(mEglSurface == EGL_NO_SURFACE)) return;
516adfeec94560c6661c5e6600b10c05f34a40f6454Chris Craik#else
517adfeec94560c6661c5e6600b10c05f34a40f6454Chris Craik    if (CC_UNLIKELY(!mCanvas || mEglSurface == EGL_NO_SURFACE)) return;
518adfeec94560c6661c5e6600b10c05f34a40f6454Chris Craik#endif
519ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    prepareAndDraw(nullptr);
520ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne}
521368cdd85268999997fb495cf90c4417221797de0John Reck
522ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhnevoid CanvasContext::prepareAndDraw(RenderNode* node) {
523e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck    ATRACE_CALL();
524e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck
5257f667e7a0823d52eed2ed64a31b125f6b8da21cbMatthew Bouyack    nsecs_t vsync = mRenderThread.timeLord().computeFrameTimeNanos();
526ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    int64_t frameInfo[UI_THREAD_FRAME_INFO_SIZE];
527ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    UiFrameInfoBuilder(frameInfo)
5281b54fb27ac48495ed0b33868fda5776fb49fe0f3Chris Craik        .addFlag(FrameInfoFlags::RTAnimation)
5297f667e7a0823d52eed2ed64a31b125f6b8da21cbMatthew Bouyack        .setVsync(vsync, vsync);
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
5913145247b3e8563f25e9d908579ce03060f3e880bChris Craik    // TODO: support buildLayer
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