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 | babb3b3 | 2019-11-01 14:02:15 +0200 | [diff] [blame] | 36 | static const enum renderer_type renderers[] = { |
| 37 | RENDERER_PIXMAN, |
| 38 | RENDERER_GL, |
| 39 | }; |
| 40 | |
| 41 | static enum test_result_code |
| 42 | fixture_setup(struct weston_test_harness *harness, const enum renderer_type *arg) |
| 43 | { |
| 44 | struct compositor_setup setup; |
| 45 | |
| 46 | compositor_setup_defaults(&setup); |
| 47 | setup.renderer = *arg; |
| 48 | setup.width = 320; |
| 49 | setup.height = 240; |
| 50 | setup.shell = SHELL_TEST_DESKTOP; |
| 51 | setup.logging_scopes = "log,test-harness-plugin"; |
| 52 | |
Pekka Paalanen | babb3b3 | 2019-11-01 14:02:15 +0200 | [diff] [blame] | 53 | return weston_test_harness_execute_as_client(harness, &setup); |
| 54 | } |
| 55 | DECLARE_FIXTURE_SETUP_WITH_ARG(fixture_setup, renderers); |
Pekka Paalanen | e7c6aa6 | 2017-01-27 17:30:28 +0100 | [diff] [blame] | 56 | |
| 57 | static struct wl_subcompositor * |
| 58 | get_subcompositor(struct client *client) |
| 59 | { |
| 60 | struct global *g; |
| 61 | struct global *global_sub = NULL; |
| 62 | struct wl_subcompositor *sub; |
| 63 | |
| 64 | wl_list_for_each(g, &client->global_list, link) { |
| 65 | if (strcmp(g->interface, "wl_subcompositor")) |
| 66 | continue; |
| 67 | |
| 68 | if (global_sub) |
| 69 | assert(0 && "multiple wl_subcompositor objects"); |
| 70 | |
| 71 | global_sub = g; |
| 72 | } |
| 73 | |
| 74 | assert(global_sub && "no wl_subcompositor found"); |
| 75 | |
| 76 | assert(global_sub->version == 1); |
| 77 | |
| 78 | sub = wl_registry_bind(client->wl_registry, global_sub->name, |
| 79 | &wl_subcompositor_interface, 1); |
| 80 | assert(sub); |
| 81 | |
| 82 | return sub; |
| 83 | } |
| 84 | |
| 85 | static void |
| 86 | fill_color(pixman_image_t *image, pixman_color_t *color) |
| 87 | { |
| 88 | pixman_image_t *solid; |
| 89 | int width; |
| 90 | int height; |
| 91 | |
| 92 | width = pixman_image_get_width(image); |
| 93 | height = pixman_image_get_height(image); |
| 94 | |
| 95 | solid = pixman_image_create_solid_fill(color); |
| 96 | pixman_image_composite32(PIXMAN_OP_SRC, |
| 97 | solid, /* src */ |
| 98 | NULL, /* mask */ |
| 99 | image, /* dst */ |
| 100 | 0, 0, /* src x,y */ |
| 101 | 0, 0, /* mask x,y */ |
| 102 | 0, 0, /* dst x,y */ |
| 103 | width, height); |
| 104 | pixman_image_unref(solid); |
| 105 | } |
| 106 | |
| 107 | static pixman_color_t * |
| 108 | color(pixman_color_t *tmp, uint8_t r, uint8_t g, uint8_t b) |
| 109 | { |
| 110 | tmp->alpha = 65535; |
| 111 | tmp->red = (r << 8) + r; |
| 112 | tmp->green = (g << 8) + g; |
| 113 | tmp->blue = (b << 8) + b; |
| 114 | |
| 115 | return tmp; |
| 116 | } |
| 117 | |
Pekka Paalanen | 7009806 | 2020-01-21 15:37:14 +0200 | [diff] [blame] | 118 | static int |
| 119 | check_screen(struct client *client, |
| 120 | const char *ref_image, |
| 121 | int ref_seq_no, |
| 122 | const struct rectangle *clip, |
| 123 | int seq_no) |
| 124 | { |
| 125 | bool match; |
| 126 | |
| 127 | match = verify_screen_content(client, ref_image, ref_seq_no, clip, |
| 128 | seq_no); |
| 129 | |
| 130 | return match ? 0 : -1; |
| 131 | } |
| 132 | |
Pekka Paalanen | e7c6aa6 | 2017-01-27 17:30:28 +0100 | [diff] [blame] | 133 | static struct buffer * |
| 134 | surface_commit_color(struct client *client, struct wl_surface *surface, |
| 135 | pixman_color_t *color, int width, int height) |
| 136 | { |
| 137 | struct buffer *buf; |
| 138 | |
| 139 | buf = create_shm_buffer_a8r8g8b8(client, width, height); |
| 140 | fill_color(buf->image, color); |
| 141 | wl_surface_attach(surface, buf->proxy, 0, 0); |
| 142 | wl_surface_damage(surface, 0, 0, width, height); |
| 143 | wl_surface_commit(surface); |
| 144 | |
| 145 | return buf; |
| 146 | } |
| 147 | |
Emilio Pozuelo Monfort | 4f3cad7 | 2017-01-27 17:30:29 +0100 | [diff] [blame] | 148 | TEST(subsurface_z_order) |
Pekka Paalanen | e7c6aa6 | 2017-01-27 17:30:28 +0100 | [diff] [blame] | 149 | { |
Pekka Paalanen | e7c6aa6 | 2017-01-27 17:30:28 +0100 | [diff] [blame] | 150 | struct client *client; |
| 151 | struct wl_subcompositor *subco; |
| 152 | struct buffer *bufs[5] = { 0 }; |
| 153 | struct wl_surface *surf[5] = { 0 }; |
| 154 | struct wl_subsurface *sub[5] = { 0 }; |
| 155 | struct rectangle clip = { 40, 40, 280, 200 }; |
| 156 | int fail = 0; |
| 157 | unsigned i; |
| 158 | pixman_color_t red; |
| 159 | pixman_color_t blue; |
| 160 | pixman_color_t cyan; |
| 161 | pixman_color_t green; |
| 162 | |
| 163 | color(&red, 255, 0, 0); |
| 164 | color(&blue, 0, 0, 255); |
| 165 | color(&cyan, 0, 255, 255); |
| 166 | color(&green, 0, 255, 0); |
| 167 | |
| 168 | client = create_client_and_test_surface(100, 50, 100, 100); |
| 169 | assert(client); |
| 170 | subco = get_subcompositor(client); |
| 171 | |
| 172 | /* move the pointer clearly away from our screenshooting area */ |
Alexandros Frantzis | 2180858 | 2017-12-13 13:27:55 +0200 | [diff] [blame] | 173 | 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] | 174 | |
| 175 | /* make the parent surface red */ |
| 176 | surf[0] = client->surface->wl_surface; |
| 177 | bufs[0] = surface_commit_color(client, surf[0], &red, 100, 100); |
| 178 | /* sub[0] is not used */ |
| 179 | |
Pekka Paalanen | dc9d334 | 2019-11-15 15:48:08 +0200 | [diff] [blame] | 180 | fail += check_screen(client, "subsurface_z_order", 0, &clip, 0); |
Pekka Paalanen | e7c6aa6 | 2017-01-27 17:30:28 +0100 | [diff] [blame] | 181 | |
| 182 | /* create a blue sub-surface above red */ |
| 183 | surf[1] = wl_compositor_create_surface(client->wl_compositor); |
| 184 | sub[1] = wl_subcompositor_get_subsurface(subco, surf[1], surf[0]); |
| 185 | bufs[1] = surface_commit_color(client, surf[1], &blue, 100, 100); |
| 186 | |
| 187 | wl_subsurface_set_position(sub[1], 20, 20); |
| 188 | wl_surface_commit(surf[0]); |
| 189 | |
Pekka Paalanen | dc9d334 | 2019-11-15 15:48:08 +0200 | [diff] [blame] | 190 | fail += check_screen(client, "subsurface_z_order", 1, &clip, 1); |
Pekka Paalanen | e7c6aa6 | 2017-01-27 17:30:28 +0100 | [diff] [blame] | 191 | |
| 192 | /* create a cyan sub-surface above blue */ |
| 193 | surf[2] = wl_compositor_create_surface(client->wl_compositor); |
| 194 | sub[2] = wl_subcompositor_get_subsurface(subco, surf[2], surf[1]); |
| 195 | bufs[2] = surface_commit_color(client, surf[2], &cyan, 100, 100); |
| 196 | |
| 197 | wl_subsurface_set_position(sub[2], 20, 20); |
| 198 | wl_surface_commit(surf[1]); |
| 199 | wl_surface_commit(surf[0]); |
| 200 | |
Pekka Paalanen | dc9d334 | 2019-11-15 15:48:08 +0200 | [diff] [blame] | 201 | fail += check_screen(client, "subsurface_z_order", 2, &clip, 2); |
Pekka Paalanen | e7c6aa6 | 2017-01-27 17:30:28 +0100 | [diff] [blame] | 202 | |
| 203 | /* create a green sub-surface above blue, sibling to cyan */ |
| 204 | surf[3] = wl_compositor_create_surface(client->wl_compositor); |
| 205 | sub[3] = wl_subcompositor_get_subsurface(subco, surf[3], surf[1]); |
| 206 | bufs[3] = surface_commit_color(client, surf[3], &green, 100, 100); |
| 207 | |
| 208 | wl_subsurface_set_position(sub[3], -40, 10); |
| 209 | wl_surface_commit(surf[1]); |
| 210 | wl_surface_commit(surf[0]); |
| 211 | |
Pekka Paalanen | dc9d334 | 2019-11-15 15:48:08 +0200 | [diff] [blame] | 212 | fail += check_screen(client, "subsurface_z_order", 3, &clip, 3); |
Pekka Paalanen | e7c6aa6 | 2017-01-27 17:30:28 +0100 | [diff] [blame] | 213 | |
| 214 | /* stack blue below red, which brings also cyan and green below red */ |
| 215 | wl_subsurface_place_below(sub[1], surf[0]); |
| 216 | wl_surface_commit(surf[0]); |
| 217 | |
Pekka Paalanen | dc9d334 | 2019-11-15 15:48:08 +0200 | [diff] [blame] | 218 | fail += check_screen(client, "subsurface_z_order", 4, &clip, 4); |
Pekka Paalanen | e7c6aa6 | 2017-01-27 17:30:28 +0100 | [diff] [blame] | 219 | |
| 220 | assert(fail == 0); |
| 221 | |
| 222 | for (i = 0; i < ARRAY_LENGTH(sub); i++) |
| 223 | if (sub[i]) |
| 224 | wl_subsurface_destroy(sub[i]); |
| 225 | |
| 226 | for (i = 0; i < ARRAY_LENGTH(surf); i++) |
| 227 | if (surf[i]) |
| 228 | wl_surface_destroy(surf[i]); |
| 229 | |
| 230 | for (i = 0; i < ARRAY_LENGTH(bufs); i++) |
| 231 | if (bufs[i]) |
| 232 | buffer_destroy(bufs[i]); |
| 233 | } |