1 /*
2  * Copyright 2012 Google Inc.
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 SkLinearGradient_DEFINED
9 #define SkLinearGradient_DEFINED
10 
11 #include "SkGradientShaderPriv.h"
12 #include "SkNx.h"
13 
14 struct Sk4fStorage {
15     float fArray[4];
16 
Sk4fSk4fStorage17     operator Sk4f() const {
18         return Sk4f::Load(fArray);
19     }
20 
21     Sk4fStorage& operator=(const Sk4f& src) {
22         src.store(fArray);
23         return *this;
24     }
25 };
26 
27 class SkLinearGradient : public SkGradientShaderBase {
28 public:
29     enum {
30         // Temp flag for testing the 4f impl.
31         kForce4fContext_PrivateFlag     = 1 << 7,
32     };
33 
34     SkLinearGradient(const SkPoint pts[2], const Descriptor&);
35 
36     size_t contextSize(const ContextRec&) const override;
37 
38     class LinearGradientContext : public SkGradientShaderBase::GradientShaderBaseContext {
39     public:
40         LinearGradientContext(const SkLinearGradient&, const ContextRec&);
41 
42         void shadeSpan(int x, int y, SkPMColor dstC[], int count) override;
43 
44         struct Rec {
45             Sk4fStorage fColor;
46             float       fPos;
47             float       fPosScale;
48         };
49     private:
50         SkTDArray<Rec>  fRecs;
51         bool            fApplyAlphaAfterInterp;
52 
53         void shade4_clamp(int x, int y, SkPMColor dstC[], int count);
54         template <bool, bool> void shade4_dx_clamp(SkPMColor dstC[], int count, float fx, float dx,
55                                                    float invDx, const float dither[2]);
56 
57         typedef SkGradientShaderBase::GradientShaderBaseContext INHERITED;
58     };
59 
60     GradientType asAGradient(GradientInfo* info) const override;
61 #if SK_SUPPORT_GPU
62     const GrFragmentProcessor* asFragmentProcessor(GrContext*,
63                                                    const SkMatrix& viewM,
64                                                    const SkMatrix*,
65                                                    SkFilterQuality) const override;
66 #endif
67 
68     SK_TO_STRING_OVERRIDE()
69     SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkLinearGradient)
70 
71 protected:
72     SkLinearGradient(SkReadBuffer& buffer);
73     void flatten(SkWriteBuffer& buffer) const override;
74     Context* onCreateContext(const ContextRec&, void* storage) const override;
75 
76 private:
77     class LinearGradient4fContext;
78 
79     friend class SkGradientShader;
80     typedef SkGradientShaderBase INHERITED;
81     const SkPoint fStart;
82     const SkPoint fEnd;
83 };
84 
85 #endif
86