blob: 4eedb1a13da0af58137500b5a9a3c00ec7b1a099 [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
David Sodman0756cdd2018-04-13 11:26:33 -0700506 // Need to program geometry parts
507 getBE().compositionInfo.hwc.skipGeometry = false;
508
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700509 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800510 hwcInfo.forceClientComposition = false;
511
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700512 if (isSecure() && !display->isSecure()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800513 hwcInfo.forceClientComposition = true;
514 }
515
Mathias Agopian13127d82013-03-05 17:47:11 -0800516 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700517 const State& s(getDrawingState());
David Revemanecf0fa52017-03-03 11:32:44 -0500518 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700519 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800520 blendMode =
521 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800522 }
David Sodman0756cdd2018-04-13 11:26:33 -0700523 getBE().compositionInfo.hwc.blendMode = blendMode;
Mathias Agopian13127d82013-03-05 17:47:11 -0800524
525 // apply the layer's transform, followed by the display's global transform
526 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700527 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700528 Transform t = getTransform();
Robert Carrb5d3d262016-03-25 15:08:13 -0700529 if (!s.crop.isEmpty()) {
530 Rect activeCrop(s.crop);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700531 activeCrop = t.transform(activeCrop);
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700532 if (!activeCrop.intersect(display->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000533 activeCrop.clear();
534 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700535 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800536 // This needs to be here as transform.transform(Rect) computes the
537 // transformed rect and then takes the bounding box of the result before
538 // returning. This means
539 // transform.inverse().transform(transform.transform(Rect)) != Rect
540 // in which case we need to make sure the final rect is clipped to the
541 // display bounds.
David Sodman41fdfc92017-11-06 16:09:56 -0800542 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000543 activeCrop.clear();
544 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700545 // mark regions outside the crop as transparent
546 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
David Sodman41fdfc92017-11-06 16:09:56 -0800547 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom, s.active.w, s.active.h));
548 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
549 activeTransparentRegion.orSelf(
550 Rect(activeCrop.right, activeCrop.top, s.active.w, activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700551 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700552
Dan Stoza80d61162017-12-20 15:57:52 -0800553 // computeBounds returns a FloatRect to provide more accuracy during the
554 // transformation. We then round upon constructing 'frame'.
555 Rect frame{t.transform(computeBounds(activeTransparentRegion))};
Robert Carrb5d3d262016-03-25 15:08:13 -0700556 if (!s.finalCrop.isEmpty()) {
David Sodman41fdfc92017-11-06 16:09:56 -0800557 if (!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000558 frame.clear();
559 }
560 }
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700561 if (!frame.intersect(display->getViewport(), &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000562 frame.clear();
563 }
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700564 const Transform& tr = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800565 Rect transformedFrame = tr.transform(frame);
David Sodman0756cdd2018-04-13 11:26:33 -0700566 getBE().compositionInfo.hwc.displayFrame = transformedFrame;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800567
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700568 FloatRect sourceCrop = computeCrop(display);
David Sodman0756cdd2018-04-13 11:26:33 -0700569 getBE().compositionInfo.hwc.sourceCrop = sourceCrop;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800570
chaviw13fdc492017-06-27 12:40:18 -0700571 float alpha = static_cast<float>(getAlpha());
David Sodman0756cdd2018-04-13 11:26:33 -0700572 getBE().compositionInfo.hwc.alpha = alpha;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800573
David Sodman0756cdd2018-04-13 11:26:33 -0700574 getBE().compositionInfo.hwc.z = z;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500575
Albert Chaulk2a589632017-05-04 16:59:44 -0400576 int type = s.type;
577 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700578 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400579 if (parent.get()) {
580 auto& parentState = parent->getDrawingState();
rongliucfb187b2018-03-14 12:26:23 -0700581 if (parentState.type >= 0 || parentState.appId >= 0) {
582 type = parentState.type;
583 appId = parentState.appId;
584 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400585 }
586
David Sodman0756cdd2018-04-13 11:26:33 -0700587 getBE().compositionInfo.hwc.type = type;
588 getBE().compositionInfo.hwc.appId = appId;
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800589
Mathias Agopian29a367b2011-07-12 14:51:45 -0700590 /*
591 * Transformations are applied in this order:
592 * 1) buffer orientation/flip/mirror
593 * 2) state transformation (window manager)
594 * 3) layer orientation (screen orientation)
595 * (NOTE: the matrices are multiplied in reverse order)
596 */
597
598 const Transform bufferOrientation(mCurrentTransform);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700599 Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700600
Robert Carrcae605c2017-03-29 12:10:31 -0700601 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700602 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700603 * the code below applies the primary display's inverse transform to the
604 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700605 */
David Sodman41fdfc92017-11-06 16:09:56 -0800606 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700607 // calculate the inverse transform
608 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800609 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700610 }
Robert Carrcae605c2017-03-29 12:10:31 -0700611
612 /*
613 * Here we cancel out the orientation component of the WM transform.
614 * The scaling and translate components are already included in our bounds
615 * computation so it's enough to just omit it in the composition.
616 * See comment in onDraw with ref to b/36727915 for why.
617 */
618 transform = Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700619 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700620
621 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800622 const uint32_t orientation = transform.getOrientation();
Jorim Jaggif3bd94a2018-03-27 15:38:03 +0200623 if (orientation & Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800624 // we can only handle simple transformation
David Sodman0756cdd2018-04-13 11:26:33 -0700625 getBE().mHwcLayers[displayId].compositionType = HWC2::Composition::Client;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800626 } else {
627 auto transform = static_cast<HWC2::Transform>(orientation);
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800628 hwcInfo.transform = transform;
David Sodman0756cdd2018-04-13 11:26:33 -0700629 getBE().compositionInfo.hwc.transform = transform;
David Sodman4b7c4bc2017-11-17 12:13:59 -0800630 }
631}
632
Dominik Laskowski7e045462018-05-30 13:02:02 -0700633void Layer::forceClientComposition(int32_t displayId) {
634 if (getBE().mHwcLayers.count(displayId) == 0) {
635 ALOGE("forceClientComposition: no HWC layer found (%d)", displayId);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800636 return;
637 }
638
Dominik Laskowski7e045462018-05-30 13:02:02 -0700639 getBE().mHwcLayers[displayId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800640}
Dan Stozaee44edd2015-03-23 15:50:23 -0700641
Dominik Laskowski7e045462018-05-30 13:02:02 -0700642bool Layer::getForceClientComposition(int32_t displayId) {
643 if (getBE().mHwcLayers.count(displayId) == 0) {
644 ALOGE("getForceClientComposition: no HWC layer found (%d)", displayId);
chaviwc9232ed2017-11-14 15:31:15 -0800645 return false;
646 }
647
Dominik Laskowski7e045462018-05-30 13:02:02 -0700648 return getBE().mHwcLayers[displayId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800649}
650
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700651void Layer::updateCursorPosition(const sp<const DisplayDevice>& display) {
Dominik Laskowski7e045462018-05-30 13:02:02 -0700652 const auto displayId = display->getId();
653 if (getBE().mHwcLayers.count(displayId) == 0 ||
654 getCompositionType(displayId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800655 return;
656 }
657
658 // This gives us only the "orientation" component of the transform
659 const State& s(getCurrentState());
660
661 // Apply the layer's transform, followed by the display's global transform
662 // Here we're guaranteed that the layer's transform preserves rects
663 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700664 if (!s.crop.isEmpty()) {
665 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800666 }
667 // Subtract the transparent region and snap to the bounds
668 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700669 Rect frame(getTransform().transform(bounds));
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700670 frame.intersect(display->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700671 if (!s.finalCrop.isEmpty()) {
672 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000673 }
Dominik Laskowskieecd6592018-05-29 10:25:41 -0700674 auto& displayTransform = display->getTransform();
Dan Stoza9e56aa02015-11-02 13:00:03 -0800675 auto position = displayTransform.transform(frame);
676
Dominik Laskowski7e045462018-05-30 13:02:02 -0700677 auto error =
678 getBE().mHwcLayers[displayId].layer->setCursorPosition(position.left, position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800679 ALOGE_IF(error != HWC2::Error::None,
680 "[%s] Failed to set cursor position "
681 "to (%d, %d): %s (%d)",
682 mName.string(), position.left, position.top, to_string(error).c_str(),
683 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800684}
Riley Andrews03414a12014-07-01 14:22:59 -0700685
Mathias Agopian13127d82013-03-05 17:47:11 -0800686// ---------------------------------------------------------------------------
687// drawing...
688// ---------------------------------------------------------------------------
689
chaviwa76b2712017-09-20 12:02:26 -0700690void Layer::draw(const RenderArea& renderArea, const Region& clip) const {
691 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800692}
693
chaviwa76b2712017-09-20 12:02:26 -0700694void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) const {
695 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800696}
697
chaviwa76b2712017-09-20 12:02:26 -0700698void Layer::draw(const RenderArea& renderArea) const {
699 onDraw(renderArea, Region(renderArea.getBounds()), false);
Dan Stozac7014012014-02-14 15:03:43 -0800700}
701
David Sodman41fdfc92017-11-06 16:09:56 -0800702void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
703 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800704 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700705 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700706 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700707 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800708}
709
chaviwa76b2712017-09-20 12:02:26 -0700710void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
David Sodman41fdfc92017-11-06 16:09:56 -0800711 clearWithOpenGL(renderArea, 0, 0, 0, 0);
Mathias Agopian13127d82013-03-05 17:47:11 -0800712}
713
Dominik Laskowski7e045462018-05-30 13:02:02 -0700714void Layer::setCompositionType(int32_t displayId, HWC2::Composition type, bool callIntoHwc) {
715 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu30505fb2018-03-26 16:20:31 -0700716 ALOGE("setCompositionType called without a valid HWC layer");
717 return;
718 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700719 auto& hwcInfo = getBE().mHwcLayers[displayId];
Chia-I Wu30505fb2018-03-26 16:20:31 -0700720 auto& hwcLayer = hwcInfo.layer;
David Sodmanf6a38932018-05-25 15:27:50 -0700721 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(), to_string(type).c_str(),
Chia-I Wu30505fb2018-03-26 16:20:31 -0700722 static_cast<int>(callIntoHwc));
723 if (hwcInfo.compositionType != type) {
724 ALOGV(" actually setting");
725 hwcInfo.compositionType = type;
726 if (callIntoHwc) {
David Sodmanf6a38932018-05-25 15:27:50 -0700727 auto error = hwcLayer->setCompositionType(type);
Chia-I Wu30505fb2018-03-26 16:20:31 -0700728 ALOGE_IF(error != HWC2::Error::None,
729 "[%s] Failed to set "
730 "composition type %s: %s (%d)",
731 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
732 static_cast<int32_t>(error));
733 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800734 }
735}
736
Dominik Laskowski7e045462018-05-30 13:02:02 -0700737HWC2::Composition Layer::getCompositionType(int32_t displayId) const {
738 if (displayId == DisplayDevice::DISPLAY_ID_INVALID) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700739 // If we're querying the composition type for a display that does not
740 // have a HWC counterpart, then it will always be Client
741 return HWC2::Composition::Client;
742 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700743 if (getBE().mHwcLayers.count(displayId) == 0) {
Chia-I Wu30505fb2018-03-26 16:20:31 -0700744 ALOGE("getCompositionType called with an invalid HWC layer");
745 return HWC2::Composition::Invalid;
746 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700747 return getBE().mHwcLayers.at(displayId).compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800748}
749
Dominik Laskowski7e045462018-05-30 13:02:02 -0700750void Layer::setClearClientTarget(int32_t displayId, bool clear) {
751 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800752 ALOGE("setClearClientTarget called without a valid HWC layer");
753 return;
754 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700755 getBE().mHwcLayers[displayId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800756}
757
Dominik Laskowski7e045462018-05-30 13:02:02 -0700758bool Layer::getClearClientTarget(int32_t displayId) const {
759 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800760 ALOGE("getClearClientTarget called without a valid HWC layer");
761 return false;
762 }
Dominik Laskowski7e045462018-05-30 13:02:02 -0700763 return getBE().mHwcLayers.at(displayId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800764}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800765
Dan Stozacac35382016-01-27 12:21:06 -0800766bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
767 if (point->getFrameNumber() <= mCurrentFrameNumber) {
768 // Don't bother with a SyncPoint, since we've already latched the
769 // relevant frame
770 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700771 }
772
Dan Stozacac35382016-01-27 12:21:06 -0800773 Mutex::Autolock lock(mLocalSyncPointMutex);
774 mLocalSyncPoints.push_back(point);
775 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700776}
777
Mathias Agopian13127d82013-03-05 17:47:11 -0800778void Layer::setFiltering(bool filtering) {
779 mFiltering = filtering;
780}
781
782bool Layer::getFiltering() const {
783 return mFiltering;
784}
785
Mathias Agopian13127d82013-03-05 17:47:11 -0800786// ----------------------------------------------------------------------------
787// local state
788// ----------------------------------------------------------------------------
789
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000790static void boundPoint(vec2* point, const Rect& crop) {
791 if (point->x < crop.left) {
792 point->x = crop.left;
793 }
794 if (point->x > crop.right) {
795 point->x = crop.right;
796 }
797 if (point->y < crop.top) {
798 point->y = crop.top;
799 }
800 if (point->y > crop.bottom) {
801 point->y = crop.bottom;
802 }
803}
804
chaviwa76b2712017-09-20 12:02:26 -0700805void Layer::computeGeometry(const RenderArea& renderArea, Mesh& mesh,
806 bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700807 const Layer::State& s(getDrawingState());
chaviwa76b2712017-09-20 12:02:26 -0700808 const Transform renderAreaTransform(renderArea.getTransform());
809 const uint32_t height = renderArea.getHeight();
Dan Stoza80d61162017-12-20 15:57:52 -0800810 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700811
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000812 vec2 lt = vec2(win.left, win.top);
813 vec2 lb = vec2(win.left, win.bottom);
814 vec2 rb = vec2(win.right, win.bottom);
815 vec2 rt = vec2(win.right, win.top);
816
Robert Carr1f0a16a2016-10-24 16:27:39 -0700817 Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000818 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700819 lt = layerTransform.transform(lt);
820 lb = layerTransform.transform(lb);
821 rb = layerTransform.transform(rb);
822 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000823 }
824
Robert Carrb5d3d262016-03-25 15:08:13 -0700825 if (!s.finalCrop.isEmpty()) {
826 boundPoint(&lt, s.finalCrop);
827 boundPoint(&lb, s.finalCrop);
828 boundPoint(&rb, s.finalCrop);
829 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000830 }
831
Mathias Agopianff2ed702013-09-01 21:36:12 -0700832 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700833 position[0] = renderAreaTransform.transform(lt);
834 position[1] = renderAreaTransform.transform(lb);
835 position[2] = renderAreaTransform.transform(rb);
836 position[3] = renderAreaTransform.transform(rt);
David Sodman41fdfc92017-11-06 16:09:56 -0800837 for (size_t i = 0; i < 4; i++) {
chaviwa76b2712017-09-20 12:02:26 -0700838 position[i].y = height - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -0800839 }
840}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800841
David Sodman41fdfc92017-11-06 16:09:56 -0800842bool Layer::isSecure() const {
Dan Stoza23116082015-06-18 14:58:39 -0700843 const Layer::State& s(mDrawingState);
844 return (s.flags & layer_state_t::eLayerSecure);
845}
846
Mathias Agopian13127d82013-03-05 17:47:11 -0800847void Layer::setVisibleRegion(const Region& visibleRegion) {
848 // always called from main thread
849 this->visibleRegion = visibleRegion;
850}
851
852void Layer::setCoveredRegion(const Region& coveredRegion) {
853 // always called from main thread
854 this->coveredRegion = coveredRegion;
855}
856
David Sodman41fdfc92017-11-06 16:09:56 -0800857void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800858 // always called from main thread
859 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
860}
861
Robert Carre5f4f692018-01-12 13:12:28 -0800862void Layer::clearVisibilityRegions() {
863 visibleRegion.clear();
864 visibleNonTransparentRegion.clear();
865 coveredRegion.clear();
866}
867
Mathias Agopian13127d82013-03-05 17:47:11 -0800868// ----------------------------------------------------------------------------
869// transaction
870// ----------------------------------------------------------------------------
871
Dan Stoza7dde5992015-05-22 09:51:44 -0700872void Layer::pushPendingState() {
873 if (!mCurrentState.modified) {
874 return;
875 }
876
Dan Stoza7dde5992015-05-22 09:51:44 -0700877 // If this transaction is waiting on the receipt of a frame, generate a sync
878 // point and send it to the remote layer.
Robert Carr0d480722017-01-10 16:42:54 -0800879 if (mCurrentState.barrierLayer != nullptr) {
880 sp<Layer> barrierLayer = mCurrentState.barrierLayer.promote();
881 if (barrierLayer == nullptr) {
882 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700883 // If we can't promote the layer we are intended to wait on,
884 // then it is expired or otherwise invalid. Allow this transaction
885 // to be applied as per normal (no synchronization).
Robert Carr0d480722017-01-10 16:42:54 -0800886 mCurrentState.barrierLayer = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800887 } else {
David Sodman41fdfc92017-11-06 16:09:56 -0800888 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber);
Robert Carr0d480722017-01-10 16:42:54 -0800889 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800890 mRemoteSyncPoints.push_back(std::move(syncPoint));
891 } else {
892 // We already missed the frame we're supposed to synchronize
893 // on, so go ahead and apply the state update
Robert Carr0d480722017-01-10 16:42:54 -0800894 mCurrentState.barrierLayer = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800895 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700896 }
897
Dan Stoza7dde5992015-05-22 09:51:44 -0700898 // Wake us up to check if the frame has been received
899 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700900 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700901 }
902 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700903 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700904}
905
Pablo Ceballos05289c22016-04-14 15:49:55 -0700906void Layer::popPendingState(State* stateToCommit) {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700907 *stateToCommit = mPendingStates[0];
Dan Stoza7dde5992015-05-22 09:51:44 -0700908
909 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700910 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700911}
912
Pablo Ceballos05289c22016-04-14 15:49:55 -0700913bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700914 bool stateUpdateAvailable = false;
915 while (!mPendingStates.empty()) {
Robert Carr0d480722017-01-10 16:42:54 -0800916 if (mPendingStates[0].barrierLayer != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700917 if (mRemoteSyncPoints.empty()) {
918 // If we don't have a sync point for this, apply it anyway. It
919 // will be visually wrong, but it should keep us from getting
920 // into too much trouble.
921 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700922 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700923 stateUpdateAvailable = true;
924 continue;
925 }
926
David Sodman41fdfc92017-11-06 16:09:56 -0800927 if (mRemoteSyncPoints.front()->getFrameNumber() != mPendingStates[0].frameNumber) {
928 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800929
930 // Signal our end of the sync point and then dispose of it
931 mRemoteSyncPoints.front()->setTransactionApplied();
932 mRemoteSyncPoints.pop_front();
933 continue;
934 }
935
Dan Stoza7dde5992015-05-22 09:51:44 -0700936 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
937 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700938 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700939 stateUpdateAvailable = true;
940
941 // Signal our end of the sync point and then dispose of it
942 mRemoteSyncPoints.front()->setTransactionApplied();
943 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800944 } else {
945 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700946 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700947 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700948 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700949 stateUpdateAvailable = true;
950 }
951 }
952
953 // If we still have pending updates, wake SurfaceFlinger back up and point
954 // it at this layer so we can process them
955 if (!mPendingStates.empty()) {
956 setTransactionFlags(eTransactionNeeded);
957 mFlinger->setTransactionFlags(eTraversalNeeded);
958 }
959
960 mCurrentState.modified = false;
961 return stateUpdateAvailable;
962}
963
Mathias Agopian13127d82013-03-05 17:47:11 -0800964uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800965 ATRACE_CALL();
966
Dan Stoza7dde5992015-05-22 09:51:44 -0700967 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -0700968 Layer::State c = getCurrentState();
969 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700970 return 0;
971 }
972
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700973 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800974
David Sodman41fdfc92017-11-06 16:09:56 -0800975 const bool sizeChanged = (c.requested.w != s.requested.w) || (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700976
David Sodmaneb085e02017-10-05 18:49:04 -0700977 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700978 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000979 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800980 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
981 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
982 " requested={ wh={%4u,%4u} }}\n"
983 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
984 " requested={ wh={%4u,%4u} }}\n",
David Sodman9eeae692017-11-02 10:53:32 -0700985 this, getName().string(), mCurrentTransform,
986 getEffectiveScalingMode(), c.active.w, c.active.h, c.crop.left, c.crop.top,
987 c.crop.right, c.crop.bottom, c.crop.getWidth(), c.crop.getHeight(), c.requested.w,
988 c.requested.h, s.active.w, s.active.h, s.crop.left, s.crop.top, s.crop.right,
989 s.crop.bottom, s.crop.getWidth(), s.crop.getHeight(), s.requested.w,
990 s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800991
Jamie Gennis2a0d5b62011-09-26 16:54:44 -0700992 // record the new size, form this point on, when the client request
993 // a buffer, it'll get the new size.
David Sodmaneb085e02017-10-05 18:49:04 -0700994 setDefaultBufferSize(c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800995 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700996
Robert Carre392b552017-09-19 12:16:05 -0700997 // Don't let Layer::doTransaction update the drawing state
998 // if we have a pending resize, unless we are in fixed-size mode.
999 // the drawing state will be updated only once we receive a buffer
1000 // with the correct size.
1001 //
1002 // In particular, we want to make sure the clip (which is part
1003 // of the geometry state) is latched together with the size but is
1004 // latched immediately when no resizing is involved.
1005 //
1006 // If a sideband stream is attached, however, we want to skip this
1007 // optimization so that transactions aren't missed when a buffer
1008 // never arrives
1009 //
1010 // In the case that we don't have a buffer we ignore other factors
1011 // and avoid entering the resizePending state. At a high level the
1012 // resizePending state is to avoid applying the state of the new buffer
1013 // to the old buffer. However in the state where we don't have an old buffer
1014 // there is no such concern but we may still be being used as a parent layer.
David Sodman41fdfc92017-11-06 16:09:56 -08001015 const bool resizePending = ((c.requested.w != c.active.w) || (c.requested.h != c.active.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -08001016 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001017 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -08001018 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001019 flags |= eDontUpdateGeometryState;
1020 }
1021 }
1022
Robert Carr7bf247e2017-05-18 14:02:49 -07001023 // Here we apply various requested geometry states, depending on our
1024 // latching configuration. See Layer.h for a detailed discussion of
1025 // how geometry latching is controlled.
1026 if (!(flags & eDontUpdateGeometryState)) {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001027 Layer::State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001028
1029 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1030 // mode, which causes attributes which normally latch regardless of scaling mode,
1031 // to be delayed. We copy the requested state to the active state making sure
1032 // to respect these rules (again see Layer.h for a detailed discussion).
1033 //
1034 // There is an awkward asymmetry in the handling of the crop states in the position
1035 // states, as can be seen below. Largely this arises from position and transform
1036 // being stored in the same data structure while having different latching rules.
1037 // b/38182305
1038 //
1039 // Careful that "c" and editCurrentState may not begin as equivalent due to
1040 // applyPendingStates in the presence of deferred transactions.
1041 if (mFreezeGeometryUpdates) {
Robert Carr82364e32016-05-15 11:27:47 -07001042 float tx = c.active.transform.tx();
1043 float ty = c.active.transform.ty();
1044 c.active = c.requested;
1045 c.active.transform.set(tx, ty);
1046 editCurrentState.active = c.active;
1047 } else {
1048 editCurrentState.active = editCurrentState.requested;
1049 c.active = c.requested;
1050 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001051 }
1052
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001053 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001054 // invalidate and recompute the visible regions if needed
1055 flags |= Layer::eVisibleRegion;
1056 }
1057
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001058 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001059 // invalidate and recompute the visible regions if needed
1060 flags |= eVisibleRegion;
1061 this->contentDirty = true;
1062
1063 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001064 const uint8_t type = c.active.transform.getType();
David Sodman41fdfc92017-11-06 16:09:56 -08001065 mNeedsFiltering = (!c.active.transform.preserveRects() || (type >= Transform::SCALE));
Mathias Agopian13127d82013-03-05 17:47:11 -08001066 }
1067
Dan Stozac8145172016-04-28 16:29:06 -07001068 // If the layer is hidden, signal and clear out all local sync points so
1069 // that transactions for layers depending on this layer's frames becoming
1070 // visible are not blocked
1071 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001072 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001073 }
1074
Mathias Agopian13127d82013-03-05 17:47:11 -08001075 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001076 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001077 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001078}
1079
Pablo Ceballos05289c22016-04-14 15:49:55 -07001080void Layer::commitTransaction(const State& stateToCommit) {
1081 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001082}
1083
Mathias Agopian13127d82013-03-05 17:47:11 -08001084uint32_t Layer::getTransactionFlags(uint32_t flags) {
1085 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1086}
1087
1088uint32_t Layer::setTransactionFlags(uint32_t flags) {
1089 return android_atomic_or(flags, &mTransactionFlags);
1090}
1091
Robert Carr82364e32016-05-15 11:27:47 -07001092bool Layer::setPosition(float x, float y, bool immediate) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001093 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001094 return false;
1095 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001096
1097 // We update the requested and active position simultaneously because
1098 // we want to apply the position portion of the transform matrix immediately,
1099 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001100 mCurrentState.requested.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001101 if (immediate && !mFreezeGeometryUpdates) {
1102 // Here we directly update the active state
1103 // unlike other setters, because we store it within
1104 // the transform, but use different latching rules.
1105 // b/38182305
Robert Carr82364e32016-05-15 11:27:47 -07001106 mCurrentState.active.transform.set(x, y);
1107 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001108 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001109
Dan Stoza7dde5992015-05-22 09:51:44 -07001110 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001111 setTransactionFlags(eTransactionNeeded);
1112 return true;
1113}
Robert Carr82364e32016-05-15 11:27:47 -07001114
Robert Carr1f0a16a2016-10-24 16:27:39 -07001115bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1116 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1117 if (idx < 0) {
1118 return false;
1119 }
1120 if (childLayer->setLayer(z)) {
1121 mCurrentChildren.removeAt(idx);
1122 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001123 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001124 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001125 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001126}
1127
Robert Carr503c7042017-09-27 15:06:08 -07001128bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1129 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1130 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1131 if (idx < 0) {
1132 return false;
1133 }
1134 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1135 mCurrentChildren.removeAt(idx);
1136 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001137 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001138 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001139 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001140}
1141
Robert Carrae060832016-11-28 10:51:00 -08001142bool Layer::setLayer(int32_t z) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001143 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001144 mCurrentState.sequence++;
1145 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001146 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001147
1148 // Discard all relative layering.
1149 if (mCurrentState.zOrderRelativeOf != nullptr) {
1150 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1151 if (strongRelative != nullptr) {
1152 strongRelative->removeZOrderRelative(this);
1153 }
1154 mCurrentState.zOrderRelativeOf = nullptr;
1155 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001156 setTransactionFlags(eTransactionNeeded);
1157 return true;
1158}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001159
Robert Carrdb66e622017-04-10 16:55:57 -07001160void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1161 mCurrentState.zOrderRelatives.remove(relative);
1162 mCurrentState.sequence++;
1163 mCurrentState.modified = true;
1164 setTransactionFlags(eTransactionNeeded);
1165}
1166
1167void Layer::addZOrderRelative(const wp<Layer>& relative) {
1168 mCurrentState.zOrderRelatives.add(relative);
1169 mCurrentState.modified = true;
1170 mCurrentState.sequence++;
1171 setTransactionFlags(eTransactionNeeded);
1172}
1173
Robert Carr503d2bd2017-12-04 15:49:47 -08001174bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001175 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1176 if (handle == nullptr) {
1177 return false;
1178 }
1179 sp<Layer> relative = handle->owner.promote();
1180 if (relative == nullptr) {
1181 return false;
1182 }
1183
Robert Carr503d2bd2017-12-04 15:49:47 -08001184 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1185 mCurrentState.zOrderRelativeOf == relative) {
1186 return false;
1187 }
1188
Robert Carrdb66e622017-04-10 16:55:57 -07001189 mCurrentState.sequence++;
1190 mCurrentState.modified = true;
Robert Carr503d2bd2017-12-04 15:49:47 -08001191 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001192
chaviw9ab4bd12017-11-03 13:11:00 -07001193 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1194 if (oldZOrderRelativeOf != nullptr) {
1195 oldZOrderRelativeOf->removeZOrderRelative(this);
1196 }
Robert Carrdb66e622017-04-10 16:55:57 -07001197 mCurrentState.zOrderRelativeOf = relative;
1198 relative->addZOrderRelative(this);
1199
1200 setTransactionFlags(eTransactionNeeded);
1201
1202 return true;
1203}
1204
Mathias Agopian13127d82013-03-05 17:47:11 -08001205bool Layer::setSize(uint32_t w, uint32_t h) {
David Sodman41fdfc92017-11-06 16:09:56 -08001206 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001207 mCurrentState.requested.w = w;
1208 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001209 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001210 setTransactionFlags(eTransactionNeeded);
1211 return true;
1212}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001213bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001214 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001215 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001216 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001217 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001218 setTransactionFlags(eTransactionNeeded);
1219 return true;
1220}
chaviw13fdc492017-06-27 12:40:18 -07001221
1222bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001223 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1224 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001225 return false;
1226
1227 mCurrentState.sequence++;
1228 mCurrentState.color.r = color.r;
1229 mCurrentState.color.g = color.g;
1230 mCurrentState.color.b = color.b;
1231 mCurrentState.modified = true;
1232 setTransactionFlags(eTransactionNeeded);
1233 return true;
1234}
1235
Robert Carrd4ae7f32018-06-07 16:10:57 -07001236bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix,
1237 bool allowNonRectPreservingTransforms) {
1238 Transform t;
1239 t.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
1240
1241 if (!allowNonRectPreservingTransforms && !t.preserveRects()) {
1242 ALOGW("Attempt to set rotation matrix without permission ACCESS_SURFACE_FLINGER ignored");
1243 return false;
1244 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001245 mCurrentState.sequence++;
David Sodman41fdfc92017-11-06 16:09:56 -08001246 mCurrentState.requested.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001247 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001248 setTransactionFlags(eTransactionNeeded);
1249 return true;
1250}
1251bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001252 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001253 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001254 setTransactionFlags(eTransactionNeeded);
1255 return true;
1256}
1257bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1258 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001259 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001260 mCurrentState.sequence++;
1261 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001262 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001263 setTransactionFlags(eTransactionNeeded);
1264 return true;
1265}
Robert Carr99e27f02016-06-16 15:18:02 -07001266
1267bool Layer::setCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001268 if (mCurrentState.requestedCrop == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001269 mCurrentState.sequence++;
Robert Carr99e27f02016-06-16 15:18:02 -07001270 mCurrentState.requestedCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001271 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr99e27f02016-06-16 15:18:02 -07001272 mCurrentState.crop = crop;
1273 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001274 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1275
Dan Stoza7dde5992015-05-22 09:51:44 -07001276 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001277 setTransactionFlags(eTransactionNeeded);
1278 return true;
1279}
Robert Carr8d5227b2017-03-16 15:41:03 -07001280
1281bool Layer::setFinalCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001282 if (mCurrentState.requestedFinalCrop == crop) return false;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001283 mCurrentState.sequence++;
Robert Carr8d5227b2017-03-16 15:41:03 -07001284 mCurrentState.requestedFinalCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001285 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001286 mCurrentState.finalCrop = crop;
1287 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001288 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1289
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001290 mCurrentState.modified = true;
1291 setTransactionFlags(eTransactionNeeded);
1292 return true;
1293}
Mathias Agopian13127d82013-03-05 17:47:11 -08001294
Robert Carrc3574f72016-03-24 12:19:32 -07001295bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001296 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001297 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001298 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001299 return true;
1300}
1301
rongliucfb187b2018-03-14 12:26:23 -07001302void Layer::setInfo(int32_t type, int32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001303 mCurrentState.appId = appId;
1304 mCurrentState.type = type;
1305 mCurrentState.modified = true;
1306 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001307}
1308
Mathias Agopian13127d82013-03-05 17:47:11 -08001309bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001310 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001311 mCurrentState.sequence++;
1312 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001313 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001314 setTransactionFlags(eTransactionNeeded);
1315 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001316}
1317
Robert Carr1f0a16a2016-10-24 16:27:39 -07001318uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001319 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001320 if (p == nullptr) {
1321 return getDrawingState().layerStack;
1322 }
1323 return p->getLayerStack();
1324}
1325
David Sodman41fdfc92017-11-06 16:09:56 -08001326void Layer::deferTransactionUntil(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001327 mCurrentState.barrierLayer = barrierLayer;
Dan Stoza7dde5992015-05-22 09:51:44 -07001328 mCurrentState.frameNumber = frameNumber;
1329 // We don't set eTransactionNeeded, because just receiving a deferral
1330 // request without any other state updates shouldn't actually induce a delay
1331 mCurrentState.modified = true;
1332 pushPendingState();
Robert Carr0d480722017-01-10 16:42:54 -08001333 mCurrentState.barrierLayer = nullptr;
Dan Stoza792e5292016-02-11 11:43:58 -08001334 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001335 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001336}
1337
David Sodman41fdfc92017-11-06 16:09:56 -08001338void Layer::deferTransactionUntil(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001339 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
1340 deferTransactionUntil(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001341}
1342
Dan Stozaee44edd2015-03-23 15:50:23 -07001343
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001344// ----------------------------------------------------------------------------
1345// pageflip handling...
1346// ----------------------------------------------------------------------------
1347
Robert Carr1f0a16a2016-10-24 16:27:39 -07001348bool Layer::isHiddenByPolicy() const {
1349 const Layer::State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001350 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001351 if (parent != nullptr && parent->isHiddenByPolicy()) {
1352 return true;
1353 }
1354 return s.flags & layer_state_t::eLayerHidden;
1355}
1356
David Sodman41fdfc92017-11-06 16:09:56 -08001357uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001358 // TODO: should we do something special if mSecure is set?
1359 if (mProtectedByApp) {
1360 // need a hardware-protected path to external video sink
1361 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001362 }
Riley Andrews03414a12014-07-01 14:22:59 -07001363 if (mPotentialCursor) {
1364 usage |= GraphicBuffer::USAGE_CURSOR;
1365 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001366 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001367 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001368}
1369
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001370void Layer::updateTransformHint(const sp<const DisplayDevice>& display) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001371 uint32_t orientation = 0;
1372 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001373 // The transform hint is used to improve performance, but we can
1374 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001375 // apply to all displays.
Dominik Laskowskia2edf612018-06-01 13:15:16 -07001376 const Transform& planeTransform = display->getTransform();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001377 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07001378 if (orientation & Transform::ROT_INVALID) {
1379 orientation = 0;
1380 }
1381 }
David Sodmaneb085e02017-10-05 18:49:04 -07001382 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001383}
1384
Mathias Agopian13127d82013-03-05 17:47:11 -08001385// ----------------------------------------------------------------------------
1386// debugging
1387// ----------------------------------------------------------------------------
1388
Kalle Raitaa099a242017-01-11 11:17:29 -08001389LayerDebugInfo Layer::getLayerDebugInfo() const {
1390 LayerDebugInfo info;
1391 const Layer::State& ds = getDrawingState();
1392 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001393 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001394 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1395 info.mType = String8(getTypeId());
1396 info.mTransparentRegion = ds.activeTransparentRegion;
1397 info.mVisibleRegion = visibleRegion;
1398 info.mSurfaceDamageRegion = surfaceDamageRegion;
1399 info.mLayerStack = getLayerStack();
1400 info.mX = ds.active.transform.tx();
1401 info.mY = ds.active.transform.ty();
1402 info.mZ = ds.z;
1403 info.mWidth = ds.active.w;
1404 info.mHeight = ds.active.h;
1405 info.mCrop = ds.crop;
1406 info.mFinalCrop = ds.finalCrop;
chaviw13fdc492017-06-27 12:40:18 -07001407 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001408 info.mFlags = ds.flags;
1409 info.mPixelFormat = getPixelFormat();
Chia-I Wu01591c92018-05-22 12:03:00 -07001410 info.mDataSpace = static_cast<android_dataspace>(mCurrentDataSpace);
Kalle Raitaa099a242017-01-11 11:17:29 -08001411 info.mMatrix[0][0] = ds.active.transform[0][0];
1412 info.mMatrix[0][1] = ds.active.transform[0][1];
1413 info.mMatrix[1][0] = ds.active.transform[1][0];
1414 info.mMatrix[1][1] = ds.active.transform[1][1];
1415 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001416 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001417 if (buffer != 0) {
1418 info.mActiveBufferWidth = buffer->getWidth();
1419 info.mActiveBufferHeight = buffer->getHeight();
1420 info.mActiveBufferStride = buffer->getStride();
1421 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001422 } else {
1423 info.mActiveBufferWidth = 0;
1424 info.mActiveBufferHeight = 0;
1425 info.mActiveBufferStride = 0;
1426 info.mActiveBufferFormat = 0;
1427 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001428 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001429 info.mNumQueuedFrames = getQueuedFrameCount();
1430 info.mRefreshPending = isBufferLatched();
1431 info.mIsOpaque = isOpaque(ds);
1432 info.mContentDirty = contentDirty;
1433 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001434}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001435
Dan Stozae22aec72016-08-01 13:20:59 -07001436void Layer::miniDumpHeader(String8& result) {
Yichi Chen6ca35192018-05-29 12:20:43 +08001437 result.append("-------------------------------");
1438 result.append("-------------------------------");
1439 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001440 result.append(" Layer name\n");
1441 result.append(" Z | ");
1442 result.append(" Comp Type | ");
Yichi Chen6ca35192018-05-29 12:20:43 +08001443 result.append(" Transform | ");
Dan Stozae22aec72016-08-01 13:20:59 -07001444 result.append(" Disp Frame (LTRB) | ");
1445 result.append(" Source Crop (LTRB)\n");
Yichi Chen6ca35192018-05-29 12:20:43 +08001446 result.append("-------------------------------");
1447 result.append("-------------------------------");
1448 result.append("-----------------------------\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001449}
1450
Dominik Laskowski7e045462018-05-30 13:02:02 -07001451void Layer::miniDump(String8& result, int32_t displayId) const {
1452 if (getBE().mHwcLayers.count(displayId) == 0) {
Dan Stozae22aec72016-08-01 13:20:59 -07001453 return;
1454 }
1455
1456 String8 name;
1457 if (mName.length() > 77) {
1458 std::string shortened;
1459 shortened.append(mName.string(), 36);
1460 shortened.append("[...]");
1461 shortened.append(mName.string() + (mName.length() - 36), 36);
1462 name = shortened.c_str();
1463 } else {
1464 name = mName;
1465 }
1466
1467 result.appendFormat(" %s\n", name.string());
1468
1469 const Layer::State& layerState(getDrawingState());
Chia-I Wu1e043612018-03-01 09:45:09 -08001470 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
1471 result.appendFormat(" rel %6d | ", layerState.z);
1472 } else {
1473 result.appendFormat(" %10d | ", layerState.z);
1474 }
Dominik Laskowski7e045462018-05-30 13:02:02 -07001475 result.appendFormat("%10s | ", to_string(getCompositionType(displayId)).c_str());
David Sodman0756cdd2018-04-13 11:26:33 -07001476 result.appendFormat("%10s | ", to_string(getBE().mHwcLayers[displayId].transform).c_str());
1477 const Rect& frame = getBE().compositionInfo.hwc.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001478 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
David Sodman0756cdd2018-04-13 11:26:33 -07001479 const FloatRect& crop = getBE().compositionInfo.hwc.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001480 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 -07001481
Yichi Chen6ca35192018-05-29 12:20:43 +08001482 result.append("- - - - - - - - - - - - - - - -");
1483 result.append("- - - - - - - - - - - - - - - -");
1484 result.append("- - - - - - - - - - - - - - -\n");
Dan Stozae22aec72016-08-01 13:20:59 -07001485}
Dan Stozae22aec72016-08-01 13:20:59 -07001486
Svetoslavd85084b2014-03-20 10:28:31 -07001487void Layer::dumpFrameStats(String8& result) const {
1488 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001489}
1490
Svetoslavd85084b2014-03-20 10:28:31 -07001491void Layer::clearFrameStats() {
1492 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001493}
1494
Jamie Gennis6547ff42013-07-16 20:12:42 -07001495void Layer::logFrameStats() {
1496 mFrameTracker.logAndResetStats(mName);
1497}
1498
Svetoslavd85084b2014-03-20 10:28:31 -07001499void Layer::getFrameStats(FrameStats* outStats) const {
1500 mFrameTracker.getStats(outStats);
1501}
1502
Brian Andersond6927fb2016-07-23 23:37:30 -07001503void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001504 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001505 Mutex::Autolock lock(mFrameEventHistoryMutex);
1506 mFrameEventHistory.checkFencesForCompletion();
1507 mFrameEventHistory.dump(result);
1508}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001509
Brian Anderson5ea5e592016-12-01 16:54:33 -08001510void Layer::onDisconnect() {
1511 Mutex::Autolock lock(mFrameEventHistoryMutex);
1512 mFrameEventHistory.onDisconnect();
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001513 mTimeStats.onDisconnect(getName().c_str());
Brian Anderson5ea5e592016-12-01 16:54:33 -08001514}
1515
Brian Anderson3890c392016-07-25 12:48:08 -07001516void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001517 FrameEventHistoryDelta* outDelta) {
Yiwei Zhangfaf3ded2018-05-02 17:37:17 -07001518 if (newTimestamps) {
1519 mTimeStats.setPostTime(getName().c_str(), newTimestamps->frameNumber,
1520 newTimestamps->postedTime);
1521 }
1522
Brian Andersond6927fb2016-07-23 23:37:30 -07001523 Mutex::Autolock lock(mFrameEventHistoryMutex);
1524 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001525 // If there are any unsignaled fences in the aquire timeline at this
1526 // point, the previously queued frame hasn't been latched yet. Go ahead
1527 // and try to get the signal time here so the syscall is taken out of
1528 // the main thread's critical path.
1529 mAcquireTimeline.updateSignalTimes();
1530 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001531 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001532 mFrameEventHistory.addQueue(*newTimestamps);
1533 }
1534
Brian Anderson3890c392016-07-25 12:48:08 -07001535 if (outDelta) {
1536 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001537 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001538}
Dan Stozae77c7662016-05-13 11:37:28 -07001539
Chia-I Wu98f1c102017-05-30 14:54:08 -07001540size_t Layer::getChildrenCount() const {
1541 size_t count = 0;
1542 for (const sp<Layer>& child : mCurrentChildren) {
1543 count += 1 + child->getChildrenCount();
1544 }
1545 return count;
1546}
1547
Robert Carr1f0a16a2016-10-24 16:27:39 -07001548void Layer::addChild(const sp<Layer>& layer) {
1549 mCurrentChildren.add(layer);
1550 layer->setParent(this);
1551}
1552
1553ssize_t Layer::removeChild(const sp<Layer>& layer) {
1554 layer->setParent(nullptr);
1555 return mCurrentChildren.remove(layer);
1556}
1557
Robert Carr1db73f62016-12-21 12:58:51 -08001558bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1559 sp<Handle> handle = nullptr;
1560 sp<Layer> newParent = nullptr;
1561 if (newParentHandle == nullptr) {
1562 return false;
1563 }
1564 handle = static_cast<Handle*>(newParentHandle.get());
1565 newParent = handle->owner.promote();
1566 if (newParent == nullptr) {
1567 ALOGE("Unable to promote Layer handle");
1568 return false;
1569 }
1570
1571 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001572 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001573
1574 sp<Client> client(child->mClientRef.promote());
1575 if (client != nullptr) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001576 client->updateParent(newParent);
Robert Carr1db73f62016-12-21 12:58:51 -08001577 }
1578 }
1579 mCurrentChildren.clear();
1580
1581 return true;
1582}
1583
Robert Carr15eae092018-03-23 13:43:53 -07001584void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001585 for (const sp<Layer>& child : mDrawingChildren) {
1586 child->mDrawingParent = newParent;
1587 }
1588}
1589
chaviwf1961f72017-09-18 16:41:07 -07001590bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1591 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001592 return false;
1593 }
1594
1595 auto handle = static_cast<Handle*>(newParentHandle.get());
1596 sp<Layer> newParent = handle->owner.promote();
1597 if (newParent == nullptr) {
1598 ALOGE("Unable to promote Layer handle");
1599 return false;
1600 }
1601
chaviwf1961f72017-09-18 16:41:07 -07001602 sp<Layer> parent = getParent();
1603 if (parent != nullptr) {
1604 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001605 }
chaviwf1961f72017-09-18 16:41:07 -07001606 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001607
chaviwf1961f72017-09-18 16:41:07 -07001608 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001609 sp<Client> newParentClient(newParent->mClientRef.promote());
1610
chaviwf1961f72017-09-18 16:41:07 -07001611 if (client != newParentClient) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001612 client->updateParent(newParent);
chaviw06178942017-07-27 10:25:59 -07001613 }
1614
chaviw06178942017-07-27 10:25:59 -07001615 return true;
1616}
1617
Robert Carr9524cb32017-02-13 11:32:32 -08001618bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001619 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001620 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001621 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001622 if (client != nullptr && parentClient != client) {
Robert Carr7f619b22017-11-06 12:56:35 -08001623 client->detachLayer(child.get());
1624 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001625 }
Robert Carr7f619b22017-11-06 12:56:35 -08001626 }
Robert Carr9524cb32017-02-13 11:32:32 -08001627
1628 return true;
1629}
1630
Chia-I Wu11481472018-05-04 10:43:19 -07001631bool Layer::isLegacyDataSpace() const {
1632 // return true when no higher bits are set
Chia-I Wu01591c92018-05-22 12:03:00 -07001633 return !(mCurrentDataSpace & (ui::Dataspace::STANDARD_MASK |
Chia-I Wu11481472018-05-04 10:43:19 -07001634 ui::Dataspace::TRANSFER_MASK | ui::Dataspace::RANGE_MASK));
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001635}
1636
Robert Carr1f0a16a2016-10-24 16:27:39 -07001637void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001638 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001639}
1640
1641void Layer::clearSyncPoints() {
1642 for (const auto& child : mCurrentChildren) {
1643 child->clearSyncPoints();
1644 }
1645
1646 Mutex::Autolock lock(mLocalSyncPointMutex);
1647 for (auto& point : mLocalSyncPoints) {
1648 point->setFrameAvailable();
1649 }
1650 mLocalSyncPoints.clear();
1651}
1652
1653int32_t Layer::getZ() const {
1654 return mDrawingState.z;
1655}
1656
Robert Carr29abff82017-12-04 13:51:20 -08001657bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1658 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1659 const State& state = useDrawing ? mDrawingState : mCurrentState;
1660 return state.zOrderRelativeOf != nullptr;
1661}
1662
David Sodman41fdfc92017-11-06 16:09:56 -08001663__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001664 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001665 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1666 "makeTraversalList received invalid stateSet");
1667 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1668 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1669 const State& state = useDrawing ? mDrawingState : mCurrentState;
1670
Robert Carr29abff82017-12-04 13:51:20 -08001671 if (state.zOrderRelatives.size() == 0) {
1672 *outSkipRelativeZUsers = true;
1673 return children;
1674 }
1675
chaviwfd462612018-05-31 16:11:27 -07001676 LayerVector traverse(stateSet);
Dan Stoza412903f2017-04-27 13:42:17 -07001677 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001678 sp<Layer> strongRelative = weakRelative.promote();
1679 if (strongRelative != nullptr) {
1680 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001681 }
1682 }
1683
Dan Stoza412903f2017-04-27 13:42:17 -07001684 for (const sp<Layer>& child : children) {
Robert Carr503c7042017-09-27 15:06:08 -07001685 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1686 if (childState.zOrderRelativeOf != nullptr) {
1687 continue;
1688 }
Robert Carrdb66e622017-04-10 16:55:57 -07001689 traverse.add(child);
1690 }
1691
1692 return traverse;
1693}
1694
Robert Carr1f0a16a2016-10-24 16:27:39 -07001695/**
Robert Carrdb66e622017-04-10 16:55:57 -07001696 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001697 */
Dan Stoza412903f2017-04-27 13:42:17 -07001698void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001699 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1700 // produce a new list for traversing, including our relatives, and not including our children
1701 // who are relatives of another surface. In the case that there are no relative Z,
1702 // makeTraversalList returns our children directly to avoid significant overhead.
1703 // However in this case we need to take the responsibility for filtering children which
1704 // are relatives of another surface here.
1705 bool skipRelativeZUsers = false;
1706 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001707
Robert Carr1f0a16a2016-10-24 16:27:39 -07001708 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001709 for (; i < list.size(); i++) {
1710 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001711 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1712 continue;
1713 }
1714
Robert Carrdb66e622017-04-10 16:55:57 -07001715 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001716 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001717 }
Dan Stoza412903f2017-04-27 13:42:17 -07001718 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001719 }
Robert Carr29abff82017-12-04 13:51:20 -08001720
Dan Stoza412903f2017-04-27 13:42:17 -07001721 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001722 for (; i < list.size(); i++) {
1723 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001724
1725 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1726 continue;
1727 }
Dan Stoza412903f2017-04-27 13:42:17 -07001728 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001729 }
1730}
1731
1732/**
Robert Carrdb66e622017-04-10 16:55:57 -07001733 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001734 */
Dan Stoza412903f2017-04-27 13:42:17 -07001735void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1736 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001737 // See traverseInZOrder for documentation.
1738 bool skipRelativeZUsers = false;
1739 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001740
Robert Carr1f0a16a2016-10-24 16:27:39 -07001741 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001742 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001743 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001744
1745 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1746 continue;
1747 }
1748
Robert Carrdb66e622017-04-10 16:55:57 -07001749 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001750 break;
1751 }
Dan Stoza412903f2017-04-27 13:42:17 -07001752 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001753 }
Dan Stoza412903f2017-04-27 13:42:17 -07001754 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001755 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001756 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001757
1758 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1759 continue;
1760 }
1761
Dan Stoza412903f2017-04-27 13:42:17 -07001762 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001763 }
1764}
1765
chaviw4b129c22018-04-09 16:19:43 -07001766LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1767 const std::vector<Layer*>& layersInTree) {
1768 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1769 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001770 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1771 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
chaviw4b129c22018-04-09 16:19:43 -07001772 const State& state = useDrawing ? mDrawingState : mCurrentState;
1773
chaviwfd462612018-05-31 16:11:27 -07001774 LayerVector traverse(stateSet);
chaviw4b129c22018-04-09 16:19:43 -07001775 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1776 sp<Layer> strongRelative = weakRelative.promote();
1777 // Only add relative layers that are also descendents of the top most parent of the tree.
1778 // If a relative layer is not a descendent, then it should be ignored.
1779 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1780 traverse.add(strongRelative);
1781 }
1782 }
1783
1784 for (const sp<Layer>& child : children) {
1785 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1786 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1787 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1788 // the child here since it won't be added later as a relative.
1789 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1790 childState.zOrderRelativeOf.promote().get())) {
1791 continue;
1792 }
1793 traverse.add(child);
1794 }
1795
1796 return traverse;
1797}
1798
1799void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1800 LayerVector::StateSet stateSet,
1801 const LayerVector::Visitor& visitor) {
1802 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001803
1804 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001805 for (; i < list.size(); i++) {
1806 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001807 if (relative->getZ() >= 0) {
1808 break;
1809 }
chaviw4b129c22018-04-09 16:19:43 -07001810 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001811 }
chaviw4b129c22018-04-09 16:19:43 -07001812
chaviwa76b2712017-09-20 12:02:26 -07001813 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001814 for (; i < list.size(); i++) {
1815 const auto& relative = list[i];
1816 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001817 }
1818}
1819
chaviw4b129c22018-04-09 16:19:43 -07001820std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1821 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1822 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1823
1824 std::vector<Layer*> layersInTree = {this};
1825 for (size_t i = 0; i < children.size(); i++) {
1826 const auto& child = children[i];
1827 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1828 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1829 }
1830
1831 return layersInTree;
1832}
1833
1834void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1835 const LayerVector::Visitor& visitor) {
1836 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1837 std::sort(layersInTree.begin(), layersInTree.end());
1838 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1839}
1840
Robert Carr1f0a16a2016-10-24 16:27:39 -07001841Transform Layer::getTransform() const {
1842 Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001843 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001844 if (p != nullptr) {
1845 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001846
1847 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1848 // it isFixedSize) then there may be additional scaling not accounted
1849 // for in the transform. We need to mirror this scaling in child surfaces
1850 // or we will break the contract where WM can treat child surfaces as
1851 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08001852 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001853 int bufferWidth;
1854 int bufferHeight;
1855 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08001856 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
1857 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001858 } else {
David Sodman0cc69182017-11-17 12:12:07 -08001859 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
1860 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001861 }
David Sodman41fdfc92017-11-06 16:09:56 -08001862 float sx = p->getDrawingState().active.w / static_cast<float>(bufferWidth);
1863 float sy = p->getDrawingState().active.h / static_cast<float>(bufferHeight);
Robert Carr9b429f42017-04-17 14:56:57 -07001864 Transform extraParentScaling;
1865 extraParentScaling.set(sx, 0, 0, sy);
1866 t = t * extraParentScaling;
1867 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001868 }
1869 return t * getDrawingState().active.transform;
1870}
1871
chaviw13fdc492017-06-27 12:40:18 -07001872half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001873 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001874
chaviw13fdc492017-06-27 12:40:18 -07001875 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1876 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001877}
Robert Carr6452f122017-03-21 10:41:29 -07001878
chaviw13fdc492017-06-27 12:40:18 -07001879half4 Layer::getColor() const {
1880 const half4 color(getDrawingState().color);
1881 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001882}
Robert Carr6452f122017-03-21 10:41:29 -07001883
Robert Carr1f0a16a2016-10-24 16:27:39 -07001884void Layer::commitChildList() {
1885 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1886 const auto& child = mCurrentChildren[i];
1887 child->commitChildList();
1888 }
1889 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001890 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001891}
1892
chaviw1d044282017-09-27 12:19:28 -07001893void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1894 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1895 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1896 const State& state = useDrawing ? mDrawingState : mCurrentState;
1897
1898 Transform requestedTransform = state.active.transform;
1899 Transform transform = getTransform();
1900
1901 layerInfo->set_id(sequence);
1902 layerInfo->set_name(getName().c_str());
1903 layerInfo->set_type(String8(getTypeId()));
1904
1905 for (const auto& child : children) {
1906 layerInfo->add_children(child->sequence);
1907 }
1908
1909 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1910 sp<Layer> strongRelative = weakRelative.promote();
1911 if (strongRelative != nullptr) {
1912 layerInfo->add_relatives(strongRelative->sequence);
1913 }
1914 }
1915
1916 LayerProtoHelper::writeToProto(state.activeTransparentRegion,
1917 layerInfo->mutable_transparent_region());
1918 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
1919 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
1920
1921 layerInfo->set_layer_stack(getLayerStack());
1922 layerInfo->set_z(state.z);
1923
1924 PositionProto* position = layerInfo->mutable_position();
1925 position->set_x(transform.tx());
1926 position->set_y(transform.ty());
1927
1928 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
1929 requestedPosition->set_x(requestedTransform.tx());
1930 requestedPosition->set_y(requestedTransform.ty());
1931
1932 SizeProto* size = layerInfo->mutable_size();
1933 size->set_w(state.active.w);
1934 size->set_h(state.active.h);
1935
1936 LayerProtoHelper::writeToProto(state.crop, layerInfo->mutable_crop());
1937 LayerProtoHelper::writeToProto(state.finalCrop, layerInfo->mutable_final_crop());
1938
1939 layerInfo->set_is_opaque(isOpaque(state));
1940 layerInfo->set_invalidate(contentDirty);
Chia-I Wu01591c92018-05-22 12:03:00 -07001941
1942 // XXX (b/79210409) mCurrentDataSpace is not protected
1943 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(mCurrentDataSpace)));
1944
chaviw1d044282017-09-27 12:19:28 -07001945 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1946 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
1947 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
1948 layerInfo->set_flags(state.flags);
1949
1950 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1951 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
1952
Jorim Jaggi8e0af362017-11-14 16:28:28 +01001953 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07001954 if (parent != nullptr) {
1955 layerInfo->set_parent(parent->sequence);
1956 }
1957
1958 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1959 if (zOrderRelativeOf != nullptr) {
1960 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1961 }
1962
Chia-I Wu01591c92018-05-22 12:03:00 -07001963 // XXX getBE().compositionInfo.mBuffer is not protected
David Sodman0cc69182017-11-17 12:12:07 -08001964 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001965 if (buffer != nullptr) {
1966 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
Yichi Chen6ca35192018-05-29 12:20:43 +08001967 LayerProtoHelper::writeToProto(Transform(mCurrentTransform),
1968 layerInfo->mutable_buffer_transform());
chaviw1d044282017-09-27 12:19:28 -07001969 }
1970
1971 layerInfo->set_queued_frames(getQueuedFrameCount());
1972 layerInfo->set_refresh_pending(isBufferLatched());
rongliucfb187b2018-03-14 12:26:23 -07001973 layerInfo->set_window_type(state.type);
1974 layerInfo->set_app_id(state.appId);
chaviwadc40c22018-07-10 16:57:27 -07001975 layerInfo->set_curr_frame(mCurrentFrameNumber);
1976
1977 for (const auto& pendingState : mPendingStates) {
1978 auto barrierLayer = pendingState.barrierLayer.promote();
1979 if (barrierLayer != nullptr) {
1980 BarrierLayerProto* barrierLayerProto = layerInfo->add_barrier_layer();
1981 barrierLayerProto->set_id(barrierLayer->sequence);
1982 barrierLayerProto->set_frame_number(pendingState.frameNumber);
1983 }
1984 }
chaviw1d044282017-09-27 12:19:28 -07001985}
1986
Dominik Laskowski7e045462018-05-30 13:02:02 -07001987void Layer::writeToProto(LayerProto* layerInfo, int32_t displayId) {
Peiyong Lin91b1df22018-06-18 18:00:16 -07001988 if (!hasHwcLayer(displayId)) {
1989 return;
1990 }
1991
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001992 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
1993
Dominik Laskowski7e045462018-05-30 13:02:02 -07001994 const auto& hwcInfo = getBE().mHwcLayers.at(displayId);
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001995
1996 const Rect& frame = hwcInfo.displayFrame;
1997 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
1998
1999 const FloatRect& crop = hwcInfo.sourceCrop;
2000 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
2001
2002 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
2003 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08002004
2005 const int32_t compositionType = static_cast<int32_t>(hwcInfo.compositionType);
2006 layerInfo->set_hwc_composition_type(compositionType);
2007
2008 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2009 static_cast<BufferLayer*>(this)->isProtected()) {
2010 layerInfo->set_is_protected(true);
2011 } else {
2012 layerInfo->set_is_protected(false);
2013 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002014}
2015
Mathias Agopian13127d82013-03-05 17:47:11 -08002016// ---------------------------------------------------------------------------
2017
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002018}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002019
2020#if defined(__gl_h_)
2021#error "don't include gl/gl.h in this file"
2022#endif
2023
2024#if defined(__gl2_h_)
2025#error "don't include gl2/gl2.h in this file"
2026#endif