1 2 /* 3 * Copyright 2011 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 #include "SkBlurDrawLooper.h" 9 #include "SkBlurMask.h" // just for SkBlurMask::ConvertRadiusToSigma 10 #include "SkBlurMaskFilter.h" 11 #include "SkCanvas.h" 12 #include "SkColorFilter.h" 13 #include "SkFlattenableBuffers.h" 14 #include "SkMaskFilter.h" 15 #include "SkPaint.h" 16 #include "SkString.h" 17 #include "SkStringUtils.h" 18 19 SkBlurDrawLooper::SkBlurDrawLooper(SkScalar radius, SkScalar dx, SkScalar dy, 20 SkColor color, uint32_t flags) { 21 this->init(SkBlurMask::ConvertRadiusToSigma(radius), dx, dy, color, flags); 22 } 23 24 SkBlurDrawLooper::SkBlurDrawLooper(SkColor color, SkScalar sigma, 25 SkScalar dx, SkScalar dy, uint32_t flags) { 26 this->init(sigma, dx, dy, color, flags); 27 } 28 29 void SkBlurDrawLooper::init(SkScalar sigma, SkScalar dx, SkScalar dy, 30 SkColor color, uint32_t flags) { 31 fDx = dx; 32 fDy = dy; 33 fBlurColor = color; 34 fBlurFlags = flags; 35 fState = kDone; 36 37 SkASSERT(flags <= kAll_BlurFlag); 38 if (sigma > 0) { 39 uint32_t blurFlags = flags & kIgnoreTransform_BlurFlag ? 40 SkBlurMaskFilter::kIgnoreTransform_BlurFlag : 41 SkBlurMaskFilter::kNone_BlurFlag; 42 43 blurFlags |= flags & kHighQuality_BlurFlag ? 44 SkBlurMaskFilter::kHighQuality_BlurFlag : 45 SkBlurMaskFilter::kNone_BlurFlag; 46 47 fBlur = SkBlurMaskFilter::Create(SkBlurMaskFilter::kNormal_BlurStyle, 48 sigma, 49 blurFlags); 50 } else { 51 fBlur = NULL; 52 } 53 54 if (flags & kOverrideColor_BlurFlag) { 55 // Set alpha to 1 for the override since transparency will already 56 // be baked into the blurred mask. 57 SkColor opaqueColor = SkColorSetA(color, 255); 58 //The SrcIn xfer mode will multiply 'color' by the incoming alpha 59 fColorFilter = SkColorFilter::CreateModeFilter(opaqueColor, 60 SkXfermode::kSrcIn_Mode); 61 } else { 62 fColorFilter = NULL; 63 } 64 } 65 66 SkBlurDrawLooper::SkBlurDrawLooper(SkFlattenableReadBuffer& buffer) 67 : INHERITED(buffer) { 68 69 fDx = buffer.readScalar(); 70 fDy = buffer.readScalar(); 71 fBlurColor = buffer.readColor(); 72 fBlur = buffer.readMaskFilter(); 73 fColorFilter = buffer.readColorFilter(); 74 fBlurFlags = buffer.readUInt() & kAll_BlurFlag; 75 } 76 77 SkBlurDrawLooper::~SkBlurDrawLooper() { 78 SkSafeUnref(fBlur); 79 SkSafeUnref(fColorFilter); 80 } 81 82 void SkBlurDrawLooper::flatten(SkFlattenableWriteBuffer& buffer) const { 83 this->INHERITED::flatten(buffer); 84 buffer.writeScalar(fDx); 85 buffer.writeScalar(fDy); 86 buffer.writeColor(fBlurColor); 87 buffer.writeFlattenable(fBlur); 88 buffer.writeFlattenable(fColorFilter); 89 buffer.writeUInt(fBlurFlags); 90 } 91 92 void SkBlurDrawLooper::init(SkCanvas*) { 93 fState = kBeforeEdge; 94 } 95 96 bool SkBlurDrawLooper::next(SkCanvas* canvas, SkPaint* paint) { 97 switch (fState) { 98 case kBeforeEdge: 99 // we do nothing if a maskfilter is already installed 100 if (paint->getMaskFilter()) { 101 fState = kDone; 102 return false; 103 } 104 #ifdef SK_BUILD_FOR_ANDROID 105 SkColor blurColor; 106 blurColor = fBlurColor; 107 if (SkColorGetA(blurColor) == 255) { 108 blurColor = SkColorSetA(blurColor, paint->getAlpha()); 109 } 110 paint->setColor(blurColor); 111 #else 112 paint->setColor(fBlurColor); 113 #endif 114 paint->setMaskFilter(fBlur); 115 paint->setColorFilter(fColorFilter); 116 canvas->save(SkCanvas::kMatrix_SaveFlag); 117 if (fBlurFlags & kIgnoreTransform_BlurFlag) { 118 SkMatrix transform(canvas->getTotalMatrix()); 119 transform.postTranslate(fDx, fDy); 120 canvas->setMatrix(transform); 121 } else { 122 canvas->translate(fDx, fDy); 123 } 124 fState = kAfterEdge; 125 return true; 126 case kAfterEdge: 127 canvas->restore(); 128 fState = kDone; 129 return true; 130 default: 131 SkASSERT(kDone == fState); 132 return false; 133 } 134 } 135 136 #ifdef SK_DEVELOPER 137 void SkBlurDrawLooper::toString(SkString* str) const { 138 str->append("SkBlurDrawLooper: "); 139 140 str->append("dx: "); 141 str->appendScalar(fDx); 142 143 str->append(" dy: "); 144 str->appendScalar(fDy); 145 146 str->append(" color: "); 147 str->appendHex(fBlurColor); 148 149 str->append(" flags: ("); 150 if (kNone_BlurFlag == fBlurFlags) { 151 str->append("None"); 152 } else { 153 bool needsSeparator = false; 154 SkAddFlagToString(str, SkToBool(kIgnoreTransform_BlurFlag & fBlurFlags), "IgnoreTransform", 155 &needsSeparator); 156 SkAddFlagToString(str, SkToBool(kOverrideColor_BlurFlag & fBlurFlags), "OverrideColor", 157 &needsSeparator); 158 SkAddFlagToString(str, SkToBool(kHighQuality_BlurFlag & fBlurFlags), "HighQuality", 159 &needsSeparator); 160 } 161 str->append(")"); 162 163 // TODO: add optional "fBlurFilter->toString(str);" when SkMaskFilter::toString is added 164 // alternatively we could cache the radius in SkBlurDrawLooper and just add it here 165 } 166 #endif 167