blob: 39adc5e929928965a2ad0c0ffb1491674ab5ec0e [file] [log] [blame]
Dan Stoza7d7ae732016-03-16 12:23:40 -07001/*
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 Stoza7d7ae732016-03-16 12:23:40 -070019namespace android {
20
Mathias Agopiane1f5e6f2017-02-06 16:34:41 -080021#if defined(__clang__)
22#pragma clang diagnostic push
23#pragma clang diagnostic ignored "-Wundefined-reinterpret-cast"
24#endif
25
26HdrCapabilities::~HdrCapabilities() = default;
27HdrCapabilities::HdrCapabilities(HdrCapabilities&& other) = default;
28HdrCapabilities& HdrCapabilities::operator=(HdrCapabilities&& other) = default;
29
30
31size_t HdrCapabilities::getFlattenedSize() const {
32 return sizeof(mMaxLuminance) +
33 sizeof(mMaxAverageLuminance) +
34 sizeof(mMinLuminance) +
35 sizeof(int32_t) +
36 mSupportedHdrTypes.size() * sizeof(int32_t);
Dan Stoza7d7ae732016-03-16 12:23:40 -070037}
38
Mathias Agopiane1f5e6f2017-02-06 16:34:41 -080039status_t HdrCapabilities::flatten(void* buffer, size_t size) const {
40
41 if (size < getFlattenedSize()) {
42 return NO_MEMORY;
Dan Stoza7d7ae732016-03-16 12:23:40 -070043 }
Mathias Agopiane1f5e6f2017-02-06 16:34:41 -080044
45 int32_t* const buf = static_cast<int32_t*>(buffer);
46 reinterpret_cast<float&>(buf[0]) = mMaxLuminance;
47 reinterpret_cast<float&>(buf[1]) = mMaxAverageLuminance;
48 reinterpret_cast<float&>(buf[2]) = mMinLuminance;
49 buf[3] = static_cast<int32_t>(mSupportedHdrTypes.size());
50 for (size_t i = 0, c = mSupportedHdrTypes.size(); i < c; ++i) {
51 buf[4 + i] = mSupportedHdrTypes[i];
Dan Stoza7d7ae732016-03-16 12:23:40 -070052 }
Mathias Agopiane1f5e6f2017-02-06 16:34:41 -080053 return NO_ERROR;
Dan Stoza7d7ae732016-03-16 12:23:40 -070054}
55
Mathias Agopiane1f5e6f2017-02-06 16:34:41 -080056status_t HdrCapabilities::unflatten(void const* buffer, size_t size) {
57
58 size_t minSize = sizeof(mMaxLuminance) +
59 sizeof(mMaxAverageLuminance) +
60 sizeof(mMinLuminance) +
61 sizeof(int32_t);
62
63 if (size < minSize) {
64 return NO_MEMORY;
65 }
66
67 int32_t const * const buf = static_cast<int32_t const *>(buffer);
68 const size_t itemCount = size_t(buf[3]);
69
70 // check the buffer is large enough
71 if (size < minSize + itemCount * sizeof(int32_t)) {
72 return BAD_VALUE;
73 }
74
75 mMaxLuminance = reinterpret_cast<float const&>(buf[0]);
76 mMaxAverageLuminance = reinterpret_cast<float const&>(buf[1]);
77 mMinLuminance = reinterpret_cast<float const&>(buf[2]);
78 if (itemCount) {
79 mSupportedHdrTypes.reserve(itemCount);
80 for (size_t i = 0; i < itemCount; ++i) {
81 mSupportedHdrTypes[i] = buf[4 + i];
82 }
83 }
84 return NO_ERROR;
85}
86
87#if defined(__clang__)
88#pragma clang diagnostic pop
89#endif
90
Dan Stoza7d7ae732016-03-16 12:23:40 -070091} // namespace android