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..b57905fabfd9975348ff1bf376746d24e4bf155d 100644 |
--- a/ui/ozone/platform/drm/gpu/drm_device.cc |
+++ b/ui/ozone/platform/drm/gpu/drm_device.cc |
@@ -14,6 +14,7 @@ |
#include "base/logging.h" |
#include "base/macros.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 +114,109 @@ bool CanQueryForResources(int fd) { |
return !drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &resources); |
} |
+struct _drm_color_lut { |
dnicoara
2016/02/10 20:33:31
structs should follow the Chromium naming conventi
robert.bradford
2016/02/12 14:55:49
Yes ultimately but there is delay in getting chang
|
+ __u16 red; |
dnicoara
2016/02/10 20:33:31
Use uint16_t
robert.bradford
2016/02/12 14:55:49
Done.
|
+ __u16 green; |
+ __u16 blue; |
+ __u16 reserved; |
+}; |
+ |
+struct _drm_color_ctm { |
dnicoara
2016/02/10 20:33:31
Same: DrmColorCtm
robert.bradford
2016/02/12 14:55:49
Done.
|
+ __s64 ctm_coeff[9]; |
dnicoara
2016/02/10 20:33:31
Use int64_t.
robert.bradford
2016/02/12 14:55:50
Done.
|
+}; |
+ |
+std::vector<uint8_t> CreateLutBlob( |
+ const std::vector<GammaRampRGBEntry>& source) { |
+ TRACE_EVENT0("drm", "CreateLutBlob"); |
+ std::vector<uint8_t> data; |
+ |
+ data.resize(source.size() * sizeof(struct _drm_color_lut)); |
+ |
+ struct _drm_color_lut* lut = |
+ reinterpret_cast<struct _drm_color_lut*>(&data[0]); |
+ |
+ for (size_t i = 0; i < source.size(); ++i) { |
+ lut[i].red = source[i].r; |
+ lut[i].green = source[i].g; |
+ lut[i].blue = source[i].b; |
+ } |
+ return data; |
+} |
+ |
+std::vector<uint8_t> CreateCTMBlob( |
+ const std::vector<float>& correction_matrix) { |
+ std::vector<uint8_t> data; |
+ data.resize(sizeof(struct _drm_color_ctm)); |
+ struct _drm_color_ctm* ctm = |
+ reinterpret_cast<struct _drm_color_ctm*>(&data[0]); |
+ |
+ for (int i = 0; i < 9; ++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 data; |
+} |
+ |
+bool SetBlobProperty(int fd, |
+ uint32_t object_id, |
+ uint32_t object_type, |
+ uint32_t prop_id, |
+ std::vector<uint8_t> data) { |
+ uint32_t blob_id; |
+ int res; |
+ res = drmModeCreatePropertyBlob(fd, &data[0], data.size(), &blob_id); |
dnicoara
2016/02/10 20:33:31
nit: Use vector_as_array(&data) from base/stl_util
dnicoara
2016/02/11 14:57:31
Actually, looks like this got deprecated in favor
robert.bradford
2016/02/12 14:55:49
Done.
|
+ if (res != 0) { |
+ LOG(ERROR) << "Error creating property blob: " << base::safe_strerror(res); |
+ 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); |
+ drmModeDestroyPropertyBlob(fd, blob_id); |
+ return false; |
+ } |
+ drmModeDestroyPropertyBlob(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 +646,71 @@ 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; |
+ } |
+ |
+ std::vector<uint8_t> degamma_blob_data = |
+ CreateLutBlob(ResampleLut(degamma_lut, degamma_lut_size)); |
+ std::vector<uint8_t> gamma_blob_data = |
+ CreateLutBlob(ResampleLut(gamma_lut, gamma_lut_size)); |
+ std::vector<uint8_t> 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 && !strcmp(property->name, "DEGAMMA_LUT")) { |
dnicoara
2016/02/10 20:33:31
nit: Maybe check the property is valid once. Somet
robert.bradford
2016/02/12 14:55:49
Done.
|
+ if (!SetBlobProperty(file_.GetPlatformFile(), crtc_id, |
+ DRM_MODE_OBJECT_CRTC, crtc_props->props[i], |
+ degamma_blob_data)) { |
+ LOG(WARNING) << "Error setting degamma property."; |
+ return false; |
+ } |
+ } |
+ if (property && !strcmp(property->name, "GAMMA_LUT")) { |
+ if (!SetBlobProperty(file_.GetPlatformFile(), crtc_id, |
+ DRM_MODE_OBJECT_CRTC, crtc_props->props[i], |
+ gamma_blob_data)) { |
+ LOG(WARNING) << "Error setting gamma property."; |
+ return false; |
+ } |
+ } |
+ if (property && !strcmp(property->name, "CTM_MATRIX")) { |
+ if (!SetBlobProperty(file_.GetPlatformFile(), crtc_id, |
+ DRM_MODE_OBJECT_CRTC, crtc_props->props[i], |
+ ctm_blob_data)) { |
+ LOG(WARNING) << "Error setting correction matrix property."; |
+ return false; |
+ } |
+ } |
+ } |
+ |
+ return true; |
+} |
+ |
} // namespace ui |