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.
5 #include "ui/ozone/platform/drm/gpu/drm_display_snapshot.h"
9 #include <xf86drmMode.h>
11 #include "base/format_macros.h"
12 #include "base/logging.h"
13 #include "base/strings/stringprintf.h"
14 #include "ui/display/util/edid_parser.h"
15 #include "ui/ozone/platform/drm/gpu/drm_device.h"
16 #include "ui/ozone/platform/drm/gpu/drm_display_mode.h"
17 #include "ui/ozone/platform/drm/gpu/drm_util.h"
19 #if !defined(DRM_MODE_CONNECTOR_DSI)
20 #define DRM_MODE_CONNECTOR_DSI 16
27 DisplayConnectionType
GetDisplayType(drmModeConnector
* connector
) {
28 switch (connector
->connector_type
) {
29 case DRM_MODE_CONNECTOR_VGA
:
30 return DISPLAY_CONNECTION_TYPE_VGA
;
31 case DRM_MODE_CONNECTOR_DVII
:
32 case DRM_MODE_CONNECTOR_DVID
:
33 case DRM_MODE_CONNECTOR_DVIA
:
34 return DISPLAY_CONNECTION_TYPE_DVI
;
35 case DRM_MODE_CONNECTOR_LVDS
:
36 case DRM_MODE_CONNECTOR_eDP
:
37 case DRM_MODE_CONNECTOR_DSI
:
38 return DISPLAY_CONNECTION_TYPE_INTERNAL
;
39 case DRM_MODE_CONNECTOR_DisplayPort
:
40 return DISPLAY_CONNECTION_TYPE_DISPLAYPORT
;
41 case DRM_MODE_CONNECTOR_HDMIA
:
42 case DRM_MODE_CONNECTOR_HDMIB
:
43 return DISPLAY_CONNECTION_TYPE_HDMI
;
45 return DISPLAY_CONNECTION_TYPE_UNKNOWN
;
49 bool IsAspectPreserving(DrmDevice
* drm
, drmModeConnector
* connector
) {
50 ScopedDrmPropertyPtr
property(drm
->GetProperty(connector
, "scaling mode"));
54 for (int props_i
= 0; props_i
< connector
->count_props
; ++props_i
) {
55 if (connector
->props
[props_i
] != property
->prop_id
)
58 for (int enums_i
= 0; enums_i
< property
->count_enums
; ++enums_i
) {
59 if (property
->enums
[enums_i
].value
== connector
->prop_values
[props_i
] &&
60 strcmp(property
->enums
[enums_i
].name
, "Full aspect") == 0)
70 DrmDisplaySnapshot::DrmDisplaySnapshot(const scoped_refptr
<DrmDevice
>& drm
,
71 drmModeConnector
* connector
,
74 : DisplaySnapshot(index
,
75 gfx::Point(crtc
->x
, crtc
->y
),
76 gfx::Size(connector
->mmWidth
, connector
->mmHeight
),
77 GetDisplayType(connector
),
78 IsAspectPreserving(drm
.get(), connector
),
81 std::vector
<const DisplayMode
*>(),
85 connector_(connector
->connector_id
),
87 dpms_property_(drm
->GetProperty(connector
, "DPMS")) {
89 VLOG(1) << "Failed to find the DPMS property for connector "
90 << connector
->connector_id
;
92 ScopedDrmPropertyBlobPtr
edid_blob(drm
->GetPropertyBlob(connector
, "EDID"));
95 std::vector
<uint8_t> edid(
96 static_cast<uint8_t*>(edid_blob
->data
),
97 static_cast<uint8_t*>(edid_blob
->data
) + edid_blob
->length
);
99 if (!GetDisplayIdFromEDID(edid
, index
, &display_id_
))
102 ParseOutputDeviceData(edid
, nullptr, &display_name_
, nullptr, nullptr);
103 ParseOutputOverscanFlag(edid
, &overscan_flag_
);
105 VLOG(1) << "Failed to get EDID blob for connector "
106 << connector
->connector_id
;
109 for (int i
= 0; i
< connector
->count_modes
; ++i
) {
110 drmModeModeInfo
& mode
= connector
->modes
[i
];
111 modes_
.push_back(new DrmDisplayMode(mode
));
113 if (crtc
->mode_valid
&& SameMode(crtc
->mode
, mode
))
114 current_mode_
= modes_
.back();
116 if (mode
.type
& DRM_MODE_TYPE_PREFERRED
)
117 native_mode_
= modes_
.back();
120 // If no preferred mode is found then use the first one. Using the first one
121 // since it should be the best mode.
122 if (!native_mode_
&& !modes_
.empty())
123 native_mode_
= modes_
.front();
126 DrmDisplaySnapshot::~DrmDisplaySnapshot() {
129 std::string
DrmDisplaySnapshot::ToString() const {
130 return base::StringPrintf(
131 "[type=%d, connector=%" PRIu32
", crtc=%" PRIu32
132 ", origin=%s, mode=%s, dim=%s]",
133 type_
, connector_
, crtc_
, origin_
.ToString().c_str(),
134 current_mode_
? current_mode_
->ToString().c_str() : "NULL",
135 physical_size_
.ToString().c_str());