| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ui/ozone/platform/drm/gpu/drm_util.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 #include <stdlib.h> | |
| 9 #include <sys/mman.h> | |
| 10 #include <xf86drmMode.h> | |
| 11 | |
| 12 #include "ui/ozone/platform/drm/gpu/drm_device.h" | |
| 13 #include "ui/ozone/platform/drm/gpu/screen_manager.h" | |
| 14 | |
| 15 namespace ui { | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 bool IsCrtcInUse(uint32_t crtc, | |
| 20 const ScopedVector<HardwareDisplayControllerInfo>& displays) { | |
| 21 for (size_t i = 0; i < displays.size(); ++i) { | |
| 22 if (crtc == displays[i]->crtc()->crtc_id) | |
| 23 return true; | |
| 24 } | |
| 25 | |
| 26 return false; | |
| 27 } | |
| 28 | |
| 29 uint32_t GetCrtc(int fd, | |
| 30 drmModeConnector* connector, | |
| 31 drmModeRes* resources, | |
| 32 const ScopedVector<HardwareDisplayControllerInfo>& displays) { | |
| 33 // If the connector already has an encoder try to re-use. | |
| 34 if (connector->encoder_id) { | |
| 35 ScopedDrmEncoderPtr encoder(drmModeGetEncoder(fd, connector->encoder_id)); | |
| 36 if (encoder && encoder->crtc_id && !IsCrtcInUse(encoder->crtc_id, displays)) | |
| 37 return encoder->crtc_id; | |
| 38 } | |
| 39 | |
| 40 // Try to find an encoder for the connector. | |
| 41 for (int i = 0; i < connector->count_encoders; ++i) { | |
| 42 ScopedDrmEncoderPtr encoder(drmModeGetEncoder(fd, connector->encoders[i])); | |
| 43 if (!encoder) | |
| 44 continue; | |
| 45 | |
| 46 for (int j = 0; j < resources->count_crtcs; ++j) { | |
| 47 // Check if the encoder is compatible with this CRTC | |
| 48 if (!(encoder->possible_crtcs & (1 << j)) || | |
| 49 IsCrtcInUse(resources->crtcs[j], displays)) | |
| 50 continue; | |
| 51 | |
| 52 return resources->crtcs[j]; | |
| 53 } | |
| 54 } | |
| 55 | |
| 56 return 0; | |
| 57 } | |
| 58 | |
| 59 } // namespace | |
| 60 | |
| 61 HardwareDisplayControllerInfo::HardwareDisplayControllerInfo( | |
| 62 ScopedDrmConnectorPtr connector, | |
| 63 ScopedDrmCrtcPtr crtc) | |
| 64 : connector_(connector.Pass()), crtc_(crtc.Pass()) { | |
| 65 } | |
| 66 | |
| 67 HardwareDisplayControllerInfo::~HardwareDisplayControllerInfo() { | |
| 68 } | |
| 69 | |
| 70 ScopedVector<HardwareDisplayControllerInfo> GetAvailableDisplayControllerInfos( | |
| 71 int fd) { | |
| 72 ScopedDrmResourcesPtr resources(drmModeGetResources(fd)); | |
| 73 DCHECK(resources) << "Failed to get DRM resources"; | |
| 74 ScopedVector<HardwareDisplayControllerInfo> displays; | |
| 75 | |
| 76 for (int i = 0; i < resources->count_connectors; ++i) { | |
| 77 ScopedDrmConnectorPtr connector( | |
| 78 drmModeGetConnector(fd, resources->connectors[i])); | |
| 79 | |
| 80 if (!connector || connector->connection != DRM_MODE_CONNECTED || | |
| 81 connector->count_modes == 0) | |
| 82 continue; | |
| 83 | |
| 84 uint32_t crtc_id = GetCrtc(fd, connector.get(), resources.get(), displays); | |
| 85 if (!crtc_id) | |
| 86 continue; | |
| 87 | |
| 88 ScopedDrmCrtcPtr crtc(drmModeGetCrtc(fd, crtc_id)); | |
| 89 displays.push_back( | |
| 90 new HardwareDisplayControllerInfo(connector.Pass(), crtc.Pass())); | |
| 91 } | |
| 92 | |
| 93 return displays.Pass(); | |
| 94 } | |
| 95 | |
| 96 bool SameMode(const drmModeModeInfo& lhs, const drmModeModeInfo& rhs) { | |
| 97 return lhs.clock == rhs.clock && lhs.hdisplay == rhs.hdisplay && | |
| 98 lhs.vdisplay == rhs.vdisplay && lhs.vrefresh == rhs.vrefresh && | |
| 99 lhs.hsync_start == rhs.hsync_start && lhs.hsync_end == rhs.hsync_end && | |
| 100 lhs.htotal == rhs.htotal && lhs.hskew == rhs.hskew && | |
| 101 lhs.vsync_start == rhs.vsync_start && lhs.vsync_end == rhs.vsync_end && | |
| 102 lhs.vtotal == rhs.vtotal && lhs.vscan == rhs.vscan && | |
| 103 lhs.flags == rhs.flags && strcmp(lhs.name, rhs.name) == 0; | |
| 104 } | |
| 105 | |
| 106 void ForceInitializationOfPrimaryDisplay(const scoped_refptr<DrmDevice>& drm, | |
| 107 ScreenManager* screen_manager) { | |
| 108 VLOG(2) << "Forcing initialization of primary display."; | |
| 109 ScopedVector<HardwareDisplayControllerInfo> displays = | |
| 110 GetAvailableDisplayControllerInfos(drm->get_fd()); | |
| 111 | |
| 112 if (displays.empty()) | |
| 113 return; | |
| 114 | |
| 115 screen_manager->AddDisplayController(drm, displays[0]->crtc()->crtc_id, | |
| 116 displays[0]->connector()->connector_id); | |
| 117 screen_manager->ConfigureDisplayController( | |
| 118 drm, displays[0]->crtc()->crtc_id, displays[0]->connector()->connector_id, | |
| 119 gfx::Point(), displays[0]->connector()->modes[0]); | |
| 120 } | |
| 121 | |
| 122 } // namespace ui | |
| OLD | NEW |