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"
|
2016-10-27 18:47:55 +00:00
|
|
|
#include "GrRenderTargetContext.h"
|
2015-05-29 15:02:10 +00:00
|
|
|
#include "GrCaps.h"
|
2015-05-27 18:02:55 +00:00
|
|
|
#include "GrContext.h"
|
2016-08-19 20:29:27 +00:00
|
|
|
#include "GrFixedClip.h"
|
2015-05-27 18:02:55 +00:00
|
|
|
#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"
|
2016-11-21 19:05:03 +00:00
|
|
|
#include "GrTextureProxy.h"
|
2015-05-27 18:02:55 +00:00
|
|
|
#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"
|
2016-08-06 05:32:12 +00:00
|
|
|
#include "SkTLazy.h"
|
2015-05-27 18:02:55 +00:00
|
|
|
|
|
|
|
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.
|
2016-10-27 18:47:55 +00:00
|
|
|
static bool draw_mask(GrRenderTargetContext* renderTargetContext,
|
2015-05-27 18:02:55 +00:00
|
|
|
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());
|
2016-10-03 21:15:28 +00:00
|
|
|
matrix.preConcat(viewMatrix);
|
|
|
|
grp->addCoverageFragmentProcessor(GrSimpleTextureEffect::Make(mask, nullptr, matrix));
|
2015-05-27 18:02:55 +00:00
|
|
|
|
|
|
|
SkMatrix inverse;
|
|
|
|
if (!viewMatrix.invert(&inverse)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-27 18:47:55 +00:00
|
|
|
renderTargetContext->fillRectWithLocalMatrix(clip, *grp, SkMatrix::I(), SkRect::Make(maskRect),
|
|
|
|
inverse);
|
2015-05-27 18:02:55 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-27 18:47:55 +00:00
|
|
|
static bool sw_draw_with_mask_filter(GrRenderTargetContext* renderTargetContext,
|
2015-11-30 13:45:06 +00:00
|
|
|
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;
|
|
|
|
|
2016-11-07 22:57:18 +00:00
|
|
|
sk_sp<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-11-07 22:57:18 +00:00
|
|
|
return draw_mask(renderTargetContext, clipData, viewMatrix, dstM.fBounds, grp, texture.get());
|
2015-05-27 18:02:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a mask of 'devPath' and place the result in 'mask'.
|
2016-11-21 19:05:03 +00:00
|
|
|
static sk_sp<GrTextureProxy> create_mask_GPU(GrContext* context,
|
|
|
|
const SkIRect& maskRect,
|
|
|
|
const SkPath& devPath,
|
|
|
|
SkStrokeRec::InitStyle fillOrHairline,
|
|
|
|
bool doAA,
|
|
|
|
int sampleCnt) {
|
2016-04-28 16:55:15 +00:00
|
|
|
if (!doAA) {
|
|
|
|
// Don't need MSAA if mask isn't AA
|
|
|
|
sampleCnt = 0;
|
2015-05-27 18:02:55 +00:00
|
|
|
}
|
|
|
|
|
2016-12-01 17:57:20 +00:00
|
|
|
sk_sp<GrRenderTargetContext> rtContext(context->makeRenderTargetContextWithFallback(
|
2016-10-27 18:47:55 +00:00
|
|
|
SkBackingFit::kApprox, maskRect.width(), maskRect.height(), kAlpha_8_GrPixelConfig, nullptr,
|
|
|
|
sampleCnt));
|
2016-11-21 19:05:03 +00:00
|
|
|
if (!rtContext) {
|
2015-08-27 14:41:13 +00:00
|
|
|
return nullptr;
|
2015-05-27 18:02:55 +00:00
|
|
|
}
|
|
|
|
|
2016-11-21 19:05:03 +00:00
|
|
|
rtContext->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 17:25:00 +00:00
|
|
|
const SkIRect clipRect = SkIRect::MakeWH(maskRect.width(), maskRect.height());
|
|
|
|
GrFixedClip clip(clipRect);
|
2015-05-27 18:02:55 +00:00
|
|
|
|
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-11-21 19:05:03 +00:00
|
|
|
rtContext->drawPath(clip, tempPaint, translate, devPath, GrStyle(fillOrHairline));
|
|
|
|
return sk_ref_sp(rtContext->asDeferredTexture());
|
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,
|
2016-10-27 18:47:55 +00:00
|
|
|
GrRenderTargetContext* renderTargetContext,
|
2015-11-09 19:55:57 +00:00
|
|
|
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;
|
2016-11-24 00:37:13 +00:00
|
|
|
clip.getConservativeBounds(renderTargetContext->worstCaseWidth(),
|
|
|
|
renderTargetContext->worstCaseHeight(),
|
2016-10-27 18:47:55 +00:00
|
|
|
&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()) {
|
2016-08-19 15:07:22 +00:00
|
|
|
SkScalar scale = GrStyle::MatrixToScaleFactor(viewMatrix);
|
|
|
|
if (0 == scale || !style.applyToPath(tmpPath.init(), &fillOrHairline, *path, scale)) {
|
2016-05-10 16:14:17 +00:00
|
|
|
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(),
|
2016-10-27 18:47:55 +00:00
|
|
|
renderTargetContext,
|
2015-11-09 19:55:57 +00:00
|
|
|
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-11-21 19:05:03 +00:00
|
|
|
sk_sp<GrTextureProxy> maskProxy(create_mask_GPU(context,
|
|
|
|
finalIRect,
|
|
|
|
*path,
|
|
|
|
fillOrHairline,
|
|
|
|
paint->isAntiAlias(),
|
|
|
|
renderTargetContext->numColorSamples()));
|
|
|
|
if (maskProxy) {
|
2015-11-09 19:55:57 +00:00
|
|
|
GrTexture* filtered;
|
|
|
|
|
2016-11-21 19:05:03 +00:00
|
|
|
GrTexture* mask = maskProxy->instantiate(context->textureProvider());
|
|
|
|
if (!mask) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (maskFilter->filterMaskGPU(mask, viewMatrix, finalIRect, &filtered)) {
|
2015-11-09 19:55:57 +00:00
|
|
|
// filterMaskGPU gives us ownership of a ref to the result
|
2016-11-07 22:57:18 +00:00
|
|
|
sk_sp<GrTexture> atu(filtered);
|
2016-10-27 18:47:55 +00:00
|
|
|
if (draw_mask(renderTargetContext, clip, viewMatrix, finalIRect, paint, filtered)) {
|
2015-11-09 19:55:57 +00:00
|
|
|
// This path is completely drawn
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-27 18:47:55 +00:00
|
|
|
sw_draw_with_mask_filter(renderTargetContext, 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,
|
2016-10-27 18:47:55 +00:00
|
|
|
GrRenderTargetContext* renderTargetContext,
|
2015-11-09 19:55:57 +00:00
|
|
|
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-10-27 18:47:55 +00:00
|
|
|
draw_path_with_mask_filter(context, renderTargetContext, clip, paint, viewMatrix, mf,
|
2016-05-10 16:14:17 +00:00
|
|
|
style, &path, pathIsMutable);
|
2015-11-09 19:55:57 +00:00
|
|
|
}
|
|
|
|
|
2016-03-29 16:03:52 +00:00
|
|
|
void GrBlurUtils::drawPathWithMaskFilter(GrContext* context,
|
2016-10-27 18:47:55 +00:00
|
|
|
GrRenderTargetContext* renderTargetContext,
|
2015-05-27 18:02:55 +00:00
|
|
|
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-10-27 18:47:55 +00:00
|
|
|
if (!SkPaintToGrPaint(context, renderTargetContext, paint, viewMatrix, &grPaint)) {
|
2015-05-27 18:02:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-20 13:40:55 +00:00
|
|
|
SkMaskFilter* mf = paint.getMaskFilter();
|
|
|
|
if (mf && !mf->asFragmentProcessor(nullptr, nullptr, viewMatrix)) {
|
|
|
|
// The MaskFilter wasn't already handled in SkPaintToGrPaint
|
2016-10-27 18:47:55 +00:00
|
|
|
draw_path_with_mask_filter(context, renderTargetContext, clip, &grPaint, viewMatrix,
|
2016-10-20 13:40:55 +00:00
|
|
|
mf, style,
|
2016-05-10 16:14:17 +00:00
|
|
|
path, pathIsMutable);
|
2015-11-09 19:55:57 +00:00
|
|
|
} else {
|
2016-10-27 18:47:55 +00:00
|
|
|
renderTargetContext->drawPath(clip, grPaint, viewMatrix, *path, style);
|
2015-05-27 18:02:55 +00:00
|
|
|
}
|
|
|
|
}
|