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 | #include "include/gpu/GrRecordingContext.h" |
11 | #include "src/gpu/GrBitmapTextureMaker.h" |
12 | #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" |
18 | #include "src/gpu/GrSoftwarePathRenderer.h" |
19 | #include "src/gpu/GrStyle.h" |
20 | #include "src/gpu/GrTextureProxy.h" |
21 | #include "src/gpu/effects/GrTextureEffect.h" |
22 | #include "src/gpu/geometry/GrStyledShape.h" |
23 | |
24 | #include "include/core/SkPaint.h" |
25 | #include "src/core/SkDraw.h" |
26 | #include "src/core/SkMaskFilterBase.h" |
27 | #include "src/core/SkMatrixProvider.h" |
28 | #include "src/core/SkTLazy.h" |
29 | #include "src/gpu/SkGr.h" |
30 | |
31 | static bool clip_bounds_quick_reject(const SkIRect& clipBounds, const SkIRect& rect) { |
32 | return clipBounds.isEmpty() || rect.isEmpty() || !SkIRect::Intersects(clipBounds, rect); |
33 | } |
34 | |
35 | static constexpr auto kMaskOrigin = kTopLeft_GrSurfaceOrigin; |
36 | |
37 | static GrSurfaceProxyView find_filtered_mask(GrProxyProvider* provider, const GrUniqueKey& key) { |
38 | return provider->findCachedProxyWithColorTypeFallback(key, kMaskOrigin, GrColorType::kAlpha_8, |
39 | 1); |
40 | } |
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(GrRenderTargetContext* renderTargetContext, |
46 | const GrClip* clip, |
47 | const SkMatrix& viewMatrix, |
48 | const SkIRect& maskRect, |
49 | GrPaint&& paint, |
50 | GrSurfaceProxyView mask) { |
51 | SkMatrix inverse; |
52 | if (!viewMatrix.invert(&inverse)) { |
53 | return false; |
54 | } |
55 | |
56 | SkMatrix matrix = SkMatrix::Translate(-SkIntToScalar(maskRect.fLeft), |
57 | -SkIntToScalar(maskRect.fTop)); |
58 | matrix.preConcat(viewMatrix); |
59 | paint.setCoverageFragmentProcessor( |
60 | GrTextureEffect::Make(std::move(mask), kUnknown_SkAlphaType, matrix)); |
61 | |
62 | renderTargetContext->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), |
63 | SkRect::Make(maskRect), inverse); |
64 | return true; |
65 | } |
66 | |
67 | static void mask_release_proc(void* addr, void* /*context*/) { |
68 | SkMask::FreeImage(addr); |
69 | } |
70 | |
71 | static bool sw_draw_with_mask_filter(GrRecordingContext* context, |
72 | GrRenderTargetContext* renderTargetContext, |
73 | const GrClip* clipData, |
74 | const SkMatrix& viewMatrix, |
75 | const GrStyledShape& shape, |
76 | const SkMaskFilter* filter, |
77 | const SkIRect& clipBounds, |
78 | GrPaint&& paint, |
79 | const GrUniqueKey& key) { |
80 | SkASSERT(filter); |
81 | SkASSERT(!shape.style().applies()); |
82 | |
83 | auto proxyProvider = context->priv().proxyProvider(); |
84 | |
85 | GrSurfaceProxyView filteredMaskView; |
86 | |
87 | SkStrokeRec::InitStyle fillOrHairline = shape.style().isSimpleHairline() |
88 | ? SkStrokeRec::kHairline_InitStyle |
89 | : SkStrokeRec::kFill_InitStyle; |
90 | |
91 | if (key.isValid()) { |
92 | filteredMaskView = find_filtered_mask(proxyProvider, key); |
93 | } |
94 | |
95 | SkIRect drawRect; |
96 | if (filteredMaskView) { |
97 | SkRect devBounds = shape.bounds(); |
98 | viewMatrix.mapRect(&devBounds); |
99 | |
100 | // Here we need to recompute the destination bounds in order to draw the mask correctly |
101 | SkMask srcM, dstM; |
102 | if (!SkDraw::ComputeMaskBounds(devBounds, &clipBounds, filter, &viewMatrix, |
103 | &srcM.fBounds)) { |
104 | return false; |
105 | } |
106 | |
107 | srcM.fFormat = SkMask::kA8_Format; |
108 | |
109 | if (!as_MFB(filter)->filterMask(&dstM, srcM, viewMatrix, nullptr)) { |
110 | return false; |
111 | } |
112 | |
113 | // Unfortunately, we cannot double check that the computed bounds (i.e., dstM.fBounds) |
114 | // match the stored bounds of the mask bc the proxy may have been recreated and, |
115 | // when it is recreated, it just gets the bounds of the underlying GrTexture (which |
116 | // might be a loose fit). |
117 | drawRect = dstM.fBounds; |
118 | } else { |
119 | // TODO: it seems like we could create an SkDraw here and set its fMatrix field rather |
120 | // than explicitly transforming the path to device space. |
121 | SkPath devPath; |
122 | |
123 | shape.asPath(&devPath); |
124 | |
125 | devPath.transform(viewMatrix); |
126 | |
127 | SkMask srcM, dstM; |
128 | if (!SkDraw::DrawToMask(devPath, &clipBounds, filter, &viewMatrix, &srcM, |
129 | SkMask::kComputeBoundsAndRenderImage_CreateMode, fillOrHairline)) { |
130 | return false; |
131 | } |
132 | SkAutoMaskFreeImage autoSrc(srcM.fImage); |
133 | |
134 | SkASSERT(SkMask::kA8_Format == srcM.fFormat); |
135 | |
136 | if (!as_MFB(filter)->filterMask(&dstM, srcM, viewMatrix, nullptr)) { |
137 | return false; |
138 | } |
139 | // this will free-up dstM when we're done (allocated in filterMask()) |
140 | SkAutoMaskFreeImage autoDst(dstM.fImage); |
141 | |
142 | if (clip_bounds_quick_reject(clipBounds, dstM.fBounds)) { |
143 | return false; |
144 | } |
145 | |
146 | // we now have a device-aligned 8bit mask in dstM, ready to be drawn using |
147 | // the current clip (and identity matrix) and GrPaint settings |
148 | SkBitmap bm; |
149 | if (!bm.installPixels(SkImageInfo::MakeA8(dstM.fBounds.width(), dstM.fBounds.height()), |
150 | autoDst.release(), dstM.fRowBytes, mask_release_proc, nullptr)) { |
151 | return false; |
152 | } |
153 | bm.setImmutable(); |
154 | |
155 | GrBitmapTextureMaker maker(context, bm, SkBackingFit::kApprox); |
156 | filteredMaskView = maker.view(GrMipmapped::kNo); |
157 | if (!filteredMaskView.proxy()) { |
158 | return false; |
159 | } |
160 | |
161 | SkASSERT(kMaskOrigin == filteredMaskView.origin()); |
162 | |
163 | drawRect = dstM.fBounds; |
164 | |
165 | if (key.isValid()) { |
166 | proxyProvider->assignUniqueKeyToProxy(key, filteredMaskView.asTextureProxy()); |
167 | } |
168 | } |
169 | |
170 | return draw_mask(renderTargetContext, clipData, viewMatrix, drawRect, std::move(paint), |
171 | std::move(filteredMaskView)); |
172 | } |
173 | |
174 | // Create a mask of 'shape' and return the resulting renderTargetContext |
175 | static std::unique_ptr<GrRenderTargetContext> create_mask_GPU(GrRecordingContext* context, |
176 | const SkIRect& maskRect, |
177 | const SkMatrix& origViewMatrix, |
178 | const GrStyledShape& shape, |
179 | int sampleCnt) { |
180 | // Use GrResourceProvider::MakeApprox to implement our own approximate size matching, but demand |
181 | // a "SkBackingFit::kExact" size match on the actual render target. We do this because the |
182 | // filter will reach outside the src bounds, so we need to pre-clear these values to ensure a |
183 | // "decal" sampling effect (i.e., ensure reads outside the src bounds return alpha=0). |
184 | // |
185 | // FIXME: Reads outside the left and top edges will actually clamp to the edge pixel. And in the |
186 | // event that MakeApprox does not change the size, reads outside the right and/or bottom will do |
187 | // the same. We should offset our filter within the render target and expand the size as needed |
188 | // to guarantee at least 1px of padding on all sides. |
189 | auto approxSize = GrResourceProvider::MakeApprox(maskRect.size()); |
190 | auto rtContext = GrRenderTargetContext::MakeWithFallback( |
191 | context, GrColorType::kAlpha_8, nullptr, SkBackingFit::kExact, approxSize, sampleCnt, |
192 | GrMipmapped::kNo, GrProtected::kNo, kMaskOrigin); |
193 | if (!rtContext) { |
194 | return nullptr; |
195 | } |
196 | |
197 | rtContext->clear(SK_PMColor4fTRANSPARENT); |
198 | |
199 | GrPaint maskPaint; |
200 | maskPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op); |
201 | |
202 | // setup new clip |
203 | GrFixedClip clip(rtContext->dimensions(), SkIRect::MakeWH(maskRect.width(), maskRect.height())); |
204 | |
205 | // Draw the mask into maskTexture with the path's integerized top-left at the origin using |
206 | // maskPaint. |
207 | SkMatrix viewMatrix = origViewMatrix; |
208 | viewMatrix.postTranslate(-SkIntToScalar(maskRect.fLeft), -SkIntToScalar(maskRect.fTop)); |
209 | rtContext->drawShape(&clip, std::move(maskPaint), GrAA::kYes, viewMatrix, shape); |
210 | return rtContext; |
211 | } |
212 | |
213 | static bool get_unclipped_shape_dev_bounds(const GrStyledShape& shape, const SkMatrix& matrix, |
214 | SkIRect* devBounds) { |
215 | SkRect shapeBounds = shape.styledBounds(); |
216 | if (shapeBounds.isEmpty()) { |
217 | return false; |
218 | } |
219 | SkRect shapeDevBounds; |
220 | matrix.mapRect(&shapeDevBounds, shapeBounds); |
221 | // Even though these are "unclipped" bounds we still clip to the int32_t range. |
222 | // This is the largest int32_t that is representable exactly as a float. The next 63 larger ints |
223 | // would round down to this value when cast to a float, but who really cares. |
224 | // INT32_MIN is exactly representable. |
225 | static constexpr int32_t kMaxInt = 2147483520; |
226 | if (!shapeDevBounds.intersect(SkRect::MakeLTRB(INT32_MIN, INT32_MIN, kMaxInt, kMaxInt))) { |
227 | return false; |
228 | } |
229 | // Make sure that the resulting SkIRect can have representable width and height |
230 | if (SkScalarRoundToInt(shapeDevBounds.width()) > kMaxInt || |
231 | SkScalarRoundToInt(shapeDevBounds.height()) > kMaxInt) { |
232 | return false; |
233 | } |
234 | shapeDevBounds.roundOut(devBounds); |
235 | return true; |
236 | } |
237 | |
238 | // Gets the shape bounds, the clip bounds, and the intersection (if any). Returns false if there |
239 | // is no intersection. |
240 | static bool get_shape_and_clip_bounds(GrRenderTargetContext* renderTargetContext, |
241 | const GrClip* clip, |
242 | const GrStyledShape& shape, |
243 | const SkMatrix& matrix, |
244 | SkIRect* unclippedDevShapeBounds, |
245 | SkIRect* devClipBounds) { |
246 | // compute bounds as intersection of rt size, clip, and path |
247 | *devClipBounds = clip ? clip->getConservativeBounds() |
248 | : SkIRect::MakeWH(renderTargetContext->width(), |
249 | renderTargetContext->height()); |
250 | |
251 | if (!get_unclipped_shape_dev_bounds(shape, matrix, unclippedDevShapeBounds)) { |
252 | *unclippedDevShapeBounds = SkIRect::MakeEmpty(); |
253 | return false; |
254 | } |
255 | |
256 | return true; |
257 | } |
258 | |
259 | static void draw_shape_with_mask_filter(GrRecordingContext* context, |
260 | GrRenderTargetContext* renderTargetContext, |
261 | const GrClip* clip, |
262 | GrPaint&& paint, |
263 | const SkMatrix& viewMatrix, |
264 | const SkMaskFilterBase* maskFilter, |
265 | const GrStyledShape& origShape) { |
266 | SkASSERT(maskFilter); |
267 | |
268 | const GrStyledShape* shape = &origShape; |
269 | SkTLazy<GrStyledShape> tmpShape; |
270 | |
271 | if (origShape.style().applies()) { |
272 | SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix); |
273 | if (0 == styleScale) { |
274 | return; |
275 | } |
276 | |
277 | tmpShape.init(origShape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale)); |
278 | if (tmpShape.get()->isEmpty()) { |
279 | return; |
280 | } |
281 | |
282 | shape = tmpShape.get(); |
283 | } |
284 | |
285 | if (maskFilter->directFilterMaskGPU(context, renderTargetContext, std::move(paint), clip, |
286 | viewMatrix, *shape)) { |
287 | // the mask filter was able to draw itself directly, so there's nothing |
288 | // left to do. |
289 | return; |
290 | } |
291 | assert_alive(paint); |
292 | |
293 | // If the path is hairline, ignore inverse fill. |
294 | bool inverseFilled = shape->inverseFilled() && |
295 | !GrPathRenderer::IsStrokeHairlineOrEquivalent(shape->style(), |
296 | viewMatrix, nullptr); |
297 | |
298 | SkIRect unclippedDevShapeBounds, devClipBounds; |
299 | if (!get_shape_and_clip_bounds(renderTargetContext, clip, *shape, viewMatrix, |
300 | &unclippedDevShapeBounds, &devClipBounds)) { |
301 | // TODO: just cons up an opaque mask here |
302 | if (!inverseFilled) { |
303 | return; |
304 | } |
305 | } |
306 | |
307 | // To prevent overloading the cache with entries during animations we limit the cache of masks |
308 | // to cases where the matrix preserves axis alignment. |
309 | #ifdef SK_DISABLE_MASKFILTERED_MASK_CACHING |
310 | bool useCache = false; |
311 | #else |
312 | bool useCache = !inverseFilled && viewMatrix.preservesAxisAlignment() && |
313 | shape->hasUnstyledKey() && as_MFB(maskFilter)->asABlur(nullptr); |
314 | #endif |
315 | |
316 | const SkIRect* boundsForClip = &devClipBounds; |
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; |
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 = renderTargetContext->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 | GrUniqueKey maskKey; |
343 | if (useCache) { |
344 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
345 | GrUniqueKey::Builder builder(&maskKey, kDomain, 5 + 2 + shape->unstyledKeySize(), |
346 | "Mask Filtered Masks" ); |
347 | |
348 | // We require the upper left 2x2 of the matrix to match exactly for a cache hit. |
349 | SkScalar sx = viewMatrix.get(SkMatrix::kMScaleX); |
350 | SkScalar sy = viewMatrix.get(SkMatrix::kMScaleY); |
351 | SkScalar kx = viewMatrix.get(SkMatrix::kMSkewX); |
352 | SkScalar ky = viewMatrix.get(SkMatrix::kMSkewY); |
353 | SkScalar tx = viewMatrix.get(SkMatrix::kMTransX); |
354 | SkScalar ty = viewMatrix.get(SkMatrix::kMTransY); |
355 | // Allow 8 bits each in x and y of subpixel positioning. |
356 | 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 | |
380 | SkIRect maskRect; |
381 | if (maskFilter->canFilterMaskGPU(*shape, |
382 | unclippedDevShapeBounds, |
383 | *boundsForClip, |
384 | viewMatrix, |
385 | &maskRect)) { |
386 | if (clip_bounds_quick_reject(*boundsForClip, maskRect)) { |
387 | // clipped out |
388 | return; |
389 | } |
390 | |
391 | GrSurfaceProxyView filteredMaskView; |
392 | |
393 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
394 | |
395 | if (maskKey.isValid()) { |
396 | filteredMaskView = find_filtered_mask(proxyProvider, maskKey); |
397 | } |
398 | |
399 | if (!filteredMaskView) { |
400 | std::unique_ptr<GrRenderTargetContext> maskRTC(create_mask_GPU( |
401 | context, |
402 | maskRect, |
403 | viewMatrix, |
404 | *shape, |
405 | renderTargetContext->numSamples())); |
406 | if (maskRTC) { |
407 | filteredMaskView = maskFilter->filterMaskGPU(context, |
408 | maskRTC->readSurfaceView(), |
409 | maskRTC->colorInfo().colorType(), |
410 | maskRTC->colorInfo().alphaType(), |
411 | viewMatrix, |
412 | maskRect); |
413 | if (filteredMaskView.proxy() && maskKey.isValid()) { |
414 | SkASSERT(filteredMaskView.asTextureProxy()); |
415 | proxyProvider->assignUniqueKeyToProxy(maskKey, |
416 | filteredMaskView.asTextureProxy()); |
417 | } |
418 | } |
419 | } |
420 | |
421 | if (filteredMaskView) { |
422 | if (draw_mask(renderTargetContext, clip, viewMatrix, maskRect, std::move(paint), |
423 | std::move(filteredMaskView))) { |
424 | // This path is completely drawn |
425 | return; |
426 | } |
427 | assert_alive(paint); |
428 | } |
429 | } |
430 | |
431 | sw_draw_with_mask_filter(context, renderTargetContext, clip, viewMatrix, *shape, |
432 | maskFilter, *boundsForClip, std::move(paint), maskKey); |
433 | } |
434 | |
435 | void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext* context, |
436 | GrRenderTargetContext* renderTargetContext, |
437 | const GrClip* clip, |
438 | const GrStyledShape& shape, |
439 | GrPaint&& paint, |
440 | const SkMatrix& viewMatrix, |
441 | const SkMaskFilter* mf) { |
442 | draw_shape_with_mask_filter(context, renderTargetContext, clip, std::move(paint), |
443 | viewMatrix, as_MFB(mf), shape); |
444 | } |
445 | |
446 | void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext* context, |
447 | GrRenderTargetContext* renderTargetContext, |
448 | const GrClip* clip, |
449 | const SkPaint& paint, |
450 | const SkMatrixProvider& matrixProvider, |
451 | const GrStyledShape& shape) { |
452 | if (context->abandoned()) { |
453 | return; |
454 | } |
455 | |
456 | GrPaint grPaint; |
457 | if (!SkPaintToGrPaint(context, renderTargetContext->colorInfo(), paint, matrixProvider, |
458 | &grPaint)) { |
459 | return; |
460 | } |
461 | |
462 | const SkMatrix& viewMatrix(matrixProvider.localToDevice()); |
463 | SkMaskFilterBase* mf = as_MFB(paint.getMaskFilter()); |
464 | if (mf && !mf->hasFragmentProcessor()) { |
465 | // The MaskFilter wasn't already handled in SkPaintToGrPaint |
466 | draw_shape_with_mask_filter(context, renderTargetContext, clip, std::move(grPaint), |
467 | viewMatrix, mf, shape); |
468 | } else { |
469 | GrAA aa = GrAA(paint.isAntiAlias()); |
470 | renderTargetContext->drawShape(clip, std::move(grPaint), aa, viewMatrix, shape); |
471 | } |
472 | } |
473 | |