blob: b79a38a884b395c038a25205cb80429010483417 [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"
Brian Osman11052242016-10-27 14:47:55 -04009#include "GrRenderTargetContext.h"
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 Phillips784b7bf2016-12-09 13:35:02 -050014#include "GrRenderTargetContextPriv.h"
robertphillipsccb1b572015-05-27 11:02:55 -070015#include "effects/GrSimpleTextureEffect.h"
bsalomon6663acf2016-05-10 09:14:17 -070016#include "GrStyle.h"
robertphillipsd728f0c2016-11-21 11:05:03 -080017#include "GrTextureProxy.h"
robertphillipsccb1b572015-05-27 11:02:55 -070018#include "SkDraw.h"
Brian Osman3b655982017-03-07 16:58:08 -050019#include "SkGr.h"
Mike Reed80747ef2018-01-23 15:29:32 -050020#include "SkMaskFilterBase.h"
robertphillipsccb1b572015-05-27 11:02:55 -070021#include "SkPaint.h"
csmartdaltonc6f411e2016-08-05 22:32:12 -070022#include "SkTLazy.h"
robertphillipsccb1b572015-05-27 11:02:55 -070023
24static bool clip_bounds_quick_reject(const SkIRect& clipBounds, const SkIRect& rect) {
25 return clipBounds.isEmpty() || rect.isEmpty() || !SkIRect::Intersects(clipBounds, rect);
26}
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.
Robert Phillips296b1cc2017-03-15 10:42:12 -040031static bool draw_mask(GrRenderTargetContext* renderTargetContext,
robertphillipsccb1b572015-05-27 11:02:55 -070032 const GrClip& clip,
33 const SkMatrix& viewMatrix,
robertphillipsf054b172016-05-13 05:06:19 -070034 const SkIRect& maskRect,
Brian Salomon82f44312017-01-11 13:42:54 -050035 GrPaint&& paint,
Robert Phillips4a24da52016-12-14 09:00:07 -050036 sk_sp<GrTextureProxy> mask) {
Brian Salomon82f44312017-01-11 13:42:54 -050037 SkMatrix inverse;
38 if (!viewMatrix.invert(&inverse)) {
39 return false;
40 }
Robert Phillips4a24da52016-12-14 09:00:07 -050041
Robert Phillips67c18d62017-01-20 12:44:06 -050042 SkMatrix matrix = SkMatrix::MakeTrans(-SkIntToScalar(maskRect.fLeft),
43 -SkIntToScalar(maskRect.fTop));
Brian Salomon2ebd0c82016-10-03 17:15:28 -040044 matrix.preConcat(viewMatrix);
Brian Osman2240be92017-10-18 13:15:13 -040045 paint.addCoverageFragmentProcessor(GrSimpleTextureEffect::Make(std::move(mask), matrix));
robertphillipsccb1b572015-05-27 11:02:55 -070046
Brian Salomon82f44312017-01-11 13:42:54 -050047 renderTargetContext->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050048 SkRect::Make(maskRect), inverse);
robertphillipsccb1b572015-05-27 11:02:55 -070049 return true;
50}
51
Robert Phillips4a24da52016-12-14 09:00:07 -050052static bool sw_draw_with_mask_filter(GrContext* context,
53 GrRenderTargetContext* renderTargetContext,
robertphillips0e7029e2015-11-30 05:45:06 -080054 const GrClip& clipData,
55 const SkMatrix& viewMatrix,
56 const SkPath& devPath,
57 const SkMaskFilter* filter,
58 const SkIRect& clipBounds,
Brian Salomon82f44312017-01-11 13:42:54 -050059 GrPaint&& paint,
bsalomon6663acf2016-05-10 09:14:17 -070060 SkStrokeRec::InitStyle fillOrHairline) {
robertphillipsccb1b572015-05-27 11:02:55 -070061 SkMask srcM, dstM;
robertphillipsccb1b572015-05-27 11:02:55 -070062 if (!SkDraw::DrawToMask(devPath, &clipBounds, filter, &viewMatrix, &srcM,
bsalomon6663acf2016-05-10 09:14:17 -070063 SkMask::kComputeBoundsAndRenderImage_CreateMode, fillOrHairline)) {
robertphillipsccb1b572015-05-27 11:02:55 -070064 return false;
65 }
66 SkAutoMaskFreeImage autoSrc(srcM.fImage);
67
Mike Reed80747ef2018-01-23 15:29:32 -050068 if (!as_MFB(filter)->filterMask(&dstM, srcM, viewMatrix, nullptr)) {
robertphillipsccb1b572015-05-27 11:02:55 -070069 return false;
70 }
71 // this will free-up dstM when we're done (allocated in filterMask())
72 SkAutoMaskFreeImage autoDst(dstM.fImage);
73
74 if (clip_bounds_quick_reject(clipBounds, dstM.fBounds)) {
75 return false;
76 }
77
78 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
79 // the current clip (and identity matrix) and GrPaint settings
80 GrSurfaceDesc desc;
Robert Phillips40fd7c92017-01-30 08:06:27 -050081 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
robertphillipsccb1b572015-05-27 11:02:55 -070082 desc.fWidth = dstM.fBounds.width();
83 desc.fHeight = dstM.fBounds.height();
84 desc.fConfig = kAlpha_8_GrPixelConfig;
85
Robert Phillipsc949ce92017-01-19 16:59:04 -050086 sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeDeferredSurfaceContext(
87 desc,
Greg Daniel65c7f662017-10-30 13:39:09 -040088 GrMipMapped::kNo,
Robert Phillipsc949ce92017-01-19 16:59:04 -050089 SkBackingFit::kApprox,
90 SkBudgeted::kYes);
91 if (!sContext) {
Robert Phillips4a24da52016-12-14 09:00:07 -050092 return false;
93 }
94
Robert Phillipsc949ce92017-01-19 16:59:04 -050095 SkImageInfo ii = SkImageInfo::MakeA8(desc.fWidth, desc.fHeight);
96 if (!sContext->writePixels(ii, dstM.fImage, dstM.fRowBytes, 0, 0)) {
robertphillipsccb1b572015-05-27 11:02:55 -070097 return false;
98 }
robertphillipsccb1b572015-05-27 11:02:55 -070099
Robert Phillips296b1cc2017-03-15 10:42:12 -0400100 return draw_mask(renderTargetContext, clipData, viewMatrix,
Robert Phillipsf200a902017-01-30 13:27:37 -0500101 dstM.fBounds, std::move(paint), sContext->asTextureProxyRef());
robertphillipsccb1b572015-05-27 11:02:55 -0700102}
103
104// Create a mask of 'devPath' and place the result in 'mask'.
robertphillipsd728f0c2016-11-21 11:05:03 -0800105static sk_sp<GrTextureProxy> create_mask_GPU(GrContext* context,
106 const SkIRect& maskRect,
107 const SkPath& devPath,
108 SkStrokeRec::InitStyle fillOrHairline,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500109 GrAA aa,
robertphillipsd728f0c2016-11-21 11:05:03 -0800110 int sampleCnt) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500111 if (GrAA::kNo == aa) {
robertphillipsd4c741e2016-04-28 09:55:15 -0700112 // Don't need MSAA if mask isn't AA
113 sampleCnt = 0;
114 }
115
Robert Phillips55360b12016-12-05 13:18:47 +0000116 sk_sp<GrRenderTargetContext> rtContext(context->makeDeferredRenderTargetContextWithFallback(
Brian Osman11052242016-10-27 14:47:55 -0400117 SkBackingFit::kApprox, maskRect.width(), maskRect.height(), kAlpha_8_GrPixelConfig, nullptr,
118 sampleCnt));
robertphillipsd728f0c2016-11-21 11:05:03 -0800119 if (!rtContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700120 return nullptr;
robertphillipsccb1b572015-05-27 11:02:55 -0700121 }
122
Robert Phillips784b7bf2016-12-09 13:35:02 -0500123 rtContext->priv().absClear(nullptr, 0x0);
robertphillipsccb1b572015-05-27 11:02:55 -0700124
Brian Salomon82f44312017-01-11 13:42:54 -0500125 GrPaint maskPaint;
126 maskPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op);
robertphillipsccb1b572015-05-27 11:02:55 -0700127
128 // setup new clip
cdalton846c0512016-05-13 10:25:00 -0700129 const SkIRect clipRect = SkIRect::MakeWH(maskRect.width(), maskRect.height());
130 GrFixedClip clip(clipRect);
robertphillipsccb1b572015-05-27 11:02:55 -0700131
robertphillips3833daa2015-09-14 11:18:13 -0700132 // Draw the mask into maskTexture with the path's integerized top-left at
Brian Salomon82f44312017-01-11 13:42:54 -0500133 // the origin using maskPaint.
robertphillipsccb1b572015-05-27 11:02:55 -0700134 SkMatrix translate;
robertphillipsf054b172016-05-13 05:06:19 -0700135 translate.setTranslate(-SkIntToScalar(maskRect.fLeft), -SkIntToScalar(maskRect.fTop));
Brian Salomon82f44312017-01-11 13:42:54 -0500136 rtContext->drawPath(clip, std::move(maskPaint), aa, translate, devPath,
137 GrStyle(fillOrHairline));
Robert Phillipsf200a902017-01-30 13:27:37 -0500138 return rtContext->asTextureProxyRef();
robertphillipsccb1b572015-05-27 11:02:55 -0700139}
140
bsalomonc55271f2015-11-09 11:55:57 -0800141static void draw_path_with_mask_filter(GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -0400142 GrRenderTargetContext* renderTargetContext,
bsalomonc55271f2015-11-09 11:55:57 -0800143 const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500144 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500145 GrAA aa,
bsalomonc55271f2015-11-09 11:55:57 -0800146 const SkMatrix& viewMatrix,
Mike Reed80747ef2018-01-23 15:29:32 -0500147 const SkMaskFilterBase* maskFilter,
bsalomon6663acf2016-05-10 09:14:17 -0700148 const GrStyle& style,
149 const SkPath* path,
bsalomonc55271f2015-11-09 11:55:57 -0800150 bool pathIsMutable) {
151 SkASSERT(maskFilter);
152
153 SkIRect clipBounds;
Robert Phillips784b7bf2016-12-09 13:35:02 -0500154 clip.getConservativeBounds(renderTargetContext->width(),
155 renderTargetContext->height(),
Brian Osman11052242016-10-27 14:47:55 -0400156 &clipBounds);
bsalomonc55271f2015-11-09 11:55:57 -0800157 SkTLazy<SkPath> tmpPath;
bsalomon6663acf2016-05-10 09:14:17 -0700158 SkStrokeRec::InitStyle fillOrHairline;
bsalomonc55271f2015-11-09 11:55:57 -0800159
bsalomon6663acf2016-05-10 09:14:17 -0700160 // We just fully apply the style here.
161 if (style.applies()) {
senorblancob6a40b82016-08-19 08:07:22 -0700162 SkScalar scale = GrStyle::MatrixToScaleFactor(viewMatrix);
163 if (0 == scale || !style.applyToPath(tmpPath.init(), &fillOrHairline, *path, scale)) {
bsalomon6663acf2016-05-10 09:14:17 -0700164 return;
165 }
166 pathIsMutable = true;
167 path = tmpPath.get();
168 } else if (style.isSimpleHairline()) {
169 fillOrHairline = SkStrokeRec::kHairline_InitStyle;
bsalomon055e1922016-05-06 07:22:58 -0700170 } else {
bsalomon6663acf2016-05-10 09:14:17 -0700171 SkASSERT(style.isSimpleFill());
172 fillOrHairline = SkStrokeRec::kFill_InitStyle;
bsalomonc55271f2015-11-09 11:55:57 -0800173 }
174
175 // transform the path into device space
bsalomon6663acf2016-05-10 09:14:17 -0700176 if (!viewMatrix.isIdentity()) {
177 SkPath* result;
178 if (pathIsMutable) {
179 result = const_cast<SkPath*>(path);
180 } else {
181 if (!tmpPath.isValid()) {
182 tmpPath.init();
183 }
184 result = tmpPath.get();
185 }
186 path->transform(viewMatrix, result);
187 path = result;
188 result->setIsVolatile(true);
189 pathIsMutable = true;
190 }
bsalomonc55271f2015-11-09 11:55:57 -0800191
192 SkRect maskRect;
bsalomon6663acf2016-05-10 09:14:17 -0700193 if (maskFilter->canFilterMaskGPU(SkRRect::MakeRect(path->getBounds()),
bsalomonc55271f2015-11-09 11:55:57 -0800194 clipBounds,
195 viewMatrix,
196 &maskRect)) {
robertphillipsf054b172016-05-13 05:06:19 -0700197 // This mask will ultimately be drawn as a non-AA rect (see draw_mask).
198 // Non-AA rects have a bad habit of snapping arbitrarily. Integerize here
199 // so the mask draws in a reproducible manner.
bsalomonc55271f2015-11-09 11:55:57 -0800200 SkIRect finalIRect;
201 maskRect.roundOut(&finalIRect);
202 if (clip_bounds_quick_reject(clipBounds, finalIRect)) {
203 // clipped out
204 return;
205 }
206
Robert Phillipsd3749482017-03-14 09:17:43 -0400207 if (maskFilter->directFilterMaskGPU(context,
Brian Osman11052242016-10-27 14:47:55 -0400208 renderTargetContext,
Brian Salomon82f44312017-01-11 13:42:54 -0500209 std::move(paint),
bsalomonc55271f2015-11-09 11:55:57 -0800210 clip,
211 viewMatrix,
bsalomon6663acf2016-05-10 09:14:17 -0700212 SkStrokeRec(fillOrHairline),
213 *path)) {
bsalomonc55271f2015-11-09 11:55:57 -0800214 // the mask filter was able to draw itself directly, so there's nothing
215 // left to do.
216 return;
217 }
218
robertphillipsd728f0c2016-11-21 11:05:03 -0800219 sk_sp<GrTextureProxy> maskProxy(create_mask_GPU(context,
220 finalIRect,
221 *path,
222 fillOrHairline,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500223 aa,
robertphillipsd728f0c2016-11-21 11:05:03 -0800224 renderTargetContext->numColorSamples()));
225 if (maskProxy) {
Robert Phillips4a24da52016-12-14 09:00:07 -0500226 sk_sp<GrTextureProxy> filtered = maskFilter->filterMaskGPU(context,
227 std::move(maskProxy),
228 viewMatrix,
229 finalIRect);
230 if (filtered) {
Robert Phillips296b1cc2017-03-15 10:42:12 -0400231 if (draw_mask(renderTargetContext, clip, viewMatrix,
Brian Salomon82f44312017-01-11 13:42:54 -0500232 finalIRect, std::move(paint), std::move(filtered))) {
bsalomonc55271f2015-11-09 11:55:57 -0800233 // This path is completely drawn
234 return;
235 }
236 }
237 }
238 }
239
Brian Salomon82f44312017-01-11 13:42:54 -0500240 sw_draw_with_mask_filter(context, renderTargetContext, clip, viewMatrix, *path, maskFilter,
241 clipBounds, std::move(paint), fillOrHairline);
bsalomon6663acf2016-05-10 09:14:17 -0700242}
243
244void GrBlurUtils::drawPathWithMaskFilter(GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -0400245 GrRenderTargetContext* renderTargetContext,
bsalomon6663acf2016-05-10 09:14:17 -0700246 const GrClip& clip,
247 const SkPath& path,
Brian Salomon82f44312017-01-11 13:42:54 -0500248 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500249 GrAA aa,
bsalomon6663acf2016-05-10 09:14:17 -0700250 const SkMatrix& viewMatrix,
251 const SkMaskFilter* mf,
252 const GrStyle& style,
253 bool pathIsMutable) {
Brian Salomon82f44312017-01-11 13:42:54 -0500254 draw_path_with_mask_filter(context, renderTargetContext, clip, std::move(paint), aa, viewMatrix,
Mike Reed80747ef2018-01-23 15:29:32 -0500255 as_MFB(mf), style, &path, pathIsMutable);
bsalomonc55271f2015-11-09 11:55:57 -0800256}
257
258void GrBlurUtils::drawPathWithMaskFilter(GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -0400259 GrRenderTargetContext* renderTargetContext,
bsalomonc55271f2015-11-09 11:55:57 -0800260 const GrClip& clip,
261 const SkPath& origPath,
robertphillipsccb1b572015-05-27 11:02:55 -0700262 const SkPaint& paint,
263 const SkMatrix& origViewMatrix,
264 const SkMatrix* prePathMatrix,
265 const SkIRect& clipBounds,
266 bool pathIsMutable) {
bsalomon6663acf2016-05-10 09:14:17 -0700267 SkASSERT(!pathIsMutable || origPath.isVolatile());
robertphillipsccb1b572015-05-27 11:02:55 -0700268
bsalomon6663acf2016-05-10 09:14:17 -0700269 GrStyle style(paint);
robertphillipsccb1b572015-05-27 11:02:55 -0700270 // If we have a prematrix, apply it to the path, optimizing for the case
271 // where the original path can in fact be modified in place (even though
272 // its parameter type is const).
bsalomon6663acf2016-05-10 09:14:17 -0700273
274 const SkPath* path = &origPath;
robertphillipsccb1b572015-05-27 11:02:55 -0700275 SkTLazy<SkPath> tmpPath;
robertphillipsccb1b572015-05-27 11:02:55 -0700276
277 SkMatrix viewMatrix = origViewMatrix;
278
279 if (prePathMatrix) {
bsalomon6663acf2016-05-10 09:14:17 -0700280 // Styling, blurs, and shading are supposed to be applied *after* the prePathMatrix.
281 if (!paint.getMaskFilter() && !paint.getShader() && !style.applies()) {
robertphillipsccb1b572015-05-27 11:02:55 -0700282 viewMatrix.preConcat(*prePathMatrix);
283 } else {
bsalomon6663acf2016-05-10 09:14:17 -0700284 SkPath* result = pathIsMutable ? const_cast<SkPath*>(path) : tmpPath.init();
285 pathIsMutable = true;
286 path->transform(*prePathMatrix, result);
287 path = result;
288 result->setIsVolatile(true);
robertphillipsccb1b572015-05-27 11:02:55 -0700289 }
290 }
291 // at this point we're done with prePathMatrix
292 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
293
294 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400295 if (!SkPaintToGrPaint(context, renderTargetContext->colorSpaceInfo(), paint, viewMatrix,
296 &grPaint)) {
robertphillipsccb1b572015-05-27 11:02:55 -0700297 return;
298 }
Chris Dalton3b51df12017-11-27 14:33:06 -0700299 GrAA aa = GrAA(paint.isAntiAlias());
Mike Reed80747ef2018-01-23 15:29:32 -0500300 SkMaskFilterBase* mf = as_MFB(paint.getMaskFilter());
Mike Reedbfadcf02018-01-20 22:24:21 +0000301 if (mf && !mf->hasFragmentProcessor()) {
Robert Phillipsa29a9562016-10-20 09:40:55 -0400302 // The MaskFilter wasn't already handled in SkPaintToGrPaint
Brian Salomon82f44312017-01-11 13:42:54 -0500303 draw_path_with_mask_filter(context, renderTargetContext, clip, std::move(grPaint), aa,
304 viewMatrix, mf, style, path, pathIsMutable);
bsalomonc55271f2015-11-09 11:55:57 -0800305 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500306 renderTargetContext->drawPath(clip, std::move(grPaint), aa, viewMatrix, *path, style);
robertphillipsccb1b572015-05-27 11:02:55 -0700307 }
robertphillipsccb1b572015-05-27 11:02:55 -0700308}