blob: b1db6d34a3f4504f6d17e4765d3f604c9a26124b [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 {
25namespace surfaceflinger {
Nataniel Borges797b0e42019-02-15 14:11:58 -080026
27void 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
37void 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
47void LayerProtoHelper::writeToProto(const Region& region,
48 std::function<RegionProto*()> getRegionProto) {
49 if (region.isEmpty()) {
50 return;
51 }
52
chaviw1d044282017-09-27 12:19:28 -070053 Region::const_iterator head = region.begin();
54 Region::const_iterator const tail = region.end();
Nataniel Borges797b0e42019-02-15 14:11:58 -080055 // Use a lambda do avoid writing the object header when the object is empty
56 RegionProto* regionProto = getRegionProto();
chaviw1d044282017-09-27 12:19:28 -070057 while (head != tail) {
Nataniel Borges797b0e42019-02-15 14:11:58 -080058 std::function<RectProto*()> getProtoRect = [&]() { return regionProto->add_rect(); };
59 writeToProto(*head, getProtoRect);
chaviw1d044282017-09-27 12:19:28 -070060 head++;
61 }
62}
63
Nataniel Borges797b0e42019-02-15 14:11:58 -080064void 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 }
chaviw1d044282017-09-27 12:19:28 -070073}
74
Nataniel Borges797b0e42019-02-15 14:11:58 -080075void 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 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -080085}
86
Nataniel Borges797b0e42019-02-15 14:11:58 -080087void 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 }
chaviw1d044282017-09-27 12:19:28 -070096}
97
Peiyong Linefefaac2018-08-17 12:27:51 -070098void LayerProtoHelper::writeToProto(const ui::Transform& transform,
99 TransformProto* transformProto) {
Nataniel Borges84a46182019-02-26 15:36:32 -0800100 const uint32_t type = transform.getType() | (transform.getOrientation() << 8);
Nataniel Borges797b0e42019-02-15 14:11:58 -0800101 transformProto->set_type(type);
102
Nataniel Borges9a2433c2019-03-13 15:02:45 -0700103 // 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.
Nataniel Borges84a46182019-02-26 15:36:32 -0800106 if (type & (ui::Transform::SCALE | ui::Transform::UNKNOWN)) {
Nataniel Borges797b0e42019-02-15 14:11:58 -0800107 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 }
chaviw1d044282017-09-27 12:19:28 -0700112}
113
114void LayerProtoHelper::writeToProto(const sp<GraphicBuffer>& buffer,
Nataniel Borges797b0e42019-02-15 14:11:58 -0800115 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 }
chaviw1d044282017-09-27 12:19:28 -0700125}
126
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700127void 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();
Michael Wright44753b12020-07-08 13:48:11 +0100135 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));
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700141
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);
Vishnu Nair47074b82020-08-14 11:54:47 -0700150 proto->set_focusable(inputInfo.focusable);
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700151 proto->set_has_wallpaper(inputInfo.hasWallpaper);
152
153 proto->set_global_scale_factor(inputInfo.globalScaleFactor);
chaviw1ff3d1e2020-07-01 15:53:47 -0700154 LayerProtoHelper::writeToProto(inputInfo.transform, proto->mutable_transform());
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700155 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
chaviwddeae262020-01-06 10:31:23 -0800165void 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
chaviw1d044282017-09-27 12:19:28 -0700173} // namespace surfaceflinger
174} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800175
176// TODO(b/129481165): remove the #pragma below and fix conversion issues
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100177#pragma clang diagnostic pop // ignored "-Wconversion -Wextra"