blob: 010d53f3d8f9ff1e28700d3b1bb52a75ea45b3a2 [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 Phillipsfde67e42020-10-07 15:33:43 -040010#include "include/gpu/GrDirectContext.h"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040011#include "include/gpu/GrRecordingContext.h"
Greg Daniel6f5441a2020-01-28 17:02:49 -050012#include "src/gpu/GrBitmapTextureMaker.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrCaps.h"
Adlai Hollera0693042020-10-14 11:23:11 -040014#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrFixedClip.h"
16#include "src/gpu/GrProxyProvider.h"
17#include "src/gpu/GrRecordingContextPriv.h"
18#include "src/gpu/GrRenderTargetContext.h"
19#include "src/gpu/GrRenderTargetContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrSoftwarePathRenderer.h"
21#include "src/gpu/GrStyle.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040022#include "src/gpu/GrTextureProxy.h"
Robert Phillipsd464feb2020-10-08 11:00:02 -040023#include "src/gpu/GrThreadSafeCache.h"
Brian Salomonb8f098d2020-01-07 11:15:44 -050024#include "src/gpu/effects/GrTextureEffect.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000025#include "src/gpu/geometry/GrStyledShape.h"
Robert Phillipsc7c2baf2018-03-08 09:51:04 -050026
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "include/core/SkPaint.h"
28#include "src/core/SkDraw.h"
29#include "src/core/SkMaskFilterBase.h"
Brian Osman449b1152020-04-15 16:43:00 -040030#include "src/core/SkMatrixProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/core/SkTLazy.h"
32#include "src/gpu/SkGr.h"
robertphillipsccb1b572015-05-27 11:02:55 -070033
34static bool clip_bounds_quick_reject(const SkIRect& clipBounds, const SkIRect& rect) {
35 return clipBounds.isEmpty() || rect.isEmpty() || !SkIRect::Intersects(clipBounds, rect);
36}
37
Brian Salomond005b692020-04-01 15:47:05 -040038static constexpr auto kMaskOrigin = kTopLeft_GrSurfaceOrigin;
39
robertphillipsccb1b572015-05-27 11:02:55 -070040// Draw a mask using the supplied paint. Since the coverage/geometry
41// is already burnt into the mask this boils down to a rect draw.
42// Return true if the mask was successfully drawn.
Robert Phillips296b1cc2017-03-15 10:42:12 -040043static bool draw_mask(GrRenderTargetContext* renderTargetContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -040044 const GrClip* clip,
robertphillipsccb1b572015-05-27 11:02:55 -070045 const SkMatrix& viewMatrix,
robertphillipsf054b172016-05-13 05:06:19 -070046 const SkIRect& maskRect,
Brian Salomon82f44312017-01-11 13:42:54 -050047 GrPaint&& paint,
Greg Daniel5c082492020-01-29 15:06:49 -050048 GrSurfaceProxyView mask) {
Brian Salomon82f44312017-01-11 13:42:54 -050049 SkMatrix inverse;
50 if (!viewMatrix.invert(&inverse)) {
51 return false;
52 }
Robert Phillips4a24da52016-12-14 09:00:07 -050053
Mike Reed1f607332020-05-21 12:11:27 -040054 SkMatrix matrix = SkMatrix::Translate(-SkIntToScalar(maskRect.fLeft),
Robert Phillips67c18d62017-01-20 12:44:06 -050055 -SkIntToScalar(maskRect.fTop));
Brian Salomon2ebd0c82016-10-03 17:15:28 -040056 matrix.preConcat(viewMatrix);
John Stiles41d91b62020-07-21 14:39:40 -040057 paint.setCoverageFragmentProcessor(
Greg Danield2ccbb52020-02-05 10:45:39 -050058 GrTextureEffect::Make(std::move(mask), kUnknown_SkAlphaType, matrix));
robertphillipsccb1b572015-05-27 11:02:55 -070059
Brian Salomon82f44312017-01-11 13:42:54 -050060 renderTargetContext->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050061 SkRect::Make(maskRect), inverse);
robertphillipsccb1b572015-05-27 11:02:55 -070062 return true;
63}
64
Robert Phillipsc7c2baf2018-03-08 09:51:04 -050065static void mask_release_proc(void* addr, void* /*context*/) {
66 SkMask::FreeImage(addr);
67}
68
Robert Phillips6e17ffe2020-10-06 14:52:11 -040069#ifdef SK_DEBUG
70// Brute force computation of the destination bounds of a SW filtered mask
71static SkIRect sw_calc_draw_rect(const SkMatrix& viewMatrix,
72 const GrStyledShape& shape,
73 const SkMaskFilter* filter,
74 const SkIRect& clipBounds) {
75 SkRect devBounds = shape.bounds();
76 viewMatrix.mapRect(&devBounds);
77
78 SkMask srcM, dstM;
79 if (!SkDraw::ComputeMaskBounds(devBounds, &clipBounds, filter, &viewMatrix, &srcM.fBounds)) {
80 return {};
81 }
82
83 srcM.fFormat = SkMask::kA8_Format;
84
85 if (!as_MFB(filter)->filterMask(&dstM, srcM, viewMatrix, nullptr)) {
86 return {};
87 }
88
89 return dstM.fBounds;
90}
91#endif
92
93// This stores the mapping from an unclipped, integerized, device-space, shape bounds to
94// the filtered mask's draw rect.
95struct DrawRectData {
96 SkIVector fOffset;
97 SkISize fSize;
98};
99
100static sk_sp<SkData> create_data(const SkIRect& drawRect, const SkIRect& origDevBounds) {
101
102 DrawRectData drawRectData { {drawRect.fLeft - origDevBounds.fLeft,
103 drawRect.fTop - origDevBounds.fTop},
104 drawRect.size() };
105
106 return SkData::MakeWithCopy(&drawRectData, sizeof(drawRectData));
107}
108
109static SkIRect extract_draw_rect_from_data(SkData* data, const SkIRect& origDevBounds) {
110 auto drawRectData = static_cast<const DrawRectData*>(data->data());
111
112 return SkIRect::MakeXYWH(origDevBounds.fLeft + drawRectData->fOffset.fX,
113 origDevBounds.fTop + drawRectData->fOffset.fY,
114 drawRectData->fSize.fWidth,
115 drawRectData->fSize.fHeight);
116}
117
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400118static GrSurfaceProxyView sw_create_filtered_mask(GrRecordingContext* rContext,
119 const SkMatrix& viewMatrix,
120 const GrStyledShape& shape,
121 const SkMaskFilter* filter,
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400122 const SkIRect& unclippedDevShapeBounds,
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400123 const SkIRect& clipBounds,
124 SkIRect* drawRect,
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400125 GrUniqueKey* key) {
Robert Phillips27927a52018-08-20 13:18:12 -0400126 SkASSERT(filter);
127 SkASSERT(!shape.style().applies());
128
Robert Phillipsd464feb2020-10-08 11:00:02 -0400129 auto threadSafeCache = rContext->priv().threadSafeCache();
Robert Phillips27927a52018-08-20 13:18:12 -0400130
Greg Daniel5c082492020-01-29 15:06:49 -0500131 GrSurfaceProxyView filteredMaskView;
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400132 sk_sp<SkData> data;
Robert Phillips27927a52018-08-20 13:18:12 -0400133
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400134 if (key->isValid()) {
Robert Phillipsd464feb2020-10-08 11:00:02 -0400135 std::tie(filteredMaskView, data) = threadSafeCache->findWithData(*key);
robertphillipsccb1b572015-05-27 11:02:55 -0700136 }
137
Brian Salomond005b692020-04-01 15:47:05 -0400138 if (filteredMaskView) {
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400139 SkASSERT(data);
Robert Phillipsc73c1af2020-09-29 13:47:39 -0400140 SkASSERT(kMaskOrigin == filteredMaskView.origin());
141
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400142 *drawRect = extract_draw_rect_from_data(data.get(), unclippedDevShapeBounds);
Robert Phillips4a24da52016-12-14 09:00:07 -0500143
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400144 SkDEBUGCODE(auto oldDrawRect = sw_calc_draw_rect(viewMatrix, shape, filter, clipBounds));
145 SkASSERT(*drawRect == oldDrawRect);
Robert Phillips31c080b2018-08-23 10:45:32 -0400146 } else {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400147 SkStrokeRec::InitStyle fillOrHairline = shape.style().isSimpleHairline()
148 ? SkStrokeRec::kHairline_InitStyle
149 : SkStrokeRec::kFill_InitStyle;
150
Robert Phillips31c080b2018-08-23 10:45:32 -0400151 // TODO: it seems like we could create an SkDraw here and set its fMatrix field rather
152 // than explicitly transforming the path to device space.
153 SkPath devPath;
154
155 shape.asPath(&devPath);
156
157 devPath.transform(viewMatrix);
158
159 SkMask srcM, dstM;
160 if (!SkDraw::DrawToMask(devPath, &clipBounds, filter, &viewMatrix, &srcM,
161 SkMask::kComputeBoundsAndRenderImage_CreateMode, fillOrHairline)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400162 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400163 }
164 SkAutoMaskFreeImage autoSrc(srcM.fImage);
165
166 SkASSERT(SkMask::kA8_Format == srcM.fFormat);
167
168 if (!as_MFB(filter)->filterMask(&dstM, srcM, viewMatrix, nullptr)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400169 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400170 }
171 // this will free-up dstM when we're done (allocated in filterMask())
172 SkAutoMaskFreeImage autoDst(dstM.fImage);
173
174 if (clip_bounds_quick_reject(clipBounds, dstM.fBounds)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400175 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400176 }
177
178 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
179 // the current clip (and identity matrix) and GrPaint settings
180 SkBitmap bm;
181 if (!bm.installPixels(SkImageInfo::MakeA8(dstM.fBounds.width(), dstM.fBounds.height()),
182 autoDst.release(), dstM.fRowBytes, mask_release_proc, nullptr)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400183 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400184 }
185 bm.setImmutable();
186
Robert Phillipsc73c1af2020-09-29 13:47:39 -0400187 GrBitmapTextureMaker maker(rContext, bm, SkBackingFit::kApprox);
Brian Salomon7e67dca2020-07-21 09:27:25 -0400188 filteredMaskView = maker.view(GrMipmapped::kNo);
Greg Danielcc104db2020-02-03 14:17:08 -0500189 if (!filteredMaskView.proxy()) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400190 return {};
Robert Phillips31c080b2018-08-23 10:45:32 -0400191 }
192
Brian Salomond005b692020-04-01 15:47:05 -0400193 SkASSERT(kMaskOrigin == filteredMaskView.origin());
Robert Phillips59b39e72018-08-30 11:51:25 -0400194
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400195 *drawRect = dstM.fBounds;
Robert Phillips31c080b2018-08-23 10:45:32 -0400196
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400197 if (key->isValid()) {
198 key->setCustomData(create_data(*drawRect, unclippedDevShapeBounds));
Robert Phillipsd464feb2020-10-08 11:00:02 -0400199 std::tie(filteredMaskView, data) = threadSafeCache->addWithData(*key, filteredMaskView);
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400200 // If we got a different view back from 'addWithData' it could have a different drawRect
201 *drawRect = extract_draw_rect_from_data(data.get(), unclippedDevShapeBounds);
Robert Phillips31c080b2018-08-23 10:45:32 -0400202 }
Robert Phillips48ce22b2018-03-23 10:33:12 -0400203 }
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500204
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400205 return filteredMaskView;
robertphillipsccb1b572015-05-27 11:02:55 -0700206}
207
Robert Phillips40b05c32019-09-20 12:40:55 -0400208// Create a mask of 'shape' and return the resulting renderTargetContext
209static std::unique_ptr<GrRenderTargetContext> create_mask_GPU(GrRecordingContext* context,
210 const SkIRect& maskRect,
211 const SkMatrix& origViewMatrix,
Michael Ludwig2686d692020-04-17 20:21:37 +0000212 const GrStyledShape& shape,
Robert Phillips40b05c32019-09-20 12:40:55 -0400213 int sampleCnt) {
Chris Dalton0493fbd2019-09-18 15:49:46 -0600214 // Use GrResourceProvider::MakeApprox to implement our own approximate size matching, but demand
215 // a "SkBackingFit::kExact" size match on the actual render target. We do this because the
216 // filter will reach outside the src bounds, so we need to pre-clear these values to ensure a
217 // "decal" sampling effect (i.e., ensure reads outside the src bounds return alpha=0).
218 //
219 // FIXME: Reads outside the left and top edges will actually clamp to the edge pixel. And in the
220 // event that MakeApprox does not change the size, reads outside the right and/or bottom will do
221 // the same. We should offset our filter within the render target and expand the size as needed
222 // to guarantee at least 1px of padding on all sides.
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400223 auto approxSize = GrResourceProvider::MakeApprox(maskRect.size());
Greg Daniele20fcad2020-01-08 11:52:34 -0500224 auto rtContext = GrRenderTargetContext::MakeWithFallback(
225 context, GrColorType::kAlpha_8, nullptr, SkBackingFit::kExact, approxSize, sampleCnt,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400226 GrMipmapped::kNo, GrProtected::kNo, kMaskOrigin);
robertphillipsd728f0c2016-11-21 11:05:03 -0800227 if (!rtContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700228 return nullptr;
robertphillipsccb1b572015-05-27 11:02:55 -0700229 }
230
Chris Dalton0493fbd2019-09-18 15:49:46 -0600231 rtContext->clear(SK_PMColor4fTRANSPARENT);
robertphillipsccb1b572015-05-27 11:02:55 -0700232
Brian Salomon82f44312017-01-11 13:42:54 -0500233 GrPaint maskPaint;
234 maskPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op);
robertphillipsccb1b572015-05-27 11:02:55 -0700235
236 // setup new clip
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400237 GrFixedClip clip(rtContext->dimensions(), SkIRect::MakeWH(maskRect.width(), maskRect.height()));
robertphillipsccb1b572015-05-27 11:02:55 -0700238
Brian Salomond005b692020-04-01 15:47:05 -0400239 // Draw the mask into maskTexture with the path's integerized top-left at the origin using
240 // maskPaint.
Robert Phillips27927a52018-08-20 13:18:12 -0400241 SkMatrix viewMatrix = origViewMatrix;
242 viewMatrix.postTranslate(-SkIntToScalar(maskRect.fLeft), -SkIntToScalar(maskRect.fTop));
Michael Ludwig7c12e282020-05-29 09:54:07 -0400243 rtContext->drawShape(&clip, std::move(maskPaint), GrAA::kYes, viewMatrix, shape);
Robert Phillips40b05c32019-09-20 12:40:55 -0400244 return rtContext;
robertphillipsccb1b572015-05-27 11:02:55 -0700245}
246
Michael Ludwig2686d692020-04-17 20:21:37 +0000247static bool get_unclipped_shape_dev_bounds(const GrStyledShape& shape, const SkMatrix& matrix,
Robert Phillips27927a52018-08-20 13:18:12 -0400248 SkIRect* devBounds) {
249 SkRect shapeBounds = shape.styledBounds();
250 if (shapeBounds.isEmpty()) {
251 return false;
252 }
253 SkRect shapeDevBounds;
254 matrix.mapRect(&shapeDevBounds, shapeBounds);
255 // Even though these are "unclipped" bounds we still clip to the int32_t range.
256 // This is the largest int32_t that is representable exactly as a float. The next 63 larger ints
257 // would round down to this value when cast to a float, but who really cares.
258 // INT32_MIN is exactly representable.
259 static constexpr int32_t kMaxInt = 2147483520;
260 if (!shapeDevBounds.intersect(SkRect::MakeLTRB(INT32_MIN, INT32_MIN, kMaxInt, kMaxInt))) {
261 return false;
262 }
263 // Make sure that the resulting SkIRect can have representable width and height
264 if (SkScalarRoundToInt(shapeDevBounds.width()) > kMaxInt ||
265 SkScalarRoundToInt(shapeDevBounds.height()) > kMaxInt) {
266 return false;
267 }
268 shapeDevBounds.roundOut(devBounds);
269 return true;
270}
bsalomonc55271f2015-11-09 11:55:57 -0800271
Robert Phillips27927a52018-08-20 13:18:12 -0400272// Gets the shape bounds, the clip bounds, and the intersection (if any). Returns false if there
273// is no intersection.
274static bool get_shape_and_clip_bounds(GrRenderTargetContext* renderTargetContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400275 const GrClip* clip,
Michael Ludwig2686d692020-04-17 20:21:37 +0000276 const GrStyledShape& shape,
Robert Phillips27927a52018-08-20 13:18:12 -0400277 const SkMatrix& matrix,
278 SkIRect* unclippedDevShapeBounds,
279 SkIRect* devClipBounds) {
280 // compute bounds as intersection of rt size, clip, and path
Michael Ludwige06a8972020-06-11 10:29:00 -0400281 *devClipBounds = clip ? clip->getConservativeBounds()
Michael Ludwig7c12e282020-05-29 09:54:07 -0400282 : SkIRect::MakeWH(renderTargetContext->width(),
283 renderTargetContext->height());
bsalomonc55271f2015-11-09 11:55:57 -0800284
Robert Phillips27927a52018-08-20 13:18:12 -0400285 if (!get_unclipped_shape_dev_bounds(shape, matrix, unclippedDevShapeBounds)) {
Brian Salomon44207f32020-01-06 15:20:18 -0500286 *unclippedDevShapeBounds = SkIRect::MakeEmpty();
Robert Phillips27927a52018-08-20 13:18:12 -0400287 return false;
bsalomonc55271f2015-11-09 11:55:57 -0800288 }
289
Robert Phillips27927a52018-08-20 13:18:12 -0400290 return true;
291}
292
Robert Phillips52c17c42020-10-05 11:55:59 -0400293// The key and clip-bounds are computed together because the caching decision can impact the
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400294// clip-bound - since we only cache un-clipped masks the clip can be removed entirely.
Robert Phillips52c17c42020-10-05 11:55:59 -0400295// A 'false' return value indicates that the shape is known to be clipped away.
296static bool compute_key_and_clip_bounds(GrUniqueKey* maskKey,
297 SkIRect* boundsForClip,
298 const GrCaps* caps,
299 const SkMatrix& viewMatrix,
300 bool inverseFilled,
301 const SkMaskFilterBase* maskFilter,
302 const GrStyledShape& shape,
303 const SkIRect& unclippedDevShapeBounds,
304 const SkIRect& devClipBounds) {
305 *boundsForClip = devClipBounds;
306
307#ifndef SK_DISABLE_MASKFILTERED_MASK_CACHING
308 // To prevent overloading the cache with entries during animations we limit the cache of masks
309 // to cases where the matrix preserves axis alignment.
310 bool useCache = !inverseFilled && viewMatrix.preservesAxisAlignment() &&
311 shape.hasUnstyledKey() && as_MFB(maskFilter)->asABlur(nullptr);
312
313 if (useCache) {
314 SkIRect clippedMaskRect, unClippedMaskRect;
315 maskFilter->canFilterMaskGPU(shape, unclippedDevShapeBounds, devClipBounds,
316 viewMatrix, &clippedMaskRect);
317 maskFilter->canFilterMaskGPU(shape, unclippedDevShapeBounds, unclippedDevShapeBounds,
318 viewMatrix, &unClippedMaskRect);
319 if (clippedMaskRect.isEmpty()) {
320 return false;
321 }
322
323 // Use the cache only if >50% of the filtered mask is visible.
324 int unclippedWidth = unClippedMaskRect.width();
325 int unclippedHeight = unClippedMaskRect.height();
326 int64_t unclippedArea = sk_64_mul(unclippedWidth, unclippedHeight);
327 int64_t clippedArea = sk_64_mul(clippedMaskRect.width(), clippedMaskRect.height());
328 int maxTextureSize = caps->maxTextureSize();
329 if (unclippedArea > 2 * clippedArea || unclippedWidth > maxTextureSize ||
330 unclippedHeight > maxTextureSize) {
331 useCache = false;
332 } else {
333 // Make the clip not affect the mask
334 *boundsForClip = unclippedDevShapeBounds;
335 }
336 }
337
338 if (useCache) {
339 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
340 GrUniqueKey::Builder builder(maskKey, kDomain, 5 + 2 + shape.unstyledKeySize(),
341 "Mask Filtered Masks");
342
343 // We require the upper left 2x2 of the matrix to match exactly for a cache hit.
344 SkScalar sx = viewMatrix.get(SkMatrix::kMScaleX);
345 SkScalar sy = viewMatrix.get(SkMatrix::kMScaleY);
346 SkScalar kx = viewMatrix.get(SkMatrix::kMSkewX);
347 SkScalar ky = viewMatrix.get(SkMatrix::kMSkewY);
348 SkScalar tx = viewMatrix.get(SkMatrix::kMTransX);
349 SkScalar ty = viewMatrix.get(SkMatrix::kMTransY);
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400350 // Allow 8 bits each in x and y of subpixel positioning. But, note that we're allowing
351 // reuse for integer translations.
Robert Phillips52c17c42020-10-05 11:55:59 -0400352 SkFixed fracX = SkScalarToFixed(SkScalarFraction(tx)) & 0x0000FF00;
353 SkFixed fracY = SkScalarToFixed(SkScalarFraction(ty)) & 0x0000FF00;
354
355 builder[0] = SkFloat2Bits(sx);
356 builder[1] = SkFloat2Bits(sy);
357 builder[2] = SkFloat2Bits(kx);
358 builder[3] = SkFloat2Bits(ky);
359 // Distinguish between hairline and filled paths. For hairlines, we also need to include
360 // the cap. (SW grows hairlines by 0.5 pixel with round and square caps). Note that
361 // stroke-and-fill of hairlines is turned into pure fill by SkStrokeRec, so this covers
362 // all cases we might see.
363 uint32_t styleBits = shape.style().isSimpleHairline()
364 ? ((shape.style().strokeRec().getCap() << 1) | 1)
365 : 0;
366 builder[4] = fracX | (fracY >> 8) | (styleBits << 16);
367
368 SkMaskFilterBase::BlurRec rec;
369 SkAssertResult(as_MFB(maskFilter)->asABlur(&rec));
370
371 builder[5] = rec.fStyle; // TODO: we could put this with the other style bits
372 builder[6] = SkFloat2Bits(rec.fSigma);
373 shape.writeUnstyledKey(&builder[7]);
374 }
375#endif
376
377 return true;
378}
379
Robert Phillipsfde67e42020-10-07 15:33:43 -0400380static GrSurfaceProxyView hw_create_filtered_mask(GrDirectContext* dContext,
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400381 GrRenderTargetContext* renderTargetContext,
382 const SkMatrix& viewMatrix,
383 const GrStyledShape& shape,
384 const SkMaskFilterBase* filter,
385 const SkIRect& unclippedDevShapeBounds,
386 const SkIRect& clipBounds,
387 SkIRect* maskRect,
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400388 GrUniqueKey* key) {
Robert Phillipsfde67e42020-10-07 15:33:43 -0400389 if (!filter->canFilterMaskGPU(shape,
390 unclippedDevShapeBounds,
391 clipBounds,
392 viewMatrix,
393 maskRect)) {
394 return {};
395 }
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400396
Robert Phillipsfde67e42020-10-07 15:33:43 -0400397 if (clip_bounds_quick_reject(clipBounds, *maskRect)) {
398 // clipped out
399 return {};
400 }
401
Robert Phillipsd464feb2020-10-08 11:00:02 -0400402 auto threadSafeCache = dContext->priv().threadSafeCache();
Robert Phillipsfde67e42020-10-07 15:33:43 -0400403
404 GrSurfaceProxyView lazyView;
Robert Phillipsd464feb2020-10-08 11:00:02 -0400405 sk_sp<GrThreadSafeCache::Trampoline> trampoline;
Robert Phillipsfde67e42020-10-07 15:33:43 -0400406
407 if (key->isValid()) {
408 // In this case, we want GPU-filtered masks to have priority over SW-generated ones so
409 // we pre-emptively add a lazy-view to the cache and fill it in later.
Robert Phillipsd464feb2020-10-08 11:00:02 -0400410 std::tie(lazyView, trampoline) = GrThreadSafeCache::CreateLazyView(
Robert Phillipsfde67e42020-10-07 15:33:43 -0400411 dContext, GrColorType::kAlpha_8, maskRect->size(),
412 kMaskOrigin, SkBackingFit::kApprox);
413 if (!lazyView) {
414 return {}; // fall back to a SW-created mask - 'create_mask_GPU' probably won't succeed
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400415 }
416
Robert Phillipsfde67e42020-10-07 15:33:43 -0400417 key->setCustomData(create_data(*maskRect, unclippedDevShapeBounds));
Robert Phillipsd464feb2020-10-08 11:00:02 -0400418 auto [cachedView, data] = threadSafeCache->findOrAddWithData(*key, lazyView);
Robert Phillipsfde67e42020-10-07 15:33:43 -0400419 if (cachedView != lazyView) {
420 // In this case, the gpu-thread lost out to a recording thread - use its result.
421 SkASSERT(data);
422 SkASSERT(cachedView.asTextureProxy());
423 SkASSERT(cachedView.origin() == kMaskOrigin);
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400424
Robert Phillipsfde67e42020-10-07 15:33:43 -0400425 *maskRect = extract_draw_rect_from_data(data.get(), unclippedDevShapeBounds);
426 return cachedView;
427 }
428 }
429
430 std::unique_ptr<GrRenderTargetContext> maskRTC(create_mask_GPU(
431 dContext,
432 *maskRect,
433 viewMatrix,
434 shape,
435 renderTargetContext->numSamples()));
436 if (!maskRTC) {
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400437 if (key->isValid()) {
Robert Phillipsfde67e42020-10-07 15:33:43 -0400438 // It is very unlikely that 'create_mask_GPU' will fail after 'CreateLazyView'
439 // succeeded but, if it does, remove the lazy-view from the cache and fallback to
440 // a SW-created mask. Note that any recording threads that glommed onto the
441 // lazy-view will have to, later, drop those draws.
Robert Phillipsd464feb2020-10-08 11:00:02 -0400442 threadSafeCache->remove(*key);
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400443 }
Robert Phillipsfde67e42020-10-07 15:33:43 -0400444 return {};
445 }
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400446
Robert Phillipsfde67e42020-10-07 15:33:43 -0400447 auto filteredMaskView = filter->filterMaskGPU(dContext,
448 maskRTC->readSurfaceView(),
449 maskRTC->colorInfo().colorType(),
450 maskRTC->colorInfo().alphaType(),
451 viewMatrix,
452 *maskRect);
453 if (!filteredMaskView) {
454 if (key->isValid()) {
455 // Remove the lazy-view from the cache and fallback to a SW-created mask. Note that
456 // any recording threads that glommed onto the lazy-view will have to, later, drop
457 // those draws.
Robert Phillipsd464feb2020-10-08 11:00:02 -0400458 threadSafeCache->remove(*key);
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400459 }
Robert Phillipsfde67e42020-10-07 15:33:43 -0400460 return {};
461 }
462
463 if (key->isValid()) {
464 SkASSERT(filteredMaskView.dimensions() == lazyView.dimensions());
465 SkASSERT(filteredMaskView.swizzle() == lazyView.swizzle());
466 SkASSERT(filteredMaskView.origin() == lazyView.origin());
467
468 trampoline->fProxy = filteredMaskView.asTextureProxyRef();
469 return lazyView;
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400470 }
471
472 return filteredMaskView;
473}
474
475static void draw_shape_with_mask_filter(GrRecordingContext* rContext,
Robert Phillips27927a52018-08-20 13:18:12 -0400476 GrRenderTargetContext* renderTargetContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400477 const GrClip* clip,
Robert Phillips27927a52018-08-20 13:18:12 -0400478 GrPaint&& paint,
Robert Phillips27927a52018-08-20 13:18:12 -0400479 const SkMatrix& viewMatrix,
480 const SkMaskFilterBase* maskFilter,
Michael Ludwig2686d692020-04-17 20:21:37 +0000481 const GrStyledShape& origShape) {
Robert Phillips27927a52018-08-20 13:18:12 -0400482 SkASSERT(maskFilter);
483
Michael Ludwig2686d692020-04-17 20:21:37 +0000484 const GrStyledShape* shape = &origShape;
485 SkTLazy<GrStyledShape> tmpShape;
Robert Phillips27927a52018-08-20 13:18:12 -0400486
487 if (origShape.style().applies()) {
488 SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
489 if (0 == styleScale) {
490 return;
bsalomon6663acf2016-05-10 09:14:17 -0700491 }
Robert Phillips27927a52018-08-20 13:18:12 -0400492
493 tmpShape.init(origShape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale));
John Stilesa008b0f2020-08-16 08:48:02 -0400494 if (tmpShape->isEmpty()) {
Robert Phillips27927a52018-08-20 13:18:12 -0400495 return;
496 }
497
498 shape = tmpShape.get();
bsalomon6663acf2016-05-10 09:14:17 -0700499 }
bsalomonc55271f2015-11-09 11:55:57 -0800500
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400501 if (maskFilter->directFilterMaskGPU(rContext, renderTargetContext, std::move(paint), clip,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400502 viewMatrix, *shape)) {
Robert Phillipsb889f202018-08-17 19:55:59 +0000503 // the mask filter was able to draw itself directly, so there's nothing
504 // left to do.
505 return;
506 }
Mike Klein16885072018-12-11 09:54:31 -0500507 assert_alive(paint);
Robert Phillipsb889f202018-08-17 19:55:59 +0000508
Robert Phillips27927a52018-08-20 13:18:12 -0400509 // If the path is hairline, ignore inverse fill.
510 bool inverseFilled = shape->inverseFilled() &&
511 !GrPathRenderer::IsStrokeHairlineOrEquivalent(shape->style(),
512 viewMatrix, nullptr);
513
514 SkIRect unclippedDevShapeBounds, devClipBounds;
Robert Phillips31c080b2018-08-23 10:45:32 -0400515 if (!get_shape_and_clip_bounds(renderTargetContext, clip, *shape, viewMatrix,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400516 &unclippedDevShapeBounds, &devClipBounds)) {
Robert Phillips27927a52018-08-20 13:18:12 -0400517 // TODO: just cons up an opaque mask here
518 if (!inverseFilled) {
519 return;
520 }
521 }
522
Robert Phillips31c080b2018-08-23 10:45:32 -0400523 GrUniqueKey maskKey;
Robert Phillips52c17c42020-10-05 11:55:59 -0400524 SkIRect boundsForClip;
525 if (!compute_key_and_clip_bounds(&maskKey, &boundsForClip,
526 renderTargetContext->caps(),
527 viewMatrix, inverseFilled,
528 maskFilter, *shape,
529 unclippedDevShapeBounds,
530 devClipBounds)) {
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400531 return; // 'shape' was entirely clipped out
Robert Phillips31c080b2018-08-23 10:45:32 -0400532 }
533
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400534 GrSurfaceProxyView filteredMaskView;
Robert Phillips31c080b2018-08-23 10:45:32 -0400535 SkIRect maskRect;
bsalomonc55271f2015-11-09 11:55:57 -0800536
Robert Phillipsfde67e42020-10-07 15:33:43 -0400537 if (auto dContext = rContext->asDirectContext()) {
538 filteredMaskView = hw_create_filtered_mask(dContext, renderTargetContext,
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400539 viewMatrix, *shape, maskFilter,
540 unclippedDevShapeBounds, boundsForClip,
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400541 &maskRect, &maskKey);
Brian Salomond005b692020-04-01 15:47:05 -0400542 if (filteredMaskView) {
Brian Salomonfc118442019-11-22 19:09:27 -0500543 if (draw_mask(renderTargetContext, clip, viewMatrix, maskRect, std::move(paint),
Greg Daniel5c082492020-01-29 15:06:49 -0500544 std::move(filteredMaskView))) {
Robert Phillips20390c32018-08-17 11:01:03 -0400545 // This path is completely drawn
546 return;
547 }
Mike Klein16885072018-12-11 09:54:31 -0500548 assert_alive(paint);
Robert Phillips20390c32018-08-17 11:01:03 -0400549 }
bsalomonc55271f2015-11-09 11:55:57 -0800550 }
551
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400552 // Either HW mask rendering failed or we're in a DDL recording thread
553 filteredMaskView = sw_create_filtered_mask(rContext,
554 viewMatrix, *shape, maskFilter,
Robert Phillips6e17ffe2020-10-06 14:52:11 -0400555 unclippedDevShapeBounds, boundsForClip,
556 &maskRect, &maskKey);
Robert Phillipsc3bdd1c2020-10-05 13:17:09 -0400557 if (filteredMaskView) {
558 if (draw_mask(renderTargetContext, clip, viewMatrix, maskRect, std::move(paint),
559 std::move(filteredMaskView))) {
560 return;
561 }
562 assert_alive(paint);
563 }
bsalomon6663acf2016-05-10 09:14:17 -0700564}
565
Robert Phillips7af8fe52019-02-14 17:27:00 -0500566void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext* context,
Robert Phillips27927a52018-08-20 13:18:12 -0400567 GrRenderTargetContext* renderTargetContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400568 const GrClip* clip,
Michael Ludwig2686d692020-04-17 20:21:37 +0000569 const GrStyledShape& shape,
Robert Phillips27927a52018-08-20 13:18:12 -0400570 GrPaint&& paint,
Robert Phillips27927a52018-08-20 13:18:12 -0400571 const SkMatrix& viewMatrix,
572 const SkMaskFilter* mf) {
Robert Phillips793324c2018-08-24 13:53:56 -0400573 draw_shape_with_mask_filter(context, renderTargetContext, clip, std::move(paint),
Robert Phillips27927a52018-08-20 13:18:12 -0400574 viewMatrix, as_MFB(mf), shape);
bsalomonc55271f2015-11-09 11:55:57 -0800575}
576
Robert Phillips69893702019-02-22 11:16:30 -0500577void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext* context,
Robert Phillips27927a52018-08-20 13:18:12 -0400578 GrRenderTargetContext* renderTargetContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400579 const GrClip* clip,
Robert Phillips27927a52018-08-20 13:18:12 -0400580 const SkPaint& paint,
Brian Osman449b1152020-04-15 16:43:00 -0400581 const SkMatrixProvider& matrixProvider,
Michael Ludwig2686d692020-04-17 20:21:37 +0000582 const GrStyledShape& shape) {
Robert Phillips9eb00022020-06-30 15:30:12 -0400583 if (context->abandoned()) {
Robert Phillipsbebfd412018-03-08 11:07:23 -0500584 return;
585 }
586
robertphillipsccb1b572015-05-27 11:02:55 -0700587 GrPaint grPaint;
Brian Osman449b1152020-04-15 16:43:00 -0400588 if (!SkPaintToGrPaint(context, renderTargetContext->colorInfo(), paint, matrixProvider,
589 &grPaint)) {
robertphillipsccb1b572015-05-27 11:02:55 -0700590 return;
591 }
Robert Phillips27927a52018-08-20 13:18:12 -0400592
Brian Osman449b1152020-04-15 16:43:00 -0400593 const SkMatrix& viewMatrix(matrixProvider.localToDevice());
Mike Reed80747ef2018-01-23 15:29:32 -0500594 SkMaskFilterBase* mf = as_MFB(paint.getMaskFilter());
Mike Reedbfadcf02018-01-20 22:24:21 +0000595 if (mf && !mf->hasFragmentProcessor()) {
Robert Phillipsa29a9562016-10-20 09:40:55 -0400596 // The MaskFilter wasn't already handled in SkPaintToGrPaint
Robert Phillips793324c2018-08-24 13:53:56 -0400597 draw_shape_with_mask_filter(context, renderTargetContext, clip, std::move(grPaint),
Robert Phillips27927a52018-08-20 13:18:12 -0400598 viewMatrix, mf, shape);
bsalomonc55271f2015-11-09 11:55:57 -0800599 } else {
Robert Phillips793324c2018-08-24 13:53:56 -0400600 GrAA aa = GrAA(paint.isAntiAlias());
Robert Phillips27927a52018-08-20 13:18:12 -0400601 renderTargetContext->drawShape(clip, std::move(grPaint), aa, viewMatrix, shape);
robertphillipsccb1b572015-05-27 11:02:55 -0700602 }
robertphillipsccb1b572015-05-27 11:02:55 -0700603}