blob: 2b0c50b7ee0c0bcfa0d461525723acc22c439523 [file] [log] [blame]
robertphillipsccb1b572015-05-27 11:02:55 -07001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrBlurUtils.h"
Robert Phillipsc7c2baf2018-03-08 09:51:04 -05009
Robert Phillipsfde67e42020-10-07 15:33:43 -040010#include "include/gpu/GrDirectContext.h"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040011#include "include/gpu/GrRecordingContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrCaps.h"
Adlai Hollera0693042020-10-14 11:23:11 -040013#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrFixedClip.h"
15#include "src/gpu/GrProxyProvider.h"
16#include "src/gpu/GrRecordingContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/GrSoftwarePathRenderer.h"
18#include "src/gpu/GrStyle.h"
Brian Salomoneebe7352020-12-09 16:37:04 -050019#include "src/gpu/GrSurfaceDrawContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040020#include "src/gpu/GrTextureProxy.h"
Robert Phillipsd464feb2020-10-08 11:00:02 -040021#include "src/gpu/GrThreadSafeCache.h"
Brian Salomon27c42022021-04-28 12:39:21 -040022#include "src/gpu/SkGr.h"
Brian Salomonb8f098d2020-01-07 11:15:44 -050023#include "src/gpu/effects/GrTextureEffect.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000024#include "src/gpu/geometry/GrStyledShape.h"
Robert Phillipsc7c2baf2018-03-08 09:51:04 -050025
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "include/core/SkPaint.h"
27#include "src/core/SkDraw.h"
28#include "src/core/SkMaskFilterBase.h"
Brian Osman449b1152020-04-15 16:43:00 -040029#include "src/core/SkMatrixProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/core/SkTLazy.h"
31#include "src/gpu/SkGr.h"
robertphillipsccb1b572015-05-27 11:02:55 -070032
33static bool clip_bounds_quick_reject(const SkIRect& clipBounds, const SkIRect& rect) {
34 return clipBounds.isEmpty() || rect.isEmpty() || !SkIRect::Intersects(clipBounds, rect);
35}
36
Brian Salomond005b692020-04-01 15:47:05 -040037static constexpr auto kMaskOrigin = kTopLeft_GrSurfaceOrigin;
38
robertphillipsccb1b572015-05-27 11:02:55 -070039// Draw a mask using the supplied paint. Since the coverage/geometry
40// is already burnt into the mask this boils down to a rect draw.
41// Return true if the mask was successfully drawn.
Brian Salomon1aa1f5f2020-12-11 17:25:17 -050042static bool draw_mask(GrSurfaceDrawContext* surfaceDrawContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -040043 const GrClip* clip,
robertphillipsccb1b572015-05-27 11:02:55 -070044 const SkMatrix& viewMatrix,
Chris Dalton35619552021-03-10 19:20:43 -070045 const SkIRect& maskBounds,
Brian Salomon82f44312017-01-11 13:42:54 -050046 GrPaint&& paint,
Greg Daniel5c082492020-01-29 15:06:49 -050047 GrSurfaceProxyView mask) {
Brian Salomon82f44312017-01-11 13:42:54 -050048 SkMatrix inverse;
49 if (!viewMatrix.invert(&inverse)) {
50 return false;
51 }
Robert Phillips4a24da52016-12-14 09:00:07 -050052
Brian Salomonb43d6992021-01-05 14:37:40 -050053 mask.concatSwizzle(GrSwizzle("aaaa"));
54
Chris Dalton35619552021-03-10 19:20:43 -070055 SkMatrix matrix = SkMatrix::Translate(-SkIntToScalar(maskBounds.fLeft),
56 -SkIntToScalar(maskBounds.fTop));
Brian Salomon2ebd0c82016-10-03 17:15:28 -040057 matrix.preConcat(viewMatrix);
John Stiles41d91b62020-07-21 14:39:40 -040058 paint.setCoverageFragmentProcessor(
Greg Danield2ccbb52020-02-05 10:45:39 -050059 GrTextureEffect::Make(std::move(mask), kUnknown_SkAlphaType, matrix));
robertphillipsccb1b572015-05-27 11:02:55 -070060
Chris Dalton35619552021-03-10 19:20:43 -070061 surfaceDrawContext->fillPixelsWithLocalMatrix(clip, std::move(paint), maskBounds, inverse);
robertphillipsccb1b572015-05-27 11:02:55 -070062 return true;
63}
64
Robert Phillipsc7c2baf2018-03-08 09:51:04 -050065static void mask_release_proc(void* addr, void* /*context*/) {
66 SkMask::FreeImage(addr);
67}
68
Robert Phillips6e17ffe2020-10-06 14:52:11 -040069// This stores the mapping from an unclipped, integerized, device-space, shape bounds to
70// the filtered mask's draw rect.
71struct DrawRectData {
72 SkIVector fOffset;
73 SkISize fSize;
74};
75
76static sk_sp<SkData> create_data(const SkIRect& drawRect, const SkIRect& origDevBounds) {
77
78 DrawRectData drawRectData { {drawRect.fLeft - origDevBounds.fLeft,
79 drawRect.fTop - origDevBounds.fTop},
80 drawRect.size() };
81
82 return SkData::MakeWithCopy(&drawRectData, sizeof(drawRectData));
83}
84
85static SkIRect extract_draw_rect_from_data(SkData* data, const SkIRect& origDevBounds) {
86 auto drawRectData = static_cast<const DrawRectData*>(data->data());
87
88 return SkIRect::MakeXYWH(origDevBounds.fLeft + drawRectData->fOffset.fX,
89 origDevBounds.fTop + drawRectData->fOffset.fY,
90 drawRectData->fSize.fWidth,
91 drawRectData->fSize.fHeight);
92}
93
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -040094static GrSurfaceProxyView sw_create_filtered_mask(GrRecordingContext* rContext,
95 const SkMatrix& viewMatrix,
96 const GrStyledShape& shape,
97 const SkMaskFilter* filter,
Robert Phillips6e17ffe2020-10-06 14:52:11 -040098 const SkIRect& unclippedDevShapeBounds,
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -040099 const SkIRect& clipBounds,
100 SkIRect* drawRect,
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400101 GrUniqueKey* key) {
Robert Phillips27927a52018-08-20 13:18:12 -0400102 SkASSERT(filter);
103 SkASSERT(!shape.style().applies());
104
Robert Phillipsd464feb2020-10-08 11:00:02 -0400105 auto threadSafeCache = rContext->priv().threadSafeCache();
Robert Phillips27927a52018-08-20 13:18:12 -0400106
Greg Daniel5c082492020-01-29 15:06:49 -0500107 GrSurfaceProxyView filteredMaskView;
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400108 sk_sp<SkData> data;
Robert Phillips27927a52018-08-20 13:18:12 -0400109
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400110 if (key->isValid()) {
Robert Phillipsd464feb2020-10-08 11:00:02 -0400111 std::tie(filteredMaskView, data) = threadSafeCache->findWithData(*key);
robertphillipsccb1b572015-05-27 11:02:55 -0700112 }
113
Brian Salomond005b692020-04-01 15:47:05 -0400114 if (filteredMaskView) {
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400115 SkASSERT(data);
Robert Phillipsc73c1af2020-09-29 13:47:39 -0400116 SkASSERT(kMaskOrigin == filteredMaskView.origin());
117
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400118 *drawRect = extract_draw_rect_from_data(data.get(), unclippedDevShapeBounds);
Robert Phillips31c080b2018-08-23 10:45:32 -0400119 } else {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400120 SkStrokeRec::InitStyle fillOrHairline = shape.style().isSimpleHairline()
121 ? SkStrokeRec::kHairline_InitStyle
122 : SkStrokeRec::kFill_InitStyle;
123
Robert Phillips31c080b2018-08-23 10:45:32 -0400124 // TODO: it seems like we could create an SkDraw here and set its fMatrix field rather
125 // than explicitly transforming the path to device space.
126 SkPath devPath;
127
128 shape.asPath(&devPath);
129
130 devPath.transform(viewMatrix);
131
132 SkMask srcM, dstM;
133 if (!SkDraw::DrawToMask(devPath, &clipBounds, filter, &viewMatrix, &srcM,
134 SkMask::kComputeBoundsAndRenderImage_CreateMode, fillOrHairline)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400135 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400136 }
137 SkAutoMaskFreeImage autoSrc(srcM.fImage);
138
139 SkASSERT(SkMask::kA8_Format == srcM.fFormat);
140
141 if (!as_MFB(filter)->filterMask(&dstM, srcM, viewMatrix, nullptr)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400142 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400143 }
144 // this will free-up dstM when we're done (allocated in filterMask())
145 SkAutoMaskFreeImage autoDst(dstM.fImage);
146
147 if (clip_bounds_quick_reject(clipBounds, dstM.fBounds)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400148 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400149 }
150
151 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
152 // the current clip (and identity matrix) and GrPaint settings
153 SkBitmap bm;
154 if (!bm.installPixels(SkImageInfo::MakeA8(dstM.fBounds.width(), dstM.fBounds.height()),
155 autoDst.release(), dstM.fRowBytes, mask_release_proc, nullptr)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400156 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400157 }
158 bm.setImmutable();
159
Brian Salomon27c42022021-04-28 12:39:21 -0400160 std::tie(filteredMaskView, std::ignore) = GrMakeUncachedBitmapProxyView(
161 rContext,
162 bm,
163 GrMipmapped::kNo,
164 SkBackingFit::kApprox);
165 if (!filteredMaskView) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400166 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400167 }
168
Brian Salomond005b692020-04-01 15:47:05 -0400169 SkASSERT(kMaskOrigin == filteredMaskView.origin());
Robert Phillips59b39e72018-08-30 11:51:25 -0400170
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400171 *drawRect = dstM.fBounds;
Robert Phillips31c080b2018-08-23 10:45:32 -0400172
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400173 if (key->isValid()) {
174 key->setCustomData(create_data(*drawRect, unclippedDevShapeBounds));
Robert Phillipsd464feb2020-10-08 11:00:02 -0400175 std::tie(filteredMaskView, data) = threadSafeCache->addWithData(*key, filteredMaskView);
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400176 // If we got a different view back from 'addWithData' it could have a different drawRect
177 *drawRect = extract_draw_rect_from_data(data.get(), unclippedDevShapeBounds);
Robert Phillips31c080b2018-08-23 10:45:32 -0400178 }
Robert Phillips48ce22b2018-03-23 10:33:12 -0400179 }
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500180
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400181 return filteredMaskView;
robertphillipsccb1b572015-05-27 11:02:55 -0700182}
183
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500184// Create a mask of 'shape' and return the resulting surfaceDrawContext
Brian Salomoneebe7352020-12-09 16:37:04 -0500185static std::unique_ptr<GrSurfaceDrawContext> create_mask_GPU(GrRecordingContext* context,
186 const SkIRect& maskRect,
187 const SkMatrix& origViewMatrix,
188 const GrStyledShape& shape,
189 int sampleCnt) {
Chris Dalton92934d72021-04-23 09:08:53 -0600190 // We cache blur masks. Use default surface props here so we can use the same cached mask
191 // regardless of the final dst surface.
192 SkSurfaceProps defaultSurfaceProps;
Chris Daltonf5b87f92021-04-19 17:27:09 -0600193
Chris Dalton0493fbd2019-09-18 15:49:46 -0600194 // Use GrResourceProvider::MakeApprox to implement our own approximate size matching, but demand
195 // a "SkBackingFit::kExact" size match on the actual render target. We do this because the
196 // filter will reach outside the src bounds, so we need to pre-clear these values to ensure a
197 // "decal" sampling effect (i.e., ensure reads outside the src bounds return alpha=0).
198 //
199 // FIXME: Reads outside the left and top edges will actually clamp to the edge pixel. And in the
200 // event that MakeApprox does not change the size, reads outside the right and/or bottom will do
201 // the same. We should offset our filter within the render target and expand the size as needed
202 // to guarantee at least 1px of padding on all sides.
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400203 auto approxSize = GrResourceProvider::MakeApprox(maskRect.size());
Chris Daltonf5b87f92021-04-19 17:27:09 -0600204 auto rtContext = GrSurfaceDrawContext::MakeWithFallback(context, GrColorType::kAlpha_8, nullptr,
205 SkBackingFit::kExact, approxSize,
Chris Dalton92934d72021-04-23 09:08:53 -0600206 defaultSurfaceProps, sampleCnt,
Chris Daltonf5b87f92021-04-19 17:27:09 -0600207 GrMipmapped::kNo, GrProtected::kNo,
208 kMaskOrigin);
robertphillipsd728f0c2016-11-21 11:05:03 -0800209 if (!rtContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700210 return nullptr;
robertphillipsccb1b572015-05-27 11:02:55 -0700211 }
212
Chris Dalton0493fbd2019-09-18 15:49:46 -0600213 rtContext->clear(SK_PMColor4fTRANSPARENT);
robertphillipsccb1b572015-05-27 11:02:55 -0700214
Brian Salomon82f44312017-01-11 13:42:54 -0500215 GrPaint maskPaint;
216 maskPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op);
robertphillipsccb1b572015-05-27 11:02:55 -0700217
218 // setup new clip
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400219 GrFixedClip clip(rtContext->dimensions(), SkIRect::MakeWH(maskRect.width(), maskRect.height()));
robertphillipsccb1b572015-05-27 11:02:55 -0700220
Brian Salomond005b692020-04-01 15:47:05 -0400221 // Draw the mask into maskTexture with the path's integerized top-left at the origin using
222 // maskPaint.
Robert Phillips27927a52018-08-20 13:18:12 -0400223 SkMatrix viewMatrix = origViewMatrix;
224 viewMatrix.postTranslate(-SkIntToScalar(maskRect.fLeft), -SkIntToScalar(maskRect.fTop));
Chris Dalton21859012021-01-29 23:44:53 -0700225 rtContext->drawShape(&clip, std::move(maskPaint), GrAA::kYes, viewMatrix, GrStyledShape(shape));
Robert Phillips40b05c32019-09-20 12:40:55 -0400226 return rtContext;
robertphillipsccb1b572015-05-27 11:02:55 -0700227}
228
Michael Ludwig2686d692020-04-17 20:21:37 +0000229static bool get_unclipped_shape_dev_bounds(const GrStyledShape& shape, const SkMatrix& matrix,
Robert Phillips27927a52018-08-20 13:18:12 -0400230 SkIRect* devBounds) {
231 SkRect shapeBounds = shape.styledBounds();
232 if (shapeBounds.isEmpty()) {
233 return false;
234 }
235 SkRect shapeDevBounds;
236 matrix.mapRect(&shapeDevBounds, shapeBounds);
237 // Even though these are "unclipped" bounds we still clip to the int32_t range.
238 // This is the largest int32_t that is representable exactly as a float. The next 63 larger ints
239 // would round down to this value when cast to a float, but who really cares.
240 // INT32_MIN is exactly representable.
241 static constexpr int32_t kMaxInt = 2147483520;
242 if (!shapeDevBounds.intersect(SkRect::MakeLTRB(INT32_MIN, INT32_MIN, kMaxInt, kMaxInt))) {
243 return false;
244 }
245 // Make sure that the resulting SkIRect can have representable width and height
246 if (SkScalarRoundToInt(shapeDevBounds.width()) > kMaxInt ||
247 SkScalarRoundToInt(shapeDevBounds.height()) > kMaxInt) {
248 return false;
249 }
250 shapeDevBounds.roundOut(devBounds);
251 return true;
252}
bsalomonc55271f2015-11-09 11:55:57 -0800253
Robert Phillips27927a52018-08-20 13:18:12 -0400254// Gets the shape bounds, the clip bounds, and the intersection (if any). Returns false if there
255// is no intersection.
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500256static bool get_shape_and_clip_bounds(GrSurfaceDrawContext* surfaceDrawContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400257 const GrClip* clip,
Michael Ludwig2686d692020-04-17 20:21:37 +0000258 const GrStyledShape& shape,
Robert Phillips27927a52018-08-20 13:18:12 -0400259 const SkMatrix& matrix,
260 SkIRect* unclippedDevShapeBounds,
261 SkIRect* devClipBounds) {
262 // compute bounds as intersection of rt size, clip, and path
Michael Ludwige06a8972020-06-11 10:29:00 -0400263 *devClipBounds = clip ? clip->getConservativeBounds()
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500264 : SkIRect::MakeWH(surfaceDrawContext->width(),
265 surfaceDrawContext->height());
bsalomonc55271f2015-11-09 11:55:57 -0800266
Robert Phillips27927a52018-08-20 13:18:12 -0400267 if (!get_unclipped_shape_dev_bounds(shape, matrix, unclippedDevShapeBounds)) {
Brian Salomon44207f32020-01-06 15:20:18 -0500268 *unclippedDevShapeBounds = SkIRect::MakeEmpty();
Robert Phillips27927a52018-08-20 13:18:12 -0400269 return false;
bsalomonc55271f2015-11-09 11:55:57 -0800270 }
271
Robert Phillips27927a52018-08-20 13:18:12 -0400272 return true;
273}
274
Robert Phillips52c17c42020-10-05 11:55:59 -0400275// The key and clip-bounds are computed together because the caching decision can impact the
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400276// clip-bound - since we only cache un-clipped masks the clip can be removed entirely.
Robert Phillips52c17c42020-10-05 11:55:59 -0400277// A 'false' return value indicates that the shape is known to be clipped away.
278static bool compute_key_and_clip_bounds(GrUniqueKey* maskKey,
279 SkIRect* boundsForClip,
280 const GrCaps* caps,
281 const SkMatrix& viewMatrix,
282 bool inverseFilled,
283 const SkMaskFilterBase* maskFilter,
284 const GrStyledShape& shape,
285 const SkIRect& unclippedDevShapeBounds,
286 const SkIRect& devClipBounds) {
287 *boundsForClip = devClipBounds;
288
289#ifndef SK_DISABLE_MASKFILTERED_MASK_CACHING
290 // To prevent overloading the cache with entries during animations we limit the cache of masks
Robert Phillipsbaa74dc2021-05-04 17:04:32 -0400291 // to cases where the matrix preserves axis alignment. Additionally, caching blurred masks
292 // for non-nine-patchable blurred round rects can also quickly flood the cache.
293 bool useCache = !inverseFilled &&
294 viewMatrix.preservesAxisAlignment() &&
295 shape.hasUnstyledKey() &&
296 as_MFB(maskFilter)->asABlur(nullptr) &&
297 !shape.asRRect(nullptr, nullptr, nullptr, nullptr);
Robert Phillips52c17c42020-10-05 11:55:59 -0400298
299 if (useCache) {
300 SkIRect clippedMaskRect, unClippedMaskRect;
301 maskFilter->canFilterMaskGPU(shape, unclippedDevShapeBounds, devClipBounds,
302 viewMatrix, &clippedMaskRect);
303 maskFilter->canFilterMaskGPU(shape, unclippedDevShapeBounds, unclippedDevShapeBounds,
304 viewMatrix, &unClippedMaskRect);
305 if (clippedMaskRect.isEmpty()) {
306 return false;
307 }
308
309 // Use the cache only if >50% of the filtered mask is visible.
310 int unclippedWidth = unClippedMaskRect.width();
311 int unclippedHeight = unClippedMaskRect.height();
312 int64_t unclippedArea = sk_64_mul(unclippedWidth, unclippedHeight);
313 int64_t clippedArea = sk_64_mul(clippedMaskRect.width(), clippedMaskRect.height());
314 int maxTextureSize = caps->maxTextureSize();
315 if (unclippedArea > 2 * clippedArea || unclippedWidth > maxTextureSize ||
316 unclippedHeight > maxTextureSize) {
317 useCache = false;
318 } else {
319 // Make the clip not affect the mask
320 *boundsForClip = unclippedDevShapeBounds;
321 }
322 }
323
324 if (useCache) {
325 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
326 GrUniqueKey::Builder builder(maskKey, kDomain, 5 + 2 + shape.unstyledKeySize(),
327 "Mask Filtered Masks");
328
329 // We require the upper left 2x2 of the matrix to match exactly for a cache hit.
330 SkScalar sx = viewMatrix.get(SkMatrix::kMScaleX);
331 SkScalar sy = viewMatrix.get(SkMatrix::kMScaleY);
332 SkScalar kx = viewMatrix.get(SkMatrix::kMSkewX);
333 SkScalar ky = viewMatrix.get(SkMatrix::kMSkewY);
334 SkScalar tx = viewMatrix.get(SkMatrix::kMTransX);
335 SkScalar ty = viewMatrix.get(SkMatrix::kMTransY);
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400336 // Allow 8 bits each in x and y of subpixel positioning. But, note that we're allowing
337 // reuse for integer translations.
Robert Phillips52c17c42020-10-05 11:55:59 -0400338 SkFixed fracX = SkScalarToFixed(SkScalarFraction(tx)) & 0x0000FF00;
339 SkFixed fracY = SkScalarToFixed(SkScalarFraction(ty)) & 0x0000FF00;
340
341 builder[0] = SkFloat2Bits(sx);
342 builder[1] = SkFloat2Bits(sy);
343 builder[2] = SkFloat2Bits(kx);
344 builder[3] = SkFloat2Bits(ky);
345 // Distinguish between hairline and filled paths. For hairlines, we also need to include
346 // the cap. (SW grows hairlines by 0.5 pixel with round and square caps). Note that
347 // stroke-and-fill of hairlines is turned into pure fill by SkStrokeRec, so this covers
348 // all cases we might see.
349 uint32_t styleBits = shape.style().isSimpleHairline()
350 ? ((shape.style().strokeRec().getCap() << 1) | 1)
351 : 0;
352 builder[4] = fracX | (fracY >> 8) | (styleBits << 16);
353
354 SkMaskFilterBase::BlurRec rec;
355 SkAssertResult(as_MFB(maskFilter)->asABlur(&rec));
356
357 builder[5] = rec.fStyle; // TODO: we could put this with the other style bits
358 builder[6] = SkFloat2Bits(rec.fSigma);
359 shape.writeUnstyledKey(&builder[7]);
360 }
361#endif
362
363 return true;
364}
365
Robert Phillipsfde67e42020-10-07 15:33:43 -0400366static GrSurfaceProxyView hw_create_filtered_mask(GrDirectContext* dContext,
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500367 GrSurfaceDrawContext* surfaceDrawContext,
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400368 const SkMatrix& viewMatrix,
369 const GrStyledShape& shape,
370 const SkMaskFilterBase* filter,
371 const SkIRect& unclippedDevShapeBounds,
372 const SkIRect& clipBounds,
373 SkIRect* maskRect,
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400374 GrUniqueKey* key) {
Robert Phillipsfde67e42020-10-07 15:33:43 -0400375 if (!filter->canFilterMaskGPU(shape,
376 unclippedDevShapeBounds,
377 clipBounds,
378 viewMatrix,
379 maskRect)) {
380 return {};
381 }
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400382
Robert Phillipsfde67e42020-10-07 15:33:43 -0400383 if (clip_bounds_quick_reject(clipBounds, *maskRect)) {
384 // clipped out
385 return {};
386 }
387
Robert Phillipsd464feb2020-10-08 11:00:02 -0400388 auto threadSafeCache = dContext->priv().threadSafeCache();
Robert Phillipsfde67e42020-10-07 15:33:43 -0400389
390 GrSurfaceProxyView lazyView;
Robert Phillipsd464feb2020-10-08 11:00:02 -0400391 sk_sp<GrThreadSafeCache::Trampoline> trampoline;
Robert Phillipsfde67e42020-10-07 15:33:43 -0400392
393 if (key->isValid()) {
394 // In this case, we want GPU-filtered masks to have priority over SW-generated ones so
395 // we pre-emptively add a lazy-view to the cache and fill it in later.
Robert Phillipsd464feb2020-10-08 11:00:02 -0400396 std::tie(lazyView, trampoline) = GrThreadSafeCache::CreateLazyView(
Robert Phillipsfde67e42020-10-07 15:33:43 -0400397 dContext, GrColorType::kAlpha_8, maskRect->size(),
398 kMaskOrigin, SkBackingFit::kApprox);
399 if (!lazyView) {
400 return {}; // fall back to a SW-created mask - 'create_mask_GPU' probably won't succeed
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400401 }
402
Robert Phillipsfde67e42020-10-07 15:33:43 -0400403 key->setCustomData(create_data(*maskRect, unclippedDevShapeBounds));
Robert Phillipsd464feb2020-10-08 11:00:02 -0400404 auto [cachedView, data] = threadSafeCache->findOrAddWithData(*key, lazyView);
Robert Phillipsfde67e42020-10-07 15:33:43 -0400405 if (cachedView != lazyView) {
406 // In this case, the gpu-thread lost out to a recording thread - use its result.
407 SkASSERT(data);
408 SkASSERT(cachedView.asTextureProxy());
409 SkASSERT(cachedView.origin() == kMaskOrigin);
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400410
Robert Phillipsfde67e42020-10-07 15:33:43 -0400411 *maskRect = extract_draw_rect_from_data(data.get(), unclippedDevShapeBounds);
412 return cachedView;
413 }
414 }
415
Brian Salomoneebe7352020-12-09 16:37:04 -0500416 std::unique_ptr<GrSurfaceDrawContext> maskRTC(create_mask_GPU(
Robert Phillipsfde67e42020-10-07 15:33:43 -0400417 dContext,
418 *maskRect,
419 viewMatrix,
420 shape,
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500421 surfaceDrawContext->numSamples()));
Robert Phillipsfde67e42020-10-07 15:33:43 -0400422 if (!maskRTC) {
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400423 if (key->isValid()) {
Robert Phillipsfde67e42020-10-07 15:33:43 -0400424 // It is very unlikely that 'create_mask_GPU' will fail after 'CreateLazyView'
425 // succeeded but, if it does, remove the lazy-view from the cache and fallback to
426 // a SW-created mask. Note that any recording threads that glommed onto the
427 // lazy-view will have to, later, drop those draws.
Robert Phillipsd464feb2020-10-08 11:00:02 -0400428 threadSafeCache->remove(*key);
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400429 }
Robert Phillipsfde67e42020-10-07 15:33:43 -0400430 return {};
431 }
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400432
Robert Phillipsfde67e42020-10-07 15:33:43 -0400433 auto filteredMaskView = filter->filterMaskGPU(dContext,
434 maskRTC->readSurfaceView(),
435 maskRTC->colorInfo().colorType(),
436 maskRTC->colorInfo().alphaType(),
437 viewMatrix,
438 *maskRect);
439 if (!filteredMaskView) {
440 if (key->isValid()) {
441 // Remove the lazy-view from the cache and fallback to a SW-created mask. Note that
442 // any recording threads that glommed onto the lazy-view will have to, later, drop
443 // those draws.
Robert Phillipsd464feb2020-10-08 11:00:02 -0400444 threadSafeCache->remove(*key);
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400445 }
Robert Phillipsfde67e42020-10-07 15:33:43 -0400446 return {};
447 }
448
449 if (key->isValid()) {
450 SkASSERT(filteredMaskView.dimensions() == lazyView.dimensions());
451 SkASSERT(filteredMaskView.swizzle() == lazyView.swizzle());
452 SkASSERT(filteredMaskView.origin() == lazyView.origin());
453
454 trampoline->fProxy = filteredMaskView.asTextureProxyRef();
455 return lazyView;
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400456 }
457
458 return filteredMaskView;
459}
460
461static void draw_shape_with_mask_filter(GrRecordingContext* rContext,
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500462 GrSurfaceDrawContext* surfaceDrawContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400463 const GrClip* clip,
Robert Phillips27927a52018-08-20 13:18:12 -0400464 GrPaint&& paint,
Robert Phillips27927a52018-08-20 13:18:12 -0400465 const SkMatrix& viewMatrix,
466 const SkMaskFilterBase* maskFilter,
Michael Ludwig2686d692020-04-17 20:21:37 +0000467 const GrStyledShape& origShape) {
Robert Phillips27927a52018-08-20 13:18:12 -0400468 SkASSERT(maskFilter);
469
Michael Ludwig2686d692020-04-17 20:21:37 +0000470 const GrStyledShape* shape = &origShape;
471 SkTLazy<GrStyledShape> tmpShape;
Robert Phillips27927a52018-08-20 13:18:12 -0400472
473 if (origShape.style().applies()) {
474 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
Robert Phillips27358372021-02-10 12:28:38 -0500475 if (styleScale == 0) {
Robert Phillips27927a52018-08-20 13:18:12 -0400476 return;
bsalomon6663acf2016-05-10 09:14:17 -0700477 }
Robert Phillips27927a52018-08-20 13:18:12 -0400478
479 tmpShape.init(origShape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale));
John Stilesa008b0f2020-08-16 08:48:02 -0400480 if (tmpShape->isEmpty()) {
Robert Phillips27927a52018-08-20 13:18:12 -0400481 return;
482 }
483
484 shape = tmpShape.get();
bsalomon6663acf2016-05-10 09:14:17 -0700485 }
bsalomonc55271f2015-11-09 11:55:57 -0800486
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500487 if (maskFilter->directFilterMaskGPU(rContext, surfaceDrawContext, std::move(paint), clip,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400488 viewMatrix, *shape)) {
Robert Phillipsb889f202018-08-17 19:55:59 +0000489 // the mask filter was able to draw itself directly, so there's nothing
490 // left to do.
491 return;
492 }
Mike Klein16885072018-12-11 09:54:31 -0500493 assert_alive(paint);
Robert Phillipsb889f202018-08-17 19:55:59 +0000494
Robert Phillips27927a52018-08-20 13:18:12 -0400495 // If the path is hairline, ignore inverse fill.
496 bool inverseFilled = shape->inverseFilled() &&
497 !GrPathRenderer::IsStrokeHairlineOrEquivalent(shape->style(),
498 viewMatrix, nullptr);
499
500 SkIRect unclippedDevShapeBounds, devClipBounds;
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500501 if (!get_shape_and_clip_bounds(surfaceDrawContext, clip, *shape, viewMatrix,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400502 &unclippedDevShapeBounds, &devClipBounds)) {
Robert Phillips27927a52018-08-20 13:18:12 -0400503 // TODO: just cons up an opaque mask here
504 if (!inverseFilled) {
505 return;
506 }
507 }
508
Robert Phillips31c080b2018-08-23 10:45:32 -0400509 GrUniqueKey maskKey;
Robert Phillips52c17c42020-10-05 11:55:59 -0400510 SkIRect boundsForClip;
511 if (!compute_key_and_clip_bounds(&maskKey, &boundsForClip,
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500512 surfaceDrawContext->caps(),
Robert Phillips52c17c42020-10-05 11:55:59 -0400513 viewMatrix, inverseFilled,
514 maskFilter, *shape,
515 unclippedDevShapeBounds,
516 devClipBounds)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400517 return; // 'shape' was entirely clipped out
Robert Phillips31c080b2018-08-23 10:45:32 -0400518 }
519
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400520 GrSurfaceProxyView filteredMaskView;
Robert Phillips31c080b2018-08-23 10:45:32 -0400521 SkIRect maskRect;
bsalomonc55271f2015-11-09 11:55:57 -0800522
Robert Phillipsfde67e42020-10-07 15:33:43 -0400523 if (auto dContext = rContext->asDirectContext()) {
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500524 filteredMaskView = hw_create_filtered_mask(dContext, surfaceDrawContext,
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400525 viewMatrix, *shape, maskFilter,
526 unclippedDevShapeBounds, boundsForClip,
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400527 &maskRect, &maskKey);
Brian Salomond005b692020-04-01 15:47:05 -0400528 if (filteredMaskView) {
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500529 if (draw_mask(surfaceDrawContext, clip, viewMatrix, maskRect, std::move(paint),
Greg Daniel5c082492020-01-29 15:06:49 -0500530 std::move(filteredMaskView))) {
Robert Phillips20390c32018-08-17 11:01:03 -0400531 // This path is completely drawn
532 return;
533 }
Mike Klein16885072018-12-11 09:54:31 -0500534 assert_alive(paint);
Robert Phillips20390c32018-08-17 11:01:03 -0400535 }
bsalomonc55271f2015-11-09 11:55:57 -0800536 }
537
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400538 // Either HW mask rendering failed or we're in a DDL recording thread
539 filteredMaskView = sw_create_filtered_mask(rContext,
540 viewMatrix, *shape, maskFilter,
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400541 unclippedDevShapeBounds, boundsForClip,
542 &maskRect, &maskKey);
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400543 if (filteredMaskView) {
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500544 if (draw_mask(surfaceDrawContext, clip, viewMatrix, maskRect, std::move(paint),
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400545 std::move(filteredMaskView))) {
546 return;
547 }
548 assert_alive(paint);
549 }
bsalomon6663acf2016-05-10 09:14:17 -0700550}
551
Robert Phillips7af8fe52019-02-14 17:27:00 -0500552void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext* context,
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500553 GrSurfaceDrawContext* surfaceDrawContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400554 const GrClip* clip,
Michael Ludwig2686d692020-04-17 20:21:37 +0000555 const GrStyledShape& shape,
Robert Phillips27927a52018-08-20 13:18:12 -0400556 GrPaint&& paint,
Robert Phillips27927a52018-08-20 13:18:12 -0400557 const SkMatrix& viewMatrix,
558 const SkMaskFilter* mf) {
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500559 draw_shape_with_mask_filter(context, surfaceDrawContext, clip, std::move(paint),
Robert Phillips27927a52018-08-20 13:18:12 -0400560 viewMatrix, as_MFB(mf), shape);
bsalomonc55271f2015-11-09 11:55:57 -0800561}
562
Robert Phillips69893702019-02-22 11:16:30 -0500563void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext* context,
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500564 GrSurfaceDrawContext* surfaceDrawContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400565 const GrClip* clip,
Robert Phillips27927a52018-08-20 13:18:12 -0400566 const SkPaint& paint,
Brian Osman449b1152020-04-15 16:43:00 -0400567 const SkMatrixProvider& matrixProvider,
Michael Ludwig2686d692020-04-17 20:21:37 +0000568 const GrStyledShape& shape) {
Robert Phillips9eb00022020-06-30 15:30:12 -0400569 if (context->abandoned()) {
Robert Phillipsbebfd412018-03-08 11:07:23 -0500570 return;
571 }
572
robertphillipsccb1b572015-05-27 11:02:55 -0700573 GrPaint grPaint;
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500574 if (!SkPaintToGrPaint(context, surfaceDrawContext->colorInfo(), paint, matrixProvider,
Brian Osman449b1152020-04-15 16:43:00 -0400575 &grPaint)) {
robertphillipsccb1b572015-05-27 11:02:55 -0700576 return;
577 }
Robert Phillips27927a52018-08-20 13:18:12 -0400578
Brian Osman449b1152020-04-15 16:43:00 -0400579 const SkMatrix& viewMatrix(matrixProvider.localToDevice());
Mike Reed80747ef2018-01-23 15:29:32 -0500580 SkMaskFilterBase* mf = as_MFB(paint.getMaskFilter());
Mike Reedbfadcf02018-01-20 22:24:21 +0000581 if (mf && !mf->hasFragmentProcessor()) {
Robert Phillipsa29a9562016-10-20 09:40:55 -0400582 // The MaskFilter wasn't already handled in SkPaintToGrPaint
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500583 draw_shape_with_mask_filter(context, surfaceDrawContext, clip, std::move(grPaint),
Robert Phillips27927a52018-08-20 13:18:12 -0400584 viewMatrix, mf, shape);
bsalomonc55271f2015-11-09 11:55:57 -0800585 } else {
Chris Dalton94df5722021-04-19 17:40:40 -0600586 surfaceDrawContext->drawShape(clip, std::move(grPaint), surfaceDrawContext->chooseAA(paint),
Chris Daltonfd708652021-03-17 21:10:17 -0600587 viewMatrix, GrStyledShape(shape));
robertphillipsccb1b572015-05-27 11:02:55 -0700588 }
robertphillipsccb1b572015-05-27 11:02:55 -0700589}