• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 SkTwoPointConicalGradient_DEFINED
9 #define SkTwoPointConicalGradient_DEFINED
10 
11 #include "src/shaders/gradients/SkGradientShaderPriv.h"
12 
13 class SkTwoPointConicalGradient final : public SkGradientShaderBase {
14 public:
15     // See https://skia.org/dev/design/conical for what focal data means and how our shader works.
16     // We make it public so the GPU shader can also use it.
17     struct FocalData {
18         SkScalar    fR1;        // r1 after mapping focal point to (0, 0)
19         SkScalar    fFocalX;    // f
20         bool        fIsSwapped; // whether we swapped r0, r1
21 
22         // The input r0, r1 are the radii when we map centers to {(0, 0), (1, 0)}.
23         // We'll post concat matrix with our transformation matrix that maps focal point to (0, 0).
24         // Returns true if the set succeeded
25         bool set(SkScalar r0, SkScalar r1, SkMatrix* matrix);
26 
27         // Whether the focal point (0, 0) is on the end circle with center (1, 0) and radius r1. If
28         // this is true, it's as if an aircraft is flying at Mach 1 and all circles (soundwaves)
29         // will go through the focal point (aircraft). In our previous implementations, this was
30         // known as the edge case where the inside circle touches the outside circle (on the focal
31         // point). If we were to solve for t bruteforcely using a quadratic equation, this case
32         // implies that the quadratic equation degenerates to a linear equation.
isFocalOnCircleFocalData33         bool isFocalOnCircle() const { return SkScalarNearlyZero(1 - fR1); }
34 
isSwappedFocalData35         bool isSwapped() const { return fIsSwapped; }
isWellBehavedFocalData36         bool isWellBehaved() const { return !this->isFocalOnCircle() && fR1 > 1; }
isNativelyFocalFocalData37         bool isNativelyFocal() const { return SkScalarNearlyZero(fFocalX); }
38     };
39 
40     enum class Type {
41         kRadial,
42         kStrip,
43         kFocal
44     };
45 
46     static sk_sp<SkShader> Create(const SkPoint& start, SkScalar startRadius,
47                                   const SkPoint& end, SkScalar endRadius,
48                                   const Descriptor&);
49 
50     SkShader::GradientType asAGradient(GradientInfo* info) const  override;
51 #if SK_SUPPORT_GPU
52     std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(const GrFPArgs&) const override;
53 #endif
54     bool isOpaque() const override;
55 
getCenterX1()56     SkScalar getCenterX1() const { return SkPoint::Distance(fCenter1, fCenter2); }
getStartRadius()57     SkScalar getStartRadius() const { return fRadius1; }
getDiffRadius()58     SkScalar getDiffRadius() const { return fRadius2 - fRadius1; }
getStartCenter()59     const SkPoint& getStartCenter() const { return fCenter1; }
getEndCenter()60     const SkPoint& getEndCenter() const { return fCenter2; }
getEndRadius()61     SkScalar getEndRadius() const { return fRadius2; }
62 
getType()63     Type getType() const { return fType; }
getFocalData()64     const FocalData& getFocalData() const { return fFocalData; }
65 
66 protected:
67     void flatten(SkWriteBuffer& buffer) const override;
68 
69     void appendGradientStages(SkArenaAlloc* alloc, SkRasterPipeline* tPipeline,
70                               SkRasterPipeline* postPipeline) const override;
71 
72     skvm::F32 transformT(skvm::Builder*, skvm::Uniforms*,
73                          skvm::F32 x, skvm::F32 y, skvm::I32* mask) const final;
74 
75 private:
76     SK_FLATTENABLE_HOOKS(SkTwoPointConicalGradient)
77 
78     SkTwoPointConicalGradient(const SkPoint& c0, SkScalar r0,
79                               const SkPoint& c1, SkScalar r1,
80                               const Descriptor&, Type, const SkMatrix&, const FocalData&);
81 
82     SkPoint  fCenter1;
83     SkPoint  fCenter2;
84     SkScalar fRadius1;
85     SkScalar fRadius2;
86     Type     fType;
87 
88     FocalData fFocalData;
89 
90     friend class SkGradientShader;
91     typedef SkGradientShaderBase INHERITED;
92 };
93 
94 #endif
95