blob: c651c4848b0dc7abdc776202af6fd518f8ba10a2 [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
8#include "GrBlurUtils.h"
Robert Phillipsc7c2baf2018-03-08 09:51:04 -05009
bsalomon76228632015-05-29 08:02:10 -070010#include "GrCaps.h"
robertphillipsccb1b572015-05-27 11:02:55 -070011#include "GrContext.h"
Robert Phillipsc949ce92017-01-19 16:59:04 -050012#include "GrContextPriv.h"
csmartdalton02fa32c2016-08-19 13:29:27 -070013#include "GrFixedClip.h"
Robert Phillipsc7c2baf2018-03-08 09:51:04 -050014#include "GrProxyProvider.h"
15#include "GrRenderTargetContext.h"
Robert Phillips784b7bf2016-12-09 13:35:02 -050016#include "GrRenderTargetContextPriv.h"
bsalomon6663acf2016-05-10 09:14:17 -070017#include "GrStyle.h"
robertphillipsd728f0c2016-11-21 11:05:03 -080018#include "GrTextureProxy.h"
Robert Phillipsc7c2baf2018-03-08 09:51:04 -050019#include "effects/GrSimpleTextureEffect.h"
20
robertphillipsccb1b572015-05-27 11:02:55 -070021#include "SkDraw.h"
Brian Osman3b655982017-03-07 16:58:08 -050022#include "SkGr.h"
Mike Reed80747ef2018-01-23 15:29:32 -050023#include "SkMaskFilterBase.h"
robertphillipsccb1b572015-05-27 11:02:55 -070024#include "SkPaint.h"
csmartdaltonc6f411e2016-08-05 22:32:12 -070025#include "SkTLazy.h"
robertphillipsccb1b572015-05-27 11:02:55 -070026
27static bool clip_bounds_quick_reject(const SkIRect& clipBounds, const SkIRect& rect) {
28 return clipBounds.isEmpty() || rect.isEmpty() || !SkIRect::Intersects(clipBounds, rect);
29}
30
31// Draw a mask using the supplied paint. Since the coverage/geometry
32// is already burnt into the mask this boils down to a rect draw.
33// Return true if the mask was successfully drawn.
Robert Phillips296b1cc2017-03-15 10:42:12 -040034static bool draw_mask(GrRenderTargetContext* renderTargetContext,
robertphillipsccb1b572015-05-27 11:02:55 -070035 const GrClip& clip,
36 const SkMatrix& viewMatrix,
robertphillipsf054b172016-05-13 05:06:19 -070037 const SkIRect& maskRect,
Brian Salomon82f44312017-01-11 13:42:54 -050038 GrPaint&& paint,
Robert Phillips4a24da52016-12-14 09:00:07 -050039 sk_sp<GrTextureProxy> mask) {
Brian Salomon82f44312017-01-11 13:42:54 -050040 SkMatrix inverse;
41 if (!viewMatrix.invert(&inverse)) {
42 return false;
43 }
Robert Phillips4a24da52016-12-14 09:00:07 -050044
Robert Phillips67c18d62017-01-20 12:44:06 -050045 SkMatrix matrix = SkMatrix::MakeTrans(-SkIntToScalar(maskRect.fLeft),
46 -SkIntToScalar(maskRect.fTop));
Brian Salomon2ebd0c82016-10-03 17:15:28 -040047 matrix.preConcat(viewMatrix);
Brian Osman2240be92017-10-18 13:15:13 -040048 paint.addCoverageFragmentProcessor(GrSimpleTextureEffect::Make(std::move(mask), matrix));
robertphillipsccb1b572015-05-27 11:02:55 -070049
Brian Salomon82f44312017-01-11 13:42:54 -050050 renderTargetContext->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050051 SkRect::Make(maskRect), inverse);
robertphillipsccb1b572015-05-27 11:02:55 -070052 return true;
53}
54
Robert Phillipsc7c2baf2018-03-08 09:51:04 -050055static void mask_release_proc(void* addr, void* /*context*/) {
56 SkMask::FreeImage(addr);
57}
58
Robert Phillips4a24da52016-12-14 09:00:07 -050059static bool sw_draw_with_mask_filter(GrContext* context,
60 GrRenderTargetContext* renderTargetContext,
robertphillips0e7029e2015-11-30 05:45:06 -080061 const GrClip& clipData,
62 const SkMatrix& viewMatrix,
63 const SkPath& devPath,
64 const SkMaskFilter* filter,
65 const SkIRect& clipBounds,
Brian Salomon82f44312017-01-11 13:42:54 -050066 GrPaint&& paint,
bsalomon6663acf2016-05-10 09:14:17 -070067 SkStrokeRec::InitStyle fillOrHairline) {
Robert Phillipsc7c2baf2018-03-08 09:51:04 -050068 SkMask srcM, dstM;
robertphillipsccb1b572015-05-27 11:02:55 -070069 if (!SkDraw::DrawToMask(devPath, &clipBounds, filter, &viewMatrix, &srcM,
bsalomon6663acf2016-05-10 09:14:17 -070070 SkMask::kComputeBoundsAndRenderImage_CreateMode, fillOrHairline)) {
robertphillipsccb1b572015-05-27 11:02:55 -070071 return false;
72 }
73 SkAutoMaskFreeImage autoSrc(srcM.fImage);
74
Mike Reed80747ef2018-01-23 15:29:32 -050075 if (!as_MFB(filter)->filterMask(&dstM, srcM, viewMatrix, nullptr)) {
robertphillipsccb1b572015-05-27 11:02:55 -070076 return false;
77 }
78 // this will free-up dstM when we're done (allocated in filterMask())
79 SkAutoMaskFreeImage autoDst(dstM.fImage);
80
81 if (clip_bounds_quick_reject(clipBounds, dstM.fBounds)) {
82 return false;
83 }
84
85 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
86 // the current clip (and identity matrix) and GrPaint settings
Robert Phillipsc7c2baf2018-03-08 09:51:04 -050087 SkBitmap bm;
88 if (!bm.installPixels(SkImageInfo::MakeA8(dstM.fBounds.width(), dstM.fBounds.height()),
89 autoDst.release(), dstM.fRowBytes, mask_release_proc, nullptr)) {
Robert Phillips4a24da52016-12-14 09:00:07 -050090 return false;
91 }
Robert Phillipsc7c2baf2018-03-08 09:51:04 -050092 bm.setImmutable();
Robert Phillips4a24da52016-12-14 09:00:07 -050093
Robert Phillipsc7c2baf2018-03-08 09:51:04 -050094 sk_sp<SkImage> image = SkImage::MakeFromBitmap(bm);
Robert Phillips48ce22b2018-03-23 10:33:12 -040095 if (!image) {
96 return false;
97 }
Robert Phillipsc7c2baf2018-03-08 09:51:04 -050098
99 auto proxyProvider = context->contextPriv().proxyProvider();
100 sk_sp<GrTextureProxy> maskProxy = proxyProvider->createTextureProxy(std::move(image),
101 kNone_GrSurfaceFlags,
102 1, SkBudgeted::kYes,
103 SkBackingFit::kApprox);
Robert Phillips48ce22b2018-03-23 10:33:12 -0400104 if (!maskProxy) {
105 return false;
106 }
robertphillipsccb1b572015-05-27 11:02:55 -0700107
Robert Phillips296b1cc2017-03-15 10:42:12 -0400108 return draw_mask(renderTargetContext, clipData, viewMatrix,
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500109 dstM.fBounds, std::move(paint), std::move(maskProxy));
robertphillipsccb1b572015-05-27 11:02:55 -0700110}
111
112// Create a mask of 'devPath' and place the result in 'mask'.
robertphillipsd728f0c2016-11-21 11:05:03 -0800113static sk_sp<GrTextureProxy> create_mask_GPU(GrContext* context,
114 const SkIRect& maskRect,
115 const SkPath& devPath,
116 SkStrokeRec::InitStyle fillOrHairline,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500117 GrAA aa,
robertphillipsd728f0c2016-11-21 11:05:03 -0800118 int sampleCnt) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500119 if (GrAA::kNo == aa) {
robertphillipsd4c741e2016-04-28 09:55:15 -0700120 // Don't need MSAA if mask isn't AA
Brian Salomonbdecacf2018-02-02 20:32:49 -0500121 sampleCnt = 1;
robertphillipsd4c741e2016-04-28 09:55:15 -0700122 }
123
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500124 sk_sp<GrRenderTargetContext> rtContext(
125 context->contextPriv().makeDeferredRenderTargetContextWithFallback(
126 SkBackingFit::kApprox, maskRect.width(), maskRect.height(), kAlpha_8_GrPixelConfig,
127 nullptr, sampleCnt));
robertphillipsd728f0c2016-11-21 11:05:03 -0800128 if (!rtContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700129 return nullptr;
robertphillipsccb1b572015-05-27 11:02:55 -0700130 }
131
Robert Phillips784b7bf2016-12-09 13:35:02 -0500132 rtContext->priv().absClear(nullptr, 0x0);
robertphillipsccb1b572015-05-27 11:02:55 -0700133
Brian Salomon82f44312017-01-11 13:42:54 -0500134 GrPaint maskPaint;
135 maskPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op);
robertphillipsccb1b572015-05-27 11:02:55 -0700136
137 // setup new clip
cdalton846c0512016-05-13 10:25:00 -0700138 const SkIRect clipRect = SkIRect::MakeWH(maskRect.width(), maskRect.height());
139 GrFixedClip clip(clipRect);
robertphillipsccb1b572015-05-27 11:02:55 -0700140
robertphillips3833daa2015-09-14 11:18:13 -0700141 // Draw the mask into maskTexture with the path's integerized top-left at
Brian Salomon82f44312017-01-11 13:42:54 -0500142 // the origin using maskPaint.
robertphillipsccb1b572015-05-27 11:02:55 -0700143 SkMatrix translate;
robertphillipsf054b172016-05-13 05:06:19 -0700144 translate.setTranslate(-SkIntToScalar(maskRect.fLeft), -SkIntToScalar(maskRect.fTop));
Brian Salomon82f44312017-01-11 13:42:54 -0500145 rtContext->drawPath(clip, std::move(maskPaint), aa, translate, devPath,
146 GrStyle(fillOrHairline));
Robert Phillipsf200a902017-01-30 13:27:37 -0500147 return rtContext->asTextureProxyRef();
robertphillipsccb1b572015-05-27 11:02:55 -0700148}
149
bsalomonc55271f2015-11-09 11:55:57 -0800150static void draw_path_with_mask_filter(GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -0400151 GrRenderTargetContext* renderTargetContext,
bsalomonc55271f2015-11-09 11:55:57 -0800152 const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500153 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500154 GrAA aa,
bsalomonc55271f2015-11-09 11:55:57 -0800155 const SkMatrix& viewMatrix,
Mike Reed80747ef2018-01-23 15:29:32 -0500156 const SkMaskFilterBase* maskFilter,
bsalomon6663acf2016-05-10 09:14:17 -0700157 const GrStyle& style,
158 const SkPath* path,
bsalomonc55271f2015-11-09 11:55:57 -0800159 bool pathIsMutable) {
160 SkASSERT(maskFilter);
161
162 SkIRect clipBounds;
Robert Phillips784b7bf2016-12-09 13:35:02 -0500163 clip.getConservativeBounds(renderTargetContext->width(),
164 renderTargetContext->height(),
Brian Osman11052242016-10-27 14:47:55 -0400165 &clipBounds);
bsalomonc55271f2015-11-09 11:55:57 -0800166 SkTLazy<SkPath> tmpPath;
bsalomon6663acf2016-05-10 09:14:17 -0700167 SkStrokeRec::InitStyle fillOrHairline;
bsalomonc55271f2015-11-09 11:55:57 -0800168
bsalomon6663acf2016-05-10 09:14:17 -0700169 // We just fully apply the style here.
170 if (style.applies()) {
senorblancob6a40b82016-08-19 08:07:22 -0700171 SkScalar scale = GrStyle::MatrixToScaleFactor(viewMatrix);
172 if (0 == scale || !style.applyToPath(tmpPath.init(), &fillOrHairline, *path, scale)) {
bsalomon6663acf2016-05-10 09:14:17 -0700173 return;
174 }
175 pathIsMutable = true;
176 path = tmpPath.get();
177 } else if (style.isSimpleHairline()) {
178 fillOrHairline = SkStrokeRec::kHairline_InitStyle;
bsalomon055e1922016-05-06 07:22:58 -0700179 } else {
bsalomon6663acf2016-05-10 09:14:17 -0700180 SkASSERT(style.isSimpleFill());
181 fillOrHairline = SkStrokeRec::kFill_InitStyle;
bsalomonc55271f2015-11-09 11:55:57 -0800182 }
183
184 // transform the path into device space
bsalomon6663acf2016-05-10 09:14:17 -0700185 if (!viewMatrix.isIdentity()) {
186 SkPath* result;
187 if (pathIsMutable) {
188 result = const_cast<SkPath*>(path);
189 } else {
190 if (!tmpPath.isValid()) {
191 tmpPath.init();
192 }
193 result = tmpPath.get();
194 }
195 path->transform(viewMatrix, result);
196 path = result;
197 result->setIsVolatile(true);
198 pathIsMutable = true;
199 }
bsalomonc55271f2015-11-09 11:55:57 -0800200
201 SkRect maskRect;
bsalomon6663acf2016-05-10 09:14:17 -0700202 if (maskFilter->canFilterMaskGPU(SkRRect::MakeRect(path->getBounds()),
bsalomonc55271f2015-11-09 11:55:57 -0800203 clipBounds,
204 viewMatrix,
205 &maskRect)) {
robertphillipsf054b172016-05-13 05:06:19 -0700206 // This mask will ultimately be drawn as a non-AA rect (see draw_mask).
207 // Non-AA rects have a bad habit of snapping arbitrarily. Integerize here
208 // so the mask draws in a reproducible manner.
bsalomonc55271f2015-11-09 11:55:57 -0800209 SkIRect finalIRect;
210 maskRect.roundOut(&finalIRect);
211 if (clip_bounds_quick_reject(clipBounds, finalIRect)) {
212 // clipped out
213 return;
214 }
215
Robert Phillipsd3749482017-03-14 09:17:43 -0400216 if (maskFilter->directFilterMaskGPU(context,
Brian Osman11052242016-10-27 14:47:55 -0400217 renderTargetContext,
Brian Salomon82f44312017-01-11 13:42:54 -0500218 std::move(paint),
bsalomonc55271f2015-11-09 11:55:57 -0800219 clip,
220 viewMatrix,
bsalomon6663acf2016-05-10 09:14:17 -0700221 SkStrokeRec(fillOrHairline),
222 *path)) {
bsalomonc55271f2015-11-09 11:55:57 -0800223 // the mask filter was able to draw itself directly, so there's nothing
224 // left to do.
225 return;
226 }
227
robertphillipsd728f0c2016-11-21 11:05:03 -0800228 sk_sp<GrTextureProxy> maskProxy(create_mask_GPU(context,
229 finalIRect,
230 *path,
231 fillOrHairline,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500232 aa,
robertphillipsd728f0c2016-11-21 11:05:03 -0800233 renderTargetContext->numColorSamples()));
234 if (maskProxy) {
Robert Phillips4a24da52016-12-14 09:00:07 -0500235 sk_sp<GrTextureProxy> filtered = maskFilter->filterMaskGPU(context,
236 std::move(maskProxy),
237 viewMatrix,
238 finalIRect);
239 if (filtered) {
Robert Phillips296b1cc2017-03-15 10:42:12 -0400240 if (draw_mask(renderTargetContext, clip, viewMatrix,
Brian Salomon82f44312017-01-11 13:42:54 -0500241 finalIRect, std::move(paint), std::move(filtered))) {
bsalomonc55271f2015-11-09 11:55:57 -0800242 // This path is completely drawn
243 return;
244 }
245 }
246 }
247 }
248
Brian Salomon82f44312017-01-11 13:42:54 -0500249 sw_draw_with_mask_filter(context, renderTargetContext, clip, viewMatrix, *path, maskFilter,
250 clipBounds, std::move(paint), fillOrHairline);
bsalomon6663acf2016-05-10 09:14:17 -0700251}
252
253void GrBlurUtils::drawPathWithMaskFilter(GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -0400254 GrRenderTargetContext* renderTargetContext,
bsalomon6663acf2016-05-10 09:14:17 -0700255 const GrClip& clip,
256 const SkPath& path,
Brian Salomon82f44312017-01-11 13:42:54 -0500257 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500258 GrAA aa,
bsalomon6663acf2016-05-10 09:14:17 -0700259 const SkMatrix& viewMatrix,
260 const SkMaskFilter* mf,
261 const GrStyle& style,
262 bool pathIsMutable) {
Brian Salomon82f44312017-01-11 13:42:54 -0500263 draw_path_with_mask_filter(context, renderTargetContext, clip, std::move(paint), aa, viewMatrix,
Mike Reed80747ef2018-01-23 15:29:32 -0500264 as_MFB(mf), style, &path, pathIsMutable);
bsalomonc55271f2015-11-09 11:55:57 -0800265}
266
267void GrBlurUtils::drawPathWithMaskFilter(GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -0400268 GrRenderTargetContext* renderTargetContext,
bsalomonc55271f2015-11-09 11:55:57 -0800269 const GrClip& clip,
270 const SkPath& origPath,
robertphillipsccb1b572015-05-27 11:02:55 -0700271 const SkPaint& paint,
272 const SkMatrix& origViewMatrix,
273 const SkMatrix* prePathMatrix,
274 const SkIRect& clipBounds,
275 bool pathIsMutable) {
Khushalc421ca12018-06-26 14:38:34 -0700276 if (context->abandoned()) {
Robert Phillipsbebfd412018-03-08 11:07:23 -0500277 return;
278 }
279
bsalomon6663acf2016-05-10 09:14:17 -0700280 SkASSERT(!pathIsMutable || origPath.isVolatile());
robertphillipsccb1b572015-05-27 11:02:55 -0700281
bsalomon6663acf2016-05-10 09:14:17 -0700282 GrStyle style(paint);
robertphillipsccb1b572015-05-27 11:02:55 -0700283 // If we have a prematrix, apply it to the path, optimizing for the case
284 // where the original path can in fact be modified in place (even though
285 // its parameter type is const).
bsalomon6663acf2016-05-10 09:14:17 -0700286
287 const SkPath* path = &origPath;
robertphillipsccb1b572015-05-27 11:02:55 -0700288 SkTLazy<SkPath> tmpPath;
robertphillipsccb1b572015-05-27 11:02:55 -0700289
290 SkMatrix viewMatrix = origViewMatrix;
291
292 if (prePathMatrix) {
bsalomon6663acf2016-05-10 09:14:17 -0700293 // Styling, blurs, and shading are supposed to be applied *after* the prePathMatrix.
294 if (!paint.getMaskFilter() && !paint.getShader() && !style.applies()) {
robertphillipsccb1b572015-05-27 11:02:55 -0700295 viewMatrix.preConcat(*prePathMatrix);
296 } else {
bsalomon6663acf2016-05-10 09:14:17 -0700297 SkPath* result = pathIsMutable ? const_cast<SkPath*>(path) : tmpPath.init();
298 pathIsMutable = true;
299 path->transform(*prePathMatrix, result);
300 path = result;
301 result->setIsVolatile(true);
robertphillipsccb1b572015-05-27 11:02:55 -0700302 }
303 }
304 // at this point we're done with prePathMatrix
305 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
306
307 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400308 if (!SkPaintToGrPaint(context, renderTargetContext->colorSpaceInfo(), paint, viewMatrix,
309 &grPaint)) {
robertphillipsccb1b572015-05-27 11:02:55 -0700310 return;
311 }
Chris Dalton3b51df12017-11-27 14:33:06 -0700312 GrAA aa = GrAA(paint.isAntiAlias());
Mike Reed80747ef2018-01-23 15:29:32 -0500313 SkMaskFilterBase* mf = as_MFB(paint.getMaskFilter());
Mike Reedbfadcf02018-01-20 22:24:21 +0000314 if (mf && !mf->hasFragmentProcessor()) {
Robert Phillipsa29a9562016-10-20 09:40:55 -0400315 // The MaskFilter wasn't already handled in SkPaintToGrPaint
Brian Salomon82f44312017-01-11 13:42:54 -0500316 draw_path_with_mask_filter(context, renderTargetContext, clip, std::move(grPaint), aa,
317 viewMatrix, mf, style, path, pathIsMutable);
bsalomonc55271f2015-11-09 11:55:57 -0800318 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500319 renderTargetContext->drawPath(clip, std::move(grPaint), aa, viewMatrix, *path, style);
robertphillipsccb1b572015-05-27 11:02:55 -0700320 }
robertphillipsccb1b572015-05-27 11:02:55 -0700321}