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" |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 9 | #include "GrBlurUtils.h" |
| 10 | #include "GrCaps.h" |
Brian Osman | 1cb4171 | 2017-10-19 12:54:52 -0400 | [diff] [blame] | 11 | #include "GrColorSpaceXform.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" |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 15 | #include "GrTextureMaker.h" |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 16 | #include "SkDraw.h" |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 17 | #include "SkGr.h" |
Mike Reed | 80747ef | 2018-01-23 15:29:32 -0500 | [diff] [blame] | 18 | #include "SkMaskFilterBase.h" |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 19 | #include "effects/GrBicubicEffect.h" |
| 20 | #include "effects/GrSimpleTextureEffect.h" |
| 21 | #include "effects/GrTextureDomain.h" |
| 22 | |
| 23 | static inline bool use_shader(bool textureIsAlphaOnly, const SkPaint& paint) { |
| 24 | return textureIsAlphaOnly && paint.getShader(); |
| 25 | } |
| 26 | |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 27 | ////////////////////////////////////////////////////////////////////////////// |
| 28 | // Helper functions for dropping src rect constraint in bilerp mode. |
| 29 | |
| 30 | static const SkScalar kColorBleedTolerance = 0.001f; |
| 31 | |
| 32 | static bool has_aligned_samples(const SkRect& srcRect, const SkRect& transformedRect) { |
| 33 | // detect pixel disalignment |
Brian Salomon | a911f8f | 2015-11-18 15:19:57 -0500 | [diff] [blame] | 34 | if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) - transformedRect.left()) < kColorBleedTolerance && |
| 35 | SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) - transformedRect.top()) < kColorBleedTolerance && |
| 36 | SkScalarAbs(transformedRect.width() - srcRect.width()) < kColorBleedTolerance && |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 37 | SkScalarAbs(transformedRect.height() - srcRect.height()) < kColorBleedTolerance) { |
| 38 | return true; |
| 39 | } |
| 40 | return false; |
| 41 | } |
| 42 | |
| 43 | static bool may_color_bleed(const SkRect& srcRect, |
| 44 | const SkRect& transformedRect, |
| 45 | const SkMatrix& m, |
Brian Salomon | 7c8460e | 2017-05-12 11:36:10 -0400 | [diff] [blame] | 46 | GrFSAAType fsaaType) { |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 47 | // Only gets called if has_aligned_samples returned false. |
| 48 | // So we can assume that sampling is axis aligned but not texel aligned. |
| 49 | SkASSERT(!has_aligned_samples(srcRect, transformedRect)); |
| 50 | SkRect innerSrcRect(srcRect), innerTransformedRect, outerTransformedRect(transformedRect); |
Brian Salomon | 7c8460e | 2017-05-12 11:36:10 -0400 | [diff] [blame] | 51 | if (GrFSAAType::kUnifiedMSAA == fsaaType) { |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 52 | innerSrcRect.inset(SK_Scalar1, SK_Scalar1); |
| 53 | } else { |
| 54 | innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf); |
| 55 | } |
| 56 | m.mapRect(&innerTransformedRect, innerSrcRect); |
| 57 | |
| 58 | // The gap between outerTransformedRect and innerTransformedRect |
| 59 | // represents the projection of the source border area, which is |
| 60 | // problematic for color bleeding. We must check whether any |
| 61 | // destination pixels sample the border area. |
| 62 | outerTransformedRect.inset(kColorBleedTolerance, kColorBleedTolerance); |
| 63 | innerTransformedRect.outset(kColorBleedTolerance, kColorBleedTolerance); |
| 64 | SkIRect outer, inner; |
| 65 | outerTransformedRect.round(&outer); |
| 66 | innerTransformedRect.round(&inner); |
| 67 | // If the inner and outer rects round to the same result, it means the |
| 68 | // border does not overlap any pixel centers. Yay! |
| 69 | return inner != outer; |
| 70 | } |
| 71 | |
| 72 | static bool can_ignore_bilerp_constraint(const GrTextureProducer& producer, |
| 73 | const SkRect& srcRect, |
| 74 | const SkMatrix& srcRectToDeviceSpace, |
Brian Salomon | 7c8460e | 2017-05-12 11:36:10 -0400 | [diff] [blame] | 75 | GrFSAAType fsaaType) { |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 76 | if (srcRectToDeviceSpace.rectStaysRect()) { |
| 77 | // sampling is axis-aligned |
| 78 | SkRect transformedRect; |
| 79 | srcRectToDeviceSpace.mapRect(&transformedRect, srcRect); |
| 80 | |
| 81 | if (has_aligned_samples(srcRect, transformedRect) || |
Brian Salomon | 7c8460e | 2017-05-12 11:36:10 -0400 | [diff] [blame] | 82 | !may_color_bleed(srcRect, transformedRect, srcRectToDeviceSpace, fsaaType)) { |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 83 | return true; |
| 84 | } |
| 85 | } |
| 86 | return false; |
| 87 | } |
| 88 | |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 89 | /** |
Brian Salomon | b80ffee | 2018-05-23 16:39:39 -0400 | [diff] [blame^] | 90 | * Checks whether the paint is compatible with using GrRenderTargetContext::drawTexture. It is more |
| 91 | * efficient than the GrTextureProducer general case. |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 92 | */ |
Brian Salomon | b80ffee | 2018-05-23 16:39:39 -0400 | [diff] [blame^] | 93 | static bool can_use_draw_texture(const SkPaint& paint) { |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 94 | return (!paint.getColorFilter() && !paint.getShader() && !paint.getMaskFilter() && |
Brian Salomon | b5ef1f9 | 2018-01-11 11:46:21 -0500 | [diff] [blame] | 95 | !paint.getImageFilter() && paint.getFilterQuality() < kMedium_SkFilterQuality && |
Brian Salomon | b80ffee | 2018-05-23 16:39:39 -0400 | [diff] [blame^] | 96 | paint.getBlendMode() == SkBlendMode::kSrcOver); |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 97 | } |
| 98 | |
Brian Salomon | be3c1d2 | 2018-05-21 12:54:39 -0400 | [diff] [blame] | 99 | static void draw_texture(const SkPaint& paint, const SkMatrix& ctm, const SkRect* src, |
Brian Salomon | b80ffee | 2018-05-23 16:39:39 -0400 | [diff] [blame^] | 100 | const SkRect* dst, GrAA aa, SkCanvas::SrcRectConstraint constraint, |
| 101 | sk_sp<GrTextureProxy> proxy, |
| 102 | |
Brian Salomon | be3c1d2 | 2018-05-21 12:54:39 -0400 | [diff] [blame] | 103 | SkColorSpace* colorSpace, const GrClip& clip, GrRenderTargetContext* rtc) { |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 104 | SkASSERT(!(SkToBool(src) && !SkToBool(dst))); |
| 105 | SkRect srcRect = src ? *src : SkRect::MakeWH(proxy->width(), proxy->height()); |
| 106 | SkRect dstRect = dst ? *dst : srcRect; |
| 107 | if (src && !SkRect::MakeIWH(proxy->width(), proxy->height()).contains(srcRect)) { |
| 108 | // Shrink the src rect to be within bounds and proportionately shrink the dst rect. |
| 109 | SkMatrix srcToDst; |
| 110 | srcToDst.setRectToRect(srcRect, dstRect, SkMatrix::kFill_ScaleToFit); |
| 111 | SkAssertResult(srcRect.intersect(SkRect::MakeIWH(proxy->width(), proxy->height()))); |
| 112 | srcToDst.mapRect(&dstRect, srcRect); |
| 113 | } |
Brian Osman | f06ead9 | 2017-10-30 13:47:41 -0400 | [diff] [blame] | 114 | auto csxf = GrColorSpaceXform::Make(colorSpace, proxy->config(), |
| 115 | rtc->colorSpaceInfo().colorSpace()); |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 116 | GrSamplerState::Filter filter; |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 117 | switch (paint.getFilterQuality()) { |
| 118 | case kNone_SkFilterQuality: |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 119 | filter = GrSamplerState::Filter::kNearest; |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 120 | break; |
| 121 | case kLow_SkFilterQuality: |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 122 | filter = GrSamplerState::Filter::kBilerp; |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 123 | break; |
| 124 | case kMedium_SkFilterQuality: |
| 125 | case kHigh_SkFilterQuality: |
| 126 | SK_ABORT("Quality level not allowed."); |
| 127 | } |
| 128 | GrColor color = GrPixelConfigIsAlphaOnly(proxy->config()) |
| 129 | ? SkColorToPremulGrColor(paint.getColor()) |
| 130 | : SkColorAlphaToGrColor(paint.getColor()); |
Brian Salomon | b80ffee | 2018-05-23 16:39:39 -0400 | [diff] [blame^] | 131 | rtc->drawTexture(clip, std::move(proxy), filter, color, srcRect, dstRect, aa, constraint, ctm, |
Brian Salomon | be3c1d2 | 2018-05-21 12:54:39 -0400 | [diff] [blame] | 132 | std::move(csxf)); |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 133 | } |
| 134 | |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 135 | ////////////////////////////////////////////////////////////////////////////// |
| 136 | |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 137 | void SkGpuDevice::drawPinnedTextureProxy(sk_sp<GrTextureProxy> proxy, uint32_t pinnedUniqueID, |
| 138 | SkColorSpace* colorSpace, SkAlphaType alphaType, |
| 139 | const SkRect* srcRect, const SkRect* dstRect, |
| 140 | SkCanvas::SrcRectConstraint constraint, |
| 141 | const SkMatrix& viewMatrix, const SkPaint& paint) { |
Brian Salomon | 4e6cf91 | 2018-01-17 09:27:35 -0500 | [diff] [blame] | 142 | GrAA aa = GrAA(paint.isAntiAlias()); |
Brian Salomon | b80ffee | 2018-05-23 16:39:39 -0400 | [diff] [blame^] | 143 | if (can_use_draw_texture(paint)) { |
| 144 | draw_texture(paint, viewMatrix, srcRect, dstRect, aa, constraint, std::move(proxy), |
| 145 | colorSpace, this->clip(), fRenderTargetContext.get()); |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 146 | return; |
| 147 | } |
Greg Daniel | c77085d | 2017-11-01 16:38:48 -0400 | [diff] [blame] | 148 | GrTextureAdjuster adjuster(this->context(), std::move(proxy), alphaType, pinnedUniqueID, |
| 149 | colorSpace); |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 150 | this->drawTextureProducer(&adjuster, srcRect, dstRect, constraint, viewMatrix, paint); |
| 151 | } |
| 152 | |
| 153 | void SkGpuDevice::drawTextureMaker(GrTextureMaker* maker, int imageW, int imageH, |
| 154 | const SkRect* srcRect, const SkRect* dstRect, |
| 155 | SkCanvas::SrcRectConstraint constraint, |
| 156 | const SkMatrix& viewMatrix, const SkPaint& paint) { |
Brian Salomon | 4e6cf91 | 2018-01-17 09:27:35 -0500 | [diff] [blame] | 157 | GrAA aa = GrAA(paint.isAntiAlias()); |
Brian Salomon | b80ffee | 2018-05-23 16:39:39 -0400 | [diff] [blame^] | 158 | if (can_use_draw_texture(paint)) { |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 159 | sk_sp<SkColorSpace> cs; |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 160 | // We've done enough checks above to allow us to pass ClampNearest() and not check for |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 161 | // scaling adjustments. |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 162 | auto proxy = maker->refTextureProxyForParams( |
| 163 | GrSamplerState::ClampNearest(), fRenderTargetContext->colorSpaceInfo().colorSpace(), |
| 164 | &cs, nullptr); |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 165 | if (!proxy) { |
| 166 | return; |
| 167 | } |
Brian Salomon | b80ffee | 2018-05-23 16:39:39 -0400 | [diff] [blame^] | 168 | draw_texture(paint, viewMatrix, srcRect, dstRect, aa, constraint, std::move(proxy), |
| 169 | cs.get(), this->clip(), fRenderTargetContext.get()); |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 170 | return; |
| 171 | } |
| 172 | this->drawTextureProducer(maker, srcRect, dstRect, constraint, viewMatrix, paint); |
| 173 | } |
| 174 | |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 175 | void SkGpuDevice::drawTextureProducer(GrTextureProducer* producer, |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 176 | const SkRect* srcRect, |
| 177 | const SkRect* dstRect, |
| 178 | SkCanvas::SrcRectConstraint constraint, |
| 179 | const SkMatrix& viewMatrix, |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 180 | const SkPaint& paint) { |
ericrk | 369e937 | 2016-02-05 15:32:36 -0800 | [diff] [blame] | 181 | // This is the funnel for all non-tiled bitmap/image draw calls. Log a histogram entry. |
| 182 | SK_HISTOGRAM_BOOLEAN("DrawTiled", false); |
| 183 | |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 184 | // Figure out the actual dst and src rect by clipping the src rect to the bounds of the |
| 185 | // adjuster. If the src rect is clipped then the dst rect must be recomputed. Also determine |
| 186 | // the matrix that maps the src rect to the dst rect. |
| 187 | SkRect clippedSrcRect; |
| 188 | SkRect clippedDstRect; |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 189 | const SkRect srcBounds = SkRect::MakeIWH(producer->width(), producer->height()); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 190 | SkMatrix srcToDstMatrix; |
| 191 | if (srcRect) { |
| 192 | if (!dstRect) { |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 193 | dstRect = &srcBounds; |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 194 | } |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 195 | if (!srcBounds.contains(*srcRect)) { |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 196 | clippedSrcRect = *srcRect; |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 197 | if (!clippedSrcRect.intersect(srcBounds)) { |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 198 | return; |
| 199 | } |
| 200 | if (!srcToDstMatrix.setRectToRect(*srcRect, *dstRect, SkMatrix::kFill_ScaleToFit)) { |
| 201 | return; |
| 202 | } |
| 203 | srcToDstMatrix.mapRect(&clippedDstRect, clippedSrcRect); |
| 204 | } else { |
| 205 | clippedSrcRect = *srcRect; |
| 206 | clippedDstRect = *dstRect; |
| 207 | if (!srcToDstMatrix.setRectToRect(*srcRect, *dstRect, SkMatrix::kFill_ScaleToFit)) { |
| 208 | return; |
| 209 | } |
| 210 | } |
| 211 | } else { |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 212 | clippedSrcRect = srcBounds; |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 213 | if (dstRect) { |
| 214 | clippedDstRect = *dstRect; |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 215 | if (!srcToDstMatrix.setRectToRect(srcBounds, *dstRect, SkMatrix::kFill_ScaleToFit)) { |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 216 | return; |
| 217 | } |
| 218 | } else { |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 219 | clippedDstRect = srcBounds; |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 220 | srcToDstMatrix.reset(); |
| 221 | } |
| 222 | } |
| 223 | |
ericrk | 983294f | 2016-04-18 09:14:00 -0700 | [diff] [blame] | 224 | // Now that we have both the view and srcToDst matrices, log our scale factor. |
| 225 | LogDrawScaleFactor(SkMatrix::Concat(viewMatrix, srcToDstMatrix), paint.getFilterQuality()); |
| 226 | |
bsalomon | f1ecd21 | 2015-12-09 17:06:02 -0800 | [diff] [blame] | 227 | this->drawTextureProducerImpl(producer, clippedSrcRect, clippedDstRect, constraint, viewMatrix, |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 228 | srcToDstMatrix, paint); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 229 | } |
| 230 | |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 231 | void SkGpuDevice::drawTextureProducerImpl(GrTextureProducer* producer, |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 232 | const SkRect& clippedSrcRect, |
| 233 | const SkRect& clippedDstRect, |
| 234 | SkCanvas::SrcRectConstraint constraint, |
| 235 | const SkMatrix& viewMatrix, |
| 236 | const SkMatrix& srcToDstMatrix, |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 237 | const SkPaint& paint) { |
Brian Salomon | 09d994e | 2016-12-21 11:14:46 -0500 | [diff] [blame] | 238 | // Specifying the texture coords as local coordinates is an attempt to enable more GrDrawOp |
| 239 | // combining by not baking anything about the srcRect, dstRect, or viewMatrix, into the texture |
| 240 | // FP. In the future this should be an opaque optimization enabled by the combination of |
| 241 | // GrDrawOp/GP and FP. |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 242 | const SkMaskFilter* mf = paint.getMaskFilter(); |
Mike Reed | 547c859 | 2018-02-05 15:59:23 -0500 | [diff] [blame] | 243 | if (mf && as_MFB(mf)->hasFragmentProcessor()) { |
| 244 | mf = nullptr; |
| 245 | } |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 246 | // The shader expects proper local coords, so we can't replace local coords with texture coords |
| 247 | // if the shader will be used. If we have a mask filter we will change the underlying geometry |
| 248 | // that is rendered. |
bsalomon | f1ecd21 | 2015-12-09 17:06:02 -0800 | [diff] [blame] | 249 | bool canUseTextureCoordsAsLocalCoords = !use_shader(producer->isAlphaOnly(), paint) && !mf; |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 250 | |
| 251 | bool doBicubic; |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 252 | GrSamplerState::Filter fm = GrSkFilterQualityToGrFilterMode( |
Brian Osman | db78cba | 2018-02-15 10:09:48 -0500 | [diff] [blame] | 253 | paint.getFilterQuality(), viewMatrix, srcToDstMatrix, |
| 254 | fContext->contextPriv().sharpenMipmappedTextures(), &doBicubic); |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 255 | const GrSamplerState::Filter* filterMode = doBicubic ? nullptr : &fm; |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 256 | |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 257 | GrTextureProducer::FilterConstraint constraintMode; |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 258 | if (SkCanvas::kFast_SrcRectConstraint == constraint) { |
| 259 | constraintMode = GrTextureAdjuster::kNo_FilterConstraint; |
| 260 | } else { |
| 261 | constraintMode = GrTextureAdjuster::kYes_FilterConstraint; |
| 262 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 263 | |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 264 | // If we have to outset for AA then we will generate texture coords outside the src rect. The |
| 265 | // same happens for any mask filter that extends the bounds rendered in the dst. |
| 266 | // This is conservative as a mask filter does not have to expand the bounds rendered. |
| 267 | bool coordsAllInsideSrcRect = !paint.isAntiAlias() && !mf; |
| 268 | |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 269 | // Check for optimization to drop the src rect constraint when on bilerp. |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 270 | if (filterMode && GrSamplerState::Filter::kBilerp == *filterMode && |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 271 | GrTextureAdjuster::kYes_FilterConstraint == constraintMode && coordsAllInsideSrcRect) { |
| 272 | SkMatrix combinedMatrix; |
| 273 | combinedMatrix.setConcat(viewMatrix, srcToDstMatrix); |
| 274 | if (can_ignore_bilerp_constraint(*producer, clippedSrcRect, combinedMatrix, |
Brian Salomon | 7c8460e | 2017-05-12 11:36:10 -0400 | [diff] [blame] | 275 | fRenderTargetContext->fsaaType())) { |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 276 | constraintMode = GrTextureAdjuster::kNo_FilterConstraint; |
| 277 | } |
| 278 | } |
| 279 | |
bsalomon | 3aa5fce | 2015-11-12 09:59:44 -0800 | [diff] [blame] | 280 | const SkMatrix* textureMatrix; |
| 281 | SkMatrix tempMatrix; |
| 282 | if (canUseTextureCoordsAsLocalCoords) { |
| 283 | textureMatrix = &SkMatrix::I(); |
| 284 | } else { |
| 285 | if (!srcToDstMatrix.invert(&tempMatrix)) { |
| 286 | return; |
| 287 | } |
| 288 | textureMatrix = &tempMatrix; |
| 289 | } |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 290 | auto fp = producer->createFragmentProcessor( |
| 291 | *textureMatrix, clippedSrcRect, constraintMode, coordsAllInsideSrcRect, filterMode, |
| 292 | fRenderTargetContext->colorSpaceInfo().colorSpace()); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 293 | if (!fp) { |
| 294 | return; |
| 295 | } |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 296 | |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 297 | GrPaint grPaint; |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 298 | if (!SkPaintToGrPaintWithTexture(fContext.get(), fRenderTargetContext->colorSpaceInfo(), paint, |
| 299 | viewMatrix, std::move(fp), producer->isAlphaOnly(), |
| 300 | &grPaint)) { |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 301 | return; |
| 302 | } |
Chris Dalton | 3b51df1 | 2017-11-27 14:33:06 -0700 | [diff] [blame] | 303 | GrAA aa = GrAA(paint.isAntiAlias()); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 304 | if (canUseTextureCoordsAsLocalCoords) { |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 305 | fRenderTargetContext->fillRectToRect(this->clip(), std::move(grPaint), aa, viewMatrix, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 306 | clippedDstRect, clippedSrcRect); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 307 | return; |
| 308 | } |
| 309 | |
| 310 | if (!mf) { |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 311 | fRenderTargetContext->drawRect(this->clip(), std::move(grPaint), aa, viewMatrix, |
| 312 | clippedDstRect); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 313 | return; |
| 314 | } |
| 315 | |
| 316 | // 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] | 317 | if (viewMatrix.isScaleTranslate()) { |
| 318 | SkRect devClippedDstRect; |
| 319 | viewMatrix.mapRectScaleTranslate(&devClippedDstRect, clippedDstRect); |
| 320 | |
| 321 | SkStrokeRec rec(SkStrokeRec::kFill_InitStyle); |
Mike Reed | 80747ef | 2018-01-23 15:29:32 -0500 | [diff] [blame] | 322 | if (as_MFB(mf)->directFilterRRectMaskGPU(fContext.get(), |
| 323 | fRenderTargetContext.get(), |
| 324 | std::move(grPaint), |
| 325 | this->clip(), |
| 326 | viewMatrix, |
| 327 | rec, |
| 328 | SkRRect::MakeRect(clippedDstRect), |
| 329 | SkRRect::MakeRect(devClippedDstRect))) { |
robertphillips | 6cfb106 | 2016-08-22 16:13:48 -0700 | [diff] [blame] | 330 | return; |
| 331 | } |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 332 | } |
robertphillips | 6cfb106 | 2016-08-22 16:13:48 -0700 | [diff] [blame] | 333 | |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 334 | SkPath rectPath; |
| 335 | rectPath.addRect(clippedDstRect); |
robertphillips | 0e7029e | 2015-11-30 05:45:06 -0800 | [diff] [blame] | 336 | rectPath.setIsVolatile(true); |
Brian Salomon | 0166cfd | 2017-03-13 17:58:25 -0400 | [diff] [blame] | 337 | GrBlurUtils::drawPathWithMaskFilter(this->context(), fRenderTargetContext.get(), this->clip(), |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 338 | rectPath, std::move(grPaint), aa, viewMatrix, mf, |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 339 | GrStyle::SimpleFill(), true); |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 340 | } |