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;
110 buf->multiplanar = false;
112 for (j = 0; dri2bufs[i].names[j]; j++) {
113 buf->bo[j] = omap_bo_from_name(disp->dev, dri2bufs[i].names[j]);
114 buf->pitches[j] = dri2bufs[i].pitch[j];
115 }
117 buf->nbo = j;
119 bufs[i] = buf;
120 }
122 return bufs;
123 }
125 static int
126 post_buffer(struct display *disp, struct buffer *buf)
127 {
128 MSG("unimplemented");
129 return -1;
130 }
132 static int
133 post_vid_buffer(struct display *disp, struct buffer *buf,
134 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
135 {
136 struct display_x11 *disp_x11 = to_display_x11(disp);
137 struct buffer_x11 *buf_x11 = to_buffer_x11(buf);
138 CARD64 count;
139 BoxRec b = {
140 .x1 = x,
141 .y1 = y,
142 .x2 = x + w,
143 .y2 = y + h,
144 };
146 DRI2SwapBuffersVid(disp_x11->dpy, disp_x11->win, 0, 0, 0, &count,
147 buf_x11->dri2buf->attachment, &b);
148 DBG("DRI2SwapBuffersVid[%u]: count=%llu",
149 buf_x11->dri2buf->attachment, count);
151 return 0;
152 }
154 static void
155 close_x11(struct display *disp)
156 {
157 struct display_x11 *disp_x11 = to_display_x11(disp);
158 XCloseDisplay(disp_x11->dpy);
159 }
161 void
162 disp_x11_usage(void)
163 {
164 MSG("X11 Display Options:");
165 MSG("\t-w WxH\tset window dimensions");
166 }
168 /*** Move these somewhere common ***/
169 #include <ctype.h>
170 static Bool is_fourcc(unsigned int val)
171 {
172 char *str = (char *)&val;
173 return isalnum(str[0]) && isalnum(str[1]) && isalnum(str[2]) && isalnum(str[3]);
174 }
175 #define ATOM(name) XInternAtom(dpy, name, False)
176 static inline void print_hex(int len, const unsigned char *val)
177 {
178 char buf[33];
179 int i, j;
180 for (i = 0; i < len; i += j) {
181 for (j = 0; (j < 16) && ((i + j) < len); ++j)
182 sprintf(&buf[j * 2], "%02x", val[i + j]);
183 fprintf(stderr, "\t%s\n", buf);
184 }
185 }
186 /***********************************/
189 static Bool WireToEvent(Display *dpy, XExtDisplayInfo *info,
190 XEvent *event, xEvent *wire)
191 {
192 switch ((wire->u.u.type & 0x7f) - info->codes->first_event) {
194 case DRI2_BufferSwapComplete:
195 {
196 // xDRI2BufferSwapComplete *awire = (xDRI2BufferSwapComplete *)wire;
197 DBG("BufferSwapComplete");
198 return True;
199 }
200 default:
201 /* client doesn't support server event */
202 break;
203 }
205 return False;
206 }
208 static Status EventToWire(Display *dpy, XExtDisplayInfo *info,
209 XEvent *event, xEvent *wire)
210 {
211 switch (event->type) {
212 default:
213 /* client doesn't support server event */
214 break;
215 }
217 return Success;
218 }
220 static const DRI2EventOps ops = {
221 .WireToEvent = WireToEvent,
222 .EventToWire = EventToWire,
223 };
225 struct display *
226 disp_x11_open(int argc, char **argv)
227 {
228 struct display_x11 *disp_x11 = NULL;
229 struct display *disp;
230 Display *dpy;
231 Window root, win;
232 drm_magic_t magic;
233 int eventBase, errorBase, major, minor;
234 char *driver, *device;
235 unsigned int nformats, *formats;
236 unsigned int i, width = 500, height = 500;
237 CARD32 *pval;
239 MSG("attempting to open X11 connection");
240 dpy = XOpenDisplay(NULL);
241 if (!dpy) {
242 ERROR("Could not open display");
243 goto no_x11;
244 }
246 if (!DRI2InitDisplay(dpy, &ops)) {
247 ERROR("DRI2InitDisplay failed");
248 goto no_x11;
249 }
251 if (!DRI2QueryExtension(dpy, &eventBase, &errorBase)) {
252 ERROR("DRI2QueryExtension failed");
253 goto no_x11;
254 }
256 MSG("DRI2QueryExtension: eventBase=%d, errorBase=%d", eventBase, errorBase);
258 if (!DRI2QueryVersion(dpy, &major, &minor)) {
259 ERROR("DRI2QueryVersion failed");
260 goto no_x11;
261 }
263 MSG("DRI2QueryVersion: major=%d, minor=%d", major, minor);
265 root = RootWindow(dpy, DefaultScreen(dpy));
267 if (!DRI2Connect(dpy, root, DRI2DriverDRI, &driver, &device)) {
268 MSG("DRI2Connect failed");
269 goto no_x11;
270 }
272 MSG("DRI2Connect: driver=%s, device=%s", driver, device);
274 disp_x11 = calloc(1, sizeof(*disp_x11));
275 if (!disp_x11) {
276 ERROR("allocation failed");
277 goto no_x11;
278 }
280 disp = &disp_x11->base;
282 if (!global_fd) {
283 MSG("opening device: %s", device);
284 global_fd = open(device, O_RDWR);
285 if (global_fd < 0) {
286 ERROR("could not open drm device: %s (%d)",
287 strerror(errno), errno);
288 goto no_x11_free;
289 }
291 if (drmGetMagic(global_fd, &magic)) {
292 ERROR("drmGetMagic failed");
293 goto no_x11_free;
294 }
296 if (!DRI2Authenticate(dpy, root, magic)) {
297 ERROR("DRI2Authenticate failed");
298 goto no_x11_free;
299 }
300 }
302 disp->fd = global_fd;
304 disp->dev = omap_device_new(disp->fd);
305 if (!disp->dev) {
306 ERROR("couldn't create device");
307 goto no_x11_free;
308 }
310 disp->get_buffers = get_buffers;
311 disp->get_vid_buffers = get_vid_buffers;
312 disp->post_buffer = post_buffer;
313 disp->post_vid_buffer = post_vid_buffer;
314 disp->close = close_x11;
315 disp->multiplanar = false;
317 /* note: set args to NULL after we've parsed them so other modules know
318 * that it is already parsed (since the arg parsing is decentralized)
319 */
320 for (i = 1; i < argc; i++) {
321 if (!argv[i]) {
322 continue;
323 }
324 if (!strcmp("-w", argv[i])) {
325 argv[i++] = NULL;
326 if (sscanf(argv[i], "%dx%d", &width, &height) != 2) {
327 ERROR("invalid arg: %s", argv[i]);
328 goto no_x11_free;
329 }
330 } else {
331 /* ignore */
332 continue;
333 }
334 argv[i] = NULL;
335 }
337 win = XCreateSimpleWindow(dpy, RootWindow(dpy, 0), 1, 1,
338 width, height, 0, BlackPixel (dpy, 0), BlackPixel(dpy, 0));
339 XMapWindow(dpy, win);
340 XFlush(dpy);
342 disp_x11->dpy = dpy;
343 disp_x11->win = win;
345 if (!DRI2GetFormats(dpy, RootWindow(dpy, DefaultScreen(dpy)),
346 &nformats, &formats)) {
347 ERROR("DRI2GetFormats failed");
348 goto no_x11_free;
349 }
351 if (nformats == 0) {
352 ERROR("no formats!");
353 goto no_x11_free;
354 }
356 /* print out supported formats */
357 MSG("Found %d supported formats:", nformats);
358 for (i = 0; i < nformats; i++) {
359 if (is_fourcc(formats[i])) {
360 MSG(" %d: %08x (\"%.4s\")", i, formats[i], (char *)&formats[i]);
361 } else {
362 MSG(" %d: %08x (device dependent)", i, formats[i]);
363 }
364 }
366 DRI2CreateDrawable(dpy, win);
368 /* check some attribute.. just to exercise the code-path: */
369 if (!DRI2GetAttribute(dpy, win, ATOM("XV_CSC_MATRIX"), &i, &pval)) {
370 ERROR("DRI2GetAttribute failed");
371 goto no_x11_free;
372 }
374 MSG("Got CSC matrix:");
375 print_hex(i*4, (const unsigned char *)pval);
377 // XXX
379 return disp;
381 no_x11_free:
382 XFree(driver);
383 XFree(device);
384 no_x11:
385 ERROR("unimplemented");
386 return NULL;
387 }