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 "qemu/memfd.h"
10 #include "standard-headers/drm/drm_fourcc.h"
11 #include "trace.h"
12
qemu_pixelformat_from_pixman(pixman_format_code_t format)13 PixelFormat qemu_pixelformat_from_pixman(pixman_format_code_t format)
14 {
15 PixelFormat pf;
16 uint8_t bpp;
17
18 bpp = pf.bits_per_pixel = PIXMAN_FORMAT_BPP(format);
19 pf.bytes_per_pixel = PIXMAN_FORMAT_BPP(format) / 8;
20 pf.depth = PIXMAN_FORMAT_DEPTH(format);
21
22 pf.abits = PIXMAN_FORMAT_A(format);
23 pf.rbits = PIXMAN_FORMAT_R(format);
24 pf.gbits = PIXMAN_FORMAT_G(format);
25 pf.bbits = PIXMAN_FORMAT_B(format);
26
27 switch (PIXMAN_FORMAT_TYPE(format)) {
28 case PIXMAN_TYPE_ARGB:
29 pf.ashift = pf.bbits + pf.gbits + pf.rbits;
30 pf.rshift = pf.bbits + pf.gbits;
31 pf.gshift = pf.bbits;
32 pf.bshift = 0;
33 break;
34 case PIXMAN_TYPE_ABGR:
35 pf.ashift = pf.rbits + pf.gbits + pf.bbits;
36 pf.bshift = pf.rbits + pf.gbits;
37 pf.gshift = pf.rbits;
38 pf.rshift = 0;
39 break;
40 case PIXMAN_TYPE_BGRA:
41 pf.bshift = bpp - pf.bbits;
42 pf.gshift = bpp - (pf.bbits + pf.gbits);
43 pf.rshift = bpp - (pf.bbits + pf.gbits + pf.rbits);
44 pf.ashift = 0;
45 break;
46 case PIXMAN_TYPE_RGBA:
47 pf.rshift = bpp - pf.rbits;
48 pf.gshift = bpp - (pf.rbits + pf.gbits);
49 pf.bshift = bpp - (pf.rbits + pf.gbits + pf.bbits);
50 pf.ashift = 0;
51 break;
52 default:
53 g_assert_not_reached();
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
qemu_default_pixman_format(int bpp,bool native_endian)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
qemu_drm_format_to_pixman(uint32_t drm_format)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
qemu_pixman_to_drm_format(pixman_format_code_t pixman_format)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
qemu_pixman_get_type(int rshift,int gshift,int bshift,int endian)129 int qemu_pixman_get_type(int rshift, int gshift, int bshift, int endian)
130 {
131 int type = PIXMAN_TYPE_OTHER;
132 bool native_endian = (endian == G_BYTE_ORDER);
133
134 if (rshift > gshift && gshift > bshift) {
135 if (bshift == 0) {
136 type = native_endian ? PIXMAN_TYPE_ARGB : PIXMAN_TYPE_BGRA;
137 } else {
138 type = native_endian ? PIXMAN_TYPE_RGBA : PIXMAN_TYPE_ABGR;
139 }
140 } else if (rshift < gshift && gshift < bshift) {
141 if (rshift == 0) {
142 type = native_endian ? PIXMAN_TYPE_ABGR : PIXMAN_TYPE_RGBA;
143 } else {
144 type = native_endian ? PIXMAN_TYPE_BGRA : PIXMAN_TYPE_ARGB;
145 }
146 }
147 return type;
148 }
149
150 #ifdef CONFIG_PIXMAN
qemu_pixman_get_format(PixelFormat * pf,int endian)151 pixman_format_code_t qemu_pixman_get_format(PixelFormat *pf, int endian)
152 {
153 pixman_format_code_t format;
154 int type;
155
156 type = qemu_pixman_get_type(pf->rshift, pf->gshift, pf->bshift, endian);
157 format = PIXMAN_FORMAT(pf->bits_per_pixel, type,
158 pf->abits, pf->rbits, pf->gbits, pf->bbits);
159 if (!pixman_format_supported_source(format)) {
160 return 0;
161 }
162 return format;
163 }
164 #endif
165
166 /*
167 * Return true for known-good pixman conversions.
168 *
169 * UIs using pixman for format conversion can hook this into
170 * DisplayChangeListenerOps->dpy_gfx_check_format
171 */
qemu_pixman_check_format(DisplayChangeListener * dcl,pixman_format_code_t format)172 bool qemu_pixman_check_format(DisplayChangeListener *dcl,
173 pixman_format_code_t format)
174 {
175 switch (format) {
176 /* 32 bpp */
177 case PIXMAN_x8r8g8b8:
178 case PIXMAN_a8r8g8b8:
179 case PIXMAN_b8g8r8x8:
180 case PIXMAN_b8g8r8a8:
181 /* 24 bpp */
182 case PIXMAN_r8g8b8:
183 case PIXMAN_b8g8r8:
184 /* 16 bpp */
185 case PIXMAN_x1r5g5b5:
186 case PIXMAN_r5g6b5:
187 return true;
188 default:
189 return false;
190 }
191 }
192
193 #ifdef CONFIG_PIXMAN
qemu_pixman_linebuf_create(pixman_format_code_t format,int width)194 pixman_image_t *qemu_pixman_linebuf_create(pixman_format_code_t format,
195 int width)
196 {
197 pixman_image_t *image = pixman_image_create_bits(format, width, 1, NULL, 0);
198 assert(image != NULL);
199 return image;
200 }
201
202 /* fill linebuf from framebuffer */
qemu_pixman_linebuf_fill(pixman_image_t * linebuf,pixman_image_t * fb,int width,int x,int y)203 void qemu_pixman_linebuf_fill(pixman_image_t *linebuf, pixman_image_t *fb,
204 int width, int x, int y)
205 {
206 pixman_image_composite(PIXMAN_OP_SRC, fb, NULL, linebuf,
207 x, y, 0, 0, 0, 0, width, 1);
208 }
209
qemu_pixman_mirror_create(pixman_format_code_t format,pixman_image_t * image)210 pixman_image_t *qemu_pixman_mirror_create(pixman_format_code_t format,
211 pixman_image_t *image)
212 {
213 return pixman_image_create_bits(format,
214 pixman_image_get_width(image),
215 pixman_image_get_height(image),
216 NULL,
217 pixman_image_get_stride(image));
218 }
219 #endif
220
qemu_pixman_image_unref(pixman_image_t * image)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 #ifdef CONFIG_PIXMAN
qemu_pixman_glyph_from_vgafont(int height,const uint8_t * font,unsigned int ch)230 pixman_image_t *qemu_pixman_glyph_from_vgafont(int height, const uint8_t *font,
231 unsigned int ch)
232 {
233 pixman_image_t *glyph;
234 uint8_t *data;
235 bool bit;
236 int x, y;
237
238 glyph = pixman_image_create_bits(PIXMAN_a8, 8, height,
239 NULL, 0);
240 data = (uint8_t *)pixman_image_get_data(glyph);
241
242 font += height * ch;
243 for (y = 0; y < height; y++, font++) {
244 for (x = 0; x < 8; x++, data++) {
245 bit = (*font) & (1 << (7-x));
246 *data = bit ? 0xff : 0x00;
247 }
248 }
249 return glyph;
250 }
251
qemu_pixman_glyph_render(pixman_image_t * glyph,pixman_image_t * surface,pixman_color_t * fgcol,pixman_color_t * bgcol,int x,int y,int cw,int ch)252 void qemu_pixman_glyph_render(pixman_image_t *glyph,
253 pixman_image_t *surface,
254 pixman_color_t *fgcol,
255 pixman_color_t *bgcol,
256 int x, int y, int cw, int ch)
257 {
258 pixman_image_t *ifg = pixman_image_create_solid_fill(fgcol);
259 pixman_image_t *ibg = pixman_image_create_solid_fill(bgcol);
260
261 pixman_image_composite(PIXMAN_OP_SRC, ibg, NULL, surface,
262 0, 0, 0, 0,
263 cw * x, ch * y,
264 cw, ch);
265 pixman_image_composite(PIXMAN_OP_OVER, ifg, glyph, surface,
266 0, 0, 0, 0,
267 cw * x, ch * y,
268 cw, ch);
269 pixman_image_unref(ifg);
270 pixman_image_unref(ibg);
271 }
272 #endif /* CONFIG_PIXMAN */
273
274 static void *
qemu_pixman_shareable_alloc(const char * name,size_t size,qemu_pixman_shareable * handle,Error ** errp)275 qemu_pixman_shareable_alloc(const char *name, size_t size,
276 qemu_pixman_shareable *handle,
277 Error **errp)
278 {
279 #ifdef WIN32
280 return qemu_win32_map_alloc(size, handle, errp);
281 #else
282 return qemu_memfd_alloc(name, size, 0, handle, errp);
283 #endif
284 }
285
286 static void
qemu_pixman_shareable_free(qemu_pixman_shareable handle,void * ptr,size_t size)287 qemu_pixman_shareable_free(qemu_pixman_shareable handle,
288 void *ptr, size_t size)
289 {
290 #ifdef WIN32
291 qemu_win32_map_free(ptr, handle, &error_warn);
292 #else
293 qemu_memfd_free(ptr, size, handle);
294 #endif
295 }
296
297 static void
qemu_pixman_shared_image_destroy(pixman_image_t * image,void * data)298 qemu_pixman_shared_image_destroy(pixman_image_t *image, void *data)
299 {
300 qemu_pixman_shareable handle = PTR_TO_SHAREABLE(data);
301 void *ptr = pixman_image_get_data(image);
302 size_t size = pixman_image_get_height(image) * pixman_image_get_stride(image);
303
304 qemu_pixman_shareable_free(handle, ptr, size);
305 }
306
307 bool
qemu_pixman_image_new_shareable(pixman_image_t ** image,qemu_pixman_shareable * handle,const char * name,pixman_format_code_t format,int width,int height,int rowstride_bytes,Error ** errp)308 qemu_pixman_image_new_shareable(pixman_image_t **image,
309 qemu_pixman_shareable *handle,
310 const char *name,
311 pixman_format_code_t format,
312 int width,
313 int height,
314 int rowstride_bytes,
315 Error **errp)
316 {
317 ERRP_GUARD();
318 size_t size = height * rowstride_bytes;
319 void *bits = NULL;
320
321 g_return_val_if_fail(image != NULL, false);
322 g_return_val_if_fail(handle != NULL, false);
323
324 bits = qemu_pixman_shareable_alloc(name, size, handle, errp);
325 if (!bits) {
326 return false;
327 }
328
329 *image = pixman_image_create_bits(format, width, height, bits, rowstride_bytes);
330 if (!*image) {
331 error_setg(errp, "Failed to allocate image");
332 qemu_pixman_shareable_free(*handle, bits, size);
333 return false;
334 }
335
336 pixman_image_set_destroy_function(*image,
337 qemu_pixman_shared_image_destroy,
338 SHAREABLE_TO_PTR(*handle));
339
340 return true;
341 }
342