Commit 038aac62 authored by Alexander Larsson's avatar Alexander Larsson Committed by Matthias Clasen

gdk: Add support for OpenGL

This adds the new type GdkGLContext that wraps an OpenGL context for a
particular native window. It also adds support for the gdk paint
machinery to use OpenGL to draw everything. As soon as anyone creates
a GL context for a native window we create a "paint context" for that
GdkWindow and switch to using GL for painting it.

This commit contains only an implementation for X11 (using GLX).

The way painting works is that all client gl contexts draw into
offscreen buffers rather than directly to the back buffer, and the
way something gets onto the window is by using gdk_cairo_draw_from_gl()
to draw part of that buffer onto the draw cairo context.

As a fallback (if we're doing redirected drawing or some effect like a
cairo_push_group()) we read back the gl buffer into memory and composite
using cairo. This means that GL rendering works in all cases, including
rendering to a PDF. However, this is not particularly fast.

In the *typical* case, where we're drawing directly to the window in
the regular paint loop we hit the fast path. The fast path uses opengl
to draw the buffer to the window back buffer, either by blitting or
texturing. Then we track the region that was drawn, and when the draw
ends we paint the normal cairo surface to the window (using
texture-from-pixmap in the X11 case, or texture from cairo image
otherwise) in the regions where there is no gl painted.

There are some complexities wrt layering of gl and cairo areas though:
* We track via gdk_window_mark_paint_from_clip() whenever gtk is
  painting over a region we previously rendered with opengl
  (flushed_region). This area (needs_blend_region) is blended
  rather than copied at the end of the frame.
* If we're drawing a gl texture with alpha we first copy the current
  cairo_surface inside the target region to the back buffer before
  we blend over it.

These two operations allow us full stacking of transparent gl and cairo
regions.
parent d0147a6f
......@@ -32,6 +32,7 @@
<xi:include href="xml/windows.xml" />
<xi:include href="xml/gdkframeclock.xml" />
<xi:include href="xml/gdkframetimings.xml" />
<xi:include href="xml/gdkglcontext.xml" />
<xi:include href="xml/events.xml" />
<xi:include href="xml/event_structs.xml" />
<xi:include href="xml/keys.xml" />
......
......@@ -394,6 +394,7 @@ gdk_window_constrain_size
gdk_window_beep
gdk_window_get_scale_factor
gdk_window_set_opaque_region
gdk_window_create_gl_context
gdk_window_mark_paint_from_clip
<SUBSECTION>
......@@ -618,6 +619,7 @@ gdk_cairo_rectangle
gdk_cairo_region
gdk_cairo_region_create_from_surface
gdk_cairo_surface_create_from_pixbuf
gdk_cairo_draw_from_gl
</SECTION>
<SECTION>
......@@ -1005,6 +1007,7 @@ gdk_x11_display_error_trap_pop
gdk_x11_display_error_trap_pop_ignored
gdk_x11_display_set_cursor_theme
gdk_x11_display_set_window_scale
gdk_x11_display_get_glx_version
gdk_x11_register_standard_event_type
gdk_x11_screen_get_screen_number
gdk_x11_screen_get_xscreen
......@@ -1124,6 +1127,11 @@ GDK_X11_KEYMAP_CLASS
GDK_IS_X11_KEYMAP
GDK_IS_X11_KEYMAP_CLASS
GDK_X11_KEYMAP_GET_CLASS
GDK_TYPE_X11_GL_CONTEXT
GDK_X11_GL_CONTEXT
GDK_X11_GL_CONTEXT_CLASS
GDK_IS_X11_GL_CONTEXT
GDK_IS_X11_GL_CONTEXT_CLASS
GDK_TYPE_X11_SCREEN
GDK_X11_SCREEN
GDK_X11_SCREEN_CLASS
......@@ -1274,3 +1282,26 @@ gdk_frame_timings_get_predicted_presentation_time
<SUBSECTION Private>
gdk_frame_get_type
</SECTION>
<SECTION>
<FILE>gdkglcontext</FILE>
GdkGLContext
gdk_gl_context_get_visual
gdk_gl_context_get_window
gdk_gl_context_make_current
gdk_gl_context_clear_current
gdk_gl_context_get_current
GDK_GL_ERROR
GDK_TYPE_GL_ERROR
GDK_TYPE_GL_PROFILE
<SUBSECTION Standard>
GDK_GL_CONTEXT
GDK_IS_GL_CONTEXT
GDK_TYPE_GL_CONTEXT
GDK_GL_CONTEXT_CLASS
GDK_GL_CONTEXT_GET_CLASS
GDK_IS_GL_CONTEXT_CLASS
gdk_gl_context_get_type
gdk_gl_error_quark
</SECTION>
......@@ -8,6 +8,7 @@ gdk_display_get_type
gdk_display_manager_get_type
gdk_drag_context_get_type
gdk_frame_clock_get_type
gdk_gl_context_get_type
gdk_keymap_get_type
gdk_screen_get_type
gdk_visual_get_type
......
......@@ -71,6 +71,7 @@ gdk_public_h_sources = \
gdkdnd.h \
gdkevents.h \
gdkframetimings.h \
gdkglcontext.h \
gdkkeys.h \
gdkkeysyms.h \
gdkkeysyms-compat.h \
......@@ -107,6 +108,7 @@ gdk_private_headers = \
gdkdndprivate.h \
gdkframeclockidle.h \
gdkframeclockprivate.h \
gdkglcontextprivate.h \
gdkscreenprivate.h \
gdkinternals.h \
gdkintl.h \
......@@ -131,6 +133,8 @@ gdk_c_sources = \
gdkdnd.c \
gdkevents.c \
gdkframetimings.c \
gdkgl.c \
gdkglcontext.c \
gdkglobals.c \
gdkkeys.c \
gdkkeyuni.c \
......
......@@ -148,7 +148,8 @@ static const GDebugKey gdk_debug_keys[] = {
{"draw", GDK_DEBUG_DRAW},
{"eventloop", GDK_DEBUG_EVENTLOOP},
{"frames", GDK_DEBUG_FRAMES},
{"settings", GDK_DEBUG_SETTINGS}
{"settings", GDK_DEBUG_SETTINGS},
{"opengl", GDK_DEBUG_OPENGL},
};
static gboolean
......
......@@ -41,6 +41,7 @@
#include <gdk/gdkevents.h>
#include <gdk/gdkframeclock.h>
#include <gdk/gdkframetimings.h>
#include <gdk/gdkglcontext.h>
#include <gdk/gdkkeys.h>
#include <gdk/gdkkeysyms.h>
#include <gdk/gdkmain.h>
......
......@@ -70,6 +70,17 @@ GDK_AVAILABLE_IN_3_10
cairo_surface_t * gdk_cairo_surface_create_from_pixbuf (const GdkPixbuf *pixbuf,
int scale,
GdkWindow *for_window);
GDK_AVAILABLE_IN_3_16
void gdk_cairo_draw_from_gl (cairo_t *cr,
GdkWindow *window,
int source,
int source_type,
int buffer_scale,
int x,
int y,
int width,
int height);
G_END_DECLS
......
......@@ -2228,3 +2228,68 @@ gdk_error_trap_pop (void)
{
return gdk_error_trap_pop_internal (TRUE);
}
/*< private >
* gdk_display_destroy_gl_context:
* @display: a #GdkDisplay
* @context: a #GdkGLContext
*
* Destroys the platform-specific parts of the @context.
*
* The @context instance is still valid, though inert, after
* this functionr returns.
*/
void
gdk_display_destroy_gl_context (GdkDisplay *display,
GdkGLContext *context)
{
GdkGLContext *current = gdk_display_get_current_gl_context (display);
if (current == context)
g_object_set_data (G_OBJECT (display), "-gdk-gl-current-context", NULL);
GDK_DISPLAY_GET_CLASS (display)->destroy_gl_context (display, context);
}
/*< private >
* gdk_display_make_gl_context_current:
* @display: a #GdkDisplay
* @context: (optional): a #GdkGLContext, or %NULL
*
* Makes the given @context the current GL context, or unsets
* the current GL context if @context is %NULL.
*
* Returns: %TRUE if successful
*/
gboolean
gdk_display_make_gl_context_current (GdkDisplay *display,
GdkGLContext *context)
{
GdkGLContext *current = gdk_display_get_current_gl_context (display);
if (current == context)
return TRUE;
if (context == NULL)
g_object_set_data (G_OBJECT (display), "-gdk-gl-current-context", NULL);
else
g_object_set_data_full (G_OBJECT (display), "-gdk-gl-current-context",
g_object_ref (context),
(GDestroyNotify) g_object_unref);
return GDK_DISPLAY_GET_CLASS (display)->make_gl_context_current (display, context);
}
/*< private >
* gdk_display_get_current_gl_context:
* @display: a #GdkDisplay
*
* Retrieves the current #GdkGLContext associated with @display.
*
* Returns: (transfer none): the current #GdkGLContext or %NULL
*/
GdkGLContext *
gdk_display_get_current_gl_context (GdkDisplay *display)
{
return g_object_get_data (G_OBJECT (display), "-gdk-gl-current-context");
}
......@@ -225,6 +225,11 @@ struct _GdkDisplayClass
gchar * (*utf8_to_string_target) (GdkDisplay *display,
const gchar *text);
gboolean (*make_gl_context_current) (GdkDisplay *display,
GdkGLContext *context);
void (*destroy_gl_context) (GdkDisplay *display,
GdkGLContext *context);
/* Signals */
void (*opened) (GdkDisplay *display);
void (*closed) (GdkDisplay *display,
......@@ -303,6 +308,12 @@ void _gdk_display_create_window_impl (GdkDisplay *display
gint attributes_mask);
GdkWindow * _gdk_display_create_window (GdkDisplay *display);
void gdk_display_destroy_gl_context (GdkDisplay *display,
GdkGLContext *context);
gboolean gdk_display_make_gl_context_current (GdkDisplay *display,
GdkGLContext *context);
GdkGLContext * gdk_display_get_current_gl_context (GdkDisplay *display);
G_END_DECLS
#endif /* __GDK_DISPLAY_PRIVATE_H__ */
/* GDK - The GIMP Drawing Kit
* Copyright (C) 2005 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "gdkcairo.h"
#include "gdkglcontextprivate.h"
#include "gdkinternals.h"
#include <epoxy/gl.h>
#include <math.h>
static cairo_user_data_key_t direct_key;
void
gdk_cairo_surface_mark_as_direct (cairo_surface_t *surface,
GdkWindow *window)
{
cairo_surface_set_user_data (surface, &direct_key,
g_object_ref (window), g_object_unref);
}
/* x,y,width,height describes a rectangle in the gl render buffer
coordinate space, and its top left corner is drawn at the current
position according to the cairo translation. */
/**
* gdk_cairo_draw_from_gl
* @cr: a cairo context
* @window: The window we're rendering for (not necessarily into)
* @source: The GL id of the source buffer
* @source_type: The type of the @source
* @buffer_scale: The scale-factor that the @source buffer is allocated for
* @x: The source x position in @source to start copying from in GL coordinates
* @y: The source y position in @source to start copying from in GL coordinates
* @width: The width of the region to draw
* @height: The height of the region to draw
*
* This is the main way to draw GL content in Gtk+. It takes a render buffer id
* (@source_type == #GL_RENDERBUFFER) or a texture id (@source_type == #GL_TEXTURE)
* and draws it onto @cr with an OVER operation, respecting the current clip.
*
* This will work for *all* cairo_t, as long as @window is realized, but the
* fallback implementation that reads back the pixels from the buffer may be
* used in the general case. In the case of direct drawing to a window with
* no special effects applied to @cr it will however use a more efficient
* approach.
*
* For #GL_RENDERBUFFER the code will always fall back to software for buffers
* with alpha components, so make sure you use #GL_TEXTURE if using alpha.
*/
void
gdk_cairo_draw_from_gl (cairo_t *cr,
GdkWindow *window,
int source,
int source_type,
int buffer_scale,
int x,
int y,
int width,
int height)
{
GdkGLContext *context;
cairo_surface_t *image;
cairo_matrix_t matrix;
int dx, dy, window_scale;
gboolean trivial_transform;
cairo_surface_t *group_target;
GdkWindow *direct_window, *impl_window;
GLuint framebuffer;
GLint alpha_size = 0;
cairo_region_t *clip_region;
impl_window = window->impl_window;
window_scale = gdk_window_get_scale_factor (impl_window);
context = gdk_window_get_paint_gl_context (window, NULL);
if (context == NULL)
{
g_warning ("gdk_cairo_draw_gl_render_buffer failed - no paint context");
return;
}
clip_region = gdk_cairo_region_from_clip (cr);
if (!gdk_gl_context_make_current (context))
g_error ("make current failed");
glGenFramebuffersEXT (1, &framebuffer);
glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, framebuffer);
if (source_type == GL_RENDERBUFFER)
{
glGetRenderbufferParameteriv (GL_RENDERBUFFER, GL_RENDERBUFFER_ALPHA_SIZE, &alpha_size);
glBindRenderbufferEXT (GL_RENDERBUFFER_EXT, source);
glFramebufferRenderbufferEXT (GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
GL_RENDERBUFFER_EXT, source);
glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, 0);
}
else if (source_type == GL_TEXTURE)
{
glGetTexLevelParameteriv (GL_TEXTURE_2D, 0, GL_TEXTURE_ALPHA_SIZE, &alpha_size);
glBindTexture (GL_TEXTURE_2D, source);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glFramebufferTexture2DEXT (GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
GL_TEXTURE_2D, source, 0);
glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, 0);
}
else
{
g_warning ("Unsupported gl source type %d\n", source_type);
return;
}
group_target = cairo_get_group_target (cr);
direct_window = cairo_surface_get_user_data (group_target, &direct_key);
cairo_get_matrix (cr, &matrix);
dx = matrix.x0;
dy = matrix.y0;
/* Trivial == integer-only translation */
trivial_transform =
(double)dx == matrix.x0 && (double)dy == matrix.y0 &&
matrix.xx == 1.0 && matrix.xy == 0.0 &&
matrix.yx == 0.0 && matrix.yy == 1.0;
/* For direct paint of non-alpha renderbuffer, we can
just do a bitblit */
if (source_type == GL_RENDERBUFFER &&
alpha_size == 0 &&
direct_window != NULL &&
direct_window->current_paint.use_gl &&
trivial_transform &&
clip_region != NULL)
{
int window_height;
int i;
/* Translate to impl coords */
cairo_region_translate (clip_region, dx, dy);
glEnable (GL_SCISSOR_TEST);
window_height = gdk_window_get_height (impl_window);
glDrawBuffer (GL_BACK);
#define FLIP_Y(_y) (window_height*window_scale - (_y))
for (i = 0; i < cairo_region_num_rectangles (clip_region); i++)
{
cairo_rectangle_int_t clip_rect, dest;
cairo_region_get_rectangle (clip_region, i, &clip_rect);
clip_rect.x *= window_scale;
clip_rect.y *= window_scale;
clip_rect.width *= window_scale;
clip_rect.height *= window_scale;
glScissor (clip_rect.x, FLIP_Y (clip_rect.y + clip_rect.height),
clip_rect.width, clip_rect.height);
dest.x = dx * window_scale;
dest.y = dy * window_scale;
dest.width = width * window_scale / buffer_scale;
dest.height = height * window_scale / buffer_scale;
if (gdk_rectangle_intersect (&clip_rect, &dest, &dest))
{
int clipped_src_x = x + (dest.x - dx * window_scale);
int clipped_src_y = y + (height - dest.height - (dest.y - dy * window_scale));
glBlitFramebufferEXT(clipped_src_x, clipped_src_y,
(clipped_src_x + dest.width), (clipped_src_y + dest.height),
dest.x, FLIP_Y(dest.y + dest.height),
dest.x + dest.width, FLIP_Y(dest.y),
GL_COLOR_BUFFER_BIT, GL_NEAREST);
if (impl_window->current_paint.flushed_region)
{
cairo_rectangle_int_t flushed_rect;
flushed_rect.x = dest.x / window_scale;
flushed_rect.y = dest.y / window_scale;
flushed_rect.width = (dest.x + dest.width + window_scale - 1) / window_scale - flushed_rect.x;
flushed_rect.height = (dest.y + dest.height + window_scale - 1) / window_scale - flushed_rect.y;
cairo_region_union_rectangle (impl_window->current_paint.flushed_region,
&flushed_rect);
cairo_region_subtract_rectangle (impl_window->current_paint.need_blend_region,
&flushed_rect);
}
}
}
glDisable (GL_SCISSOR_TEST);
#undef FLIP_Y
}
/* For direct paint of alpha or non-alpha textures we can use texturing */
else if (source_type == GL_TEXTURE &&
direct_window != NULL &&
direct_window->current_paint.use_gl &&
trivial_transform &&
clip_region != NULL)
{
int window_height;
GLint texture_width;
GLint texture_height;
int i;
/* Translate to impl coords */
cairo_region_translate (clip_region, dx, dy);
glGetTexLevelParameteriv (GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &texture_width);
glGetTexLevelParameteriv (GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &texture_height);
if (alpha_size != 0)
{
cairo_region_t *opaque_region, *blend_region;
opaque_region = cairo_region_copy (clip_region);
cairo_region_subtract (opaque_region, impl_window->current_paint.flushed_region);
cairo_region_subtract (opaque_region, impl_window->current_paint.need_blend_region);
if (!cairo_region_is_empty (opaque_region))
gdk_gl_texture_from_surface (impl_window->current_paint.surface,
opaque_region);
blend_region = cairo_region_copy (clip_region);
cairo_region_intersect (blend_region, impl_window->current_paint.need_blend_region);
glEnable (GL_BLEND);
if (!cairo_region_is_empty (blend_region))
gdk_gl_texture_from_surface (impl_window->current_paint.surface,
blend_region);
cairo_region_destroy (opaque_region);
cairo_region_destroy (blend_region);
}
glEnable (GL_SCISSOR_TEST);
glEnable (GL_TEXTURE_2D);
window_height = gdk_window_get_height (impl_window);
#define FLIP_Y(_y) (window_height*window_scale - (_y))
for (i = 0; i < cairo_region_num_rectangles (clip_region); i++)
{
cairo_rectangle_int_t clip_rect, dest;
cairo_region_get_rectangle (clip_region, i, &clip_rect);
clip_rect.x *= window_scale;
clip_rect.y *= window_scale;
clip_rect.width *= window_scale;
clip_rect.height *= window_scale;
glScissor (clip_rect.x, FLIP_Y (clip_rect.y + clip_rect.height),
clip_rect.width, clip_rect.height);
dest.x = dx * window_scale;
dest.y = dy * window_scale;
dest.width = width * window_scale / buffer_scale;
dest.height = height * window_scale / buffer_scale;
if (gdk_rectangle_intersect (&clip_rect, &dest, &dest))
{
int clipped_src_x = x + (dest.x - dx * window_scale);
int clipped_src_y = y + (height - dest.height - (dest.y - dy * window_scale));
glBegin (GL_QUADS);
glTexCoord2f (clipped_src_x / (float)texture_width, clipped_src_y / (float)texture_height);
glVertex2f (dest.x, FLIP_Y(dest.y + dest.height));
glTexCoord2f ((clipped_src_x + dest.width) / (float)texture_width, clipped_src_y / (float)texture_height);
glVertex2f (dest.x + dest.width, FLIP_Y(dest.y + dest.height));
glTexCoord2f ((clipped_src_x + dest.width) / (float)texture_width, (clipped_src_y + dest.height) / (float)texture_height);
glVertex2f (dest.x + dest.width, FLIP_Y(dest.y));
glTexCoord2f (clipped_src_x / (float)texture_width, (clipped_src_y + dest.height) / (float)texture_height);
glVertex2f (dest.x, FLIP_Y(dest.y));
glEnd();
if (impl_window->current_paint.flushed_region)
{
cairo_rectangle_int_t flushed_rect;
flushed_rect.x = dest.x / window_scale;
flushed_rect.y = dest.y / window_scale;
flushed_rect.width = (dest.x + dest.width + window_scale - 1) / window_scale - flushed_rect.x;
flushed_rect.height = (dest.y + dest.height + window_scale - 1) / window_scale - flushed_rect.y;
cairo_region_union_rectangle (impl_window->current_paint.flushed_region,
&flushed_rect);
cairo_region_subtract_rectangle (impl_window->current_paint.need_blend_region,
&flushed_rect);
}
}
}
if (alpha_size != 0)
glDisable (GL_BLEND);
glDisable (GL_TEXTURE_2D);
glDisable (GL_SCISSOR_TEST);
#undef FLIP_Y
}
else
{
/* Software fallback */
/* TODO: avoid reading back non-required data due to dest clip */
image = cairo_surface_create_similar_image (cairo_get_target (cr),
(alpha_size == 0) ? CAIRO_FORMAT_RGB24 : CAIRO_FORMAT_ARGB32,
width, height);
#ifdef HAVE_CAIRO_SURFACE_SET_DEVICE_SCALE
cairo_surface_set_device_scale (image, buffer_scale, buffer_scale);
#endif
glPixelStorei (GL_PACK_ALIGNMENT, 4);
glPixelStorei (GL_PACK_ROW_LENGTH, cairo_image_surface_get_stride (image) / 4);
glReadPixels (x, y, width, height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV,
cairo_image_surface_get_data (image));
glPixelStorei (GL_PACK_ROW_LENGTH, 0);
cairo_surface_mark_dirty (image);
/* Invert due to opengl having different origin */
cairo_scale (cr, 1, -1);
cairo_translate (cr, 0, -height / buffer_scale);
cairo_set_source_surface (cr, image, 0, 0);
cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
cairo_paint (cr);
cairo_surface_destroy (image);
}
glDrawBuffer (GL_BACK);
glReadBuffer(GL_BACK);
glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, 0);
glDeleteFramebuffersEXT (1, &framebuffer);
if (clip_region)
cairo_region_destroy (clip_region);
}
void
gdk_gl_texture_from_surface (cairo_surface_t *surface,
cairo_region_t *region)
{
GdkGLContext *current;
cairo_surface_t *image;
double device_x_offset, device_y_offset;
cairo_rectangle_int_t rect, e;
int n_rects, i;
GdkWindow *window;
int window_height;
unsigned int texture_id;
int window_scale;
double sx, sy;
current = gdk_gl_context_get_current ();
if (current &&
GDK_GL_CONTEXT_GET_CLASS (current)->texture_from_surface &&
GDK_GL_CONTEXT_GET_CLASS (current)->texture_from_surface (current, surface, region))
return;
/* Software fallback */
window = gdk_gl_context_get_window (gdk_gl_context_get_current ());
window_scale = gdk_window_get_scale_factor (window);
window_height = gdk_window_get_height (window);
sx = sy = 1;
#ifdef HAVE_CAIRO_SURFACE_SET_DEVICE_SCALE
cairo_surface_get_device_scale (window->current_paint.surface, &sx, &sy);
#endif
cairo_surface_get_device_offset (surface,
&device_x_offset, &device_y_offset);
glGenTextures (1, &texture_id);
glBindTexture (GL_TEXTURE_RECTANGLE_ARB, texture_id);
glEnable (GL_TEXTURE_RECTANGLE_ARB);
n_rects = cairo_region_num_rectangles (region);
for (i = 0; i < n_rects; i++)
{
cairo_region_get_rectangle (region, i, &rect);
glScissor (rect.x * window_scale, (window_height - rect.y - rect.height) * window_scale,
rect.width * window_scale, rect.height * window_scale);
e = rect;
e.x *= sx;
e.y *= sy;
e.x += (int)device_x_offset;
e.y += (int)device_y_offset;
e.width *= sx;
e.height *= sy;
image = cairo_surface_map_to_image (surface, &e);
glPixelStorei (GL_UNPACK_ALIGNMENT, 4);
glPixelStorei (GL_UNPACK_ROW_LENGTH, cairo_image_surface_get_stride (image)/4);
glTexImage2D (GL_TEXTURE_RECTANGLE_ARB, 0, 4, e.width, e.height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV,
cairo_image_surface_get_data (image));
glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
cairo_surface_unmap_image (surface, image);
#define FLIP_Y(_y) (window_height - (_y))
glBegin (GL_QUADS);
glTexCoord2f (0.0f * sx, rect.height * sy);
glVertex2f (rect.x * window_scale, FLIP_Y(rect.y + rect.height) * window_scale);
glTexCoord2f (rect.width * sx, rect.height * sy);
glVertex2f ((rect.x + rect.width) * window_scale, FLIP_Y(rect.y + rect.height) * window_scale);
glTexCoord2f (rect.width * sx, 0.0f * sy);
glVertex2f ((rect.x + rect.width) * window_scale, FLIP_Y(rect.y) * window_scale);
glTexCoord2f (0.0f * sx, 0.0f * sy);
glVertex2f (rect.x * window_scale, FLIP_Y(rect.y) * window_scale);
glEnd();
}
glDisable (GL_TEXTURE_RECTANGLE_ARB);
glDeleteTextures (1, &texture_id);
}
/* GDK - The GIMP Drawing Kit
*
* gdkglcontext.c: GL context abstraction
*
* Copyright © 2014 Emmanuele Bassi
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* SECTION:gdkglcontext
* @Title: GdkGLContext
* @Short_description: OpenGL context
*
* #GdkGLContext is an object representing the platform-specific
* OpenGL drawing context.
*
* #GdkGLContexts are created for a #GdkWindow using gdk_window_create_gl_context(), and
* the context will be tied to the native window backing that window, matching the
* GdkVisual of the window.
*
* A #GdkGLContexts normal framebuffer draws directly on to the back buffer of the native
* window backing the #GdkWindow, so its not allowed to draw directly to that, as the
* gdk repaint system is in full control of that. Instead you can create render buffers
* or textures and use gdk_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, context creation can fail, returning
* a %NULL context.
*
* A #GdkGLContext has to be made "current" in order to start using
* it, otherwise any OpenGL call will be ignored.
*