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 | uint64_t address = reinterpret_cast<uint64_t>(®ion); |
| 53 | regionProto->set_id(address); |
| 54 | while (head != tail) { |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame^] | 55 | std::function<RectProto*()> getProtoRect = [&]() { return regionProto->add_rect(); }; |
| 56 | writeToProto(*head, getProtoRect); |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 57 | head++; |
| 58 | } |
| 59 | } |
| 60 | |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame^] | 61 | void LayerProtoHelper::writeToProto(const Rect& rect, std::function<RectProto*()> getRectProto) { |
| 62 | if (rect.left != 0 || rect.right != 0 || rect.top != 0 || rect.bottom != 0) { |
| 63 | // Use a lambda do avoid writing the object header when the object is empty |
| 64 | RectProto* rectProto = getRectProto(); |
| 65 | rectProto->set_left(rect.left); |
| 66 | rectProto->set_top(rect.top); |
| 67 | rectProto->set_bottom(rect.bottom); |
| 68 | rectProto->set_right(rect.right); |
| 69 | } |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame^] | 72 | void LayerProtoHelper::writeToProto(const FloatRect& rect, |
| 73 | std::function<FloatRectProto*()> getFloatRectProto) { |
| 74 | if (rect.left != 0 || rect.right != 0 || rect.top != 0 || rect.bottom != 0) { |
| 75 | // Use a lambda do avoid writing the object header when the object is empty |
| 76 | FloatRectProto* rectProto = getFloatRectProto(); |
| 77 | rectProto->set_left(rect.left); |
| 78 | rectProto->set_top(rect.top); |
| 79 | rectProto->set_bottom(rect.bottom); |
| 80 | rectProto->set_right(rect.right); |
| 81 | } |
Yiwei Zhang | 7124ad3 | 2018-02-21 13:02:45 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame^] | 84 | void LayerProtoHelper::writeToProto(const half4 color, std::function<ColorProto*()> getColorProto) { |
| 85 | if (color.r != 0 || color.g != 0 || color.b != 0 || color.a != 0) { |
| 86 | // Use a lambda do avoid writing the object header when the object is empty |
| 87 | ColorProto* colorProto = getColorProto(); |
| 88 | colorProto->set_r(color.r); |
| 89 | colorProto->set_g(color.g); |
| 90 | colorProto->set_b(color.b); |
| 91 | colorProto->set_a(color.a); |
| 92 | } |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Peiyong Lin | efefaac | 2018-08-17 12:27:51 -0700 | [diff] [blame] | 95 | void LayerProtoHelper::writeToProto(const ui::Transform& transform, |
| 96 | TransformProto* transformProto) { |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame^] | 97 | const uint32_t type = transform.getType(); |
| 98 | transformProto->set_type(type); |
| 99 | |
| 100 | if (type & |
| 101 | (ui::Transform::SCALE | ui::Transform::ROTATE | ui::Transform::TRANSLATE | |
| 102 | ui::Transform::UNKNOWN)) { |
| 103 | transformProto->set_dsdx(transform[0][0]); |
| 104 | transformProto->set_dtdx(transform[0][1]); |
| 105 | transformProto->set_dsdy(transform[1][0]); |
| 106 | transformProto->set_dtdy(transform[1][1]); |
| 107 | } |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | void LayerProtoHelper::writeToProto(const sp<GraphicBuffer>& buffer, |
Nataniel Borges | 797b0e4 | 2019-02-15 14:11:58 -0800 | [diff] [blame^] | 111 | std::function<ActiveBufferProto*()> getActiveBufferProto) { |
| 112 | if (buffer->getWidth() != 0 || buffer->getHeight() != 0 || buffer->getStride() != 0 || |
| 113 | buffer->format != 0) { |
| 114 | // Use a lambda do avoid writing the object header when the object is empty |
| 115 | ActiveBufferProto* activeBufferProto = getActiveBufferProto(); |
| 116 | activeBufferProto->set_width(buffer->getWidth()); |
| 117 | activeBufferProto->set_height(buffer->getHeight()); |
| 118 | activeBufferProto->set_stride(buffer->getStride()); |
| 119 | activeBufferProto->set_format(buffer->format); |
| 120 | } |
chaviw | 1d04428 | 2017-09-27 12:19:28 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | } // namespace surfaceflinger |
| 124 | } // namespace android |