robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 1 | /* |
| 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" |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 10 | #include "GrCaps.h" |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 11 | #include "GrContext.h" |
| 12 | #include "effects/GrSimpleTextureEffect.h" |
| 13 | #include "GrStrokeInfo.h" |
| 14 | #include "GrTexture.h" |
| 15 | #include "GrTextureProvider.h" |
| 16 | #include "SkDraw.h" |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 17 | #include "SkGrPriv.h" |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 18 | #include "SkMaskFilter.h" |
| 19 | #include "SkPaint.h" |
| 20 | |
| 21 | static bool clip_bounds_quick_reject(const SkIRect& clipBounds, const SkIRect& rect) { |
| 22 | return clipBounds.isEmpty() || rect.isEmpty() || !SkIRect::Intersects(clipBounds, rect); |
| 23 | } |
| 24 | |
| 25 | // Draw a mask using the supplied paint. Since the coverage/geometry |
| 26 | // is already burnt into the mask this boils down to a rect draw. |
| 27 | // Return true if the mask was successfully drawn. |
| 28 | static bool draw_mask(GrDrawContext* drawContext, |
| 29 | GrRenderTarget* rt, |
| 30 | const GrClip& clip, |
| 31 | const SkMatrix& viewMatrix, |
| 32 | const SkRect& maskRect, |
| 33 | GrPaint* grp, |
| 34 | GrTexture* mask) { |
| 35 | SkMatrix matrix; |
| 36 | matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop); |
| 37 | matrix.postIDiv(mask->width(), mask->height()); |
| 38 | |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 39 | grp->addCoverageFragmentProcessor(GrSimpleTextureEffect::Create(grp->getProcessorDataManager(), |
joshualitt | 5f10b5c | 2015-07-09 10:24:35 -0700 | [diff] [blame] | 40 | mask, matrix, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 41 | kDevice_GrCoordSet))->unref(); |
| 42 | |
| 43 | SkMatrix inverse; |
| 44 | if (!viewMatrix.invert(&inverse)) { |
| 45 | return false; |
| 46 | } |
| 47 | drawContext->drawNonAARectWithLocalMatrix(rt, clip, *grp, SkMatrix::I(), maskRect, inverse); |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | static bool draw_with_mask_filter(GrDrawContext* drawContext, |
| 52 | GrTextureProvider* textureProvider, |
| 53 | GrRenderTarget* rt, |
| 54 | const GrClip& clipData, |
| 55 | const SkMatrix& viewMatrix, |
| 56 | const SkPath& devPath, |
| 57 | SkMaskFilter* filter, |
| 58 | const SkIRect& clipBounds, |
| 59 | GrPaint* grp, |
| 60 | SkPaint::Style style) { |
| 61 | SkMask srcM, dstM; |
| 62 | |
| 63 | if (!SkDraw::DrawToMask(devPath, &clipBounds, filter, &viewMatrix, &srcM, |
| 64 | SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) { |
| 65 | return false; |
| 66 | } |
| 67 | SkAutoMaskFreeImage autoSrc(srcM.fImage); |
| 68 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 69 | if (!filter->filterMask(&dstM, srcM, viewMatrix, nullptr)) { |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 70 | return false; |
| 71 | } |
| 72 | // this will free-up dstM when we're done (allocated in filterMask()) |
| 73 | SkAutoMaskFreeImage autoDst(dstM.fImage); |
| 74 | |
| 75 | if (clip_bounds_quick_reject(clipBounds, dstM.fBounds)) { |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | // we now have a device-aligned 8bit mask in dstM, ready to be drawn using |
| 80 | // the current clip (and identity matrix) and GrPaint settings |
| 81 | GrSurfaceDesc desc; |
| 82 | desc.fWidth = dstM.fBounds.width(); |
| 83 | desc.fHeight = dstM.fBounds.height(); |
| 84 | desc.fConfig = kAlpha_8_GrPixelConfig; |
| 85 | |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 86 | SkAutoTUnref<GrTexture> texture(textureProvider->createApproxTexture(desc)); |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 87 | if (!texture) { |
| 88 | return false; |
| 89 | } |
| 90 | texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, |
| 91 | dstM.fImage, dstM.fRowBytes); |
| 92 | |
| 93 | SkRect maskRect = SkRect::Make(dstM.fBounds); |
| 94 | |
| 95 | return draw_mask(drawContext, rt, clipData, viewMatrix, maskRect, grp, texture); |
| 96 | } |
| 97 | |
| 98 | // Create a mask of 'devPath' and place the result in 'mask'. |
| 99 | static GrTexture* create_mask_GPU(GrContext* context, |
robertphillips | 3833daa | 2015-09-14 11:18:13 -0700 | [diff] [blame] | 100 | SkRect* maskRect, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 101 | const SkPath& devPath, |
| 102 | const GrStrokeInfo& strokeInfo, |
| 103 | bool doAA, |
| 104 | int sampleCnt) { |
robertphillips | 3833daa | 2015-09-14 11:18:13 -0700 | [diff] [blame] | 105 | // This mask will ultimately be drawn as a non-AA rect (see draw_mask). |
| 106 | // Non-AA rects have a bad habit of snapping arbitrarily. Integerize here |
| 107 | // so the mask draws in a reproducible manner. |
| 108 | *maskRect = SkRect::Make(maskRect->roundOut()); |
| 109 | |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 110 | GrSurfaceDesc desc; |
| 111 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
robertphillips | 3833daa | 2015-09-14 11:18:13 -0700 | [diff] [blame] | 112 | desc.fWidth = SkScalarCeilToInt(maskRect->width()); |
| 113 | desc.fHeight = SkScalarCeilToInt(maskRect->height()); |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 114 | desc.fSampleCnt = doAA ? sampleCnt : 0; |
| 115 | // We actually only need A8, but it often isn't supported as a |
| 116 | // render target so default to RGBA_8888 |
| 117 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 118 | |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 119 | if (context->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, desc.fSampleCnt > 0)) { |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 120 | desc.fConfig = kAlpha_8_GrPixelConfig; |
| 121 | } |
| 122 | |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 123 | GrTexture* mask = context->textureProvider()->createApproxTexture(desc); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 124 | if (nullptr == mask) { |
| 125 | return nullptr; |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 126 | } |
| 127 | |
robertphillips | 3833daa | 2015-09-14 11:18:13 -0700 | [diff] [blame] | 128 | SkRect clipRect = SkRect::MakeWH(maskRect->width(), maskRect->height()); |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 129 | |
robertphillips | c9a3706 | 2015-09-01 08:34:28 -0700 | [diff] [blame] | 130 | SkAutoTUnref<GrDrawContext> drawContext(context->drawContext()); |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 131 | if (!drawContext) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 132 | return nullptr; |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 133 | } |
| 134 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 135 | drawContext->clear(mask->asRenderTarget(), nullptr, 0x0, true); |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 136 | |
| 137 | GrPaint tempPaint; |
| 138 | tempPaint.setAntiAlias(doAA); |
| 139 | tempPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op); |
| 140 | |
| 141 | // setup new clip |
| 142 | GrClip clip(clipRect); |
| 143 | |
robertphillips | 3833daa | 2015-09-14 11:18:13 -0700 | [diff] [blame] | 144 | // Draw the mask into maskTexture with the path's integerized top-left at |
| 145 | // the origin using tempPaint. |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 146 | SkMatrix translate; |
robertphillips | 3833daa | 2015-09-14 11:18:13 -0700 | [diff] [blame] | 147 | translate.setTranslate(-maskRect->fLeft, -maskRect->fTop); |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 148 | drawContext->drawPath(mask->asRenderTarget(), clip, tempPaint, translate, devPath, strokeInfo); |
| 149 | return mask; |
| 150 | } |
| 151 | |
| 152 | void GrBlurUtils::drawPathWithMaskFilter(GrContext* context, |
| 153 | GrDrawContext* drawContext, |
| 154 | GrRenderTarget* renderTarget, |
| 155 | const GrClip& clip, |
| 156 | const SkPath& origSrcPath, |
| 157 | const SkPaint& paint, |
| 158 | const SkMatrix& origViewMatrix, |
| 159 | const SkMatrix* prePathMatrix, |
| 160 | const SkIRect& clipBounds, |
| 161 | bool pathIsMutable) { |
| 162 | SkASSERT(!pathIsMutable || origSrcPath.isVolatile()); |
| 163 | |
| 164 | GrStrokeInfo strokeInfo(paint); |
| 165 | |
| 166 | // If we have a prematrix, apply it to the path, optimizing for the case |
| 167 | // where the original path can in fact be modified in place (even though |
| 168 | // its parameter type is const). |
| 169 | SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath); |
| 170 | SkTLazy<SkPath> tmpPath; |
| 171 | SkTLazy<SkPath> effectPath; |
| 172 | SkPathEffect* pathEffect = paint.getPathEffect(); |
| 173 | |
| 174 | SkMatrix viewMatrix = origViewMatrix; |
| 175 | |
| 176 | if (prePathMatrix) { |
| 177 | // stroking, path effects, and blurs are supposed to be applied *after* the prePathMatrix. |
| 178 | // The pre-path-matrix also should not affect shading. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 179 | if (nullptr == paint.getMaskFilter() && nullptr == pathEffect && nullptr == paint.getShader() && |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 180 | (strokeInfo.isFillStyle() || strokeInfo.isHairlineStyle())) { |
| 181 | viewMatrix.preConcat(*prePathMatrix); |
| 182 | } else { |
| 183 | SkPath* result = pathPtr; |
| 184 | |
| 185 | if (!pathIsMutable) { |
| 186 | result = tmpPath.init(); |
| 187 | result->setIsVolatile(true); |
| 188 | pathIsMutable = true; |
| 189 | } |
| 190 | // should I push prePathMatrix on our MV stack temporarily, instead |
| 191 | // of applying it here? See SkDraw.cpp |
| 192 | pathPtr->transform(*prePathMatrix, result); |
| 193 | pathPtr = result; |
| 194 | } |
| 195 | } |
| 196 | // at this point we're done with prePathMatrix |
| 197 | SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;) |
| 198 | |
| 199 | GrPaint grPaint; |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 200 | if (!SkPaintToGrPaint(context, paint, viewMatrix, &grPaint)) { |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 201 | return; |
| 202 | } |
| 203 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 204 | const SkRect* cullRect = nullptr; // TODO: what is our bounds? |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 205 | if (!strokeInfo.isDashed() && pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, |
| 206 | &strokeInfo, cullRect)) { |
| 207 | pathPtr = effectPath.get(); |
| 208 | pathIsMutable = true; |
| 209 | } |
| 210 | |
| 211 | if (paint.getMaskFilter()) { |
| 212 | if (!strokeInfo.isHairlineStyle()) { |
| 213 | SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init(); |
| 214 | if (strokeInfo.isDashed()) { |
| 215 | if (pathEffect->filterPath(strokedPath, *pathPtr, &strokeInfo, cullRect)) { |
| 216 | pathPtr = strokedPath; |
| 217 | pathIsMutable = true; |
| 218 | } |
| 219 | strokeInfo.removeDash(); |
| 220 | } |
| 221 | if (strokeInfo.applyToPath(strokedPath, *pathPtr)) { |
| 222 | pathPtr = strokedPath; |
| 223 | pathIsMutable = true; |
| 224 | strokeInfo.setFillStyle(); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | // avoid possibly allocating a new path in transform if we can |
| 229 | SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init(); |
| 230 | if (!pathIsMutable) { |
| 231 | devPathPtr->setIsVolatile(true); |
| 232 | } |
| 233 | |
| 234 | // transform the path into device space |
| 235 | pathPtr->transform(viewMatrix, devPathPtr); |
| 236 | |
| 237 | SkRect maskRect; |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 238 | if (paint.getMaskFilter()->canFilterMaskGPU(SkRRect::MakeRect(devPathPtr->getBounds()), |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 239 | clipBounds, |
| 240 | viewMatrix, |
| 241 | &maskRect)) { |
| 242 | SkIRect finalIRect; |
| 243 | maskRect.roundOut(&finalIRect); |
| 244 | if (clip_bounds_quick_reject(clipBounds, finalIRect)) { |
| 245 | // clipped out |
| 246 | return; |
| 247 | } |
| 248 | |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 249 | if (paint.getMaskFilter()->directFilterMaskGPU(context->textureProvider(), |
| 250 | drawContext, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 251 | renderTarget, |
| 252 | &grPaint, |
| 253 | clip, |
| 254 | viewMatrix, |
| 255 | strokeInfo, |
| 256 | *devPathPtr)) { |
| 257 | // the mask filter was able to draw itself directly, so there's nothing |
| 258 | // left to do. |
| 259 | return; |
| 260 | } |
| 261 | |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 262 | SkAutoTUnref<GrTexture> mask(create_mask_GPU(context, |
robertphillips | 3833daa | 2015-09-14 11:18:13 -0700 | [diff] [blame] | 263 | &maskRect, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 264 | *devPathPtr, |
| 265 | strokeInfo, |
| 266 | grPaint.isAntiAlias(), |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 267 | renderTarget->numColorSamples())); |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 268 | if (mask) { |
| 269 | GrTexture* filtered; |
| 270 | |
| 271 | if (paint.getMaskFilter()->filterMaskGPU(mask, viewMatrix, maskRect, |
| 272 | &filtered, true)) { |
| 273 | // filterMaskGPU gives us ownership of a ref to the result |
| 274 | SkAutoTUnref<GrTexture> atu(filtered); |
| 275 | if (draw_mask(drawContext, |
| 276 | renderTarget, |
| 277 | clip, |
| 278 | viewMatrix, |
| 279 | maskRect, |
| 280 | &grPaint, |
| 281 | filtered)) { |
| 282 | // This path is completely drawn |
| 283 | return; |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | // draw the mask on the CPU - this is a fallthrough path in case the |
| 290 | // GPU path fails |
| 291 | SkPaint::Style style = strokeInfo.isHairlineStyle() ? SkPaint::kStroke_Style : |
| 292 | SkPaint::kFill_Style; |
| 293 | draw_with_mask_filter(drawContext, context->textureProvider(), renderTarget, |
| 294 | clip, viewMatrix, *devPathPtr, |
| 295 | paint.getMaskFilter(), clipBounds, &grPaint, style); |
| 296 | return; |
| 297 | } |
| 298 | |
| 299 | drawContext->drawPath(renderTarget, clip, grPaint, viewMatrix, *pathPtr, strokeInfo); |
| 300 | } |
| 301 | |