comparison src/video/ps3/SDL_ps3video.c @ 3257:94fb40a4a9a7

Merged Martin's code changes from Google Summer of Code 2009
author Sam Lantinga <slouken@libsdl.org>
date Mon, 07 Sep 2009 04:51:29 +0000
parents
children 83518f8fcd61
comparison
equal deleted inserted replaced
3256:83c87f2b2aab 3257:94fb40a4a9a7
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
37 #include "SDL_ps3video.h"
38 #include "SDL_ps3spe_c.h"
39 #include "SDL_ps3events_c.h"
40 #include "SDL_ps3render_c.h"
41 #include "SDL_ps3modes_c.h"
42
43 #include <fcntl.h>
44 #include <linux/fb.h>
45 #include <asm/ps3fb.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 void PS3_VideoQuit(_THIS);
53
54 /* Stores the SPE executable name of fb_writer_spu */
55 extern spe_program_handle_t fb_writer_spu;
56
57 /* PS3 driver bootstrap functions */
58
59 static int
60 PS3_Available(void)
61 {
62 deprintf(1, "+PS3_Available()\n");
63 const char *envr = SDL_getenv("SDL_VIDEODRIVER");
64 if ((envr) && (SDL_strcmp(envr, PS3VID_DRIVER_NAME) == 0)) {
65 return (1);
66 }
67
68 deprintf(1, "-PS3_Available()\n");
69 return (0);
70 }
71
72 static void
73 PS3_DeleteDevice(SDL_VideoDevice * device)
74 {
75 deprintf(1, "+PS3_DeleteDevice()\n");
76 SDL_free(device->driverdata);
77 SDL_free(device);
78 deprintf(1, "-PS3_DeleteDevice()\n");
79 }
80
81 static SDL_VideoDevice *
82 PS3_CreateDevice(int devindex)
83 {
84 deprintf(1, "+PS3_CreateDevice()\n");
85 SDL_VideoDevice *device;
86 SDL_VideoData *data;
87
88 /* Initialize all variables that we clean on shutdown */
89 device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
90 if (!device) {
91 SDL_OutOfMemory();
92 if (device) {
93 SDL_free(device);
94 }
95 return (0);
96 }
97 data = (struct SDL_VideoData *) SDL_calloc(1, sizeof(SDL_VideoData));
98 if (!data) {
99 SDL_OutOfMemory();
100 SDL_free(device);
101 return (0);
102 }
103 device->driverdata = data;
104
105 /* Set the function pointers */
106 device->VideoInit = PS3_VideoInit;
107 device->VideoQuit = PS3_VideoQuit;
108 device->SetDisplayMode = PS3_SetDisplayMode;
109 device->GetDisplayModes = PS3_GetDisplayModes;
110 device->PumpEvents = PS3_PumpEvents;
111
112 device->free = PS3_DeleteDevice;
113
114 deprintf(1, "-PS3_CreateDevice()\n");
115 return device;
116 }
117
118 VideoBootStrap PS3_bootstrap = {
119 PS3VID_DRIVER_NAME, "SDL PS3 Cell video driver",
120 PS3_Available, PS3_CreateDevice
121 };
122
123
124 int
125 PS3_VideoInit(_THIS)
126 {
127 deprintf(1, "PS3_VideoInit()\n");
128
129 SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
130 SDL_DisplayMode mode;
131
132 /* Create SPU fb_parms and thread structure */
133 data->fb_parms = (struct fb_writer_parms_t *)
134 memalign(16, sizeof(struct fb_writer_parms_t));
135 data->fb_thread_data = (spu_data_t *) malloc(sizeof(spu_data_t));
136 if (data->fb_parms == NULL || data->fb_thread_data == NULL) {
137 SDL_OutOfMemory();
138 return -1;
139 }
140 data->fb_thread_data->program = fb_writer_spu;
141 data->fb_thread_data->program_name = "fb_writer_spu";
142 data->fb_thread_data->argp = (void *)data->fb_parms;
143 data->fb_thread_data->keepalive = 1;
144 data->fb_thread_data->booted = 0;
145
146 SPE_Start(data->fb_thread_data);
147
148 /* Open the device */
149 data->fbdev = open(PS3DEV, O_RDWR);
150 if (data->fbdev < 0) {
151 SDL_SetError("[PS3] Unable to open device %s", PS3DEV);
152 return -1;
153 }
154
155 /* Take control of frame buffer from kernel, for details see
156 * http://felter.org/wesley/files/ps3/linux-20061110-docs/ApplicationProgrammingEnvironment.html
157 * kernel will no longer flip the screen itself
158 */
159 ioctl(data->fbdev, PS3FB_IOCTL_ON, 0);
160
161 /* Unblank screen */
162 ioctl(data->fbdev, FBIOBLANK, 0);
163
164 struct fb_fix_screeninfo fb_finfo;
165 if (ioctl(data->fbdev, FBIOGET_FSCREENINFO, &fb_finfo)) {
166 SDL_SetError("[PS3] Can't get fixed screeninfo");
167 return (0);
168 }
169
170 /* Note: on PS3, fb_finfo.smem_len is enough for double buffering */
171 if ((data->frame_buffer = (uint8_t *)mmap(0, fb_finfo.smem_len,
172 PROT_READ | PROT_WRITE, MAP_SHARED,
173 data->fbdev, 0)) == (uint8_t *) - 1) {
174 SDL_SetError("[PS3] Can't mmap for %s", PS3DEV);
175 return (0);
176 } else {
177 /* Enable double buffering */
178 }
179
180 /* Blank screen */
181 memset(data->frame_buffer, 0x00, fb_finfo.smem_len);
182
183 PS3_InitModes(_this);
184 SDL_AddRenderDriver(0, &SDL_PS3_RenderDriver);
185
186 /* We're done! */
187 return 0;
188 }
189
190 void
191 PS3_VideoQuit(_THIS)
192 {
193 deprintf(1, "PS3_VideoQuit()\n");
194 SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
195
196 PS3_QuitModes(_this);
197
198 /* Unmap framebuffer */
199 if (data->frame_buffer) {
200 struct fb_fix_screeninfo fb_finfo;
201 if (ioctl(data->fbdev, FBIOGET_FSCREENINFO, &fb_finfo) != -1) {
202 munmap(data->frame_buffer, fb_finfo.smem_len);
203 data->frame_buffer = 0;
204 }
205 }
206
207 /* Shutdown SPE and related resources */
208 if (data->fb_parms)
209 free((void *)data->fb_parms);
210 if (data->fb_thread_data) {
211 SPE_Shutdown(data->fb_thread_data);
212 free((void *)data->fb_thread_data);
213 }
214
215 /* Close device */
216 if (data->fbdev) {
217 /* Give control of frame buffer back to kernel */
218 ioctl(data->fbdev, PS3FB_IOCTL_OFF, 0);
219 close(data->fbdev);
220 data->fbdev = -1;
221 }
222 }
223
224 /* vi: set ts=4 sw=4 expandtab: */