1 
2 /*
3  * Copyright 2012 Google Inc.
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 #ifndef SkTwoPointConicalGradient_DEFINED
10 #define SkTwoPointConicalGradient_DEFINED
11 
12 #include "SkGradientShaderPriv.h"
13 
14 // TODO(dominikg): Worth making it truly immutable (i.e. set values in constructor)?
15 // Should only be initialized once via init(). Immutable afterwards.
16 struct TwoPtRadial {
17     enum {
18         // This value is outside the range SK_FixedMin to SK_FixedMax.
19         kDontDrawT  = 0x80000000
20     };
21 
22     float   fCenterX, fCenterY;
23     float   fDCenterX, fDCenterY;
24     float   fRadius;
25     float   fDRadius;
26     float   fA;
27     float   fRadius2;
28     float   fRDR;
29     bool    fFlipped;
30 
31     void init(const SkPoint& center0, SkScalar rad0,
32               const SkPoint& center1, SkScalar rad1,
33               bool flipped);
34 
DontDrawTTwoPtRadial35     static bool DontDrawT(SkFixed t) {
36         return kDontDrawT == (uint32_t)t;
37     }
38 };
39 
40 
41 class SkTwoPointConicalGradient : public SkGradientShaderBase {
42     TwoPtRadial fRec;
43 public:
44     SkTwoPointConicalGradient(const SkPoint& start, SkScalar startRadius,
45                               const SkPoint& end, SkScalar endRadius,
46                               bool flippedGrad, const Descriptor&);
47 
48 
49     size_t contextSize(const ContextRec&) const override;
50 
51     class TwoPointConicalGradientContext : public SkGradientShaderBase::GradientShaderBaseContext {
52     public:
53         TwoPointConicalGradientContext(const SkTwoPointConicalGradient&, const ContextRec&);
~TwoPointConicalGradientContext()54         ~TwoPointConicalGradientContext() {}
55 
56         void shadeSpan(int x, int y, SkPMColor dstC[], int count) override;
57 
58     private:
59         typedef SkGradientShaderBase::GradientShaderBaseContext INHERITED;
60     };
61 
62     SkShader::GradientType asAGradient(GradientInfo* info) const  override;
63 #if SK_SUPPORT_GPU
64     const GrFragmentProcessor* asFragmentProcessor(GrContext*,
65                                                    const SkMatrix&,
66                                                    const SkMatrix*,
67                                                    SkFilterQuality) const override;
68 #endif
69     bool isOpaque() const override;
70 
getCenterX1()71     SkScalar getCenterX1() const { return SkPoint::Distance(fCenter1, fCenter2); }
getStartRadius()72     SkScalar getStartRadius() const { return fRadius1; }
getDiffRadius()73     SkScalar getDiffRadius() const { return fRadius2 - fRadius1; }
getStartCenter()74     const SkPoint& getStartCenter() const { return fCenter1; }
getEndCenter()75     const SkPoint& getEndCenter() const { return fCenter2; }
getEndRadius()76     SkScalar getEndRadius() const { return fRadius2; }
isFlippedGrad()77     bool isFlippedGrad() const { return fFlippedGrad; }
78 
79     SK_TO_STRING_OVERRIDE()
80     SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkTwoPointConicalGradient)
81 
82 protected:
83     SkTwoPointConicalGradient(SkReadBuffer& buffer);
84     void flatten(SkWriteBuffer& buffer) const override;
85     Context* onCreateContext(const ContextRec&, void* storage) const override;
86 
87 private:
88     SkPoint fCenter1;
89     SkPoint fCenter2;
90     SkScalar fRadius1;
91     SkScalar fRadius2;
92     bool fFlippedGrad;
93 
94     friend class SkGradientShader;
95     typedef SkGradientShaderBase INHERITED;
96 };
97 
98 #endif
99