drc: update memhandler according to pcsxr code
[pcsx_rearmed.git] / frontend / plugin_lib.c
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 // P.E.Op.S.
35 extern int UseFrameSkip;
36 extern float fps_skip;
37
38 static int get_cpu_ticks(void)
39 {
40         static unsigned long last_utime;
41         static int fd;
42         unsigned long utime, ret;
43         char buf[128];
44
45         if (fd == 0)
46                 fd = open("/proc/self/stat", O_RDONLY);
47         lseek(fd, 0, SEEK_SET);
48         buf[0] = 0;
49         read(fd, buf, sizeof(buf));
50         buf[sizeof(buf) - 1] = 0;
51
52         sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu", &utime);
53         ret = utime - last_utime;
54         last_utime = utime;
55         return ret;
56 }
57
58 static void print_fps(void)
59 {
60         if (pl_fbdev_bpp == 16)
61                 pl_text_out16(2, pl_fbdev_h - 10, "%2d %4.1f", flips_per_sec, vsps_cur);
62 }
63
64 static void print_cpu_usage(void)
65 {
66         if (pl_fbdev_bpp == 16)
67                 pl_text_out16(pl_fbdev_w - 28, pl_fbdev_h - 10, "%3d", tick_per_sec);
68 }
69
70 void *pl_fbdev_set_mode(int w, int h, int bpp)
71 {
72         void *ret;
73
74         if (w == pl_fbdev_w && h == pl_fbdev_h && bpp == pl_fbdev_bpp)
75                 return pl_fbdev_buf;
76
77         pl_fbdev_w = w;
78         pl_fbdev_h = h;
79         pl_fbdev_bpp = bpp;
80
81         vout_fbdev_clear(layer_fb);
82         ret = vout_fbdev_resize(layer_fb, w, h, bpp, 0, 0, 0, 0, 3);
83         if (ret == NULL)
84                 fprintf(stderr, "failed to set mode\n");
85         else
86                 pl_fbdev_buf = ret;
87
88         menu_notify_mode_change(w, h, bpp);
89
90         return pl_fbdev_buf;
91 }
92
93 void *pl_fbdev_flip(void)
94 {
95         flip_cnt++;
96
97         if (pl_fbdev_buf != NULL) {
98                 if (g_opts & OPT_SHOWFPS)
99                         print_fps();
100                 if (g_opts & OPT_SHOWCPU)
101                         print_cpu_usage();
102         }
103
104         // let's flip now
105         pl_fbdev_buf = vout_fbdev_flip(layer_fb);
106         return pl_fbdev_buf;
107 }
108
109 int pl_fbdev_open(void)
110 {
111         pl_fbdev_buf = vout_fbdev_flip(layer_fb);
112         omap_enable_layer(1);
113         return 0;
114 }
115
116 void pl_fbdev_close(void)
117 {
118         omap_enable_layer(0);
119 }
120
121 static void update_input(void)
122 {
123         int actions[IN_BINDTYPE_COUNT] = { 0, };
124
125         in_update(actions);
126         if (actions[IN_BINDTYPE_EMU] & PEV_MENU)
127                 stop = 1;
128         keystate = actions[IN_BINDTYPE_PLAYER12];
129
130 #ifdef X11
131         extern int x11_update_keys(void);
132         keystate |= x11_update_keys();
133 #endif
134 }
135
136 #define MAX_LAG_FRAMES 3
137
138 #define tvdiff(tv, tv_old) \
139         ((tv.tv_sec - tv_old.tv_sec) * 1000000 + tv.tv_usec - tv_old.tv_usec)
140 // assumes us < 1000000
141 #define tvadd(tv, us) { \
142         tv.tv_usec += us; \
143         if (tv.tv_usec >= 1000000) { \
144                 tv.tv_usec -= 1000000; \
145                 tv.tv_sec++; \
146         } \
147 }
148
149 /* called on every vsync */
150 void pl_frame_limit(void)
151 {
152         static struct timeval tv_old, tv_expect;
153         static int vsync_cnt_prev;
154         struct timeval now;
155         int diff;
156
157         vsync_cnt++;
158
159         /* doing input here because the pad is polled
160          * thousands of times per frame for some reason */
161         update_input();
162
163         pcnt_end(PCNT_ALL);
164         gettimeofday(&now, 0);
165
166         if (now.tv_sec != tv_old.tv_sec) {
167                 diff = tvdiff(now, tv_old);
168                 vsps_cur = 0.0f;
169                 if (0 < diff && diff < 2000000)
170                         vsps_cur = 1000000.0f * (vsync_cnt - vsync_cnt_prev) / diff;
171                 vsync_cnt_prev = vsync_cnt;
172                 flips_per_sec = flip_cnt;
173                 flip_cnt = 0;
174                 tv_old = now;
175                 if (g_opts & OPT_SHOWCPU)
176                         tick_per_sec = get_cpu_ticks();
177         }
178 #ifdef PCNT
179         static int ya_vsync_count;
180         if (++ya_vsync_count == PCNT_FRAMES) {
181                 pcnt_print(vsps_cur);
182                 ya_vsync_count = 0;
183         }
184 #endif
185
186         if (!(g_opts & OPT_NO_FRAMELIM)) {
187                 tvadd(tv_expect, pl_frame_interval);
188                 diff = tvdiff(tv_expect, now);
189                 if (diff > MAX_LAG_FRAMES * pl_frame_interval || diff < -MAX_LAG_FRAMES * pl_frame_interval) {
190                         //printf("pl_frame_limit reset, diff=%d, iv %d\n", diff, pl_frame_interval);
191                         tv_expect = now;
192                 }
193                 else if (diff > pl_frame_interval) {
194                         // yay for working usleep on pandora!
195                         //printf("usleep %d\n", diff - pl_frame_interval / 2);
196                         usleep(diff - pl_frame_interval / 2);
197                 }
198                 else if (diff < 0 && UseFrameSkip) {
199                         // P.E.Op.S. makes skip decision based on this
200                         fps_skip = 1000000.0f / (float)-diff;
201                 }
202         }
203
204         pcnt_start(PCNT_ALL);
205 }
206
207 static void pl_text_out16_(int x, int y, const char *text)
208 {
209         int i, l, len = strlen(text), w = pl_fbdev_w;
210         unsigned short *screen = (unsigned short *)pl_fbdev_buf + x + y * w;
211         unsigned short val = 0xffff;
212
213         for (i = 0; i < len; i++, screen += 8)
214         {
215                 for (l = 0; l < 8; l++)
216                 {
217                         unsigned char fd = fontdata8x8[text[i] * 8 + l];
218                         unsigned short *s = screen + l * w;
219                         if (fd&0x80) s[0] = val;
220                         if (fd&0x40) s[1] = val;
221                         if (fd&0x20) s[2] = val;
222                         if (fd&0x10) s[3] = val;
223                         if (fd&0x08) s[4] = val;
224                         if (fd&0x04) s[5] = val;
225                         if (fd&0x02) s[6] = val;
226                         if (fd&0x01) s[7] = val;
227                 }
228         }
229 }
230
231 void pl_text_out16(int x, int y, const char *texto, ...)
232 {
233         va_list args;
234         char    buffer[256];
235
236         va_start(args, texto);
237         vsnprintf(buffer, sizeof(buffer), texto, args);
238         va_end(args);
239
240         pl_text_out16_(x, y, buffer);
241 }
242
243 static void pl_get_layer_pos(int *x, int *y, int *w, int *h)
244 {
245         *x = g_layer_x;
246         *y = g_layer_y;
247         *w = g_layer_w;
248         *h = g_layer_h;
249 }
250
251 extern int UseFrameSkip; // hmh
252
253 const struct rearmed_cbs pl_rearmed_cbs = {
254         pl_get_layer_pos,
255         pl_fbdev_open,
256         pl_fbdev_set_mode,
257         pl_fbdev_flip,
258         pl_fbdev_close,
259         &UseFrameSkip,
260 };
261
262 /* watchdog */
263 static void *watchdog_thread(void *unused)
264 {
265         int vsync_cnt_old = 0;
266         int seen_dead = 0;
267         int sleep_time = 5;
268
269         while (1)
270         {
271                 sleep(sleep_time);
272
273                 if (stop) {
274                         seen_dead = 0;
275                         sleep_time = 5;
276                         continue;
277                 }
278                 if (vsync_cnt != vsync_cnt_old) {
279                         vsync_cnt_old = vsync_cnt;
280                         seen_dead = 0;
281                         sleep_time = 2;
282                         continue;
283                 }
284
285                 seen_dead++;
286                 sleep_time = 1;
287                 if (seen_dead > 1)
288                         fprintf(stderr, "watchdog: seen_dead %d\n", seen_dead);
289                 if (seen_dead > 4) {
290                         fprintf(stderr, "watchdog: lockup detected, aborting\n");
291                         // we can't do any cleanup here really, the main thread is
292                         // likely touching resources and would crash anyway
293                         abort();
294                 }
295         }
296 }
297
298 void pl_start_watchdog(void)
299 {
300         pthread_attr_t attr;
301         pthread_t tid;
302         int ret;
303         
304         pthread_attr_init(&attr);
305         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
306
307         ret = pthread_create(&tid, &attr, watchdog_thread, NULL);
308         if (ret != 0)
309                 fprintf(stderr, "could not start watchdog: %d\n", ret);
310 }
311