blob: 7a8a252cffe340ce7273b29d53e163b1a77299f0 [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"
robertphillipsccb1b572015-05-27 11:02:55 -070017#include "GrTexture.h"
robertphillipsd728f0c2016-11-21 11:05:03 -080018#include "GrTextureProxy.h"
robertphillipsccb1b572015-05-27 11:02:55 -070019#include "GrTextureProvider.h"
20#include "SkDraw.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070021#include "SkGrPriv.h"
robertphillipsccb1b572015-05-27 11:02:55 -070022#include "SkMaskFilter.h"
23#include "SkPaint.h"
csmartdaltonc6f411e2016-08-05 22:32:12 -070024#include "SkTLazy.h"
robertphillipsccb1b572015-05-27 11:02:55 -070025
26static bool clip_bounds_quick_reject(const SkIRect& clipBounds, const SkIRect& rect) {
27 return clipBounds.isEmpty() || rect.isEmpty() || !SkIRect::Intersects(clipBounds, rect);
28}
29
30// Draw a mask using the supplied paint. Since the coverage/geometry
31// is already burnt into the mask this boils down to a rect draw.
32// Return true if the mask was successfully drawn.
Brian Osman11052242016-10-27 14:47:55 -040033static bool draw_mask(GrRenderTargetContext* renderTargetContext,
Robert Phillips4a24da52016-12-14 09:00:07 -050034 GrTextureProvider* textureProvider,
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) {
Robert Phillips4a24da52016-12-14 09:00:07 -050040 // TODO: defer this instantiation
41 GrTexture* maskTex = mask->instantiate(textureProvider);
42 if (!maskTex) {
43 return false;
44 }
Brian Salomon82f44312017-01-11 13:42:54 -050045 SkMatrix inverse;
46 if (!viewMatrix.invert(&inverse)) {
47 return false;
48 }
Robert Phillips4a24da52016-12-14 09:00:07 -050049
Robert Phillips67c18d62017-01-20 12:44:06 -050050 SkMatrix matrix = SkMatrix::MakeTrans(-SkIntToScalar(maskRect.fLeft),
51 -SkIntToScalar(maskRect.fTop));
Brian Salomon2ebd0c82016-10-03 17:15:28 -040052 matrix.preConcat(viewMatrix);
Brian Salomon82f44312017-01-11 13:42:54 -050053 paint.addCoverageFragmentProcessor(GrSimpleTextureEffect::Make(maskTex, nullptr, matrix));
robertphillipsccb1b572015-05-27 11:02:55 -070054
Brian Salomon82f44312017-01-11 13:42:54 -050055 renderTargetContext->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050056 SkRect::Make(maskRect), inverse);
robertphillipsccb1b572015-05-27 11:02:55 -070057 return true;
58}
59
Robert Phillips4a24da52016-12-14 09:00:07 -050060static bool sw_draw_with_mask_filter(GrContext* context,
61 GrRenderTargetContext* renderTargetContext,
robertphillips0e7029e2015-11-30 05:45:06 -080062 const GrClip& clipData,
63 const SkMatrix& viewMatrix,
64 const SkPath& devPath,
65 const SkMaskFilter* filter,
66 const SkIRect& clipBounds,
Brian Salomon82f44312017-01-11 13:42:54 -050067 GrPaint&& paint,
bsalomon6663acf2016-05-10 09:14:17 -070068 SkStrokeRec::InitStyle fillOrHairline) {
robertphillipsccb1b572015-05-27 11:02:55 -070069 SkMask srcM, dstM;
robertphillipsccb1b572015-05-27 11:02:55 -070070 if (!SkDraw::DrawToMask(devPath, &clipBounds, filter, &viewMatrix, &srcM,
bsalomon6663acf2016-05-10 09:14:17 -070071 SkMask::kComputeBoundsAndRenderImage_CreateMode, fillOrHairline)) {
robertphillipsccb1b572015-05-27 11:02:55 -070072 return false;
73 }
74 SkAutoMaskFreeImage autoSrc(srcM.fImage);
75
halcanary96fcdcc2015-08-27 07:41:13 -070076 if (!filter->filterMask(&dstM, srcM, viewMatrix, nullptr)) {
robertphillipsccb1b572015-05-27 11:02:55 -070077 return false;
78 }
79 // this will free-up dstM when we're done (allocated in filterMask())
80 SkAutoMaskFreeImage autoDst(dstM.fImage);
81
82 if (clip_bounds_quick_reject(clipBounds, dstM.fBounds)) {
83 return false;
84 }
85
86 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
87 // the current clip (and identity matrix) and GrPaint settings
88 GrSurfaceDesc desc;
89 desc.fWidth = dstM.fBounds.width();
90 desc.fHeight = dstM.fBounds.height();
91 desc.fConfig = kAlpha_8_GrPixelConfig;
92
Robert Phillipsc949ce92017-01-19 16:59:04 -050093 sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeDeferredSurfaceContext(
94 desc,
95 SkBackingFit::kApprox,
96 SkBudgeted::kYes);
97 if (!sContext) {
Robert Phillips4a24da52016-12-14 09:00:07 -050098 return false;
99 }
100
Robert Phillipsc949ce92017-01-19 16:59:04 -0500101 SkImageInfo ii = SkImageInfo::MakeA8(desc.fWidth, desc.fHeight);
102 if (!sContext->writePixels(ii, dstM.fImage, dstM.fRowBytes, 0, 0)) {
robertphillipsccb1b572015-05-27 11:02:55 -0700103 return false;
104 }
robertphillipsccb1b572015-05-27 11:02:55 -0700105
Brian Salomon82f44312017-01-11 13:42:54 -0500106 return draw_mask(renderTargetContext, context->textureProvider(), clipData, viewMatrix,
Robert Phillipsc949ce92017-01-19 16:59:04 -0500107 dstM.fBounds, std::move(paint), sk_ref_sp(sContext->asDeferredTexture()));
robertphillipsccb1b572015-05-27 11:02:55 -0700108}
109
110// Create a mask of 'devPath' and place the result in 'mask'.
robertphillipsd728f0c2016-11-21 11:05:03 -0800111static sk_sp<GrTextureProxy> create_mask_GPU(GrContext* context,
112 const SkIRect& maskRect,
113 const SkPath& devPath,
114 SkStrokeRec::InitStyle fillOrHairline,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500115 GrAA aa,
robertphillipsd728f0c2016-11-21 11:05:03 -0800116 int sampleCnt) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500117 if (GrAA::kNo == aa) {
robertphillipsd4c741e2016-04-28 09:55:15 -0700118 // Don't need MSAA if mask isn't AA
119 sampleCnt = 0;
120 }
121
Robert Phillips55360b12016-12-05 13:18:47 +0000122 sk_sp<GrRenderTargetContext> rtContext(context->makeDeferredRenderTargetContextWithFallback(
Brian Osman11052242016-10-27 14:47:55 -0400123 SkBackingFit::kApprox, maskRect.width(), maskRect.height(), kAlpha_8_GrPixelConfig, nullptr,
124 sampleCnt));
robertphillipsd728f0c2016-11-21 11:05:03 -0800125 if (!rtContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700126 return nullptr;
robertphillipsccb1b572015-05-27 11:02:55 -0700127 }
128
Robert Phillips784b7bf2016-12-09 13:35:02 -0500129 rtContext->priv().absClear(nullptr, 0x0);
robertphillipsccb1b572015-05-27 11:02:55 -0700130
Brian Salomon82f44312017-01-11 13:42:54 -0500131 GrPaint maskPaint;
132 maskPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op);
robertphillipsccb1b572015-05-27 11:02:55 -0700133
134 // setup new clip
cdalton846c0512016-05-13 10:25:00 -0700135 const SkIRect clipRect = SkIRect::MakeWH(maskRect.width(), maskRect.height());
136 GrFixedClip clip(clipRect);
robertphillipsccb1b572015-05-27 11:02:55 -0700137
robertphillips3833daa2015-09-14 11:18:13 -0700138 // Draw the mask into maskTexture with the path's integerized top-left at
Brian Salomon82f44312017-01-11 13:42:54 -0500139 // the origin using maskPaint.
robertphillipsccb1b572015-05-27 11:02:55 -0700140 SkMatrix translate;
robertphillipsf054b172016-05-13 05:06:19 -0700141 translate.setTranslate(-SkIntToScalar(maskRect.fLeft), -SkIntToScalar(maskRect.fTop));
Brian Salomon82f44312017-01-11 13:42:54 -0500142 rtContext->drawPath(clip, std::move(maskPaint), aa, translate, devPath,
143 GrStyle(fillOrHairline));
robertphillipsd728f0c2016-11-21 11:05:03 -0800144 return sk_ref_sp(rtContext->asDeferredTexture());
robertphillipsccb1b572015-05-27 11:02:55 -0700145}
146
bsalomonc55271f2015-11-09 11:55:57 -0800147static void draw_path_with_mask_filter(GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -0400148 GrRenderTargetContext* renderTargetContext,
bsalomonc55271f2015-11-09 11:55:57 -0800149 const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500150 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500151 GrAA aa,
bsalomonc55271f2015-11-09 11:55:57 -0800152 const SkMatrix& viewMatrix,
153 const SkMaskFilter* maskFilter,
bsalomon6663acf2016-05-10 09:14:17 -0700154 const GrStyle& style,
155 const SkPath* path,
bsalomonc55271f2015-11-09 11:55:57 -0800156 bool pathIsMutable) {
157 SkASSERT(maskFilter);
158
159 SkIRect clipBounds;
Robert Phillips784b7bf2016-12-09 13:35:02 -0500160 clip.getConservativeBounds(renderTargetContext->width(),
161 renderTargetContext->height(),
Brian Osman11052242016-10-27 14:47:55 -0400162 &clipBounds);
bsalomonc55271f2015-11-09 11:55:57 -0800163 SkTLazy<SkPath> tmpPath;
bsalomon6663acf2016-05-10 09:14:17 -0700164 SkStrokeRec::InitStyle fillOrHairline;
bsalomonc55271f2015-11-09 11:55:57 -0800165
bsalomon6663acf2016-05-10 09:14:17 -0700166 // We just fully apply the style here.
167 if (style.applies()) {
senorblancob6a40b82016-08-19 08:07:22 -0700168 SkScalar scale = GrStyle::MatrixToScaleFactor(viewMatrix);
169 if (0 == scale || !style.applyToPath(tmpPath.init(), &fillOrHairline, *path, scale)) {
bsalomon6663acf2016-05-10 09:14:17 -0700170 return;
171 }
172 pathIsMutable = true;
173 path = tmpPath.get();
174 } else if (style.isSimpleHairline()) {
175 fillOrHairline = SkStrokeRec::kHairline_InitStyle;
bsalomon055e1922016-05-06 07:22:58 -0700176 } else {
bsalomon6663acf2016-05-10 09:14:17 -0700177 SkASSERT(style.isSimpleFill());
178 fillOrHairline = SkStrokeRec::kFill_InitStyle;
bsalomonc55271f2015-11-09 11:55:57 -0800179 }
180
181 // transform the path into device space
bsalomon6663acf2016-05-10 09:14:17 -0700182 if (!viewMatrix.isIdentity()) {
183 SkPath* result;
184 if (pathIsMutable) {
185 result = const_cast<SkPath*>(path);
186 } else {
187 if (!tmpPath.isValid()) {
188 tmpPath.init();
189 }
190 result = tmpPath.get();
191 }
192 path->transform(viewMatrix, result);
193 path = result;
194 result->setIsVolatile(true);
195 pathIsMutable = true;
196 }
bsalomonc55271f2015-11-09 11:55:57 -0800197
198 SkRect maskRect;
bsalomon6663acf2016-05-10 09:14:17 -0700199 if (maskFilter->canFilterMaskGPU(SkRRect::MakeRect(path->getBounds()),
bsalomonc55271f2015-11-09 11:55:57 -0800200 clipBounds,
201 viewMatrix,
202 &maskRect)) {
robertphillipsf054b172016-05-13 05:06:19 -0700203 // This mask will ultimately be drawn as a non-AA rect (see draw_mask).
204 // Non-AA rects have a bad habit of snapping arbitrarily. Integerize here
205 // so the mask draws in a reproducible manner.
bsalomonc55271f2015-11-09 11:55:57 -0800206 SkIRect finalIRect;
207 maskRect.roundOut(&finalIRect);
208 if (clip_bounds_quick_reject(clipBounds, finalIRect)) {
209 // clipped out
210 return;
211 }
212
213 if (maskFilter->directFilterMaskGPU(context->textureProvider(),
Brian Osman11052242016-10-27 14:47:55 -0400214 renderTargetContext,
Brian Salomon82f44312017-01-11 13:42:54 -0500215 std::move(paint),
bsalomonc55271f2015-11-09 11:55:57 -0800216 clip,
217 viewMatrix,
bsalomon6663acf2016-05-10 09:14:17 -0700218 SkStrokeRec(fillOrHairline),
219 *path)) {
bsalomonc55271f2015-11-09 11:55:57 -0800220 // the mask filter was able to draw itself directly, so there's nothing
221 // left to do.
222 return;
223 }
224
robertphillipsd728f0c2016-11-21 11:05:03 -0800225 sk_sp<GrTextureProxy> maskProxy(create_mask_GPU(context,
226 finalIRect,
227 *path,
228 fillOrHairline,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500229 aa,
robertphillipsd728f0c2016-11-21 11:05:03 -0800230 renderTargetContext->numColorSamples()));
231 if (maskProxy) {
Robert Phillips4a24da52016-12-14 09:00:07 -0500232 sk_sp<GrTextureProxy> filtered = maskFilter->filterMaskGPU(context,
233 std::move(maskProxy),
234 viewMatrix,
235 finalIRect);
236 if (filtered) {
Brian Salomon82f44312017-01-11 13:42:54 -0500237 if (draw_mask(renderTargetContext, context->textureProvider(), clip, viewMatrix,
238 finalIRect, std::move(paint), std::move(filtered))) {
bsalomonc55271f2015-11-09 11:55:57 -0800239 // This path is completely drawn
240 return;
241 }
242 }
243 }
244 }
245
Brian Salomon82f44312017-01-11 13:42:54 -0500246 sw_draw_with_mask_filter(context, renderTargetContext, clip, viewMatrix, *path, maskFilter,
247 clipBounds, std::move(paint), fillOrHairline);
bsalomon6663acf2016-05-10 09:14:17 -0700248}
249
250void GrBlurUtils::drawPathWithMaskFilter(GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -0400251 GrRenderTargetContext* renderTargetContext,
bsalomon6663acf2016-05-10 09:14:17 -0700252 const GrClip& clip,
253 const SkPath& path,
Brian Salomon82f44312017-01-11 13:42:54 -0500254 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500255 GrAA aa,
bsalomon6663acf2016-05-10 09:14:17 -0700256 const SkMatrix& viewMatrix,
257 const SkMaskFilter* mf,
258 const GrStyle& style,
259 bool pathIsMutable) {
Brian Salomon82f44312017-01-11 13:42:54 -0500260 draw_path_with_mask_filter(context, renderTargetContext, clip, std::move(paint), aa, viewMatrix,
261 mf, style, &path, pathIsMutable);
bsalomonc55271f2015-11-09 11:55:57 -0800262}
263
264void GrBlurUtils::drawPathWithMaskFilter(GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -0400265 GrRenderTargetContext* renderTargetContext,
bsalomonc55271f2015-11-09 11:55:57 -0800266 const GrClip& clip,
267 const SkPath& origPath,
robertphillipsccb1b572015-05-27 11:02:55 -0700268 const SkPaint& paint,
269 const SkMatrix& origViewMatrix,
270 const SkMatrix* prePathMatrix,
271 const SkIRect& clipBounds,
272 bool pathIsMutable) {
bsalomon6663acf2016-05-10 09:14:17 -0700273 SkASSERT(!pathIsMutable || origPath.isVolatile());
robertphillipsccb1b572015-05-27 11:02:55 -0700274
bsalomon6663acf2016-05-10 09:14:17 -0700275 GrStyle style(paint);
robertphillipsccb1b572015-05-27 11:02:55 -0700276 // If we have a prematrix, apply it to the path, optimizing for the case
277 // where the original path can in fact be modified in place (even though
278 // its parameter type is const).
bsalomon6663acf2016-05-10 09:14:17 -0700279
280 const SkPath* path = &origPath;
robertphillipsccb1b572015-05-27 11:02:55 -0700281 SkTLazy<SkPath> tmpPath;
robertphillipsccb1b572015-05-27 11:02:55 -0700282
283 SkMatrix viewMatrix = origViewMatrix;
284
285 if (prePathMatrix) {
bsalomon6663acf2016-05-10 09:14:17 -0700286 // Styling, blurs, and shading are supposed to be applied *after* the prePathMatrix.
287 if (!paint.getMaskFilter() && !paint.getShader() && !style.applies()) {
robertphillipsccb1b572015-05-27 11:02:55 -0700288 viewMatrix.preConcat(*prePathMatrix);
289 } else {
bsalomon6663acf2016-05-10 09:14:17 -0700290 SkPath* result = pathIsMutable ? const_cast<SkPath*>(path) : tmpPath.init();
291 pathIsMutable = true;
292 path->transform(*prePathMatrix, result);
293 path = result;
294 result->setIsVolatile(true);
robertphillipsccb1b572015-05-27 11:02:55 -0700295 }
296 }
297 // at this point we're done with prePathMatrix
298 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
299
300 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400301 if (!SkPaintToGrPaint(context, renderTargetContext, paint, viewMatrix, &grPaint)) {
robertphillipsccb1b572015-05-27 11:02:55 -0700302 return;
303 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500304 GrAA aa = GrBoolToAA(paint.isAntiAlias());
Robert Phillipsa29a9562016-10-20 09:40:55 -0400305 SkMaskFilter* mf = paint.getMaskFilter();
306 if (mf && !mf->asFragmentProcessor(nullptr, nullptr, viewMatrix)) {
307 // The MaskFilter wasn't already handled in SkPaintToGrPaint
Brian Salomon82f44312017-01-11 13:42:54 -0500308 draw_path_with_mask_filter(context, renderTargetContext, clip, std::move(grPaint), aa,
309 viewMatrix, mf, style, path, pathIsMutable);
bsalomonc55271f2015-11-09 11:55:57 -0800310 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500311 renderTargetContext->drawPath(clip, std::move(grPaint), aa, viewMatrix, *path, style);
robertphillipsccb1b572015-05-27 11:02:55 -0700312 }
robertphillipsccb1b572015-05-27 11:02:55 -0700313}