blob: 60feb0de73d53a095409422285ea50a255c629f6 [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"
David Sodman41fdfc92017-11-06 16:09:56 -080053#include "clz.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054
Mathias Agopian1b031492012-06-20 17:51:20 -070055#include "DisplayHardware/HWComposer.h"
56
Mathias Agopian875d8e12013-06-07 15:35:48 -070057#include "RenderEngine/RenderEngine.h"
58
Dan Stozac5da2712016-07-20 15:38:12 -070059#include <mutex>
chaviw1d044282017-09-27 12:19:28 -070060#include "LayerProtoHelper.h"
Dan Stozac5da2712016-07-20 15:38:12 -070061
David Sodman41fdfc92017-11-06 16:09:56 -080062#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064namespace android {
65
Mathias Agopian13127d82013-03-05 17:47:11 -080066int32_t Layer::sSequence = 1;
67
David Sodman41fdfc92017-11-06 16:09:56 -080068Layer::Layer(SurfaceFlinger* flinger, const sp<Client>& client, const String8& name, uint32_t w,
69 uint32_t h, uint32_t flags)
David Sodman0c69cad2017-08-21 12:12:51 -070070 : contentDirty(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080071 sequence(uint32_t(android_atomic_inc(&sSequence))),
72 mFlinger(flinger),
Mathias Agopian13127d82013-03-05 17:47:11 -080073 mPremultipliedAlpha(true),
David Sodman0c69cad2017-08-21 12:12:51 -070074 mName(name),
Mathias Agopian13127d82013-03-05 17:47:11 -080075 mTransactionFlags(0),
Dan Stoza7dde5992015-05-22 09:51:44 -070076 mPendingStateMutex(),
77 mPendingStates(),
Mathias Agopiana67932f2011-04-20 14:20:59 -070078 mQueuedFrames(0),
Jesse Hall399184a2014-03-03 15:42:54 -080079 mSidebandStreamChanged(false),
Mathias Agopiana9347642017-02-13 16:42:28 -080080 mActiveBufferSlot(BufferQueue::INVALID_BUFFER_SLOT),
Mathias Agopiana67932f2011-04-20 14:20:59 -070081 mCurrentTransform(0),
Robert Carrc3574f72016-03-24 12:19:32 -070082 mOverrideScalingMode(-1),
Mathias Agopiana67932f2011-04-20 14:20:59 -070083 mCurrentOpacity(true),
Dan Stozacac35382016-01-27 12:21:06 -080084 mCurrentFrameNumber(0),
Mathias Agopian82d7ab62012-01-19 18:34:40 -080085 mFrameLatencyNeeded(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080086 mFiltering(false),
87 mNeedsFiltering(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080088 mProtectedByApp(false),
Riley Andrews03414a12014-07-01 14:22:59 -070089 mClientRef(client),
Dan Stozaa4650a52015-05-12 12:56:16 -070090 mPotentialCursor(false),
91 mQueueItemLock(),
92 mQueueItemCondition(),
93 mQueueItems(),
Dan Stoza65476f32015-05-14 09:27:25 -070094 mLastFrameNumberReceived(0),
Robert Carr82364e32016-05-15 11:27:47 -070095 mAutoRefresh(false),
David Sodmanb8af7922017-12-21 15:17:55 -080096 mFreezeGeometryUpdates(false),
David Sodman2b727ac2017-12-21 14:28:08 -080097 mBE{this, name.string()} {
Dan Stoza9e56aa02015-11-02 13:00:03 -080098
Mathias Agopiana67932f2011-04-20 14:20:59 -070099 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700100
101 uint32_t layerFlags = 0;
David Sodman41fdfc92017-11-06 16:09:56 -0800102 if (flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
103 if (flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
104 if (flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700105
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700106 mName = name;
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700107 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700108
109 mCurrentState.active.w = w;
110 mCurrentState.active.h = h;
David Sodman0c69cad2017-08-21 12:12:51 -0700111 mCurrentState.flags = layerFlags;
Robert Carr3dcabfa2016-03-01 18:36:58 -0800112 mCurrentState.active.transform.set(0, 0);
Robert Carrb5d3d262016-03-25 15:08:13 -0700113 mCurrentState.crop.makeInvalid();
114 mCurrentState.finalCrop.makeInvalid();
Robert Carr7bf247e2017-05-18 14:02:49 -0700115 mCurrentState.requestedFinalCrop = mCurrentState.finalCrop;
116 mCurrentState.requestedCrop = mCurrentState.crop;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700117 mCurrentState.z = 0;
chaviw13fdc492017-06-27 12:40:18 -0700118 mCurrentState.color.a = 1.0f;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700119 mCurrentState.layerStack = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700120 mCurrentState.sequence = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700121 mCurrentState.requested = mCurrentState.active;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500122 mCurrentState.appId = 0;
123 mCurrentState.type = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700124
125 // drawing state & current state are identical
126 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700127
Dan Stoza9e56aa02015-11-02 13:00:03 -0800128 const auto& hwc = flinger->getHwComposer();
129 const auto& activeConfig = hwc.getActiveConfig(HWC_DISPLAY_PRIMARY);
130 nsecs_t displayPeriod = activeConfig->getVsyncPeriod();
Jamie Gennis6547ff42013-07-16 20:12:42 -0700131 mFrameTracker.setDisplayRefreshPeriod(displayPeriod);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800132
133 CompositorTiming compositorTiming;
134 flinger->getCompositorTiming(&compositorTiming);
135 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
David Sodman9eeae692017-11-02 10:53:32 -0700136
Jamie Gennise8696a42012-01-15 18:54:57 -0800137}
138
David Sodman41fdfc92017-11-06 16:09:56 -0800139void Layer::onFirstRef() {}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700140
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700141Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800142 sp<Client> c(mClientRef.promote());
143 if (c != 0) {
144 c->detachLayer(this);
145 }
146
147 for (auto& point : mRemoteSyncPoints) {
148 point->setTransactionApplied();
149 }
150 for (auto& point : mLocalSyncPoints) {
151 point->setFrameAvailable();
152 }
Jamie Gennis6547ff42013-07-16 20:12:42 -0700153 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700154}
155
Mathias Agopian13127d82013-03-05 17:47:11 -0800156// ---------------------------------------------------------------------------
157// callbacks
158// ---------------------------------------------------------------------------
159
David Sodmaneb085e02017-10-05 18:49:04 -0700160/*
161 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
162 * Layer. So, the implementation is done in BufferLayer. When called on a
163 * ColorLayer object, it's essentially a NOP.
164 */
David Sodmaneb085e02017-10-05 18:49:04 -0700165void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800166
Chia-I Wuc6657022017-08-15 11:18:17 -0700167void Layer::onRemovedFromCurrentState() {
168 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
169
chaviw8b3871a2017-11-01 17:41:01 -0700170 mPendingRemoval = true;
171
Robert Carr5edb1ad2017-04-25 10:54:24 -0700172 if (mCurrentState.zOrderRelativeOf != nullptr) {
173 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
174 if (strongRelative != nullptr) {
175 strongRelative->removeZOrderRelative(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700176 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700177 }
178 mCurrentState.zOrderRelativeOf = nullptr;
179 }
180
Chia-I Wuc6657022017-08-15 11:18:17 -0700181 for (const auto& child : mCurrentChildren) {
182 child->onRemovedFromCurrentState();
183 }
184}
Chia-I Wu38512252017-05-17 14:36:16 -0700185
Chia-I Wuc6657022017-08-15 11:18:17 -0700186void Layer::onRemoved() {
187 // the layer is removed from SF mLayersPendingRemoval
David Sodmaneb085e02017-10-05 18:49:04 -0700188 abandon();
Chia-I Wuc6657022017-08-15 11:18:17 -0700189
Steven Thomasb02664d2017-07-26 18:48:28 -0700190 destroyAllHwcLayers();
Chia-I Wu38512252017-05-17 14:36:16 -0700191
Robert Carr1f0a16a2016-10-24 16:27:39 -0700192 for (const auto& child : mCurrentChildren) {
193 child->onRemoved();
194 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700195}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700196
Mathias Agopian13127d82013-03-05 17:47:11 -0800197// ---------------------------------------------------------------------------
198// set-up
199// ---------------------------------------------------------------------------
200
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700201const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800202 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800203}
204
chaviw13fdc492017-06-27 12:40:18 -0700205bool Layer::getPremultipledAlpha() const {
206 return mPremultipliedAlpha;
207}
208
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700209sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800210 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700211 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800212}
213
214// ---------------------------------------------------------------------------
215// h/w composer set-up
216// ---------------------------------------------------------------------------
217
Steven Thomasb02664d2017-07-26 18:48:28 -0700218bool Layer::createHwcLayer(HWComposer* hwc, int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700219 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(hwcId) != 0,
David Sodman9eeae692017-11-02 10:53:32 -0700220 "Already have a layer for hwcId %d", hwcId);
David Sodmanf6a38932018-05-25 15:27:50 -0700221 HWC2::Layer* layer = hwc->createLayer(hwcId);
Steven Thomasb02664d2017-07-26 18:48:28 -0700222 if (!layer) {
223 return false;
224 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700225 LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers[hwcId];
Steven Thomasb02664d2017-07-26 18:48:28 -0700226 hwcInfo.hwc = hwc;
227 hwcInfo.layer = layer;
David Sodmanf6a38932018-05-25 15:27:50 -0700228 layer->setLayerDestroyedListener(
229 [this, hwcId](HWC2::Layer* /*layer*/) { getBE().mHwcLayers.erase(hwcId); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700230 return true;
231}
232
Chia-I Wu83806892017-11-16 10:50:20 -0800233bool Layer::destroyHwcLayer(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700234 if (getBE().mHwcLayers.count(hwcId) == 0) {
Chia-I Wu83806892017-11-16 10:50:20 -0800235 return false;
Steven Thomasb02664d2017-07-26 18:48:28 -0700236 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700237 auto& hwcInfo = getBE().mHwcLayers[hwcId];
David Sodman41fdfc92017-11-06 16:09:56 -0800238 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr, "Attempt to destroy null layer");
Steven Thomasb02664d2017-07-26 18:48:28 -0700239 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
David Sodmanf6a38932018-05-25 15:27:50 -0700240 hwcInfo.hwc->destroyLayer(hwcId, hwcInfo.layer);
241 // The layer destroyed listener should have cleared the entry from
242 // mHwcLayers. Verify that.
243 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(hwcId) != 0,
244 "Stale layer entry in getBE().mHwcLayers");
Chia-I Wu83806892017-11-16 10:50:20 -0800245 return true;
Steven Thomasb02664d2017-07-26 18:48:28 -0700246}
247
248void Layer::destroyAllHwcLayers() {
David Sodman6f65f3e2017-11-03 14:28:09 -0700249 size_t numLayers = getBE().mHwcLayers.size();
Steven Thomasb02664d2017-07-26 18:48:28 -0700250 for (size_t i = 0; i < numLayers; ++i) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700251 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.empty(), "destroyAllHwcLayers failed");
252 destroyHwcLayer(getBE().mHwcLayers.begin()->first);
Steven Thomasb02664d2017-07-26 18:48:28 -0700253 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700254 LOG_ALWAYS_FATAL_IF(!getBE().mHwcLayers.empty(),
David Sodman41fdfc92017-11-06 16:09:56 -0800255 "All hardware composer layers should have been destroyed");
Steven Thomasb02664d2017-07-26 18:48:28 -0700256}
Steven Thomasb02664d2017-07-26 18:48:28 -0700257
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800258Rect Layer::getContentCrop() const {
259 // this is the crop rectangle that applies to the buffer
260 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700261 Rect crop;
262 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800263 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700264 crop = mCurrentCrop;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800265 } else if (getBE().compositionInfo.mBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800266 // otherwise we use the whole buffer
David Sodman0cc69182017-11-17 12:12:07 -0800267 crop = getBE().compositionInfo.mBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700268 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800269 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700270 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700271 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700272 return crop;
273}
274
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700275static Rect reduce(const Rect& win, const Region& exclude) {
276 if (CC_LIKELY(exclude.isEmpty())) {
277 return win;
278 }
279 if (exclude.isRect()) {
280 return win.reduce(exclude.getBounds());
281 }
282 return Region(win).subtract(exclude).getBounds();
283}
284
Dan Stoza80d61162017-12-20 15:57:52 -0800285static FloatRect reduce(const FloatRect& win, const Region& exclude) {
286 if (CC_LIKELY(exclude.isEmpty())) {
287 return win;
288 }
289 // Convert through Rect (by rounding) for lack of FloatRegion
290 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
291}
292
Robert Carr1f0a16a2016-10-24 16:27:39 -0700293Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
294 const Layer::State& s(getDrawingState());
295 Rect win(s.active.w, s.active.h);
296
297 if (!s.crop.isEmpty()) {
298 win.intersect(s.crop, &win);
299 }
300
301 Transform t = getTransform();
302 win = t.transform(win);
303
Robert Carr41b08b52017-06-01 16:11:34 -0700304 if (!s.finalCrop.isEmpty()) {
305 win.intersect(s.finalCrop, &win);
306 }
307
Chia-I Wue41dbe62017-06-13 14:10:56 -0700308 const sp<Layer>& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700309 // Now we need to calculate the parent bounds, so we can clip ourselves to those.
310 // When calculating the parent bounds for purposes of clipping,
311 // we don't need to constrain the parent to its transparent region.
312 // The transparent region is an optimization based on the
313 // buffer contents of the layer, but does not affect the space allocated to
314 // it by policy, and thus children should be allowed to extend into the
315 // parent's transparent region. In fact one of the main uses, is to reduce
316 // buffer allocation size in cases where a child window sits behind a main window
317 // (by marking the hole in the parent window as a transparent region)
318 if (p != nullptr) {
319 Rect bounds = p->computeScreenBounds(false);
320 bounds.intersect(win, &win);
321 }
322
323 if (reduceTransparentRegion) {
324 auto const screenTransparentRegion = t.transform(s.activeTransparentRegion);
325 win = reduce(win, screenTransparentRegion);
326 }
327
328 return win;
329}
330
Dan Stoza80d61162017-12-20 15:57:52 -0800331FloatRect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700332 const Layer::State& s(getDrawingState());
Michael Lentine6c925ed2014-09-26 17:55:01 -0700333 return computeBounds(s.activeTransparentRegion);
334}
335
Dan Stoza80d61162017-12-20 15:57:52 -0800336FloatRect Layer::computeBounds(const Region& activeTransparentRegion) const {
Michael Lentine6c925ed2014-09-26 17:55:01 -0700337 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800338 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700339
340 if (!s.crop.isEmpty()) {
341 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800342 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700343
344 Rect bounds = win;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700345 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700346 if (p != nullptr) {
Robert Carrde9ec442017-02-08 17:43:36 -0800347 // Look in computeScreenBounds recursive call for explanation of
348 // why we pass false here.
349 bounds = p->computeScreenBounds(false /* reduceTransparentRegion */);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700350 }
351
352 Transform t = getTransform();
Dan Stoza80d61162017-12-20 15:57:52 -0800353
354 FloatRect floatWin = win.toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700355 if (p != nullptr) {
Dan Stoza80d61162017-12-20 15:57:52 -0800356 floatWin = t.transform(floatWin);
357 floatWin = floatWin.intersect(bounds.toFloatRect());
358 floatWin = t.inverse().transform(floatWin);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700359 }
360
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700361 // subtract the transparent region and snap to the bounds
Dan Stoza80d61162017-12-20 15:57:52 -0800362 return reduce(floatWin, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800363}
364
Robert Carr1f0a16a2016-10-24 16:27:39 -0700365Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& hw) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700366 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800367 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700368 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800369
370 // apply the projection's clipping to the window crop in
371 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700372 // if there are no window scaling involved, this operation will map to full
373 // pixels in the buffer.
374 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
375 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700376
377 Rect activeCrop(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700378 if (!s.crop.isEmpty()) {
Chia-I Wudf7867f2017-07-20 14:24:37 -0700379 activeCrop.intersect(s.crop, &activeCrop);
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700380 }
381
Robert Carr1f0a16a2016-10-24 16:27:39 -0700382 Transform t = getTransform();
383 activeCrop = t.transform(activeCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000384 if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
385 activeCrop.clear();
386 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700387 if (!s.finalCrop.isEmpty()) {
David Sodman41fdfc92017-11-06 16:09:56 -0800388 if (!activeCrop.intersect(s.finalCrop, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000389 activeCrop.clear();
390 }
391 }
chaviwb1154d12017-10-31 14:15:36 -0700392
393 const auto& p = mDrawingParent.promote();
394 if (p != nullptr) {
395 auto parentCrop = p->computeInitialCrop(hw);
396 activeCrop.intersect(parentCrop, &activeCrop);
397 }
398
Robert Carr1f0a16a2016-10-24 16:27:39 -0700399 return activeCrop;
400}
401
Dan Stoza5a423ea2017-02-16 14:10:39 -0800402FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700403 // the content crop is the area of the content that gets scaled to the
404 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800405 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700406
407 // In addition there is a WM-specified crop we pull from our drawing state.
408 const State& s(getDrawingState());
409
410 // Screen space to make reduction to parent crop clearer.
411 Rect activeCrop = computeInitialCrop(hw);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700412 Transform t = getTransform();
413 // Back to layer space to work with the content crop.
414 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800415
Michael Lentine28ea2172014-11-19 18:32:37 -0800416 // This needs to be here as transform.transform(Rect) computes the
417 // transformed rect and then takes the bounding box of the result before
418 // returning. This means
419 // transform.inverse().transform(transform.transform(Rect)) != Rect
420 // in which case we need to make sure the final rect is clipped to the
421 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000422 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
423 activeCrop.clear();
424 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800425
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700426 // subtract the transparent region and snap to the bounds
427 activeCrop = reduce(activeCrop, s.activeTransparentRegion);
428
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000429 // Transform the window crop to match the buffer coordinate system,
430 // which means using the inverse of the current transform set on the
431 // SurfaceFlingerConsumer.
432 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700433 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000434 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700435 * the code below applies the primary display's inverse transform to the
436 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000437 */
David Sodman41fdfc92017-11-06 16:09:56 -0800438 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000439 // calculate the inverse transform
440 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800441 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800442 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000443 // and apply to the current transform
David Sodman41fdfc92017-11-06 16:09:56 -0800444 invTransform = (Transform(invTransformOrient) * Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800445 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000446
447 int winWidth = s.active.w;
448 int winHeight = s.active.h;
449 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
450 // If the activeCrop has been rotate the ends are rotated but not
451 // the space itself so when transforming ends back we can't rely on
452 // a modification of the axes of rotation. To account for this we
453 // need to reorient the inverse rotation in terms of the current
454 // axes of rotation.
455 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
456 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
457 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800458 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000459 }
460 winWidth = s.active.h;
461 winHeight = s.active.w;
462 }
David Sodman41fdfc92017-11-06 16:09:56 -0800463 const Rect winCrop = activeCrop.transform(invTransform, s.active.w, s.active.h);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000464
465 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800466 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000467 float yScale = crop.getHeight() / float(winHeight);
468
David Sodman41fdfc92017-11-06 16:09:56 -0800469 float insetL = winCrop.left * xScale;
470 float insetT = winCrop.top * yScale;
471 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000472 float insetB = (winHeight - winCrop.bottom) * yScale;
473
David Sodman41fdfc92017-11-06 16:09:56 -0800474 crop.left += insetL;
475 crop.top += insetT;
476 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000477 crop.bottom -= insetB;
478
Mathias Agopian13127d82013-03-05 17:47:11 -0800479 return crop;
480}
481
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700482void Layer::setGeometry(const sp<const DisplayDevice>& display, uint32_t z) {
483 const auto hwcId = display->getHwcDisplayId();
David Sodman6f65f3e2017-11-03 14:28:09 -0700484 auto& hwcInfo = getBE().mHwcLayers[hwcId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700485
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700486 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800487 hwcInfo.forceClientComposition = false;
488
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700489 if (isSecure() && !display->isSecure()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800490 hwcInfo.forceClientComposition = true;
491 }
492
Chia-I Wu30505fb2018-03-26 16:20:31 -0700493 auto& hwcLayer = hwcInfo.layer;
494
Mathias Agopian13127d82013-03-05 17:47:11 -0800495 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700496 const State& s(getDrawingState());
David Revemanecf0fa52017-03-03 11:32:44 -0500497 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700498 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800499 blendMode =
500 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800501 }
David Sodmanf6a38932018-05-25 15:27:50 -0700502 auto error = hwcLayer->setBlendMode(blendMode);
Chia-I Wu30505fb2018-03-26 16:20:31 -0700503 ALOGE_IF(error != HWC2::Error::None,
504 "[%s] Failed to set blend mode %s:"
505 " %s (%d)",
506 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
507 static_cast<int32_t>(error));
Mathias Agopian13127d82013-03-05 17:47:11 -0800508
509 // apply the layer's transform, followed by the display's global transform
510 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700511 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700512 Transform t = getTransform();
Robert Carrb5d3d262016-03-25 15:08:13 -0700513 if (!s.crop.isEmpty()) {
514 Rect activeCrop(s.crop);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700515 activeCrop = t.transform(activeCrop);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700516 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000517 activeCrop.clear();
518 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700519 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800520 // This needs to be here as transform.transform(Rect) computes the
521 // transformed rect and then takes the bounding box of the result before
522 // returning. This means
523 // transform.inverse().transform(transform.transform(Rect)) != Rect
524 // in which case we need to make sure the final rect is clipped to the
525 // display bounds.
David Sodman41fdfc92017-11-06 16:09:56 -0800526 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000527 activeCrop.clear();
528 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700529 // mark regions outside the crop as transparent
530 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
David Sodman41fdfc92017-11-06 16:09:56 -0800531 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom, s.active.w, s.active.h));
532 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
533 activeTransparentRegion.orSelf(
534 Rect(activeCrop.right, activeCrop.top, s.active.w, activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700535 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700536
Dan Stoza80d61162017-12-20 15:57:52 -0800537 // computeBounds returns a FloatRect to provide more accuracy during the
538 // transformation. We then round upon constructing 'frame'.
539 Rect frame{t.transform(computeBounds(activeTransparentRegion))};
Robert Carrb5d3d262016-03-25 15:08:13 -0700540 if (!s.finalCrop.isEmpty()) {
David Sodman41fdfc92017-11-06 16:09:56 -0800541 if (!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000542 frame.clear();
543 }
544 }
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700545 if (!frame.intersect(display->getViewport(), &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000546 frame.clear();
547 }
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700548 const Transform& tr = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800549 Rect transformedFrame = tr.transform(frame);
David Sodmanf6a38932018-05-25 15:27:50 -0700550 error = hwcLayer->setDisplayFrame(transformedFrame);
Chia-I Wu30505fb2018-03-26 16:20:31 -0700551 if (error != HWC2::Error::None) {
552 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
553 transformedFrame.left, transformedFrame.top, transformedFrame.right,
554 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
555 } else {
556 hwcInfo.displayFrame = transformedFrame;
557 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800558
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700559 FloatRect sourceCrop = computeCrop(display);
David Sodmanf6a38932018-05-25 15:27:50 -0700560 error = hwcLayer->setSourceCrop(sourceCrop);
Chia-I Wu30505fb2018-03-26 16:20:31 -0700561 if (error != HWC2::Error::None) {
562 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
563 "%s (%d)",
564 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
565 to_string(error).c_str(), static_cast<int32_t>(error));
566 } else {
567 hwcInfo.sourceCrop = sourceCrop;
568 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800569
chaviw13fdc492017-06-27 12:40:18 -0700570 float alpha = static_cast<float>(getAlpha());
David Sodmanf6a38932018-05-25 15:27:50 -0700571 error = hwcLayer->setPlaneAlpha(alpha);
Chia-I Wu30505fb2018-03-26 16:20:31 -0700572 ALOGE_IF(error != HWC2::Error::None,
573 "[%s] Failed to set plane alpha %.3f: "
574 "%s (%d)",
575 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800576
David Sodmanf6a38932018-05-25 15:27:50 -0700577 error = hwcLayer->setZOrder(z);
Chia-I Wu30505fb2018-03-26 16:20:31 -0700578 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
579 to_string(error).c_str(), static_cast<int32_t>(error));
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500580
Albert Chaulk2a589632017-05-04 16:59:44 -0400581 int type = s.type;
582 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700583 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400584 if (parent.get()) {
585 auto& parentState = parent->getDrawingState();
rongliucfb187b2018-03-14 12:26:23 -0700586 if (parentState.type >= 0 || parentState.appId >= 0) {
587 type = parentState.type;
588 appId = parentState.appId;
589 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400590 }
591
David Sodmanf6a38932018-05-25 15:27:50 -0700592 error = hwcLayer->setInfo(type, appId);
Chia-I Wu30505fb2018-03-26 16:20:31 -0700593 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
594 static_cast<int32_t>(error));
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800595
Mathias Agopian29a367b2011-07-12 14:51:45 -0700596 /*
597 * Transformations are applied in this order:
598 * 1) buffer orientation/flip/mirror
599 * 2) state transformation (window manager)
600 * 3) layer orientation (screen orientation)
601 * (NOTE: the matrices are multiplied in reverse order)
602 */
603
604 const Transform bufferOrientation(mCurrentTransform);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700605 Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700606
Robert Carrcae605c2017-03-29 12:10:31 -0700607 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700608 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700609 * the code below applies the primary display's inverse transform to the
610 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700611 */
David Sodman41fdfc92017-11-06 16:09:56 -0800612 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700613 // calculate the inverse transform
614 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800615 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700616 }
Robert Carrcae605c2017-03-29 12:10:31 -0700617
618 /*
619 * Here we cancel out the orientation component of the WM transform.
620 * The scaling and translate components are already included in our bounds
621 * computation so it's enough to just omit it in the composition.
622 * See comment in onDraw with ref to b/36727915 for why.
623 */
624 transform = Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700625 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700626
627 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800628 const uint32_t orientation = transform.getOrientation();
Jorim Jaggif3bd94a2018-03-27 15:38:03 +0200629 if (orientation & Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800630 // we can only handle simple transformation
Chia-I Wu30505fb2018-03-26 16:20:31 -0700631 hwcInfo.forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800632 } else {
633 auto transform = static_cast<HWC2::Transform>(orientation);
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800634 hwcInfo.transform = transform;
David Sodmanf6a38932018-05-25 15:27:50 -0700635 auto error = hwcLayer->setTransform(transform);
Chia-I Wu30505fb2018-03-26 16:20:31 -0700636 ALOGE_IF(error != HWC2::Error::None,
637 "[%s] Failed to set transform %s: "
638 "%s (%d)",
639 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
640 static_cast<int32_t>(error));
David Sodman4b7c4bc2017-11-17 12:13:59 -0800641 }
642}
643
Dan Stoza9e56aa02015-11-02 13:00:03 -0800644void Layer::forceClientComposition(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700645 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800646 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
647 return;
648 }
649
David Sodman6f65f3e2017-11-03 14:28:09 -0700650 getBE().mHwcLayers[hwcId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800651}
Dan Stozaee44edd2015-03-23 15:50:23 -0700652
chaviwc9232ed2017-11-14 15:31:15 -0800653bool Layer::getForceClientComposition(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700654 if (getBE().mHwcLayers.count(hwcId) == 0) {
chaviwc9232ed2017-11-14 15:31:15 -0800655 ALOGE("getForceClientComposition: no HWC layer found (%d)", hwcId);
656 return false;
657 }
658
David Sodman6f65f3e2017-11-03 14:28:09 -0700659 return getBE().mHwcLayers[hwcId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800660}
661
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700662void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
663 auto hwcId = display->getHwcDisplayId();
David Sodman6f65f3e2017-11-03 14:28:09 -0700664 if (getBE().mHwcLayers.count(hwcId) == 0 ||
David Sodman9eeae692017-11-02 10:53:32 -0700665 getCompositionType(hwcId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800666 return;
667 }
668
669 // This gives us only the "orientation" component of the transform
670 const State& s(getCurrentState());
671
672 // Apply the layer's transform, followed by the display's global transform
673 // Here we're guaranteed that the layer's transform preserves rects
674 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700675 if (!s.crop.isEmpty()) {
676 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800677 }
678 // Subtract the transparent region and snap to the bounds
679 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700680 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700681 frame.intersect(display->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700682 if (!s.finalCrop.isEmpty()) {
683 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000684 }
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700685 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800686 auto position = displayTransform.transform(frame);
687
David Sodmanf6a38932018-05-25 15:27:50 -0700688 auto error = getBE().mHwcLayers[hwcId].layer->setCursorPosition(position.left,
David Sodman9eeae692017-11-02 10:53:32 -0700689 position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800690 ALOGE_IF(error != HWC2::Error::None,
691 "[%s] Failed to set cursor position "
692 "to (%d, %d): %s (%d)",
693 mName.string(), position.left, position.top, to_string(error).c_str(),
694 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800695}
Riley Andrews03414a12014-07-01 14:22:59 -0700696
Mathias Agopian13127d82013-03-05 17:47:11 -0800697// ---------------------------------------------------------------------------
698// drawing...
699// ---------------------------------------------------------------------------
700
chaviwa76b2712017-09-20 12:02:26 -0700701void Layer::draw(const RenderArea& renderArea, const Region& clip) const {
702 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800703}
704
chaviwa76b2712017-09-20 12:02:26 -0700705void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) const {
706 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800707}
708
chaviwa76b2712017-09-20 12:02:26 -0700709void Layer::draw(const RenderArea& renderArea) const {
710 onDraw(renderArea, Region(renderArea.getBounds()), false);
Dan Stozac7014012014-02-14 15:03:43 -0800711}
712
David Sodman41fdfc92017-11-06 16:09:56 -0800713void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
714 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800715 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700716 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700717 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700718 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800719}
720
chaviwa76b2712017-09-20 12:02:26 -0700721void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
David Sodman41fdfc92017-11-06 16:09:56 -0800722 clearWithOpenGL(renderArea, 0, 0, 0, 0);
Mathias Agopian13127d82013-03-05 17:47:11 -0800723}
724
Chia-I Wu30505fb2018-03-26 16:20:31 -0700725void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type, bool callIntoHwc) {
726 if (getBE().mHwcLayers.count(hwcId) == 0) {
727 ALOGE("setCompositionType called without a valid HWC layer");
728 return;
729 }
730 auto& hwcInfo = getBE().mHwcLayers[hwcId];
731 auto& hwcLayer = hwcInfo.layer;
David Sodmanf6a38932018-05-25 15:27:50 -0700732 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(), to_string(type).c_str(),
Chia-I Wu30505fb2018-03-26 16:20:31 -0700733 static_cast<int>(callIntoHwc));
734 if (hwcInfo.compositionType != type) {
735 ALOGV(" actually setting");
736 hwcInfo.compositionType = type;
737 if (callIntoHwc) {
David Sodmanf6a38932018-05-25 15:27:50 -0700738 auto error = hwcLayer->setCompositionType(type);
Chia-I Wu30505fb2018-03-26 16:20:31 -0700739 ALOGE_IF(error != HWC2::Error::None,
740 "[%s] Failed to set "
741 "composition type %s: %s (%d)",
742 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
743 static_cast<int32_t>(error));
744 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800745 }
746}
747
748HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700749 if (hwcId == DisplayDevice::DISPLAY_ID_INVALID) {
750 // If we're querying the composition type for a display that does not
751 // have a HWC counterpart, then it will always be Client
752 return HWC2::Composition::Client;
753 }
Chia-I Wu30505fb2018-03-26 16:20:31 -0700754 if (getBE().mHwcLayers.count(hwcId) == 0) {
755 ALOGE("getCompositionType called with an invalid HWC layer");
756 return HWC2::Composition::Invalid;
757 }
758 return getBE().mHwcLayers.at(hwcId).compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800759}
760
761void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700762 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800763 ALOGE("setClearClientTarget called without a valid HWC layer");
764 return;
765 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700766 getBE().mHwcLayers[hwcId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800767}
768
769bool Layer::getClearClientTarget(int32_t hwcId) const {
David Sodman6f65f3e2017-11-03 14:28:09 -0700770 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800771 ALOGE("getClearClientTarget called without a valid HWC layer");
772 return false;
773 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700774 return getBE().mHwcLayers.at(hwcId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800775}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800776
Dan Stozacac35382016-01-27 12:21:06 -0800777bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
778 if (point->getFrameNumber() <= mCurrentFrameNumber) {
779 // Don't bother with a SyncPoint, since we've already latched the
780 // relevant frame
781 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700782 }
783
Dan Stozacac35382016-01-27 12:21:06 -0800784 Mutex::Autolock lock(mLocalSyncPointMutex);
785 mLocalSyncPoints.push_back(point);
786 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700787}
788
Mathias Agopian13127d82013-03-05 17:47:11 -0800789void Layer::setFiltering(bool filtering) {
790 mFiltering = filtering;
791}
792
793bool Layer::getFiltering() const {
794 return mFiltering;
795}
796
Mathias Agopian13127d82013-03-05 17:47:11 -0800797// ----------------------------------------------------------------------------
798// local state
799// ----------------------------------------------------------------------------
800
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000801static void boundPoint(vec2* point, const Rect& crop) {
802 if (point->x < crop.left) {
803 point->x = crop.left;
804 }
805 if (point->x > crop.right) {
806 point->x = crop.right;
807 }
808 if (point->y < crop.top) {
809 point->y = crop.top;
810 }
811 if (point->y > crop.bottom) {
812 point->y = crop.bottom;
813 }
814}
815
chaviwa76b2712017-09-20 12:02:26 -0700816void Layer::computeGeometry(const RenderArea& renderArea, Mesh& mesh,
817 bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700818 const Layer::State& s(getDrawingState());
chaviwa76b2712017-09-20 12:02:26 -0700819 const Transform renderAreaTransform(renderArea.getTransform());
820 const uint32_t height = renderArea.getHeight();
Dan Stoza80d61162017-12-20 15:57:52 -0800821 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700822
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000823 vec2 lt = vec2(win.left, win.top);
824 vec2 lb = vec2(win.left, win.bottom);
825 vec2 rb = vec2(win.right, win.bottom);
826 vec2 rt = vec2(win.right, win.top);
827
Robert Carr1f0a16a2016-10-24 16:27:39 -0700828 Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000829 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700830 lt = layerTransform.transform(lt);
831 lb = layerTransform.transform(lb);
832 rb = layerTransform.transform(rb);
833 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000834 }
835
Robert Carrb5d3d262016-03-25 15:08:13 -0700836 if (!s.finalCrop.isEmpty()) {
837 boundPoint(&lt, s.finalCrop);
838 boundPoint(&lb, s.finalCrop);
839 boundPoint(&rb, s.finalCrop);
840 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000841 }
842
Mathias Agopianff2ed702013-09-01 21:36:12 -0700843 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700844 position[0] = renderAreaTransform.transform(lt);
845 position[1] = renderAreaTransform.transform(lb);
846 position[2] = renderAreaTransform.transform(rb);
847 position[3] = renderAreaTransform.transform(rt);
David Sodman41fdfc92017-11-06 16:09:56 -0800848 for (size_t i = 0; i < 4; i++) {
chaviwa76b2712017-09-20 12:02:26 -0700849 position[i].y = height - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -0800850 }
851}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800852
David Sodman41fdfc92017-11-06 16:09:56 -0800853bool Layer::isSecure() const {
Dan Stoza23116082015-06-18 14:58:39 -0700854 const Layer::State& s(mDrawingState);
855 return (s.flags & layer_state_t::eLayerSecure);
856}
857
Mathias Agopian13127d82013-03-05 17:47:11 -0800858void Layer::setVisibleRegion(const Region& visibleRegion) {
859 // always called from main thread
860 this->visibleRegion = visibleRegion;
861}
862
863void Layer::setCoveredRegion(const Region& coveredRegion) {
864 // always called from main thread
865 this->coveredRegion = coveredRegion;
866}
867
David Sodman41fdfc92017-11-06 16:09:56 -0800868void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800869 // always called from main thread
870 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
871}
872
Robert Carre5f4f692018-01-12 13:12:28 -0800873void Layer::clearVisibilityRegions() {
874 visibleRegion.clear();
875 visibleNonTransparentRegion.clear();
876 coveredRegion.clear();
877}
878
Mathias Agopian13127d82013-03-05 17:47:11 -0800879// ----------------------------------------------------------------------------
880// transaction
881// ----------------------------------------------------------------------------
882
Dan Stoza7dde5992015-05-22 09:51:44 -0700883void Layer::pushPendingState() {
884 if (!mCurrentState.modified) {
885 return;
886 }
887
Dan Stoza7dde5992015-05-22 09:51:44 -0700888 // If this transaction is waiting on the receipt of a frame, generate a sync
889 // point and send it to the remote layer.
Robert Carr0d480722017-01-10 16:42:54 -0800890 if (mCurrentState.barrierLayer != nullptr) {
891 sp<Layer> barrierLayer = mCurrentState.barrierLayer.promote();
892 if (barrierLayer == nullptr) {
893 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700894 // If we can't promote the layer we are intended to wait on,
895 // then it is expired or otherwise invalid. Allow this transaction
896 // to be applied as per normal (no synchronization).
Robert Carr0d480722017-01-10 16:42:54 -0800897 mCurrentState.barrierLayer = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800898 } else {
David Sodman41fdfc92017-11-06 16:09:56 -0800899 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber);
Robert Carr0d480722017-01-10 16:42:54 -0800900 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800901 mRemoteSyncPoints.push_back(std::move(syncPoint));
902 } else {
903 // We already missed the frame we're supposed to synchronize
904 // on, so go ahead and apply the state update
Robert Carr0d480722017-01-10 16:42:54 -0800905 mCurrentState.barrierLayer = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800906 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700907 }
908
Dan Stoza7dde5992015-05-22 09:51:44 -0700909 // Wake us up to check if the frame has been received
910 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700911 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700912 }
913 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700914 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700915}
916
Pablo Ceballos05289c22016-04-14 15:49:55 -0700917void Layer::popPendingState(State* stateToCommit) {
918 auto oldFlags = stateToCommit->flags;
919 *stateToCommit = mPendingStates[0];
David Sodman41fdfc92017-11-06 16:09:56 -0800920 stateToCommit->flags =
921 (oldFlags & ~stateToCommit->mask) | (stateToCommit->flags & stateToCommit->mask);
Dan Stoza7dde5992015-05-22 09:51:44 -0700922
923 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700924 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700925}
926
Pablo Ceballos05289c22016-04-14 15:49:55 -0700927bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700928 bool stateUpdateAvailable = false;
929 while (!mPendingStates.empty()) {
Robert Carr0d480722017-01-10 16:42:54 -0800930 if (mPendingStates[0].barrierLayer != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700931 if (mRemoteSyncPoints.empty()) {
932 // If we don't have a sync point for this, apply it anyway. It
933 // will be visually wrong, but it should keep us from getting
934 // into too much trouble.
935 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700936 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700937 stateUpdateAvailable = true;
938 continue;
939 }
940
David Sodman41fdfc92017-11-06 16:09:56 -0800941 if (mRemoteSyncPoints.front()->getFrameNumber() != mPendingStates[0].frameNumber) {
942 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800943
944 // Signal our end of the sync point and then dispose of it
945 mRemoteSyncPoints.front()->setTransactionApplied();
946 mRemoteSyncPoints.pop_front();
947 continue;
948 }
949
Dan Stoza7dde5992015-05-22 09:51:44 -0700950 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
951 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700952 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700953 stateUpdateAvailable = true;
954
955 // Signal our end of the sync point and then dispose of it
956 mRemoteSyncPoints.front()->setTransactionApplied();
957 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800958 } else {
959 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700960 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700961 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700962 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700963 stateUpdateAvailable = true;
964 }
965 }
966
967 // If we still have pending updates, wake SurfaceFlinger back up and point
968 // it at this layer so we can process them
969 if (!mPendingStates.empty()) {
970 setTransactionFlags(eTransactionNeeded);
971 mFlinger->setTransactionFlags(eTraversalNeeded);
972 }
973
974 mCurrentState.modified = false;
975 return stateUpdateAvailable;
976}
977
Mathias Agopian13127d82013-03-05 17:47:11 -0800978uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800979 ATRACE_CALL();
980
Dan Stoza7dde5992015-05-22 09:51:44 -0700981 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -0700982 Layer::State c = getCurrentState();
983 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700984 return 0;
985 }
986
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700987 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800988
David Sodman41fdfc92017-11-06 16:09:56 -0800989 const bool sizeChanged = (c.requested.w != s.requested.w) || (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700990
David Sodmaneb085e02017-10-05 18:49:04 -0700991 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700992 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000993 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800994 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
995 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
996 " requested={ wh={%4u,%4u} }}\n"
997 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
998 " requested={ wh={%4u,%4u} }}\n",
David Sodman9eeae692017-11-02 10:53:32 -0700999 this, getName().string(), mCurrentTransform,
1000 getEffectiveScalingMode(), c.active.w, c.active.h, c.crop.left, c.crop.top,
1001 c.crop.right, c.crop.bottom, c.crop.getWidth(), c.crop.getHeight(), c.requested.w,
1002 c.requested.h, s.active.w, s.active.h, s.crop.left, s.crop.top, s.crop.right,
1003 s.crop.bottom, s.crop.getWidth(), s.crop.getHeight(), s.requested.w,
1004 s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001005
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001006 // record the new size, form this point on, when the client request
1007 // a buffer, it'll get the new size.
David Sodmaneb085e02017-10-05 18:49:04 -07001008 setDefaultBufferSize(c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001009 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001010
Robert Carre392b552017-09-19 12:16:05 -07001011 // Don't let Layer::doTransaction update the drawing state
1012 // if we have a pending resize, unless we are in fixed-size mode.
1013 // the drawing state will be updated only once we receive a buffer
1014 // with the correct size.
1015 //
1016 // In particular, we want to make sure the clip (which is part
1017 // of the geometry state) is latched together with the size but is
1018 // latched immediately when no resizing is involved.
1019 //
1020 // If a sideband stream is attached, however, we want to skip this
1021 // optimization so that transactions aren't missed when a buffer
1022 // never arrives
1023 //
1024 // In the case that we don't have a buffer we ignore other factors
1025 // and avoid entering the resizePending state. At a high level the
1026 // resizePending state is to avoid applying the state of the new buffer
1027 // to the old buffer. However in the state where we don't have an old buffer
1028 // there is no such concern but we may still be being used as a parent layer.
David Sodman41fdfc92017-11-06 16:09:56 -08001029 const bool resizePending = ((c.requested.w != c.active.w) || (c.requested.h != c.active.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -08001030 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001031 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -08001032 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001033 flags |= eDontUpdateGeometryState;
1034 }
1035 }
1036
Robert Carr7bf247e2017-05-18 14:02:49 -07001037 // Here we apply various requested geometry states, depending on our
1038 // latching configuration. See Layer.h for a detailed discussion of
1039 // how geometry latching is controlled.
1040 if (!(flags & eDontUpdateGeometryState)) {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001041 Layer::State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001042
1043 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1044 // mode, which causes attributes which normally latch regardless of scaling mode,
1045 // to be delayed. We copy the requested state to the active state making sure
1046 // to respect these rules (again see Layer.h for a detailed discussion).
1047 //
1048 // There is an awkward asymmetry in the handling of the crop states in the position
1049 // states, as can be seen below. Largely this arises from position and transform
1050 // being stored in the same data structure while having different latching rules.
1051 // b/38182305
1052 //
1053 // Careful that "c" and editCurrentState may not begin as equivalent due to
1054 // applyPendingStates in the presence of deferred transactions.
1055 if (mFreezeGeometryUpdates) {
Robert Carr82364e32016-05-15 11:27:47 -07001056 float tx = c.active.transform.tx();
1057 float ty = c.active.transform.ty();
1058 c.active = c.requested;
1059 c.active.transform.set(tx, ty);
1060 editCurrentState.active = c.active;
1061 } else {
1062 editCurrentState.active = editCurrentState.requested;
1063 c.active = c.requested;
1064 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001065 }
1066
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001067 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001068 // invalidate and recompute the visible regions if needed
1069 flags |= Layer::eVisibleRegion;
1070 }
1071
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001072 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001073 // invalidate and recompute the visible regions if needed
1074 flags |= eVisibleRegion;
1075 this->contentDirty = true;
1076
1077 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001078 const uint8_t type = c.active.transform.getType();
David Sodman41fdfc92017-11-06 16:09:56 -08001079 mNeedsFiltering = (!c.active.transform.preserveRects() || (type >= Transform::SCALE));
Mathias Agopian13127d82013-03-05 17:47:11 -08001080 }
1081
Dan Stozac8145172016-04-28 16:29:06 -07001082 // If the layer is hidden, signal and clear out all local sync points so
1083 // that transactions for layers depending on this layer's frames becoming
1084 // visible are not blocked
1085 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001086 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001087 }
1088
Mathias Agopian13127d82013-03-05 17:47:11 -08001089 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001090 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001091 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001092}
1093
Pablo Ceballos05289c22016-04-14 15:49:55 -07001094void Layer::commitTransaction(const State& stateToCommit) {
1095 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001096}
1097
Mathias Agopian13127d82013-03-05 17:47:11 -08001098uint32_t Layer::getTransactionFlags(uint32_t flags) {
1099 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1100}
1101
1102uint32_t Layer::setTransactionFlags(uint32_t flags) {
1103 return android_atomic_or(flags, &mTransactionFlags);
1104}
1105
Robert Carr82364e32016-05-15 11:27:47 -07001106bool Layer::setPosition(float x, float y, bool immediate) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001107 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001108 return false;
1109 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001110
1111 // We update the requested and active position simultaneously because
1112 // we want to apply the position portion of the transform matrix immediately,
1113 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001114 mCurrentState.requested.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001115 if (immediate && !mFreezeGeometryUpdates) {
1116 // Here we directly update the active state
1117 // unlike other setters, because we store it within
1118 // the transform, but use different latching rules.
1119 // b/38182305
Robert Carr82364e32016-05-15 11:27:47 -07001120 mCurrentState.active.transform.set(x, y);
1121 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001122 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001123
Dan Stoza7dde5992015-05-22 09:51:44 -07001124 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001125 setTransactionFlags(eTransactionNeeded);
1126 return true;
1127}
Robert Carr82364e32016-05-15 11:27:47 -07001128
Robert Carr1f0a16a2016-10-24 16:27:39 -07001129bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1130 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1131 if (idx < 0) {
1132 return false;
1133 }
1134 if (childLayer->setLayer(z)) {
1135 mCurrentChildren.removeAt(idx);
1136 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001137 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001138 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001139 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001140}
1141
Robert Carr503c7042017-09-27 15:06:08 -07001142bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1143 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1144 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1145 if (idx < 0) {
1146 return false;
1147 }
1148 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1149 mCurrentChildren.removeAt(idx);
1150 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001151 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001152 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001153 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001154}
1155
Robert Carrae060832016-11-28 10:51:00 -08001156bool Layer::setLayer(int32_t z) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001157 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001158 mCurrentState.sequence++;
1159 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001160 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001161
1162 // Discard all relative layering.
1163 if (mCurrentState.zOrderRelativeOf != nullptr) {
1164 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1165 if (strongRelative != nullptr) {
1166 strongRelative->removeZOrderRelative(this);
1167 }
1168 mCurrentState.zOrderRelativeOf = nullptr;
1169 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001170 setTransactionFlags(eTransactionNeeded);
1171 return true;
1172}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001173
Robert Carrdb66e622017-04-10 16:55:57 -07001174void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1175 mCurrentState.zOrderRelatives.remove(relative);
1176 mCurrentState.sequence++;
1177 mCurrentState.modified = true;
1178 setTransactionFlags(eTransactionNeeded);
1179}
1180
1181void Layer::addZOrderRelative(const wp<Layer>& relative) {
1182 mCurrentState.zOrderRelatives.add(relative);
1183 mCurrentState.modified = true;
1184 mCurrentState.sequence++;
1185 setTransactionFlags(eTransactionNeeded);
1186}
1187
Robert Carr503d2bd2017-12-04 15:49:47 -08001188bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001189 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1190 if (handle == nullptr) {
1191 return false;
1192 }
1193 sp<Layer> relative = handle->owner.promote();
1194 if (relative == nullptr) {
1195 return false;
1196 }
1197
Robert Carr503d2bd2017-12-04 15:49:47 -08001198 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1199 mCurrentState.zOrderRelativeOf == relative) {
1200 return false;
1201 }
1202
Robert Carrdb66e622017-04-10 16:55:57 -07001203 mCurrentState.sequence++;
1204 mCurrentState.modified = true;
Robert Carr503d2bd2017-12-04 15:49:47 -08001205 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001206
chaviw9ab4bd12017-11-03 13:11:00 -07001207 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1208 if (oldZOrderRelativeOf != nullptr) {
1209 oldZOrderRelativeOf->removeZOrderRelative(this);
1210 }
Robert Carrdb66e622017-04-10 16:55:57 -07001211 mCurrentState.zOrderRelativeOf = relative;
1212 relative->addZOrderRelative(this);
1213
1214 setTransactionFlags(eTransactionNeeded);
1215
1216 return true;
1217}
1218
Mathias Agopian13127d82013-03-05 17:47:11 -08001219bool Layer::setSize(uint32_t w, uint32_t h) {
David Sodman41fdfc92017-11-06 16:09:56 -08001220 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001221 mCurrentState.requested.w = w;
1222 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001223 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001224 setTransactionFlags(eTransactionNeeded);
1225 return true;
1226}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001227bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001228 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001229 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001230 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001231 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001232 setTransactionFlags(eTransactionNeeded);
1233 return true;
1234}
chaviw13fdc492017-06-27 12:40:18 -07001235
1236bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001237 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1238 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001239 return false;
1240
1241 mCurrentState.sequence++;
1242 mCurrentState.color.r = color.r;
1243 mCurrentState.color.g = color.g;
1244 mCurrentState.color.b = color.b;
1245 mCurrentState.modified = true;
1246 setTransactionFlags(eTransactionNeeded);
1247 return true;
1248}
1249
Mathias Agopian13127d82013-03-05 17:47:11 -08001250bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
1251 mCurrentState.sequence++;
David Sodman41fdfc92017-11-06 16:09:56 -08001252 mCurrentState.requested.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001253 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001254 setTransactionFlags(eTransactionNeeded);
1255 return true;
1256}
1257bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001258 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001259 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001260 setTransactionFlags(eTransactionNeeded);
1261 return true;
1262}
1263bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1264 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001265 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001266 mCurrentState.sequence++;
1267 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001268 mCurrentState.mask = mask;
1269 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001270 setTransactionFlags(eTransactionNeeded);
1271 return true;
1272}
Robert Carr99e27f02016-06-16 15:18:02 -07001273
1274bool Layer::setCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001275 if (mCurrentState.requestedCrop == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001276 mCurrentState.sequence++;
Robert Carr99e27f02016-06-16 15:18:02 -07001277 mCurrentState.requestedCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001278 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr99e27f02016-06-16 15:18:02 -07001279 mCurrentState.crop = crop;
1280 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001281 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1282
Dan Stoza7dde5992015-05-22 09:51:44 -07001283 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001284 setTransactionFlags(eTransactionNeeded);
1285 return true;
1286}
Robert Carr8d5227b2017-03-16 15:41:03 -07001287
1288bool Layer::setFinalCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001289 if (mCurrentState.requestedFinalCrop == crop) return false;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001290 mCurrentState.sequence++;
Robert Carr8d5227b2017-03-16 15:41:03 -07001291 mCurrentState.requestedFinalCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001292 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001293 mCurrentState.finalCrop = crop;
1294 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001295 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1296
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001297 mCurrentState.modified = true;
1298 setTransactionFlags(eTransactionNeeded);
1299 return true;
1300}
Mathias Agopian13127d82013-03-05 17:47:11 -08001301
Robert Carrc3574f72016-03-24 12:19:32 -07001302bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001303 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001304 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001305 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001306 return true;
1307}
1308
rongliucfb187b2018-03-14 12:26:23 -07001309void Layer::setInfo(int32_t type, int32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001310 mCurrentState.appId = appId;
1311 mCurrentState.type = type;
1312 mCurrentState.modified = true;
1313 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001314}
1315
Mathias Agopian13127d82013-03-05 17:47:11 -08001316bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001317 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001318 mCurrentState.sequence++;
1319 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001320 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001321 setTransactionFlags(eTransactionNeeded);
1322 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001323}
1324
Robert Carr1f0a16a2016-10-24 16:27:39 -07001325uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001326 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001327 if (p == nullptr) {
1328 return getDrawingState().layerStack;
1329 }
1330 return p->getLayerStack();
1331}
1332
David Sodman41fdfc92017-11-06 16:09:56 -08001333void Layer::deferTransactionUntil(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001334 mCurrentState.barrierLayer = barrierLayer;
Dan Stoza7dde5992015-05-22 09:51:44 -07001335 mCurrentState.frameNumber = frameNumber;
1336 // We don't set eTransactionNeeded, because just receiving a deferral
1337 // request without any other state updates shouldn't actually induce a delay
1338 mCurrentState.modified = true;
1339 pushPendingState();
Robert Carr0d480722017-01-10 16:42:54 -08001340 mCurrentState.barrierLayer = nullptr;
Dan Stoza792e5292016-02-11 11:43:58 -08001341 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001342 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001343}
1344
David Sodman41fdfc92017-11-06 16:09:56 -08001345void Layer::deferTransactionUntil(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001346 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
1347 deferTransactionUntil(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001348}
1349
Dan Stozaee44edd2015-03-23 15:50:23 -07001350
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001351// ----------------------------------------------------------------------------
1352// pageflip handling...
1353// ----------------------------------------------------------------------------
1354
Robert Carr1f0a16a2016-10-24 16:27:39 -07001355bool Layer::isHiddenByPolicy() const {
1356 const Layer::State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001357 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001358 if (parent != nullptr && parent->isHiddenByPolicy()) {
1359 return true;
1360 }
1361 return s.flags & layer_state_t::eLayerHidden;
1362}
1363
David Sodman41fdfc92017-11-06 16:09:56 -08001364uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001365 // TODO: should we do something special if mSecure is set?
1366 if (mProtectedByApp) {
1367 // need a hardware-protected path to external video sink
1368 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001369 }
Riley Andrews03414a12014-07-01 14:22:59 -07001370 if (mPotentialCursor) {
1371 usage |= GraphicBuffer::USAGE_CURSOR;
1372 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001373 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001374 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001375}
1376
Mathias Agopian84300952012-11-21 16:02:13 -08001377void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001378 uint32_t orientation = 0;
1379 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001380 // The transform hint is used to improve performance, but we can
1381 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001382 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07001383 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07001384 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07001385 if (orientation & Transform::ROT_INVALID) {
1386 orientation = 0;
1387 }
1388 }
David Sodmaneb085e02017-10-05 18:49:04 -07001389 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001390}
1391
Mathias Agopian13127d82013-03-05 17:47:11 -08001392// ----------------------------------------------------------------------------
1393// debugging
1394// ----------------------------------------------------------------------------
1395
Kalle Raitaa099a242017-01-11 11:17:29 -08001396LayerDebugInfo Layer::getLayerDebugInfo() const {
1397 LayerDebugInfo info;
1398 const Layer::State& ds = getDrawingState();
1399 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001400 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001401 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1402 info.mType = String8(getTypeId());
1403 info.mTransparentRegion = ds.activeTransparentRegion;
1404 info.mVisibleRegion = visibleRegion;
1405 info.mSurfaceDamageRegion = surfaceDamageRegion;
1406 info.mLayerStack = getLayerStack();
1407 info.mX = ds.active.transform.tx();
1408 info.mY = ds.active.transform.ty();
1409 info.mZ = ds.z;
1410 info.mWidth = ds.active.w;
1411 info.mHeight = ds.active.h;
1412 info.mCrop = ds.crop;
1413 info.mFinalCrop = ds.finalCrop;
chaviw13fdc492017-06-27 12:40:18 -07001414 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001415 info.mFlags = ds.flags;
1416 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001417 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Kalle Raitaa099a242017-01-11 11:17:29 -08001418 info.mMatrix[0][0] = ds.active.transform[0][0];
1419 info.mMatrix[0][1] = ds.active.transform[0][1];
1420 info.mMatrix[1][0] = ds.active.transform[1][0];
1421 info.mMatrix[1][1] = ds.active.transform[1][1];
1422 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001423 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001424 if (buffer != 0) {
1425 info.mActiveBufferWidth = buffer->getWidth();
1426 info.mActiveBufferHeight = buffer->getHeight();
1427 info.mActiveBufferStride = buffer->getStride();
1428 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001429 } else {
1430 info.mActiveBufferWidth = 0;
1431 info.mActiveBufferHeight = 0;
1432 info.mActiveBufferStride = 0;
1433 info.mActiveBufferFormat = 0;
1434 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001435 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001436 info.mNumQueuedFrames = getQueuedFrameCount();
1437 info.mRefreshPending = isBufferLatched();
1438 info.mIsOpaque = isOpaque(ds);
1439 info.mContentDirty = contentDirty;
1440 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001441}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001442
Dan Stozae22aec72016-08-01 13:20:59 -07001443void Layer::miniDumpHeader(String8& result) {
1444 result.append("----------------------------------------");
1445 result.append("---------------------------------------\n");
1446 result.append(" Layer name\n");
1447 result.append(" Z | ");
1448 result.append(" Comp Type | ");
1449 result.append(" Disp Frame (LTRB) | ");
1450 result.append(" Source Crop (LTRB)\n");
1451 result.append("----------------------------------------");
1452 result.append("---------------------------------------\n");
1453}
1454
1455void Layer::miniDump(String8& result, int32_t hwcId) const {
David Sodman6f65f3e2017-11-03 14:28:09 -07001456 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stozae22aec72016-08-01 13:20:59 -07001457 return;
1458 }
1459
1460 String8 name;
1461 if (mName.length() > 77) {
1462 std::string shortened;
1463 shortened.append(mName.string(), 36);
1464 shortened.append("[...]");
1465 shortened.append(mName.string() + (mName.length() - 36), 36);
1466 name = shortened.c_str();
1467 } else {
1468 name = mName;
1469 }
1470
1471 result.appendFormat(" %s\n", name.string());
1472
1473 const Layer::State& layerState(getDrawingState());
Chia-I Wu30505fb2018-03-26 16:20:31 -07001474 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(hwcId);
Chia-I Wu1e043612018-03-01 09:45:09 -08001475 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
1476 result.appendFormat(" rel %6d | ", layerState.z);
1477 } else {
1478 result.appendFormat(" %10d | ", layerState.z);
1479 }
David Sodman41fdfc92017-11-06 16:09:56 -08001480 result.appendFormat("%10s | ", to_string(getCompositionType(hwcId)).c_str());
Chia-I Wu30505fb2018-03-26 16:20:31 -07001481 const Rect& frame = hwcInfo.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001482 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Chia-I Wu30505fb2018-03-26 16:20:31 -07001483 const FloatRect& crop = hwcInfo.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001484 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 -07001485
1486 result.append("- - - - - - - - - - - - - - - - - - - - ");
1487 result.append("- - - - - - - - - - - - - - - - - - - -\n");
1488}
Dan Stozae22aec72016-08-01 13:20:59 -07001489
Svetoslavd85084b2014-03-20 10:28:31 -07001490void Layer::dumpFrameStats(String8& result) const {
1491 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001492}
1493
Svetoslavd85084b2014-03-20 10:28:31 -07001494void Layer::clearFrameStats() {
1495 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001496}
1497
Jamie Gennis6547ff42013-07-16 20:12:42 -07001498void Layer::logFrameStats() {
1499 mFrameTracker.logAndResetStats(mName);
1500}
1501
Svetoslavd85084b2014-03-20 10:28:31 -07001502void Layer::getFrameStats(FrameStats* outStats) const {
1503 mFrameTracker.getStats(outStats);
1504}
1505
Brian Andersond6927fb2016-07-23 23:37:30 -07001506void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001507 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001508 Mutex::Autolock lock(mFrameEventHistoryMutex);
1509 mFrameEventHistory.checkFencesForCompletion();
1510 mFrameEventHistory.dump(result);
1511}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001512
Brian Anderson5ea5e592016-12-01 16:54:33 -08001513void Layer::onDisconnect() {
1514 Mutex::Autolock lock(mFrameEventHistoryMutex);
1515 mFrameEventHistory.onDisconnect();
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001516 mTimeStats.onDisconnect(getName().c_str());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001517}
1518
Brian Anderson3890c392016-07-25 12:48:08 -07001519void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001520 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001521 if (newTimestamps) {
1522 mTimeStats.setPostTime(getName().c_str(), newTimestamps->frameNumber,
1523 newTimestamps->postedTime);
1524 }
1525
Brian Andersond6927fb2016-07-23 23:37:30 -07001526 Mutex::Autolock lock(mFrameEventHistoryMutex);
1527 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001528 // If there are any unsignaled fences in the aquire timeline at this
1529 // point, the previously queued frame hasn't been latched yet. Go ahead
1530 // and try to get the signal time here so the syscall is taken out of
1531 // the main thread's critical path.
1532 mAcquireTimeline.updateSignalTimes();
1533 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001534 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001535 mFrameEventHistory.addQueue(*newTimestamps);
1536 }
1537
Brian Anderson3890c392016-07-25 12:48:08 -07001538 if (outDelta) {
1539 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001540 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001541}
Dan Stozae77c7662016-05-13 11:37:28 -07001542
Chia-I Wu98f1c102017-05-30 14:54:08 -07001543size_t Layer::getChildrenCount() const {
1544 size_t count = 0;
1545 for (const sp<Layer>& child : mCurrentChildren) {
1546 count += 1 + child->getChildrenCount();
1547 }
1548 return count;
1549}
1550
Robert Carr1f0a16a2016-10-24 16:27:39 -07001551void Layer::addChild(const sp<Layer>& layer) {
1552 mCurrentChildren.add(layer);
1553 layer->setParent(this);
1554}
1555
1556ssize_t Layer::removeChild(const sp<Layer>& layer) {
1557 layer->setParent(nullptr);
1558 return mCurrentChildren.remove(layer);
1559}
1560
Robert Carr1db73f62016-12-21 12:58:51 -08001561bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1562 sp<Handle> handle = nullptr;
1563 sp<Layer> newParent = nullptr;
1564 if (newParentHandle == nullptr) {
1565 return false;
1566 }
1567 handle = static_cast<Handle*>(newParentHandle.get());
1568 newParent = handle->owner.promote();
1569 if (newParent == nullptr) {
1570 ALOGE("Unable to promote Layer handle");
1571 return false;
1572 }
1573
1574 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001575 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001576
1577 sp<Client> client(child->mClientRef.promote());
1578 if (client != nullptr) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001579 client->updateParent(newParent);
Robert Carr1db73f62016-12-21 12:58:51 -08001580 }
1581 }
1582 mCurrentChildren.clear();
1583
1584 return true;
1585}
1586
Robert Carr15eae092018-03-23 13:43:53 -07001587void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001588 for (const sp<Layer>& child : mDrawingChildren) {
1589 child->mDrawingParent = newParent;
1590 }
1591}
1592
chaviwf1961f72017-09-18 16:41:07 -07001593bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1594 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001595 return false;
1596 }
1597
1598 auto handle = static_cast<Handle*>(newParentHandle.get());
1599 sp<Layer> newParent = handle->owner.promote();
1600 if (newParent == nullptr) {
1601 ALOGE("Unable to promote Layer handle");
1602 return false;
1603 }
1604
chaviwf1961f72017-09-18 16:41:07 -07001605 sp<Layer> parent = getParent();
1606 if (parent != nullptr) {
1607 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001608 }
chaviwf1961f72017-09-18 16:41:07 -07001609 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001610
chaviwf1961f72017-09-18 16:41:07 -07001611 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001612 sp<Client> newParentClient(newParent->mClientRef.promote());
1613
chaviwf1961f72017-09-18 16:41:07 -07001614 if (client != newParentClient) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001615 client->updateParent(newParent);
chaviw06178942017-07-27 10:25:59 -07001616 }
1617
chaviw06178942017-07-27 10:25:59 -07001618 return true;
1619}
1620
Robert Carr9524cb32017-02-13 11:32:32 -08001621bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001622 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001623 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001624 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001625 if (client != nullptr && parentClient != client) {
Robert Carr7f619b22017-11-06 12:56:35 -08001626 client->detachLayer(child.get());
1627 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001628 }
Robert Carr7f619b22017-11-06 12:56:35 -08001629 }
Robert Carr9524cb32017-02-13 11:32:32 -08001630
1631 return true;
1632}
1633
Chia-I Wu11481472018-05-04 10:43:19 -07001634bool Layer::isLegacyDataSpace() const {
1635 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001636 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001637 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001638}
1639
Robert Carr1f0a16a2016-10-24 16:27:39 -07001640void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001641 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001642}
1643
1644void Layer::clearSyncPoints() {
1645 for (const auto& child : mCurrentChildren) {
1646 child->clearSyncPoints();
1647 }
1648
1649 Mutex::Autolock lock(mLocalSyncPointMutex);
1650 for (auto& point : mLocalSyncPoints) {
1651 point->setFrameAvailable();
1652 }
1653 mLocalSyncPoints.clear();
1654}
1655
1656int32_t Layer::getZ() const {
1657 return mDrawingState.z;
1658}
1659
Robert Carr29abff82017-12-04 13:51:20 -08001660bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1661 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1662 const State& state = useDrawing ? mDrawingState : mCurrentState;
1663 return state.zOrderRelativeOf != nullptr;
1664}
1665
David Sodman41fdfc92017-11-06 16:09:56 -08001666__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001667 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001668 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1669 "makeTraversalList received invalid stateSet");
1670 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1671 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1672 const State& state = useDrawing ? mDrawingState : mCurrentState;
1673
Robert Carr29abff82017-12-04 13:51:20 -08001674 if (state.zOrderRelatives.size() == 0) {
1675 *outSkipRelativeZUsers = true;
1676 return children;
1677 }
1678
Robert Carrdb66e622017-04-10 16:55:57 -07001679 LayerVector traverse;
Dan Stoza412903f2017-04-27 13:42:17 -07001680 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001681 sp<Layer> strongRelative = weakRelative.promote();
1682 if (strongRelative != nullptr) {
1683 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001684 }
1685 }
1686
Dan Stoza412903f2017-04-27 13:42:17 -07001687 for (const sp<Layer>& child : children) {
Robert Carr503c7042017-09-27 15:06:08 -07001688 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1689 if (childState.zOrderRelativeOf != nullptr) {
1690 continue;
1691 }
Robert Carrdb66e622017-04-10 16:55:57 -07001692 traverse.add(child);
1693 }
1694
1695 return traverse;
1696}
1697
Robert Carr1f0a16a2016-10-24 16:27:39 -07001698/**
Robert Carrdb66e622017-04-10 16:55:57 -07001699 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001700 */
Dan Stoza412903f2017-04-27 13:42:17 -07001701void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001702 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1703 // produce a new list for traversing, including our relatives, and not including our children
1704 // who are relatives of another surface. In the case that there are no relative Z,
1705 // makeTraversalList returns our children directly to avoid significant overhead.
1706 // However in this case we need to take the responsibility for filtering children which
1707 // are relatives of another surface here.
1708 bool skipRelativeZUsers = false;
1709 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001710
Robert Carr1f0a16a2016-10-24 16:27:39 -07001711 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001712 for (; i < list.size(); i++) {
1713 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001714 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1715 continue;
1716 }
1717
Robert Carrdb66e622017-04-10 16:55:57 -07001718 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001719 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001720 }
Dan Stoza412903f2017-04-27 13:42:17 -07001721 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001722 }
Robert Carr29abff82017-12-04 13:51:20 -08001723
Dan Stoza412903f2017-04-27 13:42:17 -07001724 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001725 for (; i < list.size(); i++) {
1726 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001727
1728 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1729 continue;
1730 }
Dan Stoza412903f2017-04-27 13:42:17 -07001731 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001732 }
1733}
1734
1735/**
Robert Carrdb66e622017-04-10 16:55:57 -07001736 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001737 */
Dan Stoza412903f2017-04-27 13:42:17 -07001738void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1739 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001740 // See traverseInZOrder for documentation.
1741 bool skipRelativeZUsers = false;
1742 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001743
Robert Carr1f0a16a2016-10-24 16:27:39 -07001744 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001745 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001746 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001747
1748 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1749 continue;
1750 }
1751
Robert Carrdb66e622017-04-10 16:55:57 -07001752 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001753 break;
1754 }
Dan Stoza412903f2017-04-27 13:42:17 -07001755 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001756 }
Dan Stoza412903f2017-04-27 13:42:17 -07001757 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001758 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001759 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001760
1761 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1762 continue;
1763 }
1764
Dan Stoza412903f2017-04-27 13:42:17 -07001765 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001766 }
1767}
1768
chaviw4b129c22018-04-09 16:19:43 -07001769LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1770 const std::vector<Layer*>& layersInTree) {
1771 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1772 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001773 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1774 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
chaviw4b129c22018-04-09 16:19:43 -07001775 const State& state = useDrawing ? mDrawingState : mCurrentState;
1776
1777 LayerVector traverse;
1778 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1779 sp<Layer> strongRelative = weakRelative.promote();
1780 // Only add relative layers that are also descendents of the top most parent of the tree.
1781 // If a relative layer is not a descendent, then it should be ignored.
1782 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1783 traverse.add(strongRelative);
1784 }
1785 }
1786
1787 for (const sp<Layer>& child : children) {
1788 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1789 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1790 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1791 // the child here since it won't be added later as a relative.
1792 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1793 childState.zOrderRelativeOf.promote().get())) {
1794 continue;
1795 }
1796 traverse.add(child);
1797 }
1798
1799 return traverse;
1800}
1801
1802void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1803 LayerVector::StateSet stateSet,
1804 const LayerVector::Visitor& visitor) {
1805 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001806
1807 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001808 for (; i < list.size(); i++) {
1809 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001810 if (relative->getZ() >= 0) {
1811 break;
1812 }
chaviw4b129c22018-04-09 16:19:43 -07001813 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001814 }
chaviw4b129c22018-04-09 16:19:43 -07001815
chaviwa76b2712017-09-20 12:02:26 -07001816 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001817 for (; i < list.size(); i++) {
1818 const auto& relative = list[i];
1819 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001820 }
1821}
1822
chaviw4b129c22018-04-09 16:19:43 -07001823std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1824 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1825 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1826
1827 std::vector<Layer*> layersInTree = {this};
1828 for (size_t i = 0; i < children.size(); i++) {
1829 const auto& child = children[i];
1830 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1831 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1832 }
1833
1834 return layersInTree;
1835}
1836
1837void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1838 const LayerVector::Visitor& visitor) {
1839 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1840 std::sort(layersInTree.begin(), layersInTree.end());
1841 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1842}
1843
Robert Carr1f0a16a2016-10-24 16:27:39 -07001844Transform Layer::getTransform() const {
1845 Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001846 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001847 if (p != nullptr) {
1848 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001849
1850 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1851 // it isFixedSize) then there may be additional scaling not accounted
1852 // for in the transform. We need to mirror this scaling in child surfaces
1853 // or we will break the contract where WM can treat child surfaces as
1854 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08001855 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001856 int bufferWidth;
1857 int bufferHeight;
1858 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08001859 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
1860 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001861 } else {
David Sodman0cc69182017-11-17 12:12:07 -08001862 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
1863 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001864 }
David Sodman41fdfc92017-11-06 16:09:56 -08001865 float sx = p->getDrawingState().active.w / static_cast<float>(bufferWidth);
1866 float sy = p->getDrawingState().active.h / static_cast<float>(bufferHeight);
Robert Carr9b429f42017-04-17 14:56:57 -07001867 Transform extraParentScaling;
1868 extraParentScaling.set(sx, 0, 0, sy);
1869 t = t * extraParentScaling;
1870 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001871 }
1872 return t * getDrawingState().active.transform;
1873}
1874
chaviw13fdc492017-06-27 12:40:18 -07001875half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001876 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001877
chaviw13fdc492017-06-27 12:40:18 -07001878 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1879 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001880}
Robert Carr6452f122017-03-21 10:41:29 -07001881
chaviw13fdc492017-06-27 12:40:18 -07001882half4 Layer::getColor() const {
1883 const half4 color(getDrawingState().color);
1884 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001885}
Robert Carr6452f122017-03-21 10:41:29 -07001886
Robert Carr1f0a16a2016-10-24 16:27:39 -07001887void Layer::commitChildList() {
1888 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1889 const auto& child = mCurrentChildren[i];
1890 child->commitChildList();
1891 }
1892 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001893 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001894}
1895
chaviw1d044282017-09-27 12:19:28 -07001896void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1897 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1898 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1899 const State& state = useDrawing ? mDrawingState : mCurrentState;
1900
1901 Transform requestedTransform = state.active.transform;
1902 Transform transform = getTransform();
1903
1904 layerInfo->set_id(sequence);
1905 layerInfo->set_name(getName().c_str());
1906 layerInfo->set_type(String8(getTypeId()));
1907
1908 for (const auto& child : children) {
1909 layerInfo->add_children(child->sequence);
1910 }
1911
1912 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1913 sp<Layer> strongRelative = weakRelative.promote();
1914 if (strongRelative != nullptr) {
1915 layerInfo->add_relatives(strongRelative->sequence);
1916 }
1917 }
1918
1919 LayerProtoHelper::writeToProto(state.activeTransparentRegion,
1920 layerInfo->mutable_transparent_region());
1921 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
1922 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
1923
1924 layerInfo->set_layer_stack(getLayerStack());
1925 layerInfo->set_z(state.z);
1926
1927 PositionProto* position = layerInfo->mutable_position();
1928 position->set_x(transform.tx());
1929 position->set_y(transform.ty());
1930
1931 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
1932 requestedPosition->set_x(requestedTransform.tx());
1933 requestedPosition->set_y(requestedTransform.ty());
1934
1935 SizeProto* size = layerInfo->mutable_size();
1936 size->set_w(state.active.w);
1937 size->set_h(state.active.h);
1938
1939 LayerProtoHelper::writeToProto(state.crop, layerInfo->mutable_crop());
1940 LayerProtoHelper::writeToProto(state.finalCrop, layerInfo->mutable_final_crop());
1941
1942 layerInfo->set_is_opaque(isOpaque(state));
1943 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07001944
1945 // XXX (b/79210409) mCurrentDataSpace is not protected
1946 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1947
chaviw1d044282017-09-27 12:19:28 -07001948 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1949 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
1950 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
1951 layerInfo->set_flags(state.flags);
1952
1953 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1954 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
1955
Jorim Jaggi8e0af362017-11-14 16:28:28 +01001956 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07001957 if (parent != nullptr) {
1958 layerInfo->set_parent(parent->sequence);
1959 }
1960
1961 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1962 if (zOrderRelativeOf != nullptr) {
1963 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1964 }
1965
Chia-I Wu01591c92018-05-22 12:03:00 -07001966 // XXX getBE().compositionInfo.mBuffer is not protected
David Sodman0cc69182017-11-17 12:12:07 -08001967 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001968 if (buffer != nullptr) {
1969 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
chaviw1d044282017-09-27 12:19:28 -07001970 }
1971
1972 layerInfo->set_queued_frames(getQueuedFrameCount());
1973 layerInfo->set_refresh_pending(isBufferLatched());
rongliucfb187b2018-03-14 12:26:23 -07001974 layerInfo->set_window_type(state.type);
1975 layerInfo->set_app_id(state.appId);
chaviw1d044282017-09-27 12:19:28 -07001976}
1977
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001978void Layer::writeToProto(LayerProto* layerInfo, int32_t hwcId) {
1979 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
1980
1981 const auto& hwcInfo = getBE().mHwcLayers.at(hwcId);
1982
1983 const Rect& frame = hwcInfo.displayFrame;
1984 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
1985
1986 const FloatRect& crop = hwcInfo.sourceCrop;
1987 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
1988
1989 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
1990 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08001991
1992 const int32_t compositionType = static_cast<int32_t>(hwcInfo.compositionType);
1993 layerInfo->set_hwc_composition_type(compositionType);
1994
1995 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
1996 static_cast<BufferLayer*>(this)->isProtected()) {
1997 layerInfo->set_is_protected(true);
1998 } else {
1999 layerInfo->set_is_protected(false);
2000 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002001}
2002
Mathias Agopian13127d82013-03-05 17:47:11 -08002003// ---------------------------------------------------------------------------
2004
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002005}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002006
2007#if defined(__gl_h_)
2008#error "don't include gl/gl.h in this file"
2009#endif
2010
2011#if defined(__gl2_h_)
2012#error "don't include gl2/gl2.h in this file"
2013#endif