blob: 8a39cd6330ce45bb324980f140a1a894a9a866c7 [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),
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000054 ownerUid(args.ownerUid),
Vishnu Nair1391de22023-03-05 19:56:14 -080055 ownerPid(args.ownerPid),
56 parentId(args.parentId),
57 layerIdToMirror(args.layerIdToMirror) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000058 layerId = static_cast<int32_t>(args.sequence);
59 changes |= RequestedLayerState::Changes::Created;
60 metadata.merge(args.metadata);
61 changes |= RequestedLayerState::Changes::Metadata;
62 handleAlive = true;
Vishnu Nair1391de22023-03-05 19:56:14 -080063 if (parentId != UNASSIGNED_LAYER_ID) {
Vishnu Naircfb2d252023-01-19 04:44:02 +000064 canBeRoot = false;
65 }
Vishnu Naira9c43762023-01-27 19:10:25 +000066 if (layerIdToMirror != UNASSIGNED_LAYER_ID) {
67 changes |= RequestedLayerState::Changes::Mirror;
68 } else if (args.layerStackToMirror != ui::INVALID_LAYER_STACK) {
69 layerStackToMirror = args.layerStackToMirror;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000070 changes |= RequestedLayerState::Changes::Mirror;
71 }
72
73 flags = 0;
74 if (args.flags & ISurfaceComposerClient::eHidden) flags |= layer_state_t::eLayerHidden;
75 if (args.flags & ISurfaceComposerClient::eOpaque) flags |= layer_state_t::eLayerOpaque;
76 if (args.flags & ISurfaceComposerClient::eSecure) flags |= layer_state_t::eLayerSecure;
77 if (args.flags & ISurfaceComposerClient::eSkipScreenshot) {
78 flags |= layer_state_t::eLayerSkipScreenshot;
79 }
80 premultipliedAlpha = !(args.flags & ISurfaceComposerClient::eNonPremultiplied);
81 potentialCursor = args.flags & ISurfaceComposerClient::eCursorWindow;
82 protectedByApp = args.flags & ISurfaceComposerClient::eProtectedByApp;
83 if (args.flags & ISurfaceComposerClient::eNoColorFill) {
84 // Set an invalid color so there is no color fill.
85 // (b/259981098) use an explicit flag instead of relying on invalid values.
86 color.r = -1.0_hf;
87 color.g = -1.0_hf;
88 color.b = -1.0_hf;
89 } else {
90 color.rgb = {0.0_hf, 0.0_hf, 0.0_hf};
91 }
Vishnu Naircfb2d252023-01-19 04:44:02 +000092 LLOGV(layerId, "Created %s flags=%d", getDebugString().c_str(), flags);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000093 color.a = 1.0f;
94
95 crop.makeInvalid();
96 z = 0;
97 layerStack = ui::DEFAULT_LAYER_STACK;
98 transformToDisplayInverse = false;
99 dataspace = ui::Dataspace::UNKNOWN;
Sally Qi963049b2023-03-23 14:06:21 -0700100 desiredHdrSdrRatio = 1.f;
101 currentHdrSdrRatio = 1.f;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000102 dataspaceRequested = false;
103 hdrMetadata.validTypes = 0;
104 surfaceDamageRegion = Region::INVALID_REGION;
105 cornerRadius = 0.0f;
106 backgroundBlurRadius = 0;
107 api = -1;
108 hasColorTransform = false;
109 bufferTransform = 0;
110 requestedTransform.reset();
111 bufferData = std::make_shared<BufferData>();
112 bufferData->frameNumber = 0;
113 bufferData->acquireFence = sp<Fence>::make(-1);
114 acquireFenceTime = std::make_shared<FenceTime>(bufferData->acquireFence);
115 colorSpaceAgnostic = false;
116 frameRateSelectionPriority = Layer::PRIORITY_UNSET;
117 shadowRadius = 0.f;
118 fixedTransformHint = ui::Transform::ROT_INVALID;
119 destinationFrame.makeInvalid();
120 isTrustedOverlay = false;
121 dropInputMode = gui::DropInputMode::NONE;
122 dimmingEnabled = true;
123 defaultFrameRateCompatibility =
124 static_cast<int8_t>(scheduler::LayerInfo::FrameRateCompatibility::Default);
125 dataspace = ui::Dataspace::V0_SRGB;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000126 gameMode = gui::GameMode::Unsupported;
127 requestedFrameRate = {};
Alec Mourif4af03e2023-02-11 00:25:24 +0000128 cachingHint = gui::CachingHint::Enabled;
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000129
130 if (name.length() > 77) {
131 std::string shortened;
132 shortened.append(name, 0, 36);
133 shortened.append("[...]");
134 shortened.append(name, name.length() - 36);
135 debugName = std::move(shortened);
136 } else {
137 debugName = name;
138 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000139}
140
141void RequestedLayerState::merge(const ResolvedComposerState& resolvedComposerState) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000142 const uint32_t oldFlags = flags;
143 const half oldAlpha = color.a;
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700144 const bool hadBuffer = externalTexture != nullptr;
Vishnu Naird1f74982023-06-15 20:16:51 -0700145 uint64_t oldFramenumber = hadBuffer ? bufferData->frameNumber : 0;
Vishnu Naira02943f2023-06-03 13:44:46 -0700146 const ui::Size oldBufferSize = hadBuffer
147 ? ui::Size(externalTexture->getWidth(), externalTexture->getHeight())
148 : ui::Size();
Vishnu Nair0808ae62023-08-07 21:42:42 -0700149 const uint64_t oldUsageFlags = hadBuffer ? externalTexture->getUsage() : 0;
150
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700151 const bool hadSideStream = sidebandStream != nullptr;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000152 const layer_state_t& clientState = resolvedComposerState.state;
Vishnu Nair854ce1c2023-08-19 15:00:13 -0700153 const bool hadSomethingToDraw = hasSomethingToDraw();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000154 uint64_t clientChanges = what | layer_state_t::diff(clientState);
155 layer_state_t::merge(clientState);
156 what = clientChanges;
Vishnu Naira02943f2023-06-03 13:44:46 -0700157 LLOGV(layerId, "requested=%" PRIu64 "flags=%" PRIu64, clientState.what, clientChanges);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000158
159 if (clientState.what & layer_state_t::eFlagsChanged) {
160 if ((oldFlags ^ flags) & layer_state_t::eLayerHidden) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000161 changes |= RequestedLayerState::Changes::Visibility |
162 RequestedLayerState::Changes::VisibleRegion;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000163 }
164 if ((oldFlags ^ flags) & layer_state_t::eIgnoreDestinationFrame) {
165 changes |= RequestedLayerState::Changes::Geometry;
166 }
167 }
Vishnu Naird1f74982023-06-15 20:16:51 -0700168
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700169 if (clientState.what & layer_state_t::eBufferChanged) {
170 externalTexture = resolvedComposerState.externalTexture;
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700171 const bool hasBuffer = externalTexture != nullptr;
172 if (hasBuffer || hasBuffer != hadBuffer) {
173 changes |= RequestedLayerState::Changes::Buffer;
Vishnu Naira02943f2023-06-03 13:44:46 -0700174 const ui::Size newBufferSize = hasBuffer
175 ? ui::Size(externalTexture->getWidth(), externalTexture->getHeight())
176 : ui::Size();
177 if (oldBufferSize != newBufferSize) {
178 changes |= RequestedLayerState::Changes::BufferSize;
179 changes |= RequestedLayerState::Changes::Geometry;
180 }
Vishnu Nair0808ae62023-08-07 21:42:42 -0700181 const uint64_t usageFlags = hasBuffer ? externalTexture->getUsage() : 0;
182 if (oldUsageFlags != usageFlags) {
183 changes |= RequestedLayerState::Changes::BufferUsageFlags;
184 }
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700185 }
186
187 if (hasBuffer != hadBuffer) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000188 changes |= RequestedLayerState::Changes::Geometry |
189 RequestedLayerState::Changes::VisibleRegion |
Vishnu Naird47bcee2023-02-24 18:08:51 +0000190 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Input;
191 }
Vishnu Naird1f74982023-06-15 20:16:51 -0700192
193 if (hasBuffer) {
194 const bool frameNumberChanged =
195 bufferData->flags.test(BufferData::BufferDataChange::frameNumberChanged);
196 const uint64_t frameNumber =
197 frameNumberChanged ? bufferData->frameNumber : oldFramenumber + 1;
198 bufferData->frameNumber = frameNumber;
199
200 if ((barrierProducerId > bufferData->producerId) ||
201 ((barrierProducerId == bufferData->producerId) &&
202 (barrierFrameNumber > bufferData->frameNumber))) {
203 ALOGE("Out of order buffers detected for %s producedId=%d frameNumber=%" PRIu64
204 " -> producedId=%d frameNumber=%" PRIu64,
205 getDebugString().c_str(), bufferData->producerId, bufferData->frameNumber,
206 bufferData->producerId, frameNumber);
207 TransactionTraceWriter::getInstance().invoke("out_of_order_buffers_",
208 /*overwrite=*/false);
209 }
210
211 barrierProducerId = std::max(bufferData->producerId, barrierProducerId);
212 barrierFrameNumber = std::max(bufferData->frameNumber, barrierFrameNumber);
213 }
Vishnu Nair63221212023-04-06 15:17:37 -0700214 }
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700215
Vishnu Nair63221212023-04-06 15:17:37 -0700216 if (clientState.what & layer_state_t::eSidebandStreamChanged) {
217 changes |= RequestedLayerState::Changes::SidebandStream;
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700218 const bool hasSideStream = sidebandStream != nullptr;
219 if (hasSideStream != hadSideStream) {
220 changes |= RequestedLayerState::Changes::Geometry |
221 RequestedLayerState::Changes::VisibleRegion |
222 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Input;
223 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000224 }
225 if (what & (layer_state_t::eAlphaChanged)) {
226 if (oldAlpha == 0 || color.a == 0) {
227 changes |= RequestedLayerState::Changes::Visibility |
228 RequestedLayerState::Changes::VisibleRegion;
229 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000230 }
Vishnu Nair854ce1c2023-08-19 15:00:13 -0700231
232 if (hadSomethingToDraw != hasSomethingToDraw()) {
233 changes |= RequestedLayerState::Changes::Visibility |
234 RequestedLayerState::Changes::VisibleRegion;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000235 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000236 if (clientChanges & layer_state_t::HIERARCHY_CHANGES)
237 changes |= RequestedLayerState::Changes::Hierarchy;
238 if (clientChanges & layer_state_t::CONTENT_CHANGES)
239 changes |= RequestedLayerState::Changes::Content;
240 if (clientChanges & layer_state_t::GEOMETRY_CHANGES)
241 changes |= RequestedLayerState::Changes::Geometry;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000242 if (clientChanges & layer_state_t::AFFECTS_CHILDREN)
243 changes |= RequestedLayerState::Changes::AffectsChildren;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000244 if (clientChanges & layer_state_t::INPUT_CHANGES)
245 changes |= RequestedLayerState::Changes::Input;
246 if (clientChanges & layer_state_t::VISIBLE_REGION_CHANGES)
247 changes |= RequestedLayerState::Changes::VisibleRegion;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000248 if (clientState.what & layer_state_t::eColorTransformChanged) {
249 static const mat4 identityMatrix = mat4();
250 hasColorTransform = colorTransform != identityMatrix;
251 }
Vishnu Naird47bcee2023-02-24 18:08:51 +0000252 if (clientState.what &
253 (layer_state_t::eLayerChanged | layer_state_t::eRelativeLayerChanged |
254 layer_state_t::eLayerStackChanged)) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000255 changes |= RequestedLayerState::Changes::Z;
256 }
257 if (clientState.what & layer_state_t::eReparent) {
258 changes |= RequestedLayerState::Changes::Parent;
Vishnu Nair1391de22023-03-05 19:56:14 -0800259 parentId = resolvedComposerState.parentId;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000260 parentSurfaceControlForChild = nullptr;
Vishnu Nair04f89692022-11-16 23:21:05 +0000261 // Once a layer has be reparented, it cannot be placed at the root. It sounds odd
262 // but thats the existing logic and until we make this behavior more explicit, we need
263 // to maintain this logic.
264 canBeRoot = false;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000265 }
266 if (clientState.what & layer_state_t::eRelativeLayerChanged) {
267 changes |= RequestedLayerState::Changes::RelativeParent;
Vishnu Nair1391de22023-03-05 19:56:14 -0800268 relativeParentId = resolvedComposerState.relativeParentId;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000269 isRelativeOf = true;
270 relativeLayerSurfaceControl = nullptr;
271 }
272 if ((clientState.what & layer_state_t::eLayerChanged ||
273 (clientState.what & layer_state_t::eReparent && parentId == UNASSIGNED_LAYER_ID)) &&
274 isRelativeOf) {
275 // clear out relz data
276 relativeParentId = UNASSIGNED_LAYER_ID;
277 isRelativeOf = false;
278 changes |= RequestedLayerState::Changes::RelativeParent;
279 }
280 if (clientState.what & layer_state_t::eReparent && parentId == relativeParentId) {
281 // provide a hint that we are are now a direct child and not a relative child.
282 changes |= RequestedLayerState::Changes::RelativeParent;
283 }
284 if (clientState.what & layer_state_t::eInputInfoChanged) {
Vishnu Nair1391de22023-03-05 19:56:14 -0800285 touchCropId = resolvedComposerState.touchCropId;
286 windowInfoHandle->editInfo()->touchableRegionCropHandle.clear();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000287 }
288 if (clientState.what & layer_state_t::eStretchChanged) {
289 stretchEffect.sanitize();
290 }
291
292 if (clientState.what & layer_state_t::eHasListenerCallbacksChanged) {
293 // TODO(b/238781169) handle callbacks
294 }
295
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000296 if (clientState.what & layer_state_t::ePositionChanged) {
297 requestedTransform.set(x, y);
298 }
299
300 if (clientState.what & layer_state_t::eMatrixChanged) {
301 requestedTransform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
302 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000303 if (clientState.what & layer_state_t::eMetadataChanged) {
304 const int32_t requestedGameMode =
305 clientState.metadata.getInt32(gui::METADATA_GAME_MODE, -1);
306 if (requestedGameMode != -1) {
307 // The transaction will be received on the Task layer and needs to be applied to all
308 // child layers.
309 if (static_cast<int32_t>(gameMode) != requestedGameMode) {
310 gameMode = static_cast<gui::GameMode>(requestedGameMode);
Vishnu Naira02943f2023-06-03 13:44:46 -0700311 changes |= RequestedLayerState::Changes::GameMode;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000312 }
313 }
314 }
315 if (clientState.what & layer_state_t::eFrameRateChanged) {
316 const auto compatibility =
317 Layer::FrameRate::convertCompatibility(clientState.frameRateCompatibility);
318 const auto strategy = Layer::FrameRate::convertChangeFrameRateStrategy(
319 clientState.changeFrameRateStrategy);
320 requestedFrameRate =
321 Layer::FrameRate(Fps::fromValue(clientState.frameRate), compatibility, strategy);
322 changes |= RequestedLayerState::Changes::FrameRate;
323 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000324}
325
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000326ui::Size RequestedLayerState::getUnrotatedBufferSize(uint32_t displayRotationFlags) const {
327 uint32_t bufferWidth = externalTexture->getWidth();
328 uint32_t bufferHeight = externalTexture->getHeight();
329 // Undo any transformations on the buffer.
330 if (bufferTransform & ui::Transform::ROT_90) {
331 std::swap(bufferWidth, bufferHeight);
332 }
333 if (transformToDisplayInverse) {
334 if (displayRotationFlags & ui::Transform::ROT_90) {
335 std::swap(bufferWidth, bufferHeight);
336 }
337 }
338 return {bufferWidth, bufferHeight};
339}
340
341ui::Transform RequestedLayerState::getTransform(uint32_t displayRotationFlags) const {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000342 if ((flags & layer_state_t::eIgnoreDestinationFrame) || destinationFrame.isEmpty()) {
343 // If destination frame is not set, use the requested transform set via
344 // Transaction::setPosition and Transaction::setMatrix.
345 return requestedTransform;
346 }
347
348 Rect destRect = destinationFrame;
349 int32_t destW = destRect.width();
350 int32_t destH = destRect.height();
351 if (destRect.left < 0) {
352 destRect.left = 0;
353 destRect.right = destW;
354 }
355 if (destRect.top < 0) {
356 destRect.top = 0;
357 destRect.bottom = destH;
358 }
359
360 if (!externalTexture) {
361 ui::Transform transform;
362 transform.set(static_cast<float>(destRect.left), static_cast<float>(destRect.top));
363 return transform;
364 }
365
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000366 ui::Size bufferSize = getUnrotatedBufferSize(displayRotationFlags);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000367
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000368 float sx = static_cast<float>(destW) / static_cast<float>(bufferSize.width);
369 float sy = static_cast<float>(destH) / static_cast<float>(bufferSize.height);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000370 ui::Transform transform;
371 transform.set(sx, 0, 0, sy);
372 transform.set(static_cast<float>(destRect.left), static_cast<float>(destRect.top));
373 return transform;
374}
375
376std::string RequestedLayerState::getDebugString() const {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000377 std::stringstream debug;
Vishnu Nair444f3952023-04-11 13:01:02 -0700378 debug << "RequestedLayerState{" << name;
379 if (parentId != UNASSIGNED_LAYER_ID) debug << " parentId=" << parentId;
380 if (relativeParentId != UNASSIGNED_LAYER_ID) debug << " relativeParentId=" << relativeParentId;
381 if (!mirrorIds.empty()) debug << " mirrorId=" << layerIdsToString(mirrorIds);
382 if (!handleAlive) debug << " !handle";
383 if (z != 0) debug << " z=" << z;
384 if (layerStack.id != 0) debug << " layerStack=" << layerStack.id;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000385 return debug.str();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000386}
387
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000388std::ostream& operator<<(std::ostream& out, const RequestedLayerState& obj) {
389 out << obj.debugName;
390 if (obj.relativeParentId != UNASSIGNED_LAYER_ID) out << " parent=" << obj.parentId;
391 if (!obj.handleAlive) out << " handleNotAlive";
392 return out;
393}
394
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000395std::string RequestedLayerState::getDebugStringShort() const {
396 return "[" + std::to_string(id) + "]" + name;
397}
398
399bool RequestedLayerState::canBeDestroyed() const {
400 return !handleAlive && parentId == UNASSIGNED_LAYER_ID;
401}
402bool RequestedLayerState::isRoot() const {
403 return canBeRoot && parentId == UNASSIGNED_LAYER_ID;
404}
405bool RequestedLayerState::isHiddenByPolicy() const {
406 return (flags & layer_state_t::eLayerHidden) == layer_state_t::eLayerHidden;
407};
408half4 RequestedLayerState::getColor() const {
Vishnu Naira02943f2023-06-03 13:44:46 -0700409 if (sidebandStream || externalTexture) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000410 return {0._hf, 0._hf, 0._hf, color.a};
411 }
412 return color;
413}
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000414Rect RequestedLayerState::getBufferSize(uint32_t displayRotationFlags) const {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000415 // for buffer state layers we use the display frame size as the buffer size.
416 if (!externalTexture) {
417 return Rect::INVALID_RECT;
418 }
419
420 uint32_t bufWidth = externalTexture->getWidth();
421 uint32_t bufHeight = externalTexture->getHeight();
422
423 // Undo any transformations on the buffer and return the result.
424 if (bufferTransform & ui::Transform::ROT_90) {
425 std::swap(bufWidth, bufHeight);
426 }
427
428 if (transformToDisplayInverse) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000429 uint32_t invTransform = displayRotationFlags;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000430 if (invTransform & ui::Transform::ROT_90) {
431 std::swap(bufWidth, bufHeight);
432 }
433 }
434
435 return Rect(0, 0, static_cast<int32_t>(bufWidth), static_cast<int32_t>(bufHeight));
436}
437
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000438Rect RequestedLayerState::getCroppedBufferSize(const Rect& bufferSize) const {
439 Rect size = bufferSize;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000440 if (!crop.isEmpty() && size.isValid()) {
441 size.intersect(crop, &size);
442 } else if (!crop.isEmpty()) {
443 size = crop;
444 }
445 return size;
446}
447
448Rect RequestedLayerState::getBufferCrop() const {
449 // this is the crop rectangle that applies to the buffer
450 // itself (as opposed to the window)
451 if (!bufferCrop.isEmpty()) {
452 // if the buffer crop is defined, we use that
453 return bufferCrop;
454 } else if (externalTexture != nullptr) {
455 // otherwise we use the whole buffer
456 return externalTexture->getBounds();
457 } else {
458 // if we don't have a buffer yet, we use an empty/invalid crop
459 return Rect();
460 }
461}
462
463aidl::android::hardware::graphics::composer3::Composition RequestedLayerState::getCompositionType()
464 const {
465 using aidl::android::hardware::graphics::composer3::Composition;
466 // TODO(b/238781169) check about sidestream ready flag
467 if (sidebandStream.get()) {
468 return Composition::SIDEBAND;
469 }
470 if (!externalTexture) {
471 return Composition::SOLID_COLOR;
472 }
473 if (flags & layer_state_t::eLayerIsDisplayDecoration) {
474 return Composition::DISPLAY_DECORATION;
475 }
476 if (potentialCursor) {
477 return Composition::CURSOR;
478 }
479 return Composition::DEVICE;
480}
481
482Rect RequestedLayerState::reduce(const Rect& win, const Region& exclude) {
483 if (CC_LIKELY(exclude.isEmpty())) {
484 return win;
485 }
486 if (exclude.isRect()) {
487 return win.reduce(exclude.getBounds());
488 }
489 return Region(win).subtract(exclude).getBounds();
490}
491
Vishnu Nair04f89692022-11-16 23:21:05 +0000492// Returns true if the layer has a relative parent that is not its own parent. This is an input
493// error from the client, and this check allows us to handle it gracefully. If both parentId and
494// relativeParentId is unassigned then the layer does not have a valid relative parent.
495// If the relative parentid is unassigned, the layer will be considered relative but won't be
496// reachable.
497bool RequestedLayerState::hasValidRelativeParent() const {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000498 return isRelativeOf &&
499 (parentId != relativeParentId || relativeParentId == UNASSIGNED_LAYER_ID);
Vishnu Nair04f89692022-11-16 23:21:05 +0000500}
501
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000502bool RequestedLayerState::hasInputInfo() const {
503 if (!windowInfoHandle) {
504 return false;
505 }
506 const auto windowInfo = windowInfoHandle->getInfo();
507 return windowInfo->token != nullptr ||
508 windowInfo->inputConfig.test(gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL);
509}
510
Vishnu Nair80a5a702023-02-11 01:21:51 +0000511bool RequestedLayerState::hasBlur() const {
512 return backgroundBlurRadius > 0 || blurRegions.size() > 0;
513}
514
Vishnu Naird47bcee2023-02-24 18:08:51 +0000515bool RequestedLayerState::hasFrameUpdate() const {
516 return what & layer_state_t::CONTENT_DIRTY &&
517 (externalTexture || bgColorLayerId != UNASSIGNED_LAYER_ID);
518}
519
520bool RequestedLayerState::hasReadyFrame() const {
521 return hasFrameUpdate() || changes.test(Changes::SidebandStream) || autoRefresh;
522}
523
524bool RequestedLayerState::hasSidebandStreamFrame() const {
525 return hasFrameUpdate() && sidebandStream.get();
526}
527
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700528bool RequestedLayerState::willReleaseBufferOnLatch() const {
529 return changes.test(Changes::Buffer) && !externalTexture;
530}
531
Vishnu Nair4d9cef92023-06-24 22:34:41 +0000532bool RequestedLayerState::backpressureEnabled() const {
533 return flags & layer_state_t::eEnableBackpressure;
534}
535
536bool RequestedLayerState::isSimpleBufferUpdate(const layer_state_t& s) const {
537 static constexpr uint64_t requiredFlags = layer_state_t::eBufferChanged;
538 if ((s.what & requiredFlags) != requiredFlags) {
539 ATRACE_FORMAT_INSTANT("%s: false [missing required flags 0x%" PRIx64 "]", __func__,
540 (s.what | requiredFlags) & ~s.what);
541 return false;
542 }
543
544 static constexpr uint64_t deniedFlags = layer_state_t::eProducerDisconnect |
545 layer_state_t::eLayerChanged | layer_state_t::eRelativeLayerChanged |
546 layer_state_t::eTransparentRegionChanged | layer_state_t::eFlagsChanged |
547 layer_state_t::eBlurRegionsChanged | layer_state_t::eLayerStackChanged |
548 layer_state_t::eAutoRefreshChanged | layer_state_t::eReparent;
549 if (s.what & deniedFlags) {
550 ATRACE_FORMAT_INSTANT("%s: false [has denied flags 0x%" PRIx64 "]", __func__,
551 s.what & deniedFlags);
552 return false;
553 }
554
555 bool changedFlags = diff(s);
556 static constexpr auto deniedChanges = layer_state_t::ePositionChanged |
557 layer_state_t::eAlphaChanged | layer_state_t::eColorTransformChanged |
558 layer_state_t::eBackgroundColorChanged | layer_state_t::eMatrixChanged |
559 layer_state_t::eCornerRadiusChanged | layer_state_t::eBackgroundBlurRadiusChanged |
560 layer_state_t::eBufferTransformChanged |
561 layer_state_t::eTransformToDisplayInverseChanged | layer_state_t::eCropChanged |
562 layer_state_t::eDataspaceChanged | layer_state_t::eHdrMetadataChanged |
563 layer_state_t::eSidebandStreamChanged | layer_state_t::eColorSpaceAgnosticChanged |
564 layer_state_t::eShadowRadiusChanged | layer_state_t::eFixedTransformHintChanged |
565 layer_state_t::eTrustedOverlayChanged | layer_state_t::eStretchChanged |
566 layer_state_t::eBufferCropChanged | layer_state_t::eDestinationFrameChanged |
567 layer_state_t::eDimmingEnabledChanged | layer_state_t::eExtendedRangeBrightnessChanged;
568 if (changedFlags & deniedChanges) {
569 ATRACE_FORMAT_INSTANT("%s: false [has denied changes flags 0x%" PRIx64 "]", __func__,
570 s.what & deniedChanges);
571 return false;
572 }
573
574 return true;
575}
576
Vishnu Nair0808ae62023-08-07 21:42:42 -0700577bool RequestedLayerState::isProtected() const {
578 return externalTexture && externalTexture->getUsage() & GRALLOC_USAGE_PROTECTED;
579}
580
Vishnu Nair854ce1c2023-08-19 15:00:13 -0700581bool RequestedLayerState::hasSomethingToDraw() const {
582 return externalTexture != nullptr || sidebandStream != nullptr || shadowRadius > 0.f ||
583 backgroundBlurRadius > 0 || blurRegions.size() > 0 ||
584 (color.r >= 0.0_hf && color.g >= 0.0_hf && color.b >= 0.0_hf);
585}
586
Vishnu Naird47bcee2023-02-24 18:08:51 +0000587void RequestedLayerState::clearChanges() {
588 what = 0;
589 changes.clear();
590}
591
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000592} // namespace android::surfaceflinger::frontend