7 #include <X11/extensions/Xfixes.h>
20 static int dri2_open(Display *dpy)
23 char *driver, *device;
26 if (!DRI2Connect(dpy, DefaultRootWindow(dpy), &driver, &device))
29 printf ("Connecting to %s driver on %s\n", driver, device);
31 fd = open("/dev/dri/card0", O_RDWR);
35 if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
38 if (!DRI2Authenticate(dpy, DefaultRootWindow(dpy), auth.magic))
44 static void dri2_copy_swap(Display *dpy, Drawable d,
45 int width, int height, int has_front)
55 region = XFixesCreateRegion(dpy, &rect, 1);
56 DRI2CopyRegion(dpy, d, region, DRI2BufferFrontLeft, DRI2BufferBackLeft);
58 DRI2CopyRegion(dpy, d, region, DRI2BufferFakeFrontLeft, DRI2BufferFrontLeft);
59 XFixesDestroyRegion(dpy, region);
62 static void xsync(Display *dpy, Window win)
66 image = XGetImage(dpy, win, 0, 0, 1, 1, ~0, ZPixmap);
71 static double elapsed(const struct timespec *start,
72 const struct timespec *end)
74 return (end->tv_sec - start->tv_sec) +
75 1e-9*(end->tv_nsec - start->tv_nsec);
78 static void run(Display *dpy, int width, int height,
79 unsigned int *attachments, int nattachments,
83 XSetWindowAttributes attr;
86 struct timespec start, end;
88 /* Be nasty and install a fullscreen window on top so that we
89 * can guarantee we do not get clipped by children.
91 attr.override_redirect = 1;
92 win = XCreateWindow(dpy, DefaultRootWindow(dpy),
93 0, 0, width, height, 0,
94 DefaultDepth(dpy, DefaultScreen(dpy)),
96 DefaultVisual(dpy, DefaultScreen(dpy)),
97 CWOverrideRedirect, &attr);
101 DRI2CreateDrawable(dpy, win);
103 buffers = DRI2GetBuffers(dpy, win, &width, &height,
104 attachments, nattachments, &count);
105 if (count != nattachments)
109 clock_gettime(CLOCK_MONOTONIC, &start);
110 for (count = 0; count < COUNT; count++)
111 DRI2SwapBuffers(dpy, win, 0, 0, 0);
113 clock_gettime(CLOCK_MONOTONIC, &end);
114 printf("%d %s (%dx%d) swaps in %fs.\n",
115 count, name, width, height, elapsed(&start, &end));
118 clock_gettime(CLOCK_MONOTONIC, &start);
119 for (count = 0; count < COUNT; count++)
120 dri2_copy_swap(dpy, win, width, height, nattachments == 2);
122 clock_gettime(CLOCK_MONOTONIC, &end);
124 printf("%d %s (%dx%d) blits in %fs.\n",
125 count, name, width, height, elapsed(&start, &end));
127 DRI2SwapInterval(dpy, win, 0);
130 clock_gettime(CLOCK_MONOTONIC, &start);
131 for (count = 0; count < COUNT; count++)
132 DRI2SwapBuffers(dpy, win, 0, 0, 0);
134 clock_gettime(CLOCK_MONOTONIC, &end);
135 printf("%d %s (%dx%d) vblank=0 swaps in %fs.\n",
136 count, name, width, height, elapsed(&start, &end));
138 XDestroyWindow(dpy, win);
147 int width, height, fd;
148 unsigned int attachments[] = {
153 dpy = XOpenDisplay (NULL);
161 width = WidthOfScreen(DefaultScreenOfDisplay(dpy));
162 height = HeightOfScreen(DefaultScreenOfDisplay(dpy));
163 run(dpy, width, height, attachments, 1, "fullscreen");
164 run(dpy, width, height, attachments, 2, "fullscreen (with front)");
168 run(dpy, width, height, attachments, 1, "windowed");
169 run(dpy, width, height, attachments, 2, "windowed (with front)");