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 // TODO(dominikg): Worth making it truly immutable (i.e. set values in constructor)? 15 // Should only be initialized once via init(). Immutable afterwards. 16 struct TwoPtRadial { 17 enum { 18 kDontDrawT = 0x80000000 19 }; 20 21 float fCenterX, fCenterY; 22 float fDCenterX, fDCenterY; 23 float fRadius; 24 float fDRadius; 25 float fA; 26 float fRadius2; 27 float fRDR; 28 bool fFlipped; 29 30 void init(const SkPoint& center0, SkScalar rad0, 31 const SkPoint& center1, SkScalar rad1, 32 bool flipped); 33 34 static bool DontDrawT(SkFixed t) { 35 return kDontDrawT == (uint32_t)t; 36 } 37 }; 38 39 40 class SkTwoPointConicalGradient : public SkGradientShaderBase { 41 TwoPtRadial fRec; 42 void init(); 43 44 public: 45 SkTwoPointConicalGradient(const SkPoint& start, SkScalar startRadius, 46 const SkPoint& end, SkScalar endRadius, 47 bool flippedGrad, const Descriptor&, 48 const SkMatrix* localMatrix); 49 50 51 virtual size_t contextSize() const SK_OVERRIDE; 52 53 class TwoPointConicalGradientContext : public SkGradientShaderBase::GradientShaderBaseContext { 54 public: 55 TwoPointConicalGradientContext(const SkTwoPointConicalGradient&, const ContextRec&); 56 ~TwoPointConicalGradientContext() {} 57 58 virtual void shadeSpan(int x, int y, SkPMColor dstC[], int count) SK_OVERRIDE; 59 60 private: 61 typedef SkGradientShaderBase::GradientShaderBaseContext INHERITED; 62 }; 63 64 virtual BitmapType asABitmap(SkBitmap* bitmap, 65 SkMatrix* matrix, 66 TileMode* xy) const; 67 virtual SkShader::GradientType asAGradient(GradientInfo* info) const SK_OVERRIDE; 68 virtual bool asNewEffect(GrContext*, const SkPaint&, const SkMatrix*, GrColor* grColor, 69 GrEffectRef**) const SK_OVERRIDE; 70 virtual bool isOpaque() const SK_OVERRIDE; 71 72 SkScalar getCenterX1() const { return SkPoint::Distance(fCenter1, fCenter2); } 73 SkScalar getStartRadius() const { return fRadius1; } 74 SkScalar getDiffRadius() const { return fRadius2 - fRadius1; } 75 const SkPoint& getStartCenter() const { return fCenter1; } 76 const SkPoint& getEndCenter() const { return fCenter2; } 77 SkScalar getEndRadius() const { return fRadius2; } 78 bool isFlippedGrad() const { return fFlippedGrad; } 79 80 SK_TO_STRING_OVERRIDE() 81 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkTwoPointConicalGradient) 82 83 protected: 84 SkTwoPointConicalGradient(SkReadBuffer& buffer); 85 virtual void flatten(SkWriteBuffer& buffer) const SK_OVERRIDE; 86 virtual Context* onCreateContext(const ContextRec&, void* storage) const SK_OVERRIDE; 87 88 private: 89 SkPoint fCenter1; 90 SkPoint fCenter2; 91 SkScalar fRadius1; 92 SkScalar fRadius2; 93 bool fFlippedGrad; 94 95 typedef SkGradientShaderBase INHERITED; 96 }; 97 98 #endif 99