Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / ozone / platform / drm / gpu / drm_display.cc
blob4e949ecfc5f343dbfdf025db4c3301135ee6f9f3
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"
14 namespace ui {
16 namespace {
18 const char kContentProtection[] = "Content Protection";
20 struct ContentProtectionMapping {
21 const char* name;
22 HDCPState state;
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,
32 HDCPState state) {
33 std::string name;
34 for (size_t i = 0; i < arraysize(kContentProtectionStates); ++i) {
35 if (kContentProtectionStates[i].state == state) {
36 name = kContentProtectionStates[i].name;
37 break;
41 for (int i = 0; i < property->count_enums; ++i)
42 if (name == property->enums[i].name)
43 return i;
45 NOTREACHED();
46 return 0;
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)
53 continue;
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;
62 NOTREACHED();
63 return std::string();
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]);
75 return modes;
78 } // namespace
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());
96 return params;
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");
106 if (mode) {
107 if (!screen_manager_->ConfigureDisplayController(drm_, crtc_, connector_,
108 origin, *mode)) {
109 VLOG(1) << "Failed to configure: device=" << drm_->device_path().value()
110 << " crtc=" << crtc_ << " connector=" << connector_;
111 return false;
113 } else {
114 if (!screen_manager_->DisableDisplayController(drm_, crtc_)) {
115 VLOG(1) << "Failed to disable device=" << drm_->device_path().value()
116 << " crtc=" << crtc_;
117 return false;
121 origin_ = origin;
122 return true;
125 bool DrmDisplay::GetHDCPState(HDCPState* state) {
126 ScopedDrmConnectorPtr connector(drm_->GetConnector(connector_));
127 if (!connector) {
128 PLOG(ERROR) << "Failed to get connector " << connector_;
129 return false;
132 ScopedDrmPropertyPtr hdcp_property(
133 drm_->GetProperty(connector.get(), kContentProtection));
134 if (!hdcp_property) {
135 PLOG(ERROR) << "'" << kContentProtection << "' property doesn't exist.";
136 return false;
139 std::string name =
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 << ")";
145 return true;
149 LOG(ERROR) << "Unknown content protection value '" << name << "'";
150 return false;
153 bool DrmDisplay::SetHDCPState(HDCPState state) {
154 ScopedDrmConnectorPtr connector(drm_->GetConnector(connector_));
155 if (!connector) {
156 PLOG(ERROR) << "Failed to get connector " << connector_;
157 return false;
160 ScopedDrmPropertyPtr hdcp_property(
161 drm_->GetProperty(connector.get(), kContentProtection));
162 if (!hdcp_property) {
163 LOG(ERROR) << "'" << kContentProtection << "' property doesn't exist.";
164 return false;
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();
179 } // namespace ui