99433796856c330962d4e48db996e0ace945de60
[platform/upstream/libva.git] / src / X11 / va_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 "xf86drm.h"
39 #include "va_dri2.h"
40 #include "va_dri2str.h"
41
42 static char va_dri2ExtensionName[] = DRI2_NAME;
43 static XExtensionInfo _va_dri2_info_data;
44 static XExtensionInfo *va_dri2Info = &_va_dri2_info_data;
45 static XEXT_GENERATE_CLOSE_DISPLAY (VA_DRI2CloseDisplay, va_dri2Info)
46 static /* const */ XExtensionHooks va_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     VA_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, va_dri2Info, 
61                                    va_dri2ExtensionName, 
62                                    &va_dri2ExtensionHooks, 
63                                    0, NULL)
64
65 Bool VA_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 VA_DRI2QueryVersion(Display *dpy, int *major, int *minor)
79 {
80     XExtDisplayInfo *info = DRI2FindDisplay (dpy);
81     xDRI2QueryVersionReply rep;
82     xDRI2QueryVersionReq *req;
83
84     XextCheckExtension (dpy, info, va_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 VA_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, va_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 VA_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, va_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 VA_DRI2CreateDrawable(Display *dpy, XID drawable)
189 {
190     XExtDisplayInfo *info = DRI2FindDisplay(dpy);
191     xDRI2CreateDrawableReq *req;
192
193     XextSimpleCheckExtension (dpy, info, va_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 VA_DRI2DestroyDrawable(Display *dpy, XID drawable)
205 {
206     XExtDisplayInfo *info = DRI2FindDisplay(dpy);
207     xDRI2DestroyDrawableReq *req;
208
209     XextSimpleCheckExtension (dpy, info, va_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 VA_DRI2Buffer *VA_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     VA_DRI2Buffer *buffers;
231     xDRI2Buffer repBuffer;
232     CARD32 *p;
233     int i;
234
235     XextCheckExtension (dpy, info, va_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(rep.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 VA_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, va_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
298     _XReply(dpy, (xReply *)&rep, 0, xFalse);
299
300     UnlockDisplay(dpy);
301     SyncHandle();
302 }