2 * (C) GraÅžvydas "notaz" Ignotas, 2010
4 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
5 * See the COPYING file in the top-level directory.
15 #include "linux/fbdev.h"
16 #include "linux/oshide.h"
18 static SDL_Surface *g_screen;
19 static void *g_screen_fbp;
20 static Uint16 g_8bpp_pal[256];
21 static Uint32 g_start_ticks;
23 static inline int min(int v1, int v2)
25 return v1 < v2 ? v1 : v2;
28 static SDL_Surface *alloc_surface(int w, int h, int bpp)
30 // SDL has some pointer overuse, every surface has a format,
31 // why make format accessible through pointer?
37 ret = calloc(1, sizeof(*ret));
41 ret->s.format = &ret->f;
42 ret->f.BitsPerPixel = bpp;
43 ret->f.BytesPerPixel = (bpp + 7) / 8;
47 ret->s.pitch = w * ret->f.BytesPerPixel;
54 SDL_Init(Uint32 flags)
58 if (g_start_ticks == 0) {
65 g_start_ticks = SDL_GetTicks();
75 if (g_start_ticks != 0) {
82 SDL_InitSubSystem(Uint32 flags)
89 SDL_QuitSubSystem(Uint32 flags)
94 DECLSPEC Uint32 SDLCALL
100 gettimeofday(&tv, NULL);
101 ret = tv.tv_sec * 1000;
102 ret += tv.tv_usec * 1048 >> 20;
104 ret -= g_start_ticks;
105 dbg(" SDL_GetTicks %d", ret);
109 DECLSPEC SDL_Surface * SDLCALL
110 SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags)
112 struct vout_fbdev *fbdev;
114 trace("%d, %d, %d, %08x", width, height, bpp, flags);
119 if (bpp != 8 && bpp != 16) {
120 err("unsupported bpp: %d\n", bpp);
124 // XXX: destroy old g_screen?
126 g_screen = alloc_surface(width, height, bpp);
127 if (g_screen == NULL)
130 fbdev = vout_fbdev_init("/dev/fb0", &width, &height, 0);
132 goto fail_fbdev_init;
134 g_screen_fbp = vout_fbdev_flip(fbdev);
136 g_screen->pixels = g_screen_fbp;
138 // we have to fake this for now..
139 g_screen->pixels = calloc(g_screen->pitch * height, 1);
141 if (g_screen->pixels == NULL) {
145 g_screen->hwdata = (void *)fbdev;
147 dbg("returning %p", g_screen);
151 vout_fbdev_finish(fbdev);
159 SDL_Flip(SDL_Surface *screen)
161 struct vout_fbdev *fbdev;
165 if (screen != g_screen) {
166 err("flip not on screen surface?");
170 if (screen->format->BitsPerPixel == 8) {
171 int l = screen->pitch * screen->h;
173 do_clut(g_screen_fbp, screen->pixels, g_8bpp_pal, l);
175 Uint16 *d = g_screen_fbp;
176 Uint8 *s = screen->pixels;
178 // XXX: perhaps optimize this sh*t
179 for (; l > 0; d++, s++, l--)
184 fbdev = (void *)screen->hwdata;
185 g_screen_fbp = vout_fbdev_flip(fbdev);
187 if (screen->format->BitsPerPixel != 8)
188 screen->pixels = g_screen_fbp;
193 static int do_rect_clip(const SDL_Surface *s, SDL_Rect *r)
195 int x = r->x, y = r->y, w = r->w, h = r->h;
220 r->x = x; r->y = y; r->w = w; r->h = h;
221 return (w > 0 && h > 0) ? 0 : -1;
225 SDL_UpperBlit(SDL_Surface *src, SDL_Rect *srcrect,
226 SDL_Surface *dst, SDL_Rect *dstrect)
228 int sx = 0, sy = 0, sw, sh;
230 SDL_Rect tmprect = { 0, };
232 trace("%p, %p, %p, %p", src, srcrect, dst, dstrect);
234 if (src == NULL || dst == NULL)
237 if (srcrect != NULL) {
238 // XXX: dst pos may need to be adjusted in some corner cases
239 if (do_rect_clip(src, srcrect) < 0)
253 // SDL just uses source w and h
256 if (do_rect_clip(dst, dstrect) < 0)
263 dbg("(%d %d %d %d) -> (%d %d %d %d)", sx, sy, sw, sh, dx, dy, sw, sh);
265 if (src->format->BitsPerPixel == dst->format->BitsPerPixel) {
266 int Bpp = src->format->BytesPerPixel;
267 int dpitch = dst->pitch;
268 int spitch = src->pitch;
269 Uint8 *d = (Uint8 *)dst->pixels + dpitch * dy + dx * Bpp;
270 Uint8 *s = (Uint8 *)src->pixels + spitch * sy + sx * Bpp;
272 for (sw *= Bpp; sh > 0; d += dpitch, s += spitch, sh--)
281 DECLSPEC SDL_Surface * SDLCALL
282 SDL_CreateRGBSurface(Uint32 flags, int width, int height, int depth,
283 Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
287 trace("%08x, %d, %d, %d, %04x, %04x, %04x, %04x",
288 flags, width, height, depth, Rmask, Gmask, Bmask, Amask);
290 ret = alloc_surface(width, height, depth);
294 ret->pixels = calloc(ret->pitch * height, 1);
295 if (ret->pixels == NULL)
298 dbg("returning %p", ret);
307 SDL_SetColors(SDL_Surface *surface, SDL_Color *colors, int firstcolor, int ncolors)
311 trace("%p, %p, %d, %d", surface, colors, firstcolor, ncolors);
313 if (surface != g_screen)
316 to = min(ARRAY_SIZE(g_8bpp_pal), firstcolor + ncolors);
317 for (i = firstcolor; i < to; i++) {
318 SDL_Color *c = &colors[i - firstcolor];
319 int r = c->r, g = c->g, b = c->b;
320 // noiz2sa gets a good deal darker that real SDL,
321 // don't know what's going on there
323 if (r < 0xf8 && (r & 7) > 3) r += 8;
324 if (g < 0xfc && (g & 3) > 1) g += 4;
325 if (b < 0xf8 && (b & 7) > 3) b += 8;
327 g_8bpp_pal[i] = ((r << 8) & 0xf800) | ((g << 3) & 0x07e0) | (b >> 3);
328 // g_8bpp_pal[i] += 0x0821;
335 SDL_FillRect(SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color)
337 trace("%p, %p, %04x", dst, dstrect, color);
339 if (dst->format->BytesPerPixel != 1) {
344 memset(dst->pixels, color, dst->pitch * dst->h);
350 DECLSPEC SDL_Surface * SDLCALL
351 SDL_CreateRGBSurfaceFrom(void *pixels, int width, int height, int depth, int pitch,
352 Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
357 DECLSPEC SDL_RWops * SDLCALL
358 SDL_RWFromFile(const char *file, const char *mode)
363 trace("%s, %s", file, mode);
365 f = fopen(file, mode);
369 ret = calloc(1, sizeof(*ret));
374 ret->hidden.stdio.fp = f;
379 DECLSPEC void SDLCALL
380 SDL_FreeRW(SDL_RWops *area)
382 if (area->hidden.stdio.fp) {
383 fclose(area->hidden.stdio.fp);
384 area->hidden.stdio.fp = NULL;
389 DECLSPEC SDL_Surface * SDLCALL
390 SDL_LoadBMP_RW(SDL_RWops *src, int freesrc)
393 unsigned char magic[2];
403 uint32_t compress_type;
409 } __attribute__((packed)) bmp;
410 SDL_Surface *ret = NULL;
411 int data_size, read_size;
412 uint8_t *tmp_buf = NULL;
416 trace("%p, %d", src, freesrc);
418 f = src->hidden.stdio.fp;
422 if (fread(&bmp, 1, sizeof(bmp), f) != sizeof(bmp)) {
423 err("bmp read error");
427 if (bmp.magic[0] != 'B' || bmp.magic[1] != 'M' || bmp.header_sz != 40) {
428 err("unhandled BMP format, sz=%d", bmp.header_sz);
432 if ((unsigned)bmp.width > 0xffff || (unsigned)bmp.height > 0xffff) {
433 err("BMP size %dx%d out of range", bmp.width, bmp.height);
437 if (bmp.bitspp != 4) {
438 err("BMP unhandled bpp: %d", bmp.bitspp);
442 bytespp = bmp.bitspp < 8 ? 1 : (bmp.bitspp + 7) / 8;
443 data_size = bmp.width * bmp.height * bytespp;
444 tmp_buf = malloc(data_size);
448 if (fseek(f, bmp.bmp_offset, SEEK_SET) != 0) {
449 err("BMP seek error");
453 read_size = bmp.width * bmp.bitspp / 8;
454 for (i = bmp.height - 1; i > 0; i--) {
455 if (fread((char *)tmp_buf + i * read_size, 1, read_size, f) != read_size) {
456 err("BMP read error");
461 if (bmp.bitspp == 4) {
462 // just convert to 8bpp now
463 int c = bmp.width * bmp.height / 2;
464 uint8_t *p = tmp_buf;
465 for (i = c - 1; i >= 0; i--) {
466 p[i * 2 + 1] = p[i] & 0xf;
467 p[i * 2] = p[i] >> 4;
471 ret = alloc_surface(bmp.width, bmp.height, 8);
475 ret->pixels = tmp_buf;
487 DECLSPEC SDL_Surface * SDLCALL
488 SDL_ConvertSurface(SDL_Surface *src, SDL_PixelFormat *fmt, Uint32 flags)
493 trace("%p, %p, %08x", src, fmt, flags);
495 if (src->format->BitsPerPixel != fmt->BitsPerPixel) {
496 err("convert: not implemented");
500 data = malloc(src->pitch * src->h);
503 memcpy(data, src->pixels, src->pitch * src->h);
505 ret = alloc_surface(src->w, src->h, fmt->BitsPerPixel);
515 SDL_SetColorKey(SDL_Surface *surface, Uint32 flag, Uint32 key)
517 trace("%p, %08x, %04x", surface, flag, key);
521 DECLSPEC void SDLCALL
522 SDL_WM_SetCaption(const char *title, const char *icon)
524 trace("%s, %s", title, icon);
528 SDL_ShowCursor(int toggle)
534 DECLSPEC char * SDLCALL