blob: 2bb1edfe57cc47221c8931f87d793b7cf9b71ca8 [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 Ludwig663afe52019-06-03 16:46:19 -040022#include "src/gpu/geometry/GrShape.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"
27#include "src/core/SkTLazy.h"
28#include "src/gpu/SkGr.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,
Greg Daniel5c082492020-01-29 15:06:49 -050042 GrSurfaceProxyView 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 Salomonfc118442019-11-22 19:09:27 -050051 paint.addCoverageFragmentProcessor(
Greg Daniel5c082492020-01-29 15:06:49 -050052 GrTextureEffect::Make(mask.detachProxy(), kUnknown_SkAlphaType, matrix));
robertphillipsccb1b572015-05-27 11:02:55 -070053
Brian Salomon82f44312017-01-11 13:42:54 -050054 renderTargetContext->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050055 SkRect::Make(maskRect), inverse);
robertphillipsccb1b572015-05-27 11:02:55 -070056 return true;
57}
58
Robert Phillipsc7c2baf2018-03-08 09:51:04 -050059static void mask_release_proc(void* addr, void* /*context*/) {
60 SkMask::FreeImage(addr);
61}
62
Robert Phillips7af8fe52019-02-14 17:27:00 -050063static bool sw_draw_with_mask_filter(GrRecordingContext* context,
Robert Phillips4a24da52016-12-14 09:00:07 -050064 GrRenderTargetContext* renderTargetContext,
robertphillips0e7029e2015-11-30 05:45:06 -080065 const GrClip& clipData,
66 const SkMatrix& viewMatrix,
Robert Phillips27927a52018-08-20 13:18:12 -040067 const GrShape& shape,
robertphillips0e7029e2015-11-30 05:45:06 -080068 const SkMaskFilter* filter,
69 const SkIRect& clipBounds,
Robert Phillips31c080b2018-08-23 10:45:32 -040070 GrPaint&& paint,
71 const GrUniqueKey& key) {
Robert Phillips27927a52018-08-20 13:18:12 -040072 SkASSERT(filter);
73 SkASSERT(!shape.style().applies());
74
Robert Phillips9da87e02019-02-04 13:26:26 -050075 auto proxyProvider = context->priv().proxyProvider();
Robert Phillips27927a52018-08-20 13:18:12 -040076
Greg Daniel5c082492020-01-29 15:06:49 -050077 GrSurfaceProxyView filteredMaskView;
Robert Phillips27927a52018-08-20 13:18:12 -040078
79 SkStrokeRec::InitStyle fillOrHairline = shape.style().isSimpleHairline()
80 ? SkStrokeRec::kHairline_InitStyle
81 : SkStrokeRec::kFill_InitStyle;
82
Robert Phillips31c080b2018-08-23 10:45:32 -040083 if (key.isValid()) {
84 // TODO: this cache look up is duplicated in draw_shape_with_mask_filter for gpu
Greg Daniel5c082492020-01-29 15:06:49 -050085 static const GrSurfaceOrigin kCacheOrigin = kTopLeft_GrSurfaceOrigin;
86 auto filteredMask = proxyProvider->findOrCreateProxyByUniqueKey(key, GrColorType::kAlpha_8,
87 kCacheOrigin);
88 if (filteredMask) {
89 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(
90 filteredMask->backendFormat(), GrColorType::kAlpha_8);
91 filteredMaskView = GrSurfaceProxyView(std::move(filteredMask), kCacheOrigin, swizzle);
92 }
robertphillipsccb1b572015-05-27 11:02:55 -070093 }
94
Robert Phillips31c080b2018-08-23 10:45:32 -040095 SkIRect drawRect;
Greg Daniel5c082492020-01-29 15:06:49 -050096 if (filteredMaskView.proxy()) {
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
Greg Daniel6f5441a2020-01-28 17:02:49 -0500155 GrBitmapTextureMaker maker(context, bm, GrBitmapTextureMaker::Cached::kNo,
156 SkBackingFit::kApprox);
Greg Daniel5c082492020-01-29 15:06:49 -0500157 auto [filteredMask, grCT] = maker.refTextureProxy(GrMipMapped::kNo);
Robert Phillips31c080b2018-08-23 10:45:32 -0400158 if (!filteredMask) {
159 return false;
160 }
161
Greg Daniel5c082492020-01-29 15:06:49 -0500162 // TODO: refTextureProxy should return a view instead of a proxy
Robert Phillips59b39e72018-08-30 11:51:25 -0400163 SkASSERT(kTopLeft_GrSurfaceOrigin == filteredMask->origin());
164
Robert Phillips31c080b2018-08-23 10:45:32 -0400165 drawRect = dstM.fBounds;
166
167 if (key.isValid()) {
168 proxyProvider->assignUniqueKeyToProxy(key, filteredMask.get());
169 }
Greg Daniel5c082492020-01-29 15:06:49 -0500170 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(filteredMask->backendFormat(),
171 grCT);
172 filteredMaskView = GrSurfaceProxyView(std::move(filteredMask), kTopLeft_GrSurfaceOrigin,
173 swizzle);
Robert Phillips48ce22b2018-03-23 10:33:12 -0400174 }
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500175
Brian Salomonfc118442019-11-22 19:09:27 -0500176 return draw_mask(renderTargetContext, clipData, viewMatrix, drawRect, std::move(paint),
Greg Daniel5c082492020-01-29 15:06:49 -0500177 std::move(filteredMaskView));
robertphillipsccb1b572015-05-27 11:02:55 -0700178}
179
Robert Phillips40b05c32019-09-20 12:40:55 -0400180// Create a mask of 'shape' and return the resulting renderTargetContext
181static std::unique_ptr<GrRenderTargetContext> create_mask_GPU(GrRecordingContext* context,
182 const SkIRect& maskRect,
183 const SkMatrix& origViewMatrix,
184 const GrShape& shape,
185 int sampleCnt) {
Chris Dalton0493fbd2019-09-18 15:49:46 -0600186 // Use GrResourceProvider::MakeApprox to implement our own approximate size matching, but demand
187 // a "SkBackingFit::kExact" size match on the actual render target. We do this because the
188 // filter will reach outside the src bounds, so we need to pre-clear these values to ensure a
189 // "decal" sampling effect (i.e., ensure reads outside the src bounds return alpha=0).
190 //
191 // FIXME: Reads outside the left and top edges will actually clamp to the edge pixel. And in the
192 // event that MakeApprox does not change the size, reads outside the right and/or bottom will do
193 // the same. We should offset our filter within the render target and expand the size as needed
194 // to guarantee at least 1px of padding on all sides.
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400195 auto approxSize = GrResourceProvider::MakeApprox(maskRect.size());
Greg Daniele20fcad2020-01-08 11:52:34 -0500196 auto rtContext = GrRenderTargetContext::MakeWithFallback(
197 context, GrColorType::kAlpha_8, nullptr, SkBackingFit::kExact, approxSize, sampleCnt,
198 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
robertphillipsd728f0c2016-11-21 11:05:03 -0800199 if (!rtContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700200 return nullptr;
robertphillipsccb1b572015-05-27 11:02:55 -0700201 }
202
Chris Dalton0493fbd2019-09-18 15:49:46 -0600203 rtContext->clear(SK_PMColor4fTRANSPARENT);
robertphillipsccb1b572015-05-27 11:02:55 -0700204
Brian Salomon82f44312017-01-11 13:42:54 -0500205 GrPaint maskPaint;
206 maskPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op);
robertphillipsccb1b572015-05-27 11:02:55 -0700207
208 // setup new clip
cdalton846c0512016-05-13 10:25:00 -0700209 const SkIRect clipRect = SkIRect::MakeWH(maskRect.width(), maskRect.height());
210 GrFixedClip clip(clipRect);
robertphillipsccb1b572015-05-27 11:02:55 -0700211
robertphillips3833daa2015-09-14 11:18:13 -0700212 // Draw the mask into maskTexture with the path's integerized top-left at
Brian Salomon82f44312017-01-11 13:42:54 -0500213 // the origin using maskPaint.
Robert Phillips27927a52018-08-20 13:18:12 -0400214 SkMatrix viewMatrix = origViewMatrix;
215 viewMatrix.postTranslate(-SkIntToScalar(maskRect.fLeft), -SkIntToScalar(maskRect.fTop));
Robert Phillips793324c2018-08-24 13:53:56 -0400216 rtContext->drawShape(clip, std::move(maskPaint), GrAA::kYes, viewMatrix, shape);
Robert Phillips40b05c32019-09-20 12:40:55 -0400217 return rtContext;
robertphillipsccb1b572015-05-27 11:02:55 -0700218}
219
Robert Phillips27927a52018-08-20 13:18:12 -0400220static bool get_unclipped_shape_dev_bounds(const GrShape& shape, const SkMatrix& matrix,
221 SkIRect* devBounds) {
222 SkRect shapeBounds = shape.styledBounds();
223 if (shapeBounds.isEmpty()) {
224 return false;
225 }
226 SkRect shapeDevBounds;
227 matrix.mapRect(&shapeDevBounds, shapeBounds);
228 // Even though these are "unclipped" bounds we still clip to the int32_t range.
229 // This is the largest int32_t that is representable exactly as a float. The next 63 larger ints
230 // would round down to this value when cast to a float, but who really cares.
231 // INT32_MIN is exactly representable.
232 static constexpr int32_t kMaxInt = 2147483520;
233 if (!shapeDevBounds.intersect(SkRect::MakeLTRB(INT32_MIN, INT32_MIN, kMaxInt, kMaxInt))) {
234 return false;
235 }
236 // Make sure that the resulting SkIRect can have representable width and height
237 if (SkScalarRoundToInt(shapeDevBounds.width()) > kMaxInt ||
238 SkScalarRoundToInt(shapeDevBounds.height()) > kMaxInt) {
239 return false;
240 }
241 shapeDevBounds.roundOut(devBounds);
242 return true;
243}
bsalomonc55271f2015-11-09 11:55:57 -0800244
Robert Phillips27927a52018-08-20 13:18:12 -0400245// Gets the shape bounds, the clip bounds, and the intersection (if any). Returns false if there
246// is no intersection.
247static bool get_shape_and_clip_bounds(GrRenderTargetContext* renderTargetContext,
248 const GrClip& clip,
249 const GrShape& shape,
250 const SkMatrix& matrix,
251 SkIRect* unclippedDevShapeBounds,
252 SkIRect* devClipBounds) {
253 // compute bounds as intersection of rt size, clip, and path
Robert Phillips784b7bf2016-12-09 13:35:02 -0500254 clip.getConservativeBounds(renderTargetContext->width(),
255 renderTargetContext->height(),
Robert Phillips27927a52018-08-20 13:18:12 -0400256 devClipBounds);
bsalomonc55271f2015-11-09 11:55:57 -0800257
Robert Phillips27927a52018-08-20 13:18:12 -0400258 if (!get_unclipped_shape_dev_bounds(shape, matrix, unclippedDevShapeBounds)) {
Brian Salomon44207f32020-01-06 15:20:18 -0500259 *unclippedDevShapeBounds = SkIRect::MakeEmpty();
Robert Phillips27927a52018-08-20 13:18:12 -0400260 return false;
bsalomonc55271f2015-11-09 11:55:57 -0800261 }
262
Robert Phillips27927a52018-08-20 13:18:12 -0400263 return true;
264}
265
Robert Phillips7af8fe52019-02-14 17:27:00 -0500266static void draw_shape_with_mask_filter(GrRecordingContext* context,
Robert Phillips27927a52018-08-20 13:18:12 -0400267 GrRenderTargetContext* renderTargetContext,
268 const GrClip& clip,
269 GrPaint&& paint,
Robert Phillips27927a52018-08-20 13:18:12 -0400270 const SkMatrix& viewMatrix,
271 const SkMaskFilterBase* maskFilter,
272 const GrShape& origShape) {
273 SkASSERT(maskFilter);
274
275 const GrShape* shape = &origShape;
276 SkTLazy<GrShape> tmpShape;
277
278 if (origShape.style().applies()) {
279 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
280 if (0 == styleScale) {
281 return;
bsalomon6663acf2016-05-10 09:14:17 -0700282 }
Robert Phillips27927a52018-08-20 13:18:12 -0400283
284 tmpShape.init(origShape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale));
285 if (tmpShape.get()->isEmpty()) {
286 return;
287 }
288
289 shape = tmpShape.get();
bsalomon6663acf2016-05-10 09:14:17 -0700290 }
bsalomonc55271f2015-11-09 11:55:57 -0800291
Robert Phillipsb889f202018-08-17 19:55:59 +0000292 if (maskFilter->directFilterMaskGPU(context,
293 renderTargetContext,
294 std::move(paint),
295 clip,
296 viewMatrix,
Robert Phillips27927a52018-08-20 13:18:12 -0400297 *shape)) {
Robert Phillipsb889f202018-08-17 19:55:59 +0000298 // the mask filter was able to draw itself directly, so there's nothing
299 // left to do.
300 return;
301 }
Mike Klein16885072018-12-11 09:54:31 -0500302 assert_alive(paint);
Robert Phillipsb889f202018-08-17 19:55:59 +0000303
Robert Phillips27927a52018-08-20 13:18:12 -0400304 // If the path is hairline, ignore inverse fill.
305 bool inverseFilled = shape->inverseFilled() &&
306 !GrPathRenderer::IsStrokeHairlineOrEquivalent(shape->style(),
307 viewMatrix, nullptr);
308
309 SkIRect unclippedDevShapeBounds, devClipBounds;
Robert Phillips31c080b2018-08-23 10:45:32 -0400310 if (!get_shape_and_clip_bounds(renderTargetContext, clip, *shape, viewMatrix,
Robert Phillips27927a52018-08-20 13:18:12 -0400311 &unclippedDevShapeBounds,
312 &devClipBounds)) {
313 // TODO: just cons up an opaque mask here
314 if (!inverseFilled) {
315 return;
316 }
317 }
318
Robert Phillips31c080b2018-08-23 10:45:32 -0400319 // To prevent overloading the cache with entries during animations we limit the cache of masks
320 // to cases where the matrix preserves axis alignment.
Robert Phillipsde479282018-08-28 11:20:08 -0400321#ifdef SK_DISABLE_MASKFILTERED_MASK_CACHING
322 bool useCache = false;
323#else
Robert Phillips31c080b2018-08-23 10:45:32 -0400324 bool useCache = !inverseFilled && viewMatrix.preservesAxisAlignment() &&
325 shape->hasUnstyledKey() && as_MFB(maskFilter)->asABlur(nullptr);
Robert Phillipsde479282018-08-28 11:20:08 -0400326#endif
Robert Phillips31c080b2018-08-23 10:45:32 -0400327
328 const SkIRect* boundsForClip = &devClipBounds;
329 if (useCache) {
330 SkIRect clippedMaskRect, unClippedMaskRect;
331 maskFilter->canFilterMaskGPU(*shape, unclippedDevShapeBounds, devClipBounds,
332 viewMatrix, &clippedMaskRect);
333 maskFilter->canFilterMaskGPU(*shape, unclippedDevShapeBounds, unclippedDevShapeBounds,
334 viewMatrix, &unClippedMaskRect);
335 if (clippedMaskRect.isEmpty()) {
336 return;
337 }
338
339 // Use the cache only if >50% of the filtered mask is visible.
340 int unclippedWidth = unClippedMaskRect.width();
341 int unclippedHeight = unClippedMaskRect.height();
342 int64_t unclippedArea = sk_64_mul(unclippedWidth, unclippedHeight);
343 int64_t clippedArea = sk_64_mul(clippedMaskRect.width(), clippedMaskRect.height());
344 int maxTextureSize = renderTargetContext->caps()->maxTextureSize();
345 if (unclippedArea > 2 * clippedArea || unclippedWidth > maxTextureSize ||
346 unclippedHeight > maxTextureSize) {
347 useCache = false;
348 } else {
349 // Make the clip not affect the mask
350 boundsForClip = &unclippedDevShapeBounds;
351 }
352 }
353
354 GrUniqueKey maskKey;
355 if (useCache) {
356 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
357 GrUniqueKey::Builder builder(&maskKey, kDomain, 5 + 2 + shape->unstyledKeySize(),
358 "Mask Filtered Masks");
359
360 // We require the upper left 2x2 of the matrix to match exactly for a cache hit.
361 SkScalar sx = viewMatrix.get(SkMatrix::kMScaleX);
362 SkScalar sy = viewMatrix.get(SkMatrix::kMScaleY);
363 SkScalar kx = viewMatrix.get(SkMatrix::kMSkewX);
364 SkScalar ky = viewMatrix.get(SkMatrix::kMSkewY);
365 SkScalar tx = viewMatrix.get(SkMatrix::kMTransX);
366 SkScalar ty = viewMatrix.get(SkMatrix::kMTransY);
367 // Allow 8 bits each in x and y of subpixel positioning.
368 SkFixed fracX = SkScalarToFixed(SkScalarFraction(tx)) & 0x0000FF00;
369 SkFixed fracY = SkScalarToFixed(SkScalarFraction(ty)) & 0x0000FF00;
370
371 builder[0] = SkFloat2Bits(sx);
372 builder[1] = SkFloat2Bits(sy);
373 builder[2] = SkFloat2Bits(kx);
374 builder[3] = SkFloat2Bits(ky);
375 // Distinguish between hairline and filled paths. For hairlines, we also need to include
376 // the cap. (SW grows hairlines by 0.5 pixel with round and square caps). Note that
377 // stroke-and-fill of hairlines is turned into pure fill by SkStrokeRec, so this covers
378 // all cases we might see.
379 uint32_t styleBits = shape->style().isSimpleHairline()
380 ? ((shape->style().strokeRec().getCap() << 1) | 1)
381 : 0;
382 builder[4] = fracX | (fracY >> 8) | (styleBits << 16);
383
384 SkMaskFilterBase::BlurRec rec;
385 SkAssertResult(as_MFB(maskFilter)->asABlur(&rec));
386
387 builder[5] = rec.fStyle; // TODO: we could put this with the other style bits
Robert Phillips55ff5d32019-01-07 16:11:36 -0500388 builder[6] = SkFloat2Bits(rec.fSigma);
Robert Phillips31c080b2018-08-23 10:45:32 -0400389 shape->writeUnstyledKey(&builder[7]);
390 }
391
392 SkIRect maskRect;
Robert Phillips27927a52018-08-20 13:18:12 -0400393 if (maskFilter->canFilterMaskGPU(*shape,
Robert Phillips31c080b2018-08-23 10:45:32 -0400394 unclippedDevShapeBounds,
395 *boundsForClip,
bsalomonc55271f2015-11-09 11:55:57 -0800396 viewMatrix,
397 &maskRect)) {
Robert Phillips31c080b2018-08-23 10:45:32 -0400398 if (clip_bounds_quick_reject(*boundsForClip, maskRect)) {
bsalomonc55271f2015-11-09 11:55:57 -0800399 // clipped out
400 return;
401 }
402
Greg Daniel5c082492020-01-29 15:06:49 -0500403 GrSurfaceProxyView filteredMaskView;
Robert Phillips20390c32018-08-17 11:01:03 -0400404
Robert Phillips9da87e02019-02-04 13:26:26 -0500405 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillips31c080b2018-08-23 10:45:32 -0400406
407 if (maskKey.isValid()) {
408 // TODO: this cache look up is duplicated in sw_draw_with_mask_filter for raster
Greg Daniel5c082492020-01-29 15:06:49 -0500409 static const GrSurfaceOrigin kCacheOrigin = kTopLeft_GrSurfaceOrigin;
410 auto filteredMask = proxyProvider->findOrCreateProxyByUniqueKey(
411 maskKey, GrColorType::kAlpha_8, kCacheOrigin);
412 if (filteredMask) {
413 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(
414 filteredMask->backendFormat(), GrColorType::kAlpha_8);
415 filteredMaskView = GrSurfaceProxyView(std::move(filteredMask), kCacheOrigin,
416 swizzle);
417 }
Robert Phillips31c080b2018-08-23 10:45:32 -0400418 }
419
Greg Daniel5c082492020-01-29 15:06:49 -0500420 if (!filteredMaskView.proxy()) {
Robert Phillips40b05c32019-09-20 12:40:55 -0400421 std::unique_ptr<GrRenderTargetContext> maskRTC(create_mask_GPU(
Greg Daniel5c082492020-01-29 15:06:49 -0500422 context,
423 maskRect,
424 viewMatrix,
425 *shape,
426 renderTargetContext->numSamples()));
Robert Phillips40b05c32019-09-20 12:40:55 -0400427 if (maskRTC) {
Greg Daniel5c082492020-01-29 15:06:49 -0500428 filteredMaskView = maskFilter->filterMaskGPU(context,
429 maskRTC->readSurfaceView(),
430 maskRTC->colorInfo().colorType(),
431 maskRTC->colorInfo().alphaType(),
432 viewMatrix,
433 maskRect);
434 if (filteredMaskView.proxy() && maskKey.isValid()) {
435 SkASSERT(filteredMaskView.asTextureProxy());
436 proxyProvider->assignUniqueKeyToProxy(maskKey,
437 filteredMaskView.asTextureProxy());
Robert Phillips31c080b2018-08-23 10:45:32 -0400438 }
bsalomonc55271f2015-11-09 11:55:57 -0800439 }
440 }
Robert Phillips20390c32018-08-17 11:01:03 -0400441
Greg Daniel5c082492020-01-29 15:06:49 -0500442 if (filteredMaskView.proxy()) {
Brian Salomonfc118442019-11-22 19:09:27 -0500443 if (draw_mask(renderTargetContext, clip, viewMatrix, maskRect, std::move(paint),
Greg Daniel5c082492020-01-29 15:06:49 -0500444 std::move(filteredMaskView))) {
Robert Phillips20390c32018-08-17 11:01:03 -0400445 // This path is completely drawn
446 return;
447 }
Mike Klein16885072018-12-11 09:54:31 -0500448 assert_alive(paint);
Robert Phillips20390c32018-08-17 11:01:03 -0400449 }
bsalomonc55271f2015-11-09 11:55:57 -0800450 }
451
Robert Phillips27927a52018-08-20 13:18:12 -0400452 sw_draw_with_mask_filter(context, renderTargetContext, clip, viewMatrix, *shape,
Robert Phillips31c080b2018-08-23 10:45:32 -0400453 maskFilter, *boundsForClip, std::move(paint), maskKey);
bsalomon6663acf2016-05-10 09:14:17 -0700454}
455
Robert Phillips7af8fe52019-02-14 17:27:00 -0500456void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext* context,
Robert Phillips27927a52018-08-20 13:18:12 -0400457 GrRenderTargetContext* renderTargetContext,
458 const GrClip& clip,
459 const GrShape& shape,
460 GrPaint&& paint,
Robert Phillips27927a52018-08-20 13:18:12 -0400461 const SkMatrix& viewMatrix,
462 const SkMaskFilter* mf) {
Robert Phillips793324c2018-08-24 13:53:56 -0400463 draw_shape_with_mask_filter(context, renderTargetContext, clip, std::move(paint),
Robert Phillips27927a52018-08-20 13:18:12 -0400464 viewMatrix, as_MFB(mf), shape);
bsalomonc55271f2015-11-09 11:55:57 -0800465}
466
Robert Phillips69893702019-02-22 11:16:30 -0500467void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext* context,
Robert Phillips27927a52018-08-20 13:18:12 -0400468 GrRenderTargetContext* renderTargetContext,
469 const GrClip& clip,
470 const SkPaint& paint,
471 const SkMatrix& viewMatrix,
472 const GrShape& shape) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500473 if (context->priv().abandoned()) {
Robert Phillipsbebfd412018-03-08 11:07:23 -0500474 return;
475 }
476
robertphillipsccb1b572015-05-27 11:02:55 -0700477 GrPaint grPaint;
Brian Salomon4bc0c1f2019-09-30 15:12:27 -0400478 if (!SkPaintToGrPaint(context, renderTargetContext->colorInfo(), paint, viewMatrix, &grPaint)) {
robertphillipsccb1b572015-05-27 11:02:55 -0700479 return;
480 }
Robert Phillips27927a52018-08-20 13:18:12 -0400481
Mike Reed80747ef2018-01-23 15:29:32 -0500482 SkMaskFilterBase* mf = as_MFB(paint.getMaskFilter());
Mike Reedbfadcf02018-01-20 22:24:21 +0000483 if (mf && !mf->hasFragmentProcessor()) {
Robert Phillipsa29a9562016-10-20 09:40:55 -0400484 // The MaskFilter wasn't already handled in SkPaintToGrPaint
Robert Phillips793324c2018-08-24 13:53:56 -0400485 draw_shape_with_mask_filter(context, renderTargetContext, clip, std::move(grPaint),
Robert Phillips27927a52018-08-20 13:18:12 -0400486 viewMatrix, mf, shape);
bsalomonc55271f2015-11-09 11:55:57 -0800487 } else {
Robert Phillips793324c2018-08-24 13:53:56 -0400488 GrAA aa = GrAA(paint.isAntiAlias());
Robert Phillips27927a52018-08-20 13:18:12 -0400489 renderTargetContext->drawShape(clip, std::move(grPaint), aa, viewMatrix, shape);
robertphillipsccb1b572015-05-27 11:02:55 -0700490 }
robertphillipsccb1b572015-05-27 11:02:55 -0700491}