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 ad4979c45cb28bd17cfa94cd80a9d8ee8e9656e5..d6a5b697897f5234f549722dd7002a6a23b6388c 100644 |
| --- a/ui/ozone/platform/drm/gpu/drm_device.cc |
| +++ b/ui/ozone/platform/drm/gpu/drm_device.cc |
| @@ -12,6 +12,7 @@ |
| #include "base/logging.h" |
| #include "base/message_loop/message_loop.h" |
| +#include "base/posix/safe_strerror.h" |
| #include "base/stl_util.h" |
| #include "base/task_runner.h" |
| #include "base/thread_task_runner_handle.h" |
| @@ -112,6 +113,123 @@ bool CanQueryForResources(int fd) { |
| return !drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &resources); |
| } |
| +#ifndef drm_r32g32b32 |
| +struct drm_r32g32b32 { |
| + /* |
| + * Data is in U8.24 fixed point format. |
| + * All platforms support values within [0, 1.0] range, |
| + * for Red, Green and Blue colors. |
| + */ |
| + __u32 r32; |
| + __u32 g32; |
| + __u32 b32; |
| + __u32 reserved; |
| +}; |
| +#endif // drm_r32g32b32 |
| + |
| +#ifndef drm_palette |
| +struct drm_palette { |
| + /* |
| + * Starting of palette LUT in R32G32B32 format. |
| + * Each of RGB value is in U8.24 fixed point format. |
| + */ |
| + struct drm_r32g32b32 lut[0]; |
| +}; |
| +#endif // drm_palette |
| + |
| +#ifndef drm_ctm |
| +struct drm_ctm { |
| + /* |
| + * Each value is in S31.32 format. |
| + * This is 3x3 matrix in row major format. |
| + * Integer part will be clipped to nearest |
| + * max/min boundary as supported by the HW platform. |
| + */ |
| + __s64 ctm_coeff[9]; |
| +}; |
| +#endif // drm_ctm |
| + |
| +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_r32g32b32)); |
| + |
| + struct drm_palette* palette = reinterpret_cast<struct drm_palette*>(&data[0]); |
| + |
| + for (size_t i = 0; i < source.size(); ++i) { |
| + palette->lut[i].r32 = source[i].r; |
| + palette->lut[i].g32 = source[i].g; |
| + palette->lut[i].b32 = source[i].b; |
| + } |
| + return data; |
| +} |
| + |
| +std::vector<uint8_t> CreateCTMBlob(const float correction_matrix[9]) { |
| + std::vector<uint8_t> data; |
| + data.resize(sizeof(struct drm_ctm)); |
| + struct drm_ctm* ctm = reinterpret_cast<struct drm_ctm*>(&data[0]); |
| + |
| + for (int i = 0; i < 9; ++i) { |
| + 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); |
| + 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); |
| + |
| +#define RESAMPLE(array, m, i, r) \ |
|
spang
2015/11/09 19:40:47
Please put a function in the anonymous namespace i
robert.bradford
2016/02/04 18:52:28
Ended up with it inline which worked better than a
|
| + array[i].m + (array[i + 1].m - array[i].m) * r / 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 = RESAMPLE(lut_in, r, base_index, remaining); |
| + result[i].g = RESAMPLE(lut_in, g, base_index, remaining); |
| + result[i].b = RESAMPLE(lut_in, b, base_index, remaining); |
| + } else { |
| + result[i] = lut_in[lut_in.size() - 1]; |
| + } |
| + } |
| + |
| +#undef RESAMPLE |
| + return result; |
| +} |
| + |
| } // namespace |
| class DrmDevice::PageFlipManager { |
| @@ -541,4 +659,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 float correction_matrix[9]) { |
| + 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, "COEFFICIENTS_BEFORE_CTM")) { |
| + degamma_lut_size = crtc_props->prop_values[i]; |
| + } |
| + if (property && !strcmp(property->name, "COEFFICIENTS_AFTER_CTM")) { |
| + 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, "PALETTE_BEFORE_CTM")) { |
| + 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, "PALETTE_AFTER_CTM")) { |
| + 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")) { |
| + 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 |