blob: d63b12645233a1bb289f54a8f8eb309630d9b9a9 [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
Vishnu Naira9c43762023-01-27 19:10:25 +000048std::string layerIdsToString(const std::vector<uint32_t>& layerIds) {
49 std::stringstream stream;
50 stream << "{";
51 for (auto layerId : layerIds) {
52 stream << layerId << ",";
53 }
54 stream << "}";
55 return stream.str();
56}
57
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000058} // namespace
59
60RequestedLayerState::RequestedLayerState(const LayerCreationArgs& args)
61 : id(args.sequence),
Vishnu Naircfb2d252023-01-19 04:44:02 +000062 name(args.name + "#" + std::to_string(args.sequence)),
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000063 canBeRoot(args.addToRoot),
64 layerCreationFlags(args.flags),
65 textureName(args.textureName),
66 ownerUid(args.ownerUid),
67 ownerPid(args.ownerPid) {
68 layerId = static_cast<int32_t>(args.sequence);
69 changes |= RequestedLayerState::Changes::Created;
70 metadata.merge(args.metadata);
71 changes |= RequestedLayerState::Changes::Metadata;
72 handleAlive = true;
73 parentId = LayerHandle::getLayerId(args.parentHandle.promote());
Vishnu Naircfb2d252023-01-19 04:44:02 +000074 if (args.parentHandle != nullptr) {
75 canBeRoot = false;
76 }
Vishnu Naira9c43762023-01-27 19:10:25 +000077 layerIdToMirror = LayerHandle::getLayerId(args.mirrorLayerHandle.promote());
78 if (layerIdToMirror != UNASSIGNED_LAYER_ID) {
79 changes |= RequestedLayerState::Changes::Mirror;
80 } else if (args.layerStackToMirror != ui::INVALID_LAYER_STACK) {
81 layerStackToMirror = args.layerStackToMirror;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +000082 changes |= RequestedLayerState::Changes::Mirror;
83 }
84
85 flags = 0;
86 if (args.flags & ISurfaceComposerClient::eHidden) flags |= layer_state_t::eLayerHidden;
87 if (args.flags & ISurfaceComposerClient::eOpaque) flags |= layer_state_t::eLayerOpaque;
88 if (args.flags & ISurfaceComposerClient::eSecure) flags |= layer_state_t::eLayerSecure;
89 if (args.flags & ISurfaceComposerClient::eSkipScreenshot) {
90 flags |= layer_state_t::eLayerSkipScreenshot;
91 }
92 premultipliedAlpha = !(args.flags & ISurfaceComposerClient::eNonPremultiplied);
93 potentialCursor = args.flags & ISurfaceComposerClient::eCursorWindow;
94 protectedByApp = args.flags & ISurfaceComposerClient::eProtectedByApp;
95 if (args.flags & ISurfaceComposerClient::eNoColorFill) {
96 // Set an invalid color so there is no color fill.
97 // (b/259981098) use an explicit flag instead of relying on invalid values.
98 color.r = -1.0_hf;
99 color.g = -1.0_hf;
100 color.b = -1.0_hf;
101 } else {
102 color.rgb = {0.0_hf, 0.0_hf, 0.0_hf};
103 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000104 LLOGV(layerId, "Created %s flags=%d", getDebugString().c_str(), flags);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000105 color.a = 1.0f;
106
107 crop.makeInvalid();
108 z = 0;
109 layerStack = ui::DEFAULT_LAYER_STACK;
110 transformToDisplayInverse = false;
111 dataspace = ui::Dataspace::UNKNOWN;
John Reck68796592023-01-25 13:47:12 -0500112 desiredSdrHdrRatio = 1.f;
113 currentSdrHdrRatio = 1.f;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000114 dataspaceRequested = false;
115 hdrMetadata.validTypes = 0;
116 surfaceDamageRegion = Region::INVALID_REGION;
117 cornerRadius = 0.0f;
118 backgroundBlurRadius = 0;
119 api = -1;
120 hasColorTransform = false;
121 bufferTransform = 0;
122 requestedTransform.reset();
123 bufferData = std::make_shared<BufferData>();
124 bufferData->frameNumber = 0;
125 bufferData->acquireFence = sp<Fence>::make(-1);
126 acquireFenceTime = std::make_shared<FenceTime>(bufferData->acquireFence);
127 colorSpaceAgnostic = false;
128 frameRateSelectionPriority = Layer::PRIORITY_UNSET;
129 shadowRadius = 0.f;
130 fixedTransformHint = ui::Transform::ROT_INVALID;
131 destinationFrame.makeInvalid();
132 isTrustedOverlay = false;
133 dropInputMode = gui::DropInputMode::NONE;
134 dimmingEnabled = true;
135 defaultFrameRateCompatibility =
136 static_cast<int8_t>(scheduler::LayerInfo::FrameRateCompatibility::Default);
137 dataspace = ui::Dataspace::V0_SRGB;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000138 gameMode = gui::GameMode::Unsupported;
139 requestedFrameRate = {};
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;
145 const bool hadBufferOrSideStream = hasValidBuffer() || sidebandStream != nullptr;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000146 const layer_state_t& clientState = resolvedComposerState.state;
147
148 uint64_t clientChanges = what | layer_state_t::diff(clientState);
149 layer_state_t::merge(clientState);
150 what = clientChanges;
151
152 if (clientState.what & layer_state_t::eFlagsChanged) {
153 if ((oldFlags ^ flags) & layer_state_t::eLayerHidden) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000154 changes |= RequestedLayerState::Changes::Visibility |
155 RequestedLayerState::Changes::VisibleRegion;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000156 }
157 if ((oldFlags ^ flags) & layer_state_t::eIgnoreDestinationFrame) {
158 changes |= RequestedLayerState::Changes::Geometry;
159 }
160 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000161 if (clientState.what &
162 (layer_state_t::eBufferChanged | layer_state_t::eSidebandStreamChanged)) {
163 const bool hasBufferOrSideStream = hasValidBuffer() || sidebandStream != nullptr;
164 if (hadBufferOrSideStream != hasBufferOrSideStream) {
165 changes |= RequestedLayerState::Changes::Geometry |
166 RequestedLayerState::Changes::VisibleRegion |
167 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Input |
168 RequestedLayerState::Changes::Buffer;
169 }
170 }
171 if (what & (layer_state_t::eAlphaChanged)) {
172 if (oldAlpha == 0 || color.a == 0) {
173 changes |= RequestedLayerState::Changes::Visibility |
174 RequestedLayerState::Changes::VisibleRegion;
175 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000176 }
177 if (clientChanges & layer_state_t::HIERARCHY_CHANGES)
178 changes |= RequestedLayerState::Changes::Hierarchy;
179 if (clientChanges & layer_state_t::CONTENT_CHANGES)
180 changes |= RequestedLayerState::Changes::Content;
181 if (clientChanges & layer_state_t::GEOMETRY_CHANGES)
182 changes |= RequestedLayerState::Changes::Geometry;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000183 if (clientChanges & layer_state_t::AFFECTS_CHILDREN)
184 changes |= RequestedLayerState::Changes::AffectsChildren;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000185 if (clientChanges & layer_state_t::INPUT_CHANGES)
186 changes |= RequestedLayerState::Changes::Input;
187 if (clientChanges & layer_state_t::VISIBLE_REGION_CHANGES)
188 changes |= RequestedLayerState::Changes::VisibleRegion;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000189 if (clientState.what & layer_state_t::eColorTransformChanged) {
190 static const mat4 identityMatrix = mat4();
191 hasColorTransform = colorTransform != identityMatrix;
192 }
Vishnu Nair04f89692022-11-16 23:21:05 +0000193 if (clientState.what & (layer_state_t::eLayerChanged | layer_state_t::eRelativeLayerChanged)) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000194 changes |= RequestedLayerState::Changes::Z;
195 }
196 if (clientState.what & layer_state_t::eReparent) {
197 changes |= RequestedLayerState::Changes::Parent;
198 parentId = getLayerIdFromSurfaceControl(clientState.parentSurfaceControlForChild);
199 parentSurfaceControlForChild = nullptr;
Vishnu Nair04f89692022-11-16 23:21:05 +0000200 // Once a layer has be reparented, it cannot be placed at the root. It sounds odd
201 // but thats the existing logic and until we make this behavior more explicit, we need
202 // to maintain this logic.
203 canBeRoot = false;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000204 }
205 if (clientState.what & layer_state_t::eRelativeLayerChanged) {
206 changes |= RequestedLayerState::Changes::RelativeParent;
207 relativeParentId = getLayerIdFromSurfaceControl(clientState.relativeLayerSurfaceControl);
208 isRelativeOf = true;
209 relativeLayerSurfaceControl = nullptr;
210 }
211 if ((clientState.what & layer_state_t::eLayerChanged ||
212 (clientState.what & layer_state_t::eReparent && parentId == UNASSIGNED_LAYER_ID)) &&
213 isRelativeOf) {
214 // clear out relz data
215 relativeParentId = UNASSIGNED_LAYER_ID;
216 isRelativeOf = false;
217 changes |= RequestedLayerState::Changes::RelativeParent;
218 }
219 if (clientState.what & layer_state_t::eReparent && parentId == relativeParentId) {
220 // provide a hint that we are are now a direct child and not a relative child.
221 changes |= RequestedLayerState::Changes::RelativeParent;
222 }
223 if (clientState.what & layer_state_t::eInputInfoChanged) {
224 wp<IBinder>& touchableRegionCropHandle =
225 windowInfoHandle->editInfo()->touchableRegionCropHandle;
226 touchCropId = LayerHandle::getLayerId(touchableRegionCropHandle.promote());
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000227 touchableRegionCropHandle.clear();
228 }
229 if (clientState.what & layer_state_t::eStretchChanged) {
230 stretchEffect.sanitize();
231 }
232
233 if (clientState.what & layer_state_t::eHasListenerCallbacksChanged) {
234 // TODO(b/238781169) handle callbacks
235 }
236
237 if (clientState.what & layer_state_t::eBufferChanged) {
238 externalTexture = resolvedComposerState.externalTexture;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000239 }
240
241 if (clientState.what & layer_state_t::ePositionChanged) {
242 requestedTransform.set(x, y);
243 }
244
245 if (clientState.what & layer_state_t::eMatrixChanged) {
246 requestedTransform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
247 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000248 if (clientState.what & layer_state_t::eMetadataChanged) {
249 const int32_t requestedGameMode =
250 clientState.metadata.getInt32(gui::METADATA_GAME_MODE, -1);
251 if (requestedGameMode != -1) {
252 // The transaction will be received on the Task layer and needs to be applied to all
253 // child layers.
254 if (static_cast<int32_t>(gameMode) != requestedGameMode) {
255 gameMode = static_cast<gui::GameMode>(requestedGameMode);
256 changes |= RequestedLayerState::Changes::AffectsChildren;
257 }
258 }
259 }
260 if (clientState.what & layer_state_t::eFrameRateChanged) {
261 const auto compatibility =
262 Layer::FrameRate::convertCompatibility(clientState.frameRateCompatibility);
263 const auto strategy = Layer::FrameRate::convertChangeFrameRateStrategy(
264 clientState.changeFrameRateStrategy);
265 requestedFrameRate =
266 Layer::FrameRate(Fps::fromValue(clientState.frameRate), compatibility, strategy);
267 changes |= RequestedLayerState::Changes::FrameRate;
268 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000269}
270
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000271ui::Size RequestedLayerState::getUnrotatedBufferSize(uint32_t displayRotationFlags) const {
272 uint32_t bufferWidth = externalTexture->getWidth();
273 uint32_t bufferHeight = externalTexture->getHeight();
274 // Undo any transformations on the buffer.
275 if (bufferTransform & ui::Transform::ROT_90) {
276 std::swap(bufferWidth, bufferHeight);
277 }
278 if (transformToDisplayInverse) {
279 if (displayRotationFlags & ui::Transform::ROT_90) {
280 std::swap(bufferWidth, bufferHeight);
281 }
282 }
283 return {bufferWidth, bufferHeight};
284}
285
286ui::Transform RequestedLayerState::getTransform(uint32_t displayRotationFlags) const {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000287 if ((flags & layer_state_t::eIgnoreDestinationFrame) || destinationFrame.isEmpty()) {
288 // If destination frame is not set, use the requested transform set via
289 // Transaction::setPosition and Transaction::setMatrix.
290 return requestedTransform;
291 }
292
293 Rect destRect = destinationFrame;
294 int32_t destW = destRect.width();
295 int32_t destH = destRect.height();
296 if (destRect.left < 0) {
297 destRect.left = 0;
298 destRect.right = destW;
299 }
300 if (destRect.top < 0) {
301 destRect.top = 0;
302 destRect.bottom = destH;
303 }
304
305 if (!externalTexture) {
306 ui::Transform transform;
307 transform.set(static_cast<float>(destRect.left), static_cast<float>(destRect.top));
308 return transform;
309 }
310
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000311 ui::Size bufferSize = getUnrotatedBufferSize(displayRotationFlags);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000312
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000313 float sx = static_cast<float>(destW) / static_cast<float>(bufferSize.width);
314 float sy = static_cast<float>(destH) / static_cast<float>(bufferSize.height);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000315 ui::Transform transform;
316 transform.set(sx, 0, 0, sy);
317 transform.set(static_cast<float>(destRect.left), static_cast<float>(destRect.top));
318 return transform;
319}
320
321std::string RequestedLayerState::getDebugString() const {
322 return "[" + std::to_string(id) + "]" + name + ",parent=" + layerIdToString(parentId) +
323 ",relativeParent=" + layerIdToString(relativeParentId) +
324 ",isRelativeOf=" + std::to_string(isRelativeOf) +
Vishnu Naira9c43762023-01-27 19:10:25 +0000325 ",mirrorIds=" + layerIdsToString(mirrorIds) +
Vishnu Nair04f89692022-11-16 23:21:05 +0000326 ",handleAlive=" + std::to_string(handleAlive) + ",z=" + std::to_string(z);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000327}
328
329std::string RequestedLayerState::getDebugStringShort() const {
330 return "[" + std::to_string(id) + "]" + name;
331}
332
333bool RequestedLayerState::canBeDestroyed() const {
334 return !handleAlive && parentId == UNASSIGNED_LAYER_ID;
335}
336bool RequestedLayerState::isRoot() const {
337 return canBeRoot && parentId == UNASSIGNED_LAYER_ID;
338}
339bool RequestedLayerState::isHiddenByPolicy() const {
340 return (flags & layer_state_t::eLayerHidden) == layer_state_t::eLayerHidden;
341};
342half4 RequestedLayerState::getColor() const {
343 if ((sidebandStream != nullptr) || (externalTexture != nullptr)) {
344 return {0._hf, 0._hf, 0._hf, color.a};
345 }
346 return color;
347}
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000348Rect RequestedLayerState::getBufferSize(uint32_t displayRotationFlags) const {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000349 // for buffer state layers we use the display frame size as the buffer size.
350 if (!externalTexture) {
351 return Rect::INVALID_RECT;
352 }
353
354 uint32_t bufWidth = externalTexture->getWidth();
355 uint32_t bufHeight = externalTexture->getHeight();
356
357 // Undo any transformations on the buffer and return the result.
358 if (bufferTransform & ui::Transform::ROT_90) {
359 std::swap(bufWidth, bufHeight);
360 }
361
362 if (transformToDisplayInverse) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000363 uint32_t invTransform = displayRotationFlags;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000364 if (invTransform & ui::Transform::ROT_90) {
365 std::swap(bufWidth, bufHeight);
366 }
367 }
368
369 return Rect(0, 0, static_cast<int32_t>(bufWidth), static_cast<int32_t>(bufHeight));
370}
371
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000372Rect RequestedLayerState::getCroppedBufferSize(const Rect& bufferSize) const {
373 Rect size = bufferSize;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000374 if (!crop.isEmpty() && size.isValid()) {
375 size.intersect(crop, &size);
376 } else if (!crop.isEmpty()) {
377 size = crop;
378 }
379 return size;
380}
381
382Rect RequestedLayerState::getBufferCrop() const {
383 // this is the crop rectangle that applies to the buffer
384 // itself (as opposed to the window)
385 if (!bufferCrop.isEmpty()) {
386 // if the buffer crop is defined, we use that
387 return bufferCrop;
388 } else if (externalTexture != nullptr) {
389 // otherwise we use the whole buffer
390 return externalTexture->getBounds();
391 } else {
392 // if we don't have a buffer yet, we use an empty/invalid crop
393 return Rect();
394 }
395}
396
397aidl::android::hardware::graphics::composer3::Composition RequestedLayerState::getCompositionType()
398 const {
399 using aidl::android::hardware::graphics::composer3::Composition;
400 // TODO(b/238781169) check about sidestream ready flag
401 if (sidebandStream.get()) {
402 return Composition::SIDEBAND;
403 }
404 if (!externalTexture) {
405 return Composition::SOLID_COLOR;
406 }
407 if (flags & layer_state_t::eLayerIsDisplayDecoration) {
408 return Composition::DISPLAY_DECORATION;
409 }
410 if (potentialCursor) {
411 return Composition::CURSOR;
412 }
413 return Composition::DEVICE;
414}
415
416Rect RequestedLayerState::reduce(const Rect& win, const Region& exclude) {
417 if (CC_LIKELY(exclude.isEmpty())) {
418 return win;
419 }
420 if (exclude.isRect()) {
421 return win.reduce(exclude.getBounds());
422 }
423 return Region(win).subtract(exclude).getBounds();
424}
425
Vishnu Nair04f89692022-11-16 23:21:05 +0000426// Returns true if the layer has a relative parent that is not its own parent. This is an input
427// error from the client, and this check allows us to handle it gracefully. If both parentId and
428// relativeParentId is unassigned then the layer does not have a valid relative parent.
429// If the relative parentid is unassigned, the layer will be considered relative but won't be
430// reachable.
431bool RequestedLayerState::hasValidRelativeParent() const {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000432 return isRelativeOf &&
433 (parentId != relativeParentId || relativeParentId == UNASSIGNED_LAYER_ID);
Vishnu Nair04f89692022-11-16 23:21:05 +0000434}
435
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000436bool RequestedLayerState::hasInputInfo() const {
437 if (!windowInfoHandle) {
438 return false;
439 }
440 const auto windowInfo = windowInfoHandle->getInfo();
441 return windowInfo->token != nullptr ||
442 windowInfo->inputConfig.test(gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL);
443}
444
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000445} // namespace android::surfaceflinger::frontend