blob: 92e671753c2d7c3346c9bc85071824a615eb32a6 [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 Phillipsb7bfbc22020-07-01 12:55:01 -040010#include "include/gpu/GrRecordingContext.h"
Greg Daniel6f5441a2020-01-28 17:02:49 -050011#include "src/gpu/GrBitmapTextureMaker.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrCaps.h"
13#include "src/gpu/GrFixedClip.h"
14#include "src/gpu/GrProxyProvider.h"
15#include "src/gpu/GrRecordingContextPriv.h"
16#include "src/gpu/GrRenderTargetContext.h"
17#include "src/gpu/GrRenderTargetContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrSoftwarePathRenderer.h"
19#include "src/gpu/GrStyle.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040020#include "src/gpu/GrTextureProxy.h"
Robert Phillipsc73c1af2020-09-29 13:47:39 -040021#include "src/gpu/GrThreadSafeUniquelyKeyedProxyViewCache.h"
Brian Salomonb8f098d2020-01-07 11:15:44 -050022#include "src/gpu/effects/GrTextureEffect.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000023#include "src/gpu/geometry/GrStyledShape.h"
Robert Phillipsc7c2baf2018-03-08 09:51:04 -050024
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "include/core/SkPaint.h"
26#include "src/core/SkDraw.h"
27#include "src/core/SkMaskFilterBase.h"
Brian Osman449b1152020-04-15 16:43:00 -040028#include "src/core/SkMatrixProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/core/SkTLazy.h"
30#include "src/gpu/SkGr.h"
robertphillipsccb1b572015-05-27 11:02:55 -070031
32static bool clip_bounds_quick_reject(const SkIRect& clipBounds, const SkIRect& rect) {
33 return clipBounds.isEmpty() || rect.isEmpty() || !SkIRect::Intersects(clipBounds, rect);
34}
35
Brian Salomond005b692020-04-01 15:47:05 -040036static constexpr auto kMaskOrigin = kTopLeft_GrSurfaceOrigin;
37
38static GrSurfaceProxyView find_filtered_mask(GrProxyProvider* provider, const GrUniqueKey& key) {
Brian Salomon0029db02020-04-03 10:41:24 -040039 return provider->findCachedProxyWithColorTypeFallback(key, kMaskOrigin, GrColorType::kAlpha_8,
40 1);
Brian Salomond005b692020-04-01 15:47:05 -040041}
42
robertphillipsccb1b572015-05-27 11:02:55 -070043// Draw a mask using the supplied paint. Since the coverage/geometry
44// is already burnt into the mask this boils down to a rect draw.
45// Return true if the mask was successfully drawn.
Robert Phillips296b1cc2017-03-15 10:42:12 -040046static bool draw_mask(GrRenderTargetContext* renderTargetContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -040047 const GrClip* clip,
robertphillipsccb1b572015-05-27 11:02:55 -070048 const SkMatrix& viewMatrix,
robertphillipsf054b172016-05-13 05:06:19 -070049 const SkIRect& maskRect,
Brian Salomon82f44312017-01-11 13:42:54 -050050 GrPaint&& paint,
Greg Daniel5c082492020-01-29 15:06:49 -050051 GrSurfaceProxyView mask) {
Brian Salomon82f44312017-01-11 13:42:54 -050052 SkMatrix inverse;
53 if (!viewMatrix.invert(&inverse)) {
54 return false;
55 }
Robert Phillips4a24da52016-12-14 09:00:07 -050056
Mike Reed1f607332020-05-21 12:11:27 -040057 SkMatrix matrix = SkMatrix::Translate(-SkIntToScalar(maskRect.fLeft),
Robert Phillips67c18d62017-01-20 12:44:06 -050058 -SkIntToScalar(maskRect.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
Brian Salomon82f44312017-01-11 13:42:54 -050063 renderTargetContext->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050064 SkRect::Make(maskRect), inverse);
robertphillipsccb1b572015-05-27 11:02:55 -070065 return true;
66}
67
Robert Phillipsc7c2baf2018-03-08 09:51:04 -050068static void mask_release_proc(void* addr, void* /*context*/) {
69 SkMask::FreeImage(addr);
70}
71
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -040072static GrSurfaceProxyView sw_create_filtered_mask(GrRecordingContext* rContext,
73 const SkMatrix& viewMatrix,
74 const GrStyledShape& shape,
75 const SkMaskFilter* filter,
76 const SkIRect& clipBounds,
77 SkIRect* drawRect,
78 const GrUniqueKey& key) {
Robert Phillips27927a52018-08-20 13:18:12 -040079 SkASSERT(filter);
80 SkASSERT(!shape.style().applies());
81
Robert Phillipsc73c1af2020-09-29 13:47:39 -040082 auto threadSafeViewCache = rContext->priv().threadSafeViewCache();
Robert Phillips27927a52018-08-20 13:18:12 -040083
Greg Daniel5c082492020-01-29 15:06:49 -050084 GrSurfaceProxyView filteredMaskView;
Robert Phillips27927a52018-08-20 13:18:12 -040085
Robert Phillips31c080b2018-08-23 10:45:32 -040086 if (key.isValid()) {
Robert Phillipsc73c1af2020-09-29 13:47:39 -040087 filteredMaskView = threadSafeViewCache->find(key);
robertphillipsccb1b572015-05-27 11:02:55 -070088 }
89
Brian Salomond005b692020-04-01 15:47:05 -040090 if (filteredMaskView) {
Robert Phillipsc73c1af2020-09-29 13:47:39 -040091 SkASSERT(kMaskOrigin == filteredMaskView.origin());
92
Robert Phillips31c080b2018-08-23 10:45:32 -040093 SkRect devBounds = shape.bounds();
94 viewMatrix.mapRect(&devBounds);
Robert Phillips4a24da52016-12-14 09:00:07 -050095
Robert Phillips31c080b2018-08-23 10:45:32 -040096 // Here we need to recompute the destination bounds in order to draw the mask correctly
97 SkMask srcM, dstM;
98 if (!SkDraw::ComputeMaskBounds(devBounds, &clipBounds, filter, &viewMatrix,
99 &srcM.fBounds)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400100 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400101 }
102
103 srcM.fFormat = SkMask::kA8_Format;
104
105 if (!as_MFB(filter)->filterMask(&dstM, srcM, viewMatrix, nullptr)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400106 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400107 }
108
109 // Unfortunately, we cannot double check that the computed bounds (i.e., dstM.fBounds)
110 // match the stored bounds of the mask bc the proxy may have been recreated and,
111 // when it is recreated, it just gets the bounds of the underlying GrTexture (which
112 // might be a loose fit).
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400113 *drawRect = dstM.fBounds;
Robert Phillips31c080b2018-08-23 10:45:32 -0400114 } else {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400115 SkStrokeRec::InitStyle fillOrHairline = shape.style().isSimpleHairline()
116 ? SkStrokeRec::kHairline_InitStyle
117 : SkStrokeRec::kFill_InitStyle;
118
Robert Phillips31c080b2018-08-23 10:45:32 -0400119 // TODO: it seems like we could create an SkDraw here and set its fMatrix field rather
120 // than explicitly transforming the path to device space.
121 SkPath devPath;
122
123 shape.asPath(&devPath);
124
125 devPath.transform(viewMatrix);
126
127 SkMask srcM, dstM;
128 if (!SkDraw::DrawToMask(devPath, &clipBounds, filter, &viewMatrix, &srcM,
129 SkMask::kComputeBoundsAndRenderImage_CreateMode, fillOrHairline)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400130 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400131 }
132 SkAutoMaskFreeImage autoSrc(srcM.fImage);
133
134 SkASSERT(SkMask::kA8_Format == srcM.fFormat);
135
136 if (!as_MFB(filter)->filterMask(&dstM, srcM, viewMatrix, nullptr)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400137 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400138 }
139 // this will free-up dstM when we're done (allocated in filterMask())
140 SkAutoMaskFreeImage autoDst(dstM.fImage);
141
142 if (clip_bounds_quick_reject(clipBounds, dstM.fBounds)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400143 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400144 }
145
146 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
147 // the current clip (and identity matrix) and GrPaint settings
148 SkBitmap bm;
149 if (!bm.installPixels(SkImageInfo::MakeA8(dstM.fBounds.width(), dstM.fBounds.height()),
150 autoDst.release(), dstM.fRowBytes, mask_release_proc, nullptr)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400151 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400152 }
153 bm.setImmutable();
154
Robert Phillipsc73c1af2020-09-29 13:47:39 -0400155 GrBitmapTextureMaker maker(rContext, bm, SkBackingFit::kApprox);
Brian Salomon7e67dca2020-07-21 09:27:25 -0400156 filteredMaskView = maker.view(GrMipmapped::kNo);
Greg Danielcc104db2020-02-03 14:17:08 -0500157 if (!filteredMaskView.proxy()) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400158 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400159 }
160
Brian Salomond005b692020-04-01 15:47:05 -0400161 SkASSERT(kMaskOrigin == filteredMaskView.origin());
Robert Phillips59b39e72018-08-30 11:51:25 -0400162
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400163 *drawRect = dstM.fBounds;
Robert Phillips31c080b2018-08-23 10:45:32 -0400164
165 if (key.isValid()) {
Robert Phillipsc73c1af2020-09-29 13:47:39 -0400166 filteredMaskView = threadSafeViewCache->add(key, filteredMaskView);
Robert Phillips31c080b2018-08-23 10:45:32 -0400167 }
Robert Phillips48ce22b2018-03-23 10:33:12 -0400168 }
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500169
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400170 return filteredMaskView;
robertphillipsccb1b572015-05-27 11:02:55 -0700171}
172
Robert Phillips40b05c32019-09-20 12:40:55 -0400173// Create a mask of 'shape' and return the resulting renderTargetContext
174static std::unique_ptr<GrRenderTargetContext> create_mask_GPU(GrRecordingContext* context,
175 const SkIRect& maskRect,
176 const SkMatrix& origViewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +0000177 const GrStyledShape& shape,
Robert Phillips40b05c32019-09-20 12:40:55 -0400178 int sampleCnt) {
Chris Dalton0493fbd2019-09-18 15:49:46 -0600179 // Use GrResourceProvider::MakeApprox to implement our own approximate size matching, but demand
180 // a "SkBackingFit::kExact" size match on the actual render target. We do this because the
181 // filter will reach outside the src bounds, so we need to pre-clear these values to ensure a
182 // "decal" sampling effect (i.e., ensure reads outside the src bounds return alpha=0).
183 //
184 // FIXME: Reads outside the left and top edges will actually clamp to the edge pixel. And in the
185 // event that MakeApprox does not change the size, reads outside the right and/or bottom will do
186 // the same. We should offset our filter within the render target and expand the size as needed
187 // to guarantee at least 1px of padding on all sides.
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400188 auto approxSize = GrResourceProvider::MakeApprox(maskRect.size());
Greg Daniele20fcad2020-01-08 11:52:34 -0500189 auto rtContext = GrRenderTargetContext::MakeWithFallback(
190 context, GrColorType::kAlpha_8, nullptr, SkBackingFit::kExact, approxSize, sampleCnt,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400191 GrMipmapped::kNo, GrProtected::kNo, kMaskOrigin);
robertphillipsd728f0c2016-11-21 11:05:03 -0800192 if (!rtContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700193 return nullptr;
robertphillipsccb1b572015-05-27 11:02:55 -0700194 }
195
Chris Dalton0493fbd2019-09-18 15:49:46 -0600196 rtContext->clear(SK_PMColor4fTRANSPARENT);
robertphillipsccb1b572015-05-27 11:02:55 -0700197
Brian Salomon82f44312017-01-11 13:42:54 -0500198 GrPaint maskPaint;
199 maskPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op);
robertphillipsccb1b572015-05-27 11:02:55 -0700200
201 // setup new clip
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400202 GrFixedClip clip(rtContext->dimensions(), SkIRect::MakeWH(maskRect.width(), maskRect.height()));
robertphillipsccb1b572015-05-27 11:02:55 -0700203
Brian Salomond005b692020-04-01 15:47:05 -0400204 // Draw the mask into maskTexture with the path's integerized top-left at the origin using
205 // maskPaint.
Robert Phillips27927a52018-08-20 13:18:12 -0400206 SkMatrix viewMatrix = origViewMatrix;
207 viewMatrix.postTranslate(-SkIntToScalar(maskRect.fLeft), -SkIntToScalar(maskRect.fTop));
Michael Ludwig7c12e282020-05-29 09:54:07 -0400208 rtContext->drawShape(&clip, std::move(maskPaint), GrAA::kYes, viewMatrix, shape);
Robert Phillips40b05c32019-09-20 12:40:55 -0400209 return rtContext;
robertphillipsccb1b572015-05-27 11:02:55 -0700210}
211
Michael Ludwig2686d692020-04-17 20:21:37 +0000212static bool get_unclipped_shape_dev_bounds(const GrStyledShape& shape, const SkMatrix& matrix,
Robert Phillips27927a52018-08-20 13:18:12 -0400213 SkIRect* devBounds) {
214 SkRect shapeBounds = shape.styledBounds();
215 if (shapeBounds.isEmpty()) {
216 return false;
217 }
218 SkRect shapeDevBounds;
219 matrix.mapRect(&shapeDevBounds, shapeBounds);
220 // Even though these are "unclipped" bounds we still clip to the int32_t range.
221 // This is the largest int32_t that is representable exactly as a float. The next 63 larger ints
222 // would round down to this value when cast to a float, but who really cares.
223 // INT32_MIN is exactly representable.
224 static constexpr int32_t kMaxInt = 2147483520;
225 if (!shapeDevBounds.intersect(SkRect::MakeLTRB(INT32_MIN, INT32_MIN, kMaxInt, kMaxInt))) {
226 return false;
227 }
228 // Make sure that the resulting SkIRect can have representable width and height
229 if (SkScalarRoundToInt(shapeDevBounds.width()) > kMaxInt ||
230 SkScalarRoundToInt(shapeDevBounds.height()) > kMaxInt) {
231 return false;
232 }
233 shapeDevBounds.roundOut(devBounds);
234 return true;
235}
bsalomonc55271f2015-11-09 11:55:57 -0800236
Robert Phillips27927a52018-08-20 13:18:12 -0400237// Gets the shape bounds, the clip bounds, and the intersection (if any). Returns false if there
238// is no intersection.
239static bool get_shape_and_clip_bounds(GrRenderTargetContext* renderTargetContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400240 const GrClip* clip,
Michael Ludwig2686d692020-04-17 20:21:37 +0000241 const GrStyledShape& shape,
Robert Phillips27927a52018-08-20 13:18:12 -0400242 const SkMatrix& matrix,
243 SkIRect* unclippedDevShapeBounds,
244 SkIRect* devClipBounds) {
245 // compute bounds as intersection of rt size, clip, and path
Michael Ludwige06a8972020-06-11 10:29:00 -0400246 *devClipBounds = clip ? clip->getConservativeBounds()
Michael Ludwig7c12e282020-05-29 09:54:07 -0400247 : SkIRect::MakeWH(renderTargetContext->width(),
248 renderTargetContext->height());
bsalomonc55271f2015-11-09 11:55:57 -0800249
Robert Phillips27927a52018-08-20 13:18:12 -0400250 if (!get_unclipped_shape_dev_bounds(shape, matrix, unclippedDevShapeBounds)) {
Brian Salomon44207f32020-01-06 15:20:18 -0500251 *unclippedDevShapeBounds = SkIRect::MakeEmpty();
Robert Phillips27927a52018-08-20 13:18:12 -0400252 return false;
bsalomonc55271f2015-11-09 11:55:57 -0800253 }
254
Robert Phillips27927a52018-08-20 13:18:12 -0400255 return true;
256}
257
Robert Phillips52c17c42020-10-05 11:55:59 -0400258// The key and clip-bounds are computed together because the caching decision can impact the
259// clip-bound.
260// A 'false' return value indicates that the shape is known to be clipped away.
261static bool compute_key_and_clip_bounds(GrUniqueKey* maskKey,
262 SkIRect* boundsForClip,
263 const GrCaps* caps,
264 const SkMatrix& viewMatrix,
265 bool inverseFilled,
266 const SkMaskFilterBase* maskFilter,
267 const GrStyledShape& shape,
268 const SkIRect& unclippedDevShapeBounds,
269 const SkIRect& devClipBounds) {
270 *boundsForClip = devClipBounds;
271
272#ifndef SK_DISABLE_MASKFILTERED_MASK_CACHING
273 // To prevent overloading the cache with entries during animations we limit the cache of masks
274 // to cases where the matrix preserves axis alignment.
275 bool useCache = !inverseFilled && viewMatrix.preservesAxisAlignment() &&
276 shape.hasUnstyledKey() && as_MFB(maskFilter)->asABlur(nullptr);
277
278 if (useCache) {
279 SkIRect clippedMaskRect, unClippedMaskRect;
280 maskFilter->canFilterMaskGPU(shape, unclippedDevShapeBounds, devClipBounds,
281 viewMatrix, &clippedMaskRect);
282 maskFilter->canFilterMaskGPU(shape, unclippedDevShapeBounds, unclippedDevShapeBounds,
283 viewMatrix, &unClippedMaskRect);
284 if (clippedMaskRect.isEmpty()) {
285 return false;
286 }
287
288 // Use the cache only if >50% of the filtered mask is visible.
289 int unclippedWidth = unClippedMaskRect.width();
290 int unclippedHeight = unClippedMaskRect.height();
291 int64_t unclippedArea = sk_64_mul(unclippedWidth, unclippedHeight);
292 int64_t clippedArea = sk_64_mul(clippedMaskRect.width(), clippedMaskRect.height());
293 int maxTextureSize = caps->maxTextureSize();
294 if (unclippedArea > 2 * clippedArea || unclippedWidth > maxTextureSize ||
295 unclippedHeight > maxTextureSize) {
296 useCache = false;
297 } else {
298 // Make the clip not affect the mask
299 *boundsForClip = unclippedDevShapeBounds;
300 }
301 }
302
303 if (useCache) {
304 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
305 GrUniqueKey::Builder builder(maskKey, kDomain, 5 + 2 + shape.unstyledKeySize(),
306 "Mask Filtered Masks");
307
308 // We require the upper left 2x2 of the matrix to match exactly for a cache hit.
309 SkScalar sx = viewMatrix.get(SkMatrix::kMScaleX);
310 SkScalar sy = viewMatrix.get(SkMatrix::kMScaleY);
311 SkScalar kx = viewMatrix.get(SkMatrix::kMSkewX);
312 SkScalar ky = viewMatrix.get(SkMatrix::kMSkewY);
313 SkScalar tx = viewMatrix.get(SkMatrix::kMTransX);
314 SkScalar ty = viewMatrix.get(SkMatrix::kMTransY);
315 // Allow 8 bits each in x and y of subpixel positioning.
316 SkFixed fracX = SkScalarToFixed(SkScalarFraction(tx)) & 0x0000FF00;
317 SkFixed fracY = SkScalarToFixed(SkScalarFraction(ty)) & 0x0000FF00;
318
319 builder[0] = SkFloat2Bits(sx);
320 builder[1] = SkFloat2Bits(sy);
321 builder[2] = SkFloat2Bits(kx);
322 builder[3] = SkFloat2Bits(ky);
323 // Distinguish between hairline and filled paths. For hairlines, we also need to include
324 // the cap. (SW grows hairlines by 0.5 pixel with round and square caps). Note that
325 // stroke-and-fill of hairlines is turned into pure fill by SkStrokeRec, so this covers
326 // all cases we might see.
327 uint32_t styleBits = shape.style().isSimpleHairline()
328 ? ((shape.style().strokeRec().getCap() << 1) | 1)
329 : 0;
330 builder[4] = fracX | (fracY >> 8) | (styleBits << 16);
331
332 SkMaskFilterBase::BlurRec rec;
333 SkAssertResult(as_MFB(maskFilter)->asABlur(&rec));
334
335 builder[5] = rec.fStyle; // TODO: we could put this with the other style bits
336 builder[6] = SkFloat2Bits(rec.fSigma);
337 shape.writeUnstyledKey(&builder[7]);
338 }
339#endif
340
341 return true;
342}
343
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400344static GrSurfaceProxyView hw_create_filtered_mask(GrRecordingContext* rContext,
345 GrRenderTargetContext* renderTargetContext,
346 const SkMatrix& viewMatrix,
347 const GrStyledShape& shape,
348 const SkMaskFilterBase* filter,
349 const SkIRect& unclippedDevShapeBounds,
350 const SkIRect& clipBounds,
351 SkIRect* maskRect,
352 const GrUniqueKey& key) {
353 GrSurfaceProxyView filteredMaskView;
354
355 if (filter->canFilterMaskGPU(shape,
356 unclippedDevShapeBounds,
357 clipBounds,
358 viewMatrix,
359 maskRect)) {
360 if (clip_bounds_quick_reject(clipBounds, *maskRect)) {
361 // clipped out
362 return {};
363 }
364
365 GrProxyProvider* proxyProvider = rContext->priv().proxyProvider();
366
367 // TODO: this path should also use the thread-safe proxy-view cache!
368 if (key.isValid()) {
369 filteredMaskView = find_filtered_mask(proxyProvider, key);
370 }
371
372 if (!filteredMaskView) {
373 std::unique_ptr<GrRenderTargetContext> maskRTC(create_mask_GPU(
374 rContext,
375 *maskRect,
376 viewMatrix,
377 shape,
378 renderTargetContext->numSamples()));
379 if (maskRTC) {
380 filteredMaskView = filter->filterMaskGPU(rContext,
381 maskRTC->readSurfaceView(),
382 maskRTC->colorInfo().colorType(),
383 maskRTC->colorInfo().alphaType(),
384 viewMatrix,
385 *maskRect);
386 if (filteredMaskView && key.isValid()) {
387 SkASSERT(filteredMaskView.asTextureProxy());
388 proxyProvider->assignUniqueKeyToProxy(key, filteredMaskView.asTextureProxy());
389 }
390 }
391 }
392 }
393
394 return filteredMaskView;
395}
396
397static void draw_shape_with_mask_filter(GrRecordingContext* rContext,
Robert Phillips27927a52018-08-20 13:18:12 -0400398 GrRenderTargetContext* renderTargetContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400399 const GrClip* clip,
Robert Phillips27927a52018-08-20 13:18:12 -0400400 GrPaint&& paint,
Robert Phillips27927a52018-08-20 13:18:12 -0400401 const SkMatrix& viewMatrix,
402 const SkMaskFilterBase* maskFilter,
Michael Ludwig2686d692020-04-17 20:21:37 +0000403 const GrStyledShape& origShape) {
Robert Phillips27927a52018-08-20 13:18:12 -0400404 SkASSERT(maskFilter);
405
Michael Ludwig2686d692020-04-17 20:21:37 +0000406 const GrStyledShape* shape = &origShape;
407 SkTLazy<GrStyledShape> tmpShape;
Robert Phillips27927a52018-08-20 13:18:12 -0400408
409 if (origShape.style().applies()) {
410 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
411 if (0 == styleScale) {
412 return;
bsalomon6663acf2016-05-10 09:14:17 -0700413 }
Robert Phillips27927a52018-08-20 13:18:12 -0400414
415 tmpShape.init(origShape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale));
John Stilesa008b0f2020-08-16 08:48:02 -0400416 if (tmpShape->isEmpty()) {
Robert Phillips27927a52018-08-20 13:18:12 -0400417 return;
418 }
419
420 shape = tmpShape.get();
bsalomon6663acf2016-05-10 09:14:17 -0700421 }
bsalomonc55271f2015-11-09 11:55:57 -0800422
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400423 if (maskFilter->directFilterMaskGPU(rContext, renderTargetContext, std::move(paint), clip,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400424 viewMatrix, *shape)) {
Robert Phillipsb889f202018-08-17 19:55:59 +0000425 // the mask filter was able to draw itself directly, so there's nothing
426 // left to do.
427 return;
428 }
Mike Klein16885072018-12-11 09:54:31 -0500429 assert_alive(paint);
Robert Phillipsb889f202018-08-17 19:55:59 +0000430
Robert Phillips27927a52018-08-20 13:18:12 -0400431 // If the path is hairline, ignore inverse fill.
432 bool inverseFilled = shape->inverseFilled() &&
433 !GrPathRenderer::IsStrokeHairlineOrEquivalent(shape->style(),
434 viewMatrix, nullptr);
435
436 SkIRect unclippedDevShapeBounds, devClipBounds;
Robert Phillips31c080b2018-08-23 10:45:32 -0400437 if (!get_shape_and_clip_bounds(renderTargetContext, clip, *shape, viewMatrix,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400438 &unclippedDevShapeBounds, &devClipBounds)) {
Robert Phillips27927a52018-08-20 13:18:12 -0400439 // TODO: just cons up an opaque mask here
440 if (!inverseFilled) {
441 return;
442 }
443 }
444
Robert Phillips31c080b2018-08-23 10:45:32 -0400445 GrUniqueKey maskKey;
Robert Phillips52c17c42020-10-05 11:55:59 -0400446 SkIRect boundsForClip;
447 if (!compute_key_and_clip_bounds(&maskKey, &boundsForClip,
448 renderTargetContext->caps(),
449 viewMatrix, inverseFilled,
450 maskFilter, *shape,
451 unclippedDevShapeBounds,
452 devClipBounds)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400453 return; // 'shape' was entirely clipped out
Robert Phillips31c080b2018-08-23 10:45:32 -0400454 }
455
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400456 GrSurfaceProxyView filteredMaskView;
Robert Phillips31c080b2018-08-23 10:45:32 -0400457 SkIRect maskRect;
bsalomonc55271f2015-11-09 11:55:57 -0800458
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400459 if (rContext->asDirectContext()) {
460 filteredMaskView = hw_create_filtered_mask(rContext, renderTargetContext,
461 viewMatrix, *shape, maskFilter,
462 unclippedDevShapeBounds, boundsForClip,
463 &maskRect, maskKey);
Brian Salomond005b692020-04-01 15:47:05 -0400464 if (filteredMaskView) {
Brian Salomonfc118442019-11-22 19:09:27 -0500465 if (draw_mask(renderTargetContext, clip, viewMatrix, maskRect, std::move(paint),
Greg Daniel5c082492020-01-29 15:06:49 -0500466 std::move(filteredMaskView))) {
Robert Phillips20390c32018-08-17 11:01:03 -0400467 // This path is completely drawn
468 return;
469 }
Mike Klein16885072018-12-11 09:54:31 -0500470 assert_alive(paint);
Robert Phillips20390c32018-08-17 11:01:03 -0400471 }
bsalomonc55271f2015-11-09 11:55:57 -0800472 }
473
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400474 // Either HW mask rendering failed or we're in a DDL recording thread
475 filteredMaskView = sw_create_filtered_mask(rContext,
476 viewMatrix, *shape, maskFilter,
477 boundsForClip,
478 &maskRect, maskKey);
479 if (filteredMaskView) {
480 if (draw_mask(renderTargetContext, clip, viewMatrix, maskRect, std::move(paint),
481 std::move(filteredMaskView))) {
482 return;
483 }
484 assert_alive(paint);
485 }
bsalomon6663acf2016-05-10 09:14:17 -0700486}
487
Robert Phillips7af8fe52019-02-14 17:27:00 -0500488void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext* context,
Robert Phillips27927a52018-08-20 13:18:12 -0400489 GrRenderTargetContext* renderTargetContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400490 const GrClip* clip,
Michael Ludwig2686d692020-04-17 20:21:37 +0000491 const GrStyledShape& shape,
Robert Phillips27927a52018-08-20 13:18:12 -0400492 GrPaint&& paint,
Robert Phillips27927a52018-08-20 13:18:12 -0400493 const SkMatrix& viewMatrix,
494 const SkMaskFilter* mf) {
Robert Phillips793324c2018-08-24 13:53:56 -0400495 draw_shape_with_mask_filter(context, renderTargetContext, clip, std::move(paint),
Robert Phillips27927a52018-08-20 13:18:12 -0400496 viewMatrix, as_MFB(mf), shape);
bsalomonc55271f2015-11-09 11:55:57 -0800497}
498
Robert Phillips69893702019-02-22 11:16:30 -0500499void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext* context,
Robert Phillips27927a52018-08-20 13:18:12 -0400500 GrRenderTargetContext* renderTargetContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400501 const GrClip* clip,
Robert Phillips27927a52018-08-20 13:18:12 -0400502 const SkPaint& paint,
Brian Osman449b1152020-04-15 16:43:00 -0400503 const SkMatrixProvider& matrixProvider,
Michael Ludwig2686d692020-04-17 20:21:37 +0000504 const GrStyledShape& shape) {
Robert Phillips9eb00022020-06-30 15:30:12 -0400505 if (context->abandoned()) {
Robert Phillipsbebfd412018-03-08 11:07:23 -0500506 return;
507 }
508
robertphillipsccb1b572015-05-27 11:02:55 -0700509 GrPaint grPaint;
Brian Osman449b1152020-04-15 16:43:00 -0400510 if (!SkPaintToGrPaint(context, renderTargetContext->colorInfo(), paint, matrixProvider,
511 &grPaint)) {
robertphillipsccb1b572015-05-27 11:02:55 -0700512 return;
513 }
Robert Phillips27927a52018-08-20 13:18:12 -0400514
Brian Osman449b1152020-04-15 16:43:00 -0400515 const SkMatrix& viewMatrix(matrixProvider.localToDevice());
Mike Reed80747ef2018-01-23 15:29:32 -0500516 SkMaskFilterBase* mf = as_MFB(paint.getMaskFilter());
Mike Reedbfadcf02018-01-20 22:24:21 +0000517 if (mf && !mf->hasFragmentProcessor()) {
Robert Phillipsa29a9562016-10-20 09:40:55 -0400518 // The MaskFilter wasn't already handled in SkPaintToGrPaint
Robert Phillips793324c2018-08-24 13:53:56 -0400519 draw_shape_with_mask_filter(context, renderTargetContext, clip, std::move(grPaint),
Robert Phillips27927a52018-08-20 13:18:12 -0400520 viewMatrix, mf, shape);
bsalomonc55271f2015-11-09 11:55:57 -0800521 } else {
Robert Phillips793324c2018-08-24 13:53:56 -0400522 GrAA aa = GrAA(paint.isAntiAlias());
Robert Phillips27927a52018-08-20 13:18:12 -0400523 renderTargetContext->drawShape(clip, std::move(grPaint), aa, viewMatrix, shape);
robertphillipsccb1b572015-05-27 11:02:55 -0700524 }
robertphillipsccb1b572015-05-27 11:02:55 -0700525}