~elementary-os/ubuntu-package-imports/mutter-bionic

« back to all changes in this revision

Viewing changes to cogl/tests/conform/test-pipeline-cache-unrefs-texture.c

  • Committer: RabbitBot
  • Date: 2018-04-11 14:49:36 UTC
  • Revision ID: rabbitbot@elementary.io-20180411144936-hgymqa9d8d1xfpbh
Initial import, version 3.28.0-2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <cogl/cogl.h>
 
2
 
 
3
#include "test-utils.h"
 
4
 
 
5
/* Keep track of the number of textures that we've created and are
 
6
 * still alive */
 
7
static int destroyed_texture_count = 0;
 
8
 
 
9
#define N_TEXTURES 3
 
10
 
 
11
static void
 
12
free_texture_cb (void *user_data)
 
13
{
 
14
  destroyed_texture_count++;
 
15
}
 
16
 
 
17
static CoglTexture *
 
18
create_texture (void)
 
19
{
 
20
  static const guint8 data[] =
 
21
    { 0xff, 0xff, 0xff, 0xff };
 
22
  static CoglUserDataKey texture_data_key;
 
23
  CoglTexture2D *tex_2d;
 
24
 
 
25
  tex_2d = cogl_texture_2d_new_from_data (test_ctx,
 
26
                                          1, 1, /* width / height */
 
27
                                          COGL_PIXEL_FORMAT_RGBA_8888_PRE,
 
28
                                          4, /* rowstride */
 
29
                                          data,
 
30
                                          NULL);
 
31
 
 
32
  /* Set some user data on the texture so we can track when it has
 
33
   * been destroyed */
 
34
  cogl_object_set_user_data (COGL_OBJECT (tex_2d),
 
35
                             &texture_data_key,
 
36
                             GINT_TO_POINTER (1),
 
37
                             free_texture_cb);
 
38
 
 
39
  return tex_2d;
 
40
}
 
41
 
 
42
void
 
43
test_pipeline_cache_unrefs_texture (void)
 
44
{
 
45
  CoglPipeline *pipeline = cogl_pipeline_new (test_ctx);
 
46
  CoglPipeline *simple_pipeline;
 
47
  int i;
 
48
 
 
49
  /* Create a pipeline with three texture layers. That way we can be
 
50
   * pretty sure the pipeline will cause a unique shader to be
 
51
   * generated in the cache */
 
52
  for (i = 0; i < N_TEXTURES; i++)
 
53
    {
 
54
      CoglTexture *tex = create_texture ();
 
55
      cogl_pipeline_set_layer_texture (pipeline, i, tex);
 
56
      cogl_object_unref (tex);
 
57
    }
 
58
 
 
59
  /* Draw something with the pipeline to ensure it gets into the
 
60
   * pipeline cache */
 
61
  cogl_framebuffer_draw_rectangle (test_fb,
 
62
                                   pipeline,
 
63
                                   0, 0, 10, 10);
 
64
  cogl_framebuffer_finish (test_fb);
 
65
 
 
66
  /* Draw something else so that it is no longer the current flushed
 
67
   * pipeline, and the units have a different texture bound */
 
68
  simple_pipeline = cogl_pipeline_new (test_ctx);
 
69
  for (i = 0; i < N_TEXTURES; i++)
 
70
    {
 
71
      CoglColor combine_constant;
 
72
      cogl_color_init_from_4ub (&combine_constant, i, 0, 0, 255);
 
73
      cogl_pipeline_set_layer_combine_constant (simple_pipeline,
 
74
                                                i,
 
75
                                                &combine_constant);
 
76
    }
 
77
  cogl_framebuffer_draw_rectangle (test_fb, simple_pipeline, 0, 0, 10, 10);
 
78
  cogl_framebuffer_finish (test_fb);
 
79
  cogl_object_unref (simple_pipeline);
 
80
 
 
81
  g_assert_cmpint (destroyed_texture_count, ==, 0);
 
82
 
 
83
  /* Destroy the pipeline. This should immediately cause the textures
 
84
   * to be freed */
 
85
  cogl_object_unref (pipeline);
 
86
 
 
87
  g_assert_cmpint (destroyed_texture_count, ==, N_TEXTURES);
 
88
 
 
89
  if (cogl_test_verbose ())
 
90
    g_print ("OK\n");
 
91
}