blob: aec2fac78070ca9aa34464f2d509662651d17c2f [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
Mathias Agopiane1f5e6f2017-02-06 16:34:41 -080026size_t HdrCapabilities::getFlattenedSize() const {
27 return sizeof(mMaxLuminance) +
28 sizeof(mMaxAverageLuminance) +
29 sizeof(mMinLuminance) +
30 sizeof(int32_t) +
Peiyong Lin62665892018-04-16 11:07:44 -070031 mSupportedHdrTypes.size() * sizeof(ui::Hdr);
Dan Stoza7d7ae732016-03-16 12:23:40 -070032}
33
Mathias Agopiane1f5e6f2017-02-06 16:34:41 -080034status_t HdrCapabilities::flatten(void* buffer, size_t size) const {
35
36 if (size < getFlattenedSize()) {
37 return NO_MEMORY;
Dan Stoza7d7ae732016-03-16 12:23:40 -070038 }
Mathias Agopiane1f5e6f2017-02-06 16:34:41 -080039
40 int32_t* const buf = static_cast<int32_t*>(buffer);
41 reinterpret_cast<float&>(buf[0]) = mMaxLuminance;
42 reinterpret_cast<float&>(buf[1]) = mMaxAverageLuminance;
43 reinterpret_cast<float&>(buf[2]) = mMinLuminance;
44 buf[3] = static_cast<int32_t>(mSupportedHdrTypes.size());
45 for (size_t i = 0, c = mSupportedHdrTypes.size(); i < c; ++i) {
Peiyong Lin62665892018-04-16 11:07:44 -070046 buf[4 + i] = static_cast<int32_t>(mSupportedHdrTypes[i]);
Dan Stoza7d7ae732016-03-16 12:23:40 -070047 }
Mathias Agopiane1f5e6f2017-02-06 16:34:41 -080048 return NO_ERROR;
Dan Stoza7d7ae732016-03-16 12:23:40 -070049}
50
Mathias Agopiane1f5e6f2017-02-06 16:34:41 -080051status_t HdrCapabilities::unflatten(void const* buffer, size_t size) {
52
53 size_t minSize = sizeof(mMaxLuminance) +
54 sizeof(mMaxAverageLuminance) +
55 sizeof(mMinLuminance) +
56 sizeof(int32_t);
57
58 if (size < minSize) {
59 return NO_MEMORY;
60 }
61
62 int32_t const * const buf = static_cast<int32_t const *>(buffer);
63 const size_t itemCount = size_t(buf[3]);
64
65 // check the buffer is large enough
66 if (size < minSize + itemCount * sizeof(int32_t)) {
67 return BAD_VALUE;
68 }
69
70 mMaxLuminance = reinterpret_cast<float const&>(buf[0]);
71 mMaxAverageLuminance = reinterpret_cast<float const&>(buf[1]);
72 mMinLuminance = reinterpret_cast<float const&>(buf[2]);
73 if (itemCount) {
Jay Patel06a13892017-08-16 14:51:09 -070074 mSupportedHdrTypes.resize(itemCount);
Mathias Agopiane1f5e6f2017-02-06 16:34:41 -080075 for (size_t i = 0; i < itemCount; ++i) {
Peiyong Lin62665892018-04-16 11:07:44 -070076 mSupportedHdrTypes[i] = static_cast<ui::Hdr>(buf[4 + i]);
Mathias Agopiane1f5e6f2017-02-06 16:34:41 -080077 }
78 }
79 return NO_ERROR;
80}
81
82#if defined(__clang__)
83#pragma clang diagnostic pop
84#endif
85
Dan Stoza7d7ae732016-03-16 12:23:40 -070086} // namespace android