blob: 0d43f904066ef76c030d02ace484596a89fbd9ef [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 Phillips11bc3052021-06-21 09:25:11 -040010#if GR_OGA
11
Robert Phillipsfde67e42020-10-07 15:33:43 -040012#include "include/gpu/GrDirectContext.h"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040013#include "include/gpu/GrRecordingContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrCaps.h"
Adlai Hollera0693042020-10-14 11:23:11 -040015#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrFixedClip.h"
17#include "src/gpu/GrProxyProvider.h"
18#include "src/gpu/GrRecordingContextPriv.h"
Robert Phillips1a82a4e2021-07-01 10:27:44 -040019#include "src/gpu/GrResourceProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrStyle.h"
Brian Salomoneebe7352020-12-09 16:37:04 -050021#include "src/gpu/GrSurfaceDrawContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040022#include "src/gpu/GrTextureProxy.h"
Robert Phillipsd464feb2020-10-08 11:00:02 -040023#include "src/gpu/GrThreadSafeCache.h"
Robert Phillips62214f72021-06-15 10:12:51 -040024#include "src/gpu/GrUtil.h"
Brian Salomon27c42022021-04-28 12:39:21 -040025#include "src/gpu/SkGr.h"
Brian Salomonb8f098d2020-01-07 11:15:44 -050026#include "src/gpu/effects/GrTextureEffect.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000027#include "src/gpu/geometry/GrStyledShape.h"
Robert Phillipsc7c2baf2018-03-08 09:51:04 -050028
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "include/core/SkPaint.h"
30#include "src/core/SkDraw.h"
31#include "src/core/SkMaskFilterBase.h"
Brian Osman449b1152020-04-15 16:43:00 -040032#include "src/core/SkMatrixProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/core/SkTLazy.h"
34#include "src/gpu/SkGr.h"
robertphillipsccb1b572015-05-27 11:02:55 -070035
36static bool clip_bounds_quick_reject(const SkIRect& clipBounds, const SkIRect& rect) {
37 return clipBounds.isEmpty() || rect.isEmpty() || !SkIRect::Intersects(clipBounds, rect);
38}
39
Brian Salomond005b692020-04-01 15:47:05 -040040static constexpr auto kMaskOrigin = kTopLeft_GrSurfaceOrigin;
41
robertphillipsccb1b572015-05-27 11:02:55 -070042// 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.
Brian Salomon1aa1f5f2020-12-11 17:25:17 -050045static bool draw_mask(GrSurfaceDrawContext* surfaceDrawContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -040046 const GrClip* clip,
robertphillipsccb1b572015-05-27 11:02:55 -070047 const SkMatrix& viewMatrix,
Chris Dalton35619552021-03-10 19:20:43 -070048 const SkIRect& maskBounds,
Brian Salomon82f44312017-01-11 13:42:54 -050049 GrPaint&& paint,
Greg Daniel5c082492020-01-29 15:06:49 -050050 GrSurfaceProxyView mask) {
Brian Salomon82f44312017-01-11 13:42:54 -050051 SkMatrix inverse;
52 if (!viewMatrix.invert(&inverse)) {
53 return false;
54 }
Robert Phillips4a24da52016-12-14 09:00:07 -050055
Brian Salomonb43d6992021-01-05 14:37:40 -050056 mask.concatSwizzle(GrSwizzle("aaaa"));
57
Chris Dalton35619552021-03-10 19:20:43 -070058 SkMatrix matrix = SkMatrix::Translate(-SkIntToScalar(maskBounds.fLeft),
59 -SkIntToScalar(maskBounds.fTop));
Brian Salomon2ebd0c82016-10-03 17:15:28 -040060 matrix.preConcat(viewMatrix);
John Stiles41d91b62020-07-21 14:39:40 -040061 paint.setCoverageFragmentProcessor(
Greg Danield2ccbb52020-02-05 10:45:39 -050062 GrTextureEffect::Make(std::move(mask), kUnknown_SkAlphaType, matrix));
robertphillipsccb1b572015-05-27 11:02:55 -070063
Chris Dalton35619552021-03-10 19:20:43 -070064 surfaceDrawContext->fillPixelsWithLocalMatrix(clip, std::move(paint), maskBounds, inverse);
robertphillipsccb1b572015-05-27 11:02:55 -070065 return true;
66}
67
Robert Phillipsc7c2baf2018-03-08 09:51:04 -050068static void mask_release_proc(void* addr, void* /*context*/) {
69 SkMask::FreeImage(addr);
70}
71
Robert Phillips6e17ffe2020-10-06 14:52:11 -040072// This stores the mapping from an unclipped, integerized, device-space, shape bounds to
73// the filtered mask's draw rect.
74struct DrawRectData {
75 SkIVector fOffset;
76 SkISize fSize;
77};
78
79static sk_sp<SkData> create_data(const SkIRect& drawRect, const SkIRect& origDevBounds) {
80
81 DrawRectData drawRectData { {drawRect.fLeft - origDevBounds.fLeft,
82 drawRect.fTop - origDevBounds.fTop},
83 drawRect.size() };
84
85 return SkData::MakeWithCopy(&drawRectData, sizeof(drawRectData));
86}
87
88static SkIRect extract_draw_rect_from_data(SkData* data, const SkIRect& origDevBounds) {
89 auto drawRectData = static_cast<const DrawRectData*>(data->data());
90
91 return SkIRect::MakeXYWH(origDevBounds.fLeft + drawRectData->fOffset.fX,
92 origDevBounds.fTop + drawRectData->fOffset.fY,
93 drawRectData->fSize.fWidth,
94 drawRectData->fSize.fHeight);
95}
96
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -040097static GrSurfaceProxyView sw_create_filtered_mask(GrRecordingContext* rContext,
98 const SkMatrix& viewMatrix,
99 const GrStyledShape& shape,
100 const SkMaskFilter* filter,
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400101 const SkIRect& unclippedDevShapeBounds,
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400102 const SkIRect& clipBounds,
103 SkIRect* drawRect,
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400104 GrUniqueKey* key) {
Robert Phillips27927a52018-08-20 13:18:12 -0400105 SkASSERT(filter);
106 SkASSERT(!shape.style().applies());
107
Robert Phillipsd464feb2020-10-08 11:00:02 -0400108 auto threadSafeCache = rContext->priv().threadSafeCache();
Robert Phillips27927a52018-08-20 13:18:12 -0400109
Greg Daniel5c082492020-01-29 15:06:49 -0500110 GrSurfaceProxyView filteredMaskView;
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400111 sk_sp<SkData> data;
Robert Phillips27927a52018-08-20 13:18:12 -0400112
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400113 if (key->isValid()) {
Robert Phillipsd464feb2020-10-08 11:00:02 -0400114 std::tie(filteredMaskView, data) = threadSafeCache->findWithData(*key);
robertphillipsccb1b572015-05-27 11:02:55 -0700115 }
116
Brian Salomond005b692020-04-01 15:47:05 -0400117 if (filteredMaskView) {
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400118 SkASSERT(data);
Robert Phillipsc73c1af2020-09-29 13:47:39 -0400119 SkASSERT(kMaskOrigin == filteredMaskView.origin());
120
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400121 *drawRect = extract_draw_rect_from_data(data.get(), unclippedDevShapeBounds);
Robert Phillips31c080b2018-08-23 10:45:32 -0400122 } else {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400123 SkStrokeRec::InitStyle fillOrHairline = shape.style().isSimpleHairline()
124 ? SkStrokeRec::kHairline_InitStyle
125 : SkStrokeRec::kFill_InitStyle;
126
Robert Phillips31c080b2018-08-23 10:45:32 -0400127 // TODO: it seems like we could create an SkDraw here and set its fMatrix field rather
128 // than explicitly transforming the path to device space.
129 SkPath devPath;
130
131 shape.asPath(&devPath);
132
133 devPath.transform(viewMatrix);
134
135 SkMask srcM, dstM;
136 if (!SkDraw::DrawToMask(devPath, &clipBounds, filter, &viewMatrix, &srcM,
137 SkMask::kComputeBoundsAndRenderImage_CreateMode, fillOrHairline)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400138 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400139 }
140 SkAutoMaskFreeImage autoSrc(srcM.fImage);
141
142 SkASSERT(SkMask::kA8_Format == srcM.fFormat);
143
144 if (!as_MFB(filter)->filterMask(&dstM, srcM, viewMatrix, nullptr)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400145 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400146 }
147 // this will free-up dstM when we're done (allocated in filterMask())
148 SkAutoMaskFreeImage autoDst(dstM.fImage);
149
150 if (clip_bounds_quick_reject(clipBounds, dstM.fBounds)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400151 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400152 }
153
154 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
155 // the current clip (and identity matrix) and GrPaint settings
156 SkBitmap bm;
157 if (!bm.installPixels(SkImageInfo::MakeA8(dstM.fBounds.width(), dstM.fBounds.height()),
158 autoDst.release(), dstM.fRowBytes, mask_release_proc, nullptr)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400159 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400160 }
161 bm.setImmutable();
162
Brian Salomon27c42022021-04-28 12:39:21 -0400163 std::tie(filteredMaskView, std::ignore) = GrMakeUncachedBitmapProxyView(
164 rContext,
165 bm,
166 GrMipmapped::kNo,
167 SkBackingFit::kApprox);
168 if (!filteredMaskView) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400169 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400170 }
171
Brian Salomond005b692020-04-01 15:47:05 -0400172 SkASSERT(kMaskOrigin == filteredMaskView.origin());
Robert Phillips59b39e72018-08-30 11:51:25 -0400173
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400174 *drawRect = dstM.fBounds;
Robert Phillips31c080b2018-08-23 10:45:32 -0400175
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400176 if (key->isValid()) {
177 key->setCustomData(create_data(*drawRect, unclippedDevShapeBounds));
Robert Phillipsd464feb2020-10-08 11:00:02 -0400178 std::tie(filteredMaskView, data) = threadSafeCache->addWithData(*key, filteredMaskView);
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400179 // If we got a different view back from 'addWithData' it could have a different drawRect
180 *drawRect = extract_draw_rect_from_data(data.get(), unclippedDevShapeBounds);
Robert Phillips31c080b2018-08-23 10:45:32 -0400181 }
Robert Phillips48ce22b2018-03-23 10:33:12 -0400182 }
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500183
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400184 return filteredMaskView;
robertphillipsccb1b572015-05-27 11:02:55 -0700185}
186
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500187// Create a mask of 'shape' and return the resulting surfaceDrawContext
Brian Salomoneebe7352020-12-09 16:37:04 -0500188static std::unique_ptr<GrSurfaceDrawContext> create_mask_GPU(GrRecordingContext* context,
189 const SkIRect& maskRect,
190 const SkMatrix& origViewMatrix,
191 const GrStyledShape& shape,
192 int sampleCnt) {
Chris Dalton92934d72021-04-23 09:08:53 -0600193 // We cache blur masks. Use default surface props here so we can use the same cached mask
194 // regardless of the final dst surface.
195 SkSurfaceProps defaultSurfaceProps;
Chris Daltonf5b87f92021-04-19 17:27:09 -0600196
Chris Dalton0493fbd2019-09-18 15:49:46 -0600197 // Use GrResourceProvider::MakeApprox to implement our own approximate size matching, but demand
198 // a "SkBackingFit::kExact" size match on the actual render target. We do this because the
199 // filter will reach outside the src bounds, so we need to pre-clear these values to ensure a
200 // "decal" sampling effect (i.e., ensure reads outside the src bounds return alpha=0).
201 //
202 // FIXME: Reads outside the left and top edges will actually clamp to the edge pixel. And in the
203 // event that MakeApprox does not change the size, reads outside the right and/or bottom will do
204 // the same. We should offset our filter within the render target and expand the size as needed
205 // to guarantee at least 1px of padding on all sides.
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400206 auto approxSize = GrResourceProvider::MakeApprox(maskRect.size());
Chris Daltonf5b87f92021-04-19 17:27:09 -0600207 auto rtContext = GrSurfaceDrawContext::MakeWithFallback(context, GrColorType::kAlpha_8, nullptr,
208 SkBackingFit::kExact, approxSize,
Chris Dalton92934d72021-04-23 09:08:53 -0600209 defaultSurfaceProps, sampleCnt,
Chris Daltonf5b87f92021-04-19 17:27:09 -0600210 GrMipmapped::kNo, GrProtected::kNo,
211 kMaskOrigin);
robertphillipsd728f0c2016-11-21 11:05:03 -0800212 if (!rtContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700213 return nullptr;
robertphillipsccb1b572015-05-27 11:02:55 -0700214 }
215
Chris Dalton0493fbd2019-09-18 15:49:46 -0600216 rtContext->clear(SK_PMColor4fTRANSPARENT);
robertphillipsccb1b572015-05-27 11:02:55 -0700217
Brian Salomon82f44312017-01-11 13:42:54 -0500218 GrPaint maskPaint;
219 maskPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op);
robertphillipsccb1b572015-05-27 11:02:55 -0700220
221 // setup new clip
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400222 GrFixedClip clip(rtContext->dimensions(), SkIRect::MakeWH(maskRect.width(), maskRect.height()));
robertphillipsccb1b572015-05-27 11:02:55 -0700223
Brian Salomond005b692020-04-01 15:47:05 -0400224 // Draw the mask into maskTexture with the path's integerized top-left at the origin using
225 // maskPaint.
Robert Phillips27927a52018-08-20 13:18:12 -0400226 SkMatrix viewMatrix = origViewMatrix;
227 viewMatrix.postTranslate(-SkIntToScalar(maskRect.fLeft), -SkIntToScalar(maskRect.fTop));
Chris Dalton21859012021-01-29 23:44:53 -0700228 rtContext->drawShape(&clip, std::move(maskPaint), GrAA::kYes, viewMatrix, GrStyledShape(shape));
Robert Phillips40b05c32019-09-20 12:40:55 -0400229 return rtContext;
robertphillipsccb1b572015-05-27 11:02:55 -0700230}
231
Michael Ludwig2686d692020-04-17 20:21:37 +0000232static bool get_unclipped_shape_dev_bounds(const GrStyledShape& shape, const SkMatrix& matrix,
Robert Phillips27927a52018-08-20 13:18:12 -0400233 SkIRect* devBounds) {
234 SkRect shapeBounds = shape.styledBounds();
235 if (shapeBounds.isEmpty()) {
236 return false;
237 }
238 SkRect shapeDevBounds;
239 matrix.mapRect(&shapeDevBounds, shapeBounds);
240 // Even though these are "unclipped" bounds we still clip to the int32_t range.
241 // This is the largest int32_t that is representable exactly as a float. The next 63 larger ints
242 // would round down to this value when cast to a float, but who really cares.
243 // INT32_MIN is exactly representable.
244 static constexpr int32_t kMaxInt = 2147483520;
245 if (!shapeDevBounds.intersect(SkRect::MakeLTRB(INT32_MIN, INT32_MIN, kMaxInt, kMaxInt))) {
246 return false;
247 }
248 // Make sure that the resulting SkIRect can have representable width and height
249 if (SkScalarRoundToInt(shapeDevBounds.width()) > kMaxInt ||
250 SkScalarRoundToInt(shapeDevBounds.height()) > kMaxInt) {
251 return false;
252 }
253 shapeDevBounds.roundOut(devBounds);
254 return true;
255}
bsalomonc55271f2015-11-09 11:55:57 -0800256
Robert Phillips27927a52018-08-20 13:18:12 -0400257// Gets the shape bounds, the clip bounds, and the intersection (if any). Returns false if there
258// is no intersection.
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500259static bool get_shape_and_clip_bounds(GrSurfaceDrawContext* surfaceDrawContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400260 const GrClip* clip,
Michael Ludwig2686d692020-04-17 20:21:37 +0000261 const GrStyledShape& shape,
Robert Phillips27927a52018-08-20 13:18:12 -0400262 const SkMatrix& matrix,
263 SkIRect* unclippedDevShapeBounds,
264 SkIRect* devClipBounds) {
265 // compute bounds as intersection of rt size, clip, and path
Michael Ludwige06a8972020-06-11 10:29:00 -0400266 *devClipBounds = clip ? clip->getConservativeBounds()
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500267 : SkIRect::MakeWH(surfaceDrawContext->width(),
268 surfaceDrawContext->height());
bsalomonc55271f2015-11-09 11:55:57 -0800269
Robert Phillips27927a52018-08-20 13:18:12 -0400270 if (!get_unclipped_shape_dev_bounds(shape, matrix, unclippedDevShapeBounds)) {
Brian Salomon44207f32020-01-06 15:20:18 -0500271 *unclippedDevShapeBounds = SkIRect::MakeEmpty();
Robert Phillips27927a52018-08-20 13:18:12 -0400272 return false;
bsalomonc55271f2015-11-09 11:55:57 -0800273 }
274
Robert Phillips27927a52018-08-20 13:18:12 -0400275 return true;
276}
277
Robert Phillips52c17c42020-10-05 11:55:59 -0400278// The key and clip-bounds are computed together because the caching decision can impact the
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400279// clip-bound - since we only cache un-clipped masks the clip can be removed entirely.
Robert Phillips52c17c42020-10-05 11:55:59 -0400280// A 'false' return value indicates that the shape is known to be clipped away.
281static bool compute_key_and_clip_bounds(GrUniqueKey* maskKey,
282 SkIRect* boundsForClip,
283 const GrCaps* caps,
284 const SkMatrix& viewMatrix,
285 bool inverseFilled,
286 const SkMaskFilterBase* maskFilter,
287 const GrStyledShape& shape,
288 const SkIRect& unclippedDevShapeBounds,
289 const SkIRect& devClipBounds) {
290 *boundsForClip = devClipBounds;
291
292#ifndef SK_DISABLE_MASKFILTERED_MASK_CACHING
293 // To prevent overloading the cache with entries during animations we limit the cache of masks
Robert Phillipsabf03532021-05-05 18:39:26 +0000294 // to cases where the matrix preserves axis alignment.
295 bool useCache = !inverseFilled && viewMatrix.preservesAxisAlignment() &&
296 shape.hasUnstyledKey() && as_MFB(maskFilter)->asABlur(nullptr);
Robert Phillips52c17c42020-10-05 11:55:59 -0400297
298 if (useCache) {
299 SkIRect clippedMaskRect, unClippedMaskRect;
300 maskFilter->canFilterMaskGPU(shape, unclippedDevShapeBounds, devClipBounds,
301 viewMatrix, &clippedMaskRect);
302 maskFilter->canFilterMaskGPU(shape, unclippedDevShapeBounds, unclippedDevShapeBounds,
303 viewMatrix, &unClippedMaskRect);
304 if (clippedMaskRect.isEmpty()) {
305 return false;
306 }
307
308 // Use the cache only if >50% of the filtered mask is visible.
309 int unclippedWidth = unClippedMaskRect.width();
310 int unclippedHeight = unClippedMaskRect.height();
311 int64_t unclippedArea = sk_64_mul(unclippedWidth, unclippedHeight);
312 int64_t clippedArea = sk_64_mul(clippedMaskRect.width(), clippedMaskRect.height());
313 int maxTextureSize = caps->maxTextureSize();
314 if (unclippedArea > 2 * clippedArea || unclippedWidth > maxTextureSize ||
315 unclippedHeight > maxTextureSize) {
316 useCache = false;
317 } else {
318 // Make the clip not affect the mask
319 *boundsForClip = unclippedDevShapeBounds;
320 }
321 }
322
323 if (useCache) {
324 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
325 GrUniqueKey::Builder builder(maskKey, kDomain, 5 + 2 + shape.unstyledKeySize(),
326 "Mask Filtered Masks");
327
328 // We require the upper left 2x2 of the matrix to match exactly for a cache hit.
329 SkScalar sx = viewMatrix.get(SkMatrix::kMScaleX);
330 SkScalar sy = viewMatrix.get(SkMatrix::kMScaleY);
331 SkScalar kx = viewMatrix.get(SkMatrix::kMSkewX);
332 SkScalar ky = viewMatrix.get(SkMatrix::kMSkewY);
333 SkScalar tx = viewMatrix.get(SkMatrix::kMTransX);
334 SkScalar ty = viewMatrix.get(SkMatrix::kMTransY);
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400335 // Allow 8 bits each in x and y of subpixel positioning. But, note that we're allowing
336 // reuse for integer translations.
Robert Phillips52c17c42020-10-05 11:55:59 -0400337 SkFixed fracX = SkScalarToFixed(SkScalarFraction(tx)) & 0x0000FF00;
338 SkFixed fracY = SkScalarToFixed(SkScalarFraction(ty)) & 0x0000FF00;
339
340 builder[0] = SkFloat2Bits(sx);
341 builder[1] = SkFloat2Bits(sy);
342 builder[2] = SkFloat2Bits(kx);
343 builder[3] = SkFloat2Bits(ky);
344 // Distinguish between hairline and filled paths. For hairlines, we also need to include
345 // the cap. (SW grows hairlines by 0.5 pixel with round and square caps). Note that
346 // stroke-and-fill of hairlines is turned into pure fill by SkStrokeRec, so this covers
347 // all cases we might see.
348 uint32_t styleBits = shape.style().isSimpleHairline()
349 ? ((shape.style().strokeRec().getCap() << 1) | 1)
350 : 0;
351 builder[4] = fracX | (fracY >> 8) | (styleBits << 16);
352
353 SkMaskFilterBase::BlurRec rec;
354 SkAssertResult(as_MFB(maskFilter)->asABlur(&rec));
355
356 builder[5] = rec.fStyle; // TODO: we could put this with the other style bits
357 builder[6] = SkFloat2Bits(rec.fSigma);
358 shape.writeUnstyledKey(&builder[7]);
359 }
360#endif
361
362 return true;
363}
364
Robert Phillipsfde67e42020-10-07 15:33:43 -0400365static GrSurfaceProxyView hw_create_filtered_mask(GrDirectContext* dContext,
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500366 GrSurfaceDrawContext* surfaceDrawContext,
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400367 const SkMatrix& viewMatrix,
368 const GrStyledShape& shape,
369 const SkMaskFilterBase* filter,
370 const SkIRect& unclippedDevShapeBounds,
371 const SkIRect& clipBounds,
372 SkIRect* maskRect,
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400373 GrUniqueKey* key) {
Robert Phillipsfde67e42020-10-07 15:33:43 -0400374 if (!filter->canFilterMaskGPU(shape,
375 unclippedDevShapeBounds,
376 clipBounds,
377 viewMatrix,
378 maskRect)) {
379 return {};
380 }
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400381
Robert Phillipsfde67e42020-10-07 15:33:43 -0400382 if (clip_bounds_quick_reject(clipBounds, *maskRect)) {
383 // clipped out
384 return {};
385 }
386
Robert Phillipsd464feb2020-10-08 11:00:02 -0400387 auto threadSafeCache = dContext->priv().threadSafeCache();
Robert Phillipsfde67e42020-10-07 15:33:43 -0400388
389 GrSurfaceProxyView lazyView;
Robert Phillipsd464feb2020-10-08 11:00:02 -0400390 sk_sp<GrThreadSafeCache::Trampoline> trampoline;
Robert Phillipsfde67e42020-10-07 15:33:43 -0400391
392 if (key->isValid()) {
393 // In this case, we want GPU-filtered masks to have priority over SW-generated ones so
394 // we pre-emptively add a lazy-view to the cache and fill it in later.
Robert Phillipsd464feb2020-10-08 11:00:02 -0400395 std::tie(lazyView, trampoline) = GrThreadSafeCache::CreateLazyView(
Robert Phillipsfde67e42020-10-07 15:33:43 -0400396 dContext, GrColorType::kAlpha_8, maskRect->size(),
397 kMaskOrigin, SkBackingFit::kApprox);
398 if (!lazyView) {
399 return {}; // fall back to a SW-created mask - 'create_mask_GPU' probably won't succeed
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400400 }
401
Robert Phillipsfde67e42020-10-07 15:33:43 -0400402 key->setCustomData(create_data(*maskRect, unclippedDevShapeBounds));
Robert Phillipsd464feb2020-10-08 11:00:02 -0400403 auto [cachedView, data] = threadSafeCache->findOrAddWithData(*key, lazyView);
Robert Phillipsfde67e42020-10-07 15:33:43 -0400404 if (cachedView != lazyView) {
405 // In this case, the gpu-thread lost out to a recording thread - use its result.
406 SkASSERT(data);
407 SkASSERT(cachedView.asTextureProxy());
408 SkASSERT(cachedView.origin() == kMaskOrigin);
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400409
Robert Phillipsfde67e42020-10-07 15:33:43 -0400410 *maskRect = extract_draw_rect_from_data(data.get(), unclippedDevShapeBounds);
411 return cachedView;
412 }
413 }
414
Brian Salomoneebe7352020-12-09 16:37:04 -0500415 std::unique_ptr<GrSurfaceDrawContext> maskRTC(create_mask_GPU(
Robert Phillipsfde67e42020-10-07 15:33:43 -0400416 dContext,
417 *maskRect,
418 viewMatrix,
419 shape,
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500420 surfaceDrawContext->numSamples()));
Robert Phillipsfde67e42020-10-07 15:33:43 -0400421 if (!maskRTC) {
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400422 if (key->isValid()) {
Robert Phillipsfde67e42020-10-07 15:33:43 -0400423 // It is very unlikely that 'create_mask_GPU' will fail after 'CreateLazyView'
424 // succeeded but, if it does, remove the lazy-view from the cache and fallback to
425 // a SW-created mask. Note that any recording threads that glommed onto the
426 // lazy-view will have to, later, drop those draws.
Robert Phillipsd464feb2020-10-08 11:00:02 -0400427 threadSafeCache->remove(*key);
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400428 }
Robert Phillipsfde67e42020-10-07 15:33:43 -0400429 return {};
430 }
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400431
Robert Phillipsfde67e42020-10-07 15:33:43 -0400432 auto filteredMaskView = filter->filterMaskGPU(dContext,
433 maskRTC->readSurfaceView(),
434 maskRTC->colorInfo().colorType(),
435 maskRTC->colorInfo().alphaType(),
436 viewMatrix,
437 *maskRect);
438 if (!filteredMaskView) {
439 if (key->isValid()) {
440 // Remove the lazy-view from the cache and fallback to a SW-created mask. Note that
441 // any recording threads that glommed onto the lazy-view will have to, later, drop
442 // those draws.
Robert Phillipsd464feb2020-10-08 11:00:02 -0400443 threadSafeCache->remove(*key);
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400444 }
Robert Phillipsfde67e42020-10-07 15:33:43 -0400445 return {};
446 }
447
448 if (key->isValid()) {
449 SkASSERT(filteredMaskView.dimensions() == lazyView.dimensions());
450 SkASSERT(filteredMaskView.swizzle() == lazyView.swizzle());
451 SkASSERT(filteredMaskView.origin() == lazyView.origin());
452
453 trampoline->fProxy = filteredMaskView.asTextureProxyRef();
454 return lazyView;
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400455 }
456
457 return filteredMaskView;
458}
459
460static void draw_shape_with_mask_filter(GrRecordingContext* rContext,
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500461 GrSurfaceDrawContext* surfaceDrawContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400462 const GrClip* clip,
Robert Phillips27927a52018-08-20 13:18:12 -0400463 GrPaint&& paint,
Robert Phillips27927a52018-08-20 13:18:12 -0400464 const SkMatrix& viewMatrix,
465 const SkMaskFilterBase* maskFilter,
Michael Ludwig2686d692020-04-17 20:21:37 +0000466 const GrStyledShape& origShape) {
Robert Phillips27927a52018-08-20 13:18:12 -0400467 SkASSERT(maskFilter);
468
Michael Ludwig2686d692020-04-17 20:21:37 +0000469 const GrStyledShape* shape = &origShape;
470 SkTLazy<GrStyledShape> tmpShape;
Robert Phillips27927a52018-08-20 13:18:12 -0400471
472 if (origShape.style().applies()) {
473 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
Robert Phillips27358372021-02-10 12:28:38 -0500474 if (styleScale == 0) {
Robert Phillips27927a52018-08-20 13:18:12 -0400475 return;
bsalomon6663acf2016-05-10 09:14:17 -0700476 }
Robert Phillips27927a52018-08-20 13:18:12 -0400477
478 tmpShape.init(origShape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale));
John Stilesa008b0f2020-08-16 08:48:02 -0400479 if (tmpShape->isEmpty()) {
Robert Phillips27927a52018-08-20 13:18:12 -0400480 return;
481 }
482
483 shape = tmpShape.get();
bsalomon6663acf2016-05-10 09:14:17 -0700484 }
bsalomonc55271f2015-11-09 11:55:57 -0800485
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500486 if (maskFilter->directFilterMaskGPU(rContext, surfaceDrawContext, std::move(paint), clip,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400487 viewMatrix, *shape)) {
Robert Phillipsb889f202018-08-17 19:55:59 +0000488 // the mask filter was able to draw itself directly, so there's nothing
489 // left to do.
490 return;
491 }
Mike Klein16885072018-12-11 09:54:31 -0500492 assert_alive(paint);
Robert Phillipsb889f202018-08-17 19:55:59 +0000493
Robert Phillips27927a52018-08-20 13:18:12 -0400494 // If the path is hairline, ignore inverse fill.
495 bool inverseFilled = shape->inverseFilled() &&
Robert Phillips62214f72021-06-15 10:12:51 -0400496 !GrIsStrokeHairlineOrEquivalent(shape->style(), viewMatrix, nullptr);
Robert Phillips27927a52018-08-20 13:18:12 -0400497
498 SkIRect unclippedDevShapeBounds, devClipBounds;
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500499 if (!get_shape_and_clip_bounds(surfaceDrawContext, clip, *shape, viewMatrix,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400500 &unclippedDevShapeBounds, &devClipBounds)) {
Robert Phillips27927a52018-08-20 13:18:12 -0400501 // TODO: just cons up an opaque mask here
502 if (!inverseFilled) {
503 return;
504 }
505 }
506
Robert Phillips31c080b2018-08-23 10:45:32 -0400507 GrUniqueKey maskKey;
Robert Phillips52c17c42020-10-05 11:55:59 -0400508 SkIRect boundsForClip;
509 if (!compute_key_and_clip_bounds(&maskKey, &boundsForClip,
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500510 surfaceDrawContext->caps(),
Robert Phillips52c17c42020-10-05 11:55:59 -0400511 viewMatrix, inverseFilled,
512 maskFilter, *shape,
513 unclippedDevShapeBounds,
514 devClipBounds)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400515 return; // 'shape' was entirely clipped out
Robert Phillips31c080b2018-08-23 10:45:32 -0400516 }
517
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400518 GrSurfaceProxyView filteredMaskView;
Robert Phillips31c080b2018-08-23 10:45:32 -0400519 SkIRect maskRect;
bsalomonc55271f2015-11-09 11:55:57 -0800520
Robert Phillipsfde67e42020-10-07 15:33:43 -0400521 if (auto dContext = rContext->asDirectContext()) {
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500522 filteredMaskView = hw_create_filtered_mask(dContext, surfaceDrawContext,
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400523 viewMatrix, *shape, maskFilter,
524 unclippedDevShapeBounds, boundsForClip,
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400525 &maskRect, &maskKey);
Brian Salomond005b692020-04-01 15:47:05 -0400526 if (filteredMaskView) {
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500527 if (draw_mask(surfaceDrawContext, clip, viewMatrix, maskRect, std::move(paint),
Greg Daniel5c082492020-01-29 15:06:49 -0500528 std::move(filteredMaskView))) {
Robert Phillips20390c32018-08-17 11:01:03 -0400529 // This path is completely drawn
530 return;
531 }
Mike Klein16885072018-12-11 09:54:31 -0500532 assert_alive(paint);
Robert Phillips20390c32018-08-17 11:01:03 -0400533 }
bsalomonc55271f2015-11-09 11:55:57 -0800534 }
535
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400536 // Either HW mask rendering failed or we're in a DDL recording thread
537 filteredMaskView = sw_create_filtered_mask(rContext,
538 viewMatrix, *shape, maskFilter,
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400539 unclippedDevShapeBounds, boundsForClip,
540 &maskRect, &maskKey);
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400541 if (filteredMaskView) {
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500542 if (draw_mask(surfaceDrawContext, clip, viewMatrix, maskRect, std::move(paint),
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400543 std::move(filteredMaskView))) {
544 return;
545 }
546 assert_alive(paint);
547 }
bsalomon6663acf2016-05-10 09:14:17 -0700548}
549
Robert Phillips7af8fe52019-02-14 17:27:00 -0500550void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext* context,
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500551 GrSurfaceDrawContext* surfaceDrawContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400552 const GrClip* clip,
Michael Ludwig2686d692020-04-17 20:21:37 +0000553 const GrStyledShape& shape,
Robert Phillips27927a52018-08-20 13:18:12 -0400554 GrPaint&& paint,
Robert Phillips27927a52018-08-20 13:18:12 -0400555 const SkMatrix& viewMatrix,
556 const SkMaskFilter* mf) {
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500557 draw_shape_with_mask_filter(context, surfaceDrawContext, clip, std::move(paint),
Robert Phillips27927a52018-08-20 13:18:12 -0400558 viewMatrix, as_MFB(mf), shape);
bsalomonc55271f2015-11-09 11:55:57 -0800559}
560
Robert Phillips69893702019-02-22 11:16:30 -0500561void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext* context,
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500562 GrSurfaceDrawContext* surfaceDrawContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400563 const GrClip* clip,
Robert Phillips27927a52018-08-20 13:18:12 -0400564 const SkPaint& paint,
Brian Osman449b1152020-04-15 16:43:00 -0400565 const SkMatrixProvider& matrixProvider,
Michael Ludwig2686d692020-04-17 20:21:37 +0000566 const GrStyledShape& shape) {
Robert Phillips9eb00022020-06-30 15:30:12 -0400567 if (context->abandoned()) {
Robert Phillipsbebfd412018-03-08 11:07:23 -0500568 return;
569 }
570
robertphillipsccb1b572015-05-27 11:02:55 -0700571 GrPaint grPaint;
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500572 if (!SkPaintToGrPaint(context, surfaceDrawContext->colorInfo(), paint, matrixProvider,
Brian Osman449b1152020-04-15 16:43:00 -0400573 &grPaint)) {
robertphillipsccb1b572015-05-27 11:02:55 -0700574 return;
575 }
Robert Phillips27927a52018-08-20 13:18:12 -0400576
Brian Osman449b1152020-04-15 16:43:00 -0400577 const SkMatrix& viewMatrix(matrixProvider.localToDevice());
Mike Reed80747ef2018-01-23 15:29:32 -0500578 SkMaskFilterBase* mf = as_MFB(paint.getMaskFilter());
Mike Reedbfadcf02018-01-20 22:24:21 +0000579 if (mf && !mf->hasFragmentProcessor()) {
Robert Phillipsa29a9562016-10-20 09:40:55 -0400580 // The MaskFilter wasn't already handled in SkPaintToGrPaint
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500581 draw_shape_with_mask_filter(context, surfaceDrawContext, clip, std::move(grPaint),
Robert Phillips27927a52018-08-20 13:18:12 -0400582 viewMatrix, mf, shape);
bsalomonc55271f2015-11-09 11:55:57 -0800583 } else {
Chris Dalton94df5722021-04-19 17:40:40 -0600584 surfaceDrawContext->drawShape(clip, std::move(grPaint), surfaceDrawContext->chooseAA(paint),
Chris Daltonfd708652021-03-17 21:10:17 -0600585 viewMatrix, GrStyledShape(shape));
robertphillipsccb1b572015-05-27 11:02:55 -0700586 }
robertphillipsccb1b572015-05-27 11:02:55 -0700587}
Robert Phillips11bc3052021-06-21 09:25:11 -0400588
589#else // GR_OGA
590
591void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext* context,
592 GrSurfaceDrawContext* surfaceDrawContext,
593 const GrClip* clip,
594 const GrStyledShape& shape,
595 GrPaint&& paint,
596 const SkMatrix& viewMatrix,
597 const SkMaskFilter* mf) {
598}
599
600void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext* context,
601 GrSurfaceDrawContext* surfaceDrawContext,
602 const GrClip* clip,
603 const SkPaint& paint,
604 const SkMatrixProvider& matrixProvider,
605 const GrStyledShape& shape) {
606}
607
608#endif // GR_OGA