Some minor fpsoverlay improvements
authorMatthias Clasen <mclasen@redhat.com>
Wed, 1 Nov 2023 18:45:57 +0000 (14:45 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Fri, 17 Nov 2023 20:28:58 +0000 (15:28 -0500)
Avoid some allocations.

gtk/inspector/fpsoverlay.c

index a5aa45275cecbdc6e1e7448a473edb27346c9a54..a016066e9f86c75f3d3831ddd358c7d1637d0d12 100644 (file)
@@ -100,8 +100,6 @@ gtk_fps_overlay_snapshot (GtkInspectorOverlay *overlay,
   graphene_rect_t bounds;
   gboolean has_bounds;
   double overlay_opacity;
-  PangoGlyphString *glyphs;
-  GskRenderNode *fps_node;
 
   now = gdk_frame_clock_get_frame_time (gtk_widget_get_frame_clock (widget));
   info = g_hash_table_lookup (self->infos, widget);
@@ -149,42 +147,39 @@ gtk_fps_overlay_snapshot (GtkInspectorOverlay *overlay,
       has_bounds = gtk_widget_compute_bounds (widget, widget, &bounds);
     }
 
-  glyphs = pango_glyph_string_copy (self->glyphs);
+  gtk_snapshot_save (snapshot);
+  if (has_bounds)
+    gtk_snapshot_translate (snapshot, &GRAPHENE_POINT_INIT (bounds.origin.x + bounds.size.width - self->width, bounds.origin.y));
+
+  if (overlay_opacity < 1.0)
+    gtk_snapshot_push_opacity (snapshot, overlay_opacity);
+  gtk_snapshot_append_color (snapshot,
+                             &(GdkRGBA) { 0, 0, 0, 0.5 },
+                             &GRAPHENE_RECT_INIT (-1, -1, self->width + 2, self->height + 2));
 
   fps = gtk_fps_overlay_get_fps (widget);
-  if (fps == 0.0)
+  if (fps != 0.0)
     {
-      for (int i = 0; i < 7; i++)
-        glyphs->glyphs[i].glyph = PANGO_GLYPH_EMPTY;
-    }
-  else
-    {
-      char *fps_string = g_strdup_printf ("%7.2f fps", fps);
+      GskRenderNode *fps_node;
+      char fps_string[40];
+
+      g_snprintf (fps_string, sizeof (fps_string), "%7.2f fps", fps);
       for (int i = 0; i < 7; i++)
         {
           if (g_ascii_isdigit (fps_string[i]))
-            glyphs->glyphs[i].glyph = self->digits->glyphs[fps_string[i] - '0'].glyph;
+            self->glyphs->glyphs[i].glyph = self->digits->glyphs[fps_string[i] - '0'].glyph;
           else if (fps_string[i] == ' ')
-            glyphs->glyphs[i].glyph = PANGO_GLYPH_EMPTY;
+            self->glyphs->glyphs[i].glyph = PANGO_GLYPH_EMPTY;
         }
-      g_free (fps_string);
-    }
 
-  gtk_snapshot_save (snapshot);
-  if (has_bounds)
-    gtk_snapshot_translate (snapshot, &GRAPHENE_POINT_INIT (bounds.origin.x + bounds.size.width - self->width, bounds.origin.y));
+      fps_node = gsk_text_node_new (self->font,
+                                    self->glyphs,
+                                    &(GdkRGBA) { 1, 1, 1, 1 },
+                                    &GRAPHENE_POINT_INIT (0, self->baseline));
+      gtk_snapshot_append_node (snapshot, fps_node);
+      gsk_render_node_unref (fps_node);
+    }
 
-  if (overlay_opacity < 1.0)
-    gtk_snapshot_push_opacity (snapshot, overlay_opacity);
-  gtk_snapshot_append_color (snapshot,
-                             &(GdkRGBA) { 0, 0, 0, 0.5 },
-                             &GRAPHENE_RECT_INIT (-1, -1, self->width + 2, self->height + 2));
-  fps_node = gsk_text_node_new (self->font,
-                                glyphs,
-                                &(GdkRGBA) { 1, 1, 1, 1 },
-                                &GRAPHENE_POINT_INIT (0, self->baseline));
-  gtk_snapshot_append_node (snapshot, fps_node);
-  gsk_render_node_unref (fps_node);
   if (overlay_opacity < 1.0)
     gtk_snapshot_pop (snapshot);
   gtk_snapshot_restore (snapshot);