| 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> |
| 10 | #include <string.h> |
| 11 | #include <stdarg.h> |
| 12 | #include <sys/time.h> |
| 13 | #include <sys/types.h> |
| 14 | #include <sys/stat.h> |
| 15 | #include <fcntl.h> |
| 16 | #include <unistd.h> |
| 17 | #include <pthread.h> |
| 18 | |
| 19 | #include "plugin_lib.h" |
| 20 | #include "linux/fbdev.h" |
| 21 | #include "common/fonts.h" |
| 22 | #include "common/input.h" |
| 23 | #include "omap.h" |
| 24 | #include "menu.h" |
| 25 | #include "pcnt.h" |
| 26 | #include "../libpcsxcore/new_dynarec/new_dynarec.h" |
| 27 | |
| 28 | void *pl_fbdev_buf; |
| 29 | int pl_frame_interval; |
| 30 | int keystate; |
| 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; |
| 34 | |
| 35 | static int get_cpu_ticks(void) |
| 36 | { |
| 37 | static unsigned long last_utime; |
| 38 | static int fd; |
| 39 | unsigned long utime, ret; |
| 40 | char buf[128]; |
| 41 | |
| 42 | if (fd == 0) |
| 43 | fd = open("/proc/self/stat", O_RDONLY); |
| 44 | lseek(fd, 0, SEEK_SET); |
| 45 | buf[0] = 0; |
| 46 | read(fd, buf, sizeof(buf)); |
| 47 | buf[sizeof(buf) - 1] = 0; |
| 48 | |
| 49 | sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu", &utime); |
| 50 | ret = utime - last_utime; |
| 51 | last_utime = utime; |
| 52 | return ret; |
| 53 | } |
| 54 | |
| 55 | static void print_fps(void) |
| 56 | { |
| 57 | if (pl_fbdev_bpp == 16) |
| 58 | pl_text_out16(2, pl_fbdev_h - 10, "%2d %4.1f", flips_per_sec, vsps_cur); |
| 59 | } |
| 60 | |
| 61 | static void print_cpu_usage(void) |
| 62 | { |
| 63 | if (pl_fbdev_bpp == 16) |
| 64 | pl_text_out16(pl_fbdev_w - 28, pl_fbdev_h - 10, "%3d", tick_per_sec); |
| 65 | } |
| 66 | |
| 67 | void *pl_fbdev_set_mode(int w, int h, int bpp) |
| 68 | { |
| 69 | void *ret; |
| 70 | |
| 71 | if (w == pl_fbdev_w && h == pl_fbdev_h && bpp == pl_fbdev_bpp) |
| 72 | return pl_fbdev_buf; |
| 73 | |
| 74 | pl_fbdev_w = w; |
| 75 | pl_fbdev_h = h; |
| 76 | pl_fbdev_bpp = bpp; |
| 77 | |
| 78 | vout_fbdev_clear(layer_fb); |
| 79 | ret = vout_fbdev_resize(layer_fb, w, h, bpp, 0, 0, 0, 0, 3); |
| 80 | if (ret == NULL) |
| 81 | fprintf(stderr, "failed to set mode\n"); |
| 82 | else |
| 83 | pl_fbdev_buf = ret; |
| 84 | |
| 85 | menu_notify_mode_change(w, h, bpp); |
| 86 | |
| 87 | return pl_fbdev_buf; |
| 88 | } |
| 89 | |
| 90 | void *pl_fbdev_flip(void) |
| 91 | { |
| 92 | flip_cnt++; |
| 93 | |
| 94 | if (pl_fbdev_buf != NULL) { |
| 95 | if (g_opts & OPT_SHOWFPS) |
| 96 | print_fps(); |
| 97 | if (g_opts & OPT_SHOWCPU) |
| 98 | print_cpu_usage(); |
| 99 | } |
| 100 | |
| 101 | // let's flip now |
| 102 | pl_fbdev_buf = vout_fbdev_flip(layer_fb); |
| 103 | return pl_fbdev_buf; |
| 104 | } |
| 105 | |
| 106 | int pl_fbdev_open(void) |
| 107 | { |
| 108 | pl_fbdev_buf = vout_fbdev_flip(layer_fb); |
| 109 | omap_enable_layer(1); |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | void pl_fbdev_close(void) |
| 114 | { |
| 115 | omap_enable_layer(0); |
| 116 | } |
| 117 | |
| 118 | static void update_input(void) |
| 119 | { |
| 120 | int actions[IN_BINDTYPE_COUNT] = { 0, }; |
| 121 | |
| 122 | in_update(actions); |
| 123 | if (actions[IN_BINDTYPE_EMU] & PEV_MENU) |
| 124 | stop = 1; |
| 125 | keystate = actions[IN_BINDTYPE_PLAYER12]; |
| 126 | |
| 127 | #ifdef X11 |
| 128 | extern int x11_update_keys(void); |
| 129 | keystate |= x11_update_keys(); |
| 130 | #endif |
| 131 | } |
| 132 | |
| 133 | #define MAX_LAG_FRAMES 3 |
| 134 | |
| 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) { \ |
| 139 | tv.tv_usec += us; \ |
| 140 | if (tv.tv_usec >= 1000000) { \ |
| 141 | tv.tv_usec -= 1000000; \ |
| 142 | tv.tv_sec++; \ |
| 143 | } \ |
| 144 | } |
| 145 | |
| 146 | /* called on every vsync */ |
| 147 | void pl_frame_limit(void) |
| 148 | { |
| 149 | static struct timeval tv_old, tv_expect; |
| 150 | static int vsync_cnt_prev; |
| 151 | struct timeval now; |
| 152 | int diff; |
| 153 | |
| 154 | vsync_cnt++; |
| 155 | |
| 156 | /* doing input here because the pad is polled |
| 157 | * thousands of times per frame for some reason */ |
| 158 | update_input(); |
| 159 | |
| 160 | pcnt_end(PCNT_ALL); |
| 161 | gettimeofday(&now, 0); |
| 162 | |
| 163 | if (now.tv_sec != tv_old.tv_sec) { |
| 164 | diff = tvdiff(now, tv_old); |
| 165 | vsps_cur = 0.0f; |
| 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; |
| 170 | flip_cnt = 0; |
| 171 | tv_old = now; |
| 172 | if (g_opts & OPT_SHOWCPU) |
| 173 | tick_per_sec = get_cpu_ticks(); |
| 174 | } |
| 175 | #ifdef PCNT |
| 176 | static int ya_vsync_count; |
| 177 | if (++ya_vsync_count == PCNT_FRAMES) { |
| 178 | pcnt_print(vsps_cur); |
| 179 | ya_vsync_count = 0; |
| 180 | } |
| 181 | #endif |
| 182 | |
| 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); |
| 188 | tv_expect = now; |
| 189 | } |
| 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); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | pcnt_start(PCNT_ALL); |
| 198 | } |
| 199 | |
| 200 | static void pl_text_out16_(int x, int y, const char *text) |
| 201 | { |
| 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; |
| 205 | |
| 206 | for (i = 0; i < len; i++, screen += 8) |
| 207 | { |
| 208 | for (l = 0; l < 8; l++) |
| 209 | { |
| 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; |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | void pl_text_out16(int x, int y, const char *texto, ...) |
| 225 | { |
| 226 | va_list args; |
| 227 | char buffer[256]; |
| 228 | |
| 229 | va_start(args, texto); |
| 230 | vsnprintf(buffer, sizeof(buffer), texto, args); |
| 231 | va_end(args); |
| 232 | |
| 233 | pl_text_out16_(x, y, buffer); |
| 234 | } |
| 235 | |
| 236 | static void pl_get_layer_pos(int *x, int *y, int *w, int *h) |
| 237 | { |
| 238 | *x = g_layer_x; |
| 239 | *y = g_layer_y; |
| 240 | *w = g_layer_w; |
| 241 | *h = g_layer_h; |
| 242 | } |
| 243 | |
| 244 | extern int UseFrameSkip; // hmh |
| 245 | |
| 246 | const struct rearmed_cbs pl_rearmed_cbs = { |
| 247 | pl_get_layer_pos, |
| 248 | pl_fbdev_open, |
| 249 | pl_fbdev_set_mode, |
| 250 | pl_fbdev_flip, |
| 251 | pl_fbdev_close, |
| 252 | &UseFrameSkip, |
| 253 | }; |
| 254 | |
| 255 | /* watchdog */ |
| 256 | static void *watchdog_thread(void *unused) |
| 257 | { |
| 258 | int vsync_cnt_old = 0; |
| 259 | int seen_dead = 0; |
| 260 | int sleep_time = 5; |
| 261 | |
| 262 | while (1) |
| 263 | { |
| 264 | sleep(sleep_time); |
| 265 | |
| 266 | if (stop) { |
| 267 | seen_dead = 0; |
| 268 | sleep_time = 5; |
| 269 | continue; |
| 270 | } |
| 271 | if (vsync_cnt != vsync_cnt_old) { |
| 272 | vsync_cnt_old = vsync_cnt; |
| 273 | seen_dead = 0; |
| 274 | sleep_time = 2; |
| 275 | continue; |
| 276 | } |
| 277 | |
| 278 | seen_dead++; |
| 279 | sleep_time = 1; |
| 280 | if (seen_dead > 1) |
| 281 | fprintf(stderr, "watchdog: seen_dead %d\n", seen_dead); |
| 282 | if (seen_dead > 4) { |
| 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 |
| 286 | abort(); |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | void pl_start_watchdog(void) |
| 292 | { |
| 293 | pthread_attr_t attr; |
| 294 | pthread_t tid; |
| 295 | int ret; |
| 296 | |
| 297 | pthread_attr_init(&attr); |
| 298 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 299 | |
| 300 | ret = pthread_create(&tid, &attr, watchdog_thread, NULL); |
| 301 | if (ret != 0) |
| 302 | fprintf(stderr, "could not start watchdog: %d\n", ret); |
| 303 | } |
| 304 | |