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 | |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 56 | Region::const_iterator head = region.begin(); |
| 57 | Region::const_iterator const tail = region.end(); |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 58 | // Use a lambda do avoid writing the object header when the object is empty |
| 59 | RegionProto* regionProto = getRegionProto(); |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 60 | while (head != tail) { |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 61 | std::function<RectProto*()> getProtoRect = [&]() { return regionProto->add_rect(); }; |
| 62 | writeToProto(*head, getProtoRect); |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 63 | head++; |
| 64 | } |
| 65 | } |
| 66 | |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 67 | void LayerProtoHelper::writeToProto(const Rect& rect, std::function<RectProto*()> getRectProto) { |
| 68 | if (rect.left != 0 || rect.right != 0 || rect.top != 0 || rect.bottom != 0) { |
| 69 | // Use a lambda do avoid writing the object header when the object is empty |
| 70 | RectProto* rectProto = getRectProto(); |
| 71 | rectProto->set_left(rect.left); |
| 72 | rectProto->set_top(rect.top); |
| 73 | rectProto->set_bottom(rect.bottom); |
| 74 | rectProto->set_right(rect.right); |
| 75 | } |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 78 | void LayerProtoHelper::writeToProto(const FloatRect& rect, |
| 79 | std::function<FloatRectProto*()> getFloatRectProto) { |
| 80 | if (rect.left != 0 || rect.right != 0 || rect.top != 0 || rect.bottom != 0) { |
| 81 | // Use a lambda do avoid writing the object header when the object is empty |
| 82 | FloatRectProto* rectProto = getFloatRectProto(); |
| 83 | rectProto->set_left(rect.left); |
| 84 | rectProto->set_top(rect.top); |
| 85 | rectProto->set_bottom(rect.bottom); |
| 86 | rectProto->set_right(rect.right); |
| 87 | } |
Yiwei Zhang | 7124ad3 | 2018-02-21 13:02:45 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 90 | void LayerProtoHelper::writeToProto(const half4 color, std::function<ColorProto*()> getColorProto) { |
| 91 | if (color.r != 0 || color.g != 0 || color.b != 0 || color.a != 0) { |
| 92 | // Use a lambda do avoid writing the object header when the object is empty |
| 93 | ColorProto* colorProto = getColorProto(); |
| 94 | colorProto->set_r(color.r); |
| 95 | colorProto->set_g(color.g); |
| 96 | colorProto->set_b(color.b); |
| 97 | colorProto->set_a(color.a); |
| 98 | } |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Peiyong Lin | efefaac | 2018-08-17 12:27:51 -0700 | [diff] [blame] | 101 | void LayerProtoHelper::writeToProto(const ui::Transform& transform, |
| 102 | TransformProto* transformProto) { |
Nataniel Borges | 84a4618 | 2019-02-26 15:36:32 -0800 | [diff] [blame] | 103 | const uint32_t type = transform.getType() | (transform.getOrientation() << 8); |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 104 | transformProto->set_type(type); |
| 105 | |
Nataniel Borges | 9a2433c | 2019-03-13 15:02:45 -0700 | [diff] [blame] | 106 | // Rotations that are 90/180/270 have their own type so the transform matrix can be |
| 107 | // reconstructed later. All other rotation have the type UKNOWN so we need to save the transform |
| 108 | // values in that case. |
Nataniel Borges | 84a4618 | 2019-02-26 15:36:32 -0800 | [diff] [blame] | 109 | if (type & (ui::Transform::SCALE | ui::Transform::UNKNOWN)) { |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 110 | transformProto->set_dsdx(transform[0][0]); |
| 111 | transformProto->set_dtdx(transform[0][1]); |
| 112 | transformProto->set_dsdy(transform[1][0]); |
| 113 | transformProto->set_dtdy(transform[1][1]); |
| 114 | } |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void LayerProtoHelper::writeToProto(const sp<GraphicBuffer>& buffer, |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 118 | std::function<ActiveBufferProto*()> getActiveBufferProto) { |
| 119 | if (buffer->getWidth() != 0 || buffer->getHeight() != 0 || buffer->getStride() != 0 || |
| 120 | buffer->format != 0) { |
| 121 | // Use a lambda do avoid writing the object header when the object is empty |
| 122 | ActiveBufferProto* activeBufferProto = getActiveBufferProto(); |
| 123 | activeBufferProto->set_width(buffer->getWidth()); |
| 124 | activeBufferProto->set_height(buffer->getHeight()); |
| 125 | activeBufferProto->set_stride(buffer->getStride()); |
| 126 | activeBufferProto->set_format(buffer->format); |
| 127 | } |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 130 | void LayerProtoHelper::writeToProto( |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame^] | 131 | const WindowInfo& inputInfo, const wp<Layer>& touchableRegionBounds, |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 132 | std::function<InputWindowInfoProto*()> getInputWindowInfoProto) { |
| 133 | if (inputInfo.token == nullptr) { |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | InputWindowInfoProto* proto = getInputWindowInfoProto(); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 138 | proto->set_layout_params_flags(inputInfo.flags.get()); |
chaviw | 98318de | 2021-05-19 16:45:23 -0500 | [diff] [blame^] | 139 | using U = std::underlying_type_t<WindowInfo::Type>; |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 140 | // TODO(b/129481165): This static assert can be safely removed once conversion warnings |
| 141 | // are re-enabled. |
| 142 | static_assert(std::is_same_v<U, int32_t>); |
| 143 | proto->set_layout_params_type(static_cast<U>(inputInfo.type)); |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 144 | |
| 145 | LayerProtoHelper::writeToProto({inputInfo.frameLeft, inputInfo.frameTop, inputInfo.frameRight, |
| 146 | inputInfo.frameBottom}, |
| 147 | [&]() { return proto->mutable_frame(); }); |
| 148 | LayerProtoHelper::writeToProto(inputInfo.touchableRegion, |
| 149 | [&]() { return proto->mutable_touchable_region(); }); |
| 150 | |
| 151 | proto->set_surface_inset(inputInfo.surfaceInset); |
| 152 | proto->set_visible(inputInfo.visible); |
Vishnu Nair | 47074b8 | 2020-08-14 11:54:47 -0700 | [diff] [blame] | 153 | proto->set_focusable(inputInfo.focusable); |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 154 | proto->set_has_wallpaper(inputInfo.hasWallpaper); |
| 155 | |
| 156 | proto->set_global_scale_factor(inputInfo.globalScaleFactor); |
chaviw | 1ff3d1e | 2020-07-01 15:53:47 -0700 | [diff] [blame] | 157 | LayerProtoHelper::writeToProto(inputInfo.transform, proto->mutable_transform()); |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 158 | proto->set_replace_touchable_region_with_crop(inputInfo.replaceTouchableRegionWithCrop); |
| 159 | auto cropLayer = touchableRegionBounds.promote(); |
| 160 | if (cropLayer != nullptr) { |
| 161 | proto->set_crop_layer_id(cropLayer->sequence); |
| 162 | LayerProtoHelper::writeToProto(cropLayer->getScreenBounds( |
| 163 | false /* reduceTransparentRegion */), |
| 164 | [&]() { return proto->mutable_touchable_region_crop(); }); |
| 165 | } |
| 166 | } |
| 167 | |
chaviw | ddeae26 | 2020-01-06 10:31:23 -0800 | [diff] [blame] | 168 | void LayerProtoHelper::writeToProto(const mat4 matrix, ColorTransformProto* colorTransformProto) { |
| 169 | for (int i = 0; i < mat4::ROW_SIZE; i++) { |
| 170 | for (int j = 0; j < mat4::COL_SIZE; j++) { |
| 171 | colorTransformProto->add_val(matrix[i][j]); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 176 | } // namespace surfaceflinger |
| 177 | } // namespace android |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 178 | |
| 179 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
Marin Shalamanov | bed7fd3 | 2020-12-21 20:02:20 +0100 | [diff] [blame] | 180 | #pragma clang diagnostic pop // ignored "-Wconversion -Wextra" |