SkTwoPointConicalGradient.h 3.5 KB

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