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/gpu/drm_device.h" | 5 #include "ui/ozone/platform/drm/gpu/drm_device.h" |
6 | 6 |
7 #include <fcntl.h> | 7 #include <fcntl.h> |
8 #include <sys/mman.h> | 8 #include <sys/mman.h> |
9 #include <unistd.h> | 9 #include <unistd.h> |
10 #include <xf86drm.h> | 10 #include <xf86drm.h> |
11 #include <xf86drmMode.h> | 11 #include <xf86drmMode.h> |
12 #include <utility> | 12 #include <utility> |
13 | 13 |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/macros.h" | 15 #include "base/macros.h" |
16 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
| 17 #include "base/posix/safe_strerror.h" |
17 #include "base/task_runner.h" | 18 #include "base/task_runner.h" |
18 #include "base/thread_task_runner_handle.h" | 19 #include "base/thread_task_runner_handle.h" |
19 #include "base/trace_event/trace_event.h" | 20 #include "base/trace_event/trace_event.h" |
20 #include "third_party/skia/include/core/SkImageInfo.h" | 21 #include "third_party/skia/include/core/SkImageInfo.h" |
21 #include "ui/display/types/gamma_ramp_rgb_entry.h" | 22 #include "ui/display/types/gamma_ramp_rgb_entry.h" |
22 #include "ui/ozone/platform/drm/common/drm_util.h" | 23 #include "ui/ozone/platform/drm/common/drm_util.h" |
23 #include "ui/ozone/platform/drm/gpu/hardware_display_plane_manager_legacy.h" | 24 #include "ui/ozone/platform/drm/gpu/hardware_display_plane_manager_legacy.h" |
24 | 25 |
25 #if defined(USE_DRM_ATOMIC) | 26 #if defined(USE_DRM_ATOMIC) |
26 #include "ui/ozone/platform/drm/gpu/hardware_display_plane_manager_atomic.h" | 27 #include "ui/ozone/platform/drm/gpu/hardware_display_plane_manager_atomic.h" |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 } | 107 } |
107 | 108 |
108 bool CanQueryForResources(int fd) { | 109 bool CanQueryForResources(int fd) { |
109 drm_mode_card_res resources; | 110 drm_mode_card_res resources; |
110 memset(&resources, 0, sizeof(resources)); | 111 memset(&resources, 0, sizeof(resources)); |
111 // If there is no error getting DRM resources then assume this is a | 112 // If there is no error getting DRM resources then assume this is a |
112 // modesetting device. | 113 // modesetting device. |
113 return !drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &resources); | 114 return !drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &resources); |
114 } | 115 } |
115 | 116 |
| 117 // TODO(robert.bradford): Replace with libdrm structures after libdrm roll. |
| 118 // https://crbug.com/586475 |
| 119 struct DrmColorLut { |
| 120 uint16_t red; |
| 121 uint16_t green; |
| 122 uint16_t blue; |
| 123 uint16_t reserved; |
| 124 }; |
| 125 |
| 126 struct DrmColorCtm { |
| 127 int64_t ctm_coeff[9]; |
| 128 }; |
| 129 |
| 130 using ScopedDrmColorLutPtr = scoped_ptr<DrmColorLut, base::FreeDeleter>; |
| 131 using ScopedDrmColorCtmPtr = scoped_ptr<DrmColorCtm, base::FreeDeleter>; |
| 132 |
| 133 ScopedDrmColorLutPtr CreateLutBlob( |
| 134 const std::vector<GammaRampRGBEntry>& source) { |
| 135 TRACE_EVENT0("drm", "CreateLutBlob"); |
| 136 ScopedDrmColorLutPtr lut( |
| 137 static_cast<DrmColorLut*>(malloc(sizeof(DrmColorLut) * source.size()))); |
| 138 DrmColorLut* p = lut.get(); |
| 139 for (size_t i = 0; i < source.size(); ++i) { |
| 140 p[i].red = source[i].r; |
| 141 p[i].green = source[i].g; |
| 142 p[i].blue = source[i].b; |
| 143 } |
| 144 return lut; |
| 145 } |
| 146 |
| 147 ScopedDrmColorCtmPtr CreateCTMBlob( |
| 148 const std::vector<float>& correction_matrix) { |
| 149 ScopedDrmColorCtmPtr ctm( |
| 150 static_cast<DrmColorCtm*>(malloc(sizeof(DrmColorCtm)))); |
| 151 for (int i = 0; i < arraysize(ctm->ctm_coeff); ++i) { |
| 152 if (correction_matrix[i] < 0) { |
| 153 ctm->ctm_coeff[i] = static_cast<uint64_t>( |
| 154 -correction_matrix[i] * (static_cast<uint64_t>(1) << 32)); |
| 155 ctm->ctm_coeff[i] |= static_cast<uint64_t>(1) << 63; |
| 156 } else { |
| 157 ctm->ctm_coeff[i] = static_cast<uint64_t>( |
| 158 correction_matrix[i] * (static_cast<uint64_t>(1) << 32)); |
| 159 } |
| 160 } |
| 161 return ctm; |
| 162 } |
| 163 |
| 164 bool SetBlobProperty(int fd, |
| 165 uint32_t object_id, |
| 166 uint32_t object_type, |
| 167 uint32_t prop_id, |
| 168 const char* property_name, |
| 169 unsigned char* data, |
| 170 size_t length) { |
| 171 uint32_t blob_id; |
| 172 int res; |
| 173 res = drmModeCreatePropertyBlob(fd, data, length, &blob_id); |
| 174 if (res != 0) { |
| 175 LOG(ERROR) << "Error creating property blob: " << base::safe_strerror(res) |
| 176 << " for property " << property_name; |
| 177 return false; |
| 178 } |
| 179 res = drmModeObjectSetProperty(fd, object_id, object_type, prop_id, blob_id); |
| 180 if (res != 0) { |
| 181 LOG(ERROR) << "Error updating property: " << base::safe_strerror(res) |
| 182 << " for property " << property_name; |
| 183 drmModeDestroyPropertyBlob(fd, blob_id); |
| 184 return false; |
| 185 } |
| 186 drmModeDestroyPropertyBlob(fd, blob_id); |
| 187 return true; |
| 188 } |
| 189 |
| 190 std::vector<GammaRampRGBEntry> ResampleLut( |
| 191 const std::vector<GammaRampRGBEntry>& lut_in, |
| 192 size_t desired_size) { |
| 193 TRACE_EVENT1("drm", "ResampleLut", "desired_size", desired_size); |
| 194 if (lut_in.size() == desired_size) |
| 195 return lut_in; |
| 196 |
| 197 std::vector<GammaRampRGBEntry> result; |
| 198 result.resize(desired_size); |
| 199 |
| 200 for (size_t i = 0; i < desired_size; ++i) { |
| 201 size_t base_index = lut_in.size() * i / desired_size; |
| 202 size_t remaining = lut_in.size() * i % desired_size; |
| 203 if (base_index < lut_in.size() - 1) { |
| 204 result[i].r = lut_in[base_index].r + |
| 205 (lut_in[base_index + 1].r - lut_in[base_index].r) * |
| 206 remaining / desired_size; |
| 207 result[i].g = lut_in[base_index].g + |
| 208 (lut_in[base_index + 1].g - lut_in[base_index].g) * |
| 209 remaining / desired_size; |
| 210 result[i].b = lut_in[base_index].b + |
| 211 (lut_in[base_index + 1].b - lut_in[base_index].b) * |
| 212 remaining / desired_size; |
| 213 } else { |
| 214 result[i] = lut_in[lut_in.size() - 1]; |
| 215 } |
| 216 } |
| 217 |
| 218 return result; |
| 219 } |
| 220 |
116 } // namespace | 221 } // namespace |
117 | 222 |
118 class DrmDevice::PageFlipManager { | 223 class DrmDevice::PageFlipManager { |
119 public: | 224 public: |
120 PageFlipManager() : next_id_(0) {} | 225 PageFlipManager() : next_id_(0) {} |
121 ~PageFlipManager() {} | 226 ~PageFlipManager() {} |
122 | 227 |
123 void OnPageFlip(uint32_t frame, | 228 void OnPageFlip(uint32_t frame, |
124 uint32_t seconds, | 229 uint32_t seconds, |
125 uint32_t useconds, | 230 uint32_t useconds, |
(...skipping 409 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
535 g.push_back(lut[i].g); | 640 g.push_back(lut[i].g); |
536 b.push_back(lut[i].b); | 641 b.push_back(lut[i].b); |
537 } | 642 } |
538 | 643 |
539 DCHECK(file_.IsValid()); | 644 DCHECK(file_.IsValid()); |
540 TRACE_EVENT0("drm", "DrmDevice::SetGamma"); | 645 TRACE_EVENT0("drm", "DrmDevice::SetGamma"); |
541 return (drmModeCrtcSetGamma(file_.GetPlatformFile(), crtc_id, r.size(), &r[0], | 646 return (drmModeCrtcSetGamma(file_.GetPlatformFile(), crtc_id, r.size(), &r[0], |
542 &g[0], &b[0]) == 0); | 647 &g[0], &b[0]) == 0); |
543 } | 648 } |
544 | 649 |
| 650 bool DrmDevice::SetColorCorrection( |
| 651 uint32_t crtc_id, |
| 652 const std::vector<GammaRampRGBEntry>& degamma_lut, |
| 653 const std::vector<GammaRampRGBEntry>& gamma_lut, |
| 654 const std::vector<float>& correction_matrix) { |
| 655 ScopedDrmObjectPropertyPtr crtc_props(drmModeObjectGetProperties( |
| 656 file_.GetPlatformFile(), crtc_id, DRM_MODE_OBJECT_CRTC)); |
| 657 uint64_t degamma_lut_size = 0; |
| 658 uint64_t gamma_lut_size = 0; |
| 659 |
| 660 for (uint32_t i = 0; i < crtc_props->count_props; ++i) { |
| 661 ScopedDrmPropertyPtr property( |
| 662 drmModeGetProperty(file_.GetPlatformFile(), crtc_props->props[i])); |
| 663 if (property && !strcmp(property->name, "DEGAMMA_LUT_SIZE")) { |
| 664 degamma_lut_size = crtc_props->prop_values[i]; |
| 665 } |
| 666 if (property && !strcmp(property->name, "GAMMA_LUT_SIZE")) { |
| 667 gamma_lut_size = crtc_props->prop_values[i]; |
| 668 } |
| 669 |
| 670 if (degamma_lut_size && gamma_lut_size) |
| 671 break; |
| 672 } |
| 673 |
| 674 if (degamma_lut_size == 0 || gamma_lut_size == 0) { |
| 675 LOG(WARNING) << "No available (de)gamma tables."; |
| 676 return false; |
| 677 } |
| 678 |
| 679 ScopedDrmColorLutPtr degamma_blob_data = |
| 680 CreateLutBlob(ResampleLut(degamma_lut, degamma_lut_size)); |
| 681 ScopedDrmColorLutPtr gamma_blob_data = |
| 682 CreateLutBlob(ResampleLut(gamma_lut, gamma_lut_size)); |
| 683 ScopedDrmColorCtmPtr ctm_blob_data = CreateCTMBlob(correction_matrix); |
| 684 |
| 685 for (uint32_t i = 0; i < crtc_props->count_props; ++i) { |
| 686 ScopedDrmPropertyPtr property( |
| 687 drmModeGetProperty(file_.GetPlatformFile(), crtc_props->props[i])); |
| 688 if (!property) |
| 689 continue; |
| 690 |
| 691 if (!strcmp(property->name, "DEGAMMA_LUT")) { |
| 692 if (!SetBlobProperty( |
| 693 file_.GetPlatformFile(), crtc_id, DRM_MODE_OBJECT_CRTC, |
| 694 crtc_props->props[i], property->name, |
| 695 reinterpret_cast<unsigned char*>(degamma_blob_data.get()), |
| 696 sizeof(DrmColorLut) * degamma_lut_size)) |
| 697 return false; |
| 698 } |
| 699 if (!strcmp(property->name, "GAMMA_LUT")) { |
| 700 if (!SetBlobProperty( |
| 701 file_.GetPlatformFile(), crtc_id, DRM_MODE_OBJECT_CRTC, |
| 702 crtc_props->props[i], property->name, |
| 703 reinterpret_cast<unsigned char*>(gamma_blob_data.get()), |
| 704 sizeof(DrmColorLut) * gamma_lut_size)) |
| 705 return false; |
| 706 } |
| 707 if (!strcmp(property->name, "CTM")) { |
| 708 if (!SetBlobProperty( |
| 709 file_.GetPlatformFile(), crtc_id, DRM_MODE_OBJECT_CRTC, |
| 710 crtc_props->props[i], property->name, |
| 711 reinterpret_cast<unsigned char*>(ctm_blob_data.get()), |
| 712 sizeof(DrmColorCtm))) |
| 713 return false; |
| 714 } |
| 715 } |
| 716 |
| 717 return true; |
| 718 } |
| 719 |
545 } // namespace ui | 720 } // namespace ui |
OLD | NEW |