diff options
author | 2024-03-08 23:28:32 +0000 | |
---|---|---|
committer | 2024-03-18 22:34:42 +0000 | |
commit | 47504789e2d22b4ef5d98700992b0243e0cc8922 (patch) | |
tree | a464fc500b039976f73887310b32128f41267e84 | |
parent | e7feba5da3ab1a9cd8ca1c5cb8b60e68e0ec2f93 (diff) |
libgui: avoid allocations for bit manip
Avoid doing allocations when getting layer metadata
attributes.
Bug: 328177618
Test: TH
Change-Id: I63189bce152bd275bd46d9d2cdd4d7d02d282533
-rw-r--r-- | libs/gui/LayerMetadata.cpp | 26 |
1 files changed, 15 insertions, 11 deletions
diff --git a/libs/gui/LayerMetadata.cpp b/libs/gui/LayerMetadata.cpp index 4e12fd330c..535a0218b6 100644 --- a/libs/gui/LayerMetadata.cpp +++ b/libs/gui/LayerMetadata.cpp @@ -100,27 +100,31 @@ bool LayerMetadata::has(uint32_t key) const { int32_t LayerMetadata::getInt32(uint32_t key, int32_t fallback) const { if (!has(key)) return fallback; const std::vector<uint8_t>& data = mMap.at(key); - if (data.size() < sizeof(uint32_t)) return fallback; - Parcel p; - p.setData(data.data(), data.size()); - return p.readInt32(); + + // TODO: should handle when not equal? + if (data.size() < sizeof(int32_t)) return fallback; + + int32_t result; + memcpy(&result, data.data(), sizeof(result)); + return result; } void LayerMetadata::setInt32(uint32_t key, int32_t value) { std::vector<uint8_t>& data = mMap[key]; - Parcel p; - p.writeInt32(value); - data.resize(p.dataSize()); - memcpy(data.data(), p.data(), p.dataSize()); + data.resize(sizeof(value)); + memcpy(data.data(), &value, sizeof(value)); } std::optional<int64_t> LayerMetadata::getInt64(uint32_t key) const { if (!has(key)) return std::nullopt; const std::vector<uint8_t>& data = mMap.at(key); + + // TODO: should handle when not equal? if (data.size() < sizeof(int64_t)) return std::nullopt; - Parcel p; - p.setData(data.data(), data.size()); - return p.readInt64(); + + int64_t result; + memcpy(&result, data.data(), sizeof(result)); + return result; } std::string LayerMetadata::itemToString(uint32_t key, const char* separator) const { |