blob: 5f575e3c8471796bb1f44bd07582efadbd8b8ce5 [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"
9#include "GrDrawContext.h"
bsalomon76228632015-05-29 08:02:10 -070010#include "GrCaps.h"
robertphillipsccb1b572015-05-27 11:02:55 -070011#include "GrContext.h"
csmartdalton02fa32c2016-08-19 13:29:27 -070012#include "GrFixedClip.h"
robertphillipsccb1b572015-05-27 11:02:55 -070013#include "effects/GrSimpleTextureEffect.h"
bsalomon6663acf2016-05-10 09:14:17 -070014#include "GrStyle.h"
robertphillipsccb1b572015-05-27 11:02:55 -070015#include "GrTexture.h"
16#include "GrTextureProvider.h"
17#include "SkDraw.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070018#include "SkGrPriv.h"
robertphillipsccb1b572015-05-27 11:02:55 -070019#include "SkMaskFilter.h"
20#include "SkPaint.h"
csmartdaltonc6f411e2016-08-05 22:32:12 -070021#include "SkTLazy.h"
robertphillipsccb1b572015-05-27 11:02:55 -070022
23static bool clip_bounds_quick_reject(const SkIRect& clipBounds, const SkIRect& rect) {
24 return clipBounds.isEmpty() || rect.isEmpty() || !SkIRect::Intersects(clipBounds, rect);
25}
26
27// Draw a mask using the supplied paint. Since the coverage/geometry
28// is already burnt into the mask this boils down to a rect draw.
29// Return true if the mask was successfully drawn.
30static bool draw_mask(GrDrawContext* drawContext,
robertphillipsccb1b572015-05-27 11:02:55 -070031 const GrClip& clip,
32 const SkMatrix& viewMatrix,
robertphillipsf054b172016-05-13 05:06:19 -070033 const SkIRect& maskRect,
robertphillipsccb1b572015-05-27 11:02:55 -070034 GrPaint* grp,
35 GrTexture* mask) {
36 SkMatrix matrix;
robertphillipsf054b172016-05-13 05:06:19 -070037 matrix.setTranslate(-SkIntToScalar(maskRect.fLeft), -SkIntToScalar(maskRect.fTop));
robertphillipsccb1b572015-05-27 11:02:55 -070038 matrix.postIDiv(mask->width(), mask->height());
Brian Salomon2ebd0c82016-10-03 17:15:28 -040039 matrix.preConcat(viewMatrix);
40 grp->addCoverageFragmentProcessor(GrSimpleTextureEffect::Make(mask, nullptr, matrix));
robertphillipsccb1b572015-05-27 11:02:55 -070041
42 SkMatrix inverse;
43 if (!viewMatrix.invert(&inverse)) {
44 return false;
45 }
Brian Salomon2ebd0c82016-10-03 17:15:28 -040046 drawContext->fillRectWithLocalMatrix(clip, *grp, SkMatrix::I(), SkRect::Make(maskRect),
47 inverse);
robertphillipsccb1b572015-05-27 11:02:55 -070048 return true;
49}
50
robertphillips0e7029e2015-11-30 05:45:06 -080051static bool sw_draw_with_mask_filter(GrDrawContext* drawContext,
52 GrTextureProvider* textureProvider,
53 const GrClip& clipData,
54 const SkMatrix& viewMatrix,
55 const SkPath& devPath,
56 const SkMaskFilter* filter,
57 const SkIRect& clipBounds,
58 GrPaint* grp,
bsalomon6663acf2016-05-10 09:14:17 -070059 SkStrokeRec::InitStyle fillOrHairline) {
robertphillipsccb1b572015-05-27 11:02:55 -070060 SkMask srcM, dstM;
robertphillipsccb1b572015-05-27 11:02:55 -070061 if (!SkDraw::DrawToMask(devPath, &clipBounds, filter, &viewMatrix, &srcM,
bsalomon6663acf2016-05-10 09:14:17 -070062 SkMask::kComputeBoundsAndRenderImage_CreateMode, fillOrHairline)) {
robertphillipsccb1b572015-05-27 11:02:55 -070063 return false;
64 }
65 SkAutoMaskFreeImage autoSrc(srcM.fImage);
66
halcanary96fcdcc2015-08-27 07:41:13 -070067 if (!filter->filterMask(&dstM, srcM, viewMatrix, nullptr)) {
robertphillipsccb1b572015-05-27 11:02:55 -070068 return false;
69 }
70 // this will free-up dstM when we're done (allocated in filterMask())
71 SkAutoMaskFreeImage autoDst(dstM.fImage);
72
73 if (clip_bounds_quick_reject(clipBounds, dstM.fBounds)) {
74 return false;
75 }
76
77 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
78 // the current clip (and identity matrix) and GrPaint settings
79 GrSurfaceDesc desc;
80 desc.fWidth = dstM.fBounds.width();
81 desc.fHeight = dstM.fBounds.height();
82 desc.fConfig = kAlpha_8_GrPixelConfig;
83
bsalomoneae62002015-07-31 13:59:30 -070084 SkAutoTUnref<GrTexture> texture(textureProvider->createApproxTexture(desc));
robertphillipsccb1b572015-05-27 11:02:55 -070085 if (!texture) {
86 return false;
87 }
88 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
89 dstM.fImage, dstM.fRowBytes);
90
robertphillipsf054b172016-05-13 05:06:19 -070091 return draw_mask(drawContext, clipData, viewMatrix, dstM.fBounds, grp, texture);
robertphillipsccb1b572015-05-27 11:02:55 -070092}
93
94// Create a mask of 'devPath' and place the result in 'mask'.
robertphillipsd4c741e2016-04-28 09:55:15 -070095static sk_sp<GrTexture> create_mask_GPU(GrContext* context,
robertphillipsf054b172016-05-13 05:06:19 -070096 const SkIRect& maskRect,
robertphillipsd4c741e2016-04-28 09:55:15 -070097 const SkPath& devPath,
bsalomon6663acf2016-05-10 09:14:17 -070098 SkStrokeRec::InitStyle fillOrHairline,
robertphillipsd4c741e2016-04-28 09:55:15 -070099 bool doAA,
100 int sampleCnt) {
robertphillipsd4c741e2016-04-28 09:55:15 -0700101 if (!doAA) {
102 // Don't need MSAA if mask isn't AA
103 sampleCnt = 0;
104 }
105
robertphillips48fde9c2016-09-06 05:20:20 -0700106 sk_sp<GrDrawContext> drawContext(context->makeDrawContextWithFallback(SkBackingFit::kApprox,
107 maskRect.width(),
108 maskRect.height(),
109 kAlpha_8_GrPixelConfig,
110 nullptr,
111 sampleCnt));
robertphillipsccb1b572015-05-27 11:02:55 -0700112 if (!drawContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700113 return nullptr;
robertphillipsccb1b572015-05-27 11:02:55 -0700114 }
115
robertphillips2e1e51f2015-10-15 08:01:48 -0700116 drawContext->clear(nullptr, 0x0, true);
robertphillipsccb1b572015-05-27 11:02:55 -0700117
118 GrPaint tempPaint;
119 tempPaint.setAntiAlias(doAA);
120 tempPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op);
121
122 // setup new clip
cdalton846c0512016-05-13 10:25:00 -0700123 const SkIRect clipRect = SkIRect::MakeWH(maskRect.width(), maskRect.height());
124 GrFixedClip clip(clipRect);
robertphillipsccb1b572015-05-27 11:02:55 -0700125
robertphillips3833daa2015-09-14 11:18:13 -0700126 // Draw the mask into maskTexture with the path's integerized top-left at
127 // the origin using tempPaint.
robertphillipsccb1b572015-05-27 11:02:55 -0700128 SkMatrix translate;
robertphillipsf054b172016-05-13 05:06:19 -0700129 translate.setTranslate(-SkIntToScalar(maskRect.fLeft), -SkIntToScalar(maskRect.fTop));
bsalomon6663acf2016-05-10 09:14:17 -0700130 drawContext->drawPath(clip, tempPaint, translate, devPath, GrStyle(fillOrHairline));
robertphillipsd4c741e2016-04-28 09:55:15 -0700131 return drawContext->asTexture();;
robertphillipsccb1b572015-05-27 11:02:55 -0700132}
133
bsalomonc55271f2015-11-09 11:55:57 -0800134static void draw_path_with_mask_filter(GrContext* context,
135 GrDrawContext* drawContext,
bsalomonc55271f2015-11-09 11:55:57 -0800136 const GrClip& clip,
137 GrPaint* paint,
138 const SkMatrix& viewMatrix,
139 const SkMaskFilter* maskFilter,
bsalomon6663acf2016-05-10 09:14:17 -0700140 const GrStyle& style,
141 const SkPath* path,
bsalomonc55271f2015-11-09 11:55:57 -0800142 bool pathIsMutable) {
143 SkASSERT(maskFilter);
144
145 SkIRect clipBounds;
robertphillips7bceedc2015-12-01 12:51:26 -0800146 clip.getConservativeBounds(drawContext->width(), drawContext->height(), &clipBounds);
bsalomonc55271f2015-11-09 11:55:57 -0800147 SkTLazy<SkPath> tmpPath;
bsalomon6663acf2016-05-10 09:14:17 -0700148 SkStrokeRec::InitStyle fillOrHairline;
bsalomonc55271f2015-11-09 11:55:57 -0800149
bsalomon6663acf2016-05-10 09:14:17 -0700150 // We just fully apply the style here.
151 if (style.applies()) {
senorblancob6a40b82016-08-19 08:07:22 -0700152 SkScalar scale = GrStyle::MatrixToScaleFactor(viewMatrix);
153 if (0 == scale || !style.applyToPath(tmpPath.init(), &fillOrHairline, *path, scale)) {
bsalomon6663acf2016-05-10 09:14:17 -0700154 return;
155 }
156 pathIsMutable = true;
157 path = tmpPath.get();
158 } else if (style.isSimpleHairline()) {
159 fillOrHairline = SkStrokeRec::kHairline_InitStyle;
bsalomon055e1922016-05-06 07:22:58 -0700160 } else {
bsalomon6663acf2016-05-10 09:14:17 -0700161 SkASSERT(style.isSimpleFill());
162 fillOrHairline = SkStrokeRec::kFill_InitStyle;
bsalomonc55271f2015-11-09 11:55:57 -0800163 }
164
165 // transform the path into device space
bsalomon6663acf2016-05-10 09:14:17 -0700166 if (!viewMatrix.isIdentity()) {
167 SkPath* result;
168 if (pathIsMutable) {
169 result = const_cast<SkPath*>(path);
170 } else {
171 if (!tmpPath.isValid()) {
172 tmpPath.init();
173 }
174 result = tmpPath.get();
175 }
176 path->transform(viewMatrix, result);
177 path = result;
178 result->setIsVolatile(true);
179 pathIsMutable = true;
180 }
bsalomonc55271f2015-11-09 11:55:57 -0800181
182 SkRect maskRect;
bsalomon6663acf2016-05-10 09:14:17 -0700183 if (maskFilter->canFilterMaskGPU(SkRRect::MakeRect(path->getBounds()),
bsalomonc55271f2015-11-09 11:55:57 -0800184 clipBounds,
185 viewMatrix,
186 &maskRect)) {
robertphillipsf054b172016-05-13 05:06:19 -0700187 // This mask will ultimately be drawn as a non-AA rect (see draw_mask).
188 // Non-AA rects have a bad habit of snapping arbitrarily. Integerize here
189 // so the mask draws in a reproducible manner.
bsalomonc55271f2015-11-09 11:55:57 -0800190 SkIRect finalIRect;
191 maskRect.roundOut(&finalIRect);
192 if (clip_bounds_quick_reject(clipBounds, finalIRect)) {
193 // clipped out
194 return;
195 }
196
197 if (maskFilter->directFilterMaskGPU(context->textureProvider(),
198 drawContext,
199 paint,
200 clip,
201 viewMatrix,
bsalomon6663acf2016-05-10 09:14:17 -0700202 SkStrokeRec(fillOrHairline),
203 *path)) {
bsalomonc55271f2015-11-09 11:55:57 -0800204 // the mask filter was able to draw itself directly, so there's nothing
205 // left to do.
206 return;
207 }
208
robertphillipsd4c741e2016-04-28 09:55:15 -0700209 sk_sp<GrTexture> mask(create_mask_GPU(context,
robertphillipsf054b172016-05-13 05:06:19 -0700210 finalIRect,
bsalomon6663acf2016-05-10 09:14:17 -0700211 *path,
212 fillOrHairline,
robertphillipsd4c741e2016-04-28 09:55:15 -0700213 paint->isAntiAlias(),
214 drawContext->numColorSamples()));
bsalomonc55271f2015-11-09 11:55:57 -0800215 if (mask) {
216 GrTexture* filtered;
217
robertphillips8bad3ac2016-06-27 11:11:05 -0700218 if (maskFilter->filterMaskGPU(mask.get(), viewMatrix, finalIRect, &filtered)) {
bsalomonc55271f2015-11-09 11:55:57 -0800219 // filterMaskGPU gives us ownership of a ref to the result
220 SkAutoTUnref<GrTexture> atu(filtered);
robertphillipsf054b172016-05-13 05:06:19 -0700221 if (draw_mask(drawContext, clip, viewMatrix, finalIRect, paint, filtered)) {
bsalomonc55271f2015-11-09 11:55:57 -0800222 // This path is completely drawn
223 return;
224 }
225 }
226 }
227 }
228
robertphillips0e7029e2015-11-30 05:45:06 -0800229 sw_draw_with_mask_filter(drawContext, context->textureProvider(),
bsalomon6663acf2016-05-10 09:14:17 -0700230 clip, viewMatrix, *path,
231 maskFilter, clipBounds, paint, fillOrHairline);
232}
233
234void GrBlurUtils::drawPathWithMaskFilter(GrContext* context,
235 GrDrawContext* drawContext,
236 const GrClip& clip,
237 const SkPath& path,
238 GrPaint* paint,
239 const SkMatrix& viewMatrix,
240 const SkMaskFilter* mf,
241 const GrStyle& style,
242 bool pathIsMutable) {
243 draw_path_with_mask_filter(context, drawContext, clip, paint, viewMatrix, mf,
244 style, &path, pathIsMutable);
bsalomonc55271f2015-11-09 11:55:57 -0800245}
246
247void GrBlurUtils::drawPathWithMaskFilter(GrContext* context,
248 GrDrawContext* drawContext,
bsalomonc55271f2015-11-09 11:55:57 -0800249 const GrClip& clip,
250 const SkPath& origPath,
robertphillipsccb1b572015-05-27 11:02:55 -0700251 const SkPaint& paint,
252 const SkMatrix& origViewMatrix,
253 const SkMatrix* prePathMatrix,
254 const SkIRect& clipBounds,
255 bool pathIsMutable) {
bsalomon6663acf2016-05-10 09:14:17 -0700256 SkASSERT(!pathIsMutable || origPath.isVolatile());
robertphillipsccb1b572015-05-27 11:02:55 -0700257
bsalomon6663acf2016-05-10 09:14:17 -0700258 GrStyle style(paint);
robertphillipsccb1b572015-05-27 11:02:55 -0700259 // If we have a prematrix, apply it to the path, optimizing for the case
260 // where the original path can in fact be modified in place (even though
261 // its parameter type is const).
bsalomon6663acf2016-05-10 09:14:17 -0700262
263 const SkPath* path = &origPath;
robertphillipsccb1b572015-05-27 11:02:55 -0700264 SkTLazy<SkPath> tmpPath;
robertphillipsccb1b572015-05-27 11:02:55 -0700265
266 SkMatrix viewMatrix = origViewMatrix;
267
268 if (prePathMatrix) {
bsalomon6663acf2016-05-10 09:14:17 -0700269 // Styling, blurs, and shading are supposed to be applied *after* the prePathMatrix.
270 if (!paint.getMaskFilter() && !paint.getShader() && !style.applies()) {
robertphillipsccb1b572015-05-27 11:02:55 -0700271 viewMatrix.preConcat(*prePathMatrix);
272 } else {
bsalomon6663acf2016-05-10 09:14:17 -0700273 SkPath* result = pathIsMutable ? const_cast<SkPath*>(path) : tmpPath.init();
274 pathIsMutable = true;
275 path->transform(*prePathMatrix, result);
276 path = result;
277 result->setIsVolatile(true);
robertphillipsccb1b572015-05-27 11:02:55 -0700278 }
279 }
280 // at this point we're done with prePathMatrix
281 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
282
283 GrPaint grPaint;
brianosman8fe485b2016-07-25 12:31:51 -0700284 if (!SkPaintToGrPaint(context, drawContext, paint, viewMatrix, &grPaint)) {
robertphillipsccb1b572015-05-27 11:02:55 -0700285 return;
286 }
287
robertphillipsccb1b572015-05-27 11:02:55 -0700288 if (paint.getMaskFilter()) {
robertphillips7bceedc2015-12-01 12:51:26 -0800289 draw_path_with_mask_filter(context, drawContext, clip, &grPaint, viewMatrix,
bsalomon6663acf2016-05-10 09:14:17 -0700290 paint.getMaskFilter(), style,
291 path, pathIsMutable);
bsalomonc55271f2015-11-09 11:55:57 -0800292 } else {
bsalomon6663acf2016-05-10 09:14:17 -0700293 drawContext->drawPath(clip, grPaint, viewMatrix, *path, style);
robertphillipsccb1b572015-05-27 11:02:55 -0700294 }
robertphillipsccb1b572015-05-27 11:02:55 -0700295}