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