blob: c8a2b0754bc970bab0c72ef00fe405f9f94cf79f [file] [log] [blame]
Evan Rosky1f6d6d52018-12-06 10:47:26 -08001/*
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
21using android::base::StringPrintf;
22
23namespace android {
24
25LayerMetadata::LayerMetadata() = default;
26
27LayerMetadata::LayerMetadata(std::unordered_map<uint32_t, std::vector<uint8_t>> map)
28 : mMap(std::move(map)) {}
29
30LayerMetadata::LayerMetadata(const LayerMetadata& other) = default;
31
32LayerMetadata::LayerMetadata(LayerMetadata&& other) = default;
33
34void LayerMetadata::merge(const LayerMetadata& other) {
35 for (const auto& entry : other.mMap) {
36 mMap[entry.first] = entry.second;
37 }
38}
39
40status_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
56status_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
70LayerMetadata& LayerMetadata::operator=(const LayerMetadata& other) {
71 mMap = other.mMap;
72 return *this;
73}
74
75LayerMetadata& LayerMetadata::operator=(LayerMetadata&& other) {
76 mMap = std::move(other.mMap);
77 return *this;
78}
79
80bool LayerMetadata::has(uint32_t key) const {
81 return mMap.count(key);
82}
83
84int32_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
93void 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
101std::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