OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/ozone/platform/drm/common/drm_util.h" | 5 #include "ui/ozone/platform/drm/common/drm_util.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <stdlib.h> | 8 #include <stdlib.h> |
9 #include <sys/mman.h> | 9 #include <sys/mman.h> |
10 #include <xf86drmMode.h> | 10 #include <xf86drmMode.h> |
11 | 11 |
| 12 #include "ui/display/util/edid_parser.h" |
| 13 |
| 14 #if !defined(DRM_MODE_CONNECTOR_DSI) |
| 15 #define DRM_MODE_CONNECTOR_DSI 16 |
| 16 #endif |
| 17 |
12 namespace ui { | 18 namespace ui { |
13 | 19 |
14 namespace { | 20 namespace { |
15 | 21 |
16 bool IsCrtcInUse(uint32_t crtc, | 22 bool IsCrtcInUse(uint32_t crtc, |
17 const ScopedVector<HardwareDisplayControllerInfo>& displays) { | 23 const ScopedVector<HardwareDisplayControllerInfo>& displays) { |
18 for (size_t i = 0; i < displays.size(); ++i) { | 24 for (size_t i = 0; i < displays.size(); ++i) { |
19 if (crtc == displays[i]->crtc()->crtc_id) | 25 if (crtc == displays[i]->crtc()->crtc_id) |
20 return true; | 26 return true; |
21 } | 27 } |
(...skipping 24 matching lines...) Expand all Loading... |
46 IsCrtcInUse(resources->crtcs[j], displays)) | 52 IsCrtcInUse(resources->crtcs[j], displays)) |
47 continue; | 53 continue; |
48 | 54 |
49 return resources->crtcs[j]; | 55 return resources->crtcs[j]; |
50 } | 56 } |
51 } | 57 } |
52 | 58 |
53 return 0; | 59 return 0; |
54 } | 60 } |
55 | 61 |
| 62 // Computes the refresh rate for the specific mode. If we have enough |
| 63 // information use the mode timings to compute a more exact value otherwise |
| 64 // fallback to using the mode's vertical refresh rate (the kernel computes this |
| 65 // the same way, however there is a loss in precision since |vrefresh| is sent |
| 66 // as an integer). |
| 67 float GetRefreshRate(const drmModeModeInfo& mode) { |
| 68 if (!mode.htotal || !mode.vtotal) |
| 69 return mode.vrefresh; |
| 70 |
| 71 float clock = mode.clock; |
| 72 float htotal = mode.htotal; |
| 73 float vtotal = mode.vtotal; |
| 74 |
| 75 return (clock * 1000.0f) / (htotal * vtotal); |
| 76 } |
| 77 |
| 78 DisplayConnectionType GetDisplayType(drmModeConnector* connector) { |
| 79 switch (connector->connector_type) { |
| 80 case DRM_MODE_CONNECTOR_VGA: |
| 81 return DISPLAY_CONNECTION_TYPE_VGA; |
| 82 case DRM_MODE_CONNECTOR_DVII: |
| 83 case DRM_MODE_CONNECTOR_DVID: |
| 84 case DRM_MODE_CONNECTOR_DVIA: |
| 85 return DISPLAY_CONNECTION_TYPE_DVI; |
| 86 case DRM_MODE_CONNECTOR_LVDS: |
| 87 case DRM_MODE_CONNECTOR_eDP: |
| 88 case DRM_MODE_CONNECTOR_DSI: |
| 89 return DISPLAY_CONNECTION_TYPE_INTERNAL; |
| 90 case DRM_MODE_CONNECTOR_DisplayPort: |
| 91 return DISPLAY_CONNECTION_TYPE_DISPLAYPORT; |
| 92 case DRM_MODE_CONNECTOR_HDMIA: |
| 93 case DRM_MODE_CONNECTOR_HDMIB: |
| 94 return DISPLAY_CONNECTION_TYPE_HDMI; |
| 95 default: |
| 96 return DISPLAY_CONNECTION_TYPE_UNKNOWN; |
| 97 } |
| 98 } |
| 99 |
| 100 int GetDrmProperty(int fd, |
| 101 drmModeConnector* connector, |
| 102 const std::string& name, |
| 103 ScopedDrmPropertyPtr* property) { |
| 104 for (int i = 0; i < connector->count_props; ++i) { |
| 105 ScopedDrmPropertyPtr tmp(drmModeGetProperty(fd, connector->props[i])); |
| 106 if (!tmp) |
| 107 continue; |
| 108 |
| 109 if (name == tmp->name) { |
| 110 *property = tmp.Pass(); |
| 111 return i; |
| 112 } |
| 113 } |
| 114 |
| 115 return -1; |
| 116 } |
| 117 |
| 118 std::string GetNameForEnumValue(drmModePropertyRes* property, uint32_t value) { |
| 119 for (int i = 0; i < property->count_enums; ++i) |
| 120 if (property->enums[i].value == value) |
| 121 return property->enums[i].name; |
| 122 |
| 123 return std::string(); |
| 124 } |
| 125 |
| 126 ScopedDrmPropertyBlobPtr GetDrmPropertyBlob(int fd, |
| 127 drmModeConnector* connector, |
| 128 const std::string& name) { |
| 129 ScopedDrmPropertyPtr property; |
| 130 int index = GetDrmProperty(fd, connector, name, &property); |
| 131 if (index < 0) |
| 132 return nullptr; |
| 133 |
| 134 if (property->flags & DRM_MODE_PROP_BLOB) { |
| 135 return ScopedDrmPropertyBlobPtr( |
| 136 drmModeGetPropertyBlob(fd, connector->prop_values[index])); |
| 137 } |
| 138 |
| 139 return nullptr; |
| 140 } |
| 141 |
| 142 bool IsAspectPreserving(int fd, drmModeConnector* connector) { |
| 143 ScopedDrmPropertyPtr property; |
| 144 int index = GetDrmProperty(fd, connector, "scaling mode", &property); |
| 145 if (index < 0) |
| 146 return false; |
| 147 |
| 148 return (GetNameForEnumValue(property.get(), connector->prop_values[index]) == |
| 149 "Full aspect"); |
| 150 } |
| 151 |
56 } // namespace | 152 } // namespace |
57 | 153 |
58 HardwareDisplayControllerInfo::HardwareDisplayControllerInfo( | 154 HardwareDisplayControllerInfo::HardwareDisplayControllerInfo( |
59 ScopedDrmConnectorPtr connector, | 155 ScopedDrmConnectorPtr connector, |
60 ScopedDrmCrtcPtr crtc) | 156 ScopedDrmCrtcPtr crtc) |
61 : connector_(connector.Pass()), crtc_(crtc.Pass()) { | 157 : connector_(connector.Pass()), crtc_(crtc.Pass()) { |
62 } | 158 } |
63 | 159 |
64 HardwareDisplayControllerInfo::~HardwareDisplayControllerInfo() { | 160 HardwareDisplayControllerInfo::~HardwareDisplayControllerInfo() { |
65 } | 161 } |
(...skipping 27 matching lines...) Expand all Loading... |
93 bool SameMode(const drmModeModeInfo& lhs, const drmModeModeInfo& rhs) { | 189 bool SameMode(const drmModeModeInfo& lhs, const drmModeModeInfo& rhs) { |
94 return lhs.clock == rhs.clock && lhs.hdisplay == rhs.hdisplay && | 190 return lhs.clock == rhs.clock && lhs.hdisplay == rhs.hdisplay && |
95 lhs.vdisplay == rhs.vdisplay && lhs.vrefresh == rhs.vrefresh && | 191 lhs.vdisplay == rhs.vdisplay && lhs.vrefresh == rhs.vrefresh && |
96 lhs.hsync_start == rhs.hsync_start && lhs.hsync_end == rhs.hsync_end && | 192 lhs.hsync_start == rhs.hsync_start && lhs.hsync_end == rhs.hsync_end && |
97 lhs.htotal == rhs.htotal && lhs.hskew == rhs.hskew && | 193 lhs.htotal == rhs.htotal && lhs.hskew == rhs.hskew && |
98 lhs.vsync_start == rhs.vsync_start && lhs.vsync_end == rhs.vsync_end && | 194 lhs.vsync_start == rhs.vsync_start && lhs.vsync_end == rhs.vsync_end && |
99 lhs.vtotal == rhs.vtotal && lhs.vscan == rhs.vscan && | 195 lhs.vtotal == rhs.vtotal && lhs.vscan == rhs.vscan && |
100 lhs.flags == rhs.flags && strcmp(lhs.name, rhs.name) == 0; | 196 lhs.flags == rhs.flags && strcmp(lhs.name, rhs.name) == 0; |
101 } | 197 } |
102 | 198 |
| 199 DisplayMode_Params CreateDisplayModeParams(const drmModeModeInfo& mode) { |
| 200 DisplayMode_Params params; |
| 201 params.size = gfx::Size(mode.hdisplay, mode.vdisplay); |
| 202 params.is_interlaced = mode.flags & DRM_MODE_FLAG_INTERLACE; |
| 203 params.refresh_rate = GetRefreshRate(mode); |
| 204 |
| 205 return params; |
| 206 } |
| 207 |
| 208 DisplaySnapshot_Params CreateDisplaySnapshotParams( |
| 209 HardwareDisplayControllerInfo* info, |
| 210 int fd, |
| 211 size_t display_index) { |
| 212 DisplaySnapshot_Params params; |
| 213 params.display_id = display_index; |
| 214 params.physical_size = |
| 215 gfx::Size(info->connector()->mmWidth, info->connector()->mmHeight); |
| 216 params.type = GetDisplayType(info->connector()); |
| 217 params.is_aspect_preserving_scaling = |
| 218 IsAspectPreserving(fd, info->connector()); |
| 219 |
| 220 ScopedDrmPropertyBlobPtr edid_blob( |
| 221 GetDrmPropertyBlob(fd, info->connector(), "EDID")); |
| 222 |
| 223 if (edid_blob) { |
| 224 std::vector<uint8_t> edid( |
| 225 static_cast<uint8_t*>(edid_blob->data), |
| 226 static_cast<uint8_t*>(edid_blob->data) + edid_blob->length); |
| 227 |
| 228 if (!GetDisplayIdFromEDID(edid, display_index, ¶ms.display_id)) |
| 229 params.display_id = display_index; |
| 230 |
| 231 ParseOutputDeviceData(edid, nullptr, ¶ms.display_name, nullptr, |
| 232 nullptr); |
| 233 ParseOutputOverscanFlag(edid, ¶ms.has_overscan); |
| 234 } else { |
| 235 VLOG(1) << "Failed to get EDID blob for connector " |
| 236 << info->connector()->connector_id; |
| 237 } |
| 238 |
| 239 for (int i = 0; i < info->connector()->count_modes; ++i) { |
| 240 const drmModeModeInfo& mode = info->connector()->modes[i]; |
| 241 params.modes.push_back(CreateDisplayModeParams(mode)); |
| 242 |
| 243 if (info->crtc()->mode_valid && SameMode(info->crtc()->mode, mode)) { |
| 244 params.has_current_mode = true; |
| 245 params.current_mode = params.modes.back(); |
| 246 } |
| 247 |
| 248 if (mode.type & DRM_MODE_TYPE_PREFERRED) { |
| 249 params.has_native_mode = true; |
| 250 params.native_mode = params.modes.back(); |
| 251 } |
| 252 } |
| 253 |
| 254 // If no preferred mode is found then use the first one. Using the first one |
| 255 // since it should be the best mode. |
| 256 if (!params.has_native_mode && !params.modes.empty()) { |
| 257 params.has_native_mode = true; |
| 258 params.native_mode = params.modes.front(); |
| 259 } |
| 260 |
| 261 return params; |
| 262 } |
| 263 |
103 } // namespace ui | 264 } // namespace ui |
OLD | NEW |