blob: 3bdc82dca0ab521018d1d69f0f57eb751cff8043 [file] [log] [blame]
Pekka Paalanene7c6aa62017-01-27 17:30:28 +01001/*
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"
Pekka Paalanenbabb3b32019-11-01 14:02:15 +020034#include "weston-test-fixture-compositor.h"
Pekka Paalanene7c6aa62017-01-27 17:30:28 +010035
Pekka Paalanenef813882021-02-15 13:46:42 +020036struct setup_args {
37 struct fixture_metadata meta;
38 enum renderer_type renderer;
39};
40
41static const struct setup_args my_setup_args[] = {
42 {
43 .renderer = RENDERER_PIXMAN,
44 .meta.name = "pixman"
45 },
46 {
47 .renderer = RENDERER_GL,
48 .meta.name = "GL"
49 },
Pekka Paalanenbabb3b32019-11-01 14:02:15 +020050};
51
52static enum test_result_code
Pekka Paalanenef813882021-02-15 13:46:42 +020053fixture_setup(struct weston_test_harness *harness, const struct setup_args *arg)
Pekka Paalanenbabb3b32019-11-01 14:02:15 +020054{
55 struct compositor_setup setup;
56
57 compositor_setup_defaults(&setup);
Pekka Paalanenef813882021-02-15 13:46:42 +020058 setup.renderer = arg->renderer;
Pekka Paalanenbabb3b32019-11-01 14:02:15 +020059 setup.width = 320;
60 setup.height = 240;
61 setup.shell = SHELL_TEST_DESKTOP;
62 setup.logging_scopes = "log,test-harness-plugin";
63
Pekka Paalanenbabb3b32019-11-01 14:02:15 +020064 return weston_test_harness_execute_as_client(harness, &setup);
65}
Pekka Paalanenef813882021-02-15 13:46:42 +020066DECLARE_FIXTURE_SETUP_WITH_ARG(fixture_setup, my_setup_args, meta);
Pekka Paalanene7c6aa62017-01-27 17:30:28 +010067
68static struct wl_subcompositor *
69get_subcompositor(struct client *client)
70{
71 struct global *g;
72 struct global *global_sub = NULL;
73 struct wl_subcompositor *sub;
74
75 wl_list_for_each(g, &client->global_list, link) {
76 if (strcmp(g->interface, "wl_subcompositor"))
77 continue;
78
79 if (global_sub)
80 assert(0 && "multiple wl_subcompositor objects");
81
82 global_sub = g;
83 }
84
85 assert(global_sub && "no wl_subcompositor found");
86
87 assert(global_sub->version == 1);
88
89 sub = wl_registry_bind(client->wl_registry, global_sub->name,
90 &wl_subcompositor_interface, 1);
91 assert(sub);
92
93 return sub;
94}
95
Pekka Paalanen70098062020-01-21 15:37:14 +020096static int
97check_screen(struct client *client,
98 const char *ref_image,
99 int ref_seq_no,
100 const struct rectangle *clip,
101 int seq_no)
102{
103 bool match;
104
105 match = verify_screen_content(client, ref_image, ref_seq_no, clip,
106 seq_no);
107
108 return match ? 0 : -1;
109}
110
Pekka Paalanene7c6aa62017-01-27 17:30:28 +0100111static struct buffer *
112surface_commit_color(struct client *client, struct wl_surface *surface,
113 pixman_color_t *color, int width, int height)
114{
115 struct buffer *buf;
116
117 buf = create_shm_buffer_a8r8g8b8(client, width, height);
Pekka Paalanenf26d17f2020-03-11 17:24:47 +0200118 fill_image_with_color(buf->image, color);
Pekka Paalanene7c6aa62017-01-27 17:30:28 +0100119 wl_surface_attach(surface, buf->proxy, 0, 0);
120 wl_surface_damage(surface, 0, 0, width, height);
121 wl_surface_commit(surface);
122
123 return buf;
124}
125
Emilio Pozuelo Monfort4f3cad72017-01-27 17:30:29 +0100126TEST(subsurface_z_order)
Pekka Paalanene7c6aa62017-01-27 17:30:28 +0100127{
Pekka Paalanene7c6aa62017-01-27 17:30:28 +0100128 struct client *client;
129 struct wl_subcompositor *subco;
130 struct buffer *bufs[5] = { 0 };
131 struct wl_surface *surf[5] = { 0 };
132 struct wl_subsurface *sub[5] = { 0 };
133 struct rectangle clip = { 40, 40, 280, 200 };
134 int fail = 0;
135 unsigned i;
136 pixman_color_t red;
137 pixman_color_t blue;
138 pixman_color_t cyan;
139 pixman_color_t green;
140
Pekka Paalanenf26d17f2020-03-11 17:24:47 +0200141 color_rgb888(&red, 255, 0, 0);
142 color_rgb888(&blue, 0, 0, 255);
143 color_rgb888(&cyan, 0, 255, 255);
144 color_rgb888(&green, 0, 255, 0);
Pekka Paalanene7c6aa62017-01-27 17:30:28 +0100145
146 client = create_client_and_test_surface(100, 50, 100, 100);
147 assert(client);
148 subco = get_subcompositor(client);
149
150 /* move the pointer clearly away from our screenshooting area */
Alexandros Frantzis21808582017-12-13 13:27:55 +0200151 weston_test_move_pointer(client->test->weston_test, 0, 1, 0, 2, 30);
Pekka Paalanene7c6aa62017-01-27 17:30:28 +0100152
153 /* make the parent surface red */
154 surf[0] = client->surface->wl_surface;
Pekka Paalanen9cf91212021-06-18 12:52:04 +0300155 client->surface->wl_surface = NULL; /* we stole it and destroy it */
Pekka Paalanene7c6aa62017-01-27 17:30:28 +0100156 bufs[0] = surface_commit_color(client, surf[0], &red, 100, 100);
157 /* sub[0] is not used */
158
Pekka Paalanendc9d3342019-11-15 15:48:08 +0200159 fail += check_screen(client, "subsurface_z_order", 0, &clip, 0);
Pekka Paalanene7c6aa62017-01-27 17:30:28 +0100160
161 /* create a blue sub-surface above red */
162 surf[1] = wl_compositor_create_surface(client->wl_compositor);
163 sub[1] = wl_subcompositor_get_subsurface(subco, surf[1], surf[0]);
164 bufs[1] = surface_commit_color(client, surf[1], &blue, 100, 100);
165
166 wl_subsurface_set_position(sub[1], 20, 20);
167 wl_surface_commit(surf[0]);
168
Pekka Paalanendc9d3342019-11-15 15:48:08 +0200169 fail += check_screen(client, "subsurface_z_order", 1, &clip, 1);
Pekka Paalanene7c6aa62017-01-27 17:30:28 +0100170
171 /* create a cyan sub-surface above blue */
172 surf[2] = wl_compositor_create_surface(client->wl_compositor);
173 sub[2] = wl_subcompositor_get_subsurface(subco, surf[2], surf[1]);
174 bufs[2] = surface_commit_color(client, surf[2], &cyan, 100, 100);
175
176 wl_subsurface_set_position(sub[2], 20, 20);
177 wl_surface_commit(surf[1]);
178 wl_surface_commit(surf[0]);
179
Pekka Paalanendc9d3342019-11-15 15:48:08 +0200180 fail += check_screen(client, "subsurface_z_order", 2, &clip, 2);
Pekka Paalanene7c6aa62017-01-27 17:30:28 +0100181
182 /* create a green sub-surface above blue, sibling to cyan */
183 surf[3] = wl_compositor_create_surface(client->wl_compositor);
184 sub[3] = wl_subcompositor_get_subsurface(subco, surf[3], surf[1]);
185 bufs[3] = surface_commit_color(client, surf[3], &green, 100, 100);
186
187 wl_subsurface_set_position(sub[3], -40, 10);
188 wl_surface_commit(surf[1]);
189 wl_surface_commit(surf[0]);
190
Pekka Paalanendc9d3342019-11-15 15:48:08 +0200191 fail += check_screen(client, "subsurface_z_order", 3, &clip, 3);
Pekka Paalanene7c6aa62017-01-27 17:30:28 +0100192
193 /* stack blue below red, which brings also cyan and green below red */
194 wl_subsurface_place_below(sub[1], surf[0]);
195 wl_surface_commit(surf[0]);
196
Pekka Paalanendc9d3342019-11-15 15:48:08 +0200197 fail += check_screen(client, "subsurface_z_order", 4, &clip, 4);
Pekka Paalanene7c6aa62017-01-27 17:30:28 +0100198
199 assert(fail == 0);
200
201 for (i = 0; i < ARRAY_LENGTH(sub); i++)
202 if (sub[i])
203 wl_subsurface_destroy(sub[i]);
204
205 for (i = 0; i < ARRAY_LENGTH(surf); i++)
206 if (surf[i])
207 wl_surface_destroy(surf[i]);
208
209 for (i = 0; i < ARRAY_LENGTH(bufs); i++)
210 if (bufs[i])
211 buffer_destroy(bufs[i]);
Pekka Paalanen9cf91212021-06-18 12:52:04 +0300212
213 wl_subcompositor_destroy(subco);
214 client_destroy(client);
Pekka Paalanene7c6aa62017-01-27 17:30:28 +0100215}