epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Mike Reed | 80747ef | 2018-01-23 15:29:32 -0500 | [diff] [blame] | 8 | #include "SkMaskFilterBase.h" |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 9 | |
| 10 | #include "SkAutoMalloc.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 11 | #include "SkBlitter.h" |
Mike Reed | 18e7556 | 2018-03-12 14:03:47 -0400 | [diff] [blame] | 12 | #include "SkBlurPriv.h" |
Kevin Lubick | c456b73 | 2017-01-11 17:21:57 +0000 | [diff] [blame] | 13 | #include "SkCachedData.h" |
Mike Reed | 1bd556a | 2018-01-26 11:42:38 -0500 | [diff] [blame] | 14 | #include "SkCoverageModePriv.h" |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 15 | #include "SkDraw.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 16 | #include "SkPath.h" |
Kevin Lubick | c456b73 | 2017-01-11 17:21:57 +0000 | [diff] [blame] | 17 | #include "SkRRect.h" |
Hal Canary | 95e3c05 | 2017-01-11 12:44:43 -0500 | [diff] [blame] | 18 | #include "SkRasterClip.h" |
Mike Reed | 20dc672 | 2018-01-24 16:34:02 -0500 | [diff] [blame] | 19 | #include "SkReadBuffer.h" |
Mike Reed | 20dc672 | 2018-01-24 16:34:02 -0500 | [diff] [blame] | 20 | #include "SkWriteBuffer.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 21 | |
robertphillips@google.com | 0b82852 | 2013-07-03 15:49:05 +0000 | [diff] [blame] | 22 | #if SK_SUPPORT_GPU |
Robert Phillips | 4a24da5 | 2016-12-14 09:00:07 -0500 | [diff] [blame] | 23 | #include "GrTextureProxy.h" |
Mike Reed | bfadcf0 | 2018-01-20 22:24:21 +0000 | [diff] [blame] | 24 | #include "GrFragmentProcessor.h" |
Mike Reed | 547c859 | 2018-02-05 15:59:23 -0500 | [diff] [blame] | 25 | #include "effects/GrXfermodeFragmentProcessor.h" |
Jim Van Verth | d401da6 | 2018-05-03 10:40:30 -0400 | [diff] [blame] | 26 | #include "text/GrSDFMaskFilter.h" |
robertphillips@google.com | 0b82852 | 2013-07-03 15:49:05 +0000 | [diff] [blame] | 27 | #endif |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 28 | |
Mike Reed | 80747ef | 2018-01-23 15:29:32 -0500 | [diff] [blame] | 29 | SkMaskFilterBase::NinePatch::~NinePatch() { |
reed | b0df8be | 2015-02-04 09:07:17 -0800 | [diff] [blame] | 30 | if (fCache) { |
| 31 | SkASSERT((const void*)fMask.fImage == fCache->data()); |
| 32 | fCache->unref(); |
| 33 | } else { |
| 34 | SkMask::FreeImage(fMask.fImage); |
| 35 | } |
| 36 | } |
| 37 | |
Mike Reed | 80747ef | 2018-01-23 15:29:32 -0500 | [diff] [blame] | 38 | bool SkMaskFilterBase::asABlur(BlurRec*) const { |
reed@google.com | daaafa6 | 2014-04-29 15:20:16 +0000 | [diff] [blame] | 39 | return false; |
| 40 | } |
| 41 | |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 42 | static void extractMaskSubset(const SkMask& src, SkMask* dst) { |
| 43 | SkASSERT(src.fBounds.contains(dst->fBounds)); |
| 44 | |
| 45 | const int dx = dst->fBounds.left() - src.fBounds.left(); |
| 46 | const int dy = dst->fBounds.top() - src.fBounds.top(); |
| 47 | dst->fImage = src.fImage + dy * src.fRowBytes + dx; |
| 48 | dst->fRowBytes = src.fRowBytes; |
| 49 | dst->fFormat = src.fFormat; |
| 50 | } |
| 51 | |
| 52 | static void blitClippedMask(SkBlitter* blitter, const SkMask& mask, |
| 53 | const SkIRect& bounds, const SkIRect& clipR) { |
| 54 | SkIRect r; |
| 55 | if (r.intersect(bounds, clipR)) { |
| 56 | blitter->blitMask(mask, r); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | static void blitClippedRect(SkBlitter* blitter, const SkIRect& rect, const SkIRect& clipR) { |
| 61 | SkIRect r; |
| 62 | if (r.intersect(rect, clipR)) { |
| 63 | blitter->blitRect(r.left(), r.top(), r.width(), r.height()); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | #if 0 |
| 68 | static void dump(const SkMask& mask) { |
| 69 | for (int y = mask.fBounds.top(); y < mask.fBounds.bottom(); ++y) { |
| 70 | for (int x = mask.fBounds.left(); x < mask.fBounds.right(); ++x) { |
| 71 | SkDebugf("%02X", *mask.getAddr8(x, y)); |
| 72 | } |
| 73 | SkDebugf("\n"); |
| 74 | } |
| 75 | SkDebugf("\n"); |
| 76 | } |
| 77 | #endif |
| 78 | |
| 79 | static void draw_nine_clipped(const SkMask& mask, const SkIRect& outerR, |
reed@google.com | dab9b4f | 2012-11-19 16:45:14 +0000 | [diff] [blame] | 80 | const SkIPoint& center, bool fillCenter, |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 81 | const SkIRect& clipR, SkBlitter* blitter) { |
reed@google.com | dab9b4f | 2012-11-19 16:45:14 +0000 | [diff] [blame] | 82 | int cx = center.x(); |
| 83 | int cy = center.y(); |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 84 | SkMask m; |
skia.committer@gmail.com | 453995e | 2012-11-10 02:01:26 +0000 | [diff] [blame] | 85 | |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 86 | // top-left |
| 87 | m.fBounds = mask.fBounds; |
| 88 | m.fBounds.fRight = cx; |
| 89 | m.fBounds.fBottom = cy; |
tomhudson@google.com | 3c0ecc5 | 2013-04-25 16:56:40 +0000 | [diff] [blame] | 90 | if (m.fBounds.width() > 0 && m.fBounds.height() > 0) { |
| 91 | extractMaskSubset(mask, &m); |
| 92 | m.fBounds.offsetTo(outerR.left(), outerR.top()); |
| 93 | blitClippedMask(blitter, m, m.fBounds, clipR); |
| 94 | } |
skia.committer@gmail.com | 453995e | 2012-11-10 02:01:26 +0000 | [diff] [blame] | 95 | |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 96 | // top-right |
| 97 | m.fBounds = mask.fBounds; |
| 98 | m.fBounds.fLeft = cx + 1; |
| 99 | m.fBounds.fBottom = cy; |
tomhudson@google.com | 3c0ecc5 | 2013-04-25 16:56:40 +0000 | [diff] [blame] | 100 | if (m.fBounds.width() > 0 && m.fBounds.height() > 0) { |
| 101 | extractMaskSubset(mask, &m); |
| 102 | m.fBounds.offsetTo(outerR.right() - m.fBounds.width(), outerR.top()); |
| 103 | blitClippedMask(blitter, m, m.fBounds, clipR); |
| 104 | } |
skia.committer@gmail.com | 453995e | 2012-11-10 02:01:26 +0000 | [diff] [blame] | 105 | |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 106 | // bottom-left |
| 107 | m.fBounds = mask.fBounds; |
| 108 | m.fBounds.fRight = cx; |
| 109 | m.fBounds.fTop = cy + 1; |
tomhudson@google.com | 3c0ecc5 | 2013-04-25 16:56:40 +0000 | [diff] [blame] | 110 | if (m.fBounds.width() > 0 && m.fBounds.height() > 0) { |
| 111 | extractMaskSubset(mask, &m); |
| 112 | m.fBounds.offsetTo(outerR.left(), outerR.bottom() - m.fBounds.height()); |
| 113 | blitClippedMask(blitter, m, m.fBounds, clipR); |
| 114 | } |
skia.committer@gmail.com | 453995e | 2012-11-10 02:01:26 +0000 | [diff] [blame] | 115 | |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 116 | // bottom-right |
| 117 | m.fBounds = mask.fBounds; |
| 118 | m.fBounds.fLeft = cx + 1; |
| 119 | m.fBounds.fTop = cy + 1; |
tomhudson@google.com | 3c0ecc5 | 2013-04-25 16:56:40 +0000 | [diff] [blame] | 120 | if (m.fBounds.width() > 0 && m.fBounds.height() > 0) { |
| 121 | extractMaskSubset(mask, &m); |
| 122 | m.fBounds.offsetTo(outerR.right() - m.fBounds.width(), |
| 123 | outerR.bottom() - m.fBounds.height()); |
| 124 | blitClippedMask(blitter, m, m.fBounds, clipR); |
| 125 | } |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 126 | |
| 127 | SkIRect innerR; |
| 128 | innerR.set(outerR.left() + cx - mask.fBounds.left(), |
| 129 | outerR.top() + cy - mask.fBounds.top(), |
| 130 | outerR.right() + (cx + 1 - mask.fBounds.right()), |
| 131 | outerR.bottom() + (cy + 1 - mask.fBounds.bottom())); |
reed@google.com | dab9b4f | 2012-11-19 16:45:14 +0000 | [diff] [blame] | 132 | if (fillCenter) { |
| 133 | blitClippedRect(blitter, innerR, clipR); |
| 134 | } |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 135 | |
| 136 | const int innerW = innerR.width(); |
| 137 | size_t storageSize = (innerW + 1) * (sizeof(int16_t) + sizeof(uint8_t)); |
| 138 | SkAutoSMalloc<4*1024> storage(storageSize); |
| 139 | int16_t* runs = (int16_t*)storage.get(); |
| 140 | uint8_t* alpha = (uint8_t*)(runs + innerW + 1); |
| 141 | |
| 142 | SkIRect r; |
| 143 | // top |
| 144 | r.set(innerR.left(), outerR.top(), innerR.right(), innerR.top()); |
| 145 | if (r.intersect(clipR)) { |
| 146 | int startY = SkMax32(0, r.top() - outerR.top()); |
| 147 | int stopY = startY + r.height(); |
| 148 | int width = r.width(); |
| 149 | for (int y = startY; y < stopY; ++y) { |
| 150 | runs[0] = width; |
| 151 | runs[width] = 0; |
| 152 | alpha[0] = *mask.getAddr8(cx, mask.fBounds.top() + y); |
| 153 | blitter->blitAntiH(r.left(), outerR.top() + y, alpha, runs); |
| 154 | } |
| 155 | } |
| 156 | // bottom |
| 157 | r.set(innerR.left(), innerR.bottom(), innerR.right(), outerR.bottom()); |
| 158 | if (r.intersect(clipR)) { |
| 159 | int startY = outerR.bottom() - r.bottom(); |
| 160 | int stopY = startY + r.height(); |
| 161 | int width = r.width(); |
| 162 | for (int y = startY; y < stopY; ++y) { |
| 163 | runs[0] = width; |
| 164 | runs[width] = 0; |
| 165 | alpha[0] = *mask.getAddr8(cx, mask.fBounds.bottom() - y - 1); |
| 166 | blitter->blitAntiH(r.left(), outerR.bottom() - y - 1, alpha, runs); |
| 167 | } |
| 168 | } |
| 169 | // left |
| 170 | r.set(outerR.left(), innerR.top(), innerR.left(), innerR.bottom()); |
| 171 | if (r.intersect(clipR)) { |
Mike Reed | bb3bf14 | 2017-06-29 08:43:43 -0400 | [diff] [blame] | 172 | SkMask m; |
| 173 | m.fImage = mask.getAddr8(mask.fBounds.left() + r.left() - outerR.left(), |
| 174 | mask.fBounds.top() + cy); |
| 175 | m.fBounds = r; |
| 176 | m.fRowBytes = 0; // so we repeat the scanline for our height |
| 177 | m.fFormat = SkMask::kA8_Format; |
| 178 | blitter->blitMask(m, r); |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 179 | } |
| 180 | // right |
| 181 | r.set(innerR.right(), innerR.top(), outerR.right(), innerR.bottom()); |
| 182 | if (r.intersect(clipR)) { |
Mike Reed | bb3bf14 | 2017-06-29 08:43:43 -0400 | [diff] [blame] | 183 | SkMask m; |
| 184 | m.fImage = mask.getAddr8(mask.fBounds.right() - outerR.right() + r.left(), |
| 185 | mask.fBounds.top() + cy); |
| 186 | m.fBounds = r; |
| 187 | m.fRowBytes = 0; // so we repeat the scanline for our height |
| 188 | m.fFormat = SkMask::kA8_Format; |
| 189 | blitter->blitMask(m, r); |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
reed | 868074b | 2014-06-03 10:53:59 -0700 | [diff] [blame] | 193 | static void draw_nine(const SkMask& mask, const SkIRect& outerR, const SkIPoint& center, |
| 194 | bool fillCenter, const SkRasterClip& clip, SkBlitter* blitter) { |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 195 | // if we get here, we need to (possibly) resolve the clip and blitter |
| 196 | SkAAClipBlitterWrapper wrapper(clip, blitter); |
| 197 | blitter = wrapper.getBlitter(); |
skia.committer@gmail.com | 453995e | 2012-11-10 02:01:26 +0000 | [diff] [blame] | 198 | |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 199 | SkRegion::Cliperator clipper(wrapper.getRgn(), outerR); |
skia.committer@gmail.com | 453995e | 2012-11-10 02:01:26 +0000 | [diff] [blame] | 200 | |
reed | 868074b | 2014-06-03 10:53:59 -0700 | [diff] [blame] | 201 | if (!clipper.done()) { |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 202 | const SkIRect& cr = clipper.rect(); |
| 203 | do { |
reed@google.com | dab9b4f | 2012-11-19 16:45:14 +0000 | [diff] [blame] | 204 | draw_nine_clipped(mask, outerR, center, fillCenter, cr, blitter); |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 205 | clipper.next(); |
| 206 | } while (!clipper.done()); |
| 207 | } |
| 208 | } |
| 209 | |
reed@google.com | dab9b4f | 2012-11-19 16:45:14 +0000 | [diff] [blame] | 210 | static int countNestedRects(const SkPath& path, SkRect rects[2]) { |
caryclark | 95bc5f3 | 2015-04-08 08:34:15 -0700 | [diff] [blame] | 211 | if (path.isNestedFillRects(rects)) { |
reed@google.com | dab9b4f | 2012-11-19 16:45:14 +0000 | [diff] [blame] | 212 | return 2; |
| 213 | } |
| 214 | return path.isRect(&rects[0]); |
| 215 | } |
| 216 | |
Mike Reed | 80747ef | 2018-01-23 15:29:32 -0500 | [diff] [blame] | 217 | bool SkMaskFilterBase::filterRRect(const SkRRect& devRRect, const SkMatrix& matrix, |
| 218 | const SkRasterClip& clip, SkBlitter* blitter) const { |
scroggo@google.com | a8e33a9 | 2013-11-08 18:02:53 +0000 | [diff] [blame] | 219 | // Attempt to speed up drawing by creating a nine patch. If a nine patch |
| 220 | // cannot be used, return false to allow our caller to recover and perform |
| 221 | // the drawing another way. |
| 222 | NinePatch patch; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 223 | patch.fMask.fImage = nullptr; |
scroggo@google.com | a8e33a9 | 2013-11-08 18:02:53 +0000 | [diff] [blame] | 224 | if (kTrue_FilterReturn != this->filterRRectToNine(devRRect, matrix, |
| 225 | clip.getBounds(), |
| 226 | &patch)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 227 | SkASSERT(nullptr == patch.fMask.fImage); |
scroggo@google.com | a8e33a9 | 2013-11-08 18:02:53 +0000 | [diff] [blame] | 228 | return false; |
| 229 | } |
reed | 868074b | 2014-06-03 10:53:59 -0700 | [diff] [blame] | 230 | draw_nine(patch.fMask, patch.fOuterRect, patch.fCenter, true, clip, blitter); |
scroggo@google.com | a8e33a9 | 2013-11-08 18:02:53 +0000 | [diff] [blame] | 231 | return true; |
| 232 | } |
| 233 | |
Mike Reed | 80747ef | 2018-01-23 15:29:32 -0500 | [diff] [blame] | 234 | bool SkMaskFilterBase::filterPath(const SkPath& devPath, const SkMatrix& matrix, |
| 235 | const SkRasterClip& clip, SkBlitter* blitter, |
| 236 | SkStrokeRec::InitStyle style) const { |
reed@google.com | dab9b4f | 2012-11-19 16:45:14 +0000 | [diff] [blame] | 237 | SkRect rects[2]; |
| 238 | int rectCount = 0; |
bsalomon | 055e192 | 2016-05-06 07:22:58 -0700 | [diff] [blame] | 239 | if (SkStrokeRec::kFill_InitStyle == style) { |
reed@google.com | dab9b4f | 2012-11-19 16:45:14 +0000 | [diff] [blame] | 240 | rectCount = countNestedRects(devPath, rects); |
| 241 | } |
| 242 | if (rectCount > 0) { |
| 243 | NinePatch patch; |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 244 | |
reed | b0df8be | 2015-02-04 09:07:17 -0800 | [diff] [blame] | 245 | switch (this->filterRectsToNine(rects, rectCount, matrix, clip.getBounds(), &patch)) { |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 246 | case kFalse_FilterReturn: |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 247 | SkASSERT(nullptr == patch.fMask.fImage); |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 248 | return false; |
reed@google.com | dab9b4f | 2012-11-19 16:45:14 +0000 | [diff] [blame] | 249 | |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 250 | case kTrue_FilterReturn: |
reed | 868074b | 2014-06-03 10:53:59 -0700 | [diff] [blame] | 251 | draw_nine(patch.fMask, patch.fOuterRect, patch.fCenter, 1 == rectCount, clip, |
| 252 | blitter); |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 253 | return true; |
reed@google.com | dab9b4f | 2012-11-19 16:45:14 +0000 | [diff] [blame] | 254 | |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 255 | case kUnimplemented_FilterReturn: |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 256 | SkASSERT(nullptr == patch.fMask.fImage); |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 257 | // fall through |
| 258 | break; |
| 259 | } |
| 260 | } |
| 261 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 262 | SkMask srcM, dstM; |
| 263 | |
| 264 | if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), this, &matrix, &srcM, |
junov@chromium.org | 2ac4ef5 | 2012-04-04 15:16:51 +0000 | [diff] [blame] | 265 | SkMask::kComputeBoundsAndRenderImage_CreateMode, |
| 266 | style)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 267 | return false; |
| 268 | } |
bungeman@google.com | 02f5584 | 2011-10-04 21:25:00 +0000 | [diff] [blame] | 269 | SkAutoMaskFreeImage autoSrc(srcM.fImage); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 270 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 271 | if (!this->filterMask(&dstM, srcM, matrix, nullptr)) { |
qiankun.miao | eabd0d7 | 2015-01-07 19:20:49 -0800 | [diff] [blame] | 272 | return false; |
reed@google.com | feb8cc8 | 2011-04-19 20:11:25 +0000 | [diff] [blame] | 273 | } |
bungeman@google.com | 02f5584 | 2011-10-04 21:25:00 +0000 | [diff] [blame] | 274 | SkAutoMaskFreeImage autoDst(dstM.fImage); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 275 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 276 | // if we get here, we need to (possibly) resolve the clip and blitter |
| 277 | SkAAClipBlitterWrapper wrapper(clip, blitter); |
| 278 | blitter = wrapper.getBlitter(); |
| 279 | |
| 280 | SkRegion::Cliperator clipper(wrapper.getRgn(), dstM.fBounds); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 281 | |
reed | 868074b | 2014-06-03 10:53:59 -0700 | [diff] [blame] | 282 | if (!clipper.done()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 283 | const SkIRect& cr = clipper.rect(); |
| 284 | do { |
| 285 | blitter->blitMask(dstM, cr); |
| 286 | clipper.next(); |
| 287 | } while (!clipper.done()); |
| 288 | } |
| 289 | |
| 290 | return true; |
| 291 | } |
| 292 | |
Mike Reed | 80747ef | 2018-01-23 15:29:32 -0500 | [diff] [blame] | 293 | SkMaskFilterBase::FilterReturn |
| 294 | SkMaskFilterBase::filterRRectToNine(const SkRRect&, const SkMatrix&, |
| 295 | const SkIRect& clipBounds, NinePatch*) const { |
scroggo@google.com | a8e33a9 | 2013-11-08 18:02:53 +0000 | [diff] [blame] | 296 | return kUnimplemented_FilterReturn; |
| 297 | } |
| 298 | |
Mike Reed | 80747ef | 2018-01-23 15:29:32 -0500 | [diff] [blame] | 299 | SkMaskFilterBase::FilterReturn |
| 300 | SkMaskFilterBase::filterRectsToNine(const SkRect[], int count, const SkMatrix&, |
| 301 | const SkIRect& clipBounds, NinePatch*) const { |
reed@google.com | d729b3e | 2012-11-09 14:30:48 +0000 | [diff] [blame] | 302 | return kUnimplemented_FilterReturn; |
| 303 | } |
| 304 | |
robertphillips@google.com | 0b82852 | 2013-07-03 15:49:05 +0000 | [diff] [blame] | 305 | #if SK_SUPPORT_GPU |
Mike Reed | 80747ef | 2018-01-23 15:29:32 -0500 | [diff] [blame] | 306 | std::unique_ptr<GrFragmentProcessor> |
| 307 | SkMaskFilterBase::asFragmentProcessor(const GrFPArgs& args) const { |
Mike Reed | bfadcf0 | 2018-01-20 22:24:21 +0000 | [diff] [blame] | 308 | auto fp = this->onAsFragmentProcessor(args); |
| 309 | if (fp) { |
| 310 | SkASSERT(this->hasFragmentProcessor()); |
| 311 | } else { |
| 312 | SkASSERT(!this->hasFragmentProcessor()); |
| 313 | } |
| 314 | return fp; |
| 315 | } |
Mike Reed | 80747ef | 2018-01-23 15:29:32 -0500 | [diff] [blame] | 316 | bool SkMaskFilterBase::hasFragmentProcessor() const { |
Mike Reed | bfadcf0 | 2018-01-20 22:24:21 +0000 | [diff] [blame] | 317 | return this->onHasFragmentProcessor(); |
| 318 | } |
| 319 | |
Mike Reed | 80747ef | 2018-01-23 15:29:32 -0500 | [diff] [blame] | 320 | std::unique_ptr<GrFragmentProcessor> |
| 321 | SkMaskFilterBase::onAsFragmentProcessor(const GrFPArgs&) const { |
Mike Reed | bfadcf0 | 2018-01-20 22:24:21 +0000 | [diff] [blame] | 322 | return nullptr; |
| 323 | } |
Mike Reed | 80747ef | 2018-01-23 15:29:32 -0500 | [diff] [blame] | 324 | bool SkMaskFilterBase::onHasFragmentProcessor() const { return false; } |
Mike Reed | bfadcf0 | 2018-01-20 22:24:21 +0000 | [diff] [blame] | 325 | |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 326 | bool SkMaskFilterBase::canFilterMaskGPU(const GrShape& shape, |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 327 | const SkIRect& devSpaceShapeBounds, |
Mike Reed | 80747ef | 2018-01-23 15:29:32 -0500 | [diff] [blame] | 328 | const SkIRect& clipBounds, |
| 329 | const SkMatrix& ctm, |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 330 | SkIRect* maskRect) const { |
robertphillips@google.com | 4914931 | 2013-07-03 15:34:35 +0000 | [diff] [blame] | 331 | return false; |
| 332 | } |
| 333 | |
Mike Reed | 80747ef | 2018-01-23 15:29:32 -0500 | [diff] [blame] | 334 | bool SkMaskFilterBase::directFilterMaskGPU(GrContext*, |
Robert Phillips | 20390c3 | 2018-08-17 11:01:03 -0400 | [diff] [blame] | 335 | GrRenderTargetContext*, |
Mike Reed | 80747ef | 2018-01-23 15:29:32 -0500 | [diff] [blame] | 336 | GrPaint&&, |
| 337 | const GrClip&, |
| 338 | const SkMatrix& viewMatrix, |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 339 | const GrShape&) const { |
commit-bot@chromium.org | cf34bc0 | 2014-01-30 15:34:43 +0000 | [diff] [blame] | 340 | return false; |
| 341 | } |
| 342 | |
Mike Reed | 80747ef | 2018-01-23 15:29:32 -0500 | [diff] [blame] | 343 | sk_sp<GrTextureProxy> SkMaskFilterBase::filterMaskGPU(GrContext*, |
| 344 | sk_sp<GrTextureProxy> srcProxy, |
| 345 | const SkMatrix& ctm, |
| 346 | const SkIRect& maskRect) const { |
Robert Phillips | 4a24da5 | 2016-12-14 09:00:07 -0500 | [diff] [blame] | 347 | return nullptr; |
reed@google.com | 2b75f42 | 2011-07-07 13:43:38 +0000 | [diff] [blame] | 348 | } |
robertphillips@google.com | 0b82852 | 2013-07-03 15:49:05 +0000 | [diff] [blame] | 349 | #endif |
reed@google.com | 2b75f42 | 2011-07-07 13:43:38 +0000 | [diff] [blame] | 350 | |
Mike Reed | 80747ef | 2018-01-23 15:29:32 -0500 | [diff] [blame] | 351 | void SkMaskFilterBase::computeFastBounds(const SkRect& src, SkRect* dst) const { |
reed@google.com | 9efd9a0 | 2012-01-30 15:41:43 +0000 | [diff] [blame] | 352 | SkMask srcM, dstM; |
| 353 | |
reed | b07a94f | 2014-11-19 05:03:18 -0800 | [diff] [blame] | 354 | srcM.fBounds = src.roundOut(); |
reed@google.com | 9efd9a0 | 2012-01-30 15:41:43 +0000 | [diff] [blame] | 355 | srcM.fRowBytes = 0; |
| 356 | srcM.fFormat = SkMask::kA8_Format; |
| 357 | |
| 358 | SkIPoint margin; // ignored |
| 359 | if (this->filterMask(&dstM, srcM, SkMatrix::I(), &margin)) { |
| 360 | dst->set(dstM.fBounds); |
| 361 | } else { |
| 362 | dst->set(srcM.fBounds); |
| 363 | } |
| 364 | } |
Mike Reed | 20dc672 | 2018-01-24 16:34:02 -0500 | [diff] [blame] | 365 | |
| 366 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 367 | |
| 368 | template <typename T> static inline T join(const T& a, const T& b) { |
| 369 | T r = a; |
| 370 | r.join(b); |
| 371 | return r; |
| 372 | } |
Mike Reed | 1bd556a | 2018-01-26 11:42:38 -0500 | [diff] [blame] | 373 | template <typename T> static inline T sect(const T& a, const T& b) { |
| 374 | T r = a; |
| 375 | return r.intersect(b) ? r : T::MakeEmpty(); |
| 376 | } |
Mike Reed | 20dc672 | 2018-01-24 16:34:02 -0500 | [diff] [blame] | 377 | |
| 378 | class SkComposeMF : public SkMaskFilterBase { |
| 379 | public: |
| 380 | SkComposeMF(sk_sp<SkMaskFilter> outer, sk_sp<SkMaskFilter> inner) |
| 381 | : fOuter(std::move(outer)) |
| 382 | , fInner(std::move(inner)) |
| 383 | { |
| 384 | SkASSERT(as_MFB(fOuter)->getFormat() == SkMask::kA8_Format); |
| 385 | SkASSERT(as_MFB(fInner)->getFormat() == SkMask::kA8_Format); |
| 386 | } |
| 387 | |
| 388 | bool filterMask(SkMask* dst, const SkMask& src, const SkMatrix&, SkIPoint*) const override; |
| 389 | |
| 390 | void computeFastBounds(const SkRect& src, SkRect* dst) const override { |
| 391 | SkRect tmp; |
| 392 | as_MFB(fInner)->computeFastBounds(src, &tmp); |
| 393 | as_MFB(fOuter)->computeFastBounds(tmp, dst); |
| 394 | } |
| 395 | |
| 396 | SkMask::Format getFormat() const override { return SkMask::kA8_Format; } |
Mike Reed | 20dc672 | 2018-01-24 16:34:02 -0500 | [diff] [blame] | 397 | |
Mike Reed | 547c859 | 2018-02-05 15:59:23 -0500 | [diff] [blame] | 398 | protected: |
| 399 | #if SK_SUPPORT_GPU |
| 400 | std::unique_ptr<GrFragmentProcessor> onAsFragmentProcessor(const GrFPArgs& args) const override{ |
| 401 | std::unique_ptr<GrFragmentProcessor> array[2] = { |
| 402 | as_MFB(fInner)->asFragmentProcessor(args), |
| 403 | as_MFB(fOuter)->asFragmentProcessor(args), |
| 404 | }; |
| 405 | if (!array[0] || !array[1]) { |
| 406 | return nullptr; |
| 407 | } |
| 408 | return GrFragmentProcessor::RunInSeries(array, 2); |
| 409 | } |
| 410 | |
| 411 | bool onHasFragmentProcessor() const override { |
| 412 | return as_MFB(fInner)->hasFragmentProcessor() && as_MFB(fOuter)->hasFragmentProcessor(); |
| 413 | } |
| 414 | #endif |
| 415 | |
Mike Reed | 20dc672 | 2018-01-24 16:34:02 -0500 | [diff] [blame] | 416 | private: |
Mike Klein | 4fee323 | 2018-10-18 17:27:16 -0400 | [diff] [blame^] | 417 | SK_FLATTENABLE_HOOKS(SkComposeMF) |
| 418 | |
Mike Reed | 20dc672 | 2018-01-24 16:34:02 -0500 | [diff] [blame] | 419 | sk_sp<SkMaskFilter> fOuter; |
| 420 | sk_sp<SkMaskFilter> fInner; |
| 421 | |
| 422 | void flatten(SkWriteBuffer&) const override; |
| 423 | |
| 424 | friend class SkMaskFilter; |
| 425 | |
| 426 | typedef SkMaskFilterBase INHERITED; |
| 427 | }; |
| 428 | |
| 429 | bool SkComposeMF::filterMask(SkMask* dst, const SkMask& src, const SkMatrix& ctm, |
| 430 | SkIPoint* margin) const { |
| 431 | SkIPoint innerMargin; |
| 432 | SkMask innerMask; |
| 433 | |
| 434 | if (!as_MFB(fInner)->filterMask(&innerMask, src, ctm, &innerMargin)) { |
| 435 | return false; |
| 436 | } |
| 437 | if (!as_MFB(fOuter)->filterMask(dst, innerMask, ctm, margin)) { |
| 438 | return false; |
| 439 | } |
| 440 | if (margin) { |
| 441 | margin->fX += innerMargin.fX; |
| 442 | margin->fY += innerMargin.fY; |
| 443 | } |
| 444 | sk_free(innerMask.fImage); |
| 445 | return true; |
| 446 | } |
| 447 | |
| 448 | void SkComposeMF::flatten(SkWriteBuffer & buffer) const { |
| 449 | buffer.writeFlattenable(fOuter.get()); |
| 450 | buffer.writeFlattenable(fInner.get()); |
| 451 | } |
| 452 | |
| 453 | sk_sp<SkFlattenable> SkComposeMF::CreateProc(SkReadBuffer& buffer) { |
| 454 | auto outer = buffer.readMaskFilter(); |
| 455 | auto inner = buffer.readMaskFilter(); |
| 456 | if (!buffer.validate(outer && inner)) { |
| 457 | return nullptr; |
| 458 | } |
| 459 | return SkMaskFilter::MakeCompose(std::move(outer), std::move(inner)); |
| 460 | } |
| 461 | |
Mike Reed | 20dc672 | 2018-01-24 16:34:02 -0500 | [diff] [blame] | 462 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 463 | |
| 464 | class SkCombineMF : public SkMaskFilterBase { |
| 465 | public: |
Mike Reed | 1bd556a | 2018-01-26 11:42:38 -0500 | [diff] [blame] | 466 | SkCombineMF(sk_sp<SkMaskFilter> dst, sk_sp<SkMaskFilter> src, SkCoverageMode mode) |
Mike Reed | 20dc672 | 2018-01-24 16:34:02 -0500 | [diff] [blame] | 467 | : fDst(std::move(dst)) |
| 468 | , fSrc(std::move(src)) |
| 469 | , fMode(mode) |
| 470 | { |
| 471 | SkASSERT(as_MFB(fSrc)->getFormat() == SkMask::kA8_Format); |
| 472 | SkASSERT(as_MFB(fDst)->getFormat() == SkMask::kA8_Format); |
| 473 | } |
| 474 | |
| 475 | bool filterMask(SkMask* dst, const SkMask& src, const SkMatrix&, SkIPoint*) const override; |
| 476 | |
| 477 | void computeFastBounds(const SkRect& src, SkRect* dst) const override { |
| 478 | SkRect srcR, dstR; |
| 479 | as_MFB(fSrc)->computeFastBounds(src, &srcR); |
| 480 | as_MFB(fDst)->computeFastBounds(src, &dstR); |
| 481 | *dst = join(srcR, dstR); |
| 482 | } |
| 483 | |
| 484 | SkMask::Format getFormat() const override { return SkMask::kA8_Format; } |
Mike Reed | 1bd556a | 2018-01-26 11:42:38 -0500 | [diff] [blame] | 485 | |
Mike Klein | 4fee323 | 2018-10-18 17:27:16 -0400 | [diff] [blame^] | 486 | SK_FLATTENABLE_HOOKS(SkCombineMF) |
Mike Reed | 20dc672 | 2018-01-24 16:34:02 -0500 | [diff] [blame] | 487 | |
Mike Reed | 547c859 | 2018-02-05 15:59:23 -0500 | [diff] [blame] | 488 | protected: |
| 489 | #if SK_SUPPORT_GPU |
| 490 | std::unique_ptr<GrFragmentProcessor> onAsFragmentProcessor(const GrFPArgs& args) const override{ |
| 491 | auto src = as_MFB(fSrc)->asFragmentProcessor(args); |
| 492 | auto dst = as_MFB(fDst)->asFragmentProcessor(args); |
| 493 | if (!src || !dst) { |
| 494 | return nullptr; |
| 495 | } |
| 496 | return GrXfermodeFragmentProcessor::MakeFromTwoProcessors(std::move(src), std::move(dst), |
| 497 | SkUncorrelatedCoverageModeToBlendMode(fMode)); |
| 498 | } |
| 499 | |
| 500 | bool onHasFragmentProcessor() const override { |
| 501 | return as_MFB(fSrc)->hasFragmentProcessor() && as_MFB(fDst)->hasFragmentProcessor(); |
| 502 | } |
| 503 | #endif |
| 504 | |
Mike Reed | 20dc672 | 2018-01-24 16:34:02 -0500 | [diff] [blame] | 505 | private: |
| 506 | sk_sp<SkMaskFilter> fDst; |
| 507 | sk_sp<SkMaskFilter> fSrc; |
Mike Reed | 1bd556a | 2018-01-26 11:42:38 -0500 | [diff] [blame] | 508 | SkCoverageMode fMode; |
Mike Reed | 20dc672 | 2018-01-24 16:34:02 -0500 | [diff] [blame] | 509 | |
| 510 | void flatten(SkWriteBuffer&) const override; |
| 511 | |
| 512 | friend class SkMaskFilter; |
| 513 | |
| 514 | typedef SkMaskFilterBase INHERITED; |
| 515 | }; |
| 516 | |
| 517 | #include "SkSafeMath.h" |
| 518 | |
| 519 | class DrawIntoMask : public SkDraw { |
Mike Reed | 20dc672 | 2018-01-24 16:34:02 -0500 | [diff] [blame] | 520 | public: |
Mike Reed | 1bd556a | 2018-01-26 11:42:38 -0500 | [diff] [blame] | 521 | // we ignore the offset of the mask->fBounds |
Mike Reed | 20dc672 | 2018-01-24 16:34:02 -0500 | [diff] [blame] | 522 | DrawIntoMask(SkMask* mask) { |
| 523 | int w = mask->fBounds.width(); |
| 524 | int h = mask->fBounds.height(); |
| 525 | size_t size = SkSafeMath::Mul(w, h); |
| 526 | mask->fFormat = SkMask::kA8_Format; |
| 527 | mask->fImage = SkMask::AllocImage(size, SkMask::kZeroInit_Alloc); |
| 528 | mask->fRowBytes = w; |
| 529 | |
| 530 | SkAssertResult(fDst.reset(*mask)); |
| 531 | |
| 532 | fMatrixStorage.reset(); |
| 533 | fMatrix = &fMatrixStorage; |
| 534 | |
| 535 | fRCStorage.setRect({ 0, 0, w, h }); |
| 536 | fRC = &fRCStorage; |
Mike Reed | 20dc672 | 2018-01-24 16:34:02 -0500 | [diff] [blame] | 537 | } |
| 538 | |
Mike Reed | 1bd556a | 2018-01-26 11:42:38 -0500 | [diff] [blame] | 539 | void drawAsBitmap(const SkMask& m, const SkPaint& p) { |
| 540 | SkBitmap b; |
| 541 | b.installMaskPixels(m); |
| 542 | this->drawSprite(b, m.fBounds.fLeft, m.fBounds.fTop, p); |
Mike Reed | 20dc672 | 2018-01-24 16:34:02 -0500 | [diff] [blame] | 543 | } |
Mike Reed | 1bd556a | 2018-01-26 11:42:38 -0500 | [diff] [blame] | 544 | |
| 545 | private: |
| 546 | SkMatrix fMatrixStorage; |
| 547 | SkRasterClip fRCStorage; |
Mike Reed | 20dc672 | 2018-01-24 16:34:02 -0500 | [diff] [blame] | 548 | }; |
| 549 | |
Mike Reed | 1bd556a | 2018-01-26 11:42:38 -0500 | [diff] [blame] | 550 | static SkIRect join(const SkIRect& src, const SkIRect& dst, SkCoverageMode mode) { |
| 551 | switch (mode) { |
| 552 | case SkCoverageMode::kUnion: return join(src, dst); |
| 553 | case SkCoverageMode::kIntersect: return sect(src, dst); |
| 554 | case SkCoverageMode::kDifference: return src; |
| 555 | case SkCoverageMode::kReverseDifference: return dst; |
| 556 | case SkCoverageMode::kXor: return join(src, dst); |
| 557 | } |
| 558 | // not reached |
| 559 | return { 0, 0, 0, 0 }; |
| 560 | } |
| 561 | |
Mike Reed | 20dc672 | 2018-01-24 16:34:02 -0500 | [diff] [blame] | 562 | bool SkCombineMF::filterMask(SkMask* dst, const SkMask& src, const SkMatrix& ctm, |
| 563 | SkIPoint* margin) const { |
| 564 | SkIPoint srcP, dstP; |
| 565 | SkMask srcM, dstM; |
| 566 | |
| 567 | if (!as_MFB(fSrc)->filterMask(&srcM, src, ctm, &srcP)) { |
| 568 | return false; |
| 569 | } |
| 570 | if (!as_MFB(fDst)->filterMask(&dstM, src, ctm, &dstP)) { |
| 571 | return false; |
| 572 | } |
| 573 | |
Mike Reed | 1bd556a | 2018-01-26 11:42:38 -0500 | [diff] [blame] | 574 | dst->fBounds = join(srcM.fBounds, dstM.fBounds, fMode); |
Mike Reed | 20dc672 | 2018-01-24 16:34:02 -0500 | [diff] [blame] | 575 | dst->fFormat = SkMask::kA8_Format; |
| 576 | if (src.fImage == nullptr) { |
| 577 | dst->fImage = nullptr; |
| 578 | return true; |
| 579 | } |
| 580 | |
| 581 | DrawIntoMask md(dst); |
| 582 | SkPaint p; |
| 583 | |
| 584 | p.setBlendMode(SkBlendMode::kSrc); |
Mike Reed | 1bd556a | 2018-01-26 11:42:38 -0500 | [diff] [blame] | 585 | dstM.fBounds.offset(-dst->fBounds.fLeft, -dst->fBounds.fTop); |
| 586 | md.drawAsBitmap(dstM, p); |
Mike Reed | 547c859 | 2018-02-05 15:59:23 -0500 | [diff] [blame] | 587 | p.setBlendMode(SkUncorrelatedCoverageModeToBlendMode(fMode)); |
Mike Reed | 1bd556a | 2018-01-26 11:42:38 -0500 | [diff] [blame] | 588 | srcM.fBounds.offset(-dst->fBounds.fLeft, -dst->fBounds.fTop); |
| 589 | md.drawAsBitmap(srcM, p); |
Mike Reed | 20dc672 | 2018-01-24 16:34:02 -0500 | [diff] [blame] | 590 | |
| 591 | sk_free(srcM.fImage); |
| 592 | sk_free(dstM.fImage); |
| 593 | return true; |
| 594 | } |
| 595 | |
| 596 | void SkCombineMF::flatten(SkWriteBuffer & buffer) const { |
| 597 | buffer.writeFlattenable(fDst.get()); |
| 598 | buffer.writeFlattenable(fSrc.get()); |
| 599 | buffer.write32(static_cast<uint32_t>(fMode)); |
| 600 | } |
| 601 | |
| 602 | sk_sp<SkFlattenable> SkCombineMF::CreateProc(SkReadBuffer& buffer) { |
Mike Reed | 20dc672 | 2018-01-24 16:34:02 -0500 | [diff] [blame] | 603 | auto dst = buffer.readMaskFilter(); |
| 604 | auto src = buffer.readMaskFilter(); |
Mike Reed | de5c502 | 2018-01-26 14:59:12 -0500 | [diff] [blame] | 605 | SkCoverageMode mode = buffer.read32LE(SkCoverageMode::kLast); |
| 606 | if (!buffer.validate(dst && src)) { |
Mike Reed | 20dc672 | 2018-01-24 16:34:02 -0500 | [diff] [blame] | 607 | return nullptr; |
| 608 | } |
| 609 | return SkMaskFilter::MakeCombine(std::move(dst), std::move(src), mode); |
| 610 | } |
| 611 | |
Mike Reed | be33bab | 2018-02-06 16:07:51 -0500 | [diff] [blame] | 612 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 613 | |
Florin Malita | c6c5ead | 2018-04-11 15:33:40 -0400 | [diff] [blame] | 614 | class SkMatrixMF : public SkMaskFilterBase { |
Mike Reed | be33bab | 2018-02-06 16:07:51 -0500 | [diff] [blame] | 615 | public: |
Florin Malita | c6c5ead | 2018-04-11 15:33:40 -0400 | [diff] [blame] | 616 | SkMatrixMF(sk_sp<SkMaskFilter> filter, const SkMatrix& lm) |
Mike Reed | be33bab | 2018-02-06 16:07:51 -0500 | [diff] [blame] | 617 | : fFilter(std::move(filter)) |
| 618 | , fLM(lm) |
| 619 | {} |
| 620 | |
| 621 | bool filterMask(SkMask* dst, const SkMask& src, const SkMatrix& ctm, |
| 622 | SkIPoint* margin) const override { |
| 623 | return as_MFB(fFilter)->filterMask(dst, src, SkMatrix::Concat(ctm, fLM), margin); |
| 624 | } |
| 625 | |
| 626 | void computeFastBounds(const SkRect& src, SkRect* dst) const override { |
| 627 | *dst = src; |
| 628 | SkRect tmp; |
| 629 | fLM.mapRect(&tmp, src); |
| 630 | as_MFB(fFilter)->computeFastBounds(tmp, dst); |
| 631 | } |
| 632 | |
| 633 | SkMask::Format getFormat() const override { return as_MFB(fFilter)->getFormat(); } |
| 634 | |
Mike Klein | 4fee323 | 2018-10-18 17:27:16 -0400 | [diff] [blame^] | 635 | SK_FLATTENABLE_HOOKS(SkMatrixMF) |
Mike Reed | be33bab | 2018-02-06 16:07:51 -0500 | [diff] [blame] | 636 | |
| 637 | protected: |
| 638 | #if SK_SUPPORT_GPU |
| 639 | std::unique_ptr<GrFragmentProcessor> onAsFragmentProcessor(const GrFPArgs& args) const override{ |
Florin Malita | c6c5ead | 2018-04-11 15:33:40 -0400 | [diff] [blame] | 640 | return as_MFB(fFilter)->asFragmentProcessor(GrFPArgs::WithPostLocalMatrix(args, fLM)); |
Mike Reed | be33bab | 2018-02-06 16:07:51 -0500 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | bool onHasFragmentProcessor() const override { |
| 644 | return as_MFB(fFilter)->hasFragmentProcessor(); |
| 645 | } |
| 646 | #endif |
| 647 | |
| 648 | private: |
| 649 | sk_sp<SkMaskFilter> fFilter; |
| 650 | const SkMatrix fLM; |
| 651 | |
| 652 | void flatten(SkWriteBuffer& buffer) const override { |
| 653 | buffer.writeMatrix(fLM); |
| 654 | buffer.writeFlattenable(fFilter.get()); |
| 655 | } |
| 656 | |
| 657 | friend class SkMaskFilter; |
| 658 | typedef SkMaskFilterBase INHERITED; |
| 659 | }; |
| 660 | |
Florin Malita | c6c5ead | 2018-04-11 15:33:40 -0400 | [diff] [blame] | 661 | sk_sp<SkFlattenable> SkMatrixMF::CreateProc(SkReadBuffer& buffer) { |
| 662 | SkMatrix m; |
| 663 | buffer.readMatrix(&m); |
Mike Reed | be33bab | 2018-02-06 16:07:51 -0500 | [diff] [blame] | 664 | auto filter = buffer.readMaskFilter(); |
Florin Malita | c6c5ead | 2018-04-11 15:33:40 -0400 | [diff] [blame] | 665 | return filter ? filter->makeWithMatrix(m) : nullptr; |
Mike Reed | be33bab | 2018-02-06 16:07:51 -0500 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
Mike Reed | 20dc672 | 2018-01-24 16:34:02 -0500 | [diff] [blame] | 669 | |
Mike Reed | 1bd556a | 2018-01-26 11:42:38 -0500 | [diff] [blame] | 670 | sk_sp<SkMaskFilter> SkMaskFilter::MakeCompose(sk_sp<SkMaskFilter> outer, |
| 671 | sk_sp<SkMaskFilter> inner) { |
Mike Reed | 20dc672 | 2018-01-24 16:34:02 -0500 | [diff] [blame] | 672 | if (!outer) { |
| 673 | return inner; |
| 674 | } |
| 675 | if (!inner) { |
| 676 | return outer; |
| 677 | } |
| 678 | if (as_MFB(inner)->getFormat() != SkMask::kA8_Format || |
| 679 | as_MFB(outer)->getFormat() != SkMask::kA8_Format) { |
| 680 | return nullptr; |
| 681 | } |
| 682 | return sk_sp<SkMaskFilter>(new SkComposeMF(std::move(outer), std::move(inner))); |
| 683 | } |
| 684 | |
| 685 | sk_sp<SkMaskFilter> SkMaskFilter::MakeCombine(sk_sp<SkMaskFilter> dst, sk_sp<SkMaskFilter> src, |
Mike Reed | 1bd556a | 2018-01-26 11:42:38 -0500 | [diff] [blame] | 686 | SkCoverageMode mode) { |
| 687 | if (!dst) { |
Mike Reed | 20dc672 | 2018-01-24 16:34:02 -0500 | [diff] [blame] | 688 | return src; |
| 689 | } |
Mike Reed | 1bd556a | 2018-01-26 11:42:38 -0500 | [diff] [blame] | 690 | if (!src) { |
Mike Reed | 20dc672 | 2018-01-24 16:34:02 -0500 | [diff] [blame] | 691 | return dst; |
| 692 | } |
Mike Reed | 20dc672 | 2018-01-24 16:34:02 -0500 | [diff] [blame] | 693 | |
| 694 | if (as_MFB(dst)->getFormat() != SkMask::kA8_Format || |
| 695 | as_MFB(src)->getFormat() != SkMask::kA8_Format) { |
| 696 | return nullptr; |
| 697 | } |
| 698 | return sk_sp<SkMaskFilter>(new SkCombineMF(std::move(dst), std::move(src), mode)); |
| 699 | } |
| 700 | |
Florin Malita | c6c5ead | 2018-04-11 15:33:40 -0400 | [diff] [blame] | 701 | sk_sp<SkMaskFilter> SkMaskFilter::makeWithMatrix(const SkMatrix& lm) const { |
Mike Reed | be33bab | 2018-02-06 16:07:51 -0500 | [diff] [blame] | 702 | sk_sp<SkMaskFilter> me = sk_ref_sp(const_cast<SkMaskFilter*>(this)); |
| 703 | if (lm.isIdentity()) { |
| 704 | return me; |
| 705 | } |
Florin Malita | c6c5ead | 2018-04-11 15:33:40 -0400 | [diff] [blame] | 706 | return sk_sp<SkMaskFilter>(new SkMatrixMF(std::move(me), lm)); |
Mike Reed | be33bab | 2018-02-06 16:07:51 -0500 | [diff] [blame] | 707 | } |
| 708 | |
Mike Reed | 20dc672 | 2018-01-24 16:34:02 -0500 | [diff] [blame] | 709 | void SkMaskFilter::InitializeFlattenables() { |
Florin Malita | c6c5ead | 2018-04-11 15:33:40 -0400 | [diff] [blame] | 710 | SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkMatrixMF) |
Mike Reed | 20dc672 | 2018-01-24 16:34:02 -0500 | [diff] [blame] | 711 | SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkComposeMF) |
| 712 | SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkCombineMF) |
Mike Reed | 18e7556 | 2018-03-12 14:03:47 -0400 | [diff] [blame] | 713 | sk_register_blur_maskfilter_createproc(); |
Jim Van Verth | d401da6 | 2018-05-03 10:40:30 -0400 | [diff] [blame] | 714 | #if SK_SUPPORT_GPU |
| 715 | gr_register_sdf_maskfilter_createproc(); |
| 716 | #endif |
Mike Reed | 20dc672 | 2018-01-24 16:34:02 -0500 | [diff] [blame] | 717 | } |