tests: add test_seat_release() for symmetry
[platform/upstream/weston.git] / tests / subsurface-shot-test.c
1 /*
2  * Copyright © 2015 Samsung Electronics Co., Ltd
3  * Copyright © 2016 Collabora, Ltd.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the
14  * next paragraph) shall be included in all copies or substantial
15  * portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24  * SOFTWARE.
25  */
26
27 #include "config.h"
28
29 #include <stdio.h>
30 #include <string.h>
31 #include <sys/mman.h>
32
33 #include "weston-test-client-helper.h"
34
35 char *server_parameters = "--use-pixman --width=320 --height=240"
36         " --shell=weston-test-desktop-shell.so";
37
38 static struct wl_subcompositor *
39 get_subcompositor(struct client *client)
40 {
41         struct global *g;
42         struct global *global_sub = NULL;
43         struct wl_subcompositor *sub;
44
45         wl_list_for_each(g, &client->global_list, link) {
46                 if (strcmp(g->interface, "wl_subcompositor"))
47                         continue;
48
49                 if (global_sub)
50                         assert(0 && "multiple wl_subcompositor objects");
51
52                 global_sub = g;
53         }
54
55         assert(global_sub && "no wl_subcompositor found");
56
57         assert(global_sub->version == 1);
58
59         sub = wl_registry_bind(client->wl_registry, global_sub->name,
60                                &wl_subcompositor_interface, 1);
61         assert(sub);
62
63         return sub;
64 }
65
66 static void
67 fill_color(pixman_image_t *image, pixman_color_t *color)
68 {
69         pixman_image_t *solid;
70         int width;
71         int height;
72
73         width = pixman_image_get_width(image);
74         height = pixman_image_get_height(image);
75
76         solid = pixman_image_create_solid_fill(color);
77         pixman_image_composite32(PIXMAN_OP_SRC,
78                                  solid, /* src */
79                                  NULL, /* mask */
80                                  image, /* dst */
81                                  0, 0, /* src x,y */
82                                  0, 0, /* mask x,y */
83                                  0, 0, /* dst x,y */
84                                  width, height);
85         pixman_image_unref(solid);
86 }
87
88 static pixman_color_t *
89 color(pixman_color_t *tmp, uint8_t r, uint8_t g, uint8_t b)
90 {
91         tmp->alpha = 65535;
92         tmp->red = (r << 8) + r;
93         tmp->green = (g << 8) + g;
94         tmp->blue = (b << 8) + b;
95
96         return tmp;
97 }
98
99 static void
100 write_visual_diff(pixman_image_t *ref_image,
101                   struct buffer *shot,
102                   const struct rectangle *clip,
103                   const char *test_name,
104                   int seq_no)
105 {
106         char *fname;
107         char *ext_test_name;
108         pixman_image_t *diff;
109         int ret;
110
111         ret = asprintf(&ext_test_name, "%s-diff", test_name);
112         assert(ret >= 0);
113
114         fname = screenshot_output_filename(ext_test_name, seq_no);
115         diff = visualize_image_difference(shot->image, ref_image, clip);
116         write_image_as_png(diff, fname);
117
118         pixman_image_unref(diff);
119         free(fname);
120         free(ext_test_name);
121 }
122
123 static int
124 check_screen(struct client *client,
125              const char *ref_image,
126              int ref_seq_no,
127              const struct rectangle *clip,
128              int seq_no)
129 {
130         const char *test_name = get_test_name();
131         struct buffer *shot;
132         pixman_image_t *ref;
133         char *ref_fname;
134         char *shot_fname;
135         bool match;
136
137         ref_fname = screenshot_reference_filename(ref_image, ref_seq_no);
138         shot_fname = screenshot_output_filename(test_name, seq_no);
139
140         ref = load_image_from_png(ref_fname);
141         assert(ref);
142
143         shot = capture_screenshot_of_output(client);
144         assert(shot);
145
146         match = check_images_match(shot->image, ref, clip);
147         printf("ref %s vs. shot %s: %s\n", ref_fname, shot_fname,
148                match ? "PASS" : "FAIL");
149
150         write_image_as_png(shot->image, shot_fname);
151         if (!match)
152                 write_visual_diff(ref, shot, clip, test_name, seq_no);
153
154         buffer_destroy(shot);
155         pixman_image_unref(ref);
156         free(ref_fname);
157         free(shot_fname);
158
159         return match ? 0 : -1;
160 }
161
162 static struct buffer *
163 surface_commit_color(struct client *client, struct wl_surface *surface,
164                      pixman_color_t *color, int width, int height)
165 {
166         struct buffer *buf;
167
168         buf = create_shm_buffer_a8r8g8b8(client, width, height);
169         fill_color(buf->image, color);
170         wl_surface_attach(surface, buf->proxy, 0, 0);
171         wl_surface_damage(surface, 0, 0, width, height);
172         wl_surface_commit(surface);
173
174         return buf;
175 }
176
177 TEST(subsurface_z_order)
178 {
179         const char *test_name = get_test_name();
180         struct client *client;
181         struct wl_subcompositor *subco;
182         struct buffer *bufs[5] = { 0 };
183         struct wl_surface *surf[5] = { 0 };
184         struct wl_subsurface *sub[5] = { 0 };
185         struct rectangle clip = { 40, 40, 280, 200 };
186         int fail = 0;
187         unsigned i;
188         pixman_color_t red;
189         pixman_color_t blue;
190         pixman_color_t cyan;
191         pixman_color_t green;
192
193         color(&red, 255, 0, 0);
194         color(&blue, 0, 0, 255);
195         color(&cyan, 0, 255, 255);
196         color(&green, 0, 255, 0);
197
198         client = create_client_and_test_surface(100, 50, 100, 100);
199         assert(client);
200         subco = get_subcompositor(client);
201
202         /* move the pointer clearly away from our screenshooting area */
203         weston_test_move_pointer(client->test->weston_test, 0, 1, 0, 2, 30);
204
205         /* make the parent surface red */
206         surf[0] = client->surface->wl_surface;
207         bufs[0] = surface_commit_color(client, surf[0], &red, 100, 100);
208         /* sub[0] is not used */
209
210         fail += check_screen(client, test_name, 0, &clip, 0);
211
212         /* create a blue sub-surface above red */
213         surf[1] = wl_compositor_create_surface(client->wl_compositor);
214         sub[1] = wl_subcompositor_get_subsurface(subco, surf[1], surf[0]);
215         bufs[1] = surface_commit_color(client, surf[1], &blue, 100, 100);
216
217         wl_subsurface_set_position(sub[1], 20, 20);
218         wl_surface_commit(surf[0]);
219
220         fail += check_screen(client, test_name, 1, &clip, 1);
221
222         /* create a cyan sub-surface above blue */
223         surf[2] = wl_compositor_create_surface(client->wl_compositor);
224         sub[2] = wl_subcompositor_get_subsurface(subco, surf[2], surf[1]);
225         bufs[2] = surface_commit_color(client, surf[2], &cyan, 100, 100);
226
227         wl_subsurface_set_position(sub[2], 20, 20);
228         wl_surface_commit(surf[1]);
229         wl_surface_commit(surf[0]);
230
231         fail += check_screen(client, test_name, 2, &clip, 2);
232
233         /* create a green sub-surface above blue, sibling to cyan */
234         surf[3] = wl_compositor_create_surface(client->wl_compositor);
235         sub[3] = wl_subcompositor_get_subsurface(subco, surf[3], surf[1]);
236         bufs[3] = surface_commit_color(client, surf[3], &green, 100, 100);
237
238         wl_subsurface_set_position(sub[3], -40, 10);
239         wl_surface_commit(surf[1]);
240         wl_surface_commit(surf[0]);
241
242         fail += check_screen(client, test_name, 3, &clip, 3);
243
244         /* stack blue below red, which brings also cyan and green below red */
245         wl_subsurface_place_below(sub[1], surf[0]);
246         wl_surface_commit(surf[0]);
247
248         fail += check_screen(client, test_name, 4, &clip, 4);
249
250         assert(fail == 0);
251
252         for (i = 0; i < ARRAY_LENGTH(sub); i++)
253                 if (sub[i])
254                         wl_subsurface_destroy(sub[i]);
255
256         for (i = 0; i < ARRAY_LENGTH(surf); i++)
257                 if (surf[i])
258                         wl_surface_destroy(surf[i]);
259
260         for (i = 0; i < ARRAY_LENGTH(bufs); i++)
261                 if (bufs[i])
262                         buffer_destroy(bufs[i]);
263 }