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 "src/shaders/SkShaderBase.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 | */ |
18 | class SkColorShader : public SkShaderBase { |
19 | public: |
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 | bool isOpaque() const override; |
27 | bool isConstant() const override { return true; } |
28 | |
29 | GradientType asAGradient(GradientInfo* info) const override; |
30 | |
31 | #if SK_SUPPORT_GPU |
32 | std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(const GrFPArgs&) const override; |
33 | #endif |
34 | |
35 | private: |
36 | SK_FLATTENABLE_HOOKS(SkColorShader) |
37 | |
38 | void flatten(SkWriteBuffer&) const override; |
39 | |
40 | bool onAsLuminanceColor(SkColor* lum) const override { |
41 | *lum = fColor; |
42 | return true; |
43 | } |
44 | |
45 | bool onAppendStages(const SkStageRec&) const override; |
46 | |
47 | skvm::Color onProgram(skvm::Builder*, skvm::F32 x, skvm::F32 y, skvm::Color paint, |
48 | const SkMatrix& ctm, const SkMatrix* localM, |
49 | SkFilterQuality quality, const SkColorInfo& dst, |
50 | skvm::Uniforms* uniforms, SkArenaAlloc*) const override; |
51 | |
52 | SkColor fColor; |
53 | }; |
54 | |
55 | class SkColor4Shader : public SkShaderBase { |
56 | public: |
57 | SkColor4Shader(const SkColor4f&, sk_sp<SkColorSpace>); |
58 | |
59 | bool isOpaque() const override { return fColor.isOpaque(); } |
60 | bool isConstant() const override { return true; } |
61 | |
62 | #if SK_SUPPORT_GPU |
63 | std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(const GrFPArgs&) const override; |
64 | #endif |
65 | |
66 | private: |
67 | SK_FLATTENABLE_HOOKS(SkColor4Shader) |
68 | |
69 | void flatten(SkWriteBuffer&) const override; |
70 | bool onAppendStages(const SkStageRec&) const override; |
71 | |
72 | skvm::Color onProgram(skvm::Builder*, skvm::F32 x, skvm::F32 y, skvm::Color paint, |
73 | const SkMatrix& ctm, const SkMatrix* localM, |
74 | SkFilterQuality quality, const SkColorInfo& dst, |
75 | skvm::Uniforms* uniforms, SkArenaAlloc*) const override; |
76 | |
77 | sk_sp<SkColorSpace> fColorSpace; |
78 | const SkColor4f fColor; |
79 | }; |
80 | |
81 | #endif |
82 | |