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"
22d04794a9a3f9edc8b7ca336175d66eb81a8f55faJohn Reck#include "DeferredLayerUpdater.h"
233b20251a355c88193c439f928a84ae69483fb488John Reck#include "EglManager.h"
240b7e8245db728d127ada698be63d78b33fc6e4daChris Craik#include "LayerUpdateQueue.h"
25d04794a9a3f9edc8b7ca336175d66eb81a8f55faJohn Reck#include "LayerRenderer.h"
26d04794a9a3f9edc8b7ca336175d66eb81a8f55faJohn Reck#include "OpenGLRenderer.h"
27d04794a9a3f9edc8b7ca336175d66eb81a8f55faJohn Reck#include "Properties.h"
284f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck#include "RenderThread.h"
29dccca44ffda4836b56a21da95a046c9708ffd49csergeyv#include "hwui/Canvas.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() {
7951f2d606dcbfba3cc5b03dfea37c1304b91c232fJohn Reck    destroy(nullptr);
80443a714fa7c0dd07fee3527cc5bc3d3ca1fb7d44John Reck    mRenderThread.renderState().unregisterCanvasContext(this);
814f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
824f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
8351f2d606dcbfba3cc5b03dfea37c1304b91c232fJohn Reckvoid CanvasContext::destroy(TreeObserver* observer) {
8417035b0211a3c9d45ea46a99217a6acbe76e8fbeJohn Reck    stopDrawing();
85d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    setSurface(nullptr);
8651f2d606dcbfba3cc5b03dfea37c1304b91c232fJohn Reck    freePrefetchedLayers(observer);
8751f2d606dcbfba3cc5b03dfea37c1304b91c232fJohn Reck    destroyHardwareResources(observer);
88e2478d45ccbe5b6abb360ac9d44771b5f4a50bdeJohn Reck    mAnimationContext->destroy();
896246d27813f25b85f6e4b5cb1121fe8484bcce2dChris Craik#if !HWUI_NEW_OPS
904f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    if (mCanvas) {
914f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck        delete mCanvas;
92d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik        mCanvas = nullptr;
934f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    }
946246d27813f25b85f6e4b5cb1121fe8484bcce2dChris Craik#endif
9523b797ab5151eb2474f3bdd679f2f07bfd723042John Reck}
9623b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
97f648108f83d4e74811919e9811efb8fcc184b8a3John Reckvoid CanvasContext::setSurface(Surface* surface) {
98fbc8df03e498baf47ff1a5e05e182f1bcd60c770John Reck    ATRACE_CALL();
99fbc8df03e498baf47ff1a5e05e182f1bcd60c770John Reck
100f648108f83d4e74811919e9811efb8fcc184b8a3John Reck    mNativeSurface = surface;
101a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck
10223b797ab5151eb2474f3bdd679f2f07bfd723042John Reck    if (mEglSurface != EGL_NO_SURFACE) {
1033b20251a355c88193c439f928a84ae69483fb488John Reck        mEglManager.destroySurface(mEglSurface);
10423b797ab5151eb2474f3bdd679f2f07bfd723042John Reck        mEglSurface = EGL_NO_SURFACE;
10523b797ab5151eb2474f3bdd679f2f07bfd723042John Reck    }
10623b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
107f648108f83d4e74811919e9811efb8fcc184b8a3John Reck    if (surface) {
108f648108f83d4e74811919e9811efb8fcc184b8a3John Reck        mEglSurface = mEglManager.createSurface(surface);
10923b797ab5151eb2474f3bdd679f2f07bfd723042John Reck    }
11023b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
11128912a508493e583c48772e2a234e0ed66849490John Reck    mFrameNumber = -1;
11228912a508493e583c48772e2a234e0ed66849490John Reck
11323b797ab5151eb2474f3bdd679f2f07bfd723042John Reck    if (mEglSurface != EGL_NO_SURFACE) {
1141125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck        const bool preserveBuffer = (mSwapBehavior != kSwap_discardBuffer);
1151125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck        mBufferPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer);
1164f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck        mHaveNewSurface = true;
117e486d932ca5a10446a3c98d6d065213913277268John Reck        mSwapHistory.clear();
118368cdd85268999997fb495cf90c4417221797de0John Reck    } else {
119368cdd85268999997fb495cf90c4417221797de0John Reck        mRenderThread.removeFrameCallback(this);
1204f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    }
1214f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
1224f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
1231125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reckvoid CanvasContext::setSwapBehavior(SwapBehavior swapBehavior) {
1241125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck    mSwapBehavior = swapBehavior;
1251125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck}
1261125d1fa92ab9f3b8315bbfb72e038b62dfd454bJohn Reck
127f648108f83d4e74811919e9811efb8fcc184b8a3John Reckvoid CanvasContext::initialize(Surface* surface) {
128f648108f83d4e74811919e9811efb8fcc184b8a3John Reck    setSurface(surface);
129b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik#if !HWUI_NEW_OPS
1300bcd0cb6b1193168fa2840855195347488daab9eThomas Buhot    if (mCanvas) return;
1313b20251a355c88193c439f928a84ae69483fb488John Reck    mCanvas = new OpenGLRenderer(mRenderThread.renderState());
1324f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    mCanvas->initProperties();
133b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik#endif
1344f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
1354f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
136f648108f83d4e74811919e9811efb8fcc184b8a3John Reckvoid CanvasContext::updateSurface(Surface* surface) {
137f648108f83d4e74811919e9811efb8fcc184b8a3John Reck    setSurface(surface);
138f7d9c1dc84671d4e99657ef071d275700d85bb11John Reck}
139f7d9c1dc84671d4e99657ef071d275700d85bb11John Reck
140f648108f83d4e74811919e9811efb8fcc184b8a3John Reckbool CanvasContext::pauseSurface(Surface* surface) {
14101a5ea35fbba4c5bb1d7790ae1677a2fa752e042John Reck    return mRenderThread.removeFrameCallback(this);
1424f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
1434f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
1448afcc76920499d0a384dba1470c5a377f80ed768John Reckvoid CanvasContext::setStopped(bool stopped) {
1458afcc76920499d0a384dba1470c5a377f80ed768John Reck    if (mStopped != stopped) {
1468afcc76920499d0a384dba1470c5a377f80ed768John Reck        mStopped = stopped;
1478afcc76920499d0a384dba1470c5a377f80ed768John Reck        if (mStopped) {
1488afcc76920499d0a384dba1470c5a377f80ed768John Reck            mRenderThread.removeFrameCallback(this);
1498afcc76920499d0a384dba1470c5a377f80ed768John Reck            if (mEglManager.isCurrent(mEglSurface)) {
1508afcc76920499d0a384dba1470c5a377f80ed768John Reck                mEglManager.makeCurrent(EGL_NO_SURFACE);
1518afcc76920499d0a384dba1470c5a377f80ed768John Reck            }
152306f331f91a86da271ce30d4f14d6badf0d25704John Reck        } else if (mIsDirty && hasSurface()) {
153306f331f91a86da271ce30d4f14d6badf0d25704John Reck            mRenderThread.postFrameCallback(this);
1548afcc76920499d0a384dba1470c5a377f80ed768John Reck        }
1558afcc76920499d0a384dba1470c5a377f80ed768John Reck    }
1568afcc76920499d0a384dba1470c5a377f80ed768John Reck}
1578afcc76920499d0a384dba1470c5a377f80ed768John Reck
158284b24358410cb0200e525a5ba36994090c83f20Chris Craik// TODO: don't pass viewport size, it's automatic via EGL
15950210d912925aef14e4ce69be82e4949122a3cd9Alan Viverettevoid CanvasContext::setup(int width, int height, float lightRadius,
16064bb413a664001c95c8439cf097dc3033f4ed733Andreas Gampe        uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
16198787e6c9b2c10b1ab7820bdac168686025b924aChris Craik#if HWUI_NEW_OPS
1626e068c0182f6f85bccb855a647510724d1c65a13Chris Craik    mLightGeometry.radius = lightRadius;
16398787e6c9b2c10b1ab7820bdac168686025b924aChris Craik    mLightInfo.ambientShadowAlpha = ambientShadowAlpha;
16498787e6c9b2c10b1ab7820bdac168686025b924aChris Craik    mLightInfo.spotShadowAlpha = spotShadowAlpha;
16598787e6c9b2c10b1ab7820bdac168686025b924aChris Craik#else
1664f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    if (!mCanvas) return;
16750210d912925aef14e4ce69be82e4949122a3cd9Alan Viverette    mCanvas->initLight(lightRadius, ambientShadowAlpha, spotShadowAlpha);
16898787e6c9b2c10b1ab7820bdac168686025b924aChris Craik#endif
16950210d912925aef14e4ce69be82e4949122a3cd9Alan Viverette}
17050210d912925aef14e4ce69be82e4949122a3cd9Alan Viverette
17150210d912925aef14e4ce69be82e4949122a3cd9Alan Viverettevoid CanvasContext::setLightCenter(const Vector3& lightCenter) {
17298787e6c9b2c10b1ab7820bdac168686025b924aChris Craik#if HWUI_NEW_OPS
1736e068c0182f6f85bccb855a647510724d1c65a13Chris Craik    mLightGeometry.center = lightCenter;
17498787e6c9b2c10b1ab7820bdac168686025b924aChris Craik#else
17550210d912925aef14e4ce69be82e4949122a3cd9Alan Viverette    if (!mCanvas) return;
17650210d912925aef14e4ce69be82e4949122a3cd9Alan Viverette    mCanvas->setLightCenter(lightCenter);
17798787e6c9b2c10b1ab7820bdac168686025b924aChris Craik#endif
1784f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}
1794f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
18063a06673253914510bbeebd500655008682dade1John Reckvoid CanvasContext::setOpaque(bool opaque) {
18163a06673253914510bbeebd500655008682dade1John Reck    mOpaque = opaque;
18263a06673253914510bbeebd500655008682dade1John Reck}
18363a06673253914510bbeebd500655008682dade1John Reck
1848afcc76920499d0a384dba1470c5a377f80ed768John Reckbool CanvasContext::makeCurrent() {
1858afcc76920499d0a384dba1470c5a377f80ed768John Reck    if (mStopped) return false;
1868afcc76920499d0a384dba1470c5a377f80ed768John Reck
187dbc9a86d05e5e835051de22f6cb30ec1921e9705John Reck    // TODO: Figure out why this workaround is needed, see b/13913604
188dbc9a86d05e5e835051de22f6cb30ec1921e9705John Reck    // In the meantime this matches the behavior of GLRenderer, so it is not a regression
189f2dcc2aecb94e726096256c47b913ed0a57ae7e2John Reck    EGLint error = 0;
190f2dcc2aecb94e726096256c47b913ed0a57ae7e2John Reck    mHaveNewSurface |= mEglManager.makeCurrent(mEglSurface, &error);
191f2dcc2aecb94e726096256c47b913ed0a57ae7e2John Reck    if (error) {
192f2dcc2aecb94e726096256c47b913ed0a57ae7e2John Reck        setSurface(nullptr);
193f2dcc2aecb94e726096256c47b913ed0a57ae7e2John Reck    }
1948afcc76920499d0a384dba1470c5a377f80ed768John Reck    return !error;
195860d155f866cc15a725e7ce03763280987f24901John Reck}
196860d155f866cc15a725e7ce03763280987f24901John Reck
197bf3c602284f9a344faf185c3a5e94a264ba44c4fJohn Reckstatic bool wasSkipped(FrameInfo* info) {
1981b54fb27ac48495ed0b33868fda5776fb49fe0f3Chris Craik    return info && ((*info)[FrameInfoIndex::Flags] & FrameInfoFlags::SkippedFrame);
199bf3c602284f9a344faf185c3a5e94a264ba44c4fJohn Reck}
200bf3c602284f9a344faf185c3a5e94a264ba44c4fJohn Reck
201ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhnevoid CanvasContext::prepareTree(TreeInfo& info, int64_t* uiFrameInfo,
202ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne        int64_t syncQueued, RenderNode* target) {
203f9be77940e365036fecd8cc0e491e8545c34e79bJohn Reck    mRenderThread.removeFrameCallback(this);
20418f16e6fba74eda173e1e7c869e6e2e2acc073ffJohn Reck
205bf3c602284f9a344faf185c3a5e94a264ba44c4fJohn Reck    // If the previous frame was dropped we don't need to hold onto it, so
206bf3c602284f9a344faf185c3a5e94a264ba44c4fJohn Reck    // just keep using the previous frame's structure instead
207bf3c602284f9a344faf185c3a5e94a264ba44c4fJohn Reck    if (!wasSkipped(mCurrentFrameInfo)) {
208bf3c602284f9a344faf185c3a5e94a264ba44c4fJohn Reck        mCurrentFrameInfo = &mFrames.next();
209bf3c602284f9a344faf185c3a5e94a264ba44c4fJohn Reck    }
210ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    mCurrentFrameInfo->importUiThreadInfo(uiFrameInfo);
211be3fba05e823f740f65b2679929347dc3dd282adJohn Reck    mCurrentFrameInfo->set(FrameInfoIndex::SyncQueued) = syncQueued;
212ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    mCurrentFrameInfo->markSyncStart();
213ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck
214e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck    info.damageAccumulator = &mDamageAccumulator;
2150b7e8245db728d127ada698be63d78b33fc6e4daChris Craik#if HWUI_NEW_OPS
2160b7e8245db728d127ada698be63d78b33fc6e4daChris Craik    info.layerUpdateQueue = &mLayerUpdateQueue;
2170b7e8245db728d127ada698be63d78b33fc6e4daChris Craik#else
21825fbb3fa1138675379102a44405852555cefccbdJohn Reck    info.renderer = mCanvas;
2190b7e8245db728d127ada698be63d78b33fc6e4daChris Craik#endif
22000e79c9947b741194ff6c0d08ede9b3befbf9c9dJohn Reck
221ec845a215e343cdb3b2e4c7b6aff7b24beb0236bJohn Reck    mAnimationContext->startFrame(info.mode);
222ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    for (const sp<RenderNode>& node : mRenderNodes) {
223ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne        // Only the primary target node will be drawn full - all other nodes would get drawn in
224ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne        // real time mode. In case of a window, the primary node is the window content and the other
225ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne        // node(s) are non client / filler nodes.
226ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne        info.mode = (node.get() == target ? TreeInfo::MODE_FULL : TreeInfo::MODE_RT_ONLY);
227ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne        node->prepareTree(info);
228975591a7af883d866d86ab819e164c6004694744John Reck        GL_CHECKPOINT(MODERATE);
229ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    }
230119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    mAnimationContext->runRemainingAnimations(info);
231975591a7af883d866d86ab819e164c6004694744John Reck    GL_CHECKPOINT(MODERATE);
232e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck
23351f2d606dcbfba3cc5b03dfea37c1304b91c232fJohn Reck    freePrefetchedLayers(info.observer);
234975591a7af883d866d86ab819e164c6004694744John Reck    GL_CHECKPOINT(MODERATE);
235998a6d81896df8b662cc10ddeb35087b78b38d72John Reck
236306f331f91a86da271ce30d4f14d6badf0d25704John Reck    mIsDirty = true;
237306f331f91a86da271ce30d4f14d6badf0d25704John Reck
238f648108f83d4e74811919e9811efb8fcc184b8a3John Reck    if (CC_UNLIKELY(!mNativeSurface.get())) {
2391b54fb27ac48495ed0b33868fda5776fb49fe0f3Chris Craik        mCurrentFrameInfo->addFlag(FrameInfoFlags::SkippedFrame);
240aa95a88327d9a3ac8a4a00b065b78ac0f28b3a19John Reck        info.out.canDrawThisFrame = false;
241aa95a88327d9a3ac8a4a00b065b78ac0f28b3a19John Reck        return;
242aa95a88327d9a3ac8a4a00b065b78ac0f28b3a19John Reck    }
243aa95a88327d9a3ac8a4a00b065b78ac0f28b3a19John Reck
244e486d932ca5a10446a3c98d6d065213913277268John Reck    if (CC_LIKELY(mSwapHistory.size())) {
245e486d932ca5a10446a3c98d6d065213913277268John Reck        nsecs_t latestVsync = mRenderThread.timeLord().latestVsync();
246e486d932ca5a10446a3c98d6d065213913277268John Reck        const SwapHistory& lastSwap = mSwapHistory.back();
24752b783f76af58a948dad4d3b4d1b7ad7979347ceJohn Reck        nsecs_t vsyncDelta = std::abs(lastSwap.vsyncTime - latestVsync);
248e486d932ca5a10446a3c98d6d065213913277268John Reck        // The slight fudge-factor is to deal with cases where
249e486d932ca5a10446a3c98d6d065213913277268John Reck        // the vsync was estimated due to being slow handling the signal.
250e486d932ca5a10446a3c98d6d065213913277268John Reck        // See the logic in TimeLord#computeFrameTimeNanos or in
251e486d932ca5a10446a3c98d6d065213913277268John Reck        // Choreographer.java for details on when this happens
252e486d932ca5a10446a3c98d6d065213913277268John Reck        if (vsyncDelta < 2_ms) {
253e486d932ca5a10446a3c98d6d065213913277268John Reck            // Already drew for this vsync pulse, UI draw request missed
254e486d932ca5a10446a3c98d6d065213913277268John Reck            // the deadline for RT animations
255e486d932ca5a10446a3c98d6d065213913277268John Reck            info.out.canDrawThisFrame = false;
256e486d932ca5a10446a3c98d6d065213913277268John Reck        } else if (lastSwap.swapTime < latestVsync) {
257e486d932ca5a10446a3c98d6d065213913277268John Reck            info.out.canDrawThisFrame = true;
258e486d932ca5a10446a3c98d6d065213913277268John Reck        } else {
259e486d932ca5a10446a3c98d6d065213913277268John Reck            // We're maybe behind? Find out for sure
260e486d932ca5a10446a3c98d6d065213913277268John Reck            int runningBehind = 0;
261f648108f83d4e74811919e9811efb8fcc184b8a3John Reck            // TODO: Have this method be on Surface, too, not just ANativeWindow...
262f648108f83d4e74811919e9811efb8fcc184b8a3John Reck            ANativeWindow* window = mNativeSurface.get();
263f648108f83d4e74811919e9811efb8fcc184b8a3John Reck            window->query(window, NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &runningBehind);
264e486d932ca5a10446a3c98d6d065213913277268John Reck            info.out.canDrawThisFrame = !runningBehind;
265e486d932ca5a10446a3c98d6d065213913277268John Reck        }
266e486d932ca5a10446a3c98d6d065213913277268John Reck    } else {
267e486d932ca5a10446a3c98d6d065213913277268John Reck        info.out.canDrawThisFrame = true;
268e486d932ca5a10446a3c98d6d065213913277268John Reck    }
269a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck
270aef9dc8d186bd5f78068ab2d5240b5e9c8ab44b6John Reck    if (!info.out.canDrawThisFrame) {
2711b54fb27ac48495ed0b33868fda5776fb49fe0f3Chris Craik        mCurrentFrameInfo->addFlag(FrameInfoFlags::SkippedFrame);
272aef9dc8d186bd5f78068ab2d5240b5e9c8ab44b6John Reck    }
273aef9dc8d186bd5f78068ab2d5240b5e9c8ab44b6John Reck
274a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    if (info.out.hasAnimations || !info.out.canDrawThisFrame) {
275cd028f336e36b22dbe8cf623eb5bd2361314495cJohn Reck        if (!info.out.requiresUiRedraw) {
276f9be77940e365036fecd8cc0e491e8545c34e79bJohn Reck            // If animationsNeedsRedraw is set don't bother posting for an RT anim
277f9be77940e365036fecd8cc0e491e8545c34e79bJohn Reck            // as we will just end up fighting the UI thread.
278f9be77940e365036fecd8cc0e491e8545c34e79bJohn Reck            mRenderThread.postFrameCallback(this);
279f9be77940e365036fecd8cc0e491e8545c34e79bJohn Reck        }
280e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck    }
281e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck}
282e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck
283f47a594f5250b1914c36423ee6b371f0b8db09d0John Reckvoid CanvasContext::stopDrawing() {
284f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck    mRenderThread.removeFrameCallback(this);
285f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck}
286f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck
287a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reckvoid CanvasContext::notifyFramePending() {
288a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    ATRACE_CALL();
289a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    mRenderThread.pushBackFrameCallback(this);
290a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck}
291a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck
292e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reckvoid CanvasContext::draw() {
293b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik#if !HWUI_NEW_OPS
2944f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    LOG_ALWAYS_FATAL_IF(!mCanvas || mEglSurface == EGL_NO_SURFACE,
295a7090e0cfd7c719a6d4c03aae34f5db98754cbddChris Craik            "drawRenderNode called on a context with no canvas or surface!");
296b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik#endif
2974f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
298e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck    SkRect dirty;
299e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck    mDamageAccumulator.finish(&dirty);
300e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck
3016d4d0db312fa8a9fce629dd92aa133c6f8249cf8John Reck    // TODO: Re-enable after figuring out cause of b/22592975
3026d4d0db312fa8a9fce629dd92aa133c6f8249cf8John Reck//    if (dirty.isEmpty() && Properties::skipEmptyFrames) {
3036d4d0db312fa8a9fce629dd92aa133c6f8249cf8John Reck//        mCurrentFrameInfo->addFlag(FrameInfoFlags::SkippedFrame);
3046d4d0db312fa8a9fce629dd92aa133c6f8249cf8John Reck//        return;
3056d4d0db312fa8a9fce629dd92aa133c6f8249cf8John Reck//    }
306240ff6246a29602539fd0295274e1c769e743a2eJohn Reck
307240ff6246a29602539fd0295274e1c769e743a2eJohn Reck    mCurrentFrameInfo->markIssueDrawCommandsStart();
308240ff6246a29602539fd0295274e1c769e743a2eJohn Reck
309149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    Frame frame = mEglManager.beginFrame(mEglSurface);
310b565df13a9e5c7b1d7d93bdfa4a793752d66d3ccChris Craik
31177c40109cf25d29f85ee6c13aeb96e22e55f33abJohn Reck    if (frame.width() != mLastFrameWidth || frame.height() != mLastFrameHeight) {
31264e445bf74bee2098781d608cedfd723d8cc88d3Chris Craik        // can't rely on prior content of window if viewport size changes
313e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck        dirty.setEmpty();
31477c40109cf25d29f85ee6c13aeb96e22e55f33abJohn Reck        mLastFrameWidth = frame.width();
31577c40109cf25d29f85ee6c13aeb96e22e55f33abJohn Reck        mLastFrameHeight = frame.height();
316149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    } else if (mHaveNewSurface || frame.bufferAge() == 0) {
317149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck        // New surface needs a full draw
318e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck        dirty.setEmpty();
319fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck    } else {
320149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck        if (!dirty.isEmpty() && !dirty.intersect(0, 0, frame.width(), frame.height())) {
3210a97330b98dd633b58dcfff405d94476c89e867dJohn Reck            ALOGW("Dirty " RECT_STRING " doesn't intersect with 0 0 %d %d ?",
322149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck                    SK_RECT_ARGS(dirty), frame.width(), frame.height());
3230a97330b98dd633b58dcfff405d94476c89e867dJohn Reck            dirty.setEmpty();
3240a97330b98dd633b58dcfff405d94476c89e867dJohn Reck        }
325e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck        profiler().unionDirty(&dirty);
3264f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    }
3274f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
328149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    if (dirty.isEmpty()) {
329149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck        dirty.set(0, 0, frame.width(), frame.height());
33013d1b4ab10fbee5e81a2ba1ac59cfae1e51d3ef0Season Li    }
33113d1b4ab10fbee5e81a2ba1ac59cfae1e51d3ef0Season Li
332149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    // At this point dirty is the area of the screen to update. However,
333149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    // the area of the frame we need to repaint is potentially different, so
334149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    // stash the screen area for later
335149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    SkRect screenDirty(dirty);
336149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck
337149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    // If the buffer age is 0 we do a full-screen repaint (handled above)
338149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    // If the buffer age is 1 the buffer contents are the same as they were
339149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    // last frame so there's nothing to union() against
340149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    // Therefore we only care about the > 1 case.
341149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    if (frame.bufferAge() > 1) {
342e486d932ca5a10446a3c98d6d065213913277268John Reck        if (frame.bufferAge() > (int) mSwapHistory.size()) {
343149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck            // We don't have enough history to handle this old of a buffer
344149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck            // Just do a full-draw
345149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck            dirty.set(0, 0, frame.width(), frame.height());
346149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck        } else {
347149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck            // At this point we haven't yet added the latest frame
348149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck            // to the damage history (happens below)
349149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck            // So we need to damage
350e486d932ca5a10446a3c98d6d065213913277268John Reck            for (int i = mSwapHistory.size() - 1;
351e486d932ca5a10446a3c98d6d065213913277268John Reck                    i > ((int) mSwapHistory.size()) - frame.bufferAge(); i--) {
352e486d932ca5a10446a3c98d6d065213913277268John Reck                dirty.join(mSwapHistory[i].damage);
353149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck            }
354149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck        }
3554f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    }
3564f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
357149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    mEglManager.damageFrame(frame, dirty);
358ddf2215d9807b641dbcb304779ef6b530f876ac7Chris Craik
359ddf2215d9807b641dbcb304779ef6b530f876ac7Chris Craik#if HWUI_NEW_OPS
3603a5811b50157e7ba50854caf957e806aee794d39Chris Craik    auto& caches = Caches::getInstance();
3619cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik    FrameBuilder frameBuilder(dirty, frame.width(), frame.height(), mLightGeometry, caches);
3629cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik
3639cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik    frameBuilder.deferLayers(mLayerUpdateQueue);
3640b7e8245db728d127ada698be63d78b33fc6e4daChris Craik    mLayerUpdateQueue.clear();
3659cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik
3669cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik    frameBuilder.deferRenderNodeScene(mRenderNodes, mContentDrawBounds);
3679cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik
3681dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik    BakedOpRenderer renderer(caches, mRenderThread.renderState(),
36998787e6c9b2c10b1ab7820bdac168686025b924aChris Craik            mOpaque, mLightInfo);
370f158b49c888f722194afe5a80539a2b020c130bcChris Craik    frameBuilder.replayBakedOps<BakedOpDispatcher>(renderer);
3711dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik    profiler().draw(&renderer);
3725854b34881b1a747ac80b5077869ef270a92b1f4Chris Craik    bool drew = renderer.didDraw();
373ddf2215d9807b641dbcb304779ef6b530f876ac7Chris Craik
3741dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik    // post frame cleanup
3751dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik    caches.clearGarbage();
3761dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik    caches.pathCache.trim();
3771dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik    caches.tessellationCache.trim();
3781dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik
3791dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik#if DEBUG_MEMORY_USAGE
3801dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik    mCaches.dumpMemoryUsage();
3811dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik#else
3821dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik    if (CC_UNLIKELY(Properties::debugLevel & kDebugMemory)) {
3831dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik        caches.dumpMemoryUsage();
3841dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik    }
3851dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik#endif
3861dfa0704964c17e45775b9e01f1fa0b1a10774f7Chris Craik
387ddf2215d9807b641dbcb304779ef6b530f876ac7Chris Craik#else
38864e445bf74bee2098781d608cedfd723d8cc88d3Chris Craik    mCanvas->prepareDirty(frame.width(), frame.height(),
38964e445bf74bee2098781d608cedfd723d8cc88d3Chris Craik            dirty.fLeft, dirty.fTop, dirty.fRight, dirty.fBottom, mOpaque);
390149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck
3914f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck    Rect outBounds;
392b6c2624c8d9ee71d1c8d0aaf8082f92bb84b1418Robert Carr    // It there are multiple render nodes, they are laid out as follows:
393b6c2624c8d9ee71d1c8d0aaf8082f92bb84b1418Robert Carr    // #0 - backdrop (content + caption)
394b816087962aba0019b022303330f03b897b580edSkuhne    // #1 - content (positioned at (0,0) and clipped to - its bounds mContentDrawBounds)
395b6c2624c8d9ee71d1c8d0aaf8082f92bb84b1418Robert Carr    // #2 - additional overlay nodes
396ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    // Usually the backdrop cannot be seen since it will be entirely covered by the content. While
397ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    // resizing however it might become partially visible. The following render loop will crop the
398b6c2624c8d9ee71d1c8d0aaf8082f92bb84b1418Robert Carr    // backdrop against the content and draw the remaining part of it. It will then draw the content
399b6c2624c8d9ee71d1c8d0aaf8082f92bb84b1418Robert Carr    // cropped to the backdrop (since that indicates a shrinking of the window).
400b6c2624c8d9ee71d1c8d0aaf8082f92bb84b1418Robert Carr    //
401b6c2624c8d9ee71d1c8d0aaf8082f92bb84b1418Robert Carr    // Additional nodes will be drawn on top with no particular clipping semantics.
402b6c2624c8d9ee71d1c8d0aaf8082f92bb84b1418Robert Carr
403ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    // The bounds of the backdrop against which the content should be clipped.
404b816087962aba0019b022303330f03b897b580edSkuhne    Rect backdropBounds = mContentDrawBounds;
405b816087962aba0019b022303330f03b897b580edSkuhne    // Usually the contents bounds should be mContentDrawBounds - however - we will
406b816087962aba0019b022303330f03b897b580edSkuhne    // move it towards the fixed edge to give it a more stable appearance (for the moment).
407b816087962aba0019b022303330f03b897b580edSkuhne    Rect contentBounds;
408ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    // If there is no content bounds we ignore the layering as stated above and start with 2.
409b6c2624c8d9ee71d1c8d0aaf8082f92bb84b1418Robert Carr    int layer = (mContentDrawBounds.isEmpty() || mRenderNodes.size() == 1) ? 2 : 0;
410ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    // Draw all render nodes. Note that
411ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    for (const sp<RenderNode>& node : mRenderNodes) {
412ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne        if (layer == 0) { // Backdrop.
413b816087962aba0019b022303330f03b897b580edSkuhne            // Draw the backdrop clipped to the inverse content bounds, but assume that the content
414b816087962aba0019b022303330f03b897b580edSkuhne            // was moved to the upper left corner.
415ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            const RenderProperties& properties = node->properties();
416ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            Rect targetBounds(properties.getLeft(), properties.getTop(),
417ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                              properties.getRight(), properties.getBottom());
418b816087962aba0019b022303330f03b897b580edSkuhne            // Move the content bounds towards the fixed corner of the backdrop.
419b816087962aba0019b022303330f03b897b580edSkuhne            const int x = targetBounds.left;
420b816087962aba0019b022303330f03b897b580edSkuhne            const int y = targetBounds.top;
421b816087962aba0019b022303330f03b897b580edSkuhne            contentBounds.set(x, y, x + mContentDrawBounds.getWidth(),
422b816087962aba0019b022303330f03b897b580edSkuhne                                    y + mContentDrawBounds.getHeight());
423ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            // Remember the intersection of the target bounds and the intersection bounds against
424ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            // which we have to crop the content.
425b816087962aba0019b022303330f03b897b580edSkuhne            backdropBounds.set(x, y, x + backdropBounds.getWidth(), y + backdropBounds.getHeight());
426ac02eb9035a13a3d09c2def9ed63d04225eb2509Chris Craik            backdropBounds.doIntersect(targetBounds);
427ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            // Check if we have to draw something on the left side ...
428b816087962aba0019b022303330f03b897b580edSkuhne            if (targetBounds.left < contentBounds.left) {
429eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita                mCanvas->save(SaveFlags::Clip);
430ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                if (mCanvas->clipRect(targetBounds.left, targetBounds.top,
431b816087962aba0019b022303330f03b897b580edSkuhne                                      contentBounds.left, targetBounds.bottom,
432ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                                      SkRegion::kIntersect_Op)) {
433ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                    mCanvas->drawRenderNode(node.get(), outBounds);
434ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                }
435ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                // Reduce the target area by the area we have just painted.
436b816087962aba0019b022303330f03b897b580edSkuhne                targetBounds.left = std::min(contentBounds.left, targetBounds.right);
437ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                mCanvas->restore();
438ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            }
439ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            // ... or on the right side ...
440b816087962aba0019b022303330f03b897b580edSkuhne            if (targetBounds.right > contentBounds.right &&
441ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                !targetBounds.isEmpty()) {
442eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita                mCanvas->save(SaveFlags::Clip);
443b816087962aba0019b022303330f03b897b580edSkuhne                if (mCanvas->clipRect(contentBounds.right, targetBounds.top,
444ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                                      targetBounds.right, targetBounds.bottom,
445ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                                      SkRegion::kIntersect_Op)) {
446ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                    mCanvas->drawRenderNode(node.get(), outBounds);
447ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                }
448ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                // Reduce the target area by the area we have just painted.
449b816087962aba0019b022303330f03b897b580edSkuhne                targetBounds.right = std::max(targetBounds.left, contentBounds.right);
450ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                mCanvas->restore();
451ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            }
452ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            // ... or at the top ...
453b816087962aba0019b022303330f03b897b580edSkuhne            if (targetBounds.top < contentBounds.top &&
454ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                !targetBounds.isEmpty()) {
455eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita                mCanvas->save(SaveFlags::Clip);
456ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                if (mCanvas->clipRect(targetBounds.left, targetBounds.top, targetBounds.right,
457b816087962aba0019b022303330f03b897b580edSkuhne                                      contentBounds.top,
458ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                                      SkRegion::kIntersect_Op)) {
459ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                    mCanvas->drawRenderNode(node.get(), outBounds);
460ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                }
461ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                // Reduce the target area by the area we have just painted.
462b816087962aba0019b022303330f03b897b580edSkuhne                targetBounds.top = std::min(contentBounds.top, targetBounds.bottom);
463ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                mCanvas->restore();
464ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            }
465ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            // ... or at the bottom.
466b816087962aba0019b022303330f03b897b580edSkuhne            if (targetBounds.bottom > contentBounds.bottom &&
467ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                !targetBounds.isEmpty()) {
468eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita                mCanvas->save(SaveFlags::Clip);
469b816087962aba0019b022303330f03b897b580edSkuhne                if (mCanvas->clipRect(targetBounds.left, contentBounds.bottom, targetBounds.right,
470ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                                      targetBounds.bottom, SkRegion::kIntersect_Op)) {
471ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                    mCanvas->drawRenderNode(node.get(), outBounds);
472ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                }
473ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                mCanvas->restore();
474ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            }
475ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne        } else if (layer == 1) { // Content
476ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            // It gets cropped against the bounds of the backdrop to stay inside.
477eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita            mCanvas->save(SaveFlags::MatrixClip);
478b816087962aba0019b022303330f03b897b580edSkuhne
479b816087962aba0019b022303330f03b897b580edSkuhne            // We shift and clip the content to match its final location in the window.
480b816087962aba0019b022303330f03b897b580edSkuhne            const float left = mContentDrawBounds.left;
481b816087962aba0019b022303330f03b897b580edSkuhne            const float top = mContentDrawBounds.top;
482b816087962aba0019b022303330f03b897b580edSkuhne            const float dx = backdropBounds.left - left;
483b816087962aba0019b022303330f03b897b580edSkuhne            const float dy = backdropBounds.top - top;
484b816087962aba0019b022303330f03b897b580edSkuhne            const float width = backdropBounds.getWidth();
485b816087962aba0019b022303330f03b897b580edSkuhne            const float height = backdropBounds.getHeight();
486b6c2624c8d9ee71d1c8d0aaf8082f92bb84b1418Robert Carr
487b816087962aba0019b022303330f03b897b580edSkuhne            mCanvas->translate(dx, dy);
488b816087962aba0019b022303330f03b897b580edSkuhne            if (mCanvas->clipRect(left, top, left + width, top + height, SkRegion::kIntersect_Op)) {
489ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne                mCanvas->drawRenderNode(node.get(), outBounds);
490ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            }
491ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            mCanvas->restore();
492ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne        } else { // draw the rest on top at will!
493ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne            mCanvas->drawRenderNode(node.get(), outBounds);
494ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne        }
495ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne        layer++;
496ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    }
4974f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
498fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck    profiler().draw(mCanvas);
4994f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck
500107843de4507b3511006cb9c77b8d0364374385aTom Hudson    bool drew = mCanvas->finish();
501ddf2215d9807b641dbcb304779ef6b530f876ac7Chris Craik#endif
5029372ac3621848085e77b867f220c0b5ffce4010dJohn Reck
50338f6c034d153bb648d45bce09d80a69ba3e03360John Reck    waitOnFences();
50438f6c034d153bb648d45bce09d80a69ba3e03360John Reck
505975591a7af883d866d86ab819e164c6004694744John Reck    GL_CHECKPOINT(LOW);
5069372ac3621848085e77b867f220c0b5ffce4010dJohn Reck
507ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    // Even if we decided to cancel the frame, from the perspective of jank
508ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    // metrics the frame was swapped at this point
509ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    mCurrentFrameInfo->markSwapBuffers();
510306f331f91a86da271ce30d4f14d6badf0d25704John Reck    mIsDirty = false;
511ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck
512c96955d9bb997b51be5fa929b5a67349d0459c3aJohn Reck    if (drew || mEglManager.damageRequiresSwap()) {
513149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck        if (CC_UNLIKELY(!mEglManager.swapBuffers(frame, screenDirty))) {
514149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck            setSurface(nullptr);
515149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck        }
516e486d932ca5a10446a3c98d6d065213913277268John Reck        SwapHistory& swap = mSwapHistory.next();
517e486d932ca5a10446a3c98d6d065213913277268John Reck        swap.damage = screenDirty;
518e486d932ca5a10446a3c98d6d065213913277268John Reck        swap.swapTime = systemTime(CLOCK_MONOTONIC);
519e486d932ca5a10446a3c98d6d065213913277268John Reck        swap.vsyncTime = mRenderThread.timeLord().latestVsync();
520149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck        mHaveNewSurface = false;
52128912a508493e583c48772e2a234e0ed66849490John Reck        mFrameNumber = -1;
52223b797ab5151eb2474f3bdd679f2f07bfd723042John Reck    }
523fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck
524ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    // TODO: Use a fence for real completion?
525ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    mCurrentFrameInfo->markFrameCompleted();
526149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck
527149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck#if LOG_FRAMETIME_MMA
528149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    float thisFrame = mCurrentFrameInfo->duration(
529149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck            FrameInfoIndex::IssueDrawCommandsStart,
530149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck            FrameInfoIndex::FrameCompleted) / NANOS_PER_MILLIS_F;
531149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    if (sFrameCount) {
532149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck        sBenchMma = ((9 * sBenchMma) + thisFrame) / 10;
533149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    } else {
534149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck        sBenchMma = thisFrame;
535149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    }
536149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    if (++sFrameCount == 10) {
537149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck        sFrameCount = 1;
538149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck        ALOGD("Average frame time: %.4f", sBenchMma);
539149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck    }
540149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck#endif
541149173d28c0843aba86b0810ce75b34be6a0d08fJohn Reck
542edc524c90506d80e0fc5fb67e8de7b8f3ef53439John Reck    mJankTracker.addFrame(*mCurrentFrameInfo);
543ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    mRenderThread.jankTracker().addFrame(*mCurrentFrameInfo);
544910beb8f5d9042163e2ad0dbb744d9f147db9604Andres Morales    if (CC_UNLIKELY(mFrameMetricsReporter.get() != nullptr)) {
545910beb8f5d9042163e2ad0dbb744d9f147db9604Andres Morales        mFrameMetricsReporter->reportFrameMetrics(mCurrentFrameInfo->data());
54606f5bc70a667a02b14e31d3f53f91d3661e30666Andres Morales    }
54738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck
54838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    GpuMemoryTracker::onFrameCompleted();
54923b797ab5151eb2474f3bdd679f2f07bfd723042John Reck}
55023b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
551e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck// Called by choreographer to do an RT-driven animation
55218f16e6fba74eda173e1e7c869e6e2e2acc073ffJohn Reckvoid CanvasContext::doFrame() {
553adfeec94560c6661c5e6600b10c05f34a40f6454Chris Craik#if HWUI_NEW_OPS
554adfeec94560c6661c5e6600b10c05f34a40f6454Chris Craik    if (CC_UNLIKELY(mEglSurface == EGL_NO_SURFACE)) return;
555adfeec94560c6661c5e6600b10c05f34a40f6454Chris Craik#else
556adfeec94560c6661c5e6600b10c05f34a40f6454Chris Craik    if (CC_UNLIKELY(!mCanvas || mEglSurface == EGL_NO_SURFACE)) return;
557adfeec94560c6661c5e6600b10c05f34a40f6454Chris Craik#endif
558ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    prepareAndDraw(nullptr);
559ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne}
560368cdd85268999997fb495cf90c4417221797de0John Reck
561ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhnevoid CanvasContext::prepareAndDraw(RenderNode* node) {
562e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck    ATRACE_CALL();
563e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck
5647f667e7a0823d52eed2ed64a31b125f6b8da21cbMatthew Bouyack    nsecs_t vsync = mRenderThread.timeLord().computeFrameTimeNanos();
565ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    int64_t frameInfo[UI_THREAD_FRAME_INFO_SIZE];
566ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    UiFrameInfoBuilder(frameInfo)
5671b54fb27ac48495ed0b33868fda5776fb49fe0f3Chris Craik        .addFlag(FrameInfoFlags::RTAnimation)
5687f667e7a0823d52eed2ed64a31b125f6b8da21cbMatthew Bouyack        .setVsync(vsync, vsync);
569fe5e7b7346a54537b980796ceeca66bfdbd05561John Reck
570e2e53a7079733694bd52dbce665e9ceff21e9727Chris Craik    TreeInfo info(TreeInfo::MODE_RT_ONLY, *this);
571ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne    prepareTree(info, frameInfo, systemTime(CLOCK_MONOTONIC), node);
572a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    if (info.out.canDrawThisFrame) {
573e4267ea4f20740c37c01bfb6aefcf61fddc4566aJohn Reck        draw();
574a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck    }
575e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck}
576e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck
5773b20251a355c88193c439f928a84ae69483fb488John Reckvoid CanvasContext::invokeFunctor(RenderThread& thread, Functor* functor) {
578d3d8dafc2f61fb118c060720b52684c59303f3dbJohn Reck    ATRACE_CALL();
5790d1f634f4b5e1bb37aa51777efb6a68619488d01John Reck    DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
5803b20251a355c88193c439f928a84ae69483fb488John Reck    if (thread.eglManager().hasEglContext()) {
5810d1f634f4b5e1bb37aa51777efb6a68619488d01John Reck        mode = DrawGlInfo::kModeProcess;
5820d1f634f4b5e1bb37aa51777efb6a68619488d01John Reck    }
5836f07a0dc875a9eac67312085a8e0133b9e2f4771John Reck
584d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    thread.renderState().invokeFunctor(functor, mode, nullptr);
58523b797ab5151eb2474f3bdd679f2f07bfd723042John Reck}
58623b797ab5151eb2474f3bdd679f2f07bfd723042John Reck
587998a6d81896df8b662cc10ddeb35087b78b38d72John Reckvoid CanvasContext::markLayerInUse(RenderNode* node) {
58851f2d606dcbfba3cc5b03dfea37c1304b91c232fJohn Reck    if (mPrefetchedLayers.erase(node)) {
589d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik        node->decStrong(nullptr);
590998a6d81896df8b662cc10ddeb35087b78b38d72John Reck    }
591998a6d81896df8b662cc10ddeb35087b78b38d72John Reck}
592998a6d81896df8b662cc10ddeb35087b78b38d72John Reck
59351f2d606dcbfba3cc5b03dfea37c1304b91c232fJohn Reckvoid CanvasContext::freePrefetchedLayers(TreeObserver* observer) {
59451f2d606dcbfba3cc5b03dfea37c1304b91c232fJohn Reck    if (mPrefetchedLayers.size()) {
59551f2d606dcbfba3cc5b03dfea37c1304b91c232fJohn Reck        for (auto& node : mPrefetchedLayers) {
59651f2d606dcbfba3cc5b03dfea37c1304b91c232fJohn Reck            ALOGW("Incorrectly called buildLayer on View: %s, destroying layer...",
59751f2d606dcbfba3cc5b03dfea37c1304b91c232fJohn Reck                    node->getName());
59851f2d606dcbfba3cc5b03dfea37c1304b91c232fJohn Reck            node->destroyHardwareResources(observer);
59951f2d606dcbfba3cc5b03dfea37c1304b91c232fJohn Reck            node->decStrong(observer);
60051f2d606dcbfba3cc5b03dfea37c1304b91c232fJohn Reck        }
60151f2d606dcbfba3cc5b03dfea37c1304b91c232fJohn Reck        mPrefetchedLayers.clear();
602998a6d81896df8b662cc10ddeb35087b78b38d72John Reck    }
603998a6d81896df8b662cc10ddeb35087b78b38d72John Reck}
604998a6d81896df8b662cc10ddeb35087b78b38d72John Reck
60551f2d606dcbfba3cc5b03dfea37c1304b91c232fJohn Reckvoid CanvasContext::buildLayer(RenderNode* node, TreeObserver* observer) {
6063e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    ATRACE_CALL();
6076246d27813f25b85f6e4b5cb1121fe8484bcce2dChris Craik    if (!mEglManager.hasEglContext()) return;
6086246d27813f25b85f6e4b5cb1121fe8484bcce2dChris Craik#if !HWUI_NEW_OPS
6096246d27813f25b85f6e4b5cb1121fe8484bcce2dChris Craik    if (!mCanvas) return;
6106246d27813f25b85f6e4b5cb1121fe8484bcce2dChris Craik#endif
6116246d27813f25b85f6e4b5cb1121fe8484bcce2dChris Craik
6123e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    // buildLayer() will leave the tree in an unknown state, so we must stop drawing
6133e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    stopDrawing();
6143e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck
615e2e53a7079733694bd52dbce665e9ceff21e9727Chris Craik    TreeInfo info(TreeInfo::MODE_FULL, *this);
6163e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    info.damageAccumulator = &mDamageAccumulator;
61751f2d606dcbfba3cc5b03dfea37c1304b91c232fJohn Reck    info.observer = observer;
6180b7e8245db728d127ada698be63d78b33fc6e4daChris Craik#if HWUI_NEW_OPS
6190b7e8245db728d127ada698be63d78b33fc6e4daChris Craik    info.layerUpdateQueue = &mLayerUpdateQueue;
6200b7e8245db728d127ada698be63d78b33fc6e4daChris Craik#else
6213e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    info.renderer = mCanvas;
6220b7e8245db728d127ada698be63d78b33fc6e4daChris Craik#endif
6239eb9f6f8cbbbd87d45da8071aa54cb066a797723John Reck    info.runAnimations = false;
6243e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    node->prepareTree(info);
6253e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    SkRect ignore;
6263e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    mDamageAccumulator.finish(&ignore);
6273e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    // Tickle the GENERIC property on node to mark it as dirty for damaging
6283e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    // purposes when the frame is actually drawn
6293e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    node->setPropertyFieldsDirty(RenderNode::GENERIC);
6303e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck
63198787e6c9b2c10b1ab7820bdac168686025b924aChris Craik#if HWUI_NEW_OPS
6326246d27813f25b85f6e4b5cb1121fe8484bcce2dChris Craik    static const std::vector< sp<RenderNode> > emptyNodeList;
6336246d27813f25b85f6e4b5cb1121fe8484bcce2dChris Craik    auto& caches = Caches::getInstance();
6349cd1bbe5c9e14472e631d8cc10005613925f34afChris Craik    FrameBuilder frameBuilder(mLayerUpdateQueue, mLightGeometry, caches);
6356246d27813f25b85f6e4b5cb1121fe8484bcce2dChris Craik    mLayerUpdateQueue.clear();
6366246d27813f25b85f6e4b5cb1121fe8484bcce2dChris Craik    BakedOpRenderer renderer(caches, mRenderThread.renderState(),
6376246d27813f25b85f6e4b5cb1121fe8484bcce2dChris Craik            mOpaque, mLightInfo);
6386246d27813f25b85f6e4b5cb1121fe8484bcce2dChris Craik    LOG_ALWAYS_FATAL_IF(renderer.didDraw(), "shouldn't draw in buildlayer case");
6396246d27813f25b85f6e4b5cb1121fe8484bcce2dChris Craik    frameBuilder.replayBakedOps<BakedOpDispatcher>(renderer);
64098787e6c9b2c10b1ab7820bdac168686025b924aChris Craik#else
641443a714fa7c0dd07fee3527cc5bc3d3ca1fb7d44John Reck    mCanvas->markLayersAsBuildLayers();
6423e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck    mCanvas->flushLayerUpdates();
64398787e6c9b2c10b1ab7820bdac168686025b924aChris Craik#endif
644998a6d81896df8b662cc10ddeb35087b78b38d72John Reck
645d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik    node->incStrong(nullptr);
64651f2d606dcbfba3cc5b03dfea37c1304b91c232fJohn Reck    mPrefetchedLayers.insert(node);
6473e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck}
6483e8249568cc428296ac76c7ddce3f0382d40fe5bJohn Reck
64919b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reckbool CanvasContext::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
65068bfe0a37a0dcef52abd81688d8520c5d16e1a85John Reck    layer->apply();
6513b20251a355c88193c439f928a84ae69483fb488John Reck    return LayerRenderer::copyLayer(mRenderThread.renderState(), layer->backingLayer(), bitmap);
65219b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck}
65319b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck
65451f2d606dcbfba3cc5b03dfea37c1304b91c232fJohn Reckvoid CanvasContext::destroyHardwareResources(TreeObserver* observer) {
655f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck    stopDrawing();
6563b20251a355c88193c439f928a84ae69483fb488John Reck    if (mEglManager.hasEglContext()) {
65751f2d606dcbfba3cc5b03dfea37c1304b91c232fJohn Reck        freePrefetchedLayers(observer);
658ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne        for (const sp<RenderNode>& node : mRenderNodes) {
65951f2d606dcbfba3cc5b03dfea37c1304b91c232fJohn Reck            node->destroyHardwareResources(observer);
660ea7a7fb75acb7305eb774ca7bc7e96103bd49323Skuhne        }
66100e79c9947b741194ff6c0d08ede9b3befbf9c9dJohn Reck        Caches& caches = Caches::getInstance();
66200e79c9947b741194ff6c0d08ede9b3befbf9c9dJohn Reck        // Make sure to release all the textures we were owning as there won't
66300e79c9947b741194ff6c0d08ede9b3befbf9c9dJohn Reck        // be another draw
66400e79c9947b741194ff6c0d08ede9b3befbf9c9dJohn Reck        caches.textureCache.resetMarkInUse(this);
6659fded232a9548a304e0145011df8849fba0dcda7Chris Craik        mRenderThread.renderState().flush(Caches::FlushMode::Layers);
666f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck    }
667f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck}
668f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck
669f47a594f5250b1914c36423ee6b371f0b8db09d0John Reckvoid CanvasContext::trimMemory(RenderThread& thread, int level) {
670f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck    // No context means nothing to free
671f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck    if (!thread.eglManager().hasEglContext()) return;
672f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck
673786afcb3eec18315ec54987a08814ff28f13d09fJorim Jaggi    ATRACE_CALL();
674f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck    if (level >= TRIM_MEMORY_COMPLETE) {
6759fded232a9548a304e0145011df8849fba0dcda7Chris Craik        thread.renderState().flush(Caches::FlushMode::Full);
676f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck        thread.eglManager().destroy();
677f47a594f5250b1914c36423ee6b371f0b8db09d0John Reck    } else if (level >= TRIM_MEMORY_UI_HIDDEN) {
6789fded232a9548a304e0145011df8849fba0dcda7Chris Craik        thread.renderState().flush(Caches::FlushMode::Moderate);
679e1628b7c6fc3822fa83cf02028ce8ad67abb0afeJohn Reck    }
680e1628b7c6fc3822fa83cf02028ce8ad67abb0afeJohn Reck}
681e1628b7c6fc3822fa83cf02028ce8ad67abb0afeJohn Reck
682fc53ef27793a39e9effd829e9cae02a9ca14147eJohn Reckvoid CanvasContext::runWithGlContext(RenderTask* task) {
683d7db4d767246b41d44995acb93d03d220b53c748John Reck    LOG_ALWAYS_FATAL_IF(!mEglManager.hasEglContext(),
684d7db4d767246b41d44995acb93d03d220b53c748John Reck            "GL context not initialized!");
68519b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck    task->run();
68619b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck}
68719b6bcfd83eb7fb92ebd06d2fec89e308311f1d0John Reck
6881949e7928eeec22cd3f74b5f763a4eb433238453John ReckLayer* CanvasContext::createTextureLayer() {
6898afcc76920499d0a384dba1470c5a377f80ed768John Reck    mEglManager.initialize();
6903b20251a355c88193c439f928a84ae69483fb488John Reck    return LayerRenderer::createTextureLayer(mRenderThread.renderState());
6911949e7928eeec22cd3f74b5f763a4eb433238453John Reck}
6921949e7928eeec22cd3f74b5f763a4eb433238453John Reck
6933b20251a355c88193c439f928a84ae69483fb488John Reckvoid CanvasContext::setTextureAtlas(RenderThread& thread,
6943b20251a355c88193c439f928a84ae69483fb488John Reck        const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize) {
6953b20251a355c88193c439f928a84ae69483fb488John Reck    thread.eglManager().setTextureAtlas(buffer, map, mapSize);
69666f0be65a1046f54ddce0498b242c1fa0776b1eaJohn Reck}
69766f0be65a1046f54ddce0498b242c1fa0776b1eaJohn Reck
698ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reckvoid CanvasContext::dumpFrames(int fd) {
699ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    FILE* file = fdopen(fd, "a");
7004db3d17debef68f72d23999d69ae68b75f59dda3John Reck    fprintf(file, "\n\n---PROFILEDATA---\n");
7011b54fb27ac48495ed0b33868fda5776fb49fe0f3Chris Craik    for (size_t i = 0; i < static_cast<size_t>(FrameInfoIndex::NumIndexes); i++) {
7022a8bb05a31ddd0d44d8513cba9fbd9b4ef9b97f6John Reck        fprintf(file, "%s", FrameInfoNames[i].c_str());
7034db3d17debef68f72d23999d69ae68b75f59dda3John Reck        fprintf(file, ",");
7044db3d17debef68f72d23999d69ae68b75f59dda3John Reck    }
705ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    for (size_t i = 0; i < mFrames.size(); i++) {
706ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck        FrameInfo& frame = mFrames[i];
7071b54fb27ac48495ed0b33868fda5776fb49fe0f3Chris Craik        if (frame[FrameInfoIndex::SyncStart] == 0) {
708ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck            continue;
709ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck        }
710ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck        fprintf(file, "\n");
7111b54fb27ac48495ed0b33868fda5776fb49fe0f3Chris Craik        for (int i = 0; i < static_cast<int>(FrameInfoIndex::NumIndexes); i++) {
712ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck            fprintf(file, "%" PRId64 ",", frame[i]);
713ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck        }
714ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    }
715ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    fprintf(file, "\n---PROFILEDATA---\n\n");
716ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    fflush(file);
717ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck}
718ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck
719ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reckvoid CanvasContext::resetFrameStats() {
720ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    mFrames.clear();
721ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck    mRenderThread.jankTracker().reset();
722ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck}
723ba6adf66d3c44c0aa2fd8a224862ff1901d64300John Reck
724e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reckvoid CanvasContext::serializeDisplayListTree() {
725e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck#if ENABLE_RENDERNODE_SERIALIZATION
726e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    using namespace google::protobuf::io;
727e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    char package[128];
728e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    // Check whether tracing is enabled for this process.
729e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    FILE * file = fopen("/proc/self/cmdline", "r");
730e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    if (file) {
731e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck        if (!fgets(package, 128, file)) {
732e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck            ALOGE("Error reading cmdline: %s (%d)", strerror(errno), errno);
733e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck            fclose(file);
734e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck            return;
735e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck        }
736e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck        fclose(file);
737e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    } else {
738e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck        ALOGE("Error opening /proc/self/cmdline: %s (%d)", strerror(errno),
739e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck                errno);
740e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck        return;
741e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    }
742e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    char path[1024];
743e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    snprintf(path, 1024, "/data/data/%s/cache/rendertree_dump", package);
744e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    int fd = open(path, O_CREAT | O_WRONLY, S_IRWXU | S_IRGRP | S_IROTH);
745e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    if (fd == -1) {
746e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck        ALOGD("Failed to open '%s'", path);
747e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck        return;
748e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    }
749e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    proto::RenderNode tree;
750e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    // TODO: Streaming writes?
751e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    mRootRenderNode->copyTo(&tree);
752e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    std::string data = tree.SerializeAsString();
753e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    write(fd, data.c_str(), data.length());
754e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck    close(fd);
755e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck#endif
756e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck}
757e248bd1b2c3fcf8088429507e73b31f45ee2544bJohn Reck
75838f6c034d153bb648d45bce09d80a69ba3e03360John Reckvoid CanvasContext::waitOnFences() {
75938f6c034d153bb648d45bce09d80a69ba3e03360John Reck    if (mFrameFences.size()) {
76038f6c034d153bb648d45bce09d80a69ba3e03360John Reck        ATRACE_CALL();
76138f6c034d153bb648d45bce09d80a69ba3e03360John Reck        for (auto& fence : mFrameFences) {
76238f6c034d153bb648d45bce09d80a69ba3e03360John Reck            fence->getResult();
76338f6c034d153bb648d45bce09d80a69ba3e03360John Reck        }
76438f6c034d153bb648d45bce09d80a69ba3e03360John Reck        mFrameFences.clear();
76538f6c034d153bb648d45bce09d80a69ba3e03360John Reck    }
76638f6c034d153bb648d45bce09d80a69ba3e03360John Reck}
76738f6c034d153bb648d45bce09d80a69ba3e03360John Reck
76838f6c034d153bb648d45bce09d80a69ba3e03360John Reckclass CanvasContext::FuncTaskProcessor : public TaskProcessor<bool> {
76938f6c034d153bb648d45bce09d80a69ba3e03360John Reckpublic:
77038f6c034d153bb648d45bce09d80a69ba3e03360John Reck    FuncTaskProcessor(Caches& caches)
77138f6c034d153bb648d45bce09d80a69ba3e03360John Reck            : TaskProcessor<bool>(&caches.tasks) {}
77238f6c034d153bb648d45bce09d80a69ba3e03360John Reck
77338f6c034d153bb648d45bce09d80a69ba3e03360John Reck    virtual void onProcess(const sp<Task<bool> >& task) override {
77438f6c034d153bb648d45bce09d80a69ba3e03360John Reck        FuncTask* t = static_cast<FuncTask*>(task.get());
77538f6c034d153bb648d45bce09d80a69ba3e03360John Reck        t->func();
77638f6c034d153bb648d45bce09d80a69ba3e03360John Reck        task->setResult(true);
77738f6c034d153bb648d45bce09d80a69ba3e03360John Reck    }
77838f6c034d153bb648d45bce09d80a69ba3e03360John Reck};
77938f6c034d153bb648d45bce09d80a69ba3e03360John Reck
78038f6c034d153bb648d45bce09d80a69ba3e03360John Reckvoid CanvasContext::enqueueFrameWork(std::function<void()>&& func) {
78138f6c034d153bb648d45bce09d80a69ba3e03360John Reck    if (!mFrameWorkProcessor.get()) {
78238f6c034d153bb648d45bce09d80a69ba3e03360John Reck        mFrameWorkProcessor = new FuncTaskProcessor(Caches::getInstance());
78338f6c034d153bb648d45bce09d80a69ba3e03360John Reck    }
78438f6c034d153bb648d45bce09d80a69ba3e03360John Reck    sp<FuncTask> task(new FuncTask());
78538f6c034d153bb648d45bce09d80a69ba3e03360John Reck    task->func = func;
78638f6c034d153bb648d45bce09d80a69ba3e03360John Reck    mFrameWorkProcessor->add(task);
78738f6c034d153bb648d45bce09d80a69ba3e03360John Reck}
78838f6c034d153bb648d45bce09d80a69ba3e03360John Reck
78928912a508493e583c48772e2a234e0ed66849490John Reckint64_t CanvasContext::getFrameNumber() {
79028912a508493e583c48772e2a234e0ed66849490John Reck    // mFrameNumber is reset to -1 when the surface changes or we swap buffers
79128912a508493e583c48772e2a234e0ed66849490John Reck    if (mFrameNumber == -1 && mNativeSurface.get()) {
79228912a508493e583c48772e2a234e0ed66849490John Reck        mFrameNumber = static_cast<int64_t>(mNativeSurface->getNextFrameNumber());
79328912a508493e583c48772e2a234e0ed66849490John Reck    }
79428912a508493e583c48772e2a234e0ed66849490John Reck    return mFrameNumber;
79528912a508493e583c48772e2a234e0ed66849490John Reck}
79628912a508493e583c48772e2a234e0ed66849490John Reck
79723b797ab5151eb2474f3bdd679f2f07bfd723042John Reck} /* namespace renderthread */
79823b797ab5151eb2474f3bdd679f2f07bfd723042John Reck} /* namespace uirenderer */
79923b797ab5151eb2474f3bdd679f2f07bfd723042John Reck} /* namespace android */
800