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

Side by Side 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: Improve memory management / casting 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 unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/ozone/platform/drm/gpu/drm_device.h" 5 #include "ui/ozone/platform/drm/gpu/drm_device.h"
6 6
7 #include <fcntl.h> 7 #include <fcntl.h>
8 #include <sys/mman.h> 8 #include <sys/mman.h>
9 #include <unistd.h> 9 #include <unistd.h>
10 #include <xf86drm.h> 10 #include <xf86drm.h>
11 #include <xf86drmMode.h> 11 #include <xf86drmMode.h>
12 #include <utility> 12 #include <utility>
13 13
14 #include "base/logging.h" 14 #include "base/logging.h"
15 #include "base/macros.h" 15 #include "base/macros.h"
16 #include "base/message_loop/message_loop.h" 16 #include "base/message_loop/message_loop.h"
17 #include "base/posix/safe_strerror.h"
17 #include "base/task_runner.h" 18 #include "base/task_runner.h"
18 #include "base/thread_task_runner_handle.h" 19 #include "base/thread_task_runner_handle.h"
19 #include "base/trace_event/trace_event.h" 20 #include "base/trace_event/trace_event.h"
20 #include "third_party/skia/include/core/SkImageInfo.h" 21 #include "third_party/skia/include/core/SkImageInfo.h"
21 #include "ui/display/types/gamma_ramp_rgb_entry.h" 22 #include "ui/display/types/gamma_ramp_rgb_entry.h"
22 #include "ui/ozone/platform/drm/common/drm_util.h" 23 #include "ui/ozone/platform/drm/common/drm_util.h"
23 #include "ui/ozone/platform/drm/gpu/hardware_display_plane_manager_legacy.h" 24 #include "ui/ozone/platform/drm/gpu/hardware_display_plane_manager_legacy.h"
24 25
25 #if defined(USE_DRM_ATOMIC) 26 #if defined(USE_DRM_ATOMIC)
26 #include "ui/ozone/platform/drm/gpu/hardware_display_plane_manager_atomic.h" 27 #include "ui/ozone/platform/drm/gpu/hardware_display_plane_manager_atomic.h"
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 } 107 }
107 108
108 bool CanQueryForResources(int fd) { 109 bool CanQueryForResources(int fd) {
109 drm_mode_card_res resources; 110 drm_mode_card_res resources;
110 memset(&resources, 0, sizeof(resources)); 111 memset(&resources, 0, sizeof(resources));
111 // If there is no error getting DRM resources then assume this is a 112 // If there is no error getting DRM resources then assume this is a
112 // modesetting device. 113 // modesetting device.
113 return !drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &resources); 114 return !drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &resources);
114 } 115 }
115 116
117 // TODO(robert.bradford): Replace with libdrm structures after libdrm roll.
118 // https://crbug.com/586475
119 struct DrmColorLut {
120 uint16_t red;
121 uint16_t green;
122 uint16_t blue;
123 uint16_t reserved;
124 };
125
126 struct DrmColorCtm {
127 int64_t ctm_coeff[9];
128 };
129
130 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.
131 TRACE_EVENT0("drm", "CreateLutBlob");
132
133 DrmColorLut* lut = new DrmColorLut[source.size()];
134 for (size_t i = 0; i < source.size(); ++i) {
135 lut[i].red = source[i].r;
136 lut[i].green = source[i].g;
137 lut[i].blue = source[i].b;
138 }
139 return lut;
140 }
141
142 DrmColorCtm* CreateCTMBlob(const std::vector<float>& correction_matrix) {
143 DrmColorCtm* ctm = new DrmColorCtm;
144
145 for (int i = 0; i < 9; ++i) {
146 if (correction_matrix[i] < 0) {
147 ctm->ctm_coeff[i] = static_cast<uint64_t>(
148 -correction_matrix[i] * (static_cast<uint64_t>(1) << 32));
149 ctm->ctm_coeff[i] |= static_cast<uint64_t>(1) << 63;
150 } else {
151 ctm->ctm_coeff[i] = static_cast<uint64_t>(
152 correction_matrix[i] * (static_cast<uint64_t>(1) << 32));
153 }
154 }
155 return ctm;
156 }
157
158 bool SetBlobProperty(int fd,
159 uint32_t object_id,
160 uint32_t object_type,
161 uint32_t prop_id,
162 const char* property_name,
163 unsigned char* data,
164 size_t length) {
165 uint32_t blob_id;
166 int res;
167 res = drmModeCreatePropertyBlob(fd, data, length, &blob_id);
168 if (res != 0) {
169 LOG(ERROR) << "Error creating property blob: " << base::safe_strerror(res)
170 << " for property " << property_name;
171 return false;
172 }
173 res = drmModeObjectSetProperty(fd, object_id, object_type, prop_id, blob_id);
174 if (res != 0) {
175 LOG(ERROR) << "Error updating property: " << base::safe_strerror(res)
176 << " for property " << property_name;
177 drmModeDestroyPropertyBlob(fd, blob_id);
178 return false;
179 }
180 drmModeDestroyPropertyBlob(fd, blob_id);
181 return true;
182 }
183
184 std::vector<GammaRampRGBEntry> ResampleLut(
185 const std::vector<GammaRampRGBEntry>& lut_in,
186 size_t desired_size) {
187 TRACE_EVENT1("drm", "ResampleLut", "desired_size", desired_size);
188 if (lut_in.size() == desired_size)
189 return lut_in;
190
191 std::vector<GammaRampRGBEntry> result;
192 result.resize(desired_size);
193
194 for (size_t i = 0; i < desired_size; ++i) {
195 size_t base_index = lut_in.size() * i / desired_size;
196 size_t remaining = lut_in.size() * i % desired_size;
197 if (base_index < lut_in.size() - 1) {
198 result[i].r = lut_in[base_index].r +
199 (lut_in[base_index + 1].r - lut_in[base_index].r) *
200 remaining / desired_size;
201 result[i].g = lut_in[base_index].g +
202 (lut_in[base_index + 1].g - lut_in[base_index].g) *
203 remaining / desired_size;
204 result[i].b = lut_in[base_index].b +
205 (lut_in[base_index + 1].b - lut_in[base_index].b) *
206 remaining / desired_size;
207 } else {
208 result[i] = lut_in[lut_in.size() - 1];
209 }
210 }
211
212 return result;
213 }
214
116 } // namespace 215 } // namespace
117 216
118 class DrmDevice::PageFlipManager { 217 class DrmDevice::PageFlipManager {
119 public: 218 public:
120 PageFlipManager() : next_id_(0) {} 219 PageFlipManager() : next_id_(0) {}
121 ~PageFlipManager() {} 220 ~PageFlipManager() {}
122 221
123 void OnPageFlip(uint32_t frame, 222 void OnPageFlip(uint32_t frame,
124 uint32_t seconds, 223 uint32_t seconds,
125 uint32_t useconds, 224 uint32_t useconds,
(...skipping 409 matching lines...) Expand 10 before | Expand all | Expand 10 after
535 g.push_back(lut[i].g); 634 g.push_back(lut[i].g);
536 b.push_back(lut[i].b); 635 b.push_back(lut[i].b);
537 } 636 }
538 637
539 DCHECK(file_.IsValid()); 638 DCHECK(file_.IsValid());
540 TRACE_EVENT0("drm", "DrmDevice::SetGamma"); 639 TRACE_EVENT0("drm", "DrmDevice::SetGamma");
541 return (drmModeCrtcSetGamma(file_.GetPlatformFile(), crtc_id, r.size(), &r[0], 640 return (drmModeCrtcSetGamma(file_.GetPlatformFile(), crtc_id, r.size(), &r[0],
542 &g[0], &b[0]) == 0); 641 &g[0], &b[0]) == 0);
543 } 642 }
544 643
644 bool DrmDevice::SetColorCorrection(
645 uint32_t crtc_id,
646 const std::vector<GammaRampRGBEntry>& degamma_lut,
647 const std::vector<GammaRampRGBEntry>& gamma_lut,
648 const std::vector<float>& correction_matrix) {
649 ScopedDrmObjectPropertyPtr crtc_props(drmModeObjectGetProperties(
650 file_.GetPlatformFile(), crtc_id, DRM_MODE_OBJECT_CRTC));
651 uint64_t degamma_lut_size = 0;
652 uint64_t gamma_lut_size = 0;
653
654 for (uint32_t i = 0; i < crtc_props->count_props; ++i) {
655 ScopedDrmPropertyPtr property(
656 drmModeGetProperty(file_.GetPlatformFile(), crtc_props->props[i]));
657 if (property && !strcmp(property->name, "DEGAMMA_LUT_SIZE")) {
658 degamma_lut_size = crtc_props->prop_values[i];
659 }
660 if (property && !strcmp(property->name, "GAMMA_LUT_SIZE")) {
661 gamma_lut_size = crtc_props->prop_values[i];
662 }
663
664 if (degamma_lut_size && gamma_lut_size)
665 break;
666 }
667
668 if (degamma_lut_size == 0 || gamma_lut_size == 0) {
669 LOG(WARNING) << "No available (de)gamma tables.";
670 return false;
671 }
672
673 scoped_ptr<DrmColorLut, base::FreeDeleter> degamma_blob_data(
674 CreateLutBlob(ResampleLut(degamma_lut, degamma_lut_size)));
675 scoped_ptr<DrmColorLut, base::FreeDeleter> gamma_blob_data(
676 CreateLutBlob(ResampleLut(gamma_lut, gamma_lut_size)));
677 scoped_ptr<DrmColorCtm, base::FreeDeleter> ctm_blob_data(
678 CreateCTMBlob(correction_matrix));
679
680 for (uint32_t i = 0; i < crtc_props->count_props; ++i) {
681 ScopedDrmPropertyPtr property(
682 drmModeGetProperty(file_.GetPlatformFile(), crtc_props->props[i]));
683 if (!property)
684 continue;
685
686 if (!strcmp(property->name, "DEGAMMA_LUT")) {
687 if (!SetBlobProperty(
688 file_.GetPlatformFile(), crtc_id, DRM_MODE_OBJECT_CRTC,
689 crtc_props->props[i], property->name,
690 reinterpret_cast<unsigned char*>(degamma_blob_data.get()),
691 sizeof(DrmColorLut) * degamma_lut_size))
692 return false;
693 }
694 if (!strcmp(property->name, "GAMMA_LUT")) {
695 if (!SetBlobProperty(
696 file_.GetPlatformFile(), crtc_id, DRM_MODE_OBJECT_CRTC,
697 crtc_props->props[i], property->name,
698 reinterpret_cast<unsigned char*>(gamma_blob_data.get()),
699 sizeof(DrmColorLut) * gamma_lut_size))
700 return false;
701 }
702 if (!strcmp(property->name, "CTM")) {
robert.bradford 2016/02/23 19:24:21 CTM_MATRIX -> CTM in kernel API.
703 if (!SetBlobProperty(
704 file_.GetPlatformFile(), crtc_id, DRM_MODE_OBJECT_CRTC,
705 crtc_props->props[i], property->name,
706 reinterpret_cast<unsigned char*>(ctm_blob_data.get()),
707 sizeof(DrmColorCtm)))
708 return false;
709 }
710 }
711
712 return true;
713 }
714
545 } // namespace ui 715 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698