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 |
| 18 | #pragma clang diagnostic push |
| 19 | #pragma clang diagnostic ignored "-Wconversion" |
Marin Shalamanov | bed7fd3 | 2020-12-21 20:02:20 +0100 | [diff] [blame] | 20 | #pragma clang diagnostic ignored "-Wextra" |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 21 | |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 22 | #include "LayerProtoHelper.h" |
| 23 | |
| 24 | namespace android { |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 25 | |
| 26 | using gui::WindowInfo; |
| 27 | |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 28 | namespace surfaceflinger { |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 29 | |
| 30 | void 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 | |
| 40 | void 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 | |
| 50 | void LayerProtoHelper::writeToProto(const Region& region, |
| 51 | std::function<RegionProto*()> getRegionProto) { |
| 52 | if (region.isEmpty()) { |
| 53 | return; |
| 54 | } |
| 55 | |
Vishnu Nair | 6b59115 | 2021-10-08 11:45:14 -0700 | [diff] [blame] | 56 | writeToProto(region, getRegionProto()); |
| 57 | } |
| 58 | |
| 59 | void LayerProtoHelper::writeToProto(const Region& region, RegionProto* regionProto) { |
| 60 | if (region.isEmpty()) { |
| 61 | return; |
| 62 | } |
| 63 | |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 64 | Region::const_iterator head = region.begin(); |
| 65 | Region::const_iterator const tail = region.end(); |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 66 | // 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] | 67 | while (head != tail) { |
Vishnu Nair | 6b59115 | 2021-10-08 11:45:14 -0700 | [diff] [blame] | 68 | writeToProto(*head, regionProto->add_rect()); |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 69 | head++; |
| 70 | } |
| 71 | } |
| 72 | |
Vishnu Nair | 6b59115 | 2021-10-08 11:45:14 -0700 | [diff] [blame] | 73 | void 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 Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 81 | void 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 Nair | 6b59115 | 2021-10-08 11:45:14 -0700 | [diff] [blame] | 84 | writeToProto(rect, getRectProto()); |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 85 | } |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Vishnu Nair | 6b59115 | 2021-10-08 11:45:14 -0700 | [diff] [blame] | 88 | void 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 | |
| 95 | void 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 Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 102 | void 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 Zhang | 7124ad3 | 2018-02-21 13:02:45 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 114 | void 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 | } |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 123 | } |
| 124 | |
chaviw | 0a39899 | 2021-08-13 10:13:01 -0500 | [diff] [blame] | 125 | void LayerProtoHelper::writeToProtoDeprecated(const ui::Transform& transform, |
| 126 | TransformProto* transformProto) { |
Nataniel Borges | 84a4618 | 2019-02-26 15:36:32 -0800 | [diff] [blame] | 127 | const uint32_t type = transform.getType() | (transform.getOrientation() << 8); |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 128 | transformProto->set_type(type); |
| 129 | |
Nataniel Borges | 9a2433c | 2019-03-13 15:02:45 -0700 | [diff] [blame] | 130 | // 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 Borges | 84a4618 | 2019-02-26 15:36:32 -0800 | [diff] [blame] | 133 | if (type & (ui::Transform::SCALE | ui::Transform::UNKNOWN)) { |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 134 | 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 | } |
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::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 Nair | d37343b | 2022-01-12 16:18:56 -0800 | [diff] [blame] | 157 | void LayerProtoHelper::writeToProto(const renderengine::ExternalTexture& buffer, |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 158 | std::function<ActiveBufferProto*()> getActiveBufferProto) { |
Vishnu Nair | d8f5e9f | 2022-02-03 10:23:28 -0800 | [diff] [blame^] | 159 | if (buffer.getWidth() != 0 || buffer.getHeight() != 0 || buffer.getUsage() != 0 || |
| 160 | buffer.getPixelFormat() != 0) { |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 161 | // Use a lambda do avoid writing the object header when the object is empty |
| 162 | ActiveBufferProto* activeBufferProto = getActiveBufferProto(); |
Vishnu Nair | d8f5e9f | 2022-02-03 10:23:28 -0800 | [diff] [blame^] | 163 | activeBufferProto->set_width(buffer.getWidth()); |
| 164 | activeBufferProto->set_height(buffer.getHeight()); |
| 165 | activeBufferProto->set_stride(buffer.getUsage()); |
| 166 | activeBufferProto->set_format(buffer.getPixelFormat()); |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 167 | } |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 170 | void LayerProtoHelper::writeToProto( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 171 | const WindowInfo& inputInfo, const wp<Layer>& touchableRegionBounds, |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 172 | std::function<InputWindowInfoProto*()> getInputWindowInfoProto) { |
| 173 | if (inputInfo.token == nullptr) { |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | InputWindowInfoProto* proto = getInputWindowInfoProto(); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 178 | proto->set_layout_params_flags(inputInfo.flags.get()); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 179 | using U = std::underlying_type_t<WindowInfo::Type>; |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 180 | // 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>); |
| 183 | proto->set_layout_params_type(static_cast<U>(inputInfo.type)); |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 184 | |
| 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); |
| 192 | proto->set_visible(inputInfo.visible); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 193 | proto->set_focusable(inputInfo.focusable); |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 194 | proto->set_has_wallpaper(inputInfo.hasWallpaper); |
| 195 | |
| 196 | proto->set_global_scale_factor(inputInfo.globalScaleFactor); |
chaviw | 0a39899 | 2021-08-13 10:13:01 -0500 | [diff] [blame] | 197 | LayerProtoHelper::writeToProtoDeprecated(inputInfo.transform, proto->mutable_transform()); |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 198 | proto->set_replace_touchable_region_with_crop(inputInfo.replaceTouchableRegionWithCrop); |
| 199 | auto cropLayer = touchableRegionBounds.promote(); |
| 200 | if (cropLayer != nullptr) { |
| 201 | proto->set_crop_layer_id(cropLayer->sequence); |
| 202 | LayerProtoHelper::writeToProto(cropLayer->getScreenBounds( |
| 203 | false /* reduceTransparentRegion */), |
| 204 | [&]() { return proto->mutable_touchable_region_crop(); }); |
| 205 | } |
| 206 | } |
| 207 | |
chaviw | ddeae26 | 2020-01-06 10:31:23 -0800 | [diff] [blame] | 208 | void LayerProtoHelper::writeToProto(const mat4 matrix, ColorTransformProto* colorTransformProto) { |
| 209 | for (int i = 0; i < mat4::ROW_SIZE; i++) { |
| 210 | for (int j = 0; j < mat4::COL_SIZE; j++) { |
| 211 | colorTransformProto->add_val(matrix[i][j]); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
Vishnu Nair | 6b59115 | 2021-10-08 11:45:14 -0700 | [diff] [blame] | 216 | void LayerProtoHelper::readFromProto(const ColorTransformProto& colorTransformProto, mat4& matrix) { |
| 217 | for (int i = 0; i < mat4::ROW_SIZE; i++) { |
| 218 | for (int j = 0; j < mat4::COL_SIZE; j++) { |
| 219 | matrix[i][j] = colorTransformProto.val(i * mat4::COL_SIZE + j); |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | void LayerProtoHelper::writeToProto(const android::BlurRegion region, BlurRegion* proto) { |
| 225 | proto->set_blur_radius(region.blurRadius); |
| 226 | proto->set_corner_radius_tl(region.cornerRadiusTL); |
| 227 | proto->set_corner_radius_tr(region.cornerRadiusTR); |
| 228 | proto->set_corner_radius_bl(region.cornerRadiusBL); |
| 229 | proto->set_corner_radius_br(region.cornerRadiusBR); |
| 230 | proto->set_alpha(region.alpha); |
| 231 | proto->set_left(region.left); |
| 232 | proto->set_top(region.top); |
| 233 | proto->set_right(region.right); |
| 234 | proto->set_bottom(region.bottom); |
| 235 | } |
| 236 | |
| 237 | void LayerProtoHelper::readFromProto(const BlurRegion& proto, android::BlurRegion& outRegion) { |
| 238 | outRegion.blurRadius = proto.blur_radius(); |
| 239 | outRegion.cornerRadiusTL = proto.corner_radius_tl(); |
| 240 | outRegion.cornerRadiusTR = proto.corner_radius_tr(); |
| 241 | outRegion.cornerRadiusBL = proto.corner_radius_bl(); |
| 242 | outRegion.cornerRadiusBR = proto.corner_radius_br(); |
| 243 | outRegion.alpha = proto.alpha(); |
| 244 | outRegion.left = proto.left(); |
| 245 | outRegion.top = proto.top(); |
| 246 | outRegion.right = proto.right(); |
| 247 | outRegion.bottom = proto.bottom(); |
| 248 | } |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 249 | } // namespace surfaceflinger |
| 250 | } // namespace android |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 251 | |
| 252 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
Marin Shalamanov | bed7fd3 | 2020-12-21 20:02:20 +0100 | [diff] [blame] | 253 | #pragma clang diagnostic pop // ignored "-Wconversion -Wextra" |