]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/omapdrmtest.git/blob - util/display-x11.c
758b08fb60ac50c270e4d3292f44d72c82162254
[glsdk/omapdrmtest.git] / util / display-x11.c
1 /*
2  * Copyright (C) 2011 Texas Instruments
3  * Author: Rob Clark <rob.clark@linaro.org>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published by
7  * the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
22 #include "util.h"
24 #include <X11/Xlib.h>
25 #include <X11/Xmd.h>
26 #include <X11/Xregion.h>
27 #include <X11/extensions/dri2proto.h>
28 #include <X11/extensions/dri2.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
34 static int global_fd = 0;
36 #define to_display_x11(x) container_of(x, struct display_x11, base)
37 struct display_x11 {
38         struct display base;
39         Display *dpy;
40         Window win;
41 };
43 #define to_buffer_x11(x) container_of(x, struct buffer_x11, base)
44 struct buffer_x11 {
45         struct buffer base;
46         DRI2Buffer *dri2buf;
47 };
50 #define MAX_BUFFERS 32
52 static struct buffer **
53 get_buffers(struct display *disp, uint32_t n)
54 {
55         ERROR("unimplemented");
56         return NULL;
57 }
59 static struct buffer **
60 get_vid_buffers(struct display *disp, uint32_t n,
61                 uint32_t fourcc, uint32_t w, uint32_t h)
62 {
63         struct display_x11 *disp_x11 = to_display_x11(disp);
64         struct buffer **bufs;
65         unsigned attachments[MAX_BUFFERS+1] = {
66                         DRI2BufferFrontLeft, 32, /* always requested, never returned */
67         };
68         DRI2Buffer *dri2bufs;
69         uint32_t i, j, nbufs = 1;
71         for (i = 0; i < n; i++) {
72                 attachments[ nbufs * 2     ] = i + 1;
73                 attachments[(nbufs * 2) + 1] = fourcc;
74                 nbufs++;
75         }
77         dri2bufs = DRI2GetBuffersVid(disp_x11->dpy, disp_x11->win,
78                         w, h, attachments, nbufs, &nbufs);
79         if (!dri2bufs) {
80                 ERROR("DRI2GetBuffersVid failed");
81                 return NULL;
82         }
84         MSG("DRI2GetBuffers: nbufs=%d", nbufs);
86         if (nbufs != n) {
87                 ERROR("wrong number of bufs: %d vs %d", nbufs, n);
88                 return NULL;
89         }
91         bufs = calloc(nbufs, sizeof(struct buffer *));
93         for (i = 0; i < nbufs; i++) {
94                 struct buffer *buf;
95                 struct buffer_x11 *buf_x11;
97                 buf_x11 = calloc(1, sizeof(*buf_x11));
98                 if (!buf_x11) {
99                         ERROR("allocation failed");
100                         return NULL;
101                 }
103                 buf_x11->dri2buf = &dri2bufs[i];
105                 buf = &buf_x11->base;
107                 buf->fourcc = fourcc;
108                 buf->width = w;
109                 buf->height = h;
111                 for (j = 0; dri2bufs[i].names[j]; j++) {
112                         buf->bo[j] = omap_bo_from_name(disp->dev, dri2bufs[i].names[j]);
113                 }
114                 buf->nbo = j;
116                 bufs[i] = buf;
117         }
119         return bufs;
122 static int
123 post_buffer(struct display *disp, struct buffer *buf)
125         MSG("unimplemented");
126         return -1;
129 static int
130 post_vid_buffer(struct display *disp, struct buffer *buf,
131                 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
133         struct display_x11 *disp_x11 = to_display_x11(disp);
134         struct buffer_x11 *buf_x11 = to_buffer_x11(buf);
135         CARD64 count;
136         BoxRec b = {
137                         .x1 = x,
138                         .y1 = y,
139                         .x2 = x + w,
140                         .y2 = y + h,
141         };
143         DRI2SwapBuffersVid(disp_x11->dpy, disp_x11->win, 0, 0, 0, &count,
144                         buf_x11->dri2buf->attachment, &b);
145         MSG("DRI2SwapBuffersVid[%u]: count=%llu",
146                         buf_x11->dri2buf->attachment, count);
148         return 0;
151 void
152 disp_x11_usage(void)
154         MSG("X11 Display Options:");
155         MSG("\t-w WxH\tset window dimensions");
158 /*** Move these somewhere common ***/
159 #include <ctype.h>
160 static Bool is_fourcc(unsigned int val)
162         char *str = (char *)&val;
163         return isalnum(str[0]) && isalnum(str[1]) && isalnum(str[2]) && isalnum(str[3]);
165 #define ATOM(name) XInternAtom(dpy, name, False)
166 static inline void print_hex(int len, const unsigned char *val)
168         char buf[33];
169         int i, j;
170         for (i = 0; i < len; i += j) {
171                 for (j = 0; (j < 16) && ((i + j) < len); ++j)
172                         sprintf(&buf[j * 2], "%02x", val[i + j]);
173                 fprintf(stderr, "\t%s\n", buf);
174         }
176 /***********************************/
179 static Bool WireToEvent(Display *dpy, XExtDisplayInfo *info,
180                 XEvent *event, xEvent *wire)
182         switch ((wire->u.u.type & 0x7f) - info->codes->first_event) {
184         case DRI2_BufferSwapComplete:
185         {
186 //              xDRI2BufferSwapComplete *awire = (xDRI2BufferSwapComplete *)wire;
187                 MSG("BufferSwapComplete");
188                 return True;
189         }
190         default:
191                 /* client doesn't support server event */
192                 break;
193         }
195         return False;
198 static Status EventToWire(Display *dpy, XExtDisplayInfo *info,
199                 XEvent *event, xEvent *wire)
201    switch (event->type) {
202    default:
203       /* client doesn't support server event */
204       break;
205    }
207    return Success;
210 static const DRI2EventOps ops = {
211                 .WireToEvent = WireToEvent,
212                 .EventToWire = EventToWire,
213 };
215 struct display *
216 disp_x11_open(int argc, char **argv)
218         struct display_x11 *disp_x11 = NULL;
219         struct display *disp;
220         Display *dpy;
221         Window root, win;
222         drm_magic_t magic;
223         int eventBase, errorBase, major, minor;
224         char *driver, *device;
225         unsigned int nformats, *formats;
226         unsigned int i, width = 500, height = 500;
227         CARD32 *pval;
229         MSG("attempting to open X11 connection");
230         dpy = XOpenDisplay(NULL);
231         if (!dpy) {
232                 ERROR("Could not open display");
233                 goto no_x11;
234         }
236         if (!DRI2InitDisplay(dpy, &ops)) {
237                 ERROR("DRI2InitDisplay failed");
238                 goto no_x11;
239         }
241         if (!DRI2QueryExtension(dpy, &eventBase, &errorBase)) {
242                 ERROR("DRI2QueryExtension failed");
243                 goto no_x11;
244         }
246         MSG("DRI2QueryExtension: eventBase=%d, errorBase=%d", eventBase, errorBase);
248         if (!DRI2QueryVersion(dpy, &major, &minor)) {
249                 ERROR("DRI2QueryVersion failed");
250                 goto no_x11;
251         }
253         MSG("DRI2QueryVersion: major=%d, minor=%d", major, minor);
255         root = RootWindow(dpy, DefaultScreen(dpy));
257         if (!DRI2Connect(dpy, root, DRI2DriverDRI, &driver, &device)) {
258                 MSG("DRI2Connect failed");
259                 goto no_x11;
260         }
262         MSG("DRI2Connect: driver=%s, device=%s", driver, device);
264         disp_x11 = calloc(1, sizeof(*disp_x11));
265         if (!disp_x11) {
266                 ERROR("allocation failed");
267                 goto no_x11;
268         }
270         disp = &disp_x11->base;
272         if (!global_fd) {
273                 MSG("opening device: %s", device);
274                 global_fd = open(device, O_RDWR);
275                 if (global_fd < 0) {
276                         ERROR("could not open drm device: %s (%d)",
277                                         strerror(errno), errno);
278                         goto no_x11_free;
279                 }
281                 if (drmGetMagic(global_fd, &magic)) {
282                         ERROR("drmGetMagic failed");
283                         goto no_x11_free;
284                 }
286                 if (!DRI2Authenticate(dpy, root, magic)) {
287                         ERROR("DRI2Authenticate failed");
288                         goto no_x11_free;
289                 }
290         }
292         disp->fd = global_fd;
294         disp->dev = omap_device_new(disp->fd);
295         if (!disp->dev) {
296                 ERROR("couldn't create device");
297                 goto no_x11_free;
298         }
300         disp->get_buffers = get_buffers;
301         disp->get_vid_buffers = get_vid_buffers;
302         disp->post_buffer = post_buffer;
303         disp->post_vid_buffer = post_vid_buffer;
305         /* note: set args to NULL after we've parsed them so other modules know
306          * that it is already parsed (since the arg parsing is decentralized)
307          */
308         for (i = 1; i < argc; i++) {
309                 if (!argv[i]) {
310                         continue;
311                 }
312                 if (!strcmp("-w", argv[i])) {
313                         argv[i++] = NULL;
314                         if (sscanf(argv[i], "%dx%d", &width, &height) != 2) {
315                                 ERROR("invalid arg: %s", argv[i]);
316                                 goto no_x11_free;
317                         }
318                 } else {
319                         /* ignore */
320                         continue;
321                 }
322                 argv[i] = NULL;
323         }
325         win = XCreateSimpleWindow(dpy, RootWindow(dpy, 0), 1, 1,
326                         width, height, 0, BlackPixel (dpy, 0), BlackPixel(dpy, 0));
327         XMapWindow(dpy, win);
328         XFlush(dpy);
330         disp_x11->dpy = dpy;
331         disp_x11->win = win;
333         if (!DRI2GetFormats(dpy, RootWindow(dpy, DefaultScreen(dpy)),
334                         &nformats, &formats)) {
335                 ERROR("DRI2GetFormats failed");
336                 goto no_x11_free;
337         }
339         if (nformats == 0) {
340                 ERROR("no formats!");
341                 goto no_x11_free;
342         }
344         /* print out supported formats */
345         MSG("Found %d supported formats:", nformats);
346         for (i = 0; i < nformats; i++) {
347                 if (is_fourcc(formats[i])) {
348                         MSG("  %d: %08x (\"%.4s\")", i, formats[i], (char *)&formats[i]);
349                 } else {
350                         MSG("  %d: %08x (device dependent)", i, formats[i]);
351                 }
352         }
354         DRI2CreateDrawable(dpy, win);
356         /* check some attribute.. just to exercise the code-path: */
357         if (!DRI2GetAttribute(dpy, win, ATOM("XV_CSC_MATRIX"), &i, &pval)) {
358                 ERROR("DRI2GetAttribute failed");
359                 goto no_x11_free;
360         }
362         MSG("Got CSC matrix:");
363         print_hex(i*4, (const unsigned char *)pval);
365         // XXX
367         return disp;
369 no_x11_free:
370         XFree(driver);
371         XFree(device);
372 no_x11:
373         ERROR("unimplemented");
374         return NULL;