12ed6d0ab740d74ff881bc1b9f8c953d1d16f638
[mesa.git] / src / gallium / winsys / g3dvl / dri / dri2.c
1 /*
2 * Copyright © 2008 Red Hat, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Soft-
6 * ware"), to deal in the Software without restriction, including without
7 * limitation the rights to use, copy, modify, merge, publish, distribute,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, provided that the above copyright
10 * notice(s) and this permission notice appear in all copies of the Soft-
11 * ware and that both the above copyright notice(s) and this permission
12 * notice appear in supporting documentation.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
16 * ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY
17 * RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN
18 * THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSE-
19 * QUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFOR-
22 * MANCE OF THIS SOFTWARE.
23 *
24 * Except as contained in this notice, the name of a copyright holder shall
25 * not be used in advertising or otherwise to promote the sale, use or
26 * other dealings in this Software without prior written authorization of
27 * the copyright holder.
28 *
29 * Authors:
30 * Kristian Høgsberg (krh@redhat.com)
31 */
32
33
34 #define NEED_REPLIES
35 #include <stdio.h>
36 #include <X11/Xlibint.h>
37 #include <X11/extensions/Xext.h>
38 #include <X11/extensions/extutil.h>
39 #include <X11/extensions/dri2proto.h>
40 #include "xf86drm.h"
41 #include "dri2.h"
42 #if 0
43 #include "glxclient.h"
44 #include "GL/glxext.h"
45 #endif
46
47 /* Allow the build to work with an older versions of dri2proto.h and
48 * dri2tokens.h.
49 */
50 #if DRI2_MINOR < 1
51 #undef DRI2_MINOR
52 #define DRI2_MINOR 1
53 #define X_DRI2GetBuffersWithFormat 7
54 #endif
55
56
57 static char dri2ExtensionName[] = DRI2_NAME;
58 static XExtensionInfo *dri2Info;
59 static XEXT_GENERATE_CLOSE_DISPLAY (DRI2CloseDisplay, dri2Info)
60
61 static Bool
62 DRI2WireToEvent(Display *dpy, XEvent *event, xEvent *wire);
63 static Status
64 DRI2EventToWire(Display *dpy, XEvent *event, xEvent *wire);
65
66 static /* const */ XExtensionHooks dri2ExtensionHooks = {
67 NULL, /* create_gc */
68 NULL, /* copy_gc */
69 NULL, /* flush_gc */
70 NULL, /* free_gc */
71 NULL, /* create_font */
72 NULL, /* free_font */
73 DRI2CloseDisplay, /* close_display */
74 DRI2WireToEvent, /* wire_to_event */
75 DRI2EventToWire, /* event_to_wire */
76 NULL, /* error */
77 NULL, /* error_string */
78 };
79
80 static XEXT_GENERATE_FIND_DISPLAY (DRI2FindDisplay,
81 dri2Info,
82 dri2ExtensionName,
83 &dri2ExtensionHooks,
84 0, NULL)
85
86 static Bool
87 DRI2WireToEvent(Display *dpy, XEvent *event, xEvent *wire)
88 {
89 #if 0
90 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
91 XExtDisplayInfo *glx_info = __glXFindDisplay(dpy);
92
93 XextCheckExtension(dpy, info, dri2ExtensionName, False);
94
95 switch ((wire->u.u.type & 0x7f) - info->codes->first_event) {
96
97 #ifdef X_DRI2SwapBuffers
98 case DRI2_BufferSwapComplete:
99 {
100 GLXBufferSwapComplete *aevent = (GLXBufferSwapComplete *)event;
101 xDRI2BufferSwapComplete *awire = (xDRI2BufferSwapComplete *)wire;
102 aevent->serial = _XSetLastRequestRead(dpy, (xGenericReply *) wire);
103 aevent->type =
104 (glx_info->codes->first_event + GLX_BufferSwapComplete) & 0x75;
105 aevent->send_event = (awire->type & 0x80) != 0;
106 aevent->display = dpy;
107 aevent->drawable = awire->drawable;
108 switch (awire->event_type) {
109 case DRI2_EXCHANGE_COMPLETE:
110 aevent->event_type = GLX_EXCHANGE_COMPLETE_INTEL;
111 break;
112 case DRI2_BLIT_COMPLETE:
113 aevent->event_type = GLX_BLIT_COMPLETE_INTEL;
114 break;
115 case DRI2_FLIP_COMPLETE:
116 aevent->event_type = GLX_FLIP_COMPLETE_INTEL;
117 break;
118 default:
119 /* unknown swap completion type */
120 return False;
121 }
122 aevent->ust = ((CARD64)awire->ust_hi << 32) | awire->ust_lo;
123 aevent->msc = ((CARD64)awire->msc_hi << 32) | awire->msc_lo;
124 aevent->sbc = ((CARD64)awire->sbc_hi << 32) | awire->sbc_lo;
125 return True;
126 }
127 #endif
128 #ifdef DRI2_InvalidateBuffers
129 case DRI2_InvalidateBuffers:
130 {
131 xDRI2InvalidateBuffers *awire = (xDRI2InvalidateBuffers *)wire;
132
133 dri2InvalidateBuffers(dpy, awire->drawable);
134 return False;
135 }
136 #endif
137 default:
138 /* client doesn't support server event */
139 break;
140 }
141 #endif
142 return False;
143 }
144
145 /* We don't actually support this. It doesn't make sense for clients to
146 * send each other DRI2 events.
147 */
148 static Status
149 DRI2EventToWire(Display *dpy, XEvent *event, xEvent *wire)
150 {
151 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
152
153 XextCheckExtension(dpy, info, dri2ExtensionName, False);
154
155 switch (event->type) {
156 default:
157 /* client doesn't support server event */
158 break;
159 }
160
161 return Success;
162 }
163
164 Bool
165 DRI2QueryExtension(Display * dpy, int *eventBase, int *errorBase)
166 {
167 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
168
169 if (XextHasExtension(info)) {
170 *eventBase = info->codes->first_event;
171 *errorBase = info->codes->first_error;
172 return True;
173 }
174
175 return False;
176 }
177
178 Bool
179 DRI2QueryVersion(Display * dpy, int *major, int *minor)
180 {
181 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
182 xDRI2QueryVersionReply rep;
183 xDRI2QueryVersionReq *req;
184 int i, nevents;
185
186 XextCheckExtension(dpy, info, dri2ExtensionName, False);
187
188 LockDisplay(dpy);
189 GetReq(DRI2QueryVersion, req);
190 req->reqType = info->codes->major_opcode;
191 req->dri2ReqType = X_DRI2QueryVersion;
192 req->majorVersion = DRI2_MAJOR;
193 req->minorVersion = DRI2_MINOR;
194 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
195 UnlockDisplay(dpy);
196 SyncHandle();
197 return False;
198 }
199 *major = rep.majorVersion;
200 *minor = rep.minorVersion;
201 UnlockDisplay(dpy);
202 SyncHandle();
203
204 switch (rep.minorVersion) {
205 case 1:
206 nevents = 0;
207 break;
208 case 2:
209 nevents = 1;
210 break;
211 case 3:
212 default:
213 nevents = 2;
214 break;
215 }
216
217 for (i = 0; i < nevents; i++) {
218 XESetWireToEvent (dpy, info->codes->first_event + i, DRI2WireToEvent);
219 XESetEventToWire (dpy, info->codes->first_event + i, DRI2EventToWire);
220 }
221
222 return True;
223 }
224
225 Bool
226 DRI2Connect(Display * dpy, XID window, char **driverName, char **deviceName)
227 {
228 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
229 xDRI2ConnectReply rep;
230 xDRI2ConnectReq *req;
231
232 XextCheckExtension(dpy, info, dri2ExtensionName, False);
233
234 LockDisplay(dpy);
235 GetReq(DRI2Connect, req);
236 req->reqType = info->codes->major_opcode;
237 req->dri2ReqType = X_DRI2Connect;
238 req->window = window;
239 req->driverType = DRI2DriverDRI;
240 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
241 UnlockDisplay(dpy);
242 SyncHandle();
243 return False;
244 }
245
246 if (rep.driverNameLength == 0 && rep.deviceNameLength == 0) {
247 UnlockDisplay(dpy);
248 SyncHandle();
249 return False;
250 }
251
252 *driverName = Xmalloc(rep.driverNameLength + 1);
253 if (*driverName == NULL) {
254 _XEatData(dpy,
255 ((rep.driverNameLength + 3) & ~3) +
256 ((rep.deviceNameLength + 3) & ~3));
257 UnlockDisplay(dpy);
258 SyncHandle();
259 return False;
260 }
261 _XReadPad(dpy, *driverName, rep.driverNameLength);
262 (*driverName)[rep.driverNameLength] = '\0';
263
264 *deviceName = Xmalloc(rep.deviceNameLength + 1);
265 if (*deviceName == NULL) {
266 Xfree(*driverName);
267 _XEatData(dpy, ((rep.deviceNameLength + 3) & ~3));
268 UnlockDisplay(dpy);
269 SyncHandle();
270 return False;
271 }
272 _XReadPad(dpy, *deviceName, rep.deviceNameLength);
273 (*deviceName)[rep.deviceNameLength] = '\0';
274
275 UnlockDisplay(dpy);
276 SyncHandle();
277
278 return True;
279 }
280
281 Bool
282 DRI2Authenticate(Display * dpy, XID window, drm_magic_t magic)
283 {
284 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
285 xDRI2AuthenticateReq *req;
286 xDRI2AuthenticateReply rep;
287
288 XextCheckExtension(dpy, info, dri2ExtensionName, False);
289
290 LockDisplay(dpy);
291 GetReq(DRI2Authenticate, req);
292 req->reqType = info->codes->major_opcode;
293 req->dri2ReqType = X_DRI2Authenticate;
294 req->window = window;
295 req->magic = magic;
296
297 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
298 UnlockDisplay(dpy);
299 SyncHandle();
300 return False;
301 }
302
303 UnlockDisplay(dpy);
304 SyncHandle();
305
306 return rep.authenticated;
307 }
308
309 void
310 DRI2CreateDrawable(Display * dpy, XID drawable)
311 {
312 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
313 xDRI2CreateDrawableReq *req;
314
315 XextSimpleCheckExtension(dpy, info, dri2ExtensionName);
316
317 LockDisplay(dpy);
318 GetReq(DRI2CreateDrawable, req);
319 req->reqType = info->codes->major_opcode;
320 req->dri2ReqType = X_DRI2CreateDrawable;
321 req->drawable = drawable;
322 UnlockDisplay(dpy);
323 SyncHandle();
324 }
325
326 void
327 DRI2DestroyDrawable(Display * dpy, XID drawable)
328 {
329 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
330 xDRI2DestroyDrawableReq *req;
331
332 XextSimpleCheckExtension(dpy, info, dri2ExtensionName);
333
334 XSync(dpy, False);
335
336 LockDisplay(dpy);
337 GetReq(DRI2DestroyDrawable, req);
338 req->reqType = info->codes->major_opcode;
339 req->dri2ReqType = X_DRI2DestroyDrawable;
340 req->drawable = drawable;
341 UnlockDisplay(dpy);
342 SyncHandle();
343 }
344
345 DRI2Buffer *
346 DRI2GetBuffers(Display * dpy, XID drawable,
347 int *width, int *height,
348 unsigned int *attachments, int count, int *outCount)
349 {
350 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
351 xDRI2GetBuffersReply rep;
352 xDRI2GetBuffersReq *req;
353 DRI2Buffer *buffers;
354 xDRI2Buffer repBuffer;
355 CARD32 *p;
356 int i;
357
358 XextCheckExtension(dpy, info, dri2ExtensionName, False);
359
360 LockDisplay(dpy);
361 GetReqExtra(DRI2GetBuffers, count * 4, req);
362 req->reqType = info->codes->major_opcode;
363 req->dri2ReqType = X_DRI2GetBuffers;
364 req->drawable = drawable;
365 req->count = count;
366 p = (CARD32 *) & req[1];
367 for (i = 0; i < count; i++)
368 p[i] = attachments[i];
369
370 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
371 UnlockDisplay(dpy);
372 SyncHandle();
373 return NULL;
374 }
375
376 *width = rep.width;
377 *height = rep.height;
378 *outCount = rep.count;
379
380 buffers = Xmalloc(rep.count * sizeof buffers[0]);
381 if (buffers == NULL) {
382 _XEatData(dpy, rep.count * sizeof repBuffer);
383 UnlockDisplay(dpy);
384 SyncHandle();
385 return NULL;
386 }
387
388 for (i = 0; i < rep.count; i++) {
389 _XReadPad(dpy, (char *) &repBuffer, sizeof repBuffer);
390 buffers[i].attachment = repBuffer.attachment;
391 buffers[i].name = repBuffer.name;
392 buffers[i].pitch = repBuffer.pitch;
393 buffers[i].cpp = repBuffer.cpp;
394 buffers[i].flags = repBuffer.flags;
395 }
396
397 UnlockDisplay(dpy);
398 SyncHandle();
399
400 return buffers;
401 }
402
403
404 DRI2Buffer *
405 DRI2GetBuffersWithFormat(Display * dpy, XID drawable,
406 int *width, int *height,
407 unsigned int *attachments, int count, int *outCount)
408 {
409 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
410 xDRI2GetBuffersReply rep;
411 xDRI2GetBuffersReq *req;
412 DRI2Buffer *buffers;
413 xDRI2Buffer repBuffer;
414 CARD32 *p;
415 int i;
416
417 XextCheckExtension(dpy, info, dri2ExtensionName, False);
418
419 LockDisplay(dpy);
420 GetReqExtra(DRI2GetBuffers, count * (4 * 2), req);
421 req->reqType = info->codes->major_opcode;
422 req->dri2ReqType = X_DRI2GetBuffersWithFormat;
423 req->drawable = drawable;
424 req->count = count;
425 p = (CARD32 *) & req[1];
426 for (i = 0; i < (count * 2); i++)
427 p[i] = attachments[i];
428
429 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
430 UnlockDisplay(dpy);
431 SyncHandle();
432 return NULL;
433 }
434
435 *width = rep.width;
436 *height = rep.height;
437 *outCount = rep.count;
438
439 buffers = Xmalloc(rep.count * sizeof buffers[0]);
440 if (buffers == NULL) {
441 _XEatData(dpy, rep.count * sizeof repBuffer);
442 UnlockDisplay(dpy);
443 SyncHandle();
444 return NULL;
445 }
446
447 for (i = 0; i < rep.count; i++) {
448 _XReadPad(dpy, (char *) &repBuffer, sizeof repBuffer);
449 buffers[i].attachment = repBuffer.attachment;
450 buffers[i].name = repBuffer.name;
451 buffers[i].pitch = repBuffer.pitch;
452 buffers[i].cpp = repBuffer.cpp;
453 buffers[i].flags = repBuffer.flags;
454 }
455
456 UnlockDisplay(dpy);
457 SyncHandle();
458
459 return buffers;
460 }
461
462
463 void
464 DRI2CopyRegion(Display * dpy, XID drawable, XserverRegion region,
465 CARD32 dest, CARD32 src)
466 {
467 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
468 xDRI2CopyRegionReq *req;
469
470 XextSimpleCheckExtension(dpy, info, dri2ExtensionName);
471
472 LockDisplay(dpy);
473 GetReq(DRI2CopyRegion, req);
474 req->reqType = info->codes->major_opcode;
475 req->dri2ReqType = X_DRI2CopyRegion;
476 req->drawable = drawable;
477 req->region = region;
478 req->dest = dest;
479 req->src = src;
480
481 UnlockDisplay(dpy);
482 SyncHandle();
483 }
484
485 #ifdef X_DRI2SwapBuffers
486 static void
487 load_swap_req(xDRI2SwapBuffersReq *req, CARD64 target, CARD64 divisor,
488 CARD64 remainder)
489 {
490 req->target_msc_hi = target >> 32;
491 req->target_msc_lo = target & 0xffffffff;
492 req->divisor_hi = divisor >> 32;
493 req->divisor_lo = divisor & 0xffffffff;
494 req->remainder_hi = remainder >> 32;
495 req->remainder_lo = remainder & 0xffffffff;
496 }
497
498 static CARD64
499 vals_to_card64(CARD32 lo, CARD32 hi)
500 {
501 return (CARD64)hi << 32 | lo;
502 }
503
504 void DRI2SwapBuffers(Display *dpy, XID drawable, CARD64 target_msc,
505 CARD64 divisor, CARD64 remainder, CARD64 *count)
506 {
507 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
508 xDRI2SwapBuffersReq *req;
509 xDRI2SwapBuffersReply rep;
510
511 XextSimpleCheckExtension (dpy, info, dri2ExtensionName);
512
513 LockDisplay(dpy);
514 GetReq(DRI2SwapBuffers, req);
515 req->reqType = info->codes->major_opcode;
516 req->dri2ReqType = X_DRI2SwapBuffers;
517 req->drawable = drawable;
518 load_swap_req(req, target_msc, divisor, remainder);
519
520 _XReply(dpy, (xReply *)&rep, 0, xFalse);
521
522 *count = vals_to_card64(rep.swap_lo, rep.swap_hi);
523
524 UnlockDisplay(dpy);
525 SyncHandle();
526 }
527 #endif
528
529 #ifdef X_DRI2GetMSC
530 Bool DRI2GetMSC(Display *dpy, XID drawable, CARD64 *ust, CARD64 *msc,
531 CARD64 *sbc)
532 {
533 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
534 xDRI2GetMSCReq *req;
535 xDRI2MSCReply rep;
536
537 XextCheckExtension (dpy, info, dri2ExtensionName, False);
538
539 LockDisplay(dpy);
540 GetReq(DRI2GetMSC, req);
541 req->reqType = info->codes->major_opcode;
542 req->dri2ReqType = X_DRI2GetMSC;
543 req->drawable = drawable;
544
545 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
546 UnlockDisplay(dpy);
547 SyncHandle();
548 return False;
549 }
550
551 *ust = vals_to_card64(rep.ust_lo, rep.ust_hi);
552 *msc = vals_to_card64(rep.msc_lo, rep.msc_hi);
553 *sbc = vals_to_card64(rep.sbc_lo, rep.sbc_hi);
554
555 UnlockDisplay(dpy);
556 SyncHandle();
557
558 return True;
559 }
560 #endif
561
562 #ifdef X_DRI2WaitMSC
563 static void
564 load_msc_req(xDRI2WaitMSCReq *req, CARD64 target, CARD64 divisor,
565 CARD64 remainder)
566 {
567 req->target_msc_hi = target >> 32;
568 req->target_msc_lo = target & 0xffffffff;
569 req->divisor_hi = divisor >> 32;
570 req->divisor_lo = divisor & 0xffffffff;
571 req->remainder_hi = remainder >> 32;
572 req->remainder_lo = remainder & 0xffffffff;
573 }
574
575 Bool DRI2WaitMSC(Display *dpy, XID drawable, CARD64 target_msc, CARD64 divisor,
576 CARD64 remainder, CARD64 *ust, CARD64 *msc, CARD64 *sbc)
577 {
578 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
579 xDRI2WaitMSCReq *req;
580 xDRI2MSCReply rep;
581
582 XextCheckExtension (dpy, info, dri2ExtensionName, False);
583
584 LockDisplay(dpy);
585 GetReq(DRI2WaitMSC, req);
586 req->reqType = info->codes->major_opcode;
587 req->dri2ReqType = X_DRI2WaitMSC;
588 req->drawable = drawable;
589 load_msc_req(req, target_msc, divisor, remainder);
590
591 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
592 UnlockDisplay(dpy);
593 SyncHandle();
594 return False;
595 }
596
597 *ust = ((CARD64)rep.ust_hi << 32) | (CARD64)rep.ust_lo;
598 *msc = ((CARD64)rep.msc_hi << 32) | (CARD64)rep.msc_lo;
599 *sbc = ((CARD64)rep.sbc_hi << 32) | (CARD64)rep.sbc_lo;
600
601 UnlockDisplay(dpy);
602 SyncHandle();
603
604 return True;
605 }
606 #endif
607
608 #ifdef X_DRI2WaitSBC
609 static void
610 load_sbc_req(xDRI2WaitSBCReq *req, CARD64 target)
611 {
612 req->target_sbc_hi = target >> 32;
613 req->target_sbc_lo = target & 0xffffffff;
614 }
615
616 Bool DRI2WaitSBC(Display *dpy, XID drawable, CARD64 target_sbc, CARD64 *ust,
617 CARD64 *msc, CARD64 *sbc)
618 {
619 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
620 xDRI2WaitSBCReq *req;
621 xDRI2MSCReply rep;
622
623 XextCheckExtension (dpy, info, dri2ExtensionName, False);
624
625 LockDisplay(dpy);
626 GetReq(DRI2WaitSBC, req);
627 req->reqType = info->codes->major_opcode;
628 req->dri2ReqType = X_DRI2WaitSBC;
629 req->drawable = drawable;
630 load_sbc_req(req, target_sbc);
631
632 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
633 UnlockDisplay(dpy);
634 SyncHandle();
635 return False;
636 }
637
638 *ust = ((CARD64)rep.ust_hi << 32) | rep.ust_lo;
639 *msc = ((CARD64)rep.msc_hi << 32) | rep.msc_lo;
640 *sbc = ((CARD64)rep.sbc_hi << 32) | rep.sbc_lo;
641
642 UnlockDisplay(dpy);
643 SyncHandle();
644
645 return True;
646 }
647 #endif
648
649 #ifdef X_DRI2SwapInterval
650 void DRI2SwapInterval(Display *dpy, XID drawable, int interval)
651 {
652 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
653 xDRI2SwapIntervalReq *req;
654
655 XextSimpleCheckExtension (dpy, info, dri2ExtensionName);
656
657 LockDisplay(dpy);
658 GetReq(DRI2SwapInterval, req);
659 req->reqType = info->codes->major_opcode;
660 req->dri2ReqType = X_DRI2SwapInterval;
661 req->drawable = drawable;
662 req->interval = interval;
663 UnlockDisplay(dpy);
664 SyncHandle();
665 }
666 #endif