blob: 39bf07abd27d0f8e66bcfb887176cadaf5f6af00 [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;
99 dataspaceRequested = false;
100 hdrMetadata.validTypes = 0;
101 surfaceDamageRegion = Region::INVALID_REGION;
102 cornerRadius = 0.0f;
103 backgroundBlurRadius = 0;
104 api = -1;
105 hasColorTransform = false;
106 bufferTransform = 0;
107 requestedTransform.reset();
108 bufferData = std::make_shared<BufferData>();
109 bufferData->frameNumber = 0;
110 bufferData->acquireFence = sp<Fence>::make(-1);
111 acquireFenceTime = std::make_shared<FenceTime>(bufferData->acquireFence);
112 colorSpaceAgnostic = false;
113 frameRateSelectionPriority = Layer::PRIORITY_UNSET;
114 shadowRadius = 0.f;
115 fixedTransformHint = ui::Transform::ROT_INVALID;
116 destinationFrame.makeInvalid();
117 isTrustedOverlay = false;
118 dropInputMode = gui::DropInputMode::NONE;
119 dimmingEnabled = true;
120 defaultFrameRateCompatibility =
121 static_cast<int8_t>(scheduler::LayerInfo::FrameRateCompatibility::Default);
122 dataspace = ui::Dataspace::V0_SRGB;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000123 gameMode = gui::GameMode::Unsupported;
124 requestedFrameRate = {};
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000125}
126
127void RequestedLayerState::merge(const ResolvedComposerState& resolvedComposerState) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000128 const uint32_t oldFlags = flags;
129 const half oldAlpha = color.a;
130 const bool hadBufferOrSideStream = hasValidBuffer() || sidebandStream != nullptr;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000131 const layer_state_t& clientState = resolvedComposerState.state;
132
133 uint64_t clientChanges = what | layer_state_t::diff(clientState);
134 layer_state_t::merge(clientState);
135 what = clientChanges;
136
137 if (clientState.what & layer_state_t::eFlagsChanged) {
138 if ((oldFlags ^ flags) & layer_state_t::eLayerHidden) {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000139 changes |= RequestedLayerState::Changes::Visibility |
140 RequestedLayerState::Changes::VisibleRegion;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000141 }
142 if ((oldFlags ^ flags) & layer_state_t::eIgnoreDestinationFrame) {
143 changes |= RequestedLayerState::Changes::Geometry;
144 }
145 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000146 if (clientState.what &
147 (layer_state_t::eBufferChanged | layer_state_t::eSidebandStreamChanged)) {
148 const bool hasBufferOrSideStream = hasValidBuffer() || sidebandStream != nullptr;
149 if (hadBufferOrSideStream != hasBufferOrSideStream) {
150 changes |= RequestedLayerState::Changes::Geometry |
151 RequestedLayerState::Changes::VisibleRegion |
152 RequestedLayerState::Changes::Visibility | RequestedLayerState::Changes::Input |
153 RequestedLayerState::Changes::Buffer;
154 }
155 }
156 if (what & (layer_state_t::eAlphaChanged)) {
157 if (oldAlpha == 0 || color.a == 0) {
158 changes |= RequestedLayerState::Changes::Visibility |
159 RequestedLayerState::Changes::VisibleRegion;
160 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000161 }
162 if (clientChanges & layer_state_t::HIERARCHY_CHANGES)
163 changes |= RequestedLayerState::Changes::Hierarchy;
164 if (clientChanges & layer_state_t::CONTENT_CHANGES)
165 changes |= RequestedLayerState::Changes::Content;
166 if (clientChanges & layer_state_t::GEOMETRY_CHANGES)
167 changes |= RequestedLayerState::Changes::Geometry;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000168 if (clientChanges & layer_state_t::AFFECTS_CHILDREN)
169 changes |= RequestedLayerState::Changes::AffectsChildren;
Vishnu Naircfb2d252023-01-19 04:44:02 +0000170 if (clientChanges & layer_state_t::INPUT_CHANGES)
171 changes |= RequestedLayerState::Changes::Input;
172 if (clientChanges & layer_state_t::VISIBLE_REGION_CHANGES)
173 changes |= RequestedLayerState::Changes::VisibleRegion;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000174 if (clientState.what & layer_state_t::eColorTransformChanged) {
175 static const mat4 identityMatrix = mat4();
176 hasColorTransform = colorTransform != identityMatrix;
177 }
Vishnu Nair04f89692022-11-16 23:21:05 +0000178 if (clientState.what & (layer_state_t::eLayerChanged | layer_state_t::eRelativeLayerChanged)) {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000179 changes |= RequestedLayerState::Changes::Z;
180 }
181 if (clientState.what & layer_state_t::eReparent) {
182 changes |= RequestedLayerState::Changes::Parent;
183 parentId = getLayerIdFromSurfaceControl(clientState.parentSurfaceControlForChild);
184 parentSurfaceControlForChild = nullptr;
Vishnu Nair04f89692022-11-16 23:21:05 +0000185 // Once a layer has be reparented, it cannot be placed at the root. It sounds odd
186 // but thats the existing logic and until we make this behavior more explicit, we need
187 // to maintain this logic.
188 canBeRoot = false;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000189 }
190 if (clientState.what & layer_state_t::eRelativeLayerChanged) {
191 changes |= RequestedLayerState::Changes::RelativeParent;
192 relativeParentId = getLayerIdFromSurfaceControl(clientState.relativeLayerSurfaceControl);
193 isRelativeOf = true;
194 relativeLayerSurfaceControl = nullptr;
195 }
196 if ((clientState.what & layer_state_t::eLayerChanged ||
197 (clientState.what & layer_state_t::eReparent && parentId == UNASSIGNED_LAYER_ID)) &&
198 isRelativeOf) {
199 // clear out relz data
200 relativeParentId = UNASSIGNED_LAYER_ID;
201 isRelativeOf = false;
202 changes |= RequestedLayerState::Changes::RelativeParent;
203 }
204 if (clientState.what & layer_state_t::eReparent && parentId == relativeParentId) {
205 // provide a hint that we are are now a direct child and not a relative child.
206 changes |= RequestedLayerState::Changes::RelativeParent;
207 }
208 if (clientState.what & layer_state_t::eInputInfoChanged) {
209 wp<IBinder>& touchableRegionCropHandle =
210 windowInfoHandle->editInfo()->touchableRegionCropHandle;
211 touchCropId = LayerHandle::getLayerId(touchableRegionCropHandle.promote());
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000212 touchableRegionCropHandle.clear();
213 }
214 if (clientState.what & layer_state_t::eStretchChanged) {
215 stretchEffect.sanitize();
216 }
217
218 if (clientState.what & layer_state_t::eHasListenerCallbacksChanged) {
219 // TODO(b/238781169) handle callbacks
220 }
221
222 if (clientState.what & layer_state_t::eBufferChanged) {
223 externalTexture = resolvedComposerState.externalTexture;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000224 }
225
226 if (clientState.what & layer_state_t::ePositionChanged) {
227 requestedTransform.set(x, y);
228 }
229
230 if (clientState.what & layer_state_t::eMatrixChanged) {
231 requestedTransform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
232 }
Vishnu Naircfb2d252023-01-19 04:44:02 +0000233 if (clientState.what & layer_state_t::eMetadataChanged) {
234 const int32_t requestedGameMode =
235 clientState.metadata.getInt32(gui::METADATA_GAME_MODE, -1);
236 if (requestedGameMode != -1) {
237 // The transaction will be received on the Task layer and needs to be applied to all
238 // child layers.
239 if (static_cast<int32_t>(gameMode) != requestedGameMode) {
240 gameMode = static_cast<gui::GameMode>(requestedGameMode);
241 changes |= RequestedLayerState::Changes::AffectsChildren;
242 }
243 }
244 }
245 if (clientState.what & layer_state_t::eFrameRateChanged) {
246 const auto compatibility =
247 Layer::FrameRate::convertCompatibility(clientState.frameRateCompatibility);
248 const auto strategy = Layer::FrameRate::convertChangeFrameRateStrategy(
249 clientState.changeFrameRateStrategy);
250 requestedFrameRate =
251 Layer::FrameRate(Fps::fromValue(clientState.frameRate), compatibility, strategy);
252 changes |= RequestedLayerState::Changes::FrameRate;
253 }
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000254}
255
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000256ui::Size RequestedLayerState::getUnrotatedBufferSize(uint32_t displayRotationFlags) const {
257 uint32_t bufferWidth = externalTexture->getWidth();
258 uint32_t bufferHeight = externalTexture->getHeight();
259 // Undo any transformations on the buffer.
260 if (bufferTransform & ui::Transform::ROT_90) {
261 std::swap(bufferWidth, bufferHeight);
262 }
263 if (transformToDisplayInverse) {
264 if (displayRotationFlags & ui::Transform::ROT_90) {
265 std::swap(bufferWidth, bufferHeight);
266 }
267 }
268 return {bufferWidth, bufferHeight};
269}
270
271ui::Transform RequestedLayerState::getTransform(uint32_t displayRotationFlags) const {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000272 if ((flags & layer_state_t::eIgnoreDestinationFrame) || destinationFrame.isEmpty()) {
273 // If destination frame is not set, use the requested transform set via
274 // Transaction::setPosition and Transaction::setMatrix.
275 return requestedTransform;
276 }
277
278 Rect destRect = destinationFrame;
279 int32_t destW = destRect.width();
280 int32_t destH = destRect.height();
281 if (destRect.left < 0) {
282 destRect.left = 0;
283 destRect.right = destW;
284 }
285 if (destRect.top < 0) {
286 destRect.top = 0;
287 destRect.bottom = destH;
288 }
289
290 if (!externalTexture) {
291 ui::Transform transform;
292 transform.set(static_cast<float>(destRect.left), static_cast<float>(destRect.top));
293 return transform;
294 }
295
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000296 ui::Size bufferSize = getUnrotatedBufferSize(displayRotationFlags);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000297
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000298 float sx = static_cast<float>(destW) / static_cast<float>(bufferSize.width);
299 float sy = static_cast<float>(destH) / static_cast<float>(bufferSize.height);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000300 ui::Transform transform;
301 transform.set(sx, 0, 0, sy);
302 transform.set(static_cast<float>(destRect.left), static_cast<float>(destRect.top));
303 return transform;
304}
305
306std::string RequestedLayerState::getDebugString() const {
307 return "[" + std::to_string(id) + "]" + name + ",parent=" + layerIdToString(parentId) +
308 ",relativeParent=" + layerIdToString(relativeParentId) +
309 ",isRelativeOf=" + std::to_string(isRelativeOf) +
310 ",mirrorId=" + layerIdToString(mirrorId) +
Vishnu Nair04f89692022-11-16 23:21:05 +0000311 ",handleAlive=" + std::to_string(handleAlive) + ",z=" + std::to_string(z);
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000312}
313
314std::string RequestedLayerState::getDebugStringShort() const {
315 return "[" + std::to_string(id) + "]" + name;
316}
317
318bool RequestedLayerState::canBeDestroyed() const {
319 return !handleAlive && parentId == UNASSIGNED_LAYER_ID;
320}
321bool RequestedLayerState::isRoot() const {
322 return canBeRoot && parentId == UNASSIGNED_LAYER_ID;
323}
324bool RequestedLayerState::isHiddenByPolicy() const {
325 return (flags & layer_state_t::eLayerHidden) == layer_state_t::eLayerHidden;
326};
327half4 RequestedLayerState::getColor() const {
328 if ((sidebandStream != nullptr) || (externalTexture != nullptr)) {
329 return {0._hf, 0._hf, 0._hf, color.a};
330 }
331 return color;
332}
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000333Rect RequestedLayerState::getBufferSize(uint32_t displayRotationFlags) const {
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000334 // for buffer state layers we use the display frame size as the buffer size.
335 if (!externalTexture) {
336 return Rect::INVALID_RECT;
337 }
338
339 uint32_t bufWidth = externalTexture->getWidth();
340 uint32_t bufHeight = externalTexture->getHeight();
341
342 // Undo any transformations on the buffer and return the result.
343 if (bufferTransform & ui::Transform::ROT_90) {
344 std::swap(bufWidth, bufHeight);
345 }
346
347 if (transformToDisplayInverse) {
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000348 uint32_t invTransform = displayRotationFlags;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000349 if (invTransform & ui::Transform::ROT_90) {
350 std::swap(bufWidth, bufHeight);
351 }
352 }
353
354 return Rect(0, 0, static_cast<int32_t>(bufWidth), static_cast<int32_t>(bufHeight));
355}
356
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000357Rect RequestedLayerState::getCroppedBufferSize(const Rect& bufferSize) const {
358 Rect size = bufferSize;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000359 if (!crop.isEmpty() && size.isValid()) {
360 size.intersect(crop, &size);
361 } else if (!crop.isEmpty()) {
362 size = crop;
363 }
364 return size;
365}
366
367Rect RequestedLayerState::getBufferCrop() const {
368 // this is the crop rectangle that applies to the buffer
369 // itself (as opposed to the window)
370 if (!bufferCrop.isEmpty()) {
371 // if the buffer crop is defined, we use that
372 return bufferCrop;
373 } else if (externalTexture != nullptr) {
374 // otherwise we use the whole buffer
375 return externalTexture->getBounds();
376 } else {
377 // if we don't have a buffer yet, we use an empty/invalid crop
378 return Rect();
379 }
380}
381
382aidl::android::hardware::graphics::composer3::Composition RequestedLayerState::getCompositionType()
383 const {
384 using aidl::android::hardware::graphics::composer3::Composition;
385 // TODO(b/238781169) check about sidestream ready flag
386 if (sidebandStream.get()) {
387 return Composition::SIDEBAND;
388 }
389 if (!externalTexture) {
390 return Composition::SOLID_COLOR;
391 }
392 if (flags & layer_state_t::eLayerIsDisplayDecoration) {
393 return Composition::DISPLAY_DECORATION;
394 }
395 if (potentialCursor) {
396 return Composition::CURSOR;
397 }
398 return Composition::DEVICE;
399}
400
401Rect RequestedLayerState::reduce(const Rect& win, const Region& exclude) {
402 if (CC_LIKELY(exclude.isEmpty())) {
403 return win;
404 }
405 if (exclude.isRect()) {
406 return win.reduce(exclude.getBounds());
407 }
408 return Region(win).subtract(exclude).getBounds();
409}
410
Vishnu Nair04f89692022-11-16 23:21:05 +0000411// Returns true if the layer has a relative parent that is not its own parent. This is an input
412// error from the client, and this check allows us to handle it gracefully. If both parentId and
413// relativeParentId is unassigned then the layer does not have a valid relative parent.
414// If the relative parentid is unassigned, the layer will be considered relative but won't be
415// reachable.
416bool RequestedLayerState::hasValidRelativeParent() const {
Vishnu Naircfb2d252023-01-19 04:44:02 +0000417 return isRelativeOf &&
418 (parentId != relativeParentId || relativeParentId == UNASSIGNED_LAYER_ID);
Vishnu Nair04f89692022-11-16 23:21:05 +0000419}
420
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000421bool RequestedLayerState::hasInputInfo() const {
422 if (!windowInfoHandle) {
423 return false;
424 }
425 const auto windowInfo = windowInfoHandle->getInfo();
426 return windowInfo->token != nullptr ||
427 windowInfo->inputConfig.test(gui::WindowInfo::InputConfig::NO_INPUT_CHANNEL);
428}
429
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000430} // namespace android::surfaceflinger::frontend