d08e7326 |
1 | #include <stdio.h> |
2 | #include <string.h> |
3 | #include <sys/types.h> |
4 | #include <sys/stat.h> |
5 | #include <fcntl.h> |
6 | #include <sys/ioctl.h> |
7 | #include <sys/mman.h> |
8 | #include <unistd.h> |
9 | #include <linux/fb.h> |
10 | #include <linux/matroxfb.h> |
11 | |
12 | #include "../common/emu.h" |
13 | #include "fbdev.h" |
14 | |
15 | #define FBDEV_MAX_BUFFERS 3 |
16 | |
17 | static int fbdev = -1; |
18 | static void *fbdev_mem = MAP_FAILED; |
19 | static int fbdev_mem_size; |
20 | static struct fb_var_screeninfo fbvar_old; |
21 | static struct fb_var_screeninfo fbvar_new; |
22 | static int fbdev_buffer_write; |
23 | |
24 | void *fbdev_buffers[FBDEV_MAX_BUFFERS]; |
25 | int fbdev_buffer_count; |
26 | |
27 | void plat_video_flip(void) |
28 | { |
29 | int draw_buf; |
30 | |
31 | if (fbdev_buffer_count < 2) |
32 | return; |
33 | |
34 | draw_buf = fbdev_buffer_write; |
35 | fbdev_buffer_write++; |
36 | if (fbdev_buffer_write >= fbdev_buffer_count) |
37 | fbdev_buffer_write = 0; |
38 | |
39 | fbvar_new.yoffset = fbvar_old.yres * draw_buf; |
40 | g_screen_ptr = fbdev_buffers[fbdev_buffer_write]; |
41 | } |
42 | |
43 | void plat_video_wait_vsync(void) |
44 | { |
45 | int arg = 0; |
46 | ioctl(fbdev, FBIO_WAITFORVSYNC, &arg); |
47 | } |
48 | |
49 | int vout_fbdev_init(int *w, int *h) |
50 | { |
51 | static const char *fbdev_name = "/dev/fb0"; |
52 | int i, ret; |
53 | |
54 | fbdev = open(fbdev_name, O_RDWR); |
55 | if (fbdev == -1) { |
56 | fprintf(stderr, "%s: ", fbdev_name); |
57 | perror("open"); |
58 | return -1; |
59 | } |
60 | |
61 | ret = ioctl(fbdev, FBIOGET_VSCREENINFO, &fbvar_old); |
62 | if (ret == -1) { |
63 | perror("FBIOGET_VSCREENINFO ioctl"); |
64 | goto fail; |
65 | } |
66 | |
67 | fbvar_new = fbvar_old; |
68 | printf("%s: %ix%i@%d\n", fbdev_name, fbvar_old.xres, fbvar_old.yres, fbvar_old.bits_per_pixel); |
69 | *w = fbvar_old.xres; |
70 | *h = fbvar_old.yres; |
71 | fbdev_buffer_count = FBDEV_MAX_BUFFERS; // be optimistic |
72 | |
73 | if (fbvar_new.bits_per_pixel != 16) { |
74 | printf(" switching to 16bpp\n"); |
75 | fbvar_new.bits_per_pixel = 16; |
76 | ret = ioctl(fbdev, FBIOPUT_VSCREENINFO, &fbvar_new); |
77 | if (ret == -1) { |
78 | perror("FBIOPUT_VSCREENINFO ioctl"); |
79 | goto fail; |
80 | } |
81 | } |
82 | |
83 | if (fbvar_new.yres_virtual < fbvar_old.yres * fbdev_buffer_count) { |
84 | fbvar_new.yres_virtual = fbvar_old.yres * fbdev_buffer_count; |
85 | ret = ioctl(fbdev, FBIOPUT_VSCREENINFO, &fbvar_new); |
86 | if (ret == -1) { |
87 | fbdev_buffer_count = 1; |
88 | fprintf(stderr, "Warning: failed to increase virtual resolution, " |
89 | "doublebuffering disabled\n"); |
90 | } |
91 | } |
92 | |
93 | fbdev_mem_size = *w * *h * 2 * fbdev_buffer_count; |
94 | fbdev_mem = mmap(0, fbdev_mem_size, PROT_WRITE|PROT_READ, MAP_SHARED, fbdev, 0); |
95 | if (fbdev_mem == MAP_FAILED && fbdev_buffer_count > 1) { |
96 | fbdev_mem_size = *w * *h * 2; |
97 | fbdev_buffer_count = 1; |
98 | fprintf(stderr, "Warning: can't map %d bytes, doublebuffering disabled\n", fbdev_mem_size); |
99 | fbdev_mem = mmap(0, fbdev_mem_size, PROT_WRITE|PROT_READ, MAP_SHARED, fbdev, 0); |
100 | } |
101 | if (fbdev_mem == MAP_FAILED) { |
102 | perror("mmap framebuffer"); |
103 | goto fail; |
104 | } |
105 | memset(fbdev_mem, 0, fbdev_mem_size); |
106 | for (i = 0; i < fbdev_buffer_count; i++) |
107 | fbdev_buffers[i] = (char *)fbdev_mem + i * *w * *h * 2; |
108 | g_screen_ptr = fbdev_buffers[0]; |
109 | |
110 | // some checks |
111 | ret = 0; |
112 | ret = ioctl(fbdev, FBIO_WAITFORVSYNC, &ret); |
113 | if (ret != 0) |
114 | fprintf(stderr, "Warning: vsync doesn't seem to be supported\n"); |
115 | |
116 | if (fbdev_buffer_count > 1) { |
117 | fbdev_buffer_write = 0; |
118 | fbvar_new.yoffset = fbvar_old.yres * (fbdev_buffer_count - 1); |
119 | ret = ioctl(fbdev, FBIOPAN_DISPLAY, &fbvar_new); |
120 | if (ret != 0) { |
121 | fbdev_buffer_count = 1; |
122 | fprintf(stderr, "Warning: can't pan display, doublebuffering disabled\n"); |
123 | } |
124 | } |
125 | |
126 | printf("fbdev initialized.\n"); |
127 | return 0; |
128 | |
129 | fail: |
130 | close(fbdev); |
131 | return -1; |
132 | } |
133 | |
134 | void vout_fbdev_finish(void) |
135 | { |
136 | ioctl(fbdev, FBIOPUT_VSCREENINFO, &fbvar_old); |
137 | if (fbdev_mem != MAP_FAILED) |
138 | munmap(fbdev_mem, fbdev_mem_size); |
139 | if (fbdev >= 0) |
140 | close(fbdev); |
141 | fbdev_mem = NULL; |
142 | fbdev = -1; |
143 | } |
144 | |
145 | #if 0 |
146 | void *g_screen_ptr; |
147 | int main() |
148 | { |
149 | int w, h; |
150 | vout_fbdev_init(&w, &h); |
151 | //while (1) |
152 | { |
153 | memset(g_screen_ptr, 0xff, fbdev_mem_size / 2); |
154 | plat_video_wait_vsync(); |
155 | plat_video_flip(); |
156 | memset(g_screen_ptr, 0x00, fbdev_mem_size / 2); |
157 | usleep(8000); |
158 | // plat_video_wait_vsync(); |
159 | plat_video_flip(); |
160 | } |
161 | } |
162 | #endif |