Merge remote branch 'origin/master' into lp-binning
[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 <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
44 /* Allow the build to work with an older versions of dri2proto.h and
45 * dri2tokens.h.
46 */
47 #if DRI2_MINOR < 1
48 #undef DRI2_MINOR
49 #define DRI2_MINOR 1
50 #define X_DRI2GetBuffersWithFormat 7
51 #endif
52
53
54 static char dri2ExtensionName[] = DRI2_NAME;
55 static XExtensionInfo *dri2Info;
56 static XEXT_GENERATE_CLOSE_DISPLAY (DRI2CloseDisplay, dri2Info)
57
58 static /* const */ XExtensionHooks dri2ExtensionHooks = {
59 NULL, /* create_gc */
60 NULL, /* copy_gc */
61 NULL, /* flush_gc */
62 NULL, /* free_gc */
63 NULL, /* create_font */
64 NULL, /* free_font */
65 DRI2CloseDisplay, /* close_display */
66 NULL, /* wire_to_event */
67 NULL, /* event_to_wire */
68 NULL, /* error */
69 NULL, /* error_string */
70 };
71
72 static XEXT_GENERATE_FIND_DISPLAY (DRI2FindDisplay,
73 dri2Info,
74 dri2ExtensionName,
75 &dri2ExtensionHooks,
76 0, NULL)
77
78 Bool
79 DRI2QueryExtension(Display * dpy, int *eventBase, int *errorBase)
80 {
81 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
82
83 if (XextHasExtension(info)) {
84 *eventBase = info->codes->first_event;
85 *errorBase = info->codes->first_error;
86 return True;
87 }
88
89 return False;
90 }
91
92 Bool
93 DRI2QueryVersion(Display * dpy, int *major, int *minor)
94 {
95 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
96 xDRI2QueryVersionReply rep;
97 xDRI2QueryVersionReq *req;
98
99 XextCheckExtension(dpy, info, dri2ExtensionName, False);
100
101 LockDisplay(dpy);
102 GetReq(DRI2QueryVersion, req);
103 req->reqType = info->codes->major_opcode;
104 req->dri2ReqType = X_DRI2QueryVersion;
105 req->majorVersion = DRI2_MAJOR;
106 req->minorVersion = DRI2_MINOR;
107 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
108 UnlockDisplay(dpy);
109 SyncHandle();
110 return False;
111 }
112 *major = rep.majorVersion;
113 *minor = rep.minorVersion;
114 UnlockDisplay(dpy);
115 SyncHandle();
116
117 return True;
118 }
119
120 Bool
121 DRI2Connect(Display * dpy, XID window, char **driverName, char **deviceName)
122 {
123 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
124 xDRI2ConnectReply rep;
125 xDRI2ConnectReq *req;
126
127 XextCheckExtension(dpy, info, dri2ExtensionName, False);
128
129 LockDisplay(dpy);
130 GetReq(DRI2Connect, req);
131 req->reqType = info->codes->major_opcode;
132 req->dri2ReqType = X_DRI2Connect;
133 req->window = window;
134 req->driverType = DRI2DriverDRI;
135 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
136 UnlockDisplay(dpy);
137 SyncHandle();
138 return False;
139 }
140
141 if (rep.driverNameLength == 0 && rep.deviceNameLength == 0) {
142 UnlockDisplay(dpy);
143 SyncHandle();
144 return False;
145 }
146
147 *driverName = Xmalloc(rep.driverNameLength + 1);
148 if (*driverName == NULL) {
149 _XEatData(dpy,
150 ((rep.driverNameLength + 3) & ~3) +
151 ((rep.deviceNameLength + 3) & ~3));
152 UnlockDisplay(dpy);
153 SyncHandle();
154 return False;
155 }
156 _XReadPad(dpy, *driverName, rep.driverNameLength);
157 (*driverName)[rep.driverNameLength] = '\0';
158
159 *deviceName = Xmalloc(rep.deviceNameLength + 1);
160 if (*deviceName == NULL) {
161 Xfree(*driverName);
162 _XEatData(dpy, ((rep.deviceNameLength + 3) & ~3));
163 UnlockDisplay(dpy);
164 SyncHandle();
165 return False;
166 }
167 _XReadPad(dpy, *deviceName, rep.deviceNameLength);
168 (*deviceName)[rep.deviceNameLength] = '\0';
169
170 UnlockDisplay(dpy);
171 SyncHandle();
172
173 return True;
174 }
175
176 Bool
177 DRI2Authenticate(Display * dpy, XID window, drm_magic_t magic)
178 {
179 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
180 xDRI2AuthenticateReq *req;
181 xDRI2AuthenticateReply rep;
182
183 XextCheckExtension(dpy, info, dri2ExtensionName, False);
184
185 LockDisplay(dpy);
186 GetReq(DRI2Authenticate, req);
187 req->reqType = info->codes->major_opcode;
188 req->dri2ReqType = X_DRI2Authenticate;
189 req->window = window;
190 req->magic = magic;
191
192 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
193 UnlockDisplay(dpy);
194 SyncHandle();
195 return False;
196 }
197
198 UnlockDisplay(dpy);
199 SyncHandle();
200
201 return rep.authenticated;
202 }
203
204 void
205 DRI2CreateDrawable(Display * dpy, XID drawable)
206 {
207 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
208 xDRI2CreateDrawableReq *req;
209
210 XextSimpleCheckExtension(dpy, info, dri2ExtensionName);
211
212 LockDisplay(dpy);
213 GetReq(DRI2CreateDrawable, req);
214 req->reqType = info->codes->major_opcode;
215 req->dri2ReqType = X_DRI2CreateDrawable;
216 req->drawable = drawable;
217 UnlockDisplay(dpy);
218 SyncHandle();
219 }
220
221 void
222 DRI2DestroyDrawable(Display * dpy, XID drawable)
223 {
224 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
225 xDRI2DestroyDrawableReq *req;
226
227 XextSimpleCheckExtension(dpy, info, dri2ExtensionName);
228
229 XSync(dpy, False);
230
231 LockDisplay(dpy);
232 GetReq(DRI2DestroyDrawable, req);
233 req->reqType = info->codes->major_opcode;
234 req->dri2ReqType = X_DRI2DestroyDrawable;
235 req->drawable = drawable;
236 UnlockDisplay(dpy);
237 SyncHandle();
238 }
239
240 DRI2Buffer *
241 DRI2GetBuffers(Display * dpy, XID drawable,
242 int *width, int *height,
243 unsigned int *attachments, int count, int *outCount)
244 {
245 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
246 xDRI2GetBuffersReply rep;
247 xDRI2GetBuffersReq *req;
248 DRI2Buffer *buffers;
249 xDRI2Buffer repBuffer;
250 CARD32 *p;
251 int i;
252
253 XextCheckExtension(dpy, info, dri2ExtensionName, False);
254
255 LockDisplay(dpy);
256 GetReqExtra(DRI2GetBuffers, count * 4, req);
257 req->reqType = info->codes->major_opcode;
258 req->dri2ReqType = X_DRI2GetBuffers;
259 req->drawable = drawable;
260 req->count = count;
261 p = (CARD32 *) & req[1];
262 for (i = 0; i < count; i++)
263 p[i] = attachments[i];
264
265 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
266 UnlockDisplay(dpy);
267 SyncHandle();
268 return NULL;
269 }
270
271 *width = rep.width;
272 *height = rep.height;
273 *outCount = rep.count;
274
275 buffers = Xmalloc(rep.count * sizeof buffers[0]);
276 if (buffers == NULL) {
277 _XEatData(dpy, rep.count * sizeof repBuffer);
278 UnlockDisplay(dpy);
279 SyncHandle();
280 return NULL;
281 }
282
283 for (i = 0; i < rep.count; i++) {
284 _XReadPad(dpy, (char *) &repBuffer, sizeof repBuffer);
285 buffers[i].attachment = repBuffer.attachment;
286 buffers[i].name = repBuffer.name;
287 buffers[i].pitch = repBuffer.pitch;
288 buffers[i].cpp = repBuffer.cpp;
289 buffers[i].flags = repBuffer.flags;
290 }
291
292 UnlockDisplay(dpy);
293 SyncHandle();
294
295 return buffers;
296 }
297
298
299 DRI2Buffer *
300 DRI2GetBuffersWithFormat(Display * dpy, XID drawable,
301 int *width, int *height,
302 unsigned int *attachments, int count, int *outCount)
303 {
304 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
305 xDRI2GetBuffersReply rep;
306 xDRI2GetBuffersReq *req;
307 DRI2Buffer *buffers;
308 xDRI2Buffer repBuffer;
309 CARD32 *p;
310 int i;
311
312 XextCheckExtension(dpy, info, dri2ExtensionName, False);
313
314 LockDisplay(dpy);
315 GetReqExtra(DRI2GetBuffers, count * (4 * 2), req);
316 req->reqType = info->codes->major_opcode;
317 req->dri2ReqType = X_DRI2GetBuffersWithFormat;
318 req->drawable = drawable;
319 req->count = count;
320 p = (CARD32 *) & req[1];
321 for (i = 0; i < (count * 2); i++)
322 p[i] = attachments[i];
323
324 if (!_XReply(dpy, (xReply *) & rep, 0, xFalse)) {
325 UnlockDisplay(dpy);
326 SyncHandle();
327 return NULL;
328 }
329
330 *width = rep.width;
331 *height = rep.height;
332 *outCount = rep.count;
333
334 buffers = Xmalloc(rep.count * sizeof buffers[0]);
335 if (buffers == NULL) {
336 _XEatData(dpy, rep.count * sizeof repBuffer);
337 UnlockDisplay(dpy);
338 SyncHandle();
339 return NULL;
340 }
341
342 for (i = 0; i < rep.count; i++) {
343 _XReadPad(dpy, (char *) &repBuffer, sizeof repBuffer);
344 buffers[i].attachment = repBuffer.attachment;
345 buffers[i].name = repBuffer.name;
346 buffers[i].pitch = repBuffer.pitch;
347 buffers[i].cpp = repBuffer.cpp;
348 buffers[i].flags = repBuffer.flags;
349 }
350
351 UnlockDisplay(dpy);
352 SyncHandle();
353
354 return buffers;
355 }
356
357
358 void
359 DRI2CopyRegion(Display * dpy, XID drawable, XserverRegion region,
360 CARD32 dest, CARD32 src)
361 {
362 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
363 xDRI2CopyRegionReq *req;
364 xDRI2CopyRegionReply rep;
365
366 XextSimpleCheckExtension(dpy, info, dri2ExtensionName);
367
368 LockDisplay(dpy);
369 GetReq(DRI2CopyRegion, req);
370 req->reqType = info->codes->major_opcode;
371 req->dri2ReqType = X_DRI2CopyRegion;
372 req->drawable = drawable;
373 req->region = region;
374 req->dest = dest;
375 req->src = src;
376
377 _XReply(dpy, (xReply *) & rep, 0, xFalse);
378
379 UnlockDisplay(dpy);
380 SyncHandle();
381 }
382
383 #endif /* GLX_DIRECT_RENDERING */