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