blob: 2b1495d487aa3ced6b47131c1c265b5480c54698 [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"
9#include "src/gpu/GrColorSpaceXform.h"
10#include "src/gpu/GrGpu.h"
11#include "src/gpu/GrProxyProvider.h"
12#include "src/gpu/GrRecordingContextPriv.h"
13#include "src/gpu/GrTextureAdjuster.h"
14#include "src/gpu/SkGr.h"
Brian Osmane8e54582016-11-28 10:06:27 -050015
Brian Salomone7499c72019-06-24 12:12:36 -040016GrTextureAdjuster::GrTextureAdjuster(GrRecordingContext* context,
Greg Danielcc104db2020-02-03 14:17:08 -050017 GrSurfaceProxyView original,
Greg Daniela4828a12019-10-11 13:51:02 -040018 const GrColorInfo& colorInfo,
19 uint32_t uniqueID,
20 bool useDecal)
Greg Danielcc104db2020-02-03 14:17:08 -050021 : INHERITED(context, {colorInfo, original.proxy()->dimensions()}, useDecal)
Greg Daniela4828a12019-10-11 13:51:02 -040022 , fOriginal(std::move(original))
23 , fUniqueID(uniqueID) {}
24
Brian Osmanb3f38302018-09-07 15:24:44 -040025void GrTextureAdjuster::makeCopyKey(const CopyParams& params, GrUniqueKey* copyKey) {
Brian Osman61624f02016-12-09 14:51:59 -050026 // Destination color space is irrelevant - we already have a texture so we're just sub-setting
Brian Osmane8e54582016-11-28 10:06:27 -050027 GrUniqueKey baseKey;
Brian Salomon1a217eb2019-10-24 10:50:36 -040028 GrMakeKeyFromImageID(&baseKey, fUniqueID, SkIRect::MakeSize(this->dimensions()));
Brian Osmane8e54582016-11-28 10:06:27 -050029 MakeCopyKeyFromOrigKey(baseKey, params, copyKey);
30}
31
Brian Salomon238069b2018-07-11 15:58:57 -040032void GrTextureAdjuster::didCacheCopy(const GrUniqueKey& copyKey, uint32_t contextUniqueID) {
Brian Osmane8e54582016-11-28 10:06:27 -050033 // We don't currently have a mechanism for notifications on Images!
34}
35
Greg Danielcc104db2020-02-03 14:17:08 -050036GrSurfaceProxyView GrTextureAdjuster::refTextureProxyViewCopy(const CopyParams& copyParams,
37 bool willBeMipped,
38 bool copyForMipsOnly) {
Robert Phillips9338c602019-02-19 12:52:29 -050039 GrProxyProvider* proxyProvider = this->context()->priv().proxyProvider();
Robert Phillips1afd4cd2018-01-08 13:40:32 -050040
Robert Phillips0c984a02017-03-16 07:51:56 -040041 GrUniqueKey key;
Brian Osmanb3f38302018-09-07 15:24:44 -040042 this->makeCopyKey(copyParams, &key);
Greg Daniel09c94002018-06-08 22:11:51 +000043 sk_sp<GrTextureProxy> cachedCopy;
Greg Danielcc104db2020-02-03 14:17:08 -050044 const GrSurfaceProxyView& originalView = this->originalProxyView();
Robert Phillips0c984a02017-03-16 07:51:56 -040045 if (key.isValid()) {
Brian Salomon2af3e702019-08-11 19:10:31 -040046 cachedCopy = proxyProvider->findOrCreateProxyByUniqueKey(key, this->colorType(),
Greg Danielcc104db2020-02-03 14:17:08 -050047 originalView.origin());
Greg Daniel09c94002018-06-08 22:11:51 +000048 if (cachedCopy && (!willBeMipped || GrMipMapped::kYes == cachedCopy->mipMapped())) {
Greg Danielcc104db2020-02-03 14:17:08 -050049 // TODO: Once we no longer use CopyOnGpu which can fallback to arbitrary formats and
50 // colorTypes, we can use the swizzle of the originalView.
51 GrSwizzle swizzle = cachedCopy->textureSwizzle();
52 return GrSurfaceProxyView(std::move(cachedCopy), originalView.origin(), swizzle);
Robert Phillips0c984a02017-03-16 07:51:56 -040053 }
54 }
55
Greg Danielcc104db2020-02-03 14:17:08 -050056 GrSurfaceProxyView copyView;
Greg Danielcce65002020-01-23 11:16:49 -050057 if (copyForMipsOnly) {
Greg Danielcc104db2020-02-03 14:17:08 -050058 copyView = GrCopyBaseMipMapToTextureProxy(this->context(), originalView.proxy(),
59 originalView.origin(), this->colorType());
Greg Danielcce65002020-01-23 11:16:49 -050060 } else {
Greg Danielcc104db2020-02-03 14:17:08 -050061 copyView = CopyOnGpu(this->context(), this->originalProxyViewRef(), this->colorType(),
62 copyParams, willBeMipped);
Greg Danielcce65002020-01-23 11:16:49 -050063 }
Greg Danielcc104db2020-02-03 14:17:08 -050064 if (copyView.proxy()) {
Robert Phillips0c984a02017-03-16 07:51:56 -040065 if (key.isValid()) {
Greg Danielcc104db2020-02-03 14:17:08 -050066 SkASSERT(copyView.origin() == originalView.origin());
Greg Daniel09c94002018-06-08 22:11:51 +000067 if (cachedCopy) {
Greg Danielcc104db2020-02-03 14:17:08 -050068 SkASSERT(GrMipMapped::kYes == copyView.asTextureProxy()->mipMapped() &&
Greg Daniel09c94002018-06-08 22:11:51 +000069 GrMipMapped::kNo == cachedCopy->mipMapped());
70 // If we had a cachedProxy, that means there already is a proxy in the cache which
71 // matches the key, but it does not have mip levels and we require them. Thus we
72 // must remove the unique key from that proxy.
Chris Dalton2de13dd2019-01-03 15:11:59 -070073 SkASSERT(cachedCopy->getUniqueKey() == key);
74 proxyProvider->removeUniqueKeyFromProxy(cachedCopy.get());
Greg Daniel09c94002018-06-08 22:11:51 +000075 }
Greg Danielcc104db2020-02-03 14:17:08 -050076 proxyProvider->assignUniqueKeyToProxy(key, copyView.asTextureProxy());
Robert Phillipsa41c6852019-02-07 10:44:10 -050077 this->didCacheCopy(key, proxyProvider->contextID());
Brian Osmane8e54582016-11-28 10:06:27 -050078 }
79 }
Greg Danielcc104db2020-02-03 14:17:08 -050080 return copyView;
Brian Osmane8e54582016-11-28 10:06:27 -050081}
82
Greg Danielcc104db2020-02-03 14:17:08 -050083GrSurfaceProxyView GrTextureAdjuster::onRefTextureProxyViewForParams(GrSamplerState params,
84 bool willBeMipped,
85 SkScalar scaleAdjust[2]) {
Robert Phillips9338c602019-02-19 12:52:29 -050086 if (this->context()->priv().abandoned()) {
Brian Osmane8e54582016-11-28 10:06:27 -050087 // The texture was abandoned.
Greg Danielcc104db2020-02-03 14:17:08 -050088 return {};
Brian Osmane8e54582016-11-28 10:06:27 -050089 }
Brian Salomon4df00922017-09-07 16:34:11 +000090
Robert Phillips9338c602019-02-19 12:52:29 -050091 SkASSERT(this->width() <= this->context()->priv().caps()->maxTextureSize() &&
92 this->height() <= this->context()->priv().caps()->maxTextureSize());
Brian Osman875f7852018-04-12 13:29:08 -040093
Greg Danielcc104db2020-02-03 14:17:08 -050094 GrSurfaceProxyView view = this->originalProxyViewRef();
95 GrTextureProxy* texProxy = view.asTextureProxy();
96 SkASSERT(texProxy);
97 CopyParams copyParams;
98
Greg Daniel8f5bbda2018-06-08 17:22:23 -040099 bool needsCopyForMipsOnly = false;
100 if (!params.isRepeated() ||
Greg Danielcc104db2020-02-03 14:17:08 -0500101 !GrGpu::IsACopyNeededForRepeatWrapMode(this->context()->priv().caps(), texProxy,
102 texProxy->dimensions(), params.filter(), &copyParams,
Brian Salomon1a217eb2019-10-24 10:50:36 -0400103 scaleAdjust)) {
Robert Phillips9338c602019-02-19 12:52:29 -0500104 needsCopyForMipsOnly = GrGpu::IsACopyNeededForMips(this->context()->priv().caps(),
Greg Danielcc104db2020-02-03 14:17:08 -0500105 texProxy, params.filter(),
Greg Daniel8f5bbda2018-06-08 17:22:23 -0400106 &copyParams);
107 if (!needsCopyForMipsOnly) {
Greg Danielcc104db2020-02-03 14:17:08 -0500108 return view;
Greg Daniel8f5bbda2018-06-08 17:22:23 -0400109 }
Brian Osmane8e54582016-11-28 10:06:27 -0500110 }
111
Greg Danielcc104db2020-02-03 14:17:08 -0500112 GrSurfaceProxyView result = this->refTextureProxyViewCopy(copyParams, willBeMipped,
113 needsCopyForMipsOnly);
114 if (!result.proxy() && needsCopyForMipsOnly) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -0400115 // If we were unable to make a copy and we only needed a copy for mips, then we will return
116 // the source texture here and require that the GPU backend is able to fall back to using
117 // bilerp if mips are required.
Greg Danielcc104db2020-02-03 14:17:08 -0500118 return view;
Greg Daniel8f5bbda2018-06-08 17:22:23 -0400119 }
Greg Danielcc104db2020-02-03 14:17:08 -0500120 SkASSERT(result.asTextureProxy());
Greg Daniel8f5bbda2018-06-08 17:22:23 -0400121 return result;
Brian Osmane8e54582016-11-28 10:06:27 -0500122}
123
Brian Salomonaff329b2017-08-11 09:40:37 -0400124std::unique_ptr<GrFragmentProcessor> GrTextureAdjuster::createFragmentProcessor(
125 const SkMatrix& origTextureMatrix,
Greg Danielc77085d2017-11-01 16:38:48 -0400126 const SkRect& constraintRect,
Brian Salomonaff329b2017-08-11 09:40:37 -0400127 FilterConstraint filterConstraint,
128 bool coordsLimitedToConstraintRect,
Brian Osman05c8f462018-10-22 17:13:36 -0400129 const GrSamplerState::Filter* filterOrNullForBicubic) {
Brian Osmane8e54582016-11-28 10:06:27 -0500130 SkMatrix textureMatrix = origTextureMatrix;
Brian Osmane8e54582016-11-28 10:06:27 -0500131
Robert Phillips67c18d62017-01-20 12:44:06 -0500132 SkScalar scaleAdjust[2] = { 1.0f, 1.0f };
Greg Danielcc104db2020-02-03 14:17:08 -0500133 GrSurfaceProxyView view =
134 this->refTextureProxyViewForParams(filterOrNullForBicubic, scaleAdjust);
135 if (!view.proxy()) {
Brian Osmane8e54582016-11-28 10:06:27 -0500136 return nullptr;
137 }
Greg Danielcc104db2020-02-03 14:17:08 -0500138 SkASSERT(view.asTextureProxy());
Brian Osmane8e54582016-11-28 10:06:27 -0500139 // If we made a copy then we only copied the contentArea, in which case the new texture is all
140 // content.
Greg Danielcc104db2020-02-03 14:17:08 -0500141 if (view.proxy() != this->originalProxyView().proxy()) {
Robert Phillips67c18d62017-01-20 12:44:06 -0500142 textureMatrix.postScale(scaleAdjust[0], scaleAdjust[1]);
Brian Osmane8e54582016-11-28 10:06:27 -0500143 }
144
Michael Ludwigddeed372019-02-20 16:50:10 -0500145 SkRect domain;
Brian Osmane8e54582016-11-28 10:06:27 -0500146 DomainMode domainMode =
Greg Danielc77085d2017-11-01 16:38:48 -0400147 DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect,
Greg Danielcc104db2020-02-03 14:17:08 -0500148 view.proxy(), filterOrNullForBicubic, &domain);
Brian Osmane8e54582016-11-28 10:06:27 -0500149 if (kTightCopy_DomainMode == domainMode) {
150 // TODO: Copy the texture and adjust the texture matrix (both parts need to consider
151 // non-int constraint rect)
152 // For now: treat as bilerp and ignore what goes on above level 0.
153
154 // We only expect MIP maps to require a tight copy.
155 SkASSERT(filterOrNullForBicubic &&
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400156 GrSamplerState::Filter::kMipMap == *filterOrNullForBicubic);
157 static const GrSamplerState::Filter kBilerp = GrSamplerState::Filter::kBilerp;
Brian Osmane8e54582016-11-28 10:06:27 -0500158 domainMode =
Greg Danielc77085d2017-11-01 16:38:48 -0400159 DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect,
Greg Danielcc104db2020-02-03 14:17:08 -0500160 view.proxy(), &kBilerp, &domain);
Brian Osmane8e54582016-11-28 10:06:27 -0500161 SkASSERT(kTightCopy_DomainMode != domainMode);
162 }
163 SkASSERT(kNoDomain_DomainMode == domainMode ||
164 (domain.fLeft <= domain.fRight && domain.fTop <= domain.fBottom));
Michael Ludwigddeed372019-02-20 16:50:10 -0500165 return this->createFragmentProcessorForDomainAndFilter(
Greg Danielcc104db2020-02-03 14:17:08 -0500166 std::move(view), textureMatrix, domainMode, domain, filterOrNullForBicubic);
Brian Osmane8e54582016-11-28 10:06:27 -0500167}