all repos — mgba @ f09e44062f3117825df934eef05795728a9e2296

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 GBAThread context;
30	struct GBAVideoSoftwareRenderer renderer;
31	GBAVideoSoftwareRendererCreate(&renderer);
32
33	renderer.outputBuffer = malloc(256 * 256 * 4);
34	renderer.outputBufferStride = 256;
35
36	context.fd = fd;
37	context.fname = fname;
38	context.useDebugger = 0;
39	context.renderer = &renderer.d;
40	context.frameskip = 0;
41	context.sync.videoFrameWait = 0;
42	context.sync.audioWait = 0;
43	context.startCallback = 0;
44	context.cleanCallback = 0;
45	_thread = &context;
46	GBAThreadStart(&context);
47
48	int frames = 0;
49	time_t start = time(0);
50	_GBAPerfRunloop(&context, &frames);
51	time_t end = time(0);
52	int duration = end - start;
53
54	GBAThreadJoin(&context);
55	close(fd);
56
57	free(renderer.outputBuffer);
58
59	printf("%u frames in %i seconds: %g fps (%gx)\n", frames, duration, frames / (float) duration, frames / (duration * 60.f));
60
61	return 0;
62}
63
64static void _GBAPerfRunloop(struct GBAThread* context, int* frames) {
65	struct timeval lastEcho;
66	gettimeofday(&lastEcho, 0);
67	int lastFrames = 0;
68	while (context->state < THREAD_EXITING) {
69		if (GBASyncWaitFrameStart(&context->sync, 0)) {
70			++*frames;
71			++lastFrames;
72			struct timeval currentTime;
73			long timeDiff;
74			gettimeofday(&currentTime, 0);
75			timeDiff = currentTime.tv_sec - lastEcho.tv_sec;
76			timeDiff *= 1000;
77			timeDiff += (currentTime.tv_usec - lastEcho.tv_usec) / 1000;
78			if (timeDiff >= 1000) {
79				printf("\033[2K\rCurrent FPS: %g (%gx)", lastFrames / (timeDiff / 1000.0f), lastFrames / (float) (60 * (timeDiff / 1000.0f)));
80				fflush(stdout);
81				lastEcho = currentTime;
82				lastFrames = 0;
83			}
84		}
85		GBASyncWaitFrameEnd(&context->sync);
86	}
87}
88
89static void _GBAPerfShutdown(int signal) {
90	pthread_mutex_lock(&_thread->stateMutex);
91	_thread->state = THREAD_EXITING;
92	pthread_mutex_unlock(&_thread->stateMutex);
93}