X-Git-Url: https://git.cweiske.de/enigma2.git/blobdiff_plain/d6f6602d7cea3a7899990fe79216af7d98d05917..acab01604673e3fb0d13e47f9ffb5c32e8d9dd07:/lib/gdi/gpixmap.cpp diff --git a/lib/gdi/gpixmap.cpp b/lib/gdi/gpixmap.cpp index b6711fcb..8fc26399 100644 --- a/lib/gdi/gpixmap.cpp +++ b/lib/gdi/gpixmap.cpp @@ -1,4 +1,6 @@ #include +#include +#include gLookup::gLookup() :size(0), lookup(0) @@ -47,48 +49,79 @@ void gLookup::build(int _size, const gPalette &pal, const gRGB &start, const gRG } } -gSurface::~gSurface() +gSurface::gSurface() { + x = 0; + y = 0; + bpp = 0; + bypp = 0; + stride = 0; + data = 0; + data_phys = 0; + clut.colors = 0; + clut.data = 0; + type = 0; } -gSurfaceSystem::gSurfaceSystem(eSize size, int _bpp) +gSurface::gSurface(eSize size, int _bpp, int accel) { - x=size.width(); - y=size.height(); - bpp=_bpp; + x = size.width(); + y = size.height(); + bpp = _bpp; + switch (bpp) { case 8: - bypp=1; + bypp = 1; break; case 15: case 16: - bypp=2; + bypp = 2; break; case 24: // never use 24bit mode case 32: - bypp=4; + bypp = 4; break; default: - bypp=(bpp+7)/8; + bypp = (bpp+7)/8; } - stride=x*bypp; - if (bpp==8) - { - clut.colors=256; - clut.data=new gRGB[clut.colors]; - } else + + stride = x*bypp; + + data = 0; + data_phys = 0; + + if (accel) { - clut.colors=0; - clut.data=0; + stride += 63; + stride &=~63; + + if (gAccel::getInstance()) + eDebug("accel memory: %d", gAccel::getInstance()->accelAlloc(data, data_phys, y * stride)); + else + eDebug("no accel available"); } - data=malloc(x*y*bypp); + + clut.colors = 0; + clut.data = 0; + + if (!data) + data = malloc(y * stride); + + type = 1; } -gSurfaceSystem::~gSurfaceSystem() +gSurface::~gSurface() { - free(data); - delete[] clut.data; + if (type) + { + if (data_phys) + gAccel::getInstance()->accelFree(data_phys); + else + free(data); + + delete[] clut.data; + } } gPixmap *gPixmap::lock() @@ -102,127 +135,208 @@ void gPixmap::unlock() contentlock.unlock(1); } -void gPixmap::fill(const eRect &area, const gColor &color) +void gPixmap::fill(const gRegion ®ion, const gColor &color) { - if ((area.height()<=0) || (area.width()<=0)) - return; - if (surface->bpp == 8) - for (int y=area.top(); ydata)+y*surface->stride+area.left(), color.color, area.width()); - else if (surface->bpp == 32) - for (int y=area.top(); ybpp == 8) + { + for (int y=area.top(); ydata)+y*surface->stride+area.left(), color.color, area.width()); + } else if (surface->bpp == 32) { - __u32 *dst=(__u32*)(((__u8*)surface->data)+y*surface->stride+area.left()*surface->bypp); - int x=area.width(); __u32 col; if (surface->clut.data && color < surface->clut.colors) col=(surface->clut.data[color].a<<24)|(surface->clut.data[color].r<<16)|(surface->clut.data[color].g<<8)|(surface->clut.data[color].b); else col=0x10101*color; + col^=0xFF000000; - while (x--) - *dst++=col; - } - else - eWarning("couldn't fill %d bpp", surface->bpp); -} -void gPixmap::blit(const gPixmap &src, ePoint pos, const eRect &clip, int flag) -{ - eRect area=eRect(pos, src.getSize()); - area&=clip; - area&=eRect(ePoint(0, 0), getSize()); - if ((area.width()<0) || (area.height()<0)) - return; + if (surface->data_phys && gAccel::getInstance()) + if (!gAccel::getInstance()->fill(surface, area, col)) + continue; - eRect srcarea=area; - srcarea.moveBy(-pos.x(), -pos.y()); + for (int y=area.top(); ydata)+y*surface->stride+area.left()*surface->bypp); + int x=area.width(); + while (x--) + *dst++=col; + } + } else + eWarning("couldn't fill %d bpp", surface->bpp); + } +} - if ((surface->bpp == 8) && (src.surface->bpp==8)) +void gPixmap::blit(const gPixmap &src, ePoint pos, const gRegion &clip, int flag) +{ + for (unsigned int i=0; idata; - __u8 *dstptr=(__u8*)surface->data; - - srcptr+=srcarea.left()*surface->bypp+srcarea.top()*src.surface->stride; - dstptr+=area.left()*surface->bypp+area.top()*surface->stride; - for (int y=0; ydata_phys && src.surface->data_phys) && (gAccel::getInstance())) + if (!gAccel::getInstance()->blit(surface, src.surface, area.topLeft(), srcarea, flag)) + continue; + flag &= ~ blitAlphaBlend; + + if ((surface->bpp == 8) && (src.surface->bpp==8)) { - if (flag & blitAlphaTest) + __u8 *srcptr=(__u8*)src.surface->data; + __u8 *dstptr=(__u8*)surface->data; + + srcptr+=srcarea.left()*src.surface->bypp+srcarea.top()*src.surface->stride; + dstptr+=area.left()*surface->bypp+area.top()*surface->stride; + for (int y=0; ybypp); - srcptr+=src.surface->stride; - dstptr+=surface->stride; - } - } else if ((surface->bpp == 32) && (src.surface->bpp==8)) - { - __u8 *srcptr=(__u8*)src.surface->data; - __u8 *dstptr=(__u8*)surface->data; // !! - __u32 pal[256]; - - for (int i=0; i<256; ++i) + if (!*src) + { + src++; + dst++; + } else + *dst++=*src++; + } + } else + memcpy(dstptr, srcptr, area.width()*surface->bypp); + srcptr+=src.surface->stride; + dstptr+=surface->stride; + } + } else if ((surface->bpp == 32) && (src.surface->bpp==32)) { - if (src.surface->clut.data && (iclut.colors)) - pal[i]=(src.surface->clut.data[i].a<<24)|(src.surface->clut.data[i].r<<16)|(src.surface->clut.data[i].g<<8)|(src.surface->clut.data[i].b); - else - pal[i]=0x010101*i; - pal[i]^=0xFF000000; - } + __u32 *srcptr=(__u32*)src.surface->data; + __u32 *dstptr=(__u32*)surface->data; - srcptr+=srcarea.left()*surface->bypp+srcarea.top()*src.surface->stride; - dstptr+=area.left()*surface->bypp+area.top()*surface->stride; - for (int y=0; ystride/4; + dstptr+=area.left()+area.top()*surface->stride/4; + for (int y=0; ybypp); + srcptr+=src.surface->stride/4; + dstptr+=surface->stride/4; + } + } else if ((surface->bpp == 32) && (src.surface->bpp==8)) + { + __u8 *srcptr=(__u8*)src.surface->data; + __u8 *dstptr=(__u8*)surface->data; // !! + __u32 pal[256]; + + for (int i=0; i<256; ++i) + { + if (src.surface->clut.data && (iclut.colors)) + pal[i]=(src.surface->clut.data[i].a<<24)|(src.surface->clut.data[i].r<<16)|(src.surface->clut.data[i].g<<8)|(src.surface->clut.data[i].b); + else + pal[i]=0x010101*i; + pal[i]^=0xFF000000; + } + + srcptr+=srcarea.left()*src.surface->bypp+srcarea.top()*src.surface->stride; + dstptr+=area.left()*surface->bypp+area.top()*surface->stride; + for (int y=0; ystride; + dstptr+=surface->stride; } - srcptr+=src.surface->stride; - dstptr+=surface->stride; - } - } else - eFatal("cannot blit %dbpp from %dbpp", surface->bpp, src.surface->bpp); + } else + eFatal("cannot blit %dbpp from %dbpp", surface->bpp, src.surface->bpp); + } } void gPixmap::mergePalette(const gPixmap &target) { if ((!surface->clut.colors) || (!target.surface->clut.colors)) return; + gColor *lookup=new gColor[surface->clut.colors]; for (int i=0; iclut.colors; i++) @@ -245,30 +359,120 @@ void gPixmap::mergePalette(const gPixmap &target) delete [] lookup; } -void gPixmap::line(ePoint start, ePoint dst, gColor color) +static inline int sgn(int a) { -int Ax=start.x(), -Ay=start.y(), Bx=dst.x(), -By=dst.y(); int dX, dY, fbXincr, -fbYincr, fbXYincr, dPr, dPru, P; __u8 -*AfbAddr = &((__u8*)surface->data)[Ay*surface->stride+Ax*surface->bypp]; __u8 -*BfbAddr = &((__u8*)surface->data)[By*surface->stride+Bx*surface->bypp]; fbXincr= -surface->bypp; if ( (dX=Bx-Ax) >= 0) goto AFTERNEGX; dX=-dX; -fbXincr=-1; AFTERNEGX: fbYincr=surface->stride; if ( (dY=By --Ay) >= 0) goto AFTERNEGY; fbYincr=-surface->stride; dY=-dY;AFTERNEGY: -fbXYincr = fbXincr+fbYincr; if (dY > dX) goto YisIndependent; dPr = dY+ -dY; P = -dX; dPru = P+P; dY = dX>>1; XLOOP: *AfbAddr=color; *BfbAddr=color; if ((P+=dPr) > 0) -goto RightAndUp; AfbAddr+=fbXincr; BfbAddr-=fbXincr; if ((dY=dY-1) > 0) goto XLOOP; *AfbAddr=color; if ((dX & 1) -== 0) return; *BfbAddr=color; return; RightAndUp: AfbAddr+=fbXYincr; BfbAddr-=fbXYincr; P+=dPru; if ((dY=dY-1) > -0) goto XLOOP; *AfbAddr=color; if ((dX & 1) == 0) return; *BfbAddr=color; return; YisIndependent: dPr = dX+dX; P -= -dY; dPru = P+P; dX = dY>>1; YLOOP: *AfbAddr=color; *BfbAddr=color; if ((P+=dPr) > 0) goto RightAndUp2; AfbAddr -+=fbYincr; BfbAddr-=fbYincr; if ((dX=dX-1) > 0) goto YLOOP; *AfbAddr=color; if ((dY & 1) == 0) return; *BfbAddr= -color;return; RightAndUp2: AfbAddr+=fbXYincr; BfbAddr-=fbXYincr; P+=dPru; if ((dX=dX-1) > 0) goto YLOOP; *AfbAddr -=color; if((dY & 1) == 0) return; *BfbAddr=color; return; + if (a < 0) + return -1; + else if (!a) + return 0; + else + return 1; +} + +void gPixmap::line(const gRegion &clip, ePoint start, ePoint dst, gColor color) +{ + __u8 *srf8 = 0; + __u32 *srf32 = 0; + int stride = surface->stride; + + if (clip.rects.empty()) + return; + + __u32 col; + if (surface->bpp == 8) + { + srf8 = (__u8*)surface->data; + } else if (surface->bpp == 32) + { + srf32 = (__u32*)surface->data; + + if (surface->clut.data && color < surface->clut.colors) + col=(surface->clut.data[color].a<<24)|(surface->clut.data[color].r<<16)|(surface->clut.data[color].g<<8)|(surface->clut.data[color].b); + else + col=0x10101*color; + col^=0xFF000000; + } + + int xa = start.x(), ya = start.y(), xb = dst.x(), yb = dst.y(); + int dx, dy, x, y, s1, s2, e, temp, swap, i; + dy=abs(yb-ya); + dx=abs(xb-xa); + s1=sgn(xb-xa); + s2=sgn(yb-ya); + x=xa; + y=ya; + if (dy>dx) + { + temp=dx; + dx=dy; + dy=temp; + swap=1; + } else + swap=0; + e = 2*dy-dx; + + int lasthit = 0; + for(i=1; i<=dx; i++) + { + /* i don't like this clipping loop, but the only */ + /* other choice i see is to calculate the intersections */ + /* before iterating through the pixels. */ + + /* one could optimize this because of the ordering */ + /* of the bands. */ + + lasthit = 0; + int a = lasthit; + + /* if last pixel was invisble, first check bounding box */ + if (a == -1) + { + /* check if we just got into the bbox again */ + if (clip.extends.contains(x, y)) + lasthit = a = 0; + else + goto fail; + } else if (!clip.rects[a].contains(x, y)) + { + do + { + ++a; + if (a == clip.rects.size()) + a = 0; + if (a == lasthit) + { + goto fail; + lasthit = -1; + } + } while (!clip.rects[a].contains(x, y)); + lasthit = a; + } + + if (srf8) + srf8[y * stride + x] = color; + if (srf32) + srf32[y * stride/4 + x] = col; +fail: + while (e>=0) + { + if (swap==1) x+=s1; + else y+=s2; + e-=2*dx; + } + if (swap==1) + y+=s2; + else + x+=s1; + e+=2*dy; + } } gColor gPalette::findColor(const gRGB &rgb) const { + /* grayscale? */ + if (!data) + return (rgb.r + rgb.g + rgb.b) / 3; + int difference=1<<30, best_choice=0; for (int t=0; t=difference) continue; + if (!ttd) + return t; difference=ttd; best_choice=t; } @@ -305,8 +511,8 @@ gPixmap::gPixmap(gSurface *surface): surface(surface) { } -gPixmap::gPixmap(eSize size, int bpp) +gPixmap::gPixmap(eSize size, int bpp, int accel) { - surface = new gSurfaceSystem(size, bpp); + surface = new gSurface(size, bpp, accel); }