OSDN Git Service

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