blob: c38314cb1d0fce4d704dd4128a63dd814bb10c22 [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 Phillips6e17ffe2020-10-06 14:52:11 -040072#ifdef SK_DEBUG
73// Brute force computation of the destination bounds of a SW filtered mask
74static SkIRect sw_calc_draw_rect(const SkMatrix& viewMatrix,
75 const GrStyledShape& shape,
76 const SkMaskFilter* filter,
77 const SkIRect& clipBounds) {
78 SkRect devBounds = shape.bounds();
79 viewMatrix.mapRect(&devBounds);
80
81 SkMask srcM, dstM;
82 if (!SkDraw::ComputeMaskBounds(devBounds, &clipBounds, filter, &viewMatrix, &srcM.fBounds)) {
83 return {};
84 }
85
86 srcM.fFormat = SkMask::kA8_Format;
87
88 if (!as_MFB(filter)->filterMask(&dstM, srcM, viewMatrix, nullptr)) {
89 return {};
90 }
91
92 return dstM.fBounds;
93}
94#endif
95
96// This stores the mapping from an unclipped, integerized, device-space, shape bounds to
97// the filtered mask's draw rect.
98struct DrawRectData {
99 SkIVector fOffset;
100 SkISize fSize;
101};
102
103static sk_sp<SkData> create_data(const SkIRect& drawRect, const SkIRect& origDevBounds) {
104
105 DrawRectData drawRectData { {drawRect.fLeft - origDevBounds.fLeft,
106 drawRect.fTop - origDevBounds.fTop},
107 drawRect.size() };
108
109 return SkData::MakeWithCopy(&drawRectData, sizeof(drawRectData));
110}
111
112static SkIRect extract_draw_rect_from_data(SkData* data, const SkIRect& origDevBounds) {
113 auto drawRectData = static_cast<const DrawRectData*>(data->data());
114
115 return SkIRect::MakeXYWH(origDevBounds.fLeft + drawRectData->fOffset.fX,
116 origDevBounds.fTop + drawRectData->fOffset.fY,
117 drawRectData->fSize.fWidth,
118 drawRectData->fSize.fHeight);
119}
120
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400121static GrSurfaceProxyView sw_create_filtered_mask(GrRecordingContext* rContext,
122 const SkMatrix& viewMatrix,
123 const GrStyledShape& shape,
124 const SkMaskFilter* filter,
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400125 const SkIRect& unclippedDevShapeBounds,
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400126 const SkIRect& clipBounds,
127 SkIRect* drawRect,
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400128 GrUniqueKey* key) {
Robert Phillips27927a52018-08-20 13:18:12 -0400129 SkASSERT(filter);
130 SkASSERT(!shape.style().applies());
131
Robert Phillipsc73c1af2020-09-29 13:47:39 -0400132 auto threadSafeViewCache = rContext->priv().threadSafeViewCache();
Robert Phillips27927a52018-08-20 13:18:12 -0400133
Greg Daniel5c082492020-01-29 15:06:49 -0500134 GrSurfaceProxyView filteredMaskView;
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400135 sk_sp<SkData> data;
Robert Phillips27927a52018-08-20 13:18:12 -0400136
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400137 if (key->isValid()) {
138 std::tie(filteredMaskView, data) = threadSafeViewCache->findWithData(*key);
robertphillipsccb1b572015-05-27 11:02:55 -0700139 }
140
Brian Salomond005b692020-04-01 15:47:05 -0400141 if (filteredMaskView) {
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400142 SkASSERT(data);
Robert Phillipsc73c1af2020-09-29 13:47:39 -0400143 SkASSERT(kMaskOrigin == filteredMaskView.origin());
144
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400145 *drawRect = extract_draw_rect_from_data(data.get(), unclippedDevShapeBounds);
Robert Phillips4a24da52016-12-14 09:00:07 -0500146
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400147 SkDEBUGCODE(auto oldDrawRect = sw_calc_draw_rect(viewMatrix, shape, filter, clipBounds));
148 SkASSERT(*drawRect == oldDrawRect);
Robert Phillips31c080b2018-08-23 10:45:32 -0400149 } else {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400150 SkStrokeRec::InitStyle fillOrHairline = shape.style().isSimpleHairline()
151 ? SkStrokeRec::kHairline_InitStyle
152 : SkStrokeRec::kFill_InitStyle;
153
Robert Phillips31c080b2018-08-23 10:45:32 -0400154 // TODO: it seems like we could create an SkDraw here and set its fMatrix field rather
155 // than explicitly transforming the path to device space.
156 SkPath devPath;
157
158 shape.asPath(&devPath);
159
160 devPath.transform(viewMatrix);
161
162 SkMask srcM, dstM;
163 if (!SkDraw::DrawToMask(devPath, &clipBounds, filter, &viewMatrix, &srcM,
164 SkMask::kComputeBoundsAndRenderImage_CreateMode, fillOrHairline)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400165 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400166 }
167 SkAutoMaskFreeImage autoSrc(srcM.fImage);
168
169 SkASSERT(SkMask::kA8_Format == srcM.fFormat);
170
171 if (!as_MFB(filter)->filterMask(&dstM, srcM, viewMatrix, nullptr)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400172 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400173 }
174 // this will free-up dstM when we're done (allocated in filterMask())
175 SkAutoMaskFreeImage autoDst(dstM.fImage);
176
177 if (clip_bounds_quick_reject(clipBounds, dstM.fBounds)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400178 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400179 }
180
181 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
182 // the current clip (and identity matrix) and GrPaint settings
183 SkBitmap bm;
184 if (!bm.installPixels(SkImageInfo::MakeA8(dstM.fBounds.width(), dstM.fBounds.height()),
185 autoDst.release(), dstM.fRowBytes, mask_release_proc, nullptr)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400186 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400187 }
188 bm.setImmutable();
189
Robert Phillipsc73c1af2020-09-29 13:47:39 -0400190 GrBitmapTextureMaker maker(rContext, bm, SkBackingFit::kApprox);
Brian Salomon7e67dca2020-07-21 09:27:25 -0400191 filteredMaskView = maker.view(GrMipmapped::kNo);
Greg Danielcc104db2020-02-03 14:17:08 -0500192 if (!filteredMaskView.proxy()) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400193 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400194 }
195
Brian Salomond005b692020-04-01 15:47:05 -0400196 SkASSERT(kMaskOrigin == filteredMaskView.origin());
Robert Phillips59b39e72018-08-30 11:51:25 -0400197
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400198 *drawRect = dstM.fBounds;
Robert Phillips31c080b2018-08-23 10:45:32 -0400199
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400200 if (key->isValid()) {
201 key->setCustomData(create_data(*drawRect, unclippedDevShapeBounds));
202 std::tie(filteredMaskView, data) = threadSafeViewCache->addWithData(*key,
203 filteredMaskView);
204 // If we got a different view back from 'addWithData' it could have a different drawRect
205 *drawRect = extract_draw_rect_from_data(data.get(), unclippedDevShapeBounds);
Robert Phillips31c080b2018-08-23 10:45:32 -0400206 }
Robert Phillips48ce22b2018-03-23 10:33:12 -0400207 }
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500208
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400209 return filteredMaskView;
robertphillipsccb1b572015-05-27 11:02:55 -0700210}
211
Robert Phillips40b05c32019-09-20 12:40:55 -0400212// Create a mask of 'shape' and return the resulting renderTargetContext
213static std::unique_ptr<GrRenderTargetContext> create_mask_GPU(GrRecordingContext* context,
214 const SkIRect& maskRect,
215 const SkMatrix& origViewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +0000216 const GrStyledShape& shape,
Robert Phillips40b05c32019-09-20 12:40:55 -0400217 int sampleCnt) {
Chris Dalton0493fbd2019-09-18 15:49:46 -0600218 // Use GrResourceProvider::MakeApprox to implement our own approximate size matching, but demand
219 // a "SkBackingFit::kExact" size match on the actual render target. We do this because the
220 // filter will reach outside the src bounds, so we need to pre-clear these values to ensure a
221 // "decal" sampling effect (i.e., ensure reads outside the src bounds return alpha=0).
222 //
223 // FIXME: Reads outside the left and top edges will actually clamp to the edge pixel. And in the
224 // event that MakeApprox does not change the size, reads outside the right and/or bottom will do
225 // the same. We should offset our filter within the render target and expand the size as needed
226 // to guarantee at least 1px of padding on all sides.
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400227 auto approxSize = GrResourceProvider::MakeApprox(maskRect.size());
Greg Daniele20fcad2020-01-08 11:52:34 -0500228 auto rtContext = GrRenderTargetContext::MakeWithFallback(
229 context, GrColorType::kAlpha_8, nullptr, SkBackingFit::kExact, approxSize, sampleCnt,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400230 GrMipmapped::kNo, GrProtected::kNo, kMaskOrigin);
robertphillipsd728f0c2016-11-21 11:05:03 -0800231 if (!rtContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700232 return nullptr;
robertphillipsccb1b572015-05-27 11:02:55 -0700233 }
234
Chris Dalton0493fbd2019-09-18 15:49:46 -0600235 rtContext->clear(SK_PMColor4fTRANSPARENT);
robertphillipsccb1b572015-05-27 11:02:55 -0700236
Brian Salomon82f44312017-01-11 13:42:54 -0500237 GrPaint maskPaint;
238 maskPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op);
robertphillipsccb1b572015-05-27 11:02:55 -0700239
240 // setup new clip
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400241 GrFixedClip clip(rtContext->dimensions(), SkIRect::MakeWH(maskRect.width(), maskRect.height()));
robertphillipsccb1b572015-05-27 11:02:55 -0700242
Brian Salomond005b692020-04-01 15:47:05 -0400243 // Draw the mask into maskTexture with the path's integerized top-left at the origin using
244 // maskPaint.
Robert Phillips27927a52018-08-20 13:18:12 -0400245 SkMatrix viewMatrix = origViewMatrix;
246 viewMatrix.postTranslate(-SkIntToScalar(maskRect.fLeft), -SkIntToScalar(maskRect.fTop));
Michael Ludwig7c12e282020-05-29 09:54:07 -0400247 rtContext->drawShape(&clip, std::move(maskPaint), GrAA::kYes, viewMatrix, shape);
Robert Phillips40b05c32019-09-20 12:40:55 -0400248 return rtContext;
robertphillipsccb1b572015-05-27 11:02:55 -0700249}
250
Michael Ludwig2686d692020-04-17 20:21:37 +0000251static bool get_unclipped_shape_dev_bounds(const GrStyledShape& shape, const SkMatrix& matrix,
Robert Phillips27927a52018-08-20 13:18:12 -0400252 SkIRect* devBounds) {
253 SkRect shapeBounds = shape.styledBounds();
254 if (shapeBounds.isEmpty()) {
255 return false;
256 }
257 SkRect shapeDevBounds;
258 matrix.mapRect(&shapeDevBounds, shapeBounds);
259 // Even though these are "unclipped" bounds we still clip to the int32_t range.
260 // This is the largest int32_t that is representable exactly as a float. The next 63 larger ints
261 // would round down to this value when cast to a float, but who really cares.
262 // INT32_MIN is exactly representable.
263 static constexpr int32_t kMaxInt = 2147483520;
264 if (!shapeDevBounds.intersect(SkRect::MakeLTRB(INT32_MIN, INT32_MIN, kMaxInt, kMaxInt))) {
265 return false;
266 }
267 // Make sure that the resulting SkIRect can have representable width and height
268 if (SkScalarRoundToInt(shapeDevBounds.width()) > kMaxInt ||
269 SkScalarRoundToInt(shapeDevBounds.height()) > kMaxInt) {
270 return false;
271 }
272 shapeDevBounds.roundOut(devBounds);
273 return true;
274}
bsalomonc55271f2015-11-09 11:55:57 -0800275
Robert Phillips27927a52018-08-20 13:18:12 -0400276// Gets the shape bounds, the clip bounds, and the intersection (if any). Returns false if there
277// is no intersection.
278static bool get_shape_and_clip_bounds(GrRenderTargetContext* renderTargetContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400279 const GrClip* clip,
Michael Ludwig2686d692020-04-17 20:21:37 +0000280 const GrStyledShape& shape,
Robert Phillips27927a52018-08-20 13:18:12 -0400281 const SkMatrix& matrix,
282 SkIRect* unclippedDevShapeBounds,
283 SkIRect* devClipBounds) {
284 // compute bounds as intersection of rt size, clip, and path
Michael Ludwige06a8972020-06-11 10:29:00 -0400285 *devClipBounds = clip ? clip->getConservativeBounds()
Michael Ludwig7c12e282020-05-29 09:54:07 -0400286 : SkIRect::MakeWH(renderTargetContext->width(),
287 renderTargetContext->height());
bsalomonc55271f2015-11-09 11:55:57 -0800288
Robert Phillips27927a52018-08-20 13:18:12 -0400289 if (!get_unclipped_shape_dev_bounds(shape, matrix, unclippedDevShapeBounds)) {
Brian Salomon44207f32020-01-06 15:20:18 -0500290 *unclippedDevShapeBounds = SkIRect::MakeEmpty();
Robert Phillips27927a52018-08-20 13:18:12 -0400291 return false;
bsalomonc55271f2015-11-09 11:55:57 -0800292 }
293
Robert Phillips27927a52018-08-20 13:18:12 -0400294 return true;
295}
296
Robert Phillips52c17c42020-10-05 11:55:59 -0400297// The key and clip-bounds are computed together because the caching decision can impact the
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400298// clip-bound - since we only cache un-clipped masks the clip can be removed entirely.
Robert Phillips52c17c42020-10-05 11:55:59 -0400299// A 'false' return value indicates that the shape is known to be clipped away.
300static bool compute_key_and_clip_bounds(GrUniqueKey* maskKey,
301 SkIRect* boundsForClip,
302 const GrCaps* caps,
303 const SkMatrix& viewMatrix,
304 bool inverseFilled,
305 const SkMaskFilterBase* maskFilter,
306 const GrStyledShape& shape,
307 const SkIRect& unclippedDevShapeBounds,
308 const SkIRect& devClipBounds) {
309 *boundsForClip = devClipBounds;
310
311#ifndef SK_DISABLE_MASKFILTERED_MASK_CACHING
312 // To prevent overloading the cache with entries during animations we limit the cache of masks
313 // to cases where the matrix preserves axis alignment.
314 bool useCache = !inverseFilled && viewMatrix.preservesAxisAlignment() &&
315 shape.hasUnstyledKey() && as_MFB(maskFilter)->asABlur(nullptr);
316
317 if (useCache) {
318 SkIRect clippedMaskRect, unClippedMaskRect;
319 maskFilter->canFilterMaskGPU(shape, unclippedDevShapeBounds, devClipBounds,
320 viewMatrix, &clippedMaskRect);
321 maskFilter->canFilterMaskGPU(shape, unclippedDevShapeBounds, unclippedDevShapeBounds,
322 viewMatrix, &unClippedMaskRect);
323 if (clippedMaskRect.isEmpty()) {
324 return false;
325 }
326
327 // Use the cache only if >50% of the filtered mask is visible.
328 int unclippedWidth = unClippedMaskRect.width();
329 int unclippedHeight = unClippedMaskRect.height();
330 int64_t unclippedArea = sk_64_mul(unclippedWidth, unclippedHeight);
331 int64_t clippedArea = sk_64_mul(clippedMaskRect.width(), clippedMaskRect.height());
332 int maxTextureSize = caps->maxTextureSize();
333 if (unclippedArea > 2 * clippedArea || unclippedWidth > maxTextureSize ||
334 unclippedHeight > maxTextureSize) {
335 useCache = false;
336 } else {
337 // Make the clip not affect the mask
338 *boundsForClip = unclippedDevShapeBounds;
339 }
340 }
341
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);
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400354 // Allow 8 bits each in x and y of subpixel positioning. But, note that we're allowing
355 // reuse for integer translations.
Robert Phillips52c17c42020-10-05 11:55:59 -0400356 SkFixed fracX = SkScalarToFixed(SkScalarFraction(tx)) & 0x0000FF00;
357 SkFixed fracY = SkScalarToFixed(SkScalarFraction(ty)) & 0x0000FF00;
358
359 builder[0] = SkFloat2Bits(sx);
360 builder[1] = SkFloat2Bits(sy);
361 builder[2] = SkFloat2Bits(kx);
362 builder[3] = SkFloat2Bits(ky);
363 // Distinguish between hairline and filled paths. For hairlines, we also need to include
364 // the cap. (SW grows hairlines by 0.5 pixel with round and square caps). Note that
365 // stroke-and-fill of hairlines is turned into pure fill by SkStrokeRec, so this covers
366 // all cases we might see.
367 uint32_t styleBits = shape.style().isSimpleHairline()
368 ? ((shape.style().strokeRec().getCap() << 1) | 1)
369 : 0;
370 builder[4] = fracX | (fracY >> 8) | (styleBits << 16);
371
372 SkMaskFilterBase::BlurRec rec;
373 SkAssertResult(as_MFB(maskFilter)->asABlur(&rec));
374
375 builder[5] = rec.fStyle; // TODO: we could put this with the other style bits
376 builder[6] = SkFloat2Bits(rec.fSigma);
377 shape.writeUnstyledKey(&builder[7]);
378 }
379#endif
380
381 return true;
382}
383
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400384static GrSurfaceProxyView hw_create_filtered_mask(GrRecordingContext* rContext,
385 GrRenderTargetContext* renderTargetContext,
386 const SkMatrix& viewMatrix,
387 const GrStyledShape& shape,
388 const SkMaskFilterBase* filter,
389 const SkIRect& unclippedDevShapeBounds,
390 const SkIRect& clipBounds,
391 SkIRect* maskRect,
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400392 GrUniqueKey* key) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400393 GrSurfaceProxyView filteredMaskView;
394
395 if (filter->canFilterMaskGPU(shape,
396 unclippedDevShapeBounds,
397 clipBounds,
398 viewMatrix,
399 maskRect)) {
400 if (clip_bounds_quick_reject(clipBounds, *maskRect)) {
401 // clipped out
402 return {};
403 }
404
405 GrProxyProvider* proxyProvider = rContext->priv().proxyProvider();
406
407 // TODO: this path should also use the thread-safe proxy-view cache!
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400408 if (key->isValid()) {
409 filteredMaskView = find_filtered_mask(proxyProvider, *key);
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400410 }
411
412 if (!filteredMaskView) {
413 std::unique_ptr<GrRenderTargetContext> maskRTC(create_mask_GPU(
414 rContext,
415 *maskRect,
416 viewMatrix,
417 shape,
418 renderTargetContext->numSamples()));
419 if (maskRTC) {
420 filteredMaskView = filter->filterMaskGPU(rContext,
421 maskRTC->readSurfaceView(),
422 maskRTC->colorInfo().colorType(),
423 maskRTC->colorInfo().alphaType(),
424 viewMatrix,
425 *maskRect);
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400426 if (filteredMaskView && key->isValid()) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400427 SkASSERT(filteredMaskView.asTextureProxy());
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400428
429 // This customData isn't being used yet
430 key->setCustomData(create_data(*maskRect, unclippedDevShapeBounds));
431 proxyProvider->assignUniqueKeyToProxy(*key, filteredMaskView.asTextureProxy());
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400432 }
433 }
434 }
435 }
436
437 return filteredMaskView;
438}
439
440static void draw_shape_with_mask_filter(GrRecordingContext* rContext,
Robert Phillips27927a52018-08-20 13:18:12 -0400441 GrRenderTargetContext* renderTargetContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400442 const GrClip* clip,
Robert Phillips27927a52018-08-20 13:18:12 -0400443 GrPaint&& paint,
Robert Phillips27927a52018-08-20 13:18:12 -0400444 const SkMatrix& viewMatrix,
445 const SkMaskFilterBase* maskFilter,
Michael Ludwig2686d692020-04-17 20:21:37 +0000446 const GrStyledShape& origShape) {
Robert Phillips27927a52018-08-20 13:18:12 -0400447 SkASSERT(maskFilter);
448
Michael Ludwig2686d692020-04-17 20:21:37 +0000449 const GrStyledShape* shape = &origShape;
450 SkTLazy<GrStyledShape> tmpShape;
Robert Phillips27927a52018-08-20 13:18:12 -0400451
452 if (origShape.style().applies()) {
453 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
454 if (0 == styleScale) {
455 return;
bsalomon6663acf2016-05-10 09:14:17 -0700456 }
Robert Phillips27927a52018-08-20 13:18:12 -0400457
458 tmpShape.init(origShape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale));
John Stilesa008b0f2020-08-16 08:48:02 -0400459 if (tmpShape->isEmpty()) {
Robert Phillips27927a52018-08-20 13:18:12 -0400460 return;
461 }
462
463 shape = tmpShape.get();
bsalomon6663acf2016-05-10 09:14:17 -0700464 }
bsalomonc55271f2015-11-09 11:55:57 -0800465
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400466 if (maskFilter->directFilterMaskGPU(rContext, renderTargetContext, std::move(paint), clip,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400467 viewMatrix, *shape)) {
Robert Phillipsb889f202018-08-17 19:55:59 +0000468 // the mask filter was able to draw itself directly, so there's nothing
469 // left to do.
470 return;
471 }
Mike Klein16885072018-12-11 09:54:31 -0500472 assert_alive(paint);
Robert Phillipsb889f202018-08-17 19:55:59 +0000473
Robert Phillips27927a52018-08-20 13:18:12 -0400474 // If the path is hairline, ignore inverse fill.
475 bool inverseFilled = shape->inverseFilled() &&
476 !GrPathRenderer::IsStrokeHairlineOrEquivalent(shape->style(),
477 viewMatrix, nullptr);
478
479 SkIRect unclippedDevShapeBounds, devClipBounds;
Robert Phillips31c080b2018-08-23 10:45:32 -0400480 if (!get_shape_and_clip_bounds(renderTargetContext, clip, *shape, viewMatrix,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400481 &unclippedDevShapeBounds, &devClipBounds)) {
Robert Phillips27927a52018-08-20 13:18:12 -0400482 // TODO: just cons up an opaque mask here
483 if (!inverseFilled) {
484 return;
485 }
486 }
487
Robert Phillips31c080b2018-08-23 10:45:32 -0400488 GrUniqueKey maskKey;
Robert Phillips52c17c42020-10-05 11:55:59 -0400489 SkIRect boundsForClip;
490 if (!compute_key_and_clip_bounds(&maskKey, &boundsForClip,
491 renderTargetContext->caps(),
492 viewMatrix, inverseFilled,
493 maskFilter, *shape,
494 unclippedDevShapeBounds,
495 devClipBounds)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400496 return; // 'shape' was entirely clipped out
Robert Phillips31c080b2018-08-23 10:45:32 -0400497 }
498
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400499 GrSurfaceProxyView filteredMaskView;
Robert Phillips31c080b2018-08-23 10:45:32 -0400500 SkIRect maskRect;
bsalomonc55271f2015-11-09 11:55:57 -0800501
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400502 if (rContext->asDirectContext()) {
503 filteredMaskView = hw_create_filtered_mask(rContext, renderTargetContext,
504 viewMatrix, *shape, maskFilter,
505 unclippedDevShapeBounds, boundsForClip,
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400506 &maskRect, &maskKey);
Brian Salomond005b692020-04-01 15:47:05 -0400507 if (filteredMaskView) {
Brian Salomonfc118442019-11-22 19:09:27 -0500508 if (draw_mask(renderTargetContext, clip, viewMatrix, maskRect, std::move(paint),
Greg Daniel5c082492020-01-29 15:06:49 -0500509 std::move(filteredMaskView))) {
Robert Phillips20390c32018-08-17 11:01:03 -0400510 // This path is completely drawn
511 return;
512 }
Mike Klein16885072018-12-11 09:54:31 -0500513 assert_alive(paint);
Robert Phillips20390c32018-08-17 11:01:03 -0400514 }
bsalomonc55271f2015-11-09 11:55:57 -0800515 }
516
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400517 // Either HW mask rendering failed or we're in a DDL recording thread
518 filteredMaskView = sw_create_filtered_mask(rContext,
519 viewMatrix, *shape, maskFilter,
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400520 unclippedDevShapeBounds, boundsForClip,
521 &maskRect, &maskKey);
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400522 if (filteredMaskView) {
523 if (draw_mask(renderTargetContext, clip, viewMatrix, maskRect, std::move(paint),
524 std::move(filteredMaskView))) {
525 return;
526 }
527 assert_alive(paint);
528 }
bsalomon6663acf2016-05-10 09:14:17 -0700529}
530
Robert Phillips7af8fe52019-02-14 17:27:00 -0500531void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext* context,
Robert Phillips27927a52018-08-20 13:18:12 -0400532 GrRenderTargetContext* renderTargetContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400533 const GrClip* clip,
Michael Ludwig2686d692020-04-17 20:21:37 +0000534 const GrStyledShape& shape,
Robert Phillips27927a52018-08-20 13:18:12 -0400535 GrPaint&& paint,
Robert Phillips27927a52018-08-20 13:18:12 -0400536 const SkMatrix& viewMatrix,
537 const SkMaskFilter* mf) {
Robert Phillips793324c2018-08-24 13:53:56 -0400538 draw_shape_with_mask_filter(context, renderTargetContext, clip, std::move(paint),
Robert Phillips27927a52018-08-20 13:18:12 -0400539 viewMatrix, as_MFB(mf), shape);
bsalomonc55271f2015-11-09 11:55:57 -0800540}
541
Robert Phillips69893702019-02-22 11:16:30 -0500542void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext* context,
Robert Phillips27927a52018-08-20 13:18:12 -0400543 GrRenderTargetContext* renderTargetContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400544 const GrClip* clip,
Robert Phillips27927a52018-08-20 13:18:12 -0400545 const SkPaint& paint,
Brian Osman449b1152020-04-15 16:43:00 -0400546 const SkMatrixProvider& matrixProvider,
Michael Ludwig2686d692020-04-17 20:21:37 +0000547 const GrStyledShape& shape) {
Robert Phillips9eb00022020-06-30 15:30:12 -0400548 if (context->abandoned()) {
Robert Phillipsbebfd412018-03-08 11:07:23 -0500549 return;
550 }
551
robertphillipsccb1b572015-05-27 11:02:55 -0700552 GrPaint grPaint;
Brian Osman449b1152020-04-15 16:43:00 -0400553 if (!SkPaintToGrPaint(context, renderTargetContext->colorInfo(), paint, matrixProvider,
554 &grPaint)) {
robertphillipsccb1b572015-05-27 11:02:55 -0700555 return;
556 }
Robert Phillips27927a52018-08-20 13:18:12 -0400557
Brian Osman449b1152020-04-15 16:43:00 -0400558 const SkMatrix& viewMatrix(matrixProvider.localToDevice());
Mike Reed80747ef2018-01-23 15:29:32 -0500559 SkMaskFilterBase* mf = as_MFB(paint.getMaskFilter());
Mike Reedbfadcf02018-01-20 22:24:21 +0000560 if (mf && !mf->hasFragmentProcessor()) {
Robert Phillipsa29a9562016-10-20 09:40:55 -0400561 // The MaskFilter wasn't already handled in SkPaintToGrPaint
Robert Phillips793324c2018-08-24 13:53:56 -0400562 draw_shape_with_mask_filter(context, renderTargetContext, clip, std::move(grPaint),
Robert Phillips27927a52018-08-20 13:18:12 -0400563 viewMatrix, mf, shape);
bsalomonc55271f2015-11-09 11:55:57 -0800564 } else {
Robert Phillips793324c2018-08-24 13:53:56 -0400565 GrAA aa = GrAA(paint.isAntiAlias());
Robert Phillips27927a52018-08-20 13:18:12 -0400566 renderTargetContext->drawShape(clip, std::move(grPaint), aa, viewMatrix, shape);
robertphillipsccb1b572015-05-27 11:02:55 -0700567 }
robertphillipsccb1b572015-05-27 11:02:55 -0700568}