GrBlurUtils.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. #include "src/gpu/GrBlurUtils.h"
  8. #include "include/private/GrRecordingContext.h"
  9. #include "src/gpu/GrCaps.h"
  10. #include "src/gpu/GrFixedClip.h"
  11. #include "src/gpu/GrProxyProvider.h"
  12. #include "src/gpu/GrRecordingContextPriv.h"
  13. #include "src/gpu/GrRenderTargetContext.h"
  14. #include "src/gpu/GrRenderTargetContextPriv.h"
  15. #include "src/gpu/GrSoftwarePathRenderer.h"
  16. #include "src/gpu/GrStyle.h"
  17. #include "src/gpu/GrTextureProxy.h"
  18. #include "src/gpu/effects/generated/GrSimpleTextureEffect.h"
  19. #include "src/gpu/geometry/GrShape.h"
  20. #include "include/core/SkPaint.h"
  21. #include "src/core/SkDraw.h"
  22. #include "src/core/SkMaskFilterBase.h"
  23. #include "src/core/SkTLazy.h"
  24. #include "src/gpu/SkGr.h"
  25. static bool clip_bounds_quick_reject(const SkIRect& clipBounds, const SkIRect& rect) {
  26. return clipBounds.isEmpty() || rect.isEmpty() || !SkIRect::Intersects(clipBounds, rect);
  27. }
  28. // Draw a mask using the supplied paint. Since the coverage/geometry
  29. // is already burnt into the mask this boils down to a rect draw.
  30. // Return true if the mask was successfully drawn.
  31. static bool draw_mask(GrRenderTargetContext* renderTargetContext,
  32. const GrClip& clip,
  33. const SkMatrix& viewMatrix,
  34. const SkIRect& maskRect,
  35. GrPaint&& paint,
  36. sk_sp<GrTextureProxy> mask) {
  37. SkMatrix inverse;
  38. if (!viewMatrix.invert(&inverse)) {
  39. return false;
  40. }
  41. SkMatrix matrix = SkMatrix::MakeTrans(-SkIntToScalar(maskRect.fLeft),
  42. -SkIntToScalar(maskRect.fTop));
  43. matrix.preConcat(viewMatrix);
  44. paint.addCoverageFragmentProcessor(GrSimpleTextureEffect::Make(std::move(mask), matrix));
  45. renderTargetContext->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(),
  46. SkRect::Make(maskRect), inverse);
  47. return true;
  48. }
  49. static void mask_release_proc(void* addr, void* /*context*/) {
  50. SkMask::FreeImage(addr);
  51. }
  52. static bool sw_draw_with_mask_filter(GrRecordingContext* context,
  53. GrRenderTargetContext* renderTargetContext,
  54. const GrClip& clipData,
  55. const SkMatrix& viewMatrix,
  56. const GrShape& shape,
  57. const SkMaskFilter* filter,
  58. const SkIRect& clipBounds,
  59. GrPaint&& paint,
  60. const GrUniqueKey& key) {
  61. SkASSERT(filter);
  62. SkASSERT(!shape.style().applies());
  63. auto proxyProvider = context->priv().proxyProvider();
  64. sk_sp<GrTextureProxy> filteredMask;
  65. SkStrokeRec::InitStyle fillOrHairline = shape.style().isSimpleHairline()
  66. ? SkStrokeRec::kHairline_InitStyle
  67. : SkStrokeRec::kFill_InitStyle;
  68. if (key.isValid()) {
  69. // TODO: this cache look up is duplicated in draw_shape_with_mask_filter for gpu
  70. filteredMask = proxyProvider->findOrCreateProxyByUniqueKey(key, kTopLeft_GrSurfaceOrigin);
  71. }
  72. SkIRect drawRect;
  73. if (filteredMask) {
  74. SkRect devBounds = shape.bounds();
  75. viewMatrix.mapRect(&devBounds);
  76. // Here we need to recompute the destination bounds in order to draw the mask correctly
  77. SkMask srcM, dstM;
  78. if (!SkDraw::ComputeMaskBounds(devBounds, &clipBounds, filter, &viewMatrix,
  79. &srcM.fBounds)) {
  80. return false;
  81. }
  82. srcM.fFormat = SkMask::kA8_Format;
  83. if (!as_MFB(filter)->filterMask(&dstM, srcM, viewMatrix, nullptr)) {
  84. return false;
  85. }
  86. // Unfortunately, we cannot double check that the computed bounds (i.e., dstM.fBounds)
  87. // match the stored bounds of the mask bc the proxy may have been recreated and,
  88. // when it is recreated, it just gets the bounds of the underlying GrTexture (which
  89. // might be a loose fit).
  90. drawRect = dstM.fBounds;
  91. } else {
  92. // TODO: it seems like we could create an SkDraw here and set its fMatrix field rather
  93. // than explicitly transforming the path to device space.
  94. SkPath devPath;
  95. shape.asPath(&devPath);
  96. devPath.transform(viewMatrix);
  97. SkMask srcM, dstM;
  98. if (!SkDraw::DrawToMask(devPath, &clipBounds, filter, &viewMatrix, &srcM,
  99. SkMask::kComputeBoundsAndRenderImage_CreateMode, fillOrHairline)) {
  100. return false;
  101. }
  102. SkAutoMaskFreeImage autoSrc(srcM.fImage);
  103. SkASSERT(SkMask::kA8_Format == srcM.fFormat);
  104. if (!as_MFB(filter)->filterMask(&dstM, srcM, viewMatrix, nullptr)) {
  105. return false;
  106. }
  107. // this will free-up dstM when we're done (allocated in filterMask())
  108. SkAutoMaskFreeImage autoDst(dstM.fImage);
  109. if (clip_bounds_quick_reject(clipBounds, dstM.fBounds)) {
  110. return false;
  111. }
  112. // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
  113. // the current clip (and identity matrix) and GrPaint settings
  114. SkBitmap bm;
  115. if (!bm.installPixels(SkImageInfo::MakeA8(dstM.fBounds.width(), dstM.fBounds.height()),
  116. autoDst.release(), dstM.fRowBytes, mask_release_proc, nullptr)) {
  117. return false;
  118. }
  119. bm.setImmutable();
  120. sk_sp<SkImage> image = SkImage::MakeFromBitmap(bm);
  121. if (!image) {
  122. return false;
  123. }
  124. filteredMask = proxyProvider->createTextureProxy(std::move(image), GrRenderable::kNo, 1,
  125. SkBudgeted::kYes, SkBackingFit::kApprox);
  126. if (!filteredMask) {
  127. return false;
  128. }
  129. SkASSERT(kTopLeft_GrSurfaceOrigin == filteredMask->origin());
  130. drawRect = dstM.fBounds;
  131. if (key.isValid()) {
  132. proxyProvider->assignUniqueKeyToProxy(key, filteredMask.get());
  133. }
  134. }
  135. return draw_mask(renderTargetContext, clipData, viewMatrix, drawRect,
  136. std::move(paint), std::move(filteredMask));
  137. }
  138. // Create a mask of 'shape' and place the result in 'mask'.
  139. static sk_sp<GrTextureProxy> create_mask_GPU(GrRecordingContext* context,
  140. const SkIRect& maskRect,
  141. const SkMatrix& origViewMatrix,
  142. const GrShape& shape,
  143. int sampleCnt) {
  144. sk_sp<GrRenderTargetContext> rtContext(
  145. context->priv().makeDeferredRenderTargetContextWithFallback(
  146. SkBackingFit::kApprox, maskRect.width(), maskRect.height(),
  147. GrColorType::kAlpha_8, nullptr, sampleCnt, GrMipMapped::kNo,
  148. kTopLeft_GrSurfaceOrigin));
  149. if (!rtContext) {
  150. return nullptr;
  151. }
  152. rtContext->priv().absClear(nullptr, SK_PMColor4fTRANSPARENT);
  153. GrPaint maskPaint;
  154. maskPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op);
  155. // setup new clip
  156. const SkIRect clipRect = SkIRect::MakeWH(maskRect.width(), maskRect.height());
  157. GrFixedClip clip(clipRect);
  158. // Draw the mask into maskTexture with the path's integerized top-left at
  159. // the origin using maskPaint.
  160. SkMatrix viewMatrix = origViewMatrix;
  161. viewMatrix.postTranslate(-SkIntToScalar(maskRect.fLeft), -SkIntToScalar(maskRect.fTop));
  162. rtContext->drawShape(clip, std::move(maskPaint), GrAA::kYes, viewMatrix, shape);
  163. return rtContext->asTextureProxyRef();
  164. }
  165. static bool get_unclipped_shape_dev_bounds(const GrShape& shape, const SkMatrix& matrix,
  166. SkIRect* devBounds) {
  167. SkRect shapeBounds = shape.styledBounds();
  168. if (shapeBounds.isEmpty()) {
  169. return false;
  170. }
  171. SkRect shapeDevBounds;
  172. matrix.mapRect(&shapeDevBounds, shapeBounds);
  173. // Even though these are "unclipped" bounds we still clip to the int32_t range.
  174. // This is the largest int32_t that is representable exactly as a float. The next 63 larger ints
  175. // would round down to this value when cast to a float, but who really cares.
  176. // INT32_MIN is exactly representable.
  177. static constexpr int32_t kMaxInt = 2147483520;
  178. if (!shapeDevBounds.intersect(SkRect::MakeLTRB(INT32_MIN, INT32_MIN, kMaxInt, kMaxInt))) {
  179. return false;
  180. }
  181. // Make sure that the resulting SkIRect can have representable width and height
  182. if (SkScalarRoundToInt(shapeDevBounds.width()) > kMaxInt ||
  183. SkScalarRoundToInt(shapeDevBounds.height()) > kMaxInt) {
  184. return false;
  185. }
  186. shapeDevBounds.roundOut(devBounds);
  187. return true;
  188. }
  189. // Gets the shape bounds, the clip bounds, and the intersection (if any). Returns false if there
  190. // is no intersection.
  191. static bool get_shape_and_clip_bounds(GrRenderTargetContext* renderTargetContext,
  192. const GrClip& clip,
  193. const GrShape& shape,
  194. const SkMatrix& matrix,
  195. SkIRect* unclippedDevShapeBounds,
  196. SkIRect* devClipBounds) {
  197. // compute bounds as intersection of rt size, clip, and path
  198. clip.getConservativeBounds(renderTargetContext->width(),
  199. renderTargetContext->height(),
  200. devClipBounds);
  201. if (!get_unclipped_shape_dev_bounds(shape, matrix, unclippedDevShapeBounds)) {
  202. *unclippedDevShapeBounds = SkIRect::EmptyIRect();
  203. return false;
  204. }
  205. return true;
  206. }
  207. static void draw_shape_with_mask_filter(GrRecordingContext* context,
  208. GrRenderTargetContext* renderTargetContext,
  209. const GrClip& clip,
  210. GrPaint&& paint,
  211. const SkMatrix& viewMatrix,
  212. const SkMaskFilterBase* maskFilter,
  213. const GrShape& origShape) {
  214. SkASSERT(maskFilter);
  215. const GrShape* shape = &origShape;
  216. SkTLazy<GrShape> tmpShape;
  217. if (origShape.style().applies()) {
  218. SkScalar styleScale = GrStyle::MatrixToScaleFactor(viewMatrix);
  219. if (0 == styleScale) {
  220. return;
  221. }
  222. tmpShape.init(origShape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, styleScale));
  223. if (tmpShape.get()->isEmpty()) {
  224. return;
  225. }
  226. shape = tmpShape.get();
  227. }
  228. if (maskFilter->directFilterMaskGPU(context,
  229. renderTargetContext,
  230. std::move(paint),
  231. clip,
  232. viewMatrix,
  233. *shape)) {
  234. // the mask filter was able to draw itself directly, so there's nothing
  235. // left to do.
  236. return;
  237. }
  238. assert_alive(paint);
  239. // If the path is hairline, ignore inverse fill.
  240. bool inverseFilled = shape->inverseFilled() &&
  241. !GrPathRenderer::IsStrokeHairlineOrEquivalent(shape->style(),
  242. viewMatrix, nullptr);
  243. SkIRect unclippedDevShapeBounds, devClipBounds;
  244. if (!get_shape_and_clip_bounds(renderTargetContext, clip, *shape, viewMatrix,
  245. &unclippedDevShapeBounds,
  246. &devClipBounds)) {
  247. // TODO: just cons up an opaque mask here
  248. if (!inverseFilled) {
  249. return;
  250. }
  251. }
  252. // To prevent overloading the cache with entries during animations we limit the cache of masks
  253. // to cases where the matrix preserves axis alignment.
  254. #ifdef SK_DISABLE_MASKFILTERED_MASK_CACHING
  255. bool useCache = false;
  256. #else
  257. bool useCache = !inverseFilled && viewMatrix.preservesAxisAlignment() &&
  258. shape->hasUnstyledKey() && as_MFB(maskFilter)->asABlur(nullptr);
  259. #endif
  260. const SkIRect* boundsForClip = &devClipBounds;
  261. if (useCache) {
  262. SkIRect clippedMaskRect, unClippedMaskRect;
  263. maskFilter->canFilterMaskGPU(*shape, unclippedDevShapeBounds, devClipBounds,
  264. viewMatrix, &clippedMaskRect);
  265. maskFilter->canFilterMaskGPU(*shape, unclippedDevShapeBounds, unclippedDevShapeBounds,
  266. viewMatrix, &unClippedMaskRect);
  267. if (clippedMaskRect.isEmpty()) {
  268. return;
  269. }
  270. // Use the cache only if >50% of the filtered mask is visible.
  271. int unclippedWidth = unClippedMaskRect.width();
  272. int unclippedHeight = unClippedMaskRect.height();
  273. int64_t unclippedArea = sk_64_mul(unclippedWidth, unclippedHeight);
  274. int64_t clippedArea = sk_64_mul(clippedMaskRect.width(), clippedMaskRect.height());
  275. int maxTextureSize = renderTargetContext->caps()->maxTextureSize();
  276. if (unclippedArea > 2 * clippedArea || unclippedWidth > maxTextureSize ||
  277. unclippedHeight > maxTextureSize) {
  278. useCache = false;
  279. } else {
  280. // Make the clip not affect the mask
  281. boundsForClip = &unclippedDevShapeBounds;
  282. }
  283. }
  284. GrUniqueKey maskKey;
  285. if (useCache) {
  286. static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
  287. GrUniqueKey::Builder builder(&maskKey, kDomain, 5 + 2 + shape->unstyledKeySize(),
  288. "Mask Filtered Masks");
  289. // We require the upper left 2x2 of the matrix to match exactly for a cache hit.
  290. SkScalar sx = viewMatrix.get(SkMatrix::kMScaleX);
  291. SkScalar sy = viewMatrix.get(SkMatrix::kMScaleY);
  292. SkScalar kx = viewMatrix.get(SkMatrix::kMSkewX);
  293. SkScalar ky = viewMatrix.get(SkMatrix::kMSkewY);
  294. SkScalar tx = viewMatrix.get(SkMatrix::kMTransX);
  295. SkScalar ty = viewMatrix.get(SkMatrix::kMTransY);
  296. // Allow 8 bits each in x and y of subpixel positioning.
  297. SkFixed fracX = SkScalarToFixed(SkScalarFraction(tx)) & 0x0000FF00;
  298. SkFixed fracY = SkScalarToFixed(SkScalarFraction(ty)) & 0x0000FF00;
  299. builder[0] = SkFloat2Bits(sx);
  300. builder[1] = SkFloat2Bits(sy);
  301. builder[2] = SkFloat2Bits(kx);
  302. builder[3] = SkFloat2Bits(ky);
  303. // Distinguish between hairline and filled paths. For hairlines, we also need to include
  304. // the cap. (SW grows hairlines by 0.5 pixel with round and square caps). Note that
  305. // stroke-and-fill of hairlines is turned into pure fill by SkStrokeRec, so this covers
  306. // all cases we might see.
  307. uint32_t styleBits = shape->style().isSimpleHairline()
  308. ? ((shape->style().strokeRec().getCap() << 1) | 1)
  309. : 0;
  310. builder[4] = fracX | (fracY >> 8) | (styleBits << 16);
  311. SkMaskFilterBase::BlurRec rec;
  312. SkAssertResult(as_MFB(maskFilter)->asABlur(&rec));
  313. builder[5] = rec.fStyle; // TODO: we could put this with the other style bits
  314. builder[6] = SkFloat2Bits(rec.fSigma);
  315. shape->writeUnstyledKey(&builder[7]);
  316. }
  317. SkIRect maskRect;
  318. if (maskFilter->canFilterMaskGPU(*shape,
  319. unclippedDevShapeBounds,
  320. *boundsForClip,
  321. viewMatrix,
  322. &maskRect)) {
  323. if (clip_bounds_quick_reject(*boundsForClip, maskRect)) {
  324. // clipped out
  325. return;
  326. }
  327. sk_sp<GrTextureProxy> filteredMask;
  328. GrProxyProvider* proxyProvider = context->priv().proxyProvider();
  329. if (maskKey.isValid()) {
  330. // TODO: this cache look up is duplicated in sw_draw_with_mask_filter for raster
  331. filteredMask = proxyProvider->findOrCreateProxyByUniqueKey(
  332. maskKey, kTopLeft_GrSurfaceOrigin);
  333. }
  334. if (!filteredMask) {
  335. sk_sp<GrTextureProxy> maskProxy(create_mask_GPU(
  336. context,
  337. maskRect,
  338. viewMatrix,
  339. *shape,
  340. renderTargetContext->numSamples()));
  341. if (maskProxy) {
  342. filteredMask = maskFilter->filterMaskGPU(context,
  343. std::move(maskProxy),
  344. viewMatrix,
  345. maskRect);
  346. SkASSERT(kTopLeft_GrSurfaceOrigin == filteredMask->origin());
  347. if (filteredMask && maskKey.isValid()) {
  348. proxyProvider->assignUniqueKeyToProxy(maskKey, filteredMask.get());
  349. }
  350. }
  351. }
  352. if (filteredMask) {
  353. if (draw_mask(renderTargetContext, clip, viewMatrix,
  354. maskRect, std::move(paint), std::move(filteredMask))) {
  355. // This path is completely drawn
  356. return;
  357. }
  358. assert_alive(paint);
  359. }
  360. }
  361. sw_draw_with_mask_filter(context, renderTargetContext, clip, viewMatrix, *shape,
  362. maskFilter, *boundsForClip, std::move(paint), maskKey);
  363. }
  364. void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext* context,
  365. GrRenderTargetContext* renderTargetContext,
  366. const GrClip& clip,
  367. const GrShape& shape,
  368. GrPaint&& paint,
  369. const SkMatrix& viewMatrix,
  370. const SkMaskFilter* mf) {
  371. draw_shape_with_mask_filter(context, renderTargetContext, clip, std::move(paint),
  372. viewMatrix, as_MFB(mf), shape);
  373. }
  374. void GrBlurUtils::drawShapeWithMaskFilter(GrRecordingContext* context,
  375. GrRenderTargetContext* renderTargetContext,
  376. const GrClip& clip,
  377. const SkPaint& paint,
  378. const SkMatrix& viewMatrix,
  379. const GrShape& shape) {
  380. if (context->priv().abandoned()) {
  381. return;
  382. }
  383. GrPaint grPaint;
  384. if (!SkPaintToGrPaint(context, renderTargetContext->colorSpaceInfo(), paint, viewMatrix,
  385. &grPaint)) {
  386. return;
  387. }
  388. SkMaskFilterBase* mf = as_MFB(paint.getMaskFilter());
  389. if (mf && !mf->hasFragmentProcessor()) {
  390. // The MaskFilter wasn't already handled in SkPaintToGrPaint
  391. draw_shape_with_mask_filter(context, renderTargetContext, clip, std::move(grPaint),
  392. viewMatrix, mf, shape);
  393. } else {
  394. GrAA aa = GrAA(paint.isAntiAlias());
  395. renderTargetContext->drawShape(clip, std::move(grPaint), aa, viewMatrix, shape);
  396. }
  397. }