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

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: "reimplement" drmMode{Create,Destroy}PropertyBlob to fix build Created 4 years, 9 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/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 struct _drm_mode_create_blob {
spang 2016/03/23 16:19:37 Follow our naming style. DrmModeCreateBlob
robert.bradford 2016/03/23 20:52:33 Done.
131 uint64_t data;
132 uint32_t length;
133 uint32_t blob_id;
134 };
135
136 struct _drm_mode_destroy_blob {
spang 2016/03/23 16:19:37 DrmModeDestroyBlob
robert.bradford 2016/03/23 20:52:33 Done.
137 uint32_t blob_id;
138 };
139
140 #define _DRM_IOCTL_MODE_CREATEPROPBLOB \
spang 2016/03/23 16:19:37 Should be able to just #ifdef these.
robert.bradford 2016/03/23 20:52:33 Done.
141 DRM_IOWR(0xBD, struct _drm_mode_create_blob)
142 #define _DRM_IOCTL_MODE_DESTROYPROPBLOB \
143 DRM_IOWR(0xBE, struct _drm_mode_destroy_blob)
144
145 int _drmModeCreatePropertyBlob(int fd,
spang 2016/03/23 16:19:37 CreatePropertyBlob()
robert.bradford 2016/03/23 20:52:33 Done.
146 const void* data,
147 size_t length,
148 uint32_t* id) {
149 struct _drm_mode_create_blob create;
150 int ret;
151
152 if (length >= 0xffffffff)
153 return -ERANGE;
154
155 memset(&create, 0, sizeof(create));
156
157 create.length = length;
158 create.data = (uintptr_t)data;
159 create.blob_id = 0;
160 *id = 0;
161
162 ret = drmIoctl(fd, _DRM_IOCTL_MODE_CREATEPROPBLOB, &create);
163 ret = ret < 0 ? -errno : ret;
164 if (ret != 0)
165 return ret;
166
167 *id = create.blob_id;
168 return 0;
169 }
170
171 int _drmModeDestroyPropertyBlob(int fd, uint32_t id) {
spang 2016/03/23 16:19:37 DestroyPropertyBlob()
robert.bradford 2016/03/23 20:52:33 Done.
172 struct _drm_mode_destroy_blob destroy;
173 int ret;
174
175 memset(&destroy, 0, sizeof(destroy));
176 destroy.blob_id = id;
177 ret = drmIoctl(fd, _DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy);
178 return ret < 0 ? -errno : ret;
179 }
180
181 using ScopedDrmColorLutPtr = scoped_ptr<DrmColorLut, base::FreeDeleter>;
182 using ScopedDrmColorCtmPtr = scoped_ptr<DrmColorCtm, base::FreeDeleter>;
183
184 ScopedDrmColorLutPtr CreateLutBlob(
185 const std::vector<GammaRampRGBEntry>& source) {
186 TRACE_EVENT0("drm", "CreateLutBlob");
187 ScopedDrmColorLutPtr lut(
188 static_cast<DrmColorLut*>(malloc(sizeof(DrmColorLut) * source.size())));
189 DrmColorLut* p = lut.get();
190 for (size_t i = 0; i < source.size(); ++i) {
191 p[i].red = source[i].r;
192 p[i].green = source[i].g;
193 p[i].blue = source[i].b;
194 }
195 return lut;
196 }
197
198 ScopedDrmColorCtmPtr CreateCTMBlob(
199 const std::vector<float>& correction_matrix) {
200 ScopedDrmColorCtmPtr ctm(
201 static_cast<DrmColorCtm*>(malloc(sizeof(DrmColorCtm))));
202 for (size_t i = 0; i < arraysize(ctm->ctm_coeff); ++i) {
203 if (correction_matrix[i] < 0) {
204 ctm->ctm_coeff[i] = static_cast<uint64_t>(
205 -correction_matrix[i] * (static_cast<uint64_t>(1) << 32));
206 ctm->ctm_coeff[i] |= static_cast<uint64_t>(1) << 63;
207 } else {
208 ctm->ctm_coeff[i] = static_cast<uint64_t>(
209 correction_matrix[i] * (static_cast<uint64_t>(1) << 32));
210 }
211 }
212 return ctm;
213 }
214
215 bool SetBlobProperty(int fd,
216 uint32_t object_id,
217 uint32_t object_type,
218 uint32_t prop_id,
219 const char* property_name,
220 unsigned char* data,
221 size_t length) {
222 uint32_t blob_id;
223 int res;
224 res = _drmModeCreatePropertyBlob(fd, data, length, &blob_id);
225 if (res != 0) {
226 LOG(ERROR) << "Error creating property blob: " << base::safe_strerror(res)
227 << " for property " << property_name;
228 return false;
229 }
230 res = drmModeObjectSetProperty(fd, object_id, object_type, prop_id, blob_id);
231 if (res != 0) {
232 LOG(ERROR) << "Error updating property: " << base::safe_strerror(res)
233 << " for property " << property_name;
234 _drmModeDestroyPropertyBlob(fd, blob_id);
235 return false;
236 }
237 _drmModeDestroyPropertyBlob(fd, blob_id);
238 return true;
239 }
240
241 std::vector<GammaRampRGBEntry> ResampleLut(
242 const std::vector<GammaRampRGBEntry>& lut_in,
243 size_t desired_size) {
244 TRACE_EVENT1("drm", "ResampleLut", "desired_size", desired_size);
245 if (lut_in.size() == desired_size)
246 return lut_in;
247
248 std::vector<GammaRampRGBEntry> result;
249 result.resize(desired_size);
250
251 for (size_t i = 0; i < desired_size; ++i) {
252 size_t base_index = lut_in.size() * i / desired_size;
253 size_t remaining = lut_in.size() * i % desired_size;
254 if (base_index < lut_in.size() - 1) {
255 result[i].r = lut_in[base_index].r +
256 (lut_in[base_index + 1].r - lut_in[base_index].r) *
257 remaining / desired_size;
258 result[i].g = lut_in[base_index].g +
259 (lut_in[base_index + 1].g - lut_in[base_index].g) *
260 remaining / desired_size;
261 result[i].b = lut_in[base_index].b +
262 (lut_in[base_index + 1].b - lut_in[base_index].b) *
263 remaining / desired_size;
264 } else {
265 result[i] = lut_in[lut_in.size() - 1];
266 }
267 }
268
269 return result;
270 }
271
116 } // namespace 272 } // namespace
117 273
118 class DrmDevice::PageFlipManager { 274 class DrmDevice::PageFlipManager {
119 public: 275 public:
120 PageFlipManager() : next_id_(0) {} 276 PageFlipManager() : next_id_(0) {}
121 ~PageFlipManager() {} 277 ~PageFlipManager() {}
122 278
123 void OnPageFlip(uint32_t frame, 279 void OnPageFlip(uint32_t frame,
124 uint32_t seconds, 280 uint32_t seconds,
125 uint32_t useconds, 281 uint32_t useconds,
(...skipping 409 matching lines...) Expand 10 before | Expand all | Expand 10 after
535 g.push_back(lut[i].g); 691 g.push_back(lut[i].g);
536 b.push_back(lut[i].b); 692 b.push_back(lut[i].b);
537 } 693 }
538 694
539 DCHECK(file_.IsValid()); 695 DCHECK(file_.IsValid());
540 TRACE_EVENT0("drm", "DrmDevice::SetGamma"); 696 TRACE_EVENT0("drm", "DrmDevice::SetGamma");
541 return (drmModeCrtcSetGamma(file_.GetPlatformFile(), crtc_id, r.size(), &r[0], 697 return (drmModeCrtcSetGamma(file_.GetPlatformFile(), crtc_id, r.size(), &r[0],
542 &g[0], &b[0]) == 0); 698 &g[0], &b[0]) == 0);
543 } 699 }
544 700
701 bool DrmDevice::SetColorCorrection(
702 uint32_t crtc_id,
703 const std::vector<GammaRampRGBEntry>& degamma_lut,
704 const std::vector<GammaRampRGBEntry>& gamma_lut,
705 const std::vector<float>& correction_matrix) {
706 ScopedDrmObjectPropertyPtr crtc_props(drmModeObjectGetProperties(
707 file_.GetPlatformFile(), crtc_id, DRM_MODE_OBJECT_CRTC));
708 uint64_t degamma_lut_size = 0;
709 uint64_t gamma_lut_size = 0;
710
711 for (uint32_t i = 0; i < crtc_props->count_props; ++i) {
712 ScopedDrmPropertyPtr property(
713 drmModeGetProperty(file_.GetPlatformFile(), crtc_props->props[i]));
714 if (property && !strcmp(property->name, "DEGAMMA_LUT_SIZE")) {
715 degamma_lut_size = crtc_props->prop_values[i];
716 }
717 if (property && !strcmp(property->name, "GAMMA_LUT_SIZE")) {
718 gamma_lut_size = crtc_props->prop_values[i];
719 }
720
721 if (degamma_lut_size && gamma_lut_size)
722 break;
723 }
724
725 if (degamma_lut_size == 0 || gamma_lut_size == 0) {
726 LOG(WARNING) << "No available (de)gamma tables.";
727 return false;
728 }
729
730 ScopedDrmColorLutPtr degamma_blob_data =
731 CreateLutBlob(ResampleLut(degamma_lut, degamma_lut_size));
732 ScopedDrmColorLutPtr gamma_blob_data =
733 CreateLutBlob(ResampleLut(gamma_lut, gamma_lut_size));
734 ScopedDrmColorCtmPtr ctm_blob_data = CreateCTMBlob(correction_matrix);
735
736 for (uint32_t i = 0; i < crtc_props->count_props; ++i) {
737 ScopedDrmPropertyPtr property(
738 drmModeGetProperty(file_.GetPlatformFile(), crtc_props->props[i]));
739 if (!property)
740 continue;
741
742 if (!strcmp(property->name, "DEGAMMA_LUT")) {
743 if (!SetBlobProperty(
744 file_.GetPlatformFile(), crtc_id, DRM_MODE_OBJECT_CRTC,
745 crtc_props->props[i], property->name,
746 reinterpret_cast<unsigned char*>(degamma_blob_data.get()),
747 sizeof(DrmColorLut) * degamma_lut_size))
748 return false;
749 }
750 if (!strcmp(property->name, "GAMMA_LUT")) {
751 if (!SetBlobProperty(
752 file_.GetPlatformFile(), crtc_id, DRM_MODE_OBJECT_CRTC,
753 crtc_props->props[i], property->name,
754 reinterpret_cast<unsigned char*>(gamma_blob_data.get()),
755 sizeof(DrmColorLut) * gamma_lut_size))
756 return false;
757 }
758 if (!strcmp(property->name, "CTM")) {
759 if (!SetBlobProperty(
760 file_.GetPlatformFile(), crtc_id, DRM_MODE_OBJECT_CRTC,
761 crtc_props->props[i], property->name,
762 reinterpret_cast<unsigned char*>(ctm_blob_data.get()),
763 sizeof(DrmColorCtm)))
764 return false;
765 }
766 }
767
768 return true;
769 }
770
545 } // namespace ui 771 } // 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