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.
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"
13 PixelFormat
qemu_pixelformat_from_pixman(pixman_format_code_t format
)
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
);
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
);
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
;
34 case PIXMAN_TYPE_ABGR
:
35 pf
.ashift
= pf
.rbits
+ pf
.gbits
+ pf
.bbits
;
36 pf
.bshift
= pf
.rbits
+ pf
.gbits
;
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
);
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
);
53 g_assert_not_reached();
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
;
68 pixman_format_code_t
qemu_default_pixman_format(int bpp
, bool native_endian
)
73 return PIXMAN_x1r5g5b5
;
79 return PIXMAN_x8r8g8b8
;
86 return PIXMAN_b8g8r8x8
;
93 /* Note: drm is little endian, pixman is native endian */
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
},
105 pixman_format_code_t
qemu_drm_format_to_pixman(uint32_t drm_format
)
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
;
117 uint32_t qemu_pixman_to_drm_format(pixman_format_code_t pixman_format
)
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
;
129 int qemu_pixman_get_type(int rshift
, int gshift
, int bshift
)
131 int type
= PIXMAN_TYPE_OTHER
;
133 if (rshift
> gshift
&& gshift
> bshift
) {
135 type
= PIXMAN_TYPE_ARGB
;
137 type
= PIXMAN_TYPE_RGBA
;
139 } else if (rshift
< gshift
&& gshift
< bshift
) {
141 type
= PIXMAN_TYPE_ABGR
;
143 type
= PIXMAN_TYPE_BGRA
;
150 pixman_format_code_t
qemu_pixman_get_format(PixelFormat
*pf
)
152 pixman_format_code_t format
;
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
)) {
166 * Return true for known-good pixman conversions.
168 * UIs using pixman for format conversion can hook this into
169 * DisplayChangeListenerOps->dpy_gfx_check_format
171 bool qemu_pixman_check_format(DisplayChangeListener
*dcl
,
172 pixman_format_code_t format
)
176 case PIXMAN_x8r8g8b8
:
177 case PIXMAN_a8r8g8b8
:
178 case PIXMAN_b8g8r8x8
:
179 case PIXMAN_b8g8r8a8
:
184 case PIXMAN_x1r5g5b5
:
193 pixman_image_t
*qemu_pixman_linebuf_create(pixman_format_code_t format
,
196 pixman_image_t
*image
= pixman_image_create_bits(format
, width
, 1, NULL
, 0);
197 assert(image
!= NULL
);
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
)
205 pixman_image_composite(PIXMAN_OP_SRC
, fb
, NULL
, linebuf
,
206 x
, y
, 0, 0, 0, 0, width
, 1);
209 pixman_image_t
*qemu_pixman_mirror_create(pixman_format_code_t format
,
210 pixman_image_t
*image
)
212 return pixman_image_create_bits(format
,
213 pixman_image_get_width(image
),
214 pixman_image_get_height(image
),
216 pixman_image_get_stride(image
));
220 void qemu_pixman_image_unref(pixman_image_t
*image
)
225 pixman_image_unref(image
);
229 pixman_image_t
*qemu_pixman_glyph_from_vgafont(int height
, const uint8_t *font
,
232 pixman_image_t
*glyph
;
237 glyph
= pixman_image_create_bits(PIXMAN_a8
, 8, height
,
239 data
= (uint8_t *)pixman_image_get_data(glyph
);
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;
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
)
257 pixman_image_t
*ifg
= pixman_image_create_solid_fill(fgcol
);
258 pixman_image_t
*ibg
= pixman_image_create_solid_fill(bgcol
);
260 pixman_image_composite(PIXMAN_OP_SRC
, ibg
, NULL
, surface
,
264 pixman_image_composite(PIXMAN_OP_OVER
, ifg
, glyph
, surface
,
268 pixman_image_unref(ifg
);
269 pixman_image_unref(ibg
);
271 #endif /* CONFIG_PIXMAN */
274 qemu_pixman_shareable_alloc(const char *name
, size_t size
,
275 qemu_pixman_shareable
*handle
,
279 return qemu_win32_map_alloc(size
, handle
, errp
);
281 return qemu_memfd_alloc(name
, size
, 0, handle
, errp
);
286 qemu_pixman_shareable_free(qemu_pixman_shareable handle
,
287 void *ptr
, size_t size
)
290 qemu_win32_map_free(ptr
, handle
, &error_warn
);
292 qemu_memfd_free(ptr
, size
, handle
);
297 qemu_pixman_shared_image_destroy(pixman_image_t
*image
, void *data
)
299 qemu_pixman_shareable handle
= PTR_TO_SHAREABLE(data
);
300 void *ptr
= pixman_image_get_data(image
);
301 size_t size
= pixman_image_get_height(image
) * pixman_image_get_stride(image
);
303 qemu_pixman_shareable_free(handle
, ptr
, size
);
307 qemu_pixman_image_new_shareable(pixman_image_t
**image
,
308 qemu_pixman_shareable
*handle
,
310 pixman_format_code_t format
,
317 size_t size
= height
* rowstride_bytes
;
320 g_return_val_if_fail(image
!= NULL
, false);
321 g_return_val_if_fail(handle
!= NULL
, false);
323 bits
= qemu_pixman_shareable_alloc(name
, size
, handle
, errp
);
328 *image
= pixman_image_create_bits(format
, width
, height
, bits
, rowstride_bytes
);
330 error_setg(errp
, "Failed to allocate image");
331 qemu_pixman_shareable_free(*handle
, bits
, size
);
335 pixman_image_set_destroy_function(*image
,
336 qemu_pixman_shared_image_destroy
,
337 SHAREABLE_TO_PTR(*handle
));