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> |
b60f2812 |
17 | |
72228559 |
18 | #include "plugin_lib.h" |
b60f2812 |
19 | #include "linux/fbdev.h" |
69af03a2 |
20 | #include "common/fonts.h" |
21 | #include "common/input.h" |
22 | #include "omap.h" |
3c70c47b |
23 | #include "menu.h" |
72228559 |
24 | #include "pcnt.h" |
69af03a2 |
25 | #include "../libpcsxcore/new_dynarec/new_dynarec.h" |
b60f2812 |
26 | |
b60f2812 |
27 | void *pl_fbdev_buf; |
69af03a2 |
28 | int keystate; |
72228559 |
29 | static int pl_fbdev_w, pl_fbdev_h, pl_fbdev_bpp; |
30 | static int flip_cnt, flips_per_sec, tick_per_sec; |
31 | extern float fps_cur; // XXX |
32 | |
33 | static int get_cpu_ticks(void) |
34 | { |
35 | static unsigned long last_utime; |
36 | static int fd; |
37 | unsigned long utime, ret; |
38 | char buf[128]; |
39 | |
40 | if (fd == 0) |
41 | fd = open("/proc/self/stat", O_RDONLY); |
42 | lseek(fd, 0, SEEK_SET); |
43 | buf[0] = 0; |
44 | read(fd, buf, sizeof(buf)); |
45 | buf[sizeof(buf) - 1] = 0; |
46 | |
47 | sscanf(buf, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu", &utime); |
48 | ret = utime - last_utime; |
49 | last_utime = utime; |
50 | return ret; |
51 | } |
52 | |
53 | static void print_fps(void) |
54 | { |
55 | if (pl_fbdev_bpp == 16) |
56 | pl_text_out16(2, pl_fbdev_h - 10, "%2d %4.1f", flips_per_sec, fps_cur); |
57 | } |
58 | |
59 | static void print_cpu_usage(void) |
60 | { |
61 | if (pl_fbdev_bpp == 16) |
62 | pl_text_out16(pl_fbdev_w - 28, pl_fbdev_h - 10, "%3d", tick_per_sec); |
63 | } |
b60f2812 |
64 | |
65 | int pl_fbdev_init(void) |
66 | { |
69af03a2 |
67 | pl_fbdev_buf = vout_fbdev_flip(layer_fb); |
68 | return 0; |
69 | } |
b60f2812 |
70 | |
69af03a2 |
71 | int pl_fbdev_set_mode(int w, int h, int bpp) |
72 | { |
d352cde2 |
73 | void *ret; |
b60f2812 |
74 | |
72228559 |
75 | if (w == pl_fbdev_w && h == pl_fbdev_h && bpp == pl_fbdev_bpp) |
76 | return 0; |
77 | |
69af03a2 |
78 | pl_fbdev_w = w; |
72228559 |
79 | pl_fbdev_h = h; |
80 | pl_fbdev_bpp = bpp; |
d352cde2 |
81 | |
82 | vout_fbdev_clear(layer_fb); |
69af03a2 |
83 | ret = vout_fbdev_resize(layer_fb, w, h, bpp, 0, 0, 0, 0, 3); |
d352cde2 |
84 | if (ret == NULL) |
69af03a2 |
85 | fprintf(stderr, "failed to set mode\n"); |
d352cde2 |
86 | else |
87 | pl_fbdev_buf = ret; |
88 | |
bd6267e6 |
89 | menu_notify_mode_change(w, h, bpp); |
3c70c47b |
90 | |
d352cde2 |
91 | return (ret != NULL) ? 0 : -1; |
69af03a2 |
92 | } |
93 | |
72228559 |
94 | void pl_fbdev_flip(void) |
69af03a2 |
95 | { |
72228559 |
96 | flip_cnt++; |
bd6267e6 |
97 | if (g_opts & OPT_SHOWFPS) |
98 | print_fps(); |
99 | if (g_opts & OPT_SHOWCPU) |
100 | print_cpu_usage(); |
72228559 |
101 | |
69af03a2 |
102 | // let's flip now |
103 | pl_fbdev_buf = vout_fbdev_flip(layer_fb); |
b60f2812 |
104 | } |
105 | |
69af03a2 |
106 | void pl_fbdev_finish(void) |
b60f2812 |
107 | { |
b60f2812 |
108 | } |
109 | |
15d46930 |
110 | static void update_input(void) |
111 | { |
112 | int actions[IN_BINDTYPE_COUNT] = { 0, }; |
113 | |
114 | in_update(actions); |
115 | if (actions[IN_BINDTYPE_EMU] & PEV_MENU) |
116 | stop = 1; |
117 | keystate = actions[IN_BINDTYPE_PLAYER12]; |
118 | } |
119 | |
72228559 |
120 | /* called on every vsync */ |
121 | void pl_frame_limit(void) |
122 | { |
123 | extern void CheckFrameRate(void); |
124 | static int oldsec; |
125 | struct timeval tv; |
126 | |
15d46930 |
127 | /* doing input here because the pad is polled |
128 | * thousands of times per frame for some reason */ |
129 | update_input(); |
130 | |
72228559 |
131 | pcnt_end(PCNT_ALL); |
132 | gettimeofday(&tv, 0); |
133 | |
134 | if (tv.tv_sec != oldsec) { |
135 | flips_per_sec = flip_cnt; |
136 | flip_cnt = 0; |
72228559 |
137 | oldsec = tv.tv_sec; |
bd6267e6 |
138 | if (g_opts & OPT_SHOWCPU) |
139 | tick_per_sec = get_cpu_ticks(); |
72228559 |
140 | } |
141 | #ifdef PCNT |
142 | static int ya_vsync_count; |
143 | if (++ya_vsync_count == PCNT_FRAMES) { |
144 | pcnt_print(fps_cur); |
145 | ya_vsync_count = 0; |
146 | } |
147 | #endif |
148 | |
149 | CheckFrameRate(); |
150 | |
151 | pcnt_start(PCNT_ALL); |
152 | } |
153 | |
69af03a2 |
154 | static void pl_text_out16_(int x, int y, const char *text) |
b60f2812 |
155 | { |
69af03a2 |
156 | int i, l, len = strlen(text), w = pl_fbdev_w; |
157 | unsigned short *screen = (unsigned short *)pl_fbdev_buf + x + y * w; |
158 | unsigned short val = 0xffff; |
159 | |
160 | for (i = 0; i < len; i++, screen += 8) |
161 | { |
162 | for (l = 0; l < 8; l++) |
163 | { |
164 | unsigned char fd = fontdata8x8[text[i] * 8 + l]; |
165 | unsigned short *s = screen + l * w; |
166 | if (fd&0x80) s[0] = val; |
167 | if (fd&0x40) s[1] = val; |
168 | if (fd&0x20) s[2] = val; |
169 | if (fd&0x10) s[3] = val; |
170 | if (fd&0x08) s[4] = val; |
171 | if (fd&0x04) s[5] = val; |
172 | if (fd&0x02) s[6] = val; |
173 | if (fd&0x01) s[7] = val; |
174 | } |
175 | } |
b60f2812 |
176 | } |
177 | |
69af03a2 |
178 | void pl_text_out16(int x, int y, const char *texto, ...) |
b60f2812 |
179 | { |
69af03a2 |
180 | va_list args; |
181 | char buffer[256]; |
182 | |
183 | va_start(args, texto); |
184 | vsnprintf(buffer, sizeof(buffer), texto, args); |
185 | va_end(args); |
186 | |
187 | pl_text_out16_(x, y, buffer); |
b60f2812 |
188 | } |
189 | |