blob: e348f4bc7a7d097527bc2957a968d78ddc601ad2 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/private/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"
Brian Salomonb8f098d2020-01-07 11:15:44 -050021#include "src/gpu/effects/GrTextureEffect.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000022#include "src/gpu/geometry/GrStyledShape.h"
Robert Phillipsc7c2baf2018-03-08 09:51:04 -050023
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "include/core/SkPaint.h"
25#include "src/core/SkDraw.h"
26#include "src/core/SkMaskFilterBase.h"
Brian Osman449b1152020-04-15 16:43:00 -040027#include "src/core/SkMatrixProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/core/SkTLazy.h"
29#include "src/gpu/SkGr.h"
robertphillipsccb1b572015-05-27 11:02:55 -070030
31static bool clip_bounds_quick_reject(const SkIRect& clipBounds, const SkIRect& rect) {
32 return clipBounds.isEmpty() || rect.isEmpty() || !SkIRect::Intersects(clipBounds, rect);
33}
34
Brian Salomond005b692020-04-01 15:47:05 -040035static constexpr auto kMaskOrigin = kTopLeft_GrSurfaceOrigin;
36
37static GrSurfaceProxyView find_filtered_mask(GrProxyProvider* provider, const GrUniqueKey& key) {
Brian Salomon0029db02020-04-03 10:41:24 -040038 return provider->findCachedProxyWithColorTypeFallback(key, kMaskOrigin, GrColorType::kAlpha_8,
39 1);
Brian Salomond005b692020-04-01 15:47:05 -040040}
41
robertphillipsccb1b572015-05-27 11:02:55 -070042// Draw a mask using the supplied paint. Since the coverage/geometry
43// is already burnt into the mask this boils down to a rect draw.
44// Return true if the mask was successfully drawn.
Robert Phillips296b1cc2017-03-15 10:42:12 -040045static bool draw_mask(GrRenderTargetContext* renderTargetContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -040046 const GrClip* clip,
robertphillipsccb1b572015-05-27 11:02:55 -070047 const SkMatrix& viewMatrix,
robertphillipsf054b172016-05-13 05:06:19 -070048 const SkIRect& maskRect,
Brian Salomon82f44312017-01-11 13:42:54 -050049 GrPaint&& paint,
Greg Daniel5c082492020-01-29 15:06:49 -050050 GrSurfaceProxyView mask) {
Brian Salomon82f44312017-01-11 13:42:54 -050051 SkMatrix inverse;
52 if (!viewMatrix.invert(&inverse)) {
53 return false;
54 }
Robert Phillips4a24da52016-12-14 09:00:07 -050055
Mike Reed1f607332020-05-21 12:11:27 -040056 SkMatrix matrix = SkMatrix::Translate(-SkIntToScalar(maskRect.fLeft),
Robert Phillips67c18d62017-01-20 12:44:06 -050057 -SkIntToScalar(maskRect.fTop));
Brian Salomon2ebd0c82016-10-03 17:15:28 -040058 matrix.preConcat(viewMatrix);
Brian Salomonfc118442019-11-22 19:09:27 -050059 paint.addCoverageFragmentProcessor(
Greg Danield2ccbb52020-02-05 10:45:39 -050060 GrTextureEffect::Make(std::move(mask), kUnknown_SkAlphaType, matrix));
robertphillipsccb1b572015-05-27 11:02:55 -070061
Brian Salomon82f44312017-01-11 13:42:54 -050062 renderTargetContext->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050063 SkRect::Make(maskRect), 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 Phillips7af8fe52019-02-14 17:27:00 -050071static bool sw_draw_with_mask_filter(GrRecordingContext* context,
Robert Phillips4a24da52016-12-14 09:00:07 -050072 GrRenderTargetContext* renderTargetContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -040073 const GrClip* clipData,
robertphillips0e7029e2015-11-30 05:45:06 -080074 const SkMatrix& viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +000075 const GrStyledShape& shape,
robertphillips0e7029e2015-11-30 05:45:06 -080076 const SkMaskFilter* filter,
77 const SkIRect& clipBounds,
Robert Phillips31c080b2018-08-23 10:45:32 -040078 GrPaint&& paint,
79 const GrUniqueKey& key) {
Robert Phillips27927a52018-08-20 13:18:12 -040080 SkASSERT(filter);
81 SkASSERT(!shape.style().applies());
82
Robert Phillips9da87e02019-02-04 13:26:26 -050083 auto proxyProvider = context->priv().proxyProvider();
Robert Phillips27927a52018-08-20 13:18:12 -040084
Greg Daniel5c082492020-01-29 15:06:49 -050085 GrSurfaceProxyView filteredMaskView;
Robert Phillips27927a52018-08-20 13:18:12 -040086
87 SkStrokeRec::InitStyle fillOrHairline = shape.style().isSimpleHairline()
88 ? SkStrokeRec::kHairline_InitStyle
89 : SkStrokeRec::kFill_InitStyle;
90
Robert Phillips31c080b2018-08-23 10:45:32 -040091 if (key.isValid()) {
Brian Salomond005b692020-04-01 15:47:05 -040092 filteredMaskView = find_filtered_mask(proxyProvider, key);
robertphillipsccb1b572015-05-27 11:02:55 -070093 }
94
Robert Phillips31c080b2018-08-23 10:45:32 -040095 SkIRect drawRect;
Brian Salomond005b692020-04-01 15:47:05 -040096 if (filteredMaskView) {
Robert Phillips31c080b2018-08-23 10:45:32 -040097 SkRect devBounds = shape.bounds();
98 viewMatrix.mapRect(&devBounds);
Robert Phillips4a24da52016-12-14 09:00:07 -050099
Robert Phillips31c080b2018-08-23 10:45:32 -0400100 // Here we need to recompute the destination bounds in order to draw the mask correctly
101 SkMask srcM, dstM;
102 if (!SkDraw::ComputeMaskBounds(devBounds, &clipBounds, filter, &viewMatrix,
103 &srcM.fBounds)) {
104 return false;
105 }
106
107 srcM.fFormat = SkMask::kA8_Format;
108
109 if (!as_MFB(filter)->filterMask(&dstM, srcM, viewMatrix, nullptr)) {
110 return false;
111 }
112
113 // Unfortunately, we cannot double check that the computed bounds (i.e., dstM.fBounds)
114 // match the stored bounds of the mask bc the proxy may have been recreated and,
115 // when it is recreated, it just gets the bounds of the underlying GrTexture (which
116 // might be a loose fit).
117 drawRect = dstM.fBounds;
118 } else {
119 // 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)) {
130 return false;
131 }
132 SkAutoMaskFreeImage autoSrc(srcM.fImage);
133
134 SkASSERT(SkMask::kA8_Format == srcM.fFormat);
135
136 if (!as_MFB(filter)->filterMask(&dstM, srcM, viewMatrix, nullptr)) {
137 return false;
138 }
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)) {
143 return false;
144 }
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)) {
151 return false;
152 }
153 bm.setImmutable();
154
Brian Salomonbc074a62020-03-18 10:06:13 -0400155 GrBitmapTextureMaker maker(context, bm, SkBackingFit::kApprox);
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500156 filteredMaskView = maker.view(GrMipMapped::kNo);
Greg Danielcc104db2020-02-03 14:17:08 -0500157 if (!filteredMaskView.proxy()) {
Robert Phillips31c080b2018-08-23 10:45:32 -0400158 return false;
159 }
160
Brian Salomond005b692020-04-01 15:47:05 -0400161 SkASSERT(kMaskOrigin == filteredMaskView.origin());
Robert Phillips59b39e72018-08-30 11:51:25 -0400162
Robert Phillips31c080b2018-08-23 10:45:32 -0400163 drawRect = dstM.fBounds;
164
165 if (key.isValid()) {
Greg Danielcc104db2020-02-03 14:17:08 -0500166 proxyProvider->assignUniqueKeyToProxy(key, filteredMaskView.asTextureProxy());
Robert Phillips31c080b2018-08-23 10:45:32 -0400167 }
Robert Phillips48ce22b2018-03-23 10:33:12 -0400168 }
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500169
Brian Salomonfc118442019-11-22 19:09:27 -0500170 return draw_mask(renderTargetContext, clipData, viewMatrix, drawRect, std::move(paint),
Greg Daniel5c082492020-01-29 15:06:49 -0500171 std::move(filteredMaskView));
robertphillipsccb1b572015-05-27 11:02:55 -0700172}
173
Robert Phillips40b05c32019-09-20 12:40:55 -0400174// Create a mask of 'shape' and return the resulting renderTargetContext
175static std::unique_ptr<GrRenderTargetContext> create_mask_GPU(GrRecordingContext* context,
176 const SkIRect& maskRect,
177 const SkMatrix& origViewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +0000178 const GrStyledShape& shape,
Robert Phillips40b05c32019-09-20 12:40:55 -0400179 int sampleCnt) {
Chris Dalton0493fbd2019-09-18 15:49:46 -0600180 // Use GrResourceProvider::MakeApprox to implement our own approximate size matching, but demand
181 // a "SkBackingFit::kExact" size match on the actual render target. We do this because the
182 // filter will reach outside the src bounds, so we need to pre-clear these values to ensure a
183 // "decal" sampling effect (i.e., ensure reads outside the src bounds return alpha=0).
184 //
185 // FIXME: Reads outside the left and top edges will actually clamp to the edge pixel. And in the
186 // event that MakeApprox does not change the size, reads outside the right and/or bottom will do
187 // the same. We should offset our filter within the render target and expand the size as needed
188 // to guarantee at least 1px of padding on all sides.
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400189 auto approxSize = GrResourceProvider::MakeApprox(maskRect.size());
Greg Daniele20fcad2020-01-08 11:52:34 -0500190 auto rtContext = GrRenderTargetContext::MakeWithFallback(
191 context, GrColorType::kAlpha_8, nullptr, SkBackingFit::kExact, approxSize, sampleCnt,
Brian Salomond005b692020-04-01 15:47:05 -0400192 GrMipMapped::kNo, GrProtected::kNo, kMaskOrigin);
robertphillipsd728f0c2016-11-21 11:05:03 -0800193 if (!rtContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700194 return nullptr;
robertphillipsccb1b572015-05-27 11:02:55 -0700195 }
196
Chris Dalton0493fbd2019-09-18 15:49:46 -0600197 rtContext->clear(SK_PMColor4fTRANSPARENT);
robertphillipsccb1b572015-05-27 11:02:55 -0700198
Brian Salomon82f44312017-01-11 13:42:54 -0500199 GrPaint maskPaint;
200 maskPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op);
robertphillipsccb1b572015-05-27 11:02:55 -0700201
202 // setup new clip
Michael Ludwig4926b072020-06-04 19:39:42 +0000203 const SkIRect clipRect = SkIRect::MakeWH(maskRect.width(), maskRect.height());
204 GrFixedClip clip(clipRect);
robertphillipsccb1b572015-05-27 11:02:55 -0700205
Brian Salomond005b692020-04-01 15:47:05 -0400206 // Draw the mask into maskTexture with the path's integerized top-left at the origin using
207 // maskPaint.
Robert Phillips27927a52018-08-20 13:18:12 -0400208 SkMatrix viewMatrix = origViewMatrix;
209 viewMatrix.postTranslate(-SkIntToScalar(maskRect.fLeft), -SkIntToScalar(maskRect.fTop));
Michael Ludwig7c12e282020-05-29 09:54:07 -0400210 rtContext->drawShape(&clip, std::move(maskPaint), GrAA::kYes, viewMatrix, shape);
Robert Phillips40b05c32019-09-20 12:40:55 -0400211 return rtContext;
robertphillipsccb1b572015-05-27 11:02:55 -0700212}
213
Michael Ludwig2686d692020-04-17 20:21:37 +0000214static bool get_unclipped_shape_dev_bounds(const GrStyledShape& shape, const SkMatrix& matrix,
Robert Phillips27927a52018-08-20 13:18:12 -0400215 SkIRect* devBounds) {
216 SkRect shapeBounds = shape.styledBounds();
217 if (shapeBounds.isEmpty()) {
218 return false;
219 }
220 SkRect shapeDevBounds;
221 matrix.mapRect(&shapeDevBounds, shapeBounds);
222 // Even though these are "unclipped" bounds we still clip to the int32_t range.
223 // This is the largest int32_t that is representable exactly as a float. The next 63 larger ints
224 // would round down to this value when cast to a float, but who really cares.
225 // INT32_MIN is exactly representable.
226 static constexpr int32_t kMaxInt = 2147483520;
227 if (!shapeDevBounds.intersect(SkRect::MakeLTRB(INT32_MIN, INT32_MIN, kMaxInt, kMaxInt))) {
228 return false;
229 }
230 // Make sure that the resulting SkIRect can have representable width and height
231 if (SkScalarRoundToInt(shapeDevBounds.width()) > kMaxInt ||
232 SkScalarRoundToInt(shapeDevBounds.height()) > kMaxInt) {
233 return false;
234 }
235 shapeDevBounds.roundOut(devBounds);
236 return true;
237}
bsalomonc55271f2015-11-09 11:55:57 -0800238
Robert Phillips27927a52018-08-20 13:18:12 -0400239// Gets the shape bounds, the clip bounds, and the intersection (if any). Returns false if there
240// is no intersection.
241static bool get_shape_and_clip_bounds(GrRenderTargetContext* renderTargetContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400242 const GrClip* clip,
Michael Ludwig2686d692020-04-17 20:21:37 +0000243 const GrStyledShape& shape,
Robert Phillips27927a52018-08-20 13:18:12 -0400244 const SkMatrix& matrix,
245 SkIRect* unclippedDevShapeBounds,
246 SkIRect* devClipBounds) {
247 // compute bounds as intersection of rt size, clip, and path
Michael Ludwig7c12e282020-05-29 09:54:07 -0400248 *devClipBounds = clip ? clip->getConservativeBounds(renderTargetContext->width(),
249 renderTargetContext->height())
250 : SkIRect::MakeWH(renderTargetContext->width(),
251 renderTargetContext->height());
bsalomonc55271f2015-11-09 11:55:57 -0800252
Robert Phillips27927a52018-08-20 13:18:12 -0400253 if (!get_unclipped_shape_dev_bounds(shape, matrix, unclippedDevShapeBounds)) {
Brian Salomon44207f32020-01-06 15:20:18 -0500254 *unclippedDevShapeBounds = SkIRect::MakeEmpty();
Robert Phillips27927a52018-08-20 13:18:12 -0400255 return false;
bsalomonc55271f2015-11-09 11:55:57 -0800256 }
257
Robert Phillips27927a52018-08-20 13:18:12 -0400258 return true;
259}
260
Robert Phillips7af8fe52019-02-14 17:27:00 -0500261static void draw_shape_with_mask_filter(GrRecordingContext* context,
Robert Phillips27927a52018-08-20 13:18:12 -0400262 GrRenderTargetContext* renderTargetContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400263 const GrClip* clip,
Robert Phillips27927a52018-08-20 13:18:12 -0400264 GrPaint&& paint,
Robert Phillips27927a52018-08-20 13:18:12 -0400265 const SkMatrix& viewMatrix,
266 const SkMaskFilterBase* maskFilter,
Michael Ludwig2686d692020-04-17 20:21:37 +0000267 const GrStyledShape& origShape) {
Robert Phillips27927a52018-08-20 13:18:12 -0400268 SkASSERT(maskFilter);
269
Michael Ludwig2686d692020-04-17 20:21:37 +0000270 const GrStyledShape* shape = &origShape;
271 SkTLazy<GrStyledShape> tmpShape;
Robert Phillips27927a52018-08-20 13:18:12 -0400272
273 if (origShape.style().applies()) {
274 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
275 if (0 == styleScale) {
276 return;
bsalomon6663acf2016-05-10 09:14:17 -0700277 }
Robert Phillips27927a52018-08-20 13:18:12 -0400278
279 tmpShape.init(origShape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale));
280 if (tmpShape.get()->isEmpty()) {
281 return;
282 }
283
284 shape = tmpShape.get();
bsalomon6663acf2016-05-10 09:14:17 -0700285 }
bsalomonc55271f2015-11-09 11:55:57 -0800286
Michael Ludwig7c12e282020-05-29 09:54:07 -0400287 if (maskFilter->directFilterMaskGPU(context, renderTargetContext, std::move(paint), clip,
288 viewMatrix, *shape)) {
Robert Phillipsb889f202018-08-17 19:55:59 +0000289 // the mask filter was able to draw itself directly, so there's nothing
290 // left to do.
291 return;
292 }
Mike Klein16885072018-12-11 09:54:31 -0500293 assert_alive(paint);
Robert Phillipsb889f202018-08-17 19:55:59 +0000294
Robert Phillips27927a52018-08-20 13:18:12 -0400295 // If the path is hairline, ignore inverse fill.
296 bool inverseFilled = shape->inverseFilled() &&
297 !GrPathRenderer::IsStrokeHairlineOrEquivalent(shape->style(),
298 viewMatrix, nullptr);
299
300 SkIRect unclippedDevShapeBounds, devClipBounds;
Robert Phillips31c080b2018-08-23 10:45:32 -0400301 if (!get_shape_and_clip_bounds(renderTargetContext, clip, *shape, viewMatrix,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400302 &unclippedDevShapeBounds, &devClipBounds)) {
Robert Phillips27927a52018-08-20 13:18:12 -0400303 // TODO: just cons up an opaque mask here
304 if (!inverseFilled) {
305 return;
306 }
307 }
308
Robert Phillips31c080b2018-08-23 10:45:32 -0400309 // To prevent overloading the cache with entries during animations we limit the cache of masks
310 // to cases where the matrix preserves axis alignment.
Robert Phillipsde479282018-08-28 11:20:08 -0400311#ifdef SK_DISABLE_MASKFILTERED_MASK_CACHING
312 bool useCache = false;
313#else
Robert Phillips31c080b2018-08-23 10:45:32 -0400314 bool useCache = !inverseFilled && viewMatrix.preservesAxisAlignment() &&
315 shape->hasUnstyledKey() && as_MFB(maskFilter)->asABlur(nullptr);
Robert Phillipsde479282018-08-28 11:20:08 -0400316#endif
Robert Phillips31c080b2018-08-23 10:45:32 -0400317
318 const SkIRect* boundsForClip = &devClipBounds;
319 if (useCache) {
320 SkIRect clippedMaskRect, unClippedMaskRect;
321 maskFilter->canFilterMaskGPU(*shape, unclippedDevShapeBounds, devClipBounds,
322 viewMatrix, &clippedMaskRect);
323 maskFilter->canFilterMaskGPU(*shape, unclippedDevShapeBounds, unclippedDevShapeBounds,
324 viewMatrix, &unClippedMaskRect);
325 if (clippedMaskRect.isEmpty()) {
326 return;
327 }
328
329 // Use the cache only if >50% of the filtered mask is visible.
330 int unclippedWidth = unClippedMaskRect.width();
331 int unclippedHeight = unClippedMaskRect.height();
332 int64_t unclippedArea = sk_64_mul(unclippedWidth, unclippedHeight);
333 int64_t clippedArea = sk_64_mul(clippedMaskRect.width(), clippedMaskRect.height());
334 int maxTextureSize = renderTargetContext->caps()->maxTextureSize();
335 if (unclippedArea > 2 * clippedArea || unclippedWidth > maxTextureSize ||
336 unclippedHeight > maxTextureSize) {
337 useCache = false;
338 } else {
339 // Make the clip not affect the mask
340 boundsForClip = &unclippedDevShapeBounds;
341 }
342 }
343
344 GrUniqueKey maskKey;
345 if (useCache) {
346 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
347 GrUniqueKey::Builder builder(&maskKey, kDomain, 5 + 2 + shape->unstyledKeySize(),
348 "Mask Filtered Masks");
349
350 // We require the upper left 2x2 of the matrix to match exactly for a cache hit.
351 SkScalar sx = viewMatrix.get(SkMatrix::kMScaleX);
352 SkScalar sy = viewMatrix.get(SkMatrix::kMScaleY);
353 SkScalar kx = viewMatrix.get(SkMatrix::kMSkewX);
354 SkScalar ky = viewMatrix.get(SkMatrix::kMSkewY);
355 SkScalar tx = viewMatrix.get(SkMatrix::kMTransX);
356 SkScalar ty = viewMatrix.get(SkMatrix::kMTransY);
357 // Allow 8 bits each in x and y of subpixel positioning.
358 SkFixed fracX = SkScalarToFixed(SkScalarFraction(tx)) & 0x0000FF00;
359 SkFixed fracY = SkScalarToFixed(SkScalarFraction(ty)) & 0x0000FF00;
360
361 builder[0] = SkFloat2Bits(sx);
362 builder[1] = SkFloat2Bits(sy);
363 builder[2] = SkFloat2Bits(kx);
364 builder[3] = SkFloat2Bits(ky);
365 // Distinguish between hairline and filled paths. For hairlines, we also need to include
366 // the cap. (SW grows hairlines by 0.5 pixel with round and square caps). Note that
367 // stroke-and-fill of hairlines is turned into pure fill by SkStrokeRec, so this covers
368 // all cases we might see.
369 uint32_t styleBits = shape->style().isSimpleHairline()
370 ? ((shape->style().strokeRec().getCap() << 1) | 1)
371 : 0;
372 builder[4] = fracX | (fracY >> 8) | (styleBits << 16);
373
374 SkMaskFilterBase::BlurRec rec;
375 SkAssertResult(as_MFB(maskFilter)->asABlur(&rec));
376
377 builder[5] = rec.fStyle; // TODO: we could put this with the other style bits
Robert Phillips55ff5d32019-01-07 16:11:36 -0500378 builder[6] = SkFloat2Bits(rec.fSigma);
Robert Phillips31c080b2018-08-23 10:45:32 -0400379 shape->writeUnstyledKey(&builder[7]);
380 }
381
382 SkIRect maskRect;
Robert Phillips27927a52018-08-20 13:18:12 -0400383 if (maskFilter->canFilterMaskGPU(*shape,
Robert Phillips31c080b2018-08-23 10:45:32 -0400384 unclippedDevShapeBounds,
385 *boundsForClip,
bsalomonc55271f2015-11-09 11:55:57 -0800386 viewMatrix,
387 &maskRect)) {
Robert Phillips31c080b2018-08-23 10:45:32 -0400388 if (clip_bounds_quick_reject(*boundsForClip, maskRect)) {
bsalomonc55271f2015-11-09 11:55:57 -0800389 // clipped out
390 return;
391 }
392
Greg Daniel5c082492020-01-29 15:06:49 -0500393 GrSurfaceProxyView filteredMaskView;
Robert Phillips20390c32018-08-17 11:01:03 -0400394
Robert Phillips9da87e02019-02-04 13:26:26 -0500395 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillips31c080b2018-08-23 10:45:32 -0400396
397 if (maskKey.isValid()) {
Brian Salomond005b692020-04-01 15:47:05 -0400398 filteredMaskView = find_filtered_mask(proxyProvider, maskKey);
Robert Phillips31c080b2018-08-23 10:45:32 -0400399 }
400
Brian Salomond005b692020-04-01 15:47:05 -0400401 if (!filteredMaskView) {
Robert Phillips40b05c32019-09-20 12:40:55 -0400402 std::unique_ptr<GrRenderTargetContext> maskRTC(create_mask_GPU(
Greg Daniel5c082492020-01-29 15:06:49 -0500403 context,
404 maskRect,
405 viewMatrix,
406 *shape,
407 renderTargetContext->numSamples()));
Robert Phillips40b05c32019-09-20 12:40:55 -0400408 if (maskRTC) {
Greg Daniel5c082492020-01-29 15:06:49 -0500409 filteredMaskView = maskFilter->filterMaskGPU(context,
410 maskRTC->readSurfaceView(),
411 maskRTC->colorInfo().colorType(),
412 maskRTC->colorInfo().alphaType(),
413 viewMatrix,
414 maskRect);
415 if (filteredMaskView.proxy() && maskKey.isValid()) {
416 SkASSERT(filteredMaskView.asTextureProxy());
417 proxyProvider->assignUniqueKeyToProxy(maskKey,
418 filteredMaskView.asTextureProxy());
Robert Phillips31c080b2018-08-23 10:45:32 -0400419 }
bsalomonc55271f2015-11-09 11:55:57 -0800420 }
421 }
Robert Phillips20390c32018-08-17 11:01:03 -0400422
Brian Salomond005b692020-04-01 15:47:05 -0400423 if (filteredMaskView) {
Brian Salomonfc118442019-11-22 19:09:27 -0500424 if (draw_mask(renderTargetContext, clip, viewMatrix, maskRect, std::move(paint),
Greg Daniel5c082492020-01-29 15:06:49 -0500425 std::move(filteredMaskView))) {
Robert Phillips20390c32018-08-17 11:01:03 -0400426 // This path is completely drawn
427 return;
428 }
Mike Klein16885072018-12-11 09:54:31 -0500429 assert_alive(paint);
Robert Phillips20390c32018-08-17 11:01:03 -0400430 }
bsalomonc55271f2015-11-09 11:55:57 -0800431 }
432
Robert Phillips27927a52018-08-20 13:18:12 -0400433 sw_draw_with_mask_filter(context, renderTargetContext, clip, viewMatrix, *shape,
Robert Phillips31c080b2018-08-23 10:45:32 -0400434 maskFilter, *boundsForClip, std::move(paint), maskKey);
bsalomon6663acf2016-05-10 09:14:17 -0700435}
436
Robert Phillips7af8fe52019-02-14 17:27:00 -0500437void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext* context,
Robert Phillips27927a52018-08-20 13:18:12 -0400438 GrRenderTargetContext* renderTargetContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400439 const GrClip* clip,
Michael Ludwig2686d692020-04-17 20:21:37 +0000440 const GrStyledShape& shape,
Robert Phillips27927a52018-08-20 13:18:12 -0400441 GrPaint&& paint,
Robert Phillips27927a52018-08-20 13:18:12 -0400442 const SkMatrix& viewMatrix,
443 const SkMaskFilter* mf) {
Robert Phillips793324c2018-08-24 13:53:56 -0400444 draw_shape_with_mask_filter(context, renderTargetContext, clip, std::move(paint),
Robert Phillips27927a52018-08-20 13:18:12 -0400445 viewMatrix, as_MFB(mf), shape);
bsalomonc55271f2015-11-09 11:55:57 -0800446}
447
Robert Phillips69893702019-02-22 11:16:30 -0500448void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext* context,
Robert Phillips27927a52018-08-20 13:18:12 -0400449 GrRenderTargetContext* renderTargetContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400450 const GrClip* clip,
Robert Phillips27927a52018-08-20 13:18:12 -0400451 const SkPaint& paint,
Brian Osman449b1152020-04-15 16:43:00 -0400452 const SkMatrixProvider& matrixProvider,
Michael Ludwig2686d692020-04-17 20:21:37 +0000453 const GrStyledShape& shape) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500454 if (context->priv().abandoned()) {
Robert Phillipsbebfd412018-03-08 11:07:23 -0500455 return;
456 }
457
robertphillipsccb1b572015-05-27 11:02:55 -0700458 GrPaint grPaint;
Brian Osman449b1152020-04-15 16:43:00 -0400459 if (!SkPaintToGrPaint(context, renderTargetContext->colorInfo(), paint, matrixProvider,
460 &grPaint)) {
robertphillipsccb1b572015-05-27 11:02:55 -0700461 return;
462 }
Robert Phillips27927a52018-08-20 13:18:12 -0400463
Brian Osman449b1152020-04-15 16:43:00 -0400464 const SkMatrix& viewMatrix(matrixProvider.localToDevice());
Mike Reed80747ef2018-01-23 15:29:32 -0500465 SkMaskFilterBase* mf = as_MFB(paint.getMaskFilter());
Mike Reedbfadcf02018-01-20 22:24:21 +0000466 if (mf && !mf->hasFragmentProcessor()) {
Robert Phillipsa29a9562016-10-20 09:40:55 -0400467 // The MaskFilter wasn't already handled in SkPaintToGrPaint
Robert Phillips793324c2018-08-24 13:53:56 -0400468 draw_shape_with_mask_filter(context, renderTargetContext, clip, std::move(grPaint),
Robert Phillips27927a52018-08-20 13:18:12 -0400469 viewMatrix, mf, shape);
bsalomonc55271f2015-11-09 11:55:57 -0800470 } else {
Robert Phillips793324c2018-08-24 13:53:56 -0400471 GrAA aa = GrAA(paint.isAntiAlias());
Robert Phillips27927a52018-08-20 13:18:12 -0400472 renderTargetContext->drawShape(clip, std::move(grPaint), aa, viewMatrix, shape);
robertphillipsccb1b572015-05-27 11:02:55 -0700473 }
robertphillipsccb1b572015-05-27 11:02:55 -0700474}