2014-10-09 08:45:44 +00:00
|
|
|
/* GDK - The GIMP Drawing Kit
|
2014-10-12 03:04:40 +00:00
|
|
|
* Copyright (C) 2014 Red Hat, Inc.
|
2014-10-09 08:45:44 +00:00
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
|
2021-09-24 19:11:00 +00:00
|
|
|
#include "gdkglcontextprivate.h"
|
|
|
|
#include "gdksurfaceprivate.h"
|
2014-10-09 08:45:44 +00:00
|
|
|
|
|
|
|
#include <epoxy/gl.h>
|
|
|
|
#include <math.h>
|
2014-11-22 18:17:22 +00:00
|
|
|
#include <string.h>
|
2014-10-09 08:45:44 +00:00
|
|
|
|
|
|
|
/* 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. */
|
2014-10-12 03:17:34 +00:00
|
|
|
|
2014-10-09 08:45:44 +00:00
|
|
|
/**
|
2014-10-12 03:17:34 +00:00
|
|
|
* gdk_cairo_draw_from_gl:
|
2014-10-09 08:45:44 +00:00
|
|
|
* @cr: a cairo context
|
2018-03-20 14:14:10 +00:00
|
|
|
* @surface: The surface we're rendering for (not necessarily into)
|
2014-10-12 03:17:34 +00:00
|
|
|
* @source: The GL ID of the source buffer
|
2014-10-09 08:45:44 +00:00
|
|
|
* @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
|
|
|
|
*
|
2021-09-11 22:10:10 +00:00
|
|
|
* The main way to not draw GL content in GTK.
|
2014-10-09 08:45:44 +00:00
|
|
|
*
|
2021-05-20 03:39:18 +00:00
|
|
|
* 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,
|
2021-02-21 14:19:37 +00:00
|
|
|
* respecting the current clip. The top left corner of the rectangle specified
|
|
|
|
* by @x, @y, @width and @height will be drawn at the current (0,0) position of
|
|
|
|
* the `cairo_t`.
|
|
|
|
*
|
|
|
|
* This will work for *all* `cairo_t`, as long as @surface is realized, but the
|
2014-10-09 08:45:44 +00:00
|
|
|
* fallback implementation that reads back the pixels from the buffer may be
|
2018-03-20 14:14:10 +00:00
|
|
|
* used in the general case. In the case of direct drawing to a surface with
|
2014-10-09 08:45:44 +00:00
|
|
|
* no special effects applied to @cr it will however use a more efficient
|
|
|
|
* approach.
|
|
|
|
*
|
2021-05-20 03:39:18 +00:00
|
|
|
* 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.
|
2014-10-12 03:17:34 +00:00
|
|
|
*
|
2014-11-06 08:11:17 +00:00
|
|
|
* Calling this may change the current GL context.
|
2021-09-11 22:10:10 +00:00
|
|
|
*
|
|
|
|
* Deprecated: 4.6: The function is overly complex and produces broken output
|
|
|
|
* in various combinations of arguments. If you want to draw with GL textures
|
|
|
|
* in GTK, use [ctor@Gdk.GLTexture.new]; if you want to use that texture in
|
|
|
|
* Cairo, use [method@Gdk.Texture.download] to download the data into a Cairo
|
|
|
|
* image surface.
|
2014-10-09 08:45:44 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
gdk_cairo_draw_from_gl (cairo_t *cr,
|
2018-03-20 14:14:10 +00:00
|
|
|
GdkSurface *surface,
|
2014-10-09 08:45:44 +00:00
|
|
|
int source,
|
|
|
|
int source_type,
|
|
|
|
int buffer_scale,
|
|
|
|
int x,
|
|
|
|
int y,
|
|
|
|
int width,
|
|
|
|
int height)
|
|
|
|
{
|
2014-11-06 08:12:40 +00:00
|
|
|
GdkGLContext *paint_context;
|
2014-10-09 08:45:44 +00:00
|
|
|
cairo_surface_t *image;
|
2014-11-05 14:19:00 +00:00
|
|
|
guint framebuffer;
|
|
|
|
int alpha_size = 0;
|
2016-11-22 19:03:14 +00:00
|
|
|
int major, minor, version;
|
2018-07-31 10:18:59 +00:00
|
|
|
gboolean es_use_bgra = FALSE;
|
2014-10-09 08:45:44 +00:00
|
|
|
|
2018-03-20 14:14:10 +00:00
|
|
|
paint_context = gdk_surface_get_paint_gl_context (surface, NULL);
|
2014-11-06 08:12:40 +00:00
|
|
|
if (paint_context == NULL)
|
2014-10-09 08:45:44 +00:00
|
|
|
{
|
|
|
|
g_warning ("gdk_cairo_draw_gl_render_buffer failed - no paint context");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-31 10:18:59 +00:00
|
|
|
es_use_bgra = gdk_gl_context_use_es_bgra (paint_context);
|
2014-10-09 08:45:44 +00:00
|
|
|
|
2014-11-06 08:12:40 +00:00
|
|
|
gdk_gl_context_make_current (paint_context);
|
2014-11-05 14:19:00 +00:00
|
|
|
|
2021-10-05 23:23:02 +00:00
|
|
|
glGenFramebuffers (1, &framebuffer);
|
2014-10-09 08:45:44 +00:00
|
|
|
|
|
|
|
if (source_type == GL_RENDERBUFFER)
|
|
|
|
{
|
2015-01-28 19:06:37 +00:00
|
|
|
glBindRenderbuffer (GL_RENDERBUFFER, source);
|
2014-11-05 08:52:51 +00:00
|
|
|
glGetRenderbufferParameteriv (GL_RENDERBUFFER, GL_RENDERBUFFER_ALPHA_SIZE, &alpha_size);
|
2014-10-09 08:45:44 +00:00
|
|
|
}
|
|
|
|
else if (source_type == GL_TEXTURE)
|
|
|
|
{
|
2014-10-27 20:15:37 +00:00
|
|
|
glBindTexture (GL_TEXTURE_2D, source);
|
|
|
|
|
2016-04-22 16:27:04 +00:00
|
|
|
if (gdk_gl_context_get_use_es (paint_context))
|
|
|
|
alpha_size = 1;
|
|
|
|
else
|
|
|
|
glGetTexLevelParameteriv (GL_TEXTURE_2D, 0, GL_TEXTURE_ALPHA_SIZE, &alpha_size);
|
2014-10-09 08:45:44 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_warning ("Unsupported gl source type %d\n", source_type);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-22 19:03:14 +00:00
|
|
|
gdk_gl_context_get_version (paint_context, &major, &minor);
|
|
|
|
version = major * 100 + minor;
|
2014-11-05 08:52:51 +00:00
|
|
|
|
2016-11-22 19:03:14 +00:00
|
|
|
/* TODO: Use glTexSubImage2D() and do a row-by-row copy to replace
|
|
|
|
* the GL_UNPACK_ROW_LENGTH support
|
|
|
|
*/
|
|
|
|
if (gdk_gl_context_get_use_es (paint_context) &&
|
2024-03-16 10:31:17 +00:00
|
|
|
!(version >= 300 || gdk_gl_context_has_feature (paint_context, GDK_GL_FEATURE_UNPACK_SUBIMAGE)))
|
2020-10-01 07:47:34 +00:00
|
|
|
return;
|
2014-10-09 08:45:44 +00:00
|
|
|
|
2016-11-22 19:03:14 +00:00
|
|
|
/* 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);
|
2014-10-09 08:45:44 +00:00
|
|
|
|
2016-11-22 19:03:14 +00:00
|
|
|
cairo_surface_set_device_scale (image, buffer_scale, buffer_scale);
|
2016-04-22 16:27:04 +00:00
|
|
|
|
2020-06-12 13:08:44 +00:00
|
|
|
glBindFramebuffer (GL_FRAMEBUFFER, framebuffer);
|
2014-10-09 08:45:44 +00:00
|
|
|
|
2016-11-22 19:03:14 +00:00
|
|
|
if (source_type == GL_RENDERBUFFER)
|
|
|
|
{
|
|
|
|
/* Create a framebuffer with the source renderbuffer and
|
|
|
|
make it the current target for reads */
|
2020-06-12 13:08:44 +00:00
|
|
|
glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
|
|
|
|
GL_RENDERBUFFER, source);
|
2014-10-09 08:45:44 +00:00
|
|
|
}
|
2016-11-22 19:03:14 +00:00
|
|
|
else
|
2014-10-09 08:45:44 +00:00
|
|
|
{
|
2020-06-12 13:08:44 +00:00
|
|
|
glFramebufferTexture2D (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
|
|
|
|
GL_TEXTURE_2D, source, 0);
|
2016-11-22 19:03:14 +00:00
|
|
|
}
|
2014-10-09 08:45:44 +00:00
|
|
|
|
2016-11-22 19:03:14 +00:00
|
|
|
glPixelStorei (GL_PACK_ALIGNMENT, 4);
|
|
|
|
glPixelStorei (GL_PACK_ROW_LENGTH, cairo_image_surface_get_stride (image) / 4);
|
2014-10-09 08:45:44 +00:00
|
|
|
|
2016-11-22 19:03:14 +00:00
|
|
|
/* The implicit format conversion is going to make this path slower */
|
|
|
|
if (!gdk_gl_context_get_use_es (paint_context))
|
|
|
|
glReadPixels (x, y, width, height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV,
|
|
|
|
cairo_image_surface_get_data (image));
|
2014-10-09 08:45:44 +00:00
|
|
|
else
|
2018-07-31 10:18:59 +00:00
|
|
|
glReadPixels (x, y, width, height, es_use_bgra ? GL_BGRA : GL_RGBA, GL_UNSIGNED_BYTE,
|
2016-11-22 19:03:14 +00:00
|
|
|
cairo_image_surface_get_data (image));
|
2014-10-09 08:45:44 +00:00
|
|
|
|
2016-11-22 19:03:14 +00:00
|
|
|
glPixelStorei (GL_PACK_ROW_LENGTH, 0);
|
2014-10-09 08:45:44 +00:00
|
|
|
|
2020-06-12 13:08:44 +00:00
|
|
|
glBindFramebuffer (GL_FRAMEBUFFER, 0);
|
2014-11-05 08:52:51 +00:00
|
|
|
|
2021-10-05 23:23:02 +00:00
|
|
|
glDeleteFramebuffers (1, &framebuffer);
|
|
|
|
|
2016-11-22 19:03:14 +00:00
|
|
|
cairo_surface_mark_dirty (image);
|
2014-10-09 08:45:44 +00:00
|
|
|
|
2016-11-22 19:03:14 +00:00
|
|
|
cairo_set_source_surface (cr, image, 0, 0);
|
|
|
|
cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
|
|
|
|
cairo_paint (cr);
|
2014-10-09 08:45:44 +00:00
|
|
|
|
2016-11-22 19:03:14 +00:00
|
|
|
cairo_surface_destroy (image);
|
2014-10-09 08:45:44 +00:00
|
|
|
}
|