1 /*
2  * Copyright 2021 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/DynamicDisplayInfo.h>
18 
19 #include <cstdint>
20 
21 #include <ui/FlattenableHelpers.h>
22 
23 #define RETURN_IF_ERROR(op) \
24     if (const status_t status = (op); status != OK) return status;
25 
26 namespace android::ui {
27 
getActiveDisplayMode() const28 std::optional<ui::DisplayMode> DynamicDisplayInfo::getActiveDisplayMode() const {
29     for (const auto& currMode : supportedDisplayModes) {
30         if (currMode.id == activeDisplayModeId) {
31             return currMode;
32         }
33     }
34     return {};
35 }
36 
getFlattenedSize() const37 size_t DynamicDisplayInfo::getFlattenedSize() const {
38     return FlattenableHelpers::getFlattenedSize(supportedDisplayModes) +
39             FlattenableHelpers::getFlattenedSize(activeDisplayModeId) +
40             FlattenableHelpers::getFlattenedSize(supportedColorModes) +
41             FlattenableHelpers::getFlattenedSize(activeColorMode) +
42             FlattenableHelpers::getFlattenedSize(hdrCapabilities) +
43             FlattenableHelpers::getFlattenedSize(autoLowLatencyModeSupported) +
44             FlattenableHelpers::getFlattenedSize(gameContentTypeSupported);
45 }
46 
flatten(void * buffer,size_t size) const47 status_t DynamicDisplayInfo::flatten(void* buffer, size_t size) const {
48     if (size < getFlattenedSize()) {
49         return NO_MEMORY;
50     }
51     RETURN_IF_ERROR(FlattenableHelpers::flatten(&buffer, &size, supportedDisplayModes));
52     RETURN_IF_ERROR(FlattenableHelpers::flatten(&buffer, &size, activeDisplayModeId));
53     RETURN_IF_ERROR(FlattenableHelpers::flatten(&buffer, &size, supportedColorModes));
54     RETURN_IF_ERROR(FlattenableHelpers::flatten(&buffer, &size, activeColorMode));
55     RETURN_IF_ERROR(FlattenableHelpers::flatten(&buffer, &size, hdrCapabilities));
56     RETURN_IF_ERROR(FlattenableHelpers::flatten(&buffer, &size, autoLowLatencyModeSupported));
57     RETURN_IF_ERROR(FlattenableHelpers::flatten(&buffer, &size, gameContentTypeSupported));
58     return OK;
59 }
60 
unflatten(const void * buffer,size_t size)61 status_t DynamicDisplayInfo::unflatten(const void* buffer, size_t size) {
62     RETURN_IF_ERROR(FlattenableHelpers::unflatten(&buffer, &size, &supportedDisplayModes));
63     RETURN_IF_ERROR(FlattenableHelpers::unflatten(&buffer, &size, &activeDisplayModeId));
64     RETURN_IF_ERROR(FlattenableHelpers::unflatten(&buffer, &size, &supportedColorModes));
65     RETURN_IF_ERROR(FlattenableHelpers::unflatten(&buffer, &size, &activeColorMode));
66     RETURN_IF_ERROR(FlattenableHelpers::unflatten(&buffer, &size, &hdrCapabilities));
67     RETURN_IF_ERROR(FlattenableHelpers::unflatten(&buffer, &size, &autoLowLatencyModeSupported));
68     RETURN_IF_ERROR(FlattenableHelpers::unflatten(&buffer, &size, &gameContentTypeSupported));
69     return OK;
70 }
71 
72 } // namespace android::ui
73