Index: ui/ozone/platform/drm/gpu/drm_device.cc |
diff --git a/ui/ozone/platform/drm/gpu/drm_device.cc b/ui/ozone/platform/drm/gpu/drm_device.cc |
index 3a30fd23e5ac3cf4b83919525a8c01dcd60e39ed..891a136c8f8d70d892616d2bc3a77ad4a8e30194 100644 |
--- a/ui/ozone/platform/drm/gpu/drm_device.cc |
+++ b/ui/ozone/platform/drm/gpu/drm_device.cc |
@@ -13,7 +13,9 @@ |
#include "base/logging.h" |
#include "base/macros.h" |
+#include "base/memory/free_deleter.h" |
#include "base/message_loop/message_loop.h" |
+#include "base/posix/safe_strerror.h" |
#include "base/task_runner.h" |
#include "base/thread_task_runner_handle.h" |
#include "base/trace_event/trace_event.h" |
@@ -113,6 +115,161 @@ bool CanQueryForResources(int fd) { |
return !drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &resources); |
} |
+// TODO(robert.bradford): Replace with libdrm structures after libdrm roll. |
+// https://crbug.com/586475 |
+struct DrmColorLut { |
+ uint16_t red; |
+ uint16_t green; |
+ uint16_t blue; |
+ uint16_t reserved; |
+}; |
+ |
+struct DrmColorCtm { |
+ int64_t ctm_coeff[9]; |
+}; |
+ |
+struct DrmModeCreateBlob { |
+ uint64_t data; |
+ uint32_t length; |
+ uint32_t blob_id; |
+}; |
+ |
+struct DrmModeDestroyBlob { |
+ uint32_t blob_id; |
+}; |
+ |
+#ifndef DRM_IOCTL_MODE_CREATEPROPBLOB |
+#define DRM_IOCTL_MODE_CREATEPROPBLOB DRM_IOWR(0xBD, struct DrmModeCreateBlob) |
+#endif |
+ |
+#ifndef DRM_IOCTL_MODE_DESTROYPROPBLOB |
+#define DRM_IOCTL_MODE_DESTROYPROPBLOB DRM_IOWR(0xBE, struct DrmModeDestroyBlob) |
+#endif |
+ |
+int CreatePropertyBlob(int fd, const void* data, size_t length, uint32_t* id) { |
+ DrmModeCreateBlob create; |
+ int ret; |
+ |
+ if (length >= 0xffffffff) |
+ return -ERANGE; |
+ |
+ memset(&create, 0, sizeof(create)); |
+ |
+ create.length = length; |
+ create.data = (uintptr_t)data; |
+ create.blob_id = 0; |
+ *id = 0; |
+ |
+ ret = drmIoctl(fd, DRM_IOCTL_MODE_CREATEPROPBLOB, &create); |
+ ret = ret < 0 ? -errno : ret; |
+ if (ret != 0) |
+ return ret; |
+ |
+ *id = create.blob_id; |
+ return 0; |
+} |
+ |
+int DestroyPropertyBlob(int fd, uint32_t id) { |
+ DrmModeDestroyBlob destroy; |
+ int ret; |
+ |
+ memset(&destroy, 0, sizeof(destroy)); |
+ destroy.blob_id = id; |
+ ret = drmIoctl(fd, DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy); |
+ return ret < 0 ? -errno : ret; |
+} |
+ |
+using ScopedDrmColorLutPtr = scoped_ptr<DrmColorLut, base::FreeDeleter>; |
+using ScopedDrmColorCtmPtr = scoped_ptr<DrmColorCtm, base::FreeDeleter>; |
+ |
+ScopedDrmColorLutPtr CreateLutBlob( |
+ const std::vector<GammaRampRGBEntry>& source) { |
+ TRACE_EVENT0("drm", "CreateLutBlob"); |
+ ScopedDrmColorLutPtr lut( |
+ static_cast<DrmColorLut*>(malloc(sizeof(DrmColorLut) * source.size()))); |
+ DrmColorLut* p = lut.get(); |
+ for (size_t i = 0; i < source.size(); ++i) { |
+ p[i].red = source[i].r; |
+ p[i].green = source[i].g; |
+ p[i].blue = source[i].b; |
+ } |
+ return lut; |
+} |
+ |
+ScopedDrmColorCtmPtr CreateCTMBlob( |
+ const std::vector<float>& correction_matrix) { |
+ ScopedDrmColorCtmPtr ctm( |
+ static_cast<DrmColorCtm*>(malloc(sizeof(DrmColorCtm)))); |
+ for (size_t i = 0; i < arraysize(ctm->ctm_coeff); ++i) { |
+ if (correction_matrix[i] < 0) { |
+ ctm->ctm_coeff[i] = static_cast<uint64_t>( |
+ -correction_matrix[i] * (static_cast<uint64_t>(1) << 32)); |
+ ctm->ctm_coeff[i] |= static_cast<uint64_t>(1) << 63; |
+ } else { |
+ ctm->ctm_coeff[i] = static_cast<uint64_t>( |
+ correction_matrix[i] * (static_cast<uint64_t>(1) << 32)); |
+ } |
+ } |
+ return ctm; |
+} |
+ |
+bool SetBlobProperty(int fd, |
+ uint32_t object_id, |
+ uint32_t object_type, |
+ uint32_t prop_id, |
+ const char* property_name, |
+ unsigned char* data, |
+ size_t length) { |
+ uint32_t blob_id; |
+ int res; |
+ res = CreatePropertyBlob(fd, data, length, &blob_id); |
+ if (res != 0) { |
+ LOG(ERROR) << "Error creating property blob: " << base::safe_strerror(res) |
+ << " for property " << property_name; |
+ return false; |
+ } |
+ res = drmModeObjectSetProperty(fd, object_id, object_type, prop_id, blob_id); |
+ if (res != 0) { |
+ LOG(ERROR) << "Error updating property: " << base::safe_strerror(res) |
+ << " for property " << property_name; |
+ DestroyPropertyBlob(fd, blob_id); |
+ return false; |
+ } |
+ DestroyPropertyBlob(fd, blob_id); |
+ return true; |
+} |
+ |
+std::vector<GammaRampRGBEntry> ResampleLut( |
+ const std::vector<GammaRampRGBEntry>& lut_in, |
+ size_t desired_size) { |
+ TRACE_EVENT1("drm", "ResampleLut", "desired_size", desired_size); |
+ if (lut_in.size() == desired_size) |
+ return lut_in; |
+ |
+ std::vector<GammaRampRGBEntry> result; |
+ result.resize(desired_size); |
+ |
+ for (size_t i = 0; i < desired_size; ++i) { |
+ size_t base_index = lut_in.size() * i / desired_size; |
+ size_t remaining = lut_in.size() * i % desired_size; |
+ if (base_index < lut_in.size() - 1) { |
+ result[i].r = lut_in[base_index].r + |
+ (lut_in[base_index + 1].r - lut_in[base_index].r) * |
+ remaining / desired_size; |
+ result[i].g = lut_in[base_index].g + |
+ (lut_in[base_index + 1].g - lut_in[base_index].g) * |
+ remaining / desired_size; |
+ result[i].b = lut_in[base_index].b + |
+ (lut_in[base_index + 1].b - lut_in[base_index].b) * |
+ remaining / desired_size; |
+ } else { |
+ result[i] = lut_in[lut_in.size() - 1]; |
+ } |
+ } |
+ |
+ return result; |
+} |
+ |
} // namespace |
class DrmDevice::PageFlipManager { |
@@ -542,4 +699,74 @@ bool DrmDevice::SetGammaRamp(uint32_t crtc_id, |
&g[0], &b[0]) == 0); |
} |
+bool DrmDevice::SetColorCorrection( |
+ uint32_t crtc_id, |
+ const std::vector<GammaRampRGBEntry>& degamma_lut, |
+ const std::vector<GammaRampRGBEntry>& gamma_lut, |
+ const std::vector<float>& correction_matrix) { |
+ ScopedDrmObjectPropertyPtr crtc_props(drmModeObjectGetProperties( |
+ file_.GetPlatformFile(), crtc_id, DRM_MODE_OBJECT_CRTC)); |
+ uint64_t degamma_lut_size = 0; |
+ uint64_t gamma_lut_size = 0; |
+ |
+ for (uint32_t i = 0; i < crtc_props->count_props; ++i) { |
+ ScopedDrmPropertyPtr property( |
+ drmModeGetProperty(file_.GetPlatformFile(), crtc_props->props[i])); |
+ if (property && !strcmp(property->name, "DEGAMMA_LUT_SIZE")) { |
+ degamma_lut_size = crtc_props->prop_values[i]; |
+ } |
+ if (property && !strcmp(property->name, "GAMMA_LUT_SIZE")) { |
+ gamma_lut_size = crtc_props->prop_values[i]; |
+ } |
+ |
+ if (degamma_lut_size && gamma_lut_size) |
+ break; |
+ } |
+ |
+ if (degamma_lut_size == 0 || gamma_lut_size == 0) { |
+ LOG(WARNING) << "No available (de)gamma tables."; |
+ return false; |
+ } |
+ |
+ ScopedDrmColorLutPtr degamma_blob_data = |
+ CreateLutBlob(ResampleLut(degamma_lut, degamma_lut_size)); |
+ ScopedDrmColorLutPtr gamma_blob_data = |
+ CreateLutBlob(ResampleLut(gamma_lut, gamma_lut_size)); |
+ ScopedDrmColorCtmPtr ctm_blob_data = CreateCTMBlob(correction_matrix); |
+ |
+ for (uint32_t i = 0; i < crtc_props->count_props; ++i) { |
+ ScopedDrmPropertyPtr property( |
+ drmModeGetProperty(file_.GetPlatformFile(), crtc_props->props[i])); |
+ if (!property) |
+ continue; |
+ |
+ if (!strcmp(property->name, "DEGAMMA_LUT")) { |
+ if (!SetBlobProperty( |
+ file_.GetPlatformFile(), crtc_id, DRM_MODE_OBJECT_CRTC, |
+ crtc_props->props[i], property->name, |
+ reinterpret_cast<unsigned char*>(degamma_blob_data.get()), |
+ sizeof(DrmColorLut) * degamma_lut_size)) |
+ return false; |
+ } |
+ if (!strcmp(property->name, "GAMMA_LUT")) { |
+ if (!SetBlobProperty( |
+ file_.GetPlatformFile(), crtc_id, DRM_MODE_OBJECT_CRTC, |
+ crtc_props->props[i], property->name, |
+ reinterpret_cast<unsigned char*>(gamma_blob_data.get()), |
+ sizeof(DrmColorLut) * gamma_lut_size)) |
+ return false; |
+ } |
+ if (!strcmp(property->name, "CTM")) { |
+ if (!SetBlobProperty( |
+ file_.GetPlatformFile(), crtc_id, DRM_MODE_OBJECT_CRTC, |
+ crtc_props->props[i], property->name, |
+ reinterpret_cast<unsigned char*>(ctm_blob_data.get()), |
+ sizeof(DrmColorCtm))) |
+ return false; |
+ } |
+ } |
+ |
+ return true; |
+} |
+ |
} // namespace ui |