mutter/tests/conform/test-copy-replace-texture.c
Robert Bragg 6436cd073d Declare interface types as void and remove cast macros
This declares the interface types CoglFramebuffer, CoglBuffer,
CoglTexture, CoglMetaTexture and CoglPrimitiveTexture as void when
including the public cogl.h header so that users don't have to use lots
of C type casts between instance types and interface types.

This also removes all of the COGL_XYZ() type cast macros since they do
nothing more than compile time type casting but it's less readable if
you haven't seen that coding pattern before.

Unlike with gobject based apis that use per-type macros for casting and
performing runtime type checking we instead prefer to do our runtime
type checking internally within the front-end public apis when objects
are passed into Cogl. This greatly reduces the verbosity for users of
the api and may help reduce the chance of excessive runtime type
checking that can sometimes be a problem.

(cherry picked from commit 248a76f5eac7e5ae4fb45208577f9a55360812a7)

Since we can't break the 1.x api this version of the patch actually
defines compatible NOP macros within deprecated/cogl-type-casts.h
2013-11-27 19:33:44 +00:00

122 lines
3.6 KiB
C

#include <cogl/cogl.h>
#include <string.h>
#include "test-utils.h"
/* Keep track of the number of textures that we've created and are
* still alive */
static int alive_texture_mask = 0;
#define N_LAYERS 3
#define N_PIPELINES 4
#define PIPELINE_LAYER_MASK(pipeline_num) \
(((1 << N_LAYERS) - 1) << (N_LAYERS * (pipeline_num) + 1))
#define LAST_PIPELINE_MASK PIPELINE_LAYER_MASK (N_PIPELINES - 1)
#define FIRST_PIPELINE_MASK PIPELINE_LAYER_MASK (0)
static void
free_texture_cb (void *user_data)
{
int texture_num = GPOINTER_TO_INT (user_data);
alive_texture_mask &= ~(1 << texture_num);
}
static CoglTexture *
create_texture (void)
{
static const guint8 data[] =
{ 0xff, 0xff, 0xff, 0xff };
static CoglUserDataKey texture_data_key;
CoglTexture2D *tex_2d;
static int texture_num = 1;
alive_texture_mask |= (1 << texture_num);
tex_2d = cogl_texture_2d_new_from_data (test_ctx,
1, 1, /* width / height */
COGL_PIXEL_FORMAT_RGBA_8888_PRE,
COGL_PIXEL_FORMAT_ANY,
4, /* rowstride */
data,
NULL);
/* Set some user data on the texture so we can track when it has
* been destroyed */
cogl_object_set_user_data (COGL_OBJECT (tex_2d),
&texture_data_key,
GINT_TO_POINTER (texture_num),
free_texture_cb);
texture_num++;
return tex_2d;
}
void
test_copy_replace_texture (void)
{
CoglPipeline *pipelines[N_PIPELINES];
int pipeline_num;
/* Create a set of pipeline copies each with three of their own
* replacement textures */
for (pipeline_num = 0; pipeline_num < N_PIPELINES; pipeline_num++)
{
int layer_num;
if (pipeline_num == 0)
pipelines[pipeline_num] = cogl_pipeline_new (test_ctx);
else
pipelines[pipeline_num] =
cogl_pipeline_copy (pipelines[pipeline_num - 1]);
for (layer_num = 0; layer_num < N_LAYERS; layer_num++)
{
CoglTexture *tex = create_texture ();
cogl_pipeline_set_layer_texture (pipelines[pipeline_num],
layer_num,
tex);
cogl_object_unref (tex);
}
}
/* Unref everything but the last pipeline */
for (pipeline_num = 0; pipeline_num < N_PIPELINES - 1; pipeline_num++)
cogl_object_unref (pipelines[pipeline_num]);
if (alive_texture_mask && cogl_test_verbose ())
{
int i;
g_print ("Alive textures:");
for (i = 0; i < N_PIPELINES * N_LAYERS; i++)
if ((alive_texture_mask & (1 << (i + 1))))
g_print (" %i", i);
g_print ("\n");
}
/* Ideally there should only be the textures from the last pipeline
* left alive. We also let Cogl keep the textures from the first
* texture alive because currently the child of the third layer in
* the first pipeline will retain its authority on the unit index
* state so that it can set it to 2. If there are more textures then
* it means the pipeline isn't correctly pruning redundant
* ancestors */
g_assert_cmpint (alive_texture_mask & ~FIRST_PIPELINE_MASK,
==,
LAST_PIPELINE_MASK);
/* Clean up the last pipeline */
cogl_object_unref (pipelines[N_PIPELINES - 1]);
/* That should get rid of the last of the textures */
g_assert_cmpint (alive_texture_mask, ==, 0);
if (cogl_test_verbose ())
g_print ("OK\n");
}