blob: e3f92606310c498251cf0278f83d7a0fefa0304c [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/gpu/GrCaps.h"
12#include "src/gpu/GrFixedClip.h"
13#include "src/gpu/GrProxyProvider.h"
14#include "src/gpu/GrRecordingContextPriv.h"
15#include "src/gpu/GrRenderTargetContext.h"
16#include "src/gpu/GrRenderTargetContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/GrSoftwarePathRenderer.h"
18#include "src/gpu/GrStyle.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040019#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/effects/generated/GrSimpleTextureEffect.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040021#include "src/gpu/geometry/GrShape.h"
Robert Phillipsc7c2baf2018-03-08 09:51:04 -050022
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "include/core/SkPaint.h"
24#include "src/core/SkDraw.h"
25#include "src/core/SkMaskFilterBase.h"
26#include "src/core/SkTLazy.h"
27#include "src/gpu/SkGr.h"
robertphillipsccb1b572015-05-27 11:02:55 -070028
29static bool clip_bounds_quick_reject(const SkIRect& clipBounds, const SkIRect& rect) {
30 return clipBounds.isEmpty() || rect.isEmpty() || !SkIRect::Intersects(clipBounds, rect);
31}
32
33// Draw a mask using the supplied paint. Since the coverage/geometry
34// is already burnt into the mask this boils down to a rect draw.
35// Return true if the mask was successfully drawn.
Robert Phillips296b1cc2017-03-15 10:42:12 -040036static bool draw_mask(GrRenderTargetContext* renderTargetContext,
robertphillipsccb1b572015-05-27 11:02:55 -070037 const GrClip& clip,
38 const SkMatrix& viewMatrix,
robertphillipsf054b172016-05-13 05:06:19 -070039 const SkIRect& maskRect,
Brian Salomon82f44312017-01-11 13:42:54 -050040 GrPaint&& paint,
Brian Salomonfc118442019-11-22 19:09:27 -050041 sk_sp<GrTextureProxy> mask) {
Brian Salomon82f44312017-01-11 13:42:54 -050042 SkMatrix inverse;
43 if (!viewMatrix.invert(&inverse)) {
44 return false;
45 }
Robert Phillips4a24da52016-12-14 09:00:07 -050046
Robert Phillips67c18d62017-01-20 12:44:06 -050047 SkMatrix matrix = SkMatrix::MakeTrans(-SkIntToScalar(maskRect.fLeft),
48 -SkIntToScalar(maskRect.fTop));
Brian Salomon2ebd0c82016-10-03 17:15:28 -040049 matrix.preConcat(viewMatrix);
Brian Salomonfc118442019-11-22 19:09:27 -050050 paint.addCoverageFragmentProcessor(
51 GrSimpleTextureEffect::Make(std::move(mask), kUnknown_SkAlphaType, 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
Brian Salomon2af3e702019-08-11 19:10:31 -040084 filteredMask = proxyProvider->findOrCreateProxyByUniqueKey(key, GrColorType::kAlpha_8,
85 kTopLeft_GrSurfaceOrigin);
robertphillipsccb1b572015-05-27 11:02:55 -070086 }
87
Robert Phillips31c080b2018-08-23 10:45:32 -040088 SkIRect drawRect;
89 if (filteredMask) {
90 SkRect devBounds = shape.bounds();
91 viewMatrix.mapRect(&devBounds);
Robert Phillips4a24da52016-12-14 09:00:07 -050092
Robert Phillips31c080b2018-08-23 10:45:32 -040093 // Here we need to recompute the destination bounds in order to draw the mask correctly
94 SkMask srcM, dstM;
95 if (!SkDraw::ComputeMaskBounds(devBounds, &clipBounds, filter, &viewMatrix,
96 &srcM.fBounds)) {
97 return false;
98 }
99
100 srcM.fFormat = SkMask::kA8_Format;
101
102 if (!as_MFB(filter)->filterMask(&dstM, srcM, viewMatrix, nullptr)) {
103 return false;
104 }
105
106 // Unfortunately, we cannot double check that the computed bounds (i.e., dstM.fBounds)
107 // match the stored bounds of the mask bc the proxy may have been recreated and,
108 // when it is recreated, it just gets the bounds of the underlying GrTexture (which
109 // might be a loose fit).
110 drawRect = dstM.fBounds;
111 } else {
112 // TODO: it seems like we could create an SkDraw here and set its fMatrix field rather
113 // than explicitly transforming the path to device space.
114 SkPath devPath;
115
116 shape.asPath(&devPath);
117
118 devPath.transform(viewMatrix);
119
120 SkMask srcM, dstM;
121 if (!SkDraw::DrawToMask(devPath, &clipBounds, filter, &viewMatrix, &srcM,
122 SkMask::kComputeBoundsAndRenderImage_CreateMode, fillOrHairline)) {
123 return false;
124 }
125 SkAutoMaskFreeImage autoSrc(srcM.fImage);
126
127 SkASSERT(SkMask::kA8_Format == srcM.fFormat);
128
129 if (!as_MFB(filter)->filterMask(&dstM, srcM, viewMatrix, nullptr)) {
130 return false;
131 }
132 // this will free-up dstM when we're done (allocated in filterMask())
133 SkAutoMaskFreeImage autoDst(dstM.fImage);
134
135 if (clip_bounds_quick_reject(clipBounds, dstM.fBounds)) {
136 return false;
137 }
138
139 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
140 // the current clip (and identity matrix) and GrPaint settings
141 SkBitmap bm;
142 if (!bm.installPixels(SkImageInfo::MakeA8(dstM.fBounds.width(), dstM.fBounds.height()),
143 autoDst.release(), dstM.fRowBytes, mask_release_proc, nullptr)) {
144 return false;
145 }
146 bm.setImmutable();
147
148 sk_sp<SkImage> image = SkImage::MakeFromBitmap(bm);
149 if (!image) {
150 return false;
151 }
152
Brian Salomon96b383a2019-08-13 16:55:41 -0400153 filteredMask = proxyProvider->createTextureProxy(std::move(image), 1, SkBudgeted::kYes,
154 SkBackingFit::kApprox);
Robert Phillips31c080b2018-08-23 10:45:32 -0400155 if (!filteredMask) {
156 return false;
157 }
158
Robert Phillips59b39e72018-08-30 11:51:25 -0400159 SkASSERT(kTopLeft_GrSurfaceOrigin == filteredMask->origin());
160
Robert Phillips31c080b2018-08-23 10:45:32 -0400161 drawRect = dstM.fBounds;
162
163 if (key.isValid()) {
164 proxyProvider->assignUniqueKeyToProxy(key, filteredMask.get());
165 }
Robert Phillips48ce22b2018-03-23 10:33:12 -0400166 }
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500167
Brian Salomonfc118442019-11-22 19:09:27 -0500168 return draw_mask(renderTargetContext, clipData, viewMatrix, drawRect, std::move(paint),
169 std::move(filteredMask));
robertphillipsccb1b572015-05-27 11:02:55 -0700170}
171
Robert Phillips40b05c32019-09-20 12:40:55 -0400172// Create a mask of 'shape' and return the resulting renderTargetContext
173static std::unique_ptr<GrRenderTargetContext> create_mask_GPU(GrRecordingContext* context,
174 const SkIRect& maskRect,
175 const SkMatrix& origViewMatrix,
176 const GrShape& shape,
177 int sampleCnt) {
Chris Dalton0493fbd2019-09-18 15:49:46 -0600178 // Use GrResourceProvider::MakeApprox to implement our own approximate size matching, but demand
179 // a "SkBackingFit::kExact" size match on the actual render target. We do this because the
180 // filter will reach outside the src bounds, so we need to pre-clear these values to ensure a
181 // "decal" sampling effect (i.e., ensure reads outside the src bounds return alpha=0).
182 //
183 // FIXME: Reads outside the left and top edges will actually clamp to the edge pixel. And in the
184 // event that MakeApprox does not change the size, reads outside the right and/or bottom will do
185 // the same. We should offset our filter within the render target and expand the size as needed
186 // to guarantee at least 1px of padding on all sides.
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400187 auto approxSize = GrResourceProvider::MakeApprox(maskRect.size());
Brian Salomonbf6b9792019-08-21 09:38:10 -0400188 auto rtContext = context->priv().makeDeferredRenderTargetContextWithFallback(
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400189 SkBackingFit::kExact, approxSize.width(), approxSize.height(), GrColorType::kAlpha_8,
190 nullptr, sampleCnt, GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin);
robertphillipsd728f0c2016-11-21 11:05:03 -0800191 if (!rtContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700192 return nullptr;
robertphillipsccb1b572015-05-27 11:02:55 -0700193 }
194
Chris Dalton0493fbd2019-09-18 15:49:46 -0600195 rtContext->clear(SK_PMColor4fTRANSPARENT);
robertphillipsccb1b572015-05-27 11:02:55 -0700196
Brian Salomon82f44312017-01-11 13:42:54 -0500197 GrPaint maskPaint;
198 maskPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op);
robertphillipsccb1b572015-05-27 11:02:55 -0700199
200 // setup new clip
cdalton846c0512016-05-13 10:25:00 -0700201 const SkIRect clipRect = SkIRect::MakeWH(maskRect.width(), maskRect.height());
202 GrFixedClip clip(clipRect);
robertphillipsccb1b572015-05-27 11:02:55 -0700203
robertphillips3833daa2015-09-14 11:18:13 -0700204 // Draw the mask into maskTexture with the path's integerized top-left at
Brian Salomon82f44312017-01-11 13:42:54 -0500205 // the origin using maskPaint.
Robert Phillips27927a52018-08-20 13:18:12 -0400206 SkMatrix viewMatrix = origViewMatrix;
207 viewMatrix.postTranslate(-SkIntToScalar(maskRect.fLeft), -SkIntToScalar(maskRect.fTop));
Robert Phillips793324c2018-08-24 13:53:56 -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
Robert Phillips27927a52018-08-20 13:18:12 -0400212static bool get_unclipped_shape_dev_bounds(const GrShape& shape, const SkMatrix& matrix,
213 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,
240 const GrClip& clip,
241 const GrShape& shape,
242 const SkMatrix& matrix,
243 SkIRect* unclippedDevShapeBounds,
244 SkIRect* devClipBounds) {
245 // compute bounds as intersection of rt size, clip, and path
Robert Phillips784b7bf2016-12-09 13:35:02 -0500246 clip.getConservativeBounds(renderTargetContext->width(),
247 renderTargetContext->height(),
Robert Phillips27927a52018-08-20 13:18:12 -0400248 devClipBounds);
bsalomonc55271f2015-11-09 11:55:57 -0800249
Robert Phillips27927a52018-08-20 13:18:12 -0400250 if (!get_unclipped_shape_dev_bounds(shape, matrix, unclippedDevShapeBounds)) {
251 *unclippedDevShapeBounds = SkIRect::EmptyIRect();
252 return false;
bsalomonc55271f2015-11-09 11:55:57 -0800253 }
254
Robert Phillips27927a52018-08-20 13:18:12 -0400255 return true;
256}
257
Robert Phillips7af8fe52019-02-14 17:27:00 -0500258static void draw_shape_with_mask_filter(GrRecordingContext* context,
Robert Phillips27927a52018-08-20 13:18:12 -0400259 GrRenderTargetContext* renderTargetContext,
260 const GrClip& clip,
261 GrPaint&& paint,
Robert Phillips27927a52018-08-20 13:18:12 -0400262 const SkMatrix& viewMatrix,
263 const SkMaskFilterBase* maskFilter,
264 const GrShape& origShape) {
265 SkASSERT(maskFilter);
266
267 const GrShape* shape = &origShape;
268 SkTLazy<GrShape> tmpShape;
269
270 if (origShape.style().applies()) {
271 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
272 if (0 == styleScale) {
273 return;
bsalomon6663acf2016-05-10 09:14:17 -0700274 }
Robert Phillips27927a52018-08-20 13:18:12 -0400275
276 tmpShape.init(origShape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale));
277 if (tmpShape.get()->isEmpty()) {
278 return;
279 }
280
281 shape = tmpShape.get();
bsalomon6663acf2016-05-10 09:14:17 -0700282 }
bsalomonc55271f2015-11-09 11:55:57 -0800283
Robert Phillipsb889f202018-08-17 19:55:59 +0000284 if (maskFilter->directFilterMaskGPU(context,
285 renderTargetContext,
286 std::move(paint),
287 clip,
288 viewMatrix,
Robert Phillips27927a52018-08-20 13:18:12 -0400289 *shape)) {
Robert Phillipsb889f202018-08-17 19:55:59 +0000290 // the mask filter was able to draw itself directly, so there's nothing
291 // left to do.
292 return;
293 }
Mike Klein16885072018-12-11 09:54:31 -0500294 assert_alive(paint);
Robert Phillipsb889f202018-08-17 19:55:59 +0000295
Robert Phillips27927a52018-08-20 13:18:12 -0400296 // If the path is hairline, ignore inverse fill.
297 bool inverseFilled = shape->inverseFilled() &&
298 !GrPathRenderer::IsStrokeHairlineOrEquivalent(shape->style(),
299 viewMatrix, nullptr);
300
301 SkIRect unclippedDevShapeBounds, devClipBounds;
Robert Phillips31c080b2018-08-23 10:45:32 -0400302 if (!get_shape_and_clip_bounds(renderTargetContext, clip, *shape, viewMatrix,
Robert Phillips27927a52018-08-20 13:18:12 -0400303 &unclippedDevShapeBounds,
304 &devClipBounds)) {
305 // TODO: just cons up an opaque mask here
306 if (!inverseFilled) {
307 return;
308 }
309 }
310
Robert Phillips31c080b2018-08-23 10:45:32 -0400311 // To prevent overloading the cache with entries during animations we limit the cache of masks
312 // to cases where the matrix preserves axis alignment.
Robert Phillipsde479282018-08-28 11:20:08 -0400313#ifdef SK_DISABLE_MASKFILTERED_MASK_CACHING
314 bool useCache = false;
315#else
Robert Phillips31c080b2018-08-23 10:45:32 -0400316 bool useCache = !inverseFilled && viewMatrix.preservesAxisAlignment() &&
317 shape->hasUnstyledKey() && as_MFB(maskFilter)->asABlur(nullptr);
Robert Phillipsde479282018-08-28 11:20:08 -0400318#endif
Robert Phillips31c080b2018-08-23 10:45:32 -0400319
320 const SkIRect* boundsForClip = &devClipBounds;
321 if (useCache) {
322 SkIRect clippedMaskRect, unClippedMaskRect;
323 maskFilter->canFilterMaskGPU(*shape, unclippedDevShapeBounds, devClipBounds,
324 viewMatrix, &clippedMaskRect);
325 maskFilter->canFilterMaskGPU(*shape, unclippedDevShapeBounds, unclippedDevShapeBounds,
326 viewMatrix, &unClippedMaskRect);
327 if (clippedMaskRect.isEmpty()) {
328 return;
329 }
330
331 // Use the cache only if >50% of the filtered mask is visible.
332 int unclippedWidth = unClippedMaskRect.width();
333 int unclippedHeight = unClippedMaskRect.height();
334 int64_t unclippedArea = sk_64_mul(unclippedWidth, unclippedHeight);
335 int64_t clippedArea = sk_64_mul(clippedMaskRect.width(), clippedMaskRect.height());
336 int maxTextureSize = renderTargetContext->caps()->maxTextureSize();
337 if (unclippedArea > 2 * clippedArea || unclippedWidth > maxTextureSize ||
338 unclippedHeight > maxTextureSize) {
339 useCache = false;
340 } else {
341 // Make the clip not affect the mask
342 boundsForClip = &unclippedDevShapeBounds;
343 }
344 }
345
346 GrUniqueKey maskKey;
347 if (useCache) {
348 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
349 GrUniqueKey::Builder builder(&maskKey, kDomain, 5 + 2 + shape->unstyledKeySize(),
350 "Mask Filtered Masks");
351
352 // We require the upper left 2x2 of the matrix to match exactly for a cache hit.
353 SkScalar sx = viewMatrix.get(SkMatrix::kMScaleX);
354 SkScalar sy = viewMatrix.get(SkMatrix::kMScaleY);
355 SkScalar kx = viewMatrix.get(SkMatrix::kMSkewX);
356 SkScalar ky = viewMatrix.get(SkMatrix::kMSkewY);
357 SkScalar tx = viewMatrix.get(SkMatrix::kMTransX);
358 SkScalar ty = viewMatrix.get(SkMatrix::kMTransY);
359 // Allow 8 bits each in x and y of subpixel positioning.
360 SkFixed fracX = SkScalarToFixed(SkScalarFraction(tx)) & 0x0000FF00;
361 SkFixed fracY = SkScalarToFixed(SkScalarFraction(ty)) & 0x0000FF00;
362
363 builder[0] = SkFloat2Bits(sx);
364 builder[1] = SkFloat2Bits(sy);
365 builder[2] = SkFloat2Bits(kx);
366 builder[3] = SkFloat2Bits(ky);
367 // Distinguish between hairline and filled paths. For hairlines, we also need to include
368 // the cap. (SW grows hairlines by 0.5 pixel with round and square caps). Note that
369 // stroke-and-fill of hairlines is turned into pure fill by SkStrokeRec, so this covers
370 // all cases we might see.
371 uint32_t styleBits = shape->style().isSimpleHairline()
372 ? ((shape->style().strokeRec().getCap() << 1) | 1)
373 : 0;
374 builder[4] = fracX | (fracY >> 8) | (styleBits << 16);
375
376 SkMaskFilterBase::BlurRec rec;
377 SkAssertResult(as_MFB(maskFilter)->asABlur(&rec));
378
379 builder[5] = rec.fStyle; // TODO: we could put this with the other style bits
Robert Phillips55ff5d32019-01-07 16:11:36 -0500380 builder[6] = SkFloat2Bits(rec.fSigma);
Robert Phillips31c080b2018-08-23 10:45:32 -0400381 shape->writeUnstyledKey(&builder[7]);
382 }
383
384 SkIRect maskRect;
Robert Phillips27927a52018-08-20 13:18:12 -0400385 if (maskFilter->canFilterMaskGPU(*shape,
Robert Phillips31c080b2018-08-23 10:45:32 -0400386 unclippedDevShapeBounds,
387 *boundsForClip,
bsalomonc55271f2015-11-09 11:55:57 -0800388 viewMatrix,
389 &maskRect)) {
Robert Phillips31c080b2018-08-23 10:45:32 -0400390 if (clip_bounds_quick_reject(*boundsForClip, maskRect)) {
bsalomonc55271f2015-11-09 11:55:57 -0800391 // clipped out
392 return;
393 }
394
Robert Phillips20390c32018-08-17 11:01:03 -0400395 sk_sp<GrTextureProxy> filteredMask;
396
Robert Phillips9da87e02019-02-04 13:26:26 -0500397 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillips31c080b2018-08-23 10:45:32 -0400398
399 if (maskKey.isValid()) {
400 // TODO: this cache look up is duplicated in sw_draw_with_mask_filter for raster
401 filteredMask = proxyProvider->findOrCreateProxyByUniqueKey(
Brian Salomon2af3e702019-08-11 19:10:31 -0400402 maskKey, GrColorType::kAlpha_8, kTopLeft_GrSurfaceOrigin);
Robert Phillips31c080b2018-08-23 10:45:32 -0400403 }
404
Robert Phillips20390c32018-08-17 11:01:03 -0400405 if (!filteredMask) {
Robert Phillips40b05c32019-09-20 12:40:55 -0400406 std::unique_ptr<GrRenderTargetContext> maskRTC(create_mask_GPU(
Robert Phillips20390c32018-08-17 11:01:03 -0400407 context,
Robert Phillips31c080b2018-08-23 10:45:32 -0400408 maskRect,
Robert Phillips27927a52018-08-20 13:18:12 -0400409 viewMatrix,
410 *shape,
Chris Dalton6ce447a2019-06-23 18:07:38 -0600411 renderTargetContext->numSamples()));
Robert Phillips40b05c32019-09-20 12:40:55 -0400412 if (maskRTC) {
Robert Phillips20390c32018-08-17 11:01:03 -0400413 filteredMask = maskFilter->filterMaskGPU(context,
Robert Phillips40b05c32019-09-20 12:40:55 -0400414 maskRTC->asTextureProxyRef(),
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400415 maskRTC->colorInfo().colorType(),
416 maskRTC->colorInfo().alphaType(),
Robert Phillips20390c32018-08-17 11:01:03 -0400417 viewMatrix,
Robert Phillips31c080b2018-08-23 10:45:32 -0400418 maskRect);
Robert Phillips59b39e72018-08-30 11:51:25 -0400419 SkASSERT(kTopLeft_GrSurfaceOrigin == filteredMask->origin());
420
Robert Phillips31c080b2018-08-23 10:45:32 -0400421 if (filteredMask && maskKey.isValid()) {
422 proxyProvider->assignUniqueKeyToProxy(maskKey, filteredMask.get());
423 }
bsalomonc55271f2015-11-09 11:55:57 -0800424 }
425 }
Robert Phillips20390c32018-08-17 11:01:03 -0400426
427 if (filteredMask) {
Brian Salomonfc118442019-11-22 19:09:27 -0500428 if (draw_mask(renderTargetContext, clip, viewMatrix, maskRect, std::move(paint),
429 std::move(filteredMask))) {
Robert Phillips20390c32018-08-17 11:01:03 -0400430 // This path is completely drawn
431 return;
432 }
Mike Klein16885072018-12-11 09:54:31 -0500433 assert_alive(paint);
Robert Phillips20390c32018-08-17 11:01:03 -0400434 }
bsalomonc55271f2015-11-09 11:55:57 -0800435 }
436
Robert Phillips27927a52018-08-20 13:18:12 -0400437 sw_draw_with_mask_filter(context, renderTargetContext, clip, viewMatrix, *shape,
Robert Phillips31c080b2018-08-23 10:45:32 -0400438 maskFilter, *boundsForClip, std::move(paint), maskKey);
bsalomon6663acf2016-05-10 09:14:17 -0700439}
440
Robert Phillips7af8fe52019-02-14 17:27:00 -0500441void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext* context,
Robert Phillips27927a52018-08-20 13:18:12 -0400442 GrRenderTargetContext* renderTargetContext,
443 const GrClip& clip,
444 const GrShape& shape,
445 GrPaint&& paint,
Robert Phillips27927a52018-08-20 13:18:12 -0400446 const SkMatrix& viewMatrix,
447 const SkMaskFilter* mf) {
Robert Phillips793324c2018-08-24 13:53:56 -0400448 draw_shape_with_mask_filter(context, renderTargetContext, clip, std::move(paint),
Robert Phillips27927a52018-08-20 13:18:12 -0400449 viewMatrix, as_MFB(mf), shape);
bsalomonc55271f2015-11-09 11:55:57 -0800450}
451
Robert Phillips69893702019-02-22 11:16:30 -0500452void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext* context,
Robert Phillips27927a52018-08-20 13:18:12 -0400453 GrRenderTargetContext* renderTargetContext,
454 const GrClip& clip,
455 const SkPaint& paint,
456 const SkMatrix& viewMatrix,
457 const GrShape& shape) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500458 if (context->priv().abandoned()) {
Robert Phillipsbebfd412018-03-08 11:07:23 -0500459 return;
460 }
461
robertphillipsccb1b572015-05-27 11:02:55 -0700462 GrPaint grPaint;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400463 if (!SkPaintToGrPaint(context, renderTargetContext->colorInfo(), paint, viewMatrix, &grPaint)) {
robertphillipsccb1b572015-05-27 11:02:55 -0700464 return;
465 }
Robert Phillips27927a52018-08-20 13:18:12 -0400466
Mike Reed80747ef2018-01-23 15:29:32 -0500467 SkMaskFilterBase* mf = as_MFB(paint.getMaskFilter());
Mike Reedbfadcf02018-01-20 22:24:21 +0000468 if (mf && !mf->hasFragmentProcessor()) {
Robert Phillipsa29a9562016-10-20 09:40:55 -0400469 // The MaskFilter wasn't already handled in SkPaintToGrPaint
Robert Phillips793324c2018-08-24 13:53:56 -0400470 draw_shape_with_mask_filter(context, renderTargetContext, clip, std::move(grPaint),
Robert Phillips27927a52018-08-20 13:18:12 -0400471 viewMatrix, mf, shape);
bsalomonc55271f2015-11-09 11:55:57 -0800472 } else {
Robert Phillips793324c2018-08-24 13:53:56 -0400473 GrAA aa = GrAA(paint.isAntiAlias());
Robert Phillips27927a52018-08-20 13:18:12 -0400474 renderTargetContext->drawShape(clip, std::move(grPaint), aa, viewMatrix, shape);
robertphillipsccb1b572015-05-27 11:02:55 -0700475 }
robertphillipsccb1b572015-05-27 11:02:55 -0700476}