blob: 0e2f90889db6ca39cfd6577fe494095e25a6416b [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 Phillipsbf5cb0f2020-02-21 13:46:38 +000024GrSurfaceProxyView GrTextureProducer::CopyOnGpu(GrRecordingContext* context,
25 GrSurfaceProxyView inputView,
26 GrColorType colorType,
27 const CopyParams& copyParams,
28 bool dstWillRequireMipMaps) {
Robert Phillipsb66b42f2017-03-14 08:53:02 -040029 SkASSERT(context);
Greg Danielcc104db2020-02-03 14:17:08 -050030 SkASSERT(inputView.asTextureProxy());
Robert Phillipsb66b42f2017-03-14 08:53:02 -040031
Robert Phillipsbf5cb0f2020-02-21 13:46:38 +000032 const SkRect dstRect = SkRect::Make(copyParams.fDimensions);
33 GrMipMapped mipMapped = dstWillRequireMipMaps ? GrMipMapped::kYes : GrMipMapped::kNo;
Greg Daniel09c94002018-06-08 22:11:51 +000034
Robert Phillipsbf5cb0f2020-02-21 13:46:38 +000035 GrSurfaceProxy* proxy = inputView.proxy();
36 SkRect localRect = proxy->getBoundsRect();
37
38 bool resizing = false;
39 if (copyParams.fFilter != GrSamplerState::Filter::kNearest) {
40 resizing = localRect.width() != dstRect.width() || localRect.height() != dstRect.height();
41 }
42
43 if (copyParams.fFilter == GrSamplerState::Filter::kNearest && !resizing &&
44 dstWillRequireMipMaps) {
45 GrSurfaceProxyView view = GrCopyBaseMipMapToTextureProxy(context, proxy, inputView.origin(),
46 colorType);
47 if (view.proxy()) {
48 return view;
49 }
Greg Daniel09c94002018-06-08 22:11:51 +000050 }
51
Greg Daniele20fcad2020-01-08 11:52:34 -050052 auto copyRTC = GrRenderTargetContext::MakeWithFallback(
Robert Phillipsbf5cb0f2020-02-21 13:46:38 +000053 context, colorType, nullptr, SkBackingFit::kExact, copyParams.fDimensions, 1,
54 mipMapped, proxy->isProtected(), inputView.origin());
Robert Phillipsb66b42f2017-03-14 08:53:02 -040055 if (!copyRTC) {
Greg Danielcc104db2020-02-03 14:17:08 -050056 return {};
Robert Phillipsb66b42f2017-03-14 08:53:02 -040057 }
58
Robert Phillipsbf5cb0f2020-02-21 13:46:38 +000059 const auto& caps = *context->priv().caps();
Robert Phillipsb66b42f2017-03-14 08:53:02 -040060 GrPaint paint;
Robert Phillipsbf5cb0f2020-02-21 13:46:38 +000061
62 GrSamplerState sampler(GrSamplerState::WrapMode::kClamp, copyParams.fFilter);
63 auto boundsRect = SkRect::Make(proxy->dimensions());
64 auto fp = GrTextureEffect::MakeSubset(std::move(inputView), kUnknown_SkAlphaType, SkMatrix::I(),
65 sampler, boundsRect, localRect, caps);
Brian Salomon7eabfe82019-12-02 14:20:20 -050066 paint.addColorFragmentProcessor(std::move(fp));
Robert Phillipsb66b42f2017-03-14 08:53:02 -040067 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
68
Robert Phillipsbf5cb0f2020-02-21 13:46:38 +000069 copyRTC->fillRectToRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), dstRect,
70 localRect);
Greg Danielcc104db2020-02-03 14:17:08 -050071 return copyRTC->readSurfaceView();
Robert Phillipsb66b42f2017-03-14 08:53:02 -040072}
73
Brian Osmane8e54582016-11-28 10:06:27 -050074/** Determines whether a texture domain is necessary and if so what domain to use. There are two
75 * rectangles to consider:
Robert Phillips3798c862017-03-27 11:08:16 -040076 * - The first is the content area specified by the texture adjuster (i.e., textureContentArea).
77 * We can *never* allow filtering to cause bleed of pixels outside this rectangle.
78 * - The second rectangle is the constraint rectangle (i.e., constraintRect), which is known to
79 * be contained by the content area. The filterConstraint specifies whether we are allowed to
80 * bleed across this rect.
Brian Osmane8e54582016-11-28 10:06:27 -050081 *
82 * We want to avoid using a domain if possible. We consider the above rectangles, the filter type,
83 * and whether the coords generated by the draw would all fall within the constraint rect. If the
84 * latter is true we only need to consider whether the filter would extend beyond the rects.
85 */
86GrTextureProducer::DomainMode GrTextureProducer::DetermineDomainMode(
Brian Salomon2bbdcc42017-09-07 12:36:34 -040087 const SkRect& constraintRect,
88 FilterConstraint filterConstraint,
89 bool coordsLimitedToConstraintRect,
Greg Danielcc104db2020-02-03 14:17:08 -050090 GrSurfaceProxy* proxy,
Brian Salomon2bbdcc42017-09-07 12:36:34 -040091 const GrSamplerState::Filter* filterModeOrNullForBicubic,
92 SkRect* domainRect) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -040093 const SkIRect proxyBounds = SkIRect::MakeSize(proxy->dimensions());
Robert Phillips51e7ca32017-03-27 10:14:08 -040094
95 SkASSERT(proxyBounds.contains(constraintRect));
Robert Phillips51e7ca32017-03-27 10:14:08 -040096
Brian Salomon5c60b752019-12-13 15:03:43 -050097 const bool proxyIsExact = proxy->isFunctionallyExact();
Brian Salomon4df00922017-09-07 16:34:11 +000098
Robert Phillips51e7ca32017-03-27 10:14:08 -040099 // If the constraint rectangle contains the whole proxy then no need for a domain.
100 if (constraintRect.contains(proxyBounds) && proxyIsExact) {
101 return kNoDomain_DomainMode;
102 }
103
Robert Phillips51e7ca32017-03-27 10:14:08 -0400104 bool restrictFilterToRect = (filterConstraint == GrTextureProducer::kYes_FilterConstraint);
105
106 // If we can filter outside the constraint rect, and there is no non-content area of the
107 // proxy, and we aren't going to generate sample coords outside the constraint rect then we
108 // don't need a domain.
Greg Danielc77085d2017-11-01 16:38:48 -0400109 if (!restrictFilterToRect && proxyIsExact && coordsLimitedToConstraintRect) {
Robert Phillips51e7ca32017-03-27 10:14:08 -0400110 return kNoDomain_DomainMode;
111 }
112
Brian Salomoned729f92020-02-05 12:15:18 -0500113 // Get the domain inset based on sampling mode (or bail if mipped). This is used
114 // to evaluate whether we will read outside a non-exact proxy's dimensions.
115 // TODO: Let GrTextureEffect handle this.
Brian Salomon4df00922017-09-07 16:34:11 +0000116 SkScalar filterHalfWidth = 0.f;
Robert Phillips51e7ca32017-03-27 10:14:08 -0400117 if (filterModeOrNullForBicubic) {
118 switch (*filterModeOrNullForBicubic) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400119 case GrSamplerState::Filter::kNearest:
Robert Phillips51e7ca32017-03-27 10:14:08 -0400120 if (coordsLimitedToConstraintRect) {
121 return kNoDomain_DomainMode;
Brian Salomon4df00922017-09-07 16:34:11 +0000122 } else {
123 filterHalfWidth = 0.f;
Robert Phillips51e7ca32017-03-27 10:14:08 -0400124 }
125 break;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400126 case GrSamplerState::Filter::kBilerp:
Brian Salomon4df00922017-09-07 16:34:11 +0000127 filterHalfWidth = .5f;
Robert Phillips51e7ca32017-03-27 10:14:08 -0400128 break;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400129 case GrSamplerState::Filter::kMipMap:
Greg Danielc77085d2017-11-01 16:38:48 -0400130 if (restrictFilterToRect || !proxyIsExact) {
Brian Salomon4df00922017-09-07 16:34:11 +0000131 // No domain can save us here.
132 return kTightCopy_DomainMode;
133 }
134 return kNoDomain_DomainMode;
Robert Phillips51e7ca32017-03-27 10:14:08 -0400135 }
Brian Salomon4df00922017-09-07 16:34:11 +0000136 } else {
137 // bicubic does nearest filtering internally.
138 filterHalfWidth = 1.5f;
Robert Phillips51e7ca32017-03-27 10:14:08 -0400139 }
140
Robert Phillips51e7ca32017-03-27 10:14:08 -0400141 if (restrictFilterToRect) {
Brian Salomoned729f92020-02-05 12:15:18 -0500142 *domainRect = constraintRect;
Greg Danielc77085d2017-11-01 16:38:48 -0400143 } else if (!proxyIsExact) {
144 // If we got here then: proxy is not exact, the coords are limited to the
Brian Salomon4df00922017-09-07 16:34:11 +0000145 // constraint rect, and we're allowed to filter across the constraint rect boundary. So
Greg Danielc77085d2017-11-01 16:38:48 -0400146 // we check whether the filter would reach across the edge of the proxy.
Brian Salomon4df00922017-09-07 16:34:11 +0000147 // We will only set the sides that are required.
148
Mike Reed274218e2018-01-08 15:05:02 -0500149 *domainRect = SkRectPriv::MakeLargest();
Brian Salomon4df00922017-09-07 16:34:11 +0000150 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 Danielc77085d2017-11-01 16:38:48 -0400154 if (proxyBounds.fRight - filterHalfWidth < constraintRect.fRight) {
Brian Salomoned729f92020-02-05 12:15:18 -0500155 domainRect->fRight = proxyBounds.fRight;
Brian Salomon4df00922017-09-07 16:34:11 +0000156 needContentAreaConstraint = true;
157 }
Greg Danielc77085d2017-11-01 16:38:48 -0400158 if (proxyBounds.fBottom - filterHalfWidth < constraintRect.fBottom) {
Brian Salomoned729f92020-02-05 12:15:18 -0500159 domainRect->fBottom = proxyBounds.fBottom;
Brian Salomon4df00922017-09-07 16:34:11 +0000160 needContentAreaConstraint = true;
161 }
162 if (!needContentAreaConstraint) {
163 return kNoDomain_DomainMode;
164 }
Robert Phillips51e7ca32017-03-27 10:14:08 -0400165 }
Brian Salomon4df00922017-09-07 16:34:11 +0000166 } else {
167 return kNoDomain_DomainMode;
Robert Phillips51e7ca32017-03-27 10:14:08 -0400168 }
Brian Salomon4df00922017-09-07 16:34:11 +0000169
Brian Salomoned729f92020-02-05 12:15:18 -0500170 if (!filterModeOrNullForBicubic) {
171 // Bicubic doesn't yet rely on GrTextureEffect to do this insetting.
172 domainRect->inset(0.5f, 0.5f);
173 if (domainRect->fLeft > domainRect->fRight) {
174 domainRect->fLeft = domainRect->fRight =
175 SkScalarAve(domainRect->fLeft, domainRect->fRight);
176 }
177 if (domainRect->fTop > domainRect->fBottom) {
178 domainRect->fTop = domainRect->fBottom =
179 SkScalarAve(domainRect->fTop, domainRect->fBottom);
180 }
Brian Salomon4df00922017-09-07 16:34:11 +0000181 }
Brian Salomoned729f92020-02-05 12:15:18 -0500182
Brian Salomon4df00922017-09-07 16:34:11 +0000183 return kDomain_DomainMode;
Robert Phillips51e7ca32017-03-27 10:14:08 -0400184}
185
Michael Ludwigddeed372019-02-20 16:50:10 -0500186std::unique_ptr<GrFragmentProcessor> GrTextureProducer::createFragmentProcessorForDomainAndFilter(
Greg Danielcc104db2020-02-03 14:17:08 -0500187 GrSurfaceProxyView view,
Brian Salomonaff329b2017-08-11 09:40:37 -0400188 const SkMatrix& textureMatrix,
189 DomainMode domainMode,
190 const SkRect& domain,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400191 const GrSamplerState::Filter* filterOrNullForBicubic) {
Robert Phillipsb66b42f2017-03-14 08:53:02 -0400192 SkASSERT(kTightCopy_DomainMode != domainMode);
Greg Danielcc104db2020-02-03 14:17:08 -0500193 SkASSERT(view.asTextureProxy());
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();
Brian Salomond0d033a2020-02-18 16:59:28 -0500196 auto wm = fDomainNeedsDecal ? GrSamplerState::WrapMode::kClampToBorder
197 : GrSamplerState::WrapMode::kClamp;
Robert Phillipsb66b42f2017-03-14 08:53:02 -0400198 if (filterOrNullForBicubic) {
Brian Salomond0d033a2020-02-18 16:59:28 -0500199 GrSamplerState samplerState(wm, *filterOrNullForBicubic);
Brian Salomonca6b2f42020-01-24 11:31:21 -0500200 if (kNoDomain_DomainMode == domainMode) {
Greg Danield2ccbb52020-02-05 10:45:39 -0500201 return GrTextureEffect::Make(std::move(view), srcAlphaType, textureMatrix, samplerState,
202 caps);
Robert Phillipsb66b42f2017-03-14 08:53:02 -0400203 }
Greg Danield2ccbb52020-02-05 10:45:39 -0500204 return GrTextureEffect::MakeSubset(std::move(view), srcAlphaType, textureMatrix,
Brian Salomonca6b2f42020-01-24 11:31:21 -0500205 samplerState, domain, caps);
Robert Phillipsb66b42f2017-03-14 08:53:02 -0400206 } else {
Michael Ludwigddeed372019-02-20 16:50:10 -0500207
Brian Salomona86fc7a2019-05-28 20:42:58 -0400208 static constexpr auto kDir = GrBicubicEffect::Direction::kXY;
Brian Salomond0d033a2020-02-18 16:59:28 -0500209 const auto& caps = *fContext->priv().caps();
210 if (kDomain_DomainMode == domainMode) {
211 return GrBicubicEffect::MakeSubset(std::move(view), srcAlphaType, textureMatrix, wm, wm,
212 domain, kDir, caps);
Robert Phillipsb66b42f2017-03-14 08:53:02 -0400213 } else {
Brian Salomond0d033a2020-02-18 16:59:28 -0500214 return GrBicubicEffect::Make(std::move(view), srcAlphaType, textureMatrix, wm, wm, kDir,
215 caps);
Robert Phillipsb66b42f2017-03-14 08:53:02 -0400216 }
217 }
218}
Brian Salomon2a943df2018-05-04 13:43:19 -0400219
Greg Danielc61d7e32020-02-04 14:27:45 -0500220GrSurfaceProxyView GrTextureProducer::viewForParams(
Robert Phillipsbf5cb0f2020-02-21 13:46:38 +0000221 const GrSamplerState::Filter* filterOrNullForBicubic, SkScalar scaleAdjust[2]) {
Michael Ludwigddeed372019-02-20 16:50:10 -0500222 GrSamplerState sampler; // Default is nearest + clamp
223 if (filterOrNullForBicubic) {
224 sampler.setFilterMode(*filterOrNullForBicubic);
225 }
226 if (fDomainNeedsDecal) {
227 // Assuming hardware support, switch to clamp-to-border instead of clamp
228 if (fContext->priv().caps()->clampToBorderSupport()) {
229 sampler.setWrapModeX(GrSamplerState::WrapMode::kClampToBorder);
230 sampler.setWrapModeY(GrSamplerState::WrapMode::kClampToBorder);
231 }
232 }
Robert Phillipsbf5cb0f2020-02-21 13:46:38 +0000233 return this->viewForParams(sampler, scaleAdjust);
Michael Ludwigddeed372019-02-20 16:50:10 -0500234}
235
Robert Phillipsbf5cb0f2020-02-21 13:46:38 +0000236GrSurfaceProxyView GrTextureProducer::viewForParams(GrSamplerState sampler,
237 SkScalar scaleAdjust[2]) {
238 // Check that the caller pre-initialized scaleAdjust
239 SkASSERT(!scaleAdjust || (scaleAdjust[0] == 1 && scaleAdjust[1] == 1));
240
Greg Daniel82c6b102020-01-21 10:33:22 -0500241 const GrCaps* caps = this->context()->priv().caps();
Greg Daniel8e9b4c42018-07-20 10:30:48 -0400242
243 int mipCount = SkMipMap::ComputeLevelCount(this->width(), this->height());
244 bool willBeMipped = GrSamplerState::Filter::kMipMap == sampler.filter() && mipCount &&
Greg Daniel82c6b102020-01-21 10:33:22 -0500245 caps->mipMapSupport();
Greg Daniel8e9b4c42018-07-20 10:30:48 -0400246
Robert Phillipsbf5cb0f2020-02-21 13:46:38 +0000247 auto result = this->onRefTextureProxyViewForParams(sampler, willBeMipped, scaleAdjust);
Brian Salomon2a943df2018-05-04 13:43:19 -0400248
Greg Daniel022b1e02018-07-20 14:54:00 -0400249 // Check to make sure that if we say the texture willBeMipped that the returned texture has mip
250 // maps, unless the config is not copyable.
Robert Phillipsbf5cb0f2020-02-21 13:46:38 +0000251 SkASSERT(!result.proxy() || !willBeMipped ||
252 result.asTextureProxy()->mipMapped() == GrMipMapped::kYes ||
Greg Danielcc104db2020-02-03 14:17:08 -0500253 !caps->isFormatCopyable(result.proxy()->backendFormat()));
254
Robert Phillipsbf5cb0f2020-02-21 13:46:38 +0000255 SkASSERT(!result.proxy() || result.asTextureProxy());
256
257 SkDEBUGCODE(bool expectNoScale = (sampler.filter() != GrSamplerState::Filter::kMipMap &&
258 !sampler.isRepeated()));
259 // Check that the "no scaling expected" case always returns a proxy of the same size as the
260 // producer.
261 SkASSERT(!result.proxy() || !expectNoScale ||
262 result.proxy()->dimensions() == this->dimensions());
Greg Daniel82c6b102020-01-21 10:33:22 -0500263
Brian Salomon2a943df2018-05-04 13:43:19 -0400264 return result;
265}
Greg Daniel5f4b09d2018-06-12 16:39:59 -0400266
Greg Danielc61d7e32020-02-04 14:27:45 -0500267std::pair<GrSurfaceProxyView, GrColorType> GrTextureProducer::view(GrMipMapped willNeedMips) {
Greg Daniel5f4b09d2018-06-12 16:39:59 -0400268 GrSamplerState::Filter filter =
269 GrMipMapped::kNo == willNeedMips ? GrSamplerState::Filter::kNearest
270 : GrSamplerState::Filter::kMipMap;
271 GrSamplerState sampler(GrSamplerState::WrapMode::kClamp, filter);
Greg Daniel8e9b4c42018-07-20 10:30:48 -0400272
Robert Phillipsbf5cb0f2020-02-21 13:46:38 +0000273 auto result = this->viewForParams(sampler, nullptr);
Greg Daniel8e9b4c42018-07-20 10:30:48 -0400274
Greg Daniel82c6b102020-01-21 10:33:22 -0500275#ifdef SK_DEBUG
276 const GrCaps* caps = this->context()->priv().caps();
277 // Check that the resulting proxy format is compatible with the GrColorType of this producer
Greg Danielcc104db2020-02-03 14:17:08 -0500278 SkASSERT(!result.proxy() ||
279 result.proxy()->isFormatCompressed(caps) ||
280 caps->areColorTypeAndFormatCompatible(this->colorType(),
281 result.proxy()->backendFormat()));
Greg Daniel82c6b102020-01-21 10:33:22 -0500282#endif
Greg Daniel5f4b09d2018-06-12 16:39:59 -0400283
Greg Daniel82c6b102020-01-21 10:33:22 -0500284 return {result, this->colorType()};
Greg Daniel5f4b09d2018-06-12 16:39:59 -0400285}