xref: /aosp_15_r20/external/drm_hwcomposer/hwc2_device/HwcDisplayConfigs.cpp (revision 0a9764fe0a15e71ebbeb85e87e10990c23aab47f)
1 /*
2  * Copyright (C) 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 #define LOG_TAG "drmhwc"
18 
19 #include "HwcDisplayConfigs.h"
20 
21 #include <cmath>
22 #include <cstring>
23 
24 #include "drm/DrmConnector.h"
25 #include "utils/log.h"
26 #include "utils/properties.h"
27 
28 constexpr uint32_t kHeadlessModeDisplayWidthMm = 163;
29 constexpr uint32_t kHeadlessModeDisplayHeightMm = 122;
30 constexpr uint32_t kHeadlessModeDisplayWidthPx = 1024;
31 constexpr uint32_t kHeadlessModeDisplayHeightPx = 768;
32 constexpr uint32_t kHeadlessModeDisplayVRefresh = 60;
33 constexpr uint32_t kSyncLen = 10;
34 constexpr uint32_t kBackPorch = 10;
35 constexpr uint32_t kFrontPorch = 10;
36 constexpr uint32_t kHzInKHz = 1000;
37 
38 namespace android {
39 
40 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
41 uint32_t HwcDisplayConfigs::last_config_id = 1;
42 
GenFakeMode(uint16_t width,uint16_t height)43 void HwcDisplayConfigs::GenFakeMode(uint16_t width, uint16_t height) {
44   hwc_configs.clear();
45 
46   last_config_id++;
47   preferred_config_id = active_config_id = last_config_id;
48   auto headless_drm_mode_info = (drmModeModeInfo){
49       .hdisplay = width,
50       .vdisplay = height,
51       .vrefresh = kHeadlessModeDisplayVRefresh,
52       .name = "VIRTUAL-MODE",
53   };
54 
55   if (width == 0 || height == 0) {
56     strcpy(headless_drm_mode_info.name, "HEADLESS-MODE");
57     headless_drm_mode_info.hdisplay = kHeadlessModeDisplayWidthPx;
58     headless_drm_mode_info.vdisplay = kHeadlessModeDisplayHeightPx;
59   }
60 
61   /* We need a valid mode to pass the kernel validation */
62 
63   headless_drm_mode_info.hsync_start = headless_drm_mode_info.hdisplay +
64                                        kFrontPorch;
65   headless_drm_mode_info.hsync_end = headless_drm_mode_info.hsync_start +
66                                      kSyncLen;
67   headless_drm_mode_info.htotal = headless_drm_mode_info.hsync_end + kBackPorch;
68 
69   headless_drm_mode_info.vsync_start = headless_drm_mode_info.vdisplay +
70                                        kFrontPorch;
71   headless_drm_mode_info.vsync_end = headless_drm_mode_info.vsync_start +
72                                      kSyncLen;
73   headless_drm_mode_info.vtotal = headless_drm_mode_info.vsync_end + kBackPorch;
74 
75   headless_drm_mode_info.clock = (headless_drm_mode_info.htotal *
76                                   headless_drm_mode_info.vtotal *
77                                   headless_drm_mode_info.vrefresh) /
78                                  kHzInKHz;
79 
80   hwc_configs[active_config_id] = (HwcDisplayConfig){
81       .id = active_config_id,
82       .group_id = 1,
83       .mode = DrmMode(&headless_drm_mode_info),
84   };
85 
86   mm_width = kHeadlessModeDisplayWidthMm;
87   mm_height = kHeadlessModeDisplayHeightMm;
88 }
89 
90 // NOLINTNEXTLINE (readability-function-cognitive-complexity): Fixme
Update(DrmConnector & connector)91 HWC2::Error HwcDisplayConfigs::Update(DrmConnector &connector) {
92   /* In case UpdateModes will fail we will still have one mode for headless
93    * mode
94    */
95   GenFakeMode(0, 0);
96   /* Read real configs */
97   auto ret = connector.UpdateModes();
98   if (ret != 0) {
99     ALOGE("Failed to update display modes %d", ret);
100     return HWC2::Error::BadDisplay;
101   }
102 
103   if (connector.GetModes().empty()) {
104     ALOGE("No modes reported by KMS");
105     return HWC2::Error::BadDisplay;
106   }
107 
108   hwc_configs.clear();
109   mm_width = connector.GetMmWidth();
110   mm_height = connector.GetMmHeight();
111 
112   preferred_config_id = 0;
113   uint32_t preferred_config_group_id = 0;
114 
115   auto first_config_id = last_config_id;
116   uint32_t last_group_id = 1;
117   const bool use_config_groups = Properties::UseConfigGroups();
118 
119   /* Group modes */
120   for (const auto &mode : connector.GetModes()) {
121     /* Find group for the new mode or create new group */
122     uint32_t group_found = 0;
123     if (use_config_groups) {
124       for (auto &hwc_config : hwc_configs) {
125         if (mode.GetRawMode().hdisplay ==
126                 hwc_config.second.mode.GetRawMode().hdisplay &&
127             mode.GetRawMode().vdisplay ==
128                 hwc_config.second.mode.GetRawMode().vdisplay) {
129           group_found = hwc_config.second.group_id;
130         }
131       }
132     }
133     if (group_found == 0) {
134       group_found = last_group_id++;
135     }
136 
137     bool disabled = false;
138     if ((mode.GetRawMode().flags & DRM_MODE_FLAG_3D_MASK) != 0) {
139       ALOGI("Disabling display mode %s (Modes with 3D flag aren't supported)",
140             mode.GetName().c_str());
141       disabled = true;
142     }
143 
144     /* Add config */
145     hwc_configs[last_config_id] = {
146         .id = last_config_id,
147         .group_id = group_found,
148         .mode = mode,
149         .disabled = disabled,
150     };
151 
152     /* Chwck if the mode is preferred */
153     if ((mode.GetRawMode().type & DRM_MODE_TYPE_PREFERRED) != 0 &&
154         preferred_config_id == 0) {
155       preferred_config_id = last_config_id;
156       preferred_config_group_id = group_found;
157     }
158 
159     last_config_id++;
160   }
161 
162   /* We must have preferred mode. Set first mode as preferred
163    * in case KMS haven't reported anything. */
164   if (preferred_config_id == 0) {
165     preferred_config_id = first_config_id;
166     preferred_config_group_id = 1;
167   }
168 
169   for (uint32_t group = 1; group < last_group_id; group++) {
170     bool has_interlaced = false;
171     bool has_progressive = false;
172     for (auto &hwc_config : hwc_configs) {
173       if (hwc_config.second.group_id != group || hwc_config.second.disabled) {
174         continue;
175       }
176 
177       if (hwc_config.second.IsInterlaced()) {
178         has_interlaced = true;
179       } else {
180         has_progressive = true;
181       }
182     }
183 
184     auto has_both = has_interlaced && has_progressive;
185     if (!has_both) {
186       continue;
187     }
188 
189     bool group_contains_preferred_interlaced = false;
190     if (group == preferred_config_group_id &&
191         hwc_configs[preferred_config_id].IsInterlaced()) {
192       group_contains_preferred_interlaced = true;
193     }
194 
195     for (auto &hwc_config : hwc_configs) {
196       if (hwc_config.second.group_id != group || hwc_config.second.disabled) {
197         continue;
198       }
199 
200       auto disable = group_contains_preferred_interlaced
201                          ? !hwc_config.second.IsInterlaced()
202                          : hwc_config.second.IsInterlaced();
203 
204       if (disable) {
205         ALOGI(
206             "Group %i: Disabling display mode %s (This group should consist "
207             "of %s modes)",
208             group, hwc_config.second.mode.GetName().c_str(),
209             group_contains_preferred_interlaced ? "interlaced" : "progressive");
210 
211         hwc_config.second.disabled = true;
212       }
213     }
214   }
215 
216   /* Group should not contain 2 modes with FPS delta less than ~1HZ
217    * otherwise android.graphics.cts.SetFrameRateTest CTS will fail
218    */
219   constexpr float kMinFpsDelta = 1.0;  // FPS
220   for (uint32_t m1 = first_config_id; m1 < last_config_id; m1++) {
221     for (uint32_t m2 = first_config_id; m2 < last_config_id; m2++) {
222       if (m1 != m2 && hwc_configs[m1].group_id == hwc_configs[m2].group_id &&
223           !hwc_configs[m1].disabled && !hwc_configs[m2].disabled &&
224           fabsf(hwc_configs[m1].mode.GetVRefresh() -
225                 hwc_configs[m2].mode.GetVRefresh()) < kMinFpsDelta) {
226         ALOGI(
227             "Group %i: Disabling display mode %s (Refresh rate value is "
228             "too close to existing mode %s)",
229             hwc_configs[m2].group_id, hwc_configs[m2].mode.GetName().c_str(),
230             hwc_configs[m1].mode.GetName().c_str());
231 
232         hwc_configs[m2].disabled = true;
233       }
234     }
235   }
236 
237   return HWC2::Error::None;
238 }
239 
240 }  // namespace android
241