home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / SVGALIB / SVGALIB1.TAR / svgalib / demos / accel.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-22  |  5.4 KB  |  232 lines

  1.  
  2. /*
  3.  * This is only a tool to test the acceleration primitives.
  4.  * Do not use the primitives as library graphics functions;
  5.  * have higher level functions make use of acceleration
  6.  * primitives if available, otherwise using normal framebuffer code.
  7.  * For example, the vgagl should use acceleration when it can.
  8.  */
  9.  
  10. #include <stdlib.h>
  11. #include "vga.h"
  12. #include "vgagl.h"
  13.  
  14.  
  15. int vgamode;
  16. int background_blits;
  17.  
  18.  
  19. void Configure();
  20. void DrawDots();
  21. void DoAccelTest( void (*accelfunc)(), char *name, int exp_pixelrate,
  22.     int pixels_per_call );
  23. void FillBoxTest();
  24. void ScreenCopyTest();
  25. void ScrollTest();
  26. void FillBoxXORTest();
  27.  
  28.  
  29. void main() {
  30.     int accelfuncs;
  31.     vga_init();
  32.  
  33.     Configure();
  34.  
  35.     /* Getting accel info only works if the mode it set. */
  36.     vga_setmode(vgamode);
  37.  
  38.     accelfuncs = vga_ext_set(VGA_EXT_AVAILABLE, VGA_AVAIL_ACCEL);
  39.  
  40.     usleep(100000);
  41.     vga_setmode(TEXT);
  42.  
  43.     if (accelfuncs == 0) {
  44.         printf("No acceleration supported.\n");
  45.         exit(0);
  46.     }
  47.     printf("Accelflags: 0x%08X\n", accelfuncs);
  48.  
  49.     if (accelfuncs & ACCELFLAG_FILLBOX)
  50.         printf("FillBox supported.\n");
  51.     if (accelfuncs & ACCELFLAG_SCREENCOPY)
  52.         printf("ScreenCopy supported.\n");
  53.     if (accelfuncs & ACCELFLAG_PUTIMAGE)
  54.         printf("PutImage supported.\n");
  55.     if (accelfuncs & ACCELFLAG_DRAWLINE)
  56.         printf("DrawLine.\n");
  57.     if (accelfuncs & ACCELFLAG_SETFGCOLOR)
  58.         printf("SetFGColor supported.\n");
  59.     if (accelfuncs & ACCELFLAG_SETBGCOLOR)
  60.         printf("SetBGColor supported.\n");
  61.     if (accelfuncs & ACCELFLAG_SETTRANSPARENCY)
  62.         printf("SetTransparency supported.\n");
  63.     if (accelfuncs & ACCELFLAG_SETRASTEROP)
  64.         printf("SetRasterOp supported.\n");
  65.     if (accelfuncs & ACCELFLAG_PUTBITMAP)
  66.         printf("PutBitmap supported.\n");
  67.     if (accelfuncs & ACCELFLAG_SCREENCOPYBITMAP)
  68.         printf("ScreenCopyBitmap supported.\n");
  69.     if (accelfuncs & ACCELFLAG_DRAWHLINELIST)
  70.         printf("DrawHLineList supported.n");
  71.     if (accelfuncs & ACCELFLAG_SETMODE)
  72.         printf("SetMode supported.\n");
  73.     if (accelfuncs & ACCELFLAG_SYNC)
  74.         printf("Sync supported.\n");
  75.  
  76.     printf("\nPress enter to start test.\n");
  77.     getchar();
  78.  
  79.     vga_setmode(vgamode);
  80.     gl_setcontextvga(vgamode);
  81.  
  82.     background_blits = 0;
  83.     if (accelfuncs & ACCELFLAG_SYNC)
  84.         background_blits = 1;
  85.  
  86.     if (accelfuncs & ACCELFLAG_FILLBOX)
  87.         DoAccelTest(
  88.             FillBoxTest,
  89.             "FillBox",
  90.             50000000 / BYTESPERPIXEL,
  91.             WIDTH * HEIGHT * 256
  92.         );
  93.  
  94.     if (accelfuncs & ACCELFLAG_SCREENCOPY) {
  95.         DrawDots();
  96.         DoAccelTest(
  97.             ScreenCopyTest,
  98.             "ScreenCopy",
  99.             20000000 / BYTESPERPIXEL,
  100.             WIDTH * (HEIGHT / 3) * 3
  101.         );
  102.     }
  103.     if ((accelfuncs & ACCELFLAG_SCREENCOPY)
  104.     && (accelfuncs & ACCELFLAG_FILLBOX)) {
  105.         DrawDots();
  106.         DoAccelTest(
  107.             ScrollTest,
  108.             "Scroll Demo",
  109.             20000000 / BYTESPERPIXEL,
  110.             WIDTH * HEIGHT
  111.         );
  112.     }
  113.     if ((accelfuncs & ACCELFLAG_FILLBOX)
  114.     && (accelfuncs & ACCELFLAG_SETRASTEROP)) {
  115.         DrawDots();
  116.         DoAccelTest(
  117.             FillBoxXORTest,
  118.             "FillBox XOR",
  119.             30000000 / BYTESPERPIXEL,
  120.             WIDTH * HEIGHT * 256
  121.         );
  122.     }
  123.     vga_setmode(TEXT);
  124.     exit(-1);
  125. }
  126.  
  127.  
  128. void Configure() {
  129.     int allowed[GLASTMODE + 1];
  130.  
  131.     for (;;) {
  132.         int i;
  133.         int m;
  134.         for (i = G320x200x16; i <= GLASTMODE; i++) {
  135.             allowed[i] = 0;
  136.             if (vga_hasmode(i)
  137.             && vga_getmodeinfo(i)->bytesperpixel >= 1) {
  138.                 printf("%2d  %s\n", i, vga_getmodename(i));
  139.                 allowed[i] = 1;
  140.             }
  141.         }
  142.  
  143.         printf("\nWhich mode? ");
  144.         scanf("%d", &m);
  145.         getchar();
  146.         printf("\n");
  147.         if (m >= G320x200x16 && m <= GLASTMODE) {
  148.             vgamode = m;
  149.             break;
  150.         }
  151.     }
  152. }
  153.  
  154. void DrawDots() {
  155.     int i, n;
  156.     /* Fill the screen with random dots. */
  157.     gl_clearscreen(0);
  158.     n = WIDTH * HEIGHT / 100;    /* 1% filled. */
  159.     for (i = 0; i < n; i++)
  160.         gl_setpixel(rand() % WIDTH, rand() % HEIGHT, rand() % COLORS);
  161. }
  162.  
  163. void DoAccelTest( void (*accelfunc)(), char *name, int exp_pixelrate,
  164. int pixels_per_call ) {
  165.     int i, n, startclock, diffclock;
  166.     int rate, byterate;
  167.     if (exp_pixelrate < 0)
  168.         /* Special case, #iterations indicated. */
  169.         n = -exp_pixelrate;
  170.     else
  171.         /* Aim for about 5 seconds. */
  172.         n = exp_pixelrate * 5 / pixels_per_call + 1;
  173.     if (background_blits)
  174.         vga_accel(ACCEL_SETMODE, BLITS_IN_BACKGROUND);
  175.     startclock = clock();
  176.     for (i = 0; i < n; i++)
  177.         (*accelfunc)();
  178.     if (background_blits)
  179.         vga_accel(ACCEL_SYNC);
  180.     diffclock = clock() - startclock;
  181.     rate = (long long) n * pixels_per_call * 100 / diffclock;
  182.     byterate = rate * BYTESPERPIXEL;
  183.     printf("%s: %ld.%ld Mpixels/s (%ld.%ld Mbytes/s)\n", name,
  184.         rate / 1000000, (rate % 1000000) / 100000,
  185.         byterate / 1000000, (byterate % 1000000) / 100000);
  186. }
  187.  
  188. void FillBoxTest() {
  189.     int i;
  190.     for (i = 0; i < 256; i++) {
  191.         vga_accel(ACCEL_SETFGCOLOR, i);
  192.         vga_accel(ACCEL_FILLBOX, 0, 0, WIDTH, HEIGHT);
  193.     }
  194. }
  195.  
  196. void ScreenCopyTest() {
  197.     /* Copy first 1/3 to second 1/3. */
  198.     vga_accel(ACCEL_SCREENCOPY, 0, 0, 0, HEIGHT / 3, WIDTH, HEIGHT / 3);
  199.     /* Copy third 1/3 to first 1/3. */
  200.     vga_accel(ACCEL_SCREENCOPY, 0, (HEIGHT / 3) * 2, 0, 0,
  201.         WIDTH, HEIGHT / 3);
  202.     /* Copy second 1/3 to third 1/3. */
  203.     vga_accel(ACCEL_SCREENCOPY, 0, HEIGHT / 3, 0, (HEIGHT / 3) * 2,
  204.         WIDTH, HEIGHT / 3);
  205. }
  206.  
  207. void ScrollTest() {
  208.     int i;
  209.     /*
  210.      * First write the line that will be scrolled into the screen,
  211.      * located after the visible screen.
  212.      */
  213.     /* Clear the line. */
  214.     vga_accel(ACCEL_SETFGCOLOR, 0);
  215.      vga_accel(ACCEL_FILLBOX, 0, HEIGHT, WIDTH, 1);
  216.      if (background_blits)
  217.          vga_accel(ACCEL_SYNC);
  218.     /* Write some new dots. */
  219.     for (i = 0; i < WIDTH / 100; i++)
  220.         gl_setpixel(rand() % WIDTH, HEIGHT, rand() % COLORS);
  221.     /* Scroll. */
  222.     vga_accel(ACCEL_SCREENCOPY, 0, 1, 0, 0,
  223.         WIDTH, HEIGHT);
  224. }
  225.  
  226. void FillBoxXORTest() {
  227.     int i;
  228.     vga_accel(ACCEL_SETRASTEROP, ROP_XOR);
  229.     FillBoxTest();
  230.     vga_accel(ACCEL_SETRASTEROP, ROP_COPY);
  231. }
  232.