platform ps2, handle audio similar to psp
[picodrive.git] / linux / fbdev.c
1 /*
2  * (C) GraÅžvydas "notaz" Ignotas, 2009-2010
3  *
4  * This work is licensed under the terms of any of these licenses
5  * (at your option):
6  *  - GNU GPL, version 2 or later.
7  *  - GNU LGPL, version 2.1 or later.
8  * See the COPYING file in the top-level directory.
9  */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <fcntl.h>
17 #include <sys/ioctl.h>
18 #include <sys/mman.h>
19 #include <unistd.h>
20 #include <linux/fb.h>
21 #include <linux/matroxfb.h>
22
23 #include "fbdev.h"
24
25 #define PFX "fbdev: "
26
27 struct vout_fbdev {
28         int     fd;
29         void    *mem;
30         size_t  mem_size;
31         struct  fb_var_screeninfo fbvar_old;
32         struct  fb_var_screeninfo fbvar_new;
33         int     buffer_write;
34         int     fb_size;
35         int     buffer_count;
36         int     top_border, bottom_border;
37         void    *mem_saved;
38         size_t  mem_saved_size;
39 };
40
41 void *vout_fbdev_flip(struct vout_fbdev *fbdev)
42 {
43         int draw_buf;
44
45         if (fbdev->buffer_count < 2)
46                 return fbdev->mem;
47
48         draw_buf = fbdev->buffer_write;
49         fbdev->buffer_write++;
50         if (fbdev->buffer_write >= fbdev->buffer_count)
51                 fbdev->buffer_write = 0;
52
53         fbdev->fbvar_new.yoffset = 
54                 (fbdev->top_border + fbdev->fbvar_new.yres + fbdev->bottom_border) * draw_buf +
55                 fbdev->top_border;
56
57         ioctl(fbdev->fd, FBIOPAN_DISPLAY, &fbdev->fbvar_new);
58
59         return (char *)fbdev->mem + fbdev->fb_size * fbdev->buffer_write;
60 }
61
62 void vout_fbdev_wait_vsync(struct vout_fbdev *fbdev)
63 {
64         int arg = 0;
65         ioctl(fbdev->fd, FBIO_WAITFORVSYNC, &arg);
66 }
67
68 /* it is recommended to call vout_fbdev_clear() before this */
69 void *vout_fbdev_resize(struct vout_fbdev *fbdev, int w, int h, int bpp,
70                       int left_border, int right_border, int top_border, int bottom_border, int buffer_cnt)
71 {
72         int w_total = left_border + w + right_border;
73         int h_total = top_border + h + bottom_border;
74         size_t mem_size;
75         int ret;
76
77         // unblank to be sure the mode is really accepted
78         ioctl(fbdev->fd, FBIOBLANK, FB_BLANK_UNBLANK);
79
80         if (fbdev->fbvar_new.bits_per_pixel != bpp ||
81                         fbdev->fbvar_new.xres != w ||
82                         fbdev->fbvar_new.yres != h ||
83                         fbdev->fbvar_new.xres_virtual != w_total||
84                         fbdev->fbvar_new.yres_virtual < h_total ||
85                         fbdev->fbvar_new.xoffset != left_border ||
86                         fbdev->buffer_count != buffer_cnt)
87         {
88                 if (fbdev->fbvar_new.bits_per_pixel != bpp ||
89                                 w != fbdev->fbvar_new.xres || h != fbdev->fbvar_new.yres)
90                         printf(PFX "switching to %dx%d@%d\n", w, h, bpp);
91
92                 fbdev->fbvar_new.xres = w;
93                 fbdev->fbvar_new.yres = h;
94                 fbdev->fbvar_new.xres_virtual = w_total;
95                 fbdev->fbvar_new.yres_virtual = h_total * buffer_cnt;
96                 fbdev->fbvar_new.xoffset = left_border;
97                 fbdev->fbvar_new.yoffset = top_border;
98                 fbdev->fbvar_new.bits_per_pixel = bpp;
99                 fbdev->fbvar_new.nonstd = 0; // can set YUV here on omapfb
100                 fbdev->buffer_count = buffer_cnt;
101                 fbdev->buffer_write = buffer_cnt > 1 ? 1 : 0;
102
103                 // seems to help a bit to avoid glitches
104                 vout_fbdev_wait_vsync(fbdev);
105
106                 ret = ioctl(fbdev->fd, FBIOPUT_VSCREENINFO, &fbdev->fbvar_new);
107                 if (ret == -1) {
108                         // retry with no multibuffering
109                         fbdev->fbvar_new.yres_virtual = h_total;
110                         ret = ioctl(fbdev->fd, FBIOPUT_VSCREENINFO, &fbdev->fbvar_new);
111                         if (ret == -1) {
112                                 perror(PFX "FBIOPUT_VSCREENINFO ioctl");
113                                 return NULL;
114                         }
115                         fbdev->buffer_count = 1;
116                         fbdev->buffer_write = 0;
117                         fprintf(stderr, PFX "Warning: failed to increase virtual resolution, "
118                                         "multibuffering disabled\n");
119                 }
120
121         }
122
123         fbdev->fb_size = w_total * h_total * bpp / 8;
124         fbdev->top_border = top_border;
125         fbdev->bottom_border = bottom_border;
126
127         mem_size = fbdev->fb_size * fbdev->buffer_count;
128         if (fbdev->mem_size >= mem_size)
129                 goto out;
130
131         if (fbdev->mem != NULL)
132                 munmap(fbdev->mem, fbdev->mem_size);
133
134         fbdev->mem = mmap(0, mem_size, PROT_WRITE|PROT_READ, MAP_SHARED, fbdev->fd, 0);
135         if (fbdev->mem == MAP_FAILED && fbdev->buffer_count > 1) {
136                 fprintf(stderr, PFX "Warning: can't map %zd bytes, doublebuffering disabled\n", mem_size);
137                 fbdev->buffer_count = 1;
138                 fbdev->buffer_write = 0;
139                 mem_size = fbdev->fb_size;
140                 fbdev->mem = mmap(0, mem_size, PROT_WRITE|PROT_READ, MAP_SHARED, fbdev->fd, 0);
141         }
142         if (fbdev->mem == MAP_FAILED) {
143                 fbdev->mem = NULL;
144                 fbdev->mem_size = 0;
145                 perror(PFX "mmap framebuffer");
146                 return NULL;
147         }
148
149         fbdev->mem_size = mem_size;
150
151 out:
152         return (char *)fbdev->mem + fbdev->fb_size * fbdev->buffer_write;
153 }
154
155 void vout_fbdev_clear(struct vout_fbdev *fbdev)
156 {
157         memset(fbdev->mem, 0, fbdev->mem_size);
158 }
159
160 void vout_fbdev_clear_lines(struct vout_fbdev *fbdev, int y, int count)
161 {
162         int stride = fbdev->fbvar_new.xres_virtual * fbdev->fbvar_new.bits_per_pixel / 8;
163         int i;
164
165         if (y + count > fbdev->top_border + fbdev->fbvar_new.yres)
166                 count = fbdev->top_border + fbdev->fbvar_new.yres - y;
167
168         if (y >= 0 && count > 0)
169                 for (i = 0; i < fbdev->buffer_count; i++)
170                         memset((char *)fbdev->mem + fbdev->fb_size * i + y * stride, 0, stride * count);
171 }
172
173 int vout_fbdev_get_fd(struct vout_fbdev *fbdev)
174 {
175         return fbdev->fd;
176 }
177
178 struct vout_fbdev *vout_fbdev_init(const char *fbdev_name, int *w, int *h, int bpp, int buffer_cnt)
179 {
180         struct vout_fbdev *fbdev;
181         int req_w, req_h;
182         void *pret;
183         int ret;
184
185         fbdev = calloc(1, sizeof(*fbdev));
186         if (fbdev == NULL)
187                 return NULL;
188
189         fbdev->fd = open(fbdev_name, O_RDWR);
190         if (fbdev->fd == -1) {
191                 fprintf(stderr, PFX "%s: ", fbdev_name);
192                 perror("open");
193                 goto fail_open;
194         }
195
196         ret = ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->fbvar_old);
197         if (ret == -1) {
198                 perror(PFX "FBIOGET_VSCREENINFO ioctl");
199                 goto fail;
200         }
201
202         fbdev->fbvar_new = fbdev->fbvar_old;
203
204         req_w = fbdev->fbvar_new.xres;
205         if (*w != 0)
206                 req_w = *w;
207         req_h = fbdev->fbvar_new.yres;
208         if (*h != 0)
209                 req_h = *h;
210
211         pret = vout_fbdev_resize(fbdev, req_w, req_h, bpp, 0, 0, 0, 0, buffer_cnt);
212         if (pret == NULL)
213                 goto fail;
214
215         printf(PFX "%s: %ix%i@%d\n", fbdev_name, fbdev->fbvar_new.xres,
216                 fbdev->fbvar_new.yres, fbdev->fbvar_new.bits_per_pixel);
217         *w = fbdev->fbvar_new.xres;
218         *h = fbdev->fbvar_new.yres;
219
220         memset(fbdev->mem, 0, fbdev->mem_size);
221
222         // some checks
223         ret = 0;
224         ret = ioctl(fbdev->fd, FBIO_WAITFORVSYNC, &ret);
225         if (ret != 0)
226                 fprintf(stderr, PFX "Warning: vsync doesn't seem to be supported\n");
227
228         if (fbdev->buffer_count > 1) {
229                 fbdev->buffer_write = 0;
230                 fbdev->fbvar_new.yoffset = fbdev->fbvar_new.yres * (fbdev->buffer_count - 1);
231                 ret = ioctl(fbdev->fd, FBIOPAN_DISPLAY, &fbdev->fbvar_new);
232                 if (ret != 0) {
233                         fbdev->buffer_count = 1;
234                         fprintf(stderr, PFX "Warning: can't pan display, doublebuffering disabled\n");
235                 }
236         }
237
238         printf("fbdev initialized.\n");
239         return fbdev;
240
241 fail:
242         close(fbdev->fd);
243 fail_open:
244         free(fbdev);
245         return NULL;
246 }
247
248 static void vout_fbdev_release(struct vout_fbdev *fbdev)
249 {
250         ioctl(fbdev->fd, FBIOPUT_VSCREENINFO, &fbdev->fbvar_old);
251         if (fbdev->mem != MAP_FAILED)
252                 munmap(fbdev->mem, fbdev->mem_size);
253         fbdev->mem = NULL;
254 }
255
256 int vout_fbdev_save(struct vout_fbdev *fbdev)
257 {
258         void *tmp;
259
260         if (fbdev == NULL || fbdev->mem == NULL || fbdev->mem == MAP_FAILED) {
261                 fprintf(stderr, PFX "bad args for save\n");
262                 return -1;
263         }
264
265         if (fbdev->mem_saved_size < fbdev->mem_size) {
266                 tmp = realloc(fbdev->mem_saved, fbdev->mem_size);
267                 if (tmp == NULL)
268                         return -1;
269                 fbdev->mem_saved = tmp;
270         }
271         memcpy(fbdev->mem_saved, fbdev->mem, fbdev->mem_size);
272         fbdev->mem_saved_size = fbdev->mem_size;
273
274         vout_fbdev_release(fbdev);
275         return 0;
276 }
277
278 int vout_fbdev_restore(struct vout_fbdev *fbdev)
279 {
280         int ret;
281
282         if (fbdev == NULL || fbdev->mem != NULL) {
283                 fprintf(stderr, PFX "bad args/state for restore\n");
284                 return -1;
285         }
286
287         fbdev->mem = mmap(0, fbdev->mem_size, PROT_WRITE|PROT_READ, MAP_SHARED, fbdev->fd, 0);
288         if (fbdev->mem == MAP_FAILED) {
289                 perror(PFX "restore: memory restore failed");
290                 return -1;
291         }
292         memcpy(fbdev->mem, fbdev->mem_saved, fbdev->mem_size);
293
294         ret = ioctl(fbdev->fd, FBIOPUT_VSCREENINFO, &fbdev->fbvar_new);
295         if (ret == -1) {
296                 perror(PFX "restore: FBIOPUT_VSCREENINFO");
297                 return -1;
298         }
299
300         return 0;
301 }
302
303 void vout_fbdev_finish(struct vout_fbdev *fbdev)
304 {
305         vout_fbdev_release(fbdev);
306         if (fbdev->fd >= 0)
307                 close(fbdev->fd);
308         fbdev->fd = -1;
309         free(fbdev);
310 }
311