blob: ef4bda0aae64302c4b02cb2beeba8031efedea2d [file] [log] [blame]
robertphillipsccb1b572015-05-27 11:02:55 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrBlurUtils.h"
Robert Phillipsc7c2baf2018-03-08 09:51:04 -05009
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040010#include "include/gpu/GrRecordingContext.h"
Greg Daniel6f5441a2020-01-28 17:02:49 -050011#include "src/gpu/GrBitmapTextureMaker.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrCaps.h"
13#include "src/gpu/GrFixedClip.h"
14#include "src/gpu/GrProxyProvider.h"
15#include "src/gpu/GrRecordingContextPriv.h"
16#include "src/gpu/GrRenderTargetContext.h"
17#include "src/gpu/GrRenderTargetContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrSoftwarePathRenderer.h"
19#include "src/gpu/GrStyle.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040020#include "src/gpu/GrTextureProxy.h"
Robert Phillipsc73c1af2020-09-29 13:47:39 -040021#include "src/gpu/GrThreadSafeUniquelyKeyedProxyViewCache.h"
Brian Salomonb8f098d2020-01-07 11:15:44 -050022#include "src/gpu/effects/GrTextureEffect.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000023#include "src/gpu/geometry/GrStyledShape.h"
Robert Phillipsc7c2baf2018-03-08 09:51:04 -050024
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "include/core/SkPaint.h"
26#include "src/core/SkDraw.h"
27#include "src/core/SkMaskFilterBase.h"
Brian Osman449b1152020-04-15 16:43:00 -040028#include "src/core/SkMatrixProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/core/SkTLazy.h"
30#include "src/gpu/SkGr.h"
robertphillipsccb1b572015-05-27 11:02:55 -070031
32static bool clip_bounds_quick_reject(const SkIRect& clipBounds, const SkIRect& rect) {
33 return clipBounds.isEmpty() || rect.isEmpty() || !SkIRect::Intersects(clipBounds, rect);
34}
35
Brian Salomond005b692020-04-01 15:47:05 -040036static constexpr auto kMaskOrigin = kTopLeft_GrSurfaceOrigin;
37
38static GrSurfaceProxyView find_filtered_mask(GrProxyProvider* provider, const GrUniqueKey& key) {
Brian Salomon0029db02020-04-03 10:41:24 -040039 return provider->findCachedProxyWithColorTypeFallback(key, kMaskOrigin, GrColorType::kAlpha_8,
40 1);
Brian Salomond005b692020-04-01 15:47:05 -040041}
42
robertphillipsccb1b572015-05-27 11:02:55 -070043// Draw a mask using the supplied paint. Since the coverage/geometry
44// is already burnt into the mask this boils down to a rect draw.
45// Return true if the mask was successfully drawn.
Robert Phillips296b1cc2017-03-15 10:42:12 -040046static bool draw_mask(GrRenderTargetContext* renderTargetContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -040047 const GrClip* clip,
robertphillipsccb1b572015-05-27 11:02:55 -070048 const SkMatrix& viewMatrix,
robertphillipsf054b172016-05-13 05:06:19 -070049 const SkIRect& maskRect,
Brian Salomon82f44312017-01-11 13:42:54 -050050 GrPaint&& paint,
Greg Daniel5c082492020-01-29 15:06:49 -050051 GrSurfaceProxyView mask) {
Brian Salomon82f44312017-01-11 13:42:54 -050052 SkMatrix inverse;
53 if (!viewMatrix.invert(&inverse)) {
54 return false;
55 }
Robert Phillips4a24da52016-12-14 09:00:07 -050056
Mike Reed1f607332020-05-21 12:11:27 -040057 SkMatrix matrix = SkMatrix::Translate(-SkIntToScalar(maskRect.fLeft),
Robert Phillips67c18d62017-01-20 12:44:06 -050058 -SkIntToScalar(maskRect.fTop));
Brian Salomon2ebd0c82016-10-03 17:15:28 -040059 matrix.preConcat(viewMatrix);
John Stiles41d91b62020-07-21 14:39:40 -040060 paint.setCoverageFragmentProcessor(
Greg Danield2ccbb52020-02-05 10:45:39 -050061 GrTextureEffect::Make(std::move(mask), kUnknown_SkAlphaType, matrix));
robertphillipsccb1b572015-05-27 11:02:55 -070062
Brian Salomon82f44312017-01-11 13:42:54 -050063 renderTargetContext->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050064 SkRect::Make(maskRect), inverse);
robertphillipsccb1b572015-05-27 11:02:55 -070065 return true;
66}
67
Robert Phillipsc7c2baf2018-03-08 09:51:04 -050068static void mask_release_proc(void* addr, void* /*context*/) {
69 SkMask::FreeImage(addr);
70}
71
Robert Phillipsc73c1af2020-09-29 13:47:39 -040072static bool sw_draw_with_mask_filter(GrRecordingContext* rContext,
Robert Phillips4a24da52016-12-14 09:00:07 -050073 GrRenderTargetContext* renderTargetContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -040074 const GrClip* clipData,
robertphillips0e7029e2015-11-30 05:45:06 -080075 const SkMatrix& viewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +000076 const GrStyledShape& shape,
robertphillips0e7029e2015-11-30 05:45:06 -080077 const SkMaskFilter* filter,
78 const SkIRect& clipBounds,
Robert Phillips31c080b2018-08-23 10:45:32 -040079 GrPaint&& paint,
80 const GrUniqueKey& key) {
Robert Phillips27927a52018-08-20 13:18:12 -040081 SkASSERT(filter);
82 SkASSERT(!shape.style().applies());
83
Robert Phillipsc73c1af2020-09-29 13:47:39 -040084 auto threadSafeViewCache = rContext->priv().threadSafeViewCache();
Robert Phillips27927a52018-08-20 13:18:12 -040085
Greg Daniel5c082492020-01-29 15:06:49 -050086 GrSurfaceProxyView filteredMaskView;
Robert Phillips27927a52018-08-20 13:18:12 -040087
88 SkStrokeRec::InitStyle fillOrHairline = shape.style().isSimpleHairline()
89 ? SkStrokeRec::kHairline_InitStyle
90 : SkStrokeRec::kFill_InitStyle;
91
Robert Phillips31c080b2018-08-23 10:45:32 -040092 if (key.isValid()) {
Robert Phillipsc73c1af2020-09-29 13:47:39 -040093 filteredMaskView = threadSafeViewCache->find(key);
robertphillipsccb1b572015-05-27 11:02:55 -070094 }
95
Robert Phillips31c080b2018-08-23 10:45:32 -040096 SkIRect drawRect;
Brian Salomond005b692020-04-01 15:47:05 -040097 if (filteredMaskView) {
Robert Phillipsc73c1af2020-09-29 13:47:39 -040098 SkASSERT(kMaskOrigin == filteredMaskView.origin());
99
Robert Phillips31c080b2018-08-23 10:45:32 -0400100 SkRect devBounds = shape.bounds();
101 viewMatrix.mapRect(&devBounds);
Robert Phillips4a24da52016-12-14 09:00:07 -0500102
Robert Phillips31c080b2018-08-23 10:45:32 -0400103 // Here we need to recompute the destination bounds in order to draw the mask correctly
104 SkMask srcM, dstM;
105 if (!SkDraw::ComputeMaskBounds(devBounds, &clipBounds, filter, &viewMatrix,
106 &srcM.fBounds)) {
107 return false;
108 }
109
110 srcM.fFormat = SkMask::kA8_Format;
111
112 if (!as_MFB(filter)->filterMask(&dstM, srcM, viewMatrix, nullptr)) {
113 return false;
114 }
115
116 // Unfortunately, we cannot double check that the computed bounds (i.e., dstM.fBounds)
117 // match the stored bounds of the mask bc the proxy may have been recreated and,
118 // when it is recreated, it just gets the bounds of the underlying GrTexture (which
119 // might be a loose fit).
120 drawRect = dstM.fBounds;
121 } else {
122 // TODO: it seems like we could create an SkDraw here and set its fMatrix field rather
123 // than explicitly transforming the path to device space.
124 SkPath devPath;
125
126 shape.asPath(&devPath);
127
128 devPath.transform(viewMatrix);
129
130 SkMask srcM, dstM;
131 if (!SkDraw::DrawToMask(devPath, &clipBounds, filter, &viewMatrix, &srcM,
132 SkMask::kComputeBoundsAndRenderImage_CreateMode, fillOrHairline)) {
133 return false;
134 }
135 SkAutoMaskFreeImage autoSrc(srcM.fImage);
136
137 SkASSERT(SkMask::kA8_Format == srcM.fFormat);
138
139 if (!as_MFB(filter)->filterMask(&dstM, srcM, viewMatrix, nullptr)) {
140 return false;
141 }
142 // this will free-up dstM when we're done (allocated in filterMask())
143 SkAutoMaskFreeImage autoDst(dstM.fImage);
144
145 if (clip_bounds_quick_reject(clipBounds, dstM.fBounds)) {
146 return false;
147 }
148
149 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
150 // the current clip (and identity matrix) and GrPaint settings
151 SkBitmap bm;
152 if (!bm.installPixels(SkImageInfo::MakeA8(dstM.fBounds.width(), dstM.fBounds.height()),
153 autoDst.release(), dstM.fRowBytes, mask_release_proc, nullptr)) {
154 return false;
155 }
156 bm.setImmutable();
157
Robert Phillipsc73c1af2020-09-29 13:47:39 -0400158 GrBitmapTextureMaker maker(rContext, bm, SkBackingFit::kApprox);
Brian Salomon7e67dca2020-07-21 09:27:25 -0400159 filteredMaskView = maker.view(GrMipmapped::kNo);
Greg Danielcc104db2020-02-03 14:17:08 -0500160 if (!filteredMaskView.proxy()) {
Robert Phillips31c080b2018-08-23 10:45:32 -0400161 return false;
162 }
163
Brian Salomond005b692020-04-01 15:47:05 -0400164 SkASSERT(kMaskOrigin == filteredMaskView.origin());
Robert Phillips59b39e72018-08-30 11:51:25 -0400165
Robert Phillips31c080b2018-08-23 10:45:32 -0400166 drawRect = dstM.fBounds;
167
168 if (key.isValid()) {
Robert Phillipsc73c1af2020-09-29 13:47:39 -0400169 filteredMaskView = threadSafeViewCache->add(key, filteredMaskView);
Robert Phillips31c080b2018-08-23 10:45:32 -0400170 }
Robert Phillips48ce22b2018-03-23 10:33:12 -0400171 }
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500172
Brian Salomonfc118442019-11-22 19:09:27 -0500173 return draw_mask(renderTargetContext, clipData, viewMatrix, drawRect, std::move(paint),
Greg Daniel5c082492020-01-29 15:06:49 -0500174 std::move(filteredMaskView));
robertphillipsccb1b572015-05-27 11:02:55 -0700175}
176
Robert Phillips40b05c32019-09-20 12:40:55 -0400177// Create a mask of 'shape' and return the resulting renderTargetContext
178static std::unique_ptr<GrRenderTargetContext> create_mask_GPU(GrRecordingContext* context,
179 const SkIRect& maskRect,
180 const SkMatrix& origViewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +0000181 const GrStyledShape& shape,
Robert Phillips40b05c32019-09-20 12:40:55 -0400182 int sampleCnt) {
Chris Dalton0493fbd2019-09-18 15:49:46 -0600183 // Use GrResourceProvider::MakeApprox to implement our own approximate size matching, but demand
184 // a "SkBackingFit::kExact" size match on the actual render target. We do this because the
185 // filter will reach outside the src bounds, so we need to pre-clear these values to ensure a
186 // "decal" sampling effect (i.e., ensure reads outside the src bounds return alpha=0).
187 //
188 // FIXME: Reads outside the left and top edges will actually clamp to the edge pixel. And in the
189 // event that MakeApprox does not change the size, reads outside the right and/or bottom will do
190 // the same. We should offset our filter within the render target and expand the size as needed
191 // to guarantee at least 1px of padding on all sides.
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400192 auto approxSize = GrResourceProvider::MakeApprox(maskRect.size());
Greg Daniele20fcad2020-01-08 11:52:34 -0500193 auto rtContext = GrRenderTargetContext::MakeWithFallback(
194 context, GrColorType::kAlpha_8, nullptr, SkBackingFit::kExact, approxSize, sampleCnt,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400195 GrMipmapped::kNo, GrProtected::kNo, kMaskOrigin);
robertphillipsd728f0c2016-11-21 11:05:03 -0800196 if (!rtContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700197 return nullptr;
robertphillipsccb1b572015-05-27 11:02:55 -0700198 }
199
Chris Dalton0493fbd2019-09-18 15:49:46 -0600200 rtContext->clear(SK_PMColor4fTRANSPARENT);
robertphillipsccb1b572015-05-27 11:02:55 -0700201
Brian Salomon82f44312017-01-11 13:42:54 -0500202 GrPaint maskPaint;
203 maskPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op);
robertphillipsccb1b572015-05-27 11:02:55 -0700204
205 // setup new clip
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400206 GrFixedClip clip(rtContext->dimensions(), SkIRect::MakeWH(maskRect.width(), maskRect.height()));
robertphillipsccb1b572015-05-27 11:02:55 -0700207
Brian Salomond005b692020-04-01 15:47:05 -0400208 // Draw the mask into maskTexture with the path's integerized top-left at the origin using
209 // maskPaint.
Robert Phillips27927a52018-08-20 13:18:12 -0400210 SkMatrix viewMatrix = origViewMatrix;
211 viewMatrix.postTranslate(-SkIntToScalar(maskRect.fLeft), -SkIntToScalar(maskRect.fTop));
Michael Ludwig7c12e282020-05-29 09:54:07 -0400212 rtContext->drawShape(&clip, std::move(maskPaint), GrAA::kYes, viewMatrix, shape);
Robert Phillips40b05c32019-09-20 12:40:55 -0400213 return rtContext;
robertphillipsccb1b572015-05-27 11:02:55 -0700214}
215
Michael Ludwig2686d692020-04-17 20:21:37 +0000216static bool get_unclipped_shape_dev_bounds(const GrStyledShape& shape, const SkMatrix& matrix,
Robert Phillips27927a52018-08-20 13:18:12 -0400217 SkIRect* devBounds) {
218 SkRect shapeBounds = shape.styledBounds();
219 if (shapeBounds.isEmpty()) {
220 return false;
221 }
222 SkRect shapeDevBounds;
223 matrix.mapRect(&shapeDevBounds, shapeBounds);
224 // Even though these are "unclipped" bounds we still clip to the int32_t range.
225 // This is the largest int32_t that is representable exactly as a float. The next 63 larger ints
226 // would round down to this value when cast to a float, but who really cares.
227 // INT32_MIN is exactly representable.
228 static constexpr int32_t kMaxInt = 2147483520;
229 if (!shapeDevBounds.intersect(SkRect::MakeLTRB(INT32_MIN, INT32_MIN, kMaxInt, kMaxInt))) {
230 return false;
231 }
232 // Make sure that the resulting SkIRect can have representable width and height
233 if (SkScalarRoundToInt(shapeDevBounds.width()) > kMaxInt ||
234 SkScalarRoundToInt(shapeDevBounds.height()) > kMaxInt) {
235 return false;
236 }
237 shapeDevBounds.roundOut(devBounds);
238 return true;
239}
bsalomonc55271f2015-11-09 11:55:57 -0800240
Robert Phillips27927a52018-08-20 13:18:12 -0400241// Gets the shape bounds, the clip bounds, and the intersection (if any). Returns false if there
242// is no intersection.
243static bool get_shape_and_clip_bounds(GrRenderTargetContext* renderTargetContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400244 const GrClip* clip,
Michael Ludwig2686d692020-04-17 20:21:37 +0000245 const GrStyledShape& shape,
Robert Phillips27927a52018-08-20 13:18:12 -0400246 const SkMatrix& matrix,
247 SkIRect* unclippedDevShapeBounds,
248 SkIRect* devClipBounds) {
249 // compute bounds as intersection of rt size, clip, and path
Michael Ludwige06a8972020-06-11 10:29:00 -0400250 *devClipBounds = clip ? clip->getConservativeBounds()
Michael Ludwig7c12e282020-05-29 09:54:07 -0400251 : SkIRect::MakeWH(renderTargetContext->width(),
252 renderTargetContext->height());
bsalomonc55271f2015-11-09 11:55:57 -0800253
Robert Phillips27927a52018-08-20 13:18:12 -0400254 if (!get_unclipped_shape_dev_bounds(shape, matrix, unclippedDevShapeBounds)) {
Brian Salomon44207f32020-01-06 15:20:18 -0500255 *unclippedDevShapeBounds = SkIRect::MakeEmpty();
Robert Phillips27927a52018-08-20 13:18:12 -0400256 return false;
bsalomonc55271f2015-11-09 11:55:57 -0800257 }
258
Robert Phillips27927a52018-08-20 13:18:12 -0400259 return true;
260}
261
Robert Phillips7af8fe52019-02-14 17:27:00 -0500262static void draw_shape_with_mask_filter(GrRecordingContext* context,
Robert Phillips27927a52018-08-20 13:18:12 -0400263 GrRenderTargetContext* renderTargetContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400264 const GrClip* clip,
Robert Phillips27927a52018-08-20 13:18:12 -0400265 GrPaint&& paint,
Robert Phillips27927a52018-08-20 13:18:12 -0400266 const SkMatrix& viewMatrix,
267 const SkMaskFilterBase* maskFilter,
Michael Ludwig2686d692020-04-17 20:21:37 +0000268 const GrStyledShape& origShape) {
Robert Phillips27927a52018-08-20 13:18:12 -0400269 SkASSERT(maskFilter);
270
Michael Ludwig2686d692020-04-17 20:21:37 +0000271 const GrStyledShape* shape = &origShape;
272 SkTLazy<GrStyledShape> tmpShape;
Robert Phillips27927a52018-08-20 13:18:12 -0400273
274 if (origShape.style().applies()) {
275 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
276 if (0 == styleScale) {
277 return;
bsalomon6663acf2016-05-10 09:14:17 -0700278 }
Robert Phillips27927a52018-08-20 13:18:12 -0400279
280 tmpShape.init(origShape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale));
John Stilesa008b0f2020-08-16 08:48:02 -0400281 if (tmpShape->isEmpty()) {
Robert Phillips27927a52018-08-20 13:18:12 -0400282 return;
283 }
284
285 shape = tmpShape.get();
bsalomon6663acf2016-05-10 09:14:17 -0700286 }
bsalomonc55271f2015-11-09 11:55:57 -0800287
Michael Ludwig7c12e282020-05-29 09:54:07 -0400288 if (maskFilter->directFilterMaskGPU(context, renderTargetContext, std::move(paint), clip,
289 viewMatrix, *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,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400303 &unclippedDevShapeBounds, &devClipBounds)) {
Robert Phillips27927a52018-08-20 13:18:12 -0400304 // TODO: just cons up an opaque mask here
305 if (!inverseFilled) {
306 return;
307 }
308 }
309
Robert Phillips31c080b2018-08-23 10:45:32 -0400310 // To prevent overloading the cache with entries during animations we limit the cache of masks
311 // to cases where the matrix preserves axis alignment.
Robert Phillipsde479282018-08-28 11:20:08 -0400312#ifdef SK_DISABLE_MASKFILTERED_MASK_CACHING
313 bool useCache = false;
314#else
Robert Phillips31c080b2018-08-23 10:45:32 -0400315 bool useCache = !inverseFilled && viewMatrix.preservesAxisAlignment() &&
316 shape->hasUnstyledKey() && as_MFB(maskFilter)->asABlur(nullptr);
Robert Phillipsde479282018-08-28 11:20:08 -0400317#endif
Robert Phillips31c080b2018-08-23 10:45:32 -0400318
319 const SkIRect* boundsForClip = &devClipBounds;
320 if (useCache) {
321 SkIRect clippedMaskRect, unClippedMaskRect;
322 maskFilter->canFilterMaskGPU(*shape, unclippedDevShapeBounds, devClipBounds,
323 viewMatrix, &clippedMaskRect);
324 maskFilter->canFilterMaskGPU(*shape, unclippedDevShapeBounds, unclippedDevShapeBounds,
325 viewMatrix, &unClippedMaskRect);
326 if (clippedMaskRect.isEmpty()) {
327 return;
328 }
329
330 // Use the cache only if >50% of the filtered mask is visible.
331 int unclippedWidth = unClippedMaskRect.width();
332 int unclippedHeight = unClippedMaskRect.height();
333 int64_t unclippedArea = sk_64_mul(unclippedWidth, unclippedHeight);
334 int64_t clippedArea = sk_64_mul(clippedMaskRect.width(), clippedMaskRect.height());
335 int maxTextureSize = renderTargetContext->caps()->maxTextureSize();
336 if (unclippedArea > 2 * clippedArea || unclippedWidth > maxTextureSize ||
337 unclippedHeight > maxTextureSize) {
338 useCache = false;
339 } else {
340 // Make the clip not affect the mask
341 boundsForClip = &unclippedDevShapeBounds;
342 }
343 }
344
345 GrUniqueKey maskKey;
346 if (useCache) {
347 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
348 GrUniqueKey::Builder builder(&maskKey, kDomain, 5 + 2 + shape->unstyledKeySize(),
349 "Mask Filtered Masks");
350
351 // We require the upper left 2x2 of the matrix to match exactly for a cache hit.
352 SkScalar sx = viewMatrix.get(SkMatrix::kMScaleX);
353 SkScalar sy = viewMatrix.get(SkMatrix::kMScaleY);
354 SkScalar kx = viewMatrix.get(SkMatrix::kMSkewX);
355 SkScalar ky = viewMatrix.get(SkMatrix::kMSkewY);
356 SkScalar tx = viewMatrix.get(SkMatrix::kMTransX);
357 SkScalar ty = viewMatrix.get(SkMatrix::kMTransY);
358 // Allow 8 bits each in x and y of subpixel positioning.
359 SkFixed fracX = SkScalarToFixed(SkScalarFraction(tx)) & 0x0000FF00;
360 SkFixed fracY = SkScalarToFixed(SkScalarFraction(ty)) & 0x0000FF00;
361
362 builder[0] = SkFloat2Bits(sx);
363 builder[1] = SkFloat2Bits(sy);
364 builder[2] = SkFloat2Bits(kx);
365 builder[3] = SkFloat2Bits(ky);
366 // Distinguish between hairline and filled paths. For hairlines, we also need to include
367 // the cap. (SW grows hairlines by 0.5 pixel with round and square caps). Note that
368 // stroke-and-fill of hairlines is turned into pure fill by SkStrokeRec, so this covers
369 // all cases we might see.
370 uint32_t styleBits = shape->style().isSimpleHairline()
371 ? ((shape->style().strokeRec().getCap() << 1) | 1)
372 : 0;
373 builder[4] = fracX | (fracY >> 8) | (styleBits << 16);
374
375 SkMaskFilterBase::BlurRec rec;
376 SkAssertResult(as_MFB(maskFilter)->asABlur(&rec));
377
378 builder[5] = rec.fStyle; // TODO: we could put this with the other style bits
Robert Phillips55ff5d32019-01-07 16:11:36 -0500379 builder[6] = SkFloat2Bits(rec.fSigma);
Robert Phillips31c080b2018-08-23 10:45:32 -0400380 shape->writeUnstyledKey(&builder[7]);
381 }
382
383 SkIRect maskRect;
Robert Phillips27927a52018-08-20 13:18:12 -0400384 if (maskFilter->canFilterMaskGPU(*shape,
Robert Phillips31c080b2018-08-23 10:45:32 -0400385 unclippedDevShapeBounds,
386 *boundsForClip,
bsalomonc55271f2015-11-09 11:55:57 -0800387 viewMatrix,
388 &maskRect)) {
Robert Phillips31c080b2018-08-23 10:45:32 -0400389 if (clip_bounds_quick_reject(*boundsForClip, maskRect)) {
bsalomonc55271f2015-11-09 11:55:57 -0800390 // clipped out
391 return;
392 }
393
Greg Daniel5c082492020-01-29 15:06:49 -0500394 GrSurfaceProxyView filteredMaskView;
Robert Phillips20390c32018-08-17 11:01:03 -0400395
Robert Phillips9da87e02019-02-04 13:26:26 -0500396 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Robert Phillips31c080b2018-08-23 10:45:32 -0400397
Robert Phillipsc73c1af2020-09-29 13:47:39 -0400398 // TODO: this path should also use the thread-safe proxy-view cache!
Robert Phillips31c080b2018-08-23 10:45:32 -0400399 if (maskKey.isValid()) {
Brian Salomond005b692020-04-01 15:47:05 -0400400 filteredMaskView = find_filtered_mask(proxyProvider, maskKey);
Robert Phillips31c080b2018-08-23 10:45:32 -0400401 }
402
Brian Salomond005b692020-04-01 15:47:05 -0400403 if (!filteredMaskView) {
Robert Phillips40b05c32019-09-20 12:40:55 -0400404 std::unique_ptr<GrRenderTargetContext> maskRTC(create_mask_GPU(
Greg Daniel5c082492020-01-29 15:06:49 -0500405 context,
406 maskRect,
407 viewMatrix,
408 *shape,
409 renderTargetContext->numSamples()));
Robert Phillips40b05c32019-09-20 12:40:55 -0400410 if (maskRTC) {
Greg Daniel5c082492020-01-29 15:06:49 -0500411 filteredMaskView = maskFilter->filterMaskGPU(context,
412 maskRTC->readSurfaceView(),
413 maskRTC->colorInfo().colorType(),
414 maskRTC->colorInfo().alphaType(),
415 viewMatrix,
416 maskRect);
417 if (filteredMaskView.proxy() && maskKey.isValid()) {
418 SkASSERT(filteredMaskView.asTextureProxy());
419 proxyProvider->assignUniqueKeyToProxy(maskKey,
420 filteredMaskView.asTextureProxy());
Robert Phillips31c080b2018-08-23 10:45:32 -0400421 }
bsalomonc55271f2015-11-09 11:55:57 -0800422 }
423 }
Robert Phillips20390c32018-08-17 11:01:03 -0400424
Brian Salomond005b692020-04-01 15:47:05 -0400425 if (filteredMaskView) {
Brian Salomonfc118442019-11-22 19:09:27 -0500426 if (draw_mask(renderTargetContext, clip, viewMatrix, maskRect, std::move(paint),
Greg Daniel5c082492020-01-29 15:06:49 -0500427 std::move(filteredMaskView))) {
Robert Phillips20390c32018-08-17 11:01:03 -0400428 // This path is completely drawn
429 return;
430 }
Mike Klein16885072018-12-11 09:54:31 -0500431 assert_alive(paint);
Robert Phillips20390c32018-08-17 11:01:03 -0400432 }
bsalomonc55271f2015-11-09 11:55:57 -0800433 }
434
Robert Phillips27927a52018-08-20 13:18:12 -0400435 sw_draw_with_mask_filter(context, renderTargetContext, clip, viewMatrix, *shape,
Robert Phillips31c080b2018-08-23 10:45:32 -0400436 maskFilter, *boundsForClip, std::move(paint), maskKey);
bsalomon6663acf2016-05-10 09:14:17 -0700437}
438
Robert Phillips7af8fe52019-02-14 17:27:00 -0500439void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext* context,
Robert Phillips27927a52018-08-20 13:18:12 -0400440 GrRenderTargetContext* renderTargetContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400441 const GrClip* clip,
Michael Ludwig2686d692020-04-17 20:21:37 +0000442 const GrStyledShape& shape,
Robert Phillips27927a52018-08-20 13:18:12 -0400443 GrPaint&& paint,
Robert Phillips27927a52018-08-20 13:18:12 -0400444 const SkMatrix& viewMatrix,
445 const SkMaskFilter* mf) {
Robert Phillips793324c2018-08-24 13:53:56 -0400446 draw_shape_with_mask_filter(context, renderTargetContext, clip, std::move(paint),
Robert Phillips27927a52018-08-20 13:18:12 -0400447 viewMatrix, as_MFB(mf), shape);
bsalomonc55271f2015-11-09 11:55:57 -0800448}
449
Robert Phillips69893702019-02-22 11:16:30 -0500450void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext* context,
Robert Phillips27927a52018-08-20 13:18:12 -0400451 GrRenderTargetContext* renderTargetContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400452 const GrClip* clip,
Robert Phillips27927a52018-08-20 13:18:12 -0400453 const SkPaint& paint,
Brian Osman449b1152020-04-15 16:43:00 -0400454 const SkMatrixProvider& matrixProvider,
Michael Ludwig2686d692020-04-17 20:21:37 +0000455 const GrStyledShape& shape) {
Robert Phillips9eb00022020-06-30 15:30:12 -0400456 if (context->abandoned()) {
Robert Phillipsbebfd412018-03-08 11:07:23 -0500457 return;
458 }
459
robertphillipsccb1b572015-05-27 11:02:55 -0700460 GrPaint grPaint;
Brian Osman449b1152020-04-15 16:43:00 -0400461 if (!SkPaintToGrPaint(context, renderTargetContext->colorInfo(), paint, matrixProvider,
462 &grPaint)) {
robertphillipsccb1b572015-05-27 11:02:55 -0700463 return;
464 }
Robert Phillips27927a52018-08-20 13:18:12 -0400465
Brian Osman449b1152020-04-15 16:43:00 -0400466 const SkMatrix& viewMatrix(matrixProvider.localToDevice());
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}