Merge commit 'origin/perrtblend'
[mesa.git] / src / glx / x11 / 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 #ifdef GLX_DIRECT_RENDERING
35
36 #define NEED_REPLIES
37 #include <stdio.h>
38 #include <X11/Xlibint.h>
39 #include <X11/extensions/Xext.h>
40 #include <X11/extensions/extutil.h>
41 #include <X11/extensions/dri2proto.h>
42 #include "xf86drm.h"
43 #include "dri2.h"
44 #include "glxclient.h"
45 #include "GL/glxext.h"
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 1, NULL)
85
86 static Bool
87 DRI2WireToEvent(Display *dpy, XEvent *event, xEvent *wire)
88 {
89 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
90 XExtDisplayInfo *glx_info = __glXFindDisplay(dpy);
91 static int glx_event_base;
92 static Bool found_glx_info = False;
93
94 XextCheckExtension(dpy, info, dri2ExtensionName, False);
95
96 switch ((wire->u.u.type & 0x7f) - info->codes->first_event) {
97 case DRI2_BufferSwapComplete:
98 {
99 GLXBufferSwapComplete *aevent = (GLXBufferSwapComplete *)event;
100 xDRI2BufferSwapComplete *awire = (xDRI2BufferSwapComplete *)wire;
101 aevent->serial = _XSetLastRequestRead(dpy, (xGenericReply *) wire);
102 aevent->type =
103 (glx_info->codes->first_event + GLX_BufferSwapComplete) & 0x75;
104 aevent->send_event = (awire->type & 0x80) != 0;
105 aevent->display = dpy;
106 aevent->drawable = awire->drawable;
107 switch (awire->event_type) {
108 case DRI2_EXCHANGE_COMPLETE:
109 aevent->event_type = GLX_EXCHANGE_COMPLETE_INTEL;
110 break;
111 case DRI2_BLIT_COMPLETE:
112 aevent->event_type = GLX_BLIT_COMPLETE_INTEL;
113 break;
114 case DRI2_FLIP_COMPLETE:
115 aevent->event_type = GLX_FLIP_COMPLETE_INTEL;
116 break;
117 default:
118 /* unknown swap completion type */
119 return False;
120 }
121 aevent->ust = ((CARD64)awire->ust_hi << 32) | awire->ust_lo;
122 aevent->msc = ((CARD64)awire->msc_hi << 32) | awire->msc_lo;
123 aevent->sbc = ((CARD64)awire->sbc_hi << 32) | awire->sbc_lo;
124 return True;
125 }
126 default:
127 /* client doesn't support server event */
128 break;
129 }
130
131 return False;
132 }
133
134 /* We don't actually support this. It doesn't make sense for clients to
135 * send each other DRI2 events.
136 */
137 static Status
138 DRI2EventToWire(Display *dpy, XEvent *event, xEvent *wire)
139 {
140 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
141
142 XextCheckExtension(dpy, info, dri2ExtensionName, False);
143
144 switch (event->type) {
145 default:
146 /* client doesn't support server event */
147 break;
148 }
149
150 return Success;
151 }
152
153 Bool
154 DRI2QueryExtension(Display * dpy, int *eventBase, int *errorBase)
155 {
156 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
157
158 if (XextHasExtension(info)) {
159 *eventBase = info->codes->first_event;
160 *errorBase = info->codes->first_error;
161 return True;
162 }
163
164 return False;
165 }
166
167 Bool
168 DRI2QueryVersion(Display * dpy, int *major, int *minor)
169 {
170 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
171 xDRI2QueryVersionReply rep;
172 xDRI2QueryVersionReq *req;
173
174 XextCheckExtension(dpy, info, dri2ExtensionName, False);
175
176 LockDisplay(dpy);
177 GetReq(DRI2QueryVersion, req);
178 req->reqType = info->codes->major_opcode;
179 req->dri2ReqType = X_DRI2QueryVersion;
180 req->majorVersion = DRI2_MAJOR;
181 req->minorVersion = DRI2_MINOR;
182 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
183 UnlockDisplay(dpy);
184 SyncHandle();
185 return False;
186 }
187 *major = rep.majorVersion;
188 *minor = rep.minorVersion;
189 UnlockDisplay(dpy);
190 SyncHandle();
191
192 return True;
193 }
194
195 Bool
196 DRI2Connect(Display * dpy, XID window, char **driverName, char **deviceName)
197 {
198 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
199 xDRI2ConnectReply rep;
200 xDRI2ConnectReq *req;
201
202 XextCheckExtension(dpy, info, dri2ExtensionName, False);
203
204 LockDisplay(dpy);
205 GetReq(DRI2Connect, req);
206 req->reqType = info->codes->major_opcode;
207 req->dri2ReqType = X_DRI2Connect;
208 req->window = window;
209 req->driverType = DRI2DriverDRI;
210 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
211 UnlockDisplay(dpy);
212 SyncHandle();
213 return False;
214 }
215
216 if (rep.driverNameLength == 0 && rep.deviceNameLength == 0) {
217 UnlockDisplay(dpy);
218 SyncHandle();
219 return False;
220 }
221
222 *driverName = Xmalloc(rep.driverNameLength + 1);
223 if (*driverName == NULL) {
224 _XEatData(dpy,
225 ((rep.driverNameLength + 3) & ~3) +
226 ((rep.deviceNameLength + 3) & ~3));
227 UnlockDisplay(dpy);
228 SyncHandle();
229 return False;
230 }
231 _XReadPad(dpy, *driverName, rep.driverNameLength);
232 (*driverName)[rep.driverNameLength] = '\0';
233
234 *deviceName = Xmalloc(rep.deviceNameLength + 1);
235 if (*deviceName == NULL) {
236 Xfree(*driverName);
237 _XEatData(dpy, ((rep.deviceNameLength + 3) & ~3));
238 UnlockDisplay(dpy);
239 SyncHandle();
240 return False;
241 }
242 _XReadPad(dpy, *deviceName, rep.deviceNameLength);
243 (*deviceName)[rep.deviceNameLength] = '\0';
244
245 UnlockDisplay(dpy);
246 SyncHandle();
247
248 return True;
249 }
250
251 Bool
252 DRI2Authenticate(Display * dpy, XID window, drm_magic_t magic)
253 {
254 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
255 xDRI2AuthenticateReq *req;
256 xDRI2AuthenticateReply rep;
257
258 XextCheckExtension(dpy, info, dri2ExtensionName, False);
259
260 LockDisplay(dpy);
261 GetReq(DRI2Authenticate, req);
262 req->reqType = info->codes->major_opcode;
263 req->dri2ReqType = X_DRI2Authenticate;
264 req->window = window;
265 req->magic = magic;
266
267 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
268 UnlockDisplay(dpy);
269 SyncHandle();
270 return False;
271 }
272
273 UnlockDisplay(dpy);
274 SyncHandle();
275
276 return rep.authenticated;
277 }
278
279 void
280 DRI2CreateDrawable(Display * dpy, XID drawable)
281 {
282 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
283 xDRI2CreateDrawableReq *req;
284
285 XextSimpleCheckExtension(dpy, info, dri2ExtensionName);
286
287 LockDisplay(dpy);
288 GetReq(DRI2CreateDrawable, req);
289 req->reqType = info->codes->major_opcode;
290 req->dri2ReqType = X_DRI2CreateDrawable;
291 req->drawable = drawable;
292 UnlockDisplay(dpy);
293 SyncHandle();
294 }
295
296 void
297 DRI2DestroyDrawable(Display * dpy, XID drawable)
298 {
299 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
300 xDRI2DestroyDrawableReq *req;
301
302 XextSimpleCheckExtension(dpy, info, dri2ExtensionName);
303
304 XSync(dpy, False);
305
306 LockDisplay(dpy);
307 GetReq(DRI2DestroyDrawable, req);
308 req->reqType = info->codes->major_opcode;
309 req->dri2ReqType = X_DRI2DestroyDrawable;
310 req->drawable = drawable;
311 UnlockDisplay(dpy);
312 SyncHandle();
313 }
314
315 DRI2Buffer *
316 DRI2GetBuffers(Display * dpy, XID drawable,
317 int *width, int *height,
318 unsigned int *attachments, int count, int *outCount)
319 {
320 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
321 xDRI2GetBuffersReply rep;
322 xDRI2GetBuffersReq *req;
323 DRI2Buffer *buffers;
324 xDRI2Buffer repBuffer;
325 CARD32 *p;
326 int i;
327
328 XextCheckExtension(dpy, info, dri2ExtensionName, False);
329
330 LockDisplay(dpy);
331 GetReqExtra(DRI2GetBuffers, count * 4, req);
332 req->reqType = info->codes->major_opcode;
333 req->dri2ReqType = X_DRI2GetBuffers;
334 req->drawable = drawable;
335 req->count = count;
336 p = (CARD32 *) & req[1];
337 for (i = 0; i < count; i++)
338 p[i] = attachments[i];
339
340 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
341 UnlockDisplay(dpy);
342 SyncHandle();
343 return NULL;
344 }
345
346 *width = rep.width;
347 *height = rep.height;
348 *outCount = rep.count;
349
350 buffers = Xmalloc(rep.count * sizeof buffers[0]);
351 if (buffers == NULL) {
352 _XEatData(dpy, rep.count * sizeof repBuffer);
353 UnlockDisplay(dpy);
354 SyncHandle();
355 return NULL;
356 }
357
358 for (i = 0; i < rep.count; i++) {
359 _XReadPad(dpy, (char *) &repBuffer, sizeof repBuffer);
360 buffers[i].attachment = repBuffer.attachment;
361 buffers[i].name = repBuffer.name;
362 buffers[i].pitch = repBuffer.pitch;
363 buffers[i].cpp = repBuffer.cpp;
364 buffers[i].flags = repBuffer.flags;
365 }
366
367 UnlockDisplay(dpy);
368 SyncHandle();
369
370 return buffers;
371 }
372
373
374 DRI2Buffer *
375 DRI2GetBuffersWithFormat(Display * dpy, XID drawable,
376 int *width, int *height,
377 unsigned int *attachments, int count, int *outCount)
378 {
379 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
380 xDRI2GetBuffersReply rep;
381 xDRI2GetBuffersReq *req;
382 DRI2Buffer *buffers;
383 xDRI2Buffer repBuffer;
384 CARD32 *p;
385 int i;
386
387 XextCheckExtension(dpy, info, dri2ExtensionName, False);
388
389 LockDisplay(dpy);
390 GetReqExtra(DRI2GetBuffers, count * (4 * 2), req);
391 req->reqType = info->codes->major_opcode;
392 req->dri2ReqType = X_DRI2GetBuffersWithFormat;
393 req->drawable = drawable;
394 req->count = count;
395 p = (CARD32 *) & req[1];
396 for (i = 0; i < (count * 2); i++)
397 p[i] = attachments[i];
398
399 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
400 UnlockDisplay(dpy);
401 SyncHandle();
402 return NULL;
403 }
404
405 *width = rep.width;
406 *height = rep.height;
407 *outCount = rep.count;
408
409 buffers = Xmalloc(rep.count * sizeof buffers[0]);
410 if (buffers == NULL) {
411 _XEatData(dpy, rep.count * sizeof repBuffer);
412 UnlockDisplay(dpy);
413 SyncHandle();
414 return NULL;
415 }
416
417 for (i = 0; i < rep.count; i++) {
418 _XReadPad(dpy, (char *) &repBuffer, sizeof repBuffer);
419 buffers[i].attachment = repBuffer.attachment;
420 buffers[i].name = repBuffer.name;
421 buffers[i].pitch = repBuffer.pitch;
422 buffers[i].cpp = repBuffer.cpp;
423 buffers[i].flags = repBuffer.flags;
424 }
425
426 UnlockDisplay(dpy);
427 SyncHandle();
428
429 return buffers;
430 }
431
432
433 void
434 DRI2CopyRegion(Display * dpy, XID drawable, XserverRegion region,
435 CARD32 dest, CARD32 src)
436 {
437 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
438 xDRI2CopyRegionReq *req;
439 xDRI2CopyRegionReply rep;
440
441 XextSimpleCheckExtension(dpy, info, dri2ExtensionName);
442
443 LockDisplay(dpy);
444 GetReq(DRI2CopyRegion, req);
445 req->reqType = info->codes->major_opcode;
446 req->dri2ReqType = X_DRI2CopyRegion;
447 req->drawable = drawable;
448 req->region = region;
449 req->dest = dest;
450 req->src = src;
451
452 _XReply(dpy, (xReply *) & rep, 0, xFalse);
453
454 UnlockDisplay(dpy);
455 SyncHandle();
456 }
457
458 static void
459 load_swap_req(xDRI2SwapBuffersReq *req, CARD64 target, CARD64 divisor,
460 CARD64 remainder)
461 {
462 req->target_msc_hi = target >> 32;
463 req->target_msc_lo = target & 0xffffffff;
464 req->divisor_hi = divisor >> 32;
465 req->divisor_lo = divisor & 0xffffffff;
466 req->remainder_hi = remainder >> 32;
467 req->remainder_lo = remainder & 0xffffffff;
468 }
469
470 static CARD64
471 vals_to_card64(CARD32 lo, CARD32 hi)
472 {
473 return (CARD64)hi << 32 | lo;
474 }
475
476 void DRI2SwapBuffers(Display *dpy, XID drawable, CARD64 target_msc,
477 CARD64 divisor, CARD64 remainder, CARD64 *count)
478 {
479 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
480 xDRI2SwapBuffersReq *req;
481 xDRI2SwapBuffersReply rep;
482
483 XextSimpleCheckExtension (dpy, info, dri2ExtensionName);
484
485 LockDisplay(dpy);
486 GetReq(DRI2SwapBuffers, req);
487 req->reqType = info->codes->major_opcode;
488 req->dri2ReqType = X_DRI2SwapBuffers;
489 req->drawable = drawable;
490 load_swap_req(req, target_msc, divisor, remainder);
491
492 _XReply(dpy, (xReply *)&rep, 0, xFalse);
493
494 *count = vals_to_card64(rep.swap_lo, rep.swap_hi);
495
496 UnlockDisplay(dpy);
497 SyncHandle();
498 }
499
500 Bool DRI2GetMSC(Display *dpy, XID drawable, CARD64 *ust, CARD64 *msc,
501 CARD64 *sbc)
502 {
503 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
504 xDRI2GetMSCReq *req;
505 xDRI2MSCReply rep;
506
507 XextCheckExtension (dpy, info, dri2ExtensionName, False);
508
509 LockDisplay(dpy);
510 GetReq(DRI2GetMSC, req);
511 req->reqType = info->codes->major_opcode;
512 req->dri2ReqType = X_DRI2GetMSC;
513 req->drawable = drawable;
514
515 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
516 UnlockDisplay(dpy);
517 SyncHandle();
518 return False;
519 }
520
521 *ust = vals_to_card64(rep.ust_lo, rep.ust_hi);
522 *msc = vals_to_card64(rep.msc_lo, rep.msc_hi);
523 *sbc = vals_to_card64(rep.sbc_lo, rep.sbc_hi);
524
525 UnlockDisplay(dpy);
526 SyncHandle();
527
528 return True;
529 }
530
531 static void
532 load_msc_req(xDRI2WaitMSCReq *req, CARD64 target, CARD64 divisor,
533 CARD64 remainder)
534 {
535 req->target_msc_hi = target >> 32;
536 req->target_msc_lo = target & 0xffffffff;
537 req->divisor_hi = divisor >> 32;
538 req->divisor_lo = divisor & 0xffffffff;
539 req->remainder_hi = remainder >> 32;
540 req->remainder_lo = remainder & 0xffffffff;
541 }
542
543 Bool DRI2WaitMSC(Display *dpy, XID drawable, CARD64 target_msc, CARD64 divisor,
544 CARD64 remainder, CARD64 *ust, CARD64 *msc, CARD64 *sbc)
545 {
546 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
547 xDRI2WaitMSCReq *req;
548 xDRI2MSCReply rep;
549
550 XextCheckExtension (dpy, info, dri2ExtensionName, False);
551
552 LockDisplay(dpy);
553 GetReq(DRI2WaitMSC, req);
554 req->reqType = info->codes->major_opcode;
555 req->dri2ReqType = X_DRI2WaitMSC;
556 req->drawable = drawable;
557 load_msc_req(req, target_msc, divisor, remainder);
558
559 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
560 UnlockDisplay(dpy);
561 SyncHandle();
562 return False;
563 }
564
565 *ust = ((CARD64)rep.ust_hi << 32) | (CARD64)rep.ust_lo;
566 *msc = ((CARD64)rep.msc_hi << 32) | (CARD64)rep.msc_lo;
567 *sbc = ((CARD64)rep.sbc_hi << 32) | (CARD64)rep.sbc_lo;
568
569 UnlockDisplay(dpy);
570 SyncHandle();
571
572 return True;
573 }
574
575 static void
576 load_sbc_req(xDRI2WaitSBCReq *req, CARD64 target)
577 {
578 req->target_sbc_hi = target >> 32;
579 req->target_sbc_lo = target & 0xffffffff;
580 }
581
582 Bool DRI2WaitSBC(Display *dpy, XID drawable, CARD64 target_sbc, CARD64 *ust,
583 CARD64 *msc, CARD64 *sbc)
584 {
585 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
586 xDRI2WaitSBCReq *req;
587 xDRI2MSCReply rep;
588
589 XextCheckExtension (dpy, info, dri2ExtensionName, False);
590
591 LockDisplay(dpy);
592 GetReq(DRI2WaitSBC, req);
593 req->reqType = info->codes->major_opcode;
594 req->dri2ReqType = X_DRI2WaitSBC;
595 req->drawable = drawable;
596 load_sbc_req(req, target_sbc);
597
598 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
599 UnlockDisplay(dpy);
600 SyncHandle();
601 return False;
602 }
603
604 *ust = ((CARD64)rep.ust_hi << 32) | rep.ust_lo;
605 *msc = ((CARD64)rep.msc_hi << 32) | rep.msc_lo;
606 *sbc = ((CARD64)rep.sbc_hi << 32) | rep.sbc_lo;
607
608 UnlockDisplay(dpy);
609 SyncHandle();
610
611 return True;
612 }
613
614 void DRI2SwapInterval(Display *dpy, XID drawable, int interval)
615 {
616 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
617 xDRI2SwapIntervalReq *req;
618
619 XextSimpleCheckExtension (dpy, info, dri2ExtensionName);
620
621 LockDisplay(dpy);
622 GetReq(DRI2SwapInterval, req);
623 req->reqType = info->codes->major_opcode;
624 req->dri2ReqType = X_DRI2SwapInterval;
625 req->drawable = drawable;
626 req->interval = interval;
627 UnlockDisplay(dpy);
628 SyncHandle();
629 }
630
631 #endif /* GLX_DIRECT_RENDERING */