blob: 458ebdb453077ac1a73484d4b9cbb368495cd183 [file] [log] [blame]
Bryce Harringtonfb9089d2014-11-04 16:39:38 -08001/*
2 * Copyright © 2015 Samsung Electronics Co., Ltd
3 *
Bryce Harrington2cc92972015-06-11 15:39:40 -07004 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
Bryce Harringtonfb9089d2014-11-04 16:39:38 -080011 *
Bryce Harrington2cc92972015-06-11 15:39:40 -070012 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
Bryce Harringtonfb9089d2014-11-04 16:39:38 -080024 */
25
26#include "config.h"
27
Bryce Harringtonfb9089d2014-11-04 16:39:38 -080028#include <stdio.h>
Bryce Harringtonfb9089d2014-11-04 16:39:38 -080029
30#include "weston-test-client-helper.h"
31
32char *server_parameters="--use-pixman --width=320 --height=240";
33
Derek Foreman35b7f252015-05-25 15:19:38 -050034static void
Pekka Paalanen95e28722016-05-20 18:01:05 +030035draw_stuff(pixman_image_t *image)
Derek Foreman35b7f252015-05-25 15:19:38 -050036{
Pekka Paalanen95e28722016-05-20 18:01:05 +030037 int w, h;
38 int stride; /* bytes */
Derek Foreman35b7f252015-05-25 15:19:38 -050039 int x, y;
Derek Foreman97b9b172015-05-26 12:00:49 -050040 uint8_t r, g, b;
Pekka Paalanen95e28722016-05-20 18:01:05 +030041 uint32_t *pixels;
Derek Foreman97b9b172015-05-26 12:00:49 -050042 uint32_t *pixel;
Pekka Paalanen95e28722016-05-20 18:01:05 +030043 pixman_format_code_t fmt;
44
45 fmt = pixman_image_get_format(image);
46 w = pixman_image_get_width(image);
47 h = pixman_image_get_height(image);
48 stride = pixman_image_get_stride(image);
49 pixels = pixman_image_get_data(image);
50
51 assert(PIXMAN_FORMAT_BPP(fmt) == 32);
Derek Foreman35b7f252015-05-25 15:19:38 -050052
53 for (x = 0; x < w; x++)
54 for (y = 0; y < h; y++) {
Derek Foreman97b9b172015-05-26 12:00:49 -050055 b = x;
56 g = x + y;
57 r = y;
Pekka Paalanen95e28722016-05-20 18:01:05 +030058 pixel = pixels + (y * stride / 4) + x;
Derek Foreman97b9b172015-05-26 12:00:49 -050059 *pixel = (255 << 24) | (r << 16) | (g << 8) | b;
Derek Foreman35b7f252015-05-25 15:19:38 -050060 }
61}
62
Bryce Harringtonfb9089d2014-11-04 16:39:38 -080063TEST(internal_screenshot)
64{
Pekka Paalanen95e28722016-05-20 18:01:05 +030065 struct buffer *buf;
Bryce Harringtonfb9089d2014-11-04 16:39:38 -080066 struct client *client;
Derek Foreman35b7f252015-05-25 15:19:38 -050067 struct wl_surface *surface;
Pekka Paalanen365c1292016-05-27 14:11:01 +030068 struct buffer *screenshot = NULL;
Pekka Paalanen289fdeb2016-05-25 11:47:41 +030069 pixman_image_t *reference_good = NULL;
70 pixman_image_t *reference_bad = NULL;
Pekka Paalanena5bb91d2016-06-02 18:02:46 +030071 pixman_image_t *diffimg;
Bryce Harringtonfb9089d2014-11-04 16:39:38 -080072 struct rectangle clip;
73 const char *fname;
Bryce Harringtonfb9089d2014-11-04 16:39:38 -080074 bool match = false;
75 bool dump_all_images = true;
76
Bryce Harringtonfb9089d2014-11-04 16:39:38 -080077 /* Create the client */
Bryce Harrington111e0222015-05-14 15:07:55 -070078 printf("Creating client for test\n");
Bryce Harringtonfb9089d2014-11-04 16:39:38 -080079 client = create_client_and_test_surface(100, 100, 100, 100);
80 assert(client);
Derek Foreman35b7f252015-05-25 15:19:38 -050081 surface = client->surface->wl_surface;
82
Pekka Paalanene651bb02016-06-10 10:50:04 +030083 /*
84 * We are racing our screenshooting against weston-desktop-shell
85 * setting the cursor. If w-d-s wins, our screenshot will have a cursor
86 * shown, which makes the image comparison fail. Our window and the
87 * default pointer position are accidentally causing an overlap that
88 * intersects our test clip rectangle.
89 *
90 * w-d-s wins very rarely though, so the race is easy to miss. You can
91 * make it happen by putting a delay before the call to
92 * create_client_and_test_surface().
93 *
94 * The weston_test_move_pointer() below makes the race irrelevant, as
95 * the cursor won't overlap with anything we care about.
96 */
97
98 /* Move the pointer away from the screenshot area. */
99 weston_test_move_pointer(client->test->weston_test, 0, 0);
100
Pekka Paalanen95e28722016-05-20 18:01:05 +0300101 buf = create_shm_buffer_a8r8g8b8(client, 100, 100);
102 draw_stuff(buf->image);
103 wl_surface_attach(surface, buf->proxy, 0, 0);
Derek Foreman35b7f252015-05-25 15:19:38 -0500104 wl_surface_damage(surface, 0, 0, 100, 100);
105 wl_surface_commit(surface);
Bryce Harringtonfb9089d2014-11-04 16:39:38 -0800106
107 /* Take a snapshot. Result will be in screenshot->wl_buffer. */
Bryce Harrington111e0222015-05-14 15:07:55 -0700108 printf("Taking a screenshot\n");
109 screenshot = capture_screenshot_of_output(client);
110 assert(screenshot);
Bryce Harringtonfb9089d2014-11-04 16:39:38 -0800111
Derek Foreman35b7f252015-05-25 15:19:38 -0500112 /* Load good reference image */
113 fname = screenshot_reference_filename("internal-screenshot-good", 0);
114 printf("Loading good reference image %s\n", fname);
Pekka Paalanen289fdeb2016-05-25 11:47:41 +0300115 reference_good = load_image_from_png(fname);
Derek Foreman35b7f252015-05-25 15:19:38 -0500116 assert(reference_good);
117
118 /* Load bad reference image */
119 fname = screenshot_reference_filename("internal-screenshot-bad", 0);
120 printf("Loading bad reference image %s\n", fname);
Pekka Paalanen289fdeb2016-05-25 11:47:41 +0300121 reference_bad = load_image_from_png(fname);
Derek Foreman35b7f252015-05-25 15:19:38 -0500122 assert(reference_bad);
Bryce Harringtonfb9089d2014-11-04 16:39:38 -0800123
Pekka Paalanen47d68da2016-05-23 17:42:27 +0300124 /* Test check_images_match() without a clip.
Derek Foreman35b7f252015-05-25 15:19:38 -0500125 * We expect this to fail since we use a bad reference image
Bryce Harringtonfb9089d2014-11-04 16:39:38 -0800126 */
Pekka Paalanen365c1292016-05-27 14:11:01 +0300127 match = check_images_match(screenshot->image, reference_bad, NULL);
Bryce Harringtonfb9089d2014-11-04 16:39:38 -0800128 printf("Screenshot %s reference image\n", match? "equal to" : "different from");
129 assert(!match);
Pekka Paalanen289fdeb2016-05-25 11:47:41 +0300130 pixman_image_unref(reference_bad);
Bryce Harringtonfb9089d2014-11-04 16:39:38 -0800131
Pekka Paalanen47d68da2016-05-23 17:42:27 +0300132 /* Test check_images_match() with clip.
Bryce Harringtonfb9089d2014-11-04 16:39:38 -0800133 * Alpha-blending and other effects can cause irrelevant discrepancies, so look only
134 * at a small portion of the solid-colored background
135 */
Derek Foreman35b7f252015-05-25 15:19:38 -0500136 clip.x = 100;
137 clip.y = 100;
138 clip.width = 100;
139 clip.height = 100;
Bryce Harringtonfb9089d2014-11-04 16:39:38 -0800140 printf("Clip: %d,%d %d x %d\n", clip.x, clip.y, clip.width, clip.height);
Pekka Paalanen365c1292016-05-27 14:11:01 +0300141 match = check_images_match(screenshot->image, reference_good, &clip);
Bryce Harringtonfb9089d2014-11-04 16:39:38 -0800142 printf("Screenshot %s reference image in clipped area\n", match? "matches" : "doesn't match");
Pekka Paalanena5bb91d2016-06-02 18:02:46 +0300143 if (!match) {
144 diffimg = visualize_image_difference(screenshot->image, reference_good, &clip);
145 fname = screenshot_output_filename("internal-screenshot-error", 0);
146 write_image_as_png(diffimg, fname);
147 pixman_image_unref(diffimg);
148 }
Pekka Paalanen289fdeb2016-05-25 11:47:41 +0300149 pixman_image_unref(reference_good);
Bryce Harringtonfb9089d2014-11-04 16:39:38 -0800150
151 /* Test dumping of non-matching images */
152 if (!match || dump_all_images) {
Bryce Harrington85e65f52015-05-14 12:21:13 -0700153 fname = screenshot_output_filename("internal-screenshot", 0);
Pekka Paalanen365c1292016-05-27 14:11:01 +0300154 write_image_as_png(screenshot->image, fname);
Bryce Harringtonfb9089d2014-11-04 16:39:38 -0800155 }
156
Pekka Paalanen365c1292016-05-27 14:11:01 +0300157 buffer_destroy(screenshot);
Bryce Harringtonfb9089d2014-11-04 16:39:38 -0800158
159 printf("Test complete\n");
160 assert(match);
161}