blob: 63a010d72519359b5fd7610b3f662725381a649d [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dan Stoza9e56aa02015-11-02 13:00:03 -080017//#define LOG_NDEBUG 0
18#undef LOG_TAG
19#define LOG_TAG "Layer"
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080020#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
Mathias Agopian13127d82013-03-05 17:47:11 -080022#include <math.h>
David Sodman41fdfc92017-11-06 16:09:56 -080023#include <stdint.h>
24#include <stdlib.h>
25#include <sys/types.h>
chaviw4b129c22018-04-09 16:19:43 -070026#include <algorithm>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027
Mathias Agopiana67932f2011-04-20 14:20:59 -070028#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070029#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070030#include <cutils/properties.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031
32#include <utils/Errors.h>
33#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080034#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080036#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037
Courtney Goeltzenleuchter36c44dc2017-04-14 09:33:16 -060038#include <ui/DebugUtils.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070039#include <ui/GraphicBuffer.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080040#include <ui/PixelFormat.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080041
Dan Stoza6b9454d2014-11-07 16:00:59 -080042#include <gui/BufferItem.h>
Kalle Raitaa099a242017-01-11 11:17:29 -080043#include <gui/LayerDebugInfo.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080044#include <gui/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045
Yiwei Zhang60d1a192018-03-07 14:52:28 -080046#include "BufferLayer.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020047#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070048#include "DisplayDevice.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include "Layer.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070050#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070051#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
Mathias Agopian1b031492012-06-20 17:51:20 -070054#include "DisplayHardware/HWComposer.h"
55
Peiyong Lincbc184f2018-08-22 13:24:10 -070056#include <renderengine/RenderEngine.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070057
Dan Stozac5da2712016-07-20 15:38:12 -070058#include <mutex>
chaviw1d044282017-09-27 12:19:28 -070059#include "LayerProtoHelper.h"
Dan Stozac5da2712016-07-20 15:38:12 -070060
David Sodman41fdfc92017-11-06 16:09:56 -080061#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063namespace android {
64
Lloyd Piquef1c675b2018-09-12 20:45:39 -070065std::atomic<int32_t> Layer::sSequence{1};
Mathias Agopian13127d82013-03-05 17:47:11 -080066
Lloyd Pique42ab75e2018-09-12 20:46:03 -070067Layer::Layer(const LayerCreationArgs& args)
68 : mFlinger(args.flinger),
69 mName(args.name),
70 mClientRef(args.client),
71 mBE{this, args.name.string()} {
Mathias Agopiana67932f2011-04-20 14:20:59 -070072 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -070073
74 uint32_t layerFlags = 0;
Lloyd Pique42ab75e2018-09-12 20:46:03 -070075 if (args.flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
76 if (args.flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
77 if (args.flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070078
Dan Stozaf7ba41a2017-05-10 15:11:11 -070079 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070080
Lloyd Pique42ab75e2018-09-12 20:46:03 -070081 mCurrentState.active_legacy.w = args.w;
82 mCurrentState.active_legacy.h = args.h;
David Sodman0c69cad2017-08-21 12:12:51 -070083 mCurrentState.flags = layerFlags;
Marissa Wallf58c14b2018-07-24 10:50:43 -070084 mCurrentState.active_legacy.transform.set(0, 0);
85 mCurrentState.crop_legacy.makeInvalid();
Marissa Wallf58c14b2018-07-24 10:50:43 -070086 mCurrentState.requestedCrop_legacy = mCurrentState.crop_legacy;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070087 mCurrentState.z = 0;
chaviw13fdc492017-06-27 12:40:18 -070088 mCurrentState.color.a = 1.0f;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070089 mCurrentState.layerStack = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -070090 mCurrentState.sequence = 0;
Marissa Wallf58c14b2018-07-24 10:50:43 -070091 mCurrentState.requested_legacy = mCurrentState.active_legacy;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -050092 mCurrentState.appId = 0;
93 mCurrentState.type = 0;
Marissa Wall61c58622018-07-18 10:12:20 -070094 mCurrentState.active.w = 0;
95 mCurrentState.active.h = 0;
96 mCurrentState.active.transform.set(0, 0);
97 mCurrentState.transform = 0;
98 mCurrentState.transformToDisplayInverse = false;
99 mCurrentState.crop.makeInvalid();
100 mCurrentState.acquireFence = new Fence(-1);
101 mCurrentState.dataspace = ui::Dataspace::UNKNOWN;
102 mCurrentState.hdrMetadata.validTypes = 0;
103 mCurrentState.surfaceDamageRegion.clear();
104 mCurrentState.api = -1;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700105
106 // drawing state & current state are identical
107 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700108
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800109 CompositorTiming compositorTiming;
Lloyd Pique42ab75e2018-09-12 20:46:03 -0700110 args.flinger->getCompositorTiming(&compositorTiming);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800111 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jorim Jaggibd6480f2018-08-10 14:37:31 +0200112 mFrameTracker.setDisplayRefreshPeriod(compositorTiming.interval);
Dan Stoza436ccf32018-06-21 12:10:12 -0700113}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700114
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700115Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800116 sp<Client> c(mClientRef.promote());
117 if (c != 0) {
118 c->detachLayer(this);
119 }
120
Jamie Gennis6547ff42013-07-16 20:12:42 -0700121 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700122}
123
Mathias Agopian13127d82013-03-05 17:47:11 -0800124// ---------------------------------------------------------------------------
125// callbacks
126// ---------------------------------------------------------------------------
127
David Sodmaneb085e02017-10-05 18:49:04 -0700128/*
129 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
130 * Layer. So, the implementation is done in BufferLayer. When called on a
131 * ColorLayer object, it's essentially a NOP.
132 */
David Sodmaneb085e02017-10-05 18:49:04 -0700133void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800134
Chia-I Wuc6657022017-08-15 11:18:17 -0700135void Layer::onRemovedFromCurrentState() {
136 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
137
Robert Carr5edb1ad2017-04-25 10:54:24 -0700138 if (mCurrentState.zOrderRelativeOf != nullptr) {
139 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
140 if (strongRelative != nullptr) {
141 strongRelative->removeZOrderRelative(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700142 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700143 }
144 mCurrentState.zOrderRelativeOf = nullptr;
145 }
Robert Carr43406072018-09-19 13:22:47 -0700146
Chia-I Wuc6657022017-08-15 11:18:17 -0700147 for (const auto& child : mCurrentChildren) {
148 child->onRemovedFromCurrentState();
149 }
150}
Chia-I Wu38512252017-05-17 14:36:16 -0700151
Chia-I Wuc6657022017-08-15 11:18:17 -0700152void Layer::onRemoved() {
153 // the layer is removed from SF mLayersPendingRemoval
David Sodmaneb085e02017-10-05 18:49:04 -0700154 abandon();
Chia-I Wuc6657022017-08-15 11:18:17 -0700155
Steven Thomasb02664d2017-07-26 18:48:28 -0700156 destroyAllHwcLayers();
Chia-I Wu38512252017-05-17 14:36:16 -0700157
Robert Carr43406072018-09-19 13:22:47 -0700158 mRemoved = true;
159
160 for (auto& point : mRemoteSyncPoints) {
161 point->setTransactionApplied();
162 }
163 for (auto& point : mLocalSyncPoints) {
164 point->setFrameAvailable();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700165 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700166}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700167
Mathias Agopian13127d82013-03-05 17:47:11 -0800168// ---------------------------------------------------------------------------
169// set-up
170// ---------------------------------------------------------------------------
171
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700172const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800173 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174}
175
chaviw13fdc492017-06-27 12:40:18 -0700176bool Layer::getPremultipledAlpha() const {
177 return mPremultipliedAlpha;
178}
179
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700180sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800181 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700182 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800183}
184
185// ---------------------------------------------------------------------------
186// h/w composer set-up
187// ---------------------------------------------------------------------------
188
Dominik Laskowski7e045462018-05-30 13:02:02 -0700189bool Layer::createHwcLayer(HWComposer* hwc, int32_t displayId) {
190 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(displayId) != 0,
191 "Already have a layer for display %d", displayId);
David Sodmanb8aaea12017-12-14 15:54:51 -0800192 auto layer = std::shared_ptr<HWC2::Layer>(
193 hwc->createLayer(displayId),
194 [hwc, displayId](HWC2::Layer* layer) {
195 hwc->destroyLayer(displayId, layer); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700196 if (!layer) {
197 return false;
198 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700199 LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers[displayId];
Steven Thomasb02664d2017-07-26 18:48:28 -0700200 hwcInfo.hwc = hwc;
201 hwcInfo.layer = layer;
David Sodmanf6a38932018-05-25 15:27:50 -0700202 layer->setLayerDestroyedListener(
Dominik Laskowski7e045462018-05-30 13:02:02 -0700203 [this, displayId](HWC2::Layer* /*layer*/) { getBE().mHwcLayers.erase(displayId); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700204 return true;
205}
206
Dominik Laskowski7e045462018-05-30 13:02:02 -0700207bool Layer::destroyHwcLayer(int32_t displayId) {
208 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu83806892017-11-16 10:50:20 -0800209 return false;
Steven Thomasb02664d2017-07-26 18:48:28 -0700210 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700211 auto& hwcInfo = getBE().mHwcLayers[displayId];
David Sodman41fdfc92017-11-06 16:09:56 -0800212 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr, "Attempt to destroy null layer");
Steven Thomasb02664d2017-07-26 18:48:28 -0700213 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
David Sodmanb8aaea12017-12-14 15:54:51 -0800214 hwcInfo.layer = nullptr;
215
Chia-I Wu83806892017-11-16 10:50:20 -0800216 return true;
Steven Thomasb02664d2017-07-26 18:48:28 -0700217}
218
219void Layer::destroyAllHwcLayers() {
David Sodman6f65f3e2017-11-03 14:28:09 -0700220 size_t numLayers = getBE().mHwcLayers.size();
Steven Thomasb02664d2017-07-26 18:48:28 -0700221 for (size_t i = 0; i < numLayers; ++i) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700222 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.empty(), "destroyAllHwcLayers failed");
223 destroyHwcLayer(getBE().mHwcLayers.begin()->first);
Steven Thomasb02664d2017-07-26 18:48:28 -0700224 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700225 LOG_ALWAYS_FATAL_IF(!getBE().mHwcLayers.empty(),
David Sodman41fdfc92017-11-06 16:09:56 -0800226 "All hardware composer layers should have been destroyed");
Steven Thomasb02664d2017-07-26 18:48:28 -0700227}
Steven Thomasb02664d2017-07-26 18:48:28 -0700228
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800229Rect Layer::getContentCrop() const {
230 // this is the crop rectangle that applies to the buffer
231 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700232 Rect crop;
233 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800234 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700235 crop = mCurrentCrop;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800236 } else if (getBE().compositionInfo.mBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800237 // otherwise we use the whole buffer
David Sodman0cc69182017-11-17 12:12:07 -0800238 crop = getBE().compositionInfo.mBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700239 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800240 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700241 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700242 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700243 return crop;
244}
245
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700246static Rect reduce(const Rect& win, const Region& exclude) {
247 if (CC_LIKELY(exclude.isEmpty())) {
248 return win;
249 }
250 if (exclude.isRect()) {
251 return win.reduce(exclude.getBounds());
252 }
253 return Region(win).subtract(exclude).getBounds();
254}
255
Dan Stoza80d61162017-12-20 15:57:52 -0800256static FloatRect reduce(const FloatRect& win, const Region& exclude) {
257 if (CC_LIKELY(exclude.isEmpty())) {
258 return win;
259 }
260 // Convert through Rect (by rounding) for lack of FloatRegion
261 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
262}
263
Robert Carr1f0a16a2016-10-24 16:27:39 -0700264Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
Alec Mourib416efd2018-09-06 21:01:59 +0000265 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700266 Rect win(getActiveWidth(s), getActiveHeight(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700267
Marissa Wall61c58622018-07-18 10:12:20 -0700268 Rect crop = getCrop(s);
269 if (!crop.isEmpty()) {
270 win.intersect(crop, &win);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700271 }
272
Peiyong Linefefaac2018-08-17 12:27:51 -0700273 ui::Transform t = getTransform();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700274 win = t.transform(win);
275
Chia-I Wue41dbe62017-06-13 14:10:56 -0700276 const sp<Layer>& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700277 // Now we need to calculate the parent bounds, so we can clip ourselves to those.
278 // When calculating the parent bounds for purposes of clipping,
279 // we don't need to constrain the parent to its transparent region.
280 // The transparent region is an optimization based on the
281 // buffer contents of the layer, but does not affect the space allocated to
282 // it by policy, and thus children should be allowed to extend into the
283 // parent's transparent region. In fact one of the main uses, is to reduce
284 // buffer allocation size in cases where a child window sits behind a main window
285 // (by marking the hole in the parent window as a transparent region)
286 if (p != nullptr) {
287 Rect bounds = p->computeScreenBounds(false);
288 bounds.intersect(win, &win);
289 }
290
291 if (reduceTransparentRegion) {
Marissa Wall61c58622018-07-18 10:12:20 -0700292 auto const screenTransparentRegion = t.transform(getActiveTransparentRegion(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700293 win = reduce(win, screenTransparentRegion);
294 }
295
296 return win;
297}
298
Dan Stoza80d61162017-12-20 15:57:52 -0800299FloatRect Layer::computeBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000300 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700301 return computeBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700302}
303
Dan Stoza80d61162017-12-20 15:57:52 -0800304FloatRect Layer::computeBounds(const Region& activeTransparentRegion) const {
Alec Mourib416efd2018-09-06 21:01:59 +0000305 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700306 Rect win(getActiveWidth(s), getActiveHeight(s));
Robert Carrb5d3d262016-03-25 15:08:13 -0700307
Marissa Wall61c58622018-07-18 10:12:20 -0700308 Rect crop = getCrop(s);
309 if (!crop.isEmpty()) {
310 win.intersect(crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800311 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700312
Chia-I Wue41dbe62017-06-13 14:10:56 -0700313 const auto& p = mDrawingParent.promote();
Robert Carrd4ae7f32018-06-07 16:10:57 -0700314 FloatRect floatWin = win.toFloatRect();
315 FloatRect parentBounds = floatWin;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700316 if (p != nullptr) {
Robert Carrd4ae7f32018-06-07 16:10:57 -0700317 // We pass an empty Region here for reasons mirroring that of the case described in
318 // the computeScreenBounds reduceTransparentRegion=false case.
319 parentBounds = p->computeBounds(Region());
Robert Carr1f0a16a2016-10-24 16:27:39 -0700320 }
321
Peiyong Linefefaac2018-08-17 12:27:51 -0700322 ui::Transform t = s.active_legacy.transform;
Dan Stoza80d61162017-12-20 15:57:52 -0800323
Vishnu Nairdcce0e22018-08-23 08:35:19 -0700324 if (p != nullptr) {
Dan Stoza80d61162017-12-20 15:57:52 -0800325 floatWin = t.transform(floatWin);
Robert Carrd4ae7f32018-06-07 16:10:57 -0700326 floatWin = floatWin.intersect(parentBounds);
Dan Stoza80d61162017-12-20 15:57:52 -0800327 floatWin = t.inverse().transform(floatWin);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700328 }
329
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700330 // subtract the transparent region and snap to the bounds
Dan Stoza80d61162017-12-20 15:57:52 -0800331 return reduce(floatWin, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800332}
333
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700334Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& display) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700335 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800336 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700337 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800338
339 // apply the projection's clipping to the window crop in
340 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700341 // if there are no window scaling involved, this operation will map to full
342 // pixels in the buffer.
343 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
344 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700345
Marissa Wall61c58622018-07-18 10:12:20 -0700346 Rect activeCrop(getActiveWidth(s), getActiveHeight(s));
347 Rect crop = getCrop(s);
348 if (!crop.isEmpty()) {
349 activeCrop.intersect(crop, &activeCrop);
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700350 }
351
Peiyong Linefefaac2018-08-17 12:27:51 -0700352 ui::Transform t = getTransform();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700353 activeCrop = t.transform(activeCrop);
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700354 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000355 activeCrop.clear();
356 }
chaviwb1154d12017-10-31 14:15:36 -0700357
358 const auto& p = mDrawingParent.promote();
359 if (p != nullptr) {
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700360 auto parentCrop = p->computeInitialCrop(display);
chaviwb1154d12017-10-31 14:15:36 -0700361 activeCrop.intersect(parentCrop, &activeCrop);
362 }
363
Robert Carr1f0a16a2016-10-24 16:27:39 -0700364 return activeCrop;
365}
366
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700367FloatRect Layer::computeCrop(const sp<const DisplayDevice>& display) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700368 // the content crop is the area of the content that gets scaled to the
369 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800370 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700371
372 // In addition there is a WM-specified crop we pull from our drawing state.
373 const State& s(getDrawingState());
374
375 // Screen space to make reduction to parent crop clearer.
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700376 Rect activeCrop = computeInitialCrop(display);
Peiyong Linefefaac2018-08-17 12:27:51 -0700377 ui::Transform t = getTransform();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700378 // Back to layer space to work with the content crop.
379 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800380
Michael Lentine28ea2172014-11-19 18:32:37 -0800381 // This needs to be here as transform.transform(Rect) computes the
382 // transformed rect and then takes the bounding box of the result before
383 // returning. This means
384 // transform.inverse().transform(transform.transform(Rect)) != Rect
385 // in which case we need to make sure the final rect is clipped to the
386 // display bounds.
Marissa Wall61c58622018-07-18 10:12:20 -0700387 if (!activeCrop.intersect(Rect(getActiveWidth(s), getActiveHeight(s)), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000388 activeCrop.clear();
389 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800390
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700391 // subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700392 activeCrop = reduce(activeCrop, getActiveTransparentRegion(s));
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700393
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000394 // Transform the window crop to match the buffer coordinate system,
395 // which means using the inverse of the current transform set on the
396 // SurfaceFlingerConsumer.
397 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700398 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000399 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700400 * the code below applies the primary display's inverse transform to the
401 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000402 */
David Sodman41fdfc92017-11-06 16:09:56 -0800403 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000404 // calculate the inverse transform
405 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800406 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800407 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000408 // and apply to the current transform
Peiyong Linefefaac2018-08-17 12:27:51 -0700409 invTransform = (ui::Transform(invTransformOrient) *
410 ui::Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800411 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000412
Marissa Wall61c58622018-07-18 10:12:20 -0700413 int winWidth = getActiveWidth(s);
414 int winHeight = getActiveHeight(s);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000415 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
416 // If the activeCrop has been rotate the ends are rotated but not
417 // the space itself so when transforming ends back we can't rely on
418 // a modification of the axes of rotation. To account for this we
419 // need to reorient the inverse rotation in terms of the current
420 // axes of rotation.
421 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
422 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
423 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800424 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000425 }
Marissa Wall61c58622018-07-18 10:12:20 -0700426 winWidth = getActiveHeight(s);
427 winHeight = getActiveWidth(s);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000428 }
Marissa Wall61c58622018-07-18 10:12:20 -0700429 const Rect winCrop = activeCrop.transform(invTransform, getActiveWidth(s), getActiveHeight(s));
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000430
431 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800432 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000433 float yScale = crop.getHeight() / float(winHeight);
434
David Sodman41fdfc92017-11-06 16:09:56 -0800435 float insetL = winCrop.left * xScale;
436 float insetT = winCrop.top * yScale;
437 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000438 float insetB = (winHeight - winCrop.bottom) * yScale;
439
David Sodman41fdfc92017-11-06 16:09:56 -0800440 crop.left += insetL;
441 crop.top += insetT;
442 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000443 crop.bottom -= insetB;
444
Mathias Agopian13127d82013-03-05 17:47:11 -0800445 return crop;
446}
447
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700448void Layer::setGeometry(const sp<const DisplayDevice>& display, uint32_t z) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700449 const auto displayId = display->getId();
Peiyong Lin91b1df22018-06-18 18:00:16 -0700450 if (!hasHwcLayer(displayId)) {
451 ALOGE("[%s] failed to setGeometry: no HWC layer found (%d)",
452 mName.string(), displayId);
453 return;
454 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700455 auto& hwcInfo = getBE().mHwcLayers[displayId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700456
David Sodman10a41ff2018-08-05 12:14:17 -0700457 // Device or Cursor layers
458 if (mPotentialCursor) {
459 ALOGV("[%s] Requesting Cursor composition", mName.string());
460 setCompositionType(displayId, HWC2::Composition::Cursor);
461 } else {
462 ALOGV("[%s] Requesting Device composition", mName.string());
463 setCompositionType(displayId, HWC2::Composition::Device);
464 }
465
466 // Need to program geometry parts
467 getBE().compositionInfo.hwc.skipGeometry = false;
468
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700469 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800470 hwcInfo.forceClientComposition = false;
471
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700472 if (isSecure() && !display->isSecure()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800473 hwcInfo.forceClientComposition = true;
474 }
475
Mathias Agopian13127d82013-03-05 17:47:11 -0800476 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700477 const State& s(getDrawingState());
David Revemanecf0fa52017-03-03 11:32:44 -0500478 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700479 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800480 blendMode =
481 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800482 }
David Sodmanba340492018-08-05 21:51:33 -0700483 getBE().compositionInfo.hwc.blendMode = blendMode;
Mathias Agopian13127d82013-03-05 17:47:11 -0800484
485 // apply the layer's transform, followed by the display's global transform
486 // here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700487 Region activeTransparentRegion(getActiveTransparentRegion(s));
Peiyong Linefefaac2018-08-17 12:27:51 -0700488 ui::Transform t = getTransform();
Marissa Wall61c58622018-07-18 10:12:20 -0700489 Rect activeCrop = getCrop(s);
490 if (!activeCrop.isEmpty()) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700491 activeCrop = t.transform(activeCrop);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700492 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000493 activeCrop.clear();
494 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700495 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800496 // This needs to be here as transform.transform(Rect) computes the
497 // transformed rect and then takes the bounding box of the result before
498 // returning. This means
499 // transform.inverse().transform(transform.transform(Rect)) != Rect
500 // in which case we need to make sure the final rect is clipped to the
501 // display bounds.
Marissa Wall61c58622018-07-18 10:12:20 -0700502 if (!activeCrop.intersect(Rect(getActiveWidth(s), getActiveHeight(s)), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000503 activeCrop.clear();
504 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700505 // mark regions outside the crop as transparent
Marissa Wall61c58622018-07-18 10:12:20 -0700506 activeTransparentRegion.orSelf(Rect(0, 0, getActiveWidth(s), activeCrop.top));
Marissa Wallf58c14b2018-07-24 10:50:43 -0700507 activeTransparentRegion.orSelf(
Marissa Wall61c58622018-07-18 10:12:20 -0700508 Rect(0, activeCrop.bottom, getActiveWidth(s), getActiveHeight(s)));
David Sodman41fdfc92017-11-06 16:09:56 -0800509 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
510 activeTransparentRegion.orSelf(
Marissa Wall61c58622018-07-18 10:12:20 -0700511 Rect(activeCrop.right, activeCrop.top, getActiveWidth(s), activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700512 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700513
Dan Stoza80d61162017-12-20 15:57:52 -0800514 // computeBounds returns a FloatRect to provide more accuracy during the
515 // transformation. We then round upon constructing 'frame'.
516 Rect frame{t.transform(computeBounds(activeTransparentRegion))};
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700517 if (!frame.intersect(display->getViewport(), &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000518 frame.clear();
519 }
Peiyong Linefefaac2018-08-17 12:27:51 -0700520 const ui::Transform& tr = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800521 Rect transformedFrame = tr.transform(frame);
David Sodmanba340492018-08-05 21:51:33 -0700522 getBE().compositionInfo.hwc.displayFrame = transformedFrame;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800523
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700524 FloatRect sourceCrop = computeCrop(display);
David Sodmanba340492018-08-05 21:51:33 -0700525 getBE().compositionInfo.hwc.sourceCrop = sourceCrop;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800526
chaviw13fdc492017-06-27 12:40:18 -0700527 float alpha = static_cast<float>(getAlpha());
David Sodmanba340492018-08-05 21:51:33 -0700528 getBE().compositionInfo.hwc.alpha = alpha;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800529
David Sodmanba340492018-08-05 21:51:33 -0700530 getBE().compositionInfo.hwc.z = z;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500531
Albert Chaulk2a589632017-05-04 16:59:44 -0400532 int type = s.type;
533 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700534 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400535 if (parent.get()) {
536 auto& parentState = parent->getDrawingState();
rongliucfb187b2018-03-14 12:26:23 -0700537 if (parentState.type >= 0 || parentState.appId >= 0) {
538 type = parentState.type;
539 appId = parentState.appId;
540 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400541 }
542
David Sodmanba340492018-08-05 21:51:33 -0700543 getBE().compositionInfo.hwc.type = type;
544 getBE().compositionInfo.hwc.appId = appId;
545
Mathias Agopian29a367b2011-07-12 14:51:45 -0700546 /*
547 * Transformations are applied in this order:
548 * 1) buffer orientation/flip/mirror
549 * 2) state transformation (window manager)
550 * 3) layer orientation (screen orientation)
551 * (NOTE: the matrices are multiplied in reverse order)
552 */
553
Peiyong Linefefaac2018-08-17 12:27:51 -0700554 const ui::Transform bufferOrientation(mCurrentTransform);
555 ui::Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700556
Robert Carrcae605c2017-03-29 12:10:31 -0700557 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700558 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700559 * the code below applies the primary display's inverse transform to the
560 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700561 */
David Sodman41fdfc92017-11-06 16:09:56 -0800562 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700563 // calculate the inverse transform
564 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800565 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700566 }
Robert Carrcae605c2017-03-29 12:10:31 -0700567
568 /*
569 * Here we cancel out the orientation component of the WM transform.
570 * The scaling and translate components are already included in our bounds
571 * computation so it's enough to just omit it in the composition.
572 * See comment in onDraw with ref to b/36727915 for why.
573 */
Peiyong Linefefaac2018-08-17 12:27:51 -0700574 transform = ui::Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700575 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700576
577 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800578 const uint32_t orientation = transform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -0700579 if (orientation & ui::Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800580 // we can only handle simple transformation
Lloyd Pique074e8122018-07-26 12:57:23 -0700581 hwcInfo.forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800582 } else {
583 auto transform = static_cast<HWC2::Transform>(orientation);
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800584 hwcInfo.transform = transform;
David Sodmanba340492018-08-05 21:51:33 -0700585 getBE().compositionInfo.hwc.transform = transform;
David Sodman4b7c4bc2017-11-17 12:13:59 -0800586 }
587}
588
Dominik Laskowski7e045462018-05-30 13:02:02 -0700589void Layer::forceClientComposition(int32_t displayId) {
590 if (getBE().mHwcLayers.count(displayId) == 0) {
591 ALOGE("forceClientComposition: no HWC layer found (%d)", displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800592 return;
593 }
594
Dominik Laskowski7e045462018-05-30 13:02:02 -0700595 getBE().mHwcLayers[displayId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800596}
Dan Stozaee44edd2015-03-23 15:50:23 -0700597
Dominik Laskowski7e045462018-05-30 13:02:02 -0700598bool Layer::getForceClientComposition(int32_t displayId) {
599 if (getBE().mHwcLayers.count(displayId) == 0) {
600 ALOGE("getForceClientComposition: no HWC layer found (%d)", displayId);
chaviwc9232ed2017-11-14 15:31:15 -0800601 return false;
602 }
603
Dominik Laskowski7e045462018-05-30 13:02:02 -0700604 return getBE().mHwcLayers[displayId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800605}
606
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700607void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700608 const auto displayId = display->getId();
609 if (getBE().mHwcLayers.count(displayId) == 0 ||
610 getCompositionType(displayId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800611 return;
612 }
613
614 // This gives us only the "orientation" component of the transform
615 const State& s(getCurrentState());
616
617 // Apply the layer's transform, followed by the display's global transform
618 // Here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700619 Rect win(getActiveWidth(s), getActiveHeight(s));
620 Rect crop = getCrop(s);
621 if (!crop.isEmpty()) {
622 win.intersect(crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800623 }
624 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700625 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700626 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700627 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700628 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800629 auto position = displayTransform.transform(frame);
630
Dominik Laskowski7e045462018-05-30 13:02:02 -0700631 auto error =
David Sodmanb8aaea12017-12-14 15:54:51 -0800632 (getBE().mHwcLayers[displayId].layer)->setCursorPosition(
633 position.left, position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800634 ALOGE_IF(error != HWC2::Error::None,
635 "[%s] Failed to set cursor position "
636 "to (%d, %d): %s (%d)",
637 mName.string(), position.left, position.top, to_string(error).c_str(),
638 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800639}
Riley Andrews03414a12014-07-01 14:22:59 -0700640
Mathias Agopian13127d82013-03-05 17:47:11 -0800641// ---------------------------------------------------------------------------
642// drawing...
643// ---------------------------------------------------------------------------
644
Marissa Wall61c58622018-07-18 10:12:20 -0700645void Layer::draw(const RenderArea& renderArea, const Region& clip) {
chaviwa76b2712017-09-20 12:02:26 -0700646 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800647}
648
Marissa Wall61c58622018-07-18 10:12:20 -0700649void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) {
chaviwa76b2712017-09-20 12:02:26 -0700650 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800651}
652
David Sodman41fdfc92017-11-06 16:09:56 -0800653void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
654 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800655 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700656 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700657 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700658 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800659}
660
David Sodmanc1498e62018-09-12 14:36:26 -0700661void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
662 clearWithOpenGL(renderArea, 0, 0, 0, 0);
663}
664
David Sodman10a41ff2018-08-05 12:14:17 -0700665void Layer::setCompositionType(int32_t displayId, HWC2::Composition type, bool /*callIntoHwc*/) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700666 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu30505fb2018-03-26 16:20:31 -0700667 ALOGE("setCompositionType called without a valid HWC layer");
668 return;
David Sodman10a41ff2018-08-05 12:14:17 -0700669 } else {
670 if (getBE().mHwcLayers[displayId].compositionType != type) {
671 ALOGV("setCompositionType: Changing compositionType from %s to %s",
672 to_string(getBE().mHwcLayers[displayId].compositionType).c_str(),
673 to_string(type).c_str());
674 getBE().mHwcLayers[displayId].compositionType = type;
Chia-I Wu30505fb2018-03-26 16:20:31 -0700675 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800676 }
677}
678
Dominik Laskowski7e045462018-05-30 13:02:02 -0700679HWC2::Composition Layer::getCompositionType(int32_t displayId) const {
David Sodman15fb96e2018-01-07 10:23:24 -0800680 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700681 // If we're querying the composition type for a display that does not
682 // have a HWC counterpart, then it will always be Client
683 return HWC2::Composition::Client;
684 }
David Sodman15fb96e2018-01-07 10:23:24 -0800685 return getBE().mHwcLayers[displayId].compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800686}
687
Dominik Laskowski7e045462018-05-30 13:02:02 -0700688void Layer::setClearClientTarget(int32_t displayId, bool clear) {
689 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800690 ALOGE("setClearClientTarget called without a valid HWC layer");
691 return;
692 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700693 getBE().mHwcLayers[displayId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800694}
695
Dominik Laskowski7e045462018-05-30 13:02:02 -0700696bool Layer::getClearClientTarget(int32_t displayId) const {
697 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800698 ALOGE("getClearClientTarget called without a valid HWC layer");
699 return false;
700 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700701 return getBE().mHwcLayers.at(displayId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800702}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800703
Dan Stozacac35382016-01-27 12:21:06 -0800704bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
705 if (point->getFrameNumber() <= mCurrentFrameNumber) {
706 // Don't bother with a SyncPoint, since we've already latched the
707 // relevant frame
708 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700709 }
710
Dan Stozacac35382016-01-27 12:21:06 -0800711 Mutex::Autolock lock(mLocalSyncPointMutex);
712 mLocalSyncPoints.push_back(point);
713 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700714}
715
Mathias Agopian13127d82013-03-05 17:47:11 -0800716// ----------------------------------------------------------------------------
717// local state
718// ----------------------------------------------------------------------------
719
Peiyong Lin833074a2018-08-28 11:53:54 -0700720void Layer::computeGeometry(const RenderArea& renderArea,
721 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700722 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700723 const ui::Transform renderAreaTransform(renderArea.getTransform());
Dan Stoza80d61162017-12-20 15:57:52 -0800724 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700725
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000726 vec2 lt = vec2(win.left, win.top);
727 vec2 lb = vec2(win.left, win.bottom);
728 vec2 rb = vec2(win.right, win.bottom);
729 vec2 rt = vec2(win.right, win.top);
730
Peiyong Linefefaac2018-08-17 12:27:51 -0700731 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000732 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700733 lt = layerTransform.transform(lt);
734 lb = layerTransform.transform(lb);
735 rb = layerTransform.transform(rb);
736 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000737 }
738
Peiyong Lin833074a2018-08-28 11:53:54 -0700739 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700740 position[0] = renderAreaTransform.transform(lt);
741 position[1] = renderAreaTransform.transform(lb);
742 position[2] = renderAreaTransform.transform(rb);
743 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800744}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800745
David Sodman41fdfc92017-11-06 16:09:56 -0800746bool Layer::isSecure() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000747 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700748 return (s.flags & layer_state_t::eLayerSecure);
749}
750
Mathias Agopian13127d82013-03-05 17:47:11 -0800751void Layer::setVisibleRegion(const Region& visibleRegion) {
752 // always called from main thread
753 this->visibleRegion = visibleRegion;
754}
755
756void Layer::setCoveredRegion(const Region& coveredRegion) {
757 // always called from main thread
758 this->coveredRegion = coveredRegion;
759}
760
David Sodman41fdfc92017-11-06 16:09:56 -0800761void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800762 // always called from main thread
763 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
764}
765
Robert Carre5f4f692018-01-12 13:12:28 -0800766void Layer::clearVisibilityRegions() {
767 visibleRegion.clear();
768 visibleNonTransparentRegion.clear();
769 coveredRegion.clear();
770}
771
Mathias Agopian13127d82013-03-05 17:47:11 -0800772// ----------------------------------------------------------------------------
773// transaction
774// ----------------------------------------------------------------------------
775
Dan Stoza7dde5992015-05-22 09:51:44 -0700776void Layer::pushPendingState() {
777 if (!mCurrentState.modified) {
778 return;
779 }
780
Dan Stoza7dde5992015-05-22 09:51:44 -0700781 // If this transaction is waiting on the receipt of a frame, generate a sync
782 // point and send it to the remote layer.
Marissa Wallf58c14b2018-07-24 10:50:43 -0700783 if (mCurrentState.barrierLayer_legacy != nullptr) {
784 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800785 if (barrierLayer == nullptr) {
786 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700787 // If we can't promote the layer we are intended to wait on,
788 // then it is expired or otherwise invalid. Allow this transaction
789 // to be applied as per normal (no synchronization).
Marissa Wallf58c14b2018-07-24 10:50:43 -0700790 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800791 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700792 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800793 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800794 mRemoteSyncPoints.push_back(std::move(syncPoint));
795 } else {
796 // We already missed the frame we're supposed to synchronize
797 // on, so go ahead and apply the state update
Marissa Wallf58c14b2018-07-24 10:50:43 -0700798 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800799 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700800 }
801
Dan Stoza7dde5992015-05-22 09:51:44 -0700802 // Wake us up to check if the frame has been received
803 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700804 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700805 }
806 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700807 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700808}
809
Pablo Ceballos05289c22016-04-14 15:49:55 -0700810void Layer::popPendingState(State* stateToCommit) {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700811 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700812
813 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700814 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700815}
816
Pablo Ceballos05289c22016-04-14 15:49:55 -0700817bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700818 bool stateUpdateAvailable = false;
819 while (!mPendingStates.empty()) {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700820 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700821 if (mRemoteSyncPoints.empty()) {
822 // If we don't have a sync point for this, apply it anyway. It
823 // will be visually wrong, but it should keep us from getting
824 // into too much trouble.
825 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700826 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700827 stateUpdateAvailable = true;
828 continue;
829 }
830
Marissa Wallf58c14b2018-07-24 10:50:43 -0700831 if (mRemoteSyncPoints.front()->getFrameNumber() !=
832 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800833 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800834
835 // Signal our end of the sync point and then dispose of it
836 mRemoteSyncPoints.front()->setTransactionApplied();
837 mRemoteSyncPoints.pop_front();
838 continue;
839 }
840
Dan Stoza7dde5992015-05-22 09:51:44 -0700841 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
842 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700843 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700844 stateUpdateAvailable = true;
845
846 // Signal our end of the sync point and then dispose of it
847 mRemoteSyncPoints.front()->setTransactionApplied();
848 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800849 } else {
850 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700851 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700852 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700853 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700854 stateUpdateAvailable = true;
855 }
856 }
857
858 // If we still have pending updates, wake SurfaceFlinger back up and point
859 // it at this layer so we can process them
860 if (!mPendingStates.empty()) {
861 setTransactionFlags(eTransactionNeeded);
862 mFlinger->setTransactionFlags(eTraversalNeeded);
863 }
864
865 mCurrentState.modified = false;
866 return stateUpdateAvailable;
867}
868
Marissa Wall61c58622018-07-18 10:12:20 -0700869uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000870 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800871
Marissa Wall61c58622018-07-18 10:12:20 -0700872 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
873 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700874
David Sodmaneb085e02017-10-05 18:49:04 -0700875 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700876 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000877 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800878 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
879 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
880 " requested={ wh={%4u,%4u} }}\n"
881 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
882 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700883 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700884 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
885 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
886 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
887 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
888 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700889 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
890 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
891 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800892 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700893
Robert Carre392b552017-09-19 12:16:05 -0700894 // Don't let Layer::doTransaction update the drawing state
895 // if we have a pending resize, unless we are in fixed-size mode.
896 // the drawing state will be updated only once we receive a buffer
897 // with the correct size.
898 //
899 // In particular, we want to make sure the clip (which is part
900 // of the geometry state) is latched together with the size but is
901 // latched immediately when no resizing is involved.
902 //
903 // If a sideband stream is attached, however, we want to skip this
904 // optimization so that transactions aren't missed when a buffer
905 // never arrives
906 //
907 // In the case that we don't have a buffer we ignore other factors
908 // and avoid entering the resizePending state. At a high level the
909 // resizePending state is to avoid applying the state of the new buffer
910 // to the old buffer. However in the state where we don't have an old buffer
911 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700912 const bool resizePending =
913 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
914 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -0800915 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700916 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -0800917 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700918 flags |= eDontUpdateGeometryState;
919 }
920 }
921
Robert Carr7bf247e2017-05-18 14:02:49 -0700922 // Here we apply various requested geometry states, depending on our
923 // latching configuration. See Layer.h for a detailed discussion of
924 // how geometry latching is controlled.
925 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000926 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700927
928 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
929 // mode, which causes attributes which normally latch regardless of scaling mode,
930 // to be delayed. We copy the requested state to the active state making sure
931 // to respect these rules (again see Layer.h for a detailed discussion).
932 //
933 // There is an awkward asymmetry in the handling of the crop states in the position
934 // states, as can be seen below. Largely this arises from position and transform
935 // being stored in the same data structure while having different latching rules.
936 // b/38182305
937 //
Marissa Wall61c58622018-07-18 10:12:20 -0700938 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700939 // applyPendingStates in the presence of deferred transactions.
940 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -0700941 float tx = stateToCommit->active_legacy.transform.tx();
942 float ty = stateToCommit->active_legacy.transform.ty();
943 stateToCommit->active_legacy = stateToCommit->requested_legacy;
944 stateToCommit->active_legacy.transform.set(tx, ty);
945 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700946 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700947 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -0700948 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700949 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800950 }
951
Marissa Wall61c58622018-07-18 10:12:20 -0700952 return flags;
953}
954
955uint32_t Layer::doTransaction(uint32_t flags) {
956 ATRACE_CALL();
957
958 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +0000959 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -0700960 if (!applyPendingStates(&c)) {
961 return 0;
962 }
963
964 flags = doTransactionResize(flags, &c);
965
Alec Mourib416efd2018-09-06 21:01:59 +0000966 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700967
968 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800969 // invalidate and recompute the visible regions if needed
970 flags |= Layer::eVisibleRegion;
971 }
972
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700973 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800974 // invalidate and recompute the visible regions if needed
975 flags |= eVisibleRegion;
976 this->contentDirty = true;
977
978 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -0700979 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -0700980 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -0800981 }
982
Dan Stozac8145172016-04-28 16:29:06 -0700983 // If the layer is hidden, signal and clear out all local sync points so
984 // that transactions for layers depending on this layer's frames becoming
985 // visible are not blocked
986 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700987 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -0700988 }
989
Mathias Agopian13127d82013-03-05 17:47:11 -0800990 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -0700991 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -0800992 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800993}
994
Pablo Ceballos05289c22016-04-14 15:49:55 -0700995void Layer::commitTransaction(const State& stateToCommit) {
996 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -0700997}
998
Mathias Agopian13127d82013-03-05 17:47:11 -0800999uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Piquef1c675b2018-09-12 20:45:39 -07001000 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -08001001}
1002
1003uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Piquef1c675b2018-09-12 20:45:39 -07001004 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -08001005}
1006
Robert Carr82364e32016-05-15 11:27:47 -07001007bool Layer::setPosition(float x, float y, bool immediate) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001008 if (mCurrentState.requested_legacy.transform.tx() == x &&
1009 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001010 return false;
1011 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001012
1013 // We update the requested and active position simultaneously because
1014 // we want to apply the position portion of the transform matrix immediately,
1015 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Marissa Wallf58c14b2018-07-24 10:50:43 -07001016 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001017 if (immediate && !mFreezeGeometryUpdates) {
1018 // Here we directly update the active state
1019 // unlike other setters, because we store it within
1020 // the transform, but use different latching rules.
1021 // b/38182305
Marissa Wallf58c14b2018-07-24 10:50:43 -07001022 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001023 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001024 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001025
Dan Stoza7dde5992015-05-22 09:51:44 -07001026 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001027 setTransactionFlags(eTransactionNeeded);
1028 return true;
1029}
Robert Carr82364e32016-05-15 11:27:47 -07001030
Robert Carr1f0a16a2016-10-24 16:27:39 -07001031bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1032 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1033 if (idx < 0) {
1034 return false;
1035 }
1036 if (childLayer->setLayer(z)) {
1037 mCurrentChildren.removeAt(idx);
1038 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001039 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001040 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001041 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001042}
1043
Robert Carr503c7042017-09-27 15:06:08 -07001044bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1045 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1046 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1047 if (idx < 0) {
1048 return false;
1049 }
1050 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1051 mCurrentChildren.removeAt(idx);
1052 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001053 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001054 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001055 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001056}
1057
Robert Carrae060832016-11-28 10:51:00 -08001058bool Layer::setLayer(int32_t z) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001059 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001060 mCurrentState.sequence++;
1061 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001062 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001063
1064 // Discard all relative layering.
1065 if (mCurrentState.zOrderRelativeOf != nullptr) {
1066 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1067 if (strongRelative != nullptr) {
1068 strongRelative->removeZOrderRelative(this);
1069 }
1070 mCurrentState.zOrderRelativeOf = nullptr;
1071 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001072 setTransactionFlags(eTransactionNeeded);
1073 return true;
1074}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001075
Robert Carrdb66e622017-04-10 16:55:57 -07001076void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1077 mCurrentState.zOrderRelatives.remove(relative);
1078 mCurrentState.sequence++;
1079 mCurrentState.modified = true;
1080 setTransactionFlags(eTransactionNeeded);
1081}
1082
1083void Layer::addZOrderRelative(const wp<Layer>& relative) {
1084 mCurrentState.zOrderRelatives.add(relative);
1085 mCurrentState.modified = true;
1086 mCurrentState.sequence++;
1087 setTransactionFlags(eTransactionNeeded);
1088}
1089
Robert Carr503d2bd2017-12-04 15:49:47 -08001090bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001091 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1092 if (handle == nullptr) {
1093 return false;
1094 }
1095 sp<Layer> relative = handle->owner.promote();
1096 if (relative == nullptr) {
1097 return false;
1098 }
1099
Robert Carr503d2bd2017-12-04 15:49:47 -08001100 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1101 mCurrentState.zOrderRelativeOf == relative) {
1102 return false;
1103 }
1104
Robert Carrdb66e622017-04-10 16:55:57 -07001105 mCurrentState.sequence++;
1106 mCurrentState.modified = true;
Robert Carr503d2bd2017-12-04 15:49:47 -08001107 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001108
chaviw9ab4bd12017-11-03 13:11:00 -07001109 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1110 if (oldZOrderRelativeOf != nullptr) {
1111 oldZOrderRelativeOf->removeZOrderRelative(this);
1112 }
Robert Carrdb66e622017-04-10 16:55:57 -07001113 mCurrentState.zOrderRelativeOf = relative;
1114 relative->addZOrderRelative(this);
1115
1116 setTransactionFlags(eTransactionNeeded);
1117
1118 return true;
1119}
1120
Mathias Agopian13127d82013-03-05 17:47:11 -08001121bool Layer::setSize(uint32_t w, uint32_t h) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001122 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
1123 return false;
1124 mCurrentState.requested_legacy.w = w;
1125 mCurrentState.requested_legacy.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001126 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001127 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001128
1129 // record the new size, from this point on, when the client request
1130 // a buffer, it'll get the new size.
1131 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001132 return true;
1133}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001134bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001135 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001136 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001137 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001138 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001139 setTransactionFlags(eTransactionNeeded);
1140 return true;
1141}
chaviw13fdc492017-06-27 12:40:18 -07001142
1143bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001144 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1145 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001146 return false;
1147
1148 mCurrentState.sequence++;
1149 mCurrentState.color.r = color.r;
1150 mCurrentState.color.g = color.g;
1151 mCurrentState.color.b = color.b;
1152 mCurrentState.modified = true;
1153 setTransactionFlags(eTransactionNeeded);
1154 return true;
1155}
1156
Robert Carrd4ae7f32018-06-07 16:10:57 -07001157bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1158 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001159 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001160 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1161
1162 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1163 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1164 return false;
1165 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001166 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001167 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1168 matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001169 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001170 setTransactionFlags(eTransactionNeeded);
1171 return true;
1172}
Marissa Wall61c58622018-07-18 10:12:20 -07001173
Mathias Agopian13127d82013-03-05 17:47:11 -08001174bool Layer::setTransparentRegionHint(const Region& transparent) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001175 mCurrentState.requestedTransparentRegion_legacy = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001176 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001177 setTransactionFlags(eTransactionNeeded);
1178 return true;
1179}
1180bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1181 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001182 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001183 mCurrentState.sequence++;
1184 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001185 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001186 setTransactionFlags(eTransactionNeeded);
1187 return true;
1188}
Robert Carr99e27f02016-06-16 15:18:02 -07001189
Marissa Wallf58c14b2018-07-24 10:50:43 -07001190bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
1191 if (mCurrentState.requestedCrop_legacy == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001192 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001193 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001194 if (immediate && !mFreezeGeometryUpdates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001195 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001196 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001197 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1198
Dan Stoza7dde5992015-05-22 09:51:44 -07001199 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001200 setTransactionFlags(eTransactionNeeded);
1201 return true;
1202}
Robert Carr8d5227b2017-03-16 15:41:03 -07001203
Robert Carrc3574f72016-03-24 12:19:32 -07001204bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001205 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001206 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001207 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001208 return true;
1209}
1210
rongliucfb187b2018-03-14 12:26:23 -07001211void Layer::setInfo(int32_t type, int32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001212 mCurrentState.appId = appId;
1213 mCurrentState.type = type;
1214 mCurrentState.modified = true;
1215 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001216}
1217
Mathias Agopian13127d82013-03-05 17:47:11 -08001218bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001219 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001220 mCurrentState.sequence++;
1221 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001222 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001223 setTransactionFlags(eTransactionNeeded);
1224 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001225}
1226
Robert Carr1f0a16a2016-10-24 16:27:39 -07001227uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001228 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001229 if (p == nullptr) {
1230 return getDrawingState().layerStack;
1231 }
1232 return p->getLayerStack();
1233}
1234
Marissa Wallf58c14b2018-07-24 10:50:43 -07001235void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
1236 mCurrentState.barrierLayer_legacy = barrierLayer;
1237 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001238 // We don't set eTransactionNeeded, because just receiving a deferral
1239 // request without any other state updates shouldn't actually induce a delay
1240 mCurrentState.modified = true;
1241 pushPendingState();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001242 mCurrentState.barrierLayer_legacy = nullptr;
1243 mCurrentState.frameNumber_legacy = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001244 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001245}
1246
Marissa Wallf58c14b2018-07-24 10:50:43 -07001247void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001248 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001249 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001250}
1251
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001252// ----------------------------------------------------------------------------
1253// pageflip handling...
1254// ----------------------------------------------------------------------------
1255
Robert Carr1f0a16a2016-10-24 16:27:39 -07001256bool Layer::isHiddenByPolicy() const {
Alec Mourib416efd2018-09-06 21:01:59 +00001257 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001258 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001259 if (parent != nullptr && parent->isHiddenByPolicy()) {
1260 return true;
1261 }
1262 return s.flags & layer_state_t::eLayerHidden;
1263}
1264
David Sodman41fdfc92017-11-06 16:09:56 -08001265uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001266 // TODO: should we do something special if mSecure is set?
1267 if (mProtectedByApp) {
1268 // need a hardware-protected path to external video sink
1269 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001270 }
Riley Andrews03414a12014-07-01 14:22:59 -07001271 if (mPotentialCursor) {
1272 usage |= GraphicBuffer::USAGE_CURSOR;
1273 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001274 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001275 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001276}
1277
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001278void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001279 uint32_t orientation = 0;
1280 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001281 // The transform hint is used to improve performance, but we can
1282 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001283 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001284 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001285 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001286 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001287 orientation = 0;
1288 }
1289 }
David Sodmaneb085e02017-10-05 18:49:04 -07001290 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001291}
1292
Mathias Agopian13127d82013-03-05 17:47:11 -08001293// ----------------------------------------------------------------------------
1294// debugging
1295// ----------------------------------------------------------------------------
1296
Marissa Wall61c58622018-07-18 10:12:20 -07001297// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001298LayerDebugInfo Layer::getLayerDebugInfo() const {
1299 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001300 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001301 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001302 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001303 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1304 info.mType = String8(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001305 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001306 info.mVisibleRegion = visibleRegion;
1307 info.mSurfaceDamageRegion = surfaceDamageRegion;
1308 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001309 info.mX = ds.active_legacy.transform.tx();
1310 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001311 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001312 info.mWidth = ds.active_legacy.w;
1313 info.mHeight = ds.active_legacy.h;
1314 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001315 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001316 info.mFlags = ds.flags;
1317 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001318 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001319 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1320 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1321 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1322 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001323 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001324 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001325 if (buffer != 0) {
1326 info.mActiveBufferWidth = buffer->getWidth();
1327 info.mActiveBufferHeight = buffer->getHeight();
1328 info.mActiveBufferStride = buffer->getStride();
1329 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001330 } else {
1331 info.mActiveBufferWidth = 0;
1332 info.mActiveBufferHeight = 0;
1333 info.mActiveBufferStride = 0;
1334 info.mActiveBufferFormat = 0;
1335 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001336 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001337 info.mNumQueuedFrames = getQueuedFrameCount();
1338 info.mRefreshPending = isBufferLatched();
1339 info.mIsOpaque = isOpaque(ds);
1340 info.mContentDirty = contentDirty;
1341 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001342}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001343
Dan Stozae22aec72016-08-01 13:20:59 -07001344void Layer::miniDumpHeader(String8& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001345 result.append("-------------------------------");
1346 result.append("-------------------------------");
1347 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001348 result.append(" Layer name\n");
1349 result.append(" Z | ");
1350 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001351 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001352 result.append(" Disp Frame (LTRB) | ");
1353 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001354 result.append("-------------------------------");
1355 result.append("-------------------------------");
1356 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001357}
1358
Dominik Laskowski7e045462018-05-30 13:02:02 -07001359void Layer::miniDump(String8& result, int32_t displayId) const {
1360 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stozae22aec72016-08-01 13:20:59 -07001361 return;
1362 }
1363
1364 String8 name;
1365 if (mName.length() > 77) {
1366 std::string shortened;
1367 shortened.append(mName.string(), 36);
1368 shortened.append("[...]");
1369 shortened.append(mName.string() + (mName.length() - 36), 36);
1370 name = shortened.c_str();
1371 } else {
1372 name = mName;
1373 }
1374
1375 result.appendFormat(" %s\n", name.string());
1376
Alec Mourib416efd2018-09-06 21:01:59 +00001377 const State& layerState(getDrawingState());
Lloyd Pique074e8122018-07-26 12:57:23 -07001378 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(displayId);
Chia-I Wu1e043612018-03-01 09:45:09 -08001379 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
1380 result.appendFormat(" rel %6d | ", layerState.z);
1381 } else {
1382 result.appendFormat(" %10d | ", layerState.z);
1383 }
Dominik Laskowski7e045462018-05-30 13:02:02 -07001384 result.appendFormat("%10s | ", to_string(getCompositionType(displayId)).c_str());
Lloyd Pique074e8122018-07-26 12:57:23 -07001385 result.appendFormat("%10s | ", to_string(hwcInfo.transform).c_str());
1386 const Rect& frame = hwcInfo.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001387 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique074e8122018-07-26 12:57:23 -07001388 const FloatRect& crop = hwcInfo.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001389 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 -07001390
David Sodman7e4ae112018-02-09 15:02:28 -08001391 result.append("- - - - - - - - - - - - - - - -\n");
1392
1393 std::string compositionInfoStr;
1394 getBE().compositionInfo.dump(compositionInfoStr, "compositionInfo");
1395 result.append(compositionInfoStr.c_str());
1396
Yichi Chen6ca35192018-05-29 12:20:43 +08001397 result.append("- - - - - - - - - - - - - - - -");
1398 result.append("- - - - - - - - - - - - - - - -");
1399 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001400}
Dan Stozae22aec72016-08-01 13:20:59 -07001401
Svetoslavd85084b2014-03-20 10:28:31 -07001402void Layer::dumpFrameStats(String8& result) const {
1403 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001404}
1405
Svetoslavd85084b2014-03-20 10:28:31 -07001406void Layer::clearFrameStats() {
1407 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001408}
1409
Jamie Gennis6547ff42013-07-16 20:12:42 -07001410void Layer::logFrameStats() {
1411 mFrameTracker.logAndResetStats(mName);
1412}
1413
Svetoslavd85084b2014-03-20 10:28:31 -07001414void Layer::getFrameStats(FrameStats* outStats) const {
1415 mFrameTracker.getStats(outStats);
1416}
1417
Brian Andersond6927fb2016-07-23 23:37:30 -07001418void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001419 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001420 Mutex::Autolock lock(mFrameEventHistoryMutex);
1421 mFrameEventHistory.checkFencesForCompletion();
1422 mFrameEventHistory.dump(result);
1423}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001424
Brian Anderson5ea5e592016-12-01 16:54:33 -08001425void Layer::onDisconnect() {
1426 Mutex::Autolock lock(mFrameEventHistoryMutex);
1427 mFrameEventHistory.onDisconnect();
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001428 mTimeStats.onDisconnect(getName().c_str());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001429}
1430
Brian Anderson3890c392016-07-25 12:48:08 -07001431void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001432 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001433 if (newTimestamps) {
1434 mTimeStats.setPostTime(getName().c_str(), newTimestamps->frameNumber,
1435 newTimestamps->postedTime);
1436 }
1437
Brian Andersond6927fb2016-07-23 23:37:30 -07001438 Mutex::Autolock lock(mFrameEventHistoryMutex);
1439 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001440 // If there are any unsignaled fences in the aquire timeline at this
1441 // point, the previously queued frame hasn't been latched yet. Go ahead
1442 // and try to get the signal time here so the syscall is taken out of
1443 // the main thread's critical path.
1444 mAcquireTimeline.updateSignalTimes();
1445 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001446 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001447 mFrameEventHistory.addQueue(*newTimestamps);
1448 }
1449
Brian Anderson3890c392016-07-25 12:48:08 -07001450 if (outDelta) {
1451 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001452 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001453}
Dan Stozae77c7662016-05-13 11:37:28 -07001454
Chia-I Wu98f1c102017-05-30 14:54:08 -07001455size_t Layer::getChildrenCount() const {
1456 size_t count = 0;
1457 for (const sp<Layer>& child : mCurrentChildren) {
1458 count += 1 + child->getChildrenCount();
1459 }
1460 return count;
1461}
1462
Robert Carr1f0a16a2016-10-24 16:27:39 -07001463void Layer::addChild(const sp<Layer>& layer) {
1464 mCurrentChildren.add(layer);
1465 layer->setParent(this);
1466}
1467
1468ssize_t Layer::removeChild(const sp<Layer>& layer) {
1469 layer->setParent(nullptr);
1470 return mCurrentChildren.remove(layer);
1471}
1472
Robert Carr1db73f62016-12-21 12:58:51 -08001473bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1474 sp<Handle> handle = nullptr;
1475 sp<Layer> newParent = nullptr;
1476 if (newParentHandle == nullptr) {
1477 return false;
1478 }
1479 handle = static_cast<Handle*>(newParentHandle.get());
1480 newParent = handle->owner.promote();
1481 if (newParent == nullptr) {
1482 ALOGE("Unable to promote Layer handle");
1483 return false;
1484 }
1485
1486 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001487 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001488
1489 sp<Client> client(child->mClientRef.promote());
1490 if (client != nullptr) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001491 client->updateParent(newParent);
Robert Carr1db73f62016-12-21 12:58:51 -08001492 }
1493 }
1494 mCurrentChildren.clear();
1495
1496 return true;
1497}
1498
Robert Carr15eae092018-03-23 13:43:53 -07001499void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001500 for (const sp<Layer>& child : mDrawingChildren) {
1501 child->mDrawingParent = newParent;
1502 }
1503}
1504
chaviwf1961f72017-09-18 16:41:07 -07001505bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1506 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001507 return false;
1508 }
1509
1510 auto handle = static_cast<Handle*>(newParentHandle.get());
1511 sp<Layer> newParent = handle->owner.promote();
1512 if (newParent == nullptr) {
1513 ALOGE("Unable to promote Layer handle");
1514 return false;
1515 }
1516
chaviwf1961f72017-09-18 16:41:07 -07001517 sp<Layer> parent = getParent();
1518 if (parent != nullptr) {
1519 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001520 }
chaviwf1961f72017-09-18 16:41:07 -07001521 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001522
chaviwf1961f72017-09-18 16:41:07 -07001523 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001524 sp<Client> newParentClient(newParent->mClientRef.promote());
1525
chaviwf1961f72017-09-18 16:41:07 -07001526 if (client != newParentClient) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001527 client->updateParent(newParent);
chaviw06178942017-07-27 10:25:59 -07001528 }
1529
chaviw06178942017-07-27 10:25:59 -07001530 return true;
1531}
1532
Robert Carr9524cb32017-02-13 11:32:32 -08001533bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001534 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001535 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001536 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001537 if (client != nullptr && parentClient != client) {
Robert Carr7f619b22017-11-06 12:56:35 -08001538 client->detachLayer(child.get());
1539 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001540 }
Robert Carr7f619b22017-11-06 12:56:35 -08001541 }
Robert Carr9524cb32017-02-13 11:32:32 -08001542
1543 return true;
1544}
1545
Chia-I Wu11481472018-05-04 10:43:19 -07001546bool Layer::isLegacyDataSpace() const {
1547 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001548 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001549 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001550}
1551
Robert Carr1f0a16a2016-10-24 16:27:39 -07001552void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001553 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001554}
1555
1556void Layer::clearSyncPoints() {
1557 for (const auto& child : mCurrentChildren) {
1558 child->clearSyncPoints();
1559 }
1560
1561 Mutex::Autolock lock(mLocalSyncPointMutex);
1562 for (auto& point : mLocalSyncPoints) {
1563 point->setFrameAvailable();
1564 }
1565 mLocalSyncPoints.clear();
1566}
1567
1568int32_t Layer::getZ() const {
1569 return mDrawingState.z;
1570}
1571
Robert Carr29abff82017-12-04 13:51:20 -08001572bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1573 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1574 const State& state = useDrawing ? mDrawingState : mCurrentState;
1575 return state.zOrderRelativeOf != nullptr;
1576}
1577
David Sodman41fdfc92017-11-06 16:09:56 -08001578__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001579 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001580 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1581 "makeTraversalList received invalid stateSet");
1582 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1583 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1584 const State& state = useDrawing ? mDrawingState : mCurrentState;
1585
Robert Carr29abff82017-12-04 13:51:20 -08001586 if (state.zOrderRelatives.size() == 0) {
1587 *outSkipRelativeZUsers = true;
1588 return children;
1589 }
1590
chaviwfd462612018-05-31 16:11:27 -07001591 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001592 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001593 sp<Layer> strongRelative = weakRelative.promote();
1594 if (strongRelative != nullptr) {
1595 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001596 }
1597 }
1598
Dan Stoza412903f2017-04-27 13:42:17 -07001599 for (const sp<Layer>& child : children) {
Robert Carr503c7042017-09-27 15:06:08 -07001600 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1601 if (childState.zOrderRelativeOf != nullptr) {
1602 continue;
1603 }
Robert Carrdb66e622017-04-10 16:55:57 -07001604 traverse.add(child);
1605 }
1606
1607 return traverse;
1608}
1609
Robert Carr1f0a16a2016-10-24 16:27:39 -07001610/**
Robert Carrdb66e622017-04-10 16:55:57 -07001611 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001612 */
Dan Stoza412903f2017-04-27 13:42:17 -07001613void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001614 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1615 // produce a new list for traversing, including our relatives, and not including our children
1616 // who are relatives of another surface. In the case that there are no relative Z,
1617 // makeTraversalList returns our children directly to avoid significant overhead.
1618 // However in this case we need to take the responsibility for filtering children which
1619 // are relatives of another surface here.
1620 bool skipRelativeZUsers = false;
1621 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001622
Robert Carr1f0a16a2016-10-24 16:27:39 -07001623 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001624 for (; i < list.size(); i++) {
1625 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001626 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1627 continue;
1628 }
1629
Robert Carrdb66e622017-04-10 16:55:57 -07001630 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001631 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001632 }
Dan Stoza412903f2017-04-27 13:42:17 -07001633 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001634 }
Robert Carr29abff82017-12-04 13:51:20 -08001635
Dan Stoza412903f2017-04-27 13:42:17 -07001636 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001637 for (; i < list.size(); i++) {
1638 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001639
1640 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1641 continue;
1642 }
Dan Stoza412903f2017-04-27 13:42:17 -07001643 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001644 }
1645}
1646
1647/**
Robert Carrdb66e622017-04-10 16:55:57 -07001648 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001649 */
Dan Stoza412903f2017-04-27 13:42:17 -07001650void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1651 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001652 // See traverseInZOrder for documentation.
1653 bool skipRelativeZUsers = false;
1654 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001655
Robert Carr1f0a16a2016-10-24 16:27:39 -07001656 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001657 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001658 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001659
1660 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1661 continue;
1662 }
1663
Robert Carrdb66e622017-04-10 16:55:57 -07001664 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001665 break;
1666 }
Dan Stoza412903f2017-04-27 13:42:17 -07001667 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001668 }
Dan Stoza412903f2017-04-27 13:42:17 -07001669 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001670 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001671 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001672
1673 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1674 continue;
1675 }
1676
Dan Stoza412903f2017-04-27 13:42:17 -07001677 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001678 }
1679}
1680
chaviw4b129c22018-04-09 16:19:43 -07001681LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1682 const std::vector<Layer*>& layersInTree) {
1683 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1684 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001685 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1686 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
chaviw4b129c22018-04-09 16:19:43 -07001687 const State& state = useDrawing ? mDrawingState : mCurrentState;
1688
chaviwfd462612018-05-31 16:11:27 -07001689 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001690 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1691 sp<Layer> strongRelative = weakRelative.promote();
1692 // Only add relative layers that are also descendents of the top most parent of the tree.
1693 // If a relative layer is not a descendent, then it should be ignored.
1694 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1695 traverse.add(strongRelative);
1696 }
1697 }
1698
1699 for (const sp<Layer>& child : children) {
1700 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1701 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1702 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1703 // the child here since it won't be added later as a relative.
1704 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1705 childState.zOrderRelativeOf.promote().get())) {
1706 continue;
1707 }
1708 traverse.add(child);
1709 }
1710
1711 return traverse;
1712}
1713
1714void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1715 LayerVector::StateSet stateSet,
1716 const LayerVector::Visitor& visitor) {
1717 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001718
1719 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001720 for (; i < list.size(); i++) {
1721 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001722 if (relative->getZ() >= 0) {
1723 break;
1724 }
chaviw4b129c22018-04-09 16:19:43 -07001725 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001726 }
chaviw4b129c22018-04-09 16:19:43 -07001727
chaviwa76b2712017-09-20 12:02:26 -07001728 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001729 for (; i < list.size(); i++) {
1730 const auto& relative = list[i];
1731 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001732 }
1733}
1734
chaviw4b129c22018-04-09 16:19:43 -07001735std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1736 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1737 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1738
1739 std::vector<Layer*> layersInTree = {this};
1740 for (size_t i = 0; i < children.size(); i++) {
1741 const auto& child = children[i];
1742 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1743 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1744 }
1745
1746 return layersInTree;
1747}
1748
1749void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1750 const LayerVector::Visitor& visitor) {
1751 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1752 std::sort(layersInTree.begin(), layersInTree.end());
1753 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1754}
1755
Peiyong Linefefaac2018-08-17 12:27:51 -07001756ui::Transform Layer::getTransform() const {
1757 ui::Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001758 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001759 if (p != nullptr) {
1760 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001761
1762 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1763 // it isFixedSize) then there may be additional scaling not accounted
1764 // for in the transform. We need to mirror this scaling in child surfaces
1765 // or we will break the contract where WM can treat child surfaces as
1766 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08001767 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001768 int bufferWidth;
1769 int bufferHeight;
1770 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08001771 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
1772 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001773 } else {
David Sodman0cc69182017-11-17 12:12:07 -08001774 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
1775 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001776 }
Marissa Wall61c58622018-07-18 10:12:20 -07001777 float sx = p->getActiveWidth(p->getDrawingState()) / static_cast<float>(bufferWidth);
1778 float sy = p->getActiveHeight(p->getDrawingState()) / static_cast<float>(bufferHeight);
Peiyong Linefefaac2018-08-17 12:27:51 -07001779 ui::Transform extraParentScaling;
Robert Carr9b429f42017-04-17 14:56:57 -07001780 extraParentScaling.set(sx, 0, 0, sy);
1781 t = t * extraParentScaling;
1782 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001783 }
Marissa Wall61c58622018-07-18 10:12:20 -07001784 return t * getActiveTransform(getDrawingState());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001785}
1786
chaviw13fdc492017-06-27 12:40:18 -07001787half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001788 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001789
chaviw13fdc492017-06-27 12:40:18 -07001790 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1791 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001792}
Robert Carr6452f122017-03-21 10:41:29 -07001793
chaviw13fdc492017-06-27 12:40:18 -07001794half4 Layer::getColor() const {
1795 const half4 color(getDrawingState().color);
1796 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001797}
Robert Carr6452f122017-03-21 10:41:29 -07001798
Robert Carr1f0a16a2016-10-24 16:27:39 -07001799void Layer::commitChildList() {
1800 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1801 const auto& child = mCurrentChildren[i];
1802 child->commitChildList();
1803 }
1804 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001805 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001806}
1807
chaviw1d044282017-09-27 12:19:28 -07001808void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1809 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1810 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1811 const State& state = useDrawing ? mDrawingState : mCurrentState;
1812
Peiyong Linefefaac2018-08-17 12:27:51 -07001813 ui::Transform requestedTransform = state.active_legacy.transform;
1814 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07001815
1816 layerInfo->set_id(sequence);
1817 layerInfo->set_name(getName().c_str());
1818 layerInfo->set_type(String8(getTypeId()));
1819
1820 for (const auto& child : children) {
1821 layerInfo->add_children(child->sequence);
1822 }
1823
1824 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1825 sp<Layer> strongRelative = weakRelative.promote();
1826 if (strongRelative != nullptr) {
1827 layerInfo->add_relatives(strongRelative->sequence);
1828 }
1829 }
1830
Marissa Wallf58c14b2018-07-24 10:50:43 -07001831 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
chaviw1d044282017-09-27 12:19:28 -07001832 layerInfo->mutable_transparent_region());
1833 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
1834 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
1835
1836 layerInfo->set_layer_stack(getLayerStack());
1837 layerInfo->set_z(state.z);
1838
1839 PositionProto* position = layerInfo->mutable_position();
1840 position->set_x(transform.tx());
1841 position->set_y(transform.ty());
1842
1843 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
1844 requestedPosition->set_x(requestedTransform.tx());
1845 requestedPosition->set_y(requestedTransform.ty());
1846
1847 SizeProto* size = layerInfo->mutable_size();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001848 size->set_w(state.active_legacy.w);
1849 size->set_h(state.active_legacy.h);
chaviw1d044282017-09-27 12:19:28 -07001850
Marissa Wallf58c14b2018-07-24 10:50:43 -07001851 LayerProtoHelper::writeToProto(state.crop_legacy, layerInfo->mutable_crop());
chaviw1d044282017-09-27 12:19:28 -07001852
1853 layerInfo->set_is_opaque(isOpaque(state));
1854 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07001855
1856 // XXX (b/79210409) mCurrentDataSpace is not protected
1857 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1858
chaviw1d044282017-09-27 12:19:28 -07001859 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1860 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
1861 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
1862 layerInfo->set_flags(state.flags);
1863
1864 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1865 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
1866
Jorim Jaggi8e0af362017-11-14 16:28:28 +01001867 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07001868 if (parent != nullptr) {
1869 layerInfo->set_parent(parent->sequence);
1870 }
1871
1872 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1873 if (zOrderRelativeOf != nullptr) {
1874 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1875 }
1876
Chia-I Wu01591c92018-05-22 12:03:00 -07001877 // XXX getBE().compositionInfo.mBuffer is not protected
David Sodman0cc69182017-11-17 12:12:07 -08001878 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001879 if (buffer != nullptr) {
1880 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
Peiyong Linefefaac2018-08-17 12:27:51 -07001881 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
Yichi Chen6ca35192018-05-29 12:20:43 +08001882 layerInfo->mutable_buffer_transform());
chaviw1d044282017-09-27 12:19:28 -07001883 }
1884
1885 layerInfo->set_queued_frames(getQueuedFrameCount());
1886 layerInfo->set_refresh_pending(isBufferLatched());
rongliucfb187b2018-03-14 12:26:23 -07001887 layerInfo->set_window_type(state.type);
1888 layerInfo->set_app_id(state.appId);
chaviwadc40c22018-07-10 16:57:27 -07001889 layerInfo->set_curr_frame(mCurrentFrameNumber);
1890
1891 for (const auto& pendingState : mPendingStates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001892 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
chaviwadc40c22018-07-10 16:57:27 -07001893 if (barrierLayer != nullptr) {
1894 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1895 barrierLayerProto->set_id(barrierLayer->sequence);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001896 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
chaviwadc40c22018-07-10 16:57:27 -07001897 }
1898 }
chaviw1d044282017-09-27 12:19:28 -07001899}
1900
Dominik Laskowski7e045462018-05-30 13:02:02 -07001901void Layer::writeToProto(LayerProto* layerInfo, int32_t displayId) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07001902 if (!hasHwcLayer(displayId)) {
1903 return;
1904 }
1905
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001906 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
1907
Dominik Laskowski7e045462018-05-30 13:02:02 -07001908 const auto& hwcInfo = getBE().mHwcLayers.at(displayId);
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001909
1910 const Rect& frame = hwcInfo.displayFrame;
1911 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
1912
1913 const FloatRect& crop = hwcInfo.sourceCrop;
1914 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
1915
1916 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
1917 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08001918
1919 const int32_t compositionType = static_cast<int32_t>(hwcInfo.compositionType);
1920 layerInfo->set_hwc_composition_type(compositionType);
1921
1922 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
1923 static_cast<BufferLayer*>(this)->isProtected()) {
1924 layerInfo->set_is_protected(true);
1925 } else {
1926 layerInfo->set_is_protected(false);
1927 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001928}
1929
Mathias Agopian13127d82013-03-05 17:47:11 -08001930// ---------------------------------------------------------------------------
1931
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001932}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07001933
1934#if defined(__gl_h_)
1935#error "don't include gl/gl.h in this file"
1936#endif
1937
1938#if defined(__gl2_h_)
1939#error "don't include gl2/gl2.h in this file"
1940#endif