• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * SDL - Simple DirectMedia Layer
3  * CELL BE Support for PS3 Framebuffer
4  * Copyright (C) 2008, 2009 International Business Machines Corporation
5  *
6  * This library is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published
8  * by the Free Software Foundation; either version 2.1 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
19  * USA
20  *
21  *  Martin Lowinski  <lowinski [at] de [dot] ibm [ibm] com>
22  *  Dirk Herrendoerfer <d.herrendoerfer [at] de [dot] ibm [dot] com>
23  *  SPE code based on research by:
24  *  Rene Becker
25  *  Thimo Emmerich
26  */
27 
28 #include "SDL_config.h"
29 
30 #include "SDL_video.h"
31 #include "SDL_ps3video.h"
32 #include "SDL_ps3yuv_c.h"
33 #include "../SDL_yuvfuncs.h"
34 #include "spulibs/spu_common.h"
35 
36 /* Stores the executable name */
37 extern spe_program_handle_t yuv2rgb_spu;
38 extern spe_program_handle_t bilin_scaler_spu;
39 
40 int SPE_Start(_THIS, spu_data_t * spe_data);
41 int SPE_Stop(_THIS, spu_data_t * spe_data);
42 int SPE_Boot(_THIS, spu_data_t * spe_data);
43 int SPE_Shutdown(_THIS, spu_data_t * spe_data);
44 int SPE_SendMsg(_THIS, spu_data_t * spe_data, unsigned int msg);
45 int SPE_WaitForMsg(_THIS, spu_data_t * spe_data, unsigned int msg);
46 void SPE_RunContext(void *thread_argp);
47 
48 
49 /* The functions used to manipulate software video overlays */
50 static struct private_yuvhwfuncs ps3_yuvfuncs = {
51   PS3_LockYUVOverlay,
52   PS3_UnlockYUVOverlay,
53   PS3_DisplayYUVOverlay,
54   PS3_FreeYUVOverlay
55 };
56 
57 
58 struct private_yuvhwdata {
59 	SDL_Surface *display;
60 	SDL_Surface *stretch;
61     volatile void * pixels __attribute__((aligned(128)));
62 
63 	/* These are just so we don't have to allocate them separately */
64 	Uint16 pitches[3];
65 	Uint8 * planes[3];
66 
67 	unsigned int scale;
68 
69 	/* Scaled YUV picture */
70 	Uint8 * scaler_out __attribute__((aligned(128)));
71 
72 	/* YUV2RGB converter data */
73     volatile struct yuv2rgb_parms_t * converter_parms __attribute__((aligned(128)));
74 
75 	/* Scaler data */
76     volatile struct scale_parms_t * scaler_parms __attribute__((aligned(128)));
77 
78 	Uint8 locked;
79 };
80 
81 
PS3_CreateYUVOverlay(_THIS,int width,int height,Uint32 format,SDL_Surface * display)82 SDL_Overlay *PS3_CreateYUVOverlay(_THIS, int width, int height, Uint32 format, SDL_Surface *display) {
83 	/* Only RGB packed pixel conversion supported */
84 	if ((display->format->BytesPerPixel != 2) &&
85 			(display->format->BytesPerPixel != 3) &&
86 			(display->format->BytesPerPixel != 4))
87 	{
88 		SDL_SetError ("Can't use YUV data on non 16/24/32 bit surfaces");
89 		return NULL;
90 	}
91 
92 	/* Double-check the requested format. We'll only support YV12 */
93 	switch (format) {
94 	    case SDL_IYUV_OVERLAY:
95 		case SDL_YV12_OVERLAY:
96 			/* Supported YUV format */
97 			break;
98 		default:
99 			SDL_SetError("Unsupported YUV format");
100 			return NULL;
101 	}
102 
103 	SDL_Overlay* overlay;
104 	struct private_yuvhwdata* hwdata;
105 
106 	/* Create the overlay structure */
107 	overlay = (SDL_Overlay *) SDL_calloc(1, sizeof(SDL_Overlay));
108 	if (overlay == NULL) {
109 		SDL_OutOfMemory();
110 		return NULL;
111 	}
112 	SDL_memset(overlay, 0, (sizeof *overlay));
113 
114 	/* Set the basic attributes */
115 	overlay->format = format;
116 	overlay->w = width;
117 	overlay->h = height;
118 	overlay->hwdata = NULL;
119 
120 	/* Set up the PS3 YUV surface function structure */
121 	overlay->hwfuncs = &ps3_yuvfuncs;
122 
123 	/* Create the pixel data and lookup tables */
124 	hwdata = (struct private_yuvhwdata *) SDL_calloc(1, sizeof(struct private_yuvhwdata));
125 	if (hwdata == NULL) {
126 		SDL_OutOfMemory();
127 		SDL_FreeYUVOverlay(overlay);
128 		return NULL;
129 	}
130 	overlay->hwdata = hwdata;
131 
132 	hwdata->stretch = NULL;
133 	hwdata->display = display;
134 
135 	/* Create SPU parms structure */
136 	hwdata->converter_parms = (struct yuv2rgb_parms_t *) memalign(16, sizeof(struct yuv2rgb_parms_t));
137 	hwdata->scaler_parms = (struct scale_parms_t *) memalign(16, sizeof(struct scale_parms_t));
138 	if (hwdata->converter_parms == NULL || hwdata->scaler_parms == NULL) {
139 		SDL_FreeYUVOverlay(overlay);
140 		SDL_OutOfMemory();
141 		return(NULL);
142 	}
143 
144 	/* Set up the SPEs */
145 	scaler_thread_data = (spu_data_t *) malloc(sizeof(spu_data_t));
146 	converter_thread_data = (spu_data_t *) malloc(sizeof(spu_data_t));
147 	if (converter_thread_data == NULL || scaler_thread_data == NULL) {
148 		SDL_FreeYUVOverlay(overlay);
149 		SDL_OutOfMemory();
150 		return(NULL);
151 	}
152 
153 	scaler_thread_data->program = bilin_scaler_spu;
154 	scaler_thread_data->program_name = "bilin_scaler_spu";
155 	scaler_thread_data->keepalive = 0;
156 	scaler_thread_data->booted = 0;
157 
158 	converter_thread_data->program = yuv2rgb_spu;
159 	converter_thread_data->program_name = "yuv2rgb_spu";
160 	converter_thread_data->keepalive = 1;
161 	converter_thread_data->booted = 0;
162 
163 	SPE_Start(this, converter_thread_data);
164 
165 	hwdata->pixels = (Uint8 *) memalign(16, width * height + ((width * height) >> 1));
166 	if (hwdata->pixels == NULL) {
167 		SDL_FreeYUVOverlay(overlay);
168 		SDL_OutOfMemory();
169 		return(NULL);
170 	}
171 
172 	/* Find the pitch and offset values for the overlay */
173 	overlay->pitches = hwdata->pitches;
174 	overlay->pixels = hwdata->planes;
175 	switch (format) {
176 	    case SDL_YV12_OVERLAY:
177 	    case SDL_IYUV_OVERLAY:
178 			overlay->pitches[0] = overlay->w;
179 			overlay->pitches[1] = overlay->pitches[0] / 2;
180 			overlay->pitches[2] = overlay->pitches[0] / 2;
181 			overlay->pixels[0] = (Uint8 *)hwdata->pixels;
182 			overlay->pixels[1] = overlay->pixels[0] +
183 				overlay->pitches[0] * overlay->h;
184 			overlay->pixels[2] = overlay->pixels[1] +
185 				overlay->pitches[1] * overlay->h / 2;
186 			overlay->planes = 3;
187 		break;
188 	    default:
189 		/* We should never get here (caught above) */
190 		break;
191 	}
192 
193 	/* We're all done.. */
194 	return overlay;
195 }
196 
197 
PS3_LockYUVOverlay(_THIS,SDL_Overlay * overlay)198 int PS3_LockYUVOverlay(_THIS, SDL_Overlay *overlay) {
199 	if (overlay == NULL) {
200 		return -1;
201 	}
202 	overlay->hwdata->locked = 1;
203 
204 	return 0;
205 }
206 
207 
PS3_UnlockYUVOverlay(_THIS,SDL_Overlay * overlay)208 void PS3_UnlockYUVOverlay(_THIS, SDL_Overlay *overlay) {
209 	if (overlay == NULL) {
210 		return;
211 	}
212 	overlay->hwdata->locked = 0;
213 
214 	return;
215 }
216 
217 
PS3_DisplayYUVOverlay(_THIS,SDL_Overlay * overlay,SDL_Rect * src,SDL_Rect * dst)218 int PS3_DisplayYUVOverlay(_THIS, SDL_Overlay *overlay, SDL_Rect *src, SDL_Rect *dst) {
219 	if ((overlay == NULL) || (overlay->hwdata == NULL)) {
220 		return -1;
221 	}
222 
223 	Uint8 *lum, *Cr, *Cb;
224 	struct private_yuvhwdata *hwdata;
225 	SDL_Surface *display;
226 
227 	hwdata = overlay->hwdata;
228 	display = hwdata->display;
229 
230 	/* Do we have to scale? */
231 	if ((src->w != dst->w) || (src->h != dst->h) ) {
232 		hwdata->scale = 1;
233 		deprintf(1, "[PS3] We need to scale\n");
234 	} else {
235 		hwdata->scale = 0;
236 		deprintf(1, "[PS3] No scaling\n");
237 	}
238 
239 	/* Find out where the various portions of the image are */
240 	switch (overlay->format) {
241 		case SDL_YV12_OVERLAY:
242 			lum = (Uint8 *)overlay->pixels[0];
243 			Cr =  (Uint8 *)overlay->pixels[1];
244 			Cb =  (Uint8 *)overlay->pixels[2];
245 			break;
246 		case SDL_IYUV_OVERLAY:
247 			lum = (Uint8 *)overlay->pixels[0];
248 			Cr =  (Uint8 *)overlay->pixels[2];
249 			Cb =  (Uint8 *)overlay->pixels[1];
250 			break;
251 		default:
252 			SDL_SetError("Unsupported YUV format in blit");
253 			return -1;
254 	}
255 
256 	if (hwdata->scale) {
257 		/* Alloc mem for scaled YUV picture */
258 		hwdata->scaler_out = (Uint8 *) memalign(16, dst->w * dst->h + ((dst->w * dst->h) >> 1));
259 		if (hwdata->scaler_out == NULL) {
260 			SDL_FreeYUVOverlay(overlay);
261 			SDL_OutOfMemory();
262 			return -1;
263 		}
264 
265 		/* Set parms for scaling */
266 		hwdata->scaler_parms->src_pixel_width = src->w;
267 		hwdata->scaler_parms->src_pixel_height = src->h;
268 		hwdata->scaler_parms->dst_pixel_width = dst->w;
269 		hwdata->scaler_parms->dst_pixel_height = dst->h;
270 		hwdata->scaler_parms->y_plane = lum;
271 		hwdata->scaler_parms->v_plane = Cr;
272 		hwdata->scaler_parms->u_plane = Cb;
273 		hwdata->scaler_parms->dstBuffer = hwdata->scaler_out;
274 		scaler_thread_data->argp = (void *)hwdata->scaler_parms;
275 
276 		/* Scale the YUV overlay to given size */
277 		SPE_Start(this, scaler_thread_data);
278 		SPE_Stop(this, scaler_thread_data);
279 
280 		/* Set parms for converting after scaling */
281 		hwdata->converter_parms->y_plane = hwdata->scaler_out;
282 		hwdata->converter_parms->v_plane = hwdata->scaler_out + dst->w * dst->h;
283 		hwdata->converter_parms->u_plane = hwdata->scaler_out + dst->w * dst->h + ((dst->w * dst->h) >> 2);
284 	} else {
285 		/* Set parms for converting */
286 		hwdata->converter_parms->y_plane = lum;
287 		hwdata->converter_parms->v_plane = Cr;
288 		hwdata->converter_parms->u_plane = Cb;
289 	}
290 
291 	hwdata->converter_parms->src_pixel_width = dst->w;
292 	hwdata->converter_parms->src_pixel_height = dst->h;
293 	hwdata->converter_parms->dstBuffer = (Uint8 *) s_pixels;
294 	converter_thread_data->argp = (void *)hwdata->converter_parms;
295 
296 	/* Convert YUV overlay to RGB */
297 	SPE_SendMsg(this, converter_thread_data, SPU_START);
298 	SPE_SendMsg(this, converter_thread_data, (unsigned int)converter_thread_data->argp);
299 
300 	/* Centering */
301 	s_bounded_input_width = dst->w;
302 	s_bounded_input_height = dst->h;
303 
304 	/* UpdateRects() will do the rest.. */
305 	SDL_UpdateRects(display, 1, dst);
306 
307 	if (hwdata->scale)
308 		SDL_free((void *)hwdata->scaler_out);
309 
310 	return 0;
311 }
312 
313 
PS3_FreeYUVOverlay(_THIS,SDL_Overlay * overlay)314 void PS3_FreeYUVOverlay(_THIS, SDL_Overlay *overlay) {
315 	if (overlay == NULL) {
316 		return;
317 	}
318 
319 	if (overlay->hwdata == NULL) {
320 		return;
321 	}
322 
323 	struct private_yuvhwdata * hwdata;
324 	hwdata = overlay->hwdata;
325 
326 	if (scaler_thread_data)
327 		SDL_free(scaler_thread_data);
328 	if (converter_thread_data) {
329 		SPE_Shutdown(this, converter_thread_data);
330 		SDL_free(converter_thread_data);
331 	}
332 
333 	if (hwdata) {
334 		if (hwdata->pixels)
335 			SDL_free((void *)hwdata->pixels);
336 		SDL_free(hwdata);
337 	}
338 	return;
339 }
340 
341