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