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-surface-backend-private.h"
52 #include "cairo-surface-offset-private.h"
53 #include "cairo-surface-snapshot-inline.h"
54 #include "cairo-surface-subsurface-inline.h"
55 #include "cairo-rtree-private.h"
57 static cairo_int_status_t
58 _cairo_gl_create_gradient_texture (cairo_gl_surface_t *dst,
59 const cairo_gradient_pattern_t *pattern,
60 cairo_gl_gradient_t **gradient)
62 cairo_gl_context_t *ctx;
63 cairo_status_t status;
65 status = _cairo_gl_context_acquire (dst->base.device, &ctx);
66 if (unlikely (status))
69 status = _cairo_gl_gradient_create (ctx, pattern->n_stops, pattern->stops, gradient);
71 return _cairo_gl_context_release (ctx, status);
74 static cairo_int_status_t
75 _resolve_multisampling (cairo_gl_surface_t *surface)
77 cairo_gl_context_t *ctx;
78 cairo_int_status_t status;
80 if (! surface->msaa_active)
81 return CAIRO_INT_STATUS_SUCCESS;
83 if (surface->base.device == NULL)
84 return CAIRO_INT_STATUS_SUCCESS;
86 /* GLES surfaces do not need explicit resolution. */
87 if (((cairo_gl_context_t *) surface->base.device)->gl_flavor == CAIRO_GL_FLAVOR_ES)
88 return CAIRO_INT_STATUS_SUCCESS;
90 if (! _cairo_gl_surface_is_texture (surface))
91 return CAIRO_INT_STATUS_SUCCESS;
93 status = _cairo_gl_context_acquire (surface->base.device, &ctx);
94 if (unlikely (status))
97 ctx->current_target = surface;
99 #if CAIRO_HAS_GL_SURFACE
100 _cairo_gl_activate_surface_as_nonmultisampling (ctx, surface);
103 status = _cairo_gl_context_release (ctx, status);
108 _cairo_gl_image_cache_lock (cairo_gl_context_t *ctx,
109 cairo_gl_image_t *image_node)
111 _cairo_rtree_pin (&ctx->image_cache.rtree, &image_node->node);
115 _cairo_gl_image_cache_unlock (cairo_gl_context_t *ctx)
117 if (ctx->image_cache.surface)
118 _cairo_rtree_unpin (&(ctx->image_cache.rtree));
121 static cairo_int_status_t
122 _cairo_gl_copy_texture (cairo_gl_surface_t *dst,
123 cairo_gl_surface_t *image,
125 int width, int height,
126 cairo_bool_t replace,
127 cairo_gl_context_t **ctx)
129 cairo_int_status_t status;
130 cairo_gl_context_t *ctx_out;
131 cairo_gl_dispatch_t *dispatch;
132 cairo_gl_surface_t *cache_surface;
133 cairo_gl_surface_t *target;
135 if (! _cairo_gl_surface_is_texture (image))
136 return CAIRO_INT_STATUS_UNSUPPORTED;
138 status = _cairo_gl_context_acquire (dst->base.device, &ctx_out);
139 if(unlikely (status))
142 if (! ctx_out->image_cache.surface) {
143 status = _cairo_gl_image_cache_init (ctx_out);
144 if (unlikely (status))
149 _cairo_gl_composite_flush (ctx_out);
151 /* Bind framebuffer of source image. */
152 dispatch = &ctx_out->dispatch;
153 cache_surface = ctx_out->image_cache.surface;
154 target = ctx_out->current_target;
156 _cairo_gl_ensure_framebuffer (ctx_out, image);
157 dispatch->BindFramebuffer (GL_FRAMEBUFFER, image->fb);
158 glBindTexture (ctx_out->tex_target, cache_surface->tex);
160 glCopyTexSubImage2D (ctx_out->tex_target, 0, x, y, 0, 0, width, height);
161 dispatch->BindFramebuffer (GL_FRAMEBUFFER, target->fb);
162 ctx_out->current_target = target;
165 return CAIRO_INT_STATUS_SUCCESS;
168 static cairo_int_status_t
169 _cairo_gl_image_cache_replace_image (cairo_gl_image_t *image_node,
170 cairo_gl_surface_t *dst,
171 cairo_gl_surface_t *image,
172 cairo_gl_context_t **ctx)
174 cairo_int_status_t status;
175 /* Paint image to cache. */
176 status = _cairo_gl_copy_texture (dst, image, image_node->node.x,
178 image->width, image->height,
181 image->content_changed = FALSE;
185 static cairo_int_status_t
186 _cairo_gl_image_cache_add_image (cairo_gl_context_t *ctx,
187 cairo_gl_surface_t *dst,
188 cairo_gl_surface_t *image,
189 cairo_gl_image_t **image_node)
191 cairo_int_status_t status;
192 cairo_rtree_node_t *node = NULL;
194 cairo_bool_t replaced = FALSE;
196 if (! image->base.device ||
197 (image->width > IMAGE_CACHE_MAX_SIZE ||
198 image->height > IMAGE_CACHE_MAX_SIZE))
199 return CAIRO_INT_STATUS_UNSUPPORTED;
201 width = image->width;
202 height = image->height;
205 (cairo_gl_image_t *) cairo_surface_get_user_data (&image->base,
206 (const cairo_user_data_key_t *) (&image->base));
208 if (image->content_changed) {
209 status = _cairo_gl_image_cache_replace_image (*image_node,
212 if (unlikely (status))
218 _cairo_gl_image_cache_lock (ctx, *image_node);
220 image->content_changed = FALSE;
222 return _cairo_gl_context_release (ctx, status);
223 return CAIRO_INT_STATUS_SUCCESS;
226 status = _cairo_rtree_insert (&ctx->image_cache.rtree, width,
228 /* Search for an unlocked slot. */
229 if (status == CAIRO_INT_STATUS_UNSUPPORTED) {
230 _cairo_gl_composite_flush (ctx);
231 _cairo_gl_image_cache_unlock (ctx);
233 status = _cairo_rtree_evict_random (&ctx->image_cache.rtree,
234 width, height, &node);
236 if (status == CAIRO_INT_STATUS_SUCCESS)
237 status = _cairo_rtree_node_insert (&ctx->image_cache.rtree,
238 node, width, height, &node);
244 /* Paint image to cache. */
245 status = _cairo_gl_copy_texture (dst, image, node->x, node->y,
246 image->width, image->height,
248 if (unlikely (status))
251 *image_node = (cairo_gl_image_t *)node;
252 (*image_node)->ctx = ctx;
253 (*image_node)->original_surface = &image->base;
255 (*image_node)->p1.x = node->x;
256 (*image_node)->p1.y = node->y;
257 (*image_node)->p2.x = node->x + image->width;
258 (*image_node)->p2.y = node->y + image->height;
259 if (! _cairo_gl_device_requires_power_of_two_textures (&ctx->base)) {
260 (*image_node)->p1.x /= IMAGE_CACHE_WIDTH;
261 (*image_node)->p2.x /= IMAGE_CACHE_WIDTH;
262 (*image_node)->p1.y /= IMAGE_CACHE_HEIGHT;
263 (*image_node)->p2.y /= IMAGE_CACHE_HEIGHT;
265 (*image_node)->user_data_removed = FALSE;
266 image->content_changed = FALSE;
268 status = cairo_surface_set_user_data (&image->base,
269 (const cairo_user_data_key_t *) &image->base,
270 (void *) *image_node,
271 _cairo_gl_image_node_fini);
273 _cairo_gl_image_cache_lock (ctx, *image_node);
274 return _cairo_gl_context_release (ctx, status);
277 static cairo_status_t
278 _cairo_gl_subsurface_clone_operand_init (cairo_gl_operand_t *operand,
279 const cairo_pattern_t *_src,
280 cairo_gl_surface_t *dst,
281 const cairo_rectangle_int_t *sample,
282 const cairo_rectangle_int_t *extents)
284 const cairo_surface_pattern_t *src = (cairo_surface_pattern_t *)_src;
285 cairo_surface_pattern_t local_pattern;
286 cairo_surface_subsurface_t *sub;
287 cairo_gl_surface_t *surface;
288 cairo_gl_context_t *ctx;
289 cairo_surface_attributes_t *attributes;
290 cairo_status_t status;
292 sub = (cairo_surface_subsurface_t *) src->surface;
295 sub->snapshot->type == CAIRO_SURFACE_TYPE_GL &&
296 sub->snapshot->device == dst->base.device)
298 surface = (cairo_gl_surface_t *)
299 cairo_surface_reference (sub->snapshot);
303 status = _cairo_gl_context_acquire (dst->base.device, &ctx);
304 if (unlikely (status))
307 /* XXX Trim surface to the sample area within the subsurface? */
308 surface = (cairo_gl_surface_t *)
309 _cairo_gl_surface_create_scratch (ctx,
310 sub->target->content,
315 if (surface->base.status)
316 return _cairo_gl_context_release (ctx, surface->base.status);
318 _cairo_pattern_init_for_surface (&local_pattern, sub->target);
319 cairo_matrix_init_translate (&local_pattern.base.matrix,
320 sub->extents.x, sub->extents.y);
321 local_pattern.base.filter = CAIRO_FILTER_NEAREST;
322 status = _cairo_surface_paint (&surface->base,
323 CAIRO_OPERATOR_SOURCE,
326 _cairo_pattern_fini (&local_pattern.base);
328 status = _cairo_gl_context_release (ctx, status);
329 if (unlikely (status)) {
330 cairo_surface_destroy (&surface->base);
334 _cairo_surface_subsurface_set_snapshot (&sub->base, &surface->base);
337 status = _resolve_multisampling (surface);
338 if (unlikely (status))
341 attributes = &operand->texture.attributes;
343 operand->type = CAIRO_GL_OPERAND_TEXTURE;
344 operand->texture.surface = surface;
345 operand->texture.owns_surface = surface;
346 operand->texture.tex = surface->tex;
347 operand->texture.use_atlas = FALSE;
349 if (_cairo_gl_device_requires_power_of_two_textures (dst->base.device)) {
350 attributes->matrix = src->base.matrix;
354 cairo_matrix_init_scale (&m,
355 1.0 / surface->width,
356 1.0 / surface->height);
357 cairo_matrix_multiply (&attributes->matrix, &src->base.matrix, &m);
360 attributes->extend = src->base.extend;
361 attributes->filter = src->base.filter;
362 attributes->has_component_alpha = src->base.has_component_alpha;
363 return CAIRO_STATUS_SUCCESS;
366 static cairo_status_t
367 _cairo_gl_subsurface_operand_init (cairo_gl_operand_t *operand,
368 const cairo_pattern_t *_src,
369 cairo_gl_surface_t *dst,
370 const cairo_rectangle_int_t *sample,
371 const cairo_rectangle_int_t *extents)
373 const cairo_surface_pattern_t *src = (cairo_surface_pattern_t *)_src;
374 cairo_surface_subsurface_t *sub;
375 cairo_gl_surface_t *surface;
376 cairo_surface_attributes_t *attributes;
377 cairo_int_status_t status;
378 cairo_gl_image_t *image_node = NULL;
379 cairo_gl_context_t *ctx = (cairo_gl_context_t *)dst->base.device;
381 sub = (cairo_surface_subsurface_t *) src->surface;
383 if (sample->x < 0 || sample->y < 0 ||
384 sample->x + sample->width > sub->extents.width ||
385 sample->y + sample->height > sub->extents.height)
387 return _cairo_gl_subsurface_clone_operand_init (operand, _src,
388 dst, sample, extents);
391 surface = (cairo_gl_surface_t *) sub->target;
392 if (surface->base.device &&
393 (surface->base.device != dst->base.device ||
394 (! surface->tex && ! surface->bounded_tex)))
395 return CAIRO_INT_STATUS_UNSUPPORTED;
397 if (! _cairo_gl_surface_is_texture (surface))
398 return CAIRO_INT_STATUS_UNSUPPORTED;
400 status = _resolve_multisampling (surface);
401 if (unlikely (status))
404 _cairo_gl_operand_copy(operand, &surface->operand);
405 *operand = surface->operand;
406 operand->texture.use_atlas = FALSE;
408 attributes = &operand->texture.attributes;
409 attributes->extend = src->base.extend;
410 attributes->filter = src->base.filter;
411 attributes->has_component_alpha = src->base.has_component_alpha;
413 attributes->matrix = src->base.matrix;
414 attributes->matrix.x0 += sub->extents.x;
415 attributes->matrix.y0 += sub->extents.y;
417 if (surface->needs_to_cache)
418 status = _cairo_gl_image_cache_add_image (ctx, dst, surface,
421 /* Translate the matrix from
422 * (unnormalized src -> unnormalized src) to
423 * (unnormalized dst -> unnormalized src)
426 if (unlikely (status) || ! image_node)
427 cairo_matrix_multiply (&attributes->matrix,
429 &surface->operand.texture.attributes.matrix);
431 cairo_matrix_t matrix = src->base.matrix;
432 operand->texture.surface = ctx->image_cache.surface;
433 operand->texture.owns_surface = NULL;
434 operand->texture.tex = ctx->image_cache.surface->tex;
435 attributes->extend = CAIRO_EXTEND_NONE;
436 operand->texture.extend = src->base.extend;
437 attributes->matrix.x0 = image_node->node.x + sub->extents.x;
438 attributes->matrix.y0 = image_node->node.y + sub->extents.y;
439 operand->texture.use_atlas = TRUE;
441 operand->texture.p1.x = image_node->p1.x;
442 operand->texture.p1.y = image_node->p1.y;
443 operand->texture.p2.x = image_node->p2.x;
444 operand->texture.p2.y = image_node->p2.y;
445 if (src->base.extend == CAIRO_EXTEND_PAD) {
446 operand->texture.p1.x += 0.5 / IMAGE_CACHE_WIDTH;
447 operand->texture.p1.y += 0.5 / IMAGE_CACHE_HEIGHT;
448 operand->texture.p2.x -= 0.5 / IMAGE_CACHE_WIDTH;
449 operand->texture.p2.y -= 0.5 / IMAGE_CACHE_HEIGHT;
452 cairo_matrix_multiply (&attributes->matrix,
454 &ctx->image_cache.surface->operand.texture.attributes.matrix);
457 return CAIRO_STATUS_SUCCESS;
460 static cairo_status_t
461 _cairo_gl_surface_operand_init (cairo_gl_operand_t *operand,
462 const cairo_pattern_t *_src,
463 cairo_gl_surface_t *dst,
464 const cairo_rectangle_int_t *sample,
465 const cairo_rectangle_int_t *extents)
467 const cairo_surface_pattern_t *src = (cairo_surface_pattern_t *)_src;
468 cairo_gl_surface_t *surface;
469 cairo_surface_attributes_t *attributes;
470 cairo_int_status_t status;
471 cairo_gl_image_t *image_node = NULL;
472 cairo_gl_context_t *ctx = (cairo_gl_context_t *)dst->base.device;
474 surface = (cairo_gl_surface_t *) src->surface;
475 if (surface->base.type != CAIRO_SURFACE_TYPE_GL)
476 return CAIRO_INT_STATUS_UNSUPPORTED;
478 if (surface->base.backend->type != CAIRO_SURFACE_TYPE_GL) {
479 if (_cairo_surface_is_subsurface (&surface->base))
480 return _cairo_gl_subsurface_operand_init (operand, _src, dst,
482 else if (_cairo_surface_is_snapshot (src->surface)) {
483 cairo_surface_snapshot_t *surface_snapshot;
484 cairo_pattern_t *sub_pattern;
486 surface_snapshot = (cairo_surface_snapshot_t *)src->surface;
487 surface = (cairo_gl_surface_t *)surface_snapshot->target;
488 if (surface->base.type != CAIRO_SURFACE_TYPE_GL)
489 return CAIRO_INT_STATUS_UNSUPPORTED;
491 if (_cairo_surface_is_subsurface (&surface->base)) {
492 sub_pattern = cairo_pattern_create_for_surface (&surface->base);
493 status = _cairo_gl_subsurface_operand_init (operand,
498 cairo_pattern_destroy (sub_pattern);
503 return CAIRO_INT_STATUS_UNSUPPORTED;
506 if (surface->base.device &&
507 (surface->base.device != dst->base.device ||
508 (! surface->tex && ! surface->bounded_tex)))
509 return CAIRO_INT_STATUS_UNSUPPORTED;
511 status = _resolve_multisampling (surface);
512 if (unlikely (status))
515 _cairo_gl_operand_copy(operand, &surface->operand);
516 operand->texture.use_atlas = FALSE;
518 attributes = &operand->texture.attributes;
519 attributes->extend = src->base.extend;
520 attributes->filter = src->base.filter;
521 attributes->has_component_alpha = src->base.has_component_alpha;
523 if (surface->needs_to_cache)
524 status = _cairo_gl_image_cache_add_image (ctx, dst, surface,
527 if (unlikely (status) || ! image_node)
528 cairo_matrix_multiply (&attributes->matrix,
530 &attributes->matrix);
532 cairo_matrix_t matrix = src->base.matrix;
533 operand->texture.use_atlas = TRUE;
534 attributes->extend = CAIRO_EXTEND_NONE;
535 operand->texture.extend = src->base.extend;
537 operand->texture.p1.x = image_node->p1.x;
538 operand->texture.p1.y = image_node->p1.y;
539 operand->texture.p2.x = image_node->p2.x;
540 operand->texture.p2.y = image_node->p2.y;
541 if (src->base.extend == CAIRO_EXTEND_PAD) {
542 operand->texture.p1.x += 0.5 / IMAGE_CACHE_WIDTH;
543 operand->texture.p1.y += 0.5 / IMAGE_CACHE_HEIGHT;
544 operand->texture.p2.x -= 0.5 / IMAGE_CACHE_WIDTH;
545 operand->texture.p2.y -= 0.5 / IMAGE_CACHE_HEIGHT;
548 operand->texture.surface = ctx->image_cache.surface;
549 operand->texture.owns_surface = NULL;
550 operand->texture.tex = ctx->image_cache.surface->tex;
551 matrix.x0 += image_node->node.x;
552 matrix.y0 += image_node->node.y;
553 cairo_matrix_multiply (&attributes->matrix,
555 &ctx->image_cache.surface->operand.texture.attributes.matrix);
558 return CAIRO_STATUS_SUCCESS;
561 static cairo_status_t
562 _cairo_gl_pattern_texture_setup (cairo_gl_operand_t *operand,
563 const cairo_pattern_t *_src,
564 cairo_gl_surface_t *dst,
565 const cairo_rectangle_int_t *extents)
567 cairo_status_t status;
568 cairo_gl_surface_t *surface;
569 cairo_gl_context_t *ctx;
570 cairo_image_surface_t *image;
571 cairo_bool_t src_is_gl_surface = FALSE;
572 pixman_format_code_t pixman_format;
574 if (_src->type == CAIRO_PATTERN_TYPE_SURFACE) {
575 cairo_surface_t* src_surface = ((cairo_surface_pattern_t *) _src)->surface;
576 src_is_gl_surface = src_surface->type == CAIRO_SURFACE_TYPE_GL;
579 status = _cairo_gl_context_acquire (dst->base.device, &ctx);
580 if (unlikely (status))
583 surface = (cairo_gl_surface_t *)
584 _cairo_gl_surface_create_scratch (ctx,
585 CAIRO_CONTENT_COLOR_ALPHA,
586 extents->width, extents->height,
589 /* XXX: This is a hack for driver that does not support PBO, we
590 don't need an extra step of downloading newly created texture
591 to image, we can create image directly. */
592 if (! _cairo_is_little_endian ())
593 pixman_format = PIXMAN_r8g8b8a8;
595 pixman_format = PIXMAN_a8b8g8r8;
596 image = (cairo_image_surface_t*)
597 _cairo_image_surface_create_with_pixman_format (NULL,
602 if (unlikely (image->base.status)) {
603 status = _cairo_gl_context_release (ctx, status);
605 /* The error status in the image is issue that caused the problem. */
606 status = image->base.status;
608 cairo_surface_destroy (&image->base);
612 /* If the pattern is a GL surface, it belongs to some other GL context,
613 so we need to release this device while we paint it to the image. */
614 if (src_is_gl_surface) {
615 status = _cairo_gl_context_release (ctx, status);
616 if (unlikely (status))
620 status = _cairo_surface_offset_paint (&image->base, extents->x, extents->y,
621 CAIRO_OPERATOR_SOURCE, _src, NULL);
623 if (src_is_gl_surface) {
624 status = _cairo_gl_context_acquire (dst->base.device, &ctx);
625 if (unlikely (status))
629 status = _cairo_surface_unmap_image (&surface->base, image);
630 status = _cairo_gl_context_release (ctx, status);
631 if (unlikely (status))
634 *operand = surface->operand;
635 operand->texture.owns_surface = surface;
636 operand->texture.attributes.matrix.x0 -= extents->x * operand->texture.attributes.matrix.xx;
637 operand->texture.attributes.matrix.y0 -= extents->y * operand->texture.attributes.matrix.yy;
638 dst->needs_to_cache = TRUE;
639 operand->texture.use_atlas = FALSE;
641 return CAIRO_STATUS_SUCCESS;
644 cairo_surface_destroy (&surface->base);
649 _cairo_gl_solid_operand_init (cairo_gl_operand_t *operand,
650 const cairo_color_t *color)
652 operand->type = CAIRO_GL_OPERAND_CONSTANT;
653 operand->constant.color[0] = color->red * color->alpha;
654 operand->constant.color[1] = color->green * color->alpha;
655 operand->constant.color[2] = color->blue * color->alpha;
656 operand->constant.color[3] = color->alpha;
660 _cairo_gl_operand_translate (cairo_gl_operand_t *operand,
661 double tx, double ty)
663 switch (operand->type) {
664 case CAIRO_GL_OPERAND_TEXTURE:
665 operand->texture.attributes.matrix.x0 -= tx * operand->texture.attributes.matrix.xx;
666 operand->texture.attributes.matrix.y0 -= ty * operand->texture.attributes.matrix.yy;
669 case CAIRO_GL_OPERAND_LINEAR_GRADIENT:
670 case CAIRO_GL_OPERAND_RADIAL_GRADIENT_A0:
671 case CAIRO_GL_OPERAND_RADIAL_GRADIENT_NONE:
672 case CAIRO_GL_OPERAND_RADIAL_GRADIENT_EXT:
673 operand->gradient.m.x0 -= tx * operand->gradient.m.xx;
674 operand->gradient.m.y0 -= ty * operand->gradient.m.yy;
677 case CAIRO_GL_OPERAND_NONE:
678 case CAIRO_GL_OPERAND_CONSTANT:
679 case CAIRO_GL_OPERAND_COUNT:
685 static cairo_status_t
686 _cairo_gl_gradient_operand_init (cairo_gl_operand_t *operand,
687 const cairo_pattern_t *pattern,
688 cairo_gl_surface_t *dst)
690 const cairo_gradient_pattern_t *gradient = (const cairo_gradient_pattern_t *)pattern;
691 cairo_status_t status;
693 assert (gradient->base.type == CAIRO_PATTERN_TYPE_LINEAR ||
694 gradient->base.type == CAIRO_PATTERN_TYPE_RADIAL);
696 if (! _cairo_gl_device_has_glsl (dst->base.device))
697 return CAIRO_INT_STATUS_UNSUPPORTED;
699 status = _cairo_gl_create_gradient_texture (dst,
701 &operand->gradient.gradient);
702 if (unlikely (status))
705 if (gradient->base.type == CAIRO_PATTERN_TYPE_LINEAR) {
706 cairo_linear_pattern_t *linear = (cairo_linear_pattern_t *) gradient;
707 double x0, y0, dx, dy, sf, offset;
709 dx = linear->pd2.x - linear->pd1.x;
710 dy = linear->pd2.y - linear->pd1.y;
711 sf = 1.0 / (dx * dx + dy * dy);
717 offset = dx * x0 + dy * y0;
719 operand->type = CAIRO_GL_OPERAND_LINEAR_GRADIENT;
721 cairo_matrix_init (&operand->gradient.m, dx, 0, dy, 1, -offset, 0);
722 if (! _cairo_matrix_is_identity (&pattern->matrix)) {
723 cairo_matrix_multiply (&operand->gradient.m,
725 &operand->gradient.m);
729 cairo_circle_double_t circles[2];
730 double x0, y0, r0, dx, dy, dr;
732 cairo_radial_pattern_t *radial_pattern = (cairo_radial_pattern_t *)gradient;
735 * Some fragment shader implementations use half-floats to
736 * represent numbers, so the maximum number they can represent
737 * is about 2^14. Some intermediate computations used in the
738 * radial gradient shaders can produce results of up to 2*k^4.
739 * Setting k=8 makes the maximum result about 8192 (assuming
740 * that the extreme circles are not much smaller than the
741 * destination image).
743 _cairo_gradient_pattern_fit_to_range (gradient, 8.,
744 &operand->gradient.m, circles);
747 * Instead of using scaled data that might introducing rounding
748 * errors, we use original data directly
750 if (circles[0].center.x)
751 scale = radial_pattern->cd1.center.x / circles[0].center.x;
752 else if (circles[0].center.y)
753 scale = radial_pattern->cd1.center.y / circles[0].center.y;
754 else if (circles[0].radius)
755 scale = radial_pattern->cd1.radius / circles[0].radius;
756 else if (circles[1].center.x)
757 scale = radial_pattern->cd2.center.x / circles[1].center.x;
758 else if (circles[1].center.y)
759 scale = radial_pattern->cd2.center.y / circles[1].center.y;
760 else if (circles[1].radius)
761 scale = radial_pattern->cd2.radius / circles[1].radius;
763 x0 = circles[0].center.x;
764 y0 = circles[0].center.y;
765 r0 = circles[0].radius;
766 dx = radial_pattern->cd2.center.x - radial_pattern->cd1.center.x;
767 dy = radial_pattern->cd2.center.y - radial_pattern->cd1.center.y;
768 dr = radial_pattern->cd2.radius - radial_pattern->cd1.radius;
770 operand->gradient.a = (dx * dx + dy * dy - dr * dr)/(scale * scale);
771 operand->gradient.radius_0 = r0;
772 operand->gradient.circle_d.center.x = dx / scale;
773 operand->gradient.circle_d.center.y = dy / scale;
774 operand->gradient.circle_d.radius = dr / scale;
776 if (operand->gradient.a == 0)
777 operand->type = CAIRO_GL_OPERAND_RADIAL_GRADIENT_A0;
778 else if (pattern->extend == CAIRO_EXTEND_NONE)
779 operand->type = CAIRO_GL_OPERAND_RADIAL_GRADIENT_NONE;
781 operand->type = CAIRO_GL_OPERAND_RADIAL_GRADIENT_EXT;
783 cairo_matrix_init_translate (&m, -x0, -y0);
784 cairo_matrix_multiply (&operand->gradient.m,
785 &operand->gradient.m,
789 operand->gradient.extend = pattern->extend;
791 return CAIRO_STATUS_SUCCESS;
795 _cairo_gl_operand_copy (cairo_gl_operand_t *dst,
796 const cairo_gl_operand_t *src)
800 case CAIRO_GL_OPERAND_CONSTANT:
802 case CAIRO_GL_OPERAND_LINEAR_GRADIENT:
803 case CAIRO_GL_OPERAND_RADIAL_GRADIENT_A0:
804 case CAIRO_GL_OPERAND_RADIAL_GRADIENT_NONE:
805 case CAIRO_GL_OPERAND_RADIAL_GRADIENT_EXT:
806 _cairo_gl_gradient_reference (dst->gradient.gradient);
808 case CAIRO_GL_OPERAND_TEXTURE:
809 cairo_surface_reference (&dst->texture.owns_surface->base);
812 case CAIRO_GL_OPERAND_COUNT:
814 case CAIRO_GL_OPERAND_NONE:
820 _cairo_gl_operand_destroy (cairo_gl_operand_t *operand)
822 switch (operand->type) {
823 case CAIRO_GL_OPERAND_CONSTANT:
825 case CAIRO_GL_OPERAND_LINEAR_GRADIENT:
826 case CAIRO_GL_OPERAND_RADIAL_GRADIENT_A0:
827 case CAIRO_GL_OPERAND_RADIAL_GRADIENT_NONE:
828 case CAIRO_GL_OPERAND_RADIAL_GRADIENT_EXT:
829 _cairo_gl_gradient_destroy (operand->gradient.gradient);
831 case CAIRO_GL_OPERAND_TEXTURE:
832 cairo_surface_destroy (&operand->texture.owns_surface->base);
835 case CAIRO_GL_OPERAND_COUNT:
837 case CAIRO_GL_OPERAND_NONE:
841 operand->type = CAIRO_GL_OPERAND_NONE;
845 _cairo_gl_operand_init (cairo_gl_operand_t *operand,
846 const cairo_pattern_t *pattern,
847 cairo_gl_surface_t *dst,
848 const cairo_rectangle_int_t *sample,
849 const cairo_rectangle_int_t *extents,
850 cairo_bool_t use_color_attribute)
852 cairo_int_status_t status;
854 TRACE ((stderr, "%s: type=%d\n", __FUNCTION__, pattern->type));
855 switch (pattern->type) {
856 case CAIRO_PATTERN_TYPE_SOLID:
857 _cairo_gl_solid_operand_init (operand,
858 &((cairo_solid_pattern_t *) pattern)->color);
859 operand->use_color_attribute = use_color_attribute;
860 return CAIRO_STATUS_SUCCESS;
861 case CAIRO_PATTERN_TYPE_SURFACE:
862 status = _cairo_gl_surface_operand_init (operand, pattern, dst,
864 if (status == CAIRO_INT_STATUS_UNSUPPORTED)
869 case CAIRO_PATTERN_TYPE_LINEAR:
870 case CAIRO_PATTERN_TYPE_RADIAL:
871 status = _cairo_gl_gradient_operand_init (operand, pattern, dst);
872 if (status == CAIRO_INT_STATUS_UNSUPPORTED)
878 case CAIRO_PATTERN_TYPE_MESH:
879 case CAIRO_PATTERN_TYPE_RASTER_SOURCE:
883 return _cairo_gl_pattern_texture_setup (operand, pattern, dst, extents);
887 _cairo_gl_operand_get_filter (cairo_gl_operand_t *operand)
889 cairo_filter_t filter;
891 switch ((int) operand->type) {
892 case CAIRO_GL_OPERAND_TEXTURE:
893 filter = operand->texture.attributes.filter;
895 case CAIRO_GL_OPERAND_LINEAR_GRADIENT:
896 case CAIRO_GL_OPERAND_RADIAL_GRADIENT_A0:
897 case CAIRO_GL_OPERAND_RADIAL_GRADIENT_NONE:
898 case CAIRO_GL_OPERAND_RADIAL_GRADIENT_EXT:
899 filter = CAIRO_FILTER_BILINEAR;
902 filter = CAIRO_FILTER_DEFAULT;
910 _cairo_gl_operand_get_gl_filter (cairo_gl_operand_t *operand)
912 cairo_filter_t filter = _cairo_gl_operand_get_filter (operand);
914 return filter != CAIRO_FILTER_FAST && filter != CAIRO_FILTER_NEAREST ?
920 _cairo_gl_operand_get_use_atlas (cairo_gl_operand_t *operand)
922 if (operand->type != CAIRO_GL_OPERAND_TEXTURE)
925 return operand->texture.use_atlas;
929 _cairo_gl_operand_get_extend (cairo_gl_operand_t *operand)
931 cairo_extend_t extend;
933 switch ((int) operand->type) {
934 case CAIRO_GL_OPERAND_TEXTURE:
935 if (! operand->texture.use_atlas)
936 extend = operand->texture.attributes.extend;
938 extend = operand->texture.extend;
940 case CAIRO_GL_OPERAND_LINEAR_GRADIENT:
941 case CAIRO_GL_OPERAND_RADIAL_GRADIENT_A0:
942 case CAIRO_GL_OPERAND_RADIAL_GRADIENT_NONE:
943 case CAIRO_GL_OPERAND_RADIAL_GRADIENT_EXT:
944 extend = operand->gradient.extend;
947 extend = CAIRO_EXTEND_NONE;
955 _cairo_gl_operand_get_atlas_extend (cairo_gl_operand_t *operand)
957 cairo_extend_t extend;
959 switch ((int) operand->type) {
960 case CAIRO_GL_OPERAND_TEXTURE:
961 if (operand->texture.use_atlas)
962 extend = operand->texture.extend;
964 extend = CAIRO_EXTEND_NONE;
967 extend = CAIRO_EXTEND_NONE;
975 _cairo_gl_operand_bind_to_shader (cairo_gl_context_t *ctx,
976 cairo_gl_operand_t *operand,
977 cairo_gl_tex_t tex_unit)
979 char uniform_name[50];
981 static const char *names[] = { "source", "mask" };
983 strcpy (uniform_name, names[tex_unit]);
984 custom_part = uniform_name + strlen (names[tex_unit]);
986 switch (operand->type) {
988 case CAIRO_GL_OPERAND_COUNT:
990 case CAIRO_GL_OPERAND_NONE:
992 case CAIRO_GL_OPERAND_CONSTANT:
993 if (!operand->use_color_attribute) {
994 strcpy (custom_part, "_constant");
995 _cairo_gl_shader_bind_vec4 (ctx,
997 operand->constant.color[0],
998 operand->constant.color[1],
999 operand->constant.color[2],
1000 operand->constant.color[3]);
1003 case CAIRO_GL_OPERAND_RADIAL_GRADIENT_NONE:
1004 case CAIRO_GL_OPERAND_RADIAL_GRADIENT_EXT:
1005 strcpy (custom_part, "_a");
1006 _cairo_gl_shader_bind_float (ctx,
1008 operand->gradient.a);
1010 case CAIRO_GL_OPERAND_RADIAL_GRADIENT_A0:
1011 strcpy (custom_part, "_circle_d");
1012 _cairo_gl_shader_bind_vec3 (ctx,
1014 operand->gradient.circle_d.center.x,
1015 operand->gradient.circle_d.center.y,
1016 operand->gradient.circle_d.radius);
1017 strcpy (custom_part, "_radius_0");
1018 _cairo_gl_shader_bind_float (ctx,
1020 operand->gradient.radius_0);
1022 case CAIRO_GL_OPERAND_LINEAR_GRADIENT:
1023 case CAIRO_GL_OPERAND_TEXTURE:
1025 * For GLES2 we use shaders to implement GL_CLAMP_TO_BORDER (used
1026 * with CAIRO_EXTEND_NONE). When bilinear filtering is enabled,
1027 * these shaders need the texture dimensions for their calculations.
1029 if (ctx->gl_flavor == CAIRO_GL_FLAVOR_ES &&
1030 _cairo_gl_operand_get_extend (operand) == CAIRO_EXTEND_NONE &&
1031 _cairo_gl_operand_get_gl_filter (operand) == GL_LINEAR)
1033 float width, height;
1034 if (operand->type == CAIRO_GL_OPERAND_TEXTURE) {
1035 width = operand->texture.surface->width;
1036 height = operand->texture.surface->height;
1039 width = operand->gradient.gradient->tex_width,
1042 strcpy (custom_part, "_texdims");
1043 _cairo_gl_shader_bind_vec2 (ctx, uniform_name, width, height);
1051 _cairo_gl_operand_needs_setup (cairo_gl_operand_t *dest,
1052 cairo_gl_operand_t *source,
1053 unsigned int vertex_offset,
1054 cairo_bool_t *needs_flush)
1056 if (dest->type != source->type)
1058 if (dest->vertex_offset != vertex_offset)
1061 switch (source->type) {
1062 case CAIRO_GL_OPERAND_NONE:
1063 case CAIRO_GL_OPERAND_CONSTANT:
1064 if (source->use_color_attribute) {
1065 *needs_flush = FALSE;
1068 return dest->constant.color[0] != source->constant.color[0] ||
1069 dest->constant.color[1] != source->constant.color[1] ||
1070 dest->constant.color[2] != source->constant.color[2] ||
1071 dest->constant.color[3] != source->constant.color[3];
1073 case CAIRO_GL_OPERAND_TEXTURE:
1074 return dest->texture.surface != source->texture.surface ||
1075 dest->texture.attributes.extend != source->texture.attributes.extend ||
1076 dest->texture.attributes.filter != source->texture.attributes.filter ||
1077 dest->texture.attributes.has_component_alpha != source->texture.attributes.has_component_alpha;
1078 case CAIRO_GL_OPERAND_LINEAR_GRADIENT:
1079 case CAIRO_GL_OPERAND_RADIAL_GRADIENT_A0:
1080 case CAIRO_GL_OPERAND_RADIAL_GRADIENT_NONE:
1081 case CAIRO_GL_OPERAND_RADIAL_GRADIENT_EXT:
1082 /* XXX: improve this */
1085 case CAIRO_GL_OPERAND_COUNT:
1093 _cairo_gl_operand_get_vertex_size (cairo_gl_operand_t *operand)
1095 switch (operand->type) {
1097 case CAIRO_GL_OPERAND_COUNT:
1099 case CAIRO_GL_OPERAND_NONE:
1101 case CAIRO_GL_OPERAND_CONSTANT:
1102 if (operand->use_color_attribute)
1103 return 4 * sizeof (GLubyte);
1106 case CAIRO_GL_OPERAND_TEXTURE:
1107 if (operand->texture.use_atlas)
1108 return 6 * sizeof (GLfloat);
1110 return 2 * sizeof (GLfloat);
1111 case CAIRO_GL_OPERAND_LINEAR_GRADIENT:
1112 case CAIRO_GL_OPERAND_RADIAL_GRADIENT_A0:
1113 case CAIRO_GL_OPERAND_RADIAL_GRADIENT_NONE:
1114 case CAIRO_GL_OPERAND_RADIAL_GRADIENT_EXT:
1115 return 2 * sizeof (GLfloat);
1119 static inline cairo_int_status_t
1120 _cairo_gl_context_get_image_cache (cairo_gl_context_t *ctx,
1121 cairo_gl_image_cache_t **cache_out)
1123 if (! ctx->image_cache.surface)
1124 return CAIRO_INT_STATUS_UNSUPPORTED;
1126 *cache_out = &(ctx->image_cache);
1127 return CAIRO_INT_STATUS_SUCCESS;
1130 /* Called from _cairo_rtree_node_remove. */
1132 _cairo_gl_image_node_destroy (cairo_rtree_node_t *node)
1134 cairo_surface_t *surface;
1136 cairo_gl_image_t *image_node = cairo_container_of (node,
1140 surface = image_node->original_surface;
1141 image_node->node_removed = TRUE;
1142 /* Remove from original surface. */
1143 if (image_node->original_surface &&
1144 ! image_node->user_data_removed) {
1145 cairo_surface_set_user_data (image_node->original_surface,
1146 (const cairo_user_data_key_t *) surface,
1147 (void *) NULL, NULL);
1152 _cairo_gl_image_node_fini (void *data)
1154 cairo_gl_image_t *image_node = (cairo_gl_image_t *)data;
1156 image_node->user_data_removed = TRUE;
1158 if (! image_node->node_removed && ! image_node->node.pinned)
1159 _cairo_rtree_node_remove (&image_node->ctx->image_cache.rtree,