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