blob: 2bf8bc9a3337e6a860adab8c9251c191f58bf14e [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;
Vishnu Nair61ff12a2023-08-30 21:41:46 -0700153 const bool oldBufferFormatOpaque = LayerSnapshot::isOpaqueFormat(
154 externalTexture ? externalTexture->getPixelFormat() : PIXEL_FORMAT_NONE);
Vishnu Nair0808ae62023-08-07 21:42:42 -0700155
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700156 const bool hadSideStream = sidebandStream != nullptr;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000157 const layer_state_t& clientState = resolvedComposerState.state;
Vishnu Nair854ce1c2023-08-19 15:00:13 -0700158 const bool hadSomethingToDraw = hasSomethingToDraw();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000159 uint64_t clientChanges = what | layer_state_t::diff(clientState);
160 layer_state_t::merge(clientState);
161 what = clientChanges;
Vishnu Naira02943f2023-06-03 13:44:46 -0700162 LLOGV(layerId, "requested=%" PRIu64 "flags=%" PRIu64, clientState.what, clientChanges);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000163
164 if (clientState.what & layer_state_t::eFlagsChanged) {
Vishnu Nair61ff12a2023-08-30 21:41:46 -0700165 if ((oldFlags ^ flags) & (layer_state_t::eLayerHidden | layer_state_t::eLayerOpaque)) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000166 changes |= RequestedLayerState::Changes::Visibility |
167 RequestedLayerState::Changes::VisibleRegion;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000168 }
169 if ((oldFlags ^ flags) & layer_state_t::eIgnoreDestinationFrame) {
170 changes |= RequestedLayerState::Changes::Geometry;
171 }
172 }
Vishnu Naird1f74982023-06-15 20:16:51 -0700173
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700174 if (clientState.what & layer_state_t::eBufferChanged) {
175 externalTexture = resolvedComposerState.externalTexture;
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700176 const bool hasBuffer = externalTexture != nullptr;
177 if (hasBuffer || hasBuffer != hadBuffer) {
178 changes |= RequestedLayerState::Changes::Buffer;
Vishnu Naira02943f2023-06-03 13:44:46 -0700179 const ui::Size newBufferSize = hasBuffer
180 ? ui::Size(externalTexture->getWidth(), externalTexture->getHeight())
181 : ui::Size();
182 if (oldBufferSize != newBufferSize) {
183 changes |= RequestedLayerState::Changes::BufferSize;
184 changes |= RequestedLayerState::Changes::Geometry;
185 }
Vishnu Nair0808ae62023-08-07 21:42:42 -0700186 const uint64_t usageFlags = hasBuffer ? externalTexture->getUsage() : 0;
187 if (oldUsageFlags != usageFlags) {
188 changes |= RequestedLayerState::Changes::BufferUsageFlags;
189 }
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700190 }
191
192 if (hasBuffer != hadBuffer) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000193 changes |= RequestedLayerState::Changes::Geometry |
194 RequestedLayerState::Changes::VisibleRegion |
Vishnu Naird47bcee2023-02-24 18:08:51 +0000195 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Input;
196 }
Vishnu Naird1f74982023-06-15 20:16:51 -0700197
198 if (hasBuffer) {
199 const bool frameNumberChanged =
200 bufferData->flags.test(BufferData::BufferDataChange::frameNumberChanged);
201 const uint64_t frameNumber =
202 frameNumberChanged ? bufferData->frameNumber : oldFramenumber + 1;
203 bufferData->frameNumber = frameNumber;
204
205 if ((barrierProducerId > bufferData->producerId) ||
206 ((barrierProducerId == bufferData->producerId) &&
207 (barrierFrameNumber > bufferData->frameNumber))) {
208 ALOGE("Out of order buffers detected for %s producedId=%d frameNumber=%" PRIu64
209 " -> producedId=%d frameNumber=%" PRIu64,
210 getDebugString().c_str(), bufferData->producerId, bufferData->frameNumber,
211 bufferData->producerId, frameNumber);
212 TransactionTraceWriter::getInstance().invoke("out_of_order_buffers_",
213 /*overwrite=*/false);
214 }
215
216 barrierProducerId = std::max(bufferData->producerId, barrierProducerId);
217 barrierFrameNumber = std::max(bufferData->frameNumber, barrierFrameNumber);
218 }
Vishnu Nair61ff12a2023-08-30 21:41:46 -0700219
220 const bool newBufferFormatOpaque = LayerSnapshot::isOpaqueFormat(
221 externalTexture ? externalTexture->getPixelFormat() : PIXEL_FORMAT_NONE);
222 if (newBufferFormatOpaque != oldBufferFormatOpaque) {
223 changes |= RequestedLayerState::Changes::Visibility |
224 RequestedLayerState::Changes::VisibleRegion;
225 }
Vishnu Nair63221212023-04-06 15:17:37 -0700226 }
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700227
Vishnu Nair63221212023-04-06 15:17:37 -0700228 if (clientState.what & layer_state_t::eSidebandStreamChanged) {
229 changes |= RequestedLayerState::Changes::SidebandStream;
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700230 const bool hasSideStream = sidebandStream != nullptr;
231 if (hasSideStream != hadSideStream) {
232 changes |= RequestedLayerState::Changes::Geometry |
233 RequestedLayerState::Changes::VisibleRegion |
234 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Input;
235 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000236 }
237 if (what & (layer_state_t::eAlphaChanged)) {
238 if (oldAlpha == 0 || color.a == 0) {
239 changes |= RequestedLayerState::Changes::Visibility |
240 RequestedLayerState::Changes::VisibleRegion;
241 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000242 }
Vishnu Nair854ce1c2023-08-19 15:00:13 -0700243
244 if (hadSomethingToDraw != hasSomethingToDraw()) {
245 changes |= RequestedLayerState::Changes::Visibility |
246 RequestedLayerState::Changes::VisibleRegion;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000247 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000248 if (clientChanges & layer_state_t::HIERARCHY_CHANGES)
249 changes |= RequestedLayerState::Changes::Hierarchy;
250 if (clientChanges & layer_state_t::CONTENT_CHANGES)
251 changes |= RequestedLayerState::Changes::Content;
252 if (clientChanges & layer_state_t::GEOMETRY_CHANGES)
253 changes |= RequestedLayerState::Changes::Geometry;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000254 if (clientChanges & layer_state_t::AFFECTS_CHILDREN)
255 changes |= RequestedLayerState::Changes::AffectsChildren;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000256 if (clientChanges & layer_state_t::INPUT_CHANGES)
257 changes |= RequestedLayerState::Changes::Input;
258 if (clientChanges & layer_state_t::VISIBLE_REGION_CHANGES)
259 changes |= RequestedLayerState::Changes::VisibleRegion;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000260 if (clientState.what & layer_state_t::eColorTransformChanged) {
261 static const mat4 identityMatrix = mat4();
262 hasColorTransform = colorTransform != identityMatrix;
263 }
Vishnu Naird47bcee2023-02-24 18:08:51 +0000264 if (clientState.what &
265 (layer_state_t::eLayerChanged | layer_state_t::eRelativeLayerChanged |
266 layer_state_t::eLayerStackChanged)) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000267 changes |= RequestedLayerState::Changes::Z;
268 }
269 if (clientState.what & layer_state_t::eReparent) {
270 changes |= RequestedLayerState::Changes::Parent;
Vishnu Nair1391de22023-03-05 19:56:14 -0800271 parentId = resolvedComposerState.parentId;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000272 parentSurfaceControlForChild = nullptr;
Vishnu Nair04f89692022-11-16 23:21:05 +0000273 // Once a layer has be reparented, it cannot be placed at the root. It sounds odd
274 // but thats the existing logic and until we make this behavior more explicit, we need
275 // to maintain this logic.
276 canBeRoot = false;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000277 }
278 if (clientState.what & layer_state_t::eRelativeLayerChanged) {
279 changes |= RequestedLayerState::Changes::RelativeParent;
Vishnu Nair1391de22023-03-05 19:56:14 -0800280 relativeParentId = resolvedComposerState.relativeParentId;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000281 isRelativeOf = true;
282 relativeLayerSurfaceControl = nullptr;
283 }
284 if ((clientState.what & layer_state_t::eLayerChanged ||
285 (clientState.what & layer_state_t::eReparent && parentId == UNASSIGNED_LAYER_ID)) &&
286 isRelativeOf) {
287 // clear out relz data
288 relativeParentId = UNASSIGNED_LAYER_ID;
289 isRelativeOf = false;
290 changes |= RequestedLayerState::Changes::RelativeParent;
291 }
292 if (clientState.what & layer_state_t::eReparent && parentId == relativeParentId) {
293 // provide a hint that we are are now a direct child and not a relative child.
294 changes |= RequestedLayerState::Changes::RelativeParent;
295 }
296 if (clientState.what & layer_state_t::eInputInfoChanged) {
Vishnu Nair1391de22023-03-05 19:56:14 -0800297 touchCropId = resolvedComposerState.touchCropId;
298 windowInfoHandle->editInfo()->touchableRegionCropHandle.clear();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000299 }
300 if (clientState.what & layer_state_t::eStretchChanged) {
301 stretchEffect.sanitize();
302 }
303
304 if (clientState.what & layer_state_t::eHasListenerCallbacksChanged) {
305 // TODO(b/238781169) handle callbacks
306 }
307
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000308 if (clientState.what & layer_state_t::ePositionChanged) {
309 requestedTransform.set(x, y);
310 }
311
312 if (clientState.what & layer_state_t::eMatrixChanged) {
313 requestedTransform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
314 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000315 if (clientState.what & layer_state_t::eMetadataChanged) {
316 const int32_t requestedGameMode =
317 clientState.metadata.getInt32(gui::METADATA_GAME_MODE, -1);
318 if (requestedGameMode != -1) {
319 // The transaction will be received on the Task layer and needs to be applied to all
320 // child layers.
321 if (static_cast<int32_t>(gameMode) != requestedGameMode) {
322 gameMode = static_cast<gui::GameMode>(requestedGameMode);
Vishnu Naira02943f2023-06-03 13:44:46 -0700323 changes |= RequestedLayerState::Changes::GameMode;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000324 }
325 }
326 }
327 if (clientState.what & layer_state_t::eFrameRateChanged) {
328 const auto compatibility =
329 Layer::FrameRate::convertCompatibility(clientState.frameRateCompatibility);
330 const auto strategy = Layer::FrameRate::convertChangeFrameRateStrategy(
331 clientState.changeFrameRateStrategy);
Rachel Leece6e0042023-06-27 11:22:54 -0700332 requestedFrameRate.vote =
333 Layer::FrameRate::FrameRateVote(Fps::fromValue(clientState.frameRate),
334 compatibility, strategy);
335 changes |= RequestedLayerState::Changes::FrameRate;
336 }
337 if (clientState.what & layer_state_t::eFrameRateCategoryChanged) {
338 const auto category = Layer::FrameRate::convertCategory(clientState.frameRateCategory);
339 requestedFrameRate.category = category;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000340 changes |= RequestedLayerState::Changes::FrameRate;
341 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000342}
343
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000344ui::Size RequestedLayerState::getUnrotatedBufferSize(uint32_t displayRotationFlags) const {
345 uint32_t bufferWidth = externalTexture->getWidth();
346 uint32_t bufferHeight = externalTexture->getHeight();
347 // Undo any transformations on the buffer.
348 if (bufferTransform & ui::Transform::ROT_90) {
349 std::swap(bufferWidth, bufferHeight);
350 }
351 if (transformToDisplayInverse) {
352 if (displayRotationFlags & ui::Transform::ROT_90) {
353 std::swap(bufferWidth, bufferHeight);
354 }
355 }
356 return {bufferWidth, bufferHeight};
357}
358
359ui::Transform RequestedLayerState::getTransform(uint32_t displayRotationFlags) const {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000360 if ((flags & layer_state_t::eIgnoreDestinationFrame) || destinationFrame.isEmpty()) {
361 // If destination frame is not set, use the requested transform set via
362 // Transaction::setPosition and Transaction::setMatrix.
363 return requestedTransform;
364 }
365
366 Rect destRect = destinationFrame;
367 int32_t destW = destRect.width();
368 int32_t destH = destRect.height();
369 if (destRect.left < 0) {
370 destRect.left = 0;
371 destRect.right = destW;
372 }
373 if (destRect.top < 0) {
374 destRect.top = 0;
375 destRect.bottom = destH;
376 }
377
378 if (!externalTexture) {
379 ui::Transform transform;
380 transform.set(static_cast<float>(destRect.left), static_cast<float>(destRect.top));
381 return transform;
382 }
383
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000384 ui::Size bufferSize = getUnrotatedBufferSize(displayRotationFlags);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000385
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000386 float sx = static_cast<float>(destW) / static_cast<float>(bufferSize.width);
387 float sy = static_cast<float>(destH) / static_cast<float>(bufferSize.height);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000388 ui::Transform transform;
389 transform.set(sx, 0, 0, sy);
390 transform.set(static_cast<float>(destRect.left), static_cast<float>(destRect.top));
391 return transform;
392}
393
394std::string RequestedLayerState::getDebugString() const {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000395 std::stringstream debug;
Vishnu Nair444f3952023-04-11 13:01:02 -0700396 debug << "RequestedLayerState{" << name;
397 if (parentId != UNASSIGNED_LAYER_ID) debug << " parentId=" << parentId;
398 if (relativeParentId != UNASSIGNED_LAYER_ID) debug << " relativeParentId=" << relativeParentId;
399 if (!mirrorIds.empty()) debug << " mirrorId=" << layerIdsToString(mirrorIds);
400 if (!handleAlive) debug << " !handle";
401 if (z != 0) debug << " z=" << z;
402 if (layerStack.id != 0) debug << " layerStack=" << layerStack.id;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000403 return debug.str();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000404}
405
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000406std::ostream& operator<<(std::ostream& out, const RequestedLayerState& obj) {
407 out << obj.debugName;
408 if (obj.relativeParentId != UNASSIGNED_LAYER_ID) out << " parent=" << obj.parentId;
409 if (!obj.handleAlive) out << " handleNotAlive";
410 return out;
411}
412
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000413std::string RequestedLayerState::getDebugStringShort() const {
414 return "[" + std::to_string(id) + "]" + name;
415}
416
417bool RequestedLayerState::canBeDestroyed() const {
418 return !handleAlive && parentId == UNASSIGNED_LAYER_ID;
419}
420bool RequestedLayerState::isRoot() const {
421 return canBeRoot && parentId == UNASSIGNED_LAYER_ID;
422}
423bool RequestedLayerState::isHiddenByPolicy() const {
424 return (flags & layer_state_t::eLayerHidden) == layer_state_t::eLayerHidden;
425};
426half4 RequestedLayerState::getColor() const {
Vishnu Naira02943f2023-06-03 13:44:46 -0700427 if (sidebandStream || externalTexture) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000428 return {0._hf, 0._hf, 0._hf, color.a};
429 }
430 return color;
431}
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000432Rect RequestedLayerState::getBufferSize(uint32_t displayRotationFlags) const {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000433 // for buffer state layers we use the display frame size as the buffer size.
434 if (!externalTexture) {
435 return Rect::INVALID_RECT;
436 }
437
438 uint32_t bufWidth = externalTexture->getWidth();
439 uint32_t bufHeight = externalTexture->getHeight();
440
441 // Undo any transformations on the buffer and return the result.
442 if (bufferTransform & ui::Transform::ROT_90) {
443 std::swap(bufWidth, bufHeight);
444 }
445
446 if (transformToDisplayInverse) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000447 uint32_t invTransform = displayRotationFlags;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000448 if (invTransform & ui::Transform::ROT_90) {
449 std::swap(bufWidth, bufHeight);
450 }
451 }
452
453 return Rect(0, 0, static_cast<int32_t>(bufWidth), static_cast<int32_t>(bufHeight));
454}
455
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000456Rect RequestedLayerState::getCroppedBufferSize(const Rect& bufferSize) const {
457 Rect size = bufferSize;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000458 if (!crop.isEmpty() && size.isValid()) {
459 size.intersect(crop, &size);
460 } else if (!crop.isEmpty()) {
461 size = crop;
462 }
463 return size;
464}
465
466Rect RequestedLayerState::getBufferCrop() const {
467 // this is the crop rectangle that applies to the buffer
468 // itself (as opposed to the window)
Chavi Weingarten07597342023-09-14 21:10:59 +0000469 if (!bufferCrop.isEmpty() && externalTexture != nullptr) {
470 // if the buffer crop is defined and there's a valid buffer, intersect buffer size and crop
471 // since the crop should never exceed the size of the buffer.
472 Rect sizeAndCrop;
473 externalTexture->getBounds().intersect(bufferCrop, &sizeAndCrop);
474 return sizeAndCrop;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000475 } else if (externalTexture != nullptr) {
476 // otherwise we use the whole buffer
477 return externalTexture->getBounds();
Chavi Weingarten07597342023-09-14 21:10:59 +0000478 } else if (!bufferCrop.isEmpty()) {
479 // if the buffer crop is defined, we use that
480 return bufferCrop;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000481 } else {
482 // if we don't have a buffer yet, we use an empty/invalid crop
483 return Rect();
484 }
485}
486
487aidl::android::hardware::graphics::composer3::Composition RequestedLayerState::getCompositionType()
488 const {
489 using aidl::android::hardware::graphics::composer3::Composition;
490 // TODO(b/238781169) check about sidestream ready flag
491 if (sidebandStream.get()) {
492 return Composition::SIDEBAND;
493 }
494 if (!externalTexture) {
495 return Composition::SOLID_COLOR;
496 }
497 if (flags & layer_state_t::eLayerIsDisplayDecoration) {
498 return Composition::DISPLAY_DECORATION;
499 }
Vishnu Nairbd51f952023-08-31 22:50:14 -0700500 if (flags & layer_state_t::eLayerIsRefreshRateIndicator) {
501 return Composition::REFRESH_RATE_INDICATOR;
502 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000503 if (potentialCursor) {
504 return Composition::CURSOR;
505 }
506 return Composition::DEVICE;
507}
508
509Rect RequestedLayerState::reduce(const Rect& win, const Region& exclude) {
510 if (CC_LIKELY(exclude.isEmpty())) {
511 return win;
512 }
513 if (exclude.isRect()) {
514 return win.reduce(exclude.getBounds());
515 }
516 return Region(win).subtract(exclude).getBounds();
517}
518
Vishnu Nair04f89692022-11-16 23:21:05 +0000519// Returns true if the layer has a relative parent that is not its own parent. This is an input
520// error from the client, and this check allows us to handle it gracefully. If both parentId and
521// relativeParentId is unassigned then the layer does not have a valid relative parent.
522// If the relative parentid is unassigned, the layer will be considered relative but won't be
523// reachable.
524bool RequestedLayerState::hasValidRelativeParent() const {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000525 return isRelativeOf &&
526 (parentId != relativeParentId || relativeParentId == UNASSIGNED_LAYER_ID);
Vishnu Nair04f89692022-11-16 23:21:05 +0000527}
528
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000529bool RequestedLayerState::hasInputInfo() const {
530 if (!windowInfoHandle) {
531 return false;
532 }
533 const auto windowInfo = windowInfoHandle->getInfo();
534 return windowInfo->token != nullptr ||
535 windowInfo->inputConfig.test(gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL);
536}
537
Vishnu Nair80a5a702023-02-11 01:21:51 +0000538bool RequestedLayerState::hasBlur() const {
539 return backgroundBlurRadius > 0 || blurRegions.size() > 0;
540}
541
Vishnu Naird47bcee2023-02-24 18:08:51 +0000542bool RequestedLayerState::hasFrameUpdate() const {
543 return what & layer_state_t::CONTENT_DIRTY &&
544 (externalTexture || bgColorLayerId != UNASSIGNED_LAYER_ID);
545}
546
547bool RequestedLayerState::hasReadyFrame() const {
548 return hasFrameUpdate() || changes.test(Changes::SidebandStream) || autoRefresh;
549}
550
551bool RequestedLayerState::hasSidebandStreamFrame() const {
552 return hasFrameUpdate() && sidebandStream.get();
553}
554
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700555bool RequestedLayerState::willReleaseBufferOnLatch() const {
556 return changes.test(Changes::Buffer) && !externalTexture;
557}
558
Vishnu Nair4d9cef92023-06-24 22:34:41 +0000559bool RequestedLayerState::backpressureEnabled() const {
560 return flags & layer_state_t::eEnableBackpressure;
561}
562
563bool RequestedLayerState::isSimpleBufferUpdate(const layer_state_t& s) const {
564 static constexpr uint64_t requiredFlags = layer_state_t::eBufferChanged;
565 if ((s.what & requiredFlags) != requiredFlags) {
566 ATRACE_FORMAT_INSTANT("%s: false [missing required flags 0x%" PRIx64 "]", __func__,
567 (s.what | requiredFlags) & ~s.what);
568 return false;
569 }
570
571 static constexpr uint64_t deniedFlags = layer_state_t::eProducerDisconnect |
572 layer_state_t::eLayerChanged | layer_state_t::eRelativeLayerChanged |
573 layer_state_t::eTransparentRegionChanged | layer_state_t::eFlagsChanged |
574 layer_state_t::eBlurRegionsChanged | layer_state_t::eLayerStackChanged |
575 layer_state_t::eAutoRefreshChanged | layer_state_t::eReparent;
576 if (s.what & deniedFlags) {
577 ATRACE_FORMAT_INSTANT("%s: false [has denied flags 0x%" PRIx64 "]", __func__,
578 s.what & deniedFlags);
579 return false;
580 }
581
582 bool changedFlags = diff(s);
583 static constexpr auto deniedChanges = layer_state_t::ePositionChanged |
584 layer_state_t::eAlphaChanged | layer_state_t::eColorTransformChanged |
585 layer_state_t::eBackgroundColorChanged | layer_state_t::eMatrixChanged |
586 layer_state_t::eCornerRadiusChanged | layer_state_t::eBackgroundBlurRadiusChanged |
587 layer_state_t::eBufferTransformChanged |
588 layer_state_t::eTransformToDisplayInverseChanged | layer_state_t::eCropChanged |
589 layer_state_t::eDataspaceChanged | layer_state_t::eHdrMetadataChanged |
590 layer_state_t::eSidebandStreamChanged | layer_state_t::eColorSpaceAgnosticChanged |
591 layer_state_t::eShadowRadiusChanged | layer_state_t::eFixedTransformHintChanged |
592 layer_state_t::eTrustedOverlayChanged | layer_state_t::eStretchChanged |
593 layer_state_t::eBufferCropChanged | layer_state_t::eDestinationFrameChanged |
594 layer_state_t::eDimmingEnabledChanged | layer_state_t::eExtendedRangeBrightnessChanged;
595 if (changedFlags & deniedChanges) {
596 ATRACE_FORMAT_INSTANT("%s: false [has denied changes flags 0x%" PRIx64 "]", __func__,
597 s.what & deniedChanges);
598 return false;
599 }
600
601 return true;
602}
603
Vishnu Nair0808ae62023-08-07 21:42:42 -0700604bool RequestedLayerState::isProtected() const {
605 return externalTexture && externalTexture->getUsage() & GRALLOC_USAGE_PROTECTED;
606}
607
Vishnu Nair854ce1c2023-08-19 15:00:13 -0700608bool RequestedLayerState::hasSomethingToDraw() const {
609 return externalTexture != nullptr || sidebandStream != nullptr || shadowRadius > 0.f ||
610 backgroundBlurRadius > 0 || blurRegions.size() > 0 ||
611 (color.r >= 0.0_hf && color.g >= 0.0_hf && color.b >= 0.0_hf);
612}
613
Vishnu Naird47bcee2023-02-24 18:08:51 +0000614void RequestedLayerState::clearChanges() {
615 what = 0;
616 changes.clear();
617}
618
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000619} // namespace android::surfaceflinger::frontend