glx: Add missing null check in DRI2WireToEvent
[mesa.git] / src / glx / 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 #include <stdio.h>
37 #include <X11/Xlibint.h>
38 #include <X11/extensions/Xext.h>
39 #include <X11/extensions/extutil.h>
40 #include <X11/extensions/dri2proto.h>
41 #include "xf86drm.h"
42 #include "dri2.h"
43 #include "glxclient.h"
44 #include "GL/glxext.h"
45
46 /* Allow the build to work with an older versions of dri2proto.h and
47 * dri2tokens.h.
48 */
49 #if DRI2_MINOR < 1
50 #undef DRI2_MINOR
51 #define DRI2_MINOR 1
52 #define X_DRI2GetBuffersWithFormat 7
53 #endif
54
55
56 static char dri2ExtensionName[] = DRI2_NAME;
57 static XExtensionInfo *dri2Info;
58 static XEXT_GENERATE_CLOSE_DISPLAY (DRI2CloseDisplay, dri2Info)
59
60 static Bool
61 DRI2WireToEvent(Display *dpy, XEvent *event, xEvent *wire);
62 static Status
63 DRI2EventToWire(Display *dpy, XEvent *event, xEvent *wire);
64 static int
65 DRI2Error(Display *display, xError *err, XExtCodes *codes, int *ret_code);
66
67 static /* const */ XExtensionHooks dri2ExtensionHooks = {
68 NULL, /* create_gc */
69 NULL, /* copy_gc */
70 NULL, /* flush_gc */
71 NULL, /* free_gc */
72 NULL, /* create_font */
73 NULL, /* free_font */
74 DRI2CloseDisplay, /* close_display */
75 DRI2WireToEvent, /* wire_to_event */
76 DRI2EventToWire, /* event_to_wire */
77 DRI2Error, /* error */
78 NULL, /* error_string */
79 };
80
81 static XEXT_GENERATE_FIND_DISPLAY (DRI2FindDisplay,
82 dri2Info,
83 dri2ExtensionName,
84 &dri2ExtensionHooks,
85 0, NULL)
86
87 static Bool
88 DRI2WireToEvent(Display *dpy, XEvent *event, xEvent *wire)
89 {
90 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
91 struct glx_drawable *glxDraw;
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 xDRI2BufferSwapComplete2 *awire = (xDRI2BufferSwapComplete2 *)wire;
102 __GLXDRIdrawable *pdraw;
103
104 pdraw = dri2GetGlxDrawableFromXDrawableId(dpy, awire->drawable);
105 if (pdraw == NULL)
106 return False;
107
108 /* Ignore swap events if we're not looking for them */
109 aevent->type = dri2GetSwapEventType(dpy, awire->drawable);
110 if(!aevent->type)
111 return False;
112
113 aevent->serial = _XSetLastRequestRead(dpy, (xGenericReply *) wire);
114 aevent->send_event = (awire->type & 0x80) != 0;
115 aevent->display = dpy;
116 aevent->drawable = awire->drawable;
117 switch (awire->event_type) {
118 case DRI2_EXCHANGE_COMPLETE:
119 aevent->event_type = GLX_EXCHANGE_COMPLETE_INTEL;
120 break;
121 case DRI2_BLIT_COMPLETE:
122 aevent->event_type = GLX_COPY_COMPLETE_INTEL;
123 break;
124 case DRI2_FLIP_COMPLETE:
125 aevent->event_type = GLX_FLIP_COMPLETE_INTEL;
126 break;
127 default:
128 /* unknown swap completion type */
129 return False;
130 }
131 aevent->ust = ((CARD64)awire->ust_hi << 32) | awire->ust_lo;
132 aevent->msc = ((CARD64)awire->msc_hi << 32) | awire->msc_lo;
133
134 glxDraw = GetGLXDrawable(dpy, pdraw->drawable);
135 if (awire->sbc < glxDraw->lastEventSbc)
136 glxDraw->eventSbcWrap += 0x100000000;
137 glxDraw->lastEventSbc = awire->sbc;
138 aevent->sbc = awire->sbc + glxDraw->eventSbcWrap;
139
140 return True;
141 }
142 #endif
143 #ifdef DRI2_InvalidateBuffers
144 case DRI2_InvalidateBuffers:
145 {
146 xDRI2InvalidateBuffers *awire = (xDRI2InvalidateBuffers *)wire;
147
148 dri2InvalidateBuffers(dpy, awire->drawable);
149 return False;
150 }
151 #endif
152 default:
153 /* client doesn't support server event */
154 break;
155 }
156
157 return False;
158 }
159
160 /* We don't actually support this. It doesn't make sense for clients to
161 * send each other DRI2 events.
162 */
163 static Status
164 DRI2EventToWire(Display *dpy, XEvent *event, xEvent *wire)
165 {
166 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
167
168 XextCheckExtension(dpy, info, dri2ExtensionName, False);
169
170 switch (event->type) {
171 default:
172 /* client doesn't support server event */
173 break;
174 }
175
176 return Success;
177 }
178
179 static int
180 DRI2Error(Display *display, xError *err, XExtCodes *codes, int *ret_code)
181 {
182 if (err->majorCode == codes->major_opcode &&
183 err->errorCode == BadDrawable &&
184 err->minorCode == X_DRI2CopyRegion)
185 return True;
186
187 /* If the X drawable was destroyed before the GLX drawable, the
188 * DRI2 drawble will be gone by the time we call
189 * DRI2DestroyDrawable. So just ignore BadDrawable here. */
190 if (err->majorCode == codes->major_opcode &&
191 err->errorCode == BadDrawable &&
192 err->minorCode == X_DRI2DestroyDrawable)
193 return True;
194
195 /* If the server is non-local DRI2Connect will raise BadRequest.
196 * Swallow this so that DRI2Connect can signal this in its return code */
197 if (err->majorCode == codes->major_opcode &&
198 err->minorCode == X_DRI2Connect &&
199 err->errorCode == BadRequest) {
200 *ret_code = False;
201 return True;
202 }
203
204 return False;
205 }
206
207 Bool
208 DRI2QueryExtension(Display * dpy, int *eventBase, int *errorBase)
209 {
210 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
211
212 if (XextHasExtension(info)) {
213 *eventBase = info->codes->first_event;
214 *errorBase = info->codes->first_error;
215 return True;
216 }
217
218 return False;
219 }
220
221 Bool
222 DRI2QueryVersion(Display * dpy, int *major, int *minor)
223 {
224 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
225 xDRI2QueryVersionReply rep;
226 xDRI2QueryVersionReq *req;
227 int i, nevents;
228
229 XextCheckExtension(dpy, info, dri2ExtensionName, False);
230
231 LockDisplay(dpy);
232 GetReq(DRI2QueryVersion, req);
233 req->reqType = info->codes->major_opcode;
234 req->dri2ReqType = X_DRI2QueryVersion;
235 req->majorVersion = DRI2_MAJOR;
236 req->minorVersion = DRI2_MINOR;
237 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
238 UnlockDisplay(dpy);
239 SyncHandle();
240 return False;
241 }
242 *major = rep.majorVersion;
243 *minor = rep.minorVersion;
244 UnlockDisplay(dpy);
245 SyncHandle();
246
247 switch (rep.minorVersion) {
248 case 1:
249 nevents = 0;
250 break;
251 case 2:
252 nevents = 1;
253 break;
254 case 3:
255 default:
256 nevents = 2;
257 break;
258 }
259
260 for (i = 0; i < nevents; i++) {
261 XESetWireToEvent (dpy, info->codes->first_event + i, DRI2WireToEvent);
262 XESetEventToWire (dpy, info->codes->first_event + i, DRI2EventToWire);
263 }
264
265 return True;
266 }
267
268 Bool
269 DRI2Connect(Display * dpy, XID window, char **driverName, char **deviceName)
270 {
271 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
272 xDRI2ConnectReply rep;
273 xDRI2ConnectReq *req;
274
275 XextCheckExtension(dpy, info, dri2ExtensionName, False);
276
277 LockDisplay(dpy);
278 GetReq(DRI2Connect, req);
279 req->reqType = info->codes->major_opcode;
280 req->dri2ReqType = X_DRI2Connect;
281 req->window = window;
282
283 req->driverType = DRI2DriverDRI;
284 #ifdef DRI2DriverPrimeShift
285 {
286 char *prime = getenv("DRI_PRIME");
287 if (prime) {
288 uint32_t primeid;
289 errno = 0;
290 primeid = strtoul(prime, NULL, 0);
291 if (errno == 0)
292 req->driverType |=
293 ((primeid & DRI2DriverPrimeMask) << DRI2DriverPrimeShift);
294 }
295 }
296 #endif
297
298 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
299 UnlockDisplay(dpy);
300 SyncHandle();
301 return False;
302 }
303
304 if (rep.driverNameLength == 0 && rep.deviceNameLength == 0) {
305 UnlockDisplay(dpy);
306 SyncHandle();
307 return False;
308 }
309
310 *driverName = malloc(rep.driverNameLength + 1);
311 if (*driverName == NULL) {
312 _XEatData(dpy,
313 ((rep.driverNameLength + 3) & ~3) +
314 ((rep.deviceNameLength + 3) & ~3));
315 UnlockDisplay(dpy);
316 SyncHandle();
317 return False;
318 }
319 _XReadPad(dpy, *driverName, rep.driverNameLength);
320 (*driverName)[rep.driverNameLength] = '\0';
321
322 *deviceName = malloc(rep.deviceNameLength + 1);
323 if (*deviceName == NULL) {
324 free(*driverName);
325 _XEatData(dpy, ((rep.deviceNameLength + 3) & ~3));
326 UnlockDisplay(dpy);
327 SyncHandle();
328 return False;
329 }
330 _XReadPad(dpy, *deviceName, rep.deviceNameLength);
331 (*deviceName)[rep.deviceNameLength] = '\0';
332
333 UnlockDisplay(dpy);
334 SyncHandle();
335
336 return True;
337 }
338
339 Bool
340 DRI2Authenticate(Display * dpy, XID window, drm_magic_t magic)
341 {
342 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
343 xDRI2AuthenticateReq *req;
344 xDRI2AuthenticateReply rep;
345
346 XextCheckExtension(dpy, info, dri2ExtensionName, False);
347
348 LockDisplay(dpy);
349 GetReq(DRI2Authenticate, req);
350 req->reqType = info->codes->major_opcode;
351 req->dri2ReqType = X_DRI2Authenticate;
352 req->window = window;
353 req->magic = magic;
354
355 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
356 UnlockDisplay(dpy);
357 SyncHandle();
358 return False;
359 }
360
361 UnlockDisplay(dpy);
362 SyncHandle();
363
364 return rep.authenticated;
365 }
366
367 void
368 DRI2CreateDrawable(Display * dpy, XID drawable)
369 {
370 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
371 xDRI2CreateDrawableReq *req;
372
373 XextSimpleCheckExtension(dpy, info, dri2ExtensionName);
374
375 LockDisplay(dpy);
376 GetReq(DRI2CreateDrawable, req);
377 req->reqType = info->codes->major_opcode;
378 req->dri2ReqType = X_DRI2CreateDrawable;
379 req->drawable = drawable;
380 UnlockDisplay(dpy);
381 SyncHandle();
382 }
383
384 void
385 DRI2DestroyDrawable(Display * dpy, XID drawable)
386 {
387 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
388 xDRI2DestroyDrawableReq *req;
389
390 XextSimpleCheckExtension(dpy, info, dri2ExtensionName);
391
392 XSync(dpy, False);
393
394 LockDisplay(dpy);
395 GetReq(DRI2DestroyDrawable, req);
396 req->reqType = info->codes->major_opcode;
397 req->dri2ReqType = X_DRI2DestroyDrawable;
398 req->drawable = drawable;
399 UnlockDisplay(dpy);
400 SyncHandle();
401 }
402
403 DRI2Buffer *
404 DRI2GetBuffers(Display * dpy, XID drawable,
405 int *width, int *height,
406 unsigned int *attachments, int count, int *outCount)
407 {
408 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
409 xDRI2GetBuffersReply rep;
410 xDRI2GetBuffersReq *req;
411 DRI2Buffer *buffers;
412 xDRI2Buffer repBuffer;
413 CARD32 *p;
414 int i;
415
416 XextCheckExtension(dpy, info, dri2ExtensionName, False);
417
418 LockDisplay(dpy);
419 GetReqExtra(DRI2GetBuffers, count * 4, req);
420 req->reqType = info->codes->major_opcode;
421 req->dri2ReqType = X_DRI2GetBuffers;
422 req->drawable = drawable;
423 req->count = count;
424 p = (CARD32 *) & req[1];
425 for (i = 0; i < count; i++)
426 p[i] = attachments[i];
427
428 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
429 UnlockDisplay(dpy);
430 SyncHandle();
431 return NULL;
432 }
433
434 *width = rep.width;
435 *height = rep.height;
436 *outCount = rep.count;
437
438 buffers = malloc(rep.count * sizeof buffers[0]);
439 if (buffers == NULL) {
440 _XEatData(dpy, rep.count * sizeof repBuffer);
441 UnlockDisplay(dpy);
442 SyncHandle();
443 return NULL;
444 }
445
446 for (i = 0; i < rep.count; i++) {
447 _XReadPad(dpy, (char *) &repBuffer, sizeof repBuffer);
448 buffers[i].attachment = repBuffer.attachment;
449 buffers[i].name = repBuffer.name;
450 buffers[i].pitch = repBuffer.pitch;
451 buffers[i].cpp = repBuffer.cpp;
452 buffers[i].flags = repBuffer.flags;
453 }
454
455 UnlockDisplay(dpy);
456 SyncHandle();
457
458 return buffers;
459 }
460
461
462 DRI2Buffer *
463 DRI2GetBuffersWithFormat(Display * dpy, XID drawable,
464 int *width, int *height,
465 unsigned int *attachments, int count, int *outCount)
466 {
467 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
468 xDRI2GetBuffersReply rep;
469 xDRI2GetBuffersReq *req;
470 DRI2Buffer *buffers;
471 xDRI2Buffer repBuffer;
472 CARD32 *p;
473 int i;
474
475 XextCheckExtension(dpy, info, dri2ExtensionName, False);
476
477 LockDisplay(dpy);
478 GetReqExtra(DRI2GetBuffers, count * (4 * 2), req);
479 req->reqType = info->codes->major_opcode;
480 req->dri2ReqType = X_DRI2GetBuffersWithFormat;
481 req->drawable = drawable;
482 req->count = count;
483 p = (CARD32 *) & req[1];
484 for (i = 0; i < (count * 2); i++)
485 p[i] = attachments[i];
486
487 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
488 UnlockDisplay(dpy);
489 SyncHandle();
490 return NULL;
491 }
492
493 *width = rep.width;
494 *height = rep.height;
495 *outCount = rep.count;
496
497 buffers = malloc(rep.count * sizeof buffers[0]);
498 if (buffers == NULL) {
499 _XEatData(dpy, rep.count * sizeof repBuffer);
500 UnlockDisplay(dpy);
501 SyncHandle();
502 return NULL;
503 }
504
505 for (i = 0; i < rep.count; i++) {
506 _XReadPad(dpy, (char *) &repBuffer, sizeof repBuffer);
507 buffers[i].attachment = repBuffer.attachment;
508 buffers[i].name = repBuffer.name;
509 buffers[i].pitch = repBuffer.pitch;
510 buffers[i].cpp = repBuffer.cpp;
511 buffers[i].flags = repBuffer.flags;
512 }
513
514 UnlockDisplay(dpy);
515 SyncHandle();
516
517 return buffers;
518 }
519
520
521 void
522 DRI2CopyRegion(Display * dpy, XID drawable, XserverRegion region,
523 CARD32 dest, CARD32 src)
524 {
525 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
526 xDRI2CopyRegionReq *req;
527 xDRI2CopyRegionReply rep;
528
529 XextSimpleCheckExtension(dpy, info, dri2ExtensionName);
530
531 LockDisplay(dpy);
532 GetReq(DRI2CopyRegion, req);
533 req->reqType = info->codes->major_opcode;
534 req->dri2ReqType = X_DRI2CopyRegion;
535 req->drawable = drawable;
536 req->region = region;
537 req->dest = dest;
538 req->src = src;
539
540 _XReply(dpy, (xReply *) & rep, 0, xFalse);
541
542 UnlockDisplay(dpy);
543 SyncHandle();
544 }
545
546 #endif /* GLX_DIRECT_RENDERING */