michael@0: michael@0: /* michael@0: * Copyright 2011 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: #include "SkBlurDrawLooper.h" michael@0: #include "SkBlurMask.h" // just for SkBlurMask::ConvertRadiusToSigma michael@0: #include "SkBlurMaskFilter.h" michael@0: #include "SkCanvas.h" michael@0: #include "SkColorFilter.h" michael@0: #include "SkReadBuffer.h" michael@0: #include "SkWriteBuffer.h" michael@0: #include "SkMaskFilter.h" michael@0: #include "SkPaint.h" michael@0: #include "SkString.h" michael@0: #include "SkStringUtils.h" michael@0: michael@0: SkBlurDrawLooper::SkBlurDrawLooper(SkScalar radius, SkScalar dx, SkScalar dy, michael@0: SkColor color, uint32_t flags) { michael@0: this->init(SkBlurMask::ConvertRadiusToSigma(radius), dx, dy, color, flags); michael@0: } michael@0: michael@0: SkBlurDrawLooper::SkBlurDrawLooper(SkColor color, SkScalar sigma, michael@0: SkScalar dx, SkScalar dy, uint32_t flags) { michael@0: this->init(sigma, dx, dy, color, flags); michael@0: } michael@0: michael@0: void SkBlurDrawLooper::init(SkScalar sigma, SkScalar dx, SkScalar dy, michael@0: SkColor color, uint32_t flags) { michael@0: fDx = dx; michael@0: fDy = dy; michael@0: fBlurColor = color; michael@0: fBlurFlags = flags; michael@0: michael@0: SkASSERT(flags <= kAll_BlurFlag); michael@0: if (sigma > 0) { michael@0: uint32_t blurFlags = flags & kIgnoreTransform_BlurFlag ? michael@0: SkBlurMaskFilter::kIgnoreTransform_BlurFlag : michael@0: SkBlurMaskFilter::kNone_BlurFlag; michael@0: michael@0: blurFlags |= flags & kHighQuality_BlurFlag ? michael@0: SkBlurMaskFilter::kHighQuality_BlurFlag : michael@0: SkBlurMaskFilter::kNone_BlurFlag; michael@0: michael@0: fBlur = SkBlurMaskFilter::Create(SkBlurMaskFilter::kNormal_BlurStyle, michael@0: sigma, michael@0: blurFlags); michael@0: } else { michael@0: fBlur = NULL; michael@0: } michael@0: michael@0: if (flags & kOverrideColor_BlurFlag) { michael@0: // Set alpha to 1 for the override since transparency will already michael@0: // be baked into the blurred mask. michael@0: SkColor opaqueColor = SkColorSetA(color, 255); michael@0: //The SrcIn xfer mode will multiply 'color' by the incoming alpha michael@0: fColorFilter = SkColorFilter::CreateModeFilter(opaqueColor, michael@0: SkXfermode::kSrcIn_Mode); michael@0: } else { michael@0: fColorFilter = NULL; michael@0: } michael@0: } michael@0: michael@0: SkBlurDrawLooper::SkBlurDrawLooper(SkReadBuffer& buffer) michael@0: : INHERITED(buffer) { michael@0: michael@0: fDx = buffer.readScalar(); michael@0: fDy = buffer.readScalar(); michael@0: fBlurColor = buffer.readColor(); michael@0: fBlur = buffer.readMaskFilter(); michael@0: fColorFilter = buffer.readColorFilter(); michael@0: fBlurFlags = buffer.readUInt() & kAll_BlurFlag; michael@0: } michael@0: michael@0: SkBlurDrawLooper::~SkBlurDrawLooper() { michael@0: SkSafeUnref(fBlur); michael@0: SkSafeUnref(fColorFilter); michael@0: } michael@0: michael@0: void SkBlurDrawLooper::flatten(SkWriteBuffer& buffer) const { michael@0: this->INHERITED::flatten(buffer); michael@0: buffer.writeScalar(fDx); michael@0: buffer.writeScalar(fDy); michael@0: buffer.writeColor(fBlurColor); michael@0: buffer.writeFlattenable(fBlur); michael@0: buffer.writeFlattenable(fColorFilter); michael@0: buffer.writeUInt(fBlurFlags); michael@0: } michael@0: michael@0: SkDrawLooper::Context* SkBlurDrawLooper::createContext(SkCanvas*, void* storage) const { michael@0: return SkNEW_PLACEMENT_ARGS(storage, BlurDrawLooperContext, (this)); michael@0: } michael@0: michael@0: SkBlurDrawLooper::BlurDrawLooperContext::BlurDrawLooperContext( michael@0: const SkBlurDrawLooper* looper) michael@0: : fLooper(looper), fState(SkBlurDrawLooper::kBeforeEdge) {} michael@0: michael@0: bool SkBlurDrawLooper::BlurDrawLooperContext::next(SkCanvas* canvas, michael@0: SkPaint* paint) { michael@0: switch (fState) { michael@0: case kBeforeEdge: michael@0: // we do nothing if a maskfilter is already installed michael@0: if (paint->getMaskFilter()) { michael@0: fState = kDone; michael@0: return false; michael@0: } michael@0: #ifdef SK_BUILD_FOR_ANDROID michael@0: SkColor blurColor; michael@0: blurColor = fLooper->fBlurColor; michael@0: if (SkColorGetA(blurColor) == 255) { michael@0: blurColor = SkColorSetA(blurColor, paint->getAlpha()); michael@0: } michael@0: paint->setColor(blurColor); michael@0: #else michael@0: paint->setColor(fLooper->fBlurColor); michael@0: #endif michael@0: paint->setMaskFilter(fLooper->fBlur); michael@0: paint->setColorFilter(fLooper->fColorFilter); michael@0: canvas->save(SkCanvas::kMatrix_SaveFlag); michael@0: if (fLooper->fBlurFlags & kIgnoreTransform_BlurFlag) { michael@0: SkMatrix transform(canvas->getTotalMatrix()); michael@0: transform.postTranslate(fLooper->fDx, fLooper->fDy); michael@0: canvas->setMatrix(transform); michael@0: } else { michael@0: canvas->translate(fLooper->fDx, fLooper->fDy); michael@0: } michael@0: fState = kAfterEdge; michael@0: return true; michael@0: case kAfterEdge: michael@0: canvas->restore(); michael@0: fState = kDone; michael@0: return true; michael@0: default: michael@0: SkASSERT(kDone == fState); michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: #ifndef SK_IGNORE_TO_STRING michael@0: void SkBlurDrawLooper::toString(SkString* str) const { michael@0: str->append("SkBlurDrawLooper: "); michael@0: michael@0: str->append("dx: "); michael@0: str->appendScalar(fDx); michael@0: michael@0: str->append(" dy: "); michael@0: str->appendScalar(fDy); michael@0: michael@0: str->append(" color: "); michael@0: str->appendHex(fBlurColor); michael@0: michael@0: str->append(" flags: ("); michael@0: if (kNone_BlurFlag == fBlurFlags) { michael@0: str->append("None"); michael@0: } else { michael@0: bool needsSeparator = false; michael@0: SkAddFlagToString(str, SkToBool(kIgnoreTransform_BlurFlag & fBlurFlags), "IgnoreTransform", michael@0: &needsSeparator); michael@0: SkAddFlagToString(str, SkToBool(kOverrideColor_BlurFlag & fBlurFlags), "OverrideColor", michael@0: &needsSeparator); michael@0: SkAddFlagToString(str, SkToBool(kHighQuality_BlurFlag & fBlurFlags), "HighQuality", michael@0: &needsSeparator); michael@0: } michael@0: str->append(")"); michael@0: michael@0: // TODO: add optional "fBlurFilter->toString(str);" when SkMaskFilter::toString is added michael@0: // alternatively we could cache the radius in SkBlurDrawLooper and just add it here michael@0: } michael@0: #endif