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