[gtk+] gdk: Add a gl texture implementation



commit b366ea84a76e8af1e8dd2e699e951c36afc26d28
Author: Matthias Clasen <mclasen redhat com>
Date:   Wed Jan 17 00:32:26 2018 -0500

    gdk: Add a gl texture implementation
    
    This will be used to pass a GL textures from the application
    (or rather, GtkGLArea) down to the GSK GL renderer.

 gdk/gdktexture.c        |  109 +++++++++++++++++++++++++++++++++++++++++++++++
 gdk/gdktexture.h        |    9 ++++
 gdk/gdktextureprivate.h |    7 +++
 3 files changed, 125 insertions(+), 0 deletions(-)
---
diff --git a/gdk/gdktexture.c b/gdk/gdktexture.c
index 936b5c7..c87ed57 100644
--- a/gdk/gdktexture.c
+++ b/gdk/gdktexture.c
@@ -39,6 +39,8 @@
 #include "gdkinternals.h"
 #include "gdkcairo.h"
 
+#include <epoxy/gl.h>
+
 /**
  * SECTION:gdktexture
  * @Short_description: Image data for display
@@ -432,6 +434,88 @@ gdk_pixbuf_texture_init (GdkPixbufTexture *self)
 {
 }
 
+/* GdkGLTexture */
+
+
+struct _GdkGLTexture {
+  GdkTexture parent_instance;
+
+  GdkGLContext *context;
+  int id;
+
+  GDestroyNotify destroy;
+  gpointer data;
+};
+
+struct _GdkGLTextureClass {
+  GdkTextureClass parent_class;
+};
+
+G_DEFINE_TYPE (GdkGLTexture, gdk_gl_texture, GDK_TYPE_TEXTURE)
+
+static void
+gdk_gl_texture_dispose (GObject *object)
+{
+  GdkGLTexture *self = GDK_GL_TEXTURE (object);
+
+  g_object_unref (self->context);
+
+  if (self->destroy)
+    self->destroy (self->data);
+
+  G_OBJECT_CLASS (gdk_gl_texture_parent_class)->dispose (object);
+}
+
+static void
+gdk_gl_texture_download (GdkTexture *texture,
+                         guchar     *data,
+                         gsize       stride)
+{
+  GdkGLTexture *self = GDK_GL_TEXTURE (texture);
+  cairo_surface_t *surface;
+  cairo_t *cr;
+  GdkWindow *window;
+
+  surface = cairo_image_surface_create_for_data (data,
+                                                 CAIRO_FORMAT_ARGB32,
+                                                 texture->width, texture->height,
+                                                 stride);
+
+  cr = cairo_create (surface);
+  window = gdk_gl_context_get_window (self->context);
+  gdk_cairo_draw_from_gl (cr, window, self->id, GL_TEXTURE, 1, 0, 0, texture->width, texture->height);
+  cairo_destroy (cr);
+  cairo_surface_finish (surface);
+  cairo_surface_destroy (surface);
+}
+
+static void
+gdk_gl_texture_class_init (GdkGLTextureClass *klass)
+{
+  GdkTextureClass *texture_class = GDK_TEXTURE_CLASS (klass);
+  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+
+  texture_class->download = gdk_gl_texture_download;
+  gobject_class->dispose = gdk_gl_texture_dispose;
+}
+
+static void
+gdk_gl_texture_init (GdkGLTexture *self)
+{
+}
+
+GdkGLContext *
+gdk_gl_texture_get_context (GdkGLTexture *self)
+{
+  return self->context;
+}
+
+int
+gdk_gl_texture_get_id (GdkGLTexture *self)
+{
+  return self->id;
+}
+
 /**
  * gdk_texture_new_for_pixbuf:
  * @pixbuf: a #GdkPixbuf
@@ -532,6 +616,31 @@ gdk_texture_new_from_file (GFile   *file,
   return texture;
 }
 
+GdkTexture *
+gdk_texture_new_for_gl (GdkGLContext   *context,
+                        int             id,
+                        int             width,
+                        int             height,
+                        GDestroyNotify  destroy,
+                        gpointer        data)
+{
+  GdkGLTexture *self;
+
+  g_return_val_if_fail (GDK_IS_GL_CONTEXT (context), NULL);
+
+  self = g_object_new (GDK_TYPE_GL_TEXTURE,
+                       "width", width,
+                       "height", height,
+                       NULL);
+
+  self->context = g_object_ref (context);
+  self->id = id;
+  self->destroy = destroy;
+  self->data = data;
+
+  return GDK_TEXTURE (self);
+}
+
 /**
  * gdk_texture_get_width:
  * @texture: a #GdkTexture
diff --git a/gdk/gdktexture.h b/gdk/gdktexture.h
index fe957c0..4f3ead2 100644
--- a/gdk/gdktexture.h
+++ b/gdk/gdktexture.h
@@ -26,6 +26,7 @@
 #include <gdk/gdkversionmacros.h>
 #include <gdk/gdktypes.h>
 #include <gdk-pixbuf/gdk-pixbuf.h>
+#include <gdk/gdkglcontext.h>
 
 G_BEGIN_DECLS
 
@@ -56,6 +57,14 @@ GdkTexture *            gdk_texture_new_from_file              (GFile
                                                                 GError         **error);
 
 GDK_AVAILABLE_IN_3_94
+GdkTexture *            gdk_texture_new_for_gl                 (GdkGLContext    *context,
+                                                                int              id,
+                                                                int              width,
+                                                                int              height,
+                                                                GDestroyNotify   destroy,
+                                                                gpointer         data);
+
+GDK_AVAILABLE_IN_3_94
 int                     gdk_texture_get_width                  (GdkTexture      *texture);
 GDK_AVAILABLE_IN_3_94
 int                     gdk_texture_get_height                 (GdkTexture      *texture);
diff --git a/gdk/gdktextureprivate.h b/gdk/gdktextureprivate.h
index f9e5fbe..6aa96d1 100644
--- a/gdk/gdktextureprivate.h
+++ b/gdk/gdktextureprivate.h
@@ -44,6 +44,13 @@ void                    gdk_texture_clear_render_data   (GdkTexture
 gpointer                gdk_texture_get_render_data     (GdkTexture             *self,
                                                          gpointer                key);
 
+#define GDK_TYPE_GL_TEXTURE (gdk_gl_texture_get_type ())
+
+G_DECLARE_FINAL_TYPE (GdkGLTexture, gdk_gl_texture, GDK, GL_TEXTURE, GdkTexture)
+
+GdkGLContext *          gdk_gl_texture_get_context      (GdkGLTexture           *self);
+int                     gdk_gl_texture_get_id           (GdkGLTexture           *self);
+
 G_END_DECLS
 
 #endif /* __GDK_TEXTURE_PRIVATE_H__ */


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