2 * SDL - Simple DirectMedia Layer
3 * CELL BE Support for PS3 Framebuffer
4 * Copyright (C) 2008, 2009 International Business Machines Corporation
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.
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.
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
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:
28 #include "SDL_config.h"
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"
36 /* Stores the executable name */
37 extern spe_program_handle_t yuv2rgb_spu;
38 extern spe_program_handle_t bilin_scaler_spu;
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);
49 /* The functions used to manipulate software video overlays */
50 static struct private_yuvhwfuncs ps3_yuvfuncs = {
53 PS3_DisplayYUVOverlay,
58 struct private_yuvhwdata {
61 volatile void * pixels __attribute__((aligned(128)));
63 /* These are just so we don't have to allocate them separately */
69 /* Scaled YUV picture */
70 Uint8 * scaler_out __attribute__((aligned(128)));
72 /* YUV2RGB converter data */
73 volatile struct yuv2rgb_parms_t * converter_parms __attribute__((aligned(128)));
76 volatile struct scale_parms_t * scaler_parms __attribute__((aligned(128)));
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))
88 SDL_SetError ("Can't use YUV data on non 16/24/32 bit surfaces");
92 /* Double-check the requested format. We'll only support YV12 */
94 case SDL_IYUV_OVERLAY:
95 case SDL_YV12_OVERLAY:
96 /* Supported YUV format */
99 SDL_SetError("Unsupported YUV format");
103 SDL_Overlay* overlay;
104 struct private_yuvhwdata* hwdata;
106 /* Create the overlay structure */
107 overlay = (SDL_Overlay *) SDL_calloc(1, sizeof(SDL_Overlay));
108 if (overlay == NULL) {
112 SDL_memset(overlay, 0, (sizeof *overlay));
114 /* Set the basic attributes */
115 overlay->format = format;
118 overlay->hwdata = NULL;
120 /* Set up the PS3 YUV surface function structure */
121 overlay->hwfuncs = &ps3_yuvfuncs;
123 /* Create the pixel data and lookup tables */
124 hwdata = (struct private_yuvhwdata *) SDL_calloc(1, sizeof(struct private_yuvhwdata));
125 if (hwdata == NULL) {
127 SDL_FreeYUVOverlay(overlay);
130 overlay->hwdata = hwdata;
132 hwdata->stretch = NULL;
133 hwdata->display = display;
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);
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);
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;
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;
163 SPE_Start(this, converter_thread_data);
165 hwdata->pixels = (Uint8 *) memalign(16, width * height + ((width * height) >> 1));
166 if (hwdata->pixels == NULL) {
167 SDL_FreeYUVOverlay(overlay);
172 /* Find the pitch and offset values for the overlay */
173 overlay->pitches = hwdata->pitches;
174 overlay->pixels = hwdata->planes;
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;
189 /* We should never get here (caught above) */
193 /* We're all done.. */
198 int PS3_LockYUVOverlay(_THIS, SDL_Overlay *overlay) {
199 if (overlay == NULL) {
202 overlay->hwdata->locked = 1;
208 void PS3_UnlockYUVOverlay(_THIS, SDL_Overlay *overlay) {
209 if (overlay == NULL) {
212 overlay->hwdata->locked = 0;
218 int PS3_DisplayYUVOverlay(_THIS, SDL_Overlay *overlay, SDL_Rect *src, SDL_Rect *dst) {
219 if ((overlay == NULL) || (overlay->hwdata == NULL)) {
223 Uint8 *lum, *Cr, *Cb;
224 struct private_yuvhwdata *hwdata;
225 SDL_Surface *display;
227 hwdata = overlay->hwdata;
228 display = hwdata->display;
230 /* Do we have to scale? */
231 if ((src->w != dst->w) || (src->h != dst->h) ) {
233 deprintf(1, "[PS3] We need to scale\n");
236 deprintf(1, "[PS3] No scaling\n");
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];
246 case SDL_IYUV_OVERLAY:
247 lum = (Uint8 *)overlay->pixels[0];
248 Cr = (Uint8 *)overlay->pixels[2];
249 Cb = (Uint8 *)overlay->pixels[1];
252 SDL_SetError("Unsupported YUV format in blit");
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);
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;
276 /* Scale the YUV overlay to given size */
277 SPE_Start(this, scaler_thread_data);
278 SPE_Stop(this, scaler_thread_data);
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);
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;
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;
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);
301 s_bounded_input_width = dst->w;
302 s_bounded_input_height = dst->h;
304 /* UpdateRects() will do the rest.. */
305 SDL_UpdateRects(display, 1, dst);
308 SDL_free((void *)hwdata->scaler_out);
314 void PS3_FreeYUVOverlay(_THIS, SDL_Overlay *overlay) {
315 if (overlay == NULL) {
319 if (overlay->hwdata == NULL) {
323 struct private_yuvhwdata * hwdata;
324 hwdata = overlay->hwdata;
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);
335 SDL_free((void *)hwdata->pixels);