blob: 6d667ba133c31fef1e3f8544ae20cf482f90ffbe [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 Danielc61d7e32020-02-04 14:27:45 -050036GrSurfaceProxyView GrTextureAdjuster::copy(const CopyParams& copyParams, bool willBeMipped,
37 bool copyForMipsOnly) {
Robert Phillips9338c602019-02-19 12:52:29 -050038 GrProxyProvider* proxyProvider = this->context()->priv().proxyProvider();
Robert Phillips1afd4cd2018-01-08 13:40:32 -050039
Robert Phillips0c984a02017-03-16 07:51:56 -040040 GrUniqueKey key;
Brian Osmanb3f38302018-09-07 15:24:44 -040041 this->makeCopyKey(copyParams, &key);
Greg Daniel09c94002018-06-08 22:11:51 +000042 sk_sp<GrTextureProxy> cachedCopy;
Greg Danielcc104db2020-02-03 14:17:08 -050043 const GrSurfaceProxyView& originalView = this->originalProxyView();
Robert Phillips0c984a02017-03-16 07:51:56 -040044 if (key.isValid()) {
Brian Salomon2af3e702019-08-11 19:10:31 -040045 cachedCopy = proxyProvider->findOrCreateProxyByUniqueKey(key, this->colorType(),
Greg Danielcc104db2020-02-03 14:17:08 -050046 originalView.origin());
Greg Daniel09c94002018-06-08 22:11:51 +000047 if (cachedCopy && (!willBeMipped || GrMipMapped::kYes == cachedCopy->mipMapped())) {
Greg Danielcc104db2020-02-03 14:17:08 -050048 // TODO: Once we no longer use CopyOnGpu which can fallback to arbitrary formats and
49 // colorTypes, we can use the swizzle of the originalView.
50 GrSwizzle swizzle = cachedCopy->textureSwizzle();
51 return GrSurfaceProxyView(std::move(cachedCopy), originalView.origin(), swizzle);
Robert Phillips0c984a02017-03-16 07:51:56 -040052 }
53 }
54
Greg Danielcc104db2020-02-03 14:17:08 -050055 GrSurfaceProxyView copyView;
Greg Danielcce65002020-01-23 11:16:49 -050056 if (copyForMipsOnly) {
Greg Danielcc104db2020-02-03 14:17:08 -050057 copyView = GrCopyBaseMipMapToTextureProxy(this->context(), originalView.proxy(),
58 originalView.origin(), this->colorType());
Greg Danielcce65002020-01-23 11:16:49 -050059 } else {
Greg Danielcc104db2020-02-03 14:17:08 -050060 copyView = CopyOnGpu(this->context(), this->originalProxyViewRef(), this->colorType(),
61 copyParams, willBeMipped);
Greg Danielcce65002020-01-23 11:16:49 -050062 }
Greg Danielcc104db2020-02-03 14:17:08 -050063 if (copyView.proxy()) {
Robert Phillips0c984a02017-03-16 07:51:56 -040064 if (key.isValid()) {
Greg Danielcc104db2020-02-03 14:17:08 -050065 SkASSERT(copyView.origin() == originalView.origin());
Greg Daniel09c94002018-06-08 22:11:51 +000066 if (cachedCopy) {
Greg Danielcc104db2020-02-03 14:17:08 -050067 SkASSERT(GrMipMapped::kYes == copyView.asTextureProxy()->mipMapped() &&
Greg Daniel09c94002018-06-08 22:11:51 +000068 GrMipMapped::kNo == cachedCopy->mipMapped());
69 // If we had a cachedProxy, that means there already is a proxy in the cache which
70 // matches the key, but it does not have mip levels and we require them. Thus we
71 // must remove the unique key from that proxy.
Chris Dalton2de13dd2019-01-03 15:11:59 -070072 SkASSERT(cachedCopy->getUniqueKey() == key);
73 proxyProvider->removeUniqueKeyFromProxy(cachedCopy.get());
Greg Daniel09c94002018-06-08 22:11:51 +000074 }
Greg Danielcc104db2020-02-03 14:17:08 -050075 proxyProvider->assignUniqueKeyToProxy(key, copyView.asTextureProxy());
Robert Phillipsa41c6852019-02-07 10:44:10 -050076 this->didCacheCopy(key, proxyProvider->contextID());
Brian Osmane8e54582016-11-28 10:06:27 -050077 }
78 }
Greg Danielcc104db2020-02-03 14:17:08 -050079 return copyView;
Brian Osmane8e54582016-11-28 10:06:27 -050080}
81
Greg Danielcc104db2020-02-03 14:17:08 -050082GrSurfaceProxyView GrTextureAdjuster::onRefTextureProxyViewForParams(GrSamplerState params,
83 bool willBeMipped,
84 SkScalar scaleAdjust[2]) {
Robert Phillips9338c602019-02-19 12:52:29 -050085 if (this->context()->priv().abandoned()) {
Brian Osmane8e54582016-11-28 10:06:27 -050086 // The texture was abandoned.
Greg Danielcc104db2020-02-03 14:17:08 -050087 return {};
Brian Osmane8e54582016-11-28 10:06:27 -050088 }
Brian Salomon4df00922017-09-07 16:34:11 +000089
Robert Phillips9338c602019-02-19 12:52:29 -050090 SkASSERT(this->width() <= this->context()->priv().caps()->maxTextureSize() &&
91 this->height() <= this->context()->priv().caps()->maxTextureSize());
Brian Osman875f7852018-04-12 13:29:08 -040092
Greg Danielcc104db2020-02-03 14:17:08 -050093 GrSurfaceProxyView view = this->originalProxyViewRef();
94 GrTextureProxy* texProxy = view.asTextureProxy();
95 SkASSERT(texProxy);
96 CopyParams copyParams;
97
Greg Daniel8f5bbda2018-06-08 17:22:23 -040098 bool needsCopyForMipsOnly = false;
99 if (!params.isRepeated() ||
Greg Danielcc104db2020-02-03 14:17:08 -0500100 !GrGpu::IsACopyNeededForRepeatWrapMode(this->context()->priv().caps(), texProxy,
101 texProxy->dimensions(), params.filter(), &copyParams,
Brian Salomon1a217eb2019-10-24 10:50:36 -0400102 scaleAdjust)) {
Robert Phillips9338c602019-02-19 12:52:29 -0500103 needsCopyForMipsOnly = GrGpu::IsACopyNeededForMips(this->context()->priv().caps(),
Greg Danielcc104db2020-02-03 14:17:08 -0500104 texProxy, params.filter(),
Greg Daniel8f5bbda2018-06-08 17:22:23 -0400105 &copyParams);
106 if (!needsCopyForMipsOnly) {
Greg Danielcc104db2020-02-03 14:17:08 -0500107 return view;
Greg Daniel8f5bbda2018-06-08 17:22:23 -0400108 }
Brian Osmane8e54582016-11-28 10:06:27 -0500109 }
110
Greg Danielc61d7e32020-02-04 14:27:45 -0500111 GrSurfaceProxyView result = this->copy(copyParams, willBeMipped, needsCopyForMipsOnly);
Greg Danielcc104db2020-02-03 14:17:08 -0500112 if (!result.proxy() && needsCopyForMipsOnly) {
Greg Daniel8f5bbda2018-06-08 17:22:23 -0400113 // If we were unable to make a copy and we only needed a copy for mips, then we will return
114 // the source texture here and require that the GPU backend is able to fall back to using
115 // bilerp if mips are required.
Greg Danielcc104db2020-02-03 14:17:08 -0500116 return view;
Greg Daniel8f5bbda2018-06-08 17:22:23 -0400117 }
Greg Danielcc104db2020-02-03 14:17:08 -0500118 SkASSERT(result.asTextureProxy());
Greg Daniel8f5bbda2018-06-08 17:22:23 -0400119 return result;
Brian Osmane8e54582016-11-28 10:06:27 -0500120}
121
Brian Salomonaff329b2017-08-11 09:40:37 -0400122std::unique_ptr<GrFragmentProcessor> GrTextureAdjuster::createFragmentProcessor(
123 const SkMatrix& origTextureMatrix,
Greg Danielc77085d2017-11-01 16:38:48 -0400124 const SkRect& constraintRect,
Brian Salomonaff329b2017-08-11 09:40:37 -0400125 FilterConstraint filterConstraint,
126 bool coordsLimitedToConstraintRect,
Brian Osman05c8f462018-10-22 17:13:36 -0400127 const GrSamplerState::Filter* filterOrNullForBicubic) {
Brian Osmane8e54582016-11-28 10:06:27 -0500128 SkMatrix textureMatrix = origTextureMatrix;
Brian Osmane8e54582016-11-28 10:06:27 -0500129
Robert Phillips67c18d62017-01-20 12:44:06 -0500130 SkScalar scaleAdjust[2] = { 1.0f, 1.0f };
Greg Danielc61d7e32020-02-04 14:27:45 -0500131 GrSurfaceProxyView view = this->viewForParams(filterOrNullForBicubic, scaleAdjust);
Greg Danielcc104db2020-02-03 14:17:08 -0500132 if (!view.proxy()) {
Brian Osmane8e54582016-11-28 10:06:27 -0500133 return nullptr;
134 }
Greg Danielcc104db2020-02-03 14:17:08 -0500135 SkASSERT(view.asTextureProxy());
Brian Osmane8e54582016-11-28 10:06:27 -0500136 // If we made a copy then we only copied the contentArea, in which case the new texture is all
137 // content.
Greg Danielcc104db2020-02-03 14:17:08 -0500138 if (view.proxy() != this->originalProxyView().proxy()) {
Robert Phillips67c18d62017-01-20 12:44:06 -0500139 textureMatrix.postScale(scaleAdjust[0], scaleAdjust[1]);
Brian Osmane8e54582016-11-28 10:06:27 -0500140 }
141
Michael Ludwigddeed372019-02-20 16:50:10 -0500142 SkRect domain;
Brian Osmane8e54582016-11-28 10:06:27 -0500143 DomainMode domainMode =
Greg Danielc77085d2017-11-01 16:38:48 -0400144 DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect,
Greg Danielcc104db2020-02-03 14:17:08 -0500145 view.proxy(), filterOrNullForBicubic, &domain);
Brian Osmane8e54582016-11-28 10:06:27 -0500146 if (kTightCopy_DomainMode == domainMode) {
147 // TODO: Copy the texture and adjust the texture matrix (both parts need to consider
148 // non-int constraint rect)
149 // For now: treat as bilerp and ignore what goes on above level 0.
150
151 // We only expect MIP maps to require a tight copy.
152 SkASSERT(filterOrNullForBicubic &&
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400153 GrSamplerState::Filter::kMipMap == *filterOrNullForBicubic);
154 static const GrSamplerState::Filter kBilerp = GrSamplerState::Filter::kBilerp;
Brian Osmane8e54582016-11-28 10:06:27 -0500155 domainMode =
Greg Danielc77085d2017-11-01 16:38:48 -0400156 DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect,
Greg Danielcc104db2020-02-03 14:17:08 -0500157 view.proxy(), &kBilerp, &domain);
Brian Osmane8e54582016-11-28 10:06:27 -0500158 SkASSERT(kTightCopy_DomainMode != domainMode);
159 }
160 SkASSERT(kNoDomain_DomainMode == domainMode ||
161 (domain.fLeft <= domain.fRight && domain.fTop <= domain.fBottom));
Michael Ludwigddeed372019-02-20 16:50:10 -0500162 return this->createFragmentProcessorForDomainAndFilter(
Greg Danielcc104db2020-02-03 14:17:08 -0500163 std::move(view), textureMatrix, domainMode, domain, filterOrNullForBicubic);
Brian Osmane8e54582016-11-28 10:06:27 -0500164}