9ce633c40d486b60298e023e3a9d161ea26cfab0
[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
45 /* Allow the build to work with an older versions of dri2proto.h and
46 * dri2tokens.h.
47 */
48 #if DRI2_MINOR < 1
49 #undef DRI2_MINOR
50 #define DRI2_MINOR 1
51 #define X_DRI2GetBuffersWithFormat 7
52 #endif
53
54
55 static char dri2ExtensionName[] = DRI2_NAME;
56 static XExtensionInfo *dri2Info;
57 static XEXT_GENERATE_CLOSE_DISPLAY (DRI2CloseDisplay, dri2Info)
58
59 static /* const */ XExtensionHooks dri2ExtensionHooks = {
60 NULL, /* create_gc */
61 NULL, /* copy_gc */
62 NULL, /* flush_gc */
63 NULL, /* free_gc */
64 NULL, /* create_font */
65 NULL, /* free_font */
66 DRI2CloseDisplay, /* close_display */
67 NULL, /* wire_to_event */
68 NULL, /* event_to_wire */
69 NULL, /* error */
70 NULL, /* error_string */
71 };
72
73 static XEXT_GENERATE_FIND_DISPLAY (DRI2FindDisplay,
74 dri2Info,
75 dri2ExtensionName,
76 &dri2ExtensionHooks,
77 0, NULL)
78
79 Bool
80 DRI2QueryExtension(Display * dpy, int *eventBase, int *errorBase)
81 {
82 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
83
84 if (XextHasExtension(info)) {
85 *eventBase = info->codes->first_event;
86 *errorBase = info->codes->first_error;
87 return True;
88 }
89
90 return False;
91 }
92
93 Bool
94 DRI2QueryVersion(Display * dpy, int *major, int *minor)
95 {
96 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
97 xDRI2QueryVersionReply rep;
98 xDRI2QueryVersionReq *req;
99
100 XextCheckExtension(dpy, info, dri2ExtensionName, False);
101
102 LockDisplay(dpy);
103 GetReq(DRI2QueryVersion, req);
104 req->reqType = info->codes->major_opcode;
105 req->dri2ReqType = X_DRI2QueryVersion;
106 req->majorVersion = DRI2_MAJOR;
107 req->minorVersion = DRI2_MINOR;
108 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
109 UnlockDisplay(dpy);
110 SyncHandle();
111 return False;
112 }
113 *major = rep.majorVersion;
114 *minor = rep.minorVersion;
115 UnlockDisplay(dpy);
116 SyncHandle();
117
118 return True;
119 }
120
121 Bool
122 DRI2Connect(Display * dpy, XID window, char **driverName, char **deviceName)
123 {
124 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
125 xDRI2ConnectReply rep;
126 xDRI2ConnectReq *req;
127
128 XextCheckExtension(dpy, info, dri2ExtensionName, False);
129
130 LockDisplay(dpy);
131 GetReq(DRI2Connect, req);
132 req->reqType = info->codes->major_opcode;
133 req->dri2ReqType = X_DRI2Connect;
134 req->window = window;
135 req->driverType = DRI2DriverDRI;
136 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
137 UnlockDisplay(dpy);
138 SyncHandle();
139 return False;
140 }
141
142 if (rep.driverNameLength == 0 && rep.deviceNameLength == 0) {
143 UnlockDisplay(dpy);
144 SyncHandle();
145 return False;
146 }
147
148 *driverName = Xmalloc(rep.driverNameLength + 1);
149 if (*driverName == NULL) {
150 _XEatData(dpy,
151 ((rep.driverNameLength + 3) & ~3) +
152 ((rep.deviceNameLength + 3) & ~3));
153 UnlockDisplay(dpy);
154 SyncHandle();
155 return False;
156 }
157 _XReadPad(dpy, *driverName, rep.driverNameLength);
158 (*driverName)[rep.driverNameLength] = '\0';
159
160 *deviceName = Xmalloc(rep.deviceNameLength + 1);
161 if (*deviceName == NULL) {
162 Xfree(*driverName);
163 _XEatData(dpy, ((rep.deviceNameLength + 3) & ~3));
164 UnlockDisplay(dpy);
165 SyncHandle();
166 return False;
167 }
168 _XReadPad(dpy, *deviceName, rep.deviceNameLength);
169 (*deviceName)[rep.deviceNameLength] = '\0';
170
171 UnlockDisplay(dpy);
172 SyncHandle();
173
174 return True;
175 }
176
177 Bool
178 DRI2Authenticate(Display * dpy, XID window, drm_magic_t magic)
179 {
180 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
181 xDRI2AuthenticateReq *req;
182 xDRI2AuthenticateReply rep;
183
184 XextCheckExtension(dpy, info, dri2ExtensionName, False);
185
186 LockDisplay(dpy);
187 GetReq(DRI2Authenticate, req);
188 req->reqType = info->codes->major_opcode;
189 req->dri2ReqType = X_DRI2Authenticate;
190 req->window = window;
191 req->magic = magic;
192
193 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
194 UnlockDisplay(dpy);
195 SyncHandle();
196 return False;
197 }
198
199 UnlockDisplay(dpy);
200 SyncHandle();
201
202 return rep.authenticated;
203 }
204
205 void
206 DRI2CreateDrawable(Display * dpy, XID drawable)
207 {
208 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
209 xDRI2CreateDrawableReq *req;
210
211 XextSimpleCheckExtension(dpy, info, dri2ExtensionName);
212
213 LockDisplay(dpy);
214 GetReq(DRI2CreateDrawable, req);
215 req->reqType = info->codes->major_opcode;
216 req->dri2ReqType = X_DRI2CreateDrawable;
217 req->drawable = drawable;
218 UnlockDisplay(dpy);
219 SyncHandle();
220 }
221
222 void
223 DRI2DestroyDrawable(Display * dpy, XID drawable)
224 {
225 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
226 xDRI2DestroyDrawableReq *req;
227
228 XextSimpleCheckExtension(dpy, info, dri2ExtensionName);
229
230 XSync(dpy, False);
231
232 LockDisplay(dpy);
233 GetReq(DRI2DestroyDrawable, req);
234 req->reqType = info->codes->major_opcode;
235 req->dri2ReqType = X_DRI2DestroyDrawable;
236 req->drawable = drawable;
237 UnlockDisplay(dpy);
238 SyncHandle();
239 }
240
241 DRI2Buffer *
242 DRI2GetBuffers(Display * dpy, XID drawable,
243 int *width, int *height,
244 unsigned int *attachments, int count, int *outCount)
245 {
246 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
247 xDRI2GetBuffersReply rep;
248 xDRI2GetBuffersReq *req;
249 DRI2Buffer *buffers;
250 xDRI2Buffer repBuffer;
251 CARD32 *p;
252 int i;
253
254 XextCheckExtension(dpy, info, dri2ExtensionName, False);
255
256 LockDisplay(dpy);
257 GetReqExtra(DRI2GetBuffers, count * 4, req);
258 req->reqType = info->codes->major_opcode;
259 req->dri2ReqType = X_DRI2GetBuffers;
260 req->drawable = drawable;
261 req->count = count;
262 p = (CARD32 *) & req[1];
263 for (i = 0; i < count; i++)
264 p[i] = attachments[i];
265
266 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
267 UnlockDisplay(dpy);
268 SyncHandle();
269 return NULL;
270 }
271
272 *width = rep.width;
273 *height = rep.height;
274 *outCount = rep.count;
275
276 buffers = Xmalloc(rep.count * sizeof buffers[0]);
277 if (buffers == NULL) {
278 _XEatData(dpy, rep.count * sizeof repBuffer);
279 UnlockDisplay(dpy);
280 SyncHandle();
281 return NULL;
282 }
283
284 for (i = 0; i < rep.count; i++) {
285 _XReadPad(dpy, (char *) &repBuffer, sizeof repBuffer);
286 buffers[i].attachment = repBuffer.attachment;
287 buffers[i].name = repBuffer.name;
288 buffers[i].pitch = repBuffer.pitch;
289 buffers[i].cpp = repBuffer.cpp;
290 buffers[i].flags = repBuffer.flags;
291 }
292
293 UnlockDisplay(dpy);
294 SyncHandle();
295
296 return buffers;
297 }
298
299
300 DRI2Buffer *
301 DRI2GetBuffersWithFormat(Display * dpy, XID drawable,
302 int *width, int *height,
303 unsigned int *attachments, int count, int *outCount)
304 {
305 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
306 xDRI2GetBuffersReply rep;
307 xDRI2GetBuffersReq *req;
308 DRI2Buffer *buffers;
309 xDRI2Buffer repBuffer;
310 CARD32 *p;
311 int i;
312
313 XextCheckExtension(dpy, info, dri2ExtensionName, False);
314
315 LockDisplay(dpy);
316 GetReqExtra(DRI2GetBuffers, count * (4 * 2), req);
317 req->reqType = info->codes->major_opcode;
318 req->dri2ReqType = X_DRI2GetBuffersWithFormat;
319 req->drawable = drawable;
320 req->count = count;
321 p = (CARD32 *) & req[1];
322 for (i = 0; i < (count * 2); i++)
323 p[i] = attachments[i];
324
325 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
326 UnlockDisplay(dpy);
327 SyncHandle();
328 return NULL;
329 }
330
331 *width = rep.width;
332 *height = rep.height;
333 *outCount = rep.count;
334
335 buffers = Xmalloc(rep.count * sizeof buffers[0]);
336 if (buffers == NULL) {
337 _XEatData(dpy, rep.count * sizeof repBuffer);
338 UnlockDisplay(dpy);
339 SyncHandle();
340 return NULL;
341 }
342
343 for (i = 0; i < rep.count; i++) {
344 _XReadPad(dpy, (char *) &repBuffer, sizeof repBuffer);
345 buffers[i].attachment = repBuffer.attachment;
346 buffers[i].name = repBuffer.name;
347 buffers[i].pitch = repBuffer.pitch;
348 buffers[i].cpp = repBuffer.cpp;
349 buffers[i].flags = repBuffer.flags;
350 }
351
352 UnlockDisplay(dpy);
353 SyncHandle();
354
355 return buffers;
356 }
357
358
359 void
360 DRI2CopyRegion(Display * dpy, XID drawable, XserverRegion region,
361 CARD32 dest, CARD32 src)
362 {
363 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
364 xDRI2CopyRegionReq *req;
365 xDRI2CopyRegionReply rep;
366
367 XextSimpleCheckExtension(dpy, info, dri2ExtensionName);
368
369 LockDisplay(dpy);
370 GetReq(DRI2CopyRegion, req);
371 req->reqType = info->codes->major_opcode;
372 req->dri2ReqType = X_DRI2CopyRegion;
373 req->drawable = drawable;
374 req->region = region;
375 req->dest = dest;
376 req->src = src;
377
378 _XReply(dpy, (xReply *) & rep, 0, xFalse);
379
380 UnlockDisplay(dpy);
381 SyncHandle();
382 }
383
384 static void
385 load_swap_req(xDRI2SwapBuffersReq *req, CARD64 target, CARD64 divisor,
386 CARD64 remainder)
387 {
388 req->target_msc_hi = target >> 32;
389 req->target_msc_lo = target & 0xffffffff;
390 req->divisor_hi = divisor >> 32;
391 req->divisor_lo = divisor & 0xffffffff;
392 req->remainder_hi = remainder >> 32;
393 req->remainder_lo = remainder & 0xffffffff;
394 }
395
396 static CARD64
397 vals_to_card64(CARD32 lo, CARD32 hi)
398 {
399 return (CARD64)hi << 32 | lo;
400 }
401
402 void DRI2SwapBuffers(Display *dpy, XID drawable, CARD64 target_msc,
403 CARD64 divisor, CARD64 remainder, CARD64 *count)
404 {
405 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
406 xDRI2SwapBuffersReq *req;
407 xDRI2SwapBuffersReply rep;
408
409 XextSimpleCheckExtension (dpy, info, dri2ExtensionName);
410
411 LockDisplay(dpy);
412 GetReq(DRI2SwapBuffers, req);
413 req->reqType = info->codes->major_opcode;
414 req->dri2ReqType = X_DRI2SwapBuffers;
415 req->drawable = drawable;
416 load_swap_req(req, target_msc, divisor, remainder);
417
418 _XReply(dpy, (xReply *)&rep, 0, xFalse);
419
420 *count = vals_to_card64(rep.swap_lo, rep.swap_hi);
421
422 UnlockDisplay(dpy);
423 SyncHandle();
424 }
425
426 Bool DRI2GetMSC(Display *dpy, XID drawable, CARD64 *ust, CARD64 *msc,
427 CARD64 *sbc)
428 {
429 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
430 xDRI2GetMSCReq *req;
431 xDRI2MSCReply rep;
432
433 XextCheckExtension (dpy, info, dri2ExtensionName, False);
434
435 LockDisplay(dpy);
436 GetReq(DRI2GetMSC, req);
437 req->reqType = info->codes->major_opcode;
438 req->dri2ReqType = X_DRI2GetMSC;
439 req->drawable = drawable;
440
441 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
442 UnlockDisplay(dpy);
443 SyncHandle();
444 return False;
445 }
446
447 *ust = vals_to_card64(rep.ust_lo, rep.ust_hi);
448 *msc = vals_to_card64(rep.msc_lo, rep.msc_hi);
449 *sbc = vals_to_card64(rep.sbc_lo, rep.sbc_hi);
450
451 UnlockDisplay(dpy);
452 SyncHandle();
453
454 return True;
455 }
456
457 static void
458 load_msc_req(xDRI2WaitMSCReq *req, CARD64 target, CARD64 divisor,
459 CARD64 remainder)
460 {
461 req->target_msc_hi = target >> 32;
462 req->target_msc_lo = target & 0xffffffff;
463 req->divisor_hi = divisor >> 32;
464 req->divisor_lo = divisor & 0xffffffff;
465 req->remainder_hi = remainder >> 32;
466 req->remainder_lo = remainder & 0xffffffff;
467 }
468
469 Bool DRI2WaitMSC(Display *dpy, XID drawable, CARD64 target_msc, CARD64 divisor,
470 CARD64 remainder, CARD64 *ust, CARD64 *msc, CARD64 *sbc)
471 {
472 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
473 xDRI2WaitMSCReq *req;
474 xDRI2MSCReply rep;
475
476 XextCheckExtension (dpy, info, dri2ExtensionName, False);
477
478 LockDisplay(dpy);
479 GetReq(DRI2WaitMSC, req);
480 req->reqType = info->codes->major_opcode;
481 req->dri2ReqType = X_DRI2WaitMSC;
482 req->drawable = drawable;
483 load_msc_req(req, target_msc, divisor, remainder);
484
485 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
486 UnlockDisplay(dpy);
487 SyncHandle();
488 return False;
489 }
490
491 *ust = ((CARD64)rep.ust_hi << 32) | (CARD64)rep.ust_lo;
492 *msc = ((CARD64)rep.msc_hi << 32) | (CARD64)rep.msc_lo;
493 *sbc = ((CARD64)rep.sbc_hi << 32) | (CARD64)rep.sbc_lo;
494
495 UnlockDisplay(dpy);
496 SyncHandle();
497
498 return True;
499 }
500
501 static void
502 load_sbc_req(xDRI2WaitSBCReq *req, CARD64 target)
503 {
504 req->target_sbc_hi = target >> 32;
505 req->target_sbc_lo = target & 0xffffffff;
506 }
507
508 Bool DRI2WaitSBC(Display *dpy, XID drawable, CARD64 target_sbc, CARD64 *ust,
509 CARD64 *msc, CARD64 *sbc)
510 {
511 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
512 xDRI2WaitSBCReq *req;
513 xDRI2MSCReply rep;
514
515 XextCheckExtension (dpy, info, dri2ExtensionName, False);
516
517 LockDisplay(dpy);
518 GetReq(DRI2WaitSBC, req);
519 req->reqType = info->codes->major_opcode;
520 req->dri2ReqType = X_DRI2WaitSBC;
521 req->drawable = drawable;
522 load_sbc_req(req, target_sbc);
523
524 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
525 UnlockDisplay(dpy);
526 SyncHandle();
527 return False;
528 }
529
530 *ust = ((CARD64)rep.ust_hi << 32) | rep.ust_lo;
531 *msc = ((CARD64)rep.msc_hi << 32) | rep.msc_lo;
532 *sbc = ((CARD64)rep.sbc_hi << 32) | rep.sbc_lo;
533
534 UnlockDisplay(dpy);
535 SyncHandle();
536
537 return True;
538 }
539
540 void DRI2SwapInterval(Display *dpy, XID drawable, int interval)
541 {
542 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
543 xDRI2SwapIntervalReq *req;
544
545 XextSimpleCheckExtension (dpy, info, dri2ExtensionName);
546
547 LockDisplay(dpy);
548 GetReq(DRI2SwapInterval, req);
549 req->reqType = info->codes->major_opcode;
550 req->dri2ReqType = X_DRI2SwapInterval;
551 req->drawable = drawable;
552 req->interval = interval;
553 UnlockDisplay(dpy);
554 SyncHandle();
555 }
556
557 #endif /* GLX_DIRECT_RENDERING */