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

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: Rebase after quirks changes Created 4 years, 8 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
« no previous file with comments | « ui/ozone/platform/drm/gpu/drm_device.h ('k') | ui/ozone/platform/drm/gpu/drm_display.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/memory/free_deleter.h"
16 #include "base/message_loop/message_loop.h" 17 #include "base/message_loop/message_loop.h"
18 #include "base/posix/safe_strerror.h"
17 #include "base/task_runner.h" 19 #include "base/task_runner.h"
18 #include "base/thread_task_runner_handle.h" 20 #include "base/thread_task_runner_handle.h"
19 #include "base/trace_event/trace_event.h" 21 #include "base/trace_event/trace_event.h"
20 #include "third_party/skia/include/core/SkImageInfo.h" 22 #include "third_party/skia/include/core/SkImageInfo.h"
21 #include "ui/display/types/gamma_ramp_rgb_entry.h" 23 #include "ui/display/types/gamma_ramp_rgb_entry.h"
22 #include "ui/ozone/platform/drm/common/drm_util.h" 24 #include "ui/ozone/platform/drm/common/drm_util.h"
23 #include "ui/ozone/platform/drm/gpu/hardware_display_plane_manager_legacy.h" 25 #include "ui/ozone/platform/drm/gpu/hardware_display_plane_manager_legacy.h"
24 26
25 #if defined(USE_DRM_ATOMIC) 27 #if defined(USE_DRM_ATOMIC)
26 #include "ui/ozone/platform/drm/gpu/hardware_display_plane_manager_atomic.h" 28 #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 } 108 }
107 109
108 bool CanQueryForResources(int fd) { 110 bool CanQueryForResources(int fd) {
109 drm_mode_card_res resources; 111 drm_mode_card_res resources;
110 memset(&resources, 0, sizeof(resources)); 112 memset(&resources, 0, sizeof(resources));
111 // If there is no error getting DRM resources then assume this is a 113 // If there is no error getting DRM resources then assume this is a
112 // modesetting device. 114 // modesetting device.
113 return !drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &resources); 115 return !drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &resources);
114 } 116 }
115 117
118 // TODO(robert.bradford): Replace with libdrm structures after libdrm roll.
119 // https://crbug.com/586475
120 struct DrmColorLut {
121 uint16_t red;
122 uint16_t green;
123 uint16_t blue;
124 uint16_t reserved;
125 };
126
127 struct DrmColorCtm {
128 int64_t ctm_coeff[9];
129 };
130
131 struct DrmModeCreateBlob {
132 uint64_t data;
133 uint32_t length;
134 uint32_t blob_id;
135 };
136
137 struct DrmModeDestroyBlob {
138 uint32_t blob_id;
139 };
140
141 #ifndef DRM_IOCTL_MODE_CREATEPROPBLOB
142 #define DRM_IOCTL_MODE_CREATEPROPBLOB DRM_IOWR(0xBD, struct DrmModeCreateBlob)
143 #endif
144
145 #ifndef DRM_IOCTL_MODE_DESTROYPROPBLOB
146 #define DRM_IOCTL_MODE_DESTROYPROPBLOB DRM_IOWR(0xBE, struct DrmModeDestroyBlob)
147 #endif
148
149 int CreatePropertyBlob(int fd, const void* data, size_t length, uint32_t* id) {
150 DrmModeCreateBlob create;
151 int ret;
152
153 if (length >= 0xffffffff)
154 return -ERANGE;
155
156 memset(&create, 0, sizeof(create));
157
158 create.length = length;
159 create.data = (uintptr_t)data;
160 create.blob_id = 0;
161 *id = 0;
162
163 ret = drmIoctl(fd, DRM_IOCTL_MODE_CREATEPROPBLOB, &create);
164 ret = ret < 0 ? -errno : ret;
165 if (ret != 0)
166 return ret;
167
168 *id = create.blob_id;
169 return 0;
170 }
171
172 int DestroyPropertyBlob(int fd, uint32_t id) {
173 DrmModeDestroyBlob destroy;
174 int ret;
175
176 memset(&destroy, 0, sizeof(destroy));
177 destroy.blob_id = id;
178 ret = drmIoctl(fd, DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy);
179 return ret < 0 ? -errno : ret;
180 }
181
182 using ScopedDrmColorLutPtr = scoped_ptr<DrmColorLut, base::FreeDeleter>;
183 using ScopedDrmColorCtmPtr = scoped_ptr<DrmColorCtm, base::FreeDeleter>;
184
185 ScopedDrmColorLutPtr CreateLutBlob(
186 const std::vector<GammaRampRGBEntry>& source) {
187 TRACE_EVENT0("drm", "CreateLutBlob");
188 ScopedDrmColorLutPtr lut(
189 static_cast<DrmColorLut*>(malloc(sizeof(DrmColorLut) * source.size())));
190 DrmColorLut* p = lut.get();
191 for (size_t i = 0; i < source.size(); ++i) {
192 p[i].red = source[i].r;
193 p[i].green = source[i].g;
194 p[i].blue = source[i].b;
195 }
196 return lut;
197 }
198
199 ScopedDrmColorCtmPtr CreateCTMBlob(
200 const std::vector<float>& correction_matrix) {
201 ScopedDrmColorCtmPtr ctm(
202 static_cast<DrmColorCtm*>(malloc(sizeof(DrmColorCtm))));
203 for (size_t i = 0; i < arraysize(ctm->ctm_coeff); ++i) {
204 if (correction_matrix[i] < 0) {
205 ctm->ctm_coeff[i] = static_cast<uint64_t>(
206 -correction_matrix[i] * (static_cast<uint64_t>(1) << 32));
207 ctm->ctm_coeff[i] |= static_cast<uint64_t>(1) << 63;
208 } else {
209 ctm->ctm_coeff[i] = static_cast<uint64_t>(
210 correction_matrix[i] * (static_cast<uint64_t>(1) << 32));
211 }
212 }
213 return ctm;
214 }
215
216 bool SetBlobProperty(int fd,
217 uint32_t object_id,
218 uint32_t object_type,
219 uint32_t prop_id,
220 const char* property_name,
221 unsigned char* data,
222 size_t length) {
223 uint32_t blob_id;
224 int res;
225 res = CreatePropertyBlob(fd, data, length, &blob_id);
226 if (res != 0) {
227 LOG(ERROR) << "Error creating property blob: " << base::safe_strerror(res)
228 << " for property " << property_name;
229 return false;
230 }
231 res = drmModeObjectSetProperty(fd, object_id, object_type, prop_id, blob_id);
232 if (res != 0) {
233 LOG(ERROR) << "Error updating property: " << base::safe_strerror(res)
234 << " for property " << property_name;
235 DestroyPropertyBlob(fd, blob_id);
236 return false;
237 }
238 DestroyPropertyBlob(fd, blob_id);
239 return true;
240 }
241
242 std::vector<GammaRampRGBEntry> ResampleLut(
243 const std::vector<GammaRampRGBEntry>& lut_in,
244 size_t desired_size) {
245 TRACE_EVENT1("drm", "ResampleLut", "desired_size", desired_size);
246 if (lut_in.size() == desired_size)
247 return lut_in;
248
249 std::vector<GammaRampRGBEntry> result;
250 result.resize(desired_size);
251
252 for (size_t i = 0; i < desired_size; ++i) {
253 size_t base_index = lut_in.size() * i / desired_size;
254 size_t remaining = lut_in.size() * i % desired_size;
255 if (base_index < lut_in.size() - 1) {
256 result[i].r = lut_in[base_index].r +
257 (lut_in[base_index + 1].r - lut_in[base_index].r) *
258 remaining / desired_size;
259 result[i].g = lut_in[base_index].g +
260 (lut_in[base_index + 1].g - lut_in[base_index].g) *
261 remaining / desired_size;
262 result[i].b = lut_in[base_index].b +
263 (lut_in[base_index + 1].b - lut_in[base_index].b) *
264 remaining / desired_size;
265 } else {
266 result[i] = lut_in[lut_in.size() - 1];
267 }
268 }
269
270 return result;
271 }
272
116 } // namespace 273 } // namespace
117 274
118 class DrmDevice::PageFlipManager { 275 class DrmDevice::PageFlipManager {
119 public: 276 public:
120 PageFlipManager() : next_id_(0) {} 277 PageFlipManager() : next_id_(0) {}
121 ~PageFlipManager() {} 278 ~PageFlipManager() {}
122 279
123 void OnPageFlip(uint32_t frame, 280 void OnPageFlip(uint32_t frame,
124 uint32_t seconds, 281 uint32_t seconds,
125 uint32_t useconds, 282 uint32_t useconds,
(...skipping 409 matching lines...) Expand 10 before | Expand all | Expand 10 after
535 g.push_back(lut[i].g); 692 g.push_back(lut[i].g);
536 b.push_back(lut[i].b); 693 b.push_back(lut[i].b);
537 } 694 }
538 695
539 DCHECK(file_.IsValid()); 696 DCHECK(file_.IsValid());
540 TRACE_EVENT0("drm", "DrmDevice::SetGamma"); 697 TRACE_EVENT0("drm", "DrmDevice::SetGamma");
541 return (drmModeCrtcSetGamma(file_.GetPlatformFile(), crtc_id, r.size(), &r[0], 698 return (drmModeCrtcSetGamma(file_.GetPlatformFile(), crtc_id, r.size(), &r[0],
542 &g[0], &b[0]) == 0); 699 &g[0], &b[0]) == 0);
543 } 700 }
544 701
702 bool DrmDevice::SetColorCorrection(
703 uint32_t crtc_id,
704 const std::vector<GammaRampRGBEntry>& degamma_lut,
705 const std::vector<GammaRampRGBEntry>& gamma_lut,
706 const std::vector<float>& correction_matrix) {
707 ScopedDrmObjectPropertyPtr crtc_props(drmModeObjectGetProperties(
708 file_.GetPlatformFile(), crtc_id, DRM_MODE_OBJECT_CRTC));
709 uint64_t degamma_lut_size = 0;
710 uint64_t gamma_lut_size = 0;
711
712 for (uint32_t i = 0; i < crtc_props->count_props; ++i) {
713 ScopedDrmPropertyPtr property(
714 drmModeGetProperty(file_.GetPlatformFile(), crtc_props->props[i]));
715 if (property && !strcmp(property->name, "DEGAMMA_LUT_SIZE")) {
716 degamma_lut_size = crtc_props->prop_values[i];
717 }
718 if (property && !strcmp(property->name, "GAMMA_LUT_SIZE")) {
719 gamma_lut_size = crtc_props->prop_values[i];
720 }
721
722 if (degamma_lut_size && gamma_lut_size)
723 break;
724 }
725
726 if (degamma_lut_size == 0 || gamma_lut_size == 0) {
727 LOG(WARNING) << "No available (de)gamma tables.";
728 return false;
729 }
730
731 ScopedDrmColorLutPtr degamma_blob_data =
732 CreateLutBlob(ResampleLut(degamma_lut, degamma_lut_size));
733 ScopedDrmColorLutPtr gamma_blob_data =
734 CreateLutBlob(ResampleLut(gamma_lut, gamma_lut_size));
735 ScopedDrmColorCtmPtr ctm_blob_data = CreateCTMBlob(correction_matrix);
736
737 for (uint32_t i = 0; i < crtc_props->count_props; ++i) {
738 ScopedDrmPropertyPtr property(
739 drmModeGetProperty(file_.GetPlatformFile(), crtc_props->props[i]));
740 if (!property)
741 continue;
742
743 if (!strcmp(property->name, "DEGAMMA_LUT")) {
744 if (!SetBlobProperty(
745 file_.GetPlatformFile(), crtc_id, DRM_MODE_OBJECT_CRTC,
746 crtc_props->props[i], property->name,
747 reinterpret_cast<unsigned char*>(degamma_blob_data.get()),
748 sizeof(DrmColorLut) * degamma_lut_size))
749 return false;
750 }
751 if (!strcmp(property->name, "GAMMA_LUT")) {
752 if (!SetBlobProperty(
753 file_.GetPlatformFile(), crtc_id, DRM_MODE_OBJECT_CRTC,
754 crtc_props->props[i], property->name,
755 reinterpret_cast<unsigned char*>(gamma_blob_data.get()),
756 sizeof(DrmColorLut) * gamma_lut_size))
757 return false;
758 }
759 if (!strcmp(property->name, "CTM")) {
760 if (!SetBlobProperty(
761 file_.GetPlatformFile(), crtc_id, DRM_MODE_OBJECT_CRTC,
762 crtc_props->props[i], property->name,
763 reinterpret_cast<unsigned char*>(ctm_blob_data.get()),
764 sizeof(DrmColorCtm)))
765 return false;
766 }
767 }
768
769 return true;
770 }
771
545 } // namespace ui 772 } // namespace ui
OLDNEW
« no previous file with comments | « ui/ozone/platform/drm/gpu/drm_device.h ('k') | ui/ozone/platform/drm/gpu/drm_display.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698