| 1 |
#include <clutter/clutter.h> |
| 2 |
#include <cogl/cogl.h> |
| 3 |
|
| 4 |
#include "test-conform-common.h" |
| 5 |
|
| 6 |
#define SOURCE_SIZE 32 |
| 7 |
#define SOURCE_DIVISIONS_X 2 |
| 8 |
#define SOURCE_DIVISIONS_Y 2 |
| 9 |
#define DIVISION_WIDTH (SOURCE_SIZE / SOURCE_DIVISIONS_X) |
| 10 |
#define DIVISION_HEIGHT (SOURCE_SIZE / SOURCE_DIVISIONS_Y) |
| 11 |
|
| 12 |
static const ClutterColor |
| 13 |
corner_colors[SOURCE_DIVISIONS_X * SOURCE_DIVISIONS_Y] = |
| 14 |
{ |
| 15 |
{ 0xff, 0x00, 0x00, 0xff }, /* red top left */ |
| 16 |
{ 0x00, 0xff, 0x00, 0xff }, /* green top right */ |
| 17 |
{ 0x00, 0x00, 0xff, 0xff }, /* blue bottom left */ |
| 18 |
{ 0xff, 0x00, 0xff, 0xff } /* purple bottom right */ |
| 19 |
}; |
| 20 |
|
| 21 |
static const ClutterColor stage_color = { 0x0, 0x0, 0x0, 0xff }; |
| 22 |
|
| 23 |
typedef struct _TestState |
| 24 |
{ |
| 25 |
ClutterActor *stage; |
| 26 |
guint frame; |
| 27 |
gboolean was_painted; |
| 28 |
} TestState; |
| 29 |
|
| 30 |
static ClutterActor * |
| 31 |
create_source (void) |
| 32 |
{ |
| 33 |
int x, y; |
| 34 |
ClutterActor *group = clutter_group_new (); |
| 35 |
|
| 36 |
/* Create a group with a different coloured rectangle at each |
| 37 |
corner */ |
| 38 |
for (y = 0; y < SOURCE_DIVISIONS_Y; y++) |
| 39 |
for (x = 0; x < SOURCE_DIVISIONS_X; x++) |
| 40 |
{ |
| 41 |
ClutterActor *rect = clutter_rectangle_new (); |
| 42 |
clutter_actor_set_size (rect, DIVISION_WIDTH, DIVISION_HEIGHT); |
| 43 |
clutter_actor_set_position (rect, |
| 44 |
DIVISION_WIDTH * x, |
| 45 |
DIVISION_HEIGHT * y); |
| 46 |
clutter_rectangle_set_color (CLUTTER_RECTANGLE (rect), |
| 47 |
corner_colors + |
| 48 |
(y * SOURCE_DIVISIONS_X + x)); |
| 49 |
clutter_container_add (CLUTTER_CONTAINER (group), rect, NULL); |
| 50 |
} |
| 51 |
|
| 52 |
return group; |
| 53 |
} |
| 54 |
|
| 55 |
static void |
| 56 |
pre_paint_clip_cb (void) |
| 57 |
{ |
| 58 |
/* Generate a clip path that clips out the top left division */ |
| 59 |
cogl_path_move_to (DIVISION_WIDTH, 0); |
| 60 |
cogl_path_line_to (SOURCE_SIZE, 0); |
| 61 |
cogl_path_line_to (SOURCE_SIZE, SOURCE_SIZE); |
| 62 |
cogl_path_line_to (0, SOURCE_SIZE); |
| 63 |
cogl_path_line_to (0, DIVISION_HEIGHT); |
| 64 |
cogl_path_line_to (DIVISION_WIDTH, DIVISION_HEIGHT); |
| 65 |
cogl_path_close (); |
| 66 |
cogl_clip_push_from_path (); |
| 67 |
} |
| 68 |
|
| 69 |
static void |
| 70 |
post_paint_clip_cb (void) |
| 71 |
{ |
| 72 |
cogl_clip_pop (); |
| 73 |
} |
| 74 |
|
| 75 |
static void |
| 76 |
validate_part (TestState *state, |
| 77 |
int xpos, int ypos, |
| 78 |
int clip_flags) |
| 79 |
{ |
| 80 |
int x, y; |
| 81 |
|
| 82 |
/* Check whether the center of each division is the right color */ |
| 83 |
for (y = 0; y < SOURCE_DIVISIONS_Y; y++) |
| 84 |
for (x = 0; x < SOURCE_DIVISIONS_X; x++) |
| 85 |
{ |
| 86 |
guchar *pixels; |
| 87 |
const ClutterColor *correct_color; |
| 88 |
|
| 89 |
/* Read the center pixels of this division */ |
| 90 |
pixels = clutter_stage_read_pixels (CLUTTER_STAGE (state->stage), |
| 91 |
x * DIVISION_WIDTH + |
| 92 |
DIVISION_WIDTH / 2 + xpos, |
| 93 |
y * DIVISION_HEIGHT + |
| 94 |
DIVISION_HEIGHT / 2 + ypos, |
| 95 |
1, 1); |
| 96 |
|
| 97 |
/* If this division is clipped then it should be the stage |
| 98 |
color */ |
| 99 |
if ((clip_flags & (1 << ((y * SOURCE_DIVISIONS_X) + x)))) |
| 100 |
correct_color = &stage_color; |
| 101 |
else |
| 102 |
/* Otherwise it should be the color for this division */ |
| 103 |
correct_color = corner_colors + (y * SOURCE_DIVISIONS_X) + x; |
| 104 |
|
| 105 |
g_assert (pixels != NULL); |
| 106 |
g_assert_cmpint (pixels[0], ==, correct_color->red); |
| 107 |
g_assert_cmpint (pixels[1], ==, correct_color->green); |
| 108 |
g_assert_cmpint (pixels[2], ==, correct_color->blue); |
| 109 |
|
| 110 |
g_free (pixels); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
static void |
| 115 |
validate_result (TestState *state) |
| 116 |
{ |
| 117 |
int ypos = 0; |
| 118 |
|
| 119 |
if (g_test_verbose ()) |
| 120 |
g_print ("Testing onscreen clone...\n"); |
| 121 |
validate_part (state, SOURCE_SIZE, ypos * SOURCE_SIZE, 0); |
| 122 |
ypos++; |
| 123 |
|
| 124 |
#if 0 /* this doesn't work */ |
| 125 |
if (g_test_verbose ()) |
| 126 |
g_print ("Testing offscreen clone...\n"); |
| 127 |
validate_part (state, SOURCE_SIZE, ypos * SOURCE_SIZE, 0); |
| 128 |
#endif |
| 129 |
ypos++; |
| 130 |
|
| 131 |
if (g_test_verbose ()) |
| 132 |
g_print ("Testing onscreen clone with rectangular clip...\n"); |
| 133 |
validate_part (state, SOURCE_SIZE, ypos * SOURCE_SIZE, ~1); |
| 134 |
ypos++; |
| 135 |
|
| 136 |
if (g_test_verbose ()) |
| 137 |
g_print ("Testing onscreen clone with path clip...\n"); |
| 138 |
validate_part (state, SOURCE_SIZE, ypos * SOURCE_SIZE, 1); |
| 139 |
ypos++; |
| 140 |
} |
| 141 |
|
| 142 |
static gboolean |
| 143 |
on_paint (gpointer data) |
| 144 |
{ |
| 145 |
TestState *state = data; |
| 146 |
int frame_num; |
| 147 |
|
| 148 |
/* XXX: validate_result calls clutter_stage_read_pixels which will result in |
| 149 |
* another paint run so to avoid infinite recursion we only aim to validate |
| 150 |
* the first frame. */ |
| 151 |
frame_num = state->frame++; |
| 152 |
if (frame_num == 1) |
| 153 |
validate_result (state); |
| 154 |
|
| 155 |
state->was_painted = TRUE; |
| 156 |
|
| 157 |
return G_SOURCE_REMOVE; |
| 158 |
} |
| 159 |
|
| 160 |
void |
| 161 |
texture_fbo (TestConformSimpleFixture *fixture, |
| 162 |
gconstpointer data) |
| 163 |
{ |
| 164 |
TestState state; |
| 165 |
ClutterActor *actor; |
| 166 |
int ypos = 0; |
| 167 |
|
| 168 |
if (!cogl_features_available (COGL_FEATURE_OFFSCREEN)) |
| 169 |
{ |
| 170 |
if (g_test_verbose ()) |
| 171 |
g_print ("Offscreen buffers are not available, skipping.\n"); |
| 172 |
|
| 173 |
return; |
| 174 |
} |
| 175 |
|
| 176 |
state.frame = 0; |
| 177 |
|
| 178 |
state.stage = clutter_stage_new (); |
| 179 |
|
| 180 |
clutter_stage_set_color (CLUTTER_STAGE (state.stage), &stage_color); |
| 181 |
|
| 182 |
/* Onscreen source with clone next to it */ |
| 183 |
actor = create_source (); |
| 184 |
clutter_container_add (CLUTTER_CONTAINER (state.stage), actor, NULL); |
| 185 |
clutter_actor_set_position (actor, 0, ypos * SOURCE_SIZE); |
| 186 |
actor = clutter_texture_new_from_actor (actor); |
| 187 |
clutter_actor_set_position (actor, SOURCE_SIZE, ypos * SOURCE_SIZE); |
| 188 |
clutter_container_add (CLUTTER_CONTAINER (state.stage), actor, NULL); |
| 189 |
ypos++; |
| 190 |
|
| 191 |
/* Offscreen source with clone */ |
| 192 |
#if 0 /* this doesn't work */ |
| 193 |
actor = create_source (); |
| 194 |
actor = clutter_texture_new_from_actor (actor); |
| 195 |
clutter_actor_set_position (actor, SOURCE_SIZE, ypos * SOURCE_SIZE); |
| 196 |
clutter_container_add (CLUTTER_CONTAINER (state.stage), actor, NULL); |
| 197 |
#endif |
| 198 |
ypos++; |
| 199 |
|
| 200 |
/* Source clipped to the top left division */ |
| 201 |
actor = create_source (); |
| 202 |
clutter_container_add (CLUTTER_CONTAINER (state.stage), actor, NULL); |
| 203 |
clutter_actor_set_position (actor, 0, ypos * SOURCE_SIZE); |
| 204 |
clutter_actor_set_clip (actor, 0, 0, DIVISION_WIDTH, DIVISION_HEIGHT); |
| 205 |
actor = clutter_texture_new_from_actor (actor); |
| 206 |
clutter_actor_set_position (actor, SOURCE_SIZE, ypos * SOURCE_SIZE); |
| 207 |
clutter_container_add (CLUTTER_CONTAINER (state.stage), actor, NULL); |
| 208 |
ypos++; |
| 209 |
|
| 210 |
/* Source clipped to everything but top left division using a |
| 211 |
path */ |
| 212 |
actor = create_source (); |
| 213 |
clutter_container_add (CLUTTER_CONTAINER (state.stage), actor, NULL); |
| 214 |
clutter_actor_set_position (actor, 0, ypos * SOURCE_SIZE); |
| 215 |
g_signal_connect (actor, "paint", |
| 216 |
G_CALLBACK (pre_paint_clip_cb), NULL); |
| 217 |
g_signal_connect_after (actor, "paint", |
| 218 |
G_CALLBACK (post_paint_clip_cb), NULL); |
| 219 |
actor = clutter_texture_new_from_actor (actor); |
| 220 |
clutter_actor_set_position (actor, SOURCE_SIZE, ypos * SOURCE_SIZE); |
| 221 |
clutter_container_add (CLUTTER_CONTAINER (state.stage), actor, NULL); |
| 222 |
ypos++; |
| 223 |
|
| 224 |
clutter_actor_show (state.stage); |
| 225 |
|
| 226 |
clutter_threads_add_repaint_func_full (CLUTTER_REPAINT_FLAGS_POST_PAINT, |
| 227 |
on_paint, |
| 228 |
&state, |
| 229 |
NULL); |
| 230 |
|
| 231 |
while (!state.was_painted) |
| 232 |
g_main_context_iteration (NULL, FALSE); |
| 233 |
|
| 234 |
clutter_actor_destroy (state.stage); |
| 235 |
} |