From 9370f03eebed5d25a0aa3a2a67d48bdc03c5c2bf Mon Sep 17 00:00:00 2001
From: Henry (Yu) Song <hsong@sisa.samsung.com>
Date: Tue, 11 Oct 2011 09:08:38 -0700
Subject: [PATCH 1/3] gl/msaa: Prevent stroke overlap

When stroking we do not send our polygon to the tessellator, so
it may have overlapping stroke components. Use the stencil buffer
to prevent stroke components from overlapping.
---
 src/cairo-gl-msaa-compositor.c |   26 ++++++++++++++++++++++++++
 1 files changed, 26 insertions(+), 0 deletions(-)

diff --git a/src/cairo-gl-msaa-compositor.c b/src/cairo-gl-msaa-compositor.c
index 7a0e828..01ef40f 100644
--- a/src/cairo-gl-msaa-compositor.c
+++ b/src/cairo-gl-msaa-compositor.c
@@ -247,6 +247,27 @@ _stroke_shaper_add_quad (void			*closure,
 						      quad);
 }
 
+static void
+_prevent_overlapping_drawing (cairo_clip_t	*clip,
+			      cairo_bool_t	 used_stencil_buffer_for_clip)
+{
+   if (used_stencil_buffer_for_clip == FALSE) {
+	/* Enable the stencil buffer, even if we have no clip so that
+	   we can use it below to prevent overlapping shapes. We initialize
+	   it all to one here which represents infinite clip. */
+	glDepthMask (GL_TRUE);
+	glEnable (GL_STENCIL_TEST);
+	glClearStencil(1);
+	glClear (GL_STENCIL_BUFFER_BIT);
+	glStencilFunc (GL_EQUAL, 1, 1);
+    }
+
+    /* This means that once we draw to a particular pixel nothing else can
+       be drawn there until the stencil buffer is reset or the stencil test
+       is disabled. */
+    glStencilOp (GL_ZERO, GL_ZERO, GL_ZERO);
+}
+
 static cairo_int_status_t
 _cairo_gl_msaa_compositor_stroke (const cairo_compositor_t	*compositor,
 				  cairo_composite_rectangles_t	*composite,
@@ -261,6 +282,8 @@ _cairo_gl_msaa_compositor_stroke (const cairo_compositor_t	*compositor,
     cairo_gl_surface_t *dst = (cairo_gl_surface_t *) composite->surface;
     struct _tristrip_composite_info info;
 
+    cairo_bool_t used_stencil_buffer_for_clip = FALSE;
+
     if (antialias != CAIRO_ANTIALIAS_NONE)
 	return CAIRO_INT_STATUS_UNSUPPORTED;
 
@@ -296,11 +319,14 @@ _cairo_gl_msaa_compositor_stroke (const cairo_compositor_t	*compositor,
     if (! _cairo_composite_rectangles_can_reduce_clip (composite,
 						       composite->clip))
     {
+	used_stencil_buffer_for_clip = TRUE;
 	status = _draw_clip_to_stencil_buffer (info.ctx, &info.setup, composite->clip);
 	if (unlikely (status))
 	    goto finish;
     }
 
+    _prevent_overlapping_drawing (composite->clip, used_stencil_buffer_for_clip);
+
     status = _cairo_path_fixed_stroke_to_shaper ((cairo_path_fixed_t *) path,
 						 style,
 						 ctm,
-- 
1.7.4.1

