Evan Rosky | 1f6d6d5 | 2018-12-06 10:47:26 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2019 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 <android-base/stringprintf.h> |
| 18 | #include <binder/Parcel.h> |
| 19 | #include <gui/LayerMetadata.h> |
| 20 | |
| 21 | using android::base::StringPrintf; |
| 22 | |
| 23 | namespace android { |
| 24 | |
| 25 | LayerMetadata::LayerMetadata() = default; |
| 26 | |
| 27 | LayerMetadata::LayerMetadata(std::unordered_map<uint32_t, std::vector<uint8_t>> map) |
| 28 | : mMap(std::move(map)) {} |
| 29 | |
| 30 | LayerMetadata::LayerMetadata(const LayerMetadata& other) = default; |
| 31 | |
| 32 | LayerMetadata::LayerMetadata(LayerMetadata&& other) = default; |
| 33 | |
| 34 | void LayerMetadata::merge(const LayerMetadata& other) { |
| 35 | for (const auto& entry : other.mMap) { |
| 36 | mMap[entry.first] = entry.second; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | status_t LayerMetadata::writeToParcel(Parcel* parcel) const { |
| 41 | parcel->writeInt32(static_cast<int>(mMap.size())); |
| 42 | status_t status = OK; |
| 43 | for (const auto& entry : mMap) { |
| 44 | status = parcel->writeUint32(entry.first); |
| 45 | if (status != OK) { |
| 46 | break; |
| 47 | } |
| 48 | status = parcel->writeByteVector(entry.second); |
| 49 | if (status != OK) { |
| 50 | break; |
| 51 | } |
| 52 | } |
| 53 | return status; |
| 54 | } |
| 55 | |
| 56 | status_t LayerMetadata::readFromParcel(const Parcel* parcel) { |
| 57 | int size = parcel->readInt32(); |
| 58 | status_t status = OK; |
| 59 | mMap.clear(); |
| 60 | for (int i = 0; i < size; ++i) { |
| 61 | uint32_t key = parcel->readUint32(); |
| 62 | status = parcel->readByteVector(&mMap[key]); |
| 63 | if (status != OK) { |
| 64 | break; |
| 65 | } |
| 66 | } |
| 67 | return status; |
| 68 | } |
| 69 | |
| 70 | LayerMetadata& LayerMetadata::operator=(const LayerMetadata& other) { |
| 71 | mMap = other.mMap; |
| 72 | return *this; |
| 73 | } |
| 74 | |
| 75 | LayerMetadata& LayerMetadata::operator=(LayerMetadata&& other) { |
| 76 | mMap = std::move(other.mMap); |
| 77 | return *this; |
| 78 | } |
| 79 | |
| 80 | bool LayerMetadata::has(uint32_t key) const { |
| 81 | return mMap.count(key); |
| 82 | } |
| 83 | |
| 84 | int32_t LayerMetadata::getInt32(uint32_t key, int32_t fallback) const { |
| 85 | if (!has(key)) return fallback; |
| 86 | const std::vector<uint8_t>& data = mMap.at(key); |
| 87 | if (data.size() < sizeof(uint32_t)) return fallback; |
| 88 | Parcel p; |
| 89 | p.setData(data.data(), data.size()); |
| 90 | return p.readInt32(); |
| 91 | } |
| 92 | |
| 93 | void LayerMetadata::setInt32(uint32_t key, int32_t value) { |
| 94 | std::vector<uint8_t>& data = mMap[key]; |
| 95 | Parcel p; |
| 96 | p.writeInt32(value); |
| 97 | data.resize(p.dataSize()); |
| 98 | memcpy(data.data(), p.data(), p.dataSize()); |
| 99 | } |
| 100 | |
| 101 | std::string LayerMetadata::itemToString(uint32_t key, const char* separator) const { |
| 102 | if (!has(key)) return std::string(); |
| 103 | switch (key) { |
| 104 | case METADATA_OWNER_UID: |
| 105 | return StringPrintf("ownerUID%s%d", separator, getInt32(key, 0)); |
| 106 | case METADATA_WINDOW_TYPE: |
| 107 | return StringPrintf("windowType%s%d", separator, getInt32(key, 0)); |
| 108 | default: |
| 109 | return StringPrintf("%d%s%dbytes", key, separator, |
| 110 | static_cast<int>(mMap.at(key).size())); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | } // namespace android |