blob: 9a3d750670f699e56c1ef6c5f4b8bb6fcff072ef [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
17#include "FrontEnd/LayerCreationArgs.h"
18#define ATRACE_TAG ATRACE_TAG_GRAPHICS
19#undef LOG_TAG
20#define LOG_TAG "RequestedLayerState"
21
22#include <private/android_filesystem_config.h>
23#include <sys/types.h>
24
25#include "Layer.h"
26#include "LayerHandle.h"
27#include "RequestedLayerState.h"
28
29namespace android::surfaceflinger::frontend {
30using ftl::Flags;
31using namespace ftl::flag_operators;
32
33namespace {
34uint32_t getLayerIdFromSurfaceControl(sp<SurfaceControl> surfaceControl) {
35 if (!surfaceControl) {
36 return UNASSIGNED_LAYER_ID;
37 }
38
39 return LayerHandle::getLayerId(surfaceControl->getHandle());
40}
41
42std::string layerIdToString(uint32_t layerId) {
43 return layerId == UNASSIGNED_LAYER_ID ? std::to_string(layerId) : "none";
44}
45
46} // namespace
47
48RequestedLayerState::RequestedLayerState(const LayerCreationArgs& args)
49 : id(args.sequence),
50 name(args.name),
51 canBeRoot(args.addToRoot),
52 layerCreationFlags(args.flags),
53 textureName(args.textureName),
54 ownerUid(args.ownerUid),
55 ownerPid(args.ownerPid) {
56 layerId = static_cast<int32_t>(args.sequence);
57 changes |= RequestedLayerState::Changes::Created;
58 metadata.merge(args.metadata);
59 changes |= RequestedLayerState::Changes::Metadata;
60 handleAlive = true;
61 parentId = LayerHandle::getLayerId(args.parentHandle.promote());
62 mirrorId = LayerHandle::getLayerId(args.mirrorLayerHandle.promote());
63 if (mirrorId != UNASSIGNED_LAYER_ID) {
64 changes |= RequestedLayerState::Changes::Mirror;
65 }
66
67 flags = 0;
68 if (args.flags & ISurfaceComposerClient::eHidden) flags |= layer_state_t::eLayerHidden;
69 if (args.flags & ISurfaceComposerClient::eOpaque) flags |= layer_state_t::eLayerOpaque;
70 if (args.flags & ISurfaceComposerClient::eSecure) flags |= layer_state_t::eLayerSecure;
71 if (args.flags & ISurfaceComposerClient::eSkipScreenshot) {
72 flags |= layer_state_t::eLayerSkipScreenshot;
73 }
74 premultipliedAlpha = !(args.flags & ISurfaceComposerClient::eNonPremultiplied);
75 potentialCursor = args.flags & ISurfaceComposerClient::eCursorWindow;
76 protectedByApp = args.flags & ISurfaceComposerClient::eProtectedByApp;
77 if (args.flags & ISurfaceComposerClient::eNoColorFill) {
78 // Set an invalid color so there is no color fill.
79 // (b/259981098) use an explicit flag instead of relying on invalid values.
80 color.r = -1.0_hf;
81 color.g = -1.0_hf;
82 color.b = -1.0_hf;
83 } else {
84 color.rgb = {0.0_hf, 0.0_hf, 0.0_hf};
85 }
86 color.a = 1.0f;
87
88 crop.makeInvalid();
89 z = 0;
90 layerStack = ui::DEFAULT_LAYER_STACK;
91 transformToDisplayInverse = false;
92 dataspace = ui::Dataspace::UNKNOWN;
93 dataspaceRequested = false;
94 hdrMetadata.validTypes = 0;
95 surfaceDamageRegion = Region::INVALID_REGION;
96 cornerRadius = 0.0f;
97 backgroundBlurRadius = 0;
98 api = -1;
99 hasColorTransform = false;
100 bufferTransform = 0;
101 requestedTransform.reset();
102 bufferData = std::make_shared<BufferData>();
103 bufferData->frameNumber = 0;
104 bufferData->acquireFence = sp<Fence>::make(-1);
105 acquireFenceTime = std::make_shared<FenceTime>(bufferData->acquireFence);
106 colorSpaceAgnostic = false;
107 frameRateSelectionPriority = Layer::PRIORITY_UNSET;
108 shadowRadius = 0.f;
109 fixedTransformHint = ui::Transform::ROT_INVALID;
110 destinationFrame.makeInvalid();
111 isTrustedOverlay = false;
112 dropInputMode = gui::DropInputMode::NONE;
113 dimmingEnabled = true;
114 defaultFrameRateCompatibility =
115 static_cast<int8_t>(scheduler::LayerInfo::FrameRateCompatibility::Default);
116 dataspace = ui::Dataspace::V0_SRGB;
117}
118
119void RequestedLayerState::merge(const ResolvedComposerState& resolvedComposerState) {
120 bool oldFlags = flags;
121 Rect oldBufferSize = getBufferSize();
122 const layer_state_t& clientState = resolvedComposerState.state;
123
124 uint64_t clientChanges = what | layer_state_t::diff(clientState);
125 layer_state_t::merge(clientState);
126 what = clientChanges;
127
128 if (clientState.what & layer_state_t::eFlagsChanged) {
129 if ((oldFlags ^ flags) & layer_state_t::eLayerHidden) {
130 changes |= RequestedLayerState::Changes::Visibility;
131 }
132 if ((oldFlags ^ flags) & layer_state_t::eIgnoreDestinationFrame) {
133 changes |= RequestedLayerState::Changes::Geometry;
134 }
135 }
136 if (clientState.what & layer_state_t::eBufferChanged && oldBufferSize != getBufferSize()) {
137 changes |= RequestedLayerState::Changes::Geometry;
138 }
139 if (clientChanges & layer_state_t::HIERARCHY_CHANGES)
140 changes |= RequestedLayerState::Changes::Hierarchy;
141 if (clientChanges & layer_state_t::CONTENT_CHANGES)
142 changes |= RequestedLayerState::Changes::Content;
143 if (clientChanges & layer_state_t::GEOMETRY_CHANGES)
144 changes |= RequestedLayerState::Changes::Geometry;
145
146 if (clientState.what & layer_state_t::eColorTransformChanged) {
147 static const mat4 identityMatrix = mat4();
148 hasColorTransform = colorTransform != identityMatrix;
149 }
150 if (clientState.what & layer_state_t::eLayerChanged) {
151 changes |= RequestedLayerState::Changes::Z;
152 }
153 if (clientState.what & layer_state_t::eReparent) {
154 changes |= RequestedLayerState::Changes::Parent;
155 parentId = getLayerIdFromSurfaceControl(clientState.parentSurfaceControlForChild);
156 parentSurfaceControlForChild = nullptr;
157 }
158 if (clientState.what & layer_state_t::eRelativeLayerChanged) {
159 changes |= RequestedLayerState::Changes::RelativeParent;
160 relativeParentId = getLayerIdFromSurfaceControl(clientState.relativeLayerSurfaceControl);
161 isRelativeOf = true;
162 relativeLayerSurfaceControl = nullptr;
163 }
164 if ((clientState.what & layer_state_t::eLayerChanged ||
165 (clientState.what & layer_state_t::eReparent && parentId == UNASSIGNED_LAYER_ID)) &&
166 isRelativeOf) {
167 // clear out relz data
168 relativeParentId = UNASSIGNED_LAYER_ID;
169 isRelativeOf = false;
170 changes |= RequestedLayerState::Changes::RelativeParent;
171 }
172 if (clientState.what & layer_state_t::eReparent && parentId == relativeParentId) {
173 // provide a hint that we are are now a direct child and not a relative child.
174 changes |= RequestedLayerState::Changes::RelativeParent;
175 }
176 if (clientState.what & layer_state_t::eInputInfoChanged) {
177 wp<IBinder>& touchableRegionCropHandle =
178 windowInfoHandle->editInfo()->touchableRegionCropHandle;
179 touchCropId = LayerHandle::getLayerId(touchableRegionCropHandle.promote());
180 changes |= RequestedLayerState::Changes::Input;
181 touchableRegionCropHandle.clear();
182 }
183 if (clientState.what & layer_state_t::eStretchChanged) {
184 stretchEffect.sanitize();
185 }
186
187 if (clientState.what & layer_state_t::eHasListenerCallbacksChanged) {
188 // TODO(b/238781169) handle callbacks
189 }
190
191 if (clientState.what & layer_state_t::eBufferChanged) {
192 externalTexture = resolvedComposerState.externalTexture;
Vishnu Nairdc4d31b2022-11-17 03:20:58 +0000193 }
194
195 if (clientState.what & layer_state_t::ePositionChanged) {
196 requestedTransform.set(x, y);
197 }
198
199 if (clientState.what & layer_state_t::eMatrixChanged) {
200 requestedTransform.set(matrix.dsdx, matrix.dtdy, matrix.dtdx, matrix.dsdy);
201 }
202}
203
204ui::Transform RequestedLayerState::getTransform() const {
205 if ((flags & layer_state_t::eIgnoreDestinationFrame) || destinationFrame.isEmpty()) {
206 // If destination frame is not set, use the requested transform set via
207 // Transaction::setPosition and Transaction::setMatrix.
208 return requestedTransform;
209 }
210
211 Rect destRect = destinationFrame;
212 int32_t destW = destRect.width();
213 int32_t destH = destRect.height();
214 if (destRect.left < 0) {
215 destRect.left = 0;
216 destRect.right = destW;
217 }
218 if (destRect.top < 0) {
219 destRect.top = 0;
220 destRect.bottom = destH;
221 }
222
223 if (!externalTexture) {
224 ui::Transform transform;
225 transform.set(static_cast<float>(destRect.left), static_cast<float>(destRect.top));
226 return transform;
227 }
228
229 uint32_t bufferWidth = externalTexture->getWidth();
230 uint32_t bufferHeight = externalTexture->getHeight();
231 // Undo any transformations on the buffer.
232 if (bufferTransform & ui::Transform::ROT_90) {
233 std::swap(bufferWidth, bufferHeight);
234 }
235 // TODO(b/238781169) remove dep
236 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
237 if (transformToDisplayInverse) {
238 if (invTransform & ui::Transform::ROT_90) {
239 std::swap(bufferWidth, bufferHeight);
240 }
241 }
242
243 float sx = static_cast<float>(destW) / static_cast<float>(bufferWidth);
244 float sy = static_cast<float>(destH) / static_cast<float>(bufferHeight);
245 ui::Transform transform;
246 transform.set(sx, 0, 0, sy);
247 transform.set(static_cast<float>(destRect.left), static_cast<float>(destRect.top));
248 return transform;
249}
250
251std::string RequestedLayerState::getDebugString() const {
252 return "[" + std::to_string(id) + "]" + name + ",parent=" + layerIdToString(parentId) +
253 ",relativeParent=" + layerIdToString(relativeParentId) +
254 ",isRelativeOf=" + std::to_string(isRelativeOf) +
255 ",mirrorId=" + layerIdToString(mirrorId) +
256 ",handleAlive=" + std::to_string(handleAlive);
257}
258
259std::string RequestedLayerState::getDebugStringShort() const {
260 return "[" + std::to_string(id) + "]" + name;
261}
262
263bool RequestedLayerState::canBeDestroyed() const {
264 return !handleAlive && parentId == UNASSIGNED_LAYER_ID;
265}
266bool RequestedLayerState::isRoot() const {
267 return canBeRoot && parentId == UNASSIGNED_LAYER_ID;
268}
269bool RequestedLayerState::isHiddenByPolicy() const {
270 return (flags & layer_state_t::eLayerHidden) == layer_state_t::eLayerHidden;
271};
272half4 RequestedLayerState::getColor() const {
273 if ((sidebandStream != nullptr) || (externalTexture != nullptr)) {
274 return {0._hf, 0._hf, 0._hf, color.a};
275 }
276 return color;
277}
278Rect RequestedLayerState::getBufferSize() const {
279 // for buffer state layers we use the display frame size as the buffer size.
280 if (!externalTexture) {
281 return Rect::INVALID_RECT;
282 }
283
284 uint32_t bufWidth = externalTexture->getWidth();
285 uint32_t bufHeight = externalTexture->getHeight();
286
287 // Undo any transformations on the buffer and return the result.
288 if (bufferTransform & ui::Transform::ROT_90) {
289 std::swap(bufWidth, bufHeight);
290 }
291
292 if (transformToDisplayInverse) {
293 // TODO(b/238781169) pass in display metrics (would be useful for input info as well
294 uint32_t invTransform = DisplayDevice::getPrimaryDisplayRotationFlags();
295 if (invTransform & ui::Transform::ROT_90) {
296 std::swap(bufWidth, bufHeight);
297 }
298 }
299
300 return Rect(0, 0, static_cast<int32_t>(bufWidth), static_cast<int32_t>(bufHeight));
301}
302
303Rect RequestedLayerState::getCroppedBufferSize() const {
304 Rect size = getBufferSize();
305 if (!crop.isEmpty() && size.isValid()) {
306 size.intersect(crop, &size);
307 } else if (!crop.isEmpty()) {
308 size = crop;
309 }
310 return size;
311}
312
313Rect RequestedLayerState::getBufferCrop() const {
314 // this is the crop rectangle that applies to the buffer
315 // itself (as opposed to the window)
316 if (!bufferCrop.isEmpty()) {
317 // if the buffer crop is defined, we use that
318 return bufferCrop;
319 } else if (externalTexture != nullptr) {
320 // otherwise we use the whole buffer
321 return externalTexture->getBounds();
322 } else {
323 // if we don't have a buffer yet, we use an empty/invalid crop
324 return Rect();
325 }
326}
327
328aidl::android::hardware::graphics::composer3::Composition RequestedLayerState::getCompositionType()
329 const {
330 using aidl::android::hardware::graphics::composer3::Composition;
331 // TODO(b/238781169) check about sidestream ready flag
332 if (sidebandStream.get()) {
333 return Composition::SIDEBAND;
334 }
335 if (!externalTexture) {
336 return Composition::SOLID_COLOR;
337 }
338 if (flags & layer_state_t::eLayerIsDisplayDecoration) {
339 return Composition::DISPLAY_DECORATION;
340 }
341 if (potentialCursor) {
342 return Composition::CURSOR;
343 }
344 return Composition::DEVICE;
345}
346
347Rect RequestedLayerState::reduce(const Rect& win, const Region& exclude) {
348 if (CC_LIKELY(exclude.isEmpty())) {
349 return win;
350 }
351 if (exclude.isRect()) {
352 return win.reduce(exclude.getBounds());
353 }
354 return Region(win).subtract(exclude).getBounds();
355}
356
357} // namespace android::surfaceflinger::frontend