[Cogl] [PATCH 2/3] conform: Add color attribute based blending test

Robert Bragg robert at sixbynine.org
Thu May 30 07:40:34 PDT 2013


From: Robert Bragg <robert at linux.intel.com>

This adds a test to make sure that if the same pipeline is used to draw
two primitives, one which doesn't need blending because it has an opaque
color associated, and another using a color attribute that requires
blending then Cogl should recognize that it needs to enable blending for
the second primitive even though the pipeline hasn't changed.
---
 tests/conform/Makefile.am         |  1 +
 tests/conform/test-blend.c        | 64 +++++++++++++++++++++++++++++++++++++++
 tests/conform/test-conform-main.c |  1 +
 tests/conform/test-utils.c        | 14 +++++++++
 tests/conform/test-utils.h        | 18 +++++++++++
 5 files changed, 98 insertions(+)
 create mode 100644 tests/conform/test-blend.c

diff --git a/tests/conform/Makefile.am b/tests/conform/Makefile.am
index 8b73c53..e445741 100644
--- a/tests/conform/Makefile.am
+++ b/tests/conform/Makefile.am
@@ -28,6 +28,7 @@ unported_test_sources = \
 test_sources = \
 	test-atlas-migration.c \
 	test-blend-strings.c \
+	test-blend.c \
 	test-depth-test.c \
 	test-color-hsl.c \
 	test-color-mask.c \
diff --git a/tests/conform/test-blend.c b/tests/conform/test-blend.c
new file mode 100644
index 0000000..e67f11b
--- /dev/null
+++ b/tests/conform/test-blend.c
@@ -0,0 +1,64 @@
+#include <cogl/cogl.h>
+
+#include <string.h>
+
+#include "test-utils.h"
+
+static void
+paint (void)
+{
+  CoglPipeline *pipeline = cogl_pipeline_new (test_ctx);
+  int width = cogl_framebuffer_get_width (test_fb);
+  int half_width = width / 2;
+  int height = cogl_framebuffer_get_height (test_fb);
+  CoglVertexP2 tri0_vertices[] = {
+        { 0, 0 },
+        { 0, height },
+        { half_width, height },
+  };
+  CoglVertexP2C4 tri1_vertices[] = {
+        { half_width, 0, 0x80, 0x80, 0x80, 0x80 },
+        { half_width, height, 0x80, 0x80, 0x80, 0x80 },
+        { width, height, 0x80, 0x80, 0x80, 0x80 },
+  };
+  CoglPrimitive *tri0;
+  CoglPrimitive *tri1;
+
+  cogl_framebuffer_clear4f (test_fb, COGL_BUFFER_BIT_COLOR, 0, 0, 0, 0);
+
+  tri0 = cogl_primitive_new_p2 (test_ctx, COGL_VERTICES_MODE_TRIANGLES,
+                                3, tri0_vertices);
+  tri1 = cogl_primitive_new_p2c4 (test_ctx, COGL_VERTICES_MODE_TRIANGLES,
+                                  3, tri1_vertices);
+
+  /* Check that cogl correctly handles the case where we draw
+   * different primitives same pipeline and switch from using the
+   * opaque color associated with the pipeline and using a colour
+   * attribute with an alpha component which implies blending is
+   * required.
+   *
+   * If Cogl gets this wrong then then in all likelyhood the second
+   * primitive will be drawn with blending still disabled.
+   */
+
+  cogl_framebuffer_draw_primitive (test_fb, pipeline, tri0);
+  cogl_framebuffer_draw_primitive (test_fb, pipeline, tri1);
+
+  test_utils_check_pixel_and_alpha (test_fb,
+                                    half_width + 5,
+                                    height - 5,
+                                    0x80808080);
+}
+
+void
+test_blend (void)
+{
+  cogl_framebuffer_orthographic (test_fb, 0, 0,
+                                 cogl_framebuffer_get_width (test_fb),
+                                 cogl_framebuffer_get_height (test_fb),
+                                 -1,
+                                 100);
+
+  paint ();
+}
+
diff --git a/tests/conform/test-conform-main.c b/tests/conform/test-conform-main.c
index 671d0b2..c0234f2 100644
--- a/tests/conform/test-conform-main.c
+++ b/tests/conform/test-conform-main.c
@@ -53,6 +53,7 @@ main (int argc, char **argv)
   UNPORTED_TEST (test_fixed);
   UNPORTED_TEST (test_materials);
   ADD_TEST (test_blend_strings, 0, 0);
+  ADD_TEST (test_blend, 0, 0);
   ADD_TEST (test_premult, 0, 0);
   UNPORTED_TEST (test_readpixels);
   ADD_TEST (test_path, 0, 0);
diff --git a/tests/conform/test-utils.c b/tests/conform/test-utils.c
index a10019a..b8f2dcd 100644
--- a/tests/conform/test-utils.c
+++ b/tests/conform/test-utils.c
@@ -265,6 +265,20 @@ test_utils_check_pixel (CoglFramebuffer *fb,
 }
 
 void
+test_utils_check_pixel_and_alpha (CoglFramebuffer *fb,
+                                  int x, int y, uint32_t expected_pixel)
+{
+  uint8_t pixel[4];
+
+  cogl_framebuffer_read_pixels (fb,
+                                x, y, 1, 1,
+                                COGL_PIXEL_FORMAT_RGBA_8888_PRE,
+                                pixel);
+
+  test_utils_compare_pixel_and_alpha (pixel, expected_pixel);
+}
+
+void
 test_utils_check_pixel_rgb (CoglFramebuffer *fb,
                             int x, int y, int r, int g, int b)
 {
diff --git a/tests/conform/test-utils.h b/tests/conform/test-utils.h
index ae2fb64..49dbe1b 100644
--- a/tests/conform/test-utils.h
+++ b/tests/conform/test-utils.h
@@ -52,6 +52,24 @@ void
 test_utils_check_pixel (CoglFramebuffer *framebuffer,
                         int x, int y, uint32_t expected_pixel);
 
+/**
+ * @framebuffer: The #CoglFramebuffer to read from
+ * @x: x co-ordinate of the pixel to test
+ * @y: y co-ordinate of the pixel to test
+ * @pixel: An integer of the form 0xRRGGBBAA representing the expected
+ *         pixel value
+ *
+ * This performs reads a pixel on the given cogl @framebuffer and
+ * asserts that it matches the given color. The alpha channel is also
+ * checked unlike with test_utils_check_pixel(). The pixels are
+ * converted to a string and compared with g_assert_cmpstr so that if
+ * the comparison fails then the assert will display a meaningful
+ * message.
+ */
+void
+test_utils_check_pixel_and_alpha (CoglFramebuffer *fb,
+                                  int x, int y, uint32_t expected_pixel);
+
 /*
  * test_utils_check_pixel:
  * @framebuffer: The #CoglFramebuffer to read from
-- 
1.8.2.1



More information about the Cogl mailing list