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) { |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 178 | GrBackendFormat format = |
| 179 | context->contextPriv().caps()->getBackendFormatFromColorType(kAlpha_8_SkColorType); |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 180 | sk_sp<GrRenderTargetContext> rtContext( |
| 181 | context->contextPriv().makeDeferredRenderTargetContextWithFallback( |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 182 | format, SkBackingFit::kApprox, maskRect.width(), maskRect.height(), |
| 183 | kAlpha_8_GrPixelConfig, nullptr, sampleCnt, GrMipMapped::kNo, |
| 184 | kTopLeft_GrSurfaceOrigin)); |
robertphillips | d728f0c | 2016-11-21 11:05:03 -0800 | [diff] [blame] | 185 | if (!rtContext) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 186 | return nullptr; |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Brian Osman | 9a9baae | 2018-11-05 15:06:26 -0500 | [diff] [blame] | 189 | rtContext->priv().absClear(nullptr, SK_PMColor4fTRANSPARENT); |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 190 | |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 191 | GrPaint maskPaint; |
| 192 | maskPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op); |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 193 | |
| 194 | // setup new clip |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 195 | const SkIRect clipRect = SkIRect::MakeWH(maskRect.width(), maskRect.height()); |
| 196 | GrFixedClip clip(clipRect); |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 197 | |
robertphillips | 3833daa | 2015-09-14 11:18:13 -0700 | [diff] [blame] | 198 | // 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] | 199 | // the origin using maskPaint. |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 200 | SkMatrix viewMatrix = origViewMatrix; |
| 201 | viewMatrix.postTranslate(-SkIntToScalar(maskRect.fLeft), -SkIntToScalar(maskRect.fTop)); |
Robert Phillips | 793324c | 2018-08-24 13:53:56 -0400 | [diff] [blame] | 202 | rtContext->drawShape(clip, std::move(maskPaint), GrAA::kYes, viewMatrix, shape); |
Robert Phillips | f200a90 | 2017-01-30 13:27:37 -0500 | [diff] [blame] | 203 | return rtContext->asTextureProxyRef(); |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 206 | static bool get_unclipped_shape_dev_bounds(const GrShape& shape, const SkMatrix& matrix, |
| 207 | SkIRect* devBounds) { |
| 208 | SkRect shapeBounds = shape.styledBounds(); |
| 209 | if (shapeBounds.isEmpty()) { |
| 210 | return false; |
| 211 | } |
| 212 | SkRect shapeDevBounds; |
| 213 | matrix.mapRect(&shapeDevBounds, shapeBounds); |
| 214 | // Even though these are "unclipped" bounds we still clip to the int32_t range. |
| 215 | // This is the largest int32_t that is representable exactly as a float. The next 63 larger ints |
| 216 | // would round down to this value when cast to a float, but who really cares. |
| 217 | // INT32_MIN is exactly representable. |
| 218 | static constexpr int32_t kMaxInt = 2147483520; |
| 219 | if (!shapeDevBounds.intersect(SkRect::MakeLTRB(INT32_MIN, INT32_MIN, kMaxInt, kMaxInt))) { |
| 220 | return false; |
| 221 | } |
| 222 | // Make sure that the resulting SkIRect can have representable width and height |
| 223 | if (SkScalarRoundToInt(shapeDevBounds.width()) > kMaxInt || |
| 224 | SkScalarRoundToInt(shapeDevBounds.height()) > kMaxInt) { |
| 225 | return false; |
| 226 | } |
| 227 | shapeDevBounds.roundOut(devBounds); |
| 228 | return true; |
| 229 | } |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 230 | |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 231 | // Gets the shape bounds, the clip bounds, and the intersection (if any). Returns false if there |
| 232 | // is no intersection. |
| 233 | static bool get_shape_and_clip_bounds(GrRenderTargetContext* renderTargetContext, |
| 234 | const GrClip& clip, |
| 235 | const GrShape& shape, |
| 236 | const SkMatrix& matrix, |
| 237 | SkIRect* unclippedDevShapeBounds, |
| 238 | SkIRect* devClipBounds) { |
| 239 | // compute bounds as intersection of rt size, clip, and path |
Robert Phillips | 784b7bf | 2016-12-09 13:35:02 -0500 | [diff] [blame] | 240 | clip.getConservativeBounds(renderTargetContext->width(), |
| 241 | renderTargetContext->height(), |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 242 | devClipBounds); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 243 | |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 244 | if (!get_unclipped_shape_dev_bounds(shape, matrix, unclippedDevShapeBounds)) { |
| 245 | *unclippedDevShapeBounds = SkIRect::EmptyIRect(); |
| 246 | return false; |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 247 | } |
| 248 | |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 249 | return true; |
| 250 | } |
| 251 | |
| 252 | static void draw_shape_with_mask_filter(GrContext* context, |
| 253 | GrRenderTargetContext* renderTargetContext, |
| 254 | const GrClip& clip, |
| 255 | GrPaint&& paint, |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 256 | const SkMatrix& viewMatrix, |
| 257 | const SkMaskFilterBase* maskFilter, |
| 258 | const GrShape& origShape) { |
| 259 | SkASSERT(maskFilter); |
| 260 | |
| 261 | const GrShape* shape = &origShape; |
| 262 | SkTLazy<GrShape> tmpShape; |
| 263 | |
| 264 | if (origShape.style().applies()) { |
| 265 | SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix); |
| 266 | if (0 == styleScale) { |
| 267 | return; |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 268 | } |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 269 | |
| 270 | tmpShape.init(origShape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale)); |
| 271 | if (tmpShape.get()->isEmpty()) { |
| 272 | return; |
| 273 | } |
| 274 | |
| 275 | shape = tmpShape.get(); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 276 | } |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 277 | |
Robert Phillips | b889f20 | 2018-08-17 19:55:59 +0000 | [diff] [blame] | 278 | if (maskFilter->directFilterMaskGPU(context, |
| 279 | renderTargetContext, |
| 280 | std::move(paint), |
| 281 | clip, |
| 282 | viewMatrix, |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 283 | *shape)) { |
Robert Phillips | b889f20 | 2018-08-17 19:55:59 +0000 | [diff] [blame] | 284 | // the mask filter was able to draw itself directly, so there's nothing |
| 285 | // left to do. |
| 286 | return; |
| 287 | } |
Mike Klein | 1688507 | 2018-12-11 09:54:31 -0500 | [diff] [blame] | 288 | assert_alive(paint); |
Robert Phillips | b889f20 | 2018-08-17 19:55:59 +0000 | [diff] [blame] | 289 | |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 290 | // If the path is hairline, ignore inverse fill. |
| 291 | bool inverseFilled = shape->inverseFilled() && |
| 292 | !GrPathRenderer::IsStrokeHairlineOrEquivalent(shape->style(), |
| 293 | viewMatrix, nullptr); |
| 294 | |
| 295 | SkIRect unclippedDevShapeBounds, devClipBounds; |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 296 | if (!get_shape_and_clip_bounds(renderTargetContext, clip, *shape, viewMatrix, |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 297 | &unclippedDevShapeBounds, |
| 298 | &devClipBounds)) { |
| 299 | // TODO: just cons up an opaque mask here |
| 300 | if (!inverseFilled) { |
| 301 | return; |
| 302 | } |
| 303 | } |
| 304 | |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 305 | // To prevent overloading the cache with entries during animations we limit the cache of masks |
| 306 | // to cases where the matrix preserves axis alignment. |
Robert Phillips | de47928 | 2018-08-28 11:20:08 -0400 | [diff] [blame] | 307 | #ifdef SK_DISABLE_MASKFILTERED_MASK_CACHING |
| 308 | bool useCache = false; |
| 309 | #else |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 310 | bool useCache = !inverseFilled && viewMatrix.preservesAxisAlignment() && |
| 311 | shape->hasUnstyledKey() && as_MFB(maskFilter)->asABlur(nullptr); |
Robert Phillips | de47928 | 2018-08-28 11:20:08 -0400 | [diff] [blame] | 312 | #endif |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 313 | |
| 314 | const SkIRect* boundsForClip = &devClipBounds; |
| 315 | if (useCache) { |
| 316 | SkIRect clippedMaskRect, unClippedMaskRect; |
| 317 | maskFilter->canFilterMaskGPU(*shape, unclippedDevShapeBounds, devClipBounds, |
| 318 | viewMatrix, &clippedMaskRect); |
| 319 | maskFilter->canFilterMaskGPU(*shape, unclippedDevShapeBounds, unclippedDevShapeBounds, |
| 320 | viewMatrix, &unClippedMaskRect); |
| 321 | if (clippedMaskRect.isEmpty()) { |
| 322 | return; |
| 323 | } |
| 324 | |
| 325 | // Use the cache only if >50% of the filtered mask is visible. |
| 326 | int unclippedWidth = unClippedMaskRect.width(); |
| 327 | int unclippedHeight = unClippedMaskRect.height(); |
| 328 | int64_t unclippedArea = sk_64_mul(unclippedWidth, unclippedHeight); |
| 329 | int64_t clippedArea = sk_64_mul(clippedMaskRect.width(), clippedMaskRect.height()); |
| 330 | int maxTextureSize = renderTargetContext->caps()->maxTextureSize(); |
| 331 | if (unclippedArea > 2 * clippedArea || unclippedWidth > maxTextureSize || |
| 332 | unclippedHeight > maxTextureSize) { |
| 333 | useCache = false; |
| 334 | } else { |
| 335 | // Make the clip not affect the mask |
| 336 | boundsForClip = &unclippedDevShapeBounds; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | GrUniqueKey maskKey; |
| 341 | if (useCache) { |
| 342 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 343 | GrUniqueKey::Builder builder(&maskKey, kDomain, 5 + 2 + shape->unstyledKeySize(), |
| 344 | "Mask Filtered Masks"); |
| 345 | |
| 346 | // We require the upper left 2x2 of the matrix to match exactly for a cache hit. |
| 347 | SkScalar sx = viewMatrix.get(SkMatrix::kMScaleX); |
| 348 | SkScalar sy = viewMatrix.get(SkMatrix::kMScaleY); |
| 349 | SkScalar kx = viewMatrix.get(SkMatrix::kMSkewX); |
| 350 | SkScalar ky = viewMatrix.get(SkMatrix::kMSkewY); |
| 351 | SkScalar tx = viewMatrix.get(SkMatrix::kMTransX); |
| 352 | SkScalar ty = viewMatrix.get(SkMatrix::kMTransY); |
| 353 | // Allow 8 bits each in x and y of subpixel positioning. |
| 354 | SkFixed fracX = SkScalarToFixed(SkScalarFraction(tx)) & 0x0000FF00; |
| 355 | SkFixed fracY = SkScalarToFixed(SkScalarFraction(ty)) & 0x0000FF00; |
| 356 | |
| 357 | builder[0] = SkFloat2Bits(sx); |
| 358 | builder[1] = SkFloat2Bits(sy); |
| 359 | builder[2] = SkFloat2Bits(kx); |
| 360 | builder[3] = SkFloat2Bits(ky); |
| 361 | // Distinguish between hairline and filled paths. For hairlines, we also need to include |
| 362 | // the cap. (SW grows hairlines by 0.5 pixel with round and square caps). Note that |
| 363 | // stroke-and-fill of hairlines is turned into pure fill by SkStrokeRec, so this covers |
| 364 | // all cases we might see. |
| 365 | uint32_t styleBits = shape->style().isSimpleHairline() |
| 366 | ? ((shape->style().strokeRec().getCap() << 1) | 1) |
| 367 | : 0; |
| 368 | builder[4] = fracX | (fracY >> 8) | (styleBits << 16); |
| 369 | |
| 370 | SkMaskFilterBase::BlurRec rec; |
| 371 | SkAssertResult(as_MFB(maskFilter)->asABlur(&rec)); |
| 372 | |
| 373 | 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] | 374 | builder[6] = SkFloat2Bits(rec.fSigma); |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 375 | shape->writeUnstyledKey(&builder[7]); |
| 376 | } |
| 377 | |
| 378 | SkIRect maskRect; |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 379 | if (maskFilter->canFilterMaskGPU(*shape, |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 380 | unclippedDevShapeBounds, |
| 381 | *boundsForClip, |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 382 | viewMatrix, |
| 383 | &maskRect)) { |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 384 | if (clip_bounds_quick_reject(*boundsForClip, maskRect)) { |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 385 | // clipped out |
| 386 | return; |
| 387 | } |
| 388 | |
Robert Phillips | 20390c3 | 2018-08-17 11:01:03 -0400 | [diff] [blame] | 389 | sk_sp<GrTextureProxy> filteredMask; |
| 390 | |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 391 | GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider(); |
| 392 | |
| 393 | if (maskKey.isValid()) { |
| 394 | // TODO: this cache look up is duplicated in sw_draw_with_mask_filter for raster |
| 395 | filteredMask = proxyProvider->findOrCreateProxyByUniqueKey( |
Robert Phillips | 59b39e7 | 2018-08-30 11:51:25 -0400 | [diff] [blame] | 396 | maskKey, kTopLeft_GrSurfaceOrigin); |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 397 | } |
| 398 | |
Robert Phillips | 20390c3 | 2018-08-17 11:01:03 -0400 | [diff] [blame] | 399 | if (!filteredMask) { |
| 400 | sk_sp<GrTextureProxy> maskProxy(create_mask_GPU( |
| 401 | context, |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 402 | maskRect, |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 403 | viewMatrix, |
| 404 | *shape, |
robertphillips | d728f0c | 2016-11-21 11:05:03 -0800 | [diff] [blame] | 405 | renderTargetContext->numColorSamples())); |
Robert Phillips | 20390c3 | 2018-08-17 11:01:03 -0400 | [diff] [blame] | 406 | if (maskProxy) { |
| 407 | filteredMask = maskFilter->filterMaskGPU(context, |
| 408 | std::move(maskProxy), |
| 409 | viewMatrix, |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 410 | maskRect); |
Robert Phillips | 59b39e7 | 2018-08-30 11:51:25 -0400 | [diff] [blame] | 411 | SkASSERT(kTopLeft_GrSurfaceOrigin == filteredMask->origin()); |
| 412 | |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 413 | if (filteredMask && maskKey.isValid()) { |
| 414 | proxyProvider->assignUniqueKeyToProxy(maskKey, filteredMask.get()); |
| 415 | } |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 416 | } |
| 417 | } |
Robert Phillips | 20390c3 | 2018-08-17 11:01:03 -0400 | [diff] [blame] | 418 | |
| 419 | if (filteredMask) { |
| 420 | if (draw_mask(renderTargetContext, clip, viewMatrix, |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 421 | maskRect, std::move(paint), std::move(filteredMask))) { |
Robert Phillips | 20390c3 | 2018-08-17 11:01:03 -0400 | [diff] [blame] | 422 | // This path is completely drawn |
| 423 | return; |
| 424 | } |
Mike Klein | 1688507 | 2018-12-11 09:54:31 -0500 | [diff] [blame] | 425 | assert_alive(paint); |
Robert Phillips | 20390c3 | 2018-08-17 11:01:03 -0400 | [diff] [blame] | 426 | } |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 427 | } |
| 428 | |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 429 | sw_draw_with_mask_filter(context, renderTargetContext, clip, viewMatrix, *shape, |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 430 | maskFilter, *boundsForClip, std::move(paint), maskKey); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 431 | } |
| 432 | |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 433 | void GrBlurUtils::drawShapeWithMaskFilter(GrContext* context, |
| 434 | GrRenderTargetContext* renderTargetContext, |
| 435 | const GrClip& clip, |
| 436 | const GrShape& shape, |
| 437 | GrPaint&& paint, |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 438 | const SkMatrix& viewMatrix, |
| 439 | const SkMaskFilter* mf) { |
Robert Phillips | 793324c | 2018-08-24 13:53:56 -0400 | [diff] [blame] | 440 | draw_shape_with_mask_filter(context, renderTargetContext, clip, std::move(paint), |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 441 | viewMatrix, as_MFB(mf), shape); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 442 | } |
| 443 | |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 444 | void GrBlurUtils::drawShapeWithMaskFilter(GrContext* context, |
| 445 | GrRenderTargetContext* renderTargetContext, |
| 446 | const GrClip& clip, |
| 447 | const SkPaint& paint, |
| 448 | const SkMatrix& viewMatrix, |
| 449 | const GrShape& shape) { |
Khushal | c421ca1 | 2018-06-26 14:38:34 -0700 | [diff] [blame] | 450 | if (context->abandoned()) { |
Robert Phillips | bebfd41 | 2018-03-08 11:07:23 -0500 | [diff] [blame] | 451 | return; |
| 452 | } |
| 453 | |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 454 | GrPaint grPaint; |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 455 | if (!SkPaintToGrPaint(context, renderTargetContext->colorSpaceInfo(), paint, viewMatrix, |
| 456 | &grPaint)) { |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 457 | return; |
| 458 | } |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 459 | |
Mike Reed | 80747ef | 2018-01-23 15:29:32 -0500 | [diff] [blame] | 460 | SkMaskFilterBase* mf = as_MFB(paint.getMaskFilter()); |
Mike Reed | bfadcf0 | 2018-01-20 22:24:21 +0000 | [diff] [blame] | 461 | if (mf && !mf->hasFragmentProcessor()) { |
Robert Phillips | a29a956 | 2016-10-20 09:40:55 -0400 | [diff] [blame] | 462 | // The MaskFilter wasn't already handled in SkPaintToGrPaint |
Robert Phillips | 793324c | 2018-08-24 13:53:56 -0400 | [diff] [blame] | 463 | draw_shape_with_mask_filter(context, renderTargetContext, clip, std::move(grPaint), |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 464 | viewMatrix, mf, shape); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 465 | } else { |
Robert Phillips | 793324c | 2018-08-24 13:53:56 -0400 | [diff] [blame] | 466 | GrAA aa = GrAA(paint.isAntiAlias()); |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 467 | renderTargetContext->drawShape(clip, std::move(grPaint), aa, viewMatrix, shape); |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 468 | } |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 469 | } |