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