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 vsync_usec_time;
39 static int get_cpu_ticks(void)
41 static unsigned long last_utime;
43 unsigned long utime, ret;
47 fd = open("/proc/self/stat", O_RDONLY);
48 lseek(fd, 0, SEEK_SET);
50 read(fd, buf, sizeof(buf));
51 buf[sizeof(buf) - 1] = 0;
53 sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu", &utime);
54 ret = utime - last_utime;
59 static void print_hud(void)
61 if (pl_fbdev_bpp == 16)
62 pl_text_out16(2, pl_fbdev_h - 10, "%s", hud_msg);
65 static void print_fps(void)
67 if (pl_fbdev_bpp == 16)
68 pl_text_out16(2, pl_fbdev_h - 10, "%2d %4.1f", flips_per_sec, vsps_cur);
71 static void print_cpu_usage(void)
73 if (pl_fbdev_bpp == 16)
74 pl_text_out16(pl_fbdev_w - 28, pl_fbdev_h - 10, "%3d", tick_per_sec);
77 void *pl_fbdev_set_mode(int w, int h, int bpp)
81 if (w == pl_fbdev_w && h == pl_fbdev_h && bpp == pl_fbdev_bpp)
88 vout_fbdev_clear(layer_fb);
89 ret = vout_fbdev_resize(layer_fb, w, h, bpp, 0, 0, 0, 0, 3);
91 fprintf(stderr, "failed to set mode\n");
95 menu_notify_mode_change(w, h, bpp);
100 void *pl_fbdev_flip(void)
104 if (pl_fbdev_buf != NULL) {
107 else if (g_opts & OPT_SHOWFPS)
110 if (g_opts & OPT_SHOWCPU)
115 pl_fbdev_buf = vout_fbdev_flip(layer_fb);
119 int pl_fbdev_open(void)
123 pl_fbdev_buf = vout_fbdev_flip(layer_fb);
124 omap_enable_layer(1);
126 // try to align redraws to vsync
127 vout_fbdev_wait_vsync(layer_fb);
128 gettimeofday(&now, 0);
129 vsync_usec_time = now.tv_usec;
130 while (vsync_usec_time >= pl_frame_interval)
131 vsync_usec_time -= pl_frame_interval;
136 void pl_fbdev_close(void)
138 omap_enable_layer(0);
141 void *pl_prepare_screenshot(int *w, int *h, int *bpp)
150 static void update_input(void)
152 int actions[IN_BINDTYPE_COUNT] = { 0, };
153 unsigned int emu_act;
156 if (in_type == PSE_PAD_TYPE_ANALOGPAD)
158 emu_act = actions[IN_BINDTYPE_EMU];
161 for (; !(emu_act & 1); emu_act >>= 1, which++)
165 emu_set_action(emu_act);
167 in_keystate = actions[IN_BINDTYPE_PLAYER12];
170 extern int x11_update_keys(unsigned int *action);
171 in_keystate |= x11_update_keys(&emu_act);
172 emu_set_action(emu_act);
176 #define MAX_LAG_FRAMES 3
178 #define tvdiff(tv, tv_old) \
179 ((tv.tv_sec - tv_old.tv_sec) * 1000000 + tv.tv_usec - tv_old.tv_usec)
180 // assumes us < 1000000
181 #define tvadd(tv, us) { \
183 if (tv.tv_usec >= 1000000) { \
184 tv.tv_usec -= 1000000; \
189 /* called on every vsync */
190 void pl_frame_limit(void)
192 static struct timeval tv_old, tv_expect;
193 static int vsync_cnt_prev;
199 /* doing input here because the pad is polled
200 * thousands of times per frame for some reason */
204 gettimeofday(&now, 0);
206 if (now.tv_sec != tv_old.tv_sec) {
207 diff = tvdiff(now, tv_old);
209 if (0 < diff && diff < 2000000)
210 vsps_cur = 1000000.0f * (vsync_cnt - vsync_cnt_prev) / diff;
211 vsync_cnt_prev = vsync_cnt;
212 flips_per_sec = flip_cnt;
215 if (g_opts & OPT_SHOWCPU)
216 tick_per_sec = get_cpu_ticks();
218 if (hud_new_msg > 0) {
220 if (hud_new_msg == 0)
225 static int ya_vsync_count;
226 if (++ya_vsync_count == PCNT_FRAMES) {
227 pcnt_print(vsps_cur);
232 tvadd(tv_expect, pl_frame_interval);
233 diff = tvdiff(tv_expect, now);
234 if (diff > MAX_LAG_FRAMES * pl_frame_interval || diff < -MAX_LAG_FRAMES * pl_frame_interval) {
235 //printf("pl_frame_limit reset, diff=%d, iv %d\n", diff, pl_frame_interval);
238 // try to align with vsync
239 usadj = vsync_usec_time;
240 while (usadj < tv_expect.tv_usec - pl_frame_interval)
241 usadj += pl_frame_interval;
242 tv_expect.tv_usec = usadj;
245 if (!(g_opts & OPT_NO_FRAMELIM) && diff > pl_frame_interval) {
246 // yay for working usleep on pandora!
247 //printf("usleep %d\n", diff - pl_frame_interval / 2);
248 usleep(diff - pl_frame_interval / 2);
251 if (pl_rearmed_cbs.frameskip) {
252 if (diff < -pl_frame_interval)
253 pl_rearmed_cbs.fskip_advice = 1;
255 pl_rearmed_cbs.fskip_advice = 0;
258 pcnt_start(PCNT_ALL);
261 static void pl_text_out16_(int x, int y, const char *text)
263 int i, l, len = strlen(text), w = pl_fbdev_w;
264 unsigned short *screen = (unsigned short *)pl_fbdev_buf + x + y * w;
265 unsigned short val = 0xffff;
267 for (i = 0; i < len; i++, screen += 8)
269 for (l = 0; l < 8; l++)
271 unsigned char fd = fontdata8x8[text[i] * 8 + l];
272 unsigned short *s = screen + l * w;
273 if (fd&0x80) s[0] = val;
274 if (fd&0x40) s[1] = val;
275 if (fd&0x20) s[2] = val;
276 if (fd&0x10) s[3] = val;
277 if (fd&0x08) s[4] = val;
278 if (fd&0x04) s[5] = val;
279 if (fd&0x02) s[6] = val;
280 if (fd&0x01) s[7] = val;
285 void pl_text_out16(int x, int y, const char *texto, ...)
290 va_start(args, texto);
291 vsnprintf(buffer, sizeof(buffer), texto, args);
294 pl_text_out16_(x, y, buffer);
297 static void pl_get_layer_pos(int *x, int *y, int *w, int *h)
305 struct rearmed_cbs pl_rearmed_cbs = {
314 static void *watchdog_thread(void *unused)
316 int vsync_cnt_old = 0;
321 // don't interfere with debug
333 if (vsync_cnt != vsync_cnt_old) {
334 vsync_cnt_old = vsync_cnt;
343 fprintf(stderr, "watchdog: seen_dead %d\n", seen_dead);
345 fprintf(stderr, "watchdog: lockup detected, aborting\n");
346 // we can't do any cleanup here really, the main thread is
347 // likely touching resources and would crash anyway
353 void pl_start_watchdog(void)
359 pthread_attr_init(&attr);
360 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
362 ret = pthread_create(&tid, &attr, watchdog_thread, NULL);
364 fprintf(stderr, "could not start watchdog: %d\n", ret);