michael@0: /* michael@0: * Copyright 2006 The Android Open Source Project michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "SkGradientShaderPriv.h" michael@0: #include "SkLinearGradient.h" michael@0: #include "SkRadialGradient.h" michael@0: #include "SkTwoPointRadialGradient.h" michael@0: #include "SkTwoPointConicalGradient.h" michael@0: #include "SkSweepGradient.h" michael@0: michael@0: SkGradientShaderBase::SkGradientShaderBase(const Descriptor& desc) { michael@0: SkASSERT(desc.fCount > 1); michael@0: michael@0: fCacheAlpha = 256; // init to a value that paint.getAlpha() can't return michael@0: michael@0: fMapper = desc.fMapper; michael@0: SkSafeRef(fMapper); michael@0: fGradFlags = SkToU8(desc.fFlags); michael@0: michael@0: SkASSERT((unsigned)desc.fTileMode < SkShader::kTileModeCount); michael@0: SkASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gTileProcs)); michael@0: fTileMode = desc.fTileMode; michael@0: fTileProc = gTileProcs[desc.fTileMode]; michael@0: michael@0: fCache16 = fCache16Storage = NULL; michael@0: fCache32 = NULL; michael@0: fCache32PixelRef = NULL; michael@0: michael@0: /* Note: we let the caller skip the first and/or last position. michael@0: i.e. pos[0] = 0.3, pos[1] = 0.7 michael@0: In these cases, we insert dummy entries to ensure that the final data michael@0: will be bracketed by [0, 1]. michael@0: i.e. our_pos[0] = 0, our_pos[1] = 0.3, our_pos[2] = 0.7, our_pos[3] = 1 michael@0: michael@0: Thus colorCount (the caller's value, and fColorCount (our value) may michael@0: differ by up to 2. In the above example: michael@0: colorCount = 2 michael@0: fColorCount = 4 michael@0: */ michael@0: fColorCount = desc.fCount; michael@0: // check if we need to add in dummy start and/or end position/colors michael@0: bool dummyFirst = false; michael@0: bool dummyLast = false; michael@0: if (desc.fPos) { michael@0: dummyFirst = desc.fPos[0] != 0; michael@0: dummyLast = desc.fPos[desc.fCount - 1] != SK_Scalar1; michael@0: fColorCount += dummyFirst + dummyLast; michael@0: } michael@0: michael@0: if (fColorCount > kColorStorageCount) { michael@0: size_t size = sizeof(SkColor) + sizeof(Rec); michael@0: fOrigColors = reinterpret_cast( michael@0: sk_malloc_throw(size * fColorCount)); michael@0: } michael@0: else { michael@0: fOrigColors = fStorage; michael@0: } michael@0: michael@0: // Now copy over the colors, adding the dummies as needed michael@0: { michael@0: SkColor* origColors = fOrigColors; michael@0: if (dummyFirst) { michael@0: *origColors++ = desc.fColors[0]; michael@0: } michael@0: memcpy(origColors, desc.fColors, desc.fCount * sizeof(SkColor)); michael@0: if (dummyLast) { michael@0: origColors += desc.fCount; michael@0: *origColors = desc.fColors[desc.fCount - 1]; michael@0: } michael@0: } michael@0: michael@0: fRecs = (Rec*)(fOrigColors + fColorCount); michael@0: if (fColorCount > 2) { michael@0: Rec* recs = fRecs; michael@0: recs->fPos = 0; michael@0: // recs->fScale = 0; // unused; michael@0: recs += 1; michael@0: if (desc.fPos) { michael@0: /* We need to convert the user's array of relative positions into michael@0: fixed-point positions and scale factors. We need these results michael@0: to be strictly monotonic (no two values equal or out of order). michael@0: Hence this complex loop that just jams a zero for the scale michael@0: value if it sees a segment out of order, and it assures that michael@0: we start at 0 and end at 1.0 michael@0: */ michael@0: SkFixed prev = 0; michael@0: int startIndex = dummyFirst ? 0 : 1; michael@0: int count = desc.fCount + dummyLast; michael@0: for (int i = startIndex; i < count; i++) { michael@0: // force the last value to be 1.0 michael@0: SkFixed curr; michael@0: if (i == desc.fCount) { // we're really at the dummyLast michael@0: curr = SK_Fixed1; michael@0: } else { michael@0: curr = SkScalarToFixed(desc.fPos[i]); michael@0: } michael@0: // pin curr withing range michael@0: if (curr < 0) { michael@0: curr = 0; michael@0: } else if (curr > SK_Fixed1) { michael@0: curr = SK_Fixed1; michael@0: } michael@0: recs->fPos = curr; michael@0: if (curr > prev) { michael@0: recs->fScale = (1 << 24) / (curr - prev); michael@0: } else { michael@0: recs->fScale = 0; // ignore this segment michael@0: } michael@0: // get ready for the next value michael@0: prev = curr; michael@0: recs += 1; michael@0: } michael@0: } else { // assume even distribution michael@0: SkFixed dp = SK_Fixed1 / (desc.fCount - 1); michael@0: SkFixed p = dp; michael@0: SkFixed scale = (desc.fCount - 1) << 8; // (1 << 24) / dp michael@0: for (int i = 1; i < desc.fCount; i++) { michael@0: recs->fPos = p; michael@0: recs->fScale = scale; michael@0: recs += 1; michael@0: p += dp; michael@0: } michael@0: } michael@0: } michael@0: this->initCommon(); michael@0: } michael@0: michael@0: static uint32_t pack_mode_flags(SkShader::TileMode mode, uint32_t flags) { michael@0: SkASSERT(0 == (flags >> 28)); michael@0: SkASSERT(0 == ((uint32_t)mode >> 4)); michael@0: return (flags << 4) | mode; michael@0: } michael@0: michael@0: static SkShader::TileMode unpack_mode(uint32_t packed) { michael@0: return (SkShader::TileMode)(packed & 0xF); michael@0: } michael@0: michael@0: static uint32_t unpack_flags(uint32_t packed) { michael@0: return packed >> 4; michael@0: } michael@0: michael@0: SkGradientShaderBase::SkGradientShaderBase(SkReadBuffer& buffer) : INHERITED(buffer) { michael@0: fCacheAlpha = 256; michael@0: michael@0: fMapper = buffer.readUnitMapper(); michael@0: michael@0: fCache16 = fCache16Storage = NULL; michael@0: fCache32 = NULL; michael@0: fCache32PixelRef = NULL; michael@0: michael@0: int colorCount = fColorCount = buffer.getArrayCount(); michael@0: if (colorCount > kColorStorageCount) { michael@0: size_t allocSize = (sizeof(SkColor) + sizeof(SkPMColor) + sizeof(Rec)) * colorCount; michael@0: if (buffer.validateAvailable(allocSize)) { michael@0: fOrigColors = reinterpret_cast(sk_malloc_throw(allocSize)); michael@0: } else { michael@0: fOrigColors = NULL; michael@0: colorCount = fColorCount = 0; michael@0: } michael@0: } else { michael@0: fOrigColors = fStorage; michael@0: } michael@0: buffer.readColorArray(fOrigColors, colorCount); michael@0: michael@0: { michael@0: uint32_t packed = buffer.readUInt(); michael@0: fGradFlags = SkToU8(unpack_flags(packed)); michael@0: fTileMode = unpack_mode(packed); michael@0: } michael@0: fTileProc = gTileProcs[fTileMode]; michael@0: fRecs = (Rec*)(fOrigColors + colorCount); michael@0: if (colorCount > 2) { michael@0: Rec* recs = fRecs; michael@0: recs[0].fPos = 0; michael@0: for (int i = 1; i < colorCount; i++) { michael@0: recs[i].fPos = buffer.readInt(); michael@0: recs[i].fScale = buffer.readUInt(); michael@0: } michael@0: } michael@0: buffer.readMatrix(&fPtsToUnit); michael@0: this->initCommon(); michael@0: } michael@0: michael@0: SkGradientShaderBase::~SkGradientShaderBase() { michael@0: if (fCache16Storage) { michael@0: sk_free(fCache16Storage); michael@0: } michael@0: SkSafeUnref(fCache32PixelRef); michael@0: if (fOrigColors != fStorage) { michael@0: sk_free(fOrigColors); michael@0: } michael@0: SkSafeUnref(fMapper); michael@0: } michael@0: michael@0: void SkGradientShaderBase::initCommon() { michael@0: fFlags = 0; michael@0: unsigned colorAlpha = 0xFF; michael@0: for (int i = 0; i < fColorCount; i++) { michael@0: colorAlpha &= SkColorGetA(fOrigColors[i]); michael@0: } michael@0: fColorsAreOpaque = colorAlpha == 0xFF; michael@0: } michael@0: michael@0: void SkGradientShaderBase::flatten(SkWriteBuffer& buffer) const { michael@0: this->INHERITED::flatten(buffer); michael@0: buffer.writeFlattenable(fMapper); michael@0: buffer.writeColorArray(fOrigColors, fColorCount); michael@0: buffer.writeUInt(pack_mode_flags(fTileMode, fGradFlags)); michael@0: if (fColorCount > 2) { michael@0: Rec* recs = fRecs; michael@0: for (int i = 1; i < fColorCount; i++) { michael@0: buffer.writeInt(recs[i].fPos); michael@0: buffer.writeUInt(recs[i].fScale); michael@0: } michael@0: } michael@0: buffer.writeMatrix(fPtsToUnit); michael@0: } michael@0: michael@0: bool SkGradientShaderBase::isOpaque() const { michael@0: return fColorsAreOpaque; michael@0: } michael@0: michael@0: bool SkGradientShaderBase::setContext(const SkBitmap& device, michael@0: const SkPaint& paint, michael@0: const SkMatrix& matrix) { michael@0: if (!this->INHERITED::setContext(device, paint, matrix)) { michael@0: return false; michael@0: } michael@0: michael@0: const SkMatrix& inverse = this->getTotalInverse(); michael@0: michael@0: if (!fDstToIndex.setConcat(fPtsToUnit, inverse)) { michael@0: // need to keep our set/end context calls balanced. michael@0: this->INHERITED::endContext(); michael@0: return false; michael@0: } michael@0: michael@0: fDstToIndexProc = fDstToIndex.getMapXYProc(); michael@0: fDstToIndexClass = (uint8_t)SkShader::ComputeMatrixClass(fDstToIndex); michael@0: michael@0: // now convert our colors in to PMColors michael@0: unsigned paintAlpha = this->getPaintAlpha(); michael@0: michael@0: fFlags = this->INHERITED::getFlags(); michael@0: if (fColorsAreOpaque && paintAlpha == 0xFF) { michael@0: fFlags |= kOpaqueAlpha_Flag; michael@0: } michael@0: // we can do span16 as long as our individual colors are opaque, michael@0: // regardless of the paint's alpha michael@0: if (fColorsAreOpaque) { michael@0: fFlags |= kHasSpan16_Flag; michael@0: } michael@0: michael@0: this->setCacheAlpha(paintAlpha); michael@0: return true; michael@0: } michael@0: michael@0: void SkGradientShaderBase::setCacheAlpha(U8CPU alpha) const { michael@0: // if the new alpha differs from the previous time we were called, inval our cache michael@0: // this will trigger the cache to be rebuilt. michael@0: // we don't care about the first time, since the cache ptrs will already be NULL michael@0: if (fCacheAlpha != alpha) { michael@0: fCache16 = NULL; // inval the cache michael@0: fCache32 = NULL; // inval the cache michael@0: fCacheAlpha = alpha; // record the new alpha michael@0: // inform our subclasses michael@0: if (fCache32PixelRef) { michael@0: fCache32PixelRef->notifyPixelsChanged(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: #define Fixed_To_Dot8(x) (((x) + 0x80) >> 8) michael@0: michael@0: /** We take the original colors, not our premultiplied PMColors, since we can michael@0: build a 16bit table as long as the original colors are opaque, even if the michael@0: paint specifies a non-opaque alpha. michael@0: */ michael@0: void SkGradientShaderBase::Build16bitCache(uint16_t cache[], SkColor c0, SkColor c1, michael@0: int count) { michael@0: SkASSERT(count > 1); michael@0: SkASSERT(SkColorGetA(c0) == 0xFF); michael@0: SkASSERT(SkColorGetA(c1) == 0xFF); michael@0: michael@0: SkFixed r = SkColorGetR(c0); michael@0: SkFixed g = SkColorGetG(c0); michael@0: SkFixed b = SkColorGetB(c0); michael@0: michael@0: SkFixed dr = SkIntToFixed(SkColorGetR(c1) - r) / (count - 1); michael@0: SkFixed dg = SkIntToFixed(SkColorGetG(c1) - g) / (count - 1); michael@0: SkFixed db = SkIntToFixed(SkColorGetB(c1) - b) / (count - 1); michael@0: michael@0: r = SkIntToFixed(r) + 0x8000; michael@0: g = SkIntToFixed(g) + 0x8000; michael@0: b = SkIntToFixed(b) + 0x8000; michael@0: michael@0: do { michael@0: unsigned rr = r >> 16; michael@0: unsigned gg = g >> 16; michael@0: unsigned bb = b >> 16; michael@0: cache[0] = SkPackRGB16(SkR32ToR16(rr), SkG32ToG16(gg), SkB32ToB16(bb)); michael@0: cache[kCache16Count] = SkDitherPack888ToRGB16(rr, gg, bb); michael@0: cache += 1; michael@0: r += dr; michael@0: g += dg; michael@0: b += db; michael@0: } while (--count != 0); michael@0: } michael@0: michael@0: /* michael@0: * r,g,b used to be SkFixed, but on gcc (4.2.1 mac and 4.6.3 goobuntu) in michael@0: * release builds, we saw a compiler error where the 0xFF parameter in michael@0: * SkPackARGB32() was being totally ignored whenever it was called with michael@0: * a non-zero add (e.g. 0x8000). michael@0: * michael@0: * We found two work-arounds: michael@0: * 1. change r,g,b to unsigned (or just one of them) michael@0: * 2. change SkPackARGB32 to + its (a << SK_A32_SHIFT) value instead michael@0: * of using | michael@0: * michael@0: * We chose #1 just because it was more localized. michael@0: * See http://code.google.com/p/skia/issues/detail?id=1113 michael@0: * michael@0: * The type SkUFixed encapsulate this need for unsigned, but logically Fixed. michael@0: */ michael@0: typedef uint32_t SkUFixed; michael@0: michael@0: void SkGradientShaderBase::Build32bitCache(SkPMColor cache[], SkColor c0, SkColor c1, michael@0: int count, U8CPU paintAlpha, uint32_t gradFlags) { michael@0: SkASSERT(count > 1); michael@0: michael@0: // need to apply paintAlpha to our two endpoints michael@0: uint32_t a0 = SkMulDiv255Round(SkColorGetA(c0), paintAlpha); michael@0: uint32_t a1 = SkMulDiv255Round(SkColorGetA(c1), paintAlpha); michael@0: michael@0: michael@0: const bool interpInPremul = SkToBool(gradFlags & michael@0: SkGradientShader::kInterpolateColorsInPremul_Flag); michael@0: michael@0: uint32_t r0 = SkColorGetR(c0); michael@0: uint32_t g0 = SkColorGetG(c0); michael@0: uint32_t b0 = SkColorGetB(c0); michael@0: michael@0: uint32_t r1 = SkColorGetR(c1); michael@0: uint32_t g1 = SkColorGetG(c1); michael@0: uint32_t b1 = SkColorGetB(c1); michael@0: michael@0: if (interpInPremul) { michael@0: r0 = SkMulDiv255Round(r0, a0); michael@0: g0 = SkMulDiv255Round(g0, a0); michael@0: b0 = SkMulDiv255Round(b0, a0); michael@0: michael@0: r1 = SkMulDiv255Round(r1, a1); michael@0: g1 = SkMulDiv255Round(g1, a1); michael@0: b1 = SkMulDiv255Round(b1, a1); michael@0: } michael@0: michael@0: SkFixed da = SkIntToFixed(a1 - a0) / (count - 1); michael@0: SkFixed dr = SkIntToFixed(r1 - r0) / (count - 1); michael@0: SkFixed dg = SkIntToFixed(g1 - g0) / (count - 1); michael@0: SkFixed db = SkIntToFixed(b1 - b0) / (count - 1); michael@0: michael@0: /* We pre-add 1/8 to avoid having to add this to our [0] value each time michael@0: in the loop. Without this, the bias for each would be michael@0: 0x2000 0xA000 0xE000 0x6000 michael@0: With this trick, we can add 0 for the first (no-op) and just adjust the michael@0: others. michael@0: */ michael@0: SkUFixed a = SkIntToFixed(a0) + 0x2000; michael@0: SkUFixed r = SkIntToFixed(r0) + 0x2000; michael@0: SkUFixed g = SkIntToFixed(g0) + 0x2000; michael@0: SkUFixed b = SkIntToFixed(b0) + 0x2000; michael@0: michael@0: /* michael@0: * Our dither-cell (spatially) is michael@0: * 0 2 michael@0: * 3 1 michael@0: * Where michael@0: * [0] -> [-1/8 ... 1/8 ) values near 0 michael@0: * [1] -> [ 1/8 ... 3/8 ) values near 1/4 michael@0: * [2] -> [ 3/8 ... 5/8 ) values near 1/2 michael@0: * [3] -> [ 5/8 ... 7/8 ) values near 3/4 michael@0: */ michael@0: michael@0: if (0xFF == a0 && 0 == da) { michael@0: do { michael@0: cache[kCache32Count*0] = SkPackARGB32(0xFF, (r + 0 ) >> 16, michael@0: (g + 0 ) >> 16, michael@0: (b + 0 ) >> 16); michael@0: cache[kCache32Count*1] = SkPackARGB32(0xFF, (r + 0x8000) >> 16, michael@0: (g + 0x8000) >> 16, michael@0: (b + 0x8000) >> 16); michael@0: cache[kCache32Count*2] = SkPackARGB32(0xFF, (r + 0xC000) >> 16, michael@0: (g + 0xC000) >> 16, michael@0: (b + 0xC000) >> 16); michael@0: cache[kCache32Count*3] = SkPackARGB32(0xFF, (r + 0x4000) >> 16, michael@0: (g + 0x4000) >> 16, michael@0: (b + 0x4000) >> 16); michael@0: cache += 1; michael@0: r += dr; michael@0: g += dg; michael@0: b += db; michael@0: } while (--count != 0); michael@0: } else if (interpInPremul) { michael@0: do { michael@0: cache[kCache32Count*0] = SkPackARGB32((a + 0 ) >> 16, michael@0: (r + 0 ) >> 16, michael@0: (g + 0 ) >> 16, michael@0: (b + 0 ) >> 16); michael@0: cache[kCache32Count*1] = SkPackARGB32((a + 0x8000) >> 16, michael@0: (r + 0x8000) >> 16, michael@0: (g + 0x8000) >> 16, michael@0: (b + 0x8000) >> 16); michael@0: cache[kCache32Count*2] = SkPackARGB32((a + 0xC000) >> 16, michael@0: (r + 0xC000) >> 16, michael@0: (g + 0xC000) >> 16, michael@0: (b + 0xC000) >> 16); michael@0: cache[kCache32Count*3] = SkPackARGB32((a + 0x4000) >> 16, michael@0: (r + 0x4000) >> 16, michael@0: (g + 0x4000) >> 16, michael@0: (b + 0x4000) >> 16); michael@0: cache += 1; michael@0: a += da; michael@0: r += dr; michael@0: g += dg; michael@0: b += db; michael@0: } while (--count != 0); michael@0: } else { // interpolate in unpreml space michael@0: do { michael@0: cache[kCache32Count*0] = SkPremultiplyARGBInline((a + 0 ) >> 16, michael@0: (r + 0 ) >> 16, michael@0: (g + 0 ) >> 16, michael@0: (b + 0 ) >> 16); michael@0: cache[kCache32Count*1] = SkPremultiplyARGBInline((a + 0x8000) >> 16, michael@0: (r + 0x8000) >> 16, michael@0: (g + 0x8000) >> 16, michael@0: (b + 0x8000) >> 16); michael@0: cache[kCache32Count*2] = SkPremultiplyARGBInline((a + 0xC000) >> 16, michael@0: (r + 0xC000) >> 16, michael@0: (g + 0xC000) >> 16, michael@0: (b + 0xC000) >> 16); michael@0: cache[kCache32Count*3] = SkPremultiplyARGBInline((a + 0x4000) >> 16, michael@0: (r + 0x4000) >> 16, michael@0: (g + 0x4000) >> 16, michael@0: (b + 0x4000) >> 16); michael@0: cache += 1; michael@0: a += da; michael@0: r += dr; michael@0: g += dg; michael@0: b += db; michael@0: } while (--count != 0); michael@0: } michael@0: } michael@0: michael@0: static inline int SkFixedToFFFF(SkFixed x) { michael@0: SkASSERT((unsigned)x <= SK_Fixed1); michael@0: return x - (x >> 16); michael@0: } michael@0: michael@0: static inline U16CPU bitsTo16(unsigned x, const unsigned bits) { michael@0: SkASSERT(x < (1U << bits)); michael@0: if (6 == bits) { michael@0: return (x << 10) | (x << 4) | (x >> 2); michael@0: } michael@0: if (8 == bits) { michael@0: return (x << 8) | x; michael@0: } michael@0: sk_throw(); michael@0: return 0; michael@0: } michael@0: michael@0: const uint16_t* SkGradientShaderBase::getCache16() const { michael@0: if (fCache16 == NULL) { michael@0: // double the count for dither entries michael@0: const int entryCount = kCache16Count * 2; michael@0: const size_t allocSize = sizeof(uint16_t) * entryCount; michael@0: michael@0: if (fCache16Storage == NULL) { // set the storage and our working ptr michael@0: fCache16Storage = (uint16_t*)sk_malloc_throw(allocSize); michael@0: } michael@0: fCache16 = fCache16Storage; michael@0: if (fColorCount == 2) { michael@0: Build16bitCache(fCache16, fOrigColors[0], fOrigColors[1], michael@0: kCache16Count); michael@0: } else { michael@0: Rec* rec = fRecs; michael@0: int prevIndex = 0; michael@0: for (int i = 1; i < fColorCount; i++) { michael@0: int nextIndex = SkFixedToFFFF(rec[i].fPos) >> kCache16Shift; michael@0: SkASSERT(nextIndex < kCache16Count); michael@0: michael@0: if (nextIndex > prevIndex) michael@0: Build16bitCache(fCache16 + prevIndex, fOrigColors[i-1], fOrigColors[i], nextIndex - prevIndex + 1); michael@0: prevIndex = nextIndex; michael@0: } michael@0: } michael@0: michael@0: if (fMapper) { michael@0: fCache16Storage = (uint16_t*)sk_malloc_throw(allocSize); michael@0: uint16_t* linear = fCache16; // just computed linear data michael@0: uint16_t* mapped = fCache16Storage; // storage for mapped data michael@0: SkUnitMapper* map = fMapper; michael@0: for (int i = 0; i < kCache16Count; i++) { michael@0: int index = map->mapUnit16(bitsTo16(i, kCache16Bits)) >> kCache16Shift; michael@0: mapped[i] = linear[index]; michael@0: mapped[i + kCache16Count] = linear[index + kCache16Count]; michael@0: } michael@0: sk_free(fCache16); michael@0: fCache16 = fCache16Storage; michael@0: } michael@0: } michael@0: return fCache16; michael@0: } michael@0: michael@0: const SkPMColor* SkGradientShaderBase::getCache32() const { michael@0: if (fCache32 == NULL) { michael@0: SkImageInfo info; michael@0: info.fWidth = kCache32Count; michael@0: info.fHeight = 4; // for our 4 dither rows michael@0: info.fAlphaType = kPremul_SkAlphaType; michael@0: info.fColorType = kPMColor_SkColorType; michael@0: michael@0: if (NULL == fCache32PixelRef) { michael@0: fCache32PixelRef = SkMallocPixelRef::NewAllocate(info, 0, NULL); michael@0: } michael@0: fCache32 = (SkPMColor*)fCache32PixelRef->getAddr(); michael@0: if (fColorCount == 2) { michael@0: Build32bitCache(fCache32, fOrigColors[0], fOrigColors[1], michael@0: kCache32Count, fCacheAlpha, fGradFlags); michael@0: } else { michael@0: Rec* rec = fRecs; michael@0: int prevIndex = 0; michael@0: for (int i = 1; i < fColorCount; i++) { michael@0: int nextIndex = SkFixedToFFFF(rec[i].fPos) >> kCache32Shift; michael@0: SkASSERT(nextIndex < kCache32Count); michael@0: michael@0: if (nextIndex > prevIndex) michael@0: Build32bitCache(fCache32 + prevIndex, fOrigColors[i-1], michael@0: fOrigColors[i], nextIndex - prevIndex + 1, michael@0: fCacheAlpha, fGradFlags); michael@0: prevIndex = nextIndex; michael@0: } michael@0: } michael@0: michael@0: if (fMapper) { michael@0: SkMallocPixelRef* newPR = SkMallocPixelRef::NewAllocate(info, 0, NULL); michael@0: SkPMColor* linear = fCache32; // just computed linear data michael@0: SkPMColor* mapped = (SkPMColor*)newPR->getAddr(); // storage for mapped data michael@0: SkUnitMapper* map = fMapper; michael@0: for (int i = 0; i < kCache32Count; i++) { michael@0: int index = map->mapUnit16((i << 8) | i) >> 8; michael@0: mapped[i + kCache32Count*0] = linear[index + kCache32Count*0]; michael@0: mapped[i + kCache32Count*1] = linear[index + kCache32Count*1]; michael@0: mapped[i + kCache32Count*2] = linear[index + kCache32Count*2]; michael@0: mapped[i + kCache32Count*3] = linear[index + kCache32Count*3]; michael@0: } michael@0: fCache32PixelRef->unref(); michael@0: fCache32PixelRef = newPR; michael@0: fCache32 = (SkPMColor*)newPR->getAddr(); michael@0: } michael@0: } michael@0: return fCache32; michael@0: } michael@0: michael@0: /* michael@0: * Because our caller might rebuild the same (logically the same) gradient michael@0: * over and over, we'd like to return exactly the same "bitmap" if possible, michael@0: * allowing the client to utilize a cache of our bitmap (e.g. with a GPU). michael@0: * To do that, we maintain a private cache of built-bitmaps, based on our michael@0: * colors and positions. Note: we don't try to flatten the fMapper, so if one michael@0: * is present, we skip the cache for now. michael@0: */ michael@0: void SkGradientShaderBase::getGradientTableBitmap(SkBitmap* bitmap) const { michael@0: // our caller assumes no external alpha, so we ensure that our cache is michael@0: // built with 0xFF michael@0: this->setCacheAlpha(0xFF); michael@0: michael@0: // don't have a way to put the mapper into our cache-key yet michael@0: if (fMapper) { michael@0: // force our cahce32pixelref to be built michael@0: (void)this->getCache32(); michael@0: bitmap->setConfig(SkImageInfo::MakeN32Premul(kCache32Count, 1)); michael@0: bitmap->setPixelRef(fCache32PixelRef); michael@0: return; michael@0: } michael@0: michael@0: // build our key: [numColors + colors[] + {positions[]} + flags ] michael@0: int count = 1 + fColorCount + 1; michael@0: if (fColorCount > 2) { michael@0: count += fColorCount - 1; // fRecs[].fPos michael@0: } michael@0: michael@0: SkAutoSTMalloc<16, int32_t> storage(count); michael@0: int32_t* buffer = storage.get(); michael@0: michael@0: *buffer++ = fColorCount; michael@0: memcpy(buffer, fOrigColors, fColorCount * sizeof(SkColor)); michael@0: buffer += fColorCount; michael@0: if (fColorCount > 2) { michael@0: for (int i = 1; i < fColorCount; i++) { michael@0: *buffer++ = fRecs[i].fPos; michael@0: } michael@0: } michael@0: *buffer++ = fGradFlags; michael@0: SkASSERT(buffer - storage.get() == count); michael@0: michael@0: /////////////////////////////////// michael@0: michael@0: SK_DECLARE_STATIC_MUTEX(gMutex); michael@0: static SkBitmapCache* gCache; michael@0: // each cache cost 1K of RAM, since each bitmap will be 1x256 at 32bpp michael@0: static const int MAX_NUM_CACHED_GRADIENT_BITMAPS = 32; michael@0: SkAutoMutexAcquire ama(gMutex); michael@0: michael@0: if (NULL == gCache) { michael@0: gCache = SkNEW_ARGS(SkBitmapCache, (MAX_NUM_CACHED_GRADIENT_BITMAPS)); michael@0: } michael@0: size_t size = count * sizeof(int32_t); michael@0: michael@0: if (!gCache->find(storage.get(), size, bitmap)) { michael@0: // force our cahce32pixelref to be built michael@0: (void)this->getCache32(); michael@0: bitmap->setConfig(SkImageInfo::MakeN32Premul(kCache32Count, 1)); michael@0: bitmap->setPixelRef(fCache32PixelRef); michael@0: michael@0: gCache->add(storage.get(), size, *bitmap); michael@0: } michael@0: } michael@0: michael@0: void SkGradientShaderBase::commonAsAGradient(GradientInfo* info) const { michael@0: if (info) { michael@0: if (info->fColorCount >= fColorCount) { michael@0: if (info->fColors) { michael@0: memcpy(info->fColors, fOrigColors, fColorCount * sizeof(SkColor)); michael@0: } michael@0: if (info->fColorOffsets) { michael@0: if (fColorCount == 2) { michael@0: info->fColorOffsets[0] = 0; michael@0: info->fColorOffsets[1] = SK_Scalar1; michael@0: } else if (fColorCount > 2) { michael@0: for (int i = 0; i < fColorCount; ++i) { michael@0: info->fColorOffsets[i] = SkFixedToScalar(fRecs[i].fPos); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: info->fColorCount = fColorCount; michael@0: info->fTileMode = fTileMode; michael@0: info->fGradientFlags = fGradFlags; michael@0: } michael@0: } michael@0: michael@0: #ifndef SK_IGNORE_TO_STRING michael@0: void SkGradientShaderBase::toString(SkString* str) const { michael@0: michael@0: str->appendf("%d colors: ", fColorCount); michael@0: michael@0: for (int i = 0; i < fColorCount; ++i) { michael@0: str->appendHex(fOrigColors[i]); michael@0: if (i < fColorCount-1) { michael@0: str->append(", "); michael@0: } michael@0: } michael@0: michael@0: if (fColorCount > 2) { michael@0: str->append(" points: ("); michael@0: for (int i = 0; i < fColorCount; ++i) { michael@0: str->appendScalar(SkFixedToScalar(fRecs[i].fPos)); michael@0: if (i < fColorCount-1) { michael@0: str->append(", "); michael@0: } michael@0: } michael@0: str->append(")"); michael@0: } michael@0: michael@0: static const char* gTileModeName[SkShader::kTileModeCount] = { michael@0: "clamp", "repeat", "mirror" michael@0: }; michael@0: michael@0: str->append(" "); michael@0: str->append(gTileModeName[fTileMode]); michael@0: michael@0: // TODO: add "fMapper->toString(str);" when SkUnitMapper::toString is added michael@0: michael@0: this->INHERITED::toString(str); michael@0: } michael@0: #endif michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #include "SkEmptyShader.h" michael@0: michael@0: // assumes colors is SkColor* and pos is SkScalar* michael@0: #define EXPAND_1_COLOR(count) \ michael@0: SkColor tmp[2]; \ michael@0: do { \ michael@0: if (1 == count) { \ michael@0: tmp[0] = tmp[1] = colors[0]; \ michael@0: colors = tmp; \ michael@0: pos = NULL; \ michael@0: count = 2; \ michael@0: } \ michael@0: } while (0) michael@0: michael@0: static void desc_init(SkGradientShaderBase::Descriptor* desc, michael@0: const SkColor colors[], michael@0: const SkScalar pos[], int colorCount, michael@0: SkShader::TileMode mode, michael@0: SkUnitMapper* mapper, uint32_t flags) { michael@0: desc->fColors = colors; michael@0: desc->fPos = pos; michael@0: desc->fCount = colorCount; michael@0: desc->fTileMode = mode; michael@0: desc->fMapper = mapper; michael@0: desc->fFlags = flags; michael@0: } michael@0: michael@0: SkShader* SkGradientShader::CreateLinear(const SkPoint pts[2], michael@0: const SkColor colors[], michael@0: const SkScalar pos[], int colorCount, michael@0: SkShader::TileMode mode, michael@0: SkUnitMapper* mapper, michael@0: uint32_t flags) { michael@0: if (NULL == pts || NULL == colors || colorCount < 1) { michael@0: return NULL; michael@0: } michael@0: EXPAND_1_COLOR(colorCount); michael@0: michael@0: SkGradientShaderBase::Descriptor desc; michael@0: desc_init(&desc, colors, pos, colorCount, mode, mapper, flags); michael@0: return SkNEW_ARGS(SkLinearGradient, (pts, desc)); michael@0: } michael@0: michael@0: SkShader* SkGradientShader::CreateRadial(const SkPoint& center, SkScalar radius, michael@0: const SkColor colors[], michael@0: const SkScalar pos[], int colorCount, michael@0: SkShader::TileMode mode, michael@0: SkUnitMapper* mapper, michael@0: uint32_t flags) { michael@0: if (radius <= 0 || NULL == colors || colorCount < 1) { michael@0: return NULL; michael@0: } michael@0: EXPAND_1_COLOR(colorCount); michael@0: michael@0: SkGradientShaderBase::Descriptor desc; michael@0: desc_init(&desc, colors, pos, colorCount, mode, mapper, flags); michael@0: return SkNEW_ARGS(SkRadialGradient, (center, radius, desc)); michael@0: } michael@0: michael@0: SkShader* SkGradientShader::CreateTwoPointRadial(const SkPoint& start, michael@0: SkScalar startRadius, michael@0: const SkPoint& end, michael@0: SkScalar endRadius, michael@0: const SkColor colors[], michael@0: const SkScalar pos[], michael@0: int colorCount, michael@0: SkShader::TileMode mode, michael@0: SkUnitMapper* mapper, michael@0: uint32_t flags) { michael@0: if (startRadius < 0 || endRadius < 0 || NULL == colors || colorCount < 1) { michael@0: return NULL; michael@0: } michael@0: EXPAND_1_COLOR(colorCount); michael@0: michael@0: SkGradientShaderBase::Descriptor desc; michael@0: desc_init(&desc, colors, pos, colorCount, mode, mapper, flags); michael@0: return SkNEW_ARGS(SkTwoPointRadialGradient, michael@0: (start, startRadius, end, endRadius, desc)); michael@0: } michael@0: michael@0: SkShader* SkGradientShader::CreateTwoPointConical(const SkPoint& start, michael@0: SkScalar startRadius, michael@0: const SkPoint& end, michael@0: SkScalar endRadius, michael@0: const SkColor colors[], michael@0: const SkScalar pos[], michael@0: int colorCount, michael@0: SkShader::TileMode mode, michael@0: SkUnitMapper* mapper, michael@0: uint32_t flags) { michael@0: if (startRadius < 0 || endRadius < 0 || NULL == colors || colorCount < 1) { michael@0: return NULL; michael@0: } michael@0: if (start == end && startRadius == endRadius) { michael@0: return SkNEW(SkEmptyShader); michael@0: } michael@0: EXPAND_1_COLOR(colorCount); michael@0: michael@0: SkGradientShaderBase::Descriptor desc; michael@0: desc_init(&desc, colors, pos, colorCount, mode, mapper, flags); michael@0: return SkNEW_ARGS(SkTwoPointConicalGradient, michael@0: (start, startRadius, end, endRadius, desc)); michael@0: } michael@0: michael@0: SkShader* SkGradientShader::CreateSweep(SkScalar cx, SkScalar cy, michael@0: const SkColor colors[], michael@0: const SkScalar pos[], michael@0: int colorCount, SkUnitMapper* mapper, michael@0: uint32_t flags) { michael@0: if (NULL == colors || colorCount < 1) { michael@0: return NULL; michael@0: } michael@0: EXPAND_1_COLOR(colorCount); michael@0: michael@0: SkGradientShaderBase::Descriptor desc; michael@0: desc_init(&desc, colors, pos, colorCount, SkShader::kClamp_TileMode, mapper, flags); michael@0: return SkNEW_ARGS(SkSweepGradient, (cx, cy, desc)); michael@0: } michael@0: michael@0: SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkGradientShader) michael@0: SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLinearGradient) michael@0: SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkRadialGradient) michael@0: SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSweepGradient) michael@0: SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkTwoPointRadialGradient) michael@0: SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkTwoPointConicalGradient) michael@0: SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #if SK_SUPPORT_GPU michael@0: michael@0: #include "effects/GrTextureStripAtlas.h" michael@0: #include "GrTBackendEffectFactory.h" michael@0: #include "SkGr.h" michael@0: michael@0: GrGLGradientEffect::GrGLGradientEffect(const GrBackendEffectFactory& factory) michael@0: : INHERITED(factory) michael@0: , fCachedYCoord(SK_ScalarMax) { michael@0: } michael@0: michael@0: GrGLGradientEffect::~GrGLGradientEffect() { } michael@0: michael@0: void GrGLGradientEffect::emitUniforms(GrGLShaderBuilder* builder, EffectKey key) { michael@0: michael@0: if (GrGradientEffect::kTwo_ColorType == ColorTypeFromKey(key)) { // 2 Color case michael@0: fColorStartUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, michael@0: kVec4f_GrSLType, "GradientStartColor"); michael@0: fColorEndUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, michael@0: kVec4f_GrSLType, "GradientEndColor"); michael@0: michael@0: } else if (GrGradientEffect::kThree_ColorType == ColorTypeFromKey(key)){ // 3 Color Case michael@0: fColorStartUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, michael@0: kVec4f_GrSLType, "GradientStartColor"); michael@0: fColorMidUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, michael@0: kVec4f_GrSLType, "GradientMidColor"); michael@0: fColorEndUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, michael@0: kVec4f_GrSLType, "GradientEndColor"); michael@0: michael@0: } else { // if not a fast case michael@0: fFSYUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, michael@0: kFloat_GrSLType, "GradientYCoordFS"); michael@0: } michael@0: } michael@0: michael@0: static inline void set_color_uni(const GrGLUniformManager& uman, michael@0: const GrGLUniformManager::UniformHandle uni, michael@0: const SkColor* color) { michael@0: uman.set4f(uni, michael@0: SkColorGetR(*color) / 255.f, michael@0: SkColorGetG(*color) / 255.f, michael@0: SkColorGetB(*color) / 255.f, michael@0: SkColorGetA(*color) / 255.f); michael@0: } michael@0: michael@0: static inline void set_mul_color_uni(const GrGLUniformManager& uman, michael@0: const GrGLUniformManager::UniformHandle uni, michael@0: const SkColor* color){ michael@0: float a = SkColorGetA(*color) / 255.f; michael@0: float aDiv255 = a / 255.f; michael@0: uman.set4f(uni, michael@0: SkColorGetR(*color) * aDiv255, michael@0: SkColorGetG(*color) * aDiv255, michael@0: SkColorGetB(*color) * aDiv255, michael@0: a); michael@0: } michael@0: michael@0: void GrGLGradientEffect::setData(const GrGLUniformManager& uman, michael@0: const GrDrawEffect& drawEffect) { michael@0: michael@0: const GrGradientEffect& e = drawEffect.castEffect(); michael@0: michael@0: michael@0: if (GrGradientEffect::kTwo_ColorType == e.getColorType()){ michael@0: michael@0: if (GrGradientEffect::kBeforeInterp_PremulType == e.getPremulType()) { michael@0: set_mul_color_uni(uman, fColorStartUni, e.getColors(0)); michael@0: set_mul_color_uni(uman, fColorEndUni, e.getColors(1)); michael@0: } else { michael@0: set_color_uni(uman, fColorStartUni, e.getColors(0)); michael@0: set_color_uni(uman, fColorEndUni, e.getColors(1)); michael@0: } michael@0: michael@0: } else if (GrGradientEffect::kThree_ColorType == e.getColorType()){ michael@0: michael@0: if (GrGradientEffect::kBeforeInterp_PremulType == e.getPremulType()) { michael@0: set_mul_color_uni(uman, fColorStartUni, e.getColors(0)); michael@0: set_mul_color_uni(uman, fColorMidUni, e.getColors(1)); michael@0: set_mul_color_uni(uman, fColorEndUni, e.getColors(2)); michael@0: } else { michael@0: set_color_uni(uman, fColorStartUni, e.getColors(0)); michael@0: set_color_uni(uman, fColorMidUni, e.getColors(1)); michael@0: set_color_uni(uman, fColorEndUni, e.getColors(2)); michael@0: } michael@0: } else { michael@0: michael@0: SkScalar yCoord = e.getYCoord(); michael@0: if (yCoord != fCachedYCoord) { michael@0: uman.set1f(fFSYUni, yCoord); michael@0: fCachedYCoord = yCoord; michael@0: } michael@0: } michael@0: } michael@0: michael@0: michael@0: GrGLEffect::EffectKey GrGLGradientEffect::GenBaseGradientKey(const GrDrawEffect& drawEffect) { michael@0: const GrGradientEffect& e = drawEffect.castEffect(); michael@0: michael@0: EffectKey key = 0; michael@0: michael@0: if (GrGradientEffect::kTwo_ColorType == e.getColorType()) { michael@0: key |= kTwoColorKey; michael@0: } else if (GrGradientEffect::kThree_ColorType == e.getColorType()){ michael@0: key |= kThreeColorKey; michael@0: } michael@0: michael@0: if (GrGradientEffect::kBeforeInterp_PremulType == e.getPremulType()) { michael@0: key |= kPremulBeforeInterpKey; michael@0: } michael@0: michael@0: return key; michael@0: } michael@0: michael@0: void GrGLGradientEffect::emitColor(GrGLShaderBuilder* builder, michael@0: const char* gradientTValue, michael@0: EffectKey key, michael@0: const char* outputColor, michael@0: const char* inputColor, michael@0: const TextureSamplerArray& samplers) { michael@0: if (GrGradientEffect::kTwo_ColorType == ColorTypeFromKey(key)){ michael@0: builder->fsCodeAppendf("\tvec4 colorTemp = mix(%s, %s, clamp(%s, 0.0, 1.0));\n", michael@0: builder->getUniformVariable(fColorStartUni).c_str(), michael@0: builder->getUniformVariable(fColorEndUni).c_str(), michael@0: gradientTValue); michael@0: // Note that we could skip this step if both colors are known to be opaque. Two michael@0: // considerations: michael@0: // The gradient SkShader reporting opaque is more restrictive than necessary in the two pt michael@0: // case. Make sure the key reflects this optimization (and note that it can use the same michael@0: // shader as thekBeforeIterp case). This same optimization applies to the 3 color case below. michael@0: if (GrGradientEffect::kAfterInterp_PremulType == PremulTypeFromKey(key)) { michael@0: builder->fsCodeAppend("\tcolorTemp.rgb *= colorTemp.a;\n"); michael@0: } michael@0: michael@0: builder->fsCodeAppendf("\t%s = %s;\n", outputColor, michael@0: (GrGLSLExpr4(inputColor) * GrGLSLExpr4("colorTemp")).c_str()); michael@0: } else if (GrGradientEffect::kThree_ColorType == ColorTypeFromKey(key)){ michael@0: builder->fsCodeAppendf("\tfloat oneMinus2t = 1.0 - (2.0 * (%s));\n", michael@0: gradientTValue); michael@0: builder->fsCodeAppendf("\tvec4 colorTemp = clamp(oneMinus2t, 0.0, 1.0) * %s;\n", michael@0: builder->getUniformVariable(fColorStartUni).c_str()); michael@0: if (kTegra3_GrGLRenderer == builder->ctxInfo().renderer()) { michael@0: // The Tegra3 compiler will sometimes never return if we have michael@0: // min(abs(oneMinus2t), 1.0), or do the abs first in a separate expression. michael@0: builder->fsCodeAppend("\tfloat minAbs = abs(oneMinus2t);\n"); michael@0: builder->fsCodeAppend("\tminAbs = minAbs > 1.0 ? 1.0 : minAbs;\n"); michael@0: builder->fsCodeAppendf("\tcolorTemp += (1.0 - minAbs) * %s;\n", michael@0: builder->getUniformVariable(fColorMidUni).c_str()); michael@0: } else { michael@0: builder->fsCodeAppendf("\tcolorTemp += (1.0 - min(abs(oneMinus2t), 1.0)) * %s;\n", michael@0: builder->getUniformVariable(fColorMidUni).c_str()); michael@0: } michael@0: builder->fsCodeAppendf("\tcolorTemp += clamp(-oneMinus2t, 0.0, 1.0) * %s;\n", michael@0: builder->getUniformVariable(fColorEndUni).c_str()); michael@0: if (GrGradientEffect::kAfterInterp_PremulType == PremulTypeFromKey(key)) { michael@0: builder->fsCodeAppend("\tcolorTemp.rgb *= colorTemp.a;\n"); michael@0: } michael@0: michael@0: builder->fsCodeAppendf("\t%s = %s;\n", outputColor, michael@0: (GrGLSLExpr4(inputColor) * GrGLSLExpr4("colorTemp")).c_str()); michael@0: } else { michael@0: builder->fsCodeAppendf("\tvec2 coord = vec2(%s, %s);\n", michael@0: gradientTValue, michael@0: builder->getUniformVariable(fFSYUni).c_str()); michael@0: builder->fsCodeAppendf("\t%s = ", outputColor); michael@0: builder->fsAppendTextureLookupAndModulate(inputColor, michael@0: samplers[0], michael@0: "coord"); michael@0: builder->fsCodeAppend(";\n"); michael@0: } michael@0: } michael@0: michael@0: ///////////////////////////////////////////////////////////////////// michael@0: michael@0: GrGradientEffect::GrGradientEffect(GrContext* ctx, michael@0: const SkGradientShaderBase& shader, michael@0: const SkMatrix& matrix, michael@0: SkShader::TileMode tileMode) { michael@0: michael@0: fIsOpaque = shader.isOpaque(); michael@0: michael@0: SkShader::GradientInfo info; michael@0: SkScalar pos[3] = {0}; michael@0: michael@0: info.fColorCount = 3; michael@0: info.fColors = &fColors[0]; michael@0: info.fColorOffsets = &pos[0]; michael@0: shader.asAGradient(&info); michael@0: michael@0: // The two and three color specializations do not currently support tiling. michael@0: bool foundSpecialCase = false; michael@0: if (SkShader::kClamp_TileMode == info.fTileMode) { michael@0: if (2 == info.fColorCount) { michael@0: fRow = -1; // flag for no atlas michael@0: fColorType = kTwo_ColorType; michael@0: foundSpecialCase = true; michael@0: } else if (3 == info.fColorCount && michael@0: (SkScalarAbs(pos[1] - SK_ScalarHalf) < SK_Scalar1 / 1000)) { // 3 color symmetric michael@0: fRow = -1; // flag for no atlas michael@0: fColorType = kThree_ColorType; michael@0: foundSpecialCase = true; michael@0: } michael@0: } michael@0: if (foundSpecialCase) { michael@0: if (SkGradientShader::kInterpolateColorsInPremul_Flag & info.fGradientFlags) { michael@0: fPremulType = kBeforeInterp_PremulType; michael@0: } else { michael@0: fPremulType = kAfterInterp_PremulType; michael@0: } michael@0: fCoordTransform.reset(kCoordSet, matrix); michael@0: } else { michael@0: // doesn't matter how this is set, just be consistent because it is part of the effect key. michael@0: fPremulType = kBeforeInterp_PremulType; michael@0: SkBitmap bitmap; michael@0: shader.getGradientTableBitmap(&bitmap); michael@0: fColorType = kTexture_ColorType; michael@0: michael@0: GrTextureStripAtlas::Desc desc; michael@0: desc.fWidth = bitmap.width(); michael@0: desc.fHeight = 32; michael@0: desc.fRowHeight = bitmap.height(); michael@0: desc.fContext = ctx; michael@0: desc.fConfig = SkImageInfo2GrPixelConfig(bitmap.colorType(), bitmap.alphaType()); michael@0: fAtlas = GrTextureStripAtlas::GetAtlas(desc); michael@0: SkASSERT(NULL != fAtlas); michael@0: michael@0: // We always filter the gradient table. Each table is one row of a texture, always y-clamp. michael@0: GrTextureParams params; michael@0: params.setFilterMode(GrTextureParams::kBilerp_FilterMode); michael@0: params.setTileModeX(tileMode); michael@0: michael@0: fRow = fAtlas->lockRow(bitmap); michael@0: if (-1 != fRow) { michael@0: fYCoord = fAtlas->getYOffset(fRow) + SK_ScalarHalf * michael@0: fAtlas->getVerticalScaleFactor(); michael@0: fCoordTransform.reset(kCoordSet, matrix, fAtlas->getTexture()); michael@0: fTextureAccess.reset(fAtlas->getTexture(), params); michael@0: } else { michael@0: GrTexture* texture = GrLockAndRefCachedBitmapTexture(ctx, bitmap, ¶ms); michael@0: fCoordTransform.reset(kCoordSet, matrix, texture); michael@0: fTextureAccess.reset(texture, params); michael@0: fYCoord = SK_ScalarHalf; michael@0: michael@0: // Unlock immediately, this is not great, but we don't have a way of michael@0: // knowing when else to unlock it currently, so it may get purged from michael@0: // the cache, but it'll still be ref'd until it's no longer being used. michael@0: GrUnlockAndUnrefCachedBitmapTexture(texture); michael@0: } michael@0: this->addTextureAccess(&fTextureAccess); michael@0: } michael@0: this->addCoordTransform(&fCoordTransform); michael@0: } michael@0: michael@0: GrGradientEffect::~GrGradientEffect() { michael@0: if (this->useAtlas()) { michael@0: fAtlas->unlockRow(fRow); michael@0: } michael@0: } michael@0: michael@0: bool GrGradientEffect::onIsEqual(const GrEffect& effect) const { michael@0: const GrGradientEffect& s = CastEffect(effect); michael@0: michael@0: if (this->fColorType == s.getColorType()){ michael@0: michael@0: if (kTwo_ColorType == fColorType) { michael@0: if (*this->getColors(0) != *s.getColors(0) || michael@0: *this->getColors(1) != *s.getColors(1)) { michael@0: return false; michael@0: } michael@0: } else if (kThree_ColorType == fColorType) { michael@0: if (*this->getColors(0) != *s.getColors(0) || michael@0: *this->getColors(1) != *s.getColors(1) || michael@0: *this->getColors(2) != *s.getColors(2)) { michael@0: return false; michael@0: } michael@0: } else { michael@0: if (fYCoord != s.getYCoord()) { michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: return fTextureAccess.getTexture() == s.fTextureAccess.getTexture() && michael@0: fTextureAccess.getParams().getTileModeX() == michael@0: s.fTextureAccess.getParams().getTileModeX() && michael@0: this->useAtlas() == s.useAtlas() && michael@0: fCoordTransform.getMatrix().cheapEqualTo(s.fCoordTransform.getMatrix()); michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: void GrGradientEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const { michael@0: if (fIsOpaque && (kA_GrColorComponentFlag & *validFlags) && 0xff == GrColorUnpackA(*color)) { michael@0: *validFlags = kA_GrColorComponentFlag; michael@0: } else { michael@0: *validFlags = 0; michael@0: } michael@0: } michael@0: michael@0: int GrGradientEffect::RandomGradientParams(SkRandom* random, michael@0: SkColor colors[], michael@0: SkScalar** stops, michael@0: SkShader::TileMode* tm) { michael@0: int outColors = random->nextRangeU(1, kMaxRandomGradientColors); michael@0: michael@0: // if one color, omit stops, otherwise randomly decide whether or not to michael@0: if (outColors == 1 || (outColors >= 2 && random->nextBool())) { michael@0: *stops = NULL; michael@0: } michael@0: michael@0: SkScalar stop = 0.f; michael@0: for (int i = 0; i < outColors; ++i) { michael@0: colors[i] = random->nextU(); michael@0: if (NULL != *stops) { michael@0: (*stops)[i] = stop; michael@0: stop = i < outColors - 1 ? stop + random->nextUScalar1() * (1.f - stop) : 1.f; michael@0: } michael@0: } michael@0: *tm = static_cast(random->nextULessThan(SkShader::kTileModeCount)); michael@0: michael@0: return outColors; michael@0: } michael@0: michael@0: #endif