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