1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/patches/archive/fix-gradient-clamp.patch Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,211 @@ 1.4 +diff --git a/gfx/skia/src/effects/SkGradientShader.cpp b/gfx/skia/src/effects/SkGradientShader.cpp 1.5 +--- a/gfx/skia/src/effects/SkGradientShader.cpp 1.6 ++++ b/gfx/skia/src/effects/SkGradientShader.cpp 1.7 +@@ -167,16 +167,17 @@ private: 1.8 + 1.9 + mutable uint16_t* fCache16; // working ptr. If this is NULL, we need to recompute the cache values 1.10 + mutable SkPMColor* fCache32; // working ptr. If this is NULL, we need to recompute the cache values 1.11 + 1.12 + mutable uint16_t* fCache16Storage; // storage for fCache16, allocated on demand 1.13 + mutable SkMallocPixelRef* fCache32PixelRef; 1.14 + mutable unsigned fCacheAlpha; // the alpha value we used when we computed the cache. larger than 8bits so we can store uninitialized value 1.15 + 1.16 ++ static SkPMColor PremultiplyColor(SkColor c0, U8CPU alpha); 1.17 + static void Build16bitCache(uint16_t[], SkColor c0, SkColor c1, int count); 1.18 + static void Build32bitCache(SkPMColor[], SkColor c0, SkColor c1, int count, 1.19 + U8CPU alpha); 1.20 + void setCacheAlpha(U8CPU alpha) const; 1.21 + void initCommon(); 1.22 + 1.23 + typedef SkShader INHERITED; 1.24 + }; 1.25 +@@ -512,16 +513,31 @@ static inline U8CPU dither_fixed_to_8(Sk 1.26 + * For dithering with premultiply, we want to ceiling the alpha component, 1.27 + * to ensure that it is always >= any color component. 1.28 + */ 1.29 + static inline U8CPU dither_ceil_fixed_to_8(SkFixed n) { 1.30 + n >>= 8; 1.31 + return ((n << 1) - (n | (n >> 8))) >> 8; 1.32 + } 1.33 + 1.34 ++SkPMColor Gradient_Shader::PremultiplyColor(SkColor c0, U8CPU paintAlpha) 1.35 ++{ 1.36 ++ SkFixed a = SkMulDiv255Round(SkColorGetA(c0), paintAlpha); 1.37 ++ SkFixed r = SkColorGetR(c0); 1.38 ++ SkFixed g = SkColorGetG(c0); 1.39 ++ SkFixed b = SkColorGetB(c0); 1.40 ++ 1.41 ++ a = SkIntToFixed(a) + 0x8000; 1.42 ++ r = SkIntToFixed(r) + 0x8000; 1.43 ++ g = SkIntToFixed(g) + 0x8000; 1.44 ++ b = SkIntToFixed(b) + 0x8000; 1.45 ++ 1.46 ++ return SkPremultiplyARGBInline(a >> 16, r >> 16, g >> 16, b >> 16); 1.47 ++} 1.48 ++ 1.49 + void Gradient_Shader::Build32bitCache(SkPMColor cache[], SkColor c0, SkColor c1, 1.50 + int count, U8CPU paintAlpha) { 1.51 + SkASSERT(count > 1); 1.52 + 1.53 + // need to apply paintAlpha to our two endpoints 1.54 + SkFixed a = SkMulDiv255Round(SkColorGetA(c0), paintAlpha); 1.55 + SkFixed da; 1.56 + { 1.57 +@@ -613,24 +629,24 @@ const uint16_t* Gradient_Shader::getCach 1.58 + } 1.59 + } 1.60 + return fCache16; 1.61 + } 1.62 + 1.63 + const SkPMColor* Gradient_Shader::getCache32() const { 1.64 + if (fCache32 == NULL) { 1.65 + // double the count for dither entries 1.66 +- const int entryCount = kCache32Count * 2; 1.67 ++ const int entryCount = kCache32Count * 2 + 2; 1.68 + const size_t allocSize = sizeof(SkPMColor) * entryCount; 1.69 + 1.70 + if (NULL == fCache32PixelRef) { 1.71 + fCache32PixelRef = SkNEW_ARGS(SkMallocPixelRef, 1.72 + (NULL, allocSize, NULL)); 1.73 + } 1.74 +- fCache32 = (SkPMColor*)fCache32PixelRef->getAddr(); 1.75 ++ fCache32 = (SkPMColor*)fCache32PixelRef->getAddr() + 1; 1.76 + if (fColorCount == 2) { 1.77 + Build32bitCache(fCache32, fOrigColors[0], fOrigColors[1], 1.78 + kCache32Count, fCacheAlpha); 1.79 + } else { 1.80 + Rec* rec = fRecs; 1.81 + int prevIndex = 0; 1.82 + for (int i = 1; i < fColorCount; i++) { 1.83 + int nextIndex = SkFixedToFFFF(rec[i].fPos) >> (16 - kCache32Bits); 1.84 +@@ -644,28 +660,31 @@ const SkPMColor* Gradient_Shader::getCac 1.85 + } 1.86 + SkASSERT(prevIndex == kCache32Count - 1); 1.87 + } 1.88 + 1.89 + if (fMapper) { 1.90 + SkMallocPixelRef* newPR = SkNEW_ARGS(SkMallocPixelRef, 1.91 + (NULL, allocSize, NULL)); 1.92 + SkPMColor* linear = fCache32; // just computed linear data 1.93 +- SkPMColor* mapped = (SkPMColor*)newPR->getAddr(); // storage for mapped data 1.94 ++ SkPMColor* mapped = (SkPMColor*)newPR->getAddr() + 1; // storage for mapped data 1.95 + SkUnitMapper* map = fMapper; 1.96 + for (int i = 0; i < kCache32Count; i++) { 1.97 + int index = map->mapUnit16((i << 8) | i) >> 8; 1.98 + mapped[i] = linear[index]; 1.99 + mapped[i + kCache32Count] = linear[index + kCache32Count]; 1.100 + } 1.101 + fCache32PixelRef->unref(); 1.102 + fCache32PixelRef = newPR; 1.103 +- fCache32 = (SkPMColor*)newPR->getAddr(); 1.104 ++ fCache32 = (SkPMColor*)newPR->getAddr() + 1; 1.105 + } 1.106 + } 1.107 ++ //Write the clamp colours into the first and last entries of fCache32 1.108 ++ fCache32[-1] = PremultiplyColor(fOrigColors[0], fCacheAlpha); 1.109 ++ fCache32[kCache32Count * 2] = PremultiplyColor(fOrigColors[fColorCount - 1], fCacheAlpha); 1.110 + return fCache32; 1.111 + } 1.112 + 1.113 + /* 1.114 + * Because our caller might rebuild the same (logically the same) gradient 1.115 + * over and over, we'd like to return exactly the same "bitmap" if possible, 1.116 + * allowing the client to utilize a cache of our bitmap (e.g. with a GPU). 1.117 + * To do that, we maintain a private cache of built-bitmaps, based on our 1.118 +@@ -875,28 +894,38 @@ void Linear_Gradient::shadeSpan(int x, i 1.119 + dx = dxStorage[0]; 1.120 + } else { 1.121 + SkASSERT(fDstToIndexClass == kLinear_MatrixClass); 1.122 + dx = SkScalarToFixed(fDstToIndex.getScaleX()); 1.123 + } 1.124 + 1.125 + if (SkFixedNearlyZero(dx)) { 1.126 + // we're a vertical gradient, so no change in a span 1.127 +- unsigned fi = proc(fx) >> (16 - kCache32Bits); 1.128 +- sk_memset32_dither(dstC, cache[toggle + fi], 1.129 +- cache[(toggle ^ TOGGLE_MASK) + fi], count); 1.130 ++ if (proc == clamp_tileproc) { 1.131 ++ if (fx < 0) { 1.132 ++ sk_memset32(dstC, cache[-1], count); 1.133 ++ } else if (fx > 0xFFFF) { 1.134 ++ sk_memset32(dstC, cache[kCache32Count * 2], count); 1.135 ++ } else { 1.136 ++ unsigned fi = proc(fx) >> (16 - kCache32Bits); 1.137 ++ sk_memset32_dither(dstC, cache[toggle + fi], 1.138 ++ cache[(toggle ^ TOGGLE_MASK) + fi], count); 1.139 ++ } 1.140 ++ } else { 1.141 ++ unsigned fi = proc(fx) >> (16 - kCache32Bits); 1.142 ++ sk_memset32_dither(dstC, cache[toggle + fi], 1.143 ++ cache[(toggle ^ TOGGLE_MASK) + fi], count); 1.144 ++ } 1.145 + } else if (proc == clamp_tileproc) { 1.146 + SkClampRange range; 1.147 +- range.init(fx, dx, count, 0, 0xFF); 1.148 ++ range.init(fx, dx, count, cache[-1], cache[kCache32Count * 2]); 1.149 + 1.150 + if ((count = range.fCount0) > 0) { 1.151 +- sk_memset32_dither(dstC, 1.152 +- cache[toggle + range.fV0], 1.153 +- cache[(toggle ^ TOGGLE_MASK) + range.fV0], 1.154 +- count); 1.155 ++ // Do we really want to dither the clamp values? 1.156 ++ sk_memset32(dstC, range.fV0, count); 1.157 + dstC += count; 1.158 + } 1.159 + if ((count = range.fCount1) > 0) { 1.160 + int unroll = count >> 3; 1.161 + fx = range.fFx1; 1.162 + for (int i = 0; i < unroll; i++) { 1.163 + NO_CHECK_ITER; NO_CHECK_ITER; 1.164 + NO_CHECK_ITER; NO_CHECK_ITER; 1.165 +@@ -905,20 +934,17 @@ void Linear_Gradient::shadeSpan(int x, i 1.166 + } 1.167 + if ((count &= 7) > 0) { 1.168 + do { 1.169 + NO_CHECK_ITER; 1.170 + } while (--count != 0); 1.171 + } 1.172 + } 1.173 + if ((count = range.fCount2) > 0) { 1.174 +- sk_memset32_dither(dstC, 1.175 +- cache[toggle + range.fV1], 1.176 +- cache[(toggle ^ TOGGLE_MASK) + range.fV1], 1.177 +- count); 1.178 ++ sk_memset32(dstC, range.fV1, count); 1.179 + } 1.180 + } else if (proc == mirror_tileproc) { 1.181 + do { 1.182 + unsigned fi = mirror_8bits(fx >> 8); 1.183 + SkASSERT(fi <= 0xFF); 1.184 + fx += dx; 1.185 + *dstC++ = cache[toggle + fi]; 1.186 + toggle ^= TOGGLE_MASK; 1.187 +@@ -1670,19 +1699,24 @@ public: 1.188 + } 1.189 + SkScalar b = (SkScalarMul(fDiff.fX, fx) + 1.190 + SkScalarMul(fDiff.fY, fy) - fStartRadius) * 2; 1.191 + SkScalar db = (SkScalarMul(fDiff.fX, dx) + 1.192 + SkScalarMul(fDiff.fY, dy)) * 2; 1.193 + if (proc == clamp_tileproc) { 1.194 + for (; count > 0; --count) { 1.195 + SkFixed t = two_point_radial(b, fx, fy, fSr2D2, foura, fOneOverTwoA, posRoot); 1.196 +- SkFixed index = SkClampMax(t, 0xFFFF); 1.197 +- SkASSERT(index <= 0xFFFF); 1.198 +- *dstC++ = cache[index >> (16 - kCache32Bits)]; 1.199 ++ if (t < 0) { 1.200 ++ *dstC++ = cache[-1]; 1.201 ++ } else if (t > 0xFFFF) { 1.202 ++ *dstC++ = cache[kCache32Count * 2]; 1.203 ++ } else { 1.204 ++ SkASSERT(t <= 0xFFFF); 1.205 ++ *dstC++ = cache[t >> (16 - kCache32Bits)]; 1.206 ++ } 1.207 + fx += dx; 1.208 + fy += dy; 1.209 + b += db; 1.210 + } 1.211 + } else if (proc == mirror_tileproc) { 1.212 + for (; count > 0; --count) { 1.213 + SkFixed t = two_point_radial(b, fx, fy, fSr2D2, foura, fOneOverTwoA, posRoot); 1.214 + SkFixed index = mirror_tileproc(t);