blob: 2f2e79f7e2507c51497cb1cdd38b175da5065fec [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 Phillips7cef6782021-07-01 13:21:37 -040010#if SK_GPU_V1
Robert Phillips11bc3052021-06-21 09:25:11 -040011
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"
Greg Danielf91aeb22019-06-18 09:58:02 -040021#include "src/gpu/GrTextureProxy.h"
Robert Phillipsd464feb2020-10-08 11:00:02 -040022#include "src/gpu/GrThreadSafeCache.h"
Robert Phillips62214f72021-06-15 10:12:51 -040023#include "src/gpu/GrUtil.h"
Brian Salomon27c42022021-04-28 12:39:21 -040024#include "src/gpu/SkGr.h"
Brian Salomonb8f098d2020-01-07 11:15:44 -050025#include "src/gpu/effects/GrTextureEffect.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000026#include "src/gpu/geometry/GrStyledShape.h"
Robert Phillips4dca8312021-07-28 15:13:20 -040027#include "src/gpu/v1/SurfaceDrawContext_v1.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.
Robert Phillips4dca8312021-07-28 15:13:20 -040045static bool draw_mask(skgpu::v1::SurfaceDrawContext* sdc,
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
Robert Phillips4dca8312021-07-28 15:13:20 -040064 sdc->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
Robert Phillips4dca8312021-07-28 15:13:20 -0400188static std::unique_ptr<skgpu::v1::SurfaceDrawContext> create_mask_GPU(
189 GrRecordingContext* rContext,
190 const SkIRect& maskRect,
191 const SkMatrix& origViewMatrix,
192 const GrStyledShape& shape,
193 int sampleCnt) {
Chris Dalton92934d72021-04-23 09:08:53 -0600194 // We cache blur masks. Use default surface props here so we can use the same cached mask
195 // regardless of the final dst surface.
196 SkSurfaceProps defaultSurfaceProps;
Chris Daltonf5b87f92021-04-19 17:27:09 -0600197
Chris Dalton0493fbd2019-09-18 15:49:46 -0600198 // Use GrResourceProvider::MakeApprox to implement our own approximate size matching, but demand
199 // a "SkBackingFit::kExact" size match on the actual render target. We do this because the
200 // filter will reach outside the src bounds, so we need to pre-clear these values to ensure a
201 // "decal" sampling effect (i.e., ensure reads outside the src bounds return alpha=0).
202 //
203 // FIXME: Reads outside the left and top edges will actually clamp to the edge pixel. And in the
204 // event that MakeApprox does not change the size, reads outside the right and/or bottom will do
205 // the same. We should offset our filter within the render target and expand the size as needed
206 // to guarantee at least 1px of padding on all sides.
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400207 auto approxSize = GrResourceProvider::MakeApprox(maskRect.size());
Robert Phillips4dca8312021-07-28 15:13:20 -0400208 auto sdc = skgpu::v1::SurfaceDrawContext::MakeWithFallback(rContext,
209 GrColorType::kAlpha_8,
210 nullptr,
211 SkBackingFit::kExact,
212 approxSize,
213 defaultSurfaceProps,
214 sampleCnt,
215 GrMipmapped::kNo,
216 GrProtected::kNo,
217 kMaskOrigin);
218 if (!sdc) {
halcanary96fcdcc2015-08-27 07:41:13 -0700219 return nullptr;
robertphillipsccb1b572015-05-27 11:02:55 -0700220 }
221
Robert Phillips4dca8312021-07-28 15:13:20 -0400222 sdc->clear(SK_PMColor4fTRANSPARENT);
robertphillipsccb1b572015-05-27 11:02:55 -0700223
Brian Salomon82f44312017-01-11 13:42:54 -0500224 GrPaint maskPaint;
225 maskPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op);
robertphillipsccb1b572015-05-27 11:02:55 -0700226
227 // setup new clip
Robert Phillips4dca8312021-07-28 15:13:20 -0400228 GrFixedClip clip(sdc->dimensions(), SkIRect::MakeWH(maskRect.width(), maskRect.height()));
robertphillipsccb1b572015-05-27 11:02:55 -0700229
Brian Salomond005b692020-04-01 15:47:05 -0400230 // Draw the mask into maskTexture with the path's integerized top-left at the origin using
231 // maskPaint.
Robert Phillips27927a52018-08-20 13:18:12 -0400232 SkMatrix viewMatrix = origViewMatrix;
233 viewMatrix.postTranslate(-SkIntToScalar(maskRect.fLeft), -SkIntToScalar(maskRect.fTop));
Robert Phillips4dca8312021-07-28 15:13:20 -0400234 sdc->drawShape(&clip, std::move(maskPaint), GrAA::kYes, viewMatrix, GrStyledShape(shape));
235 return sdc;
robertphillipsccb1b572015-05-27 11:02:55 -0700236}
237
Michael Ludwig2686d692020-04-17 20:21:37 +0000238static bool get_unclipped_shape_dev_bounds(const GrStyledShape& shape, const SkMatrix& matrix,
Robert Phillips27927a52018-08-20 13:18:12 -0400239 SkIRect* devBounds) {
240 SkRect shapeBounds = shape.styledBounds();
241 if (shapeBounds.isEmpty()) {
242 return false;
243 }
244 SkRect shapeDevBounds;
245 matrix.mapRect(&shapeDevBounds, shapeBounds);
246 // Even though these are "unclipped" bounds we still clip to the int32_t range.
247 // This is the largest int32_t that is representable exactly as a float. The next 63 larger ints
248 // would round down to this value when cast to a float, but who really cares.
249 // INT32_MIN is exactly representable.
250 static constexpr int32_t kMaxInt = 2147483520;
251 if (!shapeDevBounds.intersect(SkRect::MakeLTRB(INT32_MIN, INT32_MIN, kMaxInt, kMaxInt))) {
252 return false;
253 }
254 // Make sure that the resulting SkIRect can have representable width and height
255 if (SkScalarRoundToInt(shapeDevBounds.width()) > kMaxInt ||
256 SkScalarRoundToInt(shapeDevBounds.height()) > kMaxInt) {
257 return false;
258 }
259 shapeDevBounds.roundOut(devBounds);
260 return true;
261}
bsalomonc55271f2015-11-09 11:55:57 -0800262
Robert Phillips27927a52018-08-20 13:18:12 -0400263// Gets the shape bounds, the clip bounds, and the intersection (if any). Returns false if there
264// is no intersection.
Robert Phillips4dca8312021-07-28 15:13:20 -0400265static bool get_shape_and_clip_bounds(skgpu::v1::SurfaceDrawContext* sdc,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400266 const GrClip* clip,
Michael Ludwig2686d692020-04-17 20:21:37 +0000267 const GrStyledShape& shape,
Robert Phillips27927a52018-08-20 13:18:12 -0400268 const SkMatrix& matrix,
269 SkIRect* unclippedDevShapeBounds,
270 SkIRect* devClipBounds) {
271 // compute bounds as intersection of rt size, clip, and path
Michael Ludwige06a8972020-06-11 10:29:00 -0400272 *devClipBounds = clip ? clip->getConservativeBounds()
Robert Phillips4dca8312021-07-28 15:13:20 -0400273 : SkIRect::MakeWH(sdc->width(), sdc->height());
bsalomonc55271f2015-11-09 11:55:57 -0800274
Robert Phillips27927a52018-08-20 13:18:12 -0400275 if (!get_unclipped_shape_dev_bounds(shape, matrix, unclippedDevShapeBounds)) {
Brian Salomon44207f32020-01-06 15:20:18 -0500276 *unclippedDevShapeBounds = SkIRect::MakeEmpty();
Robert Phillips27927a52018-08-20 13:18:12 -0400277 return false;
bsalomonc55271f2015-11-09 11:55:57 -0800278 }
279
Robert Phillips27927a52018-08-20 13:18:12 -0400280 return true;
281}
282
Robert Phillips52c17c42020-10-05 11:55:59 -0400283// The key and clip-bounds are computed together because the caching decision can impact the
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400284// clip-bound - since we only cache un-clipped masks the clip can be removed entirely.
Robert Phillips52c17c42020-10-05 11:55:59 -0400285// A 'false' return value indicates that the shape is known to be clipped away.
286static bool compute_key_and_clip_bounds(GrUniqueKey* maskKey,
287 SkIRect* boundsForClip,
288 const GrCaps* caps,
289 const SkMatrix& viewMatrix,
290 bool inverseFilled,
291 const SkMaskFilterBase* maskFilter,
292 const GrStyledShape& shape,
293 const SkIRect& unclippedDevShapeBounds,
294 const SkIRect& devClipBounds) {
295 *boundsForClip = devClipBounds;
296
297#ifndef SK_DISABLE_MASKFILTERED_MASK_CACHING
298 // To prevent overloading the cache with entries during animations we limit the cache of masks
Robert Phillipsabf03532021-05-05 18:39:26 +0000299 // to cases where the matrix preserves axis alignment.
300 bool useCache = !inverseFilled && viewMatrix.preservesAxisAlignment() &&
301 shape.hasUnstyledKey() && as_MFB(maskFilter)->asABlur(nullptr);
Robert Phillips52c17c42020-10-05 11:55:59 -0400302
303 if (useCache) {
304 SkIRect clippedMaskRect, unClippedMaskRect;
305 maskFilter->canFilterMaskGPU(shape, unclippedDevShapeBounds, devClipBounds,
306 viewMatrix, &clippedMaskRect);
307 maskFilter->canFilterMaskGPU(shape, unclippedDevShapeBounds, unclippedDevShapeBounds,
308 viewMatrix, &unClippedMaskRect);
309 if (clippedMaskRect.isEmpty()) {
310 return false;
311 }
312
313 // Use the cache only if >50% of the filtered mask is visible.
314 int unclippedWidth = unClippedMaskRect.width();
315 int unclippedHeight = unClippedMaskRect.height();
316 int64_t unclippedArea = sk_64_mul(unclippedWidth, unclippedHeight);
317 int64_t clippedArea = sk_64_mul(clippedMaskRect.width(), clippedMaskRect.height());
318 int maxTextureSize = caps->maxTextureSize();
319 if (unclippedArea > 2 * clippedArea || unclippedWidth > maxTextureSize ||
320 unclippedHeight > maxTextureSize) {
321 useCache = false;
322 } else {
323 // Make the clip not affect the mask
324 *boundsForClip = unclippedDevShapeBounds;
325 }
326 }
327
328 if (useCache) {
329 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
330 GrUniqueKey::Builder builder(maskKey, kDomain, 5 + 2 + shape.unstyledKeySize(),
331 "Mask Filtered Masks");
332
333 // We require the upper left 2x2 of the matrix to match exactly for a cache hit.
334 SkScalar sx = viewMatrix.get(SkMatrix::kMScaleX);
335 SkScalar sy = viewMatrix.get(SkMatrix::kMScaleY);
336 SkScalar kx = viewMatrix.get(SkMatrix::kMSkewX);
337 SkScalar ky = viewMatrix.get(SkMatrix::kMSkewY);
338 SkScalar tx = viewMatrix.get(SkMatrix::kMTransX);
339 SkScalar ty = viewMatrix.get(SkMatrix::kMTransY);
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400340 // Allow 8 bits each in x and y of subpixel positioning. But, note that we're allowing
341 // reuse for integer translations.
Robert Phillips52c17c42020-10-05 11:55:59 -0400342 SkFixed fracX = SkScalarToFixed(SkScalarFraction(tx)) & 0x0000FF00;
343 SkFixed fracY = SkScalarToFixed(SkScalarFraction(ty)) & 0x0000FF00;
344
345 builder[0] = SkFloat2Bits(sx);
346 builder[1] = SkFloat2Bits(sy);
347 builder[2] = SkFloat2Bits(kx);
348 builder[3] = SkFloat2Bits(ky);
349 // Distinguish between hairline and filled paths. For hairlines, we also need to include
350 // the cap. (SW grows hairlines by 0.5 pixel with round and square caps). Note that
351 // stroke-and-fill of hairlines is turned into pure fill by SkStrokeRec, so this covers
352 // all cases we might see.
353 uint32_t styleBits = shape.style().isSimpleHairline()
354 ? ((shape.style().strokeRec().getCap() << 1) | 1)
355 : 0;
356 builder[4] = fracX | (fracY >> 8) | (styleBits << 16);
357
358 SkMaskFilterBase::BlurRec rec;
359 SkAssertResult(as_MFB(maskFilter)->asABlur(&rec));
360
361 builder[5] = rec.fStyle; // TODO: we could put this with the other style bits
362 builder[6] = SkFloat2Bits(rec.fSigma);
363 shape.writeUnstyledKey(&builder[7]);
364 }
365#endif
366
367 return true;
368}
369
Robert Phillipsfde67e42020-10-07 15:33:43 -0400370static GrSurfaceProxyView hw_create_filtered_mask(GrDirectContext* dContext,
Robert Phillips4dca8312021-07-28 15:13:20 -0400371 skgpu::v1::SurfaceDrawContext* sdc,
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400372 const SkMatrix& viewMatrix,
373 const GrStyledShape& shape,
374 const SkMaskFilterBase* filter,
375 const SkIRect& unclippedDevShapeBounds,
376 const SkIRect& clipBounds,
377 SkIRect* maskRect,
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400378 GrUniqueKey* key) {
Robert Phillipsfde67e42020-10-07 15:33:43 -0400379 if (!filter->canFilterMaskGPU(shape,
380 unclippedDevShapeBounds,
381 clipBounds,
382 viewMatrix,
383 maskRect)) {
384 return {};
385 }
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400386
Robert Phillipsfde67e42020-10-07 15:33:43 -0400387 if (clip_bounds_quick_reject(clipBounds, *maskRect)) {
388 // clipped out
389 return {};
390 }
391
Robert Phillipsd464feb2020-10-08 11:00:02 -0400392 auto threadSafeCache = dContext->priv().threadSafeCache();
Robert Phillipsfde67e42020-10-07 15:33:43 -0400393
394 GrSurfaceProxyView lazyView;
Robert Phillipsd464feb2020-10-08 11:00:02 -0400395 sk_sp<GrThreadSafeCache::Trampoline> trampoline;
Robert Phillipsfde67e42020-10-07 15:33:43 -0400396
397 if (key->isValid()) {
398 // In this case, we want GPU-filtered masks to have priority over SW-generated ones so
399 // we pre-emptively add a lazy-view to the cache and fill it in later.
Robert Phillipsd464feb2020-10-08 11:00:02 -0400400 std::tie(lazyView, trampoline) = GrThreadSafeCache::CreateLazyView(
Robert Phillipsfde67e42020-10-07 15:33:43 -0400401 dContext, GrColorType::kAlpha_8, maskRect->size(),
402 kMaskOrigin, SkBackingFit::kApprox);
403 if (!lazyView) {
404 return {}; // fall back to a SW-created mask - 'create_mask_GPU' probably won't succeed
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400405 }
406
Robert Phillipsfde67e42020-10-07 15:33:43 -0400407 key->setCustomData(create_data(*maskRect, unclippedDevShapeBounds));
Robert Phillipsd464feb2020-10-08 11:00:02 -0400408 auto [cachedView, data] = threadSafeCache->findOrAddWithData(*key, lazyView);
Robert Phillipsfde67e42020-10-07 15:33:43 -0400409 if (cachedView != lazyView) {
410 // In this case, the gpu-thread lost out to a recording thread - use its result.
411 SkASSERT(data);
412 SkASSERT(cachedView.asTextureProxy());
413 SkASSERT(cachedView.origin() == kMaskOrigin);
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400414
Robert Phillipsfde67e42020-10-07 15:33:43 -0400415 *maskRect = extract_draw_rect_from_data(data.get(), unclippedDevShapeBounds);
416 return cachedView;
417 }
418 }
419
Robert Phillips4dca8312021-07-28 15:13:20 -0400420 std::unique_ptr<skgpu::v1::SurfaceDrawContext> maskSDC(create_mask_GPU(dContext,
421 *maskRect,
422 viewMatrix,
423 shape,
424 sdc->numSamples()));
425 if (!maskSDC) {
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400426 if (key->isValid()) {
Robert Phillipsfde67e42020-10-07 15:33:43 -0400427 // It is very unlikely that 'create_mask_GPU' will fail after 'CreateLazyView'
428 // succeeded but, if it does, remove the lazy-view from the cache and fallback to
429 // a SW-created mask. Note that any recording threads that glommed onto the
430 // lazy-view will have to, later, drop those draws.
Robert Phillipsd464feb2020-10-08 11:00:02 -0400431 threadSafeCache->remove(*key);
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400432 }
Robert Phillipsfde67e42020-10-07 15:33:43 -0400433 return {};
434 }
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400435
Robert Phillipsfde67e42020-10-07 15:33:43 -0400436 auto filteredMaskView = filter->filterMaskGPU(dContext,
Robert Phillips4dca8312021-07-28 15:13:20 -0400437 maskSDC->readSurfaceView(),
438 maskSDC->colorInfo().colorType(),
439 maskSDC->colorInfo().alphaType(),
Robert Phillipsfde67e42020-10-07 15:33:43 -0400440 viewMatrix,
441 *maskRect);
442 if (!filteredMaskView) {
443 if (key->isValid()) {
444 // Remove the lazy-view from the cache and fallback to a SW-created mask. Note that
445 // any recording threads that glommed onto the lazy-view will have to, later, drop
446 // those draws.
Robert Phillipsd464feb2020-10-08 11:00:02 -0400447 threadSafeCache->remove(*key);
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400448 }
Robert Phillipsfde67e42020-10-07 15:33:43 -0400449 return {};
450 }
451
452 if (key->isValid()) {
453 SkASSERT(filteredMaskView.dimensions() == lazyView.dimensions());
454 SkASSERT(filteredMaskView.swizzle() == lazyView.swizzle());
455 SkASSERT(filteredMaskView.origin() == lazyView.origin());
456
457 trampoline->fProxy = filteredMaskView.asTextureProxyRef();
458 return lazyView;
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400459 }
460
461 return filteredMaskView;
462}
463
464static void draw_shape_with_mask_filter(GrRecordingContext* rContext,
Robert Phillips4dca8312021-07-28 15:13:20 -0400465 skgpu::v1::SurfaceDrawContext* sdc,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400466 const GrClip* clip,
Robert Phillips27927a52018-08-20 13:18:12 -0400467 GrPaint&& paint,
Robert Phillips27927a52018-08-20 13:18:12 -0400468 const SkMatrix& viewMatrix,
469 const SkMaskFilterBase* maskFilter,
Michael Ludwig2686d692020-04-17 20:21:37 +0000470 const GrStyledShape& origShape) {
Robert Phillips27927a52018-08-20 13:18:12 -0400471 SkASSERT(maskFilter);
472
Michael Ludwig2686d692020-04-17 20:21:37 +0000473 const GrStyledShape* shape = &origShape;
474 SkTLazy<GrStyledShape> tmpShape;
Robert Phillips27927a52018-08-20 13:18:12 -0400475
476 if (origShape.style().applies()) {
477 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
Robert Phillips27358372021-02-10 12:28:38 -0500478 if (styleScale == 0) {
Robert Phillips27927a52018-08-20 13:18:12 -0400479 return;
bsalomon6663acf2016-05-10 09:14:17 -0700480 }
Robert Phillips27927a52018-08-20 13:18:12 -0400481
482 tmpShape.init(origShape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale));
John Stilesa008b0f2020-08-16 08:48:02 -0400483 if (tmpShape->isEmpty()) {
Robert Phillips27927a52018-08-20 13:18:12 -0400484 return;
485 }
486
487 shape = tmpShape.get();
bsalomon6663acf2016-05-10 09:14:17 -0700488 }
bsalomonc55271f2015-11-09 11:55:57 -0800489
Robert Phillips4dca8312021-07-28 15:13:20 -0400490 if (maskFilter->directFilterMaskGPU(rContext, sdc, std::move(paint), clip,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400491 viewMatrix, *shape)) {
Robert Phillipsb889f202018-08-17 19:55:59 +0000492 // the mask filter was able to draw itself directly, so there's nothing
493 // left to do.
494 return;
495 }
Mike Klein16885072018-12-11 09:54:31 -0500496 assert_alive(paint);
Robert Phillipsb889f202018-08-17 19:55:59 +0000497
Robert Phillips27927a52018-08-20 13:18:12 -0400498 // If the path is hairline, ignore inverse fill.
499 bool inverseFilled = shape->inverseFilled() &&
Robert Phillips62214f72021-06-15 10:12:51 -0400500 !GrIsStrokeHairlineOrEquivalent(shape->style(), viewMatrix, nullptr);
Robert Phillips27927a52018-08-20 13:18:12 -0400501
502 SkIRect unclippedDevShapeBounds, devClipBounds;
Robert Phillips4dca8312021-07-28 15:13:20 -0400503 if (!get_shape_and_clip_bounds(sdc, clip, *shape, viewMatrix,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400504 &unclippedDevShapeBounds, &devClipBounds)) {
Robert Phillips27927a52018-08-20 13:18:12 -0400505 // TODO: just cons up an opaque mask here
506 if (!inverseFilled) {
507 return;
508 }
509 }
510
Robert Phillips31c080b2018-08-23 10:45:32 -0400511 GrUniqueKey maskKey;
Robert Phillips52c17c42020-10-05 11:55:59 -0400512 SkIRect boundsForClip;
513 if (!compute_key_and_clip_bounds(&maskKey, &boundsForClip,
Robert Phillips4dca8312021-07-28 15:13:20 -0400514 sdc->caps(),
Robert Phillips52c17c42020-10-05 11:55:59 -0400515 viewMatrix, inverseFilled,
516 maskFilter, *shape,
517 unclippedDevShapeBounds,
518 devClipBounds)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400519 return; // 'shape' was entirely clipped out
Robert Phillips31c080b2018-08-23 10:45:32 -0400520 }
521
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400522 GrSurfaceProxyView filteredMaskView;
Robert Phillips31c080b2018-08-23 10:45:32 -0400523 SkIRect maskRect;
bsalomonc55271f2015-11-09 11:55:57 -0800524
Robert Phillipsfde67e42020-10-07 15:33:43 -0400525 if (auto dContext = rContext->asDirectContext()) {
Robert Phillips4dca8312021-07-28 15:13:20 -0400526 filteredMaskView = hw_create_filtered_mask(dContext, sdc,
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400527 viewMatrix, *shape, maskFilter,
528 unclippedDevShapeBounds, boundsForClip,
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400529 &maskRect, &maskKey);
Brian Salomond005b692020-04-01 15:47:05 -0400530 if (filteredMaskView) {
Robert Phillips4dca8312021-07-28 15:13:20 -0400531 if (draw_mask(sdc, clip, viewMatrix, maskRect, std::move(paint),
Greg Daniel5c082492020-01-29 15:06:49 -0500532 std::move(filteredMaskView))) {
Robert Phillips20390c32018-08-17 11:01:03 -0400533 // This path is completely drawn
534 return;
535 }
Mike Klein16885072018-12-11 09:54:31 -0500536 assert_alive(paint);
Robert Phillips20390c32018-08-17 11:01:03 -0400537 }
bsalomonc55271f2015-11-09 11:55:57 -0800538 }
539
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400540 // Either HW mask rendering failed or we're in a DDL recording thread
541 filteredMaskView = sw_create_filtered_mask(rContext,
542 viewMatrix, *shape, maskFilter,
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400543 unclippedDevShapeBounds, boundsForClip,
544 &maskRect, &maskKey);
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400545 if (filteredMaskView) {
Robert Phillips4dca8312021-07-28 15:13:20 -0400546 if (draw_mask(sdc, clip, viewMatrix, maskRect, std::move(paint),
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400547 std::move(filteredMaskView))) {
548 return;
549 }
550 assert_alive(paint);
551 }
bsalomon6663acf2016-05-10 09:14:17 -0700552}
553
Robert Phillips4dca8312021-07-28 15:13:20 -0400554void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext* rContext,
555 skgpu::v1::SurfaceDrawContext* sdc,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400556 const GrClip* clip,
Michael Ludwig2686d692020-04-17 20:21:37 +0000557 const GrStyledShape& shape,
Robert Phillips27927a52018-08-20 13:18:12 -0400558 GrPaint&& paint,
Robert Phillips27927a52018-08-20 13:18:12 -0400559 const SkMatrix& viewMatrix,
560 const SkMaskFilter* mf) {
Robert Phillips4dca8312021-07-28 15:13:20 -0400561 draw_shape_with_mask_filter(rContext, sdc, clip, std::move(paint),
Robert Phillips27927a52018-08-20 13:18:12 -0400562 viewMatrix, as_MFB(mf), shape);
bsalomonc55271f2015-11-09 11:55:57 -0800563}
564
Robert Phillips4dca8312021-07-28 15:13:20 -0400565void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext* rContext,
566 skgpu::v1::SurfaceDrawContext* sdc,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400567 const GrClip* clip,
Robert Phillips27927a52018-08-20 13:18:12 -0400568 const SkPaint& paint,
Brian Osman449b1152020-04-15 16:43:00 -0400569 const SkMatrixProvider& matrixProvider,
Michael Ludwig2686d692020-04-17 20:21:37 +0000570 const GrStyledShape& shape) {
Robert Phillips4dca8312021-07-28 15:13:20 -0400571 if (rContext->abandoned()) {
Robert Phillipsbebfd412018-03-08 11:07:23 -0500572 return;
573 }
574
robertphillipsccb1b572015-05-27 11:02:55 -0700575 GrPaint grPaint;
Robert Phillips4dca8312021-07-28 15:13:20 -0400576 if (!SkPaintToGrPaint(rContext, sdc->colorInfo(), paint, matrixProvider, &grPaint)) {
robertphillipsccb1b572015-05-27 11:02:55 -0700577 return;
578 }
Robert Phillips27927a52018-08-20 13:18:12 -0400579
Brian Osman449b1152020-04-15 16:43:00 -0400580 const SkMatrix& viewMatrix(matrixProvider.localToDevice());
Mike Reed80747ef2018-01-23 15:29:32 -0500581 SkMaskFilterBase* mf = as_MFB(paint.getMaskFilter());
Mike Reedbfadcf02018-01-20 22:24:21 +0000582 if (mf && !mf->hasFragmentProcessor()) {
Robert Phillipsa29a9562016-10-20 09:40:55 -0400583 // The MaskFilter wasn't already handled in SkPaintToGrPaint
Robert Phillips4dca8312021-07-28 15:13:20 -0400584 draw_shape_with_mask_filter(rContext, sdc, clip, std::move(grPaint), viewMatrix, mf, shape);
bsalomonc55271f2015-11-09 11:55:57 -0800585 } else {
Robert Phillips4dca8312021-07-28 15:13:20 -0400586 sdc->drawShape(clip, std::move(grPaint), sdc->chooseAA(paint), viewMatrix,
587 GrStyledShape(shape));
robertphillipsccb1b572015-05-27 11:02:55 -0700588 }
robertphillipsccb1b572015-05-27 11:02:55 -0700589}
Robert Phillips11bc3052021-06-21 09:25:11 -0400590
Robert Phillips7cef6782021-07-01 13:21:37 -0400591#else // SK_GPU_V1
Robert Phillips11bc3052021-06-21 09:25:11 -0400592
Robert Phillips4dca8312021-07-28 15:13:20 -0400593void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext*,
594 skgpu::v1::SurfaceDrawContext*,
595 const GrClip*,
596 const GrStyledShape&,
597 GrPaint&&,
Robert Phillips11bc3052021-06-21 09:25:11 -0400598 const SkMatrix& viewMatrix,
Robert Phillips4dca8312021-07-28 15:13:20 -0400599 const SkMaskFilter*) {
Robert Phillips11bc3052021-06-21 09:25:11 -0400600}
601
Robert Phillips4dca8312021-07-28 15:13:20 -0400602void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext*,
603 skgpu::v1::SurfaceDrawContext*,
604 const GrClip*,
605 const SkPaint&,
606 const SkMatrixProvider&,
607 const GrStyledShape&) {
Robert Phillips11bc3052021-06-21 09:25:11 -0400608}
609
Robert Phillips7cef6782021-07-01 13:21:37 -0400610#endif // SK_GPU_V1