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
17 // TODO(b/129481165): remove the #pragma below and fix conversion issues
18 #pragma clang diagnostic push
19 #pragma clang diagnostic ignored "-Wconversion"
20 #pragma clang diagnostic ignored "-Wextra"
21
22 #include "LayerProtoHelper.h"
23
24 namespace android {
25
26 using gui::WindowInfo;
27
28 namespace surfaceflinger {
29
writePositionToProto(const float x,const float y,std::function<PositionProto * ()> getPositionProto)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
writeSizeToProto(const uint32_t w,const uint32_t h,std::function<SizeProto * ()> getSizeProto)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
writeToProto(const Region & region,std::function<RegionProto * ()> getRegionProto)50 void LayerProtoHelper::writeToProto(const Region& region,
51 std::function<RegionProto*()> getRegionProto) {
52 if (region.isEmpty()) {
53 return;
54 }
55
56 Region::const_iterator head = region.begin();
57 Region::const_iterator const tail = region.end();
58 // Use a lambda do avoid writing the object header when the object is empty
59 RegionProto* regionProto = getRegionProto();
60 while (head != tail) {
61 std::function<RectProto*()> getProtoRect = [&]() { return regionProto->add_rect(); };
62 writeToProto(*head, getProtoRect);
63 head++;
64 }
65 }
66
writeToProto(const Rect & rect,std::function<RectProto * ()> getRectProto)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 }
76 }
77
writeToProto(const FloatRect & rect,std::function<FloatRectProto * ()> getFloatRectProto)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 }
88 }
89
writeToProto(const half4 color,std::function<ColorProto * ()> getColorProto)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 }
99 }
100
writeToProtoDeprecated(const ui::Transform & transform,TransformProto * transformProto)101 void LayerProtoHelper::writeToProtoDeprecated(const ui::Transform& transform,
102 TransformProto* transformProto) {
103 const uint32_t type = transform.getType() | (transform.getOrientation() << 8);
104 transformProto->set_type(type);
105
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.
109 if (type & (ui::Transform::SCALE | ui::Transform::UNKNOWN)) {
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 }
115 }
116
writeTransformToProto(const ui::Transform & transform,TransformProto * transformProto)117 void LayerProtoHelper::writeTransformToProto(const ui::Transform& transform,
118 TransformProto* transformProto) {
119 const uint32_t type = transform.getType() | (transform.getOrientation() << 8);
120 transformProto->set_type(type);
121
122 // Rotations that are 90/180/270 have their own type so the transform matrix can be
123 // reconstructed later. All other rotation have the type UNKNOWN so we need to save the
124 // transform values in that case.
125 if (type & (ui::Transform::SCALE | ui::Transform::UNKNOWN)) {
126 transformProto->set_dsdx(transform.dsdx());
127 transformProto->set_dtdx(transform.dtdx());
128 transformProto->set_dtdy(transform.dtdy());
129 transformProto->set_dsdy(transform.dsdy());
130 }
131 }
132
writeToProto(const sp<GraphicBuffer> & buffer,std::function<ActiveBufferProto * ()> getActiveBufferProto)133 void LayerProtoHelper::writeToProto(const sp<GraphicBuffer>& buffer,
134 std::function<ActiveBufferProto*()> getActiveBufferProto) {
135 if (buffer->getWidth() != 0 || buffer->getHeight() != 0 || buffer->getStride() != 0 ||
136 buffer->format != 0) {
137 // Use a lambda do avoid writing the object header when the object is empty
138 ActiveBufferProto* activeBufferProto = getActiveBufferProto();
139 activeBufferProto->set_width(buffer->getWidth());
140 activeBufferProto->set_height(buffer->getHeight());
141 activeBufferProto->set_stride(buffer->getStride());
142 activeBufferProto->set_format(buffer->format);
143 }
144 }
145
writeToProto(const WindowInfo & inputInfo,const wp<Layer> & touchableRegionBounds,std::function<InputWindowInfoProto * ()> getInputWindowInfoProto)146 void LayerProtoHelper::writeToProto(
147 const WindowInfo& inputInfo, const wp<Layer>& touchableRegionBounds,
148 std::function<InputWindowInfoProto*()> getInputWindowInfoProto) {
149 if (inputInfo.token == nullptr) {
150 return;
151 }
152
153 InputWindowInfoProto* proto = getInputWindowInfoProto();
154 proto->set_layout_params_flags(inputInfo.flags.get());
155 using U = std::underlying_type_t<WindowInfo::Type>;
156 // TODO(b/129481165): This static assert can be safely removed once conversion warnings
157 // are re-enabled.
158 static_assert(std::is_same_v<U, int32_t>);
159 proto->set_layout_params_type(static_cast<U>(inputInfo.type));
160
161 LayerProtoHelper::writeToProto({inputInfo.frameLeft, inputInfo.frameTop, inputInfo.frameRight,
162 inputInfo.frameBottom},
163 [&]() { return proto->mutable_frame(); });
164 LayerProtoHelper::writeToProto(inputInfo.touchableRegion,
165 [&]() { return proto->mutable_touchable_region(); });
166
167 proto->set_surface_inset(inputInfo.surfaceInset);
168 proto->set_visible(inputInfo.visible);
169 proto->set_focusable(inputInfo.focusable);
170 proto->set_has_wallpaper(inputInfo.hasWallpaper);
171
172 proto->set_global_scale_factor(inputInfo.globalScaleFactor);
173 LayerProtoHelper::writeToProtoDeprecated(inputInfo.transform, proto->mutable_transform());
174 proto->set_replace_touchable_region_with_crop(inputInfo.replaceTouchableRegionWithCrop);
175 auto cropLayer = touchableRegionBounds.promote();
176 if (cropLayer != nullptr) {
177 proto->set_crop_layer_id(cropLayer->sequence);
178 LayerProtoHelper::writeToProto(cropLayer->getScreenBounds(
179 false /* reduceTransparentRegion */),
180 [&]() { return proto->mutable_touchable_region_crop(); });
181 }
182 }
183
writeToProto(const mat4 matrix,ColorTransformProto * colorTransformProto)184 void LayerProtoHelper::writeToProto(const mat4 matrix, ColorTransformProto* colorTransformProto) {
185 for (int i = 0; i < mat4::ROW_SIZE; i++) {
186 for (int j = 0; j < mat4::COL_SIZE; j++) {
187 colorTransformProto->add_val(matrix[i][j]);
188 }
189 }
190 }
191
192 } // namespace surfaceflinger
193 } // namespace android
194
195 // TODO(b/129481165): remove the #pragma below and fix conversion issues
196 #pragma clang diagnostic pop // ignored "-Wconversion -Wextra"
197