Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 "GrTextureProducer.h" |
Brian Salomon | c65aec9 | 2017-03-09 09:03:58 -0500 | [diff] [blame] | 9 | #include "GrClip.h" |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 10 | #include "GrProxyProvider.h" |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 11 | #include "GrRenderTargetContext.h" |
Robert Phillips | d9d8485 | 2017-06-09 10:48:29 -0400 | [diff] [blame] | 12 | #include "GrTextureProxy.h" |
Greg Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 13 | #include "SkGr.h" |
Mike Reed | 274218e | 2018-01-08 15:05:02 -0500 | [diff] [blame] | 14 | #include "SkRectPriv.h" |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 15 | #include "effects/GrBicubicEffect.h" |
| 16 | #include "effects/GrSimpleTextureEffect.h" |
| 17 | #include "effects/GrTextureDomain.h" |
| 18 | |
Robert Phillips | b66b42f | 2017-03-14 08:53:02 -0400 | [diff] [blame] | 19 | sk_sp<GrTextureProxy> GrTextureProducer::CopyOnGpu(GrContext* context, |
| 20 | sk_sp<GrTextureProxy> inputProxy, |
Greg Daniel | e1da1d9 | 2017-10-06 15:59:27 -0400 | [diff] [blame] | 21 | const CopyParams& copyParams, |
| 22 | bool dstWillRequireMipMaps) { |
Robert Phillips | b66b42f | 2017-03-14 08:53:02 -0400 | [diff] [blame] | 23 | SkASSERT(context); |
| 24 | |
Robert Phillips | b66b42f | 2017-03-14 08:53:02 -0400 | [diff] [blame] | 25 | const SkRect dstRect = SkRect::MakeIWH(copyParams.fWidth, copyParams.fHeight); |
Greg Daniel | 45d6303 | 2017-10-30 13:41:26 -0400 | [diff] [blame] | 26 | GrMipMapped mipMapped = dstWillRequireMipMaps ? GrMipMapped::kYes : GrMipMapped::kNo; |
Robert Phillips | b66b42f | 2017-03-14 08:53:02 -0400 | [diff] [blame] | 27 | |
Greg Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 28 | SkRect localRect = SkRect::MakeWH(inputProxy->width(), inputProxy->height()); |
| 29 | |
| 30 | bool needsDomain = false; |
| 31 | bool resizing = false; |
| 32 | if (copyParams.fFilter != GrSamplerState::Filter::kNearest) { |
| 33 | bool resizing = localRect.width() != dstRect.width() || |
| 34 | localRect.height() != dstRect.height(); |
| 35 | needsDomain = resizing && !GrProxyProvider::IsFunctionallyExact(inputProxy.get()); |
| 36 | } |
| 37 | |
| 38 | if (copyParams.fFilter == GrSamplerState::Filter::kNearest && !needsDomain && !resizing && |
| 39 | dstWillRequireMipMaps) { |
| 40 | sk_sp<GrTextureProxy> proxy = GrCopyBaseMipMapToTextureProxy(context, inputProxy.get()); |
| 41 | if (proxy) { |
| 42 | return proxy; |
| 43 | } |
| 44 | } |
| 45 | |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 46 | sk_sp<GrRenderTargetContext> copyRTC = |
| 47 | context->contextPriv().makeDeferredRenderTargetContextWithFallback( |
Brian Salomon | 366093f | 2018-02-13 09:25:22 -0500 | [diff] [blame] | 48 | SkBackingFit::kExact, dstRect.width(), dstRect.height(), inputProxy->config(), |
Brian Osman | 9363ac4 | 2018-06-01 16:10:53 -0400 | [diff] [blame] | 49 | nullptr, 1, mipMapped, inputProxy->origin()); |
Robert Phillips | b66b42f | 2017-03-14 08:53:02 -0400 | [diff] [blame] | 50 | if (!copyRTC) { |
| 51 | return nullptr; |
| 52 | } |
| 53 | |
| 54 | GrPaint paint; |
Robert Phillips | b66b42f | 2017-03-14 08:53:02 -0400 | [diff] [blame] | 55 | |
| 56 | if (needsDomain) { |
| 57 | const SkRect domain = localRect.makeInset(0.5f, 0.5f); |
| 58 | // This would cause us to read values from outside the subset. Surely, the caller knows |
| 59 | // better! |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 60 | SkASSERT(copyParams.fFilter != GrSamplerState::Filter::kMipMap); |
Robert Phillips | b66b42f | 2017-03-14 08:53:02 -0400 | [diff] [blame] | 61 | paint.addColorFragmentProcessor( |
Brian Osman | 2240be9 | 2017-10-18 13:15:13 -0400 | [diff] [blame] | 62 | GrTextureDomainEffect::Make(std::move(inputProxy), SkMatrix::I(), domain, |
| 63 | GrTextureDomain::kClamp_Mode, copyParams.fFilter)); |
Robert Phillips | b66b42f | 2017-03-14 08:53:02 -0400 | [diff] [blame] | 64 | } else { |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 65 | GrSamplerState samplerState(GrSamplerState::WrapMode::kClamp, copyParams.fFilter); |
Brian Osman | 2240be9 | 2017-10-18 13:15:13 -0400 | [diff] [blame] | 66 | paint.addColorTextureProcessor(std::move(inputProxy), SkMatrix::I(), samplerState); |
Robert Phillips | b66b42f | 2017-03-14 08:53:02 -0400 | [diff] [blame] | 67 | } |
| 68 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 69 | |
| 70 | copyRTC->fillRectToRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), dstRect, |
| 71 | localRect); |
| 72 | return copyRTC->asTextureProxyRef(); |
| 73 | } |
| 74 | |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 75 | /** Determines whether a texture domain is necessary and if so what domain to use. There are two |
| 76 | * rectangles to consider: |
Robert Phillips | 3798c86 | 2017-03-27 11:08:16 -0400 | [diff] [blame] | 77 | * - The first is the content area specified by the texture adjuster (i.e., textureContentArea). |
| 78 | * We can *never* allow filtering to cause bleed of pixels outside this rectangle. |
| 79 | * - The second rectangle is the constraint rectangle (i.e., constraintRect), which is known to |
| 80 | * be contained by the content area. The filterConstraint specifies whether we are allowed to |
| 81 | * bleed across this rect. |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 82 | * |
| 83 | * We want to avoid using a domain if possible. We consider the above rectangles, the filter type, |
| 84 | * and whether the coords generated by the draw would all fall within the constraint rect. If the |
| 85 | * latter is true we only need to consider whether the filter would extend beyond the rects. |
| 86 | */ |
| 87 | GrTextureProducer::DomainMode GrTextureProducer::DetermineDomainMode( |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 88 | const SkRect& constraintRect, |
| 89 | FilterConstraint filterConstraint, |
| 90 | bool coordsLimitedToConstraintRect, |
| 91 | GrTextureProxy* proxy, |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 92 | const GrSamplerState::Filter* filterModeOrNullForBicubic, |
| 93 | SkRect* domainRect) { |
Robert Phillips | 51e7ca3 | 2017-03-27 10:14:08 -0400 | [diff] [blame] | 94 | const SkIRect proxyBounds = SkIRect::MakeWH(proxy->width(), proxy->height()); |
| 95 | |
| 96 | SkASSERT(proxyBounds.contains(constraintRect)); |
Robert Phillips | 51e7ca3 | 2017-03-27 10:14:08 -0400 | [diff] [blame] | 97 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 98 | const bool proxyIsExact = GrProxyProvider::IsFunctionallyExact(proxy); |
Brian Salomon | 4df0092 | 2017-09-07 16:34:11 +0000 | [diff] [blame] | 99 | |
Robert Phillips | 51e7ca3 | 2017-03-27 10:14:08 -0400 | [diff] [blame] | 100 | // If the constraint rectangle contains the whole proxy then no need for a domain. |
| 101 | if (constraintRect.contains(proxyBounds) && proxyIsExact) { |
| 102 | return kNoDomain_DomainMode; |
| 103 | } |
| 104 | |
Robert Phillips | 51e7ca3 | 2017-03-27 10:14:08 -0400 | [diff] [blame] | 105 | bool restrictFilterToRect = (filterConstraint == GrTextureProducer::kYes_FilterConstraint); |
| 106 | |
| 107 | // If we can filter outside the constraint rect, and there is no non-content area of the |
| 108 | // proxy, and we aren't going to generate sample coords outside the constraint rect then we |
| 109 | // don't need a domain. |
Greg Daniel | c77085d | 2017-11-01 16:38:48 -0400 | [diff] [blame] | 110 | if (!restrictFilterToRect && proxyIsExact && coordsLimitedToConstraintRect) { |
Robert Phillips | 51e7ca3 | 2017-03-27 10:14:08 -0400 | [diff] [blame] | 111 | return kNoDomain_DomainMode; |
| 112 | } |
| 113 | |
| 114 | // Get the domain inset based on sampling mode (or bail if mipped) |
Brian Salomon | 4df0092 | 2017-09-07 16:34:11 +0000 | [diff] [blame] | 115 | SkScalar filterHalfWidth = 0.f; |
Robert Phillips | 51e7ca3 | 2017-03-27 10:14:08 -0400 | [diff] [blame] | 116 | if (filterModeOrNullForBicubic) { |
| 117 | switch (*filterModeOrNullForBicubic) { |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 118 | case GrSamplerState::Filter::kNearest: |
Robert Phillips | 51e7ca3 | 2017-03-27 10:14:08 -0400 | [diff] [blame] | 119 | if (coordsLimitedToConstraintRect) { |
| 120 | return kNoDomain_DomainMode; |
Brian Salomon | 4df0092 | 2017-09-07 16:34:11 +0000 | [diff] [blame] | 121 | } else { |
| 122 | filterHalfWidth = 0.f; |
Robert Phillips | 51e7ca3 | 2017-03-27 10:14:08 -0400 | [diff] [blame] | 123 | } |
| 124 | break; |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 125 | case GrSamplerState::Filter::kBilerp: |
Brian Salomon | 4df0092 | 2017-09-07 16:34:11 +0000 | [diff] [blame] | 126 | filterHalfWidth = .5f; |
Robert Phillips | 51e7ca3 | 2017-03-27 10:14:08 -0400 | [diff] [blame] | 127 | break; |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 128 | case GrSamplerState::Filter::kMipMap: |
Greg Daniel | c77085d | 2017-11-01 16:38:48 -0400 | [diff] [blame] | 129 | if (restrictFilterToRect || !proxyIsExact) { |
Brian Salomon | 4df0092 | 2017-09-07 16:34:11 +0000 | [diff] [blame] | 130 | // No domain can save us here. |
| 131 | return kTightCopy_DomainMode; |
| 132 | } |
| 133 | return kNoDomain_DomainMode; |
Robert Phillips | 51e7ca3 | 2017-03-27 10:14:08 -0400 | [diff] [blame] | 134 | } |
Brian Salomon | 4df0092 | 2017-09-07 16:34:11 +0000 | [diff] [blame] | 135 | } else { |
| 136 | // bicubic does nearest filtering internally. |
| 137 | filterHalfWidth = 1.5f; |
Robert Phillips | 51e7ca3 | 2017-03-27 10:14:08 -0400 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | // Both bilerp and bicubic use bilinear filtering and so need to be clamped to the center |
| 141 | // of the edge texel. Pinning to the texel center has no impact on nearest mode and MIP-maps |
| 142 | |
| 143 | static const SkScalar kDomainInset = 0.5f; |
| 144 | // Figure out the limits of pixels we're allowed to sample from. |
| 145 | // Unless we know the amount of outset and the texture matrix we have to conservatively enforce |
| 146 | // the domain. |
| 147 | if (restrictFilterToRect) { |
| 148 | *domainRect = constraintRect.makeInset(kDomainInset, kDomainInset); |
Greg Daniel | c77085d | 2017-11-01 16:38:48 -0400 | [diff] [blame] | 149 | } else if (!proxyIsExact) { |
| 150 | // If we got here then: proxy is not exact, the coords are limited to the |
Brian Salomon | 4df0092 | 2017-09-07 16:34:11 +0000 | [diff] [blame] | 151 | // constraint rect, and we're allowed to filter across the constraint rect boundary. So |
Greg Daniel | c77085d | 2017-11-01 16:38:48 -0400 | [diff] [blame] | 152 | // we check whether the filter would reach across the edge of the proxy. |
Brian Salomon | 4df0092 | 2017-09-07 16:34:11 +0000 | [diff] [blame] | 153 | // We will only set the sides that are required. |
| 154 | |
Mike Reed | 274218e | 2018-01-08 15:05:02 -0500 | [diff] [blame] | 155 | *domainRect = SkRectPriv::MakeLargest(); |
Brian Salomon | 4df0092 | 2017-09-07 16:34:11 +0000 | [diff] [blame] | 156 | if (coordsLimitedToConstraintRect) { |
| 157 | // We may be able to use the fact that the texture coords are limited to the constraint |
| 158 | // rect in order to avoid having to add a domain. |
| 159 | bool needContentAreaConstraint = false; |
Greg Daniel | c77085d | 2017-11-01 16:38:48 -0400 | [diff] [blame] | 160 | if (proxyBounds.fRight - filterHalfWidth < constraintRect.fRight) { |
| 161 | domainRect->fRight = proxyBounds.fRight - kDomainInset; |
Brian Salomon | 4df0092 | 2017-09-07 16:34:11 +0000 | [diff] [blame] | 162 | needContentAreaConstraint = true; |
| 163 | } |
Greg Daniel | c77085d | 2017-11-01 16:38:48 -0400 | [diff] [blame] | 164 | if (proxyBounds.fBottom - filterHalfWidth < constraintRect.fBottom) { |
| 165 | domainRect->fBottom = proxyBounds.fBottom - kDomainInset; |
Brian Salomon | 4df0092 | 2017-09-07 16:34:11 +0000 | [diff] [blame] | 166 | needContentAreaConstraint = true; |
| 167 | } |
| 168 | if (!needContentAreaConstraint) { |
| 169 | return kNoDomain_DomainMode; |
| 170 | } |
| 171 | } else { |
| 172 | // Our sample coords for the texture are allowed to be outside the constraintRect so we |
| 173 | // don't consider it when computing the domain. |
Greg Daniel | c77085d | 2017-11-01 16:38:48 -0400 | [diff] [blame] | 174 | domainRect->fRight = proxyBounds.fRight - kDomainInset; |
| 175 | domainRect->fBottom = proxyBounds.fBottom - kDomainInset; |
Robert Phillips | 51e7ca3 | 2017-03-27 10:14:08 -0400 | [diff] [blame] | 176 | } |
Brian Salomon | 4df0092 | 2017-09-07 16:34:11 +0000 | [diff] [blame] | 177 | } else { |
| 178 | return kNoDomain_DomainMode; |
Robert Phillips | 51e7ca3 | 2017-03-27 10:14:08 -0400 | [diff] [blame] | 179 | } |
Brian Salomon | 4df0092 | 2017-09-07 16:34:11 +0000 | [diff] [blame] | 180 | |
| 181 | if (domainRect->fLeft > domainRect->fRight) { |
| 182 | domainRect->fLeft = domainRect->fRight = SkScalarAve(domainRect->fLeft, domainRect->fRight); |
| 183 | } |
| 184 | if (domainRect->fTop > domainRect->fBottom) { |
| 185 | domainRect->fTop = domainRect->fBottom = SkScalarAve(domainRect->fTop, domainRect->fBottom); |
| 186 | } |
| 187 | return kDomain_DomainMode; |
Robert Phillips | 51e7ca3 | 2017-03-27 10:14:08 -0400 | [diff] [blame] | 188 | } |
| 189 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 190 | std::unique_ptr<GrFragmentProcessor> GrTextureProducer::CreateFragmentProcessorForDomainAndFilter( |
| 191 | sk_sp<GrTextureProxy> proxy, |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 192 | const SkMatrix& textureMatrix, |
| 193 | DomainMode domainMode, |
| 194 | const SkRect& domain, |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 195 | const GrSamplerState::Filter* filterOrNullForBicubic) { |
Robert Phillips | b66b42f | 2017-03-14 08:53:02 -0400 | [diff] [blame] | 196 | SkASSERT(kTightCopy_DomainMode != domainMode); |
| 197 | if (filterOrNullForBicubic) { |
| 198 | if (kDomain_DomainMode == domainMode) { |
Brian Osman | 2240be9 | 2017-10-18 13:15:13 -0400 | [diff] [blame] | 199 | return GrTextureDomainEffect::Make(std::move(proxy), textureMatrix, domain, |
Brian Osman | 5e34167 | 2017-10-18 10:23:18 -0400 | [diff] [blame] | 200 | GrTextureDomain::kClamp_Mode, |
Robert Phillips | b66b42f | 2017-03-14 08:53:02 -0400 | [diff] [blame] | 201 | *filterOrNullForBicubic); |
| 202 | } else { |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 203 | GrSamplerState samplerState(GrSamplerState::WrapMode::kClamp, *filterOrNullForBicubic); |
Brian Osman | 2240be9 | 2017-10-18 13:15:13 -0400 | [diff] [blame] | 204 | return GrSimpleTextureEffect::Make(std::move(proxy), textureMatrix, samplerState); |
Robert Phillips | b66b42f | 2017-03-14 08:53:02 -0400 | [diff] [blame] | 205 | } |
| 206 | } else { |
| 207 | if (kDomain_DomainMode == domainMode) { |
Brian Osman | 5e34167 | 2017-10-18 10:23:18 -0400 | [diff] [blame] | 208 | return GrBicubicEffect::Make(std::move(proxy), textureMatrix, domain); |
Robert Phillips | b66b42f | 2017-03-14 08:53:02 -0400 | [diff] [blame] | 209 | } else { |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 210 | static const GrSamplerState::WrapMode kClampClamp[] = { |
| 211 | GrSamplerState::WrapMode::kClamp, GrSamplerState::WrapMode::kClamp}; |
Brian Osman | 5e34167 | 2017-10-18 10:23:18 -0400 | [diff] [blame] | 212 | return GrBicubicEffect::Make(std::move(proxy), textureMatrix, kClampClamp); |
Robert Phillips | b66b42f | 2017-03-14 08:53:02 -0400 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | } |
Brian Salomon | 2a943df | 2018-05-04 13:43:19 -0400 | [diff] [blame] | 216 | |
| 217 | sk_sp<GrTextureProxy> GrTextureProducer::refTextureProxyForParams( |
| 218 | const GrSamplerState& sampler, |
| 219 | SkColorSpace* dstColorSpace, |
| 220 | sk_sp<SkColorSpace>* proxyColorSpace, |
| 221 | SkScalar scaleAdjust[2]) { |
| 222 | // Check that the caller pre-initialized scaleAdjust |
| 223 | SkASSERT(!scaleAdjust || (scaleAdjust[0] == 1 && scaleAdjust[1] == 1)); |
| 224 | // Check that if the caller passed nullptr for scaleAdjust that we're in the case where there |
| 225 | // can be no scaling. |
| 226 | SkDEBUGCODE(bool expectNoScale = (sampler.filter() != GrSamplerState::Filter::kMipMap && |
| 227 | !sampler.isRepeated())); |
| 228 | SkASSERT(scaleAdjust || expectNoScale); |
| 229 | auto result = |
| 230 | this->onRefTextureProxyForParams(sampler, dstColorSpace, proxyColorSpace, scaleAdjust); |
| 231 | |
| 232 | // Check that the "no scaling expected" case always returns a proxy of the same size as the |
| 233 | // producer. |
| 234 | SkASSERT(!result || !expectNoScale || |
| 235 | (result->width() == this->width() && result->height() == this->height())); |
| 236 | return result; |
| 237 | } |
Greg Daniel | 5f4b09d | 2018-06-12 16:39:59 -0400 | [diff] [blame] | 238 | |
| 239 | sk_sp<GrTextureProxy> GrTextureProducer::refTextureProxy(GrMipMapped willNeedMips, |
| 240 | SkColorSpace* dstColorSpace, |
| 241 | sk_sp<SkColorSpace>* proxyColorSpace) { |
| 242 | GrSamplerState::Filter filter = |
| 243 | GrMipMapped::kNo == willNeedMips ? GrSamplerState::Filter::kNearest |
| 244 | : GrSamplerState::Filter::kMipMap; |
| 245 | GrSamplerState sampler(GrSamplerState::WrapMode::kClamp, filter); |
| 246 | auto result = |
| 247 | this->onRefTextureProxyForParams(sampler, dstColorSpace, proxyColorSpace, nullptr); |
| 248 | |
| 249 | // Check that no scaling occured and we returned a proxy of the same size as the producer. |
| 250 | SkASSERT(!result || (result->width() == this->width() && result->height() == this->height())); |
| 251 | return result; |
| 252 | } |