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 private: 73 SK_FLATTENABLE_HOOKS(SkTwoPointConicalGradient) 74 75 SkTwoPointConicalGradient(const SkPoint& c0, SkScalar r0, 76 const SkPoint& c1, SkScalar r1, 77 const Descriptor&, Type, const SkMatrix&, const FocalData&); 78 79 SkPoint fCenter1; 80 SkPoint fCenter2; 81 SkScalar fRadius1; 82 SkScalar fRadius2; 83 Type fType; 84 85 FocalData fFocalData; 86 87 friend class SkGradientShader; 88 typedef SkGradientShaderBase INHERITED; 89 }; 90 91 #endif 92