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