png: Move texture data download further down

We only download the data when we actually need it for writing into the
PNG stream.

This allows modifying the download parameters (in particular color state
in the next commit) while writing out their settings, so the code for
selecting the right colorstate liives in only one place.

We have to be careful though, because the download now happens after the
setjmp(), so we need to make sure the error path handles both cases
without leaking: Where the download has happened and where it hasn't.
This commit is contained in:
Benjamin Otte 2024-07-16 21:14:13 +02:00
parent 03daf42fb5
commit 6c33afc13c

View File

@ -451,15 +451,11 @@ gdk_save_png (GdkTexture *texture)
return NULL; return NULL;
} }
gdk_texture_downloader_init (&downloader, texture); bytes = NULL;
gdk_texture_downloader_set_format (&downloader, format);
bytes = gdk_texture_downloader_download_bytes (&downloader, &stride);
gdk_texture_downloader_finish (&downloader);
data = g_bytes_get_data (bytes, NULL);
if (sigsetjmp (png_jmpbuf (png), 1)) if (sigsetjmp (png_jmpbuf (png), 1))
{ {
g_bytes_unref (bytes); g_clear_pointer (&bytes, g_bytes_unref);
g_free (io.data); g_free (io.data);
png_destroy_read_struct (&png, &info, NULL); png_destroy_read_struct (&png, &info, NULL);
return NULL; return NULL;
@ -479,6 +475,12 @@ gdk_save_png (GdkTexture *texture)
png_set_swap (png); png_set_swap (png);
#endif #endif
gdk_texture_downloader_init (&downloader, texture);
gdk_texture_downloader_set_format (&downloader, format);
bytes = gdk_texture_downloader_download_bytes (&downloader, &stride);
gdk_texture_downloader_finish (&downloader);
data = g_bytes_get_data (bytes, NULL);
for (y = 0; y < height; y++) for (y = 0; y < height; y++)
png_write_row (png, data + y * stride); png_write_row (png, data + y * stride);