Merge commit 'origin/gallium-0.1' into gallium-0.2
[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, XID window,
106 char **driverName, char **deviceName)
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->window = window;
119 req->driverType = DRI2DriverDRI;
120 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
121 UnlockDisplay(dpy);
122 SyncHandle();
123 return False;
124 }
125
126 if (rep.driverNameLength == 0 && rep.deviceNameLength == 0) {
127 UnlockDisplay(dpy);
128 SyncHandle();
129 return False;
130 }
131
132 *driverName = Xmalloc(rep.driverNameLength + 1);
133 if (*driverName == NULL) {
134 _XEatData(dpy,
135 ((rep.driverNameLength + 3) & ~3) +
136 ((rep.deviceNameLength + 3) & ~3));
137 UnlockDisplay(dpy);
138 SyncHandle();
139 return False;
140 }
141 _XReadPad(dpy, *driverName, rep.driverNameLength);
142 (*driverName)[rep.driverNameLength] = '\0';
143
144 *deviceName = Xmalloc(rep.deviceNameLength + 1);
145 if (*deviceName == NULL) {
146 Xfree(*driverName);
147 _XEatData(dpy, ((rep.deviceNameLength + 3) & ~3));
148 UnlockDisplay(dpy);
149 SyncHandle();
150 return False;
151 }
152 _XReadPad(dpy, *deviceName, rep.deviceNameLength);
153 (*deviceName)[rep.deviceNameLength] = '\0';
154
155 UnlockDisplay(dpy);
156 SyncHandle();
157
158 return True;
159 }
160
161 Bool DRI2Authenticate(Display *dpy, XID window, drm_magic_t magic)
162 {
163 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
164 xDRI2AuthenticateReq *req;
165 xDRI2AuthenticateReply rep;
166
167 XextCheckExtension (dpy, info, dri2ExtensionName, False);
168
169 LockDisplay(dpy);
170 GetReq(DRI2Authenticate, req);
171 req->reqType = info->codes->major_opcode;
172 req->dri2ReqType = X_DRI2Authenticate;
173 req->window = window;
174 req->magic = magic;
175
176 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
177 UnlockDisplay(dpy);
178 SyncHandle();
179 return False;
180 }
181
182 UnlockDisplay(dpy);
183 SyncHandle();
184
185 return rep.authenticated;
186 }
187
188 void DRI2CreateDrawable(Display *dpy, XID drawable)
189 {
190 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
191 xDRI2CreateDrawableReq *req;
192
193 XextSimpleCheckExtension (dpy, info, dri2ExtensionName);
194
195 LockDisplay(dpy);
196 GetReq(DRI2CreateDrawable, req);
197 req->reqType = info->codes->major_opcode;
198 req->dri2ReqType = X_DRI2CreateDrawable;
199 req->drawable = drawable;
200 UnlockDisplay(dpy);
201 SyncHandle();
202 }
203
204 void DRI2DestroyDrawable(Display *dpy, XID drawable)
205 {
206 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
207 xDRI2DestroyDrawableReq *req;
208
209 XextSimpleCheckExtension (dpy, info, dri2ExtensionName);
210
211 XSync(dpy, False);
212
213 LockDisplay(dpy);
214 GetReq(DRI2DestroyDrawable, req);
215 req->reqType = info->codes->major_opcode;
216 req->dri2ReqType = X_DRI2DestroyDrawable;
217 req->drawable = drawable;
218 UnlockDisplay(dpy);
219 SyncHandle();
220 }
221
222 DRI2Buffer *DRI2GetBuffers(Display *dpy, XID drawable,
223 int *width, int *height,
224 unsigned int *attachments, int count,
225 int *outCount)
226 {
227 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
228 xDRI2GetBuffersReply rep;
229 xDRI2GetBuffersReq *req;
230 DRI2Buffer *buffers;
231 xDRI2Buffer repBuffer;
232 CARD32 *p;
233 int i;
234
235 XextCheckExtension (dpy, info, dri2ExtensionName, False);
236
237 LockDisplay(dpy);
238 GetReqExtra(DRI2GetBuffers, count * 4, req);
239 req->reqType = info->codes->major_opcode;
240 req->dri2ReqType = X_DRI2GetBuffers;
241 req->drawable = drawable;
242 req->count = count;
243 p = (CARD32 *) &req[1];
244 for (i = 0; i < count; i++)
245 p[i] = attachments[i];
246
247 if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
248 UnlockDisplay(dpy);
249 SyncHandle();
250 return NULL;
251 }
252
253 *width = rep.width;
254 *height = rep.height;
255 *outCount = rep.count;
256
257 buffers = Xmalloc(count * sizeof buffers[0]);
258 if (buffers == NULL) {
259 _XEatData(dpy, rep.count * sizeof repBuffer);
260 UnlockDisplay(dpy);
261 SyncHandle();
262 return NULL;
263 }
264
265 for (i = 0; i < rep.count; i++) {
266 _XReadPad(dpy, (char *) &repBuffer, sizeof repBuffer);
267 buffers[i].attachment = repBuffer.attachment;
268 buffers[i].name = repBuffer.name;
269 buffers[i].pitch = repBuffer.pitch;
270 buffers[i].cpp = repBuffer.cpp;
271 buffers[i].flags = repBuffer.flags;
272 }
273
274 UnlockDisplay(dpy);
275 SyncHandle();
276
277 return buffers;
278 }
279
280 void DRI2CopyRegion(Display *dpy, XID drawable, XserverRegion region,
281 CARD32 dest, CARD32 src)
282 {
283 XExtDisplayInfo *info = DRI2FindDisplay(dpy);
284 xDRI2CopyRegionReq *req;
285 xDRI2CopyRegionReply rep;
286
287 XextSimpleCheckExtension (dpy, info, dri2ExtensionName);
288
289 LockDisplay(dpy);
290 GetReq(DRI2CopyRegion, req);
291 req->reqType = info->codes->major_opcode;
292 req->dri2ReqType = X_DRI2CopyRegion;
293 req->drawable = drawable;
294 req->region = region;
295 req->dest = dest;
296 req->src = src;
297 req->bitmask = 0;
298
299 _XReply(dpy, (xReply *)&rep, 0, xFalse);
300
301 UnlockDisplay(dpy);
302 SyncHandle();
303 }