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