blob: 2cf4c1b7ddb30168630fef61404c15d3db6b2c07 [file] [log] [blame]
Vishnu Nairdc4d31b2022-11-17 03:20:58 +00001/*
2 * Copyright 2022 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 */
Vishnu Naira02943f2023-06-03 13:44:46 -070016// #define LOG_NDEBUG 0
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000017
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
19#undef LOG_TAG
Vishnu Naira02943f2023-06-03 13:44:46 -070020#define LOG_TAG "SurfaceFlinger"
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000021
Vishnu Nair4d9cef92023-06-24 22:34:41 +000022#include <gui/TraceUtils.h>
Vishnu Naircfb2d252023-01-19 04:44:02 +000023#include <log/log.h>
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000024#include <private/android_filesystem_config.h>
25#include <sys/types.h>
26
Rachel Leece6e0042023-06-27 11:22:54 -070027#include <scheduler/Fps.h>
28
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000029#include "Layer.h"
Vishnu Naircfb2d252023-01-19 04:44:02 +000030#include "LayerCreationArgs.h"
Vishnu Naircfb2d252023-01-19 04:44:02 +000031#include "LayerLog.h"
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000032#include "RequestedLayerState.h"
33
34namespace android::surfaceflinger::frontend {
35using ftl::Flags;
36using namespace ftl::flag_operators;
37
38namespace {
Vishnu Naira9c43762023-01-27 19:10:25 +000039std::string layerIdsToString(const std::vector<uint32_t>& layerIds) {
40 std::stringstream stream;
41 stream << "{";
42 for (auto layerId : layerIds) {
43 stream << layerId << ",";
44 }
45 stream << "}";
46 return stream.str();
47}
48
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000049} // namespace
50
51RequestedLayerState::RequestedLayerState(const LayerCreationArgs& args)
52 : id(args.sequence),
Vishnu Naircfb2d252023-01-19 04:44:02 +000053 name(args.name + "#" + std::to_string(args.sequence)),
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000054 canBeRoot(args.addToRoot),
55 layerCreationFlags(args.flags),
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000056 ownerUid(args.ownerUid),
Vishnu Nair1391de22023-03-05 19:56:14 -080057 ownerPid(args.ownerPid),
58 parentId(args.parentId),
59 layerIdToMirror(args.layerIdToMirror) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000060 layerId = static_cast<int32_t>(args.sequence);
61 changes |= RequestedLayerState::Changes::Created;
62 metadata.merge(args.metadata);
63 changes |= RequestedLayerState::Changes::Metadata;
64 handleAlive = true;
Vishnu Nair1391de22023-03-05 19:56:14 -080065 if (parentId != UNASSIGNED_LAYER_ID) {
Vishnu Naircfb2d252023-01-19 04:44:02 +000066 canBeRoot = false;
67 }
Vishnu Naira9c43762023-01-27 19:10:25 +000068 if (layerIdToMirror != UNASSIGNED_LAYER_ID) {
69 changes |= RequestedLayerState::Changes::Mirror;
70 } else if (args.layerStackToMirror != ui::INVALID_LAYER_STACK) {
71 layerStackToMirror = args.layerStackToMirror;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000072 changes |= RequestedLayerState::Changes::Mirror;
73 }
74
75 flags = 0;
76 if (args.flags & ISurfaceComposerClient::eHidden) flags |= layer_state_t::eLayerHidden;
77 if (args.flags & ISurfaceComposerClient::eOpaque) flags |= layer_state_t::eLayerOpaque;
78 if (args.flags & ISurfaceComposerClient::eSecure) flags |= layer_state_t::eLayerSecure;
79 if (args.flags & ISurfaceComposerClient::eSkipScreenshot) {
80 flags |= layer_state_t::eLayerSkipScreenshot;
81 }
82 premultipliedAlpha = !(args.flags & ISurfaceComposerClient::eNonPremultiplied);
83 potentialCursor = args.flags & ISurfaceComposerClient::eCursorWindow;
84 protectedByApp = args.flags & ISurfaceComposerClient::eProtectedByApp;
85 if (args.flags & ISurfaceComposerClient::eNoColorFill) {
86 // Set an invalid color so there is no color fill.
87 // (b/259981098) use an explicit flag instead of relying on invalid values.
88 color.r = -1.0_hf;
89 color.g = -1.0_hf;
90 color.b = -1.0_hf;
91 } else {
92 color.rgb = {0.0_hf, 0.0_hf, 0.0_hf};
93 }
Vishnu Naircfb2d252023-01-19 04:44:02 +000094 LLOGV(layerId, "Created %s flags=%d", getDebugString().c_str(), flags);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000095 color.a = 1.0f;
96
97 crop.makeInvalid();
98 z = 0;
99 layerStack = ui::DEFAULT_LAYER_STACK;
100 transformToDisplayInverse = false;
Sally Qi963049b2023-03-23 14:06:21 -0700101 desiredHdrSdrRatio = 1.f;
102 currentHdrSdrRatio = 1.f;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000103 dataspaceRequested = false;
104 hdrMetadata.validTypes = 0;
105 surfaceDamageRegion = Region::INVALID_REGION;
106 cornerRadius = 0.0f;
107 backgroundBlurRadius = 0;
108 api = -1;
109 hasColorTransform = false;
110 bufferTransform = 0;
111 requestedTransform.reset();
112 bufferData = std::make_shared<BufferData>();
113 bufferData->frameNumber = 0;
114 bufferData->acquireFence = sp<Fence>::make(-1);
115 acquireFenceTime = std::make_shared<FenceTime>(bufferData->acquireFence);
116 colorSpaceAgnostic = false;
117 frameRateSelectionPriority = Layer::PRIORITY_UNSET;
118 shadowRadius = 0.f;
119 fixedTransformHint = ui::Transform::ROT_INVALID;
120 destinationFrame.makeInvalid();
121 isTrustedOverlay = false;
122 dropInputMode = gui::DropInputMode::NONE;
123 dimmingEnabled = true;
Vishnu Nair3fbe3262023-09-29 17:07:00 -0700124 defaultFrameRateCompatibility = static_cast<int8_t>(scheduler::FrameRateCompatibility::Default);
Rachel Leece6e0042023-06-27 11:22:54 -0700125 frameRateCategory = static_cast<int8_t>(FrameRateCategory::Default);
Rachel Lee67afbea2023-09-28 15:35:07 -0700126 frameRateCategorySmoothSwitchOnly = false;
Rachel Lee58cc90d2023-09-05 18:50:20 -0700127 frameRateSelectionStrategy =
Rachel Lee70f7b692023-11-22 11:24:02 -0800128 static_cast<int8_t>(scheduler::LayerInfo::FrameRateSelectionStrategy::Propagate);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000129 dataspace = ui::Dataspace::V0_SRGB;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000130 gameMode = gui::GameMode::Unsupported;
131 requestedFrameRate = {};
Alec Mourif4af03e2023-02-11 00:25:24 +0000132 cachingHint = gui::CachingHint::Enabled;
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000133
134 if (name.length() > 77) {
135 std::string shortened;
136 shortened.append(name, 0, 36);
137 shortened.append("[...]");
138 shortened.append(name, name.length() - 36);
139 debugName = std::move(shortened);
140 } else {
141 debugName = name;
142 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000143}
144
145void RequestedLayerState::merge(const ResolvedComposerState& resolvedComposerState) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000146 const uint32_t oldFlags = flags;
147 const half oldAlpha = color.a;
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700148 const bool hadBuffer = externalTexture != nullptr;
Vishnu Naird1f74982023-06-15 20:16:51 -0700149 uint64_t oldFramenumber = hadBuffer ? bufferData->frameNumber : 0;
Vishnu Naira02943f2023-06-03 13:44:46 -0700150 const ui::Size oldBufferSize = hadBuffer
151 ? ui::Size(externalTexture->getWidth(), externalTexture->getHeight())
152 : ui::Size();
Vishnu Nair0808ae62023-08-07 21:42:42 -0700153 const uint64_t oldUsageFlags = hadBuffer ? externalTexture->getUsage() : 0;
Vishnu Nair61ff12a2023-08-30 21:41:46 -0700154 const bool oldBufferFormatOpaque = LayerSnapshot::isOpaqueFormat(
155 externalTexture ? externalTexture->getPixelFormat() : PIXEL_FORMAT_NONE);
Vishnu Nair0808ae62023-08-07 21:42:42 -0700156
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700157 const bool hadSideStream = sidebandStream != nullptr;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000158 const layer_state_t& clientState = resolvedComposerState.state;
Vishnu Nair854ce1c2023-08-19 15:00:13 -0700159 const bool hadSomethingToDraw = hasSomethingToDraw();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000160 uint64_t clientChanges = what | layer_state_t::diff(clientState);
161 layer_state_t::merge(clientState);
162 what = clientChanges;
Vishnu Naira02943f2023-06-03 13:44:46 -0700163 LLOGV(layerId, "requested=%" PRIu64 "flags=%" PRIu64, clientState.what, clientChanges);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000164
165 if (clientState.what & layer_state_t::eFlagsChanged) {
Vishnu Nair61ff12a2023-08-30 21:41:46 -0700166 if ((oldFlags ^ flags) & (layer_state_t::eLayerHidden | layer_state_t::eLayerOpaque)) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000167 changes |= RequestedLayerState::Changes::Visibility |
168 RequestedLayerState::Changes::VisibleRegion;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000169 }
170 if ((oldFlags ^ flags) & layer_state_t::eIgnoreDestinationFrame) {
171 changes |= RequestedLayerState::Changes::Geometry;
172 }
Vishnu Nair59a6be32024-01-29 10:26:21 -0800173 if ((oldFlags ^ flags) & layer_state_t::eCanOccludePresentation) {
174 changes |= RequestedLayerState::Changes::Input;
175 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000176 }
Vishnu Naird1f74982023-06-15 20:16:51 -0700177
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700178 if (clientState.what & layer_state_t::eBufferChanged) {
179 externalTexture = resolvedComposerState.externalTexture;
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700180 const bool hasBuffer = externalTexture != nullptr;
181 if (hasBuffer || hasBuffer != hadBuffer) {
182 changes |= RequestedLayerState::Changes::Buffer;
Vishnu Naira02943f2023-06-03 13:44:46 -0700183 const ui::Size newBufferSize = hasBuffer
184 ? ui::Size(externalTexture->getWidth(), externalTexture->getHeight())
185 : ui::Size();
186 if (oldBufferSize != newBufferSize) {
187 changes |= RequestedLayerState::Changes::BufferSize;
188 changes |= RequestedLayerState::Changes::Geometry;
189 }
Vishnu Nair0808ae62023-08-07 21:42:42 -0700190 const uint64_t usageFlags = hasBuffer ? externalTexture->getUsage() : 0;
191 if (oldUsageFlags != usageFlags) {
192 changes |= RequestedLayerState::Changes::BufferUsageFlags;
193 }
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700194 }
195
196 if (hasBuffer != hadBuffer) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000197 changes |= RequestedLayerState::Changes::Geometry |
198 RequestedLayerState::Changes::VisibleRegion |
Vishnu Naird47bcee2023-02-24 18:08:51 +0000199 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Input;
200 }
Vishnu Naird1f74982023-06-15 20:16:51 -0700201
202 if (hasBuffer) {
203 const bool frameNumberChanged =
204 bufferData->flags.test(BufferData::BufferDataChange::frameNumberChanged);
205 const uint64_t frameNumber =
206 frameNumberChanged ? bufferData->frameNumber : oldFramenumber + 1;
207 bufferData->frameNumber = frameNumber;
208
209 if ((barrierProducerId > bufferData->producerId) ||
210 ((barrierProducerId == bufferData->producerId) &&
211 (barrierFrameNumber > bufferData->frameNumber))) {
212 ALOGE("Out of order buffers detected for %s producedId=%d frameNumber=%" PRIu64
213 " -> producedId=%d frameNumber=%" PRIu64,
Patrick Williams0db92f12023-10-18 12:02:06 -0500214 getDebugString().c_str(), barrierProducerId, barrierFrameNumber,
Vishnu Naird1f74982023-06-15 20:16:51 -0700215 bufferData->producerId, frameNumber);
216 TransactionTraceWriter::getInstance().invoke("out_of_order_buffers_",
217 /*overwrite=*/false);
218 }
219
220 barrierProducerId = std::max(bufferData->producerId, barrierProducerId);
221 barrierFrameNumber = std::max(bufferData->frameNumber, barrierFrameNumber);
222 }
Vishnu Nair61ff12a2023-08-30 21:41:46 -0700223
224 const bool newBufferFormatOpaque = LayerSnapshot::isOpaqueFormat(
225 externalTexture ? externalTexture->getPixelFormat() : PIXEL_FORMAT_NONE);
226 if (newBufferFormatOpaque != oldBufferFormatOpaque) {
227 changes |= RequestedLayerState::Changes::Visibility |
228 RequestedLayerState::Changes::VisibleRegion;
229 }
Vishnu Nair63221212023-04-06 15:17:37 -0700230 }
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700231
Vishnu Nair63221212023-04-06 15:17:37 -0700232 if (clientState.what & layer_state_t::eSidebandStreamChanged) {
233 changes |= RequestedLayerState::Changes::SidebandStream;
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700234 const bool hasSideStream = sidebandStream != nullptr;
235 if (hasSideStream != hadSideStream) {
236 changes |= RequestedLayerState::Changes::Geometry |
237 RequestedLayerState::Changes::VisibleRegion |
238 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Input;
239 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000240 }
241 if (what & (layer_state_t::eAlphaChanged)) {
242 if (oldAlpha == 0 || color.a == 0) {
Vishnu Nair93e26b92023-10-16 21:36:51 -0700243 changes |= RequestedLayerState::Changes::Visibility;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000244 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000245 }
Vishnu Nair854ce1c2023-08-19 15:00:13 -0700246
247 if (hadSomethingToDraw != hasSomethingToDraw()) {
248 changes |= RequestedLayerState::Changes::Visibility |
249 RequestedLayerState::Changes::VisibleRegion;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000250 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000251 if (clientChanges & layer_state_t::HIERARCHY_CHANGES)
252 changes |= RequestedLayerState::Changes::Hierarchy;
253 if (clientChanges & layer_state_t::CONTENT_CHANGES)
254 changes |= RequestedLayerState::Changes::Content;
255 if (clientChanges & layer_state_t::GEOMETRY_CHANGES)
256 changes |= RequestedLayerState::Changes::Geometry;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000257 if (clientChanges & layer_state_t::AFFECTS_CHILDREN)
258 changes |= RequestedLayerState::Changes::AffectsChildren;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000259 if (clientChanges & layer_state_t::INPUT_CHANGES)
260 changes |= RequestedLayerState::Changes::Input;
261 if (clientChanges & layer_state_t::VISIBLE_REGION_CHANGES)
262 changes |= RequestedLayerState::Changes::VisibleRegion;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000263 if (clientState.what & layer_state_t::eColorTransformChanged) {
264 static const mat4 identityMatrix = mat4();
265 hasColorTransform = colorTransform != identityMatrix;
266 }
Vishnu Naird47bcee2023-02-24 18:08:51 +0000267 if (clientState.what &
268 (layer_state_t::eLayerChanged | layer_state_t::eRelativeLayerChanged |
269 layer_state_t::eLayerStackChanged)) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000270 changes |= RequestedLayerState::Changes::Z;
271 }
272 if (clientState.what & layer_state_t::eReparent) {
273 changes |= RequestedLayerState::Changes::Parent;
Vishnu Nair1391de22023-03-05 19:56:14 -0800274 parentId = resolvedComposerState.parentId;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000275 parentSurfaceControlForChild = nullptr;
Vishnu Nair04f89692022-11-16 23:21:05 +0000276 // Once a layer has be reparented, it cannot be placed at the root. It sounds odd
277 // but thats the existing logic and until we make this behavior more explicit, we need
278 // to maintain this logic.
279 canBeRoot = false;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000280 }
281 if (clientState.what & layer_state_t::eRelativeLayerChanged) {
282 changes |= RequestedLayerState::Changes::RelativeParent;
Vishnu Nair1391de22023-03-05 19:56:14 -0800283 relativeParentId = resolvedComposerState.relativeParentId;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000284 isRelativeOf = true;
285 relativeLayerSurfaceControl = nullptr;
286 }
287 if ((clientState.what & layer_state_t::eLayerChanged ||
288 (clientState.what & layer_state_t::eReparent && parentId == UNASSIGNED_LAYER_ID)) &&
289 isRelativeOf) {
290 // clear out relz data
291 relativeParentId = UNASSIGNED_LAYER_ID;
292 isRelativeOf = false;
293 changes |= RequestedLayerState::Changes::RelativeParent;
294 }
295 if (clientState.what & layer_state_t::eReparent && parentId == relativeParentId) {
296 // provide a hint that we are are now a direct child and not a relative child.
297 changes |= RequestedLayerState::Changes::RelativeParent;
298 }
299 if (clientState.what & layer_state_t::eInputInfoChanged) {
Vishnu Nair1391de22023-03-05 19:56:14 -0800300 touchCropId = resolvedComposerState.touchCropId;
301 windowInfoHandle->editInfo()->touchableRegionCropHandle.clear();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000302 }
303 if (clientState.what & layer_state_t::eStretchChanged) {
304 stretchEffect.sanitize();
305 }
306
307 if (clientState.what & layer_state_t::eHasListenerCallbacksChanged) {
308 // TODO(b/238781169) handle callbacks
309 }
310
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000311 if (clientState.what & layer_state_t::ePositionChanged) {
312 requestedTransform.set(x, y);
313 }
314
315 if (clientState.what & layer_state_t::eMatrixChanged) {
316 requestedTransform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
317 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000318 if (clientState.what & layer_state_t::eMetadataChanged) {
319 const int32_t requestedGameMode =
320 clientState.metadata.getInt32(gui::METADATA_GAME_MODE, -1);
321 if (requestedGameMode != -1) {
322 // The transaction will be received on the Task layer and needs to be applied to all
323 // child layers.
324 if (static_cast<int32_t>(gameMode) != requestedGameMode) {
325 gameMode = static_cast<gui::GameMode>(requestedGameMode);
Vishnu Naira02943f2023-06-03 13:44:46 -0700326 changes |= RequestedLayerState::Changes::GameMode;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000327 }
328 }
329 }
330 if (clientState.what & layer_state_t::eFrameRateChanged) {
331 const auto compatibility =
332 Layer::FrameRate::convertCompatibility(clientState.frameRateCompatibility);
333 const auto strategy = Layer::FrameRate::convertChangeFrameRateStrategy(
334 clientState.changeFrameRateStrategy);
Rachel Leece6e0042023-06-27 11:22:54 -0700335 requestedFrameRate.vote =
336 Layer::FrameRate::FrameRateVote(Fps::fromValue(clientState.frameRate),
337 compatibility, strategy);
338 changes |= RequestedLayerState::Changes::FrameRate;
339 }
340 if (clientState.what & layer_state_t::eFrameRateCategoryChanged) {
341 const auto category = Layer::FrameRate::convertCategory(clientState.frameRateCategory);
342 requestedFrameRate.category = category;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000343 changes |= RequestedLayerState::Changes::FrameRate;
344 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000345}
346
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000347ui::Size RequestedLayerState::getUnrotatedBufferSize(uint32_t displayRotationFlags) const {
348 uint32_t bufferWidth = externalTexture->getWidth();
349 uint32_t bufferHeight = externalTexture->getHeight();
350 // Undo any transformations on the buffer.
351 if (bufferTransform & ui::Transform::ROT_90) {
352 std::swap(bufferWidth, bufferHeight);
353 }
354 if (transformToDisplayInverse) {
355 if (displayRotationFlags & ui::Transform::ROT_90) {
356 std::swap(bufferWidth, bufferHeight);
357 }
358 }
359 return {bufferWidth, bufferHeight};
360}
361
362ui::Transform RequestedLayerState::getTransform(uint32_t displayRotationFlags) const {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000363 if ((flags & layer_state_t::eIgnoreDestinationFrame) || destinationFrame.isEmpty()) {
364 // If destination frame is not set, use the requested transform set via
365 // Transaction::setPosition and Transaction::setMatrix.
366 return requestedTransform;
367 }
368
369 Rect destRect = destinationFrame;
370 int32_t destW = destRect.width();
371 int32_t destH = destRect.height();
372 if (destRect.left < 0) {
373 destRect.left = 0;
374 destRect.right = destW;
375 }
376 if (destRect.top < 0) {
377 destRect.top = 0;
378 destRect.bottom = destH;
379 }
380
381 if (!externalTexture) {
382 ui::Transform transform;
383 transform.set(static_cast<float>(destRect.left), static_cast<float>(destRect.top));
384 return transform;
385 }
386
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000387 ui::Size bufferSize = getUnrotatedBufferSize(displayRotationFlags);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000388
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000389 float sx = static_cast<float>(destW) / static_cast<float>(bufferSize.width);
390 float sy = static_cast<float>(destH) / static_cast<float>(bufferSize.height);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000391 ui::Transform transform;
392 transform.set(sx, 0, 0, sy);
393 transform.set(static_cast<float>(destRect.left), static_cast<float>(destRect.top));
394 return transform;
395}
396
397std::string RequestedLayerState::getDebugString() const {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000398 std::stringstream debug;
Vishnu Nair444f3952023-04-11 13:01:02 -0700399 debug << "RequestedLayerState{" << name;
400 if (parentId != UNASSIGNED_LAYER_ID) debug << " parentId=" << parentId;
401 if (relativeParentId != UNASSIGNED_LAYER_ID) debug << " relativeParentId=" << relativeParentId;
402 if (!mirrorIds.empty()) debug << " mirrorId=" << layerIdsToString(mirrorIds);
403 if (!handleAlive) debug << " !handle";
404 if (z != 0) debug << " z=" << z;
405 if (layerStack.id != 0) debug << " layerStack=" << layerStack.id;
Patrick Williams0db92f12023-10-18 12:02:06 -0500406 debug << "}";
Vishnu Nair80a5a702023-02-11 01:21:51 +0000407 return debug.str();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000408}
409
Ady Abraham4cb525b2023-09-22 17:34:45 -0700410std::ostream& operator<<(std::ostream& out, const scheduler::LayerInfo::FrameRate& obj) {
411 out << obj.vote.rate;
412 out << " " << ftl::enum_string_full(obj.vote.type);
413 out << " " << ftl::enum_string_full(obj.category);
414 return out;
415}
416
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000417std::ostream& operator<<(std::ostream& out, const RequestedLayerState& obj) {
418 out << obj.debugName;
419 if (obj.relativeParentId != UNASSIGNED_LAYER_ID) out << " parent=" << obj.parentId;
420 if (!obj.handleAlive) out << " handleNotAlive";
Ady Abraham4cb525b2023-09-22 17:34:45 -0700421 if (obj.requestedFrameRate.isValid())
422 out << " requestedFrameRate: {" << obj.requestedFrameRate << "}";
Vishnu Nairf13c8982023-12-02 11:26:09 -0800423 if (obj.dropInputMode != gui::DropInputMode::NONE)
424 out << " dropInputMode=" << static_cast<uint32_t>(obj.dropInputMode);
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000425 return out;
426}
427
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000428std::string RequestedLayerState::getDebugStringShort() const {
429 return "[" + std::to_string(id) + "]" + name;
430}
431
432bool RequestedLayerState::canBeDestroyed() const {
433 return !handleAlive && parentId == UNASSIGNED_LAYER_ID;
434}
435bool RequestedLayerState::isRoot() const {
436 return canBeRoot && parentId == UNASSIGNED_LAYER_ID;
437}
438bool RequestedLayerState::isHiddenByPolicy() const {
439 return (flags & layer_state_t::eLayerHidden) == layer_state_t::eLayerHidden;
440};
441half4 RequestedLayerState::getColor() const {
Vishnu Naira02943f2023-06-03 13:44:46 -0700442 if (sidebandStream || externalTexture) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000443 return {0._hf, 0._hf, 0._hf, color.a};
444 }
445 return color;
446}
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000447Rect RequestedLayerState::getBufferSize(uint32_t displayRotationFlags) const {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000448 // for buffer state layers we use the display frame size as the buffer size.
449 if (!externalTexture) {
450 return Rect::INVALID_RECT;
451 }
452
453 uint32_t bufWidth = externalTexture->getWidth();
454 uint32_t bufHeight = externalTexture->getHeight();
455
456 // Undo any transformations on the buffer and return the result.
457 if (bufferTransform & ui::Transform::ROT_90) {
458 std::swap(bufWidth, bufHeight);
459 }
460
461 if (transformToDisplayInverse) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000462 uint32_t invTransform = displayRotationFlags;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000463 if (invTransform & ui::Transform::ROT_90) {
464 std::swap(bufWidth, bufHeight);
465 }
466 }
467
468 return Rect(0, 0, static_cast<int32_t>(bufWidth), static_cast<int32_t>(bufHeight));
469}
470
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000471Rect RequestedLayerState::getCroppedBufferSize(const Rect& bufferSize) const {
472 Rect size = bufferSize;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000473 if (!crop.isEmpty() && size.isValid()) {
474 size.intersect(crop, &size);
475 } else if (!crop.isEmpty()) {
476 size = crop;
477 }
478 return size;
479}
480
481Rect RequestedLayerState::getBufferCrop() const {
482 // this is the crop rectangle that applies to the buffer
483 // itself (as opposed to the window)
Chavi Weingarten07597342023-09-14 21:10:59 +0000484 if (!bufferCrop.isEmpty() && externalTexture != nullptr) {
485 // if the buffer crop is defined and there's a valid buffer, intersect buffer size and crop
486 // since the crop should never exceed the size of the buffer.
487 Rect sizeAndCrop;
488 externalTexture->getBounds().intersect(bufferCrop, &sizeAndCrop);
489 return sizeAndCrop;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000490 } else if (externalTexture != nullptr) {
491 // otherwise we use the whole buffer
492 return externalTexture->getBounds();
Chavi Weingarten07597342023-09-14 21:10:59 +0000493 } else if (!bufferCrop.isEmpty()) {
494 // if the buffer crop is defined, we use that
495 return bufferCrop;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000496 } else {
497 // if we don't have a buffer yet, we use an empty/invalid crop
498 return Rect();
499 }
500}
501
502aidl::android::hardware::graphics::composer3::Composition RequestedLayerState::getCompositionType()
503 const {
504 using aidl::android::hardware::graphics::composer3::Composition;
505 // TODO(b/238781169) check about sidestream ready flag
506 if (sidebandStream.get()) {
507 return Composition::SIDEBAND;
508 }
509 if (!externalTexture) {
510 return Composition::SOLID_COLOR;
511 }
512 if (flags & layer_state_t::eLayerIsDisplayDecoration) {
513 return Composition::DISPLAY_DECORATION;
514 }
Vishnu Nairbd51f952023-08-31 22:50:14 -0700515 if (flags & layer_state_t::eLayerIsRefreshRateIndicator) {
516 return Composition::REFRESH_RATE_INDICATOR;
517 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000518 if (potentialCursor) {
519 return Composition::CURSOR;
520 }
521 return Composition::DEVICE;
522}
523
524Rect RequestedLayerState::reduce(const Rect& win, const Region& exclude) {
525 if (CC_LIKELY(exclude.isEmpty())) {
526 return win;
527 }
528 if (exclude.isRect()) {
529 return win.reduce(exclude.getBounds());
530 }
531 return Region(win).subtract(exclude).getBounds();
532}
533
Vishnu Nair04f89692022-11-16 23:21:05 +0000534// Returns true if the layer has a relative parent that is not its own parent. This is an input
535// error from the client, and this check allows us to handle it gracefully. If both parentId and
536// relativeParentId is unassigned then the layer does not have a valid relative parent.
537// If the relative parentid is unassigned, the layer will be considered relative but won't be
538// reachable.
539bool RequestedLayerState::hasValidRelativeParent() const {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000540 return isRelativeOf &&
541 (parentId != relativeParentId || relativeParentId == UNASSIGNED_LAYER_ID);
Vishnu Nair04f89692022-11-16 23:21:05 +0000542}
543
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000544bool RequestedLayerState::hasInputInfo() const {
545 if (!windowInfoHandle) {
546 return false;
547 }
548 const auto windowInfo = windowInfoHandle->getInfo();
549 return windowInfo->token != nullptr ||
550 windowInfo->inputConfig.test(gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL);
551}
552
Vishnu Nair80a5a702023-02-11 01:21:51 +0000553bool RequestedLayerState::hasBlur() const {
554 return backgroundBlurRadius > 0 || blurRegions.size() > 0;
555}
556
Vishnu Naird47bcee2023-02-24 18:08:51 +0000557bool RequestedLayerState::hasFrameUpdate() const {
558 return what & layer_state_t::CONTENT_DIRTY &&
559 (externalTexture || bgColorLayerId != UNASSIGNED_LAYER_ID);
560}
561
562bool RequestedLayerState::hasReadyFrame() const {
563 return hasFrameUpdate() || changes.test(Changes::SidebandStream) || autoRefresh;
564}
565
566bool RequestedLayerState::hasSidebandStreamFrame() const {
567 return hasFrameUpdate() && sidebandStream.get();
568}
569
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700570bool RequestedLayerState::willReleaseBufferOnLatch() const {
571 return changes.test(Changes::Buffer) && !externalTexture;
572}
573
Vishnu Nair4d9cef92023-06-24 22:34:41 +0000574bool RequestedLayerState::backpressureEnabled() const {
575 return flags & layer_state_t::eEnableBackpressure;
576}
577
578bool RequestedLayerState::isSimpleBufferUpdate(const layer_state_t& s) const {
579 static constexpr uint64_t requiredFlags = layer_state_t::eBufferChanged;
580 if ((s.what & requiredFlags) != requiredFlags) {
581 ATRACE_FORMAT_INSTANT("%s: false [missing required flags 0x%" PRIx64 "]", __func__,
582 (s.what | requiredFlags) & ~s.what);
583 return false;
584 }
585
586 static constexpr uint64_t deniedFlags = layer_state_t::eProducerDisconnect |
587 layer_state_t::eLayerChanged | layer_state_t::eRelativeLayerChanged |
588 layer_state_t::eTransparentRegionChanged | layer_state_t::eFlagsChanged |
589 layer_state_t::eBlurRegionsChanged | layer_state_t::eLayerStackChanged |
590 layer_state_t::eAutoRefreshChanged | layer_state_t::eReparent;
591 if (s.what & deniedFlags) {
592 ATRACE_FORMAT_INSTANT("%s: false [has denied flags 0x%" PRIx64 "]", __func__,
593 s.what & deniedFlags);
594 return false;
595 }
596
597 bool changedFlags = diff(s);
598 static constexpr auto deniedChanges = layer_state_t::ePositionChanged |
599 layer_state_t::eAlphaChanged | layer_state_t::eColorTransformChanged |
600 layer_state_t::eBackgroundColorChanged | layer_state_t::eMatrixChanged |
601 layer_state_t::eCornerRadiusChanged | layer_state_t::eBackgroundBlurRadiusChanged |
602 layer_state_t::eBufferTransformChanged |
603 layer_state_t::eTransformToDisplayInverseChanged | layer_state_t::eCropChanged |
604 layer_state_t::eDataspaceChanged | layer_state_t::eHdrMetadataChanged |
605 layer_state_t::eSidebandStreamChanged | layer_state_t::eColorSpaceAgnosticChanged |
606 layer_state_t::eShadowRadiusChanged | layer_state_t::eFixedTransformHintChanged |
607 layer_state_t::eTrustedOverlayChanged | layer_state_t::eStretchChanged |
608 layer_state_t::eBufferCropChanged | layer_state_t::eDestinationFrameChanged |
609 layer_state_t::eDimmingEnabledChanged | layer_state_t::eExtendedRangeBrightnessChanged;
610 if (changedFlags & deniedChanges) {
611 ATRACE_FORMAT_INSTANT("%s: false [has denied changes flags 0x%" PRIx64 "]", __func__,
612 s.what & deniedChanges);
613 return false;
614 }
615
616 return true;
617}
618
Vishnu Nair0808ae62023-08-07 21:42:42 -0700619bool RequestedLayerState::isProtected() const {
620 return externalTexture && externalTexture->getUsage() & GRALLOC_USAGE_PROTECTED;
621}
622
Vishnu Nair854ce1c2023-08-19 15:00:13 -0700623bool RequestedLayerState::hasSomethingToDraw() const {
624 return externalTexture != nullptr || sidebandStream != nullptr || shadowRadius > 0.f ||
625 backgroundBlurRadius > 0 || blurRegions.size() > 0 ||
626 (color.r >= 0.0_hf && color.g >= 0.0_hf && color.b >= 0.0_hf);
627}
628
Vishnu Naird47bcee2023-02-24 18:08:51 +0000629void RequestedLayerState::clearChanges() {
630 what = 0;
631 changes.clear();
632}
633
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000634} // namespace android::surfaceflinger::frontend