class Gdk::GLContext


Gdk::GLContext is an object representing a platform-specific OpenGL draw context.

Gdk::GLContexts are created for a surface using Gdk::Surface#create_gl_context, and the context will match the characteristics of the surface.

A Gdk::GLContext 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 Gdk::GLContext is platform-specific and context creation can fail, returning nil context.

A Gdk::GLContext 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 Gdk::GLContext instance you need a Gdk::Surface, which you typically get during the realize call of a widget.

A Gdk::GLContext is not realized until either Gdk::GLContext#make_current or Gdk::GLContext#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 Gdk::Surface#create_gl_context by calling Gdk::GLContext#realize. If the realization fails you have the option to change the settings of the Gdk::GLContext and try again.

Using a Gdk::GLContext

You will need to make the Gdk::GLContext 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:

WARNING ⚠️ The following code is in c ⚠️

gdk_gl_context_make_current (context);

You can now perform your drawing using OpenGL commands.

You can check which Gdk::GLContext is the current one by using Gdk::GLContext#current; you can also unset any Gdk::GLContext that is currently set by calling Gdk::GLContext#clear_current.

Defined in:



Class Method Summary

Instance Method Summary

Instance methods inherited from class Gdk::DrawContext

==(other : self) ==, begin_frame(region : Cairo::Region) : Nil begin_frame, display : Gdk::Display | Nil display, display=(value : Gdk::Display | Nil) : Gdk::Display | Nil display=, end_frame : Nil end_frame, frame_region : Cairo::Region | Nil frame_region, hash(hasher) hash, is_in_frame : Bool is_in_frame, surface : Gdk::Surface | Nil surface, surface=(value : Gdk::Surface | Nil) : Gdk::Surface | Nil surface=

Constructor methods inherited from class Gdk::DrawContext

new(*, display : Gdk::Display | Nil = nil, surface : Gdk::Surface | Nil = nil)

Class methods inherited from class Gdk::DrawContext

g_type : UInt64 g_type

Instance methods inherited from class GObject::Object

==(other : self) ==, bind_property(source_property : String, target : GObject::Object, target_property : String, flags : GObject::BindingFlags) : GObject::Binding bind_property, bind_property_full(source_property : String, target : GObject::Object, target_property : String, flags : GObject::BindingFlags, transform_to : GObject::Closure, transform_from : GObject::Closure) : GObject::Binding bind_property_full, data(key : String) : Pointer(Void) | Nil data, finalize finalize, freeze_notify : Nil freeze_notify, getv(names : Enumerable(String), values : Enumerable(_)) : Nil getv, hash(hasher) hash, notify(property_name : String) : Nil notify, notify_by_pspec(pspec : GObject::ParamSpec) : Nil notify_by_pspec, notify_signal notify_signal, property(property_name : String, value : _) : Nil property, qdata(quark : UInt32) : Pointer(Void) | Nil qdata, ref_count : UInt32 ref_count, run_dispose : Nil run_dispose, set_data(key : String, data : Pointer(Void) | Nil) : Nil set_data, set_property(property_name : String, value : _) : Nil set_property, steal_data(key : String) : Pointer(Void) | Nil steal_data, steal_qdata(quark : UInt32) : Pointer(Void) | Nil steal_qdata, thaw_notify : Nil thaw_notify, to_unsafe : Pointer(Void) to_unsafe, watch_closure(closure : GObject::Closure) : Nil watch_closure

Constructor methods inherited from class GObject::Object

cast(obj : GObject::Object) : self cast, new(pointer : Pointer(Void), transfer : GICrystal::Transfer)
, newv(object_type : UInt64, parameters : Enumerable(GObject::Parameter)) : self newv

Class methods inherited from class GObject::Object

cast?(obj : GObject::Object) : self | Nil cast?, compat_control(what : UInt64, data : Pointer(Void) | Nil) : UInt64 compat_control, g_type : UInt64 g_type, interface_find_property(g_iface : GObject::TypeInterface, property_name : String) : GObject::ParamSpec interface_find_property, interface_list_properties(g_iface : GObject::TypeInterface) : Enumerable(GObject::ParamSpec) interface_list_properties

Macros inherited from class GObject::Object

previous_vfunc(*args) previous_vfunc, previous_vfunc!(*args) previous_vfunc!, signal(signature) signal

Constructor Detail

def #

Initialize a new GLContext.

[View source]
def*, allowed_apis : Gdk::GLAPI | Nil = nil, api : Gdk::GLAPI | Nil = nil, display : Gdk::Display | Nil = nil, shared_context : Gdk::GLContext | Nil = nil, surface : Gdk::Surface | Nil = nil) #

[View source]

Class Method Detail

def self.clear_current : Nil #

Clears the current Gdk::GLContext.

Any OpenGL call after this function returns will be ignored until Gdk::GLContext#make_current is called.

[View source]
def self.current : Gdk::GLContext | Nil #

Retrieves the current Gdk::GLContext.

[View source]
def self.g_type : UInt64 #

Returns the type id (GType) registered in GLib type system.

[View source]

Instance Method Detail

def ==(other : self) #
Description copied from class Reference

Returns true if this reference is the same as other. Invokes same?.

def allowed_apis : Gdk::GLAPI #

Gets the allowed APIs set via gdk_gl_context_set_allowed_apis().

[View source]
def allowed_apis=(apis : Gdk::GLAPI) : Nil #

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.

[View source]
def api : Gdk::GLAPI #

Gets the API currently in use.

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

[View source]
def debug_enabled : Bool #

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

See Gdk::GLContext#debug_enabled=.

[View source]
def debug_enabled=(enabled : Bool) : Nil #

Sets whether the Gdk::GLContext should perform extra validations and runtime checking.

This is useful during development, but has additional overhead.

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

[View source]
def display : Gdk::Display | Nil #

Retrieves the display the context is created for

[View source]
def forward_compatible : Bool #

Retrieves whether the context is forward-compatible.

See Gdk::GLContext#forward_compatible=.

[View source]
def forward_compatible=(compatible : Bool) : Nil #

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

[View source]
def hash(hasher) #
Description copied from class Reference

See Object#hash(hasher)

def is_legacy : Bool #

Whether the Gdk::GLContext is in legacy mode or not.

The Gdk::GLContext 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.

[View source]
def is_shared(other : Gdk::GLContext) : Bool #

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 Gdk::GLTexture 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.

[View source]
def make_current : Nil #

Makes the context the current one.

[View source]
def realize : Bool #

Realizes the given Gdk::GLContext.

It is safe to call this function on a realized Gdk::GLContext.

[View source]
def required_version(major : Int32 | Nil, minor : Int32 | Nil) : Nil #

Retrieves required OpenGL version.

See Gdk::GLContext#required_version=.

[View source]
def set_required_version(major : Int32, minor : Int32) : Nil #

Sets the major and minor version of OpenGL to request.

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

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

[View source]
def shared_context : Gdk::GLContext | Nil #

Used to retrieves the Gdk::GLContext that this context 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 nil.


[View source]
def shared_context=(value : Gdk::GLContext | Nil) : Gdk::GLContext | Nil #

[View source]
def surface : Gdk::Surface | Nil #

Retrieves the surface used by the context.

[View source]
def use_es : Bool #

Checks whether the context is using an OpenGL or OpenGL ES profile.

[View source]
def use_es=(use_es : Int32) : Nil #

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

Not all platforms support OpenGL ES.

The context 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 context is realized.

You should check the return value of Gdk::GLContext#use_es after calling Gdk::GLContext#realize to decide whether to use the OpenGL or OpenGL ES API, extensions, or shaders.

[View source]
def version(major : Int32, minor : Int32) : Nil #

Retrieves the OpenGL version of the context.

The context must be realized prior to calling this function.

[View source]