home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / x / xibm.zip / ppc / ppcCache.c < prev    next >
C/C++ Source or Header  |  1989-11-07  |  5KB  |  237 lines

  1. /*
  2.  * Copyright IBM Corporation 1987,1988,1989
  3.  *
  4.  * All Rights Reserved
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software and its
  7.  * documentation for any purpose and without fee is hereby granted,
  8.  * provided that the above copyright notice appear in all copies and that 
  9.  * both that copyright notice and this permission notice appear in
  10.  * supporting documentation, and that the name of IBM not be
  11.  * used in advertising or publicity pertaining to distribution of the
  12.  * software without specific, written prior permission.
  13.  *
  14.  * IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  15.  * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  16.  * IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  17.  * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  18.  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  19.  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  20.  * SOFTWARE.
  21.  *
  22. */
  23. /*
  24.  * ppcCache.c - generic font cache manager
  25.  */
  26.  
  27. #include "X.h"
  28. #include "scrnintstr.h"
  29. #include "ppcCache.h"
  30. #include "pixmapstr.h"
  31. #include "ppc.h"
  32.  
  33. void
  34. ppcCacheInit(pScreen)
  35.     ScreenPtr pScreen;
  36.     {
  37.     int i;
  38.     ppcScrnPrivPtr devPriv;
  39.     ppcCacheInfoPtr cacheInfo;
  40.     static int inited = 0;
  41.  
  42.     if ( inited )
  43.         return;
  44.  
  45.     inited = 1;
  46.  
  47.     devPriv = (ppcScrnPrivPtr) pScreen->devPrivate;
  48.     cacheInfo = devPriv->cacheInfo;
  49.  
  50.     for( i = 0; i < NUM_BAND_HEIGHTS; i++ )
  51.         cacheInfo->bandsByHeight[i] = (ppcBandInfoPtr)NULL;
  52.  
  53.     for( i = 0; i < cacheInfo->numPlanes; i++ )
  54.         {
  55.         (cacheInfo->planes[i]).bandListHead = NULL;
  56.         (cacheInfo->planes[i]).bandListTail = NULL;
  57.         (cacheInfo->planes[i]).nextY = 0;
  58.         }
  59.  
  60.     }
  61.  
  62.  
  63. /*
  64.  * newBand - create a new band and return a pointer to it
  65.  */
  66. static ppcBandInfoPtr
  67. newBand(cacheInfo, height)
  68.     ppcCacheInfoPtr cacheInfo;
  69.     int height;
  70.     {
  71.     ppcBandInfoPtr band;
  72.     int bandlist;
  73.     int i;
  74.  
  75.     if ( ( band = (ppcBandInfoPtr)Xalloc(sizeof(ppcBandInfo)) ) == NULL )
  76.         {
  77.         return((ppcBandInfoPtr)NULL);
  78.         }
  79.  
  80.     band->h = BAND_HEIGHT(height);
  81.  
  82.     /*
  83.      * look through the planes for one with room for this band
  84.      */
  85.     for( i = 0; i < cacheInfo->numPlanes; i++ )
  86.         {
  87.         register ppcPlaneInfoPtr planeInfo;
  88.         planeInfo = &(cacheInfo->planes[i]);
  89.         if ( ( band->h + planeInfo->nextY ) <= 
  90.                 planeInfo->h )
  91.             {
  92.  
  93.             /*
  94.              * init rest of band's fields
  95.              */
  96.             band->y = planeInfo->nextY;
  97.             planeInfo->nextY += band->h;
  98.             band->plane = i;
  99.             band->nextX = 0;
  100.             band->w = planeInfo->w;
  101.             band->bmList = (ppcBMInfoPtr)NULL;
  102.             band->bmListTail = (ppcBMInfoPtr)NULL;
  103.             band->nextBand = 
  104.                 band->nextBandByHeight = (ppcBandInfoPtr)NULL;
  105.             /*
  106.              * insert band on plane's list
  107.              */
  108.             if ( planeInfo->bandListTail == NULL )
  109.                 {
  110.                 planeInfo->bandListTail = 
  111.                         planeInfo->bandListHead = band;
  112.                 band->prevBand = (ppcBandInfoPtr)NULL;
  113.                 }
  114.             else
  115.                 {
  116.                 planeInfo->bandListTail->nextBand = band;
  117.                 band->prevBand = planeInfo->bandListTail;
  118.                 planeInfo->bandListTail = band;
  119.                 }
  120.  
  121.             /*
  122.              * insert onto the appropriate by-height band list
  123.              */
  124.             bandlist = BAND_LIST(band->h);
  125.  
  126.             if ( cacheInfo->bandsByHeightTail[bandlist] == NULL )
  127.                 {
  128.                 cacheInfo->bandsByHeightTail[bandlist] =
  129.                     cacheInfo->bandsByHeight[bandlist] =
  130.                     band;
  131.                 band->prevBandByHeight = (ppcBandInfoPtr)NULL;
  132.                 }
  133.             else
  134.                 {
  135.                 (cacheInfo->bandsByHeightTail[bandlist])->nextBandByHeight = band;
  136.                 band->prevBandByHeight = 
  137.                     cacheInfo->bandsByHeightTail[bandlist];
  138.                 cacheInfo->bandsByHeightTail[bandlist] = band;
  139.                 }
  140.  
  141.             return(band);
  142.             }
  143.         }
  144.  
  145.     /*
  146.      * If we get this far we didn't find any space for the band
  147.      */
  148.     Xfree(band);
  149.     return((ppcBandInfoPtr)NULL);
  150.  
  151.     }
  152.  
  153. /*
  154.  * allocate space for a new bitmap in the cache
  155.  */
  156. ppcBMInfoPtr
  157. newBM(cacheInfo, w, h)
  158.     ppcCacheInfoPtr cacheInfo;
  159.     int w, h;
  160.     {
  161.     ppcBandInfoPtr b;
  162.     ppcBMInfoPtr nbm;
  163.  
  164.     /*
  165.      * look through all bands of the correct height
  166.      */
  167.     for( b = cacheInfo->bandsByHeight[BAND_LIST(h)]; b != NULL; 
  168.                     b = b->nextBandByHeight )
  169.         {
  170.         if ( ( b->w - b->nextX ) >= w )
  171.             {
  172.  
  173.             if ( (nbm = (ppcBMInfoPtr)Xalloc(sizeof(ppcBMInfo))) 
  174.                             == (ppcBMInfoPtr)NULL)
  175.                 {
  176.                 return((ppcBMInfoPtr)NULL);
  177.                 }
  178.  
  179.             /* init nbm */
  180.             nbm->w = w;
  181.             nbm->x = b->nextX;
  182.             nbm->h = h;
  183.             nbm->y = b->y;
  184.             nbm->used = 1;
  185.  
  186.             /* update band */
  187.             b->nextX += w;
  188.  
  189.             /* now link nbm into used list */
  190.             if ( b->bmListTail == NULL )
  191.                 {
  192.                 b->bmList = nbm;
  193.                 b->bmListTail = nbm;
  194.                 nbm->next = nbm->prev = NULL;
  195.                 }
  196.             else 
  197.                 {
  198.                 nbm->next = NULL;
  199.                 nbm->prev = b->bmListTail;
  200.                 b->bmListTail->next = nbm;
  201.                 b->bmListTail = nbm;
  202.                 }
  203.             return(nbm);
  204.             }
  205.         }
  206.     /* didn't find enough space in any band, lets make a new one */
  207.     if ( ( b = newBand(cacheInfo, h) ) == NULL )
  208.         return((ppcBMInfoPtr)NULL);
  209.  
  210.     if ( (nbm = (ppcBMInfoPtr)Xalloc(sizeof(ppcBMInfo))) == 
  211.                             (ppcBMInfoPtr)NULL)
  212.         {
  213.         return((ppcBMInfoPtr)NULL);
  214.         }
  215.  
  216.     /* this code cheats because it knows that the new band is empty */
  217.  
  218.     /* init nbm */
  219.     nbm->w = w;
  220.     nbm->x = b->nextX;
  221.     nbm->h = h;
  222.     nbm->y = b->y;
  223.     nbm->used = 1;
  224.  
  225.     /* update band */
  226.     b->nextX += w;
  227.  
  228.     /* link into band */
  229.     b->bmList = nbm;
  230.     b->bmListTail = nbm;
  231.     nbm->next = nbm->prev = NULL;
  232.  
  233.     return(nbm);
  234.     
  235.     }
  236.  
  237.