• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         .output_type = 1,  // OutputType::SYSTEM
151     };
152 
153     /* Chwck if the mode is preferred */
154     if ((mode.GetRawMode().type & DRM_MODE_TYPE_PREFERRED) != 0 &&
155         preferred_config_id == 0) {
156       preferred_config_id = last_config_id;
157       preferred_config_group_id = group_found;
158     }
159 
160     last_config_id++;
161   }
162 
163   /* We must have preferred mode. Set first mode as preferred
164    * in case KMS haven't reported anything. */
165   if (preferred_config_id == 0) {
166     preferred_config_id = first_config_id;
167     preferred_config_group_id = 1;
168   }
169 
170   for (uint32_t group = 1; group < last_group_id; group++) {
171     bool has_interlaced = false;
172     bool has_progressive = false;
173     for (auto &hwc_config : hwc_configs) {
174       if (hwc_config.second.group_id != group || hwc_config.second.disabled) {
175         continue;
176       }
177 
178       if (hwc_config.second.IsInterlaced()) {
179         has_interlaced = true;
180       } else {
181         has_progressive = true;
182       }
183     }
184 
185     auto has_both = has_interlaced && has_progressive;
186     if (!has_both) {
187       continue;
188     }
189 
190     bool group_contains_preferred_interlaced = false;
191     if (group == preferred_config_group_id &&
192         hwc_configs[preferred_config_id].IsInterlaced()) {
193       group_contains_preferred_interlaced = true;
194     }
195 
196     for (auto &hwc_config : hwc_configs) {
197       if (hwc_config.second.group_id != group || hwc_config.second.disabled) {
198         continue;
199       }
200 
201       auto disable = group_contains_preferred_interlaced
202                          ? !hwc_config.second.IsInterlaced()
203                          : hwc_config.second.IsInterlaced();
204 
205       if (disable) {
206         ALOGI(
207             "Group %i: Disabling display mode %s (This group should consist "
208             "of %s modes)",
209             group, hwc_config.second.mode.GetName().c_str(),
210             group_contains_preferred_interlaced ? "interlaced" : "progressive");
211 
212         hwc_config.second.disabled = true;
213       }
214     }
215   }
216 
217   /* Group should not contain 2 modes with FPS delta less than ~1HZ
218    * otherwise android.graphics.cts.SetFrameRateTest CTS will fail
219    */
220   constexpr float kMinFpsDelta = 1.0;  // FPS
221   for (uint32_t m1 = first_config_id; m1 < last_config_id; m1++) {
222     for (uint32_t m2 = first_config_id; m2 < last_config_id; m2++) {
223       if (m1 != m2 && hwc_configs[m1].group_id == hwc_configs[m2].group_id &&
224           !hwc_configs[m1].disabled && !hwc_configs[m2].disabled &&
225           fabsf(hwc_configs[m1].mode.GetVRefresh() -
226                 hwc_configs[m2].mode.GetVRefresh()) < kMinFpsDelta) {
227         ALOGI(
228             "Group %i: Disabling display mode %s (Refresh rate value is "
229             "too close to existing mode %s)",
230             hwc_configs[m2].group_id, hwc_configs[m2].mode.GetName().c_str(),
231             hwc_configs[m1].mode.GetName().c_str());
232 
233         hwc_configs[m2].disabled = true;
234       }
235     }
236   }
237 
238   return HWC2::Error::None;
239 }
240 
241 }  // namespace android
242