comparison src/video/ps3/SDL_ps3video.c @ 3144:0d8d1f870964 gsoc2009_ps3

Moved SPE-functions to SDL_ps3spe.c. Added ActivateRenderer() and PS3_QueryTexturePixels(). Added yuv2rgb_spu but not yet in use.
author Martin Lowinski <martin@goldtopf.org>
date Wed, 10 Jun 2009 09:15:33 +0000
parents c146645a770e
children 7828eed2f31a
comparison
equal deleted inserted replaced
3143:8fdabaa064c3 3144:0d8d1f870964
31 #include "SDL_video.h" 31 #include "SDL_video.h"
32 #include "SDL_mouse.h" 32 #include "SDL_mouse.h"
33 #include "../SDL_sysvideo.h" 33 #include "../SDL_sysvideo.h"
34 #include "../SDL_pixels_c.h" 34 #include "../SDL_pixels_c.h"
35 #include "../../events/SDL_events_c.h" 35 #include "../../events/SDL_events_c.h"
36 #include "spulibs/spu_common.h"
37 36
38 #include "SDL_ps3video.h" 37 #include "SDL_ps3video.h"
38 #include "SDL_ps3spe_c.h"
39 #include "SDL_ps3events_c.h" 39 #include "SDL_ps3events_c.h"
40 #include "SDL_ps3render_c.h" 40 #include "SDL_ps3render_c.h"
41 41
42 #include <fcntl.h> 42 #include <fcntl.h>
43 #include <linux/fb.h> 43 #include <linux/fb.h>
44 #include <asm/ps3fb.h> 44 #include <asm/ps3fb.h>
45 #include <libspe2.h>
46 #include <sys/mman.h> 45 #include <sys/mman.h>
47 46
48 #define PS3VID_DRIVER_NAME "ps3" 47 #define PS3VID_DRIVER_NAME "ps3"
49 48
50 /* Initialization/Query functions */ 49 /* Initialization/Query functions */
51 static int PS3_VideoInit(_THIS); 50 static int PS3_VideoInit(_THIS);
52 static int PS3_SetDisplayMode(_THIS, SDL_DisplayMode * mode); 51 static int PS3_SetDisplayMode(_THIS, SDL_DisplayMode * mode);
53 static void PS3_VideoQuit(_THIS); 52 static void PS3_VideoQuit(_THIS);
54
55 /* SPU specific functions */
56 int SPE_Start(_THIS, spu_data_t * spe_data);
57 int SPE_Stop(_THIS, spu_data_t * spe_data);
58 int SPE_Boot(_THIS, spu_data_t * spe_data);
59 int SPE_Shutdown(_THIS, spu_data_t * spe_data);
60 int SPE_SendMsg(spu_data_t * spe_data, unsigned int msg);
61 int SPE_WaitForMsg(spu_data_t * spe_data, unsigned int msg);
62 void SPE_RunContext(void *thread_argp);
63 53
64 /* Stores the SPE executable name of fb_writer_spu */ 54 /* Stores the SPE executable name of fb_writer_spu */
65 extern spe_program_handle_t fb_writer_spu; 55 extern spe_program_handle_t fb_writer_spu;
66 56
67 /* PS3 driver bootstrap functions */ 57 /* PS3 driver bootstrap functions */
165 data->fb_thread_data->program_name = "fb_writer_spu"; 155 data->fb_thread_data->program_name = "fb_writer_spu";
166 data->fb_thread_data->argp = (void *)data->fb_parms; 156 data->fb_thread_data->argp = (void *)data->fb_parms;
167 data->fb_thread_data->keepalive = 1; 157 data->fb_thread_data->keepalive = 1;
168 data->fb_thread_data->booted = 0; 158 data->fb_thread_data->booted = 0;
169 159
170 SPE_Start(_this, data->fb_thread_data); 160 SPE_Start(data->fb_thread_data);
171 161
172 /* Open the device */ 162 /* Open the device */
173 data->fbdev = open(PS3DEV, O_RDWR); 163 data->fbdev = open(PS3DEV, O_RDWR);
174 if (data->fbdev < 0) { 164 if (data->fbdev < 0) {
175 SDL_SetError("[PS3] Unable to open device %s", PS3DEV); 165 SDL_SetError("[PS3] Unable to open device %s", PS3DEV);
196 PROT_READ | PROT_WRITE, MAP_SHARED, 186 PROT_READ | PROT_WRITE, MAP_SHARED,
197 data->fbdev, 0)) == (uint8_t *) - 1) { 187 data->fbdev, 0)) == (uint8_t *) - 1) {
198 SDL_SetError("[PS3] Can't mmap for %s", PS3DEV); 188 SDL_SetError("[PS3] Can't mmap for %s", PS3DEV);
199 return (0); 189 return (0);
200 } else { 190 } else {
201 //current->flags |= SDL_DOUBLEBUF; 191 /* Enable double buffering */
202 } 192 }
203 193
204 /* Blank screen */ 194 /* Blank screen */
205 memset(data->frame_buffer, 0x00, fb_finfo.smem_len); 195 memset(data->frame_buffer, 0x00, fb_finfo.smem_len);
206 196
218 void 208 void
219 PS3_VideoQuit(_THIS) 209 PS3_VideoQuit(_THIS)
220 { 210 {
221 deprintf(1, "PS3_VideoQuit()\n"); 211 deprintf(1, "PS3_VideoQuit()\n");
222 SDL_VideoData *data = (SDL_VideoData *) _this->driverdata; 212 SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
213
214 /* Unmap framebuffer */
223 if (data->frame_buffer) { 215 if (data->frame_buffer) {
224 struct fb_fix_screeninfo fb_finfo; 216 struct fb_fix_screeninfo fb_finfo;
225 if (ioctl(data->fbdev, FBIOGET_FSCREENINFO, &fb_finfo) != -1) { 217 if (ioctl(data->fbdev, FBIOGET_FSCREENINFO, &fb_finfo) != -1) {
226 munmap(data->frame_buffer, fb_finfo.smem_len); 218 munmap(data->frame_buffer, fb_finfo.smem_len);
227 data->frame_buffer = 0; 219 data->frame_buffer = 0;
228 } 220 }
229 } 221 }
230 222
223 /* Shutdown SPE and related resources */
231 if (data->fb_parms) 224 if (data->fb_parms)
232 free((void *)data->fb_parms); 225 free((void *)data->fb_parms);
233 if (data->fb_thread_data) { 226 if (data->fb_thread_data) {
234 SPE_Shutdown(_this, data->fb_thread_data); 227 SPE_Shutdown(data->fb_thread_data);
235 free((void *)data->fb_thread_data); 228 free((void *)data->fb_thread_data);
236 } 229 }
237 } 230
238 231 /* Close device */
239 232 if (data->fbdev > 0) {
240 /* 233 /* Give control of frame buffer back to kernel */
241 * SPE handling 234 ioctl(data->fbdev, PS3FB_IOCTL_OFF, 0);
242 */ 235 close(data->fbdev);
243 236 data->fbdev = -1;
244 /* Start the SPE thread */ 237 }
245 int SPE_Start(_THIS, spu_data_t * spe_data)
246 {
247 deprintf(2, "[PS3->SPU] Start SPE: %s\n", spe_data->program_name);
248 if (!(spe_data->booted))
249 SPE_Boot(_this, spe_data);
250
251 /* To allow re-running of context, spe_ctx_entry has to be set before each call */
252 spe_data->entry = SPE_DEFAULT_ENTRY;
253 spe_data->error_code = 0;
254
255 /* Create SPE thread and run */
256 deprintf(2, "[PS3->SPU] Create Thread: %s\n", spe_data->program_name);
257 if (pthread_create
258 (&spe_data->thread, NULL, (void *)&SPE_RunContext, (void *)spe_data)) {
259 deprintf(2, "[PS3->SPU] Could not create pthread for spe: %s\n", spe_data->program_name);
260 SDL_SetError("[PS3->SPU] Could not create pthread for spe");
261 return -1;
262 }
263
264 if (spe_data->keepalive)
265 SPE_WaitForMsg(spe_data, SPU_READY);
266 }
267
268
269 /* Stop the SPE thread */
270 int SPE_Stop(_THIS, spu_data_t * spe_data)
271 {
272 deprintf(2, "[PS3->SPU] Stop SPE: %s\n", spe_data->program_name);
273 /* Wait for SPE thread to complete */
274 deprintf(2, "[PS3->SPU] Wait for SPE thread to complete: %s\n", spe_data->program_name);
275 if (pthread_join(spe_data->thread, NULL)) {
276 deprintf(2, "[PS3->SPU] Failed joining the thread: %s\n", spe_data->program_name);
277 SDL_SetError("[PS3->SPU] Failed joining the thread");
278 return -1;
279 }
280
281 return 0;
282 }
283
284 /* Create SPE context and load program */
285 int SPE_Boot(_THIS, spu_data_t * spe_data)
286 {
287 /* Create SPE context */
288 deprintf(2, "[PS3->SPU] Create SPE Context: %s\n", spe_data->program_name);
289 spe_data->ctx = spe_context_create(0, NULL);
290 if (spe_data->ctx == NULL) {
291 deprintf(2, "[PS3->SPU] Failed creating SPE context: %s\n", spe_data->program_name);
292 SDL_SetError("[PS3->SPU] Failed creating SPE context");
293 return -1;
294 }
295
296 /* Load SPE object into SPE local store */
297 deprintf(2, "[PS3->SPU] Load Program into SPE: %s\n", spe_data->program_name);
298 if (spe_program_load(spe_data->ctx, &spe_data->program)) {
299 deprintf(2, "[PS3->SPU] Failed loading program into SPE context: %s\n", spe_data->program_name);
300 SDL_SetError
301 ("[PS3->SPU] Failed loading program into SPE context");
302 return -1;
303 }
304 spe_data->booted = 1;
305 deprintf(2, "[PS3->SPU] SPE boot successful\n");
306
307 return 0;
308 }
309
310 /* (Stop and) shutdown the SPE */
311 int SPE_Shutdown(_THIS, spu_data_t * spe_data)
312 {
313 if (spe_data->keepalive && spe_data->booted) {
314 SPE_SendMsg(spe_data, SPU_EXIT);
315 SPE_Stop(_this, spe_data);
316 }
317
318 /* Destroy SPE context */
319 deprintf(2, "[PS3->SPU] Destroy SPE context: %s\n", spe_data->program_name);
320 if (spe_context_destroy(spe_data->ctx)) {
321 deprintf(2, "[PS3->SPU] Failed destroying context: %s\n", spe_data->program_name);
322 SDL_SetError("[PS3->SPU] Failed destroying context");
323 return -1;
324 }
325 deprintf(2, "[PS3->SPU] SPE shutdown successful: %s\n", spe_data->program_name);
326 return 0;
327 }
328
329 /* Send message to the SPE via mailboxe */
330 int SPE_SendMsg(spu_data_t * spe_data, unsigned int msg)
331 {
332 deprintf(2, "[PS3->SPU] Sending message %u to %s\n", msg, spe_data->program_name);
333 /* Send one message, block until message was sent */
334 unsigned int spe_in_mbox_msgs[1];
335 spe_in_mbox_msgs[0] = msg;
336 int in_mbox_write = spe_in_mbox_write(spe_data->ctx, spe_in_mbox_msgs, 1, SPE_MBOX_ALL_BLOCKING);
337
338 if (1 > in_mbox_write) {
339 deprintf(2, "[PS3->SPU] No message could be written to %s\n", spe_data->program_name);
340 SDL_SetError("[PS3->SPU] No message could be written");
341 return -1;
342 }
343 return 0;
344 }
345
346
347 /* Read 1 message from SPE, block until at least 1 message was received */
348 int SPE_WaitForMsg(spu_data_t * spe_data, unsigned int msg)
349 {
350 deprintf(2, "[PS3->SPU] Waiting for message from %s\n", spe_data->program_name);
351 unsigned int out_messages[1];
352 while (!spe_out_mbox_status(spe_data->ctx));
353 int mbox_read = spe_out_mbox_read(spe_data->ctx, out_messages, 1);
354 deprintf(2, "[PS3->SPU] Got message from %s, message was %u\n", spe_data->program_name, out_messages[0]);
355 if (out_messages[0] == msg)
356 return 0;
357 else
358 return -1;
359 }
360
361 /* Re-runnable invocation of the spe_context_run call */
362 void SPE_RunContext(void *thread_argp)
363 {
364 /* argp is the pointer to argument to be passed to the SPE program */
365 spu_data_t *args = (spu_data_t *) thread_argp;
366 deprintf(3, "[PS3->SPU] void* argp=0x%x\n", (unsigned int)args->argp);
367
368 /* Run it.. */
369 deprintf(2, "[PS3->SPU] Run SPE program: %s\n", args->program_name);
370 if (spe_context_run
371 (args->ctx, &args->entry, 0, (void *)args->argp, NULL,
372 NULL) < 0) {
373 deprintf(2, "[PS3->SPU] Failed running SPE context: %s\n", args->program_name);
374 SDL_SetError("[PS3->SPU] Failed running SPE context: %s", args->program_name);
375 exit(1);
376 }
377
378 pthread_exit(NULL);
379 } 238 }
380 239
381 /* vi: set ts=4 sw=4 expandtab: */ 240 /* vi: set ts=4 sw=4 expandtab: */