blob: cfc2c8ddc1344381f550687b03ee320225821856 [file] [log] [blame]
Brian Osmane8e54582016-11-28 10:06:27 -05001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/private/GrRecordingContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "src/core/SkMipMap.h"
10#include "src/core/SkRectPriv.h"
11#include "src/gpu/GrClip.h"
12#include "src/gpu/GrContextPriv.h"
13#include "src/gpu/GrProxyProvider.h"
14#include "src/gpu/GrRecordingContextPriv.h"
15#include "src/gpu/GrRenderTargetContext.h"
16#include "src/gpu/GrTextureProducer.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040017#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/SkGr.h"
19#include "src/gpu/effects/GrBicubicEffect.h"
20#include "src/gpu/effects/GrTextureDomain.h"
21#include "src/gpu/effects/generated/GrSimpleTextureEffect.h"
Brian Osmane8e54582016-11-28 10:06:27 -050022
Robert Phillips9338c602019-02-19 12:52:29 -050023sk_sp<GrTextureProxy> GrTextureProducer::CopyOnGpu(GrRecordingContext* context,
Robert Phillipsb66b42f2017-03-14 08:53:02 -040024 sk_sp<GrTextureProxy> inputProxy,
Brian Salomond6287472019-06-24 15:50:07 -040025 GrColorType colorType,
Greg Daniele1da1d92017-10-06 15:59:27 -040026 const CopyParams& copyParams,
27 bool dstWillRequireMipMaps) {
Robert Phillipsb66b42f2017-03-14 08:53:02 -040028 SkASSERT(context);
29
Brian Salomon1a217eb2019-10-24 10:50:36 -040030 const SkRect dstRect = SkRect::Make(copyParams.fDimensions);
Greg Daniel45d63032017-10-30 13:41:26 -040031 GrMipMapped mipMapped = dstWillRequireMipMaps ? GrMipMapped::kYes : GrMipMapped::kNo;
Robert Phillipsb66b42f2017-03-14 08:53:02 -040032
Brian Salomon9f2b86c2019-10-22 10:37:46 -040033 SkRect localRect = inputProxy->getBoundsRect();
Greg Daniel09c94002018-06-08 22:11:51 +000034
35 bool needsDomain = false;
36 bool resizing = false;
37 if (copyParams.fFilter != GrSamplerState::Filter::kNearest) {
38 bool resizing = localRect.width() != dstRect.width() ||
39 localRect.height() != dstRect.height();
40 needsDomain = resizing && !GrProxyProvider::IsFunctionallyExact(inputProxy.get());
41 }
42
43 if (copyParams.fFilter == GrSamplerState::Filter::kNearest && !needsDomain && !resizing &&
44 dstWillRequireMipMaps) {
Greg Danielc594e622019-10-15 14:01:49 -040045 sk_sp<GrTextureProxy> proxy = GrCopyBaseMipMapToTextureProxy(context, inputProxy.get(),
46 colorType);
Greg Daniel09c94002018-06-08 22:11:51 +000047 if (proxy) {
48 return proxy;
49 }
50 }
51
Brian Salomonbf6b9792019-08-21 09:38:10 -040052 auto copyRTC = context->priv().makeDeferredRenderTargetContextWithFallback(
53 SkBackingFit::kExact, dstRect.width(), dstRect.height(), colorType, nullptr, 1,
54 mipMapped, inputProxy->origin());
Robert Phillipsb66b42f2017-03-14 08:53:02 -040055 if (!copyRTC) {
56 return nullptr;
57 }
58
59 GrPaint paint;
Robert Phillipsb66b42f2017-03-14 08:53:02 -040060
61 if (needsDomain) {
62 const SkRect domain = localRect.makeInset(0.5f, 0.5f);
63 // This would cause us to read values from outside the subset. Surely, the caller knows
64 // better!
Brian Salomon2bbdcc42017-09-07 12:36:34 -040065 SkASSERT(copyParams.fFilter != GrSamplerState::Filter::kMipMap);
Brian Salomon078e8fa2019-11-22 04:10:18 +000066 paint.addColorFragmentProcessor(
67 GrTextureDomainEffect::Make(std::move(inputProxy), colorType, SkMatrix::I(), domain,
68 GrTextureDomain::kClamp_Mode, copyParams.fFilter));
Robert Phillipsb66b42f2017-03-14 08:53:02 -040069 } else {
Brian Salomon2bbdcc42017-09-07 12:36:34 -040070 GrSamplerState samplerState(GrSamplerState::WrapMode::kClamp, copyParams.fFilter);
Brian Salomon078e8fa2019-11-22 04:10:18 +000071 paint.addColorTextureProcessor(std::move(inputProxy), colorType, SkMatrix::I(),
Greg Danielc594e622019-10-15 14:01:49 -040072 samplerState);
Robert Phillipsb66b42f2017-03-14 08:53:02 -040073 }
74 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
75
76 copyRTC->fillRectToRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), dstRect,
77 localRect);
78 return copyRTC->asTextureProxyRef();
79}
80
Brian Osmane8e54582016-11-28 10:06:27 -050081/** Determines whether a texture domain is necessary and if so what domain to use. There are two
82 * rectangles to consider:
Robert Phillips3798c862017-03-27 11:08:16 -040083 * - The first is the content area specified by the texture adjuster (i.e., textureContentArea).
84 * We can *never* allow filtering to cause bleed of pixels outside this rectangle.
85 * - The second rectangle is the constraint rectangle (i.e., constraintRect), which is known to
86 * be contained by the content area. The filterConstraint specifies whether we are allowed to
87 * bleed across this rect.
Brian Osmane8e54582016-11-28 10:06:27 -050088 *
89 * We want to avoid using a domain if possible. We consider the above rectangles, the filter type,
90 * and whether the coords generated by the draw would all fall within the constraint rect. If the
91 * latter is true we only need to consider whether the filter would extend beyond the rects.
92 */
93GrTextureProducer::DomainMode GrTextureProducer::DetermineDomainMode(
Brian Salomon2bbdcc42017-09-07 12:36:34 -040094 const SkRect& constraintRect,
95 FilterConstraint filterConstraint,
96 bool coordsLimitedToConstraintRect,
97 GrTextureProxy* proxy,
Brian Salomon2bbdcc42017-09-07 12:36:34 -040098 const GrSamplerState::Filter* filterModeOrNullForBicubic,
99 SkRect* domainRect) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400100 const SkIRect proxyBounds = SkIRect::MakeSize(proxy->dimensions());
Robert Phillips51e7ca32017-03-27 10:14:08 -0400101
102 SkASSERT(proxyBounds.contains(constraintRect));
Robert Phillips51e7ca32017-03-27 10:14:08 -0400103
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500104 const bool proxyIsExact = GrProxyProvider::IsFunctionallyExact(proxy);
Brian Salomon4df00922017-09-07 16:34:11 +0000105
Robert Phillips51e7ca32017-03-27 10:14:08 -0400106 // If the constraint rectangle contains the whole proxy then no need for a domain.
107 if (constraintRect.contains(proxyBounds) && proxyIsExact) {
108 return kNoDomain_DomainMode;
109 }
110
Robert Phillips51e7ca32017-03-27 10:14:08 -0400111 bool restrictFilterToRect = (filterConstraint == GrTextureProducer::kYes_FilterConstraint);
112
113 // If we can filter outside the constraint rect, and there is no non-content area of the
114 // proxy, and we aren't going to generate sample coords outside the constraint rect then we
115 // don't need a domain.
Greg Danielc77085d2017-11-01 16:38:48 -0400116 if (!restrictFilterToRect && proxyIsExact && coordsLimitedToConstraintRect) {
Robert Phillips51e7ca32017-03-27 10:14:08 -0400117 return kNoDomain_DomainMode;
118 }
119
120 // Get the domain inset based on sampling mode (or bail if mipped)
Brian Salomon4df00922017-09-07 16:34:11 +0000121 SkScalar filterHalfWidth = 0.f;
Robert Phillips51e7ca32017-03-27 10:14:08 -0400122 if (filterModeOrNullForBicubic) {
123 switch (*filterModeOrNullForBicubic) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400124 case GrSamplerState::Filter::kNearest:
Robert Phillips51e7ca32017-03-27 10:14:08 -0400125 if (coordsLimitedToConstraintRect) {
126 return kNoDomain_DomainMode;
Brian Salomon4df00922017-09-07 16:34:11 +0000127 } else {
128 filterHalfWidth = 0.f;
Robert Phillips51e7ca32017-03-27 10:14:08 -0400129 }
130 break;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400131 case GrSamplerState::Filter::kBilerp:
Brian Salomon4df00922017-09-07 16:34:11 +0000132 filterHalfWidth = .5f;
Robert Phillips51e7ca32017-03-27 10:14:08 -0400133 break;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400134 case GrSamplerState::Filter::kMipMap:
Greg Danielc77085d2017-11-01 16:38:48 -0400135 if (restrictFilterToRect || !proxyIsExact) {
Brian Salomon4df00922017-09-07 16:34:11 +0000136 // No domain can save us here.
137 return kTightCopy_DomainMode;
138 }
139 return kNoDomain_DomainMode;
Robert Phillips51e7ca32017-03-27 10:14:08 -0400140 }
Brian Salomon4df00922017-09-07 16:34:11 +0000141 } else {
142 // bicubic does nearest filtering internally.
143 filterHalfWidth = 1.5f;
Robert Phillips51e7ca32017-03-27 10:14:08 -0400144 }
145
146 // Both bilerp and bicubic use bilinear filtering and so need to be clamped to the center
147 // of the edge texel. Pinning to the texel center has no impact on nearest mode and MIP-maps
148
149 static const SkScalar kDomainInset = 0.5f;
150 // Figure out the limits of pixels we're allowed to sample from.
151 // Unless we know the amount of outset and the texture matrix we have to conservatively enforce
152 // the domain.
153 if (restrictFilterToRect) {
154 *domainRect = constraintRect.makeInset(kDomainInset, kDomainInset);
Greg Danielc77085d2017-11-01 16:38:48 -0400155 } else if (!proxyIsExact) {
156 // If we got here then: proxy is not exact, the coords are limited to the
Brian Salomon4df00922017-09-07 16:34:11 +0000157 // constraint rect, and we're allowed to filter across the constraint rect boundary. So
Greg Danielc77085d2017-11-01 16:38:48 -0400158 // we check whether the filter would reach across the edge of the proxy.
Brian Salomon4df00922017-09-07 16:34:11 +0000159 // We will only set the sides that are required.
160
Mike Reed274218e2018-01-08 15:05:02 -0500161 *domainRect = SkRectPriv::MakeLargest();
Brian Salomon4df00922017-09-07 16:34:11 +0000162 if (coordsLimitedToConstraintRect) {
163 // We may be able to use the fact that the texture coords are limited to the constraint
164 // rect in order to avoid having to add a domain.
165 bool needContentAreaConstraint = false;
Greg Danielc77085d2017-11-01 16:38:48 -0400166 if (proxyBounds.fRight - filterHalfWidth < constraintRect.fRight) {
167 domainRect->fRight = proxyBounds.fRight - kDomainInset;
Brian Salomon4df00922017-09-07 16:34:11 +0000168 needContentAreaConstraint = true;
169 }
Greg Danielc77085d2017-11-01 16:38:48 -0400170 if (proxyBounds.fBottom - filterHalfWidth < constraintRect.fBottom) {
171 domainRect->fBottom = proxyBounds.fBottom - kDomainInset;
Brian Salomon4df00922017-09-07 16:34:11 +0000172 needContentAreaConstraint = true;
173 }
174 if (!needContentAreaConstraint) {
175 return kNoDomain_DomainMode;
176 }
177 } else {
178 // Our sample coords for the texture are allowed to be outside the constraintRect so we
179 // don't consider it when computing the domain.
Greg Danielc77085d2017-11-01 16:38:48 -0400180 domainRect->fRight = proxyBounds.fRight - kDomainInset;
181 domainRect->fBottom = proxyBounds.fBottom - kDomainInset;
Robert Phillips51e7ca32017-03-27 10:14:08 -0400182 }
Brian Salomon4df00922017-09-07 16:34:11 +0000183 } else {
184 return kNoDomain_DomainMode;
Robert Phillips51e7ca32017-03-27 10:14:08 -0400185 }
Brian Salomon4df00922017-09-07 16:34:11 +0000186
187 if (domainRect->fLeft > domainRect->fRight) {
188 domainRect->fLeft = domainRect->fRight = SkScalarAve(domainRect->fLeft, domainRect->fRight);
189 }
190 if (domainRect->fTop > domainRect->fBottom) {
191 domainRect->fTop = domainRect->fBottom = SkScalarAve(domainRect->fTop, domainRect->fBottom);
192 }
193 return kDomain_DomainMode;
Robert Phillips51e7ca32017-03-27 10:14:08 -0400194}
195
Michael Ludwigddeed372019-02-20 16:50:10 -0500196std::unique_ptr<GrFragmentProcessor> GrTextureProducer::createFragmentProcessorForDomainAndFilter(
Brian Salomonaff329b2017-08-11 09:40:37 -0400197 sk_sp<GrTextureProxy> proxy,
Brian Salomonaff329b2017-08-11 09:40:37 -0400198 const SkMatrix& textureMatrix,
199 DomainMode domainMode,
200 const SkRect& domain,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400201 const GrSamplerState::Filter* filterOrNullForBicubic) {
Robert Phillipsb66b42f2017-03-14 08:53:02 -0400202 SkASSERT(kTightCopy_DomainMode != domainMode);
Michael Ludwigddeed372019-02-20 16:50:10 -0500203 bool clampToBorderSupport = fContext->priv().caps()->clampToBorderSupport();
Brian Salomon078e8fa2019-11-22 04:10:18 +0000204 GrColorType srcColorType = this->colorType();
Robert Phillipsb66b42f2017-03-14 08:53:02 -0400205 if (filterOrNullForBicubic) {
Michael Ludwigddeed372019-02-20 16:50:10 -0500206 if (kDomain_DomainMode == domainMode || (fDomainNeedsDecal && !clampToBorderSupport)) {
207 GrTextureDomain::Mode wrapMode = fDomainNeedsDecal ? GrTextureDomain::kDecal_Mode
208 : GrTextureDomain::kClamp_Mode;
Brian Salomon078e8fa2019-11-22 04:10:18 +0000209 return GrTextureDomainEffect::Make(std::move(proxy), srcColorType, textureMatrix,
Michael Ludwig170de012019-11-15 21:55:18 +0000210 domain, wrapMode, *filterOrNullForBicubic);
Robert Phillipsb66b42f2017-03-14 08:53:02 -0400211 } else {
Michael Ludwigddeed372019-02-20 16:50:10 -0500212 GrSamplerState::WrapMode wrapMode =
213 fDomainNeedsDecal ? GrSamplerState::WrapMode::kClampToBorder
214 : GrSamplerState::WrapMode::kClamp;
215 GrSamplerState samplerState(wrapMode, *filterOrNullForBicubic);
Brian Salomon078e8fa2019-11-22 04:10:18 +0000216 return GrSimpleTextureEffect::Make(std::move(proxy), srcColorType, textureMatrix,
Greg Danielc594e622019-10-15 14:01:49 -0400217 samplerState);
Robert Phillipsb66b42f2017-03-14 08:53:02 -0400218 }
219 } else {
Michael Ludwigddeed372019-02-20 16:50:10 -0500220 static const GrSamplerState::WrapMode kClampClamp[] = {
221 GrSamplerState::WrapMode::kClamp, GrSamplerState::WrapMode::kClamp};
222 static const GrSamplerState::WrapMode kDecalDecal[] = {
223 GrSamplerState::WrapMode::kClampToBorder, GrSamplerState::WrapMode::kClampToBorder};
224
Brian Salomona86fc7a2019-05-28 20:42:58 -0400225 static constexpr auto kDir = GrBicubicEffect::Direction::kXY;
Michael Ludwigddeed372019-02-20 16:50:10 -0500226 if (kDomain_DomainMode == domainMode || (fDomainNeedsDecal && !clampToBorderSupport)) {
227 GrTextureDomain::Mode wrapMode = fDomainNeedsDecal ? GrTextureDomain::kDecal_Mode
228 : GrTextureDomain::kClamp_Mode;
Brian Salomon078e8fa2019-11-22 04:10:18 +0000229 return GrBicubicEffect::Make(std::move(proxy), srcColorType, textureMatrix, kClampClamp,
230 wrapMode, wrapMode, kDir, this->alphaType(),
Michael Ludwigddeed372019-02-20 16:50:10 -0500231 kDomain_DomainMode == domainMode ? &domain : nullptr);
Robert Phillipsb66b42f2017-03-14 08:53:02 -0400232 } else {
Brian Salomon078e8fa2019-11-22 04:10:18 +0000233 return GrBicubicEffect::Make(std::move(proxy), srcColorType, textureMatrix,
Brian Salomon1127c0b2019-06-13 20:22:10 +0000234 fDomainNeedsDecal ? kDecalDecal : kClampClamp, kDir,
Brian Salomon078e8fa2019-11-22 04:10:18 +0000235 this->alphaType());
Robert Phillipsb66b42f2017-03-14 08:53:02 -0400236 }
237 }
238}
Brian Salomon2a943df2018-05-04 13:43:19 -0400239
240sk_sp<GrTextureProxy> GrTextureProducer::refTextureProxyForParams(
Michael Ludwigddeed372019-02-20 16:50:10 -0500241 const GrSamplerState::Filter* filterOrNullForBicubic,
242 SkScalar scaleAdjust[2]) {
243 GrSamplerState sampler; // Default is nearest + clamp
244 if (filterOrNullForBicubic) {
245 sampler.setFilterMode(*filterOrNullForBicubic);
246 }
247 if (fDomainNeedsDecal) {
248 // Assuming hardware support, switch to clamp-to-border instead of clamp
249 if (fContext->priv().caps()->clampToBorderSupport()) {
250 sampler.setWrapModeX(GrSamplerState::WrapMode::kClampToBorder);
251 sampler.setWrapModeY(GrSamplerState::WrapMode::kClampToBorder);
252 }
253 }
254 return this->refTextureProxyForParams(sampler, scaleAdjust);
255}
256
257sk_sp<GrTextureProxy> GrTextureProducer::refTextureProxyForParams(
Brian Salomon2a943df2018-05-04 13:43:19 -0400258 const GrSamplerState& sampler,
Brian Salomon2a943df2018-05-04 13:43:19 -0400259 SkScalar scaleAdjust[2]) {
260 // Check that the caller pre-initialized scaleAdjust
261 SkASSERT(!scaleAdjust || (scaleAdjust[0] == 1 && scaleAdjust[1] == 1));
262 // Check that if the caller passed nullptr for scaleAdjust that we're in the case where there
263 // can be no scaling.
264 SkDEBUGCODE(bool expectNoScale = (sampler.filter() != GrSamplerState::Filter::kMipMap &&
265 !sampler.isRepeated()));
266 SkASSERT(scaleAdjust || expectNoScale);
Greg Daniel8e9b4c42018-07-20 10:30:48 -0400267
268 int mipCount = SkMipMap::ComputeLevelCount(this->width(), this->height());
269 bool willBeMipped = GrSamplerState::Filter::kMipMap == sampler.filter() && mipCount &&
Robert Phillips9338c602019-02-19 12:52:29 -0500270 this->context()->priv().caps()->mipMapSupport();
Greg Daniel8e9b4c42018-07-20 10:30:48 -0400271
Brian Osman6064e1c2018-10-19 14:27:54 -0400272 auto result = this->onRefTextureProxyForParams(sampler, willBeMipped, scaleAdjust);
Brian Salomon2a943df2018-05-04 13:43:19 -0400273
Greg Daniel022b1e02018-07-20 14:54:00 -0400274 // Check to make sure that if we say the texture willBeMipped that the returned texture has mip
275 // maps, unless the config is not copyable.
276 SkASSERT(!result || !willBeMipped || result->mipMapped() == GrMipMapped::kYes ||
Greg Daniel6980c4e2019-07-30 16:18:18 -0400277 !this->context()->priv().caps()->isFormatCopyable(result->backendFormat()));
Greg Daniel022b1e02018-07-20 14:54:00 -0400278
Brian Salomon2a943df2018-05-04 13:43:19 -0400279 // Check that the "no scaling expected" case always returns a proxy of the same size as the
280 // producer.
Brian Salomon1a217eb2019-10-24 10:50:36 -0400281 SkASSERT(!result || !expectNoScale || result->dimensions() == this->dimensions());
Brian Salomon2a943df2018-05-04 13:43:19 -0400282 return result;
283}
Greg Daniel5f4b09d2018-06-12 16:39:59 -0400284
Brian Osman6064e1c2018-10-19 14:27:54 -0400285sk_sp<GrTextureProxy> GrTextureProducer::refTextureProxy(GrMipMapped willNeedMips) {
Greg Daniel5f4b09d2018-06-12 16:39:59 -0400286 GrSamplerState::Filter filter =
287 GrMipMapped::kNo == willNeedMips ? GrSamplerState::Filter::kNearest
288 : GrSamplerState::Filter::kMipMap;
289 GrSamplerState sampler(GrSamplerState::WrapMode::kClamp, filter);
Greg Daniel8e9b4c42018-07-20 10:30:48 -0400290
291 int mipCount = SkMipMap::ComputeLevelCount(this->width(), this->height());
292 bool willBeMipped = GrSamplerState::Filter::kMipMap == sampler.filter() && mipCount &&
Robert Phillips9338c602019-02-19 12:52:29 -0500293 this->context()->priv().caps()->mipMapSupport();
Greg Daniel8e9b4c42018-07-20 10:30:48 -0400294
Brian Osman6064e1c2018-10-19 14:27:54 -0400295 auto result = this->onRefTextureProxyForParams(sampler, willBeMipped, nullptr);
Greg Daniel5f4b09d2018-06-12 16:39:59 -0400296
Greg Daniel022b1e02018-07-20 14:54:00 -0400297 // Check to make sure that if we say the texture willBeMipped that the returned texture has mip
298 // maps, unless the config is not copyable.
299 SkASSERT(!result || !willBeMipped || result->mipMapped() == GrMipMapped::kYes ||
Greg Daniel6980c4e2019-07-30 16:18:18 -0400300 !this->context()->priv().caps()->isFormatCopyable(result->backendFormat()));
Greg Daniel022b1e02018-07-20 14:54:00 -0400301
Greg Daniel5f4b09d2018-06-12 16:39:59 -0400302 // Check that no scaling occured and we returned a proxy of the same size as the producer.
Brian Salomon1a217eb2019-10-24 10:50:36 -0400303 SkASSERT(!result || result->dimensions() == this->dimensions());
Greg Daniel5f4b09d2018-06-12 16:39:59 -0400304 return result;
305}