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"
|
|
|
|
#include "gdkglcontextprivate.h"
|
|
|
|
|
|
|
|
#include "gdkinternals.h"
|
|
|
|
|
|
|
|
#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
|
|
|
|
2014-11-06 08:13:36 +00:00
|
|
|
static const char *
|
|
|
|
get_vertex_type_name (int type)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case GL_VERTEX_SHADER:
|
|
|
|
return "vertex";
|
|
|
|
case GL_GEOMETRY_SHADER:
|
|
|
|
return "geometry";
|
|
|
|
case GL_FRAGMENT_SHADER:
|
|
|
|
return "fragment";
|
2017-10-06 19:19:42 +00:00
|
|
|
default:
|
|
|
|
return "unknown";
|
2014-11-06 08:13:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-06 21:47:59 +00:00
|
|
|
static guint
|
2015-03-22 18:40:57 +00:00
|
|
|
create_shader (int type,
|
|
|
|
const char *code)
|
2014-11-06 08:13:36 +00:00
|
|
|
{
|
|
|
|
guint shader;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
shader = glCreateShader (type);
|
|
|
|
glShaderSource (shader, 1, &code, NULL);
|
|
|
|
glCompileShader (shader);
|
|
|
|
|
|
|
|
glGetShaderiv (shader, GL_COMPILE_STATUS, &status);
|
|
|
|
if (status == GL_FALSE)
|
|
|
|
{
|
|
|
|
int log_len;
|
|
|
|
char *buffer;
|
|
|
|
|
|
|
|
glGetShaderiv (shader, GL_INFO_LOG_LENGTH, &log_len);
|
|
|
|
|
|
|
|
buffer = g_malloc (log_len + 1);
|
|
|
|
glGetShaderInfoLog (shader, log_len, NULL, buffer);
|
|
|
|
|
2016-02-28 17:20:39 +00:00
|
|
|
g_warning ("Compile failure in %s shader:\n%s", get_vertex_type_name (type), buffer);
|
2014-11-06 08:13:36 +00:00
|
|
|
g_free (buffer);
|
|
|
|
|
|
|
|
glDeleteShader (shader);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return shader;
|
|
|
|
}
|
|
|
|
|
2014-11-22 17:29:27 +00:00
|
|
|
static void
|
|
|
|
make_program (GdkGLContextProgram *program,
|
2016-04-22 11:34:33 +00:00
|
|
|
const char *vertex_shader_path,
|
|
|
|
const char *fragment_shader_path)
|
2014-11-06 08:13:36 +00:00
|
|
|
{
|
2014-11-22 17:29:27 +00:00
|
|
|
guint vertex_shader, fragment_shader;
|
2016-04-22 11:34:33 +00:00
|
|
|
GBytes *source;
|
2014-11-06 08:13:36 +00:00
|
|
|
int status;
|
|
|
|
|
2016-04-22 11:34:33 +00:00
|
|
|
source = g_resources_lookup_data (vertex_shader_path, 0, NULL);
|
|
|
|
g_assert (source != NULL);
|
|
|
|
vertex_shader = create_shader (GL_VERTEX_SHADER, g_bytes_get_data (source, NULL));
|
|
|
|
g_bytes_unref (source);
|
2014-11-06 08:13:36 +00:00
|
|
|
if (vertex_shader == 0)
|
2014-11-22 17:29:27 +00:00
|
|
|
return;
|
2014-11-06 08:13:36 +00:00
|
|
|
|
2016-04-22 11:34:33 +00:00
|
|
|
source = g_resources_lookup_data (fragment_shader_path, 0, NULL);
|
|
|
|
g_assert (source != NULL);
|
|
|
|
fragment_shader = create_shader (GL_FRAGMENT_SHADER, g_bytes_get_data (source, NULL));
|
|
|
|
g_bytes_unref (source);
|
2014-11-06 08:13:36 +00:00
|
|
|
if (fragment_shader == 0)
|
|
|
|
{
|
|
|
|
glDeleteShader (vertex_shader);
|
2014-11-22 17:29:27 +00:00
|
|
|
return;
|
2014-11-06 08:13:36 +00:00
|
|
|
}
|
|
|
|
|
2014-11-22 17:29:27 +00:00
|
|
|
program->program = glCreateProgram ();
|
|
|
|
glAttachShader (program->program, vertex_shader);
|
|
|
|
glAttachShader (program->program, fragment_shader);
|
2014-11-06 08:13:36 +00:00
|
|
|
|
2014-11-22 17:29:27 +00:00
|
|
|
glLinkProgram (program->program);
|
2014-11-06 08:13:36 +00:00
|
|
|
|
|
|
|
glDeleteShader (vertex_shader);
|
|
|
|
glDeleteShader (fragment_shader);
|
|
|
|
|
2014-11-22 17:29:27 +00:00
|
|
|
glGetProgramiv (program->program, GL_LINK_STATUS, &status);
|
2014-11-06 08:13:36 +00:00
|
|
|
if (status == GL_FALSE)
|
|
|
|
{
|
|
|
|
int log_len;
|
|
|
|
char *buffer;
|
|
|
|
|
2014-11-22 17:29:27 +00:00
|
|
|
glGetProgramiv (program->program, GL_INFO_LOG_LENGTH, &log_len);
|
2014-11-06 08:13:36 +00:00
|
|
|
|
|
|
|
buffer = g_malloc (log_len + 1);
|
2014-11-22 17:29:27 +00:00
|
|
|
glGetProgramInfoLog (program->program, log_len, NULL, buffer);
|
2014-11-06 08:13:36 +00:00
|
|
|
g_warning ("Linker failure: %s\n", buffer);
|
|
|
|
g_free (buffer);
|
|
|
|
|
2014-11-22 17:29:27 +00:00
|
|
|
glDeleteProgram (program->program);
|
2014-11-06 08:13:36 +00:00
|
|
|
}
|
|
|
|
|
2014-11-22 17:29:27 +00:00
|
|
|
program->position_location = glGetAttribLocation (program->program, "position");
|
|
|
|
program->uv_location = glGetAttribLocation (program->program, "uv");
|
|
|
|
program->map_location = glGetUniformLocation (program->program, "map");
|
2016-04-25 12:38:22 +00:00
|
|
|
program->flip_location = glGetUniformLocation (program->program, "flipColors");
|
2014-11-06 08:13:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bind_vao (GdkGLContextPaintData *paint_data)
|
|
|
|
{
|
|
|
|
if (paint_data->vertex_array_object == 0)
|
|
|
|
{
|
|
|
|
glGenVertexArrays (1, &paint_data->vertex_array_object);
|
|
|
|
/* ATM we only use one VAO, so always bind it */
|
|
|
|
glBindVertexArray (paint_data->vertex_array_object);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-22 12:52:49 +00:00
|
|
|
static void
|
|
|
|
use_texture_gles_program (GdkGLContextPaintData *paint_data)
|
|
|
|
{
|
|
|
|
if (paint_data->texture_2d_quad_program.program == 0)
|
|
|
|
make_program (&paint_data->texture_2d_quad_program,
|
|
|
|
"/org/gtk/libgdk/glsl/gles2-texture.vs.glsl",
|
|
|
|
"/org/gtk/libgdk/glsl/gles2-texture.fs.glsl");
|
|
|
|
|
|
|
|
if (paint_data->current_program != &paint_data->texture_2d_quad_program)
|
|
|
|
{
|
|
|
|
paint_data->current_program = &paint_data->texture_2d_quad_program;
|
|
|
|
glUseProgram (paint_data->current_program->program);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-06 08:13:36 +00:00
|
|
|
static void
|
2014-11-22 17:29:27 +00:00
|
|
|
use_texture_2d_program (GdkGLContextPaintData *paint_data)
|
2014-11-06 08:13:36 +00:00
|
|
|
{
|
2016-04-22 11:34:33 +00:00
|
|
|
const char *vertex_shader_path = paint_data->is_legacy
|
|
|
|
? "/org/gtk/libgdk/glsl/gl2-texture-2d.vs.glsl"
|
|
|
|
: "/org/gtk/libgdk/glsl/gl3-texture-2d.vs.glsl";
|
|
|
|
|
|
|
|
const char *fragment_shader_path = paint_data->is_legacy
|
|
|
|
? "/org/gtk/libgdk/glsl/gl2-texture-2d.fs.glsl"
|
|
|
|
: "/org/gtk/libgdk/glsl/gl3-texture-2d.fs.glsl";
|
2015-10-06 18:55:43 +00:00
|
|
|
|
2014-11-22 17:29:27 +00:00
|
|
|
if (paint_data->texture_2d_quad_program.program == 0)
|
2016-04-22 11:34:33 +00:00
|
|
|
make_program (&paint_data->texture_2d_quad_program, vertex_shader_path, fragment_shader_path);
|
2014-11-06 08:13:36 +00:00
|
|
|
|
2014-11-22 17:29:27 +00:00
|
|
|
if (paint_data->current_program != &paint_data->texture_2d_quad_program)
|
2014-11-06 08:13:36 +00:00
|
|
|
{
|
2014-11-22 17:29:27 +00:00
|
|
|
paint_data->current_program = &paint_data->texture_2d_quad_program;
|
|
|
|
glUseProgram (paint_data->current_program->program);
|
2014-11-06 08:13:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-06 10:31:01 +00:00
|
|
|
static void
|
2014-11-22 17:29:27 +00:00
|
|
|
use_texture_rect_program (GdkGLContextPaintData *paint_data)
|
2014-11-06 10:31:01 +00:00
|
|
|
{
|
2016-04-22 11:34:33 +00:00
|
|
|
const char *vertex_shader_path = paint_data->is_legacy
|
|
|
|
? "/org/gtk/libgdk/glsl/gl2-texture-rect.vs.glsl"
|
|
|
|
: "/org/gtk/libgdk/glsl/gl3-texture-rect.vs.glsl";
|
|
|
|
|
|
|
|
const char *fragment_shader_path = paint_data->is_legacy
|
|
|
|
? "/org/gtk/libgdk/glsl/gl2-texture-rect.fs.glsl"
|
|
|
|
: "/org/gtk/libgdk/glsl/gl3-texture-rect.vs.glsl";
|
2014-11-06 10:31:01 +00:00
|
|
|
|
2014-11-22 17:29:27 +00:00
|
|
|
if (paint_data->texture_rect_quad_program.program == 0)
|
2016-04-22 11:34:33 +00:00
|
|
|
make_program (&paint_data->texture_rect_quad_program, vertex_shader_path, fragment_shader_path);
|
2014-11-06 10:31:01 +00:00
|
|
|
|
2014-11-22 17:29:27 +00:00
|
|
|
if (paint_data->current_program != &paint_data->texture_rect_quad_program)
|
2014-11-06 10:31:01 +00:00
|
|
|
{
|
2014-11-22 17:29:27 +00:00
|
|
|
paint_data->current_program = &paint_data->texture_rect_quad_program;
|
|
|
|
glUseProgram (paint_data->current_program->program);
|
2014-11-06 10:31:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-05 14:42:06 +00:00
|
|
|
void
|
2014-11-22 17:47:35 +00:00
|
|
|
gdk_gl_texture_quads (GdkGLContext *paint_context,
|
|
|
|
guint texture_target,
|
|
|
|
int n_quads,
|
2016-04-25 12:38:22 +00:00
|
|
|
GdkTexturedQuad *quads,
|
|
|
|
gboolean flip_colors)
|
2014-11-05 14:42:06 +00:00
|
|
|
{
|
2014-11-14 09:26:29 +00:00
|
|
|
GdkGLContextPaintData *paint_data = gdk_gl_context_get_paint_data (paint_context);
|
2014-11-22 17:29:27 +00:00
|
|
|
GdkGLContextProgram *program;
|
2018-03-20 14:14:10 +00:00
|
|
|
GdkSurface *surface = gdk_gl_context_get_surface (paint_context);
|
|
|
|
int surface_scale = gdk_surface_get_scale_factor (surface);
|
|
|
|
float w = gdk_surface_get_width (surface) * surface_scale;
|
|
|
|
float h = gdk_surface_get_height (surface) * surface_scale;
|
2014-11-22 17:47:35 +00:00
|
|
|
int i;
|
2014-11-22 18:17:22 +00:00
|
|
|
float *vertex_buffer_data;
|
2014-11-06 08:13:36 +00:00
|
|
|
|
|
|
|
bind_vao (paint_data);
|
|
|
|
|
|
|
|
if (paint_data->tmp_vertex_buffer == 0)
|
|
|
|
glGenBuffers(1, &paint_data->tmp_vertex_buffer);
|
|
|
|
|
2016-04-22 12:52:49 +00:00
|
|
|
if (paint_data->use_es)
|
|
|
|
use_texture_gles_program (paint_data);
|
2014-11-06 10:31:01 +00:00
|
|
|
else
|
2016-04-22 12:52:49 +00:00
|
|
|
{
|
|
|
|
if (texture_target == GL_TEXTURE_RECTANGLE_ARB)
|
|
|
|
use_texture_rect_program (paint_data);
|
|
|
|
else
|
|
|
|
use_texture_2d_program (paint_data);
|
|
|
|
}
|
2014-11-22 17:29:27 +00:00
|
|
|
|
|
|
|
program = paint_data->current_program;
|
2014-11-06 08:13:36 +00:00
|
|
|
|
2016-04-25 12:38:22 +00:00
|
|
|
/* Use texture unit 0 */
|
2014-11-06 08:13:36 +00:00
|
|
|
glActiveTexture (GL_TEXTURE0);
|
2016-04-25 12:38:22 +00:00
|
|
|
glUniform1i(program->map_location, 0);
|
|
|
|
|
|
|
|
/* Flip 'R' and 'B' colors on GLES, if necessary */
|
|
|
|
if (gdk_gl_context_get_use_es (paint_context))
|
|
|
|
glUniform1i (program->flip_location, flip_colors ? 1 : 0);
|
2014-11-06 10:31:01 +00:00
|
|
|
|
2015-01-15 01:30:28 +00:00
|
|
|
glEnableVertexAttribArray (program->position_location);
|
|
|
|
glEnableVertexAttribArray (program->uv_location);
|
2014-11-06 08:13:36 +00:00
|
|
|
glBindBuffer (GL_ARRAY_BUFFER, paint_data->tmp_vertex_buffer);
|
2014-11-22 18:12:58 +00:00
|
|
|
|
|
|
|
glVertexAttribPointer (program->position_location, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, NULL);
|
2014-11-24 03:05:18 +00:00
|
|
|
glVertexAttribPointer (program->uv_location, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, (void *) (sizeof(float) * 2));
|
2014-11-22 17:47:35 +00:00
|
|
|
|
2014-11-22 18:17:22 +00:00
|
|
|
#define VERTEX_SIZE 4
|
|
|
|
|
|
|
|
#define QUAD_N_VERTICES 6
|
|
|
|
|
|
|
|
#define QUAD_SIZE (VERTEX_SIZE * QUAD_N_VERTICES)
|
|
|
|
|
|
|
|
vertex_buffer_data = g_new (float, n_quads * QUAD_SIZE);
|
|
|
|
|
2014-11-22 17:47:35 +00:00
|
|
|
for (i = 0; i < n_quads; i++)
|
|
|
|
{
|
|
|
|
GdkTexturedQuad *quad = &quads[i];
|
2014-11-22 18:17:22 +00:00
|
|
|
float vertex_data[] = {
|
|
|
|
(quad->x1 * 2) / w - 1, (quad->y1 * 2) / h - 1, quad->u1, quad->v1,
|
|
|
|
(quad->x1 * 2) / w - 1, (quad->y2 * 2) / h - 1, quad->u1, quad->v2,
|
2014-11-22 18:12:58 +00:00
|
|
|
(quad->x2 * 2) / w - 1, (quad->y1 * 2) / h - 1, quad->u2, quad->v1,
|
2014-11-22 18:17:22 +00:00
|
|
|
|
2014-11-22 18:12:58 +00:00
|
|
|
(quad->x2 * 2) / w - 1, (quad->y2 * 2) / h - 1, quad->u2, quad->v2,
|
|
|
|
(quad->x1 * 2) / w - 1, (quad->y2 * 2) / h - 1, quad->u1, quad->v2,
|
2014-11-22 18:17:22 +00:00
|
|
|
(quad->x2 * 2) / w - 1, (quad->y1 * 2) / h - 1, quad->u2, quad->v1,
|
2014-11-22 17:47:35 +00:00
|
|
|
};
|
|
|
|
|
2014-11-22 18:17:22 +00:00
|
|
|
float *vertex = &vertex_buffer_data[i * QUAD_SIZE];
|
|
|
|
memcpy (vertex, vertex_data, sizeof(vertex_data));
|
2014-11-22 17:47:35 +00:00
|
|
|
}
|
|
|
|
|
2014-11-22 18:17:22 +00:00
|
|
|
glBufferData (GL_ARRAY_BUFFER, sizeof(float) * n_quads * QUAD_SIZE, vertex_buffer_data, GL_STREAM_DRAW);
|
|
|
|
glDrawArrays (GL_TRIANGLES, 0, n_quads * QUAD_N_VERTICES);
|
|
|
|
|
|
|
|
g_free (vertex_buffer_data);
|
|
|
|
|
2015-01-15 01:30:28 +00:00
|
|
|
glDisableVertexAttribArray (program->position_location);
|
|
|
|
glDisableVertexAttribArray (program->uv_location);
|
2014-11-05 14:42:06 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*
|
2014-10-12 03:17:34 +00:00
|
|
|
* This is the main way to draw GL content in GTK+. It takes a render buffer ID
|
2014-10-09 08:45:44 +00:00
|
|
|
* (@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.
|
2014-10-30 11:22:08 +00:00
|
|
|
* 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.
|
2014-10-09 08:45:44 +00:00
|
|
|
*
|
2018-03-20 14:14:10 +00:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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.
|
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;
|
|
|
|
cairo_matrix_t matrix;
|
2014-11-05 14:19:00 +00:00
|
|
|
guint framebuffer;
|
|
|
|
int alpha_size = 0;
|
2014-10-09 08:45:44 +00:00
|
|
|
cairo_region_t *clip_region;
|
2014-11-05 14:19:00 +00:00
|
|
|
GdkGLContextPaintData *paint_data;
|
2016-11-22 19:03:14 +00:00
|
|
|
int major, minor, version;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
clip_region = gdk_cairo_region_from_clip (cr);
|
|
|
|
|
2014-11-06 08:12:40 +00:00
|
|
|
gdk_gl_context_make_current (paint_context);
|
|
|
|
paint_data = gdk_gl_context_get_paint_data (paint_context);
|
2014-11-05 14:19:00 +00:00
|
|
|
|
|
|
|
if (paint_data->tmp_framebuffer == 0)
|
|
|
|
glGenFramebuffersEXT (1, &paint_data->tmp_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;
|
|
|
|
}
|
|
|
|
|
|
|
|
cairo_get_matrix (cr, &matrix);
|
|
|
|
|
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) &&
|
|
|
|
!(version >= 300 || gdk_gl_context_has_unpack_subimage (paint_context)))
|
|
|
|
goto out;
|
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
|
|
|
|
2016-11-22 19:03:14 +00:00
|
|
|
framebuffer = paint_data->tmp_framebuffer;
|
|
|
|
glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, 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 */
|
|
|
|
glFramebufferRenderbufferEXT (GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
|
|
|
|
GL_RENDERBUFFER_EXT, 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
|
|
|
{
|
2016-11-22 19:03:14 +00:00
|
|
|
glFramebufferTexture2DEXT (GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
|
|
|
|
GL_TEXTURE_2D, source, 0);
|
|
|
|
}
|
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
|
2016-11-22 19:03:14 +00:00
|
|
|
glReadPixels (x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE,
|
|
|
|
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
|
|
|
|
2016-11-22 19:03:14 +00:00
|
|
|
glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, 0);
|
2014-11-05 08:52:51 +00:00
|
|
|
|
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
|
|
|
/* Invert due to opengl having different origin */
|
|
|
|
cairo_scale (cr, 1, -1);
|
|
|
|
cairo_translate (cr, 0, -height / buffer_scale);
|
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
|
|
|
|
2016-04-23 11:16:03 +00:00
|
|
|
out:
|
2014-10-09 08:45:44 +00:00
|
|
|
if (clip_region)
|
|
|
|
cairo_region_destroy (clip_region);
|
|
|
|
}
|
|
|
|
|
2014-11-06 08:12:40 +00:00
|
|
|
/* This is always called with the paint context current */
|
2014-10-09 08:45:44 +00:00
|
|
|
void
|
2018-03-20 14:14:10 +00:00
|
|
|
gdk_gl_texture_from_surface (cairo_surface_t *cairo_surface,
|
2014-10-12 03:17:34 +00:00
|
|
|
cairo_region_t *region)
|
2014-10-09 08:45:44 +00:00
|
|
|
{
|
2014-11-06 08:12:40 +00:00
|
|
|
GdkGLContext *paint_context;
|
2018-01-12 00:48:27 +00:00
|
|
|
GdkDisplay *display;
|
2014-10-09 08:45:44 +00:00
|
|
|
cairo_surface_t *image;
|
|
|
|
double device_x_offset, device_y_offset;
|
|
|
|
cairo_rectangle_int_t rect, e;
|
|
|
|
int n_rects, i;
|
2018-03-20 14:14:10 +00:00
|
|
|
GdkSurface *surface;
|
|
|
|
int unscaled_surface_height;
|
2014-10-09 08:45:44 +00:00
|
|
|
unsigned int texture_id;
|
2018-03-20 11:05:26 +00:00
|
|
|
int surface_scale;
|
2014-10-09 08:45:44 +00:00
|
|
|
double sx, sy;
|
2014-10-27 20:15:37 +00:00
|
|
|
float umax, vmax;
|
|
|
|
gboolean use_texture_rectangle;
|
|
|
|
guint target;
|
2016-11-22 19:03:14 +00:00
|
|
|
|
2014-11-06 08:12:40 +00:00
|
|
|
paint_context = gdk_gl_context_get_current ();
|
2018-01-12 00:48:27 +00:00
|
|
|
if (paint_context)
|
|
|
|
display = gdk_draw_context_get_display (GDK_DRAW_CONTEXT (paint_context));
|
|
|
|
else
|
|
|
|
display = NULL;
|
|
|
|
|
|
|
|
if (paint_context &&
|
|
|
|
GDK_DISPLAY_DEBUG_CHECK (display, GL_SOFTWARE) == 0 &&
|
2014-11-06 08:12:40 +00:00
|
|
|
GDK_GL_CONTEXT_GET_CLASS (paint_context)->texture_from_surface &&
|
2018-03-20 14:14:10 +00:00
|
|
|
GDK_GL_CONTEXT_GET_CLASS (paint_context)->texture_from_surface (paint_context, cairo_surface, region))
|
2014-10-09 08:45:44 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* Software fallback */
|
2014-11-06 08:12:40 +00:00
|
|
|
use_texture_rectangle = gdk_gl_context_use_texture_rectangle (paint_context);
|
2014-10-27 20:15:37 +00:00
|
|
|
|
2018-03-20 14:14:10 +00:00
|
|
|
surface = gdk_gl_context_get_surface (paint_context);
|
|
|
|
surface_scale = gdk_surface_get_scale_factor (surface);
|
|
|
|
gdk_surface_get_unscaled_size (surface, NULL, &unscaled_surface_height);
|
2014-10-09 08:45:44 +00:00
|
|
|
|
|
|
|
sx = sy = 1;
|
2018-03-20 14:14:10 +00:00
|
|
|
cairo_surface_get_device_scale (cairo_surface, &sx, &sy);
|
|
|
|
cairo_surface_get_device_offset (cairo_surface, &device_x_offset, &device_y_offset);
|
2014-10-09 08:45:44 +00:00
|
|
|
|
|
|
|
glGenTextures (1, &texture_id);
|
2014-10-27 20:15:37 +00:00
|
|
|
if (use_texture_rectangle)
|
|
|
|
target = GL_TEXTURE_RECTANGLE_ARB;
|
|
|
|
else
|
|
|
|
target = GL_TEXTURE_2D;
|
|
|
|
|
|
|
|
glBindTexture (target, texture_id);
|
2014-11-06 19:37:10 +00:00
|
|
|
glEnable (GL_SCISSOR_TEST);
|
2014-10-27 20:15:37 +00:00
|
|
|
|
2014-11-06 10:31:01 +00:00
|
|
|
glTexParameteri (target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri (target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri (target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
glTexParameteri (target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
2014-10-09 08:45:44 +00:00
|
|
|
|
|
|
|
n_rects = cairo_region_num_rectangles (region);
|
2014-11-22 17:47:35 +00:00
|
|
|
|
2018-03-20 14:14:10 +00:00
|
|
|
#define FLIP_Y(_y) (unscaled_surface_height - (_y))
|
2014-12-15 21:31:48 +00:00
|
|
|
|
2014-10-09 08:45:44 +00:00
|
|
|
for (i = 0; i < n_rects; i++)
|
|
|
|
{
|
|
|
|
cairo_region_get_rectangle (region, i, &rect);
|
|
|
|
|
2018-03-20 11:05:26 +00:00
|
|
|
glScissor (rect.x * surface_scale, FLIP_Y ((rect.y + rect.height) * surface_scale),
|
|
|
|
rect.width * surface_scale, rect.height * surface_scale);
|
2014-10-09 08:45:44 +00:00
|
|
|
|
|
|
|
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;
|
2018-03-20 14:14:10 +00:00
|
|
|
image = cairo_surface_map_to_image (cairo_surface, &e);
|
2014-10-09 08:45:44 +00:00
|
|
|
|
2016-11-01 03:47:52 +00:00
|
|
|
gdk_gl_context_upload_texture (paint_context,
|
|
|
|
cairo_image_surface_get_data (image),
|
|
|
|
e.width,
|
|
|
|
e.height,
|
|
|
|
cairo_image_surface_get_stride (image),
|
|
|
|
target);
|
2014-10-09 08:45:44 +00:00
|
|
|
|
2018-03-20 14:14:10 +00:00
|
|
|
cairo_surface_unmap_image (cairo_surface, image);
|
2014-10-09 08:45:44 +00:00
|
|
|
|
2014-10-27 20:15:37 +00:00
|
|
|
if (use_texture_rectangle)
|
|
|
|
{
|
|
|
|
umax = rect.width * sx;
|
|
|
|
vmax = rect.height * sy;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
umax = 1.0;
|
|
|
|
vmax = 1.0;
|
|
|
|
}
|
|
|
|
|
2014-11-22 17:39:18 +00:00
|
|
|
{
|
|
|
|
GdkTexturedQuad quad = {
|
2018-03-20 11:05:26 +00:00
|
|
|
rect.x * surface_scale, FLIP_Y(rect.y * surface_scale),
|
|
|
|
(rect.x + rect.width) * surface_scale, FLIP_Y((rect.y + rect.height) * surface_scale),
|
2014-11-22 17:39:18 +00:00
|
|
|
0, 0,
|
|
|
|
umax, vmax,
|
|
|
|
};
|
|
|
|
|
2014-12-15 20:55:10 +00:00
|
|
|
/* We don't want to combine the quads here, because they have different textures.
|
|
|
|
* And we don't want to upload the unused source areas to make it one texture. */
|
2016-04-25 12:38:22 +00:00
|
|
|
gdk_gl_texture_quads (paint_context, target, 1, &quad, TRUE);
|
2014-11-22 17:39:18 +00:00
|
|
|
}
|
2014-10-09 08:45:44 +00:00
|
|
|
}
|
|
|
|
|
2015-02-09 19:09:31 +00:00
|
|
|
#undef FLIP_Y
|
|
|
|
|
2014-11-06 19:37:10 +00:00
|
|
|
glDisable (GL_SCISSOR_TEST);
|
2014-10-09 08:45:44 +00:00
|
|
|
glDeleteTextures (1, &texture_id);
|
|
|
|
}
|
2016-04-25 10:29:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gdk_cairo_surface_upload_to_gl:
|
|
|
|
* @surface: a Cairo surface
|
|
|
|
* @target: a GL texture target
|
|
|
|
* @width: the width of the texture @target
|
|
|
|
* @height: the height of the texture @target
|
|
|
|
* @context: (nullable): a #GdkGLContext, or %NULL to use the currently
|
|
|
|
* bound context
|
|
|
|
*
|
|
|
|
* Uploads the contents of a Cairo @surface to a GL texture @target.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gdk_cairo_surface_upload_to_gl (cairo_surface_t *surface,
|
|
|
|
int target,
|
|
|
|
int width,
|
|
|
|
int height,
|
|
|
|
GdkGLContext *context)
|
|
|
|
{
|
|
|
|
cairo_rectangle_int_t rect;
|
|
|
|
cairo_surface_t *tmp;
|
|
|
|
double device_x_offset, device_y_offset;
|
|
|
|
|
|
|
|
g_return_if_fail (surface != NULL);
|
|
|
|
g_return_if_fail (context == NULL || GDK_IS_GL_CONTEXT (context));
|
|
|
|
|
|
|
|
if (context == NULL)
|
|
|
|
context = gdk_gl_context_get_current ();
|
|
|
|
|
|
|
|
cairo_surface_flush (surface);
|
|
|
|
|
|
|
|
cairo_surface_get_device_offset (surface, &device_x_offset, &device_y_offset);
|
|
|
|
|
|
|
|
rect.x = (int) device_x_offset;
|
|
|
|
rect.y = (int) device_y_offset;
|
2016-08-16 09:53:49 +00:00
|
|
|
rect.width = width;
|
|
|
|
rect.height = height;
|
2016-04-25 10:29:14 +00:00
|
|
|
tmp = cairo_surface_map_to_image (surface, &rect);
|
|
|
|
|
2016-11-01 03:47:52 +00:00
|
|
|
gdk_gl_context_upload_texture (context,
|
|
|
|
cairo_image_surface_get_data (tmp),
|
|
|
|
rect.width,
|
|
|
|
rect.height,
|
|
|
|
cairo_image_surface_get_stride (tmp),
|
|
|
|
target);
|
2016-04-25 10:29:14 +00:00
|
|
|
|
|
|
|
cairo_surface_unmap_image (surface, tmp);
|
|
|
|
}
|