SkColorShader.h revision 7ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577
1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef SkColorShader_DEFINED
18#define SkColorShader_DEFINED
19
20#include "SkShader.h"
21
22/** \class SkColorShader
23    A Shader that represents a single color. In general, this effect can be
24    accomplished by just using the color field on the paint, but if an
25    actual shader object is needed, this provides that feature.
26*/
27class SK_API SkColorShader : public SkShader {
28public:
29    /** Create a ColorShader that will inherit its color from the Paint
30        at draw time.
31    */
32    SkColorShader();
33
34    /** Create a ColorShader that ignores the color in the paint, and uses the
35        specified color. Note: like all shaders, at draw time the paint's alpha
36        will be respected, and is applied to the specified color.
37    */
38    SkColorShader(SkColor c);
39
40    virtual ~SkColorShader();
41
42    virtual uint32_t getFlags() { return fFlags; }
43    virtual uint8_t getSpan16Alpha() const;
44    virtual bool setContext(const SkBitmap& device, const SkPaint& paint,
45                            const SkMatrix& matrix);
46    virtual void shadeSpan(int x, int y, SkPMColor span[], int count);
47    virtual void shadeSpan16(int x, int y, uint16_t span[], int count);
48    virtual void shadeSpanAlpha(int x, int y, uint8_t alpha[], int count);
49
50    virtual BitmapType asABitmap(SkBitmap* outTexture,
51                                 SkMatrix* outMatrix,
52                                 TileMode xy[2],
53                                 SkScalar* twoPointRadialParams) const;
54
55    virtual GradientType asAGradient(GradientInfo* info) const;
56
57protected:
58    SkColorShader(SkFlattenableReadBuffer& );
59    virtual void flatten(SkFlattenableWriteBuffer& );
60    virtual Factory getFactory() { return CreateProc; }
61private:
62    static SkFlattenable* CreateProc(SkFlattenableReadBuffer& buffer) {
63        return SkNEW_ARGS(SkColorShader, (buffer));
64    }
65    SkColor     fColor;         // ignored if fInheritColor is true
66    SkPMColor   fPMColor;       // cached after setContext()
67    uint32_t    fFlags;         // cached after setContext()
68    uint16_t    fColor16;       // cached after setContext()
69    SkBool8     fInheritColor;
70
71    // deferred allocation, used for asABitmap()
72    mutable SkPixelRef* fAsABitmapPixelRef;
73
74    typedef SkShader INHERITED;
75};
76
77#endif
78