[gtk+/wip/gl-texture] wip: Add a gl texture implementation



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

    wip: Add a gl texture implementation
    
    This will be used to pass a GL texture from the application
    down to the GSK GL renderer.

 gdk/gdktexture.c |   90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 gdk/gdktexture.h |    6 +++
 2 files changed, 96 insertions(+), 0 deletions(-)
---
diff --git a/gdk/gdktexture.c b/gdk/gdktexture.c
index 936b5c7..9b9ebf0 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,73 @@ gdk_pixbuf_texture_init (GdkPixbufTexture *self)
 {
 }
 
+/* GdkGLTexture */
+
+#define GDK_TYPE_GL_TEXTURE (gdk_gl_texture_get_type ())
+
+G_DECLARE_FINAL_TYPE (GdkGLTexture, gdk_gl_texture, GDK, GL_TEXTURE, GdkTexture)
+
+struct _GdkGLTexture {
+  GdkTexture parent_instance;
+
+  GdkGLContext *context;
+  int id;
+};
+
+struct _GdkGLTextureClass {
+  GdkTextureClass parent_class;
+};
+
+G_DEFINE_TYPE (GdkGLTexture, gdk_gl_texture, GDK_TYPE_TEXTURE)
+
+static void
+gdk_gl_texture_finalize (GObject *object)
+{
+  GdkGLTexture *self = GDK_GL_TEXTURE (object);
+
+  g_object_unref (self->context);
+
+  G_OBJECT_CLASS (gdk_gl_texture_parent_class)->finalize (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->finalize = gdk_gl_texture_finalize;
+}
+
+static void
+gdk_gl_texture_init (GdkGLTexture *self)
+{
+}
+
 /**
  * gdk_texture_new_for_pixbuf:
  * @pixbuf: a #GdkPixbuf
@@ -532,6 +601,27 @@ gdk_texture_new_from_file (GFile   *file,
   return texture;
 }
 
+GdkTexture *
+gdk_texture_new_for_gl (GdkGLContext *context,
+                        int           id,
+                        int           width,
+                        int           height)
+{
+  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;
+
+  return GDK_TEXTURE (self);
+}
+
 /**
  * gdk_texture_get_width:
  * @texture: a #GdkTexture
diff --git a/gdk/gdktexture.h b/gdk/gdktexture.h
index fe957c0..b95a7fe 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
 
@@ -54,6 +55,11 @@ GdkTexture *            gdk_texture_new_from_resource          (const char
 GDK_AVAILABLE_IN_3_94
 GdkTexture *            gdk_texture_new_from_file              (GFile           *file,
                                                                 GError         **error);
+GDK_AVAILABLE_IN_3_94
+GdkTexture *            gdk_texture_new_for_gl                 (GdkGLContext    *context,
+                                                                int              id,
+                                                                int              width,
+                                                                int              height);
 
 GDK_AVAILABLE_IN_3_94
 int                     gdk_texture_get_width                  (GdkTexture      *texture);


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