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" |
| 17 | #include "SkGr.h" |
| 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, |
| 100 | const SkRect& maskRect, |
| 101 | const SkPath& devPath, |
| 102 | const GrStrokeInfo& strokeInfo, |
| 103 | bool doAA, |
| 104 | int sampleCnt) { |
| 105 | GrSurfaceDesc desc; |
| 106 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 107 | desc.fWidth = SkScalarCeilToInt(maskRect.width()); |
| 108 | desc.fHeight = SkScalarCeilToInt(maskRect.height()); |
| 109 | desc.fSampleCnt = doAA ? sampleCnt : 0; |
| 110 | // We actually only need A8, but it often isn't supported as a |
| 111 | // render target so default to RGBA_8888 |
| 112 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 113 | |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 114 | if (context->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, desc.fSampleCnt > 0)) { |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 115 | desc.fConfig = kAlpha_8_GrPixelConfig; |
| 116 | } |
| 117 | |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 118 | GrTexture* mask = context->textureProvider()->createApproxTexture(desc); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame^] | 119 | if (nullptr == mask) { |
| 120 | return nullptr; |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height()); |
| 124 | |
| 125 | GrDrawContext* drawContext = context->drawContext(); |
| 126 | if (!drawContext) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame^] | 127 | return nullptr; |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 128 | } |
| 129 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame^] | 130 | drawContext->clear(mask->asRenderTarget(), nullptr, 0x0, true); |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 131 | |
| 132 | GrPaint tempPaint; |
| 133 | tempPaint.setAntiAlias(doAA); |
| 134 | tempPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op); |
| 135 | |
| 136 | // setup new clip |
| 137 | GrClip clip(clipRect); |
| 138 | |
| 139 | // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint. |
| 140 | SkMatrix translate; |
| 141 | translate.setTranslate(-maskRect.fLeft, -maskRect.fTop); |
| 142 | drawContext->drawPath(mask->asRenderTarget(), clip, tempPaint, translate, devPath, strokeInfo); |
| 143 | return mask; |
| 144 | } |
| 145 | |
| 146 | void GrBlurUtils::drawPathWithMaskFilter(GrContext* context, |
| 147 | GrDrawContext* drawContext, |
| 148 | GrRenderTarget* renderTarget, |
| 149 | const GrClip& clip, |
| 150 | const SkPath& origSrcPath, |
| 151 | const SkPaint& paint, |
| 152 | const SkMatrix& origViewMatrix, |
| 153 | const SkMatrix* prePathMatrix, |
| 154 | const SkIRect& clipBounds, |
| 155 | bool pathIsMutable) { |
| 156 | SkASSERT(!pathIsMutable || origSrcPath.isVolatile()); |
| 157 | |
| 158 | GrStrokeInfo strokeInfo(paint); |
| 159 | |
| 160 | // If we have a prematrix, apply it to the path, optimizing for the case |
| 161 | // where the original path can in fact be modified in place (even though |
| 162 | // its parameter type is const). |
| 163 | SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath); |
| 164 | SkTLazy<SkPath> tmpPath; |
| 165 | SkTLazy<SkPath> effectPath; |
| 166 | SkPathEffect* pathEffect = paint.getPathEffect(); |
| 167 | |
| 168 | SkMatrix viewMatrix = origViewMatrix; |
| 169 | |
| 170 | if (prePathMatrix) { |
| 171 | // stroking, path effects, and blurs are supposed to be applied *after* the prePathMatrix. |
| 172 | // The pre-path-matrix also should not affect shading. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame^] | 173 | if (nullptr == paint.getMaskFilter() && nullptr == pathEffect && nullptr == paint.getShader() && |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 174 | (strokeInfo.isFillStyle() || strokeInfo.isHairlineStyle())) { |
| 175 | viewMatrix.preConcat(*prePathMatrix); |
| 176 | } else { |
| 177 | SkPath* result = pathPtr; |
| 178 | |
| 179 | if (!pathIsMutable) { |
| 180 | result = tmpPath.init(); |
| 181 | result->setIsVolatile(true); |
| 182 | pathIsMutable = true; |
| 183 | } |
| 184 | // should I push prePathMatrix on our MV stack temporarily, instead |
| 185 | // of applying it here? See SkDraw.cpp |
| 186 | pathPtr->transform(*prePathMatrix, result); |
| 187 | pathPtr = result; |
| 188 | } |
| 189 | } |
| 190 | // at this point we're done with prePathMatrix |
| 191 | SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;) |
| 192 | |
| 193 | GrPaint grPaint; |
| 194 | if (!SkPaint2GrPaint(context, renderTarget, paint, viewMatrix, true, &grPaint)) { |
| 195 | return; |
| 196 | } |
| 197 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame^] | 198 | const SkRect* cullRect = nullptr; // TODO: what is our bounds? |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 199 | if (!strokeInfo.isDashed() && pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, |
| 200 | &strokeInfo, cullRect)) { |
| 201 | pathPtr = effectPath.get(); |
| 202 | pathIsMutable = true; |
| 203 | } |
| 204 | |
| 205 | if (paint.getMaskFilter()) { |
| 206 | if (!strokeInfo.isHairlineStyle()) { |
| 207 | SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init(); |
| 208 | if (strokeInfo.isDashed()) { |
| 209 | if (pathEffect->filterPath(strokedPath, *pathPtr, &strokeInfo, cullRect)) { |
| 210 | pathPtr = strokedPath; |
| 211 | pathIsMutable = true; |
| 212 | } |
| 213 | strokeInfo.removeDash(); |
| 214 | } |
| 215 | if (strokeInfo.applyToPath(strokedPath, *pathPtr)) { |
| 216 | pathPtr = strokedPath; |
| 217 | pathIsMutable = true; |
| 218 | strokeInfo.setFillStyle(); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | // avoid possibly allocating a new path in transform if we can |
| 223 | SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init(); |
| 224 | if (!pathIsMutable) { |
| 225 | devPathPtr->setIsVolatile(true); |
| 226 | } |
| 227 | |
| 228 | // transform the path into device space |
| 229 | pathPtr->transform(viewMatrix, devPathPtr); |
| 230 | |
| 231 | SkRect maskRect; |
| 232 | if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(), |
| 233 | clipBounds, |
| 234 | viewMatrix, |
| 235 | &maskRect)) { |
| 236 | SkIRect finalIRect; |
| 237 | maskRect.roundOut(&finalIRect); |
| 238 | if (clip_bounds_quick_reject(clipBounds, finalIRect)) { |
| 239 | // clipped out |
| 240 | return; |
| 241 | } |
| 242 | |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 243 | if (paint.getMaskFilter()->directFilterMaskGPU(context->textureProvider(), |
| 244 | drawContext, |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 245 | renderTarget, |
| 246 | &grPaint, |
| 247 | clip, |
| 248 | viewMatrix, |
| 249 | strokeInfo, |
| 250 | *devPathPtr)) { |
| 251 | // the mask filter was able to draw itself directly, so there's nothing |
| 252 | // left to do. |
| 253 | return; |
| 254 | } |
| 255 | |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 256 | SkAutoTUnref<GrTexture> mask(create_mask_GPU(context, |
| 257 | maskRect, |
| 258 | *devPathPtr, |
| 259 | strokeInfo, |
| 260 | grPaint.isAntiAlias(), |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame] | 261 | renderTarget->numColorSamples())); |
robertphillips | ccb1b57 | 2015-05-27 11:02:55 -0700 | [diff] [blame] | 262 | if (mask) { |
| 263 | GrTexture* filtered; |
| 264 | |
| 265 | if (paint.getMaskFilter()->filterMaskGPU(mask, viewMatrix, maskRect, |
| 266 | &filtered, true)) { |
| 267 | // filterMaskGPU gives us ownership of a ref to the result |
| 268 | SkAutoTUnref<GrTexture> atu(filtered); |
| 269 | if (draw_mask(drawContext, |
| 270 | renderTarget, |
| 271 | clip, |
| 272 | viewMatrix, |
| 273 | maskRect, |
| 274 | &grPaint, |
| 275 | filtered)) { |
| 276 | // This path is completely drawn |
| 277 | return; |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | // draw the mask on the CPU - this is a fallthrough path in case the |
| 284 | // GPU path fails |
| 285 | SkPaint::Style style = strokeInfo.isHairlineStyle() ? SkPaint::kStroke_Style : |
| 286 | SkPaint::kFill_Style; |
| 287 | draw_with_mask_filter(drawContext, context->textureProvider(), renderTarget, |
| 288 | clip, viewMatrix, *devPathPtr, |
| 289 | paint.getMaskFilter(), clipBounds, &grPaint, style); |
| 290 | return; |
| 291 | } |
| 292 | |
| 293 | drawContext->drawPath(renderTarget, clip, grPaint, viewMatrix, *pathPtr, strokeInfo); |
| 294 | } |
| 295 | |