diff options
| author | Marc-André Lureau <marcandre.lureau@redhat.com> | 2024-10-08 16:50:27 +0400 |
|---|---|---|
| committer | Marc-André Lureau <marcandre.lureau@redhat.com> | 2024-10-14 17:34:09 +0400 |
| commit | 1ff788db9781615be745671ebdb2eb82c137c5b8 (patch) | |
| tree | 85b092cc9ac20e344f99259a81bc2741740317f3 /ui/qemu-pixman.c | |
| parent | 5f899c34af1dbb0f621287faf9bcfb60fa237543 (diff) | |
| download | focaccia-qemu-1ff788db9781615be745671ebdb2eb82c137c5b8.tar.gz focaccia-qemu-1ff788db9781615be745671ebdb2eb82c137c5b8.zip | |
ui: refactor using a common qemu_pixman_shareable
Use a common shareable type for win32 & unix, and helper functions. This simplify the code as it avoids a lot of #ifdef'ery. Note: if it helps review, commits could be reordered to introduce the common type before introducing shareable memory for unix. Suggested-by: Akihiko Odaki <akihiko.odaki@daynix.com> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com> Reviewed-by: Akihiko Odaki <akihiko.odaki@daynix.com> Message-ID: <20241008125028.1177932-19-marcandre.lureau@redhat.com>
Diffstat (limited to 'ui/qemu-pixman.c')
| -rw-r--r-- | ui/qemu-pixman.c | 68 |
1 files changed, 60 insertions, 8 deletions
diff --git a/ui/qemu-pixman.c b/ui/qemu-pixman.c index 46a91e7f7a..6ef4376f4e 100644 --- a/ui/qemu-pixman.c +++ b/ui/qemu-pixman.c @@ -270,19 +270,71 @@ void qemu_pixman_glyph_render(pixman_image_t *glyph, } #endif /* CONFIG_PIXMAN */ -void -qemu_pixman_shared_image_destroy(pixman_image_t *image, void *data) +static void * +qemu_pixman_shareable_alloc(const char *name, size_t size, + qemu_pixman_shareable *handle, + Error **errp) { - void *ptr = pixman_image_get_data(image); - #ifdef WIN32 - HANDLE handle = data; + return qemu_win32_map_alloc(size, handle, errp); +#else + return qemu_memfd_alloc(name, size, 0, handle, errp); +#endif +} +static void +qemu_pixman_shareable_free(qemu_pixman_shareable handle, + void *ptr, size_t size) +{ +#ifdef WIN32 qemu_win32_map_free(ptr, handle, &error_warn); #else - int shmfd = GPOINTER_TO_INT(data); + qemu_memfd_free(ptr, size, handle); +#endif +} + +static void +qemu_pixman_shared_image_destroy(pixman_image_t *image, void *data) +{ + qemu_pixman_shareable handle = PTR_TO_SHAREABLE(data); + void *ptr = pixman_image_get_data(image); size_t size = pixman_image_get_height(image) * pixman_image_get_stride(image); - qemu_memfd_free(ptr, size, shmfd); -#endif + qemu_pixman_shareable_free(handle, ptr, size); +} + +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) +{ + ERRP_GUARD(); + size_t size = height * rowstride_bytes; + void *bits = NULL; + + g_return_val_if_fail(image != NULL, false); + g_return_val_if_fail(handle != NULL, false); + + bits = qemu_pixman_shareable_alloc(name, size, handle, errp); + if (!bits) { + return false; + } + + *image = pixman_image_create_bits(format, width, height, bits, rowstride_bytes); + if (!*image) { + error_setg(errp, "Failed to allocate image"); + qemu_pixman_shareable_free(*handle, bits, size); + return false; + } + + pixman_image_set_destroy_function(*image, + qemu_pixman_shared_image_destroy, + SHAREABLE_TO_PTR(*handle)); + + return true; } |