blob: 3bd0643a6dfd9f44cda2a035bab28aea0e884844 [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();
Michael Wright44753b12020-07-08 13:48:11 +0100178 proto->set_layout_params_flags(inputInfo.flags.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>);
183 proto->set_layout_params_type(static_cast<U>(inputInfo.type));
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);
192 proto->set_visible(inputInfo.visible);
Vishnu Nair47074b82020-08-14 11:54:47 -0700193 proto->set_focusable(inputInfo.focusable);
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700194 proto->set_has_wallpaper(inputInfo.hasWallpaper);
195
196 proto->set_global_scale_factor(inputInfo.globalScaleFactor);
chaviw0a398992021-08-13 10:13:01 -0500197 LayerProtoHelper::writeToProtoDeprecated(inputInfo.transform, proto->mutable_transform());
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700198 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
chaviwddeae262020-01-06 10:31:23 -0800208void 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 Nair6b591152021-10-08 11:45:14 -0700216void 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
224void 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
237void 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}
chaviw1d044282017-09-27 12:19:28 -0700249} // namespace surfaceflinger
250} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800251
252// TODO(b/129481165): remove the #pragma below and fix conversion issues
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100253#pragma clang diagnostic pop // ignored "-Wconversion -Wextra"