glx: remove #include "glheader.h" lines
[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 static char dri2ExtensionName[] = DRI2_NAME;
43 static XExtensionInfo *dri2Info;
44 static XEXT_GENERATE_CLOSE_DISPLAY (DRI2CloseDisplay, dri2Info)
45 static /* const */ XExtensionHooks dri2ExtensionHooks = {
46 NULL, /* create_gc */
47 NULL, /* copy_gc */
48 NULL, /* flush_gc */
49 NULL, /* free_gc */
50 NULL, /* create_font */
51 NULL, /* free_font */
52 DRI2CloseDisplay, /* close_display */
53 NULL, /* wire_to_event */
54 NULL, /* event_to_wire */
55 NULL, /* error */
56 NULL, /* error_string */
57 };
58
59 static XEXT_GENERATE_FIND_DISPLAY (DRI2FindDisplay, dri2Info,
60 dri2ExtensionName,
61 &dri2ExtensionHooks,
62 0, NULL)
63
64 Bool DRI2QueryExtension(Display *dpy, int *eventBase, int *errorBase)
65 {
66 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
67
68 if (XextHasExtension(info)) {
69 *eventBase = info->codes->first_event;
70 *errorBase = info->codes->first_error;
71 return True;
72 }
73
74 return False;
75 }
76
77 Bool DRI2QueryVersion(Display *dpy, int *major, int *minor)
78 {
79 XExtDisplayInfo *info = DRI2FindDisplay (dpy);
80 xDRI2QueryVersionReply rep;
81 xDRI2QueryVersionReq *req;
82
83 XextCheckExtension (dpy, info, dri2ExtensionName, False);
84
85 LockDisplay(dpy);
86 GetReq(DRI2QueryVersion, req);
87 req->reqType = info->codes->major_opcode;
88 req->dri2ReqType = X_DRI2QueryVersion;
89 req->majorVersion = DRI2_MAJOR;
90 req->minorVersion = DRI2_MINOR;
91 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
92 UnlockDisplay(dpy);
93 SyncHandle();
94 return False;
95 }
96 *major = rep.majorVersion;
97 *minor = rep.minorVersion;
98 UnlockDisplay(dpy);
99 SyncHandle();
100
101 return True;
102 }
103
104 Bool DRI2Connect(Display *dpy, int screen,
105 char **driverName, char **busId, unsigned int *sareaHandle)
106 {
107 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
108 xDRI2ConnectReply rep;
109 xDRI2ConnectReq *req;
110
111 XextCheckExtension (dpy, info, dri2ExtensionName, False);
112
113 LockDisplay(dpy);
114 GetReq(DRI2Connect, req);
115 req->reqType = info->codes->major_opcode;
116 req->dri2ReqType = X_DRI2Connect;
117 req->screen = screen;
118 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
119 UnlockDisplay(dpy);
120 SyncHandle();
121 return False;
122 }
123
124 if (rep.driverNameLength == 0 && rep.busIdLength == 0) {
125 UnlockDisplay(dpy);
126 SyncHandle();
127 return False;
128 }
129
130 *driverName = Xmalloc(rep.driverNameLength + 1);
131 if (*driverName == NULL) {
132 _XEatData(dpy,
133 ((rep.driverNameLength + 3) & ~3) +
134 ((rep.busIdLength + 3) & ~3));
135 UnlockDisplay(dpy);
136 SyncHandle();
137 return False;
138 }
139 _XReadPad(dpy, *driverName, rep.driverNameLength);
140 (*driverName)[rep.driverNameLength] = '\0';
141
142 *busId = Xmalloc(rep.busIdLength + 1);
143 if (*busId == NULL) {
144 Xfree(*driverName);
145 _XEatData(dpy, ((rep.busIdLength + 3) & ~3));
146 UnlockDisplay(dpy);
147 SyncHandle();
148 return False;
149 }
150 _XReadPad(dpy, *busId, rep.busIdLength);
151 (*busId)[rep.busIdLength] = '\0';
152
153 UnlockDisplay(dpy);
154 SyncHandle();
155
156 return True;
157 }
158
159 Bool DRI2AuthConnection(Display *dpy, int screen, drm_magic_t magic)
160 {
161 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
162 xDRI2AuthConnectionReq *req;
163 xDRI2AuthConnectionReply rep;
164
165 XextCheckExtension (dpy, info, dri2ExtensionName, False);
166
167 LockDisplay(dpy);
168 GetReq(DRI2AuthConnection, req);
169 req->reqType = info->codes->major_opcode;
170 req->dri2ReqType = X_DRI2AuthConnection;
171 req->screen = screen;
172 req->magic = magic;
173 rep.authenticated = 0;
174 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
175 UnlockDisplay(dpy);
176 SyncHandle();
177 return False;
178 }
179 UnlockDisplay(dpy);
180 SyncHandle();
181
182 return rep.authenticated;
183 }
184
185 void DRI2CreateDrawable(Display *dpy, XID drawable)
186 {
187 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
188 xDRI2CreateDrawableReq *req;
189
190 XextSimpleCheckExtension (dpy, info, dri2ExtensionName);
191
192 LockDisplay(dpy);
193 GetReq(DRI2CreateDrawable, req);
194 req->reqType = info->codes->major_opcode;
195 req->dri2ReqType = X_DRI2CreateDrawable;
196 req->drawable = drawable;
197 UnlockDisplay(dpy);
198 SyncHandle();
199 }
200
201 DRI2Buffer *DRI2GetBuffers(Display *dpy, XID drawable,
202 int *width, int *height,
203 unsigned int *attachments, int count,
204 int *outCount)
205 {
206 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
207 xDRI2GetBuffersReply rep;
208 xDRI2GetBuffersReq *req;
209 DRI2Buffer *buffers;
210 xDRI2Buffer repBuffer;
211 CARD32 *p;
212 int i;
213
214 XextCheckExtension (dpy, info, dri2ExtensionName, False);
215
216 LockDisplay(dpy);
217 GetReqExtra(DRI2GetBuffers, count * 4, req);
218 req->reqType = info->codes->major_opcode;
219 req->dri2ReqType = X_DRI2GetBuffers;
220 req->drawable = drawable;
221 req->count = count;
222 p = (CARD32 *) &req[1];
223 for (i = 0; i < count; i++)
224 p[i] = attachments[i];
225
226 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
227 UnlockDisplay(dpy);
228 SyncHandle();
229 return NULL;
230 }
231
232 *width = rep.width;
233 *height = rep.height;
234 *outCount = rep.count;
235
236 buffers = Xmalloc(count * sizeof buffers[0]);
237 if (buffers == NULL) {
238 _XEatData(dpy, rep.count * sizeof repBuffer);
239 UnlockDisplay(dpy);
240 SyncHandle();
241 return NULL;
242 }
243
244 for (i = 0; i < rep.count; i++) {
245 _XReadPad(dpy, (char *) &repBuffer, sizeof repBuffer);
246 buffers[i].attachment = repBuffer.attachment;
247 buffers[i].name = repBuffer.name;
248 buffers[i].pitch = repBuffer.pitch;
249 buffers[i].cpp = repBuffer.cpp;
250 buffers[i].flags = repBuffer.flags;
251 }
252
253 UnlockDisplay(dpy);
254 SyncHandle();
255
256 return buffers;
257 }
258
259 void DRI2SwapBuffers(Display *dpy, XID drawable,
260 int x, int y, int width, int height)
261 {
262 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
263 xDRI2SwapBuffersReq *req;
264 xDRI2SwapBuffersReply rep;
265
266 XextSimpleCheckExtension (dpy, info, dri2ExtensionName);
267
268 LockDisplay(dpy);
269 GetReq(DRI2SwapBuffers, req);
270 req->reqType = info->codes->major_opcode;
271 req->dri2ReqType = X_DRI2SwapBuffers;
272 req->drawable = drawable;
273 req->x = x;
274 req->y = y;
275 req->width = width;
276 req->height = height;
277
278 _XReply(dpy, (xReply *)&rep, 0, xFalse);
279
280 UnlockDisplay(dpy);
281 SyncHandle();
282 }
283
284 void DRI2DestroyDrawable(Display *dpy, XID drawable)
285 {
286 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
287 xDRI2DestroyDrawableReq *req;
288
289 XextSimpleCheckExtension (dpy, info, dri2ExtensionName);
290
291 XSync(dpy, False);
292
293 LockDisplay(dpy);
294 GetReq(DRI2DestroyDrawable, req);
295 req->reqType = info->codes->major_opcode;
296 req->dri2ReqType = X_DRI2DestroyDrawable;
297 req->drawable = drawable;
298 UnlockDisplay(dpy);
299 SyncHandle();
300 }