| 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 <drm_fourcc.h> | 7 #include <drm_fourcc.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <stdlib.h> | 9 #include <stdlib.h> |
| 10 #include <sys/mman.h> | 10 #include <sys/mman.h> |
| 11 #include <xf86drm.h> | 11 #include <xf86drm.h> |
| 12 #include <xf86drmMode.h> | 12 #include <xf86drmMode.h> |
| 13 #include <algorithm> | |
| 14 #include <utility> | 13 #include <utility> |
| 15 | 14 |
| 16 #include "base/containers/small_map.h" | |
| 17 #include "ui/display/util/edid_parser.h" | 15 #include "ui/display/util/edid_parser.h" |
| 18 | 16 |
| 19 #if !defined(DRM_MODE_CONNECTOR_DSI) | 17 #if !defined(DRM_MODE_CONNECTOR_DSI) |
| 20 #define DRM_MODE_CONNECTOR_DSI 16 | 18 #define DRM_MODE_CONNECTOR_DSI 16 |
| 21 #endif | 19 #endif |
| 22 | 20 |
| 23 #if !defined(DRM_CAP_CURSOR_WIDTH) | 21 #if !defined(DRM_CAP_CURSOR_WIDTH) |
| 24 #define DRM_CAP_CURSOR_WIDTH 0x8 | 22 #define DRM_CAP_CURSOR_WIDTH 0x8 |
| 25 #endif | 23 #endif |
| 26 | 24 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 51 bool IsCrtcInUse(uint32_t crtc, | 49 bool IsCrtcInUse(uint32_t crtc, |
| 52 const ScopedVector<HardwareDisplayControllerInfo>& displays) { | 50 const ScopedVector<HardwareDisplayControllerInfo>& displays) { |
| 53 for (size_t i = 0; i < displays.size(); ++i) { | 51 for (size_t i = 0; i < displays.size(); ++i) { |
| 54 if (crtc == displays[i]->crtc()->crtc_id) | 52 if (crtc == displays[i]->crtc()->crtc_id) |
| 55 return true; | 53 return true; |
| 56 } | 54 } |
| 57 | 55 |
| 58 return false; | 56 return false; |
| 59 } | 57 } |
| 60 | 58 |
| 61 // Return a CRTC compatible with |connector| and not already used in |displays|. | |
| 62 // If there are multiple compatible CRTCs, the one that supports the majority of | |
| 63 // planes will be returned. | |
| 64 uint32_t GetCrtc(int fd, | 59 uint32_t GetCrtc(int fd, |
| 65 drmModeConnector* connector, | 60 drmModeConnector* connector, |
| 66 drmModeRes* resources, | 61 drmModeRes* resources, |
| 67 const ScopedVector<HardwareDisplayControllerInfo>& displays) { | 62 const ScopedVector<HardwareDisplayControllerInfo>& displays) { |
| 68 ScopedDrmPlaneResPtr plane_resources(drmModeGetPlaneResources(fd)); | 63 // If the connector already has an encoder try to re-use. |
| 69 std::vector<ScopedDrmPlanePtr> planes; | 64 if (connector->encoder_id) { |
| 70 for (uint32_t i = 0; i < plane_resources->count_planes; i++) | 65 ScopedDrmEncoderPtr encoder(drmModeGetEncoder(fd, connector->encoder_id)); |
| 71 planes.emplace_back(drmModeGetPlane(fd, plane_resources->planes[i])); | 66 if (encoder && encoder->crtc_id && !IsCrtcInUse(encoder->crtc_id, displays)) |
| 72 | 67 return encoder->crtc_id; |
| 73 DCHECK_GE(32, resources->count_crtcs); | 68 } |
| 74 uint32_t best_crtc = 0; | |
| 75 int best_crtc_planes = 0; | |
| 76 | 69 |
| 77 // Try to find an encoder for the connector. | 70 // Try to find an encoder for the connector. |
| 78 for (int i = 0; i < connector->count_encoders; ++i) { | 71 for (int i = 0; i < connector->count_encoders; ++i) { |
| 79 ScopedDrmEncoderPtr encoder(drmModeGetEncoder(fd, connector->encoders[i])); | 72 ScopedDrmEncoderPtr encoder(drmModeGetEncoder(fd, connector->encoders[i])); |
| 80 if (!encoder) | 73 if (!encoder) |
| 81 continue; | 74 continue; |
| 82 | 75 |
| 83 for (int j = 0; j < resources->count_crtcs; ++j) { | 76 for (int j = 0; j < resources->count_crtcs; ++j) { |
| 84 // Check if the encoder is compatible with this CRTC | 77 // Check if the encoder is compatible with this CRTC |
| 85 int crtc_bit = 1 << j; | 78 if (!(encoder->possible_crtcs & (1 << j)) || |
| 86 if (!(encoder->possible_crtcs & crtc_bit) || | |
| 87 IsCrtcInUse(resources->crtcs[j], displays)) | 79 IsCrtcInUse(resources->crtcs[j], displays)) |
| 88 continue; | 80 continue; |
| 89 | 81 |
| 90 int supported_planes = std::count_if( | 82 return resources->crtcs[j]; |
| 91 planes.begin(), planes.end(), [crtc_bit](const ScopedDrmPlanePtr& p) { | |
| 92 return p->possible_crtcs & crtc_bit; | |
| 93 }); | |
| 94 | |
| 95 if (supported_planes > best_crtc_planes) { | |
| 96 best_crtc_planes = supported_planes; | |
| 97 best_crtc = resources->crtcs[j]; | |
| 98 } | |
| 99 } | 83 } |
| 100 } | 84 } |
| 101 | 85 |
| 102 return best_crtc; | 86 return 0; |
| 103 } | 87 } |
| 104 | 88 |
| 105 // Computes the refresh rate for the specific mode. If we have enough | 89 // Computes the refresh rate for the specific mode. If we have enough |
| 106 // information use the mode timings to compute a more exact value otherwise | 90 // information use the mode timings to compute a more exact value otherwise |
| 107 // fallback to using the mode's vertical refresh rate (the kernel computes this | 91 // fallback to using the mode's vertical refresh rate (the kernel computes this |
| 108 // the same way, however there is a loss in precision since |vrefresh| is sent | 92 // the same way, however there is a loss in precision since |vrefresh| is sent |
| 109 // as an integer). | 93 // as an integer). |
| 110 float GetRefreshRate(const drmModeModeInfo& mode) { | 94 float GetRefreshRate(const drmModeModeInfo& mode) { |
| 111 if (!mode.htotal || !mode.vtotal) | 95 if (!mode.htotal || !mode.vtotal) |
| 112 return mode.vrefresh; | 96 return mode.vrefresh; |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 | 219 |
| 236 HardwareDisplayControllerInfo::~HardwareDisplayControllerInfo() { | 220 HardwareDisplayControllerInfo::~HardwareDisplayControllerInfo() { |
| 237 } | 221 } |
| 238 | 222 |
| 239 ScopedVector<HardwareDisplayControllerInfo> GetAvailableDisplayControllerInfos( | 223 ScopedVector<HardwareDisplayControllerInfo> GetAvailableDisplayControllerInfos( |
| 240 int fd) { | 224 int fd) { |
| 241 ScopedDrmResourcesPtr resources(drmModeGetResources(fd)); | 225 ScopedDrmResourcesPtr resources(drmModeGetResources(fd)); |
| 242 DCHECK(resources) << "Failed to get DRM resources"; | 226 DCHECK(resources) << "Failed to get DRM resources"; |
| 243 ScopedVector<HardwareDisplayControllerInfo> displays; | 227 ScopedVector<HardwareDisplayControllerInfo> displays; |
| 244 | 228 |
| 245 std::vector<ScopedDrmConnectorPtr> available_connectors; | |
| 246 std::vector<ScopedDrmConnectorPtr::element_type*> connectors; | |
| 247 for (int i = 0; i < resources->count_connectors; ++i) { | 229 for (int i = 0; i < resources->count_connectors; ++i) { |
| 248 ScopedDrmConnectorPtr connector( | 230 ScopedDrmConnectorPtr connector( |
| 249 drmModeGetConnector(fd, resources->connectors[i])); | 231 drmModeGetConnector(fd, resources->connectors[i])); |
| 250 connectors.push_back(connector.get()); | |
| 251 | 232 |
| 252 if (connector && connector->connection == DRM_MODE_CONNECTED && | 233 if (!connector || connector->connection != DRM_MODE_CONNECTED || |
| 253 connector->count_modes != 0) | 234 connector->count_modes == 0) |
| 254 available_connectors.push_back(std::move(connector)); | 235 continue; |
| 255 } | |
| 256 | 236 |
| 257 base::SmallMap<std::map<ScopedDrmConnectorPtr::element_type*, int>> | 237 uint32_t crtc_id = GetCrtc(fd, connector.get(), resources.get(), displays); |
| 258 connector_crtcs; | |
| 259 for (auto& c : available_connectors) { | |
| 260 uint32_t possible_crtcs = 0; | |
| 261 for (int i = 0; i < c->count_encoders; ++i) { | |
| 262 ScopedDrmEncoderPtr encoder(drmModeGetEncoder(fd, c->encoders[i])); | |
| 263 if (!encoder) | |
| 264 continue; | |
| 265 possible_crtcs |= encoder->possible_crtcs; | |
| 266 } | |
| 267 connector_crtcs[c.get()] = possible_crtcs; | |
| 268 } | |
| 269 // Make sure to start assigning a crtc to the connector that supports the | |
| 270 // fewest crtcs first. | |
| 271 std::stable_sort(available_connectors.begin(), available_connectors.end(), | |
| 272 [&connector_crtcs](const ScopedDrmConnectorPtr& c1, | |
| 273 const ScopedDrmConnectorPtr& c2) { | |
| 274 // When c1 supports a proper subset of the crtcs of c2, we | |
| 275 // should process c1 first (return true). | |
| 276 int c1_crtcs = connector_crtcs[c1.get()]; | |
| 277 int c2_crtcs = connector_crtcs[c2.get()]; | |
| 278 return (c1_crtcs & c2_crtcs) == c1_crtcs && | |
| 279 c1_crtcs != c2_crtcs; | |
| 280 }); | |
| 281 | |
| 282 for (auto& c : available_connectors) { | |
| 283 uint32_t crtc_id = GetCrtc(fd, c.get(), resources.get(), displays); | |
| 284 if (!crtc_id) | 238 if (!crtc_id) |
| 285 continue; | 239 continue; |
| 286 | 240 |
| 287 ScopedDrmCrtcPtr crtc(drmModeGetCrtc(fd, crtc_id)); | 241 ScopedDrmCrtcPtr crtc(drmModeGetCrtc(fd, crtc_id)); |
| 288 size_t index = std::find(connectors.begin(), connectors.end(), c.get()) - | 242 displays.push_back(new HardwareDisplayControllerInfo(std::move(connector), |
| 289 connectors.begin(); | 243 std::move(crtc), i)); |
| 290 DCHECK_LT(index, connectors.size()); | |
| 291 displays.push_back(new HardwareDisplayControllerInfo( | |
| 292 std::move(c), std::move(crtc), index)); | |
| 293 } | 244 } |
| 294 | 245 |
| 295 return displays; | 246 return displays; |
| 296 } | 247 } |
| 297 | 248 |
| 298 bool SameMode(const drmModeModeInfo& lhs, const drmModeModeInfo& rhs) { | 249 bool SameMode(const drmModeModeInfo& lhs, const drmModeModeInfo& rhs) { |
| 299 return lhs.clock == rhs.clock && lhs.hdisplay == rhs.hdisplay && | 250 return lhs.clock == rhs.clock && lhs.hdisplay == rhs.hdisplay && |
| 300 lhs.vdisplay == rhs.vdisplay && lhs.vrefresh == rhs.vrefresh && | 251 lhs.vdisplay == rhs.vdisplay && lhs.vrefresh == rhs.vrefresh && |
| 301 lhs.hsync_start == rhs.hsync_start && lhs.hsync_end == rhs.hsync_end && | 252 lhs.hsync_start == rhs.hsync_start && lhs.hsync_end == rhs.hsync_end && |
| 302 lhs.htotal == rhs.htotal && lhs.hskew == rhs.hskew && | 253 lhs.htotal == rhs.htotal && lhs.hskew == rhs.hskew && |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 444 case gfx::BufferFormat::BGR_565: | 395 case gfx::BufferFormat::BGR_565: |
| 445 return DRM_FORMAT_RGB565; | 396 return DRM_FORMAT_RGB565; |
| 446 case gfx::BufferFormat::UYVY_422: | 397 case gfx::BufferFormat::UYVY_422: |
| 447 return DRM_FORMAT_UYVY; | 398 return DRM_FORMAT_UYVY; |
| 448 default: | 399 default: |
| 449 NOTREACHED(); | 400 NOTREACHED(); |
| 450 return 0; | 401 return 0; |
| 451 } | 402 } |
| 452 } | 403 } |
| 453 } // namespace ui | 404 } // namespace ui |
| OLD | NEW |