robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | */ |
| 7 | |
| 8 | #include "GrBlurUtils.h" |
Robert Phillips | c7c2baf | 2018-03-08 09:51:04 -0500 | [diff] [blame] | 9 | |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 10 | #include "GrCaps.h" |
Robert Phillips | 6a6de56 | 2019-02-15 15:19:15 -0500 | [diff] [blame] | 11 | #include "GrContextPriv.h" |
csmartdalton | 02fa32c | 2016-08-19 13:29:27 -0700 | [diff] [blame] | 12 | #include "GrFixedClip.h" |
Robert Phillips | c7c2baf | 2018-03-08 09:51:04 -0500 | [diff] [blame] | 13 | #include "GrProxyProvider.h" |
Robert Phillips | 7af8fe5 | 2019-02-14 17:27:00 -0500 | [diff] [blame] | 14 | #include "GrRecordingContext.h" |
| 15 | #include "GrRecordingContextPriv.h" |
Robert Phillips | c7c2baf | 2018-03-08 09:51:04 -0500 | [diff] [blame] | 16 | #include "GrRenderTargetContext.h" |
Robert Phillips | 784b7bf | 2016-12-09 13:35:02 -0500 | [diff] [blame] | 17 | #include "GrRenderTargetContextPriv.h" |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 18 | #include "GrShape.h" |
| 19 | #include "GrSoftwarePathRenderer.h" |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 20 | #include "GrStyle.h" |
robertphillips | d728f0c | 2016-11-21 11:05:03 -0800 | [diff] [blame] | 21 | #include "GrTextureProxy.h" |
Robert Phillips | c7c2baf | 2018-03-08 09:51:04 -0500 | [diff] [blame] | 22 | #include "effects/GrSimpleTextureEffect.h" |
| 23 | |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 24 | #include "SkDraw.h" |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 25 | #include "SkGr.h" |
Mike Reed | 80747ef | 2018-01-23 15:29:32 -0500 | [diff] [blame] | 26 | #include "SkMaskFilterBase.h" |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 27 | #include "SkPaint.h" |
csmartdalton | c6f411e | 2016-08-05 22:32:12 -0700 | [diff] [blame] | 28 | #include "SkTLazy.h" |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 29 | |
| 30 | static bool clip_bounds_quick_reject(const SkIRect& clipBounds, const SkIRect& rect) { |
| 31 | return clipBounds.isEmpty() || rect.isEmpty() || !SkIRect::Intersects(clipBounds, rect); |
| 32 | } |
| 33 | |
| 34 | // Draw a mask using the supplied paint. Since the coverage/geometry |
| 35 | // is already burnt into the mask this boils down to a rect draw. |
| 36 | // Return true if the mask was successfully drawn. |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 37 | static bool draw_mask(GrRenderTargetContext* renderTargetContext, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 38 | const GrClip& clip, |
| 39 | const SkMatrix& viewMatrix, |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 40 | const SkIRect& maskRect, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 41 | GrPaint&& paint, |
Robert Phillips | 4a24da5 | 2016-12-14 09:00:07 -0500 | [diff] [blame] | 42 | sk_sp<GrTextureProxy> mask) { |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 43 | SkMatrix inverse; |
| 44 | if (!viewMatrix.invert(&inverse)) { |
| 45 | return false; |
| 46 | } |
Robert Phillips | 4a24da5 | 2016-12-14 09:00:07 -0500 | [diff] [blame] | 47 | |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 48 | SkMatrix matrix = SkMatrix::MakeTrans(-SkIntToScalar(maskRect.fLeft), |
| 49 | -SkIntToScalar(maskRect.fTop)); |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 50 | matrix.preConcat(viewMatrix); |
Brian Osman | 2240be9 | 2017-10-18 13:15:13 -0400 | [diff] [blame] | 51 | paint.addCoverageFragmentProcessor(GrSimpleTextureEffect::Make(std::move(mask), matrix)); |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 52 | |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 53 | renderTargetContext->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 54 | SkRect::Make(maskRect), inverse); |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 55 | return true; |
| 56 | } |
| 57 | |
Robert Phillips | c7c2baf | 2018-03-08 09:51:04 -0500 | [diff] [blame] | 58 | static void mask_release_proc(void* addr, void* /*context*/) { |
| 59 | SkMask::FreeImage(addr); |
| 60 | } |
| 61 | |
Robert Phillips | 7af8fe5 | 2019-02-14 17:27:00 -0500 | [diff] [blame] | 62 | static bool sw_draw_with_mask_filter(GrRecordingContext* context, |
Robert Phillips | 4a24da5 | 2016-12-14 09:00:07 -0500 | [diff] [blame] | 63 | GrRenderTargetContext* renderTargetContext, |
robertphillips | 0e7029e | 2015-11-30 05:45:06 -0800 | [diff] [blame] | 64 | const GrClip& clipData, |
| 65 | const SkMatrix& viewMatrix, |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 66 | const GrShape& shape, |
robertphillips | 0e7029e | 2015-11-30 05:45:06 -0800 | [diff] [blame] | 67 | const SkMaskFilter* filter, |
| 68 | const SkIRect& clipBounds, |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 69 | GrPaint&& paint, |
| 70 | const GrUniqueKey& key) { |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 71 | SkASSERT(filter); |
| 72 | SkASSERT(!shape.style().applies()); |
| 73 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 74 | auto proxyProvider = context->priv().proxyProvider(); |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 75 | |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 76 | sk_sp<GrTextureProxy> filteredMask; |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 77 | |
| 78 | SkStrokeRec::InitStyle fillOrHairline = shape.style().isSimpleHairline() |
| 79 | ? SkStrokeRec::kHairline_InitStyle |
| 80 | : SkStrokeRec::kFill_InitStyle; |
| 81 | |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 82 | if (key.isValid()) { |
| 83 | // TODO: this cache look up is duplicated in draw_shape_with_mask_filter for gpu |
| 84 | filteredMask = proxyProvider->findOrCreateProxyByUniqueKey(key, kTopLeft_GrSurfaceOrigin); |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 87 | SkIRect drawRect; |
| 88 | if (filteredMask) { |
| 89 | SkRect devBounds = shape.bounds(); |
| 90 | viewMatrix.mapRect(&devBounds); |
Robert Phillips | 4a24da5 | 2016-12-14 09:00:07 -0500 | [diff] [blame] | 91 | |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 92 | // Here we need to recompute the destination bounds in order to draw the mask correctly |
| 93 | SkMask srcM, dstM; |
| 94 | if (!SkDraw::ComputeMaskBounds(devBounds, &clipBounds, filter, &viewMatrix, |
| 95 | &srcM.fBounds)) { |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | srcM.fFormat = SkMask::kA8_Format; |
| 100 | |
| 101 | if (!as_MFB(filter)->filterMask(&dstM, srcM, viewMatrix, nullptr)) { |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | // Unfortunately, we cannot double check that the computed bounds (i.e., dstM.fBounds) |
| 106 | // match the stored bounds of the mask bc the proxy may have been recreated and, |
| 107 | // when it is recreated, it just gets the bounds of the underlying GrTexture (which |
| 108 | // might be a loose fit). |
| 109 | drawRect = dstM.fBounds; |
| 110 | } else { |
| 111 | // TODO: it seems like we could create an SkDraw here and set its fMatrix field rather |
| 112 | // than explicitly transforming the path to device space. |
| 113 | SkPath devPath; |
| 114 | |
| 115 | shape.asPath(&devPath); |
| 116 | |
| 117 | devPath.transform(viewMatrix); |
| 118 | |
| 119 | SkMask srcM, dstM; |
| 120 | if (!SkDraw::DrawToMask(devPath, &clipBounds, filter, &viewMatrix, &srcM, |
| 121 | SkMask::kComputeBoundsAndRenderImage_CreateMode, fillOrHairline)) { |
| 122 | return false; |
| 123 | } |
| 124 | SkAutoMaskFreeImage autoSrc(srcM.fImage); |
| 125 | |
| 126 | SkASSERT(SkMask::kA8_Format == srcM.fFormat); |
| 127 | |
| 128 | if (!as_MFB(filter)->filterMask(&dstM, srcM, viewMatrix, nullptr)) { |
| 129 | return false; |
| 130 | } |
| 131 | // this will free-up dstM when we're done (allocated in filterMask()) |
| 132 | SkAutoMaskFreeImage autoDst(dstM.fImage); |
| 133 | |
| 134 | if (clip_bounds_quick_reject(clipBounds, dstM.fBounds)) { |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | // we now have a device-aligned 8bit mask in dstM, ready to be drawn using |
| 139 | // the current clip (and identity matrix) and GrPaint settings |
| 140 | SkBitmap bm; |
| 141 | if (!bm.installPixels(SkImageInfo::MakeA8(dstM.fBounds.width(), dstM.fBounds.height()), |
| 142 | autoDst.release(), dstM.fRowBytes, mask_release_proc, nullptr)) { |
| 143 | return false; |
| 144 | } |
| 145 | bm.setImmutable(); |
| 146 | |
| 147 | sk_sp<SkImage> image = SkImage::MakeFromBitmap(bm); |
| 148 | if (!image) { |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | filteredMask = proxyProvider->createTextureProxy(std::move(image), |
| 153 | kNone_GrSurfaceFlags, |
| 154 | 1, SkBudgeted::kYes, |
| 155 | SkBackingFit::kApprox); |
| 156 | if (!filteredMask) { |
| 157 | return false; |
| 158 | } |
| 159 | |
Robert Phillips | 59b39e7 | 2018-08-30 11:51:25 -0400 | [diff] [blame] | 160 | SkASSERT(kTopLeft_GrSurfaceOrigin == filteredMask->origin()); |
| 161 | |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 162 | drawRect = dstM.fBounds; |
| 163 | |
| 164 | if (key.isValid()) { |
| 165 | proxyProvider->assignUniqueKeyToProxy(key, filteredMask.get()); |
| 166 | } |
Robert Phillips | 48ce22b | 2018-03-23 10:33:12 -0400 | [diff] [blame] | 167 | } |
Robert Phillips | c7c2baf | 2018-03-08 09:51:04 -0500 | [diff] [blame] | 168 | |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 169 | return draw_mask(renderTargetContext, clipData, viewMatrix, drawRect, |
| 170 | std::move(paint), std::move(filteredMask)); |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 173 | // Create a mask of 'shape' and place the result in 'mask'. |
Robert Phillips | 7af8fe5 | 2019-02-14 17:27:00 -0500 | [diff] [blame] | 174 | static sk_sp<GrTextureProxy> create_mask_GPU(GrRecordingContext* context, |
robertphillips | d728f0c | 2016-11-21 11:05:03 -0800 | [diff] [blame] | 175 | const SkIRect& maskRect, |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 176 | const SkMatrix& origViewMatrix, |
| 177 | const GrShape& shape, |
robertphillips | d728f0c | 2016-11-21 11:05:03 -0800 | [diff] [blame] | 178 | int sampleCnt) { |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 179 | GrBackendFormat format = |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 180 | context->priv().caps()->getBackendFormatFromColorType(kAlpha_8_SkColorType); |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 181 | sk_sp<GrRenderTargetContext> rtContext( |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 182 | context->priv().makeDeferredRenderTargetContextWithFallback( |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 183 | format, SkBackingFit::kApprox, maskRect.width(), maskRect.height(), |
| 184 | kAlpha_8_GrPixelConfig, nullptr, sampleCnt, GrMipMapped::kNo, |
| 185 | kTopLeft_GrSurfaceOrigin)); |
robertphillips | d728f0c | 2016-11-21 11:05:03 -0800 | [diff] [blame] | 186 | if (!rtContext) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 187 | return nullptr; |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Brian Osman | 9a9baae | 2018-11-05 15:06:26 -0500 | [diff] [blame] | 190 | rtContext->priv().absClear(nullptr, SK_PMColor4fTRANSPARENT); |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 191 | |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 192 | GrPaint maskPaint; |
| 193 | maskPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op); |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 194 | |
| 195 | // setup new clip |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 196 | const SkIRect clipRect = SkIRect::MakeWH(maskRect.width(), maskRect.height()); |
| 197 | GrFixedClip clip(clipRect); |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 198 | |
robertphillips | 3833daa | 2015-09-14 11:18:13 -0700 | [diff] [blame] | 199 | // Draw the mask into maskTexture with the path's integerized top-left at |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 200 | // the origin using maskPaint. |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 201 | SkMatrix viewMatrix = origViewMatrix; |
| 202 | viewMatrix.postTranslate(-SkIntToScalar(maskRect.fLeft), -SkIntToScalar(maskRect.fTop)); |
Robert Phillips | 793324c | 2018-08-24 13:53:56 -0400 | [diff] [blame] | 203 | rtContext->drawShape(clip, std::move(maskPaint), GrAA::kYes, viewMatrix, shape); |
Robert Phillips | f200a90 | 2017-01-30 13:27:37 -0500 | [diff] [blame] | 204 | return rtContext->asTextureProxyRef(); |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 207 | static bool get_unclipped_shape_dev_bounds(const GrShape& shape, const SkMatrix& matrix, |
| 208 | SkIRect* devBounds) { |
| 209 | SkRect shapeBounds = shape.styledBounds(); |
| 210 | if (shapeBounds.isEmpty()) { |
| 211 | return false; |
| 212 | } |
| 213 | SkRect shapeDevBounds; |
| 214 | matrix.mapRect(&shapeDevBounds, shapeBounds); |
| 215 | // Even though these are "unclipped" bounds we still clip to the int32_t range. |
| 216 | // This is the largest int32_t that is representable exactly as a float. The next 63 larger ints |
| 217 | // would round down to this value when cast to a float, but who really cares. |
| 218 | // INT32_MIN is exactly representable. |
| 219 | static constexpr int32_t kMaxInt = 2147483520; |
| 220 | if (!shapeDevBounds.intersect(SkRect::MakeLTRB(INT32_MIN, INT32_MIN, kMaxInt, kMaxInt))) { |
| 221 | return false; |
| 222 | } |
| 223 | // Make sure that the resulting SkIRect can have representable width and height |
| 224 | if (SkScalarRoundToInt(shapeDevBounds.width()) > kMaxInt || |
| 225 | SkScalarRoundToInt(shapeDevBounds.height()) > kMaxInt) { |
| 226 | return false; |
| 227 | } |
| 228 | shapeDevBounds.roundOut(devBounds); |
| 229 | return true; |
| 230 | } |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 231 | |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 232 | // Gets the shape bounds, the clip bounds, and the intersection (if any). Returns false if there |
| 233 | // is no intersection. |
| 234 | static bool get_shape_and_clip_bounds(GrRenderTargetContext* renderTargetContext, |
| 235 | const GrClip& clip, |
| 236 | const GrShape& shape, |
| 237 | const SkMatrix& matrix, |
| 238 | SkIRect* unclippedDevShapeBounds, |
| 239 | SkIRect* devClipBounds) { |
| 240 | // compute bounds as intersection of rt size, clip, and path |
Robert Phillips | 784b7bf | 2016-12-09 13:35:02 -0500 | [diff] [blame] | 241 | clip.getConservativeBounds(renderTargetContext->width(), |
| 242 | renderTargetContext->height(), |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 243 | devClipBounds); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 244 | |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 245 | if (!get_unclipped_shape_dev_bounds(shape, matrix, unclippedDevShapeBounds)) { |
| 246 | *unclippedDevShapeBounds = SkIRect::EmptyIRect(); |
| 247 | return false; |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 248 | } |
| 249 | |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 250 | return true; |
| 251 | } |
| 252 | |
Robert Phillips | 7af8fe5 | 2019-02-14 17:27:00 -0500 | [diff] [blame] | 253 | static void draw_shape_with_mask_filter(GrRecordingContext* context, |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 254 | GrRenderTargetContext* renderTargetContext, |
| 255 | const GrClip& clip, |
| 256 | GrPaint&& paint, |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 257 | const SkMatrix& viewMatrix, |
| 258 | const SkMaskFilterBase* maskFilter, |
| 259 | const GrShape& origShape) { |
| 260 | SkASSERT(maskFilter); |
| 261 | |
| 262 | const GrShape* shape = &origShape; |
| 263 | SkTLazy<GrShape> tmpShape; |
| 264 | |
| 265 | if (origShape.style().applies()) { |
| 266 | SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix); |
| 267 | if (0 == styleScale) { |
| 268 | return; |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 269 | } |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 270 | |
| 271 | tmpShape.init(origShape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale)); |
| 272 | if (tmpShape.get()->isEmpty()) { |
| 273 | return; |
| 274 | } |
| 275 | |
| 276 | shape = tmpShape.get(); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 277 | } |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 278 | |
Robert Phillips | b889f20 | 2018-08-17 19:55:59 +0000 | [diff] [blame] | 279 | if (maskFilter->directFilterMaskGPU(context, |
| 280 | renderTargetContext, |
| 281 | std::move(paint), |
| 282 | clip, |
| 283 | viewMatrix, |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 284 | *shape)) { |
Robert Phillips | b889f20 | 2018-08-17 19:55:59 +0000 | [diff] [blame] | 285 | // the mask filter was able to draw itself directly, so there's nothing |
| 286 | // left to do. |
| 287 | return; |
| 288 | } |
Mike Klein | 1688507 | 2018-12-11 09:54:31 -0500 | [diff] [blame] | 289 | assert_alive(paint); |
Robert Phillips | b889f20 | 2018-08-17 19:55:59 +0000 | [diff] [blame] | 290 | |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 291 | // If the path is hairline, ignore inverse fill. |
| 292 | bool inverseFilled = shape->inverseFilled() && |
| 293 | !GrPathRenderer::IsStrokeHairlineOrEquivalent(shape->style(), |
| 294 | viewMatrix, nullptr); |
| 295 | |
| 296 | SkIRect unclippedDevShapeBounds, devClipBounds; |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 297 | if (!get_shape_and_clip_bounds(renderTargetContext, clip, *shape, viewMatrix, |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 298 | &unclippedDevShapeBounds, |
| 299 | &devClipBounds)) { |
| 300 | // TODO: just cons up an opaque mask here |
| 301 | if (!inverseFilled) { |
| 302 | return; |
| 303 | } |
| 304 | } |
| 305 | |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 306 | // To prevent overloading the cache with entries during animations we limit the cache of masks |
| 307 | // to cases where the matrix preserves axis alignment. |
Robert Phillips | de47928 | 2018-08-28 11:20:08 -0400 | [diff] [blame] | 308 | #ifdef SK_DISABLE_MASKFILTERED_MASK_CACHING |
| 309 | bool useCache = false; |
| 310 | #else |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 311 | bool useCache = !inverseFilled && viewMatrix.preservesAxisAlignment() && |
| 312 | shape->hasUnstyledKey() && as_MFB(maskFilter)->asABlur(nullptr); |
Robert Phillips | de47928 | 2018-08-28 11:20:08 -0400 | [diff] [blame] | 313 | #endif |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 314 | |
| 315 | const SkIRect* boundsForClip = &devClipBounds; |
| 316 | if (useCache) { |
| 317 | SkIRect clippedMaskRect, unClippedMaskRect; |
| 318 | maskFilter->canFilterMaskGPU(*shape, unclippedDevShapeBounds, devClipBounds, |
| 319 | viewMatrix, &clippedMaskRect); |
| 320 | maskFilter->canFilterMaskGPU(*shape, unclippedDevShapeBounds, unclippedDevShapeBounds, |
| 321 | viewMatrix, &unClippedMaskRect); |
| 322 | if (clippedMaskRect.isEmpty()) { |
| 323 | return; |
| 324 | } |
| 325 | |
| 326 | // Use the cache only if >50% of the filtered mask is visible. |
| 327 | int unclippedWidth = unClippedMaskRect.width(); |
| 328 | int unclippedHeight = unClippedMaskRect.height(); |
| 329 | int64_t unclippedArea = sk_64_mul(unclippedWidth, unclippedHeight); |
| 330 | int64_t clippedArea = sk_64_mul(clippedMaskRect.width(), clippedMaskRect.height()); |
| 331 | int maxTextureSize = renderTargetContext->caps()->maxTextureSize(); |
| 332 | if (unclippedArea > 2 * clippedArea || unclippedWidth > maxTextureSize || |
| 333 | unclippedHeight > maxTextureSize) { |
| 334 | useCache = false; |
| 335 | } else { |
| 336 | // Make the clip not affect the mask |
| 337 | boundsForClip = &unclippedDevShapeBounds; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | GrUniqueKey maskKey; |
| 342 | if (useCache) { |
| 343 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 344 | GrUniqueKey::Builder builder(&maskKey, kDomain, 5 + 2 + shape->unstyledKeySize(), |
| 345 | "Mask Filtered Masks"); |
| 346 | |
| 347 | // We require the upper left 2x2 of the matrix to match exactly for a cache hit. |
| 348 | SkScalar sx = viewMatrix.get(SkMatrix::kMScaleX); |
| 349 | SkScalar sy = viewMatrix.get(SkMatrix::kMScaleY); |
| 350 | SkScalar kx = viewMatrix.get(SkMatrix::kMSkewX); |
| 351 | SkScalar ky = viewMatrix.get(SkMatrix::kMSkewY); |
| 352 | SkScalar tx = viewMatrix.get(SkMatrix::kMTransX); |
| 353 | SkScalar ty = viewMatrix.get(SkMatrix::kMTransY); |
| 354 | // Allow 8 bits each in x and y of subpixel positioning. |
| 355 | SkFixed fracX = SkScalarToFixed(SkScalarFraction(tx)) & 0x0000FF00; |
| 356 | SkFixed fracY = SkScalarToFixed(SkScalarFraction(ty)) & 0x0000FF00; |
| 357 | |
| 358 | builder[0] = SkFloat2Bits(sx); |
| 359 | builder[1] = SkFloat2Bits(sy); |
| 360 | builder[2] = SkFloat2Bits(kx); |
| 361 | builder[3] = SkFloat2Bits(ky); |
| 362 | // Distinguish between hairline and filled paths. For hairlines, we also need to include |
| 363 | // the cap. (SW grows hairlines by 0.5 pixel with round and square caps). Note that |
| 364 | // stroke-and-fill of hairlines is turned into pure fill by SkStrokeRec, so this covers |
| 365 | // all cases we might see. |
| 366 | uint32_t styleBits = shape->style().isSimpleHairline() |
| 367 | ? ((shape->style().strokeRec().getCap() << 1) | 1) |
| 368 | : 0; |
| 369 | builder[4] = fracX | (fracY >> 8) | (styleBits << 16); |
| 370 | |
| 371 | SkMaskFilterBase::BlurRec rec; |
| 372 | SkAssertResult(as_MFB(maskFilter)->asABlur(&rec)); |
| 373 | |
| 374 | builder[5] = rec.fStyle; // TODO: we could put this with the other style bits |
Robert Phillips | 55ff5d3 | 2019-01-07 16:11:36 -0500 | [diff] [blame] | 375 | builder[6] = SkFloat2Bits(rec.fSigma); |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 376 | shape->writeUnstyledKey(&builder[7]); |
| 377 | } |
| 378 | |
| 379 | SkIRect maskRect; |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 380 | if (maskFilter->canFilterMaskGPU(*shape, |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 381 | unclippedDevShapeBounds, |
| 382 | *boundsForClip, |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 383 | viewMatrix, |
| 384 | &maskRect)) { |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 385 | if (clip_bounds_quick_reject(*boundsForClip, maskRect)) { |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 386 | // clipped out |
| 387 | return; |
| 388 | } |
| 389 | |
Robert Phillips | 20390c3 | 2018-08-17 11:01:03 -0400 | [diff] [blame] | 390 | sk_sp<GrTextureProxy> filteredMask; |
| 391 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 392 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 393 | |
| 394 | if (maskKey.isValid()) { |
| 395 | // TODO: this cache look up is duplicated in sw_draw_with_mask_filter for raster |
| 396 | filteredMask = proxyProvider->findOrCreateProxyByUniqueKey( |
Robert Phillips | 59b39e7 | 2018-08-30 11:51:25 -0400 | [diff] [blame] | 397 | maskKey, kTopLeft_GrSurfaceOrigin); |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 398 | } |
| 399 | |
Robert Phillips | 20390c3 | 2018-08-17 11:01:03 -0400 | [diff] [blame] | 400 | if (!filteredMask) { |
| 401 | sk_sp<GrTextureProxy> maskProxy(create_mask_GPU( |
| 402 | context, |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 403 | maskRect, |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 404 | viewMatrix, |
| 405 | *shape, |
robertphillips | d728f0c | 2016-11-21 11:05:03 -0800 | [diff] [blame] | 406 | renderTargetContext->numColorSamples())); |
Robert Phillips | 20390c3 | 2018-08-17 11:01:03 -0400 | [diff] [blame] | 407 | if (maskProxy) { |
| 408 | filteredMask = maskFilter->filterMaskGPU(context, |
| 409 | std::move(maskProxy), |
| 410 | viewMatrix, |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 411 | maskRect); |
Robert Phillips | 59b39e7 | 2018-08-30 11:51:25 -0400 | [diff] [blame] | 412 | SkASSERT(kTopLeft_GrSurfaceOrigin == filteredMask->origin()); |
| 413 | |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 414 | if (filteredMask && maskKey.isValid()) { |
| 415 | proxyProvider->assignUniqueKeyToProxy(maskKey, filteredMask.get()); |
| 416 | } |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 417 | } |
| 418 | } |
Robert Phillips | 20390c3 | 2018-08-17 11:01:03 -0400 | [diff] [blame] | 419 | |
| 420 | if (filteredMask) { |
| 421 | if (draw_mask(renderTargetContext, clip, viewMatrix, |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 422 | maskRect, std::move(paint), std::move(filteredMask))) { |
Robert Phillips | 20390c3 | 2018-08-17 11:01:03 -0400 | [diff] [blame] | 423 | // This path is completely drawn |
| 424 | return; |
| 425 | } |
Mike Klein | 1688507 | 2018-12-11 09:54:31 -0500 | [diff] [blame] | 426 | assert_alive(paint); |
Robert Phillips | 20390c3 | 2018-08-17 11:01:03 -0400 | [diff] [blame] | 427 | } |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 428 | } |
| 429 | |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 430 | sw_draw_with_mask_filter(context, renderTargetContext, clip, viewMatrix, *shape, |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 431 | maskFilter, *boundsForClip, std::move(paint), maskKey); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 432 | } |
| 433 | |
Robert Phillips | 7af8fe5 | 2019-02-14 17:27:00 -0500 | [diff] [blame] | 434 | void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext* context, |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 435 | GrRenderTargetContext* renderTargetContext, |
| 436 | const GrClip& clip, |
| 437 | const GrShape& shape, |
| 438 | GrPaint&& paint, |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 439 | const SkMatrix& viewMatrix, |
| 440 | const SkMaskFilter* mf) { |
Robert Phillips | 793324c | 2018-08-24 13:53:56 -0400 | [diff] [blame] | 441 | draw_shape_with_mask_filter(context, renderTargetContext, clip, std::move(paint), |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 442 | viewMatrix, as_MFB(mf), shape); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 443 | } |
| 444 | |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 445 | void GrBlurUtils::drawShapeWithMaskFilter(GrContext* context, |
| 446 | GrRenderTargetContext* renderTargetContext, |
| 447 | const GrClip& clip, |
| 448 | const SkPaint& paint, |
| 449 | const SkMatrix& viewMatrix, |
| 450 | const GrShape& shape) { |
Robert Phillips | 6a6de56 | 2019-02-15 15:19:15 -0500 | [diff] [blame] | 451 | if (context->priv().abandoned()) { |
Robert Phillips | bebfd41 | 2018-03-08 11:07:23 -0500 | [diff] [blame] | 452 | return; |
| 453 | } |
| 454 | |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 455 | GrPaint grPaint; |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 456 | if (!SkPaintToGrPaint(context, renderTargetContext->colorSpaceInfo(), paint, viewMatrix, |
| 457 | &grPaint)) { |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 458 | return; |
| 459 | } |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 460 | |
Mike Reed | 80747ef | 2018-01-23 15:29:32 -0500 | [diff] [blame] | 461 | SkMaskFilterBase* mf = as_MFB(paint.getMaskFilter()); |
Mike Reed | bfadcf0 | 2018-01-20 22:24:21 +0000 | [diff] [blame] | 462 | if (mf && !mf->hasFragmentProcessor()) { |
Robert Phillips | a29a956 | 2016-10-20 09:40:55 -0400 | [diff] [blame] | 463 | // The MaskFilter wasn't already handled in SkPaintToGrPaint |
Robert Phillips | 793324c | 2018-08-24 13:53:56 -0400 | [diff] [blame] | 464 | draw_shape_with_mask_filter(context, renderTargetContext, clip, std::move(grPaint), |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 465 | viewMatrix, mf, shape); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 466 | } else { |
Robert Phillips | 793324c | 2018-08-24 13:53:56 -0400 | [diff] [blame] | 467 | GrAA aa = GrAA(paint.isAntiAlias()); |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 468 | renderTargetContext->drawShape(clip, std::move(grPaint), aa, viewMatrix, shape); |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 469 | } |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 470 | } |