1 #include "SkBlurDrawLooper.h" 2 #include "SkBlurMaskFilter.h" 3 #include "SkCanvas.h" 4 #include "SkPaint.h" 5 #include "SkMaskFilter.h" 6 7 SkBlurDrawLooper::SkBlurDrawLooper(SkScalar radius, SkScalar dx, SkScalar dy, 8 SkColor color) 9 : fDx(dx), fDy(dy), fBlurColor(color) 10 { 11 if (radius > 0) 12 fBlur = SkBlurMaskFilter::Create(radius, 13 SkBlurMaskFilter::kNormal_BlurStyle); 14 else 15 fBlur = NULL; 16 } 17 18 SkBlurDrawLooper::SkBlurDrawLooper(SkFlattenableReadBuffer& buffer) 19 { 20 fDx = buffer.readScalar(); 21 fDy = buffer.readScalar(); 22 fBlurColor = buffer.readU32(); 23 fBlur = static_cast<SkMaskFilter*>(buffer.readFlattenable()); 24 } 25 26 SkBlurDrawLooper::~SkBlurDrawLooper() 27 { 28 fBlur->safeUnref(); 29 } 30 31 void SkBlurDrawLooper::flatten(SkFlattenableWriteBuffer& buffer) 32 { 33 buffer.writeScalar(fDx); 34 buffer.writeScalar(fDy); 35 buffer.write32(fBlurColor); 36 buffer.writeFlattenable(fBlur); 37 } 38 39 void SkBlurDrawLooper::init(SkCanvas* canvas, SkPaint* paint) 40 { 41 // we do nothing if a maskfilter is already installed 42 if (paint->getMaskFilter() != NULL) 43 fState = kDone; 44 else 45 { 46 fState = kBeforeEdge; 47 fPaint = paint; 48 fCanvas = canvas; 49 fSaveCount = canvas->getSaveCount(); 50 } 51 } 52 53 bool SkBlurDrawLooper::next() 54 { 55 switch (fState) { 56 case kBeforeEdge: 57 fSavedColor = fPaint->getColor(); 58 fPaint->setColor(fBlurColor); 59 fPaint->setMaskFilter(fBlur); 60 fCanvas->save(SkCanvas::kMatrix_SaveFlag); 61 fCanvas->translate(fDx, fDy); 62 fState = kAfterEdge; 63 return true; 64 case kAfterEdge: 65 fPaint->setColor(fSavedColor); 66 fPaint->setMaskFilter(NULL); 67 fCanvas->restore(); // to remove the translate we did earlier 68 fState = kDone; 69 return true; 70 default: 71 SkASSERT(kDone == fState); 72 return false; 73 } 74 } 75 76 void SkBlurDrawLooper::restore() 77 { 78 if (kAfterEdge == fState) 79 { 80 fPaint->setColor(fSavedColor); 81 fPaint->setMaskFilter(NULL); 82 fCanvas->restore(); // to remove the translate we did earlier 83 fState = kDone; 84 } 85 } 86 87 /////////////////////////////////////////////////////////////////////////////// 88 89 static SkFlattenable::Registrar gReg("SkBlurDrawLooper", 90 SkBlurDrawLooper::CreateProc); 91 92