blob: a4777d1148b649943ca183980e9888d2940672f2 [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 Nair7ee4f462023-04-19 09:54:09 -0700150 const bool hadSideStream = sidebandStream != nullptr;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000151 const layer_state_t& clientState = resolvedComposerState.state;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000152 const bool hadBlur = hasBlur();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000153 uint64_t clientChanges = what | layer_state_t::diff(clientState);
154 layer_state_t::merge(clientState);
155 what = clientChanges;
Vishnu Naira02943f2023-06-03 13:44:46 -0700156 LLOGV(layerId, "requested=%" PRIu64 "flags=%" PRIu64, clientState.what, clientChanges);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000157
158 if (clientState.what & layer_state_t::eFlagsChanged) {
159 if ((oldFlags ^ flags) & layer_state_t::eLayerHidden) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000160 changes |= RequestedLayerState::Changes::Visibility |
161 RequestedLayerState::Changes::VisibleRegion;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000162 }
163 if ((oldFlags ^ flags) & layer_state_t::eIgnoreDestinationFrame) {
164 changes |= RequestedLayerState::Changes::Geometry;
165 }
166 }
Vishnu Naird1f74982023-06-15 20:16:51 -0700167
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700168 if (clientState.what & layer_state_t::eBufferChanged) {
169 externalTexture = resolvedComposerState.externalTexture;
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700170 const bool hasBuffer = externalTexture != nullptr;
171 if (hasBuffer || hasBuffer != hadBuffer) {
172 changes |= RequestedLayerState::Changes::Buffer;
Vishnu Naira02943f2023-06-03 13:44:46 -0700173 const ui::Size newBufferSize = hasBuffer
174 ? ui::Size(externalTexture->getWidth(), externalTexture->getHeight())
175 : ui::Size();
176 if (oldBufferSize != newBufferSize) {
177 changes |= RequestedLayerState::Changes::BufferSize;
178 changes |= RequestedLayerState::Changes::Geometry;
179 }
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700180 }
181
182 if (hasBuffer != hadBuffer) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000183 changes |= RequestedLayerState::Changes::Geometry |
184 RequestedLayerState::Changes::VisibleRegion |
Vishnu Naird47bcee2023-02-24 18:08:51 +0000185 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Input;
186 }
Vishnu Naird1f74982023-06-15 20:16:51 -0700187
188 if (hasBuffer) {
189 const bool frameNumberChanged =
190 bufferData->flags.test(BufferData::BufferDataChange::frameNumberChanged);
191 const uint64_t frameNumber =
192 frameNumberChanged ? bufferData->frameNumber : oldFramenumber + 1;
193 bufferData->frameNumber = frameNumber;
194
195 if ((barrierProducerId > bufferData->producerId) ||
196 ((barrierProducerId == bufferData->producerId) &&
197 (barrierFrameNumber > bufferData->frameNumber))) {
198 ALOGE("Out of order buffers detected for %s producedId=%d frameNumber=%" PRIu64
199 " -> producedId=%d frameNumber=%" PRIu64,
200 getDebugString().c_str(), bufferData->producerId, bufferData->frameNumber,
201 bufferData->producerId, frameNumber);
202 TransactionTraceWriter::getInstance().invoke("out_of_order_buffers_",
203 /*overwrite=*/false);
204 }
205
206 barrierProducerId = std::max(bufferData->producerId, barrierProducerId);
207 barrierFrameNumber = std::max(bufferData->frameNumber, barrierFrameNumber);
208 }
Vishnu Nair63221212023-04-06 15:17:37 -0700209 }
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700210
Vishnu Nair63221212023-04-06 15:17:37 -0700211 if (clientState.what & layer_state_t::eSidebandStreamChanged) {
212 changes |= RequestedLayerState::Changes::SidebandStream;
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700213 const bool hasSideStream = sidebandStream != nullptr;
214 if (hasSideStream != hadSideStream) {
215 changes |= RequestedLayerState::Changes::Geometry |
216 RequestedLayerState::Changes::VisibleRegion |
217 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Input;
218 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000219 }
220 if (what & (layer_state_t::eAlphaChanged)) {
221 if (oldAlpha == 0 || color.a == 0) {
222 changes |= RequestedLayerState::Changes::Visibility |
223 RequestedLayerState::Changes::VisibleRegion;
224 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000225 }
Vishnu Nair80a5a702023-02-11 01:21:51 +0000226 if (what & (layer_state_t::eBackgroundBlurRadiusChanged | layer_state_t::eBlurRegionsChanged)) {
227 if (hadBlur != hasBlur()) {
228 changes |= RequestedLayerState::Changes::Visibility |
229 RequestedLayerState::Changes::VisibleRegion;
230 }
231 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000232 if (clientChanges & layer_state_t::HIERARCHY_CHANGES)
233 changes |= RequestedLayerState::Changes::Hierarchy;
234 if (clientChanges & layer_state_t::CONTENT_CHANGES)
235 changes |= RequestedLayerState::Changes::Content;
236 if (clientChanges & layer_state_t::GEOMETRY_CHANGES)
237 changes |= RequestedLayerState::Changes::Geometry;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000238 if (clientChanges & layer_state_t::AFFECTS_CHILDREN)
239 changes |= RequestedLayerState::Changes::AffectsChildren;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000240 if (clientChanges & layer_state_t::INPUT_CHANGES)
241 changes |= RequestedLayerState::Changes::Input;
242 if (clientChanges & layer_state_t::VISIBLE_REGION_CHANGES)
243 changes |= RequestedLayerState::Changes::VisibleRegion;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000244 if (clientState.what & layer_state_t::eColorTransformChanged) {
245 static const mat4 identityMatrix = mat4();
246 hasColorTransform = colorTransform != identityMatrix;
247 }
Vishnu Naird47bcee2023-02-24 18:08:51 +0000248 if (clientState.what &
249 (layer_state_t::eLayerChanged | layer_state_t::eRelativeLayerChanged |
250 layer_state_t::eLayerStackChanged)) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000251 changes |= RequestedLayerState::Changes::Z;
252 }
253 if (clientState.what & layer_state_t::eReparent) {
254 changes |= RequestedLayerState::Changes::Parent;
Vishnu Nair1391de22023-03-05 19:56:14 -0800255 parentId = resolvedComposerState.parentId;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000256 parentSurfaceControlForChild = nullptr;
Vishnu Nair04f89692022-11-16 23:21:05 +0000257 // Once a layer has be reparented, it cannot be placed at the root. It sounds odd
258 // but thats the existing logic and until we make this behavior more explicit, we need
259 // to maintain this logic.
260 canBeRoot = false;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000261 }
262 if (clientState.what & layer_state_t::eRelativeLayerChanged) {
263 changes |= RequestedLayerState::Changes::RelativeParent;
Vishnu Nair1391de22023-03-05 19:56:14 -0800264 relativeParentId = resolvedComposerState.relativeParentId;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000265 isRelativeOf = true;
266 relativeLayerSurfaceControl = nullptr;
267 }
268 if ((clientState.what & layer_state_t::eLayerChanged ||
269 (clientState.what & layer_state_t::eReparent && parentId == UNASSIGNED_LAYER_ID)) &&
270 isRelativeOf) {
271 // clear out relz data
272 relativeParentId = UNASSIGNED_LAYER_ID;
273 isRelativeOf = false;
274 changes |= RequestedLayerState::Changes::RelativeParent;
275 }
276 if (clientState.what & layer_state_t::eReparent && parentId == relativeParentId) {
277 // provide a hint that we are are now a direct child and not a relative child.
278 changes |= RequestedLayerState::Changes::RelativeParent;
279 }
280 if (clientState.what & layer_state_t::eInputInfoChanged) {
Vishnu Nair1391de22023-03-05 19:56:14 -0800281 touchCropId = resolvedComposerState.touchCropId;
282 windowInfoHandle->editInfo()->touchableRegionCropHandle.clear();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000283 }
284 if (clientState.what & layer_state_t::eStretchChanged) {
285 stretchEffect.sanitize();
286 }
287
288 if (clientState.what & layer_state_t::eHasListenerCallbacksChanged) {
289 // TODO(b/238781169) handle callbacks
290 }
291
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000292 if (clientState.what & layer_state_t::ePositionChanged) {
293 requestedTransform.set(x, y);
294 }
295
296 if (clientState.what & layer_state_t::eMatrixChanged) {
297 requestedTransform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
298 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000299 if (clientState.what & layer_state_t::eMetadataChanged) {
300 const int32_t requestedGameMode =
301 clientState.metadata.getInt32(gui::METADATA_GAME_MODE, -1);
302 if (requestedGameMode != -1) {
303 // The transaction will be received on the Task layer and needs to be applied to all
304 // child layers.
305 if (static_cast<int32_t>(gameMode) != requestedGameMode) {
306 gameMode = static_cast<gui::GameMode>(requestedGameMode);
Vishnu Naira02943f2023-06-03 13:44:46 -0700307 changes |= RequestedLayerState::Changes::GameMode;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000308 }
309 }
310 }
311 if (clientState.what & layer_state_t::eFrameRateChanged) {
312 const auto compatibility =
313 Layer::FrameRate::convertCompatibility(clientState.frameRateCompatibility);
314 const auto strategy = Layer::FrameRate::convertChangeFrameRateStrategy(
315 clientState.changeFrameRateStrategy);
316 requestedFrameRate =
317 Layer::FrameRate(Fps::fromValue(clientState.frameRate), compatibility, strategy);
318 changes |= RequestedLayerState::Changes::FrameRate;
319 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000320}
321
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000322ui::Size RequestedLayerState::getUnrotatedBufferSize(uint32_t displayRotationFlags) const {
323 uint32_t bufferWidth = externalTexture->getWidth();
324 uint32_t bufferHeight = externalTexture->getHeight();
325 // Undo any transformations on the buffer.
326 if (bufferTransform & ui::Transform::ROT_90) {
327 std::swap(bufferWidth, bufferHeight);
328 }
329 if (transformToDisplayInverse) {
330 if (displayRotationFlags & ui::Transform::ROT_90) {
331 std::swap(bufferWidth, bufferHeight);
332 }
333 }
334 return {bufferWidth, bufferHeight};
335}
336
337ui::Transform RequestedLayerState::getTransform(uint32_t displayRotationFlags) const {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000338 if ((flags & layer_state_t::eIgnoreDestinationFrame) || destinationFrame.isEmpty()) {
339 // If destination frame is not set, use the requested transform set via
340 // Transaction::setPosition and Transaction::setMatrix.
341 return requestedTransform;
342 }
343
344 Rect destRect = destinationFrame;
345 int32_t destW = destRect.width();
346 int32_t destH = destRect.height();
347 if (destRect.left < 0) {
348 destRect.left = 0;
349 destRect.right = destW;
350 }
351 if (destRect.top < 0) {
352 destRect.top = 0;
353 destRect.bottom = destH;
354 }
355
356 if (!externalTexture) {
357 ui::Transform transform;
358 transform.set(static_cast<float>(destRect.left), static_cast<float>(destRect.top));
359 return transform;
360 }
361
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000362 ui::Size bufferSize = getUnrotatedBufferSize(displayRotationFlags);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000363
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000364 float sx = static_cast<float>(destW) / static_cast<float>(bufferSize.width);
365 float sy = static_cast<float>(destH) / static_cast<float>(bufferSize.height);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000366 ui::Transform transform;
367 transform.set(sx, 0, 0, sy);
368 transform.set(static_cast<float>(destRect.left), static_cast<float>(destRect.top));
369 return transform;
370}
371
372std::string RequestedLayerState::getDebugString() const {
Vishnu Nair80a5a702023-02-11 01:21:51 +0000373 std::stringstream debug;
Vishnu Nair444f3952023-04-11 13:01:02 -0700374 debug << "RequestedLayerState{" << name;
375 if (parentId != UNASSIGNED_LAYER_ID) debug << " parentId=" << parentId;
376 if (relativeParentId != UNASSIGNED_LAYER_ID) debug << " relativeParentId=" << relativeParentId;
377 if (!mirrorIds.empty()) debug << " mirrorId=" << layerIdsToString(mirrorIds);
378 if (!handleAlive) debug << " !handle";
379 if (z != 0) debug << " z=" << z;
380 if (layerStack.id != 0) debug << " layerStack=" << layerStack.id;
Vishnu Nair80a5a702023-02-11 01:21:51 +0000381 return debug.str();
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000382}
383
Vishnu Nair3cc15a42023-06-30 06:20:22 +0000384std::ostream& operator<<(std::ostream& out, const RequestedLayerState& obj) {
385 out << obj.debugName;
386 if (obj.relativeParentId != UNASSIGNED_LAYER_ID) out << " parent=" << obj.parentId;
387 if (!obj.handleAlive) out << " handleNotAlive";
388 return out;
389}
390
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000391std::string RequestedLayerState::getDebugStringShort() const {
392 return "[" + std::to_string(id) + "]" + name;
393}
394
395bool RequestedLayerState::canBeDestroyed() const {
396 return !handleAlive && parentId == UNASSIGNED_LAYER_ID;
397}
398bool RequestedLayerState::isRoot() const {
399 return canBeRoot && parentId == UNASSIGNED_LAYER_ID;
400}
401bool RequestedLayerState::isHiddenByPolicy() const {
402 return (flags & layer_state_t::eLayerHidden) == layer_state_t::eLayerHidden;
403};
404half4 RequestedLayerState::getColor() const {
Vishnu Naira02943f2023-06-03 13:44:46 -0700405 if (sidebandStream || externalTexture) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000406 return {0._hf, 0._hf, 0._hf, color.a};
407 }
408 return color;
409}
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000410Rect RequestedLayerState::getBufferSize(uint32_t displayRotationFlags) const {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000411 // for buffer state layers we use the display frame size as the buffer size.
412 if (!externalTexture) {
413 return Rect::INVALID_RECT;
414 }
415
416 uint32_t bufWidth = externalTexture->getWidth();
417 uint32_t bufHeight = externalTexture->getHeight();
418
419 // Undo any transformations on the buffer and return the result.
420 if (bufferTransform & ui::Transform::ROT_90) {
421 std::swap(bufWidth, bufHeight);
422 }
423
424 if (transformToDisplayInverse) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000425 uint32_t invTransform = displayRotationFlags;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000426 if (invTransform & ui::Transform::ROT_90) {
427 std::swap(bufWidth, bufHeight);
428 }
429 }
430
431 return Rect(0, 0, static_cast<int32_t>(bufWidth), static_cast<int32_t>(bufHeight));
432}
433
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000434Rect RequestedLayerState::getCroppedBufferSize(const Rect& bufferSize) const {
435 Rect size = bufferSize;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000436 if (!crop.isEmpty() && size.isValid()) {
437 size.intersect(crop, &size);
438 } else if (!crop.isEmpty()) {
439 size = crop;
440 }
441 return size;
442}
443
444Rect RequestedLayerState::getBufferCrop() const {
445 // this is the crop rectangle that applies to the buffer
446 // itself (as opposed to the window)
447 if (!bufferCrop.isEmpty()) {
448 // if the buffer crop is defined, we use that
449 return bufferCrop;
450 } else if (externalTexture != nullptr) {
451 // otherwise we use the whole buffer
452 return externalTexture->getBounds();
453 } else {
454 // if we don't have a buffer yet, we use an empty/invalid crop
455 return Rect();
456 }
457}
458
459aidl::android::hardware::graphics::composer3::Composition RequestedLayerState::getCompositionType()
460 const {
461 using aidl::android::hardware::graphics::composer3::Composition;
462 // TODO(b/238781169) check about sidestream ready flag
463 if (sidebandStream.get()) {
464 return Composition::SIDEBAND;
465 }
466 if (!externalTexture) {
467 return Composition::SOLID_COLOR;
468 }
469 if (flags & layer_state_t::eLayerIsDisplayDecoration) {
470 return Composition::DISPLAY_DECORATION;
471 }
472 if (potentialCursor) {
473 return Composition::CURSOR;
474 }
475 return Composition::DEVICE;
476}
477
478Rect RequestedLayerState::reduce(const Rect& win, const Region& exclude) {
479 if (CC_LIKELY(exclude.isEmpty())) {
480 return win;
481 }
482 if (exclude.isRect()) {
483 return win.reduce(exclude.getBounds());
484 }
485 return Region(win).subtract(exclude).getBounds();
486}
487
Vishnu Nair04f89692022-11-16 23:21:05 +0000488// Returns true if the layer has a relative parent that is not its own parent. This is an input
489// error from the client, and this check allows us to handle it gracefully. If both parentId and
490// relativeParentId is unassigned then the layer does not have a valid relative parent.
491// If the relative parentid is unassigned, the layer will be considered relative but won't be
492// reachable.
493bool RequestedLayerState::hasValidRelativeParent() const {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000494 return isRelativeOf &&
495 (parentId != relativeParentId || relativeParentId == UNASSIGNED_LAYER_ID);
Vishnu Nair04f89692022-11-16 23:21:05 +0000496}
497
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000498bool RequestedLayerState::hasInputInfo() const {
499 if (!windowInfoHandle) {
500 return false;
501 }
502 const auto windowInfo = windowInfoHandle->getInfo();
503 return windowInfo->token != nullptr ||
504 windowInfo->inputConfig.test(gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL);
505}
506
Vishnu Nair80a5a702023-02-11 01:21:51 +0000507bool RequestedLayerState::hasBlur() const {
508 return backgroundBlurRadius > 0 || blurRegions.size() > 0;
509}
510
Vishnu Naird47bcee2023-02-24 18:08:51 +0000511bool RequestedLayerState::hasFrameUpdate() const {
512 return what & layer_state_t::CONTENT_DIRTY &&
513 (externalTexture || bgColorLayerId != UNASSIGNED_LAYER_ID);
514}
515
516bool RequestedLayerState::hasReadyFrame() const {
517 return hasFrameUpdate() || changes.test(Changes::SidebandStream) || autoRefresh;
518}
519
520bool RequestedLayerState::hasSidebandStreamFrame() const {
521 return hasFrameUpdate() && sidebandStream.get();
522}
523
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700524bool RequestedLayerState::willReleaseBufferOnLatch() const {
525 return changes.test(Changes::Buffer) && !externalTexture;
526}
527
Vishnu Nair4d9cef92023-06-24 22:34:41 +0000528bool RequestedLayerState::backpressureEnabled() const {
529 return flags & layer_state_t::eEnableBackpressure;
530}
531
532bool RequestedLayerState::isSimpleBufferUpdate(const layer_state_t& s) const {
533 static constexpr uint64_t requiredFlags = layer_state_t::eBufferChanged;
534 if ((s.what & requiredFlags) != requiredFlags) {
535 ATRACE_FORMAT_INSTANT("%s: false [missing required flags 0x%" PRIx64 "]", __func__,
536 (s.what | requiredFlags) & ~s.what);
537 return false;
538 }
539
540 static constexpr uint64_t deniedFlags = layer_state_t::eProducerDisconnect |
541 layer_state_t::eLayerChanged | layer_state_t::eRelativeLayerChanged |
542 layer_state_t::eTransparentRegionChanged | layer_state_t::eFlagsChanged |
543 layer_state_t::eBlurRegionsChanged | layer_state_t::eLayerStackChanged |
544 layer_state_t::eAutoRefreshChanged | layer_state_t::eReparent;
545 if (s.what & deniedFlags) {
546 ATRACE_FORMAT_INSTANT("%s: false [has denied flags 0x%" PRIx64 "]", __func__,
547 s.what & deniedFlags);
548 return false;
549 }
550
551 bool changedFlags = diff(s);
552 static constexpr auto deniedChanges = layer_state_t::ePositionChanged |
553 layer_state_t::eAlphaChanged | layer_state_t::eColorTransformChanged |
554 layer_state_t::eBackgroundColorChanged | layer_state_t::eMatrixChanged |
555 layer_state_t::eCornerRadiusChanged | layer_state_t::eBackgroundBlurRadiusChanged |
556 layer_state_t::eBufferTransformChanged |
557 layer_state_t::eTransformToDisplayInverseChanged | layer_state_t::eCropChanged |
558 layer_state_t::eDataspaceChanged | layer_state_t::eHdrMetadataChanged |
559 layer_state_t::eSidebandStreamChanged | layer_state_t::eColorSpaceAgnosticChanged |
560 layer_state_t::eShadowRadiusChanged | layer_state_t::eFixedTransformHintChanged |
561 layer_state_t::eTrustedOverlayChanged | layer_state_t::eStretchChanged |
562 layer_state_t::eBufferCropChanged | layer_state_t::eDestinationFrameChanged |
563 layer_state_t::eDimmingEnabledChanged | layer_state_t::eExtendedRangeBrightnessChanged;
564 if (changedFlags & deniedChanges) {
565 ATRACE_FORMAT_INSTANT("%s: false [has denied changes flags 0x%" PRIx64 "]", __func__,
566 s.what & deniedChanges);
567 return false;
568 }
569
570 return true;
571}
572
Vishnu Naird47bcee2023-02-24 18:08:51 +0000573void RequestedLayerState::clearChanges() {
574 what = 0;
575 changes.clear();
576}
577
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000578} // namespace android::surfaceflinger::frontend