1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/*
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2006 The Android Open Source Project
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com */
7ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
8a5572e5bb2a2bbeeb59de0741c2527869d365a0ccommit-bot@chromium.org#include "SkBitmapProcShader.h"
98367b8cb7a6b2f163ee41365323cb219b7049b15reed#include "SkColorShader.h"
10ce56d965069c1649afe14319cb239e6ad670682acommit-bot@chromium.org#include "SkEmptyShader.h"
11a5572e5bb2a2bbeeb59de0741c2527869d365a0ccommit-bot@chromium.org#include "SkReadBuffer.h"
12a5572e5bb2a2bbeeb59de0741c2527869d365a0ccommit-bot@chromium.org#include "SkMallocPixelRef.h"
13a5572e5bb2a2bbeeb59de0741c2527869d365a0ccommit-bot@chromium.org#include "SkPaint.h"
14c5d9bb0f677069f62ec76373b9730e70e7352455commit-bot@chromium.org#include "SkPicture.h"
15c5d9bb0f677069f62ec76373b9730e70e7352455commit-bot@chromium.org#include "SkPictureShader.h"
16d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org#include "SkScalar.h"
178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkShader.h"
188fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org#include "SkThread.h"
198b0e8ac5f582de80356019406e2975079bf0829dcommit-bot@chromium.org#include "SkWriteBuffer.h"
208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
218fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org//#define SK_TRACK_SHADER_LIFETIME
228fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org
238fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org#ifdef SK_TRACK_SHADER_LIFETIME
248fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org    static int32_t gShaderCounter;
258fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org#endif
268fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org
278fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.orgstatic inline void inc_shader_counter() {
288fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org#ifdef SK_TRACK_SHADER_LIFETIME
298fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org    int32_t prev = sk_atomic_inc(&gShaderCounter);
308fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org    SkDebugf("+++ shader counter %d\n", prev + 1);
318fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org#endif
328fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org}
338fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.orgstatic inline void dec_shader_counter() {
348fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org#ifdef SK_TRACK_SHADER_LIFETIME
358fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org    int32_t prev = sk_atomic_dec(&gShaderCounter);
368fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org    SkDebugf("--- shader counter %d\n", prev - 1);
378fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org#endif
388fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org}
398fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org
409c9005a347e9996f357bd79591bd34f74f8bbc66commit-bot@chromium.orgSkShader::SkShader(const SkMatrix* localMatrix) {
418fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org    inc_shader_counter();
429c9005a347e9996f357bd79591bd34f74f8bbc66commit-bot@chromium.org    if (localMatrix) {
439c9005a347e9996f357bd79591bd34f74f8bbc66commit-bot@chromium.org        fLocalMatrix = *localMatrix;
449c9005a347e9996f357bd79591bd34f74f8bbc66commit-bot@chromium.org    } else {
459c9005a347e9996f357bd79591bd34f74f8bbc66commit-bot@chromium.org        fLocalMatrix.reset();
469c9005a347e9996f357bd79591bd34f74f8bbc66commit-bot@chromium.org    }
478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
499fa60daad4d5f54c0dbe3dbcc7608a8f6d721187reed#ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING
508fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.orgSkShader::SkShader(SkReadBuffer& buffer) : INHERITED(buffer) {
518fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org    inc_shader_counter();
528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (buffer.readBool()) {
53f94b3a4cebd4adab09c40ebe23c02a615e10c394bsalomon@google.com        buffer.readMatrix(&fLocalMatrix);
54f94b3a4cebd4adab09c40ebe23c02a615e10c394bsalomon@google.com    } else {
55f94b3a4cebd4adab09c40ebe23c02a615e10c394bsalomon@google.com        fLocalMatrix.reset();
568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
589fa60daad4d5f54c0dbe3dbcc7608a8f6d721187reed#endif
598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkShader::~SkShader() {
618fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org    dec_shader_counter();
628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
648b0e8ac5f582de80356019406e2975079bf0829dcommit-bot@chromium.orgvoid SkShader::flatten(SkWriteBuffer& buffer) const {
658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    this->INHERITED::flatten(buffer);
665970f625e96cdc007c563ae72f343ae0d71719a1commit-bot@chromium.org    bool hasLocalM = !fLocalMatrix.isIdentity();
67f94b3a4cebd4adab09c40ebe23c02a615e10c394bsalomon@google.com    buffer.writeBool(hasLocalM);
68f94b3a4cebd4adab09c40ebe23c02a615e10c394bsalomon@google.com    if (hasLocalM) {
69f94b3a4cebd4adab09c40ebe23c02a615e10c394bsalomon@google.com        buffer.writeMatrix(fLocalMatrix);
708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
73ce56d965069c1649afe14319cb239e6ad670682acommit-bot@chromium.orgbool SkShader::computeTotalInverse(const ContextRec& rec, SkMatrix* totalInverse) const {
745970f625e96cdc007c563ae72f343ae0d71719a1commit-bot@chromium.org    SkMatrix total;
755970f625e96cdc007c563ae72f343ae0d71719a1commit-bot@chromium.org    total.setConcat(*rec.fMatrix, fLocalMatrix);
768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
775970f625e96cdc007c563ae72f343ae0d71719a1commit-bot@chromium.org    const SkMatrix* m = &total;
7880116dcf1e1baf9817ae42d0aca51f7eabaa2880commit-bot@chromium.org    if (rec.fLocalMatrix) {
7980116dcf1e1baf9817ae42d0aca51f7eabaa2880commit-bot@chromium.org        total.setConcat(*m, *rec.fLocalMatrix);
8080116dcf1e1baf9817ae42d0aca51f7eabaa2880commit-bot@chromium.org        m = &total;
8180116dcf1e1baf9817ae42d0aca51f7eabaa2880commit-bot@chromium.org    }
8287fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org    return m->invert(totalInverse);
8387fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org}
8487fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org
858367b8cb7a6b2f163ee41365323cb219b7049b15reedbool SkShader::asLuminanceColor(SkColor* colorPtr) const {
868367b8cb7a6b2f163ee41365323cb219b7049b15reed    SkColor storage;
878367b8cb7a6b2f163ee41365323cb219b7049b15reed    if (NULL == colorPtr) {
888367b8cb7a6b2f163ee41365323cb219b7049b15reed        colorPtr = &storage;
898367b8cb7a6b2f163ee41365323cb219b7049b15reed    }
908367b8cb7a6b2f163ee41365323cb219b7049b15reed    if (this->onAsLuminanceColor(colorPtr)) {
918367b8cb7a6b2f163ee41365323cb219b7049b15reed        *colorPtr = SkColorSetA(*colorPtr, 0xFF);   // we only return opaque
928367b8cb7a6b2f163ee41365323cb219b7049b15reed        return true;
938367b8cb7a6b2f163ee41365323cb219b7049b15reed    }
948367b8cb7a6b2f163ee41365323cb219b7049b15reed    return false;
958367b8cb7a6b2f163ee41365323cb219b7049b15reed}
968367b8cb7a6b2f163ee41365323cb219b7049b15reed
97ce56d965069c1649afe14319cb239e6ad670682acommit-bot@chromium.orgSkShader::Context* SkShader::createContext(const ContextRec& rec, void* storage) const {
98ce56d965069c1649afe14319cb239e6ad670682acommit-bot@chromium.org    if (!this->computeTotalInverse(rec, NULL)) {
99ce56d965069c1649afe14319cb239e6ad670682acommit-bot@chromium.org        return NULL;
100ce56d965069c1649afe14319cb239e6ad670682acommit-bot@chromium.org    }
101ce56d965069c1649afe14319cb239e6ad670682acommit-bot@chromium.org    return this->onCreateContext(rec, storage);
1028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
10480116dcf1e1baf9817ae42d0aca51f7eabaa2880commit-bot@chromium.orgSkShader::Context* SkShader::onCreateContext(const ContextRec& rec, void*) const {
105f3e505984d08fb96e753be2b561f479dc3a4c544commit-bot@chromium.org    return NULL;
106f3e505984d08fb96e753be2b561f479dc3a4c544commit-bot@chromium.org}
107f3e505984d08fb96e753be2b561f479dc3a4c544commit-bot@chromium.org
108f3e505984d08fb96e753be2b561f479dc3a4c544commit-bot@chromium.orgsize_t SkShader::contextSize() const {
109f3e505984d08fb96e753be2b561f479dc3a4c544commit-bot@chromium.org    return 0;
110f3e505984d08fb96e753be2b561f479dc3a4c544commit-bot@chromium.org}
111f3e505984d08fb96e753be2b561f479dc3a4c544commit-bot@chromium.org
112e901b6de3ef8dea842008a08fc81e92fb1478d61commit-bot@chromium.orgSkShader::Context::Context(const SkShader& shader, const ContextRec& rec)
11380116dcf1e1baf9817ae42d0aca51f7eabaa2880commit-bot@chromium.org    : fShader(shader), fCTM(*rec.fMatrix)
11487fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org{
11587fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org    // Because the context parameters must be valid at this point, we know that the matrix is
11687fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org    // invertible.
117ce56d965069c1649afe14319cb239e6ad670682acommit-bot@chromium.org    SkAssertResult(fShader.computeTotalInverse(rec, &fTotalInverse));
11887fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org    fTotalInverseClass = (uint8_t)ComputeMatrixClass(fTotalInverse);
11987fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org
120e901b6de3ef8dea842008a08fc81e92fb1478d61commit-bot@chromium.org    fPaintAlpha = rec.fPaint->getAlpha();
121a641f3f18e5319773989812a888f3fad49e4f2adreed@google.com}
122a641f3f18e5319773989812a888f3fad49e4f2adreed@google.com
12387fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.orgSkShader::Context::~Context() {}
12487fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org
12587fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.orgSkShader::Context::ShadeProc SkShader::Context::asAShadeProc(void** ctx) {
1263bafe74a29c37761082980ed4ee9b831256bd27ereed@google.com    return NULL;
1273bafe74a29c37761082980ed4ee9b831256bd27ereed@google.com}
1283bafe74a29c37761082980ed4ee9b831256bd27ereed@google.com
1298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkColorPriv.h"
1308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
13187fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.orgvoid SkShader::Context::shadeSpan16(int x, int y, uint16_t span16[], int count) {
1328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(span16);
1338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(count > 0);
1348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(this->canCallShadeSpan16());
1358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // basically, if we get here, the subclass screwed up
1370c00f21fee3f5cfa3aa7e5d46ff94cb8cf340451tomhudson@google.com    SkDEBUGFAIL("kHasSpan16 flag is set, but shadeSpan16() not implemented");
1388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define kTempColorQuadCount 6   // balance between speed (larger) and saving stack-space
1417c2f27d788fff9dbf66a6d52753e47f786a313c0reed@google.com#define kTempColorCount     (kTempColorQuadCount << 2)
1428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_CPU_BENDIAN
1448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkU32BitShiftToByteOffset(shift)    (3 - ((shift) >> 3))
1458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else
1468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    #define SkU32BitShiftToByteOffset(shift)    ((shift) >> 3)
1478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
1488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
14987fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.orgvoid SkShader::Context::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
1508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(count > 0);
1518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPMColor   colors[kTempColorCount];
1538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    while ((count -= kTempColorCount) >= 0) {
1558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        this->shadeSpan(x, y, colors, kTempColorCount);
1568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        x += kTempColorCount;
1578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
1598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int quads = kTempColorQuadCount;
1608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        do {
1618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            U8CPU a0 = srcA[0];
1628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            U8CPU a1 = srcA[4];
1638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            U8CPU a2 = srcA[8];
1648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            U8CPU a3 = srcA[12];
1658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            srcA += 4*4;
1668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            *alpha++ = SkToU8(a0);
1678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            *alpha++ = SkToU8(a1);
1688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            *alpha++ = SkToU8(a2);
1698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            *alpha++ = SkToU8(a3);
1708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } while (--quads != 0);
1718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(count < 0);
1738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT(count + kTempColorCount >= 0);
1748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (count += kTempColorCount) {
1758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        this->shadeSpan(x, y, colors, count);
1768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
1788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        do {
1798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            *alpha++ = *srcA;
1808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            srcA += 4;
1818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } while (--count != 0);
1828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#if 0
1848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    do {
1858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int n = count;
1868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (n > kTempColorCount)
1878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            n = kTempColorCount;
1888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkASSERT(n > 0);
1898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        this->shadeSpan(x, y, colors, n);
1918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        x += n;
1928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        count -= n;
1938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        const uint8_t* srcA = (const uint8_t*)colors + SkU32BitShiftToByteOffset(SK_A32_SHIFT);
1958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        do {
1968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            *alpha++ = *srcA;
1978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            srcA += 4;
1988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } while (--n != 0);
1998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    } while (count > 0);
2008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
2018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
20387fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.orgSkShader::Context::MatrixClass SkShader::Context::ComputeMatrixClass(const SkMatrix& mat) {
2048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    MatrixClass mc = kLinear_MatrixClass;
2058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2068d430185e08d2067584837a76b7193b803fee7a0tomhudson@google.com    if (mat.hasPerspective()) {
2078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if (mat.fixedStepInX(0, NULL, NULL)) {
2088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            mc = kFixedStepInX_MatrixClass;
2098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        } else {
2108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            mc = kPerspective_MatrixClass;
2118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
2128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return mc;
2148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//////////////////////////////////////////////////////////////////////////////
2178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
21887fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.orgSkShader::BitmapType SkShader::asABitmap(SkBitmap*, SkMatrix*, TileMode*) const {
219f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    return kNone_BitmapType;
2208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
222d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.orgSkShader::GradientType SkShader::asAGradient(GradientInfo* info) const {
223d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org    return kNone_GradientType;
224d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org}
225d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org
226b0a8a377f832c59cee939ad721e1f87d378b7142joshualittbool SkShader::asFragmentProcessor(GrContext*, const SkPaint&, const SkMatrix*, GrColor*,
227b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt                                   GrFragmentProcessor**)  const {
2289de5b514d38c5b36066bcdc14fba2f7e5196d372dandov    return false;
22903c1c359b336ad20d23ab07004cdafafd14c90a5rileya@google.com}
23003c1c359b336ad20d23ab07004cdafafd14c90a5rileya@google.com
2318fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.orgSkShader* SkShader::refAsALocalMatrixShader(SkMatrix*) const {
2328fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org    return NULL;
2338fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org}
2348fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org
235ce56d965069c1649afe14319cb239e6ad670682acommit-bot@chromium.orgSkShader* SkShader::CreateEmptyShader() {
236ce56d965069c1649afe14319cb239e6ad670682acommit-bot@chromium.org    return SkNEW(SkEmptyShader);
237ce56d965069c1649afe14319cb239e6ad670682acommit-bot@chromium.org}
238ce56d965069c1649afe14319cb239e6ad670682acommit-bot@chromium.org
2398367b8cb7a6b2f163ee41365323cb219b7049b15reedSkShader* SkShader::CreateColorShader(SkColor color) {
2408367b8cb7a6b2f163ee41365323cb219b7049b15reed    return SkNEW_ARGS(SkColorShader, (color));
2418367b8cb7a6b2f163ee41365323cb219b7049b15reed}
2428367b8cb7a6b2f163ee41365323cb219b7049b15reed
2439c9005a347e9996f357bd79591bd34f74f8bbc66commit-bot@chromium.orgSkShader* SkShader::CreateBitmapShader(const SkBitmap& src, TileMode tmx, TileMode tmy,
2449c9005a347e9996f357bd79591bd34f74f8bbc66commit-bot@chromium.org                                       const SkMatrix* localMatrix) {
2459c9005a347e9996f357bd79591bd34f74f8bbc66commit-bot@chromium.org    return ::CreateBitmapShader(src, tmx, tmy, localMatrix, NULL);
2468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2485aacfe9ffcf1849727dca6761b4a221bd4315f26commit-bot@chromium.orgSkShader* SkShader::CreatePictureShader(SkPicture* src, TileMode tmx, TileMode tmy,
249b5f7826c51af2862aebcabe61e1ba684f925e488fmalita                                        const SkMatrix* localMatrix, const SkRect* tile) {
250b5f7826c51af2862aebcabe61e1ba684f925e488fmalita    return SkPictureShader::Create(src, tmx, tmy, localMatrix, tile);
251c5d9bb0f677069f62ec76373b9730e70e7352455commit-bot@chromium.org}
252c5d9bb0f677069f62ec76373b9730e70e7352455commit-bot@chromium.org
2530f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org#ifndef SK_IGNORE_TO_STRING
25476f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.comvoid SkShader::toString(SkString* str) const {
2555970f625e96cdc007c563ae72f343ae0d71719a1commit-bot@chromium.org    if (!fLocalMatrix.isIdentity()) {
25676f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com        str->append(" ");
2575970f625e96cdc007c563ae72f343ae0d71719a1commit-bot@chromium.org        fLocalMatrix.toString(str);
25876f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com    }
25976f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com}
26076f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com#endif
26176f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com
2628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//////////////////////////////////////////////////////////////////////////////
2638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkUtils.h"
2658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
26687fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.orgSkColorShader::SkColorShader(SkColor c)
26776a3b2abd02841c4ae786ac4cf59c3a51c545f73commit-bot@chromium.org    : fColor(c) {
268f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com}
269f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
270b6e161937bc890f0aa12ac5e27415d4d260ea6e0junov@chromium.orgbool SkColorShader::isOpaque() const {
271b6e161937bc890f0aa12ac5e27415d4d260ea6e0junov@chromium.org    return SkColorGetA(fColor) == 255;
272b6e161937bc890f0aa12ac5e27415d4d260ea6e0junov@chromium.org}
273b6e161937bc890f0aa12ac5e27415d4d260ea6e0junov@chromium.org
2749fa60daad4d5f54c0dbe3dbcc7608a8f6d721187reed#ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING
2758b0e8ac5f582de80356019406e2975079bf0829dcommit-bot@chromium.orgSkColorShader::SkColorShader(SkReadBuffer& b) : INHERITED(b) {
27676a3b2abd02841c4ae786ac4cf59c3a51c545f73commit-bot@chromium.org    // V25_COMPATIBILITY_CODE We had a boolean to make the color shader inherit the paint's
27776a3b2abd02841c4ae786ac4cf59c3a51c545f73commit-bot@chromium.org    // color. We don't support that any more.
2787ed173b1ebac84671fb0dc1b9bd323a5e6e63771commit-bot@chromium.org    if (b.isVersionLT(SkReadBuffer::kColorShaderNoBool_Version)) {
27976a3b2abd02841c4ae786ac4cf59c3a51c545f73commit-bot@chromium.org        if (b.readBool()) {
28076a3b2abd02841c4ae786ac4cf59c3a51c545f73commit-bot@chromium.org            SkDEBUGFAIL("We shouldn't have pictures that recorded the inherited case.");
28176a3b2abd02841c4ae786ac4cf59c3a51c545f73commit-bot@chromium.org            fColor = SK_ColorWHITE;
28276a3b2abd02841c4ae786ac4cf59c3a51c545f73commit-bot@chromium.org            return;
28376a3b2abd02841c4ae786ac4cf59c3a51c545f73commit-bot@chromium.org        }
2848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
285c73dd5c6880739f26216f198c757028fd28df1a4djsollen@google.com    fColor = b.readColor();
2868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2879fa60daad4d5f54c0dbe3dbcc7608a8f6d721187reed#endif
2889fa60daad4d5f54c0dbe3dbcc7608a8f6d721187reed
2899fa60daad4d5f54c0dbe3dbcc7608a8f6d721187reedSkFlattenable* SkColorShader::CreateProc(SkReadBuffer& buffer) {
2909fa60daad4d5f54c0dbe3dbcc7608a8f6d721187reed    return SkNEW_ARGS(SkColorShader, (buffer.readColor()));
2919fa60daad4d5f54c0dbe3dbcc7608a8f6d721187reed}
2928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
2938b0e8ac5f582de80356019406e2975079bf0829dcommit-bot@chromium.orgvoid SkColorShader::flatten(SkWriteBuffer& buffer) const {
294c73dd5c6880739f26216f198c757028fd28df1a4djsollen@google.com    buffer.writeColor(fColor);
2958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
2968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
29787fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.orguint32_t SkColorShader::ColorShaderContext::getFlags() const {
29859ccef695cef28a74ab2ea13d5a6c9017af45402reed@google.com    return fFlags;
29959ccef695cef28a74ab2ea13d5a6c9017af45402reed@google.com}
30059ccef695cef28a74ab2ea13d5a6c9017af45402reed@google.com
30187fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.orguint8_t SkColorShader::ColorShaderContext::getSpan16Alpha() const {
3028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return SkGetPackedA32(fPMColor);
3038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
305ce56d965069c1649afe14319cb239e6ad670682acommit-bot@chromium.orgSkShader::Context* SkColorShader::onCreateContext(const ContextRec& rec, void* storage) const {
306e901b6de3ef8dea842008a08fc81e92fb1478d61commit-bot@chromium.org    return SkNEW_PLACEMENT_ARGS(storage, ColorShaderContext, (*this, rec));
30787fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org}
30887fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org
30987fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.orgSkColorShader::ColorShaderContext::ColorShaderContext(const SkColorShader& shader,
310e901b6de3ef8dea842008a08fc81e92fb1478d61commit-bot@chromium.org                                                      const ContextRec& rec)
311e901b6de3ef8dea842008a08fc81e92fb1478d61commit-bot@chromium.org    : INHERITED(shader, rec)
31287fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org{
3139c9005a347e9996f357bd79591bd34f74f8bbc66commit-bot@chromium.org    SkColor color = shader.fColor;
314e901b6de3ef8dea842008a08fc81e92fb1478d61commit-bot@chromium.org    unsigned a = SkAlphaMul(SkColorGetA(color), SkAlpha255To256(rec.fPaint->getAlpha()));
3158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
31687fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org    unsigned r = SkColorGetR(color);
31787fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org    unsigned g = SkColorGetG(color);
31887fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org    unsigned b = SkColorGetB(color);
3198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // we want this before we apply any alpha
3218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fColor16 = SkPack888ToRGB16(r, g, b);
3228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (a != 255) {
3248f073382bb6a9b3998a74e6b58654476b77b4c86reed@android.com        r = SkMulDiv255Round(r, a);
3258f073382bb6a9b3998a74e6b58654476b77b4c86reed@android.com        g = SkMulDiv255Round(g, a);
3268f073382bb6a9b3998a74e6b58654476b77b4c86reed@android.com        b = SkMulDiv255Round(b, a);
3278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
3288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fPMColor = SkPackARGB32(a, r, g, b);
3298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
3308f073382bb6a9b3998a74e6b58654476b77b4c86reed@android.com    fFlags = kConstInY32_Flag;
3315b81535014f545f6498f5c8721723b81576989b1reed@android.com    if (255 == a) {
3325119bdb952025a30f115b9c6a187173956e55097reed@android.com        fFlags |= kOpaqueAlpha_Flag;
333e901b6de3ef8dea842008a08fc81e92fb1478d61commit-bot@chromium.org        if (rec.fPaint->isDither() == false) {
3345b81535014f545f6498f5c8721723b81576989b1reed@android.com            fFlags |= kHasSpan16_Flag;
3355b81535014f545f6498f5c8721723b81576989b1reed@android.com        }
3365119bdb952025a30f115b9c6a187173956e55097reed@android.com    }
3378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
33987fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.orgvoid SkColorShader::ColorShaderContext::shadeSpan(int x, int y, SkPMColor span[], int count) {
3408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    sk_memset32(span, fPMColor, count);
3418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
34387fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.orgvoid SkColorShader::ColorShaderContext::shadeSpan16(int x, int y, uint16_t span[], int count) {
3448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    sk_memset16(span, fColor16, count);
3458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
34787fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.orgvoid SkColorShader::ColorShaderContext::shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) {
3488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    memset(alpha, SkGetPackedA32(fPMColor), count);
3498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
3508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
351f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com// if we had a asAColor method, that would be more efficient...
352f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.comSkShader::BitmapType SkColorShader::asABitmap(SkBitmap* bitmap, SkMatrix* matrix,
35391f319c5dc4493384f0a52aaeef3dcc311ef6ed0rileya@google.com                                              TileMode modes[]) const {
3542be9e8b407624fa696854b78b407b97a01dbb703reed@google.com    return kNone_BitmapType;
355f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com}
356f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
357d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.orgSkShader::GradientType SkColorShader::asAGradient(GradientInfo* info) const {
358d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org    if (info) {
359d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org        if (info->fColors && info->fColorCount >= 1) {
360d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org            info->fColors[0] = fColor;
361d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org        }
362d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org        info->fColorCount = 1;
363d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org        info->fTileMode = SkShader::kRepeat_TileMode;
364d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org    }
365d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org    return kColor_GradientType;
366d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org}
36737a201231b8f6381938282675eb9abb50ab3b389reed@google.com
3689de5b514d38c5b36066bcdc14fba2f7e5196d372dandov#if SK_SUPPORT_GPU
3699de5b514d38c5b36066bcdc14fba2f7e5196d372dandov
3709de5b514d38c5b36066bcdc14fba2f7e5196d372dandov#include "SkGr.h"
3719de5b514d38c5b36066bcdc14fba2f7e5196d372dandov
372b0a8a377f832c59cee939ad721e1f87d378b7142joshualittbool SkColorShader::asFragmentProcessor(GrContext*, const SkPaint& paint, const SkMatrix*,
373b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt                                        GrColor* paintColor, GrFragmentProcessor** fp) const {
374b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt    *fp = NULL;
3759de5b514d38c5b36066bcdc14fba2f7e5196d372dandov    SkColor skColor = fColor;
3769de5b514d38c5b36066bcdc14fba2f7e5196d372dandov    U8CPU newA = SkMulDiv255Round(SkColorGetA(fColor), paint.getAlpha());
37783d081ae1d731b5039e99823620f5e287542ee39bsalomon    *paintColor = SkColor2GrColor(SkColorSetA(skColor, newA));
3789de5b514d38c5b36066bcdc14fba2f7e5196d372dandov    return true;
3799de5b514d38c5b36066bcdc14fba2f7e5196d372dandov}
3809de5b514d38c5b36066bcdc14fba2f7e5196d372dandov
3819de5b514d38c5b36066bcdc14fba2f7e5196d372dandov#else
3829de5b514d38c5b36066bcdc14fba2f7e5196d372dandov
383b0a8a377f832c59cee939ad721e1f87d378b7142joshualittbool SkColorShader::asFragmentProcessor(GrContext*, const SkPaint&, const SkMatrix*, GrColor*,
384b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt                                        GrFragmentProcessor**) const {
3859de5b514d38c5b36066bcdc14fba2f7e5196d372dandov    SkDEBUGFAIL("Should not call in GPU-less build");
3869de5b514d38c5b36066bcdc14fba2f7e5196d372dandov    return false;
3879de5b514d38c5b36066bcdc14fba2f7e5196d372dandov}
3889de5b514d38c5b36066bcdc14fba2f7e5196d372dandov
3899de5b514d38c5b36066bcdc14fba2f7e5196d372dandov#endif
3909de5b514d38c5b36066bcdc14fba2f7e5196d372dandov
3910f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org#ifndef SK_IGNORE_TO_STRING
39276f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.comvoid SkColorShader::toString(SkString* str) const {
39376f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com    str->append("SkColorShader: (");
39476f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com
39576a3b2abd02841c4ae786ac4cf59c3a51c545f73commit-bot@chromium.org    str->append("Color: ");
39676a3b2abd02841c4ae786ac4cf59c3a51c545f73commit-bot@chromium.org    str->appendHex(fColor);
39776f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com
39876f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com    this->INHERITED::toString(str);
39976f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com
40076f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com    str->append(")");
40176f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com}
40276f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com#endif
40376f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com
40437a201231b8f6381938282675eb9abb50ab3b389reed@google.com///////////////////////////////////////////////////////////////////////////////
40537a201231b8f6381938282675eb9abb50ab3b389reed@google.com
4069fa60daad4d5f54c0dbe3dbcc7608a8f6d721187reedSkFlattenable* SkEmptyShader::CreateProc(SkReadBuffer&) {
4079fa60daad4d5f54c0dbe3dbcc7608a8f6d721187reed    return SkShader::CreateEmptyShader();
4089fa60daad4d5f54c0dbe3dbcc7608a8f6d721187reed}
4099fa60daad4d5f54c0dbe3dbcc7608a8f6d721187reed
41087fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org#ifndef SK_IGNORE_TO_STRING
41137a201231b8f6381938282675eb9abb50ab3b389reed@google.com#include "SkEmptyShader.h"
41237a201231b8f6381938282675eb9abb50ab3b389reed@google.com
41376f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.comvoid SkEmptyShader::toString(SkString* str) const {
41476f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com    str->append("SkEmptyShader: (");
41576f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com
41676f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com    this->INHERITED::toString(str);
41776f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com
41876f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com    str->append(")");
41976f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com}
42076f9e938df0b5826fd4c80b854ceafaf385cfbe1robertphillips@google.com#endif
421