Class GLContext

All Implemented Interfaces:
Proxy
Direct Known Subclasses:
GLContext.GLContext$Impl

@Generated("org.javagi.JavaGI") public abstract class GLContext extends DrawContext

Represents a platform-specific OpenGL draw context.

GdkGLContexts are created for a surface using Surface.createGlContext(), 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 Gdk.cairoDrawFromGl(Context, Surface, int, int, int, int, int, int, int) 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 null 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 makeCurrent() 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 Surface.createGlContext() 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 getCurrent(); you can also unset any GdkGLContext that is currently set by calling clearCurrent().

  • Constructor Details

    • GLContext

      public GLContext(MemorySegment address)
      Create a GLContext instance for the provided memory address.
      Parameters:
      address - the memory address of the native object
    • GLContext

      public GLContext()
      Create a new GLContext.
  • Method Details

    • getType

      public static @Nullable Type getType()
      Get the GType of the GLContext class.
      Returns:
      the GType
    • getMemoryLayout

      public static MemoryLayout getMemoryLayout()
      The memory layout of the native struct.
      Returns:
      the memory layout
    • asParent

      protected GLContext asParent()
      Return this instance as if it were its parent type. Comparable to the Java super keyword, but ensures the parent typeclass is also used in native code.
      Overrides:
      asParent in class DrawContext
      Returns:
      the instance as if it were its parent type
    • clearCurrent

      public static void clearCurrent()

      Clears the current GdkGLContext.

      Any OpenGL call after this function returns will be ignored until makeCurrent() is called.

    • getCurrent

      public static @Nullable GLContext getCurrent()
      Retrieves the current GdkGLContext.
      Returns:
      the current GdkGLContext
    • getAllowedApis

      public Set<GLAPI> getAllowedApis()
      Gets the allowed APIs set via gdk_gl_context_set_allowed_apis().
      Returns:
      the allowed APIs
      Since:
      4.6
    • getApi

      public Set<GLAPI> getApi()

      Gets the API currently in use.

      If the renderer has not been realized yet, 0 is returned.

      Returns:
      the currently used API
      Since:
      4.6
    • getDebugEnabled

      public boolean getDebugEnabled()

      Retrieves whether the context is doing extra validations and runtime checking.

      See setDebugEnabled(boolean).

      Returns:
      true if debugging is enabled
    • getDisplay

      public @Nullable Display getDisplay()
      Retrieves the display the this GLContext is created for
      Overrides:
      getDisplay in class DrawContext
      Returns:
      a GdkDisplay
    • getForwardCompatible

      public boolean getForwardCompatible()

      Retrieves whether the context is forward-compatible.

      See setForwardCompatible(boolean).

      Returns:
      true if the context should be forward-compatible
    • getRequiredVersion

      public void getRequiredVersion(@Nullable Out<Integer> major, @Nullable Out<Integer> minor)

      Retrieves required OpenGL version set as a requirement for the this GLContext realization. It will not change even if a greater OpenGL version is supported and used after the this GLContext is realized. See getVersion(Out, Out) for the real version in use.

      See setRequiredVersion(int, int).

      Parameters:
      major - return location for the major version to request
      minor - return location for the minor version to request
    • getSharedContext

      @Deprecated public @Nullable GLContext getSharedContext()
      Deprecated.
      Use isShared(GLContext) to check if contexts can be shared.

      Used to retrieves the GdkGLContext that this this GLContext share 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 null.

      Returns:
      null
    • getSurface

      public @Nullable Surface getSurface()
      Retrieves the surface used by the context.
      Overrides:
      getSurface in class DrawContext
      Returns:
      a GdkSurface
    • getUseEs

      public boolean getUseEs()
      Checks whether the this GLContext is using an OpenGL or OpenGL ES profile.
      Returns:
      true if the GdkGLContext is using an OpenGL ES profile; false if other profile is in use of if the this GLContext has not yet been realized.
    • getVersion

      public void getVersion(Out<Integer> major, Out<Integer> minor)

      Retrieves the OpenGL version of the context.

      The this GLContext must be realized prior to calling this function.

      Parameters:
      major - return location for the major version
      minor - return location for the minor version
    • isLegacy

      public boolean isLegacy()

      Whether the GdkGLContext is in legacy mode or not.

      The GdkGLContext must 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.

      Returns:
      true if the GL context is in legacy mode
    • isShared

      public boolean isShared(GLContext other)

      Checks if the two GL contexts can share resources.

      When they can, the texture IDs from other can be used in self. This is particularly useful when passing GdkGLTexture objects 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.

      Parameters:
      other - the GdkGLContext that should be compatible with this GLContext
      Returns:
      true if the two GL contexts are compatible.
      Since:
      4.4
    • makeCurrent

      public void makeCurrent()
      Makes the this GLContext the current one.
    • realize

      public boolean realize() throws GErrorException

      Realizes the given GdkGLContext.

      It is safe to call this function on a realized GdkGLContext.

      Returns:
      true if the context is realized
      Throws:
      GErrorException - see GError
    • setAllowedApis

      public void setAllowedApis(Set<GLAPI> apis)

      Sets the allowed APIs. When gdk_gl_context_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 gdk_gl_context_realize().

      By default, all APIs are allowed.

      Parameters:
      apis - the allowed APIs
      Since:
      4.6
    • setAllowedApis

      public void setAllowedApis(GLAPI... apis)

      Sets the allowed APIs. When gdk_gl_context_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 gdk_gl_context_realize().

      By default, all APIs are allowed.

      Parameters:
      apis - the allowed APIs
      Since:
      4.6
    • setDebugEnabled

      public void setDebugEnabled(boolean enabled)

      Sets whether the GdkGLContext should perform extra validations and runtime checking.

      This is useful during development, but has additional overhead.

      The GdkGLContext must not be realized or made current prior to calling this function.

      Parameters:
      enabled - whether to enable debugging in the context
    • setForwardCompatible

      public void setForwardCompatible(boolean compatible)

      Sets whether the GdkGLContext should 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 GdkGLContext must not be realized or made current prior to calling this function.

      Parameters:
      compatible - whether the context should be forward-compatible
    • setRequiredVersion

      public void setRequiredVersion(int major, int minor)

      Sets the major and minor version of OpenGL to request.

      Setting major and minor to zero will use the default values.

      Setting major and minor lower than the minimum versions required by GTK will result in the context choosing the minimum version.

      The this GLContext must not be realized or made current prior to calling this function.

      Parameters:
      major - the major version to request
      minor - the minor version to request
    • setUseEs

      public void setUseEs(int useEs)

      Requests that GDK create an OpenGL ES context instead of an OpenGL one.

      Not all platforms support OpenGL ES.

      The this GLContext must not have been realized.

      By default, GDK will attempt to automatically detect whether the underlying GL implementation is OpenGL or OpenGL ES once the this GLContext is realized.

      You should check the return value of getUseEs() after calling realize() to decide whether to use the OpenGL or OpenGL ES API, extensions, or shaders.

      Parameters:
      useEs - whether the context should use OpenGL ES instead of OpenGL, or -1 to allow auto-detection