blob: 79f7b1f1e87ddb90089b3c924e021ec7572f7f25 [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
chaviw1d044282017-09-27 12:19:28 -070056 Region::const_iterator head = region.begin();
57 Region::const_iterator const tail = region.end();
Nataniel Borges797b0e42019-02-15 14:11:58 -080058 // Use a lambda do avoid writing the object header when the object is empty
59 RegionProto* regionProto = getRegionProto();
chaviw1d044282017-09-27 12:19:28 -070060 while (head != tail) {
Nataniel Borges797b0e42019-02-15 14:11:58 -080061 std::function<RectProto*()> getProtoRect = [&]() { return regionProto->add_rect(); };
62 writeToProto(*head, getProtoRect);
chaviw1d044282017-09-27 12:19:28 -070063 head++;
64 }
65}
66
Nataniel Borges797b0e42019-02-15 14:11:58 -080067void 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 }
chaviw1d044282017-09-27 12:19:28 -070076}
77
Nataniel Borges797b0e42019-02-15 14:11:58 -080078void 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 Zhang7124ad32018-02-21 13:02:45 -080088}
89
Nataniel Borges797b0e42019-02-15 14:11:58 -080090void 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 }
chaviw1d044282017-09-27 12:19:28 -070099}
100
Peiyong Linefefaac2018-08-17 12:27:51 -0700101void LayerProtoHelper::writeToProto(const ui::Transform& transform,
102 TransformProto* transformProto) {
Nataniel Borges84a46182019-02-26 15:36:32 -0800103 const uint32_t type = transform.getType() | (transform.getOrientation() << 8);
Nataniel Borges797b0e42019-02-15 14:11:58 -0800104 transformProto->set_type(type);
105
Nataniel Borges9a2433c2019-03-13 15:02:45 -0700106 // 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 Borges84a46182019-02-26 15:36:32 -0800109 if (type & (ui::Transform::SCALE | ui::Transform::UNKNOWN)) {
Nataniel Borges797b0e42019-02-15 14:11:58 -0800110 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 }
chaviw1d044282017-09-27 12:19:28 -0700115}
116
117void LayerProtoHelper::writeToProto(const sp<GraphicBuffer>& buffer,
Nataniel Borges797b0e42019-02-15 14:11:58 -0800118 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 }
chaviw1d044282017-09-27 12:19:28 -0700128}
129
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700130void LayerProtoHelper::writeToProto(
chaviw98318de2021-05-19 16:45:23 -0500131 const WindowInfo& inputInfo, const wp<Layer>& touchableRegionBounds,
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700132 std::function<InputWindowInfoProto*()> getInputWindowInfoProto) {
133 if (inputInfo.token == nullptr) {
134 return;
135 }
136
137 InputWindowInfoProto* proto = getInputWindowInfoProto();
Michael Wright44753b12020-07-08 13:48:11 +0100138 proto->set_layout_params_flags(inputInfo.flags.get());
chaviw98318de2021-05-19 16:45:23 -0500139 using U = std::underlying_type_t<WindowInfo::Type>;
Michael Wright44753b12020-07-08 13:48:11 +0100140 // 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 Nair9245d3b2019-03-22 13:38:56 -0700144
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 Nair47074b82020-08-14 11:54:47 -0700153 proto->set_focusable(inputInfo.focusable);
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700154 proto->set_has_wallpaper(inputInfo.hasWallpaper);
155
156 proto->set_global_scale_factor(inputInfo.globalScaleFactor);
chaviw1ff3d1e2020-07-01 15:53:47 -0700157 LayerProtoHelper::writeToProto(inputInfo.transform, proto->mutable_transform());
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700158 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
chaviwddeae262020-01-06 10:31:23 -0800168void 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
chaviw1d044282017-09-27 12:19:28 -0700176} // namespace surfaceflinger
177} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800178
179// TODO(b/129481165): remove the #pragma below and fix conversion issues
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100180#pragma clang diagnostic pop // ignored "-Wconversion -Wextra"