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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
|
/*
* Copyright © 2010 Intel Corporation
*
* 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.
*
* Authors:
* jian.j.zhao@intel.com
*/
/*
* Simple test case on the intel_swap_event.
*
*/
#include "piglit-util.h"
#include "GL/glx.h"
#include <sys/time.h>
#if defined(GLX_MESA_swap_control) && defined(GLX_INTEL_swap_event)
/* return current time (in seconds) */
static double
current_time(void)
{
struct timeval tv;
#ifdef __VMS
(void) gettimeofday(&tv, NULL );
#else
struct timezone tz;
(void) gettimeofday(&tv, &tz);
#endif
return (double) tv.tv_sec + tv.tv_usec / 1000000.0;
}
PFNGLXSWAPINTERVALMESAPROC pglXSwapIntervalMESA;
PFNGLXGETSWAPINTERVALMESAPROC pglXGetSwapIntervalMESA;
#define STACK_L 10
static GLboolean fullscreen = GL_FALSE; /* Create a fullscreen window */
static GLboolean verbose = GL_FALSE; /* Disable verbose. */
static GLboolean Automatic = GL_FALSE; /* Disable verbose. */
static GLboolean test_events = GL_FALSE; /* Disable verbose. */
static GLboolean interval_diff = GL_FALSE; /* Disable verbose. */
static GLboolean async = GL_FALSE; /* Disable verbose. */
int event_base, Glx_event, count=0, swap_count=0, event_count=0;
static int Intel_swap_event=0;
int event_count_total=0, frames_total=0, message_count=0;
static double time_call=0.0, time_fin=0.0, time_val=0.0;
double swap_start[STACK_L],swap_returned[STACK_L];
int interval=1;
char * swap_event_type=NULL;
/**
* Determine whether or not a GLX extension is supported.
*/
static int
is_glx_extension_supported(Display *dpy, const char *query)
{
const int scrnum = DefaultScreen(dpy);
const char *glx_extensions = NULL;
const size_t len = strlen(query);
const char *ptr;
if (glx_extensions == NULL) {
glx_extensions = glXQueryExtensionsString(dpy, scrnum);
}
ptr = strstr(glx_extensions, query);
return ((ptr != NULL) && ((ptr[len] == ' ') || (ptr[len] == '\0')));
}
static void
query_swap_event(Display *dpy)
{
if ( ! is_glx_extension_supported(dpy, "GLX_INTEL_swap_event")) {
printf("The GLX_INTEL_swap_event is not supported in current version.\n");
piglit_report_result(PIGLIT_SKIP);
} else {
printf("The GLX_INTEL_swap_event is supported in current version.\n");
}
if (interval_diff) {
if ( ! is_glx_extension_supported(dpy, "GLX_MESA_swap_control")) {
printf("GLX_MESA_swap_control was not supported by the driver.\n");
piglit_report_result(PIGLIT_SKIP);
} else {
pglXGetSwapIntervalMESA = (PFNGLXGETSWAPINTERVALMESAPROC)
glXGetProcAddressARB((const GLubyte *) "glXGetSwapIntervalMESA");
pglXSwapIntervalMESA = (PFNGLXSWAPINTERVALMESAPROC)
glXGetProcAddressARB((const GLubyte *) "glXSwapIntervalMESA");
}
}
}
/** Draw single frame, do SwapBuffers, compute FPS */
static void
draw_frame(Display *dpy, Window win)
{
static int frames = 0, async_swap;
static double tRot0 = -1.0, tRate0 = -1.0;
static double swap_freq[2];
double dt, t = current_time();
int tem;
if (tRot0 < 0.0)
tRot0 = t;
dt = t - tRot0;
tRot0 = t;
if (tRate0 < 0.0)
tRate0 = t;
if (t - tRate0 >= 3.0) {
if (interval_diff) {
if (message_count & 0x1) {
(*pglXSwapIntervalMESA)(1);
interval=(*pglXGetSwapIntervalMESA)();
if ( ! interval == 1 ) {
printf("Failed to set swap interval to 1.\n");
piglit_report_result(PIGLIT_FAILURE);
}
} else{
(*pglXSwapIntervalMESA)(0);
interval=(*pglXGetSwapIntervalMESA)();
if ( ! interval == 0 ) {
printf("Failed to set swap interval to 0.\n");
piglit_report_result(PIGLIT_FAILURE);
}
}
}
message_count++;
GLfloat seconds = t - tRate0;
if ( (time_val / frames) < 0.0016 ) {
// 0.0016 <=> 60Hz * 10 or 100Hz * 6
async_swap=1;
} else{
async_swap=0;
}
interval=1-interval;
swap_freq[interval] = frames / seconds;
if (Automatic) {
if (message_count==2) {
if (test_events) {
if ( ! Intel_swap_event == 0 ) {
if (verbose) {
printf("glXSwapBuffers is called %d times and there\
is %d Intel_swap_event received in past %3.1f seconds.\n", swap_count,
event_count, seconds);
printf("There is swap event received, and the swap \
type is %s.\n", swap_event_type);
}
piglit_report_result(PIGLIT_SUCCESS);
} else{
if (verbose) {
printf("glXSwapBuffers is called %d times and there\
is %d Intel_swap_event received in past %3.1f seconds.\n", swap_count,
event_count, seconds);
printf("There is no swap event received, and the \
swap type is %s.\n", swap_event_type);
}
piglit_report_result(PIGLIT_FAILURE);
}
}
if (interval_diff) {
tem = 1.5 * swap_freq[1];
if ( swap_freq[0] >= tem ) {
if (verbose) {
printf("The swap frequency of no swap interval is \
much larger than swap interval being 1.\n");
}
piglit_report_result(PIGLIT_SUCCESS);
} else{
if(fullscreen)
{
if(verbose)
printf("In fullscreen mode, the swap frequency of \
no swap interval is limited under fresh rate.\n");
piglit_report_result(PIGLIT_SUCCESS);
}
if (verbose) {
printf("The swap frequency of no swap interval is \
not much larger than swap interval being 1. They are %lf and %lf.\n",
swap_freq[0], swap_freq[1]);
}
piglit_report_result(PIGLIT_FAILURE);
}
}
if (async) {
if (verbose) {
printf("It takes about %lf seconds returning back from\
the glXSwapBuffers call on average.\n", (time_val / frames));
}
if (async_swap ==1 ) {
piglit_report_result(PIGLIT_SUCCESS);
} else{
piglit_report_result(PIGLIT_FAILURE);
}
}
}
}
tRate0 = t;
frames = 0;
time_val = 0;
swap_count= 0;
event_count= 0;
}
if (frames_total & 1) {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glColor3f(1.0f, 1.0f, 1.0f);
} else {
glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
glColor3f(1.0f, 0.0f, 0.0f);
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
count=frames_total%STACK_L;
time_call=current_time();
swap_start[count]=time_call;
glXSwapBuffers(dpy, win);
time_fin=current_time();
swap_returned[count]=time_fin;
time_val+=(time_fin-time_call);
frames++;
frames_total++;
swap_count++;
}
/**
* Remove window border/decorations.
*/
static void
no_border( Display *dpy, Window w)
{
static const unsigned MWM_HINTS_DECORATIONS = (1 << 1);
static const int PROP_MOTIF_WM_HINTS_ELEMENTS = 5;
typedef struct
{
unsigned long flags;
unsigned long functions;
unsigned long decorations;
long inputMode;
unsigned long status;
} PropMotifWmHints;
PropMotifWmHints motif_hints;
Atom prop, proptype;
unsigned long flags = 0;
/* setup the property */
motif_hints.flags = MWM_HINTS_DECORATIONS;
motif_hints.decorations = flags;
/* get the atom for the property */
prop = XInternAtom( dpy, "_MOTIF_WM_HINTS", True );
if (!prop) {
/* something went wrong! */
return;
}
/* not sure this is correct, seems to work, XA_WM_HINTS didn't work */
proptype = prop;
XChangeProperty( dpy, w, /* display, window */
prop, proptype, /* property, type */
32, /* format: 32-bit datums */
PropModeReplace, /* mode */
(unsigned char *) &motif_hints, /* data */
PROP_MOTIF_WM_HINTS_ELEMENTS /* nelements */
);
}
/*
* Create an RGB, double-buffered window.
* Return the window and context handles.
*/
static void
make_window( Display *dpy, const char *name,
int x, int y, int width, int height,
Window *winRet, GLXContext *ctxRet, GLXWindow *glxWinRet)
{
int attribs[] = {
GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
GLX_RENDER_TYPE, GLX_RGBA_BIT,
GLX_DOUBLEBUFFER, True, /* Request a double-buffered color buffer with */
GLX_RED_SIZE, 1, /* the maximum number of bits per component */
GLX_GREEN_SIZE, 1,
GLX_BLUE_SIZE, 1,
None };
int scrnum, nelements;
XSetWindowAttributes attr;
unsigned long mask;
Window root;
Window win;
GLXContext ctx;
XVisualInfo *visinfo;
GLXFBConfig *fbc;
GLXWindow gwin;
scrnum = DefaultScreen( dpy );
root = RootWindow( dpy, scrnum );
if (fullscreen) {
x = 0; y = 0;
width = DisplayWidth( dpy, scrnum );
height = DisplayHeight( dpy, scrnum );
}
fbc = glXChooseFBConfig(dpy, scrnum, attribs, &nelements);
visinfo = glXGetVisualFromFBConfig(dpy, fbc[0]);
if (!visinfo) {
printf("Error: couldn't get an RGB, Double-buffered visual\n");
piglit_report_result(PIGLIT_SKIP);
}
ctx = glXCreateNewContext(dpy, fbc[0], GLX_RGBA_TYPE, 0, GL_TRUE);
/* window attributes */
attr.background_pixel = 0;
attr.border_pixel = 0;
attr.colormap = XCreateColormap( dpy, root, visinfo->visual, AllocNone);
attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
/* XXX this is a bad way to get a borderless window! */
mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
win = XCreateWindow( dpy, root, x, y, width, height,
0, visinfo->depth, InputOutput,
visinfo->visual, mask, &attr );
XMapWindow(dpy, win);
gwin = glXCreateWindow(dpy, fbc[0], win, attribs);
glXMakeContextCurrent(dpy, gwin, gwin, ctx);
glXSelectEvent(dpy, gwin, GLX_BUFFER_SWAP_COMPLETE_INTEL_MASK);
if (fullscreen)
no_border(dpy, win);
XFree(visinfo);
*winRet = win;
*ctxRet = ctx;
*glxWinRet = gwin;
}
/**
* Only handle one glx event.
*/
void
handle_event(Display *dpy, Window win, XEvent *event)
{
(void) dpy;
(void) win;
if ( Glx_event == event->type) {
XEvent * event_p=event;
GLXBufferSwapComplete * glx_event=(GLXBufferSwapComplete *) event_p;
static double t_last=-1.0;
time_fin=current_time();
if (t_last < 0) {
t_last=time_fin;
}
if ( time_fin - t_last >= 3.0) {
if ( verbose ) {
count=event_count_total%STACK_L;
printf("It receives the recent event at %lf seconds, and that\
glXSwapBuffers was called at %lf seconds, its swap returned at %lf seconds, so\
the total time of glXSwapBuffers takes is %lf seconds.\n", time_fin,
swap_start[count], swap_returned[count], (time_fin-swap_start[count]));
}
t_last=time_fin;
}
switch (glx_event->event_type) {
case GLX_EXCHANGE_COMPLETE_INTEL:
Intel_swap_event=GLX_EXCHANGE_COMPLETE_INTEL;
swap_event_type="GLX_EXCHANGE_COMPLETE_INTEL";
event_count++;
event_count_total++;
break;
case GLX_COPY_COMPLETE_INTEL:
Intel_swap_event=GLX_COPY_COMPLETE_INTEL;
swap_event_type="GLX_COPY_COMPLETE_INTEL";
event_count++;
event_count_total++;
break;
case GLX_FLIP_COMPLETE_INTEL:
Intel_swap_event=GLX_FLIP_COMPLETE_INTEL;
swap_event_type="GLX_FLIP_COMPLETE_INTEL";
event_count++;
event_count_total++;
break;
}
}
}
static void
event_loop(Display *dpy, GLXWindow win)
{
while (1) {
while (XPending(dpy) > 0) {
XEvent event;
XNextEvent(dpy, &event);
Glx_event=event_base + GLX_BufferSwapComplete;
handle_event(dpy, win, &event);
}
draw_frame(dpy, win);
}
}
static void
usage(void)
{
printf("Usage:\n");
printf(" -fullscreen run in fullscreen mode\n");
printf(" -v verbose mode, have more log\n");
printf(" -auto test automatically \n");
printf(" --event test whether we can get swap events\n");
printf(" --interval we expect that swap interval set to 0 should \
have higher swap frequency than interval to 1\n");
printf(" --async test whether glXSwapBuffers is done asynchronously\n");
}
int
main(int argc, char *argv[])
{
unsigned int winWidth = 30, winHeight = 30;
int x = 0, y = 0;
Display *dpy;
Window win;
GLXWindow glxWin;
GLXContext ctx;
char *dpyName = NULL;
int i, error_base;
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "-auto") == 0) {
Automatic = GL_TRUE;
}
else if (strcmp(argv[i], "-v") == 0) {
verbose = GL_TRUE;
}
else if (strcmp(argv[i], "-fullscreen") == 0) {
fullscreen = GL_TRUE;
}
else if (strcmp(argv[i], "--event") == 0) {
test_events=GL_TRUE;
}
else if (strcmp(argv[i], "--async") == 0) {
async = GL_TRUE;
}
else if (strcmp(argv[i], "--interval") == 0) {
interval_diff = GL_TRUE;
}
else {
usage();
piglit_report_result(PIGLIT_SKIP);
}
}
if (!( interval_diff || async || test_events )) {
printf("Which do you want to test, events? asynchronous? or swap interval?\n");
usage();
piglit_report_result(PIGLIT_SKIP);
}
dpy = XOpenDisplay(dpyName);
if (!dpy) {
printf("Error: couldn't open display %s\n",
dpyName ? dpyName : getenv("DISPLAY"));
piglit_report_result(PIGLIT_FAILURE);
}
make_window(dpy, "Swap event test", x, y, winWidth, winHeight, &win, &ctx, &glxWin);
query_swap_event(dpy);
glXQueryExtension(dpy, &error_base, &event_base);
if (interval_diff) {
(*pglXSwapIntervalMESA)(1);
interval=(*pglXGetSwapIntervalMESA)();
if ( ! interval == 1 ) {
printf("Failed to set swap interval to 1.\n");
piglit_report_result(PIGLIT_FAILURE);
}
}
event_loop(dpy, glxWin);
glXDestroyContext(dpy, ctx);
XDestroyWindow(dpy, win);
XCloseDisplay(dpy);
return 0;
}
#else
int
main(int argc, char *argv[])
{
piglit_report_result(PIGLIT_SKIP);
return 0;
}
#endif /* GLX_MESA_swap_control && GLX_INTEL_swap_event */
|