blob: 103fa73cd3a7121792e8316ed1b3e7ac28546efd [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
8#include "GrBlurUtils.h"
Robert Phillipsc7c2baf2018-03-08 09:51:04 -05009
bsalomon76228632015-05-29 08:02:10 -070010#include "GrCaps.h"
Robert Phillips6a6de562019-02-15 15:19:15 -050011#include "GrContextPriv.h"
csmartdalton02fa32c2016-08-19 13:29:27 -070012#include "GrFixedClip.h"
Robert Phillipsc7c2baf2018-03-08 09:51:04 -050013#include "GrProxyProvider.h"
Robert Phillips7af8fe52019-02-14 17:27:00 -050014#include "GrRecordingContext.h"
15#include "GrRecordingContextPriv.h"
Robert Phillipsc7c2baf2018-03-08 09:51:04 -050016#include "GrRenderTargetContext.h"
Robert Phillips784b7bf2016-12-09 13:35:02 -050017#include "GrRenderTargetContextPriv.h"
Robert Phillips27927a52018-08-20 13:18:12 -040018#include "GrShape.h"
19#include "GrSoftwarePathRenderer.h"
bsalomon6663acf2016-05-10 09:14:17 -070020#include "GrStyle.h"
robertphillipsd728f0c2016-11-21 11:05:03 -080021#include "GrTextureProxy.h"
Robert Phillipsc7c2baf2018-03-08 09:51:04 -050022#include "effects/GrSimpleTextureEffect.h"
23
robertphillipsccb1b572015-05-27 11:02:55 -070024#include "SkDraw.h"
Brian Osman3b655982017-03-07 16:58:08 -050025#include "SkGr.h"
Mike Reed80747ef2018-01-23 15:29:32 -050026#include "SkMaskFilterBase.h"
robertphillipsccb1b572015-05-27 11:02:55 -070027#include "SkPaint.h"
csmartdaltonc6f411e2016-08-05 22:32:12 -070028#include "SkTLazy.h"
robertphillipsccb1b572015-05-27 11:02:55 -070029
30static bool clip_bounds_quick_reject(const SkIRect& clipBounds, const SkIRect& rect) {
31 return clipBounds.isEmpty() || rect.isEmpty() || !SkIRect::Intersects(clipBounds, rect);
32}
33
34// Draw a mask using the supplied paint. Since the coverage/geometry
35// is already burnt into the mask this boils down to a rect draw.
36// Return true if the mask was successfully drawn.
Robert Phillips296b1cc2017-03-15 10:42:12 -040037static bool draw_mask(GrRenderTargetContext* renderTargetContext,
robertphillipsccb1b572015-05-27 11:02:55 -070038 const GrClip& clip,
39 const SkMatrix& viewMatrix,
robertphillipsf054b172016-05-13 05:06:19 -070040 const SkIRect& maskRect,
Brian Salomon82f44312017-01-11 13:42:54 -050041 GrPaint&& paint,
Robert Phillips4a24da52016-12-14 09:00:07 -050042 sk_sp<GrTextureProxy> mask) {
Brian Salomon82f44312017-01-11 13:42:54 -050043 SkMatrix inverse;
44 if (!viewMatrix.invert(&inverse)) {
45 return false;
46 }
Robert Phillips4a24da52016-12-14 09:00:07 -050047
Robert Phillips67c18d62017-01-20 12:44:06 -050048 SkMatrix matrix = SkMatrix::MakeTrans(-SkIntToScalar(maskRect.fLeft),
49 -SkIntToScalar(maskRect.fTop));
Brian Salomon2ebd0c82016-10-03 17:15:28 -040050 matrix.preConcat(viewMatrix);
Brian Osman2240be92017-10-18 13:15:13 -040051 paint.addCoverageFragmentProcessor(GrSimpleTextureEffect::Make(std::move(mask), matrix));
robertphillipsccb1b572015-05-27 11:02:55 -070052
Brian Salomon82f44312017-01-11 13:42:54 -050053 renderTargetContext->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050054 SkRect::Make(maskRect), inverse);
robertphillipsccb1b572015-05-27 11:02:55 -070055 return true;
56}
57
Robert Phillipsc7c2baf2018-03-08 09:51:04 -050058static void mask_release_proc(void* addr, void* /*context*/) {
59 SkMask::FreeImage(addr);
60}
61
Robert Phillips7af8fe52019-02-14 17:27:00 -050062static bool sw_draw_with_mask_filter(GrRecordingContext* context,
Robert Phillips4a24da52016-12-14 09:00:07 -050063 GrRenderTargetContext* renderTargetContext,
robertphillips0e7029e2015-11-30 05:45:06 -080064 const GrClip& clipData,
65 const SkMatrix& viewMatrix,
Robert Phillips27927a52018-08-20 13:18:12 -040066 const GrShape& shape,
robertphillips0e7029e2015-11-30 05:45:06 -080067 const SkMaskFilter* filter,
68 const SkIRect& clipBounds,
Robert Phillips31c080b2018-08-23 10:45:32 -040069 GrPaint&& paint,
70 const GrUniqueKey& key) {
Robert Phillips27927a52018-08-20 13:18:12 -040071 SkASSERT(filter);
72 SkASSERT(!shape.style().applies());
73
Robert Phillips9da87e02019-02-04 13:26:26 -050074 auto proxyProvider = context->priv().proxyProvider();
Robert Phillips27927a52018-08-20 13:18:12 -040075
Robert Phillips31c080b2018-08-23 10:45:32 -040076 sk_sp<GrTextureProxy> filteredMask;
Robert Phillips27927a52018-08-20 13:18:12 -040077
78 SkStrokeRec::InitStyle fillOrHairline = shape.style().isSimpleHairline()
79 ? SkStrokeRec::kHairline_InitStyle
80 : SkStrokeRec::kFill_InitStyle;
81
Robert Phillips31c080b2018-08-23 10:45:32 -040082 if (key.isValid()) {
83 // TODO: this cache look up is duplicated in draw_shape_with_mask_filter for gpu
84 filteredMask = proxyProvider->findOrCreateProxyByUniqueKey(key, kTopLeft_GrSurfaceOrigin);
robertphillipsccb1b572015-05-27 11:02:55 -070085 }
86
Robert Phillips31c080b2018-08-23 10:45:32 -040087 SkIRect drawRect;
88 if (filteredMask) {
89 SkRect devBounds = shape.bounds();
90 viewMatrix.mapRect(&devBounds);
Robert Phillips4a24da52016-12-14 09:00:07 -050091
Robert Phillips31c080b2018-08-23 10:45:32 -040092 // Here we need to recompute the destination bounds in order to draw the mask correctly
93 SkMask srcM, dstM;
94 if (!SkDraw::ComputeMaskBounds(devBounds, &clipBounds, filter, &viewMatrix,
95 &srcM.fBounds)) {
96 return false;
97 }
98
99 srcM.fFormat = SkMask::kA8_Format;
100
101 if (!as_MFB(filter)->filterMask(&dstM, srcM, viewMatrix, nullptr)) {
102 return false;
103 }
104
105 // Unfortunately, we cannot double check that the computed bounds (i.e., dstM.fBounds)
106 // match the stored bounds of the mask bc the proxy may have been recreated and,
107 // when it is recreated, it just gets the bounds of the underlying GrTexture (which
108 // might be a loose fit).
109 drawRect = dstM.fBounds;
110 } else {
111 // TODO: it seems like we could create an SkDraw here and set its fMatrix field rather
112 // than explicitly transforming the path to device space.
113 SkPath devPath;
114
115 shape.asPath(&devPath);
116
117 devPath.transform(viewMatrix);
118
119 SkMask srcM, dstM;
120 if (!SkDraw::DrawToMask(devPath, &clipBounds, filter, &viewMatrix, &srcM,
121 SkMask::kComputeBoundsAndRenderImage_CreateMode, fillOrHairline)) {
122 return false;
123 }
124 SkAutoMaskFreeImage autoSrc(srcM.fImage);
125
126 SkASSERT(SkMask::kA8_Format == srcM.fFormat);
127
128 if (!as_MFB(filter)->filterMask(&dstM, srcM, viewMatrix, nullptr)) {
129 return false;
130 }
131 // this will free-up dstM when we're done (allocated in filterMask())
132 SkAutoMaskFreeImage autoDst(dstM.fImage);
133
134 if (clip_bounds_quick_reject(clipBounds, dstM.fBounds)) {
135 return false;
136 }
137
138 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
139 // the current clip (and identity matrix) and GrPaint settings
140 SkBitmap bm;
141 if (!bm.installPixels(SkImageInfo::MakeA8(dstM.fBounds.width(), dstM.fBounds.height()),
142 autoDst.release(), dstM.fRowBytes, mask_release_proc, nullptr)) {
143 return false;
144 }
145 bm.setImmutable();
146
147 sk_sp<SkImage> image = SkImage::MakeFromBitmap(bm);
148 if (!image) {
149 return false;
150 }
151
152 filteredMask = proxyProvider->createTextureProxy(std::move(image),
153 kNone_GrSurfaceFlags,
154 1, SkBudgeted::kYes,
155 SkBackingFit::kApprox);
156 if (!filteredMask) {
157 return false;
158 }
159
Robert Phillips59b39e72018-08-30 11:51:25 -0400160 SkASSERT(kTopLeft_GrSurfaceOrigin == filteredMask->origin());
161
Robert Phillips31c080b2018-08-23 10:45:32 -0400162 drawRect = dstM.fBounds;
163
164 if (key.isValid()) {
165 proxyProvider->assignUniqueKeyToProxy(key, filteredMask.get());
166 }
Robert Phillips48ce22b2018-03-23 10:33:12 -0400167 }
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500168
Robert Phillips31c080b2018-08-23 10:45:32 -0400169 return draw_mask(renderTargetContext, clipData, viewMatrix, drawRect,
170 std::move(paint), std::move(filteredMask));
robertphillipsccb1b572015-05-27 11:02:55 -0700171}
172
Robert Phillips27927a52018-08-20 13:18:12 -0400173// Create a mask of 'shape' and place the result in 'mask'.
Robert Phillips7af8fe52019-02-14 17:27:00 -0500174static sk_sp<GrTextureProxy> create_mask_GPU(GrRecordingContext* context,
robertphillipsd728f0c2016-11-21 11:05:03 -0800175 const SkIRect& maskRect,
Robert Phillips27927a52018-08-20 13:18:12 -0400176 const SkMatrix& origViewMatrix,
177 const GrShape& shape,
robertphillipsd728f0c2016-11-21 11:05:03 -0800178 int sampleCnt) {
Greg Daniel4065d452018-11-16 15:43:41 -0500179 GrBackendFormat format =
Robert Phillips9da87e02019-02-04 13:26:26 -0500180 context->priv().caps()->getBackendFormatFromColorType(kAlpha_8_SkColorType);
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500181 sk_sp<GrRenderTargetContext> rtContext(
Robert Phillips9da87e02019-02-04 13:26:26 -0500182 context->priv().makeDeferredRenderTargetContextWithFallback(
Greg Daniel4065d452018-11-16 15:43:41 -0500183 format, SkBackingFit::kApprox, maskRect.width(), maskRect.height(),
184 kAlpha_8_GrPixelConfig, nullptr, sampleCnt, GrMipMapped::kNo,
185 kTopLeft_GrSurfaceOrigin));
robertphillipsd728f0c2016-11-21 11:05:03 -0800186 if (!rtContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700187 return nullptr;
robertphillipsccb1b572015-05-27 11:02:55 -0700188 }
189
Brian Osman9a9baae2018-11-05 15:06:26 -0500190 rtContext->priv().absClear(nullptr, SK_PMColor4fTRANSPARENT);
robertphillipsccb1b572015-05-27 11:02:55 -0700191
Brian Salomon82f44312017-01-11 13:42:54 -0500192 GrPaint maskPaint;
193 maskPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op);
robertphillipsccb1b572015-05-27 11:02:55 -0700194
195 // setup new clip
cdalton846c0512016-05-13 10:25:00 -0700196 const SkIRect clipRect = SkIRect::MakeWH(maskRect.width(), maskRect.height());
197 GrFixedClip clip(clipRect);
robertphillipsccb1b572015-05-27 11:02:55 -0700198
robertphillips3833daa2015-09-14 11:18:13 -0700199 // Draw the mask into maskTexture with the path's integerized top-left at
Brian Salomon82f44312017-01-11 13:42:54 -0500200 // the origin using maskPaint.
Robert Phillips27927a52018-08-20 13:18:12 -0400201 SkMatrix viewMatrix = origViewMatrix;
202 viewMatrix.postTranslate(-SkIntToScalar(maskRect.fLeft), -SkIntToScalar(maskRect.fTop));
Robert Phillips793324c2018-08-24 13:53:56 -0400203 rtContext->drawShape(clip, std::move(maskPaint), GrAA::kYes, viewMatrix, shape);
Robert Phillipsf200a902017-01-30 13:27:37 -0500204 return rtContext->asTextureProxyRef();
robertphillipsccb1b572015-05-27 11:02:55 -0700205}
206
Robert Phillips27927a52018-08-20 13:18:12 -0400207static bool get_unclipped_shape_dev_bounds(const GrShape& shape, const SkMatrix& matrix,
208 SkIRect* devBounds) {
209 SkRect shapeBounds = shape.styledBounds();
210 if (shapeBounds.isEmpty()) {
211 return false;
212 }
213 SkRect shapeDevBounds;
214 matrix.mapRect(&shapeDevBounds, shapeBounds);
215 // Even though these are "unclipped" bounds we still clip to the int32_t range.
216 // This is the largest int32_t that is representable exactly as a float. The next 63 larger ints
217 // would round down to this value when cast to a float, but who really cares.
218 // INT32_MIN is exactly representable.
219 static constexpr int32_t kMaxInt = 2147483520;
220 if (!shapeDevBounds.intersect(SkRect::MakeLTRB(INT32_MIN, INT32_MIN, kMaxInt, kMaxInt))) {
221 return false;
222 }
223 // Make sure that the resulting SkIRect can have representable width and height
224 if (SkScalarRoundToInt(shapeDevBounds.width()) > kMaxInt ||
225 SkScalarRoundToInt(shapeDevBounds.height()) > kMaxInt) {
226 return false;
227 }
228 shapeDevBounds.roundOut(devBounds);
229 return true;
230}
bsalomonc55271f2015-11-09 11:55:57 -0800231
Robert Phillips27927a52018-08-20 13:18:12 -0400232// Gets the shape bounds, the clip bounds, and the intersection (if any). Returns false if there
233// is no intersection.
234static bool get_shape_and_clip_bounds(GrRenderTargetContext* renderTargetContext,
235 const GrClip& clip,
236 const GrShape& shape,
237 const SkMatrix& matrix,
238 SkIRect* unclippedDevShapeBounds,
239 SkIRect* devClipBounds) {
240 // compute bounds as intersection of rt size, clip, and path
Robert Phillips784b7bf2016-12-09 13:35:02 -0500241 clip.getConservativeBounds(renderTargetContext->width(),
242 renderTargetContext->height(),
Robert Phillips27927a52018-08-20 13:18:12 -0400243 devClipBounds);
bsalomonc55271f2015-11-09 11:55:57 -0800244
Robert Phillips27927a52018-08-20 13:18:12 -0400245 if (!get_unclipped_shape_dev_bounds(shape, matrix, unclippedDevShapeBounds)) {
246 *unclippedDevShapeBounds = SkIRect::EmptyIRect();
247 return false;
bsalomonc55271f2015-11-09 11:55:57 -0800248 }
249
Robert Phillips27927a52018-08-20 13:18:12 -0400250 return true;
251}
252
Robert Phillips7af8fe52019-02-14 17:27:00 -0500253static void draw_shape_with_mask_filter(GrRecordingContext* context,
Robert Phillips27927a52018-08-20 13:18:12 -0400254 GrRenderTargetContext* renderTargetContext,
255 const GrClip& clip,
256 GrPaint&& paint,
Robert Phillips27927a52018-08-20 13:18:12 -0400257 const SkMatrix& viewMatrix,
258 const SkMaskFilterBase* maskFilter,
259 const GrShape& origShape) {
260 SkASSERT(maskFilter);
261
262 const GrShape* shape = &origShape;
263 SkTLazy<GrShape> tmpShape;
264
265 if (origShape.style().applies()) {
266 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
267 if (0 == styleScale) {
268 return;
bsalomon6663acf2016-05-10 09:14:17 -0700269 }
Robert Phillips27927a52018-08-20 13:18:12 -0400270
271 tmpShape.init(origShape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale));
272 if (tmpShape.get()->isEmpty()) {
273 return;
274 }
275
276 shape = tmpShape.get();
bsalomon6663acf2016-05-10 09:14:17 -0700277 }
bsalomonc55271f2015-11-09 11:55:57 -0800278
Robert Phillipsb889f202018-08-17 19:55:59 +0000279 if (maskFilter->directFilterMaskGPU(context,
280 renderTargetContext,
281 std::move(paint),
282 clip,
283 viewMatrix,
Robert Phillips27927a52018-08-20 13:18:12 -0400284 *shape)) {
Robert Phillipsb889f202018-08-17 19:55:59 +0000285 // the mask filter was able to draw itself directly, so there's nothing
286 // left to do.
287 return;
288 }
Mike Klein16885072018-12-11 09:54:31 -0500289 assert_alive(paint);
Robert Phillipsb889f202018-08-17 19:55:59 +0000290
Robert Phillips27927a52018-08-20 13:18:12 -0400291 // If the path is hairline, ignore inverse fill.
292 bool inverseFilled = shape->inverseFilled() &&
293 !GrPathRenderer::IsStrokeHairlineOrEquivalent(shape->style(),
294 viewMatrix, nullptr);
295
296 SkIRect unclippedDevShapeBounds, devClipBounds;
Robert Phillips31c080b2018-08-23 10:45:32 -0400297 if (!get_shape_and_clip_bounds(renderTargetContext, clip, *shape, viewMatrix,
Robert Phillips27927a52018-08-20 13:18:12 -0400298 &unclippedDevShapeBounds,
299 &devClipBounds)) {
300 // TODO: just cons up an opaque mask here
301 if (!inverseFilled) {
302 return;
303 }
304 }
305
Robert Phillips31c080b2018-08-23 10:45:32 -0400306 // To prevent overloading the cache with entries during animations we limit the cache of masks
307 // to cases where the matrix preserves axis alignment.
Robert Phillipsde479282018-08-28 11:20:08 -0400308#ifdef SK_DISABLE_MASKFILTERED_MASK_CACHING
309 bool useCache = false;
310#else
Robert Phillips31c080b2018-08-23 10:45:32 -0400311 bool useCache = !inverseFilled && viewMatrix.preservesAxisAlignment() &&
312 shape->hasUnstyledKey() && as_MFB(maskFilter)->asABlur(nullptr);
Robert Phillipsde479282018-08-28 11:20:08 -0400313#endif
Robert Phillips31c080b2018-08-23 10:45:32 -0400314
315 const SkIRect* boundsForClip = &devClipBounds;
316 if (useCache) {
317 SkIRect clippedMaskRect, unClippedMaskRect;
318 maskFilter->canFilterMaskGPU(*shape, unclippedDevShapeBounds, devClipBounds,
319 viewMatrix, &clippedMaskRect);
320 maskFilter->canFilterMaskGPU(*shape, unclippedDevShapeBounds, unclippedDevShapeBounds,
321 viewMatrix, &unClippedMaskRect);
322 if (clippedMaskRect.isEmpty()) {
323 return;
324 }
325
326 // Use the cache only if >50% of the filtered mask is visible.
327 int unclippedWidth = unClippedMaskRect.width();
328 int unclippedHeight = unClippedMaskRect.height();
329 int64_t unclippedArea = sk_64_mul(unclippedWidth, unclippedHeight);
330 int64_t clippedArea = sk_64_mul(clippedMaskRect.width(), clippedMaskRect.height());
331 int maxTextureSize = renderTargetContext->caps()->maxTextureSize();
332 if (unclippedArea > 2 * clippedArea || unclippedWidth > maxTextureSize ||
333 unclippedHeight > maxTextureSize) {
334 useCache = false;
335 } else {
336 // Make the clip not affect the mask
337 boundsForClip = &unclippedDevShapeBounds;
338 }
339 }
340
341 GrUniqueKey maskKey;
342 if (useCache) {
343 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
344 GrUniqueKey::Builder builder(&maskKey, kDomain, 5 + 2 + shape->unstyledKeySize(),
345 "Mask Filtered Masks");
346
347 // We require the upper left 2x2 of the matrix to match exactly for a cache hit.
348 SkScalar sx = viewMatrix.get(SkMatrix::kMScaleX);
349 SkScalar sy = viewMatrix.get(SkMatrix::kMScaleY);
350 SkScalar kx = viewMatrix.get(SkMatrix::kMSkewX);
351 SkScalar ky = viewMatrix.get(SkMatrix::kMSkewY);
352 SkScalar tx = viewMatrix.get(SkMatrix::kMTransX);
353 SkScalar ty = viewMatrix.get(SkMatrix::kMTransY);
354 // Allow 8 bits each in x and y of subpixel positioning.
355 SkFixed fracX = SkScalarToFixed(SkScalarFraction(tx)) & 0x0000FF00;
356 SkFixed fracY = SkScalarToFixed(SkScalarFraction(ty)) & 0x0000FF00;
357
358 builder[0] = SkFloat2Bits(sx);
359 builder[1] = SkFloat2Bits(sy);
360 builder[2] = SkFloat2Bits(kx);
361 builder[3] = SkFloat2Bits(ky);
362 // Distinguish between hairline and filled paths. For hairlines, we also need to include
363 // the cap. (SW grows hairlines by 0.5 pixel with round and square caps). Note that
364 // stroke-and-fill of hairlines is turned into pure fill by SkStrokeRec, so this covers
365 // all cases we might see.
366 uint32_t styleBits = shape->style().isSimpleHairline()
367 ? ((shape->style().strokeRec().getCap() << 1) | 1)
368 : 0;
369 builder[4] = fracX | (fracY >> 8) | (styleBits << 16);
370
371 SkMaskFilterBase::BlurRec rec;
372 SkAssertResult(as_MFB(maskFilter)->asABlur(&rec));
373
374 builder[5] = rec.fStyle; // TODO: we could put this with the other style bits
Robert Phillips55ff5d32019-01-07 16:11:36 -0500375 builder[6] = SkFloat2Bits(rec.fSigma);
Robert Phillips31c080b2018-08-23 10:45:32 -0400376 shape->writeUnstyledKey(&builder[7]);
377 }
378
379 SkIRect maskRect;
Robert Phillips27927a52018-08-20 13:18:12 -0400380 if (maskFilter->canFilterMaskGPU(*shape,
Robert Phillips31c080b2018-08-23 10:45:32 -0400381 unclippedDevShapeBounds,
382 *boundsForClip,
bsalomonc55271f2015-11-09 11:55:57 -0800383 viewMatrix,
384 &maskRect)) {
Robert Phillips31c080b2018-08-23 10:45:32 -0400385 if (clip_bounds_quick_reject(*boundsForClip, maskRect)) {
bsalomonc55271f2015-11-09 11:55:57 -0800386 // clipped out
387 return;
388 }
389
Robert Phillips20390c32018-08-17 11:01:03 -0400390 sk_sp<GrTextureProxy> filteredMask;
391
Robert Phillips9da87e02019-02-04 13:26:26 -0500392 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillips31c080b2018-08-23 10:45:32 -0400393
394 if (maskKey.isValid()) {
395 // TODO: this cache look up is duplicated in sw_draw_with_mask_filter for raster
396 filteredMask = proxyProvider->findOrCreateProxyByUniqueKey(
Robert Phillips59b39e72018-08-30 11:51:25 -0400397 maskKey, kTopLeft_GrSurfaceOrigin);
Robert Phillips31c080b2018-08-23 10:45:32 -0400398 }
399
Robert Phillips20390c32018-08-17 11:01:03 -0400400 if (!filteredMask) {
401 sk_sp<GrTextureProxy> maskProxy(create_mask_GPU(
402 context,
Robert Phillips31c080b2018-08-23 10:45:32 -0400403 maskRect,
Robert Phillips27927a52018-08-20 13:18:12 -0400404 viewMatrix,
405 *shape,
robertphillipsd728f0c2016-11-21 11:05:03 -0800406 renderTargetContext->numColorSamples()));
Robert Phillips20390c32018-08-17 11:01:03 -0400407 if (maskProxy) {
408 filteredMask = maskFilter->filterMaskGPU(context,
409 std::move(maskProxy),
410 viewMatrix,
Robert Phillips31c080b2018-08-23 10:45:32 -0400411 maskRect);
Robert Phillips59b39e72018-08-30 11:51:25 -0400412 SkASSERT(kTopLeft_GrSurfaceOrigin == filteredMask->origin());
413
Robert Phillips31c080b2018-08-23 10:45:32 -0400414 if (filteredMask && maskKey.isValid()) {
415 proxyProvider->assignUniqueKeyToProxy(maskKey, filteredMask.get());
416 }
bsalomonc55271f2015-11-09 11:55:57 -0800417 }
418 }
Robert Phillips20390c32018-08-17 11:01:03 -0400419
420 if (filteredMask) {
421 if (draw_mask(renderTargetContext, clip, viewMatrix,
Robert Phillips31c080b2018-08-23 10:45:32 -0400422 maskRect, std::move(paint), std::move(filteredMask))) {
Robert Phillips20390c32018-08-17 11:01:03 -0400423 // This path is completely drawn
424 return;
425 }
Mike Klein16885072018-12-11 09:54:31 -0500426 assert_alive(paint);
Robert Phillips20390c32018-08-17 11:01:03 -0400427 }
bsalomonc55271f2015-11-09 11:55:57 -0800428 }
429
Robert Phillips27927a52018-08-20 13:18:12 -0400430 sw_draw_with_mask_filter(context, renderTargetContext, clip, viewMatrix, *shape,
Robert Phillips31c080b2018-08-23 10:45:32 -0400431 maskFilter, *boundsForClip, std::move(paint), maskKey);
bsalomon6663acf2016-05-10 09:14:17 -0700432}
433
Robert Phillips7af8fe52019-02-14 17:27:00 -0500434void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext* context,
Robert Phillips27927a52018-08-20 13:18:12 -0400435 GrRenderTargetContext* renderTargetContext,
436 const GrClip& clip,
437 const GrShape& shape,
438 GrPaint&& paint,
Robert Phillips27927a52018-08-20 13:18:12 -0400439 const SkMatrix& viewMatrix,
440 const SkMaskFilter* mf) {
Robert Phillips793324c2018-08-24 13:53:56 -0400441 draw_shape_with_mask_filter(context, renderTargetContext, clip, std::move(paint),
Robert Phillips27927a52018-08-20 13:18:12 -0400442 viewMatrix, as_MFB(mf), shape);
bsalomonc55271f2015-11-09 11:55:57 -0800443}
444
Robert Phillips27927a52018-08-20 13:18:12 -0400445void GrBlurUtils::drawShapeWithMaskFilter(GrContext* context,
446 GrRenderTargetContext* renderTargetContext,
447 const GrClip& clip,
448 const SkPaint& paint,
449 const SkMatrix& viewMatrix,
450 const GrShape& shape) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500451 if (context->priv().abandoned()) {
Robert Phillipsbebfd412018-03-08 11:07:23 -0500452 return;
453 }
454
robertphillipsccb1b572015-05-27 11:02:55 -0700455 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400456 if (!SkPaintToGrPaint(context, renderTargetContext->colorSpaceInfo(), paint, viewMatrix,
457 &grPaint)) {
robertphillipsccb1b572015-05-27 11:02:55 -0700458 return;
459 }
Robert Phillips27927a52018-08-20 13:18:12 -0400460
Mike Reed80747ef2018-01-23 15:29:32 -0500461 SkMaskFilterBase* mf = as_MFB(paint.getMaskFilter());
Mike Reedbfadcf02018-01-20 22:24:21 +0000462 if (mf && !mf->hasFragmentProcessor()) {
Robert Phillipsa29a9562016-10-20 09:40:55 -0400463 // The MaskFilter wasn't already handled in SkPaintToGrPaint
Robert Phillips793324c2018-08-24 13:53:56 -0400464 draw_shape_with_mask_filter(context, renderTargetContext, clip, std::move(grPaint),
Robert Phillips27927a52018-08-20 13:18:12 -0400465 viewMatrix, mf, shape);
bsalomonc55271f2015-11-09 11:55:57 -0800466 } else {
Robert Phillips793324c2018-08-24 13:53:56 -0400467 GrAA aa = GrAA(paint.isAntiAlias());
Robert Phillips27927a52018-08-20 13:18:12 -0400468 renderTargetContext->drawShape(clip, std::move(grPaint), aa, viewMatrix, shape);
robertphillipsccb1b572015-05-27 11:02:55 -0700469 }
robertphillipsccb1b572015-05-27 11:02:55 -0700470}