Mercurial > sdl-ios-xcode
comparison src/video/x11/SDL_x11yuv.c @ 0:74212992fb08
Initial revision
author | Sam Lantinga <slouken@lokigames.com> |
---|---|
date | Thu, 26 Apr 2001 16:45:43 +0000 |
parents | |
children | d4ebd1bbea9a |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:74212992fb08 |
---|---|
1 /* | |
2 SDL - Simple DirectMedia Layer | |
3 Copyright (C) 1997, 1998, 1999, 2000, 2001 Sam Lantinga | |
4 | |
5 This library is free software; you can redistribute it and/or | |
6 modify it under the terms of the GNU Library General Public | |
7 License as published by the Free Software Foundation; either | |
8 version 2 of the License, or (at your option) any later version. | |
9 | |
10 This library is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 Library General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU Library General Public | |
16 License along with this library; if not, write to the Free | |
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | |
19 Sam Lantinga | |
20 slouken@devolution.com | |
21 */ | |
22 | |
23 #ifdef SAVE_RCSID | |
24 static char rcsid = | |
25 "@(#) $Id$"; | |
26 #endif | |
27 | |
28 /* This is the XFree86 Xv extension implementation of YUV video overlays */ | |
29 | |
30 #ifdef XFREE86_XV | |
31 | |
32 #include <stdlib.h> | |
33 #include <string.h> | |
34 #include <X11/Xlib.h> | |
35 #include <sys/ipc.h> | |
36 #include <sys/shm.h> | |
37 #include <X11/extensions/XShm.h> | |
38 #include <X11/extensions/Xvlib.h> | |
39 | |
40 #include "SDL_error.h" | |
41 #include "SDL_video.h" | |
42 #include "SDL_x11yuv_c.h" | |
43 #include "SDL_yuvfuncs.h" | |
44 | |
45 #define XFREE86_REFRESH_HACK | |
46 #ifdef XFREE86_REFRESH_HACK | |
47 #include "SDL_x11image_c.h" | |
48 #endif | |
49 | |
50 /* Workaround when pitch != width */ | |
51 #define PITCH_WORKAROUND | |
52 | |
53 /* The functions used to manipulate software video overlays */ | |
54 static struct private_yuvhwfuncs x11_yuvfuncs = { | |
55 X11_LockYUVOverlay, | |
56 X11_UnlockYUVOverlay, | |
57 X11_DisplayYUVOverlay, | |
58 X11_FreeYUVOverlay | |
59 }; | |
60 | |
61 struct private_yuvhwdata { | |
62 int port; | |
63 XShmSegmentInfo yuvshm; | |
64 XvImage *image; | |
65 }; | |
66 | |
67 | |
68 SDL_Overlay *X11_CreateYUVOverlay(_THIS, int width, int height, Uint32 format, SDL_Surface *display) | |
69 { | |
70 SDL_Overlay *overlay; | |
71 struct private_yuvhwdata *hwdata; | |
72 int xv_port; | |
73 int i, j, k; | |
74 int adaptors; | |
75 XvAdaptorInfo *ainfo; | |
76 XShmSegmentInfo *yuvshm; | |
77 | |
78 /* Look for the XVideo extension with a valid port for this format */ | |
79 xv_port = -1; | |
80 if ( (Success == XvQueryExtension(GFX_Display, &j, &j, &j, &j, &j)) && | |
81 (Success == XvQueryAdaptors(GFX_Display, | |
82 RootWindow(GFX_Display, SDL_Screen), | |
83 &adaptors, &ainfo)) ) { | |
84 for ( i=0; (i < adaptors) && (xv_port == -1); ++i ) { | |
85 /* Check to see if the visual can be used */ | |
86 if ( BUGGY_XFREE86(<=, 4001) ) { | |
87 int visual_ok = 0; | |
88 for ( j=0; j<ainfo[i].num_formats; ++j ) { | |
89 if ( ainfo[i].formats[j].visual_id == | |
90 SDL_Visual->visualid ) { | |
91 visual_ok = 1; | |
92 break; | |
93 } | |
94 } | |
95 if ( ! visual_ok ) { | |
96 continue; | |
97 } | |
98 } | |
99 if ( (ainfo[i].type & XvInputMask) && | |
100 (ainfo[i].type & XvImageMask) ) { | |
101 int num_formats; | |
102 XvImageFormatValues *formats; | |
103 formats = XvListImageFormats(GFX_Display, | |
104 ainfo[i].base_id, &num_formats); | |
105 for ( j=0; (j < num_formats) && (xv_port == -1); ++j ) { | |
106 if ( (Uint32)formats[j].id == format ) { | |
107 for ( k=0; k < ainfo[i].num_ports; ++k ) { | |
108 if ( Success == XvGrabPort(GFX_Display, ainfo[i].base_id+k, CurrentTime) ) { | |
109 xv_port = ainfo[i].base_id+k; | |
110 break; | |
111 } | |
112 } | |
113 } | |
114 } | |
115 } | |
116 } | |
117 } | |
118 if ( xv_port == -1 ) { | |
119 SDL_SetError("No available video ports for requested format"); | |
120 return(NULL); | |
121 } | |
122 | |
123 /* Create the overlay structure */ | |
124 overlay = (SDL_Overlay *)malloc(sizeof *overlay); | |
125 if ( overlay == NULL ) { | |
126 XvUngrabPort(GFX_Display, xv_port, CurrentTime); | |
127 SDL_OutOfMemory(); | |
128 return(NULL); | |
129 } | |
130 memset(overlay, 0, (sizeof *overlay)); | |
131 | |
132 /* Fill in the basic members */ | |
133 overlay->format = format; | |
134 overlay->w = width; | |
135 overlay->h = height; | |
136 | |
137 /* Set up the YUV surface function structure */ | |
138 overlay->hwfuncs = &x11_yuvfuncs; | |
139 overlay->hw_overlay = 1; | |
140 | |
141 /* Create the pixel data and lookup tables */ | |
142 hwdata = (struct private_yuvhwdata *)malloc(sizeof *hwdata); | |
143 overlay->hwdata = hwdata; | |
144 if ( hwdata == NULL ) { | |
145 XvUngrabPort(GFX_Display, xv_port, CurrentTime); | |
146 SDL_OutOfMemory(); | |
147 SDL_FreeYUVOverlay(overlay); | |
148 return(NULL); | |
149 } | |
150 yuvshm = &hwdata->yuvshm; | |
151 memset(yuvshm, 0, sizeof(*yuvshm)); | |
152 hwdata->port = xv_port; | |
153 hwdata->image = XvShmCreateImage(GFX_Display, xv_port, format, | |
154 0, width, height, yuvshm); | |
155 | |
156 #ifdef PITCH_WORKAROUND | |
157 if ( hwdata->image != NULL && hwdata->image->pitches[0] != width ) | |
158 { | |
159 /* Ajust overlay width according to pitch */ | |
160 switch (format) { | |
161 case SDL_YV12_OVERLAY: | |
162 case SDL_IYUV_OVERLAY: | |
163 width = hwdata->image->pitches[0]; | |
164 break; | |
165 case SDL_YUY2_OVERLAY: | |
166 case SDL_UYVY_OVERLAY: | |
167 case SDL_YVYU_OVERLAY: | |
168 width = hwdata->image->pitches[0] / 2; | |
169 break; | |
170 default: | |
171 /* We should never get here (caught above) */ | |
172 return(NULL); | |
173 } | |
174 | |
175 XFree(hwdata->image); | |
176 hwdata->image = XvShmCreateImage(GFX_Display, xv_port, format, | |
177 0, width, height, yuvshm); | |
178 } | |
179 #endif | |
180 | |
181 if ( hwdata->image == NULL ) { | |
182 SDL_OutOfMemory(); | |
183 SDL_FreeYUVOverlay(overlay); | |
184 return(NULL); | |
185 } | |
186 yuvshm->shmid = shmget(IPC_PRIVATE, hwdata->image->data_size, | |
187 IPC_CREAT | 0777); | |
188 if ( yuvshm->shmid < 0 ) { | |
189 SDL_SetError("Unable to get %d bytes shared memory", | |
190 hwdata->image->data_size); | |
191 SDL_FreeYUVOverlay(overlay); | |
192 return(NULL); | |
193 } | |
194 yuvshm->shmaddr = (char *) shmat(yuvshm->shmid, 0, 0); | |
195 yuvshm->readOnly = False; | |
196 hwdata->image->data = yuvshm->shmaddr; | |
197 | |
198 XShmAttach(GFX_Display, yuvshm); | |
199 XSync(GFX_Display, False); | |
200 shmctl(yuvshm->shmid, IPC_RMID, 0); | |
201 | |
202 /* Find the pitch and offset values for the overlay */ | |
203 overlay->planes = hwdata->image->num_planes; | |
204 overlay->pitches = (Uint16 *)malloc(overlay->planes * sizeof(Uint16)); | |
205 overlay->pixels = (Uint8 **)malloc(overlay->planes * sizeof(Uint8 *)); | |
206 if ( !overlay->pitches || !overlay->pixels ) { | |
207 SDL_OutOfMemory(); | |
208 SDL_FreeYUVOverlay(overlay); | |
209 return(NULL); | |
210 } | |
211 for ( i=0; i<overlay->planes; ++i ) { | |
212 overlay->pitches[i] = hwdata->image->pitches[i]; | |
213 overlay->pixels[i] = (Uint8 *)hwdata->image->data + | |
214 hwdata->image->offsets[i]; | |
215 } | |
216 | |
217 #ifdef XFREE86_REFRESH_HACK | |
218 /* Work around an XFree86 X server bug (?) | |
219 We can't perform normal updates in windows that have video | |
220 being output to them. See SDL_x11image.c for more details. | |
221 */ | |
222 X11_DisableAutoRefresh(this); | |
223 #endif | |
224 | |
225 /* We're all done.. */ | |
226 return(overlay); | |
227 } | |
228 | |
229 int X11_LockYUVOverlay(_THIS, SDL_Overlay *overlay) | |
230 { | |
231 return(0); | |
232 } | |
233 | |
234 void X11_UnlockYUVOverlay(_THIS, SDL_Overlay *overlay) | |
235 { | |
236 return; | |
237 } | |
238 | |
239 int X11_DisplayYUVOverlay(_THIS, SDL_Overlay *overlay, SDL_Rect *dstrect) | |
240 { | |
241 struct private_yuvhwdata *hwdata; | |
242 | |
243 hwdata = overlay->hwdata; | |
244 XvShmPutImage(GFX_Display, hwdata->port, SDL_Window, SDL_GC, | |
245 hwdata->image, 0, 0, overlay->w, overlay->h, | |
246 dstrect->x, dstrect->y, dstrect->w, dstrect->h, False); | |
247 XSync(GFX_Display, False); | |
248 return(0); | |
249 } | |
250 | |
251 void X11_FreeYUVOverlay(_THIS, SDL_Overlay *overlay) | |
252 { | |
253 struct private_yuvhwdata *hwdata; | |
254 | |
255 hwdata = overlay->hwdata; | |
256 if ( hwdata ) { | |
257 XvUngrabPort(GFX_Display, hwdata->port, CurrentTime); | |
258 if ( hwdata->yuvshm.shmaddr ) { | |
259 XShmDetach(GFX_Display, &hwdata->yuvshm); | |
260 shmdt(hwdata->yuvshm.shmaddr); | |
261 } | |
262 if ( hwdata->image ) { | |
263 XFree(hwdata->image); | |
264 } | |
265 free(hwdata); | |
266 } | |
267 if ( overlay->pitches ) { | |
268 free(overlay->pitches); | |
269 overlay->pitches = NULL; | |
270 } | |
271 if ( overlay->pixels ) { | |
272 free(overlay->pixels); | |
273 overlay->pixels = NULL; | |
274 } | |
275 #ifdef XFREE86_REFRESH_HACK | |
276 X11_EnableAutoRefresh(this); | |
277 #endif | |
278 } | |
279 | |
280 #endif /* XFREE86_XV */ |