1 /* cairo - a vector graphics library with display and print output
3 * Copyright © 2009 Eric Anholt
4 * Copyright © 2009 Chris Wilson
5 * Copyright © 2005,2010 Red Hat, Inc
6 * Copyright © 2011 Intel Corporation
8 * This library is free software; you can redistribute it and/or
9 * modify it either under the terms of the GNU Lesser General Public
10 * License version 2.1 as published by the Free Software Foundation
11 * (the "LGPL") or, at your option, under the terms of the Mozilla
12 * Public License Version 1.1 (the "MPL"). If you do not alter this
13 * notice, a recipient may use your version of this file under either
14 * the MPL or the LGPL.
16 * You should have received a copy of the LGPL along with this library
17 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
19 * You should have received a copy of the MPL along with this library
20 * in the file COPYING-MPL-1.1
22 * The contents of this file are subject to the Mozilla Public License
23 * Version 1.1 (the "License"); you may not use this file except in
24 * compliance with the License. You may obtain a copy of the License at
25 * http://www.mozilla.org/MPL/
27 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
28 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
29 * the specific language governing rights and limitations.
31 * The Original Code is the cairo graphics library.
33 * The Initial Developer of the Original Code is Red Hat, Inc.
36 * Benjamin Otte <otte@gnome.org>
37 * Carl Worth <cworth@cworth.org>
38 * Chris Wilson <chris@chris-wilson.co.uk>
39 * Eric Anholt <eric@anholt.net>
44 #include "cairo-gl-private.h"
46 #include "cairo-composite-rectangles-private.h"
47 #include "cairo-compositor-private.h"
48 #include "cairo-default-context-private.h"
49 #include "cairo-error-private.h"
50 #include "cairo-image-surface-private.h"
51 #include "cairo-spans-compositor-private.h"
52 #include "cairo-surface-backend-private.h"
53 #include "cairo-surface-offset-private.h"
55 static cairo_int_status_t
56 acquire (void *abstract_dst)
58 return CAIRO_STATUS_SUCCESS;
61 static cairo_int_status_t
62 release (void *abstract_dst)
64 return CAIRO_STATUS_SUCCESS;
67 static cairo_int_status_t
68 set_clip_region (void *_surface,
69 cairo_region_t *region)
71 cairo_gl_surface_t *surface = _surface;
73 surface->clip_region = region;
74 return CAIRO_STATUS_SUCCESS;
77 static cairo_int_status_t
78 draw_image_boxes (void *_dst,
79 cairo_image_surface_t *image,
83 cairo_gl_surface_t *dst = _dst;
84 struct _cairo_boxes_chunk *chunk;
87 if (_cairo_gl_surface_is_texture (dst))
88 return CAIRO_INT_STATUS_UNSUPPORTED;
90 for (chunk = &boxes->chunks; chunk; chunk = chunk->next) {
91 for (i = 0; i < chunk->count; i++) {
92 cairo_box_t *b = &chunk->base[i];
93 int x = _cairo_fixed_integer_part (b->p1.x);
94 int y = _cairo_fixed_integer_part (b->p1.y);
95 int w = _cairo_fixed_integer_part (b->p2.x) - x;
96 int h = _cairo_fixed_integer_part (b->p2.y) - y;
97 cairo_status_t status;
99 status = _cairo_gl_surface_draw_image (dst, image,
103 if (unlikely (status))
108 return CAIRO_STATUS_SUCCESS;
112 emit_aligned_boxes (cairo_gl_context_t *ctx,
113 const cairo_boxes_t *boxes)
115 const struct _cairo_boxes_chunk *chunk;
118 for (chunk = &boxes->chunks; chunk; chunk = chunk->next) {
119 for (i = 0; i < chunk->count; i++) {
120 int x1 = _cairo_fixed_integer_part (chunk->base[i].p1.x);
121 int y1 = _cairo_fixed_integer_part (chunk->base[i].p1.y);
122 int x2 = _cairo_fixed_integer_part (chunk->base[i].p2.x);
123 int y2 = _cairo_fixed_integer_part (chunk->base[i].p2.y);
124 _cairo_gl_composite_emit_rect (ctx, x1, y1, x2, y2, 0);
129 static cairo_int_status_t
130 fill_boxes (void *_dst,
132 const cairo_color_t *color,
133 cairo_boxes_t *boxes)
135 cairo_gl_composite_t setup;
136 cairo_gl_context_t *ctx;
137 cairo_int_status_t status;
139 status = _cairo_gl_composite_init (&setup, op, _dst, FALSE);
140 if (unlikely (status))
143 _cairo_gl_composite_set_solid_source (&setup, color);
145 status = _cairo_gl_composite_begin (&setup, &ctx);
146 if (unlikely (status))
149 emit_aligned_boxes (ctx, boxes);
150 status = _cairo_gl_context_release (ctx, CAIRO_STATUS_SUCCESS);
153 _cairo_gl_composite_fini (&setup);
157 static cairo_int_status_t
158 composite_boxes (void *_dst,
160 cairo_surface_t *abstract_src,
161 cairo_surface_t *abstract_mask,
168 cairo_boxes_t *boxes,
169 const cairo_rectangle_int_t *extents)
171 cairo_gl_composite_t setup;
172 cairo_gl_context_t *ctx;
173 cairo_int_status_t status;
175 status = _cairo_gl_composite_init (&setup, op, _dst, FALSE);
176 if (unlikely (status))
179 _cairo_gl_composite_set_source_operand (&setup,
180 source_to_operand (abstract_src));
181 _cairo_gl_operand_translate (&setup.src, dst_x-src_x, dst_y-src_y);
183 _cairo_gl_composite_set_mask_operand (&setup,
184 source_to_operand (abstract_mask));
185 _cairo_gl_operand_translate (&setup.mask, dst_x-mask_x, dst_y-mask_y);
187 status = _cairo_gl_composite_begin (&setup, &ctx);
188 if (unlikely (status))
191 emit_aligned_boxes (ctx, boxes);
192 status = _cairo_gl_context_release (ctx, CAIRO_STATUS_SUCCESS);
195 _cairo_gl_composite_fini (&setup);
199 static cairo_int_status_t
200 composite (void *_dst,
202 cairo_surface_t *abstract_src,
203 cairo_surface_t *abstract_mask,
213 cairo_gl_composite_t setup;
214 cairo_gl_context_t *ctx;
215 cairo_int_status_t status;
217 status = _cairo_gl_composite_init (&setup, op, _dst, FALSE);
218 if (unlikely (status))
221 _cairo_gl_composite_set_source_operand (&setup,
222 source_to_operand (abstract_src));
223 _cairo_gl_operand_translate (&setup.src, dst_x-src_x, dst_y-src_y);
225 _cairo_gl_composite_set_mask_operand (&setup,
226 source_to_operand (abstract_mask));
227 _cairo_gl_operand_translate (&setup.mask, dst_x-mask_x, dst_y-mask_y);
229 status = _cairo_gl_composite_begin (&setup, &ctx);
230 if (unlikely (status))
234 _cairo_gl_composite_emit_rect (ctx, dst_x, dst_y, dst_x+width, dst_y+height, 0);
235 status = _cairo_gl_context_release (ctx, CAIRO_STATUS_SUCCESS);
238 _cairo_gl_composite_fini (&setup);
242 static cairo_int_status_t
244 cairo_surface_t *src,
245 cairo_surface_t *mask,
255 cairo_int_status_t status;
257 /* we could avoid some repetition... */
258 status = composite (dst, CAIRO_OPERATOR_DEST_OUT, mask, NULL,
263 if (unlikely (status))
266 status = composite (dst, CAIRO_OPERATOR_ADD, src, mask,
271 if (unlikely (status))
274 return CAIRO_STATUS_SUCCESS;
277 static cairo_int_status_t
278 traps_to_operand (void *_dst,
279 const cairo_rectangle_int_t *extents,
280 cairo_antialias_t antialias,
281 cairo_traps_t *traps,
282 cairo_gl_operand_t *operand,
283 int dst_x, int dst_y)
285 pixman_format_code_t pixman_format;
286 pixman_image_t *pixman_image;
287 cairo_surface_t *image, *mask;
288 cairo_surface_pattern_t pattern;
289 cairo_status_t status;
291 pixman_format = antialias != CAIRO_ANTIALIAS_NONE ? PIXMAN_a8 : PIXMAN_a1;
292 pixman_image = pixman_image_create_bits (pixman_format,
296 if (unlikely (pixman_image == NULL))
297 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
299 _pixman_image_add_traps (pixman_image, extents->x, extents->y, traps);
300 image = _cairo_image_surface_create_for_pixman_image (pixman_image,
302 if (unlikely (image->status)) {
303 pixman_image_unref (pixman_image);
304 return image->status;
307 /* GLES2 only supports RGB/RGBA when uploading */
308 if (_cairo_gl_get_flavor () == CAIRO_GL_FLAVOR_ES) {
309 cairo_surface_pattern_t pattern;
310 cairo_surface_t *rgba_image;
312 /* XXX perform this fixup inside _cairo_gl_draw_image() */
315 _cairo_image_surface_create_with_pixman_format (NULL,
316 _cairo_is_little_endian () ? PIXMAN_a8b8g8r8 : PIXMAN_r8g8b8a8,
320 if (unlikely (rgba_image->status))
321 return rgba_image->status;
323 _cairo_pattern_init_for_surface (&pattern, image);
324 status = _cairo_surface_paint (rgba_image, CAIRO_OPERATOR_SOURCE,
325 &pattern.base, NULL);
326 _cairo_pattern_fini (&pattern.base);
328 cairo_surface_destroy (image);
331 if (unlikely (status)) {
332 cairo_surface_destroy (image);
337 mask = _cairo_surface_create_similar_scratch (_dst,
338 CAIRO_CONTENT_COLOR_ALPHA,
341 if (unlikely (mask->status)) {
342 cairo_surface_destroy (image);
346 status = _cairo_gl_surface_draw_image ((cairo_gl_surface_t *)mask,
347 (cairo_image_surface_t *)image,
349 extents->width, extents->height,
351 cairo_surface_destroy (image);
353 if (unlikely (status))
356 _cairo_pattern_init_for_surface (&pattern, mask);
357 cairo_matrix_init_translate (&pattern.base.matrix,
358 -extents->x+dst_x, -extents->y+dst_y);
359 pattern.base.filter = CAIRO_FILTER_NEAREST;
360 pattern.base.extend = CAIRO_EXTEND_NONE;
361 status = _cairo_gl_operand_init (operand, &pattern.base, _dst,
362 &_cairo_unbounded_rectangle,
363 &_cairo_unbounded_rectangle,
365 _cairo_pattern_fini (&pattern.base);
367 if (unlikely (status))
370 operand->texture.owns_surface = mask;
371 return CAIRO_STATUS_SUCCESS;
374 cairo_surface_destroy (mask);
378 static cairo_int_status_t
379 composite_traps (void *_dst,
381 cairo_surface_t *abstract_src,
386 const cairo_rectangle_int_t *extents,
387 cairo_antialias_t antialias,
388 cairo_traps_t *traps)
390 cairo_gl_composite_t setup;
391 cairo_gl_context_t *ctx;
392 cairo_int_status_t status;
394 status = _cairo_gl_composite_init (&setup, op, _dst, FALSE);
395 if (unlikely (status))
398 _cairo_gl_composite_set_source_operand (&setup,
399 source_to_operand (abstract_src));
400 _cairo_gl_operand_translate (&setup.src, -src_x-dst_x, -src_y-dst_y);
401 status = traps_to_operand (_dst, extents, antialias, traps, &setup.mask, dst_x, dst_y);
402 if (unlikely (status))
405 status = _cairo_gl_composite_begin (&setup, &ctx);
406 if (unlikely (status))
410 _cairo_gl_composite_emit_rect (ctx,
411 extents->x-dst_x, extents->y-dst_y,
412 extents->x-dst_x+extents->width,
413 extents->y-dst_y+extents->height, 0);
414 status = _cairo_gl_context_release (ctx, CAIRO_STATUS_SUCCESS);
417 _cairo_gl_composite_fini (&setup);
421 static cairo_gl_surface_t *
422 tristrip_to_surface (void *_dst,
423 const cairo_rectangle_int_t *extents,
424 cairo_antialias_t antialias,
425 cairo_tristrip_t *strip)
427 pixman_format_code_t pixman_format;
428 pixman_image_t *pixman_image;
429 cairo_surface_t *image, *mask;
430 cairo_status_t status;
432 pixman_format = antialias != CAIRO_ANTIALIAS_NONE ? PIXMAN_a8 : PIXMAN_a1,
433 pixman_image = pixman_image_create_bits (pixman_format,
437 if (unlikely (pixman_image == NULL))
438 return (cairo_gl_surface_t *)_cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
440 _pixman_image_add_tristrip (pixman_image, extents->x, extents->y, strip);
441 image = _cairo_image_surface_create_for_pixman_image (pixman_image,
443 if (unlikely (image->status)) {
444 pixman_image_unref (pixman_image);
445 return (cairo_gl_surface_t *)image;
448 mask = _cairo_surface_create_similar_scratch (_dst,
449 CAIRO_CONTENT_COLOR_ALPHA,
452 if (unlikely (mask->status)) {
453 cairo_surface_destroy (image);
454 return (cairo_gl_surface_t *)mask;
457 status = _cairo_gl_surface_draw_image ((cairo_gl_surface_t *)mask,
458 (cairo_image_surface_t *)image,
460 extents->width, extents->height,
462 cairo_surface_destroy (image);
463 if (unlikely (status)) {
464 cairo_surface_destroy (mask);
465 return (cairo_gl_surface_t*)_cairo_surface_create_in_error (status);
468 return (cairo_gl_surface_t*)mask;
471 static cairo_int_status_t
472 composite_tristrip (void *_dst,
474 cairo_surface_t *abstract_src,
479 const cairo_rectangle_int_t *extents,
480 cairo_antialias_t antialias,
481 cairo_tristrip_t *strip)
483 cairo_gl_composite_t setup;
484 cairo_gl_context_t *ctx;
485 cairo_gl_surface_t *mask;
486 cairo_int_status_t status;
488 mask = tristrip_to_surface (_dst, extents, antialias, strip);
489 if (unlikely (mask->base.status))
490 return mask->base.status;
492 status = _cairo_gl_composite_init (&setup, op, _dst, FALSE);
493 if (unlikely (status))
496 _cairo_gl_composite_set_source_operand (&setup,
497 source_to_operand (abstract_src));
499 //_cairo_gl_composite_set_mask_surface (&setup, mask, 0, 0);
501 status = _cairo_gl_composite_begin (&setup, &ctx);
502 if (unlikely (status))
506 _cairo_gl_composite_emit_rect (ctx,
508 dst_x+extents->width,
509 dst_y+extents->height, 0);
510 status = _cairo_gl_context_release (ctx, CAIRO_STATUS_SUCCESS);
513 _cairo_gl_composite_fini (&setup);
514 cairo_surface_destroy (&mask->base);
518 static cairo_int_status_t
519 check_composite (const cairo_composite_rectangles_t *extents)
521 if (! _cairo_gl_operator_is_supported (extents->op))
522 return UNSUPPORTED ("unsupported operator");
524 return CAIRO_STATUS_SUCCESS;
527 const cairo_compositor_t *
528 _cairo_gl_traps_compositor_get (void)
530 static cairo_traps_compositor_t compositor;
532 if (compositor.base.delegate == NULL) {
533 _cairo_traps_compositor_init (&compositor, &_cairo_fallback_compositor);
534 compositor.acquire = acquire;
535 compositor.release = release;
536 compositor.set_clip_region = set_clip_region;
537 compositor.pattern_to_surface = _cairo_gl_pattern_to_source;
538 compositor.draw_image_boxes = draw_image_boxes;
539 //compositor.copy_boxes = copy_boxes;
540 compositor.fill_boxes = fill_boxes;
541 compositor.check_composite = check_composite;
542 compositor.composite = composite;
543 compositor.lerp = lerp;
544 //compositor.check_composite_boxes = check_composite_boxes;
545 compositor.composite_boxes = composite_boxes;
546 //compositor.check_composite_traps = check_composite_traps;
547 compositor.composite_traps = composite_traps;
548 //compositor.check_composite_tristrip = check_composite_traps;
549 compositor.composite_tristrip = composite_tristrip;
550 compositor.check_composite_glyphs = _cairo_gl_check_composite_glyphs;
551 compositor.composite_glyphs = _cairo_gl_composite_glyphs;
554 return &compositor.base;