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 | |
| 17 | #include "LayerProtoHelper.h" |
| 18 | |
| 19 | namespace android { |
| 20 | namespace surfaceflinger { |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 21 | |
| 22 | void LayerProtoHelper::writePositionToProto(const float x, const float y, |
| 23 | std::function<PositionProto*()> getPositionProto) { |
| 24 | if (x != 0 || y != 0) { |
| 25 | // Use a lambda do avoid writing the object header when the object is empty |
| 26 | PositionProto* position = getPositionProto(); |
| 27 | position->set_x(x); |
| 28 | position->set_y(y); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | void LayerProtoHelper::writeSizeToProto(const uint32_t w, const uint32_t h, |
| 33 | std::function<SizeProto*()> getSizeProto) { |
| 34 | if (w != 0 || h != 0) { |
| 35 | // Use a lambda do avoid writing the object header when the object is empty |
| 36 | SizeProto* size = getSizeProto(); |
| 37 | size->set_w(w); |
| 38 | size->set_h(h); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | void LayerProtoHelper::writeToProto(const Region& region, |
| 43 | std::function<RegionProto*()> getRegionProto) { |
| 44 | if (region.isEmpty()) { |
| 45 | return; |
| 46 | } |
| 47 | |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 48 | Region::const_iterator head = region.begin(); |
| 49 | Region::const_iterator const tail = region.end(); |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 50 | // Use a lambda do avoid writing the object header when the object is empty |
| 51 | RegionProto* regionProto = getRegionProto(); |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 52 | while (head != tail) { |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 53 | std::function<RectProto*()> getProtoRect = [&]() { return regionProto->add_rect(); }; |
| 54 | writeToProto(*head, getProtoRect); |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 55 | head++; |
| 56 | } |
| 57 | } |
| 58 | |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 59 | void LayerProtoHelper::writeToProto(const Rect& rect, std::function<RectProto*()> getRectProto) { |
| 60 | if (rect.left != 0 || rect.right != 0 || rect.top != 0 || rect.bottom != 0) { |
| 61 | // Use a lambda do avoid writing the object header when the object is empty |
| 62 | RectProto* rectProto = getRectProto(); |
| 63 | rectProto->set_left(rect.left); |
| 64 | rectProto->set_top(rect.top); |
| 65 | rectProto->set_bottom(rect.bottom); |
| 66 | rectProto->set_right(rect.right); |
| 67 | } |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 70 | void LayerProtoHelper::writeToProto(const FloatRect& rect, |
| 71 | std::function<FloatRectProto*()> getFloatRectProto) { |
| 72 | if (rect.left != 0 || rect.right != 0 || rect.top != 0 || rect.bottom != 0) { |
| 73 | // Use a lambda do avoid writing the object header when the object is empty |
| 74 | FloatRectProto* rectProto = getFloatRectProto(); |
| 75 | rectProto->set_left(rect.left); |
| 76 | rectProto->set_top(rect.top); |
| 77 | rectProto->set_bottom(rect.bottom); |
| 78 | rectProto->set_right(rect.right); |
| 79 | } |
Yiwei Zhang | 7124ad3 | 2018-02-21 13:02:45 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 82 | void LayerProtoHelper::writeToProto(const half4 color, std::function<ColorProto*()> getColorProto) { |
| 83 | if (color.r != 0 || color.g != 0 || color.b != 0 || color.a != 0) { |
| 84 | // Use a lambda do avoid writing the object header when the object is empty |
| 85 | ColorProto* colorProto = getColorProto(); |
| 86 | colorProto->set_r(color.r); |
| 87 | colorProto->set_g(color.g); |
| 88 | colorProto->set_b(color.b); |
| 89 | colorProto->set_a(color.a); |
| 90 | } |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Peiyong Lin | efefaac | 2018-08-17 12:27:51 -0700 | [diff] [blame] | 93 | void LayerProtoHelper::writeToProto(const ui::Transform& transform, |
| 94 | TransformProto* transformProto) { |
Nataniel Borges | 84a4618 | 2019-02-26 15:36:32 -0800 | [diff] [blame] | 95 | const uint32_t type = transform.getType() | (transform.getOrientation() << 8); |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 96 | transformProto->set_type(type); |
| 97 | |
Nataniel Borges | 9a2433c | 2019-03-13 15:02:45 -0700 | [diff] [blame] | 98 | // Rotations that are 90/180/270 have their own type so the transform matrix can be |
| 99 | // reconstructed later. All other rotation have the type UKNOWN so we need to save the transform |
| 100 | // values in that case. |
Nataniel Borges | 84a4618 | 2019-02-26 15:36:32 -0800 | [diff] [blame] | 101 | if (type & (ui::Transform::SCALE | ui::Transform::UNKNOWN)) { |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 102 | transformProto->set_dsdx(transform[0][0]); |
| 103 | transformProto->set_dtdx(transform[0][1]); |
| 104 | transformProto->set_dsdy(transform[1][0]); |
| 105 | transformProto->set_dtdy(transform[1][1]); |
| 106 | } |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | void LayerProtoHelper::writeToProto(const sp<GraphicBuffer>& buffer, |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame] | 110 | std::function<ActiveBufferProto*()> getActiveBufferProto) { |
| 111 | if (buffer->getWidth() != 0 || buffer->getHeight() != 0 || buffer->getStride() != 0 || |
| 112 | buffer->format != 0) { |
| 113 | // Use a lambda do avoid writing the object header when the object is empty |
| 114 | ActiveBufferProto* activeBufferProto = getActiveBufferProto(); |
| 115 | activeBufferProto->set_width(buffer->getWidth()); |
| 116 | activeBufferProto->set_height(buffer->getHeight()); |
| 117 | activeBufferProto->set_stride(buffer->getStride()); |
| 118 | activeBufferProto->set_format(buffer->format); |
| 119 | } |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Vishnu Nair | 9245d3b | 2019-03-22 13:38:56 -0700 | [diff] [blame^] | 122 | void LayerProtoHelper::writeToProto( |
| 123 | const InputWindowInfo& inputInfo, const wp<Layer>& touchableRegionBounds, |
| 124 | std::function<InputWindowInfoProto*()> getInputWindowInfoProto) { |
| 125 | if (inputInfo.token == nullptr) { |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | InputWindowInfoProto* proto = getInputWindowInfoProto(); |
| 130 | proto->set_layout_params_flags(inputInfo.layoutParamsFlags); |
| 131 | proto->set_layout_params_type(inputInfo.layoutParamsType); |
| 132 | |
| 133 | LayerProtoHelper::writeToProto({inputInfo.frameLeft, inputInfo.frameTop, inputInfo.frameRight, |
| 134 | inputInfo.frameBottom}, |
| 135 | [&]() { return proto->mutable_frame(); }); |
| 136 | LayerProtoHelper::writeToProto(inputInfo.touchableRegion, |
| 137 | [&]() { return proto->mutable_touchable_region(); }); |
| 138 | |
| 139 | proto->set_surface_inset(inputInfo.surfaceInset); |
| 140 | proto->set_visible(inputInfo.visible); |
| 141 | proto->set_can_receive_keys(inputInfo.canReceiveKeys); |
| 142 | proto->set_has_focus(inputInfo.hasFocus); |
| 143 | proto->set_has_wallpaper(inputInfo.hasWallpaper); |
| 144 | |
| 145 | proto->set_global_scale_factor(inputInfo.globalScaleFactor); |
| 146 | proto->set_window_x_scale(inputInfo.windowXScale); |
| 147 | proto->set_window_y_scale(inputInfo.windowYScale); |
| 148 | proto->set_replace_touchable_region_with_crop(inputInfo.replaceTouchableRegionWithCrop); |
| 149 | auto cropLayer = touchableRegionBounds.promote(); |
| 150 | if (cropLayer != nullptr) { |
| 151 | proto->set_crop_layer_id(cropLayer->sequence); |
| 152 | LayerProtoHelper::writeToProto(cropLayer->getScreenBounds( |
| 153 | false /* reduceTransparentRegion */), |
| 154 | [&]() { return proto->mutable_touchable_region_crop(); }); |
| 155 | } |
| 156 | } |
| 157 | |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 158 | } // namespace surfaceflinger |
| 159 | } // namespace android |