4 * This work is licensed under the terms of the GNU GPLv2 or later.
5 * See the COPYING file in the top-level directory.
13 #include <sys/types.h>
19 #include "plugin_lib.h"
20 #include "linux/fbdev.h"
21 #include "common/fonts.h"
22 #include "common/input.h"
26 #include "../libpcsxcore/new_dynarec/new_dynarec.h"
29 int pl_frame_interval;
31 static int pl_fbdev_w, pl_fbdev_h, pl_fbdev_bpp;
32 static int flip_cnt, vsync_cnt, flips_per_sec, tick_per_sec;
33 static float vsps_cur;
35 static int get_cpu_ticks(void)
37 static unsigned long last_utime;
39 unsigned long utime, ret;
43 fd = open("/proc/self/stat", O_RDONLY);
44 lseek(fd, 0, SEEK_SET);
46 read(fd, buf, sizeof(buf));
47 buf[sizeof(buf) - 1] = 0;
49 sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu", &utime);
50 ret = utime - last_utime;
55 static void print_fps(void)
57 if (pl_fbdev_bpp == 16)
58 pl_text_out16(2, pl_fbdev_h - 10, "%2d %4.1f", flips_per_sec, vsps_cur);
61 static void print_cpu_usage(void)
63 if (pl_fbdev_bpp == 16)
64 pl_text_out16(pl_fbdev_w - 28, pl_fbdev_h - 10, "%3d", tick_per_sec);
67 void *pl_fbdev_set_mode(int w, int h, int bpp)
71 if (w == pl_fbdev_w && h == pl_fbdev_h && bpp == pl_fbdev_bpp)
78 vout_fbdev_clear(layer_fb);
79 ret = vout_fbdev_resize(layer_fb, w, h, bpp, 0, 0, 0, 0, 3);
81 fprintf(stderr, "failed to set mode\n");
85 menu_notify_mode_change(w, h, bpp);
90 void *pl_fbdev_flip(void)
94 if (pl_fbdev_buf != NULL) {
95 if (g_opts & OPT_SHOWFPS)
97 if (g_opts & OPT_SHOWCPU)
102 pl_fbdev_buf = vout_fbdev_flip(layer_fb);
106 int pl_fbdev_open(void)
108 pl_fbdev_buf = vout_fbdev_flip(layer_fb);
109 omap_enable_layer(1);
113 void pl_fbdev_close(void)
115 omap_enable_layer(0);
118 static void update_input(void)
120 int actions[IN_BINDTYPE_COUNT] = { 0, };
123 if (actions[IN_BINDTYPE_EMU] & PEV_MENU)
125 keystate = actions[IN_BINDTYPE_PLAYER12];
128 extern int x11_update_keys(void);
129 keystate |= x11_update_keys();
133 #define MAX_LAG_FRAMES 3
135 #define tvdiff(tv, tv_old) \
136 ((tv.tv_sec - tv_old.tv_sec) * 1000000 + tv.tv_usec - tv_old.tv_usec)
137 // assumes us < 1000000
138 #define tvadd(tv, us) { \
140 if (tv.tv_usec >= 1000000) { \
141 tv.tv_usec -= 1000000; \
146 /* called on every vsync */
147 void pl_frame_limit(void)
149 static struct timeval tv_old, tv_expect;
150 static int vsync_cnt_prev;
156 /* doing input here because the pad is polled
157 * thousands of times per frame for some reason */
161 gettimeofday(&now, 0);
163 if (now.tv_sec != tv_old.tv_sec) {
164 diff = tvdiff(now, tv_old);
166 if (0 < diff && diff < 2000000)
167 vsps_cur = 1000000.0f * (vsync_cnt - vsync_cnt_prev) / diff;
168 vsync_cnt_prev = vsync_cnt;
169 flips_per_sec = flip_cnt;
172 if (g_opts & OPT_SHOWCPU)
173 tick_per_sec = get_cpu_ticks();
176 static int ya_vsync_count;
177 if (++ya_vsync_count == PCNT_FRAMES) {
178 pcnt_print(vsps_cur);
183 if (!(g_opts & OPT_NO_FRAMELIM)) {
184 tvadd(tv_expect, pl_frame_interval);
185 diff = tvdiff(tv_expect, now);
186 if (diff > MAX_LAG_FRAMES * pl_frame_interval || diff < -MAX_LAG_FRAMES * pl_frame_interval) {
187 //printf("pl_frame_limit reset, diff=%d, iv %d\n", diff, pl_frame_interval);
190 else if (diff > pl_frame_interval) {
191 // yay for working usleep on pandora!
192 //printf("usleep %d\n", diff - pl_frame_interval / 2);
193 usleep(diff - pl_frame_interval / 2);
197 pcnt_start(PCNT_ALL);
200 static void pl_text_out16_(int x, int y, const char *text)
202 int i, l, len = strlen(text), w = pl_fbdev_w;
203 unsigned short *screen = (unsigned short *)pl_fbdev_buf + x + y * w;
204 unsigned short val = 0xffff;
206 for (i = 0; i < len; i++, screen += 8)
208 for (l = 0; l < 8; l++)
210 unsigned char fd = fontdata8x8[text[i] * 8 + l];
211 unsigned short *s = screen + l * w;
212 if (fd&0x80) s[0] = val;
213 if (fd&0x40) s[1] = val;
214 if (fd&0x20) s[2] = val;
215 if (fd&0x10) s[3] = val;
216 if (fd&0x08) s[4] = val;
217 if (fd&0x04) s[5] = val;
218 if (fd&0x02) s[6] = val;
219 if (fd&0x01) s[7] = val;
224 void pl_text_out16(int x, int y, const char *texto, ...)
229 va_start(args, texto);
230 vsnprintf(buffer, sizeof(buffer), texto, args);
233 pl_text_out16_(x, y, buffer);
236 static void pl_get_layer_pos(int *x, int *y, int *w, int *h)
244 extern int UseFrameSkip; // hmh
246 const struct rearmed_cbs pl_rearmed_cbs = {
256 static void *watchdog_thread(void *unused)
258 int vsync_cnt_old = 0;
271 if (vsync_cnt != vsync_cnt_old) {
272 vsync_cnt_old = vsync_cnt;
281 fprintf(stderr, "watchdog: seen_dead %d\n", seen_dead);
283 fprintf(stderr, "watchdog: lockup detected, aborting\n");
284 // we can't do any cleanup here really, the main thread is
285 // likely touching resources and would crash anyway
291 void pl_start_watchdog(void)
297 pthread_attr_init(&attr);
298 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
300 ret = pthread_create(&tid, &attr, watchdog_thread, NULL);
302 fprintf(stderr, "could not start watchdog: %d\n", ret);