blob: f764c0185e4c972e813c77dbfe3f46943054f7ff [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
Matt Sarett6cc6ae752017-04-18 18:29:12 -040011#include "SkColorSpaceXformer.h"
rileya@google.com589708b2012-07-26 20:04:23 +000012#include "SkGradientShaderPriv.h"
13
Florin Malita3d96f062017-06-29 12:53:19 -040014class SkTwoPointConicalGradient final : public SkGradientShaderBase {
rileya@google.com589708b2012-07-26 20:04:23 +000015public:
Yuqian Lid208a882018-01-04 10:08:42 -050016 // 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).
Mike Reeda2f14de2018-05-09 14:01:00 -040025 // Returns true if the set succeeded
26 bool set(SkScalar r0, SkScalar r1, SkMatrix* matrix);
Yuqian Lid208a882018-01-04 10:08:42 -050027
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.
34 bool isFocalOnCircle() const { return SkScalarNearlyZero(1 - fR1); }
35
36 bool isSwapped() const { return fIsSwapped; }
37 bool isWellBehaved() const { return !this->isFocalOnCircle() && fR1 > 1; }
38 bool isNativelyFocal() const { return SkScalarNearlyZero(fFocalX); }
39 };
40
Yuqian Li33a86562018-01-17 12:05:20 -050041 enum class Type {
42 kRadial,
43 kStrip,
44 kFocal
45 };
46
Florin Malita9c2212f2017-07-29 18:23:10 -040047 static sk_sp<SkShader> Create(const SkPoint& start, SkScalar startRadius,
48 const SkPoint& end, SkScalar endRadius,
Florin Malita5f379a82017-10-18 16:22:35 -040049 const Descriptor&);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000050
mtklein36352bf2015-03-25 18:17:31 -070051 SkShader::GradientType asAGradient(GradientInfo* info) const override;
bsalomonc21b09e2015-08-28 18:46:56 -070052#if SK_SUPPORT_GPU
Mike Reede3429e62018-01-19 11:43:34 -050053 std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(const GrFPArgs&) const override;
bsalomonc21b09e2015-08-28 18:46:56 -070054#endif
mtklein36352bf2015-03-25 18:17:31 -070055 bool isOpaque() const override;
rileya@google.com589708b2012-07-26 20:04:23 +000056
rileya@google.com1c6d64b2012-07-27 15:49:05 +000057 SkScalar getCenterX1() const { return SkPoint::Distance(fCenter1, fCenter2); }
58 SkScalar getStartRadius() const { return fRadius1; }
59 SkScalar getDiffRadius() const { return fRadius2 - fRadius1; }
commit-bot@chromium.org2af1a2d2014-04-04 13:50:50 +000060 const SkPoint& getStartCenter() const { return fCenter1; }
61 const SkPoint& getEndCenter() const { return fCenter2; }
62 SkScalar getEndRadius() const { return fRadius2; }
rileya@google.com1c6d64b2012-07-27 15:49:05 +000063
Yuqian Li33a86562018-01-17 12:05:20 -050064 Type getType() const { return fType; }
Yuqian Li33a86562018-01-17 12:05:20 -050065 const FocalData& getFocalData() const { return fFocalData; }
66
rileya@google.com589708b2012-07-26 20:04:23 +000067protected:
mtklein36352bf2015-03-25 18:17:31 -070068 void flatten(SkWriteBuffer& buffer) const override;
Matt Sarett6cc6ae752017-04-18 18:29:12 -040069 sk_sp<SkShader> onMakeColorSpace(SkColorSpaceXformer* xformer) const override;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000070
Florin Malita50b20842017-07-29 19:08:28 -040071 void appendGradientStages(SkArenaAlloc* alloc, SkRasterPipeline* tPipeline,
72 SkRasterPipeline* postPipeline) const override;
Florin Malita3d96f062017-06-29 12:53:19 -040073
rileya@google.com589708b2012-07-26 20:04:23 +000074private:
Mike Klein4fee3232018-10-18 17:27:16 -040075 SK_FLATTENABLE_HOOKS(SkTwoPointConicalGradient)
76
Florin Malita9c2212f2017-07-29 18:23:10 -040077 SkTwoPointConicalGradient(const SkPoint& c0, SkScalar r0,
78 const SkPoint& c1, SkScalar r1,
Yuqian Lid208a882018-01-04 10:08:42 -050079 const Descriptor&, Type, const SkMatrix&, const FocalData&);
Florin Malita9c2212f2017-07-29 18:23:10 -040080
81 SkPoint fCenter1;
82 SkPoint fCenter2;
commit-bot@chromium.org44d83c12014-04-21 13:10:25 +000083 SkScalar fRadius1;
84 SkScalar fRadius2;
Florin Malita9c2212f2017-07-29 18:23:10 -040085 Type fType;
skia.committer@gmail.comd3b28e82014-04-22 03:05:17 +000086
Yuqian Lid208a882018-01-04 10:08:42 -050087 FocalData fFocalData;
88
reed9fa60da2014-08-21 07:59:51 -070089 friend class SkGradientShader;
commit-bot@chromium.org53783b02014-04-17 21:09:49 +000090 typedef SkGradientShaderBase INHERITED;
rileya@google.com589708b2012-07-26 20:04:23 +000091};
92
rileya@google.com589708b2012-07-26 20:04:23 +000093#endif