18a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2006 The Android Open Source Project
38a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.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.
68a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */
78a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
8ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SkShader_DEFINED
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkShader_DEFINED
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkBitmap.h"
138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkFlattenable.h"
148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkMask.h"
158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkMatrix.h"
168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPaint.h"
174beef91ec04b2edfbe983e672d50cd8f477eda7fbsalomon#include "../gpu/GrColor.h"
188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkPath;
20c5d9bb0f677069f62ec76373b9730e70e7352455commit-bot@chromium.orgclass SkPicture;
21795905562d1bd8bbedcf47f6a00efb220ec8bbe0commit-bot@chromium.orgclass SkXfermode;
2203c1c359b336ad20d23ab07004cdafafd14c90a5rileya@google.comclass GrContext;
23b0a8a377f832c59cee939ad721e1f87d378b7142joshualittclass GrFragmentProcessor;
248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** \class SkShader
26ad91799832b9ba2a3de30c0f57968148b4be17afreed@google.com *
27880dc472ab2c987083db9eb83efb2930787f0b26reed@google.com *  Shaders specify the source color(s) for what is being drawn. If a paint
28880dc472ab2c987083db9eb83efb2930787f0b26reed@google.com *  has no shader, then the paint's color is used. If the paint has a
29880dc472ab2c987083db9eb83efb2930787f0b26reed@google.com *  shader, then the shader's color(s) are use instead, but they are
30880dc472ab2c987083db9eb83efb2930787f0b26reed@google.com *  modulated by the paint's alpha. This makes it easy to create a shader
31880dc472ab2c987083db9eb83efb2930787f0b26reed@google.com *  once (e.g. bitmap tiling or gradient) and then change its transparency
32880dc472ab2c987083db9eb83efb2930787f0b26reed@google.com *  w/o having to modify the original shader... only the paint's alpha needs
33880dc472ab2c987083db9eb83efb2930787f0b26reed@google.com *  to be modified.
34ad91799832b9ba2a3de30c0f57968148b4be17afreed@google.com */
357ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.orgclass SK_API SkShader : public SkFlattenable {
368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
370456e0b7b85060e9b9597ce414c4c2b19aff4f58robertphillips@google.com    SK_DECLARE_INST_COUNT(SkShader)
380456e0b7b85060e9b9597ce414c4c2b19aff4f58robertphillips@google.com
399c9005a347e9996f357bd79591bd34f74f8bbc66commit-bot@chromium.org    SkShader(const SkMatrix* localMatrix = NULL);
408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    virtual ~SkShader();
418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
42ad91799832b9ba2a3de30c0f57968148b4be17afreed@google.com    /**
435970f625e96cdc007c563ae72f343ae0d71719a1commit-bot@chromium.org     *  Returns the local matrix.
44c870d494dd0dc88f3f4155a0d6257a1be8280880scroggo     *
45c870d494dd0dc88f3f4155a0d6257a1be8280880scroggo     *  FIXME: This can be incorrect for a Shader with its own local matrix
46c870d494dd0dc88f3f4155a0d6257a1be8280880scroggo     *  that is also wrapped via CreateLocalMatrixShader.
47ad91799832b9ba2a3de30c0f57968148b4be17afreed@google.com     */
485970f625e96cdc007c563ae72f343ae0d71719a1commit-bot@chromium.org    const SkMatrix& getLocalMatrix() const { return fLocalMatrix; }
49f94b3a4cebd4adab09c40ebe23c02a615e10c394bsalomon@google.com
50f94b3a4cebd4adab09c40ebe23c02a615e10c394bsalomon@google.com    /**
515970f625e96cdc007c563ae72f343ae0d71719a1commit-bot@chromium.org     *  Returns true if the local matrix is not an identity matrix.
52c870d494dd0dc88f3f4155a0d6257a1be8280880scroggo     *
53c870d494dd0dc88f3f4155a0d6257a1be8280880scroggo     *  FIXME: This can be incorrect for a Shader with its own local matrix
54c870d494dd0dc88f3f4155a0d6257a1be8280880scroggo     *  that is also wrapped via CreateLocalMatrixShader.
55f94b3a4cebd4adab09c40ebe23c02a615e10c394bsalomon@google.com     */
565970f625e96cdc007c563ae72f343ae0d71719a1commit-bot@chromium.org    bool hasLocalMatrix() const { return !fLocalMatrix.isIdentity(); }
57ad91799832b9ba2a3de30c0f57968148b4be17afreed@google.com
588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    enum TileMode {
590beaba5b34888001c3811c26be6954696a2cdbdfreed@google.com        /** replicate the edge color if the shader draws outside of its
600beaba5b34888001c3811c26be6954696a2cdbdfreed@google.com         *  original bounds
610beaba5b34888001c3811c26be6954696a2cdbdfreed@google.com         */
620beaba5b34888001c3811c26be6954696a2cdbdfreed@google.com        kClamp_TileMode,
630beaba5b34888001c3811c26be6954696a2cdbdfreed@google.com
640beaba5b34888001c3811c26be6954696a2cdbdfreed@google.com        /** repeat the shader's image horizontally and vertically */
650beaba5b34888001c3811c26be6954696a2cdbdfreed@google.com        kRepeat_TileMode,
660beaba5b34888001c3811c26be6954696a2cdbdfreed@google.com
670beaba5b34888001c3811c26be6954696a2cdbdfreed@google.com        /** repeat the shader's image horizontally and vertically, alternating
680beaba5b34888001c3811c26be6954696a2cdbdfreed@google.com         *  mirror images so that adjacent images always seam
690beaba5b34888001c3811c26be6954696a2cdbdfreed@google.com         */
700beaba5b34888001c3811c26be6954696a2cdbdfreed@google.com        kMirror_TileMode,
710beaba5b34888001c3811c26be6954696a2cdbdfreed@google.com
720beaba5b34888001c3811c26be6954696a2cdbdfreed@google.com#if 0
730beaba5b34888001c3811c26be6954696a2cdbdfreed@google.com        /** only draw within the original domain, return 0 everywhere else */
740beaba5b34888001c3811c26be6954696a2cdbdfreed@google.com        kDecal_TileMode,
750beaba5b34888001c3811c26be6954696a2cdbdfreed@google.com#endif
768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kTileModeCount
788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    };
798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // override these in your subclass
818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    enum Flags {
838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        //!< set if all of the colors will be opaque
843c9b2a4a0e4f57db23640e85959ee78b86634628reed@android.com        kOpaqueAlpha_Flag  = 0x01,
855119bdb952025a30f115b9c6a187173956e55097reed@android.com
868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        //! set if this shader's shadeSpan16() method can be called
873c9b2a4a0e4f57db23640e85959ee78b86634628reed@android.com        kHasSpan16_Flag = 0x02,
885119bdb952025a30f115b9c6a187173956e55097reed@android.com
898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        /** Set this bit if the shader's native data type is instrinsically 16
908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            bit, meaning that calling the 32bit shadeSpan() entry point will
918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            mean the the impl has to up-sample 16bit data into 32bit. Used as a
928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            a means of clearing a dither request if the it will have no effect
938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        */
945119bdb952025a30f115b9c6a187173956e55097reed@android.com        kIntrinsicly16_Flag = 0x04,
955119bdb952025a30f115b9c6a187173956e55097reed@android.com
9687fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org        /** set if the spans only vary in X (const in Y).
975119bdb952025a30f115b9c6a187173956e55097reed@android.com            e.g. an Nx1 bitmap that is being tiled in Y, or a linear-gradient
983c9b2a4a0e4f57db23640e85959ee78b86634628reed@android.com            that varies from left-to-right. This flag specifies this for
993c9b2a4a0e4f57db23640e85959ee78b86634628reed@android.com            shadeSpan().
1005119bdb952025a30f115b9c6a187173956e55097reed@android.com         */
1013c9b2a4a0e4f57db23640e85959ee78b86634628reed@android.com        kConstInY32_Flag = 0x08,
1027c2f27d788fff9dbf66a6d52753e47f786a313c0reed@google.com
1033c9b2a4a0e4f57db23640e85959ee78b86634628reed@android.com        /** same as kConstInY32_Flag, but is set if this is true for shadeSpan16
1043c9b2a4a0e4f57db23640e85959ee78b86634628reed@android.com            which may not always be the case, since shadeSpan16 may be
1053c9b2a4a0e4f57db23640e85959ee78b86634628reed@android.com            predithered, which would mean it was not const in Y, even though
1063c9b2a4a0e4f57db23640e85959ee78b86634628reed@android.com            the 32bit shadeSpan() would be const.
1073c9b2a4a0e4f57db23640e85959ee78b86634628reed@android.com         */
1083c9b2a4a0e4f57db23640e85959ee78b86634628reed@android.com        kConstInY16_Flag = 0x10
1098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    };
1108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
111ad91799832b9ba2a3de30c0f57968148b4be17afreed@google.com    /**
112b6e161937bc890f0aa12ac5e27415d4d260ea6e0junov@chromium.org     *  Returns true if the shader is guaranteed to produce only opaque
113b6e161937bc890f0aa12ac5e27415d4d260ea6e0junov@chromium.org     *  colors, subject to the SkPaint using the shader to apply an opaque
114b6e161937bc890f0aa12ac5e27415d4d260ea6e0junov@chromium.org     *  alpha value. Subclasses should override this to allow some
11587fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org     *  optimizations.
116b6e161937bc890f0aa12ac5e27415d4d260ea6e0junov@chromium.org     */
117b6e161937bc890f0aa12ac5e27415d4d260ea6e0junov@chromium.org    virtual bool isOpaque() const { return false; }
118b6e161937bc890f0aa12ac5e27415d4d260ea6e0junov@chromium.org
119e901b6de3ef8dea842008a08fc81e92fb1478d61commit-bot@chromium.org    /**
120e901b6de3ef8dea842008a08fc81e92fb1478d61commit-bot@chromium.org     *  ContextRec acts as a parameter bundle for creating Contexts.
121e901b6de3ef8dea842008a08fc81e92fb1478d61commit-bot@chromium.org     */
122e901b6de3ef8dea842008a08fc81e92fb1478d61commit-bot@chromium.org    struct ContextRec {
12380116dcf1e1baf9817ae42d0aca51f7eabaa2880commit-bot@chromium.org        ContextRec() : fDevice(NULL), fPaint(NULL), fMatrix(NULL), fLocalMatrix(NULL) {}
124e901b6de3ef8dea842008a08fc81e92fb1478d61commit-bot@chromium.org        ContextRec(const SkBitmap& device, const SkPaint& paint, const SkMatrix& matrix)
125e901b6de3ef8dea842008a08fc81e92fb1478d61commit-bot@chromium.org            : fDevice(&device)
126e901b6de3ef8dea842008a08fc81e92fb1478d61commit-bot@chromium.org            , fPaint(&paint)
12780116dcf1e1baf9817ae42d0aca51f7eabaa2880commit-bot@chromium.org            , fMatrix(&matrix)
12880116dcf1e1baf9817ae42d0aca51f7eabaa2880commit-bot@chromium.org            , fLocalMatrix(NULL) {}
129e901b6de3ef8dea842008a08fc81e92fb1478d61commit-bot@chromium.org
13080116dcf1e1baf9817ae42d0aca51f7eabaa2880commit-bot@chromium.org        const SkBitmap* fDevice;        // the bitmap we are drawing into
13180116dcf1e1baf9817ae42d0aca51f7eabaa2880commit-bot@chromium.org        const SkPaint*  fPaint;         // the current paint associated with the draw
13280116dcf1e1baf9817ae42d0aca51f7eabaa2880commit-bot@chromium.org        const SkMatrix* fMatrix;        // the current matrix in the canvas
13380116dcf1e1baf9817ae42d0aca51f7eabaa2880commit-bot@chromium.org        const SkMatrix* fLocalMatrix;   // optional local matrix
134e901b6de3ef8dea842008a08fc81e92fb1478d61commit-bot@chromium.org    };
135e901b6de3ef8dea842008a08fc81e92fb1478d61commit-bot@chromium.org
13687fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org    class Context : public ::SkNoncopyable {
13787fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org    public:
138e901b6de3ef8dea842008a08fc81e92fb1478d61commit-bot@chromium.org        Context(const SkShader& shader, const ContextRec&);
139001f4ed2fb62ecdc98ce2884d925de11b7516d23commit-bot@chromium.org
14087fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org        virtual ~Context();
141bc2f1dc85e458af7bdb87873e60207f9f7299e4acommit-bot@chromium.org
14287fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org        /**
14387fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org         *  Called sometimes before drawing with this shader. Return the type of
14487fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org         *  alpha your shader will return. The default implementation returns 0.
14587fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org         *  Your subclass should override if it can (even sometimes) report a
14687fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org         *  non-zero value, since that will enable various blitters to perform
14787fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org         *  faster.
14887fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org         */
14987fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org        virtual uint32_t getFlags() const { return 0; }
150bc2f1dc85e458af7bdb87873e60207f9f7299e4acommit-bot@chromium.org
15187fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org        /**
15287fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org         *  Return the alpha associated with the data returned by shadeSpan16(). If
15387fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org         *  kHasSpan16_Flag is not set, this value is meaningless.
15487fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org         */
15587fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org        virtual uint8_t getSpan16Alpha() const { return fPaintAlpha; }
156bc2f1dc85e458af7bdb87873e60207f9f7299e4acommit-bot@chromium.org
15787fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org        /**
15887fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org         *  Called for each span of the object being drawn. Your subclass should
15987fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org         *  set the appropriate colors (with premultiplied alpha) that correspond
16087fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org         *  to the specified device coordinates.
16187fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org         */
16287fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org        virtual void shadeSpan(int x, int y, SkPMColor[], int count) = 0;
16387fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org
16487fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org        typedef void (*ShadeProc)(void* ctx, int x, int y, SkPMColor[], int count);
16587fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org        virtual ShadeProc asAShadeProc(void** ctx);
16687fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org
16787fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org        /**
16887fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org         *  Called only for 16bit devices when getFlags() returns
16987fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org         *  kOpaqueAlphaFlag | kHasSpan16_Flag
17087fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org         */
17187fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org        virtual void shadeSpan16(int x, int y, uint16_t[], int count);
17287fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org
17387fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org        /**
17487fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org         *  Similar to shadeSpan, but only returns the alpha-channel for a span.
17587fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org         *  The default implementation calls shadeSpan() and then extracts the alpha
17687fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org         *  values from the returned colors.
17787fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org         */
17887fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org        virtual void shadeSpanAlpha(int x, int y, uint8_t alpha[], int count);
179ad91799832b9ba2a3de30c0f57968148b4be17afreed@google.com
18087fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org        /**
18187fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org         *  Helper function that returns true if this shader's shadeSpan16() method
18287fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org         *  can be called.
18387fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org         */
18487fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org        bool canCallShadeSpan16() {
18587fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org            return SkShader::CanCallShadeSpan16(this->getFlags());
18687fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org        }
18787fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org
188cc0e3110bde60201882a2c512a5cdd73c838f351reed        // Notification from blitter::blitMask in case we need to see the non-alpha channels
189cc0e3110bde60201882a2c512a5cdd73c838f351reed        virtual void set3DMask(const SkMask*) {}
190cc0e3110bde60201882a2c512a5cdd73c838f351reed
19187fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org    protected:
19287fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org        // Reference to shader, so we don't have to dupe information.
19387fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org        const SkShader& fShader;
19487fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org
19587fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org        enum MatrixClass {
19687fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org            kLinear_MatrixClass,            // no perspective
19787fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org            kFixedStepInX_MatrixClass,      // fast perspective, need to call fixedStepInX() each
19887fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org                                            // scanline
19987fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org            kPerspective_MatrixClass        // slow perspective, need to mappoints each pixel
20087fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org        };
20187fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org        static MatrixClass ComputeMatrixClass(const SkMatrix&);
20287fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org
20380116dcf1e1baf9817ae42d0aca51f7eabaa2880commit-bot@chromium.org        uint8_t         getPaintAlpha() const { return fPaintAlpha; }
20480116dcf1e1baf9817ae42d0aca51f7eabaa2880commit-bot@chromium.org        const SkMatrix& getTotalInverse() const { return fTotalInverse; }
20580116dcf1e1baf9817ae42d0aca51f7eabaa2880commit-bot@chromium.org        MatrixClass     getInverseClass() const { return (MatrixClass)fTotalInverseClass; }
20680116dcf1e1baf9817ae42d0aca51f7eabaa2880commit-bot@chromium.org        const SkMatrix& getCTM() const { return fCTM; }
20787fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org    private:
20880116dcf1e1baf9817ae42d0aca51f7eabaa2880commit-bot@chromium.org        SkMatrix    fCTM;
20980116dcf1e1baf9817ae42d0aca51f7eabaa2880commit-bot@chromium.org        SkMatrix    fTotalInverse;
21080116dcf1e1baf9817ae42d0aca51f7eabaa2880commit-bot@chromium.org        uint8_t     fPaintAlpha;
21180116dcf1e1baf9817ae42d0aca51f7eabaa2880commit-bot@chromium.org        uint8_t     fTotalInverseClass;
21287fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org
21387fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org        typedef SkNoncopyable INHERITED;
21487fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org    };
2153bafe74a29c37761082980ed4ee9b831256bd27ereed@google.com
216ad91799832b9ba2a3de30c0f57968148b4be17afreed@google.com    /**
21787fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org     *  Create the actual object that does the shading.
21887fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org     *  Size of storage must be >= contextSize.
219ad91799832b9ba2a3de30c0f57968148b4be17afreed@google.com     */
220ce56d965069c1649afe14319cb239e6ad670682acommit-bot@chromium.org    Context* createContext(const ContextRec&, void* storage) const;
2218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
222ad91799832b9ba2a3de30c0f57968148b4be17afreed@google.com    /**
22387fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org     *  Return the size of a Context returned by createContext.
224f3e505984d08fb96e753be2b561f479dc3a4c544commit-bot@chromium.org     *
225f3e505984d08fb96e753be2b561f479dc3a4c544commit-bot@chromium.org     *  Override this if your subclass overrides createContext, to return the correct size of
226f3e505984d08fb96e753be2b561f479dc3a4c544commit-bot@chromium.org     *  your subclass' context.
227ad91799832b9ba2a3de30c0f57968148b4be17afreed@google.com     */
228f3e505984d08fb96e753be2b561f479dc3a4c544commit-bot@chromium.org    virtual size_t contextSize() const;
2298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
230ad91799832b9ba2a3de30c0f57968148b4be17afreed@google.com    /**
231ad91799832b9ba2a3de30c0f57968148b4be17afreed@google.com     *  Helper to check the flags to know if it is legal to call shadeSpan16()
232ad91799832b9ba2a3de30c0f57968148b4be17afreed@google.com     */
2338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static bool CanCallShadeSpan16(uint32_t flags) {
2348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return (flags & kHasSpan16_Flag) != 0;
2358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
2368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
237ad91799832b9ba2a3de30c0f57968148b4be17afreed@google.com    /**
238f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com     Gives method bitmap should be read to implement a shader.
239f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com     Also determines number and interpretation of "extra" parameters returned
240f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com     by asABitmap
241f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com     */
242f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    enum BitmapType {
243f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        kNone_BitmapType,   //<! Shader is not represented as a bitmap
2447c2f27d788fff9dbf66a6d52753e47f786a313c0reed@google.com        kDefault_BitmapType,//<! Access bitmap using local coords transformed
245f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                            //   by matrix. No extras
2467c2f27d788fff9dbf66a6d52753e47f786a313c0reed@google.com        kRadial_BitmapType, //<! Access bitmap by transforming local coordinates
2477c2f27d788fff9dbf66a6d52753e47f786a313c0reed@google.com                            //   by the matrix and taking the distance of result
2487c2f27d788fff9dbf66a6d52753e47f786a313c0reed@google.com                            //   from  (0,0) as bitmap column. Bitmap is 1 pixel
249f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                            //   tall. No extras
2507c2f27d788fff9dbf66a6d52753e47f786a313c0reed@google.com        kSweep_BitmapType,  //<! Access bitmap by transforming local coordinates
251f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                            //   by the matrix and taking the angle of result
252f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                            //   to (0,0) as bitmap x coord, where angle = 0 is
2537c2f27d788fff9dbf66a6d52753e47f786a313c0reed@google.com                            //   bitmap left edge of bitmap = 2pi is the
254f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                            //   right edge. Bitmap is 1 pixel tall. No extras
255d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org        kTwoPointRadial_BitmapType,
2567c2f27d788fff9dbf66a6d52753e47f786a313c0reed@google.com                            //<! Matrix transforms to space where (0,0) is
257f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                            //   the center of the starting circle.  The second
2587c2f27d788fff9dbf66a6d52753e47f786a313c0reed@google.com                            //   circle will be centered (x, 0) where x  may be
2597c2f27d788fff9dbf66a6d52753e47f786a313c0reed@google.com                            //   0. The post-matrix space is normalized such
260f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                            //   that 1 is the second radius - first radius.
261f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                            //   Three extra parameters are returned:
2627c2f27d788fff9dbf66a6d52753e47f786a313c0reed@google.com                            //      0: x-offset of second circle center
263f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                            //         to first.
2647c2f27d788fff9dbf66a6d52753e47f786a313c0reed@google.com                            //      1: radius of first circle in post-matrix
265f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                            //         space
266f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                            //      2: the second radius minus the first radius
2677c2f27d788fff9dbf66a6d52753e47f786a313c0reed@google.com                            //         in pre-transformed space.
2683e33258cd15faae4a2906ddcc586e4008ee20e68rileya@google.com        kTwoPointConical_BitmapType,
2693e33258cd15faae4a2906ddcc586e4008ee20e68rileya@google.com                            //<! Matrix transforms to space where (0,0) is
2703e33258cd15faae4a2906ddcc586e4008ee20e68rileya@google.com                            //   the center of the starting circle.  The second
2713e33258cd15faae4a2906ddcc586e4008ee20e68rileya@google.com                            //   circle will be centered (x, 0) where x  may be
2723e33258cd15faae4a2906ddcc586e4008ee20e68rileya@google.com                            //   0.
2733e33258cd15faae4a2906ddcc586e4008ee20e68rileya@google.com                            //   Three extra parameters are returned:
2743e33258cd15faae4a2906ddcc586e4008ee20e68rileya@google.com                            //      0: x-offset of second circle center
2753e33258cd15faae4a2906ddcc586e4008ee20e68rileya@google.com                            //         to first.
2763e33258cd15faae4a2906ddcc586e4008ee20e68rileya@google.com                            //      1: radius of first circle
2773e33258cd15faae4a2906ddcc586e4008ee20e68rileya@google.com                            //      2: the second radius minus the first radius
27822e57f991628451c02f970beea379ad632bb6a10rileya@google.com        kLinear_BitmapType, //<! Access bitmap using local coords transformed
27922e57f991628451c02f970beea379ad632bb6a10rileya@google.com                            //   by matrix. No extras
280d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org
28122e57f991628451c02f970beea379ad632bb6a10rileya@google.com       kLast_BitmapType = kLinear_BitmapType
282f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    };
2838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Optional methods for shaders that can pretend to be a bitmap/texture
2847c2f27d788fff9dbf66a6d52753e47f786a313c0reed@google.com        to play along with opengl. Default just returns kNone_BitmapType and
285f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        ignores the out parameters.
286f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
287f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        @param outTexture if non-NULL will be the bitmap representing the shader
288f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                          after return.
289f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        @param outMatrix  if non-NULL will be the matrix to apply to vertices
290f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                          to access the bitmap after return.
291f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        @param xy         if non-NULL will be the tile modes that should be
292f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                          used to access the bitmap after return.
293f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        @param twoPointRadialParams Two extra return values needed for two point
294f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                                    radial bitmaps. The first is the x-offset of
295f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                                    the second point and the second is the radius
296f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                                    about the first point.
2978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
298f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    virtual BitmapType asABitmap(SkBitmap* outTexture, SkMatrix* outMatrix,
29991f319c5dc4493384f0a52aaeef3dcc311ef6ed0rileya@google.com                         TileMode xy[2]) const;
3008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
301d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org    /**
302d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org     *  If the shader subclass can be represented as a gradient, asAGradient
303d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org     *  returns the matching GradientType enum (or kNone_GradientType if it
304d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org     *  cannot). Also, if info is not null, asAGradient populates info with
305d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org     *  the relevant (see below) parameters for the gradient.  fColorCount
306d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org     *  is both an input and output parameter.  On input, it indicates how
307d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org     *  many entries in fColors and fColorOffsets can be used, if they are
308d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org     *  non-NULL.  After asAGradient has run, fColorCount indicates how
309d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org     *  many color-offset pairs there are in the gradient.  If there is
310d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org     *  insufficient space to store all of the color-offset pairs, fColors
311d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org     *  and fColorOffsets will not be altered.  fColorOffsets specifies
312d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org     *  where on the range of 0 to 1 to transition to the given color.
313d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org     *  The meaning of fPoint and fRadius is dependant on the type of gradient.
314d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org     *
315d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org     *  None:
316d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org     *      info is ignored.
317d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org     *  Color:
318d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org     *      fColorOffsets[0] is meaningless.
319d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org     *  Linear:
320d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org     *      fPoint[0] and fPoint[1] are the end-points of the gradient
321d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org     *  Radial:
322d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org     *      fPoint[0] and fRadius[0] are the center and radius
323d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org     *  Radial2:
324d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org     *      fPoint[0] and fRadius[0] are the center and radius of the 1st circle
325d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org     *      fPoint[1] and fRadius[1] are the center and radius of the 2nd circle
326d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org     *  Sweep:
327d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org     *      fPoint[0] is the center of the sweep.
328d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org     */
329d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org
330d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org    enum GradientType {
331d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org        kNone_GradientType,
332d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org        kColor_GradientType,
333d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org        kLinear_GradientType,
334d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org        kRadial_GradientType,
335d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org        kRadial2_GradientType,
336d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org        kSweep_GradientType,
33783226976b532141b26ff3a40f381a5d08ce3259dreed@google.com        kConical_GradientType,
33883226976b532141b26ff3a40f381a5d08ce3259dreed@google.com        kLast_GradientType = kConical_GradientType
339d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org    };
340d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org
341d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org    struct GradientInfo {
342d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org        int         fColorCount;    //!< In-out parameter, specifies passed size
343d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org                                    //   of fColors/fColorOffsets on input, and
344d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org                                    //   actual number of colors/offsets on
345d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org                                    //   output.
346d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org        SkColor*    fColors;        //!< The colors in the gradient.
347d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org        SkScalar*   fColorOffsets;  //!< The unit offset for color transitions.
348d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org        SkPoint     fPoint[2];      //!< Type specific, see above.
349d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org        SkScalar    fRadius[2];     //!< Type specific, see above.
350d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org        TileMode    fTileMode;      //!< The tile mode used.
3513d3a860d0ba878adb905512a45c500a67532b0a3reed@google.com        uint32_t    fGradientFlags; //!< see SkGradientShader::Flags
352d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org    };
353d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org
354d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org    virtual GradientType asAGradient(GradientInfo* info) const;
355d3ae77965e94e0efda496f5461cbec4533cb5b16vandebo@chromium.org
35603c1c359b336ad20d23ab07004cdafafd14c90a5rileya@google.com    /**
357795905562d1bd8bbedcf47f6a00efb220ec8bbe0commit-bot@chromium.org     *  If the shader subclass is composed of two shaders, return true, and if rec is not NULL,
358795905562d1bd8bbedcf47f6a00efb220ec8bbe0commit-bot@chromium.org     *  fill it out with info about the shader.
3593055879cbf88095a46343736f3f4417761b8afe7commit-bot@chromium.org     *
3603055879cbf88095a46343736f3f4417761b8afe7commit-bot@chromium.org     *  These are bare pointers; the ownership and reference count are unchanged.
361795905562d1bd8bbedcf47f6a00efb220ec8bbe0commit-bot@chromium.org     */
362795905562d1bd8bbedcf47f6a00efb220ec8bbe0commit-bot@chromium.org
363795905562d1bd8bbedcf47f6a00efb220ec8bbe0commit-bot@chromium.org    struct ComposeRec {
364795905562d1bd8bbedcf47f6a00efb220ec8bbe0commit-bot@chromium.org        const SkShader*     fShaderA;
365795905562d1bd8bbedcf47f6a00efb220ec8bbe0commit-bot@chromium.org        const SkShader*     fShaderB;
366795905562d1bd8bbedcf47f6a00efb220ec8bbe0commit-bot@chromium.org        const SkXfermode*   fMode;
367795905562d1bd8bbedcf47f6a00efb220ec8bbe0commit-bot@chromium.org    };
368795905562d1bd8bbedcf47f6a00efb220ec8bbe0commit-bot@chromium.org
369795905562d1bd8bbedcf47f6a00efb220ec8bbe0commit-bot@chromium.org    virtual bool asACompose(ComposeRec* rec) const { return false; }
370795905562d1bd8bbedcf47f6a00efb220ec8bbe0commit-bot@chromium.org
371795905562d1bd8bbedcf47f6a00efb220ec8bbe0commit-bot@chromium.org
372795905562d1bd8bbedcf47f6a00efb220ec8bbe0commit-bot@chromium.org    /**
37383d081ae1d731b5039e99823620f5e287542ee39bsalomon     *  Returns true if the shader subclass succeeds in creating an effect or if none is required.
37483d081ae1d731b5039e99823620f5e287542ee39bsalomon     *  False is returned if it fails or if there is not an implementation of this method in the
37583d081ae1d731b5039e99823620f5e287542ee39bsalomon     *  shader subclass.
37683d081ae1d731b5039e99823620f5e287542ee39bsalomon     *
37783d081ae1d731b5039e99823620f5e287542ee39bsalomon     *  On success an implementation of this method must inspect the SkPaint and set paintColor to
37883d081ae1d731b5039e99823620f5e287542ee39bsalomon     *  the color the effect expects as its input color. If the SkShader wishes to emit a solid
37983d081ae1d731b5039e99823620f5e287542ee39bsalomon     *  color then it should set paintColor to that color and not create an effect. Note that
38083d081ae1d731b5039e99823620f5e287542ee39bsalomon     *  GrColor is always premul. The common patterns are to convert paint's SkColor to GrColor or
38183d081ae1d731b5039e99823620f5e287542ee39bsalomon     *  to extract paint's alpha and replicate it to all channels in paintColor. Upon failure
38283d081ae1d731b5039e99823620f5e287542ee39bsalomon     *  paintColor should not be modified. It is not recommended to specialize the effect to
38383d081ae1d731b5039e99823620f5e287542ee39bsalomon     *  the paint's color as then many GPU shaders may be generated.
38483d081ae1d731b5039e99823620f5e287542ee39bsalomon     *
38583d081ae1d731b5039e99823620f5e287542ee39bsalomon     *  The GrContext may be used by the effect to create textures. The GPU device does not
38687fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org     *  call createContext. Instead we pass the SkPaint here in case the shader needs paint info.
38703c1c359b336ad20d23ab07004cdafafd14c90a5rileya@google.com     */
388b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt    virtual bool asFragmentProcessor(GrContext*, const SkPaint&, const SkMatrix*, GrColor*,
389b0a8a377f832c59cee939ad721e1f87d378b7142joshualitt                                     GrFragmentProcessor**) const;
39003c1c359b336ad20d23ab07004cdafafd14c90a5rileya@google.com
3918367b8cb7a6b2f163ee41365323cb219b7049b15reed    /**
3928367b8cb7a6b2f163ee41365323cb219b7049b15reed     *  If the shader can represent its "average" luminance in a single color, return true and
3938367b8cb7a6b2f163ee41365323cb219b7049b15reed     *  if color is not NULL, return that color. If it cannot, return false and ignore the color
3948367b8cb7a6b2f163ee41365323cb219b7049b15reed     *  parameter.
3958367b8cb7a6b2f163ee41365323cb219b7049b15reed     *
3968367b8cb7a6b2f163ee41365323cb219b7049b15reed     *  Note: if this returns true, the returned color will always be opaque, as only the RGB
3978367b8cb7a6b2f163ee41365323cb219b7049b15reed     *  components are used to compute luminance.
3988367b8cb7a6b2f163ee41365323cb219b7049b15reed     */
3998367b8cb7a6b2f163ee41365323cb219b7049b15reed    bool asLuminanceColor(SkColor*) const;
4008367b8cb7a6b2f163ee41365323cb219b7049b15reed
401795905562d1bd8bbedcf47f6a00efb220ec8bbe0commit-bot@chromium.org#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
402795905562d1bd8bbedcf47f6a00efb220ec8bbe0commit-bot@chromium.org    /**
403795905562d1bd8bbedcf47f6a00efb220ec8bbe0commit-bot@chromium.org     *  If the shader is a custom shader which has data the caller might want, call this function
404795905562d1bd8bbedcf47f6a00efb220ec8bbe0commit-bot@chromium.org     *  to get that data.
405795905562d1bd8bbedcf47f6a00efb220ec8bbe0commit-bot@chromium.org     */
406795905562d1bd8bbedcf47f6a00efb220ec8bbe0commit-bot@chromium.org    virtual bool asACustomShader(void** customData) const { return false; }
407795905562d1bd8bbedcf47f6a00efb220ec8bbe0commit-bot@chromium.org#endif
408795905562d1bd8bbedcf47f6a00efb220ec8bbe0commit-bot@chromium.org
4098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    //////////////////////////////////////////////////////////////////////////
4108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    //  Factory methods for stock shaders
4118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
412ce56d965069c1649afe14319cb239e6ad670682acommit-bot@chromium.org    /**
413ce56d965069c1649afe14319cb239e6ad670682acommit-bot@chromium.org     *  Call this to create a new "empty" shader, that will not draw anything.
414ce56d965069c1649afe14319cb239e6ad670682acommit-bot@chromium.org     */
415ce56d965069c1649afe14319cb239e6ad670682acommit-bot@chromium.org    static SkShader* CreateEmptyShader();
416ce56d965069c1649afe14319cb239e6ad670682acommit-bot@chromium.org
4178367b8cb7a6b2f163ee41365323cb219b7049b15reed    /**
4188367b8cb7a6b2f163ee41365323cb219b7049b15reed     *  Call this to create a new shader that just draws the specified color. This should always
4198367b8cb7a6b2f163ee41365323cb219b7049b15reed     *  draw the same as a paint with this color (and no shader).
4208367b8cb7a6b2f163ee41365323cb219b7049b15reed     */
4218367b8cb7a6b2f163ee41365323cb219b7049b15reed    static SkShader* CreateColorShader(SkColor);
4228367b8cb7a6b2f163ee41365323cb219b7049b15reed
4238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Call this to create a new shader that will draw with the specified bitmap.
42499c114e0ac732ba01705e24d12f5e4dd7e144abdreed@google.com     *
42599c114e0ac732ba01705e24d12f5e4dd7e144abdreed@google.com     *  If the bitmap cannot be used (e.g. has no pixels, or its dimensions
42699c114e0ac732ba01705e24d12f5e4dd7e144abdreed@google.com     *  exceed implementation limits (currently at 64K - 1)) then SkEmptyShader
42799c114e0ac732ba01705e24d12f5e4dd7e144abdreed@google.com     *  may be returned.
42899c114e0ac732ba01705e24d12f5e4dd7e144abdreed@google.com     *
42991246b9fc7740547d4b1b3f144684483c8e968cdcommit-bot@chromium.org     *  If the src is kA8_Config then that mask will be colorized using the color on
43091246b9fc7740547d4b1b3f144684483c8e968cdcommit-bot@chromium.org     *  the paint.
43191246b9fc7740547d4b1b3f144684483c8e968cdcommit-bot@chromium.org     *
43299c114e0ac732ba01705e24d12f5e4dd7e144abdreed@google.com     *  @param src  The bitmap to use inside the shader
43399c114e0ac732ba01705e24d12f5e4dd7e144abdreed@google.com     *  @param tmx  The tiling mode to use when sampling the bitmap in the x-direction.
43499c114e0ac732ba01705e24d12f5e4dd7e144abdreed@google.com     *  @param tmy  The tiling mode to use when sampling the bitmap in the y-direction.
43599c114e0ac732ba01705e24d12f5e4dd7e144abdreed@google.com     *  @return     Returns a new shader object. Note: this function never returns null.
4368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
4378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static SkShader* CreateBitmapShader(const SkBitmap& src,
4389c9005a347e9996f357bd79591bd34f74f8bbc66commit-bot@chromium.org                                        TileMode tmx, TileMode tmy,
4399c9005a347e9996f357bd79591bd34f74f8bbc66commit-bot@chromium.org                                        const SkMatrix* localMatrix = NULL);
4408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
441c5d9bb0f677069f62ec76373b9730e70e7352455commit-bot@chromium.org    /** Call this to create a new shader that will draw with the specified picture.
442c5d9bb0f677069f62ec76373b9730e70e7352455commit-bot@chromium.org     *
443c5d9bb0f677069f62ec76373b9730e70e7352455commit-bot@chromium.org     *  @param src  The picture to use inside the shader (if not NULL, its ref count
444855e88edfafe4b3892e99f932c38fa7433b2fcbecommit-bot@chromium.org     *              is incremented). The SkPicture must not be changed after
445855e88edfafe4b3892e99f932c38fa7433b2fcbecommit-bot@chromium.org     *              successfully creating a picture shader.
446855e88edfafe4b3892e99f932c38fa7433b2fcbecommit-bot@chromium.org     *              FIXME: src cannot be const due to SkCanvas::drawPicture
447c5d9bb0f677069f62ec76373b9730e70e7352455commit-bot@chromium.org     *  @param tmx  The tiling mode to use when sampling the bitmap in the x-direction.
448c5d9bb0f677069f62ec76373b9730e70e7352455commit-bot@chromium.org     *  @param tmy  The tiling mode to use when sampling the bitmap in the y-direction.
449b5f7826c51af2862aebcabe61e1ba684f925e488fmalita     *  @param tile The tile rectangle in picture coordinates: this represents the subset
450b5f7826c51af2862aebcabe61e1ba684f925e488fmalita     *              (or superset) of the picture used when building a tile. It is not
451b5f7826c51af2862aebcabe61e1ba684f925e488fmalita     *              affected by localMatrix and does not imply scaling (only translation
452b5f7826c51af2862aebcabe61e1ba684f925e488fmalita     *              and cropping). If null, the tile rect is considered equal to the picture
453b5f7826c51af2862aebcabe61e1ba684f925e488fmalita     *              bounds.
454c5d9bb0f677069f62ec76373b9730e70e7352455commit-bot@chromium.org     *  @return     Returns a new shader object. Note: this function never returns null.
455c5d9bb0f677069f62ec76373b9730e70e7352455commit-bot@chromium.org    */
456b5f7826c51af2862aebcabe61e1ba684f925e488fmalita    static SkShader* CreatePictureShader(SkPicture* src,
457b5f7826c51af2862aebcabe61e1ba684f925e488fmalita                                         TileMode tmx, TileMode tmy,
458b5f7826c51af2862aebcabe61e1ba684f925e488fmalita                                         const SkMatrix* localMatrix,
459b5f7826c51af2862aebcabe61e1ba684f925e488fmalita                                         const SkRect* tile);
460c5d9bb0f677069f62ec76373b9730e70e7352455commit-bot@chromium.org
4618fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org    /**
4628fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org     *  Return a shader that will apply the specified localMatrix to the proxy shader.
4638fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org     *  The specified matrix will be applied before any matrix associated with the proxy.
4648fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org     *
4658fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org     *  Note: ownership of the proxy is not transferred (though a ref is taken).
4668fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org     */
4678fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org    static SkShader* CreateLocalMatrixShader(SkShader* proxy, const SkMatrix& localMatrix);
4688fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org
4698fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org    /**
4708fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org     *  If this shader can be represented by another shader + a localMatrix, return that shader
4718fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org     *  and, if not NULL, the localMatrix. If not, return NULL and ignore the localMatrix parameter.
4728fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org     *
4738fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org     *  Note: the returned shader (if not NULL) will have been ref'd, and it is the responsibility
4748fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org     *  of the caller to balance that with unref() when they are done.
4758fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org     */
4768fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org    virtual SkShader* refAsALocalMatrixShader(SkMatrix* localMatrix) const;
4778fae213590981b8ca37839a4e3cae1dae4e611fdcommit-bot@chromium.org
4780f10f7bf1fb43ca6346dc220a076773b1f19a367commit-bot@chromium.org    SK_TO_STRING_VIRT()
479c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org    SK_DEFINE_FLATTENABLE_TYPE(SkShader)
480c0b7e10c6a68f59e1653e6c18e6bc954b3c3f0cfcommit-bot@chromium.org
4818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprotected:
4829fa60daad4d5f54c0dbe3dbcc7608a8f6d721187reed#ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING
4838b0e8ac5f582de80356019406e2975079bf0829dcommit-bot@chromium.org    SkShader(SkReadBuffer& );
4849fa60daad4d5f54c0dbe3dbcc7608a8f6d721187reed#endif
4858b0e8ac5f582de80356019406e2975079bf0829dcommit-bot@chromium.org    virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE;
48687fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org
487ce56d965069c1649afe14319cb239e6ad670682acommit-bot@chromium.org    bool computeTotalInverse(const ContextRec&, SkMatrix* totalInverse) const;
488ce56d965069c1649afe14319cb239e6ad670682acommit-bot@chromium.org
489ce56d965069c1649afe14319cb239e6ad670682acommit-bot@chromium.org    /**
490ce56d965069c1649afe14319cb239e6ad670682acommit-bot@chromium.org     *  Your subclass must also override contextSize() if it overrides onCreateContext().
491ce56d965069c1649afe14319cb239e6ad670682acommit-bot@chromium.org     *  Base class impl returns NULL.
492ce56d965069c1649afe14319cb239e6ad670682acommit-bot@chromium.org     */
493ce56d965069c1649afe14319cb239e6ad670682acommit-bot@chromium.org    virtual Context* onCreateContext(const ContextRec&, void* storage) const;
49487fcd950198a16211b3988610beebb5ca5bcf323commit-bot@chromium.org
4958367b8cb7a6b2f163ee41365323cb219b7049b15reed    virtual bool onAsLuminanceColor(SkColor*) const {
4968367b8cb7a6b2f163ee41365323cb219b7049b15reed        return false;
4978367b8cb7a6b2f163ee41365323cb219b7049b15reed    }
498ce56d965069c1649afe14319cb239e6ad670682acommit-bot@chromium.orgprivate:
499ef0fd61dc2ea50ce29b1fe0feed473479b32e42cscroggo    // This is essentially const, but not officially so it can be modified in
500ef0fd61dc2ea50ce29b1fe0feed473479b32e42cscroggo    // constructors.
501ce56d965069c1649afe14319cb239e6ad670682acommit-bot@chromium.org    SkMatrix fLocalMatrix;
502c870d494dd0dc88f3f4155a0d6257a1be8280880scroggo
503c870d494dd0dc88f3f4155a0d6257a1be8280880scroggo    // So the SkLocalMatrixShader can whack fLocalMatrix in its SkReadBuffer constructor.
504c870d494dd0dc88f3f4155a0d6257a1be8280880scroggo    friend class SkLocalMatrixShader;
505c870d494dd0dc88f3f4155a0d6257a1be8280880scroggo
5068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    typedef SkFlattenable INHERITED;
5078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
5088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
5098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
510