Chromium Code Reviews| 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..9c2282bcd8fa7c241ab9c4f06188e0045716a5a2 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,104 @@ 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]; |
| +}; |
| + |
| +DrmColorLut* CreateLutBlob(const std::vector<GammaRampRGBEntry>& source) { |
|
dcheng
2016/02/23 20:01:55
Return a scoped_ptr here and below.
robert.bradford
2016/02/24 12:34:20
Done.
|
| + TRACE_EVENT0("drm", "CreateLutBlob"); |
| + |
| + DrmColorLut* lut = new DrmColorLut[source.size()]; |
| + 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 lut; |
| +} |
| + |
| +DrmColorCtm* CreateCTMBlob(const std::vector<float>& correction_matrix) { |
| + DrmColorCtm* ctm = new DrmColorCtm; |
| + |
| + 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 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 = drmModeCreatePropertyBlob(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; |
| + 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 +641,75 @@ 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; |
| + } |
| + |
| + scoped_ptr<DrmColorLut, base::FreeDeleter> degamma_blob_data( |
| + CreateLutBlob(ResampleLut(degamma_lut, degamma_lut_size))); |
| + scoped_ptr<DrmColorLut, base::FreeDeleter> gamma_blob_data( |
| + CreateLutBlob(ResampleLut(gamma_lut, gamma_lut_size))); |
| + scoped_ptr<DrmColorCtm, base::FreeDeleter> 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")) { |
|
robert.bradford
2016/02/23 19:24:21
CTM_MATRIX -> CTM in kernel API.
|
| + 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 |