~oem-solutions-group/unity-2d/clutter-1.0

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Emilio Pozuelo Monfort
  • Date: 2010-03-21 13:27:56 UTC
  • mto: (2.1.3 experimental)
  • 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
 
 
5
#include "test-conform-common.h"
 
6
 
 
7
#define RED 0
 
8
#define GREEN 1
 
9
#define BLUE 2
 
10
 
 
11
#define FRAMEBUFFER_WIDTH  640
 
12
#define FRAMEBUFFER_HEIGHT 480
 
13
 
 
14
static const ClutterColor stage_color = { 0x0, 0x0, 0x0, 0xff };
 
15
 
 
16
 
 
17
static void
 
18
on_paint (ClutterActor *actor, void *state)
 
19
{
 
20
  float saved_viewport[4];
 
21
  CoglMatrix saved_projection;
 
22
  CoglMatrix projection;
 
23
  CoglMatrix modelview;
 
24
  guchar *data;
 
25
  CoglHandle tex;
 
26
  CoglHandle offscreen;
 
27
  guint8 pixel[4];
 
28
 
 
29
  /* Save the Clutter viewport/matrices and load identity matrices */
 
30
 
 
31
  cogl_get_viewport (saved_viewport);
 
32
  cogl_get_projection_matrix (&saved_projection);
 
33
  cogl_push_matrix ();
 
34
 
 
35
  cogl_matrix_init_identity (&projection);
 
36
  cogl_matrix_init_identity (&modelview);
 
37
 
 
38
  cogl_set_projection_matrix (&projection);
 
39
  cogl_set_modelview_matrix (&modelview);
 
40
 
 
41
  data = g_malloc (FRAMEBUFFER_WIDTH * 4 * FRAMEBUFFER_HEIGHT);
 
42
  tex = cogl_texture_new_from_data (FRAMEBUFFER_WIDTH, FRAMEBUFFER_HEIGHT,
 
43
                                    COGL_TEXTURE_NO_SLICING,
 
44
                                    COGL_PIXEL_FORMAT_RGBA_8888, /* data fmt */
 
45
                                    COGL_PIXEL_FORMAT_ANY, /* internal fmt */
 
46
                                    FRAMEBUFFER_WIDTH * 4, /* rowstride */
 
47
                                    data);
 
48
  g_free (data);
 
49
  offscreen = cogl_offscreen_new_to_texture (tex);
 
50
 
 
51
  /* Set a scale and translate transform on the window framebuffer before
 
52
   * switching to the offscreen framebuffer so we can verify it gets restored
 
53
   * when we switch back
 
54
   *
 
55
   * The test is going to draw a grid of 4 colors to a texture which we
 
56
   * subsequently draw to the window with a fullscreen rectangle. This
 
57
   * transform will flip the texture left to right, scale it to a quater of the
 
58
   * window size and slide it to the top right of the window.
 
59
   */
 
60
  cogl_translate (0.5, 0.5, 0);
 
61
  cogl_scale (-0.5, 0.5, 1);
 
62
 
 
63
  cogl_push_framebuffer (offscreen);
 
64
 
 
65
  /* Cogl should release the last reference when we call cogl_pop_framebuffer()
 
66
   */
 
67
  cogl_handle_unref (offscreen);
 
68
 
 
69
  /* Setup something other than the identity matrix for the modelview so we can
 
70
   * verify it gets restored when we call cogl_pop_framebuffer () */
 
71
  cogl_scale (2, 2, 1);
 
72
 
 
73
  /* red, top left */
 
74
  cogl_set_source_color4ub (0xff, 0x00, 0x00, 0xff);
 
75
  cogl_rectangle (-0.5, 0.5, 0, 0);
 
76
  /* green, top right */
 
77
  cogl_set_source_color4ub (0x00, 0xff, 0x00, 0xff);
 
78
  cogl_rectangle (0, 0.5, 0.5, 0);
 
79
  /* blue, bottom left */
 
80
  cogl_set_source_color4ub (0x00, 0x00, 0xff, 0xff);
 
81
  cogl_rectangle (-0.5, 0, 0, -0.5);
 
82
  /* white, bottom right */
 
83
  cogl_set_source_color4ub (0xff, 0xff, 0xff, 0xff);
 
84
  cogl_rectangle (0, 0, 0.5, -0.5);
 
85
 
 
86
  cogl_pop_framebuffer ();
 
87
 
 
88
  cogl_set_source_texture (tex);
 
89
  cogl_rectangle (-1, 1, 1, -1);
 
90
 
 
91
  cogl_handle_unref (tex);
 
92
 
 
93
  /* NB: The texture is drawn flipped horizontally and scaled to fit in the
 
94
   * top right corner of the window. */
 
95
 
 
96
  /* red, top right */
 
97
  cogl_read_pixels (FRAMEBUFFER_WIDTH - 1, 0, 1, 1,
 
98
                    COGL_READ_PIXELS_COLOR_BUFFER,
 
99
                    COGL_PIXEL_FORMAT_RGBA_8888_PRE,
 
100
                    pixel);
 
101
  g_assert (pixel[RED] == 0xff && pixel[GREEN] == 0x00 && pixel[BLUE] == 0x00);
 
102
 
 
103
  /* green, top left */
 
104
  cogl_read_pixels ((FRAMEBUFFER_WIDTH/2), 0, 1, 1,
 
105
                    COGL_READ_PIXELS_COLOR_BUFFER,
 
106
                    COGL_PIXEL_FORMAT_RGBA_8888_PRE,
 
107
                    pixel);
 
108
  g_assert (pixel[RED] == 0x00 && pixel[GREEN] == 0xff && pixel[BLUE] == 0x00);
 
109
 
 
110
  /* blue, bottom right */
 
111
  cogl_read_pixels (FRAMEBUFFER_WIDTH - 1, (FRAMEBUFFER_HEIGHT/2) - 1, 1, 1,
 
112
                    COGL_READ_PIXELS_COLOR_BUFFER,
 
113
                    COGL_PIXEL_FORMAT_RGBA_8888_PRE,
 
114
                    pixel);
 
115
  g_assert (pixel[RED] == 0x00 && pixel[GREEN] == 0x00 && pixel[BLUE] == 0xff);
 
116
 
 
117
  /* white, bottom left */
 
118
  cogl_read_pixels ((FRAMEBUFFER_WIDTH/2), (FRAMEBUFFER_HEIGHT/2) - 1, 1, 1,
 
119
                    COGL_READ_PIXELS_COLOR_BUFFER,
 
120
                    COGL_PIXEL_FORMAT_RGBA_8888_PRE,
 
121
                    pixel);
 
122
  g_assert (pixel[RED] == 0xff && pixel[GREEN] == 0xff && pixel[BLUE] == 0xff);
 
123
 
 
124
  /* Comment this out if you want visual feedback of what this test
 
125
   * paints.
 
126
   */
 
127
  clutter_main_quit ();
 
128
}
 
129
 
 
130
static gboolean
 
131
queue_redraw (gpointer stage)
 
132
{
 
133
  clutter_actor_queue_redraw (CLUTTER_ACTOR (stage));
 
134
 
 
135
  return TRUE;
 
136
}
 
137
 
 
138
void
 
139
test_cogl_offscreen (TestConformSimpleFixture *fixture,
 
140
                     gconstpointer data)
 
141
{
 
142
  guint idle_source;
 
143
  ClutterActor *stage;
 
144
 
 
145
  stage = clutter_stage_get_default ();
 
146
  clutter_stage_set_color (CLUTTER_STAGE (stage), &stage_color);
 
147
 
 
148
  /* We force continuous redrawing of the stage, since we need to skip
 
149
   * the first few frames, and we wont be doing anything else that
 
150
   * will trigger redrawing. */
 
151
  idle_source = g_idle_add (queue_redraw, stage);
 
152
  g_signal_connect_after (stage, "paint", G_CALLBACK (on_paint), NULL);
 
153
 
 
154
  clutter_actor_show (stage);
 
155
  clutter_main ();
 
156
 
 
157
  g_source_remove (idle_source);
 
158
 
 
159
  /* Remove all of the actors from the stage */
 
160
  clutter_container_foreach (CLUTTER_CONTAINER (stage),
 
161
                             (ClutterCallback) clutter_actor_destroy,
 
162
                             NULL);
 
163
 
 
164
  if (g_test_verbose ())
 
165
    g_print ("OK\n");
 
166
}
 
167