blob: 453b51e9173a6a99e479c41411164e113458dca1 [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;
125 defaultFrameRateCompatibility =
126 static_cast<int8_t>(scheduler::LayerInfo::FrameRateCompatibility::Default);
Rachel Leece6e0042023-06-27 11:22:54 -0700127 frameRateCategory = static_cast<int8_t>(FrameRateCategory::Default);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000128 dataspace = ui::Dataspace::V0_SRGB;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000129 gameMode = gui::GameMode::Unsupported;
130 requestedFrameRate = {};
Alec Mourif4af03e2023-02-11 00:25:24 +0000131 cachingHint = gui::CachingHint::Enabled;
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000132
133 if (name.length() > 77) {
134 std::string shortened;
135 shortened.append(name, 0, 36);
136 shortened.append("[...]");
137 shortened.append(name, name.length() - 36);
138 debugName = std::move(shortened);
139 } else {
140 debugName = name;
141 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000142}
143
144void RequestedLayerState::merge(const ResolvedComposerState& resolvedComposerState) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000145 const uint32_t oldFlags = flags;
146 const half oldAlpha = color.a;
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700147 const bool hadBuffer = externalTexture != nullptr;
Vishnu Naird1f74982023-06-15 20:16:51 -0700148 uint64_t oldFramenumber = hadBuffer ? bufferData->frameNumber : 0;
Vishnu Naira02943f2023-06-03 13:44:46 -0700149 const ui::Size oldBufferSize = hadBuffer
150 ? ui::Size(externalTexture->getWidth(), externalTexture->getHeight())
151 : ui::Size();
Vishnu Nair0808ae62023-08-07 21:42:42 -0700152 const uint64_t oldUsageFlags = hadBuffer ? externalTexture->getUsage() : 0;
153
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700154 const bool hadSideStream = sidebandStream != nullptr;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000155 const layer_state_t& clientState = resolvedComposerState.state;
Vishnu Nair854ce1c2023-08-19 15:00:13 -0700156 const bool hadSomethingToDraw = hasSomethingToDraw();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000157 uint64_t clientChanges = what | layer_state_t::diff(clientState);
158 layer_state_t::merge(clientState);
159 what = clientChanges;
Vishnu Naira02943f2023-06-03 13:44:46 -0700160 LLOGV(layerId, "requested=%" PRIu64 "flags=%" PRIu64, clientState.what, clientChanges);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000161
162 if (clientState.what & layer_state_t::eFlagsChanged) {
163 if ((oldFlags ^ flags) & layer_state_t::eLayerHidden) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000164 changes |= RequestedLayerState::Changes::Visibility |
165 RequestedLayerState::Changes::VisibleRegion;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000166 }
167 if ((oldFlags ^ flags) & layer_state_t::eIgnoreDestinationFrame) {
168 changes |= RequestedLayerState::Changes::Geometry;
169 }
170 }
Vishnu Naird1f74982023-06-15 20:16:51 -0700171
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700172 if (clientState.what & layer_state_t::eBufferChanged) {
173 externalTexture = resolvedComposerState.externalTexture;
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700174 const bool hasBuffer = externalTexture != nullptr;
175 if (hasBuffer || hasBuffer != hadBuffer) {
176 changes |= RequestedLayerState::Changes::Buffer;
Vishnu Naira02943f2023-06-03 13:44:46 -0700177 const ui::Size newBufferSize = hasBuffer
178 ? ui::Size(externalTexture->getWidth(), externalTexture->getHeight())
179 : ui::Size();
180 if (oldBufferSize != newBufferSize) {
181 changes |= RequestedLayerState::Changes::BufferSize;
182 changes |= RequestedLayerState::Changes::Geometry;
183 }
Vishnu Nair0808ae62023-08-07 21:42:42 -0700184 const uint64_t usageFlags = hasBuffer ? externalTexture->getUsage() : 0;
185 if (oldUsageFlags != usageFlags) {
186 changes |= RequestedLayerState::Changes::BufferUsageFlags;
187 }
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700188 }
189
190 if (hasBuffer != hadBuffer) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000191 changes |= RequestedLayerState::Changes::Geometry |
192 RequestedLayerState::Changes::VisibleRegion |
Vishnu Naird47bcee2023-02-24 18:08:51 +0000193 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Input;
194 }
Vishnu Naird1f74982023-06-15 20:16:51 -0700195
196 if (hasBuffer) {
197 const bool frameNumberChanged =
198 bufferData->flags.test(BufferData::BufferDataChange::frameNumberChanged);
199 const uint64_t frameNumber =
200 frameNumberChanged ? bufferData->frameNumber : oldFramenumber + 1;
201 bufferData->frameNumber = frameNumber;
202
203 if ((barrierProducerId > bufferData->producerId) ||
204 ((barrierProducerId == bufferData->producerId) &&
205 (barrierFrameNumber > bufferData->frameNumber))) {
206 ALOGE("Out of order buffers detected for %s producedId=%d frameNumber=%" PRIu64
207 " -> producedId=%d frameNumber=%" PRIu64,
208 getDebugString().c_str(), bufferData->producerId, bufferData->frameNumber,
209 bufferData->producerId, frameNumber);
210 TransactionTraceWriter::getInstance().invoke("out_of_order_buffers_",
211 /*overwrite=*/false);
212 }
213
214 barrierProducerId = std::max(bufferData->producerId, barrierProducerId);
215 barrierFrameNumber = std::max(bufferData->frameNumber, barrierFrameNumber);
216 }
Vishnu Nair63221212023-04-06 15:17:37 -0700217 }
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700218
Vishnu Nair63221212023-04-06 15:17:37 -0700219 if (clientState.what & layer_state_t::eSidebandStreamChanged) {
220 changes |= RequestedLayerState::Changes::SidebandStream;
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700221 const bool hasSideStream = sidebandStream != nullptr;
222 if (hasSideStream != hadSideStream) {
223 changes |= RequestedLayerState::Changes::Geometry |
224 RequestedLayerState::Changes::VisibleRegion |
225 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Input;
226 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000227 }
228 if (what & (layer_state_t::eAlphaChanged)) {
229 if (oldAlpha == 0 || color.a == 0) {
230 changes |= RequestedLayerState::Changes::Visibility |
231 RequestedLayerState::Changes::VisibleRegion;
232 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000233 }
Vishnu Nair854ce1c2023-08-19 15:00:13 -0700234
235 if (hadSomethingToDraw != hasSomethingToDraw()) {
236 changes |= RequestedLayerState::Changes::Visibility |
237 RequestedLayerState::Changes::VisibleRegion;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000238 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000239 if (clientChanges & layer_state_t::HIERARCHY_CHANGES)
240 changes |= RequestedLayerState::Changes::Hierarchy;
241 if (clientChanges & layer_state_t::CONTENT_CHANGES)
242 changes |= RequestedLayerState::Changes::Content;
243 if (clientChanges & layer_state_t::GEOMETRY_CHANGES)
244 changes |= RequestedLayerState::Changes::Geometry;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000245 if (clientChanges & layer_state_t::AFFECTS_CHILDREN)
246 changes |= RequestedLayerState::Changes::AffectsChildren;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000247 if (clientChanges & layer_state_t::INPUT_CHANGES)
248 changes |= RequestedLayerState::Changes::Input;
249 if (clientChanges & layer_state_t::VISIBLE_REGION_CHANGES)
250 changes |= RequestedLayerState::Changes::VisibleRegion;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000251 if (clientState.what & layer_state_t::eColorTransformChanged) {
252 static const mat4 identityMatrix = mat4();
253 hasColorTransform = colorTransform != identityMatrix;
254 }
Vishnu Naird47bcee2023-02-24 18:08:51 +0000255 if (clientState.what &
256 (layer_state_t::eLayerChanged | layer_state_t::eRelativeLayerChanged |
257 layer_state_t::eLayerStackChanged)) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000258 changes |= RequestedLayerState::Changes::Z;
259 }
260 if (clientState.what & layer_state_t::eReparent) {
261 changes |= RequestedLayerState::Changes::Parent;
Vishnu Nair1391de22023-03-05 19:56:14 -0800262 parentId = resolvedComposerState.parentId;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000263 parentSurfaceControlForChild = nullptr;
Vishnu Nair04f89692022-11-16 23:21:05 +0000264 // Once a layer has be reparented, it cannot be placed at the root. It sounds odd
265 // but thats the existing logic and until we make this behavior more explicit, we need
266 // to maintain this logic.
267 canBeRoot = false;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000268 }
269 if (clientState.what & layer_state_t::eRelativeLayerChanged) {
270 changes |= RequestedLayerState::Changes::RelativeParent;
Vishnu Nair1391de22023-03-05 19:56:14 -0800271 relativeParentId = resolvedComposerState.relativeParentId;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000272 isRelativeOf = true;
273 relativeLayerSurfaceControl = nullptr;
274 }
275 if ((clientState.what & layer_state_t::eLayerChanged ||
276 (clientState.what & layer_state_t::eReparent && parentId == UNASSIGNED_LAYER_ID)) &&
277 isRelativeOf) {
278 // clear out relz data
279 relativeParentId = UNASSIGNED_LAYER_ID;
280 isRelativeOf = false;
281 changes |= RequestedLayerState::Changes::RelativeParent;
282 }
283 if (clientState.what & layer_state_t::eReparent && parentId == relativeParentId) {
284 // provide a hint that we are are now a direct child and not a relative child.
285 changes |= RequestedLayerState::Changes::RelativeParent;
286 }
287 if (clientState.what & layer_state_t::eInputInfoChanged) {
Vishnu Nair1391de22023-03-05 19:56:14 -0800288 touchCropId = resolvedComposerState.touchCropId;
289 windowInfoHandle->editInfo()->touchableRegionCropHandle.clear();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000290 }
291 if (clientState.what & layer_state_t::eStretchChanged) {
292 stretchEffect.sanitize();
293 }
294
295 if (clientState.what & layer_state_t::eHasListenerCallbacksChanged) {
296 // TODO(b/238781169) handle callbacks
297 }
298
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000299 if (clientState.what & layer_state_t::ePositionChanged) {
300 requestedTransform.set(x, y);
301 }
302
303 if (clientState.what & layer_state_t::eMatrixChanged) {
304 requestedTransform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
305 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000306 if (clientState.what & layer_state_t::eMetadataChanged) {
307 const int32_t requestedGameMode =
308 clientState.metadata.getInt32(gui::METADATA_GAME_MODE, -1);
309 if (requestedGameMode != -1) {
310 // The transaction will be received on the Task layer and needs to be applied to all
311 // child layers.
312 if (static_cast<int32_t>(gameMode) != requestedGameMode) {
313 gameMode = static_cast<gui::GameMode>(requestedGameMode);
Vishnu Naira02943f2023-06-03 13:44:46 -0700314 changes |= RequestedLayerState::Changes::GameMode;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000315 }
316 }
317 }
318 if (clientState.what & layer_state_t::eFrameRateChanged) {
319 const auto compatibility =
320 Layer::FrameRate::convertCompatibility(clientState.frameRateCompatibility);
321 const auto strategy = Layer::FrameRate::convertChangeFrameRateStrategy(
322 clientState.changeFrameRateStrategy);
Rachel Leece6e0042023-06-27 11:22:54 -0700323 requestedFrameRate.vote =
324 Layer::FrameRate::FrameRateVote(Fps::fromValue(clientState.frameRate),
325 compatibility, strategy);
326 changes |= RequestedLayerState::Changes::FrameRate;
327 }
328 if (clientState.what & layer_state_t::eFrameRateCategoryChanged) {
329 const auto category = Layer::FrameRate::convertCategory(clientState.frameRateCategory);
330 requestedFrameRate.category = category;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000331 changes |= RequestedLayerState::Changes::FrameRate;
332 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000333}
334
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000335ui::Size RequestedLayerState::getUnrotatedBufferSize(uint32_t displayRotationFlags) const {
336 uint32_t bufferWidth = externalTexture->getWidth();
337 uint32_t bufferHeight = externalTexture->getHeight();
338 // Undo any transformations on the buffer.
339 if (bufferTransform & ui::Transform::ROT_90) {
340 std::swap(bufferWidth, bufferHeight);
341 }
342 if (transformToDisplayInverse) {
343 if (displayRotationFlags & ui::Transform::ROT_90) {
344 std::swap(bufferWidth, bufferHeight);
345 }
346 }
347 return {bufferWidth, bufferHeight};
348}
349
350ui::Transform RequestedLayerState::getTransform(uint32_t displayRotationFlags) const {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000351 if ((flags & layer_state_t::eIgnoreDestinationFrame) || destinationFrame.isEmpty()) {
352 // If destination frame is not set, use the requested transform set via
353 // Transaction::setPosition and Transaction::setMatrix.
354 return requestedTransform;
355 }
356
357 Rect destRect = destinationFrame;
358 int32_t destW = destRect.width();
359 int32_t destH = destRect.height();
360 if (destRect.left < 0) {
361 destRect.left = 0;
362 destRect.right = destW;
363 }
364 if (destRect.top < 0) {
365 destRect.top = 0;
366 destRect.bottom = destH;
367 }
368
369 if (!externalTexture) {
370 ui::Transform transform;
371 transform.set(static_cast<float>(destRect.left), static_cast<float>(destRect.top));
372 return transform;
373 }
374
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000375 ui::Size bufferSize = getUnrotatedBufferSize(displayRotationFlags);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000376
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000377 float sx = static_cast<float>(destW) / static_cast<float>(bufferSize.width);
378 float sy = static_cast<float>(destH) / static_cast<float>(bufferSize.height);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000379 ui::Transform transform;
380 transform.set(sx, 0, 0, sy);
381 transform.set(static_cast<float>(destRect.left), static_cast<float>(destRect.top));
382 return transform;
383}
384
385std::string RequestedLayerState::getDebugString() const {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000386 std::stringstream debug;
Vishnu Nair444f3952023-04-11 13:01:02 -0700387 debug << "RequestedLayerState{" << name;
388 if (parentId != UNASSIGNED_LAYER_ID) debug << " parentId=" << parentId;
389 if (relativeParentId != UNASSIGNED_LAYER_ID) debug << " relativeParentId=" << relativeParentId;
390 if (!mirrorIds.empty()) debug << " mirrorId=" << layerIdsToString(mirrorIds);
391 if (!handleAlive) debug << " !handle";
392 if (z != 0) debug << " z=" << z;
393 if (layerStack.id != 0) debug << " layerStack=" << layerStack.id;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000394 return debug.str();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000395}
396
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000397std::ostream& operator<<(std::ostream& out, const RequestedLayerState& obj) {
398 out << obj.debugName;
399 if (obj.relativeParentId != UNASSIGNED_LAYER_ID) out << " parent=" << obj.parentId;
400 if (!obj.handleAlive) out << " handleNotAlive";
401 return out;
402}
403
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000404std::string RequestedLayerState::getDebugStringShort() const {
405 return "[" + std::to_string(id) + "]" + name;
406}
407
408bool RequestedLayerState::canBeDestroyed() const {
409 return !handleAlive && parentId == UNASSIGNED_LAYER_ID;
410}
411bool RequestedLayerState::isRoot() const {
412 return canBeRoot && parentId == UNASSIGNED_LAYER_ID;
413}
414bool RequestedLayerState::isHiddenByPolicy() const {
415 return (flags & layer_state_t::eLayerHidden) == layer_state_t::eLayerHidden;
416};
417half4 RequestedLayerState::getColor() const {
Vishnu Naira02943f2023-06-03 13:44:46 -0700418 if (sidebandStream || externalTexture) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000419 return {0._hf, 0._hf, 0._hf, color.a};
420 }
421 return color;
422}
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000423Rect RequestedLayerState::getBufferSize(uint32_t displayRotationFlags) const {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000424 // for buffer state layers we use the display frame size as the buffer size.
425 if (!externalTexture) {
426 return Rect::INVALID_RECT;
427 }
428
429 uint32_t bufWidth = externalTexture->getWidth();
430 uint32_t bufHeight = externalTexture->getHeight();
431
432 // Undo any transformations on the buffer and return the result.
433 if (bufferTransform & ui::Transform::ROT_90) {
434 std::swap(bufWidth, bufHeight);
435 }
436
437 if (transformToDisplayInverse) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000438 uint32_t invTransform = displayRotationFlags;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000439 if (invTransform & ui::Transform::ROT_90) {
440 std::swap(bufWidth, bufHeight);
441 }
442 }
443
444 return Rect(0, 0, static_cast<int32_t>(bufWidth), static_cast<int32_t>(bufHeight));
445}
446
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000447Rect RequestedLayerState::getCroppedBufferSize(const Rect& bufferSize) const {
448 Rect size = bufferSize;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000449 if (!crop.isEmpty() && size.isValid()) {
450 size.intersect(crop, &size);
451 } else if (!crop.isEmpty()) {
452 size = crop;
453 }
454 return size;
455}
456
457Rect RequestedLayerState::getBufferCrop() const {
458 // this is the crop rectangle that applies to the buffer
459 // itself (as opposed to the window)
460 if (!bufferCrop.isEmpty()) {
461 // if the buffer crop is defined, we use that
462 return bufferCrop;
463 } else if (externalTexture != nullptr) {
464 // otherwise we use the whole buffer
465 return externalTexture->getBounds();
466 } else {
467 // if we don't have a buffer yet, we use an empty/invalid crop
468 return Rect();
469 }
470}
471
472aidl::android::hardware::graphics::composer3::Composition RequestedLayerState::getCompositionType()
473 const {
474 using aidl::android::hardware::graphics::composer3::Composition;
475 // TODO(b/238781169) check about sidestream ready flag
476 if (sidebandStream.get()) {
477 return Composition::SIDEBAND;
478 }
479 if (!externalTexture) {
480 return Composition::SOLID_COLOR;
481 }
482 if (flags & layer_state_t::eLayerIsDisplayDecoration) {
483 return Composition::DISPLAY_DECORATION;
484 }
485 if (potentialCursor) {
486 return Composition::CURSOR;
487 }
488 return Composition::DEVICE;
489}
490
491Rect RequestedLayerState::reduce(const Rect& win, const Region& exclude) {
492 if (CC_LIKELY(exclude.isEmpty())) {
493 return win;
494 }
495 if (exclude.isRect()) {
496 return win.reduce(exclude.getBounds());
497 }
498 return Region(win).subtract(exclude).getBounds();
499}
500
Vishnu Nair04f89692022-11-16 23:21:05 +0000501// Returns true if the layer has a relative parent that is not its own parent. This is an input
502// error from the client, and this check allows us to handle it gracefully. If both parentId and
503// relativeParentId is unassigned then the layer does not have a valid relative parent.
504// If the relative parentid is unassigned, the layer will be considered relative but won't be
505// reachable.
506bool RequestedLayerState::hasValidRelativeParent() const {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000507 return isRelativeOf &&
508 (parentId != relativeParentId || relativeParentId == UNASSIGNED_LAYER_ID);
Vishnu Nair04f89692022-11-16 23:21:05 +0000509}
510
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000511bool RequestedLayerState::hasInputInfo() const {
512 if (!windowInfoHandle) {
513 return false;
514 }
515 const auto windowInfo = windowInfoHandle->getInfo();
516 return windowInfo->token != nullptr ||
517 windowInfo->inputConfig.test(gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL);
518}
519
Vishnu Nair80a5a702023-02-11 01:21:51 +0000520bool RequestedLayerState::hasBlur() const {
521 return backgroundBlurRadius > 0 || blurRegions.size() > 0;
522}
523
Vishnu Naird47bcee2023-02-24 18:08:51 +0000524bool RequestedLayerState::hasFrameUpdate() const {
525 return what & layer_state_t::CONTENT_DIRTY &&
526 (externalTexture || bgColorLayerId != UNASSIGNED_LAYER_ID);
527}
528
529bool RequestedLayerState::hasReadyFrame() const {
530 return hasFrameUpdate() || changes.test(Changes::SidebandStream) || autoRefresh;
531}
532
533bool RequestedLayerState::hasSidebandStreamFrame() const {
534 return hasFrameUpdate() && sidebandStream.get();
535}
536
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700537bool RequestedLayerState::willReleaseBufferOnLatch() const {
538 return changes.test(Changes::Buffer) && !externalTexture;
539}
540
Vishnu Nair4d9cef92023-06-24 22:34:41 +0000541bool RequestedLayerState::backpressureEnabled() const {
542 return flags & layer_state_t::eEnableBackpressure;
543}
544
545bool RequestedLayerState::isSimpleBufferUpdate(const layer_state_t& s) const {
546 static constexpr uint64_t requiredFlags = layer_state_t::eBufferChanged;
547 if ((s.what & requiredFlags) != requiredFlags) {
548 ATRACE_FORMAT_INSTANT("%s: false [missing required flags 0x%" PRIx64 "]", __func__,
549 (s.what | requiredFlags) & ~s.what);
550 return false;
551 }
552
553 static constexpr uint64_t deniedFlags = layer_state_t::eProducerDisconnect |
554 layer_state_t::eLayerChanged | layer_state_t::eRelativeLayerChanged |
555 layer_state_t::eTransparentRegionChanged | layer_state_t::eFlagsChanged |
556 layer_state_t::eBlurRegionsChanged | layer_state_t::eLayerStackChanged |
557 layer_state_t::eAutoRefreshChanged | layer_state_t::eReparent;
558 if (s.what & deniedFlags) {
559 ATRACE_FORMAT_INSTANT("%s: false [has denied flags 0x%" PRIx64 "]", __func__,
560 s.what & deniedFlags);
561 return false;
562 }
563
564 bool changedFlags = diff(s);
565 static constexpr auto deniedChanges = layer_state_t::ePositionChanged |
566 layer_state_t::eAlphaChanged | layer_state_t::eColorTransformChanged |
567 layer_state_t::eBackgroundColorChanged | layer_state_t::eMatrixChanged |
568 layer_state_t::eCornerRadiusChanged | layer_state_t::eBackgroundBlurRadiusChanged |
569 layer_state_t::eBufferTransformChanged |
570 layer_state_t::eTransformToDisplayInverseChanged | layer_state_t::eCropChanged |
571 layer_state_t::eDataspaceChanged | layer_state_t::eHdrMetadataChanged |
572 layer_state_t::eSidebandStreamChanged | layer_state_t::eColorSpaceAgnosticChanged |
573 layer_state_t::eShadowRadiusChanged | layer_state_t::eFixedTransformHintChanged |
574 layer_state_t::eTrustedOverlayChanged | layer_state_t::eStretchChanged |
575 layer_state_t::eBufferCropChanged | layer_state_t::eDestinationFrameChanged |
576 layer_state_t::eDimmingEnabledChanged | layer_state_t::eExtendedRangeBrightnessChanged;
577 if (changedFlags & deniedChanges) {
578 ATRACE_FORMAT_INSTANT("%s: false [has denied changes flags 0x%" PRIx64 "]", __func__,
579 s.what & deniedChanges);
580 return false;
581 }
582
583 return true;
584}
585
Vishnu Nair0808ae62023-08-07 21:42:42 -0700586bool RequestedLayerState::isProtected() const {
587 return externalTexture && externalTexture->getUsage() & GRALLOC_USAGE_PROTECTED;
588}
589
Vishnu Nair854ce1c2023-08-19 15:00:13 -0700590bool RequestedLayerState::hasSomethingToDraw() const {
591 return externalTexture != nullptr || sidebandStream != nullptr || shadowRadius > 0.f ||
592 backgroundBlurRadius > 0 || blurRegions.size() > 0 ||
593 (color.r >= 0.0_hf && color.g >= 0.0_hf && color.b >= 0.0_hf);
594}
595
Vishnu Naird47bcee2023-02-24 18:08:51 +0000596void RequestedLayerState::clearChanges() {
597 what = 0;
598 changes.clear();
599}
600
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000601} // namespace android::surfaceflinger::frontend