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