tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

fix-gradient-clamp.patch (9511B)


      1 diff --git a/gfx/skia/src/effects/SkGradientShader.cpp b/gfx/skia/src/effects/SkGradientShader.cpp
      2 --- a/gfx/skia/src/effects/SkGradientShader.cpp
      3 +++ b/gfx/skia/src/effects/SkGradientShader.cpp
      4 @@ -167,16 +167,17 @@ private:
      5 
      6     mutable uint16_t*   fCache16;   // working ptr. If this is NULL, we need to recompute the cache values
      7     mutable SkPMColor*  fCache32;   // working ptr. If this is NULL, we need to recompute the cache values
      8 
      9     mutable uint16_t*   fCache16Storage;    // storage for fCache16, allocated on demand
     10     mutable SkMallocPixelRef* fCache32PixelRef;
     11     mutable unsigned    fCacheAlpha;        // the alpha value we used when we computed the cache. larger than 8bits so we can store uninitialized value
     12 
     13 +    static SkPMColor PremultiplyColor(SkColor c0, U8CPU alpha);
     14     static void Build16bitCache(uint16_t[], SkColor c0, SkColor c1, int count);
     15     static void Build32bitCache(SkPMColor[], SkColor c0, SkColor c1, int count,
     16                                 U8CPU alpha);
     17     void setCacheAlpha(U8CPU alpha) const;
     18     void initCommon();
     19 
     20     typedef SkShader INHERITED;
     21 };
     22 @@ -512,16 +513,31 @@ static inline U8CPU dither_fixed_to_8(Sk
     23  *  For dithering with premultiply, we want to ceiling the alpha component,
     24  *  to ensure that it is always >= any color component.
     25  */
     26 static inline U8CPU dither_ceil_fixed_to_8(SkFixed n) {
     27     n >>= 8;
     28     return ((n << 1) - (n | (n >> 8))) >> 8;
     29 }
     30 
     31 +SkPMColor Gradient_Shader::PremultiplyColor(SkColor c0, U8CPU paintAlpha)
     32 +{
     33 +    SkFixed a = SkMulDiv255Round(SkColorGetA(c0), paintAlpha);
     34 +    SkFixed r = SkColorGetR(c0);
     35 +    SkFixed g = SkColorGetG(c0);
     36 +    SkFixed b = SkColorGetB(c0);
     37 +    
     38 +    a = SkIntToFixed(a) + 0x8000;
     39 +    r = SkIntToFixed(r) + 0x8000;
     40 +    g = SkIntToFixed(g) + 0x8000;
     41 +    b = SkIntToFixed(b) + 0x8000;
     42 +        
     43 +    return SkPremultiplyARGBInline(a >> 16, r >> 16, g >> 16, b >> 16);
     44 +}
     45 +
     46 void Gradient_Shader::Build32bitCache(SkPMColor cache[], SkColor c0, SkColor c1,
     47                                       int count, U8CPU paintAlpha) {
     48     SkASSERT(count > 1);
     49 
     50     // need to apply paintAlpha to our two endpoints
     51     SkFixed a = SkMulDiv255Round(SkColorGetA(c0), paintAlpha);
     52     SkFixed da;
     53     {
     54 @@ -613,24 +629,24 @@ const uint16_t* Gradient_Shader::getCach
     55         }
     56     }
     57     return fCache16;
     58 }
     59 
     60 const SkPMColor* Gradient_Shader::getCache32() const {
     61     if (fCache32 == NULL) {
     62         // double the count for dither entries
     63 -        const int entryCount = kCache32Count * 2;
     64 +        const int entryCount = kCache32Count * 2 + 2;
     65         const size_t allocSize = sizeof(SkPMColor) * entryCount;
     66 
     67         if (NULL == fCache32PixelRef) {
     68             fCache32PixelRef = SkNEW_ARGS(SkMallocPixelRef,
     69                                           (NULL, allocSize, NULL));
     70         }
     71 -        fCache32 = (SkPMColor*)fCache32PixelRef->getAddr();
     72 +        fCache32 = (SkPMColor*)fCache32PixelRef->getAddr() + 1;
     73         if (fColorCount == 2) {
     74             Build32bitCache(fCache32, fOrigColors[0], fOrigColors[1],
     75                             kCache32Count, fCacheAlpha);
     76         } else {
     77             Rec* rec = fRecs;
     78             int prevIndex = 0;
     79             for (int i = 1; i < fColorCount; i++) {
     80                 int nextIndex = SkFixedToFFFF(rec[i].fPos) >> (16 - kCache32Bits);
     81 @@ -644,28 +660,31 @@ const SkPMColor* Gradient_Shader::getCac
     82             }
     83             SkASSERT(prevIndex == kCache32Count - 1);
     84         }
     85 
     86         if (fMapper) {
     87             SkMallocPixelRef* newPR = SkNEW_ARGS(SkMallocPixelRef,
     88                                                  (NULL, allocSize, NULL));
     89             SkPMColor* linear = fCache32;           // just computed linear data
     90 -            SkPMColor* mapped = (SkPMColor*)newPR->getAddr();    // storage for mapped data
     91 +            SkPMColor* mapped = (SkPMColor*)newPR->getAddr() + 1;    // storage for mapped data
     92             SkUnitMapper* map = fMapper;
     93             for (int i = 0; i < kCache32Count; i++) {
     94                 int index = map->mapUnit16((i << 8) | i) >> 8;
     95                 mapped[i] = linear[index];
     96                 mapped[i + kCache32Count] = linear[index + kCache32Count];
     97             }
     98             fCache32PixelRef->unref();
     99             fCache32PixelRef = newPR;
    100 -            fCache32 = (SkPMColor*)newPR->getAddr();
    101 +            fCache32 = (SkPMColor*)newPR->getAddr() + 1;
    102         }
    103     }
    104 +    //Write the clamp colours into the first and last entries of fCache32
    105 +    fCache32[-1] = PremultiplyColor(fOrigColors[0], fCacheAlpha);
    106 +    fCache32[kCache32Count * 2] = PremultiplyColor(fOrigColors[fColorCount - 1], fCacheAlpha);
    107     return fCache32;
    108 }
    109 
    110 /*
    111  *  Because our caller might rebuild the same (logically the same) gradient
    112  *  over and over, we'd like to return exactly the same "bitmap" if possible,
    113  *  allowing the client to utilize a cache of our bitmap (e.g. with a GPU).
    114  *  To do that, we maintain a private cache of built-bitmaps, based on our
    115 @@ -875,28 +894,38 @@ void Linear_Gradient::shadeSpan(int x, i
    116             dx = dxStorage[0];
    117         } else {
    118             SkASSERT(fDstToIndexClass == kLinear_MatrixClass);
    119             dx = SkScalarToFixed(fDstToIndex.getScaleX());
    120         }
    121 
    122         if (SkFixedNearlyZero(dx)) {
    123             // we're a vertical gradient, so no change in a span
    124 -            unsigned fi = proc(fx) >> (16 - kCache32Bits);
    125 -            sk_memset32_dither(dstC, cache[toggle + fi],
    126 -                                     cache[(toggle ^ TOGGLE_MASK) + fi], count);
    127 +            if (proc == clamp_tileproc) {
    128 +                if (fx < 0) {
    129 +                    sk_memset32(dstC, cache[-1], count);
    130 +                } else if (fx > 0xFFFF) {
    131 +                    sk_memset32(dstC, cache[kCache32Count * 2], count);
    132 +                } else {
    133 +                    unsigned fi = proc(fx) >> (16 - kCache32Bits);
    134 +                    sk_memset32_dither(dstC, cache[toggle + fi],
    135 +                                       cache[(toggle ^ TOGGLE_MASK) + fi], count);
    136 +                }
    137 +            } else {
    138 +                unsigned fi = proc(fx) >> (16 - kCache32Bits);
    139 +                sk_memset32_dither(dstC, cache[toggle + fi],
    140 +                                   cache[(toggle ^ TOGGLE_MASK) + fi], count);
    141 +            }
    142         } else if (proc == clamp_tileproc) {
    143             SkClampRange range;
    144 -            range.init(fx, dx, count, 0, 0xFF);
    145 +            range.init(fx, dx, count, cache[-1], cache[kCache32Count * 2]);
    146 
    147             if ((count = range.fCount0) > 0) {
    148 -                sk_memset32_dither(dstC,
    149 -                                   cache[toggle + range.fV0],
    150 -                                   cache[(toggle ^ TOGGLE_MASK) + range.fV0],
    151 -                                   count);
    152 +                 // Do we really want to dither the clamp values?
    153 +                 sk_memset32(dstC, range.fV0, count);
    154                 dstC += count;
    155             }
    156             if ((count = range.fCount1) > 0) {
    157                 int unroll = count >> 3;
    158                 fx = range.fFx1;
    159                 for (int i = 0; i < unroll; i++) {
    160                     NO_CHECK_ITER;  NO_CHECK_ITER;
    161                     NO_CHECK_ITER;  NO_CHECK_ITER;
    162 @@ -905,20 +934,17 @@ void Linear_Gradient::shadeSpan(int x, i
    163                 }
    164                 if ((count &= 7) > 0) {
    165                     do {
    166                         NO_CHECK_ITER;
    167                     } while (--count != 0);
    168                 }
    169             }
    170             if ((count = range.fCount2) > 0) {
    171 -                sk_memset32_dither(dstC,
    172 -                                   cache[toggle + range.fV1],
    173 -                                   cache[(toggle ^ TOGGLE_MASK) + range.fV1],
    174 -                                   count);
    175 +                sk_memset32(dstC, range.fV1, count);
    176             }
    177         } else if (proc == mirror_tileproc) {
    178             do {
    179                 unsigned fi = mirror_8bits(fx >> 8);
    180                 SkASSERT(fi <= 0xFF);
    181                 fx += dx;
    182                 *dstC++ = cache[toggle + fi];
    183                 toggle ^= TOGGLE_MASK;
    184 @@ -1670,19 +1699,24 @@ public:
    185             }
    186             SkScalar b = (SkScalarMul(fDiff.fX, fx) +
    187                          SkScalarMul(fDiff.fY, fy) - fStartRadius) * 2;
    188             SkScalar db = (SkScalarMul(fDiff.fX, dx) +
    189                           SkScalarMul(fDiff.fY, dy)) * 2;
    190             if (proc == clamp_tileproc) {
    191                 for (; count > 0; --count) {
    192                     SkFixed t = two_point_radial(b, fx, fy, fSr2D2, foura, fOneOverTwoA, posRoot);
    193 -                    SkFixed index = SkClampMax(t, 0xFFFF);
    194 -                    SkASSERT(index <= 0xFFFF);
    195 -                    *dstC++ = cache[index >> (16 - kCache32Bits)];
    196 +                    if (t < 0) {
    197 +                      *dstC++ = cache[-1];
    198 +                    } else if (t > 0xFFFF) {
    199 +                      *dstC++ = cache[kCache32Count * 2];
    200 +                    } else {
    201 +                      SkASSERT(t <= 0xFFFF);
    202 +                      *dstC++ = cache[t >> (16 - kCache32Bits)];
    203 +                    }
    204                     fx += dx;
    205                     fy += dy;
    206                     b += db;
    207                 }
    208             } else if (proc == mirror_tileproc) {
    209                 for (; count > 0; --count) {
    210                     SkFixed t = two_point_radial(b, fx, fy, fSr2D2, foura, fOneOverTwoA, posRoot);
    211                     SkFixed index = mirror_tileproc(t);