/*
 * Copyright © 2023 Collabora, Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "config.h"

#include "weston-test-client-helper.h"
#include "weston-test-fixture-compositor.h"
#include "weston-output-capture-client-protocol.h"
#include "weston-test-assert.h"

struct setup_args {
	struct fixture_metadata meta;
	enum weston_renderer_type renderer;
	enum client_buffer_type buffer_type;
};

static const struct setup_args my_setup_args[] = {
	{
		.meta.name = "Pixman",
		.renderer = WESTON_RENDERER_PIXMAN,
		.buffer_type = CLIENT_BUFFER_TYPE_SHM,
	},
	{
		.meta.name = "GL - dmabuf",
		.renderer = WESTON_RENDERER_GL,
		.buffer_type = CLIENT_BUFFER_TYPE_DMABUF,
	},
	{
		.meta.name = "Vulkan - dmabuf",
		.renderer = WESTON_RENDERER_VULKAN,
		.buffer_type = CLIENT_BUFFER_TYPE_DMABUF,
	},
};

static enum test_result_code
fixture_setup(struct weston_test_harness *harness, const struct setup_args *arg)
{
	struct compositor_setup setup;

	compositor_setup_defaults(&setup);
	setup.backend = WESTON_BACKEND_DRM;
	setup.renderer = arg->renderer;
	setup.shell = SHELL_TEST_DESKTOP;
	setup.logging_scopes = "log,drm-backend";

	weston_ini_setup(&setup,
			 cfgln("[shell]"),
			 cfgln("startup-animation=%s", "none"));

	return weston_test_harness_execute_as_client(harness, &setup);
}
DECLARE_FIXTURE_SETUP_WITH_ARG(fixture_setup, my_setup_args, meta);

static void
draw_stuff(pixman_image_t *image)
{
	int w, h;
	int stride; /* bytes */
	int x, y;
	uint32_t r, g, b;
	uint32_t *pixels;
	uint32_t *pixel;
	pixman_format_code_t fmt;

	fmt = pixman_image_get_format(image);
	w = pixman_image_get_width(image);
	h = pixman_image_get_height(image);
	stride = pixman_image_get_stride(image);
	pixels = pixman_image_get_data(image);

	test_assert_int_eq(PIXMAN_FORMAT_BPP(fmt), 32);

	for (x = 0; x < w; x++)
		for (y = 0; y < h; y++) {
			b = x;
			g = x + y;
			r = y;
			pixel = pixels + (y * stride / 4) + x;
			*pixel = (255U << 24) | (r << 16) | (g << 8) | b;
		}
}

static enum test_result_code
drm_writeback_screenshot(struct wet_testsuite_data *suite_data)
{
	const struct setup_args *args = &my_setup_args[get_test_fixture_index()];
	struct client *client;
	struct buffer *buffer;
	struct buffer *buffer_for_second_screenshot;
	struct buffer *screenshot = NULL;
	struct buffer *second_screenshot = NULL;
	pixman_image_t *reference = NULL;
	pixman_image_t *diffimg = NULL;
	struct wl_surface *surface;
	struct rectangle clip;
	char *fname;
	bool match;
	int frame;

	/* create client */
	testlog("Creating client for test\n");
	client = create_client_and_test_surface(100, 100, 100, 100);
	test_assert_ptr_not_null(client);
	surface = client->surface->wl_surface;

	/* move pointer away from image so it does not interfere with the
	 * comparison of the writeback screenshot with the reference image */
	weston_test_move_pointer(client->test->weston_test, 0, 1, 0, 0, 0);

	buffer = create_shm_buffer_a8r8g8b8(client, 100, 100);
	draw_stuff(buffer->image);

	wl_surface_attach(surface, buffer->proxy, 0, 0);
	wl_surface_damage(surface, 0, 0, 100, 100);
	frame_callback_set(surface, &frame);
	wl_surface_commit(surface);
	frame_callback_wait(client, &frame);

	/* take screenshot */
	testlog("Taking a screenshot\n");
	screenshot = client_capture_output(client, client->output,
					   WESTON_CAPTURE_V1_SOURCE_WRITEBACK,
					   args->buffer_type);
	test_assert_ptr_not_null(screenshot);
	buffer_destroy(screenshot);

	/* Use new buffer to avoid deadlock between first and second screenshot. */
	buffer_for_second_screenshot = create_shm_buffer_a8r8g8b8(client, 100, 100);
	draw_stuff(buffer_for_second_screenshot->image);

	wl_surface_attach(surface, buffer_for_second_screenshot->proxy, 0, 0);
	wl_surface_damage(surface, 0, 0, 100, 100);
	frame_callback_set(surface, &frame);
	wl_surface_commit(surface);
	frame_callback_wait(client, &frame);

	/* take another screenshot; this is important to ensure the
	 * writeback state machine is working correctly */
	testlog("Taking another screenshot\n");
	second_screenshot = client_capture_output(client, client->output,
						  WESTON_CAPTURE_V1_SOURCE_WRITEBACK,
						  args->buffer_type);
	test_assert_ptr_not_null(second_screenshot);

	/* load reference image */
	fname = screenshot_reference_filename("drm-writeback-screenshot", 0);
	testlog("Loading good reference image %s\n", fname);
	reference = load_image_from_png(fname);
	test_assert_ptr_not_null(reference);
	free(fname);

	/* check if they match - only the colored square matters, so the
	 * clip is used to ignore the background */
	clip.x = 100;
	clip.y = 100;
	clip.width = 100;
	clip.height = 100;
	client_buffer_util_maybe_sync_dmabuf_start(second_screenshot->buf);
	match = check_images_match(second_screenshot->image, reference, &clip, NULL);
	testlog("Screenshot %s reference image\n", match? "equal to" : "different from");
	if (!match) {
		diffimg = visualize_image_difference(second_screenshot->image, reference, &clip, NULL);
		fname = output_filename_for_test_case("error", 0, "png");
		write_image_as_png(diffimg, fname);
		pixman_image_unref(diffimg);
		free(fname);
	}
	client_buffer_util_maybe_sync_dmabuf_end(second_screenshot->buf);

	pixman_image_unref(reference);
	buffer_destroy(second_screenshot);
	buffer_destroy(buffer);
	buffer_destroy(buffer_for_second_screenshot);
	client_destroy(client);

	test_assert_true(match);

	return RESULT_OK;
}

DECLARE_TEST_LIST(
	TESTFN(drm_writeback_screenshot),
);
