bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [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 "SkGpuDevice.h" |
| 9 | |
| 10 | #include "GrBlurUtils.h" |
| 11 | #include "GrCaps.h" |
| 12 | #include "GrDrawContext.h" |
| 13 | #include "GrStrokeInfo.h" |
| 14 | #include "GrTextureParamsAdjuster.h" |
| 15 | #include "SkDraw.h" |
| 16 | #include "SkGrPriv.h" |
| 17 | #include "SkMaskFilter.h" |
| 18 | #include "effects/GrBicubicEffect.h" |
| 19 | #include "effects/GrSimpleTextureEffect.h" |
| 20 | #include "effects/GrTextureDomain.h" |
| 21 | |
| 22 | static inline bool use_shader(bool textureIsAlphaOnly, const SkPaint& paint) { |
| 23 | return textureIsAlphaOnly && paint.getShader(); |
| 24 | } |
| 25 | |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 26 | ////////////////////////////////////////////////////////////////////////////// |
| 27 | // Helper functions for dropping src rect constraint in bilerp mode. |
| 28 | |
| 29 | static const SkScalar kColorBleedTolerance = 0.001f; |
| 30 | |
| 31 | static bool has_aligned_samples(const SkRect& srcRect, const SkRect& transformedRect) { |
| 32 | // detect pixel disalignment |
Brian Salomon | a911f8f | 2015-11-18 15:19:57 -0500 | [diff] [blame] | 33 | if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) - transformedRect.left()) < kColorBleedTolerance && |
| 34 | SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) - transformedRect.top()) < kColorBleedTolerance && |
| 35 | SkScalarAbs(transformedRect.width() - srcRect.width()) < kColorBleedTolerance && |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 36 | SkScalarAbs(transformedRect.height() - srcRect.height()) < kColorBleedTolerance) { |
| 37 | return true; |
| 38 | } |
| 39 | return false; |
| 40 | } |
| 41 | |
| 42 | static bool may_color_bleed(const SkRect& srcRect, |
| 43 | const SkRect& transformedRect, |
| 44 | const SkMatrix& m, |
| 45 | bool isMSAA) { |
| 46 | // Only gets called if has_aligned_samples returned false. |
| 47 | // So we can assume that sampling is axis aligned but not texel aligned. |
| 48 | SkASSERT(!has_aligned_samples(srcRect, transformedRect)); |
| 49 | SkRect innerSrcRect(srcRect), innerTransformedRect, outerTransformedRect(transformedRect); |
| 50 | if (isMSAA) { |
| 51 | innerSrcRect.inset(SK_Scalar1, SK_Scalar1); |
| 52 | } else { |
| 53 | innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf); |
| 54 | } |
| 55 | m.mapRect(&innerTransformedRect, innerSrcRect); |
| 56 | |
| 57 | // The gap between outerTransformedRect and innerTransformedRect |
| 58 | // represents the projection of the source border area, which is |
| 59 | // problematic for color bleeding. We must check whether any |
| 60 | // destination pixels sample the border area. |
| 61 | outerTransformedRect.inset(kColorBleedTolerance, kColorBleedTolerance); |
| 62 | innerTransformedRect.outset(kColorBleedTolerance, kColorBleedTolerance); |
| 63 | SkIRect outer, inner; |
| 64 | outerTransformedRect.round(&outer); |
| 65 | innerTransformedRect.round(&inner); |
| 66 | // If the inner and outer rects round to the same result, it means the |
| 67 | // border does not overlap any pixel centers. Yay! |
| 68 | return inner != outer; |
| 69 | } |
| 70 | |
| 71 | static bool can_ignore_bilerp_constraint(const GrTextureProducer& producer, |
| 72 | const SkRect& srcRect, |
| 73 | const SkMatrix& srcRectToDeviceSpace, |
| 74 | bool isMSAA) { |
| 75 | if (srcRectToDeviceSpace.rectStaysRect()) { |
| 76 | // sampling is axis-aligned |
| 77 | SkRect transformedRect; |
| 78 | srcRectToDeviceSpace.mapRect(&transformedRect, srcRect); |
| 79 | |
| 80 | if (has_aligned_samples(srcRect, transformedRect) || |
| 81 | !may_color_bleed(srcRect, transformedRect, srcRectToDeviceSpace, isMSAA)) { |
| 82 | return true; |
| 83 | } |
| 84 | } |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | ////////////////////////////////////////////////////////////////////////////// |
| 89 | |
| 90 | void SkGpuDevice::drawTextureProducer(GrTextureProducer* producer, |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 91 | bool alphaOnly, |
| 92 | const SkRect* srcRect, |
| 93 | const SkRect* dstRect, |
| 94 | SkCanvas::SrcRectConstraint constraint, |
| 95 | const SkMatrix& viewMatrix, |
| 96 | const GrClip& clip, |
| 97 | const SkPaint& paint) { |
| 98 | // Figure out the actual dst and src rect by clipping the src rect to the bounds of the |
| 99 | // adjuster. If the src rect is clipped then the dst rect must be recomputed. Also determine |
| 100 | // the matrix that maps the src rect to the dst rect. |
| 101 | SkRect clippedSrcRect; |
| 102 | SkRect clippedDstRect; |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 103 | const SkRect srcBounds = SkRect::MakeIWH(producer->width(), producer->height()); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 104 | SkMatrix srcToDstMatrix; |
| 105 | if (srcRect) { |
| 106 | if (!dstRect) { |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 107 | dstRect = &srcBounds; |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 108 | } |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 109 | if (!srcBounds.contains(*srcRect)) { |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 110 | clippedSrcRect = *srcRect; |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 111 | if (!clippedSrcRect.intersect(srcBounds)) { |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 112 | return; |
| 113 | } |
| 114 | if (!srcToDstMatrix.setRectToRect(*srcRect, *dstRect, SkMatrix::kFill_ScaleToFit)) { |
| 115 | return; |
| 116 | } |
| 117 | srcToDstMatrix.mapRect(&clippedDstRect, clippedSrcRect); |
| 118 | } else { |
| 119 | clippedSrcRect = *srcRect; |
| 120 | clippedDstRect = *dstRect; |
| 121 | if (!srcToDstMatrix.setRectToRect(*srcRect, *dstRect, SkMatrix::kFill_ScaleToFit)) { |
| 122 | return; |
| 123 | } |
| 124 | } |
| 125 | } else { |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 126 | clippedSrcRect = srcBounds; |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 127 | if (dstRect) { |
| 128 | clippedDstRect = *dstRect; |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 129 | if (!srcToDstMatrix.setRectToRect(srcBounds, *dstRect, SkMatrix::kFill_ScaleToFit)) { |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 130 | return; |
| 131 | } |
| 132 | } else { |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 133 | clippedDstRect = srcBounds; |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 134 | srcToDstMatrix.reset(); |
| 135 | } |
| 136 | } |
| 137 | |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 138 | this->drawTextureProducerImpl(producer, alphaOnly, clippedSrcRect, clippedDstRect, constraint, |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 139 | viewMatrix, srcToDstMatrix, clip, paint); |
| 140 | } |
| 141 | |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 142 | void SkGpuDevice::drawTextureProducerImpl(GrTextureProducer* producer, |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 143 | bool alphaTexture, |
| 144 | const SkRect& clippedSrcRect, |
| 145 | const SkRect& clippedDstRect, |
| 146 | SkCanvas::SrcRectConstraint constraint, |
| 147 | const SkMatrix& viewMatrix, |
| 148 | const SkMatrix& srcToDstMatrix, |
| 149 | const GrClip& clip, |
| 150 | const SkPaint& paint) { |
| 151 | // Specifying the texture coords as local coordinates is an attempt to enable more batching |
| 152 | // by not baking anything about the srcRect, dstRect, or viewMatrix, into the texture FP. In |
| 153 | // the future this should be an opaque optimization enabled by the combination of batch/GP and |
| 154 | // FP. |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 155 | const SkMaskFilter* mf = paint.getMaskFilter(); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 156 | // The shader expects proper local coords, so we can't replace local coords with texture coords |
| 157 | // if the shader will be used. If we have a mask filter we will change the underlying geometry |
| 158 | // that is rendered. |
| 159 | bool canUseTextureCoordsAsLocalCoords = !use_shader(alphaTexture, paint) && !mf; |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 160 | |
| 161 | bool doBicubic; |
| 162 | GrTextureParams::FilterMode fm = |
| 163 | GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), viewMatrix, srcToDstMatrix, |
| 164 | &doBicubic); |
| 165 | const GrTextureParams::FilterMode* filterMode = doBicubic ? nullptr : &fm; |
| 166 | |
| 167 | GrTextureAdjuster::FilterConstraint constraintMode; |
| 168 | if (SkCanvas::kFast_SrcRectConstraint == constraint) { |
| 169 | constraintMode = GrTextureAdjuster::kNo_FilterConstraint; |
| 170 | } else { |
| 171 | constraintMode = GrTextureAdjuster::kYes_FilterConstraint; |
| 172 | } |
| 173 | |
| 174 | // If we have to outset for AA then we will generate texture coords outside the src rect. The |
| 175 | // same happens for any mask filter that extends the bounds rendered in the dst. |
| 176 | // This is conservative as a mask filter does not have to expand the bounds rendered. |
| 177 | bool coordsAllInsideSrcRect = !paint.isAntiAlias() && !mf; |
| 178 | |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 179 | // Check for optimization to drop the src rect constraint when on bilerp. |
| 180 | if (filterMode && GrTextureParams::kBilerp_FilterMode == *filterMode && |
| 181 | GrTextureAdjuster::kYes_FilterConstraint == constraintMode && coordsAllInsideSrcRect) { |
| 182 | SkMatrix combinedMatrix; |
| 183 | combinedMatrix.setConcat(viewMatrix, srcToDstMatrix); |
| 184 | if (can_ignore_bilerp_constraint(*producer, clippedSrcRect, combinedMatrix, |
| 185 | fRenderTarget->isUnifiedMultisampled())) { |
| 186 | constraintMode = GrTextureAdjuster::kNo_FilterConstraint; |
| 187 | } |
| 188 | } |
| 189 | |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 190 | const SkMatrix* textureMatrix; |
| 191 | SkMatrix tempMatrix; |
| 192 | if (canUseTextureCoordsAsLocalCoords) { |
| 193 | textureMatrix = &SkMatrix::I(); |
| 194 | } else { |
| 195 | if (!srcToDstMatrix.invert(&tempMatrix)) { |
| 196 | return; |
| 197 | } |
| 198 | textureMatrix = &tempMatrix; |
| 199 | } |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 200 | SkAutoTUnref<const GrFragmentProcessor> fp(producer->createFragmentProcessor( |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 201 | *textureMatrix, clippedSrcRect, constraintMode, coordsAllInsideSrcRect, filterMode)); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 202 | if (!fp) { |
| 203 | return; |
| 204 | } |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 205 | |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 206 | GrPaint grPaint; |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 207 | if (!SkPaintToGrPaintWithTexture(fContext, paint, viewMatrix, fp, alphaTexture, &grPaint)) { |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 208 | return; |
| 209 | } |
| 210 | |
| 211 | if (canUseTextureCoordsAsLocalCoords) { |
bsalomon | 3877ab8 | 2015-11-11 16:52:03 -0800 | [diff] [blame] | 212 | fDrawContext->fillRectToRect(clip, grPaint, viewMatrix, clippedDstRect, clippedSrcRect); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 213 | return; |
| 214 | } |
| 215 | |
| 216 | if (!mf) { |
| 217 | fDrawContext->drawRect(clip, grPaint, viewMatrix, clippedDstRect); |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | // First see if we can do the draw + mask filter direct to the dst. |
| 222 | SkStrokeRec rec(SkStrokeRec::kFill_InitStyle); |
| 223 | SkRRect rrect; |
| 224 | rrect.setRect(clippedDstRect); |
| 225 | if (mf->directFilterRRectMaskGPU(fContext->textureProvider(), |
| 226 | fDrawContext, |
| 227 | &grPaint, |
| 228 | clip, |
| 229 | viewMatrix, |
| 230 | rec, |
| 231 | rrect)) { |
| 232 | return; |
| 233 | } |
| 234 | SkPath rectPath; |
| 235 | rectPath.addRect(clippedDstRect); |
robertphillips | 0e7029e | 2015-11-30 05:45:06 -0800 | [diff] [blame] | 236 | rectPath.setIsVolatile(true); |
robertphillips | 7bceedc | 2015-12-01 12:51:26 -0800 | [diff] [blame] | 237 | GrBlurUtils::drawPathWithMaskFilter(this->context(), fDrawContext, fClip, |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 238 | rectPath, &grPaint, viewMatrix, mf, paint.getPathEffect(), |
robertphillips | 0e7029e | 2015-11-30 05:45:06 -0800 | [diff] [blame] | 239 | GrStrokeInfo::FillInfo(), true); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 240 | } |