Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
424 lines (367 loc) · 13.4 KB

testblitspeed.c

File metadata and controls

424 lines (367 loc) · 13.4 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
* Benchmarks surface-to-surface blits in various formats.
*
* Written by Ryan C. Gordon.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "SDL.h"
static SDL_Surface *dest = NULL;
static SDL_Surface *src = NULL;
static int testSeconds = 10;
May 28, 2006
May 28, 2006
18
static int
May 29, 2006
May 29, 2006
19
percent(int val, int total)
May 28, 2006
May 28, 2006
21
return ((int) ((((float) val) / ((float) total)) * 100.0f));
May 28, 2006
May 28, 2006
24
static int
May 29, 2006
May 29, 2006
25
randRange(int lo, int hi)
May 29, 2006
May 29, 2006
27
return (lo + (int) (((double) hi) * rand() / (RAND_MAX + 1.0)));
May 28, 2006
May 28, 2006
30
static void
May 29, 2006
May 29, 2006
31
copy_trunc_str(char *str, size_t strsize, const char *flagstr)
May 29, 2006
May 29, 2006
33
34
if ((strlen(str) + strlen(flagstr)) >= (strsize - 1))
strcpy(str + (strsize - 5), " ...");
May 29, 2006
May 29, 2006
36
strcat(str, flagstr);
May 28, 2006
May 28, 2006
39
static void
May 29, 2006
May 29, 2006
40
41
__append_sdl_surface_flag(SDL_Surface * _surface, char *str,
size_t strsize, Uint32 flag, const char *flagstr)
42
43
{
if (_surface->flags & flag)
May 29, 2006
May 29, 2006
44
copy_trunc_str(str, strsize, flagstr);
Jan 5, 2006
Jan 5, 2006
45
}
46
47
48
49
50
#define append_sdl_surface_flag(a, b, c, fl) __append_sdl_surface_flag(a, b, c, fl, " " #fl)
#define print_tf_state(str, val) printf("%s: {%s}\n", str, (val) ? "true" : "false" )
May 28, 2006
May 28, 2006
51
static void
May 29, 2006
May 29, 2006
52
output_videoinfo_details(void)
Jan 5, 2006
Jan 5, 2006
53
{
May 29, 2006
May 29, 2006
54
55
const SDL_VideoInfo *info = SDL_GetVideoInfo();
printf("SDL_GetVideoInfo():\n");
Jan 5, 2006
Jan 5, 2006
56
if (info == NULL)
May 29, 2006
May 29, 2006
57
printf(" (null.)\n");
May 28, 2006
May 28, 2006
58
else {
May 29, 2006
May 29, 2006
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
print_tf_state(" hardware surface available", info->hw_available);
print_tf_state(" window manager available", info->wm_available);
print_tf_state(" accelerated hardware->hardware blits",
info->blit_hw);
print_tf_state(" accelerated hardware->hardware colorkey blits",
info->blit_hw_CC);
print_tf_state(" accelerated hardware->hardware alpha blits",
info->blit_hw_A);
print_tf_state(" accelerated software->hardware blits",
info->blit_sw);
print_tf_state(" accelerated software->hardware colorkey blits",
info->blit_sw_CC);
print_tf_state(" accelerated software->hardware alpha blits",
info->blit_sw_A);
print_tf_state(" accelerated color fills", info->blit_fill);
printf(" video memory: (%d)\n", info->video_mem);
Jan 5, 2006
Jan 5, 2006
75
76
}
May 29, 2006
May 29, 2006
77
printf("\n");
Jan 5, 2006
Jan 5, 2006
78
79
}
May 28, 2006
May 28, 2006
80
static void
May 29, 2006
May 29, 2006
81
output_surface_details(const char *name, SDL_Surface * surface)
May 29, 2006
May 29, 2006
83
printf("Details for %s:\n", name);
May 28, 2006
May 28, 2006
85
if (surface == NULL) {
May 29, 2006
May 29, 2006
86
printf("-WARNING- You've got a NULL surface!");
May 28, 2006
May 28, 2006
87
} else {
88
char f[256];
May 29, 2006
May 29, 2006
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
printf(" width : %d\n", surface->w);
printf(" height : %d\n", surface->h);
printf(" depth : %d bits per pixel\n",
surface->format->BitsPerPixel);
printf(" pitch : %d\n", (int) surface->pitch);
printf(" alpha : %d\n", (int) surface->format->alpha);
printf(" colorkey : 0x%X\n",
(unsigned int) surface->format->colorkey);
printf(" red bits : 0x%08X mask, %d shift, %d loss\n",
(int) surface->format->Rmask,
(int) surface->format->Rshift, (int) surface->format->Rloss);
printf(" green bits : 0x%08X mask, %d shift, %d loss\n",
(int) surface->format->Gmask,
(int) surface->format->Gshift, (int) surface->format->Gloss);
printf(" blue bits : 0x%08X mask, %d shift, %d loss\n",
(int) surface->format->Bmask,
(int) surface->format->Bshift, (int) surface->format->Bloss);
printf(" alpha bits : 0x%08X mask, %d shift, %d loss\n",
(int) surface->format->Amask,
(int) surface->format->Ashift, (int) surface->format->Aloss);
110
111
112
f[0] = '\0';
May 28, 2006
May 28, 2006
113
/*append_sdl_surface_flag(surface, f, sizeof (f), SDL_SWSURFACE); */
114
if ((surface->flags & SDL_HWSURFACE) == 0)
May 29, 2006
May 29, 2006
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
copy_trunc_str(f, sizeof(f), " SDL_SWSURFACE");
append_sdl_surface_flag(surface, f, sizeof(f), SDL_HWSURFACE);
append_sdl_surface_flag(surface, f, sizeof(f), SDL_ASYNCBLIT);
append_sdl_surface_flag(surface, f, sizeof(f), SDL_ANYFORMAT);
append_sdl_surface_flag(surface, f, sizeof(f), SDL_HWPALETTE);
append_sdl_surface_flag(surface, f, sizeof(f), SDL_DOUBLEBUF);
append_sdl_surface_flag(surface, f, sizeof(f), SDL_FULLSCREEN);
append_sdl_surface_flag(surface, f, sizeof(f), SDL_OPENGL);
append_sdl_surface_flag(surface, f, sizeof(f), SDL_RESIZABLE);
append_sdl_surface_flag(surface, f, sizeof(f), SDL_NOFRAME);
append_sdl_surface_flag(surface, f, sizeof(f), SDL_HWACCEL);
append_sdl_surface_flag(surface, f, sizeof(f), SDL_SRCCOLORKEY);
append_sdl_surface_flag(surface, f, sizeof(f), SDL_RLEACCELOK);
append_sdl_surface_flag(surface, f, sizeof(f), SDL_RLEACCEL);
append_sdl_surface_flag(surface, f, sizeof(f), SDL_SRCALPHA);
append_sdl_surface_flag(surface, f, sizeof(f), SDL_PREALLOC);
132
133
if (f[0] == '\0')
May 29, 2006
May 29, 2006
134
strcpy(f, " (none)");
May 29, 2006
May 29, 2006
136
printf(" flags :%s\n", f);
Jan 5, 2006
Jan 5, 2006
137
}
May 29, 2006
May 29, 2006
139
printf("\n");
May 28, 2006
May 28, 2006
142
static void
May 29, 2006
May 29, 2006
143
output_details(void)
May 29, 2006
May 29, 2006
145
146
147
output_videoinfo_details();
output_surface_details("Source Surface", src);
output_surface_details("Destination Surface", dest);
May 28, 2006
May 28, 2006
150
static Uint32
May 29, 2006
May 29, 2006
151
blit(SDL_Surface * dst, SDL_Surface * src, int x, int y)
152
153
154
155
156
157
158
159
160
{
Uint32 start = 0;
SDL_Rect srcRect;
SDL_Rect dstRect;
srcRect.x = 0;
srcRect.y = 0;
dstRect.x = x;
dstRect.y = y;
May 28, 2006
May 28, 2006
161
dstRect.w = srcRect.w = src->w; /* SDL will clip as appropriate. */
162
163
dstRect.h = srcRect.h = src->h;
May 29, 2006
May 29, 2006
164
165
166
start = SDL_GetTicks();
SDL_BlitSurface(src, &srcRect, dst, &dstRect);
return (SDL_GetTicks() - start);
May 28, 2006
May 28, 2006
169
static void
May 29, 2006
May 29, 2006
170
blitCentered(SDL_Surface * dst, SDL_Surface * src)
171
172
173
{
int x = (dst->w - src->w) / 2;
int y = (dst->h - src->h) / 2;
May 29, 2006
May 29, 2006
174
blit(dst, src, x, y);
May 28, 2006
May 28, 2006
177
static int
May 29, 2006
May 29, 2006
178
atoi_hex(const char *str)
179
180
181
182
{
if (str == NULL)
return 0;
May 29, 2006
May 29, 2006
183
if (strlen(str) > 2) {
184
185
int retval = 0;
if ((str[0] == '0') && (str[1] == 'x'))
May 29, 2006
May 29, 2006
186
sscanf(str + 2, "%X", &retval);
May 28, 2006
May 28, 2006
187
return (retval);
May 29, 2006
May 29, 2006
190
return (atoi(str));
May 28, 2006
May 28, 2006
194
static int
May 29, 2006
May 29, 2006
195
setup_test(int argc, char **argv)
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
{
const char *dumpfile = NULL;
SDL_Surface *bmp = NULL;
Uint32 dstbpp = 32;
Uint32 dstrmask = 0x00FF0000;
Uint32 dstgmask = 0x0000FF00;
Uint32 dstbmask = 0x000000FF;
Uint32 dstamask = 0x00000000;
Uint32 dstflags = 0;
int dstw = 640;
int dsth = 480;
Uint32 srcbpp = 32;
Uint32 srcrmask = 0x00FF0000;
Uint32 srcgmask = 0x0000FF00;
Uint32 srcbmask = 0x000000FF;
Uint32 srcamask = 0x00000000;
Uint32 srcflags = 0;
int srcw = 640;
int srch = 480;
Jan 5, 2006
Jan 5, 2006
215
216
217
218
219
220
Uint32 origsrcalphaflags = 0;
Uint32 origdstalphaflags = 0;
Uint32 srcalphaflags = 0;
Uint32 dstalphaflags = 0;
int srcalpha = 255;
int dstalpha = 255;
221
int screenSurface = 0;
Jan 5, 2006
Jan 5, 2006
222
int i = 0;
May 28, 2006
May 28, 2006
224
for (i = 1; i < argc; i++) {
225
226
const char *arg = argv[i];
May 29, 2006
May 29, 2006
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
if (strcmp(arg, "--dstbpp") == 0)
dstbpp = atoi(argv[++i]);
else if (strcmp(arg, "--dstrmask") == 0)
dstrmask = atoi_hex(argv[++i]);
else if (strcmp(arg, "--dstgmask") == 0)
dstgmask = atoi_hex(argv[++i]);
else if (strcmp(arg, "--dstbmask") == 0)
dstbmask = atoi_hex(argv[++i]);
else if (strcmp(arg, "--dstamask") == 0)
dstamask = atoi_hex(argv[++i]);
else if (strcmp(arg, "--dstwidth") == 0)
dstw = atoi(argv[++i]);
else if (strcmp(arg, "--dstheight") == 0)
dsth = atoi(argv[++i]);
else if (strcmp(arg, "--dsthwsurface") == 0)
242
dstflags |= SDL_HWSURFACE;
May 29, 2006
May 29, 2006
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
else if (strcmp(arg, "--srcbpp") == 0)
srcbpp = atoi(argv[++i]);
else if (strcmp(arg, "--srcrmask") == 0)
srcrmask = atoi_hex(argv[++i]);
else if (strcmp(arg, "--srcgmask") == 0)
srcgmask = atoi_hex(argv[++i]);
else if (strcmp(arg, "--srcbmask") == 0)
srcbmask = atoi_hex(argv[++i]);
else if (strcmp(arg, "--srcamask") == 0)
srcamask = atoi_hex(argv[++i]);
else if (strcmp(arg, "--srcwidth") == 0)
srcw = atoi(argv[++i]);
else if (strcmp(arg, "--srcheight") == 0)
srch = atoi(argv[++i]);
else if (strcmp(arg, "--srchwsurface") == 0)
258
srcflags |= SDL_HWSURFACE;
May 29, 2006
May 29, 2006
259
260
261
else if (strcmp(arg, "--seconds") == 0)
testSeconds = atoi(argv[++i]);
else if (strcmp(arg, "--screen") == 0)
262
screenSurface = 1;
May 29, 2006
May 29, 2006
263
else if (strcmp(arg, "--dumpfile") == 0)
264
dumpfile = argv[++i];
Jan 5, 2006
Jan 5, 2006
265
/* !!! FIXME: set colorkey. */
May 28, 2006
May 28, 2006
266
else if (0) { /* !!! FIXME: we handle some commandlines elsewhere now */
May 29, 2006
May 29, 2006
267
fprintf(stderr, "Unknown commandline option: %s\n", arg);
May 28, 2006
May 28, 2006
268
return (0);
May 29, 2006
May 29, 2006
272
273
if (SDL_Init(SDL_INIT_VIDEO) == -1) {
fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());
May 28, 2006
May 28, 2006
274
return (0);
May 29, 2006
May 29, 2006
277
bmp = SDL_LoadBMP("sample.bmp");
May 28, 2006
May 28, 2006
278
if (bmp == NULL) {
May 29, 2006
May 29, 2006
279
280
fprintf(stderr, "SDL_LoadBMP failed: %s\n", SDL_GetError());
SDL_Quit();
May 28, 2006
May 28, 2006
281
return (0);
May 28, 2006
May 28, 2006
284
285
286
287
if ((dstflags & SDL_HWSURFACE) == 0)
dstflags |= SDL_SWSURFACE;
if ((srcflags & SDL_HWSURFACE) == 0)
srcflags |= SDL_SWSURFACE;
288
289
if (screenSurface)
May 29, 2006
May 29, 2006
290
dest = SDL_SetVideoMode(dstw, dsth, dstbpp, dstflags);
May 28, 2006
May 28, 2006
291
else {
May 29, 2006
May 29, 2006
292
293
dest = SDL_CreateRGBSurface(dstflags, dstw, dsth, dstbpp,
dstrmask, dstgmask, dstbmask, dstamask);
May 28, 2006
May 28, 2006
296
if (dest == NULL) {
May 29, 2006
May 29, 2006
297
298
fprintf(stderr, "dest surface creation failed: %s\n", SDL_GetError());
SDL_Quit();
May 28, 2006
May 28, 2006
299
return (0);
May 29, 2006
May 29, 2006
302
303
src = SDL_CreateRGBSurface(srcflags, srcw, srch, srcbpp,
srcrmask, srcgmask, srcbmask, srcamask);
May 28, 2006
May 28, 2006
304
if (src == NULL) {
May 29, 2006
May 29, 2006
305
306
fprintf(stderr, "src surface creation failed: %s\n", SDL_GetError());
SDL_Quit();
May 28, 2006
May 28, 2006
307
return (0);
Jan 5, 2006
Jan 5, 2006
310
/* handle alpha settings... */
May 28, 2006
May 28, 2006
311
312
313
srcalphaflags = (src->flags & SDL_SRCALPHA) | (src->flags & SDL_RLEACCEL);
dstalphaflags =
(dest->flags & SDL_SRCALPHA) | (dest->flags & SDL_RLEACCEL);
Jan 5, 2006
Jan 5, 2006
314
315
316
317
origsrcalphaflags = srcalphaflags;
origdstalphaflags = dstalphaflags;
srcalpha = src->format->alpha;
dstalpha = dest->format->alpha;
May 28, 2006
May 28, 2006
318
for (i = 1; i < argc; i++) {
Jan 5, 2006
Jan 5, 2006
319
320
const char *arg = argv[i];
May 29, 2006
May 29, 2006
321
322
323
324
325
if (strcmp(arg, "--srcalpha") == 0)
srcalpha = atoi(argv[++i]);
else if (strcmp(arg, "--dstalpha") == 0)
dstalpha = atoi(argv[++i]);
else if (strcmp(arg, "--srcsrcalpha") == 0)
Jan 5, 2006
Jan 5, 2006
326
srcalphaflags |= SDL_SRCALPHA;
May 29, 2006
May 29, 2006
327
else if (strcmp(arg, "--srcnosrcalpha") == 0)
Jan 5, 2006
Jan 5, 2006
328
srcalphaflags &= ~SDL_SRCALPHA;
May 29, 2006
May 29, 2006
329
else if (strcmp(arg, "--srcrleaccel") == 0)
Jan 5, 2006
Jan 5, 2006
330
srcalphaflags |= SDL_RLEACCEL;
May 29, 2006
May 29, 2006
331
else if (strcmp(arg, "--srcnorleaccel") == 0)
Jan 5, 2006
Jan 5, 2006
332
srcalphaflags &= ~SDL_RLEACCEL;
May 29, 2006
May 29, 2006
333
else if (strcmp(arg, "--dstsrcalpha") == 0)
Jan 5, 2006
Jan 5, 2006
334
dstalphaflags |= SDL_SRCALPHA;
May 29, 2006
May 29, 2006
335
else if (strcmp(arg, "--dstnosrcalpha") == 0)
Jan 5, 2006
Jan 5, 2006
336
dstalphaflags &= ~SDL_SRCALPHA;
May 29, 2006
May 29, 2006
337
else if (strcmp(arg, "--dstrleaccel") == 0)
Jan 5, 2006
Jan 5, 2006
338
dstalphaflags |= SDL_RLEACCEL;
May 29, 2006
May 29, 2006
339
else if (strcmp(arg, "--dstnorleaccel") == 0)
Jan 5, 2006
Jan 5, 2006
340
341
dstalphaflags &= ~SDL_RLEACCEL;
}
May 28, 2006
May 28, 2006
342
343
if ((dstalphaflags != origdstalphaflags)
|| (dstalpha != dest->format->alpha))
May 29, 2006
May 29, 2006
344
SDL_SetAlpha(dest, dstalphaflags, (Uint8) dstalpha);
May 28, 2006
May 28, 2006
345
346
if ((srcalphaflags != origsrcalphaflags)
|| (srcalpha != src->format->alpha))
May 29, 2006
May 29, 2006
347
SDL_SetAlpha(src, srcalphaflags, (Uint8) srcalpha);
Jan 5, 2006
Jan 5, 2006
348
349
/* set some sane defaults so we can see if the blit code is broken... */
May 29, 2006
May 29, 2006
350
351
SDL_FillRect(dest, NULL, SDL_MapRGB(dest->format, 0, 0, 0));
SDL_FillRect(src, NULL, SDL_MapRGB(src->format, 0, 0, 0));
May 29, 2006
May 29, 2006
353
354
blitCentered(src, bmp);
SDL_FreeSurface(bmp);
355
356
if (dumpfile)
May 29, 2006
May 29, 2006
357
SDL_SaveBMP(src, dumpfile); /* make sure initial convert is sane. */
May 29, 2006
May 29, 2006
359
output_details();
May 28, 2006
May 28, 2006
361
return (1);
May 28, 2006
May 28, 2006
365
static void
May 29, 2006
May 29, 2006
366
test_blit_speed(void)
May 29, 2006
May 29, 2006
368
Uint32 clearColor = SDL_MapRGB(dest->format, 0, 0, 0);
369
370
371
372
373
374
375
376
Uint32 iterations = 0;
Uint32 elasped = 0;
Uint32 end = 0;
Uint32 now = 0;
Uint32 last = 0;
int testms = testSeconds * 1000;
int wmax = (dest->w - src->w);
int hmax = (dest->h - src->h);
May 29, 2006
May 29, 2006
377
int isScreen = (SDL_GetVideoSurface() == dest);
378
379
SDL_Event event;
May 29, 2006
May 29, 2006
380
printf("Testing blit speed for %d seconds...\n", testSeconds);
May 29, 2006
May 29, 2006
382
now = SDL_GetTicks();
383
384
end = now + testms;
May 28, 2006
May 28, 2006
385
do {
386
/* pump the event queue occasionally to keep OS happy... */
May 28, 2006
May 28, 2006
387
if (now - last > 1000) {
388
last = now;
May 29, 2006
May 29, 2006
389
while (SDL_PollEvent(&event)) { /* no-op. */
May 28, 2006
May 28, 2006
390
}
391
392
393
}
iterations++;
May 29, 2006
May 29, 2006
394
elasped += blit(dest, src, randRange(0, wmax), randRange(0, hmax));
May 28, 2006
May 28, 2006
395
if (isScreen) {
May 29, 2006
May 29, 2006
396
397
SDL_Flip(dest); /* show it! */
SDL_FillRect(dest, NULL, clearColor); /* blank it for next time! */
May 29, 2006
May 29, 2006
400
now = SDL_GetTicks();
May 28, 2006
May 28, 2006
401
402
}
while (now < end);
May 29, 2006
May 29, 2006
404
405
printf("Non-blitting crap accounted for %d percent of this run.\n",
percent(testms - elasped, testms));
May 29, 2006
May 29, 2006
407
408
409
410
printf("%d blits took %d ms (%d fps).\n",
(int) iterations,
(int) elasped,
(int) (((float) iterations) / (((float) elasped) / 1000.0f)));
May 28, 2006
May 28, 2006
413
int
May 29, 2006
May 29, 2006
414
main(int argc, char **argv)
May 29, 2006
May 29, 2006
416
int initialized = setup_test(argc, argv);
May 28, 2006
May 28, 2006
417
if (initialized) {
May 29, 2006
May 29, 2006
418
419
test_blit_speed();
SDL_Quit();
May 28, 2006
May 28, 2006
421
return (!initialized);
422
423
424
}
/* end of testblitspeed.c ... */