blob: 4f0b212129c5bc06d65a1fddac7843406b92719a [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
Mathias Agopian13127d82013-03-05 17:47:11 -080065int32_t Layer::sSequence = 1;
66
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 sequence(uint32_t(android_atomic_inc(&sSequence))),
71 mFlinger(flinger),
Mathias Agopian13127d82013-03-05 17:47:11 -080072 mPremultipliedAlpha(true),
David Sodman0c69cad2017-08-21 12:12:51 -070073 mName(name),
Mathias Agopian13127d82013-03-05 17:47:11 -080074 mTransactionFlags(0),
Dan Stoza7dde5992015-05-22 09:51:44 -070075 mPendingStateMutex(),
76 mPendingStates(),
Mathias Agopiana67932f2011-04-20 14:20:59 -070077 mCurrentTransform(0),
Robert Carrc3574f72016-03-24 12:19:32 -070078 mOverrideScalingMode(-1),
Dan Stozacac35382016-01-27 12:21:06 -080079 mCurrentFrameNumber(0),
Mathias Agopian82d7ab62012-01-19 18:34:40 -080080 mFrameLatencyNeeded(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080081 mFiltering(false),
82 mNeedsFiltering(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080083 mProtectedByApp(false),
Riley Andrews03414a12014-07-01 14:22:59 -070084 mClientRef(client),
Dan Stozaa4650a52015-05-12 12:56:16 -070085 mPotentialCursor(false),
David Sodmanb8af7922017-12-21 15:17:55 -080086 mFreezeGeometryUpdates(false),
chaviwfd462612018-05-31 16:11:27 -070087 mCurrentChildren(LayerVector::StateSet::Current),
88 mDrawingChildren(LayerVector::StateSet::Drawing),
David Sodman2b727ac2017-12-21 14:28:08 -080089 mBE{this, name.string()} {
Dan Stoza9e56aa02015-11-02 13:00:03 -080090
Mathias Agopiana67932f2011-04-20 14:20:59 -070091 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070092
93 uint32_t layerFlags = 0;
David Sodman41fdfc92017-11-06 16:09:56 -080094 if (flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
95 if (flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
96 if (flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070097
Mathias Agopian4d9b8222013-03-12 17:11:48 -070098 mName = name;
Dan Stozaf7ba41a2017-05-10 15:11:11 -070099 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700100
Marissa Wallf58c14b2018-07-24 10:50:43 -0700101 mCurrentState.active_legacy.w = w;
102 mCurrentState.active_legacy.h = h;
David Sodman0c69cad2017-08-21 12:12:51 -0700103 mCurrentState.flags = layerFlags;
Marissa Wallf58c14b2018-07-24 10:50:43 -0700104 mCurrentState.active_legacy.transform.set(0, 0);
105 mCurrentState.crop_legacy.makeInvalid();
106 mCurrentState.finalCrop_legacy.makeInvalid();
107 mCurrentState.requestedFinalCrop_legacy = mCurrentState.finalCrop_legacy;
108 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700109 mCurrentState.z = 0;
chaviw13fdc492017-06-27 12:40:18 -0700110 mCurrentState.color.a = 1.0f;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700111 mCurrentState.layerStack = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700112 mCurrentState.sequence = 0;
Marissa Wallf58c14b2018-07-24 10:50:43 -0700113 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500114 mCurrentState.appId = 0;
115 mCurrentState.type = 0;
Marissa Wall61c58622018-07-18 10:12:20 -0700116 mCurrentState.active.w = 0;
117 mCurrentState.active.h = 0;
118 mCurrentState.active.transform.set(0, 0);
119 mCurrentState.transform = 0;
120 mCurrentState.transformToDisplayInverse = false;
121 mCurrentState.crop.makeInvalid();
122 mCurrentState.acquireFence = new Fence(-1);
123 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
124 mCurrentState.hdrMetadata.validTypes = 0;
125 mCurrentState.surfaceDamageRegion.clear();
126 mCurrentState.api = -1;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700127
128 // drawing state & current state are identical
129 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700130
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800131 CompositorTiming compositorTiming;
132 flinger->getCompositorTiming(&compositorTiming);
133 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200134 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Dan Stoza436ccf32018-06-21 12:10:12 -0700135}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700136
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700137Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800138 sp<Client> c(mClientRef.promote());
139 if (c != 0) {
140 c->detachLayer(this);
141 }
142
143 for (auto& point : mRemoteSyncPoints) {
144 point->setTransactionApplied();
145 }
146 for (auto& point : mLocalSyncPoints) {
147 point->setFrameAvailable();
148 }
Jamie Gennis6547ff42013-07-16 20:12:42 -0700149 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700150}
151
Mathias Agopian13127d82013-03-05 17:47:11 -0800152// ---------------------------------------------------------------------------
153// callbacks
154// ---------------------------------------------------------------------------
155
David Sodmaneb085e02017-10-05 18:49:04 -0700156/*
157 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
158 * Layer. So, the implementation is done in BufferLayer. When called on a
159 * ColorLayer object, it's essentially a NOP.
160 */
David Sodmaneb085e02017-10-05 18:49:04 -0700161void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800162
Chia-I Wuc6657022017-08-15 11:18:17 -0700163void Layer::onRemovedFromCurrentState() {
164 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
165
chaviw8b3871a2017-11-01 17:41:01 -0700166 mPendingRemoval = true;
167
Robert Carr5edb1ad2017-04-25 10:54:24 -0700168 if (mCurrentState.zOrderRelativeOf != nullptr) {
169 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
170 if (strongRelative != nullptr) {
171 strongRelative->removeZOrderRelative(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700172 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700173 }
174 mCurrentState.zOrderRelativeOf = nullptr;
175 }
176
Chia-I Wuc6657022017-08-15 11:18:17 -0700177 for (const auto& child : mCurrentChildren) {
178 child->onRemovedFromCurrentState();
179 }
180}
Chia-I Wu38512252017-05-17 14:36:16 -0700181
Chia-I Wuc6657022017-08-15 11:18:17 -0700182void Layer::onRemoved() {
183 // the layer is removed from SF mLayersPendingRemoval
David Sodmaneb085e02017-10-05 18:49:04 -0700184 abandon();
Chia-I Wuc6657022017-08-15 11:18:17 -0700185
Steven Thomasb02664d2017-07-26 18:48:28 -0700186 destroyAllHwcLayers();
Chia-I Wu38512252017-05-17 14:36:16 -0700187
Robert Carr1f0a16a2016-10-24 16:27:39 -0700188 for (const auto& child : mCurrentChildren) {
189 child->onRemoved();
190 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700191}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700192
Mathias Agopian13127d82013-03-05 17:47:11 -0800193// ---------------------------------------------------------------------------
194// set-up
195// ---------------------------------------------------------------------------
196
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700197const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800198 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800199}
200
chaviw13fdc492017-06-27 12:40:18 -0700201bool Layer::getPremultipledAlpha() const {
202 return mPremultipliedAlpha;
203}
204
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700205sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800206 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700207 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800208}
209
210// ---------------------------------------------------------------------------
211// h/w composer set-up
212// ---------------------------------------------------------------------------
213
Dominik Laskowski7e045462018-05-30 13:02:02 -0700214bool Layer::createHwcLayer(HWComposer* hwc, int32_t displayId) {
215 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(displayId) != 0,
216 "Already have a layer for display %d", displayId);
David Sodmanb8aaea12017-12-14 15:54:51 -0800217 auto layer = std::shared_ptr<HWC2::Layer>(
218 hwc->createLayer(displayId),
219 [hwc, displayId](HWC2::Layer* layer) {
220 hwc->destroyLayer(displayId, layer); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700221 if (!layer) {
222 return false;
223 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700224 LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers[displayId];
Steven Thomasb02664d2017-07-26 18:48:28 -0700225 hwcInfo.hwc = hwc;
226 hwcInfo.layer = layer;
David Sodmanf6a38932018-05-25 15:27:50 -0700227 layer->setLayerDestroyedListener(
Dominik Laskowski7e045462018-05-30 13:02:02 -0700228 [this, displayId](HWC2::Layer* /*layer*/) { getBE().mHwcLayers.erase(displayId); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700229 return true;
230}
231
Dominik Laskowski7e045462018-05-30 13:02:02 -0700232bool Layer::destroyHwcLayer(int32_t displayId) {
233 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu83806892017-11-16 10:50:20 -0800234 return false;
Steven Thomasb02664d2017-07-26 18:48:28 -0700235 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700236 auto& hwcInfo = getBE().mHwcLayers[displayId];
David Sodman41fdfc92017-11-06 16:09:56 -0800237 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr, "Attempt to destroy null layer");
Steven Thomasb02664d2017-07-26 18:48:28 -0700238 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
David Sodmanb8aaea12017-12-14 15:54:51 -0800239 hwcInfo.layer = nullptr;
240
Chia-I Wu83806892017-11-16 10:50:20 -0800241 return true;
Steven Thomasb02664d2017-07-26 18:48:28 -0700242}
243
244void Layer::destroyAllHwcLayers() {
David Sodman6f65f3e2017-11-03 14:28:09 -0700245 size_t numLayers = getBE().mHwcLayers.size();
Steven Thomasb02664d2017-07-26 18:48:28 -0700246 for (size_t i = 0; i < numLayers; ++i) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700247 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.empty(), "destroyAllHwcLayers failed");
248 destroyHwcLayer(getBE().mHwcLayers.begin()->first);
Steven Thomasb02664d2017-07-26 18:48:28 -0700249 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700250 LOG_ALWAYS_FATAL_IF(!getBE().mHwcLayers.empty(),
David Sodman41fdfc92017-11-06 16:09:56 -0800251 "All hardware composer layers should have been destroyed");
Steven Thomasb02664d2017-07-26 18:48:28 -0700252}
Steven Thomasb02664d2017-07-26 18:48:28 -0700253
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800254Rect Layer::getContentCrop() const {
255 // this is the crop rectangle that applies to the buffer
256 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700257 Rect crop;
258 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800259 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700260 crop = mCurrentCrop;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800261 } else if (getBE().compositionInfo.mBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800262 // otherwise we use the whole buffer
David Sodman0cc69182017-11-17 12:12:07 -0800263 crop = getBE().compositionInfo.mBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700264 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800265 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700266 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700267 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700268 return crop;
269}
270
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700271static Rect reduce(const Rect& win, const Region& exclude) {
272 if (CC_LIKELY(exclude.isEmpty())) {
273 return win;
274 }
275 if (exclude.isRect()) {
276 return win.reduce(exclude.getBounds());
277 }
278 return Region(win).subtract(exclude).getBounds();
279}
280
Dan Stoza80d61162017-12-20 15:57:52 -0800281static FloatRect reduce(const FloatRect& win, const Region& exclude) {
282 if (CC_LIKELY(exclude.isEmpty())) {
283 return win;
284 }
285 // Convert through Rect (by rounding) for lack of FloatRegion
286 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
287}
288
Robert Carr1f0a16a2016-10-24 16:27:39 -0700289Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
290 const Layer::State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700291 Rect win(getActiveWidth(s), getActiveHeight(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700292
Marissa Wall61c58622018-07-18 10:12:20 -0700293 Rect crop = getCrop(s);
294 if (!crop.isEmpty()) {
295 win.intersect(crop, &win);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700296 }
297
Peiyong Linefefaac2018-08-17 12:27:51 -0700298 ui::Transform t = getTransform();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700299 win = t.transform(win);
300
Marissa Wall61c58622018-07-18 10:12:20 -0700301 Rect finalCrop = getFinalCrop(s);
302 if (!finalCrop.isEmpty()) {
303 win.intersect(finalCrop, &win);
Robert Carr41b08b52017-06-01 16:11:34 -0700304 }
305
Chia-I Wue41dbe62017-06-13 14:10:56 -0700306 const sp<Layer>& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700307 // Now we need to calculate the parent bounds, so we can clip ourselves to those.
308 // When calculating the parent bounds for purposes of clipping,
309 // we don't need to constrain the parent to its transparent region.
310 // The transparent region is an optimization based on the
311 // buffer contents of the layer, but does not affect the space allocated to
312 // it by policy, and thus children should be allowed to extend into the
313 // parent's transparent region. In fact one of the main uses, is to reduce
314 // buffer allocation size in cases where a child window sits behind a main window
315 // (by marking the hole in the parent window as a transparent region)
316 if (p != nullptr) {
317 Rect bounds = p->computeScreenBounds(false);
318 bounds.intersect(win, &win);
319 }
320
321 if (reduceTransparentRegion) {
Marissa Wall61c58622018-07-18 10:12:20 -0700322 auto const screenTransparentRegion = t.transform(getActiveTransparentRegion(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700323 win = reduce(win, screenTransparentRegion);
324 }
325
326 return win;
327}
328
Dan Stoza80d61162017-12-20 15:57:52 -0800329FloatRect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700330 const Layer::State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700331 return computeBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700332}
333
Dan Stoza80d61162017-12-20 15:57:52 -0800334FloatRect Layer::computeBounds(const Region& activeTransparentRegion) const {
Michael Lentine6c925ed2014-09-26 17:55:01 -0700335 const Layer::State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700336 Rect win(getActiveWidth(s), getActiveHeight(s));
Robert Carrb5d3d262016-03-25 15:08:13 -0700337
Marissa Wall61c58622018-07-18 10:12:20 -0700338 Rect crop = getCrop(s);
339 if (!crop.isEmpty()) {
340 win.intersect(crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800341 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700342
Chia-I Wue41dbe62017-06-13 14:10:56 -0700343 const auto& p = mDrawingParent.promote();
Robert Carrd4ae7f32018-06-07 16:10:57 -0700344 FloatRect floatWin = win.toFloatRect();
345 FloatRect parentBounds = floatWin;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700346 if (p != nullptr) {
Robert Carrd4ae7f32018-06-07 16:10:57 -0700347 // We pass an empty Region here for reasons mirroring that of the case described in
348 // the computeScreenBounds reduceTransparentRegion=false case.
349 parentBounds = p->computeBounds(Region());
Robert Carr1f0a16a2016-10-24 16:27:39 -0700350 }
351
Peiyong Linefefaac2018-08-17 12:27:51 -0700352 ui::Transform t = s.active_legacy.transform;
Dan Stoza80d61162017-12-20 15:57:52 -0800353
Marissa Wallf58c14b2018-07-24 10:50:43 -0700354 if (p != nullptr || !s.finalCrop_legacy.isEmpty()) {
Dan Stoza80d61162017-12-20 15:57:52 -0800355 floatWin = t.transform(floatWin);
Robert Carrd4ae7f32018-06-07 16:10:57 -0700356 floatWin = floatWin.intersect(parentBounds);
357
Marissa Wallf58c14b2018-07-24 10:50:43 -0700358 if (!s.finalCrop_legacy.isEmpty()) {
359 floatWin = floatWin.intersect(s.finalCrop_legacy.toFloatRect());
Robert Carrd4ae7f32018-06-07 16:10:57 -0700360 }
Dan Stoza80d61162017-12-20 15:57:52 -0800361 floatWin = t.inverse().transform(floatWin);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700362 }
363
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700364 // subtract the transparent region and snap to the bounds
Dan Stoza80d61162017-12-20 15:57:52 -0800365 return reduce(floatWin, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800366}
367
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700368Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& display) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700369 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800370 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700371 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800372
373 // apply the projection's clipping to the window crop in
374 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700375 // if there are no window scaling involved, this operation will map to full
376 // pixels in the buffer.
377 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
378 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700379
Marissa Wall61c58622018-07-18 10:12:20 -0700380 Rect activeCrop(getActiveWidth(s), getActiveHeight(s));
381 Rect crop = getCrop(s);
382 if (!crop.isEmpty()) {
383 activeCrop.intersect(crop, &activeCrop);
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700384 }
385
Peiyong Linefefaac2018-08-17 12:27:51 -0700386 ui::Transform t = getTransform();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700387 activeCrop = t.transform(activeCrop);
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700388 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000389 activeCrop.clear();
390 }
Marissa Wall61c58622018-07-18 10:12:20 -0700391 Rect finalCrop = getFinalCrop(s);
392 if (!finalCrop.isEmpty()) {
393 if (!activeCrop.intersect(finalCrop, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000394 activeCrop.clear();
395 }
396 }
chaviwb1154d12017-10-31 14:15:36 -0700397
398 const auto& p = mDrawingParent.promote();
399 if (p != nullptr) {
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700400 auto parentCrop = p->computeInitialCrop(display);
chaviwb1154d12017-10-31 14:15:36 -0700401 activeCrop.intersect(parentCrop, &activeCrop);
402 }
403
Robert Carr1f0a16a2016-10-24 16:27:39 -0700404 return activeCrop;
405}
406
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700407FloatRect Layer::computeCrop(const sp<const DisplayDevice>& display) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700408 // the content crop is the area of the content that gets scaled to the
409 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800410 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700411
412 // In addition there is a WM-specified crop we pull from our drawing state.
413 const State& s(getDrawingState());
414
415 // Screen space to make reduction to parent crop clearer.
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700416 Rect activeCrop = computeInitialCrop(display);
Peiyong Linefefaac2018-08-17 12:27:51 -0700417 ui::Transform t = getTransform();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700418 // Back to layer space to work with the content crop.
419 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800420
Michael Lentine28ea2172014-11-19 18:32:37 -0800421 // This needs to be here as transform.transform(Rect) computes the
422 // transformed rect and then takes the bounding box of the result before
423 // returning. This means
424 // transform.inverse().transform(transform.transform(Rect)) != Rect
425 // in which case we need to make sure the final rect is clipped to the
426 // display bounds.
Marissa Wall61c58622018-07-18 10:12:20 -0700427 if (!activeCrop.intersect(Rect(getActiveWidth(s), getActiveHeight(s)), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000428 activeCrop.clear();
429 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800430
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700431 // subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700432 activeCrop = reduce(activeCrop, getActiveTransparentRegion(s));
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700433
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000434 // Transform the window crop to match the buffer coordinate system,
435 // which means using the inverse of the current transform set on the
436 // SurfaceFlingerConsumer.
437 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700438 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000439 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700440 * the code below applies the primary display's inverse transform to the
441 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000442 */
David Sodman41fdfc92017-11-06 16:09:56 -0800443 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000444 // calculate the inverse transform
445 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800446 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800447 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000448 // and apply to the current transform
Peiyong Linefefaac2018-08-17 12:27:51 -0700449 invTransform = (ui::Transform(invTransformOrient) *
450 ui::Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800451 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000452
Marissa Wall61c58622018-07-18 10:12:20 -0700453 int winWidth = getActiveWidth(s);
454 int winHeight = getActiveHeight(s);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000455 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
456 // If the activeCrop has been rotate the ends are rotated but not
457 // the space itself so when transforming ends back we can't rely on
458 // a modification of the axes of rotation. To account for this we
459 // need to reorient the inverse rotation in terms of the current
460 // axes of rotation.
461 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
462 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
463 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800464 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000465 }
Marissa Wall61c58622018-07-18 10:12:20 -0700466 winWidth = getActiveHeight(s);
467 winHeight = getActiveWidth(s);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000468 }
Marissa Wall61c58622018-07-18 10:12:20 -0700469 const Rect winCrop = activeCrop.transform(invTransform, getActiveWidth(s), getActiveHeight(s));
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000470
471 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800472 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000473 float yScale = crop.getHeight() / float(winHeight);
474
David Sodman41fdfc92017-11-06 16:09:56 -0800475 float insetL = winCrop.left * xScale;
476 float insetT = winCrop.top * yScale;
477 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000478 float insetB = (winHeight - winCrop.bottom) * yScale;
479
David Sodman41fdfc92017-11-06 16:09:56 -0800480 crop.left += insetL;
481 crop.top += insetT;
482 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000483 crop.bottom -= insetB;
484
Mathias Agopian13127d82013-03-05 17:47:11 -0800485 return crop;
486}
487
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700488void Layer::setGeometry(const sp<const DisplayDevice>& display, uint32_t z) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700489 const auto displayId = display->getId();
Peiyong Lin91b1df22018-06-18 18:00:16 -0700490 if (!hasHwcLayer(displayId)) {
491 ALOGE("[%s] failed to setGeometry: no HWC layer found (%d)",
492 mName.string(), displayId);
493 return;
494 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700495 auto& hwcInfo = getBE().mHwcLayers[displayId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700496
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700497 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800498 hwcInfo.forceClientComposition = false;
499
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700500 if (isSecure() && !display->isSecure()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800501 hwcInfo.forceClientComposition = true;
502 }
503
Lloyd Pique074e8122018-07-26 12:57:23 -0700504 auto& hwcLayer = hwcInfo.layer;
505
Mathias Agopian13127d82013-03-05 17:47:11 -0800506 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700507 const State& s(getDrawingState());
David Revemanecf0fa52017-03-03 11:32:44 -0500508 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700509 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800510 blendMode =
511 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800512 }
Lloyd Pique074e8122018-07-26 12:57:23 -0700513 auto error = hwcLayer->setBlendMode(blendMode);
514 ALOGE_IF(error != HWC2::Error::None,
515 "[%s] Failed to set blend mode %s:"
516 " %s (%d)",
517 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
518 static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700519 getBE().compositionInfo.hwc.blendMode = blendMode;
Mathias Agopian13127d82013-03-05 17:47:11 -0800520
521 // apply the layer's transform, followed by the display's global transform
522 // here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700523 Region activeTransparentRegion(getActiveTransparentRegion(s));
Peiyong Linefefaac2018-08-17 12:27:51 -0700524 ui::Transform t = getTransform();
Marissa Wall61c58622018-07-18 10:12:20 -0700525 Rect activeCrop = getCrop(s);
526 if (!activeCrop.isEmpty()) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700527 activeCrop = t.transform(activeCrop);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700528 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000529 activeCrop.clear();
530 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700531 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800532 // This needs to be here as transform.transform(Rect) computes the
533 // transformed rect and then takes the bounding box of the result before
534 // returning. This means
535 // transform.inverse().transform(transform.transform(Rect)) != Rect
536 // in which case we need to make sure the final rect is clipped to the
537 // display bounds.
Marissa Wall61c58622018-07-18 10:12:20 -0700538 if (!activeCrop.intersect(Rect(getActiveWidth(s), getActiveHeight(s)), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000539 activeCrop.clear();
540 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700541 // mark regions outside the crop as transparent
Marissa Wall61c58622018-07-18 10:12:20 -0700542 activeTransparentRegion.orSelf(Rect(0, 0, getActiveWidth(s), activeCrop.top));
Marissa Wallf58c14b2018-07-24 10:50:43 -0700543 activeTransparentRegion.orSelf(
Marissa Wall61c58622018-07-18 10:12:20 -0700544 Rect(0, activeCrop.bottom, getActiveWidth(s), getActiveHeight(s)));
David Sodman41fdfc92017-11-06 16:09:56 -0800545 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
546 activeTransparentRegion.orSelf(
Marissa Wall61c58622018-07-18 10:12:20 -0700547 Rect(activeCrop.right, activeCrop.top, getActiveWidth(s), activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700548 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700549
Dan Stoza80d61162017-12-20 15:57:52 -0800550 // computeBounds returns a FloatRect to provide more accuracy during the
551 // transformation. We then round upon constructing 'frame'.
552 Rect frame{t.transform(computeBounds(activeTransparentRegion))};
Marissa Wall61c58622018-07-18 10:12:20 -0700553 Rect finalCrop = getFinalCrop(s);
554 if (!finalCrop.isEmpty()) {
555 if (!frame.intersect(finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000556 frame.clear();
557 }
558 }
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700559 if (!frame.intersect(display->getViewport(), &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000560 frame.clear();
561 }
Peiyong Linefefaac2018-08-17 12:27:51 -0700562 const ui::Transform& tr = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800563 Rect transformedFrame = tr.transform(frame);
Lloyd Pique074e8122018-07-26 12:57:23 -0700564 error = hwcLayer->setDisplayFrame(transformedFrame);
565 if (error != HWC2::Error::None) {
566 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
567 transformedFrame.left, transformedFrame.top, transformedFrame.right,
568 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
569 } else {
570 hwcInfo.displayFrame = transformedFrame;
571 }
David Sodmanba340492018-08-05 21:51:33 -0700572 getBE().compositionInfo.hwc.displayFrame = transformedFrame;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800573
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700574 FloatRect sourceCrop = computeCrop(display);
Lloyd Pique074e8122018-07-26 12:57:23 -0700575 error = hwcLayer->setSourceCrop(sourceCrop);
576 if (error != HWC2::Error::None) {
577 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
578 "%s (%d)",
579 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
580 to_string(error).c_str(), static_cast<int32_t>(error));
581 } else {
582 hwcInfo.sourceCrop = sourceCrop;
583 }
David Sodmanba340492018-08-05 21:51:33 -0700584 getBE().compositionInfo.hwc.sourceCrop = sourceCrop;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800585
chaviw13fdc492017-06-27 12:40:18 -0700586 float alpha = static_cast<float>(getAlpha());
Lloyd Pique074e8122018-07-26 12:57:23 -0700587 error = hwcLayer->setPlaneAlpha(alpha);
588 ALOGE_IF(error != HWC2::Error::None,
589 "[%s] Failed to set plane alpha %.3f: "
590 "%s (%d)",
591 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700592 getBE().compositionInfo.hwc.alpha = alpha;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800593
Lloyd Pique074e8122018-07-26 12:57:23 -0700594 error = hwcLayer->setZOrder(z);
595 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
596 to_string(error).c_str(), static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700597 getBE().compositionInfo.hwc.z = z;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500598
Albert Chaulk2a589632017-05-04 16:59:44 -0400599 int type = s.type;
600 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700601 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400602 if (parent.get()) {
603 auto& parentState = parent->getDrawingState();
rongliucfb187b2018-03-14 12:26:23 -0700604 if (parentState.type >= 0 || parentState.appId >= 0) {
605 type = parentState.type;
606 appId = parentState.appId;
607 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400608 }
609
Lloyd Pique074e8122018-07-26 12:57:23 -0700610 error = hwcLayer->setInfo(type, appId);
611 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
612 static_cast<int32_t>(error));
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800613
David Sodmanba340492018-08-05 21:51:33 -0700614 getBE().compositionInfo.hwc.type = type;
615 getBE().compositionInfo.hwc.appId = appId;
616
Mathias Agopian29a367b2011-07-12 14:51:45 -0700617 /*
618 * Transformations are applied in this order:
619 * 1) buffer orientation/flip/mirror
620 * 2) state transformation (window manager)
621 * 3) layer orientation (screen orientation)
622 * (NOTE: the matrices are multiplied in reverse order)
623 */
624
Peiyong Linefefaac2018-08-17 12:27:51 -0700625 const ui::Transform bufferOrientation(mCurrentTransform);
626 ui::Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700627
Robert Carrcae605c2017-03-29 12:10:31 -0700628 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700629 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700630 * the code below applies the primary display's inverse transform to the
631 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700632 */
David Sodman41fdfc92017-11-06 16:09:56 -0800633 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700634 // calculate the inverse transform
635 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800636 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700637 }
Robert Carrcae605c2017-03-29 12:10:31 -0700638
639 /*
640 * Here we cancel out the orientation component of the WM transform.
641 * The scaling and translate components are already included in our bounds
642 * computation so it's enough to just omit it in the composition.
643 * See comment in onDraw with ref to b/36727915 for why.
644 */
Peiyong Linefefaac2018-08-17 12:27:51 -0700645 transform = ui::Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700646 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700647
648 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800649 const uint32_t orientation = transform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -0700650 if (orientation & ui::Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800651 // we can only handle simple transformation
Lloyd Pique074e8122018-07-26 12:57:23 -0700652 hwcInfo.forceClientComposition = true;
David Sodmanba340492018-08-05 21:51:33 -0700653 getBE().mHwcLayers[displayId].compositionType = HWC2::Composition::Client;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800654 } else {
655 auto transform = static_cast<HWC2::Transform>(orientation);
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800656 hwcInfo.transform = transform;
Lloyd Pique074e8122018-07-26 12:57:23 -0700657 auto error = hwcLayer->setTransform(transform);
658 ALOGE_IF(error != HWC2::Error::None,
659 "[%s] Failed to set transform %s: "
660 "%s (%d)",
661 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
662 static_cast<int32_t>(error));
David Sodmanba340492018-08-05 21:51:33 -0700663 getBE().compositionInfo.hwc.transform = transform;
David Sodman4b7c4bc2017-11-17 12:13:59 -0800664 }
665}
666
Dominik Laskowski7e045462018-05-30 13:02:02 -0700667void Layer::forceClientComposition(int32_t displayId) {
668 if (getBE().mHwcLayers.count(displayId) == 0) {
669 ALOGE("forceClientComposition: no HWC layer found (%d)", displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800670 return;
671 }
672
Dominik Laskowski7e045462018-05-30 13:02:02 -0700673 getBE().mHwcLayers[displayId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800674}
Dan Stozaee44edd2015-03-23 15:50:23 -0700675
Dominik Laskowski7e045462018-05-30 13:02:02 -0700676bool Layer::getForceClientComposition(int32_t displayId) {
677 if (getBE().mHwcLayers.count(displayId) == 0) {
678 ALOGE("getForceClientComposition: no HWC layer found (%d)", displayId);
chaviwc9232ed2017-11-14 15:31:15 -0800679 return false;
680 }
681
Dominik Laskowski7e045462018-05-30 13:02:02 -0700682 return getBE().mHwcLayers[displayId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800683}
684
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700685void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700686 const auto displayId = display->getId();
687 if (getBE().mHwcLayers.count(displayId) == 0 ||
688 getCompositionType(displayId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800689 return;
690 }
691
692 // This gives us only the "orientation" component of the transform
693 const State& s(getCurrentState());
694
695 // Apply the layer's transform, followed by the display's global transform
696 // Here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700697 Rect win(getActiveWidth(s), getActiveHeight(s));
698 Rect crop = getCrop(s);
699 if (!crop.isEmpty()) {
700 win.intersect(crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800701 }
702 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700703 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700704 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700705 frame.intersect(display->getViewport(), &frame);
Marissa Wall61c58622018-07-18 10:12:20 -0700706 Rect finalCrop = getFinalCrop(s);
707 if (!finalCrop.isEmpty()) {
708 frame.intersect(finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000709 }
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700710 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800711 auto position = displayTransform.transform(frame);
712
Dominik Laskowski7e045462018-05-30 13:02:02 -0700713 auto error =
David Sodmanb8aaea12017-12-14 15:54:51 -0800714 (getBE().mHwcLayers[displayId].layer)->setCursorPosition(
715 position.left, position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800716 ALOGE_IF(error != HWC2::Error::None,
717 "[%s] Failed to set cursor position "
718 "to (%d, %d): %s (%d)",
719 mName.string(), position.left, position.top, to_string(error).c_str(),
720 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800721}
Riley Andrews03414a12014-07-01 14:22:59 -0700722
Mathias Agopian13127d82013-03-05 17:47:11 -0800723// ---------------------------------------------------------------------------
724// drawing...
725// ---------------------------------------------------------------------------
726
Marissa Wall61c58622018-07-18 10:12:20 -0700727void Layer::draw(const RenderArea& renderArea, const Region& clip) {
chaviwa76b2712017-09-20 12:02:26 -0700728 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800729}
730
Marissa Wall61c58622018-07-18 10:12:20 -0700731void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) {
chaviwa76b2712017-09-20 12:02:26 -0700732 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800733}
734
Marissa Wall61c58622018-07-18 10:12:20 -0700735void Layer::draw(const RenderArea& renderArea) {
chaviwa76b2712017-09-20 12:02:26 -0700736 onDraw(renderArea, Region(renderArea.getBounds()), false);
Dan Stozac7014012014-02-14 15:03:43 -0800737}
738
David Sodman41fdfc92017-11-06 16:09:56 -0800739void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
740 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800741 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700742 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700743 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700744 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800745}
746
chaviwa76b2712017-09-20 12:02:26 -0700747void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
David Sodman41fdfc92017-11-06 16:09:56 -0800748 clearWithOpenGL(renderArea, 0, 0, 0, 0);
Mathias Agopian13127d82013-03-05 17:47:11 -0800749}
750
Dominik Laskowski7e045462018-05-30 13:02:02 -0700751void Layer::setCompositionType(int32_t displayId, HWC2::Composition type, bool callIntoHwc) {
752 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu30505fb2018-03-26 16:20:31 -0700753 ALOGE("setCompositionType called without a valid HWC layer");
754 return;
755 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700756 auto& hwcInfo = getBE().mHwcLayers[displayId];
Chia-I Wu30505fb2018-03-26 16:20:31 -0700757 auto& hwcLayer = hwcInfo.layer;
David Sodmanb8aaea12017-12-14 15:54:51 -0800758 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", (hwcLayer)->getId(), to_string(type).c_str(),
Chia-I Wu30505fb2018-03-26 16:20:31 -0700759 static_cast<int>(callIntoHwc));
760 if (hwcInfo.compositionType != type) {
761 ALOGV(" actually setting");
762 hwcInfo.compositionType = type;
763 if (callIntoHwc) {
David Sodmanb8aaea12017-12-14 15:54:51 -0800764 auto error = (hwcLayer)->setCompositionType(type);
Chia-I Wu30505fb2018-03-26 16:20:31 -0700765 ALOGE_IF(error != HWC2::Error::None,
766 "[%s] Failed to set "
767 "composition type %s: %s (%d)",
768 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
769 static_cast<int32_t>(error));
770 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800771 }
772}
773
Dominik Laskowski7e045462018-05-30 13:02:02 -0700774HWC2::Composition Layer::getCompositionType(int32_t displayId) const {
David Sodman15fb96e2018-01-07 10:23:24 -0800775 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700776 // If we're querying the composition type for a display that does not
777 // have a HWC counterpart, then it will always be Client
778 return HWC2::Composition::Client;
779 }
David Sodman15fb96e2018-01-07 10:23:24 -0800780 return getBE().mHwcLayers[displayId].compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800781}
782
Dominik Laskowski7e045462018-05-30 13:02:02 -0700783void Layer::setClearClientTarget(int32_t displayId, bool clear) {
784 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800785 ALOGE("setClearClientTarget called without a valid HWC layer");
786 return;
787 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700788 getBE().mHwcLayers[displayId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800789}
790
Dominik Laskowski7e045462018-05-30 13:02:02 -0700791bool Layer::getClearClientTarget(int32_t displayId) const {
792 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800793 ALOGE("getClearClientTarget called without a valid HWC layer");
794 return false;
795 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700796 return getBE().mHwcLayers.at(displayId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800797}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800798
Dan Stozacac35382016-01-27 12:21:06 -0800799bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
800 if (point->getFrameNumber() <= mCurrentFrameNumber) {
801 // Don't bother with a SyncPoint, since we've already latched the
802 // relevant frame
803 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700804 }
805
Dan Stozacac35382016-01-27 12:21:06 -0800806 Mutex::Autolock lock(mLocalSyncPointMutex);
807 mLocalSyncPoints.push_back(point);
808 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700809}
810
Mathias Agopian13127d82013-03-05 17:47:11 -0800811void Layer::setFiltering(bool filtering) {
812 mFiltering = filtering;
813}
814
815bool Layer::getFiltering() const {
816 return mFiltering;
817}
818
Mathias Agopian13127d82013-03-05 17:47:11 -0800819// ----------------------------------------------------------------------------
820// local state
821// ----------------------------------------------------------------------------
822
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000823static void boundPoint(vec2* point, const Rect& crop) {
824 if (point->x < crop.left) {
825 point->x = crop.left;
826 }
827 if (point->x > crop.right) {
828 point->x = crop.right;
829 }
830 if (point->y < crop.top) {
831 point->y = crop.top;
832 }
833 if (point->y > crop.bottom) {
834 point->y = crop.bottom;
835 }
836}
837
chaviwa76b2712017-09-20 12:02:26 -0700838void Layer::computeGeometry(const RenderArea& renderArea, Mesh& mesh,
839 bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700840 const Layer::State& s(getDrawingState());
Peiyong Linefefaac2018-08-17 12:27:51 -0700841 const ui::Transform renderAreaTransform(renderArea.getTransform());
chaviwa76b2712017-09-20 12:02:26 -0700842 const uint32_t height = renderArea.getHeight();
Dan Stoza80d61162017-12-20 15:57:52 -0800843 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700844
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000845 vec2 lt = vec2(win.left, win.top);
846 vec2 lb = vec2(win.left, win.bottom);
847 vec2 rb = vec2(win.right, win.bottom);
848 vec2 rt = vec2(win.right, win.top);
849
Peiyong Linefefaac2018-08-17 12:27:51 -0700850 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000851 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700852 lt = layerTransform.transform(lt);
853 lb = layerTransform.transform(lb);
854 rb = layerTransform.transform(rb);
855 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000856 }
857
Marissa Wall61c58622018-07-18 10:12:20 -0700858 Rect finalCrop = getFinalCrop(s);
859 if (!finalCrop.isEmpty()) {
860 boundPoint(&lt, finalCrop);
861 boundPoint(&lb, finalCrop);
862 boundPoint(&rb, finalCrop);
863 boundPoint(&rt, finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000864 }
865
Mathias Agopianff2ed702013-09-01 21:36:12 -0700866 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700867 position[0] = renderAreaTransform.transform(lt);
868 position[1] = renderAreaTransform.transform(lb);
869 position[2] = renderAreaTransform.transform(rb);
870 position[3] = renderAreaTransform.transform(rt);
David Sodman41fdfc92017-11-06 16:09:56 -0800871 for (size_t i = 0; i < 4; i++) {
chaviwa76b2712017-09-20 12:02:26 -0700872 position[i].y = height - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -0800873 }
874}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800875
David Sodman41fdfc92017-11-06 16:09:56 -0800876bool Layer::isSecure() const {
Dan Stoza23116082015-06-18 14:58:39 -0700877 const Layer::State& s(mDrawingState);
878 return (s.flags & layer_state_t::eLayerSecure);
879}
880
Mathias Agopian13127d82013-03-05 17:47:11 -0800881void Layer::setVisibleRegion(const Region& visibleRegion) {
882 // always called from main thread
883 this->visibleRegion = visibleRegion;
884}
885
886void Layer::setCoveredRegion(const Region& coveredRegion) {
887 // always called from main thread
888 this->coveredRegion = coveredRegion;
889}
890
David Sodman41fdfc92017-11-06 16:09:56 -0800891void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800892 // always called from main thread
893 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
894}
895
Robert Carre5f4f692018-01-12 13:12:28 -0800896void Layer::clearVisibilityRegions() {
897 visibleRegion.clear();
898 visibleNonTransparentRegion.clear();
899 coveredRegion.clear();
900}
901
Mathias Agopian13127d82013-03-05 17:47:11 -0800902// ----------------------------------------------------------------------------
903// transaction
904// ----------------------------------------------------------------------------
905
Dan Stoza7dde5992015-05-22 09:51:44 -0700906void Layer::pushPendingState() {
907 if (!mCurrentState.modified) {
908 return;
909 }
910
Dan Stoza7dde5992015-05-22 09:51:44 -0700911 // If this transaction is waiting on the receipt of a frame, generate a sync
912 // point and send it to the remote layer.
Marissa Wallf58c14b2018-07-24 10:50:43 -0700913 if (mCurrentState.barrierLayer_legacy != nullptr) {
914 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800915 if (barrierLayer == nullptr) {
916 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700917 // If we can't promote the layer we are intended to wait on,
918 // then it is expired or otherwise invalid. Allow this transaction
919 // to be applied as per normal (no synchronization).
Marissa Wallf58c14b2018-07-24 10:50:43 -0700920 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800921 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700922 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800923 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800924 mRemoteSyncPoints.push_back(std::move(syncPoint));
925 } else {
926 // We already missed the frame we're supposed to synchronize
927 // on, so go ahead and apply the state update
Marissa Wallf58c14b2018-07-24 10:50:43 -0700928 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800929 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700930 }
931
Dan Stoza7dde5992015-05-22 09:51:44 -0700932 // Wake us up to check if the frame has been received
933 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700934 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700935 }
936 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700937 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700938}
939
Pablo Ceballos05289c22016-04-14 15:49:55 -0700940void Layer::popPendingState(State* stateToCommit) {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700941 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700942
943 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700944 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700945}
946
Pablo Ceballos05289c22016-04-14 15:49:55 -0700947bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700948 bool stateUpdateAvailable = false;
949 while (!mPendingStates.empty()) {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700950 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700951 if (mRemoteSyncPoints.empty()) {
952 // If we don't have a sync point for this, apply it anyway. It
953 // will be visually wrong, but it should keep us from getting
954 // into too much trouble.
955 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700956 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700957 stateUpdateAvailable = true;
958 continue;
959 }
960
Marissa Wallf58c14b2018-07-24 10:50:43 -0700961 if (mRemoteSyncPoints.front()->getFrameNumber() !=
962 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800963 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800964
965 // Signal our end of the sync point and then dispose of it
966 mRemoteSyncPoints.front()->setTransactionApplied();
967 mRemoteSyncPoints.pop_front();
968 continue;
969 }
970
Dan Stoza7dde5992015-05-22 09:51:44 -0700971 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
972 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700973 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700974 stateUpdateAvailable = true;
975
976 // Signal our end of the sync point and then dispose of it
977 mRemoteSyncPoints.front()->setTransactionApplied();
978 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800979 } else {
980 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700981 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700982 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700983 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700984 stateUpdateAvailable = true;
985 }
986 }
987
988 // If we still have pending updates, wake SurfaceFlinger back up and point
989 // it at this layer so we can process them
990 if (!mPendingStates.empty()) {
991 setTransactionFlags(eTransactionNeeded);
992 mFlinger->setTransactionFlags(eTraversalNeeded);
993 }
994
995 mCurrentState.modified = false;
996 return stateUpdateAvailable;
997}
998
Marissa Wall61c58622018-07-18 10:12:20 -0700999uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001000 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001001
Marissa Wall61c58622018-07-18 10:12:20 -07001002 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
1003 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -07001004
David Sodmaneb085e02017-10-05 18:49:04 -07001005 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001006 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +00001007 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -08001008 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
1009 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1010 " requested={ wh={%4u,%4u} }}\n"
1011 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1012 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -07001013 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -07001014 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
1015 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
1016 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
1017 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
1018 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -07001019 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
1020 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
1021 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001022
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001023 // record the new size, form this point on, when the client request
1024 // a buffer, it'll get the new size.
Marissa Wall61c58622018-07-18 10:12:20 -07001025 setDefaultBufferSize(stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001026 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001027
Robert Carre392b552017-09-19 12:16:05 -07001028 // Don't let Layer::doTransaction update the drawing state
1029 // if we have a pending resize, unless we are in fixed-size mode.
1030 // the drawing state will be updated only once we receive a buffer
1031 // with the correct size.
1032 //
1033 // In particular, we want to make sure the clip (which is part
1034 // of the geometry state) is latched together with the size but is
1035 // latched immediately when no resizing is involved.
1036 //
1037 // If a sideband stream is attached, however, we want to skip this
1038 // optimization so that transactions aren't missed when a buffer
1039 // never arrives
1040 //
1041 // In the case that we don't have a buffer we ignore other factors
1042 // and avoid entering the resizePending state. At a high level the
1043 // resizePending state is to avoid applying the state of the new buffer
1044 // to the old buffer. However in the state where we don't have an old buffer
1045 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -07001046 const bool resizePending =
1047 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
1048 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -08001049 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001050 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -08001051 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001052 flags |= eDontUpdateGeometryState;
1053 }
1054 }
1055
Robert Carr7bf247e2017-05-18 14:02:49 -07001056 // Here we apply various requested geometry states, depending on our
1057 // latching configuration. See Layer.h for a detailed discussion of
1058 // how geometry latching is controlled.
1059 if (!(flags & eDontUpdateGeometryState)) {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001060 Layer::State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001061
1062 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1063 // mode, which causes attributes which normally latch regardless of scaling mode,
1064 // to be delayed. We copy the requested state to the active state making sure
1065 // to respect these rules (again see Layer.h for a detailed discussion).
1066 //
1067 // There is an awkward asymmetry in the handling of the crop states in the position
1068 // states, as can be seen below. Largely this arises from position and transform
1069 // being stored in the same data structure while having different latching rules.
1070 // b/38182305
1071 //
Marissa Wall61c58622018-07-18 10:12:20 -07001072 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -07001073 // applyPendingStates in the presence of deferred transactions.
1074 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -07001075 float tx = stateToCommit->active_legacy.transform.tx();
1076 float ty = stateToCommit->active_legacy.transform.ty();
1077 stateToCommit->active_legacy = stateToCommit->requested_legacy;
1078 stateToCommit->active_legacy.transform.set(tx, ty);
1079 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001080 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001081 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -07001082 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -07001083 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001084 }
1085
Marissa Wall61c58622018-07-18 10:12:20 -07001086 return flags;
1087}
1088
1089uint32_t Layer::doTransaction(uint32_t flags) {
1090 ATRACE_CALL();
1091
1092 pushPendingState();
1093 Layer::State c = getCurrentState();
1094 if (!applyPendingStates(&c)) {
1095 return 0;
1096 }
1097
1098 flags = doTransactionResize(flags, &c);
1099
1100 const Layer::State& s(getDrawingState());
1101
1102 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001103 // invalidate and recompute the visible regions if needed
1104 flags |= Layer::eVisibleRegion;
1105 }
1106
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001107 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001108 // invalidate and recompute the visible regions if needed
1109 flags |= eVisibleRegion;
1110 this->contentDirty = true;
1111
1112 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -07001113 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -07001114 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -08001115 }
1116
Dan Stozac8145172016-04-28 16:29:06 -07001117 // If the layer is hidden, signal and clear out all local sync points so
1118 // that transactions for layers depending on this layer's frames becoming
1119 // visible are not blocked
1120 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001121 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001122 }
1123
Mathias Agopian13127d82013-03-05 17:47:11 -08001124 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001125 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001126 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001127}
1128
Pablo Ceballos05289c22016-04-14 15:49:55 -07001129void Layer::commitTransaction(const State& stateToCommit) {
1130 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001131}
1132
Mathias Agopian13127d82013-03-05 17:47:11 -08001133uint32_t Layer::getTransactionFlags(uint32_t flags) {
1134 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1135}
1136
1137uint32_t Layer::setTransactionFlags(uint32_t flags) {
1138 return android_atomic_or(flags, &mTransactionFlags);
1139}
1140
Robert Carr82364e32016-05-15 11:27:47 -07001141bool Layer::setPosition(float x, float y, bool immediate) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001142 if (mCurrentState.requested_legacy.transform.tx() == x &&
1143 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001144 return false;
1145 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001146
1147 // We update the requested and active position simultaneously because
1148 // we want to apply the position portion of the transform matrix immediately,
1149 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Marissa Wallf58c14b2018-07-24 10:50:43 -07001150 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001151 if (immediate && !mFreezeGeometryUpdates) {
1152 // Here we directly update the active state
1153 // unlike other setters, because we store it within
1154 // the transform, but use different latching rules.
1155 // b/38182305
Marissa Wallf58c14b2018-07-24 10:50:43 -07001156 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001157 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001158 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001159
Dan Stoza7dde5992015-05-22 09:51:44 -07001160 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001161 setTransactionFlags(eTransactionNeeded);
1162 return true;
1163}
Robert Carr82364e32016-05-15 11:27:47 -07001164
Robert Carr1f0a16a2016-10-24 16:27:39 -07001165bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1166 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1167 if (idx < 0) {
1168 return false;
1169 }
1170 if (childLayer->setLayer(z)) {
1171 mCurrentChildren.removeAt(idx);
1172 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001173 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001174 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001175 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001176}
1177
Robert Carr503c7042017-09-27 15:06:08 -07001178bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1179 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1180 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1181 if (idx < 0) {
1182 return false;
1183 }
1184 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1185 mCurrentChildren.removeAt(idx);
1186 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001187 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001188 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001189 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001190}
1191
Robert Carrae060832016-11-28 10:51:00 -08001192bool Layer::setLayer(int32_t z) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001193 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001194 mCurrentState.sequence++;
1195 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001196 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001197
1198 // Discard all relative layering.
1199 if (mCurrentState.zOrderRelativeOf != nullptr) {
1200 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1201 if (strongRelative != nullptr) {
1202 strongRelative->removeZOrderRelative(this);
1203 }
1204 mCurrentState.zOrderRelativeOf = nullptr;
1205 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001206 setTransactionFlags(eTransactionNeeded);
1207 return true;
1208}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001209
Robert Carrdb66e622017-04-10 16:55:57 -07001210void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1211 mCurrentState.zOrderRelatives.remove(relative);
1212 mCurrentState.sequence++;
1213 mCurrentState.modified = true;
1214 setTransactionFlags(eTransactionNeeded);
1215}
1216
1217void Layer::addZOrderRelative(const wp<Layer>& relative) {
1218 mCurrentState.zOrderRelatives.add(relative);
1219 mCurrentState.modified = true;
1220 mCurrentState.sequence++;
1221 setTransactionFlags(eTransactionNeeded);
1222}
1223
Robert Carr503d2bd2017-12-04 15:49:47 -08001224bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001225 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1226 if (handle == nullptr) {
1227 return false;
1228 }
1229 sp<Layer> relative = handle->owner.promote();
1230 if (relative == nullptr) {
1231 return false;
1232 }
1233
Robert Carr503d2bd2017-12-04 15:49:47 -08001234 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1235 mCurrentState.zOrderRelativeOf == relative) {
1236 return false;
1237 }
1238
Robert Carrdb66e622017-04-10 16:55:57 -07001239 mCurrentState.sequence++;
1240 mCurrentState.modified = true;
Robert Carr503d2bd2017-12-04 15:49:47 -08001241 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001242
chaviw9ab4bd12017-11-03 13:11:00 -07001243 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1244 if (oldZOrderRelativeOf != nullptr) {
1245 oldZOrderRelativeOf->removeZOrderRelative(this);
1246 }
Robert Carrdb66e622017-04-10 16:55:57 -07001247 mCurrentState.zOrderRelativeOf = relative;
1248 relative->addZOrderRelative(this);
1249
1250 setTransactionFlags(eTransactionNeeded);
1251
1252 return true;
1253}
1254
Mathias Agopian13127d82013-03-05 17:47:11 -08001255bool Layer::setSize(uint32_t w, uint32_t h) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001256 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
1257 return false;
1258 mCurrentState.requested_legacy.w = w;
1259 mCurrentState.requested_legacy.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001260 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001261 setTransactionFlags(eTransactionNeeded);
1262 return true;
1263}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001264bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001265 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001266 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001267 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001268 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001269 setTransactionFlags(eTransactionNeeded);
1270 return true;
1271}
chaviw13fdc492017-06-27 12:40:18 -07001272
1273bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001274 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1275 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001276 return false;
1277
1278 mCurrentState.sequence++;
1279 mCurrentState.color.r = color.r;
1280 mCurrentState.color.g = color.g;
1281 mCurrentState.color.b = color.b;
1282 mCurrentState.modified = true;
1283 setTransactionFlags(eTransactionNeeded);
1284 return true;
1285}
1286
Robert Carrd4ae7f32018-06-07 16:10:57 -07001287bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1288 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001289 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001290 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1291
1292 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1293 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1294 return false;
1295 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001296 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001297 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1298 matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001299 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001300 setTransactionFlags(eTransactionNeeded);
1301 return true;
1302}
Marissa Wall61c58622018-07-18 10:12:20 -07001303
Mathias Agopian13127d82013-03-05 17:47:11 -08001304bool Layer::setTransparentRegionHint(const Region& transparent) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001305 mCurrentState.requestedTransparentRegion_legacy = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001306 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001307 setTransactionFlags(eTransactionNeeded);
1308 return true;
1309}
1310bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1311 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001312 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001313 mCurrentState.sequence++;
1314 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001315 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001316 setTransactionFlags(eTransactionNeeded);
1317 return true;
1318}
Robert Carr99e27f02016-06-16 15:18:02 -07001319
Marissa Wallf58c14b2018-07-24 10:50:43 -07001320bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
1321 if (mCurrentState.requestedCrop_legacy == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001322 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001323 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001324 if (immediate && !mFreezeGeometryUpdates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001325 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001326 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001327 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1328
Dan Stoza7dde5992015-05-22 09:51:44 -07001329 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001330 setTransactionFlags(eTransactionNeeded);
1331 return true;
1332}
Robert Carr8d5227b2017-03-16 15:41:03 -07001333
Marissa Wallf58c14b2018-07-24 10:50:43 -07001334bool Layer::setFinalCrop_legacy(const Rect& crop, bool immediate) {
1335 if (mCurrentState.requestedFinalCrop_legacy == crop) return false;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001336 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001337 mCurrentState.requestedFinalCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001338 if (immediate && !mFreezeGeometryUpdates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001339 mCurrentState.finalCrop_legacy = crop;
Robert Carr8d5227b2017-03-16 15:41:03 -07001340 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001341 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1342
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001343 mCurrentState.modified = true;
1344 setTransactionFlags(eTransactionNeeded);
1345 return true;
1346}
Mathias Agopian13127d82013-03-05 17:47:11 -08001347
Robert Carrc3574f72016-03-24 12:19:32 -07001348bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001349 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001350 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001351 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001352 return true;
1353}
1354
rongliucfb187b2018-03-14 12:26:23 -07001355void Layer::setInfo(int32_t type, int32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001356 mCurrentState.appId = appId;
1357 mCurrentState.type = type;
1358 mCurrentState.modified = true;
1359 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001360}
1361
Mathias Agopian13127d82013-03-05 17:47:11 -08001362bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001363 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001364 mCurrentState.sequence++;
1365 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001366 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001367 setTransactionFlags(eTransactionNeeded);
1368 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001369}
1370
Robert Carr1f0a16a2016-10-24 16:27:39 -07001371uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001372 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001373 if (p == nullptr) {
1374 return getDrawingState().layerStack;
1375 }
1376 return p->getLayerStack();
1377}
1378
Marissa Wallf58c14b2018-07-24 10:50:43 -07001379void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
1380 mCurrentState.barrierLayer_legacy = barrierLayer;
1381 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001382 // We don't set eTransactionNeeded, because just receiving a deferral
1383 // request without any other state updates shouldn't actually induce a delay
1384 mCurrentState.modified = true;
1385 pushPendingState();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001386 mCurrentState.barrierLayer_legacy = nullptr;
1387 mCurrentState.frameNumber_legacy = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001388 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001389}
1390
Marissa Wallf58c14b2018-07-24 10:50:43 -07001391void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001392 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001393 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001394}
1395
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001396// ----------------------------------------------------------------------------
1397// pageflip handling...
1398// ----------------------------------------------------------------------------
1399
Robert Carr1f0a16a2016-10-24 16:27:39 -07001400bool Layer::isHiddenByPolicy() const {
1401 const Layer::State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001402 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001403 if (parent != nullptr && parent->isHiddenByPolicy()) {
1404 return true;
1405 }
1406 return s.flags & layer_state_t::eLayerHidden;
1407}
1408
David Sodman41fdfc92017-11-06 16:09:56 -08001409uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001410 // TODO: should we do something special if mSecure is set?
1411 if (mProtectedByApp) {
1412 // need a hardware-protected path to external video sink
1413 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001414 }
Riley Andrews03414a12014-07-01 14:22:59 -07001415 if (mPotentialCursor) {
1416 usage |= GraphicBuffer::USAGE_CURSOR;
1417 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001418 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001419 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001420}
1421
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001422void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001423 uint32_t orientation = 0;
1424 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001425 // The transform hint is used to improve performance, but we can
1426 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001427 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001428 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001429 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001430 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001431 orientation = 0;
1432 }
1433 }
David Sodmaneb085e02017-10-05 18:49:04 -07001434 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001435}
1436
Mathias Agopian13127d82013-03-05 17:47:11 -08001437// ----------------------------------------------------------------------------
1438// debugging
1439// ----------------------------------------------------------------------------
1440
Marissa Wall61c58622018-07-18 10:12:20 -07001441// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001442LayerDebugInfo Layer::getLayerDebugInfo() const {
1443 LayerDebugInfo info;
1444 const Layer::State& ds = getDrawingState();
1445 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001446 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001447 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1448 info.mType = String8(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001449 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001450 info.mVisibleRegion = visibleRegion;
1451 info.mSurfaceDamageRegion = surfaceDamageRegion;
1452 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001453 info.mX = ds.active_legacy.transform.tx();
1454 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001455 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001456 info.mWidth = ds.active_legacy.w;
1457 info.mHeight = ds.active_legacy.h;
1458 info.mCrop = ds.crop_legacy;
1459 info.mFinalCrop = ds.finalCrop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001460 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001461 info.mFlags = ds.flags;
1462 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001463 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001464 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1465 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1466 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1467 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001468 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001469 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001470 if (buffer != 0) {
1471 info.mActiveBufferWidth = buffer->getWidth();
1472 info.mActiveBufferHeight = buffer->getHeight();
1473 info.mActiveBufferStride = buffer->getStride();
1474 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001475 } else {
1476 info.mActiveBufferWidth = 0;
1477 info.mActiveBufferHeight = 0;
1478 info.mActiveBufferStride = 0;
1479 info.mActiveBufferFormat = 0;
1480 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001481 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001482 info.mNumQueuedFrames = getQueuedFrameCount();
1483 info.mRefreshPending = isBufferLatched();
1484 info.mIsOpaque = isOpaque(ds);
1485 info.mContentDirty = contentDirty;
1486 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001487}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001488
Dan Stozae22aec72016-08-01 13:20:59 -07001489void Layer::miniDumpHeader(String8& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001490 result.append("-------------------------------");
1491 result.append("-------------------------------");
1492 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001493 result.append(" Layer name\n");
1494 result.append(" Z | ");
1495 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001496 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001497 result.append(" Disp Frame (LTRB) | ");
1498 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001499 result.append("-------------------------------");
1500 result.append("-------------------------------");
1501 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001502}
1503
Dominik Laskowski7e045462018-05-30 13:02:02 -07001504void Layer::miniDump(String8& result, int32_t displayId) const {
1505 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stozae22aec72016-08-01 13:20:59 -07001506 return;
1507 }
1508
1509 String8 name;
1510 if (mName.length() > 77) {
1511 std::string shortened;
1512 shortened.append(mName.string(), 36);
1513 shortened.append("[...]");
1514 shortened.append(mName.string() + (mName.length() - 36), 36);
1515 name = shortened.c_str();
1516 } else {
1517 name = mName;
1518 }
1519
1520 result.appendFormat(" %s\n", name.string());
1521
1522 const Layer::State& layerState(getDrawingState());
Lloyd Pique074e8122018-07-26 12:57:23 -07001523 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(displayId);
Chia-I Wu1e043612018-03-01 09:45:09 -08001524 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
1525 result.appendFormat(" rel %6d | ", layerState.z);
1526 } else {
1527 result.appendFormat(" %10d | ", layerState.z);
1528 }
Dominik Laskowski7e045462018-05-30 13:02:02 -07001529 result.appendFormat("%10s | ", to_string(getCompositionType(displayId)).c_str());
Lloyd Pique074e8122018-07-26 12:57:23 -07001530 result.appendFormat("%10s | ", to_string(hwcInfo.transform).c_str());
1531 const Rect& frame = hwcInfo.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001532 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique074e8122018-07-26 12:57:23 -07001533 const FloatRect& crop = hwcInfo.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001534 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 -07001535
David Sodman7e4ae112018-02-09 15:02:28 -08001536 result.append("- - - - - - - - - - - - - - - -\n");
1537
1538 std::string compositionInfoStr;
1539 getBE().compositionInfo.dump(compositionInfoStr, "compositionInfo");
1540 result.append(compositionInfoStr.c_str());
1541
Yichi Chen6ca35192018-05-29 12:20:43 +08001542 result.append("- - - - - - - - - - - - - - - -");
1543 result.append("- - - - - - - - - - - - - - - -");
1544 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001545}
Dan Stozae22aec72016-08-01 13:20:59 -07001546
Svetoslavd85084b2014-03-20 10:28:31 -07001547void Layer::dumpFrameStats(String8& result) const {
1548 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001549}
1550
Svetoslavd85084b2014-03-20 10:28:31 -07001551void Layer::clearFrameStats() {
1552 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001553}
1554
Jamie Gennis6547ff42013-07-16 20:12:42 -07001555void Layer::logFrameStats() {
1556 mFrameTracker.logAndResetStats(mName);
1557}
1558
Svetoslavd85084b2014-03-20 10:28:31 -07001559void Layer::getFrameStats(FrameStats* outStats) const {
1560 mFrameTracker.getStats(outStats);
1561}
1562
Brian Andersond6927fb2016-07-23 23:37:30 -07001563void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001564 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001565 Mutex::Autolock lock(mFrameEventHistoryMutex);
1566 mFrameEventHistory.checkFencesForCompletion();
1567 mFrameEventHistory.dump(result);
1568}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001569
Brian Anderson5ea5e592016-12-01 16:54:33 -08001570void Layer::onDisconnect() {
1571 Mutex::Autolock lock(mFrameEventHistoryMutex);
1572 mFrameEventHistory.onDisconnect();
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001573 mTimeStats.onDisconnect(getName().c_str());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001574}
1575
Brian Anderson3890c392016-07-25 12:48:08 -07001576void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001577 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001578 if (newTimestamps) {
1579 mTimeStats.setPostTime(getName().c_str(), newTimestamps->frameNumber,
1580 newTimestamps->postedTime);
1581 }
1582
Brian Andersond6927fb2016-07-23 23:37:30 -07001583 Mutex::Autolock lock(mFrameEventHistoryMutex);
1584 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001585 // If there are any unsignaled fences in the aquire timeline at this
1586 // point, the previously queued frame hasn't been latched yet. Go ahead
1587 // and try to get the signal time here so the syscall is taken out of
1588 // the main thread's critical path.
1589 mAcquireTimeline.updateSignalTimes();
1590 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001591 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001592 mFrameEventHistory.addQueue(*newTimestamps);
1593 }
1594
Brian Anderson3890c392016-07-25 12:48:08 -07001595 if (outDelta) {
1596 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001597 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001598}
Dan Stozae77c7662016-05-13 11:37:28 -07001599
Chia-I Wu98f1c102017-05-30 14:54:08 -07001600size_t Layer::getChildrenCount() const {
1601 size_t count = 0;
1602 for (const sp<Layer>& child : mCurrentChildren) {
1603 count += 1 + child->getChildrenCount();
1604 }
1605 return count;
1606}
1607
Robert Carr1f0a16a2016-10-24 16:27:39 -07001608void Layer::addChild(const sp<Layer>& layer) {
1609 mCurrentChildren.add(layer);
1610 layer->setParent(this);
1611}
1612
1613ssize_t Layer::removeChild(const sp<Layer>& layer) {
1614 layer->setParent(nullptr);
1615 return mCurrentChildren.remove(layer);
1616}
1617
Robert Carr1db73f62016-12-21 12:58:51 -08001618bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1619 sp<Handle> handle = nullptr;
1620 sp<Layer> newParent = nullptr;
1621 if (newParentHandle == nullptr) {
1622 return false;
1623 }
1624 handle = static_cast<Handle*>(newParentHandle.get());
1625 newParent = handle->owner.promote();
1626 if (newParent == nullptr) {
1627 ALOGE("Unable to promote Layer handle");
1628 return false;
1629 }
1630
1631 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001632 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001633
1634 sp<Client> client(child->mClientRef.promote());
1635 if (client != nullptr) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001636 client->updateParent(newParent);
Robert Carr1db73f62016-12-21 12:58:51 -08001637 }
1638 }
1639 mCurrentChildren.clear();
1640
1641 return true;
1642}
1643
Robert Carr15eae092018-03-23 13:43:53 -07001644void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001645 for (const sp<Layer>& child : mDrawingChildren) {
1646 child->mDrawingParent = newParent;
1647 }
1648}
1649
chaviwf1961f72017-09-18 16:41:07 -07001650bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1651 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001652 return false;
1653 }
1654
1655 auto handle = static_cast<Handle*>(newParentHandle.get());
1656 sp<Layer> newParent = handle->owner.promote();
1657 if (newParent == nullptr) {
1658 ALOGE("Unable to promote Layer handle");
1659 return false;
1660 }
1661
chaviwf1961f72017-09-18 16:41:07 -07001662 sp<Layer> parent = getParent();
1663 if (parent != nullptr) {
1664 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001665 }
chaviwf1961f72017-09-18 16:41:07 -07001666 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001667
chaviwf1961f72017-09-18 16:41:07 -07001668 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001669 sp<Client> newParentClient(newParent->mClientRef.promote());
1670
chaviwf1961f72017-09-18 16:41:07 -07001671 if (client != newParentClient) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001672 client->updateParent(newParent);
chaviw06178942017-07-27 10:25:59 -07001673 }
1674
chaviw06178942017-07-27 10:25:59 -07001675 return true;
1676}
1677
Robert Carr9524cb32017-02-13 11:32:32 -08001678bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001679 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001680 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001681 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001682 if (client != nullptr && parentClient != client) {
Robert Carr7f619b22017-11-06 12:56:35 -08001683 client->detachLayer(child.get());
1684 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001685 }
Robert Carr7f619b22017-11-06 12:56:35 -08001686 }
Robert Carr9524cb32017-02-13 11:32:32 -08001687
1688 return true;
1689}
1690
Chia-I Wu11481472018-05-04 10:43:19 -07001691bool Layer::isLegacyDataSpace() const {
1692 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001693 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001694 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001695}
1696
Robert Carr1f0a16a2016-10-24 16:27:39 -07001697void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001698 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001699}
1700
1701void Layer::clearSyncPoints() {
1702 for (const auto& child : mCurrentChildren) {
1703 child->clearSyncPoints();
1704 }
1705
1706 Mutex::Autolock lock(mLocalSyncPointMutex);
1707 for (auto& point : mLocalSyncPoints) {
1708 point->setFrameAvailable();
1709 }
1710 mLocalSyncPoints.clear();
1711}
1712
1713int32_t Layer::getZ() const {
1714 return mDrawingState.z;
1715}
1716
Robert Carr29abff82017-12-04 13:51:20 -08001717bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1718 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1719 const State& state = useDrawing ? mDrawingState : mCurrentState;
1720 return state.zOrderRelativeOf != nullptr;
1721}
1722
David Sodman41fdfc92017-11-06 16:09:56 -08001723__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001724 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001725 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1726 "makeTraversalList received invalid stateSet");
1727 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1728 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1729 const State& state = useDrawing ? mDrawingState : mCurrentState;
1730
Robert Carr29abff82017-12-04 13:51:20 -08001731 if (state.zOrderRelatives.size() == 0) {
1732 *outSkipRelativeZUsers = true;
1733 return children;
1734 }
1735
chaviwfd462612018-05-31 16:11:27 -07001736 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001737 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001738 sp<Layer> strongRelative = weakRelative.promote();
1739 if (strongRelative != nullptr) {
1740 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001741 }
1742 }
1743
Dan Stoza412903f2017-04-27 13:42:17 -07001744 for (const sp<Layer>& child : children) {
Robert Carr503c7042017-09-27 15:06:08 -07001745 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1746 if (childState.zOrderRelativeOf != nullptr) {
1747 continue;
1748 }
Robert Carrdb66e622017-04-10 16:55:57 -07001749 traverse.add(child);
1750 }
1751
1752 return traverse;
1753}
1754
Robert Carr1f0a16a2016-10-24 16:27:39 -07001755/**
Robert Carrdb66e622017-04-10 16:55:57 -07001756 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001757 */
Dan Stoza412903f2017-04-27 13:42:17 -07001758void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001759 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1760 // produce a new list for traversing, including our relatives, and not including our children
1761 // who are relatives of another surface. In the case that there are no relative Z,
1762 // makeTraversalList returns our children directly to avoid significant overhead.
1763 // However in this case we need to take the responsibility for filtering children which
1764 // are relatives of another surface here.
1765 bool skipRelativeZUsers = false;
1766 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001767
Robert Carr1f0a16a2016-10-24 16:27:39 -07001768 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001769 for (; i < list.size(); i++) {
1770 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001771 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1772 continue;
1773 }
1774
Robert Carrdb66e622017-04-10 16:55:57 -07001775 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001776 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001777 }
Dan Stoza412903f2017-04-27 13:42:17 -07001778 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001779 }
Robert Carr29abff82017-12-04 13:51:20 -08001780
Dan Stoza412903f2017-04-27 13:42:17 -07001781 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001782 for (; i < list.size(); i++) {
1783 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001784
1785 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1786 continue;
1787 }
Dan Stoza412903f2017-04-27 13:42:17 -07001788 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001789 }
1790}
1791
1792/**
Robert Carrdb66e622017-04-10 16:55:57 -07001793 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001794 */
Dan Stoza412903f2017-04-27 13:42:17 -07001795void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1796 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001797 // See traverseInZOrder for documentation.
1798 bool skipRelativeZUsers = false;
1799 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001800
Robert Carr1f0a16a2016-10-24 16:27:39 -07001801 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001802 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001803 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001804
1805 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1806 continue;
1807 }
1808
Robert Carrdb66e622017-04-10 16:55:57 -07001809 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001810 break;
1811 }
Dan Stoza412903f2017-04-27 13:42:17 -07001812 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001813 }
Dan Stoza412903f2017-04-27 13:42:17 -07001814 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001815 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001816 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001817
1818 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1819 continue;
1820 }
1821
Dan Stoza412903f2017-04-27 13:42:17 -07001822 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001823 }
1824}
1825
chaviw4b129c22018-04-09 16:19:43 -07001826LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1827 const std::vector<Layer*>& layersInTree) {
1828 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1829 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001830 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1831 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
chaviw4b129c22018-04-09 16:19:43 -07001832 const State& state = useDrawing ? mDrawingState : mCurrentState;
1833
chaviwfd462612018-05-31 16:11:27 -07001834 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001835 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1836 sp<Layer> strongRelative = weakRelative.promote();
1837 // Only add relative layers that are also descendents of the top most parent of the tree.
1838 // If a relative layer is not a descendent, then it should be ignored.
1839 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1840 traverse.add(strongRelative);
1841 }
1842 }
1843
1844 for (const sp<Layer>& child : children) {
1845 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1846 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1847 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1848 // the child here since it won't be added later as a relative.
1849 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1850 childState.zOrderRelativeOf.promote().get())) {
1851 continue;
1852 }
1853 traverse.add(child);
1854 }
1855
1856 return traverse;
1857}
1858
1859void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1860 LayerVector::StateSet stateSet,
1861 const LayerVector::Visitor& visitor) {
1862 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001863
1864 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001865 for (; i < list.size(); i++) {
1866 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001867 if (relative->getZ() >= 0) {
1868 break;
1869 }
chaviw4b129c22018-04-09 16:19:43 -07001870 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001871 }
chaviw4b129c22018-04-09 16:19:43 -07001872
chaviwa76b2712017-09-20 12:02:26 -07001873 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001874 for (; i < list.size(); i++) {
1875 const auto& relative = list[i];
1876 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001877 }
1878}
1879
chaviw4b129c22018-04-09 16:19:43 -07001880std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1881 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1882 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1883
1884 std::vector<Layer*> layersInTree = {this};
1885 for (size_t i = 0; i < children.size(); i++) {
1886 const auto& child = children[i];
1887 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1888 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1889 }
1890
1891 return layersInTree;
1892}
1893
1894void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1895 const LayerVector::Visitor& visitor) {
1896 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1897 std::sort(layersInTree.begin(), layersInTree.end());
1898 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1899}
1900
Peiyong Linefefaac2018-08-17 12:27:51 -07001901ui::Transform Layer::getTransform() const {
1902 ui::Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001903 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001904 if (p != nullptr) {
1905 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001906
1907 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1908 // it isFixedSize) then there may be additional scaling not accounted
1909 // for in the transform. We need to mirror this scaling in child surfaces
1910 // or we will break the contract where WM can treat child surfaces as
1911 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08001912 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001913 int bufferWidth;
1914 int bufferHeight;
1915 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08001916 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
1917 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001918 } else {
David Sodman0cc69182017-11-17 12:12:07 -08001919 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
1920 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001921 }
Marissa Wall61c58622018-07-18 10:12:20 -07001922 float sx = p->getActiveWidth(p->getDrawingState()) / static_cast<float>(bufferWidth);
1923 float sy = p->getActiveHeight(p->getDrawingState()) / static_cast<float>(bufferHeight);
Peiyong Linefefaac2018-08-17 12:27:51 -07001924 ui::Transform extraParentScaling;
Robert Carr9b429f42017-04-17 14:56:57 -07001925 extraParentScaling.set(sx, 0, 0, sy);
1926 t = t * extraParentScaling;
1927 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001928 }
Marissa Wall61c58622018-07-18 10:12:20 -07001929 return t * getActiveTransform(getDrawingState());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001930}
1931
chaviw13fdc492017-06-27 12:40:18 -07001932half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001933 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001934
chaviw13fdc492017-06-27 12:40:18 -07001935 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1936 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001937}
Robert Carr6452f122017-03-21 10:41:29 -07001938
chaviw13fdc492017-06-27 12:40:18 -07001939half4 Layer::getColor() const {
1940 const half4 color(getDrawingState().color);
1941 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001942}
Robert Carr6452f122017-03-21 10:41:29 -07001943
Robert Carr1f0a16a2016-10-24 16:27:39 -07001944void Layer::commitChildList() {
1945 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1946 const auto& child = mCurrentChildren[i];
1947 child->commitChildList();
1948 }
1949 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001950 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001951}
1952
chaviw1d044282017-09-27 12:19:28 -07001953void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1954 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1955 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1956 const State& state = useDrawing ? mDrawingState : mCurrentState;
1957
Peiyong Linefefaac2018-08-17 12:27:51 -07001958 ui::Transform requestedTransform = state.active_legacy.transform;
1959 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07001960
1961 layerInfo->set_id(sequence);
1962 layerInfo->set_name(getName().c_str());
1963 layerInfo->set_type(String8(getTypeId()));
1964
1965 for (const auto& child : children) {
1966 layerInfo->add_children(child->sequence);
1967 }
1968
1969 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1970 sp<Layer> strongRelative = weakRelative.promote();
1971 if (strongRelative != nullptr) {
1972 layerInfo->add_relatives(strongRelative->sequence);
1973 }
1974 }
1975
Marissa Wallf58c14b2018-07-24 10:50:43 -07001976 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
chaviw1d044282017-09-27 12:19:28 -07001977 layerInfo->mutable_transparent_region());
1978 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
1979 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
1980
1981 layerInfo->set_layer_stack(getLayerStack());
1982 layerInfo->set_z(state.z);
1983
1984 PositionProto* position = layerInfo->mutable_position();
1985 position->set_x(transform.tx());
1986 position->set_y(transform.ty());
1987
1988 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
1989 requestedPosition->set_x(requestedTransform.tx());
1990 requestedPosition->set_y(requestedTransform.ty());
1991
1992 SizeProto* size = layerInfo->mutable_size();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001993 size->set_w(state.active_legacy.w);
1994 size->set_h(state.active_legacy.h);
chaviw1d044282017-09-27 12:19:28 -07001995
Marissa Wallf58c14b2018-07-24 10:50:43 -07001996 LayerProtoHelper::writeToProto(state.crop_legacy, layerInfo->mutable_crop());
1997 LayerProtoHelper::writeToProto(state.finalCrop_legacy, layerInfo->mutable_final_crop());
chaviw1d044282017-09-27 12:19:28 -07001998
1999 layerInfo->set_is_opaque(isOpaque(state));
2000 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07002001
2002 // XXX (b/79210409) mCurrentDataSpace is not protected
2003 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
2004
chaviw1d044282017-09-27 12:19:28 -07002005 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
2006 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
2007 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
2008 layerInfo->set_flags(state.flags);
2009
2010 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
2011 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
2012
Jorim Jaggi8e0af362017-11-14 16:28:28 +01002013 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07002014 if (parent != nullptr) {
2015 layerInfo->set_parent(parent->sequence);
2016 }
2017
2018 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
2019 if (zOrderRelativeOf != nullptr) {
2020 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
2021 }
2022
Chia-I Wu01591c92018-05-22 12:03:00 -07002023 // XXX getBE().compositionInfo.mBuffer is not protected
David Sodman0cc69182017-11-17 12:12:07 -08002024 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08002025 if (buffer != nullptr) {
2026 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
Peiyong Linefefaac2018-08-17 12:27:51 -07002027 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
Yichi Chen6ca35192018-05-29 12:20:43 +08002028 layerInfo->mutable_buffer_transform());
chaviw1d044282017-09-27 12:19:28 -07002029 }
2030
2031 layerInfo->set_queued_frames(getQueuedFrameCount());
2032 layerInfo->set_refresh_pending(isBufferLatched());
rongliucfb187b2018-03-14 12:26:23 -07002033 layerInfo->set_window_type(state.type);
2034 layerInfo->set_app_id(state.appId);
chaviwadc40c22018-07-10 16:57:27 -07002035 layerInfo->set_curr_frame(mCurrentFrameNumber);
2036
2037 for (const auto& pendingState : mPendingStates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07002038 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
chaviwadc40c22018-07-10 16:57:27 -07002039 if (barrierLayer != nullptr) {
2040 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
2041 barrierLayerProto->set_id(barrierLayer->sequence);
Marissa Wallf58c14b2018-07-24 10:50:43 -07002042 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
chaviwadc40c22018-07-10 16:57:27 -07002043 }
2044 }
chaviw1d044282017-09-27 12:19:28 -07002045}
2046
Dominik Laskowski7e045462018-05-30 13:02:02 -07002047void Layer::writeToProto(LayerProto* layerInfo, int32_t displayId) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07002048 if (!hasHwcLayer(displayId)) {
2049 return;
2050 }
2051
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002052 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
2053
Dominik Laskowski7e045462018-05-30 13:02:02 -07002054 const auto& hwcInfo = getBE().mHwcLayers.at(displayId);
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002055
2056 const Rect& frame = hwcInfo.displayFrame;
2057 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
2058
2059 const FloatRect& crop = hwcInfo.sourceCrop;
2060 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
2061
2062 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
2063 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002064
2065 const int32_t compositionType = static_cast<int32_t>(hwcInfo.compositionType);
2066 layerInfo->set_hwc_composition_type(compositionType);
2067
2068 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2069 static_cast<BufferLayer*>(this)->isProtected()) {
2070 layerInfo->set_is_protected(true);
2071 } else {
2072 layerInfo->set_is_protected(false);
2073 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002074}
2075
Mathias Agopian13127d82013-03-05 17:47:11 -08002076// ---------------------------------------------------------------------------
2077
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002078}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002079
2080#if defined(__gl_h_)
2081#error "don't include gl/gl.h in this file"
2082#endif
2083
2084#if defined(__gl2_h_)
2085#error "don't include gl2/gl2.h in this file"
2086#endif