Layer.cpp revision 34beb7a0ff0494b0c5ad81104171f8a49e599163
1edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project/*
2edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Copyright (C) 2007 The Android Open Source Project
3edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *
4edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
5edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * you may not use this file except in compliance with the License.
6edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * You may obtain a copy of the License at
7edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *
8edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
9edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *
10edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
11edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
12edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * See the License for the specific language governing permissions and
14edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * limitations under the License.
15edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project */
16edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
179e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza//#define LOG_NDEBUG 0
189e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza#undef LOG_TAG
199e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza#define LOG_TAG "Layer"
201c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis#define ATRACE_TAG ATRACE_TAG_GRAPHICS
211c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis
2241fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman#include <math.h>
23edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <stdint.h>
2441fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman#include <stdlib.h>
25edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <sys/types.h>
26edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
27a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian#include <cutils/compiler.h>
28076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian#include <cutils/native_handle.h>
29a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian#include <cutils/properties.h>
30edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
31edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <utils/Errors.h>
32edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <utils/Log.h>
33399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall#include <utils/NativeHandle.h>
34edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <utils/StopWatch.h>
351c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis#include <utils/Trace.h>
36edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
3736c44dcc692ac1f7230d01d2ed3ffa93104ce478Courtney Goeltzenleuchter#include <ui/DebugUtils.h>
383330b203039dea366d4981db1408a460134b2d2cMathias Agopian#include <ui/GraphicBuffer.h>
39edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <ui/PixelFormat.h>
409cce325fae8adcf7560a28eef394489f09bad74dMathias Agopian
416b9454d1fee0347711af1746642aa7820b1ea04dDan Stoza#include <gui/BufferItem.h>
42a099a24c93bfa599fc5c36a647e946c26f68514fKalle Raita#include <gui/LayerDebugInfo.h>
4390ac799241f077a7b7e6c1875fd933864c8dd2a7Mathias Agopian#include <gui/Surface.h>
44edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
457c64f17bd0d70aec63b475c3de850dcf3abaff33Yiwei Zhang#include "BufferLayer.h"
463e25fd8609b100a75721be82d1d499f0ae9083cbMathias Agopian#include "Colorizer.h"
470f2f5ff75b7b48ceb64270655ee6b62d09bf4d00Mathias Agopian#include "DisplayDevice.h"
48edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include "Layer.h"
497b1563a0e8e742315a16c9a7b27537dbcd13d09aFabien Sanglard#include "LayerRejecter.h"
50b9b088375d33a87b201cdbe18be71802e2607717Dan Stoza#include "MonitoredProducer.h"
51edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include "SurfaceFlinger.h"
5241fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman#include "clz.h"
53edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
541b03149f3533db04e72e088d3fdd09d0087ca594Mathias Agopian#include "DisplayHardware/HWComposer.h"
551b03149f3533db04e72e088d3fdd09d0087ca594Mathias Agopian
56875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian#include "RenderEngine/RenderEngine.h"
57875d8e1323536e16dcfc90c9674d7ad32116a69aMathias Agopian
58c5da271eec001da9e11a2786f2618a45257439c3Dan Stoza#include <mutex>
591d04428c3cff3503212ec3e76775ca5ba20abc18chaviw#include "LayerProtoHelper.h"
60c5da271eec001da9e11a2786f2618a45257439c3Dan Stoza
6141fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman#define DEBUG_RESIZE 0
62edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
63edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectnamespace android {
64edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
659eeae69005ef9a774bde19195b9c06eba7e463c2David SodmanLayerBE::LayerBE()
660cc69188196037c06127c0bc56c516069dbaa558David Sodman      : mMesh(Mesh::TRIANGLE_FAN, 4, 2, 2) {
679eeae69005ef9a774bde19195b9c06eba7e463c2David Sodman}
689eeae69005ef9a774bde19195b9c06eba7e463c2David Sodman
699eeae69005ef9a774bde19195b9c06eba7e463c2David Sodman
7013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianint32_t Layer::sSequence = 1;
7113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
7241fdfc920e2a479add53a1936c3ae76cdbea41dbDavid SodmanLayer::Layer(SurfaceFlinger* flinger, const sp<Client>& client, const String8& name, uint32_t w,
7341fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman             uint32_t h, uint32_t flags)
740c69cad13dda09f1df1dbf23810b1c5b7f28ba08David Sodman      : contentDirty(false),
7513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        sequence(uint32_t(android_atomic_inc(&sSequence))),
7613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        mFlinger(flinger),
7713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        mPremultipliedAlpha(true),
780c69cad13dda09f1df1dbf23810b1c5b7f28ba08David Sodman        mName(name),
7913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        mTransactionFlags(0),
807dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza        mPendingStateMutex(),
817dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza        mPendingStates(),
82a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian        mQueuedFrames(0),
83399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall        mSidebandStreamChanged(false),
84a9347647eca3101c014be902b713772de3977d87Mathias Agopian        mActiveBufferSlot(BufferQueue::INVALID_BUFFER_SLOT),
85a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian        mCurrentTransform(0),
86c3574f7b0141c69fdca25ccafb80ff334462f9a3Robert Carr        mOverrideScalingMode(-1),
87a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian        mCurrentOpacity(true),
88cac353808ec1048333d7fd2f3d596fb4db567aa3Dan Stoza        mCurrentFrameNumber(0),
8982d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian        mFrameLatencyNeeded(false),
9013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        mFiltering(false),
9113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        mNeedsFiltering(false),
9213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        mProtectedByApp(false),
9303414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews        mClientRef(client),
94a4650a50a0b35e9e4342d6600b6eb24fd94bb8e5Dan Stoza        mPotentialCursor(false),
95a4650a50a0b35e9e4342d6600b6eb24fd94bb8e5Dan Stoza        mQueueItemLock(),
96a4650a50a0b35e9e4342d6600b6eb24fd94bb8e5Dan Stoza        mQueueItemCondition(),
97a4650a50a0b35e9e4342d6600b6eb24fd94bb8e5Dan Stoza        mQueueItems(),
9865476f3332641066a99e22338bf5cf49ce4af642Dan Stoza        mLastFrameNumberReceived(0),
9982364e3cea0bf88fa8147766433329b3dd5148b8Robert Carr        mAutoRefresh(false),
10041fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman        mFreezeGeometryUpdates(false) {
1019e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza
102a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian    mCurrentCrop.makeInvalid();
1034d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian
1044d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    uint32_t layerFlags = 0;
10541fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    if (flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
10641fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    if (flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
10741fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    if (flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
1084d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian
1094d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    mName = name;
110f7ba41abeb819e915997b4612a7af8c685d1fe3cDan Stoza    mTransactionName = String8("TX - ") + mName;
1114d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian
1124d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    mCurrentState.active.w = w;
1134d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    mCurrentState.active.h = h;
1140c69cad13dda09f1df1dbf23810b1c5b7f28ba08David Sodman    mCurrentState.flags = layerFlags;
1153dcabfab7ef80df5884b269fec17350a26da6f51Robert Carr    mCurrentState.active.transform.set(0, 0);
116b5d3d2657bad1f012377dfacd354d3100a65768aRobert Carr    mCurrentState.crop.makeInvalid();
117b5d3d2657bad1f012377dfacd354d3100a65768aRobert Carr    mCurrentState.finalCrop.makeInvalid();
1187bf247e2db180da661b18084d50e0b389686fc0cRobert Carr    mCurrentState.requestedFinalCrop = mCurrentState.finalCrop;
1197bf247e2db180da661b18084d50e0b389686fc0cRobert Carr    mCurrentState.requestedCrop = mCurrentState.crop;
1204d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    mCurrentState.z = 0;
12113fdc49516d17f41e64e62e73c313b0928bf13ccchaviw    mCurrentState.color.a = 1.0f;
1224d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    mCurrentState.layerStack = 0;
1234d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    mCurrentState.sequence = 0;
1244d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    mCurrentState.requested = mCurrentState.active;
12534beb7a0ff0494b0c5ad81104171f8a49e599163Peiyong Lin    mCurrentState.dataSpace = ui::Dataspace::UNKNOWN;
1262f5f8a51f5994cf14837030d4b3b252a9d1b950bDaniel Nicoara    mCurrentState.appId = 0;
1272f5f8a51f5994cf14837030d4b3b252a9d1b950bDaniel Nicoara    mCurrentState.type = 0;
1284d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian
1294d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    // drawing state & current state are identical
1304d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    mDrawingState = mCurrentState;
1316547ff4327aa320fbc9635668d3fc66db7dd78f6Jamie Gennis
1329e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza    const auto& hwc = flinger->getHwComposer();
1339e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza    const auto& activeConfig = hwc.getActiveConfig(HWC_DISPLAY_PRIMARY);
1349e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza    nsecs_t displayPeriod = activeConfig->getVsyncPeriod();
1356547ff4327aa320fbc9635668d3fc66db7dd78f6Jamie Gennis    mFrameTracker.setDisplayRefreshPeriod(displayPeriod);
1360a61b0c813f5991bf462e36a2314dda062727a10Brian Anderson
1370a61b0c813f5991bf462e36a2314dda062727a10Brian Anderson    CompositorTiming compositorTiming;
1380a61b0c813f5991bf462e36a2314dda062727a10Brian Anderson    flinger->getCompositorTiming(&compositorTiming);
1390a61b0c813f5991bf462e36a2314dda062727a10Brian Anderson    mFrameEventHistory.initializeCompositorTiming(compositorTiming);
1409eeae69005ef9a774bde19195b9c06eba7e463c2David Sodman
141e8696a40e09b24b634214684d18526187b316a2fJamie Gennis}
142e8696a40e09b24b634214684d18526187b316a2fJamie Gennis
14341fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodmanvoid Layer::onFirstRef() {}
14496f0819f81293076e652792794a961543e6750d7Mathias Agopian
1454d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias AgopianLayer::~Layer() {
146577c896102ebb13f9496a94674b4ca9cd15fe0adDavid Sodman    sp<Client> c(mClientRef.promote());
147577c896102ebb13f9496a94674b4ca9cd15fe0adDavid Sodman    if (c != 0) {
148577c896102ebb13f9496a94674b4ca9cd15fe0adDavid Sodman        c->detachLayer(this);
149577c896102ebb13f9496a94674b4ca9cd15fe0adDavid Sodman    }
150577c896102ebb13f9496a94674b4ca9cd15fe0adDavid Sodman
151577c896102ebb13f9496a94674b4ca9cd15fe0adDavid Sodman    for (auto& point : mRemoteSyncPoints) {
152577c896102ebb13f9496a94674b4ca9cd15fe0adDavid Sodman        point->setTransactionApplied();
153577c896102ebb13f9496a94674b4ca9cd15fe0adDavid Sodman    }
154577c896102ebb13f9496a94674b4ca9cd15fe0adDavid Sodman    for (auto& point : mLocalSyncPoints) {
155577c896102ebb13f9496a94674b4ca9cd15fe0adDavid Sodman        point->setFrameAvailable();
156577c896102ebb13f9496a94674b4ca9cd15fe0adDavid Sodman    }
1576547ff4327aa320fbc9635668d3fc66db7dd78f6Jamie Gennis    mFrameTracker.logAndResetStats(mName);
15896f0819f81293076e652792794a961543e6750d7Mathias Agopian}
15996f0819f81293076e652792794a961543e6750d7Mathias Agopian
16013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// ---------------------------------------------------------------------------
16113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// callbacks
16213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// ---------------------------------------------------------------------------
16313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
164eb085e0c579f310e2670204b14b742b68c70bc9eDavid Sodman/*
165eb085e0c579f310e2670204b14b742b68c70bc9eDavid Sodman * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
166eb085e0c579f310e2670204b14b742b68c70bc9eDavid Sodman * Layer.  So, the implementation is done in BufferLayer.  When called on a
167eb085e0c579f310e2670204b14b742b68c70bc9eDavid Sodman * ColorLayer object, it's essentially a NOP.
168eb085e0c579f310e2670204b14b742b68c70bc9eDavid Sodman */
169eb085e0c579f310e2670204b14b742b68c70bc9eDavid Sodmanvoid Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
17013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
171c665702cea06c5c42360b7f66fed1693127e6680Chia-I Wuvoid Layer::onRemovedFromCurrentState() {
172c665702cea06c5c42360b7f66fed1693127e6680Chia-I Wu    // the layer is removed from SF mCurrentState to mLayersPendingRemoval
173c665702cea06c5c42360b7f66fed1693127e6680Chia-I Wu
1748b3871addb9bbd5776f4ed59e67af2baa9c583fdchaviw    mPendingRemoval = true;
1758b3871addb9bbd5776f4ed59e67af2baa9c583fdchaviw
1765edb1add8b35c0d63645cb53fb175d459a689da5Robert Carr    if (mCurrentState.zOrderRelativeOf != nullptr) {
1775edb1add8b35c0d63645cb53fb175d459a689da5Robert Carr        sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1785edb1add8b35c0d63645cb53fb175d459a689da5Robert Carr        if (strongRelative != nullptr) {
1795edb1add8b35c0d63645cb53fb175d459a689da5Robert Carr            strongRelative->removeZOrderRelative(this);
180c665702cea06c5c42360b7f66fed1693127e6680Chia-I Wu            mFlinger->setTransactionFlags(eTraversalNeeded);
1815edb1add8b35c0d63645cb53fb175d459a689da5Robert Carr        }
1825edb1add8b35c0d63645cb53fb175d459a689da5Robert Carr        mCurrentState.zOrderRelativeOf = nullptr;
1835edb1add8b35c0d63645cb53fb175d459a689da5Robert Carr    }
1845edb1add8b35c0d63645cb53fb175d459a689da5Robert Carr
185c665702cea06c5c42360b7f66fed1693127e6680Chia-I Wu    for (const auto& child : mCurrentChildren) {
186c665702cea06c5c42360b7f66fed1693127e6680Chia-I Wu        child->onRemovedFromCurrentState();
187c665702cea06c5c42360b7f66fed1693127e6680Chia-I Wu    }
188c665702cea06c5c42360b7f66fed1693127e6680Chia-I Wu}
1893851225fddb6f1755a8034aa7a9f9ae7397b56a9Chia-I Wu
190c665702cea06c5c42360b7f66fed1693127e6680Chia-I Wuvoid Layer::onRemoved() {
191c665702cea06c5c42360b7f66fed1693127e6680Chia-I Wu    // the layer is removed from SF mLayersPendingRemoval
192eb085e0c579f310e2670204b14b742b68c70bc9eDavid Sodman    abandon();
193c665702cea06c5c42360b7f66fed1693127e6680Chia-I Wu
194b02664ddc146893e6bbe7939ee2b948d54e7166aSteven Thomas    destroyAllHwcLayers();
1953851225fddb6f1755a8034aa7a9f9ae7397b56a9Chia-I Wu
1961f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    for (const auto& child : mCurrentChildren) {
1971f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr        child->onRemoved();
1981f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    }
19948d819a1315f7d1b5abfec9d4fd34fb5aed27b1dMathias Agopian}
200cbb288bfe89f585bf48371bd31b2d4aafa32f32eMathias Agopian
20113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// ---------------------------------------------------------------------------
20213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// set-up
20313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// ---------------------------------------------------------------------------
20413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
2051eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopianconst String8& Layer::getName() const {
20613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    return mName;
20713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
20813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
20913fdc49516d17f41e64e62e73c313b0928bf13ccchaviwbool Layer::getPremultipledAlpha() const {
21013fdc49516d17f41e64e62e73c313b0928bf13ccchaviw    return mPremultipliedAlpha;
21113fdc49516d17f41e64e62e73c313b0928bf13ccchaviw}
21213fdc49516d17f41e64e62e73c313b0928bf13ccchaviw
2134d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopiansp<IBinder> Layer::getHandle() {
2144d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    Mutex::Autolock _l(mLock);
2154d9b822e2c18142e55fe2611aa6cd7dc7d4a62c6Mathias Agopian    return new Handle(mFlinger, this);
216582270d69db94286a248bd829f1ae6f910d45124Jamie Gennis}
217582270d69db94286a248bd829f1ae6f910d45124Jamie Gennis
21813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// ---------------------------------------------------------------------------
21913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// h/w composer set-up
22013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// ---------------------------------------------------------------------------
22113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
222b02664ddc146893e6bbe7939ee2b948d54e7166aSteven Thomasbool Layer::createHwcLayer(HWComposer* hwc, int32_t hwcId) {
2236f65f3e7caab86a76419e7c1d1c787634a0852d8David Sodman    LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(hwcId) != 0,
2249eeae69005ef9a774bde19195b9c06eba7e463c2David Sodman                        "Already have a layer for hwcId %d", hwcId);
225b02664ddc146893e6bbe7939ee2b948d54e7166aSteven Thomas    HWC2::Layer* layer = hwc->createLayer(hwcId);
226b02664ddc146893e6bbe7939ee2b948d54e7166aSteven Thomas    if (!layer) {
227b02664ddc146893e6bbe7939ee2b948d54e7166aSteven Thomas        return false;
228b02664ddc146893e6bbe7939ee2b948d54e7166aSteven Thomas    }
2296f65f3e7caab86a76419e7c1d1c787634a0852d8David Sodman    LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers[hwcId];
230b02664ddc146893e6bbe7939ee2b948d54e7166aSteven Thomas    hwcInfo.hwc = hwc;
231b02664ddc146893e6bbe7939ee2b948d54e7166aSteven Thomas    hwcInfo.layer = layer;
232b02664ddc146893e6bbe7939ee2b948d54e7166aSteven Thomas    layer->setLayerDestroyedListener(
2336f65f3e7caab86a76419e7c1d1c787634a0852d8David Sodman            [this, hwcId](HWC2::Layer* /*layer*/) { getBE().mHwcLayers.erase(hwcId); });
234b02664ddc146893e6bbe7939ee2b948d54e7166aSteven Thomas    return true;
235b02664ddc146893e6bbe7939ee2b948d54e7166aSteven Thomas}
236b02664ddc146893e6bbe7939ee2b948d54e7166aSteven Thomas
23783806897c6366d841971a38f4a007bb83a7afb64Chia-I Wubool Layer::destroyHwcLayer(int32_t hwcId) {
2386f65f3e7caab86a76419e7c1d1c787634a0852d8David Sodman    if (getBE().mHwcLayers.count(hwcId) == 0) {
23983806897c6366d841971a38f4a007bb83a7afb64Chia-I Wu        return false;
240b02664ddc146893e6bbe7939ee2b948d54e7166aSteven Thomas    }
2416f65f3e7caab86a76419e7c1d1c787634a0852d8David Sodman    auto& hwcInfo = getBE().mHwcLayers[hwcId];
24241fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr, "Attempt to destroy null layer");
243b02664ddc146893e6bbe7939ee2b948d54e7166aSteven Thomas    LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
244b02664ddc146893e6bbe7939ee2b948d54e7166aSteven Thomas    hwcInfo.hwc->destroyLayer(hwcId, hwcInfo.layer);
245b02664ddc146893e6bbe7939ee2b948d54e7166aSteven Thomas    // The layer destroyed listener should have cleared the entry from
246b02664ddc146893e6bbe7939ee2b948d54e7166aSteven Thomas    // mHwcLayers. Verify that.
2476f65f3e7caab86a76419e7c1d1c787634a0852d8David Sodman    LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(hwcId) != 0,
2486f65f3e7caab86a76419e7c1d1c787634a0852d8David Sodman                        "Stale layer entry in getBE().mHwcLayers");
24983806897c6366d841971a38f4a007bb83a7afb64Chia-I Wu    return true;
250b02664ddc146893e6bbe7939ee2b948d54e7166aSteven Thomas}
251b02664ddc146893e6bbe7939ee2b948d54e7166aSteven Thomas
252b02664ddc146893e6bbe7939ee2b948d54e7166aSteven Thomasvoid Layer::destroyAllHwcLayers() {
2536f65f3e7caab86a76419e7c1d1c787634a0852d8David Sodman    size_t numLayers = getBE().mHwcLayers.size();
254b02664ddc146893e6bbe7939ee2b948d54e7166aSteven Thomas    for (size_t i = 0; i < numLayers; ++i) {
2556f65f3e7caab86a76419e7c1d1c787634a0852d8David Sodman        LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.empty(), "destroyAllHwcLayers failed");
2566f65f3e7caab86a76419e7c1d1c787634a0852d8David Sodman        destroyHwcLayer(getBE().mHwcLayers.begin()->first);
257b02664ddc146893e6bbe7939ee2b948d54e7166aSteven Thomas    }
2586f65f3e7caab86a76419e7c1d1c787634a0852d8David Sodman    LOG_ALWAYS_FATAL_IF(!getBE().mHwcLayers.empty(),
25941fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman                        "All hardware composer layers should have been destroyed");
260b02664ddc146893e6bbe7939ee2b948d54e7166aSteven Thomas}
261b02664ddc146893e6bbe7939ee2b948d54e7166aSteven Thomas
262a8bca8d84b559e7dcca010f7d6514333004020c7Mathias AgopianRect Layer::getContentCrop() const {
263a8bca8d84b559e7dcca010f7d6514333004020c7Mathias Agopian    // this is the crop rectangle that applies to the buffer
264a8bca8d84b559e7dcca010f7d6514333004020c7Mathias Agopian    // itself (as opposed to the window)
265f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis    Rect crop;
266f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis    if (!mCurrentCrop.isEmpty()) {
267a8bca8d84b559e7dcca010f7d6514333004020c7Mathias Agopian        // if the buffer crop is defined, we use that
268f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis        crop = mCurrentCrop;
269566a3b4a1d1a2a6d38257113700eea92aa44ea2bPeiyong Lin    } else if (getBE().compositionInfo.mBuffer != nullptr) {
270a8bca8d84b559e7dcca010f7d6514333004020c7Mathias Agopian        // otherwise we use the whole buffer
2710cc69188196037c06127c0bc56c516069dbaa558David Sodman        crop = getBE().compositionInfo.mBuffer->getBounds();
272f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis    } else {
273a8bca8d84b559e7dcca010f7d6514333004020c7Mathias Agopian        // if we don't have a buffer yet, we use an empty/invalid crop
2744fec873a98f7b4380720cd1ad006f74c8cdc73daMathias Agopian        crop.makeInvalid();
275f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis    }
276f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis    return crop;
277f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis}
278f15a83f5814219c167f87cb8aaea622fc8493499Jamie Gennis
279f3e85d432749ca77ad707bec523b67d741d43e6eMathias Agopianstatic Rect reduce(const Rect& win, const Region& exclude) {
280f3e85d432749ca77ad707bec523b67d741d43e6eMathias Agopian    if (CC_LIKELY(exclude.isEmpty())) {
281f3e85d432749ca77ad707bec523b67d741d43e6eMathias Agopian        return win;
282f3e85d432749ca77ad707bec523b67d741d43e6eMathias Agopian    }
283f3e85d432749ca77ad707bec523b67d741d43e6eMathias Agopian    if (exclude.isRect()) {
284f3e85d432749ca77ad707bec523b67d741d43e6eMathias Agopian        return win.reduce(exclude.getBounds());
285f3e85d432749ca77ad707bec523b67d741d43e6eMathias Agopian    }
286f3e85d432749ca77ad707bec523b67d741d43e6eMathias Agopian    return Region(win).subtract(exclude).getBounds();
287f3e85d432749ca77ad707bec523b67d741d43e6eMathias Agopian}
288f3e85d432749ca77ad707bec523b67d741d43e6eMathias Agopian
28980d611613ef1a9c05a31cd3f47badfd4de9b1418Dan Stozastatic FloatRect reduce(const FloatRect& win, const Region& exclude) {
29080d611613ef1a9c05a31cd3f47badfd4de9b1418Dan Stoza    if (CC_LIKELY(exclude.isEmpty())) {
29180d611613ef1a9c05a31cd3f47badfd4de9b1418Dan Stoza        return win;
29280d611613ef1a9c05a31cd3f47badfd4de9b1418Dan Stoza    }
29380d611613ef1a9c05a31cd3f47badfd4de9b1418Dan Stoza    // Convert through Rect (by rounding) for lack of FloatRegion
29480d611613ef1a9c05a31cd3f47badfd4de9b1418Dan Stoza    return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
29580d611613ef1a9c05a31cd3f47badfd4de9b1418Dan Stoza}
29680d611613ef1a9c05a31cd3f47badfd4de9b1418Dan Stoza
2971f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert CarrRect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
2981f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    const Layer::State& s(getDrawingState());
2991f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    Rect win(s.active.w, s.active.h);
3001f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr
3011f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    if (!s.crop.isEmpty()) {
3021f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr        win.intersect(s.crop, &win);
3031f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    }
3041f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr
3051f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    Transform t = getTransform();
3061f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    win = t.transform(win);
3071f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr
30841b08b5e97b04d726fb8bbacddbce563a1c890f9Robert Carr    if (!s.finalCrop.isEmpty()) {
30941b08b5e97b04d726fb8bbacddbce563a1c890f9Robert Carr        win.intersect(s.finalCrop, &win);
31041b08b5e97b04d726fb8bbacddbce563a1c890f9Robert Carr    }
31141b08b5e97b04d726fb8bbacddbce563a1c890f9Robert Carr
312e41dbe6ab1d412efd67fdc8344be8da8b19426d2Chia-I Wu    const sp<Layer>& p = mDrawingParent.promote();
3131f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    // Now we need to calculate the parent bounds, so we can clip ourselves to those.
3141f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    // When calculating the parent bounds for purposes of clipping,
3151f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    // we don't need to constrain the parent to its transparent region.
3161f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    // The transparent region is an optimization based on the
3171f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    // buffer contents of the layer, but does not affect the space allocated to
3181f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    // it by policy, and thus children should be allowed to extend into the
3191f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    // parent's transparent region. In fact one of the main uses, is to reduce
3201f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    // buffer allocation size in cases where a child window sits behind a main window
3211f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    // (by marking the hole in the parent window as a transparent region)
3221f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    if (p != nullptr) {
3231f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr        Rect bounds = p->computeScreenBounds(false);
3241f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr        bounds.intersect(win, &win);
3251f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    }
3261f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr
3271f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    if (reduceTransparentRegion) {
3281f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr        auto const screenTransparentRegion = t.transform(s.activeTransparentRegion);
3291f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr        win = reduce(win, screenTransparentRegion);
3301f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    }
3311f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr
3321f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    return win;
3331f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr}
3341f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr
33580d611613ef1a9c05a31cd3f47badfd4de9b1418Dan StozaFloatRect Layer::computeBounds() const {
3361eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian    const Layer::State& s(getDrawingState());
3376c925ede620f4080227bb1fe8a41e4b4502348f8Michael Lentine    return computeBounds(s.activeTransparentRegion);
3386c925ede620f4080227bb1fe8a41e4b4502348f8Michael Lentine}
3396c925ede620f4080227bb1fe8a41e4b4502348f8Michael Lentine
34080d611613ef1a9c05a31cd3f47badfd4de9b1418Dan StozaFloatRect Layer::computeBounds(const Region& activeTransparentRegion) const {
3416c925ede620f4080227bb1fe8a41e4b4502348f8Michael Lentine    const Layer::State& s(getDrawingState());
34213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    Rect win(s.active.w, s.active.h);
343b5d3d2657bad1f012377dfacd354d3100a65768aRobert Carr
344b5d3d2657bad1f012377dfacd354d3100a65768aRobert Carr    if (!s.crop.isEmpty()) {
345b5d3d2657bad1f012377dfacd354d3100a65768aRobert Carr        win.intersect(s.crop, &win);
34613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    }
3471f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr
3481f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    Rect bounds = win;
349e41dbe6ab1d412efd67fdc8344be8da8b19426d2Chia-I Wu    const auto& p = mDrawingParent.promote();
3501f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    if (p != nullptr) {
351de9ec44905aca4f0ef4f473065b0e7d93d7e1cfeRobert Carr        // Look in computeScreenBounds recursive call for explanation of
352de9ec44905aca4f0ef4f473065b0e7d93d7e1cfeRobert Carr        // why we pass false here.
353de9ec44905aca4f0ef4f473065b0e7d93d7e1cfeRobert Carr        bounds = p->computeScreenBounds(false /* reduceTransparentRegion */);
3541f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    }
3551f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr
3561f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    Transform t = getTransform();
35780d611613ef1a9c05a31cd3f47badfd4de9b1418Dan Stoza
35880d611613ef1a9c05a31cd3f47badfd4de9b1418Dan Stoza    FloatRect floatWin = win.toFloatRect();
3591f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    if (p != nullptr) {
36080d611613ef1a9c05a31cd3f47badfd4de9b1418Dan Stoza        floatWin = t.transform(floatWin);
36180d611613ef1a9c05a31cd3f47badfd4de9b1418Dan Stoza        floatWin = floatWin.intersect(bounds.toFloatRect());
36280d611613ef1a9c05a31cd3f47badfd4de9b1418Dan Stoza        floatWin = t.inverse().transform(floatWin);
3631f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    }
3641f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr
3656c7f25afb75ac155bad0b3bc17c0089d0337d060Mathias Agopian    // subtract the transparent region and snap to the bounds
36680d611613ef1a9c05a31cd3f47badfd4de9b1418Dan Stoza    return reduce(floatWin, activeTransparentRegion);
36713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
36813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
3691f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert CarrRect Layer::computeInitialCrop(const sp<const DisplayDevice>& hw) const {
370b5d3d2657bad1f012377dfacd354d3100a65768aRobert Carr    // the crop is the area of the window that gets cropped, but not
37113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    // scaled in any ways.
3721eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian    const State& s(getDrawingState());
37313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
37413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    // apply the projection's clipping to the window crop in
37513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    // layerstack space, and convert-back to layer space.
3766b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian    // if there are no window scaling involved, this operation will map to full
3776b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian    // pixels in the buffer.
3786b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian    // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
3796b44267a3beb457e220cad0666c039d3a765cdb2Mathias Agopian    // a viewport clipping and a window transform. we should use floating point to fix this.
3800e8f1443b87f9009159cef6394de48894f98f826Mathias Agopian
3810e8f1443b87f9009159cef6394de48894f98f826Mathias Agopian    Rect activeCrop(s.active.w, s.active.h);
382b5d3d2657bad1f012377dfacd354d3100a65768aRobert Carr    if (!s.crop.isEmpty()) {
383df7867f3f488222f3708fb00415fbf0fcf74eb97Chia-I Wu        activeCrop.intersect(s.crop, &activeCrop);
3840e8f1443b87f9009159cef6394de48894f98f826Mathias Agopian    }
3850e8f1443b87f9009159cef6394de48894f98f826Mathias Agopian
3861f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    Transform t = getTransform();
3871f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    activeCrop = t.transform(activeCrop);
388acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos    if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
389acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos        activeCrop.clear();
390acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos    }
391b5d3d2657bad1f012377dfacd354d3100a65768aRobert Carr    if (!s.finalCrop.isEmpty()) {
39241fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman        if (!activeCrop.intersect(s.finalCrop, &activeCrop)) {
393acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos            activeCrop.clear();
394acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos        }
395acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos    }
396b1154d14ac8055f77e861c7ec8ef9c9f63c3a899chaviw
397b1154d14ac8055f77e861c7ec8ef9c9f63c3a899chaviw    const auto& p = mDrawingParent.promote();
398b1154d14ac8055f77e861c7ec8ef9c9f63c3a899chaviw    if (p != nullptr) {
399b1154d14ac8055f77e861c7ec8ef9c9f63c3a899chaviw        auto parentCrop = p->computeInitialCrop(hw);
400b1154d14ac8055f77e861c7ec8ef9c9f63c3a899chaviw        activeCrop.intersect(parentCrop, &activeCrop);
401b1154d14ac8055f77e861c7ec8ef9c9f63c3a899chaviw    }
402b1154d14ac8055f77e861c7ec8ef9c9f63c3a899chaviw
4031f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    return activeCrop;
4041f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr}
4051f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr
4065a423eaa86f4c990afcef8c55e3949d0872068b4Dan StozaFloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const {
4071f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    // the content crop is the area of the content that gets scaled to the
4081f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    // layer's size. This is in buffer space.
4095a423eaa86f4c990afcef8c55e3949d0872068b4Dan Stoza    FloatRect crop = getContentCrop().toFloatRect();
4101f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr
4111f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    // In addition there is a WM-specified crop we pull from our drawing state.
4121f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    const State& s(getDrawingState());
4131f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr
4141f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    // Screen space to make reduction to parent crop clearer.
4151f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    Rect activeCrop = computeInitialCrop(hw);
4161f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    Transform t = getTransform();
4171f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    // Back to layer space to work with the content crop.
4181f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    activeCrop = t.inverse().transform(activeCrop);
41913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
42028ea217ff4e0aa9b03dfc12b6f47cccc6aaaae08Michael Lentine    // This needs to be here as transform.transform(Rect) computes the
42128ea217ff4e0aa9b03dfc12b6f47cccc6aaaae08Michael Lentine    // transformed rect and then takes the bounding box of the result before
42228ea217ff4e0aa9b03dfc12b6f47cccc6aaaae08Michael Lentine    // returning. This means
42328ea217ff4e0aa9b03dfc12b6f47cccc6aaaae08Michael Lentine    // transform.inverse().transform(transform.transform(Rect)) != Rect
42428ea217ff4e0aa9b03dfc12b6f47cccc6aaaae08Michael Lentine    // in which case we need to make sure the final rect is clipped to the
42528ea217ff4e0aa9b03dfc12b6f47cccc6aaaae08Michael Lentine    // display bounds.
426acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos    if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
427acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos        activeCrop.clear();
428acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos    }
42913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
430f3e85d432749ca77ad707bec523b67d741d43e6eMathias Agopian    // subtract the transparent region and snap to the bounds
431f3e85d432749ca77ad707bec523b67d741d43e6eMathias Agopian    activeCrop = reduce(activeCrop, s.activeTransparentRegion);
432f3e85d432749ca77ad707bec523b67d741d43e6eMathias Agopian
433acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos    // Transform the window crop to match the buffer coordinate system,
434acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos    // which means using the inverse of the current transform set on the
435acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos    // SurfaceFlingerConsumer.
436acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos    uint32_t invTransform = mCurrentTransform;
437cae605cd5668f8107e340ad8a672c7f034147401Robert Carr    if (getTransformToDisplayInverse()) {
438acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos        /*
439021623b5a82e44e8dc1a7def1abfed351187593cPablo Ceballos         * the code below applies the primary display's inverse transform to the
440021623b5a82e44e8dc1a7def1abfed351187593cPablo Ceballos         * buffer
441acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos         */
44241fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman        uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
443acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos        // calculate the inverse transform
444acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos        if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
44541fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman            invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
44613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        }
447acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos        // and apply to the current transform
44841fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman        invTransform = (Transform(invTransformOrient) * Transform(invTransform)).getOrientation();
449acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos    }
450acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos
451acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos    int winWidth = s.active.w;
452acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos    int winHeight = s.active.h;
453acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos    if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
454acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos        // If the activeCrop has been rotate the ends are rotated but not
455acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos        // the space itself so when transforming ends back we can't rely on
456acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos        // a modification of the axes of rotation. To account for this we
457acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos        // need to reorient the inverse rotation in terms of the current
458acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos        // axes of rotation.
459acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos        bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
460acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos        bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
461acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos        if (is_h_flipped == is_v_flipped) {
46241fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman            invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
463acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos        }
464acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos        winWidth = s.active.h;
465acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos        winHeight = s.active.w;
466acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos    }
46741fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    const Rect winCrop = activeCrop.transform(invTransform, s.active.w, s.active.h);
46813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
469acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos    // below, crop is intersected with winCrop expressed in crop's coordinate space
47041fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    float xScale = crop.getWidth() / float(winWidth);
471acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos    float yScale = crop.getHeight() / float(winHeight);
47213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
47341fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    float insetL = winCrop.left * xScale;
47441fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    float insetT = winCrop.top * yScale;
47541fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    float insetR = (winWidth - winCrop.right) * xScale;
476acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos    float insetB = (winHeight - winCrop.bottom) * yScale;
477acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos
47841fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    crop.left += insetL;
47941fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    crop.top += insetT;
48041fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    crop.right -= insetR;
481acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos    crop.bottom -= insetB;
48213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
48313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    return crop;
48413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
48513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
486ae0608381b2b4699218febd6d45ad9d307544d55Robert Carrvoid Layer::setGeometry(const sp<const DisplayDevice>& displayDevice, uint32_t z)
487a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian{
4889e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza    const auto hwcId = displayDevice->getHwcDisplayId();
4896f65f3e7caab86a76419e7c1d1c787634a0852d8David Sodman    auto& hwcInfo = getBE().mHwcLayers[hwcId];
490a537c0f42e8077baafcbc65844adf1ec8397c040Mathias Agopian
4913e8b853d67c737abdb363f9c978e7d83eac4d888Mathias Agopian    // enable this layer
4929e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza    hwcInfo.forceClientComposition = false;
4939e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza
4949e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza    if (isSecure() && !displayDevice->isSecure()) {
4959e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza        hwcInfo.forceClientComposition = true;
4969e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza    }
4979e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza
4989e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza    auto& hwcLayer = hwcInfo.layer;
499dd3cb84cfbe8068790c6233b5829fae9c4a0ee93Jamie Gennis
50013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    // this gives us only the "orientation" component of the transform
5011eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian    const State& s(getDrawingState());
502ecf0fa5340c54757870881c5d24eb331c517d78cDavid Reveman    auto blendMode = HWC2::BlendMode::None;
5036452f12c95e855a96ab8f690f28914cd37de762bRobert Carr    if (!isOpaque(s) || getAlpha() != 1.0f) {
50441fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman        blendMode =
50541fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman                mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
5069e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza    }
507ecf0fa5340c54757870881c5d24eb331c517d78cDavid Reveman    auto error = hwcLayer->setBlendMode(blendMode);
50841fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    ALOGE_IF(error != HWC2::Error::None,
50941fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman             "[%s] Failed to set blend mode %s:"
51041fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman             " %s (%d)",
51141fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman             mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
51241fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman             static_cast<int32_t>(error));
51313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
51413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    // apply the layer's transform, followed by the display's global transform
51513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    // here we're guaranteed that the layer's transform preserves rects
5166c925ede620f4080227bb1fe8a41e4b4502348f8Michael Lentine    Region activeTransparentRegion(s.activeTransparentRegion);
5171f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    Transform t = getTransform();
518b5d3d2657bad1f012377dfacd354d3100a65768aRobert Carr    if (!s.crop.isEmpty()) {
519b5d3d2657bad1f012377dfacd354d3100a65768aRobert Carr        Rect activeCrop(s.crop);
5201f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr        activeCrop = t.transform(activeCrop);
52141fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman        if (!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
522acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos            activeCrop.clear();
523acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos        }
5241f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr        activeCrop = t.inverse().transform(activeCrop, true);
52528ea217ff4e0aa9b03dfc12b6f47cccc6aaaae08Michael Lentine        // This needs to be here as transform.transform(Rect) computes the
52628ea217ff4e0aa9b03dfc12b6f47cccc6aaaae08Michael Lentine        // transformed rect and then takes the bounding box of the result before
52728ea217ff4e0aa9b03dfc12b6f47cccc6aaaae08Michael Lentine        // returning. This means
52828ea217ff4e0aa9b03dfc12b6f47cccc6aaaae08Michael Lentine        // transform.inverse().transform(transform.transform(Rect)) != Rect
52928ea217ff4e0aa9b03dfc12b6f47cccc6aaaae08Michael Lentine        // in which case we need to make sure the final rect is clipped to the
53028ea217ff4e0aa9b03dfc12b6f47cccc6aaaae08Michael Lentine        // display bounds.
53141fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman        if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
532acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos            activeCrop.clear();
533acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos        }
5346c925ede620f4080227bb1fe8a41e4b4502348f8Michael Lentine        // mark regions outside the crop as transparent
5356c925ede620f4080227bb1fe8a41e4b4502348f8Michael Lentine        activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
53641fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman        activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom, s.active.w, s.active.h));
53741fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman        activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
53841fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman        activeTransparentRegion.orSelf(
53941fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman                Rect(activeCrop.right, activeCrop.top, s.active.w, activeCrop.bottom));
5406c925ede620f4080227bb1fe8a41e4b4502348f8Michael Lentine    }
5411f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr
54280d611613ef1a9c05a31cd3f47badfd4de9b1418Dan Stoza    // computeBounds returns a FloatRect to provide more accuracy during the
54380d611613ef1a9c05a31cd3f47badfd4de9b1418Dan Stoza    // transformation. We then round upon constructing 'frame'.
54480d611613ef1a9c05a31cd3f47badfd4de9b1418Dan Stoza    Rect frame{t.transform(computeBounds(activeTransparentRegion))};
545b5d3d2657bad1f012377dfacd354d3100a65768aRobert Carr    if (!s.finalCrop.isEmpty()) {
54641fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman        if (!frame.intersect(s.finalCrop, &frame)) {
547acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos            frame.clear();
548acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos        }
549acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos    }
550acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos    if (!frame.intersect(displayDevice->getViewport(), &frame)) {
551acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos        frame.clear();
552acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos    }
5539e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza    const Transform& tr(displayDevice->getTransform());
5549e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza    Rect transformedFrame = tr.transform(frame);
555ecf0fa5340c54757870881c5d24eb331c517d78cDavid Reveman    error = hwcLayer->setDisplayFrame(transformedFrame);
556e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza    if (error != HWC2::Error::None) {
55741fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman        ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
55841fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman              transformedFrame.left, transformedFrame.top, transformedFrame.right,
55941fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman              transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
560e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza    } else {
561e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza        hwcInfo.displayFrame = transformedFrame;
562e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza    }
5639e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza
5645a423eaa86f4c990afcef8c55e3949d0872068b4Dan Stoza    FloatRect sourceCrop = computeCrop(displayDevice);
5659e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza    error = hwcLayer->setSourceCrop(sourceCrop);
566e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza    if (error != HWC2::Error::None) {
567e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza        ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
56841fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman              "%s (%d)",
56941fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman              mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
57041fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman              to_string(error).c_str(), static_cast<int32_t>(error));
571e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza    } else {
572e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza        hwcInfo.sourceCrop = sourceCrop;
573e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza    }
5749e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza
57513fdc49516d17f41e64e62e73c313b0928bf13ccchaviw    float alpha = static_cast<float>(getAlpha());
5766452f12c95e855a96ab8f690f28914cd37de762bRobert Carr    error = hwcLayer->setPlaneAlpha(alpha);
57741fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    ALOGE_IF(error != HWC2::Error::None,
57841fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman             "[%s] Failed to set plane alpha %.3f: "
57941fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman             "%s (%d)",
58041fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman             mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
5819e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza
582ae0608381b2b4699218febd6d45ad9d307544d55Robert Carr    error = hwcLayer->setZOrder(z);
58341fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
58441fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman             to_string(error).c_str(), static_cast<int32_t>(error));
5852f5f8a51f5994cf14837030d4b3b252a9d1b950bDaniel Nicoara
5862a58963cd96546bc645d05c3d1421a506f42bb64Albert Chaulk    int type = s.type;
5872a58963cd96546bc645d05c3d1421a506f42bb64Albert Chaulk    int appId = s.appId;
588e41dbe6ab1d412efd67fdc8344be8da8b19426d2Chia-I Wu    sp<Layer> parent = mDrawingParent.promote();
5892a58963cd96546bc645d05c3d1421a506f42bb64Albert Chaulk    if (parent.get()) {
5902a58963cd96546bc645d05c3d1421a506f42bb64Albert Chaulk        auto& parentState = parent->getDrawingState();
591ccd348460ca25890a8fb709e82e6f780e3ce878erongliu        if (parentState.type >= 0 || parentState.appId >= 0) {
592ccd348460ca25890a8fb709e82e6f780e3ce878erongliu            type = parentState.type;
593ccd348460ca25890a8fb709e82e6f780e3ce878erongliu            appId = parentState.appId;
594ccd348460ca25890a8fb709e82e6f780e3ce878erongliu        }
5952a58963cd96546bc645d05c3d1421a506f42bb64Albert Chaulk    }
5962a58963cd96546bc645d05c3d1421a506f42bb64Albert Chaulk
5972a58963cd96546bc645d05c3d1421a506f42bb64Albert Chaulk    error = hwcLayer->setInfo(type, appId);
59841fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
59941fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman             static_cast<int32_t>(error));
6009f8386e1118c10dd4927f62637ec7162569bdbdcMathias Agopian
60129a367bb7c14c916e991a6a0028727bd06c1e16eMathias Agopian    /*
60229a367bb7c14c916e991a6a0028727bd06c1e16eMathias Agopian     * Transformations are applied in this order:
60329a367bb7c14c916e991a6a0028727bd06c1e16eMathias Agopian     * 1) buffer orientation/flip/mirror
60429a367bb7c14c916e991a6a0028727bd06c1e16eMathias Agopian     * 2) state transformation (window manager)
60529a367bb7c14c916e991a6a0028727bd06c1e16eMathias Agopian     * 3) layer orientation (screen orientation)
60629a367bb7c14c916e991a6a0028727bd06c1e16eMathias Agopian     * (NOTE: the matrices are multiplied in reverse order)
60729a367bb7c14c916e991a6a0028727bd06c1e16eMathias Agopian     */
60829a367bb7c14c916e991a6a0028727bd06c1e16eMathias Agopian
60929a367bb7c14c916e991a6a0028727bd06c1e16eMathias Agopian    const Transform bufferOrientation(mCurrentTransform);
6101f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    Transform transform(tr * t * bufferOrientation);
611c1c05de415854eb7a13a16b7e22a22de8515123aMathias Agopian
612cae605cd5668f8107e340ad8a672c7f034147401Robert Carr    if (getTransformToDisplayInverse()) {
613c1c05de415854eb7a13a16b7e22a22de8515123aMathias Agopian        /*
614021623b5a82e44e8dc1a7def1abfed351187593cPablo Ceballos         * the code below applies the primary display's inverse transform to the
615021623b5a82e44e8dc1a7def1abfed351187593cPablo Ceballos         * buffer
616c1c05de415854eb7a13a16b7e22a22de8515123aMathias Agopian         */
61741fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman        uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
618c1c05de415854eb7a13a16b7e22a22de8515123aMathias Agopian        // calculate the inverse transform
619c1c05de415854eb7a13a16b7e22a22de8515123aMathias Agopian        if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
62041fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman            invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
621c1c05de415854eb7a13a16b7e22a22de8515123aMathias Agopian        }
622cae605cd5668f8107e340ad8a672c7f034147401Robert Carr
623cae605cd5668f8107e340ad8a672c7f034147401Robert Carr        /*
624cae605cd5668f8107e340ad8a672c7f034147401Robert Carr         * Here we cancel out the orientation component of the WM transform.
625cae605cd5668f8107e340ad8a672c7f034147401Robert Carr         * The scaling and translate components are already included in our bounds
626cae605cd5668f8107e340ad8a672c7f034147401Robert Carr         * computation so it's enough to just omit it in the composition.
627cae605cd5668f8107e340ad8a672c7f034147401Robert Carr         * See comment in onDraw with ref to b/36727915 for why.
628cae605cd5668f8107e340ad8a672c7f034147401Robert Carr         */
629cae605cd5668f8107e340ad8a672c7f034147401Robert Carr        transform = Transform(invTransform) * tr * bufferOrientation;
630c1c05de415854eb7a13a16b7e22a22de8515123aMathias Agopian    }
63129a367bb7c14c916e991a6a0028727bd06c1e16eMathias Agopian
63229a367bb7c14c916e991a6a0028727bd06c1e16eMathias Agopian    // this gives us only the "orientation" component of the transform
63313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    const uint32_t orientation = transform.getOrientation();
634f3bd94a776d69730ddb95bbf7104f2fb89d92ae4Jorim Jaggi    if (orientation & Transform::ROT_INVALID) {
6359e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza        // we can only handle simple transformation
6369e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza        hwcInfo.forceClientComposition = true;
6379e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza    } else {
6389e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza        auto transform = static_cast<HWC2::Transform>(orientation);
639068e31b929b40a1bc9be742c04cbdf5b04f3ce97Yiwei Zhang        hwcInfo.transform = transform;
6409e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza        auto error = hwcLayer->setTransform(transform);
64141fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman        ALOGE_IF(error != HWC2::Error::None,
64241fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman                 "[%s] Failed to set transform %s: "
64341fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman                 "%s (%d)",
64441fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman                 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
64541fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman                 static_cast<int32_t>(error));
6469e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza    }
647a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian}
648a350ff98692b3a50cad5cc93f9f83221242ca86aMathias Agopian
6499e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stozavoid Layer::forceClientComposition(int32_t hwcId) {
6506f65f3e7caab86a76419e7c1d1c787634a0852d8David Sodman    if (getBE().mHwcLayers.count(hwcId) == 0) {
6519e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza        ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
6529e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza        return;
6539e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza    }
6549e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza
6556f65f3e7caab86a76419e7c1d1c787634a0852d8David Sodman    getBE().mHwcLayers[hwcId].forceClientComposition = true;
6569e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza}
657eb085e0c579f310e2670204b14b742b68c70bc9eDavid Sodman
658c9232edd34618360d099272fb0537cae2f715860chaviwbool Layer::getForceClientComposition(int32_t hwcId) {
6596f65f3e7caab86a76419e7c1d1c787634a0852d8David Sodman    if (getBE().mHwcLayers.count(hwcId) == 0) {
660c9232edd34618360d099272fb0537cae2f715860chaviw        ALOGE("getForceClientComposition: no HWC layer found (%d)", hwcId);
661c9232edd34618360d099272fb0537cae2f715860chaviw        return false;
662c9232edd34618360d099272fb0537cae2f715860chaviw    }
663c9232edd34618360d099272fb0537cae2f715860chaviw
6646f65f3e7caab86a76419e7c1d1c787634a0852d8David Sodman    return getBE().mHwcLayers[hwcId].forceClientComposition;
665c9232edd34618360d099272fb0537cae2f715860chaviw}
666c9232edd34618360d099272fb0537cae2f715860chaviw
6679e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stozavoid Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
6689e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza    auto hwcId = displayDevice->getHwcDisplayId();
6696f65f3e7caab86a76419e7c1d1c787634a0852d8David Sodman    if (getBE().mHwcLayers.count(hwcId) == 0 ||
6709eeae69005ef9a774bde19195b9c06eba7e463c2David Sodman        getCompositionType(hwcId) != HWC2::Composition::Cursor) {
6719e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza        return;
6729e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza    }
6739e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza
6749e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza    // This gives us only the "orientation" component of the transform
6759e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza    const State& s(getCurrentState());
676dc5b485f74edf2d2f31c62054eb6c180421a3adeJesse Hall
6779e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza    // Apply the layer's transform, followed by the display's global transform
6789e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza    // Here we're guaranteed that the layer's transform preserves rects
6799e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza    Rect win(s.active.w, s.active.h);
680b5d3d2657bad1f012377dfacd354d3100a65768aRobert Carr    if (!s.crop.isEmpty()) {
681b5d3d2657bad1f012377dfacd354d3100a65768aRobert Carr        win.intersect(s.crop, &win);
6829e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza    }
6839e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza    // Subtract the transparent region and snap to the bounds
6849e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza    Rect bounds = reduce(win, s.activeTransparentRegion);
6851f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    Rect frame(getTransform().transform(bounds));
6869e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza    frame.intersect(displayDevice->getViewport(), &frame);
687b5d3d2657bad1f012377dfacd354d3100a65768aRobert Carr    if (!s.finalCrop.isEmpty()) {
688b5d3d2657bad1f012377dfacd354d3100a65768aRobert Carr        frame.intersect(s.finalCrop, &frame);
689acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos    }
6909e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza    auto& displayTransform(displayDevice->getTransform());
6919e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza    auto position = displayTransform.transform(frame);
6929e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza
6936f65f3e7caab86a76419e7c1d1c787634a0852d8David Sodman    auto error = getBE().mHwcLayers[hwcId].layer->setCursorPosition(position.left,
6949eeae69005ef9a774bde19195b9c06eba7e463c2David Sodman                                                                              position.top);
69541fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    ALOGE_IF(error != HWC2::Error::None,
69641fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman             "[%s] Failed to set cursor position "
69741fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman             "to (%d, %d): %s (%d)",
69841fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman             mName.string(), position.left, position.top, to_string(error).c_str(),
69941fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman             static_cast<int32_t>(error));
7009e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza}
70103414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews
70213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// ---------------------------------------------------------------------------
70313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// drawing...
70413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// ---------------------------------------------------------------------------
70513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
706a76b271f0e14325fa0ebb98e1cac0a15adfea1cbchaviwvoid Layer::draw(const RenderArea& renderArea, const Region& clip) const {
707a76b271f0e14325fa0ebb98e1cac0a15adfea1cbchaviw    onDraw(renderArea, clip, false);
70813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
70913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
710a76b271f0e14325fa0ebb98e1cac0a15adfea1cbchaviwvoid Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) const {
711a76b271f0e14325fa0ebb98e1cac0a15adfea1cbchaviw    onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
71213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
71313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
714a76b271f0e14325fa0ebb98e1cac0a15adfea1cbchaviwvoid Layer::draw(const RenderArea& renderArea) const {
715a76b271f0e14325fa0ebb98e1cac0a15adfea1cbchaviw    onDraw(renderArea, Region(renderArea.getBounds()), false);
716c701401f8cec2e5309f8b57e2b97baced5093274Dan Stoza}
717c701401f8cec2e5309f8b57e2b97baced5093274Dan Stoza
71841fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodmanvoid Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
71941fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman                            float alpha) const {
720144e116f45f196396f0d59d5fc09766ab618f885Lloyd Pique    auto& engine(mFlinger->getRenderEngine());
7219eeae69005ef9a774bde19195b9c06eba7e463c2David Sodman    computeGeometry(renderArea, getBE().mMesh, false);
72219733a32799f792125913e746e8644d16f8dc223Mathias Agopian    engine.setupFillWithColor(red, green, blue, alpha);
7239eeae69005ef9a774bde19195b9c06eba7e463c2David Sodman    engine.drawMesh(getBE().mMesh);
72413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
72513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
726a76b271f0e14325fa0ebb98e1cac0a15adfea1cbchaviwvoid Layer::clearWithOpenGL(const RenderArea& renderArea) const {
72741fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    clearWithOpenGL(renderArea, 0, 0, 0, 0);
72813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
72913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
73041fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodmanvoid Layer::setCompositionType(int32_t hwcId, HWC2::Composition type, bool callIntoHwc) {
7316f65f3e7caab86a76419e7c1d1c787634a0852d8David Sodman    if (getBE().mHwcLayers.count(hwcId) == 0) {
7329e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza        ALOGE("setCompositionType called without a valid HWC layer");
7339e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza        return;
7349e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza    }
7356f65f3e7caab86a76419e7c1d1c787634a0852d8David Sodman    auto& hwcInfo = getBE().mHwcLayers[hwcId];
7369e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza    auto& hwcLayer = hwcInfo.layer;
73741fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(), to_string(type).c_str(),
73841fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman          static_cast<int>(callIntoHwc));
7399e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza    if (hwcInfo.compositionType != type) {
7409e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza        ALOGV("    actually setting");
7419e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza        hwcInfo.compositionType = type;
7429e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza        if (callIntoHwc) {
7439e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza            auto error = hwcLayer->setCompositionType(type);
74441fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman            ALOGE_IF(error != HWC2::Error::None,
74541fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman                     "[%s] Failed to set "
74641fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman                     "composition type %s: %s (%d)",
74741fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman                     mName.string(), to_string(type).c_str(), to_string(error).c_str(),
74841fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman                     static_cast<int32_t>(error));
7499e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza        }
7509e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza    }
7519e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza}
7529e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza
7539e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan StozaHWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
754ec0f717dfd3821cbc648198c6d3b98fcc3b53369Dan Stoza    if (hwcId == DisplayDevice::DISPLAY_ID_INVALID) {
755ec0f717dfd3821cbc648198c6d3b98fcc3b53369Dan Stoza        // If we're querying the composition type for a display that does not
756ec0f717dfd3821cbc648198c6d3b98fcc3b53369Dan Stoza        // have a HWC counterpart, then it will always be Client
757ec0f717dfd3821cbc648198c6d3b98fcc3b53369Dan Stoza        return HWC2::Composition::Client;
758ec0f717dfd3821cbc648198c6d3b98fcc3b53369Dan Stoza    }
7596f65f3e7caab86a76419e7c1d1c787634a0852d8David Sodman    if (getBE().mHwcLayers.count(hwcId) == 0) {
760ec0f717dfd3821cbc648198c6d3b98fcc3b53369Dan Stoza        ALOGE("getCompositionType called with an invalid HWC layer");
7619e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza        return HWC2::Composition::Invalid;
7629e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza    }
7636f65f3e7caab86a76419e7c1d1c787634a0852d8David Sodman    return getBE().mHwcLayers.at(hwcId).compositionType;
7649e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza}
7659e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza
7669e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stozavoid Layer::setClearClientTarget(int32_t hwcId, bool clear) {
7676f65f3e7caab86a76419e7c1d1c787634a0852d8David Sodman    if (getBE().mHwcLayers.count(hwcId) == 0) {
7689e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza        ALOGE("setClearClientTarget called without a valid HWC layer");
7699e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza        return;
7709e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza    }
7716f65f3e7caab86a76419e7c1d1c787634a0852d8David Sodman    getBE().mHwcLayers[hwcId].clearClientTarget = clear;
7729e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza}
7739e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza
7749e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stozabool Layer::getClearClientTarget(int32_t hwcId) const {
7756f65f3e7caab86a76419e7c1d1c787634a0852d8David Sodman    if (getBE().mHwcLayers.count(hwcId) == 0) {
7769e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza        ALOGE("getClearClientTarget called without a valid HWC layer");
7779e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza        return false;
7789e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza    }
7796f65f3e7caab86a76419e7c1d1c787634a0852d8David Sodman    return getBE().mHwcLayers.at(hwcId).clearClientTarget;
7809e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza}
7819e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stoza
782cac353808ec1048333d7fd2f3d596fb4db567aa3Dan Stozabool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
783cac353808ec1048333d7fd2f3d596fb4db567aa3Dan Stoza    if (point->getFrameNumber() <= mCurrentFrameNumber) {
784cac353808ec1048333d7fd2f3d596fb4db567aa3Dan Stoza        // Don't bother with a SyncPoint, since we've already latched the
785cac353808ec1048333d7fd2f3d596fb4db567aa3Dan Stoza        // relevant frame
786cac353808ec1048333d7fd2f3d596fb4db567aa3Dan Stoza        return false;
7877dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza    }
788cac353808ec1048333d7fd2f3d596fb4db567aa3Dan Stoza
789cac353808ec1048333d7fd2f3d596fb4db567aa3Dan Stoza    Mutex::Autolock lock(mLocalSyncPointMutex);
790cac353808ec1048333d7fd2f3d596fb4db567aa3Dan Stoza    mLocalSyncPoints.push_back(point);
791cac353808ec1048333d7fd2f3d596fb4db567aa3Dan Stoza    return true;
7927dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza}
7937dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza
79413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianvoid Layer::setFiltering(bool filtering) {
79513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mFiltering = filtering;
79613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
79713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
79813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianbool Layer::getFiltering() const {
79913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    return mFiltering;
80013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
80113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
80213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// ----------------------------------------------------------------------------
80313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// local state
80413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// ----------------------------------------------------------------------------
80513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
806acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballosstatic void boundPoint(vec2* point, const Rect& crop) {
807acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos    if (point->x < crop.left) {
808acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos        point->x = crop.left;
809acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos    }
810acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos    if (point->x > crop.right) {
811acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos        point->x = crop.right;
812acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos    }
813acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos    if (point->y < crop.top) {
814acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos        point->y = crop.top;
815acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos    }
816acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos    if (point->y > crop.bottom) {
817acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos        point->y = crop.bottom;
818acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos    }
819acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos}
820acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos
821a76b271f0e14325fa0ebb98e1cac0a15adfea1cbchaviwvoid Layer::computeGeometry(const RenderArea& renderArea, Mesh& mesh,
822a76b271f0e14325fa0ebb98e1cac0a15adfea1cbchaviw                            bool useIdentityTransform) const {
8231eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian    const Layer::State& s(getDrawingState());
824a76b271f0e14325fa0ebb98e1cac0a15adfea1cbchaviw    const Transform renderAreaTransform(renderArea.getTransform());
825a76b271f0e14325fa0ebb98e1cac0a15adfea1cbchaviw    const uint32_t height = renderArea.getHeight();
82680d611613ef1a9c05a31cd3f47badfd4de9b1418Dan Stoza    FloatRect win = computeBounds();
8273f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian
828acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos    vec2 lt = vec2(win.left, win.top);
829acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos    vec2 lb = vec2(win.left, win.bottom);
830acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos    vec2 rb = vec2(win.right, win.bottom);
831acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos    vec2 rt = vec2(win.right, win.top);
832acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos
8331f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    Transform layerTransform = getTransform();
834acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos    if (!useIdentityTransform) {
8351f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr        lt = layerTransform.transform(lt);
8361f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr        lb = layerTransform.transform(lb);
8371f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr        rb = layerTransform.transform(rb);
8381f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr        rt = layerTransform.transform(rt);
839acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos    }
840acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos
841b5d3d2657bad1f012377dfacd354d3100a65768aRobert Carr    if (!s.finalCrop.isEmpty()) {
842b5d3d2657bad1f012377dfacd354d3100a65768aRobert Carr        boundPoint(&lt, s.finalCrop);
843b5d3d2657bad1f012377dfacd354d3100a65768aRobert Carr        boundPoint(&lb, s.finalCrop);
844b5d3d2657bad1f012377dfacd354d3100a65768aRobert Carr        boundPoint(&rb, s.finalCrop);
845b5d3d2657bad1f012377dfacd354d3100a65768aRobert Carr        boundPoint(&rt, s.finalCrop);
846acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos    }
847acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos
848ff2ed70fa30f04b90dd1a2c06ec2319e157152d7Mathias Agopian    Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
849a76b271f0e14325fa0ebb98e1cac0a15adfea1cbchaviw    position[0] = renderAreaTransform.transform(lt);
850a76b271f0e14325fa0ebb98e1cac0a15adfea1cbchaviw    position[1] = renderAreaTransform.transform(lb);
851a76b271f0e14325fa0ebb98e1cac0a15adfea1cbchaviw    position[2] = renderAreaTransform.transform(rb);
852a76b271f0e14325fa0ebb98e1cac0a15adfea1cbchaviw    position[3] = renderAreaTransform.transform(rt);
85341fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    for (size_t i = 0; i < 4; i++) {
854a76b271f0e14325fa0ebb98e1cac0a15adfea1cbchaviw        position[i].y = height - position[i].y;
85513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    }
85613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
857ac45e6bff1b41acd35c981291b37b23f8e083ceeEric Hassold
85841fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodmanbool Layer::isSecure() const {
859231160866738f6ed2175701f300fed1a8e8e02b0Dan Stoza    const Layer::State& s(mDrawingState);
860231160866738f6ed2175701f300fed1a8e8e02b0Dan Stoza    return (s.flags & layer_state_t::eLayerSecure);
861231160866738f6ed2175701f300fed1a8e8e02b0Dan Stoza}
862231160866738f6ed2175701f300fed1a8e8e02b0Dan Stoza
86313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianvoid Layer::setVisibleRegion(const Region& visibleRegion) {
86413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    // always called from main thread
86513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    this->visibleRegion = visibleRegion;
86613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
86713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
86813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianvoid Layer::setCoveredRegion(const Region& coveredRegion) {
86913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    // always called from main thread
87013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    this->coveredRegion = coveredRegion;
87113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
87213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
87341fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodmanvoid Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
87413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    // always called from main thread
87513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
87613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
87713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
878e5f4f694f11ad1e8a9bb3b1b9579a175435c7c19Robert Carrvoid Layer::clearVisibilityRegions() {
879e5f4f694f11ad1e8a9bb3b1b9579a175435c7c19Robert Carr    visibleRegion.clear();
880e5f4f694f11ad1e8a9bb3b1b9579a175435c7c19Robert Carr    visibleNonTransparentRegion.clear();
881e5f4f694f11ad1e8a9bb3b1b9579a175435c7c19Robert Carr    coveredRegion.clear();
882e5f4f694f11ad1e8a9bb3b1b9579a175435c7c19Robert Carr}
883e5f4f694f11ad1e8a9bb3b1b9579a175435c7c19Robert Carr
88413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// ----------------------------------------------------------------------------
88513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// transaction
88613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// ----------------------------------------------------------------------------
88713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
8887dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stozavoid Layer::pushPendingState() {
8897dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza    if (!mCurrentState.modified) {
8907dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza        return;
8917dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza    }
8927dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza
8937dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza    // If this transaction is waiting on the receipt of a frame, generate a sync
8947dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza    // point and send it to the remote layer.
8950d48072f6047140119ff194c1194ce402fca2c0bRobert Carr    if (mCurrentState.barrierLayer != nullptr) {
8960d48072f6047140119ff194c1194ce402fca2c0bRobert Carr        sp<Layer> barrierLayer = mCurrentState.barrierLayer.promote();
8970d48072f6047140119ff194c1194ce402fca2c0bRobert Carr        if (barrierLayer == nullptr) {
8980d48072f6047140119ff194c1194ce402fca2c0bRobert Carr            ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
8997dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza            // If we can't promote the layer we are intended to wait on,
9007dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza            // then it is expired or otherwise invalid. Allow this transaction
9017dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza            // to be applied as per normal (no synchronization).
9020d48072f6047140119ff194c1194ce402fca2c0bRobert Carr            mCurrentState.barrierLayer = nullptr;
9033bddd5b7731fafd2c60e75416ccd091972d196aePablo Ceballos        } else {
90441fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman            auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber);
9050d48072f6047140119ff194c1194ce402fca2c0bRobert Carr            if (barrierLayer->addSyncPoint(syncPoint)) {
906cac353808ec1048333d7fd2f3d596fb4db567aa3Dan Stoza                mRemoteSyncPoints.push_back(std::move(syncPoint));
907cac353808ec1048333d7fd2f3d596fb4db567aa3Dan Stoza            } else {
908cac353808ec1048333d7fd2f3d596fb4db567aa3Dan Stoza                // We already missed the frame we're supposed to synchronize
909cac353808ec1048333d7fd2f3d596fb4db567aa3Dan Stoza                // on, so go ahead and apply the state update
9100d48072f6047140119ff194c1194ce402fca2c0bRobert Carr                mCurrentState.barrierLayer = nullptr;
911cac353808ec1048333d7fd2f3d596fb4db567aa3Dan Stoza            }
9127dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza        }
9137dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza
9147dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza        // Wake us up to check if the frame has been received
9157dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza        setTransactionFlags(eTransactionNeeded);
916f5702ff8b1919e0a5d342ad04ac486bbcde41461Dan Stoza        mFlinger->setTransactionFlags(eTraversalNeeded);
9177dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza    }
9187dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza    mPendingStates.push_back(mCurrentState);
919f7ba41abeb819e915997b4612a7af8c685d1fe3cDan Stoza    ATRACE_INT(mTransactionName.string(), mPendingStates.size());
9207dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza}
9217dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza
92205289c2b78d21308a95ea6ef8f1d8f24359147cdPablo Ceballosvoid Layer::popPendingState(State* stateToCommit) {
92305289c2b78d21308a95ea6ef8f1d8f24359147cdPablo Ceballos    auto oldFlags = stateToCommit->flags;
92405289c2b78d21308a95ea6ef8f1d8f24359147cdPablo Ceballos    *stateToCommit = mPendingStates[0];
92541fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    stateToCommit->flags =
92641fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman            (oldFlags & ~stateToCommit->mask) | (stateToCommit->flags & stateToCommit->mask);
9277dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza
9287dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza    mPendingStates.removeAt(0);
929f7ba41abeb819e915997b4612a7af8c685d1fe3cDan Stoza    ATRACE_INT(mTransactionName.string(), mPendingStates.size());
9307dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza}
9317dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza
93205289c2b78d21308a95ea6ef8f1d8f24359147cdPablo Ceballosbool Layer::applyPendingStates(State* stateToCommit) {
9337dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza    bool stateUpdateAvailable = false;
9347dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza    while (!mPendingStates.empty()) {
9350d48072f6047140119ff194c1194ce402fca2c0bRobert Carr        if (mPendingStates[0].barrierLayer != nullptr) {
9367dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza            if (mRemoteSyncPoints.empty()) {
9377dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza                // If we don't have a sync point for this, apply it anyway. It
9387dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza                // will be visually wrong, but it should keep us from getting
9397dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza                // into too much trouble.
9407dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza                ALOGE("[%s] No local sync point found", mName.string());
94105289c2b78d21308a95ea6ef8f1d8f24359147cdPablo Ceballos                popPendingState(stateToCommit);
9427dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza                stateUpdateAvailable = true;
9437dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza                continue;
9447dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza            }
9457dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza
94641fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman            if (mRemoteSyncPoints.front()->getFrameNumber() != mPendingStates[0].frameNumber) {
94741fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman                ALOGE("[%s] Unexpected sync point frame number found", mName.string());
948cac353808ec1048333d7fd2f3d596fb4db567aa3Dan Stoza
949cac353808ec1048333d7fd2f3d596fb4db567aa3Dan Stoza                // Signal our end of the sync point and then dispose of it
950cac353808ec1048333d7fd2f3d596fb4db567aa3Dan Stoza                mRemoteSyncPoints.front()->setTransactionApplied();
951cac353808ec1048333d7fd2f3d596fb4db567aa3Dan Stoza                mRemoteSyncPoints.pop_front();
952cac353808ec1048333d7fd2f3d596fb4db567aa3Dan Stoza                continue;
953cac353808ec1048333d7fd2f3d596fb4db567aa3Dan Stoza            }
954cac353808ec1048333d7fd2f3d596fb4db567aa3Dan Stoza
9557dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza            if (mRemoteSyncPoints.front()->frameIsAvailable()) {
9567dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza                // Apply the state update
95705289c2b78d21308a95ea6ef8f1d8f24359147cdPablo Ceballos                popPendingState(stateToCommit);
9587dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza                stateUpdateAvailable = true;
9597dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza
9607dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza                // Signal our end of the sync point and then dispose of it
9617dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza                mRemoteSyncPoints.front()->setTransactionApplied();
9627dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza                mRemoteSyncPoints.pop_front();
963792e529dee5b6cf839b982b4b06a37fbf1d3e28aDan Stoza            } else {
964792e529dee5b6cf839b982b4b06a37fbf1d3e28aDan Stoza                break;
9657dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza            }
9667dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza        } else {
96705289c2b78d21308a95ea6ef8f1d8f24359147cdPablo Ceballos            popPendingState(stateToCommit);
9687dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza            stateUpdateAvailable = true;
9697dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza        }
9707dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza    }
9717dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza
9727dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza    // If we still have pending updates, wake SurfaceFlinger back up and point
9737dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza    // it at this layer so we can process them
9747dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza    if (!mPendingStates.empty()) {
9757dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza        setTransactionFlags(eTransactionNeeded);
9767dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza        mFlinger->setTransactionFlags(eTraversalNeeded);
9777dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza    }
9787dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza
9797dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza    mCurrentState.modified = false;
9807dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza    return stateUpdateAvailable;
9817dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza}
9827dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza
98313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianuint32_t Layer::doTransaction(uint32_t flags) {
9841c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis    ATRACE_CALL();
9851c8e95cf86f2182986385bc1ee85f13f425f3a3aJamie Gennis
9867dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza    pushPendingState();
98705289c2b78d21308a95ea6ef8f1d8f24359147cdPablo Ceballos    Layer::State c = getCurrentState();
98805289c2b78d21308a95ea6ef8f1d8f24359147cdPablo Ceballos    if (!applyPendingStates(&c)) {
9897dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza        return 0;
9907dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza    }
9917dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza
9921eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian    const Layer::State& s(getDrawingState());
993edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
99441fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    const bool sizeChanged = (c.requested.w != s.requested.w) || (c.requested.h != s.requested.h);
995a138f89c5e78b7e8994823e97d6e860869762837Mathias Agopian
996eb085e0c579f310e2670204b14b742b68c70bc9eDavid Sodman    if (sizeChanged) {
997cbb288bfe89f585bf48371bd31b2d4aafa32f32eMathias Agopian        // the size changed, we need to ask our client to request a new buffer
9989d4536835248525f32f1504a3d28d5bbfa0a2910Steve Block        ALOGD_IF(DEBUG_RESIZE,
99941fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman                 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
100041fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman                 "  current={ active   ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
100141fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman                 "            requested={ wh={%4u,%4u} }}\n"
100241fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman                 "  drawing={ active   ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
100341fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman                 "            requested={ wh={%4u,%4u} }}\n",
10049eeae69005ef9a774bde19195b9c06eba7e463c2David Sodman                 this, getName().string(), mCurrentTransform,
10059eeae69005ef9a774bde19195b9c06eba7e463c2David Sodman                 getEffectiveScalingMode(), c.active.w, c.active.h, c.crop.left, c.crop.top,
10069eeae69005ef9a774bde19195b9c06eba7e463c2David Sodman                 c.crop.right, c.crop.bottom, c.crop.getWidth(), c.crop.getHeight(), c.requested.w,
10079eeae69005ef9a774bde19195b9c06eba7e463c2David Sodman                 c.requested.h, s.active.w, s.active.h, s.crop.left, s.crop.top, s.crop.right,
10089eeae69005ef9a774bde19195b9c06eba7e463c2David Sodman                 s.crop.bottom, s.crop.getWidth(), s.crop.getHeight(), s.requested.w,
10099eeae69005ef9a774bde19195b9c06eba7e463c2David Sodman                 s.requested.h);
1010a138f89c5e78b7e8994823e97d6e860869762837Mathias Agopian
10112a0d5b608447a880beff5149805425f02691442bJamie Gennis        // record the new size, form this point on, when the client request
10122a0d5b608447a880beff5149805425f02691442bJamie Gennis        // a buffer, it'll get the new size.
1013eb085e0c579f310e2670204b14b742b68c70bc9eDavid Sodman        setDefaultBufferSize(c.requested.w, c.requested.h);
1014edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
1015cbb288bfe89f585bf48371bd31b2d4aafa32f32eMathias Agopian
1016e392b55de711657d2a955b9785588d009b5e922dRobert Carr    // Don't let Layer::doTransaction update the drawing state
1017e392b55de711657d2a955b9785588d009b5e922dRobert Carr    // if we have a pending resize, unless we are in fixed-size mode.
1018e392b55de711657d2a955b9785588d009b5e922dRobert Carr    // the drawing state will be updated only once we receive a buffer
1019e392b55de711657d2a955b9785588d009b5e922dRobert Carr    // with the correct size.
1020e392b55de711657d2a955b9785588d009b5e922dRobert Carr    //
1021e392b55de711657d2a955b9785588d009b5e922dRobert Carr    // In particular, we want to make sure the clip (which is part
1022e392b55de711657d2a955b9785588d009b5e922dRobert Carr    // of the geometry state) is latched together with the size but is
1023e392b55de711657d2a955b9785588d009b5e922dRobert Carr    // latched immediately when no resizing is involved.
1024e392b55de711657d2a955b9785588d009b5e922dRobert Carr    //
1025e392b55de711657d2a955b9785588d009b5e922dRobert Carr    // If a sideband stream is attached, however, we want to skip this
1026e392b55de711657d2a955b9785588d009b5e922dRobert Carr    // optimization so that transactions aren't missed when a buffer
1027e392b55de711657d2a955b9785588d009b5e922dRobert Carr    // never arrives
1028e392b55de711657d2a955b9785588d009b5e922dRobert Carr    //
1029e392b55de711657d2a955b9785588d009b5e922dRobert Carr    // In the case that we don't have a buffer we ignore other factors
1030e392b55de711657d2a955b9785588d009b5e922dRobert Carr    // and avoid entering the resizePending state. At a high level the
1031e392b55de711657d2a955b9785588d009b5e922dRobert Carr    // resizePending state is to avoid applying the state of the new buffer
1032e392b55de711657d2a955b9785588d009b5e922dRobert Carr    // to the old buffer. However in the state where we don't have an old buffer
1033e392b55de711657d2a955b9785588d009b5e922dRobert Carr    // there is no such concern but we may still be being used as a parent layer.
103441fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    const bool resizePending = ((c.requested.w != c.active.w) || (c.requested.h != c.active.h)) &&
10350cc69188196037c06127c0bc56c516069dbaa558David Sodman            (getBE().compositionInfo.mBuffer != nullptr);
10360cd545f14261d829513e0d6e8fa5e4e4f3372b3dMathias Agopian    if (!isFixedSize()) {
10370cc69188196037c06127c0bc56c516069dbaa558David Sodman        if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
10380cd545f14261d829513e0d6e8fa5e4e4f3372b3dMathias Agopian            flags |= eDontUpdateGeometryState;
10390cd545f14261d829513e0d6e8fa5e4e4f3372b3dMathias Agopian        }
10400cd545f14261d829513e0d6e8fa5e4e4f3372b3dMathias Agopian    }
10410cd545f14261d829513e0d6e8fa5e4e4f3372b3dMathias Agopian
10427bf247e2db180da661b18084d50e0b389686fc0cRobert Carr    // Here we apply various requested geometry states, depending on our
10437bf247e2db180da661b18084d50e0b389686fc0cRobert Carr    // latching configuration. See Layer.h for a detailed discussion of
10447bf247e2db180da661b18084d50e0b389686fc0cRobert Carr    // how geometry latching is controlled.
10457bf247e2db180da661b18084d50e0b389686fc0cRobert Carr    if (!(flags & eDontUpdateGeometryState)) {
10467d05257585918bd578bc0bc18f32571a48a7304aPablo Ceballos        Layer::State& editCurrentState(getCurrentState());
10477bf247e2db180da661b18084d50e0b389686fc0cRobert Carr
10487bf247e2db180da661b18084d50e0b389686fc0cRobert Carr        // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
10497bf247e2db180da661b18084d50e0b389686fc0cRobert Carr        // mode, which causes attributes which normally latch regardless of scaling mode,
10507bf247e2db180da661b18084d50e0b389686fc0cRobert Carr        // to be delayed. We copy the requested state to the active state making sure
10517bf247e2db180da661b18084d50e0b389686fc0cRobert Carr        // to respect these rules (again see Layer.h for a detailed discussion).
10527bf247e2db180da661b18084d50e0b389686fc0cRobert Carr        //
10537bf247e2db180da661b18084d50e0b389686fc0cRobert Carr        // There is an awkward asymmetry in the handling of the crop states in the position
10547bf247e2db180da661b18084d50e0b389686fc0cRobert Carr        // states, as can be seen below. Largely this arises from position and transform
10557bf247e2db180da661b18084d50e0b389686fc0cRobert Carr        // being stored in the same data structure while having different latching rules.
10567bf247e2db180da661b18084d50e0b389686fc0cRobert Carr        // b/38182305
10577bf247e2db180da661b18084d50e0b389686fc0cRobert Carr        //
10587bf247e2db180da661b18084d50e0b389686fc0cRobert Carr        // Careful that "c" and editCurrentState may not begin as equivalent due to
10597bf247e2db180da661b18084d50e0b389686fc0cRobert Carr        // applyPendingStates in the presence of deferred transactions.
10607bf247e2db180da661b18084d50e0b389686fc0cRobert Carr        if (mFreezeGeometryUpdates) {
106182364e3cea0bf88fa8147766433329b3dd5148b8Robert Carr            float tx = c.active.transform.tx();
106282364e3cea0bf88fa8147766433329b3dd5148b8Robert Carr            float ty = c.active.transform.ty();
106382364e3cea0bf88fa8147766433329b3dd5148b8Robert Carr            c.active = c.requested;
106482364e3cea0bf88fa8147766433329b3dd5148b8Robert Carr            c.active.transform.set(tx, ty);
106582364e3cea0bf88fa8147766433329b3dd5148b8Robert Carr            editCurrentState.active = c.active;
106682364e3cea0bf88fa8147766433329b3dd5148b8Robert Carr        } else {
106782364e3cea0bf88fa8147766433329b3dd5148b8Robert Carr            editCurrentState.active = editCurrentState.requested;
106882364e3cea0bf88fa8147766433329b3dd5148b8Robert Carr            c.active = c.requested;
106982364e3cea0bf88fa8147766433329b3dd5148b8Robert Carr        }
107013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    }
107113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
10721eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian    if (s.active != c.active) {
107313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        // invalidate and recompute the visible regions if needed
107413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        flags |= Layer::eVisibleRegion;
107513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    }
107613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
10771eae0ee49402c39f1b08cc8fec129023f86494b7Mathias Agopian    if (c.sequence != s.sequence) {
107813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        // invalidate and recompute the visible regions if needed
107913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        flags |= eVisibleRegion;
108013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        this->contentDirty = true;
108113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
108213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        // we may use linear filtering, if the matrix scales us
10833dcabfab7ef80df5884b269fec17350a26da6f51Robert Carr        const uint8_t type = c.active.transform.getType();
108441fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman        mNeedsFiltering = (!c.active.transform.preserveRects() || (type >= Transform::SCALE));
108513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    }
108613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
1087c8145170a4b3bfdcfd6bd08b61ad55ae4eac9cc9Dan Stoza    // If the layer is hidden, signal and clear out all local sync points so
1088c8145170a4b3bfdcfd6bd08b61ad55ae4eac9cc9Dan Stoza    // that transactions for layers depending on this layer's frames becoming
1089c8145170a4b3bfdcfd6bd08b61ad55ae4eac9cc9Dan Stoza    // visible are not blocked
1090c8145170a4b3bfdcfd6bd08b61ad55ae4eac9cc9Dan Stoza    if (c.flags & layer_state_t::eLayerHidden) {
10911f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr        clearSyncPoints();
1092c8145170a4b3bfdcfd6bd08b61ad55ae4eac9cc9Dan Stoza    }
1093c8145170a4b3bfdcfd6bd08b61ad55ae4eac9cc9Dan Stoza
109413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    // Commit the transaction
109505289c2b78d21308a95ea6ef8f1d8f24359147cdPablo Ceballos    commitTransaction(c);
109613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    return flags;
1097edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
1098edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
109905289c2b78d21308a95ea6ef8f1d8f24359147cdPablo Ceballosvoid Layer::commitTransaction(const State& stateToCommit) {
110005289c2b78d21308a95ea6ef8f1d8f24359147cdPablo Ceballos    mDrawingState = stateToCommit;
1101a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian}
1102a67932fe6864ac346e7f78b86df11cf6c5344137Mathias Agopian
110313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianuint32_t Layer::getTransactionFlags(uint32_t flags) {
110413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    return android_atomic_and(~flags, &mTransactionFlags) & flags;
110513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
110613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
110713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianuint32_t Layer::setTransactionFlags(uint32_t flags) {
110813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    return android_atomic_or(flags, &mTransactionFlags);
110913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
111013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
111182364e3cea0bf88fa8147766433329b3dd5148b8Robert Carrbool Layer::setPosition(float x, float y, bool immediate) {
11123dcabfab7ef80df5884b269fec17350a26da6f51Robert Carr    if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
111313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        return false;
111413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mCurrentState.sequence++;
111569663fb0de2f112f525c4dcd7e2f7e0c879daaa4Robert Carr
111669663fb0de2f112f525c4dcd7e2f7e0c879daaa4Robert Carr    // We update the requested and active position simultaneously because
111769663fb0de2f112f525c4dcd7e2f7e0c879daaa4Robert Carr    // we want to apply the position portion of the transform matrix immediately,
111869663fb0de2f112f525c4dcd7e2f7e0c879daaa4Robert Carr    // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
11193dcabfab7ef80df5884b269fec17350a26da6f51Robert Carr    mCurrentState.requested.transform.set(x, y);
11207bf247e2db180da661b18084d50e0b389686fc0cRobert Carr    if (immediate && !mFreezeGeometryUpdates) {
11217bf247e2db180da661b18084d50e0b389686fc0cRobert Carr        // Here we directly update the active state
11227bf247e2db180da661b18084d50e0b389686fc0cRobert Carr        // unlike other setters, because we store it within
11237bf247e2db180da661b18084d50e0b389686fc0cRobert Carr        // the transform, but use different latching rules.
11247bf247e2db180da661b18084d50e0b389686fc0cRobert Carr        // b/38182305
112582364e3cea0bf88fa8147766433329b3dd5148b8Robert Carr        mCurrentState.active.transform.set(x, y);
112682364e3cea0bf88fa8147766433329b3dd5148b8Robert Carr    }
11277bf247e2db180da661b18084d50e0b389686fc0cRobert Carr    mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
112869663fb0de2f112f525c4dcd7e2f7e0c879daaa4Robert Carr
11297dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza    mCurrentState.modified = true;
113013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    setTransactionFlags(eTransactionNeeded);
113113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    return true;
113213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
113382364e3cea0bf88fa8147766433329b3dd5148b8Robert Carr
11341f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carrbool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
11351f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    ssize_t idx = mCurrentChildren.indexOf(childLayer);
11361f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    if (idx < 0) {
11371f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr        return false;
11381f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    }
11391f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    if (childLayer->setLayer(z)) {
11401f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr        mCurrentChildren.removeAt(idx);
11411f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr        mCurrentChildren.add(childLayer);
1142503d2bd9a0c64f90fc321f9d65fc65e592f1fa2bRobert Carr        return true;
11431f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    }
1144503d2bd9a0c64f90fc321f9d65fc65e592f1fa2bRobert Carr    return false;
11451f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr}
11461f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr
1147503c7046237f2797a5cf8584064223359d0e6d10Robert Carrbool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1148503c7046237f2797a5cf8584064223359d0e6d10Robert Carr        const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1149503c7046237f2797a5cf8584064223359d0e6d10Robert Carr    ssize_t idx = mCurrentChildren.indexOf(childLayer);
1150503c7046237f2797a5cf8584064223359d0e6d10Robert Carr    if (idx < 0) {
1151503c7046237f2797a5cf8584064223359d0e6d10Robert Carr        return false;
1152503c7046237f2797a5cf8584064223359d0e6d10Robert Carr    }
1153503c7046237f2797a5cf8584064223359d0e6d10Robert Carr    if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1154503c7046237f2797a5cf8584064223359d0e6d10Robert Carr        mCurrentChildren.removeAt(idx);
1155503c7046237f2797a5cf8584064223359d0e6d10Robert Carr        mCurrentChildren.add(childLayer);
1156503d2bd9a0c64f90fc321f9d65fc65e592f1fa2bRobert Carr        return true;
1157503c7046237f2797a5cf8584064223359d0e6d10Robert Carr    }
1158503d2bd9a0c64f90fc321f9d65fc65e592f1fa2bRobert Carr    return false;
1159503c7046237f2797a5cf8584064223359d0e6d10Robert Carr}
1160503c7046237f2797a5cf8584064223359d0e6d10Robert Carr
1161ae0608381b2b4699218febd6d45ad9d307544d55Robert Carrbool Layer::setLayer(int32_t z) {
1162503d2bd9a0c64f90fc321f9d65fc65e592f1fa2bRobert Carr    if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
116313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mCurrentState.sequence++;
116413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mCurrentState.z = z;
11657dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza    mCurrentState.modified = true;
1166db66e627ad8904491e384c64f82fc77a939b9705Robert Carr
1167db66e627ad8904491e384c64f82fc77a939b9705Robert Carr    // Discard all relative layering.
1168db66e627ad8904491e384c64f82fc77a939b9705Robert Carr    if (mCurrentState.zOrderRelativeOf != nullptr) {
1169db66e627ad8904491e384c64f82fc77a939b9705Robert Carr        sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1170db66e627ad8904491e384c64f82fc77a939b9705Robert Carr        if (strongRelative != nullptr) {
1171db66e627ad8904491e384c64f82fc77a939b9705Robert Carr            strongRelative->removeZOrderRelative(this);
1172db66e627ad8904491e384c64f82fc77a939b9705Robert Carr        }
1173db66e627ad8904491e384c64f82fc77a939b9705Robert Carr        mCurrentState.zOrderRelativeOf = nullptr;
1174db66e627ad8904491e384c64f82fc77a939b9705Robert Carr    }
117513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    setTransactionFlags(eTransactionNeeded);
117613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    return true;
117713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
11781f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr
1179db66e627ad8904491e384c64f82fc77a939b9705Robert Carrvoid Layer::removeZOrderRelative(const wp<Layer>& relative) {
1180db66e627ad8904491e384c64f82fc77a939b9705Robert Carr    mCurrentState.zOrderRelatives.remove(relative);
1181db66e627ad8904491e384c64f82fc77a939b9705Robert Carr    mCurrentState.sequence++;
1182db66e627ad8904491e384c64f82fc77a939b9705Robert Carr    mCurrentState.modified = true;
1183db66e627ad8904491e384c64f82fc77a939b9705Robert Carr    setTransactionFlags(eTransactionNeeded);
1184db66e627ad8904491e384c64f82fc77a939b9705Robert Carr}
1185db66e627ad8904491e384c64f82fc77a939b9705Robert Carr
1186db66e627ad8904491e384c64f82fc77a939b9705Robert Carrvoid Layer::addZOrderRelative(const wp<Layer>& relative) {
1187db66e627ad8904491e384c64f82fc77a939b9705Robert Carr    mCurrentState.zOrderRelatives.add(relative);
1188db66e627ad8904491e384c64f82fc77a939b9705Robert Carr    mCurrentState.modified = true;
1189db66e627ad8904491e384c64f82fc77a939b9705Robert Carr    mCurrentState.sequence++;
1190db66e627ad8904491e384c64f82fc77a939b9705Robert Carr    setTransactionFlags(eTransactionNeeded);
1191db66e627ad8904491e384c64f82fc77a939b9705Robert Carr}
1192db66e627ad8904491e384c64f82fc77a939b9705Robert Carr
1193503d2bd9a0c64f90fc321f9d65fc65e592f1fa2bRobert Carrbool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1194db66e627ad8904491e384c64f82fc77a939b9705Robert Carr    sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1195db66e627ad8904491e384c64f82fc77a939b9705Robert Carr    if (handle == nullptr) {
1196db66e627ad8904491e384c64f82fc77a939b9705Robert Carr        return false;
1197db66e627ad8904491e384c64f82fc77a939b9705Robert Carr    }
1198db66e627ad8904491e384c64f82fc77a939b9705Robert Carr    sp<Layer> relative = handle->owner.promote();
1199db66e627ad8904491e384c64f82fc77a939b9705Robert Carr    if (relative == nullptr) {
1200db66e627ad8904491e384c64f82fc77a939b9705Robert Carr        return false;
1201db66e627ad8904491e384c64f82fc77a939b9705Robert Carr    }
1202db66e627ad8904491e384c64f82fc77a939b9705Robert Carr
1203503d2bd9a0c64f90fc321f9d65fc65e592f1fa2bRobert Carr    if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1204503d2bd9a0c64f90fc321f9d65fc65e592f1fa2bRobert Carr            mCurrentState.zOrderRelativeOf == relative) {
1205503d2bd9a0c64f90fc321f9d65fc65e592f1fa2bRobert Carr        return false;
1206503d2bd9a0c64f90fc321f9d65fc65e592f1fa2bRobert Carr    }
1207503d2bd9a0c64f90fc321f9d65fc65e592f1fa2bRobert Carr
1208db66e627ad8904491e384c64f82fc77a939b9705Robert Carr    mCurrentState.sequence++;
1209db66e627ad8904491e384c64f82fc77a939b9705Robert Carr    mCurrentState.modified = true;
1210503d2bd9a0c64f90fc321f9d65fc65e592f1fa2bRobert Carr    mCurrentState.z = relativeZ;
1211db66e627ad8904491e384c64f82fc77a939b9705Robert Carr
12129ab4bd13bcd32cbe2f334cccce0a4d8cb22e14e7chaviw    auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
12139ab4bd13bcd32cbe2f334cccce0a4d8cb22e14e7chaviw    if (oldZOrderRelativeOf != nullptr) {
12149ab4bd13bcd32cbe2f334cccce0a4d8cb22e14e7chaviw        oldZOrderRelativeOf->removeZOrderRelative(this);
12159ab4bd13bcd32cbe2f334cccce0a4d8cb22e14e7chaviw    }
1216db66e627ad8904491e384c64f82fc77a939b9705Robert Carr    mCurrentState.zOrderRelativeOf = relative;
1217db66e627ad8904491e384c64f82fc77a939b9705Robert Carr    relative->addZOrderRelative(this);
1218db66e627ad8904491e384c64f82fc77a939b9705Robert Carr
1219db66e627ad8904491e384c64f82fc77a939b9705Robert Carr    setTransactionFlags(eTransactionNeeded);
1220db66e627ad8904491e384c64f82fc77a939b9705Robert Carr
1221db66e627ad8904491e384c64f82fc77a939b9705Robert Carr    return true;
1222db66e627ad8904491e384c64f82fc77a939b9705Robert Carr}
1223db66e627ad8904491e384c64f82fc77a939b9705Robert Carr
122413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianbool Layer::setSize(uint32_t w, uint32_t h) {
122541fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    if (mCurrentState.requested.w == w && mCurrentState.requested.h == h) return false;
122613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mCurrentState.requested.w = w;
122713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mCurrentState.requested.h = h;
12287dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza    mCurrentState.modified = true;
122913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    setTransactionFlags(eTransactionNeeded);
123013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    return true;
123113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
12329e56aa0fdb5f7121b9b975c6c16db103ea4d2fe9Dan Stozabool Layer::setAlpha(float alpha) {
123341fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    if (mCurrentState.color.a == alpha) return false;
123413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mCurrentState.sequence++;
123513fdc49516d17f41e64e62e73c313b0928bf13ccchaviw    mCurrentState.color.a = alpha;
12367dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza    mCurrentState.modified = true;
123713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    setTransactionFlags(eTransactionNeeded);
123813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    return true;
123913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
124013fdc49516d17f41e64e62e73c313b0928bf13ccchaviw
124113fdc49516d17f41e64e62e73c313b0928bf13ccchaviwbool Layer::setColor(const half3& color) {
124241fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
124341fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman        color.b == mCurrentState.color.b)
124413fdc49516d17f41e64e62e73c313b0928bf13ccchaviw        return false;
124513fdc49516d17f41e64e62e73c313b0928bf13ccchaviw
124613fdc49516d17f41e64e62e73c313b0928bf13ccchaviw    mCurrentState.sequence++;
124713fdc49516d17f41e64e62e73c313b0928bf13ccchaviw    mCurrentState.color.r = color.r;
124813fdc49516d17f41e64e62e73c313b0928bf13ccchaviw    mCurrentState.color.g = color.g;
124913fdc49516d17f41e64e62e73c313b0928bf13ccchaviw    mCurrentState.color.b = color.b;
125013fdc49516d17f41e64e62e73c313b0928bf13ccchaviw    mCurrentState.modified = true;
125113fdc49516d17f41e64e62e73c313b0928bf13ccchaviw    setTransactionFlags(eTransactionNeeded);
125213fdc49516d17f41e64e62e73c313b0928bf13ccchaviw    return true;
125313fdc49516d17f41e64e62e73c313b0928bf13ccchaviw}
125413fdc49516d17f41e64e62e73c313b0928bf13ccchaviw
125513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianbool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
125613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mCurrentState.sequence++;
125741fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    mCurrentState.requested.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
12587dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza    mCurrentState.modified = true;
125913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    setTransactionFlags(eTransactionNeeded);
126013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    return true;
126113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
126213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianbool Layer::setTransparentRegionHint(const Region& transparent) {
12632ca79399b933935eb1b6c0ec1f746f8c4475369cMathias Agopian    mCurrentState.requestedTransparentRegion = transparent;
12647dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza    mCurrentState.modified = true;
126513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    setTransactionFlags(eTransactionNeeded);
126613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    return true;
126713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
126813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianbool Layer::setFlags(uint8_t flags, uint8_t mask) {
126913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
127041fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    if (mCurrentState.flags == newFlags) return false;
127113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mCurrentState.sequence++;
127213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mCurrentState.flags = newFlags;
12737dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza    mCurrentState.mask = mask;
12747dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza    mCurrentState.modified = true;
127513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    setTransactionFlags(eTransactionNeeded);
127613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    return true;
127713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
127899e27f0bc236e38d88ff4f9912ede514a729b8ebRobert Carr
127999e27f0bc236e38d88ff4f9912ede514a729b8ebRobert Carrbool Layer::setCrop(const Rect& crop, bool immediate) {
128041fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    if (mCurrentState.requestedCrop == crop) return false;
128113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mCurrentState.sequence++;
128299e27f0bc236e38d88ff4f9912ede514a729b8ebRobert Carr    mCurrentState.requestedCrop = crop;
12837bf247e2db180da661b18084d50e0b389686fc0cRobert Carr    if (immediate && !mFreezeGeometryUpdates) {
128499e27f0bc236e38d88ff4f9912ede514a729b8ebRobert Carr        mCurrentState.crop = crop;
128599e27f0bc236e38d88ff4f9912ede514a729b8ebRobert Carr    }
12867bf247e2db180da661b18084d50e0b389686fc0cRobert Carr    mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
12877bf247e2db180da661b18084d50e0b389686fc0cRobert Carr
12887dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza    mCurrentState.modified = true;
128913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    setTransactionFlags(eTransactionNeeded);
129013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    return true;
129113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
12928d5227b8416b099c884429312daf2d60496fa484Robert Carr
12938d5227b8416b099c884429312daf2d60496fa484Robert Carrbool Layer::setFinalCrop(const Rect& crop, bool immediate) {
129441fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    if (mCurrentState.requestedFinalCrop == crop) return false;
1295acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos    mCurrentState.sequence++;
12968d5227b8416b099c884429312daf2d60496fa484Robert Carr    mCurrentState.requestedFinalCrop = crop;
12977bf247e2db180da661b18084d50e0b389686fc0cRobert Carr    if (immediate && !mFreezeGeometryUpdates) {
12988d5227b8416b099c884429312daf2d60496fa484Robert Carr        mCurrentState.finalCrop = crop;
12998d5227b8416b099c884429312daf2d60496fa484Robert Carr    }
13007bf247e2db180da661b18084d50e0b389686fc0cRobert Carr    mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
13017bf247e2db180da661b18084d50e0b389686fc0cRobert Carr
1302acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos    mCurrentState.modified = true;
1303acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos    setTransactionFlags(eTransactionNeeded);
1304acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos    return true;
1305acbe67888f0bd65d5400400f0115bae6bd6199dcPablo Ceballos}
130613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
1307c3574f7b0141c69fdca25ccafb80ff334462f9a3Robert Carrbool Layer::setOverrideScalingMode(int32_t scalingMode) {
130841fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    if (scalingMode == mOverrideScalingMode) return false;
1309c3574f7b0141c69fdca25ccafb80ff334462f9a3Robert Carr    mOverrideScalingMode = scalingMode;
131082364e3cea0bf88fa8147766433329b3dd5148b8Robert Carr    setTransactionFlags(eTransactionNeeded);
1311c3574f7b0141c69fdca25ccafb80ff334462f9a3Robert Carr    return true;
1312c3574f7b0141c69fdca25ccafb80ff334462f9a3Robert Carr}
1313c3574f7b0141c69fdca25ccafb80ff334462f9a3Robert Carr
1314ccd348460ca25890a8fb709e82e6f780e3ce878erongliuvoid Layer::setInfo(int32_t type, int32_t appId) {
131541fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    mCurrentState.appId = appId;
131641fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    mCurrentState.type = type;
131741fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    mCurrentState.modified = true;
131841fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    setTransactionFlags(eTransactionNeeded);
13192f5f8a51f5994cf14837030d4b3b252a9d1b950bDaniel Nicoara}
13202f5f8a51f5994cf14837030d4b3b252a9d1b950bDaniel Nicoara
132113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianbool Layer::setLayerStack(uint32_t layerStack) {
132241fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    if (mCurrentState.layerStack == layerStack) return false;
132313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mCurrentState.sequence++;
132413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    mCurrentState.layerStack = layerStack;
13257dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza    mCurrentState.modified = true;
132613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    setTransactionFlags(eTransactionNeeded);
132713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    return true;
1328a138f89c5e78b7e8994823e97d6e860869762837Mathias Agopian}
1329a138f89c5e78b7e8994823e97d6e860869762837Mathias Agopian
133034beb7a0ff0494b0c5ad81104171f8a49e599163Peiyong Linbool Layer::setDataSpace(ui::Dataspace dataSpace) {
133141fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    if (mCurrentState.dataSpace == dataSpace) return false;
1332bb09b436fa119ad8b97f1bf5c04cc5aa10b5f1f0Courtney Goeltzenleuchter    mCurrentState.sequence++;
1333bb09b436fa119ad8b97f1bf5c04cc5aa10b5f1f0Courtney Goeltzenleuchter    mCurrentState.dataSpace = dataSpace;
1334bb09b436fa119ad8b97f1bf5c04cc5aa10b5f1f0Courtney Goeltzenleuchter    mCurrentState.modified = true;
1335bb09b436fa119ad8b97f1bf5c04cc5aa10b5f1f0Courtney Goeltzenleuchter    setTransactionFlags(eTransactionNeeded);
1336bb09b436fa119ad8b97f1bf5c04cc5aa10b5f1f0Courtney Goeltzenleuchter    return true;
1337bb09b436fa119ad8b97f1bf5c04cc5aa10b5f1f0Courtney Goeltzenleuchter}
1338bb09b436fa119ad8b97f1bf5c04cc5aa10b5f1f0Courtney Goeltzenleuchter
133934beb7a0ff0494b0c5ad81104171f8a49e599163Peiyong Linui::Dataspace Layer::getDataSpace() const {
1340532b26380fd6d32dc5d7605bd766a2c4f00d0a80Courtney Goeltzenleuchter    return mCurrentState.dataSpace;
1341532b26380fd6d32dc5d7605bd766a2c4f00d0a80Courtney Goeltzenleuchter}
1342532b26380fd6d32dc5d7605bd766a2c4f00d0a80Courtney Goeltzenleuchter
13431f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carruint32_t Layer::getLayerStack() const {
1344e41dbe6ab1d412efd67fdc8344be8da8b19426d2Chia-I Wu    auto p = mDrawingParent.promote();
13451f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    if (p == nullptr) {
13461f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr        return getDrawingState().layerStack;
13471f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    }
13481f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    return p->getLayerStack();
13491f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr}
13501f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr
135141fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodmanvoid Layer::deferTransactionUntil(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
13520d48072f6047140119ff194c1194ce402fca2c0bRobert Carr    mCurrentState.barrierLayer = barrierLayer;
13537dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza    mCurrentState.frameNumber = frameNumber;
13547dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza    // We don't set eTransactionNeeded, because just receiving a deferral
13557dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza    // request without any other state updates shouldn't actually induce a delay
13567dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza    mCurrentState.modified = true;
13577dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza    pushPendingState();
13580d48072f6047140119ff194c1194ce402fca2c0bRobert Carr    mCurrentState.barrierLayer = nullptr;
1359792e529dee5b6cf839b982b4b06a37fbf1d3e28aDan Stoza    mCurrentState.frameNumber = 0;
13607dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza    mCurrentState.modified = false;
13610d48072f6047140119ff194c1194ce402fca2c0bRobert Carr}
13620d48072f6047140119ff194c1194ce402fca2c0bRobert Carr
136341fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodmanvoid Layer::deferTransactionUntil(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
13640d48072f6047140119ff194c1194ce402fca2c0bRobert Carr    sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
13650d48072f6047140119ff194c1194ce402fca2c0bRobert Carr    deferTransactionUntil(handle->owner.promote(), frameNumber);
13667dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza}
13677dde599bf1a0dbef7390d91c2689d506371cdbd7Dan Stoza
1368ee44edd0acccbf5eaa918d75737c3b65ee04fff7Dan Stoza
1369edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project// ----------------------------------------------------------------------------
1370edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project// pageflip handling...
1371edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project// ----------------------------------------------------------------------------
1372edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
13731f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carrbool Layer::isHiddenByPolicy() const {
13741f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    const Layer::State& s(mDrawingState);
1375e41dbe6ab1d412efd67fdc8344be8da8b19426d2Chia-I Wu    const auto& parent = mDrawingParent.promote();
13761f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    if (parent != nullptr && parent->isHiddenByPolicy()) {
13771f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr        return true;
13781f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    }
13791f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    return s.flags & layer_state_t::eLayerHidden;
13801f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr}
13811f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr
138241fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodmanuint32_t Layer::getEffectiveUsage(uint32_t usage) const {
138313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    // TODO: should we do something special if mSecure is set?
138413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    if (mProtectedByApp) {
138513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        // need a hardware-protected path to external video sink
138613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        usage |= GraphicBuffer::USAGE_PROTECTED;
138713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    }
138803414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews    if (mPotentialCursor) {
138903414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews        usage |= GraphicBuffer::USAGE_CURSOR;
139003414a1cfe6c1222fd7723949bd622f9cba145aaRiley Andrews    }
139113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    usage |= GraphicBuffer::USAGE_HW_COMPOSER;
139213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    return usage;
139313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
139413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
139513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopianvoid Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
139613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    uint32_t orientation = 0;
139713127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    if (!mFlinger->mDebugDisableTransformHint) {
139813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        // The transform hint is used to improve performance, but we can
139913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        // only have a single transform hint, it cannot
140013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        // apply to all displays.
140113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        const Transform& planeTransform(hw->getTransform());
140213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        orientation = planeTransform.getOrientation();
140313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        if (orientation & Transform::ROT_INVALID) {
140413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian            orientation = 0;
140513127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian        }
140613127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian    }
1407eb085e0c579f310e2670204b14b742b68c70bc9eDavid Sodman    setTransformHint(orientation);
140813127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian}
140913127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
141013127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// ----------------------------------------------------------------------------
141113127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// debugging
141213127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// ----------------------------------------------------------------------------
141313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
1414a099a24c93bfa599fc5c36a647e946c26f68514fKalle RaitaLayerDebugInfo Layer::getLayerDebugInfo() const {
1415a099a24c93bfa599fc5c36a647e946c26f68514fKalle Raita    LayerDebugInfo info;
1416a099a24c93bfa599fc5c36a647e946c26f68514fKalle Raita    const Layer::State& ds = getDrawingState();
1417a099a24c93bfa599fc5c36a647e946c26f68514fKalle Raita    info.mName = getName();
14181acbec7884a47f316005ba4f2b5e47c74317ee43chaviw    sp<Layer> parent = getParent();
1419a099a24c93bfa599fc5c36a647e946c26f68514fKalle Raita    info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1420a099a24c93bfa599fc5c36a647e946c26f68514fKalle Raita    info.mType = String8(getTypeId());
1421a099a24c93bfa599fc5c36a647e946c26f68514fKalle Raita    info.mTransparentRegion = ds.activeTransparentRegion;
1422a099a24c93bfa599fc5c36a647e946c26f68514fKalle Raita    info.mVisibleRegion = visibleRegion;
1423a099a24c93bfa599fc5c36a647e946c26f68514fKalle Raita    info.mSurfaceDamageRegion = surfaceDamageRegion;
1424a099a24c93bfa599fc5c36a647e946c26f68514fKalle Raita    info.mLayerStack = getLayerStack();
1425a099a24c93bfa599fc5c36a647e946c26f68514fKalle Raita    info.mX = ds.active.transform.tx();
1426a099a24c93bfa599fc5c36a647e946c26f68514fKalle Raita    info.mY = ds.active.transform.ty();
1427a099a24c93bfa599fc5c36a647e946c26f68514fKalle Raita    info.mZ = ds.z;
1428a099a24c93bfa599fc5c36a647e946c26f68514fKalle Raita    info.mWidth = ds.active.w;
1429a099a24c93bfa599fc5c36a647e946c26f68514fKalle Raita    info.mHeight = ds.active.h;
1430a099a24c93bfa599fc5c36a647e946c26f68514fKalle Raita    info.mCrop = ds.crop;
1431a099a24c93bfa599fc5c36a647e946c26f68514fKalle Raita    info.mFinalCrop = ds.finalCrop;
143213fdc49516d17f41e64e62e73c313b0928bf13ccchaviw    info.mColor = ds.color;
1433a099a24c93bfa599fc5c36a647e946c26f68514fKalle Raita    info.mFlags = ds.flags;
1434a099a24c93bfa599fc5c36a647e946c26f68514fKalle Raita    info.mPixelFormat = getPixelFormat();
143534beb7a0ff0494b0c5ad81104171f8a49e599163Peiyong Lin    info.mDataSpace = static_cast<android_dataspace>(getDataSpace());
1436a099a24c93bfa599fc5c36a647e946c26f68514fKalle Raita    info.mMatrix[0][0] = ds.active.transform[0][0];
1437a099a24c93bfa599fc5c36a647e946c26f68514fKalle Raita    info.mMatrix[0][1] = ds.active.transform[0][1];
1438a099a24c93bfa599fc5c36a647e946c26f68514fKalle Raita    info.mMatrix[1][0] = ds.active.transform[1][0];
1439a099a24c93bfa599fc5c36a647e946c26f68514fKalle Raita    info.mMatrix[1][1] = ds.active.transform[1][1];
1440a099a24c93bfa599fc5c36a647e946c26f68514fKalle Raita    {
14410cc69188196037c06127c0bc56c516069dbaa558David Sodman        sp<const GraphicBuffer> buffer = getBE().compositionInfo.mBuffer;
14425b4cffc352f7f5d3711d4b7798a07434a998b2d7David Sodman        if (buffer != 0) {
14435b4cffc352f7f5d3711d4b7798a07434a998b2d7David Sodman            info.mActiveBufferWidth = buffer->getWidth();
14445b4cffc352f7f5d3711d4b7798a07434a998b2d7David Sodman            info.mActiveBufferHeight = buffer->getHeight();
14455b4cffc352f7f5d3711d4b7798a07434a998b2d7David Sodman            info.mActiveBufferStride = buffer->getStride();
14465b4cffc352f7f5d3711d4b7798a07434a998b2d7David Sodman            info.mActiveBufferFormat = buffer->format;
1447a099a24c93bfa599fc5c36a647e946c26f68514fKalle Raita        } else {
1448a099a24c93bfa599fc5c36a647e946c26f68514fKalle Raita            info.mActiveBufferWidth = 0;
1449a099a24c93bfa599fc5c36a647e946c26f68514fKalle Raita            info.mActiveBufferHeight = 0;
1450a099a24c93bfa599fc5c36a647e946c26f68514fKalle Raita            info.mActiveBufferStride = 0;
1451a099a24c93bfa599fc5c36a647e946c26f68514fKalle Raita            info.mActiveBufferFormat = 0;
1452a099a24c93bfa599fc5c36a647e946c26f68514fKalle Raita        }
1453bb641244d7d73312dc65b8e338df18b22e335107Mathias Agopian    }
1454a099a24c93bfa599fc5c36a647e946c26f68514fKalle Raita    info.mNumQueuedFrames = getQueuedFrameCount();
1455a099a24c93bfa599fc5c36a647e946c26f68514fKalle Raita    info.mRefreshPending = isBufferLatched();
1456a099a24c93bfa599fc5c36a647e946c26f68514fKalle Raita    info.mIsOpaque = isOpaque(ds);
1457a099a24c93bfa599fc5c36a647e946c26f68514fKalle Raita    info.mContentDirty = contentDirty;
1458a099a24c93bfa599fc5c36a647e946c26f68514fKalle Raita    return info;
1459d606de6bb6877dc4ab93ec0be0c6bda4a8ee1ce8Mathias Agopian}
146083ce7c162855742a2d9eeebc0cd70fe48d2cd125Chia-I Wu
1461e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stozavoid Layer::miniDumpHeader(String8& result) {
1462e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza    result.append("----------------------------------------");
1463e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza    result.append("---------------------------------------\n");
1464e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza    result.append(" Layer name\n");
1465e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza    result.append("           Z | ");
1466e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza    result.append(" Comp Type | ");
1467e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza    result.append("  Disp Frame (LTRB) | ");
1468e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza    result.append("         Source Crop (LTRB)\n");
1469e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza    result.append("----------------------------------------");
1470e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza    result.append("---------------------------------------\n");
1471e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza}
1472e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza
1473e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stozavoid Layer::miniDump(String8& result, int32_t hwcId) const {
14746f65f3e7caab86a76419e7c1d1c787634a0852d8David Sodman    if (getBE().mHwcLayers.count(hwcId) == 0) {
1475e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza        return;
1476e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza    }
1477e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza
1478e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza    String8 name;
1479e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza    if (mName.length() > 77) {
1480e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza        std::string shortened;
1481e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza        shortened.append(mName.string(), 36);
1482e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza        shortened.append("[...]");
1483e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza        shortened.append(mName.string() + (mName.length() - 36), 36);
1484e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza        name = shortened.c_str();
1485e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza    } else {
1486e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza        name = mName;
1487e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza    }
1488e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza
1489e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza    result.appendFormat(" %s\n", name.string());
1490e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza
1491e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza    const Layer::State& layerState(getDrawingState());
14926f65f3e7caab86a76419e7c1d1c787634a0852d8David Sodman    const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(hwcId);
14931e04361db1b65d3998edbf820078ccfaed477cb6Chia-I Wu    if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
14941e04361db1b65d3998edbf820078ccfaed477cb6Chia-I Wu        result.appendFormat("  rel %6d | ", layerState.z);
14951e04361db1b65d3998edbf820078ccfaed477cb6Chia-I Wu    } else {
14961e04361db1b65d3998edbf820078ccfaed477cb6Chia-I Wu        result.appendFormat("  %10d | ", layerState.z);
14971e04361db1b65d3998edbf820078ccfaed477cb6Chia-I Wu    }
149841fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    result.appendFormat("%10s | ", to_string(getCompositionType(hwcId)).c_str());
1499e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza    const Rect& frame = hwcInfo.displayFrame;
150041fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
15015a423eaa86f4c990afcef8c55e3949d0872068b4Dan Stoza    const FloatRect& crop = hwcInfo.sourceCrop;
150241fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    result.appendFormat("%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right, crop.bottom);
1503e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza
1504e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza    result.append("- - - - - - - - - - - - - - - - - - - - ");
1505e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza    result.append("- - - - - - - - - - - - - - - - - - - -\n");
1506e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza}
1507e22aec741f1530cde86c38989bcc2f87626b93b0Dan Stoza
1508d85084b2b65828442eafaff9b811e9b6c9ca9fadSvetoslavvoid Layer::dumpFrameStats(String8& result) const {
1509d85084b2b65828442eafaff9b811e9b6c9ca9fadSvetoslav    mFrameTracker.dumpStats(result);
151082d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian}
151182d7ab6c7e0cf971e515134ccf072682dd1a2cdbMathias Agopian
1512d85084b2b65828442eafaff9b811e9b6c9ca9fadSvetoslavvoid Layer::clearFrameStats() {
1513d85084b2b65828442eafaff9b811e9b6c9ca9fadSvetoslav    mFrameTracker.clearStats();
151425e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian}
151525e66fc324bbc004fa8902b2d4699e41bb601104Mathias Agopian
15166547ff4327aa320fbc9635668d3fc66db7dd78f6Jamie Gennisvoid Layer::logFrameStats() {
15176547ff4327aa320fbc9635668d3fc66db7dd78f6Jamie Gennis    mFrameTracker.logAndResetStats(mName);
15186547ff4327aa320fbc9635668d3fc66db7dd78f6Jamie Gennis}
15196547ff4327aa320fbc9635668d3fc66db7dd78f6Jamie Gennis
1520d85084b2b65828442eafaff9b811e9b6c9ca9fadSvetoslavvoid Layer::getFrameStats(FrameStats* outStats) const {
1521d85084b2b65828442eafaff9b811e9b6c9ca9fadSvetoslav    mFrameTracker.getStats(outStats);
1522d85084b2b65828442eafaff9b811e9b6c9ca9fadSvetoslav}
1523d85084b2b65828442eafaff9b811e9b6c9ca9fadSvetoslav
1524d6927fb1143398370c0885844bfb58923ef740b7Brian Andersonvoid Layer::dumpFrameEvents(String8& result) {
152541fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
1526d6927fb1143398370c0885844bfb58923ef740b7Brian Anderson    Mutex::Autolock lock(mFrameEventHistoryMutex);
1527d6927fb1143398370c0885844bfb58923ef740b7Brian Anderson    mFrameEventHistory.checkFencesForCompletion();
1528d6927fb1143398370c0885844bfb58923ef740b7Brian Anderson    mFrameEventHistory.dump(result);
1529d6927fb1143398370c0885844bfb58923ef740b7Brian Anderson}
1530d6927fb1143398370c0885844bfb58923ef740b7Brian Anderson
15315ea5e5905170f32d5cf45ad35c552d64743892c3Brian Andersonvoid Layer::onDisconnect() {
15325ea5e5905170f32d5cf45ad35c552d64743892c3Brian Anderson    Mutex::Autolock lock(mFrameEventHistoryMutex);
15335ea5e5905170f32d5cf45ad35c552d64743892c3Brian Anderson    mFrameEventHistory.onDisconnect();
15345ea5e5905170f32d5cf45ad35c552d64743892c3Brian Anderson}
15355ea5e5905170f32d5cf45ad35c552d64743892c3Brian Anderson
15363890c3995c4a52439844faeb80b5503d42b977d8Brian Andersonvoid Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
153741fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman                                     FrameEventHistoryDelta* outDelta) {
1538d6927fb1143398370c0885844bfb58923ef740b7Brian Anderson    Mutex::Autolock lock(mFrameEventHistoryMutex);
1539d6927fb1143398370c0885844bfb58923ef740b7Brian Anderson    if (newTimestamps) {
1540fbc80aef0ba1b11982cf4ca88d218b65b6eca0f3Brian Anderson        // If there are any unsignaled fences in the aquire timeline at this
1541fbc80aef0ba1b11982cf4ca88d218b65b6eca0f3Brian Anderson        // point, the previously queued frame hasn't been latched yet. Go ahead
1542fbc80aef0ba1b11982cf4ca88d218b65b6eca0f3Brian Anderson        // and try to get the signal time here so the syscall is taken out of
1543fbc80aef0ba1b11982cf4ca88d218b65b6eca0f3Brian Anderson        // the main thread's critical path.
1544fbc80aef0ba1b11982cf4ca88d218b65b6eca0f3Brian Anderson        mAcquireTimeline.updateSignalTimes();
1545fbc80aef0ba1b11982cf4ca88d218b65b6eca0f3Brian Anderson        // Push the new fence after updating since it's likely still pending.
15463d4039d7a291cd9b6f2dd4b46fcdb576f2db3356Brian Anderson        mAcquireTimeline.push(newTimestamps->acquireFence);
1547d6927fb1143398370c0885844bfb58923ef740b7Brian Anderson        mFrameEventHistory.addQueue(*newTimestamps);
1548d6927fb1143398370c0885844bfb58923ef740b7Brian Anderson    }
154940845df1285b387bcbf8f43ac72228eee2606d80Pablo Ceballos
15503890c3995c4a52439844faeb80b5503d42b977d8Brian Anderson    if (outDelta) {
15513890c3995c4a52439844faeb80b5503d42b977d8Brian Anderson        mFrameEventHistory.getAndResetDelta(outDelta);
1552d6927fb1143398370c0885844bfb58923ef740b7Brian Anderson    }
155340845df1285b387bcbf8f43ac72228eee2606d80Pablo Ceballos}
1554e77c7669bee30b7c0099172cf0c38cef92412040Dan Stoza
155598f1c108b016dda77c808f3d099e7a45bdd70768Chia-I Wusize_t Layer::getChildrenCount() const {
155698f1c108b016dda77c808f3d099e7a45bdd70768Chia-I Wu    size_t count = 0;
155798f1c108b016dda77c808f3d099e7a45bdd70768Chia-I Wu    for (const sp<Layer>& child : mCurrentChildren) {
155898f1c108b016dda77c808f3d099e7a45bdd70768Chia-I Wu        count += 1 + child->getChildrenCount();
155998f1c108b016dda77c808f3d099e7a45bdd70768Chia-I Wu    }
156098f1c108b016dda77c808f3d099e7a45bdd70768Chia-I Wu    return count;
156198f1c108b016dda77c808f3d099e7a45bdd70768Chia-I Wu}
156298f1c108b016dda77c808f3d099e7a45bdd70768Chia-I Wu
15631f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carrvoid Layer::addChild(const sp<Layer>& layer) {
15641f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    mCurrentChildren.add(layer);
15651f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    layer->setParent(this);
15661f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr}
15671f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr
15681f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carrssize_t Layer::removeChild(const sp<Layer>& layer) {
15691f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    layer->setParent(nullptr);
15701f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    return mCurrentChildren.remove(layer);
15711f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr}
15721f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr
15731db73f66624e7d151710483dd58e03eed672f064Robert Carrbool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
15741db73f66624e7d151710483dd58e03eed672f064Robert Carr    sp<Handle> handle = nullptr;
15751db73f66624e7d151710483dd58e03eed672f064Robert Carr    sp<Layer> newParent = nullptr;
15761db73f66624e7d151710483dd58e03eed672f064Robert Carr    if (newParentHandle == nullptr) {
15771db73f66624e7d151710483dd58e03eed672f064Robert Carr        return false;
15781db73f66624e7d151710483dd58e03eed672f064Robert Carr    }
15791db73f66624e7d151710483dd58e03eed672f064Robert Carr    handle = static_cast<Handle*>(newParentHandle.get());
15801db73f66624e7d151710483dd58e03eed672f064Robert Carr    newParent = handle->owner.promote();
15811db73f66624e7d151710483dd58e03eed672f064Robert Carr    if (newParent == nullptr) {
15821db73f66624e7d151710483dd58e03eed672f064Robert Carr        ALOGE("Unable to promote Layer handle");
15831db73f66624e7d151710483dd58e03eed672f064Robert Carr        return false;
15841db73f66624e7d151710483dd58e03eed672f064Robert Carr    }
15851db73f66624e7d151710483dd58e03eed672f064Robert Carr
15861db73f66624e7d151710483dd58e03eed672f064Robert Carr    for (const sp<Layer>& child : mCurrentChildren) {
1587e41dbe6ab1d412efd67fdc8344be8da8b19426d2Chia-I Wu        newParent->addChild(child);
15881db73f66624e7d151710483dd58e03eed672f064Robert Carr
15891db73f66624e7d151710483dd58e03eed672f064Robert Carr        sp<Client> client(child->mClientRef.promote());
15901db73f66624e7d151710483dd58e03eed672f064Robert Carr        if (client != nullptr) {
15911db73f66624e7d151710483dd58e03eed672f064Robert Carr            client->setParentLayer(newParent);
15921db73f66624e7d151710483dd58e03eed672f064Robert Carr        }
15931db73f66624e7d151710483dd58e03eed672f064Robert Carr    }
15941db73f66624e7d151710483dd58e03eed672f064Robert Carr    mCurrentChildren.clear();
15951db73f66624e7d151710483dd58e03eed672f064Robert Carr
15961db73f66624e7d151710483dd58e03eed672f064Robert Carr    return true;
15971db73f66624e7d151710483dd58e03eed672f064Robert Carr}
15981db73f66624e7d151710483dd58e03eed672f064Robert Carr
159915eae09ebb293135dedce203bcb542a2e6e8d43aRobert Carrvoid Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
1600578038fc49f83c4c8c4accdce49df404ecd6ad02Robert Carr    for (const sp<Layer>& child : mDrawingChildren) {
1601578038fc49f83c4c8c4accdce49df404ecd6ad02Robert Carr        child->mDrawingParent = newParent;
1602578038fc49f83c4c8c4accdce49df404ecd6ad02Robert Carr    }
1603578038fc49f83c4c8c4accdce49df404ecd6ad02Robert Carr}
1604578038fc49f83c4c8c4accdce49df404ecd6ad02Robert Carr
1605f1961f713de2b3f54c8ce7653964b969e1a02bc8chaviwbool Layer::reparent(const sp<IBinder>& newParentHandle) {
1606f1961f713de2b3f54c8ce7653964b969e1a02bc8chaviw    if (newParentHandle == nullptr) {
16070617894190ea0c3ee50889bee1d4df0f369b0761chaviw        return false;
16080617894190ea0c3ee50889bee1d4df0f369b0761chaviw    }
16090617894190ea0c3ee50889bee1d4df0f369b0761chaviw
16100617894190ea0c3ee50889bee1d4df0f369b0761chaviw    auto handle = static_cast<Handle*>(newParentHandle.get());
16110617894190ea0c3ee50889bee1d4df0f369b0761chaviw    sp<Layer> newParent = handle->owner.promote();
16120617894190ea0c3ee50889bee1d4df0f369b0761chaviw    if (newParent == nullptr) {
16130617894190ea0c3ee50889bee1d4df0f369b0761chaviw        ALOGE("Unable to promote Layer handle");
16140617894190ea0c3ee50889bee1d4df0f369b0761chaviw        return false;
16150617894190ea0c3ee50889bee1d4df0f369b0761chaviw    }
16160617894190ea0c3ee50889bee1d4df0f369b0761chaviw
1617f1961f713de2b3f54c8ce7653964b969e1a02bc8chaviw    sp<Layer> parent = getParent();
1618f1961f713de2b3f54c8ce7653964b969e1a02bc8chaviw    if (parent != nullptr) {
1619f1961f713de2b3f54c8ce7653964b969e1a02bc8chaviw        parent->removeChild(this);
16200617894190ea0c3ee50889bee1d4df0f369b0761chaviw    }
1621f1961f713de2b3f54c8ce7653964b969e1a02bc8chaviw    newParent->addChild(this);
16220617894190ea0c3ee50889bee1d4df0f369b0761chaviw
1623f1961f713de2b3f54c8ce7653964b969e1a02bc8chaviw    sp<Client> client(mClientRef.promote());
16240617894190ea0c3ee50889bee1d4df0f369b0761chaviw    sp<Client> newParentClient(newParent->mClientRef.promote());
16250617894190ea0c3ee50889bee1d4df0f369b0761chaviw
1626f1961f713de2b3f54c8ce7653964b969e1a02bc8chaviw    if (client != newParentClient) {
1627f1961f713de2b3f54c8ce7653964b969e1a02bc8chaviw        client->setParentLayer(newParent);
16280617894190ea0c3ee50889bee1d4df0f369b0761chaviw    }
16290617894190ea0c3ee50889bee1d4df0f369b0761chaviw
16300617894190ea0c3ee50889bee1d4df0f369b0761chaviw    return true;
16310617894190ea0c3ee50889bee1d4df0f369b0761chaviw}
16320617894190ea0c3ee50889bee1d4df0f369b0761chaviw
16339524cb3b37a91b5741790c77ff24fd825b02bca7Robert Carrbool Layer::detachChildren() {
16347f619b2350d46eede639efa13d7ac3697551b2ccRobert Carr    for (const sp<Layer>& child : mCurrentChildren) {
1635161410b01a37bcc5522d8e91fe0a743989c86e70chaviw        sp<Client> parentClient = mClientRef.promote();
16369524cb3b37a91b5741790c77ff24fd825b02bca7Robert Carr        sp<Client> client(child->mClientRef.promote());
1637161410b01a37bcc5522d8e91fe0a743989c86e70chaviw        if (client != nullptr && parentClient != client) {
16387f619b2350d46eede639efa13d7ac3697551b2ccRobert Carr            client->detachLayer(child.get());
16397f619b2350d46eede639efa13d7ac3697551b2ccRobert Carr            child->detachChildren();
16409524cb3b37a91b5741790c77ff24fd825b02bca7Robert Carr        }
16417f619b2350d46eede639efa13d7ac3697551b2ccRobert Carr    }
16429524cb3b37a91b5741790c77ff24fd825b02bca7Robert Carr
16439524cb3b37a91b5741790c77ff24fd825b02bca7Robert Carr    return true;
16449524cb3b37a91b5741790c77ff24fd825b02bca7Robert Carr}
16459524cb3b37a91b5741790c77ff24fd825b02bca7Robert Carr
16461f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carrvoid Layer::setParent(const sp<Layer>& layer) {
1647e41dbe6ab1d412efd67fdc8344be8da8b19426d2Chia-I Wu    mCurrentParent = layer;
16481f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr}
16491f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr
16501f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carrvoid Layer::clearSyncPoints() {
16511f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    for (const auto& child : mCurrentChildren) {
16521f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr        child->clearSyncPoints();
16531f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    }
16541f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr
16551f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    Mutex::Autolock lock(mLocalSyncPointMutex);
16561f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    for (auto& point : mLocalSyncPoints) {
16571f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr        point->setFrameAvailable();
16581f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    }
16591f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    mLocalSyncPoints.clear();
16601f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr}
16611f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr
16621f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carrint32_t Layer::getZ() const {
16631f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    return mDrawingState.z;
16641f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr}
16651f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr
166629abff83bb6e148961c2319add7bd72eb76af3d4Robert Carrbool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
166729abff83bb6e148961c2319add7bd72eb76af3d4Robert Carr    const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
166829abff83bb6e148961c2319add7bd72eb76af3d4Robert Carr    const State& state = useDrawing ? mDrawingState : mCurrentState;
166929abff83bb6e148961c2319add7bd72eb76af3d4Robert Carr    return state.zOrderRelativeOf != nullptr;
167029abff83bb6e148961c2319add7bd72eb76af3d4Robert Carr}
167129abff83bb6e148961c2319add7bd72eb76af3d4Robert Carr
167241fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
167329abff83bb6e148961c2319add7bd72eb76af3d4Robert Carr        LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
1674412903fce3a93f411c85c54375a1851bfb370400Dan Stoza    LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1675412903fce3a93f411c85c54375a1851bfb370400Dan Stoza                        "makeTraversalList received invalid stateSet");
1676412903fce3a93f411c85c54375a1851bfb370400Dan Stoza    const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1677412903fce3a93f411c85c54375a1851bfb370400Dan Stoza    const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1678412903fce3a93f411c85c54375a1851bfb370400Dan Stoza    const State& state = useDrawing ? mDrawingState : mCurrentState;
1679412903fce3a93f411c85c54375a1851bfb370400Dan Stoza
168029abff83bb6e148961c2319add7bd72eb76af3d4Robert Carr    if (state.zOrderRelatives.size() == 0) {
168129abff83bb6e148961c2319add7bd72eb76af3d4Robert Carr        *outSkipRelativeZUsers = true;
168229abff83bb6e148961c2319add7bd72eb76af3d4Robert Carr        return children;
168329abff83bb6e148961c2319add7bd72eb76af3d4Robert Carr    }
168429abff83bb6e148961c2319add7bd72eb76af3d4Robert Carr
1685db66e627ad8904491e384c64f82fc77a939b9705Robert Carr    LayerVector traverse;
1686412903fce3a93f411c85c54375a1851bfb370400Dan Stoza    for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1687db66e627ad8904491e384c64f82fc77a939b9705Robert Carr        sp<Layer> strongRelative = weakRelative.promote();
1688db66e627ad8904491e384c64f82fc77a939b9705Robert Carr        if (strongRelative != nullptr) {
1689db66e627ad8904491e384c64f82fc77a939b9705Robert Carr            traverse.add(strongRelative);
1690db66e627ad8904491e384c64f82fc77a939b9705Robert Carr        }
1691db66e627ad8904491e384c64f82fc77a939b9705Robert Carr    }
1692db66e627ad8904491e384c64f82fc77a939b9705Robert Carr
1693412903fce3a93f411c85c54375a1851bfb370400Dan Stoza    for (const sp<Layer>& child : children) {
1694503c7046237f2797a5cf8584064223359d0e6d10Robert Carr        const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1695503c7046237f2797a5cf8584064223359d0e6d10Robert Carr        if (childState.zOrderRelativeOf != nullptr) {
1696503c7046237f2797a5cf8584064223359d0e6d10Robert Carr            continue;
1697503c7046237f2797a5cf8584064223359d0e6d10Robert Carr        }
1698db66e627ad8904491e384c64f82fc77a939b9705Robert Carr        traverse.add(child);
1699db66e627ad8904491e384c64f82fc77a939b9705Robert Carr    }
1700db66e627ad8904491e384c64f82fc77a939b9705Robert Carr
1701db66e627ad8904491e384c64f82fc77a939b9705Robert Carr    return traverse;
1702db66e627ad8904491e384c64f82fc77a939b9705Robert Carr}
1703db66e627ad8904491e384c64f82fc77a939b9705Robert Carr
17041f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr/**
1705db66e627ad8904491e384c64f82fc77a939b9705Robert Carr * Negatively signed relatives are before 'this' in Z-order.
17061f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr */
1707412903fce3a93f411c85c54375a1851bfb370400Dan Stozavoid Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
170829abff83bb6e148961c2319add7bd72eb76af3d4Robert Carr    // In the case we have other layers who are using a relative Z to us, makeTraversalList will
170929abff83bb6e148961c2319add7bd72eb76af3d4Robert Carr    // produce a new list for traversing, including our relatives, and not including our children
171029abff83bb6e148961c2319add7bd72eb76af3d4Robert Carr    // who are relatives of another surface. In the case that there are no relative Z,
171129abff83bb6e148961c2319add7bd72eb76af3d4Robert Carr    // makeTraversalList returns our children directly to avoid significant overhead.
171229abff83bb6e148961c2319add7bd72eb76af3d4Robert Carr    // However in this case we need to take the responsibility for filtering children which
171329abff83bb6e148961c2319add7bd72eb76af3d4Robert Carr    // are relatives of another surface here.
171429abff83bb6e148961c2319add7bd72eb76af3d4Robert Carr    bool skipRelativeZUsers = false;
171529abff83bb6e148961c2319add7bd72eb76af3d4Robert Carr    const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
1716db66e627ad8904491e384c64f82fc77a939b9705Robert Carr
17171f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    size_t i = 0;
1718db66e627ad8904491e384c64f82fc77a939b9705Robert Carr    for (; i < list.size(); i++) {
1719db66e627ad8904491e384c64f82fc77a939b9705Robert Carr        const auto& relative = list[i];
172029abff83bb6e148961c2319add7bd72eb76af3d4Robert Carr        if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
172129abff83bb6e148961c2319add7bd72eb76af3d4Robert Carr            continue;
172229abff83bb6e148961c2319add7bd72eb76af3d4Robert Carr        }
172329abff83bb6e148961c2319add7bd72eb76af3d4Robert Carr
1724db66e627ad8904491e384c64f82fc77a939b9705Robert Carr        if (relative->getZ() >= 0) {
17251f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr            break;
1726db66e627ad8904491e384c64f82fc77a939b9705Robert Carr        }
1727412903fce3a93f411c85c54375a1851bfb370400Dan Stoza        relative->traverseInZOrder(stateSet, visitor);
17281f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    }
172929abff83bb6e148961c2319add7bd72eb76af3d4Robert Carr
1730412903fce3a93f411c85c54375a1851bfb370400Dan Stoza    visitor(this);
1731db66e627ad8904491e384c64f82fc77a939b9705Robert Carr    for (; i < list.size(); i++) {
1732db66e627ad8904491e384c64f82fc77a939b9705Robert Carr        const auto& relative = list[i];
173329abff83bb6e148961c2319add7bd72eb76af3d4Robert Carr
173429abff83bb6e148961c2319add7bd72eb76af3d4Robert Carr        if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
173529abff83bb6e148961c2319add7bd72eb76af3d4Robert Carr            continue;
173629abff83bb6e148961c2319add7bd72eb76af3d4Robert Carr        }
1737412903fce3a93f411c85c54375a1851bfb370400Dan Stoza        relative->traverseInZOrder(stateSet, visitor);
17381f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    }
17391f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr}
17401f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr
17411f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr/**
1742db66e627ad8904491e384c64f82fc77a939b9705Robert Carr * Positively signed relatives are before 'this' in reverse Z-order.
17431f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr */
1744412903fce3a93f411c85c54375a1851bfb370400Dan Stozavoid Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1745412903fce3a93f411c85c54375a1851bfb370400Dan Stoza                                    const LayerVector::Visitor& visitor) {
174629abff83bb6e148961c2319add7bd72eb76af3d4Robert Carr    // See traverseInZOrder for documentation.
174729abff83bb6e148961c2319add7bd72eb76af3d4Robert Carr    bool skipRelativeZUsers = false;
174829abff83bb6e148961c2319add7bd72eb76af3d4Robert Carr    LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
1749db66e627ad8904491e384c64f82fc77a939b9705Robert Carr
17501f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    int32_t i = 0;
1751bf32499a90b40e102c59d588f2ee34d9a67e5664Joel Galenson    for (i = int32_t(list.size()) - 1; i >= 0; i--) {
1752db66e627ad8904491e384c64f82fc77a939b9705Robert Carr        const auto& relative = list[i];
175329abff83bb6e148961c2319add7bd72eb76af3d4Robert Carr
175429abff83bb6e148961c2319add7bd72eb76af3d4Robert Carr        if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
175529abff83bb6e148961c2319add7bd72eb76af3d4Robert Carr            continue;
175629abff83bb6e148961c2319add7bd72eb76af3d4Robert Carr        }
175729abff83bb6e148961c2319add7bd72eb76af3d4Robert Carr
1758db66e627ad8904491e384c64f82fc77a939b9705Robert Carr        if (relative->getZ() < 0) {
17591f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr            break;
17601f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr        }
1761412903fce3a93f411c85c54375a1851bfb370400Dan Stoza        relative->traverseInReverseZOrder(stateSet, visitor);
17621f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    }
1763412903fce3a93f411c85c54375a1851bfb370400Dan Stoza    visitor(this);
176441fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman    for (; i >= 0; i--) {
1765db66e627ad8904491e384c64f82fc77a939b9705Robert Carr        const auto& relative = list[i];
176629abff83bb6e148961c2319add7bd72eb76af3d4Robert Carr
176729abff83bb6e148961c2319add7bd72eb76af3d4Robert Carr        if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
176829abff83bb6e148961c2319add7bd72eb76af3d4Robert Carr            continue;
176929abff83bb6e148961c2319add7bd72eb76af3d4Robert Carr        }
177029abff83bb6e148961c2319add7bd72eb76af3d4Robert Carr
1771412903fce3a93f411c85c54375a1851bfb370400Dan Stoza        relative->traverseInReverseZOrder(stateSet, visitor);
17721f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    }
17731f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr}
17741f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr
1775a76b271f0e14325fa0ebb98e1cac0a15adfea1cbchaviw/**
1776a76b271f0e14325fa0ebb98e1cac0a15adfea1cbchaviw * Traverse only children in z order, ignoring relative layers.
1777a76b271f0e14325fa0ebb98e1cac0a15adfea1cbchaviw */
1778a76b271f0e14325fa0ebb98e1cac0a15adfea1cbchaviwvoid Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1779a76b271f0e14325fa0ebb98e1cac0a15adfea1cbchaviw                                     const LayerVector::Visitor& visitor) {
1780a76b271f0e14325fa0ebb98e1cac0a15adfea1cbchaviw    const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1781a76b271f0e14325fa0ebb98e1cac0a15adfea1cbchaviw    const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1782a76b271f0e14325fa0ebb98e1cac0a15adfea1cbchaviw
1783a76b271f0e14325fa0ebb98e1cac0a15adfea1cbchaviw    size_t i = 0;
1784a76b271f0e14325fa0ebb98e1cac0a15adfea1cbchaviw    for (; i < children.size(); i++) {
1785a76b271f0e14325fa0ebb98e1cac0a15adfea1cbchaviw        const auto& relative = children[i];
1786a76b271f0e14325fa0ebb98e1cac0a15adfea1cbchaviw        if (relative->getZ() >= 0) {
1787a76b271f0e14325fa0ebb98e1cac0a15adfea1cbchaviw            break;
1788a76b271f0e14325fa0ebb98e1cac0a15adfea1cbchaviw        }
1789a76b271f0e14325fa0ebb98e1cac0a15adfea1cbchaviw        relative->traverseChildrenInZOrder(stateSet, visitor);
1790a76b271f0e14325fa0ebb98e1cac0a15adfea1cbchaviw    }
1791a76b271f0e14325fa0ebb98e1cac0a15adfea1cbchaviw    visitor(this);
1792a76b271f0e14325fa0ebb98e1cac0a15adfea1cbchaviw    for (; i < children.size(); i++) {
1793a76b271f0e14325fa0ebb98e1cac0a15adfea1cbchaviw        const auto& relative = children[i];
1794a76b271f0e14325fa0ebb98e1cac0a15adfea1cbchaviw        relative->traverseChildrenInZOrder(stateSet, visitor);
1795a76b271f0e14325fa0ebb98e1cac0a15adfea1cbchaviw    }
1796a76b271f0e14325fa0ebb98e1cac0a15adfea1cbchaviw}
1797a76b271f0e14325fa0ebb98e1cac0a15adfea1cbchaviw
17981f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert CarrTransform Layer::getTransform() const {
17991f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    Transform t;
1800e41dbe6ab1d412efd67fdc8344be8da8b19426d2Chia-I Wu    const auto& p = mDrawingParent.promote();
18011f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    if (p != nullptr) {
18021f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr        t = p->getTransform();
18039b429f41cbd5964f96f9fb746af3b6932e4acc66Robert Carr
18049b429f41cbd5964f96f9fb746af3b6932e4acc66Robert Carr        // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
18059b429f41cbd5964f96f9fb746af3b6932e4acc66Robert Carr        // it isFixedSize) then there may be additional scaling not accounted
18069b429f41cbd5964f96f9fb746af3b6932e4acc66Robert Carr        // for in the transform. We need to mirror this scaling in child surfaces
18079b429f41cbd5964f96f9fb746af3b6932e4acc66Robert Carr        // or we will break the contract where WM can treat child surfaces as
18089b429f41cbd5964f96f9fb746af3b6932e4acc66Robert Carr        // pixels in the parent surface.
18090cc69188196037c06127c0bc56c516069dbaa558David Sodman        if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
18101725eeeb7db8a2c933dcc9d01cce14e17c76c6e6Robert Carr            int bufferWidth;
18111725eeeb7db8a2c933dcc9d01cce14e17c76c6e6Robert Carr            int bufferHeight;
18121725eeeb7db8a2c933dcc9d01cce14e17c76c6e6Robert Carr            if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
18130cc69188196037c06127c0bc56c516069dbaa558David Sodman                bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
18140cc69188196037c06127c0bc56c516069dbaa558David Sodman                bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
18151725eeeb7db8a2c933dcc9d01cce14e17c76c6e6Robert Carr            } else {
18160cc69188196037c06127c0bc56c516069dbaa558David Sodman                bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
18170cc69188196037c06127c0bc56c516069dbaa558David Sodman                bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
18181725eeeb7db8a2c933dcc9d01cce14e17c76c6e6Robert Carr            }
181941fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman            float sx = p->getDrawingState().active.w / static_cast<float>(bufferWidth);
182041fdfc920e2a479add53a1936c3ae76cdbea41dbDavid Sodman            float sy = p->getDrawingState().active.h / static_cast<float>(bufferHeight);
18219b429f41cbd5964f96f9fb746af3b6932e4acc66Robert Carr            Transform extraParentScaling;
18229b429f41cbd5964f96f9fb746af3b6932e4acc66Robert Carr            extraParentScaling.set(sx, 0, 0, sy);
18239b429f41cbd5964f96f9fb746af3b6932e4acc66Robert Carr            t = t * extraParentScaling;
18249b429f41cbd5964f96f9fb746af3b6932e4acc66Robert Carr        }
18251f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    }
18261f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    return t * getDrawingState().active.transform;
18271f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr}
18281f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr
182913fdc49516d17f41e64e62e73c313b0928bf13ccchaviwhalf Layer::getAlpha() const {
1830e41dbe6ab1d412efd67fdc8344be8da8b19426d2Chia-I Wu    const auto& p = mDrawingParent.promote();
18316452f12c95e855a96ab8f690f28914cd37de762bRobert Carr
183213fdc49516d17f41e64e62e73c313b0928bf13ccchaviw    half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
183313fdc49516d17f41e64e62e73c313b0928bf13ccchaviw    return parentAlpha * getDrawingState().color.a;
18346452f12c95e855a96ab8f690f28914cd37de762bRobert Carr}
18356452f12c95e855a96ab8f690f28914cd37de762bRobert Carr
183613fdc49516d17f41e64e62e73c313b0928bf13ccchaviwhalf4 Layer::getColor() const {
183713fdc49516d17f41e64e62e73c313b0928bf13ccchaviw    const half4 color(getDrawingState().color);
183813fdc49516d17f41e64e62e73c313b0928bf13ccchaviw    return half4(color.r, color.g, color.b, getAlpha());
18396452f12c95e855a96ab8f690f28914cd37de762bRobert Carr}
18406452f12c95e855a96ab8f690f28914cd37de762bRobert Carr
18411f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carrvoid Layer::commitChildList() {
18421f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    for (size_t i = 0; i < mCurrentChildren.size(); i++) {
18431f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr        const auto& child = mCurrentChildren[i];
18441f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr        child->commitChildList();
18451f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    }
18461f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr    mDrawingChildren = mCurrentChildren;
1847e41dbe6ab1d412efd67fdc8344be8da8b19426d2Chia-I Wu    mDrawingParent = mCurrentParent;
18481f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr}
18491f0a16a5d7cd00ba7fda82e7d315afa1fd1303b9Robert Carr
18501d04428c3cff3503212ec3e76775ca5ba20abc18chaviwvoid Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
18511d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
18521d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
18531d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    const State& state = useDrawing ? mDrawingState : mCurrentState;
18541d04428c3cff3503212ec3e76775ca5ba20abc18chaviw
18551d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    Transform requestedTransform = state.active.transform;
18561d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    Transform transform = getTransform();
18571d04428c3cff3503212ec3e76775ca5ba20abc18chaviw
18581d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    layerInfo->set_id(sequence);
18591d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    layerInfo->set_name(getName().c_str());
18601d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    layerInfo->set_type(String8(getTypeId()));
18611d04428c3cff3503212ec3e76775ca5ba20abc18chaviw
18621d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    for (const auto& child : children) {
18631d04428c3cff3503212ec3e76775ca5ba20abc18chaviw        layerInfo->add_children(child->sequence);
18641d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    }
18651d04428c3cff3503212ec3e76775ca5ba20abc18chaviw
18661d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
18671d04428c3cff3503212ec3e76775ca5ba20abc18chaviw        sp<Layer> strongRelative = weakRelative.promote();
18681d04428c3cff3503212ec3e76775ca5ba20abc18chaviw        if (strongRelative != nullptr) {
18691d04428c3cff3503212ec3e76775ca5ba20abc18chaviw            layerInfo->add_relatives(strongRelative->sequence);
18701d04428c3cff3503212ec3e76775ca5ba20abc18chaviw        }
18711d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    }
18721d04428c3cff3503212ec3e76775ca5ba20abc18chaviw
18731d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    LayerProtoHelper::writeToProto(state.activeTransparentRegion,
18741d04428c3cff3503212ec3e76775ca5ba20abc18chaviw                                   layerInfo->mutable_transparent_region());
18751d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
18761d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
18771d04428c3cff3503212ec3e76775ca5ba20abc18chaviw
18781d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    layerInfo->set_layer_stack(getLayerStack());
18791d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    layerInfo->set_z(state.z);
18801d04428c3cff3503212ec3e76775ca5ba20abc18chaviw
18811d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    PositionProto* position = layerInfo->mutable_position();
18821d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    position->set_x(transform.tx());
18831d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    position->set_y(transform.ty());
18841d04428c3cff3503212ec3e76775ca5ba20abc18chaviw
18851d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    PositionProto* requestedPosition = layerInfo->mutable_requested_position();
18861d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    requestedPosition->set_x(requestedTransform.tx());
18871d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    requestedPosition->set_y(requestedTransform.ty());
18881d04428c3cff3503212ec3e76775ca5ba20abc18chaviw
18891d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    SizeProto* size = layerInfo->mutable_size();
18901d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    size->set_w(state.active.w);
18911d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    size->set_h(state.active.h);
18921d04428c3cff3503212ec3e76775ca5ba20abc18chaviw
18931d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    LayerProtoHelper::writeToProto(state.crop, layerInfo->mutable_crop());
18941d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    LayerProtoHelper::writeToProto(state.finalCrop, layerInfo->mutable_final_crop());
18951d04428c3cff3503212ec3e76775ca5ba20abc18chaviw
18961d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    layerInfo->set_is_opaque(isOpaque(state));
18971d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    layerInfo->set_invalidate(contentDirty);
189834beb7a0ff0494b0c5ad81104171f8a49e599163Peiyong Lin    layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(getDataSpace())));
18991d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
19001d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
19011d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
19021d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    layerInfo->set_flags(state.flags);
19031d04428c3cff3503212ec3e76775ca5ba20abc18chaviw
19041d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
19051d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
19061d04428c3cff3503212ec3e76775ca5ba20abc18chaviw
19078e0af3679ec73e07775142825d592448b255f61cJorim Jaggi    auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
19081d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    if (parent != nullptr) {
19091d04428c3cff3503212ec3e76775ca5ba20abc18chaviw        layerInfo->set_parent(parent->sequence);
19101d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    }
19111d04428c3cff3503212ec3e76775ca5ba20abc18chaviw
19121d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
19131d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    if (zOrderRelativeOf != nullptr) {
19141d04428c3cff3503212ec3e76775ca5ba20abc18chaviw        layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
19151d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    }
19161d04428c3cff3503212ec3e76775ca5ba20abc18chaviw
19170cc69188196037c06127c0bc56c516069dbaa558David Sodman    auto buffer = getBE().compositionInfo.mBuffer;
19185b4cffc352f7f5d3711d4b7798a07434a998b2d7David Sodman    if (buffer != nullptr) {
19195b4cffc352f7f5d3711d4b7798a07434a998b2d7David Sodman        LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
19201d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    }
19211d04428c3cff3503212ec3e76775ca5ba20abc18chaviw
19221d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    layerInfo->set_queued_frames(getQueuedFrameCount());
19231d04428c3cff3503212ec3e76775ca5ba20abc18chaviw    layerInfo->set_refresh_pending(isBufferLatched());
1924ccd348460ca25890a8fb709e82e6f780e3ce878erongliu    layerInfo->set_window_type(state.type);
1925ccd348460ca25890a8fb709e82e6f780e3ce878erongliu    layerInfo->set_app_id(state.appId);
19261d04428c3cff3503212ec3e76775ca5ba20abc18chaviw}
19271d04428c3cff3503212ec3e76775ca5ba20abc18chaviw
1928068e31b929b40a1bc9be742c04cbdf5b04f3ce97Yiwei Zhangvoid Layer::writeToProto(LayerProto* layerInfo, int32_t hwcId) {
1929068e31b929b40a1bc9be742c04cbdf5b04f3ce97Yiwei Zhang    writeToProto(layerInfo, LayerVector::StateSet::Drawing);
1930068e31b929b40a1bc9be742c04cbdf5b04f3ce97Yiwei Zhang
1931068e31b929b40a1bc9be742c04cbdf5b04f3ce97Yiwei Zhang    const auto& hwcInfo = getBE().mHwcLayers.at(hwcId);
1932068e31b929b40a1bc9be742c04cbdf5b04f3ce97Yiwei Zhang
1933068e31b929b40a1bc9be742c04cbdf5b04f3ce97Yiwei Zhang    const Rect& frame = hwcInfo.displayFrame;
1934068e31b929b40a1bc9be742c04cbdf5b04f3ce97Yiwei Zhang    LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
1935068e31b929b40a1bc9be742c04cbdf5b04f3ce97Yiwei Zhang
1936068e31b929b40a1bc9be742c04cbdf5b04f3ce97Yiwei Zhang    const FloatRect& crop = hwcInfo.sourceCrop;
1937068e31b929b40a1bc9be742c04cbdf5b04f3ce97Yiwei Zhang    LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
1938068e31b929b40a1bc9be742c04cbdf5b04f3ce97Yiwei Zhang
1939068e31b929b40a1bc9be742c04cbdf5b04f3ce97Yiwei Zhang    const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
1940068e31b929b40a1bc9be742c04cbdf5b04f3ce97Yiwei Zhang    layerInfo->set_hwc_transform(transform);
19417c64f17bd0d70aec63b475c3de850dcf3abaff33Yiwei Zhang
19427c64f17bd0d70aec63b475c3de850dcf3abaff33Yiwei Zhang    const int32_t compositionType = static_cast<int32_t>(hwcInfo.compositionType);
19437c64f17bd0d70aec63b475c3de850dcf3abaff33Yiwei Zhang    layerInfo->set_hwc_composition_type(compositionType);
19447c64f17bd0d70aec63b475c3de850dcf3abaff33Yiwei Zhang
19457c64f17bd0d70aec63b475c3de850dcf3abaff33Yiwei Zhang    if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
19467c64f17bd0d70aec63b475c3de850dcf3abaff33Yiwei Zhang        static_cast<BufferLayer*>(this)->isProtected()) {
19477c64f17bd0d70aec63b475c3de850dcf3abaff33Yiwei Zhang        layerInfo->set_is_protected(true);
19487c64f17bd0d70aec63b475c3de850dcf3abaff33Yiwei Zhang    } else {
19497c64f17bd0d70aec63b475c3de850dcf3abaff33Yiwei Zhang        layerInfo->set_is_protected(false);
19507c64f17bd0d70aec63b475c3de850dcf3abaff33Yiwei Zhang    }
1951068e31b929b40a1bc9be742c04cbdf5b04f3ce97Yiwei Zhang}
1952068e31b929b40a1bc9be742c04cbdf5b04f3ce97Yiwei Zhang
195313127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian// ---------------------------------------------------------------------------
195413127d8921356dff794250e04208c3ed60b3a3dfMathias Agopian
19553f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian}; // namespace android
1956edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
19573f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian#if defined(__gl_h_)
19583f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian#error "don't include gl/gl.h in this file"
19593f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian#endif
1960edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
19613f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian#if defined(__gl2_h_)
19623f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian#error "don't include gl2/gl2.h in this file"
19633f84483382be2d528918cc1a6fbc6a7d68e0b181Mathias Agopian#endif
1964