Compare commits

...

12 Commits

Author SHA1 Message Date
Matthias Clasen
406a32d825 Fix the build 2023-02-12 16:31:40 -05:00
Matthias Clasen
beabe8c33b gsk: Drop an unnecessary call
The renderer should make all the necessary
make_current calls.
2023-02-12 15:12:43 -05:00
Matthias Clasen
90500519d6 gst: Add a comment about context confusion 2023-02-12 15:12:43 -05:00
Matthias Clasen
06921674a9 gsk: Only wait if we need to
If the texture was created in the same context
we are using it in, then waiting on the sync
will be a no-op.
2023-02-12 15:12:43 -05:00
Matthias Clasen
a91fa9a680 gltexture: Only wait if we need to
If the texture was created in the same context
we are downloading it from, then waiting on the
sync will be a no-op.
2023-02-12 15:12:43 -05:00
Matthias Clasen
ead40dc834 gldriver: Add a sync when creating textures 2023-02-12 15:12:43 -05:00
Matthias Clasen
2be928b569 gstreamer: Defer the sync
Don't sync right when we receive the buffer,
pass it along with the texture to be executed
later in the renderer.
2023-02-12 15:12:43 -05:00
Matthias Clasen
621ba6d67c Export gdk_gl_texture_new_with_sync
Otherwise we can't use it in the loadable modules,
like the GStreamer media stream.

This change puts gdk_gl_texture_new_with_sync into
the private, but exported bucket for now. We will
make new public APIs for textures (including
synchronization) when we add color management support.
2023-02-12 15:12:43 -05:00
Matthias Clasen
af929a8787 glarea: Synchronize
Create a fence object and pass it along when
creating the GL texture, so that the GL renderer
can wait for the texture to be ready.
2023-02-12 15:12:43 -05:00
Matthias Clasen
ef986f4256 gsk: Synchronize when using textures
Pass the GLsync object from texture into our
command queue, and when executing the queue,
wait on the sync object the first time we
use its associated texture.
2023-02-12 15:12:41 -05:00
Matthias Clasen
ab36c275cf gltexture: Synchronize when downloading
If the GL texture has a sync object, wait
on it before downloading the data.
2023-02-12 15:09:21 -05:00
Matthias Clasen
720327bc00 gltexture: Optionally take a sync object
Add a new (private, for now) constructor takes
a GLsync together with a texture id, for synchronization.
2023-02-12 15:09:21 -05:00
9 changed files with 188 additions and 27 deletions

View File

@@ -39,6 +39,7 @@ struct _GdkGLTexture {
GdkGLContext *context;
guint id;
GLsync sync;
GdkTexture *saved;
@@ -64,6 +65,7 @@ drop_gl_resources (GdkGLTexture *self)
g_clear_object (&self->context);
self->id = 0;
self->sync = NULL;
}
static void
@@ -99,6 +101,10 @@ gdk_gl_texture_invoke_callback (gpointer data)
context = gdk_display_get_gl_context (gdk_gl_context_get_display (invoke->self->context));
gdk_gl_context_make_current (context);
if (invoke->self->sync && context != invoke->self->context)
glWaitSync (invoke->self->sync, 0, GL_TIMEOUT_IGNORED);
glBindTexture (GL_TEXTURE_2D, invoke->self->id);
invoke->func (invoke->self, context, invoke->data);
@@ -440,6 +446,46 @@ gdk_gl_texture_new (GdkGLContext *context,
int height,
GDestroyNotify destroy,
gpointer data)
{
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);
return gdk_gl_texture_new_with_sync (context, id, NULL, width, height, destroy, data);
}
/*< private >
* gdk_gl_texture_new_with_sync:
* @context: a `GdkGLContext`
* @id: the ID of a texture that was created with @context
* @sync: (nullable): an optional GLsync object
* @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
* are released
* @data: data that gets passed to @destroy
*
* Creates a new texture for an existing GL texture.
*
* If @sync is given, consumers of the texture are required to wait on
* it before attempting to use the GL texture.
*
* The GL texture and the sync object must stay alive unmodified until
* @destroy is called, which will happen when the GdkTexture object is
* finalized, or due to an explicit call of [method@Gdk.GLTexture.release].
*
* Return value: (transfer full) (type GdkGLTexture): A newly-created
* `GdkTexture`
*/
GdkTexture *
gdk_gl_texture_new_with_sync (GdkGLContext *context,
guint id,
gpointer sync,
int width,
int height,
GDestroyNotify destroy,
gpointer data)
{
GdkGLTexture *self;
@@ -455,6 +501,7 @@ gdk_gl_texture_new (GdkGLContext *context,
self->context = g_object_ref (context);
self->id = id;
self->sync = sync;
self->destroy = destroy;
self->data = data;
@@ -463,3 +510,8 @@ gdk_gl_texture_new (GdkGLContext *context,
return GDK_TEXTURE (self);
}
gpointer
gdk_gl_texture_get_sync (GdkGLTexture *self)
{
return self->sync;
}

View File

@@ -9,6 +9,16 @@ G_BEGIN_DECLS
GdkGLContext * gdk_gl_texture_get_context (GdkGLTexture *self);
guint gdk_gl_texture_get_id (GdkGLTexture *self);
gpointer gdk_gl_texture_get_sync (GdkGLTexture *self);
GDK_AVAILABLE_IN_4_10
GdkTexture * gdk_gl_texture_new_with_sync (GdkGLContext *context,
guint id,
gpointer sync,
int width,
int height,
GDestroyNotify destroy,
gpointer data);
G_END_DECLS

View File

@@ -403,6 +403,7 @@ gsk_gl_command_queue_dispose (GObject *object)
gsk_gl_command_batches_clear (&self->batches);
gsk_gl_command_binds_clear (&self->batch_binds);
gsk_gl_command_uniforms_clear (&self->batch_uniforms);
gsk_gl_syncs_clear (&self->syncs);
gsk_gl_buffer_destroy (&self->vertices);
@@ -425,6 +426,7 @@ gsk_gl_command_queue_init (GskGLCommandQueue *self)
gsk_gl_command_batches_init (&self->batches, 128);
gsk_gl_command_binds_init (&self->batch_binds, 1024);
gsk_gl_command_uniforms_init (&self->batch_uniforms, 2048);
gsk_gl_syncs_init (&self->syncs, 10);
gsk_gl_buffer_init (&self->vertices, GL_ARRAY_BUFFER, sizeof (GskGLDrawVertex));
}
@@ -1098,17 +1100,25 @@ gsk_gl_command_queue_execute (GskGLCommandQueue *self,
if G_UNLIKELY (batch->draw.bind_count > 0)
{
const GskGLCommandBind *bind = &self->batch_binds.items[batch->draw.bind_offset];
for (guint i = 0; i < batch->draw.bind_count; i++)
{
if (textures[bind->texture] != bind->id)
{
GskGLSync *s;
if (active != bind->texture)
{
active = bind->texture;
glActiveTexture (GL_TEXTURE0 + bind->texture);
}
s = gsk_gl_syncs_get_sync (&self->syncs, bind->id);
if (s && s->sync)
{
glWaitSync ((GLsync) s->sync, 0, GL_TIMEOUT_IGNORED);
s->sync = NULL;
}
glBindTexture (GL_TEXTURE_2D, bind->id);
textures[bind->texture] = bind->id;
}
@@ -1236,6 +1246,7 @@ gsk_gl_command_queue_end_frame (GskGLCommandQueue *self)
self->batches.len = 0;
self->batch_binds.len = 0;
self->batch_uniforms.len = 0;
self->syncs.len = 0;
self->n_uploads = 0;
self->tail_batch_index = -1;
self->in_frame = FALSE;

View File

@@ -167,9 +167,15 @@ typedef union _GskGLCommandBatch
G_STATIC_ASSERT (sizeof (GskGLCommandBatch) == 32);
typedef struct _GskGLSync {
guint id;
gpointer sync;
} GskGLSync;
DEFINE_INLINE_ARRAY (GskGLCommandBatches, gsk_gl_command_batches, GskGLCommandBatch)
DEFINE_INLINE_ARRAY (GskGLCommandBinds, gsk_gl_command_binds, GskGLCommandBind)
DEFINE_INLINE_ARRAY (GskGLCommandUniforms, gsk_gl_command_uniforms, GskGLCommandUniform)
DEFINE_INLINE_ARRAY (GskGLSyncs, gsk_gl_syncs, GskGLSync)
struct _GskGLCommandQueue
{
@@ -225,6 +231,8 @@ struct _GskGLCommandQueue
*/
GskGLCommandUniforms batch_uniforms;
GskGLSyncs syncs;
/* Discovered max texture size when loading the command queue so that we
* can either scale down or slice textures to fit within this size. Assumed
* to be both height and width.
@@ -354,6 +362,37 @@ gsk_gl_command_queue_bind_framebuffer (GskGLCommandQueue *self,
return ret;
}
static inline GskGLSync *
gsk_gl_syncs_get_sync (GskGLSyncs *syncs,
guint id)
{
for (unsigned int i = 0; i < syncs->len; i++)
{
GskGLSync *sync = &syncs->items[i];
if (sync->id == id)
return sync;
}
return NULL;
}
static inline void
gsk_gl_syncs_add_sync (GskGLSyncs *syncs,
guint id,
gpointer sync)
{
GskGLSync *s;
s = gsk_gl_syncs_get_sync (syncs, id);
if (s)
g_assert (s->sync == sync);
else
{
s = gsk_gl_syncs_append (syncs);
s->id = id;
s->sync = sync;
}
}
G_END_DECLS
#endif /* __GSK_GL_COMMAND_QUEUE_PRIVATE_H__ */

View File

@@ -1329,6 +1329,7 @@ typedef struct _GskGLTextureState
{
GdkGLContext *context;
GLuint texture_id;
GLsync sync;
} GskGLTextureState;
static void
@@ -1341,6 +1342,7 @@ create_texture_from_texture_destroy (gpointer data)
gdk_gl_context_make_current (state->context);
glDeleteTextures (1, &state->texture_id);
glDeleteSync (state->sync);
g_clear_object (&state->context);
g_slice_free (GskGLTextureState, state);
}
@@ -1375,10 +1377,13 @@ gsk_gl_driver_create_gdk_texture (GskGLDriver *self,
texture->texture_id = 0;
gsk_gl_texture_free (texture);
return gdk_gl_texture_new (self->command_queue->context,
texture_id,
width,
height,
create_texture_from_texture_destroy,
state);
state->sync = glFenceSync (GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
return gdk_gl_texture_new_with_sync (self->command_queue->context,
texture_id,
state->sync,
width,
height,
create_texture_from_texture_destroy,
state);
}

View File

@@ -264,6 +264,27 @@ gsk_gl_program_set_uniform_texture (GskGLProgram *self,
texture_slot);
}
static inline void
gsk_gl_program_set_uniform_texture_with_sync (GskGLProgram *self,
guint key,
guint stamp,
GLenum texture_target,
GLenum texture_slot,
guint texture_id,
gpointer sync)
{
gsk_gl_attachment_state_bind_texture (self->driver->command_queue->attachments,
texture_target,
texture_slot,
texture_id);
gsk_gl_uniform_state_set_texture (self->uniforms,
self->program_info,
key,
stamp,
texture_slot);
gsk_gl_syncs_add_sync (&self->driver->command_queue->syncs, texture_id, sync);
}
static inline void
gsk_gl_program_set_uniform_matrix (GskGLProgram *self,
guint key,

View File

@@ -190,6 +190,7 @@ typedef struct _GskGLRenderOffscreen
/* Return location for texture ID */
guint texture_id;
gpointer sync;
/* Whether to force creating a new texture, even if the
* input already is a texture
@@ -3497,12 +3498,17 @@ gsk_gl_render_job_upload_texture (GskGLRenderJob *job,
int mag_filter,
GskGLRenderOffscreen *offscreen)
{
GdkGLTexture *gl_texture = NULL;
if (GDK_IS_GL_TEXTURE (texture))
gl_texture = (GdkGLTexture *) texture;
if (min_filter == GL_LINEAR &&
mag_filter == GL_LINEAR &&
gsk_gl_texture_library_can_cache ((GskGLTextureLibrary *)job->driver->icons_library,
texture->width,
texture->height) &&
!GDK_IS_GL_TEXTURE (texture))
!gl_texture)
{
const GskGLIconData *icon_data;
@@ -3514,6 +3520,9 @@ gsk_gl_render_job_upload_texture (GskGLRenderJob *job,
{
offscreen->texture_id = gsk_gl_driver_load_texture (job->driver, texture, min_filter, mag_filter);
init_full_texture_region (offscreen);
if (gl_texture && offscreen->texture_id == gdk_gl_texture_get_id (gl_texture) &&
gdk_gl_texture_get_context (gl_texture) != gsk_gl_command_queue_get_context (job->command_queue))
offscreen->sync = gdk_gl_texture_get_sync (gl_texture);
}
}
@@ -3535,11 +3544,12 @@ gsk_gl_render_job_visit_texture (GskGLRenderJob *job,
g_assert (offscreen.was_offscreen == FALSE);
gsk_gl_render_job_begin_draw (job, CHOOSE_PROGRAM (job, blit));
gsk_gl_program_set_uniform_texture (job->current_program,
UNIFORM_SHARED_SOURCE, 0,
GL_TEXTURE_2D,
GL_TEXTURE0,
offscreen.texture_id);
gsk_gl_program_set_uniform_texture_with_sync (job->current_program,
UNIFORM_SHARED_SOURCE, 0,
GL_TEXTURE_2D,
GL_TEXTURE0,
offscreen.texture_id,
offscreen.sync);
gsk_gl_render_job_draw_offscreen (job, bounds, &offscreen);
gsk_gl_render_job_end_draw (job);
}

View File

@@ -32,6 +32,7 @@
#include "gtksnapshot.h"
#include "gtkrenderlayoutprivate.h"
#include "gtkcssnodeprivate.h"
#include "gdk/gdkgltextureprivate.h"
#include <epoxy/gl.h>
@@ -144,6 +145,7 @@
typedef struct {
guint id;
GLsync sync;
int width;
int height;
GdkTexture *holder;
@@ -394,6 +396,8 @@ delete_one_texture (gpointer data)
texture->id = 0;
}
g_clear_pointer (&texture->sync, glDeleteSync);
g_free (texture);
}
@@ -673,6 +677,7 @@ release_texture (gpointer data)
{
Texture *texture = data;
texture->holder = NULL;
g_clear_pointer (&texture->sync, glDeleteSync);
}
static void
@@ -735,11 +740,13 @@ gtk_gl_area_snapshot (GtkWidget *widget,
priv->texture = NULL;
priv->textures = g_list_prepend (priv->textures, texture);
texture->holder = gdk_gl_texture_new (priv->context,
texture->id,
texture->width,
texture->height,
release_texture, texture);
texture->sync = glFenceSync (GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
texture->holder = gdk_gl_texture_new_with_sync (priv->context,
texture->id,
texture->sync,
texture->width,
texture->height,
release_texture, texture);
/* Our texture is rendered by OpenGL, so it is upside down,
* compared to what GSK expects, so flip it back.

View File

@@ -21,6 +21,7 @@
#include "config.h"
#include "gtkgstsinkprivate.h"
#include "gdkgltextureprivate.h"
#include "gtkgstpaintableprivate.h"
@@ -290,17 +291,22 @@ gtk_gst_sink_texture_from_buffer (GtkGstSink *self,
GstGLSyncMeta *sync_meta;
sync_meta = gst_buffer_get_gl_sync_meta (buffer);
if (sync_meta) {
if (sync_meta)
gst_gl_sync_meta_set_sync_point (sync_meta, self->gst_context);
gst_gl_sync_meta_wait (sync_meta, self->gst_gdk_context);
}
texture = gdk_gl_texture_new (self->gdk_context,
*(guint *) frame->data[0],
frame->info.width,
frame->info.height,
(GDestroyNotify) video_frame_free,
frame);
/* Note: using the gdk_context here is a (harmless) lie,
* since the texture really originates in the gst_context.
* But that is not a GdkGLContext. It is harmless, because
* we are never using the texture in the gdk_context, so we
* never make the (erroneous) decision to ignore the sync.
*/
texture = gdk_gl_texture_new_with_sync (self->gdk_context,
*(guint *) frame->data[0],
sync_meta ? sync_meta->data : NULL,
frame->info.width,
frame->info.height,
(GDestroyNotify) video_frame_free,
frame);
*pixel_aspect_ratio = ((double) frame->info.par_n) / ((double) frame->info.par_d);
}