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" |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 12 | #include "GrRenderTargetContext.h" |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 13 | #include "GrStyle.h" |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 14 | #include "GrTextureAdjuster.h" |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 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 | const SkRect* srcRect, |
| 92 | const SkRect* dstRect, |
| 93 | SkCanvas::SrcRectConstraint constraint, |
| 94 | const SkMatrix& viewMatrix, |
| 95 | const GrClip& clip, |
| 96 | const SkPaint& paint) { |
ericrk | 369e937 | 2016-02-05 15:32:36 -0800 | [diff] [blame] | 97 | // This is the funnel for all non-tiled bitmap/image draw calls. Log a histogram entry. |
| 98 | SK_HISTOGRAM_BOOLEAN("DrawTiled", false); |
| 99 | |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 100 | // Figure out the actual dst and src rect by clipping the src rect to the bounds of the |
| 101 | // adjuster. If the src rect is clipped then the dst rect must be recomputed. Also determine |
| 102 | // the matrix that maps the src rect to the dst rect. |
| 103 | SkRect clippedSrcRect; |
| 104 | SkRect clippedDstRect; |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 105 | const SkRect srcBounds = SkRect::MakeIWH(producer->width(), producer->height()); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 106 | SkMatrix srcToDstMatrix; |
| 107 | if (srcRect) { |
| 108 | if (!dstRect) { |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 109 | dstRect = &srcBounds; |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 110 | } |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 111 | if (!srcBounds.contains(*srcRect)) { |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 112 | clippedSrcRect = *srcRect; |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 113 | if (!clippedSrcRect.intersect(srcBounds)) { |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 114 | return; |
| 115 | } |
| 116 | if (!srcToDstMatrix.setRectToRect(*srcRect, *dstRect, SkMatrix::kFill_ScaleToFit)) { |
| 117 | return; |
| 118 | } |
| 119 | srcToDstMatrix.mapRect(&clippedDstRect, clippedSrcRect); |
| 120 | } else { |
| 121 | clippedSrcRect = *srcRect; |
| 122 | clippedDstRect = *dstRect; |
| 123 | if (!srcToDstMatrix.setRectToRect(*srcRect, *dstRect, SkMatrix::kFill_ScaleToFit)) { |
| 124 | return; |
| 125 | } |
| 126 | } |
| 127 | } else { |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 128 | clippedSrcRect = srcBounds; |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 129 | if (dstRect) { |
| 130 | clippedDstRect = *dstRect; |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 131 | if (!srcToDstMatrix.setRectToRect(srcBounds, *dstRect, SkMatrix::kFill_ScaleToFit)) { |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 132 | return; |
| 133 | } |
| 134 | } else { |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 135 | clippedDstRect = srcBounds; |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 136 | srcToDstMatrix.reset(); |
| 137 | } |
| 138 | } |
| 139 | |
ericrk | 983294f | 2016-04-18 09:14:00 -0700 | [diff] [blame] | 140 | // Now that we have both the view and srcToDst matrices, log our scale factor. |
| 141 | LogDrawScaleFactor(SkMatrix::Concat(viewMatrix, srcToDstMatrix), paint.getFilterQuality()); |
| 142 | |
bsalomon | f1ecd21 | 2015-12-09 17:06:02 -0800 | [diff] [blame] | 143 | this->drawTextureProducerImpl(producer, clippedSrcRect, clippedDstRect, constraint, viewMatrix, |
| 144 | srcToDstMatrix, clip, paint); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 145 | } |
| 146 | |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 147 | void SkGpuDevice::drawTextureProducerImpl(GrTextureProducer* producer, |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 148 | const SkRect& clippedSrcRect, |
| 149 | const SkRect& clippedDstRect, |
| 150 | SkCanvas::SrcRectConstraint constraint, |
| 151 | const SkMatrix& viewMatrix, |
| 152 | const SkMatrix& srcToDstMatrix, |
| 153 | const GrClip& clip, |
| 154 | const SkPaint& paint) { |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 155 | // Specifying the texture coords as local coordinates is an attempt to enable more GrDrawOp |
| 156 | // combining by not baking anything about the srcRect, dstRect, or viewMatrix, into the texture |
| 157 | // FP. In the future this should be an opaque optimization enabled by the combination of |
| 158 | // GrDrawOp/GP and FP. |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 159 | const SkMaskFilter* mf = paint.getMaskFilter(); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 160 | // The shader expects proper local coords, so we can't replace local coords with texture coords |
| 161 | // if the shader will be used. If we have a mask filter we will change the underlying geometry |
| 162 | // that is rendered. |
bsalomon | f1ecd21 | 2015-12-09 17:06:02 -0800 | [diff] [blame] | 163 | bool canUseTextureCoordsAsLocalCoords = !use_shader(producer->isAlphaOnly(), paint) && !mf; |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 164 | |
| 165 | bool doBicubic; |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 166 | GrSamplerParams::FilterMode fm = |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 167 | GrSkFilterQualityToGrFilterMode(paint.getFilterQuality(), viewMatrix, srcToDstMatrix, |
| 168 | &doBicubic); |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 169 | const GrSamplerParams::FilterMode* filterMode = doBicubic ? nullptr : &fm; |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 170 | |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 171 | GrTextureProducer::FilterConstraint constraintMode; |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 172 | if (SkCanvas::kFast_SrcRectConstraint == constraint) { |
| 173 | constraintMode = GrTextureAdjuster::kNo_FilterConstraint; |
| 174 | } else { |
| 175 | constraintMode = GrTextureAdjuster::kYes_FilterConstraint; |
| 176 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 177 | |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 178 | // If we have to outset for AA then we will generate texture coords outside the src rect. The |
| 179 | // same happens for any mask filter that extends the bounds rendered in the dst. |
| 180 | // This is conservative as a mask filter does not have to expand the bounds rendered. |
| 181 | bool coordsAllInsideSrcRect = !paint.isAntiAlias() && !mf; |
| 182 | |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 183 | // Check for optimization to drop the src rect constraint when on bilerp. |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 184 | if (filterMode && GrSamplerParams::kBilerp_FilterMode == *filterMode && |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 185 | GrTextureAdjuster::kYes_FilterConstraint == constraintMode && coordsAllInsideSrcRect) { |
| 186 | SkMatrix combinedMatrix; |
| 187 | combinedMatrix.setConcat(viewMatrix, srcToDstMatrix); |
| 188 | if (can_ignore_bilerp_constraint(*producer, clippedSrcRect, combinedMatrix, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 189 | fRenderTargetContext->isUnifiedMultisampled())) { |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 190 | constraintMode = GrTextureAdjuster::kNo_FilterConstraint; |
| 191 | } |
| 192 | } |
| 193 | |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 194 | const SkMatrix* textureMatrix; |
| 195 | SkMatrix tempMatrix; |
| 196 | if (canUseTextureCoordsAsLocalCoords) { |
| 197 | textureMatrix = &SkMatrix::I(); |
| 198 | } else { |
| 199 | if (!srcToDstMatrix.invert(&tempMatrix)) { |
| 200 | return; |
| 201 | } |
| 202 | textureMatrix = &tempMatrix; |
| 203 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 204 | sk_sp<GrFragmentProcessor> fp(producer->createFragmentProcessor( |
brianosman | 982eb7f | 2016-06-06 13:10:58 -0700 | [diff] [blame] | 205 | *textureMatrix, clippedSrcRect, constraintMode, coordsAllInsideSrcRect, filterMode, |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 206 | fRenderTargetContext->getColorSpace())); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 207 | if (!fp) { |
| 208 | return; |
| 209 | } |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 210 | |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 211 | GrPaint grPaint; |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 212 | if (!SkPaintToGrPaintWithTexture(fContext.get(), fRenderTargetContext.get(), paint, viewMatrix, |
| 213 | fp, producer->isAlphaOnly(), &grPaint)) { |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 214 | return; |
| 215 | } |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 216 | GrAA aa = GrBoolToAA(paint.isAntiAlias()); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 217 | if (canUseTextureCoordsAsLocalCoords) { |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 218 | fRenderTargetContext->fillRectToRect(clip, std::move(grPaint), aa, viewMatrix, |
| 219 | clippedDstRect, clippedSrcRect); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 220 | return; |
| 221 | } |
| 222 | |
| 223 | if (!mf) { |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 224 | fRenderTargetContext->drawRect(clip, std::move(grPaint), aa, viewMatrix, clippedDstRect); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 225 | return; |
| 226 | } |
| 227 | |
| 228 | // First see if we can do the draw + mask filter direct to the dst. |
robertphillips | 6cfb106 | 2016-08-22 16:13:48 -0700 | [diff] [blame] | 229 | if (viewMatrix.isScaleTranslate()) { |
| 230 | SkRect devClippedDstRect; |
| 231 | viewMatrix.mapRectScaleTranslate(&devClippedDstRect, clippedDstRect); |
| 232 | |
| 233 | SkStrokeRec rec(SkStrokeRec::kFill_InitStyle); |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 234 | if (mf->directFilterRRectMaskGPU(fContext.get(), |
| 235 | fRenderTargetContext.get(), |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 236 | std::move(grPaint), |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 237 | clip, |
| 238 | viewMatrix, |
| 239 | rec, |
| 240 | SkRRect::MakeRect(clippedDstRect), |
| 241 | SkRRect::MakeRect(devClippedDstRect))) { |
robertphillips | 6cfb106 | 2016-08-22 16:13:48 -0700 | [diff] [blame] | 242 | return; |
| 243 | } |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 244 | } |
robertphillips | 6cfb106 | 2016-08-22 16:13:48 -0700 | [diff] [blame] | 245 | |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 246 | SkPath rectPath; |
| 247 | rectPath.addRect(clippedDstRect); |
robertphillips | 0e7029e | 2015-11-30 05:45:06 -0800 | [diff] [blame] | 248 | rectPath.setIsVolatile(true); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 249 | GrBlurUtils::drawPathWithMaskFilter(this->context(), fRenderTargetContext.get(), fClip, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 250 | rectPath, std::move(grPaint), aa, viewMatrix, mf, |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 251 | GrStyle::SimpleFill(), true); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 252 | } |