src/platform/perf-main.c (view raw)
1#include "gba-thread.h"
2#include "gba.h"
3#include "renderers/video-software.h"
4
5#include <fcntl.h>
6#include <signal.h>
7#include <sys/time.h>
8
9static void _GBAPerfRunloop(struct GBAThread* context, int* frames);
10static void _GBAPerfShutdown(int signal);
11
12static struct GBAThread* _thread;
13
14int main(int argc, char** argv) {
15 const char* fname = "test.rom";
16 if (argc > 1) {
17 fname = argv[1];
18 }
19 int fd = open(fname, O_RDONLY);
20 if (fd < 0) {
21 return 1;
22 }
23
24 signal(SIGINT, _GBAPerfShutdown);
25
26 struct GBAVideoSoftwareRenderer renderer;
27 GBAVideoSoftwareRendererCreate(&renderer);
28
29 renderer.outputBuffer = malloc(256 * 256 * 4);
30 renderer.outputBufferStride = 256;
31
32 struct GBAThread context = {
33 .fd = fd,
34 .fname = fname,
35 .biosFd = -1,
36 .renderer = &renderer.d,
37 .frameskip = 0,
38 .sync.videoFrameWait = 0,
39 .sync.audioWait = 0
40 };
41 _thread = &context;
42 GBAThreadStart(&context);
43
44 int frames = 0;
45 time_t start = time(0);
46 _GBAPerfRunloop(&context, &frames);
47 time_t end = time(0);
48 int duration = end - start;
49
50 GBAThreadJoin(&context);
51 close(fd);
52
53 free(renderer.outputBuffer);
54
55 printf("%u frames in %i seconds: %g fps (%gx)\n", frames, duration, frames / (float) duration, frames / (duration * 60.f));
56
57 return 0;
58}
59
60static void _GBAPerfRunloop(struct GBAThread* context, int* frames) {
61 struct timeval lastEcho;
62 gettimeofday(&lastEcho, 0);
63 int lastFrames = 0;
64 while (context->state < THREAD_EXITING) {
65 if (GBASyncWaitFrameStart(&context->sync, 0)) {
66 ++*frames;
67 ++lastFrames;
68 struct timeval currentTime;
69 long timeDiff;
70 gettimeofday(¤tTime, 0);
71 timeDiff = currentTime.tv_sec - lastEcho.tv_sec;
72 timeDiff *= 1000;
73 timeDiff += (currentTime.tv_usec - lastEcho.tv_usec) / 1000;
74 if (timeDiff >= 1000) {
75 printf("\033[2K\rCurrent FPS: %g (%gx)", lastFrames / (timeDiff / 1000.0f), lastFrames / (float) (60 * (timeDiff / 1000.0f)));
76 fflush(stdout);
77 lastEcho = currentTime;
78 lastFrames = 0;
79 }
80 }
81 GBASyncWaitFrameEnd(&context->sync);
82 }
83}
84
85static void _GBAPerfShutdown(int signal) {
86 (void) (signal);
87 pthread_mutex_lock(&_thread->stateMutex);
88 _thread->state = THREAD_EXITING;
89 pthread_mutex_unlock(&_thread->stateMutex);
90}