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

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: Integrate dnicoara@ feedback 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 std::vector<uint8_t> CreateLutBlob(
131 const std::vector<GammaRampRGBEntry>& source) {
132 TRACE_EVENT0("drm", "CreateLutBlob");
133 std::vector<uint8_t> data;
134
135 data.resize(source.size() * sizeof(DrmColorLut));
136
137 DrmColorLut* lut = reinterpret_cast<DrmColorLut*>(data.data());
spang 2016/02/22 18:57:50 Please use a vector<DrmColorLut> and cast to unsig
robert.bradford 2016/02/23 19:23:01 Ack. Replaced with a scoped_ptr w/ base::FreeDelet
138
139 for (size_t i = 0; i < source.size(); ++i) {
140 lut[i].red = source[i].r;
141 lut[i].green = source[i].g;
142 lut[i].blue = source[i].b;
143 }
144 return data;
145 }
146
147 std::vector<uint8_t> CreateCTMBlob(
148 const std::vector<float>& correction_matrix) {
149 std::vector<uint8_t> data;
150 data.resize(sizeof(DrmColorCtm));
151 DrmColorCtm* ctm = reinterpret_cast<DrmColorCtm*>(data.data());
spang 2016/02/22 18:57:50 Same here, it's not OK to cast unsigned char* to a
robert.bradford 2016/02/23 19:23:01 Done. The cast is migrated now only to the SetBlob
152
153 for (int i = 0; i < 9; ++i) {
154 if (correction_matrix[i] < 0) {
155 ctm->ctm_coeff[i] = static_cast<uint64_t>(
156 -correction_matrix[i] * (static_cast<uint64_t>(1) << 32));
157 ctm->ctm_coeff[i] |= static_cast<uint64_t>(1) << 63;
158 } else {
159 ctm->ctm_coeff[i] = static_cast<uint64_t>(
160 correction_matrix[i] * (static_cast<uint64_t>(1) << 32));
161 }
162 }
163
164 return data;
165 }
166
167 bool SetBlobProperty(int fd,
168 uint32_t object_id,
169 uint32_t object_type,
170 uint32_t prop_id,
171 std::vector<uint8_t> data) {
spang 2016/02/22 18:57:50 const &
robert.bradford 2016/02/23 19:23:01 Replaced with a raw pointer now using scoped_ptr a
172 uint32_t blob_id;
173 int res;
174 res = drmModeCreatePropertyBlob(fd, data.data(), data.size(), &blob_id);
175 if (res != 0) {
176 LOG(ERROR) << "Error creating property blob: " << base::safe_strerror(res);
spang 2016/02/22 18:57:50 PLOG(ERROR)
spang 2016/02/22 19:01:46 Nevermind that's for strerror(errno).
177 return false;
178 }
179 res = drmModeObjectSetProperty(fd, object_id, object_type, prop_id, blob_id);
180 if (res != 0) {
181 LOG(ERROR) << "Error updating property: " << base::safe_strerror(res);
spang 2016/02/22 18:57:50 PLOG(ERROR)
182 drmModeDestroyPropertyBlob(fd, blob_id);
183 return false;
184 }
185 drmModeDestroyPropertyBlob(fd, blob_id);
spang 2016/02/22 18:57:50 I don't understand the lifetime of the blob. Who's
robert.bradford 2016/02/23 19:23:01 I was using the stack allocation of the std::vecto
186 return true;
187 }
188
189 std::vector<GammaRampRGBEntry> ResampleLut(
190 const std::vector<GammaRampRGBEntry>& lut_in,
191 size_t desired_size) {
192 TRACE_EVENT1("drm", "ResampleLut", "desired_size", desired_size);
193 if (lut_in.size() == desired_size)
194 return lut_in;
195
196 std::vector<GammaRampRGBEntry> result;
197 result.resize(desired_size);
198
199 for (size_t i = 0; i < desired_size; ++i) {
200 size_t base_index = lut_in.size() * i / desired_size;
201 size_t remaining = lut_in.size() * i % desired_size;
202 if (base_index < lut_in.size() - 1) {
203 result[i].r = lut_in[base_index].r +
204 (lut_in[base_index + 1].r - lut_in[base_index].r) *
205 remaining / desired_size;
206 result[i].g = lut_in[base_index].g +
207 (lut_in[base_index + 1].g - lut_in[base_index].g) *
208 remaining / desired_size;
209 result[i].b = lut_in[base_index].b +
210 (lut_in[base_index + 1].b - lut_in[base_index].b) *
211 remaining / desired_size;
212 } else {
213 result[i] = lut_in[lut_in.size() - 1];
214 }
215 }
216
217 return result;
218 }
219
116 } // namespace 220 } // namespace
117 221
118 class DrmDevice::PageFlipManager { 222 class DrmDevice::PageFlipManager {
119 public: 223 public:
120 PageFlipManager() : next_id_(0) {} 224 PageFlipManager() : next_id_(0) {}
121 ~PageFlipManager() {} 225 ~PageFlipManager() {}
122 226
123 void OnPageFlip(uint32_t frame, 227 void OnPageFlip(uint32_t frame,
124 uint32_t seconds, 228 uint32_t seconds,
125 uint32_t useconds, 229 uint32_t useconds,
(...skipping 409 matching lines...) Expand 10 before | Expand all | Expand 10 after
535 g.push_back(lut[i].g); 639 g.push_back(lut[i].g);
536 b.push_back(lut[i].b); 640 b.push_back(lut[i].b);
537 } 641 }
538 642
539 DCHECK(file_.IsValid()); 643 DCHECK(file_.IsValid());
540 TRACE_EVENT0("drm", "DrmDevice::SetGamma"); 644 TRACE_EVENT0("drm", "DrmDevice::SetGamma");
541 return (drmModeCrtcSetGamma(file_.GetPlatformFile(), crtc_id, r.size(), &r[0], 645 return (drmModeCrtcSetGamma(file_.GetPlatformFile(), crtc_id, r.size(), &r[0],
542 &g[0], &b[0]) == 0); 646 &g[0], &b[0]) == 0);
543 } 647 }
544 648
649 bool DrmDevice::SetColorCorrection(
650 uint32_t crtc_id,
651 const std::vector<GammaRampRGBEntry>& degamma_lut,
652 const std::vector<GammaRampRGBEntry>& gamma_lut,
653 const std::vector<float>& correction_matrix) {
654 ScopedDrmObjectPropertyPtr crtc_props(drmModeObjectGetProperties(
655 file_.GetPlatformFile(), crtc_id, DRM_MODE_OBJECT_CRTC));
656 uint64_t degamma_lut_size = 0;
657 uint64_t gamma_lut_size = 0;
658
659 for (uint32_t i = 0; i < crtc_props->count_props; ++i) {
660 ScopedDrmPropertyPtr property(
661 drmModeGetProperty(file_.GetPlatformFile(), crtc_props->props[i]));
662 if (property && !strcmp(property->name, "DEGAMMA_LUT_SIZE")) {
663 degamma_lut_size = crtc_props->prop_values[i];
664 }
665 if (property && !strcmp(property->name, "GAMMA_LUT_SIZE")) {
666 gamma_lut_size = crtc_props->prop_values[i];
667 }
668
669 if (degamma_lut_size && gamma_lut_size)
670 break;
671 }
672
673 if (degamma_lut_size == 0 || gamma_lut_size == 0) {
674 LOG(WARNING) << "No available (de)gamma tables.";
spang 2016/02/22 18:57:50 Will we hit this if color correction isn't support
robert.bradford 2016/02/23 19:23:01 This is protection against a broken kernel impleme
675 return false;
676 }
677
678 std::vector<uint8_t> degamma_blob_data =
679 CreateLutBlob(ResampleLut(degamma_lut, degamma_lut_size));
680 std::vector<uint8_t> gamma_blob_data =
681 CreateLutBlob(ResampleLut(gamma_lut, gamma_lut_size));
682 std::vector<uint8_t> ctm_blob_data = CreateCTMBlob(correction_matrix);
683
684 for (uint32_t i = 0; i < crtc_props->count_props; ++i) {
685 ScopedDrmPropertyPtr property(
686 drmModeGetProperty(file_.GetPlatformFile(), crtc_props->props[i]));
687 if (!property)
688 continue;
689
690 if (!strcmp(property->name, "DEGAMMA_LUT")) {
691 if (!SetBlobProperty(file_.GetPlatformFile(), crtc_id,
692 DRM_MODE_OBJECT_CRTC, crtc_props->props[i],
693 degamma_blob_data)) {
694 LOG(WARNING) << "Error setting degamma property.";
spang 2016/02/22 18:57:50 Can you pass the property name into SetBlobPropert
robert.bradford 2016/02/23 19:23:01 Done.
695 return false;
696 }
697 }
698 if (!strcmp(property->name, "GAMMA_LUT")) {
699 if (!SetBlobProperty(file_.GetPlatformFile(), crtc_id,
700 DRM_MODE_OBJECT_CRTC, crtc_props->props[i],
701 gamma_blob_data)) {
702 LOG(WARNING) << "Error setting gamma property.";
703 return false;
704 }
705 }
706 if (!strcmp(property->name, "CTM_MATRIX")) {
707 if (!SetBlobProperty(file_.GetPlatformFile(), crtc_id,
708 DRM_MODE_OBJECT_CRTC, crtc_props->props[i],
709 ctm_blob_data)) {
710 LOG(WARNING) << "Error setting correction matrix property.";
711 return false;
712 }
713 }
714 }
715
716 return true;
717 }
718
545 } // namespace ui 719 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698