rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2012 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | #ifndef SkTwoPointConicalGradient_DEFINED |
| 10 | #define SkTwoPointConicalGradient_DEFINED |
| 11 | |
| 12 | #include "SkGradientShaderPriv.h" |
| 13 | |
| 14 | struct TwoPtRadial { |
| 15 | enum { |
| 16 | kDontDrawT = 0x80000000 |
| 17 | }; |
| 18 | |
| 19 | float fCenterX, fCenterY; |
| 20 | float fDCenterX, fDCenterY; |
| 21 | float fRadius; |
| 22 | float fDRadius; |
| 23 | float fA; |
| 24 | float fRadius2; |
| 25 | float fRDR; |
| 26 | |
| 27 | void init(const SkPoint& center0, SkScalar rad0, |
| 28 | const SkPoint& center1, SkScalar rad1); |
| 29 | |
| 30 | // used by setup and nextT |
| 31 | float fRelX, fRelY, fIncX, fIncY; |
| 32 | float fB, fDB; |
| 33 | |
| 34 | void setup(SkScalar fx, SkScalar fy, SkScalar dfx, SkScalar dfy); |
| 35 | SkFixed nextT(); |
| 36 | |
| 37 | static bool DontDrawT(SkFixed t) { |
| 38 | return kDontDrawT == (uint32_t)t; |
| 39 | } |
| 40 | }; |
| 41 | |
| 42 | |
| 43 | class SkTwoPointConicalGradient : public SkGradientShaderBase { |
| 44 | TwoPtRadial fRec; |
| 45 | void init(); |
| 46 | |
| 47 | public: |
| 48 | SkTwoPointConicalGradient(const SkPoint& start, SkScalar startRadius, |
| 49 | const SkPoint& end, SkScalar endRadius, |
| 50 | const SkColor colors[], const SkScalar pos[], |
| 51 | int colorCount, SkShader::TileMode mode, |
| 52 | SkUnitMapper* mapper); |
| 53 | |
| 54 | virtual void shadeSpan(int x, int y, SkPMColor* dstCParam, |
| 55 | int count) SK_OVERRIDE; |
| 56 | virtual bool setContext(const SkBitmap& device, |
| 57 | const SkPaint& paint, |
| 58 | const SkMatrix& matrix) SK_OVERRIDE; |
| 59 | |
| 60 | virtual BitmapType asABitmap(SkBitmap* bitmap, |
| 61 | SkMatrix* matrix, |
| 62 | TileMode* xy) const; |
| 63 | virtual SkShader::GradientType asAGradient(GradientInfo* info) const SK_OVERRIDE; |
| 64 | virtual GrCustomStage* asNewCustomStage(GrContext* context, |
| 65 | GrSamplerState* sampler) const SK_OVERRIDE; |
| 66 | |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame^] | 67 | SkScalar getCenterX1() const { return SkPoint::Distance(fCenter1, fCenter2); } |
| 68 | SkScalar getStartRadius() const { return fRadius1; } |
| 69 | SkScalar getDiffRadius() const { return fRadius2 - fRadius1; } |
| 70 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 71 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkTwoPointConicalGradient) |
| 72 | |
| 73 | protected: |
| 74 | SkTwoPointConicalGradient(SkFlattenableReadBuffer& buffer); |
| 75 | virtual void flatten(SkFlattenableWriteBuffer& buffer) const SK_OVERRIDE; |
| 76 | |
| 77 | private: |
| 78 | typedef SkGradientShaderBase INHERITED; |
| 79 | const SkPoint fCenter1; |
| 80 | const SkPoint fCenter2; |
| 81 | const SkScalar fRadius1; |
| 82 | const SkScalar fRadius2; |
| 83 | }; |
| 84 | |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 85 | /////////////////////////////////////////////////////////////////////////////// |
| 86 | |
| 87 | class GrGLConical2Gradient; |
| 88 | |
| 89 | class GrConical2Gradient : public GrGradientEffect { |
| 90 | |
| 91 | public: |
| 92 | |
| 93 | GrConical2Gradient(GrTexture* texture, GrScalar center, GrScalar radius, GrScalar diffRadius); |
rileya@google.com | 1c6d64b | 2012-07-27 15:49:05 +0000 | [diff] [blame^] | 94 | GrConical2Gradient(GrContext* ctx, const SkTwoPointConicalGradient& shader, |
| 95 | GrSamplerState* sampler); |
rileya@google.com | d7cc651 | 2012-07-27 14:00:39 +0000 | [diff] [blame] | 96 | virtual ~GrConical2Gradient(); |
| 97 | |
| 98 | static const char* Name() { return "Two-Point Conical Gradient"; } |
| 99 | virtual const GrProgramStageFactory& getFactory() const SK_OVERRIDE; |
| 100 | virtual bool isEqual(const GrCustomStage&) const SK_OVERRIDE; |
| 101 | |
| 102 | // The radial gradient parameters can collapse to a linear (instead of quadratic) equation. |
| 103 | bool isDegenerate() const { return SkScalarAbs(fDiffRadius) == SkScalarAbs(fCenterX1); } |
| 104 | GrScalar center() const { return fCenterX1; } |
| 105 | GrScalar diffRadius() const { return fDiffRadius; } |
| 106 | GrScalar radius() const { return fRadius0; } |
| 107 | |
| 108 | typedef GrGLConical2Gradient GLProgramStage; |
| 109 | |
| 110 | private: |
| 111 | |
| 112 | // @{ |
| 113 | // Cache of values - these can change arbitrarily, EXCEPT |
| 114 | // we shouldn't change between degenerate and non-degenerate?! |
| 115 | |
| 116 | GrScalar fCenterX1; |
| 117 | GrScalar fRadius0; |
| 118 | GrScalar fDiffRadius; |
| 119 | |
| 120 | // @} |
| 121 | |
| 122 | typedef GrGradientEffect INHERITED; |
| 123 | }; |
| 124 | |
rileya@google.com | 589708b | 2012-07-26 20:04:23 +0000 | [diff] [blame] | 125 | #endif |
| 126 | |