blob: 76443ff938a9497036e495187987dd5de3c15613 [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"
Brian Osman32342f02017-03-04 08:12:46 -050015#include "GrResourceProvider.h"
robertphillipsccb1b572015-05-27 11:02:55 -070016#include "effects/GrSimpleTextureEffect.h"
bsalomon6663acf2016-05-10 09:14:17 -070017#include "GrStyle.h"
robertphillipsd728f0c2016-11-21 11:05:03 -080018#include "GrTextureProxy.h"
robertphillipsccb1b572015-05-27 11:02:55 -070019#include "SkDraw.h"
Brian Osman3b655982017-03-07 16:58:08 -050020#include "SkGr.h"
robertphillipsccb1b572015-05-27 11:02:55 -070021#include "SkMaskFilter.h"
22#include "SkPaint.h"
csmartdaltonc6f411e2016-08-05 22:32:12 -070023#include "SkTLazy.h"
robertphillipsccb1b572015-05-27 11:02:55 -070024
25static bool clip_bounds_quick_reject(const SkIRect& clipBounds, const SkIRect& rect) {
26 return clipBounds.isEmpty() || rect.isEmpty() || !SkIRect::Intersects(clipBounds, rect);
27}
28
29// Draw a mask using the supplied paint. Since the coverage/geometry
30// is already burnt into the mask this boils down to a rect draw.
31// Return true if the mask was successfully drawn.
Robert Phillips296b1cc2017-03-15 10:42:12 -040032static bool draw_mask(GrRenderTargetContext* renderTargetContext,
robertphillipsccb1b572015-05-27 11:02:55 -070033 const GrClip& clip,
34 const SkMatrix& viewMatrix,
robertphillipsf054b172016-05-13 05:06:19 -070035 const SkIRect& maskRect,
Brian Salomon82f44312017-01-11 13:42:54 -050036 GrPaint&& paint,
Robert Phillips4a24da52016-12-14 09:00:07 -050037 sk_sp<GrTextureProxy> mask) {
Brian Salomon82f44312017-01-11 13:42:54 -050038 SkMatrix inverse;
39 if (!viewMatrix.invert(&inverse)) {
40 return false;
41 }
Robert Phillips4a24da52016-12-14 09:00:07 -050042
Robert Phillips296b1cc2017-03-15 10:42:12 -040043 GrResourceProvider* resourceProvider = renderTargetContext->resourceProvider();
44
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);
Robert Phillips296b1cc2017-03-15 10:42:12 -040048 paint.addCoverageFragmentProcessor(GrSimpleTextureEffect::Make(resourceProvider,
49 std::move(mask),
Robert Phillips40fd7c92017-01-30 08:06:27 -050050 nullptr, matrix));
robertphillipsccb1b572015-05-27 11:02:55 -070051
Brian Salomon82f44312017-01-11 13:42:54 -050052 renderTargetContext->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050053 SkRect::Make(maskRect), inverse);
robertphillipsccb1b572015-05-27 11:02:55 -070054 return true;
55}
56
Robert Phillips4a24da52016-12-14 09:00:07 -050057static bool sw_draw_with_mask_filter(GrContext* context,
58 GrRenderTargetContext* renderTargetContext,
robertphillips0e7029e2015-11-30 05:45:06 -080059 const GrClip& clipData,
60 const SkMatrix& viewMatrix,
61 const SkPath& devPath,
62 const SkMaskFilter* filter,
63 const SkIRect& clipBounds,
Brian Salomon82f44312017-01-11 13:42:54 -050064 GrPaint&& paint,
bsalomon6663acf2016-05-10 09:14:17 -070065 SkStrokeRec::InitStyle fillOrHairline) {
robertphillipsccb1b572015-05-27 11:02:55 -070066 SkMask srcM, dstM;
robertphillipsccb1b572015-05-27 11:02:55 -070067 if (!SkDraw::DrawToMask(devPath, &clipBounds, filter, &viewMatrix, &srcM,
bsalomon6663acf2016-05-10 09:14:17 -070068 SkMask::kComputeBoundsAndRenderImage_CreateMode, fillOrHairline)) {
robertphillipsccb1b572015-05-27 11:02:55 -070069 return false;
70 }
71 SkAutoMaskFreeImage autoSrc(srcM.fImage);
72
halcanary96fcdcc2015-08-27 07:41:13 -070073 if (!filter->filterMask(&dstM, srcM, viewMatrix, nullptr)) {
robertphillipsccb1b572015-05-27 11:02:55 -070074 return false;
75 }
76 // this will free-up dstM when we're done (allocated in filterMask())
77 SkAutoMaskFreeImage autoDst(dstM.fImage);
78
79 if (clip_bounds_quick_reject(clipBounds, dstM.fBounds)) {
80 return false;
81 }
82
83 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
84 // the current clip (and identity matrix) and GrPaint settings
85 GrSurfaceDesc desc;
Robert Phillips40fd7c92017-01-30 08:06:27 -050086 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
robertphillipsccb1b572015-05-27 11:02:55 -070087 desc.fWidth = dstM.fBounds.width();
88 desc.fHeight = dstM.fBounds.height();
89 desc.fConfig = kAlpha_8_GrPixelConfig;
90
Robert Phillipsc949ce92017-01-19 16:59:04 -050091 sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeDeferredSurfaceContext(
92 desc,
93 SkBackingFit::kApprox,
94 SkBudgeted::kYes);
95 if (!sContext) {
Robert Phillips4a24da52016-12-14 09:00:07 -050096 return false;
97 }
98
Robert Phillipsc949ce92017-01-19 16:59:04 -050099 SkImageInfo ii = SkImageInfo::MakeA8(desc.fWidth, desc.fHeight);
100 if (!sContext->writePixels(ii, dstM.fImage, dstM.fRowBytes, 0, 0)) {
robertphillipsccb1b572015-05-27 11:02:55 -0700101 return false;
102 }
robertphillipsccb1b572015-05-27 11:02:55 -0700103
Robert Phillips296b1cc2017-03-15 10:42:12 -0400104 return draw_mask(renderTargetContext, clipData, viewMatrix,
Robert Phillipsf200a902017-01-30 13:27:37 -0500105 dstM.fBounds, std::move(paint), sContext->asTextureProxyRef());
robertphillipsccb1b572015-05-27 11:02:55 -0700106}
107
108// Create a mask of 'devPath' and place the result in 'mask'.
robertphillipsd728f0c2016-11-21 11:05:03 -0800109static sk_sp<GrTextureProxy> create_mask_GPU(GrContext* context,
110 const SkIRect& maskRect,
111 const SkPath& devPath,
112 SkStrokeRec::InitStyle fillOrHairline,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500113 GrAA aa,
robertphillipsd728f0c2016-11-21 11:05:03 -0800114 int sampleCnt) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500115 if (GrAA::kNo == aa) {
robertphillipsd4c741e2016-04-28 09:55:15 -0700116 // Don't need MSAA if mask isn't AA
117 sampleCnt = 0;
118 }
119
Robert Phillips55360b12016-12-05 13:18:47 +0000120 sk_sp<GrRenderTargetContext> rtContext(context->makeDeferredRenderTargetContextWithFallback(
Brian Osman11052242016-10-27 14:47:55 -0400121 SkBackingFit::kApprox, maskRect.width(), maskRect.height(), kAlpha_8_GrPixelConfig, nullptr,
122 sampleCnt));
robertphillipsd728f0c2016-11-21 11:05:03 -0800123 if (!rtContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700124 return nullptr;
robertphillipsccb1b572015-05-27 11:02:55 -0700125 }
126
Robert Phillips784b7bf2016-12-09 13:35:02 -0500127 rtContext->priv().absClear(nullptr, 0x0);
robertphillipsccb1b572015-05-27 11:02:55 -0700128
Brian Salomon82f44312017-01-11 13:42:54 -0500129 GrPaint maskPaint;
130 maskPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op);
robertphillipsccb1b572015-05-27 11:02:55 -0700131
132 // setup new clip
cdalton846c0512016-05-13 10:25:00 -0700133 const SkIRect clipRect = SkIRect::MakeWH(maskRect.width(), maskRect.height());
134 GrFixedClip clip(clipRect);
robertphillipsccb1b572015-05-27 11:02:55 -0700135
robertphillips3833daa2015-09-14 11:18:13 -0700136 // Draw the mask into maskTexture with the path's integerized top-left at
Brian Salomon82f44312017-01-11 13:42:54 -0500137 // the origin using maskPaint.
robertphillipsccb1b572015-05-27 11:02:55 -0700138 SkMatrix translate;
robertphillipsf054b172016-05-13 05:06:19 -0700139 translate.setTranslate(-SkIntToScalar(maskRect.fLeft), -SkIntToScalar(maskRect.fTop));
Brian Salomon82f44312017-01-11 13:42:54 -0500140 rtContext->drawPath(clip, std::move(maskPaint), aa, translate, devPath,
141 GrStyle(fillOrHairline));
Robert Phillipsf200a902017-01-30 13:27:37 -0500142 return rtContext->asTextureProxyRef();
robertphillipsccb1b572015-05-27 11:02:55 -0700143}
144
bsalomonc55271f2015-11-09 11:55:57 -0800145static void draw_path_with_mask_filter(GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -0400146 GrRenderTargetContext* renderTargetContext,
bsalomonc55271f2015-11-09 11:55:57 -0800147 const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500148 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500149 GrAA aa,
bsalomonc55271f2015-11-09 11:55:57 -0800150 const SkMatrix& viewMatrix,
151 const SkMaskFilter* maskFilter,
bsalomon6663acf2016-05-10 09:14:17 -0700152 const GrStyle& style,
153 const SkPath* path,
bsalomonc55271f2015-11-09 11:55:57 -0800154 bool pathIsMutable) {
155 SkASSERT(maskFilter);
156
157 SkIRect clipBounds;
Robert Phillips784b7bf2016-12-09 13:35:02 -0500158 clip.getConservativeBounds(renderTargetContext->width(),
159 renderTargetContext->height(),
Brian Osman11052242016-10-27 14:47:55 -0400160 &clipBounds);
bsalomonc55271f2015-11-09 11:55:57 -0800161 SkTLazy<SkPath> tmpPath;
bsalomon6663acf2016-05-10 09:14:17 -0700162 SkStrokeRec::InitStyle fillOrHairline;
bsalomonc55271f2015-11-09 11:55:57 -0800163
bsalomon6663acf2016-05-10 09:14:17 -0700164 // We just fully apply the style here.
165 if (style.applies()) {
senorblancob6a40b82016-08-19 08:07:22 -0700166 SkScalar scale = GrStyle::MatrixToScaleFactor(viewMatrix);
167 if (0 == scale || !style.applyToPath(tmpPath.init(), &fillOrHairline, *path, scale)) {
bsalomon6663acf2016-05-10 09:14:17 -0700168 return;
169 }
170 pathIsMutable = true;
171 path = tmpPath.get();
172 } else if (style.isSimpleHairline()) {
173 fillOrHairline = SkStrokeRec::kHairline_InitStyle;
bsalomon055e1922016-05-06 07:22:58 -0700174 } else {
bsalomon6663acf2016-05-10 09:14:17 -0700175 SkASSERT(style.isSimpleFill());
176 fillOrHairline = SkStrokeRec::kFill_InitStyle;
bsalomonc55271f2015-11-09 11:55:57 -0800177 }
178
179 // transform the path into device space
bsalomon6663acf2016-05-10 09:14:17 -0700180 if (!viewMatrix.isIdentity()) {
181 SkPath* result;
182 if (pathIsMutable) {
183 result = const_cast<SkPath*>(path);
184 } else {
185 if (!tmpPath.isValid()) {
186 tmpPath.init();
187 }
188 result = tmpPath.get();
189 }
190 path->transform(viewMatrix, result);
191 path = result;
192 result->setIsVolatile(true);
193 pathIsMutable = true;
194 }
bsalomonc55271f2015-11-09 11:55:57 -0800195
196 SkRect maskRect;
bsalomon6663acf2016-05-10 09:14:17 -0700197 if (maskFilter->canFilterMaskGPU(SkRRect::MakeRect(path->getBounds()),
bsalomonc55271f2015-11-09 11:55:57 -0800198 clipBounds,
199 viewMatrix,
200 &maskRect)) {
robertphillipsf054b172016-05-13 05:06:19 -0700201 // This mask will ultimately be drawn as a non-AA rect (see draw_mask).
202 // Non-AA rects have a bad habit of snapping arbitrarily. Integerize here
203 // so the mask draws in a reproducible manner.
bsalomonc55271f2015-11-09 11:55:57 -0800204 SkIRect finalIRect;
205 maskRect.roundOut(&finalIRect);
206 if (clip_bounds_quick_reject(clipBounds, finalIRect)) {
207 // clipped out
208 return;
209 }
210
Robert Phillipsd3749482017-03-14 09:17:43 -0400211 if (maskFilter->directFilterMaskGPU(context,
Brian Osman11052242016-10-27 14:47:55 -0400212 renderTargetContext,
Brian Salomon82f44312017-01-11 13:42:54 -0500213 std::move(paint),
bsalomonc55271f2015-11-09 11:55:57 -0800214 clip,
215 viewMatrix,
bsalomon6663acf2016-05-10 09:14:17 -0700216 SkStrokeRec(fillOrHairline),
217 *path)) {
bsalomonc55271f2015-11-09 11:55:57 -0800218 // the mask filter was able to draw itself directly, so there's nothing
219 // left to do.
220 return;
221 }
222
robertphillipsd728f0c2016-11-21 11:05:03 -0800223 sk_sp<GrTextureProxy> maskProxy(create_mask_GPU(context,
224 finalIRect,
225 *path,
226 fillOrHairline,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500227 aa,
robertphillipsd728f0c2016-11-21 11:05:03 -0800228 renderTargetContext->numColorSamples()));
229 if (maskProxy) {
Robert Phillips4a24da52016-12-14 09:00:07 -0500230 sk_sp<GrTextureProxy> filtered = maskFilter->filterMaskGPU(context,
231 std::move(maskProxy),
232 viewMatrix,
233 finalIRect);
234 if (filtered) {
Robert Phillips296b1cc2017-03-15 10:42:12 -0400235 if (draw_mask(renderTargetContext, clip, viewMatrix,
Brian Salomon82f44312017-01-11 13:42:54 -0500236 finalIRect, std::move(paint), std::move(filtered))) {
bsalomonc55271f2015-11-09 11:55:57 -0800237 // This path is completely drawn
238 return;
239 }
240 }
241 }
242 }
243
Brian Salomon82f44312017-01-11 13:42:54 -0500244 sw_draw_with_mask_filter(context, renderTargetContext, clip, viewMatrix, *path, maskFilter,
245 clipBounds, std::move(paint), fillOrHairline);
bsalomon6663acf2016-05-10 09:14:17 -0700246}
247
248void GrBlurUtils::drawPathWithMaskFilter(GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -0400249 GrRenderTargetContext* renderTargetContext,
bsalomon6663acf2016-05-10 09:14:17 -0700250 const GrClip& clip,
251 const SkPath& path,
Brian Salomon82f44312017-01-11 13:42:54 -0500252 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500253 GrAA aa,
bsalomon6663acf2016-05-10 09:14:17 -0700254 const SkMatrix& viewMatrix,
255 const SkMaskFilter* mf,
256 const GrStyle& style,
257 bool pathIsMutable) {
Brian Salomon82f44312017-01-11 13:42:54 -0500258 draw_path_with_mask_filter(context, renderTargetContext, clip, std::move(paint), aa, viewMatrix,
259 mf, style, &path, pathIsMutable);
bsalomonc55271f2015-11-09 11:55:57 -0800260}
261
262void GrBlurUtils::drawPathWithMaskFilter(GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -0400263 GrRenderTargetContext* renderTargetContext,
bsalomonc55271f2015-11-09 11:55:57 -0800264 const GrClip& clip,
265 const SkPath& origPath,
robertphillipsccb1b572015-05-27 11:02:55 -0700266 const SkPaint& paint,
267 const SkMatrix& origViewMatrix,
268 const SkMatrix* prePathMatrix,
269 const SkIRect& clipBounds,
270 bool pathIsMutable) {
bsalomon6663acf2016-05-10 09:14:17 -0700271 SkASSERT(!pathIsMutable || origPath.isVolatile());
robertphillipsccb1b572015-05-27 11:02:55 -0700272
bsalomon6663acf2016-05-10 09:14:17 -0700273 GrStyle style(paint);
robertphillipsccb1b572015-05-27 11:02:55 -0700274 // If we have a prematrix, apply it to the path, optimizing for the case
275 // where the original path can in fact be modified in place (even though
276 // its parameter type is const).
bsalomon6663acf2016-05-10 09:14:17 -0700277
278 const SkPath* path = &origPath;
robertphillipsccb1b572015-05-27 11:02:55 -0700279 SkTLazy<SkPath> tmpPath;
robertphillipsccb1b572015-05-27 11:02:55 -0700280
281 SkMatrix viewMatrix = origViewMatrix;
282
283 if (prePathMatrix) {
bsalomon6663acf2016-05-10 09:14:17 -0700284 // Styling, blurs, and shading are supposed to be applied *after* the prePathMatrix.
285 if (!paint.getMaskFilter() && !paint.getShader() && !style.applies()) {
robertphillipsccb1b572015-05-27 11:02:55 -0700286 viewMatrix.preConcat(*prePathMatrix);
287 } else {
bsalomon6663acf2016-05-10 09:14:17 -0700288 SkPath* result = pathIsMutable ? const_cast<SkPath*>(path) : tmpPath.init();
289 pathIsMutable = true;
290 path->transform(*prePathMatrix, result);
291 path = result;
292 result->setIsVolatile(true);
robertphillipsccb1b572015-05-27 11:02:55 -0700293 }
294 }
295 // at this point we're done with prePathMatrix
296 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
297
298 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400299 if (!SkPaintToGrPaint(context, renderTargetContext, paint, viewMatrix, &grPaint)) {
robertphillipsccb1b572015-05-27 11:02:55 -0700300 return;
301 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500302 GrAA aa = GrBoolToAA(paint.isAntiAlias());
Robert Phillipsa29a9562016-10-20 09:40:55 -0400303 SkMaskFilter* mf = paint.getMaskFilter();
Robert Phillipsd9d84852017-06-09 10:48:29 -0400304 if (mf && !mf->asFragmentProcessor(nullptr)) {
Robert Phillipsa29a9562016-10-20 09:40:55 -0400305 // The MaskFilter wasn't already handled in SkPaintToGrPaint
Brian Salomon82f44312017-01-11 13:42:54 -0500306 draw_path_with_mask_filter(context, renderTargetContext, clip, std::move(grPaint), aa,
307 viewMatrix, mf, style, path, pathIsMutable);
bsalomonc55271f2015-11-09 11:55:57 -0800308 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500309 renderTargetContext->drawPath(clip, std::move(grPaint), aa, viewMatrix, *path, style);
robertphillipsccb1b572015-05-27 11:02:55 -0700310 }
robertphillipsccb1b572015-05-27 11:02:55 -0700311}