GLContext#
Superclasses: DrawContext, Object
GdkGLContext is an object representing a platform-specific
OpenGL draw context.
GdkGLContexts are created for a surface using
create_gl_context, and the context will match
the characteristics of the surface.
A GdkGLContext is not tied to any particular normal framebuffer.
For instance, it cannot draw to the surface back buffer. The GDK
repaint system is in full control of the painting to that. Instead,
you can create render buffers or textures and use cairo_draw_from_gl
in the draw function of your widget to draw them. Then GDK will handle
the integration of your rendering with that of other widgets.
Support for GdkGLContext is platform-specific and context creation
can fail, returning None context.
A GdkGLContext has to be made “current” in order to start using
it, otherwise any OpenGL call will be ignored.
Creating a new OpenGL context#
In order to create a new GdkGLContext instance you need a GdkSurface,
which you typically get during the realize call of a widget.
A GdkGLContext is not realized until either make_current
or realize is called. It is possible to specify
details of the GL context like the OpenGL version to be used, or whether
the GL context should have extra state validation enabled after calling
create_gl_context by calling realize.
If the realization fails you have the option to change the settings of
the GdkGLContext and try again.
Using a GdkGLContext#
You will need to make the GdkGLContext the current context before issuing
OpenGL calls; the system sends OpenGL commands to whichever context is current.
It is possible to have multiple contexts, so you always need to ensure that
the one which you want to draw with is the current one before issuing commands:
gdk_gl_context_make_current (context);
You can now perform your drawing using OpenGL commands.
You can check which GdkGLContext is the current one by using
get_current; you can also unset any GdkGLContext
that is currently set by calling clear_current.
Methods#
- class GLContext
- classmethod clear_current() None#
Clears the current
GdkGLContext.Any OpenGL call after this function returns will be ignored until
make_currentis called.
- get_allowed_apis() GLAPI#
Gets the allowed APIs set via
set_allowed_apis().Added in version 4.6.
- get_api() GLAPI#
Gets the API currently in use.
If the renderer has not been realized yet, 0 is returned.
Added in version 4.6.
- get_debug_enabled() bool#
Retrieves whether the context is doing extra validations and runtime checking.
See
set_debug_enabled.
- get_required_version() tuple[int | None, int | None]#
Retrieves required OpenGL version set as a requirement for the
contextrealization. It will not change even if a greater OpenGL version is supported and used after thecontextis realized. Seeget_versionfor the real version in use.See
set_required_version.
Used to retrieves the
GdkGLContextthat thiscontextshare data with.As many contexts can share data now and no single shared context exists anymore, this function has been deprecated and now always returns
None.Deprecated since version 4.4:
- Use
is_sharedto check if contexts can be shared.
- Use
- get_version() tuple[int, int]#
Retrieves the OpenGL version of the
context.The
contextmust be realized prior to calling this function.
- is_legacy() bool#
Whether the
GdkGLContextis in legacy mode or not.The
GdkGLContextmust be realized before calling this function.When realizing a GL context, GDK will try to use the OpenGL 3.2 core profile; this profile removes all the OpenGL API that was deprecated prior to the 3.2 version of the specification. If the realization is successful, this function will return
False.If the underlying OpenGL implementation does not support core profiles, GDK will fall back to a pre-3.2 compatibility profile, and this function will return
True.You can use the value returned by this function to decide which kind of OpenGL API to use, or whether to do extension discovery, or what kind of shader programs to load.
Checks if the two GL contexts can share resources.
When they can, the texture IDs from
othercan be used inself. This is particularly useful when passingGdkGLTextureobjects between different contexts.Contexts created for the same display with the same properties will always be compatible, even if they are created for different surfaces. For other contexts it depends on the GL backend.
Both contexts must be realized for this check to succeed. If either one is not, this function will return
False.Added in version 4.4.
- Parameters:
other – the
GdkGLContextthat should be compatible withself
- realize() bool#
Realizes the given
GdkGLContext.It is safe to call this function on a realized
GdkGLContext.
- set_allowed_apis(apis: GLAPI) None#
Sets the allowed APIs. When
realize()is called, only the allowed APIs will be tried. If you set this to 0, realizing will always fail.If you set it on a realized context, the property will not have any effect. It is only relevant during
realize().By default, all APIs are allowed.
Added in version 4.6.
- Parameters:
apis – the allowed APIs
- set_debug_enabled(enabled: bool) None#
Sets whether the
GdkGLContextshould perform extra validations and runtime checking.This is useful during development, but has additional overhead.
The
GdkGLContextmust not be realized or made current prior to calling this function.- Parameters:
enabled – whether to enable debugging in the context
- set_forward_compatible(compatible: bool) None#
Sets whether the
GdkGLContextshould be forward-compatible.Forward-compatible contexts must not support OpenGL functionality that has been marked as deprecated in the requested version; non-forward compatible contexts, on the other hand, must support both deprecated and non deprecated functionality.
The
GdkGLContextmust not be realized or made current prior to calling this function.- Parameters:
compatible – whether the context should be forward-compatible
- set_required_version(major: int, minor: int) None#
Sets the major and minor version of OpenGL to request.
Setting
majorandminorto zero will use the default values.Setting
majorandminorlower than the minimum versions required by GTK will result in the context choosing the minimum version.The
contextmust not be realized or made current prior to calling this function.- Parameters:
major – the major version to request
minor – the minor version to request
- set_use_es(use_es: int) None#
Requests that GDK create an OpenGL ES context instead of an OpenGL one.
Not all platforms support OpenGL ES.
The
contextmust not have been realized.By default, GDK will attempt to automatically detect whether the underlying GL implementation is OpenGL or OpenGL ES once the
contextis realized.You should check the return value of
get_use_esafter callingrealizeto decide whether to use the OpenGL or OpenGL ES API, extensions, or shaders.- Parameters:
use_es – whether the context should use OpenGL ES instead of OpenGL, or -1 to allow auto-detection