4 * This work is licensed under the terms of the GNU GPLv2 or later.
5 * See the COPYING file in the top-level directory.
14 #include <sys/types.h>
20 #include "plugin_lib.h"
21 #include "linux/fbdev.h"
22 #include "common/fonts.h"
23 #include "common/input.h"
28 #include "../libpcsxcore/new_dynarec/new_dynarec.h"
29 #include "../libpcsxcore/psemu_plugin_defs.h"
32 int pl_frame_interval;
33 int in_type, in_keystate, in_a1[2] = { 127, 127 }, in_a2[2] = { 127, 127 };
34 static int pl_fbdev_w, pl_fbdev_h, pl_fbdev_bpp;
35 static int flip_cnt, vsync_cnt, flips_per_sec, tick_per_sec;
36 static float vsps_cur;
37 static int plugin_skip_advice;
38 static int vsync_usec_time;
40 extern int UseFrameSkip;
41 extern float fps_skip;
43 static int get_cpu_ticks(void)
45 static unsigned long last_utime;
47 unsigned long utime, ret;
51 fd = open("/proc/self/stat", O_RDONLY);
52 lseek(fd, 0, SEEK_SET);
54 read(fd, buf, sizeof(buf));
55 buf[sizeof(buf) - 1] = 0;
57 sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu", &utime);
58 ret = utime - last_utime;
63 static void print_hud(void)
65 if (pl_fbdev_bpp == 16)
66 pl_text_out16(2, pl_fbdev_h - 10, "%s", hud_msg);
69 static void print_fps(void)
71 if (pl_fbdev_bpp == 16)
72 pl_text_out16(2, pl_fbdev_h - 10, "%2d %4.1f", flips_per_sec, vsps_cur);
75 static void print_cpu_usage(void)
77 if (pl_fbdev_bpp == 16)
78 pl_text_out16(pl_fbdev_w - 28, pl_fbdev_h - 10, "%3d", tick_per_sec);
81 void *pl_fbdev_set_mode(int w, int h, int bpp)
85 if (w == pl_fbdev_w && h == pl_fbdev_h && bpp == pl_fbdev_bpp)
92 vout_fbdev_clear(layer_fb);
93 ret = vout_fbdev_resize(layer_fb, w, h, bpp, 0, 0, 0, 0, 3);
95 fprintf(stderr, "failed to set mode\n");
99 menu_notify_mode_change(w, h, bpp);
104 void *pl_fbdev_flip(void)
108 if (pl_fbdev_buf != NULL) {
111 else if (g_opts & OPT_SHOWFPS)
114 if (g_opts & OPT_SHOWCPU)
119 pl_fbdev_buf = vout_fbdev_flip(layer_fb);
123 int pl_fbdev_open(void)
127 pl_fbdev_buf = vout_fbdev_flip(layer_fb);
128 omap_enable_layer(1);
130 // try to align redraws to vsync
131 vout_fbdev_wait_vsync(layer_fb);
132 gettimeofday(&now, 0);
133 vsync_usec_time = now.tv_usec;
134 while (vsync_usec_time >= pl_frame_interval)
135 vsync_usec_time -= pl_frame_interval;
140 void pl_fbdev_close(void)
142 omap_enable_layer(0);
145 void *pl_prepare_screenshot(int *w, int *h, int *bpp)
154 static void update_input(void)
156 int actions[IN_BINDTYPE_COUNT] = { 0, };
157 unsigned int emu_act;
160 if (in_type == PSE_PAD_TYPE_ANALOGPAD)
162 emu_act = actions[IN_BINDTYPE_EMU];
165 for (; !(emu_act & 1); emu_act >>= 1, which++)
169 emu_set_action(emu_act);
171 in_keystate = actions[IN_BINDTYPE_PLAYER12];
174 extern int x11_update_keys(void);
175 in_keystate |= x11_update_keys();
179 #define MAX_LAG_FRAMES 3
181 #define tvdiff(tv, tv_old) \
182 ((tv.tv_sec - tv_old.tv_sec) * 1000000 + tv.tv_usec - tv_old.tv_usec)
183 // assumes us < 1000000
184 #define tvadd(tv, us) { \
186 if (tv.tv_usec >= 1000000) { \
187 tv.tv_usec -= 1000000; \
192 /* called on every vsync */
193 void pl_frame_limit(void)
195 static struct timeval tv_old, tv_expect;
196 static int vsync_cnt_prev;
202 /* doing input here because the pad is polled
203 * thousands of times per frame for some reason */
207 gettimeofday(&now, 0);
209 if (now.tv_sec != tv_old.tv_sec) {
210 diff = tvdiff(now, tv_old);
212 if (0 < diff && diff < 2000000)
213 vsps_cur = 1000000.0f * (vsync_cnt - vsync_cnt_prev) / diff;
214 vsync_cnt_prev = vsync_cnt;
215 flips_per_sec = flip_cnt;
218 if (g_opts & OPT_SHOWCPU)
219 tick_per_sec = get_cpu_ticks();
221 if (hud_new_msg > 0) {
223 if (hud_new_msg == 0)
228 static int ya_vsync_count;
229 if (++ya_vsync_count == PCNT_FRAMES) {
230 pcnt_print(vsps_cur);
235 tvadd(tv_expect, pl_frame_interval);
236 diff = tvdiff(tv_expect, now);
237 if (diff > MAX_LAG_FRAMES * pl_frame_interval || diff < -MAX_LAG_FRAMES * pl_frame_interval) {
238 //printf("pl_frame_limit reset, diff=%d, iv %d\n", diff, pl_frame_interval);
241 // try to align with vsync
242 usadj = vsync_usec_time;
243 while (usadj < tv_expect.tv_usec - pl_frame_interval)
244 usadj += pl_frame_interval;
245 tv_expect.tv_usec = usadj;
248 if (!(g_opts & OPT_NO_FRAMELIM) && diff > pl_frame_interval) {
249 // yay for working usleep on pandora!
250 //printf("usleep %d\n", diff - pl_frame_interval / 2);
251 usleep(diff - pl_frame_interval / 2);
255 if (diff < -pl_frame_interval) {
256 // P.E.Op.S. makes skip decision based on this
258 plugin_skip_advice = 1;
260 else if (diff >= 0) {
262 plugin_skip_advice = 0;
266 pcnt_start(PCNT_ALL);
269 static void pl_text_out16_(int x, int y, const char *text)
271 int i, l, len = strlen(text), w = pl_fbdev_w;
272 unsigned short *screen = (unsigned short *)pl_fbdev_buf + x + y * w;
273 unsigned short val = 0xffff;
275 for (i = 0; i < len; i++, screen += 8)
277 for (l = 0; l < 8; l++)
279 unsigned char fd = fontdata8x8[text[i] * 8 + l];
280 unsigned short *s = screen + l * w;
281 if (fd&0x80) s[0] = val;
282 if (fd&0x40) s[1] = val;
283 if (fd&0x20) s[2] = val;
284 if (fd&0x10) s[3] = val;
285 if (fd&0x08) s[4] = val;
286 if (fd&0x04) s[5] = val;
287 if (fd&0x02) s[6] = val;
288 if (fd&0x01) s[7] = val;
293 void pl_text_out16(int x, int y, const char *texto, ...)
298 va_start(args, texto);
299 vsnprintf(buffer, sizeof(buffer), texto, args);
302 pl_text_out16_(x, y, buffer);
305 static void pl_get_layer_pos(int *x, int *y, int *w, int *h)
313 const struct rearmed_cbs pl_rearmed_cbs = {
323 static void *watchdog_thread(void *unused)
325 int vsync_cnt_old = 0;
330 // don't interfere with debug
342 if (vsync_cnt != vsync_cnt_old) {
343 vsync_cnt_old = vsync_cnt;
352 fprintf(stderr, "watchdog: seen_dead %d\n", seen_dead);
354 fprintf(stderr, "watchdog: lockup detected, aborting\n");
355 // we can't do any cleanup here really, the main thread is
356 // likely touching resources and would crash anyway
362 void pl_start_watchdog(void)
368 pthread_attr_init(&attr);
369 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
371 ret = pthread_create(&tid, &attr, watchdog_thread, NULL);
373 fprintf(stderr, "could not start watchdog: %d\n", ret);