[gtk/matthiasc/for-master] ngl: Avoid cairo_to_png in debug output



commit eb23e23b035d3d13facfe419aca396c990cbb07c
Author: Matthias Clasen <mclasen redhat com>
Date:   Tue Sep 14 17:59:26 2021 -0400

    ngl: Avoid cairo_to_png in debug output
    
    Just use a GL texture and gdk_texture_save_to_png,
    it was made for this.

 gsk/ngl/gsknglcommandqueue.c  | 14 ++++++++------
 gsk/ngl/gskngldriver.c        | 24 +++++++++++-------------
 gsk/ngl/gsknglshadowlibrary.c | 23 ++++++++++-------------
 3 files changed, 29 insertions(+), 32 deletions(-)
---
diff --git a/gsk/ngl/gsknglcommandqueue.c b/gsk/ngl/gsknglcommandqueue.c
index 217fce13a2..a5fbf0dc8d 100644
--- a/gsk/ngl/gsknglcommandqueue.c
+++ b/gsk/ngl/gsknglcommandqueue.c
@@ -197,9 +197,10 @@ gsk_ngl_command_queue_capture_png (GskNglCommandQueue *self,
                                    guint               height,
                                    gboolean            flip_y)
 {
-  cairo_surface_t *surface;
-  guint8 *data;
   guint stride;
+  guint8 *data;
+  GBytes *bytes;
+  GdkTexture *texture;
 
   g_assert (GSK_IS_NGL_COMMAND_QUEUE (self));
   g_assert (filename != NULL);
@@ -222,11 +223,12 @@ gsk_ngl_command_queue_capture_png (GskNglCommandQueue *self,
       data = flipped;
     }
 
-  surface = cairo_image_surface_create_for_data (data, CAIRO_FORMAT_ARGB32, width, height, stride);
-  cairo_surface_write_to_png (surface, filename);
+  bytes = g_bytes_new_take (data, height * stride);
+  texture = gdk_memory_texture_new (width, height, GDK_MEMORY_DEFAULT, bytes, stride);
+  g_bytes_unref (bytes);
 
-  cairo_surface_destroy (surface);
-  g_free (data);
+  gdk_texture_save_to_png (texture, filename);
+  g_object_unref (texture);
 }
 
 static inline gboolean
diff --git a/gsk/ngl/gskngldriver.c b/gsk/ngl/gskngldriver.c
index 196ae46987..07ac4b3c75 100644
--- a/gsk/ngl/gskngldriver.c
+++ b/gsk/ngl/gskngldriver.c
@@ -1131,20 +1131,18 @@ gsk_ngl_driver_lookup_shader (GskNglDriver  *self,
 
 #ifdef G_ENABLE_DEBUG
 static void
-write_atlas_to_png (GskNglTextureAtlas *atlas,
+write_atlas_to_png (GskNglDriver       *driver,
+                    GskNglTextureAtlas *atlas,
                     const char         *filename)
 {
-  int stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32, atlas->width);
-  guchar *data = g_malloc (atlas->height * stride);
-  cairo_surface_t *s;
-
-  glBindTexture (GL_TEXTURE_2D, atlas->texture_id);
-  glGetTexImage (GL_TEXTURE_2D, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, data);
-  s = cairo_image_surface_create_for_data (data, CAIRO_FORMAT_ARGB32, atlas->width, atlas->height, stride);
-  cairo_surface_write_to_png (s, filename);
-
-  cairo_surface_destroy (s);
-  g_free (data);
+  GdkTexture *texture;
+
+  texture = gdk_gl_texture_new (gsk_ngl_driver_get_context (driver),
+                                atlas->texture_id,
+                                atlas->width, atlas->height,
+                                NULL, NULL);
+  gdk_texture_save_to_png (texture, filename);
+  g_object_unref (texture);
 }
 
 void
@@ -1164,7 +1162,7 @@ gsk_ngl_driver_save_atlases_to_png (GskNglDriver *self,
                                         G_DIR_SEPARATOR_S,
                                         (int)self->current_frame_id,
                                         atlas->texture_id);
-      write_atlas_to_png (atlas, filename);
+      write_atlas_to_png (self, atlas, filename);
       g_free (filename);
     }
 }
diff --git a/gsk/ngl/gsknglshadowlibrary.c b/gsk/ngl/gsknglshadowlibrary.c
index 301a3c1624..64fb45b9be 100644
--- a/gsk/ngl/gsknglshadowlibrary.c
+++ b/gsk/ngl/gsknglshadowlibrary.c
@@ -204,25 +204,22 @@ gsk_ngl_shadow_library_lookup (GskNglShadowLibrary  *self,
 
 #if 0
 static void
-write_shadow_to_png (const Shadow *shadow)
+write_shadow_to_png (GskNglDriver *driver,
+                     const Shadow *shadow)
 {
   int width = shadow->outline.bounds.size.width + (shadow->outline.bounds.origin.x * 2);
   int height = shadow->outline.bounds.size.height + (shadow->outline.bounds.origin.y * 2);
-  int stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32, width);
-  guchar *data = g_malloc (height * stride);
-  cairo_surface_t *s;
   char *filename = g_strdup_printf ("shadow_cache_%d_%d_%d.png",
                                     width, height, shadow->texture_id);
+  GdkTexture *texture;
 
-  glBindTexture (GL_TEXTURE_2D, shadow->texture_id);
-  glGetTexImage (GL_TEXTURE_2D, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, data);
-  s = cairo_image_surface_create_for_data (data, CAIRO_FORMAT_ARGB32,
-                                           width, height,
-                                           stride);
-  cairo_surface_write_to_png (s, filename);
+  texture = gdk_gl_texture_new (gsk_ngl_driver_get_context (driver),
+                                shadow->texture_id,
+                                width, height,
+                                NULL, NULL);
+  gdk_texture_save_to_png (texture, filename);
 
-  cairo_surface_destroy (s);
-  g_free (data);
+  g_object_unref (texture);
   g_free (filename);
 }
 #endif
@@ -240,7 +237,7 @@ gsk_ngl_shadow_library_begin_frame (GskNglShadowLibrary *self)
   for (i = 0, p = self->shadows->len; i < p; i++)
     {
       const Shadow *shadow = &g_array_index (self->shadows, Shadow, i);
-      write_shadow_to_png (shadow);
+      write_shadow_to_png (self->driver, shadow);
     }
 #endif
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]