blob: b7fa4f0aa23d9f7affac5ebfa1f671d3126698a4 [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 */
16
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18#undef LOG_TAG
19#define LOG_TAG "RequestedLayerState"
20
Vishnu Naircfb2d252023-01-19 04:44:02 +000021#include <log/log.h>
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000022#include <private/android_filesystem_config.h>
23#include <sys/types.h>
24
25#include "Layer.h"
Vishnu Naircfb2d252023-01-19 04:44:02 +000026#include "LayerCreationArgs.h"
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000027#include "LayerHandle.h"
Vishnu Naircfb2d252023-01-19 04:44:02 +000028#include "LayerLog.h"
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000029#include "RequestedLayerState.h"
30
31namespace android::surfaceflinger::frontend {
32using ftl::Flags;
33using namespace ftl::flag_operators;
34
35namespace {
36uint32_t getLayerIdFromSurfaceControl(sp<SurfaceControl> surfaceControl) {
37 if (!surfaceControl) {
38 return UNASSIGNED_LAYER_ID;
39 }
40
41 return LayerHandle::getLayerId(surfaceControl->getHandle());
42}
43
44std::string layerIdToString(uint32_t layerId) {
Vishnu Nair04f89692022-11-16 23:21:05 +000045 return layerId == UNASSIGNED_LAYER_ID ? "none" : std::to_string(layerId);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000046}
47
48} // namespace
49
50RequestedLayerState::RequestedLayerState(const LayerCreationArgs& args)
51 : id(args.sequence),
Vishnu Naircfb2d252023-01-19 04:44:02 +000052 name(args.name + "#" + std::to_string(args.sequence)),
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000053 canBeRoot(args.addToRoot),
54 layerCreationFlags(args.flags),
55 textureName(args.textureName),
56 ownerUid(args.ownerUid),
57 ownerPid(args.ownerPid) {
58 layerId = static_cast<int32_t>(args.sequence);
59 changes |= RequestedLayerState::Changes::Created;
60 metadata.merge(args.metadata);
61 changes |= RequestedLayerState::Changes::Metadata;
62 handleAlive = true;
63 parentId = LayerHandle::getLayerId(args.parentHandle.promote());
Vishnu Naircfb2d252023-01-19 04:44:02 +000064 if (args.parentHandle != nullptr) {
65 canBeRoot = false;
66 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000067 mirrorId = LayerHandle::getLayerId(args.mirrorLayerHandle.promote());
68 if (mirrorId != UNASSIGNED_LAYER_ID) {
69 changes |= RequestedLayerState::Changes::Mirror;
70 }
71
72 flags = 0;
73 if (args.flags & ISurfaceComposerClient::eHidden) flags |= layer_state_t::eLayerHidden;
74 if (args.flags & ISurfaceComposerClient::eOpaque) flags |= layer_state_t::eLayerOpaque;
75 if (args.flags & ISurfaceComposerClient::eSecure) flags |= layer_state_t::eLayerSecure;
76 if (args.flags & ISurfaceComposerClient::eSkipScreenshot) {
77 flags |= layer_state_t::eLayerSkipScreenshot;
78 }
79 premultipliedAlpha = !(args.flags & ISurfaceComposerClient::eNonPremultiplied);
80 potentialCursor = args.flags & ISurfaceComposerClient::eCursorWindow;
81 protectedByApp = args.flags & ISurfaceComposerClient::eProtectedByApp;
82 if (args.flags & ISurfaceComposerClient::eNoColorFill) {
83 // Set an invalid color so there is no color fill.
84 // (b/259981098) use an explicit flag instead of relying on invalid values.
85 color.r = -1.0_hf;
86 color.g = -1.0_hf;
87 color.b = -1.0_hf;
88 } else {
89 color.rgb = {0.0_hf, 0.0_hf, 0.0_hf};
90 }
Vishnu Naircfb2d252023-01-19 04:44:02 +000091 LLOGV(layerId, "Created %s flags=%d", getDebugString().c_str(), flags);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000092 color.a = 1.0f;
93
94 crop.makeInvalid();
95 z = 0;
96 layerStack = ui::DEFAULT_LAYER_STACK;
97 transformToDisplayInverse = false;
98 dataspace = ui::Dataspace::UNKNOWN;
John Reck68796592023-01-25 13:47:12 -050099 desiredSdrHdrRatio = 1.f;
100 currentSdrHdrRatio = 1.f;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000101 dataspaceRequested = false;
102 hdrMetadata.validTypes = 0;
103 surfaceDamageRegion = Region::INVALID_REGION;
104 cornerRadius = 0.0f;
105 backgroundBlurRadius = 0;
106 api = -1;
107 hasColorTransform = false;
108 bufferTransform = 0;
109 requestedTransform.reset();
110 bufferData = std::make_shared<BufferData>();
111 bufferData->frameNumber = 0;
112 bufferData->acquireFence = sp<Fence>::make(-1);
113 acquireFenceTime = std::make_shared<FenceTime>(bufferData->acquireFence);
114 colorSpaceAgnostic = false;
115 frameRateSelectionPriority = Layer::PRIORITY_UNSET;
116 shadowRadius = 0.f;
117 fixedTransformHint = ui::Transform::ROT_INVALID;
118 destinationFrame.makeInvalid();
119 isTrustedOverlay = false;
120 dropInputMode = gui::DropInputMode::NONE;
121 dimmingEnabled = true;
122 defaultFrameRateCompatibility =
123 static_cast<int8_t>(scheduler::LayerInfo::FrameRateCompatibility::Default);
124 dataspace = ui::Dataspace::V0_SRGB;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000125 gameMode = gui::GameMode::Unsupported;
126 requestedFrameRate = {};
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000127}
128
129void RequestedLayerState::merge(const ResolvedComposerState& resolvedComposerState) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000130 const uint32_t oldFlags = flags;
131 const half oldAlpha = color.a;
132 const bool hadBufferOrSideStream = hasValidBuffer() || sidebandStream != nullptr;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000133 const layer_state_t& clientState = resolvedComposerState.state;
134
135 uint64_t clientChanges = what | layer_state_t::diff(clientState);
136 layer_state_t::merge(clientState);
137 what = clientChanges;
138
139 if (clientState.what & layer_state_t::eFlagsChanged) {
140 if ((oldFlags ^ flags) & layer_state_t::eLayerHidden) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000141 changes |= RequestedLayerState::Changes::Visibility |
142 RequestedLayerState::Changes::VisibleRegion;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000143 }
144 if ((oldFlags ^ flags) & layer_state_t::eIgnoreDestinationFrame) {
145 changes |= RequestedLayerState::Changes::Geometry;
146 }
147 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000148 if (clientState.what &
149 (layer_state_t::eBufferChanged | layer_state_t::eSidebandStreamChanged)) {
150 const bool hasBufferOrSideStream = hasValidBuffer() || sidebandStream != nullptr;
151 if (hadBufferOrSideStream != hasBufferOrSideStream) {
152 changes |= RequestedLayerState::Changes::Geometry |
153 RequestedLayerState::Changes::VisibleRegion |
154 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Input |
155 RequestedLayerState::Changes::Buffer;
156 }
157 }
158 if (what & (layer_state_t::eAlphaChanged)) {
159 if (oldAlpha == 0 || color.a == 0) {
160 changes |= RequestedLayerState::Changes::Visibility |
161 RequestedLayerState::Changes::VisibleRegion;
162 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000163 }
164 if (clientChanges & layer_state_t::HIERARCHY_CHANGES)
165 changes |= RequestedLayerState::Changes::Hierarchy;
166 if (clientChanges & layer_state_t::CONTENT_CHANGES)
167 changes |= RequestedLayerState::Changes::Content;
168 if (clientChanges & layer_state_t::GEOMETRY_CHANGES)
169 changes |= RequestedLayerState::Changes::Geometry;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000170 if (clientChanges & layer_state_t::AFFECTS_CHILDREN)
171 changes |= RequestedLayerState::Changes::AffectsChildren;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000172 if (clientChanges & layer_state_t::INPUT_CHANGES)
173 changes |= RequestedLayerState::Changes::Input;
174 if (clientChanges & layer_state_t::VISIBLE_REGION_CHANGES)
175 changes |= RequestedLayerState::Changes::VisibleRegion;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000176 if (clientState.what & layer_state_t::eColorTransformChanged) {
177 static const mat4 identityMatrix = mat4();
178 hasColorTransform = colorTransform != identityMatrix;
179 }
Vishnu Nair04f89692022-11-16 23:21:05 +0000180 if (clientState.what & (layer_state_t::eLayerChanged | layer_state_t::eRelativeLayerChanged)) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000181 changes |= RequestedLayerState::Changes::Z;
182 }
183 if (clientState.what & layer_state_t::eReparent) {
184 changes |= RequestedLayerState::Changes::Parent;
185 parentId = getLayerIdFromSurfaceControl(clientState.parentSurfaceControlForChild);
186 parentSurfaceControlForChild = nullptr;
Vishnu Nair04f89692022-11-16 23:21:05 +0000187 // Once a layer has be reparented, it cannot be placed at the root. It sounds odd
188 // but thats the existing logic and until we make this behavior more explicit, we need
189 // to maintain this logic.
190 canBeRoot = false;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000191 }
192 if (clientState.what & layer_state_t::eRelativeLayerChanged) {
193 changes |= RequestedLayerState::Changes::RelativeParent;
194 relativeParentId = getLayerIdFromSurfaceControl(clientState.relativeLayerSurfaceControl);
195 isRelativeOf = true;
196 relativeLayerSurfaceControl = nullptr;
197 }
198 if ((clientState.what & layer_state_t::eLayerChanged ||
199 (clientState.what & layer_state_t::eReparent && parentId == UNASSIGNED_LAYER_ID)) &&
200 isRelativeOf) {
201 // clear out relz data
202 relativeParentId = UNASSIGNED_LAYER_ID;
203 isRelativeOf = false;
204 changes |= RequestedLayerState::Changes::RelativeParent;
205 }
206 if (clientState.what & layer_state_t::eReparent && parentId == relativeParentId) {
207 // provide a hint that we are are now a direct child and not a relative child.
208 changes |= RequestedLayerState::Changes::RelativeParent;
209 }
210 if (clientState.what & layer_state_t::eInputInfoChanged) {
211 wp<IBinder>& touchableRegionCropHandle =
212 windowInfoHandle->editInfo()->touchableRegionCropHandle;
213 touchCropId = LayerHandle::getLayerId(touchableRegionCropHandle.promote());
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000214 touchableRegionCropHandle.clear();
215 }
216 if (clientState.what & layer_state_t::eStretchChanged) {
217 stretchEffect.sanitize();
218 }
219
220 if (clientState.what & layer_state_t::eHasListenerCallbacksChanged) {
221 // TODO(b/238781169) handle callbacks
222 }
223
224 if (clientState.what & layer_state_t::eBufferChanged) {
225 externalTexture = resolvedComposerState.externalTexture;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000226 }
227
228 if (clientState.what & layer_state_t::ePositionChanged) {
229 requestedTransform.set(x, y);
230 }
231
232 if (clientState.what & layer_state_t::eMatrixChanged) {
233 requestedTransform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
234 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000235 if (clientState.what & layer_state_t::eMetadataChanged) {
236 const int32_t requestedGameMode =
237 clientState.metadata.getInt32(gui::METADATA_GAME_MODE, -1);
238 if (requestedGameMode != -1) {
239 // The transaction will be received on the Task layer and needs to be applied to all
240 // child layers.
241 if (static_cast<int32_t>(gameMode) != requestedGameMode) {
242 gameMode = static_cast<gui::GameMode>(requestedGameMode);
243 changes |= RequestedLayerState::Changes::AffectsChildren;
244 }
245 }
246 }
247 if (clientState.what & layer_state_t::eFrameRateChanged) {
248 const auto compatibility =
249 Layer::FrameRate::convertCompatibility(clientState.frameRateCompatibility);
250 const auto strategy = Layer::FrameRate::convertChangeFrameRateStrategy(
251 clientState.changeFrameRateStrategy);
252 requestedFrameRate =
253 Layer::FrameRate(Fps::fromValue(clientState.frameRate), compatibility, strategy);
254 changes |= RequestedLayerState::Changes::FrameRate;
255 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000256}
257
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000258ui::Size RequestedLayerState::getUnrotatedBufferSize(uint32_t displayRotationFlags) const {
259 uint32_t bufferWidth = externalTexture->getWidth();
260 uint32_t bufferHeight = externalTexture->getHeight();
261 // Undo any transformations on the buffer.
262 if (bufferTransform & ui::Transform::ROT_90) {
263 std::swap(bufferWidth, bufferHeight);
264 }
265 if (transformToDisplayInverse) {
266 if (displayRotationFlags & ui::Transform::ROT_90) {
267 std::swap(bufferWidth, bufferHeight);
268 }
269 }
270 return {bufferWidth, bufferHeight};
271}
272
273ui::Transform RequestedLayerState::getTransform(uint32_t displayRotationFlags) const {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000274 if ((flags & layer_state_t::eIgnoreDestinationFrame) || destinationFrame.isEmpty()) {
275 // If destination frame is not set, use the requested transform set via
276 // Transaction::setPosition and Transaction::setMatrix.
277 return requestedTransform;
278 }
279
280 Rect destRect = destinationFrame;
281 int32_t destW = destRect.width();
282 int32_t destH = destRect.height();
283 if (destRect.left < 0) {
284 destRect.left = 0;
285 destRect.right = destW;
286 }
287 if (destRect.top < 0) {
288 destRect.top = 0;
289 destRect.bottom = destH;
290 }
291
292 if (!externalTexture) {
293 ui::Transform transform;
294 transform.set(static_cast<float>(destRect.left), static_cast<float>(destRect.top));
295 return transform;
296 }
297
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000298 ui::Size bufferSize = getUnrotatedBufferSize(displayRotationFlags);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000299
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000300 float sx = static_cast<float>(destW) / static_cast<float>(bufferSize.width);
301 float sy = static_cast<float>(destH) / static_cast<float>(bufferSize.height);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000302 ui::Transform transform;
303 transform.set(sx, 0, 0, sy);
304 transform.set(static_cast<float>(destRect.left), static_cast<float>(destRect.top));
305 return transform;
306}
307
308std::string RequestedLayerState::getDebugString() const {
309 return "[" + std::to_string(id) + "]" + name + ",parent=" + layerIdToString(parentId) +
310 ",relativeParent=" + layerIdToString(relativeParentId) +
311 ",isRelativeOf=" + std::to_string(isRelativeOf) +
312 ",mirrorId=" + layerIdToString(mirrorId) +
Vishnu Nair04f89692022-11-16 23:21:05 +0000313 ",handleAlive=" + std::to_string(handleAlive) + ",z=" + std::to_string(z);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000314}
315
316std::string RequestedLayerState::getDebugStringShort() const {
317 return "[" + std::to_string(id) + "]" + name;
318}
319
320bool RequestedLayerState::canBeDestroyed() const {
321 return !handleAlive && parentId == UNASSIGNED_LAYER_ID;
322}
323bool RequestedLayerState::isRoot() const {
324 return canBeRoot && parentId == UNASSIGNED_LAYER_ID;
325}
326bool RequestedLayerState::isHiddenByPolicy() const {
327 return (flags & layer_state_t::eLayerHidden) == layer_state_t::eLayerHidden;
328};
329half4 RequestedLayerState::getColor() const {
330 if ((sidebandStream != nullptr) || (externalTexture != nullptr)) {
331 return {0._hf, 0._hf, 0._hf, color.a};
332 }
333 return color;
334}
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000335Rect RequestedLayerState::getBufferSize(uint32_t displayRotationFlags) const {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000336 // for buffer state layers we use the display frame size as the buffer size.
337 if (!externalTexture) {
338 return Rect::INVALID_RECT;
339 }
340
341 uint32_t bufWidth = externalTexture->getWidth();
342 uint32_t bufHeight = externalTexture->getHeight();
343
344 // Undo any transformations on the buffer and return the result.
345 if (bufferTransform & ui::Transform::ROT_90) {
346 std::swap(bufWidth, bufHeight);
347 }
348
349 if (transformToDisplayInverse) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000350 uint32_t invTransform = displayRotationFlags;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000351 if (invTransform & ui::Transform::ROT_90) {
352 std::swap(bufWidth, bufHeight);
353 }
354 }
355
356 return Rect(0, 0, static_cast<int32_t>(bufWidth), static_cast<int32_t>(bufHeight));
357}
358
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000359Rect RequestedLayerState::getCroppedBufferSize(const Rect& bufferSize) const {
360 Rect size = bufferSize;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000361 if (!crop.isEmpty() && size.isValid()) {
362 size.intersect(crop, &size);
363 } else if (!crop.isEmpty()) {
364 size = crop;
365 }
366 return size;
367}
368
369Rect RequestedLayerState::getBufferCrop() const {
370 // this is the crop rectangle that applies to the buffer
371 // itself (as opposed to the window)
372 if (!bufferCrop.isEmpty()) {
373 // if the buffer crop is defined, we use that
374 return bufferCrop;
375 } else if (externalTexture != nullptr) {
376 // otherwise we use the whole buffer
377 return externalTexture->getBounds();
378 } else {
379 // if we don't have a buffer yet, we use an empty/invalid crop
380 return Rect();
381 }
382}
383
384aidl::android::hardware::graphics::composer3::Composition RequestedLayerState::getCompositionType()
385 const {
386 using aidl::android::hardware::graphics::composer3::Composition;
387 // TODO(b/238781169) check about sidestream ready flag
388 if (sidebandStream.get()) {
389 return Composition::SIDEBAND;
390 }
391 if (!externalTexture) {
392 return Composition::SOLID_COLOR;
393 }
394 if (flags & layer_state_t::eLayerIsDisplayDecoration) {
395 return Composition::DISPLAY_DECORATION;
396 }
397 if (potentialCursor) {
398 return Composition::CURSOR;
399 }
400 return Composition::DEVICE;
401}
402
403Rect RequestedLayerState::reduce(const Rect& win, const Region& exclude) {
404 if (CC_LIKELY(exclude.isEmpty())) {
405 return win;
406 }
407 if (exclude.isRect()) {
408 return win.reduce(exclude.getBounds());
409 }
410 return Region(win).subtract(exclude).getBounds();
411}
412
Vishnu Nair04f89692022-11-16 23:21:05 +0000413// Returns true if the layer has a relative parent that is not its own parent. This is an input
414// error from the client, and this check allows us to handle it gracefully. If both parentId and
415// relativeParentId is unassigned then the layer does not have a valid relative parent.
416// If the relative parentid is unassigned, the layer will be considered relative but won't be
417// reachable.
418bool RequestedLayerState::hasValidRelativeParent() const {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000419 return isRelativeOf &&
420 (parentId != relativeParentId || relativeParentId == UNASSIGNED_LAYER_ID);
Vishnu Nair04f89692022-11-16 23:21:05 +0000421}
422
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000423bool RequestedLayerState::hasInputInfo() const {
424 if (!windowInfoHandle) {
425 return false;
426 }
427 const auto windowInfo = windowInfoHandle->getInfo();
428 return windowInfo->token != nullptr ||
429 windowInfo->inputConfig.test(gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL);
430}
431
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000432} // namespace android::surfaceflinger::frontend