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_sysvideo.h"
32 #include "SDL_ps3events_c.h"
33 #include "SDL_ps3video.h"
34 #include "SDL_ps3yuv_c.h"
35 #include "spulibs/spu_common.h"
39 #include <sys/ioctl.h>
44 #include <asm/ps3fb.h>
48 /* SDL_VideoDevice functions */
49 static int PS3_Available();
50 static SDL_VideoDevice *PS3_CreateDevice(int devindex);
51 static int PS3_VideoInit(_THIS, SDL_PixelFormat * vformat);
52 static void PS3_VideoQuit(_THIS);
53 static void PS3_DeleteDevice(SDL_VideoDevice * device);
54 static SDL_Surface *PS3_SetVideoMode(_THIS, SDL_Surface * current, int width, int height, int bpp, Uint32 flags);
55 static SDL_Rect **PS3_ListModes(_THIS, SDL_PixelFormat * format, Uint32 flags);
57 /* Hardware surface functions */
58 static int PS3_AllocHWSurface(_THIS, SDL_Surface * surface);
59 static void PS3_FreeHWSurface(_THIS, SDL_Surface * surface);
60 static int PS3_LockHWSurface(_THIS, SDL_Surface * surface);
61 static void PS3_UnlockHWSurface(_THIS, SDL_Surface * surface);
62 static int PS3_FlipDoubleBuffer(_THIS, SDL_Surface * surface);
63 static void PS3_DoubleBufferUpdate(_THIS, int numrects, SDL_Rect * rects);
65 /* SPU specific functions */
66 int SPE_Start(_THIS, spu_data_t * spe_data);
67 int SPE_Stop(_THIS, spu_data_t * spe_data);
68 int SPE_Boot(_THIS, spu_data_t * spe_data);
69 int SPE_Shutdown(_THIS, spu_data_t * spe_data);
70 int SPE_SendMsg(_THIS, spu_data_t * spe_data, unsigned int msg);
71 int SPE_WaitForMsg(_THIS, spu_data_t * spe_data, unsigned int msg);
72 void SPE_RunContext(void *thread_argp);
75 void enable_cursor(int enable);
77 /* Stores the SPE executable name of fb_writer_spu */
78 extern spe_program_handle_t fb_writer_spu;
80 /* SDL PS3 bootstrap function for checking availability */
81 static int PS3_Available()
86 /* SDL PS3 bootstrap function for creating the device */
87 static SDL_VideoDevice *PS3_CreateDevice(int devindex)
89 SDL_VideoDevice *this;
91 /* Initialise SDL_VideoDevice */
92 this = (SDL_VideoDevice *) SDL_malloc(sizeof(SDL_VideoDevice));
94 memset(this, 0, sizeof *this);
95 this->hidden = (struct SDL_PrivateVideoData *)
96 SDL_malloc(sizeof(struct SDL_PrivateVideoData));
99 if ((this == NULL) || (this->hidden == NULL)) {
105 memset(this->hidden, 0, sizeof(struct SDL_PrivateVideoData));
107 /* Set the function pointers */
108 this->VideoInit = PS3_VideoInit;
109 this->ListModes = PS3_ListModes;
110 this->SetVideoMode = PS3_SetVideoMode;
112 this->CreateYUVOverlay = PS3_CreateYUVOverlay;
113 this->UpdateRects = 0;
114 this->VideoQuit = PS3_VideoQuit;
115 this->AllocHWSurface = PS3_AllocHWSurface;
116 this->CheckHWBlit = 0;
117 this->FillHWRect = 0;
118 this->SetHWColorKey = 0;
119 this->SetHWAlpha = 0;
120 this->LockHWSurface = PS3_LockHWSurface;
121 this->UnlockHWSurface = PS3_UnlockHWSurface;
122 this->FlipHWSurface = PS3_FlipDoubleBuffer;
123 this->FreeHWSurface = PS3_FreeHWSurface;
124 this->SetCaption = 0;
126 this->IconifyWindow = 0;
129 this->InitOSKeymap = PS3_InitOSKeymap;
130 this->PumpEvents = PS3_PumpEvents;
132 this->free = PS3_DeleteDevice;
138 /* Bootstraping (see SDL_sysvideo.h) */
139 VideoBootStrap PS3_bootstrap = {
140 "ps3", "PS3 Cell SPU Driver",
141 PS3_Available, PS3_CreateDevice
145 /* Delete the device */
146 static void PS3_DeleteDevice(SDL_VideoDevice * device)
148 free(device->hidden);
153 /* Initialise the PS3 video device */
154 static int PS3_VideoInit(_THIS, SDL_PixelFormat * vformat)
156 /* Hide the cursor */
159 /* Create SPU fb_parms and thread structure */
160 fb_parms = (struct fb_writer_parms_t *)
161 memalign(16, sizeof(struct fb_writer_parms_t));
162 fb_thread_data = (spu_data_t *) malloc(sizeof(spu_data_t));
163 if (fb_parms == NULL || fb_thread_data == NULL) {
167 fb_thread_data->program = fb_writer_spu;
168 fb_thread_data->program_name = "fb_writer_spu";
169 fb_thread_data->argp = (void *)fb_parms;
170 fb_thread_data->keepalive = 1;
171 fb_thread_data->booted = 0;
173 SPE_Start(this, fb_thread_data);
175 /* Open the device */
176 fb_dev_fd = open(PS3_DEV_FB, O_RDWR);
178 SDL_SetError("[PS3] Unable to open device %s", PS3_DEV_FB);
182 /* Get vscreeninfo */
183 if (ioctl(fb_dev_fd, FBIOGET_VSCREENINFO, &fb_vinfo)) {
184 SDL_SetError("[PS3] Can't get VSCREENINFO");
191 /* Fill in our hardware acceleration capabilities */
192 this->info.current_w = fb_vinfo.xres;
193 this->info.current_h = fb_vinfo.yres;
194 this->info.wm_available = 0;
195 this->info.hw_available = 1;
197 /* Backup the original vinfo to restore later */
198 fb_orig_vinfo = fb_vinfo;
200 /* 16 and 15 bpp is reported as 16 bpp */
201 fb_bits_per_pixel = fb_vinfo.bits_per_pixel;
202 if (fb_bits_per_pixel == 16)
204 fb_vinfo.red.length + fb_vinfo.green.length +
205 fb_vinfo.blue.length;
207 /* Set SDL_PixelFormat */
208 vformat->BitsPerPixel = fb_vinfo.bits_per_pixel;
210 fb_vinfo.xres_virtual = fb_vinfo.xres;
211 fb_vinfo.yres_virtual = fb_vinfo.yres;
213 /* Put vscreeninfo */
214 if (ioctl(fb_dev_fd, FBIOPUT_VSCREENINFO, &fb_vinfo)) {
215 SDL_SetError("[PS3] Can't put VSCREENINFO");
222 s_fb_pixel_size = fb_vinfo.bits_per_pixel / 8;
224 s_writeable_width = fb_vinfo.xres;
225 s_writeable_height = fb_vinfo.yres;
227 /* Get ps3 screeninfo */
228 if (ioctl(fb_dev_fd, PS3FB_IOCTL_SCREENINFO, (unsigned long)&res) < 0) {
229 SDL_SetError("[PS3] PS3FB_IOCTL_SCREENINFO failed");
231 deprintf(1, "[PS3] xres:%d yres:%d xoff:%d yoff:%d\n", res.xres, res.yres, res.xoff, res.yoff);
233 /* Only use double buffering if enough fb memory is available */
234 if (res.num_frames < 2) {
235 double_buffering = 0;
237 double_buffering = 1;
240 real_width = res.xres;
241 real_height = res.yres;
244 * Take control of frame buffer from kernel, for details see
245 * http://felter.org/wesley/files/ps3/linux-20061110-docs/ApplicationProgrammingEnvironment.html
246 * kernel will no longer flip the screen itself
248 ioctl(fb_dev_fd, PS3FB_IOCTL_ON, 0);
251 ioctl(fb_dev_fd, FBIOBLANK, 0);
257 /* List available PS3 resolutions */
258 static SDL_Rect **PS3_ListModes(_THIS, SDL_PixelFormat * format, Uint32 flags)
260 /* A list of video resolutions that we query for (sorted largest to
263 static SDL_Rect PS3_resolutions[] = {
264 {0, 0, 1920, 1080}, // 1080p 16:9 HD
265 {0, 0, 1600, 1200}, // WUXGA
266 {0, 0, 1280, 1024}, // SXGA
267 {0, 0, 1280, 720}, // 720p 16:9 HD
268 {0, 0, 1024, 768}, // WXGA
269 {0, 0, 1024, 576}, // 576p 16:9
270 {0, 0, 853, 480}, // 480p 16:9
271 {0, 0, 720, 576}, // 576p 4:3 (PAL)
272 {0, 0, 720, 480}, // 480p 16:9 (NTSC)
274 static SDL_Rect *PS3_modes[] = {
286 SDL_Rect **modes = PS3_modes;
292 /* Get a list of the available display modes */
293 static SDL_Surface *PS3_SetVideoMode(_THIS, SDL_Surface * current, int width, int height, int bpp, Uint32 flags)
295 s_bounded_input_width = width < s_writeable_width ? width : s_writeable_width;
296 s_bounded_input_height = height < s_writeable_height ? height : s_writeable_height;
297 s_bounded_input_width_offset = (s_writeable_width - s_bounded_input_width) >> 1;
298 s_bounded_input_height_offset = (s_writeable_height - s_bounded_input_height) >> 1;
299 s_input_line_length = width * s_fb_pixel_size;
301 current->flags |= flags;
303 if (ioctl(fb_dev_fd, FBIOGET_FSCREENINFO, &fb_finfo)) {
304 SDL_SetError("[PS3] Can't get fixed screeninfo");
308 if (fb_finfo.type != FB_TYPE_PACKED_PIXELS) {
309 SDL_SetError("[PS3] type %s not supported",
314 /* Note: on PS3, fb_finfo.smem_len is enough for double buffering */
316 (uint8_t *) mmap(0, fb_finfo.smem_len,
317 PROT_READ | PROT_WRITE, MAP_SHARED,
318 fb_dev_fd, 0)) == (uint8_t *) - 1) {
319 SDL_SetError("[PS3] Can't mmap for %s", PS3_DEV_FB);
322 current->flags |= SDL_DOUBLEBUF;
324 if (!SDL_ReallocFormat(current, fb_bits_per_pixel, 0, 0, 0, 0)) {
329 memset(frame_buffer, 0x00, fb_finfo.smem_len);
333 frame_buffer + s_bounded_input_width_offset * s_fb_pixel_size +
334 s_bounded_input_height_offset * fb_finfo.line_length;
335 s_center[1] = s_center[0] + real_height * fb_finfo.line_length;
338 current->flags |= SDL_FULLSCREEN;
341 current->pitch = SDL_CalculatePitch(current);
343 /* Alloc aligned mem for current->pixels */
344 s_pixels = memalign(16, current->h * current->pitch);
345 current->pixels = (void *)s_pixels;
346 if (!current->pixels) {
351 /* Set the update rectangle function */
352 this->UpdateRects = PS3_DoubleBufferUpdate;
358 /* Copy screen to framebuffer and flip */
359 void PS3_DoubleBufferUpdate(_THIS, int numrects, SDL_Rect * rects)
361 if (converter_thread_data && converter_thread_data->booted)
362 SPE_WaitForMsg(this, converter_thread_data, SPU_FIN);
364 /* Adjust centering */
365 s_bounded_input_width_offset = (s_writeable_width - s_bounded_input_width) >> 1;
366 s_bounded_input_height_offset = (s_writeable_height - s_bounded_input_height) >> 1;
367 s_center[0] = frame_buffer + s_bounded_input_width_offset * s_fb_pixel_size +
368 s_bounded_input_height_offset * fb_finfo.line_length;
369 s_center[1] = s_center[0] + real_height * fb_finfo.line_length;
371 /* Set SPU parms for copying the surface to framebuffer */
372 fb_parms->data = (unsigned char *)s_pixels;
373 fb_parms->center = s_center[s_center_index];
374 fb_parms->out_line_stride = fb_finfo.line_length;
375 fb_parms->in_line_stride = s_input_line_length;
376 fb_parms->bounded_input_height = s_bounded_input_height;
377 fb_parms->bounded_input_width = s_bounded_input_width;
378 fb_parms->fb_pixel_size = s_fb_pixel_size;
380 deprintf(3, "[PS3->SPU] fb_thread_data->argp = 0x%x\n", fb_thread_data->argp);
383 SPE_SendMsg(this, fb_thread_data, SPU_START);
384 SPE_SendMsg(this, fb_thread_data, (unsigned int)fb_thread_data->argp);
386 SPE_WaitForMsg(this, fb_thread_data, SPU_FIN);
389 if (double_buffering)
390 s_center_index = s_center_index ^ 0x01;
391 PS3_FlipDoubleBuffer(this, this->screen);
395 /* Enable/Disable cursor */
396 void enable_cursor(int enable)
398 int fd = open("/dev/console", O_RDWR | O_NONBLOCK);
400 ioctl(fd, KDSETMODE, enable ? KD_TEXT : KD_GRAPHICS);
406 static int PS3_AllocHWSurface(_THIS, SDL_Surface * surface)
412 static void PS3_FreeHWSurface(_THIS, SDL_Surface * surface)
418 static int PS3_LockHWSurface(_THIS, SDL_Surface * surface)
424 static void PS3_UnlockHWSurface(_THIS, SDL_Surface * surface)
430 /* Blit/Flip buffer to the screen. Must be called after each frame! */
431 int PS3_FlipDoubleBuffer(_THIS, SDL_Surface * surface)
433 unsigned long crt = 0;
435 deprintf(1, "[PS3] Wait for vsync\n");
436 ioctl(fb_dev_fd, FBIO_WAITFORVSYNC, &crt);
438 deprintf(1, "[PS3] Page flip to buffer #%u 0x%x\n", s_center_index, s_center[s_center_index]);
439 ioctl(fb_dev_fd, PS3FB_IOCTL_FSEL, (unsigned long)&s_center_index);
444 /* Start the SPE thread */
445 int SPE_Start(_THIS, spu_data_t * spe_data)
447 deprintf(2, "[PS3->SPU] Start SPE: %s\n", spe_data->program_name);
448 if (!(spe_data->booted))
449 SPE_Boot(this, spe_data);
451 /* To allow re-running of context, spe_ctx_entry has to be set before each call */
452 spe_data->entry = SPE_DEFAULT_ENTRY;
453 spe_data->error_code = 0;
455 /* Create SPE thread and run */
456 deprintf(2, "[PS3->SPU] Create Thread: %s\n", spe_data->program_name);
458 (&spe_data->thread, NULL, (void *)&SPE_RunContext, (void *)spe_data)) {
459 deprintf(2, "[PS3->SPU] Could not create pthread for spe: %s\n", spe_data->program_name);
460 SDL_SetError("[PS3->SPU] Could not create pthread for spe");
464 if (spe_data->keepalive)
465 SPE_WaitForMsg(this, spe_data, SPU_READY);
469 /* Stop the SPE thread */
470 int SPE_Stop(_THIS, spu_data_t * spe_data)
472 deprintf(2, "[PS3->SPU] Stop SPE: %s\n", spe_data->program_name);
473 /* Wait for SPE thread to complete */
474 deprintf(2, "[PS3->SPU] Wait for SPE thread to complete: %s\n", spe_data->program_name);
475 if (pthread_join(spe_data->thread, NULL)) {
476 deprintf(2, "[PS3->SPU] Failed joining the thread: %s\n", spe_data->program_name);
477 SDL_SetError("[PS3->SPU] Failed joining the thread");
485 /* Create SPE context and load program */
486 int SPE_Boot(_THIS, spu_data_t * spe_data)
488 /* Create SPE context */
489 deprintf(2, "[PS3->SPU] Create SPE Context: %s\n", spe_data->program_name);
490 spe_data->ctx = spe_context_create(0, NULL);
491 if (spe_data->ctx == NULL) {
492 deprintf(2, "[PS3->SPU] Failed creating SPE context: %s\n", spe_data->program_name);
493 SDL_SetError("[PS3->SPU] Failed creating SPE context");
497 /* Load SPE object into SPE local store */
498 deprintf(2, "[PS3->SPU] Load Program into SPE: %s\n", spe_data->program_name);
499 if (spe_program_load(spe_data->ctx, &spe_data->program)) {
500 deprintf(2, "[PS3->SPU] Failed loading program into SPE context: %s\n", spe_data->program_name);
502 ("[PS3->SPU] Failed loading program into SPE context");
505 spe_data->booted = 1;
506 deprintf(2, "[PS3->SPU] SPE boot successful\n");
511 /* (Stop and) shutdown the SPE */
512 int SPE_Shutdown(_THIS, spu_data_t * spe_data)
514 if (spe_data->keepalive && spe_data->booted) {
515 SPE_SendMsg(this, spe_data, SPU_EXIT);
516 SPE_Stop(this, spe_data);
519 /* Destroy SPE context */
520 deprintf(2, "[PS3->SPU] Destroy SPE context: %s\n", spe_data->program_name);
521 if (spe_context_destroy(spe_data->ctx)) {
522 deprintf(2, "[PS3->SPU] Failed destroying context: %s\n", spe_data->program_name);
523 SDL_SetError("[PS3->SPU] Failed destroying context");
526 deprintf(2, "[PS3->SPU] SPE shutdown successful: %s\n", spe_data->program_name);
531 /* Send message to the SPE via mailboxe */
532 int SPE_SendMsg(_THIS, spu_data_t * spe_data, unsigned int msg)
534 deprintf(2, "[PS3->SPU] Sending message %u to %s\n", msg, spe_data->program_name);
535 /* Send one message, block until message was sent */
536 unsigned int spe_in_mbox_msgs[1];
537 spe_in_mbox_msgs[0] = msg;
538 int in_mbox_write = spe_in_mbox_write(spe_data->ctx, spe_in_mbox_msgs, 1, SPE_MBOX_ALL_BLOCKING);
540 if (1 > in_mbox_write) {
541 deprintf(2, "[PS3->SPU] No message could be written to %s\n", spe_data->program_name);
542 SDL_SetError("[PS3->SPU] No message could be written");
549 /* Read 1 message from SPE, block until at least 1 message was received */
550 int SPE_WaitForMsg(_THIS, spu_data_t * spe_data, unsigned int msg)
552 deprintf(2, "[PS3->SPU] Waiting for message from %s\n", spe_data->program_name);
553 unsigned int out_messages[1];
554 while (!spe_out_mbox_status(spe_data->ctx));
555 int mbox_read = spe_out_mbox_read(spe_data->ctx, out_messages, 1);
556 deprintf(2, "[PS3->SPU] Got message from %s, message was %u\n", spe_data->program_name, out_messages[0]);
557 if (out_messages[0] == msg)
564 /* Re-runnable invocation of the spe_context_run call */
565 void SPE_RunContext(void *thread_argp)
567 /* argp is the pointer to argument to be passed to the SPE program */
568 spu_data_t *args = (spu_data_t *) thread_argp;
569 deprintf(3, "[PS3->SPU] void* argp=0x%x\n", (unsigned int)args->argp);
572 deprintf(2, "[PS3->SPU] Run SPE program: %s\n", args->program_name);
574 (args->ctx, &args->entry, 0, (void *)args->argp, NULL,
576 deprintf(2, "[PS3->SPU] Failed running SPE context: %s\n", args->program_name);
577 SDL_SetError("[PS3->SPU] Failed running SPE context: %s", args->program_name);
585 /* Quits the video driver */
586 static void PS3_VideoQuit(_THIS)
589 /* Restore the original video mode */
590 if (ioctl(fb_dev_fd, FBIOPUT_VSCREENINFO, &fb_orig_vinfo))
591 SDL_SetError("[PS3] Can't restore original fb_var_screeninfo");
593 /* Give control of frame buffer to kernel */
594 ioctl(fb_dev_fd, PS3FB_IOCTL_OFF, 0);
600 munmap(frame_buffer, fb_finfo.smem_len);
605 free((void *)fb_parms);
606 if (fb_thread_data) {
607 SPE_Shutdown(this, fb_thread_data);
608 free((void *)fb_thread_data);
612 if (double_buffering && this->screen->pixels) {
613 free(this->screen->pixels);
615 this->screen->pixels = NULL;
619 deprintf(1, "[PS3] VideoQuit\n");