blob: 43800e279f5fb34e872c776c15d53dda82f7b444 [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),
56 textureName(args.textureName),
57 ownerUid(args.ownerUid),
Vishnu Nair1391de22023-03-05 19:56:14 -080058 ownerPid(args.ownerPid),
59 parentId(args.parentId),
60 layerIdToMirror(args.layerIdToMirror) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000061 layerId = static_cast<int32_t>(args.sequence);
62 changes |= RequestedLayerState::Changes::Created;
63 metadata.merge(args.metadata);
64 changes |= RequestedLayerState::Changes::Metadata;
65 handleAlive = true;
Vishnu Nair1391de22023-03-05 19:56:14 -080066 if (parentId != UNASSIGNED_LAYER_ID) {
Vishnu Naircfb2d252023-01-19 04:44:02 +000067 canBeRoot = false;
68 }
Vishnu Naira9c43762023-01-27 19:10:25 +000069 if (layerIdToMirror != UNASSIGNED_LAYER_ID) {
70 changes |= RequestedLayerState::Changes::Mirror;
71 } else if (args.layerStackToMirror != ui::INVALID_LAYER_STACK) {
72 layerStackToMirror = args.layerStackToMirror;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000073 changes |= RequestedLayerState::Changes::Mirror;
74 }
75
76 flags = 0;
77 if (args.flags & ISurfaceComposerClient::eHidden) flags |= layer_state_t::eLayerHidden;
78 if (args.flags & ISurfaceComposerClient::eOpaque) flags |= layer_state_t::eLayerOpaque;
79 if (args.flags & ISurfaceComposerClient::eSecure) flags |= layer_state_t::eLayerSecure;
80 if (args.flags & ISurfaceComposerClient::eSkipScreenshot) {
81 flags |= layer_state_t::eLayerSkipScreenshot;
82 }
83 premultipliedAlpha = !(args.flags & ISurfaceComposerClient::eNonPremultiplied);
84 potentialCursor = args.flags & ISurfaceComposerClient::eCursorWindow;
85 protectedByApp = args.flags & ISurfaceComposerClient::eProtectedByApp;
86 if (args.flags & ISurfaceComposerClient::eNoColorFill) {
87 // Set an invalid color so there is no color fill.
88 // (b/259981098) use an explicit flag instead of relying on invalid values.
89 color.r = -1.0_hf;
90 color.g = -1.0_hf;
91 color.b = -1.0_hf;
92 } else {
93 color.rgb = {0.0_hf, 0.0_hf, 0.0_hf};
94 }
Vishnu Naircfb2d252023-01-19 04:44:02 +000095 LLOGV(layerId, "Created %s flags=%d", getDebugString().c_str(), flags);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000096 color.a = 1.0f;
97
98 crop.makeInvalid();
99 z = 0;
100 layerStack = ui::DEFAULT_LAYER_STACK;
101 transformToDisplayInverse = false;
102 dataspace = ui::Dataspace::UNKNOWN;
Sally Qi963049b2023-03-23 14:06:21 -0700103 desiredHdrSdrRatio = 1.f;
104 currentHdrSdrRatio = 1.f;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000105 dataspaceRequested = false;
106 hdrMetadata.validTypes = 0;
107 surfaceDamageRegion = Region::INVALID_REGION;
108 cornerRadius = 0.0f;
109 backgroundBlurRadius = 0;
110 api = -1;
111 hasColorTransform = false;
112 bufferTransform = 0;
113 requestedTransform.reset();
114 bufferData = std::make_shared<BufferData>();
115 bufferData->frameNumber = 0;
116 bufferData->acquireFence = sp<Fence>::make(-1);
117 acquireFenceTime = std::make_shared<FenceTime>(bufferData->acquireFence);
118 colorSpaceAgnostic = false;
119 frameRateSelectionPriority = Layer::PRIORITY_UNSET;
120 shadowRadius = 0.f;
121 fixedTransformHint = ui::Transform::ROT_INVALID;
122 destinationFrame.makeInvalid();
123 isTrustedOverlay = false;
124 dropInputMode = gui::DropInputMode::NONE;
125 dimmingEnabled = true;
126 defaultFrameRateCompatibility =
127 static_cast<int8_t>(scheduler::LayerInfo::FrameRateCompatibility::Default);
Rachel Leece6e0042023-06-27 11:22:54 -0700128 frameRateCategory = static_cast<int8_t>(FrameRateCategory::Default);
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;
154
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700155 const bool hadSideStream = sidebandStream != nullptr;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000156 const layer_state_t& clientState = resolvedComposerState.state;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000157 const bool hadBlur = hasBlur();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000158 uint64_t clientChanges = what | layer_state_t::diff(clientState);
159 layer_state_t::merge(clientState);
160 what = clientChanges;
Vishnu Naira02943f2023-06-03 13:44:46 -0700161 LLOGV(layerId, "requested=%" PRIu64 "flags=%" PRIu64, clientState.what, clientChanges);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000162
163 if (clientState.what & layer_state_t::eFlagsChanged) {
164 if ((oldFlags ^ flags) & layer_state_t::eLayerHidden) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000165 changes |= RequestedLayerState::Changes::Visibility |
166 RequestedLayerState::Changes::VisibleRegion;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000167 }
168 if ((oldFlags ^ flags) & layer_state_t::eIgnoreDestinationFrame) {
169 changes |= RequestedLayerState::Changes::Geometry;
170 }
171 }
Vishnu Naird1f74982023-06-15 20:16:51 -0700172
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700173 if (clientState.what & layer_state_t::eBufferChanged) {
174 externalTexture = resolvedComposerState.externalTexture;
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700175 const bool hasBuffer = externalTexture != nullptr;
176 if (hasBuffer || hasBuffer != hadBuffer) {
177 changes |= RequestedLayerState::Changes::Buffer;
Vishnu Naira02943f2023-06-03 13:44:46 -0700178 const ui::Size newBufferSize = hasBuffer
179 ? ui::Size(externalTexture->getWidth(), externalTexture->getHeight())
180 : ui::Size();
181 if (oldBufferSize != newBufferSize) {
182 changes |= RequestedLayerState::Changes::BufferSize;
183 changes |= RequestedLayerState::Changes::Geometry;
184 }
Vishnu Nair0808ae62023-08-07 21:42:42 -0700185 const uint64_t usageFlags = hasBuffer ? externalTexture->getUsage() : 0;
186 if (oldUsageFlags != usageFlags) {
187 changes |= RequestedLayerState::Changes::BufferUsageFlags;
188 }
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700189 }
190
191 if (hasBuffer != hadBuffer) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000192 changes |= RequestedLayerState::Changes::Geometry |
193 RequestedLayerState::Changes::VisibleRegion |
Vishnu Naird47bcee2023-02-24 18:08:51 +0000194 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Input;
195 }
Vishnu Naird1f74982023-06-15 20:16:51 -0700196
197 if (hasBuffer) {
198 const bool frameNumberChanged =
199 bufferData->flags.test(BufferData::BufferDataChange::frameNumberChanged);
200 const uint64_t frameNumber =
201 frameNumberChanged ? bufferData->frameNumber : oldFramenumber + 1;
202 bufferData->frameNumber = frameNumber;
203
204 if ((barrierProducerId > bufferData->producerId) ||
205 ((barrierProducerId == bufferData->producerId) &&
206 (barrierFrameNumber > bufferData->frameNumber))) {
207 ALOGE("Out of order buffers detected for %s producedId=%d frameNumber=%" PRIu64
208 " -> producedId=%d frameNumber=%" PRIu64,
209 getDebugString().c_str(), bufferData->producerId, bufferData->frameNumber,
210 bufferData->producerId, frameNumber);
211 TransactionTraceWriter::getInstance().invoke("out_of_order_buffers_",
212 /*overwrite=*/false);
213 }
214
215 barrierProducerId = std::max(bufferData->producerId, barrierProducerId);
216 barrierFrameNumber = std::max(bufferData->frameNumber, barrierFrameNumber);
217 }
Vishnu Nair63221212023-04-06 15:17:37 -0700218 }
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700219
Vishnu Nair63221212023-04-06 15:17:37 -0700220 if (clientState.what & layer_state_t::eSidebandStreamChanged) {
221 changes |= RequestedLayerState::Changes::SidebandStream;
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700222 const bool hasSideStream = sidebandStream != nullptr;
223 if (hasSideStream != hadSideStream) {
224 changes |= RequestedLayerState::Changes::Geometry |
225 RequestedLayerState::Changes::VisibleRegion |
226 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Input;
227 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000228 }
229 if (what & (layer_state_t::eAlphaChanged)) {
230 if (oldAlpha == 0 || color.a == 0) {
231 changes |= RequestedLayerState::Changes::Visibility |
232 RequestedLayerState::Changes::VisibleRegion;
233 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000234 }
Vishnu Nair80a5a702023-02-11 01:21:51 +0000235 if (what & (layer_state_t::eBackgroundBlurRadiusChanged | layer_state_t::eBlurRegionsChanged)) {
236 if (hadBlur != hasBlur()) {
237 changes |= RequestedLayerState::Changes::Visibility |
238 RequestedLayerState::Changes::VisibleRegion;
239 }
240 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000241 if (clientChanges & layer_state_t::HIERARCHY_CHANGES)
242 changes |= RequestedLayerState::Changes::Hierarchy;
243 if (clientChanges & layer_state_t::CONTENT_CHANGES)
244 changes |= RequestedLayerState::Changes::Content;
245 if (clientChanges & layer_state_t::GEOMETRY_CHANGES)
246 changes |= RequestedLayerState::Changes::Geometry;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000247 if (clientChanges & layer_state_t::AFFECTS_CHILDREN)
248 changes |= RequestedLayerState::Changes::AffectsChildren;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000249 if (clientChanges & layer_state_t::INPUT_CHANGES)
250 changes |= RequestedLayerState::Changes::Input;
251 if (clientChanges & layer_state_t::VISIBLE_REGION_CHANGES)
252 changes |= RequestedLayerState::Changes::VisibleRegion;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000253 if (clientState.what & layer_state_t::eColorTransformChanged) {
254 static const mat4 identityMatrix = mat4();
255 hasColorTransform = colorTransform != identityMatrix;
256 }
Vishnu Naird47bcee2023-02-24 18:08:51 +0000257 if (clientState.what &
258 (layer_state_t::eLayerChanged | layer_state_t::eRelativeLayerChanged |
259 layer_state_t::eLayerStackChanged)) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000260 changes |= RequestedLayerState::Changes::Z;
261 }
262 if (clientState.what & layer_state_t::eReparent) {
263 changes |= RequestedLayerState::Changes::Parent;
Vishnu Nair1391de22023-03-05 19:56:14 -0800264 parentId = resolvedComposerState.parentId;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000265 parentSurfaceControlForChild = nullptr;
Vishnu Nair04f89692022-11-16 23:21:05 +0000266 // Once a layer has be reparented, it cannot be placed at the root. It sounds odd
267 // but thats the existing logic and until we make this behavior more explicit, we need
268 // to maintain this logic.
269 canBeRoot = false;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000270 }
271 if (clientState.what & layer_state_t::eRelativeLayerChanged) {
272 changes |= RequestedLayerState::Changes::RelativeParent;
Vishnu Nair1391de22023-03-05 19:56:14 -0800273 relativeParentId = resolvedComposerState.relativeParentId;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000274 isRelativeOf = true;
275 relativeLayerSurfaceControl = nullptr;
276 }
277 if ((clientState.what & layer_state_t::eLayerChanged ||
278 (clientState.what & layer_state_t::eReparent && parentId == UNASSIGNED_LAYER_ID)) &&
279 isRelativeOf) {
280 // clear out relz data
281 relativeParentId = UNASSIGNED_LAYER_ID;
282 isRelativeOf = false;
283 changes |= RequestedLayerState::Changes::RelativeParent;
284 }
285 if (clientState.what & layer_state_t::eReparent && parentId == relativeParentId) {
286 // provide a hint that we are are now a direct child and not a relative child.
287 changes |= RequestedLayerState::Changes::RelativeParent;
288 }
289 if (clientState.what & layer_state_t::eInputInfoChanged) {
Vishnu Nair1391de22023-03-05 19:56:14 -0800290 touchCropId = resolvedComposerState.touchCropId;
291 windowInfoHandle->editInfo()->touchableRegionCropHandle.clear();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000292 }
293 if (clientState.what & layer_state_t::eStretchChanged) {
294 stretchEffect.sanitize();
295 }
296
297 if (clientState.what & layer_state_t::eHasListenerCallbacksChanged) {
298 // TODO(b/238781169) handle callbacks
299 }
300
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000301 if (clientState.what & layer_state_t::ePositionChanged) {
302 requestedTransform.set(x, y);
303 }
304
305 if (clientState.what & layer_state_t::eMatrixChanged) {
306 requestedTransform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
307 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000308 if (clientState.what & layer_state_t::eMetadataChanged) {
309 const int32_t requestedGameMode =
310 clientState.metadata.getInt32(gui::METADATA_GAME_MODE, -1);
311 if (requestedGameMode != -1) {
312 // The transaction will be received on the Task layer and needs to be applied to all
313 // child layers.
314 if (static_cast<int32_t>(gameMode) != requestedGameMode) {
315 gameMode = static_cast<gui::GameMode>(requestedGameMode);
Vishnu Naira02943f2023-06-03 13:44:46 -0700316 changes |= RequestedLayerState::Changes::GameMode;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000317 }
318 }
319 }
320 if (clientState.what & layer_state_t::eFrameRateChanged) {
321 const auto compatibility =
322 Layer::FrameRate::convertCompatibility(clientState.frameRateCompatibility);
323 const auto strategy = Layer::FrameRate::convertChangeFrameRateStrategy(
324 clientState.changeFrameRateStrategy);
Rachel Leece6e0042023-06-27 11:22:54 -0700325 requestedFrameRate.vote =
326 Layer::FrameRate::FrameRateVote(Fps::fromValue(clientState.frameRate),
327 compatibility, strategy);
328 changes |= RequestedLayerState::Changes::FrameRate;
329 }
330 if (clientState.what & layer_state_t::eFrameRateCategoryChanged) {
331 const auto category = Layer::FrameRate::convertCategory(clientState.frameRateCategory);
332 requestedFrameRate.category = category;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000333 changes |= RequestedLayerState::Changes::FrameRate;
334 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000335}
336
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000337ui::Size RequestedLayerState::getUnrotatedBufferSize(uint32_t displayRotationFlags) const {
338 uint32_t bufferWidth = externalTexture->getWidth();
339 uint32_t bufferHeight = externalTexture->getHeight();
340 // Undo any transformations on the buffer.
341 if (bufferTransform & ui::Transform::ROT_90) {
342 std::swap(bufferWidth, bufferHeight);
343 }
344 if (transformToDisplayInverse) {
345 if (displayRotationFlags & ui::Transform::ROT_90) {
346 std::swap(bufferWidth, bufferHeight);
347 }
348 }
349 return {bufferWidth, bufferHeight};
350}
351
352ui::Transform RequestedLayerState::getTransform(uint32_t displayRotationFlags) const {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000353 if ((flags & layer_state_t::eIgnoreDestinationFrame) || destinationFrame.isEmpty()) {
354 // If destination frame is not set, use the requested transform set via
355 // Transaction::setPosition and Transaction::setMatrix.
356 return requestedTransform;
357 }
358
359 Rect destRect = destinationFrame;
360 int32_t destW = destRect.width();
361 int32_t destH = destRect.height();
362 if (destRect.left < 0) {
363 destRect.left = 0;
364 destRect.right = destW;
365 }
366 if (destRect.top < 0) {
367 destRect.top = 0;
368 destRect.bottom = destH;
369 }
370
371 if (!externalTexture) {
372 ui::Transform transform;
373 transform.set(static_cast<float>(destRect.left), static_cast<float>(destRect.top));
374 return transform;
375 }
376
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000377 ui::Size bufferSize = getUnrotatedBufferSize(displayRotationFlags);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000378
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000379 float sx = static_cast<float>(destW) / static_cast<float>(bufferSize.width);
380 float sy = static_cast<float>(destH) / static_cast<float>(bufferSize.height);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000381 ui::Transform transform;
382 transform.set(sx, 0, 0, sy);
383 transform.set(static_cast<float>(destRect.left), static_cast<float>(destRect.top));
384 return transform;
385}
386
387std::string RequestedLayerState::getDebugString() const {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000388 std::stringstream debug;
Vishnu Nair444f3952023-04-11 13:01:02 -0700389 debug << "RequestedLayerState{" << name;
390 if (parentId != UNASSIGNED_LAYER_ID) debug << " parentId=" << parentId;
391 if (relativeParentId != UNASSIGNED_LAYER_ID) debug << " relativeParentId=" << relativeParentId;
392 if (!mirrorIds.empty()) debug << " mirrorId=" << layerIdsToString(mirrorIds);
393 if (!handleAlive) debug << " !handle";
394 if (z != 0) debug << " z=" << z;
395 if (layerStack.id != 0) debug << " layerStack=" << layerStack.id;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000396 return debug.str();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000397}
398
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000399std::ostream& operator<<(std::ostream& out, const RequestedLayerState& obj) {
400 out << obj.debugName;
401 if (obj.relativeParentId != UNASSIGNED_LAYER_ID) out << " parent=" << obj.parentId;
402 if (!obj.handleAlive) out << " handleNotAlive";
403 return out;
404}
405
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000406std::string RequestedLayerState::getDebugStringShort() const {
407 return "[" + std::to_string(id) + "]" + name;
408}
409
410bool RequestedLayerState::canBeDestroyed() const {
411 return !handleAlive && parentId == UNASSIGNED_LAYER_ID;
412}
413bool RequestedLayerState::isRoot() const {
414 return canBeRoot && parentId == UNASSIGNED_LAYER_ID;
415}
416bool RequestedLayerState::isHiddenByPolicy() const {
417 return (flags & layer_state_t::eLayerHidden) == layer_state_t::eLayerHidden;
418};
419half4 RequestedLayerState::getColor() const {
Vishnu Naira02943f2023-06-03 13:44:46 -0700420 if (sidebandStream || externalTexture) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000421 return {0._hf, 0._hf, 0._hf, color.a};
422 }
423 return color;
424}
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000425Rect RequestedLayerState::getBufferSize(uint32_t displayRotationFlags) const {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000426 // for buffer state layers we use the display frame size as the buffer size.
427 if (!externalTexture) {
428 return Rect::INVALID_RECT;
429 }
430
431 uint32_t bufWidth = externalTexture->getWidth();
432 uint32_t bufHeight = externalTexture->getHeight();
433
434 // Undo any transformations on the buffer and return the result.
435 if (bufferTransform & ui::Transform::ROT_90) {
436 std::swap(bufWidth, bufHeight);
437 }
438
439 if (transformToDisplayInverse) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000440 uint32_t invTransform = displayRotationFlags;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000441 if (invTransform & ui::Transform::ROT_90) {
442 std::swap(bufWidth, bufHeight);
443 }
444 }
445
446 return Rect(0, 0, static_cast<int32_t>(bufWidth), static_cast<int32_t>(bufHeight));
447}
448
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000449Rect RequestedLayerState::getCroppedBufferSize(const Rect& bufferSize) const {
450 Rect size = bufferSize;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000451 if (!crop.isEmpty() && size.isValid()) {
452 size.intersect(crop, &size);
453 } else if (!crop.isEmpty()) {
454 size = crop;
455 }
456 return size;
457}
458
459Rect RequestedLayerState::getBufferCrop() const {
460 // this is the crop rectangle that applies to the buffer
461 // itself (as opposed to the window)
462 if (!bufferCrop.isEmpty()) {
463 // if the buffer crop is defined, we use that
464 return bufferCrop;
465 } else if (externalTexture != nullptr) {
466 // otherwise we use the whole buffer
467 return externalTexture->getBounds();
468 } else {
469 // if we don't have a buffer yet, we use an empty/invalid crop
470 return Rect();
471 }
472}
473
474aidl::android::hardware::graphics::composer3::Composition RequestedLayerState::getCompositionType()
475 const {
476 using aidl::android::hardware::graphics::composer3::Composition;
477 // TODO(b/238781169) check about sidestream ready flag
478 if (sidebandStream.get()) {
479 return Composition::SIDEBAND;
480 }
481 if (!externalTexture) {
482 return Composition::SOLID_COLOR;
483 }
484 if (flags & layer_state_t::eLayerIsDisplayDecoration) {
485 return Composition::DISPLAY_DECORATION;
486 }
487 if (potentialCursor) {
488 return Composition::CURSOR;
489 }
490 return Composition::DEVICE;
491}
492
493Rect RequestedLayerState::reduce(const Rect& win, const Region& exclude) {
494 if (CC_LIKELY(exclude.isEmpty())) {
495 return win;
496 }
497 if (exclude.isRect()) {
498 return win.reduce(exclude.getBounds());
499 }
500 return Region(win).subtract(exclude).getBounds();
501}
502
Vishnu Nair04f89692022-11-16 23:21:05 +0000503// Returns true if the layer has a relative parent that is not its own parent. This is an input
504// error from the client, and this check allows us to handle it gracefully. If both parentId and
505// relativeParentId is unassigned then the layer does not have a valid relative parent.
506// If the relative parentid is unassigned, the layer will be considered relative but won't be
507// reachable.
508bool RequestedLayerState::hasValidRelativeParent() const {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000509 return isRelativeOf &&
510 (parentId != relativeParentId || relativeParentId == UNASSIGNED_LAYER_ID);
Vishnu Nair04f89692022-11-16 23:21:05 +0000511}
512
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000513bool RequestedLayerState::hasInputInfo() const {
514 if (!windowInfoHandle) {
515 return false;
516 }
517 const auto windowInfo = windowInfoHandle->getInfo();
518 return windowInfo->token != nullptr ||
519 windowInfo->inputConfig.test(gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL);
520}
521
Vishnu Nair80a5a702023-02-11 01:21:51 +0000522bool RequestedLayerState::hasBlur() const {
523 return backgroundBlurRadius > 0 || blurRegions.size() > 0;
524}
525
Vishnu Naird47bcee2023-02-24 18:08:51 +0000526bool RequestedLayerState::hasFrameUpdate() const {
527 return what & layer_state_t::CONTENT_DIRTY &&
528 (externalTexture || bgColorLayerId != UNASSIGNED_LAYER_ID);
529}
530
531bool RequestedLayerState::hasReadyFrame() const {
532 return hasFrameUpdate() || changes.test(Changes::SidebandStream) || autoRefresh;
533}
534
535bool RequestedLayerState::hasSidebandStreamFrame() const {
536 return hasFrameUpdate() && sidebandStream.get();
537}
538
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700539bool RequestedLayerState::willReleaseBufferOnLatch() const {
540 return changes.test(Changes::Buffer) && !externalTexture;
541}
542
Vishnu Nair4d9cef92023-06-24 22:34:41 +0000543bool RequestedLayerState::backpressureEnabled() const {
544 return flags & layer_state_t::eEnableBackpressure;
545}
546
547bool RequestedLayerState::isSimpleBufferUpdate(const layer_state_t& s) const {
548 static constexpr uint64_t requiredFlags = layer_state_t::eBufferChanged;
549 if ((s.what & requiredFlags) != requiredFlags) {
550 ATRACE_FORMAT_INSTANT("%s: false [missing required flags 0x%" PRIx64 "]", __func__,
551 (s.what | requiredFlags) & ~s.what);
552 return false;
553 }
554
555 static constexpr uint64_t deniedFlags = layer_state_t::eProducerDisconnect |
556 layer_state_t::eLayerChanged | layer_state_t::eRelativeLayerChanged |
557 layer_state_t::eTransparentRegionChanged | layer_state_t::eFlagsChanged |
558 layer_state_t::eBlurRegionsChanged | layer_state_t::eLayerStackChanged |
559 layer_state_t::eAutoRefreshChanged | layer_state_t::eReparent;
560 if (s.what & deniedFlags) {
561 ATRACE_FORMAT_INSTANT("%s: false [has denied flags 0x%" PRIx64 "]", __func__,
562 s.what & deniedFlags);
563 return false;
564 }
565
566 bool changedFlags = diff(s);
567 static constexpr auto deniedChanges = layer_state_t::ePositionChanged |
568 layer_state_t::eAlphaChanged | layer_state_t::eColorTransformChanged |
569 layer_state_t::eBackgroundColorChanged | layer_state_t::eMatrixChanged |
570 layer_state_t::eCornerRadiusChanged | layer_state_t::eBackgroundBlurRadiusChanged |
571 layer_state_t::eBufferTransformChanged |
572 layer_state_t::eTransformToDisplayInverseChanged | layer_state_t::eCropChanged |
573 layer_state_t::eDataspaceChanged | layer_state_t::eHdrMetadataChanged |
574 layer_state_t::eSidebandStreamChanged | layer_state_t::eColorSpaceAgnosticChanged |
575 layer_state_t::eShadowRadiusChanged | layer_state_t::eFixedTransformHintChanged |
576 layer_state_t::eTrustedOverlayChanged | layer_state_t::eStretchChanged |
577 layer_state_t::eBufferCropChanged | layer_state_t::eDestinationFrameChanged |
578 layer_state_t::eDimmingEnabledChanged | layer_state_t::eExtendedRangeBrightnessChanged;
579 if (changedFlags & deniedChanges) {
580 ATRACE_FORMAT_INSTANT("%s: false [has denied changes flags 0x%" PRIx64 "]", __func__,
581 s.what & deniedChanges);
582 return false;
583 }
584
585 return true;
586}
587
Vishnu Nair0808ae62023-08-07 21:42:42 -0700588bool RequestedLayerState::isProtected() const {
589 return externalTexture && externalTexture->getUsage() & GRALLOC_USAGE_PROTECTED;
590}
591
Vishnu Naird47bcee2023-02-24 18:08:51 +0000592void RequestedLayerState::clearChanges() {
593 what = 0;
594 changes.clear();
595}
596
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000597} // namespace android::surfaceflinger::frontend