blob: 39d3b1134b9233f7ca712282c87b5ada8e43f93d [file] [log] [blame]
rileya@google.com589708b2012-07-26 20:04:23 +00001/*
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
commit-bot@chromium.orgaa64fbf2014-04-03 14:59:19 +00008#ifndef SkTwoPointConicalGradient_DEFINED
9#define SkTwoPointConicalGradient_DEFINED
rileya@google.com589708b2012-07-26 20:04:23 +000010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/shaders/gradients/SkGradientShaderPriv.h"
rileya@google.com589708b2012-07-26 20:04:23 +000012
Florin Malita3d96f062017-06-29 12:53:19 -040013class SkTwoPointConicalGradient final : public SkGradientShaderBase {
rileya@google.com589708b2012-07-26 20:04:23 +000014public:
Yuqian Lid208a882018-01-04 10:08:42 -050015 // 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).
Mike Reeda2f14de2018-05-09 14:01:00 -040024 // Returns true if the set succeeded
25 bool set(SkScalar r0, SkScalar r1, SkMatrix* matrix);
Yuqian Lid208a882018-01-04 10:08:42 -050026
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.
33 bool isFocalOnCircle() const { return SkScalarNearlyZero(1 - fR1); }
34
35 bool isSwapped() const { return fIsSwapped; }
36 bool isWellBehaved() const { return !this->isFocalOnCircle() && fR1 > 1; }
37 bool isNativelyFocal() const { return SkScalarNearlyZero(fFocalX); }
38 };
39
Yuqian Li33a86562018-01-17 12:05:20 -050040 enum class Type {
41 kRadial,
42 kStrip,
43 kFocal
44 };
45
Florin Malita9c2212f2017-07-29 18:23:10 -040046 static sk_sp<SkShader> Create(const SkPoint& start, SkScalar startRadius,
47 const SkPoint& end, SkScalar endRadius,
Florin Malita5f379a82017-10-18 16:22:35 -040048 const Descriptor&);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000049
mtklein36352bf2015-03-25 18:17:31 -070050 SkShader::GradientType asAGradient(GradientInfo* info) const override;
bsalomonc21b09e2015-08-28 18:46:56 -070051#if SK_SUPPORT_GPU
Mike Reede3429e62018-01-19 11:43:34 -050052 std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(const GrFPArgs&) const override;
bsalomonc21b09e2015-08-28 18:46:56 -070053#endif
mtklein36352bf2015-03-25 18:17:31 -070054 bool isOpaque() const override;
rileya@google.com589708b2012-07-26 20:04:23 +000055
rileya@google.com1c6d64b2012-07-27 15:49:05 +000056 SkScalar getCenterX1() const { return SkPoint::Distance(fCenter1, fCenter2); }
57 SkScalar getStartRadius() const { return fRadius1; }
58 SkScalar getDiffRadius() const { return fRadius2 - fRadius1; }
commit-bot@chromium.org2af1a2d2014-04-04 13:50:50 +000059 const SkPoint& getStartCenter() const { return fCenter1; }
60 const SkPoint& getEndCenter() const { return fCenter2; }
61 SkScalar getEndRadius() const { return fRadius2; }
rileya@google.com1c6d64b2012-07-27 15:49:05 +000062
Yuqian Li33a86562018-01-17 12:05:20 -050063 Type getType() const { return fType; }
Yuqian Li33a86562018-01-17 12:05:20 -050064 const FocalData& getFocalData() const { return fFocalData; }
65
rileya@google.com589708b2012-07-26 20:04:23 +000066protected:
mtklein36352bf2015-03-25 18:17:31 -070067 void flatten(SkWriteBuffer& buffer) const override;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000068
Florin Malita50b20842017-07-29 19:08:28 -040069 void appendGradientStages(SkArenaAlloc* alloc, SkRasterPipeline* tPipeline,
70 SkRasterPipeline* postPipeline) const override;
Florin Malita3d96f062017-06-29 12:53:19 -040071
Mike Kleince9e0602020-01-29 09:47:44 -060072 skvm::F32 transformT(skvm::Builder*, skvm::Uniforms*,
Brian Osman5aaaeea2020-06-22 14:26:03 -040073 skvm::Coord coord, skvm::I32* mask) const final;
Mike Kleincaf5ee42020-01-28 16:11:34 -060074
rileya@google.com589708b2012-07-26 20:04:23 +000075private:
Mike Klein4fee3232018-10-18 17:27:16 -040076 SK_FLATTENABLE_HOOKS(SkTwoPointConicalGradient)
77
Florin Malita9c2212f2017-07-29 18:23:10 -040078 SkTwoPointConicalGradient(const SkPoint& c0, SkScalar r0,
79 const SkPoint& c1, SkScalar r1,
Yuqian Lid208a882018-01-04 10:08:42 -050080 const Descriptor&, Type, const SkMatrix&, const FocalData&);
Florin Malita9c2212f2017-07-29 18:23:10 -040081
82 SkPoint fCenter1;
83 SkPoint fCenter2;
commit-bot@chromium.org44d83c12014-04-21 13:10:25 +000084 SkScalar fRadius1;
85 SkScalar fRadius2;
Florin Malita9c2212f2017-07-29 18:23:10 -040086 Type fType;
skia.committer@gmail.comd3b28e82014-04-22 03:05:17 +000087
Yuqian Lid208a882018-01-04 10:08:42 -050088 FocalData fFocalData;
89
reed9fa60da2014-08-21 07:59:51 -070090 friend class SkGradientShader;
John Stiles7571f9e2020-09-02 22:42:33 -040091 using INHERITED = SkGradientShaderBase;
rileya@google.com589708b2012-07-26 20:04:23 +000092};
93
rileya@google.com589708b2012-07-26 20:04:23 +000094#endif