1 /*
2 * Copyright 2018 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 <gui/HdrMetadata.h>
18 #include <limits>
19
20 namespace android {
21
getFlattenedSize() const22 size_t HdrMetadata::getFlattenedSize() const {
23 size_t size = sizeof(validTypes);
24 if (validTypes & SMPTE2086) {
25 size += sizeof(smpte2086);
26 }
27 if (validTypes & CTA861_3) {
28 size += sizeof(cta8613);
29 }
30 if (validTypes & HDR10PLUS) {
31 size += sizeof(uint32_t);
32 size += hdr10plus.size() * sizeof(hdr10plus[0]);
33 }
34 return size;
35 }
36
flatten(void * buffer,size_t size) const37 status_t HdrMetadata::flatten(void* buffer, size_t size) const {
38 if (size < getFlattenedSize()) {
39 return NO_MEMORY;
40 }
41
42 FlattenableUtils::write(buffer, size, validTypes);
43 if (validTypes & SMPTE2086) {
44 FlattenableUtils::write(buffer, size, smpte2086);
45 }
46 if (validTypes & CTA861_3) {
47 FlattenableUtils::write(buffer, size, cta8613);
48 }
49 if (validTypes & HDR10PLUS) {
50 uint32_t metadataSize = hdr10plus.size();
51 FlattenableUtils::write(buffer, size, metadataSize);
52 size_t metadataSizeinByte = metadataSize * sizeof(hdr10plus[0]);
53 memcpy(buffer, hdr10plus.data(), metadataSizeinByte);
54 FlattenableUtils::advance(buffer, size, metadataSizeinByte);
55 }
56
57 return NO_ERROR;
58 }
59
unflatten(void const * buffer,size_t size)60 status_t HdrMetadata::unflatten(void const* buffer, size_t size) {
61 if (size < sizeof(validTypes)) {
62 return NO_MEMORY;
63 }
64 FlattenableUtils::read(buffer, size, validTypes);
65 if (validTypes & SMPTE2086) {
66 if (size < sizeof(smpte2086)) {
67 return NO_MEMORY;
68 }
69 FlattenableUtils::read(buffer, size, smpte2086);
70 }
71 if (validTypes & CTA861_3) {
72 if (size < sizeof(cta8613)) {
73 return NO_MEMORY;
74 }
75 FlattenableUtils::read(buffer, size, cta8613);
76 }
77 if (validTypes & HDR10PLUS) {
78 if (size < sizeof(uint32_t)) {
79 return NO_MEMORY;
80 }
81
82 uint32_t metadataSize;
83 FlattenableUtils::read(buffer, size, metadataSize);
84
85 size_t metadataSizeinByte = metadataSize * sizeof(hdr10plus[0]);
86 if (size < metadataSizeinByte) {
87 return NO_MEMORY;
88 }
89
90 hdr10plus.resize(metadataSize);
91 memcpy(hdr10plus.data(), buffer, metadataSizeinByte);
92 FlattenableUtils::advance(buffer, size, metadataSizeinByte);
93 }
94
95 return NO_ERROR;
96 }
97
operator ==(const HdrMetadata & rhs) const98 bool HdrMetadata::operator==(const HdrMetadata& rhs) const {
99 if (validTypes != rhs.validTypes) return false;
100
101 if ((validTypes & SMPTE2086) == SMPTE2086) {
102 if (smpte2086.displayPrimaryRed.x != rhs.smpte2086.displayPrimaryRed.x ||
103 smpte2086.displayPrimaryRed.y != rhs.smpte2086.displayPrimaryRed.y ||
104 smpte2086.displayPrimaryGreen.x != rhs.smpte2086.displayPrimaryGreen.x ||
105 smpte2086.displayPrimaryGreen.y != rhs.smpte2086.displayPrimaryGreen.y ||
106 smpte2086.displayPrimaryBlue.x != rhs.smpte2086.displayPrimaryBlue.x ||
107 smpte2086.displayPrimaryBlue.y != rhs.smpte2086.displayPrimaryBlue.y ||
108 smpte2086.whitePoint.x != rhs.smpte2086.whitePoint.x ||
109 smpte2086.whitePoint.y != rhs.smpte2086.whitePoint.y ||
110 smpte2086.maxLuminance != rhs.smpte2086.maxLuminance ||
111 smpte2086.minLuminance != rhs.smpte2086.minLuminance) {
112 return false;
113 }
114 }
115
116 if ((validTypes & CTA861_3) == CTA861_3) {
117 if (cta8613.maxFrameAverageLightLevel != rhs.cta8613.maxFrameAverageLightLevel ||
118 cta8613.maxContentLightLevel != rhs.cta8613.maxContentLightLevel) {
119 return false;
120 }
121 }
122
123 if ((validTypes & HDR10PLUS) == HDR10PLUS) {
124 if (hdr10plus != rhs.hdr10plus) return false;
125 }
126
127 return true;
128 }
129
130 } // namespace android
131