test/testvidinfo.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 22 Jan 2012 17:21:00 -0500
changeset 6250 cce0ddcf6747
parent 5535 96594ac5fd1a
permissions -rw-r--r--
Updated to SDL 2.0, and SDL 2.0 can now be installed coexisting with SDL 1.2
     1 /*
     2   Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     3 
     4   This software is provided 'as-is', without any express or implied
     5   warranty.  In no event will the authors be held liable for any damages
     6   arising from the use of this software.
     7 
     8   Permission is granted to anyone to use this software for any purpose,
     9   including commercial applications, and to alter it and redistribute it
    10   freely.
    11 */
    12 
    13 /* Simple program -- figure out what kind of video display we have */
    14 
    15 #include <stdlib.h>
    16 #include <stdio.h>
    17 #include <stdlib.h>
    18 #include <string.h>
    19 
    20 #include "SDL.h"
    21 
    22 #define NUM_BLITS	10
    23 #define NUM_UPDATES	500
    24 
    25 #define FLAG_MASK	(SDL_HWSURFACE | SDL_FULLSCREEN | SDL_DOUBLEBUF | \
    26                          SDL_SRCCOLORKEY | SDL_SRCALPHA | SDL_RLEACCEL  | \
    27                          SDL_RLEACCELOK)
    28 
    29 #if 0
    30 void
    31 PrintFlags(Uint32 flags)
    32 {
    33     printf("0x%8.8x", (flags & FLAG_MASK));
    34     if (flags & SDL_HWSURFACE) {
    35         printf(" SDL_HWSURFACE");
    36     } else {
    37         printf(" SDL_SWSURFACE");
    38     }
    39     if (flags & SDL_FULLSCREEN) {
    40         printf(" | SDL_FULLSCREEN");
    41     }
    42     if (flags & SDL_DOUBLEBUF) {
    43         printf(" | SDL_DOUBLEBUF");
    44     }
    45     if (flags & SDL_SRCCOLORKEY) {
    46         printf(" | SDL_SRCCOLORKEY");
    47     }
    48     if (flags & SDL_SRCALPHA) {
    49         printf(" | SDL_SRCALPHA");
    50     }
    51     if (flags & SDL_RLEACCEL) {
    52         printf(" | SDL_RLEACCEL");
    53     }
    54     if (flags & SDL_RLEACCELOK) {
    55         printf(" | SDL_RLEACCELOK");
    56     }
    57 }
    58 
    59 int
    60 RunBlitTests(SDL_Surface * screen, SDL_Surface * bmp, int blitcount)
    61 {
    62     int i, j;
    63     int maxx;
    64     int maxy;
    65     SDL_Rect dst;
    66 
    67     maxx = (int) screen->w - bmp->w + 1;
    68     maxy = (int) screen->h - bmp->h + 1;
    69     for (i = 0; i < NUM_UPDATES; ++i) {
    70         for (j = 0; j < blitcount; ++j) {
    71             if (maxx) {
    72                 dst.x = rand() % maxx;
    73             } else {
    74                 dst.x = 0;
    75             }
    76             if (maxy) {
    77                 dst.y = rand() % maxy;
    78             } else {
    79                 dst.y = 0;
    80             }
    81             dst.w = bmp->w;
    82             dst.h = bmp->h;
    83             SDL_BlitSurface(bmp, NULL, screen, &dst);
    84         }
    85         SDL_Flip(screen);
    86     }
    87 
    88     return i;
    89 }
    90 
    91 int
    92 RunModeTests(SDL_Surface * screen)
    93 {
    94     Uint32 then, now;
    95     Uint32 frames;
    96     float seconds;
    97     int i;
    98     Uint8 r, g, b;
    99     SDL_Surface *bmp, *bmpcc, *tmp;
   100     SDL_Event event;
   101 
   102     while (SDL_PollEvent(&event)) {
   103         if (event.type == SDL_KEYDOWN)
   104             return 0;
   105     }
   106 
   107     /* First test fills and screen update speed */
   108     printf("Running color fill and fullscreen update test\n");
   109     then = SDL_GetTicks();
   110     frames = 0;
   111     for (i = 0; i < 256; ++i) {
   112         r = i;
   113         g = 0;
   114         b = 0;
   115         SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, r, g, b));
   116         SDL_Flip(screen);
   117         ++frames;
   118     }
   119     for (i = 0; i < 256; ++i) {
   120         r = 0;
   121         g = i;
   122         b = 0;
   123         SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, r, g, b));
   124         SDL_Flip(screen);
   125         ++frames;
   126     }
   127     for (i = 0; i < 256; ++i) {
   128         r = 0;
   129         g = 0;
   130         b = i;
   131         SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, r, g, b));
   132         SDL_Flip(screen);
   133         ++frames;
   134     }
   135     now = SDL_GetTicks();
   136     seconds = (float) (now - then) / 1000.0f;
   137     if (seconds > 0.0f) {
   138         printf("%d fills and flips in %2.2f seconds, %2.2f FPS\n", frames,
   139                seconds, (float) frames / seconds);
   140     } else {
   141         printf("%d fills and flips in zero seconds!n", frames);
   142     }
   143 
   144     /* clear the screen after fill test */
   145     SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
   146     SDL_Flip(screen);
   147 
   148     while (SDL_PollEvent(&event)) {
   149         if (event.type == SDL_KEYDOWN)
   150             return 0;
   151     }
   152 
   153     /* run the generic blit test */
   154     bmp = SDL_LoadBMP("sample.bmp");
   155     if (!bmp) {
   156         printf("Couldn't load sample.bmp: %s\n", SDL_GetError());
   157         return 0;
   158     }
   159     printf("Running freshly loaded blit test: %dx%d at %d bpp, flags: ",
   160            bmp->w, bmp->h, bmp->format->BitsPerPixel);
   161     PrintFlags(bmp->flags);
   162     printf("\n");
   163     then = SDL_GetTicks();
   164     frames = RunBlitTests(screen, bmp, NUM_BLITS);
   165     now = SDL_GetTicks();
   166     seconds = (float) (now - then) / 1000.0f;
   167     if (seconds > 0.0f) {
   168         printf("%d blits / %d updates in %2.2f seconds, %2.2f FPS\n",
   169                NUM_BLITS * frames, frames, seconds, (float) frames / seconds);
   170     } else {
   171         printf("%d blits / %d updates in zero seconds!\n",
   172                NUM_BLITS * frames, frames);
   173     }
   174 
   175     /* clear the screen after blit test */
   176     SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
   177     SDL_Flip(screen);
   178 
   179     while (SDL_PollEvent(&event)) {
   180         if (event.type == SDL_KEYDOWN)
   181             return 0;
   182     }
   183 
   184     /* run the colorkeyed blit test */
   185     bmpcc = SDL_LoadBMP("sample.bmp");
   186     if (!bmpcc) {
   187         printf("Couldn't load sample.bmp: %s\n", SDL_GetError());
   188         return 0;
   189     }
   190     printf("Running freshly loaded cc blit test: %dx%d at %d bpp, flags: ",
   191            bmpcc->w, bmpcc->h, bmpcc->format->BitsPerPixel);
   192     SDL_SetColorKey(bmpcc, SDL_SRCCOLORKEY | SDL_RLEACCEL,
   193                     *(Uint8 *) bmpcc->pixels);
   194 
   195     PrintFlags(bmpcc->flags);
   196     printf("\n");
   197     then = SDL_GetTicks();
   198     frames = RunBlitTests(screen, bmpcc, NUM_BLITS);
   199     now = SDL_GetTicks();
   200     seconds = (float) (now - then) / 1000.0f;
   201     if (seconds > 0.0f) {
   202         printf("%d cc blits / %d updates in %2.2f seconds, %2.2f FPS\n",
   203                NUM_BLITS * frames, frames, seconds, (float) frames / seconds);
   204     } else {
   205         printf("%d cc blits / %d updates in zero seconds!\n",
   206                NUM_BLITS * frames, frames);
   207     }
   208 
   209     /* clear the screen after cc blit test */
   210     SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
   211     SDL_Flip(screen);
   212 
   213     while (SDL_PollEvent(&event)) {
   214         if (event.type == SDL_KEYDOWN)
   215             return 0;
   216     }
   217 
   218     /* run the generic blit test */
   219     tmp = bmp;
   220     bmp = SDL_DisplayFormat(bmp);
   221     SDL_FreeSurface(tmp);
   222     if (!bmp) {
   223         printf("Couldn't convert sample.bmp: %s\n", SDL_GetError());
   224         return 0;
   225     }
   226     printf("Running display format blit test: %dx%d at %d bpp, flags: ",
   227            bmp->w, bmp->h, bmp->format->BitsPerPixel);
   228     PrintFlags(bmp->flags);
   229     printf("\n");
   230     then = SDL_GetTicks();
   231     frames = RunBlitTests(screen, bmp, NUM_BLITS);
   232     now = SDL_GetTicks();
   233     seconds = (float) (now - then) / 1000.0f;
   234     if (seconds > 0.0f) {
   235         printf("%d blits / %d updates in %2.2f seconds, %2.2f FPS\n",
   236                NUM_BLITS * frames, frames, seconds, (float) frames / seconds);
   237     } else {
   238         printf("%d blits / %d updates in zero seconds!\n",
   239                NUM_BLITS * frames, frames);
   240     }
   241 
   242     /* clear the screen after blit test */
   243     SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
   244     SDL_Flip(screen);
   245 
   246     while (SDL_PollEvent(&event)) {
   247         if (event.type == SDL_KEYDOWN)
   248             return 0;
   249     }
   250 
   251     /* run the colorkeyed blit test */
   252     tmp = bmpcc;
   253     bmpcc = SDL_DisplayFormat(bmpcc);
   254     SDL_FreeSurface(tmp);
   255     if (!bmpcc) {
   256         printf("Couldn't convert sample.bmp: %s\n", SDL_GetError());
   257         return 0;
   258     }
   259     printf("Running display format cc blit test: %dx%d at %d bpp, flags: ",
   260            bmpcc->w, bmpcc->h, bmpcc->format->BitsPerPixel);
   261     PrintFlags(bmpcc->flags);
   262     printf("\n");
   263     then = SDL_GetTicks();
   264     frames = RunBlitTests(screen, bmpcc, NUM_BLITS);
   265     now = SDL_GetTicks();
   266     seconds = (float) (now - then) / 1000.0f;
   267     if (seconds > 0.0f) {
   268         printf("%d cc blits / %d updates in %2.2f seconds, %2.2f FPS\n",
   269                NUM_BLITS * frames, frames, seconds, (float) frames / seconds);
   270     } else {
   271         printf("%d cc blits / %d updates in zero seconds!\n",
   272                NUM_BLITS * frames, frames);
   273     }
   274 
   275     /* clear the screen after cc blit test */
   276     SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
   277     SDL_Flip(screen);
   278 
   279     while (SDL_PollEvent(&event)) {
   280         if (event.type == SDL_KEYDOWN)
   281             return 0;
   282     }
   283 
   284     /* run the alpha blit test only if screen bpp>8 */
   285     if (bmp->format->BitsPerPixel > 8) {
   286         SDL_FreeSurface(bmp);
   287         bmp = SDL_LoadBMP("sample.bmp");
   288         SDL_SetAlpha(bmp, SDL_SRCALPHA, 85);    /* 85 - 33% alpha */
   289         tmp = bmp;
   290         bmp = SDL_DisplayFormat(bmp);
   291         SDL_FreeSurface(tmp);
   292         if (!bmp) {
   293             printf("Couldn't convert sample.bmp: %s\n", SDL_GetError());
   294             return 0;
   295         }
   296         printf
   297             ("Running display format alpha blit test: %dx%d at %d bpp, flags: ",
   298              bmp->w, bmp->h, bmp->format->BitsPerPixel);
   299         PrintFlags(bmp->flags);
   300         printf("\n");
   301         then = SDL_GetTicks();
   302         frames = RunBlitTests(screen, bmp, NUM_BLITS);
   303         now = SDL_GetTicks();
   304         seconds = (float) (now - then) / 1000.0f;
   305         if (seconds > 0.0f) {
   306             printf
   307                 ("%d alpha blits / %d updates in %2.2f seconds, %2.2f FPS\n",
   308                  NUM_BLITS * frames, frames, seconds,
   309                  (float) frames / seconds);
   310         } else {
   311             printf("%d alpha blits / %d updates in zero seconds!\n",
   312                    NUM_BLITS * frames, frames);
   313         }
   314     }
   315 
   316     /* clear the screen after alpha blit test */
   317     SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
   318     SDL_Flip(screen);
   319 
   320     while (SDL_PollEvent(&event)) {
   321         if (event.type == SDL_KEYDOWN)
   322             return 0;
   323     }
   324 
   325     /* run the cc+alpha blit test only if screen bpp>8 */
   326     if (bmp->format->BitsPerPixel > 8) {
   327         SDL_FreeSurface(bmpcc);
   328         bmpcc = SDL_LoadBMP("sample.bmp");
   329         SDL_SetAlpha(bmpcc, SDL_SRCALPHA, 85);  /* 85 - 33% alpha */
   330         SDL_SetColorKey(bmpcc, SDL_SRCCOLORKEY | SDL_RLEACCEL,
   331                         *(Uint8 *) bmpcc->pixels);
   332         tmp = bmpcc;
   333         bmpcc = SDL_DisplayFormat(bmpcc);
   334         SDL_FreeSurface(tmp);
   335         if (!bmpcc) {
   336             printf("Couldn't convert sample.bmp: %s\n", SDL_GetError());
   337             return 0;
   338         }
   339         printf
   340             ("Running display format cc+alpha blit test: %dx%d at %d bpp, flags: ",
   341              bmpcc->w, bmpcc->h, bmpcc->format->BitsPerPixel);
   342         PrintFlags(bmpcc->flags);
   343         printf("\n");
   344         then = SDL_GetTicks();
   345         frames = RunBlitTests(screen, bmpcc, NUM_BLITS);
   346         now = SDL_GetTicks();
   347         seconds = (float) (now - then) / 1000.0f;
   348         if (seconds > 0.0f) {
   349             printf
   350                 ("%d cc+alpha blits / %d updates in %2.2f seconds, %2.2f FPS\n",
   351                  NUM_BLITS * frames, frames, seconds,
   352                  (float) frames / seconds);
   353         } else {
   354             printf("%d cc+alpha blits / %d updates in zero seconds!\n",
   355                    NUM_BLITS * frames, frames);
   356         }
   357     }
   358 
   359     SDL_FreeSurface(bmpcc);
   360     SDL_FreeSurface(bmp);
   361 
   362     while (SDL_PollEvent(&event)) {
   363         if (event.type == SDL_KEYDOWN)
   364             return 0;
   365     }
   366     return 1;
   367 }
   368 
   369 void
   370 RunVideoTests()
   371 {
   372     static const struct
   373     {
   374         int w, h, bpp;
   375     } mode_list[] = {
   376         {
   377         640, 480, 8}, {
   378         640, 480, 16}, {
   379         640, 480, 32}, {
   380         800, 600, 8}, {
   381         800, 600, 16}, {
   382         800, 600, 32}, {
   383         1024, 768, 8}, {
   384         1024, 768, 16}, {
   385         1024, 768, 32}
   386     };
   387     static const Uint32 flags[] = {
   388         (SDL_SWSURFACE),
   389         (SDL_SWSURFACE | SDL_FULLSCREEN),
   390         (SDL_HWSURFACE | SDL_FULLSCREEN),
   391         (SDL_HWSURFACE | SDL_FULLSCREEN | SDL_DOUBLEBUF)
   392     };
   393     int i, j;
   394     SDL_Surface *screen;
   395 
   396     /* Test out several different video mode combinations */
   397     SDL_WM_SetCaption("SDL Video Benchmark", "vidtest");
   398     SDL_ShowCursor(0);
   399     for (i = 0; i < SDL_TABLESIZE(mode_list); ++i) {
   400         for (j = 0; j < SDL_TABLESIZE(flags); ++j) {
   401             printf("===================================\n");
   402             printf("Setting video mode: %dx%d at %d bpp, flags: ",
   403                    mode_list[i].w, mode_list[i].h, mode_list[i].bpp);
   404             PrintFlags(flags[j]);
   405             printf("\n");
   406             screen = SDL_SetVideoMode(mode_list[i].w,
   407                                       mode_list[i].h,
   408                                       mode_list[i].bpp, flags[j]);
   409             if (!screen) {
   410                 printf("Setting video mode failed: %s\n", SDL_GetError());
   411                 continue;
   412             }
   413             if ((screen->flags & FLAG_MASK) != flags[j]) {
   414                 printf("Flags didn't match: ");
   415                 PrintFlags(screen->flags);
   416                 printf("\n");
   417                 continue;
   418             }
   419             if (!RunModeTests(screen)) {
   420                 return;
   421             }
   422         }
   423     }
   424 }
   425 #endif
   426 
   427 int
   428 main(int argc, char *argv[])
   429 {
   430     const SDL_VideoInfo *info;
   431     int i, d, n;
   432     const char *driver;
   433     SDL_DisplayMode mode;
   434     int bpp;
   435     Uint32 Rmask, Gmask, Bmask, Amask;
   436     int nmodes;
   437 
   438     /* Print available video drivers */
   439     n = SDL_GetNumVideoDrivers();
   440     if (n == 0) {
   441         printf("No built-in video drivers\n");
   442     } else {
   443         printf("Built-in video drivers:");
   444         for (i = 0; i < n; ++i) {
   445             if (i > 0) {
   446                 printf(",");
   447             }
   448             printf(" %s", SDL_GetVideoDriver(i));
   449         }
   450         printf("\n");
   451     }
   452 
   453     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   454         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   455         exit(1);
   456     }
   457     driver = SDL_GetCurrentVideoDriver();
   458     if (driver) {
   459         printf("Video driver: %s\n", driver);
   460     }
   461     printf("Number of displays: %d\n", SDL_GetNumVideoDisplays());
   462     for (d = 0; d < SDL_GetNumVideoDisplays(); ++d) {
   463         SDL_Rect bounds;
   464 
   465         SDL_GetDisplayBounds(d, &bounds);
   466         printf("Display %d: %dx%d at %d,%d\n", d,
   467                bounds.w, bounds.h, bounds.x, bounds.y);
   468 
   469         SDL_GetDesktopDisplayMode(d, &mode);
   470         SDL_PixelFormatEnumToMasks(mode.format, &bpp, &Rmask, &Gmask, &Bmask,
   471                                    &Amask);
   472         printf("  Current mode: %dx%d@%dHz, %d bits-per-pixel\n", mode.w,
   473                mode.h, mode.refresh_rate, bpp);
   474         if (Rmask || Gmask || Bmask) {
   475             printf("      Red Mask = 0x%.8x\n", Rmask);
   476             printf("      Green Mask = 0x%.8x\n", Gmask);
   477             printf("      Blue Mask = 0x%.8x\n", Bmask);
   478             if (Amask)
   479                 printf("      Alpha Mask = 0x%.8x\n", Amask);
   480         }
   481 
   482         /* Print available fullscreen video modes */
   483         nmodes = SDL_GetNumDisplayModes(d);
   484         if (nmodes == 0) {
   485             printf("No available fullscreen video modes\n");
   486         } else {
   487             printf("  Fullscreen video modes:\n");
   488             for (i = 0; i < nmodes; ++i) {
   489                 SDL_GetDisplayMode(d, i, &mode);
   490                 SDL_PixelFormatEnumToMasks(mode.format, &bpp, &Rmask,
   491                                            &Gmask, &Bmask, &Amask);
   492                 printf("    Mode %d: %dx%d@%dHz, %d bits-per-pixel\n", i,
   493                        mode.w, mode.h, mode.refresh_rate, bpp);
   494                 if (Rmask || Gmask || Bmask) {
   495                     printf("        Red Mask = 0x%.8x\n", Rmask);
   496                     printf("        Green Mask = 0x%.8x\n", Gmask);
   497                     printf("        Blue Mask = 0x%.8x\n", Bmask);
   498                     if (Amask)
   499                         printf("        Alpha Mask = 0x%.8x\n", Amask);
   500                 }
   501             }
   502         }
   503     }
   504 
   505     info = SDL_GetVideoInfo();
   506     if (info->wm_available) {
   507         printf("A window manager is available\n");
   508     }
   509     if (info->hw_available) {
   510         printf("Hardware surfaces are available (%dK video memory)\n",
   511                info->video_mem);
   512     }
   513     if (info->blit_hw) {
   514         printf("Copy blits between hardware surfaces are accelerated\n");
   515     }
   516     if (info->blit_hw_CC) {
   517         printf("Colorkey blits between hardware surfaces are accelerated\n");
   518     }
   519     if (info->blit_hw_A) {
   520         printf("Alpha blits between hardware surfaces are accelerated\n");
   521     }
   522     if (info->blit_sw) {
   523         printf
   524             ("Copy blits from software surfaces to hardware surfaces are accelerated\n");
   525     }
   526     if (info->blit_sw_CC) {
   527         printf
   528             ("Colorkey blits from software surfaces to hardware surfaces are accelerated\n");
   529     }
   530     if (info->blit_sw_A) {
   531         printf
   532             ("Alpha blits from software surfaces to hardware surfaces are accelerated\n");
   533     }
   534     if (info->blit_fill) {
   535         printf("Color fills on hardware surfaces are accelerated\n");
   536     }
   537     printf("Current resolution: %dx%d\n", info->current_w, info->current_h);
   538 #if 0
   539     if (argv[1] && (strcmp(argv[1], "-benchmark") == 0)) {
   540         RunVideoTests();
   541     }
   542 #endif
   543 
   544     SDL_Quit();
   545     return (0);
   546 }