aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomi Valkeinen2017-06-02 04:14:11 -0500
committerTomi Valkeinen2017-06-02 04:14:11 -0500
commitcb0786049f960f2bd383617151b01318e02e9ff9 (patch)
tree8dbce2679b0b87e9edf971efc87a05c1550d0ffb
parent719f72a065f07c59e77a25b1f23daccb5369cf81 (diff)
parent7450c364a57ec7d9e2abd61ac6e025c53e9e7a42 (diff)
downloadexternal-libkmsxx-cb0786049f960f2bd383617151b01318e02e9ff9.tar.gz
external-libkmsxx-cb0786049f960f2bd383617151b01318e02e9ff9.tar.xz
external-libkmsxx-cb0786049f960f2bd383617151b01318e02e9ff9.zip
Merge branch 'color-features' of git://github.com/jsarha/kmsxx
-rw-r--r--kms++util/inc/kms++util/color.h12
-rw-r--r--kms++util/inc/kms++util/kms++util.h2
-rw-r--r--kms++util/src/color.cpp74
-rw-r--r--kms++util/src/testpat.cpp18
-rw-r--r--py/pykms/pykmsbase.cpp3
-rw-r--r--py/pykms/pykmsutil.cpp10
-rwxr-xr-xpy/tests/ctm_test.py86
-rwxr-xr-xpy/tests/plane_csc.py66
8 files changed, 243 insertions, 28 deletions
diff --git a/kms++util/inc/kms++util/color.h b/kms++util/inc/kms++util/color.h
index ba2ed25..f378433 100644
--- a/kms++util/inc/kms++util/color.h
+++ b/kms++util/inc/kms++util/color.h
@@ -6,6 +6,14 @@ namespace kms
6{ 6{
7struct YUV; 7struct YUV;
8 8
9enum class YUVType {
10 BT601_Lim = 0,
11 BT601_Full,
12 BT709_Lim,
13 BT709_Full,
14 MAX,
15};
16
9struct RGB 17struct RGB
10{ 18{
11 RGB(); 19 RGB();
@@ -19,7 +27,7 @@ struct RGB
19 uint32_t abgr8888() const; 27 uint32_t abgr8888() const;
20 uint16_t rgb565() const; 28 uint16_t rgb565() const;
21 uint16_t bgr565() const; 29 uint16_t bgr565() const;
22 YUV yuv() const; 30 YUV yuv(YUVType type = YUVType::BT601_Lim) const;
23 31
24 uint8_t b; 32 uint8_t b;
25 uint8_t g; 33 uint8_t g;
@@ -31,7 +39,7 @@ struct YUV
31{ 39{
32 YUV(); 40 YUV();
33 YUV(uint8_t y, uint8_t u, uint8_t v); 41 YUV(uint8_t y, uint8_t u, uint8_t v);
34 YUV(const RGB& rgb); 42 YUV(const RGB& rgb, YUVType type = YUVType::BT601_Lim);
35 43
36 uint8_t v; 44 uint8_t v;
37 uint8_t u; 45 uint8_t u;
diff --git a/kms++util/inc/kms++util/kms++util.h b/kms++util/inc/kms++util/kms++util.h
index c1e3c8c..d45497e 100644
--- a/kms++util/inc/kms++util/kms++util.h
+++ b/kms++util/inc/kms++util/kms++util.h
@@ -26,7 +26,7 @@ void draw_text(IMappedFramebuffer& buf, uint32_t x, uint32_t y, const std::strin
26 26
27void draw_color_bar(IMappedFramebuffer& buf, int old_xpos, int xpos, int width); 27void draw_color_bar(IMappedFramebuffer& buf, int old_xpos, int xpos, int width);
28 28
29void draw_test_pattern(IMappedFramebuffer &fb); 29void draw_test_pattern(IMappedFramebuffer &fb, YUVType yuvt = YUVType::BT601_Lim);
30} 30}
31 31
32#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) 32#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
diff --git a/kms++util/src/color.cpp b/kms++util/src/color.cpp
index ae8a4b4..2e6f217 100644
--- a/kms++util/src/color.cpp
+++ b/kms++util/src/color.cpp
@@ -59,11 +59,40 @@ uint16_t RGB::bgr565() const
59 return ((b >> 3) << 11) | ((g >> 2) << 5) | ((r >> 3) << 0); 59 return ((b >> 3) << 11) | ((g >> 2) << 5) | ((r >> 3) << 0);
60} 60}
61 61
62YUV RGB::yuv() const 62YUV RGB::yuv(YUVType type) const
63{ 63{
64 return YUV(*this); 64 return YUV(*this, type);
65} 65}
66 66
67#define CF_ONE (256)
68#define CF(a, b, c) { ((int) ((a) * CF_ONE)), ((int) ((b) * CF_ONE)), ((int) ((c) * CF_ONE)) }
69#define CLAMP(a) ((a) > (CF_ONE-1) ? (CF_ONE-1) : (a) < 0 ? 0 : (a))
70
71const int YUVcoef[static_cast<unsigned>(YUVType::MAX)][3][3] = {
72 [static_cast<unsigned>(YUVType::BT601_Lim)] = {
73 CF( 0.257, 0.504, 0.098),
74 CF(-0.148, -0.291, 0.439),
75 CF( 0.439, -0.368, -0.071) },
76 [static_cast<unsigned>(YUVType::BT601_Full)] = {
77 CF( 0.299, 0.587, 0.114),
78 CF(-0.169, -0.331, 0.500),
79 CF( 0.500, -0.419, -0.081) },
80 [static_cast<unsigned>(YUVType::BT709_Lim)] = {
81 CF( 0.1826, 0.6142, 0.0620),
82 CF(-0.1006, -0.3386, 0.4392),
83 CF( 0.4392, -0.3989, -0.0403) },
84 [static_cast<unsigned>(YUVType::BT709_Full)] = {
85 CF( 0.2126, 0.7152, 0.0722),
86 CF(-0.1146, -0.3854, 0.5000),
87 CF( 0.5000, -0.4542, -0.0468) },
88};
89
90const int YUVoffset[static_cast<unsigned>(YUVType::MAX)][3] = {
91 [static_cast<unsigned>(YUVType::BT601_Lim)] = CF(0.0625, 0.5, 0.5),
92 [static_cast<unsigned>(YUVType::BT601_Full)] = CF( 0, 0.5, 0.5),
93 [static_cast<unsigned>(YUVType::BT709_Lim)] = CF(0.0625, 0.5, 0.5),
94 [static_cast<unsigned>(YUVType::BT709_Full)] = CF( 0, 0.5, 0.5),
95};
67 96
68YUV::YUV() 97YUV::YUV()
69{ 98{
@@ -78,26 +107,41 @@ YUV::YUV(uint8_t y, uint8_t u, uint8_t v)
78 this->a = 0; 107 this->a = 0;
79} 108}
80 109
81static inline uint8_t MAKE_YUV_601_Y(uint8_t r, uint8_t g, uint8_t b) 110static inline
111uint8_t MAKE_YUV_Y(uint8_t r, uint8_t g, uint8_t b, YUVType type)
82{ 112{
83 return (((66 * r + 129 * g + 25 * b + 128) >> 8) + 16); 113 unsigned tidx = static_cast<unsigned>(type);
114
115 return CLAMP(((YUVcoef[tidx][0][0] * r + YUVcoef[tidx][0][1] * g +
116 YUVcoef[tidx][0][2] * b + CF_ONE/2) / CF_ONE) +
117 YUVoffset[tidx][0]);
84} 118}
85 119
86static inline uint8_t MAKE_YUV_601_U(uint8_t r, uint8_t g, uint8_t b) 120static inline
121uint8_t MAKE_YUV_U(uint8_t r, uint8_t g, uint8_t b, YUVType type)
87{ 122{
88 return (((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128); 123 unsigned tidx = static_cast<unsigned>(type);
124
125 return CLAMP(((YUVcoef[tidx][1][0] * r + YUVcoef[tidx][1][1] * g +
126 YUVcoef[tidx][1][2] * b + CF_ONE/2) / CF_ONE) +
127 YUVoffset[tidx][1]);
89} 128}
90 129
91static inline uint8_t MAKE_YUV_601_V(uint8_t r, uint8_t g, uint8_t b) 130static inline
131uint8_t MAKE_YUV_V(uint8_t r, uint8_t g, uint8_t b, YUVType type)
92{ 132{
93 return (((112 * r - 94 * g - 18 * b + 128) >> 8) + 128); 133 unsigned tidx = static_cast<unsigned>(type);
134
135 return CLAMP(((YUVcoef[tidx][2][0] * r + YUVcoef[tidx][2][1] * g +
136 YUVcoef[tidx][2][2] * b + CF_ONE/2) / CF_ONE) +
137 YUVoffset[tidx][2]);
94} 138}
95 139
96YUV::YUV(const RGB& rgb) 140YUV::YUV(const RGB& rgb, YUVType type)
97{ 141{
98 this->y = MAKE_YUV_601_Y(rgb.r, rgb.g, rgb.b); 142 this->y = MAKE_YUV_Y(rgb.r, rgb.g, rgb.b, type);
99 this->u = MAKE_YUV_601_U(rgb.r, rgb.g, rgb.b); 143 this->u = MAKE_YUV_U(rgb.r, rgb.g, rgb.b, type);
100 this->v = MAKE_YUV_601_V(rgb.r, rgb.g, rgb.b); 144 this->v = MAKE_YUV_V(rgb.r, rgb.g, rgb.b, type);
101 this->a = rgb.a; 145 this->a = rgb.a;
102} 146}
103} 147}
diff --git a/kms++util/src/testpat.cpp b/kms++util/src/testpat.cpp
index fbe3b2e..dbd8fe6 100644
--- a/kms++util/src/testpat.cpp
+++ b/kms++util/src/testpat.cpp
@@ -97,7 +97,7 @@ static RGB get_test_pattern_pixel(IMappedFramebuffer& fb, unsigned x, unsigned y
97 } 97 }
98} 98}
99 99
100static void draw_test_pattern_part(IMappedFramebuffer& fb, unsigned start_y, unsigned end_y) 100static void draw_test_pattern_part(IMappedFramebuffer& fb, unsigned start_y, unsigned end_y, YUVType yuvt)
101{ 101{
102 unsigned x, y; 102 unsigned x, y;
103 unsigned w = fb.width(); 103 unsigned w = fb.width();
@@ -127,7 +127,7 @@ static void draw_test_pattern_part(IMappedFramebuffer& fb, unsigned start_y, uns
127 for (x = 0; x < w; x += 2) { 127 for (x = 0; x < w; x += 2) {
128 RGB pixel1 = get_test_pattern_pixel(fb, x, y); 128 RGB pixel1 = get_test_pattern_pixel(fb, x, y);
129 RGB pixel2 = get_test_pattern_pixel(fb, x + 1, y); 129 RGB pixel2 = get_test_pattern_pixel(fb, x + 1, y);
130 draw_yuv422_macropixel(fb, x, y, pixel1.yuv(), pixel2.yuv()); 130 draw_yuv422_macropixel(fb, x, y, pixel1.yuv(yuvt), pixel2.yuv(yuvt));
131 } 131 }
132 } 132 }
133 break; 133 break;
@@ -141,8 +141,8 @@ static void draw_test_pattern_part(IMappedFramebuffer& fb, unsigned start_y, uns
141 RGB pixel01 = get_test_pattern_pixel(fb, x, y + 1); 141 RGB pixel01 = get_test_pattern_pixel(fb, x, y + 1);
142 RGB pixel11 = get_test_pattern_pixel(fb, x + 1, y + 1); 142 RGB pixel11 = get_test_pattern_pixel(fb, x + 1, y + 1);
143 draw_yuv420_macropixel(fb, x, y, 143 draw_yuv420_macropixel(fb, x, y,
144 pixel00.yuv(), pixel10.yuv(), 144 pixel00.yuv(yuvt), pixel10.yuv(yuvt),
145 pixel01.yuv(), pixel11.yuv()); 145 pixel01.yuv(yuvt), pixel11.yuv(yuvt));
146 } 146 }
147 } 147 }
148 break; 148 break;
@@ -151,10 +151,10 @@ static void draw_test_pattern_part(IMappedFramebuffer& fb, unsigned start_y, uns
151 } 151 }
152} 152}
153 153
154static void draw_test_pattern_impl(IMappedFramebuffer& fb) 154static void draw_test_pattern_impl(IMappedFramebuffer& fb, YUVType yuvt)
155{ 155{
156 if (fb.height() < 20) { 156 if (fb.height() < 20) {
157 draw_test_pattern_part(fb, 0, fb.height()); 157 draw_test_pattern_part(fb, 0, fb.height(), yuvt);
158 return; 158 return;
159 } 159 }
160 160
@@ -174,21 +174,21 @@ static void draw_test_pattern_impl(IMappedFramebuffer& fb)
174 if (n == num_threads - 1) 174 if (n == num_threads - 1)
175 end = fb.height(); 175 end = fb.height();
176 176
177 workers.push_back(thread([&fb, start, end]() { draw_test_pattern_part(fb, start, end); })); 177 workers.push_back(thread([&fb, start, end, yuvt]() { draw_test_pattern_part(fb, start, end, yuvt); }));
178 } 178 }
179 179
180 for (thread& t : workers) 180 for (thread& t : workers)
181 t.join(); 181 t.join();
182} 182}
183 183
184void draw_test_pattern(IMappedFramebuffer &fb) 184void draw_test_pattern(IMappedFramebuffer &fb, YUVType yuvt)
185{ 185{
186#ifdef DRAW_PERF_PRINT 186#ifdef DRAW_PERF_PRINT
187 Stopwatch sw; 187 Stopwatch sw;
188 sw.start(); 188 sw.start();
189#endif 189#endif
190 190
191 draw_test_pattern_impl(fb); 191 draw_test_pattern_impl(fb, yuvt);
192 192
193#ifdef DRAW_PERF_PRINT 193#ifdef DRAW_PERF_PRINT
194 double us = sw.elapsed_us(); 194 double us = sw.elapsed_us();
diff --git a/py/pykms/pykmsbase.cpp b/py/pykms/pykmsbase.cpp
index d013f1f..604e07c 100644
--- a/py/pykms/pykmsbase.cpp
+++ b/py/pykms/pykmsbase.cpp
@@ -33,6 +33,7 @@ void init_pykmsbase(py::module &m)
33 .def("get_prop_value", (uint64_t (DrmPropObject::*)(const string&) const)&DrmPropObject::get_prop_value) 33 .def("get_prop_value", (uint64_t (DrmPropObject::*)(const string&) const)&DrmPropObject::get_prop_value)
34 .def("set_prop_value",(int (DrmPropObject::*)(const string&, uint64_t)) &DrmPropObject::set_prop_value) 34 .def("set_prop_value",(int (DrmPropObject::*)(const string&, uint64_t)) &DrmPropObject::set_prop_value)
35 .def("get_prop_value_as_blob", &DrmPropObject::get_prop_value_as_blob) 35 .def("get_prop_value_as_blob", &DrmPropObject::get_prop_value_as_blob)
36 .def("get_prop", &DrmPropObject::get_prop)
36 ; 37 ;
37 38
38 py::class_<Connector, Connector*>(m, "Connector", py::base<DrmPropObject>()) 39 py::class_<Connector, Connector*>(m, "Connector", py::base<DrmPropObject>())
@@ -50,6 +51,7 @@ void init_pykmsbase(py::module &m)
50 51
51 py::class_<Crtc, Crtc*>(m, "Crtc", py::base<DrmPropObject>()) 52 py::class_<Crtc, Crtc*>(m, "Crtc", py::base<DrmPropObject>())
52 .def("set_mode", &Crtc::set_mode) 53 .def("set_mode", &Crtc::set_mode)
54 .def("disable_mode", &Crtc::disable_mode)
53 .def("page_flip", 55 .def("page_flip",
54 [](Crtc* self, Framebuffer& fb, py::object ob) 56 [](Crtc* self, Framebuffer& fb, py::object ob)
55 { 57 {
@@ -86,6 +88,7 @@ void init_pykmsbase(py::module &m)
86 88
87 py::class_<Property, Property*>(m, "Property", py::base<DrmObject>()) 89 py::class_<Property, Property*>(m, "Property", py::base<DrmObject>())
88 .def_property_readonly("name", &Property::name) 90 .def_property_readonly("name", &Property::name)
91 .def_property_readonly("enums", &Property::get_enums)
89 ; 92 ;
90 93
91 py::class_<Blob>(m, "Blob", py::base<DrmObject>()) 94 py::class_<Blob>(m, "Blob", py::base<DrmObject>())
diff --git a/py/pykms/pykmsutil.cpp b/py/pykms/pykmsutil.cpp
index cb09dea..a5a6041 100644
--- a/py/pykms/pykmsutil.cpp
+++ b/py/pykms/pykmsutil.cpp
@@ -40,9 +40,17 @@ void init_pykmstest(py::module &m)
40 py::arg("crtc"), 40 py::arg("crtc"),
41 py::arg("format") = PixelFormat::Undefined) 41 py::arg("format") = PixelFormat::Undefined)
42 ; 42 ;
43 py::enum_<YUVType>(m, "YUVType")
44 .value("BT601_Lim", YUVType::BT601_Lim)
45 .value("BT601_Full", YUVType::BT601_Full)
46 .value("BT709_Lim", YUVType::BT709_Lim)
47 .value("BT709_Full", YUVType::BT709_Full)
48 ;
43 49
44 // Use lambdas to handle IMappedFramebuffer 50 // Use lambdas to handle IMappedFramebuffer
45 m.def("draw_test_pattern", [](MappedFramebuffer& fb) { draw_test_pattern(fb); } ); 51 m.def("draw_test_pattern", [](MappedFramebuffer& fb, YUVType yuvt) { draw_test_pattern(fb, yuvt); },
52 py::arg("fb"),
53 py::arg("yuvt") = YUVType::BT601_Lim);
46 m.def("draw_color_bar", [](MappedFramebuffer& fb, int old_xpos, int xpos, int width) { 54 m.def("draw_color_bar", [](MappedFramebuffer& fb, int old_xpos, int xpos, int width) {
47 draw_color_bar(fb, old_xpos, xpos, width); 55 draw_color_bar(fb, old_xpos, xpos, width);
48 } ); 56 } );
diff --git a/py/tests/ctm_test.py b/py/tests/ctm_test.py
new file mode 100755
index 0000000..7ceed6f
--- /dev/null
+++ b/py/tests/ctm_test.py
@@ -0,0 +1,86 @@
1#!/usr/bin/python3
2
3import sys
4import pykms
5
6def ctm_to_blob(ctm, card):
7 len=9
8 arr = bytearray(len*8)
9 view = memoryview(arr).cast("I")
10
11 for x in range(len):
12 i, d = divmod(ctm[x], 1)
13 if i < 0:
14 i = -i
15 sign = 1 << 31
16 else:
17 sign = 0
18 view[x * 2 + 0] = int(d * ((2 ** 32) - 1))
19 view[x * 2 + 1] = int(i) | sign
20 #print("%f = %08x.%08x" % (ctm[x], view[x * 2 + 1], view[x * 2 + 0]))
21
22 return pykms.Blob(card, arr);
23
24
25if len(sys.argv) > 1:
26 conn_name = sys.argv[1]
27else:
28 conn_name = ""
29
30card = pykms.Card()
31res = pykms.ResourceManager(card)
32conn = res.reserve_connector(conn_name)
33crtc = res.reserve_crtc(conn)
34mode = conn.get_default_mode()
35
36fb = pykms.DumbFramebuffer(card, mode.hdisplay, mode.vdisplay, "XR24");
37pykms.draw_test_pattern(fb);
38
39crtc.set_mode(conn, fb, mode)
40
41input("press enter to set normal ctm\n")
42
43ctm = [ 1.0, 0.0, 0.0,
44 0.0, 1.0, 0.0,
45 0.0, 0.0, 1.0 ]
46
47ctmb = ctm_to_blob(ctm, card)
48
49crtc.set_prop("CTM", ctmb.id)
50
51input("press enter to set new ctm\n")
52
53ctm = [ 0.0, 1.0, 0.0,
54 0.0, 0.0, 1.0,
55 1.0, 0.0, 0.0 ]
56
57ctmb = ctm_to_blob(ctm, card)
58
59crtc.set_prop("CTM", ctmb.id)
60
61print("r->b g->r b->g ctm active\n")
62
63input("press enter to set new ctm\n")
64
65ctm = [ 0.0, 0.0, 1.0,
66 1.0, 0.0, 0.0,
67 0.0, 1.0, 0.0 ]
68
69ctmb = ctm_to_blob(ctm, card)
70
71crtc.set_prop("CTM", ctmb.id)
72input("r->g g->b b->r ctm active\n")
73
74input("press enter to turn off the crtc\n")
75
76crtc.disable_mode()
77
78input("press enter to enable crtc again\n")
79
80crtc.set_mode(conn, fb, mode)
81
82input("press enter to remove ctm\n")
83
84crtc.set_prop("CTM", 0)
85
86input("press enter to exit\n")
diff --git a/py/tests/plane_csc.py b/py/tests/plane_csc.py
new file mode 100755
index 0000000..be92c00
--- /dev/null
+++ b/py/tests/plane_csc.py
@@ -0,0 +1,66 @@
1#!/usr/bin/python3
2
3import pykms
4
5card = pykms.Card()
6res = pykms.ResourceManager(card)
7conn = res.reserve_connector("HDMI")
8crtc = res.reserve_crtc(conn)
9mode = conn.get_default_mode()
10modeb = mode.to_blob(card)
11plane = res.reserve_generic_plane(crtc, pykms.PixelFormat.UYVY)
12#plane = res.reserve_generic_plane(crtc, pykms.PixelFormat.Undefined)
13
14print("Got plane %d %d" % (plane.idx, plane.id))
15
16fb = pykms.DumbFramebuffer(card, mode.hdisplay, mode.vdisplay, "UYVY");
17pykms.draw_test_pattern(fb);
18
19req = pykms.AtomicReq(card)
20req.add(conn, "CRTC_ID", crtc.id)
21req.add(crtc, {"ACTIVE": 1,
22 "MODE_ID": modeb.id})
23
24input("Press enter to enable crtc idx %d at %s" % (crtc.idx, conn.fullname))
25r = req.commit_sync(allow_modeset = True)
26
27input("Press enter to enable plane idx %d at %s" % (plane.idx, conn.fullname))
28
29req = pykms.AtomicReq(card)
30req.add(plane, {"FB_ID": fb.id,
31 "CRTC_ID": crtc.id,
32 "SRC_X": 0 << 16,
33 "SRC_Y": 0 << 16,
34 "SRC_W": fb.width << 16,
35 "SRC_H": fb.height << 16,
36 "CRTC_X": 0,
37 "CRTC_Y": 0,
38 "CRTC_W": fb.width,
39 "CRTC_H": fb.height,
40 "zorder": 0})
41r = req.commit_sync()
42print("Plane enable request returned %d\n" % r)
43
44yuv_types = [pykms.YUVType.BT601_Lim,
45 pykms.YUVType.BT601_Full,
46 pykms.YUVType.BT709_Lim,
47 pykms.YUVType.BT709_Full]
48
49encoding_enums = plane.get_prop("COLOR_ENCODING").enums
50range_enums = plane.get_prop("COLOR_RANGE").enums
51
52for i in range(0, 2):
53 for j in range(0, 2):
54 input("press enter to for encoding: \"%s\" range: \"%s\"\n" %
55 (encoding_enums[i], range_enums[j]))
56
57 req = pykms.AtomicReq(card)
58 req.add(plane, {"COLOR_ENCODING": i,
59 "COLOR_RANGE": j})
60 req.commit_sync()
61
62 for t in yuv_types:
63 input("press enter to redraw with yuv_type %s\n" % t)
64 pykms.draw_test_pattern(fb, t);
65
66input("press enter to exit\n")