Pekka Paalanen | e7c6aa6 | 2017-01-27 17:30:28 +0100 | [diff] [blame] | 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" |
Pekka Paalanen | babb3b3 | 2019-11-01 14:02:15 +0200 | [diff] [blame] | 34 | #include "weston-test-fixture-compositor.h" |
Pekka Paalanen | e7c6aa6 | 2017-01-27 17:30:28 +0100 | [diff] [blame] | 35 | |
Pekka Paalanen | ef81388 | 2021-02-15 13:46:42 +0200 | [diff] [blame] | 36 | struct setup_args { |
| 37 | struct fixture_metadata meta; |
| 38 | enum renderer_type renderer; |
| 39 | }; |
| 40 | |
| 41 | static 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 Paalanen | babb3b3 | 2019-11-01 14:02:15 +0200 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | static enum test_result_code |
Pekka Paalanen | ef81388 | 2021-02-15 13:46:42 +0200 | [diff] [blame] | 53 | fixture_setup(struct weston_test_harness *harness, const struct setup_args *arg) |
Pekka Paalanen | babb3b3 | 2019-11-01 14:02:15 +0200 | [diff] [blame] | 54 | { |
| 55 | struct compositor_setup setup; |
| 56 | |
| 57 | compositor_setup_defaults(&setup); |
Pekka Paalanen | ef81388 | 2021-02-15 13:46:42 +0200 | [diff] [blame] | 58 | setup.renderer = arg->renderer; |
Pekka Paalanen | babb3b3 | 2019-11-01 14:02:15 +0200 | [diff] [blame] | 59 | setup.width = 320; |
| 60 | setup.height = 240; |
| 61 | setup.shell = SHELL_TEST_DESKTOP; |
| 62 | setup.logging_scopes = "log,test-harness-plugin"; |
| 63 | |
Pekka Paalanen | babb3b3 | 2019-11-01 14:02:15 +0200 | [diff] [blame] | 64 | return weston_test_harness_execute_as_client(harness, &setup); |
| 65 | } |
Pekka Paalanen | ef81388 | 2021-02-15 13:46:42 +0200 | [diff] [blame] | 66 | DECLARE_FIXTURE_SETUP_WITH_ARG(fixture_setup, my_setup_args, meta); |
Pekka Paalanen | e7c6aa6 | 2017-01-27 17:30:28 +0100 | [diff] [blame] | 67 | |
| 68 | static struct wl_subcompositor * |
| 69 | get_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 Paalanen | 7009806 | 2020-01-21 15:37:14 +0200 | [diff] [blame] | 96 | static int |
| 97 | check_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 Paalanen | e7c6aa6 | 2017-01-27 17:30:28 +0100 | [diff] [blame] | 111 | static struct buffer * |
| 112 | surface_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 Paalanen | f26d17f | 2020-03-11 17:24:47 +0200 | [diff] [blame] | 118 | fill_image_with_color(buf->image, color); |
Pekka Paalanen | e7c6aa6 | 2017-01-27 17:30:28 +0100 | [diff] [blame] | 119 | 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 Monfort | 4f3cad7 | 2017-01-27 17:30:29 +0100 | [diff] [blame] | 126 | TEST(subsurface_z_order) |
Pekka Paalanen | e7c6aa6 | 2017-01-27 17:30:28 +0100 | [diff] [blame] | 127 | { |
Pekka Paalanen | e7c6aa6 | 2017-01-27 17:30:28 +0100 | [diff] [blame] | 128 | 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 Paalanen | f26d17f | 2020-03-11 17:24:47 +0200 | [diff] [blame] | 141 | 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 Paalanen | e7c6aa6 | 2017-01-27 17:30:28 +0100 | [diff] [blame] | 145 | |
| 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 Frantzis | 2180858 | 2017-12-13 13:27:55 +0200 | [diff] [blame] | 151 | weston_test_move_pointer(client->test->weston_test, 0, 1, 0, 2, 30); |
Pekka Paalanen | e7c6aa6 | 2017-01-27 17:30:28 +0100 | [diff] [blame] | 152 | |
| 153 | /* make the parent surface red */ |
| 154 | surf[0] = client->surface->wl_surface; |
Pekka Paalanen | 9cf9121 | 2021-06-18 12:52:04 +0300 | [diff] [blame] | 155 | client->surface->wl_surface = NULL; /* we stole it and destroy it */ |
Pekka Paalanen | e7c6aa6 | 2017-01-27 17:30:28 +0100 | [diff] [blame] | 156 | bufs[0] = surface_commit_color(client, surf[0], &red, 100, 100); |
| 157 | /* sub[0] is not used */ |
| 158 | |
Pekka Paalanen | dc9d334 | 2019-11-15 15:48:08 +0200 | [diff] [blame] | 159 | fail += check_screen(client, "subsurface_z_order", 0, &clip, 0); |
Pekka Paalanen | e7c6aa6 | 2017-01-27 17:30:28 +0100 | [diff] [blame] | 160 | |
| 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 Paalanen | dc9d334 | 2019-11-15 15:48:08 +0200 | [diff] [blame] | 169 | fail += check_screen(client, "subsurface_z_order", 1, &clip, 1); |
Pekka Paalanen | e7c6aa6 | 2017-01-27 17:30:28 +0100 | [diff] [blame] | 170 | |
| 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 Paalanen | dc9d334 | 2019-11-15 15:48:08 +0200 | [diff] [blame] | 180 | fail += check_screen(client, "subsurface_z_order", 2, &clip, 2); |
Pekka Paalanen | e7c6aa6 | 2017-01-27 17:30:28 +0100 | [diff] [blame] | 181 | |
| 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 Paalanen | dc9d334 | 2019-11-15 15:48:08 +0200 | [diff] [blame] | 191 | fail += check_screen(client, "subsurface_z_order", 3, &clip, 3); |
Pekka Paalanen | e7c6aa6 | 2017-01-27 17:30:28 +0100 | [diff] [blame] | 192 | |
| 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 Paalanen | dc9d334 | 2019-11-15 15:48:08 +0200 | [diff] [blame] | 197 | fail += check_screen(client, "subsurface_z_order", 4, &clip, 4); |
Pekka Paalanen | e7c6aa6 | 2017-01-27 17:30:28 +0100 | [diff] [blame] | 198 | |
| 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 Paalanen | 9cf9121 | 2021-06-18 12:52:04 +0300 | [diff] [blame] | 212 | |
| 213 | wl_subcompositor_destroy(subco); |
| 214 | client_destroy(client); |
Pekka Paalanen | e7c6aa6 | 2017-01-27 17:30:28 +0100 | [diff] [blame] | 215 | } |