all repos — mgba @ 6616ca9111785abada65142442c51a84dc0c804d

mGBA Game Boy Advance Emulator

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