| Dan Stoza | 7d7ae73 | 2016-03-16 12:23:40 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright 2016 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 <ui/HdrCapabilities.h> | 
|  | 18 |  | 
| Dan Stoza | 7d7ae73 | 2016-03-16 12:23:40 -0700 | [diff] [blame] | 19 | namespace android { | 
|  | 20 |  | 
| Mathias Agopian | e1f5e6f | 2017-02-06 16:34:41 -0800 | [diff] [blame] | 21 | #if defined(__clang__) | 
|  | 22 | #pragma clang diagnostic push | 
|  | 23 | #pragma clang diagnostic ignored "-Wundefined-reinterpret-cast" | 
|  | 24 | #endif | 
|  | 25 |  | 
|  | 26 | HdrCapabilities::~HdrCapabilities() = default; | 
| Chih-Hung Hsieh | 5bc849f | 2018-09-25 14:21:50 -0700 | [diff] [blame] | 27 | HdrCapabilities::HdrCapabilities(HdrCapabilities&& other) noexcept = default; | 
|  | 28 | HdrCapabilities& HdrCapabilities::operator=(HdrCapabilities&& other) noexcept = default; | 
| Mathias Agopian | e1f5e6f | 2017-02-06 16:34:41 -0800 | [diff] [blame] | 29 |  | 
| Mathias Agopian | e1f5e6f | 2017-02-06 16:34:41 -0800 | [diff] [blame] | 30 | size_t HdrCapabilities::getFlattenedSize() const { | 
|  | 31 | return  sizeof(mMaxLuminance) + | 
|  | 32 | sizeof(mMaxAverageLuminance) + | 
|  | 33 | sizeof(mMinLuminance) + | 
|  | 34 | sizeof(int32_t) + | 
| Peiyong Lin | 6266589 | 2018-04-16 11:07:44 -0700 | [diff] [blame] | 35 | mSupportedHdrTypes.size() * sizeof(ui::Hdr); | 
| Dan Stoza | 7d7ae73 | 2016-03-16 12:23:40 -0700 | [diff] [blame] | 36 | } | 
|  | 37 |  | 
| Mathias Agopian | e1f5e6f | 2017-02-06 16:34:41 -0800 | [diff] [blame] | 38 | status_t HdrCapabilities::flatten(void* buffer, size_t size) const { | 
|  | 39 |  | 
|  | 40 | if (size < getFlattenedSize()) { | 
|  | 41 | return NO_MEMORY; | 
| Dan Stoza | 7d7ae73 | 2016-03-16 12:23:40 -0700 | [diff] [blame] | 42 | } | 
| Mathias Agopian | e1f5e6f | 2017-02-06 16:34:41 -0800 | [diff] [blame] | 43 |  | 
|  | 44 | int32_t* const buf = static_cast<int32_t*>(buffer); | 
|  | 45 | reinterpret_cast<float&>(buf[0]) = mMaxLuminance; | 
|  | 46 | reinterpret_cast<float&>(buf[1]) = mMaxAverageLuminance; | 
|  | 47 | reinterpret_cast<float&>(buf[2]) = mMinLuminance; | 
|  | 48 | buf[3] = static_cast<int32_t>(mSupportedHdrTypes.size()); | 
|  | 49 | for (size_t i = 0, c = mSupportedHdrTypes.size(); i < c; ++i) { | 
| Peiyong Lin | 6266589 | 2018-04-16 11:07:44 -0700 | [diff] [blame] | 50 | buf[4 + i] = static_cast<int32_t>(mSupportedHdrTypes[i]); | 
| Dan Stoza | 7d7ae73 | 2016-03-16 12:23:40 -0700 | [diff] [blame] | 51 | } | 
| Mathias Agopian | e1f5e6f | 2017-02-06 16:34:41 -0800 | [diff] [blame] | 52 | return NO_ERROR; | 
| Dan Stoza | 7d7ae73 | 2016-03-16 12:23:40 -0700 | [diff] [blame] | 53 | } | 
|  | 54 |  | 
| Mathias Agopian | e1f5e6f | 2017-02-06 16:34:41 -0800 | [diff] [blame] | 55 | status_t HdrCapabilities::unflatten(void const* buffer, size_t size) { | 
|  | 56 |  | 
|  | 57 | size_t minSize = sizeof(mMaxLuminance) + | 
|  | 58 | sizeof(mMaxAverageLuminance) + | 
|  | 59 | sizeof(mMinLuminance) + | 
|  | 60 | sizeof(int32_t); | 
|  | 61 |  | 
|  | 62 | if (size < minSize) { | 
|  | 63 | return NO_MEMORY; | 
|  | 64 | } | 
|  | 65 |  | 
|  | 66 | int32_t const * const buf = static_cast<int32_t const *>(buffer); | 
|  | 67 | const size_t itemCount = size_t(buf[3]); | 
|  | 68 |  | 
|  | 69 | // check the buffer is large enough | 
|  | 70 | if (size < minSize + itemCount * sizeof(int32_t)) { | 
|  | 71 | return BAD_VALUE; | 
|  | 72 | } | 
|  | 73 |  | 
|  | 74 | mMaxLuminance        = reinterpret_cast<float const&>(buf[0]); | 
|  | 75 | mMaxAverageLuminance = reinterpret_cast<float const&>(buf[1]); | 
|  | 76 | mMinLuminance        = reinterpret_cast<float const&>(buf[2]); | 
|  | 77 | if (itemCount) { | 
| Jay Patel | 06a1389 | 2017-08-16 14:51:09 -0700 | [diff] [blame] | 78 | mSupportedHdrTypes.resize(itemCount); | 
| Mathias Agopian | e1f5e6f | 2017-02-06 16:34:41 -0800 | [diff] [blame] | 79 | for (size_t i = 0; i < itemCount; ++i) { | 
| Peiyong Lin | 6266589 | 2018-04-16 11:07:44 -0700 | [diff] [blame] | 80 | mSupportedHdrTypes[i] = static_cast<ui::Hdr>(buf[4 + i]); | 
| Mathias Agopian | e1f5e6f | 2017-02-06 16:34:41 -0800 | [diff] [blame] | 81 | } | 
|  | 82 | } | 
|  | 83 | return NO_ERROR; | 
|  | 84 | } | 
|  | 85 |  | 
|  | 86 | #if defined(__clang__) | 
|  | 87 | #pragma clang diagnostic pop | 
|  | 88 | #endif | 
|  | 89 |  | 
| Dan Stoza | 7d7ae73 | 2016-03-16 12:23:40 -0700 | [diff] [blame] | 90 | } // namespace android |