2015-05-27 18:02:55 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "GrBlurUtils.h"
|
|
|
|
#include "GrDrawContext.h"
|
2015-05-29 15:02:10 +00:00
|
|
|
#include "GrCaps.h"
|
2015-05-27 18:02:55 +00:00
|
|
|
#include "GrContext.h"
|
|
|
|
#include "effects/GrSimpleTextureEffect.h"
|
2016-05-10 16:14:17 +00:00
|
|
|
#include "GrStyle.h"
|
2015-05-27 18:02:55 +00:00
|
|
|
#include "GrTexture.h"
|
|
|
|
#include "GrTextureProvider.h"
|
|
|
|
#include "SkDraw.h"
|
2015-09-28 13:26:28 +00:00
|
|
|
#include "SkGrPriv.h"
|
2015-05-27 18:02:55 +00:00
|
|
|
#include "SkMaskFilter.h"
|
|
|
|
#include "SkPaint.h"
|
|
|
|
|
|
|
|
static bool clip_bounds_quick_reject(const SkIRect& clipBounds, const SkIRect& rect) {
|
|
|
|
return clipBounds.isEmpty() || rect.isEmpty() || !SkIRect::Intersects(clipBounds, rect);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw a mask using the supplied paint. Since the coverage/geometry
|
|
|
|
// is already burnt into the mask this boils down to a rect draw.
|
|
|
|
// Return true if the mask was successfully drawn.
|
|
|
|
static bool draw_mask(GrDrawContext* drawContext,
|
|
|
|
const GrClip& clip,
|
|
|
|
const SkMatrix& viewMatrix,
|
2016-05-13 12:06:19 +00:00
|
|
|
const SkIRect& maskRect,
|
2015-05-27 18:02:55 +00:00
|
|
|
GrPaint* grp,
|
|
|
|
GrTexture* mask) {
|
|
|
|
SkMatrix matrix;
|
2016-05-13 12:06:19 +00:00
|
|
|
matrix.setTranslate(-SkIntToScalar(maskRect.fLeft), -SkIntToScalar(maskRect.fTop));
|
2015-05-27 18:02:55 +00:00
|
|
|
matrix.postIDiv(mask->width(), mask->height());
|
|
|
|
|
2015-10-06 15:40:50 +00:00
|
|
|
grp->addCoverageFragmentProcessor(GrSimpleTextureEffect::Create(mask, matrix,
|
|
|
|
kDevice_GrCoordSet))->unref();
|
2015-05-27 18:02:55 +00:00
|
|
|
|
|
|
|
SkMatrix inverse;
|
|
|
|
if (!viewMatrix.invert(&inverse)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-13 12:06:19 +00:00
|
|
|
drawContext->fillRectWithLocalMatrix(clip, *grp, SkMatrix::I(),
|
|
|
|
SkRect::Make(maskRect), inverse);
|
2015-05-27 18:02:55 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-11-30 13:45:06 +00:00
|
|
|
static bool sw_draw_with_mask_filter(GrDrawContext* drawContext,
|
|
|
|
GrTextureProvider* textureProvider,
|
|
|
|
const GrClip& clipData,
|
|
|
|
const SkMatrix& viewMatrix,
|
|
|
|
const SkPath& devPath,
|
|
|
|
const SkMaskFilter* filter,
|
|
|
|
const SkIRect& clipBounds,
|
|
|
|
GrPaint* grp,
|
2016-05-10 16:14:17 +00:00
|
|
|
SkStrokeRec::InitStyle fillOrHairline) {
|
2015-05-27 18:02:55 +00:00
|
|
|
SkMask srcM, dstM;
|
|
|
|
if (!SkDraw::DrawToMask(devPath, &clipBounds, filter, &viewMatrix, &srcM,
|
2016-05-10 16:14:17 +00:00
|
|
|
SkMask::kComputeBoundsAndRenderImage_CreateMode, fillOrHairline)) {
|
2015-05-27 18:02:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
SkAutoMaskFreeImage autoSrc(srcM.fImage);
|
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
if (!filter->filterMask(&dstM, srcM, viewMatrix, nullptr)) {
|
2015-05-27 18:02:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// this will free-up dstM when we're done (allocated in filterMask())
|
|
|
|
SkAutoMaskFreeImage autoDst(dstM.fImage);
|
|
|
|
|
|
|
|
if (clip_bounds_quick_reject(clipBounds, dstM.fBounds)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we now have a device-aligned 8bit mask in dstM, ready to be drawn using
|
|
|
|
// the current clip (and identity matrix) and GrPaint settings
|
|
|
|
GrSurfaceDesc desc;
|
|
|
|
desc.fWidth = dstM.fBounds.width();
|
|
|
|
desc.fHeight = dstM.fBounds.height();
|
|
|
|
desc.fConfig = kAlpha_8_GrPixelConfig;
|
|
|
|
|
2015-07-31 20:59:30 +00:00
|
|
|
SkAutoTUnref<GrTexture> texture(textureProvider->createApproxTexture(desc));
|
2015-05-27 18:02:55 +00:00
|
|
|
if (!texture) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
|
|
|
|
dstM.fImage, dstM.fRowBytes);
|
|
|
|
|
2016-05-13 12:06:19 +00:00
|
|
|
return draw_mask(drawContext, clipData, viewMatrix, dstM.fBounds, grp, texture);
|
2015-05-27 18:02:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a mask of 'devPath' and place the result in 'mask'.
|
2016-04-28 16:55:15 +00:00
|
|
|
static sk_sp<GrTexture> create_mask_GPU(GrContext* context,
|
2016-05-13 12:06:19 +00:00
|
|
|
const SkIRect& maskRect,
|
2016-04-28 16:55:15 +00:00
|
|
|
const SkPath& devPath,
|
2016-05-10 16:14:17 +00:00
|
|
|
SkStrokeRec::InitStyle fillOrHairline,
|
2016-04-28 16:55:15 +00:00
|
|
|
bool doAA,
|
|
|
|
int sampleCnt) {
|
|
|
|
if (!doAA) {
|
|
|
|
// Don't need MSAA if mask isn't AA
|
|
|
|
sampleCnt = 0;
|
2015-05-27 18:02:55 +00:00
|
|
|
}
|
|
|
|
|
2016-04-28 16:55:15 +00:00
|
|
|
// We actually only need A8, but it often isn't supported as a
|
|
|
|
// render target so default to RGBA_8888
|
|
|
|
GrPixelConfig config = kRGBA_8888_GrPixelConfig;
|
|
|
|
if (context->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, sampleCnt > 0)) {
|
|
|
|
config = kAlpha_8_GrPixelConfig;
|
2016-04-28 13:21:55 +00:00
|
|
|
}
|
|
|
|
|
2016-05-04 19:47:41 +00:00
|
|
|
sk_sp<GrDrawContext> drawContext(context->newDrawContext(SkBackingFit::kApprox,
|
2016-05-13 12:06:19 +00:00
|
|
|
maskRect.width(),
|
|
|
|
maskRect.height(),
|
2016-04-28 16:55:15 +00:00
|
|
|
config,
|
|
|
|
sampleCnt));
|
2015-05-27 18:02:55 +00:00
|
|
|
if (!drawContext) {
|
2015-08-27 14:41:13 +00:00
|
|
|
return nullptr;
|
2015-05-27 18:02:55 +00:00
|
|
|
}
|
|
|
|
|
2015-10-15 15:01:48 +00:00
|
|
|
drawContext->clear(nullptr, 0x0, true);
|
2015-05-27 18:02:55 +00:00
|
|
|
|
|
|
|
GrPaint tempPaint;
|
|
|
|
tempPaint.setAntiAlias(doAA);
|
|
|
|
tempPaint.setCoverageSetOpXPFactory(SkRegion::kReplace_Op);
|
|
|
|
|
|
|
|
// setup new clip
|
2016-05-13 12:06:19 +00:00
|
|
|
const SkRect clipRect = SkRect::MakeIWH(maskRect.width(), maskRect.height());
|
2015-05-27 18:02:55 +00:00
|
|
|
GrClip clip(clipRect);
|
|
|
|
|
2015-09-14 18:18:13 +00:00
|
|
|
// Draw the mask into maskTexture with the path's integerized top-left at
|
|
|
|
// the origin using tempPaint.
|
2015-05-27 18:02:55 +00:00
|
|
|
SkMatrix translate;
|
2016-05-13 12:06:19 +00:00
|
|
|
translate.setTranslate(-SkIntToScalar(maskRect.fLeft), -SkIntToScalar(maskRect.fTop));
|
2016-05-10 16:14:17 +00:00
|
|
|
drawContext->drawPath(clip, tempPaint, translate, devPath, GrStyle(fillOrHairline));
|
2016-04-28 16:55:15 +00:00
|
|
|
return drawContext->asTexture();;
|
2015-05-27 18:02:55 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 19:55:57 +00:00
|
|
|
static void draw_path_with_mask_filter(GrContext* context,
|
|
|
|
GrDrawContext* drawContext,
|
|
|
|
const GrClip& clip,
|
|
|
|
GrPaint* paint,
|
|
|
|
const SkMatrix& viewMatrix,
|
|
|
|
const SkMaskFilter* maskFilter,
|
2016-05-10 16:14:17 +00:00
|
|
|
const GrStyle& style,
|
|
|
|
const SkPath* path,
|
2015-11-09 19:55:57 +00:00
|
|
|
bool pathIsMutable) {
|
|
|
|
SkASSERT(maskFilter);
|
|
|
|
|
|
|
|
SkIRect clipBounds;
|
2015-12-01 20:51:26 +00:00
|
|
|
clip.getConservativeBounds(drawContext->width(), drawContext->height(), &clipBounds);
|
2015-11-09 19:55:57 +00:00
|
|
|
SkTLazy<SkPath> tmpPath;
|
2016-05-10 16:14:17 +00:00
|
|
|
SkStrokeRec::InitStyle fillOrHairline;
|
2015-11-09 19:55:57 +00:00
|
|
|
|
2016-05-10 16:14:17 +00:00
|
|
|
// We just fully apply the style here.
|
|
|
|
if (style.applies()) {
|
|
|
|
if (!style.applyToPath(tmpPath.init(), &fillOrHairline, *path,
|
|
|
|
GrStyle::MatrixToScaleFactor(viewMatrix))) {
|
|
|
|
return;
|
2016-05-10 13:19:21 +00:00
|
|
|
}
|
2016-05-10 16:14:17 +00:00
|
|
|
pathIsMutable = true;
|
|
|
|
path = tmpPath.get();
|
|
|
|
} else if (style.isSimpleHairline()) {
|
|
|
|
fillOrHairline = SkStrokeRec::kHairline_InitStyle;
|
|
|
|
} else {
|
|
|
|
SkASSERT(style.isSimpleFill());
|
|
|
|
fillOrHairline = SkStrokeRec::kFill_InitStyle;
|
2016-05-10 12:57:27 +00:00
|
|
|
}
|
2015-11-09 19:55:57 +00:00
|
|
|
|
2016-05-10 13:19:21 +00:00
|
|
|
// transform the path into device space
|
2016-05-10 16:14:17 +00:00
|
|
|
if (!viewMatrix.isIdentity()) {
|
|
|
|
SkPath* result;
|
|
|
|
if (pathIsMutable) {
|
|
|
|
result = const_cast<SkPath*>(path);
|
|
|
|
} else {
|
|
|
|
if (!tmpPath.isValid()) {
|
|
|
|
tmpPath.init();
|
|
|
|
}
|
|
|
|
result = tmpPath.get();
|
|
|
|
}
|
|
|
|
path->transform(viewMatrix, result);
|
|
|
|
path = result;
|
|
|
|
result->setIsVolatile(true);
|
|
|
|
pathIsMutable = true;
|
|
|
|
}
|
2016-05-10 13:19:21 +00:00
|
|
|
|
2015-11-09 19:55:57 +00:00
|
|
|
SkRect maskRect;
|
2016-05-10 16:14:17 +00:00
|
|
|
if (maskFilter->canFilterMaskGPU(SkRRect::MakeRect(path->getBounds()),
|
2015-11-09 19:55:57 +00:00
|
|
|
clipBounds,
|
|
|
|
viewMatrix,
|
|
|
|
&maskRect)) {
|
2016-05-13 12:06:19 +00:00
|
|
|
// This mask will ultimately be drawn as a non-AA rect (see draw_mask).
|
|
|
|
// Non-AA rects have a bad habit of snapping arbitrarily. Integerize here
|
|
|
|
// so the mask draws in a reproducible manner.
|
2015-11-09 19:55:57 +00:00
|
|
|
SkIRect finalIRect;
|
|
|
|
maskRect.roundOut(&finalIRect);
|
|
|
|
if (clip_bounds_quick_reject(clipBounds, finalIRect)) {
|
|
|
|
// clipped out
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (maskFilter->directFilterMaskGPU(context->textureProvider(),
|
|
|
|
drawContext,
|
|
|
|
paint,
|
|
|
|
clip,
|
|
|
|
viewMatrix,
|
2016-05-10 16:14:17 +00:00
|
|
|
SkStrokeRec(fillOrHairline),
|
|
|
|
*path)) {
|
2015-11-09 19:55:57 +00:00
|
|
|
// the mask filter was able to draw itself directly, so there's nothing
|
|
|
|
// left to do.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-28 16:55:15 +00:00
|
|
|
sk_sp<GrTexture> mask(create_mask_GPU(context,
|
2016-05-13 12:06:19 +00:00
|
|
|
finalIRect,
|
2016-05-10 16:14:17 +00:00
|
|
|
*path,
|
|
|
|
fillOrHairline,
|
2016-04-28 16:55:15 +00:00
|
|
|
paint->isAntiAlias(),
|
|
|
|
drawContext->numColorSamples()));
|
2015-11-09 19:55:57 +00:00
|
|
|
if (mask) {
|
|
|
|
GrTexture* filtered;
|
|
|
|
|
2016-05-13 12:06:19 +00:00
|
|
|
if (maskFilter->filterMaskGPU(mask.get(), viewMatrix, finalIRect, &filtered, true)) {
|
2015-11-09 19:55:57 +00:00
|
|
|
// filterMaskGPU gives us ownership of a ref to the result
|
|
|
|
SkAutoTUnref<GrTexture> atu(filtered);
|
2016-05-13 12:06:19 +00:00
|
|
|
if (draw_mask(drawContext, clip, viewMatrix, finalIRect, paint, filtered)) {
|
2015-11-09 19:55:57 +00:00
|
|
|
// This path is completely drawn
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-30 13:45:06 +00:00
|
|
|
sw_draw_with_mask_filter(drawContext, context->textureProvider(),
|
2016-05-10 16:14:17 +00:00
|
|
|
clip, viewMatrix, *path,
|
|
|
|
maskFilter, clipBounds, paint, fillOrHairline);
|
2015-11-09 19:55:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GrBlurUtils::drawPathWithMaskFilter(GrContext* context,
|
|
|
|
GrDrawContext* drawContext,
|
|
|
|
const GrClip& clip,
|
2016-05-10 16:14:17 +00:00
|
|
|
const SkPath& path,
|
2015-11-09 19:55:57 +00:00
|
|
|
GrPaint* paint,
|
|
|
|
const SkMatrix& viewMatrix,
|
|
|
|
const SkMaskFilter* mf,
|
2016-05-10 16:14:17 +00:00
|
|
|
const GrStyle& style,
|
2015-11-30 13:45:06 +00:00
|
|
|
bool pathIsMutable) {
|
2016-05-10 16:14:17 +00:00
|
|
|
draw_path_with_mask_filter(context, drawContext, clip, paint, viewMatrix, mf,
|
|
|
|
style, &path, pathIsMutable);
|
2015-11-09 19:55:57 +00:00
|
|
|
}
|
|
|
|
|
2016-03-29 16:03:52 +00:00
|
|
|
void GrBlurUtils::drawPathWithMaskFilter(GrContext* context,
|
2015-05-27 18:02:55 +00:00
|
|
|
GrDrawContext* drawContext,
|
|
|
|
const GrClip& clip,
|
2016-05-10 16:14:17 +00:00
|
|
|
const SkPath& origPath,
|
2015-05-27 18:02:55 +00:00
|
|
|
const SkPaint& paint,
|
|
|
|
const SkMatrix& origViewMatrix,
|
|
|
|
const SkMatrix* prePathMatrix,
|
|
|
|
const SkIRect& clipBounds,
|
|
|
|
bool pathIsMutable) {
|
2016-05-10 16:14:17 +00:00
|
|
|
SkASSERT(!pathIsMutable || origPath.isVolatile());
|
2015-05-27 18:02:55 +00:00
|
|
|
|
2016-05-10 16:14:17 +00:00
|
|
|
GrStyle style(paint);
|
2015-05-27 18:02:55 +00:00
|
|
|
// If we have a prematrix, apply it to the path, optimizing for the case
|
|
|
|
// where the original path can in fact be modified in place (even though
|
|
|
|
// its parameter type is const).
|
2016-05-10 16:14:17 +00:00
|
|
|
|
|
|
|
const SkPath* path = &origPath;
|
2015-05-27 18:02:55 +00:00
|
|
|
SkTLazy<SkPath> tmpPath;
|
|
|
|
|
|
|
|
SkMatrix viewMatrix = origViewMatrix;
|
|
|
|
|
|
|
|
if (prePathMatrix) {
|
2016-05-10 16:14:17 +00:00
|
|
|
// Styling, blurs, and shading are supposed to be applied *after* the prePathMatrix.
|
|
|
|
if (!paint.getMaskFilter() && !paint.getShader() && !style.applies()) {
|
2015-05-27 18:02:55 +00:00
|
|
|
viewMatrix.preConcat(*prePathMatrix);
|
|
|
|
} else {
|
2016-05-10 16:14:17 +00:00
|
|
|
SkPath* result = pathIsMutable ? const_cast<SkPath*>(path) : tmpPath.init();
|
|
|
|
pathIsMutable = true;
|
|
|
|
path->transform(*prePathMatrix, result);
|
|
|
|
path = result;
|
|
|
|
result->setIsVolatile(true);
|
2015-05-27 18:02:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// at this point we're done with prePathMatrix
|
|
|
|
SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
|
|
|
|
|
|
|
|
GrPaint grPaint;
|
2016-04-13 20:10:14 +00:00
|
|
|
if (!SkPaintToGrPaint(context, paint, viewMatrix, drawContext->isGammaCorrect(),
|
2016-04-06 14:38:23 +00:00
|
|
|
&grPaint)) {
|
2015-05-27 18:02:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (paint.getMaskFilter()) {
|
2015-12-01 20:51:26 +00:00
|
|
|
draw_path_with_mask_filter(context, drawContext, clip, &grPaint, viewMatrix,
|
2016-05-10 16:14:17 +00:00
|
|
|
paint.getMaskFilter(), style,
|
|
|
|
path, pathIsMutable);
|
2015-11-09 19:55:57 +00:00
|
|
|
} else {
|
2016-05-10 16:14:17 +00:00
|
|
|
drawContext->drawPath(clip, grPaint, viewMatrix, *path, style);
|
2015-05-27 18:02:55 +00:00
|
|
|
}
|
|
|
|
}
|