| Courtney Goeltzenleuchter | 9bad0d7 | 2017-12-19 12:34:34 -0700 | [diff] [blame] | 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> | 
| Valerie Hau | a82679d | 2018-11-21 09:31:43 -0800 | [diff] [blame] | 18 | #include <limits> | 
| Courtney Goeltzenleuchter | 9bad0d7 | 2017-12-19 12:34:34 -0700 | [diff] [blame] | 19 |  | 
|  | 20 | namespace android { | 
|  | 21 |  | 
|  | 22 | 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 | } | 
| Valerie Hau | a82679d | 2018-11-21 09:31:43 -0800 | [diff] [blame] | 30 | if (validTypes & HDR10PLUS) { | 
| williamtai | e409e38 | 2019-09-09 10:08:22 +0800 | [diff] [blame] | 31 | size += sizeof(uint32_t); | 
|  | 32 | size += hdr10plus.size() * sizeof(hdr10plus[0]); | 
| Valerie Hau | a82679d | 2018-11-21 09:31:43 -0800 | [diff] [blame] | 33 | } | 
| Courtney Goeltzenleuchter | 9bad0d7 | 2017-12-19 12:34:34 -0700 | [diff] [blame] | 34 | return size; | 
|  | 35 | } | 
|  | 36 |  | 
|  | 37 | 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 | } | 
| Valerie Hau | a82679d | 2018-11-21 09:31:43 -0800 | [diff] [blame] | 49 | if (validTypes & HDR10PLUS) { | 
| williamtai | e409e38 | 2019-09-09 10:08:22 +0800 | [diff] [blame] | 50 | uint32_t metadataSize = hdr10plus.size(); | 
| Valerie Hau | a82679d | 2018-11-21 09:31:43 -0800 | [diff] [blame] | 51 | FlattenableUtils::write(buffer, size, metadataSize); | 
| williamtai | e409e38 | 2019-09-09 10:08:22 +0800 | [diff] [blame] | 52 | size_t metadataSizeinByte = metadataSize * sizeof(hdr10plus[0]); | 
|  | 53 | memcpy(buffer, hdr10plus.data(), metadataSizeinByte); | 
|  | 54 | FlattenableUtils::advance(buffer, size, metadataSizeinByte); | 
| Valerie Hau | a82679d | 2018-11-21 09:31:43 -0800 | [diff] [blame] | 55 | } | 
| Courtney Goeltzenleuchter | 9bad0d7 | 2017-12-19 12:34:34 -0700 | [diff] [blame] | 56 |  | 
|  | 57 | return NO_ERROR; | 
|  | 58 | } | 
|  | 59 |  | 
|  | 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 | } | 
| Valerie Hau | a82679d | 2018-11-21 09:31:43 -0800 | [diff] [blame] | 77 | if (validTypes & HDR10PLUS) { | 
| williamtai | e409e38 | 2019-09-09 10:08:22 +0800 | [diff] [blame] | 78 | if (size < sizeof(uint32_t)) { | 
| Valerie Hau | a82679d | 2018-11-21 09:31:43 -0800 | [diff] [blame] | 79 | return NO_MEMORY; | 
|  | 80 | } | 
|  | 81 |  | 
| williamtai | e409e38 | 2019-09-09 10:08:22 +0800 | [diff] [blame] | 82 | uint32_t metadataSize; | 
| Valerie Hau | a82679d | 2018-11-21 09:31:43 -0800 | [diff] [blame] | 83 | FlattenableUtils::read(buffer, size, metadataSize); | 
|  | 84 |  | 
| williamtai | e409e38 | 2019-09-09 10:08:22 +0800 | [diff] [blame] | 85 | size_t metadataSizeinByte = metadataSize * sizeof(hdr10plus[0]); | 
|  | 86 | if (size < metadataSizeinByte) { | 
| Valerie Hau | a82679d | 2018-11-21 09:31:43 -0800 | [diff] [blame] | 87 | return NO_MEMORY; | 
|  | 88 | } | 
|  | 89 |  | 
|  | 90 | hdr10plus.resize(metadataSize); | 
| williamtai | e409e38 | 2019-09-09 10:08:22 +0800 | [diff] [blame] | 91 | memcpy(hdr10plus.data(), buffer, metadataSizeinByte); | 
|  | 92 | FlattenableUtils::advance(buffer, size, metadataSizeinByte); | 
| Valerie Hau | a82679d | 2018-11-21 09:31:43 -0800 | [diff] [blame] | 93 | } | 
| Courtney Goeltzenleuchter | 9bad0d7 | 2017-12-19 12:34:34 -0700 | [diff] [blame] | 94 |  | 
|  | 95 | return NO_ERROR; | 
|  | 96 | } | 
|  | 97 |  | 
| Courtney Goeltzenleuchter | 39a0788 | 2018-02-12 07:12:38 -0700 | [diff] [blame] | 98 | 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 |  | 
| Valerie Hau | a82679d | 2018-11-21 09:31:43 -0800 | [diff] [blame] | 123 | if ((validTypes & HDR10PLUS) == HDR10PLUS) { | 
|  | 124 | if (hdr10plus != rhs.hdr10plus) return false; | 
|  | 125 | } | 
|  | 126 |  | 
| Courtney Goeltzenleuchter | 39a0788 | 2018-02-12 07:12:38 -0700 | [diff] [blame] | 127 | return true; | 
|  | 128 | } | 
|  | 129 |  | 
| Courtney Goeltzenleuchter | 9bad0d7 | 2017-12-19 12:34:34 -0700 | [diff] [blame] | 130 | } // namespace android |