blob: 0fe1421926e530234a62677f6e9cca22d15882a1 [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"
20
chaviw1d044282017-09-27 12:19:28 -070021#include "LayerProtoHelper.h"
22
23namespace android {
24namespace surfaceflinger {
Nataniel Borges797b0e42019-02-15 14:11:58 -080025
26void LayerProtoHelper::writePositionToProto(const float x, const float y,
27 std::function<PositionProto*()> getPositionProto) {
28 if (x != 0 || y != 0) {
29 // Use a lambda do avoid writing the object header when the object is empty
30 PositionProto* position = getPositionProto();
31 position->set_x(x);
32 position->set_y(y);
33 }
34}
35
36void LayerProtoHelper::writeSizeToProto(const uint32_t w, const uint32_t h,
37 std::function<SizeProto*()> getSizeProto) {
38 if (w != 0 || h != 0) {
39 // Use a lambda do avoid writing the object header when the object is empty
40 SizeProto* size = getSizeProto();
41 size->set_w(w);
42 size->set_h(h);
43 }
44}
45
46void LayerProtoHelper::writeToProto(const Region& region,
47 std::function<RegionProto*()> getRegionProto) {
48 if (region.isEmpty()) {
49 return;
50 }
51
chaviw1d044282017-09-27 12:19:28 -070052 Region::const_iterator head = region.begin();
53 Region::const_iterator const tail = region.end();
Nataniel Borges797b0e42019-02-15 14:11:58 -080054 // Use a lambda do avoid writing the object header when the object is empty
55 RegionProto* regionProto = getRegionProto();
chaviw1d044282017-09-27 12:19:28 -070056 while (head != tail) {
Nataniel Borges797b0e42019-02-15 14:11:58 -080057 std::function<RectProto*()> getProtoRect = [&]() { return regionProto->add_rect(); };
58 writeToProto(*head, getProtoRect);
chaviw1d044282017-09-27 12:19:28 -070059 head++;
60 }
61}
62
Nataniel Borges797b0e42019-02-15 14:11:58 -080063void LayerProtoHelper::writeToProto(const Rect& rect, std::function<RectProto*()> getRectProto) {
64 if (rect.left != 0 || rect.right != 0 || rect.top != 0 || rect.bottom != 0) {
65 // Use a lambda do avoid writing the object header when the object is empty
66 RectProto* rectProto = getRectProto();
67 rectProto->set_left(rect.left);
68 rectProto->set_top(rect.top);
69 rectProto->set_bottom(rect.bottom);
70 rectProto->set_right(rect.right);
71 }
chaviw1d044282017-09-27 12:19:28 -070072}
73
Nataniel Borges797b0e42019-02-15 14:11:58 -080074void LayerProtoHelper::writeToProto(const FloatRect& rect,
75 std::function<FloatRectProto*()> getFloatRectProto) {
76 if (rect.left != 0 || rect.right != 0 || rect.top != 0 || rect.bottom != 0) {
77 // Use a lambda do avoid writing the object header when the object is empty
78 FloatRectProto* rectProto = getFloatRectProto();
79 rectProto->set_left(rect.left);
80 rectProto->set_top(rect.top);
81 rectProto->set_bottom(rect.bottom);
82 rectProto->set_right(rect.right);
83 }
Yiwei Zhang7124ad32018-02-21 13:02:45 -080084}
85
Nataniel Borges797b0e42019-02-15 14:11:58 -080086void LayerProtoHelper::writeToProto(const half4 color, std::function<ColorProto*()> getColorProto) {
87 if (color.r != 0 || color.g != 0 || color.b != 0 || color.a != 0) {
88 // Use a lambda do avoid writing the object header when the object is empty
89 ColorProto* colorProto = getColorProto();
90 colorProto->set_r(color.r);
91 colorProto->set_g(color.g);
92 colorProto->set_b(color.b);
93 colorProto->set_a(color.a);
94 }
chaviw1d044282017-09-27 12:19:28 -070095}
96
Peiyong Linefefaac2018-08-17 12:27:51 -070097void LayerProtoHelper::writeToProto(const ui::Transform& transform,
98 TransformProto* transformProto) {
Nataniel Borges84a46182019-02-26 15:36:32 -080099 const uint32_t type = transform.getType() | (transform.getOrientation() << 8);
Nataniel Borges797b0e42019-02-15 14:11:58 -0800100 transformProto->set_type(type);
101
Nataniel Borges9a2433c2019-03-13 15:02:45 -0700102 // Rotations that are 90/180/270 have their own type so the transform matrix can be
103 // reconstructed later. All other rotation have the type UKNOWN so we need to save the transform
104 // values in that case.
Nataniel Borges84a46182019-02-26 15:36:32 -0800105 if (type & (ui::Transform::SCALE | ui::Transform::UNKNOWN)) {
Nataniel Borges797b0e42019-02-15 14:11:58 -0800106 transformProto->set_dsdx(transform[0][0]);
107 transformProto->set_dtdx(transform[0][1]);
108 transformProto->set_dsdy(transform[1][0]);
109 transformProto->set_dtdy(transform[1][1]);
110 }
chaviw1d044282017-09-27 12:19:28 -0700111}
112
113void LayerProtoHelper::writeToProto(const sp<GraphicBuffer>& buffer,
Nataniel Borges797b0e42019-02-15 14:11:58 -0800114 std::function<ActiveBufferProto*()> getActiveBufferProto) {
115 if (buffer->getWidth() != 0 || buffer->getHeight() != 0 || buffer->getStride() != 0 ||
116 buffer->format != 0) {
117 // Use a lambda do avoid writing the object header when the object is empty
118 ActiveBufferProto* activeBufferProto = getActiveBufferProto();
119 activeBufferProto->set_width(buffer->getWidth());
120 activeBufferProto->set_height(buffer->getHeight());
121 activeBufferProto->set_stride(buffer->getStride());
122 activeBufferProto->set_format(buffer->format);
123 }
chaviw1d044282017-09-27 12:19:28 -0700124}
125
Vishnu Nair9245d3b2019-03-22 13:38:56 -0700126void LayerProtoHelper::writeToProto(
127 const InputWindowInfo& inputInfo, const wp<Layer>& touchableRegionBounds,
128 std::function<InputWindowInfoProto*()> getInputWindowInfoProto) {
129 if (inputInfo.token == nullptr) {
130 return;
131 }
132
133 InputWindowInfoProto* proto = getInputWindowInfoProto();
134 proto->set_layout_params_flags(inputInfo.layoutParamsFlags);
135 proto->set_layout_params_type(inputInfo.layoutParamsType);
136
137 LayerProtoHelper::writeToProto({inputInfo.frameLeft, inputInfo.frameTop, inputInfo.frameRight,
138 inputInfo.frameBottom},
139 [&]() { return proto->mutable_frame(); });
140 LayerProtoHelper::writeToProto(inputInfo.touchableRegion,
141 [&]() { return proto->mutable_touchable_region(); });
142
143 proto->set_surface_inset(inputInfo.surfaceInset);
144 proto->set_visible(inputInfo.visible);
145 proto->set_can_receive_keys(inputInfo.canReceiveKeys);
146 proto->set_has_focus(inputInfo.hasFocus);
147 proto->set_has_wallpaper(inputInfo.hasWallpaper);
148
149 proto->set_global_scale_factor(inputInfo.globalScaleFactor);
150 proto->set_window_x_scale(inputInfo.windowXScale);
151 proto->set_window_y_scale(inputInfo.windowYScale);
152 proto->set_replace_touchable_region_with_crop(inputInfo.replaceTouchableRegionWithCrop);
153 auto cropLayer = touchableRegionBounds.promote();
154 if (cropLayer != nullptr) {
155 proto->set_crop_layer_id(cropLayer->sequence);
156 LayerProtoHelper::writeToProto(cropLayer->getScreenBounds(
157 false /* reduceTransparentRegion */),
158 [&]() { return proto->mutable_touchable_region_crop(); });
159 }
160}
161
chaviwddeae262020-01-06 10:31:23 -0800162void LayerProtoHelper::writeToProto(const mat4 matrix, ColorTransformProto* colorTransformProto) {
163 for (int i = 0; i < mat4::ROW_SIZE; i++) {
164 for (int j = 0; j < mat4::COL_SIZE; j++) {
165 colorTransformProto->add_val(matrix[i][j]);
166 }
167 }
168}
169
chaviw1d044282017-09-27 12:19:28 -0700170} // namespace surfaceflinger
171} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800172
173// TODO(b/129481165): remove the #pragma below and fix conversion issues
174#pragma clang diagnostic pop // ignored "-Wconversion"