| chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 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 |  | 
| Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 17 | // TODO(b/129481165): remove the #pragma below and fix conversion issues | 
|  | 18 | #pragma clang diagnostic push | 
|  | 19 | #pragma clang diagnostic ignored "-Wconversion" | 
|  | 20 |  | 
| chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 21 | #include "LayerProtoHelper.h" | 
|  | 22 |  | 
|  | 23 | namespace android { | 
|  | 24 | namespace surfaceflinger { | 
| Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 25 |  | 
|  | 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 |  | 
|  | 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 |  | 
|  | 46 | void LayerProtoHelper::writeToProto(const Region& region, | 
|  | 47 | std::function<RegionProto*()> getRegionProto) { | 
|  | 48 | if (region.isEmpty()) { | 
|  | 49 | return; | 
|  | 50 | } | 
|  | 51 |  | 
| chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 52 | Region::const_iterator head = region.begin(); | 
|  | 53 | Region::const_iterator const tail = region.end(); | 
| Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 54 | // Use a lambda do avoid writing the object header when the object is empty | 
|  | 55 | RegionProto* regionProto = getRegionProto(); | 
| chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 56 | while (head != tail) { | 
| Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 57 | std::function<RectProto*()> getProtoRect = [&]() { return regionProto->add_rect(); }; | 
|  | 58 | writeToProto(*head, getProtoRect); | 
| chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 59 | head++; | 
|  | 60 | } | 
|  | 61 | } | 
|  | 62 |  | 
| Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 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 | } | 
| chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 72 | } | 
|  | 73 |  | 
| Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 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 | } | 
| Yiwei Zhang | 7124ad3 | 2018-02-21 13:02:45 -0800 | [diff] [blame] | 84 | } | 
|  | 85 |  | 
| Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 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 | } | 
| chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 95 | } | 
|  | 96 |  | 
| Peiyong Lin | efefaac | 2018-08-17 12:27:51 -0700 | [diff] [blame] | 97 | void LayerProtoHelper::writeToProto(const ui::Transform& transform, | 
|  | 98 | TransformProto* transformProto) { | 
| Nataniel Borges | 84a4618 | 2019-02-26 15:36:32 -0800 | [diff] [blame] | 99 | const uint32_t type = transform.getType() | (transform.getOrientation() << 8); | 
| Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 100 | transformProto->set_type(type); | 
|  | 101 |  | 
| Nataniel Borges | 9a2433c | 2019-03-13 15:02:45 -0700 | [diff] [blame] | 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. | 
| Nataniel Borges | 84a4618 | 2019-02-26 15:36:32 -0800 | [diff] [blame] | 105 | if (type & (ui::Transform::SCALE | ui::Transform::UNKNOWN)) { | 
| Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 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 | } | 
| chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 111 | } | 
|  | 112 |  | 
|  | 113 | void LayerProtoHelper::writeToProto(const sp<GraphicBuffer>& buffer, | 
| Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 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 | } | 
| chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 124 | } | 
|  | 125 |  | 
| Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame] | 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 |  | 
| chaviw | ddeae26 | 2020-01-06 10:31:23 -0800 | [diff] [blame] | 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 |  | 
| chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 170 | } // namespace surfaceflinger | 
|  | 171 | } // namespace android | 
| Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 172 |  | 
|  | 173 | // TODO(b/129481165): remove the #pragma below and fix conversion issues | 
|  | 174 | #pragma clang diagnostic pop // ignored "-Wconversion" |