blob: 9f1c6625f6da074855d21f6878dbf37237bc0bf5 [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
Robert Carrf478f9f2018-10-15 12:43:33 -0700121 mFrameTracker.logAndResetStats(mName);
122
123 // The remote sync points are cleared out when we are
124 // removed from current state.
125 Mutex::Autolock lock(mLocalSyncPointMutex);
Rob Carr4bba3702018-10-08 21:53:30 +0000126 for (auto& point : mLocalSyncPoints) {
127 point->setFrameAvailable();
128 }
Robert Carrf478f9f2018-10-15 12:43:33 -0700129
130 abandon();
131
132 destroyAllHwcLayers();
133
134 mFlinger->onLayerDestroyed();
Mathias Agopian96f08192010-06-02 23:28:45 -0700135}
136
Mathias Agopian13127d82013-03-05 17:47:11 -0800137// ---------------------------------------------------------------------------
138// callbacks
139// ---------------------------------------------------------------------------
140
David Sodmaneb085e02017-10-05 18:49:04 -0700141/*
142 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
143 * Layer. So, the implementation is done in BufferLayer. When called on a
144 * ColorLayer object, it's essentially a NOP.
145 */
David Sodmaneb085e02017-10-05 18:49:04 -0700146void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800147
Chia-I Wuc6657022017-08-15 11:18:17 -0700148void Layer::onRemovedFromCurrentState() {
Robert Carrf478f9f2018-10-15 12:43:33 -0700149 mRemovedFromCurrentState = true;
150
Chia-I Wuc6657022017-08-15 11:18:17 -0700151 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
Robert Carr5edb1ad2017-04-25 10:54:24 -0700152 if (mCurrentState.zOrderRelativeOf != nullptr) {
153 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
154 if (strongRelative != nullptr) {
155 strongRelative->removeZOrderRelative(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700156 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700157 }
158 mCurrentState.zOrderRelativeOf = nullptr;
159 }
Rob Carr4bba3702018-10-08 21:53:30 +0000160
Robert Carrf478f9f2018-10-15 12:43:33 -0700161 // Since we are no longer reachable from CurrentState SurfaceFlinger
162 // will no longer invoke doTransaction for us, and so we will
163 // never finish applying transactions. We signal the sync point
164 // now so that another layer will not become indefinitely
165 // blocked.
166 for (auto& point: mRemoteSyncPoints) {
167 point->setTransactionApplied();
168 }
169 mRemoteSyncPoints.clear();
170
Chia-I Wuc6657022017-08-15 11:18:17 -0700171 for (const auto& child : mCurrentChildren) {
172 child->onRemovedFromCurrentState();
173 }
174}
Chia-I Wu38512252017-05-17 14:36:16 -0700175
Mathias Agopian13127d82013-03-05 17:47:11 -0800176// ---------------------------------------------------------------------------
177// set-up
178// ---------------------------------------------------------------------------
179
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700180const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800181 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182}
183
chaviw13fdc492017-06-27 12:40:18 -0700184bool Layer::getPremultipledAlpha() const {
185 return mPremultipliedAlpha;
186}
187
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700188sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800189 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700190 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800191}
192
193// ---------------------------------------------------------------------------
194// h/w composer set-up
195// ---------------------------------------------------------------------------
196
Dominik Laskowski7e045462018-05-30 13:02:02 -0700197bool Layer::createHwcLayer(HWComposer* hwc, int32_t displayId) {
198 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(displayId) != 0,
199 "Already have a layer for display %d", displayId);
David Sodmanb8aaea12017-12-14 15:54:51 -0800200 auto layer = std::shared_ptr<HWC2::Layer>(
201 hwc->createLayer(displayId),
202 [hwc, displayId](HWC2::Layer* layer) {
203 hwc->destroyLayer(displayId, layer); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700204 if (!layer) {
205 return false;
206 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700207 LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers[displayId];
Steven Thomasb02664d2017-07-26 18:48:28 -0700208 hwcInfo.hwc = hwc;
209 hwcInfo.layer = layer;
David Sodmanf6a38932018-05-25 15:27:50 -0700210 layer->setLayerDestroyedListener(
Dominik Laskowski7e045462018-05-30 13:02:02 -0700211 [this, displayId](HWC2::Layer* /*layer*/) { getBE().mHwcLayers.erase(displayId); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700212 return true;
213}
214
Dominik Laskowski7e045462018-05-30 13:02:02 -0700215bool Layer::destroyHwcLayer(int32_t displayId) {
216 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu83806892017-11-16 10:50:20 -0800217 return false;
Steven Thomasb02664d2017-07-26 18:48:28 -0700218 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700219 auto& hwcInfo = getBE().mHwcLayers[displayId];
David Sodman41fdfc92017-11-06 16:09:56 -0800220 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr, "Attempt to destroy null layer");
Steven Thomasb02664d2017-07-26 18:48:28 -0700221 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
David Sodmanb8aaea12017-12-14 15:54:51 -0800222 hwcInfo.layer = nullptr;
223
Chia-I Wu83806892017-11-16 10:50:20 -0800224 return true;
Steven Thomasb02664d2017-07-26 18:48:28 -0700225}
226
227void Layer::destroyAllHwcLayers() {
David Sodman6f65f3e2017-11-03 14:28:09 -0700228 size_t numLayers = getBE().mHwcLayers.size();
Steven Thomasb02664d2017-07-26 18:48:28 -0700229 for (size_t i = 0; i < numLayers; ++i) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700230 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.empty(), "destroyAllHwcLayers failed");
231 destroyHwcLayer(getBE().mHwcLayers.begin()->first);
Steven Thomasb02664d2017-07-26 18:48:28 -0700232 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700233 LOG_ALWAYS_FATAL_IF(!getBE().mHwcLayers.empty(),
David Sodman41fdfc92017-11-06 16:09:56 -0800234 "All hardware composer layers should have been destroyed");
Robert Carrf478f9f2018-10-15 12:43:33 -0700235
236 for (const sp<Layer>& child : mDrawingChildren) {
237 child->destroyAllHwcLayers();
238 }
Steven Thomasb02664d2017-07-26 18:48:28 -0700239}
Steven Thomasb02664d2017-07-26 18:48:28 -0700240
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800241Rect Layer::getContentCrop() const {
242 // this is the crop rectangle that applies to the buffer
243 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700244 Rect crop;
245 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800246 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700247 crop = mCurrentCrop;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800248 } else if (getBE().compositionInfo.mBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800249 // otherwise we use the whole buffer
David Sodman0cc69182017-11-17 12:12:07 -0800250 crop = getBE().compositionInfo.mBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700251 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800252 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700253 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700254 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700255 return crop;
256}
257
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700258static Rect reduce(const Rect& win, const Region& exclude) {
259 if (CC_LIKELY(exclude.isEmpty())) {
260 return win;
261 }
262 if (exclude.isRect()) {
263 return win.reduce(exclude.getBounds());
264 }
265 return Region(win).subtract(exclude).getBounds();
266}
267
Dan Stoza80d61162017-12-20 15:57:52 -0800268static FloatRect reduce(const FloatRect& win, const Region& exclude) {
269 if (CC_LIKELY(exclude.isEmpty())) {
270 return win;
271 }
272 // Convert through Rect (by rounding) for lack of FloatRegion
273 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
274}
275
Robert Carr1f0a16a2016-10-24 16:27:39 -0700276Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
Alec Mourib416efd2018-09-06 21:01:59 +0000277 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700278 Rect win(getActiveWidth(s), getActiveHeight(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700279
Marissa Wall61c58622018-07-18 10:12:20 -0700280 Rect crop = getCrop(s);
281 if (!crop.isEmpty()) {
282 win.intersect(crop, &win);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700283 }
284
Peiyong Linefefaac2018-08-17 12:27:51 -0700285 ui::Transform t = getTransform();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700286 win = t.transform(win);
287
Chia-I Wue41dbe62017-06-13 14:10:56 -0700288 const sp<Layer>& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700289 // Now we need to calculate the parent bounds, so we can clip ourselves to those.
290 // When calculating the parent bounds for purposes of clipping,
291 // we don't need to constrain the parent to its transparent region.
292 // The transparent region is an optimization based on the
293 // buffer contents of the layer, but does not affect the space allocated to
294 // it by policy, and thus children should be allowed to extend into the
295 // parent's transparent region. In fact one of the main uses, is to reduce
296 // buffer allocation size in cases where a child window sits behind a main window
297 // (by marking the hole in the parent window as a transparent region)
298 if (p != nullptr) {
299 Rect bounds = p->computeScreenBounds(false);
300 bounds.intersect(win, &win);
301 }
302
303 if (reduceTransparentRegion) {
Marissa Wall61c58622018-07-18 10:12:20 -0700304 auto const screenTransparentRegion = t.transform(getActiveTransparentRegion(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700305 win = reduce(win, screenTransparentRegion);
306 }
307
308 return win;
309}
310
Dan Stoza80d61162017-12-20 15:57:52 -0800311FloatRect Layer::computeBounds() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000312 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700313 return computeBounds(getActiveTransparentRegion(s));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700314}
315
Dan Stoza80d61162017-12-20 15:57:52 -0800316FloatRect Layer::computeBounds(const Region& activeTransparentRegion) const {
Alec Mourib416efd2018-09-06 21:01:59 +0000317 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700318 Rect win(getActiveWidth(s), getActiveHeight(s));
Robert Carrb5d3d262016-03-25 15:08:13 -0700319
Marissa Wall61c58622018-07-18 10:12:20 -0700320 Rect crop = getCrop(s);
321 if (!crop.isEmpty()) {
322 win.intersect(crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800323 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700324
Chia-I Wue41dbe62017-06-13 14:10:56 -0700325 const auto& p = mDrawingParent.promote();
Robert Carrd4ae7f32018-06-07 16:10:57 -0700326 FloatRect floatWin = win.toFloatRect();
327 FloatRect parentBounds = floatWin;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700328 if (p != nullptr) {
Robert Carrd4ae7f32018-06-07 16:10:57 -0700329 // We pass an empty Region here for reasons mirroring that of the case described in
330 // the computeScreenBounds reduceTransparentRegion=false case.
331 parentBounds = p->computeBounds(Region());
Robert Carr1f0a16a2016-10-24 16:27:39 -0700332 }
333
Peiyong Linefefaac2018-08-17 12:27:51 -0700334 ui::Transform t = s.active_legacy.transform;
Dan Stoza80d61162017-12-20 15:57:52 -0800335
Vishnu Nairdcce0e22018-08-23 08:35:19 -0700336 if (p != nullptr) {
Dan Stoza80d61162017-12-20 15:57:52 -0800337 floatWin = t.transform(floatWin);
Robert Carrd4ae7f32018-06-07 16:10:57 -0700338 floatWin = floatWin.intersect(parentBounds);
Dan Stoza80d61162017-12-20 15:57:52 -0800339 floatWin = t.inverse().transform(floatWin);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700340 }
341
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700342 // subtract the transparent region and snap to the bounds
Dan Stoza80d61162017-12-20 15:57:52 -0800343 return reduce(floatWin, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800344}
345
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700346Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& display) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700347 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800348 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700349 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800350
351 // apply the projection's clipping to the window crop in
352 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700353 // if there are no window scaling involved, this operation will map to full
354 // pixels in the buffer.
355 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
356 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700357
Marissa Wall61c58622018-07-18 10:12:20 -0700358 Rect activeCrop(getActiveWidth(s), getActiveHeight(s));
359 Rect crop = getCrop(s);
360 if (!crop.isEmpty()) {
361 activeCrop.intersect(crop, &activeCrop);
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700362 }
363
Peiyong Linefefaac2018-08-17 12:27:51 -0700364 ui::Transform t = getTransform();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700365 activeCrop = t.transform(activeCrop);
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700366 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000367 activeCrop.clear();
368 }
chaviwb1154d12017-10-31 14:15:36 -0700369
370 const auto& p = mDrawingParent.promote();
371 if (p != nullptr) {
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700372 auto parentCrop = p->computeInitialCrop(display);
chaviwb1154d12017-10-31 14:15:36 -0700373 activeCrop.intersect(parentCrop, &activeCrop);
374 }
375
Robert Carr1f0a16a2016-10-24 16:27:39 -0700376 return activeCrop;
377}
378
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700379FloatRect Layer::computeCrop(const sp<const DisplayDevice>& display) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700380 // the content crop is the area of the content that gets scaled to the
381 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800382 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700383
384 // In addition there is a WM-specified crop we pull from our drawing state.
385 const State& s(getDrawingState());
386
387 // Screen space to make reduction to parent crop clearer.
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700388 Rect activeCrop = computeInitialCrop(display);
Peiyong Linefefaac2018-08-17 12:27:51 -0700389 ui::Transform t = getTransform();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700390 // Back to layer space to work with the content crop.
391 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800392
Michael Lentine28ea2172014-11-19 18:32:37 -0800393 // This needs to be here as transform.transform(Rect) computes the
394 // transformed rect and then takes the bounding box of the result before
395 // returning. This means
396 // transform.inverse().transform(transform.transform(Rect)) != Rect
397 // in which case we need to make sure the final rect is clipped to the
398 // display bounds.
Marissa Wall61c58622018-07-18 10:12:20 -0700399 if (!activeCrop.intersect(Rect(getActiveWidth(s), getActiveHeight(s)), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000400 activeCrop.clear();
401 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800402
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700403 // subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700404 activeCrop = reduce(activeCrop, getActiveTransparentRegion(s));
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700405
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000406 // Transform the window crop to match the buffer coordinate system,
407 // which means using the inverse of the current transform set on the
408 // SurfaceFlingerConsumer.
409 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700410 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000411 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700412 * the code below applies the primary display's inverse transform to the
413 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000414 */
David Sodman41fdfc92017-11-06 16:09:56 -0800415 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000416 // calculate the inverse transform
417 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800418 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800419 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000420 // and apply to the current transform
Peiyong Linefefaac2018-08-17 12:27:51 -0700421 invTransform = (ui::Transform(invTransformOrient) *
422 ui::Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800423 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000424
Marissa Wall61c58622018-07-18 10:12:20 -0700425 int winWidth = getActiveWidth(s);
426 int winHeight = getActiveHeight(s);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000427 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
428 // If the activeCrop has been rotate the ends are rotated but not
429 // the space itself so when transforming ends back we can't rely on
430 // a modification of the axes of rotation. To account for this we
431 // need to reorient the inverse rotation in terms of the current
432 // axes of rotation.
433 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
434 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
435 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800436 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000437 }
Marissa Wall61c58622018-07-18 10:12:20 -0700438 winWidth = getActiveHeight(s);
439 winHeight = getActiveWidth(s);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000440 }
Marissa Wall61c58622018-07-18 10:12:20 -0700441 const Rect winCrop = activeCrop.transform(invTransform, getActiveWidth(s), getActiveHeight(s));
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000442
443 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800444 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000445 float yScale = crop.getHeight() / float(winHeight);
446
David Sodman41fdfc92017-11-06 16:09:56 -0800447 float insetL = winCrop.left * xScale;
448 float insetT = winCrop.top * yScale;
449 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000450 float insetB = (winHeight - winCrop.bottom) * yScale;
451
David Sodman41fdfc92017-11-06 16:09:56 -0800452 crop.left += insetL;
453 crop.top += insetT;
454 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000455 crop.bottom -= insetB;
456
Mathias Agopian13127d82013-03-05 17:47:11 -0800457 return crop;
458}
459
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700460void Layer::setGeometry(const sp<const DisplayDevice>& display, uint32_t z) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700461 const auto displayId = display->getId();
Peiyong Lin91b1df22018-06-18 18:00:16 -0700462 if (!hasHwcLayer(displayId)) {
463 ALOGE("[%s] failed to setGeometry: no HWC layer found (%d)",
464 mName.string(), displayId);
465 return;
466 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700467 auto& hwcInfo = getBE().mHwcLayers[displayId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700468
David Sodman10a41ff2018-08-05 12:14:17 -0700469 // Device or Cursor layers
470 if (mPotentialCursor) {
471 ALOGV("[%s] Requesting Cursor composition", mName.string());
472 setCompositionType(displayId, HWC2::Composition::Cursor);
473 } else {
474 ALOGV("[%s] Requesting Device composition", mName.string());
475 setCompositionType(displayId, HWC2::Composition::Device);
476 }
477
478 // Need to program geometry parts
479 getBE().compositionInfo.hwc.skipGeometry = false;
480
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700481 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800482 hwcInfo.forceClientComposition = false;
483
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700484 if (isSecure() && !display->isSecure()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800485 hwcInfo.forceClientComposition = true;
486 }
487
Mathias Agopian13127d82013-03-05 17:47:11 -0800488 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700489 const State& s(getDrawingState());
David Revemanecf0fa52017-03-03 11:32:44 -0500490 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700491 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800492 blendMode =
493 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800494 }
David Sodmanba340492018-08-05 21:51:33 -0700495 getBE().compositionInfo.hwc.blendMode = blendMode;
Mathias Agopian13127d82013-03-05 17:47:11 -0800496
497 // apply the layer's transform, followed by the display's global transform
498 // here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700499 Region activeTransparentRegion(getActiveTransparentRegion(s));
Peiyong Linefefaac2018-08-17 12:27:51 -0700500 ui::Transform t = getTransform();
Marissa Wall61c58622018-07-18 10:12:20 -0700501 Rect activeCrop = getCrop(s);
502 if (!activeCrop.isEmpty()) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700503 activeCrop = t.transform(activeCrop);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700504 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000505 activeCrop.clear();
506 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700507 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800508 // This needs to be here as transform.transform(Rect) computes the
509 // transformed rect and then takes the bounding box of the result before
510 // returning. This means
511 // transform.inverse().transform(transform.transform(Rect)) != Rect
512 // in which case we need to make sure the final rect is clipped to the
513 // display bounds.
Marissa Wall61c58622018-07-18 10:12:20 -0700514 if (!activeCrop.intersect(Rect(getActiveWidth(s), getActiveHeight(s)), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000515 activeCrop.clear();
516 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700517 // mark regions outside the crop as transparent
Marissa Wall61c58622018-07-18 10:12:20 -0700518 activeTransparentRegion.orSelf(Rect(0, 0, getActiveWidth(s), activeCrop.top));
Marissa Wallf58c14b2018-07-24 10:50:43 -0700519 activeTransparentRegion.orSelf(
Marissa Wall61c58622018-07-18 10:12:20 -0700520 Rect(0, activeCrop.bottom, getActiveWidth(s), getActiveHeight(s)));
David Sodman41fdfc92017-11-06 16:09:56 -0800521 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
522 activeTransparentRegion.orSelf(
Marissa Wall61c58622018-07-18 10:12:20 -0700523 Rect(activeCrop.right, activeCrop.top, getActiveWidth(s), activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700524 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700525
Dan Stoza80d61162017-12-20 15:57:52 -0800526 // computeBounds returns a FloatRect to provide more accuracy during the
527 // transformation. We then round upon constructing 'frame'.
528 Rect frame{t.transform(computeBounds(activeTransparentRegion))};
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700529 if (!frame.intersect(display->getViewport(), &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000530 frame.clear();
531 }
Peiyong Linefefaac2018-08-17 12:27:51 -0700532 const ui::Transform& tr = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800533 Rect transformedFrame = tr.transform(frame);
David Sodmanba340492018-08-05 21:51:33 -0700534 getBE().compositionInfo.hwc.displayFrame = transformedFrame;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800535
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700536 FloatRect sourceCrop = computeCrop(display);
David Sodmanba340492018-08-05 21:51:33 -0700537 getBE().compositionInfo.hwc.sourceCrop = sourceCrop;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800538
chaviw13fdc492017-06-27 12:40:18 -0700539 float alpha = static_cast<float>(getAlpha());
David Sodmanba340492018-08-05 21:51:33 -0700540 getBE().compositionInfo.hwc.alpha = alpha;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800541
David Sodmanba340492018-08-05 21:51:33 -0700542 getBE().compositionInfo.hwc.z = z;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500543
Albert Chaulk2a589632017-05-04 16:59:44 -0400544 int type = s.type;
545 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700546 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400547 if (parent.get()) {
548 auto& parentState = parent->getDrawingState();
rongliucfb187b2018-03-14 12:26:23 -0700549 if (parentState.type >= 0 || parentState.appId >= 0) {
550 type = parentState.type;
551 appId = parentState.appId;
552 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400553 }
554
David Sodmanba340492018-08-05 21:51:33 -0700555 getBE().compositionInfo.hwc.type = type;
556 getBE().compositionInfo.hwc.appId = appId;
557
Mathias Agopian29a367b2011-07-12 14:51:45 -0700558 /*
559 * Transformations are applied in this order:
560 * 1) buffer orientation/flip/mirror
561 * 2) state transformation (window manager)
562 * 3) layer orientation (screen orientation)
563 * (NOTE: the matrices are multiplied in reverse order)
564 */
565
Peiyong Linefefaac2018-08-17 12:27:51 -0700566 const ui::Transform bufferOrientation(mCurrentTransform);
567 ui::Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700568
Robert Carrcae605c2017-03-29 12:10:31 -0700569 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700570 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700571 * the code below applies the primary display's inverse transform to the
572 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700573 */
David Sodman41fdfc92017-11-06 16:09:56 -0800574 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700575 // calculate the inverse transform
576 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800577 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700578 }
Robert Carrcae605c2017-03-29 12:10:31 -0700579
580 /*
581 * Here we cancel out the orientation component of the WM transform.
582 * The scaling and translate components are already included in our bounds
583 * computation so it's enough to just omit it in the composition.
584 * See comment in onDraw with ref to b/36727915 for why.
585 */
Peiyong Linefefaac2018-08-17 12:27:51 -0700586 transform = ui::Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700587 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700588
589 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800590 const uint32_t orientation = transform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -0700591 if (orientation & ui::Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800592 // we can only handle simple transformation
Lloyd Pique074e8122018-07-26 12:57:23 -0700593 hwcInfo.forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800594 } else {
595 auto transform = static_cast<HWC2::Transform>(orientation);
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800596 hwcInfo.transform = transform;
David Sodmanba340492018-08-05 21:51:33 -0700597 getBE().compositionInfo.hwc.transform = transform;
David Sodman4b7c4bc2017-11-17 12:13:59 -0800598 }
599}
600
Dominik Laskowski7e045462018-05-30 13:02:02 -0700601void Layer::forceClientComposition(int32_t displayId) {
602 if (getBE().mHwcLayers.count(displayId) == 0) {
603 ALOGE("forceClientComposition: no HWC layer found (%d)", displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800604 return;
605 }
606
Dominik Laskowski7e045462018-05-30 13:02:02 -0700607 getBE().mHwcLayers[displayId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800608}
Dan Stozaee44edd2015-03-23 15:50:23 -0700609
Dominik Laskowski7e045462018-05-30 13:02:02 -0700610bool Layer::getForceClientComposition(int32_t displayId) {
611 if (getBE().mHwcLayers.count(displayId) == 0) {
612 ALOGE("getForceClientComposition: no HWC layer found (%d)", displayId);
chaviwc9232ed2017-11-14 15:31:15 -0800613 return false;
614 }
615
Dominik Laskowski7e045462018-05-30 13:02:02 -0700616 return getBE().mHwcLayers[displayId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800617}
618
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700619void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700620 const auto displayId = display->getId();
621 if (getBE().mHwcLayers.count(displayId) == 0 ||
622 getCompositionType(displayId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800623 return;
624 }
625
626 // This gives us only the "orientation" component of the transform
627 const State& s(getCurrentState());
628
629 // Apply the layer's transform, followed by the display's global transform
630 // Here we're guaranteed that the layer's transform preserves rects
Marissa Wall61c58622018-07-18 10:12:20 -0700631 Rect win(getActiveWidth(s), getActiveHeight(s));
632 Rect crop = getCrop(s);
633 if (!crop.isEmpty()) {
634 win.intersect(crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800635 }
636 // Subtract the transparent region and snap to the bounds
Marissa Wall61c58622018-07-18 10:12:20 -0700637 Rect bounds = reduce(win, getActiveTransparentRegion(s));
Robert Carr1f0a16a2016-10-24 16:27:39 -0700638 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700639 frame.intersect(display->getViewport(), &frame);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700640 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800641 auto position = displayTransform.transform(frame);
642
Dominik Laskowski7e045462018-05-30 13:02:02 -0700643 auto error =
David Sodmanb8aaea12017-12-14 15:54:51 -0800644 (getBE().mHwcLayers[displayId].layer)->setCursorPosition(
645 position.left, position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800646 ALOGE_IF(error != HWC2::Error::None,
647 "[%s] Failed to set cursor position "
648 "to (%d, %d): %s (%d)",
649 mName.string(), position.left, position.top, to_string(error).c_str(),
650 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800651}
Riley Andrews03414a12014-07-01 14:22:59 -0700652
Mathias Agopian13127d82013-03-05 17:47:11 -0800653// ---------------------------------------------------------------------------
654// drawing...
655// ---------------------------------------------------------------------------
656
Marissa Wall61c58622018-07-18 10:12:20 -0700657void Layer::draw(const RenderArea& renderArea, const Region& clip) {
chaviwa76b2712017-09-20 12:02:26 -0700658 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800659}
660
Marissa Wall61c58622018-07-18 10:12:20 -0700661void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) {
chaviwa76b2712017-09-20 12:02:26 -0700662 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800663}
664
David Sodman41fdfc92017-11-06 16:09:56 -0800665void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
666 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800667 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700668 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700669 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700670 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800671}
672
David Sodmanc1498e62018-09-12 14:36:26 -0700673void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
674 clearWithOpenGL(renderArea, 0, 0, 0, 0);
675}
676
David Sodman10a41ff2018-08-05 12:14:17 -0700677void Layer::setCompositionType(int32_t displayId, HWC2::Composition type, bool /*callIntoHwc*/) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700678 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu30505fb2018-03-26 16:20:31 -0700679 ALOGE("setCompositionType called without a valid HWC layer");
680 return;
David Sodman10a41ff2018-08-05 12:14:17 -0700681 } else {
682 if (getBE().mHwcLayers[displayId].compositionType != type) {
683 ALOGV("setCompositionType: Changing compositionType from %s to %s",
684 to_string(getBE().mHwcLayers[displayId].compositionType).c_str(),
685 to_string(type).c_str());
686 getBE().mHwcLayers[displayId].compositionType = type;
Chia-I Wu30505fb2018-03-26 16:20:31 -0700687 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800688 }
689}
690
Dominik Laskowski7e045462018-05-30 13:02:02 -0700691HWC2::Composition Layer::getCompositionType(int32_t displayId) const {
David Sodman15fb96e2018-01-07 10:23:24 -0800692 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700693 // If we're querying the composition type for a display that does not
694 // have a HWC counterpart, then it will always be Client
695 return HWC2::Composition::Client;
696 }
David Sodman15fb96e2018-01-07 10:23:24 -0800697 return getBE().mHwcLayers[displayId].compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800698}
699
Dominik Laskowski7e045462018-05-30 13:02:02 -0700700void Layer::setClearClientTarget(int32_t displayId, bool clear) {
701 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800702 ALOGE("setClearClientTarget called without a valid HWC layer");
703 return;
704 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700705 getBE().mHwcLayers[displayId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800706}
707
Dominik Laskowski7e045462018-05-30 13:02:02 -0700708bool Layer::getClearClientTarget(int32_t displayId) const {
709 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800710 ALOGE("getClearClientTarget called without a valid HWC layer");
711 return false;
712 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700713 return getBE().mHwcLayers.at(displayId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800714}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800715
Dan Stozacac35382016-01-27 12:21:06 -0800716bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
717 if (point->getFrameNumber() <= mCurrentFrameNumber) {
718 // Don't bother with a SyncPoint, since we've already latched the
719 // relevant frame
720 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700721 }
722
Dan Stozacac35382016-01-27 12:21:06 -0800723 Mutex::Autolock lock(mLocalSyncPointMutex);
724 mLocalSyncPoints.push_back(point);
725 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700726}
727
Mathias Agopian13127d82013-03-05 17:47:11 -0800728// ----------------------------------------------------------------------------
729// local state
730// ----------------------------------------------------------------------------
731
Peiyong Lin833074a2018-08-28 11:53:54 -0700732void Layer::computeGeometry(const RenderArea& renderArea,
733 renderengine::Mesh& mesh,
chaviwa76b2712017-09-20 12:02:26 -0700734 bool useIdentityTransform) const {
Peiyong Linefefaac2018-08-17 12:27:51 -0700735 const ui::Transform renderAreaTransform(renderArea.getTransform());
Dan Stoza80d61162017-12-20 15:57:52 -0800736 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700737
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000738 vec2 lt = vec2(win.left, win.top);
739 vec2 lb = vec2(win.left, win.bottom);
740 vec2 rb = vec2(win.right, win.bottom);
741 vec2 rt = vec2(win.right, win.top);
742
Peiyong Linefefaac2018-08-17 12:27:51 -0700743 ui::Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000744 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700745 lt = layerTransform.transform(lt);
746 lb = layerTransform.transform(lb);
747 rb = layerTransform.transform(rb);
748 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000749 }
750
Peiyong Lin833074a2018-08-28 11:53:54 -0700751 renderengine::Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700752 position[0] = renderAreaTransform.transform(lt);
753 position[1] = renderAreaTransform.transform(lb);
754 position[2] = renderAreaTransform.transform(rb);
755 position[3] = renderAreaTransform.transform(rt);
Mathias Agopian13127d82013-03-05 17:47:11 -0800756}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800757
David Sodman41fdfc92017-11-06 16:09:56 -0800758bool Layer::isSecure() const {
Alec Mourib416efd2018-09-06 21:01:59 +0000759 const State& s(mDrawingState);
Dan Stoza23116082015-06-18 14:58:39 -0700760 return (s.flags & layer_state_t::eLayerSecure);
761}
762
Mathias Agopian13127d82013-03-05 17:47:11 -0800763void Layer::setVisibleRegion(const Region& visibleRegion) {
764 // always called from main thread
765 this->visibleRegion = visibleRegion;
766}
767
768void Layer::setCoveredRegion(const Region& coveredRegion) {
769 // always called from main thread
770 this->coveredRegion = coveredRegion;
771}
772
David Sodman41fdfc92017-11-06 16:09:56 -0800773void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800774 // always called from main thread
775 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
776}
777
Robert Carre5f4f692018-01-12 13:12:28 -0800778void Layer::clearVisibilityRegions() {
779 visibleRegion.clear();
780 visibleNonTransparentRegion.clear();
781 coveredRegion.clear();
782}
783
Mathias Agopian13127d82013-03-05 17:47:11 -0800784// ----------------------------------------------------------------------------
785// transaction
786// ----------------------------------------------------------------------------
787
Dan Stoza7dde5992015-05-22 09:51:44 -0700788void Layer::pushPendingState() {
789 if (!mCurrentState.modified) {
790 return;
791 }
792
Dan Stoza7dde5992015-05-22 09:51:44 -0700793 // If this transaction is waiting on the receipt of a frame, generate a sync
794 // point and send it to the remote layer.
Robert Carrf478f9f2018-10-15 12:43:33 -0700795 // We don't allow installing sync points after we are removed from the current state
796 // as we won't be able to signal our end.
797 if (mCurrentState.barrierLayer_legacy != nullptr && !mRemovedFromCurrentState) {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700798 sp<Layer> barrierLayer = mCurrentState.barrierLayer_legacy.promote();
Robert Carr0d480722017-01-10 16:42:54 -0800799 if (barrierLayer == nullptr) {
800 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700801 // If we can't promote the layer we are intended to wait on,
802 // then it is expired or otherwise invalid. Allow this transaction
803 // to be applied as per normal (no synchronization).
Marissa Wallf58c14b2018-07-24 10:50:43 -0700804 mCurrentState.barrierLayer_legacy = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800805 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700806 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber_legacy);
Robert Carr0d480722017-01-10 16:42:54 -0800807 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800808 mRemoteSyncPoints.push_back(std::move(syncPoint));
809 } else {
810 // We already missed the frame we're supposed to synchronize
811 // on, so go ahead and apply the state update
Marissa Wallf58c14b2018-07-24 10:50:43 -0700812 mCurrentState.barrierLayer_legacy = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800813 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700814 }
815
Dan Stoza7dde5992015-05-22 09:51:44 -0700816 // Wake us up to check if the frame has been received
817 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700818 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700819 }
820 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700821 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700822}
823
Pablo Ceballos05289c22016-04-14 15:49:55 -0700824void Layer::popPendingState(State* stateToCommit) {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700825 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700826
827 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700828 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700829}
830
Pablo Ceballos05289c22016-04-14 15:49:55 -0700831bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700832 bool stateUpdateAvailable = false;
833 while (!mPendingStates.empty()) {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700834 if (mPendingStates[0].barrierLayer_legacy != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700835 if (mRemoteSyncPoints.empty()) {
836 // If we don't have a sync point for this, apply it anyway. It
837 // will be visually wrong, but it should keep us from getting
838 // into too much trouble.
839 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700840 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700841 stateUpdateAvailable = true;
842 continue;
843 }
844
Marissa Wallf58c14b2018-07-24 10:50:43 -0700845 if (mRemoteSyncPoints.front()->getFrameNumber() !=
846 mPendingStates[0].frameNumber_legacy) {
David Sodman41fdfc92017-11-06 16:09:56 -0800847 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800848
849 // Signal our end of the sync point and then dispose of it
850 mRemoteSyncPoints.front()->setTransactionApplied();
851 mRemoteSyncPoints.pop_front();
852 continue;
853 }
854
Dan Stoza7dde5992015-05-22 09:51:44 -0700855 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
856 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700857 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700858 stateUpdateAvailable = true;
859
860 // Signal our end of the sync point and then dispose of it
861 mRemoteSyncPoints.front()->setTransactionApplied();
862 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800863 } else {
864 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700865 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700866 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700867 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700868 stateUpdateAvailable = true;
869 }
870 }
871
872 // If we still have pending updates, wake SurfaceFlinger back up and point
873 // it at this layer so we can process them
874 if (!mPendingStates.empty()) {
875 setTransactionFlags(eTransactionNeeded);
876 mFlinger->setTransactionFlags(eTraversalNeeded);
877 }
878
879 mCurrentState.modified = false;
880 return stateUpdateAvailable;
881}
882
Marissa Wall61c58622018-07-18 10:12:20 -0700883uint32_t Layer::doTransactionResize(uint32_t flags, State* stateToCommit) {
Alec Mourib416efd2018-09-06 21:01:59 +0000884 const State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800885
Marissa Wall61c58622018-07-18 10:12:20 -0700886 const bool sizeChanged = (stateToCommit->requested_legacy.w != s.requested_legacy.w) ||
887 (stateToCommit->requested_legacy.h != s.requested_legacy.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700888
David Sodmaneb085e02017-10-05 18:49:04 -0700889 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700890 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000891 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800892 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
893 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
894 " requested={ wh={%4u,%4u} }}\n"
895 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
896 " requested={ wh={%4u,%4u} }}\n",
Marissa Wallf58c14b2018-07-24 10:50:43 -0700897 this, getName().string(), mCurrentTransform, getEffectiveScalingMode(),
Marissa Wall61c58622018-07-18 10:12:20 -0700898 stateToCommit->active_legacy.w, stateToCommit->active_legacy.h,
899 stateToCommit->crop_legacy.left, stateToCommit->crop_legacy.top,
900 stateToCommit->crop_legacy.right, stateToCommit->crop_legacy.bottom,
901 stateToCommit->crop_legacy.getWidth(), stateToCommit->crop_legacy.getHeight(),
902 stateToCommit->requested_legacy.w, stateToCommit->requested_legacy.h,
Marissa Wallf58c14b2018-07-24 10:50:43 -0700903 s.active_legacy.w, s.active_legacy.h, s.crop_legacy.left, s.crop_legacy.top,
904 s.crop_legacy.right, s.crop_legacy.bottom, s.crop_legacy.getWidth(),
905 s.crop_legacy.getHeight(), s.requested_legacy.w, s.requested_legacy.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800906 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700907
Robert Carre392b552017-09-19 12:16:05 -0700908 // Don't let Layer::doTransaction update the drawing state
909 // if we have a pending resize, unless we are in fixed-size mode.
910 // the drawing state will be updated only once we receive a buffer
911 // with the correct size.
912 //
913 // In particular, we want to make sure the clip (which is part
914 // of the geometry state) is latched together with the size but is
915 // latched immediately when no resizing is involved.
916 //
917 // If a sideband stream is attached, however, we want to skip this
918 // optimization so that transactions aren't missed when a buffer
919 // never arrives
920 //
921 // In the case that we don't have a buffer we ignore other factors
922 // and avoid entering the resizePending state. At a high level the
923 // resizePending state is to avoid applying the state of the new buffer
924 // to the old buffer. However in the state where we don't have an old buffer
925 // there is no such concern but we may still be being used as a parent layer.
Marissa Wall61c58622018-07-18 10:12:20 -0700926 const bool resizePending =
927 ((stateToCommit->requested_legacy.w != stateToCommit->active_legacy.w) ||
928 (stateToCommit->requested_legacy.h != stateToCommit->active_legacy.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -0800929 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700930 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -0800931 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -0700932 flags |= eDontUpdateGeometryState;
933 }
934 }
935
Robert Carr7bf247e2017-05-18 14:02:49 -0700936 // Here we apply various requested geometry states, depending on our
937 // latching configuration. See Layer.h for a detailed discussion of
938 // how geometry latching is controlled.
939 if (!(flags & eDontUpdateGeometryState)) {
Alec Mourib416efd2018-09-06 21:01:59 +0000940 State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -0700941
942 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
943 // mode, which causes attributes which normally latch regardless of scaling mode,
944 // to be delayed. We copy the requested state to the active state making sure
945 // to respect these rules (again see Layer.h for a detailed discussion).
946 //
947 // There is an awkward asymmetry in the handling of the crop states in the position
948 // states, as can be seen below. Largely this arises from position and transform
949 // being stored in the same data structure while having different latching rules.
950 // b/38182305
951 //
Marissa Wall61c58622018-07-18 10:12:20 -0700952 // Careful that "stateToCommit" and editCurrentState may not begin as equivalent due to
Robert Carr7bf247e2017-05-18 14:02:49 -0700953 // applyPendingStates in the presence of deferred transactions.
954 if (mFreezeGeometryUpdates) {
Marissa Wall61c58622018-07-18 10:12:20 -0700955 float tx = stateToCommit->active_legacy.transform.tx();
956 float ty = stateToCommit->active_legacy.transform.ty();
957 stateToCommit->active_legacy = stateToCommit->requested_legacy;
958 stateToCommit->active_legacy.transform.set(tx, ty);
959 editCurrentState.active_legacy = stateToCommit->active_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700960 } else {
Marissa Wallf58c14b2018-07-24 10:50:43 -0700961 editCurrentState.active_legacy = editCurrentState.requested_legacy;
Marissa Wall61c58622018-07-18 10:12:20 -0700962 stateToCommit->active_legacy = stateToCommit->requested_legacy;
Robert Carr82364e32016-05-15 11:27:47 -0700963 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800964 }
965
Marissa Wall61c58622018-07-18 10:12:20 -0700966 return flags;
967}
968
969uint32_t Layer::doTransaction(uint32_t flags) {
970 ATRACE_CALL();
971
972 pushPendingState();
Alec Mourib416efd2018-09-06 21:01:59 +0000973 State c = getCurrentState();
Marissa Wall61c58622018-07-18 10:12:20 -0700974 if (!applyPendingStates(&c)) {
975 return 0;
976 }
977
978 flags = doTransactionResize(flags, &c);
979
Alec Mourib416efd2018-09-06 21:01:59 +0000980 const State& s(getDrawingState());
Marissa Wall61c58622018-07-18 10:12:20 -0700981
982 if (getActiveGeometry(c) != getActiveGeometry(s)) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800983 // invalidate and recompute the visible regions if needed
984 flags |= Layer::eVisibleRegion;
985 }
986
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700987 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800988 // invalidate and recompute the visible regions if needed
989 flags |= eVisibleRegion;
990 this->contentDirty = true;
991
992 // we may use linear filtering, if the matrix scales us
Marissa Wall61c58622018-07-18 10:12:20 -0700993 const uint8_t type = getActiveTransform(c).getType();
Peiyong Linefefaac2018-08-17 12:27:51 -0700994 mNeedsFiltering = (!getActiveTransform(c).preserveRects() || type >= ui::Transform::SCALE);
Mathias Agopian13127d82013-03-05 17:47:11 -0800995 }
996
Dan Stozac8145172016-04-28 16:29:06 -0700997 // If the layer is hidden, signal and clear out all local sync points so
998 // that transactions for layers depending on this layer's frames becoming
999 // visible are not blocked
1000 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001001 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001002 }
1003
Mathias Agopian13127d82013-03-05 17:47:11 -08001004 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001005 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001006 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001007}
1008
Pablo Ceballos05289c22016-04-14 15:49:55 -07001009void Layer::commitTransaction(const State& stateToCommit) {
1010 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001011}
1012
Mathias Agopian13127d82013-03-05 17:47:11 -08001013uint32_t Layer::getTransactionFlags(uint32_t flags) {
Lloyd Piquef1c675b2018-09-12 20:45:39 -07001014 return mTransactionFlags.fetch_and(~flags) & flags;
Mathias Agopian13127d82013-03-05 17:47:11 -08001015}
1016
1017uint32_t Layer::setTransactionFlags(uint32_t flags) {
Lloyd Piquef1c675b2018-09-12 20:45:39 -07001018 return mTransactionFlags.fetch_or(flags);
Mathias Agopian13127d82013-03-05 17:47:11 -08001019}
1020
Robert Carr82364e32016-05-15 11:27:47 -07001021bool Layer::setPosition(float x, float y, bool immediate) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001022 if (mCurrentState.requested_legacy.transform.tx() == x &&
1023 mCurrentState.requested_legacy.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001024 return false;
1025 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001026
1027 // We update the requested and active position simultaneously because
1028 // we want to apply the position portion of the transform matrix immediately,
1029 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Marissa Wallf58c14b2018-07-24 10:50:43 -07001030 mCurrentState.requested_legacy.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001031 if (immediate && !mFreezeGeometryUpdates) {
1032 // Here we directly update the active state
1033 // unlike other setters, because we store it within
1034 // the transform, but use different latching rules.
1035 // b/38182305
Marissa Wallf58c14b2018-07-24 10:50:43 -07001036 mCurrentState.active_legacy.transform.set(x, y);
Robert Carr82364e32016-05-15 11:27:47 -07001037 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001038 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001039
Dan Stoza7dde5992015-05-22 09:51:44 -07001040 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001041 setTransactionFlags(eTransactionNeeded);
1042 return true;
1043}
Robert Carr82364e32016-05-15 11:27:47 -07001044
Robert Carr1f0a16a2016-10-24 16:27:39 -07001045bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1046 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1047 if (idx < 0) {
1048 return false;
1049 }
1050 if (childLayer->setLayer(z)) {
1051 mCurrentChildren.removeAt(idx);
1052 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001053 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001054 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001055 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001056}
1057
Robert Carr503c7042017-09-27 15:06:08 -07001058bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1059 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1060 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1061 if (idx < 0) {
1062 return false;
1063 }
1064 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1065 mCurrentChildren.removeAt(idx);
1066 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001067 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001068 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001069 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001070}
1071
Robert Carrae060832016-11-28 10:51:00 -08001072bool Layer::setLayer(int32_t z) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001073 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001074 mCurrentState.sequence++;
1075 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001076 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001077
1078 // Discard all relative layering.
1079 if (mCurrentState.zOrderRelativeOf != nullptr) {
1080 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1081 if (strongRelative != nullptr) {
1082 strongRelative->removeZOrderRelative(this);
1083 }
1084 mCurrentState.zOrderRelativeOf = nullptr;
1085 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001086 setTransactionFlags(eTransactionNeeded);
1087 return true;
1088}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001089
Robert Carrdb66e622017-04-10 16:55:57 -07001090void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1091 mCurrentState.zOrderRelatives.remove(relative);
1092 mCurrentState.sequence++;
1093 mCurrentState.modified = true;
1094 setTransactionFlags(eTransactionNeeded);
1095}
1096
1097void Layer::addZOrderRelative(const wp<Layer>& relative) {
1098 mCurrentState.zOrderRelatives.add(relative);
1099 mCurrentState.modified = true;
1100 mCurrentState.sequence++;
1101 setTransactionFlags(eTransactionNeeded);
1102}
1103
Robert Carr503d2bd2017-12-04 15:49:47 -08001104bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001105 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1106 if (handle == nullptr) {
1107 return false;
1108 }
1109 sp<Layer> relative = handle->owner.promote();
1110 if (relative == nullptr) {
1111 return false;
1112 }
1113
Robert Carr503d2bd2017-12-04 15:49:47 -08001114 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1115 mCurrentState.zOrderRelativeOf == relative) {
1116 return false;
1117 }
1118
Robert Carrdb66e622017-04-10 16:55:57 -07001119 mCurrentState.sequence++;
1120 mCurrentState.modified = true;
Robert Carr503d2bd2017-12-04 15:49:47 -08001121 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001122
chaviw9ab4bd12017-11-03 13:11:00 -07001123 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1124 if (oldZOrderRelativeOf != nullptr) {
1125 oldZOrderRelativeOf->removeZOrderRelative(this);
1126 }
Robert Carrdb66e622017-04-10 16:55:57 -07001127 mCurrentState.zOrderRelativeOf = relative;
1128 relative->addZOrderRelative(this);
1129
1130 setTransactionFlags(eTransactionNeeded);
1131
1132 return true;
1133}
1134
Mathias Agopian13127d82013-03-05 17:47:11 -08001135bool Layer::setSize(uint32_t w, uint32_t h) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001136 if (mCurrentState.requested_legacy.w == w && mCurrentState.requested_legacy.h == h)
1137 return false;
1138 mCurrentState.requested_legacy.w = w;
1139 mCurrentState.requested_legacy.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001140 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001141 setTransactionFlags(eTransactionNeeded);
Vishnu Naird01c4432018-08-13 10:38:47 -07001142
1143 // record the new size, from this point on, when the client request
1144 // a buffer, it'll get the new size.
1145 setDefaultBufferSize(mCurrentState.requested_legacy.w, mCurrentState.requested_legacy.h);
Mathias Agopian13127d82013-03-05 17:47:11 -08001146 return true;
1147}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001148bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001149 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001150 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001151 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001152 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001153 setTransactionFlags(eTransactionNeeded);
1154 return true;
1155}
chaviw13fdc492017-06-27 12:40:18 -07001156
1157bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001158 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1159 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001160 return false;
1161
1162 mCurrentState.sequence++;
1163 mCurrentState.color.r = color.r;
1164 mCurrentState.color.g = color.g;
1165 mCurrentState.color.b = color.b;
1166 mCurrentState.modified = true;
1167 setTransactionFlags(eTransactionNeeded);
1168 return true;
1169}
1170
Robert Carrd4ae7f32018-06-07 16:10:57 -07001171bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1172 bool allowNonRectPreservingTransforms) {
Peiyong Linefefaac2018-08-17 12:27:51 -07001173 ui::Transform t;
Robert Carrd4ae7f32018-06-07 16:10:57 -07001174 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1175
1176 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1177 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1178 return false;
1179 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001180 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001181 mCurrentState.requested_legacy.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx,
1182 matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001183 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001184 setTransactionFlags(eTransactionNeeded);
1185 return true;
1186}
Marissa Wall61c58622018-07-18 10:12:20 -07001187
Mathias Agopian13127d82013-03-05 17:47:11 -08001188bool Layer::setTransparentRegionHint(const Region& transparent) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001189 mCurrentState.requestedTransparentRegion_legacy = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001190 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001191 setTransactionFlags(eTransactionNeeded);
1192 return true;
1193}
1194bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1195 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001196 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001197 mCurrentState.sequence++;
1198 mCurrentState.flags = newFlags;
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 Carr99e27f02016-06-16 15:18:02 -07001203
Marissa Wallf58c14b2018-07-24 10:50:43 -07001204bool Layer::setCrop_legacy(const Rect& crop, bool immediate) {
1205 if (mCurrentState.requestedCrop_legacy == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001206 mCurrentState.sequence++;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001207 mCurrentState.requestedCrop_legacy = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001208 if (immediate && !mFreezeGeometryUpdates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001209 mCurrentState.crop_legacy = crop;
Robert Carr99e27f02016-06-16 15:18:02 -07001210 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001211 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1212
Dan Stoza7dde5992015-05-22 09:51:44 -07001213 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001214 setTransactionFlags(eTransactionNeeded);
1215 return true;
1216}
Robert Carr8d5227b2017-03-16 15:41:03 -07001217
Robert Carrc3574f72016-03-24 12:19:32 -07001218bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001219 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001220 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001221 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001222 return true;
1223}
1224
rongliucfb187b2018-03-14 12:26:23 -07001225void Layer::setInfo(int32_t type, int32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001226 mCurrentState.appId = appId;
1227 mCurrentState.type = type;
1228 mCurrentState.modified = true;
1229 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001230}
1231
Mathias Agopian13127d82013-03-05 17:47:11 -08001232bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001233 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001234 mCurrentState.sequence++;
1235 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001236 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001237 setTransactionFlags(eTransactionNeeded);
1238 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001239}
1240
Robert Carr1f0a16a2016-10-24 16:27:39 -07001241uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001242 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001243 if (p == nullptr) {
1244 return getDrawingState().layerStack;
1245 }
1246 return p->getLayerStack();
1247}
1248
Marissa Wallf58c14b2018-07-24 10:50:43 -07001249void Layer::deferTransactionUntil_legacy(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
1250 mCurrentState.barrierLayer_legacy = barrierLayer;
1251 mCurrentState.frameNumber_legacy = frameNumber;
Dan Stoza7dde5992015-05-22 09:51:44 -07001252 // We don't set eTransactionNeeded, because just receiving a deferral
1253 // request without any other state updates shouldn't actually induce a delay
1254 mCurrentState.modified = true;
1255 pushPendingState();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001256 mCurrentState.barrierLayer_legacy = nullptr;
1257 mCurrentState.frameNumber_legacy = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001258 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001259}
1260
Marissa Wallf58c14b2018-07-24 10:50:43 -07001261void Layer::deferTransactionUntil_legacy(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001262 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001263 deferTransactionUntil_legacy(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001264}
1265
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001266// ----------------------------------------------------------------------------
1267// pageflip handling...
1268// ----------------------------------------------------------------------------
1269
Robert Carr1f0a16a2016-10-24 16:27:39 -07001270bool Layer::isHiddenByPolicy() const {
Alec Mourib416efd2018-09-06 21:01:59 +00001271 const State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001272 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001273 if (parent != nullptr && parent->isHiddenByPolicy()) {
1274 return true;
1275 }
1276 return s.flags & layer_state_t::eLayerHidden;
1277}
1278
David Sodman41fdfc92017-11-06 16:09:56 -08001279uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001280 // TODO: should we do something special if mSecure is set?
1281 if (mProtectedByApp) {
1282 // need a hardware-protected path to external video sink
1283 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001284 }
Riley Andrews03414a12014-07-01 14:22:59 -07001285 if (mPotentialCursor) {
1286 usage |= GraphicBuffer::USAGE_CURSOR;
1287 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001288 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001289 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001290}
1291
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001292void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001293 uint32_t orientation = 0;
1294 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001295 // The transform hint is used to improve performance, but we can
1296 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001297 // apply to all displays.
Peiyong Linefefaac2018-08-17 12:27:51 -07001298 const ui::Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001299 orientation = planeTransform.getOrientation();
Peiyong Linefefaac2018-08-17 12:27:51 -07001300 if (orientation & ui::Transform::ROT_INVALID) {
Mathias Agopiana4583642011-08-23 18:03:18 -07001301 orientation = 0;
1302 }
1303 }
David Sodmaneb085e02017-10-05 18:49:04 -07001304 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001305}
1306
Mathias Agopian13127d82013-03-05 17:47:11 -08001307// ----------------------------------------------------------------------------
1308// debugging
1309// ----------------------------------------------------------------------------
1310
Marissa Wall61c58622018-07-18 10:12:20 -07001311// TODO(marissaw): add new layer state info to layer debugging
Kalle Raitaa099a242017-01-11 11:17:29 -08001312LayerDebugInfo Layer::getLayerDebugInfo() const {
1313 LayerDebugInfo info;
Alec Mourib416efd2018-09-06 21:01:59 +00001314 const State& ds = getDrawingState();
Kalle Raitaa099a242017-01-11 11:17:29 -08001315 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001316 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001317 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1318 info.mType = String8(getTypeId());
Marissa Wallf58c14b2018-07-24 10:50:43 -07001319 info.mTransparentRegion = ds.activeTransparentRegion_legacy;
Kalle Raitaa099a242017-01-11 11:17:29 -08001320 info.mVisibleRegion = visibleRegion;
1321 info.mSurfaceDamageRegion = surfaceDamageRegion;
1322 info.mLayerStack = getLayerStack();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001323 info.mX = ds.active_legacy.transform.tx();
1324 info.mY = ds.active_legacy.transform.ty();
Kalle Raitaa099a242017-01-11 11:17:29 -08001325 info.mZ = ds.z;
Marissa Wallf58c14b2018-07-24 10:50:43 -07001326 info.mWidth = ds.active_legacy.w;
1327 info.mHeight = ds.active_legacy.h;
1328 info.mCrop = ds.crop_legacy;
chaviw13fdc492017-06-27 12:40:18 -07001329 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001330 info.mFlags = ds.flags;
1331 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001332 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001333 info.mMatrix[0][0] = ds.active_legacy.transform[0][0];
1334 info.mMatrix[0][1] = ds.active_legacy.transform[0][1];
1335 info.mMatrix[1][0] = ds.active_legacy.transform[1][0];
1336 info.mMatrix[1][1] = ds.active_legacy.transform[1][1];
Kalle Raitaa099a242017-01-11 11:17:29 -08001337 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001338 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001339 if (buffer != 0) {
1340 info.mActiveBufferWidth = buffer->getWidth();
1341 info.mActiveBufferHeight = buffer->getHeight();
1342 info.mActiveBufferStride = buffer->getStride();
1343 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001344 } else {
1345 info.mActiveBufferWidth = 0;
1346 info.mActiveBufferHeight = 0;
1347 info.mActiveBufferStride = 0;
1348 info.mActiveBufferFormat = 0;
1349 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001350 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001351 info.mNumQueuedFrames = getQueuedFrameCount();
1352 info.mRefreshPending = isBufferLatched();
1353 info.mIsOpaque = isOpaque(ds);
1354 info.mContentDirty = contentDirty;
1355 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001356}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001357
Dan Stozae22aec72016-08-01 13:20:59 -07001358void Layer::miniDumpHeader(String8& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001359 result.append("-------------------------------");
1360 result.append("-------------------------------");
1361 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001362 result.append(" Layer name\n");
1363 result.append(" Z | ");
1364 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001365 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001366 result.append(" Disp Frame (LTRB) | ");
1367 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001368 result.append("-------------------------------");
1369 result.append("-------------------------------");
1370 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001371}
1372
Dominik Laskowski7e045462018-05-30 13:02:02 -07001373void Layer::miniDump(String8& result, int32_t displayId) const {
1374 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stozae22aec72016-08-01 13:20:59 -07001375 return;
1376 }
1377
1378 String8 name;
1379 if (mName.length() > 77) {
1380 std::string shortened;
1381 shortened.append(mName.string(), 36);
1382 shortened.append("[...]");
1383 shortened.append(mName.string() + (mName.length() - 36), 36);
1384 name = shortened.c_str();
1385 } else {
1386 name = mName;
1387 }
1388
1389 result.appendFormat(" %s\n", name.string());
1390
Alec Mourib416efd2018-09-06 21:01:59 +00001391 const State& layerState(getDrawingState());
Lloyd Pique074e8122018-07-26 12:57:23 -07001392 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(displayId);
Chia-I Wu1e043612018-03-01 09:45:09 -08001393 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
1394 result.appendFormat(" rel %6d | ", layerState.z);
1395 } else {
1396 result.appendFormat(" %10d | ", layerState.z);
1397 }
Dominik Laskowski7e045462018-05-30 13:02:02 -07001398 result.appendFormat("%10s | ", to_string(getCompositionType(displayId)).c_str());
Lloyd Pique074e8122018-07-26 12:57:23 -07001399 result.appendFormat("%10s | ", to_string(hwcInfo.transform).c_str());
1400 const Rect& frame = hwcInfo.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001401 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique074e8122018-07-26 12:57:23 -07001402 const FloatRect& crop = hwcInfo.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001403 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 -07001404
David Sodman7e4ae112018-02-09 15:02:28 -08001405 result.append("- - - - - - - - - - - - - - - -\n");
1406
1407 std::string compositionInfoStr;
1408 getBE().compositionInfo.dump(compositionInfoStr, "compositionInfo");
1409 result.append(compositionInfoStr.c_str());
1410
Yichi Chen6ca35192018-05-29 12:20:43 +08001411 result.append("- - - - - - - - - - - - - - - -");
1412 result.append("- - - - - - - - - - - - - - - -");
1413 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001414}
Dan Stozae22aec72016-08-01 13:20:59 -07001415
Svetoslavd85084b2014-03-20 10:28:31 -07001416void Layer::dumpFrameStats(String8& result) const {
1417 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001418}
1419
Svetoslavd85084b2014-03-20 10:28:31 -07001420void Layer::clearFrameStats() {
1421 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001422}
1423
Jamie Gennis6547ff42013-07-16 20:12:42 -07001424void Layer::logFrameStats() {
1425 mFrameTracker.logAndResetStats(mName);
1426}
1427
Svetoslavd85084b2014-03-20 10:28:31 -07001428void Layer::getFrameStats(FrameStats* outStats) const {
1429 mFrameTracker.getStats(outStats);
1430}
1431
Brian Andersond6927fb2016-07-23 23:37:30 -07001432void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001433 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001434 Mutex::Autolock lock(mFrameEventHistoryMutex);
1435 mFrameEventHistory.checkFencesForCompletion();
1436 mFrameEventHistory.dump(result);
1437}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001438
Brian Anderson5ea5e592016-12-01 16:54:33 -08001439void Layer::onDisconnect() {
1440 Mutex::Autolock lock(mFrameEventHistoryMutex);
1441 mFrameEventHistory.onDisconnect();
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001442 mTimeStats.onDisconnect(getName().c_str());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001443}
1444
Brian Anderson3890c392016-07-25 12:48:08 -07001445void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001446 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001447 if (newTimestamps) {
1448 mTimeStats.setPostTime(getName().c_str(), newTimestamps->frameNumber,
1449 newTimestamps->postedTime);
1450 }
1451
Brian Andersond6927fb2016-07-23 23:37:30 -07001452 Mutex::Autolock lock(mFrameEventHistoryMutex);
1453 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001454 // If there are any unsignaled fences in the aquire timeline at this
1455 // point, the previously queued frame hasn't been latched yet. Go ahead
1456 // and try to get the signal time here so the syscall is taken out of
1457 // the main thread's critical path.
1458 mAcquireTimeline.updateSignalTimes();
1459 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001460 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001461 mFrameEventHistory.addQueue(*newTimestamps);
1462 }
1463
Brian Anderson3890c392016-07-25 12:48:08 -07001464 if (outDelta) {
1465 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001466 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001467}
Dan Stozae77c7662016-05-13 11:37:28 -07001468
Chia-I Wu98f1c102017-05-30 14:54:08 -07001469size_t Layer::getChildrenCount() const {
1470 size_t count = 0;
1471 for (const sp<Layer>& child : mCurrentChildren) {
1472 count += 1 + child->getChildrenCount();
1473 }
1474 return count;
1475}
1476
Robert Carr1f0a16a2016-10-24 16:27:39 -07001477void Layer::addChild(const sp<Layer>& layer) {
1478 mCurrentChildren.add(layer);
1479 layer->setParent(this);
1480}
1481
1482ssize_t Layer::removeChild(const sp<Layer>& layer) {
1483 layer->setParent(nullptr);
1484 return mCurrentChildren.remove(layer);
1485}
1486
Robert Carr1db73f62016-12-21 12:58:51 -08001487bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1488 sp<Handle> handle = nullptr;
1489 sp<Layer> newParent = nullptr;
1490 if (newParentHandle == nullptr) {
1491 return false;
1492 }
1493 handle = static_cast<Handle*>(newParentHandle.get());
1494 newParent = handle->owner.promote();
1495 if (newParent == nullptr) {
1496 ALOGE("Unable to promote Layer handle");
1497 return false;
1498 }
1499
1500 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001501 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001502
1503 sp<Client> client(child->mClientRef.promote());
1504 if (client != nullptr) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001505 client->updateParent(newParent);
Robert Carr1db73f62016-12-21 12:58:51 -08001506 }
1507 }
1508 mCurrentChildren.clear();
1509
1510 return true;
1511}
1512
Robert Carr15eae092018-03-23 13:43:53 -07001513void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001514 for (const sp<Layer>& child : mDrawingChildren) {
1515 child->mDrawingParent = newParent;
1516 }
1517}
1518
chaviwf1961f72017-09-18 16:41:07 -07001519bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1520 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001521 return false;
1522 }
1523
1524 auto handle = static_cast<Handle*>(newParentHandle.get());
1525 sp<Layer> newParent = handle->owner.promote();
1526 if (newParent == nullptr) {
1527 ALOGE("Unable to promote Layer handle");
1528 return false;
1529 }
1530
chaviwf1961f72017-09-18 16:41:07 -07001531 sp<Layer> parent = getParent();
1532 if (parent != nullptr) {
1533 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001534 }
chaviwf1961f72017-09-18 16:41:07 -07001535 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001536
chaviwf1961f72017-09-18 16:41:07 -07001537 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001538 sp<Client> newParentClient(newParent->mClientRef.promote());
1539
chaviwf1961f72017-09-18 16:41:07 -07001540 if (client != newParentClient) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001541 client->updateParent(newParent);
chaviw06178942017-07-27 10:25:59 -07001542 }
1543
chaviw06178942017-07-27 10:25:59 -07001544 return true;
1545}
1546
Robert Carr9524cb32017-02-13 11:32:32 -08001547bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001548 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001549 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001550 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001551 if (client != nullptr && parentClient != client) {
Robert Carr7f619b22017-11-06 12:56:35 -08001552 client->detachLayer(child.get());
1553 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001554 }
Robert Carr7f619b22017-11-06 12:56:35 -08001555 }
Robert Carr9524cb32017-02-13 11:32:32 -08001556
1557 return true;
1558}
1559
Peiyong Lind3788632018-09-18 16:01:31 -07001560bool Layer::setColorTransform(const mat4& matrix) {
1561 if (mCurrentState.colorTransform == matrix) {
1562 return false;
1563 }
1564 ++mCurrentState.sequence;
1565 mCurrentState.colorTransform = matrix;
1566 setTransactionFlags(eTransactionNeeded);
1567 return true;
1568}
1569
1570const mat4& Layer::getColorTransform() const {
1571 return getDrawingState().colorTransform;
1572}
1573
1574bool Layer::hasColorTransform() const {
1575 static const mat4 identityMatrix = mat4();
1576 return getDrawingState().colorTransform != identityMatrix;
1577}
1578
Chia-I Wu11481472018-05-04 10:43:19 -07001579bool Layer::isLegacyDataSpace() const {
1580 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001581 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001582 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001583}
1584
Robert Carr1f0a16a2016-10-24 16:27:39 -07001585void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001586 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001587}
1588
1589void Layer::clearSyncPoints() {
1590 for (const auto& child : mCurrentChildren) {
1591 child->clearSyncPoints();
1592 }
1593
1594 Mutex::Autolock lock(mLocalSyncPointMutex);
1595 for (auto& point : mLocalSyncPoints) {
1596 point->setFrameAvailable();
1597 }
1598 mLocalSyncPoints.clear();
1599}
1600
1601int32_t Layer::getZ() const {
1602 return mDrawingState.z;
1603}
1604
Robert Carr29abff82017-12-04 13:51:20 -08001605bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1606 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1607 const State& state = useDrawing ? mDrawingState : mCurrentState;
1608 return state.zOrderRelativeOf != nullptr;
1609}
1610
David Sodman41fdfc92017-11-06 16:09:56 -08001611__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001612 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001613 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1614 "makeTraversalList received invalid stateSet");
1615 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1616 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1617 const State& state = useDrawing ? mDrawingState : mCurrentState;
1618
Robert Carr29abff82017-12-04 13:51:20 -08001619 if (state.zOrderRelatives.size() == 0) {
1620 *outSkipRelativeZUsers = true;
1621 return children;
1622 }
1623
chaviwfd462612018-05-31 16:11:27 -07001624 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001625 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001626 sp<Layer> strongRelative = weakRelative.promote();
1627 if (strongRelative != nullptr) {
1628 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001629 }
1630 }
1631
Dan Stoza412903f2017-04-27 13:42:17 -07001632 for (const sp<Layer>& child : children) {
Robert Carr503c7042017-09-27 15:06:08 -07001633 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1634 if (childState.zOrderRelativeOf != nullptr) {
1635 continue;
1636 }
Robert Carrdb66e622017-04-10 16:55:57 -07001637 traverse.add(child);
1638 }
1639
1640 return traverse;
1641}
1642
Robert Carr1f0a16a2016-10-24 16:27:39 -07001643/**
Robert Carrdb66e622017-04-10 16:55:57 -07001644 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001645 */
Dan Stoza412903f2017-04-27 13:42:17 -07001646void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001647 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1648 // produce a new list for traversing, including our relatives, and not including our children
1649 // who are relatives of another surface. In the case that there are no relative Z,
1650 // makeTraversalList returns our children directly to avoid significant overhead.
1651 // However in this case we need to take the responsibility for filtering children which
1652 // are relatives of another surface here.
1653 bool skipRelativeZUsers = false;
1654 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001655
Robert Carr1f0a16a2016-10-24 16:27:39 -07001656 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001657 for (; i < list.size(); i++) {
1658 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001659 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1660 continue;
1661 }
1662
Robert Carrdb66e622017-04-10 16:55:57 -07001663 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001664 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001665 }
Dan Stoza412903f2017-04-27 13:42:17 -07001666 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001667 }
Robert Carr29abff82017-12-04 13:51:20 -08001668
Dan Stoza412903f2017-04-27 13:42:17 -07001669 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001670 for (; i < list.size(); i++) {
1671 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001672
1673 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1674 continue;
1675 }
Dan Stoza412903f2017-04-27 13:42:17 -07001676 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001677 }
1678}
1679
1680/**
Robert Carrdb66e622017-04-10 16:55:57 -07001681 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001682 */
Dan Stoza412903f2017-04-27 13:42:17 -07001683void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1684 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001685 // See traverseInZOrder for documentation.
1686 bool skipRelativeZUsers = false;
1687 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001688
Robert Carr1f0a16a2016-10-24 16:27:39 -07001689 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001690 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001691 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001692
1693 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1694 continue;
1695 }
1696
Robert Carrdb66e622017-04-10 16:55:57 -07001697 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001698 break;
1699 }
Dan Stoza412903f2017-04-27 13:42:17 -07001700 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001701 }
Dan Stoza412903f2017-04-27 13:42:17 -07001702 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001703 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001704 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001705
1706 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1707 continue;
1708 }
1709
Dan Stoza412903f2017-04-27 13:42:17 -07001710 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001711 }
1712}
1713
chaviw4b129c22018-04-09 16:19:43 -07001714LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1715 const std::vector<Layer*>& layersInTree) {
1716 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1717 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001718 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1719 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
chaviw4b129c22018-04-09 16:19:43 -07001720 const State& state = useDrawing ? mDrawingState : mCurrentState;
1721
chaviwfd462612018-05-31 16:11:27 -07001722 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001723 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1724 sp<Layer> strongRelative = weakRelative.promote();
1725 // Only add relative layers that are also descendents of the top most parent of the tree.
1726 // If a relative layer is not a descendent, then it should be ignored.
1727 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1728 traverse.add(strongRelative);
1729 }
1730 }
1731
1732 for (const sp<Layer>& child : children) {
1733 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1734 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1735 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1736 // the child here since it won't be added later as a relative.
1737 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1738 childState.zOrderRelativeOf.promote().get())) {
1739 continue;
1740 }
1741 traverse.add(child);
1742 }
1743
1744 return traverse;
1745}
1746
1747void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1748 LayerVector::StateSet stateSet,
1749 const LayerVector::Visitor& visitor) {
1750 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001751
1752 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001753 for (; i < list.size(); i++) {
1754 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001755 if (relative->getZ() >= 0) {
1756 break;
1757 }
chaviw4b129c22018-04-09 16:19:43 -07001758 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001759 }
chaviw4b129c22018-04-09 16:19:43 -07001760
chaviwa76b2712017-09-20 12:02:26 -07001761 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001762 for (; i < list.size(); i++) {
1763 const auto& relative = list[i];
1764 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001765 }
1766}
1767
chaviw4b129c22018-04-09 16:19:43 -07001768std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1769 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1770 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1771
1772 std::vector<Layer*> layersInTree = {this};
1773 for (size_t i = 0; i < children.size(); i++) {
1774 const auto& child = children[i];
1775 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1776 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1777 }
1778
1779 return layersInTree;
1780}
1781
1782void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1783 const LayerVector::Visitor& visitor) {
1784 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1785 std::sort(layersInTree.begin(), layersInTree.end());
1786 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1787}
1788
Peiyong Linefefaac2018-08-17 12:27:51 -07001789ui::Transform Layer::getTransform() const {
1790 ui::Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001791 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001792 if (p != nullptr) {
1793 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001794
1795 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1796 // it isFixedSize) then there may be additional scaling not accounted
1797 // for in the transform. We need to mirror this scaling in child surfaces
1798 // or we will break the contract where WM can treat child surfaces as
1799 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08001800 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001801 int bufferWidth;
1802 int bufferHeight;
1803 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08001804 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
1805 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001806 } else {
David Sodman0cc69182017-11-17 12:12:07 -08001807 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
1808 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001809 }
Marissa Wall61c58622018-07-18 10:12:20 -07001810 float sx = p->getActiveWidth(p->getDrawingState()) / static_cast<float>(bufferWidth);
1811 float sy = p->getActiveHeight(p->getDrawingState()) / static_cast<float>(bufferHeight);
Peiyong Linefefaac2018-08-17 12:27:51 -07001812 ui::Transform extraParentScaling;
Robert Carr9b429f42017-04-17 14:56:57 -07001813 extraParentScaling.set(sx, 0, 0, sy);
1814 t = t * extraParentScaling;
1815 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001816 }
Marissa Wall61c58622018-07-18 10:12:20 -07001817 return t * getActiveTransform(getDrawingState());
Robert Carr1f0a16a2016-10-24 16:27:39 -07001818}
1819
chaviw13fdc492017-06-27 12:40:18 -07001820half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001821 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001822
chaviw13fdc492017-06-27 12:40:18 -07001823 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1824 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001825}
Robert Carr6452f122017-03-21 10:41:29 -07001826
chaviw13fdc492017-06-27 12:40:18 -07001827half4 Layer::getColor() const {
1828 const half4 color(getDrawingState().color);
1829 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001830}
Robert Carr6452f122017-03-21 10:41:29 -07001831
Robert Carr1f0a16a2016-10-24 16:27:39 -07001832void Layer::commitChildList() {
1833 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1834 const auto& child = mCurrentChildren[i];
1835 child->commitChildList();
1836 }
1837 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001838 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001839}
1840
chaviw1d044282017-09-27 12:19:28 -07001841void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1842 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1843 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1844 const State& state = useDrawing ? mDrawingState : mCurrentState;
1845
Peiyong Linefefaac2018-08-17 12:27:51 -07001846 ui::Transform requestedTransform = state.active_legacy.transform;
1847 ui::Transform transform = getTransform();
chaviw1d044282017-09-27 12:19:28 -07001848
1849 layerInfo->set_id(sequence);
1850 layerInfo->set_name(getName().c_str());
1851 layerInfo->set_type(String8(getTypeId()));
1852
1853 for (const auto& child : children) {
1854 layerInfo->add_children(child->sequence);
1855 }
1856
1857 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1858 sp<Layer> strongRelative = weakRelative.promote();
1859 if (strongRelative != nullptr) {
1860 layerInfo->add_relatives(strongRelative->sequence);
1861 }
1862 }
1863
Marissa Wallf58c14b2018-07-24 10:50:43 -07001864 LayerProtoHelper::writeToProto(state.activeTransparentRegion_legacy,
chaviw1d044282017-09-27 12:19:28 -07001865 layerInfo->mutable_transparent_region());
1866 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
1867 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
1868
1869 layerInfo->set_layer_stack(getLayerStack());
1870 layerInfo->set_z(state.z);
1871
1872 PositionProto* position = layerInfo->mutable_position();
1873 position->set_x(transform.tx());
1874 position->set_y(transform.ty());
1875
1876 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
1877 requestedPosition->set_x(requestedTransform.tx());
1878 requestedPosition->set_y(requestedTransform.ty());
1879
1880 SizeProto* size = layerInfo->mutable_size();
Marissa Wallf58c14b2018-07-24 10:50:43 -07001881 size->set_w(state.active_legacy.w);
1882 size->set_h(state.active_legacy.h);
chaviw1d044282017-09-27 12:19:28 -07001883
Marissa Wallf58c14b2018-07-24 10:50:43 -07001884 LayerProtoHelper::writeToProto(state.crop_legacy, layerInfo->mutable_crop());
chaviw1d044282017-09-27 12:19:28 -07001885
1886 layerInfo->set_is_opaque(isOpaque(state));
1887 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07001888
1889 // XXX (b/79210409) mCurrentDataSpace is not protected
1890 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1891
chaviw1d044282017-09-27 12:19:28 -07001892 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1893 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
1894 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
1895 layerInfo->set_flags(state.flags);
1896
1897 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1898 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
1899
Jorim Jaggi8e0af362017-11-14 16:28:28 +01001900 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07001901 if (parent != nullptr) {
1902 layerInfo->set_parent(parent->sequence);
1903 }
1904
1905 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1906 if (zOrderRelativeOf != nullptr) {
1907 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1908 }
1909
Chia-I Wu01591c92018-05-22 12:03:00 -07001910 // XXX getBE().compositionInfo.mBuffer is not protected
David Sodman0cc69182017-11-17 12:12:07 -08001911 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001912 if (buffer != nullptr) {
1913 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
Peiyong Linefefaac2018-08-17 12:27:51 -07001914 LayerProtoHelper::writeToProto(ui::Transform(mCurrentTransform),
Yichi Chen6ca35192018-05-29 12:20:43 +08001915 layerInfo->mutable_buffer_transform());
chaviw1d044282017-09-27 12:19:28 -07001916 }
1917
1918 layerInfo->set_queued_frames(getQueuedFrameCount());
1919 layerInfo->set_refresh_pending(isBufferLatched());
rongliucfb187b2018-03-14 12:26:23 -07001920 layerInfo->set_window_type(state.type);
1921 layerInfo->set_app_id(state.appId);
chaviwadc40c22018-07-10 16:57:27 -07001922 layerInfo->set_curr_frame(mCurrentFrameNumber);
1923
1924 for (const auto& pendingState : mPendingStates) {
Marissa Wallf58c14b2018-07-24 10:50:43 -07001925 auto barrierLayer = pendingState.barrierLayer_legacy.promote();
chaviwadc40c22018-07-10 16:57:27 -07001926 if (barrierLayer != nullptr) {
1927 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1928 barrierLayerProto->set_id(barrierLayer->sequence);
Marissa Wallf58c14b2018-07-24 10:50:43 -07001929 barrierLayerProto->set_frame_number(pendingState.frameNumber_legacy);
chaviwadc40c22018-07-10 16:57:27 -07001930 }
1931 }
chaviw1d044282017-09-27 12:19:28 -07001932}
1933
Dominik Laskowski7e045462018-05-30 13:02:02 -07001934void Layer::writeToProto(LayerProto* layerInfo, int32_t displayId) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07001935 if (!hasHwcLayer(displayId)) {
1936 return;
1937 }
1938
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001939 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
1940
Dominik Laskowski7e045462018-05-30 13:02:02 -07001941 const auto& hwcInfo = getBE().mHwcLayers.at(displayId);
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001942
1943 const Rect& frame = hwcInfo.displayFrame;
1944 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
1945
1946 const FloatRect& crop = hwcInfo.sourceCrop;
1947 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
1948
1949 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
1950 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08001951
1952 const int32_t compositionType = static_cast<int32_t>(hwcInfo.compositionType);
1953 layerInfo->set_hwc_composition_type(compositionType);
1954
1955 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
1956 static_cast<BufferLayer*>(this)->isProtected()) {
1957 layerInfo->set_is_protected(true);
1958 } else {
1959 layerInfo->set_is_protected(false);
1960 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001961}
1962
Mathias Agopian13127d82013-03-05 17:47:11 -08001963// ---------------------------------------------------------------------------
1964
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001965}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07001966
1967#if defined(__gl_h_)
1968#error "don't include gl/gl.h in this file"
1969#endif
1970
1971#if defined(__gl2_h_)
1972#error "don't include gl2/gl2.h in this file"
1973#endif