b60f2812 |
1 | /* |
2 | * (C) notaz, 2010 |
3 | * |
4 | * This work is licensed under the terms of the GNU GPLv2 or later. |
5 | * See the COPYING file in the top-level directory. |
6 | */ |
7 | |
8 | #include <stdio.h> |
9 | #include <stdlib.h> |
69af03a2 |
10 | #include <string.h> |
11 | #include <stdarg.h> |
72228559 |
12 | #include <sys/time.h> |
13 | #include <sys/types.h> |
14 | #include <sys/stat.h> |
15 | #include <fcntl.h> |
16 | #include <unistd.h> |
b60f2812 |
17 | |
72228559 |
18 | #include "plugin_lib.h" |
b60f2812 |
19 | #include "linux/fbdev.h" |
69af03a2 |
20 | #include "common/fonts.h" |
21 | #include "common/input.h" |
22 | #include "omap.h" |
3c70c47b |
23 | #include "menu.h" |
72228559 |
24 | #include "pcnt.h" |
69af03a2 |
25 | #include "../libpcsxcore/new_dynarec/new_dynarec.h" |
b60f2812 |
26 | |
b60f2812 |
27 | void *pl_fbdev_buf; |
bce6b056 |
28 | int pl_frame_interval; |
69af03a2 |
29 | int keystate; |
72228559 |
30 | static int pl_fbdev_w, pl_fbdev_h, pl_fbdev_bpp; |
bce6b056 |
31 | static int flip_cnt, vsync_cnt, flips_per_sec, tick_per_sec; |
32 | static float vsps_cur; |
72228559 |
33 | |
34 | static int get_cpu_ticks(void) |
35 | { |
36 | static unsigned long last_utime; |
37 | static int fd; |
38 | unsigned long utime, ret; |
39 | char buf[128]; |
40 | |
41 | if (fd == 0) |
42 | fd = open("/proc/self/stat", O_RDONLY); |
43 | lseek(fd, 0, SEEK_SET); |
44 | buf[0] = 0; |
45 | read(fd, buf, sizeof(buf)); |
46 | buf[sizeof(buf) - 1] = 0; |
47 | |
48 | sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu", &utime); |
49 | ret = utime - last_utime; |
50 | last_utime = utime; |
51 | return ret; |
52 | } |
53 | |
54 | static void print_fps(void) |
55 | { |
56 | if (pl_fbdev_bpp == 16) |
bce6b056 |
57 | pl_text_out16(2, pl_fbdev_h - 10, "%2d %4.1f", flips_per_sec, vsps_cur); |
72228559 |
58 | } |
59 | |
60 | static void print_cpu_usage(void) |
61 | { |
62 | if (pl_fbdev_bpp == 16) |
63 | pl_text_out16(pl_fbdev_w - 28, pl_fbdev_h - 10, "%3d", tick_per_sec); |
64 | } |
b60f2812 |
65 | |
297b3d63 |
66 | void *pl_fbdev_set_mode(int w, int h, int bpp) |
69af03a2 |
67 | { |
d352cde2 |
68 | void *ret; |
b60f2812 |
69 | |
72228559 |
70 | if (w == pl_fbdev_w && h == pl_fbdev_h && bpp == pl_fbdev_bpp) |
7947ab55 |
71 | return pl_fbdev_buf; |
72228559 |
72 | |
69af03a2 |
73 | pl_fbdev_w = w; |
72228559 |
74 | pl_fbdev_h = h; |
75 | pl_fbdev_bpp = bpp; |
d352cde2 |
76 | |
77 | vout_fbdev_clear(layer_fb); |
69af03a2 |
78 | ret = vout_fbdev_resize(layer_fb, w, h, bpp, 0, 0, 0, 0, 3); |
d352cde2 |
79 | if (ret == NULL) |
69af03a2 |
80 | fprintf(stderr, "failed to set mode\n"); |
d352cde2 |
81 | else |
82 | pl_fbdev_buf = ret; |
83 | |
bd6267e6 |
84 | menu_notify_mode_change(w, h, bpp); |
3c70c47b |
85 | |
297b3d63 |
86 | return pl_fbdev_buf; |
69af03a2 |
87 | } |
88 | |
ffd0d743 |
89 | void *pl_fbdev_flip(void) |
69af03a2 |
90 | { |
72228559 |
91 | flip_cnt++; |
297b3d63 |
92 | |
93 | if (pl_fbdev_buf != NULL) { |
94 | if (g_opts & OPT_SHOWFPS) |
95 | print_fps(); |
96 | if (g_opts & OPT_SHOWCPU) |
97 | print_cpu_usage(); |
98 | } |
72228559 |
99 | |
69af03a2 |
100 | // let's flip now |
101 | pl_fbdev_buf = vout_fbdev_flip(layer_fb); |
ffd0d743 |
102 | return pl_fbdev_buf; |
b60f2812 |
103 | } |
104 | |
6d1a1ac2 |
105 | int pl_fbdev_open(void) |
106 | { |
107 | pl_fbdev_buf = vout_fbdev_flip(layer_fb); |
108 | omap_enable_layer(1); |
109 | return 0; |
110 | } |
111 | |
112 | void pl_fbdev_close(void) |
b60f2812 |
113 | { |
6d1a1ac2 |
114 | omap_enable_layer(0); |
b60f2812 |
115 | } |
116 | |
15d46930 |
117 | static void update_input(void) |
118 | { |
119 | int actions[IN_BINDTYPE_COUNT] = { 0, }; |
120 | |
121 | in_update(actions); |
122 | if (actions[IN_BINDTYPE_EMU] & PEV_MENU) |
123 | stop = 1; |
124 | keystate = actions[IN_BINDTYPE_PLAYER12]; |
447783f8 |
125 | |
126 | #ifdef X11 |
127 | extern void x11_update_keys(void); |
128 | x11_update_keys(); |
129 | #endif |
15d46930 |
130 | } |
131 | |
bce6b056 |
132 | #define MAX_LAG_FRAMES 3 |
133 | |
134 | #define tvdiff(tv, tv_old) \ |
135 | ((tv.tv_sec - tv_old.tv_sec) * 1000000 + tv.tv_usec - tv_old.tv_usec) |
136 | // assumes us < 1000000 |
137 | #define tvadd(tv, us) { \ |
138 | tv.tv_usec += us; \ |
139 | if (tv.tv_usec >= 1000000) { \ |
140 | tv.tv_usec -= 1000000; \ |
141 | tv.tv_sec++; \ |
142 | } \ |
143 | } |
144 | |
72228559 |
145 | /* called on every vsync */ |
146 | void pl_frame_limit(void) |
147 | { |
bce6b056 |
148 | static struct timeval tv_old, tv_expect; |
149 | struct timeval now; |
150 | int diff; |
151 | |
152 | vsync_cnt++; |
72228559 |
153 | |
15d46930 |
154 | /* doing input here because the pad is polled |
155 | * thousands of times per frame for some reason */ |
156 | update_input(); |
157 | |
72228559 |
158 | pcnt_end(PCNT_ALL); |
bce6b056 |
159 | gettimeofday(&now, 0); |
72228559 |
160 | |
bce6b056 |
161 | if (now.tv_sec != tv_old.tv_sec) { |
162 | diff = tvdiff(now, tv_old); |
163 | vsps_cur = 0.0f; |
164 | if (0 < diff && diff < 2000000) |
165 | vsps_cur = 1000000.0f * vsync_cnt / diff; |
72228559 |
166 | flips_per_sec = flip_cnt; |
bce6b056 |
167 | vsync_cnt = flip_cnt = 0; |
168 | tv_old = now; |
bd6267e6 |
169 | if (g_opts & OPT_SHOWCPU) |
170 | tick_per_sec = get_cpu_ticks(); |
72228559 |
171 | } |
172 | #ifdef PCNT |
173 | static int ya_vsync_count; |
174 | if (++ya_vsync_count == PCNT_FRAMES) { |
bce6b056 |
175 | pcnt_print(vsps_cur); |
72228559 |
176 | ya_vsync_count = 0; |
177 | } |
178 | #endif |
179 | |
bce6b056 |
180 | if (!(g_opts & OPT_NO_FRAMELIM)) { |
181 | tvadd(tv_expect, pl_frame_interval); |
182 | diff = tvdiff(tv_expect, now); |
183 | if (diff > MAX_LAG_FRAMES * pl_frame_interval || diff < -MAX_LAG_FRAMES * pl_frame_interval) { |
184 | //printf("pl_frame_limit reset, diff=%d, iv %d\n", diff, pl_frame_interval); |
185 | tv_expect = now; |
186 | } |
187 | else if (diff > pl_frame_interval) { |
188 | // yay for working usleep on pandora! |
189 | //printf("usleep %d\n", diff - pl_frame_interval / 2); |
190 | usleep(diff - pl_frame_interval / 2); |
191 | } |
192 | } |
72228559 |
193 | |
194 | pcnt_start(PCNT_ALL); |
195 | } |
196 | |
69af03a2 |
197 | static void pl_text_out16_(int x, int y, const char *text) |
b60f2812 |
198 | { |
69af03a2 |
199 | int i, l, len = strlen(text), w = pl_fbdev_w; |
200 | unsigned short *screen = (unsigned short *)pl_fbdev_buf + x + y * w; |
201 | unsigned short val = 0xffff; |
202 | |
203 | for (i = 0; i < len; i++, screen += 8) |
204 | { |
205 | for (l = 0; l < 8; l++) |
206 | { |
207 | unsigned char fd = fontdata8x8[text[i] * 8 + l]; |
208 | unsigned short *s = screen + l * w; |
209 | if (fd&0x80) s[0] = val; |
210 | if (fd&0x40) s[1] = val; |
211 | if (fd&0x20) s[2] = val; |
212 | if (fd&0x10) s[3] = val; |
213 | if (fd&0x08) s[4] = val; |
214 | if (fd&0x04) s[5] = val; |
215 | if (fd&0x02) s[6] = val; |
216 | if (fd&0x01) s[7] = val; |
217 | } |
218 | } |
b60f2812 |
219 | } |
220 | |
69af03a2 |
221 | void pl_text_out16(int x, int y, const char *texto, ...) |
b60f2812 |
222 | { |
69af03a2 |
223 | va_list args; |
224 | char buffer[256]; |
225 | |
226 | va_start(args, texto); |
227 | vsnprintf(buffer, sizeof(buffer), texto, args); |
228 | va_end(args); |
229 | |
230 | pl_text_out16_(x, y, buffer); |
b60f2812 |
231 | } |
232 | |
201c21e2 |
233 | static void pl_get_layer_pos(int *x, int *y, int *w, int *h) |
234 | { |
235 | *x = g_layer_x; |
236 | *y = g_layer_y; |
237 | *w = g_layer_w; |
238 | *h = g_layer_h; |
239 | } |
240 | |
78d78c3b |
241 | extern int UseFrameSkip; // hmh |
242 | |
201c21e2 |
243 | const struct rearmed_cbs pl_rearmed_cbs = { |
244 | pl_get_layer_pos, |
ffd0d743 |
245 | pl_fbdev_open, |
246 | pl_fbdev_set_mode, |
247 | pl_fbdev_flip, |
248 | pl_fbdev_close, |
78d78c3b |
249 | &UseFrameSkip, |
201c21e2 |
250 | }; |
251 | |