blob: 40d89bdb1057bca799a0173e194c7a8fe330fb79 [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),
chaviwfd462612018-05-31 16:11:27 -070097 mCurrentChildren(LayerVector::StateSet::Current),
98 mDrawingChildren(LayerVector::StateSet::Drawing),
David Sodman2b727ac2017-12-21 14:28:08 -080099 mBE{this, name.string()} {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800100
Mathias Agopiana67932f2011-04-20 14:20:59 -0700101 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700102
103 uint32_t layerFlags = 0;
David Sodman41fdfc92017-11-06 16:09:56 -0800104 if (flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
105 if (flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
106 if (flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700107
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700108 mName = name;
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700109 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700110
111 mCurrentState.active.w = w;
112 mCurrentState.active.h = h;
David Sodman0c69cad2017-08-21 12:12:51 -0700113 mCurrentState.flags = layerFlags;
Robert Carr3dcabfa2016-03-01 18:36:58 -0800114 mCurrentState.active.transform.set(0, 0);
Robert Carrb5d3d262016-03-25 15:08:13 -0700115 mCurrentState.crop.makeInvalid();
116 mCurrentState.finalCrop.makeInvalid();
Robert Carr7bf247e2017-05-18 14:02:49 -0700117 mCurrentState.requestedFinalCrop = mCurrentState.finalCrop;
118 mCurrentState.requestedCrop = mCurrentState.crop;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700119 mCurrentState.z = 0;
chaviw13fdc492017-06-27 12:40:18 -0700120 mCurrentState.color.a = 1.0f;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700121 mCurrentState.layerStack = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700122 mCurrentState.sequence = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700123 mCurrentState.requested = mCurrentState.active;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500124 mCurrentState.appId = 0;
125 mCurrentState.type = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700126
127 // drawing state & current state are identical
128 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700129
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800130 CompositorTiming compositorTiming;
131 flinger->getCompositorTiming(&compositorTiming);
132 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
Jamie Gennise8696a42012-01-15 18:54:57 -0800133}
134
Dan Stoza436ccf32018-06-21 12:10:12 -0700135void Layer::onFirstRef() NO_THREAD_SAFETY_ANALYSIS {
136 if (!isCreatedFromMainThread()) {
137 // Grab the SF state lock during this since it's the only way to safely access HWC
138 mFlinger->mStateLock.lock();
139 }
140
141 const auto& hwc = mFlinger->getHwComposer();
142 const auto& activeConfig = hwc.getActiveConfig(HWC_DISPLAY_PRIMARY);
143 nsecs_t displayPeriod = activeConfig->getVsyncPeriod();
144 mFrameTracker.setDisplayRefreshPeriod(displayPeriod);
145
146 if (!isCreatedFromMainThread()) {
147 mFlinger->mStateLock.unlock();
148 }
149}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700150
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700151Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800152 sp<Client> c(mClientRef.promote());
153 if (c != 0) {
154 c->detachLayer(this);
155 }
156
157 for (auto& point : mRemoteSyncPoints) {
158 point->setTransactionApplied();
159 }
160 for (auto& point : mLocalSyncPoints) {
161 point->setFrameAvailable();
162 }
Jamie Gennis6547ff42013-07-16 20:12:42 -0700163 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700164}
165
Mathias Agopian13127d82013-03-05 17:47:11 -0800166// ---------------------------------------------------------------------------
167// callbacks
168// ---------------------------------------------------------------------------
169
David Sodmaneb085e02017-10-05 18:49:04 -0700170/*
171 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
172 * Layer. So, the implementation is done in BufferLayer. When called on a
173 * ColorLayer object, it's essentially a NOP.
174 */
David Sodmaneb085e02017-10-05 18:49:04 -0700175void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800176
Chia-I Wuc6657022017-08-15 11:18:17 -0700177void Layer::onRemovedFromCurrentState() {
178 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
179
chaviw8b3871a2017-11-01 17:41:01 -0700180 mPendingRemoval = true;
181
Robert Carr5edb1ad2017-04-25 10:54:24 -0700182 if (mCurrentState.zOrderRelativeOf != nullptr) {
183 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
184 if (strongRelative != nullptr) {
185 strongRelative->removeZOrderRelative(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700186 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700187 }
188 mCurrentState.zOrderRelativeOf = nullptr;
189 }
190
Chia-I Wuc6657022017-08-15 11:18:17 -0700191 for (const auto& child : mCurrentChildren) {
192 child->onRemovedFromCurrentState();
193 }
194}
Chia-I Wu38512252017-05-17 14:36:16 -0700195
Chia-I Wuc6657022017-08-15 11:18:17 -0700196void Layer::onRemoved() {
197 // the layer is removed from SF mLayersPendingRemoval
David Sodmaneb085e02017-10-05 18:49:04 -0700198 abandon();
Chia-I Wuc6657022017-08-15 11:18:17 -0700199
Steven Thomasb02664d2017-07-26 18:48:28 -0700200 destroyAllHwcLayers();
Chia-I Wu38512252017-05-17 14:36:16 -0700201
Robert Carr1f0a16a2016-10-24 16:27:39 -0700202 for (const auto& child : mCurrentChildren) {
203 child->onRemoved();
204 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700205}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700206
Mathias Agopian13127d82013-03-05 17:47:11 -0800207// ---------------------------------------------------------------------------
208// set-up
209// ---------------------------------------------------------------------------
210
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700211const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800212 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213}
214
chaviw13fdc492017-06-27 12:40:18 -0700215bool Layer::getPremultipledAlpha() const {
216 return mPremultipliedAlpha;
217}
218
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700219sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800220 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700221 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800222}
223
224// ---------------------------------------------------------------------------
225// h/w composer set-up
226// ---------------------------------------------------------------------------
227
Dominik Laskowski7e045462018-05-30 13:02:02 -0700228bool Layer::createHwcLayer(HWComposer* hwc, int32_t displayId) {
229 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(displayId) != 0,
230 "Already have a layer for display %d", displayId);
231 HWC2::Layer* layer = hwc->createLayer(displayId);
Steven Thomasb02664d2017-07-26 18:48:28 -0700232 if (!layer) {
233 return false;
234 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700235 LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers[displayId];
Steven Thomasb02664d2017-07-26 18:48:28 -0700236 hwcInfo.hwc = hwc;
237 hwcInfo.layer = layer;
David Sodmanf6a38932018-05-25 15:27:50 -0700238 layer->setLayerDestroyedListener(
Dominik Laskowski7e045462018-05-30 13:02:02 -0700239 [this, displayId](HWC2::Layer* /*layer*/) { getBE().mHwcLayers.erase(displayId); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700240 return true;
241}
242
Dominik Laskowski7e045462018-05-30 13:02:02 -0700243bool Layer::destroyHwcLayer(int32_t displayId) {
244 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu83806892017-11-16 10:50:20 -0800245 return false;
Steven Thomasb02664d2017-07-26 18:48:28 -0700246 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700247 auto& hwcInfo = getBE().mHwcLayers[displayId];
David Sodman41fdfc92017-11-06 16:09:56 -0800248 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr, "Attempt to destroy null layer");
Steven Thomasb02664d2017-07-26 18:48:28 -0700249 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
Dominik Laskowski7e045462018-05-30 13:02:02 -0700250 hwcInfo.hwc->destroyLayer(displayId, hwcInfo.layer);
David Sodmanf6a38932018-05-25 15:27:50 -0700251 // The layer destroyed listener should have cleared the entry from
252 // mHwcLayers. Verify that.
Dominik Laskowski7e045462018-05-30 13:02:02 -0700253 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(displayId) != 0,
David Sodmanf6a38932018-05-25 15:27:50 -0700254 "Stale layer entry in getBE().mHwcLayers");
Chia-I Wu83806892017-11-16 10:50:20 -0800255 return true;
Steven Thomasb02664d2017-07-26 18:48:28 -0700256}
257
258void Layer::destroyAllHwcLayers() {
David Sodman6f65f3e2017-11-03 14:28:09 -0700259 size_t numLayers = getBE().mHwcLayers.size();
Steven Thomasb02664d2017-07-26 18:48:28 -0700260 for (size_t i = 0; i < numLayers; ++i) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700261 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.empty(), "destroyAllHwcLayers failed");
262 destroyHwcLayer(getBE().mHwcLayers.begin()->first);
Steven Thomasb02664d2017-07-26 18:48:28 -0700263 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700264 LOG_ALWAYS_FATAL_IF(!getBE().mHwcLayers.empty(),
David Sodman41fdfc92017-11-06 16:09:56 -0800265 "All hardware composer layers should have been destroyed");
Steven Thomasb02664d2017-07-26 18:48:28 -0700266}
Steven Thomasb02664d2017-07-26 18:48:28 -0700267
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800268Rect Layer::getContentCrop() const {
269 // this is the crop rectangle that applies to the buffer
270 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700271 Rect crop;
272 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800273 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700274 crop = mCurrentCrop;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800275 } else if (getBE().compositionInfo.mBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800276 // otherwise we use the whole buffer
David Sodman0cc69182017-11-17 12:12:07 -0800277 crop = getBE().compositionInfo.mBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700278 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800279 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700280 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700281 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700282 return crop;
283}
284
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700285static Rect reduce(const Rect& win, const Region& exclude) {
286 if (CC_LIKELY(exclude.isEmpty())) {
287 return win;
288 }
289 if (exclude.isRect()) {
290 return win.reduce(exclude.getBounds());
291 }
292 return Region(win).subtract(exclude).getBounds();
293}
294
Dan Stoza80d61162017-12-20 15:57:52 -0800295static FloatRect reduce(const FloatRect& win, const Region& exclude) {
296 if (CC_LIKELY(exclude.isEmpty())) {
297 return win;
298 }
299 // Convert through Rect (by rounding) for lack of FloatRegion
300 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
301}
302
Robert Carr1f0a16a2016-10-24 16:27:39 -0700303Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
304 const Layer::State& s(getDrawingState());
305 Rect win(s.active.w, s.active.h);
306
307 if (!s.crop.isEmpty()) {
308 win.intersect(s.crop, &win);
309 }
310
311 Transform t = getTransform();
312 win = t.transform(win);
313
Robert Carr41b08b52017-06-01 16:11:34 -0700314 if (!s.finalCrop.isEmpty()) {
315 win.intersect(s.finalCrop, &win);
316 }
317
Chia-I Wue41dbe62017-06-13 14:10:56 -0700318 const sp<Layer>& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700319 // Now we need to calculate the parent bounds, so we can clip ourselves to those.
320 // When calculating the parent bounds for purposes of clipping,
321 // we don't need to constrain the parent to its transparent region.
322 // The transparent region is an optimization based on the
323 // buffer contents of the layer, but does not affect the space allocated to
324 // it by policy, and thus children should be allowed to extend into the
325 // parent's transparent region. In fact one of the main uses, is to reduce
326 // buffer allocation size in cases where a child window sits behind a main window
327 // (by marking the hole in the parent window as a transparent region)
328 if (p != nullptr) {
329 Rect bounds = p->computeScreenBounds(false);
330 bounds.intersect(win, &win);
331 }
332
333 if (reduceTransparentRegion) {
334 auto const screenTransparentRegion = t.transform(s.activeTransparentRegion);
335 win = reduce(win, screenTransparentRegion);
336 }
337
338 return win;
339}
340
Dan Stoza80d61162017-12-20 15:57:52 -0800341FloatRect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700342 const Layer::State& s(getDrawingState());
Michael Lentine6c925ed2014-09-26 17:55:01 -0700343 return computeBounds(s.activeTransparentRegion);
344}
345
Dan Stoza80d61162017-12-20 15:57:52 -0800346FloatRect Layer::computeBounds(const Region& activeTransparentRegion) const {
Michael Lentine6c925ed2014-09-26 17:55:01 -0700347 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800348 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700349
350 if (!s.crop.isEmpty()) {
351 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800352 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700353
Chia-I Wue41dbe62017-06-13 14:10:56 -0700354 const auto& p = mDrawingParent.promote();
Robert Carrd4ae7f32018-06-07 16:10:57 -0700355 FloatRect floatWin = win.toFloatRect();
356 FloatRect parentBounds = floatWin;
Robert Carr1f0a16a2016-10-24 16:27:39 -0700357 if (p != nullptr) {
Robert Carrd4ae7f32018-06-07 16:10:57 -0700358 // We pass an empty Region here for reasons mirroring that of the case described in
359 // the computeScreenBounds reduceTransparentRegion=false case.
360 parentBounds = p->computeBounds(Region());
Robert Carr1f0a16a2016-10-24 16:27:39 -0700361 }
362
Robert Carrd4ae7f32018-06-07 16:10:57 -0700363 Transform t = s.active.transform;
Dan Stoza80d61162017-12-20 15:57:52 -0800364
Robert Carrd4ae7f32018-06-07 16:10:57 -0700365
366 if (p != nullptr || !s.finalCrop.isEmpty()) {
Dan Stoza80d61162017-12-20 15:57:52 -0800367 floatWin = t.transform(floatWin);
Robert Carrd4ae7f32018-06-07 16:10:57 -0700368 floatWin = floatWin.intersect(parentBounds);
369
370 if (!s.finalCrop.isEmpty()) {
371 floatWin = floatWin.intersect(s.finalCrop.toFloatRect());
372 }
Dan Stoza80d61162017-12-20 15:57:52 -0800373 floatWin = t.inverse().transform(floatWin);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700374 }
375
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700376 // subtract the transparent region and snap to the bounds
Dan Stoza80d61162017-12-20 15:57:52 -0800377 return reduce(floatWin, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800378}
379
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700380Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& display) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700381 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800382 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700383 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800384
385 // apply the projection's clipping to the window crop in
386 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700387 // if there are no window scaling involved, this operation will map to full
388 // pixels in the buffer.
389 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
390 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700391
392 Rect activeCrop(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700393 if (!s.crop.isEmpty()) {
Chia-I Wudf7867f2017-07-20 14:24:37 -0700394 activeCrop.intersect(s.crop, &activeCrop);
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700395 }
396
Robert Carr1f0a16a2016-10-24 16:27:39 -0700397 Transform t = getTransform();
398 activeCrop = t.transform(activeCrop);
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700399 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000400 activeCrop.clear();
401 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700402 if (!s.finalCrop.isEmpty()) {
David Sodman41fdfc92017-11-06 16:09:56 -0800403 if (!activeCrop.intersect(s.finalCrop, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000404 activeCrop.clear();
405 }
406 }
chaviwb1154d12017-10-31 14:15:36 -0700407
408 const auto& p = mDrawingParent.promote();
409 if (p != nullptr) {
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700410 auto parentCrop = p->computeInitialCrop(display);
chaviwb1154d12017-10-31 14:15:36 -0700411 activeCrop.intersect(parentCrop, &activeCrop);
412 }
413
Robert Carr1f0a16a2016-10-24 16:27:39 -0700414 return activeCrop;
415}
416
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700417FloatRect Layer::computeCrop(const sp<const DisplayDevice>& display) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700418 // the content crop is the area of the content that gets scaled to the
419 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800420 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700421
422 // In addition there is a WM-specified crop we pull from our drawing state.
423 const State& s(getDrawingState());
424
425 // Screen space to make reduction to parent crop clearer.
Dominik Laskowskia2edf612018-06-01 13:15:16 -0700426 Rect activeCrop = computeInitialCrop(display);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700427 Transform t = getTransform();
428 // Back to layer space to work with the content crop.
429 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800430
Michael Lentine28ea2172014-11-19 18:32:37 -0800431 // This needs to be here as transform.transform(Rect) computes the
432 // transformed rect and then takes the bounding box of the result before
433 // returning. This means
434 // transform.inverse().transform(transform.transform(Rect)) != Rect
435 // in which case we need to make sure the final rect is clipped to the
436 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000437 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
438 activeCrop.clear();
439 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800440
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700441 // subtract the transparent region and snap to the bounds
442 activeCrop = reduce(activeCrop, s.activeTransparentRegion);
443
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000444 // Transform the window crop to match the buffer coordinate system,
445 // which means using the inverse of the current transform set on the
446 // SurfaceFlingerConsumer.
447 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700448 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000449 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700450 * the code below applies the primary display's inverse transform to the
451 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000452 */
David Sodman41fdfc92017-11-06 16:09:56 -0800453 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000454 // calculate the inverse transform
455 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800456 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800457 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000458 // and apply to the current transform
David Sodman41fdfc92017-11-06 16:09:56 -0800459 invTransform = (Transform(invTransformOrient) * Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800460 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000461
462 int winWidth = s.active.w;
463 int winHeight = s.active.h;
464 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
465 // If the activeCrop has been rotate the ends are rotated but not
466 // the space itself so when transforming ends back we can't rely on
467 // a modification of the axes of rotation. To account for this we
468 // need to reorient the inverse rotation in terms of the current
469 // axes of rotation.
470 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
471 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
472 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800473 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000474 }
475 winWidth = s.active.h;
476 winHeight = s.active.w;
477 }
David Sodman41fdfc92017-11-06 16:09:56 -0800478 const Rect winCrop = activeCrop.transform(invTransform, s.active.w, s.active.h);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000479
480 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800481 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000482 float yScale = crop.getHeight() / float(winHeight);
483
David Sodman41fdfc92017-11-06 16:09:56 -0800484 float insetL = winCrop.left * xScale;
485 float insetT = winCrop.top * yScale;
486 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000487 float insetB = (winHeight - winCrop.bottom) * yScale;
488
David Sodman41fdfc92017-11-06 16:09:56 -0800489 crop.left += insetL;
490 crop.top += insetT;
491 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000492 crop.bottom -= insetB;
493
Mathias Agopian13127d82013-03-05 17:47:11 -0800494 return crop;
495}
496
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700497void Layer::setGeometry(const sp<const DisplayDevice>& display, uint32_t z) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700498 const auto displayId = display->getId();
Peiyong Lin91b1df22018-06-18 18:00:16 -0700499 if (!hasHwcLayer(displayId)) {
500 ALOGE("[%s] failed to setGeometry: no HWC layer found (%d)",
501 mName.string(), displayId);
502 return;
503 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700504 auto& hwcInfo = getBE().mHwcLayers[displayId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700505
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700506 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800507 hwcInfo.forceClientComposition = false;
508
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700509 if (isSecure() && !display->isSecure()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800510 hwcInfo.forceClientComposition = true;
511 }
512
Lloyd Pique074e8122018-07-26 12:57:23 -0700513 auto& hwcLayer = hwcInfo.layer;
514
Mathias Agopian13127d82013-03-05 17:47:11 -0800515 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700516 const State& s(getDrawingState());
David Revemanecf0fa52017-03-03 11:32:44 -0500517 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700518 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800519 blendMode =
520 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800521 }
Lloyd Pique074e8122018-07-26 12:57:23 -0700522 auto error = hwcLayer->setBlendMode(blendMode);
523 ALOGE_IF(error != HWC2::Error::None,
524 "[%s] Failed to set blend mode %s:"
525 " %s (%d)",
526 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
527 static_cast<int32_t>(error));
Mathias Agopian13127d82013-03-05 17:47:11 -0800528
529 // apply the layer's transform, followed by the display's global transform
530 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700531 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700532 Transform t = getTransform();
Robert Carrb5d3d262016-03-25 15:08:13 -0700533 if (!s.crop.isEmpty()) {
534 Rect activeCrop(s.crop);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700535 activeCrop = t.transform(activeCrop);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700536 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000537 activeCrop.clear();
538 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700539 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800540 // This needs to be here as transform.transform(Rect) computes the
541 // transformed rect and then takes the bounding box of the result before
542 // returning. This means
543 // transform.inverse().transform(transform.transform(Rect)) != Rect
544 // in which case we need to make sure the final rect is clipped to the
545 // display bounds.
David Sodman41fdfc92017-11-06 16:09:56 -0800546 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000547 activeCrop.clear();
548 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700549 // mark regions outside the crop as transparent
550 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
David Sodman41fdfc92017-11-06 16:09:56 -0800551 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom, s.active.w, s.active.h));
552 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
553 activeTransparentRegion.orSelf(
554 Rect(activeCrop.right, activeCrop.top, s.active.w, activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700555 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700556
Dan Stoza80d61162017-12-20 15:57:52 -0800557 // computeBounds returns a FloatRect to provide more accuracy during the
558 // transformation. We then round upon constructing 'frame'.
559 Rect frame{t.transform(computeBounds(activeTransparentRegion))};
Robert Carrb5d3d262016-03-25 15:08:13 -0700560 if (!s.finalCrop.isEmpty()) {
David Sodman41fdfc92017-11-06 16:09:56 -0800561 if (!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000562 frame.clear();
563 }
564 }
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700565 if (!frame.intersect(display->getViewport(), &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000566 frame.clear();
567 }
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700568 const Transform& tr = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800569 Rect transformedFrame = tr.transform(frame);
Lloyd Pique074e8122018-07-26 12:57:23 -0700570 error = hwcLayer->setDisplayFrame(transformedFrame);
571 if (error != HWC2::Error::None) {
572 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
573 transformedFrame.left, transformedFrame.top, transformedFrame.right,
574 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
575 } else {
576 hwcInfo.displayFrame = transformedFrame;
577 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800578
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700579 FloatRect sourceCrop = computeCrop(display);
Lloyd Pique074e8122018-07-26 12:57:23 -0700580 error = hwcLayer->setSourceCrop(sourceCrop);
581 if (error != HWC2::Error::None) {
582 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
583 "%s (%d)",
584 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
585 to_string(error).c_str(), static_cast<int32_t>(error));
586 } else {
587 hwcInfo.sourceCrop = sourceCrop;
588 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800589
chaviw13fdc492017-06-27 12:40:18 -0700590 float alpha = static_cast<float>(getAlpha());
Lloyd Pique074e8122018-07-26 12:57:23 -0700591 error = hwcLayer->setPlaneAlpha(alpha);
592 ALOGE_IF(error != HWC2::Error::None,
593 "[%s] Failed to set plane alpha %.3f: "
594 "%s (%d)",
595 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800596
Lloyd Pique074e8122018-07-26 12:57:23 -0700597 error = hwcLayer->setZOrder(z);
598 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
599 to_string(error).c_str(), static_cast<int32_t>(error));
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500600
Albert Chaulk2a589632017-05-04 16:59:44 -0400601 int type = s.type;
602 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700603 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400604 if (parent.get()) {
605 auto& parentState = parent->getDrawingState();
rongliucfb187b2018-03-14 12:26:23 -0700606 if (parentState.type >= 0 || parentState.appId >= 0) {
607 type = parentState.type;
608 appId = parentState.appId;
609 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400610 }
611
Lloyd Pique074e8122018-07-26 12:57:23 -0700612 error = hwcLayer->setInfo(type, appId);
613 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
614 static_cast<int32_t>(error));
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800615
Mathias Agopian29a367b2011-07-12 14:51:45 -0700616 /*
617 * Transformations are applied in this order:
618 * 1) buffer orientation/flip/mirror
619 * 2) state transformation (window manager)
620 * 3) layer orientation (screen orientation)
621 * (NOTE: the matrices are multiplied in reverse order)
622 */
623
624 const Transform bufferOrientation(mCurrentTransform);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700625 Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700626
Robert Carrcae605c2017-03-29 12:10:31 -0700627 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700628 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700629 * the code below applies the primary display's inverse transform to the
630 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700631 */
David Sodman41fdfc92017-11-06 16:09:56 -0800632 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700633 // calculate the inverse transform
634 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800635 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700636 }
Robert Carrcae605c2017-03-29 12:10:31 -0700637
638 /*
639 * Here we cancel out the orientation component of the WM transform.
640 * The scaling and translate components are already included in our bounds
641 * computation so it's enough to just omit it in the composition.
642 * See comment in onDraw with ref to b/36727915 for why.
643 */
644 transform = Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700645 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700646
647 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800648 const uint32_t orientation = transform.getOrientation();
Jorim Jaggif3bd94a2018-03-27 15:38:03 +0200649 if (orientation & Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800650 // we can only handle simple transformation
Lloyd Pique074e8122018-07-26 12:57:23 -0700651 hwcInfo.forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800652 } else {
653 auto transform = static_cast<HWC2::Transform>(orientation);
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800654 hwcInfo.transform = transform;
Lloyd Pique074e8122018-07-26 12:57:23 -0700655 auto error = hwcLayer->setTransform(transform);
656 ALOGE_IF(error != HWC2::Error::None,
657 "[%s] Failed to set transform %s: "
658 "%s (%d)",
659 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
660 static_cast<int32_t>(error));
David Sodman4b7c4bc2017-11-17 12:13:59 -0800661 }
662}
663
Dominik Laskowski7e045462018-05-30 13:02:02 -0700664void Layer::forceClientComposition(int32_t displayId) {
665 if (getBE().mHwcLayers.count(displayId) == 0) {
666 ALOGE("forceClientComposition: no HWC layer found (%d)", displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800667 return;
668 }
669
Dominik Laskowski7e045462018-05-30 13:02:02 -0700670 getBE().mHwcLayers[displayId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800671}
Dan Stozaee44edd2015-03-23 15:50:23 -0700672
Dominik Laskowski7e045462018-05-30 13:02:02 -0700673bool Layer::getForceClientComposition(int32_t displayId) {
674 if (getBE().mHwcLayers.count(displayId) == 0) {
675 ALOGE("getForceClientComposition: no HWC layer found (%d)", displayId);
chaviwc9232ed2017-11-14 15:31:15 -0800676 return false;
677 }
678
Dominik Laskowski7e045462018-05-30 13:02:02 -0700679 return getBE().mHwcLayers[displayId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800680}
681
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700682void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700683 const auto displayId = display->getId();
684 if (getBE().mHwcLayers.count(displayId) == 0 ||
685 getCompositionType(displayId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800686 return;
687 }
688
689 // This gives us only the "orientation" component of the transform
690 const State& s(getCurrentState());
691
692 // Apply the layer's transform, followed by the display's global transform
693 // Here we're guaranteed that the layer's transform preserves rects
694 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700695 if (!s.crop.isEmpty()) {
696 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800697 }
698 // Subtract the transparent region and snap to the bounds
699 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700700 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700701 frame.intersect(display->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700702 if (!s.finalCrop.isEmpty()) {
703 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000704 }
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700705 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800706 auto position = displayTransform.transform(frame);
707
Dominik Laskowski7e045462018-05-30 13:02:02 -0700708 auto error =
709 getBE().mHwcLayers[displayId].layer->setCursorPosition(position.left, position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800710 ALOGE_IF(error != HWC2::Error::None,
711 "[%s] Failed to set cursor position "
712 "to (%d, %d): %s (%d)",
713 mName.string(), position.left, position.top, to_string(error).c_str(),
714 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800715}
Riley Andrews03414a12014-07-01 14:22:59 -0700716
Mathias Agopian13127d82013-03-05 17:47:11 -0800717// ---------------------------------------------------------------------------
718// drawing...
719// ---------------------------------------------------------------------------
720
chaviwa76b2712017-09-20 12:02:26 -0700721void Layer::draw(const RenderArea& renderArea, const Region& clip) const {
722 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800723}
724
chaviwa76b2712017-09-20 12:02:26 -0700725void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) const {
726 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800727}
728
chaviwa76b2712017-09-20 12:02:26 -0700729void Layer::draw(const RenderArea& renderArea) const {
730 onDraw(renderArea, Region(renderArea.getBounds()), false);
Dan Stozac7014012014-02-14 15:03:43 -0800731}
732
David Sodman41fdfc92017-11-06 16:09:56 -0800733void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
734 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800735 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700736 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700737 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700738 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800739}
740
chaviwa76b2712017-09-20 12:02:26 -0700741void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
David Sodman41fdfc92017-11-06 16:09:56 -0800742 clearWithOpenGL(renderArea, 0, 0, 0, 0);
Mathias Agopian13127d82013-03-05 17:47:11 -0800743}
744
Dominik Laskowski7e045462018-05-30 13:02:02 -0700745void Layer::setCompositionType(int32_t displayId, HWC2::Composition type, bool callIntoHwc) {
746 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu30505fb2018-03-26 16:20:31 -0700747 ALOGE("setCompositionType called without a valid HWC layer");
748 return;
749 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700750 auto& hwcInfo = getBE().mHwcLayers[displayId];
Chia-I Wu30505fb2018-03-26 16:20:31 -0700751 auto& hwcLayer = hwcInfo.layer;
David Sodmanf6a38932018-05-25 15:27:50 -0700752 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(), to_string(type).c_str(),
Chia-I Wu30505fb2018-03-26 16:20:31 -0700753 static_cast<int>(callIntoHwc));
754 if (hwcInfo.compositionType != type) {
755 ALOGV(" actually setting");
756 hwcInfo.compositionType = type;
757 if (callIntoHwc) {
David Sodmanf6a38932018-05-25 15:27:50 -0700758 auto error = hwcLayer->setCompositionType(type);
Chia-I Wu30505fb2018-03-26 16:20:31 -0700759 ALOGE_IF(error != HWC2::Error::None,
760 "[%s] Failed to set "
761 "composition type %s: %s (%d)",
762 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
763 static_cast<int32_t>(error));
764 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800765 }
766}
767
Dominik Laskowski7e045462018-05-30 13:02:02 -0700768HWC2::Composition Layer::getCompositionType(int32_t displayId) const {
769 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700770 // If we're querying the composition type for a display that does not
771 // have a HWC counterpart, then it will always be Client
772 return HWC2::Composition::Client;
773 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700774 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu30505fb2018-03-26 16:20:31 -0700775 ALOGE("getCompositionType called with an invalid HWC layer");
776 return HWC2::Composition::Invalid;
777 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700778 return getBE().mHwcLayers.at(displayId).compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800779}
780
Dominik Laskowski7e045462018-05-30 13:02:02 -0700781void Layer::setClearClientTarget(int32_t displayId, bool clear) {
782 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800783 ALOGE("setClearClientTarget called without a valid HWC layer");
784 return;
785 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700786 getBE().mHwcLayers[displayId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800787}
788
Dominik Laskowski7e045462018-05-30 13:02:02 -0700789bool Layer::getClearClientTarget(int32_t displayId) const {
790 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800791 ALOGE("getClearClientTarget called without a valid HWC layer");
792 return false;
793 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700794 return getBE().mHwcLayers.at(displayId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800795}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800796
Dan Stozacac35382016-01-27 12:21:06 -0800797bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
798 if (point->getFrameNumber() <= mCurrentFrameNumber) {
799 // Don't bother with a SyncPoint, since we've already latched the
800 // relevant frame
801 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700802 }
803
Dan Stozacac35382016-01-27 12:21:06 -0800804 Mutex::Autolock lock(mLocalSyncPointMutex);
805 mLocalSyncPoints.push_back(point);
806 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700807}
808
Mathias Agopian13127d82013-03-05 17:47:11 -0800809void Layer::setFiltering(bool filtering) {
810 mFiltering = filtering;
811}
812
813bool Layer::getFiltering() const {
814 return mFiltering;
815}
816
Mathias Agopian13127d82013-03-05 17:47:11 -0800817// ----------------------------------------------------------------------------
818// local state
819// ----------------------------------------------------------------------------
820
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000821static void boundPoint(vec2* point, const Rect& crop) {
822 if (point->x < crop.left) {
823 point->x = crop.left;
824 }
825 if (point->x > crop.right) {
826 point->x = crop.right;
827 }
828 if (point->y < crop.top) {
829 point->y = crop.top;
830 }
831 if (point->y > crop.bottom) {
832 point->y = crop.bottom;
833 }
834}
835
chaviwa76b2712017-09-20 12:02:26 -0700836void Layer::computeGeometry(const RenderArea& renderArea, Mesh& mesh,
837 bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700838 const Layer::State& s(getDrawingState());
chaviwa76b2712017-09-20 12:02:26 -0700839 const Transform renderAreaTransform(renderArea.getTransform());
840 const uint32_t height = renderArea.getHeight();
Dan Stoza80d61162017-12-20 15:57:52 -0800841 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700842
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000843 vec2 lt = vec2(win.left, win.top);
844 vec2 lb = vec2(win.left, win.bottom);
845 vec2 rb = vec2(win.right, win.bottom);
846 vec2 rt = vec2(win.right, win.top);
847
Robert Carr1f0a16a2016-10-24 16:27:39 -0700848 Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000849 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700850 lt = layerTransform.transform(lt);
851 lb = layerTransform.transform(lb);
852 rb = layerTransform.transform(rb);
853 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000854 }
855
Robert Carrb5d3d262016-03-25 15:08:13 -0700856 if (!s.finalCrop.isEmpty()) {
857 boundPoint(&lt, s.finalCrop);
858 boundPoint(&lb, s.finalCrop);
859 boundPoint(&rb, s.finalCrop);
860 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000861 }
862
Mathias Agopianff2ed702013-09-01 21:36:12 -0700863 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700864 position[0] = renderAreaTransform.transform(lt);
865 position[1] = renderAreaTransform.transform(lb);
866 position[2] = renderAreaTransform.transform(rb);
867 position[3] = renderAreaTransform.transform(rt);
David Sodman41fdfc92017-11-06 16:09:56 -0800868 for (size_t i = 0; i < 4; i++) {
chaviwa76b2712017-09-20 12:02:26 -0700869 position[i].y = height - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -0800870 }
871}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800872
David Sodman41fdfc92017-11-06 16:09:56 -0800873bool Layer::isSecure() const {
Dan Stoza23116082015-06-18 14:58:39 -0700874 const Layer::State& s(mDrawingState);
875 return (s.flags & layer_state_t::eLayerSecure);
876}
877
Mathias Agopian13127d82013-03-05 17:47:11 -0800878void Layer::setVisibleRegion(const Region& visibleRegion) {
879 // always called from main thread
880 this->visibleRegion = visibleRegion;
881}
882
883void Layer::setCoveredRegion(const Region& coveredRegion) {
884 // always called from main thread
885 this->coveredRegion = coveredRegion;
886}
887
David Sodman41fdfc92017-11-06 16:09:56 -0800888void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800889 // always called from main thread
890 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
891}
892
Robert Carre5f4f692018-01-12 13:12:28 -0800893void Layer::clearVisibilityRegions() {
894 visibleRegion.clear();
895 visibleNonTransparentRegion.clear();
896 coveredRegion.clear();
897}
898
Mathias Agopian13127d82013-03-05 17:47:11 -0800899// ----------------------------------------------------------------------------
900// transaction
901// ----------------------------------------------------------------------------
902
Dan Stoza7dde5992015-05-22 09:51:44 -0700903void Layer::pushPendingState() {
904 if (!mCurrentState.modified) {
905 return;
906 }
907
Dan Stoza7dde5992015-05-22 09:51:44 -0700908 // If this transaction is waiting on the receipt of a frame, generate a sync
909 // point and send it to the remote layer.
Robert Carr0d480722017-01-10 16:42:54 -0800910 if (mCurrentState.barrierLayer != nullptr) {
911 sp<Layer> barrierLayer = mCurrentState.barrierLayer.promote();
912 if (barrierLayer == nullptr) {
913 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700914 // If we can't promote the layer we are intended to wait on,
915 // then it is expired or otherwise invalid. Allow this transaction
916 // to be applied as per normal (no synchronization).
Robert Carr0d480722017-01-10 16:42:54 -0800917 mCurrentState.barrierLayer = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800918 } else {
David Sodman41fdfc92017-11-06 16:09:56 -0800919 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber);
Robert Carr0d480722017-01-10 16:42:54 -0800920 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800921 mRemoteSyncPoints.push_back(std::move(syncPoint));
922 } else {
923 // We already missed the frame we're supposed to synchronize
924 // on, so go ahead and apply the state update
Robert Carr0d480722017-01-10 16:42:54 -0800925 mCurrentState.barrierLayer = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800926 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700927 }
928
Dan Stoza7dde5992015-05-22 09:51:44 -0700929 // Wake us up to check if the frame has been received
930 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700931 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700932 }
933 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700934 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700935}
936
Pablo Ceballos05289c22016-04-14 15:49:55 -0700937void Layer::popPendingState(State* stateToCommit) {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700938 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700939
940 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700941 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700942}
943
Pablo Ceballos05289c22016-04-14 15:49:55 -0700944bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700945 bool stateUpdateAvailable = false;
946 while (!mPendingStates.empty()) {
Robert Carr0d480722017-01-10 16:42:54 -0800947 if (mPendingStates[0].barrierLayer != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700948 if (mRemoteSyncPoints.empty()) {
949 // If we don't have a sync point for this, apply it anyway. It
950 // will be visually wrong, but it should keep us from getting
951 // into too much trouble.
952 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700953 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700954 stateUpdateAvailable = true;
955 continue;
956 }
957
David Sodman41fdfc92017-11-06 16:09:56 -0800958 if (mRemoteSyncPoints.front()->getFrameNumber() != mPendingStates[0].frameNumber) {
959 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800960
961 // Signal our end of the sync point and then dispose of it
962 mRemoteSyncPoints.front()->setTransactionApplied();
963 mRemoteSyncPoints.pop_front();
964 continue;
965 }
966
Dan Stoza7dde5992015-05-22 09:51:44 -0700967 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
968 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700969 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700970 stateUpdateAvailable = true;
971
972 // Signal our end of the sync point and then dispose of it
973 mRemoteSyncPoints.front()->setTransactionApplied();
974 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800975 } else {
976 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700977 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700978 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700979 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700980 stateUpdateAvailable = true;
981 }
982 }
983
984 // If we still have pending updates, wake SurfaceFlinger back up and point
985 // it at this layer so we can process them
986 if (!mPendingStates.empty()) {
987 setTransactionFlags(eTransactionNeeded);
988 mFlinger->setTransactionFlags(eTraversalNeeded);
989 }
990
991 mCurrentState.modified = false;
992 return stateUpdateAvailable;
993}
994
Mathias Agopian13127d82013-03-05 17:47:11 -0800995uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800996 ATRACE_CALL();
997
Dan Stoza7dde5992015-05-22 09:51:44 -0700998 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -0700999 Layer::State c = getCurrentState();
1000 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -07001001 return 0;
1002 }
1003
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001004 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001005
David Sodman41fdfc92017-11-06 16:09:56 -08001006 const bool sizeChanged = (c.requested.w != s.requested.w) || (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -07001007
David Sodmaneb085e02017-10-05 18:49:04 -07001008 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001009 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +00001010 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -08001011 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
1012 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1013 " requested={ wh={%4u,%4u} }}\n"
1014 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1015 " requested={ wh={%4u,%4u} }}\n",
David Sodman9eeae692017-11-02 10:53:32 -07001016 this, getName().string(), mCurrentTransform,
1017 getEffectiveScalingMode(), c.active.w, c.active.h, c.crop.left, c.crop.top,
1018 c.crop.right, c.crop.bottom, c.crop.getWidth(), c.crop.getHeight(), c.requested.w,
1019 c.requested.h, s.active.w, s.active.h, s.crop.left, s.crop.top, s.crop.right,
1020 s.crop.bottom, s.crop.getWidth(), s.crop.getHeight(), s.requested.w,
1021 s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001022
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001023 // record the new size, form this point on, when the client request
1024 // a buffer, it'll get the new size.
David Sodmaneb085e02017-10-05 18:49:04 -07001025 setDefaultBufferSize(c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001026 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001027
Robert Carre392b552017-09-19 12:16:05 -07001028 // Don't let Layer::doTransaction update the drawing state
1029 // if we have a pending resize, unless we are in fixed-size mode.
1030 // the drawing state will be updated only once we receive a buffer
1031 // with the correct size.
1032 //
1033 // In particular, we want to make sure the clip (which is part
1034 // of the geometry state) is latched together with the size but is
1035 // latched immediately when no resizing is involved.
1036 //
1037 // If a sideband stream is attached, however, we want to skip this
1038 // optimization so that transactions aren't missed when a buffer
1039 // never arrives
1040 //
1041 // In the case that we don't have a buffer we ignore other factors
1042 // and avoid entering the resizePending state. At a high level the
1043 // resizePending state is to avoid applying the state of the new buffer
1044 // to the old buffer. However in the state where we don't have an old buffer
1045 // there is no such concern but we may still be being used as a parent layer.
David Sodman41fdfc92017-11-06 16:09:56 -08001046 const bool resizePending = ((c.requested.w != c.active.w) || (c.requested.h != c.active.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -08001047 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001048 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -08001049 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001050 flags |= eDontUpdateGeometryState;
1051 }
1052 }
1053
Robert Carr7bf247e2017-05-18 14:02:49 -07001054 // Here we apply various requested geometry states, depending on our
1055 // latching configuration. See Layer.h for a detailed discussion of
1056 // how geometry latching is controlled.
1057 if (!(flags & eDontUpdateGeometryState)) {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001058 Layer::State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001059
1060 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1061 // mode, which causes attributes which normally latch regardless of scaling mode,
1062 // to be delayed. We copy the requested state to the active state making sure
1063 // to respect these rules (again see Layer.h for a detailed discussion).
1064 //
1065 // There is an awkward asymmetry in the handling of the crop states in the position
1066 // states, as can be seen below. Largely this arises from position and transform
1067 // being stored in the same data structure while having different latching rules.
1068 // b/38182305
1069 //
1070 // Careful that "c" and editCurrentState may not begin as equivalent due to
1071 // applyPendingStates in the presence of deferred transactions.
1072 if (mFreezeGeometryUpdates) {
Robert Carr82364e32016-05-15 11:27:47 -07001073 float tx = c.active.transform.tx();
1074 float ty = c.active.transform.ty();
1075 c.active = c.requested;
1076 c.active.transform.set(tx, ty);
1077 editCurrentState.active = c.active;
1078 } else {
1079 editCurrentState.active = editCurrentState.requested;
1080 c.active = c.requested;
1081 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001082 }
1083
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001084 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001085 // invalidate and recompute the visible regions if needed
1086 flags |= Layer::eVisibleRegion;
1087 }
1088
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001089 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001090 // invalidate and recompute the visible regions if needed
1091 flags |= eVisibleRegion;
1092 this->contentDirty = true;
1093
1094 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001095 const uint8_t type = c.active.transform.getType();
David Sodman41fdfc92017-11-06 16:09:56 -08001096 mNeedsFiltering = (!c.active.transform.preserveRects() || (type >= Transform::SCALE));
Mathias Agopian13127d82013-03-05 17:47:11 -08001097 }
1098
Dan Stozac8145172016-04-28 16:29:06 -07001099 // If the layer is hidden, signal and clear out all local sync points so
1100 // that transactions for layers depending on this layer's frames becoming
1101 // visible are not blocked
1102 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001103 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001104 }
1105
Mathias Agopian13127d82013-03-05 17:47:11 -08001106 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001107 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001108 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001109}
1110
Pablo Ceballos05289c22016-04-14 15:49:55 -07001111void Layer::commitTransaction(const State& stateToCommit) {
1112 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001113}
1114
Mathias Agopian13127d82013-03-05 17:47:11 -08001115uint32_t Layer::getTransactionFlags(uint32_t flags) {
1116 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1117}
1118
1119uint32_t Layer::setTransactionFlags(uint32_t flags) {
1120 return android_atomic_or(flags, &mTransactionFlags);
1121}
1122
Robert Carr82364e32016-05-15 11:27:47 -07001123bool Layer::setPosition(float x, float y, bool immediate) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001124 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001125 return false;
1126 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001127
1128 // We update the requested and active position simultaneously because
1129 // we want to apply the position portion of the transform matrix immediately,
1130 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001131 mCurrentState.requested.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001132 if (immediate && !mFreezeGeometryUpdates) {
1133 // Here we directly update the active state
1134 // unlike other setters, because we store it within
1135 // the transform, but use different latching rules.
1136 // b/38182305
Robert Carr82364e32016-05-15 11:27:47 -07001137 mCurrentState.active.transform.set(x, y);
1138 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001139 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001140
Dan Stoza7dde5992015-05-22 09:51:44 -07001141 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001142 setTransactionFlags(eTransactionNeeded);
1143 return true;
1144}
Robert Carr82364e32016-05-15 11:27:47 -07001145
Robert Carr1f0a16a2016-10-24 16:27:39 -07001146bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1147 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1148 if (idx < 0) {
1149 return false;
1150 }
1151 if (childLayer->setLayer(z)) {
1152 mCurrentChildren.removeAt(idx);
1153 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001154 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001155 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001156 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001157}
1158
Robert Carr503c7042017-09-27 15:06:08 -07001159bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1160 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1161 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1162 if (idx < 0) {
1163 return false;
1164 }
1165 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1166 mCurrentChildren.removeAt(idx);
1167 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001168 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001169 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001170 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001171}
1172
Robert Carrae060832016-11-28 10:51:00 -08001173bool Layer::setLayer(int32_t z) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001174 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001175 mCurrentState.sequence++;
1176 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001177 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001178
1179 // Discard all relative layering.
1180 if (mCurrentState.zOrderRelativeOf != nullptr) {
1181 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1182 if (strongRelative != nullptr) {
1183 strongRelative->removeZOrderRelative(this);
1184 }
1185 mCurrentState.zOrderRelativeOf = nullptr;
1186 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001187 setTransactionFlags(eTransactionNeeded);
1188 return true;
1189}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001190
Robert Carrdb66e622017-04-10 16:55:57 -07001191void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1192 mCurrentState.zOrderRelatives.remove(relative);
1193 mCurrentState.sequence++;
1194 mCurrentState.modified = true;
1195 setTransactionFlags(eTransactionNeeded);
1196}
1197
1198void Layer::addZOrderRelative(const wp<Layer>& relative) {
1199 mCurrentState.zOrderRelatives.add(relative);
1200 mCurrentState.modified = true;
1201 mCurrentState.sequence++;
1202 setTransactionFlags(eTransactionNeeded);
1203}
1204
Robert Carr503d2bd2017-12-04 15:49:47 -08001205bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001206 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1207 if (handle == nullptr) {
1208 return false;
1209 }
1210 sp<Layer> relative = handle->owner.promote();
1211 if (relative == nullptr) {
1212 return false;
1213 }
1214
Robert Carr503d2bd2017-12-04 15:49:47 -08001215 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1216 mCurrentState.zOrderRelativeOf == relative) {
1217 return false;
1218 }
1219
Robert Carrdb66e622017-04-10 16:55:57 -07001220 mCurrentState.sequence++;
1221 mCurrentState.modified = true;
Robert Carr503d2bd2017-12-04 15:49:47 -08001222 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001223
chaviw9ab4bd12017-11-03 13:11:00 -07001224 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1225 if (oldZOrderRelativeOf != nullptr) {
1226 oldZOrderRelativeOf->removeZOrderRelative(this);
1227 }
Robert Carrdb66e622017-04-10 16:55:57 -07001228 mCurrentState.zOrderRelativeOf = relative;
1229 relative->addZOrderRelative(this);
1230
1231 setTransactionFlags(eTransactionNeeded);
1232
1233 return true;
1234}
1235
Mathias Agopian13127d82013-03-05 17:47:11 -08001236bool Layer::setSize(uint32_t w, uint32_t h) {
David Sodman41fdfc92017-11-06 16:09:56 -08001237 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001238 mCurrentState.requested.w = w;
1239 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001240 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001241 setTransactionFlags(eTransactionNeeded);
1242 return true;
1243}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001244bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001245 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001246 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001247 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001248 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001249 setTransactionFlags(eTransactionNeeded);
1250 return true;
1251}
chaviw13fdc492017-06-27 12:40:18 -07001252
1253bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001254 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1255 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001256 return false;
1257
1258 mCurrentState.sequence++;
1259 mCurrentState.color.r = color.r;
1260 mCurrentState.color.g = color.g;
1261 mCurrentState.color.b = color.b;
1262 mCurrentState.modified = true;
1263 setTransactionFlags(eTransactionNeeded);
1264 return true;
1265}
1266
Robert Carrd4ae7f32018-06-07 16:10:57 -07001267bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1268 bool allowNonRectPreservingTransforms) {
1269 Transform t;
1270 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1271
1272 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1273 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1274 return false;
1275 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001276 mCurrentState.sequence++;
David Sodman41fdfc92017-11-06 16:09:56 -08001277 mCurrentState.requested.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001278 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001279 setTransactionFlags(eTransactionNeeded);
1280 return true;
1281}
1282bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001283 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001284 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001285 setTransactionFlags(eTransactionNeeded);
1286 return true;
1287}
1288bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1289 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001290 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001291 mCurrentState.sequence++;
1292 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001293 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001294 setTransactionFlags(eTransactionNeeded);
1295 return true;
1296}
Robert Carr99e27f02016-06-16 15:18:02 -07001297
1298bool Layer::setCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001299 if (mCurrentState.requestedCrop == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001300 mCurrentState.sequence++;
Robert Carr99e27f02016-06-16 15:18:02 -07001301 mCurrentState.requestedCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001302 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr99e27f02016-06-16 15:18:02 -07001303 mCurrentState.crop = crop;
1304 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001305 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1306
Dan Stoza7dde5992015-05-22 09:51:44 -07001307 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001308 setTransactionFlags(eTransactionNeeded);
1309 return true;
1310}
Robert Carr8d5227b2017-03-16 15:41:03 -07001311
1312bool Layer::setFinalCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001313 if (mCurrentState.requestedFinalCrop == crop) return false;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001314 mCurrentState.sequence++;
Robert Carr8d5227b2017-03-16 15:41:03 -07001315 mCurrentState.requestedFinalCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001316 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001317 mCurrentState.finalCrop = crop;
1318 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001319 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1320
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001321 mCurrentState.modified = true;
1322 setTransactionFlags(eTransactionNeeded);
1323 return true;
1324}
Mathias Agopian13127d82013-03-05 17:47:11 -08001325
Robert Carrc3574f72016-03-24 12:19:32 -07001326bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001327 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001328 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001329 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001330 return true;
1331}
1332
rongliucfb187b2018-03-14 12:26:23 -07001333void Layer::setInfo(int32_t type, int32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001334 mCurrentState.appId = appId;
1335 mCurrentState.type = type;
1336 mCurrentState.modified = true;
1337 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001338}
1339
Mathias Agopian13127d82013-03-05 17:47:11 -08001340bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001341 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001342 mCurrentState.sequence++;
1343 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001344 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001345 setTransactionFlags(eTransactionNeeded);
1346 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001347}
1348
Robert Carr1f0a16a2016-10-24 16:27:39 -07001349uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001350 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001351 if (p == nullptr) {
1352 return getDrawingState().layerStack;
1353 }
1354 return p->getLayerStack();
1355}
1356
David Sodman41fdfc92017-11-06 16:09:56 -08001357void Layer::deferTransactionUntil(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001358 mCurrentState.barrierLayer = barrierLayer;
Dan Stoza7dde5992015-05-22 09:51:44 -07001359 mCurrentState.frameNumber = frameNumber;
1360 // We don't set eTransactionNeeded, because just receiving a deferral
1361 // request without any other state updates shouldn't actually induce a delay
1362 mCurrentState.modified = true;
1363 pushPendingState();
Robert Carr0d480722017-01-10 16:42:54 -08001364 mCurrentState.barrierLayer = nullptr;
Dan Stoza792e5292016-02-11 11:43:58 -08001365 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001366 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001367}
1368
David Sodman41fdfc92017-11-06 16:09:56 -08001369void Layer::deferTransactionUntil(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001370 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
1371 deferTransactionUntil(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001372}
1373
Dan Stozaee44edd2015-03-23 15:50:23 -07001374
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001375// ----------------------------------------------------------------------------
1376// pageflip handling...
1377// ----------------------------------------------------------------------------
1378
Robert Carr1f0a16a2016-10-24 16:27:39 -07001379bool Layer::isHiddenByPolicy() const {
1380 const Layer::State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001381 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001382 if (parent != nullptr && parent->isHiddenByPolicy()) {
1383 return true;
1384 }
1385 return s.flags & layer_state_t::eLayerHidden;
1386}
1387
David Sodman41fdfc92017-11-06 16:09:56 -08001388uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001389 // TODO: should we do something special if mSecure is set?
1390 if (mProtectedByApp) {
1391 // need a hardware-protected path to external video sink
1392 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001393 }
Riley Andrews03414a12014-07-01 14:22:59 -07001394 if (mPotentialCursor) {
1395 usage |= GraphicBuffer::USAGE_CURSOR;
1396 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001397 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001398 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001399}
1400
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001401void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001402 uint32_t orientation = 0;
1403 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001404 // The transform hint is used to improve performance, but we can
1405 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001406 // apply to all displays.
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001407 const Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001408 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07001409 if (orientation & Transform::ROT_INVALID) {
1410 orientation = 0;
1411 }
1412 }
David Sodmaneb085e02017-10-05 18:49:04 -07001413 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001414}
1415
Mathias Agopian13127d82013-03-05 17:47:11 -08001416// ----------------------------------------------------------------------------
1417// debugging
1418// ----------------------------------------------------------------------------
1419
Kalle Raitaa099a242017-01-11 11:17:29 -08001420LayerDebugInfo Layer::getLayerDebugInfo() const {
1421 LayerDebugInfo info;
1422 const Layer::State& ds = getDrawingState();
1423 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001424 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001425 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1426 info.mType = String8(getTypeId());
1427 info.mTransparentRegion = ds.activeTransparentRegion;
1428 info.mVisibleRegion = visibleRegion;
1429 info.mSurfaceDamageRegion = surfaceDamageRegion;
1430 info.mLayerStack = getLayerStack();
1431 info.mX = ds.active.transform.tx();
1432 info.mY = ds.active.transform.ty();
1433 info.mZ = ds.z;
1434 info.mWidth = ds.active.w;
1435 info.mHeight = ds.active.h;
1436 info.mCrop = ds.crop;
1437 info.mFinalCrop = ds.finalCrop;
chaviw13fdc492017-06-27 12:40:18 -07001438 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001439 info.mFlags = ds.flags;
1440 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001441 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Kalle Raitaa099a242017-01-11 11:17:29 -08001442 info.mMatrix[0][0] = ds.active.transform[0][0];
1443 info.mMatrix[0][1] = ds.active.transform[0][1];
1444 info.mMatrix[1][0] = ds.active.transform[1][0];
1445 info.mMatrix[1][1] = ds.active.transform[1][1];
1446 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001447 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001448 if (buffer != 0) {
1449 info.mActiveBufferWidth = buffer->getWidth();
1450 info.mActiveBufferHeight = buffer->getHeight();
1451 info.mActiveBufferStride = buffer->getStride();
1452 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001453 } else {
1454 info.mActiveBufferWidth = 0;
1455 info.mActiveBufferHeight = 0;
1456 info.mActiveBufferStride = 0;
1457 info.mActiveBufferFormat = 0;
1458 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001459 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001460 info.mNumQueuedFrames = getQueuedFrameCount();
1461 info.mRefreshPending = isBufferLatched();
1462 info.mIsOpaque = isOpaque(ds);
1463 info.mContentDirty = contentDirty;
1464 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001465}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001466
Dan Stozae22aec72016-08-01 13:20:59 -07001467void Layer::miniDumpHeader(String8& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001468 result.append("-------------------------------");
1469 result.append("-------------------------------");
1470 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001471 result.append(" Layer name\n");
1472 result.append(" Z | ");
1473 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001474 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001475 result.append(" Disp Frame (LTRB) | ");
1476 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001477 result.append("-------------------------------");
1478 result.append("-------------------------------");
1479 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001480}
1481
Dominik Laskowski7e045462018-05-30 13:02:02 -07001482void Layer::miniDump(String8& result, int32_t displayId) const {
1483 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stozae22aec72016-08-01 13:20:59 -07001484 return;
1485 }
1486
1487 String8 name;
1488 if (mName.length() > 77) {
1489 std::string shortened;
1490 shortened.append(mName.string(), 36);
1491 shortened.append("[...]");
1492 shortened.append(mName.string() + (mName.length() - 36), 36);
1493 name = shortened.c_str();
1494 } else {
1495 name = mName;
1496 }
1497
1498 result.appendFormat(" %s\n", name.string());
1499
1500 const Layer::State& layerState(getDrawingState());
Lloyd Pique074e8122018-07-26 12:57:23 -07001501 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(displayId);
Chia-I Wu1e043612018-03-01 09:45:09 -08001502 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
1503 result.appendFormat(" rel %6d | ", layerState.z);
1504 } else {
1505 result.appendFormat(" %10d | ", layerState.z);
1506 }
Dominik Laskowski7e045462018-05-30 13:02:02 -07001507 result.appendFormat("%10s | ", to_string(getCompositionType(displayId)).c_str());
Lloyd Pique074e8122018-07-26 12:57:23 -07001508 result.appendFormat("%10s | ", to_string(hwcInfo.transform).c_str());
1509 const Rect& frame = hwcInfo.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001510 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Lloyd Pique074e8122018-07-26 12:57:23 -07001511 const FloatRect& crop = hwcInfo.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001512 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 -07001513
Yichi Chen6ca35192018-05-29 12:20:43 +08001514 result.append("- - - - - - - - - - - - - - - -");
1515 result.append("- - - - - - - - - - - - - - - -");
1516 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001517}
Dan Stozae22aec72016-08-01 13:20:59 -07001518
Svetoslavd85084b2014-03-20 10:28:31 -07001519void Layer::dumpFrameStats(String8& result) const {
1520 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001521}
1522
Svetoslavd85084b2014-03-20 10:28:31 -07001523void Layer::clearFrameStats() {
1524 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001525}
1526
Jamie Gennis6547ff42013-07-16 20:12:42 -07001527void Layer::logFrameStats() {
1528 mFrameTracker.logAndResetStats(mName);
1529}
1530
Svetoslavd85084b2014-03-20 10:28:31 -07001531void Layer::getFrameStats(FrameStats* outStats) const {
1532 mFrameTracker.getStats(outStats);
1533}
1534
Brian Andersond6927fb2016-07-23 23:37:30 -07001535void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001536 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001537 Mutex::Autolock lock(mFrameEventHistoryMutex);
1538 mFrameEventHistory.checkFencesForCompletion();
1539 mFrameEventHistory.dump(result);
1540}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001541
Brian Anderson5ea5e592016-12-01 16:54:33 -08001542void Layer::onDisconnect() {
1543 Mutex::Autolock lock(mFrameEventHistoryMutex);
1544 mFrameEventHistory.onDisconnect();
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001545 mTimeStats.onDisconnect(getName().c_str());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001546}
1547
Brian Anderson3890c392016-07-25 12:48:08 -07001548void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001549 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001550 if (newTimestamps) {
1551 mTimeStats.setPostTime(getName().c_str(), newTimestamps->frameNumber,
1552 newTimestamps->postedTime);
1553 }
1554
Brian Andersond6927fb2016-07-23 23:37:30 -07001555 Mutex::Autolock lock(mFrameEventHistoryMutex);
1556 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001557 // If there are any unsignaled fences in the aquire timeline at this
1558 // point, the previously queued frame hasn't been latched yet. Go ahead
1559 // and try to get the signal time here so the syscall is taken out of
1560 // the main thread's critical path.
1561 mAcquireTimeline.updateSignalTimes();
1562 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001563 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001564 mFrameEventHistory.addQueue(*newTimestamps);
1565 }
1566
Brian Anderson3890c392016-07-25 12:48:08 -07001567 if (outDelta) {
1568 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001569 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001570}
Dan Stozae77c7662016-05-13 11:37:28 -07001571
Chia-I Wu98f1c102017-05-30 14:54:08 -07001572size_t Layer::getChildrenCount() const {
1573 size_t count = 0;
1574 for (const sp<Layer>& child : mCurrentChildren) {
1575 count += 1 + child->getChildrenCount();
1576 }
1577 return count;
1578}
1579
Robert Carr1f0a16a2016-10-24 16:27:39 -07001580void Layer::addChild(const sp<Layer>& layer) {
1581 mCurrentChildren.add(layer);
1582 layer->setParent(this);
1583}
1584
1585ssize_t Layer::removeChild(const sp<Layer>& layer) {
1586 layer->setParent(nullptr);
1587 return mCurrentChildren.remove(layer);
1588}
1589
Robert Carr1db73f62016-12-21 12:58:51 -08001590bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1591 sp<Handle> handle = nullptr;
1592 sp<Layer> newParent = nullptr;
1593 if (newParentHandle == nullptr) {
1594 return false;
1595 }
1596 handle = static_cast<Handle*>(newParentHandle.get());
1597 newParent = handle->owner.promote();
1598 if (newParent == nullptr) {
1599 ALOGE("Unable to promote Layer handle");
1600 return false;
1601 }
1602
1603 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001604 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001605
1606 sp<Client> client(child->mClientRef.promote());
1607 if (client != nullptr) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001608 client->updateParent(newParent);
Robert Carr1db73f62016-12-21 12:58:51 -08001609 }
1610 }
1611 mCurrentChildren.clear();
1612
1613 return true;
1614}
1615
Robert Carr15eae092018-03-23 13:43:53 -07001616void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001617 for (const sp<Layer>& child : mDrawingChildren) {
1618 child->mDrawingParent = newParent;
1619 }
1620}
1621
chaviwf1961f72017-09-18 16:41:07 -07001622bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1623 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001624 return false;
1625 }
1626
1627 auto handle = static_cast<Handle*>(newParentHandle.get());
1628 sp<Layer> newParent = handle->owner.promote();
1629 if (newParent == nullptr) {
1630 ALOGE("Unable to promote Layer handle");
1631 return false;
1632 }
1633
chaviwf1961f72017-09-18 16:41:07 -07001634 sp<Layer> parent = getParent();
1635 if (parent != nullptr) {
1636 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001637 }
chaviwf1961f72017-09-18 16:41:07 -07001638 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001639
chaviwf1961f72017-09-18 16:41:07 -07001640 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001641 sp<Client> newParentClient(newParent->mClientRef.promote());
1642
chaviwf1961f72017-09-18 16:41:07 -07001643 if (client != newParentClient) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001644 client->updateParent(newParent);
chaviw06178942017-07-27 10:25:59 -07001645 }
1646
chaviw06178942017-07-27 10:25:59 -07001647 return true;
1648}
1649
Robert Carr9524cb32017-02-13 11:32:32 -08001650bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001651 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001652 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001653 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001654 if (client != nullptr && parentClient != client) {
Robert Carr7f619b22017-11-06 12:56:35 -08001655 client->detachLayer(child.get());
1656 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001657 }
Robert Carr7f619b22017-11-06 12:56:35 -08001658 }
Robert Carr9524cb32017-02-13 11:32:32 -08001659
1660 return true;
1661}
1662
Chia-I Wu11481472018-05-04 10:43:19 -07001663bool Layer::isLegacyDataSpace() const {
1664 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001665 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001666 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001667}
1668
Robert Carr1f0a16a2016-10-24 16:27:39 -07001669void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001670 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001671}
1672
1673void Layer::clearSyncPoints() {
1674 for (const auto& child : mCurrentChildren) {
1675 child->clearSyncPoints();
1676 }
1677
1678 Mutex::Autolock lock(mLocalSyncPointMutex);
1679 for (auto& point : mLocalSyncPoints) {
1680 point->setFrameAvailable();
1681 }
1682 mLocalSyncPoints.clear();
1683}
1684
1685int32_t Layer::getZ() const {
1686 return mDrawingState.z;
1687}
1688
Robert Carr29abff82017-12-04 13:51:20 -08001689bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1690 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1691 const State& state = useDrawing ? mDrawingState : mCurrentState;
1692 return state.zOrderRelativeOf != nullptr;
1693}
1694
David Sodman41fdfc92017-11-06 16:09:56 -08001695__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001696 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001697 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1698 "makeTraversalList received invalid stateSet");
1699 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1700 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1701 const State& state = useDrawing ? mDrawingState : mCurrentState;
1702
Robert Carr29abff82017-12-04 13:51:20 -08001703 if (state.zOrderRelatives.size() == 0) {
1704 *outSkipRelativeZUsers = true;
1705 return children;
1706 }
1707
chaviwfd462612018-05-31 16:11:27 -07001708 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001709 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001710 sp<Layer> strongRelative = weakRelative.promote();
1711 if (strongRelative != nullptr) {
1712 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001713 }
1714 }
1715
Dan Stoza412903f2017-04-27 13:42:17 -07001716 for (const sp<Layer>& child : children) {
Robert Carr503c7042017-09-27 15:06:08 -07001717 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1718 if (childState.zOrderRelativeOf != nullptr) {
1719 continue;
1720 }
Robert Carrdb66e622017-04-10 16:55:57 -07001721 traverse.add(child);
1722 }
1723
1724 return traverse;
1725}
1726
Robert Carr1f0a16a2016-10-24 16:27:39 -07001727/**
Robert Carrdb66e622017-04-10 16:55:57 -07001728 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001729 */
Dan Stoza412903f2017-04-27 13:42:17 -07001730void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001731 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1732 // produce a new list for traversing, including our relatives, and not including our children
1733 // who are relatives of another surface. In the case that there are no relative Z,
1734 // makeTraversalList returns our children directly to avoid significant overhead.
1735 // However in this case we need to take the responsibility for filtering children which
1736 // are relatives of another surface here.
1737 bool skipRelativeZUsers = false;
1738 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001739
Robert Carr1f0a16a2016-10-24 16:27:39 -07001740 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001741 for (; i < list.size(); i++) {
1742 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001743 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1744 continue;
1745 }
1746
Robert Carrdb66e622017-04-10 16:55:57 -07001747 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001748 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001749 }
Dan Stoza412903f2017-04-27 13:42:17 -07001750 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001751 }
Robert Carr29abff82017-12-04 13:51:20 -08001752
Dan Stoza412903f2017-04-27 13:42:17 -07001753 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001754 for (; i < list.size(); i++) {
1755 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001756
1757 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1758 continue;
1759 }
Dan Stoza412903f2017-04-27 13:42:17 -07001760 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001761 }
1762}
1763
1764/**
Robert Carrdb66e622017-04-10 16:55:57 -07001765 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001766 */
Dan Stoza412903f2017-04-27 13:42:17 -07001767void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1768 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001769 // See traverseInZOrder for documentation.
1770 bool skipRelativeZUsers = false;
1771 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001772
Robert Carr1f0a16a2016-10-24 16:27:39 -07001773 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001774 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001775 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001776
1777 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1778 continue;
1779 }
1780
Robert Carrdb66e622017-04-10 16:55:57 -07001781 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001782 break;
1783 }
Dan Stoza412903f2017-04-27 13:42:17 -07001784 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001785 }
Dan Stoza412903f2017-04-27 13:42:17 -07001786 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001787 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001788 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001789
1790 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1791 continue;
1792 }
1793
Dan Stoza412903f2017-04-27 13:42:17 -07001794 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001795 }
1796}
1797
chaviw4b129c22018-04-09 16:19:43 -07001798LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1799 const std::vector<Layer*>& layersInTree) {
1800 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1801 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001802 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1803 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
chaviw4b129c22018-04-09 16:19:43 -07001804 const State& state = useDrawing ? mDrawingState : mCurrentState;
1805
chaviwfd462612018-05-31 16:11:27 -07001806 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001807 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1808 sp<Layer> strongRelative = weakRelative.promote();
1809 // Only add relative layers that are also descendents of the top most parent of the tree.
1810 // If a relative layer is not a descendent, then it should be ignored.
1811 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1812 traverse.add(strongRelative);
1813 }
1814 }
1815
1816 for (const sp<Layer>& child : children) {
1817 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1818 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1819 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1820 // the child here since it won't be added later as a relative.
1821 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1822 childState.zOrderRelativeOf.promote().get())) {
1823 continue;
1824 }
1825 traverse.add(child);
1826 }
1827
1828 return traverse;
1829}
1830
1831void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1832 LayerVector::StateSet stateSet,
1833 const LayerVector::Visitor& visitor) {
1834 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001835
1836 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001837 for (; i < list.size(); i++) {
1838 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001839 if (relative->getZ() >= 0) {
1840 break;
1841 }
chaviw4b129c22018-04-09 16:19:43 -07001842 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001843 }
chaviw4b129c22018-04-09 16:19:43 -07001844
chaviwa76b2712017-09-20 12:02:26 -07001845 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001846 for (; i < list.size(); i++) {
1847 const auto& relative = list[i];
1848 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001849 }
1850}
1851
chaviw4b129c22018-04-09 16:19:43 -07001852std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1853 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1854 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1855
1856 std::vector<Layer*> layersInTree = {this};
1857 for (size_t i = 0; i < children.size(); i++) {
1858 const auto& child = children[i];
1859 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1860 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1861 }
1862
1863 return layersInTree;
1864}
1865
1866void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1867 const LayerVector::Visitor& visitor) {
1868 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1869 std::sort(layersInTree.begin(), layersInTree.end());
1870 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1871}
1872
Robert Carr1f0a16a2016-10-24 16:27:39 -07001873Transform Layer::getTransform() const {
1874 Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001875 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001876 if (p != nullptr) {
1877 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001878
1879 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1880 // it isFixedSize) then there may be additional scaling not accounted
1881 // for in the transform. We need to mirror this scaling in child surfaces
1882 // or we will break the contract where WM can treat child surfaces as
1883 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08001884 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001885 int bufferWidth;
1886 int bufferHeight;
1887 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08001888 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
1889 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001890 } else {
David Sodman0cc69182017-11-17 12:12:07 -08001891 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
1892 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001893 }
David Sodman41fdfc92017-11-06 16:09:56 -08001894 float sx = p->getDrawingState().active.w / static_cast<float>(bufferWidth);
1895 float sy = p->getDrawingState().active.h / static_cast<float>(bufferHeight);
Robert Carr9b429f42017-04-17 14:56:57 -07001896 Transform extraParentScaling;
1897 extraParentScaling.set(sx, 0, 0, sy);
1898 t = t * extraParentScaling;
1899 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001900 }
1901 return t * getDrawingState().active.transform;
1902}
1903
chaviw13fdc492017-06-27 12:40:18 -07001904half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001905 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001906
chaviw13fdc492017-06-27 12:40:18 -07001907 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1908 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001909}
Robert Carr6452f122017-03-21 10:41:29 -07001910
chaviw13fdc492017-06-27 12:40:18 -07001911half4 Layer::getColor() const {
1912 const half4 color(getDrawingState().color);
1913 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001914}
Robert Carr6452f122017-03-21 10:41:29 -07001915
Robert Carr1f0a16a2016-10-24 16:27:39 -07001916void Layer::commitChildList() {
1917 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1918 const auto& child = mCurrentChildren[i];
1919 child->commitChildList();
1920 }
1921 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001922 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001923}
1924
chaviw1d044282017-09-27 12:19:28 -07001925void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1926 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1927 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1928 const State& state = useDrawing ? mDrawingState : mCurrentState;
1929
1930 Transform requestedTransform = state.active.transform;
1931 Transform transform = getTransform();
1932
1933 layerInfo->set_id(sequence);
1934 layerInfo->set_name(getName().c_str());
1935 layerInfo->set_type(String8(getTypeId()));
1936
1937 for (const auto& child : children) {
1938 layerInfo->add_children(child->sequence);
1939 }
1940
1941 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1942 sp<Layer> strongRelative = weakRelative.promote();
1943 if (strongRelative != nullptr) {
1944 layerInfo->add_relatives(strongRelative->sequence);
1945 }
1946 }
1947
1948 LayerProtoHelper::writeToProto(state.activeTransparentRegion,
1949 layerInfo->mutable_transparent_region());
1950 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
1951 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
1952
1953 layerInfo->set_layer_stack(getLayerStack());
1954 layerInfo->set_z(state.z);
1955
1956 PositionProto* position = layerInfo->mutable_position();
1957 position->set_x(transform.tx());
1958 position->set_y(transform.ty());
1959
1960 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
1961 requestedPosition->set_x(requestedTransform.tx());
1962 requestedPosition->set_y(requestedTransform.ty());
1963
1964 SizeProto* size = layerInfo->mutable_size();
1965 size->set_w(state.active.w);
1966 size->set_h(state.active.h);
1967
1968 LayerProtoHelper::writeToProto(state.crop, layerInfo->mutable_crop());
1969 LayerProtoHelper::writeToProto(state.finalCrop, layerInfo->mutable_final_crop());
1970
1971 layerInfo->set_is_opaque(isOpaque(state));
1972 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07001973
1974 // XXX (b/79210409) mCurrentDataSpace is not protected
1975 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1976
chaviw1d044282017-09-27 12:19:28 -07001977 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1978 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
1979 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
1980 layerInfo->set_flags(state.flags);
1981
1982 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1983 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
1984
Jorim Jaggi8e0af362017-11-14 16:28:28 +01001985 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07001986 if (parent != nullptr) {
1987 layerInfo->set_parent(parent->sequence);
1988 }
1989
1990 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1991 if (zOrderRelativeOf != nullptr) {
1992 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1993 }
1994
Chia-I Wu01591c92018-05-22 12:03:00 -07001995 // XXX getBE().compositionInfo.mBuffer is not protected
David Sodman0cc69182017-11-17 12:12:07 -08001996 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001997 if (buffer != nullptr) {
1998 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
Yichi Chen6ca35192018-05-29 12:20:43 +08001999 LayerProtoHelper::writeToProto(Transform(mCurrentTransform),
2000 layerInfo->mutable_buffer_transform());
chaviw1d044282017-09-27 12:19:28 -07002001 }
2002
2003 layerInfo->set_queued_frames(getQueuedFrameCount());
2004 layerInfo->set_refresh_pending(isBufferLatched());
rongliucfb187b2018-03-14 12:26:23 -07002005 layerInfo->set_window_type(state.type);
2006 layerInfo->set_app_id(state.appId);
chaviwadc40c22018-07-10 16:57:27 -07002007 layerInfo->set_curr_frame(mCurrentFrameNumber);
2008
2009 for (const auto& pendingState : mPendingStates) {
2010 auto barrierLayer = pendingState.barrierLayer.promote();
2011 if (barrierLayer != nullptr) {
2012 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
2013 barrierLayerProto->set_id(barrierLayer->sequence);
2014 barrierLayerProto->set_frame_number(pendingState.frameNumber);
2015 }
2016 }
chaviw1d044282017-09-27 12:19:28 -07002017}
2018
Dominik Laskowski7e045462018-05-30 13:02:02 -07002019void Layer::writeToProto(LayerProto* layerInfo, int32_t displayId) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07002020 if (!hasHwcLayer(displayId)) {
2021 return;
2022 }
2023
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002024 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
2025
Dominik Laskowski7e045462018-05-30 13:02:02 -07002026 const auto& hwcInfo = getBE().mHwcLayers.at(displayId);
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002027
2028 const Rect& frame = hwcInfo.displayFrame;
2029 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
2030
2031 const FloatRect& crop = hwcInfo.sourceCrop;
2032 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
2033
2034 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
2035 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002036
2037 const int32_t compositionType = static_cast<int32_t>(hwcInfo.compositionType);
2038 layerInfo->set_hwc_composition_type(compositionType);
2039
2040 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2041 static_cast<BufferLayer*>(this)->isProtected()) {
2042 layerInfo->set_is_protected(true);
2043 } else {
2044 layerInfo->set_is_protected(false);
2045 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002046}
2047
Mathias Agopian13127d82013-03-05 17:47:11 -08002048// ---------------------------------------------------------------------------
2049
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002050}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002051
2052#if defined(__gl_h_)
2053#error "don't include gl/gl.h in this file"
2054#endif
2055
2056#if defined(__gl2_h_)
2057#error "don't include gl2/gl2.h in this file"
2058#endif