blob: 065b8956c512ef2059ae8ae113531e53c255feee [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
27#include "Layer.h"
Vishnu Naircfb2d252023-01-19 04:44:02 +000028#include "LayerCreationArgs.h"
Vishnu Naircfb2d252023-01-19 04:44:02 +000029#include "LayerLog.h"
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000030#include "RequestedLayerState.h"
31
32namespace android::surfaceflinger::frontend {
33using ftl::Flags;
34using namespace ftl::flag_operators;
35
36namespace {
Vishnu Naira9c43762023-01-27 19:10:25 +000037std::string layerIdsToString(const std::vector<uint32_t>& layerIds) {
38 std::stringstream stream;
39 stream << "{";
40 for (auto layerId : layerIds) {
41 stream << layerId << ",";
42 }
43 stream << "}";
44 return stream.str();
45}
46
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000047} // namespace
48
49RequestedLayerState::RequestedLayerState(const LayerCreationArgs& args)
50 : id(args.sequence),
Vishnu Naircfb2d252023-01-19 04:44:02 +000051 name(args.name + "#" + std::to_string(args.sequence)),
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000052 canBeRoot(args.addToRoot),
53 layerCreationFlags(args.flags),
54 textureName(args.textureName),
55 ownerUid(args.ownerUid),
Vishnu Nair1391de22023-03-05 19:56:14 -080056 ownerPid(args.ownerPid),
57 parentId(args.parentId),
58 layerIdToMirror(args.layerIdToMirror) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000059 layerId = static_cast<int32_t>(args.sequence);
60 changes |= RequestedLayerState::Changes::Created;
61 metadata.merge(args.metadata);
62 changes |= RequestedLayerState::Changes::Metadata;
63 handleAlive = true;
Vishnu Nair1391de22023-03-05 19:56:14 -080064 if (parentId != UNASSIGNED_LAYER_ID) {
Vishnu Naircfb2d252023-01-19 04:44:02 +000065 canBeRoot = false;
66 }
Vishnu Naira9c43762023-01-27 19:10:25 +000067 if (layerIdToMirror != UNASSIGNED_LAYER_ID) {
68 changes |= RequestedLayerState::Changes::Mirror;
69 } else if (args.layerStackToMirror != ui::INVALID_LAYER_STACK) {
70 layerStackToMirror = args.layerStackToMirror;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000071 changes |= RequestedLayerState::Changes::Mirror;
72 }
73
74 flags = 0;
75 if (args.flags & ISurfaceComposerClient::eHidden) flags |= layer_state_t::eLayerHidden;
76 if (args.flags & ISurfaceComposerClient::eOpaque) flags |= layer_state_t::eLayerOpaque;
77 if (args.flags & ISurfaceComposerClient::eSecure) flags |= layer_state_t::eLayerSecure;
78 if (args.flags & ISurfaceComposerClient::eSkipScreenshot) {
79 flags |= layer_state_t::eLayerSkipScreenshot;
80 }
81 premultipliedAlpha = !(args.flags & ISurfaceComposerClient::eNonPremultiplied);
82 potentialCursor = args.flags & ISurfaceComposerClient::eCursorWindow;
83 protectedByApp = args.flags & ISurfaceComposerClient::eProtectedByApp;
84 if (args.flags & ISurfaceComposerClient::eNoColorFill) {
85 // Set an invalid color so there is no color fill.
86 // (b/259981098) use an explicit flag instead of relying on invalid values.
87 color.r = -1.0_hf;
88 color.g = -1.0_hf;
89 color.b = -1.0_hf;
90 } else {
91 color.rgb = {0.0_hf, 0.0_hf, 0.0_hf};
92 }
Vishnu Naircfb2d252023-01-19 04:44:02 +000093 LLOGV(layerId, "Created %s flags=%d", getDebugString().c_str(), flags);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000094 color.a = 1.0f;
95
96 crop.makeInvalid();
97 z = 0;
98 layerStack = ui::DEFAULT_LAYER_STACK;
99 transformToDisplayInverse = false;
100 dataspace = ui::Dataspace::UNKNOWN;
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;
124 defaultFrameRateCompatibility =
125 static_cast<int8_t>(scheduler::LayerInfo::FrameRateCompatibility::Default);
126 dataspace = ui::Dataspace::V0_SRGB;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000127 gameMode = gui::GameMode::Unsupported;
128 requestedFrameRate = {};
Alec Mourif4af03e2023-02-11 00:25:24 +0000129 cachingHint = gui::CachingHint::Enabled;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000130}
131
132void RequestedLayerState::merge(const ResolvedComposerState& resolvedComposerState) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000133 const uint32_t oldFlags = flags;
134 const half oldAlpha = color.a;
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700135 const bool hadBuffer = externalTexture != nullptr;
Vishnu Naird1f74982023-06-15 20:16:51 -0700136 uint64_t oldFramenumber = hadBuffer ? bufferData->frameNumber : 0;
Vishnu Naira02943f2023-06-03 13:44:46 -0700137 const ui::Size oldBufferSize = hadBuffer
138 ? ui::Size(externalTexture->getWidth(), externalTexture->getHeight())
139 : ui::Size();
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700140 const bool hadSideStream = sidebandStream != nullptr;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000141 const layer_state_t& clientState = resolvedComposerState.state;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000142 const bool hadBlur = hasBlur();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000143 uint64_t clientChanges = what | layer_state_t::diff(clientState);
144 layer_state_t::merge(clientState);
145 what = clientChanges;
Vishnu Naira02943f2023-06-03 13:44:46 -0700146 LLOGV(layerId, "requested=%" PRIu64 "flags=%" PRIu64, clientState.what, clientChanges);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000147
148 if (clientState.what & layer_state_t::eFlagsChanged) {
149 if ((oldFlags ^ flags) & layer_state_t::eLayerHidden) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000150 changes |= RequestedLayerState::Changes::Visibility |
151 RequestedLayerState::Changes::VisibleRegion;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000152 }
153 if ((oldFlags ^ flags) & layer_state_t::eIgnoreDestinationFrame) {
154 changes |= RequestedLayerState::Changes::Geometry;
155 }
156 }
Vishnu Naird1f74982023-06-15 20:16:51 -0700157
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700158 if (clientState.what & layer_state_t::eBufferChanged) {
159 externalTexture = resolvedComposerState.externalTexture;
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700160 const bool hasBuffer = externalTexture != nullptr;
161 if (hasBuffer || hasBuffer != hadBuffer) {
162 changes |= RequestedLayerState::Changes::Buffer;
Vishnu Naira02943f2023-06-03 13:44:46 -0700163 const ui::Size newBufferSize = hasBuffer
164 ? ui::Size(externalTexture->getWidth(), externalTexture->getHeight())
165 : ui::Size();
166 if (oldBufferSize != newBufferSize) {
167 changes |= RequestedLayerState::Changes::BufferSize;
168 changes |= RequestedLayerState::Changes::Geometry;
169 }
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700170 }
171
172 if (hasBuffer != hadBuffer) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000173 changes |= RequestedLayerState::Changes::Geometry |
174 RequestedLayerState::Changes::VisibleRegion |
Vishnu Naird47bcee2023-02-24 18:08:51 +0000175 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Input;
176 }
Vishnu Naird1f74982023-06-15 20:16:51 -0700177
178 if (hasBuffer) {
179 const bool frameNumberChanged =
180 bufferData->flags.test(BufferData::BufferDataChange::frameNumberChanged);
181 const uint64_t frameNumber =
182 frameNumberChanged ? bufferData->frameNumber : oldFramenumber + 1;
183 bufferData->frameNumber = frameNumber;
184
185 if ((barrierProducerId > bufferData->producerId) ||
186 ((barrierProducerId == bufferData->producerId) &&
187 (barrierFrameNumber > bufferData->frameNumber))) {
188 ALOGE("Out of order buffers detected for %s producedId=%d frameNumber=%" PRIu64
189 " -> producedId=%d frameNumber=%" PRIu64,
190 getDebugString().c_str(), bufferData->producerId, bufferData->frameNumber,
191 bufferData->producerId, frameNumber);
192 TransactionTraceWriter::getInstance().invoke("out_of_order_buffers_",
193 /*overwrite=*/false);
194 }
195
196 barrierProducerId = std::max(bufferData->producerId, barrierProducerId);
197 barrierFrameNumber = std::max(bufferData->frameNumber, barrierFrameNumber);
198 }
Vishnu Nair63221212023-04-06 15:17:37 -0700199 }
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700200
Vishnu Nair63221212023-04-06 15:17:37 -0700201 if (clientState.what & layer_state_t::eSidebandStreamChanged) {
202 changes |= RequestedLayerState::Changes::SidebandStream;
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700203 const bool hasSideStream = sidebandStream != nullptr;
204 if (hasSideStream != hadSideStream) {
205 changes |= RequestedLayerState::Changes::Geometry |
206 RequestedLayerState::Changes::VisibleRegion |
207 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Input;
208 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000209 }
210 if (what & (layer_state_t::eAlphaChanged)) {
211 if (oldAlpha == 0 || color.a == 0) {
212 changes |= RequestedLayerState::Changes::Visibility |
213 RequestedLayerState::Changes::VisibleRegion;
214 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000215 }
Vishnu Nair80a5a702023-02-11 01:21:51 +0000216 if (what & (layer_state_t::eBackgroundBlurRadiusChanged | layer_state_t::eBlurRegionsChanged)) {
217 if (hadBlur != hasBlur()) {
218 changes |= RequestedLayerState::Changes::Visibility |
219 RequestedLayerState::Changes::VisibleRegion;
220 }
221 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000222 if (clientChanges & layer_state_t::HIERARCHY_CHANGES)
223 changes |= RequestedLayerState::Changes::Hierarchy;
224 if (clientChanges & layer_state_t::CONTENT_CHANGES)
225 changes |= RequestedLayerState::Changes::Content;
226 if (clientChanges & layer_state_t::GEOMETRY_CHANGES)
227 changes |= RequestedLayerState::Changes::Geometry;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000228 if (clientChanges & layer_state_t::AFFECTS_CHILDREN)
229 changes |= RequestedLayerState::Changes::AffectsChildren;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000230 if (clientChanges & layer_state_t::INPUT_CHANGES)
231 changes |= RequestedLayerState::Changes::Input;
232 if (clientChanges & layer_state_t::VISIBLE_REGION_CHANGES)
233 changes |= RequestedLayerState::Changes::VisibleRegion;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000234 if (clientState.what & layer_state_t::eColorTransformChanged) {
235 static const mat4 identityMatrix = mat4();
236 hasColorTransform = colorTransform != identityMatrix;
237 }
Vishnu Naird47bcee2023-02-24 18:08:51 +0000238 if (clientState.what &
239 (layer_state_t::eLayerChanged | layer_state_t::eRelativeLayerChanged |
240 layer_state_t::eLayerStackChanged)) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000241 changes |= RequestedLayerState::Changes::Z;
242 }
243 if (clientState.what & layer_state_t::eReparent) {
244 changes |= RequestedLayerState::Changes::Parent;
Vishnu Nair1391de22023-03-05 19:56:14 -0800245 parentId = resolvedComposerState.parentId;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000246 parentSurfaceControlForChild = nullptr;
Vishnu Nair04f89692022-11-16 23:21:05 +0000247 // Once a layer has be reparented, it cannot be placed at the root. It sounds odd
248 // but thats the existing logic and until we make this behavior more explicit, we need
249 // to maintain this logic.
250 canBeRoot = false;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000251 }
252 if (clientState.what & layer_state_t::eRelativeLayerChanged) {
253 changes |= RequestedLayerState::Changes::RelativeParent;
Vishnu Nair1391de22023-03-05 19:56:14 -0800254 relativeParentId = resolvedComposerState.relativeParentId;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000255 isRelativeOf = true;
256 relativeLayerSurfaceControl = nullptr;
257 }
258 if ((clientState.what & layer_state_t::eLayerChanged ||
259 (clientState.what & layer_state_t::eReparent && parentId == UNASSIGNED_LAYER_ID)) &&
260 isRelativeOf) {
261 // clear out relz data
262 relativeParentId = UNASSIGNED_LAYER_ID;
263 isRelativeOf = false;
264 changes |= RequestedLayerState::Changes::RelativeParent;
265 }
266 if (clientState.what & layer_state_t::eReparent && parentId == relativeParentId) {
267 // provide a hint that we are are now a direct child and not a relative child.
268 changes |= RequestedLayerState::Changes::RelativeParent;
269 }
270 if (clientState.what & layer_state_t::eInputInfoChanged) {
Vishnu Nair1391de22023-03-05 19:56:14 -0800271 touchCropId = resolvedComposerState.touchCropId;
272 windowInfoHandle->editInfo()->touchableRegionCropHandle.clear();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000273 }
274 if (clientState.what & layer_state_t::eStretchChanged) {
275 stretchEffect.sanitize();
276 }
277
278 if (clientState.what & layer_state_t::eHasListenerCallbacksChanged) {
279 // TODO(b/238781169) handle callbacks
280 }
281
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000282 if (clientState.what & layer_state_t::ePositionChanged) {
283 requestedTransform.set(x, y);
284 }
285
286 if (clientState.what & layer_state_t::eMatrixChanged) {
287 requestedTransform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
288 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000289 if (clientState.what & layer_state_t::eMetadataChanged) {
290 const int32_t requestedGameMode =
291 clientState.metadata.getInt32(gui::METADATA_GAME_MODE, -1);
292 if (requestedGameMode != -1) {
293 // The transaction will be received on the Task layer and needs to be applied to all
294 // child layers.
295 if (static_cast<int32_t>(gameMode) != requestedGameMode) {
296 gameMode = static_cast<gui::GameMode>(requestedGameMode);
Vishnu Naira02943f2023-06-03 13:44:46 -0700297 changes |= RequestedLayerState::Changes::GameMode;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000298 }
299 }
300 }
301 if (clientState.what & layer_state_t::eFrameRateChanged) {
302 const auto compatibility =
303 Layer::FrameRate::convertCompatibility(clientState.frameRateCompatibility);
304 const auto strategy = Layer::FrameRate::convertChangeFrameRateStrategy(
305 clientState.changeFrameRateStrategy);
306 requestedFrameRate =
307 Layer::FrameRate(Fps::fromValue(clientState.frameRate), compatibility, strategy);
308 changes |= RequestedLayerState::Changes::FrameRate;
309 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000310}
311
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000312ui::Size RequestedLayerState::getUnrotatedBufferSize(uint32_t displayRotationFlags) const {
313 uint32_t bufferWidth = externalTexture->getWidth();
314 uint32_t bufferHeight = externalTexture->getHeight();
315 // Undo any transformations on the buffer.
316 if (bufferTransform & ui::Transform::ROT_90) {
317 std::swap(bufferWidth, bufferHeight);
318 }
319 if (transformToDisplayInverse) {
320 if (displayRotationFlags & ui::Transform::ROT_90) {
321 std::swap(bufferWidth, bufferHeight);
322 }
323 }
324 return {bufferWidth, bufferHeight};
325}
326
327ui::Transform RequestedLayerState::getTransform(uint32_t displayRotationFlags) const {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000328 if ((flags & layer_state_t::eIgnoreDestinationFrame) || destinationFrame.isEmpty()) {
329 // If destination frame is not set, use the requested transform set via
330 // Transaction::setPosition and Transaction::setMatrix.
331 return requestedTransform;
332 }
333
334 Rect destRect = destinationFrame;
335 int32_t destW = destRect.width();
336 int32_t destH = destRect.height();
337 if (destRect.left < 0) {
338 destRect.left = 0;
339 destRect.right = destW;
340 }
341 if (destRect.top < 0) {
342 destRect.top = 0;
343 destRect.bottom = destH;
344 }
345
346 if (!externalTexture) {
347 ui::Transform transform;
348 transform.set(static_cast<float>(destRect.left), static_cast<float>(destRect.top));
349 return transform;
350 }
351
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000352 ui::Size bufferSize = getUnrotatedBufferSize(displayRotationFlags);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000353
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000354 float sx = static_cast<float>(destW) / static_cast<float>(bufferSize.width);
355 float sy = static_cast<float>(destH) / static_cast<float>(bufferSize.height);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000356 ui::Transform transform;
357 transform.set(sx, 0, 0, sy);
358 transform.set(static_cast<float>(destRect.left), static_cast<float>(destRect.top));
359 return transform;
360}
361
362std::string RequestedLayerState::getDebugString() const {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000363 std::stringstream debug;
Vishnu Nair444f3952023-04-11 13:01:02 -0700364 debug << "RequestedLayerState{" << name;
365 if (parentId != UNASSIGNED_LAYER_ID) debug << " parentId=" << parentId;
366 if (relativeParentId != UNASSIGNED_LAYER_ID) debug << " relativeParentId=" << relativeParentId;
367 if (!mirrorIds.empty()) debug << " mirrorId=" << layerIdsToString(mirrorIds);
368 if (!handleAlive) debug << " !handle";
369 if (z != 0) debug << " z=" << z;
370 if (layerStack.id != 0) debug << " layerStack=" << layerStack.id;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000371 return debug.str();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000372}
373
374std::string RequestedLayerState::getDebugStringShort() const {
375 return "[" + std::to_string(id) + "]" + name;
376}
377
378bool RequestedLayerState::canBeDestroyed() const {
379 return !handleAlive && parentId == UNASSIGNED_LAYER_ID;
380}
381bool RequestedLayerState::isRoot() const {
382 return canBeRoot && parentId == UNASSIGNED_LAYER_ID;
383}
384bool RequestedLayerState::isHiddenByPolicy() const {
385 return (flags & layer_state_t::eLayerHidden) == layer_state_t::eLayerHidden;
386};
387half4 RequestedLayerState::getColor() const {
Vishnu Naira02943f2023-06-03 13:44:46 -0700388 if (sidebandStream || externalTexture) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000389 return {0._hf, 0._hf, 0._hf, color.a};
390 }
391 return color;
392}
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000393Rect RequestedLayerState::getBufferSize(uint32_t displayRotationFlags) const {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000394 // for buffer state layers we use the display frame size as the buffer size.
395 if (!externalTexture) {
396 return Rect::INVALID_RECT;
397 }
398
399 uint32_t bufWidth = externalTexture->getWidth();
400 uint32_t bufHeight = externalTexture->getHeight();
401
402 // Undo any transformations on the buffer and return the result.
403 if (bufferTransform & ui::Transform::ROT_90) {
404 std::swap(bufWidth, bufHeight);
405 }
406
407 if (transformToDisplayInverse) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000408 uint32_t invTransform = displayRotationFlags;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000409 if (invTransform & ui::Transform::ROT_90) {
410 std::swap(bufWidth, bufHeight);
411 }
412 }
413
414 return Rect(0, 0, static_cast<int32_t>(bufWidth), static_cast<int32_t>(bufHeight));
415}
416
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000417Rect RequestedLayerState::getCroppedBufferSize(const Rect& bufferSize) const {
418 Rect size = bufferSize;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000419 if (!crop.isEmpty() && size.isValid()) {
420 size.intersect(crop, &size);
421 } else if (!crop.isEmpty()) {
422 size = crop;
423 }
424 return size;
425}
426
427Rect RequestedLayerState::getBufferCrop() const {
428 // this is the crop rectangle that applies to the buffer
429 // itself (as opposed to the window)
430 if (!bufferCrop.isEmpty()) {
431 // if the buffer crop is defined, we use that
432 return bufferCrop;
433 } else if (externalTexture != nullptr) {
434 // otherwise we use the whole buffer
435 return externalTexture->getBounds();
436 } else {
437 // if we don't have a buffer yet, we use an empty/invalid crop
438 return Rect();
439 }
440}
441
442aidl::android::hardware::graphics::composer3::Composition RequestedLayerState::getCompositionType()
443 const {
444 using aidl::android::hardware::graphics::composer3::Composition;
445 // TODO(b/238781169) check about sidestream ready flag
446 if (sidebandStream.get()) {
447 return Composition::SIDEBAND;
448 }
449 if (!externalTexture) {
450 return Composition::SOLID_COLOR;
451 }
452 if (flags & layer_state_t::eLayerIsDisplayDecoration) {
453 return Composition::DISPLAY_DECORATION;
454 }
455 if (potentialCursor) {
456 return Composition::CURSOR;
457 }
458 return Composition::DEVICE;
459}
460
461Rect RequestedLayerState::reduce(const Rect& win, const Region& exclude) {
462 if (CC_LIKELY(exclude.isEmpty())) {
463 return win;
464 }
465 if (exclude.isRect()) {
466 return win.reduce(exclude.getBounds());
467 }
468 return Region(win).subtract(exclude).getBounds();
469}
470
Vishnu Nair04f89692022-11-16 23:21:05 +0000471// Returns true if the layer has a relative parent that is not its own parent. This is an input
472// error from the client, and this check allows us to handle it gracefully. If both parentId and
473// relativeParentId is unassigned then the layer does not have a valid relative parent.
474// If the relative parentid is unassigned, the layer will be considered relative but won't be
475// reachable.
476bool RequestedLayerState::hasValidRelativeParent() const {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000477 return isRelativeOf &&
478 (parentId != relativeParentId || relativeParentId == UNASSIGNED_LAYER_ID);
Vishnu Nair04f89692022-11-16 23:21:05 +0000479}
480
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000481bool RequestedLayerState::hasInputInfo() const {
482 if (!windowInfoHandle) {
483 return false;
484 }
485 const auto windowInfo = windowInfoHandle->getInfo();
486 return windowInfo->token != nullptr ||
487 windowInfo->inputConfig.test(gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL);
488}
489
Vishnu Nair80a5a702023-02-11 01:21:51 +0000490bool RequestedLayerState::hasBlur() const {
491 return backgroundBlurRadius > 0 || blurRegions.size() > 0;
492}
493
Vishnu Naird47bcee2023-02-24 18:08:51 +0000494bool RequestedLayerState::hasFrameUpdate() const {
495 return what & layer_state_t::CONTENT_DIRTY &&
496 (externalTexture || bgColorLayerId != UNASSIGNED_LAYER_ID);
497}
498
499bool RequestedLayerState::hasReadyFrame() const {
500 return hasFrameUpdate() || changes.test(Changes::SidebandStream) || autoRefresh;
501}
502
503bool RequestedLayerState::hasSidebandStreamFrame() const {
504 return hasFrameUpdate() && sidebandStream.get();
505}
506
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700507bool RequestedLayerState::willReleaseBufferOnLatch() const {
508 return changes.test(Changes::Buffer) && !externalTexture;
509}
510
Vishnu Nair4d9cef92023-06-24 22:34:41 +0000511bool RequestedLayerState::backpressureEnabled() const {
512 return flags & layer_state_t::eEnableBackpressure;
513}
514
515bool RequestedLayerState::isSimpleBufferUpdate(const layer_state_t& s) const {
516 static constexpr uint64_t requiredFlags = layer_state_t::eBufferChanged;
517 if ((s.what & requiredFlags) != requiredFlags) {
518 ATRACE_FORMAT_INSTANT("%s: false [missing required flags 0x%" PRIx64 "]", __func__,
519 (s.what | requiredFlags) & ~s.what);
520 return false;
521 }
522
523 static constexpr uint64_t deniedFlags = layer_state_t::eProducerDisconnect |
524 layer_state_t::eLayerChanged | layer_state_t::eRelativeLayerChanged |
525 layer_state_t::eTransparentRegionChanged | layer_state_t::eFlagsChanged |
526 layer_state_t::eBlurRegionsChanged | layer_state_t::eLayerStackChanged |
527 layer_state_t::eAutoRefreshChanged | layer_state_t::eReparent;
528 if (s.what & deniedFlags) {
529 ATRACE_FORMAT_INSTANT("%s: false [has denied flags 0x%" PRIx64 "]", __func__,
530 s.what & deniedFlags);
531 return false;
532 }
533
534 bool changedFlags = diff(s);
535 static constexpr auto deniedChanges = layer_state_t::ePositionChanged |
536 layer_state_t::eAlphaChanged | layer_state_t::eColorTransformChanged |
537 layer_state_t::eBackgroundColorChanged | layer_state_t::eMatrixChanged |
538 layer_state_t::eCornerRadiusChanged | layer_state_t::eBackgroundBlurRadiusChanged |
539 layer_state_t::eBufferTransformChanged |
540 layer_state_t::eTransformToDisplayInverseChanged | layer_state_t::eCropChanged |
541 layer_state_t::eDataspaceChanged | layer_state_t::eHdrMetadataChanged |
542 layer_state_t::eSidebandStreamChanged | layer_state_t::eColorSpaceAgnosticChanged |
543 layer_state_t::eShadowRadiusChanged | layer_state_t::eFixedTransformHintChanged |
544 layer_state_t::eTrustedOverlayChanged | layer_state_t::eStretchChanged |
545 layer_state_t::eBufferCropChanged | layer_state_t::eDestinationFrameChanged |
546 layer_state_t::eDimmingEnabledChanged | layer_state_t::eExtendedRangeBrightnessChanged;
547 if (changedFlags & deniedChanges) {
548 ATRACE_FORMAT_INSTANT("%s: false [has denied changes flags 0x%" PRIx64 "]", __func__,
549 s.what & deniedChanges);
550 return false;
551 }
552
553 return true;
554}
555
Vishnu Naird47bcee2023-02-24 18:08:51 +0000556void RequestedLayerState::clearChanges() {
557 what = 0;
558 changes.clear();
559}
560
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000561} // namespace android::surfaceflinger::frontend