summaryrefslogtreecommitdiff
path: root/present.c
blob: a67893b5696fd7a8192cad49392ffd41544861ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
 * Copyright © 2013 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#include <time.h>
#include <sys/mman.h>
#include <xcb/xcb.h>
#include <xcb/xcb_aux.h>
#include <xcb/present.h>
#include <xcb/xfixes.h>
#include <xcb/sync.h>

#define	INIT_WIDTH	500
#define INIT_HEIGHT	500

int			width, height;
xcb_connection_t	*c;
int			screen_num;
xcb_screen_t		*screen;
xcb_gc_t		gc;
xcb_present_event_t	present_event;
xcb_window_t		window;
xcb_xfixes_region_t	update;
#define NPIXMAP	3
xcb_pixmap_t		pixmap[NPIXMAP];
bool			busy[NPIXMAP];
xcb_generic_event_t	*event;
xcb_generic_error_t	*error;
xcb_sync_fence_t	wait_fence;
int			using_fence;
const xcb_query_extension_reply_t	*present_extension;
uint32_t		window_mask;
uint32_t		window_values[5];
uint32_t		gc_mask;
uint32_t		gc_values[5];
xcb_segment_t		segments[2];
xcb_rectangle_t		rectangle;
xcb_ge_event_t		*xge;
int			x;
int			x_inc = 30;
uint64_t		msc;
uint64_t		target_msc;
int			interval = 0;
uint32_t		serial;
uint32_t		options = 0; // XCB_PRESENT_OPTION_ASYNC;

static void make_pixmap(int i)
{
	xcb_create_pixmap(c,
			  24,
			  (pixmap[i] = xcb_generate_id(c)),
			  window,
			  width,
			  height);
}

static void make_update(void)
{
	return;
	xcb_rectangle_t	rect = { .x = 0, .y = 0, .width = width, .height = height };

	xcb_xfixes_create_region(c,
				 (update = xcb_generate_id(c)),
				 1,
				 &rect);
}

static void discard_pixmap(void)
{
	int i;
	for (i = 0; i < NPIXMAP; i++) {
		if (pixmap[i]) {
			xcb_free_pixmap (c, pixmap[i]);
			pixmap[i] = 0;
		}
	}

	if (update) {
		xcb_xfixes_destroy_region(c, update);
		update = 0;
	}
}

static void draw(int i)
{
	if (!pixmap[i])
		make_pixmap(i);

	if (!update)
		make_update();

	gc_mask = XCB_GC_FOREGROUND;
	gc_values[0] = 0xffffffff;
	xcb_change_gc(c, gc, gc_mask, gc_values);
	rectangle.x = 0;
	rectangle.y = 0;
	rectangle.width = width;
	rectangle.height = height;
	xcb_poly_fill_rectangle(c, pixmap[i], gc, 1, &rectangle);

	gc_mask = XCB_GC_FOREGROUND;
	gc_values[0] = 0x0;
	xcb_change_gc(c, gc, gc_mask, gc_values);

	rectangle.x = x;
	rectangle.y = 0;
	rectangle.width = 60;
	rectangle.height = height;
	xcb_poly_fill_rectangle(c, pixmap[i], gc, 1, &rectangle);

	x += x_inc;
	if (x_inc > 0) {
		if (x + rectangle.width > width)
			x_inc = -x_inc;
	} else {
		if (x - rectangle.width < 0)
			x_inc = -x_inc;
	}
}

#define None 0

static void show(int i)
{
	xcb_present_pixmap (c,
			    window,
			    pixmap[i],
			    i,
			    None,
			    update,
			    0,
			    0,
			    None,
			    using_fence ? wait_fence : None,
			    None,
			    options,
			    target_msc,
			    0,
			    0,
			    0,
			    NULL);
}

int		frame_count;
int		skip_count;
int		copy_count;
int		flip_count;
uint32_t	start_time;
uint32_t	end_time;

static uint32_t
millis(void)
{
	struct timespec tp;
	clock_gettime(CLOCK_MONOTONIC, &tp);
        return (tp.tv_sec * 1000) + (tp.tv_nsec / 1000000L);
}

static void frame(void)
{
	int	p;

	for (p = 0; p < NPIXMAP; p++) {
		if (!busy[p]) {
			busy[p] = true;
			draw(p);
			target_msc += interval;
			show(p);
		}
	}
}

int
main (int argc, char **argv)
{
	c = xcb_connect(NULL, &screen_num);
	if (xcb_connection_has_error(c)) {
		printf ("connection error\n");
		exit(1);
	}
	screen = xcb_aux_get_screen(c, screen_num);

	(void) xcb_xfixes_query_version(c, XCB_XFIXES_MAJOR_VERSION, XCB_XFIXES_MINOR_VERSION);

	window_mask = XCB_CW_BACK_PIXEL|XCB_CW_EVENT_MASK;
	window_values[0] = 0xffffff;
	window_values[1] = XCB_EVENT_MASK_EXPOSURE|XCB_EVENT_MASK_KEY_PRESS;

	width = INIT_WIDTH;
	height = INIT_HEIGHT;
	xcb_create_window(c,
			  0,					/* depth */
			  (window = xcb_generate_id(c)),	/* window */
			  screen->root,				/* root */
			  0,					/* x */
			  0,					/* y */
			  width,				/* width */
			  height,				/* height */
			  0,					/* border_width */
			  XCB_WINDOW_CLASS_INPUT_OUTPUT,	/* class */
			  screen->root_visual,			/* visual */
			  window_mask,				/* mask */
			  window_values);			/* values */

	xcb_create_gc(c,
		      (gc = xcb_generate_id(c)),		/* gc */
		      window,					/* drawable */
		      0,					/* mask */
		      NULL);					/* values */

	xcb_sync_create_fence(c,
			      window,
			      (wait_fence = xcb_generate_id(c)),
			      0);

	xcb_present_select_input(c,
				 (present_event = xcb_generate_id(c)),
				 window,
				 XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY|
				 XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY|
				 XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY);

	present_extension = xcb_get_extension_data(c, &xcb_present_id);

	start_time = millis();
	draw(0);

	xcb_map_window(c, window);
	show(0);
	xcb_present_notify_msc(c, window, 0, 0, 5 * 60, 0);

	printf ("mapped\n");

	for (;;) {
		xcb_flush(c);
		event = xcb_wait_for_event(c);
		if (!event)
			break;
		switch (event->response_type) {
		case XCB_EXPOSE:
			break;
		case XCB_KEY_PRESS:
			switch (++using_fence) {
			case 1:
				printf ("wait fence\n");
				break;
			case 2:
				printf ("trigger fence\n");
				xcb_sync_trigger_fence(c, wait_fence);
				break;
			case 3:
				printf ("no fence\n");
				xcb_sync_reset_fence(c, wait_fence);
				using_fence = 0;
			}
			break;
		case 0:
			error = (xcb_generic_error_t *) event;
			printf ("error %d major %d minor %d\n",
				error->error_code,
				error->major_code,
				error->minor_code);
			break;
		case XCB_GE:
			xge = (xcb_ge_event_t *) event;
			if (xge->extension == present_extension->major_opcode) {
				switch (xge->event_type) {
				case XCB_PRESENT_CONFIGURE_NOTIFY: {
					xcb_present_configure_notify_event_t *ce = (void *) event;
					discard_pixmap();
					width = ce->width;
					height = ce->height;
					printf ("present configure %d %d %d %d\n",
						ce->x, ce->y, ce->width, ce->height);
					break;
				}
				case XCB_PRESENT_COMPLETE_NOTIFY: {
					xcb_present_complete_notify_event_t *ce = (void *) event;
//					printf ("kind %d mode %d\n", ce->kind, ce->mode);
					if (ce->kind == XCB_PRESENT_COMPLETE_KIND_PIXMAP) {
						int64_t	diff = ce->msc - target_msc;

						if (diff< 0)
							diff = -diff;
						if (diff && interval)
							printf ("target %lld actual %lld\n", target_msc, ce->msc);
						++frame_count;
						switch (ce->mode) {
						case XCB_PRESENT_COMPLETE_MODE_COPY:
							++copy_count;
							break;
						case XCB_PRESENT_COMPLETE_MODE_SKIP:
							++skip_count;
							break;
						case XCB_PRESENT_COMPLETE_MODE_FLIP:
							++flip_count;
							break;
						}
						msc = ce->msc;
						target_msc = msc + interval;
						if (interval)
							frame();
					} else {
						xcb_present_notify_msc(c, window, 0, 0, 5 * 60, 0);
						end_time = millis();
						double seconds = (end_time - start_time) / 1000.0;
						printf ("%5d frames in %8.3f seconds. frame rate: %f copy %d skip %d flip %d\n",
							frame_count, seconds, (double) frame_count / ((double) (end_time - start_time) / 1000.0),
							copy_count, skip_count, flip_count);
						frame_count = 0;
						copy_count = 0;
						skip_count = 0;
						flip_count = 0;
						start_time = millis();
					}
					break;
				}
				case XCB_PRESENT_IDLE_NOTIFY: {
					xcb_present_idle_notify_event_t *ie = (void *) event;
					busy[ie->serial] = false;
					if (!interval)
						frame();
					break;
				}
				}
			}
			break;
		default:
			printf ("response %d\n", event->response_type);
			break;
		}
	}
	return 0;
}