| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212 |
- /*
- * Copyright 2008 Tungsten Graphics
- * Jakob Bornecrantz <jakob@tungstengraphics.com>
- * Copyright 2008 Intel Corporation
- * Jesse Barnes <jesse.barnes@intel.com>
- *
- * Permission is hereby granted, free of charge, to any person obtaining a
- * copy of this software and associated documentation files (the "Software"),
- * to deal in the Software without restriction, including without limitation
- * the rights to use, copy, modify, merge, publish, distribute, sublicense,
- * and/or sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- * IN THE SOFTWARE.
- */
- #include <assert.h>
- #include <stdbool.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #include <drm_fourcc.h>
- #if HAVE_CAIRO
- #include <cairo.h>
- #include <math.h>
- #endif
- #include "common.h"
- #include "format.h"
- #include "pattern.h"
- struct color_rgba {
- uint16_t red;
- uint16_t green;
- uint16_t blue;
- uint16_t alpha;
- };
- struct color_rgb24 {
- unsigned int value:24;
- } __attribute__((__packed__));
- struct color_yuv {
- unsigned char y;
- unsigned char u;
- unsigned char v;
- };
- #define MAKE_YUV_601_Y(r, g, b) \
- ((( 66 * (r) + 129 * (g) + 25 * (b) + 128) >> 8) + 16)
- #define MAKE_YUV_601_U(r, g, b) \
- (((-38 * (r) - 74 * (g) + 112 * (b) + 128) >> 8) + 128)
- #define MAKE_YUV_601_V(r, g, b) \
- (((112 * (r) - 94 * (g) - 18 * (b) + 128) >> 8) + 128)
- #define MAKE_YUV_601(r, g, b) \
- { .y = MAKE_YUV_601_Y(r, g, b), \
- .u = MAKE_YUV_601_U(r, g, b), \
- .v = MAKE_YUV_601_V(r, g, b) }
- static inline uint16_t swap16(uint16_t x)
- {
- return ((x & 0x00ffU) << 8) | ((x & 0xff00U) >> 8);
- }
- static inline uint32_t swap32(uint32_t x)
- {
- return ((x & 0x000000ffU) << 24) |
- ((x & 0x0000ff00U) << 8) |
- ((x & 0x00ff0000U) >> 8) |
- ((x & 0xff000000U) >> 24);
- }
- #ifdef HAVE_BIG_ENDIAN
- #define cpu_to_be16(x) (x)
- #define cpu_to_le16(x) swap16(x)
- #define cpu_to_le32(x) swap32(x)
- #define fb_foreign_endian(format) (!((format) & DRM_FORMAT_BIG_ENDIAN))
- #else
- #define cpu_to_be16(x) swap16(x)
- #define cpu_to_le16(x) (x)
- #define cpu_to_le32(x) (x)
- #define fb_foreign_endian(format) ((format) & DRM_FORMAT_BIG_ENDIAN)
- #endif
- #define cpu_to_fb16(x) (fb_be ? cpu_to_be16(x) : cpu_to_le16(x))
- /* This function takes 8-bit color values */
- static inline uint32_t shiftcolor8(const struct util_color_component *comp,
- uint32_t value)
- {
- value &= 0xff;
- /* Fill the low bits with the high bits. */
- value = (value << 8) | value;
- /* Shift down to remove unwanted low bits */
- value = value >> (16 - comp->length);
- /* Shift back up to where the value should be */
- return value << comp->offset;
- }
- /* This function takes 10-bit color values */
- static inline uint32_t shiftcolor10(const struct util_color_component *comp,
- uint32_t value)
- {
- value &= 0x3ff;
- /* Fill the low bits with the high bits. */
- value = (value << 6) | (value >> 4);
- /* Shift down to remove unwanted low bits */
- value = value >> (16 - comp->length);
- /* Shift back up to where the value should be */
- return value << comp->offset;
- }
- /* This function takes 16-bit color values */
- static inline uint64_t shiftcolor16(const struct util_color_component *comp,
- uint64_t value)
- {
- value &= 0xffff;
- /* Shift down to remove unwanted low bits */
- value = value >> (16 - comp->length);
- /* Shift back up to where the value should be */
- return value << comp->offset;
- }
- #define MAKE_RGBA10(rgb, r, g, b, a) \
- (shiftcolor10(&(rgb)->red, (r)) | \
- shiftcolor10(&(rgb)->green, (g)) | \
- shiftcolor10(&(rgb)->blue, (b)) | \
- shiftcolor10(&(rgb)->alpha, (a)))
- #define MAKE_RGBA(rgb, r, g, b, a) \
- (shiftcolor8(&(rgb)->red, (r)) | \
- shiftcolor8(&(rgb)->green, (g)) | \
- shiftcolor8(&(rgb)->blue, (b)) | \
- shiftcolor8(&(rgb)->alpha, (a)))
- #define MAKE_RGB24(rgb, r, g, b) \
- { .value = MAKE_RGBA(rgb, r, g, b, 0) }
- /**
- * Takes a uint16_t, divides by 65536, converts the infinite-precision
- * result to fp16 with round-to-zero.
- *
- * Copied from mesa:src/util/half_float.c
- */
- static uint16_t uint16_div_64k_to_half(uint16_t v)
- {
- /* Zero or subnormal. Set the mantissa to (v << 8) and return. */
- if (v < 4)
- return v << 8;
- /* Count the leading 0s in the uint16_t */
- int n = __builtin_clz(v) - 16;
- /* Shift the mantissa up so bit 16 is the hidden 1 bit,
- * mask it off, then shift back down to 10 bits
- */
- int m = ( ((uint32_t)v << (n + 1)) & 0xffff ) >> 6;
- /* (0{n} 1 X{15-n}) * 2^-16
- * = 1.X * 2^(15-n-16)
- * = 1.X * 2^(14-n - 15)
- * which is the FP16 form with e = 14 - n
- */
- int e = 14 - n;
- return (e << 10) | m;
- }
- #define MAKE_RGBA8FP16(rgb, r, g, b, a) \
- (shiftcolor16(&(rgb)->red, uint16_div_64k_to_half((r) << 8)) | \
- shiftcolor16(&(rgb)->green, uint16_div_64k_to_half((g) << 8)) | \
- shiftcolor16(&(rgb)->blue, uint16_div_64k_to_half((b) << 8)) | \
- shiftcolor16(&(rgb)->alpha, uint16_div_64k_to_half((a) << 8)))
- #define MAKE_RGBA10FP16(rgb, r, g, b, a) \
- (shiftcolor16(&(rgb)->red, uint16_div_64k_to_half((r) << 6)) | \
- shiftcolor16(&(rgb)->green, uint16_div_64k_to_half((g) << 6)) | \
- shiftcolor16(&(rgb)->blue, uint16_div_64k_to_half((b) << 6)) | \
- shiftcolor16(&(rgb)->alpha, uint16_div_64k_to_half((a) << 6)))
- static void fill_smpte_yuv_planar(const struct util_yuv_info *yuv,
- unsigned char *y_mem, unsigned char *u_mem,
- unsigned char *v_mem, unsigned int width,
- unsigned int height, unsigned int stride)
- {
- const struct color_yuv colors_top[] = {
- MAKE_YUV_601(192, 192, 192), /* grey */
- MAKE_YUV_601(192, 192, 0), /* yellow */
- MAKE_YUV_601(0, 192, 192), /* cyan */
- MAKE_YUV_601(0, 192, 0), /* green */
- MAKE_YUV_601(192, 0, 192), /* magenta */
- MAKE_YUV_601(192, 0, 0), /* red */
- MAKE_YUV_601(0, 0, 192), /* blue */
- };
- const struct color_yuv colors_middle[] = {
- MAKE_YUV_601(0, 0, 192), /* blue */
- MAKE_YUV_601(19, 19, 19), /* black */
- MAKE_YUV_601(192, 0, 192), /* magenta */
- MAKE_YUV_601(19, 19, 19), /* black */
- MAKE_YUV_601(0, 192, 192), /* cyan */
- MAKE_YUV_601(19, 19, 19), /* black */
- MAKE_YUV_601(192, 192, 192), /* grey */
- };
- const struct color_yuv colors_bottom[] = {
- MAKE_YUV_601(0, 33, 76), /* in-phase */
- MAKE_YUV_601(255, 255, 255), /* super white */
- MAKE_YUV_601(50, 0, 106), /* quadrature */
- MAKE_YUV_601(19, 19, 19), /* black */
- MAKE_YUV_601(9, 9, 9), /* 3.5% */
- MAKE_YUV_601(19, 19, 19), /* 7.5% */
- MAKE_YUV_601(29, 29, 29), /* 11.5% */
- MAKE_YUV_601(19, 19, 19), /* black */
- };
- unsigned int cs = yuv->chroma_stride;
- unsigned int xsub = yuv->xsub;
- unsigned int ysub = yuv->ysub;
- unsigned int x;
- unsigned int y;
- /* Luma */
- for (y = 0; y < height * 6 / 9; ++y) {
- for (x = 0; x < width; ++x)
- y_mem[x] = colors_top[x * 7 / width].y;
- y_mem += stride;
- }
- for (; y < height * 7 / 9; ++y) {
- for (x = 0; x < width; ++x)
- y_mem[x] = colors_middle[x * 7 / width].y;
- y_mem += stride;
- }
- for (; y < height; ++y) {
- for (x = 0; x < width * 5 / 7; ++x)
- y_mem[x] = colors_bottom[x * 4 / (width * 5 / 7)].y;
- for (; x < width * 6 / 7; ++x)
- y_mem[x] = colors_bottom[(x - width * 5 / 7) * 3
- / (width / 7) + 4].y;
- for (; x < width; ++x)
- y_mem[x] = colors_bottom[7].y;
- y_mem += stride;
- }
- /* Chroma */
- for (y = 0; y < height / ysub * 6 / 9; ++y) {
- for (x = 0; x < width; x += xsub) {
- u_mem[x*cs/xsub] = colors_top[x * 7 / width].u;
- v_mem[x*cs/xsub] = colors_top[x * 7 / width].v;
- }
- u_mem += stride * cs / xsub;
- v_mem += stride * cs / xsub;
- }
- for (; y < height / ysub * 7 / 9; ++y) {
- for (x = 0; x < width; x += xsub) {
- u_mem[x*cs/xsub] = colors_middle[x * 7 / width].u;
- v_mem[x*cs/xsub] = colors_middle[x * 7 / width].v;
- }
- u_mem += stride * cs / xsub;
- v_mem += stride * cs / xsub;
- }
- for (; y < height / ysub; ++y) {
- for (x = 0; x < width * 5 / 7; x += xsub) {
- u_mem[x*cs/xsub] =
- colors_bottom[x * 4 / (width * 5 / 7)].u;
- v_mem[x*cs/xsub] =
- colors_bottom[x * 4 / (width * 5 / 7)].v;
- }
- for (; x < width * 6 / 7; x += xsub) {
- u_mem[x*cs/xsub] = colors_bottom[(x - width * 5 / 7) *
- 3 / (width / 7) + 4].u;
- v_mem[x*cs/xsub] = colors_bottom[(x - width * 5 / 7) *
- 3 / (width / 7) + 4].v;
- }
- for (; x < width; x += xsub) {
- u_mem[x*cs/xsub] = colors_bottom[7].u;
- v_mem[x*cs/xsub] = colors_bottom[7].v;
- }
- u_mem += stride * cs / xsub;
- v_mem += stride * cs / xsub;
- }
- }
- static void write_pixels_10bpp(unsigned char *mem,
- unsigned short a,
- unsigned short b,
- unsigned short c,
- unsigned short d)
- {
- mem[0] = (a & 0xff);
- mem[1] = ((a >> 8) & 0x3) | ((b & 0x3f) << 2);
- mem[2] = ((b >> 6) & 0xf) | ((c & 0xf) << 4);
- mem[3] = ((c >> 4) & 0x3f) | ((d & 0x3) << 6);
- mem[4] = ((d >> 2) & 0xff);
- }
- static void update_pixels_10bpp(unsigned char *mem, uint64_t val, uint64_t mask)
- {
- int i;
- for (i = 0; i < 5; i++, mask >>= 8, val >>= 8) {
- mem[i] &= ~(mask & 0xff);
- mem[i] |= (mask & 0xff) & val;
- }
- }
- static void fill_smpte_yuv_planar_10bpp(const struct util_yuv_info *yuv,
- unsigned char *y_mem,
- unsigned char *uv_mem,
- unsigned int width,
- unsigned int height,
- unsigned int stride)
- {
- const struct color_yuv colors_top[] = {
- MAKE_YUV_601(192, 192, 192), /* grey */
- MAKE_YUV_601(192, 192, 0), /* yellow */
- MAKE_YUV_601(0, 192, 192), /* cyan */
- MAKE_YUV_601(0, 192, 0), /* green */
- MAKE_YUV_601(192, 0, 192), /* magenta */
- MAKE_YUV_601(192, 0, 0), /* red */
- MAKE_YUV_601(0, 0, 192), /* blue */
- };
- const struct color_yuv colors_middle[] = {
- MAKE_YUV_601(0, 0, 192), /* blue */
- MAKE_YUV_601(19, 19, 19), /* black */
- MAKE_YUV_601(192, 0, 192), /* magenta */
- MAKE_YUV_601(19, 19, 19), /* black */
- MAKE_YUV_601(0, 192, 192), /* cyan */
- MAKE_YUV_601(19, 19, 19), /* black */
- MAKE_YUV_601(192, 192, 192), /* grey */
- };
- const struct color_yuv colors_bottom[] = {
- MAKE_YUV_601(0, 33, 76), /* in-phase */
- MAKE_YUV_601(255, 255, 255), /* super white */
- MAKE_YUV_601(50, 0, 106), /* quadrature */
- MAKE_YUV_601(19, 19, 19), /* black */
- MAKE_YUV_601(9, 9, 9), /* 3.5% */
- MAKE_YUV_601(19, 19, 19), /* 7.5% */
- MAKE_YUV_601(29, 29, 29), /* 11.5% */
- MAKE_YUV_601(19, 19, 19), /* black */
- };
- unsigned int cs = yuv->chroma_stride;
- unsigned int xsub = yuv->xsub;
- unsigned int ysub = yuv->ysub;
- unsigned int xstep = cs * xsub;
- unsigned int x;
- unsigned int y;
- /* Luma */
- for (y = 0; y < height * 6 / 9; ++y) {
- for (x = 0; x < width; x += 4)
- write_pixels_10bpp(&y_mem[(x * 5) / 4],
- colors_top[(x+0) * 7 / width].y << 2,
- colors_top[(x+1) * 7 / width].y << 2,
- colors_top[(x+2) * 7 / width].y << 2,
- colors_top[(x+3) * 7 / width].y << 2);
- y_mem += stride;
- }
- for (; y < height * 7 / 9; ++y) {
- for (x = 0; x < width; x += 4)
- write_pixels_10bpp(&y_mem[(x * 5) / 4],
- colors_middle[(x+0) * 7 / width].y << 2,
- colors_middle[(x+1) * 7 / width].y << 2,
- colors_middle[(x+2) * 7 / width].y << 2,
- colors_middle[(x+3) * 7 / width].y << 2);
- y_mem += stride;
- }
- for (; y < height; ++y) {
- for (x = 0; x < width * 5 / 7; x += 4)
- write_pixels_10bpp(&y_mem[(x * 5) / 4],
- colors_bottom[(x+0) * 4 / (width * 5 / 7)].y << 2,
- colors_bottom[(x+1) * 4 / (width * 5 / 7)].y << 2,
- colors_bottom[(x+2) * 4 / (width * 5 / 7)].y << 2,
- colors_bottom[(x+3) * 4 / (width * 5 / 7)].y << 2);
- for (; x < width * 6 / 7; x += 4)
- write_pixels_10bpp(&y_mem[(x * 5) / 4],
- colors_bottom[((x+0) - width * 5 / 7) * 3 / (width / 7) + 4].y << 2,
- colors_bottom[((x+1) - width * 5 / 7) * 3 / (width / 7) + 4].y << 2,
- colors_bottom[((x+2) - width * 5 / 7) * 3 / (width / 7) + 4].y << 2,
- colors_bottom[((x+3) - width * 5 / 7) * 3 / (width / 7) + 4].y << 2);
- for (; x < width; x += 4)
- write_pixels_10bpp(&y_mem[(x * 5) / 4],
- colors_bottom[7].y << 2,
- colors_bottom[7].y << 2,
- colors_bottom[7].y << 2,
- colors_bottom[7].y << 2);
- y_mem += stride;
- }
- /* Chroma */
- for (y = 0; y < height * 6 / 9; y += ysub) {
- for (x = 0; x < width; x += xstep)
- write_pixels_10bpp(&uv_mem[(x * 5) / xstep],
- colors_top[(x+0) * 7 / width].u << 2,
- colors_top[(x+0) * 7 / width].v << 2,
- colors_top[(x+xsub) * 7 / width].u << 2,
- colors_top[(x+xsub) * 7 / width].v << 2);
- uv_mem += stride * cs / xsub;
- }
- for (; y < height * 7 / 9; y += ysub) {
- for (x = 0; x < width; x += xstep)
- write_pixels_10bpp(&uv_mem[(x * 5) / xstep],
- colors_middle[(x+0) * 7 / width].u << 2,
- colors_middle[(x+0) * 7 / width].v << 2,
- colors_middle[(x+xsub) * 7 / width].u << 2,
- colors_middle[(x+xsub) * 7 / width].v << 2);
- uv_mem += stride * cs / xsub;
- }
- for (; y < height; y += ysub) {
- for (x = 0; x < width * 5 / 7; x += xstep)
- write_pixels_10bpp(&uv_mem[(x * 5) / xstep],
- colors_bottom[(x+0) * 4 / (width * 5 / 7)].u << 2,
- colors_bottom[(x+0) * 4 / (width * 5 / 7)].v << 2,
- colors_bottom[(x+xsub) * 4 / (width * 5 / 7)].u << 2,
- colors_bottom[(x+xsub) * 4 / (width * 5 / 7)].v << 2);
- for (; x < width * 6 / 7; x += xstep)
- write_pixels_10bpp(&uv_mem[(x * 5) / xstep],
- colors_bottom[((x+0) - width * 5 / 7) * 3 / (width / 7) + 4].u << 2,
- colors_bottom[((x+0) - width * 5 / 7) * 3 / (width / 7) + 4].v << 2,
- colors_bottom[((x+xsub) - width * 5 / 7) * 3 / (width / 7) + 4].u << 2,
- colors_bottom[((x+xsub) - width * 5 / 7) * 3 / (width / 7) + 4].v << 2);
- for (; x < width; x += xstep)
- write_pixels_10bpp(&uv_mem[(x * 5) / xstep],
- colors_bottom[7].u << 2,
- colors_bottom[7].v << 2,
- colors_bottom[7].u << 2,
- colors_bottom[7].v << 2);
- uv_mem += stride * cs / xsub;
- }
- }
- static void fill_smpte_yuv_packed(const struct util_yuv_info *yuv, void *mem,
- unsigned int width, unsigned int height,
- unsigned int stride)
- {
- const struct color_yuv colors_top[] = {
- MAKE_YUV_601(192, 192, 192), /* grey */
- MAKE_YUV_601(192, 192, 0), /* yellow */
- MAKE_YUV_601(0, 192, 192), /* cyan */
- MAKE_YUV_601(0, 192, 0), /* green */
- MAKE_YUV_601(192, 0, 192), /* magenta */
- MAKE_YUV_601(192, 0, 0), /* red */
- MAKE_YUV_601(0, 0, 192), /* blue */
- };
- const struct color_yuv colors_middle[] = {
- MAKE_YUV_601(0, 0, 192), /* blue */
- MAKE_YUV_601(19, 19, 19), /* black */
- MAKE_YUV_601(192, 0, 192), /* magenta */
- MAKE_YUV_601(19, 19, 19), /* black */
- MAKE_YUV_601(0, 192, 192), /* cyan */
- MAKE_YUV_601(19, 19, 19), /* black */
- MAKE_YUV_601(192, 192, 192), /* grey */
- };
- const struct color_yuv colors_bottom[] = {
- MAKE_YUV_601(0, 33, 76), /* in-phase */
- MAKE_YUV_601(255, 255, 255), /* super white */
- MAKE_YUV_601(50, 0, 106), /* quadrature */
- MAKE_YUV_601(19, 19, 19), /* black */
- MAKE_YUV_601(9, 9, 9), /* 3.5% */
- MAKE_YUV_601(19, 19, 19), /* 7.5% */
- MAKE_YUV_601(29, 29, 29), /* 11.5% */
- MAKE_YUV_601(19, 19, 19), /* black */
- };
- unsigned char *y_mem = (yuv->order & YUV_YC) ? mem : mem + 1;
- unsigned char *c_mem = (yuv->order & YUV_CY) ? mem : mem + 1;
- unsigned int u = (yuv->order & YUV_YCrCb) ? 2 : 0;
- unsigned int v = (yuv->order & YUV_YCbCr) ? 2 : 0;
- unsigned int x;
- unsigned int y;
- /* Luma */
- for (y = 0; y < height * 6 / 9; ++y) {
- for (x = 0; x < width; ++x)
- y_mem[2*x] = colors_top[x * 7 / width].y;
- y_mem += stride;
- }
- for (; y < height * 7 / 9; ++y) {
- for (x = 0; x < width; ++x)
- y_mem[2*x] = colors_middle[x * 7 / width].y;
- y_mem += stride;
- }
- for (; y < height; ++y) {
- for (x = 0; x < width * 5 / 7; ++x)
- y_mem[2*x] = colors_bottom[x * 4 / (width * 5 / 7)].y;
- for (; x < width * 6 / 7; ++x)
- y_mem[2*x] = colors_bottom[(x - width * 5 / 7) * 3
- / (width / 7) + 4].y;
- for (; x < width; ++x)
- y_mem[2*x] = colors_bottom[7].y;
- y_mem += stride;
- }
- /* Chroma */
- for (y = 0; y < height * 6 / 9; ++y) {
- for (x = 0; x < width; x += 2) {
- c_mem[2*x+u] = colors_top[x * 7 / width].u;
- c_mem[2*x+v] = colors_top[x * 7 / width].v;
- }
- c_mem += stride;
- }
- for (; y < height * 7 / 9; ++y) {
- for (x = 0; x < width; x += 2) {
- c_mem[2*x+u] = colors_middle[x * 7 / width].u;
- c_mem[2*x+v] = colors_middle[x * 7 / width].v;
- }
- c_mem += stride;
- }
- for (; y < height; ++y) {
- for (x = 0; x < width * 5 / 7; x += 2) {
- c_mem[2*x+u] = colors_bottom[x * 4 / (width * 5 / 7)].u;
- c_mem[2*x+v] = colors_bottom[x * 4 / (width * 5 / 7)].v;
- }
- for (; x < width * 6 / 7; x += 2) {
- c_mem[2*x+u] = colors_bottom[(x - width * 5 / 7) *
- 3 / (width / 7) + 4].u;
- c_mem[2*x+v] = colors_bottom[(x - width * 5 / 7) *
- 3 / (width / 7) + 4].v;
- }
- for (; x < width; x += 2) {
- c_mem[2*x+u] = colors_bottom[7].u;
- c_mem[2*x+v] = colors_bottom[7].v;
- }
- c_mem += stride;
- }
- }
- static void fill_smpte_rgb16(const struct util_rgb_info *rgb, void *mem,
- unsigned int width, unsigned int height,
- unsigned int stride, bool fb_be)
- {
- const uint16_t colors_top[] = {
- MAKE_RGBA(rgb, 192, 192, 192, 255), /* grey */
- MAKE_RGBA(rgb, 192, 192, 0, 255), /* yellow */
- MAKE_RGBA(rgb, 0, 192, 192, 255), /* cyan */
- MAKE_RGBA(rgb, 0, 192, 0, 255), /* green */
- MAKE_RGBA(rgb, 192, 0, 192, 255), /* magenta */
- MAKE_RGBA(rgb, 192, 0, 0, 255), /* red */
- MAKE_RGBA(rgb, 0, 0, 192, 255), /* blue */
- };
- const uint16_t colors_middle[] = {
- MAKE_RGBA(rgb, 0, 0, 192, 127), /* blue */
- MAKE_RGBA(rgb, 19, 19, 19, 127), /* black */
- MAKE_RGBA(rgb, 192, 0, 192, 127), /* magenta */
- MAKE_RGBA(rgb, 19, 19, 19, 127), /* black */
- MAKE_RGBA(rgb, 0, 192, 192, 127), /* cyan */
- MAKE_RGBA(rgb, 19, 19, 19, 127), /* black */
- MAKE_RGBA(rgb, 192, 192, 192, 127), /* grey */
- };
- const uint16_t colors_bottom[] = {
- MAKE_RGBA(rgb, 0, 33, 76, 255), /* in-phase */
- MAKE_RGBA(rgb, 255, 255, 255, 255), /* super white */
- MAKE_RGBA(rgb, 50, 0, 106, 255), /* quadrature */
- MAKE_RGBA(rgb, 19, 19, 19, 255), /* black */
- MAKE_RGBA(rgb, 9, 9, 9, 255), /* 3.5% */
- MAKE_RGBA(rgb, 19, 19, 19, 255), /* 7.5% */
- MAKE_RGBA(rgb, 29, 29, 29, 255), /* 11.5% */
- MAKE_RGBA(rgb, 19, 19, 19, 255), /* black */
- };
- unsigned int x;
- unsigned int y;
- for (y = 0; y < height * 6 / 9; ++y) {
- for (x = 0; x < width; ++x)
- ((uint16_t *)mem)[x] = cpu_to_fb16(colors_top[x * 7 / width]);
- mem += stride;
- }
- for (; y < height * 7 / 9; ++y) {
- for (x = 0; x < width; ++x)
- ((uint16_t *)mem)[x] = cpu_to_fb16(colors_middle[x * 7 / width]);
- mem += stride;
- }
- for (; y < height; ++y) {
- for (x = 0; x < width * 5 / 7; ++x)
- ((uint16_t *)mem)[x] =
- cpu_to_fb16(colors_bottom[x * 4 / (width * 5 / 7)]);
- for (; x < width * 6 / 7; ++x)
- ((uint16_t *)mem)[x] =
- cpu_to_fb16(colors_bottom[(x - width * 5 / 7) * 3
- / (width / 7) + 4]);
- for (; x < width; ++x)
- ((uint16_t *)mem)[x] = cpu_to_fb16(colors_bottom[7]);
- mem += stride;
- }
- }
- static void fill_smpte_rgb24(const struct util_rgb_info *rgb, void *mem,
- unsigned int width, unsigned int height,
- unsigned int stride)
- {
- const struct color_rgb24 colors_top[] = {
- MAKE_RGB24(rgb, 192, 192, 192), /* grey */
- MAKE_RGB24(rgb, 192, 192, 0), /* yellow */
- MAKE_RGB24(rgb, 0, 192, 192), /* cyan */
- MAKE_RGB24(rgb, 0, 192, 0), /* green */
- MAKE_RGB24(rgb, 192, 0, 192), /* magenta */
- MAKE_RGB24(rgb, 192, 0, 0), /* red */
- MAKE_RGB24(rgb, 0, 0, 192), /* blue */
- };
- const struct color_rgb24 colors_middle[] = {
- MAKE_RGB24(rgb, 0, 0, 192), /* blue */
- MAKE_RGB24(rgb, 19, 19, 19), /* black */
- MAKE_RGB24(rgb, 192, 0, 192), /* magenta */
- MAKE_RGB24(rgb, 19, 19, 19), /* black */
- MAKE_RGB24(rgb, 0, 192, 192), /* cyan */
- MAKE_RGB24(rgb, 19, 19, 19), /* black */
- MAKE_RGB24(rgb, 192, 192, 192), /* grey */
- };
- const struct color_rgb24 colors_bottom[] = {
- MAKE_RGB24(rgb, 0, 33, 76), /* in-phase */
- MAKE_RGB24(rgb, 255, 255, 255), /* super white */
- MAKE_RGB24(rgb, 50, 0, 106), /* quadrature */
- MAKE_RGB24(rgb, 19, 19, 19), /* black */
- MAKE_RGB24(rgb, 9, 9, 9), /* 3.5% */
- MAKE_RGB24(rgb, 19, 19, 19), /* 7.5% */
- MAKE_RGB24(rgb, 29, 29, 29), /* 11.5% */
- MAKE_RGB24(rgb, 19, 19, 19), /* black */
- };
- unsigned int x;
- unsigned int y;
- for (y = 0; y < height * 6 / 9; ++y) {
- for (x = 0; x < width; ++x)
- ((struct color_rgb24 *)mem)[x] =
- colors_top[x * 7 / width];
- mem += stride;
- }
- for (; y < height * 7 / 9; ++y) {
- for (x = 0; x < width; ++x)
- ((struct color_rgb24 *)mem)[x] =
- colors_middle[x * 7 / width];
- mem += stride;
- }
- for (; y < height; ++y) {
- for (x = 0; x < width * 5 / 7; ++x)
- ((struct color_rgb24 *)mem)[x] =
- colors_bottom[x * 4 / (width * 5 / 7)];
- for (; x < width * 6 / 7; ++x)
- ((struct color_rgb24 *)mem)[x] =
- colors_bottom[(x - width * 5 / 7) * 3
- / (width / 7) + 4];
- for (; x < width; ++x)
- ((struct color_rgb24 *)mem)[x] = colors_bottom[7];
- mem += stride;
- }
- }
- static void fill_smpte_rgb32(const struct util_rgb_info *rgb, void *mem,
- unsigned int width, unsigned int height,
- unsigned int stride)
- {
- const uint32_t colors_top[] = {
- MAKE_RGBA(rgb, 192, 192, 192, 255), /* grey */
- MAKE_RGBA(rgb, 192, 192, 0, 255), /* yellow */
- MAKE_RGBA(rgb, 0, 192, 192, 255), /* cyan */
- MAKE_RGBA(rgb, 0, 192, 0, 255), /* green */
- MAKE_RGBA(rgb, 192, 0, 192, 255), /* magenta */
- MAKE_RGBA(rgb, 192, 0, 0, 255), /* red */
- MAKE_RGBA(rgb, 0, 0, 192, 255), /* blue */
- };
- const uint32_t colors_middle[] = {
- MAKE_RGBA(rgb, 0, 0, 192, 127), /* blue */
- MAKE_RGBA(rgb, 19, 19, 19, 127), /* black */
- MAKE_RGBA(rgb, 192, 0, 192, 127), /* magenta */
- MAKE_RGBA(rgb, 19, 19, 19, 127), /* black */
- MAKE_RGBA(rgb, 0, 192, 192, 127), /* cyan */
- MAKE_RGBA(rgb, 19, 19, 19, 127), /* black */
- MAKE_RGBA(rgb, 192, 192, 192, 127), /* grey */
- };
- const uint32_t colors_bottom[] = {
- MAKE_RGBA(rgb, 0, 33, 76, 255), /* in-phase */
- MAKE_RGBA(rgb, 255, 255, 255, 255), /* super white */
- MAKE_RGBA(rgb, 50, 0, 106, 255), /* quadrature */
- MAKE_RGBA(rgb, 19, 19, 19, 255), /* black */
- MAKE_RGBA(rgb, 9, 9, 9, 255), /* 3.5% */
- MAKE_RGBA(rgb, 19, 19, 19, 255), /* 7.5% */
- MAKE_RGBA(rgb, 29, 29, 29, 255), /* 11.5% */
- MAKE_RGBA(rgb, 19, 19, 19, 255), /* black */
- };
- unsigned int x;
- unsigned int y;
- for (y = 0; y < height * 6 / 9; ++y) {
- for (x = 0; x < width; ++x)
- ((uint32_t *)mem)[x] = cpu_to_le32(colors_top[x * 7 / width]);
- mem += stride;
- }
- for (; y < height * 7 / 9; ++y) {
- for (x = 0; x < width; ++x)
- ((uint32_t *)mem)[x] = cpu_to_le32(colors_middle[x * 7 / width]);
- mem += stride;
- }
- for (; y < height; ++y) {
- for (x = 0; x < width * 5 / 7; ++x)
- ((uint32_t *)mem)[x] =
- cpu_to_le32(colors_bottom[x * 4 / (width * 5 / 7)]);
- for (; x < width * 6 / 7; ++x)
- ((uint32_t *)mem)[x] =
- cpu_to_le32(colors_bottom[(x - width * 5 / 7) * 3
- / (width / 7) + 4]);
- for (; x < width; ++x)
- ((uint32_t *)mem)[x] = cpu_to_le32(colors_bottom[7]);
- mem += stride;
- }
- }
- static void fill_smpte_rgb16fp(const struct util_rgb_info *rgb, void *mem,
- unsigned int width, unsigned int height,
- unsigned int stride)
- {
- const uint64_t colors_top[] = {
- MAKE_RGBA8FP16(rgb, 192, 192, 192, 255),/* grey */
- MAKE_RGBA8FP16(rgb, 192, 192, 0, 255), /* yellow */
- MAKE_RGBA8FP16(rgb, 0, 192, 192, 255), /* cyan */
- MAKE_RGBA8FP16(rgb, 0, 192, 0, 255), /* green */
- MAKE_RGBA8FP16(rgb, 192, 0, 192, 255), /* magenta */
- MAKE_RGBA8FP16(rgb, 192, 0, 0, 255), /* red */
- MAKE_RGBA8FP16(rgb, 0, 0, 192, 255), /* blue */
- };
- const uint64_t colors_middle[] = {
- MAKE_RGBA8FP16(rgb, 0, 0, 192, 127), /* blue */
- MAKE_RGBA8FP16(rgb, 19, 19, 19, 127), /* black */
- MAKE_RGBA8FP16(rgb, 192, 0, 192, 127), /* magenta */
- MAKE_RGBA8FP16(rgb, 19, 19, 19, 127), /* black */
- MAKE_RGBA8FP16(rgb, 0, 192, 192, 127), /* cyan */
- MAKE_RGBA8FP16(rgb, 19, 19, 19, 127), /* black */
- MAKE_RGBA8FP16(rgb, 192, 192, 192, 127),/* grey */
- };
- const uint64_t colors_bottom[] = {
- MAKE_RGBA8FP16(rgb, 0, 33, 76, 255), /* in-phase */
- MAKE_RGBA8FP16(rgb, 255, 255, 255, 255),/* super white */
- MAKE_RGBA8FP16(rgb, 50, 0, 106, 255), /* quadrature */
- MAKE_RGBA8FP16(rgb, 19, 19, 19, 255), /* black */
- MAKE_RGBA8FP16(rgb, 9, 9, 9, 255), /* 3.5% */
- MAKE_RGBA8FP16(rgb, 19, 19, 19, 255), /* 7.5% */
- MAKE_RGBA8FP16(rgb, 29, 29, 29, 255), /* 11.5% */
- MAKE_RGBA8FP16(rgb, 19, 19, 19, 255), /* black */
- };
- unsigned int x;
- unsigned int y;
- for (y = 0; y < height * 6 / 9; ++y) {
- for (x = 0; x < width; ++x)
- ((uint64_t *)mem)[x] = colors_top[x * 7 / width];
- mem += stride;
- }
- for (; y < height * 7 / 9; ++y) {
- for (x = 0; x < width; ++x)
- ((uint64_t *)mem)[x] = colors_middle[x * 7 / width];
- mem += stride;
- }
- for (; y < height; ++y) {
- for (x = 0; x < width * 5 / 7; ++x)
- ((uint64_t *)mem)[x] =
- colors_bottom[x * 4 / (width * 5 / 7)];
- for (; x < width * 6 / 7; ++x)
- ((uint64_t *)mem)[x] =
- colors_bottom[(x - width * 5 / 7) * 3
- / (width / 7) + 4];
- for (; x < width; ++x)
- ((uint64_t *)mem)[x] = colors_bottom[7];
- mem += stride;
- }
- }
- enum smpte_colors {
- SMPTE_COLOR_GREY,
- SMPTE_COLOR_YELLOW,
- SMPTE_COLOR_CYAN,
- SMPTE_COLOR_GREEN,
- SMPTE_COLOR_MAGENTA,
- SMPTE_COLOR_RED,
- SMPTE_COLOR_BLUE,
- SMPTE_COLOR_BLACK,
- SMPTE_COLOR_IN_PHASE,
- SMPTE_COLOR_SUPER_WHITE,
- SMPTE_COLOR_QUADRATURE,
- SMPTE_COLOR_3PC5,
- SMPTE_COLOR_11PC5,
- };
- static unsigned int smpte_top[7] = {
- SMPTE_COLOR_GREY,
- SMPTE_COLOR_YELLOW,
- SMPTE_COLOR_CYAN,
- SMPTE_COLOR_GREEN,
- SMPTE_COLOR_MAGENTA,
- SMPTE_COLOR_RED,
- SMPTE_COLOR_BLUE,
- };
- static unsigned int smpte_middle[7] = {
- SMPTE_COLOR_BLUE,
- SMPTE_COLOR_BLACK,
- SMPTE_COLOR_MAGENTA,
- SMPTE_COLOR_BLACK,
- SMPTE_COLOR_CYAN,
- SMPTE_COLOR_BLACK,
- SMPTE_COLOR_GREY,
- };
- static unsigned int smpte_bottom[8] = {
- SMPTE_COLOR_IN_PHASE,
- SMPTE_COLOR_SUPER_WHITE,
- SMPTE_COLOR_QUADRATURE,
- SMPTE_COLOR_BLACK,
- SMPTE_COLOR_3PC5,
- SMPTE_COLOR_BLACK,
- SMPTE_COLOR_11PC5,
- SMPTE_COLOR_BLACK,
- };
- #define EXPAND_COLOR(r, g, b) { (r) * 0x101, (g) * 0x101, (b) * 0x101 }
- static const struct drm_color_lut bw_color_lut[] = {
- EXPAND_COLOR( 0, 0, 0), /* black */
- EXPAND_COLOR(255, 255, 255), /* white */
- };
- static const struct drm_color_lut pentile_color_lut[] = {
- /* PenTile RG-GB */
- EXPAND_COLOR( 0, 0, 0), /* black */
- EXPAND_COLOR(255, 0, 0), /* red */
- EXPAND_COLOR( 0, 207, 0), /* green */
- EXPAND_COLOR( 0, 0, 255), /* blue */
- };
- static const struct drm_color_lut smpte_color_lut[] = {
- [SMPTE_COLOR_GREY] = EXPAND_COLOR(192, 192, 192),
- [SMPTE_COLOR_YELLOW] = EXPAND_COLOR(192, 192, 0),
- [SMPTE_COLOR_CYAN] = EXPAND_COLOR( 0, 192, 192),
- [SMPTE_COLOR_GREEN] = EXPAND_COLOR( 0, 192, 0),
- [SMPTE_COLOR_MAGENTA] = EXPAND_COLOR(192, 0, 192),
- [SMPTE_COLOR_RED] = EXPAND_COLOR(192, 0, 0),
- [SMPTE_COLOR_BLUE] = EXPAND_COLOR( 0, 0, 192),
- [SMPTE_COLOR_BLACK] = EXPAND_COLOR( 19, 19, 19),
- [SMPTE_COLOR_IN_PHASE] = EXPAND_COLOR( 0, 33, 76),
- [SMPTE_COLOR_SUPER_WHITE] = EXPAND_COLOR(255, 255, 255),
- [SMPTE_COLOR_QUADRATURE] = EXPAND_COLOR( 50, 0, 106),
- [SMPTE_COLOR_3PC5] = EXPAND_COLOR( 9, 9, 9),
- [SMPTE_COLOR_11PC5] = EXPAND_COLOR( 29, 29, 29),
- };
- #undef EXPAND_COLOR
- /*
- * Floyd-Steinberg dithering
- */
- struct fsd {
- unsigned int width;
- unsigned int x;
- unsigned int i;
- int red;
- int green;
- int blue;
- int error[];
- };
- static struct fsd *fsd_alloc(unsigned int width)
- {
- unsigned int n = 3 * (width + 1);
- struct fsd *fsd = malloc(sizeof(*fsd) + n * sizeof(fsd->error[0]));
- fsd->width = width;
- fsd->x = 0;
- fsd->i = 0;
- memset(fsd->error, 0, n * sizeof(fsd->error[0]));
- return fsd;
- }
- static inline int clamp(int val, int min, int max)
- {
- if (val < min)
- return min;
- if (val > max)
- return max;
- return val;
- }
- static void fsd_dither(struct fsd *fsd, struct drm_color_lut *color)
- {
- unsigned int i = fsd->i;
- fsd->red = (int)color->red + (fsd->error[3 * i] + 8) / 16;
- fsd->green = (int)color->green + (fsd->error[3 * i + 1] + 8) / 16;
- fsd->blue = (int)color->blue + (fsd->error[3 * i + 2] + 8) / 16;
- color->red = clamp(fsd->red, 0, 65535);
- color->green = clamp(fsd->green, 0, 65535);
- color->blue = clamp(fsd->blue, 0, 65535);
- }
- static void fsd_update(struct fsd *fsd, const struct drm_color_lut *actual)
- {
- int error_red = fsd->red - (int)actual->red;
- int error_green = fsd->green - (int)actual->green;
- int error_blue = fsd->blue - (int)actual->blue;
- unsigned int width = fsd->width;
- unsigned int i = fsd->i, j;
- unsigned int n = width + 1;
- /* Distribute errors over neighboring pixels */
- if (fsd->x == width - 1) {
- /* Last pixel on this scanline */
- /* South East: initialize to zero */
- fsd->error[3 * i] = 0;
- fsd->error[3 * i + 1] = 0;
- fsd->error[3 * i + 2] = 0;
- } else {
- /* East: accumulate error */
- j = (i + 1) % n;
- fsd->error[3 * j] += 7 * error_red;
- fsd->error[3 * j + 1] += 7 * error_green;
- fsd->error[3 * j + 2] += 7 * error_blue;
- /* South East: initial error */
- fsd->error[3 * i] = error_red;
- fsd->error[3 * i + 1] = error_green;
- fsd->error[3 * i + 2] = error_blue;
- }
- /* South West: accumulate error */
- j = (i + width - 1) % n;
- fsd->error[3 * j] += 3 * error_red;
- fsd->error[3 * j + 1] += 3 * error_green;
- fsd->error[3 * j + 2] += 3 * error_blue;
- /* South: accumulate error */
- j = (i + width) % n;
- fsd->error[3 * j] += 5 * error_red;
- fsd->error[3 * j + 1] += 5 * error_green;
- fsd->error[3 * j + 2] += 5 * error_blue;
- fsd->x = (fsd->x + 1) % width;
- fsd->i = (fsd->i + 1) % n;
- }
- static void write_pixel_1(uint8_t *mem, unsigned int x, unsigned int pixel)
- {
- unsigned int shift = 7 - (x & 7);
- unsigned int mask = 1U << shift;
- mem[x / 8] = (mem[x / 8] & ~mask) | ((pixel << shift) & mask);
- }
- static void write_color_1(struct fsd *fsd, uint8_t *mem, unsigned int x,
- unsigned int index)
- {
- struct drm_color_lut color = smpte_color_lut[index];
- unsigned int pixel;
- fsd_dither(fsd, &color);
- /* ITU BT.601: Y = 0.299 R + 0.587 G + 0.114 B */
- if (3 * color.red + 6 * color.green + color.blue >= 10 * 32768) {
- pixel = 1;
- color.red = color.green = color.blue = 65535;
- } else {
- pixel = 0;
- color.red = color.green = color.blue = 0;
- }
- fsd_update(fsd, &color);
- write_pixel_1(mem, x, pixel);
- }
- static void fill_smpte_c1(void *mem, unsigned int width, unsigned int height,
- unsigned int stride)
- {
- struct fsd *fsd = fsd_alloc(width);
- unsigned int x;
- unsigned int y;
- for (y = 0; y < height * 6 / 9; ++y) {
- for (x = 0; x < width; ++x)
- write_color_1(fsd, mem, x, smpte_top[x * 7 / width]);
- mem += stride;
- }
- for (; y < height * 7 / 9; ++y) {
- for (x = 0; x < width; ++x)
- write_color_1(fsd, mem, x, smpte_middle[x * 7 / width]);
- mem += stride;
- }
- for (; y < height; ++y) {
- for (x = 0; x < width * 5 / 7; ++x)
- write_color_1(fsd, mem, x,
- smpte_bottom[x * 4 / (width * 5 / 7)]);
- for (; x < width * 6 / 7; ++x)
- write_color_1(fsd, mem, x,
- smpte_bottom[(x - width * 5 / 7) * 3 /
- (width / 7) + 4]);
- for (; x < width; ++x)
- write_color_1(fsd, mem, x, smpte_bottom[7]);
- mem += stride;
- }
- free(fsd);
- }
- static void write_pixel_2(uint8_t *mem, unsigned int x, unsigned int pixel)
- {
- unsigned int shift = 6 - 2 * (x & 3);
- unsigned int mask = 3U << shift;
- mem[x / 4] = (mem[x / 4] & ~mask) | ((pixel << shift) & mask);
- }
- static void write_color_2(struct fsd *fsd, uint8_t *mem, unsigned int stride,
- unsigned int x, unsigned int index)
- {
- struct drm_color_lut color = smpte_color_lut[index];
- unsigned int r, g, b;
- fsd_dither(fsd, &color);
- if (color.red >= 32768) {
- r = 1;
- color.red = 65535;
- } else {
- r = 0;
- color.red = 0;
- }
- if (color.green >= 32768) {
- g = 2;
- color.green = 65535;
- } else {
- g = 0;
- color.green = 0;
- }
- if (color.blue >= 32768) {
- b = 3;
- color.blue = 65535;
- } else {
- b = 0;
- color.blue = 0;
- }
- fsd_update(fsd, &color);
- /* Use PenTile RG-GB */
- write_pixel_2(mem, 2 * x, r);
- write_pixel_2(mem, 2 * x + 1, g);
- write_pixel_2(mem + stride, 2 * x, g);
- write_pixel_2(mem + stride, 2 * x + 1, b);
- }
- static void fill_smpte_c2(void *mem, unsigned int width, unsigned int height,
- unsigned int stride)
- {
- struct fsd *fsd = fsd_alloc(width);
- unsigned int x;
- unsigned int y;
- /* Half resolution for PenTile RG-GB */
- width /= 2;
- height /= 2;
- for (y = 0; y < height * 6 / 9; ++y) {
- for (x = 0; x < width; ++x)
- write_color_2(fsd, mem, stride, x, smpte_top[x * 7 / width]);
- mem += 2 * stride;
- }
- for (; y < height * 7 / 9; ++y) {
- for (x = 0; x < width; ++x)
- write_color_2(fsd, mem, stride, x, smpte_middle[x * 7 / width]);
- mem += 2 * stride;
- }
- for (; y < height; ++y) {
- for (x = 0; x < width * 5 / 7; ++x)
- write_color_2(fsd, mem, stride, x,
- smpte_bottom[x * 4 / (width * 5 / 7)]);
- for (; x < width * 6 / 7; ++x)
- write_color_2(fsd, mem, stride, x,
- smpte_bottom[(x - width * 5 / 7) * 3 /
- (width / 7) + 4]);
- for (; x < width; ++x)
- write_color_2(fsd, mem, stride, x, smpte_bottom[7]);
- mem += 2 * stride;
- }
- free(fsd);
- }
- static void write_pixel_4(uint8_t *mem, unsigned int x, unsigned int pixel)
- {
- if (x & 1)
- mem[x / 2] = (mem[x / 2] & 0xf0) | (pixel & 0x0f);
- else
- mem[x / 2] = (mem[x / 2] & 0x0f) | (pixel << 4);
- }
- static void fill_smpte_c4(void *mem, unsigned int width, unsigned int height,
- unsigned int stride)
- {
- unsigned int x;
- unsigned int y;
- for (y = 0; y < height * 6 / 9; ++y) {
- for (x = 0; x < width; ++x)
- write_pixel_4(mem, x, smpte_top[x * 7 / width]);
- mem += stride;
- }
- for (; y < height * 7 / 9; ++y) {
- for (x = 0; x < width; ++x)
- write_pixel_4(mem, x, smpte_middle[x * 7 / width]);
- mem += stride;
- }
- for (; y < height; ++y) {
- for (x = 0; x < width * 5 / 7; ++x)
- write_pixel_4(mem, x,
- smpte_bottom[x * 4 / (width * 5 / 7)]);
- for (; x < width * 6 / 7; ++x)
- write_pixel_4(mem, x,
- smpte_bottom[(x - width * 5 / 7) * 3 /
- (width / 7) + 4]);
- for (; x < width; ++x)
- write_pixel_4(mem, x, smpte_bottom[7]);
- mem += stride;
- }
- }
- static void fill_smpte_c8(void *mem, unsigned int width, unsigned int height,
- unsigned int stride)
- {
- unsigned int x;
- unsigned int y;
- for (y = 0; y < height * 6 / 9; ++y) {
- for (x = 0; x < width; ++x)
- ((uint8_t *)mem)[x] = smpte_top[x * 7 / width];
- mem += stride;
- }
- for (; y < height * 7 / 9; ++y) {
- for (x = 0; x < width; ++x)
- ((uint8_t *)mem)[x] = smpte_middle[x * 7 / width];
- mem += stride;
- }
- for (; y < height; ++y) {
- for (x = 0; x < width * 5 / 7; ++x)
- ((uint8_t *)mem)[x] =
- smpte_bottom[x * 4 / (width * 5 / 7)];
- for (; x < width * 6 / 7; ++x)
- ((uint8_t *)mem)[x] =
- smpte_bottom[(x - width * 5 / 7) * 3
- / (width / 7) + 4];
- for (; x < width; ++x)
- ((uint8_t *)mem)[x] = smpte_bottom[7];
- mem += stride;
- }
- }
- void util_smpte_fill_lut(unsigned int ncolors, struct drm_color_lut *lut)
- {
- if (ncolors < ARRAY_SIZE(bw_color_lut)) {
- printf("Error: lut too small: %u < %zu\n", ncolors,
- ARRAY_SIZE(bw_color_lut));
- return;
- }
- memset(lut, 0, ncolors * sizeof(struct drm_color_lut));
- if (ncolors < ARRAY_SIZE(pentile_color_lut))
- memcpy(lut, bw_color_lut, sizeof(bw_color_lut));
- else if (ncolors < ARRAY_SIZE(smpte_color_lut))
- memcpy(lut, pentile_color_lut, sizeof(pentile_color_lut));
- else
- memcpy(lut, smpte_color_lut, sizeof(smpte_color_lut));
- }
- static void fill_smpte(const struct util_format_info *info, void *planes[3],
- unsigned int width, unsigned int height,
- unsigned int stride)
- {
- unsigned char *u, *v;
- switch (info->format) {
- case DRM_FORMAT_C1:
- return fill_smpte_c1(planes[0], width, height, stride);
- case DRM_FORMAT_C2:
- return fill_smpte_c2(planes[0], width, height, stride);
- case DRM_FORMAT_C4:
- return fill_smpte_c4(planes[0], width, height, stride);
- case DRM_FORMAT_C8:
- return fill_smpte_c8(planes[0], width, height, stride);
- case DRM_FORMAT_UYVY:
- case DRM_FORMAT_VYUY:
- case DRM_FORMAT_YUYV:
- case DRM_FORMAT_YVYU:
- return fill_smpte_yuv_packed(&info->yuv, planes[0], width,
- height, stride);
- case DRM_FORMAT_NV12:
- case DRM_FORMAT_NV21:
- case DRM_FORMAT_NV16:
- case DRM_FORMAT_NV61:
- case DRM_FORMAT_NV24:
- case DRM_FORMAT_NV42:
- u = info->yuv.order & YUV_YCbCr ? planes[1] : planes[1] + 1;
- v = info->yuv.order & YUV_YCrCb ? planes[1] : planes[1] + 1;
- return fill_smpte_yuv_planar(&info->yuv, planes[0], u, v,
- width, height, stride);
- case DRM_FORMAT_NV15:
- case DRM_FORMAT_NV20:
- case DRM_FORMAT_NV30:
- return fill_smpte_yuv_planar_10bpp(&info->yuv, planes[0],
- planes[1], width, height,
- stride);
- case DRM_FORMAT_YUV420:
- case DRM_FORMAT_YUV422:
- case DRM_FORMAT_YUV444:
- return fill_smpte_yuv_planar(&info->yuv, planes[0], planes[1],
- planes[2], width, height, stride);
- case DRM_FORMAT_YVU420:
- case DRM_FORMAT_YVU422:
- case DRM_FORMAT_YVU444:
- return fill_smpte_yuv_planar(&info->yuv, planes[0], planes[2],
- planes[1], width, height, stride);
- case DRM_FORMAT_ARGB4444:
- case DRM_FORMAT_XRGB4444:
- case DRM_FORMAT_ABGR4444:
- case DRM_FORMAT_XBGR4444:
- case DRM_FORMAT_RGBA4444:
- case DRM_FORMAT_RGBX4444:
- case DRM_FORMAT_BGRA4444:
- case DRM_FORMAT_BGRX4444:
- case DRM_FORMAT_RGB565:
- case DRM_FORMAT_RGB565 | DRM_FORMAT_BIG_ENDIAN:
- case DRM_FORMAT_BGR565:
- case DRM_FORMAT_ARGB1555:
- case DRM_FORMAT_XRGB1555:
- case DRM_FORMAT_XRGB1555 | DRM_FORMAT_BIG_ENDIAN:
- case DRM_FORMAT_ABGR1555:
- case DRM_FORMAT_XBGR1555:
- case DRM_FORMAT_RGBA5551:
- case DRM_FORMAT_RGBX5551:
- case DRM_FORMAT_BGRA5551:
- case DRM_FORMAT_BGRX5551:
- return fill_smpte_rgb16(&info->rgb, planes[0],
- width, height, stride,
- info->format & DRM_FORMAT_BIG_ENDIAN);
- case DRM_FORMAT_BGR888:
- case DRM_FORMAT_RGB888:
- return fill_smpte_rgb24(&info->rgb, planes[0],
- width, height, stride);
- case DRM_FORMAT_ARGB8888:
- case DRM_FORMAT_XRGB8888:
- case DRM_FORMAT_ABGR8888:
- case DRM_FORMAT_XBGR8888:
- case DRM_FORMAT_RGBA8888:
- case DRM_FORMAT_RGBX8888:
- case DRM_FORMAT_BGRA8888:
- case DRM_FORMAT_BGRX8888:
- case DRM_FORMAT_ARGB2101010:
- case DRM_FORMAT_XRGB2101010:
- case DRM_FORMAT_ABGR2101010:
- case DRM_FORMAT_XBGR2101010:
- case DRM_FORMAT_RGBA1010102:
- case DRM_FORMAT_RGBX1010102:
- case DRM_FORMAT_BGRA1010102:
- case DRM_FORMAT_BGRX1010102:
- return fill_smpte_rgb32(&info->rgb, planes[0],
- width, height, stride);
- case DRM_FORMAT_XRGB16161616F:
- case DRM_FORMAT_XBGR16161616F:
- case DRM_FORMAT_ARGB16161616F:
- case DRM_FORMAT_ABGR16161616F:
- return fill_smpte_rgb16fp(&info->rgb, planes[0],
- width, height, stride);
- }
- }
- #if HAVE_CAIRO
- static void byteswap_buffer16(void *mem, unsigned int width, unsigned int height,
- unsigned int stride)
- {
- unsigned int x, y;
- for (y = 0; y < height; ++y) {
- for (x = 0; x < width; ++x)
- ((uint16_t *)mem)[x] = swap16(((uint16_t *)mem)[x]);
- mem += stride;
- }
- }
- static void byteswap_buffer32(void *mem, unsigned int width, unsigned int height,
- unsigned int stride)
- {
- unsigned int x, y;
- for (y = 0; y < height; ++y) {
- for (x = 0; x < width; ++x)
- ((uint32_t *)mem)[x] = swap32(((uint32_t *)mem)[x]);
- mem += stride;
- }
- }
- #endif
- static void make_pwetty(void *data, unsigned int width, unsigned int height,
- unsigned int stride, uint32_t format)
- {
- #if HAVE_CAIRO
- cairo_surface_t *surface;
- cairo_t *cr;
- cairo_format_t cairo_format;
- bool swap16 = false;
- bool swap32 = false;
- /* we can ignore the order of R,G,B channels */
- switch (format) {
- case DRM_FORMAT_XRGB8888:
- case DRM_FORMAT_ARGB8888:
- case DRM_FORMAT_XBGR8888:
- case DRM_FORMAT_ABGR8888:
- cairo_format = CAIRO_FORMAT_ARGB32;
- break;
- case DRM_FORMAT_RGB565:
- case DRM_FORMAT_RGB565 | DRM_FORMAT_BIG_ENDIAN:
- case DRM_FORMAT_BGR565:
- cairo_format = CAIRO_FORMAT_RGB16_565;
- swap16 = fb_foreign_endian(format);
- break;
- #if CAIRO_VERSION_MAJOR > 1 || (CAIRO_VERSION_MAJOR == 1 && CAIRO_VERSION_MINOR >= 12)
- case DRM_FORMAT_ARGB2101010:
- case DRM_FORMAT_XRGB2101010:
- case DRM_FORMAT_ABGR2101010:
- case DRM_FORMAT_XBGR2101010:
- cairo_format = CAIRO_FORMAT_RGB30;
- swap32 = fb_foreign_endian(format);
- break;
- #endif
- default:
- return;
- }
- /* Cairo uses native byte order, so we may have to byteswap before... */
- if (swap16)
- byteswap_buffer16(data, width, height, stride);
- if (swap32)
- byteswap_buffer32(data, width, height, stride);
- surface = cairo_image_surface_create_for_data(data,
- cairo_format,
- width, height,
- stride);
- cr = cairo_create(surface);
- cairo_surface_destroy(surface);
- cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE);
- for (unsigned x = 0; x < width; x += 250)
- for (unsigned y = 0; y < height; y += 250) {
- char buf[64];
- cairo_move_to(cr, x, y - 20);
- cairo_line_to(cr, x, y + 20);
- cairo_move_to(cr, x - 20, y);
- cairo_line_to(cr, x + 20, y);
- cairo_new_sub_path(cr);
- cairo_arc(cr, x, y, 10, 0, M_PI * 2);
- cairo_set_line_width(cr, 4);
- cairo_set_source_rgb(cr, 0, 0, 0);
- cairo_stroke_preserve(cr);
- cairo_set_source_rgb(cr, 1, 1, 1);
- cairo_set_line_width(cr, 2);
- cairo_stroke(cr);
- snprintf(buf, sizeof buf, "%d, %d", x, y);
- cairo_move_to(cr, x + 20, y + 20);
- cairo_text_path(cr, buf);
- cairo_set_source_rgb(cr, 0, 0, 0);
- cairo_stroke_preserve(cr);
- cairo_set_source_rgb(cr, 1, 1, 1);
- cairo_fill(cr);
- }
- cairo_destroy(cr);
- /* ... and after */
- if (swap16)
- byteswap_buffer16(data, width, height, stride);
- if (swap32)
- byteswap_buffer32(data, width, height, stride);
- #endif
- }
- static struct color_yuv make_tiles_yuv_color(unsigned int x, unsigned int y,
- unsigned int width)
- {
- div_t d = div(x+y, width);
- uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
- + 0x000a1120 * (d.rem >> 6);
- struct color_yuv color =
- MAKE_YUV_601((rgb32 >> 16) & 0xff, (rgb32 >> 8) & 0xff,
- rgb32 & 0xff);
- return color;
- }
- static void fill_tiles_yuv_planar(const struct util_format_info *info,
- unsigned char *y_mem, unsigned char *u_mem,
- unsigned char *v_mem, unsigned int width,
- unsigned int height, unsigned int stride)
- {
- const struct util_yuv_info *yuv = &info->yuv;
- unsigned int cs = yuv->chroma_stride;
- unsigned int xsub = yuv->xsub;
- unsigned int ysub = yuv->ysub;
- unsigned int x;
- unsigned int y;
- for (y = 0; y < height; ++y) {
- for (x = 0; x < width; ++x) {
- struct color_yuv color =
- make_tiles_yuv_color(x, y, width);
- y_mem[x] = color.y;
- u_mem[x/xsub*cs] = color.u;
- v_mem[x/xsub*cs] = color.v;
- }
- y_mem += stride;
- if ((y + 1) % ysub == 0) {
- u_mem += stride * cs / xsub;
- v_mem += stride * cs / xsub;
- }
- }
- }
- static void fill_tiles_yuv_planar_10bpp(const struct util_format_info *info,
- unsigned char *y_mem,
- unsigned char *uv_mem,
- unsigned int width,
- unsigned int height,
- unsigned int stride)
- {
- const struct util_yuv_info *yuv = &info->yuv;
- unsigned int cs = yuv->chroma_stride;
- unsigned int xsub = yuv->xsub;
- unsigned int ysub = yuv->ysub;
- unsigned int xstep = cs * xsub;
- unsigned int x;
- unsigned int y;
- for (y = 0; y < height; ++y) {
- for (x = 0; x < width; x += 4) {
- struct color_yuv a = make_tiles_yuv_color(x+0, y, width);
- struct color_yuv b = make_tiles_yuv_color(x+1, y, width);
- struct color_yuv c = make_tiles_yuv_color(x+2, y, width);
- struct color_yuv d = make_tiles_yuv_color(x+3, y, width);
- write_pixels_10bpp(&y_mem[(x * 5) / 4],
- a.y << 2, b.y << 2, c.y << 2, d.y << 2);
- }
- y_mem += stride;
- }
- for (y = 0; y < height; y += ysub) {
- for (x = 0; x < width; x += xstep) {
- struct color_yuv a = make_tiles_yuv_color(x+0, y, width);
- struct color_yuv b = make_tiles_yuv_color(x+xsub, y, width);
- write_pixels_10bpp(&uv_mem[(x * 5) / xstep],
- a.u << 2, a.v << 2, b.u << 2, b.v << 2);
- }
- uv_mem += stride * cs / xsub;
- }
- }
- static void fill_tiles_yuv_packed(const struct util_format_info *info,
- void *mem, unsigned int width,
- unsigned int height, unsigned int stride)
- {
- const struct util_yuv_info *yuv = &info->yuv;
- unsigned char *y_mem = (yuv->order & YUV_YC) ? mem : mem + 1;
- unsigned char *c_mem = (yuv->order & YUV_CY) ? mem : mem + 1;
- unsigned int u = (yuv->order & YUV_YCrCb) ? 2 : 0;
- unsigned int v = (yuv->order & YUV_YCbCr) ? 2 : 0;
- unsigned int x;
- unsigned int y;
- for (y = 0; y < height; ++y) {
- for (x = 0; x < width; x += 2) {
- struct color_yuv color =
- make_tiles_yuv_color(x, y, width);
- y_mem[2*x] = color.y;
- c_mem[2*x+u] = color.u;
- y_mem[2*x+2] = color.y;
- c_mem[2*x+v] = color.v;
- }
- y_mem += stride;
- c_mem += stride;
- }
- }
- static void fill_tiles_rgb16(const struct util_format_info *info, void *mem,
- unsigned int width, unsigned int height,
- unsigned int stride, bool fb_be)
- {
- const struct util_rgb_info *rgb = &info->rgb;
- void *mem_base = mem;
- unsigned int x, y;
- for (y = 0; y < height; ++y) {
- for (x = 0; x < width; ++x) {
- div_t d = div(x+y, width);
- uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
- + 0x000a1120 * (d.rem >> 6);
- uint16_t color =
- MAKE_RGBA(rgb, (rgb32 >> 16) & 0xff,
- (rgb32 >> 8) & 0xff, rgb32 & 0xff,
- 255);
- ((uint16_t *)mem)[x] = cpu_to_fb16(color);
- }
- mem += stride;
- }
- make_pwetty(mem_base, width, height, stride, info->format);
- }
- static void fill_tiles_rgb24(const struct util_format_info *info, void *mem,
- unsigned int width, unsigned int height,
- unsigned int stride)
- {
- const struct util_rgb_info *rgb = &info->rgb;
- unsigned int x, y;
- for (y = 0; y < height; ++y) {
- for (x = 0; x < width; ++x) {
- div_t d = div(x+y, width);
- uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
- + 0x000a1120 * (d.rem >> 6);
- struct color_rgb24 color =
- MAKE_RGB24(rgb, (rgb32 >> 16) & 0xff,
- (rgb32 >> 8) & 0xff, rgb32 & 0xff);
- ((struct color_rgb24 *)mem)[x] = color;
- }
- mem += stride;
- }
- }
- static void fill_tiles_rgb32(const struct util_format_info *info, void *mem,
- unsigned int width, unsigned int height,
- unsigned int stride)
- {
- const struct util_rgb_info *rgb = &info->rgb;
- void *mem_base = mem;
- unsigned int x, y;
- for (y = 0; y < height; ++y) {
- for (x = 0; x < width; ++x) {
- div_t d = div(x+y, width);
- uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
- + 0x000a1120 * (d.rem >> 6);
- uint32_t alpha = ((y < height/2) && (x < width/2)) ? 127 : 255;
- uint32_t color =
- MAKE_RGBA(rgb, (rgb32 >> 16) & 0xff,
- (rgb32 >> 8) & 0xff, rgb32 & 0xff,
- alpha);
- ((uint32_t *)mem)[x] = cpu_to_le32(color);
- }
- mem += stride;
- }
- make_pwetty(mem_base, width, height, stride, info->format);
- }
- static void fill_tiles_rgb16fp(const struct util_format_info *info, void *mem,
- unsigned int width, unsigned int height,
- unsigned int stride)
- {
- const struct util_rgb_info *rgb = &info->rgb;
- unsigned int x, y;
- /* TODO: Give this actual fp16 precision */
- for (y = 0; y < height; ++y) {
- for (x = 0; x < width; ++x) {
- div_t d = div(x+y, width);
- uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
- + 0x000a1120 * (d.rem >> 6);
- uint32_t alpha = ((y < height/2) && (x < width/2)) ? 127 : 255;
- uint64_t color =
- MAKE_RGBA8FP16(rgb, (rgb32 >> 16) & 0xff,
- (rgb32 >> 8) & 0xff, rgb32 & 0xff,
- alpha);
- ((uint64_t *)mem)[x] = color;
- }
- mem += stride;
- }
- }
- static void fill_tiles(const struct util_format_info *info, void *planes[3],
- unsigned int width, unsigned int height,
- unsigned int stride)
- {
- unsigned char *u, *v;
- switch (info->format) {
- case DRM_FORMAT_UYVY:
- case DRM_FORMAT_VYUY:
- case DRM_FORMAT_YUYV:
- case DRM_FORMAT_YVYU:
- return fill_tiles_yuv_packed(info, planes[0],
- width, height, stride);
- case DRM_FORMAT_NV12:
- case DRM_FORMAT_NV21:
- case DRM_FORMAT_NV16:
- case DRM_FORMAT_NV61:
- case DRM_FORMAT_NV24:
- case DRM_FORMAT_NV42:
- u = info->yuv.order & YUV_YCbCr ? planes[1] : planes[1] + 1;
- v = info->yuv.order & YUV_YCrCb ? planes[1] : planes[1] + 1;
- return fill_tiles_yuv_planar(info, planes[0], u, v,
- width, height, stride);
- case DRM_FORMAT_NV15:
- case DRM_FORMAT_NV20:
- case DRM_FORMAT_NV30:
- return fill_tiles_yuv_planar_10bpp(info, planes[0], planes[1],
- width, height, stride);
- case DRM_FORMAT_YUV420:
- case DRM_FORMAT_YUV422:
- case DRM_FORMAT_YUV444:
- return fill_tiles_yuv_planar(info, planes[0], planes[1],
- planes[2], width, height, stride);
- case DRM_FORMAT_YVU420:
- case DRM_FORMAT_YVU422:
- case DRM_FORMAT_YVU444:
- return fill_tiles_yuv_planar(info, planes[0], planes[2],
- planes[1], width, height, stride);
- case DRM_FORMAT_ARGB4444:
- case DRM_FORMAT_XRGB4444:
- case DRM_FORMAT_ABGR4444:
- case DRM_FORMAT_XBGR4444:
- case DRM_FORMAT_RGBA4444:
- case DRM_FORMAT_RGBX4444:
- case DRM_FORMAT_BGRA4444:
- case DRM_FORMAT_BGRX4444:
- case DRM_FORMAT_RGB565:
- case DRM_FORMAT_RGB565 | DRM_FORMAT_BIG_ENDIAN:
- case DRM_FORMAT_BGR565:
- case DRM_FORMAT_ARGB1555:
- case DRM_FORMAT_XRGB1555:
- case DRM_FORMAT_XRGB1555 | DRM_FORMAT_BIG_ENDIAN:
- case DRM_FORMAT_ABGR1555:
- case DRM_FORMAT_XBGR1555:
- case DRM_FORMAT_RGBA5551:
- case DRM_FORMAT_RGBX5551:
- case DRM_FORMAT_BGRA5551:
- case DRM_FORMAT_BGRX5551:
- return fill_tiles_rgb16(info, planes[0],
- width, height, stride,
- info->format & DRM_FORMAT_BIG_ENDIAN);
- case DRM_FORMAT_BGR888:
- case DRM_FORMAT_RGB888:
- return fill_tiles_rgb24(info, planes[0],
- width, height, stride);
- case DRM_FORMAT_ARGB8888:
- case DRM_FORMAT_XRGB8888:
- case DRM_FORMAT_ABGR8888:
- case DRM_FORMAT_XBGR8888:
- case DRM_FORMAT_RGBA8888:
- case DRM_FORMAT_RGBX8888:
- case DRM_FORMAT_BGRA8888:
- case DRM_FORMAT_BGRX8888:
- case DRM_FORMAT_ARGB2101010:
- case DRM_FORMAT_XRGB2101010:
- case DRM_FORMAT_ABGR2101010:
- case DRM_FORMAT_XBGR2101010:
- case DRM_FORMAT_RGBA1010102:
- case DRM_FORMAT_RGBX1010102:
- case DRM_FORMAT_BGRA1010102:
- case DRM_FORMAT_BGRX1010102:
- return fill_tiles_rgb32(info, planes[0],
- width, height, stride);
- case DRM_FORMAT_XRGB16161616F:
- case DRM_FORMAT_XBGR16161616F:
- case DRM_FORMAT_ARGB16161616F:
- case DRM_FORMAT_ABGR16161616F:
- return fill_tiles_rgb16fp(info, planes[0],
- width, height, stride);
- }
- }
- static void fill_plain(const struct util_format_info *info, void *planes[3],
- unsigned int height,
- unsigned int stride)
- {
- switch (info->format) {
- case DRM_FORMAT_XRGB16161616F:
- case DRM_FORMAT_XBGR16161616F:
- case DRM_FORMAT_ARGB16161616F:
- case DRM_FORMAT_ABGR16161616F:
- /* 0x3838 = 0.5273 */
- memset(planes[0], 0x38, stride * height);
- break;
- default:
- memset(planes[0], 0x77, stride * height);
- break;
- }
- }
- static void fill_gradient_rgb32(const struct util_rgb_info *rgb,
- void *mem,
- unsigned int width, unsigned int height,
- unsigned int stride)
- {
- unsigned int i, j;
- for (i = 0; i < height / 2; i++) {
- uint32_t *row = mem;
- for (j = 0; j < width / 2; j++) {
- uint32_t value = MAKE_RGBA10(rgb, j & 0x3ff, j & 0x3ff, j & 0x3ff, 0);
- row[2*j] = row[2*j+1] = cpu_to_le32(value);
- }
- mem += stride;
- }
- for (; i < height; i++) {
- uint32_t *row = mem;
- for (j = 0; j < width / 2; j++) {
- uint32_t value = MAKE_RGBA10(rgb, j & 0x3fc, j & 0x3fc, j & 0x3fc, 0);
- row[2*j] = row[2*j+1] = cpu_to_le32(value);
- }
- mem += stride;
- }
- }
- static void fill_gradient_rgb16fp(const struct util_rgb_info *rgb,
- void *mem,
- unsigned int width, unsigned int height,
- unsigned int stride)
- {
- unsigned int i, j;
- for (i = 0; i < height / 2; i++) {
- uint64_t *row = mem;
- for (j = 0; j < width / 2; j++) {
- uint64_t value = MAKE_RGBA10FP16(rgb, j & 0x3ff, j & 0x3ff, j & 0x3ff, 0);
- row[2*j] = row[2*j+1] = value;
- }
- mem += stride;
- }
- for (; i < height; i++) {
- uint64_t *row = mem;
- for (j = 0; j < width / 2; j++) {
- uint64_t value = MAKE_RGBA10FP16(rgb, j & 0x3fc, j & 0x3fc, j & 0x3fc, 0);
- row[2*j] = row[2*j+1] = value;
- }
- mem += stride;
- }
- }
- /* The gradient pattern creates two horizontal gray gradients, split
- * into two halves. The top half has 10bpc precision, the bottom half
- * has 8bpc precision. When using with a 10bpc fb format, there are 3
- * possible outcomes:
- *
- * - Pixel data is encoded as 8bpc to the display, no dithering. This
- * would lead to the top and bottom halves looking identical.
- *
- * - Pixel data is encoded as 8bpc to the display, with dithering. This
- * would lead to there being a visible difference between the two halves,
- * but the top half would look a little speck-y due to the dithering.
- *
- * - Pixel data is encoded at 10bpc+ to the display (which implies
- * the display is able to show this level of depth). This should
- * lead to the top half being a very clean gradient, and visibly different
- * from the bottom half.
- *
- * Once we support additional fb formats, this approach could be extended
- * to distinguish even higher bpc precisions.
- *
- * Note that due to practical size considerations, for the screens
- * where this matters, the pattern actually emits stripes 2-pixels
- * wide for each gradient color. Otherwise the difference may be a bit
- * hard to notice.
- */
- static void fill_gradient(const struct util_format_info *info, void *planes[3],
- unsigned int width, unsigned int height,
- unsigned int stride)
- {
- switch (info->format) {
- case DRM_FORMAT_ARGB8888:
- case DRM_FORMAT_XRGB8888:
- case DRM_FORMAT_ABGR8888:
- case DRM_FORMAT_XBGR8888:
- case DRM_FORMAT_RGBA8888:
- case DRM_FORMAT_RGBX8888:
- case DRM_FORMAT_BGRA8888:
- case DRM_FORMAT_BGRX8888:
- case DRM_FORMAT_ARGB2101010:
- case DRM_FORMAT_XRGB2101010:
- case DRM_FORMAT_ABGR2101010:
- case DRM_FORMAT_XBGR2101010:
- case DRM_FORMAT_RGBA1010102:
- case DRM_FORMAT_RGBX1010102:
- case DRM_FORMAT_BGRA1010102:
- case DRM_FORMAT_BGRX1010102:
- return fill_gradient_rgb32(&info->rgb, planes[0],
- width, height, stride);
- case DRM_FORMAT_XRGB16161616F:
- case DRM_FORMAT_XBGR16161616F:
- case DRM_FORMAT_ARGB16161616F:
- case DRM_FORMAT_ABGR16161616F:
- return fill_gradient_rgb16fp(&info->rgb, planes[0],
- width, height, stride);
- }
- }
- static struct color_rgba get_black_white_value(uint64_t index)
- {
- const struct color_rgba colors[] = {
- { .red = 0, .green = 0, .blue = 0, .alpha = 255 }, /* black */
- { .red = 255, .green = 255, .blue = 255, .alpha = 255 }, /* white */
- };
- return colors[index & 0x1];
- }
- static struct color_rgba get_noise_color_value()
- {
- struct color_rgba color = {
- .red = rand(),
- .green = rand(),
- .blue = rand(),
- .alpha = 255
- };
- return color;
- }
- static struct color_rgba get_rgb_color(uint64_t index,
- enum util_fill_pattern pattern)
- {
- struct color_rgba color = {
- .red = 0,
- .green = 0,
- .blue = 0,
- .alpha = 0
- };
- switch (pattern) {
- case UTIL_PATTERN_NOISE:
- color = get_black_white_value(rand());
- case UTIL_PATTERN_NOISE_COLOR:
- color = get_noise_color_value();
- case UTIL_PATTERN_BLACK_WHITE:
- color = get_black_white_value(index);
- default:
- break;
- }
- return color;
- }
- static void insert_value_yuv_packed(const struct util_format_info *info,
- void *planes[3], unsigned int stride,
- unsigned int x, unsigned int y,
- const struct color_rgba* color)
- {
- struct color_yuv val = MAKE_YUV_601(color->red, color->green, color->blue);
- const struct util_yuv_info *yuv = &info->yuv;
- unsigned char *y_mem = (yuv->order & YUV_YC) ? planes[0] : planes[0] + 1;
- unsigned char *c_mem = (yuv->order & YUV_CY) ? planes[0] : planes[0] + 1;
- unsigned int u = (yuv->order & YUV_YCrCb) ? 2 : 0;
- unsigned int v = (yuv->order & YUV_YCbCr) ? 2 : 0;
- if (x & 0x1)
- return;
- y_mem += stride * y;
- c_mem += stride * y;
- y_mem[2*x] = val.y;
- c_mem[2*x+u] = val.u;
- y_mem[2*x+2] = val.y;
- c_mem[2*x+v] = val.v;
- }
- static void insert_value_yuv_planar(const struct util_format_info *info,
- void *planes[3], unsigned int stride,
- unsigned int x, unsigned int y,
- const struct color_rgba* color)
- {
- struct color_yuv val = MAKE_YUV_601(color->red, color->green, color->blue);
- const struct util_yuv_info *yuv = &info->yuv;
- unsigned int cs = yuv->chroma_stride;
- unsigned int xsub = yuv->xsub;
- unsigned int ysub = yuv->ysub;
- unsigned int chroma_offset = (y + 1) / ysub;
- unsigned char *y_mem = planes[0] + (y * stride);
- unsigned char *u_mem = planes[1];
- unsigned char *v_mem = planes[2];
- switch (info->format) {
- case DRM_FORMAT_NV42:
- u_mem = info->yuv.order & YUV_YCbCr ? planes[1] : planes[1] + 1;
- v_mem = info->yuv.order & YUV_YCrCb ? planes[1] : planes[1] + 1;
- break;
- case DRM_FORMAT_YVU420:
- u_mem = planes[2];
- v_mem = planes[1];
- break;
- case DRM_FORMAT_YUV420:
- default:
- break;
- }
- u_mem += (chroma_offset * (stride * cs / xsub));
- v_mem += (chroma_offset * (stride * cs / xsub));
- y_mem[x] = val.y;
- u_mem[x/xsub*cs] = val.u;
- v_mem[x/xsub*cs] = val.v;
- }
- static inline bool is_power_of_two(unsigned long val)
- {
- return (val != 0) && ((val & (val - 1)) == 0);
- }
- static bool check_yuv(const struct util_yuv_info *info)
- {
- if (__builtin_expect(
- is_power_of_two(info->xsub) &&
- is_power_of_two(info->ysub) &&
- is_power_of_two(info->chroma_stride), 1)) {
- return true;
- }
- return false;
- }
- static void insert_value_yuv_planar_10bpp(const struct util_format_info *info,
- void *planes[3], unsigned int stride,
- unsigned int x, unsigned int y,
- const struct color_rgba* color)
- {
- struct color_yuv val = MAKE_YUV_601(color->red, color->green, color->blue);
- const struct util_yuv_info *yuv = &info->yuv;
- unsigned int cs = yuv->chroma_stride;
- unsigned int xsub = yuv->xsub;
- unsigned int ysub = yuv->ysub;
- unsigned int xstep = cs * xsub;
- unsigned int ysub_mask = ysub - 1;
- unsigned int xsub_mask = xsub - 1;
- unsigned int xstep_mask = xstep - 1;
- unsigned char *y_mem = planes[0] + (y * stride);
- unsigned char *uv_mem = planes[1] + (y * (stride * cs / xsub));
- unsigned int block_start = ((x & 0x3) * 5) / 4;
- unsigned int bit_start = (x & 0x3) * 10;
- /* Plus two because val.y is only 8 bits */
- update_pixels_10bpp(&y_mem[block_start], val.y << (bit_start + 2),
- 0x3ff << bit_start);
- /* This logic only works when xsub, ysub and chroma stride is power of two. */
- assert(check_yuv(&info->yuv));
- if (y & ysub_mask)
- return;
- if (x & xsub_mask)
- return;
- block_start = ((x & ~xstep_mask) * 5) / xstep;
- bit_start = x & xstep_mask ? 0 : 20;
- update_pixels_10bpp(&uv_mem[block_start],
- ((val.u << 2) | (val.v << 12)) << bit_start,
- 0xfffff << bit_start);
- }
- static void insert_value_rgb32(const struct util_format_info *info,
- void *planes[3], unsigned int stride,
- unsigned int x, unsigned int y,
- const struct color_rgba* color)
- {
- uint32_t *row = planes[0] + (stride * y);
- uint32_t val = MAKE_RGBA10(&info->rgb, color->red, color->green,
- color->blue, color->alpha);
- row[x] = val;
- }
- static void insert_value_rgb16fp(const struct util_format_info *info,
- void *planes[3], unsigned int stride,
- unsigned int x, unsigned int y,
- const struct color_rgba* color)
- {
- uint64_t *row = planes[0] + (stride * y);
- uint64_t val = MAKE_RGBA10FP16(&info->rgb, color->red, color->green,
- color->blue, color->alpha);
- row[x] = val;
- }
- static void fill_simple_patterns(const struct util_format_info *info,
- void *planes[3], unsigned int width,
- unsigned int height, unsigned int stride,
- enum util_fill_pattern pattern)
- {
- void (*func_insert_value)(const struct util_format_info *info,
- void *planes[3], unsigned int stride,
- unsigned int x, unsigned int y,
- const struct color_rgba* color);
- int x, y;
- switch (info->format) {
- case DRM_FORMAT_UYVY:
- case DRM_FORMAT_VYUY:
- case DRM_FORMAT_YUYV:
- case DRM_FORMAT_YVYU:
- func_insert_value = &insert_value_yuv_packed;
- break;
- case DRM_FORMAT_NV12:
- case DRM_FORMAT_NV21:
- case DRM_FORMAT_NV16:
- case DRM_FORMAT_NV61:
- case DRM_FORMAT_NV24:
- case DRM_FORMAT_NV42:
- case DRM_FORMAT_YUV420:
- case DRM_FORMAT_YVU420:
- func_insert_value = &insert_value_yuv_planar;
- break;
- case DRM_FORMAT_NV15:
- case DRM_FORMAT_NV20:
- case DRM_FORMAT_NV30:
- func_insert_value = &insert_value_yuv_planar_10bpp;
- break;
- case DRM_FORMAT_ARGB8888:
- case DRM_FORMAT_XRGB8888:
- case DRM_FORMAT_ABGR8888:
- case DRM_FORMAT_XBGR8888:
- case DRM_FORMAT_RGBA8888:
- case DRM_FORMAT_RGBX8888:
- case DRM_FORMAT_BGRA8888:
- case DRM_FORMAT_BGRX8888:
- case DRM_FORMAT_ARGB2101010:
- case DRM_FORMAT_XRGB2101010:
- case DRM_FORMAT_ABGR2101010:
- case DRM_FORMAT_XBGR2101010:
- case DRM_FORMAT_RGBA1010102:
- case DRM_FORMAT_RGBX1010102:
- case DRM_FORMAT_BGRA1010102:
- case DRM_FORMAT_BGRX1010102:
- func_insert_value = &insert_value_rgb32;
- break;
- case DRM_FORMAT_XRGB16161616F:
- case DRM_FORMAT_XBGR16161616F:
- case DRM_FORMAT_ARGB16161616F:
- case DRM_FORMAT_ABGR16161616F:
- func_insert_value = &insert_value_rgb16fp;
- break;
- default:
- return;
- }
- for (y = 0; y < height; y++) {
- for (x = 0; x < width; x++) {
- struct color_rgba color = get_rgb_color((width * y) + x, pattern);
- (*func_insert_value)(info, planes, stride, x, y, &color);
- }
- }
- }
- /*
- * util_fill_pattern - Fill a buffer with a test pattern
- * @format: Pixel format
- * @pattern: Test pattern
- * @planes: Array of buffers
- * @width: Width in pixels
- * @height: Height in pixels
- * @stride: Line stride (pitch) in bytes
- * @seed: Seed for noise patterns if zero a default time based seed will be used
- *
- * Fill the buffers with the test pattern specified by the pattern parameter.
- * Supported formats vary depending on the selected pattern.
- */
- void util_fill_pattern(uint32_t format, enum util_fill_pattern pattern,
- void *planes[3], unsigned int width,
- unsigned int height, unsigned int stride, unsigned long seed)
- {
- const struct util_format_info *info;
- info = util_format_info_find(format);
- if (info == NULL)
- return;
- switch (pattern) {
- case UTIL_PATTERN_NOISE:
- case UTIL_PATTERN_NOISE_COLOR:
- if (!seed)
- seed = time(NULL);
- srand(seed);
- printf("Seed used for noise patterns %lu\n", seed);
- break;
- default:
- break;
- }
- switch (pattern) {
- case UTIL_PATTERN_TILES:
- return fill_tiles(info, planes, width, height, stride);
- case UTIL_PATTERN_SMPTE:
- return fill_smpte(info, planes, width, height, stride);
- case UTIL_PATTERN_PLAIN:
- return fill_plain(info, planes, height, stride);
- case UTIL_PATTERN_GRADIENT:
- return fill_gradient(info, planes, width, height, stride);
- case UTIL_PATTERN_NOISE:
- case UTIL_PATTERN_NOISE_COLOR:
- case UTIL_PATTERN_BLACK_WHITE:
- return fill_simple_patterns(info, planes, width, height, stride,
- pattern);
- default:
- printf("Error: unsupported test pattern %u.\n", pattern);
- break;
- }
- }
- static const char *pattern_names[] = {
- [UTIL_PATTERN_TILES] = "tiles",
- [UTIL_PATTERN_SMPTE] = "smpte",
- [UTIL_PATTERN_PLAIN] = "plain",
- [UTIL_PATTERN_GRADIENT] = "gradient",
- [UTIL_PATTERN_NOISE] = "noise",
- [UTIL_PATTERN_NOISE_COLOR] = "noise-color",
- [UTIL_PATTERN_BLACK_WHITE] = "black-white",
- };
- enum util_fill_pattern util_pattern_enum(const char *name)
- {
- unsigned int i;
- for (i = 0; i < ARRAY_SIZE(pattern_names); i++)
- if (!strcmp(pattern_names[i], name))
- return (enum util_fill_pattern)i;
- printf("Error: unsupported test pattern %s.\n", name);
- return UTIL_PATTERN_SMPTE;
- }
|