blob: 73f71135d185feed252ff439ed5ca38afeb19f11 [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
Robert Phillipsb66b42f2017-03-14 08:53:02 -040030 const SkRect dstRect = SkRect::MakeIWH(copyParams.fWidth, copyParams.fHeight);
Greg Daniel45d63032017-10-30 13:41:26 -040031 GrMipMapped mipMapped = dstWillRequireMipMaps ? GrMipMapped::kYes : GrMipMapped::kNo;
Robert Phillipsb66b42f2017-03-14 08:53:02 -040032
Greg Daniel09c94002018-06-08 22:11:51 +000033 SkRect localRect = SkRect::MakeWH(inputProxy->width(), inputProxy->height());
34
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) {
45 sk_sp<GrTextureProxy> proxy = GrCopyBaseMipMapToTextureProxy(context, inputProxy.get());
46 if (proxy) {
47 return proxy;
48 }
49 }
50
Brian Salomonbf6b9792019-08-21 09:38:10 -040051 auto copyRTC = context->priv().makeDeferredRenderTargetContextWithFallback(
52 SkBackingFit::kExact, dstRect.width(), dstRect.height(), colorType, nullptr, 1,
53 mipMapped, inputProxy->origin());
Robert Phillipsb66b42f2017-03-14 08:53:02 -040054 if (!copyRTC) {
55 return nullptr;
56 }
57
58 GrPaint paint;
Robert Phillipsb66b42f2017-03-14 08:53:02 -040059
60 if (needsDomain) {
61 const SkRect domain = localRect.makeInset(0.5f, 0.5f);
62 // This would cause us to read values from outside the subset. Surely, the caller knows
63 // better!
Brian Salomon2bbdcc42017-09-07 12:36:34 -040064 SkASSERT(copyParams.fFilter != GrSamplerState::Filter::kMipMap);
Robert Phillipsb66b42f2017-03-14 08:53:02 -040065 paint.addColorFragmentProcessor(
Brian Osman2240be92017-10-18 13:15:13 -040066 GrTextureDomainEffect::Make(std::move(inputProxy), SkMatrix::I(), domain,
67 GrTextureDomain::kClamp_Mode, copyParams.fFilter));
Robert Phillipsb66b42f2017-03-14 08:53:02 -040068 } else {
Brian Salomon2bbdcc42017-09-07 12:36:34 -040069 GrSamplerState samplerState(GrSamplerState::WrapMode::kClamp, copyParams.fFilter);
Brian Osman2240be92017-10-18 13:15:13 -040070 paint.addColorTextureProcessor(std::move(inputProxy), SkMatrix::I(), samplerState);
Robert Phillipsb66b42f2017-03-14 08:53:02 -040071 }
72 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
73
74 copyRTC->fillRectToRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), dstRect,
75 localRect);
76 return copyRTC->asTextureProxyRef();
77}
78
Brian Osmane8e54582016-11-28 10:06:27 -050079/** Determines whether a texture domain is necessary and if so what domain to use. There are two
80 * rectangles to consider:
Robert Phillips3798c862017-03-27 11:08:16 -040081 * - The first is the content area specified by the texture adjuster (i.e., textureContentArea).
82 * We can *never* allow filtering to cause bleed of pixels outside this rectangle.
83 * - The second rectangle is the constraint rectangle (i.e., constraintRect), which is known to
84 * be contained by the content area. The filterConstraint specifies whether we are allowed to
85 * bleed across this rect.
Brian Osmane8e54582016-11-28 10:06:27 -050086 *
87 * We want to avoid using a domain if possible. We consider the above rectangles, the filter type,
88 * and whether the coords generated by the draw would all fall within the constraint rect. If the
89 * latter is true we only need to consider whether the filter would extend beyond the rects.
90 */
91GrTextureProducer::DomainMode GrTextureProducer::DetermineDomainMode(
Brian Salomon2bbdcc42017-09-07 12:36:34 -040092 const SkRect& constraintRect,
93 FilterConstraint filterConstraint,
94 bool coordsLimitedToConstraintRect,
95 GrTextureProxy* proxy,
Brian Salomon2bbdcc42017-09-07 12:36:34 -040096 const GrSamplerState::Filter* filterModeOrNullForBicubic,
97 SkRect* domainRect) {
Robert Phillips51e7ca32017-03-27 10:14:08 -040098 const SkIRect proxyBounds = SkIRect::MakeWH(proxy->width(), proxy->height());
99
100 SkASSERT(proxyBounds.contains(constraintRect));
Robert Phillips51e7ca32017-03-27 10:14:08 -0400101
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500102 const bool proxyIsExact = GrProxyProvider::IsFunctionallyExact(proxy);
Brian Salomon4df00922017-09-07 16:34:11 +0000103
Robert Phillips51e7ca32017-03-27 10:14:08 -0400104 // If the constraint rectangle contains the whole proxy then no need for a domain.
105 if (constraintRect.contains(proxyBounds) && proxyIsExact) {
106 return kNoDomain_DomainMode;
107 }
108
Robert Phillips51e7ca32017-03-27 10:14:08 -0400109 bool restrictFilterToRect = (filterConstraint == GrTextureProducer::kYes_FilterConstraint);
110
111 // If we can filter outside the constraint rect, and there is no non-content area of the
112 // proxy, and we aren't going to generate sample coords outside the constraint rect then we
113 // don't need a domain.
Greg Danielc77085d2017-11-01 16:38:48 -0400114 if (!restrictFilterToRect && proxyIsExact && coordsLimitedToConstraintRect) {
Robert Phillips51e7ca32017-03-27 10:14:08 -0400115 return kNoDomain_DomainMode;
116 }
117
118 // Get the domain inset based on sampling mode (or bail if mipped)
Brian Salomon4df00922017-09-07 16:34:11 +0000119 SkScalar filterHalfWidth = 0.f;
Robert Phillips51e7ca32017-03-27 10:14:08 -0400120 if (filterModeOrNullForBicubic) {
121 switch (*filterModeOrNullForBicubic) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400122 case GrSamplerState::Filter::kNearest:
Robert Phillips51e7ca32017-03-27 10:14:08 -0400123 if (coordsLimitedToConstraintRect) {
124 return kNoDomain_DomainMode;
Brian Salomon4df00922017-09-07 16:34:11 +0000125 } else {
126 filterHalfWidth = 0.f;
Robert Phillips51e7ca32017-03-27 10:14:08 -0400127 }
128 break;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400129 case GrSamplerState::Filter::kBilerp:
Brian Salomon4df00922017-09-07 16:34:11 +0000130 filterHalfWidth = .5f;
Robert Phillips51e7ca32017-03-27 10:14:08 -0400131 break;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400132 case GrSamplerState::Filter::kMipMap:
Greg Danielc77085d2017-11-01 16:38:48 -0400133 if (restrictFilterToRect || !proxyIsExact) {
Brian Salomon4df00922017-09-07 16:34:11 +0000134 // No domain can save us here.
135 return kTightCopy_DomainMode;
136 }
137 return kNoDomain_DomainMode;
Robert Phillips51e7ca32017-03-27 10:14:08 -0400138 }
Brian Salomon4df00922017-09-07 16:34:11 +0000139 } else {
140 // bicubic does nearest filtering internally.
141 filterHalfWidth = 1.5f;
Robert Phillips51e7ca32017-03-27 10:14:08 -0400142 }
143
144 // Both bilerp and bicubic use bilinear filtering and so need to be clamped to the center
145 // of the edge texel. Pinning to the texel center has no impact on nearest mode and MIP-maps
146
147 static const SkScalar kDomainInset = 0.5f;
148 // Figure out the limits of pixels we're allowed to sample from.
149 // Unless we know the amount of outset and the texture matrix we have to conservatively enforce
150 // the domain.
151 if (restrictFilterToRect) {
152 *domainRect = constraintRect.makeInset(kDomainInset, kDomainInset);
Greg Danielc77085d2017-11-01 16:38:48 -0400153 } else if (!proxyIsExact) {
154 // If we got here then: proxy is not exact, the coords are limited to the
Brian Salomon4df00922017-09-07 16:34:11 +0000155 // constraint rect, and we're allowed to filter across the constraint rect boundary. So
Greg Danielc77085d2017-11-01 16:38:48 -0400156 // we check whether the filter would reach across the edge of the proxy.
Brian Salomon4df00922017-09-07 16:34:11 +0000157 // We will only set the sides that are required.
158
Mike Reed274218e2018-01-08 15:05:02 -0500159 *domainRect = SkRectPriv::MakeLargest();
Brian Salomon4df00922017-09-07 16:34:11 +0000160 if (coordsLimitedToConstraintRect) {
161 // We may be able to use the fact that the texture coords are limited to the constraint
162 // rect in order to avoid having to add a domain.
163 bool needContentAreaConstraint = false;
Greg Danielc77085d2017-11-01 16:38:48 -0400164 if (proxyBounds.fRight - filterHalfWidth < constraintRect.fRight) {
165 domainRect->fRight = proxyBounds.fRight - kDomainInset;
Brian Salomon4df00922017-09-07 16:34:11 +0000166 needContentAreaConstraint = true;
167 }
Greg Danielc77085d2017-11-01 16:38:48 -0400168 if (proxyBounds.fBottom - filterHalfWidth < constraintRect.fBottom) {
169 domainRect->fBottom = proxyBounds.fBottom - kDomainInset;
Brian Salomon4df00922017-09-07 16:34:11 +0000170 needContentAreaConstraint = true;
171 }
172 if (!needContentAreaConstraint) {
173 return kNoDomain_DomainMode;
174 }
175 } else {
176 // Our sample coords for the texture are allowed to be outside the constraintRect so we
177 // don't consider it when computing the domain.
Greg Danielc77085d2017-11-01 16:38:48 -0400178 domainRect->fRight = proxyBounds.fRight - kDomainInset;
179 domainRect->fBottom = proxyBounds.fBottom - kDomainInset;
Robert Phillips51e7ca32017-03-27 10:14:08 -0400180 }
Brian Salomon4df00922017-09-07 16:34:11 +0000181 } else {
182 return kNoDomain_DomainMode;
Robert Phillips51e7ca32017-03-27 10:14:08 -0400183 }
Brian Salomon4df00922017-09-07 16:34:11 +0000184
185 if (domainRect->fLeft > domainRect->fRight) {
186 domainRect->fLeft = domainRect->fRight = SkScalarAve(domainRect->fLeft, domainRect->fRight);
187 }
188 if (domainRect->fTop > domainRect->fBottom) {
189 domainRect->fTop = domainRect->fBottom = SkScalarAve(domainRect->fTop, domainRect->fBottom);
190 }
191 return kDomain_DomainMode;
Robert Phillips51e7ca32017-03-27 10:14:08 -0400192}
193
Michael Ludwigddeed372019-02-20 16:50:10 -0500194std::unique_ptr<GrFragmentProcessor> GrTextureProducer::createFragmentProcessorForDomainAndFilter(
Brian Salomonaff329b2017-08-11 09:40:37 -0400195 sk_sp<GrTextureProxy> proxy,
Brian Salomonaff329b2017-08-11 09:40:37 -0400196 const SkMatrix& textureMatrix,
197 DomainMode domainMode,
198 const SkRect& domain,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400199 const GrSamplerState::Filter* filterOrNullForBicubic) {
Robert Phillipsb66b42f2017-03-14 08:53:02 -0400200 SkASSERT(kTightCopy_DomainMode != domainMode);
Michael Ludwigddeed372019-02-20 16:50:10 -0500201 bool clampToBorderSupport = fContext->priv().caps()->clampToBorderSupport();
Robert Phillipsb66b42f2017-03-14 08:53:02 -0400202 if (filterOrNullForBicubic) {
Michael Ludwigddeed372019-02-20 16:50:10 -0500203 if (kDomain_DomainMode == domainMode || (fDomainNeedsDecal && !clampToBorderSupport)) {
204 GrTextureDomain::Mode wrapMode = fDomainNeedsDecal ? GrTextureDomain::kDecal_Mode
205 : GrTextureDomain::kClamp_Mode;
Brian Osman2240be92017-10-18 13:15:13 -0400206 return GrTextureDomainEffect::Make(std::move(proxy), textureMatrix, domain,
Michael Ludwigddeed372019-02-20 16:50:10 -0500207 wrapMode, *filterOrNullForBicubic);
Robert Phillipsb66b42f2017-03-14 08:53:02 -0400208 } else {
Michael Ludwigddeed372019-02-20 16:50:10 -0500209 GrSamplerState::WrapMode wrapMode =
210 fDomainNeedsDecal ? GrSamplerState::WrapMode::kClampToBorder
211 : GrSamplerState::WrapMode::kClamp;
212 GrSamplerState samplerState(wrapMode, *filterOrNullForBicubic);
Brian Osman2240be92017-10-18 13:15:13 -0400213 return GrSimpleTextureEffect::Make(std::move(proxy), textureMatrix, samplerState);
Robert Phillipsb66b42f2017-03-14 08:53:02 -0400214 }
215 } else {
Michael Ludwigddeed372019-02-20 16:50:10 -0500216 static const GrSamplerState::WrapMode kClampClamp[] = {
217 GrSamplerState::WrapMode::kClamp, GrSamplerState::WrapMode::kClamp};
218 static const GrSamplerState::WrapMode kDecalDecal[] = {
219 GrSamplerState::WrapMode::kClampToBorder, GrSamplerState::WrapMode::kClampToBorder};
220
Brian Salomona86fc7a2019-05-28 20:42:58 -0400221 static constexpr auto kDir = GrBicubicEffect::Direction::kXY;
Michael Ludwigddeed372019-02-20 16:50:10 -0500222 if (kDomain_DomainMode == domainMode || (fDomainNeedsDecal && !clampToBorderSupport)) {
223 GrTextureDomain::Mode wrapMode = fDomainNeedsDecal ? GrTextureDomain::kDecal_Mode
224 : GrTextureDomain::kClamp_Mode;
Brian Salomona86fc7a2019-05-28 20:42:58 -0400225 return GrBicubicEffect::Make(std::move(proxy), textureMatrix, kClampClamp, wrapMode,
Brian Salomon1127c0b2019-06-13 20:22:10 +0000226 wrapMode, kDir, this->alphaType(),
Michael Ludwigddeed372019-02-20 16:50:10 -0500227 kDomain_DomainMode == domainMode ? &domain : nullptr);
Robert Phillipsb66b42f2017-03-14 08:53:02 -0400228 } else {
Michael Ludwigddeed372019-02-20 16:50:10 -0500229 return GrBicubicEffect::Make(std::move(proxy), textureMatrix,
Brian Salomon1127c0b2019-06-13 20:22:10 +0000230 fDomainNeedsDecal ? kDecalDecal : kClampClamp, kDir,
231 this->alphaType());
Robert Phillipsb66b42f2017-03-14 08:53:02 -0400232 }
233 }
234}
Brian Salomon2a943df2018-05-04 13:43:19 -0400235
236sk_sp<GrTextureProxy> GrTextureProducer::refTextureProxyForParams(
Michael Ludwigddeed372019-02-20 16:50:10 -0500237 const GrSamplerState::Filter* filterOrNullForBicubic,
238 SkScalar scaleAdjust[2]) {
239 GrSamplerState sampler; // Default is nearest + clamp
240 if (filterOrNullForBicubic) {
241 sampler.setFilterMode(*filterOrNullForBicubic);
242 }
243 if (fDomainNeedsDecal) {
244 // Assuming hardware support, switch to clamp-to-border instead of clamp
245 if (fContext->priv().caps()->clampToBorderSupport()) {
246 sampler.setWrapModeX(GrSamplerState::WrapMode::kClampToBorder);
247 sampler.setWrapModeY(GrSamplerState::WrapMode::kClampToBorder);
248 }
249 }
250 return this->refTextureProxyForParams(sampler, scaleAdjust);
251}
252
253sk_sp<GrTextureProxy> GrTextureProducer::refTextureProxyForParams(
Brian Salomon2a943df2018-05-04 13:43:19 -0400254 const GrSamplerState& sampler,
Brian Salomon2a943df2018-05-04 13:43:19 -0400255 SkScalar scaleAdjust[2]) {
256 // Check that the caller pre-initialized scaleAdjust
257 SkASSERT(!scaleAdjust || (scaleAdjust[0] == 1 && scaleAdjust[1] == 1));
258 // Check that if the caller passed nullptr for scaleAdjust that we're in the case where there
259 // can be no scaling.
260 SkDEBUGCODE(bool expectNoScale = (sampler.filter() != GrSamplerState::Filter::kMipMap &&
261 !sampler.isRepeated()));
262 SkASSERT(scaleAdjust || expectNoScale);
Greg Daniel8e9b4c42018-07-20 10:30:48 -0400263
264 int mipCount = SkMipMap::ComputeLevelCount(this->width(), this->height());
265 bool willBeMipped = GrSamplerState::Filter::kMipMap == sampler.filter() && mipCount &&
Robert Phillips9338c602019-02-19 12:52:29 -0500266 this->context()->priv().caps()->mipMapSupport();
Greg Daniel8e9b4c42018-07-20 10:30:48 -0400267
Brian Osman6064e1c2018-10-19 14:27:54 -0400268 auto result = this->onRefTextureProxyForParams(sampler, willBeMipped, scaleAdjust);
Brian Salomon2a943df2018-05-04 13:43:19 -0400269
Greg Daniel022b1e02018-07-20 14:54:00 -0400270 // Check to make sure that if we say the texture willBeMipped that the returned texture has mip
271 // maps, unless the config is not copyable.
272 SkASSERT(!result || !willBeMipped || result->mipMapped() == GrMipMapped::kYes ||
Greg Daniel6980c4e2019-07-30 16:18:18 -0400273 !this->context()->priv().caps()->isFormatCopyable(result->backendFormat()));
Greg Daniel022b1e02018-07-20 14:54:00 -0400274
Brian Salomon2a943df2018-05-04 13:43:19 -0400275 // Check that the "no scaling expected" case always returns a proxy of the same size as the
276 // producer.
277 SkASSERT(!result || !expectNoScale ||
278 (result->width() == this->width() && result->height() == this->height()));
279 return result;
280}
Greg Daniel5f4b09d2018-06-12 16:39:59 -0400281
Brian Osman6064e1c2018-10-19 14:27:54 -0400282sk_sp<GrTextureProxy> GrTextureProducer::refTextureProxy(GrMipMapped willNeedMips) {
Greg Daniel5f4b09d2018-06-12 16:39:59 -0400283 GrSamplerState::Filter filter =
284 GrMipMapped::kNo == willNeedMips ? GrSamplerState::Filter::kNearest
285 : GrSamplerState::Filter::kMipMap;
286 GrSamplerState sampler(GrSamplerState::WrapMode::kClamp, filter);
Greg Daniel8e9b4c42018-07-20 10:30:48 -0400287
288 int mipCount = SkMipMap::ComputeLevelCount(this->width(), this->height());
289 bool willBeMipped = GrSamplerState::Filter::kMipMap == sampler.filter() && mipCount &&
Robert Phillips9338c602019-02-19 12:52:29 -0500290 this->context()->priv().caps()->mipMapSupport();
Greg Daniel8e9b4c42018-07-20 10:30:48 -0400291
Brian Osman6064e1c2018-10-19 14:27:54 -0400292 auto result = this->onRefTextureProxyForParams(sampler, willBeMipped, nullptr);
Greg Daniel5f4b09d2018-06-12 16:39:59 -0400293
Greg Daniel022b1e02018-07-20 14:54:00 -0400294 // Check to make sure that if we say the texture willBeMipped that the returned texture has mip
295 // maps, unless the config is not copyable.
296 SkASSERT(!result || !willBeMipped || result->mipMapped() == GrMipMapped::kYes ||
Greg Daniel6980c4e2019-07-30 16:18:18 -0400297 !this->context()->priv().caps()->isFormatCopyable(result->backendFormat()));
Greg Daniel022b1e02018-07-20 14:54:00 -0400298
Greg Daniel5f4b09d2018-06-12 16:39:59 -0400299 // Check that no scaling occured and we returned a proxy of the same size as the producer.
300 SkASSERT(!result || (result->width() == this->width() && result->height() == this->height()));
301 return result;
302}