Revert pointless reindents to avoid merge conflicts.
[mesa.git] / src / glx / x11 / dri2.c
1 /* -*- mode: c; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 3; coding: utf-8-unix -*- */
2 /*
3 * Copyright © 2008 Red Hat, Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Soft-
7 * ware"), to deal in the Software without restriction, including without
8 * limitation the rights to use, copy, modify, merge, publish, distribute,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, provided that the above copyright
11 * notice(s) and this permission notice appear in all copies of the Soft-
12 * ware and that both the above copyright notice(s) and this permission
13 * notice appear in supporting documentation.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
17 * ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY
18 * RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN
19 * THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSE-
20 * QUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
22 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFOR-
23 * MANCE OF THIS SOFTWARE.
24 *
25 * Except as contained in this notice, the name of a copyright holder shall
26 * not be used in advertising or otherwise to promote the sale, use or
27 * other dealings in this Software without prior written authorization of
28 * the copyright holder.
29 *
30 * Authors:
31 * Kristian Høgsberg (krh@redhat.com)
32 */
33
34
35 #define NEED_REPLIES
36 #include <X11/Xlibint.h>
37 #include <X11/extensions/Xext.h>
38 #include <X11/extensions/extutil.h>
39 #include <X11/extensions/dri2proto.h>
40 #include "xf86drm.h"
41 #include "dri2.h"
42
43 static char dri2ExtensionName[] = DRI2_NAME;
44 static XExtensionInfo *dri2Info;
45 static XEXT_GENERATE_CLOSE_DISPLAY (DRI2CloseDisplay, dri2Info)
46 static /* const */ XExtensionHooks dri2ExtensionHooks = {
47 NULL, /* create_gc */
48 NULL, /* copy_gc */
49 NULL, /* flush_gc */
50 NULL, /* free_gc */
51 NULL, /* create_font */
52 NULL, /* free_font */
53 DRI2CloseDisplay, /* close_display */
54 NULL, /* wire_to_event */
55 NULL, /* event_to_wire */
56 NULL, /* error */
57 NULL, /* error_string */
58 };
59
60 static XEXT_GENERATE_FIND_DISPLAY (DRI2FindDisplay, dri2Info,
61 dri2ExtensionName,
62 &dri2ExtensionHooks,
63 0, NULL)
64
65 Bool DRI2QueryExtension(Display *dpy, int *eventBase, int *errorBase)
66 {
67 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
68
69 if (XextHasExtension(info)) {
70 *eventBase = info->codes->first_event;
71 *errorBase = info->codes->first_error;
72 return True;
73 }
74
75 return False;
76 }
77
78 Bool DRI2QueryVersion(Display *dpy, int *major, int *minor)
79 {
80 XExtDisplayInfo *info = DRI2FindDisplay (dpy);
81 xDRI2QueryVersionReply rep;
82 xDRI2QueryVersionReq *req;
83
84 XextCheckExtension (dpy, info, dri2ExtensionName, False);
85
86 LockDisplay(dpy);
87 GetReq(DRI2QueryVersion, req);
88 req->reqType = info->codes->major_opcode;
89 req->dri2ReqType = X_DRI2QueryVersion;
90 req->majorVersion = DRI2_MAJOR;
91 req->minorVersion = DRI2_MINOR;
92 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
93 UnlockDisplay(dpy);
94 SyncHandle();
95 return False;
96 }
97 *major = rep.majorVersion;
98 *minor = rep.minorVersion;
99 UnlockDisplay(dpy);
100 SyncHandle();
101
102 return True;
103 }
104
105 Bool DRI2Connect(Display *dpy, int screen,
106 char **driverName, char **busId, unsigned int *sareaHandle)
107 {
108 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
109 xDRI2ConnectReply rep;
110 xDRI2ConnectReq *req;
111
112 XextCheckExtension (dpy, info, dri2ExtensionName, False);
113
114 LockDisplay(dpy);
115 GetReq(DRI2Connect, req);
116 req->reqType = info->codes->major_opcode;
117 req->dri2ReqType = X_DRI2Connect;
118 req->screen = screen;
119 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
120 UnlockDisplay(dpy);
121 SyncHandle();
122 return False;
123 }
124
125 if (rep.driverNameLength == 0 && rep.busIdLength == 0) {
126 UnlockDisplay(dpy);
127 SyncHandle();
128 return False;
129 }
130
131 *driverName = Xmalloc(rep.driverNameLength + 1);
132 if (*driverName == NULL) {
133 _XEatData(dpy,
134 ((rep.driverNameLength + 3) & ~3) +
135 ((rep.busIdLength + 3) & ~3));
136 UnlockDisplay(dpy);
137 SyncHandle();
138 return False;
139 }
140 _XReadPad(dpy, *driverName, rep.driverNameLength);
141 (*driverName)[rep.driverNameLength] = '\0';
142
143 *busId = Xmalloc(rep.busIdLength + 1);
144 if (*busId == NULL) {
145 Xfree(*driverName);
146 _XEatData(dpy, ((rep.busIdLength + 3) & ~3));
147 UnlockDisplay(dpy);
148 SyncHandle();
149 return False;
150 }
151 _XReadPad(dpy, *busId, rep.busIdLength);
152 (*busId)[rep.busIdLength] = '\0';
153
154 UnlockDisplay(dpy);
155 SyncHandle();
156
157 return True;
158 }
159
160 Bool DRI2AuthConnection(Display *dpy, int screen, drm_magic_t magic)
161 {
162 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
163 xDRI2AuthConnectionReq *req;
164 xDRI2AuthConnectionReply rep;
165
166 XextCheckExtension (dpy, info, dri2ExtensionName, False);
167
168 LockDisplay(dpy);
169 GetReq(DRI2AuthConnection, req);
170 req->reqType = info->codes->major_opcode;
171 req->dri2ReqType = X_DRI2AuthConnection;
172 req->screen = screen;
173 req->magic = magic;
174 rep.authenticated = 0;
175 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
176 UnlockDisplay(dpy);
177 SyncHandle();
178 return False;
179 }
180 UnlockDisplay(dpy);
181 SyncHandle();
182
183 return rep.authenticated;
184 }
185
186 void DRI2CreateDrawable(Display *dpy, XID drawable)
187 {
188 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
189 xDRI2CreateDrawableReq *req;
190
191 XextSimpleCheckExtension (dpy, info, dri2ExtensionName);
192
193 LockDisplay(dpy);
194 GetReq(DRI2CreateDrawable, req);
195 req->reqType = info->codes->major_opcode;
196 req->dri2ReqType = X_DRI2CreateDrawable;
197 req->drawable = drawable;
198 UnlockDisplay(dpy);
199 SyncHandle();
200 }
201
202 DRI2Buffer *DRI2GetBuffers(Display *dpy, XID drawable,
203 int *width, int *height,
204 unsigned int *attachments, int count,
205 int *outCount)
206 {
207 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
208 xDRI2GetBuffersReply rep;
209 xDRI2GetBuffersReq *req;
210 DRI2Buffer *buffers;
211 xDRI2Buffer repBuffer;
212 CARD32 *p;
213 int i;
214
215 XextCheckExtension (dpy, info, dri2ExtensionName, False);
216
217 LockDisplay(dpy);
218 GetReqExtra(DRI2GetBuffers, count * 4, req);
219 req->reqType = info->codes->major_opcode;
220 req->dri2ReqType = X_DRI2GetBuffers;
221 req->drawable = drawable;
222 req->count = count;
223 p = (CARD32 *) &req[1];
224 for (i = 0; i < count; i++)
225 p[i] = attachments[i];
226
227 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
228 UnlockDisplay(dpy);
229 SyncHandle();
230 return NULL;
231 }
232
233 *width = rep.width;
234 *height = rep.height;
235 *outCount = rep.count;
236
237 buffers = Xmalloc(count * sizeof buffers[0]);
238 if (buffers == NULL) {
239 _XEatData(dpy, rep.count * sizeof repBuffer);
240 UnlockDisplay(dpy);
241 SyncHandle();
242 return NULL;
243 }
244
245 for (i = 0; i < rep.count; i++) {
246 _XReadPad(dpy, (char *) &repBuffer, sizeof repBuffer);
247 buffers[i].attachment = repBuffer.attachment;
248 buffers[i].name = repBuffer.name;
249 buffers[i].pitch = repBuffer.pitch;
250 buffers[i].cpp = repBuffer.cpp;
251 buffers[i].flags = repBuffer.flags;
252 }
253
254 UnlockDisplay(dpy);
255 SyncHandle();
256
257 return buffers;
258 }
259
260 void DRI2SwapBuffers(Display *dpy, XID drawable,
261 int x, int y, int width, int height)
262 {
263 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
264 xDRI2SwapBuffersReq *req;
265 xDRI2SwapBuffersReply rep;
266
267 XextSimpleCheckExtension (dpy, info, dri2ExtensionName);
268
269 LockDisplay(dpy);
270 GetReq(DRI2SwapBuffers, req);
271 req->reqType = info->codes->major_opcode;
272 req->dri2ReqType = X_DRI2SwapBuffers;
273 req->drawable = drawable;
274 req->x = x;
275 req->y = y;
276 req->width = width;
277 req->height = height;
278
279 _XReply(dpy, (xReply *)&rep, 0, xFalse);
280
281 UnlockDisplay(dpy);
282 SyncHandle();
283 }
284
285 void DRI2DestroyDrawable(Display *dpy, XID drawable)
286 {
287 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
288 xDRI2DestroyDrawableReq *req;
289
290 XextSimpleCheckExtension (dpy, info, dri2ExtensionName);
291
292 XSync(dpy, False);
293
294 LockDisplay(dpy);
295 GetReq(DRI2DestroyDrawable, req);
296 req->reqType = info->codes->major_opcode;
297 req->dri2ReqType = X_DRI2DestroyDrawable;
298 req->drawable = drawable;
299 UnlockDisplay(dpy);
300 SyncHandle();
301 }