mirror of
https://github.com/X11Libre/xserver.git
synced 2026-10-26 01:59:57 +00:00
colormap: code refactor for loops and simplify bit shift
Signed-off-by: Herman Semenoff <GermanAizek@yandex.ru>
This commit is contained in:
parent
9f913a3156
commit
8d728ba61f
1 changed files with 77 additions and 96 deletions
173
dix/colormap.c
173
dix/colormap.c
|
|
@ -298,9 +298,7 @@ dixCreateColormap(Colormap mid, ScreenPtr pScreen, VisualPtr pVisual,
|
|||
pmap->freeRed = size;
|
||||
memset((char *) pmap->red, 0, (int) sizebytes);
|
||||
memset((char *) pmap->numPixelsRed, 0, LimitClients * sizeof(int));
|
||||
for (Pixel **pptr = &pmap->clientPixelsRed[LimitClients];
|
||||
--pptr >= pmap->clientPixelsRed;)
|
||||
*pptr = (Pixel *) NULL;
|
||||
memset(pmap->clientPixelsRed, 0, LimitClients * sizeof(Pixel *));
|
||||
if (alloc == AllocAll) {
|
||||
if (class & DynamicClass)
|
||||
pmap->flags |= CM_AllAllocated;
|
||||
|
|
@ -610,7 +608,7 @@ CopyFree(int channel, int client, ColormapPtr pmapSrc, ColormapPtr pmapDst)
|
|||
|
||||
int nalloc = 0;
|
||||
if (pmapSrc->class & DynamicClass) {
|
||||
for (int z = npix; --z >= 0; ppix++) {
|
||||
for (int z = 0; z < npix; z++, ppix++) {
|
||||
/* Copy entries */
|
||||
EntryPtr pentSrc = pentSrcFirst + *ppix;
|
||||
EntryPtr pentDst = pentDstFirst + *ppix;
|
||||
|
|
@ -1435,10 +1433,8 @@ FreePixels(ColormapPtr pmap, int client)
|
|||
int class = pmap->class;
|
||||
Pixel *ppixStart = pmap->clientPixelsRed[client];
|
||||
if (class & DynamicClass) {
|
||||
int n = pmap->numPixelsRed[client];
|
||||
for (Pixel *ppix = ppixStart; --n >= 0;) {
|
||||
FreeCell(pmap, *ppix, REDMAP);
|
||||
ppix++;
|
||||
for (int i = 0; i < pmap->numPixelsRed[client]; i++) {
|
||||
FreeCell(pmap, ppixStart[i], REDMAP);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1448,9 +1444,8 @@ FreePixels(ColormapPtr pmap, int client)
|
|||
if ((class | DynamicClass) == DirectColor) {
|
||||
ppixStart = pmap->clientPixelsGreen[client];
|
||||
if (class & DynamicClass) {
|
||||
int n = pmap->numPixelsGreen[client];
|
||||
for (Pixel *ppix = ppixStart; --n >= 0;)
|
||||
FreeCell(pmap, *ppix++, GREENMAP);
|
||||
for (int i = 0; i < pmap->numPixelsGreen[client]; i++)
|
||||
FreeCell(pmap, ppixStart[i], GREENMAP);
|
||||
}
|
||||
free(ppixStart);
|
||||
pmap->clientPixelsGreen[client] = (Pixel *) NULL;
|
||||
|
|
@ -1458,9 +1453,8 @@ FreePixels(ColormapPtr pmap, int client)
|
|||
|
||||
ppixStart = pmap->clientPixelsBlue[client];
|
||||
if (class & DynamicClass) {
|
||||
int n = pmap->numPixelsBlue[client];
|
||||
for (Pixel* ppix = ppixStart; --n >= 0;)
|
||||
FreeCell(pmap, *ppix++, BLUEMAP);
|
||||
for (int i = 0; i < pmap->numPixelsBlue[client]; i++)
|
||||
FreeCell(pmap, ppixStart[i], BLUEMAP);
|
||||
}
|
||||
free(ppixStart);
|
||||
pmap->clientPixelsBlue[client] = (Pixel *) NULL;
|
||||
|
|
@ -1516,13 +1510,13 @@ AllocColorCells(ClientPtr pClient, ColormapPtr pmap, int colors, int planes,
|
|||
ok = AllocDirect(client, pmap, colors, planes, planes, planes,
|
||||
contig, ppix, &rmask, &gmask, &bmask);
|
||||
if (ok == Success) {
|
||||
for (int r = 1, g = 1, b = 1, n = planes; --n >= 0; r += r, g += g, b += b) {
|
||||
for (int i = 0, r = 1, g = 1, b = 1; i < planes; i++, r <<= 1, g <<= 1, b <<= 1) {
|
||||
while (!(rmask & r))
|
||||
r += r;
|
||||
r <<= 1;
|
||||
while (!(gmask & g))
|
||||
g += g;
|
||||
g <<= 1;
|
||||
while (!(bmask & b))
|
||||
b += b;
|
||||
b <<= 1;
|
||||
*masks++ = r | g | b;
|
||||
}
|
||||
}
|
||||
|
|
@ -1532,9 +1526,9 @@ AllocColorCells(ClientPtr pClient, ColormapPtr pmap, int colors, int planes,
|
|||
ok = AllocPseudo(client, pmap, colors, planes, contig, ppix, &rmask,
|
||||
&ppixFirst);
|
||||
if (ok == Success) {
|
||||
for (int r = 1, n = planes; --n >= 0; r += r) {
|
||||
for (int i = 0, r = 1; i < planes; i++, r <<= 1) {
|
||||
while (!(rmask & r))
|
||||
r += r;
|
||||
r <<= 1;
|
||||
*masks++ = r;
|
||||
}
|
||||
}
|
||||
|
|
@ -1593,19 +1587,19 @@ AllocColorPlanes(int client, ColormapPtr pmap, int colors,
|
|||
/* now split that mask into three */
|
||||
*prmask = *pgmask = *pbmask = 0;
|
||||
Pixel shift = 1;
|
||||
for (int i = r; --i >= 0; shift += shift) {
|
||||
for (int i = 0; i < r; i++, shift <<= 1) {
|
||||
while (!(mask & shift))
|
||||
shift += shift;
|
||||
shift <<= 1;
|
||||
*prmask |= shift;
|
||||
}
|
||||
for (int i = g; --i >= 0; shift += shift) {
|
||||
for (int i = 0; i < g; i++, shift <<= 1) {
|
||||
while (!(mask & shift))
|
||||
shift += shift;
|
||||
shift <<= 1;
|
||||
*pgmask |= shift;
|
||||
}
|
||||
for (int i = b; --i >= 0; shift += shift) {
|
||||
for (int i = 0; i < b; i++, shift <<= 1) {
|
||||
while (!(mask & shift))
|
||||
shift += shift;
|
||||
shift <<= 1;
|
||||
*pbmask |= shift;
|
||||
}
|
||||
|
||||
|
|
@ -1649,8 +1643,8 @@ AllocDirect(int client, ColormapPtr pmap, int c, int r, int g, int b,
|
|||
return BadAlloc;
|
||||
|
||||
/* start out with empty pixels */
|
||||
for (Pixel *p = pixels; p < pixels + c; p++)
|
||||
*p = 0;
|
||||
for (int i = 0; i < c; i++)
|
||||
pixels[i] = 0;
|
||||
|
||||
Pixel *ppixRed = calloc(npixR, sizeof(Pixel));
|
||||
Pixel *ppixGreen = calloc(npixG, sizeof(Pixel));
|
||||
|
|
@ -1688,19 +1682,16 @@ AllocDirect(int client, ColormapPtr pmap, int c, int r, int g, int b,
|
|||
|
||||
if (!okR || !okG || !okB || !rpix || !gpix || !bpix) {
|
||||
if (okR) {
|
||||
Pixel *ppix = ppixRed;
|
||||
for (int npix = npixR; --npix >= 0; ppix++)
|
||||
pmap->red[*ppix].refcnt = 0;
|
||||
for (int i = 0; i < npixR; i++)
|
||||
pmap->red[ppixRed[i]].refcnt = 0;
|
||||
}
|
||||
if (okG) {
|
||||
Pixel *ppix = ppixGreen;
|
||||
for (int npix = npixG; --npix >= 0; ppix++)
|
||||
pmap->green[*ppix].refcnt = 0;
|
||||
for (int i = 0; i < npixG; i++)
|
||||
pmap->green[ppixGreen[i]].refcnt = 0;
|
||||
}
|
||||
if (okB) {
|
||||
Pixel *ppix = ppixBlue;
|
||||
for (int npix = npixB; --npix >= 0; ppix++)
|
||||
pmap->blue[*ppix].refcnt = 0;
|
||||
for (int i = 0; i < npixB; i++)
|
||||
pmap->blue[ppixBlue[i]].refcnt = 0;
|
||||
}
|
||||
free(ppixBlue);
|
||||
free(ppixGreen);
|
||||
|
|
@ -1713,34 +1704,34 @@ AllocDirect(int client, ColormapPtr pmap, int c, int r, int g, int b,
|
|||
*pbmask <<= pmap->pVisual->offsetBlue;
|
||||
|
||||
Pixel *ppix = rpix + pmap->numPixelsRed[client];
|
||||
for (Pixel *pDst = pixels, *p = ppixRed; p < ppixRed + npixR; p++) {
|
||||
*ppix++ = *p;
|
||||
if (p < ppixRed + c)
|
||||
*pDst++ |= *p << pmap->pVisual->offsetRed;
|
||||
for (int i = 0; i < npixR; i++) {
|
||||
*ppix++ = ppixRed[i];
|
||||
if (i < c)
|
||||
pixels[i] |= ppixRed[i] << pmap->pVisual->offsetRed;
|
||||
}
|
||||
pmap->numPixelsRed[client] += npixR;
|
||||
pmap->freeRed -= npixR;
|
||||
|
||||
ppix = gpix + pmap->numPixelsGreen[client];
|
||||
for (Pixel *pDst = pixels, *p = ppixGreen; p < ppixGreen + npixG; p++) {
|
||||
*ppix++ = *p;
|
||||
if (p < ppixGreen + c)
|
||||
*pDst++ |= *p << pmap->pVisual->offsetGreen;
|
||||
for (int i = 0; i < npixG; i++) {
|
||||
*ppix++ = ppixGreen[i];
|
||||
if (i < c)
|
||||
pixels[i] |= ppixGreen[i] << pmap->pVisual->offsetGreen;
|
||||
}
|
||||
pmap->numPixelsGreen[client] += npixG;
|
||||
pmap->freeGreen -= npixG;
|
||||
|
||||
ppix = bpix + pmap->numPixelsBlue[client];
|
||||
for (Pixel *pDst = pixels, *p = ppixBlue; p < ppixBlue + npixB; p++) {
|
||||
*ppix++ = *p;
|
||||
if (p < ppixBlue + c)
|
||||
*pDst++ |= *p << pmap->pVisual->offsetBlue;
|
||||
for (int i = 0; i < npixB; i++) {
|
||||
*ppix++ = ppixBlue[i];
|
||||
if (i < c)
|
||||
pixels[i] |= ppixBlue[i] << pmap->pVisual->offsetBlue;
|
||||
}
|
||||
pmap->numPixelsBlue[client] += npixB;
|
||||
pmap->freeBlue -= npixB;
|
||||
|
||||
for (Pixel *pDst = pixels; pDst < pixels + c; pDst++)
|
||||
*pDst |= ALPHAMASK(pmap->pVisual);
|
||||
for (int i = 0; i < c; i++)
|
||||
pixels[i] |= ALPHAMASK(pmap->pVisual);
|
||||
|
||||
free(ppixBlue);
|
||||
free(ppixGreen);
|
||||
|
|
@ -1769,8 +1760,8 @@ AllocPseudo(int client, ColormapPtr pmap, int c, int r, Bool contig,
|
|||
Pixel *ppix = reallocarray(pmap->clientPixelsRed[client],
|
||||
pmap->numPixelsRed[client] + npix, sizeof(Pixel));
|
||||
if (!ppix) {
|
||||
for (Pixel *p = ppixTemp; p < ppixTemp + npix; p++)
|
||||
pmap->red[*p].refcnt = 0;
|
||||
for (int i = 0; i < npix; i++)
|
||||
pmap->red[ppixTemp[i]].refcnt = 0;
|
||||
free(ppixTemp);
|
||||
return BadAlloc;
|
||||
}
|
||||
|
|
@ -1778,10 +1769,10 @@ AllocPseudo(int client, ColormapPtr pmap, int c, int r, Bool contig,
|
|||
ppix += pmap->numPixelsRed[client];
|
||||
*pppixFirst = ppix;
|
||||
Pixel *pDst = pixels;
|
||||
for (Pixel *p = ppixTemp; p < ppixTemp + npix; p++) {
|
||||
*ppix++ = *p;
|
||||
if (p < ppixTemp + c)
|
||||
*pDst++ = *p;
|
||||
for (int i = 0; i < npix; i++) {
|
||||
*ppix++ = ppixTemp[i];
|
||||
if (i < c)
|
||||
*pDst++ = ppixTemp[i];
|
||||
}
|
||||
pmap->numPixelsRed[client] += npix;
|
||||
pmap->freeRed -= npix;
|
||||
|
|
@ -1963,9 +1954,8 @@ AllocShared(ColormapPtr pmap, Pixel * ppix, int c, int r, int g, int b,
|
|||
SHAREDCOLOR **psharedList = calloc(npixShared, sizeof(SHAREDCOLOR *));
|
||||
if (!psharedList)
|
||||
return FALSE;
|
||||
|
||||
SHAREDCOLOR **ppshared = psharedList;
|
||||
for (int z = npixShared; --z >= 0;) {
|
||||
for (int z = 0; z < npixShared; z++) {
|
||||
if (!(ppshared[z] = calloc(1, sizeof(SHAREDCOLOR)))) {
|
||||
for (z++; z < npixShared; z++)
|
||||
free(ppshared[z]);
|
||||
|
|
@ -1973,10 +1963,8 @@ AllocShared(ColormapPtr pmap, Pixel * ppix, int c, int r, int g, int b,
|
|||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
int npix;
|
||||
Pixel *pptr;
|
||||
for (pptr = ppix, npix = c; --npix >= 0; pptr++) {
|
||||
for (int i = 0; i < c; i++) {
|
||||
Pixel *pptr = &ppix[i];
|
||||
Pixel basemask = ~(gmask | bmask);
|
||||
Pixel common = *pptr & basemask;
|
||||
SHAREDCOLOR *pshared = NULL;
|
||||
|
|
@ -1986,11 +1974,10 @@ AllocShared(ColormapPtr pmap, Pixel * ppix, int c, int r, int g, int b,
|
|||
while (1) {
|
||||
pshared = *ppshared++;
|
||||
pshared->refcnt = 1 << (g + b);
|
||||
int z = npixClientNew;
|
||||
for (Pixel *cptr = ppixFirst; --z >= 0; cptr++) {
|
||||
if ((*cptr & basemask) == (common | bits)) {
|
||||
pmap->red[*cptr].fShared = TRUE;
|
||||
pmap->red[*cptr].co.shco.red = pshared;
|
||||
for (int z = 0; z < npixClientNew; z++) {
|
||||
if ((ppixFirst[z] & basemask) == (common | bits)) {
|
||||
pmap->red[ppixFirst[z]].fShared = TRUE;
|
||||
pmap->red[ppixFirst[z]].co.shco.red = pshared;
|
||||
}
|
||||
}
|
||||
GetNextBitsOrBreak(bits, rmask, base);
|
||||
|
|
@ -1999,11 +1986,10 @@ AllocShared(ColormapPtr pmap, Pixel * ppix, int c, int r, int g, int b,
|
|||
else {
|
||||
pshared = *ppshared++;
|
||||
pshared->refcnt = 1 << (g + b);
|
||||
int z = npixClientNew;
|
||||
for (Pixel *cptr = ppixFirst; --z >= 0; cptr++) {
|
||||
if ((*cptr & basemask) == common) {
|
||||
pmap->red[*cptr].fShared = TRUE;
|
||||
pmap->red[*cptr].co.shco.red = pshared;
|
||||
for (int z = 0; z < npixClientNew; z++) {
|
||||
if ((ppixFirst[z] & basemask) == common) {
|
||||
pmap->red[ppixFirst[z]].fShared = TRUE;
|
||||
pmap->red[ppixFirst[z]].co.shco.red = pshared;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -2015,10 +2001,9 @@ AllocShared(ColormapPtr pmap, Pixel * ppix, int c, int r, int g, int b,
|
|||
while (1) {
|
||||
pshared = *ppshared++;
|
||||
pshared->refcnt = 1 << (r + b);
|
||||
int z = npixClientNew;
|
||||
for (Pixel *cptr = ppixFirst; --z >= 0; cptr++) {
|
||||
if ((*cptr & basemask) == (common | bits)) {
|
||||
pmap->red[*cptr].co.shco.green = pshared;
|
||||
for (int z = 0; z < npixClientNew; z++) {
|
||||
if ((ppixFirst[z] & basemask) == (common | bits)) {
|
||||
pmap->red[ppixFirst[z]].co.shco.green = pshared;
|
||||
}
|
||||
}
|
||||
GetNextBitsOrBreak(bits, gmask, base);
|
||||
|
|
@ -2027,10 +2012,9 @@ AllocShared(ColormapPtr pmap, Pixel * ppix, int c, int r, int g, int b,
|
|||
else {
|
||||
pshared = *ppshared++;
|
||||
pshared->refcnt = 1 << (g + b);
|
||||
int z = npixClientNew;
|
||||
for (Pixel* cptr = ppixFirst; --z >= 0; cptr++) {
|
||||
if ((*cptr & basemask) == common) {
|
||||
pmap->red[*cptr].co.shco.green = pshared;
|
||||
for (int z = 0; z < npixClientNew; z++) {
|
||||
if ((ppixFirst[z] & basemask) == common) {
|
||||
pmap->red[ppixFirst[z]].co.shco.green = pshared;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -2042,10 +2026,9 @@ AllocShared(ColormapPtr pmap, Pixel * ppix, int c, int r, int g, int b,
|
|||
while (1) {
|
||||
pshared = *ppshared++;
|
||||
pshared->refcnt = 1 << (r + g);
|
||||
int z = npixClientNew;
|
||||
for (Pixel* cptr = ppixFirst; --z >= 0; cptr++) {
|
||||
if ((*cptr & basemask) == (common | bits)) {
|
||||
pmap->red[*cptr].co.shco.blue = pshared;
|
||||
for (int z = 0; z < npixClientNew; z++) {
|
||||
if ((ppixFirst[z] & basemask) == (common | bits)) {
|
||||
pmap->red[ppixFirst[z]].co.shco.blue = pshared;
|
||||
}
|
||||
}
|
||||
GetNextBitsOrBreak(bits, bmask, base);
|
||||
|
|
@ -2054,10 +2037,9 @@ AllocShared(ColormapPtr pmap, Pixel * ppix, int c, int r, int g, int b,
|
|||
else {
|
||||
pshared = *ppshared++;
|
||||
pshared->refcnt = 1 << (g + b);
|
||||
int z = npixClientNew;
|
||||
for (Pixel* cptr = ppixFirst; --z >= 0; cptr++) {
|
||||
if ((*cptr & basemask) == common) {
|
||||
pmap->red[*cptr].co.shco.blue = pshared;
|
||||
for (int z = 0; z < npixClientNew; z++) {
|
||||
if ((ppixFirst[z] & basemask) == common) {
|
||||
pmap->red[ppixFirst[z]].co.shco.blue = pshared;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -2173,8 +2155,8 @@ FreeCo(ColormapPtr pmap, int client, int color, int npixIn, Pixel * ppixIn,
|
|||
/* zap all pixels which match */
|
||||
while (1) {
|
||||
/* go through pixel list */
|
||||
Pixel *pptr = ppixIn;
|
||||
for (int n = npixIn; --n >= 0; pptr++) {
|
||||
for (int i = 0; i < npixIn; i++) {
|
||||
Pixel *pptr = &ppixIn[i];
|
||||
Pixel pixTest = ((*pptr | bits) & cmask) >> offset;
|
||||
if ((pixTest >= numents) || (*pptr & rgbbad)) {
|
||||
clients[client]->errorValue = *pptr | bits;
|
||||
|
|
@ -2267,10 +2249,9 @@ StoreColors(ColormapPtr pmap, int count, xColorItem * defs, ClientPtr client)
|
|||
int numgreen = NUMGREEN(pVisual);
|
||||
int numblue = NUMBLUE(pVisual);
|
||||
Pixel rgbbad = ~RGBMASK(pVisual);
|
||||
int n = 0;
|
||||
for (xColorItem *pdef = defs; n < count; pdef++, n++) {
|
||||
for (int n = 0; n < count; n++) {
|
||||
xColorItem *pdef = &defs[n];
|
||||
bool ok = TRUE;
|
||||
|
||||
(*pmap->pScreen->ResolveColor)
|
||||
(&pdef->red, &pdef->green, &pdef->blue, pmap->pVisual);
|
||||
|
||||
|
|
@ -2340,8 +2321,8 @@ StoreColors(ColormapPtr pmap, int count, xColorItem * defs, ClientPtr client)
|
|||
}
|
||||
}
|
||||
else {
|
||||
int n = 0;
|
||||
for (xColorItem *pdef = defs; n < count; pdef++, n++) {
|
||||
for (int n = 0; n < count; n++) {
|
||||
xColorItem *pdef = &defs[n];
|
||||
bool ok = TRUE;
|
||||
|
||||
if (pdef->pixel >= pVisual->ColormapEntries) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue