gfx/skia/trunk/src/core/SkBitmapShaderTemplate.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/core/SkBitmapShaderTemplate.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,306 @@
     1.4 +
     1.5 +/*
     1.6 + * Copyright 2006 The Android Open Source Project
     1.7 + *
     1.8 + * Use of this source code is governed by a BSD-style license that can be
     1.9 + * found in the LICENSE file.
    1.10 + */
    1.11 +
    1.12 +
    1.13 +
    1.14 +#ifndef NOFILTER_BITMAP_SHADER_PREAMBLE
    1.15 +    #define NOFILTER_BITMAP_SHADER_PREAMBLE(bitmap, rb)
    1.16 +#endif
    1.17 +#ifndef NOFILTER_BITMAP_SHADER_POSTAMBLE
    1.18 +    #define NOFILTER_BITMAP_SHADER_POSTAMBLE(bitmap)
    1.19 +#endif
    1.20 +#ifndef NOFILTER_BITMAP_SHADER_PREAMBLE16
    1.21 +    #define NOFILTER_BITMAP_SHADER_PREAMBLE16(bitmap, rb)
    1.22 +#endif
    1.23 +#ifndef NOFILTER_BITMAP_SHADER_POSTAMBLE16
    1.24 +    #define NOFILTER_BITMAP_SHADER_POSTAMBLE16(bitmap)
    1.25 +#endif
    1.26 +
    1.27 +class NOFILTER_BITMAP_SHADER_CLASS : public HasSpan16_Sampler_BitmapShader {
    1.28 +public:
    1.29 +    NOFILTER_BITMAP_SHADER_CLASS(const SkBitmap& src)
    1.30 +        : HasSpan16_Sampler_BitmapShader(src, false,
    1.31 +                                         NOFILTER_BITMAP_SHADER_TILEMODE,
    1.32 +                                         NOFILTER_BITMAP_SHADER_TILEMODE)
    1.33 +    {
    1.34 +    }
    1.35 +
    1.36 +    virtual bool setContext(const SkBitmap& device, const SkPaint& paint, const SkMatrix& matrix)
    1.37 +    {
    1.38 +        if (!this->INHERITED::setContext(device, paint, matrix))
    1.39 +            return false;
    1.40 +
    1.41 +#ifdef NOFILTER_BITMAP_SHADER_USE_UNITINVERSE
    1.42 +        this->computeUnitInverse();
    1.43 +#endif
    1.44 +        return true;
    1.45 +    }
    1.46 +
    1.47 +    virtual void shadeSpan(int x, int y, SkPMColor dstC[], int count)
    1.48 +    {
    1.49 +        SkASSERT(count > 0);
    1.50 +
    1.51 +#ifdef NOFILTER_BITMAP_SHADER_SPRITEPROC32
    1.52 +        if ((this->getTotalInverse().getType() & ~SkMatrix::kTranslate_Mask) == 0)
    1.53 +        {
    1.54 +            NOFILTER_BITMAP_SHADER_SPRITEPROC32(this, x, y, dstC, count);
    1.55 +            return;
    1.56 +        }
    1.57 +#endif
    1.58 +
    1.59 +        unsigned        scale = SkAlpha255To256(this->getPaintAlpha());
    1.60 +#ifdef NOFILTER_BITMAP_SHADER_USE_UNITINVERSE
    1.61 +        const SkMatrix& inv = this->getUnitInverse();
    1.62 +        SkMatrix::MapPtProc invProc = this->getUnitInverseProc();
    1.63 +#else
    1.64 +        const SkMatrix& inv = this->getTotalInverse();
    1.65 +        SkMatrix::MapPtProc invProc = this->getInverseMapPtProc();
    1.66 +#endif
    1.67 +        const SkBitmap& srcBitmap = this->getSrcBitmap();
    1.68 +        unsigned        srcMaxX = srcBitmap.width() - 1;
    1.69 +        unsigned        srcMaxY = srcBitmap.height() - 1;
    1.70 +        unsigned        srcRB = srcBitmap.rowBytes();
    1.71 +        SkFixed         fx, fy, dx, dy;
    1.72 +
    1.73 +        const NOFILTER_BITMAP_SHADER_TYPE* srcPixels = (const NOFILTER_BITMAP_SHADER_TYPE*)srcBitmap.getPixels();
    1.74 +        NOFILTER_BITMAP_SHADER_PREAMBLE(srcBitmap, srcRB);
    1.75 +
    1.76 +        if (this->getInverseClass() == kPerspective_MatrixClass)
    1.77 +        {
    1.78 +            SkPerspIter   iter(inv, SkIntToScalar(x) + SK_ScalarHalf,
    1.79 +                                    SkIntToScalar(y) + SK_ScalarHalf, count);
    1.80 +            while ((count = iter.next()) != 0)
    1.81 +            {
    1.82 +                const SkFixed* srcXY = iter.getXY();
    1.83 +
    1.84 +/*  Do I need this?
    1.85 +#ifndef NOFILTER_BITMAP_SHADER_USE_UNITINVERSE
    1.86 +            fx >>= level;
    1.87 +            fy >>= level;
    1.88 +#endif
    1.89 +*/
    1.90 +                if (256 == scale)
    1.91 +                {
    1.92 +                    while (--count >= 0)
    1.93 +                    {
    1.94 +                        fx = *srcXY++;
    1.95 +                        fy = *srcXY++;
    1.96 +                        unsigned x = NOFILTER_BITMAP_SHADER_TILEPROC(fx, srcMaxX);
    1.97 +                        unsigned y = NOFILTER_BITMAP_SHADER_TILEPROC(fy, srcMaxY);
    1.98 +                        *dstC++ = NOFILTER_BITMAP_SHADER_SAMPLE_XY(srcPixels, x, y, srcRB);
    1.99 +                    }
   1.100 +                }
   1.101 +                else
   1.102 +                {
   1.103 +                    while (--count >= 0)
   1.104 +                    {
   1.105 +                        fx = *srcXY++;
   1.106 +                        fy = *srcXY++;
   1.107 +                        unsigned x = NOFILTER_BITMAP_SHADER_TILEPROC(fx, srcMaxX);
   1.108 +                        unsigned y = NOFILTER_BITMAP_SHADER_TILEPROC(fy, srcMaxY);
   1.109 +                        uint32_t c = NOFILTER_BITMAP_SHADER_SAMPLE_XY(srcPixels, x, y, srcRB);
   1.110 +                        *dstC++ = SkAlphaMulQ(c, scale);
   1.111 +                    }
   1.112 +                }
   1.113 +            }
   1.114 +            return;
   1.115 +        }
   1.116 +
   1.117 +        // now init fx, fy, dx, dy
   1.118 +        {
   1.119 +            SkPoint srcPt;
   1.120 +            invProc(inv, SkIntToScalar(x) + SK_ScalarHalf,
   1.121 +                         SkIntToScalar(y) + SK_ScalarHalf, &srcPt);
   1.122 +
   1.123 +            fx = SkScalarToFixed(srcPt.fX);
   1.124 +            fy = SkScalarToFixed(srcPt.fY);
   1.125 +
   1.126 +            if (this->getInverseClass() == kFixedStepInX_MatrixClass)
   1.127 +                (void)inv.fixedStepInX(SkIntToScalar(y), &dx, &dy);
   1.128 +            else
   1.129 +            {
   1.130 +                dx = SkScalarToFixed(inv.getScaleX());
   1.131 +                dy = SkScalarToFixed(inv.getSkewY());
   1.132 +            }
   1.133 +        }
   1.134 +
   1.135 +#ifndef NOFILTER_BITMAP_SHADER_USE_UNITINVERSE
   1.136 +        {   int level = this->getMipLevel() >> 16;
   1.137 +            fx >>= level;
   1.138 +            fy >>= level;
   1.139 +            dx >>= level;
   1.140 +            dy >>= level;
   1.141 +        }
   1.142 +#endif
   1.143 +
   1.144 +        if (dy == 0)
   1.145 +        {
   1.146 +            int y_index = NOFILTER_BITMAP_SHADER_TILEPROC(fy, srcMaxY);
   1.147 +//          SkDEBUGF(("fy = %g, srcMaxY = %d, y_index = %d\n", SkFixedToFloat(fy), srcMaxY, y_index));
   1.148 +            srcPixels = (const NOFILTER_BITMAP_SHADER_TYPE*)((const char*)srcPixels + y_index * srcRB);
   1.149 +            if (scale == 256)
   1.150 +                while (--count >= 0)
   1.151 +                {
   1.152 +                    unsigned x = NOFILTER_BITMAP_SHADER_TILEPROC(fx, srcMaxX);
   1.153 +                    fx += dx;
   1.154 +                    *dstC++ = NOFILTER_BITMAP_SHADER_SAMPLE_X(srcPixels, x);
   1.155 +                }
   1.156 +            else
   1.157 +                while (--count >= 0)
   1.158 +                {
   1.159 +                    unsigned x = NOFILTER_BITMAP_SHADER_TILEPROC(fx, srcMaxX);
   1.160 +                    SkPMColor c = NOFILTER_BITMAP_SHADER_SAMPLE_X(srcPixels, x);
   1.161 +                    fx += dx;
   1.162 +                    *dstC++ = SkAlphaMulQ(c, scale);
   1.163 +                }
   1.164 +        }
   1.165 +        else    // dy != 0
   1.166 +        {
   1.167 +            if (scale == 256)
   1.168 +                while (--count >= 0)
   1.169 +                {
   1.170 +                    unsigned x = NOFILTER_BITMAP_SHADER_TILEPROC(fx, srcMaxX);
   1.171 +                    unsigned y = NOFILTER_BITMAP_SHADER_TILEPROC(fy, srcMaxY);
   1.172 +                    fx += dx;
   1.173 +                    fy += dy;
   1.174 +                    *dstC++ = NOFILTER_BITMAP_SHADER_SAMPLE_XY(srcPixels, x, y, srcRB);
   1.175 +                }
   1.176 +            else
   1.177 +                while (--count >= 0)
   1.178 +                {
   1.179 +                    unsigned x = NOFILTER_BITMAP_SHADER_TILEPROC(fx, srcMaxX);
   1.180 +                    unsigned y = NOFILTER_BITMAP_SHADER_TILEPROC(fy, srcMaxY);
   1.181 +                    SkPMColor c = NOFILTER_BITMAP_SHADER_SAMPLE_XY(srcPixels, x, y, srcRB);
   1.182 +                    fx += dx;
   1.183 +                    fy += dy;
   1.184 +                    *dstC++ = SkAlphaMulQ(c, scale);
   1.185 +                }
   1.186 +        }
   1.187 +
   1.188 +        NOFILTER_BITMAP_SHADER_POSTAMBLE(srcBitmap);
   1.189 +    }
   1.190 +
   1.191 +    virtual void shadeSpan16(int x, int y, uint16_t dstC[], int count)
   1.192 +    {
   1.193 +        SkASSERT(count > 0);
   1.194 +        SkASSERT(this->getFlags() & SkShader::kHasSpan16_Flag);
   1.195 +
   1.196 +#ifdef NOFILTER_BITMAP_SHADER_SPRITEPROC16
   1.197 +        if ((this->getTotalInverse().getType() & ~SkMatrix::kTranslate_Mask) == 0)
   1.198 +        {
   1.199 +            NOFILTER_BITMAP_SHADER_SPRITEPROC16(this, x, y, dstC, count);
   1.200 +            return;
   1.201 +        }
   1.202 +#endif
   1.203 +
   1.204 +#ifdef NOFILTER_BITMAP_SHADER_USE_UNITINVERSE
   1.205 +        const SkMatrix& inv = this->getUnitInverse();
   1.206 +        SkMatrix::MapPtProc invProc = this->getUnitInverseProc();
   1.207 +#else
   1.208 +        const SkMatrix& inv = this->getTotalInverse();
   1.209 +        SkMatrix::MapPtProc invProc = this->getInverseMapPtProc();
   1.210 +#endif
   1.211 +        const SkBitmap& srcBitmap = this->getSrcBitmap();
   1.212 +        unsigned        srcMaxX = srcBitmap.width() - 1;
   1.213 +        unsigned        srcMaxY = srcBitmap.height() - 1;
   1.214 +        unsigned        srcRB = srcBitmap.rowBytes();
   1.215 +        SkFixed         fx, fy, dx, dy;
   1.216 +
   1.217 +        const NOFILTER_BITMAP_SHADER_TYPE* srcPixels = (const NOFILTER_BITMAP_SHADER_TYPE*)srcBitmap.getPixels();
   1.218 +        NOFILTER_BITMAP_SHADER_PREAMBLE16(srcBitmap, srcRB);
   1.219 +
   1.220 +        if (this->getInverseClass() == kPerspective_MatrixClass)
   1.221 +        {
   1.222 +            SkPerspIter   iter(inv, SkIntToScalar(x) + SK_ScalarHalf,
   1.223 +                                    SkIntToScalar(y) + SK_ScalarHalf, count);
   1.224 +            while ((count = iter.next()) != 0)
   1.225 +            {
   1.226 +                const SkFixed* srcXY = iter.getXY();
   1.227 +
   1.228 +                while (--count >= 0)
   1.229 +                {
   1.230 +                    fx = *srcXY++;
   1.231 +                    fy = *srcXY++;
   1.232 +                    unsigned x = NOFILTER_BITMAP_SHADER_TILEPROC(fx, srcMaxX);
   1.233 +                    unsigned y = NOFILTER_BITMAP_SHADER_TILEPROC(fy, srcMaxY);
   1.234 +                    *dstC++ = NOFILTER_BITMAP_SHADER_SAMPLE_XY16(srcPixels, x, y, srcRB);
   1.235 +                }
   1.236 +            }
   1.237 +            return;
   1.238 +        }
   1.239 +
   1.240 +        // now init fx, fy, dx, dy
   1.241 +        {
   1.242 +            SkPoint srcPt;
   1.243 +            invProc(inv, SkIntToScalar(x) + SK_ScalarHalf,
   1.244 +                         SkIntToScalar(y) + SK_ScalarHalf, &srcPt);
   1.245 +
   1.246 +            fx = SkScalarToFixed(srcPt.fX);
   1.247 +            fy = SkScalarToFixed(srcPt.fY);
   1.248 +
   1.249 +            if (this->getInverseClass() == kFixedStepInX_MatrixClass)
   1.250 +                (void)inv.fixedStepInX(SkIntToScalar(y), &dx, &dy);
   1.251 +            else
   1.252 +            {
   1.253 +                dx = SkScalarToFixed(inv.getScaleX());
   1.254 +                dy = SkScalarToFixed(inv.getSkewY());
   1.255 +            }
   1.256 +        }
   1.257 +
   1.258 +#ifndef NOFILTER_BITMAP_SHADER_USE_UNITINVERSE
   1.259 +        {   int level = this->getMipLevel() >> 16;
   1.260 +            fx >>= level;
   1.261 +            fy >>= level;
   1.262 +            dx >>= level;
   1.263 +            dy >>= level;
   1.264 +        }
   1.265 +#endif
   1.266 +
   1.267 +        if (dy == 0)
   1.268 +        {
   1.269 +            srcPixels = (const NOFILTER_BITMAP_SHADER_TYPE*)((const char*)srcPixels + NOFILTER_BITMAP_SHADER_TILEPROC(fy, srcMaxY) * srcRB);
   1.270 +            do {
   1.271 +                unsigned x = NOFILTER_BITMAP_SHADER_TILEPROC(fx, srcMaxX);
   1.272 +                fx += dx;
   1.273 +                *dstC++ = NOFILTER_BITMAP_SHADER_SAMPLE_X16(srcPixels, x);
   1.274 +            } while (--count != 0);
   1.275 +        }
   1.276 +        else    // dy != 0
   1.277 +        {
   1.278 +            do {
   1.279 +                unsigned x = NOFILTER_BITMAP_SHADER_TILEPROC(fx, srcMaxX);
   1.280 +                unsigned y = NOFILTER_BITMAP_SHADER_TILEPROC(fy, srcMaxY);
   1.281 +                fx += dx;
   1.282 +                fy += dy;
   1.283 +                *dstC++ = NOFILTER_BITMAP_SHADER_SAMPLE_XY16(srcPixels, x, y, srcRB);
   1.284 +            } while (--count != 0);
   1.285 +        }
   1.286 +
   1.287 +        NOFILTER_BITMAP_SHADER_POSTAMBLE16(srcBitmap);
   1.288 +    }
   1.289 +private:
   1.290 +    typedef HasSpan16_Sampler_BitmapShader INHERITED;
   1.291 +};
   1.292 +
   1.293 +#undef NOFILTER_BITMAP_SHADER_CLASS
   1.294 +#undef NOFILTER_BITMAP_SHADER_TYPE
   1.295 +#undef NOFILTER_BITMAP_SHADER_PREAMBLE
   1.296 +#undef NOFILTER_BITMAP_SHADER_POSTAMBLE
   1.297 +#undef NOFILTER_BITMAP_SHADER_SAMPLE_X      //(x)
   1.298 +#undef NOFILTER_BITMAP_SHADER_SAMPLE_XY     //(x, y, rowBytes)
   1.299 +#undef NOFILTER_BITMAP_SHADER_TILEMODE
   1.300 +#undef NOFILTER_BITMAP_SHADER_TILEPROC
   1.301 +
   1.302 +#undef NOFILTER_BITMAP_SHADER_PREAMBLE16
   1.303 +#undef NOFILTER_BITMAP_SHADER_POSTAMBLE16
   1.304 +#undef NOFILTER_BITMAP_SHADER_SAMPLE_X16        //(x)
   1.305 +#undef NOFILTER_BITMAP_SHADER_SAMPLE_XY16       //(x, y, rowBytes)
   1.306 +
   1.307 +#undef NOFILTER_BITMAP_SHADER_USE_UNITINVERSE
   1.308 +#undef NOFILTER_BITMAP_SHADER_SPRITEPROC16
   1.309 +#undef NOFILTER_BITMAP_SHADER_SPRITEPROC32

mercurial