pattern.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283
  1. /*
  2. * Copyright 2008 Tungsten Graphics
  3. * Jakob Bornecrantz <jakob@tungstengraphics.com>
  4. * Copyright 2008 Intel Corporation
  5. * Jesse Barnes <jesse.barnes@intel.com>
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  23. * IN THE SOFTWARE.
  24. */
  25. #include <stdint.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <drm_fourcc.h>
  30. #if HAVE_CAIRO
  31. #include <cairo.h>
  32. #include <math.h>
  33. #endif
  34. #include "common.h"
  35. #include "format.h"
  36. #include "pattern.h"
  37. struct color_rgb24 {
  38. unsigned int value:24;
  39. } __attribute__((__packed__));
  40. struct color_yuv {
  41. unsigned char y;
  42. unsigned char u;
  43. unsigned char v;
  44. };
  45. #define MAKE_YUV_601_Y(r, g, b) \
  46. ((( 66 * (r) + 129 * (g) + 25 * (b) + 128) >> 8) + 16)
  47. #define MAKE_YUV_601_U(r, g, b) \
  48. (((-38 * (r) - 74 * (g) + 112 * (b) + 128) >> 8) + 128)
  49. #define MAKE_YUV_601_V(r, g, b) \
  50. (((112 * (r) - 94 * (g) - 18 * (b) + 128) >> 8) + 128)
  51. #define MAKE_YUV_601(r, g, b) \
  52. { .y = MAKE_YUV_601_Y(r, g, b), \
  53. .u = MAKE_YUV_601_U(r, g, b), \
  54. .v = MAKE_YUV_601_V(r, g, b) }
  55. /* This function takes 8-bit color values */
  56. static inline uint32_t shiftcolor8(const struct util_color_component *comp,
  57. uint32_t value)
  58. {
  59. value &= 0xff;
  60. /* Fill the low bits with the high bits. */
  61. value = (value << 8) | value;
  62. /* Shift down to remove unwanted low bits */
  63. value = value >> (16 - comp->length);
  64. /* Shift back up to where the value should be */
  65. return value << comp->offset;
  66. }
  67. /* This function takes 10-bit color values */
  68. static inline uint32_t shiftcolor10(const struct util_color_component *comp,
  69. uint32_t value)
  70. {
  71. value &= 0x3ff;
  72. /* Fill the low bits with the high bits. */
  73. value = (value << 6) | (value >> 4);
  74. /* Shift down to remove unwanted low bits */
  75. value = value >> (16 - comp->length);
  76. /* Shift back up to where the value should be */
  77. return value << comp->offset;
  78. }
  79. /* This function takes 16-bit color values */
  80. static inline uint64_t shiftcolor16(const struct util_color_component *comp,
  81. uint64_t value)
  82. {
  83. value &= 0xffff;
  84. /* Shift down to remove unwanted low bits */
  85. value = value >> (16 - comp->length);
  86. /* Shift back up to where the value should be */
  87. return value << comp->offset;
  88. }
  89. #define MAKE_RGBA10(rgb, r, g, b, a) \
  90. (shiftcolor10(&(rgb)->red, (r)) | \
  91. shiftcolor10(&(rgb)->green, (g)) | \
  92. shiftcolor10(&(rgb)->blue, (b)) | \
  93. shiftcolor10(&(rgb)->alpha, (a)))
  94. #define MAKE_RGBA(rgb, r, g, b, a) \
  95. (shiftcolor8(&(rgb)->red, (r)) | \
  96. shiftcolor8(&(rgb)->green, (g)) | \
  97. shiftcolor8(&(rgb)->blue, (b)) | \
  98. shiftcolor8(&(rgb)->alpha, (a)))
  99. #define MAKE_RGB24(rgb, r, g, b) \
  100. { .value = MAKE_RGBA(rgb, r, g, b, 0) }
  101. /**
  102. * Takes a uint16_t, divides by 65536, converts the infinite-precision
  103. * result to fp16 with round-to-zero.
  104. *
  105. * Copied from mesa:src/util/half_float.c
  106. */
  107. static uint16_t uint16_div_64k_to_half(uint16_t v)
  108. {
  109. /* Zero or subnormal. Set the mantissa to (v << 8) and return. */
  110. if (v < 4)
  111. return v << 8;
  112. /* Count the leading 0s in the uint16_t */
  113. int n = __builtin_clz(v) - 16;
  114. /* Shift the mantissa up so bit 16 is the hidden 1 bit,
  115. * mask it off, then shift back down to 10 bits
  116. */
  117. int m = ( ((uint32_t)v << (n + 1)) & 0xffff ) >> 6;
  118. /* (0{n} 1 X{15-n}) * 2^-16
  119. * = 1.X * 2^(15-n-16)
  120. * = 1.X * 2^(14-n - 15)
  121. * which is the FP16 form with e = 14 - n
  122. */
  123. int e = 14 - n;
  124. return (e << 10) | m;
  125. }
  126. #define MAKE_RGBA8FP16(rgb, r, g, b, a) \
  127. (shiftcolor16(&(rgb)->red, uint16_div_64k_to_half((r) << 8)) | \
  128. shiftcolor16(&(rgb)->green, uint16_div_64k_to_half((g) << 8)) | \
  129. shiftcolor16(&(rgb)->blue, uint16_div_64k_to_half((b) << 8)) | \
  130. shiftcolor16(&(rgb)->alpha, uint16_div_64k_to_half((a) << 8)))
  131. #define MAKE_RGBA10FP16(rgb, r, g, b, a) \
  132. (shiftcolor16(&(rgb)->red, uint16_div_64k_to_half((r) << 6)) | \
  133. shiftcolor16(&(rgb)->green, uint16_div_64k_to_half((g) << 6)) | \
  134. shiftcolor16(&(rgb)->blue, uint16_div_64k_to_half((b) << 6)) | \
  135. shiftcolor16(&(rgb)->alpha, uint16_div_64k_to_half((a) << 6)))
  136. static void fill_smpte_yuv_planar(const struct util_yuv_info *yuv,
  137. unsigned char *y_mem, unsigned char *u_mem,
  138. unsigned char *v_mem, unsigned int width,
  139. unsigned int height, unsigned int stride)
  140. {
  141. const struct color_yuv colors_top[] = {
  142. MAKE_YUV_601(191, 192, 192), /* grey */
  143. MAKE_YUV_601(192, 192, 0), /* yellow */
  144. MAKE_YUV_601(0, 192, 192), /* cyan */
  145. MAKE_YUV_601(0, 192, 0), /* green */
  146. MAKE_YUV_601(192, 0, 192), /* magenta */
  147. MAKE_YUV_601(192, 0, 0), /* red */
  148. MAKE_YUV_601(0, 0, 192), /* blue */
  149. };
  150. const struct color_yuv colors_middle[] = {
  151. MAKE_YUV_601(0, 0, 192), /* blue */
  152. MAKE_YUV_601(19, 19, 19), /* black */
  153. MAKE_YUV_601(192, 0, 192), /* magenta */
  154. MAKE_YUV_601(19, 19, 19), /* black */
  155. MAKE_YUV_601(0, 192, 192), /* cyan */
  156. MAKE_YUV_601(19, 19, 19), /* black */
  157. MAKE_YUV_601(192, 192, 192), /* grey */
  158. };
  159. const struct color_yuv colors_bottom[] = {
  160. MAKE_YUV_601(0, 33, 76), /* in-phase */
  161. MAKE_YUV_601(255, 255, 255), /* super white */
  162. MAKE_YUV_601(50, 0, 106), /* quadrature */
  163. MAKE_YUV_601(19, 19, 19), /* black */
  164. MAKE_YUV_601(9, 9, 9), /* 3.5% */
  165. MAKE_YUV_601(19, 19, 19), /* 7.5% */
  166. MAKE_YUV_601(29, 29, 29), /* 11.5% */
  167. MAKE_YUV_601(19, 19, 19), /* black */
  168. };
  169. unsigned int cs = yuv->chroma_stride;
  170. unsigned int xsub = yuv->xsub;
  171. unsigned int ysub = yuv->ysub;
  172. unsigned int x;
  173. unsigned int y;
  174. /* Luma */
  175. for (y = 0; y < height * 6 / 9; ++y) {
  176. for (x = 0; x < width; ++x)
  177. y_mem[x] = colors_top[x * 7 / width].y;
  178. y_mem += stride;
  179. }
  180. for (; y < height * 7 / 9; ++y) {
  181. for (x = 0; x < width; ++x)
  182. y_mem[x] = colors_middle[x * 7 / width].y;
  183. y_mem += stride;
  184. }
  185. for (; y < height; ++y) {
  186. for (x = 0; x < width * 5 / 7; ++x)
  187. y_mem[x] = colors_bottom[x * 4 / (width * 5 / 7)].y;
  188. for (; x < width * 6 / 7; ++x)
  189. y_mem[x] = colors_bottom[(x - width * 5 / 7) * 3
  190. / (width / 7) + 4].y;
  191. for (; x < width; ++x)
  192. y_mem[x] = colors_bottom[7].y;
  193. y_mem += stride;
  194. }
  195. /* Chroma */
  196. for (y = 0; y < height / ysub * 6 / 9; ++y) {
  197. for (x = 0; x < width; x += xsub) {
  198. u_mem[x*cs/xsub] = colors_top[x * 7 / width].u;
  199. v_mem[x*cs/xsub] = colors_top[x * 7 / width].v;
  200. }
  201. u_mem += stride * cs / xsub;
  202. v_mem += stride * cs / xsub;
  203. }
  204. for (; y < height / ysub * 7 / 9; ++y) {
  205. for (x = 0; x < width; x += xsub) {
  206. u_mem[x*cs/xsub] = colors_middle[x * 7 / width].u;
  207. v_mem[x*cs/xsub] = colors_middle[x * 7 / width].v;
  208. }
  209. u_mem += stride * cs / xsub;
  210. v_mem += stride * cs / xsub;
  211. }
  212. for (; y < height / ysub; ++y) {
  213. for (x = 0; x < width * 5 / 7; x += xsub) {
  214. u_mem[x*cs/xsub] =
  215. colors_bottom[x * 4 / (width * 5 / 7)].u;
  216. v_mem[x*cs/xsub] =
  217. colors_bottom[x * 4 / (width * 5 / 7)].v;
  218. }
  219. for (; x < width * 6 / 7; x += xsub) {
  220. u_mem[x*cs/xsub] = colors_bottom[(x - width * 5 / 7) *
  221. 3 / (width / 7) + 4].u;
  222. v_mem[x*cs/xsub] = colors_bottom[(x - width * 5 / 7) *
  223. 3 / (width / 7) + 4].v;
  224. }
  225. for (; x < width; x += xsub) {
  226. u_mem[x*cs/xsub] = colors_bottom[7].u;
  227. v_mem[x*cs/xsub] = colors_bottom[7].v;
  228. }
  229. u_mem += stride * cs / xsub;
  230. v_mem += stride * cs / xsub;
  231. }
  232. }
  233. static void fill_smpte_yuv_packed(const struct util_yuv_info *yuv, void *mem,
  234. unsigned int width, unsigned int height,
  235. unsigned int stride)
  236. {
  237. const struct color_yuv colors_top[] = {
  238. MAKE_YUV_601(191, 192, 192), /* grey */
  239. MAKE_YUV_601(192, 192, 0), /* yellow */
  240. MAKE_YUV_601(0, 192, 192), /* cyan */
  241. MAKE_YUV_601(0, 192, 0), /* green */
  242. MAKE_YUV_601(192, 0, 192), /* magenta */
  243. MAKE_YUV_601(192, 0, 0), /* red */
  244. MAKE_YUV_601(0, 0, 192), /* blue */
  245. };
  246. const struct color_yuv colors_middle[] = {
  247. MAKE_YUV_601(0, 0, 192), /* blue */
  248. MAKE_YUV_601(19, 19, 19), /* black */
  249. MAKE_YUV_601(192, 0, 192), /* magenta */
  250. MAKE_YUV_601(19, 19, 19), /* black */
  251. MAKE_YUV_601(0, 192, 192), /* cyan */
  252. MAKE_YUV_601(19, 19, 19), /* black */
  253. MAKE_YUV_601(192, 192, 192), /* grey */
  254. };
  255. const struct color_yuv colors_bottom[] = {
  256. MAKE_YUV_601(0, 33, 76), /* in-phase */
  257. MAKE_YUV_601(255, 255, 255), /* super white */
  258. MAKE_YUV_601(50, 0, 106), /* quadrature */
  259. MAKE_YUV_601(19, 19, 19), /* black */
  260. MAKE_YUV_601(9, 9, 9), /* 3.5% */
  261. MAKE_YUV_601(19, 19, 19), /* 7.5% */
  262. MAKE_YUV_601(29, 29, 29), /* 11.5% */
  263. MAKE_YUV_601(19, 19, 19), /* black */
  264. };
  265. unsigned char *y_mem = (yuv->order & YUV_YC) ? mem : mem + 1;
  266. unsigned char *c_mem = (yuv->order & YUV_CY) ? mem : mem + 1;
  267. unsigned int u = (yuv->order & YUV_YCrCb) ? 2 : 0;
  268. unsigned int v = (yuv->order & YUV_YCbCr) ? 2 : 0;
  269. unsigned int x;
  270. unsigned int y;
  271. /* Luma */
  272. for (y = 0; y < height * 6 / 9; ++y) {
  273. for (x = 0; x < width; ++x)
  274. y_mem[2*x] = colors_top[x * 7 / width].y;
  275. y_mem += stride;
  276. }
  277. for (; y < height * 7 / 9; ++y) {
  278. for (x = 0; x < width; ++x)
  279. y_mem[2*x] = colors_middle[x * 7 / width].y;
  280. y_mem += stride;
  281. }
  282. for (; y < height; ++y) {
  283. for (x = 0; x < width * 5 / 7; ++x)
  284. y_mem[2*x] = colors_bottom[x * 4 / (width * 5 / 7)].y;
  285. for (; x < width * 6 / 7; ++x)
  286. y_mem[2*x] = colors_bottom[(x - width * 5 / 7) * 3
  287. / (width / 7) + 4].y;
  288. for (; x < width; ++x)
  289. y_mem[2*x] = colors_bottom[7].y;
  290. y_mem += stride;
  291. }
  292. /* Chroma */
  293. for (y = 0; y < height * 6 / 9; ++y) {
  294. for (x = 0; x < width; x += 2) {
  295. c_mem[2*x+u] = colors_top[x * 7 / width].u;
  296. c_mem[2*x+v] = colors_top[x * 7 / width].v;
  297. }
  298. c_mem += stride;
  299. }
  300. for (; y < height * 7 / 9; ++y) {
  301. for (x = 0; x < width; x += 2) {
  302. c_mem[2*x+u] = colors_middle[x * 7 / width].u;
  303. c_mem[2*x+v] = colors_middle[x * 7 / width].v;
  304. }
  305. c_mem += stride;
  306. }
  307. for (; y < height; ++y) {
  308. for (x = 0; x < width * 5 / 7; x += 2) {
  309. c_mem[2*x+u] = colors_bottom[x * 4 / (width * 5 / 7)].u;
  310. c_mem[2*x+v] = colors_bottom[x * 4 / (width * 5 / 7)].v;
  311. }
  312. for (; x < width * 6 / 7; x += 2) {
  313. c_mem[2*x+u] = colors_bottom[(x - width * 5 / 7) *
  314. 3 / (width / 7) + 4].u;
  315. c_mem[2*x+v] = colors_bottom[(x - width * 5 / 7) *
  316. 3 / (width / 7) + 4].v;
  317. }
  318. for (; x < width; x += 2) {
  319. c_mem[2*x+u] = colors_bottom[7].u;
  320. c_mem[2*x+v] = colors_bottom[7].v;
  321. }
  322. c_mem += stride;
  323. }
  324. }
  325. static void fill_smpte_rgb16(const struct util_rgb_info *rgb, void *mem,
  326. unsigned int width, unsigned int height,
  327. unsigned int stride)
  328. {
  329. const uint16_t colors_top[] = {
  330. MAKE_RGBA(rgb, 192, 192, 192, 255), /* grey */
  331. MAKE_RGBA(rgb, 192, 192, 0, 255), /* yellow */
  332. MAKE_RGBA(rgb, 0, 192, 192, 255), /* cyan */
  333. MAKE_RGBA(rgb, 0, 192, 0, 255), /* green */
  334. MAKE_RGBA(rgb, 192, 0, 192, 255), /* magenta */
  335. MAKE_RGBA(rgb, 192, 0, 0, 255), /* red */
  336. MAKE_RGBA(rgb, 0, 0, 192, 255), /* blue */
  337. };
  338. const uint16_t colors_middle[] = {
  339. MAKE_RGBA(rgb, 0, 0, 192, 127), /* blue */
  340. MAKE_RGBA(rgb, 19, 19, 19, 127), /* black */
  341. MAKE_RGBA(rgb, 192, 0, 192, 127), /* magenta */
  342. MAKE_RGBA(rgb, 19, 19, 19, 127), /* black */
  343. MAKE_RGBA(rgb, 0, 192, 192, 127), /* cyan */
  344. MAKE_RGBA(rgb, 19, 19, 19, 127), /* black */
  345. MAKE_RGBA(rgb, 192, 192, 192, 127), /* grey */
  346. };
  347. const uint16_t colors_bottom[] = {
  348. MAKE_RGBA(rgb, 0, 33, 76, 255), /* in-phase */
  349. MAKE_RGBA(rgb, 255, 255, 255, 255), /* super white */
  350. MAKE_RGBA(rgb, 50, 0, 106, 255), /* quadrature */
  351. MAKE_RGBA(rgb, 19, 19, 19, 255), /* black */
  352. MAKE_RGBA(rgb, 9, 9, 9, 255), /* 3.5% */
  353. MAKE_RGBA(rgb, 19, 19, 19, 255), /* 7.5% */
  354. MAKE_RGBA(rgb, 29, 29, 29, 255), /* 11.5% */
  355. MAKE_RGBA(rgb, 19, 19, 19, 255), /* black */
  356. };
  357. unsigned int x;
  358. unsigned int y;
  359. for (y = 0; y < height * 6 / 9; ++y) {
  360. for (x = 0; x < width; ++x)
  361. ((uint16_t *)mem)[x] = colors_top[x * 7 / width];
  362. mem += stride;
  363. }
  364. for (; y < height * 7 / 9; ++y) {
  365. for (x = 0; x < width; ++x)
  366. ((uint16_t *)mem)[x] = colors_middle[x * 7 / width];
  367. mem += stride;
  368. }
  369. for (; y < height; ++y) {
  370. for (x = 0; x < width * 5 / 7; ++x)
  371. ((uint16_t *)mem)[x] =
  372. colors_bottom[x * 4 / (width * 5 / 7)];
  373. for (; x < width * 6 / 7; ++x)
  374. ((uint16_t *)mem)[x] =
  375. colors_bottom[(x - width * 5 / 7) * 3
  376. / (width / 7) + 4];
  377. for (; x < width; ++x)
  378. ((uint16_t *)mem)[x] = colors_bottom[7];
  379. mem += stride;
  380. }
  381. }
  382. static void fill_smpte_rgb24(const struct util_rgb_info *rgb, void *mem,
  383. unsigned int width, unsigned int height,
  384. unsigned int stride)
  385. {
  386. const struct color_rgb24 colors_top[] = {
  387. MAKE_RGB24(rgb, 192, 192, 192), /* grey */
  388. MAKE_RGB24(rgb, 192, 192, 0), /* yellow */
  389. MAKE_RGB24(rgb, 0, 192, 192), /* cyan */
  390. MAKE_RGB24(rgb, 0, 192, 0), /* green */
  391. MAKE_RGB24(rgb, 192, 0, 192), /* magenta */
  392. MAKE_RGB24(rgb, 192, 0, 0), /* red */
  393. MAKE_RGB24(rgb, 0, 0, 192), /* blue */
  394. };
  395. const struct color_rgb24 colors_middle[] = {
  396. MAKE_RGB24(rgb, 0, 0, 192), /* blue */
  397. MAKE_RGB24(rgb, 19, 19, 19), /* black */
  398. MAKE_RGB24(rgb, 192, 0, 192), /* magenta */
  399. MAKE_RGB24(rgb, 19, 19, 19), /* black */
  400. MAKE_RGB24(rgb, 0, 192, 192), /* cyan */
  401. MAKE_RGB24(rgb, 19, 19, 19), /* black */
  402. MAKE_RGB24(rgb, 192, 192, 192), /* grey */
  403. };
  404. const struct color_rgb24 colors_bottom[] = {
  405. MAKE_RGB24(rgb, 0, 33, 76), /* in-phase */
  406. MAKE_RGB24(rgb, 255, 255, 255), /* super white */
  407. MAKE_RGB24(rgb, 50, 0, 106), /* quadrature */
  408. MAKE_RGB24(rgb, 19, 19, 19), /* black */
  409. MAKE_RGB24(rgb, 9, 9, 9), /* 3.5% */
  410. MAKE_RGB24(rgb, 19, 19, 19), /* 7.5% */
  411. MAKE_RGB24(rgb, 29, 29, 29), /* 11.5% */
  412. MAKE_RGB24(rgb, 19, 19, 19), /* black */
  413. };
  414. unsigned int x;
  415. unsigned int y;
  416. for (y = 0; y < height * 6 / 9; ++y) {
  417. for (x = 0; x < width; ++x)
  418. ((struct color_rgb24 *)mem)[x] =
  419. colors_top[x * 7 / width];
  420. mem += stride;
  421. }
  422. for (; y < height * 7 / 9; ++y) {
  423. for (x = 0; x < width; ++x)
  424. ((struct color_rgb24 *)mem)[x] =
  425. colors_middle[x * 7 / width];
  426. mem += stride;
  427. }
  428. for (; y < height; ++y) {
  429. for (x = 0; x < width * 5 / 7; ++x)
  430. ((struct color_rgb24 *)mem)[x] =
  431. colors_bottom[x * 4 / (width * 5 / 7)];
  432. for (; x < width * 6 / 7; ++x)
  433. ((struct color_rgb24 *)mem)[x] =
  434. colors_bottom[(x - width * 5 / 7) * 3
  435. / (width / 7) + 4];
  436. for (; x < width; ++x)
  437. ((struct color_rgb24 *)mem)[x] = colors_bottom[7];
  438. mem += stride;
  439. }
  440. }
  441. static void fill_smpte_rgb32(const struct util_rgb_info *rgb, void *mem,
  442. unsigned int width, unsigned int height,
  443. unsigned int stride)
  444. {
  445. const uint32_t colors_top[] = {
  446. MAKE_RGBA(rgb, 192, 192, 192, 255), /* grey */
  447. MAKE_RGBA(rgb, 192, 192, 0, 255), /* yellow */
  448. MAKE_RGBA(rgb, 0, 192, 192, 255), /* cyan */
  449. MAKE_RGBA(rgb, 0, 192, 0, 255), /* green */
  450. MAKE_RGBA(rgb, 192, 0, 192, 255), /* magenta */
  451. MAKE_RGBA(rgb, 192, 0, 0, 255), /* red */
  452. MAKE_RGBA(rgb, 0, 0, 192, 255), /* blue */
  453. };
  454. const uint32_t colors_middle[] = {
  455. MAKE_RGBA(rgb, 0, 0, 192, 127), /* blue */
  456. MAKE_RGBA(rgb, 19, 19, 19, 127), /* black */
  457. MAKE_RGBA(rgb, 192, 0, 192, 127), /* magenta */
  458. MAKE_RGBA(rgb, 19, 19, 19, 127), /* black */
  459. MAKE_RGBA(rgb, 0, 192, 192, 127), /* cyan */
  460. MAKE_RGBA(rgb, 19, 19, 19, 127), /* black */
  461. MAKE_RGBA(rgb, 192, 192, 192, 127), /* grey */
  462. };
  463. const uint32_t colors_bottom[] = {
  464. MAKE_RGBA(rgb, 0, 33, 76, 255), /* in-phase */
  465. MAKE_RGBA(rgb, 255, 255, 255, 255), /* super white */
  466. MAKE_RGBA(rgb, 50, 0, 106, 255), /* quadrature */
  467. MAKE_RGBA(rgb, 19, 19, 19, 255), /* black */
  468. MAKE_RGBA(rgb, 9, 9, 9, 255), /* 3.5% */
  469. MAKE_RGBA(rgb, 19, 19, 19, 255), /* 7.5% */
  470. MAKE_RGBA(rgb, 29, 29, 29, 255), /* 11.5% */
  471. MAKE_RGBA(rgb, 19, 19, 19, 255), /* black */
  472. };
  473. unsigned int x;
  474. unsigned int y;
  475. for (y = 0; y < height * 6 / 9; ++y) {
  476. for (x = 0; x < width; ++x)
  477. ((uint32_t *)mem)[x] = colors_top[x * 7 / width];
  478. mem += stride;
  479. }
  480. for (; y < height * 7 / 9; ++y) {
  481. for (x = 0; x < width; ++x)
  482. ((uint32_t *)mem)[x] = colors_middle[x * 7 / width];
  483. mem += stride;
  484. }
  485. for (; y < height; ++y) {
  486. for (x = 0; x < width * 5 / 7; ++x)
  487. ((uint32_t *)mem)[x] =
  488. colors_bottom[x * 4 / (width * 5 / 7)];
  489. for (; x < width * 6 / 7; ++x)
  490. ((uint32_t *)mem)[x] =
  491. colors_bottom[(x - width * 5 / 7) * 3
  492. / (width / 7) + 4];
  493. for (; x < width; ++x)
  494. ((uint32_t *)mem)[x] = colors_bottom[7];
  495. mem += stride;
  496. }
  497. }
  498. static void fill_smpte_rgb16fp(const struct util_rgb_info *rgb, void *mem,
  499. unsigned int width, unsigned int height,
  500. unsigned int stride)
  501. {
  502. const uint64_t colors_top[] = {
  503. MAKE_RGBA8FP16(rgb, 192, 192, 192, 255),/* grey */
  504. MAKE_RGBA8FP16(rgb, 192, 192, 0, 255), /* yellow */
  505. MAKE_RGBA8FP16(rgb, 0, 192, 192, 255), /* cyan */
  506. MAKE_RGBA8FP16(rgb, 0, 192, 0, 255), /* green */
  507. MAKE_RGBA8FP16(rgb, 192, 0, 192, 255), /* magenta */
  508. MAKE_RGBA8FP16(rgb, 192, 0, 0, 255), /* red */
  509. MAKE_RGBA8FP16(rgb, 0, 0, 192, 255), /* blue */
  510. };
  511. const uint64_t colors_middle[] = {
  512. MAKE_RGBA8FP16(rgb, 0, 0, 192, 127), /* blue */
  513. MAKE_RGBA8FP16(rgb, 19, 19, 19, 127), /* black */
  514. MAKE_RGBA8FP16(rgb, 192, 0, 192, 127), /* magenta */
  515. MAKE_RGBA8FP16(rgb, 19, 19, 19, 127), /* black */
  516. MAKE_RGBA8FP16(rgb, 0, 192, 192, 127), /* cyan */
  517. MAKE_RGBA8FP16(rgb, 19, 19, 19, 127), /* black */
  518. MAKE_RGBA8FP16(rgb, 192, 192, 192, 127),/* grey */
  519. };
  520. const uint64_t colors_bottom[] = {
  521. MAKE_RGBA8FP16(rgb, 0, 33, 76, 255), /* in-phase */
  522. MAKE_RGBA8FP16(rgb, 255, 255, 255, 255),/* super white */
  523. MAKE_RGBA8FP16(rgb, 50, 0, 106, 255), /* quadrature */
  524. MAKE_RGBA8FP16(rgb, 19, 19, 19, 255), /* black */
  525. MAKE_RGBA8FP16(rgb, 9, 9, 9, 255), /* 3.5% */
  526. MAKE_RGBA8FP16(rgb, 19, 19, 19, 255), /* 7.5% */
  527. MAKE_RGBA8FP16(rgb, 29, 29, 29, 255), /* 11.5% */
  528. MAKE_RGBA8FP16(rgb, 19, 19, 19, 255), /* black */
  529. };
  530. unsigned int x;
  531. unsigned int y;
  532. for (y = 0; y < height * 6 / 9; ++y) {
  533. for (x = 0; x < width; ++x)
  534. ((uint64_t *)mem)[x] = colors_top[x * 7 / width];
  535. mem += stride;
  536. }
  537. for (; y < height * 7 / 9; ++y) {
  538. for (x = 0; x < width; ++x)
  539. ((uint64_t *)mem)[x] = colors_middle[x * 7 / width];
  540. mem += stride;
  541. }
  542. for (; y < height; ++y) {
  543. for (x = 0; x < width * 5 / 7; ++x)
  544. ((uint64_t *)mem)[x] =
  545. colors_bottom[x * 4 / (width * 5 / 7)];
  546. for (; x < width * 6 / 7; ++x)
  547. ((uint64_t *)mem)[x] =
  548. colors_bottom[(x - width * 5 / 7) * 3
  549. / (width / 7) + 4];
  550. for (; x < width; ++x)
  551. ((uint64_t *)mem)[x] = colors_bottom[7];
  552. mem += stride;
  553. }
  554. }
  555. static void fill_smpte_c8(void *mem, unsigned int width, unsigned int height,
  556. unsigned int stride)
  557. {
  558. unsigned int x;
  559. unsigned int y;
  560. for (y = 0; y < height * 6 / 9; ++y) {
  561. for (x = 0; x < width; ++x)
  562. ((uint8_t *)mem)[x] = x * 7 / width;
  563. mem += stride;
  564. }
  565. for (; y < height * 7 / 9; ++y) {
  566. for (x = 0; x < width; ++x)
  567. ((uint8_t *)mem)[x] = 7 + (x * 7 / width);
  568. mem += stride;
  569. }
  570. for (; y < height; ++y) {
  571. for (x = 0; x < width * 5 / 7; ++x)
  572. ((uint8_t *)mem)[x] =
  573. 14 + (x * 4 / (width * 5 / 7));
  574. for (; x < width * 6 / 7; ++x)
  575. ((uint8_t *)mem)[x] =
  576. 14 + ((x - width * 5 / 7) * 3
  577. / (width / 7) + 4);
  578. for (; x < width; ++x)
  579. ((uint8_t *)mem)[x] = 14 + 7;
  580. mem += stride;
  581. }
  582. }
  583. void util_smpte_c8_gamma(unsigned size, struct drm_color_lut *lut)
  584. {
  585. if (size < 7 + 7 + 8) {
  586. printf("Error: gamma too small: %d < %d\n", size, 7 + 7 + 8);
  587. return;
  588. }
  589. memset(lut, 0, size * sizeof(struct drm_color_lut));
  590. #define FILL_COLOR(idx, r, g, b) \
  591. lut[idx].red = (r) << 8; \
  592. lut[idx].green = (g) << 8; \
  593. lut[idx].blue = (b) << 8
  594. FILL_COLOR( 0, 192, 192, 192); /* grey */
  595. FILL_COLOR( 1, 192, 192, 0 ); /* yellow */
  596. FILL_COLOR( 2, 0, 192, 192); /* cyan */
  597. FILL_COLOR( 3, 0, 192, 0 ); /* green */
  598. FILL_COLOR( 4, 192, 0, 192); /* magenta */
  599. FILL_COLOR( 5, 192, 0, 0 ); /* red */
  600. FILL_COLOR( 6, 0, 0, 192); /* blue */
  601. FILL_COLOR( 7, 0, 0, 192); /* blue */
  602. FILL_COLOR( 8, 19, 19, 19 ); /* black */
  603. FILL_COLOR( 9, 192, 0, 192); /* magenta */
  604. FILL_COLOR(10, 19, 19, 19 ); /* black */
  605. FILL_COLOR(11, 0, 192, 192); /* cyan */
  606. FILL_COLOR(12, 19, 19, 19 ); /* black */
  607. FILL_COLOR(13, 192, 192, 192); /* grey */
  608. FILL_COLOR(14, 0, 33, 76); /* in-phase */
  609. FILL_COLOR(15, 255, 255, 255); /* super white */
  610. FILL_COLOR(16, 50, 0, 106); /* quadrature */
  611. FILL_COLOR(17, 19, 19, 19); /* black */
  612. FILL_COLOR(18, 9, 9, 9); /* 3.5% */
  613. FILL_COLOR(19, 19, 19, 19); /* 7.5% */
  614. FILL_COLOR(20, 29, 29, 29); /* 11.5% */
  615. FILL_COLOR(21, 19, 19, 19); /* black */
  616. #undef FILL_COLOR
  617. }
  618. static void fill_smpte(const struct util_format_info *info, void *planes[3],
  619. unsigned int width, unsigned int height,
  620. unsigned int stride)
  621. {
  622. unsigned char *u, *v;
  623. switch (info->format) {
  624. case DRM_FORMAT_C8:
  625. return fill_smpte_c8(planes[0], width, height, stride);
  626. case DRM_FORMAT_UYVY:
  627. case DRM_FORMAT_VYUY:
  628. case DRM_FORMAT_YUYV:
  629. case DRM_FORMAT_YVYU:
  630. return fill_smpte_yuv_packed(&info->yuv, planes[0], width,
  631. height, stride);
  632. case DRM_FORMAT_NV12:
  633. case DRM_FORMAT_NV21:
  634. case DRM_FORMAT_NV16:
  635. case DRM_FORMAT_NV61:
  636. u = info->yuv.order & YUV_YCbCr ? planes[1] : planes[1] + 1;
  637. v = info->yuv.order & YUV_YCrCb ? planes[1] : planes[1] + 1;
  638. return fill_smpte_yuv_planar(&info->yuv, planes[0], u, v,
  639. width, height, stride);
  640. case DRM_FORMAT_YUV420:
  641. return fill_smpte_yuv_planar(&info->yuv, planes[0], planes[1],
  642. planes[2], width, height, stride);
  643. case DRM_FORMAT_YVU420:
  644. return fill_smpte_yuv_planar(&info->yuv, planes[0], planes[2],
  645. planes[1], width, height, stride);
  646. case DRM_FORMAT_ARGB4444:
  647. case DRM_FORMAT_XRGB4444:
  648. case DRM_FORMAT_ABGR4444:
  649. case DRM_FORMAT_XBGR4444:
  650. case DRM_FORMAT_RGBA4444:
  651. case DRM_FORMAT_RGBX4444:
  652. case DRM_FORMAT_BGRA4444:
  653. case DRM_FORMAT_BGRX4444:
  654. case DRM_FORMAT_RGB565:
  655. case DRM_FORMAT_BGR565:
  656. case DRM_FORMAT_ARGB1555:
  657. case DRM_FORMAT_XRGB1555:
  658. case DRM_FORMAT_ABGR1555:
  659. case DRM_FORMAT_XBGR1555:
  660. case DRM_FORMAT_RGBA5551:
  661. case DRM_FORMAT_RGBX5551:
  662. case DRM_FORMAT_BGRA5551:
  663. case DRM_FORMAT_BGRX5551:
  664. return fill_smpte_rgb16(&info->rgb, planes[0],
  665. width, height, stride);
  666. case DRM_FORMAT_BGR888:
  667. case DRM_FORMAT_RGB888:
  668. return fill_smpte_rgb24(&info->rgb, planes[0],
  669. width, height, stride);
  670. case DRM_FORMAT_ARGB8888:
  671. case DRM_FORMAT_XRGB8888:
  672. case DRM_FORMAT_ABGR8888:
  673. case DRM_FORMAT_XBGR8888:
  674. case DRM_FORMAT_RGBA8888:
  675. case DRM_FORMAT_RGBX8888:
  676. case DRM_FORMAT_BGRA8888:
  677. case DRM_FORMAT_BGRX8888:
  678. case DRM_FORMAT_ARGB2101010:
  679. case DRM_FORMAT_XRGB2101010:
  680. case DRM_FORMAT_ABGR2101010:
  681. case DRM_FORMAT_XBGR2101010:
  682. case DRM_FORMAT_RGBA1010102:
  683. case DRM_FORMAT_RGBX1010102:
  684. case DRM_FORMAT_BGRA1010102:
  685. case DRM_FORMAT_BGRX1010102:
  686. return fill_smpte_rgb32(&info->rgb, planes[0],
  687. width, height, stride);
  688. case DRM_FORMAT_XRGB16161616F:
  689. case DRM_FORMAT_XBGR16161616F:
  690. case DRM_FORMAT_ARGB16161616F:
  691. case DRM_FORMAT_ABGR16161616F:
  692. return fill_smpte_rgb16fp(&info->rgb, planes[0],
  693. width, height, stride);
  694. }
  695. }
  696. /* swap these for big endian.. */
  697. #define RED 2
  698. #define GREEN 1
  699. #define BLUE 0
  700. static void make_pwetty(void *data, unsigned int width, unsigned int height,
  701. unsigned int stride, uint32_t format)
  702. {
  703. #if HAVE_CAIRO
  704. cairo_surface_t *surface;
  705. cairo_t *cr;
  706. cairo_format_t cairo_format;
  707. /* we can ignore the order of R,G,B channels */
  708. switch (format) {
  709. case DRM_FORMAT_XRGB8888:
  710. case DRM_FORMAT_ARGB8888:
  711. case DRM_FORMAT_XBGR8888:
  712. case DRM_FORMAT_ABGR8888:
  713. cairo_format = CAIRO_FORMAT_ARGB32;
  714. break;
  715. case DRM_FORMAT_RGB565:
  716. case DRM_FORMAT_BGR565:
  717. cairo_format = CAIRO_FORMAT_RGB16_565;
  718. break;
  719. #if CAIRO_VERSION_MAJOR > 1 || (CAIRO_VERSION_MAJOR == 1 && CAIRO_VERSION_MINOR >= 12)
  720. case DRM_FORMAT_ARGB2101010:
  721. case DRM_FORMAT_XRGB2101010:
  722. case DRM_FORMAT_ABGR2101010:
  723. case DRM_FORMAT_XBGR2101010:
  724. cairo_format = CAIRO_FORMAT_RGB30;
  725. break;
  726. #endif
  727. default:
  728. return;
  729. }
  730. surface = cairo_image_surface_create_for_data(data,
  731. cairo_format,
  732. width, height,
  733. stride);
  734. cr = cairo_create(surface);
  735. cairo_surface_destroy(surface);
  736. cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE);
  737. for (unsigned x = 0; x < width; x += 250)
  738. for (unsigned y = 0; y < height; y += 250) {
  739. char buf[64];
  740. cairo_move_to(cr, x, y - 20);
  741. cairo_line_to(cr, x, y + 20);
  742. cairo_move_to(cr, x - 20, y);
  743. cairo_line_to(cr, x + 20, y);
  744. cairo_new_sub_path(cr);
  745. cairo_arc(cr, x, y, 10, 0, M_PI * 2);
  746. cairo_set_line_width(cr, 4);
  747. cairo_set_source_rgb(cr, 0, 0, 0);
  748. cairo_stroke_preserve(cr);
  749. cairo_set_source_rgb(cr, 1, 1, 1);
  750. cairo_set_line_width(cr, 2);
  751. cairo_stroke(cr);
  752. snprintf(buf, sizeof buf, "%d, %d", x, y);
  753. cairo_move_to(cr, x + 20, y + 20);
  754. cairo_text_path(cr, buf);
  755. cairo_set_source_rgb(cr, 0, 0, 0);
  756. cairo_stroke_preserve(cr);
  757. cairo_set_source_rgb(cr, 1, 1, 1);
  758. cairo_fill(cr);
  759. }
  760. cairo_destroy(cr);
  761. #endif
  762. }
  763. static void fill_tiles_yuv_planar(const struct util_format_info *info,
  764. unsigned char *y_mem, unsigned char *u_mem,
  765. unsigned char *v_mem, unsigned int width,
  766. unsigned int height, unsigned int stride)
  767. {
  768. const struct util_yuv_info *yuv = &info->yuv;
  769. unsigned int cs = yuv->chroma_stride;
  770. unsigned int xsub = yuv->xsub;
  771. unsigned int ysub = yuv->ysub;
  772. unsigned int x;
  773. unsigned int y;
  774. for (y = 0; y < height; ++y) {
  775. for (x = 0; x < width; ++x) {
  776. div_t d = div(x+y, width);
  777. uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
  778. + 0x000a1120 * (d.rem >> 6);
  779. struct color_yuv color =
  780. MAKE_YUV_601((rgb32 >> 16) & 0xff,
  781. (rgb32 >> 8) & 0xff, rgb32 & 0xff);
  782. y_mem[x] = color.y;
  783. u_mem[x/xsub*cs] = color.u;
  784. v_mem[x/xsub*cs] = color.v;
  785. }
  786. y_mem += stride;
  787. if ((y + 1) % ysub == 0) {
  788. u_mem += stride * cs / xsub;
  789. v_mem += stride * cs / xsub;
  790. }
  791. }
  792. }
  793. static void fill_tiles_yuv_packed(const struct util_format_info *info,
  794. void *mem, unsigned int width,
  795. unsigned int height, unsigned int stride)
  796. {
  797. const struct util_yuv_info *yuv = &info->yuv;
  798. unsigned char *y_mem = (yuv->order & YUV_YC) ? mem : mem + 1;
  799. unsigned char *c_mem = (yuv->order & YUV_CY) ? mem : mem + 1;
  800. unsigned int u = (yuv->order & YUV_YCrCb) ? 2 : 0;
  801. unsigned int v = (yuv->order & YUV_YCbCr) ? 2 : 0;
  802. unsigned int x;
  803. unsigned int y;
  804. for (y = 0; y < height; ++y) {
  805. for (x = 0; x < width; x += 2) {
  806. div_t d = div(x+y, width);
  807. uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
  808. + 0x000a1120 * (d.rem >> 6);
  809. struct color_yuv color =
  810. MAKE_YUV_601((rgb32 >> 16) & 0xff,
  811. (rgb32 >> 8) & 0xff, rgb32 & 0xff);
  812. y_mem[2*x] = color.y;
  813. c_mem[2*x+u] = color.u;
  814. y_mem[2*x+2] = color.y;
  815. c_mem[2*x+v] = color.v;
  816. }
  817. y_mem += stride;
  818. c_mem += stride;
  819. }
  820. }
  821. static void fill_tiles_rgb16(const struct util_format_info *info, void *mem,
  822. unsigned int width, unsigned int height,
  823. unsigned int stride)
  824. {
  825. const struct util_rgb_info *rgb = &info->rgb;
  826. void *mem_base = mem;
  827. unsigned int x, y;
  828. for (y = 0; y < height; ++y) {
  829. for (x = 0; x < width; ++x) {
  830. div_t d = div(x+y, width);
  831. uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
  832. + 0x000a1120 * (d.rem >> 6);
  833. uint16_t color =
  834. MAKE_RGBA(rgb, (rgb32 >> 16) & 0xff,
  835. (rgb32 >> 8) & 0xff, rgb32 & 0xff,
  836. 255);
  837. ((uint16_t *)mem)[x] = color;
  838. }
  839. mem += stride;
  840. }
  841. make_pwetty(mem_base, width, height, stride, info->format);
  842. }
  843. static void fill_tiles_rgb24(const struct util_format_info *info, void *mem,
  844. unsigned int width, unsigned int height,
  845. unsigned int stride)
  846. {
  847. const struct util_rgb_info *rgb = &info->rgb;
  848. unsigned int x, y;
  849. for (y = 0; y < height; ++y) {
  850. for (x = 0; x < width; ++x) {
  851. div_t d = div(x+y, width);
  852. uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
  853. + 0x000a1120 * (d.rem >> 6);
  854. struct color_rgb24 color =
  855. MAKE_RGB24(rgb, (rgb32 >> 16) & 0xff,
  856. (rgb32 >> 8) & 0xff, rgb32 & 0xff);
  857. ((struct color_rgb24 *)mem)[x] = color;
  858. }
  859. mem += stride;
  860. }
  861. }
  862. static void fill_tiles_rgb32(const struct util_format_info *info, void *mem,
  863. unsigned int width, unsigned int height,
  864. unsigned int stride)
  865. {
  866. const struct util_rgb_info *rgb = &info->rgb;
  867. void *mem_base = mem;
  868. unsigned int x, y;
  869. for (y = 0; y < height; ++y) {
  870. for (x = 0; x < width; ++x) {
  871. div_t d = div(x+y, width);
  872. uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
  873. + 0x000a1120 * (d.rem >> 6);
  874. uint32_t alpha = ((y < height/2) && (x < width/2)) ? 127 : 255;
  875. uint32_t color =
  876. MAKE_RGBA(rgb, (rgb32 >> 16) & 0xff,
  877. (rgb32 >> 8) & 0xff, rgb32 & 0xff,
  878. alpha);
  879. ((uint32_t *)mem)[x] = color;
  880. }
  881. mem += stride;
  882. }
  883. make_pwetty(mem_base, width, height, stride, info->format);
  884. }
  885. static void fill_tiles_rgb16fp(const struct util_format_info *info, void *mem,
  886. unsigned int width, unsigned int height,
  887. unsigned int stride)
  888. {
  889. const struct util_rgb_info *rgb = &info->rgb;
  890. void *mem_base = mem;
  891. unsigned int x, y;
  892. /* TODO: Give this actual fp16 precision */
  893. for (y = 0; y < height; ++y) {
  894. for (x = 0; x < width; ++x) {
  895. div_t d = div(x+y, width);
  896. uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
  897. + 0x000a1120 * (d.rem >> 6);
  898. uint32_t alpha = ((y < height/2) && (x < width/2)) ? 127 : 255;
  899. uint64_t color =
  900. MAKE_RGBA8FP16(rgb, (rgb32 >> 16) & 0xff,
  901. (rgb32 >> 8) & 0xff, rgb32 & 0xff,
  902. alpha);
  903. ((uint64_t *)mem)[x] = color;
  904. }
  905. mem += stride;
  906. }
  907. }
  908. static void fill_tiles(const struct util_format_info *info, void *planes[3],
  909. unsigned int width, unsigned int height,
  910. unsigned int stride)
  911. {
  912. unsigned char *u, *v;
  913. switch (info->format) {
  914. case DRM_FORMAT_UYVY:
  915. case DRM_FORMAT_VYUY:
  916. case DRM_FORMAT_YUYV:
  917. case DRM_FORMAT_YVYU:
  918. return fill_tiles_yuv_packed(info, planes[0],
  919. width, height, stride);
  920. case DRM_FORMAT_NV12:
  921. case DRM_FORMAT_NV21:
  922. case DRM_FORMAT_NV16:
  923. case DRM_FORMAT_NV61:
  924. u = info->yuv.order & YUV_YCbCr ? planes[1] : planes[1] + 1;
  925. v = info->yuv.order & YUV_YCrCb ? planes[1] : planes[1] + 1;
  926. return fill_tiles_yuv_planar(info, planes[0], u, v,
  927. width, height, stride);
  928. case DRM_FORMAT_YUV420:
  929. return fill_tiles_yuv_planar(info, planes[0], planes[1],
  930. planes[2], width, height, stride);
  931. case DRM_FORMAT_YVU420:
  932. return fill_tiles_yuv_planar(info, planes[0], planes[2],
  933. planes[1], width, height, stride);
  934. case DRM_FORMAT_ARGB4444:
  935. case DRM_FORMAT_XRGB4444:
  936. case DRM_FORMAT_ABGR4444:
  937. case DRM_FORMAT_XBGR4444:
  938. case DRM_FORMAT_RGBA4444:
  939. case DRM_FORMAT_RGBX4444:
  940. case DRM_FORMAT_BGRA4444:
  941. case DRM_FORMAT_BGRX4444:
  942. case DRM_FORMAT_RGB565:
  943. case DRM_FORMAT_BGR565:
  944. case DRM_FORMAT_ARGB1555:
  945. case DRM_FORMAT_XRGB1555:
  946. case DRM_FORMAT_ABGR1555:
  947. case DRM_FORMAT_XBGR1555:
  948. case DRM_FORMAT_RGBA5551:
  949. case DRM_FORMAT_RGBX5551:
  950. case DRM_FORMAT_BGRA5551:
  951. case DRM_FORMAT_BGRX5551:
  952. return fill_tiles_rgb16(info, planes[0],
  953. width, height, stride);
  954. case DRM_FORMAT_BGR888:
  955. case DRM_FORMAT_RGB888:
  956. return fill_tiles_rgb24(info, planes[0],
  957. width, height, stride);
  958. case DRM_FORMAT_ARGB8888:
  959. case DRM_FORMAT_XRGB8888:
  960. case DRM_FORMAT_ABGR8888:
  961. case DRM_FORMAT_XBGR8888:
  962. case DRM_FORMAT_RGBA8888:
  963. case DRM_FORMAT_RGBX8888:
  964. case DRM_FORMAT_BGRA8888:
  965. case DRM_FORMAT_BGRX8888:
  966. case DRM_FORMAT_ARGB2101010:
  967. case DRM_FORMAT_XRGB2101010:
  968. case DRM_FORMAT_ABGR2101010:
  969. case DRM_FORMAT_XBGR2101010:
  970. case DRM_FORMAT_RGBA1010102:
  971. case DRM_FORMAT_RGBX1010102:
  972. case DRM_FORMAT_BGRA1010102:
  973. case DRM_FORMAT_BGRX1010102:
  974. return fill_tiles_rgb32(info, planes[0],
  975. width, height, stride);
  976. case DRM_FORMAT_XRGB16161616F:
  977. case DRM_FORMAT_XBGR16161616F:
  978. case DRM_FORMAT_ARGB16161616F:
  979. case DRM_FORMAT_ABGR16161616F:
  980. return fill_tiles_rgb16fp(info, planes[0],
  981. width, height, stride);
  982. }
  983. }
  984. static void fill_plain(const struct util_format_info *info, void *planes[3],
  985. unsigned int height,
  986. unsigned int stride)
  987. {
  988. switch (info->format) {
  989. case DRM_FORMAT_XRGB16161616F:
  990. case DRM_FORMAT_XBGR16161616F:
  991. case DRM_FORMAT_ARGB16161616F:
  992. case DRM_FORMAT_ABGR16161616F:
  993. /* 0x3838 = 0.5273 */
  994. memset(planes[0], 0x38, stride * height);
  995. break;
  996. default:
  997. memset(planes[0], 0x77, stride * height);
  998. break;
  999. }
  1000. }
  1001. static void fill_gradient_rgb32(const struct util_rgb_info *rgb,
  1002. void *mem,
  1003. unsigned int width, unsigned int height,
  1004. unsigned int stride)
  1005. {
  1006. int i, j;
  1007. for (i = 0; i < height / 2; i++) {
  1008. uint32_t *row = mem;
  1009. for (j = 0; j < width / 2; j++) {
  1010. uint32_t value = MAKE_RGBA10(rgb, j & 0x3ff, j & 0x3ff, j & 0x3ff, 0);
  1011. row[2*j] = row[2*j+1] = value;
  1012. }
  1013. mem += stride;
  1014. }
  1015. for (; i < height; i++) {
  1016. uint32_t *row = mem;
  1017. for (j = 0; j < width / 2; j++) {
  1018. uint32_t value = MAKE_RGBA10(rgb, j & 0x3fc, j & 0x3fc, j & 0x3fc, 0);
  1019. row[2*j] = row[2*j+1] = value;
  1020. }
  1021. mem += stride;
  1022. }
  1023. }
  1024. static void fill_gradient_rgb16fp(const struct util_rgb_info *rgb,
  1025. void *mem,
  1026. unsigned int width, unsigned int height,
  1027. unsigned int stride)
  1028. {
  1029. int i, j;
  1030. for (i = 0; i < height / 2; i++) {
  1031. uint64_t *row = mem;
  1032. for (j = 0; j < width / 2; j++) {
  1033. uint64_t value = MAKE_RGBA10FP16(rgb, j & 0x3ff, j & 0x3ff, j & 0x3ff, 0);
  1034. row[2*j] = row[2*j+1] = value;
  1035. }
  1036. mem += stride;
  1037. }
  1038. for (; i < height; i++) {
  1039. uint64_t *row = mem;
  1040. for (j = 0; j < width / 2; j++) {
  1041. uint64_t value = MAKE_RGBA10FP16(rgb, j & 0x3fc, j & 0x3fc, j & 0x3fc, 0);
  1042. row[2*j] = row[2*j+1] = value;
  1043. }
  1044. mem += stride;
  1045. }
  1046. }
  1047. /* The gradient pattern creates two horizontal gray gradients, split
  1048. * into two halves. The top half has 10bpc precision, the bottom half
  1049. * has 8bpc precision. When using with a 10bpc fb format, there are 3
  1050. * possible outcomes:
  1051. *
  1052. * - Pixel data is encoded as 8bpc to the display, no dithering. This
  1053. * would lead to the top and bottom halves looking identical.
  1054. *
  1055. * - Pixel data is encoded as 8bpc to the display, with dithering. This
  1056. * would lead to there being a visible difference between the two halves,
  1057. * but the top half would look a little speck-y due to the dithering.
  1058. *
  1059. * - Pixel data is encoded at 10bpc+ to the display (which implies
  1060. * the display is able to show this level of depth). This should
  1061. * lead to the top half being a very clean gradient, and visibly different
  1062. * from the bottom half.
  1063. *
  1064. * Once we support additional fb formats, this approach could be extended
  1065. * to distinguish even higher bpc precisions.
  1066. *
  1067. * Note that due to practical size considerations, for the screens
  1068. * where this matters, the pattern actually emits stripes 2-pixels
  1069. * wide for each gradient color. Otherwise the difference may be a bit
  1070. * hard to notice.
  1071. */
  1072. static void fill_gradient(const struct util_format_info *info, void *planes[3],
  1073. unsigned int width, unsigned int height,
  1074. unsigned int stride)
  1075. {
  1076. switch (info->format) {
  1077. case DRM_FORMAT_ARGB8888:
  1078. case DRM_FORMAT_XRGB8888:
  1079. case DRM_FORMAT_ABGR8888:
  1080. case DRM_FORMAT_XBGR8888:
  1081. case DRM_FORMAT_RGBA8888:
  1082. case DRM_FORMAT_RGBX8888:
  1083. case DRM_FORMAT_BGRA8888:
  1084. case DRM_FORMAT_BGRX8888:
  1085. case DRM_FORMAT_ARGB2101010:
  1086. case DRM_FORMAT_XRGB2101010:
  1087. case DRM_FORMAT_ABGR2101010:
  1088. case DRM_FORMAT_XBGR2101010:
  1089. case DRM_FORMAT_RGBA1010102:
  1090. case DRM_FORMAT_RGBX1010102:
  1091. case DRM_FORMAT_BGRA1010102:
  1092. case DRM_FORMAT_BGRX1010102:
  1093. return fill_gradient_rgb32(&info->rgb, planes[0],
  1094. width, height, stride);
  1095. case DRM_FORMAT_XRGB16161616F:
  1096. case DRM_FORMAT_XBGR16161616F:
  1097. case DRM_FORMAT_ARGB16161616F:
  1098. case DRM_FORMAT_ABGR16161616F:
  1099. return fill_gradient_rgb16fp(&info->rgb, planes[0],
  1100. width, height, stride);
  1101. }
  1102. }
  1103. /*
  1104. * util_fill_pattern - Fill a buffer with a test pattern
  1105. * @format: Pixel format
  1106. * @pattern: Test pattern
  1107. * @planes: Array of buffers
  1108. * @width: Width in pixels
  1109. * @height: Height in pixels
  1110. * @stride: Line stride (pitch) in bytes
  1111. *
  1112. * Fill the buffers with the test pattern specified by the pattern parameter.
  1113. * Supported formats vary depending on the selected pattern.
  1114. */
  1115. void util_fill_pattern(uint32_t format, enum util_fill_pattern pattern,
  1116. void *planes[3], unsigned int width,
  1117. unsigned int height, unsigned int stride)
  1118. {
  1119. const struct util_format_info *info;
  1120. info = util_format_info_find(format);
  1121. if (info == NULL)
  1122. return;
  1123. switch (pattern) {
  1124. case UTIL_PATTERN_TILES:
  1125. return fill_tiles(info, planes, width, height, stride);
  1126. case UTIL_PATTERN_SMPTE:
  1127. return fill_smpte(info, planes, width, height, stride);
  1128. case UTIL_PATTERN_PLAIN:
  1129. return fill_plain(info, planes, height, stride);
  1130. case UTIL_PATTERN_GRADIENT:
  1131. return fill_gradient(info, planes, width, height, stride);
  1132. default:
  1133. printf("Error: unsupported test pattern %u.\n", pattern);
  1134. break;
  1135. }
  1136. }
  1137. static const char *pattern_names[] = {
  1138. [UTIL_PATTERN_TILES] = "tiles",
  1139. [UTIL_PATTERN_SMPTE] = "smpte",
  1140. [UTIL_PATTERN_PLAIN] = "plain",
  1141. [UTIL_PATTERN_GRADIENT] = "gradient",
  1142. };
  1143. enum util_fill_pattern util_pattern_enum(const char *name)
  1144. {
  1145. unsigned int i;
  1146. for (i = 0; i < ARRAY_SIZE(pattern_names); i++)
  1147. if (!strcmp(pattern_names[i], name))
  1148. return (enum util_fill_pattern)i;
  1149. printf("Error: unsupported test pattern %s.\n", name);
  1150. return UTIL_PATTERN_SMPTE;
  1151. }