all repos — mgba @ 09888a885291817402034f25b65f0d8a95f4d7f4

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	while (context->state < THREAD_EXITING) {
66		if (GBASyncWaitFrameStart(&context->sync, 0)) {
67			++*frames;
68		}
69		GBASyncWaitFrameEnd(&context->sync);
70	}
71}
72
73static void _GBAPerfShutdown(int signal) {
74	pthread_mutex_lock(&_thread->stateMutex);
75	_thread->state = THREAD_EXITING;
76	pthread_mutex_unlock(&_thread->stateMutex);
77}