blob: 4ba996ee351af4932292c24a4f5384ceadf5911a [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/GrStyle.h"
Brian Salomoneebe7352020-12-09 16:37:04 -050020#include "src/gpu/GrSurfaceDrawContext.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 Phillipsc7c2baf2018-03-08 09:51:04 -050027
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "include/core/SkPaint.h"
29#include "src/core/SkDraw.h"
30#include "src/core/SkMaskFilterBase.h"
Brian Osman449b1152020-04-15 16:43:00 -040031#include "src/core/SkMatrixProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032#include "src/core/SkTLazy.h"
33#include "src/gpu/SkGr.h"
robertphillipsccb1b572015-05-27 11:02:55 -070034
35static bool clip_bounds_quick_reject(const SkIRect& clipBounds, const SkIRect& rect) {
36 return clipBounds.isEmpty() || rect.isEmpty() || !SkIRect::Intersects(clipBounds, rect);
37}
38
Brian Salomond005b692020-04-01 15:47:05 -040039static constexpr auto kMaskOrigin = kTopLeft_GrSurfaceOrigin;
40
robertphillipsccb1b572015-05-27 11:02:55 -070041// Draw a mask using the supplied paint. Since the coverage/geometry
42// is already burnt into the mask this boils down to a rect draw.
43// Return true if the mask was successfully drawn.
Brian Salomon1aa1f5f2020-12-11 17:25:17 -050044static bool draw_mask(GrSurfaceDrawContext* surfaceDrawContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -040045 const GrClip* clip,
robertphillipsccb1b572015-05-27 11:02:55 -070046 const SkMatrix& viewMatrix,
Chris Dalton35619552021-03-10 19:20:43 -070047 const SkIRect& maskBounds,
Brian Salomon82f44312017-01-11 13:42:54 -050048 GrPaint&& paint,
Greg Daniel5c082492020-01-29 15:06:49 -050049 GrSurfaceProxyView mask) {
Brian Salomon82f44312017-01-11 13:42:54 -050050 SkMatrix inverse;
51 if (!viewMatrix.invert(&inverse)) {
52 return false;
53 }
Robert Phillips4a24da52016-12-14 09:00:07 -050054
Brian Salomonb43d6992021-01-05 14:37:40 -050055 mask.concatSwizzle(GrSwizzle("aaaa"));
56
Chris Dalton35619552021-03-10 19:20:43 -070057 SkMatrix matrix = SkMatrix::Translate(-SkIntToScalar(maskBounds.fLeft),
58 -SkIntToScalar(maskBounds.fTop));
Brian Salomon2ebd0c82016-10-03 17:15:28 -040059 matrix.preConcat(viewMatrix);
John Stiles41d91b62020-07-21 14:39:40 -040060 paint.setCoverageFragmentProcessor(
Greg Danield2ccbb52020-02-05 10:45:39 -050061 GrTextureEffect::Make(std::move(mask), kUnknown_SkAlphaType, matrix));
robertphillipsccb1b572015-05-27 11:02:55 -070062
Chris Dalton35619552021-03-10 19:20:43 -070063 surfaceDrawContext->fillPixelsWithLocalMatrix(clip, std::move(paint), maskBounds, inverse);
robertphillipsccb1b572015-05-27 11:02:55 -070064 return true;
65}
66
Robert Phillipsc7c2baf2018-03-08 09:51:04 -050067static void mask_release_proc(void* addr, void* /*context*/) {
68 SkMask::FreeImage(addr);
69}
70
Robert Phillips6e17ffe2020-10-06 14:52:11 -040071// This stores the mapping from an unclipped, integerized, device-space, shape bounds to
72// the filtered mask's draw rect.
73struct DrawRectData {
74 SkIVector fOffset;
75 SkISize fSize;
76};
77
78static sk_sp<SkData> create_data(const SkIRect& drawRect, const SkIRect& origDevBounds) {
79
80 DrawRectData drawRectData { {drawRect.fLeft - origDevBounds.fLeft,
81 drawRect.fTop - origDevBounds.fTop},
82 drawRect.size() };
83
84 return SkData::MakeWithCopy(&drawRectData, sizeof(drawRectData));
85}
86
87static SkIRect extract_draw_rect_from_data(SkData* data, const SkIRect& origDevBounds) {
88 auto drawRectData = static_cast<const DrawRectData*>(data->data());
89
90 return SkIRect::MakeXYWH(origDevBounds.fLeft + drawRectData->fOffset.fX,
91 origDevBounds.fTop + drawRectData->fOffset.fY,
92 drawRectData->fSize.fWidth,
93 drawRectData->fSize.fHeight);
94}
95
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -040096static GrSurfaceProxyView sw_create_filtered_mask(GrRecordingContext* rContext,
97 const SkMatrix& viewMatrix,
98 const GrStyledShape& shape,
99 const SkMaskFilter* filter,
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400100 const SkIRect& unclippedDevShapeBounds,
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400101 const SkIRect& clipBounds,
102 SkIRect* drawRect,
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400103 GrUniqueKey* key) {
Robert Phillips27927a52018-08-20 13:18:12 -0400104 SkASSERT(filter);
105 SkASSERT(!shape.style().applies());
106
Robert Phillipsd464feb2020-10-08 11:00:02 -0400107 auto threadSafeCache = rContext->priv().threadSafeCache();
Robert Phillips27927a52018-08-20 13:18:12 -0400108
Greg Daniel5c082492020-01-29 15:06:49 -0500109 GrSurfaceProxyView filteredMaskView;
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400110 sk_sp<SkData> data;
Robert Phillips27927a52018-08-20 13:18:12 -0400111
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400112 if (key->isValid()) {
Robert Phillipsd464feb2020-10-08 11:00:02 -0400113 std::tie(filteredMaskView, data) = threadSafeCache->findWithData(*key);
robertphillipsccb1b572015-05-27 11:02:55 -0700114 }
115
Brian Salomond005b692020-04-01 15:47:05 -0400116 if (filteredMaskView) {
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400117 SkASSERT(data);
Robert Phillipsc73c1af2020-09-29 13:47:39 -0400118 SkASSERT(kMaskOrigin == filteredMaskView.origin());
119
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400120 *drawRect = extract_draw_rect_from_data(data.get(), unclippedDevShapeBounds);
Robert Phillips31c080b2018-08-23 10:45:32 -0400121 } else {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400122 SkStrokeRec::InitStyle fillOrHairline = shape.style().isSimpleHairline()
123 ? SkStrokeRec::kHairline_InitStyle
124 : SkStrokeRec::kFill_InitStyle;
125
Robert Phillips31c080b2018-08-23 10:45:32 -0400126 // TODO: it seems like we could create an SkDraw here and set its fMatrix field rather
127 // than explicitly transforming the path to device space.
128 SkPath devPath;
129
130 shape.asPath(&devPath);
131
132 devPath.transform(viewMatrix);
133
134 SkMask srcM, dstM;
135 if (!SkDraw::DrawToMask(devPath, &clipBounds, filter, &viewMatrix, &srcM,
136 SkMask::kComputeBoundsAndRenderImage_CreateMode, fillOrHairline)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400137 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400138 }
139 SkAutoMaskFreeImage autoSrc(srcM.fImage);
140
141 SkASSERT(SkMask::kA8_Format == srcM.fFormat);
142
143 if (!as_MFB(filter)->filterMask(&dstM, srcM, viewMatrix, nullptr)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400144 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400145 }
146 // this will free-up dstM when we're done (allocated in filterMask())
147 SkAutoMaskFreeImage autoDst(dstM.fImage);
148
149 if (clip_bounds_quick_reject(clipBounds, dstM.fBounds)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400150 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400151 }
152
153 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
154 // the current clip (and identity matrix) and GrPaint settings
155 SkBitmap bm;
156 if (!bm.installPixels(SkImageInfo::MakeA8(dstM.fBounds.width(), dstM.fBounds.height()),
157 autoDst.release(), dstM.fRowBytes, mask_release_proc, nullptr)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400158 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400159 }
160 bm.setImmutable();
161
Brian Salomon27c42022021-04-28 12:39:21 -0400162 std::tie(filteredMaskView, std::ignore) = GrMakeUncachedBitmapProxyView(
163 rContext,
164 bm,
165 GrMipmapped::kNo,
166 SkBackingFit::kApprox);
167 if (!filteredMaskView) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400168 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400169 }
170
Brian Salomond005b692020-04-01 15:47:05 -0400171 SkASSERT(kMaskOrigin == filteredMaskView.origin());
Robert Phillips59b39e72018-08-30 11:51:25 -0400172
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400173 *drawRect = dstM.fBounds;
Robert Phillips31c080b2018-08-23 10:45:32 -0400174
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400175 if (key->isValid()) {
176 key->setCustomData(create_data(*drawRect, unclippedDevShapeBounds));
Robert Phillipsd464feb2020-10-08 11:00:02 -0400177 std::tie(filteredMaskView, data) = threadSafeCache->addWithData(*key, filteredMaskView);
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400178 // If we got a different view back from 'addWithData' it could have a different drawRect
179 *drawRect = extract_draw_rect_from_data(data.get(), unclippedDevShapeBounds);
Robert Phillips31c080b2018-08-23 10:45:32 -0400180 }
Robert Phillips48ce22b2018-03-23 10:33:12 -0400181 }
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500182
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400183 return filteredMaskView;
robertphillipsccb1b572015-05-27 11:02:55 -0700184}
185
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500186// Create a mask of 'shape' and return the resulting surfaceDrawContext
Brian Salomoneebe7352020-12-09 16:37:04 -0500187static std::unique_ptr<GrSurfaceDrawContext> create_mask_GPU(GrRecordingContext* context,
188 const SkIRect& maskRect,
189 const SkMatrix& origViewMatrix,
190 const GrStyledShape& shape,
191 int sampleCnt) {
Chris Dalton92934d72021-04-23 09:08:53 -0600192 // We cache blur masks. Use default surface props here so we can use the same cached mask
193 // regardless of the final dst surface.
194 SkSurfaceProps defaultSurfaceProps;
Chris Daltonf5b87f92021-04-19 17:27:09 -0600195
Chris Dalton0493fbd2019-09-18 15:49:46 -0600196 // Use GrResourceProvider::MakeApprox to implement our own approximate size matching, but demand
197 // a "SkBackingFit::kExact" size match on the actual render target. We do this because the
198 // filter will reach outside the src bounds, so we need to pre-clear these values to ensure a
199 // "decal" sampling effect (i.e., ensure reads outside the src bounds return alpha=0).
200 //
201 // FIXME: Reads outside the left and top edges will actually clamp to the edge pixel. And in the
202 // event that MakeApprox does not change the size, reads outside the right and/or bottom will do
203 // the same. We should offset our filter within the render target and expand the size as needed
204 // to guarantee at least 1px of padding on all sides.
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400205 auto approxSize = GrResourceProvider::MakeApprox(maskRect.size());
Chris Daltonf5b87f92021-04-19 17:27:09 -0600206 auto rtContext = GrSurfaceDrawContext::MakeWithFallback(context, GrColorType::kAlpha_8, nullptr,
207 SkBackingFit::kExact, approxSize,
Chris Dalton92934d72021-04-23 09:08:53 -0600208 defaultSurfaceProps, sampleCnt,
Chris Daltonf5b87f92021-04-19 17:27:09 -0600209 GrMipmapped::kNo, GrProtected::kNo,
210 kMaskOrigin);
robertphillipsd728f0c2016-11-21 11:05:03 -0800211 if (!rtContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700212 return nullptr;
robertphillipsccb1b572015-05-27 11:02:55 -0700213 }
214
Chris Dalton0493fbd2019-09-18 15:49:46 -0600215 rtContext->clear(SK_PMColor4fTRANSPARENT);
robertphillipsccb1b572015-05-27 11:02:55 -0700216
Brian Salomon82f44312017-01-11 13:42:54 -0500217 GrPaint maskPaint;
218 maskPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op);
robertphillipsccb1b572015-05-27 11:02:55 -0700219
220 // setup new clip
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400221 GrFixedClip clip(rtContext->dimensions(), SkIRect::MakeWH(maskRect.width(), maskRect.height()));
robertphillipsccb1b572015-05-27 11:02:55 -0700222
Brian Salomond005b692020-04-01 15:47:05 -0400223 // Draw the mask into maskTexture with the path's integerized top-left at the origin using
224 // maskPaint.
Robert Phillips27927a52018-08-20 13:18:12 -0400225 SkMatrix viewMatrix = origViewMatrix;
226 viewMatrix.postTranslate(-SkIntToScalar(maskRect.fLeft), -SkIntToScalar(maskRect.fTop));
Chris Dalton21859012021-01-29 23:44:53 -0700227 rtContext->drawShape(&clip, std::move(maskPaint), GrAA::kYes, viewMatrix, GrStyledShape(shape));
Robert Phillips40b05c32019-09-20 12:40:55 -0400228 return rtContext;
robertphillipsccb1b572015-05-27 11:02:55 -0700229}
230
Michael Ludwig2686d692020-04-17 20:21:37 +0000231static bool get_unclipped_shape_dev_bounds(const GrStyledShape& shape, const SkMatrix& matrix,
Robert Phillips27927a52018-08-20 13:18:12 -0400232 SkIRect* devBounds) {
233 SkRect shapeBounds = shape.styledBounds();
234 if (shapeBounds.isEmpty()) {
235 return false;
236 }
237 SkRect shapeDevBounds;
238 matrix.mapRect(&shapeDevBounds, shapeBounds);
239 // Even though these are "unclipped" bounds we still clip to the int32_t range.
240 // This is the largest int32_t that is representable exactly as a float. The next 63 larger ints
241 // would round down to this value when cast to a float, but who really cares.
242 // INT32_MIN is exactly representable.
243 static constexpr int32_t kMaxInt = 2147483520;
244 if (!shapeDevBounds.intersect(SkRect::MakeLTRB(INT32_MIN, INT32_MIN, kMaxInt, kMaxInt))) {
245 return false;
246 }
247 // Make sure that the resulting SkIRect can have representable width and height
248 if (SkScalarRoundToInt(shapeDevBounds.width()) > kMaxInt ||
249 SkScalarRoundToInt(shapeDevBounds.height()) > kMaxInt) {
250 return false;
251 }
252 shapeDevBounds.roundOut(devBounds);
253 return true;
254}
bsalomonc55271f2015-11-09 11:55:57 -0800255
Robert Phillips27927a52018-08-20 13:18:12 -0400256// Gets the shape bounds, the clip bounds, and the intersection (if any). Returns false if there
257// is no intersection.
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500258static bool get_shape_and_clip_bounds(GrSurfaceDrawContext* surfaceDrawContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400259 const GrClip* clip,
Michael Ludwig2686d692020-04-17 20:21:37 +0000260 const GrStyledShape& shape,
Robert Phillips27927a52018-08-20 13:18:12 -0400261 const SkMatrix& matrix,
262 SkIRect* unclippedDevShapeBounds,
263 SkIRect* devClipBounds) {
264 // compute bounds as intersection of rt size, clip, and path
Michael Ludwige06a8972020-06-11 10:29:00 -0400265 *devClipBounds = clip ? clip->getConservativeBounds()
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500266 : SkIRect::MakeWH(surfaceDrawContext->width(),
267 surfaceDrawContext->height());
bsalomonc55271f2015-11-09 11:55:57 -0800268
Robert Phillips27927a52018-08-20 13:18:12 -0400269 if (!get_unclipped_shape_dev_bounds(shape, matrix, unclippedDevShapeBounds)) {
Brian Salomon44207f32020-01-06 15:20:18 -0500270 *unclippedDevShapeBounds = SkIRect::MakeEmpty();
Robert Phillips27927a52018-08-20 13:18:12 -0400271 return false;
bsalomonc55271f2015-11-09 11:55:57 -0800272 }
273
Robert Phillips27927a52018-08-20 13:18:12 -0400274 return true;
275}
276
Robert Phillips52c17c42020-10-05 11:55:59 -0400277// The key and clip-bounds are computed together because the caching decision can impact the
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400278// clip-bound - since we only cache un-clipped masks the clip can be removed entirely.
Robert Phillips52c17c42020-10-05 11:55:59 -0400279// A 'false' return value indicates that the shape is known to be clipped away.
280static bool compute_key_and_clip_bounds(GrUniqueKey* maskKey,
281 SkIRect* boundsForClip,
282 const GrCaps* caps,
283 const SkMatrix& viewMatrix,
284 bool inverseFilled,
285 const SkMaskFilterBase* maskFilter,
286 const GrStyledShape& shape,
287 const SkIRect& unclippedDevShapeBounds,
288 const SkIRect& devClipBounds) {
289 *boundsForClip = devClipBounds;
290
291#ifndef SK_DISABLE_MASKFILTERED_MASK_CACHING
292 // To prevent overloading the cache with entries during animations we limit the cache of masks
Robert Phillipsabf03532021-05-05 18:39:26 +0000293 // to cases where the matrix preserves axis alignment.
294 bool useCache = !inverseFilled && viewMatrix.preservesAxisAlignment() &&
295 shape.hasUnstyledKey() && as_MFB(maskFilter)->asABlur(nullptr);
Robert Phillips52c17c42020-10-05 11:55:59 -0400296
297 if (useCache) {
298 SkIRect clippedMaskRect, unClippedMaskRect;
299 maskFilter->canFilterMaskGPU(shape, unclippedDevShapeBounds, devClipBounds,
300 viewMatrix, &clippedMaskRect);
301 maskFilter->canFilterMaskGPU(shape, unclippedDevShapeBounds, unclippedDevShapeBounds,
302 viewMatrix, &unClippedMaskRect);
303 if (clippedMaskRect.isEmpty()) {
304 return false;
305 }
306
307 // Use the cache only if >50% of the filtered mask is visible.
308 int unclippedWidth = unClippedMaskRect.width();
309 int unclippedHeight = unClippedMaskRect.height();
310 int64_t unclippedArea = sk_64_mul(unclippedWidth, unclippedHeight);
311 int64_t clippedArea = sk_64_mul(clippedMaskRect.width(), clippedMaskRect.height());
312 int maxTextureSize = caps->maxTextureSize();
313 if (unclippedArea > 2 * clippedArea || unclippedWidth > maxTextureSize ||
314 unclippedHeight > maxTextureSize) {
315 useCache = false;
316 } else {
317 // Make the clip not affect the mask
318 *boundsForClip = unclippedDevShapeBounds;
319 }
320 }
321
322 if (useCache) {
323 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
324 GrUniqueKey::Builder builder(maskKey, kDomain, 5 + 2 + shape.unstyledKeySize(),
325 "Mask Filtered Masks");
326
327 // We require the upper left 2x2 of the matrix to match exactly for a cache hit.
328 SkScalar sx = viewMatrix.get(SkMatrix::kMScaleX);
329 SkScalar sy = viewMatrix.get(SkMatrix::kMScaleY);
330 SkScalar kx = viewMatrix.get(SkMatrix::kMSkewX);
331 SkScalar ky = viewMatrix.get(SkMatrix::kMSkewY);
332 SkScalar tx = viewMatrix.get(SkMatrix::kMTransX);
333 SkScalar ty = viewMatrix.get(SkMatrix::kMTransY);
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400334 // Allow 8 bits each in x and y of subpixel positioning. But, note that we're allowing
335 // reuse for integer translations.
Robert Phillips52c17c42020-10-05 11:55:59 -0400336 SkFixed fracX = SkScalarToFixed(SkScalarFraction(tx)) & 0x0000FF00;
337 SkFixed fracY = SkScalarToFixed(SkScalarFraction(ty)) & 0x0000FF00;
338
339 builder[0] = SkFloat2Bits(sx);
340 builder[1] = SkFloat2Bits(sy);
341 builder[2] = SkFloat2Bits(kx);
342 builder[3] = SkFloat2Bits(ky);
343 // Distinguish between hairline and filled paths. For hairlines, we also need to include
344 // the cap. (SW grows hairlines by 0.5 pixel with round and square caps). Note that
345 // stroke-and-fill of hairlines is turned into pure fill by SkStrokeRec, so this covers
346 // all cases we might see.
347 uint32_t styleBits = shape.style().isSimpleHairline()
348 ? ((shape.style().strokeRec().getCap() << 1) | 1)
349 : 0;
350 builder[4] = fracX | (fracY >> 8) | (styleBits << 16);
351
352 SkMaskFilterBase::BlurRec rec;
353 SkAssertResult(as_MFB(maskFilter)->asABlur(&rec));
354
355 builder[5] = rec.fStyle; // TODO: we could put this with the other style bits
356 builder[6] = SkFloat2Bits(rec.fSigma);
357 shape.writeUnstyledKey(&builder[7]);
358 }
359#endif
360
361 return true;
362}
363
Robert Phillipsfde67e42020-10-07 15:33:43 -0400364static GrSurfaceProxyView hw_create_filtered_mask(GrDirectContext* dContext,
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500365 GrSurfaceDrawContext* surfaceDrawContext,
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400366 const SkMatrix& viewMatrix,
367 const GrStyledShape& shape,
368 const SkMaskFilterBase* filter,
369 const SkIRect& unclippedDevShapeBounds,
370 const SkIRect& clipBounds,
371 SkIRect* maskRect,
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400372 GrUniqueKey* key) {
Robert Phillipsfde67e42020-10-07 15:33:43 -0400373 if (!filter->canFilterMaskGPU(shape,
374 unclippedDevShapeBounds,
375 clipBounds,
376 viewMatrix,
377 maskRect)) {
378 return {};
379 }
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400380
Robert Phillipsfde67e42020-10-07 15:33:43 -0400381 if (clip_bounds_quick_reject(clipBounds, *maskRect)) {
382 // clipped out
383 return {};
384 }
385
Robert Phillipsd464feb2020-10-08 11:00:02 -0400386 auto threadSafeCache = dContext->priv().threadSafeCache();
Robert Phillipsfde67e42020-10-07 15:33:43 -0400387
388 GrSurfaceProxyView lazyView;
Robert Phillipsd464feb2020-10-08 11:00:02 -0400389 sk_sp<GrThreadSafeCache::Trampoline> trampoline;
Robert Phillipsfde67e42020-10-07 15:33:43 -0400390
391 if (key->isValid()) {
392 // In this case, we want GPU-filtered masks to have priority over SW-generated ones so
393 // we pre-emptively add a lazy-view to the cache and fill it in later.
Robert Phillipsd464feb2020-10-08 11:00:02 -0400394 std::tie(lazyView, trampoline) = GrThreadSafeCache::CreateLazyView(
Robert Phillipsfde67e42020-10-07 15:33:43 -0400395 dContext, GrColorType::kAlpha_8, maskRect->size(),
396 kMaskOrigin, SkBackingFit::kApprox);
397 if (!lazyView) {
398 return {}; // fall back to a SW-created mask - 'create_mask_GPU' probably won't succeed
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400399 }
400
Robert Phillipsfde67e42020-10-07 15:33:43 -0400401 key->setCustomData(create_data(*maskRect, unclippedDevShapeBounds));
Robert Phillipsd464feb2020-10-08 11:00:02 -0400402 auto [cachedView, data] = threadSafeCache->findOrAddWithData(*key, lazyView);
Robert Phillipsfde67e42020-10-07 15:33:43 -0400403 if (cachedView != lazyView) {
404 // In this case, the gpu-thread lost out to a recording thread - use its result.
405 SkASSERT(data);
406 SkASSERT(cachedView.asTextureProxy());
407 SkASSERT(cachedView.origin() == kMaskOrigin);
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400408
Robert Phillipsfde67e42020-10-07 15:33:43 -0400409 *maskRect = extract_draw_rect_from_data(data.get(), unclippedDevShapeBounds);
410 return cachedView;
411 }
412 }
413
Brian Salomoneebe7352020-12-09 16:37:04 -0500414 std::unique_ptr<GrSurfaceDrawContext> maskRTC(create_mask_GPU(
Robert Phillipsfde67e42020-10-07 15:33:43 -0400415 dContext,
416 *maskRect,
417 viewMatrix,
418 shape,
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500419 surfaceDrawContext->numSamples()));
Robert Phillipsfde67e42020-10-07 15:33:43 -0400420 if (!maskRTC) {
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400421 if (key->isValid()) {
Robert Phillipsfde67e42020-10-07 15:33:43 -0400422 // It is very unlikely that 'create_mask_GPU' will fail after 'CreateLazyView'
423 // succeeded but, if it does, remove the lazy-view from the cache and fallback to
424 // a SW-created mask. Note that any recording threads that glommed onto the
425 // lazy-view will have to, later, drop those draws.
Robert Phillipsd464feb2020-10-08 11:00:02 -0400426 threadSafeCache->remove(*key);
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400427 }
Robert Phillipsfde67e42020-10-07 15:33:43 -0400428 return {};
429 }
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400430
Robert Phillipsfde67e42020-10-07 15:33:43 -0400431 auto filteredMaskView = filter->filterMaskGPU(dContext,
432 maskRTC->readSurfaceView(),
433 maskRTC->colorInfo().colorType(),
434 maskRTC->colorInfo().alphaType(),
435 viewMatrix,
436 *maskRect);
437 if (!filteredMaskView) {
438 if (key->isValid()) {
439 // Remove the lazy-view from the cache and fallback to a SW-created mask. Note that
440 // any recording threads that glommed onto the lazy-view will have to, later, drop
441 // those draws.
Robert Phillipsd464feb2020-10-08 11:00:02 -0400442 threadSafeCache->remove(*key);
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400443 }
Robert Phillipsfde67e42020-10-07 15:33:43 -0400444 return {};
445 }
446
447 if (key->isValid()) {
448 SkASSERT(filteredMaskView.dimensions() == lazyView.dimensions());
449 SkASSERT(filteredMaskView.swizzle() == lazyView.swizzle());
450 SkASSERT(filteredMaskView.origin() == lazyView.origin());
451
452 trampoline->fProxy = filteredMaskView.asTextureProxyRef();
453 return lazyView;
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400454 }
455
456 return filteredMaskView;
457}
458
459static void draw_shape_with_mask_filter(GrRecordingContext* rContext,
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500460 GrSurfaceDrawContext* surfaceDrawContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400461 const GrClip* clip,
Robert Phillips27927a52018-08-20 13:18:12 -0400462 GrPaint&& paint,
Robert Phillips27927a52018-08-20 13:18:12 -0400463 const SkMatrix& viewMatrix,
464 const SkMaskFilterBase* maskFilter,
Michael Ludwig2686d692020-04-17 20:21:37 +0000465 const GrStyledShape& origShape) {
Robert Phillips27927a52018-08-20 13:18:12 -0400466 SkASSERT(maskFilter);
467
Michael Ludwig2686d692020-04-17 20:21:37 +0000468 const GrStyledShape* shape = &origShape;
469 SkTLazy<GrStyledShape> tmpShape;
Robert Phillips27927a52018-08-20 13:18:12 -0400470
471 if (origShape.style().applies()) {
472 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
Robert Phillips27358372021-02-10 12:28:38 -0500473 if (styleScale == 0) {
Robert Phillips27927a52018-08-20 13:18:12 -0400474 return;
bsalomon6663acf2016-05-10 09:14:17 -0700475 }
Robert Phillips27927a52018-08-20 13:18:12 -0400476
477 tmpShape.init(origShape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale));
John Stilesa008b0f2020-08-16 08:48:02 -0400478 if (tmpShape->isEmpty()) {
Robert Phillips27927a52018-08-20 13:18:12 -0400479 return;
480 }
481
482 shape = tmpShape.get();
bsalomon6663acf2016-05-10 09:14:17 -0700483 }
bsalomonc55271f2015-11-09 11:55:57 -0800484
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500485 if (maskFilter->directFilterMaskGPU(rContext, surfaceDrawContext, std::move(paint), clip,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400486 viewMatrix, *shape)) {
Robert Phillipsb889f202018-08-17 19:55:59 +0000487 // the mask filter was able to draw itself directly, so there's nothing
488 // left to do.
489 return;
490 }
Mike Klein16885072018-12-11 09:54:31 -0500491 assert_alive(paint);
Robert Phillipsb889f202018-08-17 19:55:59 +0000492
Robert Phillips27927a52018-08-20 13:18:12 -0400493 // If the path is hairline, ignore inverse fill.
494 bool inverseFilled = shape->inverseFilled() &&
Robert Phillips62214f72021-06-15 10:12:51 -0400495 !GrIsStrokeHairlineOrEquivalent(shape->style(), viewMatrix, nullptr);
Robert Phillips27927a52018-08-20 13:18:12 -0400496
497 SkIRect unclippedDevShapeBounds, devClipBounds;
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500498 if (!get_shape_and_clip_bounds(surfaceDrawContext, clip, *shape, viewMatrix,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400499 &unclippedDevShapeBounds, &devClipBounds)) {
Robert Phillips27927a52018-08-20 13:18:12 -0400500 // TODO: just cons up an opaque mask here
501 if (!inverseFilled) {
502 return;
503 }
504 }
505
Robert Phillips31c080b2018-08-23 10:45:32 -0400506 GrUniqueKey maskKey;
Robert Phillips52c17c42020-10-05 11:55:59 -0400507 SkIRect boundsForClip;
508 if (!compute_key_and_clip_bounds(&maskKey, &boundsForClip,
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500509 surfaceDrawContext->caps(),
Robert Phillips52c17c42020-10-05 11:55:59 -0400510 viewMatrix, inverseFilled,
511 maskFilter, *shape,
512 unclippedDevShapeBounds,
513 devClipBounds)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400514 return; // 'shape' was entirely clipped out
Robert Phillips31c080b2018-08-23 10:45:32 -0400515 }
516
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400517 GrSurfaceProxyView filteredMaskView;
Robert Phillips31c080b2018-08-23 10:45:32 -0400518 SkIRect maskRect;
bsalomonc55271f2015-11-09 11:55:57 -0800519
Robert Phillipsfde67e42020-10-07 15:33:43 -0400520 if (auto dContext = rContext->asDirectContext()) {
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500521 filteredMaskView = hw_create_filtered_mask(dContext, surfaceDrawContext,
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400522 viewMatrix, *shape, maskFilter,
523 unclippedDevShapeBounds, boundsForClip,
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400524 &maskRect, &maskKey);
Brian Salomond005b692020-04-01 15:47:05 -0400525 if (filteredMaskView) {
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500526 if (draw_mask(surfaceDrawContext, clip, viewMatrix, maskRect, std::move(paint),
Greg Daniel5c082492020-01-29 15:06:49 -0500527 std::move(filteredMaskView))) {
Robert Phillips20390c32018-08-17 11:01:03 -0400528 // This path is completely drawn
529 return;
530 }
Mike Klein16885072018-12-11 09:54:31 -0500531 assert_alive(paint);
Robert Phillips20390c32018-08-17 11:01:03 -0400532 }
bsalomonc55271f2015-11-09 11:55:57 -0800533 }
534
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400535 // Either HW mask rendering failed or we're in a DDL recording thread
536 filteredMaskView = sw_create_filtered_mask(rContext,
537 viewMatrix, *shape, maskFilter,
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400538 unclippedDevShapeBounds, boundsForClip,
539 &maskRect, &maskKey);
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400540 if (filteredMaskView) {
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500541 if (draw_mask(surfaceDrawContext, clip, viewMatrix, maskRect, std::move(paint),
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400542 std::move(filteredMaskView))) {
543 return;
544 }
545 assert_alive(paint);
546 }
bsalomon6663acf2016-05-10 09:14:17 -0700547}
548
Robert Phillips7af8fe52019-02-14 17:27:00 -0500549void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext* context,
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500550 GrSurfaceDrawContext* surfaceDrawContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400551 const GrClip* clip,
Michael Ludwig2686d692020-04-17 20:21:37 +0000552 const GrStyledShape& shape,
Robert Phillips27927a52018-08-20 13:18:12 -0400553 GrPaint&& paint,
Robert Phillips27927a52018-08-20 13:18:12 -0400554 const SkMatrix& viewMatrix,
555 const SkMaskFilter* mf) {
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500556 draw_shape_with_mask_filter(context, surfaceDrawContext, clip, std::move(paint),
Robert Phillips27927a52018-08-20 13:18:12 -0400557 viewMatrix, as_MFB(mf), shape);
bsalomonc55271f2015-11-09 11:55:57 -0800558}
559
Robert Phillips69893702019-02-22 11:16:30 -0500560void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext* context,
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500561 GrSurfaceDrawContext* surfaceDrawContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400562 const GrClip* clip,
Robert Phillips27927a52018-08-20 13:18:12 -0400563 const SkPaint& paint,
Brian Osman449b1152020-04-15 16:43:00 -0400564 const SkMatrixProvider& matrixProvider,
Michael Ludwig2686d692020-04-17 20:21:37 +0000565 const GrStyledShape& shape) {
Robert Phillips9eb00022020-06-30 15:30:12 -0400566 if (context->abandoned()) {
Robert Phillipsbebfd412018-03-08 11:07:23 -0500567 return;
568 }
569
robertphillipsccb1b572015-05-27 11:02:55 -0700570 GrPaint grPaint;
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500571 if (!SkPaintToGrPaint(context, surfaceDrawContext->colorInfo(), paint, matrixProvider,
Brian Osman449b1152020-04-15 16:43:00 -0400572 &grPaint)) {
robertphillipsccb1b572015-05-27 11:02:55 -0700573 return;
574 }
Robert Phillips27927a52018-08-20 13:18:12 -0400575
Brian Osman449b1152020-04-15 16:43:00 -0400576 const SkMatrix& viewMatrix(matrixProvider.localToDevice());
Mike Reed80747ef2018-01-23 15:29:32 -0500577 SkMaskFilterBase* mf = as_MFB(paint.getMaskFilter());
Mike Reedbfadcf02018-01-20 22:24:21 +0000578 if (mf && !mf->hasFragmentProcessor()) {
Robert Phillipsa29a9562016-10-20 09:40:55 -0400579 // The MaskFilter wasn't already handled in SkPaintToGrPaint
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500580 draw_shape_with_mask_filter(context, surfaceDrawContext, clip, std::move(grPaint),
Robert Phillips27927a52018-08-20 13:18:12 -0400581 viewMatrix, mf, shape);
bsalomonc55271f2015-11-09 11:55:57 -0800582 } else {
Chris Dalton94df5722021-04-19 17:40:40 -0600583 surfaceDrawContext->drawShape(clip, std::move(grPaint), surfaceDrawContext->chooseAA(paint),
Chris Daltonfd708652021-03-17 21:10:17 -0600584 viewMatrix, GrStyledShape(shape));
robertphillipsccb1b572015-05-27 11:02:55 -0700585 }
robertphillipsccb1b572015-05-27 11:02:55 -0700586}
Robert Phillips11bc3052021-06-21 09:25:11 -0400587
588#else // GR_OGA
589
590void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext* context,
591 GrSurfaceDrawContext* surfaceDrawContext,
592 const GrClip* clip,
593 const GrStyledShape& shape,
594 GrPaint&& paint,
595 const SkMatrix& viewMatrix,
596 const SkMaskFilter* mf) {
597}
598
599void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext* context,
600 GrSurfaceDrawContext* surfaceDrawContext,
601 const GrClip* clip,
602 const SkPaint& paint,
603 const SkMatrixProvider& matrixProvider,
604 const GrStyledShape& shape) {
605}
606
607#endif // GR_OGA