/src/skia/src/gpu/GrBlurUtils.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
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 | | |
8 | | #include "src/gpu/GrBlurUtils.h" |
9 | | |
10 | | #if SK_GPU_V1 |
11 | | |
12 | | #include "include/gpu/GrDirectContext.h" |
13 | | #include "include/gpu/GrRecordingContext.h" |
14 | | #include "src/gpu/GrCaps.h" |
15 | | #include "src/gpu/GrDirectContextPriv.h" |
16 | | #include "src/gpu/GrFixedClip.h" |
17 | | #include "src/gpu/GrProxyProvider.h" |
18 | | #include "src/gpu/GrRecordingContextPriv.h" |
19 | | #include "src/gpu/GrResourceProvider.h" |
20 | | #include "src/gpu/GrStyle.h" |
21 | | #include "src/gpu/GrTextureProxy.h" |
22 | | #include "src/gpu/GrThreadSafeCache.h" |
23 | | #include "src/gpu/GrUtil.h" |
24 | | #include "src/gpu/SkGr.h" |
25 | | #include "src/gpu/effects/GrTextureEffect.h" |
26 | | #include "src/gpu/geometry/GrStyledShape.h" |
27 | | #include "src/gpu/v1/SurfaceDrawContext_v1.h" |
28 | | |
29 | | #include "include/core/SkPaint.h" |
30 | | #include "src/core/SkDraw.h" |
31 | | #include "src/core/SkMaskFilterBase.h" |
32 | | #include "src/core/SkMatrixProvider.h" |
33 | | #include "src/core/SkTLazy.h" |
34 | | #include "src/gpu/SkGr.h" |
35 | | |
36 | 17.9k | static bool clip_bounds_quick_reject(const SkIRect& clipBounds, const SkIRect& rect) { |
37 | 17.9k | return clipBounds.isEmpty() || rect.isEmpty() || !SkIRect::Intersects(clipBounds, rect); |
38 | 17.9k | } |
39 | | |
40 | | static constexpr auto kMaskOrigin = kTopLeft_GrSurfaceOrigin; |
41 | | |
42 | | // Draw a mask using the supplied paint. Since the coverage/geometry |
43 | | // is already burnt into the mask this boils down to a rect draw. |
44 | | // Return true if the mask was successfully drawn. |
45 | | static bool draw_mask(skgpu::v1::SurfaceDrawContext* sdc, |
46 | | const GrClip* clip, |
47 | | const SkMatrix& viewMatrix, |
48 | | const SkIRect& maskBounds, |
49 | | GrPaint&& paint, |
50 | 13.2k | GrSurfaceProxyView mask) { |
51 | 13.2k | SkMatrix inverse; |
52 | 13.2k | if (!viewMatrix.invert(&inverse)) { |
53 | 97 | return false; |
54 | 97 | } |
55 | | |
56 | 13.1k | mask.concatSwizzle(GrSwizzle("aaaa")); |
57 | | |
58 | 13.1k | SkMatrix matrix = SkMatrix::Translate(-SkIntToScalar(maskBounds.fLeft), |
59 | 13.1k | -SkIntToScalar(maskBounds.fTop)); |
60 | 13.1k | matrix.preConcat(viewMatrix); |
61 | 13.1k | paint.setCoverageFragmentProcessor( |
62 | 13.1k | GrTextureEffect::Make(std::move(mask), kUnknown_SkAlphaType, matrix)); |
63 | | |
64 | 13.1k | sdc->fillPixelsWithLocalMatrix(clip, std::move(paint), maskBounds, inverse); |
65 | 13.1k | return true; |
66 | 13.1k | } |
67 | | |
68 | 647 | static void mask_release_proc(void* addr, void* /*context*/) { |
69 | 647 | SkMask::FreeImage(addr); |
70 | 647 | } |
71 | | |
72 | | // This stores the mapping from an unclipped, integerized, device-space, shape bounds to |
73 | | // the filtered mask's draw rect. |
74 | | struct DrawRectData { |
75 | | SkIVector fOffset; |
76 | | SkISize fSize; |
77 | | }; |
78 | | |
79 | 81 | static sk_sp<SkData> create_data(const SkIRect& drawRect, const SkIRect& origDevBounds) { |
80 | | |
81 | 81 | DrawRectData drawRectData { {drawRect.fLeft - origDevBounds.fLeft, |
82 | 81 | drawRect.fTop - origDevBounds.fTop}, |
83 | 81 | drawRect.size() }; |
84 | | |
85 | 81 | return SkData::MakeWithCopy(&drawRectData, sizeof(drawRectData)); |
86 | 81 | } |
87 | | |
88 | 12 | static SkIRect extract_draw_rect_from_data(SkData* data, const SkIRect& origDevBounds) { |
89 | 12 | auto drawRectData = static_cast<const DrawRectData*>(data->data()); |
90 | | |
91 | 12 | return SkIRect::MakeXYWH(origDevBounds.fLeft + drawRectData->fOffset.fX, |
92 | 12 | origDevBounds.fTop + drawRectData->fOffset.fY, |
93 | 12 | drawRectData->fSize.fWidth, |
94 | 12 | drawRectData->fSize.fHeight); |
95 | 12 | } |
96 | | |
97 | | static GrSurfaceProxyView sw_create_filtered_mask(GrRecordingContext* rContext, |
98 | | const SkMatrix& viewMatrix, |
99 | | const GrStyledShape& shape, |
100 | | const SkMaskFilter* filter, |
101 | | const SkIRect& unclippedDevShapeBounds, |
102 | | const SkIRect& clipBounds, |
103 | | SkIRect* drawRect, |
104 | 5.38k | GrUniqueKey* key) { |
105 | 5.38k | SkASSERT(filter); |
106 | 5.38k | SkASSERT(!shape.style().applies()); |
107 | | |
108 | 5.38k | auto threadSafeCache = rContext->priv().threadSafeCache(); |
109 | | |
110 | 5.38k | GrSurfaceProxyView filteredMaskView; |
111 | 5.38k | sk_sp<SkData> data; |
112 | | |
113 | 5.38k | if (key->isValid()) { |
114 | 17 | std::tie(filteredMaskView, data) = threadSafeCache->findWithData(*key); |
115 | 17 | } |
116 | | |
117 | 5.38k | if (filteredMaskView) { |
118 | 0 | SkASSERT(data); |
119 | 0 | SkASSERT(kMaskOrigin == filteredMaskView.origin()); |
120 | |
|
121 | 0 | *drawRect = extract_draw_rect_from_data(data.get(), unclippedDevShapeBounds); |
122 | 5.38k | } else { |
123 | 5.38k | SkStrokeRec::InitStyle fillOrHairline = shape.style().isSimpleHairline() |
124 | 1.01k | ? SkStrokeRec::kHairline_InitStyle |
125 | 4.37k | : SkStrokeRec::kFill_InitStyle; |
126 | | |
127 | | // TODO: it seems like we could create an SkDraw here and set its fMatrix field rather |
128 | | // than explicitly transforming the path to device space. |
129 | 5.38k | SkPath devPath; |
130 | | |
131 | 5.38k | shape.asPath(&devPath); |
132 | | |
133 | 5.38k | devPath.transform(viewMatrix); |
134 | | |
135 | 5.38k | SkMask srcM, dstM; |
136 | 5.38k | if (!SkDraw::DrawToMask(devPath, &clipBounds, filter, &viewMatrix, &srcM, |
137 | 2.74k | SkMask::kComputeBoundsAndRenderImage_CreateMode, fillOrHairline)) { |
138 | 2.74k | return {}; |
139 | 2.74k | } |
140 | 2.63k | SkAutoMaskFreeImage autoSrc(srcM.fImage); |
141 | | |
142 | 2.63k | SkASSERT(SkMask::kA8_Format == srcM.fFormat); |
143 | | |
144 | 2.63k | if (!as_MFB(filter)->filterMask(&dstM, srcM, viewMatrix, nullptr)) { |
145 | 0 | return {}; |
146 | 0 | } |
147 | | // this will free-up dstM when we're done (allocated in filterMask()) |
148 | 2.63k | SkAutoMaskFreeImage autoDst(dstM.fImage); |
149 | | |
150 | 2.63k | if (clip_bounds_quick_reject(clipBounds, dstM.fBounds)) { |
151 | 1.98k | return {}; |
152 | 1.98k | } |
153 | | |
154 | | // we now have a device-aligned 8bit mask in dstM, ready to be drawn using |
155 | | // the current clip (and identity matrix) and GrPaint settings |
156 | 647 | SkBitmap bm; |
157 | 647 | if (!bm.installPixels(SkImageInfo::MakeA8(dstM.fBounds.width(), dstM.fBounds.height()), |
158 | 0 | autoDst.release(), dstM.fRowBytes, mask_release_proc, nullptr)) { |
159 | 0 | return {}; |
160 | 0 | } |
161 | 647 | bm.setImmutable(); |
162 | | |
163 | 647 | std::tie(filteredMaskView, std::ignore) = GrMakeUncachedBitmapProxyView( |
164 | 647 | rContext, |
165 | 647 | bm, |
166 | 647 | GrMipmapped::kNo, |
167 | 647 | SkBackingFit::kApprox); |
168 | 647 | if (!filteredMaskView) { |
169 | 16 | return {}; |
170 | 16 | } |
171 | | |
172 | 631 | SkASSERT(kMaskOrigin == filteredMaskView.origin()); |
173 | | |
174 | 631 | *drawRect = dstM.fBounds; |
175 | | |
176 | 631 | if (key->isValid()) { |
177 | 7 | key->setCustomData(create_data(*drawRect, unclippedDevShapeBounds)); |
178 | 7 | std::tie(filteredMaskView, data) = threadSafeCache->addWithData(*key, filteredMaskView); |
179 | | // If we got a different view back from 'addWithData' it could have a different drawRect |
180 | 7 | *drawRect = extract_draw_rect_from_data(data.get(), unclippedDevShapeBounds); |
181 | 7 | } |
182 | 631 | } |
183 | | |
184 | 631 | return filteredMaskView; |
185 | 5.38k | } |
186 | | |
187 | | // Create a mask of 'shape' and return the resulting surfaceDrawContext |
188 | | static std::unique_ptr<skgpu::v1::SurfaceDrawContext> create_mask_GPU( |
189 | | GrRecordingContext* rContext, |
190 | | const SkIRect& maskRect, |
191 | | const SkMatrix& origViewMatrix, |
192 | | const GrStyledShape& shape, |
193 | 12.8k | int sampleCnt) { |
194 | | // We cache blur masks. Use default surface props here so we can use the same cached mask |
195 | | // regardless of the final dst surface. |
196 | 12.8k | SkSurfaceProps defaultSurfaceProps; |
197 | | |
198 | | // Use GrResourceProvider::MakeApprox to implement our own approximate size matching, but demand |
199 | | // a "SkBackingFit::kExact" size match on the actual render target. We do this because the |
200 | | // filter will reach outside the src bounds, so we need to pre-clear these values to ensure a |
201 | | // "decal" sampling effect (i.e., ensure reads outside the src bounds return alpha=0). |
202 | | // |
203 | | // FIXME: Reads outside the left and top edges will actually clamp to the edge pixel. And in the |
204 | | // event that MakeApprox does not change the size, reads outside the right and/or bottom will do |
205 | | // the same. We should offset our filter within the render target and expand the size as needed |
206 | | // to guarantee at least 1px of padding on all sides. |
207 | 12.8k | auto approxSize = GrResourceProvider::MakeApprox(maskRect.size()); |
208 | 12.8k | auto sdc = skgpu::v1::SurfaceDrawContext::MakeWithFallback(rContext, |
209 | 12.8k | GrColorType::kAlpha_8, |
210 | 12.8k | nullptr, |
211 | 12.8k | SkBackingFit::kExact, |
212 | 12.8k | approxSize, |
213 | 12.8k | defaultSurfaceProps, |
214 | 12.8k | sampleCnt, |
215 | 12.8k | GrMipmapped::kNo, |
216 | 12.8k | GrProtected::kNo, |
217 | 12.8k | kMaskOrigin); |
218 | 12.8k | if (!sdc) { |
219 | 216 | return nullptr; |
220 | 216 | } |
221 | | |
222 | 12.6k | sdc->clear(SK_PMColor4fTRANSPARENT); |
223 | | |
224 | 12.6k | GrPaint maskPaint; |
225 | 12.6k | maskPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op); |
226 | | |
227 | | // setup new clip |
228 | 12.6k | GrFixedClip clip(sdc->dimensions(), SkIRect::MakeWH(maskRect.width(), maskRect.height())); |
229 | | |
230 | | // Draw the mask into maskTexture with the path's integerized top-left at the origin using |
231 | | // maskPaint. |
232 | 12.6k | SkMatrix viewMatrix = origViewMatrix; |
233 | 12.6k | viewMatrix.postTranslate(-SkIntToScalar(maskRect.fLeft), -SkIntToScalar(maskRect.fTop)); |
234 | 12.6k | sdc->drawShape(&clip, std::move(maskPaint), GrAA::kYes, viewMatrix, GrStyledShape(shape)); |
235 | 12.6k | return sdc; |
236 | 12.6k | } |
237 | | |
238 | | static bool get_unclipped_shape_dev_bounds(const GrStyledShape& shape, const SkMatrix& matrix, |
239 | 20.9k | SkIRect* devBounds) { |
240 | 20.9k | SkRect shapeBounds = shape.styledBounds(); |
241 | 20.9k | if (shapeBounds.isEmpty()) { |
242 | 2.66k | return false; |
243 | 2.66k | } |
244 | 18.3k | SkRect shapeDevBounds; |
245 | 18.3k | matrix.mapRect(&shapeDevBounds, shapeBounds); |
246 | | // Even though these are "unclipped" bounds we still clip to the int32_t range. |
247 | | // This is the largest int32_t that is representable exactly as a float. The next 63 larger ints |
248 | | // would round down to this value when cast to a float, but who really cares. |
249 | | // INT32_MIN is exactly representable. |
250 | 18.3k | static constexpr int32_t kMaxInt = 2147483520; |
251 | 18.3k | if (!shapeDevBounds.intersect(SkRect::MakeLTRB(INT32_MIN, INT32_MIN, kMaxInt, kMaxInt))) { |
252 | 412 | return false; |
253 | 412 | } |
254 | | // Make sure that the resulting SkIRect can have representable width and height |
255 | 17.9k | if (SkScalarRoundToInt(shapeDevBounds.width()) > kMaxInt || |
256 | 17.9k | SkScalarRoundToInt(shapeDevBounds.height()) > kMaxInt) { |
257 | 0 | return false; |
258 | 0 | } |
259 | 17.9k | shapeDevBounds.roundOut(devBounds); |
260 | 17.9k | return true; |
261 | 17.9k | } |
262 | | |
263 | | // Gets the shape bounds, the clip bounds, and the intersection (if any). Returns false if there |
264 | | // is no intersection. |
265 | | static bool get_shape_and_clip_bounds(skgpu::v1::SurfaceDrawContext* sdc, |
266 | | const GrClip* clip, |
267 | | const GrStyledShape& shape, |
268 | | const SkMatrix& matrix, |
269 | | SkIRect* unclippedDevShapeBounds, |
270 | 20.9k | SkIRect* devClipBounds) { |
271 | | // compute bounds as intersection of rt size, clip, and path |
272 | 20.9k | *devClipBounds = clip ? clip->getConservativeBounds() |
273 | 0 | : SkIRect::MakeWH(sdc->width(), sdc->height()); |
274 | | |
275 | 20.9k | if (!get_unclipped_shape_dev_bounds(shape, matrix, unclippedDevShapeBounds)) { |
276 | 3.08k | *unclippedDevShapeBounds = SkIRect::MakeEmpty(); |
277 | 3.08k | return false; |
278 | 3.08k | } |
279 | | |
280 | 17.9k | return true; |
281 | 17.9k | } |
282 | | |
283 | | // The key and clip-bounds are computed together because the caching decision can impact the |
284 | | // clip-bound - since we only cache un-clipped masks the clip can be removed entirely. |
285 | | // A 'false' return value indicates that the shape is known to be clipped away. |
286 | | static bool compute_key_and_clip_bounds(GrUniqueKey* maskKey, |
287 | | SkIRect* boundsForClip, |
288 | | const GrCaps* caps, |
289 | | const SkMatrix& viewMatrix, |
290 | | bool inverseFilled, |
291 | | const SkMaskFilterBase* maskFilter, |
292 | | const GrStyledShape& shape, |
293 | | const SkIRect& unclippedDevShapeBounds, |
294 | 17.9k | const SkIRect& devClipBounds) { |
295 | 17.9k | *boundsForClip = devClipBounds; |
296 | | |
297 | 17.9k | #ifndef SK_DISABLE_MASKFILTERED_MASK_CACHING |
298 | | // To prevent overloading the cache with entries during animations we limit the cache of masks |
299 | | // to cases where the matrix preserves axis alignment. |
300 | 17.9k | bool useCache = !inverseFilled && viewMatrix.preservesAxisAlignment() && |
301 | 6.98k | shape.hasUnstyledKey() && as_MFB(maskFilter)->asABlur(nullptr); |
302 | | |
303 | 17.9k | if (useCache) { |
304 | 160 | SkIRect clippedMaskRect, unClippedMaskRect; |
305 | 160 | maskFilter->canFilterMaskGPU(shape, unclippedDevShapeBounds, devClipBounds, |
306 | 160 | viewMatrix, &clippedMaskRect); |
307 | 160 | maskFilter->canFilterMaskGPU(shape, unclippedDevShapeBounds, unclippedDevShapeBounds, |
308 | 160 | viewMatrix, &unClippedMaskRect); |
309 | 160 | if (clippedMaskRect.isEmpty()) { |
310 | 0 | return false; |
311 | 0 | } |
312 | | |
313 | | // Use the cache only if >50% of the filtered mask is visible. |
314 | 160 | int unclippedWidth = unClippedMaskRect.width(); |
315 | 160 | int unclippedHeight = unClippedMaskRect.height(); |
316 | 160 | int64_t unclippedArea = sk_64_mul(unclippedWidth, unclippedHeight); |
317 | 160 | int64_t clippedArea = sk_64_mul(clippedMaskRect.width(), clippedMaskRect.height()); |
318 | 160 | int maxTextureSize = caps->maxTextureSize(); |
319 | 160 | if (unclippedArea > 2 * clippedArea || unclippedWidth > maxTextureSize || |
320 | 113 | unclippedHeight > maxTextureSize) { |
321 | 69 | useCache = false; |
322 | 91 | } else { |
323 | | // Make the clip not affect the mask |
324 | 91 | *boundsForClip = unclippedDevShapeBounds; |
325 | 91 | } |
326 | 160 | } |
327 | | |
328 | 17.9k | if (useCache) { |
329 | 91 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
330 | 91 | GrUniqueKey::Builder builder(maskKey, kDomain, 5 + 2 + shape.unstyledKeySize(), |
331 | 91 | "Mask Filtered Masks"); |
332 | | |
333 | | // We require the upper left 2x2 of the matrix to match exactly for a cache hit. |
334 | 91 | SkScalar sx = viewMatrix.get(SkMatrix::kMScaleX); |
335 | 91 | SkScalar sy = viewMatrix.get(SkMatrix::kMScaleY); |
336 | 91 | SkScalar kx = viewMatrix.get(SkMatrix::kMSkewX); |
337 | 91 | SkScalar ky = viewMatrix.get(SkMatrix::kMSkewY); |
338 | 91 | SkScalar tx = viewMatrix.get(SkMatrix::kMTransX); |
339 | 91 | SkScalar ty = viewMatrix.get(SkMatrix::kMTransY); |
340 | | // Allow 8 bits each in x and y of subpixel positioning. But, note that we're allowing |
341 | | // reuse for integer translations. |
342 | 91 | SkFixed fracX = SkScalarToFixed(SkScalarFraction(tx)) & 0x0000FF00; |
343 | 91 | SkFixed fracY = SkScalarToFixed(SkScalarFraction(ty)) & 0x0000FF00; |
344 | | |
345 | 91 | builder[0] = SkFloat2Bits(sx); |
346 | 91 | builder[1] = SkFloat2Bits(sy); |
347 | 91 | builder[2] = SkFloat2Bits(kx); |
348 | 91 | builder[3] = SkFloat2Bits(ky); |
349 | | // Distinguish between hairline and filled paths. For hairlines, we also need to include |
350 | | // the cap. (SW grows hairlines by 0.5 pixel with round and square caps). Note that |
351 | | // stroke-and-fill of hairlines is turned into pure fill by SkStrokeRec, so this covers |
352 | | // all cases we might see. |
353 | 91 | uint32_t styleBits = shape.style().isSimpleHairline() |
354 | 40 | ? ((shape.style().strokeRec().getCap() << 1) | 1) |
355 | 51 | : 0; |
356 | 91 | builder[4] = fracX | (fracY >> 8) | (styleBits << 16); |
357 | | |
358 | 91 | SkMaskFilterBase::BlurRec rec; |
359 | 91 | SkAssertResult(as_MFB(maskFilter)->asABlur(&rec)); |
360 | | |
361 | 91 | builder[5] = rec.fStyle; // TODO: we could put this with the other style bits |
362 | 91 | builder[6] = SkFloat2Bits(rec.fSigma); |
363 | 91 | shape.writeUnstyledKey(&builder[7]); |
364 | 91 | } |
365 | 17.9k | #endif |
366 | | |
367 | 17.9k | return true; |
368 | 17.9k | } |
369 | | |
370 | | static GrSurfaceProxyView hw_create_filtered_mask(GrDirectContext* dContext, |
371 | | skgpu::v1::SurfaceDrawContext* sdc, |
372 | | const SkMatrix& viewMatrix, |
373 | | const GrStyledShape& shape, |
374 | | const SkMaskFilterBase* filter, |
375 | | const SkIRect& unclippedDevShapeBounds, |
376 | | const SkIRect& clipBounds, |
377 | | SkIRect* maskRect, |
378 | 17.9k | GrUniqueKey* key) { |
379 | 17.9k | if (!filter->canFilterMaskGPU(shape, |
380 | 17.9k | unclippedDevShapeBounds, |
381 | 17.9k | clipBounds, |
382 | 17.9k | viewMatrix, |
383 | 2.63k | maskRect)) { |
384 | 2.63k | return {}; |
385 | 2.63k | } |
386 | | |
387 | 15.2k | if (clip_bounds_quick_reject(clipBounds, *maskRect)) { |
388 | | // clipped out |
389 | 2.43k | return {}; |
390 | 2.43k | } |
391 | | |
392 | 12.8k | auto threadSafeCache = dContext->priv().threadSafeCache(); |
393 | | |
394 | 12.8k | GrSurfaceProxyView lazyView; |
395 | 12.8k | sk_sp<GrThreadSafeCache::Trampoline> trampoline; |
396 | | |
397 | 12.8k | if (key->isValid()) { |
398 | | // In this case, we want GPU-filtered masks to have priority over SW-generated ones so |
399 | | // we pre-emptively add a lazy-view to the cache and fill it in later. |
400 | 74 | std::tie(lazyView, trampoline) = GrThreadSafeCache::CreateLazyView( |
401 | 74 | dContext, GrColorType::kAlpha_8, maskRect->size(), |
402 | 74 | kMaskOrigin, SkBackingFit::kApprox); |
403 | 74 | if (!lazyView) { |
404 | 0 | return {}; // fall back to a SW-created mask - 'create_mask_GPU' probably won't succeed |
405 | 0 | } |
406 | | |
407 | 74 | key->setCustomData(create_data(*maskRect, unclippedDevShapeBounds)); |
408 | 74 | auto [cachedView, data] = threadSafeCache->findOrAddWithData(*key, lazyView); |
409 | 74 | if (cachedView != lazyView) { |
410 | | // In this case, the gpu-thread lost out to a recording thread - use its result. |
411 | 5 | SkASSERT(data); |
412 | 5 | SkASSERT(cachedView.asTextureProxy()); |
413 | 5 | SkASSERT(cachedView.origin() == kMaskOrigin); |
414 | | |
415 | 5 | *maskRect = extract_draw_rect_from_data(data.get(), unclippedDevShapeBounds); |
416 | 5 | return cachedView; |
417 | 5 | } |
418 | 12.8k | } |
419 | | |
420 | 12.8k | std::unique_ptr<skgpu::v1::SurfaceDrawContext> maskSDC(create_mask_GPU(dContext, |
421 | 12.8k | *maskRect, |
422 | 12.8k | viewMatrix, |
423 | 12.8k | shape, |
424 | 12.8k | sdc->numSamples())); |
425 | 12.8k | if (!maskSDC) { |
426 | 216 | if (key->isValid()) { |
427 | | // It is very unlikely that 'create_mask_GPU' will fail after 'CreateLazyView' |
428 | | // succeeded but, if it does, remove the lazy-view from the cache and fallback to |
429 | | // a SW-created mask. Note that any recording threads that glommed onto the |
430 | | // lazy-view will have to, later, drop those draws. |
431 | 0 | threadSafeCache->remove(*key); |
432 | 0 | } |
433 | 216 | return {}; |
434 | 216 | } |
435 | | |
436 | 12.6k | auto filteredMaskView = filter->filterMaskGPU(dContext, |
437 | 12.6k | maskSDC->readSurfaceView(), |
438 | 12.6k | maskSDC->colorInfo().colorType(), |
439 | 12.6k | maskSDC->colorInfo().alphaType(), |
440 | 12.6k | viewMatrix, |
441 | 12.6k | *maskRect); |
442 | 12.6k | if (!filteredMaskView) { |
443 | 0 | if (key->isValid()) { |
444 | | // Remove the lazy-view from the cache and fallback to a SW-created mask. Note that |
445 | | // any recording threads that glommed onto the lazy-view will have to, later, drop |
446 | | // those draws. |
447 | 0 | threadSafeCache->remove(*key); |
448 | 0 | } |
449 | 0 | return {}; |
450 | 0 | } |
451 | | |
452 | 12.6k | if (key->isValid()) { |
453 | 69 | SkASSERT(filteredMaskView.dimensions() == lazyView.dimensions()); |
454 | 69 | SkASSERT(filteredMaskView.swizzle() == lazyView.swizzle()); |
455 | 69 | SkASSERT(filteredMaskView.origin() == lazyView.origin()); |
456 | | |
457 | 69 | trampoline->fProxy = filteredMaskView.asTextureProxyRef(); |
458 | 69 | return lazyView; |
459 | 69 | } |
460 | | |
461 | 12.5k | return filteredMaskView; |
462 | 12.5k | } |
463 | | |
464 | | static void draw_shape_with_mask_filter(GrRecordingContext* rContext, |
465 | | skgpu::v1::SurfaceDrawContext* sdc, |
466 | | const GrClip* clip, |
467 | | GrPaint&& paint, |
468 | | const SkMatrix& viewMatrix, |
469 | | const SkMaskFilterBase* maskFilter, |
470 | 23.9k | const GrStyledShape& origShape) { |
471 | 23.9k | SkASSERT(maskFilter); |
472 | | |
473 | 23.9k | const GrStyledShape* shape = &origShape; |
474 | 23.9k | SkTLazy<GrStyledShape> tmpShape; |
475 | | |
476 | 23.9k | if (origShape.style().applies()) { |
477 | 13.1k | SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix); |
478 | 13.1k | if (styleScale == 0) { |
479 | 0 | return; |
480 | 0 | } |
481 | | |
482 | 13.1k | tmpShape.init(origShape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale)); |
483 | 13.1k | if (tmpShape->isEmpty()) { |
484 | 1.08k | return; |
485 | 1.08k | } |
486 | | |
487 | 12.0k | shape = tmpShape.get(); |
488 | 12.0k | } |
489 | | |
490 | 22.8k | if (maskFilter->directFilterMaskGPU(rContext, sdc, std::move(paint), clip, |
491 | 1.84k | viewMatrix, *shape)) { |
492 | | // the mask filter was able to draw itself directly, so there's nothing |
493 | | // left to do. |
494 | 1.84k | return; |
495 | 1.84k | } |
496 | 20.9k | assert_alive(paint); |
497 | | |
498 | | // If the path is hairline, ignore inverse fill. |
499 | 20.9k | bool inverseFilled = shape->inverseFilled() && |
500 | 18 | !GrIsStrokeHairlineOrEquivalent(shape->style(), viewMatrix, nullptr); |
501 | | |
502 | 20.9k | SkIRect unclippedDevShapeBounds, devClipBounds; |
503 | 20.9k | if (!get_shape_and_clip_bounds(sdc, clip, *shape, viewMatrix, |
504 | 3.08k | &unclippedDevShapeBounds, &devClipBounds)) { |
505 | | // TODO: just cons up an opaque mask here |
506 | 3.08k | if (!inverseFilled) { |
507 | 3.08k | return; |
508 | 3.08k | } |
509 | 17.9k | } |
510 | | |
511 | 17.9k | GrUniqueKey maskKey; |
512 | 17.9k | SkIRect boundsForClip; |
513 | 17.9k | if (!compute_key_and_clip_bounds(&maskKey, &boundsForClip, |
514 | 17.9k | sdc->caps(), |
515 | 17.9k | viewMatrix, inverseFilled, |
516 | 17.9k | maskFilter, *shape, |
517 | 17.9k | unclippedDevShapeBounds, |
518 | 0 | devClipBounds)) { |
519 | 0 | return; // 'shape' was entirely clipped out |
520 | 0 | } |
521 | | |
522 | 17.9k | GrSurfaceProxyView filteredMaskView; |
523 | 17.9k | SkIRect maskRect; |
524 | | |
525 | 17.9k | if (auto dContext = rContext->asDirectContext()) { |
526 | 17.9k | filteredMaskView = hw_create_filtered_mask(dContext, sdc, |
527 | 17.9k | viewMatrix, *shape, maskFilter, |
528 | 17.9k | unclippedDevShapeBounds, boundsForClip, |
529 | 17.9k | &maskRect, &maskKey); |
530 | 17.9k | if (filteredMaskView) { |
531 | 12.6k | if (draw_mask(sdc, clip, viewMatrix, maskRect, std::move(paint), |
532 | 12.5k | std::move(filteredMaskView))) { |
533 | | // This path is completely drawn |
534 | 12.5k | return; |
535 | 12.5k | } |
536 | 97 | assert_alive(paint); |
537 | 97 | } |
538 | 17.9k | } |
539 | | |
540 | | // Either HW mask rendering failed or we're in a DDL recording thread |
541 | 5.38k | filteredMaskView = sw_create_filtered_mask(rContext, |
542 | 5.38k | viewMatrix, *shape, maskFilter, |
543 | 5.38k | unclippedDevShapeBounds, boundsForClip, |
544 | 5.38k | &maskRect, &maskKey); |
545 | 5.38k | if (filteredMaskView) { |
546 | 631 | if (draw_mask(sdc, clip, viewMatrix, maskRect, std::move(paint), |
547 | 631 | std::move(filteredMaskView))) { |
548 | 631 | return; |
549 | 631 | } |
550 | 0 | assert_alive(paint); |
551 | 0 | } |
552 | 5.38k | } |
553 | | |
554 | | void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext* rContext, |
555 | | skgpu::v1::SurfaceDrawContext* sdc, |
556 | | const GrClip* clip, |
557 | | const GrStyledShape& shape, |
558 | | GrPaint&& paint, |
559 | | const SkMatrix& viewMatrix, |
560 | 0 | const SkMaskFilter* mf) { |
561 | 0 | draw_shape_with_mask_filter(rContext, sdc, clip, std::move(paint), |
562 | 0 | viewMatrix, as_MFB(mf), shape); |
563 | 0 | } |
564 | | |
565 | | void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext* rContext, |
566 | | skgpu::v1::SurfaceDrawContext* sdc, |
567 | | const GrClip* clip, |
568 | | const SkPaint& paint, |
569 | | const SkMatrixProvider& matrixProvider, |
570 | 54.5k | const GrStyledShape& shape) { |
571 | 54.5k | if (rContext->abandoned()) { |
572 | 0 | return; |
573 | 0 | } |
574 | | |
575 | 54.5k | GrPaint grPaint; |
576 | 54.5k | if (!SkPaintToGrPaint(rContext, sdc->colorInfo(), paint, matrixProvider, &grPaint)) { |
577 | 331 | return; |
578 | 331 | } |
579 | | |
580 | 54.1k | const SkMatrix& viewMatrix(matrixProvider.localToDevice()); |
581 | 54.1k | SkMaskFilterBase* mf = as_MFB(paint.getMaskFilter()); |
582 | 54.1k | if (mf && !mf->hasFragmentProcessor()) { |
583 | | // The MaskFilter wasn't already handled in SkPaintToGrPaint |
584 | 23.9k | draw_shape_with_mask_filter(rContext, sdc, clip, std::move(grPaint), viewMatrix, mf, shape); |
585 | 30.2k | } else { |
586 | 30.2k | sdc->drawShape(clip, std::move(grPaint), sdc->chooseAA(paint), viewMatrix, |
587 | 30.2k | GrStyledShape(shape)); |
588 | 30.2k | } |
589 | 54.1k | } |
590 | | |
591 | | #else // SK_GPU_V1 |
592 | | |
593 | | void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext*, |
594 | | skgpu::v1::SurfaceDrawContext*, |
595 | | const GrClip*, |
596 | | const GrStyledShape&, |
597 | | GrPaint&&, |
598 | | const SkMatrix& viewMatrix, |
599 | | const SkMaskFilter*) { |
600 | | } |
601 | | |
602 | | void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext*, |
603 | | skgpu::v1::SurfaceDrawContext*, |
604 | | const GrClip*, |
605 | | const SkPaint&, |
606 | | const SkMatrixProvider&, |
607 | | const GrStyledShape&) { |
608 | | } |
609 | | |
610 | | #endif // SK_GPU_V1 |