blob: d979c4662e23d11f1fd2662722144fe551d1a4fc [file] [log] [blame]
Vishnu Nairdc4d31b2022-11-17 03:20:58 +00001/*
2 * Copyright 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Vishnu Naira02943f2023-06-03 13:44:46 -070016// #define LOG_NDEBUG 0
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000017
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
19#undef LOG_TAG
Vishnu Naira02943f2023-06-03 13:44:46 -070020#define LOG_TAG "SurfaceFlinger"
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000021
Vishnu Nair4d9cef92023-06-24 22:34:41 +000022#include <gui/TraceUtils.h>
Vishnu Naircfb2d252023-01-19 04:44:02 +000023#include <log/log.h>
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000024#include <private/android_filesystem_config.h>
25#include <sys/types.h>
26
27#include "Layer.h"
Vishnu Naircfb2d252023-01-19 04:44:02 +000028#include "LayerCreationArgs.h"
Vishnu Naircfb2d252023-01-19 04:44:02 +000029#include "LayerLog.h"
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000030#include "RequestedLayerState.h"
31
32namespace android::surfaceflinger::frontend {
33using ftl::Flags;
34using namespace ftl::flag_operators;
35
36namespace {
Vishnu Naira9c43762023-01-27 19:10:25 +000037std::string layerIdsToString(const std::vector<uint32_t>& layerIds) {
38 std::stringstream stream;
39 stream << "{";
40 for (auto layerId : layerIds) {
41 stream << layerId << ",";
42 }
43 stream << "}";
44 return stream.str();
45}
46
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000047} // namespace
48
49RequestedLayerState::RequestedLayerState(const LayerCreationArgs& args)
50 : id(args.sequence),
Vishnu Naircfb2d252023-01-19 04:44:02 +000051 name(args.name + "#" + std::to_string(args.sequence)),
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000052 canBeRoot(args.addToRoot),
53 layerCreationFlags(args.flags),
54 textureName(args.textureName),
55 ownerUid(args.ownerUid),
Vishnu Nair1391de22023-03-05 19:56:14 -080056 ownerPid(args.ownerPid),
57 parentId(args.parentId),
58 layerIdToMirror(args.layerIdToMirror) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000059 layerId = static_cast<int32_t>(args.sequence);
60 changes |= RequestedLayerState::Changes::Created;
61 metadata.merge(args.metadata);
62 changes |= RequestedLayerState::Changes::Metadata;
63 handleAlive = true;
Vishnu Nair1391de22023-03-05 19:56:14 -080064 if (parentId != UNASSIGNED_LAYER_ID) {
Vishnu Naircfb2d252023-01-19 04:44:02 +000065 canBeRoot = false;
66 }
Vishnu Naira9c43762023-01-27 19:10:25 +000067 if (layerIdToMirror != UNASSIGNED_LAYER_ID) {
68 changes |= RequestedLayerState::Changes::Mirror;
69 } else if (args.layerStackToMirror != ui::INVALID_LAYER_STACK) {
70 layerStackToMirror = args.layerStackToMirror;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000071 changes |= RequestedLayerState::Changes::Mirror;
72 }
73
74 flags = 0;
75 if (args.flags & ISurfaceComposerClient::eHidden) flags |= layer_state_t::eLayerHidden;
76 if (args.flags & ISurfaceComposerClient::eOpaque) flags |= layer_state_t::eLayerOpaque;
77 if (args.flags & ISurfaceComposerClient::eSecure) flags |= layer_state_t::eLayerSecure;
78 if (args.flags & ISurfaceComposerClient::eSkipScreenshot) {
79 flags |= layer_state_t::eLayerSkipScreenshot;
80 }
81 premultipliedAlpha = !(args.flags & ISurfaceComposerClient::eNonPremultiplied);
82 potentialCursor = args.flags & ISurfaceComposerClient::eCursorWindow;
83 protectedByApp = args.flags & ISurfaceComposerClient::eProtectedByApp;
84 if (args.flags & ISurfaceComposerClient::eNoColorFill) {
85 // Set an invalid color so there is no color fill.
86 // (b/259981098) use an explicit flag instead of relying on invalid values.
87 color.r = -1.0_hf;
88 color.g = -1.0_hf;
89 color.b = -1.0_hf;
90 } else {
91 color.rgb = {0.0_hf, 0.0_hf, 0.0_hf};
92 }
Vishnu Naircfb2d252023-01-19 04:44:02 +000093 LLOGV(layerId, "Created %s flags=%d", getDebugString().c_str(), flags);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000094 color.a = 1.0f;
95
96 crop.makeInvalid();
97 z = 0;
98 layerStack = ui::DEFAULT_LAYER_STACK;
99 transformToDisplayInverse = false;
100 dataspace = ui::Dataspace::UNKNOWN;
Sally Qi963049b2023-03-23 14:06:21 -0700101 desiredHdrSdrRatio = 1.f;
102 currentHdrSdrRatio = 1.f;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000103 dataspaceRequested = false;
104 hdrMetadata.validTypes = 0;
105 surfaceDamageRegion = Region::INVALID_REGION;
106 cornerRadius = 0.0f;
107 backgroundBlurRadius = 0;
108 api = -1;
109 hasColorTransform = false;
110 bufferTransform = 0;
111 requestedTransform.reset();
112 bufferData = std::make_shared<BufferData>();
113 bufferData->frameNumber = 0;
114 bufferData->acquireFence = sp<Fence>::make(-1);
115 acquireFenceTime = std::make_shared<FenceTime>(bufferData->acquireFence);
116 colorSpaceAgnostic = false;
117 frameRateSelectionPriority = Layer::PRIORITY_UNSET;
118 shadowRadius = 0.f;
119 fixedTransformHint = ui::Transform::ROT_INVALID;
120 destinationFrame.makeInvalid();
121 isTrustedOverlay = false;
122 dropInputMode = gui::DropInputMode::NONE;
123 dimmingEnabled = true;
124 defaultFrameRateCompatibility =
125 static_cast<int8_t>(scheduler::LayerInfo::FrameRateCompatibility::Default);
126 dataspace = ui::Dataspace::V0_SRGB;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000127 gameMode = gui::GameMode::Unsupported;
128 requestedFrameRate = {};
Alec Mourif4af03e2023-02-11 00:25:24 +0000129 cachingHint = gui::CachingHint::Enabled;
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000130
131 if (name.length() > 77) {
132 std::string shortened;
133 shortened.append(name, 0, 36);
134 shortened.append("[...]");
135 shortened.append(name, name.length() - 36);
136 debugName = std::move(shortened);
137 } else {
138 debugName = name;
139 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000140}
141
142void RequestedLayerState::merge(const ResolvedComposerState& resolvedComposerState) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000143 const uint32_t oldFlags = flags;
144 const half oldAlpha = color.a;
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700145 const bool hadBuffer = externalTexture != nullptr;
Vishnu Naird1f74982023-06-15 20:16:51 -0700146 uint64_t oldFramenumber = hadBuffer ? bufferData->frameNumber : 0;
Vishnu Naira02943f2023-06-03 13:44:46 -0700147 const ui::Size oldBufferSize = hadBuffer
148 ? ui::Size(externalTexture->getWidth(), externalTexture->getHeight())
149 : ui::Size();
Vishnu Nair0808ae62023-08-07 21:42:42 -0700150 const uint64_t oldUsageFlags = hadBuffer ? externalTexture->getUsage() : 0;
151
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700152 const bool hadSideStream = sidebandStream != nullptr;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000153 const layer_state_t& clientState = resolvedComposerState.state;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000154 const bool hadBlur = hasBlur();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000155 uint64_t clientChanges = what | layer_state_t::diff(clientState);
156 layer_state_t::merge(clientState);
157 what = clientChanges;
Vishnu Naira02943f2023-06-03 13:44:46 -0700158 LLOGV(layerId, "requested=%" PRIu64 "flags=%" PRIu64, clientState.what, clientChanges);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000159
160 if (clientState.what & layer_state_t::eFlagsChanged) {
161 if ((oldFlags ^ flags) & layer_state_t::eLayerHidden) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000162 changes |= RequestedLayerState::Changes::Visibility |
163 RequestedLayerState::Changes::VisibleRegion;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000164 }
165 if ((oldFlags ^ flags) & layer_state_t::eIgnoreDestinationFrame) {
166 changes |= RequestedLayerState::Changes::Geometry;
167 }
168 }
Vishnu Naird1f74982023-06-15 20:16:51 -0700169
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700170 if (clientState.what & layer_state_t::eBufferChanged) {
171 externalTexture = resolvedComposerState.externalTexture;
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700172 const bool hasBuffer = externalTexture != nullptr;
173 if (hasBuffer || hasBuffer != hadBuffer) {
174 changes |= RequestedLayerState::Changes::Buffer;
Vishnu Naira02943f2023-06-03 13:44:46 -0700175 const ui::Size newBufferSize = hasBuffer
176 ? ui::Size(externalTexture->getWidth(), externalTexture->getHeight())
177 : ui::Size();
178 if (oldBufferSize != newBufferSize) {
179 changes |= RequestedLayerState::Changes::BufferSize;
180 changes |= RequestedLayerState::Changes::Geometry;
181 }
Vishnu Nair0808ae62023-08-07 21:42:42 -0700182 const uint64_t usageFlags = hasBuffer ? externalTexture->getUsage() : 0;
183 if (oldUsageFlags != usageFlags) {
184 changes |= RequestedLayerState::Changes::BufferUsageFlags;
185 }
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700186 }
187
188 if (hasBuffer != hadBuffer) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000189 changes |= RequestedLayerState::Changes::Geometry |
190 RequestedLayerState::Changes::VisibleRegion |
Vishnu Naird47bcee2023-02-24 18:08:51 +0000191 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Input;
192 }
Vishnu Naird1f74982023-06-15 20:16:51 -0700193
194 if (hasBuffer) {
195 const bool frameNumberChanged =
196 bufferData->flags.test(BufferData::BufferDataChange::frameNumberChanged);
197 const uint64_t frameNumber =
198 frameNumberChanged ? bufferData->frameNumber : oldFramenumber + 1;
199 bufferData->frameNumber = frameNumber;
200
201 if ((barrierProducerId > bufferData->producerId) ||
202 ((barrierProducerId == bufferData->producerId) &&
203 (barrierFrameNumber > bufferData->frameNumber))) {
204 ALOGE("Out of order buffers detected for %s producedId=%d frameNumber=%" PRIu64
205 " -> producedId=%d frameNumber=%" PRIu64,
206 getDebugString().c_str(), bufferData->producerId, bufferData->frameNumber,
207 bufferData->producerId, frameNumber);
208 TransactionTraceWriter::getInstance().invoke("out_of_order_buffers_",
209 /*overwrite=*/false);
210 }
211
212 barrierProducerId = std::max(bufferData->producerId, barrierProducerId);
213 barrierFrameNumber = std::max(bufferData->frameNumber, barrierFrameNumber);
214 }
Vishnu Nair63221212023-04-06 15:17:37 -0700215 }
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700216
Vishnu Nair63221212023-04-06 15:17:37 -0700217 if (clientState.what & layer_state_t::eSidebandStreamChanged) {
218 changes |= RequestedLayerState::Changes::SidebandStream;
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700219 const bool hasSideStream = sidebandStream != nullptr;
220 if (hasSideStream != hadSideStream) {
221 changes |= RequestedLayerState::Changes::Geometry |
222 RequestedLayerState::Changes::VisibleRegion |
223 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Input;
224 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000225 }
226 if (what & (layer_state_t::eAlphaChanged)) {
227 if (oldAlpha == 0 || color.a == 0) {
228 changes |= RequestedLayerState::Changes::Visibility |
229 RequestedLayerState::Changes::VisibleRegion;
230 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000231 }
Vishnu Nair80a5a702023-02-11 01:21:51 +0000232 if (what & (layer_state_t::eBackgroundBlurRadiusChanged | layer_state_t::eBlurRegionsChanged)) {
233 if (hadBlur != hasBlur()) {
234 changes |= RequestedLayerState::Changes::Visibility |
235 RequestedLayerState::Changes::VisibleRegion;
236 }
237 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000238 if (clientChanges & layer_state_t::HIERARCHY_CHANGES)
239 changes |= RequestedLayerState::Changes::Hierarchy;
240 if (clientChanges & layer_state_t::CONTENT_CHANGES)
241 changes |= RequestedLayerState::Changes::Content;
242 if (clientChanges & layer_state_t::GEOMETRY_CHANGES)
243 changes |= RequestedLayerState::Changes::Geometry;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000244 if (clientChanges & layer_state_t::AFFECTS_CHILDREN)
245 changes |= RequestedLayerState::Changes::AffectsChildren;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000246 if (clientChanges & layer_state_t::INPUT_CHANGES)
247 changes |= RequestedLayerState::Changes::Input;
248 if (clientChanges & layer_state_t::VISIBLE_REGION_CHANGES)
249 changes |= RequestedLayerState::Changes::VisibleRegion;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000250 if (clientState.what & layer_state_t::eColorTransformChanged) {
251 static const mat4 identityMatrix = mat4();
252 hasColorTransform = colorTransform != identityMatrix;
253 }
Vishnu Naird47bcee2023-02-24 18:08:51 +0000254 if (clientState.what &
255 (layer_state_t::eLayerChanged | layer_state_t::eRelativeLayerChanged |
256 layer_state_t::eLayerStackChanged)) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000257 changes |= RequestedLayerState::Changes::Z;
258 }
259 if (clientState.what & layer_state_t::eReparent) {
260 changes |= RequestedLayerState::Changes::Parent;
Vishnu Nair1391de22023-03-05 19:56:14 -0800261 parentId = resolvedComposerState.parentId;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000262 parentSurfaceControlForChild = nullptr;
Vishnu Nair04f89692022-11-16 23:21:05 +0000263 // Once a layer has be reparented, it cannot be placed at the root. It sounds odd
264 // but thats the existing logic and until we make this behavior more explicit, we need
265 // to maintain this logic.
266 canBeRoot = false;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000267 }
268 if (clientState.what & layer_state_t::eRelativeLayerChanged) {
269 changes |= RequestedLayerState::Changes::RelativeParent;
Vishnu Nair1391de22023-03-05 19:56:14 -0800270 relativeParentId = resolvedComposerState.relativeParentId;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000271 isRelativeOf = true;
272 relativeLayerSurfaceControl = nullptr;
273 }
274 if ((clientState.what & layer_state_t::eLayerChanged ||
275 (clientState.what & layer_state_t::eReparent && parentId == UNASSIGNED_LAYER_ID)) &&
276 isRelativeOf) {
277 // clear out relz data
278 relativeParentId = UNASSIGNED_LAYER_ID;
279 isRelativeOf = false;
280 changes |= RequestedLayerState::Changes::RelativeParent;
281 }
282 if (clientState.what & layer_state_t::eReparent && parentId == relativeParentId) {
283 // provide a hint that we are are now a direct child and not a relative child.
284 changes |= RequestedLayerState::Changes::RelativeParent;
285 }
286 if (clientState.what & layer_state_t::eInputInfoChanged) {
Vishnu Nair1391de22023-03-05 19:56:14 -0800287 touchCropId = resolvedComposerState.touchCropId;
288 windowInfoHandle->editInfo()->touchableRegionCropHandle.clear();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000289 }
290 if (clientState.what & layer_state_t::eStretchChanged) {
291 stretchEffect.sanitize();
292 }
293
294 if (clientState.what & layer_state_t::eHasListenerCallbacksChanged) {
295 // TODO(b/238781169) handle callbacks
296 }
297
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000298 if (clientState.what & layer_state_t::ePositionChanged) {
299 requestedTransform.set(x, y);
300 }
301
302 if (clientState.what & layer_state_t::eMatrixChanged) {
303 requestedTransform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
304 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000305 if (clientState.what & layer_state_t::eMetadataChanged) {
306 const int32_t requestedGameMode =
307 clientState.metadata.getInt32(gui::METADATA_GAME_MODE, -1);
308 if (requestedGameMode != -1) {
309 // The transaction will be received on the Task layer and needs to be applied to all
310 // child layers.
311 if (static_cast<int32_t>(gameMode) != requestedGameMode) {
312 gameMode = static_cast<gui::GameMode>(requestedGameMode);
Vishnu Naira02943f2023-06-03 13:44:46 -0700313 changes |= RequestedLayerState::Changes::GameMode;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000314 }
315 }
316 }
317 if (clientState.what & layer_state_t::eFrameRateChanged) {
318 const auto compatibility =
319 Layer::FrameRate::convertCompatibility(clientState.frameRateCompatibility);
320 const auto strategy = Layer::FrameRate::convertChangeFrameRateStrategy(
321 clientState.changeFrameRateStrategy);
322 requestedFrameRate =
323 Layer::FrameRate(Fps::fromValue(clientState.frameRate), compatibility, strategy);
324 changes |= RequestedLayerState::Changes::FrameRate;
325 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000326}
327
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000328ui::Size RequestedLayerState::getUnrotatedBufferSize(uint32_t displayRotationFlags) const {
329 uint32_t bufferWidth = externalTexture->getWidth();
330 uint32_t bufferHeight = externalTexture->getHeight();
331 // Undo any transformations on the buffer.
332 if (bufferTransform & ui::Transform::ROT_90) {
333 std::swap(bufferWidth, bufferHeight);
334 }
335 if (transformToDisplayInverse) {
336 if (displayRotationFlags & ui::Transform::ROT_90) {
337 std::swap(bufferWidth, bufferHeight);
338 }
339 }
340 return {bufferWidth, bufferHeight};
341}
342
343ui::Transform RequestedLayerState::getTransform(uint32_t displayRotationFlags) const {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000344 if ((flags & layer_state_t::eIgnoreDestinationFrame) || destinationFrame.isEmpty()) {
345 // If destination frame is not set, use the requested transform set via
346 // Transaction::setPosition and Transaction::setMatrix.
347 return requestedTransform;
348 }
349
350 Rect destRect = destinationFrame;
351 int32_t destW = destRect.width();
352 int32_t destH = destRect.height();
353 if (destRect.left < 0) {
354 destRect.left = 0;
355 destRect.right = destW;
356 }
357 if (destRect.top < 0) {
358 destRect.top = 0;
359 destRect.bottom = destH;
360 }
361
362 if (!externalTexture) {
363 ui::Transform transform;
364 transform.set(static_cast<float>(destRect.left), static_cast<float>(destRect.top));
365 return transform;
366 }
367
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000368 ui::Size bufferSize = getUnrotatedBufferSize(displayRotationFlags);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000369
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000370 float sx = static_cast<float>(destW) / static_cast<float>(bufferSize.width);
371 float sy = static_cast<float>(destH) / static_cast<float>(bufferSize.height);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000372 ui::Transform transform;
373 transform.set(sx, 0, 0, sy);
374 transform.set(static_cast<float>(destRect.left), static_cast<float>(destRect.top));
375 return transform;
376}
377
378std::string RequestedLayerState::getDebugString() const {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000379 std::stringstream debug;
Vishnu Nair444f3952023-04-11 13:01:02 -0700380 debug << "RequestedLayerState{" << name;
381 if (parentId != UNASSIGNED_LAYER_ID) debug << " parentId=" << parentId;
382 if (relativeParentId != UNASSIGNED_LAYER_ID) debug << " relativeParentId=" << relativeParentId;
383 if (!mirrorIds.empty()) debug << " mirrorId=" << layerIdsToString(mirrorIds);
384 if (!handleAlive) debug << " !handle";
385 if (z != 0) debug << " z=" << z;
386 if (layerStack.id != 0) debug << " layerStack=" << layerStack.id;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000387 return debug.str();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000388}
389
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000390std::ostream& operator<<(std::ostream& out, const RequestedLayerState& obj) {
391 out << obj.debugName;
392 if (obj.relativeParentId != UNASSIGNED_LAYER_ID) out << " parent=" << obj.parentId;
393 if (!obj.handleAlive) out << " handleNotAlive";
394 return out;
395}
396
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000397std::string RequestedLayerState::getDebugStringShort() const {
398 return "[" + std::to_string(id) + "]" + name;
399}
400
401bool RequestedLayerState::canBeDestroyed() const {
402 return !handleAlive && parentId == UNASSIGNED_LAYER_ID;
403}
404bool RequestedLayerState::isRoot() const {
405 return canBeRoot && parentId == UNASSIGNED_LAYER_ID;
406}
407bool RequestedLayerState::isHiddenByPolicy() const {
408 return (flags & layer_state_t::eLayerHidden) == layer_state_t::eLayerHidden;
409};
410half4 RequestedLayerState::getColor() const {
Vishnu Naira02943f2023-06-03 13:44:46 -0700411 if (sidebandStream || externalTexture) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000412 return {0._hf, 0._hf, 0._hf, color.a};
413 }
414 return color;
415}
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000416Rect RequestedLayerState::getBufferSize(uint32_t displayRotationFlags) const {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000417 // for buffer state layers we use the display frame size as the buffer size.
418 if (!externalTexture) {
419 return Rect::INVALID_RECT;
420 }
421
422 uint32_t bufWidth = externalTexture->getWidth();
423 uint32_t bufHeight = externalTexture->getHeight();
424
425 // Undo any transformations on the buffer and return the result.
426 if (bufferTransform & ui::Transform::ROT_90) {
427 std::swap(bufWidth, bufHeight);
428 }
429
430 if (transformToDisplayInverse) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000431 uint32_t invTransform = displayRotationFlags;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000432 if (invTransform & ui::Transform::ROT_90) {
433 std::swap(bufWidth, bufHeight);
434 }
435 }
436
437 return Rect(0, 0, static_cast<int32_t>(bufWidth), static_cast<int32_t>(bufHeight));
438}
439
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000440Rect RequestedLayerState::getCroppedBufferSize(const Rect& bufferSize) const {
441 Rect size = bufferSize;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000442 if (!crop.isEmpty() && size.isValid()) {
443 size.intersect(crop, &size);
444 } else if (!crop.isEmpty()) {
445 size = crop;
446 }
447 return size;
448}
449
450Rect RequestedLayerState::getBufferCrop() const {
451 // this is the crop rectangle that applies to the buffer
452 // itself (as opposed to the window)
453 if (!bufferCrop.isEmpty()) {
454 // if the buffer crop is defined, we use that
455 return bufferCrop;
456 } else if (externalTexture != nullptr) {
457 // otherwise we use the whole buffer
458 return externalTexture->getBounds();
459 } else {
460 // if we don't have a buffer yet, we use an empty/invalid crop
461 return Rect();
462 }
463}
464
465aidl::android::hardware::graphics::composer3::Composition RequestedLayerState::getCompositionType()
466 const {
467 using aidl::android::hardware::graphics::composer3::Composition;
468 // TODO(b/238781169) check about sidestream ready flag
469 if (sidebandStream.get()) {
470 return Composition::SIDEBAND;
471 }
472 if (!externalTexture) {
473 return Composition::SOLID_COLOR;
474 }
475 if (flags & layer_state_t::eLayerIsDisplayDecoration) {
476 return Composition::DISPLAY_DECORATION;
477 }
478 if (potentialCursor) {
479 return Composition::CURSOR;
480 }
481 return Composition::DEVICE;
482}
483
484Rect RequestedLayerState::reduce(const Rect& win, const Region& exclude) {
485 if (CC_LIKELY(exclude.isEmpty())) {
486 return win;
487 }
488 if (exclude.isRect()) {
489 return win.reduce(exclude.getBounds());
490 }
491 return Region(win).subtract(exclude).getBounds();
492}
493
Vishnu Nair04f89692022-11-16 23:21:05 +0000494// Returns true if the layer has a relative parent that is not its own parent. This is an input
495// error from the client, and this check allows us to handle it gracefully. If both parentId and
496// relativeParentId is unassigned then the layer does not have a valid relative parent.
497// If the relative parentid is unassigned, the layer will be considered relative but won't be
498// reachable.
499bool RequestedLayerState::hasValidRelativeParent() const {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000500 return isRelativeOf &&
501 (parentId != relativeParentId || relativeParentId == UNASSIGNED_LAYER_ID);
Vishnu Nair04f89692022-11-16 23:21:05 +0000502}
503
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000504bool RequestedLayerState::hasInputInfo() const {
505 if (!windowInfoHandle) {
506 return false;
507 }
508 const auto windowInfo = windowInfoHandle->getInfo();
509 return windowInfo->token != nullptr ||
510 windowInfo->inputConfig.test(gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL);
511}
512
Vishnu Nair80a5a702023-02-11 01:21:51 +0000513bool RequestedLayerState::hasBlur() const {
514 return backgroundBlurRadius > 0 || blurRegions.size() > 0;
515}
516
Vishnu Naird47bcee2023-02-24 18:08:51 +0000517bool RequestedLayerState::hasFrameUpdate() const {
518 return what & layer_state_t::CONTENT_DIRTY &&
519 (externalTexture || bgColorLayerId != UNASSIGNED_LAYER_ID);
520}
521
522bool RequestedLayerState::hasReadyFrame() const {
523 return hasFrameUpdate() || changes.test(Changes::SidebandStream) || autoRefresh;
524}
525
526bool RequestedLayerState::hasSidebandStreamFrame() const {
527 return hasFrameUpdate() && sidebandStream.get();
528}
529
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700530bool RequestedLayerState::willReleaseBufferOnLatch() const {
531 return changes.test(Changes::Buffer) && !externalTexture;
532}
533
Vishnu Nair4d9cef92023-06-24 22:34:41 +0000534bool RequestedLayerState::backpressureEnabled() const {
535 return flags & layer_state_t::eEnableBackpressure;
536}
537
538bool RequestedLayerState::isSimpleBufferUpdate(const layer_state_t& s) const {
539 static constexpr uint64_t requiredFlags = layer_state_t::eBufferChanged;
540 if ((s.what & requiredFlags) != requiredFlags) {
541 ATRACE_FORMAT_INSTANT("%s: false [missing required flags 0x%" PRIx64 "]", __func__,
542 (s.what | requiredFlags) & ~s.what);
543 return false;
544 }
545
546 static constexpr uint64_t deniedFlags = layer_state_t::eProducerDisconnect |
547 layer_state_t::eLayerChanged | layer_state_t::eRelativeLayerChanged |
548 layer_state_t::eTransparentRegionChanged | layer_state_t::eFlagsChanged |
549 layer_state_t::eBlurRegionsChanged | layer_state_t::eLayerStackChanged |
550 layer_state_t::eAutoRefreshChanged | layer_state_t::eReparent;
551 if (s.what & deniedFlags) {
552 ATRACE_FORMAT_INSTANT("%s: false [has denied flags 0x%" PRIx64 "]", __func__,
553 s.what & deniedFlags);
554 return false;
555 }
556
557 bool changedFlags = diff(s);
558 static constexpr auto deniedChanges = layer_state_t::ePositionChanged |
559 layer_state_t::eAlphaChanged | layer_state_t::eColorTransformChanged |
560 layer_state_t::eBackgroundColorChanged | layer_state_t::eMatrixChanged |
561 layer_state_t::eCornerRadiusChanged | layer_state_t::eBackgroundBlurRadiusChanged |
562 layer_state_t::eBufferTransformChanged |
563 layer_state_t::eTransformToDisplayInverseChanged | layer_state_t::eCropChanged |
564 layer_state_t::eDataspaceChanged | layer_state_t::eHdrMetadataChanged |
565 layer_state_t::eSidebandStreamChanged | layer_state_t::eColorSpaceAgnosticChanged |
566 layer_state_t::eShadowRadiusChanged | layer_state_t::eFixedTransformHintChanged |
567 layer_state_t::eTrustedOverlayChanged | layer_state_t::eStretchChanged |
568 layer_state_t::eBufferCropChanged | layer_state_t::eDestinationFrameChanged |
569 layer_state_t::eDimmingEnabledChanged | layer_state_t::eExtendedRangeBrightnessChanged;
570 if (changedFlags & deniedChanges) {
571 ATRACE_FORMAT_INSTANT("%s: false [has denied changes flags 0x%" PRIx64 "]", __func__,
572 s.what & deniedChanges);
573 return false;
574 }
575
576 return true;
577}
578
Vishnu Nair0808ae62023-08-07 21:42:42 -0700579bool RequestedLayerState::isProtected() const {
580 return externalTexture && externalTexture->getUsage() & GRALLOC_USAGE_PROTECTED;
581}
582
Vishnu Naird47bcee2023-02-24 18:08:51 +0000583void RequestedLayerState::clearChanges() {
584 what = 0;
585 changes.clear();
586}
587
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000588} // namespace android::surfaceflinger::frontend