blob: cc76998a8d1b89a7637d7514d6b0df6d9d20c5b0 [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"
Greg Daniel6f5441a2020-01-28 17:02:49 -050012#include "src/gpu/GrBitmapTextureMaker.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrCaps.h"
Adlai Hollera0693042020-10-14 11:23:11 -040014#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrFixedClip.h"
16#include "src/gpu/GrProxyProvider.h"
17#include "src/gpu/GrRecordingContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrSoftwarePathRenderer.h"
19#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"
Brian Salomonb8f098d2020-01-07 11:15:44 -050023#include "src/gpu/effects/GrTextureEffect.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000024#include "src/gpu/geometry/GrStyledShape.h"
Robert Phillipsc7c2baf2018-03-08 09:51:04 -050025
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "include/core/SkPaint.h"
27#include "src/core/SkDraw.h"
28#include "src/core/SkMaskFilterBase.h"
Brian Osman449b1152020-04-15 16:43:00 -040029#include "src/core/SkMatrixProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/core/SkTLazy.h"
31#include "src/gpu/SkGr.h"
robertphillipsccb1b572015-05-27 11:02:55 -070032
33static bool clip_bounds_quick_reject(const SkIRect& clipBounds, const SkIRect& rect) {
34 return clipBounds.isEmpty() || rect.isEmpty() || !SkIRect::Intersects(clipBounds, rect);
35}
36
Brian Salomond005b692020-04-01 15:47:05 -040037static constexpr auto kMaskOrigin = kTopLeft_GrSurfaceOrigin;
38
robertphillipsccb1b572015-05-27 11:02:55 -070039// Draw a mask using the supplied paint. Since the coverage/geometry
40// is already burnt into the mask this boils down to a rect draw.
41// Return true if the mask was successfully drawn.
Brian Salomon1aa1f5f2020-12-11 17:25:17 -050042static bool draw_mask(GrSurfaceDrawContext* surfaceDrawContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -040043 const GrClip* clip,
robertphillipsccb1b572015-05-27 11:02:55 -070044 const SkMatrix& viewMatrix,
robertphillipsf054b172016-05-13 05:06:19 -070045 const SkIRect& maskRect,
Brian Salomon82f44312017-01-11 13:42:54 -050046 GrPaint&& paint,
Greg Daniel5c082492020-01-29 15:06:49 -050047 GrSurfaceProxyView mask) {
Brian Salomon82f44312017-01-11 13:42:54 -050048 SkMatrix inverse;
49 if (!viewMatrix.invert(&inverse)) {
50 return false;
51 }
Robert Phillips4a24da52016-12-14 09:00:07 -050052
Mike Reed1f607332020-05-21 12:11:27 -040053 SkMatrix matrix = SkMatrix::Translate(-SkIntToScalar(maskRect.fLeft),
Robert Phillips67c18d62017-01-20 12:44:06 -050054 -SkIntToScalar(maskRect.fTop));
Brian Salomon2ebd0c82016-10-03 17:15:28 -040055 matrix.preConcat(viewMatrix);
John Stiles41d91b62020-07-21 14:39:40 -040056 paint.setCoverageFragmentProcessor(
Greg Danield2ccbb52020-02-05 10:45:39 -050057 GrTextureEffect::Make(std::move(mask), kUnknown_SkAlphaType, matrix));
robertphillipsccb1b572015-05-27 11:02:55 -070058
Brian Salomon1aa1f5f2020-12-11 17:25:17 -050059 surfaceDrawContext->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(),
60 SkRect::Make(maskRect), inverse);
robertphillipsccb1b572015-05-27 11:02:55 -070061 return true;
62}
63
Robert Phillipsc7c2baf2018-03-08 09:51:04 -050064static void mask_release_proc(void* addr, void* /*context*/) {
65 SkMask::FreeImage(addr);
66}
67
Robert Phillips6e17ffe2020-10-06 14:52:11 -040068// This stores the mapping from an unclipped, integerized, device-space, shape bounds to
69// the filtered mask's draw rect.
70struct DrawRectData {
71 SkIVector fOffset;
72 SkISize fSize;
73};
74
75static sk_sp<SkData> create_data(const SkIRect& drawRect, const SkIRect& origDevBounds) {
76
77 DrawRectData drawRectData { {drawRect.fLeft - origDevBounds.fLeft,
78 drawRect.fTop - origDevBounds.fTop},
79 drawRect.size() };
80
81 return SkData::MakeWithCopy(&drawRectData, sizeof(drawRectData));
82}
83
84static SkIRect extract_draw_rect_from_data(SkData* data, const SkIRect& origDevBounds) {
85 auto drawRectData = static_cast<const DrawRectData*>(data->data());
86
87 return SkIRect::MakeXYWH(origDevBounds.fLeft + drawRectData->fOffset.fX,
88 origDevBounds.fTop + drawRectData->fOffset.fY,
89 drawRectData->fSize.fWidth,
90 drawRectData->fSize.fHeight);
91}
92
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -040093static GrSurfaceProxyView sw_create_filtered_mask(GrRecordingContext* rContext,
94 const SkMatrix& viewMatrix,
95 const GrStyledShape& shape,
96 const SkMaskFilter* filter,
Robert Phillips6e17ffe2020-10-06 14:52:11 -040097 const SkIRect& unclippedDevShapeBounds,
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -040098 const SkIRect& clipBounds,
99 SkIRect* drawRect,
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400100 GrUniqueKey* key) {
Robert Phillips27927a52018-08-20 13:18:12 -0400101 SkASSERT(filter);
102 SkASSERT(!shape.style().applies());
103
Robert Phillipsd464feb2020-10-08 11:00:02 -0400104 auto threadSafeCache = rContext->priv().threadSafeCache();
Robert Phillips27927a52018-08-20 13:18:12 -0400105
Greg Daniel5c082492020-01-29 15:06:49 -0500106 GrSurfaceProxyView filteredMaskView;
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400107 sk_sp<SkData> data;
Robert Phillips27927a52018-08-20 13:18:12 -0400108
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400109 if (key->isValid()) {
Robert Phillipsd464feb2020-10-08 11:00:02 -0400110 std::tie(filteredMaskView, data) = threadSafeCache->findWithData(*key);
robertphillipsccb1b572015-05-27 11:02:55 -0700111 }
112
Brian Salomond005b692020-04-01 15:47:05 -0400113 if (filteredMaskView) {
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400114 SkASSERT(data);
Robert Phillipsc73c1af2020-09-29 13:47:39 -0400115 SkASSERT(kMaskOrigin == filteredMaskView.origin());
116
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400117 *drawRect = extract_draw_rect_from_data(data.get(), unclippedDevShapeBounds);
Robert Phillips31c080b2018-08-23 10:45:32 -0400118 } else {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400119 SkStrokeRec::InitStyle fillOrHairline = shape.style().isSimpleHairline()
120 ? SkStrokeRec::kHairline_InitStyle
121 : SkStrokeRec::kFill_InitStyle;
122
Robert Phillips31c080b2018-08-23 10:45:32 -0400123 // TODO: it seems like we could create an SkDraw here and set its fMatrix field rather
124 // than explicitly transforming the path to device space.
125 SkPath devPath;
126
127 shape.asPath(&devPath);
128
129 devPath.transform(viewMatrix);
130
131 SkMask srcM, dstM;
132 if (!SkDraw::DrawToMask(devPath, &clipBounds, filter, &viewMatrix, &srcM,
133 SkMask::kComputeBoundsAndRenderImage_CreateMode, fillOrHairline)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400134 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400135 }
136 SkAutoMaskFreeImage autoSrc(srcM.fImage);
137
138 SkASSERT(SkMask::kA8_Format == srcM.fFormat);
139
140 if (!as_MFB(filter)->filterMask(&dstM, srcM, viewMatrix, nullptr)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400141 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400142 }
143 // this will free-up dstM when we're done (allocated in filterMask())
144 SkAutoMaskFreeImage autoDst(dstM.fImage);
145
146 if (clip_bounds_quick_reject(clipBounds, dstM.fBounds)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400147 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400148 }
149
150 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
151 // the current clip (and identity matrix) and GrPaint settings
152 SkBitmap bm;
153 if (!bm.installPixels(SkImageInfo::MakeA8(dstM.fBounds.width(), dstM.fBounds.height()),
154 autoDst.release(), dstM.fRowBytes, mask_release_proc, nullptr)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400155 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400156 }
157 bm.setImmutable();
158
Robert Phillipsc73c1af2020-09-29 13:47:39 -0400159 GrBitmapTextureMaker maker(rContext, bm, SkBackingFit::kApprox);
Brian Salomon7e67dca2020-07-21 09:27:25 -0400160 filteredMaskView = maker.view(GrMipmapped::kNo);
Greg Danielcc104db2020-02-03 14:17:08 -0500161 if (!filteredMaskView.proxy()) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400162 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400163 }
164
Brian Salomond005b692020-04-01 15:47:05 -0400165 SkASSERT(kMaskOrigin == filteredMaskView.origin());
Robert Phillips59b39e72018-08-30 11:51:25 -0400166
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400167 *drawRect = dstM.fBounds;
Robert Phillips31c080b2018-08-23 10:45:32 -0400168
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400169 if (key->isValid()) {
170 key->setCustomData(create_data(*drawRect, unclippedDevShapeBounds));
Robert Phillipsd464feb2020-10-08 11:00:02 -0400171 std::tie(filteredMaskView, data) = threadSafeCache->addWithData(*key, filteredMaskView);
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400172 // If we got a different view back from 'addWithData' it could have a different drawRect
173 *drawRect = extract_draw_rect_from_data(data.get(), unclippedDevShapeBounds);
Robert Phillips31c080b2018-08-23 10:45:32 -0400174 }
Robert Phillips48ce22b2018-03-23 10:33:12 -0400175 }
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500176
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400177 return filteredMaskView;
robertphillipsccb1b572015-05-27 11:02:55 -0700178}
179
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500180// Create a mask of 'shape' and return the resulting surfaceDrawContext
Brian Salomoneebe7352020-12-09 16:37:04 -0500181static std::unique_ptr<GrSurfaceDrawContext> create_mask_GPU(GrRecordingContext* context,
182 const SkIRect& maskRect,
183 const SkMatrix& origViewMatrix,
184 const GrStyledShape& shape,
185 int sampleCnt) {
Chris Dalton0493fbd2019-09-18 15:49:46 -0600186 // Use GrResourceProvider::MakeApprox to implement our own approximate size matching, but demand
187 // a "SkBackingFit::kExact" size match on the actual render target. We do this because the
188 // filter will reach outside the src bounds, so we need to pre-clear these values to ensure a
189 // "decal" sampling effect (i.e., ensure reads outside the src bounds return alpha=0).
190 //
191 // FIXME: Reads outside the left and top edges will actually clamp to the edge pixel. And in the
192 // event that MakeApprox does not change the size, reads outside the right and/or bottom will do
193 // the same. We should offset our filter within the render target and expand the size as needed
194 // to guarantee at least 1px of padding on all sides.
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400195 auto approxSize = GrResourceProvider::MakeApprox(maskRect.size());
Brian Salomoneebe7352020-12-09 16:37:04 -0500196 auto rtContext = GrSurfaceDrawContext::MakeWithFallback(
Greg Daniele20fcad2020-01-08 11:52:34 -0500197 context, GrColorType::kAlpha_8, nullptr, SkBackingFit::kExact, approxSize, sampleCnt,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400198 GrMipmapped::kNo, GrProtected::kNo, kMaskOrigin);
robertphillipsd728f0c2016-11-21 11:05:03 -0800199 if (!rtContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700200 return nullptr;
robertphillipsccb1b572015-05-27 11:02:55 -0700201 }
202
Chris Dalton0493fbd2019-09-18 15:49:46 -0600203 rtContext->clear(SK_PMColor4fTRANSPARENT);
robertphillipsccb1b572015-05-27 11:02:55 -0700204
Brian Salomon82f44312017-01-11 13:42:54 -0500205 GrPaint maskPaint;
206 maskPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op);
robertphillipsccb1b572015-05-27 11:02:55 -0700207
208 // setup new clip
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400209 GrFixedClip clip(rtContext->dimensions(), SkIRect::MakeWH(maskRect.width(), maskRect.height()));
robertphillipsccb1b572015-05-27 11:02:55 -0700210
Brian Salomond005b692020-04-01 15:47:05 -0400211 // Draw the mask into maskTexture with the path's integerized top-left at the origin using
212 // maskPaint.
Robert Phillips27927a52018-08-20 13:18:12 -0400213 SkMatrix viewMatrix = origViewMatrix;
214 viewMatrix.postTranslate(-SkIntToScalar(maskRect.fLeft), -SkIntToScalar(maskRect.fTop));
Michael Ludwig7c12e282020-05-29 09:54:07 -0400215 rtContext->drawShape(&clip, std::move(maskPaint), GrAA::kYes, viewMatrix, shape);
Robert Phillips40b05c32019-09-20 12:40:55 -0400216 return rtContext;
robertphillipsccb1b572015-05-27 11:02:55 -0700217}
218
Michael Ludwig2686d692020-04-17 20:21:37 +0000219static bool get_unclipped_shape_dev_bounds(const GrStyledShape& shape, const SkMatrix& matrix,
Robert Phillips27927a52018-08-20 13:18:12 -0400220 SkIRect* devBounds) {
221 SkRect shapeBounds = shape.styledBounds();
222 if (shapeBounds.isEmpty()) {
223 return false;
224 }
225 SkRect shapeDevBounds;
226 matrix.mapRect(&shapeDevBounds, shapeBounds);
227 // Even though these are "unclipped" bounds we still clip to the int32_t range.
228 // This is the largest int32_t that is representable exactly as a float. The next 63 larger ints
229 // would round down to this value when cast to a float, but who really cares.
230 // INT32_MIN is exactly representable.
231 static constexpr int32_t kMaxInt = 2147483520;
232 if (!shapeDevBounds.intersect(SkRect::MakeLTRB(INT32_MIN, INT32_MIN, kMaxInt, kMaxInt))) {
233 return false;
234 }
235 // Make sure that the resulting SkIRect can have representable width and height
236 if (SkScalarRoundToInt(shapeDevBounds.width()) > kMaxInt ||
237 SkScalarRoundToInt(shapeDevBounds.height()) > kMaxInt) {
238 return false;
239 }
240 shapeDevBounds.roundOut(devBounds);
241 return true;
242}
bsalomonc55271f2015-11-09 11:55:57 -0800243
Robert Phillips27927a52018-08-20 13:18:12 -0400244// Gets the shape bounds, the clip bounds, and the intersection (if any). Returns false if there
245// is no intersection.
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500246static bool get_shape_and_clip_bounds(GrSurfaceDrawContext* surfaceDrawContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400247 const GrClip* clip,
Michael Ludwig2686d692020-04-17 20:21:37 +0000248 const GrStyledShape& shape,
Robert Phillips27927a52018-08-20 13:18:12 -0400249 const SkMatrix& matrix,
250 SkIRect* unclippedDevShapeBounds,
251 SkIRect* devClipBounds) {
252 // compute bounds as intersection of rt size, clip, and path
Michael Ludwige06a8972020-06-11 10:29:00 -0400253 *devClipBounds = clip ? clip->getConservativeBounds()
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500254 : SkIRect::MakeWH(surfaceDrawContext->width(),
255 surfaceDrawContext->height());
bsalomonc55271f2015-11-09 11:55:57 -0800256
Robert Phillips27927a52018-08-20 13:18:12 -0400257 if (!get_unclipped_shape_dev_bounds(shape, matrix, unclippedDevShapeBounds)) {
Brian Salomon44207f32020-01-06 15:20:18 -0500258 *unclippedDevShapeBounds = SkIRect::MakeEmpty();
Robert Phillips27927a52018-08-20 13:18:12 -0400259 return false;
bsalomonc55271f2015-11-09 11:55:57 -0800260 }
261
Robert Phillips27927a52018-08-20 13:18:12 -0400262 return true;
263}
264
Robert Phillips52c17c42020-10-05 11:55:59 -0400265// The key and clip-bounds are computed together because the caching decision can impact the
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400266// clip-bound - since we only cache un-clipped masks the clip can be removed entirely.
Robert Phillips52c17c42020-10-05 11:55:59 -0400267// A 'false' return value indicates that the shape is known to be clipped away.
268static bool compute_key_and_clip_bounds(GrUniqueKey* maskKey,
269 SkIRect* boundsForClip,
270 const GrCaps* caps,
271 const SkMatrix& viewMatrix,
272 bool inverseFilled,
273 const SkMaskFilterBase* maskFilter,
274 const GrStyledShape& shape,
275 const SkIRect& unclippedDevShapeBounds,
276 const SkIRect& devClipBounds) {
277 *boundsForClip = devClipBounds;
278
279#ifndef SK_DISABLE_MASKFILTERED_MASK_CACHING
280 // To prevent overloading the cache with entries during animations we limit the cache of masks
281 // to cases where the matrix preserves axis alignment.
282 bool useCache = !inverseFilled && viewMatrix.preservesAxisAlignment() &&
283 shape.hasUnstyledKey() && as_MFB(maskFilter)->asABlur(nullptr);
284
285 if (useCache) {
286 SkIRect clippedMaskRect, unClippedMaskRect;
287 maskFilter->canFilterMaskGPU(shape, unclippedDevShapeBounds, devClipBounds,
288 viewMatrix, &clippedMaskRect);
289 maskFilter->canFilterMaskGPU(shape, unclippedDevShapeBounds, unclippedDevShapeBounds,
290 viewMatrix, &unClippedMaskRect);
291 if (clippedMaskRect.isEmpty()) {
292 return false;
293 }
294
295 // Use the cache only if >50% of the filtered mask is visible.
296 int unclippedWidth = unClippedMaskRect.width();
297 int unclippedHeight = unClippedMaskRect.height();
298 int64_t unclippedArea = sk_64_mul(unclippedWidth, unclippedHeight);
299 int64_t clippedArea = sk_64_mul(clippedMaskRect.width(), clippedMaskRect.height());
300 int maxTextureSize = caps->maxTextureSize();
301 if (unclippedArea > 2 * clippedArea || unclippedWidth > maxTextureSize ||
302 unclippedHeight > maxTextureSize) {
303 useCache = false;
304 } else {
305 // Make the clip not affect the mask
306 *boundsForClip = unclippedDevShapeBounds;
307 }
308 }
309
310 if (useCache) {
311 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
312 GrUniqueKey::Builder builder(maskKey, kDomain, 5 + 2 + shape.unstyledKeySize(),
313 "Mask Filtered Masks");
314
315 // We require the upper left 2x2 of the matrix to match exactly for a cache hit.
316 SkScalar sx = viewMatrix.get(SkMatrix::kMScaleX);
317 SkScalar sy = viewMatrix.get(SkMatrix::kMScaleY);
318 SkScalar kx = viewMatrix.get(SkMatrix::kMSkewX);
319 SkScalar ky = viewMatrix.get(SkMatrix::kMSkewY);
320 SkScalar tx = viewMatrix.get(SkMatrix::kMTransX);
321 SkScalar ty = viewMatrix.get(SkMatrix::kMTransY);
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400322 // Allow 8 bits each in x and y of subpixel positioning. But, note that we're allowing
323 // reuse for integer translations.
Robert Phillips52c17c42020-10-05 11:55:59 -0400324 SkFixed fracX = SkScalarToFixed(SkScalarFraction(tx)) & 0x0000FF00;
325 SkFixed fracY = SkScalarToFixed(SkScalarFraction(ty)) & 0x0000FF00;
326
327 builder[0] = SkFloat2Bits(sx);
328 builder[1] = SkFloat2Bits(sy);
329 builder[2] = SkFloat2Bits(kx);
330 builder[3] = SkFloat2Bits(ky);
331 // Distinguish between hairline and filled paths. For hairlines, we also need to include
332 // the cap. (SW grows hairlines by 0.5 pixel with round and square caps). Note that
333 // stroke-and-fill of hairlines is turned into pure fill by SkStrokeRec, so this covers
334 // all cases we might see.
335 uint32_t styleBits = shape.style().isSimpleHairline()
336 ? ((shape.style().strokeRec().getCap() << 1) | 1)
337 : 0;
338 builder[4] = fracX | (fracY >> 8) | (styleBits << 16);
339
340 SkMaskFilterBase::BlurRec rec;
341 SkAssertResult(as_MFB(maskFilter)->asABlur(&rec));
342
343 builder[5] = rec.fStyle; // TODO: we could put this with the other style bits
344 builder[6] = SkFloat2Bits(rec.fSigma);
345 shape.writeUnstyledKey(&builder[7]);
346 }
347#endif
348
349 return true;
350}
351
Robert Phillipsfde67e42020-10-07 15:33:43 -0400352static GrSurfaceProxyView hw_create_filtered_mask(GrDirectContext* dContext,
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500353 GrSurfaceDrawContext* surfaceDrawContext,
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400354 const SkMatrix& viewMatrix,
355 const GrStyledShape& shape,
356 const SkMaskFilterBase* filter,
357 const SkIRect& unclippedDevShapeBounds,
358 const SkIRect& clipBounds,
359 SkIRect* maskRect,
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400360 GrUniqueKey* key) {
Robert Phillipsfde67e42020-10-07 15:33:43 -0400361 if (!filter->canFilterMaskGPU(shape,
362 unclippedDevShapeBounds,
363 clipBounds,
364 viewMatrix,
365 maskRect)) {
366 return {};
367 }
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400368
Robert Phillipsfde67e42020-10-07 15:33:43 -0400369 if (clip_bounds_quick_reject(clipBounds, *maskRect)) {
370 // clipped out
371 return {};
372 }
373
Robert Phillipsd464feb2020-10-08 11:00:02 -0400374 auto threadSafeCache = dContext->priv().threadSafeCache();
Robert Phillipsfde67e42020-10-07 15:33:43 -0400375
376 GrSurfaceProxyView lazyView;
Robert Phillipsd464feb2020-10-08 11:00:02 -0400377 sk_sp<GrThreadSafeCache::Trampoline> trampoline;
Robert Phillipsfde67e42020-10-07 15:33:43 -0400378
379 if (key->isValid()) {
380 // In this case, we want GPU-filtered masks to have priority over SW-generated ones so
381 // we pre-emptively add a lazy-view to the cache and fill it in later.
Robert Phillipsd464feb2020-10-08 11:00:02 -0400382 std::tie(lazyView, trampoline) = GrThreadSafeCache::CreateLazyView(
Robert Phillipsfde67e42020-10-07 15:33:43 -0400383 dContext, GrColorType::kAlpha_8, maskRect->size(),
384 kMaskOrigin, SkBackingFit::kApprox);
385 if (!lazyView) {
386 return {}; // fall back to a SW-created mask - 'create_mask_GPU' probably won't succeed
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400387 }
388
Robert Phillipsfde67e42020-10-07 15:33:43 -0400389 key->setCustomData(create_data(*maskRect, unclippedDevShapeBounds));
Robert Phillipsd464feb2020-10-08 11:00:02 -0400390 auto [cachedView, data] = threadSafeCache->findOrAddWithData(*key, lazyView);
Robert Phillipsfde67e42020-10-07 15:33:43 -0400391 if (cachedView != lazyView) {
392 // In this case, the gpu-thread lost out to a recording thread - use its result.
393 SkASSERT(data);
394 SkASSERT(cachedView.asTextureProxy());
395 SkASSERT(cachedView.origin() == kMaskOrigin);
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400396
Robert Phillipsfde67e42020-10-07 15:33:43 -0400397 *maskRect = extract_draw_rect_from_data(data.get(), unclippedDevShapeBounds);
398 return cachedView;
399 }
400 }
401
Brian Salomoneebe7352020-12-09 16:37:04 -0500402 std::unique_ptr<GrSurfaceDrawContext> maskRTC(create_mask_GPU(
Robert Phillipsfde67e42020-10-07 15:33:43 -0400403 dContext,
404 *maskRect,
405 viewMatrix,
406 shape,
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500407 surfaceDrawContext->numSamples()));
Robert Phillipsfde67e42020-10-07 15:33:43 -0400408 if (!maskRTC) {
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400409 if (key->isValid()) {
Robert Phillipsfde67e42020-10-07 15:33:43 -0400410 // It is very unlikely that 'create_mask_GPU' will fail after 'CreateLazyView'
411 // succeeded but, if it does, remove the lazy-view from the cache and fallback to
412 // a SW-created mask. Note that any recording threads that glommed onto the
413 // lazy-view will have to, later, drop those draws.
Robert Phillipsd464feb2020-10-08 11:00:02 -0400414 threadSafeCache->remove(*key);
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400415 }
Robert Phillipsfde67e42020-10-07 15:33:43 -0400416 return {};
417 }
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400418
Robert Phillipsfde67e42020-10-07 15:33:43 -0400419 auto filteredMaskView = filter->filterMaskGPU(dContext,
420 maskRTC->readSurfaceView(),
421 maskRTC->colorInfo().colorType(),
422 maskRTC->colorInfo().alphaType(),
423 viewMatrix,
424 *maskRect);
425 if (!filteredMaskView) {
426 if (key->isValid()) {
427 // Remove the lazy-view from the cache and fallback to a SW-created mask. Note that
428 // any recording threads that glommed onto the lazy-view will have to, later, drop
429 // those draws.
Robert Phillipsd464feb2020-10-08 11:00:02 -0400430 threadSafeCache->remove(*key);
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400431 }
Robert Phillipsfde67e42020-10-07 15:33:43 -0400432 return {};
433 }
434
435 if (key->isValid()) {
436 SkASSERT(filteredMaskView.dimensions() == lazyView.dimensions());
437 SkASSERT(filteredMaskView.swizzle() == lazyView.swizzle());
438 SkASSERT(filteredMaskView.origin() == lazyView.origin());
439
440 trampoline->fProxy = filteredMaskView.asTextureProxyRef();
441 return lazyView;
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400442 }
443
444 return filteredMaskView;
445}
446
447static void draw_shape_with_mask_filter(GrRecordingContext* rContext,
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500448 GrSurfaceDrawContext* surfaceDrawContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400449 const GrClip* clip,
Robert Phillips27927a52018-08-20 13:18:12 -0400450 GrPaint&& paint,
Robert Phillips27927a52018-08-20 13:18:12 -0400451 const SkMatrix& viewMatrix,
452 const SkMaskFilterBase* maskFilter,
Michael Ludwig2686d692020-04-17 20:21:37 +0000453 const GrStyledShape& origShape) {
Robert Phillips27927a52018-08-20 13:18:12 -0400454 SkASSERT(maskFilter);
455
Michael Ludwig2686d692020-04-17 20:21:37 +0000456 const GrStyledShape* shape = &origShape;
457 SkTLazy<GrStyledShape> tmpShape;
Robert Phillips27927a52018-08-20 13:18:12 -0400458
459 if (origShape.style().applies()) {
460 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
461 if (0 == styleScale) {
462 return;
bsalomon6663acf2016-05-10 09:14:17 -0700463 }
Robert Phillips27927a52018-08-20 13:18:12 -0400464
465 tmpShape.init(origShape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale));
John Stilesa008b0f2020-08-16 08:48:02 -0400466 if (tmpShape->isEmpty()) {
Robert Phillips27927a52018-08-20 13:18:12 -0400467 return;
468 }
469
470 shape = tmpShape.get();
bsalomon6663acf2016-05-10 09:14:17 -0700471 }
bsalomonc55271f2015-11-09 11:55:57 -0800472
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500473 if (maskFilter->directFilterMaskGPU(rContext, surfaceDrawContext, std::move(paint), clip,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400474 viewMatrix, *shape)) {
Robert Phillipsb889f202018-08-17 19:55:59 +0000475 // the mask filter was able to draw itself directly, so there's nothing
476 // left to do.
477 return;
478 }
Mike Klein16885072018-12-11 09:54:31 -0500479 assert_alive(paint);
Robert Phillipsb889f202018-08-17 19:55:59 +0000480
Robert Phillips27927a52018-08-20 13:18:12 -0400481 // If the path is hairline, ignore inverse fill.
482 bool inverseFilled = shape->inverseFilled() &&
483 !GrPathRenderer::IsStrokeHairlineOrEquivalent(shape->style(),
484 viewMatrix, nullptr);
485
486 SkIRect unclippedDevShapeBounds, devClipBounds;
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500487 if (!get_shape_and_clip_bounds(surfaceDrawContext, clip, *shape, viewMatrix,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400488 &unclippedDevShapeBounds, &devClipBounds)) {
Robert Phillips27927a52018-08-20 13:18:12 -0400489 // TODO: just cons up an opaque mask here
490 if (!inverseFilled) {
491 return;
492 }
493 }
494
Robert Phillips31c080b2018-08-23 10:45:32 -0400495 GrUniqueKey maskKey;
Robert Phillips52c17c42020-10-05 11:55:59 -0400496 SkIRect boundsForClip;
497 if (!compute_key_and_clip_bounds(&maskKey, &boundsForClip,
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500498 surfaceDrawContext->caps(),
Robert Phillips52c17c42020-10-05 11:55:59 -0400499 viewMatrix, inverseFilled,
500 maskFilter, *shape,
501 unclippedDevShapeBounds,
502 devClipBounds)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400503 return; // 'shape' was entirely clipped out
Robert Phillips31c080b2018-08-23 10:45:32 -0400504 }
505
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400506 GrSurfaceProxyView filteredMaskView;
Robert Phillips31c080b2018-08-23 10:45:32 -0400507 SkIRect maskRect;
bsalomonc55271f2015-11-09 11:55:57 -0800508
Robert Phillipsfde67e42020-10-07 15:33:43 -0400509 if (auto dContext = rContext->asDirectContext()) {
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500510 filteredMaskView = hw_create_filtered_mask(dContext, surfaceDrawContext,
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400511 viewMatrix, *shape, maskFilter,
512 unclippedDevShapeBounds, boundsForClip,
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400513 &maskRect, &maskKey);
Brian Salomond005b692020-04-01 15:47:05 -0400514 if (filteredMaskView) {
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500515 if (draw_mask(surfaceDrawContext, clip, viewMatrix, maskRect, std::move(paint),
Greg Daniel5c082492020-01-29 15:06:49 -0500516 std::move(filteredMaskView))) {
Robert Phillips20390c32018-08-17 11:01:03 -0400517 // This path is completely drawn
518 return;
519 }
Mike Klein16885072018-12-11 09:54:31 -0500520 assert_alive(paint);
Robert Phillips20390c32018-08-17 11:01:03 -0400521 }
bsalomonc55271f2015-11-09 11:55:57 -0800522 }
523
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400524 // Either HW mask rendering failed or we're in a DDL recording thread
525 filteredMaskView = sw_create_filtered_mask(rContext,
526 viewMatrix, *shape, maskFilter,
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400527 unclippedDevShapeBounds, boundsForClip,
528 &maskRect, &maskKey);
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400529 if (filteredMaskView) {
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500530 if (draw_mask(surfaceDrawContext, clip, viewMatrix, maskRect, std::move(paint),
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400531 std::move(filteredMaskView))) {
532 return;
533 }
534 assert_alive(paint);
535 }
bsalomon6663acf2016-05-10 09:14:17 -0700536}
537
Robert Phillips7af8fe52019-02-14 17:27:00 -0500538void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext* context,
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500539 GrSurfaceDrawContext* surfaceDrawContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400540 const GrClip* clip,
Michael Ludwig2686d692020-04-17 20:21:37 +0000541 const GrStyledShape& shape,
Robert Phillips27927a52018-08-20 13:18:12 -0400542 GrPaint&& paint,
Robert Phillips27927a52018-08-20 13:18:12 -0400543 const SkMatrix& viewMatrix,
544 const SkMaskFilter* mf) {
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500545 draw_shape_with_mask_filter(context, surfaceDrawContext, clip, std::move(paint),
Robert Phillips27927a52018-08-20 13:18:12 -0400546 viewMatrix, as_MFB(mf), shape);
bsalomonc55271f2015-11-09 11:55:57 -0800547}
548
Robert Phillips69893702019-02-22 11:16:30 -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,
Robert Phillips27927a52018-08-20 13:18:12 -0400552 const SkPaint& paint,
Brian Osman449b1152020-04-15 16:43:00 -0400553 const SkMatrixProvider& matrixProvider,
Michael Ludwig2686d692020-04-17 20:21:37 +0000554 const GrStyledShape& shape) {
Robert Phillips9eb00022020-06-30 15:30:12 -0400555 if (context->abandoned()) {
Robert Phillipsbebfd412018-03-08 11:07:23 -0500556 return;
557 }
558
robertphillipsccb1b572015-05-27 11:02:55 -0700559 GrPaint grPaint;
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500560 if (!SkPaintToGrPaint(context, surfaceDrawContext->colorInfo(), paint, matrixProvider,
Brian Osman449b1152020-04-15 16:43:00 -0400561 &grPaint)) {
robertphillipsccb1b572015-05-27 11:02:55 -0700562 return;
563 }
Robert Phillips27927a52018-08-20 13:18:12 -0400564
Brian Osman449b1152020-04-15 16:43:00 -0400565 const SkMatrix& viewMatrix(matrixProvider.localToDevice());
Mike Reed80747ef2018-01-23 15:29:32 -0500566 SkMaskFilterBase* mf = as_MFB(paint.getMaskFilter());
Mike Reedbfadcf02018-01-20 22:24:21 +0000567 if (mf && !mf->hasFragmentProcessor()) {
Robert Phillipsa29a9562016-10-20 09:40:55 -0400568 // The MaskFilter wasn't already handled in SkPaintToGrPaint
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500569 draw_shape_with_mask_filter(context, surfaceDrawContext, clip, std::move(grPaint),
Robert Phillips27927a52018-08-20 13:18:12 -0400570 viewMatrix, mf, shape);
bsalomonc55271f2015-11-09 11:55:57 -0800571 } else {
Robert Phillips793324c2018-08-24 13:53:56 -0400572 GrAA aa = GrAA(paint.isAntiAlias());
Brian Salomon1aa1f5f2020-12-11 17:25:17 -0500573 surfaceDrawContext->drawShape(clip, std::move(grPaint), aa, viewMatrix, shape);
robertphillipsccb1b572015-05-27 11:02:55 -0700574 }
robertphillipsccb1b572015-05-27 11:02:55 -0700575}