blob: fc9f16b919c2fc2f2e9f8d27bc798562124ce836 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dan Stoza9e56aa02015-11-02 13:00:03 -080017//#define LOG_NDEBUG 0
18#undef LOG_TAG
19#define LOG_TAG "Layer"
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080020#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
Mathias Agopian13127d82013-03-05 17:47:11 -080022#include <math.h>
David Sodman41fdfc92017-11-06 16:09:56 -080023#include <stdint.h>
24#include <stdlib.h>
25#include <sys/types.h>
chaviw4b129c22018-04-09 16:19:43 -070026#include <algorithm>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027
Mathias Agopiana67932f2011-04-20 14:20:59 -070028#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070029#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070030#include <cutils/properties.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031
32#include <utils/Errors.h>
33#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080034#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080036#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037
Courtney Goeltzenleuchter36c44dc2017-04-14 09:33:16 -060038#include <ui/DebugUtils.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070039#include <ui/GraphicBuffer.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080040#include <ui/PixelFormat.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080041
Dan Stoza6b9454d2014-11-07 16:00:59 -080042#include <gui/BufferItem.h>
Kalle Raitaa099a242017-01-11 11:17:29 -080043#include <gui/LayerDebugInfo.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080044#include <gui/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045
Yiwei Zhang60d1a192018-03-07 14:52:28 -080046#include "BufferLayer.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020047#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070048#include "DisplayDevice.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include "Layer.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070050#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070051#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052#include "SurfaceFlinger.h"
David Sodman41fdfc92017-11-06 16:09:56 -080053#include "clz.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054
Mathias Agopian1b031492012-06-20 17:51:20 -070055#include "DisplayHardware/HWComposer.h"
56
Mathias Agopian875d8e12013-06-07 15:35:48 -070057#include "RenderEngine/RenderEngine.h"
58
Dan Stozac5da2712016-07-20 15:38:12 -070059#include <mutex>
chaviw1d044282017-09-27 12:19:28 -070060#include "LayerProtoHelper.h"
Dan Stozac5da2712016-07-20 15:38:12 -070061
David Sodman41fdfc92017-11-06 16:09:56 -080062#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064namespace android {
65
Mathias Agopian13127d82013-03-05 17:47:11 -080066int32_t Layer::sSequence = 1;
67
David Sodman41fdfc92017-11-06 16:09:56 -080068Layer::Layer(SurfaceFlinger* flinger, const sp<Client>& client, const String8& name, uint32_t w,
69 uint32_t h, uint32_t flags)
David Sodman0c69cad2017-08-21 12:12:51 -070070 : contentDirty(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080071 sequence(uint32_t(android_atomic_inc(&sSequence))),
72 mFlinger(flinger),
Mathias Agopian13127d82013-03-05 17:47:11 -080073 mPremultipliedAlpha(true),
David Sodman0c69cad2017-08-21 12:12:51 -070074 mName(name),
Mathias Agopian13127d82013-03-05 17:47:11 -080075 mTransactionFlags(0),
Dan Stoza7dde5992015-05-22 09:51:44 -070076 mPendingStateMutex(),
77 mPendingStates(),
Mathias Agopiana67932f2011-04-20 14:20:59 -070078 mQueuedFrames(0),
Jesse Hall399184a2014-03-03 15:42:54 -080079 mSidebandStreamChanged(false),
Mathias Agopiana9347642017-02-13 16:42:28 -080080 mActiveBufferSlot(BufferQueue::INVALID_BUFFER_SLOT),
Mathias Agopiana67932f2011-04-20 14:20:59 -070081 mCurrentTransform(0),
Robert Carrc3574f72016-03-24 12:19:32 -070082 mOverrideScalingMode(-1),
Mathias Agopiana67932f2011-04-20 14:20:59 -070083 mCurrentOpacity(true),
Dan Stozacac35382016-01-27 12:21:06 -080084 mCurrentFrameNumber(0),
Mathias Agopian82d7ab62012-01-19 18:34:40 -080085 mFrameLatencyNeeded(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080086 mFiltering(false),
87 mNeedsFiltering(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080088 mProtectedByApp(false),
Riley Andrews03414a12014-07-01 14:22:59 -070089 mClientRef(client),
Dan Stozaa4650a52015-05-12 12:56:16 -070090 mPotentialCursor(false),
91 mQueueItemLock(),
92 mQueueItemCondition(),
93 mQueueItems(),
Dan Stoza65476f32015-05-14 09:27:25 -070094 mLastFrameNumberReceived(0),
Robert Carr82364e32016-05-15 11:27:47 -070095 mAutoRefresh(false),
David Sodmanb8af7922017-12-21 15:17:55 -080096 mFreezeGeometryUpdates(false),
David Sodman2b727ac2017-12-21 14:28:08 -080097 mBE{this, name.string()} {
Dan Stoza9e56aa02015-11-02 13:00:03 -080098
Mathias Agopiana67932f2011-04-20 14:20:59 -070099 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700100
101 uint32_t layerFlags = 0;
David Sodman41fdfc92017-11-06 16:09:56 -0800102 if (flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
103 if (flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
104 if (flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700105
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700106 mName = name;
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700107 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700108
109 mCurrentState.active.w = w;
110 mCurrentState.active.h = h;
David Sodman0c69cad2017-08-21 12:12:51 -0700111 mCurrentState.flags = layerFlags;
Robert Carr3dcabfa2016-03-01 18:36:58 -0800112 mCurrentState.active.transform.set(0, 0);
Robert Carrb5d3d262016-03-25 15:08:13 -0700113 mCurrentState.crop.makeInvalid();
114 mCurrentState.finalCrop.makeInvalid();
Robert Carr7bf247e2017-05-18 14:02:49 -0700115 mCurrentState.requestedFinalCrop = mCurrentState.finalCrop;
116 mCurrentState.requestedCrop = mCurrentState.crop;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700117 mCurrentState.z = 0;
chaviw13fdc492017-06-27 12:40:18 -0700118 mCurrentState.color.a = 1.0f;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700119 mCurrentState.layerStack = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700120 mCurrentState.sequence = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700121 mCurrentState.requested = mCurrentState.active;
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700122 mCurrentState.dataSpace = ui::Dataspace::UNKNOWN;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500123 mCurrentState.appId = 0;
124 mCurrentState.type = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700125
126 // drawing state & current state are identical
127 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700128
Dan Stoza9e56aa02015-11-02 13:00:03 -0800129 const auto& hwc = flinger->getHwComposer();
130 const auto& activeConfig = hwc.getActiveConfig(HWC_DISPLAY_PRIMARY);
131 nsecs_t displayPeriod = activeConfig->getVsyncPeriod();
Jamie Gennis6547ff42013-07-16 20:12:42 -0700132 mFrameTracker.setDisplayRefreshPeriod(displayPeriod);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800133
134 CompositorTiming compositorTiming;
135 flinger->getCompositorTiming(&compositorTiming);
136 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
David Sodman9eeae692017-11-02 10:53:32 -0700137
Jamie Gennise8696a42012-01-15 18:54:57 -0800138}
139
David Sodman41fdfc92017-11-06 16:09:56 -0800140void Layer::onFirstRef() {}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700141
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700142Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800143 sp<Client> c(mClientRef.promote());
144 if (c != 0) {
145 c->detachLayer(this);
146 }
147
148 for (auto& point : mRemoteSyncPoints) {
149 point->setTransactionApplied();
150 }
151 for (auto& point : mLocalSyncPoints) {
152 point->setFrameAvailable();
153 }
Jamie Gennis6547ff42013-07-16 20:12:42 -0700154 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700155}
156
Mathias Agopian13127d82013-03-05 17:47:11 -0800157// ---------------------------------------------------------------------------
158// callbacks
159// ---------------------------------------------------------------------------
160
David Sodmaneb085e02017-10-05 18:49:04 -0700161/*
162 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
163 * Layer. So, the implementation is done in BufferLayer. When called on a
164 * ColorLayer object, it's essentially a NOP.
165 */
David Sodmaneb085e02017-10-05 18:49:04 -0700166void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800167
Chia-I Wuc6657022017-08-15 11:18:17 -0700168void Layer::onRemovedFromCurrentState() {
169 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
170
chaviw8b3871a2017-11-01 17:41:01 -0700171 mPendingRemoval = true;
172
Robert Carr5edb1ad2017-04-25 10:54:24 -0700173 if (mCurrentState.zOrderRelativeOf != nullptr) {
174 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
175 if (strongRelative != nullptr) {
176 strongRelative->removeZOrderRelative(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700177 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700178 }
179 mCurrentState.zOrderRelativeOf = nullptr;
180 }
181
Chia-I Wuc6657022017-08-15 11:18:17 -0700182 for (const auto& child : mCurrentChildren) {
183 child->onRemovedFromCurrentState();
184 }
185}
Chia-I Wu38512252017-05-17 14:36:16 -0700186
Chia-I Wuc6657022017-08-15 11:18:17 -0700187void Layer::onRemoved() {
188 // the layer is removed from SF mLayersPendingRemoval
David Sodmaneb085e02017-10-05 18:49:04 -0700189 abandon();
Chia-I Wuc6657022017-08-15 11:18:17 -0700190
Steven Thomasb02664d2017-07-26 18:48:28 -0700191 destroyAllHwcLayers();
Chia-I Wu38512252017-05-17 14:36:16 -0700192
Robert Carr1f0a16a2016-10-24 16:27:39 -0700193 for (const auto& child : mCurrentChildren) {
194 child->onRemoved();
195 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700196}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700197
Mathias Agopian13127d82013-03-05 17:47:11 -0800198// ---------------------------------------------------------------------------
199// set-up
200// ---------------------------------------------------------------------------
201
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700202const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800203 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800204}
205
chaviw13fdc492017-06-27 12:40:18 -0700206bool Layer::getPremultipledAlpha() const {
207 return mPremultipliedAlpha;
208}
209
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700210sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800211 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700212 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800213}
214
215// ---------------------------------------------------------------------------
216// h/w composer set-up
217// ---------------------------------------------------------------------------
218
Steven Thomasb02664d2017-07-26 18:48:28 -0700219bool Layer::createHwcLayer(HWComposer* hwc, int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700220 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(hwcId) != 0,
David Sodman9eeae692017-11-02 10:53:32 -0700221 "Already have a layer for hwcId %d", hwcId);
David Sodman5d89c1d2017-12-14 15:54:51 -0800222
223 std::shared_ptr<LayerContainer> layer(new LayerContainer(hwc, hwcId));
Steven Thomasb02664d2017-07-26 18:48:28 -0700224 if (!layer) {
225 return false;
226 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700227 LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers[hwcId];
Steven Thomasb02664d2017-07-26 18:48:28 -0700228 hwcInfo.hwc = hwc;
229 hwcInfo.layer = layer;
Steven Thomasb02664d2017-07-26 18:48:28 -0700230 return true;
231}
232
Chia-I Wu83806892017-11-16 10:50:20 -0800233bool Layer::destroyHwcLayer(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700234 if (getBE().mHwcLayers.count(hwcId) == 0) {
Chia-I Wu83806892017-11-16 10:50:20 -0800235 return false;
Steven Thomasb02664d2017-07-26 18:48:28 -0700236 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700237 auto& hwcInfo = getBE().mHwcLayers[hwcId];
David Sodman41fdfc92017-11-06 16:09:56 -0800238 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr, "Attempt to destroy null layer");
Steven Thomasb02664d2017-07-26 18:48:28 -0700239 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
David Sodman5d89c1d2017-12-14 15:54:51 -0800240 hwcInfo.layer = nullptr;
241
242 if (getBE().mHwcLayers.count(hwcId) == 1) {
243 getBE().mHwcLayers.erase(hwcId);
244 }
245
Chia-I Wu83806892017-11-16 10:50:20 -0800246 return true;
Steven Thomasb02664d2017-07-26 18:48:28 -0700247}
248
249void Layer::destroyAllHwcLayers() {
David Sodman6f65f3e2017-11-03 14:28:09 -0700250 size_t numLayers = getBE().mHwcLayers.size();
Steven Thomasb02664d2017-07-26 18:48:28 -0700251 for (size_t i = 0; i < numLayers; ++i) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700252 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.empty(), "destroyAllHwcLayers failed");
253 destroyHwcLayer(getBE().mHwcLayers.begin()->first);
Steven Thomasb02664d2017-07-26 18:48:28 -0700254 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700255 LOG_ALWAYS_FATAL_IF(!getBE().mHwcLayers.empty(),
David Sodman41fdfc92017-11-06 16:09:56 -0800256 "All hardware composer layers should have been destroyed");
Steven Thomasb02664d2017-07-26 18:48:28 -0700257}
Steven Thomasb02664d2017-07-26 18:48:28 -0700258
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800259Rect Layer::getContentCrop() const {
260 // this is the crop rectangle that applies to the buffer
261 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700262 Rect crop;
263 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800264 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700265 crop = mCurrentCrop;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800266 } else if (getBE().compositionInfo.mBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800267 // otherwise we use the whole buffer
David Sodman0cc69182017-11-17 12:12:07 -0800268 crop = getBE().compositionInfo.mBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700269 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800270 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700271 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700272 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700273 return crop;
274}
275
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700276static Rect reduce(const Rect& win, const Region& exclude) {
277 if (CC_LIKELY(exclude.isEmpty())) {
278 return win;
279 }
280 if (exclude.isRect()) {
281 return win.reduce(exclude.getBounds());
282 }
283 return Region(win).subtract(exclude).getBounds();
284}
285
Dan Stoza80d61162017-12-20 15:57:52 -0800286static FloatRect reduce(const FloatRect& win, const Region& exclude) {
287 if (CC_LIKELY(exclude.isEmpty())) {
288 return win;
289 }
290 // Convert through Rect (by rounding) for lack of FloatRegion
291 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
292}
293
Robert Carr1f0a16a2016-10-24 16:27:39 -0700294Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
295 const Layer::State& s(getDrawingState());
296 Rect win(s.active.w, s.active.h);
297
298 if (!s.crop.isEmpty()) {
299 win.intersect(s.crop, &win);
300 }
301
302 Transform t = getTransform();
303 win = t.transform(win);
304
Robert Carr41b08b52017-06-01 16:11:34 -0700305 if (!s.finalCrop.isEmpty()) {
306 win.intersect(s.finalCrop, &win);
307 }
308
Chia-I Wue41dbe62017-06-13 14:10:56 -0700309 const sp<Layer>& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700310 // Now we need to calculate the parent bounds, so we can clip ourselves to those.
311 // When calculating the parent bounds for purposes of clipping,
312 // we don't need to constrain the parent to its transparent region.
313 // The transparent region is an optimization based on the
314 // buffer contents of the layer, but does not affect the space allocated to
315 // it by policy, and thus children should be allowed to extend into the
316 // parent's transparent region. In fact one of the main uses, is to reduce
317 // buffer allocation size in cases where a child window sits behind a main window
318 // (by marking the hole in the parent window as a transparent region)
319 if (p != nullptr) {
320 Rect bounds = p->computeScreenBounds(false);
321 bounds.intersect(win, &win);
322 }
323
324 if (reduceTransparentRegion) {
325 auto const screenTransparentRegion = t.transform(s.activeTransparentRegion);
326 win = reduce(win, screenTransparentRegion);
327 }
328
329 return win;
330}
331
Dan Stoza80d61162017-12-20 15:57:52 -0800332FloatRect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700333 const Layer::State& s(getDrawingState());
Michael Lentine6c925ed2014-09-26 17:55:01 -0700334 return computeBounds(s.activeTransparentRegion);
335}
336
Dan Stoza80d61162017-12-20 15:57:52 -0800337FloatRect Layer::computeBounds(const Region& activeTransparentRegion) const {
Michael Lentine6c925ed2014-09-26 17:55:01 -0700338 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800339 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700340
341 if (!s.crop.isEmpty()) {
342 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800343 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700344
345 Rect bounds = win;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700346 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700347 if (p != nullptr) {
Robert Carrde9ec442017-02-08 17:43:36 -0800348 // Look in computeScreenBounds recursive call for explanation of
349 // why we pass false here.
350 bounds = p->computeScreenBounds(false /* reduceTransparentRegion */);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700351 }
352
353 Transform t = getTransform();
Dan Stoza80d61162017-12-20 15:57:52 -0800354
355 FloatRect floatWin = win.toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700356 if (p != nullptr) {
Dan Stoza80d61162017-12-20 15:57:52 -0800357 floatWin = t.transform(floatWin);
358 floatWin = floatWin.intersect(bounds.toFloatRect());
359 floatWin = t.inverse().transform(floatWin);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700360 }
361
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700362 // subtract the transparent region and snap to the bounds
Dan Stoza80d61162017-12-20 15:57:52 -0800363 return reduce(floatWin, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800364}
365
Robert Carr1f0a16a2016-10-24 16:27:39 -0700366Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& hw) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700367 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800368 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700369 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800370
371 // apply the projection's clipping to the window crop in
372 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700373 // if there are no window scaling involved, this operation will map to full
374 // pixels in the buffer.
375 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
376 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700377
378 Rect activeCrop(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700379 if (!s.crop.isEmpty()) {
Chia-I Wudf7867f2017-07-20 14:24:37 -0700380 activeCrop.intersect(s.crop, &activeCrop);
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700381 }
382
Robert Carr1f0a16a2016-10-24 16:27:39 -0700383 Transform t = getTransform();
384 activeCrop = t.transform(activeCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000385 if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
386 activeCrop.clear();
387 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700388 if (!s.finalCrop.isEmpty()) {
David Sodman41fdfc92017-11-06 16:09:56 -0800389 if (!activeCrop.intersect(s.finalCrop, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000390 activeCrop.clear();
391 }
392 }
chaviwb1154d12017-10-31 14:15:36 -0700393
394 const auto& p = mDrawingParent.promote();
395 if (p != nullptr) {
396 auto parentCrop = p->computeInitialCrop(hw);
397 activeCrop.intersect(parentCrop, &activeCrop);
398 }
399
Robert Carr1f0a16a2016-10-24 16:27:39 -0700400 return activeCrop;
401}
402
Dan Stoza5a423ea2017-02-16 14:10:39 -0800403FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700404 // the content crop is the area of the content that gets scaled to the
405 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800406 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700407
408 // In addition there is a WM-specified crop we pull from our drawing state.
409 const State& s(getDrawingState());
410
411 // Screen space to make reduction to parent crop clearer.
412 Rect activeCrop = computeInitialCrop(hw);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700413 Transform t = getTransform();
414 // Back to layer space to work with the content crop.
415 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800416
Michael Lentine28ea2172014-11-19 18:32:37 -0800417 // This needs to be here as transform.transform(Rect) computes the
418 // transformed rect and then takes the bounding box of the result before
419 // returning. This means
420 // transform.inverse().transform(transform.transform(Rect)) != Rect
421 // in which case we need to make sure the final rect is clipped to the
422 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000423 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
424 activeCrop.clear();
425 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800426
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700427 // subtract the transparent region and snap to the bounds
428 activeCrop = reduce(activeCrop, s.activeTransparentRegion);
429
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000430 // Transform the window crop to match the buffer coordinate system,
431 // which means using the inverse of the current transform set on the
432 // SurfaceFlingerConsumer.
433 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700434 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000435 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700436 * the code below applies the primary display's inverse transform to the
437 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000438 */
David Sodman41fdfc92017-11-06 16:09:56 -0800439 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000440 // calculate the inverse transform
441 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800442 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800443 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000444 // and apply to the current transform
David Sodman41fdfc92017-11-06 16:09:56 -0800445 invTransform = (Transform(invTransformOrient) * Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800446 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000447
448 int winWidth = s.active.w;
449 int winHeight = s.active.h;
450 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
451 // If the activeCrop has been rotate the ends are rotated but not
452 // the space itself so when transforming ends back we can't rely on
453 // a modification of the axes of rotation. To account for this we
454 // need to reorient the inverse rotation in terms of the current
455 // axes of rotation.
456 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
457 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
458 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800459 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000460 }
461 winWidth = s.active.h;
462 winHeight = s.active.w;
463 }
David Sodman41fdfc92017-11-06 16:09:56 -0800464 const Rect winCrop = activeCrop.transform(invTransform, s.active.w, s.active.h);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000465
466 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800467 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000468 float yScale = crop.getHeight() / float(winHeight);
469
David Sodman41fdfc92017-11-06 16:09:56 -0800470 float insetL = winCrop.left * xScale;
471 float insetT = winCrop.top * yScale;
472 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000473 float insetB = (winHeight - winCrop.bottom) * yScale;
474
David Sodman41fdfc92017-11-06 16:09:56 -0800475 crop.left += insetL;
476 crop.top += insetT;
477 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000478 crop.bottom -= insetB;
479
Mathias Agopian13127d82013-03-05 17:47:11 -0800480 return crop;
481}
482
Robert Carrae060832016-11-28 10:51:00 -0800483void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice, uint32_t z)
Mathias Agopiana350ff92010-08-10 17:14:02 -0700484{
Dan Stoza9e56aa02015-11-02 13:00:03 -0800485 const auto hwcId = displayDevice->getHwcDisplayId();
David Sodman6f65f3e2017-11-03 14:28:09 -0700486 auto& hwcInfo = getBE().mHwcLayers[hwcId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700487
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700488 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800489 hwcInfo.forceClientComposition = false;
490
491 if (isSecure() && !displayDevice->isSecure()) {
492 hwcInfo.forceClientComposition = true;
493 }
494
Chia-I Wu30505fb2018-03-26 16:20:31 -0700495 auto& hwcLayer = hwcInfo.layer;
496
Mathias Agopian13127d82013-03-05 17:47:11 -0800497 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700498 const State& s(getDrawingState());
David Revemanecf0fa52017-03-03 11:32:44 -0500499 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700500 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800501 blendMode =
502 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800503 }
Chia-I Wu30505fb2018-03-26 16:20:31 -0700504 auto error = (*hwcLayer)->setBlendMode(blendMode);
505 ALOGE_IF(error != HWC2::Error::None,
506 "[%s] Failed to set blend mode %s:"
507 " %s (%d)",
508 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
509 static_cast<int32_t>(error));
Mathias Agopian13127d82013-03-05 17:47:11 -0800510
511 // apply the layer's transform, followed by the display's global transform
512 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700513 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700514 Transform t = getTransform();
Robert Carrb5d3d262016-03-25 15:08:13 -0700515 if (!s.crop.isEmpty()) {
516 Rect activeCrop(s.crop);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700517 activeCrop = t.transform(activeCrop);
David Sodman41fdfc92017-11-06 16:09:56 -0800518 if (!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000519 activeCrop.clear();
520 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700521 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800522 // This needs to be here as transform.transform(Rect) computes the
523 // transformed rect and then takes the bounding box of the result before
524 // returning. This means
525 // transform.inverse().transform(transform.transform(Rect)) != Rect
526 // in which case we need to make sure the final rect is clipped to the
527 // display bounds.
David Sodman41fdfc92017-11-06 16:09:56 -0800528 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000529 activeCrop.clear();
530 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700531 // mark regions outside the crop as transparent
532 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
David Sodman41fdfc92017-11-06 16:09:56 -0800533 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom, s.active.w, s.active.h));
534 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
535 activeTransparentRegion.orSelf(
536 Rect(activeCrop.right, activeCrop.top, s.active.w, activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700537 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700538
Dan Stoza80d61162017-12-20 15:57:52 -0800539 // computeBounds returns a FloatRect to provide more accuracy during the
540 // transformation. We then round upon constructing 'frame'.
541 Rect frame{t.transform(computeBounds(activeTransparentRegion))};
Robert Carrb5d3d262016-03-25 15:08:13 -0700542 if (!s.finalCrop.isEmpty()) {
David Sodman41fdfc92017-11-06 16:09:56 -0800543 if (!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000544 frame.clear();
545 }
546 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000547 if (!frame.intersect(displayDevice->getViewport(), &frame)) {
548 frame.clear();
549 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800550 const Transform& tr(displayDevice->getTransform());
551 Rect transformedFrame = tr.transform(frame);
Chia-I Wu30505fb2018-03-26 16:20:31 -0700552 error = (*hwcLayer)->setDisplayFrame(transformedFrame);
553 if (error != HWC2::Error::None) {
554 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
555 transformedFrame.left, transformedFrame.top, transformedFrame.right,
556 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
557 } else {
558 hwcInfo.displayFrame = transformedFrame;
559 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800560
Dan Stoza5a423ea2017-02-16 14:10:39 -0800561 FloatRect sourceCrop = computeCrop(displayDevice);
Chia-I Wu30505fb2018-03-26 16:20:31 -0700562 error = (*hwcLayer)->setSourceCrop(sourceCrop);
563 if (error != HWC2::Error::None) {
564 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
565 "%s (%d)",
566 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
567 to_string(error).c_str(), static_cast<int32_t>(error));
568 } else {
569 hwcInfo.sourceCrop = sourceCrop;
570 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800571
chaviw13fdc492017-06-27 12:40:18 -0700572 float alpha = static_cast<float>(getAlpha());
Chia-I Wu30505fb2018-03-26 16:20:31 -0700573 error = (*hwcLayer)->setPlaneAlpha(alpha);
574 ALOGE_IF(error != HWC2::Error::None,
575 "[%s] Failed to set plane alpha %.3f: "
576 "%s (%d)",
577 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800578
Chia-I Wu30505fb2018-03-26 16:20:31 -0700579 error = (*hwcLayer)->setZOrder(z);
580 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
581 to_string(error).c_str(), static_cast<int32_t>(error));
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500582
Albert Chaulk2a589632017-05-04 16:59:44 -0400583 int type = s.type;
584 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700585 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400586 if (parent.get()) {
587 auto& parentState = parent->getDrawingState();
rongliucfb187b2018-03-14 12:26:23 -0700588 if (parentState.type >= 0 || parentState.appId >= 0) {
589 type = parentState.type;
590 appId = parentState.appId;
591 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400592 }
593
Chia-I Wu30505fb2018-03-26 16:20:31 -0700594 error = (*hwcLayer)->setInfo(type, appId);
595 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
596 static_cast<int32_t>(error));
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800597
Mathias Agopian29a367b2011-07-12 14:51:45 -0700598 /*
599 * Transformations are applied in this order:
600 * 1) buffer orientation/flip/mirror
601 * 2) state transformation (window manager)
602 * 3) layer orientation (screen orientation)
603 * (NOTE: the matrices are multiplied in reverse order)
604 */
605
606 const Transform bufferOrientation(mCurrentTransform);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700607 Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700608
Robert Carrcae605c2017-03-29 12:10:31 -0700609 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700610 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700611 * the code below applies the primary display's inverse transform to the
612 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700613 */
David Sodman41fdfc92017-11-06 16:09:56 -0800614 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700615 // calculate the inverse transform
616 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800617 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700618 }
Robert Carrcae605c2017-03-29 12:10:31 -0700619
620 /*
621 * Here we cancel out the orientation component of the WM transform.
622 * The scaling and translate components are already included in our bounds
623 * computation so it's enough to just omit it in the composition.
624 * See comment in onDraw with ref to b/36727915 for why.
625 */
626 transform = Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700627 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700628
629 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800630 const uint32_t orientation = transform.getOrientation();
Jorim Jaggif3bd94a2018-03-27 15:38:03 +0200631 if (orientation & Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800632 // we can only handle simple transformation
Chia-I Wu30505fb2018-03-26 16:20:31 -0700633 hwcInfo.forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800634 } else {
635 auto transform = static_cast<HWC2::Transform>(orientation);
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800636 hwcInfo.transform = transform;
Chia-I Wu30505fb2018-03-26 16:20:31 -0700637 auto error = (*hwcLayer)->setTransform(transform);
638 ALOGE_IF(error != HWC2::Error::None,
639 "[%s] Failed to set transform %s: "
640 "%s (%d)",
641 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
642 static_cast<int32_t>(error));
David Sodman4b7c4bc2017-11-17 12:13:59 -0800643 }
644}
645
Dan Stoza9e56aa02015-11-02 13:00:03 -0800646void Layer::forceClientComposition(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700647 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800648 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
649 return;
650 }
651
David Sodman6f65f3e2017-11-03 14:28:09 -0700652 getBE().mHwcLayers[hwcId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800653}
Dan Stozaee44edd2015-03-23 15:50:23 -0700654
chaviwc9232ed2017-11-14 15:31:15 -0800655bool Layer::getForceClientComposition(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700656 if (getBE().mHwcLayers.count(hwcId) == 0) {
chaviwc9232ed2017-11-14 15:31:15 -0800657 ALOGE("getForceClientComposition: no HWC layer found (%d)", hwcId);
658 return false;
659 }
660
David Sodman6f65f3e2017-11-03 14:28:09 -0700661 return getBE().mHwcLayers[hwcId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800662}
663
Dan Stoza9e56aa02015-11-02 13:00:03 -0800664void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
665 auto hwcId = displayDevice->getHwcDisplayId();
David Sodman6f65f3e2017-11-03 14:28:09 -0700666 if (getBE().mHwcLayers.count(hwcId) == 0 ||
David Sodman9eeae692017-11-02 10:53:32 -0700667 getCompositionType(hwcId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800668 return;
669 }
670
671 // This gives us only the "orientation" component of the transform
672 const State& s(getCurrentState());
673
674 // Apply the layer's transform, followed by the display's global transform
675 // Here we're guaranteed that the layer's transform preserves rects
676 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700677 if (!s.crop.isEmpty()) {
678 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800679 }
680 // Subtract the transparent region and snap to the bounds
681 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700682 Rect frame(getTransform().transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800683 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700684 if (!s.finalCrop.isEmpty()) {
685 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000686 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800687 auto& displayTransform(displayDevice->getTransform());
688 auto position = displayTransform.transform(frame);
689
David Sodman5d89c1d2017-12-14 15:54:51 -0800690 auto error = (*getBE().mHwcLayers[hwcId].layer)->setCursorPosition(position.left,
David Sodman9eeae692017-11-02 10:53:32 -0700691 position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800692 ALOGE_IF(error != HWC2::Error::None,
693 "[%s] Failed to set cursor position "
694 "to (%d, %d): %s (%d)",
695 mName.string(), position.left, position.top, to_string(error).c_str(),
696 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800697}
Riley Andrews03414a12014-07-01 14:22:59 -0700698
Mathias Agopian13127d82013-03-05 17:47:11 -0800699// ---------------------------------------------------------------------------
700// drawing...
701// ---------------------------------------------------------------------------
702
chaviwa76b2712017-09-20 12:02:26 -0700703void Layer::draw(const RenderArea& renderArea, const Region& clip) const {
704 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800705}
706
chaviwa76b2712017-09-20 12:02:26 -0700707void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) const {
708 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800709}
710
chaviwa76b2712017-09-20 12:02:26 -0700711void Layer::draw(const RenderArea& renderArea) const {
712 onDraw(renderArea, Region(renderArea.getBounds()), false);
Dan Stozac7014012014-02-14 15:03:43 -0800713}
714
David Sodman41fdfc92017-11-06 16:09:56 -0800715void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
716 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800717 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700718 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700719 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700720 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800721}
722
chaviwa76b2712017-09-20 12:02:26 -0700723void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
David Sodman41fdfc92017-11-06 16:09:56 -0800724 clearWithOpenGL(renderArea, 0, 0, 0, 0);
Mathias Agopian13127d82013-03-05 17:47:11 -0800725}
726
Chia-I Wu30505fb2018-03-26 16:20:31 -0700727void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type, bool callIntoHwc) {
728 if (getBE().mHwcLayers.count(hwcId) == 0) {
729 ALOGE("setCompositionType called without a valid HWC layer");
730 return;
731 }
732 auto& hwcInfo = getBE().mHwcLayers[hwcId];
733 auto& hwcLayer = hwcInfo.layer;
734 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", (*hwcLayer)->getId(), to_string(type).c_str(),
735 static_cast<int>(callIntoHwc));
736 if (hwcInfo.compositionType != type) {
737 ALOGV(" actually setting");
738 hwcInfo.compositionType = type;
739 if (callIntoHwc) {
740 auto error = (*hwcLayer)->setCompositionType(type);
741 ALOGE_IF(error != HWC2::Error::None,
742 "[%s] Failed to set "
743 "composition type %s: %s (%d)",
744 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
745 static_cast<int32_t>(error));
746 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800747 }
748}
749
750HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700751 if (hwcId == DisplayDevice::DISPLAY_ID_INVALID) {
752 // If we're querying the composition type for a display that does not
753 // have a HWC counterpart, then it will always be Client
754 return HWC2::Composition::Client;
755 }
Chia-I Wu30505fb2018-03-26 16:20:31 -0700756 if (getBE().mHwcLayers.count(hwcId) == 0) {
757 ALOGE("getCompositionType called with an invalid HWC layer");
758 return HWC2::Composition::Invalid;
759 }
760 return getBE().mHwcLayers.at(hwcId).compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800761}
762
763void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700764 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800765 ALOGE("setClearClientTarget called without a valid HWC layer");
766 return;
767 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700768 getBE().mHwcLayers[hwcId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800769}
770
771bool Layer::getClearClientTarget(int32_t hwcId) const {
David Sodman6f65f3e2017-11-03 14:28:09 -0700772 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800773 ALOGE("getClearClientTarget called without a valid HWC layer");
774 return false;
775 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700776 return getBE().mHwcLayers.at(hwcId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800777}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800778
Dan Stozacac35382016-01-27 12:21:06 -0800779bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
780 if (point->getFrameNumber() <= mCurrentFrameNumber) {
781 // Don't bother with a SyncPoint, since we've already latched the
782 // relevant frame
783 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700784 }
785
Dan Stozacac35382016-01-27 12:21:06 -0800786 Mutex::Autolock lock(mLocalSyncPointMutex);
787 mLocalSyncPoints.push_back(point);
788 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700789}
790
Mathias Agopian13127d82013-03-05 17:47:11 -0800791void Layer::setFiltering(bool filtering) {
792 mFiltering = filtering;
793}
794
795bool Layer::getFiltering() const {
796 return mFiltering;
797}
798
Mathias Agopian13127d82013-03-05 17:47:11 -0800799// ----------------------------------------------------------------------------
800// local state
801// ----------------------------------------------------------------------------
802
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000803static void boundPoint(vec2* point, const Rect& crop) {
804 if (point->x < crop.left) {
805 point->x = crop.left;
806 }
807 if (point->x > crop.right) {
808 point->x = crop.right;
809 }
810 if (point->y < crop.top) {
811 point->y = crop.top;
812 }
813 if (point->y > crop.bottom) {
814 point->y = crop.bottom;
815 }
816}
817
chaviwa76b2712017-09-20 12:02:26 -0700818void Layer::computeGeometry(const RenderArea& renderArea, Mesh& mesh,
819 bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700820 const Layer::State& s(getDrawingState());
chaviwa76b2712017-09-20 12:02:26 -0700821 const Transform renderAreaTransform(renderArea.getTransform());
822 const uint32_t height = renderArea.getHeight();
Dan Stoza80d61162017-12-20 15:57:52 -0800823 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700824
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000825 vec2 lt = vec2(win.left, win.top);
826 vec2 lb = vec2(win.left, win.bottom);
827 vec2 rb = vec2(win.right, win.bottom);
828 vec2 rt = vec2(win.right, win.top);
829
Robert Carr1f0a16a2016-10-24 16:27:39 -0700830 Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000831 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700832 lt = layerTransform.transform(lt);
833 lb = layerTransform.transform(lb);
834 rb = layerTransform.transform(rb);
835 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000836 }
837
Robert Carrb5d3d262016-03-25 15:08:13 -0700838 if (!s.finalCrop.isEmpty()) {
839 boundPoint(&lt, s.finalCrop);
840 boundPoint(&lb, s.finalCrop);
841 boundPoint(&rb, s.finalCrop);
842 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000843 }
844
Mathias Agopianff2ed702013-09-01 21:36:12 -0700845 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700846 position[0] = renderAreaTransform.transform(lt);
847 position[1] = renderAreaTransform.transform(lb);
848 position[2] = renderAreaTransform.transform(rb);
849 position[3] = renderAreaTransform.transform(rt);
David Sodman41fdfc92017-11-06 16:09:56 -0800850 for (size_t i = 0; i < 4; i++) {
chaviwa76b2712017-09-20 12:02:26 -0700851 position[i].y = height - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -0800852 }
853}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800854
David Sodman41fdfc92017-11-06 16:09:56 -0800855bool Layer::isSecure() const {
Dan Stoza23116082015-06-18 14:58:39 -0700856 const Layer::State& s(mDrawingState);
857 return (s.flags & layer_state_t::eLayerSecure);
858}
859
Mathias Agopian13127d82013-03-05 17:47:11 -0800860void Layer::setVisibleRegion(const Region& visibleRegion) {
861 // always called from main thread
862 this->visibleRegion = visibleRegion;
863}
864
865void Layer::setCoveredRegion(const Region& coveredRegion) {
866 // always called from main thread
867 this->coveredRegion = coveredRegion;
868}
869
David Sodman41fdfc92017-11-06 16:09:56 -0800870void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800871 // always called from main thread
872 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
873}
874
Robert Carre5f4f692018-01-12 13:12:28 -0800875void Layer::clearVisibilityRegions() {
876 visibleRegion.clear();
877 visibleNonTransparentRegion.clear();
878 coveredRegion.clear();
879}
880
Mathias Agopian13127d82013-03-05 17:47:11 -0800881// ----------------------------------------------------------------------------
882// transaction
883// ----------------------------------------------------------------------------
884
Dan Stoza7dde5992015-05-22 09:51:44 -0700885void Layer::pushPendingState() {
886 if (!mCurrentState.modified) {
887 return;
888 }
889
Dan Stoza7dde5992015-05-22 09:51:44 -0700890 // If this transaction is waiting on the receipt of a frame, generate a sync
891 // point and send it to the remote layer.
Robert Carr0d480722017-01-10 16:42:54 -0800892 if (mCurrentState.barrierLayer != nullptr) {
893 sp<Layer> barrierLayer = mCurrentState.barrierLayer.promote();
894 if (barrierLayer == nullptr) {
895 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700896 // If we can't promote the layer we are intended to wait on,
897 // then it is expired or otherwise invalid. Allow this transaction
898 // to be applied as per normal (no synchronization).
Robert Carr0d480722017-01-10 16:42:54 -0800899 mCurrentState.barrierLayer = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800900 } else {
David Sodman41fdfc92017-11-06 16:09:56 -0800901 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber);
Robert Carr0d480722017-01-10 16:42:54 -0800902 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800903 mRemoteSyncPoints.push_back(std::move(syncPoint));
904 } else {
905 // We already missed the frame we're supposed to synchronize
906 // on, so go ahead and apply the state update
Robert Carr0d480722017-01-10 16:42:54 -0800907 mCurrentState.barrierLayer = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800908 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700909 }
910
Dan Stoza7dde5992015-05-22 09:51:44 -0700911 // Wake us up to check if the frame has been received
912 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700913 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700914 }
915 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700916 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700917}
918
Pablo Ceballos05289c22016-04-14 15:49:55 -0700919void Layer::popPendingState(State* stateToCommit) {
920 auto oldFlags = stateToCommit->flags;
921 *stateToCommit = mPendingStates[0];
David Sodman41fdfc92017-11-06 16:09:56 -0800922 stateToCommit->flags =
923 (oldFlags & ~stateToCommit->mask) | (stateToCommit->flags & stateToCommit->mask);
Dan Stoza7dde5992015-05-22 09:51:44 -0700924
925 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700926 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700927}
928
Pablo Ceballos05289c22016-04-14 15:49:55 -0700929bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700930 bool stateUpdateAvailable = false;
931 while (!mPendingStates.empty()) {
Robert Carr0d480722017-01-10 16:42:54 -0800932 if (mPendingStates[0].barrierLayer != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700933 if (mRemoteSyncPoints.empty()) {
934 // If we don't have a sync point for this, apply it anyway. It
935 // will be visually wrong, but it should keep us from getting
936 // into too much trouble.
937 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700938 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700939 stateUpdateAvailable = true;
940 continue;
941 }
942
David Sodman41fdfc92017-11-06 16:09:56 -0800943 if (mRemoteSyncPoints.front()->getFrameNumber() != mPendingStates[0].frameNumber) {
944 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800945
946 // Signal our end of the sync point and then dispose of it
947 mRemoteSyncPoints.front()->setTransactionApplied();
948 mRemoteSyncPoints.pop_front();
949 continue;
950 }
951
Dan Stoza7dde5992015-05-22 09:51:44 -0700952 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
953 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700954 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700955 stateUpdateAvailable = true;
956
957 // Signal our end of the sync point and then dispose of it
958 mRemoteSyncPoints.front()->setTransactionApplied();
959 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800960 } else {
961 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700962 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700963 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700964 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700965 stateUpdateAvailable = true;
966 }
967 }
968
969 // If we still have pending updates, wake SurfaceFlinger back up and point
970 // it at this layer so we can process them
971 if (!mPendingStates.empty()) {
972 setTransactionFlags(eTransactionNeeded);
973 mFlinger->setTransactionFlags(eTraversalNeeded);
974 }
975
976 mCurrentState.modified = false;
977 return stateUpdateAvailable;
978}
979
Mathias Agopian13127d82013-03-05 17:47:11 -0800980uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800981 ATRACE_CALL();
982
Dan Stoza7dde5992015-05-22 09:51:44 -0700983 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -0700984 Layer::State c = getCurrentState();
985 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700986 return 0;
987 }
988
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700989 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800990
David Sodman41fdfc92017-11-06 16:09:56 -0800991 const bool sizeChanged = (c.requested.w != s.requested.w) || (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700992
David Sodmaneb085e02017-10-05 18:49:04 -0700993 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700994 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000995 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800996 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
997 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
998 " requested={ wh={%4u,%4u} }}\n"
999 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1000 " requested={ wh={%4u,%4u} }}\n",
David Sodman9eeae692017-11-02 10:53:32 -07001001 this, getName().string(), mCurrentTransform,
1002 getEffectiveScalingMode(), c.active.w, c.active.h, c.crop.left, c.crop.top,
1003 c.crop.right, c.crop.bottom, c.crop.getWidth(), c.crop.getHeight(), c.requested.w,
1004 c.requested.h, s.active.w, s.active.h, s.crop.left, s.crop.top, s.crop.right,
1005 s.crop.bottom, s.crop.getWidth(), s.crop.getHeight(), s.requested.w,
1006 s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001007
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001008 // record the new size, form this point on, when the client request
1009 // a buffer, it'll get the new size.
David Sodmaneb085e02017-10-05 18:49:04 -07001010 setDefaultBufferSize(c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001011 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001012
Robert Carre392b552017-09-19 12:16:05 -07001013 // Don't let Layer::doTransaction update the drawing state
1014 // if we have a pending resize, unless we are in fixed-size mode.
1015 // the drawing state will be updated only once we receive a buffer
1016 // with the correct size.
1017 //
1018 // In particular, we want to make sure the clip (which is part
1019 // of the geometry state) is latched together with the size but is
1020 // latched immediately when no resizing is involved.
1021 //
1022 // If a sideband stream is attached, however, we want to skip this
1023 // optimization so that transactions aren't missed when a buffer
1024 // never arrives
1025 //
1026 // In the case that we don't have a buffer we ignore other factors
1027 // and avoid entering the resizePending state. At a high level the
1028 // resizePending state is to avoid applying the state of the new buffer
1029 // to the old buffer. However in the state where we don't have an old buffer
1030 // there is no such concern but we may still be being used as a parent layer.
David Sodman41fdfc92017-11-06 16:09:56 -08001031 const bool resizePending = ((c.requested.w != c.active.w) || (c.requested.h != c.active.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -08001032 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001033 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -08001034 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001035 flags |= eDontUpdateGeometryState;
1036 }
1037 }
1038
Robert Carr7bf247e2017-05-18 14:02:49 -07001039 // Here we apply various requested geometry states, depending on our
1040 // latching configuration. See Layer.h for a detailed discussion of
1041 // how geometry latching is controlled.
1042 if (!(flags & eDontUpdateGeometryState)) {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001043 Layer::State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001044
1045 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1046 // mode, which causes attributes which normally latch regardless of scaling mode,
1047 // to be delayed. We copy the requested state to the active state making sure
1048 // to respect these rules (again see Layer.h for a detailed discussion).
1049 //
1050 // There is an awkward asymmetry in the handling of the crop states in the position
1051 // states, as can be seen below. Largely this arises from position and transform
1052 // being stored in the same data structure while having different latching rules.
1053 // b/38182305
1054 //
1055 // Careful that "c" and editCurrentState may not begin as equivalent due to
1056 // applyPendingStates in the presence of deferred transactions.
1057 if (mFreezeGeometryUpdates) {
Robert Carr82364e32016-05-15 11:27:47 -07001058 float tx = c.active.transform.tx();
1059 float ty = c.active.transform.ty();
1060 c.active = c.requested;
1061 c.active.transform.set(tx, ty);
1062 editCurrentState.active = c.active;
1063 } else {
1064 editCurrentState.active = editCurrentState.requested;
1065 c.active = c.requested;
1066 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001067 }
1068
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001069 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001070 // invalidate and recompute the visible regions if needed
1071 flags |= Layer::eVisibleRegion;
1072 }
1073
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001074 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001075 // invalidate and recompute the visible regions if needed
1076 flags |= eVisibleRegion;
1077 this->contentDirty = true;
1078
1079 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001080 const uint8_t type = c.active.transform.getType();
David Sodman41fdfc92017-11-06 16:09:56 -08001081 mNeedsFiltering = (!c.active.transform.preserveRects() || (type >= Transform::SCALE));
Mathias Agopian13127d82013-03-05 17:47:11 -08001082 }
1083
Dan Stozac8145172016-04-28 16:29:06 -07001084 // If the layer is hidden, signal and clear out all local sync points so
1085 // that transactions for layers depending on this layer's frames becoming
1086 // visible are not blocked
1087 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001088 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001089 }
1090
Mathias Agopian13127d82013-03-05 17:47:11 -08001091 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001092 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001093 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001094}
1095
Pablo Ceballos05289c22016-04-14 15:49:55 -07001096void Layer::commitTransaction(const State& stateToCommit) {
1097 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001098}
1099
Mathias Agopian13127d82013-03-05 17:47:11 -08001100uint32_t Layer::getTransactionFlags(uint32_t flags) {
1101 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1102}
1103
1104uint32_t Layer::setTransactionFlags(uint32_t flags) {
1105 return android_atomic_or(flags, &mTransactionFlags);
1106}
1107
Robert Carr82364e32016-05-15 11:27:47 -07001108bool Layer::setPosition(float x, float y, bool immediate) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001109 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001110 return false;
1111 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001112
1113 // We update the requested and active position simultaneously because
1114 // we want to apply the position portion of the transform matrix immediately,
1115 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001116 mCurrentState.requested.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001117 if (immediate && !mFreezeGeometryUpdates) {
1118 // Here we directly update the active state
1119 // unlike other setters, because we store it within
1120 // the transform, but use different latching rules.
1121 // b/38182305
Robert Carr82364e32016-05-15 11:27:47 -07001122 mCurrentState.active.transform.set(x, y);
1123 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001124 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001125
Dan Stoza7dde5992015-05-22 09:51:44 -07001126 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001127 setTransactionFlags(eTransactionNeeded);
1128 return true;
1129}
Robert Carr82364e32016-05-15 11:27:47 -07001130
Robert Carr1f0a16a2016-10-24 16:27:39 -07001131bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1132 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1133 if (idx < 0) {
1134 return false;
1135 }
1136 if (childLayer->setLayer(z)) {
1137 mCurrentChildren.removeAt(idx);
1138 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001139 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001140 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001141 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001142}
1143
Robert Carr503c7042017-09-27 15:06:08 -07001144bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1145 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1146 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1147 if (idx < 0) {
1148 return false;
1149 }
1150 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1151 mCurrentChildren.removeAt(idx);
1152 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001153 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001154 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001155 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001156}
1157
Robert Carrae060832016-11-28 10:51:00 -08001158bool Layer::setLayer(int32_t z) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001159 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001160 mCurrentState.sequence++;
1161 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001162 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001163
1164 // Discard all relative layering.
1165 if (mCurrentState.zOrderRelativeOf != nullptr) {
1166 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1167 if (strongRelative != nullptr) {
1168 strongRelative->removeZOrderRelative(this);
1169 }
1170 mCurrentState.zOrderRelativeOf = nullptr;
1171 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001172 setTransactionFlags(eTransactionNeeded);
1173 return true;
1174}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001175
Robert Carrdb66e622017-04-10 16:55:57 -07001176void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1177 mCurrentState.zOrderRelatives.remove(relative);
1178 mCurrentState.sequence++;
1179 mCurrentState.modified = true;
1180 setTransactionFlags(eTransactionNeeded);
1181}
1182
1183void Layer::addZOrderRelative(const wp<Layer>& relative) {
1184 mCurrentState.zOrderRelatives.add(relative);
1185 mCurrentState.modified = true;
1186 mCurrentState.sequence++;
1187 setTransactionFlags(eTransactionNeeded);
1188}
1189
Robert Carr503d2bd2017-12-04 15:49:47 -08001190bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001191 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1192 if (handle == nullptr) {
1193 return false;
1194 }
1195 sp<Layer> relative = handle->owner.promote();
1196 if (relative == nullptr) {
1197 return false;
1198 }
1199
Robert Carr503d2bd2017-12-04 15:49:47 -08001200 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1201 mCurrentState.zOrderRelativeOf == relative) {
1202 return false;
1203 }
1204
Robert Carrdb66e622017-04-10 16:55:57 -07001205 mCurrentState.sequence++;
1206 mCurrentState.modified = true;
Robert Carr503d2bd2017-12-04 15:49:47 -08001207 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001208
chaviw9ab4bd12017-11-03 13:11:00 -07001209 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1210 if (oldZOrderRelativeOf != nullptr) {
1211 oldZOrderRelativeOf->removeZOrderRelative(this);
1212 }
Robert Carrdb66e622017-04-10 16:55:57 -07001213 mCurrentState.zOrderRelativeOf = relative;
1214 relative->addZOrderRelative(this);
1215
1216 setTransactionFlags(eTransactionNeeded);
1217
1218 return true;
1219}
1220
Mathias Agopian13127d82013-03-05 17:47:11 -08001221bool Layer::setSize(uint32_t w, uint32_t h) {
David Sodman41fdfc92017-11-06 16:09:56 -08001222 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001223 mCurrentState.requested.w = w;
1224 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001225 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001226 setTransactionFlags(eTransactionNeeded);
1227 return true;
1228}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001229bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001230 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001231 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001232 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001233 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001234 setTransactionFlags(eTransactionNeeded);
1235 return true;
1236}
chaviw13fdc492017-06-27 12:40:18 -07001237
1238bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001239 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1240 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001241 return false;
1242
1243 mCurrentState.sequence++;
1244 mCurrentState.color.r = color.r;
1245 mCurrentState.color.g = color.g;
1246 mCurrentState.color.b = color.b;
1247 mCurrentState.modified = true;
1248 setTransactionFlags(eTransactionNeeded);
1249 return true;
1250}
1251
Mathias Agopian13127d82013-03-05 17:47:11 -08001252bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
1253 mCurrentState.sequence++;
David Sodman41fdfc92017-11-06 16:09:56 -08001254 mCurrentState.requested.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001255 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001256 setTransactionFlags(eTransactionNeeded);
1257 return true;
1258}
1259bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001260 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001261 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001262 setTransactionFlags(eTransactionNeeded);
1263 return true;
1264}
1265bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1266 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001267 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001268 mCurrentState.sequence++;
1269 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001270 mCurrentState.mask = mask;
1271 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001272 setTransactionFlags(eTransactionNeeded);
1273 return true;
1274}
Robert Carr99e27f02016-06-16 15:18:02 -07001275
1276bool Layer::setCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001277 if (mCurrentState.requestedCrop == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001278 mCurrentState.sequence++;
Robert Carr99e27f02016-06-16 15:18:02 -07001279 mCurrentState.requestedCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001280 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr99e27f02016-06-16 15:18:02 -07001281 mCurrentState.crop = crop;
1282 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001283 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1284
Dan Stoza7dde5992015-05-22 09:51:44 -07001285 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001286 setTransactionFlags(eTransactionNeeded);
1287 return true;
1288}
Robert Carr8d5227b2017-03-16 15:41:03 -07001289
1290bool Layer::setFinalCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001291 if (mCurrentState.requestedFinalCrop == crop) return false;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001292 mCurrentState.sequence++;
Robert Carr8d5227b2017-03-16 15:41:03 -07001293 mCurrentState.requestedFinalCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001294 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001295 mCurrentState.finalCrop = crop;
1296 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001297 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1298
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001299 mCurrentState.modified = true;
1300 setTransactionFlags(eTransactionNeeded);
1301 return true;
1302}
Mathias Agopian13127d82013-03-05 17:47:11 -08001303
Robert Carrc3574f72016-03-24 12:19:32 -07001304bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001305 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001306 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001307 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001308 return true;
1309}
1310
rongliucfb187b2018-03-14 12:26:23 -07001311void Layer::setInfo(int32_t type, int32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001312 mCurrentState.appId = appId;
1313 mCurrentState.type = type;
1314 mCurrentState.modified = true;
1315 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001316}
1317
Mathias Agopian13127d82013-03-05 17:47:11 -08001318bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001319 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001320 mCurrentState.sequence++;
1321 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001322 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001323 setTransactionFlags(eTransactionNeeded);
1324 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001325}
1326
Peiyong Lin34beb7a2018-03-28 11:57:12 -07001327bool Layer::setDataSpace(ui::Dataspace dataSpace) {
David Sodman41fdfc92017-11-06 16:09:56 -08001328 if (mCurrentState.dataSpace == dataSpace) return false;
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -07001329 mCurrentState.sequence++;
1330 mCurrentState.dataSpace = dataSpace;
1331 mCurrentState.modified = true;
1332 setTransactionFlags(eTransactionNeeded);
1333 return true;
1334}
1335
Peiyong Lin34beb7a2018-03-28 11:57:12 -07001336ui::Dataspace Layer::getDataSpace() const {
Courtney Goeltzenleuchter532b2632017-05-05 16:34:38 -06001337 return mCurrentState.dataSpace;
1338}
1339
Robert Carr1f0a16a2016-10-24 16:27:39 -07001340uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001341 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001342 if (p == nullptr) {
1343 return getDrawingState().layerStack;
1344 }
1345 return p->getLayerStack();
1346}
1347
David Sodman41fdfc92017-11-06 16:09:56 -08001348void Layer::deferTransactionUntil(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001349 mCurrentState.barrierLayer = barrierLayer;
Dan Stoza7dde5992015-05-22 09:51:44 -07001350 mCurrentState.frameNumber = frameNumber;
1351 // We don't set eTransactionNeeded, because just receiving a deferral
1352 // request without any other state updates shouldn't actually induce a delay
1353 mCurrentState.modified = true;
1354 pushPendingState();
Robert Carr0d480722017-01-10 16:42:54 -08001355 mCurrentState.barrierLayer = nullptr;
Dan Stoza792e5292016-02-11 11:43:58 -08001356 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001357 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001358}
1359
David Sodman41fdfc92017-11-06 16:09:56 -08001360void Layer::deferTransactionUntil(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001361 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
1362 deferTransactionUntil(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001363}
1364
Dan Stozaee44edd2015-03-23 15:50:23 -07001365
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001366// ----------------------------------------------------------------------------
1367// pageflip handling...
1368// ----------------------------------------------------------------------------
1369
Robert Carr1f0a16a2016-10-24 16:27:39 -07001370bool Layer::isHiddenByPolicy() const {
1371 const Layer::State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001372 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001373 if (parent != nullptr && parent->isHiddenByPolicy()) {
1374 return true;
1375 }
1376 return s.flags & layer_state_t::eLayerHidden;
1377}
1378
David Sodman41fdfc92017-11-06 16:09:56 -08001379uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001380 // TODO: should we do something special if mSecure is set?
1381 if (mProtectedByApp) {
1382 // need a hardware-protected path to external video sink
1383 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001384 }
Riley Andrews03414a12014-07-01 14:22:59 -07001385 if (mPotentialCursor) {
1386 usage |= GraphicBuffer::USAGE_CURSOR;
1387 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001388 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001389 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001390}
1391
Mathias Agopian84300952012-11-21 16:02:13 -08001392void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001393 uint32_t orientation = 0;
1394 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001395 // The transform hint is used to improve performance, but we can
1396 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001397 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07001398 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07001399 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07001400 if (orientation & Transform::ROT_INVALID) {
1401 orientation = 0;
1402 }
1403 }
David Sodmaneb085e02017-10-05 18:49:04 -07001404 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001405}
1406
Mathias Agopian13127d82013-03-05 17:47:11 -08001407// ----------------------------------------------------------------------------
1408// debugging
1409// ----------------------------------------------------------------------------
1410
Kalle Raitaa099a242017-01-11 11:17:29 -08001411LayerDebugInfo Layer::getLayerDebugInfo() const {
1412 LayerDebugInfo info;
1413 const Layer::State& ds = getDrawingState();
1414 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001415 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001416 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1417 info.mType = String8(getTypeId());
1418 info.mTransparentRegion = ds.activeTransparentRegion;
1419 info.mVisibleRegion = visibleRegion;
1420 info.mSurfaceDamageRegion = surfaceDamageRegion;
1421 info.mLayerStack = getLayerStack();
1422 info.mX = ds.active.transform.tx();
1423 info.mY = ds.active.transform.ty();
1424 info.mZ = ds.z;
1425 info.mWidth = ds.active.w;
1426 info.mHeight = ds.active.h;
1427 info.mCrop = ds.crop;
1428 info.mFinalCrop = ds.finalCrop;
chaviw13fdc492017-06-27 12:40:18 -07001429 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001430 info.mFlags = ds.flags;
1431 info.mPixelFormat = getPixelFormat();
Peiyong Lin34beb7a2018-03-28 11:57:12 -07001432 info.mDataSpace = static_cast<android_dataspace>(getDataSpace());
Kalle Raitaa099a242017-01-11 11:17:29 -08001433 info.mMatrix[0][0] = ds.active.transform[0][0];
1434 info.mMatrix[0][1] = ds.active.transform[0][1];
1435 info.mMatrix[1][0] = ds.active.transform[1][0];
1436 info.mMatrix[1][1] = ds.active.transform[1][1];
1437 {
David Sodman0cf8f8d2017-12-20 18:19:45 -08001438 sp<const GraphicBuffer> buffer = mActiveBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001439 if (buffer != 0) {
1440 info.mActiveBufferWidth = buffer->getWidth();
1441 info.mActiveBufferHeight = buffer->getHeight();
1442 info.mActiveBufferStride = buffer->getStride();
1443 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001444 } else {
1445 info.mActiveBufferWidth = 0;
1446 info.mActiveBufferHeight = 0;
1447 info.mActiveBufferStride = 0;
1448 info.mActiveBufferFormat = 0;
1449 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001450 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001451 info.mNumQueuedFrames = getQueuedFrameCount();
1452 info.mRefreshPending = isBufferLatched();
1453 info.mIsOpaque = isOpaque(ds);
1454 info.mContentDirty = contentDirty;
1455 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001456}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001457
Dan Stozae22aec72016-08-01 13:20:59 -07001458void Layer::miniDumpHeader(String8& result) {
1459 result.append("----------------------------------------");
1460 result.append("---------------------------------------\n");
1461 result.append(" Layer name\n");
1462 result.append(" Z | ");
1463 result.append(" Comp Type | ");
1464 result.append(" Disp Frame (LTRB) | ");
1465 result.append(" Source Crop (LTRB)\n");
1466 result.append("----------------------------------------");
1467 result.append("---------------------------------------\n");
1468}
1469
1470void Layer::miniDump(String8& result, int32_t hwcId) const {
David Sodman6f65f3e2017-11-03 14:28:09 -07001471 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stozae22aec72016-08-01 13:20:59 -07001472 return;
1473 }
1474
1475 String8 name;
1476 if (mName.length() > 77) {
1477 std::string shortened;
1478 shortened.append(mName.string(), 36);
1479 shortened.append("[...]");
1480 shortened.append(mName.string() + (mName.length() - 36), 36);
1481 name = shortened.c_str();
1482 } else {
1483 name = mName;
1484 }
1485
1486 result.appendFormat(" %s\n", name.string());
1487
1488 const Layer::State& layerState(getDrawingState());
Chia-I Wu30505fb2018-03-26 16:20:31 -07001489 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(hwcId);
Chia-I Wu1e043612018-03-01 09:45:09 -08001490 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
1491 result.appendFormat(" rel %6d | ", layerState.z);
1492 } else {
1493 result.appendFormat(" %10d | ", layerState.z);
1494 }
David Sodman41fdfc92017-11-06 16:09:56 -08001495 result.appendFormat("%10s | ", to_string(getCompositionType(hwcId)).c_str());
Chia-I Wu30505fb2018-03-26 16:20:31 -07001496 const Rect& frame = hwcInfo.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001497 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Chia-I Wu30505fb2018-03-26 16:20:31 -07001498 const FloatRect& crop = hwcInfo.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001499 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 -07001500
1501 result.append("- - - - - - - - - - - - - - - - - - - - ");
1502 result.append("- - - - - - - - - - - - - - - - - - - -\n");
1503}
Dan Stozae22aec72016-08-01 13:20:59 -07001504
Svetoslavd85084b2014-03-20 10:28:31 -07001505void Layer::dumpFrameStats(String8& result) const {
1506 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001507}
1508
Svetoslavd85084b2014-03-20 10:28:31 -07001509void Layer::clearFrameStats() {
1510 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001511}
1512
Jamie Gennis6547ff42013-07-16 20:12:42 -07001513void Layer::logFrameStats() {
1514 mFrameTracker.logAndResetStats(mName);
1515}
1516
Svetoslavd85084b2014-03-20 10:28:31 -07001517void Layer::getFrameStats(FrameStats* outStats) const {
1518 mFrameTracker.getStats(outStats);
1519}
1520
Brian Andersond6927fb2016-07-23 23:37:30 -07001521void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001522 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001523 Mutex::Autolock lock(mFrameEventHistoryMutex);
1524 mFrameEventHistory.checkFencesForCompletion();
1525 mFrameEventHistory.dump(result);
1526}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001527
Brian Anderson5ea5e592016-12-01 16:54:33 -08001528void Layer::onDisconnect() {
1529 Mutex::Autolock lock(mFrameEventHistoryMutex);
1530 mFrameEventHistory.onDisconnect();
1531}
1532
Brian Anderson3890c392016-07-25 12:48:08 -07001533void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001534 FrameEventHistoryDelta* outDelta) {
Brian Andersond6927fb2016-07-23 23:37:30 -07001535 Mutex::Autolock lock(mFrameEventHistoryMutex);
1536 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001537 // If there are any unsignaled fences in the aquire timeline at this
1538 // point, the previously queued frame hasn't been latched yet. Go ahead
1539 // and try to get the signal time here so the syscall is taken out of
1540 // the main thread's critical path.
1541 mAcquireTimeline.updateSignalTimes();
1542 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001543 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001544 mFrameEventHistory.addQueue(*newTimestamps);
1545 }
1546
Brian Anderson3890c392016-07-25 12:48:08 -07001547 if (outDelta) {
1548 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001549 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001550}
Dan Stozae77c7662016-05-13 11:37:28 -07001551
Chia-I Wu98f1c102017-05-30 14:54:08 -07001552size_t Layer::getChildrenCount() const {
1553 size_t count = 0;
1554 for (const sp<Layer>& child : mCurrentChildren) {
1555 count += 1 + child->getChildrenCount();
1556 }
1557 return count;
1558}
1559
Robert Carr1f0a16a2016-10-24 16:27:39 -07001560void Layer::addChild(const sp<Layer>& layer) {
1561 mCurrentChildren.add(layer);
1562 layer->setParent(this);
1563}
1564
1565ssize_t Layer::removeChild(const sp<Layer>& layer) {
1566 layer->setParent(nullptr);
1567 return mCurrentChildren.remove(layer);
1568}
1569
Robert Carr1db73f62016-12-21 12:58:51 -08001570bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1571 sp<Handle> handle = nullptr;
1572 sp<Layer> newParent = nullptr;
1573 if (newParentHandle == nullptr) {
1574 return false;
1575 }
1576 handle = static_cast<Handle*>(newParentHandle.get());
1577 newParent = handle->owner.promote();
1578 if (newParent == nullptr) {
1579 ALOGE("Unable to promote Layer handle");
1580 return false;
1581 }
1582
1583 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001584 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001585
1586 sp<Client> client(child->mClientRef.promote());
1587 if (client != nullptr) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001588 client->updateParent(newParent);
Robert Carr1db73f62016-12-21 12:58:51 -08001589 }
1590 }
1591 mCurrentChildren.clear();
1592
1593 return true;
1594}
1595
Robert Carr15eae092018-03-23 13:43:53 -07001596void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001597 for (const sp<Layer>& child : mDrawingChildren) {
1598 child->mDrawingParent = newParent;
1599 }
1600}
1601
chaviwf1961f72017-09-18 16:41:07 -07001602bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1603 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001604 return false;
1605 }
1606
1607 auto handle = static_cast<Handle*>(newParentHandle.get());
1608 sp<Layer> newParent = handle->owner.promote();
1609 if (newParent == nullptr) {
1610 ALOGE("Unable to promote Layer handle");
1611 return false;
1612 }
1613
chaviwf1961f72017-09-18 16:41:07 -07001614 sp<Layer> parent = getParent();
1615 if (parent != nullptr) {
1616 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001617 }
chaviwf1961f72017-09-18 16:41:07 -07001618 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001619
chaviwf1961f72017-09-18 16:41:07 -07001620 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001621 sp<Client> newParentClient(newParent->mClientRef.promote());
1622
chaviwf1961f72017-09-18 16:41:07 -07001623 if (client != newParentClient) {
Robert Carr94c7d3d2018-04-24 12:30:47 -07001624 client->updateParent(newParent);
chaviw06178942017-07-27 10:25:59 -07001625 }
1626
chaviw06178942017-07-27 10:25:59 -07001627 return true;
1628}
1629
Robert Carr9524cb32017-02-13 11:32:32 -08001630bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001631 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001632 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001633 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001634 if (client != nullptr && parentClient != client) {
Robert Carr7f619b22017-11-06 12:56:35 -08001635 client->detachLayer(child.get());
1636 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001637 }
Robert Carr7f619b22017-11-06 12:56:35 -08001638 }
Robert Carr9524cb32017-02-13 11:32:32 -08001639
1640 return true;
1641}
1642
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001643bool Layer::isLegacySrgbDataSpace() const {
Peiyong Lin923e7c52018-04-16 14:16:37 -07001644 return mDrawingState.dataSpace == ui::Dataspace::SRGB ||
1645 mDrawingState.dataSpace == ui::Dataspace::SRGB_LINEAR;
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001646}
1647
Robert Carr1f0a16a2016-10-24 16:27:39 -07001648void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001649 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001650}
1651
1652void Layer::clearSyncPoints() {
1653 for (const auto& child : mCurrentChildren) {
1654 child->clearSyncPoints();
1655 }
1656
1657 Mutex::Autolock lock(mLocalSyncPointMutex);
1658 for (auto& point : mLocalSyncPoints) {
1659 point->setFrameAvailable();
1660 }
1661 mLocalSyncPoints.clear();
1662}
1663
1664int32_t Layer::getZ() const {
1665 return mDrawingState.z;
1666}
1667
Robert Carr29abff82017-12-04 13:51:20 -08001668bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1669 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1670 const State& state = useDrawing ? mDrawingState : mCurrentState;
1671 return state.zOrderRelativeOf != nullptr;
1672}
1673
David Sodman41fdfc92017-11-06 16:09:56 -08001674__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001675 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001676 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1677 "makeTraversalList received invalid stateSet");
1678 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1679 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1680 const State& state = useDrawing ? mDrawingState : mCurrentState;
1681
Robert Carr29abff82017-12-04 13:51:20 -08001682 if (state.zOrderRelatives.size() == 0) {
1683 *outSkipRelativeZUsers = true;
1684 return children;
1685 }
1686
Robert Carrdb66e622017-04-10 16:55:57 -07001687 LayerVector traverse;
Dan Stoza412903f2017-04-27 13:42:17 -07001688 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001689 sp<Layer> strongRelative = weakRelative.promote();
1690 if (strongRelative != nullptr) {
1691 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001692 }
1693 }
1694
Dan Stoza412903f2017-04-27 13:42:17 -07001695 for (const sp<Layer>& child : children) {
Robert Carr503c7042017-09-27 15:06:08 -07001696 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1697 if (childState.zOrderRelativeOf != nullptr) {
1698 continue;
1699 }
Robert Carrdb66e622017-04-10 16:55:57 -07001700 traverse.add(child);
1701 }
1702
1703 return traverse;
1704}
1705
Robert Carr1f0a16a2016-10-24 16:27:39 -07001706/**
Robert Carrdb66e622017-04-10 16:55:57 -07001707 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001708 */
Dan Stoza412903f2017-04-27 13:42:17 -07001709void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001710 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1711 // produce a new list for traversing, including our relatives, and not including our children
1712 // who are relatives of another surface. In the case that there are no relative Z,
1713 // makeTraversalList returns our children directly to avoid significant overhead.
1714 // However in this case we need to take the responsibility for filtering children which
1715 // are relatives of another surface here.
1716 bool skipRelativeZUsers = false;
1717 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001718
Robert Carr1f0a16a2016-10-24 16:27:39 -07001719 size_t i = 0;
Robert Carrdb66e622017-04-10 16:55:57 -07001720 for (; i < list.size(); i++) {
1721 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001722 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1723 continue;
1724 }
1725
Robert Carrdb66e622017-04-10 16:55:57 -07001726 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001727 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001728 }
Dan Stoza412903f2017-04-27 13:42:17 -07001729 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001730 }
Robert Carr29abff82017-12-04 13:51:20 -08001731
Dan Stoza412903f2017-04-27 13:42:17 -07001732 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001733 for (; i < list.size(); i++) {
1734 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001735
1736 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1737 continue;
1738 }
Dan Stoza412903f2017-04-27 13:42:17 -07001739 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001740 }
1741}
1742
1743/**
Robert Carrdb66e622017-04-10 16:55:57 -07001744 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001745 */
Dan Stoza412903f2017-04-27 13:42:17 -07001746void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1747 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001748 // See traverseInZOrder for documentation.
1749 bool skipRelativeZUsers = false;
1750 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001751
Robert Carr1f0a16a2016-10-24 16:27:39 -07001752 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001753 for (i = int32_t(list.size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001754 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001755
1756 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1757 continue;
1758 }
1759
Robert Carrdb66e622017-04-10 16:55:57 -07001760 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001761 break;
1762 }
Dan Stoza412903f2017-04-27 13:42:17 -07001763 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001764 }
Dan Stoza412903f2017-04-27 13:42:17 -07001765 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001766 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001767 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001768
1769 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1770 continue;
1771 }
1772
Dan Stoza412903f2017-04-27 13:42:17 -07001773 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001774 }
1775}
1776
chaviw4b129c22018-04-09 16:19:43 -07001777LayerVector Layer::makeChildrenTraversalList(LayerVector::StateSet stateSet,
1778 const std::vector<Layer*>& layersInTree) {
1779 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1780 "makeTraversalList received invalid stateSet");
chaviwa76b2712017-09-20 12:02:26 -07001781 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1782 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
chaviw4b129c22018-04-09 16:19:43 -07001783 const State& state = useDrawing ? mDrawingState : mCurrentState;
1784
1785 LayerVector traverse;
1786 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1787 sp<Layer> strongRelative = weakRelative.promote();
1788 // Only add relative layers that are also descendents of the top most parent of the tree.
1789 // If a relative layer is not a descendent, then it should be ignored.
1790 if (std::binary_search(layersInTree.begin(), layersInTree.end(), strongRelative.get())) {
1791 traverse.add(strongRelative);
1792 }
1793 }
1794
1795 for (const sp<Layer>& child : children) {
1796 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1797 // If a layer has a relativeOf layer, only ignore if the layer it's relative to is a
1798 // descendent of the top most parent of the tree. If it's not a descendent, then just add
1799 // the child here since it won't be added later as a relative.
1800 if (std::binary_search(layersInTree.begin(), layersInTree.end(),
1801 childState.zOrderRelativeOf.promote().get())) {
1802 continue;
1803 }
1804 traverse.add(child);
1805 }
1806
1807 return traverse;
1808}
1809
1810void Layer::traverseChildrenInZOrderInner(const std::vector<Layer*>& layersInTree,
1811 LayerVector::StateSet stateSet,
1812 const LayerVector::Visitor& visitor) {
1813 const LayerVector list = makeChildrenTraversalList(stateSet, layersInTree);
chaviwa76b2712017-09-20 12:02:26 -07001814
1815 size_t i = 0;
chaviw4b129c22018-04-09 16:19:43 -07001816 for (; i < list.size(); i++) {
1817 const auto& relative = list[i];
chaviwa76b2712017-09-20 12:02:26 -07001818 if (relative->getZ() >= 0) {
1819 break;
1820 }
chaviw4b129c22018-04-09 16:19:43 -07001821 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001822 }
chaviw4b129c22018-04-09 16:19:43 -07001823
chaviwa76b2712017-09-20 12:02:26 -07001824 visitor(this);
chaviw4b129c22018-04-09 16:19:43 -07001825 for (; i < list.size(); i++) {
1826 const auto& relative = list[i];
1827 relative->traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
chaviwa76b2712017-09-20 12:02:26 -07001828 }
1829}
1830
chaviw4b129c22018-04-09 16:19:43 -07001831std::vector<Layer*> Layer::getLayersInTree(LayerVector::StateSet stateSet) {
1832 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1833 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1834
1835 std::vector<Layer*> layersInTree = {this};
1836 for (size_t i = 0; i < children.size(); i++) {
1837 const auto& child = children[i];
1838 std::vector<Layer*> childLayers = child->getLayersInTree(stateSet);
1839 layersInTree.insert(layersInTree.end(), childLayers.cbegin(), childLayers.cend());
1840 }
1841
1842 return layersInTree;
1843}
1844
1845void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1846 const LayerVector::Visitor& visitor) {
1847 std::vector<Layer*> layersInTree = getLayersInTree(stateSet);
1848 std::sort(layersInTree.begin(), layersInTree.end());
1849 traverseChildrenInZOrderInner(layersInTree, stateSet, visitor);
1850}
1851
Robert Carr1f0a16a2016-10-24 16:27:39 -07001852Transform Layer::getTransform() const {
1853 Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001854 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001855 if (p != nullptr) {
1856 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001857
1858 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1859 // it isFixedSize) then there may be additional scaling not accounted
1860 // for in the transform. We need to mirror this scaling in child surfaces
1861 // or we will break the contract where WM can treat child surfaces as
1862 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08001863 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001864 int bufferWidth;
1865 int bufferHeight;
1866 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08001867 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
1868 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001869 } else {
David Sodman0cc69182017-11-17 12:12:07 -08001870 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
1871 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001872 }
David Sodman41fdfc92017-11-06 16:09:56 -08001873 float sx = p->getDrawingState().active.w / static_cast<float>(bufferWidth);
1874 float sy = p->getDrawingState().active.h / static_cast<float>(bufferHeight);
Robert Carr9b429f42017-04-17 14:56:57 -07001875 Transform extraParentScaling;
1876 extraParentScaling.set(sx, 0, 0, sy);
1877 t = t * extraParentScaling;
1878 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001879 }
1880 return t * getDrawingState().active.transform;
1881}
1882
chaviw13fdc492017-06-27 12:40:18 -07001883half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001884 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001885
chaviw13fdc492017-06-27 12:40:18 -07001886 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1887 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001888}
Robert Carr6452f122017-03-21 10:41:29 -07001889
chaviw13fdc492017-06-27 12:40:18 -07001890half4 Layer::getColor() const {
1891 const half4 color(getDrawingState().color);
1892 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001893}
Robert Carr6452f122017-03-21 10:41:29 -07001894
Robert Carr1f0a16a2016-10-24 16:27:39 -07001895void Layer::commitChildList() {
1896 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1897 const auto& child = mCurrentChildren[i];
1898 child->commitChildList();
1899 }
1900 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001901 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001902}
1903
chaviw1d044282017-09-27 12:19:28 -07001904void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1905 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1906 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1907 const State& state = useDrawing ? mDrawingState : mCurrentState;
1908
1909 Transform requestedTransform = state.active.transform;
1910 Transform transform = getTransform();
1911
1912 layerInfo->set_id(sequence);
1913 layerInfo->set_name(getName().c_str());
1914 layerInfo->set_type(String8(getTypeId()));
1915
1916 for (const auto& child : children) {
1917 layerInfo->add_children(child->sequence);
1918 }
1919
1920 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1921 sp<Layer> strongRelative = weakRelative.promote();
1922 if (strongRelative != nullptr) {
1923 layerInfo->add_relatives(strongRelative->sequence);
1924 }
1925 }
1926
1927 LayerProtoHelper::writeToProto(state.activeTransparentRegion,
1928 layerInfo->mutable_transparent_region());
1929 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
1930 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
1931
1932 layerInfo->set_layer_stack(getLayerStack());
1933 layerInfo->set_z(state.z);
1934
1935 PositionProto* position = layerInfo->mutable_position();
1936 position->set_x(transform.tx());
1937 position->set_y(transform.ty());
1938
1939 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
1940 requestedPosition->set_x(requestedTransform.tx());
1941 requestedPosition->set_y(requestedTransform.ty());
1942
1943 SizeProto* size = layerInfo->mutable_size();
1944 size->set_w(state.active.w);
1945 size->set_h(state.active.h);
1946
1947 LayerProtoHelper::writeToProto(state.crop, layerInfo->mutable_crop());
1948 LayerProtoHelper::writeToProto(state.finalCrop, layerInfo->mutable_final_crop());
1949
1950 layerInfo->set_is_opaque(isOpaque(state));
1951 layerInfo->set_invalidate(contentDirty);
Peiyong Lin34beb7a2018-03-28 11:57:12 -07001952 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(getDataSpace())));
chaviw1d044282017-09-27 12:19:28 -07001953 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1954 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
1955 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
1956 layerInfo->set_flags(state.flags);
1957
1958 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1959 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
1960
Jorim Jaggi8e0af362017-11-14 16:28:28 +01001961 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07001962 if (parent != nullptr) {
1963 layerInfo->set_parent(parent->sequence);
1964 }
1965
1966 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1967 if (zOrderRelativeOf != nullptr) {
1968 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1969 }
1970
David Sodman0cc69182017-11-17 12:12:07 -08001971 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001972 if (buffer != nullptr) {
1973 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
chaviw1d044282017-09-27 12:19:28 -07001974 }
1975
1976 layerInfo->set_queued_frames(getQueuedFrameCount());
1977 layerInfo->set_refresh_pending(isBufferLatched());
rongliucfb187b2018-03-14 12:26:23 -07001978 layerInfo->set_window_type(state.type);
1979 layerInfo->set_app_id(state.appId);
chaviw1d044282017-09-27 12:19:28 -07001980}
1981
Yiwei Zhang7124ad32018-02-21 13:02:45 -08001982void Layer::writeToProto(LayerProto* layerInfo, int32_t hwcId) {
1983 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
1984
1985 const auto& hwcInfo = getBE().mHwcLayers.at(hwcId);
1986
1987 const Rect& frame = hwcInfo.displayFrame;
1988 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
1989
1990 const FloatRect& crop = hwcInfo.sourceCrop;
1991 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
1992
1993 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
1994 layerInfo->set_hwc_transform(transform);
Yiwei Zhang60d1a192018-03-07 14:52:28 -08001995
1996 const int32_t compositionType = static_cast<int32_t>(hwcInfo.compositionType);
1997 layerInfo->set_hwc_composition_type(compositionType);
1998
1999 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
2000 static_cast<BufferLayer*>(this)->isProtected()) {
2001 layerInfo->set_is_protected(true);
2002 } else {
2003 layerInfo->set_is_protected(false);
2004 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -08002005}
2006
Mathias Agopian13127d82013-03-05 17:47:11 -08002007// ---------------------------------------------------------------------------
2008
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002009}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002010
2011#if defined(__gl_h_)
2012#error "don't include gl/gl.h in this file"
2013#endif
2014
2015#if defined(__gl2_h_)
2016#error "don't include gl2/gl2.h in this file"
2017#endif