2017-11-05 03:48:30 +00:00
|
|
|
/* gdktexture.c
|
2016-11-07 16:59:38 +00:00
|
|
|
*
|
|
|
|
* Copyright 2016 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 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2017-12-04 23:58:30 +00:00
|
|
|
* SECTION:textures
|
2017-11-02 20:39:00 +00:00
|
|
|
* @Title: GdkTexture
|
2017-11-05 03:48:30 +00:00
|
|
|
* @Short_description: Pixel data
|
2016-11-07 16:59:38 +00:00
|
|
|
*
|
2017-11-02 20:39:00 +00:00
|
|
|
* #GdkTexture is the basic element used to refer to pixel data.
|
2017-09-25 01:53:54 +00:00
|
|
|
* It is primarily mean for pixel data that will not change over
|
|
|
|
* multiple frames, and will be used for a long time.
|
2016-11-07 16:59:38 +00:00
|
|
|
*
|
|
|
|
* You cannot get your pixel data back once you've uploaded it.
|
|
|
|
*
|
2017-11-02 20:39:00 +00:00
|
|
|
* #GdkTexture is an immutable object: That means you cannot change
|
2016-11-07 16:59:38 +00:00
|
|
|
* anything about it other than increasing the reference count via
|
2017-01-01 12:40:13 +00:00
|
|
|
* g_object_ref().
|
2016-11-07 16:59:38 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2017-11-02 20:39:00 +00:00
|
|
|
#include "gdktextureprivate.h"
|
2016-11-07 16:59:38 +00:00
|
|
|
|
2017-11-17 05:34:04 +00:00
|
|
|
#include "gdkcairo.h"
|
2018-03-05 13:38:38 +00:00
|
|
|
#include "gdkinternals.h"
|
2018-01-17 05:32:26 +00:00
|
|
|
|
2017-12-04 19:04:02 +00:00
|
|
|
/**
|
|
|
|
* SECTION:gdktexture
|
|
|
|
* @Short_description: Image data for display
|
|
|
|
* @Title: GdkTexture
|
|
|
|
*
|
|
|
|
* A GdkTexture represents image data that can be displayed on screen.
|
|
|
|
*
|
|
|
|
* There are various ways to create GdkTexture objects from a #GdkPixbuf
|
|
|
|
* or a cairo surface, or other pixel data.
|
|
|
|
*
|
|
|
|
* An important aspect of GdkTextures is that they are immutable - once
|
|
|
|
* the image data has been wrapped in a GdkTexture, it may be uploaded
|
|
|
|
* to the GPU or used in other ways that make it impractical to allow
|
|
|
|
* modification.
|
|
|
|
*/
|
|
|
|
|
2016-11-07 16:59:38 +00:00
|
|
|
/**
|
2017-11-02 20:39:00 +00:00
|
|
|
* GdkTexture:
|
2016-11-07 16:59:38 +00:00
|
|
|
*
|
2017-11-02 20:39:00 +00:00
|
|
|
* The `GdkTexture` structure contains only private data.
|
2016-11-07 16:59:38 +00:00
|
|
|
*/
|
|
|
|
|
2017-01-01 12:40:13 +00:00
|
|
|
enum {
|
|
|
|
PROP_0,
|
|
|
|
PROP_WIDTH,
|
|
|
|
PROP_HEIGHT,
|
2016-11-07 16:59:38 +00:00
|
|
|
|
2017-01-01 12:40:13 +00:00
|
|
|
N_PROPS
|
|
|
|
};
|
2016-11-16 04:37:20 +00:00
|
|
|
|
2017-01-01 12:40:13 +00:00
|
|
|
static GParamSpec *properties[N_PROPS];
|
2016-11-07 16:59:38 +00:00
|
|
|
|
2017-11-02 20:39:00 +00:00
|
|
|
G_DEFINE_ABSTRACT_TYPE (GdkTexture, gdk_texture, G_TYPE_OBJECT)
|
2017-01-01 12:40:13 +00:00
|
|
|
|
2017-11-02 20:39:00 +00:00
|
|
|
#define GDK_TEXTURE_WARN_NOT_IMPLEMENTED_METHOD(obj,method) \
|
|
|
|
g_critical ("Texture of type '%s' does not implement GdkTexture::" # method, G_OBJECT_TYPE_NAME (obj))
|
2017-01-01 12:40:13 +00:00
|
|
|
|
|
|
|
static void
|
2017-11-02 20:39:00 +00:00
|
|
|
gdk_texture_real_download (GdkTexture *self,
|
2017-01-01 12:40:13 +00:00
|
|
|
guchar *data,
|
|
|
|
gsize stride)
|
|
|
|
{
|
2017-11-02 20:39:00 +00:00
|
|
|
GDK_TEXTURE_WARN_NOT_IMPLEMENTED_METHOD (self, download);
|
2016-11-07 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2017-01-01 12:40:13 +00:00
|
|
|
static cairo_surface_t *
|
2017-11-02 20:39:00 +00:00
|
|
|
gdk_texture_real_download_surface (GdkTexture *texture)
|
2016-11-07 16:59:38 +00:00
|
|
|
{
|
2017-01-01 12:40:13 +00:00
|
|
|
cairo_surface_t *surface;
|
2016-11-07 16:59:38 +00:00
|
|
|
|
2017-01-01 12:40:13 +00:00
|
|
|
surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
|
|
|
|
texture->width, texture->height);
|
2017-11-02 20:39:00 +00:00
|
|
|
gdk_texture_download (texture,
|
2017-01-01 12:40:13 +00:00
|
|
|
cairo_image_surface_get_data (surface),
|
|
|
|
cairo_image_surface_get_stride (surface));
|
|
|
|
cairo_surface_mark_dirty (surface);
|
2016-11-07 16:59:38 +00:00
|
|
|
|
2017-01-01 12:40:13 +00:00
|
|
|
return surface;
|
2016-11-07 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2017-01-01 12:40:13 +00:00
|
|
|
static void
|
2017-11-02 20:39:00 +00:00
|
|
|
gdk_texture_set_property (GObject *gobject,
|
2017-01-01 12:40:13 +00:00
|
|
|
guint prop_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
2016-11-07 16:59:38 +00:00
|
|
|
{
|
2017-11-02 20:39:00 +00:00
|
|
|
GdkTexture *self = GDK_TEXTURE (gobject);
|
2017-01-01 12:40:13 +00:00
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_WIDTH:
|
|
|
|
self->width = g_value_get_int (value);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_HEIGHT:
|
|
|
|
self->height = g_value_get_int (value);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
2016-11-07 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2017-01-01 12:40:13 +00:00
|
|
|
static void
|
2017-11-02 20:39:00 +00:00
|
|
|
gdk_texture_get_property (GObject *gobject,
|
2017-01-01 12:40:13 +00:00
|
|
|
guint prop_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
2016-11-07 16:59:38 +00:00
|
|
|
{
|
2017-11-02 20:39:00 +00:00
|
|
|
GdkTexture *self = GDK_TEXTURE (gobject);
|
2017-01-01 12:40:13 +00:00
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_WIDTH:
|
|
|
|
g_value_set_int (value, self->width);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_HEIGHT:
|
|
|
|
g_value_set_int (value, self->height);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-11-07 16:59:38 +00:00
|
|
|
|
2017-01-01 12:40:13 +00:00
|
|
|
static void
|
2017-11-02 20:39:00 +00:00
|
|
|
gdk_texture_dispose (GObject *object)
|
2017-09-25 01:53:54 +00:00
|
|
|
{
|
2017-11-02 20:39:00 +00:00
|
|
|
GdkTexture *self = GDK_TEXTURE (object);
|
2016-11-07 16:59:38 +00:00
|
|
|
|
2017-11-02 20:39:00 +00:00
|
|
|
gdk_texture_clear_render_data (self);
|
2016-11-07 16:59:38 +00:00
|
|
|
|
2017-11-02 20:39:00 +00:00
|
|
|
G_OBJECT_CLASS (gdk_texture_parent_class)->dispose (object);
|
2017-01-01 12:40:13 +00:00
|
|
|
}
|
2016-11-07 16:59:38 +00:00
|
|
|
|
2017-01-01 12:40:13 +00:00
|
|
|
static void
|
2017-11-02 20:39:00 +00:00
|
|
|
gdk_texture_class_init (GdkTextureClass *klass)
|
2017-01-01 12:40:13 +00:00
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
2017-11-02 20:39:00 +00:00
|
|
|
klass->download = gdk_texture_real_download;
|
|
|
|
klass->download_surface = gdk_texture_real_download_surface;
|
2017-01-01 12:40:13 +00:00
|
|
|
|
2017-11-02 20:39:00 +00:00
|
|
|
gobject_class->set_property = gdk_texture_set_property;
|
|
|
|
gobject_class->get_property = gdk_texture_get_property;
|
|
|
|
gobject_class->dispose = gdk_texture_dispose;
|
2017-01-01 12:40:13 +00:00
|
|
|
|
|
|
|
/**
|
2017-11-05 03:48:30 +00:00
|
|
|
* GdkTexture:width:
|
2017-01-01 12:40:13 +00:00
|
|
|
*
|
|
|
|
* The width of the texture.
|
|
|
|
*/
|
|
|
|
properties[PROP_WIDTH] =
|
|
|
|
g_param_spec_int ("width",
|
|
|
|
"Width",
|
|
|
|
"The width of the texture",
|
|
|
|
1,
|
|
|
|
G_MAXINT,
|
|
|
|
1,
|
|
|
|
G_PARAM_READWRITE |
|
|
|
|
G_PARAM_CONSTRUCT_ONLY |
|
|
|
|
G_PARAM_STATIC_STRINGS |
|
|
|
|
G_PARAM_EXPLICIT_NOTIFY);
|
|
|
|
|
|
|
|
/**
|
2017-11-05 03:48:30 +00:00
|
|
|
* GdkTexture:height:
|
2017-01-01 12:40:13 +00:00
|
|
|
*
|
|
|
|
* The height of the texture.
|
|
|
|
*/
|
|
|
|
properties[PROP_HEIGHT] =
|
|
|
|
g_param_spec_int ("height",
|
|
|
|
"Height",
|
|
|
|
"The height of the texture",
|
|
|
|
1,
|
|
|
|
G_MAXINT,
|
|
|
|
1,
|
|
|
|
G_PARAM_READWRITE |
|
|
|
|
G_PARAM_CONSTRUCT_ONLY |
|
|
|
|
G_PARAM_STATIC_STRINGS |
|
|
|
|
G_PARAM_EXPLICIT_NOTIFY);
|
|
|
|
|
|
|
|
g_object_class_install_properties (gobject_class, N_PROPS, properties);
|
|
|
|
}
|
2016-11-07 16:59:38 +00:00
|
|
|
|
2017-01-01 12:40:13 +00:00
|
|
|
static void
|
2017-11-02 20:39:00 +00:00
|
|
|
gdk_texture_init (GdkTexture *self)
|
2017-01-01 12:40:13 +00:00
|
|
|
{
|
2016-11-07 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2017-11-02 20:39:00 +00:00
|
|
|
/* GdkCairoTexture */
|
2016-11-16 03:14:32 +00:00
|
|
|
|
2017-11-02 20:39:00 +00:00
|
|
|
#define GDK_TYPE_CAIRO_TEXTURE (gdk_cairo_texture_get_type ())
|
2017-01-01 12:40:13 +00:00
|
|
|
|
2017-11-02 20:39:00 +00:00
|
|
|
G_DECLARE_FINAL_TYPE (GdkCairoTexture, gdk_cairo_texture, GDK, CAIRO_TEXTURE, GdkTexture)
|
2016-11-16 03:14:32 +00:00
|
|
|
|
2017-11-02 20:39:00 +00:00
|
|
|
struct _GdkCairoTexture {
|
|
|
|
GdkTexture parent_instance;
|
2016-11-16 03:14:32 +00:00
|
|
|
cairo_surface_t *surface;
|
|
|
|
};
|
|
|
|
|
2017-11-02 20:39:00 +00:00
|
|
|
struct _GdkCairoTextureClass {
|
|
|
|
GdkTextureClass parent_class;
|
2017-01-01 12:40:13 +00:00
|
|
|
};
|
|
|
|
|
2017-11-02 20:39:00 +00:00
|
|
|
G_DEFINE_TYPE (GdkCairoTexture, gdk_cairo_texture, GDK_TYPE_TEXTURE)
|
2017-01-01 12:40:13 +00:00
|
|
|
|
2016-11-16 03:14:32 +00:00
|
|
|
static void
|
2017-11-02 20:39:00 +00:00
|
|
|
gdk_cairo_texture_finalize (GObject *object)
|
2016-11-16 03:14:32 +00:00
|
|
|
{
|
2017-11-02 20:39:00 +00:00
|
|
|
GdkCairoTexture *self = GDK_CAIRO_TEXTURE (object);
|
2017-01-01 12:40:13 +00:00
|
|
|
|
|
|
|
cairo_surface_destroy (self->surface);
|
2016-11-16 03:14:32 +00:00
|
|
|
|
2017-11-02 20:39:00 +00:00
|
|
|
G_OBJECT_CLASS (gdk_cairo_texture_parent_class)->finalize (object);
|
2016-11-16 03:14:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static cairo_surface_t *
|
2017-11-02 20:39:00 +00:00
|
|
|
gdk_cairo_texture_download_surface (GdkTexture *texture)
|
2016-11-16 03:14:32 +00:00
|
|
|
{
|
2017-11-02 20:39:00 +00:00
|
|
|
GdkCairoTexture *self = GDK_CAIRO_TEXTURE (texture);
|
2016-11-16 03:14:32 +00:00
|
|
|
|
2017-01-01 12:40:13 +00:00
|
|
|
return cairo_surface_reference (self->surface);
|
2016-11-16 03:14:32 +00:00
|
|
|
}
|
|
|
|
|
2016-12-21 21:11:52 +00:00
|
|
|
static void
|
2017-11-02 20:39:00 +00:00
|
|
|
gdk_cairo_texture_download (GdkTexture *texture,
|
2016-12-21 21:11:52 +00:00
|
|
|
guchar *data,
|
|
|
|
gsize stride)
|
|
|
|
{
|
2017-11-02 20:39:00 +00:00
|
|
|
GdkCairoTexture *self = GDK_CAIRO_TEXTURE (texture);
|
2016-12-21 21:11:52 +00:00
|
|
|
cairo_surface_t *surface;
|
|
|
|
cairo_t *cr;
|
|
|
|
|
|
|
|
surface = cairo_image_surface_create_for_data (data,
|
|
|
|
CAIRO_FORMAT_ARGB32,
|
|
|
|
texture->width, texture->height,
|
|
|
|
stride);
|
|
|
|
cr = cairo_create (surface);
|
|
|
|
|
2017-01-01 12:40:13 +00:00
|
|
|
cairo_set_source_surface (cr, self->surface, 0, 0);
|
2016-12-21 21:11:52 +00:00
|
|
|
cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
|
|
|
|
cairo_paint (cr);
|
|
|
|
|
|
|
|
cairo_destroy (cr);
|
|
|
|
cairo_surface_finish (surface);
|
|
|
|
cairo_surface_destroy (surface);
|
|
|
|
}
|
|
|
|
|
2017-01-01 12:40:13 +00:00
|
|
|
static void
|
2017-11-02 20:39:00 +00:00
|
|
|
gdk_cairo_texture_class_init (GdkCairoTextureClass *klass)
|
2016-11-16 03:14:32 +00:00
|
|
|
{
|
2017-11-02 20:39:00 +00:00
|
|
|
GdkTextureClass *texture_class = GDK_TEXTURE_CLASS (klass);
|
2017-01-01 12:40:13 +00:00
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
2016-11-16 03:14:32 +00:00
|
|
|
|
2017-11-02 20:39:00 +00:00
|
|
|
texture_class->download = gdk_cairo_texture_download;
|
|
|
|
texture_class->download_surface = gdk_cairo_texture_download_surface;
|
2016-11-16 03:14:32 +00:00
|
|
|
|
2017-11-02 20:39:00 +00:00
|
|
|
gobject_class->finalize = gdk_cairo_texture_finalize;
|
2017-01-01 12:40:13 +00:00
|
|
|
}
|
2016-11-16 03:14:32 +00:00
|
|
|
|
2017-01-01 12:40:13 +00:00
|
|
|
static void
|
2017-11-02 20:39:00 +00:00
|
|
|
gdk_cairo_texture_init (GdkCairoTexture *self)
|
2017-01-01 12:40:13 +00:00
|
|
|
{
|
2016-11-16 03:14:32 +00:00
|
|
|
}
|
|
|
|
|
2017-09-25 01:53:54 +00:00
|
|
|
/**
|
2017-11-02 20:39:00 +00:00
|
|
|
* gdk_texture_new_for_data:
|
2017-09-25 09:15:14 +00:00
|
|
|
* @data: (array): the pixel data
|
2017-09-25 01:53:54 +00:00
|
|
|
* @width: the number of pixels in each row
|
|
|
|
* @height: the number of rows
|
|
|
|
* @stride: the distance from the beginning of one row to the next, in bytes
|
|
|
|
*
|
|
|
|
* Creates a new texture object holding the given data.
|
|
|
|
* The data is assumed to be in CAIRO_FORMAT_ARGB32 format.
|
|
|
|
*
|
2017-11-02 20:39:00 +00:00
|
|
|
* Returns: a new #GdkTexture
|
2017-09-25 01:53:54 +00:00
|
|
|
*/
|
2017-11-02 20:39:00 +00:00
|
|
|
GdkTexture *
|
|
|
|
gdk_texture_new_for_data (const guchar *data,
|
2016-11-07 16:59:38 +00:00
|
|
|
int width,
|
|
|
|
int height,
|
|
|
|
int stride)
|
|
|
|
{
|
2017-11-02 20:39:00 +00:00
|
|
|
GdkTexture *texture;
|
2016-11-16 03:14:32 +00:00
|
|
|
cairo_surface_t *original, *copy;
|
|
|
|
cairo_t *cr;
|
|
|
|
|
2018-02-02 13:58:39 +00:00
|
|
|
g_return_val_if_fail (width > 0, NULL);
|
|
|
|
g_return_val_if_fail (height > 0, NULL);
|
|
|
|
|
2016-11-16 03:14:32 +00:00
|
|
|
original = cairo_image_surface_create_for_data ((guchar *) data, CAIRO_FORMAT_ARGB32, width, height, stride);
|
|
|
|
copy = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
|
|
|
|
|
|
|
|
cr = cairo_create (copy);
|
|
|
|
cairo_set_source_surface (cr, original, 0, 0);
|
|
|
|
cairo_paint (cr);
|
|
|
|
cairo_destroy (cr);
|
|
|
|
|
2017-11-02 20:39:00 +00:00
|
|
|
texture = gdk_texture_new_for_surface (copy);
|
2016-11-16 03:14:32 +00:00
|
|
|
|
|
|
|
cairo_surface_destroy (copy);
|
2017-01-01 12:40:13 +00:00
|
|
|
cairo_surface_finish (original);
|
2016-11-16 03:14:32 +00:00
|
|
|
cairo_surface_destroy (original);
|
|
|
|
|
|
|
|
return texture;
|
2016-11-07 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2017-09-25 01:53:54 +00:00
|
|
|
/**
|
2017-11-02 20:39:00 +00:00
|
|
|
* gdk_texture_new_for_surface:
|
2017-09-25 01:53:54 +00:00
|
|
|
* @surface: a cairo image surface
|
|
|
|
*
|
|
|
|
* Creates a new texture object representing the surface.
|
|
|
|
* @surface must be an image surface with format CAIRO_FORMAT_ARGB32.
|
|
|
|
*
|
2017-11-02 20:39:00 +00:00
|
|
|
* Returns: a new #GdkTexture
|
2017-09-25 01:53:54 +00:00
|
|
|
*/
|
2017-11-02 20:39:00 +00:00
|
|
|
GdkTexture *
|
|
|
|
gdk_texture_new_for_surface (cairo_surface_t *surface)
|
2017-01-01 12:40:13 +00:00
|
|
|
{
|
2017-11-02 20:39:00 +00:00
|
|
|
GdkCairoTexture *texture;
|
2017-01-01 12:40:13 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_IMAGE, NULL);
|
2018-02-02 13:58:39 +00:00
|
|
|
g_return_val_if_fail (cairo_image_surface_get_width (surface) > 0, NULL);
|
|
|
|
g_return_val_if_fail (cairo_image_surface_get_height (surface) > 0, NULL);
|
2017-01-01 12:40:13 +00:00
|
|
|
|
2017-11-02 20:39:00 +00:00
|
|
|
texture = g_object_new (GDK_TYPE_CAIRO_TEXTURE,
|
2017-01-01 12:40:13 +00:00
|
|
|
"width", cairo_image_surface_get_width (surface),
|
|
|
|
"height", cairo_image_surface_get_height (surface),
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
texture->surface = cairo_surface_reference (surface);
|
|
|
|
|
2017-11-02 20:39:00 +00:00
|
|
|
return (GdkTexture *) texture;
|
2017-01-01 12:40:13 +00:00
|
|
|
}
|
|
|
|
|
2017-11-02 20:39:00 +00:00
|
|
|
/* GdkPixbufTexture */
|
2016-11-16 03:14:32 +00:00
|
|
|
|
2017-11-02 20:39:00 +00:00
|
|
|
#define GDK_TYPE_PIXBUF_TEXTURE (gdk_pixbuf_texture_get_type ())
|
2017-01-01 12:40:13 +00:00
|
|
|
|
2017-11-02 20:39:00 +00:00
|
|
|
G_DECLARE_FINAL_TYPE (GdkPixbufTexture, gdk_pixbuf_texture, GDK, PIXBUF_TEXTURE, GdkTexture)
|
2016-11-16 03:14:32 +00:00
|
|
|
|
2017-11-02 20:39:00 +00:00
|
|
|
struct _GdkPixbufTexture {
|
|
|
|
GdkTexture parent_instance;
|
2017-01-01 12:40:13 +00:00
|
|
|
|
2016-11-16 03:14:32 +00:00
|
|
|
GdkPixbuf *pixbuf;
|
|
|
|
};
|
|
|
|
|
2017-11-02 20:39:00 +00:00
|
|
|
struct _GdkPixbufTextureClass {
|
|
|
|
GdkTextureClass parent_class;
|
2017-01-01 12:40:13 +00:00
|
|
|
};
|
|
|
|
|
2017-11-02 20:39:00 +00:00
|
|
|
G_DEFINE_TYPE (GdkPixbufTexture, gdk_pixbuf_texture, GDK_TYPE_TEXTURE)
|
2017-01-01 12:40:13 +00:00
|
|
|
|
2016-11-16 03:14:32 +00:00
|
|
|
static void
|
2017-11-02 20:39:00 +00:00
|
|
|
gdk_pixbuf_texture_finalize (GObject *object)
|
2016-11-07 16:59:38 +00:00
|
|
|
{
|
2017-11-02 20:39:00 +00:00
|
|
|
GdkPixbufTexture *self = GDK_PIXBUF_TEXTURE (object);
|
2016-11-07 16:59:38 +00:00
|
|
|
|
2017-01-01 12:40:13 +00:00
|
|
|
g_object_unref (self->pixbuf);
|
|
|
|
|
2017-11-02 20:39:00 +00:00
|
|
|
G_OBJECT_CLASS (gdk_pixbuf_texture_parent_class)->finalize (object);
|
2016-11-07 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2016-12-21 21:11:52 +00:00
|
|
|
static void
|
2017-11-02 20:39:00 +00:00
|
|
|
gdk_pixbuf_texture_download (GdkTexture *texture,
|
2016-12-21 21:11:52 +00:00
|
|
|
guchar *data,
|
|
|
|
gsize stride)
|
|
|
|
{
|
2017-11-02 20:39:00 +00:00
|
|
|
GdkPixbufTexture *self = GDK_PIXBUF_TEXTURE (texture);
|
2016-12-21 21:11:52 +00:00
|
|
|
cairo_surface_t *surface;
|
|
|
|
|
|
|
|
surface = cairo_image_surface_create_for_data (data,
|
|
|
|
CAIRO_FORMAT_ARGB32,
|
|
|
|
texture->width, texture->height,
|
|
|
|
stride);
|
2017-01-01 12:40:13 +00:00
|
|
|
gdk_cairo_surface_paint_pixbuf (surface, self->pixbuf);
|
2016-12-21 21:11:52 +00:00
|
|
|
cairo_surface_finish (surface);
|
|
|
|
cairo_surface_destroy (surface);
|
|
|
|
}
|
|
|
|
|
2016-11-16 03:14:32 +00:00
|
|
|
static cairo_surface_t *
|
2017-11-02 20:39:00 +00:00
|
|
|
gdk_pixbuf_texture_download_surface (GdkTexture *texture)
|
2016-11-07 16:59:38 +00:00
|
|
|
{
|
2017-11-02 20:39:00 +00:00
|
|
|
GdkPixbufTexture *self = GDK_PIXBUF_TEXTURE (texture);
|
2016-11-16 03:14:32 +00:00
|
|
|
|
2017-01-01 12:40:13 +00:00
|
|
|
return gdk_cairo_surface_create_from_pixbuf (self->pixbuf, 1, NULL);
|
2016-11-16 03:14:32 +00:00
|
|
|
}
|
|
|
|
|
2017-01-01 12:40:13 +00:00
|
|
|
static void
|
2017-11-02 20:39:00 +00:00
|
|
|
gdk_pixbuf_texture_class_init (GdkPixbufTextureClass *klass)
|
2017-01-01 12:40:13 +00:00
|
|
|
{
|
2017-11-02 20:39:00 +00:00
|
|
|
GdkTextureClass *texture_class = GDK_TEXTURE_CLASS (klass);
|
2017-01-01 12:40:13 +00:00
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
2017-11-02 20:39:00 +00:00
|
|
|
texture_class->download = gdk_pixbuf_texture_download;
|
|
|
|
texture_class->download_surface = gdk_pixbuf_texture_download_surface;
|
2017-01-01 12:40:13 +00:00
|
|
|
|
2017-11-02 20:39:00 +00:00
|
|
|
gobject_class->finalize = gdk_pixbuf_texture_finalize;
|
2017-01-01 12:40:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-11-02 20:39:00 +00:00
|
|
|
gdk_pixbuf_texture_init (GdkPixbufTexture *self)
|
2017-01-01 12:40:13 +00:00
|
|
|
{
|
|
|
|
}
|
2016-11-16 03:14:32 +00:00
|
|
|
|
2017-09-25 01:53:54 +00:00
|
|
|
/**
|
2017-11-02 20:39:00 +00:00
|
|
|
* gdk_texture_new_for_pixbuf:
|
2017-09-25 01:53:54 +00:00
|
|
|
* @pixbuf: a #GdkPixbuf
|
|
|
|
*
|
|
|
|
* Creates a new texture object representing the GdkPixbuf.
|
|
|
|
*
|
2017-11-02 20:39:00 +00:00
|
|
|
* Returns: a new #GdkTexture
|
2017-09-25 01:53:54 +00:00
|
|
|
*/
|
2017-11-02 20:39:00 +00:00
|
|
|
GdkTexture *
|
|
|
|
gdk_texture_new_for_pixbuf (GdkPixbuf *pixbuf)
|
2016-11-16 03:14:32 +00:00
|
|
|
{
|
2017-11-02 20:39:00 +00:00
|
|
|
GdkPixbufTexture *self;
|
2016-11-07 16:59:38 +00:00
|
|
|
|
2016-11-16 03:14:32 +00:00
|
|
|
g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
|
|
|
|
|
2017-11-02 20:39:00 +00:00
|
|
|
self = g_object_new (GDK_TYPE_PIXBUF_TEXTURE,
|
2017-01-01 12:40:13 +00:00
|
|
|
"width", gdk_pixbuf_get_width (pixbuf),
|
|
|
|
"height", gdk_pixbuf_get_height (pixbuf),
|
|
|
|
NULL);
|
2016-11-16 03:14:32 +00:00
|
|
|
|
2017-01-01 12:40:13 +00:00
|
|
|
self->pixbuf = g_object_ref (pixbuf);
|
2016-11-16 03:14:32 +00:00
|
|
|
|
2017-11-02 20:39:00 +00:00
|
|
|
return GDK_TEXTURE (self);
|
2016-11-07 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2017-11-04 14:08:25 +00:00
|
|
|
/**
|
|
|
|
* gdk_texture_new_from_resource:
|
|
|
|
* @resource_path: the path of the resource file
|
|
|
|
*
|
|
|
|
* Creates a new texture by loading an image from a resource.
|
|
|
|
* The file format is detected automatically.
|
|
|
|
*
|
|
|
|
* It is a fatal error if @resource_path does not specify a valid
|
|
|
|
* image resource and the program will abort if that happens.
|
|
|
|
* If you are unsure about the validity of a resource, use
|
|
|
|
* gdk_texture_new_from_file() to load it.
|
|
|
|
*
|
|
|
|
* Return value: A newly-created texture
|
|
|
|
*/
|
|
|
|
GdkTexture *
|
|
|
|
gdk_texture_new_from_resource (const char *resource_path)
|
|
|
|
{
|
|
|
|
GError *error = NULL;
|
|
|
|
GdkTexture *texture;
|
|
|
|
GdkPixbuf *pixbuf;
|
|
|
|
|
|
|
|
g_return_val_if_fail (resource_path != NULL, NULL);
|
|
|
|
|
|
|
|
pixbuf = gdk_pixbuf_new_from_resource (resource_path, &error);
|
|
|
|
if (pixbuf == NULL)
|
|
|
|
g_error ("Resource path %s is not a valid image: %s", resource_path, error->message);
|
|
|
|
|
|
|
|
texture = gdk_texture_new_for_pixbuf (pixbuf);
|
|
|
|
g_object_unref (pixbuf);
|
|
|
|
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-11-05 03:48:30 +00:00
|
|
|
* gdk_texture_new_from_file:
|
2017-11-04 14:08:25 +00:00
|
|
|
* @file: #GFile to load
|
|
|
|
* @error: Return location for an error
|
|
|
|
*
|
|
|
|
* Creates a new texture by loading an image from a file. The file format is
|
|
|
|
* detected automatically. If %NULL is returned, then @error will be set.
|
|
|
|
*
|
|
|
|
* Return value: A newly-created #GdkTexture or %NULL if an error occured.
|
|
|
|
**/
|
|
|
|
GdkTexture *
|
|
|
|
gdk_texture_new_from_file (GFile *file,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
GdkTexture *texture;
|
|
|
|
GdkPixbuf *pixbuf;
|
|
|
|
GInputStream *stream;
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_FILE (file), NULL);
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
|
|
|
|
|
|
|
|
stream = G_INPUT_STREAM (g_file_read (file, NULL, error));
|
|
|
|
if (stream == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
pixbuf = gdk_pixbuf_new_from_stream (stream, NULL, error);
|
|
|
|
g_object_unref (stream);
|
|
|
|
if (pixbuf == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
texture = gdk_texture_new_for_pixbuf (pixbuf);
|
|
|
|
g_object_unref (pixbuf);
|
|
|
|
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
2016-11-07 16:59:38 +00:00
|
|
|
/**
|
2017-11-02 20:39:00 +00:00
|
|
|
* gdk_texture_get_width:
|
|
|
|
* @texture: a #GdkTexture
|
2016-11-07 16:59:38 +00:00
|
|
|
*
|
|
|
|
* Returns the width of @texture.
|
|
|
|
*
|
2017-11-02 20:39:00 +00:00
|
|
|
* Returns: the width of the #GdkTexture
|
2016-11-07 16:59:38 +00:00
|
|
|
*/
|
|
|
|
int
|
2017-11-02 20:39:00 +00:00
|
|
|
gdk_texture_get_width (GdkTexture *texture)
|
2016-11-07 16:59:38 +00:00
|
|
|
{
|
2017-11-02 20:39:00 +00:00
|
|
|
g_return_val_if_fail (GDK_IS_TEXTURE (texture), 0);
|
2016-11-07 16:59:38 +00:00
|
|
|
|
|
|
|
return texture->width;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-11-02 20:39:00 +00:00
|
|
|
* gdk_texture_get_height:
|
|
|
|
* @texture: a #GdkTexture
|
2016-11-07 16:59:38 +00:00
|
|
|
*
|
|
|
|
* Returns the height of the @texture.
|
|
|
|
*
|
2017-11-02 20:39:00 +00:00
|
|
|
* Returns: the height of the #GdkTexture
|
2016-11-07 16:59:38 +00:00
|
|
|
*/
|
|
|
|
int
|
2017-11-02 20:39:00 +00:00
|
|
|
gdk_texture_get_height (GdkTexture *texture)
|
2016-11-07 16:59:38 +00:00
|
|
|
{
|
2017-11-02 20:39:00 +00:00
|
|
|
g_return_val_if_fail (GDK_IS_TEXTURE (texture), 0);
|
2016-11-07 16:59:38 +00:00
|
|
|
|
|
|
|
return texture->height;
|
|
|
|
}
|
|
|
|
|
2016-11-16 03:14:32 +00:00
|
|
|
cairo_surface_t *
|
2017-11-02 20:39:00 +00:00
|
|
|
gdk_texture_download_surface (GdkTexture *texture)
|
2016-11-16 03:14:32 +00:00
|
|
|
{
|
2017-11-02 20:39:00 +00:00
|
|
|
return GDK_TEXTURE_GET_CLASS (texture)->download_surface (texture);
|
2016-12-21 21:11:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-11-02 20:39:00 +00:00
|
|
|
* gdk_texture_download:
|
|
|
|
* @texture: a #GdkTexture
|
2018-01-02 17:55:55 +00:00
|
|
|
* @data: (array): pointer to enough memory to be filled with the
|
2016-12-21 21:11:52 +00:00
|
|
|
* downloaded data of @texture
|
|
|
|
* @stride: rowstride in bytes
|
|
|
|
*
|
|
|
|
* Downloads the @texture into local memory. This may be
|
|
|
|
* an expensive operation, as the actual texture data may
|
|
|
|
* reside on a GPU or on a remote display server.
|
|
|
|
*
|
|
|
|
* The data format of the downloaded data is equivalent to
|
|
|
|
* %CAIRO_FORMAT_ARGB32, so every downloaded pixel requires
|
|
|
|
* 4 bytes of memory.
|
|
|
|
*
|
|
|
|
* Downloading a texture into a Cairo image surface:
|
|
|
|
* |[<!-- language="C" -->
|
|
|
|
* surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
|
2017-11-02 20:39:00 +00:00
|
|
|
* gdk_texture_get_width (texture),
|
|
|
|
* gdk_texture_get_height (texture));
|
|
|
|
* gdk_texture_download (texture,
|
2016-12-21 21:11:52 +00:00
|
|
|
* cairo_image_surface_get_data (surface),
|
|
|
|
* cairo_image_surface_get_stride (surface));
|
|
|
|
* cairo_surface_mark_dirty (surface);
|
|
|
|
* ]|
|
|
|
|
**/
|
|
|
|
void
|
2017-11-02 20:39:00 +00:00
|
|
|
gdk_texture_download (GdkTexture *texture,
|
2016-12-21 21:11:52 +00:00
|
|
|
guchar *data,
|
|
|
|
gsize stride)
|
|
|
|
{
|
2017-11-02 20:39:00 +00:00
|
|
|
g_return_if_fail (GDK_IS_TEXTURE (texture));
|
2016-12-21 21:11:52 +00:00
|
|
|
g_return_if_fail (data != NULL);
|
2017-11-02 20:39:00 +00:00
|
|
|
g_return_if_fail (stride >= gdk_texture_get_width (texture) * 4);
|
2016-12-21 21:11:52 +00:00
|
|
|
|
2017-11-02 20:39:00 +00:00
|
|
|
return GDK_TEXTURE_GET_CLASS (texture)->download (texture, data, stride);
|
2016-11-16 03:14:32 +00:00
|
|
|
}
|
|
|
|
|
2016-11-16 04:37:20 +00:00
|
|
|
gboolean
|
2017-11-02 20:39:00 +00:00
|
|
|
gdk_texture_set_render_data (GdkTexture *self,
|
2016-11-16 04:37:20 +00:00
|
|
|
gpointer key,
|
|
|
|
gpointer data,
|
|
|
|
GDestroyNotify notify)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (data != NULL, FALSE);
|
|
|
|
|
|
|
|
if (self->render_key != NULL)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
self->render_key = key;
|
|
|
|
self->render_data = data;
|
|
|
|
self->render_notify = notify;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-11-02 20:39:00 +00:00
|
|
|
gdk_texture_clear_render_data (GdkTexture *self)
|
2016-11-16 04:37:20 +00:00
|
|
|
{
|
|
|
|
if (self->render_notify)
|
|
|
|
self->render_notify (self->render_data);
|
|
|
|
|
|
|
|
self->render_key = NULL;
|
|
|
|
self->render_data = NULL;
|
|
|
|
self->render_notify = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
gpointer
|
2017-11-02 20:39:00 +00:00
|
|
|
gdk_texture_get_render_data (GdkTexture *self,
|
2016-11-16 04:37:20 +00:00
|
|
|
gpointer key)
|
|
|
|
{
|
|
|
|
if (self->render_key != key)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return self->render_data;
|
|
|
|
}
|