summaryrefslogtreecommitdiff
path: root/tests/conform/test-copy-replace-texture.c
blob: 47b0ae83b6bc9b94ccdb1b8e3383f99ee403ad73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#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");
}