SkColorShader.h revision cc9dbfb1d21098c267d8b398b51a1db95e5099b5
1
2/*
3 * Copyright 2007 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10#ifndef SkColorShader_DEFINED
11#define SkColorShader_DEFINED
12
13#include "SkShader.h"
14
15/** \class SkColorShader
16    A Shader that represents a single color. In general, this effect can be
17    accomplished by just using the color field on the paint, but if an
18    actual shader object is needed, this provides that feature.
19*/
20class SK_API SkColorShader : public SkShader {
21public:
22    /** Create a ColorShader that ignores the color in the paint, and uses the
23        specified color. Note: like all shaders, at draw time the paint's alpha
24        will be respected, and is applied to the specified color.
25    */
26    explicit SkColorShader(SkColor c);
27
28    virtual bool isOpaque() const SK_OVERRIDE;
29
30    virtual SkShader::Context* createContext(const SkBitmap& device,
31                                             const SkPaint& paint,
32                                             const SkMatrix& matrix,
33                                             void* storage) const SK_OVERRIDE;
34
35    virtual size_t contextSize() const SK_OVERRIDE {
36        return sizeof(ColorShaderContext);
37    }
38
39    class ColorShaderContext : public SkShader::Context {
40    public:
41        ColorShaderContext(const SkColorShader& shader, const SkBitmap& device,
42                           const SkPaint& paint, const SkMatrix& matrix);
43
44        virtual uint32_t getFlags() const SK_OVERRIDE;
45        virtual uint8_t getSpan16Alpha() const SK_OVERRIDE;
46        virtual void shadeSpan(int x, int y, SkPMColor span[], int count) SK_OVERRIDE;
47        virtual void shadeSpan16(int x, int y, uint16_t span[], int count) SK_OVERRIDE;
48        virtual void shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) SK_OVERRIDE;
49
50    private:
51        SkPMColor   fPMColor;
52        uint32_t    fFlags;
53        uint16_t    fColor16;
54
55        typedef SkShader::Context INHERITED;
56    };
57
58    // we return false for this, use asAGradient
59    virtual BitmapType asABitmap(SkBitmap* outTexture,
60                                 SkMatrix* outMatrix,
61                                 TileMode xy[2]) const SK_OVERRIDE;
62
63    virtual GradientType asAGradient(GradientInfo* info) const SK_OVERRIDE;
64
65    SK_TO_STRING_OVERRIDE()
66    SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkColorShader)
67
68protected:
69    SkColorShader(SkReadBuffer&);
70    virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE;
71
72private:
73    SkColor     fColor;         // ignored if fInheritColor is true
74
75    typedef SkShader INHERITED;
76};
77
78#endif
79