Chromium Code Reviews| 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 typedef scoped_ptr<DrmColorLut, base::FreeDeleter> ScopedDrmColorLutPtr; | |
|
dcheng
2016/02/26 09:37:48
Nit: prefer using ScopedDrmColorLutPtr = ... to ty
robert.bradford
2016/02/26 15:15:22
Ah good to know. Done.
| |
| 131 typedef scoped_ptr<DrmColorCtm, base::FreeDeleter> ScopedDrmColorCtmPtr; | |
| 132 | |
| 133 ScopedDrmColorLutPtr CreateLutBlob( | |
| 134 const std::vector<GammaRampRGBEntry>& source) { | |
| 135 TRACE_EVENT0("drm", "CreateLutBlob"); | |
| 136 DrmColorLut* lut = | |
|
dcheng
2016/02/26 09:37:48
ScopedDrmColorLutPtr lut(static_cast<DrmColorLut*>
robert.bradford
2016/02/26 15:15:22
Done.
| |
| 137 static_cast<DrmColorLut*>(malloc(sizeof(DrmColorLut) * source.size())); | |
| 138 for (size_t i = 0; i < source.size(); ++i) { | |
| 139 lut[i].red = source[i].r; | |
| 140 lut[i].green = source[i].g; | |
| 141 lut[i].blue = source[i].b; | |
| 142 } | |
| 143 return ScopedDrmColorLutPtr(lut); | |
| 144 } | |
| 145 | |
| 146 ScopedDrmColorCtmPtr CreateCTMBlob( | |
| 147 const std::vector<float>& correction_matrix) { | |
| 148 DrmColorCtm* ctm = static_cast<DrmColorCtm*>(malloc(sizeof(DrmColorCtm))); | |
| 149 for (int i = 0; i < 9; ++i) { | |
|
spang
2016/02/24 16:05:44
i < arraysize(ctm->ctm_coeff)
robert.bradford
2016/02/26 15:15:22
Done.
| |
| 150 if (correction_matrix[i] < 0) { | |
| 151 ctm->ctm_coeff[i] = static_cast<uint64_t>( | |
| 152 -correction_matrix[i] * (static_cast<uint64_t>(1) << 32)); | |
| 153 ctm->ctm_coeff[i] |= static_cast<uint64_t>(1) << 63; | |
| 154 } else { | |
| 155 ctm->ctm_coeff[i] = static_cast<uint64_t>( | |
| 156 correction_matrix[i] * (static_cast<uint64_t>(1) << 32)); | |
| 157 } | |
| 158 } | |
| 159 return ScopedDrmColorCtmPtr(ctm); | |
| 160 } | |
| 161 | |
| 162 bool SetBlobProperty(int fd, | |
| 163 uint32_t object_id, | |
| 164 uint32_t object_type, | |
| 165 uint32_t prop_id, | |
| 166 const char* property_name, | |
| 167 unsigned char* data, | |
| 168 size_t length) { | |
| 169 uint32_t blob_id; | |
| 170 int res; | |
| 171 res = drmModeCreatePropertyBlob(fd, data, length, &blob_id); | |
| 172 if (res != 0) { | |
| 173 LOG(ERROR) << "Error creating property blob: " << base::safe_strerror(res) | |
| 174 << " for property " << property_name; | |
| 175 return false; | |
| 176 } | |
| 177 res = drmModeObjectSetProperty(fd, object_id, object_type, prop_id, blob_id); | |
| 178 if (res != 0) { | |
| 179 LOG(ERROR) << "Error updating property: " << base::safe_strerror(res) | |
| 180 << " for property " << property_name; | |
| 181 drmModeDestroyPropertyBlob(fd, blob_id); | |
| 182 return false; | |
| 183 } | |
| 184 drmModeDestroyPropertyBlob(fd, blob_id); | |
| 185 return true; | |
| 186 } | |
| 187 | |
| 188 std::vector<GammaRampRGBEntry> ResampleLut( | |
| 189 const std::vector<GammaRampRGBEntry>& lut_in, | |
| 190 size_t desired_size) { | |
| 191 TRACE_EVENT1("drm", "ResampleLut", "desired_size", desired_size); | |
| 192 if (lut_in.size() == desired_size) | |
| 193 return lut_in; | |
| 194 | |
| 195 std::vector<GammaRampRGBEntry> result; | |
| 196 result.resize(desired_size); | |
| 197 | |
| 198 for (size_t i = 0; i < desired_size; ++i) { | |
| 199 size_t base_index = lut_in.size() * i / desired_size; | |
| 200 size_t remaining = lut_in.size() * i % desired_size; | |
| 201 if (base_index < lut_in.size() - 1) { | |
| 202 result[i].r = lut_in[base_index].r + | |
| 203 (lut_in[base_index + 1].r - lut_in[base_index].r) * | |
| 204 remaining / desired_size; | |
| 205 result[i].g = lut_in[base_index].g + | |
| 206 (lut_in[base_index + 1].g - lut_in[base_index].g) * | |
| 207 remaining / desired_size; | |
| 208 result[i].b = lut_in[base_index].b + | |
| 209 (lut_in[base_index + 1].b - lut_in[base_index].b) * | |
| 210 remaining / desired_size; | |
| 211 } else { | |
| 212 result[i] = lut_in[lut_in.size() - 1]; | |
| 213 } | |
| 214 } | |
| 215 | |
| 216 return result; | |
| 217 } | |
| 218 | |
| 116 } // namespace | 219 } // namespace |
| 117 | 220 |
| 118 class DrmDevice::PageFlipManager { | 221 class DrmDevice::PageFlipManager { |
| 119 public: | 222 public: |
| 120 PageFlipManager() : next_id_(0) {} | 223 PageFlipManager() : next_id_(0) {} |
| 121 ~PageFlipManager() {} | 224 ~PageFlipManager() {} |
| 122 | 225 |
| 123 void OnPageFlip(uint32_t frame, | 226 void OnPageFlip(uint32_t frame, |
| 124 uint32_t seconds, | 227 uint32_t seconds, |
| 125 uint32_t useconds, | 228 uint32_t useconds, |
| (...skipping 409 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 535 g.push_back(lut[i].g); | 638 g.push_back(lut[i].g); |
| 536 b.push_back(lut[i].b); | 639 b.push_back(lut[i].b); |
| 537 } | 640 } |
| 538 | 641 |
| 539 DCHECK(file_.IsValid()); | 642 DCHECK(file_.IsValid()); |
| 540 TRACE_EVENT0("drm", "DrmDevice::SetGamma"); | 643 TRACE_EVENT0("drm", "DrmDevice::SetGamma"); |
| 541 return (drmModeCrtcSetGamma(file_.GetPlatformFile(), crtc_id, r.size(), &r[0], | 644 return (drmModeCrtcSetGamma(file_.GetPlatformFile(), crtc_id, r.size(), &r[0], |
| 542 &g[0], &b[0]) == 0); | 645 &g[0], &b[0]) == 0); |
| 543 } | 646 } |
| 544 | 647 |
| 648 bool DrmDevice::SetColorCorrection( | |
| 649 uint32_t crtc_id, | |
| 650 const std::vector<GammaRampRGBEntry>& degamma_lut, | |
| 651 const std::vector<GammaRampRGBEntry>& gamma_lut, | |
| 652 const std::vector<float>& correction_matrix) { | |
| 653 ScopedDrmObjectPropertyPtr crtc_props(drmModeObjectGetProperties( | |
| 654 file_.GetPlatformFile(), crtc_id, DRM_MODE_OBJECT_CRTC)); | |
| 655 uint64_t degamma_lut_size = 0; | |
| 656 uint64_t gamma_lut_size = 0; | |
| 657 | |
| 658 for (uint32_t i = 0; i < crtc_props->count_props; ++i) { | |
| 659 ScopedDrmPropertyPtr property( | |
| 660 drmModeGetProperty(file_.GetPlatformFile(), crtc_props->props[i])); | |
| 661 if (property && !strcmp(property->name, "DEGAMMA_LUT_SIZE")) { | |
| 662 degamma_lut_size = crtc_props->prop_values[i]; | |
| 663 } | |
| 664 if (property && !strcmp(property->name, "GAMMA_LUT_SIZE")) { | |
| 665 gamma_lut_size = crtc_props->prop_values[i]; | |
| 666 } | |
| 667 | |
| 668 if (degamma_lut_size && gamma_lut_size) | |
| 669 break; | |
| 670 } | |
| 671 | |
| 672 if (degamma_lut_size == 0 || gamma_lut_size == 0) { | |
| 673 LOG(WARNING) << "No available (de)gamma tables."; | |
| 674 return false; | |
| 675 } | |
| 676 | |
| 677 ScopedDrmColorLutPtr degamma_blob_data = | |
| 678 CreateLutBlob(ResampleLut(degamma_lut, degamma_lut_size)); | |
| 679 ScopedDrmColorLutPtr gamma_blob_data = | |
| 680 CreateLutBlob(ResampleLut(gamma_lut, gamma_lut_size)); | |
| 681 ScopedDrmColorCtmPtr ctm_blob_data = CreateCTMBlob(correction_matrix); | |
| 682 | |
| 683 for (uint32_t i = 0; i < crtc_props->count_props; ++i) { | |
| 684 ScopedDrmPropertyPtr property( | |
| 685 drmModeGetProperty(file_.GetPlatformFile(), crtc_props->props[i])); | |
| 686 if (!property) | |
| 687 continue; | |
| 688 | |
| 689 if (!strcmp(property->name, "DEGAMMA_LUT")) { | |
| 690 if (!SetBlobProperty( | |
| 691 file_.GetPlatformFile(), crtc_id, DRM_MODE_OBJECT_CRTC, | |
| 692 crtc_props->props[i], property->name, | |
| 693 reinterpret_cast<unsigned char*>(degamma_blob_data.get()), | |
| 694 sizeof(DrmColorLut) * degamma_lut_size)) | |
| 695 return false; | |
| 696 } | |
| 697 if (!strcmp(property->name, "GAMMA_LUT")) { | |
| 698 if (!SetBlobProperty( | |
| 699 file_.GetPlatformFile(), crtc_id, DRM_MODE_OBJECT_CRTC, | |
| 700 crtc_props->props[i], property->name, | |
| 701 reinterpret_cast<unsigned char*>(gamma_blob_data.get()), | |
| 702 sizeof(DrmColorLut) * gamma_lut_size)) | |
| 703 return false; | |
| 704 } | |
| 705 if (!strcmp(property->name, "CTM")) { | |
| 706 if (!SetBlobProperty( | |
| 707 file_.GetPlatformFile(), crtc_id, DRM_MODE_OBJECT_CRTC, | |
| 708 crtc_props->props[i], property->name, | |
| 709 reinterpret_cast<unsigned char*>(ctm_blob_data.get()), | |
| 710 sizeof(DrmColorCtm))) | |
| 711 return false; | |
| 712 } | |
| 713 } | |
| 714 | |
| 715 return true; | |
| 716 } | |
| 717 | |
| 545 } // namespace ui | 718 } // namespace ui |
| OLD | NEW |