2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
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.
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.
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
22 #include "SDL_config.h"
24 /* Framebuffer console based SDL video driver implementation.
29 #include <sys/ioctl.h>
32 #include "SDL_video.h"
33 #include "SDL_mouse.h"
34 #include "../SDL_sysvideo.h"
35 #include "../SDL_pixels_c.h"
36 #include "../../events/SDL_events_c.h"
37 #include "../SDL_cursor_c.h"
38 #include "SDL_gsvideo.h"
39 #include "SDL_gsmouse_c.h"
40 #include "SDL_gsevents_c.h"
41 #include "SDL_gsyuv_c.h"
44 /* Initialization/Query functions */
45 static int GS_VideoInit(_THIS, SDL_PixelFormat *vformat);
46 static SDL_Rect **GS_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
47 static SDL_Surface *GS_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
48 static int GS_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors);
49 static void GS_VideoQuit(_THIS);
51 /* Hardware surface functions */
52 static int GS_AllocHWSurface(_THIS, SDL_Surface *surface);
53 static int GS_LockHWSurface(_THIS, SDL_Surface *surface);
54 static void GS_UnlockHWSurface(_THIS, SDL_Surface *surface);
55 static void GS_FreeHWSurface(_THIS, SDL_Surface *surface);
57 /* GS driver bootstrap functions */
59 static int GS_Available(void)
63 console = open(PS2_DEV_GS, O_RDWR, 0);
67 memory = open(PS2_DEV_MEM, O_RDWR, 0);
71 return((console >= 0) && (memory >= 0));
74 static void GS_DeleteDevice(SDL_VideoDevice *device)
76 SDL_free(device->hidden);
80 static SDL_VideoDevice *GS_CreateDevice(int devindex)
82 SDL_VideoDevice *this;
84 /* Initialize all variables that we clean on shutdown */
85 this = (SDL_VideoDevice *)SDL_malloc(sizeof(SDL_VideoDevice));
87 SDL_memset(this, 0, (sizeof *this));
88 this->hidden = (struct SDL_PrivateVideoData *)
89 SDL_malloc((sizeof *this->hidden));
91 if ( (this == NULL) || (this->hidden == NULL) ) {
98 SDL_memset(this->hidden, 0, (sizeof *this->hidden));
102 /* Set the function pointers */
103 this->VideoInit = GS_VideoInit;
104 this->ListModes = GS_ListModes;
105 this->SetVideoMode = GS_SetVideoMode;
106 this->CreateYUVOverlay = GS_CreateYUVOverlay;
107 this->SetColors = GS_SetColors;
108 this->UpdateRects = NULL;
109 this->VideoQuit = GS_VideoQuit;
110 this->AllocHWSurface = GS_AllocHWSurface;
111 this->CheckHWBlit = NULL;
112 this->FillHWRect = NULL;
113 this->SetHWColorKey = NULL;
114 this->SetHWAlpha = NULL;
115 this->LockHWSurface = GS_LockHWSurface;
116 this->UnlockHWSurface = GS_UnlockHWSurface;
117 this->FlipHWSurface = NULL;
118 this->FreeHWSurface = GS_FreeHWSurface;
119 this->SetIcon = NULL;
120 this->SetCaption = NULL;
121 this->GetWMInfo = NULL;
122 this->FreeWMCursor = GS_FreeWMCursor;
123 this->CreateWMCursor = GS_CreateWMCursor;
124 this->ShowWMCursor = GS_ShowWMCursor;
125 this->MoveWMCursor = GS_MoveWMCursor;
126 this->InitOSKeymap = GS_InitOSKeymap;
127 this->PumpEvents = GS_PumpEvents;
129 this->free = GS_DeleteDevice;
134 VideoBootStrap PS2GS_bootstrap = {
135 "ps2gs", "PlayStation 2 Graphics Synthesizer",
136 GS_Available, GS_CreateDevice
139 /* These are the pixel formats for the 32, 24, and 16 bit video modes */
145 } GS_pixelmasks[] = {
146 { 32, 0x000000FF, /* RGB little-endian */
149 { 24, 0x000000FF, /* RGB little-endian */
152 { 16, 0x0000001f, /* RGB little-endian */
156 /* This is a mapping from SDL bytes-per-pixel to GS pixel format */
157 static int GS_formatmap[] = {
158 -1, /* 0 bpp, not a legal value */
159 -1, /* 8 bpp, not supported (yet?) */
160 PS2_GS_PSMCT16, /* 16 bpp */
161 PS2_GS_PSMCT24, /* 24 bpp */
162 PS2_GS_PSMCT32 /* 32 bpp */
165 static unsigned long long head_tags[] __attribute__((aligned(16))) = {
166 4 | (1LL << 60), /* GIFtag */
178 #define MAXIMG (32767 * 16)
181 static inline int loadimage_nonblock(int fd, struct ps2_image *image, int size,
182 unsigned long long *hm,
183 unsigned long long *im)
185 struct ps2_plist plist;
186 struct ps2_packet packet[1 + MAXTAGS * 2];
191 /* initialize the variables */
192 data = (char *)image->ptr;
194 plist.packet = packet;
196 /* make BITBLT packet */
197 packet[pnum].ptr = hm;
198 packet[pnum].len = sizeof(head_tags);
200 hm[2] = ((unsigned long long)image->fbp << 32) |
201 ((unsigned long long)image->fbw << 48) |
202 ((unsigned long long)image->psm << 56);
203 hm[4] = ((unsigned long long)image->x << 32) |
204 ((unsigned long long)image->y << 48);
205 hm[6] = (unsigned long long)image->w |
206 ((unsigned long long)image->h << 32);
208 /* make image mode tags */
210 isize = size > MAXIMG ? MAXIMG : size;
214 packet[pnum].ptr = &im[it];
215 packet[pnum].len = sizeof(unsigned long long) * 2;
217 im[it++] = (isize >> 4) | (eop ? (1 << 15) : 0) | (2LL << 58);
220 packet[pnum].ptr = (void *)data;
221 packet[pnum].len = isize;
227 return ioctl(fd, PS2IOC_SENDL, &plist);
230 static unsigned long long tex_tags[] __attribute__((aligned(16))) = {
231 3 | (1LL << 60), /* GIFtag */
240 static unsigned long long scale_tags[] __attribute__((aligned(16))) = {
241 5 | (1LL << 60), /* GIFtag */
243 6 + (1 << 4) + (1 << 8),
245 ((unsigned long long)0 * 16) + (((unsigned long long)0 * 16) << 16),
247 ((unsigned long long)0 * 16) + (((unsigned long long)0 * 16) << 16),
256 int scaleimage_nonblock(int fd, unsigned long long *tm, unsigned long long *sm)
258 struct ps2_plist plist;
259 struct ps2_packet packet[2];
261 /* initialize the variables */
263 plist.packet = packet;
266 packet[0].len = sizeof(tex_tags);
268 packet[1].len = sizeof(scale_tags);
270 return ioctl(fd, PS2IOC_SENDL, &plist);
273 static int power_of_2(int value)
277 for ( shift = 0; (1<<shift) < value; ++shift ) {
283 static int GS_VideoInit(_THIS, SDL_PixelFormat *vformat)
285 struct ps2_screeninfo vinfo;
287 /* Initialize the library */
288 console_fd = open(PS2_DEV_GS, O_RDWR, 0);
289 if ( console_fd < 0 ) {
290 SDL_SetError("Unable to open %s", PS2_DEV_GS);
293 memory_fd = open(PS2_DEV_MEM, O_RDWR, 0);
294 if ( memory_fd < 0 ) {
297 SDL_SetError("Unable to open %s", PS2_DEV_MEM);
301 if ( ioctl(console_fd, PS2IOC_GSCREENINFO, &vinfo) < 0 ) {
305 SDL_SetError("Couldn't get console pixel format");
309 /* Determine the current screen size */
310 this->info.current_w = vinfo.w;
311 this->info.current_h = vinfo.h;
313 /* Determine the current screen depth */
315 /* Supported pixel formats */
322 SDL_SetError("Unknown console pixel format: %d", vinfo.psm);
325 vformat->BitsPerPixel = GS_pixelmasks[vinfo.psm].bpp;
326 vformat->Rmask = GS_pixelmasks[vinfo.psm].r;
327 vformat->Gmask = GS_pixelmasks[vinfo.psm].g;
328 vformat->Bmask = GS_pixelmasks[vinfo.psm].b;
331 /* Enable mouse and keyboard support */
332 if ( GS_OpenKeyboard(this) < 0 ) {
334 SDL_SetError("Unable to open keyboard");
337 if ( GS_OpenMouse(this) < 0 ) {
338 const char *sdl_nomouse;
340 sdl_nomouse = SDL_getenv("SDL_NOMOUSE");
341 if ( ! sdl_nomouse ) {
343 SDL_SetError("Unable to open mouse");
352 static SDL_Rect **GS_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
354 static SDL_Rect GS_vesa_mode_list[] = {
355 { 0, 0, 1280, 1024 },
360 static SDL_Rect *GS_vesa_modes[] = {
361 &GS_vesa_mode_list[0],
362 &GS_vesa_mode_list[1],
363 &GS_vesa_mode_list[2],
364 &GS_vesa_mode_list[3],
367 static SDL_Rect GS_tvout_stretch;
368 static SDL_Rect GS_tvout_mode;
369 static SDL_Rect *GS_tvout_modes[3];
370 SDL_Rect **modes = NULL;
372 switch (format->BitsPerPixel) {
376 if ( saved_vinfo.mode == PS2_GS_VESA ) {
377 modes = GS_vesa_modes;
381 // FIXME - what's wrong with the stretch code at 16 bpp?
382 if ( format->BitsPerPixel != 32 ) break;
383 /* Add a mode that we could possibly stretch to */
384 for ( i=0; GS_vesa_modes[i]; ++i ) {
385 if ( (GS_vesa_modes[i]->w == saved_vinfo.w) &&
386 (GS_vesa_modes[i]->h != saved_vinfo.h) ) {
387 GS_tvout_stretch.w=GS_vesa_modes[i]->w;
388 GS_tvout_stretch.h=GS_vesa_modes[i]->h;
389 GS_tvout_modes[j++] = &GS_tvout_stretch;
393 /* Add the current TV video mode */
394 GS_tvout_mode.w = saved_vinfo.w;
395 GS_tvout_mode.h = saved_vinfo.h;
396 GS_tvout_modes[j++] = &GS_tvout_mode;
397 GS_tvout_modes[j++] = NULL;
399 /* Return the created list of modes */
400 modes = GS_tvout_modes;
409 /* Various screen update functions available */
410 static void GS_DMAFullUpdate(_THIS, int numrects, SDL_Rect *rects);
412 static SDL_Surface *GS_SetVideoMode(_THIS, SDL_Surface *current,
413 int width, int height, int bpp, Uint32 flags)
415 struct ps2_screeninfo vinfo;
417 /* Set the terminal into graphics mode */
418 if ( GS_EnterGraphicsMode(this) < 0 ) {
422 /* Set the video mode and get the final screen format */
423 if ( ioctl(console_fd, PS2IOC_GSCREENINFO, &vinfo) < 0 ) {
424 SDL_SetError("Couldn't get console screen info");
427 if ( (vinfo.w != width) || (vinfo.h != height) ||
428 (GS_pixelmasks[vinfo.psm].bpp != bpp) ) {
429 /* If we're not in VESA mode, we have to scale resolution */
430 if ( saved_vinfo.mode == PS2_GS_VESA ) {
433 vinfo.res = PS2_GS_640x480;
436 vinfo.res = PS2_GS_800x600;
439 vinfo.res = PS2_GS_1024x768;
442 vinfo.res = PS2_GS_1280x1024;
445 SDL_SetError("Unsupported resolution: %dx%d\n",
449 vinfo.res |= (PS2_GS_75Hz << 8);
454 vinfo.psm = GS_formatmap[bpp/8];
455 if ( vinfo.psm < 0 ) {
456 SDL_SetError("Unsupported depth: %d bpp\n", bpp);
459 if ( ioctl(console_fd, PS2IOC_SSCREENINFO, &vinfo) < 0 ) {
460 SDL_SetError("Couldn't set console screen info");
464 /* Unmap the previous DMA buffer */
466 munmap(mapped_mem, mapped_len);
470 if ( ! SDL_ReallocFormat(current, GS_pixelmasks[vinfo.psm].bpp,
471 GS_pixelmasks[vinfo.psm].r,
472 GS_pixelmasks[vinfo.psm].g,
473 GS_pixelmasks[vinfo.psm].b, 0) ) {
477 /* Set up the new mode framebuffer */
478 current->flags = SDL_FULLSCREEN;
481 current->pitch = SDL_CalculatePitch(current);
483 /* Memory map the DMA area for block memory transfer */
484 if ( ! mapped_mem ) {
485 pixels_len = height * current->pitch;
486 mapped_len = pixels_len +
487 /* Screen update DMA command area */
488 sizeof(head_tags) + ((2 * MAXTAGS) * 16);
489 if ( saved_vinfo.mode != PS2_GS_VESA ) {
490 mapped_len += sizeof(tex_tags) + sizeof(scale_tags);
492 mapped_mem = mmap(0, mapped_len, PROT_READ|PROT_WRITE,
493 MAP_SHARED, memory_fd, 0);
494 if ( mapped_mem == MAP_FAILED ) {
495 SDL_SetError("Unable to map %d bytes for DMA",
501 /* Set up the entire screen for DMA transfer */
502 screen_image.ptr = mapped_mem;
503 screen_image.fbp = 0;
504 screen_image.fbw = (vinfo.w + 63) / 64;
505 screen_image.psm = vinfo.psm;
507 if ( vinfo.h == height ) {
510 /* Put image offscreen and scale to screen height */
511 screen_image.y = vinfo.h;
513 screen_image.w = current->w;
514 screen_image.h = current->h;
516 /* get screen image data size (qword aligned) */
517 screen_image_size = (screen_image.w * screen_image.h);
518 switch (screen_image.psm) {
520 screen_image_size *= 4;
523 screen_image_size *= 3;
526 screen_image_size *= 2;
529 screen_image_size = (screen_image_size + 15) & ~15;
531 /* Set up the memory for screen update DMA commands */
532 head_tags_mem = (unsigned long long *)
533 (mapped_mem + pixels_len);
534 image_tags_mem = (unsigned long long *)
535 ((caddr_t)head_tags_mem + sizeof(head_tags));
536 SDL_memcpy(head_tags_mem, head_tags, sizeof(head_tags));
537 if ( saved_vinfo.mode != PS2_GS_VESA ) {
538 tex_tags_mem = (unsigned long long *)
539 ((caddr_t)image_tags_mem + ((2*MAXTAGS)*16));
540 scale_tags_mem = (unsigned long long *)
541 ((caddr_t)tex_tags_mem + sizeof(tex_tags));
542 SDL_memcpy(tex_tags_mem, tex_tags, sizeof(tex_tags));
544 (vinfo.h * vinfo.w) / 64 +
545 ((unsigned long long)screen_image.fbw << 14) +
546 ((unsigned long long)screen_image.psm << 20) +
547 ((unsigned long long)power_of_2(screen_image.w) << 26) +
548 ((unsigned long long)power_of_2(screen_image.h) << 30) +
549 ((unsigned long long)1 << 34) +
550 ((unsigned long long)1 << 35);
551 SDL_memcpy(scale_tags_mem, scale_tags, sizeof(scale_tags));
553 ((unsigned long long)screen_image.w * 16) +
554 (((unsigned long long)screen_image.h * 16) << 16);
556 ((unsigned long long)vinfo.w * 16) +
557 (((unsigned long long)vinfo.h * 16) << 16);
560 current->pixels = NULL;
561 if ( SDL_getenv("SDL_FULLSCREEN_UPDATE") ) {
562 /* Correct semantics */
563 current->flags |= SDL_ASYNCBLIT;
565 /* We lie here - the screen memory isn't really the visible
566 display memory and still requires an update, but this
567 has the desired effect for most applications.
569 current->flags |= SDL_HWSURFACE;
572 /* Set the update rectangle function */
573 this->UpdateRects = GS_DMAFullUpdate;
579 /* We don't support hardware surfaces yet */
580 static int GS_AllocHWSurface(_THIS, SDL_Surface *surface)
584 static void GS_FreeHWSurface(_THIS, SDL_Surface *surface)
588 static int GS_LockHWSurface(_THIS, SDL_Surface *surface)
590 if ( surface == this->screen ) {
591 /* Since mouse motion affects 'pixels', lock it */
594 /* Make sure any pending DMA has completed */
596 ioctl(console_fd, PS2IOC_SENDQCT, 1);
600 /* If the cursor is drawn on the DMA area, remove it */
601 if ( cursor_drawn ) {
602 surface->pixels = mapped_mem + surface->offset;
603 SDL_EraseCursorNoLock(this->screen);
607 /* Set the surface pixels to the base of the DMA area */
608 surface->pixels = mapped_mem;
610 /* We're finished! */
615 static void GS_UnlockHWSurface(_THIS, SDL_Surface *surface)
617 if ( surface == this->screen ) {
618 /* Since mouse motion affects 'pixels', lock it */
620 surface->pixels = NULL;
625 static void GS_DMAFullUpdate(_THIS, int numrects, SDL_Rect *rects)
627 /* Lock so we aren't interrupted by a mouse update */
630 /* Make sure any pending DMA has completed */
632 ioctl(console_fd, PS2IOC_SENDQCT, 1);
636 /* If the mouse is visible, draw it on the DMA area */
637 if ( (SDL_cursorstate & CURSOR_VISIBLE) && !cursor_drawn ) {
638 this->screen->pixels = mapped_mem + this->screen->offset;
639 SDL_DrawCursorNoLock(this->screen);
640 this->screen->pixels = NULL;
644 /* Put the image onto the screen */
645 loadimage_nonblock(console_fd,
646 &screen_image, screen_image_size,
647 head_tags_mem, image_tags_mem);
648 if ( screen_image.y > 0 ) {
649 /* Need to scale offscreen image to TV output */
650 ioctl(console_fd, PS2IOC_SENDQCT, 1);
652 scaleimage_nonblock(console_fd, tex_tags_mem, scale_tags_mem);
657 /* We're finished! */
661 static int GS_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
666 static void GS_VideoQuit(_THIS)
668 /* Close console and input file descriptors */
669 if ( console_fd > 0 ) {
670 /* Unmap the video framebuffer */
672 /* Unmap the video framebuffer */
673 munmap(mapped_mem, mapped_len);
678 /* Restore the original video mode */
679 if ( GS_InGraphicsMode(this) ) {
680 ioctl(console_fd, PS2IOC_SSCREENINFO, &saved_vinfo);
683 /* We're all done with the graphics device */
688 GS_CloseKeyboard(this);