blob: 243582d8863ae8c4fda815e811f2bdbbaee248c6 [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
Brian Salomonb8f098d2020-01-07 11:15:44 -05008#include "src/gpu/GrTextureProducer.h"
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/private/GrRecordingContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/core/SkMipMap.h"
12#include "src/core/SkRectPriv.h"
13#include "src/gpu/GrClip.h"
14#include "src/gpu/GrContextPriv.h"
15#include "src/gpu/GrProxyProvider.h"
16#include "src/gpu/GrRecordingContextPriv.h"
17#include "src/gpu/GrRenderTargetContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040018#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/SkGr.h"
20#include "src/gpu/effects/GrBicubicEffect.h"
21#include "src/gpu/effects/GrTextureDomain.h"
Brian Salomonb8f098d2020-01-07 11:15:44 -050022#include "src/gpu/effects/GrTextureEffect.h"
Brian Osmane8e54582016-11-28 10:06:27 -050023
Robert Phillips9338c602019-02-19 12:52:29 -050024sk_sp<GrTextureProxy> GrTextureProducer::CopyOnGpu(GrRecordingContext* context,
Robert Phillipsb66b42f2017-03-14 08:53:02 -040025 sk_sp<GrTextureProxy> inputProxy,
Brian Salomond6287472019-06-24 15:50:07 -040026 GrColorType colorType,
Greg Daniele1da1d92017-10-06 15:59:27 -040027 const CopyParams& copyParams,
28 bool dstWillRequireMipMaps) {
Robert Phillipsb66b42f2017-03-14 08:53:02 -040029 SkASSERT(context);
30
Brian Salomon1a217eb2019-10-24 10:50:36 -040031 const SkRect dstRect = SkRect::Make(copyParams.fDimensions);
Greg Daniel45d63032017-10-30 13:41:26 -040032 GrMipMapped mipMapped = dstWillRequireMipMaps ? GrMipMapped::kYes : GrMipMapped::kNo;
Robert Phillipsb66b42f2017-03-14 08:53:02 -040033
Brian Salomon9f2b86c2019-10-22 10:37:46 -040034 SkRect localRect = inputProxy->getBoundsRect();
Greg Daniel09c94002018-06-08 22:11:51 +000035
Greg Daniel09c94002018-06-08 22:11:51 +000036 bool resizing = false;
37 if (copyParams.fFilter != GrSamplerState::Filter::kNearest) {
Brian Salomonca6b2f42020-01-24 11:31:21 -050038 resizing = localRect.width() != dstRect.width() || localRect.height() != dstRect.height();
Greg Daniel09c94002018-06-08 22:11:51 +000039 }
40
Brian Salomonca6b2f42020-01-24 11:31:21 -050041 if (copyParams.fFilter == GrSamplerState::Filter::kNearest && !resizing &&
Greg Daniel09c94002018-06-08 22:11:51 +000042 dstWillRequireMipMaps) {
Greg Danielc594e622019-10-15 14:01:49 -040043 sk_sp<GrTextureProxy> proxy = GrCopyBaseMipMapToTextureProxy(context, inputProxy.get(),
44 colorType);
Greg Daniel09c94002018-06-08 22:11:51 +000045 if (proxy) {
46 return proxy;
47 }
48 }
49
Greg Daniele20fcad2020-01-08 11:52:34 -050050 auto copyRTC = GrRenderTargetContext::MakeWithFallback(
51 context, colorType, nullptr, SkBackingFit::kExact, copyParams.fDimensions, 1,
52 mipMapped, inputProxy->isProtected(), inputProxy->origin());
Robert Phillipsb66b42f2017-03-14 08:53:02 -040053 if (!copyRTC) {
54 return nullptr;
55 }
56
Brian Salomonca6b2f42020-01-24 11:31:21 -050057 const auto& caps = *context->priv().caps();
Robert Phillipsb66b42f2017-03-14 08:53:02 -040058 GrPaint paint;
Robert Phillipsb66b42f2017-03-14 08:53:02 -040059
Brian Salomonca6b2f42020-01-24 11:31:21 -050060 GrSamplerState sampler(GrSamplerState::WrapMode::kClamp, copyParams.fFilter);
61 auto boundsRect = SkIRect::MakeSize(inputProxy->dimensions());
62 auto fp = GrTextureEffect::MakeTexelSubset(std::move(inputProxy), kUnknown_SkAlphaType,
63 SkMatrix::I(), sampler, boundsRect, localRect, caps);
Brian Salomon7eabfe82019-12-02 14:20:20 -050064 paint.addColorFragmentProcessor(std::move(fp));
Robert Phillipsb66b42f2017-03-14 08:53:02 -040065 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
66
67 copyRTC->fillRectToRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), dstRect,
68 localRect);
69 return copyRTC->asTextureProxyRef();
70}
71
Brian Osmane8e54582016-11-28 10:06:27 -050072/** Determines whether a texture domain is necessary and if so what domain to use. There are two
73 * rectangles to consider:
Robert Phillips3798c862017-03-27 11:08:16 -040074 * - The first is the content area specified by the texture adjuster (i.e., textureContentArea).
75 * We can *never* allow filtering to cause bleed of pixels outside this rectangle.
76 * - The second rectangle is the constraint rectangle (i.e., constraintRect), which is known to
77 * be contained by the content area. The filterConstraint specifies whether we are allowed to
78 * bleed across this rect.
Brian Osmane8e54582016-11-28 10:06:27 -050079 *
80 * We want to avoid using a domain if possible. We consider the above rectangles, the filter type,
81 * and whether the coords generated by the draw would all fall within the constraint rect. If the
82 * latter is true we only need to consider whether the filter would extend beyond the rects.
83 */
84GrTextureProducer::DomainMode GrTextureProducer::DetermineDomainMode(
Brian Salomon2bbdcc42017-09-07 12:36:34 -040085 const SkRect& constraintRect,
86 FilterConstraint filterConstraint,
87 bool coordsLimitedToConstraintRect,
88 GrTextureProxy* proxy,
Brian Salomon2bbdcc42017-09-07 12:36:34 -040089 const GrSamplerState::Filter* filterModeOrNullForBicubic,
90 SkRect* domainRect) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -040091 const SkIRect proxyBounds = SkIRect::MakeSize(proxy->dimensions());
Robert Phillips51e7ca32017-03-27 10:14:08 -040092
93 SkASSERT(proxyBounds.contains(constraintRect));
Robert Phillips51e7ca32017-03-27 10:14:08 -040094
Brian Salomon5c60b752019-12-13 15:03:43 -050095 const bool proxyIsExact = proxy->isFunctionallyExact();
Brian Salomon4df00922017-09-07 16:34:11 +000096
Robert Phillips51e7ca32017-03-27 10:14:08 -040097 // If the constraint rectangle contains the whole proxy then no need for a domain.
98 if (constraintRect.contains(proxyBounds) && proxyIsExact) {
99 return kNoDomain_DomainMode;
100 }
101
Robert Phillips51e7ca32017-03-27 10:14:08 -0400102 bool restrictFilterToRect = (filterConstraint == GrTextureProducer::kYes_FilterConstraint);
103
104 // If we can filter outside the constraint rect, and there is no non-content area of the
105 // proxy, and we aren't going to generate sample coords outside the constraint rect then we
106 // don't need a domain.
Greg Danielc77085d2017-11-01 16:38:48 -0400107 if (!restrictFilterToRect && proxyIsExact && coordsLimitedToConstraintRect) {
Robert Phillips51e7ca32017-03-27 10:14:08 -0400108 return kNoDomain_DomainMode;
109 }
110
111 // Get the domain inset based on sampling mode (or bail if mipped)
Brian Salomon4df00922017-09-07 16:34:11 +0000112 SkScalar filterHalfWidth = 0.f;
Robert Phillips51e7ca32017-03-27 10:14:08 -0400113 if (filterModeOrNullForBicubic) {
114 switch (*filterModeOrNullForBicubic) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400115 case GrSamplerState::Filter::kNearest:
Robert Phillips51e7ca32017-03-27 10:14:08 -0400116 if (coordsLimitedToConstraintRect) {
117 return kNoDomain_DomainMode;
Brian Salomon4df00922017-09-07 16:34:11 +0000118 } else {
119 filterHalfWidth = 0.f;
Robert Phillips51e7ca32017-03-27 10:14:08 -0400120 }
121 break;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400122 case GrSamplerState::Filter::kBilerp:
Brian Salomon4df00922017-09-07 16:34:11 +0000123 filterHalfWidth = .5f;
Robert Phillips51e7ca32017-03-27 10:14:08 -0400124 break;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400125 case GrSamplerState::Filter::kMipMap:
Greg Danielc77085d2017-11-01 16:38:48 -0400126 if (restrictFilterToRect || !proxyIsExact) {
Brian Salomon4df00922017-09-07 16:34:11 +0000127 // No domain can save us here.
128 return kTightCopy_DomainMode;
129 }
130 return kNoDomain_DomainMode;
Robert Phillips51e7ca32017-03-27 10:14:08 -0400131 }
Brian Salomon4df00922017-09-07 16:34:11 +0000132 } else {
133 // bicubic does nearest filtering internally.
134 filterHalfWidth = 1.5f;
Robert Phillips51e7ca32017-03-27 10:14:08 -0400135 }
136
137 // Both bilerp and bicubic use bilinear filtering and so need to be clamped to the center
138 // of the edge texel. Pinning to the texel center has no impact on nearest mode and MIP-maps
139
140 static const SkScalar kDomainInset = 0.5f;
141 // Figure out the limits of pixels we're allowed to sample from.
142 // Unless we know the amount of outset and the texture matrix we have to conservatively enforce
143 // the domain.
144 if (restrictFilterToRect) {
145 *domainRect = constraintRect.makeInset(kDomainInset, kDomainInset);
Greg Danielc77085d2017-11-01 16:38:48 -0400146 } else if (!proxyIsExact) {
147 // If we got here then: proxy is not exact, the coords are limited to the
Brian Salomon4df00922017-09-07 16:34:11 +0000148 // constraint rect, and we're allowed to filter across the constraint rect boundary. So
Greg Danielc77085d2017-11-01 16:38:48 -0400149 // we check whether the filter would reach across the edge of the proxy.
Brian Salomon4df00922017-09-07 16:34:11 +0000150 // We will only set the sides that are required.
151
Mike Reed274218e2018-01-08 15:05:02 -0500152 *domainRect = SkRectPriv::MakeLargest();
Brian Salomon4df00922017-09-07 16:34:11 +0000153 if (coordsLimitedToConstraintRect) {
154 // We may be able to use the fact that the texture coords are limited to the constraint
155 // rect in order to avoid having to add a domain.
156 bool needContentAreaConstraint = false;
Greg Danielc77085d2017-11-01 16:38:48 -0400157 if (proxyBounds.fRight - filterHalfWidth < constraintRect.fRight) {
158 domainRect->fRight = proxyBounds.fRight - kDomainInset;
Brian Salomon4df00922017-09-07 16:34:11 +0000159 needContentAreaConstraint = true;
160 }
Greg Danielc77085d2017-11-01 16:38:48 -0400161 if (proxyBounds.fBottom - filterHalfWidth < constraintRect.fBottom) {
162 domainRect->fBottom = proxyBounds.fBottom - kDomainInset;
Brian Salomon4df00922017-09-07 16:34:11 +0000163 needContentAreaConstraint = true;
164 }
165 if (!needContentAreaConstraint) {
166 return kNoDomain_DomainMode;
167 }
168 } else {
169 // Our sample coords for the texture are allowed to be outside the constraintRect so we
170 // don't consider it when computing the domain.
Greg Danielc77085d2017-11-01 16:38:48 -0400171 domainRect->fRight = proxyBounds.fRight - kDomainInset;
172 domainRect->fBottom = proxyBounds.fBottom - kDomainInset;
Robert Phillips51e7ca32017-03-27 10:14:08 -0400173 }
Brian Salomon4df00922017-09-07 16:34:11 +0000174 } else {
175 return kNoDomain_DomainMode;
Robert Phillips51e7ca32017-03-27 10:14:08 -0400176 }
Brian Salomon4df00922017-09-07 16:34:11 +0000177
178 if (domainRect->fLeft > domainRect->fRight) {
179 domainRect->fLeft = domainRect->fRight = SkScalarAve(domainRect->fLeft, domainRect->fRight);
180 }
181 if (domainRect->fTop > domainRect->fBottom) {
182 domainRect->fTop = domainRect->fBottom = SkScalarAve(domainRect->fTop, domainRect->fBottom);
183 }
184 return kDomain_DomainMode;
Robert Phillips51e7ca32017-03-27 10:14:08 -0400185}
186
Michael Ludwigddeed372019-02-20 16:50:10 -0500187std::unique_ptr<GrFragmentProcessor> GrTextureProducer::createFragmentProcessorForDomainAndFilter(
Brian Salomonaff329b2017-08-11 09:40:37 -0400188 sk_sp<GrTextureProxy> proxy,
Brian Salomonaff329b2017-08-11 09:40:37 -0400189 const SkMatrix& textureMatrix,
190 DomainMode domainMode,
191 const SkRect& domain,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400192 const GrSamplerState::Filter* filterOrNullForBicubic) {
Robert Phillipsb66b42f2017-03-14 08:53:02 -0400193 SkASSERT(kTightCopy_DomainMode != domainMode);
Brian Salomonca6b2f42020-01-24 11:31:21 -0500194 const auto& caps = *fContext->priv().caps();
Brian Salomonfc118442019-11-22 19:09:27 -0500195 SkAlphaType srcAlphaType = this->alphaType();
Robert Phillipsb66b42f2017-03-14 08:53:02 -0400196 if (filterOrNullForBicubic) {
Brian Salomonca6b2f42020-01-24 11:31:21 -0500197 GrSamplerState::WrapMode wrapMode = fDomainNeedsDecal
Brian Salomon7eabfe82019-12-02 14:20:20 -0500198 ? GrSamplerState::WrapMode::kClampToBorder
199 : GrSamplerState::WrapMode::kClamp;
200 GrSamplerState samplerState(wrapMode, *filterOrNullForBicubic);
Brian Salomonca6b2f42020-01-24 11:31:21 -0500201 if (kNoDomain_DomainMode == domainMode) {
202 return GrTextureEffect::Make(std::move(proxy), srcAlphaType, textureMatrix,
203 samplerState, caps);
Robert Phillipsb66b42f2017-03-14 08:53:02 -0400204 }
Brian Salomonca6b2f42020-01-24 11:31:21 -0500205 return GrTextureEffect::MakeSubset(std::move(proxy), srcAlphaType, textureMatrix,
206 samplerState, domain, caps);
Robert Phillipsb66b42f2017-03-14 08:53:02 -0400207 } else {
Michael Ludwigddeed372019-02-20 16:50:10 -0500208 static const GrSamplerState::WrapMode kClampClamp[] = {
209 GrSamplerState::WrapMode::kClamp, GrSamplerState::WrapMode::kClamp};
210 static const GrSamplerState::WrapMode kDecalDecal[] = {
211 GrSamplerState::WrapMode::kClampToBorder, GrSamplerState::WrapMode::kClampToBorder};
212
Brian Salomona86fc7a2019-05-28 20:42:58 -0400213 static constexpr auto kDir = GrBicubicEffect::Direction::kXY;
Brian Salomonca6b2f42020-01-24 11:31:21 -0500214 bool clampToBorderSupport = caps.clampToBorderSupport();
Michael Ludwigddeed372019-02-20 16:50:10 -0500215 if (kDomain_DomainMode == domainMode || (fDomainNeedsDecal && !clampToBorderSupport)) {
216 GrTextureDomain::Mode wrapMode = fDomainNeedsDecal ? GrTextureDomain::kDecal_Mode
217 : GrTextureDomain::kClamp_Mode;
Brian Salomonfc118442019-11-22 19:09:27 -0500218 return GrBicubicEffect::Make(std::move(proxy), textureMatrix, kClampClamp, wrapMode,
219 wrapMode, kDir, srcAlphaType,
Michael Ludwigddeed372019-02-20 16:50:10 -0500220 kDomain_DomainMode == domainMode ? &domain : nullptr);
Robert Phillipsb66b42f2017-03-14 08:53:02 -0400221 } else {
Brian Salomonfc118442019-11-22 19:09:27 -0500222 return GrBicubicEffect::Make(std::move(proxy), textureMatrix,
Brian Salomon1127c0b2019-06-13 20:22:10 +0000223 fDomainNeedsDecal ? kDecalDecal : kClampClamp, kDir,
Brian Salomonfc118442019-11-22 19:09:27 -0500224 srcAlphaType);
Robert Phillipsb66b42f2017-03-14 08:53:02 -0400225 }
226 }
227}
Brian Salomon2a943df2018-05-04 13:43:19 -0400228
229sk_sp<GrTextureProxy> GrTextureProducer::refTextureProxyForParams(
Michael Ludwigddeed372019-02-20 16:50:10 -0500230 const GrSamplerState::Filter* filterOrNullForBicubic,
231 SkScalar scaleAdjust[2]) {
232 GrSamplerState sampler; // Default is nearest + clamp
233 if (filterOrNullForBicubic) {
234 sampler.setFilterMode(*filterOrNullForBicubic);
235 }
236 if (fDomainNeedsDecal) {
237 // Assuming hardware support, switch to clamp-to-border instead of clamp
238 if (fContext->priv().caps()->clampToBorderSupport()) {
239 sampler.setWrapModeX(GrSamplerState::WrapMode::kClampToBorder);
240 sampler.setWrapModeY(GrSamplerState::WrapMode::kClampToBorder);
241 }
242 }
243 return this->refTextureProxyForParams(sampler, scaleAdjust);
244}
245
Brian Salomonccb61422020-01-09 10:46:36 -0500246sk_sp<GrTextureProxy> GrTextureProducer::refTextureProxyForParams(GrSamplerState sampler,
247 SkScalar scaleAdjust[2]) {
Brian Salomon2a943df2018-05-04 13:43:19 -0400248 // Check that the caller pre-initialized scaleAdjust
249 SkASSERT(!scaleAdjust || (scaleAdjust[0] == 1 && scaleAdjust[1] == 1));
Greg Daniel82c6b102020-01-21 10:33:22 -0500250
251 const GrCaps* caps = this->context()->priv().caps();
Greg Daniel8e9b4c42018-07-20 10:30:48 -0400252
253 int mipCount = SkMipMap::ComputeLevelCount(this->width(), this->height());
254 bool willBeMipped = GrSamplerState::Filter::kMipMap == sampler.filter() && mipCount &&
Greg Daniel82c6b102020-01-21 10:33:22 -0500255 caps->mipMapSupport();
Greg Daniel8e9b4c42018-07-20 10:30:48 -0400256
Brian Osman6064e1c2018-10-19 14:27:54 -0400257 auto result = this->onRefTextureProxyForParams(sampler, willBeMipped, scaleAdjust);
Brian Salomon2a943df2018-05-04 13:43:19 -0400258
Greg Daniel022b1e02018-07-20 14:54:00 -0400259 // Check to make sure that if we say the texture willBeMipped that the returned texture has mip
260 // maps, unless the config is not copyable.
261 SkASSERT(!result || !willBeMipped || result->mipMapped() == GrMipMapped::kYes ||
Greg Daniel82c6b102020-01-21 10:33:22 -0500262 !caps->isFormatCopyable(result->backendFormat()));
Greg Daniel022b1e02018-07-20 14:54:00 -0400263
Greg Daniel82c6b102020-01-21 10:33:22 -0500264 SkDEBUGCODE(bool expectNoScale = (sampler.filter() != GrSamplerState::Filter::kMipMap &&
265 !sampler.isRepeated()));
Brian Salomon2a943df2018-05-04 13:43:19 -0400266 // Check that the "no scaling expected" case always returns a proxy of the same size as the
267 // producer.
Brian Salomon1a217eb2019-10-24 10:50:36 -0400268 SkASSERT(!result || !expectNoScale || result->dimensions() == this->dimensions());
Greg Daniel82c6b102020-01-21 10:33:22 -0500269
Brian Salomon2a943df2018-05-04 13:43:19 -0400270 return result;
271}
Greg Daniel5f4b09d2018-06-12 16:39:59 -0400272
Greg Daniel82c6b102020-01-21 10:33:22 -0500273std::pair<sk_sp<GrTextureProxy>, GrColorType> GrTextureProducer::refTextureProxy(
274 GrMipMapped willNeedMips) {
Greg Daniel5f4b09d2018-06-12 16:39:59 -0400275 GrSamplerState::Filter filter =
276 GrMipMapped::kNo == willNeedMips ? GrSamplerState::Filter::kNearest
277 : GrSamplerState::Filter::kMipMap;
278 GrSamplerState sampler(GrSamplerState::WrapMode::kClamp, filter);
Greg Daniel8e9b4c42018-07-20 10:30:48 -0400279
Greg Daniel82c6b102020-01-21 10:33:22 -0500280 auto result = this->refTextureProxyForParams(sampler, nullptr);
Greg Daniel8e9b4c42018-07-20 10:30:48 -0400281
Greg Daniel82c6b102020-01-21 10:33:22 -0500282#ifdef SK_DEBUG
283 const GrCaps* caps = this->context()->priv().caps();
284 // Check that the resulting proxy format is compatible with the GrColorType of this producer
285 SkASSERT(!result ||
286 result->isFormatCompressed(caps) ||
287 caps->areColorTypeAndFormatCompatible(this->colorType(), result->backendFormat()));
288#endif
Greg Daniel5f4b09d2018-06-12 16:39:59 -0400289
Greg Daniel82c6b102020-01-21 10:33:22 -0500290 return {result, this->colorType()};
Greg Daniel5f4b09d2018-06-12 16:39:59 -0400291}