blob: 087d7c89e47bb394c4c276db39ce9632310cb0c9 [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;
101 dataspace = ui::Dataspace::UNKNOWN;
Sally Qi963049b2023-03-23 14:06:21 -0700102 desiredHdrSdrRatio = 1.f;
103 currentHdrSdrRatio = 1.f;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000104 dataspaceRequested = false;
105 hdrMetadata.validTypes = 0;
106 surfaceDamageRegion = Region::INVALID_REGION;
107 cornerRadius = 0.0f;
108 backgroundBlurRadius = 0;
109 api = -1;
110 hasColorTransform = false;
111 bufferTransform = 0;
112 requestedTransform.reset();
113 bufferData = std::make_shared<BufferData>();
114 bufferData->frameNumber = 0;
115 bufferData->acquireFence = sp<Fence>::make(-1);
116 acquireFenceTime = std::make_shared<FenceTime>(bufferData->acquireFence);
117 colorSpaceAgnostic = false;
118 frameRateSelectionPriority = Layer::PRIORITY_UNSET;
119 shadowRadius = 0.f;
120 fixedTransformHint = ui::Transform::ROT_INVALID;
121 destinationFrame.makeInvalid();
122 isTrustedOverlay = false;
123 dropInputMode = gui::DropInputMode::NONE;
124 dimmingEnabled = true;
Vishnu Nair3fbe3262023-09-29 17:07:00 -0700125 defaultFrameRateCompatibility = static_cast<int8_t>(scheduler::FrameRateCompatibility::Default);
Rachel Leece6e0042023-06-27 11:22:54 -0700126 frameRateCategory = static_cast<int8_t>(FrameRateCategory::Default);
Rachel Lee67afbea2023-09-28 15:35:07 -0700127 frameRateCategorySmoothSwitchOnly = false;
Rachel Lee58cc90d2023-09-05 18:50:20 -0700128 frameRateSelectionStrategy =
129 static_cast<int8_t>(scheduler::LayerInfo::FrameRateSelectionStrategy::Self);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000130 dataspace = ui::Dataspace::V0_SRGB;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000131 gameMode = gui::GameMode::Unsupported;
132 requestedFrameRate = {};
Alec Mourif4af03e2023-02-11 00:25:24 +0000133 cachingHint = gui::CachingHint::Enabled;
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000134
135 if (name.length() > 77) {
136 std::string shortened;
137 shortened.append(name, 0, 36);
138 shortened.append("[...]");
139 shortened.append(name, name.length() - 36);
140 debugName = std::move(shortened);
141 } else {
142 debugName = name;
143 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000144}
145
146void RequestedLayerState::merge(const ResolvedComposerState& resolvedComposerState) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000147 const uint32_t oldFlags = flags;
148 const half oldAlpha = color.a;
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700149 const bool hadBuffer = externalTexture != nullptr;
Vishnu Naird1f74982023-06-15 20:16:51 -0700150 uint64_t oldFramenumber = hadBuffer ? bufferData->frameNumber : 0;
Vishnu Naira02943f2023-06-03 13:44:46 -0700151 const ui::Size oldBufferSize = hadBuffer
152 ? ui::Size(externalTexture->getWidth(), externalTexture->getHeight())
153 : ui::Size();
Vishnu Nair0808ae62023-08-07 21:42:42 -0700154 const uint64_t oldUsageFlags = hadBuffer ? externalTexture->getUsage() : 0;
Vishnu Nair61ff12a2023-08-30 21:41:46 -0700155 const bool oldBufferFormatOpaque = LayerSnapshot::isOpaqueFormat(
156 externalTexture ? externalTexture->getPixelFormat() : PIXEL_FORMAT_NONE);
Vishnu Nair0808ae62023-08-07 21:42:42 -0700157
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700158 const bool hadSideStream = sidebandStream != nullptr;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000159 const layer_state_t& clientState = resolvedComposerState.state;
Vishnu Nair854ce1c2023-08-19 15:00:13 -0700160 const bool hadSomethingToDraw = hasSomethingToDraw();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000161 uint64_t clientChanges = what | layer_state_t::diff(clientState);
162 layer_state_t::merge(clientState);
163 what = clientChanges;
Vishnu Naira02943f2023-06-03 13:44:46 -0700164 LLOGV(layerId, "requested=%" PRIu64 "flags=%" PRIu64, clientState.what, clientChanges);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000165
166 if (clientState.what & layer_state_t::eFlagsChanged) {
Vishnu Nair61ff12a2023-08-30 21:41:46 -0700167 if ((oldFlags ^ flags) & (layer_state_t::eLayerHidden | layer_state_t::eLayerOpaque)) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000168 changes |= RequestedLayerState::Changes::Visibility |
169 RequestedLayerState::Changes::VisibleRegion;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000170 }
171 if ((oldFlags ^ flags) & layer_state_t::eIgnoreDestinationFrame) {
172 changes |= RequestedLayerState::Changes::Geometry;
173 }
174 }
Vishnu Naird1f74982023-06-15 20:16:51 -0700175
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700176 if (clientState.what & layer_state_t::eBufferChanged) {
177 externalTexture = resolvedComposerState.externalTexture;
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700178 const bool hasBuffer = externalTexture != nullptr;
179 if (hasBuffer || hasBuffer != hadBuffer) {
180 changes |= RequestedLayerState::Changes::Buffer;
Vishnu Naira02943f2023-06-03 13:44:46 -0700181 const ui::Size newBufferSize = hasBuffer
182 ? ui::Size(externalTexture->getWidth(), externalTexture->getHeight())
183 : ui::Size();
184 if (oldBufferSize != newBufferSize) {
185 changes |= RequestedLayerState::Changes::BufferSize;
186 changes |= RequestedLayerState::Changes::Geometry;
187 }
Vishnu Nair0808ae62023-08-07 21:42:42 -0700188 const uint64_t usageFlags = hasBuffer ? externalTexture->getUsage() : 0;
189 if (oldUsageFlags != usageFlags) {
190 changes |= RequestedLayerState::Changes::BufferUsageFlags;
191 }
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700192 }
193
194 if (hasBuffer != hadBuffer) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000195 changes |= RequestedLayerState::Changes::Geometry |
196 RequestedLayerState::Changes::VisibleRegion |
Vishnu Naird47bcee2023-02-24 18:08:51 +0000197 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Input;
198 }
Vishnu Naird1f74982023-06-15 20:16:51 -0700199
200 if (hasBuffer) {
201 const bool frameNumberChanged =
202 bufferData->flags.test(BufferData::BufferDataChange::frameNumberChanged);
203 const uint64_t frameNumber =
204 frameNumberChanged ? bufferData->frameNumber : oldFramenumber + 1;
205 bufferData->frameNumber = frameNumber;
206
207 if ((barrierProducerId > bufferData->producerId) ||
208 ((barrierProducerId == bufferData->producerId) &&
209 (barrierFrameNumber > bufferData->frameNumber))) {
210 ALOGE("Out of order buffers detected for %s producedId=%d frameNumber=%" PRIu64
211 " -> producedId=%d frameNumber=%" PRIu64,
212 getDebugString().c_str(), bufferData->producerId, bufferData->frameNumber,
213 bufferData->producerId, frameNumber);
214 TransactionTraceWriter::getInstance().invoke("out_of_order_buffers_",
215 /*overwrite=*/false);
216 }
217
218 barrierProducerId = std::max(bufferData->producerId, barrierProducerId);
219 barrierFrameNumber = std::max(bufferData->frameNumber, barrierFrameNumber);
220 }
Vishnu Nair61ff12a2023-08-30 21:41:46 -0700221
222 const bool newBufferFormatOpaque = LayerSnapshot::isOpaqueFormat(
223 externalTexture ? externalTexture->getPixelFormat() : PIXEL_FORMAT_NONE);
224 if (newBufferFormatOpaque != oldBufferFormatOpaque) {
225 changes |= RequestedLayerState::Changes::Visibility |
226 RequestedLayerState::Changes::VisibleRegion;
227 }
Vishnu Nair63221212023-04-06 15:17:37 -0700228 }
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700229
Vishnu Nair63221212023-04-06 15:17:37 -0700230 if (clientState.what & layer_state_t::eSidebandStreamChanged) {
231 changes |= RequestedLayerState::Changes::SidebandStream;
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700232 const bool hasSideStream = sidebandStream != nullptr;
233 if (hasSideStream != hadSideStream) {
234 changes |= RequestedLayerState::Changes::Geometry |
235 RequestedLayerState::Changes::VisibleRegion |
236 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Input;
237 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000238 }
239 if (what & (layer_state_t::eAlphaChanged)) {
240 if (oldAlpha == 0 || color.a == 0) {
241 changes |= RequestedLayerState::Changes::Visibility |
242 RequestedLayerState::Changes::VisibleRegion;
243 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000244 }
Vishnu Nair854ce1c2023-08-19 15:00:13 -0700245
246 if (hadSomethingToDraw != hasSomethingToDraw()) {
247 changes |= RequestedLayerState::Changes::Visibility |
248 RequestedLayerState::Changes::VisibleRegion;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000249 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000250 if (clientChanges & layer_state_t::HIERARCHY_CHANGES)
251 changes |= RequestedLayerState::Changes::Hierarchy;
252 if (clientChanges & layer_state_t::CONTENT_CHANGES)
253 changes |= RequestedLayerState::Changes::Content;
254 if (clientChanges & layer_state_t::GEOMETRY_CHANGES)
255 changes |= RequestedLayerState::Changes::Geometry;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000256 if (clientChanges & layer_state_t::AFFECTS_CHILDREN)
257 changes |= RequestedLayerState::Changes::AffectsChildren;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000258 if (clientChanges & layer_state_t::INPUT_CHANGES)
259 changes |= RequestedLayerState::Changes::Input;
260 if (clientChanges & layer_state_t::VISIBLE_REGION_CHANGES)
261 changes |= RequestedLayerState::Changes::VisibleRegion;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000262 if (clientState.what & layer_state_t::eColorTransformChanged) {
263 static const mat4 identityMatrix = mat4();
264 hasColorTransform = colorTransform != identityMatrix;
265 }
Vishnu Naird47bcee2023-02-24 18:08:51 +0000266 if (clientState.what &
267 (layer_state_t::eLayerChanged | layer_state_t::eRelativeLayerChanged |
268 layer_state_t::eLayerStackChanged)) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000269 changes |= RequestedLayerState::Changes::Z;
270 }
271 if (clientState.what & layer_state_t::eReparent) {
272 changes |= RequestedLayerState::Changes::Parent;
Vishnu Nair1391de22023-03-05 19:56:14 -0800273 parentId = resolvedComposerState.parentId;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000274 parentSurfaceControlForChild = nullptr;
Vishnu Nair04f89692022-11-16 23:21:05 +0000275 // Once a layer has be reparented, it cannot be placed at the root. It sounds odd
276 // but thats the existing logic and until we make this behavior more explicit, we need
277 // to maintain this logic.
278 canBeRoot = false;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000279 }
280 if (clientState.what & layer_state_t::eRelativeLayerChanged) {
281 changes |= RequestedLayerState::Changes::RelativeParent;
Vishnu Nair1391de22023-03-05 19:56:14 -0800282 relativeParentId = resolvedComposerState.relativeParentId;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000283 isRelativeOf = true;
284 relativeLayerSurfaceControl = nullptr;
285 }
286 if ((clientState.what & layer_state_t::eLayerChanged ||
287 (clientState.what & layer_state_t::eReparent && parentId == UNASSIGNED_LAYER_ID)) &&
288 isRelativeOf) {
289 // clear out relz data
290 relativeParentId = UNASSIGNED_LAYER_ID;
291 isRelativeOf = false;
292 changes |= RequestedLayerState::Changes::RelativeParent;
293 }
294 if (clientState.what & layer_state_t::eReparent && parentId == relativeParentId) {
295 // provide a hint that we are are now a direct child and not a relative child.
296 changes |= RequestedLayerState::Changes::RelativeParent;
297 }
298 if (clientState.what & layer_state_t::eInputInfoChanged) {
Vishnu Nair1391de22023-03-05 19:56:14 -0800299 touchCropId = resolvedComposerState.touchCropId;
300 windowInfoHandle->editInfo()->touchableRegionCropHandle.clear();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000301 }
302 if (clientState.what & layer_state_t::eStretchChanged) {
303 stretchEffect.sanitize();
304 }
305
306 if (clientState.what & layer_state_t::eHasListenerCallbacksChanged) {
307 // TODO(b/238781169) handle callbacks
308 }
309
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000310 if (clientState.what & layer_state_t::ePositionChanged) {
311 requestedTransform.set(x, y);
312 }
313
314 if (clientState.what & layer_state_t::eMatrixChanged) {
315 requestedTransform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
316 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000317 if (clientState.what & layer_state_t::eMetadataChanged) {
318 const int32_t requestedGameMode =
319 clientState.metadata.getInt32(gui::METADATA_GAME_MODE, -1);
320 if (requestedGameMode != -1) {
321 // The transaction will be received on the Task layer and needs to be applied to all
322 // child layers.
323 if (static_cast<int32_t>(gameMode) != requestedGameMode) {
324 gameMode = static_cast<gui::GameMode>(requestedGameMode);
Vishnu Naira02943f2023-06-03 13:44:46 -0700325 changes |= RequestedLayerState::Changes::GameMode;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000326 }
327 }
328 }
329 if (clientState.what & layer_state_t::eFrameRateChanged) {
330 const auto compatibility =
331 Layer::FrameRate::convertCompatibility(clientState.frameRateCompatibility);
332 const auto strategy = Layer::FrameRate::convertChangeFrameRateStrategy(
333 clientState.changeFrameRateStrategy);
Rachel Leece6e0042023-06-27 11:22:54 -0700334 requestedFrameRate.vote =
335 Layer::FrameRate::FrameRateVote(Fps::fromValue(clientState.frameRate),
336 compatibility, strategy);
337 changes |= RequestedLayerState::Changes::FrameRate;
338 }
339 if (clientState.what & layer_state_t::eFrameRateCategoryChanged) {
340 const auto category = Layer::FrameRate::convertCategory(clientState.frameRateCategory);
341 requestedFrameRate.category = category;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000342 changes |= RequestedLayerState::Changes::FrameRate;
343 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000344}
345
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000346ui::Size RequestedLayerState::getUnrotatedBufferSize(uint32_t displayRotationFlags) const {
347 uint32_t bufferWidth = externalTexture->getWidth();
348 uint32_t bufferHeight = externalTexture->getHeight();
349 // Undo any transformations on the buffer.
350 if (bufferTransform & ui::Transform::ROT_90) {
351 std::swap(bufferWidth, bufferHeight);
352 }
353 if (transformToDisplayInverse) {
354 if (displayRotationFlags & ui::Transform::ROT_90) {
355 std::swap(bufferWidth, bufferHeight);
356 }
357 }
358 return {bufferWidth, bufferHeight};
359}
360
361ui::Transform RequestedLayerState::getTransform(uint32_t displayRotationFlags) const {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000362 if ((flags & layer_state_t::eIgnoreDestinationFrame) || destinationFrame.isEmpty()) {
363 // If destination frame is not set, use the requested transform set via
364 // Transaction::setPosition and Transaction::setMatrix.
365 return requestedTransform;
366 }
367
368 Rect destRect = destinationFrame;
369 int32_t destW = destRect.width();
370 int32_t destH = destRect.height();
371 if (destRect.left < 0) {
372 destRect.left = 0;
373 destRect.right = destW;
374 }
375 if (destRect.top < 0) {
376 destRect.top = 0;
377 destRect.bottom = destH;
378 }
379
380 if (!externalTexture) {
381 ui::Transform transform;
382 transform.set(static_cast<float>(destRect.left), static_cast<float>(destRect.top));
383 return transform;
384 }
385
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000386 ui::Size bufferSize = getUnrotatedBufferSize(displayRotationFlags);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000387
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000388 float sx = static_cast<float>(destW) / static_cast<float>(bufferSize.width);
389 float sy = static_cast<float>(destH) / static_cast<float>(bufferSize.height);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000390 ui::Transform transform;
391 transform.set(sx, 0, 0, sy);
392 transform.set(static_cast<float>(destRect.left), static_cast<float>(destRect.top));
393 return transform;
394}
395
396std::string RequestedLayerState::getDebugString() const {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000397 std::stringstream debug;
Vishnu Nair444f3952023-04-11 13:01:02 -0700398 debug << "RequestedLayerState{" << name;
399 if (parentId != UNASSIGNED_LAYER_ID) debug << " parentId=" << parentId;
400 if (relativeParentId != UNASSIGNED_LAYER_ID) debug << " relativeParentId=" << relativeParentId;
401 if (!mirrorIds.empty()) debug << " mirrorId=" << layerIdsToString(mirrorIds);
402 if (!handleAlive) debug << " !handle";
403 if (z != 0) debug << " z=" << z;
404 if (layerStack.id != 0) debug << " layerStack=" << layerStack.id;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000405 return debug.str();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000406}
407
Ady Abraham4cb525b2023-09-22 17:34:45 -0700408std::ostream& operator<<(std::ostream& out, const scheduler::LayerInfo::FrameRate& obj) {
409 out << obj.vote.rate;
410 out << " " << ftl::enum_string_full(obj.vote.type);
411 out << " " << ftl::enum_string_full(obj.category);
412 return out;
413}
414
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000415std::ostream& operator<<(std::ostream& out, const RequestedLayerState& obj) {
416 out << obj.debugName;
417 if (obj.relativeParentId != UNASSIGNED_LAYER_ID) out << " parent=" << obj.parentId;
418 if (!obj.handleAlive) out << " handleNotAlive";
Ady Abraham4cb525b2023-09-22 17:34:45 -0700419 if (obj.requestedFrameRate.isValid())
420 out << " requestedFrameRate: {" << obj.requestedFrameRate << "}";
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000421 return out;
422}
423
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000424std::string RequestedLayerState::getDebugStringShort() const {
425 return "[" + std::to_string(id) + "]" + name;
426}
427
428bool RequestedLayerState::canBeDestroyed() const {
429 return !handleAlive && parentId == UNASSIGNED_LAYER_ID;
430}
431bool RequestedLayerState::isRoot() const {
432 return canBeRoot && parentId == UNASSIGNED_LAYER_ID;
433}
434bool RequestedLayerState::isHiddenByPolicy() const {
435 return (flags & layer_state_t::eLayerHidden) == layer_state_t::eLayerHidden;
436};
437half4 RequestedLayerState::getColor() const {
Vishnu Naira02943f2023-06-03 13:44:46 -0700438 if (sidebandStream || externalTexture) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000439 return {0._hf, 0._hf, 0._hf, color.a};
440 }
441 return color;
442}
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000443Rect RequestedLayerState::getBufferSize(uint32_t displayRotationFlags) const {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000444 // for buffer state layers we use the display frame size as the buffer size.
445 if (!externalTexture) {
446 return Rect::INVALID_RECT;
447 }
448
449 uint32_t bufWidth = externalTexture->getWidth();
450 uint32_t bufHeight = externalTexture->getHeight();
451
452 // Undo any transformations on the buffer and return the result.
453 if (bufferTransform & ui::Transform::ROT_90) {
454 std::swap(bufWidth, bufHeight);
455 }
456
457 if (transformToDisplayInverse) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000458 uint32_t invTransform = displayRotationFlags;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000459 if (invTransform & ui::Transform::ROT_90) {
460 std::swap(bufWidth, bufHeight);
461 }
462 }
463
464 return Rect(0, 0, static_cast<int32_t>(bufWidth), static_cast<int32_t>(bufHeight));
465}
466
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000467Rect RequestedLayerState::getCroppedBufferSize(const Rect& bufferSize) const {
468 Rect size = bufferSize;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000469 if (!crop.isEmpty() && size.isValid()) {
470 size.intersect(crop, &size);
471 } else if (!crop.isEmpty()) {
472 size = crop;
473 }
474 return size;
475}
476
477Rect RequestedLayerState::getBufferCrop() const {
478 // this is the crop rectangle that applies to the buffer
479 // itself (as opposed to the window)
Chavi Weingarten07597342023-09-14 21:10:59 +0000480 if (!bufferCrop.isEmpty() && externalTexture != nullptr) {
481 // if the buffer crop is defined and there's a valid buffer, intersect buffer size and crop
482 // since the crop should never exceed the size of the buffer.
483 Rect sizeAndCrop;
484 externalTexture->getBounds().intersect(bufferCrop, &sizeAndCrop);
485 return sizeAndCrop;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000486 } else if (externalTexture != nullptr) {
487 // otherwise we use the whole buffer
488 return externalTexture->getBounds();
Chavi Weingarten07597342023-09-14 21:10:59 +0000489 } else if (!bufferCrop.isEmpty()) {
490 // if the buffer crop is defined, we use that
491 return bufferCrop;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000492 } else {
493 // if we don't have a buffer yet, we use an empty/invalid crop
494 return Rect();
495 }
496}
497
498aidl::android::hardware::graphics::composer3::Composition RequestedLayerState::getCompositionType()
499 const {
500 using aidl::android::hardware::graphics::composer3::Composition;
501 // TODO(b/238781169) check about sidestream ready flag
502 if (sidebandStream.get()) {
503 return Composition::SIDEBAND;
504 }
505 if (!externalTexture) {
506 return Composition::SOLID_COLOR;
507 }
508 if (flags & layer_state_t::eLayerIsDisplayDecoration) {
509 return Composition::DISPLAY_DECORATION;
510 }
Vishnu Nairbd51f952023-08-31 22:50:14 -0700511 if (flags & layer_state_t::eLayerIsRefreshRateIndicator) {
512 return Composition::REFRESH_RATE_INDICATOR;
513 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000514 if (potentialCursor) {
515 return Composition::CURSOR;
516 }
517 return Composition::DEVICE;
518}
519
520Rect RequestedLayerState::reduce(const Rect& win, const Region& exclude) {
521 if (CC_LIKELY(exclude.isEmpty())) {
522 return win;
523 }
524 if (exclude.isRect()) {
525 return win.reduce(exclude.getBounds());
526 }
527 return Region(win).subtract(exclude).getBounds();
528}
529
Vishnu Nair04f89692022-11-16 23:21:05 +0000530// Returns true if the layer has a relative parent that is not its own parent. This is an input
531// error from the client, and this check allows us to handle it gracefully. If both parentId and
532// relativeParentId is unassigned then the layer does not have a valid relative parent.
533// If the relative parentid is unassigned, the layer will be considered relative but won't be
534// reachable.
535bool RequestedLayerState::hasValidRelativeParent() const {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000536 return isRelativeOf &&
537 (parentId != relativeParentId || relativeParentId == UNASSIGNED_LAYER_ID);
Vishnu Nair04f89692022-11-16 23:21:05 +0000538}
539
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000540bool RequestedLayerState::hasInputInfo() const {
541 if (!windowInfoHandle) {
542 return false;
543 }
544 const auto windowInfo = windowInfoHandle->getInfo();
545 return windowInfo->token != nullptr ||
546 windowInfo->inputConfig.test(gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL);
547}
548
Vishnu Nair80a5a702023-02-11 01:21:51 +0000549bool RequestedLayerState::hasBlur() const {
550 return backgroundBlurRadius > 0 || blurRegions.size() > 0;
551}
552
Vishnu Naird47bcee2023-02-24 18:08:51 +0000553bool RequestedLayerState::hasFrameUpdate() const {
554 return what & layer_state_t::CONTENT_DIRTY &&
555 (externalTexture || bgColorLayerId != UNASSIGNED_LAYER_ID);
556}
557
558bool RequestedLayerState::hasReadyFrame() const {
559 return hasFrameUpdate() || changes.test(Changes::SidebandStream) || autoRefresh;
560}
561
562bool RequestedLayerState::hasSidebandStreamFrame() const {
563 return hasFrameUpdate() && sidebandStream.get();
564}
565
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700566bool RequestedLayerState::willReleaseBufferOnLatch() const {
567 return changes.test(Changes::Buffer) && !externalTexture;
568}
569
Vishnu Nair4d9cef92023-06-24 22:34:41 +0000570bool RequestedLayerState::backpressureEnabled() const {
571 return flags & layer_state_t::eEnableBackpressure;
572}
573
574bool RequestedLayerState::isSimpleBufferUpdate(const layer_state_t& s) const {
575 static constexpr uint64_t requiredFlags = layer_state_t::eBufferChanged;
576 if ((s.what & requiredFlags) != requiredFlags) {
577 ATRACE_FORMAT_INSTANT("%s: false [missing required flags 0x%" PRIx64 "]", __func__,
578 (s.what | requiredFlags) & ~s.what);
579 return false;
580 }
581
582 static constexpr uint64_t deniedFlags = layer_state_t::eProducerDisconnect |
583 layer_state_t::eLayerChanged | layer_state_t::eRelativeLayerChanged |
584 layer_state_t::eTransparentRegionChanged | layer_state_t::eFlagsChanged |
585 layer_state_t::eBlurRegionsChanged | layer_state_t::eLayerStackChanged |
586 layer_state_t::eAutoRefreshChanged | layer_state_t::eReparent;
587 if (s.what & deniedFlags) {
588 ATRACE_FORMAT_INSTANT("%s: false [has denied flags 0x%" PRIx64 "]", __func__,
589 s.what & deniedFlags);
590 return false;
591 }
592
593 bool changedFlags = diff(s);
594 static constexpr auto deniedChanges = layer_state_t::ePositionChanged |
595 layer_state_t::eAlphaChanged | layer_state_t::eColorTransformChanged |
596 layer_state_t::eBackgroundColorChanged | layer_state_t::eMatrixChanged |
597 layer_state_t::eCornerRadiusChanged | layer_state_t::eBackgroundBlurRadiusChanged |
598 layer_state_t::eBufferTransformChanged |
599 layer_state_t::eTransformToDisplayInverseChanged | layer_state_t::eCropChanged |
600 layer_state_t::eDataspaceChanged | layer_state_t::eHdrMetadataChanged |
601 layer_state_t::eSidebandStreamChanged | layer_state_t::eColorSpaceAgnosticChanged |
602 layer_state_t::eShadowRadiusChanged | layer_state_t::eFixedTransformHintChanged |
603 layer_state_t::eTrustedOverlayChanged | layer_state_t::eStretchChanged |
604 layer_state_t::eBufferCropChanged | layer_state_t::eDestinationFrameChanged |
605 layer_state_t::eDimmingEnabledChanged | layer_state_t::eExtendedRangeBrightnessChanged;
606 if (changedFlags & deniedChanges) {
607 ATRACE_FORMAT_INSTANT("%s: false [has denied changes flags 0x%" PRIx64 "]", __func__,
608 s.what & deniedChanges);
609 return false;
610 }
611
612 return true;
613}
614
Vishnu Nair0808ae62023-08-07 21:42:42 -0700615bool RequestedLayerState::isProtected() const {
616 return externalTexture && externalTexture->getUsage() & GRALLOC_USAGE_PROTECTED;
617}
618
Vishnu Nair854ce1c2023-08-19 15:00:13 -0700619bool RequestedLayerState::hasSomethingToDraw() const {
620 return externalTexture != nullptr || sidebandStream != nullptr || shadowRadius > 0.f ||
621 backgroundBlurRadius > 0 || blurRegions.size() > 0 ||
622 (color.r >= 0.0_hf && color.g >= 0.0_hf && color.b >= 0.0_hf);
623}
624
Vishnu Naird47bcee2023-02-24 18:08:51 +0000625void RequestedLayerState::clearChanges() {
626 what = 0;
627 changes.clear();
628}
629
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000630} // namespace android::surfaceflinger::frontend