2018-03-05 13:38:38 +00:00
|
|
|
/* gdkgltexture.c
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "gdkgltextureprivate.h"
|
|
|
|
|
2021-09-12 22:19:35 +00:00
|
|
|
#include "gdkdisplayprivate.h"
|
2021-12-25 13:46:07 +00:00
|
|
|
#include "gdkglcontextprivate.h"
|
2021-09-22 00:01:41 +00:00
|
|
|
#include "gdkmemoryformatprivate.h"
|
2021-09-12 02:42:24 +00:00
|
|
|
#include "gdkmemorytextureprivate.h"
|
2018-03-05 13:38:38 +00:00
|
|
|
|
|
|
|
#include <epoxy/gl.h>
|
|
|
|
|
2021-02-21 05:13:57 +00:00
|
|
|
/**
|
|
|
|
* GdkGLTexture:
|
|
|
|
*
|
|
|
|
* A GdkTexture representing a GL texture object.
|
|
|
|
*/
|
|
|
|
|
2018-03-05 13:38:38 +00:00
|
|
|
struct _GdkGLTexture {
|
|
|
|
GdkTexture parent_instance;
|
|
|
|
|
|
|
|
GdkGLContext *context;
|
|
|
|
guint id;
|
2023-03-23 12:44:26 +00:00
|
|
|
gboolean has_mipmap;
|
2023-04-25 09:03:51 +00:00
|
|
|
gpointer sync;
|
2018-03-05 13:38:38 +00:00
|
|
|
|
2021-09-11 22:09:11 +00:00
|
|
|
GdkTexture *saved;
|
2018-03-05 13:38:38 +00:00
|
|
|
|
|
|
|
GDestroyNotify destroy;
|
|
|
|
gpointer data;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _GdkGLTextureClass {
|
|
|
|
GdkTextureClass parent_class;
|
|
|
|
};
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (GdkGLTexture, gdk_gl_texture, GDK_TYPE_TEXTURE)
|
|
|
|
|
|
|
|
static void
|
2023-01-30 13:24:02 +00:00
|
|
|
drop_gl_resources (GdkGLTexture *self)
|
2018-03-05 13:38:38 +00:00
|
|
|
{
|
|
|
|
if (self->destroy)
|
|
|
|
{
|
|
|
|
self->destroy (self->data);
|
|
|
|
self->destroy = NULL;
|
|
|
|
self->data = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_clear_object (&self->context);
|
|
|
|
self->id = 0;
|
2023-01-30 13:24:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gdk_gl_texture_dispose (GObject *object)
|
|
|
|
{
|
|
|
|
GdkGLTexture *self = GDK_GL_TEXTURE (object);
|
|
|
|
|
|
|
|
drop_gl_resources (self);
|
2018-03-05 13:38:38 +00:00
|
|
|
|
2021-09-11 22:09:11 +00:00
|
|
|
g_clear_object (&self->saved);
|
2018-03-05 13:38:38 +00:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (gdk_gl_texture_parent_class)->dispose (object);
|
|
|
|
}
|
|
|
|
|
2023-01-31 12:23:59 +00:00
|
|
|
typedef void (* GLFunc) (GdkGLTexture *self,
|
2023-02-01 17:38:22 +00:00
|
|
|
GdkGLContext *context,
|
|
|
|
gpointer data);
|
2023-01-31 12:23:59 +00:00
|
|
|
|
2021-09-12 22:19:35 +00:00
|
|
|
typedef struct _InvokeData
|
2021-09-12 02:42:24 +00:00
|
|
|
{
|
2021-09-12 22:19:35 +00:00
|
|
|
GdkGLTexture *self;
|
|
|
|
volatile int spinlock;
|
2023-01-31 12:23:59 +00:00
|
|
|
GLFunc func;
|
2021-09-12 22:19:35 +00:00
|
|
|
gpointer data;
|
|
|
|
} InvokeData;
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gdk_gl_texture_invoke_callback (gpointer data)
|
|
|
|
{
|
|
|
|
InvokeData *invoke = data;
|
|
|
|
GdkGLContext *context;
|
|
|
|
|
|
|
|
context = gdk_display_get_gl_context (gdk_gl_context_get_display (invoke->self->context));
|
|
|
|
|
|
|
|
gdk_gl_context_make_current (context);
|
2023-01-30 13:35:43 +00:00
|
|
|
|
|
|
|
if (invoke->self->sync && context != invoke->self->context)
|
|
|
|
glWaitSync (invoke->self->sync, 0, GL_TIMEOUT_IGNORED);
|
|
|
|
|
2021-09-12 22:19:35 +00:00
|
|
|
glBindTexture (GL_TEXTURE_2D, invoke->self->id);
|
|
|
|
|
2023-02-01 17:38:22 +00:00
|
|
|
invoke->func (invoke->self, context, invoke->data);
|
2021-09-12 22:19:35 +00:00
|
|
|
|
|
|
|
g_atomic_int_set (&invoke->spinlock, 1);
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gdk_gl_texture_run (GdkGLTexture *self,
|
2023-01-31 12:23:59 +00:00
|
|
|
GLFunc func,
|
2021-09-12 22:19:35 +00:00
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
InvokeData invoke = { self, 0, func, data };
|
|
|
|
|
|
|
|
g_main_context_invoke (NULL, gdk_gl_texture_invoke_callback, &invoke);
|
|
|
|
|
|
|
|
while (g_atomic_int_get (&invoke.spinlock) == 0);
|
|
|
|
}
|
|
|
|
|
2021-10-07 04:19:41 +00:00
|
|
|
typedef struct _Download Download;
|
|
|
|
|
|
|
|
struct _Download
|
|
|
|
{
|
2021-10-11 01:29:11 +00:00
|
|
|
GdkMemoryFormat format;
|
2021-10-07 04:19:41 +00:00
|
|
|
guchar *data;
|
|
|
|
gsize stride;
|
|
|
|
};
|
|
|
|
|
2021-10-11 01:29:11 +00:00
|
|
|
static gboolean
|
|
|
|
gdk_gl_texture_find_format (gboolean use_es,
|
2023-04-17 03:42:19 +00:00
|
|
|
guint gl_major,
|
|
|
|
guint gl_minor,
|
2023-05-29 16:51:22 +00:00
|
|
|
GdkMemoryAlpha alpha,
|
2021-10-11 01:29:11 +00:00
|
|
|
GLint gl_format,
|
|
|
|
GLint gl_type,
|
|
|
|
GdkMemoryFormat *out_format)
|
|
|
|
{
|
|
|
|
GdkMemoryFormat format;
|
|
|
|
|
|
|
|
for (format = 0; format < GDK_MEMORY_N_FORMATS; format++)
|
|
|
|
{
|
|
|
|
GLenum q_internal_format, q_format, q_type;
|
2023-05-26 12:45:05 +00:00
|
|
|
GLint q_swizzle[4];
|
2021-10-11 01:29:11 +00:00
|
|
|
|
2023-05-29 16:51:22 +00:00
|
|
|
if (gdk_memory_format_alpha (format) != alpha)
|
2023-05-30 04:08:55 +00:00
|
|
|
continue;
|
|
|
|
|
2023-05-26 12:45:05 +00:00
|
|
|
if (!gdk_memory_format_gl_format (format, use_es, gl_major, gl_minor, &q_internal_format, &q_format, &q_type, &q_swizzle))
|
2021-10-11 01:29:11 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (q_format != gl_format || q_type != gl_type)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
*out_format = format;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2021-09-16 21:14:59 +00:00
|
|
|
static inline void
|
2023-01-31 12:23:59 +00:00
|
|
|
gdk_gl_texture_do_download (GdkGLTexture *self,
|
2023-02-01 17:38:22 +00:00
|
|
|
GdkGLContext *context,
|
|
|
|
gpointer download_)
|
2021-09-16 21:14:59 +00:00
|
|
|
{
|
2023-01-31 12:23:59 +00:00
|
|
|
GdkTexture *texture = GDK_TEXTURE (self);
|
2023-05-29 16:51:22 +00:00
|
|
|
GdkMemoryFormat format;
|
2021-10-11 01:29:11 +00:00
|
|
|
gsize expected_stride;
|
2021-10-07 04:19:41 +00:00
|
|
|
Download *download = download_;
|
2021-10-11 01:29:11 +00:00
|
|
|
GLenum gl_internal_format, gl_format, gl_type;
|
2023-05-26 12:45:05 +00:00
|
|
|
GLint gl_swizzle[4];
|
2023-04-17 03:42:19 +00:00
|
|
|
int major, minor;
|
2021-10-07 04:19:41 +00:00
|
|
|
|
2023-05-29 16:51:22 +00:00
|
|
|
format = gdk_texture_get_format (texture),
|
2021-10-11 01:29:11 +00:00
|
|
|
expected_stride = texture->width * gdk_memory_format_bytes_per_pixel (download->format);
|
2023-04-17 03:42:19 +00:00
|
|
|
gdk_gl_context_get_version (context, &major, &minor);
|
2021-10-11 01:29:11 +00:00
|
|
|
|
2023-05-29 16:51:22 +00:00
|
|
|
if (!gdk_gl_context_get_use_es (context) &&
|
|
|
|
gdk_memory_format_gl_format (format,
|
|
|
|
FALSE,
|
|
|
|
major, minor,
|
|
|
|
&gl_internal_format,
|
|
|
|
&gl_format, &gl_type, &gl_swizzle))
|
2021-10-11 01:29:11 +00:00
|
|
|
{
|
2023-05-29 16:51:22 +00:00
|
|
|
if (download->stride == expected_stride &&
|
|
|
|
download->format == format)
|
|
|
|
{
|
|
|
|
glGetTexImage (GL_TEXTURE_2D,
|
|
|
|
0,
|
|
|
|
gl_format,
|
|
|
|
gl_type,
|
|
|
|
download->data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gsize stride = texture->width * gdk_memory_format_bytes_per_pixel (format);
|
|
|
|
guchar *pixels = g_malloc_n (stride, texture->height);
|
|
|
|
|
2023-05-31 02:35:45 +00:00
|
|
|
glPixelStorei (GL_PACK_ALIGNMENT, 1);
|
2023-05-29 16:51:22 +00:00
|
|
|
glGetTexImage (GL_TEXTURE_2D,
|
|
|
|
0,
|
|
|
|
gl_format,
|
|
|
|
gl_type,
|
|
|
|
pixels);
|
|
|
|
|
|
|
|
gdk_memory_convert (download->data,
|
|
|
|
download->stride,
|
|
|
|
download->format,
|
|
|
|
pixels,
|
|
|
|
stride,
|
|
|
|
format,
|
|
|
|
texture->width,
|
|
|
|
texture->height);
|
|
|
|
|
|
|
|
g_free (pixels);
|
|
|
|
|
|
|
|
}
|
2021-10-11 01:29:11 +00:00
|
|
|
}
|
|
|
|
else
|
2021-09-16 21:14:59 +00:00
|
|
|
{
|
2021-10-11 01:29:11 +00:00
|
|
|
GdkMemoryFormat actual_format;
|
|
|
|
GLint gl_read_format, gl_read_type;
|
2021-09-16 21:14:59 +00:00
|
|
|
GLuint fbo;
|
|
|
|
|
|
|
|
glGenFramebuffers (1, &fbo);
|
|
|
|
glBindFramebuffer (GL_FRAMEBUFFER, fbo);
|
|
|
|
glFramebufferTexture2D (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, self->id, 0);
|
2023-04-25 17:32:12 +00:00
|
|
|
if (gdk_gl_context_check_version (context, "4.3", "3.1"))
|
2022-02-08 00:32:44 +00:00
|
|
|
{
|
|
|
|
glGetFramebufferParameteriv (GL_FRAMEBUFFER, GL_IMPLEMENTATION_COLOR_READ_FORMAT, &gl_read_format);
|
|
|
|
glGetFramebufferParameteriv (GL_FRAMEBUFFER, GL_IMPLEMENTATION_COLOR_READ_TYPE, &gl_read_type);
|
2023-05-29 16:51:22 +00:00
|
|
|
if (!gdk_gl_texture_find_format (TRUE, major, minor, gdk_memory_format_alpha (format), gl_read_format, gl_read_type, &actual_format))
|
|
|
|
{
|
|
|
|
gl_read_format = GL_RGBA;
|
|
|
|
gl_read_type = GL_UNSIGNED_BYTE;
|
|
|
|
if (gdk_memory_format_alpha (format) == GDK_MEMORY_ALPHA_PREMULTIPLIED)
|
|
|
|
actual_format = GDK_MEMORY_R8G8B8A8_PREMULTIPLIED; /* pray */
|
|
|
|
else
|
|
|
|
actual_format = GDK_MEMORY_R8G8B8A8;
|
|
|
|
}
|
2022-02-08 00:32:44 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gl_read_format = GL_RGBA;
|
|
|
|
gl_read_type = GL_UNSIGNED_BYTE;
|
2023-05-29 16:51:22 +00:00
|
|
|
if (gdk_memory_format_alpha (format) == GDK_MEMORY_ALPHA_PREMULTIPLIED)
|
|
|
|
actual_format = GDK_MEMORY_R8G8B8A8_PREMULTIPLIED; /* pray */
|
|
|
|
else
|
|
|
|
actual_format = GDK_MEMORY_R8G8B8A8;
|
2022-02-08 00:32:44 +00:00
|
|
|
}
|
2021-10-11 01:29:11 +00:00
|
|
|
|
|
|
|
if (download->format == actual_format &&
|
|
|
|
(download->stride == expected_stride))
|
|
|
|
{
|
|
|
|
glReadPixels (0, 0,
|
2023-01-31 12:23:59 +00:00
|
|
|
texture->width, texture->height,
|
2021-10-11 01:29:11 +00:00
|
|
|
gl_read_format,
|
|
|
|
gl_read_type,
|
|
|
|
download->data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gsize actual_bpp = gdk_memory_format_bytes_per_pixel (actual_format);
|
2023-05-29 16:51:22 +00:00
|
|
|
gsize stride = actual_bpp * texture->width;
|
|
|
|
guchar *pixels = g_malloc_n (stride, texture->height);
|
2021-10-11 01:29:11 +00:00
|
|
|
|
2023-05-31 02:35:45 +00:00
|
|
|
glPixelStorei (GL_PACK_ALIGNMENT, 1);
|
2021-10-11 01:29:11 +00:00
|
|
|
glReadPixels (0, 0,
|
2023-01-31 12:23:59 +00:00
|
|
|
texture->width, texture->height,
|
2021-10-11 01:29:11 +00:00
|
|
|
gl_read_format,
|
|
|
|
gl_read_type,
|
|
|
|
pixels);
|
|
|
|
|
2023-05-29 16:51:22 +00:00
|
|
|
/* Fix up gles inadequacies */
|
|
|
|
|
|
|
|
if (gl_read_format == GL_RGBA &&
|
|
|
|
gl_read_type == GL_UNSIGNED_BYTE &&
|
|
|
|
(format == GDK_MEMORY_G8A8 ||
|
|
|
|
format == GDK_MEMORY_G8A8_PREMULTIPLIED ||
|
|
|
|
format == GDK_MEMORY_G8 ||
|
|
|
|
format == GDK_MEMORY_A8))
|
|
|
|
{
|
|
|
|
for (unsigned int y = 0; y < texture->height; y++)
|
|
|
|
{
|
|
|
|
for (unsigned int x = 0; x < texture->width; x++)
|
|
|
|
{
|
|
|
|
guchar *data = &pixels[y * stride + x * actual_bpp];
|
|
|
|
if (format == GDK_MEMORY_G8A8 ||
|
|
|
|
format == GDK_MEMORY_G8A8_PREMULTIPLIED)
|
|
|
|
{
|
|
|
|
data[3] = data[1];
|
|
|
|
data[1] = data[0];
|
|
|
|
data[2] = data[0];
|
|
|
|
}
|
|
|
|
else if (format == GDK_MEMORY_G8)
|
|
|
|
{
|
|
|
|
data[1] = data[0];
|
|
|
|
data[2] = data[0];
|
|
|
|
data[3] = 0xff;
|
|
|
|
}
|
|
|
|
else if (format == GDK_MEMORY_A8)
|
|
|
|
{
|
|
|
|
data[3] = data[0];
|
|
|
|
data[0] = 0;
|
|
|
|
data[1] = 0;
|
|
|
|
data[2] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gl_read_format == GL_RGBA &&
|
|
|
|
gl_read_type == GL_UNSIGNED_SHORT &&
|
|
|
|
(format == GDK_MEMORY_G16A16 ||
|
|
|
|
format == GDK_MEMORY_G16A16_PREMULTIPLIED ||
|
|
|
|
format == GDK_MEMORY_G16 ||
|
|
|
|
format == GDK_MEMORY_A16))
|
|
|
|
{
|
|
|
|
for (unsigned int y = 0; y < texture->height; y++)
|
|
|
|
{
|
|
|
|
for (unsigned int x = 0; x < texture->width; x++)
|
|
|
|
{
|
|
|
|
guint16 *data = (guint16 *) &pixels[y * stride + x * actual_bpp];
|
|
|
|
if (format == GDK_MEMORY_G16A16 ||
|
|
|
|
format == GDK_MEMORY_G16A16_PREMULTIPLIED)
|
|
|
|
{
|
|
|
|
data[3] = data[1];
|
|
|
|
data[1] = data[0];
|
|
|
|
data[2] = data[0];
|
|
|
|
}
|
|
|
|
else if (format == GDK_MEMORY_G16)
|
|
|
|
{
|
|
|
|
data[1] = data[0];
|
|
|
|
data[2] = data[0];
|
|
|
|
data[3] = 0xffff;
|
|
|
|
}
|
|
|
|
else if (format == GDK_MEMORY_A16)
|
|
|
|
{
|
|
|
|
data[3] = data[0];
|
|
|
|
data[0] = 0;
|
|
|
|
data[1] = 0;
|
|
|
|
data[2] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-11 01:29:11 +00:00
|
|
|
gdk_memory_convert (download->data,
|
|
|
|
download->stride,
|
|
|
|
download->format,
|
|
|
|
pixels,
|
2023-05-29 16:51:22 +00:00
|
|
|
stride,
|
2021-10-11 01:29:11 +00:00
|
|
|
actual_format,
|
|
|
|
texture->width,
|
|
|
|
texture->height);
|
|
|
|
|
|
|
|
g_free (pixels);
|
|
|
|
}
|
2021-09-16 21:14:59 +00:00
|
|
|
glBindFramebuffer (GL_FRAMEBUFFER, 0);
|
|
|
|
glDeleteFramebuffers (1, &fbo);
|
|
|
|
}
|
|
|
|
}
|
2021-10-07 00:41:30 +00:00
|
|
|
|
2021-09-12 22:19:35 +00:00
|
|
|
static void
|
2021-10-07 04:19:41 +00:00
|
|
|
gdk_gl_texture_download (GdkTexture *texture,
|
|
|
|
GdkMemoryFormat format,
|
|
|
|
guchar *data,
|
|
|
|
gsize stride)
|
2021-09-10 00:40:21 +00:00
|
|
|
{
|
|
|
|
GdkGLTexture *self = GDK_GL_TEXTURE (texture);
|
2021-10-07 04:19:41 +00:00
|
|
|
Download download;
|
2021-09-10 00:40:21 +00:00
|
|
|
|
2021-09-12 22:19:35 +00:00
|
|
|
if (self->saved)
|
|
|
|
{
|
2021-10-07 04:19:41 +00:00
|
|
|
gdk_texture_do_download (self->saved, format, data, stride);
|
2021-09-12 22:19:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-11 01:29:11 +00:00
|
|
|
download.format = format;
|
2021-10-07 04:19:41 +00:00
|
|
|
download.data = data;
|
|
|
|
download.stride = stride;
|
2021-09-10 00:40:21 +00:00
|
|
|
|
2021-10-07 04:19:41 +00:00
|
|
|
gdk_gl_texture_run (self, gdk_gl_texture_do_download, &download);
|
2021-09-10 00:40:21 +00:00
|
|
|
}
|
|
|
|
|
2018-03-05 13:38:38 +00:00
|
|
|
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;
|
2021-10-07 04:19:41 +00:00
|
|
|
|
2018-03-05 13:38:38 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
guint
|
|
|
|
gdk_gl_texture_get_id (GdkGLTexture *self)
|
|
|
|
{
|
|
|
|
return self->id;
|
|
|
|
}
|
|
|
|
|
2023-03-23 12:44:26 +00:00
|
|
|
gboolean
|
|
|
|
gdk_gl_texture_has_mipmap (GdkGLTexture *self)
|
|
|
|
{
|
|
|
|
return self->has_mipmap;
|
|
|
|
}
|
|
|
|
|
2023-04-25 09:03:51 +00:00
|
|
|
gpointer
|
|
|
|
gdk_gl_texture_get_sync (GdkGLTexture *self)
|
|
|
|
{
|
|
|
|
return self->sync;
|
|
|
|
}
|
|
|
|
|
2018-03-05 13:38:38 +00:00
|
|
|
/**
|
|
|
|
* gdk_gl_texture_release:
|
2021-02-21 05:13:57 +00:00
|
|
|
* @self: a `GdkTexture` wrapping a GL texture
|
2018-03-05 13:38:38 +00:00
|
|
|
*
|
2021-02-21 05:13:57 +00:00
|
|
|
* Releases the GL resources held by a `GdkGLTexture`.
|
2018-03-05 13:38:38 +00:00
|
|
|
*
|
|
|
|
* The texture contents are still available via the
|
2021-02-21 05:13:57 +00:00
|
|
|
* [method@Gdk.Texture.download] function, after this
|
|
|
|
* function has been called.
|
2018-03-05 13:38:38 +00:00
|
|
|
*/
|
|
|
|
void
|
2018-03-18 04:14:52 +00:00
|
|
|
gdk_gl_texture_release (GdkGLTexture *self)
|
2018-03-05 13:38:38 +00:00
|
|
|
{
|
2021-10-07 04:19:41 +00:00
|
|
|
GdkTexture *texture;
|
|
|
|
|
2018-03-18 04:14:52 +00:00
|
|
|
g_return_if_fail (GDK_IS_GL_TEXTURE (self));
|
2018-03-05 13:38:38 +00:00
|
|
|
g_return_if_fail (self->saved == NULL);
|
|
|
|
|
2021-10-07 04:19:41 +00:00
|
|
|
texture = GDK_TEXTURE (self);
|
|
|
|
self->saved = GDK_TEXTURE (gdk_memory_texture_from_texture (texture,
|
|
|
|
gdk_texture_get_format (texture)));
|
2018-03-05 13:38:38 +00:00
|
|
|
|
2023-01-30 13:24:02 +00:00
|
|
|
drop_gl_resources (self);
|
2018-03-05 13:38:38 +00:00
|
|
|
}
|
|
|
|
|
2023-04-24 12:36:38 +00:00
|
|
|
GdkTexture *
|
|
|
|
gdk_gl_texture_new_from_builder (GdkGLTextureBuilder *builder,
|
|
|
|
GDestroyNotify destroy,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GdkGLTexture *self;
|
2023-04-28 03:40:14 +00:00
|
|
|
GdkTexture *update_texture;
|
2023-04-24 12:36:38 +00:00
|
|
|
|
|
|
|
self = g_object_new (GDK_TYPE_GL_TEXTURE,
|
|
|
|
"width", gdk_gl_texture_builder_get_width (builder),
|
|
|
|
"height", gdk_gl_texture_builder_get_height (builder),
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
self->context = g_object_ref (gdk_gl_texture_builder_get_context (builder));
|
|
|
|
self->id = gdk_gl_texture_builder_get_id (builder);
|
|
|
|
GDK_TEXTURE (self)->format = gdk_gl_texture_builder_get_format (builder);
|
|
|
|
self->has_mipmap = gdk_gl_texture_builder_get_has_mipmap (builder);
|
2023-04-25 09:03:51 +00:00
|
|
|
if (gdk_gl_context_has_sync (self->context))
|
|
|
|
self->sync = gdk_gl_texture_builder_get_sync (builder);
|
2023-04-24 12:36:38 +00:00
|
|
|
self->destroy = destroy;
|
|
|
|
self->data = data;
|
|
|
|
|
2023-04-28 03:40:14 +00:00
|
|
|
update_texture = gdk_gl_texture_builder_get_update_texture (builder);
|
|
|
|
if (update_texture)
|
|
|
|
{
|
|
|
|
cairo_region_t *update_region = gdk_gl_texture_builder_get_update_region (builder);
|
|
|
|
if (update_region)
|
|
|
|
{
|
|
|
|
update_region = cairo_region_copy (update_region);
|
|
|
|
cairo_region_intersect_rectangle (update_region,
|
|
|
|
&(cairo_rectangle_int_t) {
|
|
|
|
0, 0,
|
|
|
|
update_texture->width, update_texture->height
|
|
|
|
});
|
|
|
|
gdk_texture_set_diff (GDK_TEXTURE (self), update_texture, update_region);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-24 12:36:38 +00:00
|
|
|
return GDK_TEXTURE (self);
|
|
|
|
}
|
|
|
|
|
2021-10-07 00:41:30 +00:00
|
|
|
static void
|
2021-10-12 22:36:38 +00:00
|
|
|
gdk_gl_texture_determine_format (GdkGLTexture *self)
|
2021-10-07 00:41:30 +00:00
|
|
|
{
|
2021-10-12 22:36:38 +00:00
|
|
|
GdkTexture *texture = GDK_TEXTURE (self);
|
2023-03-24 15:29:38 +00:00
|
|
|
GdkGLContext *context;
|
2021-10-12 22:36:38 +00:00
|
|
|
GLint active_texture;
|
2021-10-07 00:41:30 +00:00
|
|
|
GLint internal_format;
|
2023-03-23 12:44:26 +00:00
|
|
|
GLint width, height;
|
2021-10-07 00:41:30 +00:00
|
|
|
|
2021-12-25 13:46:07 +00:00
|
|
|
/* Abort if somebody else is GL-ing here... */
|
2023-03-24 15:29:38 +00:00
|
|
|
context = gdk_gl_context_get_current ();
|
|
|
|
if (context == NULL ||
|
|
|
|
!gdk_gl_context_is_shared (self->context, context) ||
|
2021-12-25 13:46:07 +00:00
|
|
|
/* ... or glGetTexLevelParameter() isn't supported */
|
2023-04-25 17:32:12 +00:00
|
|
|
!gdk_gl_context_check_version (context, NULL, "3.1"))
|
2021-10-12 22:36:38 +00:00
|
|
|
{
|
|
|
|
texture->format = GDK_MEMORY_DEFAULT;
|
2023-03-23 12:44:26 +00:00
|
|
|
self->has_mipmap = FALSE;
|
2021-10-12 22:36:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We need to be careful about modifying the GL context, as this is not
|
|
|
|
* expected during construction */
|
|
|
|
glGetIntegerv (GL_TEXTURE_BINDING_2D, &active_texture);
|
|
|
|
glBindTexture (GL_TEXTURE_2D, self->id);
|
|
|
|
|
2021-10-07 00:41:30 +00:00
|
|
|
glGetTexLevelParameteriv (GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &internal_format);
|
|
|
|
|
|
|
|
switch (internal_format)
|
|
|
|
{
|
|
|
|
case GL_RGB8:
|
2023-03-13 13:59:17 +00:00
|
|
|
case GL_RGB:
|
2021-10-07 00:41:30 +00:00
|
|
|
texture->format = GDK_MEMORY_R8G8B8;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GL_RGBA8:
|
|
|
|
texture->format = GDK_MEMORY_R8G8B8A8_PREMULTIPLIED;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GL_RGB16:
|
|
|
|
texture->format = GDK_MEMORY_R16G16B16;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GL_RGBA16:
|
|
|
|
texture->format = GDK_MEMORY_R16G16B16A16_PREMULTIPLIED;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GL_RGB16F:
|
|
|
|
texture->format = GDK_MEMORY_R16G16B16_FLOAT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GL_RGBA16F:
|
|
|
|
texture->format = GDK_MEMORY_R16G16B16A16_FLOAT_PREMULTIPLIED;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GL_RGB32F:
|
|
|
|
texture->format = GDK_MEMORY_R32G32B32_FLOAT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GL_RGBA32F:
|
|
|
|
texture->format = GDK_MEMORY_R32G32B32A32_FLOAT_PREMULTIPLIED;
|
|
|
|
break;
|
|
|
|
|
2022-03-24 03:20:02 +00:00
|
|
|
case GL_RGBA:
|
|
|
|
{
|
|
|
|
GLint red_size = 0;
|
|
|
|
GLint green_size = 0;
|
|
|
|
GLint blue_size = 0;
|
|
|
|
GLint alpha_size = 0;
|
|
|
|
GLint red_type = 0;
|
|
|
|
GLint green_type = 0;
|
|
|
|
GLint blue_type = 0;
|
|
|
|
GLint alpha_type = 0;
|
|
|
|
|
|
|
|
glGetTexLevelParameteriv (GL_TEXTURE_2D, 0, GL_TEXTURE_RED_TYPE, &red_type);
|
|
|
|
glGetTexLevelParameteriv (GL_TEXTURE_2D, 0, GL_TEXTURE_GREEN_TYPE, &green_type);
|
|
|
|
glGetTexLevelParameteriv (GL_TEXTURE_2D, 0, GL_TEXTURE_BLUE_TYPE, &blue_type);
|
|
|
|
glGetTexLevelParameteriv (GL_TEXTURE_2D, 0, GL_TEXTURE_ALPHA_TYPE, &alpha_type);
|
|
|
|
|
|
|
|
glGetTexLevelParameteriv (GL_TEXTURE_2D, 0, GL_TEXTURE_RED_SIZE, &red_size);
|
|
|
|
glGetTexLevelParameteriv (GL_TEXTURE_2D, 0, GL_TEXTURE_GREEN_SIZE, &green_size);
|
|
|
|
glGetTexLevelParameteriv (GL_TEXTURE_2D, 0, GL_TEXTURE_BLUE_SIZE, &blue_size);
|
|
|
|
glGetTexLevelParameteriv (GL_TEXTURE_2D, 0, GL_TEXTURE_ALPHA_SIZE, &alpha_size);
|
|
|
|
|
|
|
|
#define CHECK_RGBA(rt,gt,bt,at,rs,gs,bs,as) \
|
|
|
|
(red_type == rt && green_type == gt && blue_type == bt && alpha_type == at && \
|
|
|
|
red_size == rs && green_size == gs && blue_size == bs && alpha_size == as)
|
|
|
|
|
|
|
|
if (CHECK_RGBA (GL_UNSIGNED_NORMALIZED, GL_UNSIGNED_NORMALIZED, GL_UNSIGNED_NORMALIZED, GL_UNSIGNED_NORMALIZED, 8, 8, 8, 8))
|
|
|
|
{
|
|
|
|
texture->format = GDK_MEMORY_R8G8B8A8_PREMULTIPLIED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef CHECK_RGBA
|
|
|
|
}
|
|
|
|
|
|
|
|
G_GNUC_FALLTHROUGH;
|
|
|
|
|
2021-10-07 00:41:30 +00:00
|
|
|
default:
|
|
|
|
g_warning ("Texture in unexpected format 0x%X (%d). File a bug about adding it to GTK", internal_format, internal_format);
|
|
|
|
/* fallback to the dumbest possible format
|
|
|
|
* so that even age old GLES can do it */
|
|
|
|
texture->format = GDK_MEMORY_R8G8B8A8_PREMULTIPLIED;
|
|
|
|
break;
|
|
|
|
}
|
2021-10-12 22:36:38 +00:00
|
|
|
|
2023-03-23 12:44:26 +00:00
|
|
|
/* Determine if the texture has a mipmap.
|
|
|
|
* We do this here, since it requires binding the texture,
|
|
|
|
* and we're already doing that here.
|
|
|
|
* GL has no way to directly query 'mipmap completeness' of textures,
|
|
|
|
* so we just check that level 1 has the expected size, and assume
|
|
|
|
* that means somebody called glGenerateMipmap().
|
|
|
|
*/
|
|
|
|
glGetTexLevelParameteriv (GL_TEXTURE_2D, 1, GL_TEXTURE_WIDTH, &width);
|
|
|
|
glGetTexLevelParameteriv (GL_TEXTURE_2D, 1, GL_TEXTURE_HEIGHT, &height);
|
|
|
|
|
|
|
|
self->has_mipmap = width == texture->width / 2 &&
|
|
|
|
height == texture->height / 2;
|
|
|
|
|
|
|
|
/* restore previous state */
|
2021-10-12 22:36:38 +00:00
|
|
|
glBindTexture (GL_TEXTURE_2D, active_texture);
|
2021-10-07 00:41:30 +00:00
|
|
|
}
|
|
|
|
|
2018-03-05 13:38:38 +00:00
|
|
|
/**
|
2018-03-18 04:14:52 +00:00
|
|
|
* gdk_gl_texture_new:
|
2021-02-21 05:13:57 +00:00
|
|
|
* @context: a `GdkGLContext`
|
2018-03-05 13:38:38 +00:00
|
|
|
* @id: the ID of a texture that was created with @context
|
|
|
|
* @width: the nominal width of the texture
|
|
|
|
* @height: the nominal height of the texture
|
|
|
|
* @destroy: a destroy notify that will be called when the GL resources
|
2021-02-21 05:13:57 +00:00
|
|
|
* are released
|
2018-03-05 13:38:38 +00:00
|
|
|
* @data: data that gets passed to @destroy
|
|
|
|
*
|
|
|
|
* Creates a new texture for an existing GL texture.
|
|
|
|
*
|
|
|
|
* Note that the GL texture must not be modified until @destroy is called,
|
|
|
|
* which will happen when the GdkTexture object is finalized, or due to
|
2021-02-21 05:13:57 +00:00
|
|
|
* an explicit call of [method@Gdk.GLTexture.release].
|
2018-03-05 13:38:38 +00:00
|
|
|
*
|
2022-01-06 16:39:06 +00:00
|
|
|
* Return value: (transfer full) (type GdkGLTexture): A newly-created
|
|
|
|
* `GdkTexture`
|
2023-04-24 20:44:01 +00:00
|
|
|
*
|
|
|
|
* Deprecated: 4.12: [class@Gdk.GLTextureBuilder] supercedes this function
|
|
|
|
* and provides extended functionality for creating GL textures.
|
2018-03-12 10:06:59 +00:00
|
|
|
*/
|
2018-03-05 13:38:38 +00:00
|
|
|
GdkTexture *
|
|
|
|
gdk_gl_texture_new (GdkGLContext *context,
|
|
|
|
guint id,
|
|
|
|
int width,
|
|
|
|
int height,
|
|
|
|
GDestroyNotify destroy,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GdkGLTexture *self;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GDK_IS_GL_CONTEXT (context), NULL);
|
|
|
|
g_return_val_if_fail (id != 0, NULL);
|
|
|
|
g_return_val_if_fail (width > 0, NULL);
|
|
|
|
g_return_val_if_fail (height > 0, 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;
|
|
|
|
|
2021-10-12 22:36:38 +00:00
|
|
|
gdk_gl_texture_determine_format (self);
|
2021-10-07 00:41:30 +00:00
|
|
|
|
2018-03-05 13:38:38 +00:00
|
|
|
return GDK_TEXTURE (self);
|
|
|
|
}
|
|
|
|
|