pattern.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212
  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 <assert.h>
  26. #include <stdbool.h>
  27. #include <stdint.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <time.h>
  32. #include <drm_fourcc.h>
  33. #if HAVE_CAIRO
  34. #include <cairo.h>
  35. #include <math.h>
  36. #endif
  37. #include "common.h"
  38. #include "format.h"
  39. #include "pattern.h"
  40. struct color_rgba {
  41. uint16_t red;
  42. uint16_t green;
  43. uint16_t blue;
  44. uint16_t alpha;
  45. };
  46. struct color_rgb24 {
  47. unsigned int value:24;
  48. } __attribute__((__packed__));
  49. struct color_yuv {
  50. unsigned char y;
  51. unsigned char u;
  52. unsigned char v;
  53. };
  54. #define MAKE_YUV_601_Y(r, g, b) \
  55. ((( 66 * (r) + 129 * (g) + 25 * (b) + 128) >> 8) + 16)
  56. #define MAKE_YUV_601_U(r, g, b) \
  57. (((-38 * (r) - 74 * (g) + 112 * (b) + 128) >> 8) + 128)
  58. #define MAKE_YUV_601_V(r, g, b) \
  59. (((112 * (r) - 94 * (g) - 18 * (b) + 128) >> 8) + 128)
  60. #define MAKE_YUV_601(r, g, b) \
  61. { .y = MAKE_YUV_601_Y(r, g, b), \
  62. .u = MAKE_YUV_601_U(r, g, b), \
  63. .v = MAKE_YUV_601_V(r, g, b) }
  64. static inline uint16_t swap16(uint16_t x)
  65. {
  66. return ((x & 0x00ffU) << 8) | ((x & 0xff00U) >> 8);
  67. }
  68. static inline uint32_t swap32(uint32_t x)
  69. {
  70. return ((x & 0x000000ffU) << 24) |
  71. ((x & 0x0000ff00U) << 8) |
  72. ((x & 0x00ff0000U) >> 8) |
  73. ((x & 0xff000000U) >> 24);
  74. }
  75. #ifdef HAVE_BIG_ENDIAN
  76. #define cpu_to_be16(x) (x)
  77. #define cpu_to_le16(x) swap16(x)
  78. #define cpu_to_le32(x) swap32(x)
  79. #define fb_foreign_endian(format) (!((format) & DRM_FORMAT_BIG_ENDIAN))
  80. #else
  81. #define cpu_to_be16(x) swap16(x)
  82. #define cpu_to_le16(x) (x)
  83. #define cpu_to_le32(x) (x)
  84. #define fb_foreign_endian(format) ((format) & DRM_FORMAT_BIG_ENDIAN)
  85. #endif
  86. #define cpu_to_fb16(x) (fb_be ? cpu_to_be16(x) : cpu_to_le16(x))
  87. /* This function takes 8-bit color values */
  88. static inline uint32_t shiftcolor8(const struct util_color_component *comp,
  89. uint32_t value)
  90. {
  91. value &= 0xff;
  92. /* Fill the low bits with the high bits. */
  93. value = (value << 8) | value;
  94. /* Shift down to remove unwanted low bits */
  95. value = value >> (16 - comp->length);
  96. /* Shift back up to where the value should be */
  97. return value << comp->offset;
  98. }
  99. /* This function takes 10-bit color values */
  100. static inline uint32_t shiftcolor10(const struct util_color_component *comp,
  101. uint32_t value)
  102. {
  103. value &= 0x3ff;
  104. /* Fill the low bits with the high bits. */
  105. value = (value << 6) | (value >> 4);
  106. /* Shift down to remove unwanted low bits */
  107. value = value >> (16 - comp->length);
  108. /* Shift back up to where the value should be */
  109. return value << comp->offset;
  110. }
  111. /* This function takes 16-bit color values */
  112. static inline uint64_t shiftcolor16(const struct util_color_component *comp,
  113. uint64_t value)
  114. {
  115. value &= 0xffff;
  116. /* Shift down to remove unwanted low bits */
  117. value = value >> (16 - comp->length);
  118. /* Shift back up to where the value should be */
  119. return value << comp->offset;
  120. }
  121. #define MAKE_RGBA10(rgb, r, g, b, a) \
  122. (shiftcolor10(&(rgb)->red, (r)) | \
  123. shiftcolor10(&(rgb)->green, (g)) | \
  124. shiftcolor10(&(rgb)->blue, (b)) | \
  125. shiftcolor10(&(rgb)->alpha, (a)))
  126. #define MAKE_RGBA(rgb, r, g, b, a) \
  127. (shiftcolor8(&(rgb)->red, (r)) | \
  128. shiftcolor8(&(rgb)->green, (g)) | \
  129. shiftcolor8(&(rgb)->blue, (b)) | \
  130. shiftcolor8(&(rgb)->alpha, (a)))
  131. #define MAKE_RGB24(rgb, r, g, b) \
  132. { .value = MAKE_RGBA(rgb, r, g, b, 0) }
  133. /**
  134. * Takes a uint16_t, divides by 65536, converts the infinite-precision
  135. * result to fp16 with round-to-zero.
  136. *
  137. * Copied from mesa:src/util/half_float.c
  138. */
  139. static uint16_t uint16_div_64k_to_half(uint16_t v)
  140. {
  141. /* Zero or subnormal. Set the mantissa to (v << 8) and return. */
  142. if (v < 4)
  143. return v << 8;
  144. /* Count the leading 0s in the uint16_t */
  145. int n = __builtin_clz(v) - 16;
  146. /* Shift the mantissa up so bit 16 is the hidden 1 bit,
  147. * mask it off, then shift back down to 10 bits
  148. */
  149. int m = ( ((uint32_t)v << (n + 1)) & 0xffff ) >> 6;
  150. /* (0{n} 1 X{15-n}) * 2^-16
  151. * = 1.X * 2^(15-n-16)
  152. * = 1.X * 2^(14-n - 15)
  153. * which is the FP16 form with e = 14 - n
  154. */
  155. int e = 14 - n;
  156. return (e << 10) | m;
  157. }
  158. #define MAKE_RGBA8FP16(rgb, r, g, b, a) \
  159. (shiftcolor16(&(rgb)->red, uint16_div_64k_to_half((r) << 8)) | \
  160. shiftcolor16(&(rgb)->green, uint16_div_64k_to_half((g) << 8)) | \
  161. shiftcolor16(&(rgb)->blue, uint16_div_64k_to_half((b) << 8)) | \
  162. shiftcolor16(&(rgb)->alpha, uint16_div_64k_to_half((a) << 8)))
  163. #define MAKE_RGBA10FP16(rgb, r, g, b, a) \
  164. (shiftcolor16(&(rgb)->red, uint16_div_64k_to_half((r) << 6)) | \
  165. shiftcolor16(&(rgb)->green, uint16_div_64k_to_half((g) << 6)) | \
  166. shiftcolor16(&(rgb)->blue, uint16_div_64k_to_half((b) << 6)) | \
  167. shiftcolor16(&(rgb)->alpha, uint16_div_64k_to_half((a) << 6)))
  168. static void fill_smpte_yuv_planar(const struct util_yuv_info *yuv,
  169. unsigned char *y_mem, unsigned char *u_mem,
  170. unsigned char *v_mem, unsigned int width,
  171. unsigned int height, unsigned int stride)
  172. {
  173. const struct color_yuv colors_top[] = {
  174. MAKE_YUV_601(192, 192, 192), /* grey */
  175. MAKE_YUV_601(192, 192, 0), /* yellow */
  176. MAKE_YUV_601(0, 192, 192), /* cyan */
  177. MAKE_YUV_601(0, 192, 0), /* green */
  178. MAKE_YUV_601(192, 0, 192), /* magenta */
  179. MAKE_YUV_601(192, 0, 0), /* red */
  180. MAKE_YUV_601(0, 0, 192), /* blue */
  181. };
  182. const struct color_yuv colors_middle[] = {
  183. MAKE_YUV_601(0, 0, 192), /* blue */
  184. MAKE_YUV_601(19, 19, 19), /* black */
  185. MAKE_YUV_601(192, 0, 192), /* magenta */
  186. MAKE_YUV_601(19, 19, 19), /* black */
  187. MAKE_YUV_601(0, 192, 192), /* cyan */
  188. MAKE_YUV_601(19, 19, 19), /* black */
  189. MAKE_YUV_601(192, 192, 192), /* grey */
  190. };
  191. const struct color_yuv colors_bottom[] = {
  192. MAKE_YUV_601(0, 33, 76), /* in-phase */
  193. MAKE_YUV_601(255, 255, 255), /* super white */
  194. MAKE_YUV_601(50, 0, 106), /* quadrature */
  195. MAKE_YUV_601(19, 19, 19), /* black */
  196. MAKE_YUV_601(9, 9, 9), /* 3.5% */
  197. MAKE_YUV_601(19, 19, 19), /* 7.5% */
  198. MAKE_YUV_601(29, 29, 29), /* 11.5% */
  199. MAKE_YUV_601(19, 19, 19), /* black */
  200. };
  201. unsigned int cs = yuv->chroma_stride;
  202. unsigned int xsub = yuv->xsub;
  203. unsigned int ysub = yuv->ysub;
  204. unsigned int x;
  205. unsigned int y;
  206. /* Luma */
  207. for (y = 0; y < height * 6 / 9; ++y) {
  208. for (x = 0; x < width; ++x)
  209. y_mem[x] = colors_top[x * 7 / width].y;
  210. y_mem += stride;
  211. }
  212. for (; y < height * 7 / 9; ++y) {
  213. for (x = 0; x < width; ++x)
  214. y_mem[x] = colors_middle[x * 7 / width].y;
  215. y_mem += stride;
  216. }
  217. for (; y < height; ++y) {
  218. for (x = 0; x < width * 5 / 7; ++x)
  219. y_mem[x] = colors_bottom[x * 4 / (width * 5 / 7)].y;
  220. for (; x < width * 6 / 7; ++x)
  221. y_mem[x] = colors_bottom[(x - width * 5 / 7) * 3
  222. / (width / 7) + 4].y;
  223. for (; x < width; ++x)
  224. y_mem[x] = colors_bottom[7].y;
  225. y_mem += stride;
  226. }
  227. /* Chroma */
  228. for (y = 0; y < height / ysub * 6 / 9; ++y) {
  229. for (x = 0; x < width; x += xsub) {
  230. u_mem[x*cs/xsub] = colors_top[x * 7 / width].u;
  231. v_mem[x*cs/xsub] = colors_top[x * 7 / width].v;
  232. }
  233. u_mem += stride * cs / xsub;
  234. v_mem += stride * cs / xsub;
  235. }
  236. for (; y < height / ysub * 7 / 9; ++y) {
  237. for (x = 0; x < width; x += xsub) {
  238. u_mem[x*cs/xsub] = colors_middle[x * 7 / width].u;
  239. v_mem[x*cs/xsub] = colors_middle[x * 7 / width].v;
  240. }
  241. u_mem += stride * cs / xsub;
  242. v_mem += stride * cs / xsub;
  243. }
  244. for (; y < height / ysub; ++y) {
  245. for (x = 0; x < width * 5 / 7; x += xsub) {
  246. u_mem[x*cs/xsub] =
  247. colors_bottom[x * 4 / (width * 5 / 7)].u;
  248. v_mem[x*cs/xsub] =
  249. colors_bottom[x * 4 / (width * 5 / 7)].v;
  250. }
  251. for (; x < width * 6 / 7; x += xsub) {
  252. u_mem[x*cs/xsub] = colors_bottom[(x - width * 5 / 7) *
  253. 3 / (width / 7) + 4].u;
  254. v_mem[x*cs/xsub] = colors_bottom[(x - width * 5 / 7) *
  255. 3 / (width / 7) + 4].v;
  256. }
  257. for (; x < width; x += xsub) {
  258. u_mem[x*cs/xsub] = colors_bottom[7].u;
  259. v_mem[x*cs/xsub] = colors_bottom[7].v;
  260. }
  261. u_mem += stride * cs / xsub;
  262. v_mem += stride * cs / xsub;
  263. }
  264. }
  265. static void write_pixels_10bpp(unsigned char *mem,
  266. unsigned short a,
  267. unsigned short b,
  268. unsigned short c,
  269. unsigned short d)
  270. {
  271. mem[0] = (a & 0xff);
  272. mem[1] = ((a >> 8) & 0x3) | ((b & 0x3f) << 2);
  273. mem[2] = ((b >> 6) & 0xf) | ((c & 0xf) << 4);
  274. mem[3] = ((c >> 4) & 0x3f) | ((d & 0x3) << 6);
  275. mem[4] = ((d >> 2) & 0xff);
  276. }
  277. static void update_pixels_10bpp(unsigned char *mem, uint64_t val, uint64_t mask)
  278. {
  279. int i;
  280. for (i = 0; i < 5; i++, mask >>= 8, val >>= 8) {
  281. mem[i] &= ~(mask & 0xff);
  282. mem[i] |= (mask & 0xff) & val;
  283. }
  284. }
  285. static void fill_smpte_yuv_planar_10bpp(const struct util_yuv_info *yuv,
  286. unsigned char *y_mem,
  287. unsigned char *uv_mem,
  288. unsigned int width,
  289. unsigned int height,
  290. unsigned int stride)
  291. {
  292. const struct color_yuv colors_top[] = {
  293. MAKE_YUV_601(192, 192, 192), /* grey */
  294. MAKE_YUV_601(192, 192, 0), /* yellow */
  295. MAKE_YUV_601(0, 192, 192), /* cyan */
  296. MAKE_YUV_601(0, 192, 0), /* green */
  297. MAKE_YUV_601(192, 0, 192), /* magenta */
  298. MAKE_YUV_601(192, 0, 0), /* red */
  299. MAKE_YUV_601(0, 0, 192), /* blue */
  300. };
  301. const struct color_yuv colors_middle[] = {
  302. MAKE_YUV_601(0, 0, 192), /* blue */
  303. MAKE_YUV_601(19, 19, 19), /* black */
  304. MAKE_YUV_601(192, 0, 192), /* magenta */
  305. MAKE_YUV_601(19, 19, 19), /* black */
  306. MAKE_YUV_601(0, 192, 192), /* cyan */
  307. MAKE_YUV_601(19, 19, 19), /* black */
  308. MAKE_YUV_601(192, 192, 192), /* grey */
  309. };
  310. const struct color_yuv colors_bottom[] = {
  311. MAKE_YUV_601(0, 33, 76), /* in-phase */
  312. MAKE_YUV_601(255, 255, 255), /* super white */
  313. MAKE_YUV_601(50, 0, 106), /* quadrature */
  314. MAKE_YUV_601(19, 19, 19), /* black */
  315. MAKE_YUV_601(9, 9, 9), /* 3.5% */
  316. MAKE_YUV_601(19, 19, 19), /* 7.5% */
  317. MAKE_YUV_601(29, 29, 29), /* 11.5% */
  318. MAKE_YUV_601(19, 19, 19), /* black */
  319. };
  320. unsigned int cs = yuv->chroma_stride;
  321. unsigned int xsub = yuv->xsub;
  322. unsigned int ysub = yuv->ysub;
  323. unsigned int xstep = cs * xsub;
  324. unsigned int x;
  325. unsigned int y;
  326. /* Luma */
  327. for (y = 0; y < height * 6 / 9; ++y) {
  328. for (x = 0; x < width; x += 4)
  329. write_pixels_10bpp(&y_mem[(x * 5) / 4],
  330. colors_top[(x+0) * 7 / width].y << 2,
  331. colors_top[(x+1) * 7 / width].y << 2,
  332. colors_top[(x+2) * 7 / width].y << 2,
  333. colors_top[(x+3) * 7 / width].y << 2);
  334. y_mem += stride;
  335. }
  336. for (; y < height * 7 / 9; ++y) {
  337. for (x = 0; x < width; x += 4)
  338. write_pixels_10bpp(&y_mem[(x * 5) / 4],
  339. colors_middle[(x+0) * 7 / width].y << 2,
  340. colors_middle[(x+1) * 7 / width].y << 2,
  341. colors_middle[(x+2) * 7 / width].y << 2,
  342. colors_middle[(x+3) * 7 / width].y << 2);
  343. y_mem += stride;
  344. }
  345. for (; y < height; ++y) {
  346. for (x = 0; x < width * 5 / 7; x += 4)
  347. write_pixels_10bpp(&y_mem[(x * 5) / 4],
  348. colors_bottom[(x+0) * 4 / (width * 5 / 7)].y << 2,
  349. colors_bottom[(x+1) * 4 / (width * 5 / 7)].y << 2,
  350. colors_bottom[(x+2) * 4 / (width * 5 / 7)].y << 2,
  351. colors_bottom[(x+3) * 4 / (width * 5 / 7)].y << 2);
  352. for (; x < width * 6 / 7; x += 4)
  353. write_pixels_10bpp(&y_mem[(x * 5) / 4],
  354. colors_bottom[((x+0) - width * 5 / 7) * 3 / (width / 7) + 4].y << 2,
  355. colors_bottom[((x+1) - width * 5 / 7) * 3 / (width / 7) + 4].y << 2,
  356. colors_bottom[((x+2) - width * 5 / 7) * 3 / (width / 7) + 4].y << 2,
  357. colors_bottom[((x+3) - width * 5 / 7) * 3 / (width / 7) + 4].y << 2);
  358. for (; x < width; x += 4)
  359. write_pixels_10bpp(&y_mem[(x * 5) / 4],
  360. colors_bottom[7].y << 2,
  361. colors_bottom[7].y << 2,
  362. colors_bottom[7].y << 2,
  363. colors_bottom[7].y << 2);
  364. y_mem += stride;
  365. }
  366. /* Chroma */
  367. for (y = 0; y < height * 6 / 9; y += ysub) {
  368. for (x = 0; x < width; x += xstep)
  369. write_pixels_10bpp(&uv_mem[(x * 5) / xstep],
  370. colors_top[(x+0) * 7 / width].u << 2,
  371. colors_top[(x+0) * 7 / width].v << 2,
  372. colors_top[(x+xsub) * 7 / width].u << 2,
  373. colors_top[(x+xsub) * 7 / width].v << 2);
  374. uv_mem += stride * cs / xsub;
  375. }
  376. for (; y < height * 7 / 9; y += ysub) {
  377. for (x = 0; x < width; x += xstep)
  378. write_pixels_10bpp(&uv_mem[(x * 5) / xstep],
  379. colors_middle[(x+0) * 7 / width].u << 2,
  380. colors_middle[(x+0) * 7 / width].v << 2,
  381. colors_middle[(x+xsub) * 7 / width].u << 2,
  382. colors_middle[(x+xsub) * 7 / width].v << 2);
  383. uv_mem += stride * cs / xsub;
  384. }
  385. for (; y < height; y += ysub) {
  386. for (x = 0; x < width * 5 / 7; x += xstep)
  387. write_pixels_10bpp(&uv_mem[(x * 5) / xstep],
  388. colors_bottom[(x+0) * 4 / (width * 5 / 7)].u << 2,
  389. colors_bottom[(x+0) * 4 / (width * 5 / 7)].v << 2,
  390. colors_bottom[(x+xsub) * 4 / (width * 5 / 7)].u << 2,
  391. colors_bottom[(x+xsub) * 4 / (width * 5 / 7)].v << 2);
  392. for (; x < width * 6 / 7; x += xstep)
  393. write_pixels_10bpp(&uv_mem[(x * 5) / xstep],
  394. colors_bottom[((x+0) - width * 5 / 7) * 3 / (width / 7) + 4].u << 2,
  395. colors_bottom[((x+0) - width * 5 / 7) * 3 / (width / 7) + 4].v << 2,
  396. colors_bottom[((x+xsub) - width * 5 / 7) * 3 / (width / 7) + 4].u << 2,
  397. colors_bottom[((x+xsub) - width * 5 / 7) * 3 / (width / 7) + 4].v << 2);
  398. for (; x < width; x += xstep)
  399. write_pixels_10bpp(&uv_mem[(x * 5) / xstep],
  400. colors_bottom[7].u << 2,
  401. colors_bottom[7].v << 2,
  402. colors_bottom[7].u << 2,
  403. colors_bottom[7].v << 2);
  404. uv_mem += stride * cs / xsub;
  405. }
  406. }
  407. static void fill_smpte_yuv_packed(const struct util_yuv_info *yuv, void *mem,
  408. unsigned int width, unsigned int height,
  409. unsigned int stride)
  410. {
  411. const struct color_yuv colors_top[] = {
  412. MAKE_YUV_601(192, 192, 192), /* grey */
  413. MAKE_YUV_601(192, 192, 0), /* yellow */
  414. MAKE_YUV_601(0, 192, 192), /* cyan */
  415. MAKE_YUV_601(0, 192, 0), /* green */
  416. MAKE_YUV_601(192, 0, 192), /* magenta */
  417. MAKE_YUV_601(192, 0, 0), /* red */
  418. MAKE_YUV_601(0, 0, 192), /* blue */
  419. };
  420. const struct color_yuv colors_middle[] = {
  421. MAKE_YUV_601(0, 0, 192), /* blue */
  422. MAKE_YUV_601(19, 19, 19), /* black */
  423. MAKE_YUV_601(192, 0, 192), /* magenta */
  424. MAKE_YUV_601(19, 19, 19), /* black */
  425. MAKE_YUV_601(0, 192, 192), /* cyan */
  426. MAKE_YUV_601(19, 19, 19), /* black */
  427. MAKE_YUV_601(192, 192, 192), /* grey */
  428. };
  429. const struct color_yuv colors_bottom[] = {
  430. MAKE_YUV_601(0, 33, 76), /* in-phase */
  431. MAKE_YUV_601(255, 255, 255), /* super white */
  432. MAKE_YUV_601(50, 0, 106), /* quadrature */
  433. MAKE_YUV_601(19, 19, 19), /* black */
  434. MAKE_YUV_601(9, 9, 9), /* 3.5% */
  435. MAKE_YUV_601(19, 19, 19), /* 7.5% */
  436. MAKE_YUV_601(29, 29, 29), /* 11.5% */
  437. MAKE_YUV_601(19, 19, 19), /* black */
  438. };
  439. unsigned char *y_mem = (yuv->order & YUV_YC) ? mem : mem + 1;
  440. unsigned char *c_mem = (yuv->order & YUV_CY) ? mem : mem + 1;
  441. unsigned int u = (yuv->order & YUV_YCrCb) ? 2 : 0;
  442. unsigned int v = (yuv->order & YUV_YCbCr) ? 2 : 0;
  443. unsigned int x;
  444. unsigned int y;
  445. /* Luma */
  446. for (y = 0; y < height * 6 / 9; ++y) {
  447. for (x = 0; x < width; ++x)
  448. y_mem[2*x] = colors_top[x * 7 / width].y;
  449. y_mem += stride;
  450. }
  451. for (; y < height * 7 / 9; ++y) {
  452. for (x = 0; x < width; ++x)
  453. y_mem[2*x] = colors_middle[x * 7 / width].y;
  454. y_mem += stride;
  455. }
  456. for (; y < height; ++y) {
  457. for (x = 0; x < width * 5 / 7; ++x)
  458. y_mem[2*x] = colors_bottom[x * 4 / (width * 5 / 7)].y;
  459. for (; x < width * 6 / 7; ++x)
  460. y_mem[2*x] = colors_bottom[(x - width * 5 / 7) * 3
  461. / (width / 7) + 4].y;
  462. for (; x < width; ++x)
  463. y_mem[2*x] = colors_bottom[7].y;
  464. y_mem += stride;
  465. }
  466. /* Chroma */
  467. for (y = 0; y < height * 6 / 9; ++y) {
  468. for (x = 0; x < width; x += 2) {
  469. c_mem[2*x+u] = colors_top[x * 7 / width].u;
  470. c_mem[2*x+v] = colors_top[x * 7 / width].v;
  471. }
  472. c_mem += stride;
  473. }
  474. for (; y < height * 7 / 9; ++y) {
  475. for (x = 0; x < width; x += 2) {
  476. c_mem[2*x+u] = colors_middle[x * 7 / width].u;
  477. c_mem[2*x+v] = colors_middle[x * 7 / width].v;
  478. }
  479. c_mem += stride;
  480. }
  481. for (; y < height; ++y) {
  482. for (x = 0; x < width * 5 / 7; x += 2) {
  483. c_mem[2*x+u] = colors_bottom[x * 4 / (width * 5 / 7)].u;
  484. c_mem[2*x+v] = colors_bottom[x * 4 / (width * 5 / 7)].v;
  485. }
  486. for (; x < width * 6 / 7; x += 2) {
  487. c_mem[2*x+u] = colors_bottom[(x - width * 5 / 7) *
  488. 3 / (width / 7) + 4].u;
  489. c_mem[2*x+v] = colors_bottom[(x - width * 5 / 7) *
  490. 3 / (width / 7) + 4].v;
  491. }
  492. for (; x < width; x += 2) {
  493. c_mem[2*x+u] = colors_bottom[7].u;
  494. c_mem[2*x+v] = colors_bottom[7].v;
  495. }
  496. c_mem += stride;
  497. }
  498. }
  499. static void fill_smpte_rgb16(const struct util_rgb_info *rgb, void *mem,
  500. unsigned int width, unsigned int height,
  501. unsigned int stride, bool fb_be)
  502. {
  503. const uint16_t colors_top[] = {
  504. MAKE_RGBA(rgb, 192, 192, 192, 255), /* grey */
  505. MAKE_RGBA(rgb, 192, 192, 0, 255), /* yellow */
  506. MAKE_RGBA(rgb, 0, 192, 192, 255), /* cyan */
  507. MAKE_RGBA(rgb, 0, 192, 0, 255), /* green */
  508. MAKE_RGBA(rgb, 192, 0, 192, 255), /* magenta */
  509. MAKE_RGBA(rgb, 192, 0, 0, 255), /* red */
  510. MAKE_RGBA(rgb, 0, 0, 192, 255), /* blue */
  511. };
  512. const uint16_t colors_middle[] = {
  513. MAKE_RGBA(rgb, 0, 0, 192, 127), /* blue */
  514. MAKE_RGBA(rgb, 19, 19, 19, 127), /* black */
  515. MAKE_RGBA(rgb, 192, 0, 192, 127), /* magenta */
  516. MAKE_RGBA(rgb, 19, 19, 19, 127), /* black */
  517. MAKE_RGBA(rgb, 0, 192, 192, 127), /* cyan */
  518. MAKE_RGBA(rgb, 19, 19, 19, 127), /* black */
  519. MAKE_RGBA(rgb, 192, 192, 192, 127), /* grey */
  520. };
  521. const uint16_t colors_bottom[] = {
  522. MAKE_RGBA(rgb, 0, 33, 76, 255), /* in-phase */
  523. MAKE_RGBA(rgb, 255, 255, 255, 255), /* super white */
  524. MAKE_RGBA(rgb, 50, 0, 106, 255), /* quadrature */
  525. MAKE_RGBA(rgb, 19, 19, 19, 255), /* black */
  526. MAKE_RGBA(rgb, 9, 9, 9, 255), /* 3.5% */
  527. MAKE_RGBA(rgb, 19, 19, 19, 255), /* 7.5% */
  528. MAKE_RGBA(rgb, 29, 29, 29, 255), /* 11.5% */
  529. MAKE_RGBA(rgb, 19, 19, 19, 255), /* black */
  530. };
  531. unsigned int x;
  532. unsigned int y;
  533. for (y = 0; y < height * 6 / 9; ++y) {
  534. for (x = 0; x < width; ++x)
  535. ((uint16_t *)mem)[x] = cpu_to_fb16(colors_top[x * 7 / width]);
  536. mem += stride;
  537. }
  538. for (; y < height * 7 / 9; ++y) {
  539. for (x = 0; x < width; ++x)
  540. ((uint16_t *)mem)[x] = cpu_to_fb16(colors_middle[x * 7 / width]);
  541. mem += stride;
  542. }
  543. for (; y < height; ++y) {
  544. for (x = 0; x < width * 5 / 7; ++x)
  545. ((uint16_t *)mem)[x] =
  546. cpu_to_fb16(colors_bottom[x * 4 / (width * 5 / 7)]);
  547. for (; x < width * 6 / 7; ++x)
  548. ((uint16_t *)mem)[x] =
  549. cpu_to_fb16(colors_bottom[(x - width * 5 / 7) * 3
  550. / (width / 7) + 4]);
  551. for (; x < width; ++x)
  552. ((uint16_t *)mem)[x] = cpu_to_fb16(colors_bottom[7]);
  553. mem += stride;
  554. }
  555. }
  556. static void fill_smpte_rgb24(const struct util_rgb_info *rgb, void *mem,
  557. unsigned int width, unsigned int height,
  558. unsigned int stride)
  559. {
  560. const struct color_rgb24 colors_top[] = {
  561. MAKE_RGB24(rgb, 192, 192, 192), /* grey */
  562. MAKE_RGB24(rgb, 192, 192, 0), /* yellow */
  563. MAKE_RGB24(rgb, 0, 192, 192), /* cyan */
  564. MAKE_RGB24(rgb, 0, 192, 0), /* green */
  565. MAKE_RGB24(rgb, 192, 0, 192), /* magenta */
  566. MAKE_RGB24(rgb, 192, 0, 0), /* red */
  567. MAKE_RGB24(rgb, 0, 0, 192), /* blue */
  568. };
  569. const struct color_rgb24 colors_middle[] = {
  570. MAKE_RGB24(rgb, 0, 0, 192), /* blue */
  571. MAKE_RGB24(rgb, 19, 19, 19), /* black */
  572. MAKE_RGB24(rgb, 192, 0, 192), /* magenta */
  573. MAKE_RGB24(rgb, 19, 19, 19), /* black */
  574. MAKE_RGB24(rgb, 0, 192, 192), /* cyan */
  575. MAKE_RGB24(rgb, 19, 19, 19), /* black */
  576. MAKE_RGB24(rgb, 192, 192, 192), /* grey */
  577. };
  578. const struct color_rgb24 colors_bottom[] = {
  579. MAKE_RGB24(rgb, 0, 33, 76), /* in-phase */
  580. MAKE_RGB24(rgb, 255, 255, 255), /* super white */
  581. MAKE_RGB24(rgb, 50, 0, 106), /* quadrature */
  582. MAKE_RGB24(rgb, 19, 19, 19), /* black */
  583. MAKE_RGB24(rgb, 9, 9, 9), /* 3.5% */
  584. MAKE_RGB24(rgb, 19, 19, 19), /* 7.5% */
  585. MAKE_RGB24(rgb, 29, 29, 29), /* 11.5% */
  586. MAKE_RGB24(rgb, 19, 19, 19), /* black */
  587. };
  588. unsigned int x;
  589. unsigned int y;
  590. for (y = 0; y < height * 6 / 9; ++y) {
  591. for (x = 0; x < width; ++x)
  592. ((struct color_rgb24 *)mem)[x] =
  593. colors_top[x * 7 / width];
  594. mem += stride;
  595. }
  596. for (; y < height * 7 / 9; ++y) {
  597. for (x = 0; x < width; ++x)
  598. ((struct color_rgb24 *)mem)[x] =
  599. colors_middle[x * 7 / width];
  600. mem += stride;
  601. }
  602. for (; y < height; ++y) {
  603. for (x = 0; x < width * 5 / 7; ++x)
  604. ((struct color_rgb24 *)mem)[x] =
  605. colors_bottom[x * 4 / (width * 5 / 7)];
  606. for (; x < width * 6 / 7; ++x)
  607. ((struct color_rgb24 *)mem)[x] =
  608. colors_bottom[(x - width * 5 / 7) * 3
  609. / (width / 7) + 4];
  610. for (; x < width; ++x)
  611. ((struct color_rgb24 *)mem)[x] = colors_bottom[7];
  612. mem += stride;
  613. }
  614. }
  615. static void fill_smpte_rgb32(const struct util_rgb_info *rgb, void *mem,
  616. unsigned int width, unsigned int height,
  617. unsigned int stride)
  618. {
  619. const uint32_t colors_top[] = {
  620. MAKE_RGBA(rgb, 192, 192, 192, 255), /* grey */
  621. MAKE_RGBA(rgb, 192, 192, 0, 255), /* yellow */
  622. MAKE_RGBA(rgb, 0, 192, 192, 255), /* cyan */
  623. MAKE_RGBA(rgb, 0, 192, 0, 255), /* green */
  624. MAKE_RGBA(rgb, 192, 0, 192, 255), /* magenta */
  625. MAKE_RGBA(rgb, 192, 0, 0, 255), /* red */
  626. MAKE_RGBA(rgb, 0, 0, 192, 255), /* blue */
  627. };
  628. const uint32_t colors_middle[] = {
  629. MAKE_RGBA(rgb, 0, 0, 192, 127), /* blue */
  630. MAKE_RGBA(rgb, 19, 19, 19, 127), /* black */
  631. MAKE_RGBA(rgb, 192, 0, 192, 127), /* magenta */
  632. MAKE_RGBA(rgb, 19, 19, 19, 127), /* black */
  633. MAKE_RGBA(rgb, 0, 192, 192, 127), /* cyan */
  634. MAKE_RGBA(rgb, 19, 19, 19, 127), /* black */
  635. MAKE_RGBA(rgb, 192, 192, 192, 127), /* grey */
  636. };
  637. const uint32_t colors_bottom[] = {
  638. MAKE_RGBA(rgb, 0, 33, 76, 255), /* in-phase */
  639. MAKE_RGBA(rgb, 255, 255, 255, 255), /* super white */
  640. MAKE_RGBA(rgb, 50, 0, 106, 255), /* quadrature */
  641. MAKE_RGBA(rgb, 19, 19, 19, 255), /* black */
  642. MAKE_RGBA(rgb, 9, 9, 9, 255), /* 3.5% */
  643. MAKE_RGBA(rgb, 19, 19, 19, 255), /* 7.5% */
  644. MAKE_RGBA(rgb, 29, 29, 29, 255), /* 11.5% */
  645. MAKE_RGBA(rgb, 19, 19, 19, 255), /* black */
  646. };
  647. unsigned int x;
  648. unsigned int y;
  649. for (y = 0; y < height * 6 / 9; ++y) {
  650. for (x = 0; x < width; ++x)
  651. ((uint32_t *)mem)[x] = cpu_to_le32(colors_top[x * 7 / width]);
  652. mem += stride;
  653. }
  654. for (; y < height * 7 / 9; ++y) {
  655. for (x = 0; x < width; ++x)
  656. ((uint32_t *)mem)[x] = cpu_to_le32(colors_middle[x * 7 / width]);
  657. mem += stride;
  658. }
  659. for (; y < height; ++y) {
  660. for (x = 0; x < width * 5 / 7; ++x)
  661. ((uint32_t *)mem)[x] =
  662. cpu_to_le32(colors_bottom[x * 4 / (width * 5 / 7)]);
  663. for (; x < width * 6 / 7; ++x)
  664. ((uint32_t *)mem)[x] =
  665. cpu_to_le32(colors_bottom[(x - width * 5 / 7) * 3
  666. / (width / 7) + 4]);
  667. for (; x < width; ++x)
  668. ((uint32_t *)mem)[x] = cpu_to_le32(colors_bottom[7]);
  669. mem += stride;
  670. }
  671. }
  672. static void fill_smpte_rgb16fp(const struct util_rgb_info *rgb, void *mem,
  673. unsigned int width, unsigned int height,
  674. unsigned int stride)
  675. {
  676. const uint64_t colors_top[] = {
  677. MAKE_RGBA8FP16(rgb, 192, 192, 192, 255),/* grey */
  678. MAKE_RGBA8FP16(rgb, 192, 192, 0, 255), /* yellow */
  679. MAKE_RGBA8FP16(rgb, 0, 192, 192, 255), /* cyan */
  680. MAKE_RGBA8FP16(rgb, 0, 192, 0, 255), /* green */
  681. MAKE_RGBA8FP16(rgb, 192, 0, 192, 255), /* magenta */
  682. MAKE_RGBA8FP16(rgb, 192, 0, 0, 255), /* red */
  683. MAKE_RGBA8FP16(rgb, 0, 0, 192, 255), /* blue */
  684. };
  685. const uint64_t colors_middle[] = {
  686. MAKE_RGBA8FP16(rgb, 0, 0, 192, 127), /* blue */
  687. MAKE_RGBA8FP16(rgb, 19, 19, 19, 127), /* black */
  688. MAKE_RGBA8FP16(rgb, 192, 0, 192, 127), /* magenta */
  689. MAKE_RGBA8FP16(rgb, 19, 19, 19, 127), /* black */
  690. MAKE_RGBA8FP16(rgb, 0, 192, 192, 127), /* cyan */
  691. MAKE_RGBA8FP16(rgb, 19, 19, 19, 127), /* black */
  692. MAKE_RGBA8FP16(rgb, 192, 192, 192, 127),/* grey */
  693. };
  694. const uint64_t colors_bottom[] = {
  695. MAKE_RGBA8FP16(rgb, 0, 33, 76, 255), /* in-phase */
  696. MAKE_RGBA8FP16(rgb, 255, 255, 255, 255),/* super white */
  697. MAKE_RGBA8FP16(rgb, 50, 0, 106, 255), /* quadrature */
  698. MAKE_RGBA8FP16(rgb, 19, 19, 19, 255), /* black */
  699. MAKE_RGBA8FP16(rgb, 9, 9, 9, 255), /* 3.5% */
  700. MAKE_RGBA8FP16(rgb, 19, 19, 19, 255), /* 7.5% */
  701. MAKE_RGBA8FP16(rgb, 29, 29, 29, 255), /* 11.5% */
  702. MAKE_RGBA8FP16(rgb, 19, 19, 19, 255), /* black */
  703. };
  704. unsigned int x;
  705. unsigned int y;
  706. for (y = 0; y < height * 6 / 9; ++y) {
  707. for (x = 0; x < width; ++x)
  708. ((uint64_t *)mem)[x] = colors_top[x * 7 / width];
  709. mem += stride;
  710. }
  711. for (; y < height * 7 / 9; ++y) {
  712. for (x = 0; x < width; ++x)
  713. ((uint64_t *)mem)[x] = colors_middle[x * 7 / width];
  714. mem += stride;
  715. }
  716. for (; y < height; ++y) {
  717. for (x = 0; x < width * 5 / 7; ++x)
  718. ((uint64_t *)mem)[x] =
  719. colors_bottom[x * 4 / (width * 5 / 7)];
  720. for (; x < width * 6 / 7; ++x)
  721. ((uint64_t *)mem)[x] =
  722. colors_bottom[(x - width * 5 / 7) * 3
  723. / (width / 7) + 4];
  724. for (; x < width; ++x)
  725. ((uint64_t *)mem)[x] = colors_bottom[7];
  726. mem += stride;
  727. }
  728. }
  729. enum smpte_colors {
  730. SMPTE_COLOR_GREY,
  731. SMPTE_COLOR_YELLOW,
  732. SMPTE_COLOR_CYAN,
  733. SMPTE_COLOR_GREEN,
  734. SMPTE_COLOR_MAGENTA,
  735. SMPTE_COLOR_RED,
  736. SMPTE_COLOR_BLUE,
  737. SMPTE_COLOR_BLACK,
  738. SMPTE_COLOR_IN_PHASE,
  739. SMPTE_COLOR_SUPER_WHITE,
  740. SMPTE_COLOR_QUADRATURE,
  741. SMPTE_COLOR_3PC5,
  742. SMPTE_COLOR_11PC5,
  743. };
  744. static unsigned int smpte_top[7] = {
  745. SMPTE_COLOR_GREY,
  746. SMPTE_COLOR_YELLOW,
  747. SMPTE_COLOR_CYAN,
  748. SMPTE_COLOR_GREEN,
  749. SMPTE_COLOR_MAGENTA,
  750. SMPTE_COLOR_RED,
  751. SMPTE_COLOR_BLUE,
  752. };
  753. static unsigned int smpte_middle[7] = {
  754. SMPTE_COLOR_BLUE,
  755. SMPTE_COLOR_BLACK,
  756. SMPTE_COLOR_MAGENTA,
  757. SMPTE_COLOR_BLACK,
  758. SMPTE_COLOR_CYAN,
  759. SMPTE_COLOR_BLACK,
  760. SMPTE_COLOR_GREY,
  761. };
  762. static unsigned int smpte_bottom[8] = {
  763. SMPTE_COLOR_IN_PHASE,
  764. SMPTE_COLOR_SUPER_WHITE,
  765. SMPTE_COLOR_QUADRATURE,
  766. SMPTE_COLOR_BLACK,
  767. SMPTE_COLOR_3PC5,
  768. SMPTE_COLOR_BLACK,
  769. SMPTE_COLOR_11PC5,
  770. SMPTE_COLOR_BLACK,
  771. };
  772. #define EXPAND_COLOR(r, g, b) { (r) * 0x101, (g) * 0x101, (b) * 0x101 }
  773. static const struct drm_color_lut bw_color_lut[] = {
  774. EXPAND_COLOR( 0, 0, 0), /* black */
  775. EXPAND_COLOR(255, 255, 255), /* white */
  776. };
  777. static const struct drm_color_lut pentile_color_lut[] = {
  778. /* PenTile RG-GB */
  779. EXPAND_COLOR( 0, 0, 0), /* black */
  780. EXPAND_COLOR(255, 0, 0), /* red */
  781. EXPAND_COLOR( 0, 207, 0), /* green */
  782. EXPAND_COLOR( 0, 0, 255), /* blue */
  783. };
  784. static const struct drm_color_lut smpte_color_lut[] = {
  785. [SMPTE_COLOR_GREY] = EXPAND_COLOR(192, 192, 192),
  786. [SMPTE_COLOR_YELLOW] = EXPAND_COLOR(192, 192, 0),
  787. [SMPTE_COLOR_CYAN] = EXPAND_COLOR( 0, 192, 192),
  788. [SMPTE_COLOR_GREEN] = EXPAND_COLOR( 0, 192, 0),
  789. [SMPTE_COLOR_MAGENTA] = EXPAND_COLOR(192, 0, 192),
  790. [SMPTE_COLOR_RED] = EXPAND_COLOR(192, 0, 0),
  791. [SMPTE_COLOR_BLUE] = EXPAND_COLOR( 0, 0, 192),
  792. [SMPTE_COLOR_BLACK] = EXPAND_COLOR( 19, 19, 19),
  793. [SMPTE_COLOR_IN_PHASE] = EXPAND_COLOR( 0, 33, 76),
  794. [SMPTE_COLOR_SUPER_WHITE] = EXPAND_COLOR(255, 255, 255),
  795. [SMPTE_COLOR_QUADRATURE] = EXPAND_COLOR( 50, 0, 106),
  796. [SMPTE_COLOR_3PC5] = EXPAND_COLOR( 9, 9, 9),
  797. [SMPTE_COLOR_11PC5] = EXPAND_COLOR( 29, 29, 29),
  798. };
  799. #undef EXPAND_COLOR
  800. /*
  801. * Floyd-Steinberg dithering
  802. */
  803. struct fsd {
  804. unsigned int width;
  805. unsigned int x;
  806. unsigned int i;
  807. int red;
  808. int green;
  809. int blue;
  810. int error[];
  811. };
  812. static struct fsd *fsd_alloc(unsigned int width)
  813. {
  814. unsigned int n = 3 * (width + 1);
  815. struct fsd *fsd = malloc(sizeof(*fsd) + n * sizeof(fsd->error[0]));
  816. fsd->width = width;
  817. fsd->x = 0;
  818. fsd->i = 0;
  819. memset(fsd->error, 0, n * sizeof(fsd->error[0]));
  820. return fsd;
  821. }
  822. static inline int clamp(int val, int min, int max)
  823. {
  824. if (val < min)
  825. return min;
  826. if (val > max)
  827. return max;
  828. return val;
  829. }
  830. static void fsd_dither(struct fsd *fsd, struct drm_color_lut *color)
  831. {
  832. unsigned int i = fsd->i;
  833. fsd->red = (int)color->red + (fsd->error[3 * i] + 8) / 16;
  834. fsd->green = (int)color->green + (fsd->error[3 * i + 1] + 8) / 16;
  835. fsd->blue = (int)color->blue + (fsd->error[3 * i + 2] + 8) / 16;
  836. color->red = clamp(fsd->red, 0, 65535);
  837. color->green = clamp(fsd->green, 0, 65535);
  838. color->blue = clamp(fsd->blue, 0, 65535);
  839. }
  840. static void fsd_update(struct fsd *fsd, const struct drm_color_lut *actual)
  841. {
  842. int error_red = fsd->red - (int)actual->red;
  843. int error_green = fsd->green - (int)actual->green;
  844. int error_blue = fsd->blue - (int)actual->blue;
  845. unsigned int width = fsd->width;
  846. unsigned int i = fsd->i, j;
  847. unsigned int n = width + 1;
  848. /* Distribute errors over neighboring pixels */
  849. if (fsd->x == width - 1) {
  850. /* Last pixel on this scanline */
  851. /* South East: initialize to zero */
  852. fsd->error[3 * i] = 0;
  853. fsd->error[3 * i + 1] = 0;
  854. fsd->error[3 * i + 2] = 0;
  855. } else {
  856. /* East: accumulate error */
  857. j = (i + 1) % n;
  858. fsd->error[3 * j] += 7 * error_red;
  859. fsd->error[3 * j + 1] += 7 * error_green;
  860. fsd->error[3 * j + 2] += 7 * error_blue;
  861. /* South East: initial error */
  862. fsd->error[3 * i] = error_red;
  863. fsd->error[3 * i + 1] = error_green;
  864. fsd->error[3 * i + 2] = error_blue;
  865. }
  866. /* South West: accumulate error */
  867. j = (i + width - 1) % n;
  868. fsd->error[3 * j] += 3 * error_red;
  869. fsd->error[3 * j + 1] += 3 * error_green;
  870. fsd->error[3 * j + 2] += 3 * error_blue;
  871. /* South: accumulate error */
  872. j = (i + width) % n;
  873. fsd->error[3 * j] += 5 * error_red;
  874. fsd->error[3 * j + 1] += 5 * error_green;
  875. fsd->error[3 * j + 2] += 5 * error_blue;
  876. fsd->x = (fsd->x + 1) % width;
  877. fsd->i = (fsd->i + 1) % n;
  878. }
  879. static void write_pixel_1(uint8_t *mem, unsigned int x, unsigned int pixel)
  880. {
  881. unsigned int shift = 7 - (x & 7);
  882. unsigned int mask = 1U << shift;
  883. mem[x / 8] = (mem[x / 8] & ~mask) | ((pixel << shift) & mask);
  884. }
  885. static void write_color_1(struct fsd *fsd, uint8_t *mem, unsigned int x,
  886. unsigned int index)
  887. {
  888. struct drm_color_lut color = smpte_color_lut[index];
  889. unsigned int pixel;
  890. fsd_dither(fsd, &color);
  891. /* ITU BT.601: Y = 0.299 R + 0.587 G + 0.114 B */
  892. if (3 * color.red + 6 * color.green + color.blue >= 10 * 32768) {
  893. pixel = 1;
  894. color.red = color.green = color.blue = 65535;
  895. } else {
  896. pixel = 0;
  897. color.red = color.green = color.blue = 0;
  898. }
  899. fsd_update(fsd, &color);
  900. write_pixel_1(mem, x, pixel);
  901. }
  902. static void fill_smpte_c1(void *mem, unsigned int width, unsigned int height,
  903. unsigned int stride)
  904. {
  905. struct fsd *fsd = fsd_alloc(width);
  906. unsigned int x;
  907. unsigned int y;
  908. for (y = 0; y < height * 6 / 9; ++y) {
  909. for (x = 0; x < width; ++x)
  910. write_color_1(fsd, mem, x, smpte_top[x * 7 / width]);
  911. mem += stride;
  912. }
  913. for (; y < height * 7 / 9; ++y) {
  914. for (x = 0; x < width; ++x)
  915. write_color_1(fsd, mem, x, smpte_middle[x * 7 / width]);
  916. mem += stride;
  917. }
  918. for (; y < height; ++y) {
  919. for (x = 0; x < width * 5 / 7; ++x)
  920. write_color_1(fsd, mem, x,
  921. smpte_bottom[x * 4 / (width * 5 / 7)]);
  922. for (; x < width * 6 / 7; ++x)
  923. write_color_1(fsd, mem, x,
  924. smpte_bottom[(x - width * 5 / 7) * 3 /
  925. (width / 7) + 4]);
  926. for (; x < width; ++x)
  927. write_color_1(fsd, mem, x, smpte_bottom[7]);
  928. mem += stride;
  929. }
  930. free(fsd);
  931. }
  932. static void write_pixel_2(uint8_t *mem, unsigned int x, unsigned int pixel)
  933. {
  934. unsigned int shift = 6 - 2 * (x & 3);
  935. unsigned int mask = 3U << shift;
  936. mem[x / 4] = (mem[x / 4] & ~mask) | ((pixel << shift) & mask);
  937. }
  938. static void write_color_2(struct fsd *fsd, uint8_t *mem, unsigned int stride,
  939. unsigned int x, unsigned int index)
  940. {
  941. struct drm_color_lut color = smpte_color_lut[index];
  942. unsigned int r, g, b;
  943. fsd_dither(fsd, &color);
  944. if (color.red >= 32768) {
  945. r = 1;
  946. color.red = 65535;
  947. } else {
  948. r = 0;
  949. color.red = 0;
  950. }
  951. if (color.green >= 32768) {
  952. g = 2;
  953. color.green = 65535;
  954. } else {
  955. g = 0;
  956. color.green = 0;
  957. }
  958. if (color.blue >= 32768) {
  959. b = 3;
  960. color.blue = 65535;
  961. } else {
  962. b = 0;
  963. color.blue = 0;
  964. }
  965. fsd_update(fsd, &color);
  966. /* Use PenTile RG-GB */
  967. write_pixel_2(mem, 2 * x, r);
  968. write_pixel_2(mem, 2 * x + 1, g);
  969. write_pixel_2(mem + stride, 2 * x, g);
  970. write_pixel_2(mem + stride, 2 * x + 1, b);
  971. }
  972. static void fill_smpte_c2(void *mem, unsigned int width, unsigned int height,
  973. unsigned int stride)
  974. {
  975. struct fsd *fsd = fsd_alloc(width);
  976. unsigned int x;
  977. unsigned int y;
  978. /* Half resolution for PenTile RG-GB */
  979. width /= 2;
  980. height /= 2;
  981. for (y = 0; y < height * 6 / 9; ++y) {
  982. for (x = 0; x < width; ++x)
  983. write_color_2(fsd, mem, stride, x, smpte_top[x * 7 / width]);
  984. mem += 2 * stride;
  985. }
  986. for (; y < height * 7 / 9; ++y) {
  987. for (x = 0; x < width; ++x)
  988. write_color_2(fsd, mem, stride, x, smpte_middle[x * 7 / width]);
  989. mem += 2 * stride;
  990. }
  991. for (; y < height; ++y) {
  992. for (x = 0; x < width * 5 / 7; ++x)
  993. write_color_2(fsd, mem, stride, x,
  994. smpte_bottom[x * 4 / (width * 5 / 7)]);
  995. for (; x < width * 6 / 7; ++x)
  996. write_color_2(fsd, mem, stride, x,
  997. smpte_bottom[(x - width * 5 / 7) * 3 /
  998. (width / 7) + 4]);
  999. for (; x < width; ++x)
  1000. write_color_2(fsd, mem, stride, x, smpte_bottom[7]);
  1001. mem += 2 * stride;
  1002. }
  1003. free(fsd);
  1004. }
  1005. static void write_pixel_4(uint8_t *mem, unsigned int x, unsigned int pixel)
  1006. {
  1007. if (x & 1)
  1008. mem[x / 2] = (mem[x / 2] & 0xf0) | (pixel & 0x0f);
  1009. else
  1010. mem[x / 2] = (mem[x / 2] & 0x0f) | (pixel << 4);
  1011. }
  1012. static void fill_smpte_c4(void *mem, unsigned int width, unsigned int height,
  1013. unsigned int stride)
  1014. {
  1015. unsigned int x;
  1016. unsigned int y;
  1017. for (y = 0; y < height * 6 / 9; ++y) {
  1018. for (x = 0; x < width; ++x)
  1019. write_pixel_4(mem, x, smpte_top[x * 7 / width]);
  1020. mem += stride;
  1021. }
  1022. for (; y < height * 7 / 9; ++y) {
  1023. for (x = 0; x < width; ++x)
  1024. write_pixel_4(mem, x, smpte_middle[x * 7 / width]);
  1025. mem += stride;
  1026. }
  1027. for (; y < height; ++y) {
  1028. for (x = 0; x < width * 5 / 7; ++x)
  1029. write_pixel_4(mem, x,
  1030. smpte_bottom[x * 4 / (width * 5 / 7)]);
  1031. for (; x < width * 6 / 7; ++x)
  1032. write_pixel_4(mem, x,
  1033. smpte_bottom[(x - width * 5 / 7) * 3 /
  1034. (width / 7) + 4]);
  1035. for (; x < width; ++x)
  1036. write_pixel_4(mem, x, smpte_bottom[7]);
  1037. mem += stride;
  1038. }
  1039. }
  1040. static void fill_smpte_c8(void *mem, unsigned int width, unsigned int height,
  1041. unsigned int stride)
  1042. {
  1043. unsigned int x;
  1044. unsigned int y;
  1045. for (y = 0; y < height * 6 / 9; ++y) {
  1046. for (x = 0; x < width; ++x)
  1047. ((uint8_t *)mem)[x] = smpte_top[x * 7 / width];
  1048. mem += stride;
  1049. }
  1050. for (; y < height * 7 / 9; ++y) {
  1051. for (x = 0; x < width; ++x)
  1052. ((uint8_t *)mem)[x] = smpte_middle[x * 7 / width];
  1053. mem += stride;
  1054. }
  1055. for (; y < height; ++y) {
  1056. for (x = 0; x < width * 5 / 7; ++x)
  1057. ((uint8_t *)mem)[x] =
  1058. smpte_bottom[x * 4 / (width * 5 / 7)];
  1059. for (; x < width * 6 / 7; ++x)
  1060. ((uint8_t *)mem)[x] =
  1061. smpte_bottom[(x - width * 5 / 7) * 3
  1062. / (width / 7) + 4];
  1063. for (; x < width; ++x)
  1064. ((uint8_t *)mem)[x] = smpte_bottom[7];
  1065. mem += stride;
  1066. }
  1067. }
  1068. void util_smpte_fill_lut(unsigned int ncolors, struct drm_color_lut *lut)
  1069. {
  1070. if (ncolors < ARRAY_SIZE(bw_color_lut)) {
  1071. printf("Error: lut too small: %u < %zu\n", ncolors,
  1072. ARRAY_SIZE(bw_color_lut));
  1073. return;
  1074. }
  1075. memset(lut, 0, ncolors * sizeof(struct drm_color_lut));
  1076. if (ncolors < ARRAY_SIZE(pentile_color_lut))
  1077. memcpy(lut, bw_color_lut, sizeof(bw_color_lut));
  1078. else if (ncolors < ARRAY_SIZE(smpte_color_lut))
  1079. memcpy(lut, pentile_color_lut, sizeof(pentile_color_lut));
  1080. else
  1081. memcpy(lut, smpte_color_lut, sizeof(smpte_color_lut));
  1082. }
  1083. static void fill_smpte(const struct util_format_info *info, void *planes[3],
  1084. unsigned int width, unsigned int height,
  1085. unsigned int stride)
  1086. {
  1087. unsigned char *u, *v;
  1088. switch (info->format) {
  1089. case DRM_FORMAT_C1:
  1090. return fill_smpte_c1(planes[0], width, height, stride);
  1091. case DRM_FORMAT_C2:
  1092. return fill_smpte_c2(planes[0], width, height, stride);
  1093. case DRM_FORMAT_C4:
  1094. return fill_smpte_c4(planes[0], width, height, stride);
  1095. case DRM_FORMAT_C8:
  1096. return fill_smpte_c8(planes[0], width, height, stride);
  1097. case DRM_FORMAT_UYVY:
  1098. case DRM_FORMAT_VYUY:
  1099. case DRM_FORMAT_YUYV:
  1100. case DRM_FORMAT_YVYU:
  1101. return fill_smpte_yuv_packed(&info->yuv, planes[0], width,
  1102. height, stride);
  1103. case DRM_FORMAT_NV12:
  1104. case DRM_FORMAT_NV21:
  1105. case DRM_FORMAT_NV16:
  1106. case DRM_FORMAT_NV61:
  1107. case DRM_FORMAT_NV24:
  1108. case DRM_FORMAT_NV42:
  1109. u = info->yuv.order & YUV_YCbCr ? planes[1] : planes[1] + 1;
  1110. v = info->yuv.order & YUV_YCrCb ? planes[1] : planes[1] + 1;
  1111. return fill_smpte_yuv_planar(&info->yuv, planes[0], u, v,
  1112. width, height, stride);
  1113. case DRM_FORMAT_NV15:
  1114. case DRM_FORMAT_NV20:
  1115. case DRM_FORMAT_NV30:
  1116. return fill_smpte_yuv_planar_10bpp(&info->yuv, planes[0],
  1117. planes[1], width, height,
  1118. stride);
  1119. case DRM_FORMAT_YUV420:
  1120. case DRM_FORMAT_YUV422:
  1121. case DRM_FORMAT_YUV444:
  1122. return fill_smpte_yuv_planar(&info->yuv, planes[0], planes[1],
  1123. planes[2], width, height, stride);
  1124. case DRM_FORMAT_YVU420:
  1125. case DRM_FORMAT_YVU422:
  1126. case DRM_FORMAT_YVU444:
  1127. return fill_smpte_yuv_planar(&info->yuv, planes[0], planes[2],
  1128. planes[1], width, height, stride);
  1129. case DRM_FORMAT_ARGB4444:
  1130. case DRM_FORMAT_XRGB4444:
  1131. case DRM_FORMAT_ABGR4444:
  1132. case DRM_FORMAT_XBGR4444:
  1133. case DRM_FORMAT_RGBA4444:
  1134. case DRM_FORMAT_RGBX4444:
  1135. case DRM_FORMAT_BGRA4444:
  1136. case DRM_FORMAT_BGRX4444:
  1137. case DRM_FORMAT_RGB565:
  1138. case DRM_FORMAT_RGB565 | DRM_FORMAT_BIG_ENDIAN:
  1139. case DRM_FORMAT_BGR565:
  1140. case DRM_FORMAT_ARGB1555:
  1141. case DRM_FORMAT_XRGB1555:
  1142. case DRM_FORMAT_XRGB1555 | DRM_FORMAT_BIG_ENDIAN:
  1143. case DRM_FORMAT_ABGR1555:
  1144. case DRM_FORMAT_XBGR1555:
  1145. case DRM_FORMAT_RGBA5551:
  1146. case DRM_FORMAT_RGBX5551:
  1147. case DRM_FORMAT_BGRA5551:
  1148. case DRM_FORMAT_BGRX5551:
  1149. return fill_smpte_rgb16(&info->rgb, planes[0],
  1150. width, height, stride,
  1151. info->format & DRM_FORMAT_BIG_ENDIAN);
  1152. case DRM_FORMAT_BGR888:
  1153. case DRM_FORMAT_RGB888:
  1154. return fill_smpte_rgb24(&info->rgb, planes[0],
  1155. width, height, stride);
  1156. case DRM_FORMAT_ARGB8888:
  1157. case DRM_FORMAT_XRGB8888:
  1158. case DRM_FORMAT_ABGR8888:
  1159. case DRM_FORMAT_XBGR8888:
  1160. case DRM_FORMAT_RGBA8888:
  1161. case DRM_FORMAT_RGBX8888:
  1162. case DRM_FORMAT_BGRA8888:
  1163. case DRM_FORMAT_BGRX8888:
  1164. case DRM_FORMAT_ARGB2101010:
  1165. case DRM_FORMAT_XRGB2101010:
  1166. case DRM_FORMAT_ABGR2101010:
  1167. case DRM_FORMAT_XBGR2101010:
  1168. case DRM_FORMAT_RGBA1010102:
  1169. case DRM_FORMAT_RGBX1010102:
  1170. case DRM_FORMAT_BGRA1010102:
  1171. case DRM_FORMAT_BGRX1010102:
  1172. return fill_smpte_rgb32(&info->rgb, planes[0],
  1173. width, height, stride);
  1174. case DRM_FORMAT_XRGB16161616F:
  1175. case DRM_FORMAT_XBGR16161616F:
  1176. case DRM_FORMAT_ARGB16161616F:
  1177. case DRM_FORMAT_ABGR16161616F:
  1178. return fill_smpte_rgb16fp(&info->rgb, planes[0],
  1179. width, height, stride);
  1180. }
  1181. }
  1182. #if HAVE_CAIRO
  1183. static void byteswap_buffer16(void *mem, unsigned int width, unsigned int height,
  1184. unsigned int stride)
  1185. {
  1186. unsigned int x, y;
  1187. for (y = 0; y < height; ++y) {
  1188. for (x = 0; x < width; ++x)
  1189. ((uint16_t *)mem)[x] = swap16(((uint16_t *)mem)[x]);
  1190. mem += stride;
  1191. }
  1192. }
  1193. static void byteswap_buffer32(void *mem, unsigned int width, unsigned int height,
  1194. unsigned int stride)
  1195. {
  1196. unsigned int x, y;
  1197. for (y = 0; y < height; ++y) {
  1198. for (x = 0; x < width; ++x)
  1199. ((uint32_t *)mem)[x] = swap32(((uint32_t *)mem)[x]);
  1200. mem += stride;
  1201. }
  1202. }
  1203. #endif
  1204. static void make_pwetty(void *data, unsigned int width, unsigned int height,
  1205. unsigned int stride, uint32_t format)
  1206. {
  1207. #if HAVE_CAIRO
  1208. cairo_surface_t *surface;
  1209. cairo_t *cr;
  1210. cairo_format_t cairo_format;
  1211. bool swap16 = false;
  1212. bool swap32 = false;
  1213. /* we can ignore the order of R,G,B channels */
  1214. switch (format) {
  1215. case DRM_FORMAT_XRGB8888:
  1216. case DRM_FORMAT_ARGB8888:
  1217. case DRM_FORMAT_XBGR8888:
  1218. case DRM_FORMAT_ABGR8888:
  1219. cairo_format = CAIRO_FORMAT_ARGB32;
  1220. break;
  1221. case DRM_FORMAT_RGB565:
  1222. case DRM_FORMAT_RGB565 | DRM_FORMAT_BIG_ENDIAN:
  1223. case DRM_FORMAT_BGR565:
  1224. cairo_format = CAIRO_FORMAT_RGB16_565;
  1225. swap16 = fb_foreign_endian(format);
  1226. break;
  1227. #if CAIRO_VERSION_MAJOR > 1 || (CAIRO_VERSION_MAJOR == 1 && CAIRO_VERSION_MINOR >= 12)
  1228. case DRM_FORMAT_ARGB2101010:
  1229. case DRM_FORMAT_XRGB2101010:
  1230. case DRM_FORMAT_ABGR2101010:
  1231. case DRM_FORMAT_XBGR2101010:
  1232. cairo_format = CAIRO_FORMAT_RGB30;
  1233. swap32 = fb_foreign_endian(format);
  1234. break;
  1235. #endif
  1236. default:
  1237. return;
  1238. }
  1239. /* Cairo uses native byte order, so we may have to byteswap before... */
  1240. if (swap16)
  1241. byteswap_buffer16(data, width, height, stride);
  1242. if (swap32)
  1243. byteswap_buffer32(data, width, height, stride);
  1244. surface = cairo_image_surface_create_for_data(data,
  1245. cairo_format,
  1246. width, height,
  1247. stride);
  1248. cr = cairo_create(surface);
  1249. cairo_surface_destroy(surface);
  1250. cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE);
  1251. for (unsigned x = 0; x < width; x += 250)
  1252. for (unsigned y = 0; y < height; y += 250) {
  1253. char buf[64];
  1254. cairo_move_to(cr, x, y - 20);
  1255. cairo_line_to(cr, x, y + 20);
  1256. cairo_move_to(cr, x - 20, y);
  1257. cairo_line_to(cr, x + 20, y);
  1258. cairo_new_sub_path(cr);
  1259. cairo_arc(cr, x, y, 10, 0, M_PI * 2);
  1260. cairo_set_line_width(cr, 4);
  1261. cairo_set_source_rgb(cr, 0, 0, 0);
  1262. cairo_stroke_preserve(cr);
  1263. cairo_set_source_rgb(cr, 1, 1, 1);
  1264. cairo_set_line_width(cr, 2);
  1265. cairo_stroke(cr);
  1266. snprintf(buf, sizeof buf, "%d, %d", x, y);
  1267. cairo_move_to(cr, x + 20, y + 20);
  1268. cairo_text_path(cr, buf);
  1269. cairo_set_source_rgb(cr, 0, 0, 0);
  1270. cairo_stroke_preserve(cr);
  1271. cairo_set_source_rgb(cr, 1, 1, 1);
  1272. cairo_fill(cr);
  1273. }
  1274. cairo_destroy(cr);
  1275. /* ... and after */
  1276. if (swap16)
  1277. byteswap_buffer16(data, width, height, stride);
  1278. if (swap32)
  1279. byteswap_buffer32(data, width, height, stride);
  1280. #endif
  1281. }
  1282. static struct color_yuv make_tiles_yuv_color(unsigned int x, unsigned int y,
  1283. unsigned int width)
  1284. {
  1285. div_t d = div(x+y, width);
  1286. uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
  1287. + 0x000a1120 * (d.rem >> 6);
  1288. struct color_yuv color =
  1289. MAKE_YUV_601((rgb32 >> 16) & 0xff, (rgb32 >> 8) & 0xff,
  1290. rgb32 & 0xff);
  1291. return color;
  1292. }
  1293. static void fill_tiles_yuv_planar(const struct util_format_info *info,
  1294. unsigned char *y_mem, unsigned char *u_mem,
  1295. unsigned char *v_mem, unsigned int width,
  1296. unsigned int height, unsigned int stride)
  1297. {
  1298. const struct util_yuv_info *yuv = &info->yuv;
  1299. unsigned int cs = yuv->chroma_stride;
  1300. unsigned int xsub = yuv->xsub;
  1301. unsigned int ysub = yuv->ysub;
  1302. unsigned int x;
  1303. unsigned int y;
  1304. for (y = 0; y < height; ++y) {
  1305. for (x = 0; x < width; ++x) {
  1306. struct color_yuv color =
  1307. make_tiles_yuv_color(x, y, width);
  1308. y_mem[x] = color.y;
  1309. u_mem[x/xsub*cs] = color.u;
  1310. v_mem[x/xsub*cs] = color.v;
  1311. }
  1312. y_mem += stride;
  1313. if ((y + 1) % ysub == 0) {
  1314. u_mem += stride * cs / xsub;
  1315. v_mem += stride * cs / xsub;
  1316. }
  1317. }
  1318. }
  1319. static void fill_tiles_yuv_planar_10bpp(const struct util_format_info *info,
  1320. unsigned char *y_mem,
  1321. unsigned char *uv_mem,
  1322. unsigned int width,
  1323. unsigned int height,
  1324. unsigned int stride)
  1325. {
  1326. const struct util_yuv_info *yuv = &info->yuv;
  1327. unsigned int cs = yuv->chroma_stride;
  1328. unsigned int xsub = yuv->xsub;
  1329. unsigned int ysub = yuv->ysub;
  1330. unsigned int xstep = cs * xsub;
  1331. unsigned int x;
  1332. unsigned int y;
  1333. for (y = 0; y < height; ++y) {
  1334. for (x = 0; x < width; x += 4) {
  1335. struct color_yuv a = make_tiles_yuv_color(x+0, y, width);
  1336. struct color_yuv b = make_tiles_yuv_color(x+1, y, width);
  1337. struct color_yuv c = make_tiles_yuv_color(x+2, y, width);
  1338. struct color_yuv d = make_tiles_yuv_color(x+3, y, width);
  1339. write_pixels_10bpp(&y_mem[(x * 5) / 4],
  1340. a.y << 2, b.y << 2, c.y << 2, d.y << 2);
  1341. }
  1342. y_mem += stride;
  1343. }
  1344. for (y = 0; y < height; y += ysub) {
  1345. for (x = 0; x < width; x += xstep) {
  1346. struct color_yuv a = make_tiles_yuv_color(x+0, y, width);
  1347. struct color_yuv b = make_tiles_yuv_color(x+xsub, y, width);
  1348. write_pixels_10bpp(&uv_mem[(x * 5) / xstep],
  1349. a.u << 2, a.v << 2, b.u << 2, b.v << 2);
  1350. }
  1351. uv_mem += stride * cs / xsub;
  1352. }
  1353. }
  1354. static void fill_tiles_yuv_packed(const struct util_format_info *info,
  1355. void *mem, unsigned int width,
  1356. unsigned int height, unsigned int stride)
  1357. {
  1358. const struct util_yuv_info *yuv = &info->yuv;
  1359. unsigned char *y_mem = (yuv->order & YUV_YC) ? mem : mem + 1;
  1360. unsigned char *c_mem = (yuv->order & YUV_CY) ? mem : mem + 1;
  1361. unsigned int u = (yuv->order & YUV_YCrCb) ? 2 : 0;
  1362. unsigned int v = (yuv->order & YUV_YCbCr) ? 2 : 0;
  1363. unsigned int x;
  1364. unsigned int y;
  1365. for (y = 0; y < height; ++y) {
  1366. for (x = 0; x < width; x += 2) {
  1367. struct color_yuv color =
  1368. make_tiles_yuv_color(x, y, width);
  1369. y_mem[2*x] = color.y;
  1370. c_mem[2*x+u] = color.u;
  1371. y_mem[2*x+2] = color.y;
  1372. c_mem[2*x+v] = color.v;
  1373. }
  1374. y_mem += stride;
  1375. c_mem += stride;
  1376. }
  1377. }
  1378. static void fill_tiles_rgb16(const struct util_format_info *info, void *mem,
  1379. unsigned int width, unsigned int height,
  1380. unsigned int stride, bool fb_be)
  1381. {
  1382. const struct util_rgb_info *rgb = &info->rgb;
  1383. void *mem_base = mem;
  1384. unsigned int x, y;
  1385. for (y = 0; y < height; ++y) {
  1386. for (x = 0; x < width; ++x) {
  1387. div_t d = div(x+y, width);
  1388. uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
  1389. + 0x000a1120 * (d.rem >> 6);
  1390. uint16_t color =
  1391. MAKE_RGBA(rgb, (rgb32 >> 16) & 0xff,
  1392. (rgb32 >> 8) & 0xff, rgb32 & 0xff,
  1393. 255);
  1394. ((uint16_t *)mem)[x] = cpu_to_fb16(color);
  1395. }
  1396. mem += stride;
  1397. }
  1398. make_pwetty(mem_base, width, height, stride, info->format);
  1399. }
  1400. static void fill_tiles_rgb24(const struct util_format_info *info, void *mem,
  1401. unsigned int width, unsigned int height,
  1402. unsigned int stride)
  1403. {
  1404. const struct util_rgb_info *rgb = &info->rgb;
  1405. unsigned int x, y;
  1406. for (y = 0; y < height; ++y) {
  1407. for (x = 0; x < width; ++x) {
  1408. div_t d = div(x+y, width);
  1409. uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
  1410. + 0x000a1120 * (d.rem >> 6);
  1411. struct color_rgb24 color =
  1412. MAKE_RGB24(rgb, (rgb32 >> 16) & 0xff,
  1413. (rgb32 >> 8) & 0xff, rgb32 & 0xff);
  1414. ((struct color_rgb24 *)mem)[x] = color;
  1415. }
  1416. mem += stride;
  1417. }
  1418. }
  1419. static void fill_tiles_rgb32(const struct util_format_info *info, void *mem,
  1420. unsigned int width, unsigned int height,
  1421. unsigned int stride)
  1422. {
  1423. const struct util_rgb_info *rgb = &info->rgb;
  1424. void *mem_base = mem;
  1425. unsigned int x, y;
  1426. for (y = 0; y < height; ++y) {
  1427. for (x = 0; x < width; ++x) {
  1428. div_t d = div(x+y, width);
  1429. uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
  1430. + 0x000a1120 * (d.rem >> 6);
  1431. uint32_t alpha = ((y < height/2) && (x < width/2)) ? 127 : 255;
  1432. uint32_t color =
  1433. MAKE_RGBA(rgb, (rgb32 >> 16) & 0xff,
  1434. (rgb32 >> 8) & 0xff, rgb32 & 0xff,
  1435. alpha);
  1436. ((uint32_t *)mem)[x] = cpu_to_le32(color);
  1437. }
  1438. mem += stride;
  1439. }
  1440. make_pwetty(mem_base, width, height, stride, info->format);
  1441. }
  1442. static void fill_tiles_rgb16fp(const struct util_format_info *info, void *mem,
  1443. unsigned int width, unsigned int height,
  1444. unsigned int stride)
  1445. {
  1446. const struct util_rgb_info *rgb = &info->rgb;
  1447. unsigned int x, y;
  1448. /* TODO: Give this actual fp16 precision */
  1449. for (y = 0; y < height; ++y) {
  1450. for (x = 0; x < width; ++x) {
  1451. div_t d = div(x+y, width);
  1452. uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
  1453. + 0x000a1120 * (d.rem >> 6);
  1454. uint32_t alpha = ((y < height/2) && (x < width/2)) ? 127 : 255;
  1455. uint64_t color =
  1456. MAKE_RGBA8FP16(rgb, (rgb32 >> 16) & 0xff,
  1457. (rgb32 >> 8) & 0xff, rgb32 & 0xff,
  1458. alpha);
  1459. ((uint64_t *)mem)[x] = color;
  1460. }
  1461. mem += stride;
  1462. }
  1463. }
  1464. static void fill_tiles(const struct util_format_info *info, void *planes[3],
  1465. unsigned int width, unsigned int height,
  1466. unsigned int stride)
  1467. {
  1468. unsigned char *u, *v;
  1469. switch (info->format) {
  1470. case DRM_FORMAT_UYVY:
  1471. case DRM_FORMAT_VYUY:
  1472. case DRM_FORMAT_YUYV:
  1473. case DRM_FORMAT_YVYU:
  1474. return fill_tiles_yuv_packed(info, planes[0],
  1475. width, height, stride);
  1476. case DRM_FORMAT_NV12:
  1477. case DRM_FORMAT_NV21:
  1478. case DRM_FORMAT_NV16:
  1479. case DRM_FORMAT_NV61:
  1480. case DRM_FORMAT_NV24:
  1481. case DRM_FORMAT_NV42:
  1482. u = info->yuv.order & YUV_YCbCr ? planes[1] : planes[1] + 1;
  1483. v = info->yuv.order & YUV_YCrCb ? planes[1] : planes[1] + 1;
  1484. return fill_tiles_yuv_planar(info, planes[0], u, v,
  1485. width, height, stride);
  1486. case DRM_FORMAT_NV15:
  1487. case DRM_FORMAT_NV20:
  1488. case DRM_FORMAT_NV30:
  1489. return fill_tiles_yuv_planar_10bpp(info, planes[0], planes[1],
  1490. width, height, stride);
  1491. case DRM_FORMAT_YUV420:
  1492. case DRM_FORMAT_YUV422:
  1493. case DRM_FORMAT_YUV444:
  1494. return fill_tiles_yuv_planar(info, planes[0], planes[1],
  1495. planes[2], width, height, stride);
  1496. case DRM_FORMAT_YVU420:
  1497. case DRM_FORMAT_YVU422:
  1498. case DRM_FORMAT_YVU444:
  1499. return fill_tiles_yuv_planar(info, planes[0], planes[2],
  1500. planes[1], width, height, stride);
  1501. case DRM_FORMAT_ARGB4444:
  1502. case DRM_FORMAT_XRGB4444:
  1503. case DRM_FORMAT_ABGR4444:
  1504. case DRM_FORMAT_XBGR4444:
  1505. case DRM_FORMAT_RGBA4444:
  1506. case DRM_FORMAT_RGBX4444:
  1507. case DRM_FORMAT_BGRA4444:
  1508. case DRM_FORMAT_BGRX4444:
  1509. case DRM_FORMAT_RGB565:
  1510. case DRM_FORMAT_RGB565 | DRM_FORMAT_BIG_ENDIAN:
  1511. case DRM_FORMAT_BGR565:
  1512. case DRM_FORMAT_ARGB1555:
  1513. case DRM_FORMAT_XRGB1555:
  1514. case DRM_FORMAT_XRGB1555 | DRM_FORMAT_BIG_ENDIAN:
  1515. case DRM_FORMAT_ABGR1555:
  1516. case DRM_FORMAT_XBGR1555:
  1517. case DRM_FORMAT_RGBA5551:
  1518. case DRM_FORMAT_RGBX5551:
  1519. case DRM_FORMAT_BGRA5551:
  1520. case DRM_FORMAT_BGRX5551:
  1521. return fill_tiles_rgb16(info, planes[0],
  1522. width, height, stride,
  1523. info->format & DRM_FORMAT_BIG_ENDIAN);
  1524. case DRM_FORMAT_BGR888:
  1525. case DRM_FORMAT_RGB888:
  1526. return fill_tiles_rgb24(info, planes[0],
  1527. width, height, stride);
  1528. case DRM_FORMAT_ARGB8888:
  1529. case DRM_FORMAT_XRGB8888:
  1530. case DRM_FORMAT_ABGR8888:
  1531. case DRM_FORMAT_XBGR8888:
  1532. case DRM_FORMAT_RGBA8888:
  1533. case DRM_FORMAT_RGBX8888:
  1534. case DRM_FORMAT_BGRA8888:
  1535. case DRM_FORMAT_BGRX8888:
  1536. case DRM_FORMAT_ARGB2101010:
  1537. case DRM_FORMAT_XRGB2101010:
  1538. case DRM_FORMAT_ABGR2101010:
  1539. case DRM_FORMAT_XBGR2101010:
  1540. case DRM_FORMAT_RGBA1010102:
  1541. case DRM_FORMAT_RGBX1010102:
  1542. case DRM_FORMAT_BGRA1010102:
  1543. case DRM_FORMAT_BGRX1010102:
  1544. return fill_tiles_rgb32(info, planes[0],
  1545. width, height, stride);
  1546. case DRM_FORMAT_XRGB16161616F:
  1547. case DRM_FORMAT_XBGR16161616F:
  1548. case DRM_FORMAT_ARGB16161616F:
  1549. case DRM_FORMAT_ABGR16161616F:
  1550. return fill_tiles_rgb16fp(info, planes[0],
  1551. width, height, stride);
  1552. }
  1553. }
  1554. static void fill_plain(const struct util_format_info *info, void *planes[3],
  1555. unsigned int height,
  1556. unsigned int stride)
  1557. {
  1558. switch (info->format) {
  1559. case DRM_FORMAT_XRGB16161616F:
  1560. case DRM_FORMAT_XBGR16161616F:
  1561. case DRM_FORMAT_ARGB16161616F:
  1562. case DRM_FORMAT_ABGR16161616F:
  1563. /* 0x3838 = 0.5273 */
  1564. memset(planes[0], 0x38, stride * height);
  1565. break;
  1566. default:
  1567. memset(planes[0], 0x77, stride * height);
  1568. break;
  1569. }
  1570. }
  1571. static void fill_gradient_rgb32(const struct util_rgb_info *rgb,
  1572. void *mem,
  1573. unsigned int width, unsigned int height,
  1574. unsigned int stride)
  1575. {
  1576. unsigned int i, j;
  1577. for (i = 0; i < height / 2; i++) {
  1578. uint32_t *row = mem;
  1579. for (j = 0; j < width / 2; j++) {
  1580. uint32_t value = MAKE_RGBA10(rgb, j & 0x3ff, j & 0x3ff, j & 0x3ff, 0);
  1581. row[2*j] = row[2*j+1] = cpu_to_le32(value);
  1582. }
  1583. mem += stride;
  1584. }
  1585. for (; i < height; i++) {
  1586. uint32_t *row = mem;
  1587. for (j = 0; j < width / 2; j++) {
  1588. uint32_t value = MAKE_RGBA10(rgb, j & 0x3fc, j & 0x3fc, j & 0x3fc, 0);
  1589. row[2*j] = row[2*j+1] = cpu_to_le32(value);
  1590. }
  1591. mem += stride;
  1592. }
  1593. }
  1594. static void fill_gradient_rgb16fp(const struct util_rgb_info *rgb,
  1595. void *mem,
  1596. unsigned int width, unsigned int height,
  1597. unsigned int stride)
  1598. {
  1599. unsigned int i, j;
  1600. for (i = 0; i < height / 2; i++) {
  1601. uint64_t *row = mem;
  1602. for (j = 0; j < width / 2; j++) {
  1603. uint64_t value = MAKE_RGBA10FP16(rgb, j & 0x3ff, j & 0x3ff, j & 0x3ff, 0);
  1604. row[2*j] = row[2*j+1] = value;
  1605. }
  1606. mem += stride;
  1607. }
  1608. for (; i < height; i++) {
  1609. uint64_t *row = mem;
  1610. for (j = 0; j < width / 2; j++) {
  1611. uint64_t value = MAKE_RGBA10FP16(rgb, j & 0x3fc, j & 0x3fc, j & 0x3fc, 0);
  1612. row[2*j] = row[2*j+1] = value;
  1613. }
  1614. mem += stride;
  1615. }
  1616. }
  1617. /* The gradient pattern creates two horizontal gray gradients, split
  1618. * into two halves. The top half has 10bpc precision, the bottom half
  1619. * has 8bpc precision. When using with a 10bpc fb format, there are 3
  1620. * possible outcomes:
  1621. *
  1622. * - Pixel data is encoded as 8bpc to the display, no dithering. This
  1623. * would lead to the top and bottom halves looking identical.
  1624. *
  1625. * - Pixel data is encoded as 8bpc to the display, with dithering. This
  1626. * would lead to there being a visible difference between the two halves,
  1627. * but the top half would look a little speck-y due to the dithering.
  1628. *
  1629. * - Pixel data is encoded at 10bpc+ to the display (which implies
  1630. * the display is able to show this level of depth). This should
  1631. * lead to the top half being a very clean gradient, and visibly different
  1632. * from the bottom half.
  1633. *
  1634. * Once we support additional fb formats, this approach could be extended
  1635. * to distinguish even higher bpc precisions.
  1636. *
  1637. * Note that due to practical size considerations, for the screens
  1638. * where this matters, the pattern actually emits stripes 2-pixels
  1639. * wide for each gradient color. Otherwise the difference may be a bit
  1640. * hard to notice.
  1641. */
  1642. static void fill_gradient(const struct util_format_info *info, void *planes[3],
  1643. unsigned int width, unsigned int height,
  1644. unsigned int stride)
  1645. {
  1646. switch (info->format) {
  1647. case DRM_FORMAT_ARGB8888:
  1648. case DRM_FORMAT_XRGB8888:
  1649. case DRM_FORMAT_ABGR8888:
  1650. case DRM_FORMAT_XBGR8888:
  1651. case DRM_FORMAT_RGBA8888:
  1652. case DRM_FORMAT_RGBX8888:
  1653. case DRM_FORMAT_BGRA8888:
  1654. case DRM_FORMAT_BGRX8888:
  1655. case DRM_FORMAT_ARGB2101010:
  1656. case DRM_FORMAT_XRGB2101010:
  1657. case DRM_FORMAT_ABGR2101010:
  1658. case DRM_FORMAT_XBGR2101010:
  1659. case DRM_FORMAT_RGBA1010102:
  1660. case DRM_FORMAT_RGBX1010102:
  1661. case DRM_FORMAT_BGRA1010102:
  1662. case DRM_FORMAT_BGRX1010102:
  1663. return fill_gradient_rgb32(&info->rgb, planes[0],
  1664. width, height, stride);
  1665. case DRM_FORMAT_XRGB16161616F:
  1666. case DRM_FORMAT_XBGR16161616F:
  1667. case DRM_FORMAT_ARGB16161616F:
  1668. case DRM_FORMAT_ABGR16161616F:
  1669. return fill_gradient_rgb16fp(&info->rgb, planes[0],
  1670. width, height, stride);
  1671. }
  1672. }
  1673. static struct color_rgba get_black_white_value(uint64_t index)
  1674. {
  1675. const struct color_rgba colors[] = {
  1676. { .red = 0, .green = 0, .blue = 0, .alpha = 255 }, /* black */
  1677. { .red = 255, .green = 255, .blue = 255, .alpha = 255 }, /* white */
  1678. };
  1679. return colors[index & 0x1];
  1680. }
  1681. static struct color_rgba get_noise_color_value()
  1682. {
  1683. struct color_rgba color = {
  1684. .red = rand(),
  1685. .green = rand(),
  1686. .blue = rand(),
  1687. .alpha = 255
  1688. };
  1689. return color;
  1690. }
  1691. static struct color_rgba get_rgb_color(uint64_t index,
  1692. enum util_fill_pattern pattern)
  1693. {
  1694. struct color_rgba color = {
  1695. .red = 0,
  1696. .green = 0,
  1697. .blue = 0,
  1698. .alpha = 0
  1699. };
  1700. switch (pattern) {
  1701. case UTIL_PATTERN_NOISE:
  1702. color = get_black_white_value(rand());
  1703. case UTIL_PATTERN_NOISE_COLOR:
  1704. color = get_noise_color_value();
  1705. case UTIL_PATTERN_BLACK_WHITE:
  1706. color = get_black_white_value(index);
  1707. default:
  1708. break;
  1709. }
  1710. return color;
  1711. }
  1712. static void insert_value_yuv_packed(const struct util_format_info *info,
  1713. void *planes[3], unsigned int stride,
  1714. unsigned int x, unsigned int y,
  1715. const struct color_rgba* color)
  1716. {
  1717. struct color_yuv val = MAKE_YUV_601(color->red, color->green, color->blue);
  1718. const struct util_yuv_info *yuv = &info->yuv;
  1719. unsigned char *y_mem = (yuv->order & YUV_YC) ? planes[0] : planes[0] + 1;
  1720. unsigned char *c_mem = (yuv->order & YUV_CY) ? planes[0] : planes[0] + 1;
  1721. unsigned int u = (yuv->order & YUV_YCrCb) ? 2 : 0;
  1722. unsigned int v = (yuv->order & YUV_YCbCr) ? 2 : 0;
  1723. if (x & 0x1)
  1724. return;
  1725. y_mem += stride * y;
  1726. c_mem += stride * y;
  1727. y_mem[2*x] = val.y;
  1728. c_mem[2*x+u] = val.u;
  1729. y_mem[2*x+2] = val.y;
  1730. c_mem[2*x+v] = val.v;
  1731. }
  1732. static void insert_value_yuv_planar(const struct util_format_info *info,
  1733. void *planes[3], unsigned int stride,
  1734. unsigned int x, unsigned int y,
  1735. const struct color_rgba* color)
  1736. {
  1737. struct color_yuv val = MAKE_YUV_601(color->red, color->green, color->blue);
  1738. const struct util_yuv_info *yuv = &info->yuv;
  1739. unsigned int cs = yuv->chroma_stride;
  1740. unsigned int xsub = yuv->xsub;
  1741. unsigned int ysub = yuv->ysub;
  1742. unsigned int chroma_offset = (y + 1) / ysub;
  1743. unsigned char *y_mem = planes[0] + (y * stride);
  1744. unsigned char *u_mem = planes[1];
  1745. unsigned char *v_mem = planes[2];
  1746. switch (info->format) {
  1747. case DRM_FORMAT_NV42:
  1748. u_mem = info->yuv.order & YUV_YCbCr ? planes[1] : planes[1] + 1;
  1749. v_mem = info->yuv.order & YUV_YCrCb ? planes[1] : planes[1] + 1;
  1750. break;
  1751. case DRM_FORMAT_YVU420:
  1752. u_mem = planes[2];
  1753. v_mem = planes[1];
  1754. break;
  1755. case DRM_FORMAT_YUV420:
  1756. default:
  1757. break;
  1758. }
  1759. u_mem += (chroma_offset * (stride * cs / xsub));
  1760. v_mem += (chroma_offset * (stride * cs / xsub));
  1761. y_mem[x] = val.y;
  1762. u_mem[x/xsub*cs] = val.u;
  1763. v_mem[x/xsub*cs] = val.v;
  1764. }
  1765. static inline bool is_power_of_two(unsigned long val)
  1766. {
  1767. return (val != 0) && ((val & (val - 1)) == 0);
  1768. }
  1769. static bool check_yuv(const struct util_yuv_info *info)
  1770. {
  1771. if (__builtin_expect(
  1772. is_power_of_two(info->xsub) &&
  1773. is_power_of_two(info->ysub) &&
  1774. is_power_of_two(info->chroma_stride), 1)) {
  1775. return true;
  1776. }
  1777. return false;
  1778. }
  1779. static void insert_value_yuv_planar_10bpp(const struct util_format_info *info,
  1780. void *planes[3], unsigned int stride,
  1781. unsigned int x, unsigned int y,
  1782. const struct color_rgba* color)
  1783. {
  1784. struct color_yuv val = MAKE_YUV_601(color->red, color->green, color->blue);
  1785. const struct util_yuv_info *yuv = &info->yuv;
  1786. unsigned int cs = yuv->chroma_stride;
  1787. unsigned int xsub = yuv->xsub;
  1788. unsigned int ysub = yuv->ysub;
  1789. unsigned int xstep = cs * xsub;
  1790. unsigned int ysub_mask = ysub - 1;
  1791. unsigned int xsub_mask = xsub - 1;
  1792. unsigned int xstep_mask = xstep - 1;
  1793. unsigned char *y_mem = planes[0] + (y * stride);
  1794. unsigned char *uv_mem = planes[1] + (y * (stride * cs / xsub));
  1795. unsigned int block_start = ((x & 0x3) * 5) / 4;
  1796. unsigned int bit_start = (x & 0x3) * 10;
  1797. /* Plus two because val.y is only 8 bits */
  1798. update_pixels_10bpp(&y_mem[block_start], val.y << (bit_start + 2),
  1799. 0x3ff << bit_start);
  1800. /* This logic only works when xsub, ysub and chroma stride is power of two. */
  1801. assert(check_yuv(&info->yuv));
  1802. if (y & ysub_mask)
  1803. return;
  1804. if (x & xsub_mask)
  1805. return;
  1806. block_start = ((x & ~xstep_mask) * 5) / xstep;
  1807. bit_start = x & xstep_mask ? 0 : 20;
  1808. update_pixels_10bpp(&uv_mem[block_start],
  1809. ((val.u << 2) | (val.v << 12)) << bit_start,
  1810. 0xfffff << bit_start);
  1811. }
  1812. static void insert_value_rgb32(const struct util_format_info *info,
  1813. void *planes[3], unsigned int stride,
  1814. unsigned int x, unsigned int y,
  1815. const struct color_rgba* color)
  1816. {
  1817. uint32_t *row = planes[0] + (stride * y);
  1818. uint32_t val = MAKE_RGBA10(&info->rgb, color->red, color->green,
  1819. color->blue, color->alpha);
  1820. row[x] = val;
  1821. }
  1822. static void insert_value_rgb16fp(const struct util_format_info *info,
  1823. void *planes[3], unsigned int stride,
  1824. unsigned int x, unsigned int y,
  1825. const struct color_rgba* color)
  1826. {
  1827. uint64_t *row = planes[0] + (stride * y);
  1828. uint64_t val = MAKE_RGBA10FP16(&info->rgb, color->red, color->green,
  1829. color->blue, color->alpha);
  1830. row[x] = val;
  1831. }
  1832. static void fill_simple_patterns(const struct util_format_info *info,
  1833. void *planes[3], unsigned int width,
  1834. unsigned int height, unsigned int stride,
  1835. enum util_fill_pattern pattern)
  1836. {
  1837. void (*func_insert_value)(const struct util_format_info *info,
  1838. void *planes[3], unsigned int stride,
  1839. unsigned int x, unsigned int y,
  1840. const struct color_rgba* color);
  1841. int x, y;
  1842. switch (info->format) {
  1843. case DRM_FORMAT_UYVY:
  1844. case DRM_FORMAT_VYUY:
  1845. case DRM_FORMAT_YUYV:
  1846. case DRM_FORMAT_YVYU:
  1847. func_insert_value = &insert_value_yuv_packed;
  1848. break;
  1849. case DRM_FORMAT_NV12:
  1850. case DRM_FORMAT_NV21:
  1851. case DRM_FORMAT_NV16:
  1852. case DRM_FORMAT_NV61:
  1853. case DRM_FORMAT_NV24:
  1854. case DRM_FORMAT_NV42:
  1855. case DRM_FORMAT_YUV420:
  1856. case DRM_FORMAT_YVU420:
  1857. func_insert_value = &insert_value_yuv_planar;
  1858. break;
  1859. case DRM_FORMAT_NV15:
  1860. case DRM_FORMAT_NV20:
  1861. case DRM_FORMAT_NV30:
  1862. func_insert_value = &insert_value_yuv_planar_10bpp;
  1863. break;
  1864. case DRM_FORMAT_ARGB8888:
  1865. case DRM_FORMAT_XRGB8888:
  1866. case DRM_FORMAT_ABGR8888:
  1867. case DRM_FORMAT_XBGR8888:
  1868. case DRM_FORMAT_RGBA8888:
  1869. case DRM_FORMAT_RGBX8888:
  1870. case DRM_FORMAT_BGRA8888:
  1871. case DRM_FORMAT_BGRX8888:
  1872. case DRM_FORMAT_ARGB2101010:
  1873. case DRM_FORMAT_XRGB2101010:
  1874. case DRM_FORMAT_ABGR2101010:
  1875. case DRM_FORMAT_XBGR2101010:
  1876. case DRM_FORMAT_RGBA1010102:
  1877. case DRM_FORMAT_RGBX1010102:
  1878. case DRM_FORMAT_BGRA1010102:
  1879. case DRM_FORMAT_BGRX1010102:
  1880. func_insert_value = &insert_value_rgb32;
  1881. break;
  1882. case DRM_FORMAT_XRGB16161616F:
  1883. case DRM_FORMAT_XBGR16161616F:
  1884. case DRM_FORMAT_ARGB16161616F:
  1885. case DRM_FORMAT_ABGR16161616F:
  1886. func_insert_value = &insert_value_rgb16fp;
  1887. break;
  1888. default:
  1889. return;
  1890. }
  1891. for (y = 0; y < height; y++) {
  1892. for (x = 0; x < width; x++) {
  1893. struct color_rgba color = get_rgb_color((width * y) + x, pattern);
  1894. (*func_insert_value)(info, planes, stride, x, y, &color);
  1895. }
  1896. }
  1897. }
  1898. /*
  1899. * util_fill_pattern - Fill a buffer with a test pattern
  1900. * @format: Pixel format
  1901. * @pattern: Test pattern
  1902. * @planes: Array of buffers
  1903. * @width: Width in pixels
  1904. * @height: Height in pixels
  1905. * @stride: Line stride (pitch) in bytes
  1906. * @seed: Seed for noise patterns if zero a default time based seed will be used
  1907. *
  1908. * Fill the buffers with the test pattern specified by the pattern parameter.
  1909. * Supported formats vary depending on the selected pattern.
  1910. */
  1911. void util_fill_pattern(uint32_t format, enum util_fill_pattern pattern,
  1912. void *planes[3], unsigned int width,
  1913. unsigned int height, unsigned int stride, unsigned long seed)
  1914. {
  1915. const struct util_format_info *info;
  1916. info = util_format_info_find(format);
  1917. if (info == NULL)
  1918. return;
  1919. switch (pattern) {
  1920. case UTIL_PATTERN_NOISE:
  1921. case UTIL_PATTERN_NOISE_COLOR:
  1922. if (!seed)
  1923. seed = time(NULL);
  1924. srand(seed);
  1925. printf("Seed used for noise patterns %lu\n", seed);
  1926. break;
  1927. default:
  1928. break;
  1929. }
  1930. switch (pattern) {
  1931. case UTIL_PATTERN_TILES:
  1932. return fill_tiles(info, planes, width, height, stride);
  1933. case UTIL_PATTERN_SMPTE:
  1934. return fill_smpte(info, planes, width, height, stride);
  1935. case UTIL_PATTERN_PLAIN:
  1936. return fill_plain(info, planes, height, stride);
  1937. case UTIL_PATTERN_GRADIENT:
  1938. return fill_gradient(info, planes, width, height, stride);
  1939. case UTIL_PATTERN_NOISE:
  1940. case UTIL_PATTERN_NOISE_COLOR:
  1941. case UTIL_PATTERN_BLACK_WHITE:
  1942. return fill_simple_patterns(info, planes, width, height, stride,
  1943. pattern);
  1944. default:
  1945. printf("Error: unsupported test pattern %u.\n", pattern);
  1946. break;
  1947. }
  1948. }
  1949. static const char *pattern_names[] = {
  1950. [UTIL_PATTERN_TILES] = "tiles",
  1951. [UTIL_PATTERN_SMPTE] = "smpte",
  1952. [UTIL_PATTERN_PLAIN] = "plain",
  1953. [UTIL_PATTERN_GRADIENT] = "gradient",
  1954. [UTIL_PATTERN_NOISE] = "noise",
  1955. [UTIL_PATTERN_NOISE_COLOR] = "noise-color",
  1956. [UTIL_PATTERN_BLACK_WHITE] = "black-white",
  1957. };
  1958. enum util_fill_pattern util_pattern_enum(const char *name)
  1959. {
  1960. unsigned int i;
  1961. for (i = 0; i < ARRAY_SIZE(pattern_names); i++)
  1962. if (!strcmp(pattern_names[i], name))
  1963. return (enum util_fill_pattern)i;
  1964. printf("Error: unsupported test pattern %s.\n", name);
  1965. return UTIL_PATTERN_SMPTE;
  1966. }