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