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