changeset 479:c0a1744bc2cf

Added a -benchmark flag for testing the speeds of various video modes
author Sam Lantinga <slouken@libsdl.org>
date Sat, 31 Aug 2002 19:48:11 +0000
parents f8482d7c9595
children 92596bfe8446
files test/testvidinfo.c
diffstat 1 files changed, 187 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/test/testvidinfo.c	Sat Aug 31 04:13:28 2002 +0000
+++ b/test/testvidinfo.c	Sat Aug 31 19:48:11 2002 +0000
@@ -1,11 +1,193 @@
 
 /* Simple program -- figure out what kind of video display we have */
 
+#include <stdlib.h>
 #include <stdio.h>
 #include <stdlib.h>
 
 #include "SDL.h"
 
+#define FLAG_MASK	(SDL_HWSURFACE | SDL_FULLSCREEN | SDL_DOUBLEBUF)
+
+void PrintFlags(Uint32 flags)
+{
+	printf("0x%8.8x", (flags & FLAG_MASK));
+	if ( flags & SDL_HWSURFACE ) {
+		printf(" SDL_HWSURFACE");
+	} else {
+		printf(" SDL_SWSURFACE");
+	}
+	if ( flags & SDL_FULLSCREEN ) {
+		printf(" | SDL_FULLSCREEN");
+	}
+	if ( flags & SDL_DOUBLEBUF ) {
+		printf(" | SDL_DOUBLEBUF");
+	}
+}
+
+int RunBlitTests(SDL_Surface *screen, SDL_Surface *bmp, int blitcount)
+{
+	int i, j;
+	int maxx;
+	int maxy;
+	SDL_Rect *rects;
+
+	rects = (SDL_Rect *)malloc(blitcount * sizeof(*rects));
+	if ( ! rects ) {
+		return 0;
+	}
+	maxx = (int)screen->w - bmp->w;
+	maxy = (int)screen->h - bmp->h;
+	for ( i = 0; i < 100; ++i ) {
+		for ( j = 0; j < blitcount; ++j ) {
+			if ( maxx ) {
+				rects[j].x = rand() % maxx;
+			} else {
+				rects[j].x = 0;
+			}
+			if ( maxy ) {
+				rects[j].y = rand() % maxy;
+			} else {
+				rects[j].y = 0;
+			}
+			rects[j].w = bmp->w;
+			rects[j].h = bmp->h;
+			SDL_BlitSurface(bmp, NULL, screen, &rects[j]);
+		}
+		if ( screen->flags & SDL_DOUBLEBUF ) {
+			SDL_Flip(screen);
+		} else {
+			SDL_UpdateRects(screen, blitcount, rects);
+		}
+	}
+	free(rects);
+
+	return i;
+}
+
+void RunModeTests(SDL_Surface *screen)
+{
+	Uint32 then, now;
+	Uint32 frames;
+	int i;
+	Uint8 r, g, b;
+	Uint32 pixel;
+	SDL_Surface *bmp, *tmp;
+
+	/* First test fills and screen update speed */
+	printf("Running color fill and fullscreen update test\n");
+	then = SDL_GetTicks();
+	frames = 0;
+	for ( i = 0; i < 256; ++i ) {
+		r = i;
+		g = 0;
+		b = 0;
+		SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, r, g, b));
+		SDL_Flip(screen);
+		++frames;
+	}
+	for ( i = 0; i < 256; ++i ) {
+		r = 0;
+		g = i;
+		b = 0;
+		SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, r, g, b));
+		SDL_Flip(screen);
+		++frames;
+	}
+	for ( i = 0; i < 256; ++i ) {
+		r = 0;
+		g = 0;
+		b = i;
+		SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, r, g, b));
+		SDL_Flip(screen);
+		++frames;
+	}
+	now = SDL_GetTicks();
+	printf("%d fills and flips, %f FPS\n", frames, (float)(now - then) / frames);
+
+	bmp = SDL_LoadBMP("sample.bmp");
+	if ( ! bmp ) {
+		printf("Couldn't load sample.bmp: %s\n", SDL_GetError());
+		return;
+	}
+	printf("Running freshly loaded blit test: %dx%d at %d bpp, flags: ",
+		bmp->w, bmp->h, bmp->format->BitsPerPixel);
+	PrintFlags(bmp->flags);
+	printf("\n");
+	then = SDL_GetTicks();
+	frames = RunBlitTests(screen, bmp, 10);
+	now = SDL_GetTicks();
+	if ( frames ) {
+		printf("%d blits, %d updates, %f FPS\n", 10*frames, frames, (float)(now - then) / frames);
+	}
+
+	tmp = bmp;
+	bmp = SDL_DisplayFormat(bmp);
+	SDL_FreeSurface(tmp);
+	if ( ! bmp ) {
+		printf("Couldn't convert sample.bmp: %s\n", SDL_GetError());
+		return;
+	}
+	printf("Running display format blit test: %dx%d at %d bpp, flags: ",
+		bmp->w, bmp->h, bmp->format->BitsPerPixel);
+	PrintFlags(bmp->flags);
+	printf("\n");
+	then = SDL_GetTicks();
+	frames = RunBlitTests(screen, bmp, 10);
+	now = SDL_GetTicks();
+	if ( frames ) {
+		printf("%d blits, %d updates, %f FPS\n", 10*frames, frames, (float)(now - then) / frames);
+	}
+	SDL_FreeSurface(bmp);
+}
+
+void RunVideoTests()
+{
+	static const struct {
+		int w, h, bpp;
+	} mode_list[] = {
+		{ 640, 480, 8 }, { 640, 480, 16 }, { 640, 480, 32 },
+		{ 800, 600, 8 }, { 800, 600, 16 }, { 800, 600, 32 },
+		{ 1024, 768, 8 }, { 1024, 768, 16 }, { 1024, 768, 32 }
+	};
+	static const Uint32 flags[] = {
+		(SDL_SWSURFACE),
+		(SDL_SWSURFACE | SDL_FULLSCREEN),
+		(SDL_HWSURFACE | SDL_FULLSCREEN),
+		(SDL_HWSURFACE | SDL_FULLSCREEN | SDL_DOUBLEBUF)
+	};
+	int i, j;
+	SDL_Surface *screen;
+
+	/* Test out several different video mode combinations */
+	for ( i = 0; i < SDL_TABLESIZE(mode_list); ++i ) {
+		for ( j = 0; j < SDL_TABLESIZE(flags); ++j ) {
+			printf("===================================\n");
+			printf("Setting video mode: %dx%d at %d bpp, flags: ",
+			                          mode_list[i].w,
+			                          mode_list[i].h,
+			                          mode_list[i].bpp);
+			PrintFlags(flags[j]);
+			printf("\n");
+			screen = SDL_SetVideoMode(mode_list[i].w,
+			                          mode_list[i].h,
+			                          mode_list[i].bpp,
+			                          flags[j]);
+			if ( ! screen ) {
+				printf("Setting video mode failed: %s\n", SDL_GetError());
+				continue;
+			}
+			if ( (screen->flags & FLAG_MASK) != flags[j] ) {
+				printf("Flags didn't match: ");
+				PrintFlags(screen->flags);
+				printf("\n");
+				continue;
+			}
+			RunModeTests(screen);
+		}
+	}
+}
+
 int main(int argc, char *argv[])
 {
 	const SDL_VideoInfo *info;
@@ -73,6 +255,11 @@
 		printf(
 "Color fills on hardware surfaces are accelerated\n");
 	}
+
+	if ( argv[1] && (strcmp(argv[1], "-benchmark") == 0) ) {
+		RunVideoTests();
+	}
+
 	SDL_Quit();
 	return(0);
 }