frontend: track output and psx sizes separately
[pcsx_rearmed.git] / frontend / plugin_lib.c
... / ...
CommitLineData
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 "menu.h"
25#include "main.h"
26#include "plat.h"
27#include "pcnt.h"
28#include "pl_gun_ts.h"
29#include "../libpcsxcore/new_dynarec/new_dynarec.h"
30#include "../libpcsxcore/psemu_plugin_defs.h"
31
32int in_type1, in_type2;
33int in_a1[2] = { 127, 127 }, in_a2[2] = { 127, 127 };
34int in_adev[2] = { -1, -1 }, in_adev_axis[2][2] = {{ 0, 1 }, { 0, 1 }};
35int in_keystate, in_state_gun;
36int in_enable_vibration;
37void *tsdev;
38void *pl_vout_buf;
39int g_layer_x, g_layer_y, g_layer_w, g_layer_h;
40static int pl_vout_w, pl_vout_h, pl_vout_bpp; /* output display/layer */
41static int psx_w, psx_h, psx_bpp;
42static int vsync_cnt;
43static int is_pal, frame_interval, frame_interval1024;
44static int vsync_usec_time;
45
46
47static __attribute__((noinline)) int get_cpu_ticks(void)
48{
49 static unsigned long last_utime;
50 static int fd;
51 unsigned long utime, ret;
52 char buf[128];
53
54 if (fd == 0)
55 fd = open("/proc/self/stat", O_RDONLY);
56 lseek(fd, 0, SEEK_SET);
57 buf[0] = 0;
58 read(fd, buf, sizeof(buf));
59 buf[sizeof(buf) - 1] = 0;
60
61 sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu", &utime);
62 ret = utime - last_utime;
63 last_utime = utime;
64 return ret;
65}
66
67static void print_msg(int h, int border)
68{
69 if (pl_vout_bpp == 16)
70 pl_text_out16(border + 2, h - 10, "%s", hud_msg);
71}
72
73static void print_fps(int h, int border)
74{
75 if (pl_vout_bpp == 16)
76 pl_text_out16(border + 2, h - 10, "%2d %4.1f",
77 pl_rearmed_cbs.flips_per_sec, pl_rearmed_cbs.vsps_cur);
78}
79
80static void print_cpu_usage(int w, int h, int border)
81{
82 if (pl_vout_bpp == 16)
83 pl_text_out16(w - border - 28, h - 10, "%3d", pl_rearmed_cbs.cpu_usage);
84}
85
86// draw 192x8 status of 24 sound channels
87static __attribute__((noinline)) void draw_active_chans(int vout_w, int vout_h)
88{
89 extern void spu_get_debug_info(int *chans_out, int *run_chans,
90 int *fmod_chans_out, int *noise_chans_out); // hack
91 int live_chans, run_chans, fmod_chans, noise_chans;
92
93 static const unsigned short colors[2] = { 0x1fe3, 0x0700 };
94 unsigned short *dest = (unsigned short *)pl_vout_buf +
95 vout_w * (vout_h - 10) + vout_w / 2 - 192/2;
96 unsigned short *d, p;
97 int c, x, y;
98
99 if (pl_vout_bpp != 16)
100 return;
101
102 spu_get_debug_info(&live_chans, &run_chans, &fmod_chans, &noise_chans);
103
104 for (c = 0; c < 24; c++) {
105 d = dest + c * 8;
106 p = !(live_chans & (1<<c)) ? (run_chans & (1<<c) ? 0x01c0 : 0) :
107 (fmod_chans & (1<<c)) ? 0xf000 :
108 (noise_chans & (1<<c)) ? 0x001f :
109 colors[c & 1];
110 for (y = 0; y < 8; y++, d += vout_w)
111 for (x = 0; x < 8; x++)
112 d[x] = p;
113 }
114}
115
116void pl_print_hud(int xborder)
117{
118 int w = pl_vout_w, h = pl_vout_h;
119
120 if (h < 16)
121 return;
122
123 if (g_opts & OPT_SHOWSPU)
124 draw_active_chans(w, h);
125
126 if (hud_msg[0] != 0)
127 print_msg(h, xborder);
128 else if (g_opts & OPT_SHOWFPS)
129 print_fps(h, xborder);
130
131 if (g_opts & OPT_SHOWCPU)
132 print_cpu_usage(w, h, xborder);
133}
134
135static void *pl_vout_set_mode(int w, int h, int bpp)
136{
137 // special h handling, Wipeout likes to change it by 1-6
138 static int vsync_cnt_ms_prev;
139 if ((unsigned int)(vsync_cnt - vsync_cnt_ms_prev) < 5*60)
140 h = (h + 7) & ~7;
141 vsync_cnt_ms_prev = vsync_cnt;
142
143 if (w == psx_w && h == psx_h && bpp == psx_bpp)
144 return pl_vout_buf;
145
146 pl_vout_w = psx_w = w;
147 pl_vout_h = psx_h = h;
148 pl_vout_bpp = psx_bpp = bpp;
149
150 pl_vout_buf = plat_gvideo_set_mode(&pl_vout_w, &pl_vout_h, &pl_vout_bpp);
151 if (pl_vout_buf == NULL && pl_rearmed_cbs.pl_vout_raw_flip == NULL)
152 fprintf(stderr, "failed to set mode %dx%d@%d\n",
153 psx_w, psx_h, psx_bpp);
154
155 menu_notify_mode_change(pl_vout_w, pl_vout_h, pl_vout_bpp);
156
157 return pl_vout_buf;
158}
159
160// only used if raw flip is not defined
161static void *pl_vout_flip(void)
162{
163 pl_rearmed_cbs.flip_cnt++;
164
165 if (pl_vout_buf != NULL)
166 pl_print_hud(0);
167
168 // let's flip now
169 pl_vout_buf = plat_gvideo_flip();
170 return pl_vout_buf;
171}
172
173static int pl_vout_open(void)
174{
175 struct timeval now;
176 int h;
177
178 // force mode update
179 h = psx_h;
180 psx_h--;
181 pl_vout_buf = pl_vout_set_mode(psx_w, h, psx_bpp);
182
183 plat_gvideo_open(is_pal);
184
185 gettimeofday(&now, 0);
186 vsync_usec_time = now.tv_usec;
187 while (vsync_usec_time >= frame_interval)
188 vsync_usec_time -= frame_interval;
189
190 return 0;
191}
192
193static void pl_vout_close(void)
194{
195 plat_gvideo_close();
196}
197
198void *pl_prepare_screenshot(int *w, int *h, int *bpp)
199{
200 void *ret = plat_prepare_screenshot(w, h, bpp);
201 if (ret != NULL)
202 return ret;
203
204 *w = pl_vout_w;
205 *h = pl_vout_h;
206 *bpp = pl_vout_bpp;
207
208 return pl_vout_buf;
209}
210
211#ifndef MAEMO
212static void update_analogs(void)
213{
214 int *nubp[2] = { in_a1, in_a2 };
215 int i, a, v, ret;
216
217 for (i = 0; i < 2; i++)
218 {
219 if (in_adev[i] < 0)
220 continue;
221
222 for (a = 0; a < 2; a++) {
223 nubp[i][a] = 127;
224
225 ret = in_update_analog(in_adev[i], in_adev_axis[i][a], &v);
226 if (ret == 0) {
227 v = v / (IN_ABS_RANGE / 128) + 127;
228 nubp[i][a] = v < 0 ? 0 : v;
229 }
230 }
231 }
232 //printf("%4d %4d %4d %4d\n", in_a1[0], in_a1[1], in_a2[0], in_a2[1]);
233}
234
235static void update_input(void)
236{
237 int actions[IN_BINDTYPE_COUNT] = { 0, };
238 unsigned int emu_act;
239
240 in_update(actions);
241 if (in_type1 == PSE_PAD_TYPE_ANALOGPAD)
242 update_analogs();
243 emu_act = actions[IN_BINDTYPE_EMU];
244 in_state_gun = (emu_act & SACTION_GUN_MASK) >> SACTION_GUN_TRIGGER;
245
246 emu_act &= ~SACTION_GUN_MASK;
247 if (emu_act) {
248 int which = 0;
249 for (; !(emu_act & 1); emu_act >>= 1, which++)
250 ;
251 emu_act = which;
252 }
253 emu_set_action(emu_act);
254
255 in_keystate = actions[IN_BINDTYPE_PLAYER12];
256#ifdef X11
257 extern int x11_update_keys(unsigned int *action);
258 in_keystate |= x11_update_keys(&emu_act);
259 emu_set_action(emu_act);
260#endif
261}
262#else /* MAEMO */
263static void update_input(void)
264{
265}
266#endif
267
268void pl_update_gun(int *xn, int *xres, int *y, int *in)
269{
270 if (tsdev)
271 pl_gun_ts_update(tsdev, xn, y, in);
272
273 *xres = pl_vout_w;
274 *y = *y * pl_vout_h >> 10;
275}
276
277#define MAX_LAG_FRAMES 3
278
279#define tvdiff(tv, tv_old) \
280 ((tv.tv_sec - tv_old.tv_sec) * 1000000 + tv.tv_usec - tv_old.tv_usec)
281
282/* called on every vsync */
283void pl_frame_limit(void)
284{
285 static struct timeval tv_old, tv_expect;
286 static int vsync_cnt_prev, drc_active_vsyncs;
287 struct timeval now;
288 int diff, usadj;
289
290 vsync_cnt++;
291
292 /* doing input here because the pad is polled
293 * thousands of times per frame for some reason */
294 update_input();
295
296 pcnt_end(PCNT_ALL);
297 gettimeofday(&now, 0);
298
299 if (now.tv_sec != tv_old.tv_sec) {
300 diff = tvdiff(now, tv_old);
301 pl_rearmed_cbs.vsps_cur = 0.0f;
302 if (0 < diff && diff < 2000000)
303 pl_rearmed_cbs.vsps_cur = 1000000.0f * (vsync_cnt - vsync_cnt_prev) / diff;
304 vsync_cnt_prev = vsync_cnt;
305
306 if (g_opts & OPT_SHOWFPS)
307 pl_rearmed_cbs.flips_per_sec = pl_rearmed_cbs.flip_cnt;
308 pl_rearmed_cbs.flip_cnt = 0;
309 if (g_opts & OPT_SHOWCPU)
310 pl_rearmed_cbs.cpu_usage = get_cpu_ticks();
311
312 if (hud_new_msg > 0) {
313 hud_new_msg--;
314 if (hud_new_msg == 0)
315 hud_msg[0] = 0;
316 }
317 tv_old = now;
318 }
319#ifdef PCNT
320 static int ya_vsync_count;
321 if (++ya_vsync_count == PCNT_FRAMES) {
322 pcnt_print(pl_rearmed_cbs.vsps_cur);
323 ya_vsync_count = 0;
324 }
325#endif
326
327 // tv_expect uses usec*1024 units instead of usecs for better accuracy
328 tv_expect.tv_usec += frame_interval1024;
329 if (tv_expect.tv_usec >= (1000000 << 10)) {
330 tv_expect.tv_usec -= (1000000 << 10);
331 tv_expect.tv_sec++;
332 }
333 diff = (tv_expect.tv_sec - now.tv_sec) * 1000000 + (tv_expect.tv_usec >> 10) - now.tv_usec;
334
335 if (diff > MAX_LAG_FRAMES * frame_interval || diff < -MAX_LAG_FRAMES * frame_interval) {
336 //printf("pl_frame_limit reset, diff=%d, iv %d\n", diff, frame_interval);
337 tv_expect = now;
338 diff = 0;
339 // try to align with vsync
340 usadj = vsync_usec_time;
341 while (usadj < tv_expect.tv_usec - frame_interval)
342 usadj += frame_interval;
343 tv_expect.tv_usec = usadj << 10;
344 }
345
346 if (!(g_opts & OPT_NO_FRAMELIM) && diff > frame_interval) {
347 // yay for working usleep on pandora!
348 //printf("usleep %d\n", diff - frame_interval / 2);
349 usleep(diff - frame_interval);
350 }
351
352 if (pl_rearmed_cbs.frameskip) {
353 if (diff < -frame_interval)
354 pl_rearmed_cbs.fskip_advice = 1;
355 else if (diff >= 0)
356 pl_rearmed_cbs.fskip_advice = 0;
357
358 // recompilation is not that fast and may cause frame skip on
359 // loading screens and such, resulting in flicker or glitches
360 if (new_dynarec_did_compile) {
361 if (drc_active_vsyncs < 32)
362 pl_rearmed_cbs.fskip_advice = 0;
363 drc_active_vsyncs++;
364 }
365 else
366 drc_active_vsyncs = 0;
367 new_dynarec_did_compile = 0;
368 }
369
370 pcnt_start(PCNT_ALL);
371}
372
373void pl_timing_prepare(int is_pal_)
374{
375 pl_rearmed_cbs.fskip_advice = 0;
376 pl_rearmed_cbs.flips_per_sec = 0;
377 pl_rearmed_cbs.cpu_usage = 0;
378
379 is_pal = is_pal_;
380 frame_interval = is_pal ? 20000 : 16667;
381 frame_interval1024 = is_pal ? 20000*1024 : 17066667;
382
383 // used by P.E.Op.S. frameskip code
384 pl_rearmed_cbs.gpu_peops.fFrameRateHz = is_pal ? 50.0f : 59.94f;
385 pl_rearmed_cbs.gpu_peops.dwFrameRateTicks =
386 (100000*100 / (unsigned long)(pl_rearmed_cbs.gpu_peops.fFrameRateHz*100));
387}
388
389static void pl_text_out16_(int x, int y, const char *text)
390{
391 int i, l, len = strlen(text), w = pl_vout_w;
392 unsigned short *screen = (unsigned short *)pl_vout_buf + x + y * w;
393 unsigned short val = 0xffff;
394
395 for (i = 0; i < len; i++, screen += 8)
396 {
397 for (l = 0; l < 8; l++)
398 {
399 unsigned char fd = fontdata8x8[text[i] * 8 + l];
400 unsigned short *s = screen + l * w;
401 if (fd&0x80) s[0] = val;
402 if (fd&0x40) s[1] = val;
403 if (fd&0x20) s[2] = val;
404 if (fd&0x10) s[3] = val;
405 if (fd&0x08) s[4] = val;
406 if (fd&0x04) s[5] = val;
407 if (fd&0x02) s[6] = val;
408 if (fd&0x01) s[7] = val;
409 }
410 }
411}
412
413void pl_text_out16(int x, int y, const char *texto, ...)
414{
415 va_list args;
416 char buffer[256];
417
418 va_start(args, texto);
419 vsnprintf(buffer, sizeof(buffer), texto, args);
420 va_end(args);
421
422 pl_text_out16_(x, y, buffer);
423}
424
425static void pl_get_layer_pos(int *x, int *y, int *w, int *h)
426{
427 *x = g_layer_x;
428 *y = g_layer_y;
429 *w = g_layer_w;
430 *h = g_layer_h;
431}
432
433struct rearmed_cbs pl_rearmed_cbs = {
434 pl_get_layer_pos,
435 pl_vout_open,
436 pl_vout_set_mode,
437 pl_vout_flip,
438 pl_vout_close,
439};
440
441/* watchdog */
442static void *watchdog_thread(void *unused)
443{
444 int vsync_cnt_old = 0;
445 int seen_dead = 0;
446 int sleep_time = 5;
447
448#if !defined(NDEBUG) || defined(DRC_DBG)
449 // don't interfere with debug
450 return NULL;
451#endif
452 while (1)
453 {
454 sleep(sleep_time);
455
456 if (stop) {
457 seen_dead = 0;
458 sleep_time = 5;
459 continue;
460 }
461 if (vsync_cnt != vsync_cnt_old) {
462 vsync_cnt_old = vsync_cnt;
463 seen_dead = 0;
464 sleep_time = 2;
465 continue;
466 }
467
468 seen_dead++;
469 sleep_time = 1;
470 if (seen_dead > 1)
471 fprintf(stderr, "watchdog: seen_dead %d\n", seen_dead);
472 if (seen_dead > 4) {
473 fprintf(stderr, "watchdog: lockup detected, aborting\n");
474 // we can't do any cleanup here really, the main thread is
475 // likely touching resources and would crash anyway
476 abort();
477 }
478 }
479}
480
481void pl_start_watchdog(void)
482{
483 pthread_attr_t attr;
484 pthread_t tid;
485 int ret;
486
487 pthread_attr_init(&attr);
488 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
489
490 ret = pthread_create(&tid, &attr, watchdog_thread, NULL);
491 if (ret != 0)
492 fprintf(stderr, "could not start watchdog: %d\n", ret);
493}
494
495void pl_init(void)
496{
497 extern unsigned int hSyncCount; // from psxcounters
498 extern unsigned int frame_counter;
499
500 psx_w = psx_h = pl_vout_w = pl_vout_h = 256;
501 psx_bpp = pl_vout_bpp = 16;
502
503 tsdev = pl_gun_ts_init();
504
505 pl_rearmed_cbs.gpu_hcnt = &hSyncCount;
506 pl_rearmed_cbs.gpu_frame_count = &frame_counter;
507}