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 #include "SkPM4f.h"
13 
14 /** \class SkColorShader
15     A Shader that represents a single color. In general, this effect can be
16     accomplished by just using the color field on the paint, but if an
17     actual shader object is needed, this provides that feature.
18 */
19 class SK_API SkColorShader : public SkShader {
20 public:
21     /** Create a ColorShader that ignores the color in the paint, and uses the
22         specified color. Note: like all shaders, at draw time the paint's alpha
23         will be respected, and is applied to the specified color.
24     */
25     explicit SkColorShader(SkColor c);
26 
27     bool isOpaque() const override;
28 
contextSize(const ContextRec &)29     size_t contextSize(const ContextRec&) const override {
30         return sizeof(ColorShaderContext);
31     }
32 
33     class ColorShaderContext : public SkShader::Context {
34     public:
35         ColorShaderContext(const SkColorShader& shader, const ContextRec&);
36 
37         uint32_t getFlags() const override;
38         void shadeSpan(int x, int y, SkPMColor span[], int count) override;
39         void shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) override;
40         void shadeSpan4f(int x, int y, SkPM4f[], int count) override;
41 
42     private:
43         SkPM4f      fPM4f;
44         SkPMColor   fPMColor;
45         uint32_t    fFlags;
46 
47         typedef SkShader::Context INHERITED;
48     };
49 
50     GradientType asAGradient(GradientInfo* info) const override;
51 
52 #if SK_SUPPORT_GPU
53     const GrFragmentProcessor* asFragmentProcessor(GrContext*, const SkMatrix& viewM,
54                                                    const SkMatrix*, SkFilterQuality) const override;
55 #endif
56 
57     SK_TO_STRING_OVERRIDE()
58     SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkColorShader)
59 
60 protected:
61     SkColorShader(SkReadBuffer&);
62     void flatten(SkWriteBuffer&) const override;
63     Context* onCreateContext(const ContextRec&, void* storage) const override;
onAsLuminanceColor(SkColor * lum)64     bool onAsLuminanceColor(SkColor* lum) const override {
65         *lum = fColor;
66         return true;
67     }
68 
69 private:
70     SkColor fColor;
71 
72     typedef SkShader INHERITED;
73 };
74 
75 #endif
76