1/* GDK - The GIMP Drawing Kit
2 * Copyright (C) 2014 Red Hat, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "config.h"
19
20#include "gdkcairo.h"
21#include "gdkglcontextprivate.h"
22
23#include "gdkinternals.h"
24
25#include <epoxy/gl.h>
26#include <math.h>
27#include <string.h>
28
29static cairo_user_data_key_t direct_key;
30
31void
32gdk_cairo_surface_mark_as_direct (cairo_surface_t *surface,
33 GdkWindow *window)
34{
35 cairo_surface_set_user_data (surface, &direct_key,
36 g_object_ref (window), g_object_unref);
37}
38
39static const char *
40get_vertex_type_name (int type)
41{
42 switch (type)
43 {
44 case GL_VERTEX_SHADER:
45 return "vertex";
46 case GL_GEOMETRY_SHADER:
47 return "geometry";
48 case GL_FRAGMENT_SHADER:
49 return "fragment";
50 }
51 return "unknown";
52}
53
54static guint
55create_shader (int type,
56 const char *code)
57{
58 guint shader;
59 int status;
60
61 shader = glCreateShader (type);
62 glShaderSource (shader, 1, &code, NULL);
63 glCompileShader (shader);
64
65 glGetShaderiv (shader, GL_COMPILE_STATUS, &status);
66 if (status == GL_FALSE)
67 {
68 int log_len;
69 char *buffer;
70
71 glGetShaderiv (shader, GL_INFO_LOG_LENGTH, &log_len);
72
73 buffer = g_malloc (log_len + 1);
74 glGetShaderInfoLog (shader, log_len, NULL, buffer);
75
76 g_warning ("Compile failure in %s shader:\n%s", get_vertex_type_name (type), buffer);
77 g_free (buffer);
78
79 glDeleteShader (shader);
80
81 return 0;
82 }
83
84 return shader;
85}
86
87static void
88make_program (GdkGLContextProgram *program,
89 const char *vertex_shader_path,
90 const char *fragment_shader_path)
91{
92 guint vertex_shader, fragment_shader;
93 GBytes *source;
94 int status;
95
96 source = g_resources_lookup_data (vertex_shader_path, 0, NULL);
97 g_assert (source != NULL);
98 vertex_shader = create_shader (GL_VERTEX_SHADER, g_bytes_get_data (source, NULL));
99 g_bytes_unref (source);
100 if (vertex_shader == 0)
101 return;
102
103 source = g_resources_lookup_data (fragment_shader_path, 0, NULL);
104 g_assert (source != NULL);
105 fragment_shader = create_shader (GL_FRAGMENT_SHADER, g_bytes_get_data (source, NULL));
106 g_bytes_unref (source);
107 if (fragment_shader == 0)
108 {
109 glDeleteShader (vertex_shader);
110 return;
111 }
112
113 program->program = glCreateProgram ();
114 glAttachShader (program->program, vertex_shader);
115 glAttachShader (program->program, fragment_shader);
116
117 glLinkProgram (program->program);
118
119 glDeleteShader (vertex_shader);
120 glDeleteShader (fragment_shader);
121
122 glGetProgramiv (program->program, GL_LINK_STATUS, &status);
123 if (status == GL_FALSE)
124 {
125 int log_len;
126 char *buffer;
127
128 glGetProgramiv (program->program, GL_INFO_LOG_LENGTH, &log_len);
129
130 buffer = g_malloc (log_len + 1);
131 glGetProgramInfoLog (program->program, log_len, NULL, buffer);
132 g_warning ("Linker failure: %s\n", buffer);
133 g_free (buffer);
134
135 glDeleteProgram (program->program);
136 }
137
138 program->position_location = glGetAttribLocation (program->program, "position");
139 program->uv_location = glGetAttribLocation (program->program, "uv");
140 program->map_location = glGetUniformLocation (program->program, "map");
141 program->flip_location = glGetUniformLocation (program->program, "flipColors");
142}
143
144static void
145bind_vao (GdkGLContextPaintData *paint_data)
146{
147 if (paint_data->vertex_array_object == 0)
148 {
149 glGenVertexArrays (1, &paint_data->vertex_array_object);
150 /* ATM we only use one VAO, so always bind it */
151 glBindVertexArray (paint_data->vertex_array_object);
152 }
153}
154
155static void
156use_texture_gles_program (GdkGLContextPaintData *paint_data)
157{
158 if (paint_data->texture_2d_quad_program.program == 0)
159 make_program (&paint_data->texture_2d_quad_program,
160 "/org/gtk/libgdk/glsl/gles2-texture.vs.glsl",
161 "/org/gtk/libgdk/glsl/gles2-texture.fs.glsl");
162
163 if (paint_data->current_program != &paint_data->texture_2d_quad_program)
164 {
165 paint_data->current_program = &paint_data->texture_2d_quad_program;
166 glUseProgram (paint_data->current_program->program);
167 }
168}
169
170static void
171use_texture_2d_program (GdkGLContextPaintData *paint_data)
172{
173 const char *vertex_shader_path = paint_data->is_legacy
174 ? "/org/gtk/libgdk/glsl/gl2-texture-2d.vs.glsl"
175 : "/org/gtk/libgdk/glsl/gl3-texture-2d.vs.glsl";
176
177 const char *fragment_shader_path = paint_data->is_legacy
178 ? "/org/gtk/libgdk/glsl/gl2-texture-2d.fs.glsl"
179 : "/org/gtk/libgdk/glsl/gl3-texture-2d.fs.glsl";
180
181 if (paint_data->texture_2d_quad_program.program == 0)
182 make_program (&paint_data->texture_2d_quad_program, vertex_shader_path, fragment_shader_path);
183
184 if (paint_data->current_program != &paint_data->texture_2d_quad_program)
185 {
186 paint_data->current_program = &paint_data->texture_2d_quad_program;
187 glUseProgram (paint_data->current_program->program);
188 }
189}
190
191static void
192use_texture_rect_program (GdkGLContextPaintData *paint_data)
193{
194 const char *vertex_shader_path = paint_data->is_legacy
195 ? "/org/gtk/libgdk/glsl/gl2-texture-rect.vs.glsl"
196 : "/org/gtk/libgdk/glsl/gl3-texture-rect.vs.glsl";
197
198 const char *fragment_shader_path = paint_data->is_legacy
199 ? "/org/gtk/libgdk/glsl/gl2-texture-rect.fs.glsl"
200 : "/org/gtk/libgdk/glsl/gl3-texture-rect.vs.glsl";
201
202 if (paint_data->texture_rect_quad_program.program == 0)
203 make_program (&paint_data->texture_rect_quad_program, vertex_shader_path, fragment_shader_path);
204
205 if (paint_data->current_program != &paint_data->texture_rect_quad_program)
206 {
207 paint_data->current_program = &paint_data->texture_rect_quad_program;
208 glUseProgram (paint_data->current_program->program);
209 }
210}
211
212void
213gdk_gl_texture_quads (GdkGLContext *paint_context,
214 guint texture_target,
215 int n_quads,
216 GdkTexturedQuad *quads,
217 gboolean flip_colors)
218{
219 GdkGLContextPaintData *paint_data = gdk_gl_context_get_paint_data (paint_context);
220 GdkGLContextProgram *program;
221 GdkWindow *window = gdk_gl_context_get_window (paint_context);
222 int window_scale = gdk_window_get_scale_factor (window);
223 float w = gdk_window_get_width (window) * window_scale;
224 float h = gdk_window_get_height (window) * window_scale;
225 int i;
226 float *vertex_buffer_data;
227
228 bind_vao (paint_data);
229
230 if (paint_data->tmp_vertex_buffer == 0)
231 glGenBuffers(1, &paint_data->tmp_vertex_buffer);
232
233 if (paint_data->use_es)
234 use_texture_gles_program (paint_data);
235 else
236 {
237 if (texture_target == GL_TEXTURE_RECTANGLE_ARB)
238 use_texture_rect_program (paint_data);
239 else
240 use_texture_2d_program (paint_data);
241 }
242
243 program = paint_data->current_program;
244
245 /* Use texture unit 0 */
246 glActiveTexture (GL_TEXTURE0);
247 glUniform1i(program->map_location, 0);
248
249 /* Flip 'R' and 'B' colors on GLES, if necessary */
250 if (gdk_gl_context_get_use_es (paint_context))
251 glUniform1i (program->flip_location, flip_colors ? 1 : 0);
252
253 glEnableVertexAttribArray (program->position_location);
254 glEnableVertexAttribArray (program->uv_location);
255 glBindBuffer (GL_ARRAY_BUFFER, paint_data->tmp_vertex_buffer);
256
257 glVertexAttribPointer (program->position_location, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, NULL);
258 glVertexAttribPointer (program->uv_location, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, (void *) (sizeof(float) * 2));
259
260#define VERTEX_SIZE 4
261
262#define QUAD_N_VERTICES 6
263
264#define QUAD_SIZE (VERTEX_SIZE * QUAD_N_VERTICES)
265
266 vertex_buffer_data = g_new (float, n_quads * QUAD_SIZE);
267
268 for (i = 0; i < n_quads; i++)
269 {
270 GdkTexturedQuad *quad = &quads[i];
271 float vertex_data[] = {
272 (quad->x1 * 2) / w - 1, (quad->y1 * 2) / h - 1, quad->u1, quad->v1,
273 (quad->x1 * 2) / w - 1, (quad->y2 * 2) / h - 1, quad->u1, quad->v2,
274 (quad->x2 * 2) / w - 1, (quad->y1 * 2) / h - 1, quad->u2, quad->v1,
275
276 (quad->x2 * 2) / w - 1, (quad->y2 * 2) / h - 1, quad->u2, quad->v2,
277 (quad->x1 * 2) / w - 1, (quad->y2 * 2) / h - 1, quad->u1, quad->v2,
278 (quad->x2 * 2) / w - 1, (quad->y1 * 2) / h - 1, quad->u2, quad->v1,
279 };
280
281 float *vertex = &vertex_buffer_data[i * QUAD_SIZE];
282 memcpy (vertex, vertex_data, sizeof(vertex_data));
283 }
284
285 glBufferData (GL_ARRAY_BUFFER, sizeof(float) * n_quads * QUAD_SIZE, vertex_buffer_data, GL_STREAM_DRAW);
286 glDrawArrays (GL_TRIANGLES, 0, n_quads * QUAD_N_VERTICES);
287
288 g_free (vertex_buffer_data);
289
290 glDisableVertexAttribArray (program->position_location);
291 glDisableVertexAttribArray (program->uv_location);
292}
293
294/* x,y,width,height describes a rectangle in the gl render buffer
295 coordinate space, and its top left corner is drawn at the current
296 position according to the cairo translation. */
297
298/**
299 * gdk_cairo_draw_from_gl:
300 * @cr: a cairo context
301 * @window: The window we're rendering for (not necessarily into)
302 * @source: The GL ID of the source buffer
303 * @source_type: The type of the @source
304 * @buffer_scale: The scale-factor that the @source buffer is allocated for
305 * @x: The source x position in @source to start copying from in GL coordinates
306 * @y: The source y position in @source to start copying from in GL coordinates
307 * @width: The width of the region to draw
308 * @height: The height of the region to draw
309 *
310 * This is the main way to draw GL content in GTK+. It takes a render buffer ID
311 * (@source_type == #GL_RENDERBUFFER) or a texture id (@source_type == #GL_TEXTURE)
312 * and draws it onto @cr with an OVER operation, respecting the current clip.
313 * The top left corner of the rectangle specified by @x, @y, @width and @height
314 * will be drawn at the current (0,0) position of the cairo_t.
315 *
316 * This will work for *all* cairo_t, as long as @window is realized, but the
317 * fallback implementation that reads back the pixels from the buffer may be
318 * used in the general case. In the case of direct drawing to a window with
319 * no special effects applied to @cr it will however use a more efficient
320 * approach.
321 *
322 * For #GL_RENDERBUFFER the code will always fall back to software for buffers
323 * with alpha components, so make sure you use #GL_TEXTURE if using alpha.
324 *
325 * Calling this may change the current GL context.
326 *
327 * Since: 3.16
328 */
329void
330gdk_cairo_draw_from_gl (cairo_t *cr,
331 GdkWindow *window,
332 int source,
333 int source_type,
334 int buffer_scale,
335 int x,
336 int y,
337 int width,
338 int height)
339{
340 GdkGLContext *paint_context;
341 cairo_surface_t *image;
342 cairo_matrix_t matrix;
343 int dx, dy, window_scale;
344 gboolean trivial_transform;
345 cairo_surface_t *group_target;
346 GdkWindow *direct_window, *impl_window;
347 guint framebuffer;
348 int alpha_size = 0;
349 cairo_region_t *clip_region;
350 GdkGLContextPaintData *paint_data;
351
352 impl_window = window->impl_window;
353
354 window_scale = gdk_window_get_scale_factor (impl_window);
355
356 paint_context = gdk_window_get_paint_gl_context (window, NULL);
357 if (paint_context == NULL)
358 {
359 g_warning ("gdk_cairo_draw_gl_render_buffer failed - no paint context");
360 return;
361 }
362
363 clip_region = gdk_cairo_region_from_clip (cr);
364
365 gdk_gl_context_make_current (paint_context);
366 paint_data = gdk_gl_context_get_paint_data (paint_context);
367
368 if (paint_data->tmp_framebuffer == 0)
369 glGenFramebuffersEXT (1, &paint_data->tmp_framebuffer);
370
371 if (source_type == GL_RENDERBUFFER)
372 {
373 glBindRenderbuffer (GL_RENDERBUFFER, source);
374 glGetRenderbufferParameteriv (GL_RENDERBUFFER, GL_RENDERBUFFER_ALPHA_SIZE, &alpha_size);
375 }
376 else if (source_type == GL_TEXTURE)
377 {
378 glBindTexture (GL_TEXTURE_2D, source);
379
380 if (gdk_gl_context_get_use_es (paint_context))
381 alpha_size = 1;
382 else
383 glGetTexLevelParameteriv (GL_TEXTURE_2D, 0, GL_TEXTURE_ALPHA_SIZE, &alpha_size);
384 }
385 else
386 {
387 g_warning ("Unsupported gl source type %d\n", source_type);
388 return;
389 }
390
391 group_target = cairo_get_group_target (cr);
392 direct_window = cairo_surface_get_user_data (group_target, &direct_key);
393
394 cairo_get_matrix (cr, &matrix);
395
396 dx = matrix.x0;
397 dy = matrix.y0;
398
399 /* Trivial == integer-only translation */
400 trivial_transform =
401 (double)dx == matrix.x0 && (double)dy == matrix.y0 &&
402 matrix.xx == 1.0 && matrix.xy == 0.0 &&
403 matrix.yx == 0.0 && matrix.yy == 1.0;
404
405 /* For direct paint of non-alpha renderbuffer, we can
406 just do a bitblit */
407 if ((_gdk_gl_flags & GDK_GL_SOFTWARE_DRAW_GL) == 0 &&
408 source_type == GL_RENDERBUFFER &&
409 alpha_size == 0 &&
410 direct_window != NULL &&
411 direct_window->current_paint.use_gl &&
412 trivial_transform &&
413 clip_region != NULL)
414 {
415 int unscaled_window_height;
416 int i;
417
418 /* Create a framebuffer with the source renderbuffer and
419 make it the current target for reads */
420 framebuffer = paint_data->tmp_framebuffer;
421 glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, framebuffer);
422 glFramebufferRenderbufferEXT (GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
423 GL_RENDERBUFFER_EXT, source);
424 glBindFramebufferEXT (GL_DRAW_FRAMEBUFFER_EXT, 0);
425
426 /* Translate to impl coords */
427 cairo_region_translate (clip_region, dx, dy);
428
429 glEnable (GL_SCISSOR_TEST);
430
431 gdk_window_get_unscaled_size (impl_window, NULL, &unscaled_window_height);
432
433 /* We can use glDrawBuffer on OpenGL only; on GLES 2.0 we are already
434 * double buffered so we don't need it...
435 */
436 if (!gdk_gl_context_get_use_es (paint_context))
437 glDrawBuffer (GL_BACK);
438 else
439 {
440 int maj, min;
441
442 gdk_gl_context_get_version (paint_context, &maj, &min);
443
444 /* ... but on GLES 3.0 we can use the vectorized glDrawBuffers
445 * call.
446 */
447 if ((maj * 100 + min) >= 300)
448 {
449 static const GLenum buffers[] = { GL_BACK };
450
451 glDrawBuffers (G_N_ELEMENTS (buffers), buffers);
452 }
453 }
454
455#define FLIP_Y(_y) (unscaled_window_height - (_y))
456
457 for (i = 0; i < cairo_region_num_rectangles (clip_region); i++)
458 {
459 cairo_rectangle_int_t clip_rect, dest;
460
461 cairo_region_get_rectangle (clip_region, i, &clip_rect);
462 clip_rect.x *= window_scale;
463 clip_rect.y *= window_scale;
464 clip_rect.width *= window_scale;
465 clip_rect.height *= window_scale;
466
467 glScissor (clip_rect.x, FLIP_Y (clip_rect.y + clip_rect.height),
468 clip_rect.width, clip_rect.height);
469
470 dest.x = dx * window_scale;
471 dest.y = dy * window_scale;
472 dest.width = width * window_scale / buffer_scale;
473 dest.height = height * window_scale / buffer_scale;
474
475 if (gdk_rectangle_intersect (&clip_rect, &dest, &dest))
476 {
477 int clipped_src_x = x + (dest.x - dx * window_scale);
478 int clipped_src_y = y + (height - dest.height - (dest.y - dy * window_scale));
479 glBlitFramebufferEXT(clipped_src_x, clipped_src_y,
480 (clipped_src_x + dest.width), (clipped_src_y + dest.height),
481 dest.x, FLIP_Y(dest.y + dest.height),
482 dest.x + dest.width, FLIP_Y(dest.y),
483 GL_COLOR_BUFFER_BIT, GL_NEAREST);
484 if (impl_window->current_paint.flushed_region)
485 {
486 cairo_rectangle_int_t flushed_rect;
487
488 flushed_rect.x = dest.x / window_scale;
489 flushed_rect.y = dest.y / window_scale;
490 flushed_rect.width = (dest.x + dest.width + window_scale - 1) / window_scale - flushed_rect.x;
491 flushed_rect.height = (dest.y + dest.height + window_scale - 1) / window_scale - flushed_rect.y;
492
493 cairo_region_union_rectangle (impl_window->current_paint.flushed_region,
494 &flushed_rect);
495 cairo_region_subtract_rectangle (impl_window->current_paint.need_blend_region,
496 &flushed_rect);
497 }
498 }
499 }
500
501 glDisable (GL_SCISSOR_TEST);
502
503 glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, 0);
504
505#undef FLIP_Y
506
507 }
508 /* For direct paint of alpha or non-alpha textures we can use texturing */
509 else if ((_gdk_gl_flags & GDK_GL_SOFTWARE_DRAW_GL) == 0 &&
510 source_type == GL_TEXTURE &&
511 direct_window != NULL &&
512 direct_window->current_paint.use_gl &&
513 trivial_transform &&
514 clip_region != NULL)
515 {
516 int unscaled_window_height;
517 GLint texture_width;
518 GLint texture_height;
519 int i, n_rects, n_quads;
520 GdkTexturedQuad *quads;
521 cairo_rectangle_int_t clip_rect;
522
523 /* Translate to impl coords */
524 cairo_region_translate (clip_region, dx, dy);
525
526 if (alpha_size != 0)
527 {
528 cairo_region_t *opaque_region, *blend_region;
529
530 opaque_region = cairo_region_copy (clip_region);
531 cairo_region_subtract (opaque_region, impl_window->current_paint.flushed_region);
532 cairo_region_subtract (opaque_region, impl_window->current_paint.need_blend_region);
533
534 if (!cairo_region_is_empty (opaque_region))
535 gdk_gl_texture_from_surface (impl_window->current_paint.surface,
536 opaque_region);
537
538 blend_region = cairo_region_copy (clip_region);
539 cairo_region_intersect (blend_region, impl_window->current_paint.need_blend_region);
540
541 glEnable (GL_BLEND);
542 if (!cairo_region_is_empty (blend_region))
543 gdk_gl_texture_from_surface (impl_window->current_paint.surface,
544 blend_region);
545
546 cairo_region_destroy (opaque_region);
547 cairo_region_destroy (blend_region);
548 }
549
550 glBindTexture (GL_TEXTURE_2D, source);
551
552 if (gdk_gl_context_get_use_es (paint_context))
553 {
554 texture_width = width;
555 texture_height = height;
556 }
557 else
558 {
559 glGetTexLevelParameteriv (GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &texture_width);
560 glGetTexLevelParameteriv (GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &texture_height);
561 }
562
563 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
564 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
565 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
566 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
567
568 glEnable (GL_SCISSOR_TEST);
569
570 gdk_window_get_unscaled_size (impl_window, NULL, &unscaled_window_height);
571
572#define FLIP_Y(_y) (unscaled_window_height - (_y))
573
574 cairo_region_get_extents (clip_region, &clip_rect);
575
576 glScissor (clip_rect.x * window_scale, FLIP_Y ((clip_rect.y + clip_rect.height) * window_scale),
577 clip_rect.width * window_scale, clip_rect.height * window_scale);
578
579 n_quads = 0;
580 n_rects = cairo_region_num_rectangles (clip_region);
581 quads = g_new (GdkTexturedQuad, n_rects);
582 for (i = 0; i < n_rects; i++)
583 {
584 cairo_rectangle_int_t dest;
585
586 cairo_region_get_rectangle (clip_region, i, &clip_rect);
587
588 clip_rect.x *= window_scale;
589 clip_rect.y *= window_scale;
590 clip_rect.width *= window_scale;
591 clip_rect.height *= window_scale;
592
593 dest.x = dx * window_scale;
594 dest.y = dy * window_scale;
595 dest.width = width * window_scale / buffer_scale;
596 dest.height = height * window_scale / buffer_scale;
597
598 if (gdk_rectangle_intersect (&clip_rect, &dest, &dest))
599 {
600 int clipped_src_x = x + (dest.x - dx * window_scale);
601 int clipped_src_y = y + (height - dest.height - (dest.y - dy * window_scale));
602 GdkTexturedQuad quad = {
603 dest.x, FLIP_Y(dest.y),
604 dest.x + dest.width, FLIP_Y(dest.y + dest.height),
605 clipped_src_x / (float)texture_width, (clipped_src_y + dest.height) / (float)texture_height,
606 (clipped_src_x + dest.width) / (float)texture_width, clipped_src_y / (float)texture_height,
607 };
608
609 quads[n_quads++] = quad;
610
611 if (impl_window->current_paint.flushed_region)
612 {
613 cairo_rectangle_int_t flushed_rect;
614
615 flushed_rect.x = dest.x / window_scale;
616 flushed_rect.y = dest.y / window_scale;
617 flushed_rect.width = (dest.x + dest.width + window_scale - 1) / window_scale - flushed_rect.x;
618 flushed_rect.height = (dest.y + dest.height + window_scale - 1) / window_scale - flushed_rect.y;
619
620 cairo_region_union_rectangle (impl_window->current_paint.flushed_region,
621 &flushed_rect);
622 cairo_region_subtract_rectangle (impl_window->current_paint.need_blend_region,
623 &flushed_rect);
624 }
625 }
626 }
627
628 if (n_quads > 0)
629 gdk_gl_texture_quads (paint_context, GL_TEXTURE_2D, n_quads, quads, FALSE);
630
631 g_free (quads);
632
633 if (alpha_size != 0)
634 glDisable (GL_BLEND);
635
636#undef FLIP_Y
637
638 }
639 else
640 {
641 /* Software fallback */
642 int major, minor, version;
643
644 gdk_gl_context_get_version (paint_context, &major, &minor);
645 version = major * 100 + minor;
646
647 /* TODO: Use glTexSubImage2D() and do a row-by-row copy to replace
648 * the GL_UNPACK_ROW_LENGTH support
649 */
650 if (gdk_gl_context_get_use_es (paint_context) &&
651 !(version >= 300 || gdk_gl_context_has_unpack_subimage (paint_context)))
652 goto out;
653
654 /* TODO: avoid reading back non-required data due to dest clip */
655 image = cairo_surface_create_similar_image (cairo_get_target (cr),
656 (alpha_size == 0) ? CAIRO_FORMAT_RGB24 : CAIRO_FORMAT_ARGB32,
657 width, height);
658
659 cairo_surface_set_device_scale (image, buffer_scale, buffer_scale);
660
661 framebuffer = paint_data->tmp_framebuffer;
662 glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, framebuffer);
663
664 if (source_type == GL_RENDERBUFFER)
665 {
666 /* Create a framebuffer with the source renderbuffer and
667 make it the current target for reads */
668 glFramebufferRenderbufferEXT (GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
669 GL_RENDERBUFFER_EXT, source);
670 }
671 else
672 {
673 glFramebufferTexture2DEXT (GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
674 GL_TEXTURE_2D, source, 0);
675 }
676
677 glPixelStorei (GL_PACK_ALIGNMENT, 4);
678 glPixelStorei (GL_PACK_ROW_LENGTH, cairo_image_surface_get_stride (image) / 4);
679
680 /* The implicit format conversion is going to make this path slower */
681 if (!gdk_gl_context_get_use_es (paint_context))
682 glReadPixels (x, y, width, height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV,
683 cairo_image_surface_get_data (image));
684 else
685 glReadPixels (x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE,
686 cairo_image_surface_get_data (image));
687
688 glPixelStorei (GL_PACK_ROW_LENGTH, 0);
689
690 glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, 0);
691
692 cairo_surface_mark_dirty (image);
693
694 /* Invert due to opengl having different origin */
695 cairo_scale (cr, 1, -1);
696 cairo_translate (cr, 0, -height / buffer_scale);
697
698 cairo_set_source_surface (cr, image, 0, 0);
699 cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
700 cairo_paint (cr);
701
702 cairo_surface_destroy (image);
703 }
704
705out:
706 if (clip_region)
707 cairo_region_destroy (clip_region);
708
709}
710
711/* This is always called with the paint context current */
712void
713gdk_gl_texture_from_surface (cairo_surface_t *surface,
714 cairo_region_t *region)
715{
716 GdkGLContext *paint_context;
717 cairo_surface_t *image;
718 double device_x_offset, device_y_offset;
719 cairo_rectangle_int_t rect, e;
720 int n_rects, i;
721 GdkWindow *window;
722 int unscaled_window_height;
723 unsigned int texture_id;
724 int window_scale;
725 double sx, sy;
726 float umax, vmax;
727 gboolean use_texture_rectangle;
728 guint target;
729 paint_context = gdk_gl_context_get_current ();
730 if ((_gdk_gl_flags & GDK_GL_SOFTWARE_DRAW_SURFACE) == 0 &&
731 paint_context &&
732 GDK_GL_CONTEXT_GET_CLASS (paint_context)->texture_from_surface &&
733 GDK_GL_CONTEXT_GET_CLASS (paint_context)->texture_from_surface (paint_context, surface, region))
734 return;
735
736 /* Software fallback */
737 use_texture_rectangle = gdk_gl_context_use_texture_rectangle (paint_context);
738
739 window = gdk_gl_context_get_window (paint_context);
740 window_scale = gdk_window_get_scale_factor (window);
741 gdk_window_get_unscaled_size (window, NULL, &unscaled_window_height);
742
743 sx = sy = 1;
744 cairo_surface_get_device_scale (window->current_paint.surface, &sx, &sy);
745
746 cairo_surface_get_device_offset (surface,
747 &device_x_offset, &device_y_offset);
748
749 glGenTextures (1, &texture_id);
750 if (use_texture_rectangle)
751 target = GL_TEXTURE_RECTANGLE_ARB;
752 else
753 target = GL_TEXTURE_2D;
754
755 glBindTexture (target, texture_id);
756 glEnable (GL_SCISSOR_TEST);
757
758 glTexParameteri (target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
759 glTexParameteri (target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
760 glTexParameteri (target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
761 glTexParameteri (target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
762
763 n_rects = cairo_region_num_rectangles (region);
764
765#define FLIP_Y(_y) (unscaled_window_height - (_y))
766
767 for (i = 0; i < n_rects; i++)
768 {
769 cairo_region_get_rectangle (region, i, &rect);
770
771 glScissor (rect.x * window_scale, FLIP_Y ((rect.y + rect.height) * window_scale),
772 rect.width * window_scale, rect.height * window_scale);
773
774 e = rect;
775 e.x *= sx;
776 e.y *= sy;
777 e.x += (int)device_x_offset;
778 e.y += (int)device_y_offset;
779 e.width *= sx;
780 e.height *= sy;
781 image = cairo_surface_map_to_image (surface, &e);
782
783 gdk_gl_context_upload_texture (paint_context, image, e.width, e.height, target);
784
785 cairo_surface_unmap_image (surface, image);
786
787 if (use_texture_rectangle)
788 {
789 umax = rect.width * sx;
790 vmax = rect.height * sy;
791 }
792 else
793 {
794 umax = 1.0;
795 vmax = 1.0;
796 }
797
798 {
799 GdkTexturedQuad quad = {
800 rect.x * window_scale, FLIP_Y(rect.y * window_scale),
801 (rect.x + rect.width) * window_scale, FLIP_Y((rect.y + rect.height) * window_scale),
802 0, 0,
803 umax, vmax,
804 };
805
806 /* We don't want to combine the quads here, because they have different textures.
807 * And we don't want to upload the unused source areas to make it one texture. */
808 gdk_gl_texture_quads (paint_context, target, 1, &quad, TRUE);
809 }
810 }
811
812#undef FLIP_Y
813
814 glDisable (GL_SCISSOR_TEST);
815 glDeleteTextures (1, &texture_id);
816}
817