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> |
7c0f51de |
17 | #include <pthread.h> |
b60f2812 |
18 | |
72228559 |
19 | #include "plugin_lib.h" |
b60f2812 |
20 | #include "linux/fbdev.h" |
69af03a2 |
21 | #include "common/fonts.h" |
22 | #include "common/input.h" |
23 | #include "omap.h" |
3c70c47b |
24 | #include "menu.h" |
72228559 |
25 | #include "pcnt.h" |
69af03a2 |
26 | #include "../libpcsxcore/new_dynarec/new_dynarec.h" |
b60f2812 |
27 | |
b60f2812 |
28 | void *pl_fbdev_buf; |
bce6b056 |
29 | int pl_frame_interval; |
69af03a2 |
30 | int keystate; |
72228559 |
31 | static int pl_fbdev_w, pl_fbdev_h, pl_fbdev_bpp; |
bce6b056 |
32 | static int flip_cnt, vsync_cnt, flips_per_sec, tick_per_sec; |
33 | static float vsps_cur; |
c89cd762 |
34 | static int plugin_skip_advice; |
907b1e90 |
35 | // P.E.Op.S. |
36 | extern int UseFrameSkip; |
37 | extern float fps_skip; |
72228559 |
38 | |
39 | static int get_cpu_ticks(void) |
40 | { |
41 | static unsigned long last_utime; |
42 | static int fd; |
43 | unsigned long utime, ret; |
44 | char buf[128]; |
45 | |
46 | if (fd == 0) |
47 | fd = open("/proc/self/stat", O_RDONLY); |
48 | lseek(fd, 0, SEEK_SET); |
49 | buf[0] = 0; |
50 | read(fd, buf, sizeof(buf)); |
51 | buf[sizeof(buf) - 1] = 0; |
52 | |
53 | sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu", &utime); |
54 | ret = utime - last_utime; |
55 | last_utime = utime; |
56 | return ret; |
57 | } |
58 | |
59 | static void print_fps(void) |
60 | { |
61 | if (pl_fbdev_bpp == 16) |
bce6b056 |
62 | pl_text_out16(2, pl_fbdev_h - 10, "%2d %4.1f", flips_per_sec, vsps_cur); |
72228559 |
63 | } |
64 | |
65 | static void print_cpu_usage(void) |
66 | { |
67 | if (pl_fbdev_bpp == 16) |
68 | pl_text_out16(pl_fbdev_w - 28, pl_fbdev_h - 10, "%3d", tick_per_sec); |
69 | } |
b60f2812 |
70 | |
297b3d63 |
71 | void *pl_fbdev_set_mode(int w, int h, int bpp) |
69af03a2 |
72 | { |
d352cde2 |
73 | void *ret; |
b60f2812 |
74 | |
72228559 |
75 | if (w == pl_fbdev_w && h == pl_fbdev_h && bpp == pl_fbdev_bpp) |
7947ab55 |
76 | return pl_fbdev_buf; |
72228559 |
77 | |
69af03a2 |
78 | pl_fbdev_w = w; |
72228559 |
79 | pl_fbdev_h = h; |
80 | pl_fbdev_bpp = bpp; |
d352cde2 |
81 | |
82 | vout_fbdev_clear(layer_fb); |
69af03a2 |
83 | ret = vout_fbdev_resize(layer_fb, w, h, bpp, 0, 0, 0, 0, 3); |
d352cde2 |
84 | if (ret == NULL) |
69af03a2 |
85 | fprintf(stderr, "failed to set mode\n"); |
d352cde2 |
86 | else |
87 | pl_fbdev_buf = ret; |
88 | |
bd6267e6 |
89 | menu_notify_mode_change(w, h, bpp); |
3c70c47b |
90 | |
297b3d63 |
91 | return pl_fbdev_buf; |
69af03a2 |
92 | } |
93 | |
ffd0d743 |
94 | void *pl_fbdev_flip(void) |
69af03a2 |
95 | { |
72228559 |
96 | flip_cnt++; |
297b3d63 |
97 | |
98 | if (pl_fbdev_buf != NULL) { |
99 | if (g_opts & OPT_SHOWFPS) |
100 | print_fps(); |
101 | if (g_opts & OPT_SHOWCPU) |
102 | print_cpu_usage(); |
103 | } |
72228559 |
104 | |
69af03a2 |
105 | // let's flip now |
106 | pl_fbdev_buf = vout_fbdev_flip(layer_fb); |
ffd0d743 |
107 | return pl_fbdev_buf; |
b60f2812 |
108 | } |
109 | |
6d1a1ac2 |
110 | int pl_fbdev_open(void) |
111 | { |
112 | pl_fbdev_buf = vout_fbdev_flip(layer_fb); |
113 | omap_enable_layer(1); |
114 | return 0; |
115 | } |
116 | |
117 | void pl_fbdev_close(void) |
b60f2812 |
118 | { |
6d1a1ac2 |
119 | omap_enable_layer(0); |
b60f2812 |
120 | } |
121 | |
15d46930 |
122 | static void update_input(void) |
123 | { |
124 | int actions[IN_BINDTYPE_COUNT] = { 0, }; |
125 | |
126 | in_update(actions); |
127 | if (actions[IN_BINDTYPE_EMU] & PEV_MENU) |
128 | stop = 1; |
129 | keystate = actions[IN_BINDTYPE_PLAYER12]; |
447783f8 |
130 | |
131 | #ifdef X11 |
1bd9ee68 |
132 | extern int x11_update_keys(void); |
133 | keystate |= x11_update_keys(); |
447783f8 |
134 | #endif |
15d46930 |
135 | } |
136 | |
bce6b056 |
137 | #define MAX_LAG_FRAMES 3 |
138 | |
139 | #define tvdiff(tv, tv_old) \ |
140 | ((tv.tv_sec - tv_old.tv_sec) * 1000000 + tv.tv_usec - tv_old.tv_usec) |
141 | // assumes us < 1000000 |
142 | #define tvadd(tv, us) { \ |
143 | tv.tv_usec += us; \ |
144 | if (tv.tv_usec >= 1000000) { \ |
145 | tv.tv_usec -= 1000000; \ |
146 | tv.tv_sec++; \ |
147 | } \ |
148 | } |
149 | |
72228559 |
150 | /* called on every vsync */ |
151 | void pl_frame_limit(void) |
152 | { |
bce6b056 |
153 | static struct timeval tv_old, tv_expect; |
7c0f51de |
154 | static int vsync_cnt_prev; |
bce6b056 |
155 | struct timeval now; |
156 | int diff; |
157 | |
158 | vsync_cnt++; |
72228559 |
159 | |
15d46930 |
160 | /* doing input here because the pad is polled |
161 | * thousands of times per frame for some reason */ |
162 | update_input(); |
163 | |
72228559 |
164 | pcnt_end(PCNT_ALL); |
bce6b056 |
165 | gettimeofday(&now, 0); |
72228559 |
166 | |
bce6b056 |
167 | if (now.tv_sec != tv_old.tv_sec) { |
168 | diff = tvdiff(now, tv_old); |
169 | vsps_cur = 0.0f; |
170 | if (0 < diff && diff < 2000000) |
7c0f51de |
171 | vsps_cur = 1000000.0f * (vsync_cnt - vsync_cnt_prev) / diff; |
172 | vsync_cnt_prev = vsync_cnt; |
72228559 |
173 | flips_per_sec = flip_cnt; |
7c0f51de |
174 | flip_cnt = 0; |
bce6b056 |
175 | tv_old = now; |
bd6267e6 |
176 | if (g_opts & OPT_SHOWCPU) |
177 | tick_per_sec = get_cpu_ticks(); |
72228559 |
178 | } |
179 | #ifdef PCNT |
180 | static int ya_vsync_count; |
181 | if (++ya_vsync_count == PCNT_FRAMES) { |
bce6b056 |
182 | pcnt_print(vsps_cur); |
72228559 |
183 | ya_vsync_count = 0; |
184 | } |
185 | #endif |
186 | |
c89cd762 |
187 | tvadd(tv_expect, pl_frame_interval); |
188 | diff = tvdiff(tv_expect, now); |
189 | if (diff > MAX_LAG_FRAMES * pl_frame_interval || diff < -MAX_LAG_FRAMES * pl_frame_interval) { |
190 | //printf("pl_frame_limit reset, diff=%d, iv %d\n", diff, pl_frame_interval); |
191 | tv_expect = now; |
192 | diff = 0; |
193 | } |
194 | |
195 | if (!(g_opts & OPT_NO_FRAMELIM) && diff > pl_frame_interval) { |
196 | // yay for working usleep on pandora! |
197 | //printf("usleep %d\n", diff - pl_frame_interval / 2); |
198 | usleep(diff - pl_frame_interval / 2); |
199 | } |
200 | |
201 | plugin_skip_advice = 0; |
202 | if (UseFrameSkip && diff < -pl_frame_interval) { |
203 | // P.E.Op.S. makes skip decision based on this |
204 | fps_skip = 1.0f; |
205 | plugin_skip_advice = 1; |
bce6b056 |
206 | } |
72228559 |
207 | |
208 | pcnt_start(PCNT_ALL); |
209 | } |
210 | |
69af03a2 |
211 | static void pl_text_out16_(int x, int y, const char *text) |
b60f2812 |
212 | { |
69af03a2 |
213 | int i, l, len = strlen(text), w = pl_fbdev_w; |
214 | unsigned short *screen = (unsigned short *)pl_fbdev_buf + x + y * w; |
215 | unsigned short val = 0xffff; |
216 | |
217 | for (i = 0; i < len; i++, screen += 8) |
218 | { |
219 | for (l = 0; l < 8; l++) |
220 | { |
221 | unsigned char fd = fontdata8x8[text[i] * 8 + l]; |
222 | unsigned short *s = screen + l * w; |
223 | if (fd&0x80) s[0] = val; |
224 | if (fd&0x40) s[1] = val; |
225 | if (fd&0x20) s[2] = val; |
226 | if (fd&0x10) s[3] = val; |
227 | if (fd&0x08) s[4] = val; |
228 | if (fd&0x04) s[5] = val; |
229 | if (fd&0x02) s[6] = val; |
230 | if (fd&0x01) s[7] = val; |
231 | } |
232 | } |
b60f2812 |
233 | } |
234 | |
69af03a2 |
235 | void pl_text_out16(int x, int y, const char *texto, ...) |
b60f2812 |
236 | { |
69af03a2 |
237 | va_list args; |
238 | char buffer[256]; |
239 | |
240 | va_start(args, texto); |
241 | vsnprintf(buffer, sizeof(buffer), texto, args); |
242 | va_end(args); |
243 | |
244 | pl_text_out16_(x, y, buffer); |
b60f2812 |
245 | } |
246 | |
201c21e2 |
247 | static void pl_get_layer_pos(int *x, int *y, int *w, int *h) |
248 | { |
249 | *x = g_layer_x; |
250 | *y = g_layer_y; |
251 | *w = g_layer_w; |
252 | *h = g_layer_h; |
253 | } |
254 | |
255 | const struct rearmed_cbs pl_rearmed_cbs = { |
256 | pl_get_layer_pos, |
ffd0d743 |
257 | pl_fbdev_open, |
258 | pl_fbdev_set_mode, |
259 | pl_fbdev_flip, |
260 | pl_fbdev_close, |
c89cd762 |
261 | &plugin_skip_advice, |
201c21e2 |
262 | }; |
263 | |
7c0f51de |
264 | /* watchdog */ |
265 | static void *watchdog_thread(void *unused) |
266 | { |
267 | int vsync_cnt_old = 0; |
268 | int seen_dead = 0; |
269 | int sleep_time = 5; |
270 | |
271 | while (1) |
272 | { |
273 | sleep(sleep_time); |
274 | |
275 | if (stop) { |
276 | seen_dead = 0; |
277 | sleep_time = 5; |
278 | continue; |
279 | } |
280 | if (vsync_cnt != vsync_cnt_old) { |
281 | vsync_cnt_old = vsync_cnt; |
282 | seen_dead = 0; |
283 | sleep_time = 2; |
284 | continue; |
285 | } |
286 | |
287 | seen_dead++; |
288 | sleep_time = 1; |
289 | if (seen_dead > 1) |
290 | fprintf(stderr, "watchdog: seen_dead %d\n", seen_dead); |
291 | if (seen_dead > 4) { |
292 | fprintf(stderr, "watchdog: lockup detected, aborting\n"); |
293 | // we can't do any cleanup here really, the main thread is |
294 | // likely touching resources and would crash anyway |
295 | abort(); |
296 | } |
297 | } |
298 | } |
299 | |
300 | void pl_start_watchdog(void) |
301 | { |
302 | pthread_attr_t attr; |
303 | pthread_t tid; |
304 | int ret; |
305 | |
306 | pthread_attr_init(&attr); |
307 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
308 | |
309 | ret = pthread_create(&tid, &attr, watchdog_thread, NULL); |
310 | if (ret != 0) |
311 | fprintf(stderr, "could not start watchdog: %d\n", ret); |
312 | } |
313 | |