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/private/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/GrShape.h" |
23 | |
24 | #include "include/core/SkPaint.h" |
25 | #include "src/core/SkDraw.h" |
26 | #include "src/core/SkMaskFilterBase.h" |
27 | #include "src/core/SkTLazy.h" |
28 | #include "src/gpu/SkGr.h" |
29 | |
30 | static bool clip_bounds_quick_reject(const SkIRect& clipBounds, const SkIRect& rect) { |
31 | return clipBounds.isEmpty() || rect.isEmpty() || !SkIRect::Intersects(clipBounds, rect); |
32 | } |
33 | |
34 | static constexpr auto kMaskOrigin = kTopLeft_GrSurfaceOrigin; |
35 | |
36 | static GrSurfaceProxyView find_filtered_mask(GrProxyProvider* provider, const GrUniqueKey& key) { |
37 | return provider->findCachedProxyWithColorTypeFallback(key, kMaskOrigin, GrColorType::kAlpha_8, |
38 | 1); |
39 | } |
40 | |
41 | // Draw a mask using the supplied paint. Since the coverage/geometry |
42 | // is already burnt into the mask this boils down to a rect draw. |
43 | // Return true if the mask was successfully drawn. |
44 | static bool draw_mask(GrRenderTargetContext* renderTargetContext, |
45 | const GrClip& clip, |
46 | const SkMatrix& viewMatrix, |
47 | const SkIRect& maskRect, |
48 | GrPaint&& paint, |
49 | GrSurfaceProxyView mask) { |
50 | SkMatrix inverse; |
51 | if (!viewMatrix.invert(&inverse)) { |
52 | return false; |
53 | } |
54 | |
55 | SkMatrix matrix = SkMatrix::MakeTrans(-SkIntToScalar(maskRect.fLeft), |
56 | -SkIntToScalar(maskRect.fTop)); |
57 | matrix.preConcat(viewMatrix); |
58 | paint.addCoverageFragmentProcessor( |
59 | GrTextureEffect::Make(std::move(mask), kUnknown_SkAlphaType, matrix)); |
60 | |
61 | renderTargetContext->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), |
62 | SkRect::Make(maskRect), inverse); |
63 | return true; |
64 | } |
65 | |
66 | static void mask_release_proc(void* addr, void* /*context*/) { |
67 | SkMask::FreeImage(addr); |
68 | } |
69 | |
70 | static bool sw_draw_with_mask_filter(GrRecordingContext* context, |
71 | GrRenderTargetContext* renderTargetContext, |
72 | const GrClip& clipData, |
73 | const SkMatrix& viewMatrix, |
74 | const GrShape& shape, |
75 | const SkMaskFilter* filter, |
76 | const SkIRect& clipBounds, |
77 | GrPaint&& paint, |
78 | const GrUniqueKey& key) { |
79 | SkASSERT(filter); |
80 | SkASSERT(!shape.style().applies()); |
81 | |
82 | auto proxyProvider = context->priv().proxyProvider(); |
83 | |
84 | GrSurfaceProxyView filteredMaskView; |
85 | |
86 | SkStrokeRec::InitStyle fillOrHairline = shape.style().isSimpleHairline() |
87 | ? SkStrokeRec::kHairline_InitStyle |
88 | : SkStrokeRec::kFill_InitStyle; |
89 | |
90 | if (key.isValid()) { |
91 | filteredMaskView = find_filtered_mask(proxyProvider, key); |
92 | } |
93 | |
94 | SkIRect drawRect; |
95 | if (filteredMaskView) { |
96 | SkRect devBounds = shape.bounds(); |
97 | viewMatrix.mapRect(&devBounds); |
98 | |
99 | // Here we need to recompute the destination bounds in order to draw the mask correctly |
100 | SkMask srcM, dstM; |
101 | if (!SkDraw::ComputeMaskBounds(devBounds, &clipBounds, filter, &viewMatrix, |
102 | &srcM.fBounds)) { |
103 | return false; |
104 | } |
105 | |
106 | srcM.fFormat = SkMask::kA8_Format; |
107 | |
108 | if (!as_MFB(filter)->filterMask(&dstM, srcM, viewMatrix, nullptr)) { |
109 | return false; |
110 | } |
111 | |
112 | // Unfortunately, we cannot double check that the computed bounds (i.e., dstM.fBounds) |
113 | // match the stored bounds of the mask bc the proxy may have been recreated and, |
114 | // when it is recreated, it just gets the bounds of the underlying GrTexture (which |
115 | // might be a loose fit). |
116 | drawRect = dstM.fBounds; |
117 | } else { |
118 | // TODO: it seems like we could create an SkDraw here and set its fMatrix field rather |
119 | // than explicitly transforming the path to device space. |
120 | SkPath devPath; |
121 | |
122 | shape.asPath(&devPath); |
123 | |
124 | devPath.transform(viewMatrix); |
125 | |
126 | SkMask srcM, dstM; |
127 | if (!SkDraw::DrawToMask(devPath, &clipBounds, filter, &viewMatrix, &srcM, |
128 | SkMask::kComputeBoundsAndRenderImage_CreateMode, fillOrHairline)) { |
129 | return false; |
130 | } |
131 | SkAutoMaskFreeImage autoSrc(srcM.fImage); |
132 | |
133 | SkASSERT(SkMask::kA8_Format == srcM.fFormat); |
134 | |
135 | if (!as_MFB(filter)->filterMask(&dstM, srcM, viewMatrix, nullptr)) { |
136 | return false; |
137 | } |
138 | // this will free-up dstM when we're done (allocated in filterMask()) |
139 | SkAutoMaskFreeImage autoDst(dstM.fImage); |
140 | |
141 | if (clip_bounds_quick_reject(clipBounds, dstM.fBounds)) { |
142 | return false; |
143 | } |
144 | |
145 | // we now have a device-aligned 8bit mask in dstM, ready to be drawn using |
146 | // the current clip (and identity matrix) and GrPaint settings |
147 | SkBitmap bm; |
148 | if (!bm.installPixels(SkImageInfo::MakeA8(dstM.fBounds.width(), dstM.fBounds.height()), |
149 | autoDst.release(), dstM.fRowBytes, mask_release_proc, nullptr)) { |
150 | return false; |
151 | } |
152 | bm.setImmutable(); |
153 | |
154 | GrBitmapTextureMaker maker(context, bm, SkBackingFit::kApprox); |
155 | filteredMaskView = maker.view(GrMipMapped::kNo); |
156 | if (!filteredMaskView.proxy()) { |
157 | return false; |
158 | } |
159 | |
160 | SkASSERT(kMaskOrigin == filteredMaskView.origin()); |
161 | |
162 | drawRect = dstM.fBounds; |
163 | |
164 | if (key.isValid()) { |
165 | proxyProvider->assignUniqueKeyToProxy(key, filteredMaskView.asTextureProxy()); |
166 | } |
167 | } |
168 | |
169 | return draw_mask(renderTargetContext, clipData, viewMatrix, drawRect, std::move(paint), |
170 | std::move(filteredMaskView)); |
171 | } |
172 | |
173 | // Create a mask of 'shape' and return the resulting renderTargetContext |
174 | static std::unique_ptr<GrRenderTargetContext> create_mask_GPU(GrRecordingContext* context, |
175 | const SkIRect& maskRect, |
176 | const SkMatrix& origViewMatrix, |
177 | const GrShape& shape, |
178 | int sampleCnt) { |
179 | // Use GrResourceProvider::MakeApprox to implement our own approximate size matching, but demand |
180 | // a "SkBackingFit::kExact" size match on the actual render target. We do this because the |
181 | // filter will reach outside the src bounds, so we need to pre-clear these values to ensure a |
182 | // "decal" sampling effect (i.e., ensure reads outside the src bounds return alpha=0). |
183 | // |
184 | // FIXME: Reads outside the left and top edges will actually clamp to the edge pixel. And in the |
185 | // event that MakeApprox does not change the size, reads outside the right and/or bottom will do |
186 | // the same. We should offset our filter within the render target and expand the size as needed |
187 | // to guarantee at least 1px of padding on all sides. |
188 | auto approxSize = GrResourceProvider::MakeApprox(maskRect.size()); |
189 | auto rtContext = GrRenderTargetContext::MakeWithFallback( |
190 | context, GrColorType::kAlpha_8, nullptr, SkBackingFit::kExact, approxSize, sampleCnt, |
191 | GrMipMapped::kNo, GrProtected::kNo, kMaskOrigin); |
192 | if (!rtContext) { |
193 | return nullptr; |
194 | } |
195 | |
196 | rtContext->clear(SK_PMColor4fTRANSPARENT); |
197 | |
198 | GrPaint maskPaint; |
199 | maskPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op); |
200 | |
201 | // setup new clip |
202 | const SkIRect clipRect = SkIRect::MakeWH(maskRect.width(), maskRect.height()); |
203 | GrFixedClip clip(clipRect); |
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 GrShape& 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 GrShape& shape, |
243 | const SkMatrix& matrix, |
244 | SkIRect* unclippedDevShapeBounds, |
245 | SkIRect* devClipBounds) { |
246 | // compute bounds as intersection of rt size, clip, and path |
247 | clip.getConservativeBounds(renderTargetContext->width(), |
248 | renderTargetContext->height(), |
249 | devClipBounds); |
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 GrShape& origShape) { |
266 | SkASSERT(maskFilter); |
267 | |
268 | const GrShape* shape = &origShape; |
269 | SkTLazy<GrShape> 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, |
286 | renderTargetContext, |
287 | std::move(paint), |
288 | clip, |
289 | viewMatrix, |
290 | *shape)) { |
291 | // the mask filter was able to draw itself directly, so there's nothing |
292 | // left to do. |
293 | return; |
294 | } |
295 | assert_alive(paint); |
296 | |
297 | // If the path is hairline, ignore inverse fill. |
298 | bool inverseFilled = shape->inverseFilled() && |
299 | !GrPathRenderer::IsStrokeHairlineOrEquivalent(shape->style(), |
300 | viewMatrix, nullptr); |
301 | |
302 | SkIRect unclippedDevShapeBounds, devClipBounds; |
303 | if (!get_shape_and_clip_bounds(renderTargetContext, clip, *shape, viewMatrix, |
304 | &unclippedDevShapeBounds, |
305 | &devClipBounds)) { |
306 | // TODO: just cons up an opaque mask here |
307 | if (!inverseFilled) { |
308 | return; |
309 | } |
310 | } |
311 | |
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 | #ifdef SK_DISABLE_MASKFILTERED_MASK_CACHING |
315 | bool useCache = false; |
316 | #else |
317 | bool useCache = !inverseFilled && viewMatrix.preservesAxisAlignment() && |
318 | shape->hasUnstyledKey() && as_MFB(maskFilter)->asABlur(nullptr); |
319 | #endif |
320 | |
321 | const SkIRect* boundsForClip = &devClipBounds; |
322 | if (useCache) { |
323 | SkIRect clippedMaskRect, unClippedMaskRect; |
324 | maskFilter->canFilterMaskGPU(*shape, unclippedDevShapeBounds, devClipBounds, |
325 | viewMatrix, &clippedMaskRect); |
326 | maskFilter->canFilterMaskGPU(*shape, unclippedDevShapeBounds, unclippedDevShapeBounds, |
327 | viewMatrix, &unClippedMaskRect); |
328 | if (clippedMaskRect.isEmpty()) { |
329 | return; |
330 | } |
331 | |
332 | // Use the cache only if >50% of the filtered mask is visible. |
333 | int unclippedWidth = unClippedMaskRect.width(); |
334 | int unclippedHeight = unClippedMaskRect.height(); |
335 | int64_t unclippedArea = sk_64_mul(unclippedWidth, unclippedHeight); |
336 | int64_t clippedArea = sk_64_mul(clippedMaskRect.width(), clippedMaskRect.height()); |
337 | int maxTextureSize = renderTargetContext->caps()->maxTextureSize(); |
338 | if (unclippedArea > 2 * clippedArea || unclippedWidth > maxTextureSize || |
339 | unclippedHeight > maxTextureSize) { |
340 | useCache = false; |
341 | } else { |
342 | // Make the clip not affect the mask |
343 | boundsForClip = &unclippedDevShapeBounds; |
344 | } |
345 | } |
346 | |
347 | GrUniqueKey maskKey; |
348 | if (useCache) { |
349 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
350 | GrUniqueKey::Builder builder(&maskKey, kDomain, 5 + 2 + shape->unstyledKeySize(), |
351 | "Mask Filtered Masks" ); |
352 | |
353 | // We require the upper left 2x2 of the matrix to match exactly for a cache hit. |
354 | SkScalar sx = viewMatrix.get(SkMatrix::kMScaleX); |
355 | SkScalar sy = viewMatrix.get(SkMatrix::kMScaleY); |
356 | SkScalar kx = viewMatrix.get(SkMatrix::kMSkewX); |
357 | SkScalar ky = viewMatrix.get(SkMatrix::kMSkewY); |
358 | SkScalar tx = viewMatrix.get(SkMatrix::kMTransX); |
359 | SkScalar ty = viewMatrix.get(SkMatrix::kMTransY); |
360 | // Allow 8 bits each in x and y of subpixel positioning. |
361 | SkFixed fracX = SkScalarToFixed(SkScalarFraction(tx)) & 0x0000FF00; |
362 | SkFixed fracY = SkScalarToFixed(SkScalarFraction(ty)) & 0x0000FF00; |
363 | |
364 | builder[0] = SkFloat2Bits(sx); |
365 | builder[1] = SkFloat2Bits(sy); |
366 | builder[2] = SkFloat2Bits(kx); |
367 | builder[3] = SkFloat2Bits(ky); |
368 | // Distinguish between hairline and filled paths. For hairlines, we also need to include |
369 | // the cap. (SW grows hairlines by 0.5 pixel with round and square caps). Note that |
370 | // stroke-and-fill of hairlines is turned into pure fill by SkStrokeRec, so this covers |
371 | // all cases we might see. |
372 | uint32_t styleBits = shape->style().isSimpleHairline() |
373 | ? ((shape->style().strokeRec().getCap() << 1) | 1) |
374 | : 0; |
375 | builder[4] = fracX | (fracY >> 8) | (styleBits << 16); |
376 | |
377 | SkMaskFilterBase::BlurRec rec; |
378 | SkAssertResult(as_MFB(maskFilter)->asABlur(&rec)); |
379 | |
380 | builder[5] = rec.fStyle; // TODO: we could put this with the other style bits |
381 | builder[6] = SkFloat2Bits(rec.fSigma); |
382 | shape->writeUnstyledKey(&builder[7]); |
383 | } |
384 | |
385 | SkIRect maskRect; |
386 | if (maskFilter->canFilterMaskGPU(*shape, |
387 | unclippedDevShapeBounds, |
388 | *boundsForClip, |
389 | viewMatrix, |
390 | &maskRect)) { |
391 | if (clip_bounds_quick_reject(*boundsForClip, maskRect)) { |
392 | // clipped out |
393 | return; |
394 | } |
395 | |
396 | GrSurfaceProxyView filteredMaskView; |
397 | |
398 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
399 | |
400 | if (maskKey.isValid()) { |
401 | filteredMaskView = find_filtered_mask(proxyProvider, maskKey); |
402 | } |
403 | |
404 | if (!filteredMaskView) { |
405 | std::unique_ptr<GrRenderTargetContext> maskRTC(create_mask_GPU( |
406 | context, |
407 | maskRect, |
408 | viewMatrix, |
409 | *shape, |
410 | renderTargetContext->numSamples())); |
411 | if (maskRTC) { |
412 | filteredMaskView = maskFilter->filterMaskGPU(context, |
413 | maskRTC->readSurfaceView(), |
414 | maskRTC->colorInfo().colorType(), |
415 | maskRTC->colorInfo().alphaType(), |
416 | viewMatrix, |
417 | maskRect); |
418 | if (filteredMaskView.proxy() && maskKey.isValid()) { |
419 | SkASSERT(filteredMaskView.asTextureProxy()); |
420 | proxyProvider->assignUniqueKeyToProxy(maskKey, |
421 | filteredMaskView.asTextureProxy()); |
422 | } |
423 | } |
424 | } |
425 | |
426 | if (filteredMaskView) { |
427 | if (draw_mask(renderTargetContext, clip, viewMatrix, maskRect, std::move(paint), |
428 | std::move(filteredMaskView))) { |
429 | // This path is completely drawn |
430 | return; |
431 | } |
432 | assert_alive(paint); |
433 | } |
434 | } |
435 | |
436 | sw_draw_with_mask_filter(context, renderTargetContext, clip, viewMatrix, *shape, |
437 | maskFilter, *boundsForClip, std::move(paint), maskKey); |
438 | } |
439 | |
440 | void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext* context, |
441 | GrRenderTargetContext* renderTargetContext, |
442 | const GrClip& clip, |
443 | const GrShape& shape, |
444 | GrPaint&& paint, |
445 | const SkMatrix& viewMatrix, |
446 | const SkMaskFilter* mf) { |
447 | draw_shape_with_mask_filter(context, renderTargetContext, clip, std::move(paint), |
448 | viewMatrix, as_MFB(mf), shape); |
449 | } |
450 | |
451 | void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext* context, |
452 | GrRenderTargetContext* renderTargetContext, |
453 | const GrClip& clip, |
454 | const SkPaint& paint, |
455 | const SkMatrix& viewMatrix, |
456 | const GrShape& shape) { |
457 | if (context->priv().abandoned()) { |
458 | return; |
459 | } |
460 | |
461 | GrPaint grPaint; |
462 | if (!SkPaintToGrPaint(context, renderTargetContext->colorInfo(), paint, viewMatrix, &grPaint)) { |
463 | return; |
464 | } |
465 | |
466 | SkMaskFilterBase* mf = as_MFB(paint.getMaskFilter()); |
467 | if (mf && !mf->hasFragmentProcessor()) { |
468 | // The MaskFilter wasn't already handled in SkPaintToGrPaint |
469 | draw_shape_with_mask_filter(context, renderTargetContext, clip, std::move(grPaint), |
470 | viewMatrix, mf, shape); |
471 | } else { |
472 | GrAA aa = GrAA(paint.isAntiAlias()); |
473 | renderTargetContext->drawShape(clip, std::move(grPaint), aa, viewMatrix, shape); |
474 | } |
475 | } |
476 | |