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