Mercurial > sdl-ios-xcode
comparison src/video/ps3/SDL_ps3video.c @ 3141:3df74541339b gsoc2009_ps3
Added ps3 video driver based on the dummy driver.
Added spulib for copying to framebuffer.
Added SPU managing functions.
Added open/close and taking control of the framebuffer.
author | Martin Lowinski <martin@goldtopf.org> |
---|---|
date | Fri, 29 May 2009 09:50:21 +0000 |
parents | |
children | c146645a770e |
comparison
equal
deleted
inserted
replaced
3140:9ef99b844c60 | 3141:3df74541339b |
---|---|
1 /* | |
2 SDL - Simple DirectMedia Layer | |
3 Copyright (C) 1997-2009 Sam Lantinga | |
4 | |
5 This library is free software; you can redistribute it and/or | |
6 modify it under the terms of the GNU Lesser General Public | |
7 License as published by the Free Software Foundation; either | |
8 version 2.1 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 Lesser General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU Lesser General Public | |
16 License along with this library; if not, write to the Free Software | |
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
18 | |
19 Sam Lantinga | |
20 slouken@libsdl.org | |
21 */ | |
22 #include "SDL_config.h" | |
23 | |
24 /* SDL PS3 video driver implementation based on dummy video driver | |
25 * | |
26 * Initial work by Ryan C. Gordon (icculus@icculus.org). A good portion | |
27 * of this was cut-and-pasted from Stephane Peter's work in the AAlib | |
28 * SDL video driver. Renamed to "DUMMY" by Sam Lantinga. | |
29 */ | |
30 | |
31 #include "SDL_video.h" | |
32 #include "SDL_mouse.h" | |
33 #include "../SDL_sysvideo.h" | |
34 #include "../SDL_pixels_c.h" | |
35 #include "../../events/SDL_events_c.h" | |
36 #include "spulibs/spu_common.h" | |
37 | |
38 #include "SDL_ps3video.h" | |
39 #include "SDL_ps3events_c.h" | |
40 #include "SDL_ps3render_c.h" | |
41 | |
42 #include <fcntl.h> | |
43 #include <linux/fb.h> | |
44 #include <asm/ps3fb.h> | |
45 #include <libspe2.h> | |
46 #include <sys/mman.h> | |
47 | |
48 #define PS3VID_DRIVER_NAME "ps3" | |
49 | |
50 /* Initialization/Query functions */ | |
51 static int PS3_VideoInit(_THIS); | |
52 static int PS3_SetDisplayMode(_THIS, SDL_DisplayMode * mode); | |
53 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(_THIS, spu_data_t * spe_data, unsigned int msg); | |
61 int SPE_WaitForMsg(_THIS, spu_data_t * spe_data, unsigned int msg); | |
62 void SPE_RunContext(void *thread_argp); | |
63 | |
64 /* Stores the SPE executable name of fb_writer_spu */ | |
65 extern spe_program_handle_t fb_writer_spu; | |
66 | |
67 /* PS3 driver bootstrap functions */ | |
68 | |
69 static int | |
70 PS3_Available(void) | |
71 { | |
72 deprintf(1, "PS3_Available()\n"); | |
73 const char *envr = SDL_getenv("SDL_VIDEODRIVER"); | |
74 if ((envr) && (SDL_strcmp(envr, PS3VID_DRIVER_NAME) == 0)) { | |
75 return (1); | |
76 } | |
77 | |
78 return (0); | |
79 } | |
80 | |
81 static void | |
82 PS3_DeleteDevice(SDL_VideoDevice * device) | |
83 { | |
84 deprintf(1, "PS3_DeleteDevice()\n"); | |
85 SDL_free(device->driverdata); | |
86 SDL_free(device); | |
87 } | |
88 | |
89 static SDL_VideoDevice * | |
90 PS3_CreateDevice(int devindex) | |
91 { | |
92 deprintf(1, "PS3_CreateDevice()\n"); | |
93 SDL_VideoDevice *device; | |
94 SDL_VideoData *data; | |
95 | |
96 /* Initialize all variables that we clean on shutdown */ | |
97 device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice)); | |
98 if (!device) { | |
99 SDL_OutOfMemory(); | |
100 if (device) { | |
101 SDL_free(device); | |
102 } | |
103 return (0); | |
104 } | |
105 data = (struct SDL_VideoData *) SDL_calloc(1, sizeof(SDL_VideoData)); | |
106 if (!data) { | |
107 SDL_OutOfMemory(); | |
108 SDL_free(device); | |
109 return (0); | |
110 } | |
111 device->driverdata = data; | |
112 | |
113 /* Set the function pointers */ | |
114 device->VideoInit = PS3_VideoInit; | |
115 device->VideoQuit = PS3_VideoQuit; | |
116 device->SetDisplayMode = PS3_SetDisplayMode; | |
117 device->PumpEvents = PS3_PumpEvents; | |
118 | |
119 device->free = PS3_DeleteDevice; | |
120 | |
121 return device; | |
122 } | |
123 | |
124 VideoBootStrap PS3_bootstrap = { | |
125 PS3VID_DRIVER_NAME, "SDL PS3 Cell video driver", | |
126 PS3_Available, PS3_CreateDevice | |
127 }; | |
128 | |
129 | |
130 int | |
131 PS3_VideoInit(_THIS) | |
132 { | |
133 deprintf(1, "PS3_VideoInit()\n"); | |
134 | |
135 SDL_VideoData *data = (SDL_VideoData *) _this->driverdata; | |
136 SDL_DisplayMode mode; | |
137 | |
138 /* Use a fake 32-bpp desktop mode */ | |
139 mode.format = SDL_PIXELFORMAT_RGB888; | |
140 //mode.w = 1024; | |
141 //mode.h = 768; | |
142 mode.w = 1920; | |
143 mode.h = 1080; | |
144 mode.refresh_rate = 0; | |
145 mode.driverdata = NULL; | |
146 SDL_AddBasicVideoDisplay(&mode); | |
147 SDL_AddRenderDriver(0, &SDL_PS3_RenderDriver); | |
148 | |
149 SDL_zero(mode); | |
150 SDL_AddDisplayMode(0, &mode); | |
151 | |
152 /* | |
153 *PS3 stuff | |
154 */ | |
155 | |
156 /* Create SPU fb_parms and thread structure */ | |
157 data->fb_parms = (struct fb_writer_parms_t *) | |
158 memalign(16, sizeof(struct fb_writer_parms_t)); | |
159 data->fb_thread_data = (spu_data_t *) malloc(sizeof(spu_data_t)); | |
160 if (data->fb_parms == NULL || data->fb_thread_data == NULL) { | |
161 SDL_OutOfMemory(); | |
162 return -1; | |
163 } | |
164 data->fb_thread_data->program = fb_writer_spu; | |
165 data->fb_thread_data->program_name = "fb_writer_spu"; | |
166 data->fb_thread_data->argp = (void *)data->fb_parms; | |
167 data->fb_thread_data->keepalive = 1; | |
168 data->fb_thread_data->booted = 0; | |
169 | |
170 SPE_Start(_this, data->fb_thread_data); | |
171 | |
172 /* Open the device */ | |
173 data->fbdev = open(PS3DEV, O_RDWR); | |
174 if (data->fbdev < 0) { | |
175 SDL_SetError("[PS3] Unable to open device %s", PS3DEV); | |
176 return -1; | |
177 } | |
178 | |
179 /* Take control of frame buffer from kernel, for details see | |
180 * http://felter.org/wesley/files/ps3/linux-20061110-docs/ApplicationProgrammingEnvironment.html | |
181 * kernel will no longer flip the screen itself | |
182 */ | |
183 ioctl(data->fbdev, PS3FB_IOCTL_ON, 0); | |
184 | |
185 /* Unblank screen */ | |
186 ioctl(data->fbdev, FBIOBLANK, 0); | |
187 | |
188 struct fb_fix_screeninfo fb_finfo; | |
189 if (ioctl(data->fbdev, FBIOGET_FSCREENINFO, &fb_finfo)) { | |
190 SDL_SetError("[PS3] Can't get fixed screeninfo"); | |
191 return (0); | |
192 } | |
193 | |
194 /* Note: on PS3, fb_finfo.smem_len is enough for double buffering */ | |
195 if ((data->frame_buffer = (uint8_t *)mmap(0, fb_finfo.smem_len, | |
196 PROT_READ | PROT_WRITE, MAP_SHARED, | |
197 data->fbdev, 0)) == (uint8_t *) - 1) { | |
198 SDL_SetError("[PS3] Can't mmap for %s", PS3DEV); | |
199 return (0); | |
200 } else { | |
201 //current->flags |= SDL_DOUBLEBUF; | |
202 } | |
203 | |
204 /* Blank screen */ | |
205 memset(data->frame_buffer, 0x00, fb_finfo.smem_len); | |
206 | |
207 /* We're done! */ | |
208 return 0; | |
209 } | |
210 | |
211 static int | |
212 PS3_SetDisplayMode(_THIS, SDL_DisplayMode * mode) | |
213 { | |
214 deprintf(1, "PS3_SetDisplayMode()\n"); | |
215 return 0; | |
216 } | |
217 | |
218 void | |
219 PS3_VideoQuit(_THIS) | |
220 { | |
221 deprintf(1, "PS3_VideoQuit()\n"); | |
222 SDL_VideoData *data = (SDL_VideoData *) _this->driverdata; | |
223 if (data->frame_buffer) { | |
224 struct fb_fix_screeninfo fb_finfo; | |
225 if (ioctl(data->fbdev, FBIOGET_FSCREENINFO, &fb_finfo) != -1) { | |
226 munmap(data->frame_buffer, fb_finfo.smem_len); | |
227 data->frame_buffer = 0; | |
228 } | |
229 } | |
230 | |
231 if (data->fb_parms) | |
232 free((void *)data->fb_parms); | |
233 if (data->fb_thread_data) { | |
234 SPE_Shutdown(_this, data->fb_thread_data); | |
235 free((void *)data->fb_thread_data); | |
236 } | |
237 } | |
238 | |
239 | |
240 /* | |
241 * SPE handling | |
242 */ | |
243 | |
244 /* Start the SPE thread */ | |
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(_this, 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(_this, 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(_THIS, 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(_THIS, 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 } | |
380 | |
381 /* vi: set ts=4 sw=4 expandtab: */ |