[cairo-commit] src/cairo-gl-composite.c src/cairo-gl-device.c src/cairo-gl-private.h

Martin Robinson mrobinson at kemper.freedesktop.org
Wed May 23 12:54:07 PDT 2012


 src/cairo-gl-composite.c |   67 +++++++++++++++++++++++------------------------
 src/cairo-gl-device.c    |    1 
 src/cairo-gl-private.h   |    7 ++++
 3 files changed, 41 insertions(+), 34 deletions(-)

New commits:
commit bf9c295953fbe86434a8beefa6994c7a07e723fc
Author: Martin Robinson <mrobinson at igalia.com>
Date:   Wed May 23 11:46:25 2012 -0700

    gl: Simplify switching between primitive types
    
    Simplify the code that switches between primitive types by adding
    a new member to the context which tracks the currently active
    primitive type.

diff --git a/src/cairo-gl-composite.c b/src/cairo-gl-composite.c
index bcf41ec..81a2c5f 100644
--- a/src/cairo-gl-composite.c
+++ b/src/cairo-gl-composite.c
@@ -707,7 +707,6 @@ _cairo_gl_composite_draw_tristrip (cairo_gl_context_t *ctx)
     cairo_array_t* indices = &ctx->tristrip_indices;
     const unsigned short *indices_array = _cairo_array_index_const (indices, 0);
 
-
     if (ctx->pre_shader) {
 	cairo_gl_shader_t *prev_shader = ctx->current_shader;
 
@@ -724,8 +723,8 @@ _cairo_gl_composite_draw_tristrip (cairo_gl_context_t *ctx)
 }
 
 static inline void
-_cairo_gl_composite_draw (cairo_gl_context_t *ctx,
-			  unsigned int count)
+_cairo_gl_composite_draw_triangles (cairo_gl_context_t *ctx,
+				    unsigned int count)
 {
     if (! ctx->pre_shader) {
         glDrawArrays (GL_TRIANGLES, 0, count);
@@ -743,6 +742,27 @@ _cairo_gl_composite_draw (cairo_gl_context_t *ctx,
 }
 
 static void
+_cairo_gl_composite_draw_triangles_with_clip_region (cairo_gl_context_t *ctx,
+						     unsigned int count)
+{
+    int i, num_rectangles;
+
+    if (!ctx->clip_region)
+	_cairo_gl_composite_draw_triangles (ctx, count)
+	return;
+    }
+
+    num_rectangles = cairo_region_num_rectangles (ctx->clip_region);
+    for (i = 0; i < num_rectangles; i++) {
+	cairo_rectangle_int_t rect;
+	cairo_region_get_rectangle (ctx->clip_region, i, &rect);
+
+	glScissor (rect.x, rect.y, rect.width, rect.height);
+	_cairo_gl_composite_draw_triangles (ctx, count);
+    }
+}
+
+static void
 _cairo_gl_composite_unmap_vertex_buffer (cairo_gl_context_t *ctx)
 {
     ctx->vb_offset = 0;
@@ -760,46 +780,25 @@ _cairo_gl_composite_flush (cairo_gl_context_t *ctx)
     count = ctx->vb_offset / ctx->vertex_size;
     _cairo_gl_composite_unmap_vertex_buffer (ctx);
 
-    if ( _cairo_array_num_elements (&ctx->tristrip_indices) > 0) {
+    if (ctx->primitive_type == CAIRO_GL_PRIMITIVE_TYPE_TRISTRIPS) {
 	_cairo_gl_composite_draw_tristrip (ctx);
-    } else if (ctx->clip_region) {
-	int i, num_rectangles = cairo_region_num_rectangles (ctx->clip_region);
-
-	for (i = 0; i < num_rectangles; i++) {
-	    cairo_rectangle_int_t rect;
-
-	    cairo_region_get_rectangle (ctx->clip_region, i, &rect);
-
-	    glScissor (rect.x, rect.y, rect.width, rect.height);
-            _cairo_gl_composite_draw (ctx, count);
-	}
     } else {
-        _cairo_gl_composite_draw (ctx, count);
+	assert (ctx->primitive_type == CAIRO_GL_PRIMITIVE_TYPE_TRIANGLES);
+	_cairo_gl_composite_draw_triangles_with_clip_region (ctx, count);
     }
 
     for (i = 0; i < ARRAY_LENGTH (&ctx->glyph_cache); i++)
 	_cairo_gl_glyph_cache_unlock (&ctx->glyph_cache[i]);
 }
 
-typedef enum cairo_gl_geometry {
-    CAIRO_GL_GEOMETRY_TYPE_TRIANGLES,
-    CAIRO_GL_GEOMETRY_TYPE_TRISTRIPS
-} cairo_gl_geometry_t;
-
 static void
 _cairo_gl_composite_prepare_buffer (cairo_gl_context_t *ctx,
 				    unsigned int n_vertices,
-				    cairo_gl_geometry_t geometry_type)
+				    cairo_gl_primitive_type_t primitive_type)
 {
-    cairo_gl_dispatch_t *dispatch = &ctx->dispatch;
-
-    size_t tristrip_indices =_cairo_array_num_elements (&ctx->tristrip_indices);
-    if (geometry_type == CAIRO_GL_GEOMETRY_TYPE_TRIANGLES &&
-	tristrip_indices != 0) {
-	_cairo_gl_composite_flush (ctx);
-    } else if (geometry_type == CAIRO_GL_GEOMETRY_TYPE_TRISTRIPS &&
-	     ! _cairo_gl_context_is_flushed (ctx) && tristrip_indices == 0) {
+    if (ctx->primitive_type != primitive_type) {
 	_cairo_gl_composite_flush (ctx);
+	ctx->primitive_type = primitive_type;
     }
 
     if (ctx->vb_offset + n_vertices * ctx->vertex_size > CAIRO_GL_VBO_SIZE)
@@ -856,7 +855,7 @@ _cairo_gl_composite_emit_rect (cairo_gl_context_t *ctx,
                                uint8_t alpha)
 {
     _cairo_gl_composite_prepare_buffer (ctx, 6,
-					CAIRO_GL_GEOMETRY_TYPE_TRIANGLES);
+					CAIRO_GL_PRIMITIVE_TYPE_TRIANGLES);
 
     _cairo_gl_composite_emit_vertex (ctx, x1, y1, alpha);
     _cairo_gl_composite_emit_vertex (ctx, x2, y1, alpha);
@@ -899,7 +898,7 @@ _cairo_gl_composite_emit_glyph (cairo_gl_context_t *ctx,
                                 GLfloat glyph_y2)
 {
     _cairo_gl_composite_prepare_buffer (ctx, 6,
-					CAIRO_GL_GEOMETRY_TYPE_TRIANGLES);
+					CAIRO_GL_PRIMITIVE_TYPE_TRIANGLES);
 
     _cairo_gl_composite_emit_glyph_vertex (ctx, x1, y1, glyph_x1, glyph_y1);
     _cairo_gl_composite_emit_glyph_vertex (ctx, x2, y1, glyph_x2, glyph_y1);
@@ -987,7 +986,7 @@ _cairo_gl_composite_emit_quad_as_tristrip (cairo_gl_context_t	*ctx,
 					   const cairo_point_t	quad[4])
 {
     _cairo_gl_composite_prepare_buffer (ctx, 4,
-					CAIRO_GL_GEOMETRY_TYPE_TRISTRIPS);
+					CAIRO_GL_PRIMITIVE_TYPE_TRISTRIPS);
 
     _cairo_gl_composite_emit_point (ctx, &quad[0], 0);
     _cairo_gl_composite_emit_point (ctx, &quad[1], 0);
@@ -1007,7 +1006,7 @@ _cairo_gl_composite_emit_triangle_as_tristrip (cairo_gl_context_t	*ctx,
 					       const cairo_point_t	 triangle[3])
 {
     _cairo_gl_composite_prepare_buffer (ctx, 3,
-					CAIRO_GL_GEOMETRY_TYPE_TRISTRIPS);
+					CAIRO_GL_PRIMITIVE_TYPE_TRISTRIPS);
 
     _cairo_gl_composite_emit_point (ctx, &triangle[0], 0);
     _cairo_gl_composite_emit_point (ctx, &triangle[1], 0);
diff --git a/src/cairo-gl-device.c b/src/cairo-gl-device.c
index ff0f6c8..4e86716 100644
--- a/src/cairo-gl-device.c
+++ b/src/cairo-gl-device.c
@@ -273,6 +273,7 @@ _cairo_gl_context_init (cairo_gl_context_t *ctx)
 	    return _cairo_error (CAIRO_STATUS_NO_MEMORY);
     }
 
+    ctx->primitive_type = CAIRO_GL_PRIMITIVE_TYPE_TRIANGLES;
     _cairo_array_init (&ctx->tristrip_indices, sizeof (unsigned short));
 
     /* PBO for any sort of texture upload */
diff --git a/src/cairo-gl-private.h b/src/cairo-gl-private.h
index eb75cd9..4e911fe 100644
--- a/src/cairo-gl-private.h
+++ b/src/cairo-gl-private.h
@@ -213,6 +213,11 @@ typedef enum cairo_gl_var_type {
   CAIRO_GL_VAR_TEXCOORDS,
 } cairo_gl_var_type_t;
 
+typedef enum cairo_gl_primitive_type {
+    CAIRO_GL_PRIMITIVE_TYPE_TRIANGLES,
+    CAIRO_GL_PRIMITIVE_TYPE_TRISTRIPS
+} cairo_gl_primitive_type_t;
+
 #define cairo_gl_var_type_hash(src,mask,spans,dest) ((spans) << 3) | ((mask) << 2 | (src << 1) | (dest))
 #define CAIRO_GL_VAR_TYPE_MAX ((CAIRO_GL_VAR_TEXCOORDS << 3) | (CAIRO_GL_VAR_TEXCOORDS << 2) | (CAIRO_GL_VAR_TEXCOORDS << 1) | CAIRO_GL_VAR_TEXCOORDS)
 
@@ -334,6 +339,8 @@ struct _cairo_gl_context {
     unsigned int vertex_size;
     cairo_region_t *clip_region;
     cairo_clip_t *clip;
+
+    cairo_gl_primitive_type_t primitive_type;
     cairo_array_t tristrip_indices;
 
     cairo_bool_t has_mesa_pack_invert;


More information about the cairo-commit mailing list