prepare for source release, relicense
[ginge.git] / common / host_fb.c
1 /*
2  * GINGE - GINGE Is Not Gp2x Emulator
3  * (C) notaz, 2010-2011
4  *
5  * This work is licensed under the MAME license, see COPYING file for details.
6  */
7 #include <string.h>
8 #ifdef LOADER
9 #include "../loader/realfuncs.h"
10 #endif
11
12 #include "host_fb.h"
13
14 static void *host_screen;
15 static int host_stride;
16
17 #if defined(PND)
18
19 #include "linux/fbdev.c"
20
21 static struct vout_fbdev *fbdev;
22 static unsigned short host_pal[256];
23
24 void *host_video_flip(void)
25 {
26   host_screen = vout_fbdev_flip(fbdev);
27   return host_screen;
28 }
29
30 int host_video_init(int *stride, int no_dblbuf)
31 {
32   const char *fbdev_name;
33   int w, h;
34
35   fbdev_name = getenv("FBDEV");
36   if (fbdev_name == NULL)
37     fbdev_name = "/dev/fb1";
38
39   w = h = 0;
40   fbdev = vout_fbdev_init(fbdev_name, &w, &h, 16, no_dblbuf);
41   if (fbdev == NULL)
42     return -1;
43
44   host_stride = w * 2;
45   if (stride != 0)
46     *stride = host_stride;
47   host_video_flip();
48
49   return 0;
50 }
51
52 void host_video_finish(void)
53 {
54   vout_fbdev_finish(fbdev);
55   fbdev = NULL;
56 }
57
58 void host_video_update_pal16(unsigned short *pal)
59 {
60   memcpy(host_pal, pal, sizeof(host_pal));
61 }
62
63 void host_video_update_pal32(unsigned int *pal)
64 {
65   unsigned short *dstp = host_pal;
66   int i;
67
68   for (i = 0; i < 256; i++, pal++, dstp++) {
69     unsigned int t = *pal;
70     *dstp = ((t >> 8) & 0xf800) | ((t >> 5) & 0x07e0) | ((t >> 3) & 0x001f);
71   }
72 }
73
74 void host_video_change_bpp(int bpp)
75 {
76 }
77
78 void host_video_blit4(const unsigned char *src, int w, int h, int stride)
79 {
80   unsigned short *dst = host_screen;
81   unsigned short *hpal = host_pal;
82   int i, u;
83
84   for (i = 0; i < 240; i++, dst += host_stride / 2, src += stride) {
85     for (u = 0; i < w / 2; u++) {
86       dst[u*2 + 0] = hpal[src[u] >> 4];
87       dst[u*2 + 1] = hpal[src[u] & 0x0f];
88     }
89   }
90
91   host_video_flip();
92 }
93
94 void host_video_blit8(const unsigned char *src, int w, int h, int stride)
95 {
96   unsigned short *dst = host_screen;
97   unsigned short *hpal = host_pal;
98   int i, u;
99
100   for (i = 0; i < 240; i++, dst += host_stride / 2, src += stride) {
101     for (u = 0; u < w; u += 4) {
102       dst[u + 0] = hpal[src[u + 0]];
103       dst[u + 1] = hpal[src[u + 1]];
104       dst[u + 2] = hpal[src[u + 2]];
105       dst[u + 3] = hpal[src[u + 3]];
106     }
107   }
108
109   host_video_flip();
110 }
111
112 void host_video_blit16(const unsigned short *src, int w, int h, int stride)
113 {
114   unsigned short *dst = host_screen;
115   int i;
116
117   for (i = 0; i < 240; i++, dst += host_stride / 2, src += stride / 2)
118     memcpy(dst, src, w*2);
119
120   host_video_flip();
121 }
122
123 #elif defined(WIZ)
124
125 #include "warm/warm.c"
126 #include "wiz_video.c"
127
128 void *host_video_flip(void)
129 {
130   vout_gp2x_flip();
131   host_screen = g_screen_ptr;
132   return host_screen;
133 }
134
135 int host_video_init(int *stride, int no_dblbuf)
136 {
137   int ret;
138
139   host_stride = 320 * 2;
140   if (stride != 0)
141     *stride = host_stride;
142
143   ret = vout_gp2x_init(no_dblbuf);
144   if (ret != 0)
145     return ret;
146
147   vout_gp2x_set_mode(16, !no_dblbuf);
148   host_video_flip();
149   return 0;
150 }
151
152 void host_video_finish(void)
153 {
154   vout_gp2x_finish();
155 }
156
157 void host_video_update_pal16(unsigned short *pal)
158 {
159   vout_gp2x_set_palette16(pal, 256);
160 }
161
162 void host_video_update_pal32(unsigned int *pal)
163 {
164   vout_gp2x_set_palette32(pal, 256);
165 }
166
167 void host_video_change_bpp(int bpp)
168 {
169   vout_gp2x_set_mode(bpp, 1);
170 }
171
172 #ifdef LOADER
173 void host_video_blit4(const unsigned char *src, int w, int h, int stride)
174 {
175   memcpy(host_screen, src, 320*240/2); // FIXME
176   host_video_flip();
177 }
178
179 void host_video_blit8(const unsigned char *src, int w, int h, int stride)
180 {
181   if (probably_caanoo) {
182     unsigned char *dst = host_screen;
183     int i;
184     for (i = 0; i < 240; i++, dst += 320, src += stride)
185       memcpy(dst, src, w);
186   }
187   else {
188     extern void rotated_blit8(void *dst, const void *linesx4);
189     rotated_blit8(host_screen, src);
190   }
191
192   host_video_flip();
193 }
194
195 void host_video_blit16(const unsigned short *src, int w, int h, int stride)
196 {
197   if (probably_caanoo) {
198     unsigned short *dst = host_screen;
199     int i;
200     for (i = 0; i < 240; i++, dst += 320, src += stride / 2)
201       memcpy(dst, src, w*2);
202   }
203   else {
204     extern void rotated_blit16(void *dst, const void *linesx4);
205     rotated_blit16(host_screen, src);
206   }
207
208   host_video_flip();
209 }
210 #endif // LOADER
211
212 #endif // WIZ
213
214 // vim:shiftwidth=2:expandtab