1 // Copyright 2015 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.h"
7 #include <xf86drmMode.h>
9 #include "ui/display/types/gamma_ramp_rgb_entry.h"
10 #include "ui/ozone/platform/drm/common/drm_util.h"
11 #include "ui/ozone/platform/drm/gpu/drm_device.h"
12 #include "ui/ozone/platform/drm/gpu/screen_manager.h"
18 const char kContentProtection
[] = "Content Protection";
20 struct ContentProtectionMapping
{
25 const ContentProtectionMapping kContentProtectionStates
[] = {
26 {"Undesired", HDCP_STATE_UNDESIRED
},
27 {"Desired", HDCP_STATE_DESIRED
},
28 {"Enabled", HDCP_STATE_ENABLED
}};
30 // Converts |state| to the DRM value associated with the it.
31 uint32_t GetContentProtectionValue(drmModePropertyRes
* property
,
34 for (size_t i
= 0; i
< arraysize(kContentProtectionStates
); ++i
) {
35 if (kContentProtectionStates
[i
].state
== state
) {
36 name
= kContentProtectionStates
[i
].name
;
41 for (int i
= 0; i
< property
->count_enums
; ++i
)
42 if (name
== property
->enums
[i
].name
)
49 std::string
GetEnumNameForProperty(drmModeConnector
* connector
,
50 drmModePropertyRes
* property
) {
51 for (int prop_idx
= 0; prop_idx
< connector
->count_props
; ++prop_idx
) {
52 if (connector
->props
[prop_idx
] != property
->prop_id
)
55 for (int enum_idx
= 0; enum_idx
< property
->count_enums
; ++enum_idx
) {
56 const drm_mode_property_enum
& property_enum
= property
->enums
[enum_idx
];
57 if (property_enum
.value
== connector
->prop_values
[prop_idx
])
58 return property_enum
.name
;
66 gfx::Size
GetDrmModeSize(const drmModeModeInfo
& mode
) {
67 return gfx::Size(mode
.hdisplay
, mode
.vdisplay
);
70 std::vector
<drmModeModeInfo
> GetDrmModeVector(drmModeConnector
* connector
) {
71 std::vector
<drmModeModeInfo
> modes
;
72 for (int i
= 0; i
< connector
->count_modes
; ++i
)
73 modes
.push_back(connector
->modes
[i
]);
80 DrmDisplay::DrmDisplay(ScreenManager
* screen_manager
,
81 const scoped_refptr
<DrmDevice
>& drm
)
82 : screen_manager_(screen_manager
), drm_(drm
) {
85 DrmDisplay::~DrmDisplay() {
88 DisplaySnapshot_Params
DrmDisplay::Update(HardwareDisplayControllerInfo
* info
,
89 size_t display_index
) {
90 DisplaySnapshot_Params params
=
91 CreateDisplaySnapshotParams(info
, drm_
->get_fd(), display_index
, origin_
);
92 crtc_
= info
->crtc()->crtc_id
;
93 connector_
= info
->connector()->connector_id
;
94 display_id_
= params
.display_id
;
95 modes_
= GetDrmModeVector(info
->connector());
99 bool DrmDisplay::Configure(const drmModeModeInfo
* mode
,
100 const gfx::Point
& origin
) {
101 VLOG(1) << "DRM configuring: device=" << drm_
->device_path().value()
102 << " crtc=" << crtc_
<< " connector=" << connector_
103 << " origin=" << origin
.ToString()
104 << " size=" << (mode
? GetDrmModeSize(*mode
).ToString() : "0x0");
107 if (!screen_manager_
->ConfigureDisplayController(drm_
, crtc_
, connector_
,
109 VLOG(1) << "Failed to configure: device=" << drm_
->device_path().value()
110 << " crtc=" << crtc_
<< " connector=" << connector_
;
114 if (!screen_manager_
->DisableDisplayController(drm_
, crtc_
)) {
115 VLOG(1) << "Failed to disable device=" << drm_
->device_path().value()
116 << " crtc=" << crtc_
;
125 bool DrmDisplay::GetHDCPState(HDCPState
* state
) {
126 ScopedDrmConnectorPtr
connector(drm_
->GetConnector(connector_
));
128 PLOG(ERROR
) << "Failed to get connector " << connector_
;
132 ScopedDrmPropertyPtr
hdcp_property(
133 drm_
->GetProperty(connector
.get(), kContentProtection
));
134 if (!hdcp_property
) {
135 PLOG(ERROR
) << "'" << kContentProtection
<< "' property doesn't exist.";
140 GetEnumNameForProperty(connector
.get(), hdcp_property
.get());
141 for (size_t i
= 0; i
< arraysize(kContentProtectionStates
); ++i
) {
142 if (name
== kContentProtectionStates
[i
].name
) {
143 *state
= kContentProtectionStates
[i
].state
;
144 VLOG(3) << "HDCP state: " << *state
<< " (" << name
<< ")";
149 LOG(ERROR
) << "Unknown content protection value '" << name
<< "'";
153 bool DrmDisplay::SetHDCPState(HDCPState state
) {
154 ScopedDrmConnectorPtr
connector(drm_
->GetConnector(connector_
));
156 PLOG(ERROR
) << "Failed to get connector " << connector_
;
160 ScopedDrmPropertyPtr
hdcp_property(
161 drm_
->GetProperty(connector
.get(), kContentProtection
));
162 if (!hdcp_property
) {
163 LOG(ERROR
) << "'" << kContentProtection
<< "' property doesn't exist.";
167 return drm_
->SetProperty(
168 connector_
, hdcp_property
->prop_id
,
169 GetContentProtectionValue(hdcp_property
.get(), state
));
172 void DrmDisplay::SetGammaRamp(const std::vector
<GammaRampRGBEntry
>& lut
) {
173 if (!drm_
->SetGammaRamp(crtc_
, lut
)) {
174 LOG(ERROR
) << "Failed to set gamma ramp for display: crtc_id = " << crtc_
175 << " size = " << lut
.size();