SkGradientShader.cpp revision d3b28e8847358460aeae745da44e474f6a7ab25d
1589708bf7c706348b763e8277004cb160b202bdbrileya@google.com/*
2589708bf7c706348b763e8277004cb160b202bdbrileya@google.com * Copyright 2006 The Android Open Source Project
3589708bf7c706348b763e8277004cb160b202bdbrileya@google.com *
4589708bf7c706348b763e8277004cb160b202bdbrileya@google.com * Use of this source code is governed by a BSD-style license that can be
5589708bf7c706348b763e8277004cb160b202bdbrileya@google.com * found in the LICENSE file.
6589708bf7c706348b763e8277004cb160b202bdbrileya@google.com */
7589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
8589708bf7c706348b763e8277004cb160b202bdbrileya@google.com#include "SkGradientShaderPriv.h"
9589708bf7c706348b763e8277004cb160b202bdbrileya@google.com#include "SkLinearGradient.h"
10589708bf7c706348b763e8277004cb160b202bdbrileya@google.com#include "SkRadialGradient.h"
11589708bf7c706348b763e8277004cb160b202bdbrileya@google.com#include "SkTwoPointRadialGradient.h"
12589708bf7c706348b763e8277004cb160b202bdbrileya@google.com#include "SkTwoPointConicalGradient.h"
13589708bf7c706348b763e8277004cb160b202bdbrileya@google.com#include "SkSweepGradient.h"
14589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
15437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.comSkGradientShaderBase::SkGradientShaderBase(const Descriptor& desc) {
16437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.com    SkASSERT(desc.fCount > 1);
17589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
1853783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org    fCacheAlpha = 256;  // init to a value that paint.getAlpha() can't return
1953783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org
20437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.com    fMapper = desc.fMapper;
21437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.com    SkSafeRef(fMapper);
223d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com    fGradFlags = SkToU8(desc.fFlags);
23589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
24437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.com    SkASSERT((unsigned)desc.fTileMode < SkShader::kTileModeCount);
25589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    SkASSERT(SkShader::kTileModeCount == SK_ARRAY_COUNT(gTileProcs));
26437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.com    fTileMode = desc.fTileMode;
27437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.com    fTileProc = gTileProcs[desc.fTileMode];
28589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
2953783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org    fCache16 = fCache16Storage = NULL;
3053783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org    fCache32 = NULL;
3153783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org    fCache32PixelRef = NULL;
3253783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org
33589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    /*  Note: we let the caller skip the first and/or last position.
34589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        i.e. pos[0] = 0.3, pos[1] = 0.7
35589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        In these cases, we insert dummy entries to ensure that the final data
36589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        will be bracketed by [0, 1].
37589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        i.e. our_pos[0] = 0, our_pos[1] = 0.3, our_pos[2] = 0.7, our_pos[3] = 1
38589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
39589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        Thus colorCount (the caller's value, and fColorCount (our value) may
40589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        differ by up to 2. In the above example:
41589708bf7c706348b763e8277004cb160b202bdbrileya@google.com            colorCount = 2
42589708bf7c706348b763e8277004cb160b202bdbrileya@google.com            fColorCount = 4
43589708bf7c706348b763e8277004cb160b202bdbrileya@google.com     */
44437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.com    fColorCount = desc.fCount;
45589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    // check if we need to add in dummy start and/or end position/colors
46589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    bool dummyFirst = false;
47589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    bool dummyLast = false;
48437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.com    if (desc.fPos) {
49437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.com        dummyFirst = desc.fPos[0] != 0;
50437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.com        dummyLast = desc.fPos[desc.fCount - 1] != SK_Scalar1;
51589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        fColorCount += dummyFirst + dummyLast;
52589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    }
53589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
54589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    if (fColorCount > kColorStorageCount) {
55589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        size_t size = sizeof(SkColor) + sizeof(Rec);
56589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        fOrigColors = reinterpret_cast<SkColor*>(
57589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                                        sk_malloc_throw(size * fColorCount));
58589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    }
59589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    else {
60589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        fOrigColors = fStorage;
61589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    }
62589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
63589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    // Now copy over the colors, adding the dummies as needed
64589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    {
65589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        SkColor* origColors = fOrigColors;
66589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        if (dummyFirst) {
67437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.com            *origColors++ = desc.fColors[0];
68589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        }
69437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.com        memcpy(origColors, desc.fColors, desc.fCount * sizeof(SkColor));
70589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        if (dummyLast) {
71437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.com            origColors += desc.fCount;
72437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.com            *origColors = desc.fColors[desc.fCount - 1];
73589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        }
74589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    }
75589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
76589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    fRecs = (Rec*)(fOrigColors + fColorCount);
77589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    if (fColorCount > 2) {
78589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        Rec* recs = fRecs;
79589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        recs->fPos = 0;
80589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        //  recs->fScale = 0; // unused;
81589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        recs += 1;
82437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.com        if (desc.fPos) {
83589708bf7c706348b763e8277004cb160b202bdbrileya@google.com            /*  We need to convert the user's array of relative positions into
84589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                fixed-point positions and scale factors. We need these results
85589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                to be strictly monotonic (no two values equal or out of order).
86589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                Hence this complex loop that just jams a zero for the scale
87589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                value if it sees a segment out of order, and it assures that
88589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                we start at 0 and end at 1.0
89589708bf7c706348b763e8277004cb160b202bdbrileya@google.com            */
90589708bf7c706348b763e8277004cb160b202bdbrileya@google.com            SkFixed prev = 0;
91589708bf7c706348b763e8277004cb160b202bdbrileya@google.com            int startIndex = dummyFirst ? 0 : 1;
92437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.com            int count = desc.fCount + dummyLast;
93589708bf7c706348b763e8277004cb160b202bdbrileya@google.com            for (int i = startIndex; i < count; i++) {
94589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                // force the last value to be 1.0
95589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                SkFixed curr;
96437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.com                if (i == desc.fCount) {  // we're really at the dummyLast
97589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                    curr = SK_Fixed1;
98589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                } else {
99437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.com                    curr = SkScalarToFixed(desc.fPos[i]);
100589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                }
101589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                // pin curr withing range
102589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                if (curr < 0) {
103589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                    curr = 0;
104589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                } else if (curr > SK_Fixed1) {
105589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                    curr = SK_Fixed1;
106589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                }
107589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                recs->fPos = curr;
108589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                if (curr > prev) {
109589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                    recs->fScale = (1 << 24) / (curr - prev);
110589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                } else {
111589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                    recs->fScale = 0; // ignore this segment
112589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                }
113589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                // get ready for the next value
114589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                prev = curr;
115589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                recs += 1;
116589708bf7c706348b763e8277004cb160b202bdbrileya@google.com            }
117589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        } else {    // assume even distribution
118437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.com            SkFixed dp = SK_Fixed1 / (desc.fCount - 1);
119589708bf7c706348b763e8277004cb160b202bdbrileya@google.com            SkFixed p = dp;
120437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.com            SkFixed scale = (desc.fCount - 1) << 8;  // (1 << 24) / dp
12144d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org            for (int i = 1; i < desc.fCount - 1; i++) {
122589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                recs->fPos   = p;
123589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                recs->fScale = scale;
124589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                recs += 1;
125589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                p += dp;
126589708bf7c706348b763e8277004cb160b202bdbrileya@google.com            }
12744d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org            recs->fPos = SK_Fixed1;
128811f20de4d20b80ef40ff954acd1e816388b30becommit-bot@chromium.org            recs->fScale = scale;
129589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        }
130589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    }
131589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    this->initCommon();
132589708bf7c706348b763e8277004cb160b202bdbrileya@google.com}
133589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
1343d3a860d0ba878adb905512a45c500a67532b0a3reed@google.comstatic uint32_t pack_mode_flags(SkShader::TileMode mode, uint32_t flags) {
1353d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com    SkASSERT(0 == (flags >> 28));
1363d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com    SkASSERT(0 == ((uint32_t)mode >> 4));
1373d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com    return (flags << 4) | mode;
1383d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com}
1393d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com
1403d3a860d0ba878adb905512a45c500a67532b0a3reed@google.comstatic SkShader::TileMode unpack_mode(uint32_t packed) {
1413d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com    return (SkShader::TileMode)(packed & 0xF);
1423d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com}
1433d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com
1443d3a860d0ba878adb905512a45c500a67532b0a3reed@google.comstatic uint32_t unpack_flags(uint32_t packed) {
1453d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com    return packed >> 4;
1463d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com}
1473d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com
1488b0e8ac5f582de80356019406e2975079bf0829dcommit-bot@chromium.orgSkGradientShaderBase::SkGradientShaderBase(SkReadBuffer& buffer) : INHERITED(buffer) {
14953783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org    fCacheAlpha = 256;
15053783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org
151353482251e61971a8cf3a60bbb6910f482be634freed@google.com    fMapper = buffer.readUnitMapper();
152589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
15353783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org    fCache16 = fCache16Storage = NULL;
15453783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org    fCache32 = NULL;
15553783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org    fCache32PixelRef = NULL;
15653783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org
157c73dd5c6880739f26216f198c757028fd28df1a4djsollen@google.com    int colorCount = fColorCount = buffer.getArrayCount();
158589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    if (colorCount > kColorStorageCount) {
159ef74fa189b738e13295d6a96f86a6e10223505a8commit-bot@chromium.org        size_t allocSize = (sizeof(SkColor) + sizeof(SkPMColor) + sizeof(Rec)) * colorCount;
160ef74fa189b738e13295d6a96f86a6e10223505a8commit-bot@chromium.org        if (buffer.validateAvailable(allocSize)) {
161ef74fa189b738e13295d6a96f86a6e10223505a8commit-bot@chromium.org            fOrigColors = reinterpret_cast<SkColor*>(sk_malloc_throw(allocSize));
162ef74fa189b738e13295d6a96f86a6e10223505a8commit-bot@chromium.org        } else {
163ef74fa189b738e13295d6a96f86a6e10223505a8commit-bot@chromium.org            fOrigColors =  NULL;
164ef74fa189b738e13295d6a96f86a6e10223505a8commit-bot@chromium.org            colorCount = fColorCount = 0;
165ef74fa189b738e13295d6a96f86a6e10223505a8commit-bot@chromium.org        }
166589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    } else {
167589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        fOrigColors = fStorage;
168589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    }
169025128811219dc45fd99b6c4d1d14f833cf7a26ecommit-bot@chromium.org    buffer.readColorArray(fOrigColors, colorCount);
170589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
1713d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com    {
1723d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com        uint32_t packed = buffer.readUInt();
1733d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com        fGradFlags = SkToU8(unpack_flags(packed));
1743d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com        fTileMode = unpack_mode(packed);
1753d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com    }
176589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    fTileProc = gTileProcs[fTileMode];
177589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    fRecs = (Rec*)(fOrigColors + colorCount);
178589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    if (colorCount > 2) {
179589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        Rec* recs = fRecs;
180589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        recs[0].fPos = 0;
181589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        for (int i = 1; i < colorCount; i++) {
182c73dd5c6880739f26216f198c757028fd28df1a4djsollen@google.com            recs[i].fPos = buffer.readInt();
183c73dd5c6880739f26216f198c757028fd28df1a4djsollen@google.com            recs[i].fScale = buffer.readUInt();
184589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        }
185589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    }
186589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    buffer.readMatrix(&fPtsToUnit);
187589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    this->initCommon();
188589708bf7c706348b763e8277004cb160b202bdbrileya@google.com}
189589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
190589708bf7c706348b763e8277004cb160b202bdbrileya@google.comSkGradientShaderBase::~SkGradientShaderBase() {
19153783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org    if (fCache16Storage) {
19253783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org        sk_free(fCache16Storage);
19353783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org    }
19453783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org    SkSafeUnref(fCache32PixelRef);
195589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    if (fOrigColors != fStorage) {
196589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        sk_free(fOrigColors);
197589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    }
198589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    SkSafeUnref(fMapper);
199589708bf7c706348b763e8277004cb160b202bdbrileya@google.com}
200589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
201589708bf7c706348b763e8277004cb160b202bdbrileya@google.comvoid SkGradientShaderBase::initCommon() {
20253783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org    fFlags = 0;
203589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    unsigned colorAlpha = 0xFF;
204589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    for (int i = 0; i < fColorCount; i++) {
205589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        colorAlpha &= SkColorGetA(fOrigColors[i]);
206589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    }
207589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    fColorsAreOpaque = colorAlpha == 0xFF;
208589708bf7c706348b763e8277004cb160b202bdbrileya@google.com}
209589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
2108b0e8ac5f582de80356019406e2975079bf0829dcommit-bot@chromium.orgvoid SkGradientShaderBase::flatten(SkWriteBuffer& buffer) const {
211589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    this->INHERITED::flatten(buffer);
212589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    buffer.writeFlattenable(fMapper);
213c73dd5c6880739f26216f198c757028fd28df1a4djsollen@google.com    buffer.writeColorArray(fOrigColors, fColorCount);
2143d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com    buffer.writeUInt(pack_mode_flags(fTileMode, fGradFlags));
215589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    if (fColorCount > 2) {
216589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        Rec* recs = fRecs;
217589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        for (int i = 1; i < fColorCount; i++) {
218c73dd5c6880739f26216f198c757028fd28df1a4djsollen@google.com            buffer.writeInt(recs[i].fPos);
219c73dd5c6880739f26216f198c757028fd28df1a4djsollen@google.com            buffer.writeUInt(recs[i].fScale);
220589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        }
221589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    }
222589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    buffer.writeMatrix(fPtsToUnit);
223589708bf7c706348b763e8277004cb160b202bdbrileya@google.com}
224589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
225996402bddb4d886c38dee17341e5833500901933commit-bot@chromium.orgSkGradientShaderBase::GpuColorType SkGradientShaderBase::getGpuColorType(SkColor colors[3]) const {
226996402bddb4d886c38dee17341e5833500901933commit-bot@chromium.org    if (fColorCount <= 3) {
227996402bddb4d886c38dee17341e5833500901933commit-bot@chromium.org        memcpy(colors, fOrigColors, fColorCount * sizeof(SkColor));
228996402bddb4d886c38dee17341e5833500901933commit-bot@chromium.org    }
229996402bddb4d886c38dee17341e5833500901933commit-bot@chromium.org
230996402bddb4d886c38dee17341e5833500901933commit-bot@chromium.org    if (SkShader::kClamp_TileMode == fTileMode) {
231996402bddb4d886c38dee17341e5833500901933commit-bot@chromium.org        if (2 == fColorCount) {
232996402bddb4d886c38dee17341e5833500901933commit-bot@chromium.org            return kTwo_GpuColorType;
233996402bddb4d886c38dee17341e5833500901933commit-bot@chromium.org        } else if (3 == fColorCount &&
234996402bddb4d886c38dee17341e5833500901933commit-bot@chromium.org                   (SkScalarAbs(
235996402bddb4d886c38dee17341e5833500901933commit-bot@chromium.org                    SkFixedToScalar(fRecs[1].fPos) - SK_ScalarHalf) < SK_Scalar1 / 1000)) {
236996402bddb4d886c38dee17341e5833500901933commit-bot@chromium.org            return kThree_GpuColorType;
237996402bddb4d886c38dee17341e5833500901933commit-bot@chromium.org        }
238996402bddb4d886c38dee17341e5833500901933commit-bot@chromium.org    }
239996402bddb4d886c38dee17341e5833500901933commit-bot@chromium.org    return kTexture_GpuColorType;
240996402bddb4d886c38dee17341e5833500901933commit-bot@chromium.org}
241996402bddb4d886c38dee17341e5833500901933commit-bot@chromium.org
24244d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.orgvoid SkGradientShaderBase::FlipGradientColors(SkColor* colorDst, Rec* recDst,
24344d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org                                              SkColor* colorSrc, Rec* recSrc,
24444d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org                                              int count) {
24544d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org    SkAutoSTArray<8, SkColor> colorsTemp(count);
24644d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org    for (int i = 0; i < count; ++i) {
24744d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org        int offset = count - i - 1;
24844d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org        colorsTemp[i] = colorSrc[offset];
24944d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org    }
25044d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org    if (count > 2) {
25144d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org        SkAutoSTArray<8, Rec> recsTemp(count);
25244d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org        for (int i = 0; i < count; ++i) {
25344d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org            int offset = count - i - 1;
25444d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org            recsTemp[i].fPos = SK_Fixed1 - recSrc[offset].fPos;
25544d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org            recsTemp[i].fScale = recSrc[offset].fScale;
25644d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org        }
25744d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org        memcpy(recDst, recsTemp.get(), count * sizeof(Rec));
25844d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org    }
25944d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org    memcpy(colorDst, colorsTemp.get(), count * sizeof(SkColor));
26044d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org}
26144d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org
26244d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.orgvoid SkGradientShaderBase::flipGradientColors() {
263d3b28e8847358460aeae745da44e474f6a7ab25dskia.committer@gmail.com    FlipGradientColors(fOrigColors, fRecs, fOrigColors, fRecs, fColorCount);
26444d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org}
26544d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org
266589708bf7c706348b763e8277004cb160b202bdbrileya@google.combool SkGradientShaderBase::isOpaque() const {
267589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    return fColorsAreOpaque;
268589708bf7c706348b763e8277004cb160b202bdbrileya@google.com}
269589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
27053783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.orgbool SkGradientShaderBase::setContext(const SkBitmap& device,
27153783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org                                 const SkPaint& paint,
27253783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org                                 const SkMatrix& matrix) {
27353783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org    if (!this->INHERITED::setContext(device, paint, matrix)) {
27453783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org        return false;
27553783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org    }
276589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
27753783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org    const SkMatrix& inverse = this->getTotalInverse();
278001f4ed2fb62ecdc98ce2884d925de11b7516d23commit-bot@chromium.org
27953783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org    fDstToIndex.setConcat(fPtsToUnit, inverse);
280589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    fDstToIndexProc = fDstToIndex.getMapXYProc();
28153783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org    fDstToIndexClass = (uint8_t)SkShader::ComputeMatrixClass(fDstToIndex);
282589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
283589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    // now convert our colors in to PMColors
284589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    unsigned paintAlpha = this->getPaintAlpha();
285589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
286589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    fFlags = this->INHERITED::getFlags();
28753783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org    if (fColorsAreOpaque && paintAlpha == 0xFF) {
288589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        fFlags |= kOpaqueAlpha_Flag;
289589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    }
290589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    // we can do span16 as long as our individual colors are opaque,
291589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    // regardless of the paint's alpha
29253783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org    if (fColorsAreOpaque) {
293589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        fFlags |= kHasSpan16_Flag;
294589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    }
295589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
29653783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org    this->setCacheAlpha(paintAlpha);
29753783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org    return true;
298589708bf7c706348b763e8277004cb160b202bdbrileya@google.com}
299589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
30053783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.orgvoid SkGradientShaderBase::setCacheAlpha(U8CPU alpha) const {
30153783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org    // if the new alpha differs from the previous time we were called, inval our cache
30253783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org    // this will trigger the cache to be rebuilt.
30353783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org    // we don't care about the first time, since the cache ptrs will already be NULL
30453783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org    if (fCacheAlpha != alpha) {
30553783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org        fCache16 = NULL;            // inval the cache
30653783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org        fCache32 = NULL;            // inval the cache
30753783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org        fCacheAlpha = alpha;        // record the new alpha
30853783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org        // inform our subclasses
30953783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org        if (fCache32PixelRef) {
31053783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org            fCache32PixelRef->notifyPixelsChanged();
31153783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org        }
31253783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org    }
313589708bf7c706348b763e8277004cb160b202bdbrileya@google.com}
314589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
315589708bf7c706348b763e8277004cb160b202bdbrileya@google.com#define Fixed_To_Dot8(x)        (((x) + 0x80) >> 8)
316589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
317589708bf7c706348b763e8277004cb160b202bdbrileya@google.com/** We take the original colors, not our premultiplied PMColors, since we can
318589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    build a 16bit table as long as the original colors are opaque, even if the
319589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    paint specifies a non-opaque alpha.
320589708bf7c706348b763e8277004cb160b202bdbrileya@google.com*/
32153783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.orgvoid SkGradientShaderBase::Build16bitCache(uint16_t cache[], SkColor c0, SkColor c1,
32253783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org                                      int count) {
323589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    SkASSERT(count > 1);
324589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    SkASSERT(SkColorGetA(c0) == 0xFF);
325589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    SkASSERT(SkColorGetA(c1) == 0xFF);
326589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
327589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    SkFixed r = SkColorGetR(c0);
328589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    SkFixed g = SkColorGetG(c0);
329589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    SkFixed b = SkColorGetB(c0);
330589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
331589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    SkFixed dr = SkIntToFixed(SkColorGetR(c1) - r) / (count - 1);
332589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    SkFixed dg = SkIntToFixed(SkColorGetG(c1) - g) / (count - 1);
333589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    SkFixed db = SkIntToFixed(SkColorGetB(c1) - b) / (count - 1);
334589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
335589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    r = SkIntToFixed(r) + 0x8000;
336589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    g = SkIntToFixed(g) + 0x8000;
337589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    b = SkIntToFixed(b) + 0x8000;
338589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
339589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    do {
340589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        unsigned rr = r >> 16;
341589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        unsigned gg = g >> 16;
342589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        unsigned bb = b >> 16;
343589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        cache[0] = SkPackRGB16(SkR32ToR16(rr), SkG32ToG16(gg), SkB32ToB16(bb));
344589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        cache[kCache16Count] = SkDitherPack888ToRGB16(rr, gg, bb);
345589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        cache += 1;
346589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        r += dr;
347589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        g += dg;
348589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        b += db;
349589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    } while (--count != 0);
350589708bf7c706348b763e8277004cb160b202bdbrileya@google.com}
351589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
3523d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com/*
3533d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com *  r,g,b used to be SkFixed, but on gcc (4.2.1 mac and 4.6.3 goobuntu) in
3543d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com *  release builds, we saw a compiler error where the 0xFF parameter in
3553d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com *  SkPackARGB32() was being totally ignored whenever it was called with
3563d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com *  a non-zero add (e.g. 0x8000).
3573d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com *
3583d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com *  We found two work-arounds:
3593d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com *      1. change r,g,b to unsigned (or just one of them)
3603d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com *      2. change SkPackARGB32 to + its (a << SK_A32_SHIFT) value instead
3613d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com *         of using |
3623d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com *
3633d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com *  We chose #1 just because it was more localized.
3643d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com *  See http://code.google.com/p/skia/issues/detail?id=1113
3653d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com *
3663d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com *  The type SkUFixed encapsulate this need for unsigned, but logically Fixed.
3673d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com */
3683d3a860d0ba878adb905512a45c500a67532b0a3reed@google.comtypedef uint32_t SkUFixed;
3693d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com
37053783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.orgvoid SkGradientShaderBase::Build32bitCache(SkPMColor cache[], SkColor c0, SkColor c1,
37153783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org                                      int count, U8CPU paintAlpha, uint32_t gradFlags) {
372589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    SkASSERT(count > 1);
373589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
374589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    // need to apply paintAlpha to our two endpoints
3753d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com    uint32_t a0 = SkMulDiv255Round(SkColorGetA(c0), paintAlpha);
3763d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com    uint32_t a1 = SkMulDiv255Round(SkColorGetA(c1), paintAlpha);
377589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
378d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com
3793d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com    const bool interpInPremul = SkToBool(gradFlags &
3803d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com                           SkGradientShader::kInterpolateColorsInPremul_Flag);
3813d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com
3823d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com    uint32_t r0 = SkColorGetR(c0);
3833d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com    uint32_t g0 = SkColorGetG(c0);
3843d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com    uint32_t b0 = SkColorGetB(c0);
3853d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com
3863d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com    uint32_t r1 = SkColorGetR(c1);
3873d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com    uint32_t g1 = SkColorGetG(c1);
3883d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com    uint32_t b1 = SkColorGetB(c1);
3893d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com
3903d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com    if (interpInPremul) {
3913d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com        r0 = SkMulDiv255Round(r0, a0);
3923d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com        g0 = SkMulDiv255Round(g0, a0);
3933d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com        b0 = SkMulDiv255Round(b0, a0);
3943d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com
3953d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com        r1 = SkMulDiv255Round(r1, a1);
3963d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com        g1 = SkMulDiv255Round(g1, a1);
3973d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com        b1 = SkMulDiv255Round(b1, a1);
3983d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com    }
3993d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com
4003d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com    SkFixed da = SkIntToFixed(a1 - a0) / (count - 1);
4013d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com    SkFixed dr = SkIntToFixed(r1 - r0) / (count - 1);
4023d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com    SkFixed dg = SkIntToFixed(g1 - g0) / (count - 1);
4033d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com    SkFixed db = SkIntToFixed(b1 - b0) / (count - 1);
404589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
405d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com    /*  We pre-add 1/8 to avoid having to add this to our [0] value each time
406d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com        in the loop. Without this, the bias for each would be
407d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com            0x2000  0xA000  0xE000  0x6000
408d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com        With this trick, we can add 0 for the first (no-op) and just adjust the
409d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com        others.
410d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com     */
4113d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com    SkUFixed a = SkIntToFixed(a0) + 0x2000;
4123d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com    SkUFixed r = SkIntToFixed(r0) + 0x2000;
4133d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com    SkUFixed g = SkIntToFixed(g0) + 0x2000;
4143d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com    SkUFixed b = SkIntToFixed(b0) + 0x2000;
415d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com
416d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com    /*
417d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com     *  Our dither-cell (spatially) is
418d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com     *      0 2
419d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com     *      3 1
420d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com     *  Where
421d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com     *      [0] -> [-1/8 ... 1/8 ) values near 0
422d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com     *      [1] -> [ 1/8 ... 3/8 ) values near 1/4
423d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com     *      [2] -> [ 3/8 ... 5/8 ) values near 1/2
424d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com     *      [3] -> [ 5/8 ... 7/8 ) values near 3/4
425d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com     */
426589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
4273d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com    if (0xFF == a0 && 0 == da) {
428d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com        do {
429d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com            cache[kCache32Count*0] = SkPackARGB32(0xFF, (r + 0     ) >> 16,
430d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com                                                        (g + 0     ) >> 16,
431d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com                                                        (b + 0     ) >> 16);
432d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com            cache[kCache32Count*1] = SkPackARGB32(0xFF, (r + 0x8000) >> 16,
433d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com                                                        (g + 0x8000) >> 16,
434d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com                                                        (b + 0x8000) >> 16);
435d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com            cache[kCache32Count*2] = SkPackARGB32(0xFF, (r + 0xC000) >> 16,
436d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com                                                        (g + 0xC000) >> 16,
437d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com                                                        (b + 0xC000) >> 16);
438d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com            cache[kCache32Count*3] = SkPackARGB32(0xFF, (r + 0x4000) >> 16,
439d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com                                                        (g + 0x4000) >> 16,
440d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com                                                        (b + 0x4000) >> 16);
441d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com            cache += 1;
442d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com            r += dr;
443d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com            g += dg;
444d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com            b += db;
445d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com        } while (--count != 0);
4463d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com    } else if (interpInPremul) {
4473d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com        do {
4483d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com            cache[kCache32Count*0] = SkPackARGB32((a + 0     ) >> 16,
4493d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com                                                  (r + 0     ) >> 16,
4503d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com                                                  (g + 0     ) >> 16,
4513d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com                                                  (b + 0     ) >> 16);
4523d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com            cache[kCache32Count*1] = SkPackARGB32((a + 0x8000) >> 16,
4533d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com                                                  (r + 0x8000) >> 16,
4543d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com                                                  (g + 0x8000) >> 16,
4553d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com                                                  (b + 0x8000) >> 16);
4563d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com            cache[kCache32Count*2] = SkPackARGB32((a + 0xC000) >> 16,
4573d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com                                                  (r + 0xC000) >> 16,
4583d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com                                                  (g + 0xC000) >> 16,
4593d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com                                                  (b + 0xC000) >> 16);
4603d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com            cache[kCache32Count*3] = SkPackARGB32((a + 0x4000) >> 16,
4613d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com                                                  (r + 0x4000) >> 16,
4623d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com                                                  (g + 0x4000) >> 16,
4633d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com                                                  (b + 0x4000) >> 16);
4643d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com            cache += 1;
4653d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com            a += da;
4663d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com            r += dr;
4673d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com            g += dg;
4683d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com            b += db;
4693d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com        } while (--count != 0);
4703d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com    } else {    // interpolate in unpreml space
471d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com        do {
472d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com            cache[kCache32Count*0] = SkPremultiplyARGBInline((a + 0     ) >> 16,
473d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com                                                             (r + 0     ) >> 16,
474d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com                                                             (g + 0     ) >> 16,
475d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com                                                             (b + 0     ) >> 16);
476d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com            cache[kCache32Count*1] = SkPremultiplyARGBInline((a + 0x8000) >> 16,
477d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com                                                             (r + 0x8000) >> 16,
478d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com                                                             (g + 0x8000) >> 16,
479d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com                                                             (b + 0x8000) >> 16);
480d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com            cache[kCache32Count*2] = SkPremultiplyARGBInline((a + 0xC000) >> 16,
481d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com                                                             (r + 0xC000) >> 16,
482d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com                                                             (g + 0xC000) >> 16,
483d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com                                                             (b + 0xC000) >> 16);
484d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com            cache[kCache32Count*3] = SkPremultiplyARGBInline((a + 0x4000) >> 16,
485d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com                                                             (r + 0x4000) >> 16,
486d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com                                                             (g + 0x4000) >> 16,
487d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com                                                             (b + 0x4000) >> 16);
488d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com            cache += 1;
489d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com            a += da;
490d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com            r += dr;
491d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com            g += dg;
492d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com            b += db;
493d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com        } while (--count != 0);
494d8e0d6a87d56ece202cc529ec9269da3dcd3d6a8reed@google.com    }
495589708bf7c706348b763e8277004cb160b202bdbrileya@google.com}
496589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
497589708bf7c706348b763e8277004cb160b202bdbrileya@google.comstatic inline int SkFixedToFFFF(SkFixed x) {
498589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    SkASSERT((unsigned)x <= SK_Fixed1);
499589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    return x - (x >> 16);
500589708bf7c706348b763e8277004cb160b202bdbrileya@google.com}
501589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
502589708bf7c706348b763e8277004cb160b202bdbrileya@google.comstatic inline U16CPU bitsTo16(unsigned x, const unsigned bits) {
503589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    SkASSERT(x < (1U << bits));
504589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    if (6 == bits) {
505589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        return (x << 10) | (x << 4) | (x >> 2);
506589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    }
507589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    if (8 == bits) {
508589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        return (x << 8) | x;
509589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    }
510589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    sk_throw();
511589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    return 0;
512589708bf7c706348b763e8277004cb160b202bdbrileya@google.com}
513589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
51453783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.orgconst uint16_t* SkGradientShaderBase::getCache16() const {
51553783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org    if (fCache16 == NULL) {
51653783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org        // double the count for dither entries
51753783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org        const int entryCount = kCache16Count * 2;
51853783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org        const size_t allocSize = sizeof(uint16_t) * entryCount;
519589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
52053783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org        if (fCache16Storage == NULL) { // set the storage and our working ptr
52153783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org            fCache16Storage = (uint16_t*)sk_malloc_throw(allocSize);
52253783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org        }
52353783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org        fCache16 = fCache16Storage;
52453783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org        if (fColorCount == 2) {
52553783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org            Build16bitCache(fCache16, fOrigColors[0], fOrigColors[1],
52653783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org                            kCache16Count);
52753783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org        } else {
52853783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org            Rec* rec = fRecs;
52953783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org            int prevIndex = 0;
53053783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org            for (int i = 1; i < fColorCount; i++) {
53153783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org                int nextIndex = SkFixedToFFFF(rec[i].fPos) >> kCache16Shift;
53253783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org                SkASSERT(nextIndex < kCache16Count);
53353783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org
53453783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org                if (nextIndex > prevIndex)
53553783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org                    Build16bitCache(fCache16 + prevIndex, fOrigColors[i-1], fOrigColors[i], nextIndex - prevIndex + 1);
53653783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org                prevIndex = nextIndex;
53753783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org            }
538589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        }
539589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
54053783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org        if (fMapper) {
54153783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org            fCache16Storage = (uint16_t*)sk_malloc_throw(allocSize);
54253783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org            uint16_t* linear = fCache16;         // just computed linear data
54353783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org            uint16_t* mapped = fCache16Storage;  // storage for mapped data
54453783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org            SkUnitMapper* map = fMapper;
54553783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org            for (int i = 0; i < kCache16Count; i++) {
54653783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org                int index = map->mapUnit16(bitsTo16(i, kCache16Bits)) >> kCache16Shift;
54753783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org                mapped[i] = linear[index];
54853783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org                mapped[i + kCache16Count] = linear[index + kCache16Count];
54953783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org            }
55053783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org            sk_free(fCache16);
55153783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org            fCache16 = fCache16Storage;
552589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        }
553589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    }
55453783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org    return fCache16;
555589708bf7c706348b763e8277004cb160b202bdbrileya@google.com}
556589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
55753783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.orgconst SkPMColor* SkGradientShaderBase::getCache32() const {
55853783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org    if (fCache32 == NULL) {
55953783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org        SkImageInfo info;
56053783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org        info.fWidth = kCache32Count;
56153783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org        info.fHeight = 4;   // for our 4 dither rows
56253783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org        info.fAlphaType = kPremul_SkAlphaType;
56353783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org        info.fColorType = kN32_SkColorType;
564589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
56553783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org        if (NULL == fCache32PixelRef) {
56653783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org            fCache32PixelRef = SkMallocPixelRef::NewAllocate(info, 0, NULL);
567589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        }
56853783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org        fCache32 = (SkPMColor*)fCache32PixelRef->getAddr();
56953783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org        if (fColorCount == 2) {
57053783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org            Build32bitCache(fCache32, fOrigColors[0], fOrigColors[1],
57153783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org                            kCache32Count, fCacheAlpha, fGradFlags);
57253783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org        } else {
57353783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org            Rec* rec = fRecs;
57453783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org            int prevIndex = 0;
57553783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org            for (int i = 1; i < fColorCount; i++) {
57653783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org                int nextIndex = SkFixedToFFFF(rec[i].fPos) >> kCache32Shift;
57753783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org                SkASSERT(nextIndex < kCache32Count);
57853783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org
57953783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org                if (nextIndex > prevIndex)
58053783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org                    Build32bitCache(fCache32 + prevIndex, fOrigColors[i-1],
58153783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org                                    fOrigColors[i], nextIndex - prevIndex + 1,
58253783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org                                    fCacheAlpha, fGradFlags);
58353783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org                prevIndex = nextIndex;
58453783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org            }
585589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        }
586001f4ed2fb62ecdc98ce2884d925de11b7516d23commit-bot@chromium.org
58753783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org        if (fMapper) {
58853783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org            SkMallocPixelRef* newPR = SkMallocPixelRef::NewAllocate(info, 0, NULL);
58953783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org            SkPMColor* linear = fCache32;           // just computed linear data
59053783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org            SkPMColor* mapped = (SkPMColor*)newPR->getAddr();    // storage for mapped data
59153783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org            SkUnitMapper* map = fMapper;
59253783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org            for (int i = 0; i < kCache32Count; i++) {
59353783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org                int index = map->mapUnit16((i << 8) | i) >> 8;
59453783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org                mapped[i + kCache32Count*0] = linear[index + kCache32Count*0];
59553783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org                mapped[i + kCache32Count*1] = linear[index + kCache32Count*1];
59653783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org                mapped[i + kCache32Count*2] = linear[index + kCache32Count*2];
59753783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org                mapped[i + kCache32Count*3] = linear[index + kCache32Count*3];
59853783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org            }
59953783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org            fCache32PixelRef->unref();
60053783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org            fCache32PixelRef = newPR;
60153783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org            fCache32 = (SkPMColor*)newPR->getAddr();
60253783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org        }
603001f4ed2fb62ecdc98ce2884d925de11b7516d23commit-bot@chromium.org    }
60453783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org    return fCache32;
605589708bf7c706348b763e8277004cb160b202bdbrileya@google.com}
606589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
607589708bf7c706348b763e8277004cb160b202bdbrileya@google.com/*
608589708bf7c706348b763e8277004cb160b202bdbrileya@google.com *  Because our caller might rebuild the same (logically the same) gradient
609589708bf7c706348b763e8277004cb160b202bdbrileya@google.com *  over and over, we'd like to return exactly the same "bitmap" if possible,
610589708bf7c706348b763e8277004cb160b202bdbrileya@google.com *  allowing the client to utilize a cache of our bitmap (e.g. with a GPU).
611589708bf7c706348b763e8277004cb160b202bdbrileya@google.com *  To do that, we maintain a private cache of built-bitmaps, based on our
612589708bf7c706348b763e8277004cb160b202bdbrileya@google.com *  colors and positions. Note: we don't try to flatten the fMapper, so if one
613589708bf7c706348b763e8277004cb160b202bdbrileya@google.com *  is present, we skip the cache for now.
614589708bf7c706348b763e8277004cb160b202bdbrileya@google.com */
6151c6d64b78b24083ee9fd7411dac8a4a7e2d03a3crileya@google.comvoid SkGradientShaderBase::getGradientTableBitmap(SkBitmap* bitmap) const {
616589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    // our caller assumes no external alpha, so we ensure that our cache is
617589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    // built with 0xFF
61853783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org    this->setCacheAlpha(0xFF);
619589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
620589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    // don't have a way to put the mapper into our cache-key yet
621589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    if (fMapper) {
62253783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org        // force our cahce32pixelref to be built
62353783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org        (void)this->getCache32();
624e24ad23ae67ffcb0dc545b7e426cf08d102e0868commit-bot@chromium.org        bitmap->setConfig(SkImageInfo::MakeN32Premul(kCache32Count, 1));
62553783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org        bitmap->setPixelRef(fCache32PixelRef);
626589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        return;
627589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    }
628589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
6293d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com    // build our key: [numColors + colors[] + {positions[]} + flags ]
6303d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com    int count = 1 + fColorCount + 1;
631589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    if (fColorCount > 2) {
632589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        count += fColorCount - 1;    // fRecs[].fPos
633589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    }
634589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
635589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    SkAutoSTMalloc<16, int32_t> storage(count);
636589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    int32_t* buffer = storage.get();
637589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
638589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    *buffer++ = fColorCount;
639589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    memcpy(buffer, fOrigColors, fColorCount * sizeof(SkColor));
640589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    buffer += fColorCount;
641589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    if (fColorCount > 2) {
642589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        for (int i = 1; i < fColorCount; i++) {
643589708bf7c706348b763e8277004cb160b202bdbrileya@google.com            *buffer++ = fRecs[i].fPos;
644589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        }
645589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    }
6463d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com    *buffer++ = fGradFlags;
647589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    SkASSERT(buffer - storage.get() == count);
648589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
649589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    ///////////////////////////////////
650589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
651589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    SK_DECLARE_STATIC_MUTEX(gMutex);
652589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    static SkBitmapCache* gCache;
653589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    // each cache cost 1K of RAM, since each bitmap will be 1x256 at 32bpp
654589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    static const int MAX_NUM_CACHED_GRADIENT_BITMAPS = 32;
655589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    SkAutoMutexAcquire ama(gMutex);
656589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
657589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    if (NULL == gCache) {
658589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        gCache = SkNEW_ARGS(SkBitmapCache, (MAX_NUM_CACHED_GRADIENT_BITMAPS));
659589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    }
660589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    size_t size = count * sizeof(int32_t);
661589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
662589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    if (!gCache->find(storage.get(), size, bitmap)) {
663589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        // force our cahce32pixelref to be built
66453783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org        (void)this->getCache32();
665e24ad23ae67ffcb0dc545b7e426cf08d102e0868commit-bot@chromium.org        bitmap->setConfig(SkImageInfo::MakeN32Premul(kCache32Count, 1));
66653783b026a00683c1fb504127c3398dabb61ea73commit-bot@chromium.org        bitmap->setPixelRef(fCache32PixelRef);
667589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
668589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        gCache->add(storage.get(), size, *bitmap);
669589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    }
670589708bf7c706348b763e8277004cb160b202bdbrileya@google.com}
671589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
67244d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.orgvoid SkGradientShaderBase::commonAsAGradient(GradientInfo* info, bool flipGrad) const {
673589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    if (info) {
674589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        if (info->fColorCount >= fColorCount) {
67544d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org            SkColor* colorLoc;
67644d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org            Rec*     recLoc;
67744d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org            if (flipGrad && (info->fColors || info->fColorOffsets)) {
67844d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org                SkAutoSTArray<8, SkColor> colorStorage(fColorCount);
67944d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org                SkAutoSTArray<8, Rec> recStorage(fColorCount);
68044d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org                colorLoc = colorStorage.get();
68144d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org                recLoc = recStorage.get();
68244d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org                FlipGradientColors(colorLoc, recLoc, fOrigColors, fRecs, fColorCount);
68344d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org            } else {
68444d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org                colorLoc = fOrigColors;
68544d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org                recLoc = fRecs;
68644d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org            }
687589708bf7c706348b763e8277004cb160b202bdbrileya@google.com            if (info->fColors) {
68844d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org                memcpy(info->fColors, colorLoc, fColorCount * sizeof(SkColor));
689589708bf7c706348b763e8277004cb160b202bdbrileya@google.com            }
690589708bf7c706348b763e8277004cb160b202bdbrileya@google.com            if (info->fColorOffsets) {
691589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                if (fColorCount == 2) {
692589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                    info->fColorOffsets[0] = 0;
693589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                    info->fColorOffsets[1] = SK_Scalar1;
694589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                } else if (fColorCount > 2) {
69576f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com                    for (int i = 0; i < fColorCount; ++i) {
69644d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org                        info->fColorOffsets[i] = SkFixedToScalar(recLoc[i].fPos);
69776f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com                    }
698589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                }
699589708bf7c706348b763e8277004cb160b202bdbrileya@google.com            }
700589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        }
701589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        info->fColorCount = fColorCount;
702589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        info->fTileMode = fTileMode;
7033d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com        info->fGradientFlags = fGradFlags;
704589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    }
705589708bf7c706348b763e8277004cb160b202bdbrileya@google.com}
706589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
7070f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org#ifndef SK_IGNORE_TO_STRING
70876f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.comvoid SkGradientShaderBase::toString(SkString* str) const {
70976f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com
71076f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com    str->appendf("%d colors: ", fColorCount);
71176f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com
71276f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com    for (int i = 0; i < fColorCount; ++i) {
71376f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com        str->appendHex(fOrigColors[i]);
71476f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com        if (i < fColorCount-1) {
71576f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com            str->append(", ");
71676f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com        }
71776f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com    }
71876f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com
71976f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com    if (fColorCount > 2) {
72076f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com        str->append(" points: (");
72176f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com        for (int i = 0; i < fColorCount; ++i) {
72276f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com            str->appendScalar(SkFixedToScalar(fRecs[i].fPos));
72376f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com            if (i < fColorCount-1) {
72476f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com                str->append(", ");
72576f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com            }
72676f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com        }
72776f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com        str->append(")");
72876f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com    }
72976f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com
73076f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com    static const char* gTileModeName[SkShader::kTileModeCount] = {
73176f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com        "clamp", "repeat", "mirror"
73276f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com    };
73376f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com
73476f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com    str->append(" ");
73576f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com    str->append(gTileModeName[fTileMode]);
73676f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com
73776f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com    // TODO: add "fMapper->toString(str);" when SkUnitMapper::toString is added
73876f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com
73976f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com    this->INHERITED::toString(str);
74076f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com}
74176f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com#endif
74276f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com
743589708bf7c706348b763e8277004cb160b202bdbrileya@google.com///////////////////////////////////////////////////////////////////////////////
744589708bf7c706348b763e8277004cb160b202bdbrileya@google.com///////////////////////////////////////////////////////////////////////////////
745589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
746589708bf7c706348b763e8277004cb160b202bdbrileya@google.com#include "SkEmptyShader.h"
747589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
748589708bf7c706348b763e8277004cb160b202bdbrileya@google.com// assumes colors is SkColor* and pos is SkScalar*
749589708bf7c706348b763e8277004cb160b202bdbrileya@google.com#define EXPAND_1_COLOR(count)               \
750589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    SkColor tmp[2];                         \
751589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    do {                                    \
752589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        if (1 == count) {                   \
753589708bf7c706348b763e8277004cb160b202bdbrileya@google.com            tmp[0] = tmp[1] = colors[0];    \
754589708bf7c706348b763e8277004cb160b202bdbrileya@google.com            colors = tmp;                   \
755589708bf7c706348b763e8277004cb160b202bdbrileya@google.com            pos = NULL;                     \
756589708bf7c706348b763e8277004cb160b202bdbrileya@google.com            count = 2;                      \
757589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        }                                   \
758589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    } while (0)
759589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
760437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.comstatic void desc_init(SkGradientShaderBase::Descriptor* desc,
761437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.com                      const SkColor colors[],
762437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.com                      const SkScalar pos[], int colorCount,
763437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.com                      SkShader::TileMode mode,
7643d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com                      SkUnitMapper* mapper, uint32_t flags) {
765437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.com    desc->fColors   = colors;
766437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.com    desc->fPos      = pos;
767437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.com    desc->fCount    = colorCount;
768437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.com    desc->fTileMode = mode;
769437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.com    desc->fMapper   = mapper;
7703d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com    desc->fFlags    = flags;
771437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.com}
772437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.com
773589708bf7c706348b763e8277004cb160b202bdbrileya@google.comSkShader* SkGradientShader::CreateLinear(const SkPoint pts[2],
774589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                                         const SkColor colors[],
775589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                                         const SkScalar pos[], int colorCount,
776589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                                         SkShader::TileMode mode,
7773d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com                                         SkUnitMapper* mapper,
7783d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com                                         uint32_t flags) {
779589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    if (NULL == pts || NULL == colors || colorCount < 1) {
780589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        return NULL;
781589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    }
782589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    EXPAND_1_COLOR(colorCount);
783589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
784437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.com    SkGradientShaderBase::Descriptor desc;
7853d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com    desc_init(&desc, colors, pos, colorCount, mode, mapper, flags);
786437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.com    return SkNEW_ARGS(SkLinearGradient, (pts, desc));
787589708bf7c706348b763e8277004cb160b202bdbrileya@google.com}
788589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
789589708bf7c706348b763e8277004cb160b202bdbrileya@google.comSkShader* SkGradientShader::CreateRadial(const SkPoint& center, SkScalar radius,
790589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                                         const SkColor colors[],
791589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                                         const SkScalar pos[], int colorCount,
792589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                                         SkShader::TileMode mode,
7933d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com                                         SkUnitMapper* mapper,
7943d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com                                         uint32_t flags) {
795589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    if (radius <= 0 || NULL == colors || colorCount < 1) {
796589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        return NULL;
797589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    }
798589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    EXPAND_1_COLOR(colorCount);
799589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
800437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.com    SkGradientShaderBase::Descriptor desc;
8013d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com    desc_init(&desc, colors, pos, colorCount, mode, mapper, flags);
802437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.com    return SkNEW_ARGS(SkRadialGradient, (center, radius, desc));
803589708bf7c706348b763e8277004cb160b202bdbrileya@google.com}
804589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
805589708bf7c706348b763e8277004cb160b202bdbrileya@google.comSkShader* SkGradientShader::CreateTwoPointRadial(const SkPoint& start,
806589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                                                 SkScalar startRadius,
807589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                                                 const SkPoint& end,
808589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                                                 SkScalar endRadius,
809589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                                                 const SkColor colors[],
810589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                                                 const SkScalar pos[],
811589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                                                 int colorCount,
812589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                                                 SkShader::TileMode mode,
8133d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com                                                 SkUnitMapper* mapper,
8143d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com                                                 uint32_t flags) {
815589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    if (startRadius < 0 || endRadius < 0 || NULL == colors || colorCount < 1) {
816589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        return NULL;
817589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    }
818589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    EXPAND_1_COLOR(colorCount);
819fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
820437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.com    SkGradientShaderBase::Descriptor desc;
8213d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com    desc_init(&desc, colors, pos, colorCount, mode, mapper, flags);
822589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    return SkNEW_ARGS(SkTwoPointRadialGradient,
823437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.com                      (start, startRadius, end, endRadius, desc));
824589708bf7c706348b763e8277004cb160b202bdbrileya@google.com}
825589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
826589708bf7c706348b763e8277004cb160b202bdbrileya@google.comSkShader* SkGradientShader::CreateTwoPointConical(const SkPoint& start,
8273d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com                                                  SkScalar startRadius,
8283d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com                                                  const SkPoint& end,
8293d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com                                                  SkScalar endRadius,
8303d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com                                                  const SkColor colors[],
8313d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com                                                  const SkScalar pos[],
8323d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com                                                  int colorCount,
8333d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com                                                  SkShader::TileMode mode,
8343d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com                                                  SkUnitMapper* mapper,
8353d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com                                                  uint32_t flags) {
836589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    if (startRadius < 0 || endRadius < 0 || NULL == colors || colorCount < 1) {
837589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        return NULL;
838589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    }
839589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    if (start == end && startRadius == endRadius) {
840589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        return SkNEW(SkEmptyShader);
841589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    }
84244d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org
8431ee7c6a18a511cde879941324f173ad4c8c123f8rileya@google.com    EXPAND_1_COLOR(colorCount);
844589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
84544d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org    bool flipGradient = startRadius > endRadius;
84644d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org
847437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.com    SkGradientShaderBase::Descriptor desc;
84844d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org
84944d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org    if (!flipGradient) {
85044d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org        desc_init(&desc, colors, pos, colorCount, mode, mapper, flags);
85144d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org        return SkNEW_ARGS(SkTwoPointConicalGradient,
85244d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org                          (start, startRadius, end, endRadius, flipGradient, desc));
85344d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org    } else {
85444d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org        SkAutoSTArray<8, SkColor> colorsNew(colorCount);
85544d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org        SkAutoSTArray<8, SkScalar> posNew(colorCount);
85644d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org        for (int i = 0; i < colorCount; ++i) {
85744d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org            colorsNew[i] = colors[colorCount - i - 1];
85844d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org        }
85944d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org
86044d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org        if (pos) {
86144d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org            for (int i = 0; i < colorCount; ++i) {
86244d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org                posNew[i] = 1 - pos[colorCount - i - 1];
86344d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org            }
86444d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org            desc_init(&desc, colorsNew.get(), posNew.get(), colorCount, mode, mapper, flags);
86544d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org        } else {
86644d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org            desc_init(&desc, colorsNew.get(), NULL, colorCount, mode, mapper, flags);
86744d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org        }
86844d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org
86944d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org        return SkNEW_ARGS(SkTwoPointConicalGradient,
87044d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org                          (end, endRadius, start, startRadius, flipGradient, desc));
87144d83c1e81b0555efa94f78e2a53b862208cdd06commit-bot@chromium.org    }
872589708bf7c706348b763e8277004cb160b202bdbrileya@google.com}
873589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
874589708bf7c706348b763e8277004cb160b202bdbrileya@google.comSkShader* SkGradientShader::CreateSweep(SkScalar cx, SkScalar cy,
875589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                                        const SkColor colors[],
876589708bf7c706348b763e8277004cb160b202bdbrileya@google.com                                        const SkScalar pos[],
8773d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com                                        int colorCount, SkUnitMapper* mapper,
8783d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com                                        uint32_t flags) {
879437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.com    if (NULL == colors || colorCount < 1) {
880589708bf7c706348b763e8277004cb160b202bdbrileya@google.com        return NULL;
881589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    }
882437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.com    EXPAND_1_COLOR(colorCount);
883589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
884437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.com    SkGradientShaderBase::Descriptor desc;
8853d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com    desc_init(&desc, colors, pos, colorCount, SkShader::kClamp_TileMode, mapper, flags);
886437d6eb4e0d91710fdeb7ecedb694f658458ae00reed@google.com    return SkNEW_ARGS(SkSweepGradient, (cx, cy, desc));
887589708bf7c706348b763e8277004cb160b202bdbrileya@google.com}
888589708bf7c706348b763e8277004cb160b202bdbrileya@google.com
889589708bf7c706348b763e8277004cb160b202bdbrileya@google.comSK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkGradientShader)
890589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLinearGradient)
891589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkRadialGradient)
892589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSweepGradient)
893589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkTwoPointRadialGradient)
894589708bf7c706348b763e8277004cb160b202bdbrileya@google.com    SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkTwoPointConicalGradient)
895589708bf7c706348b763e8277004cb160b202bdbrileya@google.comSK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END
896d7cc651b8da11d52ae90e910b948f5e2d15daaf9rileya@google.com
897d7cc651b8da11d52ae90e910b948f5e2d15daaf9rileya@google.com///////////////////////////////////////////////////////////////////////////////
898d7cc651b8da11d52ae90e910b948f5e2d15daaf9rileya@google.com
899cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#if SK_SUPPORT_GPU
900cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com
901b3e50f23c57d058820701219e66b6bfb8e0681a4rileya@google.com#include "effects/GrTextureStripAtlas.h"
902c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com#include "GrTBackendEffectFactory.h"
903cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#include "SkGr.h"
904cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com
9050707c29413a5a3cc1c2d14b8c65b3692af5c7411bsalomon@google.comGrGLGradientEffect::GrGLGradientEffect(const GrBackendEffectFactory& factory)
906b3e50f23c57d058820701219e66b6bfb8e0681a4rileya@google.com    : INHERITED(factory)
90777af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com    , fCachedYCoord(SK_ScalarMax) {
908d8b5faca043100d7a1e4594b4d10e462532af390bsalomon@google.com}
909d7cc651b8da11d52ae90e910b948f5e2d15daaf9rileya@google.com
9100707c29413a5a3cc1c2d14b8c65b3692af5c7411bsalomon@google.comGrGLGradientEffect::~GrGLGradientEffect() { }
911d7cc651b8da11d52ae90e910b948f5e2d15daaf9rileya@google.com
91282d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.comvoid GrGLGradientEffect::emitUniforms(GrGLShaderBuilder* builder, EffectKey key) {
91382d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com
914996402bddb4d886c38dee17341e5833500901933commit-bot@chromium.org    if (SkGradientShaderBase::kTwo_GpuColorType == ColorTypeFromKey(key)) { // 2 Color case
91582d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        fColorStartUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
91682d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com                                             kVec4f_GrSLType, "GradientStartColor");
91782d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        fColorEndUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
91882d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com                                           kVec4f_GrSLType, "GradientEndColor");
9199a070f24f1450091dfe25cacec8bd375ec7a0e50skia.committer@gmail.com
920996402bddb4d886c38dee17341e5833500901933commit-bot@chromium.org    } else if (SkGradientShaderBase::kThree_GpuColorType == ColorTypeFromKey(key)){ // 3 Color Case
92182d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        fColorStartUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
92282d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com                                             kVec4f_GrSLType, "GradientStartColor");
92382d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        fColorMidUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
92482d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com                                           kVec4f_GrSLType, "GradientMidColor");
92582d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        fColorEndUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
92682d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com                                             kVec4f_GrSLType, "GradientEndColor");
92782d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com
92882d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com    } else { // if not a fast case
92982d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        fFSYUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
93082d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com                                      kFloat_GrSLType, "GradientYCoordFS");
93182d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com    }
93282d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com}
93382d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com
93482d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.comstatic inline void set_color_uni(const GrGLUniformManager& uman,
93582d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com                                 const GrGLUniformManager::UniformHandle uni,
93682d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com                                 const SkColor* color) {
93782d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com       uman.set4f(uni,
93882d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com                  SkColorGetR(*color) / 255.f,
93982d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com                  SkColorGetG(*color) / 255.f,
94082d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com                  SkColorGetB(*color) / 255.f,
94182d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com                  SkColorGetA(*color) / 255.f);
94282d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com}
94382d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com
94482d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.comstatic inline void set_mul_color_uni(const GrGLUniformManager& uman,
94582d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com                                     const GrGLUniformManager::UniformHandle uni,
94682d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com                                     const SkColor* color){
94782d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com       float a = SkColorGetA(*color) / 255.f;
94882d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com       float aDiv255 = a / 255.f;
94982d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com       uman.set4f(uni,
95082d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com                  SkColorGetR(*color) * aDiv255,
95182d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com                  SkColorGetG(*color) * aDiv255,
95282d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com                  SkColorGetB(*color) * aDiv255,
95382d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com                  a);
954b3e50f23c57d058820701219e66b6bfb8e0681a4rileya@google.com}
955b3e50f23c57d058820701219e66b6bfb8e0681a4rileya@google.com
956c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.comvoid GrGLGradientEffect::setData(const GrGLUniformManager& uman,
957c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com                                 const GrDrawEffect& drawEffect) {
95882d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com
959c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com    const GrGradientEffect& e = drawEffect.castEffect<GrGradientEffect>();
960d8b5faca043100d7a1e4594b4d10e462532af390bsalomon@google.com
96182d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com
962996402bddb4d886c38dee17341e5833500901933commit-bot@chromium.org    if (SkGradientShaderBase::kTwo_GpuColorType == e.getColorType()){
96382d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com
96482d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        if (GrGradientEffect::kBeforeInterp_PremulType == e.getPremulType()) {
96582d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com            set_mul_color_uni(uman, fColorStartUni, e.getColors(0));
96682d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com            set_mul_color_uni(uman, fColorEndUni,   e.getColors(1));
96782d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        } else {
96882d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com            set_color_uni(uman, fColorStartUni, e.getColors(0));
96982d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com            set_color_uni(uman, fColorEndUni,   e.getColors(1));
97082d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        }
97182d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com
972996402bddb4d886c38dee17341e5833500901933commit-bot@chromium.org    } else if (SkGradientShaderBase::kThree_GpuColorType == e.getColorType()){
97382d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com
97482d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        if (GrGradientEffect::kBeforeInterp_PremulType == e.getPremulType()) {
97582d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com            set_mul_color_uni(uman, fColorStartUni, e.getColors(0));
97682d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com            set_mul_color_uni(uman, fColorMidUni,   e.getColors(1));
97782d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com            set_mul_color_uni(uman, fColorEndUni,   e.getColors(2));
97882d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        } else {
97982d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com            set_color_uni(uman, fColorStartUni, e.getColors(0));
98082d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com            set_color_uni(uman, fColorMidUni,   e.getColors(1));
98182d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com            set_color_uni(uman, fColorEndUni,   e.getColors(2));
98282d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        }
98382d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com    } else {
9849a070f24f1450091dfe25cacec8bd375ec7a0e50skia.committer@gmail.com
98582d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        SkScalar yCoord = e.getYCoord();
98682d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        if (yCoord != fCachedYCoord) {
98782d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com            uman.set1f(fFSYUni, yCoord);
98882d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com            fCachedYCoord = yCoord;
98982d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        }
990b3e50f23c57d058820701219e66b6bfb8e0681a4rileya@google.com    }
991b3e50f23c57d058820701219e66b6bfb8e0681a4rileya@google.com}
992b3e50f23c57d058820701219e66b6bfb8e0681a4rileya@google.com
99382d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com
99482d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.comGrGLEffect::EffectKey GrGLGradientEffect::GenBaseGradientKey(const GrDrawEffect& drawEffect) {
995c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com    const GrGradientEffect& e = drawEffect.castEffect<GrGradientEffect>();
9969a070f24f1450091dfe25cacec8bd375ec7a0e50skia.committer@gmail.com
99777af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com    EffectKey key = 0;
99882d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com
999996402bddb4d886c38dee17341e5833500901933commit-bot@chromium.org    if (SkGradientShaderBase::kTwo_GpuColorType == e.getColorType()) {
100082d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        key |= kTwoColorKey;
1001996402bddb4d886c38dee17341e5833500901933commit-bot@chromium.org    } else if (SkGradientShaderBase::kThree_GpuColorType == e.getColorType()){
100282d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        key |= kThreeColorKey;
100382d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com    }
100482d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com
100582d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com    if (GrGradientEffect::kBeforeInterp_PremulType == e.getPremulType()) {
100682d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        key |= kPremulBeforeInterpKey;
100782d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com    }
100882d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com
100982d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com    return key;
1010d8b5faca043100d7a1e4594b4d10e462532af390bsalomon@google.com}
1011d8b5faca043100d7a1e4594b4d10e462532af390bsalomon@google.com
101282d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.comvoid GrGLGradientEffect::emitColor(GrGLShaderBuilder* builder,
101382d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com                                   const char* gradientTValue,
101482d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com                                   EffectKey key,
101582d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com                                   const char* outputColor,
101682d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com                                   const char* inputColor,
10173390b9ac9ad69a6e772c2b957d75d19611239025commit-bot@chromium.org                                   const TextureSamplerArray& samplers) {
1018996402bddb4d886c38dee17341e5833500901933commit-bot@chromium.org    if (SkGradientShaderBase::kTwo_GpuColorType == ColorTypeFromKey(key)){
101982d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        builder->fsCodeAppendf("\tvec4 colorTemp = mix(%s, %s, clamp(%s, 0.0, 1.0));\n",
102082d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com                               builder->getUniformVariable(fColorStartUni).c_str(),
102182d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com                               builder->getUniformVariable(fColorEndUni).c_str(),
102282d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com                               gradientTValue);
102382d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        // Note that we could skip this step if both colors are known to be opaque. Two
102482d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        // considerations:
102582d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        // The gradient SkShader reporting opaque is more restrictive than necessary in the two pt
102682d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        // case. Make sure the key reflects this optimization (and note that it can use the same
102782d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        // shader as thekBeforeIterp case). This same optimization applies to the 3 color case below.
102882d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        if (GrGradientEffect::kAfterInterp_PremulType == PremulTypeFromKey(key)) {
102982d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com            builder->fsCodeAppend("\tcolorTemp.rgb *= colorTemp.a;\n");
103082d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        }
103182d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com
1032824c346b6e0e114063c1a8ad4ba7c3a669ee2cffcommit-bot@chromium.org        builder->fsCodeAppendf("\t%s = %s;\n", outputColor,
1033a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org                               (GrGLSLExpr4(inputColor) * GrGLSLExpr4("colorTemp")).c_str());
1034996402bddb4d886c38dee17341e5833500901933commit-bot@chromium.org    } else if (SkGradientShaderBase::kThree_GpuColorType == ColorTypeFromKey(key)){
103582d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        builder->fsCodeAppendf("\tfloat oneMinus2t = 1.0 - (2.0 * (%s));\n",
103682d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com                               gradientTValue);
10370694ea7f6a16e946972b9c625f434e80eb42bb5fcommit-bot@chromium.org        builder->fsCodeAppendf("\tvec4 colorTemp = clamp(oneMinus2t, 0.0, 1.0) * %s;\n",
10380694ea7f6a16e946972b9c625f434e80eb42bb5fcommit-bot@chromium.org                               builder->getUniformVariable(fColorStartUni).c_str());
10390694ea7f6a16e946972b9c625f434e80eb42bb5fcommit-bot@chromium.org        if (kTegra3_GrGLRenderer == builder->ctxInfo().renderer()) {
10400694ea7f6a16e946972b9c625f434e80eb42bb5fcommit-bot@chromium.org            // The Tegra3 compiler will sometimes never return if we have
10410694ea7f6a16e946972b9c625f434e80eb42bb5fcommit-bot@chromium.org            // min(abs(oneMinus2t), 1.0), or do the abs first in a separate expression.
10420694ea7f6a16e946972b9c625f434e80eb42bb5fcommit-bot@chromium.org            builder->fsCodeAppend("\tfloat minAbs = abs(oneMinus2t);\n");
10430694ea7f6a16e946972b9c625f434e80eb42bb5fcommit-bot@chromium.org            builder->fsCodeAppend("\tminAbs = minAbs > 1.0 ? 1.0 : minAbs;\n");
10440694ea7f6a16e946972b9c625f434e80eb42bb5fcommit-bot@chromium.org            builder->fsCodeAppendf("\tcolorTemp += (1.0 - minAbs) * %s;\n",
10450694ea7f6a16e946972b9c625f434e80eb42bb5fcommit-bot@chromium.org                                   builder->getUniformVariable(fColorMidUni).c_str());
10460694ea7f6a16e946972b9c625f434e80eb42bb5fcommit-bot@chromium.org        } else {
10470694ea7f6a16e946972b9c625f434e80eb42bb5fcommit-bot@chromium.org            builder->fsCodeAppendf("\tcolorTemp += (1.0 - min(abs(oneMinus2t), 1.0)) * %s;\n",
10480694ea7f6a16e946972b9c625f434e80eb42bb5fcommit-bot@chromium.org                                   builder->getUniformVariable(fColorMidUni).c_str());
10490694ea7f6a16e946972b9c625f434e80eb42bb5fcommit-bot@chromium.org        }
10500694ea7f6a16e946972b9c625f434e80eb42bb5fcommit-bot@chromium.org        builder->fsCodeAppendf("\tcolorTemp += clamp(-oneMinus2t, 0.0, 1.0) * %s;\n",
105182d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com                               builder->getUniformVariable(fColorEndUni).c_str());
105282d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        if (GrGradientEffect::kAfterInterp_PremulType == PremulTypeFromKey(key)) {
105382d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com            builder->fsCodeAppend("\tcolorTemp.rgb *= colorTemp.a;\n");
105482d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        }
105534bcb9f80336fe0dc56ad5f67aeb0859bf84d92ebsalomon@google.com
1056824c346b6e0e114063c1a8ad4ba7c3a669ee2cffcommit-bot@chromium.org        builder->fsCodeAppendf("\t%s = %s;\n", outputColor,
1057a34995e18b1f0a7d8c9f23451718bb30ff0105b0commit-bot@chromium.org                               (GrGLSLExpr4(inputColor) * GrGLSLExpr4("colorTemp")).c_str());
105882d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com    } else {
105982d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        builder->fsCodeAppendf("\tvec2 coord = vec2(%s, %s);\n",
106082d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com                               gradientTValue,
106182d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com                               builder->getUniformVariable(fFSYUni).c_str());
106282d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        builder->fsCodeAppendf("\t%s = ", outputColor);
106382d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        builder->fsAppendTextureLookupAndModulate(inputColor,
106482d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com                                                  samplers[0],
106582d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com                                                  "coord");
106682d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        builder->fsCodeAppend(";\n");
106782d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com    }
1068d7cc651b8da11d52ae90e910b948f5e2d15daaf9rileya@google.com}
1069d7cc651b8da11d52ae90e910b948f5e2d15daaf9rileya@google.com
1070d7cc651b8da11d52ae90e910b948f5e2d15daaf9rileya@google.com/////////////////////////////////////////////////////////////////////
1071d7cc651b8da11d52ae90e910b948f5e2d15daaf9rileya@google.com
1072fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.comGrGradientEffect::GrGradientEffect(GrContext* ctx,
10731c6d64b78b24083ee9fd7411dac8a4a7e2d03a3crileya@google.com                                   const SkGradientShaderBase& shader,
1074d8b5faca043100d7a1e4594b4d10e462532af390bsalomon@google.com                                   const SkMatrix& matrix,
107550db75c871b203081a32190ab173f13c785a147fbsalomon@google.com                                   SkShader::TileMode tileMode) {
1076d7cc651b8da11d52ae90e910b948f5e2d15daaf9rileya@google.com
1077371e105da5d9fdfff3b4242b37ff6fc09214c8c8bsalomon@google.com    fIsOpaque = shader.isOpaque();
1078371e105da5d9fdfff3b4242b37ff6fc09214c8c8bsalomon@google.com
1079996402bddb4d886c38dee17341e5833500901933commit-bot@chromium.org    fColorType = shader.getGpuColorType(&fColors[0]);
108082d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com
108182d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com    // The two and three color specializations do not currently support tiling.
1082996402bddb4d886c38dee17341e5833500901933commit-bot@chromium.org    if (SkGradientShaderBase::kTwo_GpuColorType == fColorType ||
1083996402bddb4d886c38dee17341e5833500901933commit-bot@chromium.org        SkGradientShaderBase::kThree_GpuColorType == fColorType) {
1084996402bddb4d886c38dee17341e5833500901933commit-bot@chromium.org        fRow = -1;
10858a777a5e6ad081f54ec169dcf714343c38517161skia.committer@gmail.com
1086996402bddb4d886c38dee17341e5833500901933commit-bot@chromium.org        if (SkGradientShader::kInterpolateColorsInPremul_Flag & shader.getFlags()) {
108782d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com            fPremulType = kBeforeInterp_PremulType;
108882d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        } else {
108982d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com            fPremulType = kAfterInterp_PremulType;
109082d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        }
109177af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com        fCoordTransform.reset(kCoordSet, matrix);
1092b3e50f23c57d058820701219e66b6bfb8e0681a4rileya@google.com    } else {
109382d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        // doesn't matter how this is set, just be consistent because it is part of the effect key.
109482d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        fPremulType = kBeforeInterp_PremulType;
109582d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        SkBitmap bitmap;
109682d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        shader.getGradientTableBitmap(&bitmap);
10979a070f24f1450091dfe25cacec8bd375ec7a0e50skia.committer@gmail.com
109882d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        GrTextureStripAtlas::Desc desc;
109982d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        desc.fWidth  = bitmap.width();
110082d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        desc.fHeight = 32;
110182d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        desc.fRowHeight = bitmap.height();
110282d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        desc.fContext = ctx;
1103e24ad23ae67ffcb0dc545b7e426cf08d102e0868commit-bot@chromium.org        desc.fConfig = SkImageInfo2GrPixelConfig(bitmap.colorType(), bitmap.alphaType());
110482d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        fAtlas = GrTextureStripAtlas::GetAtlas(desc);
110582d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        SkASSERT(NULL != fAtlas);
110682d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com
110782d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        // We always filter the gradient table. Each table is one row of a texture, always y-clamp.
110882d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        GrTextureParams params;
110982d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        params.setFilterMode(GrTextureParams::kBilerp_FilterMode);
111082d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        params.setTileModeX(tileMode);
111182d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com
111282d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        fRow = fAtlas->lockRow(bitmap);
111382d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        if (-1 != fRow) {
111482d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com            fYCoord = fAtlas->getYOffset(fRow) + SK_ScalarHalf *
111582d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com            fAtlas->getVerticalScaleFactor();
111677af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com            fCoordTransform.reset(kCoordSet, matrix, fAtlas->getTexture());
111782d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com            fTextureAccess.reset(fAtlas->getTexture(), params);
111882d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        } else {
111982d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com            GrTexture* texture = GrLockAndRefCachedBitmapTexture(ctx, bitmap, &params);
112077af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com            fCoordTransform.reset(kCoordSet, matrix, texture);
112182d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com            fTextureAccess.reset(texture, params);
112282d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com            fYCoord = SK_ScalarHalf;
112382d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com
112482d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com            // Unlock immediately, this is not great, but we don't have a way of
112582d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com            // knowing when else to unlock it currently, so it may get purged from
112682d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com            // the cache, but it'll still be ref'd until it's no longer being used.
112782d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com            GrUnlockAndUnrefCachedBitmapTexture(texture);
112882d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        }
112982d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        this->addTextureAccess(&fTextureAccess);
1130b3e50f23c57d058820701219e66b6bfb8e0681a4rileya@google.com    }
113177af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com    this->addCoordTransform(&fCoordTransform);
1132d7cc651b8da11d52ae90e910b948f5e2d15daaf9rileya@google.com}
1133d7cc651b8da11d52ae90e910b948f5e2d15daaf9rileya@google.com
1134d7cc651b8da11d52ae90e910b948f5e2d15daaf9rileya@google.comGrGradientEffect::~GrGradientEffect() {
1135b3e50f23c57d058820701219e66b6bfb8e0681a4rileya@google.com    if (this->useAtlas()) {
1136b3e50f23c57d058820701219e66b6bfb8e0681a4rileya@google.com        fAtlas->unlockRow(fRow);
1137b3e50f23c57d058820701219e66b6bfb8e0681a4rileya@google.com    }
1138d7cc651b8da11d52ae90e910b948f5e2d15daaf9rileya@google.com}
1139d7cc651b8da11d52ae90e910b948f5e2d15daaf9rileya@google.com
11408a252f79629b189a03de22cd8ff0312c5bccedd1bsalomon@google.combool GrGradientEffect::onIsEqual(const GrEffect& effect) const {
11416340a41108633ac1ce5941e5cd30538630c4c55bbsalomon@google.com    const GrGradientEffect& s = CastEffect<GrGradientEffect>(effect);
114282d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com
114382d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com    if (this->fColorType == s.getColorType()){
114482d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com
1145996402bddb4d886c38dee17341e5833500901933commit-bot@chromium.org        if (SkGradientShaderBase::kTwo_GpuColorType == fColorType) {
114682d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com            if (*this->getColors(0) != *s.getColors(0) ||
114782d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com                *this->getColors(1) != *s.getColors(1)) {
114882d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com                return false;
114982d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com            }
1150996402bddb4d886c38dee17341e5833500901933commit-bot@chromium.org        } else if (SkGradientShaderBase::kThree_GpuColorType == fColorType) {
115182d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com            if (*this->getColors(0) != *s.getColors(0) ||
115282d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com                *this->getColors(1) != *s.getColors(1) ||
115382d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com                *this->getColors(2) != *s.getColors(2)) {
115482d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com                return false;
115582d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com            }
1156c0de5d6b5fe312ec39a544fcefdb7673082a07b8robertphillips@google.com        } else {
1157c0de5d6b5fe312ec39a544fcefdb7673082a07b8robertphillips@google.com            if (fYCoord != s.getYCoord()) {
1158c0de5d6b5fe312ec39a544fcefdb7673082a07b8robertphillips@google.com                return false;
1159c0de5d6b5fe312ec39a544fcefdb7673082a07b8robertphillips@google.com            }
116082d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        }
11619a070f24f1450091dfe25cacec8bd375ec7a0e50skia.committer@gmail.com
116282d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com        return fTextureAccess.getTexture() == s.fTextureAccess.getTexture()  &&
116382d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com            fTextureAccess.getParams().getTileModeX() ==
116468b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com                s.fTextureAccess.getParams().getTileModeX() &&
116582d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com            this->useAtlas() == s.useAtlas() &&
116677af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com            fCoordTransform.getMatrix().cheapEqualTo(s.fCoordTransform.getMatrix());
116782d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com    }
116882d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com
116982d1223aece4703bc9f3a3612cbabaa8c2f2809bbsalomon@google.com    return false;
117068b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com}
117168b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com
117268b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.comvoid GrGradientEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
1173b8eb2e89edf914caf5479baeffcb670d3e93f496bsalomon@google.com    if (fIsOpaque && (kA_GrColorComponentFlag & *validFlags) && 0xff == GrColorUnpackA(*color)) {
1174b8eb2e89edf914caf5479baeffcb670d3e93f496bsalomon@google.com        *validFlags = kA_GrColorComponentFlag;
117568b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com    } else {
117668b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com        *validFlags = 0;
117768b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com    }
117868b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com}
117968b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com
1180e0e7cfe44bb9d66d76120a79e5275c294bacaa22commit-bot@chromium.orgint GrGradientEffect::RandomGradientParams(SkRandom* random,
1181d472620458e2383e6dd949f4e1aaf61160717ffebsalomon@google.com                                           SkColor colors[],
1182d472620458e2383e6dd949f4e1aaf61160717ffebsalomon@google.com                                           SkScalar** stops,
1183d472620458e2383e6dd949f4e1aaf61160717ffebsalomon@google.com                                           SkShader::TileMode* tm) {
1184d472620458e2383e6dd949f4e1aaf61160717ffebsalomon@google.com    int outColors = random->nextRangeU(1, kMaxRandomGradientColors);
1185d472620458e2383e6dd949f4e1aaf61160717ffebsalomon@google.com
1186d472620458e2383e6dd949f4e1aaf61160717ffebsalomon@google.com    // if one color, omit stops, otherwise randomly decide whether or not to
1187d472620458e2383e6dd949f4e1aaf61160717ffebsalomon@google.com    if (outColors == 1 || (outColors >= 2 && random->nextBool())) {
1188d472620458e2383e6dd949f4e1aaf61160717ffebsalomon@google.com        *stops = NULL;
1189d472620458e2383e6dd949f4e1aaf61160717ffebsalomon@google.com    }
1190d472620458e2383e6dd949f4e1aaf61160717ffebsalomon@google.com
119181712883419f76e25d2ffec38a9438284a45a48dbsalomon@google.com    SkScalar stop = 0.f;
1192d472620458e2383e6dd949f4e1aaf61160717ffebsalomon@google.com    for (int i = 0; i < outColors; ++i) {
1193d472620458e2383e6dd949f4e1aaf61160717ffebsalomon@google.com        colors[i] = random->nextU();
1194d472620458e2383e6dd949f4e1aaf61160717ffebsalomon@google.com        if (NULL != *stops) {
1195d472620458e2383e6dd949f4e1aaf61160717ffebsalomon@google.com            (*stops)[i] = stop;
1196d472620458e2383e6dd949f4e1aaf61160717ffebsalomon@google.com            stop = i < outColors - 1 ? stop + random->nextUScalar1() * (1.f - stop) : 1.f;
1197d472620458e2383e6dd949f4e1aaf61160717ffebsalomon@google.com        }
1198d472620458e2383e6dd949f4e1aaf61160717ffebsalomon@google.com    }
1199d472620458e2383e6dd949f4e1aaf61160717ffebsalomon@google.com    *tm = static_cast<SkShader::TileMode>(random->nextULessThan(SkShader::kTileModeCount));
1200d472620458e2383e6dd949f4e1aaf61160717ffebsalomon@google.com
1201d472620458e2383e6dd949f4e1aaf61160717ffebsalomon@google.com    return outColors;
1202d472620458e2383e6dd949f4e1aaf61160717ffebsalomon@google.com}
1203d472620458e2383e6dd949f4e1aaf61160717ffebsalomon@google.com
1204cf8fb1f6f03fc77f9927564f9ef9abeeeec508d2bsalomon@google.com#endif
1205