blob: 37c0471c22bdc54908e9b7895f56975e8ecb6932 [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).
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.
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
Brian Salomonaff329b2017-08-11 09:40:37 -040052 std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(const AsFPArgs&) 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; }
64 const SkMatrix& getGradientMatrix() const { return fPtsToUnit; }
65 const FocalData& getFocalData() const { return fFocalData; }
66
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +000067 SK_TO_STRING_OVERRIDE()
rileya@google.com589708b2012-07-26 20:04:23 +000068 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkTwoPointConicalGradient)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000069
rileya@google.com589708b2012-07-26 20:04:23 +000070protected:
mtklein36352bf2015-03-25 18:17:31 -070071 void flatten(SkWriteBuffer& buffer) const override;
Matt Sarett6cc6ae752017-04-18 18:29:12 -040072 sk_sp<SkShader> onMakeColorSpace(SkColorSpaceXformer* xformer) const override;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000073
Florin Malita50b20842017-07-29 19:08:28 -040074 void appendGradientStages(SkArenaAlloc* alloc, SkRasterPipeline* tPipeline,
75 SkRasterPipeline* postPipeline) const override;
Florin Malita3d96f062017-06-29 12:53:19 -040076
Mike Reed34042072017-08-08 16:29:22 -040077 bool onIsRasterPipelineOnly(const SkMatrix&) const override { return true; }
Florin Malita0bb04112017-06-27 14:35:50 -040078
rileya@google.com589708b2012-07-26 20:04:23 +000079private:
Florin Malita9c2212f2017-07-29 18:23:10 -040080 SkTwoPointConicalGradient(const SkPoint& c0, SkScalar r0,
81 const SkPoint& c1, SkScalar r1,
Yuqian Lid208a882018-01-04 10:08:42 -050082 const Descriptor&, Type, const SkMatrix&, const FocalData&);
Florin Malita9c2212f2017-07-29 18:23:10 -040083
84 SkPoint fCenter1;
85 SkPoint fCenter2;
commit-bot@chromium.org44d83c12014-04-21 13:10:25 +000086 SkScalar fRadius1;
87 SkScalar fRadius2;
Florin Malita9c2212f2017-07-29 18:23:10 -040088 Type fType;
skia.committer@gmail.comd3b28e82014-04-22 03:05:17 +000089
Yuqian Lid208a882018-01-04 10:08:42 -050090 FocalData fFocalData;
91
reed9fa60da2014-08-21 07:59:51 -070092 friend class SkGradientShader;
commit-bot@chromium.org53783b02014-04-17 21:09:49 +000093 typedef SkGradientShaderBase INHERITED;
rileya@google.com589708b2012-07-26 20:04:23 +000094};
95
rileya@google.com589708b2012-07-26 20:04:23 +000096#endif