xref: /openbmc/qemu/ui/qemu-pixman.c (revision 1f2146f7)
1 /*
2  * This work is licensed under the terms of the GNU GPL, version 2 or later.
3  * See the COPYING file in the top-level directory.
4  */
5 
6 #include "qemu/osdep.h"
7 #include "ui/console.h"
8 #include "standard-headers/drm/drm_fourcc.h"
9 #include "trace.h"
10 
11 PixelFormat qemu_pixelformat_from_pixman(pixman_format_code_t format)
12 {
13     PixelFormat pf;
14     uint8_t bpp;
15 
16     bpp = pf.bits_per_pixel = PIXMAN_FORMAT_BPP(format);
17     pf.bytes_per_pixel = PIXMAN_FORMAT_BPP(format) / 8;
18     pf.depth = PIXMAN_FORMAT_DEPTH(format);
19 
20     pf.abits = PIXMAN_FORMAT_A(format);
21     pf.rbits = PIXMAN_FORMAT_R(format);
22     pf.gbits = PIXMAN_FORMAT_G(format);
23     pf.bbits = PIXMAN_FORMAT_B(format);
24 
25     switch (PIXMAN_FORMAT_TYPE(format)) {
26     case PIXMAN_TYPE_ARGB:
27         pf.ashift = pf.bbits + pf.gbits + pf.rbits;
28         pf.rshift = pf.bbits + pf.gbits;
29         pf.gshift = pf.bbits;
30         pf.bshift = 0;
31         break;
32     case PIXMAN_TYPE_ABGR:
33         pf.ashift = pf.rbits + pf.gbits + pf.bbits;
34         pf.bshift = pf.rbits + pf.gbits;
35         pf.gshift = pf.rbits;
36         pf.rshift = 0;
37         break;
38     case PIXMAN_TYPE_BGRA:
39         pf.bshift = bpp - pf.bbits;
40         pf.gshift = bpp - (pf.bbits + pf.gbits);
41         pf.rshift = bpp - (pf.bbits + pf.gbits + pf.rbits);
42         pf.ashift = 0;
43         break;
44     case PIXMAN_TYPE_RGBA:
45         pf.rshift = bpp - pf.rbits;
46         pf.gshift = bpp - (pf.rbits + pf.gbits);
47         pf.bshift = bpp - (pf.rbits + pf.gbits + pf.bbits);
48         pf.ashift = 0;
49         break;
50     default:
51         g_assert_not_reached();
52         break;
53     }
54 
55     pf.amax = (1 << pf.abits) - 1;
56     pf.rmax = (1 << pf.rbits) - 1;
57     pf.gmax = (1 << pf.gbits) - 1;
58     pf.bmax = (1 << pf.bbits) - 1;
59     pf.amask = pf.amax << pf.ashift;
60     pf.rmask = pf.rmax << pf.rshift;
61     pf.gmask = pf.gmax << pf.gshift;
62     pf.bmask = pf.bmax << pf.bshift;
63 
64     return pf;
65 }
66 
67 pixman_format_code_t qemu_default_pixman_format(int bpp, bool native_endian)
68 {
69     if (native_endian) {
70         switch (bpp) {
71         case 15:
72             return PIXMAN_x1r5g5b5;
73         case 16:
74             return PIXMAN_r5g6b5;
75         case 24:
76             return PIXMAN_r8g8b8;
77         case 32:
78             return PIXMAN_x8r8g8b8;
79         }
80     } else {
81         switch (bpp) {
82         case 24:
83             return PIXMAN_b8g8r8;
84         case 32:
85             return PIXMAN_b8g8r8x8;
86         break;
87         }
88     }
89     return 0;
90 }
91 
92 /* Note: drm is little endian, pixman is native endian */
93 static const struct {
94     uint32_t drm_format;
95     pixman_format_code_t pixman_format;
96 } drm_format_pixman_map[] = {
97     { DRM_FORMAT_RGB888,   PIXMAN_LE_r8g8b8   },
98     { DRM_FORMAT_ARGB8888, PIXMAN_LE_a8r8g8b8 },
99     { DRM_FORMAT_XRGB8888, PIXMAN_LE_x8r8g8b8 }
100 };
101 
102 pixman_format_code_t qemu_drm_format_to_pixman(uint32_t drm_format)
103 {
104     int i;
105 
106     for (i = 0; i < ARRAY_SIZE(drm_format_pixman_map); i++) {
107         if (drm_format == drm_format_pixman_map[i].drm_format) {
108             return drm_format_pixman_map[i].pixman_format;
109         }
110     }
111     return 0;
112 }
113 
114 uint32_t qemu_pixman_to_drm_format(pixman_format_code_t pixman_format)
115 {
116     int i;
117 
118     for (i = 0; i < ARRAY_SIZE(drm_format_pixman_map); i++) {
119         if (pixman_format == drm_format_pixman_map[i].pixman_format) {
120             return drm_format_pixman_map[i].drm_format;
121         }
122     }
123     return 0;
124 }
125 
126 int qemu_pixman_get_type(int rshift, int gshift, int bshift)
127 {
128     int type = PIXMAN_TYPE_OTHER;
129 
130     if (rshift > gshift && gshift > bshift) {
131         if (bshift == 0) {
132             type = PIXMAN_TYPE_ARGB;
133         } else {
134             type = PIXMAN_TYPE_RGBA;
135         }
136     } else if (rshift < gshift && gshift < bshift) {
137         if (rshift == 0) {
138             type = PIXMAN_TYPE_ABGR;
139         } else {
140             type = PIXMAN_TYPE_BGRA;
141         }
142     }
143     return type;
144 }
145 
146 pixman_format_code_t qemu_pixman_get_format(PixelFormat *pf)
147 {
148     pixman_format_code_t format;
149     int type;
150 
151     type = qemu_pixman_get_type(pf->rshift, pf->gshift, pf->bshift);
152     format = PIXMAN_FORMAT(pf->bits_per_pixel, type,
153                            pf->abits, pf->rbits, pf->gbits, pf->bbits);
154     if (!pixman_format_supported_source(format)) {
155         return 0;
156     }
157     return format;
158 }
159 
160 /*
161  * Return true for known-good pixman conversions.
162  *
163  * UIs using pixman for format conversion can hook this into
164  * DisplayChangeListenerOps->dpy_gfx_check_format
165  */
166 bool qemu_pixman_check_format(DisplayChangeListener *dcl,
167                               pixman_format_code_t format)
168 {
169     switch (format) {
170     /* 32 bpp */
171     case PIXMAN_x8r8g8b8:
172     case PIXMAN_a8r8g8b8:
173     case PIXMAN_b8g8r8x8:
174     case PIXMAN_b8g8r8a8:
175     /* 24 bpp */
176     case PIXMAN_r8g8b8:
177     case PIXMAN_b8g8r8:
178     /* 16 bpp */
179     case PIXMAN_x1r5g5b5:
180     case PIXMAN_r5g6b5:
181         return true;
182     default:
183         return false;
184     }
185 }
186 
187 pixman_image_t *qemu_pixman_linebuf_create(pixman_format_code_t format,
188                                            int width)
189 {
190     pixman_image_t *image = pixman_image_create_bits(format, width, 1, NULL, 0);
191     assert(image != NULL);
192     return image;
193 }
194 
195 /* fill linebuf from framebuffer */
196 void qemu_pixman_linebuf_fill(pixman_image_t *linebuf, pixman_image_t *fb,
197                               int width, int x, int y)
198 {
199     pixman_image_composite(PIXMAN_OP_SRC, fb, NULL, linebuf,
200                            x, y, 0, 0, 0, 0, width, 1);
201 }
202 
203 /* copy linebuf to framebuffer */
204 void qemu_pixman_linebuf_copy(pixman_image_t *fb, int width, int x, int y,
205                               pixman_image_t *linebuf)
206 {
207     pixman_image_composite(PIXMAN_OP_SRC, linebuf, NULL, fb,
208                            0, 0, 0, 0, x, y, width, 1);
209 }
210 
211 pixman_image_t *qemu_pixman_mirror_create(pixman_format_code_t format,
212                                           pixman_image_t *image)
213 {
214     return pixman_image_create_bits(format,
215                                     pixman_image_get_width(image),
216                                     pixman_image_get_height(image),
217                                     NULL,
218                                     pixman_image_get_stride(image));
219 }
220 
221 void qemu_pixman_image_unref(pixman_image_t *image)
222 {
223     if (image == NULL) {
224         return;
225     }
226     pixman_image_unref(image);
227 }
228 
229 pixman_color_t qemu_pixman_color(PixelFormat *pf, uint32_t color)
230 {
231     pixman_color_t c;
232 
233     c.red   = ((color & pf->rmask) >> pf->rshift) << (16 - pf->rbits);
234     c.green = ((color & pf->gmask) >> pf->gshift) << (16 - pf->gbits);
235     c.blue  = ((color & pf->bmask) >> pf->bshift) << (16 - pf->bbits);
236     c.alpha = ((color & pf->amask) >> pf->ashift) << (16 - pf->abits);
237     return c;
238 }
239 
240 pixman_image_t *qemu_pixman_glyph_from_vgafont(int height, const uint8_t *font,
241                                                unsigned int ch)
242 {
243     pixman_image_t *glyph;
244     uint8_t *data;
245     bool bit;
246     int x, y;
247 
248     glyph = pixman_image_create_bits(PIXMAN_a8, 8, height,
249                                      NULL, 0);
250     data = (uint8_t *)pixman_image_get_data(glyph);
251 
252     font += height * ch;
253     for (y = 0; y < height; y++, font++) {
254         for (x = 0; x < 8; x++, data++) {
255             bit = (*font) & (1 << (7-x));
256             *data = bit ? 0xff : 0x00;
257         }
258     }
259     return glyph;
260 }
261 
262 void qemu_pixman_glyph_render(pixman_image_t *glyph,
263                               pixman_image_t *surface,
264                               pixman_color_t *fgcol,
265                               pixman_color_t *bgcol,
266                               int x, int y, int cw, int ch)
267 {
268     pixman_image_t *ifg = pixman_image_create_solid_fill(fgcol);
269     pixman_image_t *ibg = pixman_image_create_solid_fill(bgcol);
270 
271     pixman_image_composite(PIXMAN_OP_SRC, ibg, NULL, surface,
272                            0, 0, 0, 0,
273                            cw * x, ch * y,
274                            cw, ch);
275     pixman_image_composite(PIXMAN_OP_OVER, ifg, glyph, surface,
276                            0, 0, 0, 0,
277                            cw * x, ch * y,
278                            cw, ch);
279     pixman_image_unref(ifg);
280     pixman_image_unref(ibg);
281 }
282