blob: 8c0050ee4767e0c6e5d78be839a167786f4ce301 [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>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026
Mathias Agopiana67932f2011-04-20 14:20:59 -070027#include <cutils/compiler.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070028#include <cutils/native_handle.h>
Mathias Agopiana67932f2011-04-20 14:20:59 -070029#include <cutils/properties.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030
31#include <utils/Errors.h>
32#include <utils/Log.h>
Jesse Hall399184a2014-03-03 15:42:54 -080033#include <utils/NativeHandle.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080035#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036
Courtney Goeltzenleuchter36c44dc2017-04-14 09:33:16 -060037#include <ui/DebugUtils.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070038#include <ui/GraphicBuffer.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039#include <ui/PixelFormat.h>
Mathias Agopian9cce3252010-02-09 17:46:37 -080040
Dan Stoza6b9454d2014-11-07 16:00:59 -080041#include <gui/BufferItem.h>
Kalle Raitaa099a242017-01-11 11:17:29 -080042#include <gui/LayerDebugInfo.h>
Mathias Agopian90ac7992012-02-25 18:48:35 -080043#include <gui/Surface.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044
Yiwei Zhang7c64f172018-03-07 14:52:28 -080045#include "BufferLayer.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020046#include "Colorizer.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070047#include "DisplayDevice.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048#include "Layer.h"
Fabien Sanglard7b1563a2016-10-13 12:05:28 -070049#include "LayerRejecter.h"
Dan Stozab9b08832014-03-13 11:55:57 -070050#include "MonitoredProducer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051#include "SurfaceFlinger.h"
David Sodman41fdfc92017-11-06 16:09:56 -080052#include "clz.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
Mathias Agopian1b031492012-06-20 17:51:20 -070054#include "DisplayHardware/HWComposer.h"
55
Mathias Agopian875d8e12013-06-07 15:35:48 -070056#include "RenderEngine/RenderEngine.h"
57
Dan Stozac5da2712016-07-20 15:38:12 -070058#include <mutex>
chaviw1d044282017-09-27 12:19:28 -070059#include "LayerProtoHelper.h"
Dan Stozac5da2712016-07-20 15:38:12 -070060
David Sodman41fdfc92017-11-06 16:09:56 -080061#define DEBUG_RESIZE 0
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063namespace android {
64
David Sodman9eeae692017-11-02 10:53:32 -070065LayerBE::LayerBE()
David Sodman0cc69182017-11-17 12:12:07 -080066 : mMesh(Mesh::TRIANGLE_FAN, 4, 2, 2) {
David Sodman9eeae692017-11-02 10:53:32 -070067}
68
69
Mathias Agopian13127d82013-03-05 17:47:11 -080070int32_t Layer::sSequence = 1;
71
David Sodman41fdfc92017-11-06 16:09:56 -080072Layer::Layer(SurfaceFlinger* flinger, const sp<Client>& client, const String8& name, uint32_t w,
73 uint32_t h, uint32_t flags)
David Sodman0c69cad2017-08-21 12:12:51 -070074 : contentDirty(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080075 sequence(uint32_t(android_atomic_inc(&sSequence))),
76 mFlinger(flinger),
Mathias Agopian13127d82013-03-05 17:47:11 -080077 mPremultipliedAlpha(true),
David Sodman0c69cad2017-08-21 12:12:51 -070078 mName(name),
Mathias Agopian13127d82013-03-05 17:47:11 -080079 mTransactionFlags(0),
Dan Stoza7dde5992015-05-22 09:51:44 -070080 mPendingStateMutex(),
81 mPendingStates(),
Mathias Agopiana67932f2011-04-20 14:20:59 -070082 mQueuedFrames(0),
Jesse Hall399184a2014-03-03 15:42:54 -080083 mSidebandStreamChanged(false),
Mathias Agopiana9347642017-02-13 16:42:28 -080084 mActiveBufferSlot(BufferQueue::INVALID_BUFFER_SLOT),
Mathias Agopiana67932f2011-04-20 14:20:59 -070085 mCurrentTransform(0),
Robert Carrc3574f72016-03-24 12:19:32 -070086 mOverrideScalingMode(-1),
Mathias Agopiana67932f2011-04-20 14:20:59 -070087 mCurrentOpacity(true),
Dan Stozacac35382016-01-27 12:21:06 -080088 mCurrentFrameNumber(0),
Mathias Agopian82d7ab62012-01-19 18:34:40 -080089 mFrameLatencyNeeded(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080090 mFiltering(false),
91 mNeedsFiltering(false),
Mathias Agopian13127d82013-03-05 17:47:11 -080092 mProtectedByApp(false),
Riley Andrews03414a12014-07-01 14:22:59 -070093 mClientRef(client),
Dan Stozaa4650a52015-05-12 12:56:16 -070094 mPotentialCursor(false),
95 mQueueItemLock(),
96 mQueueItemCondition(),
97 mQueueItems(),
Dan Stoza65476f32015-05-14 09:27:25 -070098 mLastFrameNumberReceived(0),
Robert Carr82364e32016-05-15 11:27:47 -070099 mAutoRefresh(false),
David Sodman41fdfc92017-11-06 16:09:56 -0800100 mFreezeGeometryUpdates(false) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800101
Mathias Agopiana67932f2011-04-20 14:20:59 -0700102 mCurrentCrop.makeInvalid();
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700103
104 uint32_t layerFlags = 0;
David Sodman41fdfc92017-11-06 16:09:56 -0800105 if (flags & ISurfaceComposerClient::eHidden) layerFlags |= layer_state_t::eLayerHidden;
106 if (flags & ISurfaceComposerClient::eOpaque) layerFlags |= layer_state_t::eLayerOpaque;
107 if (flags & ISurfaceComposerClient::eSecure) layerFlags |= layer_state_t::eLayerSecure;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700108
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700109 mName = name;
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700110 mTransactionName = String8("TX - ") + mName;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700111
112 mCurrentState.active.w = w;
113 mCurrentState.active.h = h;
David Sodman0c69cad2017-08-21 12:12:51 -0700114 mCurrentState.flags = layerFlags;
Robert Carr3dcabfa2016-03-01 18:36:58 -0800115 mCurrentState.active.transform.set(0, 0);
Robert Carrb5d3d262016-03-25 15:08:13 -0700116 mCurrentState.crop.makeInvalid();
117 mCurrentState.finalCrop.makeInvalid();
Robert Carr7bf247e2017-05-18 14:02:49 -0700118 mCurrentState.requestedFinalCrop = mCurrentState.finalCrop;
119 mCurrentState.requestedCrop = mCurrentState.crop;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700120 mCurrentState.z = 0;
chaviw13fdc492017-06-27 12:40:18 -0700121 mCurrentState.color.a = 1.0f;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700122 mCurrentState.layerStack = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700123 mCurrentState.sequence = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700124 mCurrentState.requested = mCurrentState.active;
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700125 mCurrentState.dataSpace = ui::Dataspace::UNKNOWN;
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500126 mCurrentState.appId = 0;
127 mCurrentState.type = 0;
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700128
129 // drawing state & current state are identical
130 mDrawingState = mCurrentState;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700131
Dan Stoza9e56aa02015-11-02 13:00:03 -0800132 const auto& hwc = flinger->getHwComposer();
133 const auto& activeConfig = hwc.getActiveConfig(HWC_DISPLAY_PRIMARY);
134 nsecs_t displayPeriod = activeConfig->getVsyncPeriod();
Jamie Gennis6547ff42013-07-16 20:12:42 -0700135 mFrameTracker.setDisplayRefreshPeriod(displayPeriod);
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800136
137 CompositorTiming compositorTiming;
138 flinger->getCompositorTiming(&compositorTiming);
139 mFrameEventHistory.initializeCompositorTiming(compositorTiming);
David Sodman9eeae692017-11-02 10:53:32 -0700140
Jamie Gennise8696a42012-01-15 18:54:57 -0800141}
142
David Sodman41fdfc92017-11-06 16:09:56 -0800143void Layer::onFirstRef() {}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700144
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700145Layer::~Layer() {
David Sodman577c8962017-12-08 14:50:53 -0800146 sp<Client> c(mClientRef.promote());
147 if (c != 0) {
148 c->detachLayer(this);
149 }
150
151 for (auto& point : mRemoteSyncPoints) {
152 point->setTransactionApplied();
153 }
154 for (auto& point : mLocalSyncPoints) {
155 point->setFrameAvailable();
156 }
Jamie Gennis6547ff42013-07-16 20:12:42 -0700157 mFrameTracker.logAndResetStats(mName);
Mathias Agopian96f08192010-06-02 23:28:45 -0700158}
159
Mathias Agopian13127d82013-03-05 17:47:11 -0800160// ---------------------------------------------------------------------------
161// callbacks
162// ---------------------------------------------------------------------------
163
David Sodmaneb085e02017-10-05 18:49:04 -0700164/*
165 * onLayerDisplayed is only meaningful for BufferLayer, but, is called through
166 * Layer. So, the implementation is done in BufferLayer. When called on a
167 * ColorLayer object, it's essentially a NOP.
168 */
David Sodmaneb085e02017-10-05 18:49:04 -0700169void Layer::onLayerDisplayed(const sp<Fence>& /*releaseFence*/) {}
Mathias Agopian13127d82013-03-05 17:47:11 -0800170
Chia-I Wuc6657022017-08-15 11:18:17 -0700171void Layer::onRemovedFromCurrentState() {
172 // the layer is removed from SF mCurrentState to mLayersPendingRemoval
173
chaviw8b3871a2017-11-01 17:41:01 -0700174 mPendingRemoval = true;
175
Robert Carr5edb1ad2017-04-25 10:54:24 -0700176 if (mCurrentState.zOrderRelativeOf != nullptr) {
177 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
178 if (strongRelative != nullptr) {
179 strongRelative->removeZOrderRelative(this);
Chia-I Wuc6657022017-08-15 11:18:17 -0700180 mFlinger->setTransactionFlags(eTraversalNeeded);
Robert Carr5edb1ad2017-04-25 10:54:24 -0700181 }
182 mCurrentState.zOrderRelativeOf = nullptr;
183 }
184
Chia-I Wuc6657022017-08-15 11:18:17 -0700185 for (const auto& child : mCurrentChildren) {
186 child->onRemovedFromCurrentState();
187 }
188}
Chia-I Wu38512252017-05-17 14:36:16 -0700189
Chia-I Wuc6657022017-08-15 11:18:17 -0700190void Layer::onRemoved() {
191 // the layer is removed from SF mLayersPendingRemoval
David Sodmaneb085e02017-10-05 18:49:04 -0700192 abandon();
Chia-I Wuc6657022017-08-15 11:18:17 -0700193
Steven Thomasb02664d2017-07-26 18:48:28 -0700194 destroyAllHwcLayers();
Chia-I Wu38512252017-05-17 14:36:16 -0700195
Robert Carr1f0a16a2016-10-24 16:27:39 -0700196 for (const auto& child : mCurrentChildren) {
197 child->onRemoved();
198 }
Mathias Agopian48d819a2009-09-10 19:41:18 -0700199}
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700200
Mathias Agopian13127d82013-03-05 17:47:11 -0800201// ---------------------------------------------------------------------------
202// set-up
203// ---------------------------------------------------------------------------
204
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700205const String8& Layer::getName() const {
Mathias Agopian13127d82013-03-05 17:47:11 -0800206 return mName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207}
208
chaviw13fdc492017-06-27 12:40:18 -0700209bool Layer::getPremultipledAlpha() const {
210 return mPremultipliedAlpha;
211}
212
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700213sp<IBinder> Layer::getHandle() {
Mathias Agopian13127d82013-03-05 17:47:11 -0800214 Mutex::Autolock _l(mLock);
Mathias Agopian4d9b8222013-03-12 17:11:48 -0700215 return new Handle(mFlinger, this);
Mathias Agopian13127d82013-03-05 17:47:11 -0800216}
217
218// ---------------------------------------------------------------------------
219// h/w composer set-up
220// ---------------------------------------------------------------------------
221
Steven Thomasb02664d2017-07-26 18:48:28 -0700222bool Layer::createHwcLayer(HWComposer* hwc, int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700223 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(hwcId) != 0,
David Sodman9eeae692017-11-02 10:53:32 -0700224 "Already have a layer for hwcId %d", hwcId);
Steven Thomasb02664d2017-07-26 18:48:28 -0700225 HWC2::Layer* layer = hwc->createLayer(hwcId);
226 if (!layer) {
227 return false;
228 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700229 LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers[hwcId];
Steven Thomasb02664d2017-07-26 18:48:28 -0700230 hwcInfo.hwc = hwc;
231 hwcInfo.layer = layer;
232 layer->setLayerDestroyedListener(
David Sodman6f65f3e2017-11-03 14:28:09 -0700233 [this, hwcId](HWC2::Layer* /*layer*/) { getBE().mHwcLayers.erase(hwcId); });
Steven Thomasb02664d2017-07-26 18:48:28 -0700234 return true;
235}
236
Chia-I Wu83806892017-11-16 10:50:20 -0800237bool Layer::destroyHwcLayer(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700238 if (getBE().mHwcLayers.count(hwcId) == 0) {
Chia-I Wu83806892017-11-16 10:50:20 -0800239 return false;
Steven Thomasb02664d2017-07-26 18:48:28 -0700240 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700241 auto& hwcInfo = getBE().mHwcLayers[hwcId];
David Sodman41fdfc92017-11-06 16:09:56 -0800242 LOG_ALWAYS_FATAL_IF(hwcInfo.layer == nullptr, "Attempt to destroy null layer");
Steven Thomasb02664d2017-07-26 18:48:28 -0700243 LOG_ALWAYS_FATAL_IF(hwcInfo.hwc == nullptr, "Missing HWComposer");
244 hwcInfo.hwc->destroyLayer(hwcId, hwcInfo.layer);
245 // The layer destroyed listener should have cleared the entry from
246 // mHwcLayers. Verify that.
David Sodman6f65f3e2017-11-03 14:28:09 -0700247 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.count(hwcId) != 0,
248 "Stale layer entry in getBE().mHwcLayers");
Chia-I Wu83806892017-11-16 10:50:20 -0800249 return true;
Steven Thomasb02664d2017-07-26 18:48:28 -0700250}
251
252void Layer::destroyAllHwcLayers() {
David Sodman6f65f3e2017-11-03 14:28:09 -0700253 size_t numLayers = getBE().mHwcLayers.size();
Steven Thomasb02664d2017-07-26 18:48:28 -0700254 for (size_t i = 0; i < numLayers; ++i) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700255 LOG_ALWAYS_FATAL_IF(getBE().mHwcLayers.empty(), "destroyAllHwcLayers failed");
256 destroyHwcLayer(getBE().mHwcLayers.begin()->first);
Steven Thomasb02664d2017-07-26 18:48:28 -0700257 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700258 LOG_ALWAYS_FATAL_IF(!getBE().mHwcLayers.empty(),
David Sodman41fdfc92017-11-06 16:09:56 -0800259 "All hardware composer layers should have been destroyed");
Steven Thomasb02664d2017-07-26 18:48:28 -0700260}
Steven Thomasb02664d2017-07-26 18:48:28 -0700261
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800262Rect Layer::getContentCrop() const {
263 // this is the crop rectangle that applies to the buffer
264 // itself (as opposed to the window)
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700265 Rect crop;
266 if (!mCurrentCrop.isEmpty()) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800267 // if the buffer crop is defined, we use that
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700268 crop = mCurrentCrop;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800269 } else if (getBE().compositionInfo.mBuffer != nullptr) {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800270 // otherwise we use the whole buffer
David Sodman0cc69182017-11-17 12:12:07 -0800271 crop = getBE().compositionInfo.mBuffer->getBounds();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700272 } else {
Mathias Agopiana8bca8d2013-02-27 22:03:19 -0800273 // if we don't have a buffer yet, we use an empty/invalid crop
Mathias Agopian4fec8732012-06-29 14:12:52 -0700274 crop.makeInvalid();
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700275 }
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700276 return crop;
277}
278
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700279static Rect reduce(const Rect& win, const Region& exclude) {
280 if (CC_LIKELY(exclude.isEmpty())) {
281 return win;
282 }
283 if (exclude.isRect()) {
284 return win.reduce(exclude.getBounds());
285 }
286 return Region(win).subtract(exclude).getBounds();
287}
288
Dan Stoza80d61162017-12-20 15:57:52 -0800289static FloatRect reduce(const FloatRect& win, const Region& exclude) {
290 if (CC_LIKELY(exclude.isEmpty())) {
291 return win;
292 }
293 // Convert through Rect (by rounding) for lack of FloatRegion
294 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
295}
296
Robert Carr1f0a16a2016-10-24 16:27:39 -0700297Rect Layer::computeScreenBounds(bool reduceTransparentRegion) const {
298 const Layer::State& s(getDrawingState());
299 Rect win(s.active.w, s.active.h);
300
301 if (!s.crop.isEmpty()) {
302 win.intersect(s.crop, &win);
303 }
304
305 Transform t = getTransform();
306 win = t.transform(win);
307
Robert Carr41b08b52017-06-01 16:11:34 -0700308 if (!s.finalCrop.isEmpty()) {
309 win.intersect(s.finalCrop, &win);
310 }
311
Chia-I Wue41dbe62017-06-13 14:10:56 -0700312 const sp<Layer>& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700313 // Now we need to calculate the parent bounds, so we can clip ourselves to those.
314 // When calculating the parent bounds for purposes of clipping,
315 // we don't need to constrain the parent to its transparent region.
316 // The transparent region is an optimization based on the
317 // buffer contents of the layer, but does not affect the space allocated to
318 // it by policy, and thus children should be allowed to extend into the
319 // parent's transparent region. In fact one of the main uses, is to reduce
320 // buffer allocation size in cases where a child window sits behind a main window
321 // (by marking the hole in the parent window as a transparent region)
322 if (p != nullptr) {
323 Rect bounds = p->computeScreenBounds(false);
324 bounds.intersect(win, &win);
325 }
326
327 if (reduceTransparentRegion) {
328 auto const screenTransparentRegion = t.transform(s.activeTransparentRegion);
329 win = reduce(win, screenTransparentRegion);
330 }
331
332 return win;
333}
334
Dan Stoza80d61162017-12-20 15:57:52 -0800335FloatRect Layer::computeBounds() const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700336 const Layer::State& s(getDrawingState());
Michael Lentine6c925ed2014-09-26 17:55:01 -0700337 return computeBounds(s.activeTransparentRegion);
338}
339
Dan Stoza80d61162017-12-20 15:57:52 -0800340FloatRect Layer::computeBounds(const Region& activeTransparentRegion) const {
Michael Lentine6c925ed2014-09-26 17:55:01 -0700341 const Layer::State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800342 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700343
344 if (!s.crop.isEmpty()) {
345 win.intersect(s.crop, &win);
Mathias Agopian13127d82013-03-05 17:47:11 -0800346 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700347
348 Rect bounds = win;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700349 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700350 if (p != nullptr) {
Robert Carrde9ec442017-02-08 17:43:36 -0800351 // Look in computeScreenBounds recursive call for explanation of
352 // why we pass false here.
353 bounds = p->computeScreenBounds(false /* reduceTransparentRegion */);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700354 }
355
356 Transform t = getTransform();
Dan Stoza80d61162017-12-20 15:57:52 -0800357
358 FloatRect floatWin = win.toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700359 if (p != nullptr) {
Dan Stoza80d61162017-12-20 15:57:52 -0800360 floatWin = t.transform(floatWin);
361 floatWin = floatWin.intersect(bounds.toFloatRect());
362 floatWin = t.inverse().transform(floatWin);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700363 }
364
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700365 // subtract the transparent region and snap to the bounds
Dan Stoza80d61162017-12-20 15:57:52 -0800366 return reduce(floatWin, activeTransparentRegion);
Mathias Agopian13127d82013-03-05 17:47:11 -0800367}
368
Robert Carr1f0a16a2016-10-24 16:27:39 -0700369Rect Layer::computeInitialCrop(const sp<const DisplayDevice>& hw) const {
Robert Carrb5d3d262016-03-25 15:08:13 -0700370 // the crop is the area of the window that gets cropped, but not
Mathias Agopian13127d82013-03-05 17:47:11 -0800371 // scaled in any ways.
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700372 const State& s(getDrawingState());
Mathias Agopian13127d82013-03-05 17:47:11 -0800373
374 // apply the projection's clipping to the window crop in
375 // layerstack space, and convert-back to layer space.
Mathias Agopian6b442672013-07-09 21:24:52 -0700376 // if there are no window scaling involved, this operation will map to full
377 // pixels in the buffer.
378 // FIXME: the 3 lines below can produce slightly incorrect clipping when we have
379 // a viewport clipping and a window transform. we should use floating point to fix this.
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700380
381 Rect activeCrop(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700382 if (!s.crop.isEmpty()) {
Chia-I Wudf7867f2017-07-20 14:24:37 -0700383 activeCrop.intersect(s.crop, &activeCrop);
Mathias Agopian0e8f1442013-08-20 21:41:07 -0700384 }
385
Robert Carr1f0a16a2016-10-24 16:27:39 -0700386 Transform t = getTransform();
387 activeCrop = t.transform(activeCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000388 if (!activeCrop.intersect(hw->getViewport(), &activeCrop)) {
389 activeCrop.clear();
390 }
Robert Carrb5d3d262016-03-25 15:08:13 -0700391 if (!s.finalCrop.isEmpty()) {
David Sodman41fdfc92017-11-06 16:09:56 -0800392 if (!activeCrop.intersect(s.finalCrop, &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000393 activeCrop.clear();
394 }
395 }
chaviwb1154d12017-10-31 14:15:36 -0700396
397 const auto& p = mDrawingParent.promote();
398 if (p != nullptr) {
399 auto parentCrop = p->computeInitialCrop(hw);
400 activeCrop.intersect(parentCrop, &activeCrop);
401 }
402
Robert Carr1f0a16a2016-10-24 16:27:39 -0700403 return activeCrop;
404}
405
Dan Stoza5a423ea2017-02-16 14:10:39 -0800406FloatRect Layer::computeCrop(const sp<const DisplayDevice>& hw) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700407 // the content crop is the area of the content that gets scaled to the
408 // layer's size. This is in buffer space.
Dan Stoza5a423ea2017-02-16 14:10:39 -0800409 FloatRect crop = getContentCrop().toFloatRect();
Robert Carr1f0a16a2016-10-24 16:27:39 -0700410
411 // In addition there is a WM-specified crop we pull from our drawing state.
412 const State& s(getDrawingState());
413
414 // Screen space to make reduction to parent crop clearer.
415 Rect activeCrop = computeInitialCrop(hw);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700416 Transform t = getTransform();
417 // Back to layer space to work with the content crop.
418 activeCrop = t.inverse().transform(activeCrop);
Mathias Agopian13127d82013-03-05 17:47:11 -0800419
Michael Lentine28ea2172014-11-19 18:32:37 -0800420 // This needs to be here as transform.transform(Rect) computes the
421 // transformed rect and then takes the bounding box of the result before
422 // returning. This means
423 // transform.inverse().transform(transform.transform(Rect)) != Rect
424 // in which case we need to make sure the final rect is clipped to the
425 // display bounds.
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000426 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
427 activeCrop.clear();
428 }
Mathias Agopian13127d82013-03-05 17:47:11 -0800429
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700430 // subtract the transparent region and snap to the bounds
431 activeCrop = reduce(activeCrop, s.activeTransparentRegion);
432
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000433 // Transform the window crop to match the buffer coordinate system,
434 // which means using the inverse of the current transform set on the
435 // SurfaceFlingerConsumer.
436 uint32_t invTransform = mCurrentTransform;
Robert Carrcae605c2017-03-29 12:10:31 -0700437 if (getTransformToDisplayInverse()) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000438 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700439 * the code below applies the primary display's inverse transform to the
440 * buffer
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000441 */
David Sodman41fdfc92017-11-06 16:09:56 -0800442 uint32_t invTransformOrient = DisplayDevice::getPrimaryDisplayOrientationTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000443 // calculate the inverse transform
444 if (invTransformOrient & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800445 invTransformOrient ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopian13127d82013-03-05 17:47:11 -0800446 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000447 // and apply to the current transform
David Sodman41fdfc92017-11-06 16:09:56 -0800448 invTransform = (Transform(invTransformOrient) * Transform(invTransform)).getOrientation();
Mathias Agopian13127d82013-03-05 17:47:11 -0800449 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000450
451 int winWidth = s.active.w;
452 int winHeight = s.active.h;
453 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
454 // If the activeCrop has been rotate the ends are rotated but not
455 // the space itself so when transforming ends back we can't rely on
456 // a modification of the axes of rotation. To account for this we
457 // need to reorient the inverse rotation in terms of the current
458 // axes of rotation.
459 bool is_h_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_H) != 0;
460 bool is_v_flipped = (invTransform & NATIVE_WINDOW_TRANSFORM_FLIP_V) != 0;
461 if (is_h_flipped == is_v_flipped) {
David Sodman41fdfc92017-11-06 16:09:56 -0800462 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000463 }
464 winWidth = s.active.h;
465 winHeight = s.active.w;
466 }
David Sodman41fdfc92017-11-06 16:09:56 -0800467 const Rect winCrop = activeCrop.transform(invTransform, s.active.w, s.active.h);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000468
469 // below, crop is intersected with winCrop expressed in crop's coordinate space
David Sodman41fdfc92017-11-06 16:09:56 -0800470 float xScale = crop.getWidth() / float(winWidth);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000471 float yScale = crop.getHeight() / float(winHeight);
472
David Sodman41fdfc92017-11-06 16:09:56 -0800473 float insetL = winCrop.left * xScale;
474 float insetT = winCrop.top * yScale;
475 float insetR = (winWidth - winCrop.right) * xScale;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000476 float insetB = (winHeight - winCrop.bottom) * yScale;
477
David Sodman41fdfc92017-11-06 16:09:56 -0800478 crop.left += insetL;
479 crop.top += insetT;
480 crop.right -= insetR;
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000481 crop.bottom -= insetB;
482
Mathias Agopian13127d82013-03-05 17:47:11 -0800483 return crop;
484}
485
Robert Carrae060832016-11-28 10:51:00 -0800486void Layer::setGeometry(const sp<const DisplayDevice>& displayDevice, uint32_t z)
Mathias Agopiana350ff92010-08-10 17:14:02 -0700487{
Dan Stoza9e56aa02015-11-02 13:00:03 -0800488 const auto hwcId = displayDevice->getHwcDisplayId();
David Sodman6f65f3e2017-11-03 14:28:09 -0700489 auto& hwcInfo = getBE().mHwcLayers[hwcId];
Mathias Agopiana537c0f2011-08-02 15:51:37 -0700490
Mathias Agopian3e8b8532012-05-13 20:42:01 -0700491 // enable this layer
Dan Stoza9e56aa02015-11-02 13:00:03 -0800492 hwcInfo.forceClientComposition = false;
493
494 if (isSecure() && !displayDevice->isSecure()) {
495 hwcInfo.forceClientComposition = true;
496 }
497
498 auto& hwcLayer = hwcInfo.layer;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700499
Mathias Agopian13127d82013-03-05 17:47:11 -0800500 // this gives us only the "orientation" component of the transform
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700501 const State& s(getDrawingState());
David Revemanecf0fa52017-03-03 11:32:44 -0500502 auto blendMode = HWC2::BlendMode::None;
Robert Carr6452f122017-03-21 10:41:29 -0700503 if (!isOpaque(s) || getAlpha() != 1.0f) {
David Sodman41fdfc92017-11-06 16:09:56 -0800504 blendMode =
505 mPremultipliedAlpha ? HWC2::BlendMode::Premultiplied : HWC2::BlendMode::Coverage;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800506 }
David Revemanecf0fa52017-03-03 11:32:44 -0500507 auto error = hwcLayer->setBlendMode(blendMode);
David Sodman41fdfc92017-11-06 16:09:56 -0800508 ALOGE_IF(error != HWC2::Error::None,
509 "[%s] Failed to set blend mode %s:"
510 " %s (%d)",
511 mName.string(), to_string(blendMode).c_str(), to_string(error).c_str(),
512 static_cast<int32_t>(error));
Mathias Agopian13127d82013-03-05 17:47:11 -0800513
514 // apply the layer's transform, followed by the display's global transform
515 // here we're guaranteed that the layer's transform preserves rects
Michael Lentine6c925ed2014-09-26 17:55:01 -0700516 Region activeTransparentRegion(s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700517 Transform t = getTransform();
Robert Carrb5d3d262016-03-25 15:08:13 -0700518 if (!s.crop.isEmpty()) {
519 Rect activeCrop(s.crop);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700520 activeCrop = t.transform(activeCrop);
David Sodman41fdfc92017-11-06 16:09:56 -0800521 if (!activeCrop.intersect(displayDevice->getViewport(), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000522 activeCrop.clear();
523 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700524 activeCrop = t.inverse().transform(activeCrop, true);
Michael Lentine28ea2172014-11-19 18:32:37 -0800525 // This needs to be here as transform.transform(Rect) computes the
526 // transformed rect and then takes the bounding box of the result before
527 // returning. This means
528 // transform.inverse().transform(transform.transform(Rect)) != Rect
529 // in which case we need to make sure the final rect is clipped to the
530 // display bounds.
David Sodman41fdfc92017-11-06 16:09:56 -0800531 if (!activeCrop.intersect(Rect(s.active.w, s.active.h), &activeCrop)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000532 activeCrop.clear();
533 }
Michael Lentine6c925ed2014-09-26 17:55:01 -0700534 // mark regions outside the crop as transparent
535 activeTransparentRegion.orSelf(Rect(0, 0, s.active.w, activeCrop.top));
David Sodman41fdfc92017-11-06 16:09:56 -0800536 activeTransparentRegion.orSelf(Rect(0, activeCrop.bottom, s.active.w, s.active.h));
537 activeTransparentRegion.orSelf(Rect(0, activeCrop.top, activeCrop.left, activeCrop.bottom));
538 activeTransparentRegion.orSelf(
539 Rect(activeCrop.right, activeCrop.top, s.active.w, activeCrop.bottom));
Michael Lentine6c925ed2014-09-26 17:55:01 -0700540 }
Robert Carr1f0a16a2016-10-24 16:27:39 -0700541
Dan Stoza80d61162017-12-20 15:57:52 -0800542 // computeBounds returns a FloatRect to provide more accuracy during the
543 // transformation. We then round upon constructing 'frame'.
544 Rect frame{t.transform(computeBounds(activeTransparentRegion))};
Robert Carrb5d3d262016-03-25 15:08:13 -0700545 if (!s.finalCrop.isEmpty()) {
David Sodman41fdfc92017-11-06 16:09:56 -0800546 if (!frame.intersect(s.finalCrop, &frame)) {
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000547 frame.clear();
548 }
549 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000550 if (!frame.intersect(displayDevice->getViewport(), &frame)) {
551 frame.clear();
552 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800553 const Transform& tr(displayDevice->getTransform());
554 Rect transformedFrame = tr.transform(frame);
David Revemanecf0fa52017-03-03 11:32:44 -0500555 error = hwcLayer->setDisplayFrame(transformedFrame);
Dan Stozae22aec72016-08-01 13:20:59 -0700556 if (error != HWC2::Error::None) {
David Sodman41fdfc92017-11-06 16:09:56 -0800557 ALOGE("[%s] Failed to set display frame [%d, %d, %d, %d]: %s (%d)", mName.string(),
558 transformedFrame.left, transformedFrame.top, transformedFrame.right,
559 transformedFrame.bottom, to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stozae22aec72016-08-01 13:20:59 -0700560 } else {
561 hwcInfo.displayFrame = transformedFrame;
562 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800563
Dan Stoza5a423ea2017-02-16 14:10:39 -0800564 FloatRect sourceCrop = computeCrop(displayDevice);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800565 error = hwcLayer->setSourceCrop(sourceCrop);
Dan Stozae22aec72016-08-01 13:20:59 -0700566 if (error != HWC2::Error::None) {
567 ALOGE("[%s] Failed to set source crop [%.3f, %.3f, %.3f, %.3f]: "
David Sodman41fdfc92017-11-06 16:09:56 -0800568 "%s (%d)",
569 mName.string(), sourceCrop.left, sourceCrop.top, sourceCrop.right, sourceCrop.bottom,
570 to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stozae22aec72016-08-01 13:20:59 -0700571 } else {
572 hwcInfo.sourceCrop = sourceCrop;
573 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800574
chaviw13fdc492017-06-27 12:40:18 -0700575 float alpha = static_cast<float>(getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -0700576 error = hwcLayer->setPlaneAlpha(alpha);
David Sodman41fdfc92017-11-06 16:09:56 -0800577 ALOGE_IF(error != HWC2::Error::None,
578 "[%s] Failed to set plane alpha %.3f: "
579 "%s (%d)",
580 mName.string(), alpha, to_string(error).c_str(), static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800581
Robert Carrae060832016-11-28 10:51:00 -0800582 error = hwcLayer->setZOrder(z);
David Sodman41fdfc92017-11-06 16:09:56 -0800583 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set Z %u: %s (%d)", mName.string(), z,
584 to_string(error).c_str(), static_cast<int32_t>(error));
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -0500585
Albert Chaulk2a589632017-05-04 16:59:44 -0400586 int type = s.type;
587 int appId = s.appId;
Chia-I Wue41dbe62017-06-13 14:10:56 -0700588 sp<Layer> parent = mDrawingParent.promote();
Albert Chaulk2a589632017-05-04 16:59:44 -0400589 if (parent.get()) {
590 auto& parentState = parent->getDrawingState();
rongliuccd34842018-03-14 12:26:23 -0700591 if (parentState.type >= 0 || parentState.appId >= 0) {
592 type = parentState.type;
593 appId = parentState.appId;
594 }
Albert Chaulk2a589632017-05-04 16:59:44 -0400595 }
596
597 error = hwcLayer->setInfo(type, appId);
David Sodman41fdfc92017-11-06 16:09:56 -0800598 ALOGE_IF(error != HWC2::Error::None, "[%s] Failed to set info (%d)", mName.string(),
599 static_cast<int32_t>(error));
Mathias Agopian9f8386e2013-01-29 18:56:42 -0800600
Mathias Agopian29a367b2011-07-12 14:51:45 -0700601 /*
602 * Transformations are applied in this order:
603 * 1) buffer orientation/flip/mirror
604 * 2) state transformation (window manager)
605 * 3) layer orientation (screen orientation)
606 * (NOTE: the matrices are multiplied in reverse order)
607 */
608
609 const Transform bufferOrientation(mCurrentTransform);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700610 Transform transform(tr * t * bufferOrientation);
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700611
Robert Carrcae605c2017-03-29 12:10:31 -0700612 if (getTransformToDisplayInverse()) {
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700613 /*
Pablo Ceballos021623b2016-04-15 17:31:51 -0700614 * the code below applies the primary display's inverse transform to the
615 * buffer
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700616 */
David Sodman41fdfc92017-11-06 16:09:56 -0800617 uint32_t invTransform = DisplayDevice::getPrimaryDisplayOrientationTransform();
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700618 // calculate the inverse transform
619 if (invTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
David Sodman41fdfc92017-11-06 16:09:56 -0800620 invTransform ^= NATIVE_WINDOW_TRANSFORM_FLIP_V | NATIVE_WINDOW_TRANSFORM_FLIP_H;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700621 }
Robert Carrcae605c2017-03-29 12:10:31 -0700622
623 /*
624 * Here we cancel out the orientation component of the WM transform.
625 * The scaling and translate components are already included in our bounds
626 * computation so it's enough to just omit it in the composition.
627 * See comment in onDraw with ref to b/36727915 for why.
628 */
629 transform = Transform(invTransform) * tr * bufferOrientation;
Mathias Agopianc1c05de2013-09-17 23:45:22 -0700630 }
Mathias Agopian29a367b2011-07-12 14:51:45 -0700631
632 // this gives us only the "orientation" component of the transform
Mathias Agopian13127d82013-03-05 17:47:11 -0800633 const uint32_t orientation = transform.getOrientation();
Jorim Jaggif3bd94a2018-03-27 15:38:03 +0200634 if (orientation & Transform::ROT_INVALID) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800635 // we can only handle simple transformation
636 hwcInfo.forceClientComposition = true;
637 } else {
638 auto transform = static_cast<HWC2::Transform>(orientation);
Yiwei Zhang068e31b2018-02-21 13:02:45 -0800639 hwcInfo.transform = transform;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800640 auto error = hwcLayer->setTransform(transform);
David Sodman41fdfc92017-11-06 16:09:56 -0800641 ALOGE_IF(error != HWC2::Error::None,
642 "[%s] Failed to set transform %s: "
643 "%s (%d)",
644 mName.string(), to_string(transform).c_str(), to_string(error).c_str(),
645 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800646 }
Mathias Agopiana350ff92010-08-10 17:14:02 -0700647}
648
Dan Stoza9e56aa02015-11-02 13:00:03 -0800649void Layer::forceClientComposition(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700650 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800651 ALOGE("forceClientComposition: no HWC layer found (%d)", hwcId);
652 return;
653 }
654
David Sodman6f65f3e2017-11-03 14:28:09 -0700655 getBE().mHwcLayers[hwcId].forceClientComposition = true;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800656}
Dan Stozaee44edd2015-03-23 15:50:23 -0700657
chaviwc9232ed2017-11-14 15:31:15 -0800658bool Layer::getForceClientComposition(int32_t hwcId) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700659 if (getBE().mHwcLayers.count(hwcId) == 0) {
chaviwc9232ed2017-11-14 15:31:15 -0800660 ALOGE("getForceClientComposition: no HWC layer found (%d)", hwcId);
661 return false;
662 }
663
David Sodman6f65f3e2017-11-03 14:28:09 -0700664 return getBE().mHwcLayers[hwcId].forceClientComposition;
chaviwc9232ed2017-11-14 15:31:15 -0800665}
666
Dan Stoza9e56aa02015-11-02 13:00:03 -0800667void Layer::updateCursorPosition(const sp<const DisplayDevice>& displayDevice) {
668 auto hwcId = displayDevice->getHwcDisplayId();
David Sodman6f65f3e2017-11-03 14:28:09 -0700669 if (getBE().mHwcLayers.count(hwcId) == 0 ||
David Sodman9eeae692017-11-02 10:53:32 -0700670 getCompositionType(hwcId) != HWC2::Composition::Cursor) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800671 return;
672 }
673
674 // This gives us only the "orientation" component of the transform
675 const State& s(getCurrentState());
676
677 // Apply the layer's transform, followed by the display's global transform
678 // Here we're guaranteed that the layer's transform preserves rects
679 Rect win(s.active.w, s.active.h);
Robert Carrb5d3d262016-03-25 15:08:13 -0700680 if (!s.crop.isEmpty()) {
681 win.intersect(s.crop, &win);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800682 }
683 // Subtract the transparent region and snap to the bounds
684 Rect bounds = reduce(win, s.activeTransparentRegion);
Robert Carr1f0a16a2016-10-24 16:27:39 -0700685 Rect frame(getTransform().transform(bounds));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800686 frame.intersect(displayDevice->getViewport(), &frame);
Robert Carrb5d3d262016-03-25 15:08:13 -0700687 if (!s.finalCrop.isEmpty()) {
688 frame.intersect(s.finalCrop, &frame);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000689 }
Dan Stoza9e56aa02015-11-02 13:00:03 -0800690 auto& displayTransform(displayDevice->getTransform());
691 auto position = displayTransform.transform(frame);
692
David Sodman6f65f3e2017-11-03 14:28:09 -0700693 auto error = getBE().mHwcLayers[hwcId].layer->setCursorPosition(position.left,
David Sodman9eeae692017-11-02 10:53:32 -0700694 position.top);
David Sodman41fdfc92017-11-06 16:09:56 -0800695 ALOGE_IF(error != HWC2::Error::None,
696 "[%s] Failed to set cursor position "
697 "to (%d, %d): %s (%d)",
698 mName.string(), position.left, position.top, to_string(error).c_str(),
699 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800700}
Riley Andrews03414a12014-07-01 14:22:59 -0700701
Mathias Agopian13127d82013-03-05 17:47:11 -0800702// ---------------------------------------------------------------------------
703// drawing...
704// ---------------------------------------------------------------------------
705
chaviwa76b2712017-09-20 12:02:26 -0700706void Layer::draw(const RenderArea& renderArea, const Region& clip) const {
707 onDraw(renderArea, clip, false);
Mathias Agopian13127d82013-03-05 17:47:11 -0800708}
709
chaviwa76b2712017-09-20 12:02:26 -0700710void Layer::draw(const RenderArea& renderArea, bool useIdentityTransform) const {
711 onDraw(renderArea, Region(renderArea.getBounds()), useIdentityTransform);
Mathias Agopian13127d82013-03-05 17:47:11 -0800712}
713
chaviwa76b2712017-09-20 12:02:26 -0700714void Layer::draw(const RenderArea& renderArea) const {
715 onDraw(renderArea, Region(renderArea.getBounds()), false);
Dan Stozac7014012014-02-14 15:03:43 -0800716}
717
David Sodman41fdfc92017-11-06 16:09:56 -0800718void Layer::clearWithOpenGL(const RenderArea& renderArea, float red, float green, float blue,
719 float alpha) const {
Lloyd Pique144e1162017-12-20 16:44:52 -0800720 auto& engine(mFlinger->getRenderEngine());
David Sodman9eeae692017-11-02 10:53:32 -0700721 computeGeometry(renderArea, getBE().mMesh, false);
Mathias Agopian19733a32013-08-28 18:13:56 -0700722 engine.setupFillWithColor(red, green, blue, alpha);
David Sodman9eeae692017-11-02 10:53:32 -0700723 engine.drawMesh(getBE().mMesh);
Mathias Agopian13127d82013-03-05 17:47:11 -0800724}
725
chaviwa76b2712017-09-20 12:02:26 -0700726void Layer::clearWithOpenGL(const RenderArea& renderArea) const {
David Sodman41fdfc92017-11-06 16:09:56 -0800727 clearWithOpenGL(renderArea, 0, 0, 0, 0);
Mathias Agopian13127d82013-03-05 17:47:11 -0800728}
729
David Sodman41fdfc92017-11-06 16:09:56 -0800730void Layer::setCompositionType(int32_t hwcId, HWC2::Composition type, bool callIntoHwc) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700731 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800732 ALOGE("setCompositionType called without a valid HWC layer");
733 return;
734 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700735 auto& hwcInfo = getBE().mHwcLayers[hwcId];
Dan Stoza9e56aa02015-11-02 13:00:03 -0800736 auto& hwcLayer = hwcInfo.layer;
David Sodman41fdfc92017-11-06 16:09:56 -0800737 ALOGV("setCompositionType(%" PRIx64 ", %s, %d)", hwcLayer->getId(), to_string(type).c_str(),
738 static_cast<int>(callIntoHwc));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800739 if (hwcInfo.compositionType != type) {
740 ALOGV(" actually setting");
741 hwcInfo.compositionType = type;
742 if (callIntoHwc) {
743 auto error = hwcLayer->setCompositionType(type);
David Sodman41fdfc92017-11-06 16:09:56 -0800744 ALOGE_IF(error != HWC2::Error::None,
745 "[%s] Failed to set "
746 "composition type %s: %s (%d)",
747 mName.string(), to_string(type).c_str(), to_string(error).c_str(),
748 static_cast<int32_t>(error));
Dan Stoza9e56aa02015-11-02 13:00:03 -0800749 }
750 }
751}
752
753HWC2::Composition Layer::getCompositionType(int32_t hwcId) const {
Dan Stozaec0f7172016-07-21 11:09:40 -0700754 if (hwcId == DisplayDevice::DISPLAY_ID_INVALID) {
755 // If we're querying the composition type for a display that does not
756 // have a HWC counterpart, then it will always be Client
757 return HWC2::Composition::Client;
758 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700759 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stozaec0f7172016-07-21 11:09:40 -0700760 ALOGE("getCompositionType called with an invalid HWC layer");
Dan Stoza9e56aa02015-11-02 13:00:03 -0800761 return HWC2::Composition::Invalid;
762 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700763 return getBE().mHwcLayers.at(hwcId).compositionType;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800764}
765
766void Layer::setClearClientTarget(int32_t hwcId, bool clear) {
David Sodman6f65f3e2017-11-03 14:28:09 -0700767 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800768 ALOGE("setClearClientTarget called without a valid HWC layer");
769 return;
770 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700771 getBE().mHwcLayers[hwcId].clearClientTarget = clear;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800772}
773
774bool Layer::getClearClientTarget(int32_t hwcId) const {
David Sodman6f65f3e2017-11-03 14:28:09 -0700775 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800776 ALOGE("getClearClientTarget called without a valid HWC layer");
777 return false;
778 }
David Sodman6f65f3e2017-11-03 14:28:09 -0700779 return getBE().mHwcLayers.at(hwcId).clearClientTarget;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800780}
Dan Stoza9e56aa02015-11-02 13:00:03 -0800781
Dan Stozacac35382016-01-27 12:21:06 -0800782bool Layer::addSyncPoint(const std::shared_ptr<SyncPoint>& point) {
783 if (point->getFrameNumber() <= mCurrentFrameNumber) {
784 // Don't bother with a SyncPoint, since we've already latched the
785 // relevant frame
786 return false;
Dan Stoza7dde5992015-05-22 09:51:44 -0700787 }
788
Dan Stozacac35382016-01-27 12:21:06 -0800789 Mutex::Autolock lock(mLocalSyncPointMutex);
790 mLocalSyncPoints.push_back(point);
791 return true;
Dan Stoza7dde5992015-05-22 09:51:44 -0700792}
793
Mathias Agopian13127d82013-03-05 17:47:11 -0800794void Layer::setFiltering(bool filtering) {
795 mFiltering = filtering;
796}
797
798bool Layer::getFiltering() const {
799 return mFiltering;
800}
801
Mathias Agopian13127d82013-03-05 17:47:11 -0800802// ----------------------------------------------------------------------------
803// local state
804// ----------------------------------------------------------------------------
805
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000806static void boundPoint(vec2* point, const Rect& crop) {
807 if (point->x < crop.left) {
808 point->x = crop.left;
809 }
810 if (point->x > crop.right) {
811 point->x = crop.right;
812 }
813 if (point->y < crop.top) {
814 point->y = crop.top;
815 }
816 if (point->y > crop.bottom) {
817 point->y = crop.bottom;
818 }
819}
820
chaviwa76b2712017-09-20 12:02:26 -0700821void Layer::computeGeometry(const RenderArea& renderArea, Mesh& mesh,
822 bool useIdentityTransform) const {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700823 const Layer::State& s(getDrawingState());
chaviwa76b2712017-09-20 12:02:26 -0700824 const Transform renderAreaTransform(renderArea.getTransform());
825 const uint32_t height = renderArea.getHeight();
Dan Stoza80d61162017-12-20 15:57:52 -0800826 FloatRect win = computeBounds();
Mathias Agopian3f844832013-08-07 21:24:32 -0700827
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000828 vec2 lt = vec2(win.left, win.top);
829 vec2 lb = vec2(win.left, win.bottom);
830 vec2 rb = vec2(win.right, win.bottom);
831 vec2 rt = vec2(win.right, win.top);
832
Robert Carr1f0a16a2016-10-24 16:27:39 -0700833 Transform layerTransform = getTransform();
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000834 if (!useIdentityTransform) {
Robert Carr1f0a16a2016-10-24 16:27:39 -0700835 lt = layerTransform.transform(lt);
836 lb = layerTransform.transform(lb);
837 rb = layerTransform.transform(rb);
838 rt = layerTransform.transform(rt);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000839 }
840
Robert Carrb5d3d262016-03-25 15:08:13 -0700841 if (!s.finalCrop.isEmpty()) {
842 boundPoint(&lt, s.finalCrop);
843 boundPoint(&lb, s.finalCrop);
844 boundPoint(&rb, s.finalCrop);
845 boundPoint(&rt, s.finalCrop);
Pablo Ceballosacbe6782016-03-04 17:54:21 +0000846 }
847
Mathias Agopianff2ed702013-09-01 21:36:12 -0700848 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
chaviwa76b2712017-09-20 12:02:26 -0700849 position[0] = renderAreaTransform.transform(lt);
850 position[1] = renderAreaTransform.transform(lb);
851 position[2] = renderAreaTransform.transform(rb);
852 position[3] = renderAreaTransform.transform(rt);
David Sodman41fdfc92017-11-06 16:09:56 -0800853 for (size_t i = 0; i < 4; i++) {
chaviwa76b2712017-09-20 12:02:26 -0700854 position[i].y = height - position[i].y;
Mathias Agopian13127d82013-03-05 17:47:11 -0800855 }
856}
Eric Hassoldac45e6b2011-02-10 14:41:26 -0800857
David Sodman41fdfc92017-11-06 16:09:56 -0800858bool Layer::isSecure() const {
Dan Stoza23116082015-06-18 14:58:39 -0700859 const Layer::State& s(mDrawingState);
860 return (s.flags & layer_state_t::eLayerSecure);
861}
862
Mathias Agopian13127d82013-03-05 17:47:11 -0800863void Layer::setVisibleRegion(const Region& visibleRegion) {
864 // always called from main thread
865 this->visibleRegion = visibleRegion;
866}
867
868void Layer::setCoveredRegion(const Region& coveredRegion) {
869 // always called from main thread
870 this->coveredRegion = coveredRegion;
871}
872
David Sodman41fdfc92017-11-06 16:09:56 -0800873void Layer::setVisibleNonTransparentRegion(const Region& setVisibleNonTransparentRegion) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800874 // always called from main thread
875 this->visibleNonTransparentRegion = setVisibleNonTransparentRegion;
876}
877
Robert Carre5f4f692018-01-12 13:12:28 -0800878void Layer::clearVisibilityRegions() {
879 visibleRegion.clear();
880 visibleNonTransparentRegion.clear();
881 coveredRegion.clear();
882}
883
Mathias Agopian13127d82013-03-05 17:47:11 -0800884// ----------------------------------------------------------------------------
885// transaction
886// ----------------------------------------------------------------------------
887
Dan Stoza7dde5992015-05-22 09:51:44 -0700888void Layer::pushPendingState() {
889 if (!mCurrentState.modified) {
890 return;
891 }
892
Dan Stoza7dde5992015-05-22 09:51:44 -0700893 // If this transaction is waiting on the receipt of a frame, generate a sync
894 // point and send it to the remote layer.
Robert Carr0d480722017-01-10 16:42:54 -0800895 if (mCurrentState.barrierLayer != nullptr) {
896 sp<Layer> barrierLayer = mCurrentState.barrierLayer.promote();
897 if (barrierLayer == nullptr) {
898 ALOGE("[%s] Unable to promote barrier Layer.", mName.string());
Dan Stoza7dde5992015-05-22 09:51:44 -0700899 // If we can't promote the layer we are intended to wait on,
900 // then it is expired or otherwise invalid. Allow this transaction
901 // to be applied as per normal (no synchronization).
Robert Carr0d480722017-01-10 16:42:54 -0800902 mCurrentState.barrierLayer = nullptr;
Pablo Ceballos3bddd5b2015-11-19 14:39:14 -0800903 } else {
David Sodman41fdfc92017-11-06 16:09:56 -0800904 auto syncPoint = std::make_shared<SyncPoint>(mCurrentState.frameNumber);
Robert Carr0d480722017-01-10 16:42:54 -0800905 if (barrierLayer->addSyncPoint(syncPoint)) {
Dan Stozacac35382016-01-27 12:21:06 -0800906 mRemoteSyncPoints.push_back(std::move(syncPoint));
907 } else {
908 // We already missed the frame we're supposed to synchronize
909 // on, so go ahead and apply the state update
Robert Carr0d480722017-01-10 16:42:54 -0800910 mCurrentState.barrierLayer = nullptr;
Dan Stozacac35382016-01-27 12:21:06 -0800911 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700912 }
913
Dan Stoza7dde5992015-05-22 09:51:44 -0700914 // Wake us up to check if the frame has been received
915 setTransactionFlags(eTransactionNeeded);
Dan Stozaf5702ff2016-11-02 16:27:47 -0700916 mFlinger->setTransactionFlags(eTraversalNeeded);
Dan Stoza7dde5992015-05-22 09:51:44 -0700917 }
918 mPendingStates.push_back(mCurrentState);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700919 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700920}
921
Pablo Ceballos05289c22016-04-14 15:49:55 -0700922void Layer::popPendingState(State* stateToCommit) {
923 auto oldFlags = stateToCommit->flags;
924 *stateToCommit = mPendingStates[0];
David Sodman41fdfc92017-11-06 16:09:56 -0800925 stateToCommit->flags =
926 (oldFlags & ~stateToCommit->mask) | (stateToCommit->flags & stateToCommit->mask);
Dan Stoza7dde5992015-05-22 09:51:44 -0700927
928 mPendingStates.removeAt(0);
Dan Stozaf7ba41a2017-05-10 15:11:11 -0700929 ATRACE_INT(mTransactionName.string(), mPendingStates.size());
Dan Stoza7dde5992015-05-22 09:51:44 -0700930}
931
Pablo Ceballos05289c22016-04-14 15:49:55 -0700932bool Layer::applyPendingStates(State* stateToCommit) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700933 bool stateUpdateAvailable = false;
934 while (!mPendingStates.empty()) {
Robert Carr0d480722017-01-10 16:42:54 -0800935 if (mPendingStates[0].barrierLayer != nullptr) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700936 if (mRemoteSyncPoints.empty()) {
937 // If we don't have a sync point for this, apply it anyway. It
938 // will be visually wrong, but it should keep us from getting
939 // into too much trouble.
940 ALOGE("[%s] No local sync point found", mName.string());
Pablo Ceballos05289c22016-04-14 15:49:55 -0700941 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700942 stateUpdateAvailable = true;
943 continue;
944 }
945
David Sodman41fdfc92017-11-06 16:09:56 -0800946 if (mRemoteSyncPoints.front()->getFrameNumber() != mPendingStates[0].frameNumber) {
947 ALOGE("[%s] Unexpected sync point frame number found", mName.string());
Dan Stozacac35382016-01-27 12:21:06 -0800948
949 // Signal our end of the sync point and then dispose of it
950 mRemoteSyncPoints.front()->setTransactionApplied();
951 mRemoteSyncPoints.pop_front();
952 continue;
953 }
954
Dan Stoza7dde5992015-05-22 09:51:44 -0700955 if (mRemoteSyncPoints.front()->frameIsAvailable()) {
956 // Apply the state update
Pablo Ceballos05289c22016-04-14 15:49:55 -0700957 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700958 stateUpdateAvailable = true;
959
960 // Signal our end of the sync point and then dispose of it
961 mRemoteSyncPoints.front()->setTransactionApplied();
962 mRemoteSyncPoints.pop_front();
Dan Stoza792e5292016-02-11 11:43:58 -0800963 } else {
964 break;
Dan Stoza7dde5992015-05-22 09:51:44 -0700965 }
Dan Stoza7dde5992015-05-22 09:51:44 -0700966 } else {
Pablo Ceballos05289c22016-04-14 15:49:55 -0700967 popPendingState(stateToCommit);
Dan Stoza7dde5992015-05-22 09:51:44 -0700968 stateUpdateAvailable = true;
969 }
970 }
971
972 // If we still have pending updates, wake SurfaceFlinger back up and point
973 // it at this layer so we can process them
974 if (!mPendingStates.empty()) {
975 setTransactionFlags(eTransactionNeeded);
976 mFlinger->setTransactionFlags(eTraversalNeeded);
977 }
978
979 mCurrentState.modified = false;
980 return stateUpdateAvailable;
981}
982
Mathias Agopian13127d82013-03-05 17:47:11 -0800983uint32_t Layer::doTransaction(uint32_t flags) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800984 ATRACE_CALL();
985
Dan Stoza7dde5992015-05-22 09:51:44 -0700986 pushPendingState();
Pablo Ceballos05289c22016-04-14 15:49:55 -0700987 Layer::State c = getCurrentState();
988 if (!applyPendingStates(&c)) {
Dan Stoza7dde5992015-05-22 09:51:44 -0700989 return 0;
990 }
991
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700992 const Layer::State& s(getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800993
David Sodman41fdfc92017-11-06 16:09:56 -0800994 const bool sizeChanged = (c.requested.w != s.requested.w) || (c.requested.h != s.requested.h);
Mathias Agopiana138f892010-05-21 17:24:35 -0700995
David Sodmaneb085e02017-10-05 18:49:04 -0700996 if (sizeChanged) {
Mathias Agopiancbb288b2009-09-07 16:32:45 -0700997 // the size changed, we need to ask our client to request a new buffer
Steve Block9d453682011-12-20 16:23:08 +0000998 ALOGD_IF(DEBUG_RESIZE,
David Sodman41fdfc92017-11-06 16:09:56 -0800999 "doTransaction: geometry (layer=%p '%s'), tr=%02x, scalingMode=%d\n"
1000 " current={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1001 " requested={ wh={%4u,%4u} }}\n"
1002 " drawing={ active ={ wh={%4u,%4u} crop={%4d,%4d,%4d,%4d} (%4d,%4d) }\n"
1003 " requested={ wh={%4u,%4u} }}\n",
David Sodman9eeae692017-11-02 10:53:32 -07001004 this, getName().string(), mCurrentTransform,
1005 getEffectiveScalingMode(), c.active.w, c.active.h, c.crop.left, c.crop.top,
1006 c.crop.right, c.crop.bottom, c.crop.getWidth(), c.crop.getHeight(), c.requested.w,
1007 c.requested.h, s.active.w, s.active.h, s.crop.left, s.crop.top, s.crop.right,
1008 s.crop.bottom, s.crop.getWidth(), s.crop.getHeight(), s.requested.w,
1009 s.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001010
Jamie Gennis2a0d5b62011-09-26 16:54:44 -07001011 // record the new size, form this point on, when the client request
1012 // a buffer, it'll get the new size.
David Sodmaneb085e02017-10-05 18:49:04 -07001013 setDefaultBufferSize(c.requested.w, c.requested.h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001014 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001015
Robert Carre392b552017-09-19 12:16:05 -07001016 // Don't let Layer::doTransaction update the drawing state
1017 // if we have a pending resize, unless we are in fixed-size mode.
1018 // the drawing state will be updated only once we receive a buffer
1019 // with the correct size.
1020 //
1021 // In particular, we want to make sure the clip (which is part
1022 // of the geometry state) is latched together with the size but is
1023 // latched immediately when no resizing is involved.
1024 //
1025 // If a sideband stream is attached, however, we want to skip this
1026 // optimization so that transactions aren't missed when a buffer
1027 // never arrives
1028 //
1029 // In the case that we don't have a buffer we ignore other factors
1030 // and avoid entering the resizePending state. At a high level the
1031 // resizePending state is to avoid applying the state of the new buffer
1032 // to the old buffer. However in the state where we don't have an old buffer
1033 // there is no such concern but we may still be being used as a parent layer.
David Sodman41fdfc92017-11-06 16:09:56 -08001034 const bool resizePending = ((c.requested.w != c.active.w) || (c.requested.h != c.active.h)) &&
David Sodman0cc69182017-11-17 12:12:07 -08001035 (getBE().compositionInfo.mBuffer != nullptr);
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001036 if (!isFixedSize()) {
David Sodman0cc69182017-11-17 12:12:07 -08001037 if (resizePending && getBE().compositionInfo.hwc.sidebandStream == nullptr) {
Mathias Agopian0cd545f2012-06-07 14:18:55 -07001038 flags |= eDontUpdateGeometryState;
1039 }
1040 }
1041
Robert Carr7bf247e2017-05-18 14:02:49 -07001042 // Here we apply various requested geometry states, depending on our
1043 // latching configuration. See Layer.h for a detailed discussion of
1044 // how geometry latching is controlled.
1045 if (!(flags & eDontUpdateGeometryState)) {
Pablo Ceballos7d052572016-06-02 17:46:05 -07001046 Layer::State& editCurrentState(getCurrentState());
Robert Carr7bf247e2017-05-18 14:02:49 -07001047
1048 // If mFreezeGeometryUpdates is true we are in the setGeometryAppliesWithResize
1049 // mode, which causes attributes which normally latch regardless of scaling mode,
1050 // to be delayed. We copy the requested state to the active state making sure
1051 // to respect these rules (again see Layer.h for a detailed discussion).
1052 //
1053 // There is an awkward asymmetry in the handling of the crop states in the position
1054 // states, as can be seen below. Largely this arises from position and transform
1055 // being stored in the same data structure while having different latching rules.
1056 // b/38182305
1057 //
1058 // Careful that "c" and editCurrentState may not begin as equivalent due to
1059 // applyPendingStates in the presence of deferred transactions.
1060 if (mFreezeGeometryUpdates) {
Robert Carr82364e32016-05-15 11:27:47 -07001061 float tx = c.active.transform.tx();
1062 float ty = c.active.transform.ty();
1063 c.active = c.requested;
1064 c.active.transform.set(tx, ty);
1065 editCurrentState.active = c.active;
1066 } else {
1067 editCurrentState.active = editCurrentState.requested;
1068 c.active = c.requested;
1069 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001070 }
1071
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001072 if (s.active != c.active) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001073 // invalidate and recompute the visible regions if needed
1074 flags |= Layer::eVisibleRegion;
1075 }
1076
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001077 if (c.sequence != s.sequence) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001078 // invalidate and recompute the visible regions if needed
1079 flags |= eVisibleRegion;
1080 this->contentDirty = true;
1081
1082 // we may use linear filtering, if the matrix scales us
Robert Carr3dcabfa2016-03-01 18:36:58 -08001083 const uint8_t type = c.active.transform.getType();
David Sodman41fdfc92017-11-06 16:09:56 -08001084 mNeedsFiltering = (!c.active.transform.preserveRects() || (type >= Transform::SCALE));
Mathias Agopian13127d82013-03-05 17:47:11 -08001085 }
1086
Dan Stozac8145172016-04-28 16:29:06 -07001087 // If the layer is hidden, signal and clear out all local sync points so
1088 // that transactions for layers depending on this layer's frames becoming
1089 // visible are not blocked
1090 if (c.flags & layer_state_t::eLayerHidden) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001091 clearSyncPoints();
Dan Stozac8145172016-04-28 16:29:06 -07001092 }
1093
Mathias Agopian13127d82013-03-05 17:47:11 -08001094 // Commit the transaction
Pablo Ceballos05289c22016-04-14 15:49:55 -07001095 commitTransaction(c);
Mathias Agopian13127d82013-03-05 17:47:11 -08001096 return flags;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001097}
1098
Pablo Ceballos05289c22016-04-14 15:49:55 -07001099void Layer::commitTransaction(const State& stateToCommit) {
1100 mDrawingState = stateToCommit;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001101}
1102
Mathias Agopian13127d82013-03-05 17:47:11 -08001103uint32_t Layer::getTransactionFlags(uint32_t flags) {
1104 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1105}
1106
1107uint32_t Layer::setTransactionFlags(uint32_t flags) {
1108 return android_atomic_or(flags, &mTransactionFlags);
1109}
1110
Robert Carr82364e32016-05-15 11:27:47 -07001111bool Layer::setPosition(float x, float y, bool immediate) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001112 if (mCurrentState.requested.transform.tx() == x && mCurrentState.requested.transform.ty() == y)
Mathias Agopian13127d82013-03-05 17:47:11 -08001113 return false;
1114 mCurrentState.sequence++;
Robert Carr69663fb2016-03-27 19:59:19 -07001115
1116 // We update the requested and active position simultaneously because
1117 // we want to apply the position portion of the transform matrix immediately,
1118 // but still delay scaling when resizing a SCALING_MODE_FREEZE layer.
Robert Carr3dcabfa2016-03-01 18:36:58 -08001119 mCurrentState.requested.transform.set(x, y);
Robert Carr7bf247e2017-05-18 14:02:49 -07001120 if (immediate && !mFreezeGeometryUpdates) {
1121 // Here we directly update the active state
1122 // unlike other setters, because we store it within
1123 // the transform, but use different latching rules.
1124 // b/38182305
Robert Carr82364e32016-05-15 11:27:47 -07001125 mCurrentState.active.transform.set(x, y);
1126 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001127 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
Robert Carr69663fb2016-03-27 19:59:19 -07001128
Dan Stoza7dde5992015-05-22 09:51:44 -07001129 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001130 setTransactionFlags(eTransactionNeeded);
1131 return true;
1132}
Robert Carr82364e32016-05-15 11:27:47 -07001133
Robert Carr1f0a16a2016-10-24 16:27:39 -07001134bool Layer::setChildLayer(const sp<Layer>& childLayer, int32_t z) {
1135 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1136 if (idx < 0) {
1137 return false;
1138 }
1139 if (childLayer->setLayer(z)) {
1140 mCurrentChildren.removeAt(idx);
1141 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001142 return true;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001143 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001144 return false;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001145}
1146
Robert Carr503c7042017-09-27 15:06:08 -07001147bool Layer::setChildRelativeLayer(const sp<Layer>& childLayer,
1148 const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
1149 ssize_t idx = mCurrentChildren.indexOf(childLayer);
1150 if (idx < 0) {
1151 return false;
1152 }
1153 if (childLayer->setRelativeLayer(relativeToHandle, relativeZ)) {
1154 mCurrentChildren.removeAt(idx);
1155 mCurrentChildren.add(childLayer);
Robert Carr503d2bd2017-12-04 15:49:47 -08001156 return true;
Robert Carr503c7042017-09-27 15:06:08 -07001157 }
Robert Carr503d2bd2017-12-04 15:49:47 -08001158 return false;
Robert Carr503c7042017-09-27 15:06:08 -07001159}
1160
Robert Carrae060832016-11-28 10:51:00 -08001161bool Layer::setLayer(int32_t z) {
Robert Carr503d2bd2017-12-04 15:49:47 -08001162 if (mCurrentState.z == z && !usingRelativeZ(LayerVector::StateSet::Current)) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001163 mCurrentState.sequence++;
1164 mCurrentState.z = z;
Dan Stoza7dde5992015-05-22 09:51:44 -07001165 mCurrentState.modified = true;
Robert Carrdb66e622017-04-10 16:55:57 -07001166
1167 // Discard all relative layering.
1168 if (mCurrentState.zOrderRelativeOf != nullptr) {
1169 sp<Layer> strongRelative = mCurrentState.zOrderRelativeOf.promote();
1170 if (strongRelative != nullptr) {
1171 strongRelative->removeZOrderRelative(this);
1172 }
1173 mCurrentState.zOrderRelativeOf = nullptr;
1174 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001175 setTransactionFlags(eTransactionNeeded);
1176 return true;
1177}
Robert Carr1f0a16a2016-10-24 16:27:39 -07001178
Robert Carrdb66e622017-04-10 16:55:57 -07001179void Layer::removeZOrderRelative(const wp<Layer>& relative) {
1180 mCurrentState.zOrderRelatives.remove(relative);
1181 mCurrentState.sequence++;
1182 mCurrentState.modified = true;
1183 setTransactionFlags(eTransactionNeeded);
1184}
1185
1186void Layer::addZOrderRelative(const wp<Layer>& relative) {
1187 mCurrentState.zOrderRelatives.add(relative);
1188 mCurrentState.modified = true;
1189 mCurrentState.sequence++;
1190 setTransactionFlags(eTransactionNeeded);
1191}
1192
Robert Carr503d2bd2017-12-04 15:49:47 -08001193bool Layer::setRelativeLayer(const sp<IBinder>& relativeToHandle, int32_t relativeZ) {
Robert Carrdb66e622017-04-10 16:55:57 -07001194 sp<Handle> handle = static_cast<Handle*>(relativeToHandle.get());
1195 if (handle == nullptr) {
1196 return false;
1197 }
1198 sp<Layer> relative = handle->owner.promote();
1199 if (relative == nullptr) {
1200 return false;
1201 }
1202
Robert Carr503d2bd2017-12-04 15:49:47 -08001203 if (mCurrentState.z == relativeZ && usingRelativeZ(LayerVector::StateSet::Current) &&
1204 mCurrentState.zOrderRelativeOf == relative) {
1205 return false;
1206 }
1207
Robert Carrdb66e622017-04-10 16:55:57 -07001208 mCurrentState.sequence++;
1209 mCurrentState.modified = true;
Robert Carr503d2bd2017-12-04 15:49:47 -08001210 mCurrentState.z = relativeZ;
Robert Carrdb66e622017-04-10 16:55:57 -07001211
chaviw9ab4bd12017-11-03 13:11:00 -07001212 auto oldZOrderRelativeOf = mCurrentState.zOrderRelativeOf.promote();
1213 if (oldZOrderRelativeOf != nullptr) {
1214 oldZOrderRelativeOf->removeZOrderRelative(this);
1215 }
Robert Carrdb66e622017-04-10 16:55:57 -07001216 mCurrentState.zOrderRelativeOf = relative;
1217 relative->addZOrderRelative(this);
1218
1219 setTransactionFlags(eTransactionNeeded);
1220
1221 return true;
1222}
1223
Mathias Agopian13127d82013-03-05 17:47:11 -08001224bool Layer::setSize(uint32_t w, uint32_t h) {
David Sodman41fdfc92017-11-06 16:09:56 -08001225 if (mCurrentState.requested.w == w && mCurrentState.requested.h == h) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001226 mCurrentState.requested.w = w;
1227 mCurrentState.requested.h = h;
Dan Stoza7dde5992015-05-22 09:51:44 -07001228 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001229 setTransactionFlags(eTransactionNeeded);
1230 return true;
1231}
Dan Stoza9e56aa02015-11-02 13:00:03 -08001232bool Layer::setAlpha(float alpha) {
David Sodman41fdfc92017-11-06 16:09:56 -08001233 if (mCurrentState.color.a == alpha) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001234 mCurrentState.sequence++;
chaviw13fdc492017-06-27 12:40:18 -07001235 mCurrentState.color.a = alpha;
Dan Stoza7dde5992015-05-22 09:51:44 -07001236 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001237 setTransactionFlags(eTransactionNeeded);
1238 return true;
1239}
chaviw13fdc492017-06-27 12:40:18 -07001240
1241bool Layer::setColor(const half3& color) {
David Sodman41fdfc92017-11-06 16:09:56 -08001242 if (color.r == mCurrentState.color.r && color.g == mCurrentState.color.g &&
1243 color.b == mCurrentState.color.b)
chaviw13fdc492017-06-27 12:40:18 -07001244 return false;
1245
1246 mCurrentState.sequence++;
1247 mCurrentState.color.r = color.r;
1248 mCurrentState.color.g = color.g;
1249 mCurrentState.color.b = color.b;
1250 mCurrentState.modified = true;
1251 setTransactionFlags(eTransactionNeeded);
1252 return true;
1253}
1254
Mathias Agopian13127d82013-03-05 17:47:11 -08001255bool Layer::setMatrix(const layer_state_t::matrix22_t& matrix) {
1256 mCurrentState.sequence++;
David Sodman41fdfc92017-11-06 16:09:56 -08001257 mCurrentState.requested.transform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
Dan Stoza7dde5992015-05-22 09:51:44 -07001258 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001259 setTransactionFlags(eTransactionNeeded);
1260 return true;
1261}
1262bool Layer::setTransparentRegionHint(const Region& transparent) {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001263 mCurrentState.requestedTransparentRegion = transparent;
Dan Stoza7dde5992015-05-22 09:51:44 -07001264 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001265 setTransactionFlags(eTransactionNeeded);
1266 return true;
1267}
1268bool Layer::setFlags(uint8_t flags, uint8_t mask) {
1269 const uint32_t newFlags = (mCurrentState.flags & ~mask) | (flags & mask);
David Sodman41fdfc92017-11-06 16:09:56 -08001270 if (mCurrentState.flags == newFlags) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001271 mCurrentState.sequence++;
1272 mCurrentState.flags = newFlags;
Dan Stoza7dde5992015-05-22 09:51:44 -07001273 mCurrentState.mask = mask;
1274 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001275 setTransactionFlags(eTransactionNeeded);
1276 return true;
1277}
Robert Carr99e27f02016-06-16 15:18:02 -07001278
1279bool Layer::setCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001280 if (mCurrentState.requestedCrop == crop) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001281 mCurrentState.sequence++;
Robert Carr99e27f02016-06-16 15:18:02 -07001282 mCurrentState.requestedCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001283 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr99e27f02016-06-16 15:18:02 -07001284 mCurrentState.crop = crop;
1285 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001286 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1287
Dan Stoza7dde5992015-05-22 09:51:44 -07001288 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001289 setTransactionFlags(eTransactionNeeded);
1290 return true;
1291}
Robert Carr8d5227b2017-03-16 15:41:03 -07001292
1293bool Layer::setFinalCrop(const Rect& crop, bool immediate) {
David Sodman41fdfc92017-11-06 16:09:56 -08001294 if (mCurrentState.requestedFinalCrop == crop) return false;
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001295 mCurrentState.sequence++;
Robert Carr8d5227b2017-03-16 15:41:03 -07001296 mCurrentState.requestedFinalCrop = crop;
Robert Carr7bf247e2017-05-18 14:02:49 -07001297 if (immediate && !mFreezeGeometryUpdates) {
Robert Carr8d5227b2017-03-16 15:41:03 -07001298 mCurrentState.finalCrop = crop;
1299 }
Robert Carr7bf247e2017-05-18 14:02:49 -07001300 mFreezeGeometryUpdates = mFreezeGeometryUpdates || !immediate;
1301
Pablo Ceballosacbe6782016-03-04 17:54:21 +00001302 mCurrentState.modified = true;
1303 setTransactionFlags(eTransactionNeeded);
1304 return true;
1305}
Mathias Agopian13127d82013-03-05 17:47:11 -08001306
Robert Carrc3574f72016-03-24 12:19:32 -07001307bool Layer::setOverrideScalingMode(int32_t scalingMode) {
David Sodman41fdfc92017-11-06 16:09:56 -08001308 if (scalingMode == mOverrideScalingMode) return false;
Robert Carrc3574f72016-03-24 12:19:32 -07001309 mOverrideScalingMode = scalingMode;
Robert Carr82364e32016-05-15 11:27:47 -07001310 setTransactionFlags(eTransactionNeeded);
Robert Carrc3574f72016-03-24 12:19:32 -07001311 return true;
1312}
1313
rongliuccd34842018-03-14 12:26:23 -07001314void Layer::setInfo(int32_t type, int32_t appId) {
David Sodman41fdfc92017-11-06 16:09:56 -08001315 mCurrentState.appId = appId;
1316 mCurrentState.type = type;
1317 mCurrentState.modified = true;
1318 setTransactionFlags(eTransactionNeeded);
Daniel Nicoara2f5f8a52016-12-20 16:11:58 -05001319}
1320
Mathias Agopian13127d82013-03-05 17:47:11 -08001321bool Layer::setLayerStack(uint32_t layerStack) {
David Sodman41fdfc92017-11-06 16:09:56 -08001322 if (mCurrentState.layerStack == layerStack) return false;
Mathias Agopian13127d82013-03-05 17:47:11 -08001323 mCurrentState.sequence++;
1324 mCurrentState.layerStack = layerStack;
Dan Stoza7dde5992015-05-22 09:51:44 -07001325 mCurrentState.modified = true;
Mathias Agopian13127d82013-03-05 17:47:11 -08001326 setTransactionFlags(eTransactionNeeded);
1327 return true;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001328}
1329
Peiyong Lin34beb7a2018-03-28 11:57:12 -07001330bool Layer::setDataSpace(ui::Dataspace dataSpace) {
David Sodman41fdfc92017-11-06 16:09:56 -08001331 if (mCurrentState.dataSpace == dataSpace) return false;
Courtney Goeltzenleuchterbb09b432016-11-30 13:51:28 -07001332 mCurrentState.sequence++;
1333 mCurrentState.dataSpace = dataSpace;
1334 mCurrentState.modified = true;
1335 setTransactionFlags(eTransactionNeeded);
1336 return true;
1337}
1338
Peiyong Lin34beb7a2018-03-28 11:57:12 -07001339ui::Dataspace Layer::getDataSpace() const {
Courtney Goeltzenleuchter532b2632017-05-05 16:34:38 -06001340 return mCurrentState.dataSpace;
1341}
1342
Robert Carr1f0a16a2016-10-24 16:27:39 -07001343uint32_t Layer::getLayerStack() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001344 auto p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001345 if (p == nullptr) {
1346 return getDrawingState().layerStack;
1347 }
1348 return p->getLayerStack();
1349}
1350
David Sodman41fdfc92017-11-06 16:09:56 -08001351void Layer::deferTransactionUntil(const sp<Layer>& barrierLayer, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001352 mCurrentState.barrierLayer = barrierLayer;
Dan Stoza7dde5992015-05-22 09:51:44 -07001353 mCurrentState.frameNumber = frameNumber;
1354 // We don't set eTransactionNeeded, because just receiving a deferral
1355 // request without any other state updates shouldn't actually induce a delay
1356 mCurrentState.modified = true;
1357 pushPendingState();
Robert Carr0d480722017-01-10 16:42:54 -08001358 mCurrentState.barrierLayer = nullptr;
Dan Stoza792e5292016-02-11 11:43:58 -08001359 mCurrentState.frameNumber = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -07001360 mCurrentState.modified = false;
Robert Carr0d480722017-01-10 16:42:54 -08001361}
1362
David Sodman41fdfc92017-11-06 16:09:56 -08001363void Layer::deferTransactionUntil(const sp<IBinder>& barrierHandle, uint64_t frameNumber) {
Robert Carr0d480722017-01-10 16:42:54 -08001364 sp<Handle> handle = static_cast<Handle*>(barrierHandle.get());
1365 deferTransactionUntil(handle->owner.promote(), frameNumber);
Dan Stoza7dde5992015-05-22 09:51:44 -07001366}
1367
Dan Stozaee44edd2015-03-23 15:50:23 -07001368
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001369// ----------------------------------------------------------------------------
1370// pageflip handling...
1371// ----------------------------------------------------------------------------
1372
Robert Carr1f0a16a2016-10-24 16:27:39 -07001373bool Layer::isHiddenByPolicy() const {
1374 const Layer::State& s(mDrawingState);
Chia-I Wue41dbe62017-06-13 14:10:56 -07001375 const auto& parent = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001376 if (parent != nullptr && parent->isHiddenByPolicy()) {
1377 return true;
1378 }
1379 return s.flags & layer_state_t::eLayerHidden;
1380}
1381
David Sodman41fdfc92017-11-06 16:09:56 -08001382uint32_t Layer::getEffectiveUsage(uint32_t usage) const {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001383 // TODO: should we do something special if mSecure is set?
1384 if (mProtectedByApp) {
1385 // need a hardware-protected path to external video sink
1386 usage |= GraphicBuffer::USAGE_PROTECTED;
Jamie Gennis54cc83e2010-11-02 11:51:32 -07001387 }
Riley Andrews03414a12014-07-01 14:22:59 -07001388 if (mPotentialCursor) {
1389 usage |= GraphicBuffer::USAGE_CURSOR;
1390 }
Jamie Gennis3599bf22011-08-10 11:48:07 -07001391 usage |= GraphicBuffer::USAGE_HW_COMPOSER;
Mathias Agopiana67932f2011-04-20 14:20:59 -07001392 return usage;
Mathias Agopianb5b7f262010-05-07 15:58:44 -07001393}
1394
Mathias Agopian84300952012-11-21 16:02:13 -08001395void Layer::updateTransformHint(const sp<const DisplayDevice>& hw) const {
Mathias Agopiana4583642011-08-23 18:03:18 -07001396 uint32_t orientation = 0;
1397 if (!mFlinger->mDebugDisableTransformHint) {
Mathias Agopian84300952012-11-21 16:02:13 -08001398 // The transform hint is used to improve performance, but we can
1399 // only have a single transform hint, it cannot
Mathias Agopian4fec8732012-06-29 14:12:52 -07001400 // apply to all displays.
Mathias Agopian42977342012-08-05 00:40:46 -07001401 const Transform& planeTransform(hw->getTransform());
Mathias Agopian4fec8732012-06-29 14:12:52 -07001402 orientation = planeTransform.getOrientation();
Mathias Agopiana4583642011-08-23 18:03:18 -07001403 if (orientation & Transform::ROT_INVALID) {
1404 orientation = 0;
1405 }
1406 }
David Sodmaneb085e02017-10-05 18:49:04 -07001407 setTransformHint(orientation);
Mathias Agopiana4583642011-08-23 18:03:18 -07001408}
1409
Mathias Agopian13127d82013-03-05 17:47:11 -08001410// ----------------------------------------------------------------------------
1411// debugging
1412// ----------------------------------------------------------------------------
1413
Kalle Raitaa099a242017-01-11 11:17:29 -08001414LayerDebugInfo Layer::getLayerDebugInfo() const {
1415 LayerDebugInfo info;
1416 const Layer::State& ds = getDrawingState();
1417 info.mName = getName();
chaviw1acbec72017-07-27 15:28:26 -07001418 sp<Layer> parent = getParent();
Kalle Raitaa099a242017-01-11 11:17:29 -08001419 info.mParentName = (parent == nullptr ? std::string("none") : parent->getName().string());
1420 info.mType = String8(getTypeId());
1421 info.mTransparentRegion = ds.activeTransparentRegion;
1422 info.mVisibleRegion = visibleRegion;
1423 info.mSurfaceDamageRegion = surfaceDamageRegion;
1424 info.mLayerStack = getLayerStack();
1425 info.mX = ds.active.transform.tx();
1426 info.mY = ds.active.transform.ty();
1427 info.mZ = ds.z;
1428 info.mWidth = ds.active.w;
1429 info.mHeight = ds.active.h;
1430 info.mCrop = ds.crop;
1431 info.mFinalCrop = ds.finalCrop;
chaviw13fdc492017-06-27 12:40:18 -07001432 info.mColor = ds.color;
Kalle Raitaa099a242017-01-11 11:17:29 -08001433 info.mFlags = ds.flags;
1434 info.mPixelFormat = getPixelFormat();
Peiyong Lin34beb7a2018-03-28 11:57:12 -07001435 info.mDataSpace = static_cast<android_dataspace>(getDataSpace());
Kalle Raitaa099a242017-01-11 11:17:29 -08001436 info.mMatrix[0][0] = ds.active.transform[0][0];
1437 info.mMatrix[0][1] = ds.active.transform[0][1];
1438 info.mMatrix[1][0] = ds.active.transform[1][0];
1439 info.mMatrix[1][1] = ds.active.transform[1][1];
1440 {
David Sodman0cc69182017-11-17 12:12:07 -08001441 sp<const GraphicBuffer> buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001442 if (buffer != 0) {
1443 info.mActiveBufferWidth = buffer->getWidth();
1444 info.mActiveBufferHeight = buffer->getHeight();
1445 info.mActiveBufferStride = buffer->getStride();
1446 info.mActiveBufferFormat = buffer->format;
Kalle Raitaa099a242017-01-11 11:17:29 -08001447 } else {
1448 info.mActiveBufferWidth = 0;
1449 info.mActiveBufferHeight = 0;
1450 info.mActiveBufferStride = 0;
1451 info.mActiveBufferFormat = 0;
1452 }
Mathias Agopian13127d82013-03-05 17:47:11 -08001453 }
Kalle Raitaa099a242017-01-11 11:17:29 -08001454 info.mNumQueuedFrames = getQueuedFrameCount();
1455 info.mRefreshPending = isBufferLatched();
1456 info.mIsOpaque = isOpaque(ds);
1457 info.mContentDirty = contentDirty;
1458 return info;
Mathias Agopian13127d82013-03-05 17:47:11 -08001459}
Chia-I Wu83ce7c12017-10-19 15:18:55 -07001460
Dan Stozae22aec72016-08-01 13:20:59 -07001461void Layer::miniDumpHeader(String8& result) {
1462 result.append("----------------------------------------");
1463 result.append("---------------------------------------\n");
1464 result.append(" Layer name\n");
1465 result.append(" Z | ");
1466 result.append(" Comp Type | ");
1467 result.append(" Disp Frame (LTRB) | ");
1468 result.append(" Source Crop (LTRB)\n");
1469 result.append("----------------------------------------");
1470 result.append("---------------------------------------\n");
1471}
1472
1473void Layer::miniDump(String8& result, int32_t hwcId) const {
David Sodman6f65f3e2017-11-03 14:28:09 -07001474 if (getBE().mHwcLayers.count(hwcId) == 0) {
Dan Stozae22aec72016-08-01 13:20:59 -07001475 return;
1476 }
1477
1478 String8 name;
1479 if (mName.length() > 77) {
1480 std::string shortened;
1481 shortened.append(mName.string(), 36);
1482 shortened.append("[...]");
1483 shortened.append(mName.string() + (mName.length() - 36), 36);
1484 name = shortened.c_str();
1485 } else {
1486 name = mName;
1487 }
1488
1489 result.appendFormat(" %s\n", name.string());
1490
1491 const Layer::State& layerState(getDrawingState());
David Sodman6f65f3e2017-11-03 14:28:09 -07001492 const LayerBE::HWCInfo& hwcInfo = getBE().mHwcLayers.at(hwcId);
Chia-I Wu1e043612018-03-01 09:45:09 -08001493 if (layerState.zOrderRelativeOf != nullptr || mDrawingParent != nullptr) {
1494 result.appendFormat(" rel %6d | ", layerState.z);
1495 } else {
1496 result.appendFormat(" %10d | ", layerState.z);
1497 }
David Sodman41fdfc92017-11-06 16:09:56 -08001498 result.appendFormat("%10s | ", to_string(getCompositionType(hwcId)).c_str());
Dan Stozae22aec72016-08-01 13:20:59 -07001499 const Rect& frame = hwcInfo.displayFrame;
David Sodman41fdfc92017-11-06 16:09:56 -08001500 result.appendFormat("%4d %4d %4d %4d | ", frame.left, frame.top, frame.right, frame.bottom);
Dan Stoza5a423ea2017-02-16 14:10:39 -08001501 const FloatRect& crop = hwcInfo.sourceCrop;
David Sodman41fdfc92017-11-06 16:09:56 -08001502 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 -07001503
1504 result.append("- - - - - - - - - - - - - - - - - - - - ");
1505 result.append("- - - - - - - - - - - - - - - - - - - -\n");
1506}
Dan Stozae22aec72016-08-01 13:20:59 -07001507
Svetoslavd85084b2014-03-20 10:28:31 -07001508void Layer::dumpFrameStats(String8& result) const {
1509 mFrameTracker.dumpStats(result);
Mathias Agopian13127d82013-03-05 17:47:11 -08001510}
1511
Svetoslavd85084b2014-03-20 10:28:31 -07001512void Layer::clearFrameStats() {
1513 mFrameTracker.clearStats();
Mathias Agopian13127d82013-03-05 17:47:11 -08001514}
1515
Jamie Gennis6547ff42013-07-16 20:12:42 -07001516void Layer::logFrameStats() {
1517 mFrameTracker.logAndResetStats(mName);
1518}
1519
Svetoslavd85084b2014-03-20 10:28:31 -07001520void Layer::getFrameStats(FrameStats* outStats) const {
1521 mFrameTracker.getStats(outStats);
1522}
1523
Brian Andersond6927fb2016-07-23 23:37:30 -07001524void Layer::dumpFrameEvents(String8& result) {
David Sodman41fdfc92017-11-06 16:09:56 -08001525 result.appendFormat("- Layer %s (%s, %p)\n", getName().string(), getTypeId(), this);
Brian Andersond6927fb2016-07-23 23:37:30 -07001526 Mutex::Autolock lock(mFrameEventHistoryMutex);
1527 mFrameEventHistory.checkFencesForCompletion();
1528 mFrameEventHistory.dump(result);
1529}
Pablo Ceballos40845df2016-01-25 17:41:15 -08001530
Brian Anderson5ea5e592016-12-01 16:54:33 -08001531void Layer::onDisconnect() {
1532 Mutex::Autolock lock(mFrameEventHistoryMutex);
1533 mFrameEventHistory.onDisconnect();
1534}
1535
Brian Anderson3890c392016-07-25 12:48:08 -07001536void Layer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
David Sodman41fdfc92017-11-06 16:09:56 -08001537 FrameEventHistoryDelta* outDelta) {
Brian Andersond6927fb2016-07-23 23:37:30 -07001538 Mutex::Autolock lock(mFrameEventHistoryMutex);
1539 if (newTimestamps) {
Brian Andersonfbc80ae2017-05-26 16:23:54 -07001540 // If there are any unsignaled fences in the aquire timeline at this
1541 // point, the previously queued frame hasn't been latched yet. Go ahead
1542 // and try to get the signal time here so the syscall is taken out of
1543 // the main thread's critical path.
1544 mAcquireTimeline.updateSignalTimes();
1545 // Push the new fence after updating since it's likely still pending.
Brian Anderson3d4039d2016-09-23 16:31:30 -07001546 mAcquireTimeline.push(newTimestamps->acquireFence);
Brian Andersond6927fb2016-07-23 23:37:30 -07001547 mFrameEventHistory.addQueue(*newTimestamps);
1548 }
1549
Brian Anderson3890c392016-07-25 12:48:08 -07001550 if (outDelta) {
1551 mFrameEventHistory.getAndResetDelta(outDelta);
Brian Andersond6927fb2016-07-23 23:37:30 -07001552 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08001553}
Dan Stozae77c7662016-05-13 11:37:28 -07001554
Chia-I Wu98f1c102017-05-30 14:54:08 -07001555size_t Layer::getChildrenCount() const {
1556 size_t count = 0;
1557 for (const sp<Layer>& child : mCurrentChildren) {
1558 count += 1 + child->getChildrenCount();
1559 }
1560 return count;
1561}
1562
Robert Carr1f0a16a2016-10-24 16:27:39 -07001563void Layer::addChild(const sp<Layer>& layer) {
1564 mCurrentChildren.add(layer);
1565 layer->setParent(this);
1566}
1567
1568ssize_t Layer::removeChild(const sp<Layer>& layer) {
1569 layer->setParent(nullptr);
1570 return mCurrentChildren.remove(layer);
1571}
1572
Robert Carr1db73f62016-12-21 12:58:51 -08001573bool Layer::reparentChildren(const sp<IBinder>& newParentHandle) {
1574 sp<Handle> handle = nullptr;
1575 sp<Layer> newParent = nullptr;
1576 if (newParentHandle == nullptr) {
1577 return false;
1578 }
1579 handle = static_cast<Handle*>(newParentHandle.get());
1580 newParent = handle->owner.promote();
1581 if (newParent == nullptr) {
1582 ALOGE("Unable to promote Layer handle");
1583 return false;
1584 }
1585
1586 for (const sp<Layer>& child : mCurrentChildren) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001587 newParent->addChild(child);
Robert Carr1db73f62016-12-21 12:58:51 -08001588
1589 sp<Client> client(child->mClientRef.promote());
1590 if (client != nullptr) {
1591 client->setParentLayer(newParent);
1592 }
1593 }
1594 mCurrentChildren.clear();
1595
1596 return true;
1597}
1598
Robert Carr15eae092018-03-23 13:43:53 -07001599void Layer::setChildrenDrawingParent(const sp<Layer>& newParent) {
Robert Carr578038f2018-03-09 12:25:24 -08001600 for (const sp<Layer>& child : mDrawingChildren) {
1601 child->mDrawingParent = newParent;
1602 }
1603}
1604
chaviwf1961f72017-09-18 16:41:07 -07001605bool Layer::reparent(const sp<IBinder>& newParentHandle) {
1606 if (newParentHandle == nullptr) {
chaviw06178942017-07-27 10:25:59 -07001607 return false;
1608 }
1609
1610 auto handle = static_cast<Handle*>(newParentHandle.get());
1611 sp<Layer> newParent = handle->owner.promote();
1612 if (newParent == nullptr) {
1613 ALOGE("Unable to promote Layer handle");
1614 return false;
1615 }
1616
chaviwf1961f72017-09-18 16:41:07 -07001617 sp<Layer> parent = getParent();
1618 if (parent != nullptr) {
1619 parent->removeChild(this);
chaviw06178942017-07-27 10:25:59 -07001620 }
chaviwf1961f72017-09-18 16:41:07 -07001621 newParent->addChild(this);
chaviw06178942017-07-27 10:25:59 -07001622
chaviwf1961f72017-09-18 16:41:07 -07001623 sp<Client> client(mClientRef.promote());
chaviw06178942017-07-27 10:25:59 -07001624 sp<Client> newParentClient(newParent->mClientRef.promote());
1625
chaviwf1961f72017-09-18 16:41:07 -07001626 if (client != newParentClient) {
1627 client->setParentLayer(newParent);
chaviw06178942017-07-27 10:25:59 -07001628 }
1629
chaviw06178942017-07-27 10:25:59 -07001630 return true;
1631}
1632
Robert Carr9524cb32017-02-13 11:32:32 -08001633bool Layer::detachChildren() {
Robert Carr7f619b22017-11-06 12:56:35 -08001634 for (const sp<Layer>& child : mCurrentChildren) {
chaviw161410b02017-07-27 10:46:08 -07001635 sp<Client> parentClient = mClientRef.promote();
Robert Carr9524cb32017-02-13 11:32:32 -08001636 sp<Client> client(child->mClientRef.promote());
chaviw161410b02017-07-27 10:46:08 -07001637 if (client != nullptr && parentClient != client) {
Robert Carr7f619b22017-11-06 12:56:35 -08001638 client->detachLayer(child.get());
1639 child->detachChildren();
Robert Carr9524cb32017-02-13 11:32:32 -08001640 }
Robert Carr7f619b22017-11-06 12:56:35 -08001641 }
Robert Carr9524cb32017-02-13 11:32:32 -08001642
1643 return true;
1644}
1645
Peiyong Lindd9b2ae2018-03-01 16:22:45 -08001646// Dataspace::UNKNOWN, Dataspace::SRGB, Dataspace::SRGB_LINEAR,
1647// Dataspace::V0_SRGB and Dataspace::V0_SRGB_LINEAR are considered legacy
1648// SRGB data space for now.
1649// Note that Dataspace::V0_SRGB and Dataspace::V0_SRGB_LINEAR are not legacy
1650// data space, however since framework doesn't distinguish them out of legacy
1651// SRGB, we have to treat them as the same for now.
1652bool Layer::isLegacySrgbDataSpace() const {
1653 // TODO(lpy) b/77652630, need to figure out when UNKNOWN can be treated as SRGB.
1654 return mDrawingState.dataSpace == ui::Dataspace::UNKNOWN ||
1655 mDrawingState.dataSpace == ui::Dataspace::SRGB ||
1656 mDrawingState.dataSpace == ui::Dataspace::SRGB_LINEAR ||
1657 mDrawingState.dataSpace == ui::Dataspace::V0_SRGB ||
1658 mDrawingState.dataSpace == ui::Dataspace::V0_SRGB_LINEAR;
1659}
1660
Robert Carr1f0a16a2016-10-24 16:27:39 -07001661void Layer::setParent(const sp<Layer>& layer) {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001662 mCurrentParent = layer;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001663}
1664
1665void Layer::clearSyncPoints() {
1666 for (const auto& child : mCurrentChildren) {
1667 child->clearSyncPoints();
1668 }
1669
1670 Mutex::Autolock lock(mLocalSyncPointMutex);
1671 for (auto& point : mLocalSyncPoints) {
1672 point->setFrameAvailable();
1673 }
1674 mLocalSyncPoints.clear();
1675}
1676
1677int32_t Layer::getZ() const {
1678 return mDrawingState.z;
1679}
1680
Robert Carr29abff82017-12-04 13:51:20 -08001681bool Layer::usingRelativeZ(LayerVector::StateSet stateSet) {
1682 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1683 const State& state = useDrawing ? mDrawingState : mCurrentState;
1684 return state.zOrderRelativeOf != nullptr;
1685}
1686
David Sodman41fdfc92017-11-06 16:09:56 -08001687__attribute__((no_sanitize("unsigned-integer-overflow"))) LayerVector Layer::makeTraversalList(
Robert Carr29abff82017-12-04 13:51:20 -08001688 LayerVector::StateSet stateSet, bool* outSkipRelativeZUsers) {
Dan Stoza412903f2017-04-27 13:42:17 -07001689 LOG_ALWAYS_FATAL_IF(stateSet == LayerVector::StateSet::Invalid,
1690 "makeTraversalList received invalid stateSet");
1691 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1692 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1693 const State& state = useDrawing ? mDrawingState : mCurrentState;
1694
Robert Carr29abff82017-12-04 13:51:20 -08001695 if (state.zOrderRelatives.size() == 0) {
1696 *outSkipRelativeZUsers = true;
1697 return children;
1698 }
1699
Robert Carrdb66e622017-04-10 16:55:57 -07001700 LayerVector traverse;
Dan Stoza412903f2017-04-27 13:42:17 -07001701 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
Robert Carrdb66e622017-04-10 16:55:57 -07001702 sp<Layer> strongRelative = weakRelative.promote();
1703 if (strongRelative != nullptr) {
1704 traverse.add(strongRelative);
Robert Carrdb66e622017-04-10 16:55:57 -07001705 }
1706 }
1707
Dan Stoza412903f2017-04-27 13:42:17 -07001708 for (const sp<Layer>& child : children) {
Robert Carr503c7042017-09-27 15:06:08 -07001709 const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState;
1710 if (childState.zOrderRelativeOf != nullptr) {
1711 continue;
1712 }
Robert Carrdb66e622017-04-10 16:55:57 -07001713 traverse.add(child);
1714 }
1715
1716 return traverse;
1717}
1718
Robert Carr1f0a16a2016-10-24 16:27:39 -07001719/**
Robert Carrdb66e622017-04-10 16:55:57 -07001720 * Negatively signed relatives are before 'this' in Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001721 */
Dan Stoza412903f2017-04-27 13:42:17 -07001722void Layer::traverseInZOrder(LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001723 // In the case we have other layers who are using a relative Z to us, makeTraversalList will
1724 // produce a new list for traversing, including our relatives, and not including our children
1725 // who are relatives of another surface. In the case that there are no relative Z,
1726 // makeTraversalList returns our children directly to avoid significant overhead.
1727 // However in this case we need to take the responsibility for filtering children which
1728 // are relatives of another surface here.
1729 bool skipRelativeZUsers = false;
1730 const LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001731
Robert Carr1f0a16a2016-10-24 16:27:39 -07001732 size_t i = 0;
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 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1736 continue;
1737 }
1738
Robert Carrdb66e622017-04-10 16:55:57 -07001739 if (relative->getZ() >= 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001740 break;
Robert Carrdb66e622017-04-10 16:55:57 -07001741 }
Dan Stoza412903f2017-04-27 13:42:17 -07001742 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001743 }
Robert Carr29abff82017-12-04 13:51:20 -08001744
Dan Stoza412903f2017-04-27 13:42:17 -07001745 visitor(this);
Robert Carrdb66e622017-04-10 16:55:57 -07001746 for (; i < list.size(); i++) {
1747 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001748
1749 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1750 continue;
1751 }
Dan Stoza412903f2017-04-27 13:42:17 -07001752 relative->traverseInZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001753 }
1754}
1755
1756/**
Robert Carrdb66e622017-04-10 16:55:57 -07001757 * Positively signed relatives are before 'this' in reverse Z-order.
Robert Carr1f0a16a2016-10-24 16:27:39 -07001758 */
Dan Stoza412903f2017-04-27 13:42:17 -07001759void Layer::traverseInReverseZOrder(LayerVector::StateSet stateSet,
1760 const LayerVector::Visitor& visitor) {
Robert Carr29abff82017-12-04 13:51:20 -08001761 // See traverseInZOrder for documentation.
1762 bool skipRelativeZUsers = false;
1763 LayerVector list = makeTraversalList(stateSet, &skipRelativeZUsers);
Robert Carrdb66e622017-04-10 16:55:57 -07001764
Robert Carr1f0a16a2016-10-24 16:27:39 -07001765 int32_t i = 0;
Joel Galensonbf324992017-11-06 11:04:12 -08001766 for (i = int32_t(list.size()) - 1; 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
Robert Carrdb66e622017-04-10 16:55:57 -07001773 if (relative->getZ() < 0) {
Robert Carr1f0a16a2016-10-24 16:27:39 -07001774 break;
1775 }
Dan Stoza412903f2017-04-27 13:42:17 -07001776 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001777 }
Dan Stoza412903f2017-04-27 13:42:17 -07001778 visitor(this);
David Sodman41fdfc92017-11-06 16:09:56 -08001779 for (; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -07001780 const auto& relative = list[i];
Robert Carr29abff82017-12-04 13:51:20 -08001781
1782 if (skipRelativeZUsers && relative->usingRelativeZ(stateSet)) {
1783 continue;
1784 }
1785
Dan Stoza412903f2017-04-27 13:42:17 -07001786 relative->traverseInReverseZOrder(stateSet, visitor);
Robert Carr1f0a16a2016-10-24 16:27:39 -07001787 }
1788}
1789
chaviwa76b2712017-09-20 12:02:26 -07001790/**
1791 * Traverse only children in z order, ignoring relative layers.
1792 */
1793void Layer::traverseChildrenInZOrder(LayerVector::StateSet stateSet,
1794 const LayerVector::Visitor& visitor) {
1795 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1796 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1797
1798 size_t i = 0;
1799 for (; i < children.size(); i++) {
1800 const auto& relative = children[i];
1801 if (relative->getZ() >= 0) {
1802 break;
1803 }
1804 relative->traverseChildrenInZOrder(stateSet, visitor);
1805 }
1806 visitor(this);
1807 for (; i < children.size(); i++) {
1808 const auto& relative = children[i];
1809 relative->traverseChildrenInZOrder(stateSet, visitor);
1810 }
1811}
1812
Robert Carr1f0a16a2016-10-24 16:27:39 -07001813Transform Layer::getTransform() const {
1814 Transform t;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001815 const auto& p = mDrawingParent.promote();
Robert Carr1f0a16a2016-10-24 16:27:39 -07001816 if (p != nullptr) {
1817 t = p->getTransform();
Robert Carr9b429f42017-04-17 14:56:57 -07001818
1819 // If the parent is not using NATIVE_WINDOW_SCALING_MODE_FREEZE (e.g.
1820 // it isFixedSize) then there may be additional scaling not accounted
1821 // for in the transform. We need to mirror this scaling in child surfaces
1822 // or we will break the contract where WM can treat child surfaces as
1823 // pixels in the parent surface.
David Sodman0cc69182017-11-17 12:12:07 -08001824 if (p->isFixedSize() && p->getBE().compositionInfo.mBuffer != nullptr) {
Robert Carr1725eee2017-04-26 18:32:15 -07001825 int bufferWidth;
1826 int bufferHeight;
1827 if ((p->mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) == 0) {
David Sodman0cc69182017-11-17 12:12:07 -08001828 bufferWidth = p->getBE().compositionInfo.mBuffer->getWidth();
1829 bufferHeight = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001830 } else {
David Sodman0cc69182017-11-17 12:12:07 -08001831 bufferHeight = p->getBE().compositionInfo.mBuffer->getWidth();
1832 bufferWidth = p->getBE().compositionInfo.mBuffer->getHeight();
Robert Carr1725eee2017-04-26 18:32:15 -07001833 }
David Sodman41fdfc92017-11-06 16:09:56 -08001834 float sx = p->getDrawingState().active.w / static_cast<float>(bufferWidth);
1835 float sy = p->getDrawingState().active.h / static_cast<float>(bufferHeight);
Robert Carr9b429f42017-04-17 14:56:57 -07001836 Transform extraParentScaling;
1837 extraParentScaling.set(sx, 0, 0, sy);
1838 t = t * extraParentScaling;
1839 }
Robert Carr1f0a16a2016-10-24 16:27:39 -07001840 }
1841 return t * getDrawingState().active.transform;
1842}
1843
chaviw13fdc492017-06-27 12:40:18 -07001844half Layer::getAlpha() const {
Chia-I Wue41dbe62017-06-13 14:10:56 -07001845 const auto& p = mDrawingParent.promote();
Robert Carr6452f122017-03-21 10:41:29 -07001846
chaviw13fdc492017-06-27 12:40:18 -07001847 half parentAlpha = (p != nullptr) ? p->getAlpha() : 1.0_hf;
1848 return parentAlpha * getDrawingState().color.a;
Robert Carr6452f122017-03-21 10:41:29 -07001849}
Robert Carr6452f122017-03-21 10:41:29 -07001850
chaviw13fdc492017-06-27 12:40:18 -07001851half4 Layer::getColor() const {
1852 const half4 color(getDrawingState().color);
1853 return half4(color.r, color.g, color.b, getAlpha());
Robert Carr6452f122017-03-21 10:41:29 -07001854}
Robert Carr6452f122017-03-21 10:41:29 -07001855
Robert Carr1f0a16a2016-10-24 16:27:39 -07001856void Layer::commitChildList() {
1857 for (size_t i = 0; i < mCurrentChildren.size(); i++) {
1858 const auto& child = mCurrentChildren[i];
1859 child->commitChildList();
1860 }
1861 mDrawingChildren = mCurrentChildren;
Chia-I Wue41dbe62017-06-13 14:10:56 -07001862 mDrawingParent = mCurrentParent;
Robert Carr1f0a16a2016-10-24 16:27:39 -07001863}
1864
chaviw1d044282017-09-27 12:19:28 -07001865void Layer::writeToProto(LayerProto* layerInfo, LayerVector::StateSet stateSet) {
1866 const bool useDrawing = stateSet == LayerVector::StateSet::Drawing;
1867 const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren;
1868 const State& state = useDrawing ? mDrawingState : mCurrentState;
1869
1870 Transform requestedTransform = state.active.transform;
1871 Transform transform = getTransform();
1872
1873 layerInfo->set_id(sequence);
1874 layerInfo->set_name(getName().c_str());
1875 layerInfo->set_type(String8(getTypeId()));
1876
1877 for (const auto& child : children) {
1878 layerInfo->add_children(child->sequence);
1879 }
1880
1881 for (const wp<Layer>& weakRelative : state.zOrderRelatives) {
1882 sp<Layer> strongRelative = weakRelative.promote();
1883 if (strongRelative != nullptr) {
1884 layerInfo->add_relatives(strongRelative->sequence);
1885 }
1886 }
1887
1888 LayerProtoHelper::writeToProto(state.activeTransparentRegion,
1889 layerInfo->mutable_transparent_region());
1890 LayerProtoHelper::writeToProto(visibleRegion, layerInfo->mutable_visible_region());
1891 LayerProtoHelper::writeToProto(surfaceDamageRegion, layerInfo->mutable_damage_region());
1892
1893 layerInfo->set_layer_stack(getLayerStack());
1894 layerInfo->set_z(state.z);
1895
1896 PositionProto* position = layerInfo->mutable_position();
1897 position->set_x(transform.tx());
1898 position->set_y(transform.ty());
1899
1900 PositionProto* requestedPosition = layerInfo->mutable_requested_position();
1901 requestedPosition->set_x(requestedTransform.tx());
1902 requestedPosition->set_y(requestedTransform.ty());
1903
1904 SizeProto* size = layerInfo->mutable_size();
1905 size->set_w(state.active.w);
1906 size->set_h(state.active.h);
1907
1908 LayerProtoHelper::writeToProto(state.crop, layerInfo->mutable_crop());
1909 LayerProtoHelper::writeToProto(state.finalCrop, layerInfo->mutable_final_crop());
1910
1911 layerInfo->set_is_opaque(isOpaque(state));
1912 layerInfo->set_invalidate(contentDirty);
Peiyong Lin34beb7a2018-03-28 11:57:12 -07001913 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(getDataSpace())));
chaviw1d044282017-09-27 12:19:28 -07001914 layerInfo->set_pixel_format(decodePixelFormat(getPixelFormat()));
1915 LayerProtoHelper::writeToProto(getColor(), layerInfo->mutable_color());
1916 LayerProtoHelper::writeToProto(state.color, layerInfo->mutable_requested_color());
1917 layerInfo->set_flags(state.flags);
1918
1919 LayerProtoHelper::writeToProto(transform, layerInfo->mutable_transform());
1920 LayerProtoHelper::writeToProto(requestedTransform, layerInfo->mutable_requested_transform());
1921
Jorim Jaggi8e0af362017-11-14 16:28:28 +01001922 auto parent = useDrawing ? mDrawingParent.promote() : mCurrentParent.promote();
chaviw1d044282017-09-27 12:19:28 -07001923 if (parent != nullptr) {
1924 layerInfo->set_parent(parent->sequence);
1925 }
1926
1927 auto zOrderRelativeOf = state.zOrderRelativeOf.promote();
1928 if (zOrderRelativeOf != nullptr) {
1929 layerInfo->set_z_order_relative_of(zOrderRelativeOf->sequence);
1930 }
1931
David Sodman0cc69182017-11-17 12:12:07 -08001932 auto buffer = getBE().compositionInfo.mBuffer;
David Sodman5b4cffc2017-11-23 13:20:29 -08001933 if (buffer != nullptr) {
1934 LayerProtoHelper::writeToProto(buffer, layerInfo->mutable_active_buffer());
chaviw1d044282017-09-27 12:19:28 -07001935 }
1936
1937 layerInfo->set_queued_frames(getQueuedFrameCount());
1938 layerInfo->set_refresh_pending(isBufferLatched());
rongliuccd34842018-03-14 12:26:23 -07001939 layerInfo->set_window_type(state.type);
1940 layerInfo->set_app_id(state.appId);
chaviw1d044282017-09-27 12:19:28 -07001941}
1942
Yiwei Zhang068e31b2018-02-21 13:02:45 -08001943void Layer::writeToProto(LayerProto* layerInfo, int32_t hwcId) {
1944 writeToProto(layerInfo, LayerVector::StateSet::Drawing);
1945
1946 const auto& hwcInfo = getBE().mHwcLayers.at(hwcId);
1947
1948 const Rect& frame = hwcInfo.displayFrame;
1949 LayerProtoHelper::writeToProto(frame, layerInfo->mutable_hwc_frame());
1950
1951 const FloatRect& crop = hwcInfo.sourceCrop;
1952 LayerProtoHelper::writeToProto(crop, layerInfo->mutable_hwc_crop());
1953
1954 const int32_t transform = static_cast<int32_t>(hwcInfo.transform);
1955 layerInfo->set_hwc_transform(transform);
Yiwei Zhang7c64f172018-03-07 14:52:28 -08001956
1957 const int32_t compositionType = static_cast<int32_t>(hwcInfo.compositionType);
1958 layerInfo->set_hwc_composition_type(compositionType);
1959
1960 if (std::strcmp(getTypeId(), "BufferLayer") == 0 &&
1961 static_cast<BufferLayer*>(this)->isProtected()) {
1962 layerInfo->set_is_protected(true);
1963 } else {
1964 layerInfo->set_is_protected(false);
1965 }
Yiwei Zhang068e31b2018-02-21 13:02:45 -08001966}
1967
Mathias Agopian13127d82013-03-05 17:47:11 -08001968// ---------------------------------------------------------------------------
1969
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001970}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07001971
1972#if defined(__gl_h_)
1973#error "don't include gl/gl.h in this file"
1974#endif
1975
1976#if defined(__gl2_h_)
1977#error "don't include gl2/gl2.h in this file"
1978#endif