blob: 1a828bf88c9ee6b1137ce27513ad3b594a4309bb [file] [log] [blame]
chaviw1d044282017-09-27 12:19:28 -07001/*
2 * Copyright (C) 2017 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
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
Marin Shalamanovbed7fd32020-12-21 20:02:20 +010020#pragma clang diagnostic ignored "-Wextra"
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080021
chaviw1d044282017-09-27 12:19:28 -070022#include "LayerProtoHelper.h"
23
24namespace android {
chaviw98318de2021-05-19 16:45:23 -050025
26using gui::WindowInfo;
27
chaviw1d044282017-09-27 12:19:28 -070028namespace surfaceflinger {
Nataniel Borges797b0e42019-02-15 14:11:58 -080029
30void LayerProtoHelper::writePositionToProto(const float x, const float y,
31 std::function<PositionProto*()> getPositionProto) {
32 if (x != 0 || y != 0) {
33 // Use a lambda do avoid writing the object header when the object is empty
34 PositionProto* position = getPositionProto();
35 position->set_x(x);
36 position->set_y(y);
37 }
38}
39
40void LayerProtoHelper::writeSizeToProto(const uint32_t w, const uint32_t h,
41 std::function<SizeProto*()> getSizeProto) {
42 if (w != 0 || h != 0) {
43 // Use a lambda do avoid writing the object header when the object is empty
44 SizeProto* size = getSizeProto();
45 size->set_w(w);
46 size->set_h(h);
47 }
48}
49
50void LayerProtoHelper::writeToProto(const Region& region,
51 std::function<RegionProto*()> getRegionProto) {
52 if (region.isEmpty()) {
53 return;
54 }
55
Vishnu Nair6b591152021-10-08 11:45:14 -070056 writeToProto(region, getRegionProto());
57}
58
59void LayerProtoHelper::writeToProto(const Region& region, RegionProto* regionProto) {
60 if (region.isEmpty()) {
61 return;
62 }
63
chaviw1d044282017-09-27 12:19:28 -070064 Region::const_iterator head = region.begin();
65 Region::const_iterator const tail = region.end();
Nataniel Borges797b0e42019-02-15 14:11:58 -080066 // Use a lambda do avoid writing the object header when the object is empty
chaviw1d044282017-09-27 12:19:28 -070067 while (head != tail) {
Vishnu Nair6b591152021-10-08 11:45:14 -070068 writeToProto(*head, regionProto->add_rect());
chaviw1d044282017-09-27 12:19:28 -070069 head++;
70 }
71}
72
Vishnu Nair6b591152021-10-08 11:45:14 -070073void LayerProtoHelper::readFromProto(const RegionProto& regionProto, Region& outRegion) {
74 for (int i = 0; i < regionProto.rect_size(); i++) {
75 Rect rect;
76 readFromProto(regionProto.rect(i), rect);
77 outRegion.orSelf(rect);
78 }
79}
80
Nataniel Borges797b0e42019-02-15 14:11:58 -080081void LayerProtoHelper::writeToProto(const Rect& rect, std::function<RectProto*()> getRectProto) {
82 if (rect.left != 0 || rect.right != 0 || rect.top != 0 || rect.bottom != 0) {
83 // Use a lambda do avoid writing the object header when the object is empty
Vishnu Nair6b591152021-10-08 11:45:14 -070084 writeToProto(rect, getRectProto());
Nataniel Borges797b0e42019-02-15 14:11:58 -080085 }
chaviw1d044282017-09-27 12:19:28 -070086}
87
Vishnu Nair6b591152021-10-08 11:45:14 -070088void LayerProtoHelper::writeToProto(const Rect& rect, RectProto* rectProto) {
89 rectProto->set_left(rect.left);
90 rectProto->set_top(rect.top);
91 rectProto->set_bottom(rect.bottom);
92 rectProto->set_right(rect.right);
93}
94
95void LayerProtoHelper::readFromProto(const RectProto& proto, Rect& outRect) {
96 outRect.left = proto.left();
97 outRect.top = proto.top();
98 outRect.bottom = proto.bottom();
99 outRect.right = proto.right();
100}
101
Nataniel Borges797b0e42019-02-15 14:11:58 -0800102void LayerProtoHelper::writeToProto(const FloatRect& rect,
103 std::function<FloatRectProto*()> getFloatRectProto) {
104 if (rect.left != 0 || rect.right != 0 || rect.top != 0 || rect.bottom != 0) {
105 // Use a lambda do avoid writing the object header when the object is empty
106 FloatRectProto* rectProto = getFloatRectProto();
107 rectProto->set_left(rect.left);
108 rectProto->set_top(rect.top);
109 rectProto->set_bottom(rect.bottom);
110 rectProto->set_right(rect.right);
111 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -0800112}
113
Nataniel Borges797b0e42019-02-15 14:11:58 -0800114void LayerProtoHelper::writeToProto(const half4 color, std::function<ColorProto*()> getColorProto) {
115 if (color.r != 0 || color.g != 0 || color.b != 0 || color.a != 0) {
116 // Use a lambda do avoid writing the object header when the object is empty
117 ColorProto* colorProto = getColorProto();
118 colorProto->set_r(color.r);
119 colorProto->set_g(color.g);
120 colorProto->set_b(color.b);
121 colorProto->set_a(color.a);
122 }
chaviw1d044282017-09-27 12:19:28 -0700123}
124
chaviw0a398992021-08-13 10:13:01 -0500125void LayerProtoHelper::writeToProtoDeprecated(const ui::Transform& transform,
126 TransformProto* transformProto) {
Nataniel Borges84a46182019-02-26 15:36:32 -0800127 const uint32_t type = transform.getType() | (transform.getOrientation() << 8);
Nataniel Borges797b0e42019-02-15 14:11:58 -0800128 transformProto->set_type(type);
129
Nataniel Borges9a2433c2019-03-13 15:02:45 -0700130 // Rotations that are 90/180/270 have their own type so the transform matrix can be
131 // reconstructed later. All other rotation have the type UKNOWN so we need to save the transform
132 // values in that case.
Nataniel Borges84a46182019-02-26 15:36:32 -0800133 if (type & (ui::Transform::SCALE | ui::Transform::UNKNOWN)) {
Nataniel Borges797b0e42019-02-15 14:11:58 -0800134 transformProto->set_dsdx(transform[0][0]);
135 transformProto->set_dtdx(transform[0][1]);
136 transformProto->set_dsdy(transform[1][0]);
137 transformProto->set_dtdy(transform[1][1]);
138 }
chaviw1d044282017-09-27 12:19:28 -0700139}
140
chaviw0a398992021-08-13 10:13:01 -0500141void LayerProtoHelper::writeTransformToProto(const ui::Transform& transform,
142 TransformProto* transformProto) {
143 const uint32_t type = transform.getType() | (transform.getOrientation() << 8);
144 transformProto->set_type(type);
145
146 // Rotations that are 90/180/270 have their own type so the transform matrix can be
147 // reconstructed later. All other rotation have the type UNKNOWN so we need to save the
148 // transform values in that case.
149 if (type & (ui::Transform::SCALE | ui::Transform::UNKNOWN)) {
150 transformProto->set_dsdx(transform.dsdx());
151 transformProto->set_dtdx(transform.dtdx());
152 transformProto->set_dtdy(transform.dtdy());
153 transformProto->set_dsdy(transform.dsdy());
154 }
155}
156
Vishnu Naird37343b2022-01-12 16:18:56 -0800157void LayerProtoHelper::writeToProto(const renderengine::ExternalTexture& buffer,
Nataniel Borges797b0e42019-02-15 14:11:58 -0800158 std::function<ActiveBufferProto*()> getActiveBufferProto) {
Vishnu Naird8f5e9f2022-02-03 10:23:28 -0800159 if (buffer.getWidth() != 0 || buffer.getHeight() != 0 || buffer.getUsage() != 0 ||
160 buffer.getPixelFormat() != 0) {
Nataniel Borges797b0e42019-02-15 14:11:58 -0800161 // Use a lambda do avoid writing the object header when the object is empty
162 ActiveBufferProto* activeBufferProto = getActiveBufferProto();
Vishnu Naird8f5e9f2022-02-03 10:23:28 -0800163 activeBufferProto->set_width(buffer.getWidth());
164 activeBufferProto->set_height(buffer.getHeight());
165 activeBufferProto->set_stride(buffer.getUsage());
166 activeBufferProto->set_format(buffer.getPixelFormat());
Nataniel Borges797b0e42019-02-15 14:11:58 -0800167 }
chaviw1d044282017-09-27 12:19:28 -0700168}
169
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700170void LayerProtoHelper::writeToProto(
chaviw98318de2021-05-19 16:45:23 -0500171 const WindowInfo& inputInfo, const wp<Layer>& touchableRegionBounds,
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700172 std::function<InputWindowInfoProto*()> getInputWindowInfoProto) {
173 if (inputInfo.token == nullptr) {
174 return;
175 }
176
177 InputWindowInfoProto* proto = getInputWindowInfoProto();
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800178 proto->set_layout_params_flags(inputInfo.layoutParamsFlags.get());
chaviw98318de2021-05-19 16:45:23 -0500179 using U = std::underlying_type_t<WindowInfo::Type>;
Michael Wright44753b12020-07-08 13:48:11 +0100180 // TODO(b/129481165): This static assert can be safely removed once conversion warnings
181 // are re-enabled.
182 static_assert(std::is_same_v<U, int32_t>);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800183 proto->set_layout_params_type(static_cast<U>(inputInfo.layoutParamsType));
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700184
185 LayerProtoHelper::writeToProto({inputInfo.frameLeft, inputInfo.frameTop, inputInfo.frameRight,
186 inputInfo.frameBottom},
187 [&]() { return proto->mutable_frame(); });
188 LayerProtoHelper::writeToProto(inputInfo.touchableRegion,
189 [&]() { return proto->mutable_touchable_region(); });
190
191 proto->set_surface_inset(inputInfo.surfaceInset);
Prabir Pradhan4d5c52f2022-01-31 08:52:10 -0800192 using InputConfig = gui::WindowInfo::InputConfig;
193 proto->set_visible(!inputInfo.inputConfig.test(InputConfig::NOT_VISIBLE));
194 proto->set_focusable(!inputInfo.inputConfig.test(InputConfig::NOT_FOCUSABLE));
195 proto->set_has_wallpaper(inputInfo.inputConfig.test(InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER));
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700196
197 proto->set_global_scale_factor(inputInfo.globalScaleFactor);
chaviw0a398992021-08-13 10:13:01 -0500198 LayerProtoHelper::writeToProtoDeprecated(inputInfo.transform, proto->mutable_transform());
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700199 proto->set_replace_touchable_region_with_crop(inputInfo.replaceTouchableRegionWithCrop);
200 auto cropLayer = touchableRegionBounds.promote();
201 if (cropLayer != nullptr) {
202 proto->set_crop_layer_id(cropLayer->sequence);
203 LayerProtoHelper::writeToProto(cropLayer->getScreenBounds(
204 false /* reduceTransparentRegion */),
205 [&]() { return proto->mutable_touchable_region_crop(); });
206 }
207}
208
chaviwddeae262020-01-06 10:31:23 -0800209void LayerProtoHelper::writeToProto(const mat4 matrix, ColorTransformProto* colorTransformProto) {
210 for (int i = 0; i < mat4::ROW_SIZE; i++) {
211 for (int j = 0; j < mat4::COL_SIZE; j++) {
212 colorTransformProto->add_val(matrix[i][j]);
213 }
214 }
215}
216
Vishnu Nair6b591152021-10-08 11:45:14 -0700217void LayerProtoHelper::readFromProto(const ColorTransformProto& colorTransformProto, mat4& matrix) {
218 for (int i = 0; i < mat4::ROW_SIZE; i++) {
219 for (int j = 0; j < mat4::COL_SIZE; j++) {
220 matrix[i][j] = colorTransformProto.val(i * mat4::COL_SIZE + j);
221 }
222 }
223}
224
225void LayerProtoHelper::writeToProto(const android::BlurRegion region, BlurRegion* proto) {
226 proto->set_blur_radius(region.blurRadius);
227 proto->set_corner_radius_tl(region.cornerRadiusTL);
228 proto->set_corner_radius_tr(region.cornerRadiusTR);
229 proto->set_corner_radius_bl(region.cornerRadiusBL);
230 proto->set_corner_radius_br(region.cornerRadiusBR);
231 proto->set_alpha(region.alpha);
232 proto->set_left(region.left);
233 proto->set_top(region.top);
234 proto->set_right(region.right);
235 proto->set_bottom(region.bottom);
236}
237
238void LayerProtoHelper::readFromProto(const BlurRegion& proto, android::BlurRegion& outRegion) {
239 outRegion.blurRadius = proto.blur_radius();
240 outRegion.cornerRadiusTL = proto.corner_radius_tl();
241 outRegion.cornerRadiusTR = proto.corner_radius_tr();
242 outRegion.cornerRadiusBL = proto.corner_radius_bl();
243 outRegion.cornerRadiusBR = proto.corner_radius_br();
244 outRegion.alpha = proto.alpha();
245 outRegion.left = proto.left();
246 outRegion.top = proto.top();
247 outRegion.right = proto.right();
248 outRegion.bottom = proto.bottom();
249}
Vishnu Nairea6ff812023-02-27 17:41:39 +0000250
251void LayerProtoHelper::writeHierarchyToProto(
252 LayersProto& outLayersProto, const frontend::LayerHierarchy& root,
253 const frontend::LayerSnapshotBuilder& snapshotBuilder,
254 const std::unordered_map<uint32_t, sp<Layer>>& legacyLayers, uint32_t traceFlags) {
255 using Variant = frontend::LayerHierarchy::Variant;
256 frontend::LayerSnapshot defaultSnapshot;
257
258 LayerProto* layerProto = outLayersProto.add_layers();
259 const frontend::RequestedLayerState& layer = *root.getLayer();
260 frontend::LayerSnapshot* snapshot = snapshotBuilder.getSnapshot(layer.id);
261
262 if (!snapshot) {
263 snapshot = &defaultSnapshot;
264 }
265 writeSnapshotToProto(layerProto, layer, *snapshot, traceFlags);
266 for (const auto& [child, variant] : root.mChildren) {
267 if (variant == Variant::Attached || variant == Variant::Detached) {
268 layerProto->add_children(child->getLayer()->id);
269 } else if (variant == Variant::Relative) {
270 layerProto->add_relatives(child->getLayer()->id);
271 }
272 }
273
274 auto parent = root.getParent();
275 if (parent && parent->getLayer()) {
276 layerProto->set_parent(parent->getLayer()->id);
277 } else {
278 layerProto->set_parent(-1);
279 }
280
281 auto relativeParent = root.getRelativeParent();
282 if (relativeParent && relativeParent->getLayer()) {
283 layerProto->set_z_order_relative_of(relativeParent->getLayer()->id);
284 } else {
285 layerProto->set_z_order_relative_of(-1);
286 }
287
288 if (traceFlags & LayerTracing::TRACE_COMPOSITION) {
289 auto it = legacyLayers.find(layer.id);
290 if (it != legacyLayers.end()) {
291 it->second->writeCompositionStateToProto(layerProto);
292 }
293 }
294
295 for (const auto& [child, variant] : root.mChildren) {
296 // avoid visiting relative layers twice
297 if (variant == Variant::Detached) {
298 continue;
299 }
300 writeHierarchyToProto(outLayersProto, *child, snapshotBuilder, legacyLayers, traceFlags);
301 }
302}
303
304void LayerProtoHelper::writeSnapshotToProto(LayerProto* layerInfo,
305 const frontend::RequestedLayerState& requestedState,
306 const frontend::LayerSnapshot& snapshot,
307 uint32_t traceFlags) {
308 const ui::Transform transform = snapshot.geomLayerTransform;
309 auto buffer = requestedState.externalTexture;
310 if (buffer != nullptr) {
311 LayerProtoHelper::writeToProto(*buffer,
312 [&]() { return layerInfo->mutable_active_buffer(); });
313 LayerProtoHelper::writeToProtoDeprecated(ui::Transform(requestedState.bufferTransform),
314 layerInfo->mutable_buffer_transform());
315 }
316 layerInfo->set_invalidate(snapshot.contentDirty);
317 layerInfo->set_is_protected(snapshot.hasProtectedContent);
318 layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(snapshot.dataspace)));
319 layerInfo->set_curr_frame(requestedState.bufferData->frameNumber);
320 layerInfo->set_requested_corner_radius(requestedState.cornerRadius);
321 layerInfo->set_corner_radius(
322 (snapshot.roundedCorner.radius.x + snapshot.roundedCorner.radius.y) / 2.0);
323 layerInfo->set_background_blur_radius(snapshot.backgroundBlurRadius);
324 layerInfo->set_is_trusted_overlay(snapshot.isTrustedOverlay);
325 LayerProtoHelper::writeToProtoDeprecated(transform, layerInfo->mutable_transform());
326 LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(),
327 [&]() { return layerInfo->mutable_position(); });
328 LayerProtoHelper::writeToProto(snapshot.geomLayerBounds,
329 [&]() { return layerInfo->mutable_bounds(); });
330 LayerProtoHelper::writeToProto(snapshot.surfaceDamage,
331 [&]() { return layerInfo->mutable_damage_region(); });
332
333 if (requestedState.hasColorTransform) {
334 LayerProtoHelper::writeToProto(snapshot.colorTransform,
335 layerInfo->mutable_color_transform());
336 }
337
338 LayerProtoHelper::writeToProto(snapshot.croppedBufferSize.toFloatRect(),
339 [&]() { return layerInfo->mutable_source_bounds(); });
340 LayerProtoHelper::writeToProto(snapshot.transformedBounds,
341 [&]() { return layerInfo->mutable_screen_bounds(); });
342 LayerProtoHelper::writeToProto(snapshot.roundedCorner.cropRect,
343 [&]() { return layerInfo->mutable_corner_radius_crop(); });
344 layerInfo->set_shadow_radius(snapshot.shadowRadius);
345
346 layerInfo->set_id(requestedState.id);
347 layerInfo->set_name(requestedState.name);
348 layerInfo->set_type("Layer");
349
350 LayerProtoHelper::writeToProto(requestedState.transparentRegion,
351 [&]() { return layerInfo->mutable_transparent_region(); });
352
353 layerInfo->set_layer_stack(snapshot.outputFilter.layerStack.id);
354 layerInfo->set_z(requestedState.z);
355
356 ui::Transform requestedTransform = requestedState.getTransform(0);
357 LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(), [&]() {
358 return layerInfo->mutable_requested_position();
359 });
360
361 LayerProtoHelper::writeToProto(requestedState.crop,
362 [&]() { return layerInfo->mutable_crop(); });
363
364 layerInfo->set_is_opaque(snapshot.contentOpaque);
365 if (requestedState.externalTexture)
366 layerInfo->set_pixel_format(
367 decodePixelFormat(requestedState.externalTexture->getPixelFormat()));
368 LayerProtoHelper::writeToProto(snapshot.color, [&]() { return layerInfo->mutable_color(); });
369 LayerProtoHelper::writeToProto(requestedState.color,
370 [&]() { return layerInfo->mutable_requested_color(); });
371 layerInfo->set_flags(requestedState.flags);
372
373 LayerProtoHelper::writeToProtoDeprecated(requestedTransform,
374 layerInfo->mutable_requested_transform());
375
376 layerInfo->set_is_relative_of(requestedState.isRelativeOf);
377
378 layerInfo->set_owner_uid(requestedState.ownerUid);
379
380 if ((traceFlags & LayerTracing::TRACE_INPUT) && snapshot.hasInputInfo()) {
381 LayerProtoHelper::writeToProto(snapshot.inputInfo, {},
382 [&]() { return layerInfo->mutable_input_window_info(); });
383 }
384
385 if (traceFlags & LayerTracing::TRACE_EXTRA) {
386 auto protoMap = layerInfo->mutable_metadata();
387 for (const auto& entry : requestedState.metadata.mMap) {
388 (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend());
389 }
390 }
391
392 LayerProtoHelper::writeToProto(requestedState.destinationFrame,
393 [&]() { return layerInfo->mutable_destination_frame(); });
394}
395
chaviw1d044282017-09-27 12:19:28 -0700396} // namespace surfaceflinger
397} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800398
399// TODO(b/129481165): remove the #pragma below and fix conversion issues
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100400#pragma clang diagnostic pop // ignored "-Wconversion -Wextra"