1 #include <cogl/cogl.h>
2 
3 #include <string.h>
4 
5 #include "test-utils.h"
6 
7 /* Non-power-of-two sized texture that should cause slicing */
8 #define TEXTURE_SIZE        384
9 /* Number of times to split the texture up on each axis */
10 #define PARTS               2
11 /* The texture is split into four parts, each with a different colour */
12 #define PART_SIZE           (TEXTURE_SIZE / PARTS)
13 
14 /* Amount of pixels to skip off the top, bottom, left and right of the
15    texture when reading back the stage */
16 #define TEST_INSET          4
17 
18 /* Size to actually render the texture at */
19 #define TEXTURE_RENDER_SIZE TEXTURE_SIZE
20 /* The size of a part once rendered */
21 #define PART_RENDER_SIZE    (TEXTURE_RENDER_SIZE / PARTS)
22 
23 static const uint32_t corner_colors[PARTS * PARTS] =
24   {
25     /* Top left     - red */    0xff0000ff,
26     /* Top right    - green */  0x00ff00ff,
27     /* Bottom left  - blue */   0x0000ffff,
28     /* Bottom right - yellow */ 0xffff00ff
29   };
30 
31 static void
validate_part(int xnum,int ynum,uint32_t color)32 validate_part (int xnum,
33                int ynum,
34                uint32_t color)
35 {
36   test_utils_check_region (test_fb,
37                            xnum * PART_RENDER_SIZE + TEST_INSET,
38                            ynum * PART_RENDER_SIZE + TEST_INSET,
39                            PART_RENDER_SIZE - TEST_INSET * 2,
40                            PART_RENDER_SIZE - TEST_INSET * 2,
41                            color);
42 }
43 
44 static void
validate_result(void)45 validate_result (void)
46 {
47   /* Validate that all four corners of the texture are drawn in the
48      right color */
49   validate_part (0, 0, corner_colors[0]);
50   validate_part (1, 0, corner_colors[1]);
51   validate_part (0, 1, corner_colors[2]);
52   validate_part (1, 1, corner_colors[3]);
53 }
54 
55 static CoglTexture *
make_texture(void)56 make_texture (void)
57 {
58   void *tex_data;
59   uint32_t *p;
60   CoglTexture *tex;
61   int partx, party, width, height;
62 
63   p = tex_data = malloc (TEXTURE_SIZE * TEXTURE_SIZE * 4);
64 
65   /* Make a texture with a different color for each part */
66   for (party = 0; party < PARTS; party++)
67     {
68       height = (party < PARTS - 1
69                 ? PART_SIZE
70                 : TEXTURE_SIZE - PART_SIZE * (PARTS - 1));
71 
72       for (partx = 0; partx < PARTS; partx++)
73         {
74           uint32_t color = corner_colors[party * PARTS + partx];
75           width = (partx < PARTS - 1
76                    ? PART_SIZE
77                    : TEXTURE_SIZE - PART_SIZE * (PARTS - 1));
78 
79           while (width-- > 0)
80             *(p++) = GUINT32_TO_BE (color);
81         }
82 
83       while (--height > 0)
84         {
85           memcpy (p, p - TEXTURE_SIZE, TEXTURE_SIZE * 4);
86           p += TEXTURE_SIZE;
87         }
88     }
89 
90   tex = test_utils_texture_new_from_data (test_ctx,
91                                           TEXTURE_SIZE,
92                                           TEXTURE_SIZE,
93                                           TEST_UTILS_TEXTURE_NO_ATLAS,
94                                           COGL_PIXEL_FORMAT_RGBA_8888_PRE,
95                                           TEXTURE_SIZE * 4,
96                                           tex_data);
97 
98   free (tex_data);
99 
100   if (cogl_test_verbose ())
101     {
102       if (cogl_texture_is_sliced (tex))
103         g_print ("Texture is sliced\n");
104       else
105         g_print ("Texture is not sliced\n");
106     }
107 
108   /* The texture should be sliced unless NPOTs are supported */
109   g_assert (cogl_has_feature (test_ctx, COGL_FEATURE_ID_TEXTURE_NPOT)
110             ? !cogl_texture_is_sliced (tex)
111             : cogl_texture_is_sliced (tex));
112 
113   return tex;
114 }
115 
116 static void
paint(void)117 paint (void)
118 {
119   CoglPipeline *pipeline = cogl_pipeline_new (test_ctx);
120   CoglTexture *texture = make_texture ();
121   int y, x;
122 
123   cogl_pipeline_set_layer_texture (pipeline, 0, texture);
124 
125   /* Just render the texture in the top left corner */
126   /* Render the texture using four separate rectangles */
127   for (y = 0; y < 2; y++)
128     for (x = 0; x < 2; x++)
129       cogl_framebuffer_draw_textured_rectangle (test_fb,
130                                                 pipeline,
131                                                 x * TEXTURE_RENDER_SIZE / 2,
132                                                 y * TEXTURE_RENDER_SIZE / 2,
133                                                 (x + 1) *
134                                                 TEXTURE_RENDER_SIZE / 2,
135                                                 (y + 1) *
136                                                 TEXTURE_RENDER_SIZE / 2,
137                                                 x / 2.0f,
138                                                 y / 2.0f,
139                                                 (x + 1) / 2.0f,
140                                                 (y + 1) / 2.0f);
141 
142   cogl_object_unref (pipeline);
143   cogl_object_unref (texture);
144 }
145 
146 void
test_npot_texture(void)147 test_npot_texture (void)
148 {
149   if (cogl_test_verbose ())
150     {
151       if (cogl_has_feature (test_ctx, COGL_FEATURE_ID_TEXTURE_NPOT))
152         g_print ("NPOT textures are supported\n");
153       else
154         g_print ("NPOT textures are not supported\n");
155     }
156 
157   cogl_framebuffer_orthographic (test_fb,
158                                  0, 0,
159                                  cogl_framebuffer_get_width (test_fb),
160                                  cogl_framebuffer_get_height (test_fb),
161                                  -1,
162                                  100);
163 
164   paint ();
165   validate_result ();
166 
167   if (cogl_test_verbose ())
168     g_print ("OK\n");
169 }
170 
171