Index: src/gpu/SkGpuDevice_drawTexture.cpp |
diff --git a/src/gpu/SkGpuDevice_drawTexture.cpp b/src/gpu/SkGpuDevice_drawTexture.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..01734d6c0b9f56e4a06ff50fac9e80fdae25a21d |
--- /dev/null |
+++ b/src/gpu/SkGpuDevice_drawTexture.cpp |
@@ -0,0 +1,161 @@ |
+/* |
+ * 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 "SkGpuDevice.h" |
+ |
+#include "GrBlurUtils.h" |
+#include "GrCaps.h" |
+#include "GrDrawContext.h" |
+#include "GrStrokeInfo.h" |
+#include "GrTextureParamsAdjuster.h" |
+#include "SkDraw.h" |
+#include "SkGrPriv.h" |
+#include "SkMaskFilter.h" |
+#include "effects/GrBicubicEffect.h" |
+#include "effects/GrSimpleTextureEffect.h" |
+#include "effects/GrTextureDomain.h" |
+ |
+static inline bool use_shader(bool textureIsAlphaOnly, const SkPaint& paint) { |
+ return textureIsAlphaOnly && paint.getShader(); |
+} |
+ |
+static const GrFragmentProcessor* mix_texture_fp_with_paint_color_and_shader( |
+ const GrFragmentProcessor* textureFP, |
+ bool textureIsAlphaOnly, |
+ GrContext* context, |
+ const SkMatrix& viewMatrix, |
+ const SkPaint& paint) { |
robertphillips
2015/11/05 20:49:39
'\n' ?
bsalomon
2015/11/06 15:24:26
Done.
|
+ if (textureIsAlphaOnly) { |
+ if (const SkShader* shader = paint.getShader()) { |
+ SkAutoTUnref<const GrFragmentProcessor> shaderFP( |
+ shader->asFragmentProcessor(context, |
+ viewMatrix, |
+ nullptr, |
+ paint.getFilterQuality())); |
+ if (!shaderFP) { |
+ return nullptr; |
+ } |
+ const GrFragmentProcessor* fpSeries[] = { shaderFP, textureFP }; |
+ return GrFragmentProcessor::RunInSeries(fpSeries, 2); |
+ } else { |
+ return GrFragmentProcessor::MulOutputByInputUnpremulColor(textureFP); |
+ } |
+ } else { |
+ return GrFragmentProcessor::MulOutputByInputAlpha(textureFP); |
+ } |
+} |
+ |
+void SkGpuDevice::drawTextureRect(GrTextureAdjuster* adjuster, |
+ bool alphaTexture, |
+ const SkRect& clippedSrcRect, |
+ const SkRect& dstRect, |
+ const SkMatrix& viewMatrix, |
+ const GrClip& clip, |
+ const SkPaint& paint, |
+ SkCanvas::SrcRectConstraint constraint) { |
+ SkMatrix localMatrix; |
+ if (localMatrix.setRectToRect(clippedSrcRect, dstRect, SkMatrix::kFill_ScaleToFit)) { |
+ this->drawTextureRectPrecomputedLocalMatrix(adjuster, alphaTexture, clippedSrcRect, |
+ dstRect, viewMatrix, localMatrix, clip, |
+ paint, constraint); |
+ } |
+} |
+ |
+void SkGpuDevice::drawTextureRectPrecomputedLocalMatrix(GrTextureAdjuster* adjuster, |
+ bool alphaTexture, |
+ const SkRect& clippedSrcRect, |
+ const SkRect& dstRect, |
+ const SkMatrix& viewMatrix, |
+ const SkMatrix& localMatrix, |
+ const GrClip& clip, |
+ const SkPaint& paint, |
+ SkCanvas::SrcRectConstraint constraint) { |
+ // Specifying the texture coords as local coordinates is an attempt to enable more batching |
+ // by not baking anything about the srcRect, dstRect, or viewMatrix, into the texture FP. In |
+ // the future this should be an opaque optimization enabled by the combination of batch/GP and |
+ // FP. |
+ const SkMatrix* textureFPMatrix; |
+ SkMatrix tempMatrix; |
+ const SkMaskFilter* mf = paint.getMaskFilter(); |
+ GrTexture* texture = adjuster->originalTexture(); |
+ // The shader expects proper local coords, so we can't replace local coords with texture coords |
+ // if the shader will be used. If we have a mask filter we will change the underlying geometry |
+ // that is rendered. |
+ bool canUseTextureCoordsAsLocalCoords = !use_shader(alphaTexture, paint) && !mf; |
+ if (canUseTextureCoordsAsLocalCoords) { |
+ textureFPMatrix = &SkMatrix::I(); |
+ } else { |
+ if (!localMatrix.invert(&tempMatrix)) { |
+ return; |
+ } |
+ tempMatrix.postIDiv(texture->width(), texture->height()); |
+ textureFPMatrix = &tempMatrix; |
+ } |
+ |
+ bool doBicubic; |
+ GrTextureParams::FilterMode fm = |
+ GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), viewMatrix, localMatrix, |
+ &doBicubic); |
+ const GrTextureParams::FilterMode* filterMode = doBicubic ? nullptr : &fm; |
+ |
+ GrTextureAdjuster::FilterConstraint constraintMode; |
+ if (SkCanvas::kFast_SrcRectConstraint == constraint) { |
+ constraintMode = GrTextureAdjuster::kNo_FilterConstraint; |
+ } else { |
+ constraintMode = GrTextureAdjuster::kYes_FilterConstraint; |
+ } |
+ |
+ // If we have to outset for AA then we will generate texture coords outside the src rect. The |
+ // same happens for any mask filter that extends the bounds rendered in the dst. |
+ bool coordsAllInsideSrcRect = !paint.isAntiAlias() && !mf; |
+ |
+ SkAutoTUnref<const GrFragmentProcessor> fp(adjuster->createFragmentProcessor( |
+ *textureFPMatrix, clippedSrcRect, constraintMode, coordsAllInsideSrcRect, filterMode)); |
+ if (!fp) { |
+ return; |
+ } |
+ fp.reset(mix_texture_fp_with_paint_color_and_shader(fp, alphaTexture, this->context(), |
+ viewMatrix, paint)); |
+ GrPaint grPaint; |
+ if (!SkPaintToGrPaintReplaceShader(fContext, paint, fp, &grPaint)) { |
+ return; |
+ } |
+ |
+ if (canUseTextureCoordsAsLocalCoords) { |
+ SkRect localRect; |
+ localRect.fLeft = clippedSrcRect.fLeft / texture->width(); |
+ localRect.fBottom = clippedSrcRect.fBottom / texture->height(); |
+ localRect.fRight = clippedSrcRect.fRight / texture->width(); |
+ localRect.fTop = clippedSrcRect.fTop / texture->height(); |
+ fDrawContext->fillRectToRect(clip, grPaint, viewMatrix, dstRect, localRect); |
+ return; |
+ } |
+ |
+ if (!mf) { |
+ fDrawContext->drawRect(clip, grPaint, viewMatrix, dstRect); |
+ return; |
+ } |
+ |
+ // First see if we can do the draw + mask filter direct to the dst. |
+ SkStrokeRec rec(SkStrokeRec::kFill_InitStyle); |
+ SkRRect rrect; |
+ rrect.setRect(dstRect); |
+ if (mf->directFilterRRectMaskGPU(fContext->textureProvider(), |
+ fDrawContext, |
+ &grPaint, |
+ clip, |
+ viewMatrix, |
+ rec, |
+ rrect)) { |
+ return; |
+ } |
+ SkPath rectPath; |
+ rectPath.addRect(dstRect); |
+ GrBlurUtils::drawPathWithMaskFilter(this->context(), fDrawContext, fRenderTarget, fClip, |
+ rectPath, &grPaint, viewMatrix, mf, paint.getPathEffect(), |
+ GrStrokeInfo::FillInfo()); |
+} |