blob: 19c84d0a854b49bacde4d8c883f15474672edff1 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dan Stoza9e56aa02015-11-02 13:00:03 -080017//#define LOG_NDEBUG 0
18#undef LOG_TAG
19#define LOG_TAG "Layer"
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080020#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
Mathias Agopian13127d82013-03-05 17:47:11 -080022#include <math.h>
David Sodman41fdfc92017-11-06 16:09:56 -080023#include <stdint.h>
24#include <stdlib.h>
25#include <sys/types.h>
chaviw4b129c22018-04-09 16:19:43 -070026#include <algorithm>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027
Mathias Agopiana67932f2011-04-20 14:20:59 -070028#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070029#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070030#include <cutils/properties.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031
32#include <utils/Errors.h>
33#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080034#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080036#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037
Courtney Goeltzenleuchter36c44dc2017-04-14 09:33:16 -060038#include <ui/DebugUtils.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070039#include <ui/GraphicBuffer.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080040#include <ui/PixelFormat.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080041
Dan Stoza6b9454d2014-11-07 16:00:59 -080042#include <gui/BufferItem.h>
Kalle Raitaa099a242017-01-11 11:17:29 -080043#include <gui/LayerDebugInfo.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080044#include <gui/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045
Yiwei Zhang60d1a192018-03-07 14:52:28 -080046#include "BufferLayer.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020047#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070048#include "DisplayDevice.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include "Layer.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070050#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070051#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
Mathias Agopian1b031492012-06-20 17:51:20 -070054#include "DisplayHardware/HWComposer.h"
55
Peiyong Lincbc184f2018-08-22 13:24:10 -070056#include <renderengine/RenderEngine.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070057
Dan Stozac5da2712016-07-20 15:38:12 -070058#include <mutex>
chaviw1d044282017-09-27 12:19:28 -070059#include "LayerProtoHelper.h"
Dan Stozac5da2712016-07-20 15:38:12 -070060
David Sodman41fdfc92017-11-06 16:09:56 -080061#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063namespace android {
64
Lloyd Piquef1c675b2018-09-12 20:45:39 -070065std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080066
David Sodman41fdfc92017-11-06 16:09:56 -080067Layer::Layer(SurfaceFlinger* flinger, const sp<Client>& client, const String8& name, uint32_t w,
68 uint32_t h, uint32_t flags)
David Sodman0c69cad2017-08-21 12:12:51 -070069 : contentDirty(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080070 mFlinger(flinger),
Mathias Agopian13127d82013-03-05 17:47:11 -080071 mPremultipliedAlpha(true),
David Sodman0c69cad2017-08-21 12:12:51 -070072 mName(name),
Mathias Agopian13127d82013-03-05 17:47:11 -080073 mTransactionFlags(0),
Dan Stoza7dde5992015-05-22 09:51:44 -070074 mPendingStateMutex(),
75 mPendingStates(),
Mathias Agopiana67932f2011-04-20 14:20:59 -070076 mCurrentTransform(0),
Robert Carrc3574f72016-03-24 12:19:32 -070077 mOverrideScalingMode(-1),
Dan Stozacac35382016-01-27 12:21:06 -080078 mCurrentFrameNumber(0),
Mathias Agopian82d7ab62012-01-19 18:34:40 -080079 mFrameLatencyNeeded(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080080 mNeedsFiltering(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080081 mProtectedByApp(false),
Riley Andrews03414a12014-07-01 14:22:59 -070082 mClientRef(client),
Dan Stozaa4650a52015-05-12 12:56:16 -070083 mPotentialCursor(false),
David Sodmanb8af7922017-12-21 15:17:55 -080084 mFreezeGeometryUpdates(false),
chaviwfd462612018-05-31 16:11:27 -070085 mCurrentChildren(LayerVector::StateSet::Current),
86 mDrawingChildren(LayerVector::StateSet::Drawing),
David Sodman2b727ac2017-12-21 14:28:08 -080087 mBE{this, name.string()} {
Dan Stoza9e56aa02015-11-02 13:00:03 -080088
Mathias Agopiana67932f2011-04-20 14:20:59 -070089 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070090
91 uint32_t layerFlags = 0;
David Sodman41fdfc92017-11-06 16:09:56 -080092 if (flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
93 if (flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
94 if (flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070095
Mathias Agopian4d9b8222013-03-12 17:11:48 -070096 mName = name;
Dan Stozaf7ba41a2017-05-10 15:11:11 -070097 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070098
Marissa Wallf58c14b2018-07-24 10:50:43 -070099 mCurrentState.active_legacy.w = w;
100 mCurrentState.active_legacy.h = h;
David Sodman0c69cad2017-08-21 12:12:51 -0700101 mCurrentState.flags = layerFlags;
Marissa Wallf58c14b2018-07-24 10:50:43 -0700102 mCurrentState.active_legacy.transform.set(0, 0);
103 mCurrentState.crop_legacy.makeInvalid();
Marissa Wallf58c14b2018-07-24 10:50:43 -0700104 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700105 mCurrentState.z = 0;
chaviw13fdc492017-06-27 12:40:18 -0700106 mCurrentState.color.a = 1.0f;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700107 mCurrentState.layerStack = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700108 mCurrentState.sequence = 0;
Marissa Wallf58c14b2018-07-24 10:50:43 -0700109 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500110 mCurrentState.appId = 0;
111 mCurrentState.type = 0;
Marissa Wall61c58622018-07-18 10:12:20 -0700112 mCurrentState.active.w = 0;
113 mCurrentState.active.h = 0;
114 mCurrentState.active.transform.set(0, 0);
115 mCurrentState.transform = 0;
116 mCurrentState.transformToDisplayInverse = false;
117 mCurrentState.crop.makeInvalid();
118 mCurrentState.acquireFence = new Fence(-1);
119 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
120 mCurrentState.hdrMetadata.validTypes = 0;
121 mCurrentState.surfaceDamageRegion.clear();
122 mCurrentState.api = -1;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700123
124 // drawing state & current state are identical
125 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700126
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800127 CompositorTiming compositorTiming;
128 flinger->getCompositorTiming(&compositorTiming);
129 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200130 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Dan Stoza436ccf32018-06-21 12:10:12 -0700131}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700132
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700133Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800134 sp<Client> c(mClientRef.promote());
135 if (c != 0) {
136 c->detachLayer(this);
137 }
138
139 for (auto& point : mRemoteSyncPoints) {
140 point->setTransactionApplied();
141 }
142 for (auto& point : mLocalSyncPoints) {
143 point->setFrameAvailable();
144 }
Jamie Gennis6547ff42013-07-16 20:12:42 -0700145 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700146}
147
Mathias Agopian13127d82013-03-05 17:47:11 -0800148// ---------------------------------------------------------------------------
149// callbacks
150// ---------------------------------------------------------------------------
151
David Sodmaneb085e02017-10-05 18:49:04 -0700152/*
153 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
154 * Layer. So, the implementation is done in BufferLayer. When called on a
155 * ColorLayer object, it's essentially a NOP.
156 */
David Sodmaneb085e02017-10-05 18:49:04 -0700157void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800158
Chia-I Wuc6657022017-08-15 11:18:17 -0700159void Layer::onRemovedFromCurrentState() {
160 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
161
chaviw8b3871a2017-11-01 17:41:01 -0700162 mPendingRemoval = true;
163
Robert Carr5edb1ad2017-04-25 10:54:24 -0700164 if (mCurrentState.zOrderRelativeOf != nullptr) {
165 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
166 if (strongRelative != nullptr) {
167 strongRelative->removeZOrderRelative(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700168 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700169 }
170 mCurrentState.zOrderRelativeOf = nullptr;
171 }
172
Chia-I Wuc6657022017-08-15 11:18:17 -0700173 for (const auto& child : mCurrentChildren) {
174 child->onRemovedFromCurrentState();
175 }
176}
Chia-I Wu38512252017-05-17 14:36:16 -0700177
Chia-I Wuc6657022017-08-15 11:18:17 -0700178void Layer::onRemoved() {
179 // the layer is removed from SF mLayersPendingRemoval
David Sodmaneb085e02017-10-05 18:49:04 -0700180 abandon();
Chia-I Wuc6657022017-08-15 11:18:17 -0700181
Steven Thomasb02664d2017-07-26 18:48:28 -0700182 destroyAllHwcLayers();
Chia-I Wu38512252017-05-17 14:36:16 -0700183
Robert Carr1f0a16a2016-10-24 16:27:39 -0700184 for (const auto& child : mCurrentChildren) {
185 child->onRemoved();
186 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700187}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700188
Mathias Agopian13127d82013-03-05 17:47:11 -0800189// ---------------------------------------------------------------------------
190// set-up
191// ---------------------------------------------------------------------------
192
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700193const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800194 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195}
196
chaviw13fdc492017-06-27 12:40:18 -0700197bool Layer::getPremultipledAlpha() const {
198 return mPremultipliedAlpha;
199}
200
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700201sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800202 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700203 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800204}
205
206// ---------------------------------------------------------------------------
207// h/w composer set-up
208// ---------------------------------------------------------------------------
209
Dominik Laskowski7e045462018-05-30 13:02:02 -0700210bool Layer::createHwcLayer(HWComposer* hwc, int32_t displayId) {
211 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(displayId) != 0,
212 "Already have a layer for display %d", displayId);
David Sodmanb8aaea12017-12-14 15:54:51 -0800213 auto layer = std::shared_ptr<HWC2::Layer>(
214 hwc->createLayer(displayId),
215 [hwc, displayId](HWC2::Layer* layer) {
216 hwc->destroyLayer(displayId, layer); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700217 if (!layer) {
218 return false;
219 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700220 LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers[displayId];
Steven Thomasb02664d2017-07-26 18:48:28 -0700221 hwcInfo.hwc = hwc;
222 hwcInfo.layer = layer;
David Sodmanf6a38932018-05-25 15:27:50 -0700223 layer->setLayerDestroyedListener(
Dominik Laskowski7e045462018-05-30 13:02:02 -0700224 [this, displayId](HWC2::Layer* /*layer*/) { getBE().mHwcLayers.erase(displayId); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700225 return true;
226}
227
Dominik Laskowski7e045462018-05-30 13:02:02 -0700228bool Layer::destroyHwcLayer(int32_t displayId) {
229 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu83806892017-11-16 10:50:20 -0800230 return false;
Steven Thomasb02664d2017-07-26 18:48:28 -0700231 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700232 auto& hwcInfo = getBE().mHwcLayers[displayId];
David Sodman41fdfc92017-11-06 16:09:56 -0800233 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr, "Attempt to destroy null layer");
Steven Thomasb02664d2017-07-26 18:48:28 -0700234 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
David Sodmanb8aaea12017-12-14 15:54:51 -0800235 hwcInfo.layer = nullptr;
236
Chia-I Wu83806892017-11-16 10:50:20 -0800237 return true;
Steven Thomasb02664d2017-07-26 18:48:28 -0700238}
239
240void Layer::destroyAllHwcLayers() {
David Sodman6f65f3e2017-11-03 14:28:09 -0700241 size_t numLayers = getBE().mHwcLayers.size();
Steven Thomasb02664d2017-07-26 18:48:28 -0700242 for (size_t i = 0; i < numLayers; ++i) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700243 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.empty(), "destroyAllHwcLayers failed");
244 destroyHwcLayer(getBE().mHwcLayers.begin()->first);
Steven Thomasb02664d2017-07-26 18:48:28 -0700245 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700246 LOG_ALWAYS_FATAL_IF(!getBE().mHwcLayers.empty(),
David Sodman41fdfc92017-11-06 16:09:56 -0800247 "All hardware composer layers should have been destroyed");
Steven Thomasb02664d2017-07-26 18:48:28 -0700248}
Steven Thomasb02664d2017-07-26 18:48:28 -0700249
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800250Rect Layer::getContentCrop() const {
251 // this is the crop rectangle that applies to the buffer
252 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700253 Rect crop;
254 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800255 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700256 crop = mCurrentCrop;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800257 } else if (getBE().compositionInfo.mBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800258 // otherwise we use the whole buffer
David Sodman0cc69182017-11-17 12:12:07 -0800259 crop = getBE().compositionInfo.mBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700260 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800261 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700262 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700263 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700264 return crop;
265}
266
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700267static Rect reduce(const Rect& win, const Region& exclude) {
268 if (CC_LIKELY(exclude.isEmpty())) {
269 return win;
270 }
271 if (exclude.isRect()) {
272 return win.reduce(exclude.getBounds());
273 }
274 return Region(win).subtract(exclude).getBounds();
275}
276
Dan Stoza80d61162017-12-20 15:57:52 -0800277static FloatRect reduce(const FloatRect& win, const Region& exclude) {
278 if (CC_LIKELY(exclude.isEmpty())) {
279 return win;
280 }
281 // Convert through Rect (by rounding) for lack of FloatRegion
282 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
283}
284
Robert Carr1f0a16a2016-10-24 16:27:39 -0700285Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
Alec Mourib416efd2018-09-06 21:01:59 +0000286 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700287 Rect win(getActiveWidth(s), getActiveHeight(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700288
Marissa Wall61c58622018-07-18 10:12:20 -0700289 Rect crop = getCrop(s);
290 if (!crop.isEmpty()) {
291 win.intersect(crop, &win);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700292 }
293
Peiyong Linefefaac2018-08-17 12:27:51 -0700294 ui::Transform t = getTransform();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700295 win = t.transform(win);
296
Chia-I Wue41dbe62017-06-13 14:10:56 -0700297 const sp<Layer>& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700298 // Now we need to calculate the parent bounds, so we can clip ourselves to those.
299 // When calculating the parent bounds for purposes of clipping,
300 // we don't need to constrain the parent to its transparent region.
301 // The transparent region is an optimization based on the
302 // buffer contents of the layer, but does not affect the space allocated to
303 // it by policy, and thus children should be allowed to extend into the
304 // parent's transparent region. In fact one of the main uses, is to reduce
305 // buffer allocation size in cases where a child window sits behind a main window
306 // (by marking the hole in the parent window as a transparent region)
307 if (p != nullptr) {
308 Rect bounds = p->computeScreenBounds(false);
309 bounds.intersect(win, &win);
310 }
311
312 if (reduceTransparentRegion) {
Marissa Wall61c58622018-07-18 10:12:20 -0700313 auto const screenTransparentRegion = t.transform(getActiveTransparentRegion(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700314 win = reduce(win, screenTransparentRegion);
315 }
316
317 return win;
318}
319
Dan Stoza80d61162017-12-20 15:57:52 -0800320FloatRect Layer::computeBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000321 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700322 return computeBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700323}
324
Dan Stoza80d61162017-12-20 15:57:52 -0800325FloatRect Layer::computeBounds(const Region& activeTransparentRegion) const {
Alec Mourib416efd2018-09-06 21:01:59 +0000326 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700327 Rect win(getActiveWidth(s), getActiveHeight(s));
Robert Carrb5d3d262016-03-25 15:08:13 -0700328
Marissa Wall61c58622018-07-18 10:12:20 -0700329 Rect crop = getCrop(s);
330 if (!crop.isEmpty()) {
331 win.intersect(crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800332 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700333
Chia-I Wue41dbe62017-06-13 14:10:56 -0700334 const auto& p = mDrawingParent.promote();
Robert Carrd4ae7f32018-06-07 16:10:57 -0700335 FloatRect floatWin = win.toFloatRect();
336 FloatRect parentBounds = floatWin;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700337 if (p != nullptr) {
Robert Carrd4ae7f32018-06-07 16:10:57 -0700338 // We pass an empty Region here for reasons mirroring that of the case described in
339 // the computeScreenBounds reduceTransparentRegion=false case.
340 parentBounds = p->computeBounds(Region());
Robert Carr1f0a16a2016-10-24 16:27:39 -0700341 }
342
Peiyong Linefefaac2018-08-17 12:27:51 -0700343 ui::Transform t = s.active_legacy.transform;
Dan Stoza80d61162017-12-20 15:57:52 -0800344
Vishnu Nairdcce0e22018-08-23 08:35:19 -0700345 if (p != nullptr) {
Dan Stoza80d61162017-12-20 15:57:52 -0800346 floatWin = t.transform(floatWin);
Robert Carrd4ae7f32018-06-07 16:10:57 -0700347 floatWin = floatWin.intersect(parentBounds);
Dan Stoza80d61162017-12-20 15:57:52 -0800348 floatWin = t.inverse().transform(floatWin);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700349 }
350
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700351 // subtract the transparent region and snap to the bounds
Dan Stoza80d61162017-12-20 15:57:52 -0800352 return reduce(floatWin, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800353}
354
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700355Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& display) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700356 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800357 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700358 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800359
360 // apply the projection's clipping to the window crop in
361 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700362 // if there are no window scaling involved, this operation will map to full
363 // pixels in the buffer.
364 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
365 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700366
Marissa Wall61c58622018-07-18 10:12:20 -0700367 Rect activeCrop(getActiveWidth(s), getActiveHeight(s));
368 Rect crop = getCrop(s);
369 if (!crop.isEmpty()) {
370 activeCrop.intersect(crop, &activeCrop);
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700371 }
372
Peiyong Linefefaac2018-08-17 12:27:51 -0700373 ui::Transform t = getTransform();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700374 activeCrop = t.transform(activeCrop);
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700375 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000376 activeCrop.clear();
377 }
chaviwb1154d12017-10-31 14:15:36 -0700378
379 const auto& p = mDrawingParent.promote();
380 if (p != nullptr) {
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700381 auto parentCrop = p->computeInitialCrop(display);
chaviwb1154d12017-10-31 14:15:36 -0700382 activeCrop.intersect(parentCrop, &activeCrop);
383 }
384
Robert Carr1f0a16a2016-10-24 16:27:39 -0700385 return activeCrop;
386}
387
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700388FloatRect Layer::computeCrop(const sp<const DisplayDevice>& display) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700389 // the content crop is the area of the content that gets scaled to the
390 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800391 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700392
393 // In addition there is a WM-specified crop we pull from our drawing state.
394 const State& s(getDrawingState());
395
396 // Screen space to make reduction to parent crop clearer.
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700397 Rect activeCrop = computeInitialCrop(display);
Peiyong Linefefaac2018-08-17 12:27:51 -0700398 ui::Transform t = getTransform();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700399 // Back to layer space to work with the content crop.
400 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800401
Michael Lentine28ea2172014-11-19 18:32:37 -0800402 // This needs to be here as transform.transform(Rect) computes the
403 // transformed rect and then takes the bounding box of the result before
404 // returning. This means
405 // transform.inverse().transform(transform.transform(Rect)) != Rect
406 // in which case we need to make sure the final rect is clipped to the
407 // display bounds.
Marissa Wall61c58622018-07-18 10:12:20 -0700408 if (!activeCrop.intersect(Rect(getActiveWidth(s), getActiveHeight(s)), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000409 activeCrop.clear();
410 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800411
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700412 // subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700413 activeCrop = reduce(activeCrop, getActiveTransparentRegion(s));
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700414
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000415 // Transform the window crop to match the buffer coordinate system,
416 // which means using the inverse of the current transform set on the
417 // SurfaceFlingerConsumer.
418 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700419 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000420 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700421 * the code below applies the primary display's inverse transform to the
422 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000423 */
David Sodman41fdfc92017-11-06 16:09:56 -0800424 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000425 // calculate the inverse transform
426 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800427 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800428 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000429 // and apply to the current transform
Peiyong Linefefaac2018-08-17 12:27:51 -0700430 invTransform = (ui::Transform(invTransformOrient) *
431 ui::Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800432 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000433
Marissa Wall61c58622018-07-18 10:12:20 -0700434 int winWidth = getActiveWidth(s);
435 int winHeight = getActiveHeight(s);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000436 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
437 // If the activeCrop has been rotate the ends are rotated but not
438 // the space itself so when transforming ends back we can't rely on
439 // a modification of the axes of rotation. To account for this we
440 // need to reorient the inverse rotation in terms of the current
441 // axes of rotation.
442 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
443 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
444 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800445 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000446 }
Marissa Wall61c58622018-07-18 10:12:20 -0700447 winWidth = getActiveHeight(s);
448 winHeight = getActiveWidth(s);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000449 }
Marissa Wall61c58622018-07-18 10:12:20 -0700450 const Rect winCrop = activeCrop.transform(invTransform, getActiveWidth(s), getActiveHeight(s));
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000451
452 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800453 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000454 float yScale = crop.getHeight() / float(winHeight);
455
David Sodman41fdfc92017-11-06 16:09:56 -0800456 float insetL = winCrop.left * xScale;
457 float insetT = winCrop.top * yScale;
458 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000459 float insetB = (winHeight - winCrop.bottom) * yScale;
460
David Sodman41fdfc92017-11-06 16:09:56 -0800461 crop.left += insetL;
462 crop.top += insetT;
463 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000464 crop.bottom -= insetB;
465
Mathias Agopian13127d82013-03-05 17:47:11 -0800466 return crop;
467}
468
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700469void Layer::setGeometry(const sp<const DisplayDevice>& display, uint32_t z) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700470 const auto displayId = display->getId();
Peiyong Lin91b1df22018-06-18 18:00:16 -0700471 if (!hasHwcLayer(displayId)) {
472 ALOGE("[%s] failed to setGeometry: no HWC layer found (%d)",
473 mName.string(), displayId);
474 return;
475 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700476 auto& hwcInfo = getBE().mHwcLayers[displayId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700477
David Sodman10a41ff2018-08-05 12:14:17 -0700478 // Device or Cursor layers
479 if (mPotentialCursor) {
480 ALOGV("[%s] Requesting Cursor composition", mName.string());
481 setCompositionType(displayId, HWC2::Composition::Cursor);
482 } else {
483 ALOGV("[%s] Requesting Device composition", mName.string());
484 setCompositionType(displayId, HWC2::Composition::Device);
485 }
486
487 // Need to program geometry parts
488 getBE().compositionInfo.hwc.skipGeometry = false;
489
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700490 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800491 hwcInfo.forceClientComposition = false;
492
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700493 if (isSecure() && !display->isSecure()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800494 hwcInfo.forceClientComposition = true;
495 }
496
Mathias Agopian13127d82013-03-05 17:47:11 -0800497 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700498 const State& s(getDrawingState());
David Revemanecf0fa52017-03-03 11:32:44 -0500499 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700500 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800501 blendMode =
502 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800503 }
David Sodmanba340492018-08-05 21:51:33 -0700504 getBE().compositionInfo.hwc.blendMode = blendMode;
Mathias Agopian13127d82013-03-05 17:47:11 -0800505
506 // apply the layer's transform, followed by the display's global transform
507 // here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700508 Region activeTransparentRegion(getActiveTransparentRegion(s));
Peiyong Linefefaac2018-08-17 12:27:51 -0700509 ui::Transform t = getTransform();
Marissa Wall61c58622018-07-18 10:12:20 -0700510 Rect activeCrop = getCrop(s);
511 if (!activeCrop.isEmpty()) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700512 activeCrop = t.transform(activeCrop);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700513 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000514 activeCrop.clear();
515 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700516 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800517 // This needs to be here as transform.transform(Rect) computes the
518 // transformed rect and then takes the bounding box of the result before
519 // returning. This means
520 // transform.inverse().transform(transform.transform(Rect)) != Rect
521 // in which case we need to make sure the final rect is clipped to the
522 // display bounds.
Marissa Wall61c58622018-07-18 10:12:20 -0700523 if (!activeCrop.intersect(Rect(getActiveWidth(s), getActiveHeight(s)), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000524 activeCrop.clear();
525 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700526 // mark regions outside the crop as transparent
Marissa Wall61c58622018-07-18 10:12:20 -0700527 activeTransparentRegion.orSelf(Rect(0, 0, getActiveWidth(s), activeCrop.top));
Marissa Wallf58c14b2018-07-24 10:50:43 -0700528 activeTransparentRegion.orSelf(
Marissa Wall61c58622018-07-18 10:12:20 -0700529 Rect(0, activeCrop.bottom, getActiveWidth(s), getActiveHeight(s)));
David Sodman41fdfc92017-11-06 16:09:56 -0800530 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
531 activeTransparentRegion.orSelf(
Marissa Wall61c58622018-07-18 10:12:20 -0700532 Rect(activeCrop.right, activeCrop.top, getActiveWidth(s), activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700533 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700534
Dan Stoza80d61162017-12-20 15:57:52 -0800535 // computeBounds returns a FloatRect to provide more accuracy during the
536 // transformation. We then round upon constructing 'frame'.
537 Rect frame{t.transform(computeBounds(activeTransparentRegion))};
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700538 if (!frame.intersect(display->getViewport(), &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000539 frame.clear();
540 }
Peiyong Linefefaac2018-08-17 12:27:51 -0700541 const ui::Transform& tr = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800542 Rect transformedFrame = tr.transform(frame);
David Sodmanba340492018-08-05 21:51:33 -0700543 getBE().compositionInfo.hwc.displayFrame = transformedFrame;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800544
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700545 FloatRect sourceCrop = computeCrop(display);
David Sodmanba340492018-08-05 21:51:33 -0700546 getBE().compositionInfo.hwc.sourceCrop = sourceCrop;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800547
chaviw13fdc492017-06-27 12:40:18 -0700548 float alpha = static_cast<float>(getAlpha());
David Sodmanba340492018-08-05 21:51:33 -0700549 getBE().compositionInfo.hwc.alpha = alpha;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800550
David Sodmanba340492018-08-05 21:51:33 -0700551 getBE().compositionInfo.hwc.z = z;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500552
Albert Chaulk2a589632017-05-04 16:59:44 -0400553 int type = s.type;
554 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700555 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400556 if (parent.get()) {
557 auto& parentState = parent->getDrawingState();
rongliucfb187b2018-03-14 12:26:23 -0700558 if (parentState.type >= 0 || parentState.appId >= 0) {
559 type = parentState.type;
560 appId = parentState.appId;
561 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400562 }
563
David Sodmanba340492018-08-05 21:51:33 -0700564 getBE().compositionInfo.hwc.type = type;
565 getBE().compositionInfo.hwc.appId = appId;
566
Mathias Agopian29a367b2011-07-12 14:51:45 -0700567 /*
568 * Transformations are applied in this order:
569 * 1) buffer orientation/flip/mirror
570 * 2) state transformation (window manager)
571 * 3) layer orientation (screen orientation)
572 * (NOTE: the matrices are multiplied in reverse order)
573 */
574
Peiyong Linefefaac2018-08-17 12:27:51 -0700575 const ui::Transform bufferOrientation(mCurrentTransform);
576 ui::Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700577
Robert Carrcae605c2017-03-29 12:10:31 -0700578 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700579 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700580 * the code below applies the primary display's inverse transform to the
581 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700582 */
David Sodman41fdfc92017-11-06 16:09:56 -0800583 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700584 // calculate the inverse transform
585 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800586 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700587 }
Robert Carrcae605c2017-03-29 12:10:31 -0700588
589 /*
590 * Here we cancel out the orientation component of the WM transform.
591 * The scaling and translate components are already included in our bounds
592 * computation so it's enough to just omit it in the composition.
593 * See comment in onDraw with ref to b/36727915 for why.
594 */
Peiyong Linefefaac2018-08-17 12:27:51 -0700595 transform = ui::Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700596 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700597
598 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800599 const uint32_t orientation = transform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -0700600 if (orientation & ui::Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800601 // we can only handle simple transformation
Lloyd Pique074e8122018-07-26 12:57:23 -0700602 hwcInfo.forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800603 } else {
604 auto transform = static_cast<HWC2::Transform>(orientation);
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800605 hwcInfo.transform = transform;
David Sodmanba340492018-08-05 21:51:33 -0700606 getBE().compositionInfo.hwc.transform = transform;
David Sodman4b7c4bc2017-11-17 12:13:59 -0800607 }
608}
609
Dominik Laskowski7e045462018-05-30 13:02:02 -0700610void Layer::forceClientComposition(int32_t displayId) {
611 if (getBE().mHwcLayers.count(displayId) == 0) {
612 ALOGE("forceClientComposition: no HWC layer found (%d)", displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800613 return;
614 }
615
Dominik Laskowski7e045462018-05-30 13:02:02 -0700616 getBE().mHwcLayers[displayId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800617}
Dan Stozaee44edd2015-03-23 15:50:23 -0700618
Dominik Laskowski7e045462018-05-30 13:02:02 -0700619bool Layer::getForceClientComposition(int32_t displayId) {
620 if (getBE().mHwcLayers.count(displayId) == 0) {
621 ALOGE("getForceClientComposition: no HWC layer found (%d)", displayId);
chaviwc9232ed2017-11-14 15:31:15 -0800622 return false;
623 }
624
Dominik Laskowski7e045462018-05-30 13:02:02 -0700625 return getBE().mHwcLayers[displayId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800626}
627
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700628void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700629 const auto displayId = display->getId();
630 if (getBE().mHwcLayers.count(displayId) == 0 ||
631 getCompositionType(displayId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800632 return;
633 }
634
635 // This gives us only the "orientation" component of the transform
636 const State& s(getCurrentState());
637
638 // Apply the layer's transform, followed by the display's global transform
639 // Here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700640 Rect win(getActiveWidth(s), getActiveHeight(s));
641 Rect crop = getCrop(s);
642 if (!crop.isEmpty()) {
643 win.intersect(crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800644 }
645 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700646 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700647 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700648 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700649 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800650 auto position = displayTransform.transform(frame);
651
Dominik Laskowski7e045462018-05-30 13:02:02 -0700652 auto error =
David Sodmanb8aaea12017-12-14 15:54:51 -0800653 (getBE().mHwcLayers[displayId].layer)->setCursorPosition(
654 position.left, position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800655 ALOGE_IF(error != HWC2::Error::None,
656 "[%s] Failed to set cursor position "
657 "to (%d, %d): %s (%d)",
658 mName.string(), position.left, position.top, to_string(error).c_str(),
659 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800660}
Riley Andrews03414a12014-07-01 14:22:59 -0700661
Mathias Agopian13127d82013-03-05 17:47:11 -0800662// ---------------------------------------------------------------------------
663// drawing...
664// ---------------------------------------------------------------------------
665
Marissa Wall61c58622018-07-18 10:12:20 -0700666void Layer::draw(const RenderArea& renderArea, const Region& clip) {
chaviwa76b2712017-09-20 12:02:26 -0700667 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800668}
669
Marissa Wall61c58622018-07-18 10:12:20 -0700670void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) {
chaviwa76b2712017-09-20 12:02:26 -0700671 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800672}
673
David Sodman41fdfc92017-11-06 16:09:56 -0800674void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
675 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800676 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700677 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700678 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700679 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800680}
681
David Sodmanc1498e62018-09-12 14:36:26 -0700682void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
683 clearWithOpenGL(renderArea, 0, 0, 0, 0);
684}
685
David Sodman10a41ff2018-08-05 12:14:17 -0700686void Layer::setCompositionType(int32_t displayId, HWC2::Composition type, bool /*callIntoHwc*/) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700687 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu30505fb2018-03-26 16:20:31 -0700688 ALOGE("setCompositionType called without a valid HWC layer");
689 return;
David Sodman10a41ff2018-08-05 12:14:17 -0700690 } else {
691 if (getBE().mHwcLayers[displayId].compositionType != type) {
692 ALOGV("setCompositionType: Changing compositionType from %s to %s",
693 to_string(getBE().mHwcLayers[displayId].compositionType).c_str(),
694 to_string(type).c_str());
695 getBE().mHwcLayers[displayId].compositionType = type;
Chia-I Wu30505fb2018-03-26 16:20:31 -0700696 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800697 }
698}
699
Dominik Laskowski7e045462018-05-30 13:02:02 -0700700HWC2::Composition Layer::getCompositionType(int32_t displayId) const {
David Sodman15fb96e2018-01-07 10:23:24 -0800701 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700702 // If we're querying the composition type for a display that does not
703 // have a HWC counterpart, then it will always be Client
704 return HWC2::Composition::Client;
705 }
David Sodman15fb96e2018-01-07 10:23:24 -0800706 return getBE().mHwcLayers[displayId].compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800707}
708
Dominik Laskowski7e045462018-05-30 13:02:02 -0700709void Layer::setClearClientTarget(int32_t displayId, bool clear) {
710 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800711 ALOGE("setClearClientTarget called without a valid HWC layer");
712 return;
713 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700714 getBE().mHwcLayers[displayId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800715}
716
Dominik Laskowski7e045462018-05-30 13:02:02 -0700717bool Layer::getClearClientTarget(int32_t displayId) const {
718 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800719 ALOGE("getClearClientTarget called without a valid HWC layer");
720 return false;
721 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700722 return getBE().mHwcLayers.at(displayId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800723}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800724
Dan Stozacac35382016-01-27 12:21:06 -0800725bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
726 if (point->getFrameNumber() <= mCurrentFrameNumber) {
727 // Don't bother with a SyncPoint, since we've already latched the
728 // relevant frame
729 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700730 }
731
Dan Stozacac35382016-01-27 12:21:06 -0800732 Mutex::Autolock lock(mLocalSyncPointMutex);
733 mLocalSyncPoints.push_back(point);
734 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700735}
736
Mathias Agopian13127d82013-03-05 17:47:11 -0800737// ----------------------------------------------------------------------------
738// local state
739// ----------------------------------------------------------------------------
740
Peiyong Lin833074a2018-08-28 11:53:54 -0700741void Layer::computeGeometry(const RenderArea& renderArea,
742 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700743 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700744 const ui::Transform renderAreaTransform(renderArea.getTransform());
Dan Stoza80d61162017-12-20 15:57:52 -0800745 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700746
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000747 vec2 lt = vec2(win.left, win.top);
748 vec2 lb = vec2(win.left, win.bottom);
749 vec2 rb = vec2(win.right, win.bottom);
750 vec2 rt = vec2(win.right, win.top);
751
Peiyong Linefefaac2018-08-17 12:27:51 -0700752 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000753 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700754 lt = layerTransform.transform(lt);
755 lb = layerTransform.transform(lb);
756 rb = layerTransform.transform(rb);
757 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000758 }
759
Peiyong Lin833074a2018-08-28 11:53:54 -0700760 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700761 position[0] = renderAreaTransform.transform(lt);
762 position[1] = renderAreaTransform.transform(lb);
763 position[2] = renderAreaTransform.transform(rb);
764 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800765}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800766
David Sodman41fdfc92017-11-06 16:09:56 -0800767bool Layer::isSecure() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000768 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700769 return (s.flags & layer_state_t::eLayerSecure);
770}
771
Mathias Agopian13127d82013-03-05 17:47:11 -0800772void Layer::setVisibleRegion(const Region& visibleRegion) {
773 // always called from main thread
774 this->visibleRegion = visibleRegion;
775}
776
777void Layer::setCoveredRegion(const Region& coveredRegion) {
778 // always called from main thread
779 this->coveredRegion = coveredRegion;
780}
781
David Sodman41fdfc92017-11-06 16:09:56 -0800782void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800783 // always called from main thread
784 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
785}
786
Robert Carre5f4f692018-01-12 13:12:28 -0800787void Layer::clearVisibilityRegions() {
788 visibleRegion.clear();
789 visibleNonTransparentRegion.clear();
790 coveredRegion.clear();
791}
792
Mathias Agopian13127d82013-03-05 17:47:11 -0800793// ----------------------------------------------------------------------------
794// transaction
795// ----------------------------------------------------------------------------
796
Dan Stoza7dde5992015-05-22 09:51:44 -0700797void Layer::pushPendingState() {
798 if (!mCurrentState.modified) {
799 return;
800 }
801
Dan Stoza7dde5992015-05-22 09:51:44 -0700802 // If this transaction is waiting on the receipt of a frame, generate a sync
803 // point and send it to the remote layer.
Marissa Wallf58c14b2018-07-24 10:50:43 -0700804 if (mCurrentState.barrierLayer_legacy != nullptr) {
805 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800806 if (barrierLayer == nullptr) {
807 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700808 // If we can't promote the layer we are intended to wait on,
809 // then it is expired or otherwise invalid. Allow this transaction
810 // to be applied as per normal (no synchronization).
Marissa Wallf58c14b2018-07-24 10:50:43 -0700811 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800812 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700813 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800814 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800815 mRemoteSyncPoints.push_back(std::move(syncPoint));
816 } else {
817 // We already missed the frame we're supposed to synchronize
818 // on, so go ahead and apply the state update
Marissa Wallf58c14b2018-07-24 10:50:43 -0700819 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800820 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700821 }
822
Dan Stoza7dde5992015-05-22 09:51:44 -0700823 // Wake us up to check if the frame has been received
824 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700825 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700826 }
827 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700828 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700829}
830
Pablo Ceballos05289c22016-04-14 15:49:55 -0700831void Layer::popPendingState(State* stateToCommit) {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700832 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700833
834 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700835 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700836}
837
Pablo Ceballos05289c22016-04-14 15:49:55 -0700838bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700839 bool stateUpdateAvailable = false;
840 while (!mPendingStates.empty()) {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700841 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700842 if (mRemoteSyncPoints.empty()) {
843 // If we don't have a sync point for this, apply it anyway. It
844 // will be visually wrong, but it should keep us from getting
845 // into too much trouble.
846 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700847 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700848 stateUpdateAvailable = true;
849 continue;
850 }
851
Marissa Wallf58c14b2018-07-24 10:50:43 -0700852 if (mRemoteSyncPoints.front()->getFrameNumber() !=
853 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800854 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800855
856 // Signal our end of the sync point and then dispose of it
857 mRemoteSyncPoints.front()->setTransactionApplied();
858 mRemoteSyncPoints.pop_front();
859 continue;
860 }
861
Dan Stoza7dde5992015-05-22 09:51:44 -0700862 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
863 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700864 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700865 stateUpdateAvailable = true;
866
867 // Signal our end of the sync point and then dispose of it
868 mRemoteSyncPoints.front()->setTransactionApplied();
869 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800870 } else {
871 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700872 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700873 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700874 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700875 stateUpdateAvailable = true;
876 }
877 }
878
879 // If we still have pending updates, wake SurfaceFlinger back up and point
880 // it at this layer so we can process them
881 if (!mPendingStates.empty()) {
882 setTransactionFlags(eTransactionNeeded);
883 mFlinger->setTransactionFlags(eTraversalNeeded);
884 }
885
886 mCurrentState.modified = false;
887 return stateUpdateAvailable;
888}
889
Marissa Wall61c58622018-07-18 10:12:20 -0700890uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000891 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800892
Marissa Wall61c58622018-07-18 10:12:20 -0700893 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
894 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700895
David Sodmaneb085e02017-10-05 18:49:04 -0700896 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700897 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000898 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800899 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
900 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
901 " requested={ wh={%4u,%4u} }}\n"
902 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
903 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700904 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700905 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
906 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
907 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
908 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
909 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700910 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
911 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
912 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800913 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700914
Robert Carre392b552017-09-19 12:16:05 -0700915 // Don't let Layer::doTransaction update the drawing state
916 // if we have a pending resize, unless we are in fixed-size mode.
917 // the drawing state will be updated only once we receive a buffer
918 // with the correct size.
919 //
920 // In particular, we want to make sure the clip (which is part
921 // of the geometry state) is latched together with the size but is
922 // latched immediately when no resizing is involved.
923 //
924 // If a sideband stream is attached, however, we want to skip this
925 // optimization so that transactions aren't missed when a buffer
926 // never arrives
927 //
928 // In the case that we don't have a buffer we ignore other factors
929 // and avoid entering the resizePending state. At a high level the
930 // resizePending state is to avoid applying the state of the new buffer
931 // to the old buffer. However in the state where we don't have an old buffer
932 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700933 const bool resizePending =
934 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
935 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -0800936 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700937 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -0800938 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700939 flags |= eDontUpdateGeometryState;
940 }
941 }
942
Robert Carr7bf247e2017-05-18 14:02:49 -0700943 // Here we apply various requested geometry states, depending on our
944 // latching configuration. See Layer.h for a detailed discussion of
945 // how geometry latching is controlled.
946 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000947 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700948
949 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
950 // mode, which causes attributes which normally latch regardless of scaling mode,
951 // to be delayed. We copy the requested state to the active state making sure
952 // to respect these rules (again see Layer.h for a detailed discussion).
953 //
954 // There is an awkward asymmetry in the handling of the crop states in the position
955 // states, as can be seen below. Largely this arises from position and transform
956 // being stored in the same data structure while having different latching rules.
957 // b/38182305
958 //
Marissa Wall61c58622018-07-18 10:12:20 -0700959 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700960 // applyPendingStates in the presence of deferred transactions.
961 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -0700962 float tx = stateToCommit->active_legacy.transform.tx();
963 float ty = stateToCommit->active_legacy.transform.ty();
964 stateToCommit->active_legacy = stateToCommit->requested_legacy;
965 stateToCommit->active_legacy.transform.set(tx, ty);
966 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700967 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700968 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -0700969 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700970 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800971 }
972
Marissa Wall61c58622018-07-18 10:12:20 -0700973 return flags;
974}
975
976uint32_t Layer::doTransaction(uint32_t flags) {
977 ATRACE_CALL();
978
979 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +0000980 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -0700981 if (!applyPendingStates(&c)) {
982 return 0;
983 }
984
985 flags = doTransactionResize(flags, &c);
986
Alec Mourib416efd2018-09-06 21:01:59 +0000987 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700988
989 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800990 // invalidate and recompute the visible regions if needed
991 flags |= Layer::eVisibleRegion;
992 }
993
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700994 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800995 // invalidate and recompute the visible regions if needed
996 flags |= eVisibleRegion;
997 this->contentDirty = true;
998
999 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -07001000 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -07001001 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -08001002 }
1003
Dan Stozac8145172016-04-28 16:29:06 -07001004 // If the layer is hidden, signal and clear out all local sync points so
1005 // that transactions for layers depending on this layer's frames becoming
1006 // visible are not blocked
1007 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001008 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001009 }
1010
Mathias Agopian13127d82013-03-05 17:47:11 -08001011 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001012 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001013 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001014}
1015
Pablo Ceballos05289c22016-04-14 15:49:55 -07001016void Layer::commitTransaction(const State& stateToCommit) {
1017 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001018}
1019
Mathias Agopian13127d82013-03-05 17:47:11 -08001020uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Piquef1c675b2018-09-12 20:45:39 -07001021 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -08001022}
1023
1024uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Piquef1c675b2018-09-12 20:45:39 -07001025 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -08001026}
1027
Robert Carr82364e32016-05-15 11:27:47 -07001028bool Layer::setPosition(float x, float y, bool immediate) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001029 if (mCurrentState.requested_legacy.transform.tx() == x &&
1030 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001031 return false;
1032 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001033
1034 // We update the requested and active position simultaneously because
1035 // we want to apply the position portion of the transform matrix immediately,
1036 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Marissa Wallf58c14b2018-07-24 10:50:43 -07001037 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001038 if (immediate && !mFreezeGeometryUpdates) {
1039 // Here we directly update the active state
1040 // unlike other setters, because we store it within
1041 // the transform, but use different latching rules.
1042 // b/38182305
Marissa Wallf58c14b2018-07-24 10:50:43 -07001043 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001044 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001045 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001046
Dan Stoza7dde5992015-05-22 09:51:44 -07001047 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001048 setTransactionFlags(eTransactionNeeded);
1049 return true;
1050}
Robert Carr82364e32016-05-15 11:27:47 -07001051
Robert Carr1f0a16a2016-10-24 16:27:39 -07001052bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1053 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1054 if (idx < 0) {
1055 return false;
1056 }
1057 if (childLayer->setLayer(z)) {
1058 mCurrentChildren.removeAt(idx);
1059 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001060 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001061 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001062 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001063}
1064
Robert Carr503c7042017-09-27 15:06:08 -07001065bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1066 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1067 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1068 if (idx < 0) {
1069 return false;
1070 }
1071 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1072 mCurrentChildren.removeAt(idx);
1073 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001074 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001075 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001076 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001077}
1078
Robert Carrae060832016-11-28 10:51:00 -08001079bool Layer::setLayer(int32_t z) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001080 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001081 mCurrentState.sequence++;
1082 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001083 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001084
1085 // Discard all relative layering.
1086 if (mCurrentState.zOrderRelativeOf != nullptr) {
1087 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1088 if (strongRelative != nullptr) {
1089 strongRelative->removeZOrderRelative(this);
1090 }
1091 mCurrentState.zOrderRelativeOf = nullptr;
1092 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001093 setTransactionFlags(eTransactionNeeded);
1094 return true;
1095}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001096
Robert Carrdb66e622017-04-10 16:55:57 -07001097void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1098 mCurrentState.zOrderRelatives.remove(relative);
1099 mCurrentState.sequence++;
1100 mCurrentState.modified = true;
1101 setTransactionFlags(eTransactionNeeded);
1102}
1103
1104void Layer::addZOrderRelative(const wp<Layer>& relative) {
1105 mCurrentState.zOrderRelatives.add(relative);
1106 mCurrentState.modified = true;
1107 mCurrentState.sequence++;
1108 setTransactionFlags(eTransactionNeeded);
1109}
1110
Robert Carr503d2bd2017-12-04 15:49:47 -08001111bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001112 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1113 if (handle == nullptr) {
1114 return false;
1115 }
1116 sp<Layer> relative = handle->owner.promote();
1117 if (relative == nullptr) {
1118 return false;
1119 }
1120
Robert Carr503d2bd2017-12-04 15:49:47 -08001121 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1122 mCurrentState.zOrderRelativeOf == relative) {
1123 return false;
1124 }
1125
Robert Carrdb66e622017-04-10 16:55:57 -07001126 mCurrentState.sequence++;
1127 mCurrentState.modified = true;
Robert Carr503d2bd2017-12-04 15:49:47 -08001128 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001129
chaviw9ab4bd12017-11-03 13:11:00 -07001130 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1131 if (oldZOrderRelativeOf != nullptr) {
1132 oldZOrderRelativeOf->removeZOrderRelative(this);
1133 }
Robert Carrdb66e622017-04-10 16:55:57 -07001134 mCurrentState.zOrderRelativeOf = relative;
1135 relative->addZOrderRelative(this);
1136
1137 setTransactionFlags(eTransactionNeeded);
1138
1139 return true;
1140}
1141
Mathias Agopian13127d82013-03-05 17:47:11 -08001142bool Layer::setSize(uint32_t w, uint32_t h) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001143 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
1144 return false;
1145 mCurrentState.requested_legacy.w = w;
1146 mCurrentState.requested_legacy.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001147 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001148 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001149
1150 // record the new size, from this point on, when the client request
1151 // a buffer, it'll get the new size.
1152 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001153 return true;
1154}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001155bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001156 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001157 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001158 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001159 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001160 setTransactionFlags(eTransactionNeeded);
1161 return true;
1162}
chaviw13fdc492017-06-27 12:40:18 -07001163
1164bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001165 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1166 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001167 return false;
1168
1169 mCurrentState.sequence++;
1170 mCurrentState.color.r = color.r;
1171 mCurrentState.color.g = color.g;
1172 mCurrentState.color.b = color.b;
1173 mCurrentState.modified = true;
1174 setTransactionFlags(eTransactionNeeded);
1175 return true;
1176}
1177
Robert Carrd4ae7f32018-06-07 16:10:57 -07001178bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1179 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001180 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001181 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1182
1183 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1184 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1185 return false;
1186 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001187 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001188 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1189 matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001190 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001191 setTransactionFlags(eTransactionNeeded);
1192 return true;
1193}
Marissa Wall61c58622018-07-18 10:12:20 -07001194
Mathias Agopian13127d82013-03-05 17:47:11 -08001195bool Layer::setTransparentRegionHint(const Region& transparent) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001196 mCurrentState.requestedTransparentRegion_legacy = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001197 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001198 setTransactionFlags(eTransactionNeeded);
1199 return true;
1200}
1201bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1202 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001203 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001204 mCurrentState.sequence++;
1205 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001206 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001207 setTransactionFlags(eTransactionNeeded);
1208 return true;
1209}
Robert Carr99e27f02016-06-16 15:18:02 -07001210
Marissa Wallf58c14b2018-07-24 10:50:43 -07001211bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
1212 if (mCurrentState.requestedCrop_legacy == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001213 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001214 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001215 if (immediate && !mFreezeGeometryUpdates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001216 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001217 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001218 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1219
Dan Stoza7dde5992015-05-22 09:51:44 -07001220 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001221 setTransactionFlags(eTransactionNeeded);
1222 return true;
1223}
Robert Carr8d5227b2017-03-16 15:41:03 -07001224
Robert Carrc3574f72016-03-24 12:19:32 -07001225bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001226 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001227 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001228 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001229 return true;
1230}
1231
rongliucfb187b2018-03-14 12:26:23 -07001232void Layer::setInfo(int32_t type, int32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001233 mCurrentState.appId = appId;
1234 mCurrentState.type = type;
1235 mCurrentState.modified = true;
1236 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001237}
1238
Mathias Agopian13127d82013-03-05 17:47:11 -08001239bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001240 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001241 mCurrentState.sequence++;
1242 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001243 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001244 setTransactionFlags(eTransactionNeeded);
1245 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001246}
1247
Robert Carr1f0a16a2016-10-24 16:27:39 -07001248uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001249 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001250 if (p == nullptr) {
1251 return getDrawingState().layerStack;
1252 }
1253 return p->getLayerStack();
1254}
1255
Marissa Wallf58c14b2018-07-24 10:50:43 -07001256void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
1257 mCurrentState.barrierLayer_legacy = barrierLayer;
1258 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001259 // We don't set eTransactionNeeded, because just receiving a deferral
1260 // request without any other state updates shouldn't actually induce a delay
1261 mCurrentState.modified = true;
1262 pushPendingState();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001263 mCurrentState.barrierLayer_legacy = nullptr;
1264 mCurrentState.frameNumber_legacy = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001265 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001266}
1267
Marissa Wallf58c14b2018-07-24 10:50:43 -07001268void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001269 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001270 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001271}
1272
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001273// ----------------------------------------------------------------------------
1274// pageflip handling...
1275// ----------------------------------------------------------------------------
1276
Robert Carr1f0a16a2016-10-24 16:27:39 -07001277bool Layer::isHiddenByPolicy() const {
Alec Mourib416efd2018-09-06 21:01:59 +00001278 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001279 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001280 if (parent != nullptr && parent->isHiddenByPolicy()) {
1281 return true;
1282 }
1283 return s.flags & layer_state_t::eLayerHidden;
1284}
1285
David Sodman41fdfc92017-11-06 16:09:56 -08001286uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001287 // TODO: should we do something special if mSecure is set?
1288 if (mProtectedByApp) {
1289 // need a hardware-protected path to external video sink
1290 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001291 }
Riley Andrews03414a12014-07-01 14:22:59 -07001292 if (mPotentialCursor) {
1293 usage |= GraphicBuffer::USAGE_CURSOR;
1294 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001295 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001296 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001297}
1298
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001299void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001300 uint32_t orientation = 0;
1301 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001302 // The transform hint is used to improve performance, but we can
1303 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001304 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001305 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001306 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001307 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001308 orientation = 0;
1309 }
1310 }
David Sodmaneb085e02017-10-05 18:49:04 -07001311 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001312}
1313
Mathias Agopian13127d82013-03-05 17:47:11 -08001314// ----------------------------------------------------------------------------
1315// debugging
1316// ----------------------------------------------------------------------------
1317
Marissa Wall61c58622018-07-18 10:12:20 -07001318// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001319LayerDebugInfo Layer::getLayerDebugInfo() const {
1320 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001321 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001322 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001323 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001324 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1325 info.mType = String8(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001326 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001327 info.mVisibleRegion = visibleRegion;
1328 info.mSurfaceDamageRegion = surfaceDamageRegion;
1329 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001330 info.mX = ds.active_legacy.transform.tx();
1331 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001332 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001333 info.mWidth = ds.active_legacy.w;
1334 info.mHeight = ds.active_legacy.h;
1335 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001336 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001337 info.mFlags = ds.flags;
1338 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001339 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001340 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1341 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1342 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1343 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001344 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001345 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001346 if (buffer != 0) {
1347 info.mActiveBufferWidth = buffer->getWidth();
1348 info.mActiveBufferHeight = buffer->getHeight();
1349 info.mActiveBufferStride = buffer->getStride();
1350 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001351 } else {
1352 info.mActiveBufferWidth = 0;
1353 info.mActiveBufferHeight = 0;
1354 info.mActiveBufferStride = 0;
1355 info.mActiveBufferFormat = 0;
1356 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001357 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001358 info.mNumQueuedFrames = getQueuedFrameCount();
1359 info.mRefreshPending = isBufferLatched();
1360 info.mIsOpaque = isOpaque(ds);
1361 info.mContentDirty = contentDirty;
1362 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001363}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001364
Dan Stozae22aec72016-08-01 13:20:59 -07001365void Layer::miniDumpHeader(String8& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001366 result.append("-------------------------------");
1367 result.append("-------------------------------");
1368 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001369 result.append(" Layer name\n");
1370 result.append(" Z | ");
1371 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001372 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001373 result.append(" Disp Frame (LTRB) | ");
1374 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001375 result.append("-------------------------------");
1376 result.append("-------------------------------");
1377 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001378}
1379
Dominik Laskowski7e045462018-05-30 13:02:02 -07001380void Layer::miniDump(String8& result, int32_t displayId) const {
1381 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stozae22aec72016-08-01 13:20:59 -07001382 return;
1383 }
1384
1385 String8 name;
1386 if (mName.length() > 77) {
1387 std::string shortened;
1388 shortened.append(mName.string(), 36);
1389 shortened.append("[...]");
1390 shortened.append(mName.string() + (mName.length() - 36), 36);
1391 name = shortened.c_str();
1392 } else {
1393 name = mName;
1394 }
1395
1396 result.appendFormat(" %s\n", name.string());
1397
Alec Mourib416efd2018-09-06 21:01:59 +00001398 const State& layerState(getDrawingState());
Lloyd Pique074e8122018-07-26 12:57:23 -07001399 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(displayId);
Chia-I Wu1e043612018-03-01 09:45:09 -08001400 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
1401 result.appendFormat(" rel %6d | ", layerState.z);
1402 } else {
1403 result.appendFormat(" %10d | ", layerState.z);
1404 }
Dominik Laskowski7e045462018-05-30 13:02:02 -07001405 result.appendFormat("%10s | ", to_string(getCompositionType(displayId)).c_str());
Lloyd Pique074e8122018-07-26 12:57:23 -07001406 result.appendFormat("%10s | ", to_string(hwcInfo.transform).c_str());
1407 const Rect& frame = hwcInfo.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001408 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique074e8122018-07-26 12:57:23 -07001409 const FloatRect& crop = hwcInfo.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001410 result.appendFormat("%6.1f %6.1f %6.1f %6.1f\n", crop.left, crop.top, crop.right, crop.bottom);
Dan Stozae22aec72016-08-01 13:20:59 -07001411
David Sodman7e4ae112018-02-09 15:02:28 -08001412 result.append("- - - - - - - - - - - - - - - -\n");
1413
1414 std::string compositionInfoStr;
1415 getBE().compositionInfo.dump(compositionInfoStr, "compositionInfo");
1416 result.append(compositionInfoStr.c_str());
1417
Yichi Chen6ca35192018-05-29 12:20:43 +08001418 result.append("- - - - - - - - - - - - - - - -");
1419 result.append("- - - - - - - - - - - - - - - -");
1420 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001421}
Dan Stozae22aec72016-08-01 13:20:59 -07001422
Svetoslavd85084b2014-03-20 10:28:31 -07001423void Layer::dumpFrameStats(String8& result) const {
1424 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001425}
1426
Svetoslavd85084b2014-03-20 10:28:31 -07001427void Layer::clearFrameStats() {
1428 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001429}
1430
Jamie Gennis6547ff42013-07-16 20:12:42 -07001431void Layer::logFrameStats() {
1432 mFrameTracker.logAndResetStats(mName);
1433}
1434
Svetoslavd85084b2014-03-20 10:28:31 -07001435void Layer::getFrameStats(FrameStats* outStats) const {
1436 mFrameTracker.getStats(outStats);
1437}
1438
Brian Andersond6927fb2016-07-23 23:37:30 -07001439void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001440 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001441 Mutex::Autolock lock(mFrameEventHistoryMutex);
1442 mFrameEventHistory.checkFencesForCompletion();
1443 mFrameEventHistory.dump(result);
1444}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001445
Brian Anderson5ea5e592016-12-01 16:54:33 -08001446void Layer::onDisconnect() {
1447 Mutex::Autolock lock(mFrameEventHistoryMutex);
1448 mFrameEventHistory.onDisconnect();
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001449 mTimeStats.onDisconnect(getName().c_str());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001450}
1451
Brian Anderson3890c392016-07-25 12:48:08 -07001452void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001453 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001454 if (newTimestamps) {
1455 mTimeStats.setPostTime(getName().c_str(), newTimestamps->frameNumber,
1456 newTimestamps->postedTime);
1457 }
1458
Brian Andersond6927fb2016-07-23 23:37:30 -07001459 Mutex::Autolock lock(mFrameEventHistoryMutex);
1460 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001461 // If there are any unsignaled fences in the aquire timeline at this
1462 // point, the previously queued frame hasn't been latched yet. Go ahead
1463 // and try to get the signal time here so the syscall is taken out of
1464 // the main thread's critical path.
1465 mAcquireTimeline.updateSignalTimes();
1466 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001467 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001468 mFrameEventHistory.addQueue(*newTimestamps);
1469 }
1470
Brian Anderson3890c392016-07-25 12:48:08 -07001471 if (outDelta) {
1472 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001473 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001474}
Dan Stozae77c7662016-05-13 11:37:28 -07001475
Chia-I Wu98f1c102017-05-30 14:54:08 -07001476size_t Layer::getChildrenCount() const {
1477 size_t count = 0;
1478 for (const sp<Layer>& child : mCurrentChildren) {
1479 count += 1 + child->getChildrenCount();
1480 }
1481 return count;
1482}
1483
Robert Carr1f0a16a2016-10-24 16:27:39 -07001484void Layer::addChild(const sp<Layer>& layer) {
1485 mCurrentChildren.add(layer);
1486 layer->setParent(this);
1487}
1488
1489ssize_t Layer::removeChild(const sp<Layer>& layer) {
1490 layer->setParent(nullptr);
1491 return mCurrentChildren.remove(layer);
1492}
1493
Robert Carr1db73f62016-12-21 12:58:51 -08001494bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1495 sp<Handle> handle = nullptr;
1496 sp<Layer> newParent = nullptr;
1497 if (newParentHandle == nullptr) {
1498 return false;
1499 }
1500 handle = static_cast<Handle*>(newParentHandle.get());
1501 newParent = handle->owner.promote();
1502 if (newParent == nullptr) {
1503 ALOGE("Unable to promote Layer handle");
1504 return false;
1505 }
1506
1507 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001508 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001509
1510 sp<Client> client(child->mClientRef.promote());
1511 if (client != nullptr) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001512 client->updateParent(newParent);
Robert Carr1db73f62016-12-21 12:58:51 -08001513 }
1514 }
1515 mCurrentChildren.clear();
1516
1517 return true;
1518}
1519
Robert Carr15eae092018-03-23 13:43:53 -07001520void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001521 for (const sp<Layer>& child : mDrawingChildren) {
1522 child->mDrawingParent = newParent;
1523 }
1524}
1525
chaviwf1961f72017-09-18 16:41:07 -07001526bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1527 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001528 return false;
1529 }
1530
1531 auto handle = static_cast<Handle*>(newParentHandle.get());
1532 sp<Layer> newParent = handle->owner.promote();
1533 if (newParent == nullptr) {
1534 ALOGE("Unable to promote Layer handle");
1535 return false;
1536 }
1537
chaviwf1961f72017-09-18 16:41:07 -07001538 sp<Layer> parent = getParent();
1539 if (parent != nullptr) {
1540 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001541 }
chaviwf1961f72017-09-18 16:41:07 -07001542 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001543
chaviwf1961f72017-09-18 16:41:07 -07001544 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001545 sp<Client> newParentClient(newParent->mClientRef.promote());
1546
chaviwf1961f72017-09-18 16:41:07 -07001547 if (client != newParentClient) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001548 client->updateParent(newParent);
chaviw06178942017-07-27 10:25:59 -07001549 }
1550
chaviw06178942017-07-27 10:25:59 -07001551 return true;
1552}
1553
Robert Carr9524cb32017-02-13 11:32:32 -08001554bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001555 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001556 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001557 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001558 if (client != nullptr && parentClient != client) {
Robert Carr7f619b22017-11-06 12:56:35 -08001559 client->detachLayer(child.get());
1560 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001561 }
Robert Carr7f619b22017-11-06 12:56:35 -08001562 }
Robert Carr9524cb32017-02-13 11:32:32 -08001563
1564 return true;
1565}
1566
Chia-I Wu11481472018-05-04 10:43:19 -07001567bool Layer::isLegacyDataSpace() const {
1568 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001569 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001570 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001571}
1572
Robert Carr1f0a16a2016-10-24 16:27:39 -07001573void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001574 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001575}
1576
1577void Layer::clearSyncPoints() {
1578 for (const auto& child : mCurrentChildren) {
1579 child->clearSyncPoints();
1580 }
1581
1582 Mutex::Autolock lock(mLocalSyncPointMutex);
1583 for (auto& point : mLocalSyncPoints) {
1584 point->setFrameAvailable();
1585 }
1586 mLocalSyncPoints.clear();
1587}
1588
1589int32_t Layer::getZ() const {
1590 return mDrawingState.z;
1591}
1592
Robert Carr29abff82017-12-04 13:51:20 -08001593bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1594 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1595 const State& state = useDrawing ? mDrawingState : mCurrentState;
1596 return state.zOrderRelativeOf != nullptr;
1597}
1598
David Sodman41fdfc92017-11-06 16:09:56 -08001599__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001600 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001601 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1602 "makeTraversalList received invalid stateSet");
1603 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1604 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1605 const State& state = useDrawing ? mDrawingState : mCurrentState;
1606
Robert Carr29abff82017-12-04 13:51:20 -08001607 if (state.zOrderRelatives.size() == 0) {
1608 *outSkipRelativeZUsers = true;
1609 return children;
1610 }
1611
chaviwfd462612018-05-31 16:11:27 -07001612 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001613 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001614 sp<Layer> strongRelative = weakRelative.promote();
1615 if (strongRelative != nullptr) {
1616 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001617 }
1618 }
1619
Dan Stoza412903f2017-04-27 13:42:17 -07001620 for (const sp<Layer>& child : children) {
Robert Carr503c7042017-09-27 15:06:08 -07001621 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1622 if (childState.zOrderRelativeOf != nullptr) {
1623 continue;
1624 }
Robert Carrdb66e622017-04-10 16:55:57 -07001625 traverse.add(child);
1626 }
1627
1628 return traverse;
1629}
1630
Robert Carr1f0a16a2016-10-24 16:27:39 -07001631/**
Robert Carrdb66e622017-04-10 16:55:57 -07001632 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001633 */
Dan Stoza412903f2017-04-27 13:42:17 -07001634void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001635 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1636 // produce a new list for traversing, including our relatives, and not including our children
1637 // who are relatives of another surface. In the case that there are no relative Z,
1638 // makeTraversalList returns our children directly to avoid significant overhead.
1639 // However in this case we need to take the responsibility for filtering children which
1640 // are relatives of another surface here.
1641 bool skipRelativeZUsers = false;
1642 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001643
Robert Carr1f0a16a2016-10-24 16:27:39 -07001644 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001645 for (; i < list.size(); i++) {
1646 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001647 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1648 continue;
1649 }
1650
Robert Carrdb66e622017-04-10 16:55:57 -07001651 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001652 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001653 }
Dan Stoza412903f2017-04-27 13:42:17 -07001654 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001655 }
Robert Carr29abff82017-12-04 13:51:20 -08001656
Dan Stoza412903f2017-04-27 13:42:17 -07001657 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001658 for (; i < list.size(); i++) {
1659 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001660
1661 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1662 continue;
1663 }
Dan Stoza412903f2017-04-27 13:42:17 -07001664 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001665 }
1666}
1667
1668/**
Robert Carrdb66e622017-04-10 16:55:57 -07001669 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001670 */
Dan Stoza412903f2017-04-27 13:42:17 -07001671void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1672 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001673 // See traverseInZOrder for documentation.
1674 bool skipRelativeZUsers = false;
1675 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001676
Robert Carr1f0a16a2016-10-24 16:27:39 -07001677 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001678 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001679 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001680
1681 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1682 continue;
1683 }
1684
Robert Carrdb66e622017-04-10 16:55:57 -07001685 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001686 break;
1687 }
Dan Stoza412903f2017-04-27 13:42:17 -07001688 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001689 }
Dan Stoza412903f2017-04-27 13:42:17 -07001690 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001691 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001692 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001693
1694 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1695 continue;
1696 }
1697
Dan Stoza412903f2017-04-27 13:42:17 -07001698 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001699 }
1700}
1701
chaviw4b129c22018-04-09 16:19:43 -07001702LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1703 const std::vector<Layer*>& layersInTree) {
1704 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1705 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001706 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1707 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
chaviw4b129c22018-04-09 16:19:43 -07001708 const State& state = useDrawing ? mDrawingState : mCurrentState;
1709
chaviwfd462612018-05-31 16:11:27 -07001710 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001711 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1712 sp<Layer> strongRelative = weakRelative.promote();
1713 // Only add relative layers that are also descendents of the top most parent of the tree.
1714 // If a relative layer is not a descendent, then it should be ignored.
1715 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1716 traverse.add(strongRelative);
1717 }
1718 }
1719
1720 for (const sp<Layer>& child : children) {
1721 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1722 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1723 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1724 // the child here since it won't be added later as a relative.
1725 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1726 childState.zOrderRelativeOf.promote().get())) {
1727 continue;
1728 }
1729 traverse.add(child);
1730 }
1731
1732 return traverse;
1733}
1734
1735void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1736 LayerVector::StateSet stateSet,
1737 const LayerVector::Visitor& visitor) {
1738 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001739
1740 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001741 for (; i < list.size(); i++) {
1742 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001743 if (relative->getZ() >= 0) {
1744 break;
1745 }
chaviw4b129c22018-04-09 16:19:43 -07001746 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001747 }
chaviw4b129c22018-04-09 16:19:43 -07001748
chaviwa76b2712017-09-20 12:02:26 -07001749 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001750 for (; i < list.size(); i++) {
1751 const auto& relative = list[i];
1752 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001753 }
1754}
1755
chaviw4b129c22018-04-09 16:19:43 -07001756std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1757 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1758 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1759
1760 std::vector<Layer*> layersInTree = {this};
1761 for (size_t i = 0; i < children.size(); i++) {
1762 const auto& child = children[i];
1763 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1764 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1765 }
1766
1767 return layersInTree;
1768}
1769
1770void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1771 const LayerVector::Visitor& visitor) {
1772 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1773 std::sort(layersInTree.begin(), layersInTree.end());
1774 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1775}
1776
Peiyong Linefefaac2018-08-17 12:27:51 -07001777ui::Transform Layer::getTransform() const {
1778 ui::Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001779 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001780 if (p != nullptr) {
1781 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001782
1783 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1784 // it isFixedSize) then there may be additional scaling not accounted
1785 // for in the transform. We need to mirror this scaling in child surfaces
1786 // or we will break the contract where WM can treat child surfaces as
1787 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08001788 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001789 int bufferWidth;
1790 int bufferHeight;
1791 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08001792 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
1793 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001794 } else {
David Sodman0cc69182017-11-17 12:12:07 -08001795 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
1796 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001797 }
Marissa Wall61c58622018-07-18 10:12:20 -07001798 float sx = p->getActiveWidth(p->getDrawingState()) / static_cast<float>(bufferWidth);
1799 float sy = p->getActiveHeight(p->getDrawingState()) / static_cast<float>(bufferHeight);
Peiyong Linefefaac2018-08-17 12:27:51 -07001800 ui::Transform extraParentScaling;
Robert Carr9b429f42017-04-17 14:56:57 -07001801 extraParentScaling.set(sx, 0, 0, sy);
1802 t = t * extraParentScaling;
1803 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001804 }
Marissa Wall61c58622018-07-18 10:12:20 -07001805 return t * getActiveTransform(getDrawingState());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001806}
1807
chaviw13fdc492017-06-27 12:40:18 -07001808half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001809 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001810
chaviw13fdc492017-06-27 12:40:18 -07001811 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1812 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001813}
Robert Carr6452f122017-03-21 10:41:29 -07001814
chaviw13fdc492017-06-27 12:40:18 -07001815half4 Layer::getColor() const {
1816 const half4 color(getDrawingState().color);
1817 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001818}
Robert Carr6452f122017-03-21 10:41:29 -07001819
Robert Carr1f0a16a2016-10-24 16:27:39 -07001820void Layer::commitChildList() {
1821 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1822 const auto& child = mCurrentChildren[i];
1823 child->commitChildList();
1824 }
1825 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001826 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001827}
1828
chaviw1d044282017-09-27 12:19:28 -07001829void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1830 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1831 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1832 const State& state = useDrawing ? mDrawingState : mCurrentState;
1833
Peiyong Linefefaac2018-08-17 12:27:51 -07001834 ui::Transform requestedTransform = state.active_legacy.transform;
1835 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07001836
1837 layerInfo->set_id(sequence);
1838 layerInfo->set_name(getName().c_str());
1839 layerInfo->set_type(String8(getTypeId()));
1840
1841 for (const auto& child : children) {
1842 layerInfo->add_children(child->sequence);
1843 }
1844
1845 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1846 sp<Layer> strongRelative = weakRelative.promote();
1847 if (strongRelative != nullptr) {
1848 layerInfo->add_relatives(strongRelative->sequence);
1849 }
1850 }
1851
Marissa Wallf58c14b2018-07-24 10:50:43 -07001852 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
chaviw1d044282017-09-27 12:19:28 -07001853 layerInfo->mutable_transparent_region());
1854 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
1855 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
1856
1857 layerInfo->set_layer_stack(getLayerStack());
1858 layerInfo->set_z(state.z);
1859
1860 PositionProto* position = layerInfo->mutable_position();
1861 position->set_x(transform.tx());
1862 position->set_y(transform.ty());
1863
1864 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
1865 requestedPosition->set_x(requestedTransform.tx());
1866 requestedPosition->set_y(requestedTransform.ty());
1867
1868 SizeProto* size = layerInfo->mutable_size();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001869 size->set_w(state.active_legacy.w);
1870 size->set_h(state.active_legacy.h);
chaviw1d044282017-09-27 12:19:28 -07001871
Marissa Wallf58c14b2018-07-24 10:50:43 -07001872 LayerProtoHelper::writeToProto(state.crop_legacy, layerInfo->mutable_crop());
chaviw1d044282017-09-27 12:19:28 -07001873
1874 layerInfo->set_is_opaque(isOpaque(state));
1875 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07001876
1877 // XXX (b/79210409) mCurrentDataSpace is not protected
1878 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1879
chaviw1d044282017-09-27 12:19:28 -07001880 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1881 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
1882 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
1883 layerInfo->set_flags(state.flags);
1884
1885 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1886 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
1887
Jorim Jaggi8e0af362017-11-14 16:28:28 +01001888 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07001889 if (parent != nullptr) {
1890 layerInfo->set_parent(parent->sequence);
1891 }
1892
1893 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1894 if (zOrderRelativeOf != nullptr) {
1895 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1896 }
1897
Chia-I Wu01591c92018-05-22 12:03:00 -07001898 // XXX getBE().compositionInfo.mBuffer is not protected
David Sodman0cc69182017-11-17 12:12:07 -08001899 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001900 if (buffer != nullptr) {
1901 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
Peiyong Linefefaac2018-08-17 12:27:51 -07001902 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
Yichi Chen6ca35192018-05-29 12:20:43 +08001903 layerInfo->mutable_buffer_transform());
chaviw1d044282017-09-27 12:19:28 -07001904 }
1905
1906 layerInfo->set_queued_frames(getQueuedFrameCount());
1907 layerInfo->set_refresh_pending(isBufferLatched());
rongliucfb187b2018-03-14 12:26:23 -07001908 layerInfo->set_window_type(state.type);
1909 layerInfo->set_app_id(state.appId);
chaviwadc40c22018-07-10 16:57:27 -07001910 layerInfo->set_curr_frame(mCurrentFrameNumber);
1911
1912 for (const auto& pendingState : mPendingStates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001913 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
chaviwadc40c22018-07-10 16:57:27 -07001914 if (barrierLayer != nullptr) {
1915 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1916 barrierLayerProto->set_id(barrierLayer->sequence);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001917 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
chaviwadc40c22018-07-10 16:57:27 -07001918 }
1919 }
chaviw1d044282017-09-27 12:19:28 -07001920}
1921
Dominik Laskowski7e045462018-05-30 13:02:02 -07001922void Layer::writeToProto(LayerProto* layerInfo, int32_t displayId) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07001923 if (!hasHwcLayer(displayId)) {
1924 return;
1925 }
1926
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001927 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
1928
Dominik Laskowski7e045462018-05-30 13:02:02 -07001929 const auto& hwcInfo = getBE().mHwcLayers.at(displayId);
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001930
1931 const Rect& frame = hwcInfo.displayFrame;
1932 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
1933
1934 const FloatRect& crop = hwcInfo.sourceCrop;
1935 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
1936
1937 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
1938 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08001939
1940 const int32_t compositionType = static_cast<int32_t>(hwcInfo.compositionType);
1941 layerInfo->set_hwc_composition_type(compositionType);
1942
1943 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
1944 static_cast<BufferLayer*>(this)->isProtected()) {
1945 layerInfo->set_is_protected(true);
1946 } else {
1947 layerInfo->set_is_protected(false);
1948 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001949}
1950
Mathias Agopian13127d82013-03-05 17:47:11 -08001951// ---------------------------------------------------------------------------
1952
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001953}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07001954
1955#if defined(__gl_h_)
1956#error "don't include gl/gl.h in this file"
1957#endif
1958
1959#if defined(__gl2_h_)
1960#error "don't include gl2/gl2.h in this file"
1961#endif