~ubuntu-branches/ubuntu/precise/clutter-1.0/precise

« back to all changes in this revision

Viewing changes to tests/conform/test-cogl-materials.c

  • Committer: Bazaar Package Importer
  • Author(s): Emilio Pozuelo Monfort
  • Date: 2010-03-21 13:27:56 UTC
  • mto: (2.1.3 experimental) (1.3.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 8.
  • Revision ID: james.westby@ubuntu.com-20100321132756-nf8yd30yxo3zzwcm
Tags: upstream-1.2.2
ImportĀ upstreamĀ versionĀ 1.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
#include <clutter/clutter.h>
 
3
#include <cogl/cogl.h>
 
4
#include <string.h>
 
5
 
 
6
#include "test-conform-common.h"
 
7
 
 
8
static const ClutterColor stage_color = { 0x0, 0x0, 0x0, 0xff };
 
9
 
 
10
#define QUAD_WIDTH 20
 
11
 
 
12
#define RED 0
 
13
#define GREEN 1
 
14
#define BLUE 2
 
15
#define ALPHA 3
 
16
 
 
17
#define MASK_RED(COLOR)   ((COLOR & 0xff000000) >> 24);
 
18
#define MASK_GREEN(COLOR) ((COLOR & 0xff0000) >> 16);
 
19
#define MASK_BLUE(COLOR)  ((COLOR & 0xff00) >> 8);
 
20
#define MASK_ALPHA(COLOR) (COLOR & 0xff);
 
21
 
 
22
#define SKIP_FRAMES 2
 
23
 
 
24
typedef struct _TestState
 
25
{
 
26
  guint frame;
 
27
  ClutterGeometry stage_geom;
 
28
} TestState;
 
29
 
 
30
 
 
31
static void
 
32
check_pixel (TestState *state, int x, int y, guint32 color)
 
33
{
 
34
  GLint y_off;
 
35
  GLint x_off;
 
36
  GLubyte pixel[4];
 
37
  guint8 r = MASK_RED (color);
 
38
  guint8 g = MASK_GREEN (color);
 
39
  guint8 b = MASK_BLUE (color);
 
40
  guint8 a = MASK_ALPHA (color);
 
41
 
 
42
  /* See what we got... */
 
43
 
 
44
  /* NB: glReadPixels is done in GL screen space so y = 0 is at the bottom */
 
45
  y_off = y * QUAD_WIDTH + (QUAD_WIDTH / 2);
 
46
  x_off = x * QUAD_WIDTH + (QUAD_WIDTH / 2);
 
47
 
 
48
  /* XXX:
 
49
   * We haven't always had good luck with GL drivers implementing glReadPixels
 
50
   * reliably and skipping the first two frames improves our chances... */
 
51
  if (state->frame <= SKIP_FRAMES)
 
52
    return;
 
53
 
 
54
  cogl_read_pixels (x_off, y_off, 1, 1,
 
55
                    COGL_READ_PIXELS_COLOR_BUFFER,
 
56
                    COGL_PIXEL_FORMAT_RGBA_8888_PRE,
 
57
                    pixel);
 
58
  if (g_test_verbose ())
 
59
    g_print ("  result = %02x, %02x, %02x, %02x\n",
 
60
             pixel[RED], pixel[GREEN], pixel[BLUE], pixel[ALPHA]);
 
61
 
 
62
  if (g_test_verbose ())
 
63
    g_print ("  expected = %x, %x, %x, %x\n",
 
64
             r, g, b, a);
 
65
  /* FIXME - allow for hardware in-precision */
 
66
  g_assert (pixel[RED] == r);
 
67
  g_assert (pixel[GREEN] == g);
 
68
  g_assert (pixel[BLUE] == b);
 
69
 
 
70
  /* FIXME
 
71
   * We ignore the alpha, since we don't know if our render target is
 
72
   * RGB or RGBA */
 
73
  /* g_assert (pixel[ALPHA] == a); */
 
74
}
 
75
 
 
76
static void
 
77
test_invalid_texture_layers (TestState *state, int x, int y)
 
78
{
 
79
  CoglHandle        material = cogl_material_new ();
 
80
  CoglTextureVertex verts[4] = {
 
81
    { .x = 0,          .y = 0,          .z = 0 },
 
82
    { .x = 0,          .y = QUAD_WIDTH, .z = 0 },
 
83
    { .x = QUAD_WIDTH, .y = QUAD_WIDTH, .z = 0 },
 
84
    { .x = QUAD_WIDTH, .y = 0,          .z = 0 },
 
85
  };
 
86
  CoglHandle vbo;
 
87
 
 
88
  cogl_push_matrix ();
 
89
 
 
90
  cogl_translate (x * QUAD_WIDTH, y * QUAD_WIDTH, 0);
 
91
 
 
92
  /* explicitly create a layer with an invalid handle. This may be desireable
 
93
   * if the user also sets a texture combine string that e.g. refers to a
 
94
   * constant color. */
 
95
  cogl_material_set_layer (material, 0, COGL_INVALID_HANDLE);
 
96
 
 
97
  cogl_set_source (material);
 
98
  cogl_rectangle (0, 0, QUAD_WIDTH, QUAD_WIDTH);
 
99
 
 
100
  cogl_translate (0, QUAD_WIDTH, 0);
 
101
  cogl_polygon (verts, 4, FALSE);
 
102
 
 
103
  cogl_translate (0, QUAD_WIDTH, 0);
 
104
  vbo = cogl_vertex_buffer_new (4);
 
105
  cogl_vertex_buffer_add (vbo,
 
106
                          "gl_Vertex",
 
107
                          2, /* n components */
 
108
                          COGL_ATTRIBUTE_TYPE_FLOAT,
 
109
                          FALSE, /* normalized */
 
110
                          sizeof (CoglTextureVertex), /* stride */
 
111
                          verts);
 
112
  cogl_vertex_buffer_draw (vbo,
 
113
                           COGL_VERTICES_MODE_TRIANGLE_FAN,
 
114
                           0, /* first */
 
115
                           4); /* count */
 
116
  cogl_handle_unref (vbo);
 
117
 
 
118
  cogl_pop_matrix ();
 
119
 
 
120
  cogl_handle_unref (material);
 
121
 
 
122
  /* We expect a white fallback material to be used */
 
123
  check_pixel (state, x, y,   0xffffffff);
 
124
  check_pixel (state, x, y+1, 0xffffffff);
 
125
  check_pixel (state, x, y+2, 0xffffffff);
 
126
}
 
127
 
 
128
static void
 
129
on_paint (ClutterActor *actor, TestState *state)
 
130
{
 
131
  int frame_num;
 
132
 
 
133
  test_invalid_texture_layers (state,
 
134
                               0, 0 /* position */
 
135
                               );
 
136
 
 
137
  /* XXX: Experiments have shown that for some buggy drivers, when using
 
138
   * glReadPixels there is some kind of race, so we delay our test for a
 
139
   * few frames and a few seconds:
 
140
   */
 
141
  frame_num = state->frame++;
 
142
  if (frame_num < SKIP_FRAMES)
 
143
    g_usleep (G_USEC_PER_SEC);
 
144
 
 
145
  /* Comment this out if you want visual feedback for what this test paints */
 
146
#if 1
 
147
  if (frame_num > SKIP_FRAMES)
 
148
    clutter_main_quit ();
 
149
#endif
 
150
}
 
151
 
 
152
static gboolean
 
153
queue_redraw (gpointer stage)
 
154
{
 
155
  clutter_actor_queue_redraw (CLUTTER_ACTOR (stage));
 
156
 
 
157
  return TRUE;
 
158
}
 
159
 
 
160
void
 
161
test_cogl_materials (TestConformSimpleFixture *fixture,
 
162
                     gconstpointer data)
 
163
{
 
164
  TestState state;
 
165
  ClutterActor *stage;
 
166
  ClutterActor *group;
 
167
  guint idle_source;
 
168
 
 
169
  state.frame = 0;
 
170
 
 
171
  stage = clutter_stage_get_default ();
 
172
 
 
173
  clutter_stage_set_color (CLUTTER_STAGE (stage), &stage_color);
 
174
  clutter_actor_get_geometry (stage, &state.stage_geom);
 
175
 
 
176
  group = clutter_group_new ();
 
177
  clutter_container_add_actor (CLUTTER_CONTAINER (stage), group);
 
178
 
 
179
  /* We force continuous redrawing of the stage, since we need to skip
 
180
   * the first few frames, and we wont be doing anything else that
 
181
   * will trigger redrawing. */
 
182
  idle_source = g_idle_add (queue_redraw, stage);
 
183
 
 
184
  g_signal_connect (group, "paint", G_CALLBACK (on_paint), &state);
 
185
 
 
186
  clutter_actor_show_all (stage);
 
187
 
 
188
  clutter_main ();
 
189
 
 
190
  g_source_remove (idle_source);
 
191
 
 
192
  if (g_test_verbose ())
 
193
    g_print ("OK\n");
 
194
}
 
195