1 /*
2  * Copyright 2022 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 <algorithm>
18 #include <functional>
19 #include <utility>
20 
21 #include <ftl/algorithm.h>
22 #include <ftl/enum.h>
23 #include <ui/DebugUtils.h>
24 
25 #include "DisplaySnapshot.h"
26 
27 namespace android::display {
28 
DisplaySnapshot(PhysicalDisplayId displayId,ui::DisplayConnectionType connectionType,DisplayModes && displayModes,ui::ColorModes && colorModes,std::optional<DeviceProductInfo> && deviceProductInfo)29 DisplaySnapshot::DisplaySnapshot(PhysicalDisplayId displayId,
30                                  ui::DisplayConnectionType connectionType,
31                                  DisplayModes&& displayModes, ui::ColorModes&& colorModes,
32                                  std::optional<DeviceProductInfo>&& deviceProductInfo)
33       : mDisplayId(displayId),
34         mConnectionType(connectionType),
35         mDisplayModes(std::move(displayModes)),
36         mColorModes(std::move(colorModes)),
37         mDeviceProductInfo(std::move(deviceProductInfo)) {}
38 
translateModeId(hal::HWConfigId hwcId) const39 std::optional<DisplayModeId> DisplaySnapshot::translateModeId(hal::HWConfigId hwcId) const {
40     return ftl::find_if(mDisplayModes,
41                         [hwcId](const DisplayModes::value_type& pair) {
42                             return pair.second->getHwcId() == hwcId;
43                         })
44             .transform(&ftl::to_key<DisplayModes>);
45 }
46 
filterColorModes(bool supportsWideColor) const47 ui::ColorModes DisplaySnapshot::filterColorModes(bool supportsWideColor) const {
48     ui::ColorModes modes = mColorModes;
49 
50     // If the display is internal and the configuration claims it's not wide color capable, filter
51     // out all wide color modes. The typical reason why this happens is that the hardware is not
52     // good enough to support GPU composition of wide color, and thus the OEMs choose to disable
53     // this capability.
54     if (mConnectionType == ui::DisplayConnectionType::Internal && !supportsWideColor) {
55         const auto it = std::remove_if(modes.begin(), modes.end(), ui::isWideColorMode);
56         modes.erase(it, modes.end());
57     }
58 
59     return modes;
60 }
61 
dump(utils::Dumper & dumper) const62 void DisplaySnapshot::dump(utils::Dumper& dumper) const {
63     using namespace std::string_view_literals;
64 
65     dumper.dump("connectionType"sv, ftl::enum_string(mConnectionType));
66 
67     dumper.dump("colorModes"sv);
68     {
69         utils::Dumper::Indent indent(dumper);
70         for (const auto mode : mColorModes) {
71             dumper.dump({}, decodeColorMode(mode));
72         }
73     }
74 
75     dumper.dump("deviceProductInfo"sv, mDeviceProductInfo);
76 }
77 
78 } // namespace android::display
79