Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(185)

Unified Diff: ui/ozone/platform/drm/gpu/drm_device.cc

Issue 1182063002: Add support for more advanced color correction (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@qcms-fixed-point-gamma
Patch Set: Integrate dnicoara@ feedback Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..7e5f4c1d8ef6467ae73a84e5d1fc5ffc77f441ca 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);
}
+// 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];
+};
+
+std::vector<uint8_t> CreateLutBlob(
+ const std::vector<GammaRampRGBEntry>& source) {
+ TRACE_EVENT0("drm", "CreateLutBlob");
+ std::vector<uint8_t> data;
+
+ data.resize(source.size() * sizeof(DrmColorLut));
+
+ DrmColorLut* lut = reinterpret_cast<DrmColorLut*>(data.data());
spang 2016/02/22 18:57:50 Please use a vector<DrmColorLut> and cast to unsig
robert.bradford 2016/02/23 19:23:01 Ack. Replaced with a scoped_ptr w/ base::FreeDelet
+
+ 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(DrmColorCtm));
+ DrmColorCtm* ctm = reinterpret_cast<DrmColorCtm*>(data.data());
spang 2016/02/22 18:57:50 Same here, it's not OK to cast unsigned char* to a
robert.bradford 2016/02/23 19:23:01 Done. The cast is migrated now only to the SetBlob
+
+ 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) {
spang 2016/02/22 18:57:50 const &
robert.bradford 2016/02/23 19:23:01 Replaced with a raw pointer now using scoped_ptr a
+ uint32_t blob_id;
+ int res;
+ res = drmModeCreatePropertyBlob(fd, data.data(), data.size(), &blob_id);
+ if (res != 0) {
+ LOG(ERROR) << "Error creating property blob: " << base::safe_strerror(res);
spang 2016/02/22 18:57:50 PLOG(ERROR)
spang 2016/02/22 19:01:46 Nevermind that's for strerror(errno).
+ 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);
spang 2016/02/22 18:57:50 PLOG(ERROR)
+ drmModeDestroyPropertyBlob(fd, blob_id);
+ return false;
+ }
+ drmModeDestroyPropertyBlob(fd, blob_id);
spang 2016/02/22 18:57:50 I don't understand the lifetime of the blob. Who's
robert.bradford 2016/02/23 19:23:01 I was using the stack allocation of the std::vecto
+ 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,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.";
spang 2016/02/22 18:57:50 Will we hit this if color correction isn't support
robert.bradford 2016/02/23 19:23:01 This is protection against a broken kernel impleme
+ 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)
+ continue;
+
+ if (!strcmp(property->name, "DEGAMMA_LUT")) {
+ if (!SetBlobProperty(file_.GetPlatformFile(), crtc_id,
+ DRM_MODE_OBJECT_CRTC, crtc_props->props[i],
+ degamma_blob_data)) {
+ LOG(WARNING) << "Error setting degamma property.";
spang 2016/02/22 18:57:50 Can you pass the property name into SetBlobPropert
robert.bradford 2016/02/23 19:23:01 Done.
+ return false;
+ }
+ }
+ if (!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 (!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

Powered by Google App Engine
This is Rietveld 408576698