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 namespace surfaceflinger {
26
writePositionToProto(const float x,const float y,std::function<PositionProto * ()> getPositionProto)27 void LayerProtoHelper::writePositionToProto(const float x, const float y,
28 std::function<PositionProto*()> getPositionProto) {
29 if (x != 0 || y != 0) {
30 // Use a lambda do avoid writing the object header when the object is empty
31 PositionProto* position = getPositionProto();
32 position->set_x(x);
33 position->set_y(y);
34 }
35 }
36
writeSizeToProto(const uint32_t w,const uint32_t h,std::function<SizeProto * ()> getSizeProto)37 void LayerProtoHelper::writeSizeToProto(const uint32_t w, const uint32_t h,
38 std::function<SizeProto*()> getSizeProto) {
39 if (w != 0 || h != 0) {
40 // Use a lambda do avoid writing the object header when the object is empty
41 SizeProto* size = getSizeProto();
42 size->set_w(w);
43 size->set_h(h);
44 }
45 }
46
writeToProto(const Region & region,std::function<RegionProto * ()> getRegionProto)47 void LayerProtoHelper::writeToProto(const Region& region,
48 std::function<RegionProto*()> getRegionProto) {
49 if (region.isEmpty()) {
50 return;
51 }
52
53 Region::const_iterator head = region.begin();
54 Region::const_iterator const tail = region.end();
55 // Use a lambda do avoid writing the object header when the object is empty
56 RegionProto* regionProto = getRegionProto();
57 while (head != tail) {
58 std::function<RectProto*()> getProtoRect = [&]() { return regionProto->add_rect(); };
59 writeToProto(*head, getProtoRect);
60 head++;
61 }
62 }
63
writeToProto(const Rect & rect,std::function<RectProto * ()> getRectProto)64 void LayerProtoHelper::writeToProto(const Rect& rect, std::function<RectProto*()> getRectProto) {
65 if (rect.left != 0 || rect.right != 0 || rect.top != 0 || rect.bottom != 0) {
66 // Use a lambda do avoid writing the object header when the object is empty
67 RectProto* rectProto = getRectProto();
68 rectProto->set_left(rect.left);
69 rectProto->set_top(rect.top);
70 rectProto->set_bottom(rect.bottom);
71 rectProto->set_right(rect.right);
72 }
73 }
74
writeToProto(const FloatRect & rect,std::function<FloatRectProto * ()> getFloatRectProto)75 void LayerProtoHelper::writeToProto(const FloatRect& rect,
76 std::function<FloatRectProto*()> getFloatRectProto) {
77 if (rect.left != 0 || rect.right != 0 || rect.top != 0 || rect.bottom != 0) {
78 // Use a lambda do avoid writing the object header when the object is empty
79 FloatRectProto* rectProto = getFloatRectProto();
80 rectProto->set_left(rect.left);
81 rectProto->set_top(rect.top);
82 rectProto->set_bottom(rect.bottom);
83 rectProto->set_right(rect.right);
84 }
85 }
86
writeToProto(const half4 color,std::function<ColorProto * ()> getColorProto)87 void LayerProtoHelper::writeToProto(const half4 color, std::function<ColorProto*()> getColorProto) {
88 if (color.r != 0 || color.g != 0 || color.b != 0 || color.a != 0) {
89 // Use a lambda do avoid writing the object header when the object is empty
90 ColorProto* colorProto = getColorProto();
91 colorProto->set_r(color.r);
92 colorProto->set_g(color.g);
93 colorProto->set_b(color.b);
94 colorProto->set_a(color.a);
95 }
96 }
97
writeToProto(const ui::Transform & transform,TransformProto * transformProto)98 void LayerProtoHelper::writeToProto(const ui::Transform& transform,
99 TransformProto* transformProto) {
100 const uint32_t type = transform.getType() | (transform.getOrientation() << 8);
101 transformProto->set_type(type);
102
103 // Rotations that are 90/180/270 have their own type so the transform matrix can be
104 // reconstructed later. All other rotation have the type UKNOWN so we need to save the transform
105 // values in that case.
106 if (type & (ui::Transform::SCALE | ui::Transform::UNKNOWN)) {
107 transformProto->set_dsdx(transform[0][0]);
108 transformProto->set_dtdx(transform[0][1]);
109 transformProto->set_dsdy(transform[1][0]);
110 transformProto->set_dtdy(transform[1][1]);
111 }
112 }
113
writeToProto(const sp<GraphicBuffer> & buffer,std::function<ActiveBufferProto * ()> getActiveBufferProto)114 void LayerProtoHelper::writeToProto(const sp<GraphicBuffer>& buffer,
115 std::function<ActiveBufferProto*()> getActiveBufferProto) {
116 if (buffer->getWidth() != 0 || buffer->getHeight() != 0 || buffer->getStride() != 0 ||
117 buffer->format != 0) {
118 // Use a lambda do avoid writing the object header when the object is empty
119 ActiveBufferProto* activeBufferProto = getActiveBufferProto();
120 activeBufferProto->set_width(buffer->getWidth());
121 activeBufferProto->set_height(buffer->getHeight());
122 activeBufferProto->set_stride(buffer->getStride());
123 activeBufferProto->set_format(buffer->format);
124 }
125 }
126
writeToProto(const InputWindowInfo & inputInfo,const wp<Layer> & touchableRegionBounds,std::function<InputWindowInfoProto * ()> getInputWindowInfoProto)127 void LayerProtoHelper::writeToProto(
128 const InputWindowInfo& inputInfo, const wp<Layer>& touchableRegionBounds,
129 std::function<InputWindowInfoProto*()> getInputWindowInfoProto) {
130 if (inputInfo.token == nullptr) {
131 return;
132 }
133
134 InputWindowInfoProto* proto = getInputWindowInfoProto();
135 proto->set_layout_params_flags(inputInfo.flags.get());
136 using U = std::underlying_type_t<InputWindowInfo::Type>;
137 // TODO(b/129481165): This static assert can be safely removed once conversion warnings
138 // are re-enabled.
139 static_assert(std::is_same_v<U, int32_t>);
140 proto->set_layout_params_type(static_cast<U>(inputInfo.type));
141
142 LayerProtoHelper::writeToProto({inputInfo.frameLeft, inputInfo.frameTop, inputInfo.frameRight,
143 inputInfo.frameBottom},
144 [&]() { return proto->mutable_frame(); });
145 LayerProtoHelper::writeToProto(inputInfo.touchableRegion,
146 [&]() { return proto->mutable_touchable_region(); });
147
148 proto->set_surface_inset(inputInfo.surfaceInset);
149 proto->set_visible(inputInfo.visible);
150 proto->set_focusable(inputInfo.focusable);
151 proto->set_has_wallpaper(inputInfo.hasWallpaper);
152
153 proto->set_global_scale_factor(inputInfo.globalScaleFactor);
154 LayerProtoHelper::writeToProto(inputInfo.transform, proto->mutable_transform());
155 proto->set_replace_touchable_region_with_crop(inputInfo.replaceTouchableRegionWithCrop);
156 auto cropLayer = touchableRegionBounds.promote();
157 if (cropLayer != nullptr) {
158 proto->set_crop_layer_id(cropLayer->sequence);
159 LayerProtoHelper::writeToProto(cropLayer->getScreenBounds(
160 false /* reduceTransparentRegion */),
161 [&]() { return proto->mutable_touchable_region_crop(); });
162 }
163 }
164
writeToProto(const mat4 matrix,ColorTransformProto * colorTransformProto)165 void LayerProtoHelper::writeToProto(const mat4 matrix, ColorTransformProto* colorTransformProto) {
166 for (int i = 0; i < mat4::ROW_SIZE; i++) {
167 for (int j = 0; j < mat4::COL_SIZE; j++) {
168 colorTransformProto->add_val(matrix[i][j]);
169 }
170 }
171 }
172
173 } // namespace surfaceflinger
174 } // namespace android
175
176 // TODO(b/129481165): remove the #pragma below and fix conversion issues
177 #pragma clang diagnostic pop // ignored "-Wconversion -Wextra"
178