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