blob: 68fd9f3899ff57ed76f126368f27f827b0c782a5 [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"
robertphillipsccb1b572015-05-27 11:02:55 -070020#include "SkMaskFilter.h"
21#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
halcanary96fcdcc2015-08-27 07:41:13 -070068 if (!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,
88 SkBackingFit::kApprox,
89 SkBudgeted::kYes);
90 if (!sContext) {
Robert Phillips4a24da52016-12-14 09:00:07 -050091 return false;
92 }
93
Robert Phillipsc949ce92017-01-19 16:59:04 -050094 SkImageInfo ii = SkImageInfo::MakeA8(desc.fWidth, desc.fHeight);
95 if (!sContext->writePixels(ii, dstM.fImage, dstM.fRowBytes, 0, 0)) {
robertphillipsccb1b572015-05-27 11:02:55 -070096 return false;
97 }
robertphillipsccb1b572015-05-27 11:02:55 -070098
Robert Phillips296b1cc2017-03-15 10:42:12 -040099 return draw_mask(renderTargetContext, clipData, viewMatrix,
Robert Phillipsf200a902017-01-30 13:27:37 -0500100 dstM.fBounds, std::move(paint), sContext->asTextureProxyRef());
robertphillipsccb1b572015-05-27 11:02:55 -0700101}
102
103// Create a mask of 'devPath' and place the result in 'mask'.
robertphillipsd728f0c2016-11-21 11:05:03 -0800104static sk_sp<GrTextureProxy> create_mask_GPU(GrContext* context,
105 const SkIRect& maskRect,
106 const SkPath& devPath,
107 SkStrokeRec::InitStyle fillOrHairline,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500108 GrAA aa,
robertphillipsd728f0c2016-11-21 11:05:03 -0800109 int sampleCnt) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500110 if (GrAA::kNo == aa) {
robertphillipsd4c741e2016-04-28 09:55:15 -0700111 // Don't need MSAA if mask isn't AA
112 sampleCnt = 0;
113 }
114
Robert Phillips55360b12016-12-05 13:18:47 +0000115 sk_sp<GrRenderTargetContext> rtContext(context->makeDeferredRenderTargetContextWithFallback(
Brian Osman11052242016-10-27 14:47:55 -0400116 SkBackingFit::kApprox, maskRect.width(), maskRect.height(), kAlpha_8_GrPixelConfig, nullptr,
117 sampleCnt));
robertphillipsd728f0c2016-11-21 11:05:03 -0800118 if (!rtContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700119 return nullptr;
robertphillipsccb1b572015-05-27 11:02:55 -0700120 }
121
Robert Phillips784b7bf2016-12-09 13:35:02 -0500122 rtContext->priv().absClear(nullptr, 0x0);
robertphillipsccb1b572015-05-27 11:02:55 -0700123
Brian Salomon82f44312017-01-11 13:42:54 -0500124 GrPaint maskPaint;
125 maskPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op);
robertphillipsccb1b572015-05-27 11:02:55 -0700126
127 // setup new clip
cdalton846c0512016-05-13 10:25:00 -0700128 const SkIRect clipRect = SkIRect::MakeWH(maskRect.width(), maskRect.height());
129 GrFixedClip clip(clipRect);
robertphillipsccb1b572015-05-27 11:02:55 -0700130
robertphillips3833daa2015-09-14 11:18:13 -0700131 // Draw the mask into maskTexture with the path's integerized top-left at
Brian Salomon82f44312017-01-11 13:42:54 -0500132 // the origin using maskPaint.
robertphillipsccb1b572015-05-27 11:02:55 -0700133 SkMatrix translate;
robertphillipsf054b172016-05-13 05:06:19 -0700134 translate.setTranslate(-SkIntToScalar(maskRect.fLeft), -SkIntToScalar(maskRect.fTop));
Brian Salomon82f44312017-01-11 13:42:54 -0500135 rtContext->drawPath(clip, std::move(maskPaint), aa, translate, devPath,
136 GrStyle(fillOrHairline));
Robert Phillipsf200a902017-01-30 13:27:37 -0500137 return rtContext->asTextureProxyRef();
robertphillipsccb1b572015-05-27 11:02:55 -0700138}
139
bsalomonc55271f2015-11-09 11:55:57 -0800140static void draw_path_with_mask_filter(GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -0400141 GrRenderTargetContext* renderTargetContext,
bsalomonc55271f2015-11-09 11:55:57 -0800142 const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500143 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500144 GrAA aa,
bsalomonc55271f2015-11-09 11:55:57 -0800145 const SkMatrix& viewMatrix,
146 const SkMaskFilter* maskFilter,
bsalomon6663acf2016-05-10 09:14:17 -0700147 const GrStyle& style,
148 const SkPath* path,
bsalomonc55271f2015-11-09 11:55:57 -0800149 bool pathIsMutable) {
150 SkASSERT(maskFilter);
151
152 SkIRect clipBounds;
Robert Phillips784b7bf2016-12-09 13:35:02 -0500153 clip.getConservativeBounds(renderTargetContext->width(),
154 renderTargetContext->height(),
Brian Osman11052242016-10-27 14:47:55 -0400155 &clipBounds);
bsalomonc55271f2015-11-09 11:55:57 -0800156 SkTLazy<SkPath> tmpPath;
bsalomon6663acf2016-05-10 09:14:17 -0700157 SkStrokeRec::InitStyle fillOrHairline;
bsalomonc55271f2015-11-09 11:55:57 -0800158
bsalomon6663acf2016-05-10 09:14:17 -0700159 // We just fully apply the style here.
160 if (style.applies()) {
senorblancob6a40b82016-08-19 08:07:22 -0700161 SkScalar scale = GrStyle::MatrixToScaleFactor(viewMatrix);
162 if (0 == scale || !style.applyToPath(tmpPath.init(), &fillOrHairline, *path, scale)) {
bsalomon6663acf2016-05-10 09:14:17 -0700163 return;
164 }
165 pathIsMutable = true;
166 path = tmpPath.get();
167 } else if (style.isSimpleHairline()) {
168 fillOrHairline = SkStrokeRec::kHairline_InitStyle;
bsalomon055e1922016-05-06 07:22:58 -0700169 } else {
bsalomon6663acf2016-05-10 09:14:17 -0700170 SkASSERT(style.isSimpleFill());
171 fillOrHairline = SkStrokeRec::kFill_InitStyle;
bsalomonc55271f2015-11-09 11:55:57 -0800172 }
173
174 // transform the path into device space
bsalomon6663acf2016-05-10 09:14:17 -0700175 if (!viewMatrix.isIdentity()) {
176 SkPath* result;
177 if (pathIsMutable) {
178 result = const_cast<SkPath*>(path);
179 } else {
180 if (!tmpPath.isValid()) {
181 tmpPath.init();
182 }
183 result = tmpPath.get();
184 }
185 path->transform(viewMatrix, result);
186 path = result;
187 result->setIsVolatile(true);
188 pathIsMutable = true;
189 }
bsalomonc55271f2015-11-09 11:55:57 -0800190
191 SkRect maskRect;
bsalomon6663acf2016-05-10 09:14:17 -0700192 if (maskFilter->canFilterMaskGPU(SkRRect::MakeRect(path->getBounds()),
bsalomonc55271f2015-11-09 11:55:57 -0800193 clipBounds,
194 viewMatrix,
195 &maskRect)) {
robertphillipsf054b172016-05-13 05:06:19 -0700196 // This mask will ultimately be drawn as a non-AA rect (see draw_mask).
197 // Non-AA rects have a bad habit of snapping arbitrarily. Integerize here
198 // so the mask draws in a reproducible manner.
bsalomonc55271f2015-11-09 11:55:57 -0800199 SkIRect finalIRect;
200 maskRect.roundOut(&finalIRect);
201 if (clip_bounds_quick_reject(clipBounds, finalIRect)) {
202 // clipped out
203 return;
204 }
205
Robert Phillipsd3749482017-03-14 09:17:43 -0400206 if (maskFilter->directFilterMaskGPU(context,
Brian Osman11052242016-10-27 14:47:55 -0400207 renderTargetContext,
Brian Salomon82f44312017-01-11 13:42:54 -0500208 std::move(paint),
bsalomonc55271f2015-11-09 11:55:57 -0800209 clip,
210 viewMatrix,
bsalomon6663acf2016-05-10 09:14:17 -0700211 SkStrokeRec(fillOrHairline),
212 *path)) {
bsalomonc55271f2015-11-09 11:55:57 -0800213 // the mask filter was able to draw itself directly, so there's nothing
214 // left to do.
215 return;
216 }
217
robertphillipsd728f0c2016-11-21 11:05:03 -0800218 sk_sp<GrTextureProxy> maskProxy(create_mask_GPU(context,
219 finalIRect,
220 *path,
221 fillOrHairline,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500222 aa,
robertphillipsd728f0c2016-11-21 11:05:03 -0800223 renderTargetContext->numColorSamples()));
224 if (maskProxy) {
Robert Phillips4a24da52016-12-14 09:00:07 -0500225 sk_sp<GrTextureProxy> filtered = maskFilter->filterMaskGPU(context,
226 std::move(maskProxy),
227 viewMatrix,
228 finalIRect);
229 if (filtered) {
Robert Phillips296b1cc2017-03-15 10:42:12 -0400230 if (draw_mask(renderTargetContext, clip, viewMatrix,
Brian Salomon82f44312017-01-11 13:42:54 -0500231 finalIRect, std::move(paint), std::move(filtered))) {
bsalomonc55271f2015-11-09 11:55:57 -0800232 // This path is completely drawn
233 return;
234 }
235 }
236 }
237 }
238
Brian Salomon82f44312017-01-11 13:42:54 -0500239 sw_draw_with_mask_filter(context, renderTargetContext, clip, viewMatrix, *path, maskFilter,
240 clipBounds, std::move(paint), fillOrHairline);
bsalomon6663acf2016-05-10 09:14:17 -0700241}
242
243void GrBlurUtils::drawPathWithMaskFilter(GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -0400244 GrRenderTargetContext* renderTargetContext,
bsalomon6663acf2016-05-10 09:14:17 -0700245 const GrClip& clip,
246 const SkPath& path,
Brian Salomon82f44312017-01-11 13:42:54 -0500247 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500248 GrAA aa,
bsalomon6663acf2016-05-10 09:14:17 -0700249 const SkMatrix& viewMatrix,
250 const SkMaskFilter* mf,
251 const GrStyle& style,
252 bool pathIsMutable) {
Brian Salomon82f44312017-01-11 13:42:54 -0500253 draw_path_with_mask_filter(context, renderTargetContext, clip, std::move(paint), aa, viewMatrix,
254 mf, style, &path, pathIsMutable);
bsalomonc55271f2015-11-09 11:55:57 -0800255}
256
257void GrBlurUtils::drawPathWithMaskFilter(GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -0400258 GrRenderTargetContext* renderTargetContext,
bsalomonc55271f2015-11-09 11:55:57 -0800259 const GrClip& clip,
260 const SkPath& origPath,
robertphillipsccb1b572015-05-27 11:02:55 -0700261 const SkPaint& paint,
262 const SkMatrix& origViewMatrix,
263 const SkMatrix* prePathMatrix,
264 const SkIRect& clipBounds,
265 bool pathIsMutable) {
bsalomon6663acf2016-05-10 09:14:17 -0700266 SkASSERT(!pathIsMutable || origPath.isVolatile());
robertphillipsccb1b572015-05-27 11:02:55 -0700267
bsalomon6663acf2016-05-10 09:14:17 -0700268 GrStyle style(paint);
robertphillipsccb1b572015-05-27 11:02:55 -0700269 // If we have a prematrix, apply it to the path, optimizing for the case
270 // where the original path can in fact be modified in place (even though
271 // its parameter type is const).
bsalomon6663acf2016-05-10 09:14:17 -0700272
273 const SkPath* path = &origPath;
robertphillipsccb1b572015-05-27 11:02:55 -0700274 SkTLazy<SkPath> tmpPath;
robertphillipsccb1b572015-05-27 11:02:55 -0700275
276 SkMatrix viewMatrix = origViewMatrix;
277
278 if (prePathMatrix) {
bsalomon6663acf2016-05-10 09:14:17 -0700279 // Styling, blurs, and shading are supposed to be applied *after* the prePathMatrix.
280 if (!paint.getMaskFilter() && !paint.getShader() && !style.applies()) {
robertphillipsccb1b572015-05-27 11:02:55 -0700281 viewMatrix.preConcat(*prePathMatrix);
282 } else {
bsalomon6663acf2016-05-10 09:14:17 -0700283 SkPath* result = pathIsMutable ? const_cast<SkPath*>(path) : tmpPath.init();
284 pathIsMutable = true;
285 path->transform(*prePathMatrix, result);
286 path = result;
287 result->setIsVolatile(true);
robertphillipsccb1b572015-05-27 11:02:55 -0700288 }
289 }
290 // at this point we're done with prePathMatrix
291 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
292
293 GrPaint grPaint;
Brian Salomonf3569f02017-10-24 12:52:33 -0400294 if (!SkPaintToGrPaint(context, renderTargetContext->colorSpaceInfo(), paint, viewMatrix,
295 &grPaint)) {
robertphillipsccb1b572015-05-27 11:02:55 -0700296 return;
297 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500298 GrAA aa = GrBoolToAA(paint.isAntiAlias());
Robert Phillipsa29a9562016-10-20 09:40:55 -0400299 SkMaskFilter* mf = paint.getMaskFilter();
Robert Phillipsd9d84852017-06-09 10:48:29 -0400300 if (mf && !mf->asFragmentProcessor(nullptr)) {
Robert Phillipsa29a9562016-10-20 09:40:55 -0400301 // The MaskFilter wasn't already handled in SkPaintToGrPaint
Brian Salomon82f44312017-01-11 13:42:54 -0500302 draw_path_with_mask_filter(context, renderTargetContext, clip, std::move(grPaint), aa,
303 viewMatrix, mf, style, path, pathIsMutable);
bsalomonc55271f2015-11-09 11:55:57 -0800304 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500305 renderTargetContext->drawPath(clip, std::move(grPaint), aa, viewMatrix, *path, style);
robertphillipsccb1b572015-05-27 11:02:55 -0700306 }
robertphillipsccb1b572015-05-27 11:02:55 -0700307}