presentation-test.c 5.9 KB
Newer Older
1 2 3
/*
 * Copyright © 2014 Collabora, Ltd.
 *
4 5 6 7 8 9 10
 * 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:
11
 *
12 13 14 15 16 17 18 19 20 21 22 23
 * 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.
24 25 26 27
 */

#include "config.h"

28
#include <stdint.h>
29
#include <stdio.h>
30
#include <stdlib.h>
31 32 33 34
#include <string.h>
#include <assert.h>
#include <time.h>

35
#include "shared/helpers.h"
36
#include "shared/xalloc.h"
37
#include "shared/timespec-util.h"
38
#include "weston-test-client-helper.h"
39
#include "presentation-time-client-protocol.h"
Pekka Paalanen's avatar
Pekka Paalanen committed
40 41 42 43 44 45 46 47 48 49 50 51
#include "weston-test-fixture-compositor.h"

static enum test_result_code
fixture_setup(struct weston_test_harness *harness)
{
	struct compositor_setup setup;

	compositor_setup_defaults(&setup);

	return weston_test_harness_execute_as_client(harness, &setup);
}
DECLARE_FIXTURE_SETUP(fixture_setup);
52

53
static struct wp_presentation *
54 55 56 57
get_presentation(struct client *client)
{
	struct global *g;
	struct global *global_pres = NULL;
58
	struct wp_presentation *pres;
59 60

	wl_list_for_each(g, &client->global_list, link) {
61
		if (strcmp(g->interface, wp_presentation_interface.name))
62 63 64 65 66 67 68 69 70 71 72 73 74
			continue;

		if (global_pres)
			assert(0 && "multiple presentation objects");

		global_pres = g;
	}

	assert(global_pres && "no presentation found");

	assert(global_pres->version == 1);

	pres = wl_registry_bind(client->wl_registry, global_pres->name,
75
				&wp_presentation_interface, 1);
76 77 78 79 80 81 82
	assert(pres);

	return pres;
}

struct feedback {
	struct client *client;
83
	struct wp_presentation_feedback *obj;
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

	enum {
		FB_PENDING = 0,
		FB_PRESENTED,
		FB_DISCARDED
	} result;

	struct wl_output *sync_output;
	uint64_t seq;
	struct timespec time;
	uint32_t refresh_nsec;
	uint32_t flags;
};

static void
feedback_sync_output(void *data,
100
		     struct wp_presentation_feedback *presentation_feedback,
101 102 103 104 105 106 107 108 109 110 111 112
		     struct wl_output *output)
{
	struct feedback *fb = data;

	assert(fb->result == FB_PENDING);

	if (output)
		fb->sync_output = output;
}

static void
feedback_presented(void *data,
113
		   struct wp_presentation_feedback *presentation_feedback,
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
		   uint32_t tv_sec_hi,
		   uint32_t tv_sec_lo,
		   uint32_t tv_nsec,
		   uint32_t refresh_nsec,
		   uint32_t seq_hi,
		   uint32_t seq_lo,
		   uint32_t flags)
{
	struct feedback *fb = data;

	assert(fb->result == FB_PENDING);
	fb->result = FB_PRESENTED;
	fb->seq = ((uint64_t)seq_hi << 32) + seq_lo;
	timespec_from_proto(&fb->time, tv_sec_hi, tv_sec_lo, tv_nsec);
	fb->refresh_nsec = refresh_nsec;
	fb->flags = flags;
}

static void
feedback_discarded(void *data,
134
		   struct wp_presentation_feedback *presentation_feedback)
135 136 137 138 139 140 141
{
	struct feedback *fb = data;

	assert(fb->result == FB_PENDING);
	fb->result = FB_DISCARDED;
}

142
static const struct wp_presentation_feedback_listener feedback_listener = {
143 144 145 146 147 148
	feedback_sync_output,
	feedback_presented,
	feedback_discarded
};

static struct feedback *
149 150 151
feedback_create(struct client *client,
		struct wl_surface *surface,
		struct wp_presentation *pres)
152 153 154 155 156
{
	struct feedback *fb;

	fb = xzalloc(sizeof *fb);
	fb->client = client;
157
	fb->obj = wp_presentation_feedback(pres, surface);
158
	wp_presentation_feedback_add_listener(fb->obj, &feedback_listener, fb);
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177

	return fb;
}

static void
feedback_wait(struct feedback *fb)
{
	while (fb->result == FB_PENDING) {
		assert(wl_display_dispatch(fb->client->wl_display) >= 0);
	}
}

static char *
pflags_to_str(uint32_t flags, char *str, unsigned len)
{
	static const struct {
		uint32_t flag;
		char sym;
	} desc[] = {
178 179 180 181
		{ WP_PRESENTATION_FEEDBACK_KIND_VSYNC, 's' },
		{ WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK, 'c' },
		{ WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION, 'e' },
		{ WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY, 'z' },
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
	};
	unsigned i;

	*str = '\0';
	if (len < ARRAY_LENGTH(desc) + 1)
		return str;

	for (i = 0; i < ARRAY_LENGTH(desc); i++)
		str[i] = flags & desc[i].flag ? desc[i].sym : '_';
	str[ARRAY_LENGTH(desc)] = '\0';

	return str;
}

static void
feedback_print(struct feedback *fb)
{
	char str[10];

	switch (fb->result) {
	case FB_PENDING:
203
		testlog("pending");
204 205
		return;
	case FB_DISCARDED:
206
		testlog("discarded");
207 208 209 210 211 212
		return;
	case FB_PRESENTED:
		break;
	}

	pflags_to_str(fb->flags, str, sizeof str);
213
	testlog("presented %lld.%09lld, refresh %u us, [%s] seq %" PRIu64,
214 215 216 217 218 219 220
		(long long)fb->time.tv_sec, (long long)fb->time.tv_nsec,
		fb->refresh_nsec / 1000, str, fb->seq);
}

static void
feedback_destroy(struct feedback *fb)
{
221
	wp_presentation_feedback_destroy(fb->obj);
222 223 224 225 226 227 228
	free(fb);
}

TEST(test_presentation_feedback_simple)
{
	struct client *client;
	struct feedback *fb;
229
	struct wp_presentation *pres;
230

231
	client = create_client_and_test_surface(100, 50, 123, 77);
232
	assert(client);
233
	pres = get_presentation(client);
234 235

	wl_surface_attach(client->surface->wl_surface,
236
			  client->surface->buffer->proxy, 0, 0);
237
	fb = feedback_create(client, client->surface->wl_surface, pres);
238 239 240 241 242 243 244
	wl_surface_damage(client->surface->wl_surface, 0, 0, 100, 100);
	wl_surface_commit(client->surface->wl_surface);

	client_roundtrip(client);

	feedback_wait(fb);

245
	testlog("%s feedback:", __func__);
246
	feedback_print(fb);
247
	testlog("\n");
248 249 250

	feedback_destroy(fb);
}