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 void 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; } getGradientMatrix()64 const SkMatrix& getGradientMatrix() const { return fPtsToUnit; } getFocalData()65 const FocalData& getFocalData() const { return fFocalData; } 66 67 SK_TO_STRING_OVERRIDE() 68 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkTwoPointConicalGradient) 69 70 protected: 71 void flatten(SkWriteBuffer& buffer) const override; 72 sk_sp<SkShader> onMakeColorSpace(SkColorSpaceXformer* xformer) const override; 73 74 void appendGradientStages(SkArenaAlloc* alloc, SkRasterPipeline* tPipeline, 75 SkRasterPipeline* postPipeline) const override; 76 onIsRasterPipelineOnly(const SkMatrix &)77 bool onIsRasterPipelineOnly(const SkMatrix&) const override { return true; } 78 79 private: 80 SkTwoPointConicalGradient(const SkPoint& c0, SkScalar r0, 81 const SkPoint& c1, SkScalar r1, 82 const Descriptor&, Type, const SkMatrix&, const FocalData&); 83 84 SkPoint fCenter1; 85 SkPoint fCenter2; 86 SkScalar fRadius1; 87 SkScalar fRadius2; 88 Type fType; 89 90 FocalData fFocalData; 91 92 friend class SkGradientShader; 93 typedef SkGradientShaderBase INHERITED; 94 }; 95 96 #endif 97