drc: fix COP2 decoding
[pcsx_rearmed.git] / frontend / plugin_lib.c
CommitLineData
b60f2812 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>
69af03a2 10#include <string.h>
11#include <stdarg.h>
72228559 12#include <sys/time.h>
13#include <sys/types.h>
14#include <sys/stat.h>
15#include <fcntl.h>
16#include <unistd.h>
7c0f51de 17#include <pthread.h>
b60f2812 18
72228559 19#include "plugin_lib.h"
b60f2812 20#include "linux/fbdev.h"
69af03a2 21#include "common/fonts.h"
22#include "common/input.h"
23#include "omap.h"
3c70c47b 24#include "menu.h"
72228559 25#include "pcnt.h"
69af03a2 26#include "../libpcsxcore/new_dynarec/new_dynarec.h"
b60f2812 27
b60f2812 28void *pl_fbdev_buf;
bce6b056 29int pl_frame_interval;
69af03a2 30int keystate;
72228559 31static int pl_fbdev_w, pl_fbdev_h, pl_fbdev_bpp;
bce6b056 32static int flip_cnt, vsync_cnt, flips_per_sec, tick_per_sec;
33static float vsps_cur;
72228559 34
35static 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
55static void print_fps(void)
56{
57 if (pl_fbdev_bpp == 16)
bce6b056 58 pl_text_out16(2, pl_fbdev_h - 10, "%2d %4.1f", flips_per_sec, vsps_cur);
72228559 59}
60
61static 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}
b60f2812 66
297b3d63 67void *pl_fbdev_set_mode(int w, int h, int bpp)
69af03a2 68{
d352cde2 69 void *ret;
b60f2812 70
72228559 71 if (w == pl_fbdev_w && h == pl_fbdev_h && bpp == pl_fbdev_bpp)
7947ab55 72 return pl_fbdev_buf;
72228559 73
69af03a2 74 pl_fbdev_w = w;
72228559 75 pl_fbdev_h = h;
76 pl_fbdev_bpp = bpp;
d352cde2 77
78 vout_fbdev_clear(layer_fb);
69af03a2 79 ret = vout_fbdev_resize(layer_fb, w, h, bpp, 0, 0, 0, 0, 3);
d352cde2 80 if (ret == NULL)
69af03a2 81 fprintf(stderr, "failed to set mode\n");
d352cde2 82 else
83 pl_fbdev_buf = ret;
84
bd6267e6 85 menu_notify_mode_change(w, h, bpp);
3c70c47b 86
297b3d63 87 return pl_fbdev_buf;
69af03a2 88}
89
ffd0d743 90void *pl_fbdev_flip(void)
69af03a2 91{
72228559 92 flip_cnt++;
297b3d63 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 }
72228559 100
69af03a2 101 // let's flip now
102 pl_fbdev_buf = vout_fbdev_flip(layer_fb);
ffd0d743 103 return pl_fbdev_buf;
b60f2812 104}
105
6d1a1ac2 106int pl_fbdev_open(void)
107{
108 pl_fbdev_buf = vout_fbdev_flip(layer_fb);
109 omap_enable_layer(1);
110 return 0;
111}
112
113void pl_fbdev_close(void)
b60f2812 114{
6d1a1ac2 115 omap_enable_layer(0);
b60f2812 116}
117
15d46930 118static 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];
447783f8 126
127#ifdef X11
1bd9ee68 128 extern int x11_update_keys(void);
129 keystate |= x11_update_keys();
447783f8 130#endif
15d46930 131}
132
bce6b056 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
72228559 146/* called on every vsync */
147void pl_frame_limit(void)
148{
bce6b056 149 static struct timeval tv_old, tv_expect;
7c0f51de 150 static int vsync_cnt_prev;
bce6b056 151 struct timeval now;
152 int diff;
153
154 vsync_cnt++;
72228559 155
15d46930 156 /* doing input here because the pad is polled
157 * thousands of times per frame for some reason */
158 update_input();
159
72228559 160 pcnt_end(PCNT_ALL);
bce6b056 161 gettimeofday(&now, 0);
72228559 162
bce6b056 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)
7c0f51de 167 vsps_cur = 1000000.0f * (vsync_cnt - vsync_cnt_prev) / diff;
168 vsync_cnt_prev = vsync_cnt;
72228559 169 flips_per_sec = flip_cnt;
7c0f51de 170 flip_cnt = 0;
bce6b056 171 tv_old = now;
bd6267e6 172 if (g_opts & OPT_SHOWCPU)
173 tick_per_sec = get_cpu_ticks();
72228559 174 }
175#ifdef PCNT
176 static int ya_vsync_count;
177 if (++ya_vsync_count == PCNT_FRAMES) {
bce6b056 178 pcnt_print(vsps_cur);
72228559 179 ya_vsync_count = 0;
180 }
181#endif
182
bce6b056 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 }
72228559 196
197 pcnt_start(PCNT_ALL);
198}
199
69af03a2 200static void pl_text_out16_(int x, int y, const char *text)
b60f2812 201{
69af03a2 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 }
b60f2812 222}
223
69af03a2 224void pl_text_out16(int x, int y, const char *texto, ...)
b60f2812 225{
69af03a2 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);
b60f2812 234}
235
201c21e2 236static 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
78d78c3b 244extern int UseFrameSkip; // hmh
245
201c21e2 246const struct rearmed_cbs pl_rearmed_cbs = {
247 pl_get_layer_pos,
ffd0d743 248 pl_fbdev_open,
249 pl_fbdev_set_mode,
250 pl_fbdev_flip,
251 pl_fbdev_close,
78d78c3b 252 &UseFrameSkip,
201c21e2 253};
254
7c0f51de 255/* watchdog */
256static 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
291void 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