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