[gtk/wip/otte/nodeeditor2: 29/32] node-editor: Add renderings with alternative renderers



commit c646d6a93d5c324c8bce3ac6f60c322456f74a5c
Author: Benjamin Otte <otte redhat com>
Date:   Sat Mar 9 14:55:17 2019 +0100

    node-editor: Add renderings with alternative renderers
    
    That's pretty rough around the edges, but it mostly works.
    Apart from sizing, screw listbox sizing.

 demos/node-editor/gtkrendererpaintable.c        | 323 ++++++++++++++++++++++++
 demos/node-editor/gtkrendererpaintableprivate.h |  43 ++++
 demos/node-editor/meson.build                   |   1 +
 demos/node-editor/node-editor-window.c          | 112 +++++++-
 demos/node-editor/node-editor-window.ui         |  32 ++-
 5 files changed, 501 insertions(+), 10 deletions(-)
---
diff --git a/demos/node-editor/gtkrendererpaintable.c b/demos/node-editor/gtkrendererpaintable.c
new file mode 100644
index 0000000000..cf11db7c0b
--- /dev/null
+++ b/demos/node-editor/gtkrendererpaintable.c
@@ -0,0 +1,323 @@
+/*
+ * Copyright © 2019 Benjamin Otte
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Benjamin Otte <otte gnome org>
+ */
+
+#include "config.h"
+
+#include "gtkrendererpaintableprivate.h"
+
+#include <gtk/gtk.h>
+
+struct _GtkRendererPaintable
+{
+  GObject parent_instance;
+
+  GskRenderer *renderer;
+  GdkPaintable *paintable;
+};
+
+struct _GtkRendererPaintableClass
+{
+  GObjectClass parent_class;
+};
+
+enum {
+  PROP_0,
+  PROP_PAINTABLE,
+  PROP_RENDERER,
+  N_PROPS
+};
+
+static GParamSpec *properties[N_PROPS] = { NULL, };
+
+static void
+gtk_renderer_paintable_paintable_snapshot (GdkPaintable *paintable,
+                                           GdkSnapshot  *snapshot,
+                                           double        width,
+                                           double        height)
+{
+  GtkRendererPaintable *self = GTK_RENDERER_PAINTABLE (paintable);
+  GtkSnapshot *node_snapshot;
+  GskRenderNode *node;
+  GdkTexture *texture;
+
+  if (self->paintable == NULL)
+    return;
+
+  if (self->renderer == NULL ||
+      !gsk_renderer_is_realized (self->renderer))
+    {
+      gdk_paintable_snapshot (self->paintable, snapshot, width, height);
+      return;
+    }
+
+  node_snapshot = gtk_snapshot_new ();
+  gdk_paintable_snapshot (self->paintable, node_snapshot, width, height);
+  node = gtk_snapshot_free_to_node (node_snapshot);
+  if (node == NULL)
+    return;
+
+  texture = gsk_renderer_render_texture (self->renderer,
+                                         node,
+                                         &GRAPHENE_RECT_INIT (0, 0, width, height));
+
+  gdk_paintable_snapshot (GDK_PAINTABLE (texture), snapshot, width, height);
+  g_object_unref (texture);
+}
+
+static int
+gtk_renderer_paintable_paintable_get_intrinsic_width (GdkPaintable *paintable)
+{
+  GtkRendererPaintable *self = GTK_RENDERER_PAINTABLE (paintable);
+
+  if (self->paintable == NULL)
+    return 0;
+
+  return gdk_paintable_get_intrinsic_width (self->paintable);
+}
+
+static int
+gtk_renderer_paintable_paintable_get_intrinsic_height (GdkPaintable *paintable)
+{
+  GtkRendererPaintable *self = GTK_RENDERER_PAINTABLE (paintable);
+
+  if (self->paintable == NULL)
+    return 0;
+
+  return gdk_paintable_get_intrinsic_height (self->paintable);
+}
+
+static double
+gtk_renderer_paintable_paintable_get_intrinsic_aspect_ratio (GdkPaintable *paintable)
+{
+  GtkRendererPaintable *self = GTK_RENDERER_PAINTABLE (paintable);
+
+  if (self->paintable == NULL)
+    return 0.0;
+
+  return gdk_paintable_get_intrinsic_aspect_ratio (self->paintable);
+}
+
+static void
+gtk_renderer_paintable_paintable_init (GdkPaintableInterface *iface)
+{
+  iface->snapshot = gtk_renderer_paintable_paintable_snapshot;
+  iface->get_intrinsic_width = gtk_renderer_paintable_paintable_get_intrinsic_width;
+  iface->get_intrinsic_height = gtk_renderer_paintable_paintable_get_intrinsic_height;
+  iface->get_intrinsic_aspect_ratio = gtk_renderer_paintable_paintable_get_intrinsic_aspect_ratio;
+}
+
+G_DEFINE_TYPE_EXTENDED (GtkRendererPaintable, gtk_renderer_paintable, G_TYPE_OBJECT, 0,
+                        G_IMPLEMENT_INTERFACE (GDK_TYPE_PAINTABLE,
+                                               gtk_renderer_paintable_paintable_init))
+
+static void
+gtk_renderer_paintable_set_property (GObject      *object,
+                                     guint         prop_id,
+                                     const GValue *value,
+                                     GParamSpec   *pspec)
+
+{
+  GtkRendererPaintable *self = GTK_RENDERER_PAINTABLE (object);
+
+  switch (prop_id)
+    {
+    case PROP_PAINTABLE:
+      gtk_renderer_paintable_set_paintable (self, g_value_get_object (value));
+      break;
+
+    case PROP_RENDERER:
+      gtk_renderer_paintable_set_renderer (self, g_value_get_object (value));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    }
+}
+
+static void
+gtk_renderer_paintable_get_property (GObject    *object,
+                                     guint       prop_id,
+                                     GValue     *value,
+                                     GParamSpec *pspec)
+{
+  GtkRendererPaintable *self = GTK_RENDERER_PAINTABLE (object);
+
+  switch (prop_id)
+    {
+    case PROP_PAINTABLE:
+      g_value_set_object (value, self->paintable);
+      break;
+
+    case PROP_RENDERER:
+      g_value_set_object (value, self->renderer);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    }
+}
+
+static void
+gtk_renderer_paintable_unset_paintable (GtkRendererPaintable *self)
+{
+  guint flags;
+  
+  if (self->paintable == NULL)
+    return;
+
+  flags = gdk_paintable_get_flags (self->paintable);
+
+  if ((flags & GDK_PAINTABLE_STATIC_CONTENTS) == 0)
+    g_signal_handlers_disconnect_by_func (self->paintable,
+                                          gdk_paintable_invalidate_contents,
+                                          self);
+
+  if ((flags & GDK_PAINTABLE_STATIC_SIZE) == 0)
+    g_signal_handlers_disconnect_by_func (self->paintable,
+                                          gdk_paintable_invalidate_size,
+                                          self);
+
+  g_clear_object (&self->paintable);
+}
+
+static void
+gtk_renderer_paintable_dispose (GObject *object)
+{
+  GtkRendererPaintable *self = GTK_RENDERER_PAINTABLE (object);
+
+  g_clear_object (&self->renderer);
+  gtk_renderer_paintable_unset_paintable (self);
+
+  G_OBJECT_CLASS (gtk_renderer_paintable_parent_class)->dispose (object);
+}
+
+static void
+gtk_renderer_paintable_class_init (GtkRendererPaintableClass *klass)
+{
+  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+
+  gobject_class->get_property = gtk_renderer_paintable_get_property;
+  gobject_class->set_property = gtk_renderer_paintable_set_property;
+  gobject_class->dispose = gtk_renderer_paintable_dispose;
+
+  properties[PROP_PAINTABLE] =
+    g_param_spec_object ("paintable",
+                         "Paintable",
+                         "The paintable to be shown",
+                         GDK_TYPE_PAINTABLE,
+                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);
+
+  properties[PROP_RENDERER] =
+    g_param_spec_object ("renderer",
+                         "Renderer",
+                         "Renderer used to render the paintable",
+                         GSK_TYPE_RENDERER,
+                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);
+
+  g_object_class_install_properties (gobject_class, N_PROPS, properties);
+}
+
+static void
+gtk_renderer_paintable_init (GtkRendererPaintable *self)
+{
+}
+
+GdkPaintable *
+gtk_renderer_paintable_new (GskRenderer  *renderer,
+                            GdkPaintable *paintable)
+{
+  g_return_val_if_fail (renderer == NULL || GSK_IS_RENDERER (renderer), NULL);
+  g_return_val_if_fail (paintable == NULL || GDK_IS_PAINTABLE (paintable), NULL);
+
+  return g_object_new (GTK_TYPE_RENDERER_PAINTABLE,
+                       "renderer", renderer,
+                       "paintable", paintable,
+                       NULL);
+}
+
+void
+gtk_renderer_paintable_set_renderer (GtkRendererPaintable *self,
+                                     GskRenderer          *renderer)
+{
+  g_return_if_fail (GTK_IS_RENDERER_PAINTABLE (self));
+  g_return_if_fail (renderer == NULL || GSK_IS_RENDERER (renderer));
+
+  if (!g_set_object (&self->renderer, renderer))
+    return;
+
+  if (self->paintable)
+    gdk_paintable_invalidate_contents (GDK_PAINTABLE (self));
+
+  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_RENDERER]);
+}
+
+GskRenderer *
+gtk_renderer_paintable_get_renderer (GtkRendererPaintable *self)
+{
+  g_return_val_if_fail (GTK_IS_RENDERER_PAINTABLE (self), NULL);
+
+  return self->renderer;
+}
+
+void
+gtk_renderer_paintable_set_paintable (GtkRendererPaintable *self,
+                                      GdkPaintable         *paintable)
+{
+  g_return_if_fail (GTK_IS_RENDERER_PAINTABLE (self));
+  g_return_if_fail (paintable == NULL || GDK_IS_PAINTABLE (paintable));
+
+  if (self->paintable == paintable)
+    return;
+
+  gtk_renderer_paintable_unset_paintable (self);
+
+  if (paintable)
+    {
+      const guint flags = gdk_paintable_get_flags (paintable);
+
+      self->paintable = g_object_ref (paintable);
+
+      if ((flags & GDK_PAINTABLE_STATIC_CONTENTS) == 0)
+        g_signal_connect_swapped (paintable,
+                                  "invalidate-contents",
+                                  G_CALLBACK (gdk_paintable_invalidate_contents),
+                                  self);
+      if ((flags & GDK_PAINTABLE_STATIC_SIZE) == 0)
+        g_signal_connect_swapped (paintable,
+                                  "invalidate-size",
+                                  G_CALLBACK (gdk_paintable_invalidate_size),
+                                  self);
+    }
+
+  gdk_paintable_invalidate_size (GDK_PAINTABLE (self));
+  gdk_paintable_invalidate_contents (GDK_PAINTABLE (self));
+
+  g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_PAINTABLE]);
+}
+
+GdkPaintable *
+gtk_renderer_paintable_get_paintable (GtkRendererPaintable *self)
+{
+  g_return_val_if_fail (GTK_IS_RENDERER_PAINTABLE (self), NULL);
+
+  return self->paintable;
+}
+
diff --git a/demos/node-editor/gtkrendererpaintableprivate.h b/demos/node-editor/gtkrendererpaintableprivate.h
new file mode 100644
index 0000000000..39b9f8e93b
--- /dev/null
+++ b/demos/node-editor/gtkrendererpaintableprivate.h
@@ -0,0 +1,43 @@
+/*
+ * Copyright © 2019 Benjamin Otte
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Benjamin Otte <otte gnome org>
+ */
+
+#ifndef __GTK_RENDERER_PAINTABLE_H__
+#define __GTK_RENDERER_PAINTABLE_H__
+
+#include <gsk/gsk.h>
+
+G_BEGIN_DECLS
+
+#define GTK_TYPE_RENDERER_PAINTABLE (gtk_renderer_paintable_get_type ())
+
+G_DECLARE_FINAL_TYPE (GtkRendererPaintable, gtk_renderer_paintable, GTK, RENDERER_PAINTABLE, GObject)
+
+GdkPaintable *  gtk_renderer_paintable_new                (GskRenderer          *renderer,
+                                                           GdkPaintable         *paintable);
+
+void            gtk_renderer_paintable_set_renderer       (GtkRendererPaintable *self,
+                                                           GskRenderer          *renderer);
+GskRenderer *   gtk_renderer_paintable_get_renderer       (GtkRendererPaintable *self) G_GNUC_PURE;
+void            gtk_renderer_paintable_set_paintable      (GtkRendererPaintable *self,
+                                                           GdkPaintable         *paintable);
+GdkPaintable *  gtk_renderer_paintable_get_paintable      (GtkRendererPaintable *self) G_GNUC_PURE;
+
+G_END_DECLS
+
+#endif /* __GTK_RENDERER_PAINTABLE_H__ */
diff --git a/demos/node-editor/meson.build b/demos/node-editor/meson.build
index b12bc326e6..81f96a2fd8 100644
--- a/demos/node-editor/meson.build
+++ b/demos/node-editor/meson.build
@@ -1,4 +1,5 @@
 node_editor_sources = [
+  'gtkrendererpaintable.c',
   'main.c',
   'node-editor-application.c',
   'node-editor-window.c',
diff --git a/demos/node-editor/node-editor-window.c b/demos/node-editor/node-editor-window.c
index ac276dd3f1..dc07f0a102 100644
--- a/demos/node-editor/node-editor-window.c
+++ b/demos/node-editor/node-editor-window.c
@@ -21,6 +21,8 @@
 
 #include "node-editor-window.h"
 
+#include "gtkrendererpaintableprivate.h"
+
 #include "gsk/gskrendernodeparserprivate.h"
 
 typedef struct
@@ -38,6 +40,10 @@ struct _NodeEditorWindow
   GtkWidget *text_view;
   GtkTextBuffer *text_buffer;
 
+  GtkWidget *renderer_listbox;
+  GListStore *renderers;
+  GdkPaintable *paintable;
+
   GArray *errors;
 };
 
@@ -114,6 +120,7 @@ text_changed (GtkTextBuffer    *buffer,
       GtkSnapshot *snapshot;
       GdkPaintable *paintable;
       graphene_rect_t bounds;
+      guint i;
 
       snapshot = gtk_snapshot_new ();
       gsk_render_node_get_bounds (node, &bounds);
@@ -122,6 +129,12 @@ text_changed (GtkTextBuffer    *buffer,
       gsk_render_node_unref (node);
       paintable = gtk_snapshot_free_to_paintable (snapshot, &bounds.size);
       gtk_picture_set_paintable (GTK_PICTURE (self->picture), paintable);
+      for (i = 0; i < g_list_model_get_n_items (G_LIST_MODEL (self->renderers)); i++)
+        {
+          gpointer item = g_list_model_get_item (G_LIST_MODEL (self->renderers), i);
+          gtk_renderer_paintable_set_paintable (item, paintable);
+          g_object_unref (item);
+        }
       g_clear_object (&paintable);
     }
   else
@@ -388,9 +401,76 @@ node_editor_window_finalize (GObject *object)
 
   g_array_free (self->errors, TRUE);
 
+  g_clear_object (&self->renderers);
+
   G_OBJECT_CLASS (node_editor_window_parent_class)->finalize (object);
 }
 
+static void
+node_editor_window_add_renderer (NodeEditorWindow *self,
+                                 GskRenderer      *renderer,
+                                 const char       *description)
+{
+  GdkSurface *surface;
+  GdkPaintable *paintable;
+
+  surface = gtk_widget_get_surface (GTK_WIDGET (self));
+  g_assert (surface != NULL);
+
+  if (renderer != NULL && !gsk_renderer_realize (renderer, surface, NULL))
+    {
+      g_object_unref (renderer);
+      return;
+    }
+
+  paintable = gtk_renderer_paintable_new (renderer, gtk_picture_get_paintable (GTK_PICTURE (self->picture)));
+  g_object_set_data_full (G_OBJECT (paintable), "description", g_strdup (description), g_free);
+  g_clear_object (&renderer);
+
+  g_list_store_append (self->renderers, paintable);
+  g_object_unref (paintable);
+}
+
+static void
+node_editor_window_realize (GtkWidget *widget)
+{
+  NodeEditorWindow *self = NODE_EDITOR_WINDOW (widget);
+
+  GTK_WIDGET_CLASS (node_editor_window_parent_class)->realize (widget);
+
+#if 0
+  node_editor_window_add_renderer (self,
+                                   NULL,
+                                   "Default");
+#endif
+  node_editor_window_add_renderer (self,
+                                   gsk_gl_renderer_new (),
+                                   "OpenGL");
+#ifdef GDK_RENDERING_VULKAN
+  node_editor_window_add_renderer (self,
+                                   gsk_vulkan_renderer_new (),
+                                   "Vulkan");
+#endif
+#ifdef GDK_WINDOWING_BROADWAY
+  node_editor_window_add_renderer (self,
+                                   gsk_broadway_renderer_new (),
+                                   "Broadway");
+#endif
+  node_editor_window_add_renderer (self,
+                                   gsk_cairo_renderer_new (),
+                                   "Cairo");
+}
+
+static void
+node_editor_window_unrealize (GtkWidget *widget)
+{
+  NodeEditorWindow *self = NODE_EDITOR_WINDOW (widget);
+
+  g_list_store_remove_all (self->renderers);
+
+  GTK_WIDGET_CLASS (node_editor_window_parent_class)->unrealize (widget);
+}
+
 static void
 node_editor_window_class_init (NodeEditorWindowClass *class)
 {
@@ -399,12 +479,16 @@ node_editor_window_class_init (NodeEditorWindowClass *class)
 
   object_class->finalize = node_editor_window_finalize;
 
-  gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (class),
+  gtk_widget_class_set_template_from_resource (widget_class,
                                                "/org/gtk/gtk4/node-editor/node-editor-window.ui");
 
+  widget_class->realize = node_editor_window_realize;
+  widget_class->unrealize = node_editor_window_unrealize;
+
   gtk_widget_class_bind_template_child (widget_class, NodeEditorWindow, text_buffer);
   gtk_widget_class_bind_template_child (widget_class, NodeEditorWindow, text_view);
   gtk_widget_class_bind_template_child (widget_class, NodeEditorWindow, picture);
+  gtk_widget_class_bind_template_child (widget_class, NodeEditorWindow, renderer_listbox);
 
   gtk_widget_class_bind_template_callback (widget_class, text_changed);
   gtk_widget_class_bind_template_callback (widget_class, text_view_query_tooltip_cb);
@@ -413,11 +497,37 @@ node_editor_window_class_init (NodeEditorWindowClass *class)
   gtk_widget_class_bind_template_callback (widget_class, export_image_cb);
 }
 
+static GtkWidget *
+node_editor_window_create_renderer_widget (gpointer item,
+                                           gpointer user_data)
+{
+  GdkPaintable *paintable = item;
+  GtkWidget *box, *label, *picture;
+
+  box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
+  gtk_widget_set_size_request (box, 120, 90);
+
+  label = gtk_label_new (g_object_get_data (G_OBJECT (paintable), "description"));
+  gtk_container_add (GTK_CONTAINER (box), label);
+
+  picture = gtk_picture_new_for_paintable (paintable);
+  gtk_container_add (GTK_CONTAINER (box), picture);
+
+  return box;
+}
+
 static void
 node_editor_window_init (NodeEditorWindow *self)
 {
   gtk_widget_init_template (GTK_WIDGET (self));
 
+  self->renderers = g_list_store_new (GDK_TYPE_PAINTABLE);
+  gtk_list_box_bind_model (GTK_LIST_BOX (self->renderer_listbox),
+                           G_LIST_MODEL (self->renderers),
+                           node_editor_window_create_renderer_widget,
+                           self,
+                           NULL);
+
   self->errors = g_array_new (FALSE, TRUE, sizeof (TextViewError));
   g_array_set_clear_func (self->errors, (GDestroyNotify)text_view_error_free);
 }
diff --git a/demos/node-editor/node-editor-window.ui b/demos/node-editor/node-editor-window.ui
index e886b53ee0..0ce06be89e 100644
--- a/demos/node-editor/node-editor-window.ui
+++ b/demos/node-editor/node-editor-window.ui
@@ -76,17 +76,31 @@
           </object>
         </child>
         <child>
-          <object class="GtkScrolledWindow">
-            <property name="expand">1</property>
-            <property name="min-content-height">100</property>
-            <property name="min-content-width">100</property>
+          <object class="GtkBox">
+            <child>
+              <object class="GtkScrolledWindow">
+                <property name="expand">1</property>
+                <property name="min-content-height">100</property>
+                <property name="min-content-width">100</property>
+                <child>
+                  <object class="GtkViewport">
+                    <child>
+                      <object class="GtkPicture" id="picture">
+                        <property name="can-shrink">0</property>
+                        <property name="halign">center</property>
+                        <property name="valign">center</property>
+                      </object>
+                    </child>
+                  </object>
+                </child>
+              </object>
+            </child>
             <child>
-              <object class="GtkViewport">
+              <object class="GtkScrolledWindow">
+                <property name="hscrollbar-policy">never</property>
                 <child>
-                  <object class="GtkPicture" id="picture">
-                    <property name="can-shrink">0</property>
-                    <property name="halign">center</property>
-                    <property name="valign">center</property>
+                  <object class="GtkListBox" id="renderer_listbox">
+                    <property name="selection-mode">none</property>
                   </object>
                 </child>
               </object>


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