epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 7 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SkBlurDrawLooper.h" |
robertphillips@google.com | 7ce661d | 2013-08-27 16:14:03 +0000 | [diff] [blame] | 9 | #include "SkBlurMask.h" // just for SkBlurMask::ConvertRadiusToSigma |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #include "SkBlurMaskFilter.h" |
| 11 | #include "SkCanvas.h" |
senorblanco@chromium.org | 4868e6b | 2011-02-18 19:03:01 +0000 | [diff] [blame] | 12 | #include "SkColorFilter.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 13 | #include "SkReadBuffer.h" |
| 14 | #include "SkWriteBuffer.h" |
robertphillips@google.com | 4991b8f | 2013-01-28 20:21:59 +0000 | [diff] [blame] | 15 | #include "SkMaskFilter.h" |
| 16 | #include "SkPaint.h" |
| 17 | #include "SkString.h" |
| 18 | #include "SkStringUtils.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 19 | |
skia.committer@gmail.com | 7bd141d | 2013-08-28 07:01:18 +0000 | [diff] [blame] | 20 | SkBlurDrawLooper::SkBlurDrawLooper(SkColor color, SkScalar sigma, |
robertphillips@google.com | 7ce661d | 2013-08-27 16:14:03 +0000 | [diff] [blame] | 21 | SkScalar dx, SkScalar dy, uint32_t flags) { |
| 22 | this->init(sigma, dx, dy, color, flags); |
| 23 | } |
| 24 | |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 25 | // only call from constructor |
| 26 | void SkBlurDrawLooper::initEffects() { |
| 27 | SkASSERT(fBlurFlags <= kAll_BlurFlag); |
| 28 | if (fSigma > 0) { |
| 29 | uint32_t flags = fBlurFlags & kIgnoreTransform_BlurFlag ? |
| 30 | SkBlurMaskFilter::kIgnoreTransform_BlurFlag : |
| 31 | SkBlurMaskFilter::kNone_BlurFlag; |
skia.committer@gmail.com | eeaeafe | 2014-04-30 03:05:25 +0000 | [diff] [blame] | 32 | |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 33 | flags |= fBlurFlags & kHighQuality_BlurFlag ? |
| 34 | SkBlurMaskFilter::kHighQuality_BlurFlag : |
| 35 | SkBlurMaskFilter::kNone_BlurFlag; |
skia.committer@gmail.com | eeaeafe | 2014-04-30 03:05:25 +0000 | [diff] [blame] | 36 | |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 37 | fBlur = SkBlurMaskFilter::Create(kNormal_SkBlurStyle, fSigma, flags); |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 38 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 39 | fBlur = nullptr; |
senorblanco@chromium.org | 4868e6b | 2011-02-18 19:03:01 +0000 | [diff] [blame] | 40 | } |
skia.committer@gmail.com | eeaeafe | 2014-04-30 03:05:25 +0000 | [diff] [blame] | 41 | |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 42 | if (fBlurFlags & kOverrideColor_BlurFlag) { |
junov@google.com | a639891 | 2011-03-11 16:45:00 +0000 | [diff] [blame] | 43 | // Set alpha to 1 for the override since transparency will already |
| 44 | // be baked into the blurred mask. |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 45 | SkColor opaqueColor = SkColorSetA(fBlurColor, 255); |
senorblanco@chromium.org | 4868e6b | 2011-02-18 19:03:01 +0000 | [diff] [blame] | 46 | //The SrcIn xfer mode will multiply 'color' by the incoming alpha |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 47 | fColorFilter = SkColorFilter::CreateModeFilter(opaqueColor, |
| 48 | SkXfermode::kSrcIn_Mode); |
| 49 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 50 | fColorFilter = nullptr; |
senorblanco@chromium.org | 4868e6b | 2011-02-18 19:03:01 +0000 | [diff] [blame] | 51 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 52 | } |
| 53 | |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 54 | void SkBlurDrawLooper::init(SkScalar sigma, SkScalar dx, SkScalar dy, |
| 55 | SkColor color, uint32_t flags) { |
| 56 | fSigma = sigma; |
| 57 | fDx = dx; |
| 58 | fDy = dy; |
| 59 | fBlurColor = color; |
| 60 | fBlurFlags = flags; |
reed@google.com | 6bac947 | 2011-06-21 19:24:00 +0000 | [diff] [blame] | 61 | |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 62 | this->initEffects(); |
| 63 | } |
| 64 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 65 | SkFlattenable* SkBlurDrawLooper::CreateProc(SkReadBuffer& buffer) { |
| 66 | const SkColor color = buffer.readColor(); |
| 67 | const SkScalar sigma = buffer.readScalar(); |
| 68 | const SkScalar dx = buffer.readScalar(); |
| 69 | const SkScalar dy = buffer.readScalar(); |
| 70 | const uint32_t flags = buffer.read32(); |
| 71 | return Create(color, sigma, dx, dy, flags); |
| 72 | } |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 73 | |
| 74 | void SkBlurDrawLooper::flatten(SkWriteBuffer& buffer) const { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 75 | buffer.writeColor(fBlurColor); |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 76 | buffer.writeScalar(fSigma); |
| 77 | buffer.writeScalar(fDx); |
| 78 | buffer.writeScalar(fDy); |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 79 | buffer.write32(fBlurFlags); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 80 | } |
| 81 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 82 | SkBlurDrawLooper::~SkBlurDrawLooper() { |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 83 | SkSafeUnref(fBlur); |
senorblanco@chromium.org | 4868e6b | 2011-02-18 19:03:01 +0000 | [diff] [blame] | 84 | SkSafeUnref(fColorFilter); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 85 | } |
| 86 | |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 87 | bool SkBlurDrawLooper::asABlurShadow(BlurShadowRec* rec) const { |
| 88 | if (fSigma <= 0 || (fBlurFlags & fBlurFlags & kIgnoreTransform_BlurFlag)) { |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | if (rec) { |
| 93 | rec->fSigma = fSigma; |
| 94 | rec->fColor = fBlurColor; |
| 95 | rec->fOffset.set(fDx, fDy); |
| 96 | rec->fStyle = kNormal_SkBlurStyle; |
| 97 | rec->fQuality = (fBlurFlags & kHighQuality_BlurFlag) ? |
| 98 | kHigh_SkBlurQuality : kLow_SkBlurQuality; |
| 99 | } |
| 100 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 101 | } |
| 102 | |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 103 | //////////////////////////////////////////////////////////////////////////////////////// |
| 104 | |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 105 | SkDrawLooper::Context* SkBlurDrawLooper::createContext(SkCanvas*, void* storage) const { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 106 | return new (storage) BlurDrawLooperContext(this); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 107 | } |
| 108 | |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 109 | SkBlurDrawLooper::BlurDrawLooperContext::BlurDrawLooperContext( |
| 110 | const SkBlurDrawLooper* looper) |
| 111 | : fLooper(looper), fState(SkBlurDrawLooper::kBeforeEdge) {} |
| 112 | |
| 113 | bool SkBlurDrawLooper::BlurDrawLooperContext::next(SkCanvas* canvas, |
| 114 | SkPaint* paint) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 115 | switch (fState) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 116 | case kBeforeEdge: |
| 117 | // we do nothing if a maskfilter is already installed |
| 118 | if (paint->getMaskFilter()) { |
| 119 | fState = kDone; |
| 120 | return false; |
| 121 | } |
djsollen@google.com | 56c6977 | 2011-11-08 19:00:26 +0000 | [diff] [blame] | 122 | #ifdef SK_BUILD_FOR_ANDROID |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 123 | SkColor blurColor; |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 124 | blurColor = fLooper->fBlurColor; |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 125 | if (SkColorGetA(blurColor) == 255) { |
| 126 | blurColor = SkColorSetA(blurColor, paint->getAlpha()); |
| 127 | } |
| 128 | paint->setColor(blurColor); |
| 129 | #else |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 130 | paint->setColor(fLooper->fBlurColor); |
djsollen@google.com | f5dbe2f | 2011-04-15 13:41:26 +0000 | [diff] [blame] | 131 | #endif |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 132 | paint->setMaskFilter(fLooper->fBlur); |
| 133 | paint->setColorFilter(fLooper->fColorFilter); |
commit-bot@chromium.org | 74e608c | 2014-04-23 19:26:38 +0000 | [diff] [blame] | 134 | canvas->save(); |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 135 | if (fLooper->fBlurFlags & kIgnoreTransform_BlurFlag) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 136 | SkMatrix transform(canvas->getTotalMatrix()); |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 137 | transform.postTranslate(fLooper->fDx, fLooper->fDy); |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 138 | canvas->setMatrix(transform); |
| 139 | } else { |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 140 | canvas->translate(fLooper->fDx, fLooper->fDy); |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 141 | } |
| 142 | fState = kAfterEdge; |
| 143 | return true; |
| 144 | case kAfterEdge: |
| 145 | canvas->restore(); |
| 146 | fState = kDone; |
| 147 | return true; |
| 148 | default: |
| 149 | SkASSERT(kDone == fState); |
| 150 | return false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 151 | } |
| 152 | } |
robertphillips@google.com | 4991b8f | 2013-01-28 20:21:59 +0000 | [diff] [blame] | 153 | |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 154 | #ifndef SK_IGNORE_TO_STRING |
robertphillips@google.com | 4991b8f | 2013-01-28 20:21:59 +0000 | [diff] [blame] | 155 | void SkBlurDrawLooper::toString(SkString* str) const { |
| 156 | str->append("SkBlurDrawLooper: "); |
| 157 | |
| 158 | str->append("dx: "); |
| 159 | str->appendScalar(fDx); |
| 160 | |
| 161 | str->append(" dy: "); |
| 162 | str->appendScalar(fDy); |
| 163 | |
| 164 | str->append(" color: "); |
| 165 | str->appendHex(fBlurColor); |
| 166 | |
| 167 | str->append(" flags: ("); |
| 168 | if (kNone_BlurFlag == fBlurFlags) { |
| 169 | str->append("None"); |
| 170 | } else { |
| 171 | bool needsSeparator = false; |
skia.committer@gmail.com | cdcb2ce | 2013-01-29 07:05:52 +0000 | [diff] [blame] | 172 | SkAddFlagToString(str, SkToBool(kIgnoreTransform_BlurFlag & fBlurFlags), "IgnoreTransform", |
robertphillips@google.com | 4991b8f | 2013-01-28 20:21:59 +0000 | [diff] [blame] | 173 | &needsSeparator); |
| 174 | SkAddFlagToString(str, SkToBool(kOverrideColor_BlurFlag & fBlurFlags), "OverrideColor", |
| 175 | &needsSeparator); |
skia.committer@gmail.com | cdcb2ce | 2013-01-29 07:05:52 +0000 | [diff] [blame] | 176 | SkAddFlagToString(str, SkToBool(kHighQuality_BlurFlag & fBlurFlags), "HighQuality", |
robertphillips@google.com | 4991b8f | 2013-01-28 20:21:59 +0000 | [diff] [blame] | 177 | &needsSeparator); |
| 178 | } |
| 179 | str->append(")"); |
| 180 | |
| 181 | // TODO: add optional "fBlurFilter->toString(str);" when SkMaskFilter::toString is added |
| 182 | // alternatively we could cache the radius in SkBlurDrawLooper and just add it here |
| 183 | } |
| 184 | #endif |