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 | */ |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 7 | #include "include/core/SkCanvas.h" |
| 8 | #include "include/core/SkColor.h" |
| 9 | #include "include/core/SkMaskFilter.h" |
| 10 | #include "include/core/SkString.h" |
| 11 | #include "include/core/SkUnPreMultiply.h" |
Ben Wagner | 729a23f | 2019-05-17 16:29:34 -0400 | [diff] [blame] | 12 | #include "src/core/SkArenaAlloc.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/core/SkBlendModePriv.h" |
| 14 | #include "src/core/SkColorSpacePriv.h" |
| 15 | #include "src/core/SkMaskFilterBase.h" |
| 16 | #include "src/core/SkReadBuffer.h" |
| 17 | #include "src/core/SkStringUtils.h" |
| 18 | #include "src/core/SkWriteBuffer.h" |
| 19 | #include "src/core/SkXfermodePriv.h" |
reed@google.com | 0716c63 | 2011-04-12 18:32:06 +0000 | [diff] [blame] | 20 | |
Mike Reed | 1d62221 | 2021-02-04 20:11:25 -0500 | [diff] [blame] | 21 | #ifdef SK_SUPPORT_LEGACY_DRAWLOOPER |
| 22 | |
| 23 | #include "include/effects/SkBlurDrawLooper.h" |
| 24 | #include "include/effects/SkLayerDrawLooper.h" |
| 25 | |
reed@google.com | 0716c63 | 2011-04-12 18:32:06 +0000 | [diff] [blame] | 26 | SkLayerDrawLooper::LayerInfo::LayerInfo() { |
mike@reedtribe.org | a8282ef | 2011-04-14 01:22:45 +0000 | [diff] [blame] | 27 | fPaintBits = 0; // ignore our paint fields |
Mike Reed | faba371 | 2016-11-03 14:45:31 -0400 | [diff] [blame] | 28 | fColorMode = SkBlendMode::kDst; // ignore our color |
reed@google.com | 0716c63 | 2011-04-12 18:32:06 +0000 | [diff] [blame] | 29 | fOffset.set(0, 0); |
| 30 | fPostTranslate = false; |
| 31 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 32 | |
vandebo@chromium.org | 3c89818 | 2011-06-22 05:01:28 +0000 | [diff] [blame] | 33 | SkLayerDrawLooper::SkLayerDrawLooper() |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 34 | : fRecs(nullptr), |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 35 | fCount(0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | SkLayerDrawLooper::~SkLayerDrawLooper() { |
| 39 | Rec* rec = fRecs; |
| 40 | while (rec) { |
| 41 | Rec* next = rec->fNext; |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 42 | delete rec; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 43 | rec = next; |
| 44 | } |
| 45 | } |
reed@google.com | 0716c63 | 2011-04-12 18:32:06 +0000 | [diff] [blame] | 46 | |
Herb Derby | 73fe7b0 | 2017-02-08 15:12:19 -0500 | [diff] [blame] | 47 | SkLayerDrawLooper::Context* |
Mike Reed | 59f2e46 | 2019-07-25 14:33:59 -0400 | [diff] [blame] | 48 | SkLayerDrawLooper::makeContext(SkArenaAlloc* alloc) const { |
Herb Derby | 73fe7b0 | 2017-02-08 15:12:19 -0500 | [diff] [blame] | 49 | return alloc->make<LayerDrawLooperContext>(this); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Brian Osman | 9d1c88d | 2018-09-04 15:16:03 -0400 | [diff] [blame] | 52 | static SkColor4f xferColor(const SkColor4f& src, const SkColor4f& dst, SkBlendMode mode) { |
reed@google.com | 0716c63 | 2011-04-12 18:32:06 +0000 | [diff] [blame] | 53 | switch (mode) { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 54 | case SkBlendMode::kSrc: |
reed@google.com | 0716c63 | 2011-04-12 18:32:06 +0000 | [diff] [blame] | 55 | return src; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 56 | case SkBlendMode::kDst: |
reed@google.com | 0716c63 | 2011-04-12 18:32:06 +0000 | [diff] [blame] | 57 | return dst; |
| 58 | default: { |
Brian Osman | d25b7c1 | 2018-09-21 16:01:59 -0400 | [diff] [blame] | 59 | SkPMColor4f pmS = src.premul(); |
| 60 | SkPMColor4f pmD = dst.premul(); |
Brian Osman | 9d1c88d | 2018-09-04 15:16:03 -0400 | [diff] [blame] | 61 | return SkBlendMode_Apply(mode, pmS, pmD).unpremul(); |
reed@google.com | 0716c63 | 2011-04-12 18:32:06 +0000 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
Leon Scroggins III | 1ff0706 | 2020-07-27 14:52:19 -0400 | [diff] [blame] | 66 | // Even with kEntirePaint_Bits, we always ensure that the base paint's |
reed@google.com | 8426058 | 2011-11-21 16:42:10 +0000 | [diff] [blame] | 67 | // text-encoding is respected, since that controls how we interpret the |
| 68 | // text/length parameters of a draw[Pos]Text call. |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 69 | void SkLayerDrawLooper::LayerDrawLooperContext::ApplyInfo( |
| 70 | SkPaint* dst, const SkPaint& src, const LayerInfo& info) { |
Brian Osman | 9d1c88d | 2018-09-04 15:16:03 -0400 | [diff] [blame] | 71 | SkColor4f srcColor = src.getColor4f(); |
Matt Sarett | 56ea77a | 2017-03-28 15:27:06 -0400 | [diff] [blame] | 72 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 73 | // The framework may respect the alpha value on the original paint. |
| 74 | // Match this legacy behavior. |
Brian Osman | 9d1c88d | 2018-09-04 15:16:03 -0400 | [diff] [blame] | 75 | if (src.getAlpha() == 255) { |
| 76 | srcColor.fA = dst->getColor4f().fA; |
Matt Sarett | 56ea77a | 2017-03-28 15:27:06 -0400 | [diff] [blame] | 77 | } |
| 78 | #endif |
Brian Osman | 9d1c88d | 2018-09-04 15:16:03 -0400 | [diff] [blame] | 79 | dst->setColor4f(xferColor(srcColor, dst->getColor4f(), (SkBlendMode)info.fColorMode), |
| 80 | sk_srgb_singleton()); |
mike@reedtribe.org | a8282ef | 2011-04-14 01:22:45 +0000 | [diff] [blame] | 81 | |
| 82 | BitFlags bits = info.fPaintBits; |
reed@google.com | 0716c63 | 2011-04-12 18:32:06 +0000 | [diff] [blame] | 83 | |
mike@reedtribe.org | 3c1225b | 2011-04-09 18:54:08 +0000 | [diff] [blame] | 84 | if (0 == bits) { |
| 85 | return; |
| 86 | } |
mike@reedtribe.org | 0e2810b | 2011-04-08 02:41:54 +0000 | [diff] [blame] | 87 | if (kEntirePaint_Bits == bits) { |
mike@reedtribe.org | a8282ef | 2011-04-14 01:22:45 +0000 | [diff] [blame] | 88 | // we've already computed these, so save it from the assignment |
Mike Reed | f738d42 | 2019-01-16 17:12:15 -0500 | [diff] [blame] | 89 | bool aa = dst->isAntiAlias(); |
| 90 | bool di = dst->isDither(); |
Brian Osman | 9d1c88d | 2018-09-04 15:16:03 -0400 | [diff] [blame] | 91 | SkColor4f c = dst->getColor4f(); |
mike@reedtribe.org | 0e2810b | 2011-04-08 02:41:54 +0000 | [diff] [blame] | 92 | *dst = src; |
Mike Reed | f738d42 | 2019-01-16 17:12:15 -0500 | [diff] [blame] | 93 | dst->setAntiAlias(aa); |
| 94 | dst->setDither(di); |
Brian Osman | 9d1c88d | 2018-09-04 15:16:03 -0400 | [diff] [blame] | 95 | dst->setColor4f(c, sk_srgb_singleton()); |
mike@reedtribe.org | 0e2810b | 2011-04-08 02:41:54 +0000 | [diff] [blame] | 96 | return; |
| 97 | } |
| 98 | |
mike@reedtribe.org | 0e2810b | 2011-04-08 02:41:54 +0000 | [diff] [blame] | 99 | if (bits & kStyle_Bit) { |
| 100 | dst->setStyle(src.getStyle()); |
| 101 | dst->setStrokeWidth(src.getStrokeWidth()); |
| 102 | dst->setStrokeMiter(src.getStrokeMiter()); |
| 103 | dst->setStrokeCap(src.getStrokeCap()); |
| 104 | dst->setStrokeJoin(src.getStrokeJoin()); |
| 105 | } |
| 106 | |
mike@reedtribe.org | 0e2810b | 2011-04-08 02:41:54 +0000 | [diff] [blame] | 107 | if (bits & kPathEffect_Bit) { |
Mike Reed | 693fdbd | 2017-01-12 10:13:40 -0500 | [diff] [blame] | 108 | dst->setPathEffect(src.refPathEffect()); |
mike@reedtribe.org | 0e2810b | 2011-04-08 02:41:54 +0000 | [diff] [blame] | 109 | } |
| 110 | if (bits & kMaskFilter_Bit) { |
Mike Reed | 693fdbd | 2017-01-12 10:13:40 -0500 | [diff] [blame] | 111 | dst->setMaskFilter(src.refMaskFilter()); |
mike@reedtribe.org | 0e2810b | 2011-04-08 02:41:54 +0000 | [diff] [blame] | 112 | } |
| 113 | if (bits & kShader_Bit) { |
Mike Reed | 693fdbd | 2017-01-12 10:13:40 -0500 | [diff] [blame] | 114 | dst->setShader(src.refShader()); |
mike@reedtribe.org | 0e2810b | 2011-04-08 02:41:54 +0000 | [diff] [blame] | 115 | } |
| 116 | if (bits & kColorFilter_Bit) { |
Mike Reed | 693fdbd | 2017-01-12 10:13:40 -0500 | [diff] [blame] | 117 | dst->setColorFilter(src.refColorFilter()); |
mike@reedtribe.org | 0e2810b | 2011-04-08 02:41:54 +0000 | [diff] [blame] | 118 | } |
| 119 | if (bits & kXfermode_Bit) { |
Mike Reed | cce0b60 | 2021-07-08 15:19:02 -0400 | [diff] [blame] | 120 | dst->setBlender(src.refBlender()); |
mike@reedtribe.org | 0e2810b | 2011-04-08 02:41:54 +0000 | [diff] [blame] | 121 | } |
| 122 | |
reed@google.com | 8426058 | 2011-11-21 16:42:10 +0000 | [diff] [blame] | 123 | // we don't override these |
mike@reedtribe.org | 0e2810b | 2011-04-08 02:41:54 +0000 | [diff] [blame] | 124 | #if 0 |
mike@reedtribe.org | 0e2810b | 2011-04-08 02:41:54 +0000 | [diff] [blame] | 125 | dst->setTypeface(src.getTypeface()); |
| 126 | dst->setTextSize(src.getTextSize()); |
| 127 | dst->setTextScaleX(src.getTextScaleX()); |
mike@reedtribe.org | 0e2810b | 2011-04-08 02:41:54 +0000 | [diff] [blame] | 128 | dst->setRasterizer(src.getRasterizer()); |
| 129 | dst->setLooper(src.getLooper()); |
| 130 | dst->setTextEncoding(src.getTextEncoding()); |
| 131 | dst->setHinting(src.getHinting()); |
| 132 | #endif |
| 133 | } |
| 134 | |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 135 | SkLayerDrawLooper::LayerDrawLooperContext::LayerDrawLooperContext( |
| 136 | const SkLayerDrawLooper* looper) : fCurrRec(looper->fRecs) {} |
| 137 | |
Mike Reed | 59f2e46 | 2019-07-25 14:33:59 -0400 | [diff] [blame] | 138 | bool SkLayerDrawLooper::LayerDrawLooperContext::next(Info* info, SkPaint* paint) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 139 | if (nullptr == fCurrRec) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 140 | return false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 141 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 142 | |
mike@reedtribe.org | a8282ef | 2011-04-14 01:22:45 +0000 | [diff] [blame] | 143 | ApplyInfo(paint, fCurrRec->fPaint, fCurrRec->fInfo); |
reed@google.com | 0716c63 | 2011-04-12 18:32:06 +0000 | [diff] [blame] | 144 | |
Mike Reed | 59f2e46 | 2019-07-25 14:33:59 -0400 | [diff] [blame] | 145 | if (info) { |
| 146 | info->fTranslate = fCurrRec->fInfo.fOffset; |
| 147 | info->fApplyPostCTM = fCurrRec->fInfo.fPostTranslate; |
reed@google.com | 0716c63 | 2011-04-12 18:32:06 +0000 | [diff] [blame] | 148 | } |
mike@reedtribe.org | e5d0def | 2011-04-08 00:53:48 +0000 | [diff] [blame] | 149 | fCurrRec = fCurrRec->fNext; |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 150 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 151 | } |
| 152 | |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 153 | bool SkLayerDrawLooper::asABlurShadow(BlurShadowRec* bsRec) const { |
| 154 | if (fCount != 2) { |
| 155 | return false; |
| 156 | } |
| 157 | const Rec* rec = fRecs; |
| 158 | |
| 159 | // bottom layer needs to be just blur(maskfilter) |
| 160 | if ((rec->fInfo.fPaintBits & ~kMaskFilter_Bit)) { |
| 161 | return false; |
| 162 | } |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 163 | if (SkBlendMode::kSrc != (SkBlendMode)rec->fInfo.fColorMode) { |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 164 | return false; |
| 165 | } |
| 166 | const SkMaskFilter* mf = rec->fPaint.getMaskFilter(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 167 | if (nullptr == mf) { |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 168 | return false; |
| 169 | } |
Mike Reed | 80747ef | 2018-01-23 15:29:32 -0500 | [diff] [blame] | 170 | SkMaskFilterBase::BlurRec maskBlur; |
| 171 | if (!as_MFB(mf)->asABlur(&maskBlur)) { |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 172 | return false; |
| 173 | } |
| 174 | |
| 175 | rec = rec->fNext; |
| 176 | // top layer needs to be "plain" |
| 177 | if (rec->fInfo.fPaintBits) { |
| 178 | return false; |
| 179 | } |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 180 | if (SkBlendMode::kDst != (SkBlendMode)rec->fInfo.fColorMode) { |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 181 | return false; |
| 182 | } |
| 183 | if (!rec->fInfo.fOffset.equals(0, 0)) { |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | if (bsRec) { |
| 188 | bsRec->fSigma = maskBlur.fSigma; |
| 189 | bsRec->fOffset = fRecs->fInfo.fOffset; |
Brian Osman | 9d1c88d | 2018-09-04 15:16:03 -0400 | [diff] [blame] | 190 | // TODO: Update BlurShadowRec to use SkColor4f? |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 191 | bsRec->fColor = fRecs->fPaint.getColor(); |
| 192 | bsRec->fStyle = maskBlur.fStyle; |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 193 | } |
| 194 | return true; |
| 195 | } |
| 196 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 197 | /////////////////////////////////////////////////////////////////////////////// |
| 198 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 199 | void SkLayerDrawLooper::flatten(SkWriteBuffer& buffer) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 200 | buffer.writeInt(fCount); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 201 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 202 | Rec* rec = fRecs; |
| 203 | for (int i = 0; i < fCount; i++) { |
reed@google.com | 842292f | 2014-02-28 15:38:22 +0000 | [diff] [blame] | 204 | // Legacy "flagsmask" field -- now ignored, remove when we bump version |
| 205 | buffer.writeInt(0); |
| 206 | |
reed@google.com | 0716c63 | 2011-04-12 18:32:06 +0000 | [diff] [blame] | 207 | buffer.writeInt(rec->fInfo.fPaintBits); |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 208 | buffer.writeInt((int)rec->fInfo.fColorMode); |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 209 | buffer.writePoint(rec->fInfo.fOffset); |
reed@google.com | 0716c63 | 2011-04-12 18:32:06 +0000 | [diff] [blame] | 210 | buffer.writeBool(rec->fInfo.fPostTranslate); |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 211 | buffer.writePaint(rec->fPaint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 212 | rec = rec->fNext; |
| 213 | } |
| 214 | } |
| 215 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 216 | sk_sp<SkFlattenable> SkLayerDrawLooper::CreateProc(SkReadBuffer& buffer) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 217 | int count = buffer.readInt(); |
| 218 | |
Kevin Lubick | 493f89e | 2020-09-14 08:37:35 -0400 | [diff] [blame] | 219 | #if defined(SK_BUILD_FOR_FUZZER) |
| 220 | if (count > 100) { |
| 221 | count = 100; |
| 222 | } |
| 223 | #endif |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame] | 224 | Builder builder; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 225 | for (int i = 0; i < count; i++) { |
reed@google.com | 0716c63 | 2011-04-12 18:32:06 +0000 | [diff] [blame] | 226 | LayerInfo info; |
reed@google.com | 842292f | 2014-02-28 15:38:22 +0000 | [diff] [blame] | 227 | // Legacy "flagsmask" field -- now ignored, remove when we bump version |
reed@google.com | 48f31bd | 2014-02-27 14:27:44 +0000 | [diff] [blame] | 228 | (void)buffer.readInt(); |
reed@google.com | 842292f | 2014-02-28 15:38:22 +0000 | [diff] [blame] | 229 | |
reed@google.com | 0716c63 | 2011-04-12 18:32:06 +0000 | [diff] [blame] | 230 | info.fPaintBits = buffer.readInt(); |
Mike Reed | faba371 | 2016-11-03 14:45:31 -0400 | [diff] [blame] | 231 | info.fColorMode = (SkBlendMode)buffer.readInt(); |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 232 | buffer.readPoint(&info.fOffset); |
reed@google.com | 0716c63 | 2011-04-12 18:32:06 +0000 | [diff] [blame] | 233 | info.fPostTranslate = buffer.readBool(); |
Mike Reed | 22cada0 | 2021-08-09 10:46:33 -0400 | [diff] [blame] | 234 | *builder.addLayerOnTop(info) = buffer.readPaint(); |
Kevin Lubick | daebae9 | 2018-05-17 11:29:10 -0400 | [diff] [blame] | 235 | if (!buffer.isValid()) { |
| 236 | return nullptr; |
| 237 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 238 | } |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 239 | return builder.detach(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 240 | } |
robertphillips@google.com | 4991b8f | 2013-01-28 20:21:59 +0000 | [diff] [blame] | 241 | |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame] | 242 | SkLayerDrawLooper::Builder::Builder() |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 243 | : fRecs(nullptr), |
| 244 | fTopRec(nullptr), |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame] | 245 | fCount(0) { |
| 246 | } |
| 247 | |
| 248 | SkLayerDrawLooper::Builder::~Builder() { |
| 249 | Rec* rec = fRecs; |
| 250 | while (rec) { |
| 251 | Rec* next = rec->fNext; |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 252 | delete rec; |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame] | 253 | rec = next; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | SkPaint* SkLayerDrawLooper::Builder::addLayer(const LayerInfo& info) { |
| 258 | fCount += 1; |
| 259 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 260 | Rec* rec = new Rec; |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame] | 261 | rec->fNext = fRecs; |
| 262 | rec->fInfo = info; |
| 263 | fRecs = rec; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 264 | if (nullptr == fTopRec) { |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame] | 265 | fTopRec = rec; |
| 266 | } |
| 267 | |
| 268 | return &rec->fPaint; |
| 269 | } |
| 270 | |
| 271 | void SkLayerDrawLooper::Builder::addLayer(SkScalar dx, SkScalar dy) { |
| 272 | LayerInfo info; |
| 273 | |
| 274 | info.fOffset.set(dx, dy); |
| 275 | (void)this->addLayer(info); |
| 276 | } |
| 277 | |
| 278 | SkPaint* SkLayerDrawLooper::Builder::addLayerOnTop(const LayerInfo& info) { |
| 279 | fCount += 1; |
| 280 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 281 | Rec* rec = new Rec; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 282 | rec->fNext = nullptr; |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame] | 283 | rec->fInfo = info; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 284 | if (nullptr == fRecs) { |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame] | 285 | fRecs = rec; |
| 286 | } else { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 287 | SkASSERT(fTopRec); |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame] | 288 | fTopRec->fNext = rec; |
| 289 | } |
| 290 | fTopRec = rec; |
| 291 | |
| 292 | return &rec->fPaint; |
| 293 | } |
| 294 | |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 295 | sk_sp<SkDrawLooper> SkLayerDrawLooper::Builder::detach() { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 296 | SkLayerDrawLooper* looper = new SkLayerDrawLooper; |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame] | 297 | looper->fCount = fCount; |
| 298 | looper->fRecs = fRecs; |
| 299 | |
| 300 | fCount = 0; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 301 | fRecs = nullptr; |
| 302 | fTopRec = nullptr; |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame] | 303 | |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 304 | return sk_sp<SkDrawLooper>(looper); |
commit-bot@chromium.org | 74ba2f6 | 2014-02-14 10:06:42 +0000 | [diff] [blame] | 305 | } |
Matt Sarett | 56ea77a | 2017-03-28 15:27:06 -0400 | [diff] [blame] | 306 | |
| 307 | sk_sp<SkDrawLooper> SkBlurDrawLooper::Make(SkColor color, SkScalar sigma, SkScalar dx, SkScalar dy) |
| 308 | { |
Leon Scroggins III | e548161 | 2019-01-09 15:57:34 -0500 | [diff] [blame] | 309 | return Make(SkColor4f::FromColor(color), sk_srgb_singleton(), sigma, dx, dy); |
| 310 | } |
| 311 | |
| 312 | sk_sp<SkDrawLooper> SkBlurDrawLooper::Make(SkColor4f color, SkColorSpace* cs, |
| 313 | SkScalar sigma, SkScalar dx, SkScalar dy) |
| 314 | { |
Matt Sarett | 56ea77a | 2017-03-28 15:27:06 -0400 | [diff] [blame] | 315 | sk_sp<SkMaskFilter> blur = nullptr; |
| 316 | if (sigma > 0.0f) { |
Mike Reed | 18e7556 | 2018-03-12 14:03:47 -0400 | [diff] [blame] | 317 | blur = SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, sigma, true); |
Matt Sarett | 56ea77a | 2017-03-28 15:27:06 -0400 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | SkLayerDrawLooper::Builder builder; |
| 321 | |
| 322 | // First layer |
| 323 | SkLayerDrawLooper::LayerInfo defaultLayer; |
| 324 | builder.addLayer(defaultLayer); |
| 325 | |
| 326 | // Blur layer |
| 327 | SkLayerDrawLooper::LayerInfo blurInfo; |
| 328 | blurInfo.fColorMode = SkBlendMode::kSrc; |
| 329 | blurInfo.fPaintBits = SkLayerDrawLooper::kMaskFilter_Bit; |
| 330 | blurInfo.fOffset = SkVector::Make(dx, dy); |
| 331 | SkPaint* paint = builder.addLayer(blurInfo); |
| 332 | paint->setMaskFilter(std::move(blur)); |
Leon Scroggins III | e548161 | 2019-01-09 15:57:34 -0500 | [diff] [blame] | 333 | paint->setColor4f(color, cs); |
Matt Sarett | 56ea77a | 2017-03-28 15:27:06 -0400 | [diff] [blame] | 334 | |
| 335 | return builder.detach(); |
| 336 | } |
Mike Reed | 1d62221 | 2021-02-04 20:11:25 -0500 | [diff] [blame] | 337 | |
| 338 | #endif |