| 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) { | 
|  | 31 | size += sizeof(size_t); | 
|  | 32 | size += hdr10plus.size(); | 
|  | 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) { | 
|  | 50 | size_t metadataSize = hdr10plus.size(); | 
|  | 51 | FlattenableUtils::write(buffer, size, metadataSize); | 
|  | 52 | memcpy(buffer, hdr10plus.data(), metadataSize); | 
|  | 53 | FlattenableUtils::advance(buffer, size, metadataSize); | 
|  | 54 | } | 
| Courtney Goeltzenleuchter | 9bad0d7 | 2017-12-19 12:34:34 -0700 | [diff] [blame] | 55 |  | 
|  | 56 | return NO_ERROR; | 
|  | 57 | } | 
|  | 58 |  | 
|  | 59 | status_t HdrMetadata::unflatten(void const* buffer, size_t size) { | 
|  | 60 | if (size < sizeof(validTypes)) { | 
|  | 61 | return NO_MEMORY; | 
|  | 62 | } | 
|  | 63 | FlattenableUtils::read(buffer, size, validTypes); | 
|  | 64 | if (validTypes & SMPTE2086) { | 
|  | 65 | if (size < sizeof(smpte2086)) { | 
|  | 66 | return NO_MEMORY; | 
|  | 67 | } | 
|  | 68 | FlattenableUtils::read(buffer, size, smpte2086); | 
|  | 69 | } | 
|  | 70 | if (validTypes & CTA861_3) { | 
|  | 71 | if (size < sizeof(cta8613)) { | 
|  | 72 | return NO_MEMORY; | 
|  | 73 | } | 
|  | 74 | FlattenableUtils::read(buffer, size, cta8613); | 
|  | 75 | } | 
| Valerie Hau | a82679d | 2018-11-21 09:31:43 -0800 | [diff] [blame] | 76 | if (validTypes & HDR10PLUS) { | 
|  | 77 | if (size < sizeof(size_t)) { | 
|  | 78 | return NO_MEMORY; | 
|  | 79 | } | 
|  | 80 |  | 
|  | 81 | size_t metadataSize; | 
|  | 82 | FlattenableUtils::read(buffer, size, metadataSize); | 
|  | 83 |  | 
|  | 84 | if (size < metadataSize) { | 
|  | 85 | return NO_MEMORY; | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | hdr10plus.resize(metadataSize); | 
|  | 89 | memcpy(hdr10plus.data(), buffer, metadataSize); | 
|  | 90 | FlattenableUtils::advance(buffer, size, metadataSize); | 
|  | 91 | } | 
| Courtney Goeltzenleuchter | 9bad0d7 | 2017-12-19 12:34:34 -0700 | [diff] [blame] | 92 |  | 
|  | 93 | return NO_ERROR; | 
|  | 94 | } | 
|  | 95 |  | 
| Courtney Goeltzenleuchter | 39a0788 | 2018-02-12 07:12:38 -0700 | [diff] [blame] | 96 | bool HdrMetadata::operator==(const HdrMetadata& rhs) const { | 
|  | 97 | if (validTypes != rhs.validTypes) return false; | 
|  | 98 |  | 
|  | 99 | if ((validTypes & SMPTE2086) == SMPTE2086) { | 
|  | 100 | if (smpte2086.displayPrimaryRed.x != rhs.smpte2086.displayPrimaryRed.x || | 
|  | 101 | smpte2086.displayPrimaryRed.y != rhs.smpte2086.displayPrimaryRed.y || | 
|  | 102 | smpte2086.displayPrimaryGreen.x != rhs.smpte2086.displayPrimaryGreen.x || | 
|  | 103 | smpte2086.displayPrimaryGreen.y != rhs.smpte2086.displayPrimaryGreen.y || | 
|  | 104 | smpte2086.displayPrimaryBlue.x != rhs.smpte2086.displayPrimaryBlue.x || | 
|  | 105 | smpte2086.displayPrimaryBlue.y != rhs.smpte2086.displayPrimaryBlue.y || | 
|  | 106 | smpte2086.whitePoint.x != rhs.smpte2086.whitePoint.x || | 
|  | 107 | smpte2086.whitePoint.y != rhs.smpte2086.whitePoint.y || | 
|  | 108 | smpte2086.maxLuminance != rhs.smpte2086.maxLuminance || | 
|  | 109 | smpte2086.minLuminance != rhs.smpte2086.minLuminance) { | 
|  | 110 | return false; | 
|  | 111 | } | 
|  | 112 | } | 
|  | 113 |  | 
|  | 114 | if ((validTypes & CTA861_3) == CTA861_3) { | 
|  | 115 | if (cta8613.maxFrameAverageLightLevel != rhs.cta8613.maxFrameAverageLightLevel || | 
|  | 116 | cta8613.maxContentLightLevel != rhs.cta8613.maxContentLightLevel) { | 
|  | 117 | return false; | 
|  | 118 | } | 
|  | 119 | } | 
|  | 120 |  | 
| Valerie Hau | a82679d | 2018-11-21 09:31:43 -0800 | [diff] [blame] | 121 | if ((validTypes & HDR10PLUS) == HDR10PLUS) { | 
|  | 122 | if (hdr10plus != rhs.hdr10plus) return false; | 
|  | 123 | } | 
|  | 124 |  | 
| Courtney Goeltzenleuchter | 39a0788 | 2018-02-12 07:12:38 -0700 | [diff] [blame] | 125 | return true; | 
|  | 126 | } | 
|  | 127 |  | 
| Courtney Goeltzenleuchter | 9bad0d7 | 2017-12-19 12:34:34 -0700 | [diff] [blame] | 128 | } // namespace android |