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