chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 1 | /* |
| 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 Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 17 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
Vishnu Nair | d018360 | 2023-03-16 18:52:15 +0000 | [diff] [blame] | 18 | #include "FrontEnd/LayerCreationArgs.h" |
| 19 | #include "FrontEnd/LayerSnapshot.h" |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 20 | #pragma clang diagnostic push |
| 21 | #pragma clang diagnostic ignored "-Wconversion" |
Marin Shalamanov | bed7fd3 | 2020-12-21 20:02:20 +0100 | [diff] [blame] | 22 | #pragma clang diagnostic ignored "-Wextra" |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 23 | |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 24 | #include "LayerProtoHelper.h" |
| 25 | |
| 26 | namespace android { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 27 | |
| 28 | using gui::WindowInfo; |
| 29 | |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 30 | namespace surfaceflinger { |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 31 | |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 32 | void LayerProtoHelper::writePositionToProto( |
| 33 | const float x, const float y, |
| 34 | std::function<perfetto::protos::PositionProto*()> getPositionProto) { |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 35 | if (x != 0 || y != 0) { |
| 36 | // Use a lambda do avoid writing the object header when the object is empty |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 37 | perfetto::protos::PositionProto* position = getPositionProto(); |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 38 | position->set_x(x); |
| 39 | position->set_y(y); |
| 40 | } |
| 41 | } |
| 42 | |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 43 | void LayerProtoHelper::writeSizeToProto( |
| 44 | const uint32_t w, const uint32_t h, |
| 45 | std::function<perfetto::protos::SizeProto*()> getSizeProto) { |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 46 | if (w != 0 || h != 0) { |
| 47 | // Use a lambda do avoid writing the object header when the object is empty |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 48 | perfetto::protos::SizeProto* size = getSizeProto(); |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 49 | size->set_w(w); |
| 50 | size->set_h(h); |
| 51 | } |
| 52 | } |
| 53 | |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 54 | void LayerProtoHelper::writeToProto( |
| 55 | const Region& region, std::function<perfetto::protos::RegionProto*()> getRegionProto) { |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 56 | if (region.isEmpty()) { |
| 57 | return; |
| 58 | } |
| 59 | |
Vishnu Nair | 6b59115 | 2021-10-08 11:45:14 -0700 | [diff] [blame] | 60 | writeToProto(region, getRegionProto()); |
| 61 | } |
| 62 | |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 63 | void LayerProtoHelper::writeToProto(const Region& region, |
| 64 | perfetto::protos::RegionProto* regionProto) { |
Vishnu Nair | 6b59115 | 2021-10-08 11:45:14 -0700 | [diff] [blame] | 65 | if (region.isEmpty()) { |
| 66 | return; |
| 67 | } |
| 68 | |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 69 | Region::const_iterator head = region.begin(); |
| 70 | Region::const_iterator const tail = region.end(); |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 71 | // Use a lambda do avoid writing the object header when the object is empty |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 72 | while (head != tail) { |
Vishnu Nair | 6b59115 | 2021-10-08 11:45:14 -0700 | [diff] [blame] | 73 | writeToProto(*head, regionProto->add_rect()); |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 74 | head++; |
| 75 | } |
| 76 | } |
| 77 | |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 78 | void LayerProtoHelper::readFromProto(const perfetto::protos::RegionProto& regionProto, |
| 79 | Region& outRegion) { |
Vishnu Nair | 6b59115 | 2021-10-08 11:45:14 -0700 | [diff] [blame] | 80 | for (int i = 0; i < regionProto.rect_size(); i++) { |
| 81 | Rect rect; |
| 82 | readFromProto(regionProto.rect(i), rect); |
| 83 | outRegion.orSelf(rect); |
| 84 | } |
| 85 | } |
| 86 | |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 87 | void LayerProtoHelper::writeToProto(const Rect& rect, |
| 88 | std::function<perfetto::protos::RectProto*()> getRectProto) { |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 89 | if (rect.left != 0 || rect.right != 0 || rect.top != 0 || rect.bottom != 0) { |
| 90 | // Use a lambda do avoid writing the object header when the object is empty |
Vishnu Nair | 6b59115 | 2021-10-08 11:45:14 -0700 | [diff] [blame] | 91 | writeToProto(rect, getRectProto()); |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 92 | } |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 95 | void LayerProtoHelper::writeToProto(const Rect& rect, perfetto::protos::RectProto* rectProto) { |
Vishnu Nair | 6b59115 | 2021-10-08 11:45:14 -0700 | [diff] [blame] | 96 | rectProto->set_left(rect.left); |
| 97 | rectProto->set_top(rect.top); |
| 98 | rectProto->set_bottom(rect.bottom); |
| 99 | rectProto->set_right(rect.right); |
| 100 | } |
| 101 | |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 102 | void LayerProtoHelper::readFromProto(const perfetto::protos::RectProto& proto, Rect& outRect) { |
Vishnu Nair | 6b59115 | 2021-10-08 11:45:14 -0700 | [diff] [blame] | 103 | outRect.left = proto.left(); |
| 104 | outRect.top = proto.top(); |
| 105 | outRect.bottom = proto.bottom(); |
| 106 | outRect.right = proto.right(); |
| 107 | } |
| 108 | |
Vishnu Nair | a9123c8 | 2024-10-03 03:56:44 +0000 | [diff] [blame] | 109 | void LayerProtoHelper::readFromProto(const perfetto::protos::RectProto& proto, FloatRect& outRect) { |
| 110 | outRect.left = proto.left(); |
| 111 | outRect.top = proto.top(); |
| 112 | outRect.bottom = proto.bottom(); |
| 113 | outRect.right = proto.right(); |
| 114 | } |
| 115 | |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 116 | void LayerProtoHelper::writeToProto( |
| 117 | const FloatRect& rect, |
| 118 | std::function<perfetto::protos::FloatRectProto*()> getFloatRectProto) { |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 119 | if (rect.left != 0 || rect.right != 0 || rect.top != 0 || rect.bottom != 0) { |
| 120 | // Use a lambda do avoid writing the object header when the object is empty |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 121 | perfetto::protos::FloatRectProto* rectProto = getFloatRectProto(); |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 122 | rectProto->set_left(rect.left); |
| 123 | rectProto->set_top(rect.top); |
| 124 | rectProto->set_bottom(rect.bottom); |
| 125 | rectProto->set_right(rect.right); |
| 126 | } |
Yiwei Zhang | 7124ad3 | 2018-02-21 13:02:45 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 129 | void LayerProtoHelper::writeToProto(const half4 color, |
| 130 | std::function<perfetto::protos::ColorProto*()> getColorProto) { |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 131 | if (color.r != 0 || color.g != 0 || color.b != 0 || color.a != 0) { |
| 132 | // Use a lambda do avoid writing the object header when the object is empty |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 133 | perfetto::protos::ColorProto* colorProto = getColorProto(); |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 134 | colorProto->set_r(color.r); |
| 135 | colorProto->set_g(color.g); |
| 136 | colorProto->set_b(color.b); |
| 137 | colorProto->set_a(color.a); |
| 138 | } |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 139 | } |
| 140 | |
chaviw | 0a39899 | 2021-08-13 10:13:01 -0500 | [diff] [blame] | 141 | void LayerProtoHelper::writeToProtoDeprecated(const ui::Transform& transform, |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 142 | perfetto::protos::TransformProto* transformProto) { |
Nataniel Borges | 84a4618 | 2019-02-26 15:36:32 -0800 | [diff] [blame] | 143 | const uint32_t type = transform.getType() | (transform.getOrientation() << 8); |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 144 | transformProto->set_type(type); |
| 145 | |
Nataniel Borges | 9a2433c | 2019-03-13 15:02:45 -0700 | [diff] [blame] | 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 UKNOWN so we need to save the transform |
| 148 | // values in that case. |
Nataniel Borges | 84a4618 | 2019-02-26 15:36:32 -0800 | [diff] [blame] | 149 | if (type & (ui::Transform::SCALE | ui::Transform::UNKNOWN)) { |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 150 | transformProto->set_dsdx(transform[0][0]); |
| 151 | transformProto->set_dtdx(transform[0][1]); |
| 152 | transformProto->set_dsdy(transform[1][0]); |
| 153 | transformProto->set_dtdy(transform[1][1]); |
| 154 | } |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 155 | } |
| 156 | |
chaviw | 0a39899 | 2021-08-13 10:13:01 -0500 | [diff] [blame] | 157 | void LayerProtoHelper::writeTransformToProto(const ui::Transform& transform, |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 158 | perfetto::protos::TransformProto* transformProto) { |
chaviw | 0a39899 | 2021-08-13 10:13:01 -0500 | [diff] [blame] | 159 | const uint32_t type = transform.getType() | (transform.getOrientation() << 8); |
| 160 | transformProto->set_type(type); |
| 161 | |
| 162 | // Rotations that are 90/180/270 have their own type so the transform matrix can be |
| 163 | // reconstructed later. All other rotation have the type UNKNOWN so we need to save the |
| 164 | // transform values in that case. |
| 165 | if (type & (ui::Transform::SCALE | ui::Transform::UNKNOWN)) { |
| 166 | transformProto->set_dsdx(transform.dsdx()); |
| 167 | transformProto->set_dtdx(transform.dtdx()); |
| 168 | transformProto->set_dtdy(transform.dtdy()); |
| 169 | transformProto->set_dsdy(transform.dsdy()); |
| 170 | } |
| 171 | } |
| 172 | |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 173 | void LayerProtoHelper::writeToProto( |
| 174 | const renderengine::ExternalTexture& buffer, |
| 175 | std::function<perfetto::protos::ActiveBufferProto*()> getActiveBufferProto) { |
Vishnu Nair | d8f5e9f | 2022-02-03 10:23:28 -0800 | [diff] [blame] | 176 | if (buffer.getWidth() != 0 || buffer.getHeight() != 0 || buffer.getUsage() != 0 || |
| 177 | buffer.getPixelFormat() != 0) { |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 178 | // Use a lambda do avoid writing the object header when the object is empty |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 179 | auto* activeBufferProto = getActiveBufferProto(); |
Vishnu Nair | d8f5e9f | 2022-02-03 10:23:28 -0800 | [diff] [blame] | 180 | activeBufferProto->set_width(buffer.getWidth()); |
| 181 | activeBufferProto->set_height(buffer.getHeight()); |
| 182 | activeBufferProto->set_stride(buffer.getUsage()); |
| 183 | activeBufferProto->set_format(buffer.getPixelFormat()); |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 184 | } |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 187 | void LayerProtoHelper::writeToProto( |
Vishnu Nair | dc83d4b | 2024-08-15 16:29:20 +0000 | [diff] [blame] | 188 | const WindowInfo& inputInfo, |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 189 | std::function<perfetto::protos::InputWindowInfoProto*()> getInputWindowInfoProto) { |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 190 | perfetto::protos::InputWindowInfoProto* proto = getInputWindowInfoProto(); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 191 | proto->set_layout_params_flags(inputInfo.layoutParamsFlags.get()); |
Siarhei Vishniakou | e2eff11 | 2023-06-21 14:20:39 -0700 | [diff] [blame] | 192 | proto->set_input_config(inputInfo.inputConfig.get()); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 193 | using U = std::underlying_type_t<WindowInfo::Type>; |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 194 | // TODO(b/129481165): This static assert can be safely removed once conversion warnings |
| 195 | // are re-enabled. |
| 196 | static_assert(std::is_same_v<U, int32_t>); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 197 | proto->set_layout_params_type(static_cast<U>(inputInfo.layoutParamsType)); |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 198 | |
Chavi Weingarten | 7f01919 | 2023-08-08 20:39:01 +0000 | [diff] [blame] | 199 | LayerProtoHelper::writeToProto({inputInfo.frame.left, inputInfo.frame.top, |
| 200 | inputInfo.frame.right, inputInfo.frame.bottom}, |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 201 | [&]() { return proto->mutable_frame(); }); |
| 202 | LayerProtoHelper::writeToProto(inputInfo.touchableRegion, |
| 203 | [&]() { return proto->mutable_touchable_region(); }); |
| 204 | |
| 205 | proto->set_surface_inset(inputInfo.surfaceInset); |
Prabir Pradhan | 4d5c52f | 2022-01-31 08:52:10 -0800 | [diff] [blame] | 206 | using InputConfig = gui::WindowInfo::InputConfig; |
| 207 | proto->set_visible(!inputInfo.inputConfig.test(InputConfig::NOT_VISIBLE)); |
| 208 | proto->set_focusable(!inputInfo.inputConfig.test(InputConfig::NOT_FOCUSABLE)); |
| 209 | proto->set_has_wallpaper(inputInfo.inputConfig.test(InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)); |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 210 | |
| 211 | proto->set_global_scale_factor(inputInfo.globalScaleFactor); |
chaviw | 0a39899 | 2021-08-13 10:13:01 -0500 | [diff] [blame] | 212 | LayerProtoHelper::writeToProtoDeprecated(inputInfo.transform, proto->mutable_transform()); |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 213 | proto->set_replace_touchable_region_with_crop(inputInfo.replaceTouchableRegionWithCrop); |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 216 | void LayerProtoHelper::writeToProto(const mat4 matrix, |
| 217 | perfetto::protos::ColorTransformProto* colorTransformProto) { |
chaviw | ddeae26 | 2020-01-06 10:31:23 -0800 | [diff] [blame] | 218 | for (int i = 0; i < mat4::ROW_SIZE; i++) { |
| 219 | for (int j = 0; j < mat4::COL_SIZE; j++) { |
| 220 | colorTransformProto->add_val(matrix[i][j]); |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 225 | void LayerProtoHelper::readFromProto( |
| 226 | const perfetto::protos::ColorTransformProto& colorTransformProto, mat4& matrix) { |
Vishnu Nair | 6b59115 | 2021-10-08 11:45:14 -0700 | [diff] [blame] | 227 | for (int i = 0; i < mat4::ROW_SIZE; i++) { |
| 228 | for (int j = 0; j < mat4::COL_SIZE; j++) { |
| 229 | matrix[i][j] = colorTransformProto.val(i * mat4::COL_SIZE + j); |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 234 | void LayerProtoHelper::writeToProto(const android::BlurRegion region, |
| 235 | perfetto::protos::BlurRegion* proto) { |
Vishnu Nair | 6b59115 | 2021-10-08 11:45:14 -0700 | [diff] [blame] | 236 | proto->set_blur_radius(region.blurRadius); |
| 237 | proto->set_corner_radius_tl(region.cornerRadiusTL); |
| 238 | proto->set_corner_radius_tr(region.cornerRadiusTR); |
| 239 | proto->set_corner_radius_bl(region.cornerRadiusBL); |
| 240 | proto->set_corner_radius_br(region.cornerRadiusBR); |
| 241 | proto->set_alpha(region.alpha); |
| 242 | proto->set_left(region.left); |
| 243 | proto->set_top(region.top); |
| 244 | proto->set_right(region.right); |
| 245 | proto->set_bottom(region.bottom); |
| 246 | } |
| 247 | |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 248 | void LayerProtoHelper::readFromProto(const perfetto::protos::BlurRegion& proto, |
| 249 | android::BlurRegion& outRegion) { |
Vishnu Nair | 6b59115 | 2021-10-08 11:45:14 -0700 | [diff] [blame] | 250 | outRegion.blurRadius = proto.blur_radius(); |
| 251 | outRegion.cornerRadiusTL = proto.corner_radius_tl(); |
| 252 | outRegion.cornerRadiusTR = proto.corner_radius_tr(); |
| 253 | outRegion.cornerRadiusBL = proto.corner_radius_bl(); |
| 254 | outRegion.cornerRadiusBR = proto.corner_radius_br(); |
| 255 | outRegion.alpha = proto.alpha(); |
| 256 | outRegion.left = proto.left(); |
| 257 | outRegion.top = proto.top(); |
| 258 | outRegion.right = proto.right(); |
| 259 | outRegion.bottom = proto.bottom(); |
| 260 | } |
Vishnu Nair | ea6ff81 | 2023-02-27 17:41:39 +0000 | [diff] [blame] | 261 | |
Vishnu Nair | b46e37c | 2024-08-16 14:30:28 +0000 | [diff] [blame] | 262 | LayerProtoFromSnapshotGenerator& LayerProtoFromSnapshotGenerator::with( |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 263 | const frontend::LayerHierarchy& root) { |
Vishnu Nair | d018360 | 2023-03-16 18:52:15 +0000 | [diff] [blame] | 264 | mLayersProto.clear_layers(); |
Vishnu Nair | 516228f | 2024-07-11 23:03:02 +0000 | [diff] [blame] | 265 | mVisitedLayers.clear(); |
Vishnu Nair | d018360 | 2023-03-16 18:52:15 +0000 | [diff] [blame] | 266 | std::unordered_set<uint64_t> stackIdsToSkip; |
| 267 | if ((mTraceFlags & LayerTracing::TRACE_VIRTUAL_DISPLAYS) == 0) { |
| 268 | for (const auto& [layerStack, displayInfo] : mDisplayInfos) { |
| 269 | if (displayInfo.isVirtual) { |
| 270 | stackIdsToSkip.insert(layerStack.id); |
| 271 | } |
Vishnu Nair | 39872bc | 2023-03-11 12:48:31 -0800 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
Vishnu Nair | d018360 | 2023-03-16 18:52:15 +0000 | [diff] [blame] | 275 | frontend::LayerHierarchy::TraversalPath path = frontend::LayerHierarchy::TraversalPath::ROOT; |
| 276 | for (auto& [child, variant] : root.mChildren) { |
| 277 | if (variant != frontend::LayerHierarchy::Variant::Attached || |
| 278 | stackIdsToSkip.find(child->getLayer()->layerStack.id) != stackIdsToSkip.end()) { |
| 279 | continue; |
| 280 | } |
| 281 | frontend::LayerHierarchy::ScopedAddToTraversalPath addChildToPath(path, |
| 282 | child->getLayer()->id, |
| 283 | variant); |
| 284 | LayerProtoFromSnapshotGenerator::writeHierarchyToProto(*child, path); |
Vishnu Nair | d19848f | 2023-03-15 18:24:28 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Vishnu Nair | d018360 | 2023-03-16 18:52:15 +0000 | [diff] [blame] | 287 | // fill in relative and parent info |
| 288 | for (int i = 0; i < mLayersProto.layers_size(); i++) { |
| 289 | auto layerProto = mLayersProto.mutable_layers()->Mutable(i); |
| 290 | auto it = mChildToRelativeParent.find(layerProto->id()); |
| 291 | if (it == mChildToRelativeParent.end()) { |
| 292 | layerProto->set_z_order_relative_of(-1); |
| 293 | } else { |
| 294 | layerProto->set_z_order_relative_of(it->second); |
| 295 | } |
| 296 | it = mChildToParent.find(layerProto->id()); |
| 297 | if (it == mChildToParent.end()) { |
| 298 | layerProto->set_parent(-1); |
| 299 | } else { |
| 300 | layerProto->set_parent(it->second); |
| 301 | } |
Vishnu Nair | d19848f | 2023-03-15 18:24:28 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Vishnu Nair | b46e37c | 2024-08-16 14:30:28 +0000 | [diff] [blame] | 304 | return *this; |
| 305 | } |
| 306 | |
| 307 | LayerProtoFromSnapshotGenerator& LayerProtoFromSnapshotGenerator::withOffscreenLayers( |
| 308 | const frontend::LayerHierarchy& offscreenRoot) { |
| 309 | // Add a fake invisible root layer to the proto output and parent all the offscreen layers to |
| 310 | // it. |
| 311 | perfetto::protos::LayerProto* rootProto = mLayersProto.add_layers(); |
| 312 | const int32_t offscreenRootLayerId = INT32_MAX - 2; |
| 313 | rootProto->set_id(offscreenRootLayerId); |
| 314 | rootProto->set_name("Offscreen Root"); |
| 315 | rootProto->set_parent(-1); |
| 316 | |
| 317 | perfetto::protos::LayersProto offscreenLayers = |
| 318 | LayerProtoFromSnapshotGenerator(mSnapshotBuilder, mDisplayInfos, mLegacyLayers, |
| 319 | mTraceFlags) |
| 320 | .with(offscreenRoot) |
| 321 | .generate(); |
| 322 | |
| 323 | for (int i = 0; i < offscreenLayers.layers_size(); i++) { |
| 324 | perfetto::protos::LayerProto* layerProto = offscreenLayers.mutable_layers()->Mutable(i); |
| 325 | if (layerProto->parent() == -1) { |
| 326 | layerProto->set_parent(offscreenRootLayerId); |
| 327 | // Add layer as child of the fake root |
| 328 | rootProto->add_children(layerProto->id()); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | mLayersProto.mutable_layers()->Reserve(mLayersProto.layers_size() + |
| 333 | offscreenLayers.layers_size()); |
| 334 | std::copy(offscreenLayers.layers().begin(), offscreenLayers.layers().end(), |
| 335 | RepeatedFieldBackInserter(mLayersProto.mutable_layers())); |
| 336 | |
| 337 | return *this; |
Vishnu Nair | d018360 | 2023-03-16 18:52:15 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | frontend::LayerSnapshot* LayerProtoFromSnapshotGenerator::getSnapshot( |
| 341 | frontend::LayerHierarchy::TraversalPath& path, const frontend::RequestedLayerState& layer) { |
| 342 | frontend::LayerSnapshot* snapshot = mSnapshotBuilder.getSnapshot(path); |
| 343 | if (snapshot) { |
| 344 | return snapshot; |
| 345 | } else { |
| 346 | mDefaultSnapshots[path] = frontend::LayerSnapshot(layer, path); |
| 347 | return &mDefaultSnapshots[path]; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | void LayerProtoFromSnapshotGenerator::writeHierarchyToProto( |
| 352 | const frontend::LayerHierarchy& root, frontend::LayerHierarchy::TraversalPath& path) { |
| 353 | using Variant = frontend::LayerHierarchy::Variant; |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 354 | perfetto::protos::LayerProto* layerProto = mLayersProto.add_layers(); |
Vishnu Nair | d018360 | 2023-03-16 18:52:15 +0000 | [diff] [blame] | 355 | const frontend::RequestedLayerState& layer = *root.getLayer(); |
| 356 | frontend::LayerSnapshot* snapshot = getSnapshot(path, layer); |
Vishnu Nair | 516228f | 2024-07-11 23:03:02 +0000 | [diff] [blame] | 357 | if (mVisitedLayers.find(snapshot->uniqueSequence) != mVisitedLayers.end()) { |
| 358 | TransactionTraceWriter::getInstance().invoke("DuplicateLayer", /* overwrite= */ false); |
| 359 | return; |
| 360 | } |
Vishnu Nair | f19c9eb | 2024-07-15 19:44:20 +0000 | [diff] [blame] | 361 | mVisitedLayers.insert(snapshot->uniqueSequence); |
Vishnu Nair | d018360 | 2023-03-16 18:52:15 +0000 | [diff] [blame] | 362 | LayerProtoHelper::writeSnapshotToProto(layerProto, layer, *snapshot, mTraceFlags); |
| 363 | |
| 364 | for (const auto& [child, variant] : root.mChildren) { |
| 365 | frontend::LayerHierarchy::ScopedAddToTraversalPath addChildToPath(path, |
| 366 | child->getLayer()->id, |
| 367 | variant); |
| 368 | frontend::LayerSnapshot* childSnapshot = getSnapshot(path, layer); |
| 369 | if (variant == Variant::Attached || variant == Variant::Detached || |
Vishnu Nair | 491827d | 2024-04-29 23:43:26 +0000 | [diff] [blame] | 370 | frontend::LayerHierarchy::isMirror(variant)) { |
Vishnu Nair | d018360 | 2023-03-16 18:52:15 +0000 | [diff] [blame] | 371 | mChildToParent[childSnapshot->uniqueSequence] = snapshot->uniqueSequence; |
| 372 | layerProto->add_children(childSnapshot->uniqueSequence); |
| 373 | } else if (variant == Variant::Relative) { |
| 374 | mChildToRelativeParent[childSnapshot->uniqueSequence] = snapshot->uniqueSequence; |
| 375 | layerProto->add_relatives(childSnapshot->uniqueSequence); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | if (mTraceFlags & LayerTracing::TRACE_COMPOSITION) { |
| 380 | auto it = mLegacyLayers.find(layer.id); |
| 381 | if (it != mLegacyLayers.end()) { |
Vishnu Nair | 2f65e97 | 2023-04-04 16:36:28 +0000 | [diff] [blame] | 382 | it->second->writeCompositionStateToProto(layerProto, snapshot->outputFilter.layerStack); |
Vishnu Nair | ea6ff81 | 2023-02-27 17:41:39 +0000 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | |
| 386 | for (const auto& [child, variant] : root.mChildren) { |
| 387 | // avoid visiting relative layers twice |
| 388 | if (variant == Variant::Detached) { |
| 389 | continue; |
| 390 | } |
Vishnu Nair | d018360 | 2023-03-16 18:52:15 +0000 | [diff] [blame] | 391 | frontend::LayerHierarchy::ScopedAddToTraversalPath addChildToPath(path, |
| 392 | child->getLayer()->id, |
| 393 | variant); |
| 394 | writeHierarchyToProto(*child, path); |
Vishnu Nair | ea6ff81 | 2023-02-27 17:41:39 +0000 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 398 | void LayerProtoHelper::writeSnapshotToProto(perfetto::protos::LayerProto* layerInfo, |
Vishnu Nair | ea6ff81 | 2023-02-27 17:41:39 +0000 | [diff] [blame] | 399 | const frontend::RequestedLayerState& requestedState, |
| 400 | const frontend::LayerSnapshot& snapshot, |
| 401 | uint32_t traceFlags) { |
| 402 | const ui::Transform transform = snapshot.geomLayerTransform; |
| 403 | auto buffer = requestedState.externalTexture; |
| 404 | if (buffer != nullptr) { |
| 405 | LayerProtoHelper::writeToProto(*buffer, |
| 406 | [&]() { return layerInfo->mutable_active_buffer(); }); |
| 407 | LayerProtoHelper::writeToProtoDeprecated(ui::Transform(requestedState.bufferTransform), |
| 408 | layerInfo->mutable_buffer_transform()); |
| 409 | } |
| 410 | layerInfo->set_invalidate(snapshot.contentDirty); |
| 411 | layerInfo->set_is_protected(snapshot.hasProtectedContent); |
| 412 | layerInfo->set_dataspace(dataspaceDetails(static_cast<android_dataspace>(snapshot.dataspace))); |
| 413 | layerInfo->set_curr_frame(requestedState.bufferData->frameNumber); |
| 414 | layerInfo->set_requested_corner_radius(requestedState.cornerRadius); |
| 415 | layerInfo->set_corner_radius( |
| 416 | (snapshot.roundedCorner.radius.x + snapshot.roundedCorner.radius.y) / 2.0); |
| 417 | layerInfo->set_background_blur_radius(snapshot.backgroundBlurRadius); |
Vishnu Nair | 9e0017e | 2024-05-22 19:02:44 +0000 | [diff] [blame] | 418 | layerInfo->set_is_trusted_overlay(snapshot.trustedOverlay == gui::TrustedOverlay::ENABLED); |
| 419 | // TODO(b/339701674) update protos |
Vishnu Nair | ea6ff81 | 2023-02-27 17:41:39 +0000 | [diff] [blame] | 420 | LayerProtoHelper::writeToProtoDeprecated(transform, layerInfo->mutable_transform()); |
| 421 | LayerProtoHelper::writePositionToProto(transform.tx(), transform.ty(), |
| 422 | [&]() { return layerInfo->mutable_position(); }); |
| 423 | LayerProtoHelper::writeToProto(snapshot.geomLayerBounds, |
| 424 | [&]() { return layerInfo->mutable_bounds(); }); |
| 425 | LayerProtoHelper::writeToProto(snapshot.surfaceDamage, |
| 426 | [&]() { return layerInfo->mutable_damage_region(); }); |
| 427 | |
| 428 | if (requestedState.hasColorTransform) { |
| 429 | LayerProtoHelper::writeToProto(snapshot.colorTransform, |
| 430 | layerInfo->mutable_color_transform()); |
| 431 | } |
| 432 | |
Vishnu Nair | a9123c8 | 2024-10-03 03:56:44 +0000 | [diff] [blame] | 433 | LayerProtoHelper::writeToProto(snapshot.croppedBufferSize, |
Vishnu Nair | ea6ff81 | 2023-02-27 17:41:39 +0000 | [diff] [blame] | 434 | [&]() { return layerInfo->mutable_source_bounds(); }); |
| 435 | LayerProtoHelper::writeToProto(snapshot.transformedBounds, |
| 436 | [&]() { return layerInfo->mutable_screen_bounds(); }); |
| 437 | LayerProtoHelper::writeToProto(snapshot.roundedCorner.cropRect, |
| 438 | [&]() { return layerInfo->mutable_corner_radius_crop(); }); |
Vishnu Nair | d9e4f46 | 2023-10-06 04:05:45 +0000 | [diff] [blame] | 439 | layerInfo->set_shadow_radius(snapshot.shadowSettings.length); |
Vishnu Nair | ea6ff81 | 2023-02-27 17:41:39 +0000 | [diff] [blame] | 440 | |
Vishnu Nair | 93b8b79 | 2023-02-27 19:40:24 +0000 | [diff] [blame] | 441 | layerInfo->set_id(snapshot.uniqueSequence); |
Vishnu Nair | d018360 | 2023-03-16 18:52:15 +0000 | [diff] [blame] | 442 | layerInfo->set_original_id(snapshot.sequence); |
Vishnu Nair | 150065b | 2023-04-17 19:14:11 -0700 | [diff] [blame] | 443 | if (!snapshot.path.isClone()) { |
| 444 | layerInfo->set_name(requestedState.name); |
| 445 | } else { |
| 446 | layerInfo->set_name(requestedState.name + "(Mirror)"); |
| 447 | } |
Vishnu Nair | ea6ff81 | 2023-02-27 17:41:39 +0000 | [diff] [blame] | 448 | layerInfo->set_type("Layer"); |
| 449 | |
| 450 | LayerProtoHelper::writeToProto(requestedState.transparentRegion, |
| 451 | [&]() { return layerInfo->mutable_transparent_region(); }); |
| 452 | |
| 453 | layerInfo->set_layer_stack(snapshot.outputFilter.layerStack.id); |
| 454 | layerInfo->set_z(requestedState.z); |
| 455 | |
| 456 | ui::Transform requestedTransform = requestedState.getTransform(0); |
| 457 | LayerProtoHelper::writePositionToProto(requestedTransform.tx(), requestedTransform.ty(), [&]() { |
| 458 | return layerInfo->mutable_requested_position(); |
| 459 | }); |
| 460 | |
Vishnu Nair | a9123c8 | 2024-10-03 03:56:44 +0000 | [diff] [blame] | 461 | LayerProtoHelper::writeToProto(Rect(requestedState.crop), |
Vishnu Nair | ea6ff81 | 2023-02-27 17:41:39 +0000 | [diff] [blame] | 462 | [&]() { return layerInfo->mutable_crop(); }); |
| 463 | |
| 464 | layerInfo->set_is_opaque(snapshot.contentOpaque); |
| 465 | if (requestedState.externalTexture) |
| 466 | layerInfo->set_pixel_format( |
| 467 | decodePixelFormat(requestedState.externalTexture->getPixelFormat())); |
| 468 | LayerProtoHelper::writeToProto(snapshot.color, [&]() { return layerInfo->mutable_color(); }); |
| 469 | LayerProtoHelper::writeToProto(requestedState.color, |
| 470 | [&]() { return layerInfo->mutable_requested_color(); }); |
| 471 | layerInfo->set_flags(requestedState.flags); |
| 472 | |
| 473 | LayerProtoHelper::writeToProtoDeprecated(requestedTransform, |
| 474 | layerInfo->mutable_requested_transform()); |
| 475 | |
| 476 | layerInfo->set_is_relative_of(requestedState.isRelativeOf); |
| 477 | |
Vishnu Nair | a02943f | 2023-06-03 13:44:46 -0700 | [diff] [blame] | 478 | layerInfo->set_owner_uid(requestedState.ownerUid.val()); |
Vishnu Nair | ea6ff81 | 2023-02-27 17:41:39 +0000 | [diff] [blame] | 479 | |
| 480 | if ((traceFlags & LayerTracing::TRACE_INPUT) && snapshot.hasInputInfo()) { |
Vishnu Nair | dc83d4b | 2024-08-15 16:29:20 +0000 | [diff] [blame] | 481 | LayerProtoHelper::writeToProto(snapshot.inputInfo, |
Vishnu Nair | ea6ff81 | 2023-02-27 17:41:39 +0000 | [diff] [blame] | 482 | [&]() { return layerInfo->mutable_input_window_info(); }); |
| 483 | } |
| 484 | |
| 485 | if (traceFlags & LayerTracing::TRACE_EXTRA) { |
| 486 | auto protoMap = layerInfo->mutable_metadata(); |
| 487 | for (const auto& entry : requestedState.metadata.mMap) { |
| 488 | (*protoMap)[entry.first] = std::string(entry.second.cbegin(), entry.second.cend()); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | LayerProtoHelper::writeToProto(requestedState.destinationFrame, |
| 493 | [&]() { return layerInfo->mutable_destination_frame(); }); |
| 494 | } |
| 495 | |
Kean Mariotti | 4ba343c | 2023-04-19 13:31:02 +0000 | [diff] [blame] | 496 | google::protobuf::RepeatedPtrField<perfetto::protos::DisplayProto> |
| 497 | LayerProtoHelper::writeDisplayInfoToProto(const frontend::DisplayInfos& displayInfos) { |
| 498 | google::protobuf::RepeatedPtrField<perfetto::protos::DisplayProto> displays; |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 499 | displays.Reserve(displayInfos.size()); |
| 500 | for (const auto& [layerStack, displayInfo] : displayInfos) { |
| 501 | auto displayProto = displays.Add(); |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 502 | displayProto->set_id(displayInfo.info.displayId.val()); |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 503 | displayProto->set_layer_stack(layerStack.id); |
| 504 | displayProto->mutable_size()->set_w(displayInfo.info.logicalWidth); |
| 505 | displayProto->mutable_size()->set_h(displayInfo.info.logicalHeight); |
| 506 | writeTransformToProto(displayInfo.transform, displayProto->mutable_transform()); |
| 507 | displayProto->set_is_virtual(displayInfo.isVirtual); |
| 508 | } |
| 509 | return displays; |
| 510 | } |
| 511 | |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 512 | } // namespace surfaceflinger |
| 513 | } // namespace android |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 514 | |
| 515 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
Marin Shalamanov | bed7fd3 | 2020-12-21 20:02:20 +0100 | [diff] [blame] | 516 | #pragma clang diagnostic pop // ignored "-Wconversion -Wextra" |