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 | |
Robert Phillips | fde67e4 | 2020-10-07 15:33:43 -0400 | [diff] [blame] | 10 | #include "include/gpu/GrDirectContext.h" |
Robert Phillips | b7bfbc2 | 2020-07-01 12:55:01 -0400 | [diff] [blame] | 11 | #include "include/gpu/GrRecordingContext.h" |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 12 | #include "src/gpu/GrBitmapTextureMaker.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/gpu/GrCaps.h" |
Robert Phillips | fde67e4 | 2020-10-07 15:33:43 -0400 | [diff] [blame] | 14 | #include "src/gpu/GrContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "src/gpu/GrFixedClip.h" |
| 16 | #include "src/gpu/GrProxyProvider.h" |
| 17 | #include "src/gpu/GrRecordingContextPriv.h" |
| 18 | #include "src/gpu/GrRenderTargetContext.h" |
| 19 | #include "src/gpu/GrRenderTargetContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "src/gpu/GrSoftwarePathRenderer.h" |
| 21 | #include "src/gpu/GrStyle.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 22 | #include "src/gpu/GrTextureProxy.h" |
Robert Phillips | d464feb | 2020-10-08 11:00:02 -0400 | [diff] [blame^] | 23 | #include "src/gpu/GrThreadSafeCache.h" |
Brian Salomon | b8f098d | 2020-01-07 11:15:44 -0500 | [diff] [blame] | 24 | #include "src/gpu/effects/GrTextureEffect.h" |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 25 | #include "src/gpu/geometry/GrStyledShape.h" |
Robert Phillips | c7c2baf | 2018-03-08 09:51:04 -0500 | [diff] [blame] | 26 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 27 | #include "include/core/SkPaint.h" |
| 28 | #include "src/core/SkDraw.h" |
| 29 | #include "src/core/SkMaskFilterBase.h" |
Brian Osman | 449b115 | 2020-04-15 16:43:00 -0400 | [diff] [blame] | 30 | #include "src/core/SkMatrixProvider.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 31 | #include "src/core/SkTLazy.h" |
| 32 | #include "src/gpu/SkGr.h" |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 33 | |
| 34 | static bool clip_bounds_quick_reject(const SkIRect& clipBounds, const SkIRect& rect) { |
| 35 | return clipBounds.isEmpty() || rect.isEmpty() || !SkIRect::Intersects(clipBounds, rect); |
| 36 | } |
| 37 | |
Brian Salomon | d005b69 | 2020-04-01 15:47:05 -0400 | [diff] [blame] | 38 | static constexpr auto kMaskOrigin = kTopLeft_GrSurfaceOrigin; |
| 39 | |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 40 | // Draw a mask using the supplied paint. Since the coverage/geometry |
| 41 | // is already burnt into the mask this boils down to a rect draw. |
| 42 | // Return true if the mask was successfully drawn. |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 43 | static bool draw_mask(GrRenderTargetContext* renderTargetContext, |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 44 | const GrClip* clip, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 45 | const SkMatrix& viewMatrix, |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 46 | const SkIRect& maskRect, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 47 | GrPaint&& paint, |
Greg Daniel | 5c08249 | 2020-01-29 15:06:49 -0500 | [diff] [blame] | 48 | GrSurfaceProxyView mask) { |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 49 | SkMatrix inverse; |
| 50 | if (!viewMatrix.invert(&inverse)) { |
| 51 | return false; |
| 52 | } |
Robert Phillips | 4a24da5 | 2016-12-14 09:00:07 -0500 | [diff] [blame] | 53 | |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 54 | SkMatrix matrix = SkMatrix::Translate(-SkIntToScalar(maskRect.fLeft), |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 55 | -SkIntToScalar(maskRect.fTop)); |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 56 | matrix.preConcat(viewMatrix); |
John Stiles | 41d91b6 | 2020-07-21 14:39:40 -0400 | [diff] [blame] | 57 | paint.setCoverageFragmentProcessor( |
Greg Daniel | d2ccbb5 | 2020-02-05 10:45:39 -0500 | [diff] [blame] | 58 | GrTextureEffect::Make(std::move(mask), kUnknown_SkAlphaType, matrix)); |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 59 | |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 60 | renderTargetContext->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 61 | SkRect::Make(maskRect), inverse); |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 62 | return true; |
| 63 | } |
| 64 | |
Robert Phillips | c7c2baf | 2018-03-08 09:51:04 -0500 | [diff] [blame] | 65 | static void mask_release_proc(void* addr, void* /*context*/) { |
| 66 | SkMask::FreeImage(addr); |
| 67 | } |
| 68 | |
Robert Phillips | 6e17ffe | 2020-10-06 14:52:11 -0400 | [diff] [blame] | 69 | #ifdef SK_DEBUG |
| 70 | // Brute force computation of the destination bounds of a SW filtered mask |
| 71 | static SkIRect sw_calc_draw_rect(const SkMatrix& viewMatrix, |
| 72 | const GrStyledShape& shape, |
| 73 | const SkMaskFilter* filter, |
| 74 | const SkIRect& clipBounds) { |
| 75 | SkRect devBounds = shape.bounds(); |
| 76 | viewMatrix.mapRect(&devBounds); |
| 77 | |
| 78 | SkMask srcM, dstM; |
| 79 | if (!SkDraw::ComputeMaskBounds(devBounds, &clipBounds, filter, &viewMatrix, &srcM.fBounds)) { |
| 80 | return {}; |
| 81 | } |
| 82 | |
| 83 | srcM.fFormat = SkMask::kA8_Format; |
| 84 | |
| 85 | if (!as_MFB(filter)->filterMask(&dstM, srcM, viewMatrix, nullptr)) { |
| 86 | return {}; |
| 87 | } |
| 88 | |
| 89 | return dstM.fBounds; |
| 90 | } |
| 91 | #endif |
| 92 | |
| 93 | // This stores the mapping from an unclipped, integerized, device-space, shape bounds to |
| 94 | // the filtered mask's draw rect. |
| 95 | struct DrawRectData { |
| 96 | SkIVector fOffset; |
| 97 | SkISize fSize; |
| 98 | }; |
| 99 | |
| 100 | static sk_sp<SkData> create_data(const SkIRect& drawRect, const SkIRect& origDevBounds) { |
| 101 | |
| 102 | DrawRectData drawRectData { {drawRect.fLeft - origDevBounds.fLeft, |
| 103 | drawRect.fTop - origDevBounds.fTop}, |
| 104 | drawRect.size() }; |
| 105 | |
| 106 | return SkData::MakeWithCopy(&drawRectData, sizeof(drawRectData)); |
| 107 | } |
| 108 | |
| 109 | static SkIRect extract_draw_rect_from_data(SkData* data, const SkIRect& origDevBounds) { |
| 110 | auto drawRectData = static_cast<const DrawRectData*>(data->data()); |
| 111 | |
| 112 | return SkIRect::MakeXYWH(origDevBounds.fLeft + drawRectData->fOffset.fX, |
| 113 | origDevBounds.fTop + drawRectData->fOffset.fY, |
| 114 | drawRectData->fSize.fWidth, |
| 115 | drawRectData->fSize.fHeight); |
| 116 | } |
| 117 | |
Robert Phillips | c3bdd1c | 2020-10-05 13:17:09 -0400 | [diff] [blame] | 118 | static GrSurfaceProxyView sw_create_filtered_mask(GrRecordingContext* rContext, |
| 119 | const SkMatrix& viewMatrix, |
| 120 | const GrStyledShape& shape, |
| 121 | const SkMaskFilter* filter, |
Robert Phillips | 6e17ffe | 2020-10-06 14:52:11 -0400 | [diff] [blame] | 122 | const SkIRect& unclippedDevShapeBounds, |
Robert Phillips | c3bdd1c | 2020-10-05 13:17:09 -0400 | [diff] [blame] | 123 | const SkIRect& clipBounds, |
| 124 | SkIRect* drawRect, |
Robert Phillips | 6e17ffe | 2020-10-06 14:52:11 -0400 | [diff] [blame] | 125 | GrUniqueKey* key) { |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 126 | SkASSERT(filter); |
| 127 | SkASSERT(!shape.style().applies()); |
| 128 | |
Robert Phillips | d464feb | 2020-10-08 11:00:02 -0400 | [diff] [blame^] | 129 | auto threadSafeCache = rContext->priv().threadSafeCache(); |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 130 | |
Greg Daniel | 5c08249 | 2020-01-29 15:06:49 -0500 | [diff] [blame] | 131 | GrSurfaceProxyView filteredMaskView; |
Robert Phillips | 6e17ffe | 2020-10-06 14:52:11 -0400 | [diff] [blame] | 132 | sk_sp<SkData> data; |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 133 | |
Robert Phillips | 6e17ffe | 2020-10-06 14:52:11 -0400 | [diff] [blame] | 134 | if (key->isValid()) { |
Robert Phillips | d464feb | 2020-10-08 11:00:02 -0400 | [diff] [blame^] | 135 | std::tie(filteredMaskView, data) = threadSafeCache->findWithData(*key); |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Brian Salomon | d005b69 | 2020-04-01 15:47:05 -0400 | [diff] [blame] | 138 | if (filteredMaskView) { |
Robert Phillips | 6e17ffe | 2020-10-06 14:52:11 -0400 | [diff] [blame] | 139 | SkASSERT(data); |
Robert Phillips | c73c1af | 2020-09-29 13:47:39 -0400 | [diff] [blame] | 140 | SkASSERT(kMaskOrigin == filteredMaskView.origin()); |
| 141 | |
Robert Phillips | 6e17ffe | 2020-10-06 14:52:11 -0400 | [diff] [blame] | 142 | *drawRect = extract_draw_rect_from_data(data.get(), unclippedDevShapeBounds); |
Robert Phillips | 4a24da5 | 2016-12-14 09:00:07 -0500 | [diff] [blame] | 143 | |
Robert Phillips | 6e17ffe | 2020-10-06 14:52:11 -0400 | [diff] [blame] | 144 | SkDEBUGCODE(auto oldDrawRect = sw_calc_draw_rect(viewMatrix, shape, filter, clipBounds)); |
| 145 | SkASSERT(*drawRect == oldDrawRect); |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 146 | } else { |
Robert Phillips | c3bdd1c | 2020-10-05 13:17:09 -0400 | [diff] [blame] | 147 | SkStrokeRec::InitStyle fillOrHairline = shape.style().isSimpleHairline() |
| 148 | ? SkStrokeRec::kHairline_InitStyle |
| 149 | : SkStrokeRec::kFill_InitStyle; |
| 150 | |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 151 | // TODO: it seems like we could create an SkDraw here and set its fMatrix field rather |
| 152 | // than explicitly transforming the path to device space. |
| 153 | SkPath devPath; |
| 154 | |
| 155 | shape.asPath(&devPath); |
| 156 | |
| 157 | devPath.transform(viewMatrix); |
| 158 | |
| 159 | SkMask srcM, dstM; |
| 160 | if (!SkDraw::DrawToMask(devPath, &clipBounds, filter, &viewMatrix, &srcM, |
| 161 | SkMask::kComputeBoundsAndRenderImage_CreateMode, fillOrHairline)) { |
Robert Phillips | c3bdd1c | 2020-10-05 13:17:09 -0400 | [diff] [blame] | 162 | return {}; |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 163 | } |
| 164 | SkAutoMaskFreeImage autoSrc(srcM.fImage); |
| 165 | |
| 166 | SkASSERT(SkMask::kA8_Format == srcM.fFormat); |
| 167 | |
| 168 | if (!as_MFB(filter)->filterMask(&dstM, srcM, viewMatrix, nullptr)) { |
Robert Phillips | c3bdd1c | 2020-10-05 13:17:09 -0400 | [diff] [blame] | 169 | return {}; |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 170 | } |
| 171 | // this will free-up dstM when we're done (allocated in filterMask()) |
| 172 | SkAutoMaskFreeImage autoDst(dstM.fImage); |
| 173 | |
| 174 | if (clip_bounds_quick_reject(clipBounds, dstM.fBounds)) { |
Robert Phillips | c3bdd1c | 2020-10-05 13:17:09 -0400 | [diff] [blame] | 175 | return {}; |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | // we now have a device-aligned 8bit mask in dstM, ready to be drawn using |
| 179 | // the current clip (and identity matrix) and GrPaint settings |
| 180 | SkBitmap bm; |
| 181 | if (!bm.installPixels(SkImageInfo::MakeA8(dstM.fBounds.width(), dstM.fBounds.height()), |
| 182 | autoDst.release(), dstM.fRowBytes, mask_release_proc, nullptr)) { |
Robert Phillips | c3bdd1c | 2020-10-05 13:17:09 -0400 | [diff] [blame] | 183 | return {}; |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 184 | } |
| 185 | bm.setImmutable(); |
| 186 | |
Robert Phillips | c73c1af | 2020-09-29 13:47:39 -0400 | [diff] [blame] | 187 | GrBitmapTextureMaker maker(rContext, bm, SkBackingFit::kApprox); |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 188 | filteredMaskView = maker.view(GrMipmapped::kNo); |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 189 | if (!filteredMaskView.proxy()) { |
Robert Phillips | c3bdd1c | 2020-10-05 13:17:09 -0400 | [diff] [blame] | 190 | return {}; |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 191 | } |
| 192 | |
Brian Salomon | d005b69 | 2020-04-01 15:47:05 -0400 | [diff] [blame] | 193 | SkASSERT(kMaskOrigin == filteredMaskView.origin()); |
Robert Phillips | 59b39e7 | 2018-08-30 11:51:25 -0400 | [diff] [blame] | 194 | |
Robert Phillips | c3bdd1c | 2020-10-05 13:17:09 -0400 | [diff] [blame] | 195 | *drawRect = dstM.fBounds; |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 196 | |
Robert Phillips | 6e17ffe | 2020-10-06 14:52:11 -0400 | [diff] [blame] | 197 | if (key->isValid()) { |
| 198 | key->setCustomData(create_data(*drawRect, unclippedDevShapeBounds)); |
Robert Phillips | d464feb | 2020-10-08 11:00:02 -0400 | [diff] [blame^] | 199 | std::tie(filteredMaskView, data) = threadSafeCache->addWithData(*key, filteredMaskView); |
Robert Phillips | 6e17ffe | 2020-10-06 14:52:11 -0400 | [diff] [blame] | 200 | // If we got a different view back from 'addWithData' it could have a different drawRect |
| 201 | *drawRect = extract_draw_rect_from_data(data.get(), unclippedDevShapeBounds); |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 202 | } |
Robert Phillips | 48ce22b | 2018-03-23 10:33:12 -0400 | [diff] [blame] | 203 | } |
Robert Phillips | c7c2baf | 2018-03-08 09:51:04 -0500 | [diff] [blame] | 204 | |
Robert Phillips | c3bdd1c | 2020-10-05 13:17:09 -0400 | [diff] [blame] | 205 | return filteredMaskView; |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Robert Phillips | 40b05c3 | 2019-09-20 12:40:55 -0400 | [diff] [blame] | 208 | // Create a mask of 'shape' and return the resulting renderTargetContext |
| 209 | static std::unique_ptr<GrRenderTargetContext> create_mask_GPU(GrRecordingContext* context, |
| 210 | const SkIRect& maskRect, |
| 211 | const SkMatrix& origViewMatrix, |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 212 | const GrStyledShape& shape, |
Robert Phillips | 40b05c3 | 2019-09-20 12:40:55 -0400 | [diff] [blame] | 213 | int sampleCnt) { |
Chris Dalton | 0493fbd | 2019-09-18 15:49:46 -0600 | [diff] [blame] | 214 | // Use GrResourceProvider::MakeApprox to implement our own approximate size matching, but demand |
| 215 | // a "SkBackingFit::kExact" size match on the actual render target. We do this because the |
| 216 | // filter will reach outside the src bounds, so we need to pre-clear these values to ensure a |
| 217 | // "decal" sampling effect (i.e., ensure reads outside the src bounds return alpha=0). |
| 218 | // |
| 219 | // FIXME: Reads outside the left and top edges will actually clamp to the edge pixel. And in the |
| 220 | // event that MakeApprox does not change the size, reads outside the right and/or bottom will do |
| 221 | // the same. We should offset our filter within the render target and expand the size as needed |
| 222 | // to guarantee at least 1px of padding on all sides. |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 223 | auto approxSize = GrResourceProvider::MakeApprox(maskRect.size()); |
Greg Daniel | e20fcad | 2020-01-08 11:52:34 -0500 | [diff] [blame] | 224 | auto rtContext = GrRenderTargetContext::MakeWithFallback( |
| 225 | context, GrColorType::kAlpha_8, nullptr, SkBackingFit::kExact, approxSize, sampleCnt, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 226 | GrMipmapped::kNo, GrProtected::kNo, kMaskOrigin); |
robertphillips | d728f0c | 2016-11-21 11:05:03 -0800 | [diff] [blame] | 227 | if (!rtContext) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 228 | return nullptr; |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Chris Dalton | 0493fbd | 2019-09-18 15:49:46 -0600 | [diff] [blame] | 231 | rtContext->clear(SK_PMColor4fTRANSPARENT); |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 232 | |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 233 | GrPaint maskPaint; |
| 234 | maskPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op); |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 235 | |
| 236 | // setup new clip |
Michael Ludwig | d1d997e | 2020-06-04 15:52:44 -0400 | [diff] [blame] | 237 | GrFixedClip clip(rtContext->dimensions(), SkIRect::MakeWH(maskRect.width(), maskRect.height())); |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 238 | |
Brian Salomon | d005b69 | 2020-04-01 15:47:05 -0400 | [diff] [blame] | 239 | // Draw the mask into maskTexture with the path's integerized top-left at the origin using |
| 240 | // maskPaint. |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 241 | SkMatrix viewMatrix = origViewMatrix; |
| 242 | viewMatrix.postTranslate(-SkIntToScalar(maskRect.fLeft), -SkIntToScalar(maskRect.fTop)); |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 243 | rtContext->drawShape(&clip, std::move(maskPaint), GrAA::kYes, viewMatrix, shape); |
Robert Phillips | 40b05c3 | 2019-09-20 12:40:55 -0400 | [diff] [blame] | 244 | return rtContext; |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 247 | 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] | 248 | SkIRect* devBounds) { |
| 249 | SkRect shapeBounds = shape.styledBounds(); |
| 250 | if (shapeBounds.isEmpty()) { |
| 251 | return false; |
| 252 | } |
| 253 | SkRect shapeDevBounds; |
| 254 | matrix.mapRect(&shapeDevBounds, shapeBounds); |
| 255 | // Even though these are "unclipped" bounds we still clip to the int32_t range. |
| 256 | // This is the largest int32_t that is representable exactly as a float. The next 63 larger ints |
| 257 | // would round down to this value when cast to a float, but who really cares. |
| 258 | // INT32_MIN is exactly representable. |
| 259 | static constexpr int32_t kMaxInt = 2147483520; |
| 260 | if (!shapeDevBounds.intersect(SkRect::MakeLTRB(INT32_MIN, INT32_MIN, kMaxInt, kMaxInt))) { |
| 261 | return false; |
| 262 | } |
| 263 | // Make sure that the resulting SkIRect can have representable width and height |
| 264 | if (SkScalarRoundToInt(shapeDevBounds.width()) > kMaxInt || |
| 265 | SkScalarRoundToInt(shapeDevBounds.height()) > kMaxInt) { |
| 266 | return false; |
| 267 | } |
| 268 | shapeDevBounds.roundOut(devBounds); |
| 269 | return true; |
| 270 | } |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 271 | |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 272 | // Gets the shape bounds, the clip bounds, and the intersection (if any). Returns false if there |
| 273 | // is no intersection. |
| 274 | static bool get_shape_and_clip_bounds(GrRenderTargetContext* renderTargetContext, |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 275 | const GrClip* clip, |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 276 | const GrStyledShape& shape, |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 277 | const SkMatrix& matrix, |
| 278 | SkIRect* unclippedDevShapeBounds, |
| 279 | SkIRect* devClipBounds) { |
| 280 | // compute bounds as intersection of rt size, clip, and path |
Michael Ludwig | e06a897 | 2020-06-11 10:29:00 -0400 | [diff] [blame] | 281 | *devClipBounds = clip ? clip->getConservativeBounds() |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 282 | : SkIRect::MakeWH(renderTargetContext->width(), |
| 283 | renderTargetContext->height()); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 284 | |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 285 | if (!get_unclipped_shape_dev_bounds(shape, matrix, unclippedDevShapeBounds)) { |
Brian Salomon | 44207f3 | 2020-01-06 15:20:18 -0500 | [diff] [blame] | 286 | *unclippedDevShapeBounds = SkIRect::MakeEmpty(); |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 287 | return false; |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 288 | } |
| 289 | |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 290 | return true; |
| 291 | } |
| 292 | |
Robert Phillips | 52c17c4 | 2020-10-05 11:55:59 -0400 | [diff] [blame] | 293 | // The key and clip-bounds are computed together because the caching decision can impact the |
Robert Phillips | 6e17ffe | 2020-10-06 14:52:11 -0400 | [diff] [blame] | 294 | // clip-bound - since we only cache un-clipped masks the clip can be removed entirely. |
Robert Phillips | 52c17c4 | 2020-10-05 11:55:59 -0400 | [diff] [blame] | 295 | // A 'false' return value indicates that the shape is known to be clipped away. |
| 296 | static bool compute_key_and_clip_bounds(GrUniqueKey* maskKey, |
| 297 | SkIRect* boundsForClip, |
| 298 | const GrCaps* caps, |
| 299 | const SkMatrix& viewMatrix, |
| 300 | bool inverseFilled, |
| 301 | const SkMaskFilterBase* maskFilter, |
| 302 | const GrStyledShape& shape, |
| 303 | const SkIRect& unclippedDevShapeBounds, |
| 304 | const SkIRect& devClipBounds) { |
| 305 | *boundsForClip = devClipBounds; |
| 306 | |
| 307 | #ifndef SK_DISABLE_MASKFILTERED_MASK_CACHING |
| 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. |
| 310 | bool useCache = !inverseFilled && viewMatrix.preservesAxisAlignment() && |
| 311 | shape.hasUnstyledKey() && as_MFB(maskFilter)->asABlur(nullptr); |
| 312 | |
| 313 | if (useCache) { |
| 314 | SkIRect clippedMaskRect, unClippedMaskRect; |
| 315 | maskFilter->canFilterMaskGPU(shape, unclippedDevShapeBounds, devClipBounds, |
| 316 | viewMatrix, &clippedMaskRect); |
| 317 | maskFilter->canFilterMaskGPU(shape, unclippedDevShapeBounds, unclippedDevShapeBounds, |
| 318 | viewMatrix, &unClippedMaskRect); |
| 319 | if (clippedMaskRect.isEmpty()) { |
| 320 | return false; |
| 321 | } |
| 322 | |
| 323 | // Use the cache only if >50% of the filtered mask is visible. |
| 324 | int unclippedWidth = unClippedMaskRect.width(); |
| 325 | int unclippedHeight = unClippedMaskRect.height(); |
| 326 | int64_t unclippedArea = sk_64_mul(unclippedWidth, unclippedHeight); |
| 327 | int64_t clippedArea = sk_64_mul(clippedMaskRect.width(), clippedMaskRect.height()); |
| 328 | int maxTextureSize = caps->maxTextureSize(); |
| 329 | if (unclippedArea > 2 * clippedArea || unclippedWidth > maxTextureSize || |
| 330 | unclippedHeight > maxTextureSize) { |
| 331 | useCache = false; |
| 332 | } else { |
| 333 | // Make the clip not affect the mask |
| 334 | *boundsForClip = unclippedDevShapeBounds; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | if (useCache) { |
| 339 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 340 | GrUniqueKey::Builder builder(maskKey, kDomain, 5 + 2 + shape.unstyledKeySize(), |
| 341 | "Mask Filtered Masks"); |
| 342 | |
| 343 | // We require the upper left 2x2 of the matrix to match exactly for a cache hit. |
| 344 | SkScalar sx = viewMatrix.get(SkMatrix::kMScaleX); |
| 345 | SkScalar sy = viewMatrix.get(SkMatrix::kMScaleY); |
| 346 | SkScalar kx = viewMatrix.get(SkMatrix::kMSkewX); |
| 347 | SkScalar ky = viewMatrix.get(SkMatrix::kMSkewY); |
| 348 | SkScalar tx = viewMatrix.get(SkMatrix::kMTransX); |
| 349 | SkScalar ty = viewMatrix.get(SkMatrix::kMTransY); |
Robert Phillips | 6e17ffe | 2020-10-06 14:52:11 -0400 | [diff] [blame] | 350 | // Allow 8 bits each in x and y of subpixel positioning. But, note that we're allowing |
| 351 | // reuse for integer translations. |
Robert Phillips | 52c17c4 | 2020-10-05 11:55:59 -0400 | [diff] [blame] | 352 | SkFixed fracX = SkScalarToFixed(SkScalarFraction(tx)) & 0x0000FF00; |
| 353 | SkFixed fracY = SkScalarToFixed(SkScalarFraction(ty)) & 0x0000FF00; |
| 354 | |
| 355 | builder[0] = SkFloat2Bits(sx); |
| 356 | builder[1] = SkFloat2Bits(sy); |
| 357 | builder[2] = SkFloat2Bits(kx); |
| 358 | builder[3] = SkFloat2Bits(ky); |
| 359 | // Distinguish between hairline and filled paths. For hairlines, we also need to include |
| 360 | // the cap. (SW grows hairlines by 0.5 pixel with round and square caps). Note that |
| 361 | // stroke-and-fill of hairlines is turned into pure fill by SkStrokeRec, so this covers |
| 362 | // all cases we might see. |
| 363 | uint32_t styleBits = shape.style().isSimpleHairline() |
| 364 | ? ((shape.style().strokeRec().getCap() << 1) | 1) |
| 365 | : 0; |
| 366 | builder[4] = fracX | (fracY >> 8) | (styleBits << 16); |
| 367 | |
| 368 | SkMaskFilterBase::BlurRec rec; |
| 369 | SkAssertResult(as_MFB(maskFilter)->asABlur(&rec)); |
| 370 | |
| 371 | builder[5] = rec.fStyle; // TODO: we could put this with the other style bits |
| 372 | builder[6] = SkFloat2Bits(rec.fSigma); |
| 373 | shape.writeUnstyledKey(&builder[7]); |
| 374 | } |
| 375 | #endif |
| 376 | |
| 377 | return true; |
| 378 | } |
| 379 | |
Robert Phillips | fde67e4 | 2020-10-07 15:33:43 -0400 | [diff] [blame] | 380 | static GrSurfaceProxyView hw_create_filtered_mask(GrDirectContext* dContext, |
Robert Phillips | c3bdd1c | 2020-10-05 13:17:09 -0400 | [diff] [blame] | 381 | GrRenderTargetContext* renderTargetContext, |
| 382 | const SkMatrix& viewMatrix, |
| 383 | const GrStyledShape& shape, |
| 384 | const SkMaskFilterBase* filter, |
| 385 | const SkIRect& unclippedDevShapeBounds, |
| 386 | const SkIRect& clipBounds, |
| 387 | SkIRect* maskRect, |
Robert Phillips | 6e17ffe | 2020-10-06 14:52:11 -0400 | [diff] [blame] | 388 | GrUniqueKey* key) { |
Robert Phillips | fde67e4 | 2020-10-07 15:33:43 -0400 | [diff] [blame] | 389 | if (!filter->canFilterMaskGPU(shape, |
| 390 | unclippedDevShapeBounds, |
| 391 | clipBounds, |
| 392 | viewMatrix, |
| 393 | maskRect)) { |
| 394 | return {}; |
| 395 | } |
Robert Phillips | c3bdd1c | 2020-10-05 13:17:09 -0400 | [diff] [blame] | 396 | |
Robert Phillips | fde67e4 | 2020-10-07 15:33:43 -0400 | [diff] [blame] | 397 | if (clip_bounds_quick_reject(clipBounds, *maskRect)) { |
| 398 | // clipped out |
| 399 | return {}; |
| 400 | } |
| 401 | |
Robert Phillips | d464feb | 2020-10-08 11:00:02 -0400 | [diff] [blame^] | 402 | auto threadSafeCache = dContext->priv().threadSafeCache(); |
Robert Phillips | fde67e4 | 2020-10-07 15:33:43 -0400 | [diff] [blame] | 403 | |
| 404 | GrSurfaceProxyView lazyView; |
Robert Phillips | d464feb | 2020-10-08 11:00:02 -0400 | [diff] [blame^] | 405 | sk_sp<GrThreadSafeCache::Trampoline> trampoline; |
Robert Phillips | fde67e4 | 2020-10-07 15:33:43 -0400 | [diff] [blame] | 406 | |
| 407 | if (key->isValid()) { |
| 408 | // In this case, we want GPU-filtered masks to have priority over SW-generated ones so |
| 409 | // we pre-emptively add a lazy-view to the cache and fill it in later. |
Robert Phillips | d464feb | 2020-10-08 11:00:02 -0400 | [diff] [blame^] | 410 | std::tie(lazyView, trampoline) = GrThreadSafeCache::CreateLazyView( |
Robert Phillips | fde67e4 | 2020-10-07 15:33:43 -0400 | [diff] [blame] | 411 | dContext, GrColorType::kAlpha_8, maskRect->size(), |
| 412 | kMaskOrigin, SkBackingFit::kApprox); |
| 413 | if (!lazyView) { |
| 414 | return {}; // fall back to a SW-created mask - 'create_mask_GPU' probably won't succeed |
Robert Phillips | c3bdd1c | 2020-10-05 13:17:09 -0400 | [diff] [blame] | 415 | } |
| 416 | |
Robert Phillips | fde67e4 | 2020-10-07 15:33:43 -0400 | [diff] [blame] | 417 | key->setCustomData(create_data(*maskRect, unclippedDevShapeBounds)); |
Robert Phillips | d464feb | 2020-10-08 11:00:02 -0400 | [diff] [blame^] | 418 | auto [cachedView, data] = threadSafeCache->findOrAddWithData(*key, lazyView); |
Robert Phillips | fde67e4 | 2020-10-07 15:33:43 -0400 | [diff] [blame] | 419 | if (cachedView != lazyView) { |
| 420 | // In this case, the gpu-thread lost out to a recording thread - use its result. |
| 421 | SkASSERT(data); |
| 422 | SkASSERT(cachedView.asTextureProxy()); |
| 423 | SkASSERT(cachedView.origin() == kMaskOrigin); |
Robert Phillips | c3bdd1c | 2020-10-05 13:17:09 -0400 | [diff] [blame] | 424 | |
Robert Phillips | fde67e4 | 2020-10-07 15:33:43 -0400 | [diff] [blame] | 425 | *maskRect = extract_draw_rect_from_data(data.get(), unclippedDevShapeBounds); |
| 426 | return cachedView; |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | std::unique_ptr<GrRenderTargetContext> maskRTC(create_mask_GPU( |
| 431 | dContext, |
| 432 | *maskRect, |
| 433 | viewMatrix, |
| 434 | shape, |
| 435 | renderTargetContext->numSamples())); |
| 436 | if (!maskRTC) { |
Robert Phillips | 6e17ffe | 2020-10-06 14:52:11 -0400 | [diff] [blame] | 437 | if (key->isValid()) { |
Robert Phillips | fde67e4 | 2020-10-07 15:33:43 -0400 | [diff] [blame] | 438 | // It is very unlikely that 'create_mask_GPU' will fail after 'CreateLazyView' |
| 439 | // succeeded but, if it does, remove the lazy-view from the cache and fallback to |
| 440 | // a SW-created mask. Note that any recording threads that glommed onto the |
| 441 | // lazy-view will have to, later, drop those draws. |
Robert Phillips | d464feb | 2020-10-08 11:00:02 -0400 | [diff] [blame^] | 442 | threadSafeCache->remove(*key); |
Robert Phillips | c3bdd1c | 2020-10-05 13:17:09 -0400 | [diff] [blame] | 443 | } |
Robert Phillips | fde67e4 | 2020-10-07 15:33:43 -0400 | [diff] [blame] | 444 | return {}; |
| 445 | } |
Robert Phillips | c3bdd1c | 2020-10-05 13:17:09 -0400 | [diff] [blame] | 446 | |
Robert Phillips | fde67e4 | 2020-10-07 15:33:43 -0400 | [diff] [blame] | 447 | auto filteredMaskView = filter->filterMaskGPU(dContext, |
| 448 | maskRTC->readSurfaceView(), |
| 449 | maskRTC->colorInfo().colorType(), |
| 450 | maskRTC->colorInfo().alphaType(), |
| 451 | viewMatrix, |
| 452 | *maskRect); |
| 453 | if (!filteredMaskView) { |
| 454 | if (key->isValid()) { |
| 455 | // Remove the lazy-view from the cache and fallback to a SW-created mask. Note that |
| 456 | // any recording threads that glommed onto the lazy-view will have to, later, drop |
| 457 | // those draws. |
Robert Phillips | d464feb | 2020-10-08 11:00:02 -0400 | [diff] [blame^] | 458 | threadSafeCache->remove(*key); |
Robert Phillips | c3bdd1c | 2020-10-05 13:17:09 -0400 | [diff] [blame] | 459 | } |
Robert Phillips | fde67e4 | 2020-10-07 15:33:43 -0400 | [diff] [blame] | 460 | return {}; |
| 461 | } |
| 462 | |
| 463 | if (key->isValid()) { |
| 464 | SkASSERT(filteredMaskView.dimensions() == lazyView.dimensions()); |
| 465 | SkASSERT(filteredMaskView.swizzle() == lazyView.swizzle()); |
| 466 | SkASSERT(filteredMaskView.origin() == lazyView.origin()); |
| 467 | |
| 468 | trampoline->fProxy = filteredMaskView.asTextureProxyRef(); |
| 469 | return lazyView; |
Robert Phillips | c3bdd1c | 2020-10-05 13:17:09 -0400 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | return filteredMaskView; |
| 473 | } |
| 474 | |
| 475 | static void draw_shape_with_mask_filter(GrRecordingContext* rContext, |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 476 | GrRenderTargetContext* renderTargetContext, |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 477 | const GrClip* clip, |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 478 | GrPaint&& paint, |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 479 | const SkMatrix& viewMatrix, |
| 480 | const SkMaskFilterBase* maskFilter, |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 481 | const GrStyledShape& origShape) { |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 482 | SkASSERT(maskFilter); |
| 483 | |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 484 | const GrStyledShape* shape = &origShape; |
| 485 | SkTLazy<GrStyledShape> tmpShape; |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 486 | |
| 487 | if (origShape.style().applies()) { |
| 488 | SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix); |
| 489 | if (0 == styleScale) { |
| 490 | return; |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 491 | } |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 492 | |
| 493 | tmpShape.init(origShape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale)); |
John Stiles | a008b0f | 2020-08-16 08:48:02 -0400 | [diff] [blame] | 494 | if (tmpShape->isEmpty()) { |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 495 | return; |
| 496 | } |
| 497 | |
| 498 | shape = tmpShape.get(); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 499 | } |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 500 | |
Robert Phillips | c3bdd1c | 2020-10-05 13:17:09 -0400 | [diff] [blame] | 501 | if (maskFilter->directFilterMaskGPU(rContext, renderTargetContext, std::move(paint), clip, |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 502 | viewMatrix, *shape)) { |
Robert Phillips | b889f20 | 2018-08-17 19:55:59 +0000 | [diff] [blame] | 503 | // the mask filter was able to draw itself directly, so there's nothing |
| 504 | // left to do. |
| 505 | return; |
| 506 | } |
Mike Klein | 1688507 | 2018-12-11 09:54:31 -0500 | [diff] [blame] | 507 | assert_alive(paint); |
Robert Phillips | b889f20 | 2018-08-17 19:55:59 +0000 | [diff] [blame] | 508 | |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 509 | // If the path is hairline, ignore inverse fill. |
| 510 | bool inverseFilled = shape->inverseFilled() && |
| 511 | !GrPathRenderer::IsStrokeHairlineOrEquivalent(shape->style(), |
| 512 | viewMatrix, nullptr); |
| 513 | |
| 514 | SkIRect unclippedDevShapeBounds, devClipBounds; |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 515 | if (!get_shape_and_clip_bounds(renderTargetContext, clip, *shape, viewMatrix, |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 516 | &unclippedDevShapeBounds, &devClipBounds)) { |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 517 | // TODO: just cons up an opaque mask here |
| 518 | if (!inverseFilled) { |
| 519 | return; |
| 520 | } |
| 521 | } |
| 522 | |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 523 | GrUniqueKey maskKey; |
Robert Phillips | 52c17c4 | 2020-10-05 11:55:59 -0400 | [diff] [blame] | 524 | SkIRect boundsForClip; |
| 525 | if (!compute_key_and_clip_bounds(&maskKey, &boundsForClip, |
| 526 | renderTargetContext->caps(), |
| 527 | viewMatrix, inverseFilled, |
| 528 | maskFilter, *shape, |
| 529 | unclippedDevShapeBounds, |
| 530 | devClipBounds)) { |
Robert Phillips | c3bdd1c | 2020-10-05 13:17:09 -0400 | [diff] [blame] | 531 | return; // 'shape' was entirely clipped out |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 532 | } |
| 533 | |
Robert Phillips | c3bdd1c | 2020-10-05 13:17:09 -0400 | [diff] [blame] | 534 | GrSurfaceProxyView filteredMaskView; |
Robert Phillips | 31c080b | 2018-08-23 10:45:32 -0400 | [diff] [blame] | 535 | SkIRect maskRect; |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 536 | |
Robert Phillips | fde67e4 | 2020-10-07 15:33:43 -0400 | [diff] [blame] | 537 | if (auto dContext = rContext->asDirectContext()) { |
| 538 | filteredMaskView = hw_create_filtered_mask(dContext, renderTargetContext, |
Robert Phillips | c3bdd1c | 2020-10-05 13:17:09 -0400 | [diff] [blame] | 539 | viewMatrix, *shape, maskFilter, |
| 540 | unclippedDevShapeBounds, boundsForClip, |
Robert Phillips | 6e17ffe | 2020-10-06 14:52:11 -0400 | [diff] [blame] | 541 | &maskRect, &maskKey); |
Brian Salomon | d005b69 | 2020-04-01 15:47:05 -0400 | [diff] [blame] | 542 | if (filteredMaskView) { |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 543 | if (draw_mask(renderTargetContext, clip, viewMatrix, maskRect, std::move(paint), |
Greg Daniel | 5c08249 | 2020-01-29 15:06:49 -0500 | [diff] [blame] | 544 | std::move(filteredMaskView))) { |
Robert Phillips | 20390c3 | 2018-08-17 11:01:03 -0400 | [diff] [blame] | 545 | // This path is completely drawn |
| 546 | return; |
| 547 | } |
Mike Klein | 1688507 | 2018-12-11 09:54:31 -0500 | [diff] [blame] | 548 | assert_alive(paint); |
Robert Phillips | 20390c3 | 2018-08-17 11:01:03 -0400 | [diff] [blame] | 549 | } |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 550 | } |
| 551 | |
Robert Phillips | c3bdd1c | 2020-10-05 13:17:09 -0400 | [diff] [blame] | 552 | // Either HW mask rendering failed or we're in a DDL recording thread |
| 553 | filteredMaskView = sw_create_filtered_mask(rContext, |
| 554 | viewMatrix, *shape, maskFilter, |
Robert Phillips | 6e17ffe | 2020-10-06 14:52:11 -0400 | [diff] [blame] | 555 | unclippedDevShapeBounds, boundsForClip, |
| 556 | &maskRect, &maskKey); |
Robert Phillips | c3bdd1c | 2020-10-05 13:17:09 -0400 | [diff] [blame] | 557 | if (filteredMaskView) { |
| 558 | if (draw_mask(renderTargetContext, clip, viewMatrix, maskRect, std::move(paint), |
| 559 | std::move(filteredMaskView))) { |
| 560 | return; |
| 561 | } |
| 562 | assert_alive(paint); |
| 563 | } |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 564 | } |
| 565 | |
Robert Phillips | 7af8fe5 | 2019-02-14 17:27:00 -0500 | [diff] [blame] | 566 | void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext* context, |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 567 | GrRenderTargetContext* renderTargetContext, |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 568 | const GrClip* clip, |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 569 | const GrStyledShape& shape, |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 570 | GrPaint&& paint, |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 571 | const SkMatrix& viewMatrix, |
| 572 | const SkMaskFilter* mf) { |
Robert Phillips | 793324c | 2018-08-24 13:53:56 -0400 | [diff] [blame] | 573 | draw_shape_with_mask_filter(context, renderTargetContext, clip, std::move(paint), |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 574 | viewMatrix, as_MFB(mf), shape); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 575 | } |
| 576 | |
Robert Phillips | 6989370 | 2019-02-22 11:16:30 -0500 | [diff] [blame] | 577 | void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext* context, |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 578 | GrRenderTargetContext* renderTargetContext, |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 579 | const GrClip* clip, |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 580 | const SkPaint& paint, |
Brian Osman | 449b115 | 2020-04-15 16:43:00 -0400 | [diff] [blame] | 581 | const SkMatrixProvider& matrixProvider, |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 582 | const GrStyledShape& shape) { |
Robert Phillips | 9eb0002 | 2020-06-30 15:30:12 -0400 | [diff] [blame] | 583 | if (context->abandoned()) { |
Robert Phillips | bebfd41 | 2018-03-08 11:07:23 -0500 | [diff] [blame] | 584 | return; |
| 585 | } |
| 586 | |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 587 | GrPaint grPaint; |
Brian Osman | 449b115 | 2020-04-15 16:43:00 -0400 | [diff] [blame] | 588 | if (!SkPaintToGrPaint(context, renderTargetContext->colorInfo(), paint, matrixProvider, |
| 589 | &grPaint)) { |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 590 | return; |
| 591 | } |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 592 | |
Brian Osman | 449b115 | 2020-04-15 16:43:00 -0400 | [diff] [blame] | 593 | const SkMatrix& viewMatrix(matrixProvider.localToDevice()); |
Mike Reed | 80747ef | 2018-01-23 15:29:32 -0500 | [diff] [blame] | 594 | SkMaskFilterBase* mf = as_MFB(paint.getMaskFilter()); |
Mike Reed | bfadcf0 | 2018-01-20 22:24:21 +0000 | [diff] [blame] | 595 | if (mf && !mf->hasFragmentProcessor()) { |
Robert Phillips | a29a956 | 2016-10-20 09:40:55 -0400 | [diff] [blame] | 596 | // The MaskFilter wasn't already handled in SkPaintToGrPaint |
Robert Phillips | 793324c | 2018-08-24 13:53:56 -0400 | [diff] [blame] | 597 | draw_shape_with_mask_filter(context, renderTargetContext, clip, std::move(grPaint), |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 598 | viewMatrix, mf, shape); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 599 | } else { |
Robert Phillips | 793324c | 2018-08-24 13:53:56 -0400 | [diff] [blame] | 600 | GrAA aa = GrAA(paint.isAntiAlias()); |
Robert Phillips | 27927a5 | 2018-08-20 13:18:12 -0400 | [diff] [blame] | 601 | renderTargetContext->drawShape(clip, std::move(grPaint), aa, viewMatrix, shape); |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 602 | } |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 603 | } |