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 for (chunk = &boxes->chunks; chunk; chunk = chunk->next) {
88 for (i = 0; i < chunk->count; i++) {
89 cairo_box_t *b = &chunk->base[i];
90 int x = _cairo_fixed_integer_part (b->p1.x);
91 int y = _cairo_fixed_integer_part (b->p1.y);
92 int w = _cairo_fixed_integer_part (b->p2.x) - x;
93 int h = _cairo_fixed_integer_part (b->p2.y) - y;
94 cairo_status_t status;
96 status = _cairo_gl_surface_draw_image (dst, image,
101 if (unlikely (status))
106 return CAIRO_STATUS_SUCCESS;
110 emit_aligned_boxes (cairo_gl_context_t *ctx,
111 const cairo_boxes_t *boxes)
113 const struct _cairo_boxes_chunk *chunk;
114 cairo_gl_emit_rect_t emit = _cairo_gl_context_choose_emit_rect (ctx);
117 for (chunk = &boxes->chunks; chunk; chunk = chunk->next) {
118 for (i = 0; i < chunk->count; i++) {
119 int x1 = _cairo_fixed_integer_part (chunk->base[i].p1.x);
120 int y1 = _cairo_fixed_integer_part (chunk->base[i].p1.y);
121 int x2 = _cairo_fixed_integer_part (chunk->base[i].p2.x);
122 int y2 = _cairo_fixed_integer_part (chunk->base[i].p2.y);
123 emit (ctx, x1, y1, x2, y2);
128 static cairo_int_status_t
129 fill_boxes (void *_dst,
131 const cairo_color_t *color,
132 cairo_boxes_t *boxes)
134 cairo_gl_composite_t setup;
135 cairo_gl_context_t *ctx;
136 cairo_int_status_t status;
138 status = _cairo_gl_composite_init (&setup, op, _dst, FALSE);
139 if (unlikely (status))
142 _cairo_gl_composite_set_solid_source (&setup, color);
144 status = _cairo_gl_composite_begin (&setup, &ctx);
145 if (unlikely (status))
148 emit_aligned_boxes (ctx, boxes);
149 status = _cairo_gl_context_release (ctx, CAIRO_STATUS_SUCCESS);
152 _cairo_gl_composite_fini (&setup);
156 static cairo_int_status_t
157 composite_boxes (void *_dst,
159 cairo_surface_t *abstract_src,
160 cairo_surface_t *abstract_mask,
167 cairo_boxes_t *boxes,
168 const cairo_rectangle_int_t *extents)
170 cairo_gl_composite_t setup;
171 cairo_gl_context_t *ctx;
172 cairo_int_status_t status;
174 status = _cairo_gl_composite_init (&setup, op, _dst, FALSE);
175 if (unlikely (status))
178 _cairo_gl_composite_set_source_operand (&setup,
179 source_to_operand (abstract_src));
180 _cairo_gl_operand_translate (&setup.src, dst_x-src_x, dst_y-src_y);
182 _cairo_gl_composite_set_mask_operand (&setup,
183 source_to_operand (abstract_mask));
184 _cairo_gl_operand_translate (&setup.mask, dst_x-mask_x, dst_y-mask_y);
186 status = _cairo_gl_composite_begin (&setup, &ctx);
187 if (unlikely (status))
190 emit_aligned_boxes (ctx, boxes);
191 status = _cairo_gl_context_release (ctx, CAIRO_STATUS_SUCCESS);
194 _cairo_gl_composite_fini (&setup);
198 static cairo_int_status_t
199 composite (void *_dst,
201 cairo_surface_t *abstract_src,
202 cairo_surface_t *abstract_mask,
212 cairo_gl_composite_t setup;
213 cairo_gl_context_t *ctx;
214 cairo_int_status_t status;
215 cairo_gl_operand_t *temp_operand = NULL;
217 status = _cairo_gl_composite_init (&setup, op, _dst, FALSE);
218 if (unlikely (status))
221 temp_operand = source_to_operand (abstract_src);
222 if (temp_operand == NULL) {
223 status = CAIRO_STATUS_NULL_POINTER;
227 _cairo_gl_composite_set_source_operand (&setup, temp_operand);
228 _cairo_gl_operand_translate (&setup.src, dst_x-src_x, dst_y-src_y);
230 _cairo_gl_composite_set_mask_operand (&setup,
231 source_to_operand (abstract_mask));
232 _cairo_gl_operand_translate (&setup.mask, dst_x-mask_x, dst_y-mask_y);
234 status = _cairo_gl_composite_begin (&setup, &ctx);
235 if (unlikely (status))
239 _cairo_gl_context_emit_rect (ctx, dst_x, dst_y, dst_x+width, dst_y+height);
240 status = _cairo_gl_context_release (ctx, CAIRO_STATUS_SUCCESS);
243 _cairo_gl_composite_fini (&setup);
247 static cairo_int_status_t
249 cairo_surface_t *src,
250 cairo_surface_t *mask,
260 cairo_int_status_t status;
262 /* we could avoid some repetition... */
263 status = composite (dst, CAIRO_OPERATOR_DEST_OUT, mask, NULL,
268 if (unlikely (status))
271 status = composite (dst, CAIRO_OPERATOR_ADD, src, mask,
276 if (unlikely (status))
279 return CAIRO_STATUS_SUCCESS;
282 static cairo_int_status_t
283 lerp_color_glyph (void *dst,
284 cairo_surface_t *src,
285 cairo_surface_t *mask,
295 cairo_int_status_t status;
297 /* we could avoid some repetition... */
298 status = composite (dst, CAIRO_OPERATOR_DEST_OUT, mask, src,
303 if (unlikely (status))
306 status = composite (dst, CAIRO_OPERATOR_ADD, src, mask,
311 if (unlikely (status))
314 return CAIRO_STATUS_SUCCESS;
317 static cairo_int_status_t
318 traps_to_operand (void *_dst,
319 const cairo_rectangle_int_t *extents,
320 cairo_antialias_t antialias,
321 cairo_traps_t *traps,
322 cairo_gl_operand_t *operand,
323 int dst_x, int dst_y)
325 pixman_format_code_t pixman_format;
326 pixman_image_t *pixman_image;
327 cairo_surface_t *image, *mask;
328 cairo_surface_pattern_t pattern;
329 cairo_status_t status;
331 pixman_format = antialias != CAIRO_ANTIALIAS_NONE ? PIXMAN_a8 : PIXMAN_a1;
332 pixman_image = pixman_image_create_bits (pixman_format,
336 if (unlikely (pixman_image == NULL))
337 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
339 _pixman_image_add_traps (pixman_image, extents->x, extents->y, traps);
340 image = _cairo_image_surface_create_for_pixman_image (pixman_image,
342 if (unlikely (image->status)) {
343 pixman_image_unref (pixman_image);
344 status = image->status;
345 cairo_surface_destroy (image);
349 mask = _cairo_surface_create_scratch (_dst,
350 CAIRO_CONTENT_COLOR_ALPHA,
355 if (unlikely (mask->status)) {
356 cairo_surface_destroy (image);
360 status = _cairo_gl_surface_draw_image ((cairo_gl_surface_t *)mask,
361 (cairo_image_surface_t *)image,
363 extents->width, extents->height,
366 cairo_surface_destroy (image);
368 if (unlikely (status))
371 _cairo_pattern_init_for_surface (&pattern, mask);
372 cairo_matrix_init_translate (&pattern.base.matrix,
373 -extents->x+dst_x, -extents->y+dst_y);
374 pattern.base.filter = CAIRO_FILTER_NEAREST;
375 pattern.base.extend = CAIRO_EXTEND_NONE;
376 status = _cairo_gl_operand_init (operand, &pattern.base, _dst,
377 &_cairo_unbounded_rectangle,
378 &_cairo_unbounded_rectangle,
380 _cairo_pattern_fini (&pattern.base);
382 if (unlikely (status))
385 operand->texture.owns_surface = (cairo_gl_surface_t *)mask;
386 return CAIRO_STATUS_SUCCESS;
389 cairo_surface_destroy (mask);
393 static cairo_int_status_t
394 composite_traps (void *_dst,
396 cairo_surface_t *abstract_src,
401 const cairo_rectangle_int_t *extents,
402 cairo_antialias_t antialias,
403 cairo_traps_t *traps)
405 cairo_gl_composite_t setup;
406 cairo_gl_context_t *ctx;
407 cairo_int_status_t status;
408 cairo_surface_t *src = (cairo_surface_t *) abstract_src;
409 cairo_surface_t *dst = (cairo_surface_t *) _dst;
410 cairo_surface_t *new_surface = NULL;
411 cairo_surface_t *new_src = (cairo_surface_t *) abstract_src;
412 int new_src_x, new_src_y;
413 cairo_surface_pattern_t new_surface_pattern;
416 new_surface = _cairo_surface_create_similar_scratch (src,
420 status = new_surface->status;
421 if (unlikely (status)) {
422 cairo_surface_destroy (new_surface);
426 _cairo_pattern_init_for_surface (&new_surface_pattern, src);
427 new_surface_pattern.base.extend = CAIRO_EXTEND_NONE;
428 new_surface_pattern.base.filter = CAIRO_FILTER_NEAREST;
429 status = _cairo_surface_paint (new_surface, CAIRO_OPERATOR_SOURCE,
430 &new_surface_pattern.base, NULL);
432 if (unlikely (status)) {
433 _cairo_pattern_fini (&new_surface_pattern.base);
434 cairo_surface_destroy (new_surface);
438 new_src = _cairo_gl_pattern_to_source (dst,
439 &new_surface_pattern.base,
442 &new_src_x, &new_src_y);
445 status = _cairo_gl_composite_init (&setup, op, _dst, FALSE);
446 if (unlikely (status))
449 _cairo_gl_composite_set_source_operand (&setup,
450 source_to_operand (new_src));
452 _cairo_gl_operand_translate (&setup.src, -src_x-dst_x, -src_y-dst_y);
453 status = traps_to_operand (_dst, extents, antialias, traps, &setup.mask, dst_x, dst_y);
454 if (unlikely (status))
457 status = _cairo_gl_composite_begin (&setup, &ctx);
458 if (unlikely (status))
462 _cairo_gl_context_emit_rect (ctx,
463 extents->x-dst_x, extents->y-dst_y,
464 extents->x-dst_x+extents->width,
465 extents->y-dst_y+extents->height);
466 status = _cairo_gl_context_release (ctx, CAIRO_STATUS_SUCCESS);
469 _cairo_gl_composite_fini (&setup);
471 if (new_src != src) {
472 cairo_surface_destroy (new_src);
473 cairo_surface_destroy (new_surface);
474 _cairo_pattern_fini (&new_surface_pattern.base);
480 static cairo_gl_surface_t *
481 tristrip_to_surface (void *_dst,
482 const cairo_rectangle_int_t *extents,
483 cairo_antialias_t antialias,
484 cairo_tristrip_t *strip)
486 pixman_format_code_t pixman_format;
487 pixman_image_t *pixman_image;
488 cairo_surface_t *image, *mask;
489 cairo_status_t status;
491 pixman_format = antialias != CAIRO_ANTIALIAS_NONE ? PIXMAN_a8 : PIXMAN_a1,
492 pixman_image = pixman_image_create_bits (pixman_format,
496 if (unlikely (pixman_image == NULL))
497 return (cairo_gl_surface_t *)_cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
499 _pixman_image_add_tristrip (pixman_image, extents->x, extents->y, strip);
500 image = _cairo_image_surface_create_for_pixman_image (pixman_image,
502 if (unlikely (image->status)) {
503 pixman_image_unref (pixman_image);
504 return (cairo_gl_surface_t *)image;
507 mask = _cairo_surface_create_scratch (_dst,
508 CAIRO_CONTENT_COLOR_ALPHA,
512 if (unlikely (mask->status)) {
513 cairo_surface_destroy (image);
514 return (cairo_gl_surface_t *)mask;
517 status = _cairo_gl_surface_draw_image ((cairo_gl_surface_t *)mask,
518 (cairo_image_surface_t *)image,
520 extents->width, extents->height,
523 cairo_surface_destroy (image);
524 if (unlikely (status)) {
525 cairo_surface_destroy (mask);
526 return (cairo_gl_surface_t*)_cairo_surface_create_in_error (status);
529 return (cairo_gl_surface_t*)mask;
532 static cairo_int_status_t
533 composite_tristrip (void *_dst,
535 cairo_surface_t *abstract_src,
540 const cairo_rectangle_int_t *extents,
541 cairo_antialias_t antialias,
542 cairo_tristrip_t *strip)
544 cairo_gl_composite_t setup;
545 cairo_gl_context_t *ctx;
546 cairo_gl_surface_t *mask;
547 cairo_int_status_t status;
548 cairo_gl_operand_t *temp_operand = NULL;
550 mask = tristrip_to_surface (_dst, extents, antialias, strip);
551 if (unlikely (mask->base.status)) {
552 status = mask->base.status;
553 cairo_surface_destroy (&mask->base);
557 status = _cairo_gl_composite_init (&setup, op, _dst, FALSE);
558 if (unlikely (status))
561 temp_operand = source_to_operand (abstract_src);
562 if (temp_operand == NULL) {
563 status = CAIRO_STATUS_NULL_POINTER;
566 _cairo_gl_composite_set_source_operand (&setup,
569 //_cairo_gl_composite_set_mask_surface (&setup, mask, 0, 0);
571 status = _cairo_gl_composite_begin (&setup, &ctx);
572 if (unlikely (status))
576 _cairo_gl_context_emit_rect (ctx,
578 dst_x+extents->width,
579 dst_y+extents->height);
580 status = _cairo_gl_context_release (ctx, CAIRO_STATUS_SUCCESS);
583 _cairo_gl_composite_fini (&setup);
584 cairo_surface_destroy (&mask->base);
588 static cairo_int_status_t
589 check_composite (const cairo_composite_rectangles_t *extents)
591 if (! _cairo_gl_operator_is_supported (extents->op))
592 return UNSUPPORTED ("unsupported operator");
594 return CAIRO_STATUS_SUCCESS;
597 const cairo_compositor_t *
598 _cairo_gl_traps_compositor_get (void)
600 static cairo_traps_compositor_t compositor;
602 if (compositor.base.delegate == NULL) {
603 _cairo_traps_compositor_init (&compositor, &_cairo_fallback_compositor);
604 compositor.acquire = acquire;
605 compositor.release = release;
606 compositor.set_clip_region = set_clip_region;
607 compositor.pattern_to_surface = _cairo_gl_pattern_to_source;
608 compositor.draw_image_boxes = draw_image_boxes;
609 //compositor.copy_boxes = copy_boxes;
610 compositor.fill_boxes = fill_boxes;
611 compositor.check_composite = check_composite;
612 compositor.composite = composite;
613 compositor.lerp = lerp;
615 compositor.lerp_color_glyph = lerp_color_glyph;
616 //compositor.check_composite_boxes = check_composite_boxes;
617 compositor.composite_boxes = composite_boxes;
618 //compositor.check_composite_traps = check_composite_traps;
619 compositor.composite_traps = composite_traps;
620 //compositor.check_composite_tristrip = check_composite_traps;
621 compositor.composite_tristrip = composite_tristrip;
622 compositor.check_composite_glyphs = _cairo_gl_check_composite_glyphs;
623 compositor.composite_glyphs = _cairo_gl_composite_glyphs;
626 return &compositor.base;