blob: 9d28b54f542f896f2930aeb8fed8d0ec20425237 [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"
robertphillipsccb1b572015-05-27 11:02:55 -070018#include "GrTexture.h"
robertphillipsd728f0c2016-11-21 11:05:03 -080019#include "GrTextureProxy.h"
robertphillipsccb1b572015-05-27 11:02:55 -070020#include "SkDraw.h"
Brian Osman3b655982017-03-07 16:58:08 -050021#include "SkGr.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.
Robert Phillips296b1cc2017-03-15 10:42:12 -040033static bool draw_mask(GrRenderTargetContext* renderTargetContext,
robertphillipsccb1b572015-05-27 11:02:55 -070034 const GrClip& clip,
35 const SkMatrix& viewMatrix,
robertphillipsf054b172016-05-13 05:06:19 -070036 const SkIRect& maskRect,
Brian Salomon82f44312017-01-11 13:42:54 -050037 GrPaint&& paint,
Robert Phillips4a24da52016-12-14 09:00:07 -050038 sk_sp<GrTextureProxy> mask) {
Brian Salomon82f44312017-01-11 13:42:54 -050039 SkMatrix inverse;
40 if (!viewMatrix.invert(&inverse)) {
41 return false;
42 }
Robert Phillips4a24da52016-12-14 09:00:07 -050043
Robert Phillips296b1cc2017-03-15 10:42:12 -040044 GrResourceProvider* resourceProvider = renderTargetContext->resourceProvider();
45
Robert Phillips67c18d62017-01-20 12:44:06 -050046 SkMatrix matrix = SkMatrix::MakeTrans(-SkIntToScalar(maskRect.fLeft),
47 -SkIntToScalar(maskRect.fTop));
Brian Salomon2ebd0c82016-10-03 17:15:28 -040048 matrix.preConcat(viewMatrix);
Robert Phillips296b1cc2017-03-15 10:42:12 -040049 paint.addCoverageFragmentProcessor(GrSimpleTextureEffect::Make(resourceProvider,
50 std::move(mask),
Robert Phillips40fd7c92017-01-30 08:06:27 -050051 nullptr, matrix));
robertphillipsccb1b572015-05-27 11:02:55 -070052
Brian Salomon82f44312017-01-11 13:42:54 -050053 renderTargetContext->fillRectWithLocalMatrix(clip, std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050054 SkRect::Make(maskRect), inverse);
robertphillipsccb1b572015-05-27 11:02:55 -070055 return true;
56}
57
Robert Phillips4a24da52016-12-14 09:00:07 -050058static bool sw_draw_with_mask_filter(GrContext* context,
59 GrRenderTargetContext* renderTargetContext,
robertphillips0e7029e2015-11-30 05:45:06 -080060 const GrClip& clipData,
61 const SkMatrix& viewMatrix,
62 const SkPath& devPath,
63 const SkMaskFilter* filter,
64 const SkIRect& clipBounds,
Brian Salomon82f44312017-01-11 13:42:54 -050065 GrPaint&& paint,
bsalomon6663acf2016-05-10 09:14:17 -070066 SkStrokeRec::InitStyle fillOrHairline) {
robertphillipsccb1b572015-05-27 11:02:55 -070067 SkMask srcM, dstM;
robertphillipsccb1b572015-05-27 11:02:55 -070068 if (!SkDraw::DrawToMask(devPath, &clipBounds, filter, &viewMatrix, &srcM,
bsalomon6663acf2016-05-10 09:14:17 -070069 SkMask::kComputeBoundsAndRenderImage_CreateMode, fillOrHairline)) {
robertphillipsccb1b572015-05-27 11:02:55 -070070 return false;
71 }
72 SkAutoMaskFreeImage autoSrc(srcM.fImage);
73
halcanary96fcdcc2015-08-27 07:41:13 -070074 if (!filter->filterMask(&dstM, srcM, viewMatrix, nullptr)) {
robertphillipsccb1b572015-05-27 11:02:55 -070075 return false;
76 }
77 // this will free-up dstM when we're done (allocated in filterMask())
78 SkAutoMaskFreeImage autoDst(dstM.fImage);
79
80 if (clip_bounds_quick_reject(clipBounds, dstM.fBounds)) {
81 return false;
82 }
83
84 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
85 // the current clip (and identity matrix) and GrPaint settings
86 GrSurfaceDesc desc;
Robert Phillips40fd7c92017-01-30 08:06:27 -050087 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
robertphillipsccb1b572015-05-27 11:02:55 -070088 desc.fWidth = dstM.fBounds.width();
89 desc.fHeight = dstM.fBounds.height();
90 desc.fConfig = kAlpha_8_GrPixelConfig;
91
Robert Phillipsc949ce92017-01-19 16:59:04 -050092 sk_sp<GrSurfaceContext> sContext = context->contextPriv().makeDeferredSurfaceContext(
93 desc,
94 SkBackingFit::kApprox,
95 SkBudgeted::kYes);
96 if (!sContext) {
Robert Phillips4a24da52016-12-14 09:00:07 -050097 return false;
98 }
99
Robert Phillipsc949ce92017-01-19 16:59:04 -0500100 SkImageInfo ii = SkImageInfo::MakeA8(desc.fWidth, desc.fHeight);
101 if (!sContext->writePixels(ii, dstM.fImage, dstM.fRowBytes, 0, 0)) {
robertphillipsccb1b572015-05-27 11:02:55 -0700102 return false;
103 }
robertphillipsccb1b572015-05-27 11:02:55 -0700104
Robert Phillips296b1cc2017-03-15 10:42:12 -0400105 return draw_mask(renderTargetContext, clipData, viewMatrix,
Robert Phillipsf200a902017-01-30 13:27:37 -0500106 dstM.fBounds, std::move(paint), sContext->asTextureProxyRef());
robertphillipsccb1b572015-05-27 11:02:55 -0700107}
108
109// Create a mask of 'devPath' and place the result in 'mask'.
robertphillipsd728f0c2016-11-21 11:05:03 -0800110static sk_sp<GrTextureProxy> create_mask_GPU(GrContext* context,
111 const SkIRect& maskRect,
112 const SkPath& devPath,
113 SkStrokeRec::InitStyle fillOrHairline,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500114 GrAA aa,
robertphillipsd728f0c2016-11-21 11:05:03 -0800115 int sampleCnt) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500116 if (GrAA::kNo == aa) {
robertphillipsd4c741e2016-04-28 09:55:15 -0700117 // Don't need MSAA if mask isn't AA
118 sampleCnt = 0;
119 }
120
Robert Phillips55360b12016-12-05 13:18:47 +0000121 sk_sp<GrRenderTargetContext> rtContext(context->makeDeferredRenderTargetContextWithFallback(
Brian Osman11052242016-10-27 14:47:55 -0400122 SkBackingFit::kApprox, maskRect.width(), maskRect.height(), kAlpha_8_GrPixelConfig, nullptr,
123 sampleCnt));
robertphillipsd728f0c2016-11-21 11:05:03 -0800124 if (!rtContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700125 return nullptr;
robertphillipsccb1b572015-05-27 11:02:55 -0700126 }
127
Robert Phillips784b7bf2016-12-09 13:35:02 -0500128 rtContext->priv().absClear(nullptr, 0x0);
robertphillipsccb1b572015-05-27 11:02:55 -0700129
Brian Salomon82f44312017-01-11 13:42:54 -0500130 GrPaint maskPaint;
131 maskPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op);
robertphillipsccb1b572015-05-27 11:02:55 -0700132
133 // setup new clip
cdalton846c0512016-05-13 10:25:00 -0700134 const SkIRect clipRect = SkIRect::MakeWH(maskRect.width(), maskRect.height());
135 GrFixedClip clip(clipRect);
robertphillipsccb1b572015-05-27 11:02:55 -0700136
robertphillips3833daa2015-09-14 11:18:13 -0700137 // Draw the mask into maskTexture with the path's integerized top-left at
Brian Salomon82f44312017-01-11 13:42:54 -0500138 // the origin using maskPaint.
robertphillipsccb1b572015-05-27 11:02:55 -0700139 SkMatrix translate;
robertphillipsf054b172016-05-13 05:06:19 -0700140 translate.setTranslate(-SkIntToScalar(maskRect.fLeft), -SkIntToScalar(maskRect.fTop));
Brian Salomon82f44312017-01-11 13:42:54 -0500141 rtContext->drawPath(clip, std::move(maskPaint), aa, translate, devPath,
142 GrStyle(fillOrHairline));
Robert Phillipsf200a902017-01-30 13:27:37 -0500143 return rtContext->asTextureProxyRef();
robertphillipsccb1b572015-05-27 11:02:55 -0700144}
145
bsalomonc55271f2015-11-09 11:55:57 -0800146static void draw_path_with_mask_filter(GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -0400147 GrRenderTargetContext* renderTargetContext,
bsalomonc55271f2015-11-09 11:55:57 -0800148 const GrClip& clip,
Brian Salomon82f44312017-01-11 13:42:54 -0500149 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500150 GrAA aa,
bsalomonc55271f2015-11-09 11:55:57 -0800151 const SkMatrix& viewMatrix,
152 const SkMaskFilter* maskFilter,
bsalomon6663acf2016-05-10 09:14:17 -0700153 const GrStyle& style,
154 const SkPath* path,
bsalomonc55271f2015-11-09 11:55:57 -0800155 bool pathIsMutable) {
156 SkASSERT(maskFilter);
157
158 SkIRect clipBounds;
Robert Phillips784b7bf2016-12-09 13:35:02 -0500159 clip.getConservativeBounds(renderTargetContext->width(),
160 renderTargetContext->height(),
Brian Osman11052242016-10-27 14:47:55 -0400161 &clipBounds);
bsalomonc55271f2015-11-09 11:55:57 -0800162 SkTLazy<SkPath> tmpPath;
bsalomon6663acf2016-05-10 09:14:17 -0700163 SkStrokeRec::InitStyle fillOrHairline;
bsalomonc55271f2015-11-09 11:55:57 -0800164
bsalomon6663acf2016-05-10 09:14:17 -0700165 // We just fully apply the style here.
166 if (style.applies()) {
senorblancob6a40b82016-08-19 08:07:22 -0700167 SkScalar scale = GrStyle::MatrixToScaleFactor(viewMatrix);
168 if (0 == scale || !style.applyToPath(tmpPath.init(), &fillOrHairline, *path, scale)) {
bsalomon6663acf2016-05-10 09:14:17 -0700169 return;
170 }
171 pathIsMutable = true;
172 path = tmpPath.get();
173 } else if (style.isSimpleHairline()) {
174 fillOrHairline = SkStrokeRec::kHairline_InitStyle;
bsalomon055e1922016-05-06 07:22:58 -0700175 } else {
bsalomon6663acf2016-05-10 09:14:17 -0700176 SkASSERT(style.isSimpleFill());
177 fillOrHairline = SkStrokeRec::kFill_InitStyle;
bsalomonc55271f2015-11-09 11:55:57 -0800178 }
179
180 // transform the path into device space
bsalomon6663acf2016-05-10 09:14:17 -0700181 if (!viewMatrix.isIdentity()) {
182 SkPath* result;
183 if (pathIsMutable) {
184 result = const_cast<SkPath*>(path);
185 } else {
186 if (!tmpPath.isValid()) {
187 tmpPath.init();
188 }
189 result = tmpPath.get();
190 }
191 path->transform(viewMatrix, result);
192 path = result;
193 result->setIsVolatile(true);
194 pathIsMutable = true;
195 }
bsalomonc55271f2015-11-09 11:55:57 -0800196
197 SkRect maskRect;
bsalomon6663acf2016-05-10 09:14:17 -0700198 if (maskFilter->canFilterMaskGPU(SkRRect::MakeRect(path->getBounds()),
bsalomonc55271f2015-11-09 11:55:57 -0800199 clipBounds,
200 viewMatrix,
201 &maskRect)) {
robertphillipsf054b172016-05-13 05:06:19 -0700202 // This mask will ultimately be drawn as a non-AA rect (see draw_mask).
203 // Non-AA rects have a bad habit of snapping arbitrarily. Integerize here
204 // so the mask draws in a reproducible manner.
bsalomonc55271f2015-11-09 11:55:57 -0800205 SkIRect finalIRect;
206 maskRect.roundOut(&finalIRect);
207 if (clip_bounds_quick_reject(clipBounds, finalIRect)) {
208 // clipped out
209 return;
210 }
211
Robert Phillipsd3749482017-03-14 09:17:43 -0400212 if (maskFilter->directFilterMaskGPU(context,
Brian Osman11052242016-10-27 14:47:55 -0400213 renderTargetContext,
Brian Salomon82f44312017-01-11 13:42:54 -0500214 std::move(paint),
bsalomonc55271f2015-11-09 11:55:57 -0800215 clip,
216 viewMatrix,
bsalomon6663acf2016-05-10 09:14:17 -0700217 SkStrokeRec(fillOrHairline),
218 *path)) {
bsalomonc55271f2015-11-09 11:55:57 -0800219 // the mask filter was able to draw itself directly, so there's nothing
220 // left to do.
221 return;
222 }
223
robertphillipsd728f0c2016-11-21 11:05:03 -0800224 sk_sp<GrTextureProxy> maskProxy(create_mask_GPU(context,
225 finalIRect,
226 *path,
227 fillOrHairline,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500228 aa,
robertphillipsd728f0c2016-11-21 11:05:03 -0800229 renderTargetContext->numColorSamples()));
230 if (maskProxy) {
Robert Phillips4a24da52016-12-14 09:00:07 -0500231 sk_sp<GrTextureProxy> filtered = maskFilter->filterMaskGPU(context,
232 std::move(maskProxy),
233 viewMatrix,
234 finalIRect);
235 if (filtered) {
Robert Phillips296b1cc2017-03-15 10:42:12 -0400236 if (draw_mask(renderTargetContext, clip, viewMatrix,
Brian Salomon82f44312017-01-11 13:42:54 -0500237 finalIRect, std::move(paint), std::move(filtered))) {
bsalomonc55271f2015-11-09 11:55:57 -0800238 // This path is completely drawn
239 return;
240 }
241 }
242 }
243 }
244
Brian Salomon82f44312017-01-11 13:42:54 -0500245 sw_draw_with_mask_filter(context, renderTargetContext, clip, viewMatrix, *path, maskFilter,
246 clipBounds, std::move(paint), fillOrHairline);
bsalomon6663acf2016-05-10 09:14:17 -0700247}
248
249void GrBlurUtils::drawPathWithMaskFilter(GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -0400250 GrRenderTargetContext* renderTargetContext,
bsalomon6663acf2016-05-10 09:14:17 -0700251 const GrClip& clip,
252 const SkPath& path,
Brian Salomon82f44312017-01-11 13:42:54 -0500253 GrPaint&& paint,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500254 GrAA aa,
bsalomon6663acf2016-05-10 09:14:17 -0700255 const SkMatrix& viewMatrix,
256 const SkMaskFilter* mf,
257 const GrStyle& style,
258 bool pathIsMutable) {
Brian Salomon82f44312017-01-11 13:42:54 -0500259 draw_path_with_mask_filter(context, renderTargetContext, clip, std::move(paint), aa, viewMatrix,
260 mf, style, &path, pathIsMutable);
bsalomonc55271f2015-11-09 11:55:57 -0800261}
262
263void GrBlurUtils::drawPathWithMaskFilter(GrContext* context,
Brian Osman11052242016-10-27 14:47:55 -0400264 GrRenderTargetContext* renderTargetContext,
bsalomonc55271f2015-11-09 11:55:57 -0800265 const GrClip& clip,
266 const SkPath& origPath,
robertphillipsccb1b572015-05-27 11:02:55 -0700267 const SkPaint& paint,
268 const SkMatrix& origViewMatrix,
269 const SkMatrix* prePathMatrix,
270 const SkIRect& clipBounds,
271 bool pathIsMutable) {
bsalomon6663acf2016-05-10 09:14:17 -0700272 SkASSERT(!pathIsMutable || origPath.isVolatile());
robertphillipsccb1b572015-05-27 11:02:55 -0700273
bsalomon6663acf2016-05-10 09:14:17 -0700274 GrStyle style(paint);
robertphillipsccb1b572015-05-27 11:02:55 -0700275 // If we have a prematrix, apply it to the path, optimizing for the case
276 // where the original path can in fact be modified in place (even though
277 // its parameter type is const).
bsalomon6663acf2016-05-10 09:14:17 -0700278
279 const SkPath* path = &origPath;
robertphillipsccb1b572015-05-27 11:02:55 -0700280 SkTLazy<SkPath> tmpPath;
robertphillipsccb1b572015-05-27 11:02:55 -0700281
282 SkMatrix viewMatrix = origViewMatrix;
283
284 if (prePathMatrix) {
bsalomon6663acf2016-05-10 09:14:17 -0700285 // Styling, blurs, and shading are supposed to be applied *after* the prePathMatrix.
286 if (!paint.getMaskFilter() && !paint.getShader() && !style.applies()) {
robertphillipsccb1b572015-05-27 11:02:55 -0700287 viewMatrix.preConcat(*prePathMatrix);
288 } else {
bsalomon6663acf2016-05-10 09:14:17 -0700289 SkPath* result = pathIsMutable ? const_cast<SkPath*>(path) : tmpPath.init();
290 pathIsMutable = true;
291 path->transform(*prePathMatrix, result);
292 path = result;
293 result->setIsVolatile(true);
robertphillipsccb1b572015-05-27 11:02:55 -0700294 }
295 }
296 // at this point we're done with prePathMatrix
297 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
298
299 GrPaint grPaint;
Brian Osman11052242016-10-27 14:47:55 -0400300 if (!SkPaintToGrPaint(context, renderTargetContext, paint, viewMatrix, &grPaint)) {
robertphillipsccb1b572015-05-27 11:02:55 -0700301 return;
302 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500303 GrAA aa = GrBoolToAA(paint.isAntiAlias());
Robert Phillipsa29a9562016-10-20 09:40:55 -0400304 SkMaskFilter* mf = paint.getMaskFilter();
305 if (mf && !mf->asFragmentProcessor(nullptr, nullptr, viewMatrix)) {
306 // The MaskFilter wasn't already handled in SkPaintToGrPaint
Brian Salomon82f44312017-01-11 13:42:54 -0500307 draw_path_with_mask_filter(context, renderTargetContext, clip, std::move(grPaint), aa,
308 viewMatrix, mf, style, path, pathIsMutable);
bsalomonc55271f2015-11-09 11:55:57 -0800309 } else {
Brian Salomon82f44312017-01-11 13:42:54 -0500310 renderTargetContext->drawPath(clip, std::move(grPaint), aa, viewMatrix, *path, style);
robertphillipsccb1b572015-05-27 11:02:55 -0700311 }
robertphillipsccb1b572015-05-27 11:02:55 -0700312}