blob: 7cb5db9db4c9d61a12179323ab8d35e88db6ab4d [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"
Robert Phillips62214f72021-06-15 10:12:51 -040022#include "src/gpu/GrUtil.h"
Brian Salomon27c42022021-04-28 12:39:21 -040023#include "src/gpu/SkGr.h"
Brian Salomonb8f098d2020-01-07 11:15:44 -050024#include "src/gpu/effects/GrTextureEffect.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000025#include "src/gpu/geometry/GrStyledShape.h"
Robert Phillipsc7c2baf2018-03-08 09:51:04 -050026
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "include/core/SkPaint.h"
28#include "src/core/SkDraw.h"
29#include "src/core/SkMaskFilterBase.h"
Brian Osman449b1152020-04-15 16:43:00 -040030#include "src/core/SkMatrixProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/core/SkTLazy.h"
32#include "src/gpu/SkGr.h"
robertphillipsccb1b572015-05-27 11:02:55 -070033
34static bool clip_bounds_quick_reject(const SkIRect& clipBounds, const SkIRect& rect) {
35 return clipBounds.isEmpty() || rect.isEmpty() || !SkIRect::Intersects(clipBounds, rect);
36}
37
Brian Salomond005b692020-04-01 15:47:05 -040038static constexpr auto kMaskOrigin = kTopLeft_GrSurfaceOrigin;
39
robertphillipsccb1b572015-05-27 11:02:55 -070040// Draw a mask using the supplied paint. Since the coverage/geometry
41// is already burnt into the mask this boils down to a rect draw.
42// Return true if the mask was successfully drawn.
Brian Salomon1aa1f5f2020-12-11 17:25:17 -050043static bool draw_mask(GrSurfaceDrawContext* surfaceDrawContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -040044 const GrClip* clip,
robertphillipsccb1b572015-05-27 11:02:55 -070045 const SkMatrix& viewMatrix,
Chris Dalton35619552021-03-10 19:20:43 -070046 const SkIRect& maskBounds,
Brian Salomon82f44312017-01-11 13:42:54 -050047 GrPaint&& paint,
Greg Daniel5c082492020-01-29 15:06:49 -050048 GrSurfaceProxyView mask) {
Brian Salomon82f44312017-01-11 13:42:54 -050049 SkMatrix inverse;
50 if (!viewMatrix.invert(&inverse)) {
51 return false;
52 }
Robert Phillips4a24da52016-12-14 09:00:07 -050053
Brian Salomonb43d6992021-01-05 14:37:40 -050054 mask.concatSwizzle(GrSwizzle("aaaa"));
55
Chris Dalton35619552021-03-10 19:20:43 -070056 SkMatrix matrix = SkMatrix::Translate(-SkIntToScalar(maskBounds.fLeft),
57 -SkIntToScalar(maskBounds.fTop));
Brian Salomon2ebd0c82016-10-03 17:15:28 -040058 matrix.preConcat(viewMatrix);
John Stiles41d91b62020-07-21 14:39:40 -040059 paint.setCoverageFragmentProcessor(
Greg Danield2ccbb52020-02-05 10:45:39 -050060 GrTextureEffect::Make(std::move(mask), kUnknown_SkAlphaType, matrix));
robertphillipsccb1b572015-05-27 11:02:55 -070061
Chris Dalton35619552021-03-10 19:20:43 -070062 surfaceDrawContext->fillPixelsWithLocalMatrix(clip, std::move(paint), maskBounds, inverse);
robertphillipsccb1b572015-05-27 11:02:55 -070063 return true;
64}
65
Robert Phillipsc7c2baf2018-03-08 09:51:04 -050066static void mask_release_proc(void* addr, void* /*context*/) {
67 SkMask::FreeImage(addr);
68}
69
Robert Phillips6e17ffe2020-10-06 14:52:11 -040070// This stores the mapping from an unclipped, integerized, device-space, shape bounds to
71// the filtered mask's draw rect.
72struct DrawRectData {
73 SkIVector fOffset;
74 SkISize fSize;
75};
76
77static sk_sp<SkData> create_data(const SkIRect& drawRect, const SkIRect& origDevBounds) {
78
79 DrawRectData drawRectData { {drawRect.fLeft - origDevBounds.fLeft,
80 drawRect.fTop - origDevBounds.fTop},
81 drawRect.size() };
82
83 return SkData::MakeWithCopy(&drawRectData, sizeof(drawRectData));
84}
85
86static SkIRect extract_draw_rect_from_data(SkData* data, const SkIRect& origDevBounds) {
87 auto drawRectData = static_cast<const DrawRectData*>(data->data());
88
89 return SkIRect::MakeXYWH(origDevBounds.fLeft + drawRectData->fOffset.fX,
90 origDevBounds.fTop + drawRectData->fOffset.fY,
91 drawRectData->fSize.fWidth,
92 drawRectData->fSize.fHeight);
93}
94
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -040095static GrSurfaceProxyView sw_create_filtered_mask(GrRecordingContext* rContext,
96 const SkMatrix& viewMatrix,
97 const GrStyledShape& shape,
98 const SkMaskFilter* filter,
Robert Phillips6e17ffe2020-10-06 14:52:11 -040099 const SkIRect& unclippedDevShapeBounds,
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400100 const SkIRect& clipBounds,
101 SkIRect* drawRect,
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400102 GrUniqueKey* key) {
Robert Phillips27927a52018-08-20 13:18:12 -0400103 SkASSERT(filter);
104 SkASSERT(!shape.style().applies());
105
Robert Phillipsd464feb2020-10-08 11:00:02 -0400106 auto threadSafeCache = rContext->priv().threadSafeCache();
Robert Phillips27927a52018-08-20 13:18:12 -0400107
Greg Daniel5c082492020-01-29 15:06:49 -0500108 GrSurfaceProxyView filteredMaskView;
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400109 sk_sp<SkData> data;
Robert Phillips27927a52018-08-20 13:18:12 -0400110
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400111 if (key->isValid()) {
Robert Phillipsd464feb2020-10-08 11:00:02 -0400112 std::tie(filteredMaskView, data) = threadSafeCache->findWithData(*key);
robertphillipsccb1b572015-05-27 11:02:55 -0700113 }
114
Brian Salomond005b692020-04-01 15:47:05 -0400115 if (filteredMaskView) {
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400116 SkASSERT(data);
Robert Phillipsc73c1af2020-09-29 13:47:39 -0400117 SkASSERT(kMaskOrigin == filteredMaskView.origin());
118
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400119 *drawRect = extract_draw_rect_from_data(data.get(), unclippedDevShapeBounds);
Robert Phillips31c080b2018-08-23 10:45:32 -0400120 } else {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400121 SkStrokeRec::InitStyle fillOrHairline = shape.style().isSimpleHairline()
122 ? SkStrokeRec::kHairline_InitStyle
123 : SkStrokeRec::kFill_InitStyle;
124
Robert Phillips31c080b2018-08-23 10:45:32 -0400125 // TODO: it seems like we could create an SkDraw here and set its fMatrix field rather
126 // than explicitly transforming the path to device space.
127 SkPath devPath;
128
129 shape.asPath(&devPath);
130
131 devPath.transform(viewMatrix);
132
133 SkMask srcM, dstM;
134 if (!SkDraw::DrawToMask(devPath, &clipBounds, filter, &viewMatrix, &srcM,
135 SkMask::kComputeBoundsAndRenderImage_CreateMode, fillOrHairline)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400136 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400137 }
138 SkAutoMaskFreeImage autoSrc(srcM.fImage);
139
140 SkASSERT(SkMask::kA8_Format == srcM.fFormat);
141
142 if (!as_MFB(filter)->filterMask(&dstM, srcM, viewMatrix, nullptr)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400143 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400144 }
145 // this will free-up dstM when we're done (allocated in filterMask())
146 SkAutoMaskFreeImage autoDst(dstM.fImage);
147
148 if (clip_bounds_quick_reject(clipBounds, dstM.fBounds)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400149 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400150 }
151
152 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
153 // the current clip (and identity matrix) and GrPaint settings
154 SkBitmap bm;
155 if (!bm.installPixels(SkImageInfo::MakeA8(dstM.fBounds.width(), dstM.fBounds.height()),
156 autoDst.release(), dstM.fRowBytes, mask_release_proc, nullptr)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400157 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400158 }
159 bm.setImmutable();
160
Brian Salomon27c42022021-04-28 12:39:21 -0400161 std::tie(filteredMaskView, std::ignore) = GrMakeUncachedBitmapProxyView(
162 rContext,
163 bm,
164 GrMipmapped::kNo,
165 SkBackingFit::kApprox);
166 if (!filteredMaskView) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400167 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400168 }
169
Brian Salomond005b692020-04-01 15:47:05 -0400170 SkASSERT(kMaskOrigin == filteredMaskView.origin());
Robert Phillips59b39e72018-08-30 11:51:25 -0400171
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400172 *drawRect = dstM.fBounds;
Robert Phillips31c080b2018-08-23 10:45:32 -0400173
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400174 if (key->isValid()) {
175 key->setCustomData(create_data(*drawRect, unclippedDevShapeBounds));
Robert Phillipsd464feb2020-10-08 11:00:02 -0400176 std::tie(filteredMaskView, data) = threadSafeCache->addWithData(*key, filteredMaskView);
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400177 // If we got a different view back from 'addWithData' it could have a different drawRect
178 *drawRect = extract_draw_rect_from_data(data.get(), unclippedDevShapeBounds);
Robert Phillips31c080b2018-08-23 10:45:32 -0400179 }
Robert Phillips48ce22b2018-03-23 10:33:12 -0400180 }
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500181
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400182 return filteredMaskView;
robertphillipsccb1b572015-05-27 11:02:55 -0700183}
184
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500185// Create a mask of 'shape' and return the resulting surfaceDrawContext
Brian Salomoneebe7352020-12-09 16:37:04 -0500186static std::unique_ptr<GrSurfaceDrawContext> create_mask_GPU(GrRecordingContext* context,
187 const SkIRect& maskRect,
188 const SkMatrix& origViewMatrix,
189 const GrStyledShape& shape,
190 int sampleCnt) {
Chris Dalton92934d72021-04-23 09:08:53 -0600191 // We cache blur masks. Use default surface props here so we can use the same cached mask
192 // regardless of the final dst surface.
193 SkSurfaceProps defaultSurfaceProps;
Chris Daltonf5b87f92021-04-19 17:27:09 -0600194
Chris Dalton0493fbd2019-09-18 15:49:46 -0600195 // Use GrResourceProvider::MakeApprox to implement our own approximate size matching, but demand
196 // a "SkBackingFit::kExact" size match on the actual render target. We do this because the
197 // filter will reach outside the src bounds, so we need to pre-clear these values to ensure a
198 // "decal" sampling effect (i.e., ensure reads outside the src bounds return alpha=0).
199 //
200 // FIXME: Reads outside the left and top edges will actually clamp to the edge pixel. And in the
201 // event that MakeApprox does not change the size, reads outside the right and/or bottom will do
202 // the same. We should offset our filter within the render target and expand the size as needed
203 // to guarantee at least 1px of padding on all sides.
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400204 auto approxSize = GrResourceProvider::MakeApprox(maskRect.size());
Chris Daltonf5b87f92021-04-19 17:27:09 -0600205 auto rtContext = GrSurfaceDrawContext::MakeWithFallback(context, GrColorType::kAlpha_8, nullptr,
206 SkBackingFit::kExact, approxSize,
Chris Dalton92934d72021-04-23 09:08:53 -0600207 defaultSurfaceProps, sampleCnt,
Chris Daltonf5b87f92021-04-19 17:27:09 -0600208 GrMipmapped::kNo, GrProtected::kNo,
209 kMaskOrigin);
robertphillipsd728f0c2016-11-21 11:05:03 -0800210 if (!rtContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700211 return nullptr;
robertphillipsccb1b572015-05-27 11:02:55 -0700212 }
213
Chris Dalton0493fbd2019-09-18 15:49:46 -0600214 rtContext->clear(SK_PMColor4fTRANSPARENT);
robertphillipsccb1b572015-05-27 11:02:55 -0700215
Brian Salomon82f44312017-01-11 13:42:54 -0500216 GrPaint maskPaint;
217 maskPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op);
robertphillipsccb1b572015-05-27 11:02:55 -0700218
219 // setup new clip
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400220 GrFixedClip clip(rtContext->dimensions(), SkIRect::MakeWH(maskRect.width(), maskRect.height()));
robertphillipsccb1b572015-05-27 11:02:55 -0700221
Brian Salomond005b692020-04-01 15:47:05 -0400222 // Draw the mask into maskTexture with the path's integerized top-left at the origin using
223 // maskPaint.
Robert Phillips27927a52018-08-20 13:18:12 -0400224 SkMatrix viewMatrix = origViewMatrix;
225 viewMatrix.postTranslate(-SkIntToScalar(maskRect.fLeft), -SkIntToScalar(maskRect.fTop));
Chris Dalton21859012021-01-29 23:44:53 -0700226 rtContext->drawShape(&clip, std::move(maskPaint), GrAA::kYes, viewMatrix, GrStyledShape(shape));
Robert Phillips40b05c32019-09-20 12:40:55 -0400227 return rtContext;
robertphillipsccb1b572015-05-27 11:02:55 -0700228}
229
Michael Ludwig2686d692020-04-17 20:21:37 +0000230static bool get_unclipped_shape_dev_bounds(const GrStyledShape& shape, const SkMatrix& matrix,
Robert Phillips27927a52018-08-20 13:18:12 -0400231 SkIRect* devBounds) {
232 SkRect shapeBounds = shape.styledBounds();
233 if (shapeBounds.isEmpty()) {
234 return false;
235 }
236 SkRect shapeDevBounds;
237 matrix.mapRect(&shapeDevBounds, shapeBounds);
238 // Even though these are "unclipped" bounds we still clip to the int32_t range.
239 // This is the largest int32_t that is representable exactly as a float. The next 63 larger ints
240 // would round down to this value when cast to a float, but who really cares.
241 // INT32_MIN is exactly representable.
242 static constexpr int32_t kMaxInt = 2147483520;
243 if (!shapeDevBounds.intersect(SkRect::MakeLTRB(INT32_MIN, INT32_MIN, kMaxInt, kMaxInt))) {
244 return false;
245 }
246 // Make sure that the resulting SkIRect can have representable width and height
247 if (SkScalarRoundToInt(shapeDevBounds.width()) > kMaxInt ||
248 SkScalarRoundToInt(shapeDevBounds.height()) > kMaxInt) {
249 return false;
250 }
251 shapeDevBounds.roundOut(devBounds);
252 return true;
253}
bsalomonc55271f2015-11-09 11:55:57 -0800254
Robert Phillips27927a52018-08-20 13:18:12 -0400255// Gets the shape bounds, the clip bounds, and the intersection (if any). Returns false if there
256// is no intersection.
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500257static bool get_shape_and_clip_bounds(GrSurfaceDrawContext* surfaceDrawContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400258 const GrClip* clip,
Michael Ludwig2686d692020-04-17 20:21:37 +0000259 const GrStyledShape& shape,
Robert Phillips27927a52018-08-20 13:18:12 -0400260 const SkMatrix& matrix,
261 SkIRect* unclippedDevShapeBounds,
262 SkIRect* devClipBounds) {
263 // compute bounds as intersection of rt size, clip, and path
Michael Ludwige06a8972020-06-11 10:29:00 -0400264 *devClipBounds = clip ? clip->getConservativeBounds()
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500265 : SkIRect::MakeWH(surfaceDrawContext->width(),
266 surfaceDrawContext->height());
bsalomonc55271f2015-11-09 11:55:57 -0800267
Robert Phillips27927a52018-08-20 13:18:12 -0400268 if (!get_unclipped_shape_dev_bounds(shape, matrix, unclippedDevShapeBounds)) {
Brian Salomon44207f32020-01-06 15:20:18 -0500269 *unclippedDevShapeBounds = SkIRect::MakeEmpty();
Robert Phillips27927a52018-08-20 13:18:12 -0400270 return false;
bsalomonc55271f2015-11-09 11:55:57 -0800271 }
272
Robert Phillips27927a52018-08-20 13:18:12 -0400273 return true;
274}
275
Robert Phillips52c17c42020-10-05 11:55:59 -0400276// The key and clip-bounds are computed together because the caching decision can impact the
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400277// clip-bound - since we only cache un-clipped masks the clip can be removed entirely.
Robert Phillips52c17c42020-10-05 11:55:59 -0400278// A 'false' return value indicates that the shape is known to be clipped away.
279static bool compute_key_and_clip_bounds(GrUniqueKey* maskKey,
280 SkIRect* boundsForClip,
281 const GrCaps* caps,
282 const SkMatrix& viewMatrix,
283 bool inverseFilled,
284 const SkMaskFilterBase* maskFilter,
285 const GrStyledShape& shape,
286 const SkIRect& unclippedDevShapeBounds,
287 const SkIRect& devClipBounds) {
288 *boundsForClip = devClipBounds;
289
290#ifndef SK_DISABLE_MASKFILTERED_MASK_CACHING
291 // To prevent overloading the cache with entries during animations we limit the cache of masks
Robert Phillipsabf03532021-05-05 18:39:26 +0000292 // to cases where the matrix preserves axis alignment.
293 bool useCache = !inverseFilled && viewMatrix.preservesAxisAlignment() &&
294 shape.hasUnstyledKey() && as_MFB(maskFilter)->asABlur(nullptr);
Robert Phillips52c17c42020-10-05 11:55:59 -0400295
296 if (useCache) {
297 SkIRect clippedMaskRect, unClippedMaskRect;
298 maskFilter->canFilterMaskGPU(shape, unclippedDevShapeBounds, devClipBounds,
299 viewMatrix, &clippedMaskRect);
300 maskFilter->canFilterMaskGPU(shape, unclippedDevShapeBounds, unclippedDevShapeBounds,
301 viewMatrix, &unClippedMaskRect);
302 if (clippedMaskRect.isEmpty()) {
303 return false;
304 }
305
306 // Use the cache only if >50% of the filtered mask is visible.
307 int unclippedWidth = unClippedMaskRect.width();
308 int unclippedHeight = unClippedMaskRect.height();
309 int64_t unclippedArea = sk_64_mul(unclippedWidth, unclippedHeight);
310 int64_t clippedArea = sk_64_mul(clippedMaskRect.width(), clippedMaskRect.height());
311 int maxTextureSize = caps->maxTextureSize();
312 if (unclippedArea > 2 * clippedArea || unclippedWidth > maxTextureSize ||
313 unclippedHeight > maxTextureSize) {
314 useCache = false;
315 } else {
316 // Make the clip not affect the mask
317 *boundsForClip = unclippedDevShapeBounds;
318 }
319 }
320
321 if (useCache) {
322 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
323 GrUniqueKey::Builder builder(maskKey, kDomain, 5 + 2 + shape.unstyledKeySize(),
324 "Mask Filtered Masks");
325
326 // We require the upper left 2x2 of the matrix to match exactly for a cache hit.
327 SkScalar sx = viewMatrix.get(SkMatrix::kMScaleX);
328 SkScalar sy = viewMatrix.get(SkMatrix::kMScaleY);
329 SkScalar kx = viewMatrix.get(SkMatrix::kMSkewX);
330 SkScalar ky = viewMatrix.get(SkMatrix::kMSkewY);
331 SkScalar tx = viewMatrix.get(SkMatrix::kMTransX);
332 SkScalar ty = viewMatrix.get(SkMatrix::kMTransY);
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400333 // Allow 8 bits each in x and y of subpixel positioning. But, note that we're allowing
334 // reuse for integer translations.
Robert Phillips52c17c42020-10-05 11:55:59 -0400335 SkFixed fracX = SkScalarToFixed(SkScalarFraction(tx)) & 0x0000FF00;
336 SkFixed fracY = SkScalarToFixed(SkScalarFraction(ty)) & 0x0000FF00;
337
338 builder[0] = SkFloat2Bits(sx);
339 builder[1] = SkFloat2Bits(sy);
340 builder[2] = SkFloat2Bits(kx);
341 builder[3] = SkFloat2Bits(ky);
342 // Distinguish between hairline and filled paths. For hairlines, we also need to include
343 // the cap. (SW grows hairlines by 0.5 pixel with round and square caps). Note that
344 // stroke-and-fill of hairlines is turned into pure fill by SkStrokeRec, so this covers
345 // all cases we might see.
346 uint32_t styleBits = shape.style().isSimpleHairline()
347 ? ((shape.style().strokeRec().getCap() << 1) | 1)
348 : 0;
349 builder[4] = fracX | (fracY >> 8) | (styleBits << 16);
350
351 SkMaskFilterBase::BlurRec rec;
352 SkAssertResult(as_MFB(maskFilter)->asABlur(&rec));
353
354 builder[5] = rec.fStyle; // TODO: we could put this with the other style bits
355 builder[6] = SkFloat2Bits(rec.fSigma);
356 shape.writeUnstyledKey(&builder[7]);
357 }
358#endif
359
360 return true;
361}
362
Robert Phillipsfde67e42020-10-07 15:33:43 -0400363static GrSurfaceProxyView hw_create_filtered_mask(GrDirectContext* dContext,
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500364 GrSurfaceDrawContext* surfaceDrawContext,
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400365 const SkMatrix& viewMatrix,
366 const GrStyledShape& shape,
367 const SkMaskFilterBase* filter,
368 const SkIRect& unclippedDevShapeBounds,
369 const SkIRect& clipBounds,
370 SkIRect* maskRect,
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400371 GrUniqueKey* key) {
Robert Phillipsfde67e42020-10-07 15:33:43 -0400372 if (!filter->canFilterMaskGPU(shape,
373 unclippedDevShapeBounds,
374 clipBounds,
375 viewMatrix,
376 maskRect)) {
377 return {};
378 }
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400379
Robert Phillipsfde67e42020-10-07 15:33:43 -0400380 if (clip_bounds_quick_reject(clipBounds, *maskRect)) {
381 // clipped out
382 return {};
383 }
384
Robert Phillipsd464feb2020-10-08 11:00:02 -0400385 auto threadSafeCache = dContext->priv().threadSafeCache();
Robert Phillipsfde67e42020-10-07 15:33:43 -0400386
387 GrSurfaceProxyView lazyView;
Robert Phillipsd464feb2020-10-08 11:00:02 -0400388 sk_sp<GrThreadSafeCache::Trampoline> trampoline;
Robert Phillipsfde67e42020-10-07 15:33:43 -0400389
390 if (key->isValid()) {
391 // In this case, we want GPU-filtered masks to have priority over SW-generated ones so
392 // we pre-emptively add a lazy-view to the cache and fill it in later.
Robert Phillipsd464feb2020-10-08 11:00:02 -0400393 std::tie(lazyView, trampoline) = GrThreadSafeCache::CreateLazyView(
Robert Phillipsfde67e42020-10-07 15:33:43 -0400394 dContext, GrColorType::kAlpha_8, maskRect->size(),
395 kMaskOrigin, SkBackingFit::kApprox);
396 if (!lazyView) {
397 return {}; // fall back to a SW-created mask - 'create_mask_GPU' probably won't succeed
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400398 }
399
Robert Phillipsfde67e42020-10-07 15:33:43 -0400400 key->setCustomData(create_data(*maskRect, unclippedDevShapeBounds));
Robert Phillipsd464feb2020-10-08 11:00:02 -0400401 auto [cachedView, data] = threadSafeCache->findOrAddWithData(*key, lazyView);
Robert Phillipsfde67e42020-10-07 15:33:43 -0400402 if (cachedView != lazyView) {
403 // In this case, the gpu-thread lost out to a recording thread - use its result.
404 SkASSERT(data);
405 SkASSERT(cachedView.asTextureProxy());
406 SkASSERT(cachedView.origin() == kMaskOrigin);
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400407
Robert Phillipsfde67e42020-10-07 15:33:43 -0400408 *maskRect = extract_draw_rect_from_data(data.get(), unclippedDevShapeBounds);
409 return cachedView;
410 }
411 }
412
Brian Salomoneebe7352020-12-09 16:37:04 -0500413 std::unique_ptr<GrSurfaceDrawContext> maskRTC(create_mask_GPU(
Robert Phillipsfde67e42020-10-07 15:33:43 -0400414 dContext,
415 *maskRect,
416 viewMatrix,
417 shape,
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500418 surfaceDrawContext->numSamples()));
Robert Phillipsfde67e42020-10-07 15:33:43 -0400419 if (!maskRTC) {
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400420 if (key->isValid()) {
Robert Phillipsfde67e42020-10-07 15:33:43 -0400421 // It is very unlikely that 'create_mask_GPU' will fail after 'CreateLazyView'
422 // succeeded but, if it does, remove the lazy-view from the cache and fallback to
423 // a SW-created mask. Note that any recording threads that glommed onto the
424 // lazy-view will have to, later, drop those draws.
Robert Phillipsd464feb2020-10-08 11:00:02 -0400425 threadSafeCache->remove(*key);
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400426 }
Robert Phillipsfde67e42020-10-07 15:33:43 -0400427 return {};
428 }
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400429
Robert Phillipsfde67e42020-10-07 15:33:43 -0400430 auto filteredMaskView = filter->filterMaskGPU(dContext,
431 maskRTC->readSurfaceView(),
432 maskRTC->colorInfo().colorType(),
433 maskRTC->colorInfo().alphaType(),
434 viewMatrix,
435 *maskRect);
436 if (!filteredMaskView) {
437 if (key->isValid()) {
438 // Remove the lazy-view from the cache and fallback to a SW-created mask. Note that
439 // any recording threads that glommed onto the lazy-view will have to, later, drop
440 // those draws.
Robert Phillipsd464feb2020-10-08 11:00:02 -0400441 threadSafeCache->remove(*key);
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400442 }
Robert Phillipsfde67e42020-10-07 15:33:43 -0400443 return {};
444 }
445
446 if (key->isValid()) {
447 SkASSERT(filteredMaskView.dimensions() == lazyView.dimensions());
448 SkASSERT(filteredMaskView.swizzle() == lazyView.swizzle());
449 SkASSERT(filteredMaskView.origin() == lazyView.origin());
450
451 trampoline->fProxy = filteredMaskView.asTextureProxyRef();
452 return lazyView;
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400453 }
454
455 return filteredMaskView;
456}
457
458static void draw_shape_with_mask_filter(GrRecordingContext* rContext,
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500459 GrSurfaceDrawContext* surfaceDrawContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400460 const GrClip* clip,
Robert Phillips27927a52018-08-20 13:18:12 -0400461 GrPaint&& paint,
Robert Phillips27927a52018-08-20 13:18:12 -0400462 const SkMatrix& viewMatrix,
463 const SkMaskFilterBase* maskFilter,
Michael Ludwig2686d692020-04-17 20:21:37 +0000464 const GrStyledShape& origShape) {
Robert Phillips27927a52018-08-20 13:18:12 -0400465 SkASSERT(maskFilter);
466
Michael Ludwig2686d692020-04-17 20:21:37 +0000467 const GrStyledShape* shape = &origShape;
468 SkTLazy<GrStyledShape> tmpShape;
Robert Phillips27927a52018-08-20 13:18:12 -0400469
470 if (origShape.style().applies()) {
471 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
Robert Phillips27358372021-02-10 12:28:38 -0500472 if (styleScale == 0) {
Robert Phillips27927a52018-08-20 13:18:12 -0400473 return;
bsalomon6663acf2016-05-10 09:14:17 -0700474 }
Robert Phillips27927a52018-08-20 13:18:12 -0400475
476 tmpShape.init(origShape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale));
John Stilesa008b0f2020-08-16 08:48:02 -0400477 if (tmpShape->isEmpty()) {
Robert Phillips27927a52018-08-20 13:18:12 -0400478 return;
479 }
480
481 shape = tmpShape.get();
bsalomon6663acf2016-05-10 09:14:17 -0700482 }
bsalomonc55271f2015-11-09 11:55:57 -0800483
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500484 if (maskFilter->directFilterMaskGPU(rContext, surfaceDrawContext, std::move(paint), clip,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400485 viewMatrix, *shape)) {
Robert Phillipsb889f202018-08-17 19:55:59 +0000486 // the mask filter was able to draw itself directly, so there's nothing
487 // left to do.
488 return;
489 }
Mike Klein16885072018-12-11 09:54:31 -0500490 assert_alive(paint);
Robert Phillipsb889f202018-08-17 19:55:59 +0000491
Robert Phillips27927a52018-08-20 13:18:12 -0400492 // If the path is hairline, ignore inverse fill.
493 bool inverseFilled = shape->inverseFilled() &&
Robert Phillips62214f72021-06-15 10:12:51 -0400494 !GrIsStrokeHairlineOrEquivalent(shape->style(), viewMatrix, nullptr);
Robert Phillips27927a52018-08-20 13:18:12 -0400495
496 SkIRect unclippedDevShapeBounds, devClipBounds;
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500497 if (!get_shape_and_clip_bounds(surfaceDrawContext, clip, *shape, viewMatrix,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400498 &unclippedDevShapeBounds, &devClipBounds)) {
Robert Phillips27927a52018-08-20 13:18:12 -0400499 // TODO: just cons up an opaque mask here
500 if (!inverseFilled) {
501 return;
502 }
503 }
504
Robert Phillips31c080b2018-08-23 10:45:32 -0400505 GrUniqueKey maskKey;
Robert Phillips52c17c42020-10-05 11:55:59 -0400506 SkIRect boundsForClip;
507 if (!compute_key_and_clip_bounds(&maskKey, &boundsForClip,
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500508 surfaceDrawContext->caps(),
Robert Phillips52c17c42020-10-05 11:55:59 -0400509 viewMatrix, inverseFilled,
510 maskFilter, *shape,
511 unclippedDevShapeBounds,
512 devClipBounds)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400513 return; // 'shape' was entirely clipped out
Robert Phillips31c080b2018-08-23 10:45:32 -0400514 }
515
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400516 GrSurfaceProxyView filteredMaskView;
Robert Phillips31c080b2018-08-23 10:45:32 -0400517 SkIRect maskRect;
bsalomonc55271f2015-11-09 11:55:57 -0800518
Robert Phillipsfde67e42020-10-07 15:33:43 -0400519 if (auto dContext = rContext->asDirectContext()) {
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500520 filteredMaskView = hw_create_filtered_mask(dContext, surfaceDrawContext,
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400521 viewMatrix, *shape, maskFilter,
522 unclippedDevShapeBounds, boundsForClip,
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400523 &maskRect, &maskKey);
Brian Salomond005b692020-04-01 15:47:05 -0400524 if (filteredMaskView) {
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500525 if (draw_mask(surfaceDrawContext, clip, viewMatrix, maskRect, std::move(paint),
Greg Daniel5c082492020-01-29 15:06:49 -0500526 std::move(filteredMaskView))) {
Robert Phillips20390c32018-08-17 11:01:03 -0400527 // This path is completely drawn
528 return;
529 }
Mike Klein16885072018-12-11 09:54:31 -0500530 assert_alive(paint);
Robert Phillips20390c32018-08-17 11:01:03 -0400531 }
bsalomonc55271f2015-11-09 11:55:57 -0800532 }
533
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400534 // Either HW mask rendering failed or we're in a DDL recording thread
535 filteredMaskView = sw_create_filtered_mask(rContext,
536 viewMatrix, *shape, maskFilter,
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400537 unclippedDevShapeBounds, boundsForClip,
538 &maskRect, &maskKey);
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400539 if (filteredMaskView) {
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500540 if (draw_mask(surfaceDrawContext, clip, viewMatrix, maskRect, std::move(paint),
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400541 std::move(filteredMaskView))) {
542 return;
543 }
544 assert_alive(paint);
545 }
bsalomon6663acf2016-05-10 09:14:17 -0700546}
547
Robert Phillips7af8fe52019-02-14 17:27:00 -0500548void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext* context,
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500549 GrSurfaceDrawContext* surfaceDrawContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400550 const GrClip* clip,
Michael Ludwig2686d692020-04-17 20:21:37 +0000551 const GrStyledShape& shape,
Robert Phillips27927a52018-08-20 13:18:12 -0400552 GrPaint&& paint,
Robert Phillips27927a52018-08-20 13:18:12 -0400553 const SkMatrix& viewMatrix,
554 const SkMaskFilter* mf) {
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500555 draw_shape_with_mask_filter(context, surfaceDrawContext, clip, std::move(paint),
Robert Phillips27927a52018-08-20 13:18:12 -0400556 viewMatrix, as_MFB(mf), shape);
bsalomonc55271f2015-11-09 11:55:57 -0800557}
558
Robert Phillips69893702019-02-22 11:16:30 -0500559void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext* context,
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500560 GrSurfaceDrawContext* surfaceDrawContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400561 const GrClip* clip,
Robert Phillips27927a52018-08-20 13:18:12 -0400562 const SkPaint& paint,
Brian Osman449b1152020-04-15 16:43:00 -0400563 const SkMatrixProvider& matrixProvider,
Michael Ludwig2686d692020-04-17 20:21:37 +0000564 const GrStyledShape& shape) {
Robert Phillips9eb00022020-06-30 15:30:12 -0400565 if (context->abandoned()) {
Robert Phillipsbebfd412018-03-08 11:07:23 -0500566 return;
567 }
568
robertphillipsccb1b572015-05-27 11:02:55 -0700569 GrPaint grPaint;
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500570 if (!SkPaintToGrPaint(context, surfaceDrawContext->colorInfo(), paint, matrixProvider,
Brian Osman449b1152020-04-15 16:43:00 -0400571 &grPaint)) {
robertphillipsccb1b572015-05-27 11:02:55 -0700572 return;
573 }
Robert Phillips27927a52018-08-20 13:18:12 -0400574
Brian Osman449b1152020-04-15 16:43:00 -0400575 const SkMatrix& viewMatrix(matrixProvider.localToDevice());
Mike Reed80747ef2018-01-23 15:29:32 -0500576 SkMaskFilterBase* mf = as_MFB(paint.getMaskFilter());
Mike Reedbfadcf02018-01-20 22:24:21 +0000577 if (mf && !mf->hasFragmentProcessor()) {
Robert Phillipsa29a9562016-10-20 09:40:55 -0400578 // The MaskFilter wasn't already handled in SkPaintToGrPaint
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500579 draw_shape_with_mask_filter(context, surfaceDrawContext, clip, std::move(grPaint),
Robert Phillips27927a52018-08-20 13:18:12 -0400580 viewMatrix, mf, shape);
bsalomonc55271f2015-11-09 11:55:57 -0800581 } else {
Chris Dalton94df5722021-04-19 17:40:40 -0600582 surfaceDrawContext->drawShape(clip, std::move(grPaint), surfaceDrawContext->chooseAA(paint),
Chris Daltonfd708652021-03-17 21:10:17 -0600583 viewMatrix, GrStyledShape(shape));
robertphillipsccb1b572015-05-27 11:02:55 -0700584 }
robertphillipsccb1b572015-05-27 11:02:55 -0700585}