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 
21 #include "LayerProtoHelper.h"
22 
23 namespace android {
24 namespace surfaceflinger {
25 
writePositionToProto(const float x,const float y,std::function<PositionProto * ()> getPositionProto)26 void 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 
writeSizeToProto(const uint32_t w,const uint32_t h,std::function<SizeProto * ()> getSizeProto)36 void 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 
writeToProto(const Region & region,std::function<RegionProto * ()> getRegionProto)46 void LayerProtoHelper::writeToProto(const Region& region,
47                                     std::function<RegionProto*()> getRegionProto) {
48     if (region.isEmpty()) {
49         return;
50     }
51 
52     Region::const_iterator head = region.begin();
53     Region::const_iterator const tail = region.end();
54     // Use a lambda do avoid writing the object header when the object is empty
55     RegionProto* regionProto = getRegionProto();
56     while (head != tail) {
57         std::function<RectProto*()> getProtoRect = [&]() { return regionProto->add_rect(); };
58         writeToProto(*head, getProtoRect);
59         head++;
60     }
61 }
62 
writeToProto(const Rect & rect,std::function<RectProto * ()> getRectProto)63 void 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     }
72 }
73 
writeToProto(const FloatRect & rect,std::function<FloatRectProto * ()> getFloatRectProto)74 void 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     }
84 }
85 
writeToProto(const half4 color,std::function<ColorProto * ()> getColorProto)86 void 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     }
95 }
96 
writeToProto(const ui::Transform & transform,TransformProto * transformProto)97 void LayerProtoHelper::writeToProto(const ui::Transform& transform,
98                                     TransformProto* transformProto) {
99     const uint32_t type = transform.getType() | (transform.getOrientation() << 8);
100     transformProto->set_type(type);
101 
102     // 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.
105     if (type & (ui::Transform::SCALE | ui::Transform::UNKNOWN)) {
106         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     }
111 }
112 
writeToProto(const sp<GraphicBuffer> & buffer,std::function<ActiveBufferProto * ()> getActiveBufferProto)113 void LayerProtoHelper::writeToProto(const sp<GraphicBuffer>& buffer,
114                                     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     }
124 }
125 
writeToProto(const InputWindowInfo & inputInfo,const wp<Layer> & touchableRegionBounds,std::function<InputWindowInfoProto * ()> getInputWindowInfoProto)126 void 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 
writeToProto(const mat4 matrix,ColorTransformProto * colorTransformProto)162 void 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 
170 } // namespace surfaceflinger
171 } // namespace android
172 
173 // TODO(b/129481165): remove the #pragma below and fix conversion issues
174 #pragma clang diagnostic pop // ignored "-Wconversion"
175