Commit Graph

65 Commits

Author SHA1 Message Date
Simon McVittie
0fa2ae48d4 gdkgltexture: Avoid use-after-free when switching GdkGLContext
`thread_current_context` might be holding the last reference to
`previous`, in which case `gdk_gl_context_make_current` on the new
context will free `previous`, leaving it a dangling pointer.
Avoid this by making sure to hold a reference.

Fixes: 41cd0c6f "gl: Fix initial EGL context creation on X11"
Resolves: https://gitlab.gnome.org/GNOME/gtk/-/issues/6995
Signed-off-by: Simon McVittie <smcv@debian.org>
2024-09-10 00:11:18 +01:00
Matthias Clasen
0ee8ca43d1 gl texture: color state support 2024-07-16 21:23:44 +02:00
Benjamin Otte
11543a229a texturedownloader: Add color state
... and plumb the color state through the downloading machinery, where
no matter what path it takes it ends up in
gdk_memory_convert_color_state() or gdk_memory_convert().

The 2nd of those has been expanded to optionally do colorstate
conversion when the 2 colorstates are different.
2024-07-16 21:23:44 +02:00
Benjamin Otte
a7ceb8ce66 gdk: Add GDK_MEMORY_U8_SRGB depth
This is an experiment for now, but it seems that encoding srgb inside
the depth makes sense, as we not just use depth to decide on the
GL fbconfigs/Vulkan formats to pick, depth also encodes how the [0...1]
color values are quantized when stored.

Let's see where this goes.
2024-07-11 14:57:19 +02:00
Matthias Clasen
5297c5f2ee gdk: Give textures a color state
Adds gdk_texture_get_color_state() and GdkTexture::color-state property.

All texture implementations initialize the property to SRGB for now.
2024-07-11 14:57:19 +02:00
Benjamin Otte
2f4e19d514 gdk: Allow querying GL SRGB formats
Nobody is using this yet.
2024-07-11 14:57:19 +02:00
Matthias Clasen
d1d4d80a1d Simply some internal api
The only caller of gdk_memory_texture_from_texture doesn't use
the second argument.
2024-06-19 02:06:14 -04:00
Benjamin Otte
141769fb46 gl: Turn has_foo flags into GdkGLFeatures
The goal is to have it mirror GdkVulkanFeatures, and in particular
having an environment variable to turn individual flags off.
2024-03-16 13:44:02 +01:00
Benjamin Otte
4e2c7d5eb0 gl: Require GL/GLES flag when looking up memory formats
This code just adds the argument to the function and fixes all callers.

It's separate because it's just a bunch of boilerplate.
2024-01-16 20:35:20 +01:00
Matthias Clasen
59578c6d18 Fix typos throughout
These were pointed out by codespell.
2024-01-07 20:44:05 -05:00
Benjamin Otte
4dfc07ef57 memoryformat: Fix variable types
Use the same types that GL uses.
2023-12-12 01:49:41 +01:00
Benjamin Otte
0889037959 gltexture: Use the right format to download
When the format is set correctly, which it should be, we should just use
it.

So do that.
2023-12-12 01:49:40 +01:00
Benjamin Otte
3a1349e8ef gl: Make gdk_memory_format_gl_format() no longer check support
Now that we have gdk_gl_context_get_memory_flags() and code can use that
function, make the code do that.

Remove support checks from gdk_memory_format_gl_format().

This is an initial naive port that doesn't try to make use of the finer-grained
flags yet.
2023-12-11 07:33:26 +01:00
Matthias Clasen
b95d8ebdd3 memoryformat: Change some private API
Make gdk_memory_format_gl_format take the GdkGLContext,
instead of just a gles boolean. This will let us
check for extensions that may be needed for certain
formats.

Update all callers.
2023-10-19 16:44:18 -04:00
Benjamin Otte
8aaecc3416 gltexture: Restore previous GL context
Texture downloads can be initiated due to the weirdest reasons - and if
they cause a GL context to be changed, it'd be basically unpredictable
when the GL context changes.

An example is the Cairo renderer - if it needs to draw a GL texture, it
will download it.

Now that no longer changes the GL context.
2023-08-24 05:00:29 +02:00
Benjamin Otte
dd4b8a4c89 gl: Pass swizzle array directly
There's no need to pass pointers to arrays, as arrays are already
pointers.
2023-08-12 23:13:23 +02:00
Matthias Clasen
f29c7e76f5 gltexture: Use proper alignment for downloads 2023-05-30 22:35:45 -04:00
Matthias Clasen
1c72f46eed gltexture: Rewrite downloading code
For non-gles, make it handle unpremultiplied formats,
and everything else, by downloading the texture in its
preferred format and, in most cases, doing a
gdk_memory_convert afterwards.

For gles, keep using glReadPixels, but handle cases
where the gl read format doesn't match the texture
format by doing the necessary swizzling before calling
gdk_memory_convert.
2023-05-30 15:57:18 -04:00
Matthias Clasen
0f61c52593 gdk: Simplify gdk_memory_format_gl_format
Make the callers of this function check for
straight alpha themselves, and only do the
version compatibility check here. This makes
the function usable in contexts where straight
alpha is acceptable.
2023-05-30 14:49:45 -04:00
Sophie Herold
ef8c835762 gsk: Support swizzle for gray and alpha formats
Swizzling is needed to display one channel memory formats
as gray etc.
2023-05-30 14:41:01 -04:00
Benjamin Otte
6506a3bc9c texturebuilder: Add ::udpate-region and ::update-texture 2023-05-02 00:30:58 +02:00
Matthias Clasen
6efaa79e3c gltexture: Synchronize when downloading
If the GL texture has a sync object, wait
on it before downloading the data.
2023-04-27 06:57:02 +02:00
Matthias Clasen
92eb845482 gltexture: Optionally take a sync object
Add a new function to TextureBuilder that takes a GLsync that
requires internal code to wait on before using the texture.

Somewhat sneakily, we don't take the sync if syncs are not supported by
the current GL context.
As public API has no code to query the sync for the destroy notify, this
is fine and it means we don't have to do the check every time we want to
call gdk_texture_get_sync() internally.
2023-04-27 06:55:37 +02:00
Benjamin Otte
18a4b2475e gltexture: Deprecate gdk_gl_texture_new()
Use GdkGLTextureBuilder instead.
2023-04-27 06:40:47 +02:00
Benjamin Otte
e37fbaf13a texturebuilder: Add ::format and ::has-mipmap
We were trying to deduce that previously. Now we have explicit API
2023-04-27 06:40:47 +02:00
Benjamin Otte
3aefed39b1 glcontext: Use GdkGLVersion elsewhere
... and add a convenience API to generate GL versions from strings to
make the gdk_gl_context_check() API nicer.
2023-04-27 02:13:32 +02:00
Benjamin Otte
8965d6c7f8 gl: Only allow RGB(A)8 on GLES2
GLES2 has no idea what 16bit textures even are, let alone floating point.
2023-04-17 05:57:44 +02:00
Matthias Clasen
5c558c0091 Fix up criticals from GdkGLTexture
I broke this in 4291f27b56.
I overlooked that there may not be a current context.
2023-03-24 11:29:38 -04:00
Matthias Clasen
7cb7e0ab35 gl: Add a private way to query mipmap status
Make GdkGLTexture determine if the texture has
a mipmap, and provide private API to query this
information.

This check is done in gdkgltexture.c instead of
gskgldriver.c, since we're already binding the
texture here for other reasons, so it is easy
to query a few more things.
2023-03-23 23:16:54 -04:00
Matthias Clasen
4291f27b56 gltexture: Try harder to determine the format
We can query these things as long as the texture ID
is valid in the current context.
2023-03-23 22:56:06 -04:00
Bilal Elmoussaoui
23408f6dbf GLTexture: Add GL_RGB format support 2023-03-13 13:59:17 +00:00
Benjamin Otte
c9f54ca371 texture: Export gdk_texture_get_format()
The API docs outline why quite well.

This should make it possible to do saving of textures to image files
without any private API with the same featureset that GTK uses.

Also remove the gsktextureprivate.h include where
gdk_texture_get_format() was the only reason for it.
2023-02-15 00:39:18 +01:00
Matthias Clasen
12057c8a30 gltexture: Small refactoring
We do this in two places, so factor out
the dropping of GL resources into a helper.
2023-02-02 10:01:36 +01:00
Matthias Clasen
6655177a0b Cosmetics 2023-02-01 18:38:22 +01:00
Matthias Clasen
1cb621633c gltexture: Use the right context
When checking characteristics of the context
for downloading, we were using self->context,
even though we are using a possibly different
context for downloading.

Pass the right context along and use it.
2023-02-01 11:32:07 +01:00
Christian Hergert
e706e14fd9 gdk/gl: handle GL_RGBA/GL_UNSIGNED_NORMALIZED
WebKit's GTK 4 port can give us textures with an internal format of
GL_RGBA with GL_UNSIGNED_NORMALIZED and a bit-depth of 8. This fixes
warnings for every GdkGLTexture created/delivered to the GskGLRenderer.

The format is essentially the same as GL_RGBA8 since it is normalized
between 0.0..1.0 for 8-bit components.

Fixes #4783
2022-03-24 18:05:11 -07:00
Benjamin Otte
2c71825324 gltexture: Only use glGetFramebufferParameter() when available
Fixes #4678
2022-02-08 01:34:19 +01:00
Maximiliano Sandoval R
ef9dbf73cc
gir: Add type annotations for {GL,Memory}Texture 2022-01-06 19:30:29 +01:00
Benjamin Otte
5803dd765d gles: glGetTexLevelParameter() isn't supported 2021-12-25 15:07:44 +01:00
Benjamin Otte
c7e36bcf22 gltexture: Fix conditions for glGetTexImage() call
The conditions were inverted, plus we failed to limit this call to
non-GLES.
2021-10-17 22:28:44 +02:00
Benjamin Otte
52e88ed4c8 gltexture: Determine format in current thread
Close widget-factory and observe:

Thread 1:
 * acquire main loop
 * handle close button
 * close window
 * dispose video and media stream
 * stop GstPlayer
WAIT on pipeline stopping

Thread 2:
 * prepare next image in pipeline
 * hand image to GtkGstSink
 * create GdkTexture from image
 * gdk_gl_texture_new() determines format
WAIT on acquiring main loop

Sounds like a deadlock?

Indeed, so don't do that.
2021-10-13 14:33:44 +02:00
Benjamin Otte
31e9072906 gl: Fix downloading textures *again*
It turns out glReadPixels() cannot convert pixels and you are only
allowed to pass a single value into the function arguments. You need to
know which ones or things will explode.

GL is great.
2021-10-13 14:33:44 +02:00
Benjamin Otte
b65b64628d texture: Refactor downloading
Pass a format do GdkTextureClass::download(). That way we can download
data in any format.

Also replace gdk_texture_download_texture() with
gdk_memory_texture_from_texture() which again takes a format.

The old functionality is still there for code that wants it: Just pass
gdk_texture_get_format (texture) as the format argument.
2021-10-12 02:06:43 +02:00
Benjamin Otte
8950c0dc96 texture: Make format a property of GdkTexture
For MemoryTexture, this is a simple change.

For GLTexture, we need to query the format at texture creation. This
sounds like a bad idea and extra work until one realizes that we'd
need to do that anyway when using the texure the first time - either
when downloading, or when trying to use it in a rendernode, where we
will soon need that information to determine if the texture prefers high
depth.
2021-10-07 02:41:30 +02:00
Benjamin Otte
48781cf7f7 memorytexture: Split out GdkMemoryFormat handling
Also, now make gdk_memory_convert() the only conversion functions
and allow conversions between any 2 formats by going via a float[4].

This could be optimized via fast-paths, but so far it isn't.
2021-10-06 22:50:07 +02:00
Benjamin Otte
551f76ea69 gltexture: Fallback in download_float()
GLES only allows downloading float if the texture matches specific
criteria and I'm too lazy to determine them, so always fall back.

And the custom stride fallback code isn't necessary, because falling
back does exactly that step already.
2021-09-16 23:59:37 +02:00
Benjamin Otte
a4f3fbbda1 gltexture: Fix download() for GLES
GLES can't do glGetTexImage(), so implement a version that does
glReadPixels() on GLES.
2021-09-16 23:59:37 +02:00
Benjamin Otte
6785461c26 gltexture: Make sure downloading textures works in a different thread
This happens in the real world when using the inspector to look at a
node recording of a GStreamer video while the video is still playing.

GStreamer will use the GL context in a different thread while we are
busy trying to download it.

A test is included.
2021-09-13 01:40:03 +02:00
Benjamin Otte
719eafa60c gl: Implement uploading and downloading HDR formats
Also refactor the GL uploading so it does the fallback in a
GLES-compatible way, which means we only need one fallback.
2021-09-12 05:54:37 +02:00
Benjamin Otte
53275481a5 texture: Add gdk_texture_download_float() 2021-09-12 05:54:37 +02:00