[Cogl] [PATCH 1/2] Simplify test-pixel-buffer

Neil Roberts neil at linux.intel.com
Tue Nov 27 12:11:16 PST 2012


test-pixel-buffer previously had two tests, one to check filling the
pixel buffer by mapping it and another to fill it by just setting the
data. These tests were set up in a kind of confusing way where it
would try to paint both steps and then validate them together using
colors looked up from a table. This patch separates out the two tests
and gets rid of the tables which hopefully makes them a bit easier to
follow.

The contents of the bitmap are now set to an image with has a
different colour for each of its four quadrants instead of just a
single colour in the hope that this will be a bit more of an extensive
test.

The old code had a third test that was commented out. This test has
been removed.

The textures are now created using cogl_texture_2d_new_* which means
they won't be in the atlas. This exposes a bug where setting the
entire contents of the texture won't handle errors properly and it
will hit an assertion. The previous code using the atlas would end up
only setting a sub-region of the larger atlas texture so the bug
wouldn't be hit. To make sure we still test this code path there is
now a third test which explicitly sets a sub-region of the texture
using the bitmap.
---
 tests/conform/test-conform-main.c |   4 +-
 tests/conform/test-pixel-buffer.c | 409 +++++++++++++++++++-------------------
 2 files changed, 207 insertions(+), 206 deletions(-)

diff --git a/tests/conform/test-conform-main.c b/tests/conform/test-conform-main.c
index 4b7b698..78eefc3 100644
--- a/tests/conform/test-conform-main.c
+++ b/tests/conform/test-conform-main.c
@@ -67,7 +67,9 @@ main (int argc, char **argv)
   UNPORTED_TEST (test_multitexture);
   UNPORTED_TEST (test_texture_mipmaps);
   ADD_TEST (test_sub_texture, 0, 0);
-  ADD_TEST (test_pixel_buffer, 0, 0);
+  ADD_TEST (test_pixel_buffer_map, 0, TEST_KNOWN_FAILURE);
+  ADD_TEST (test_pixel_buffer_set_data, 0, TEST_KNOWN_FAILURE);
+  ADD_TEST (test_pixel_buffer_sub_region, 0, 0);
   UNPORTED_TEST (test_texture_rectangle);
   ADD_TEST (test_texture_3d, TEST_REQUIREMENT_TEXTURE_3D, 0);
   ADD_TEST (test_wrap_modes, 0, 0);
diff --git a/tests/conform/test-pixel-buffer.c b/tests/conform/test-pixel-buffer.c
index cd0272b..39df4ab 100644
--- a/tests/conform/test-pixel-buffer.c
+++ b/tests/conform/test-pixel-buffer.c
@@ -3,71 +3,64 @@
 
 #include "test-utils.h"
 
-#define TILE_SIZE        32.0f
+#define BITMAP_SIZE 256
 
-enum
-{
-  TILE_MAP,
-  TILE_SET_DATA,
-  NB_TILES,
-  TILE_SET_REGION,
-};
-
-typedef struct test_tile
-{
-  uint8_t color[4];
-  gfloat x, y;
-  CoglBuffer *buffer;
-  CoglTexture *texture;
-} TestTile;
-
-typedef struct _TestState
-{
-  TestTile *tiles;
-  int width;
-  int height;
-} TestState;
-
-static CoglTexture *
-create_texture_from_bitmap (CoglBitmap *bitmap)
+/*
+ * Creates a 256 x 256 with image data split into four quadrants. The
+ * colours of these in reading order will be: blue, green, cyan,
+ * red */
+static void
+generate_bitmap_data (uint8_t *data,
+                      int stride)
 {
-  CoglTexture *texture;
-
-  texture = cogl_texture_new_from_bitmap (bitmap,
-                                          COGL_TEXTURE_NONE,
-                                          COGL_PIXEL_FORMAT_RGBA_8888,
-                                          NULL); /* don't catch errors */
+  int y, x;
 
-  g_assert (texture != NULL);
-
-  return texture;
+  for (y = 0; y < BITMAP_SIZE; y++)
+    {
+      for (x = 0; x < BITMAP_SIZE; x++)
+        {
+          int color_num = x / (BITMAP_SIZE / 2) + y / (BITMAP_SIZE / 2) * 2 + 1;
+          *(data++) = (color_num & 4) ? 255 : 0;
+          *(data++) = (color_num & 2) ? 255 : 0;
+          *(data++) = (color_num & 1) ? 255 : 0;
+          *(data++) = 255;
+        }
+      data += stride - BITMAP_SIZE * 4;
+    }
 }
 
-static void
-create_map_tile (CoglContext *context,
-                 TestTile *tile)
+static CoglBitmap *
+create_bitmap (void)
 {
   CoglBitmap *bitmap;
   CoglBuffer *buffer;
-  guchar *map;
-  unsigned int i;
-  unsigned int stride;
-  uint8_t *line;
 
-  bitmap = cogl_bitmap_new_with_size (context,
-                                      TILE_SIZE,
-                                      TILE_SIZE,
+  bitmap = cogl_bitmap_new_with_size (test_ctx,
+                                      BITMAP_SIZE,
+                                      BITMAP_SIZE,
                                       COGL_PIXEL_FORMAT_RGBA_8888);
   buffer = COGL_BUFFER (cogl_bitmap_get_buffer (bitmap));
-  stride = cogl_bitmap_get_rowstride (bitmap);
 
   g_assert (cogl_is_pixel_buffer (buffer));
   g_assert (cogl_is_buffer (buffer));
 
   cogl_buffer_set_update_hint (buffer, COGL_BUFFER_UPDATE_HINT_DYNAMIC);
   g_assert_cmpint (cogl_buffer_get_update_hint (buffer),
-            ==,
-            COGL_BUFFER_UPDATE_HINT_DYNAMIC);
+                   ==,
+                   COGL_BUFFER_UPDATE_HINT_DYNAMIC);
+
+  return bitmap;
+}
+
+static CoglBitmap *
+create_and_fill_bitmap (void)
+{
+  CoglBitmap *bitmap = create_bitmap ();
+  CoglBuffer *buffer = COGL_BUFFER (cogl_bitmap_get_buffer (bitmap));
+  uint8_t *map;
+  unsigned int stride;
+
+  stride = cogl_bitmap_get_rowstride (bitmap);
 
   map = cogl_buffer_map (buffer,
                          COGL_BUFFER_ACCESS_WRITE,
@@ -75,202 +68,208 @@ create_map_tile (CoglContext *context,
                          NULL); /* don't catch errors */
   g_assert (map);
 
-  line = g_alloca (TILE_SIZE * 4);
-  for (i = 0; i < TILE_SIZE * 4; i += 4)
-    memcpy (line + i, tile->color, 4);
+  generate_bitmap_data (map, stride);
 
-  for (i = 0; i < TILE_SIZE; i++)
-    memcpy (map + stride * i, line, TILE_SIZE * 4);
+  cogl_buffer_unmap (COGL_BUFFER (buffer));
 
-  cogl_buffer_unmap (buffer);
+  return bitmap;
+}
 
-  tile->buffer = cogl_object_ref (buffer);
-  tile->texture = create_texture_from_bitmap (bitmap);
+static CoglTexture *
+create_texture_from_bitmap (CoglBitmap *bitmap)
+{
+  CoglTexture2D *texture;
 
-  cogl_object_unref (bitmap);
+  texture = cogl_texture_2d_new_from_bitmap (bitmap,
+                                             COGL_PIXEL_FORMAT_RGBA_8888,
+                                             NULL); /* don't catch errors */
+
+  g_assert (texture != NULL);
+
+  return COGL_TEXTURE (texture);
 }
 
-#if 0
-static void
-create_set_region_tile (CoglContext *context,
-                        TestTile *tile)
+static CoglPipeline *
+create_pipeline_from_texture (CoglTexture *texture)
 {
-  CoglBitmap *bitmap;
-  CoglBuffer *buffer;
-  uint8_t bottom_color[4];
-  unsigned int rowstride = 0;
-  guchar *data;
-  unsigned int i;
-
-  bitmap = cogl_bitmap_new_with_size (context,
-                                      TILE_SIZE,
-                                      TILE_SIZE,
-                                      COGL_PIXEL_FORMAT_RGBA_8888);
-  buffer = COGL_BUFFER (cogl_bitmap_get_buffer (bitmap));
-  rowstride = cogl_bitmap_get_rowstride (bitmap);
-
-  g_assert (cogl_is_pixel_buffer (buffer));
-  g_assert (cogl_is_buffer (buffer));
+  CoglPipeline *pipeline = cogl_pipeline_new (test_ctx);
 
-  /* while at it, set/get the hint */
-  cogl_buffer_set_update_hint (buffer, COGL_BUFFER_UPDATE_HINT_STATIC);
-  g_assert (cogl_buffer_get_update_hint (buffer) ==
-            COGL_BUFFER_UPDATE_HINT_STATIC);
+  cogl_pipeline_set_layer_texture (pipeline, 0, texture);
+  cogl_pipeline_set_layer_filters (pipeline,
+                                   0, /* layer_num */
+                                   COGL_PIPELINE_FILTER_NEAREST,
+                                   COGL_PIPELINE_FILTER_NEAREST);
 
-  data = g_malloc (TILE_SIZE * TILE_SIZE * 4);
-  /* create a buffer with the data we want to copy to the buffer */
-  for (i = 0; i < TILE_SIZE * TILE_SIZE * 4; i += 4)
-      memcpy (data + i, &tile->color, 4);
+  return pipeline;
+}
 
-  cogl_pixel_array_set_region (buffer,
-                                data,
-                                TILE_SIZE, TILE_SIZE,
-                                TILE_SIZE,
-                                0, 0);
+static void
+check_colours (uint32_t color0,
+               uint32_t color1,
+               uint32_t color2,
+               uint32_t color3)
+{
+  int fb_width = cogl_framebuffer_get_width (test_fb);
+  int fb_height = cogl_framebuffer_get_height (test_fb);
 
-  memcpy (bottom_color, tile->color, 4);
-  for (i = 0; i < TILE_SIZE / 2; i++)
-    memcpy (data + i, bottom_color, 4);
+  test_utils_check_region (test_fb,
+                           1, 1, /* x/y */
+                           fb_width / 2 - 2, /* width */
+                           fb_height / 2 - 2, /* height */
+                           color0);
+  test_utils_check_region (test_fb,
+                           fb_width / 2 + 1, /* x */
+                           1, /* y */
+                           fb_width / 2 - 2, /* width */
+                           fb_height / 2 - 2, /* height */
+                           color1);
+  test_utils_check_region (test_fb,
+                           1, /* x */
+                           fb_height / 2 + 1, /* y */
+                           fb_width / 2 - 2, /* width */
+                           fb_height / 2 - 2, /* height */
+                           color2);
+  test_utils_check_region (test_fb,
+                           fb_width / 2 + 1, /* x */
+                           fb_height / 2 + 1, /* y */
+                           fb_width / 2 - 2, /* width */
+                           fb_height / 2 - 2, /* height */
+                           color3);
+}
 
-  cogl_buffer_set_data (buffer, 0, data, TILE_SIZE * TILE_SIZE * 4 / 2);
+void
+test_pixel_buffer_map (void)
+{
+  CoglBitmap *bitmap = create_and_fill_bitmap ();
+  CoglPipeline *pipeline;
+  CoglTexture *texture;
 
-  g_free (data);
+  texture = create_texture_from_bitmap (bitmap);
+  pipeline = create_pipeline_from_texture (texture);
 
-  tile->buffer = cogl_object_ref (buffer);
-  tile->texture = create_texture_from_bitmap (bitmap);
+  cogl_framebuffer_draw_rectangle (test_fb,
+                                   pipeline,
+                                   -1.0f, 1.0f,
+                                   1.0f, -1.0f);
 
   cogl_object_unref (bitmap);
+  cogl_object_unref (texture);
+  cogl_object_unref (pipeline);
+
+  check_colours (0x0000ffff,
+                 0x00ff00ff,
+                 0x00ffffff,
+                 0xff0000ff);
+
+  if (cogl_test_verbose ())
+    g_print ("OK\n");
 }
-#endif
 
-static void
-create_set_data_tile (CoglContext *context,
-                      TestTile *tile)
+void
+test_pixel_buffer_set_data (void)
 {
-  CoglBitmap *bitmap;
-  CoglBuffer *buffer;
-  unsigned int rowstride = 0;
-  CoglBool res;
-  guchar *data;
-  unsigned int i;
-
-  bitmap = cogl_bitmap_new_with_size (context,
-                                      TILE_SIZE,
-                                      TILE_SIZE,
-                                      COGL_PIXEL_FORMAT_RGBA_8888);
-  buffer = COGL_BUFFER (cogl_bitmap_get_buffer (bitmap));
-  rowstride = cogl_bitmap_get_rowstride (bitmap);
+  CoglBitmap *bitmap = create_bitmap ();
+  CoglBuffer *buffer = COGL_BUFFER (cogl_bitmap_get_buffer (bitmap));
+  CoglPipeline *pipeline;
+  CoglTexture *texture;
+  uint8_t *data;
+  unsigned int stride;
 
-  g_assert (cogl_is_pixel_buffer (buffer));
-  g_assert (cogl_is_buffer (buffer));
-  g_assert_cmpint (cogl_buffer_get_size (buffer), ==, rowstride * TILE_SIZE);
+  stride = cogl_bitmap_get_rowstride (bitmap);
 
-  /* create a buffer with the data we want to copy to the buffer */
-  data = g_malloc (TILE_SIZE * TILE_SIZE * 4);
-  for (i = 0; i < TILE_SIZE * TILE_SIZE * 4; i += 4)
-      memcpy (data + i, tile->color, 4);
+  data = g_malloc (stride * BITMAP_SIZE);
 
-  /* FIXME: this doesn't consider the rowstride */
-  res = cogl_buffer_set_data (buffer, 0, data, TILE_SIZE * TILE_SIZE * 4, NULL);
-  g_assert (res);
+  generate_bitmap_data (data, stride);
+
+  cogl_buffer_set_data (buffer,
+                        0, /* offset */
+                        data,
+                        stride * (BITMAP_SIZE - 1) +
+                        BITMAP_SIZE * 4,
+                        NULL /* don't catch errors */);
 
   g_free (data);
 
-  tile->buffer = cogl_object_ref (buffer);
-  tile->texture = create_texture_from_bitmap (bitmap);
+  texture = create_texture_from_bitmap (bitmap);
+  pipeline = create_pipeline_from_texture (texture);
 
-  cogl_object_unref (bitmap);
-}
+  cogl_framebuffer_draw_rectangle (test_fb,
+                                   pipeline,
+                                   -1.0f, 1.0f,
+                                   1.0f, -1.0f);
 
-static void
-draw_frame (TestState *state)
-{
-  unsigned int i;
+  cogl_object_unref (bitmap);
+  cogl_object_unref (texture);
+  cogl_object_unref (pipeline);
 
-  /* Paint the textures */
-  for (i = 0; i < NB_TILES; i++)
-    {
-      CoglPipeline *pipeline = cogl_pipeline_new (test_ctx);
-      cogl_pipeline_set_layer_texture (pipeline, 0, state->tiles[i].texture);
-      cogl_framebuffer_draw_rectangle (test_fb,
-                                       pipeline,
-                                       state->tiles[i].x,
-                                       state->tiles[i].y,
-                                       state->tiles[i].x + TILE_SIZE,
-                                       state->tiles[i].y + TILE_SIZE);
-      cogl_object_unref (pipeline);
-    }
+  check_colours (0x0000ffff,
+                 0x00ff00ff,
+                 0x00ffffff,
+                 0xff0000ff);
 
+  if (cogl_test_verbose ())
+    g_print ("OK\n");
 }
 
-static void
-validate_tile (TestState *state,
-               TestTile  *tile)
+static CoglTexture *
+create_white_texture (void)
 {
-  test_utils_check_region (test_fb,
-                           tile->x, tile->y,
-                           TILE_SIZE, TILE_SIZE,
-                           (tile->color[0] << 24) |
-                           (tile->color[1] << 16) |
-                           (tile->color[2] << 8) |
-                           0xff);
-}
+  CoglTexture2D *texture;
+  uint8_t *data = g_malloc (BITMAP_SIZE * BITMAP_SIZE * 4);
 
-static void
-validate_result (TestState *state)
-{
-  unsigned int i;
+  memset (data, 255, BITMAP_SIZE * BITMAP_SIZE * 4);
+
+  texture = cogl_texture_2d_new_from_data (test_ctx,
+                                           BITMAP_SIZE,
+                                           BITMAP_SIZE,
+                                           COGL_PIXEL_FORMAT_RGBA_8888,
+                                           COGL_PIXEL_FORMAT_ANY,
+                                           BITMAP_SIZE * 4, /* rowstride */
+                                           data,
+                                           NULL); /* don't catch errors */
+
+  g_free (data);
 
-  for (i = 0; i < NB_TILES; i++)
-    validate_tile (state, &state->tiles[i]);
+  return COGL_TEXTURE (texture);
 }
 
 void
-test_pixel_buffer (void)
+test_pixel_buffer_sub_region (void)
 {
-  TestState state;
-  int i;
-  static TestTile tiles[NB_TILES] =
-    {
-        /*         color             x  y buffer tex */
-
-        /* MAP */
-        { { 0xff, 0x00, 0x00, 0xff }, 0.0f, 0.0f, NULL, NULL },
-#if 0
-        /* SET_REGION */
-        { { 0x7e, 0x7e, 0xff, 0x7e }, 0.0f, TILE_SIZE, NULL, NULL },
-#endif
-        /* SET_DATA */
-        { { 0x7e, 0xff, 0x7e, 0xff }, 0.0f, TILE_SIZE, NULL, NULL }
-    };
-
-  state.width = cogl_framebuffer_get_width (test_fb);
-  state.height = cogl_framebuffer_get_height (test_fb);
-  cogl_framebuffer_orthographic (test_fb,
-                                 0, 0,
-                                 state.width,
-                                 state.height,
-                                 -1,
-                                 100);
-
-  create_map_tile (test_ctx, &tiles[TILE_MAP]);
-#if 0
-  create_set_region_tile (test_ctx, &tiles[TILE_SET_REGION]);
-#endif
-  create_set_data_tile (test_ctx, &tiles[TILE_SET_DATA]);
-
-  state.tiles = tiles;
-
-  draw_frame (&state);
-  validate_result (&state);
-
-  for (i = 0; i < NB_TILES; i++)
-    {
-      cogl_object_unref (state.tiles[i].buffer);
-      cogl_object_unref (state.tiles[i].texture);
-    }
+  CoglBitmap *bitmap = create_and_fill_bitmap ();
+  CoglPipeline *pipeline;
+  CoglTexture *texture;
+
+  texture = create_white_texture ();
+
+  /* Replace the top-right quadrant of the texture with the red part
+   * of the bitmap */
+  cogl_texture_set_region_from_bitmap (texture,
+                                       BITMAP_SIZE / 2, /* src_x */
+                                       BITMAP_SIZE / 2, /* src_y */
+                                       BITMAP_SIZE / 2, /* dst_x */
+                                       0, /* dst_y */
+                                       BITMAP_SIZE / 2, /* dst_width */
+                                       BITMAP_SIZE / 2, /* dst_height */
+                                       bitmap,
+                                       NULL /* don't catch errors */);
+
+  pipeline = create_pipeline_from_texture (texture);
+
+  cogl_framebuffer_draw_rectangle (test_fb,
+                                   pipeline,
+                                   -1.0f, 1.0f,
+                                   1.0f, -1.0f);
+
+  cogl_object_unref (bitmap);
+  cogl_object_unref (texture);
+  cogl_object_unref (pipeline);
+
+  check_colours (0xffffffff,
+                 0xff0000ff,
+                 0xffffffff,
+                 0xffffffff);
 
   if (cogl_test_verbose ())
     g_print ("OK\n");
 }
-
-- 
1.7.11.3.g3c3efa5



More information about the Cogl mailing list