| 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 SkConicalGradient_DEFINED |
| 8 |
#define SkConicalGradient_DEFINED |
| 9 |
|
| 10 |
#include "include/core/SkFlattenable.h" |
| 11 |
#include "include/core/SkPoint.h" |
| 12 |
#include "include/core/SkRefCnt.h" |
| 13 |
#include "include/core/SkScalar.h" |
| 14 |
#include "src/shaders/gradients/SkGradientBaseShader.h" |
| 15 |
|
| 16 |
#include <optional> |
| 17 |
|
| 18 |
class SkArenaAlloc; |
| 19 |
class SkMatrix; |
| 20 |
class SkRasterPipeline; |
| 21 |
class SkReadBuffer; |
| 22 |
class SkShader; |
| 23 |
class SkWriteBuffer; |
| 24 |
|
| 25 |
// Please see https://skia.org/dev/design/conical for how our shader works. |
| 26 |
class SkConicalGradient final : public SkGradientBaseShader { |
| 27 |
public: |
| 28 |
// See https://skia.org/dev/design/conical for what focal data means and how our shader works. |
| 29 |
// We make it public so the GPU shader can also use it. |
| 30 |
struct FocalData { |
| 31 |
SkScalar fR1; // r1 after mapping focal point to (0, 0) |
| 32 |
SkScalar fFocalX; // f |
| 33 |
bool fIsSwapped; // whether we swapped r0, r1 |
| 34 |
|
| 35 |
// The input r0, r1 are the radii when we map centers to {(0, 0), (1, 0)}. |
| 36 |
// We'll post concat matrix with our transformation matrix that maps focal point to (0, 0). |
| 37 |
// Returns true if the set succeeded |
| 38 |
bool set(SkScalar r0, SkScalar r1, SkMatrix* matrix); |
| 39 |
|
| 40 |
// Whether the focal point (0, 0) is on the end circle with center (1, 0) and radius r1. If |
| 41 |
// this is true, it's as if an aircraft is flying at Mach 1 and all circles (soundwaves) |
| 42 |
// will go through the focal point (aircraft). In our previous implementations, this was |
| 43 |
// known as the edge case where the inside circle touches the outside circle (on the focal |
| 44 |
// point). If we were to solve for t bruteforcely using a quadratic equation, this case |
| 45 |
// implies that the quadratic equation degenerates to a linear equation. |
| 46 |
bool isFocalOnCircle() const { return SkScalarNearlyZero(1 - fR1); } |
| 47 |
|
| 48 |
bool isSwapped() const { return fIsSwapped; } |
| 49 |
bool isWellBehaved() const { return !this->isFocalOnCircle() && fR1 > 1; } |
| 50 |
bool isNativelyFocal() const { return SkScalarNearlyZero(fFocalX); } |
| 51 |
}; |
| 52 |
|
| 53 |
enum class Type { kRadial, kStrip, kFocal }; |
| 54 |
|
| 55 |
static std::optional<SkMatrix> MapToUnitX(const SkPoint& startCenter, |
| 56 |
const SkPoint& endCenter); |
| 57 |
|
| 58 |
static sk_sp<SkShader> Create(const SkPoint& start, |
| 59 |
SkScalar startRadius, |
| 60 |
const SkPoint& end, |
| 61 |
SkScalar endRadius, |
| 62 |
const SkGradient&, |
| 63 |
const SkMatrix* localMatrix); |
| 64 |
|
| 65 |
GradientType asGradient(GradientInfo* info, SkMatrix* localMatrix) const override; |
| 66 |
bool isOpaque() const override; |
| 67 |
|
| 68 |
SkScalar getCenterX1() const { return SkPoint::Distance(fCenter1, fCenter2); } |
| 69 |
SkScalar getStartRadius() const { return fRadius1; } |
| 70 |
SkScalar getDiffRadius() const { return fRadius2 - fRadius1; } |
| 71 |
const SkPoint& getStartCenter() const { return fCenter1; } |
| 72 |
const SkPoint& getEndCenter() const { return fCenter2; } |
| 73 |
SkScalar getEndRadius() const { return fRadius2; } |
| 74 |
|
| 75 |
Type getType() const { return fType; } |
| 76 |
const FocalData& getFocalData() const { return fFocalData; } |
| 77 |
|
| 78 |
SkConicalGradient(const SkPoint& c0, |
| 79 |
SkScalar r0, |
| 80 |
const SkPoint& c1, |
| 81 |
SkScalar r1, |
| 82 |
const SkGradient&, |
| 83 |
Type, |
| 84 |
const SkMatrix&, |
| 85 |
const FocalData&); |
| 86 |
|
| 87 |
protected: |
| 88 |
void flatten(SkWriteBuffer& buffer) const override; |
| 89 |
|
| 90 |
void appendGradientStages(SkArenaAlloc* alloc, |
| 91 |
SkRasterPipeline* tPipeline, |
| 92 |
SkRasterPipeline* postPipeline) const override; |
| 93 |
|
| 94 |
private: |
| 95 |
friend void ::SkRegisterConicalGradientShaderFlattenable(); |
| 96 |
SK_FLATTENABLE_HOOKS(SkConicalGradient) |
| 97 |
|
| 98 |
SkPoint fCenter1; |
| 99 |
SkPoint fCenter2; |
| 100 |
SkScalar fRadius1; |
| 101 |
SkScalar fRadius2; |
| 102 |
Type fType; |
| 103 |
|
| 104 |
FocalData fFocalData; |
| 105 |
}; |
| 106 |
|
| 107 |
#endif |