blob: d15a5ea0015bc5d8006ce1bbb5b43ec5ecd8775f [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,
17 sk_sp<GrTextureProxy> original,
Greg Daniela4828a12019-10-11 13:51:02 -040018 const GrColorInfo& colorInfo,
19 uint32_t uniqueID,
20 bool useDecal)
Brian Salomon1a217eb2019-10-24 10:50:36 -040021 : INHERITED(context, {colorInfo, original->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 Daniele1da1d92017-10-06 15:59:27 -040036sk_sp<GrTextureProxy> GrTextureAdjuster::refTextureProxyCopy(const CopyParams& copyParams,
Greg Danielcce65002020-01-23 11:16:49 -050037 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;
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 Daniel09c94002018-06-08 22:11:51 +000046 this->originalProxy()->origin());
47 if (cachedCopy && (!willBeMipped || GrMipMapped::kYes == cachedCopy->mipMapped())) {
Robert Phillips0c984a02017-03-16 07:51:56 -040048 return cachedCopy;
49 }
50 }
51
52 sk_sp<GrTextureProxy> proxy = this->originalProxyRef();
Robert Phillips0c984a02017-03-16 07:51:56 -040053
Greg Danielcce65002020-01-23 11:16:49 -050054 sk_sp<GrTextureProxy> copy;
55 if (copyForMipsOnly) {
Greg Daniel40903af2020-01-30 14:55:05 -050056 copy = GrCopyBaseMipMapToTextureProxy(this->context(), proxy.get(),
57 this->originalProxy()->origin(), this->colorType());
Greg Danielcce65002020-01-23 11:16:49 -050058 } else {
59 copy = CopyOnGpu(this->context(), std::move(proxy), this->colorType(),
60 copyParams, willBeMipped);
61 }
Robert Phillips0c984a02017-03-16 07:51:56 -040062 if (copy) {
63 if (key.isValid()) {
Robert Phillips78814092017-07-24 15:26:17 -040064 SkASSERT(copy->origin() == this->originalProxy()->origin());
Greg Daniel09c94002018-06-08 22:11:51 +000065 if (cachedCopy) {
66 SkASSERT(GrMipMapped::kYes == copy->mipMapped() &&
67 GrMipMapped::kNo == cachedCopy->mipMapped());
68 // If we had a cachedProxy, that means there already is a proxy in the cache which
69 // matches the key, but it does not have mip levels and we require them. Thus we
70 // must remove the unique key from that proxy.
Chris Dalton2de13dd2019-01-03 15:11:59 -070071 SkASSERT(cachedCopy->getUniqueKey() == key);
72 proxyProvider->removeUniqueKeyFromProxy(cachedCopy.get());
Greg Daniel09c94002018-06-08 22:11:51 +000073 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050074 proxyProvider->assignUniqueKeyToProxy(key, copy.get());
Robert Phillipsa41c6852019-02-07 10:44:10 -050075 this->didCacheCopy(key, proxyProvider->contextID());
Brian Osmane8e54582016-11-28 10:06:27 -050076 }
77 }
78 return copy;
79}
80
Brian Salomonccb61422020-01-09 10:46:36 -050081sk_sp<GrTextureProxy> GrTextureAdjuster::onRefTextureProxyForParams(GrSamplerState params,
82 bool willBeMipped,
83 SkScalar scaleAdjust[2]) {
Robert Phillips3798c862017-03-27 11:08:16 -040084 sk_sp<GrTextureProxy> proxy = this->originalProxyRef();
Brian Osmane8e54582016-11-28 10:06:27 -050085 CopyParams copyParams;
Brian Osmane8e54582016-11-28 10:06:27 -050086
Robert Phillips9338c602019-02-19 12:52:29 -050087 if (this->context()->priv().abandoned()) {
Brian Osmane8e54582016-11-28 10:06:27 -050088 // The texture was abandoned.
89 return nullptr;
90 }
Brian Salomon4df00922017-09-07 16:34:11 +000091
Robert Phillips9338c602019-02-19 12:52:29 -050092 SkASSERT(this->width() <= this->context()->priv().caps()->maxTextureSize() &&
93 this->height() <= this->context()->priv().caps()->maxTextureSize());
Brian Osman875f7852018-04-12 13:29:08 -040094
Greg Daniel8f5bbda2018-06-08 17:22:23 -040095 bool needsCopyForMipsOnly = false;
96 if (!params.isRepeated() ||
Robert Phillips9338c602019-02-19 12:52:29 -050097 !GrGpu::IsACopyNeededForRepeatWrapMode(this->context()->priv().caps(), proxy.get(),
Brian Salomon1a217eb2019-10-24 10:50:36 -040098 proxy->dimensions(), params.filter(), &copyParams,
99 scaleAdjust)) {
Robert Phillips9338c602019-02-19 12:52:29 -0500100 needsCopyForMipsOnly = GrGpu::IsACopyNeededForMips(this->context()->priv().caps(),
Greg Daniel8f5bbda2018-06-08 17:22:23 -0400101 proxy.get(), params.filter(),
102 &copyParams);
103 if (!needsCopyForMipsOnly) {
104 return proxy;
105 }
Brian Osmane8e54582016-11-28 10:06:27 -0500106 }
107
Greg Danielcce65002020-01-23 11:16:49 -0500108 sk_sp<GrTextureProxy> result = this->refTextureProxyCopy(copyParams, willBeMipped,
109 needsCopyForMipsOnly);
Greg Daniel8f5bbda2018-06-08 17:22:23 -0400110 if (!result && needsCopyForMipsOnly) {
111 // If we were unable to make a copy and we only needed a copy for mips, then we will return
112 // the source texture here and require that the GPU backend is able to fall back to using
113 // bilerp if mips are required.
114 return this->originalProxyRef();
115 }
116 return result;
Brian Osmane8e54582016-11-28 10:06:27 -0500117}
118
Brian Salomonaff329b2017-08-11 09:40:37 -0400119std::unique_ptr<GrFragmentProcessor> GrTextureAdjuster::createFragmentProcessor(
120 const SkMatrix& origTextureMatrix,
Greg Danielc77085d2017-11-01 16:38:48 -0400121 const SkRect& constraintRect,
Brian Salomonaff329b2017-08-11 09:40:37 -0400122 FilterConstraint filterConstraint,
123 bool coordsLimitedToConstraintRect,
Brian Osman05c8f462018-10-22 17:13:36 -0400124 const GrSamplerState::Filter* filterOrNullForBicubic) {
Brian Osmane8e54582016-11-28 10:06:27 -0500125 SkMatrix textureMatrix = origTextureMatrix;
Brian Osmane8e54582016-11-28 10:06:27 -0500126
Robert Phillips67c18d62017-01-20 12:44:06 -0500127 SkScalar scaleAdjust[2] = { 1.0f, 1.0f };
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400128 sk_sp<GrTextureProxy> proxy(
Michael Ludwigddeed372019-02-20 16:50:10 -0500129 this->refTextureProxyForParams(filterOrNullForBicubic, scaleAdjust));
Robert Phillips3798c862017-03-27 11:08:16 -0400130 if (!proxy) {
Brian Osmane8e54582016-11-28 10:06:27 -0500131 return nullptr;
132 }
133 // If we made a copy then we only copied the contentArea, in which case the new texture is all
134 // content.
Robert Phillips3798c862017-03-27 11:08:16 -0400135 if (proxy.get() != this->originalProxy()) {
Robert Phillips67c18d62017-01-20 12:44:06 -0500136 textureMatrix.postScale(scaleAdjust[0], scaleAdjust[1]);
Brian Osmane8e54582016-11-28 10:06:27 -0500137 }
138
Michael Ludwigddeed372019-02-20 16:50:10 -0500139 SkRect domain;
Brian Osmane8e54582016-11-28 10:06:27 -0500140 DomainMode domainMode =
Greg Danielc77085d2017-11-01 16:38:48 -0400141 DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect,
142 proxy.get(), filterOrNullForBicubic, &domain);
Brian Osmane8e54582016-11-28 10:06:27 -0500143 if (kTightCopy_DomainMode == domainMode) {
144 // TODO: Copy the texture and adjust the texture matrix (both parts need to consider
145 // non-int constraint rect)
146 // For now: treat as bilerp and ignore what goes on above level 0.
147
148 // We only expect MIP maps to require a tight copy.
149 SkASSERT(filterOrNullForBicubic &&
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400150 GrSamplerState::Filter::kMipMap == *filterOrNullForBicubic);
151 static const GrSamplerState::Filter kBilerp = GrSamplerState::Filter::kBilerp;
Brian Osmane8e54582016-11-28 10:06:27 -0500152 domainMode =
Greg Danielc77085d2017-11-01 16:38:48 -0400153 DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect,
154 proxy.get(), &kBilerp, &domain);
Brian Osmane8e54582016-11-28 10:06:27 -0500155 SkASSERT(kTightCopy_DomainMode != domainMode);
156 }
157 SkASSERT(kNoDomain_DomainMode == domainMode ||
158 (domain.fLeft <= domain.fRight && domain.fTop <= domain.fBottom));
Michael Ludwigddeed372019-02-20 16:50:10 -0500159 return this->createFragmentProcessorForDomainAndFilter(
160 std::move(proxy), textureMatrix, domainMode, domain, filterOrNullForBicubic);
Brian Osmane8e54582016-11-28 10:06:27 -0500161}