blob: 529d33d64f07ae70e882f82e09a6195727191bb9 [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,
37 bool willBeMipped) {
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;
Robert Phillips0c984a02017-03-16 07:51:56 -040043 if (key.isValid()) {
Brian Salomon2af3e702019-08-11 19:10:31 -040044 cachedCopy = proxyProvider->findOrCreateProxyByUniqueKey(key, this->colorType(),
Greg Daniel09c94002018-06-08 22:11:51 +000045 this->originalProxy()->origin());
46 if (cachedCopy && (!willBeMipped || GrMipMapped::kYes == cachedCopy->mipMapped())) {
Robert Phillips0c984a02017-03-16 07:51:56 -040047 return cachedCopy;
48 }
49 }
50
51 sk_sp<GrTextureProxy> proxy = this->originalProxyRef();
Robert Phillips0c984a02017-03-16 07:51:56 -040052
Brian Salomond6287472019-06-24 15:50:07 -040053 sk_sp<GrTextureProxy> copy = CopyOnGpu(this->context(), std::move(proxy), this->colorType(),
Robert Phillips9338c602019-02-19 12:52:29 -050054 copyParams, willBeMipped);
Robert Phillips0c984a02017-03-16 07:51:56 -040055 if (copy) {
56 if (key.isValid()) {
Robert Phillips78814092017-07-24 15:26:17 -040057 SkASSERT(copy->origin() == this->originalProxy()->origin());
Greg Daniel09c94002018-06-08 22:11:51 +000058 if (cachedCopy) {
59 SkASSERT(GrMipMapped::kYes == copy->mipMapped() &&
60 GrMipMapped::kNo == cachedCopy->mipMapped());
61 // If we had a cachedProxy, that means there already is a proxy in the cache which
62 // matches the key, but it does not have mip levels and we require them. Thus we
63 // must remove the unique key from that proxy.
Chris Dalton2de13dd2019-01-03 15:11:59 -070064 SkASSERT(cachedCopy->getUniqueKey() == key);
65 proxyProvider->removeUniqueKeyFromProxy(cachedCopy.get());
Greg Daniel09c94002018-06-08 22:11:51 +000066 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050067 proxyProvider->assignUniqueKeyToProxy(key, copy.get());
Robert Phillipsa41c6852019-02-07 10:44:10 -050068 this->didCacheCopy(key, proxyProvider->contextID());
Brian Osmane8e54582016-11-28 10:06:27 -050069 }
70 }
71 return copy;
72}
73
Brian Salomonccb61422020-01-09 10:46:36 -050074sk_sp<GrTextureProxy> GrTextureAdjuster::onRefTextureProxyForParams(GrSamplerState params,
75 bool willBeMipped,
76 SkScalar scaleAdjust[2]) {
Robert Phillips3798c862017-03-27 11:08:16 -040077 sk_sp<GrTextureProxy> proxy = this->originalProxyRef();
Brian Osmane8e54582016-11-28 10:06:27 -050078 CopyParams copyParams;
Brian Osmane8e54582016-11-28 10:06:27 -050079
Robert Phillips9338c602019-02-19 12:52:29 -050080 if (this->context()->priv().abandoned()) {
Brian Osmane8e54582016-11-28 10:06:27 -050081 // The texture was abandoned.
82 return nullptr;
83 }
Brian Salomon4df00922017-09-07 16:34:11 +000084
Robert Phillips9338c602019-02-19 12:52:29 -050085 SkASSERT(this->width() <= this->context()->priv().caps()->maxTextureSize() &&
86 this->height() <= this->context()->priv().caps()->maxTextureSize());
Brian Osman875f7852018-04-12 13:29:08 -040087
Greg Daniel8f5bbda2018-06-08 17:22:23 -040088 bool needsCopyForMipsOnly = false;
89 if (!params.isRepeated() ||
Robert Phillips9338c602019-02-19 12:52:29 -050090 !GrGpu::IsACopyNeededForRepeatWrapMode(this->context()->priv().caps(), proxy.get(),
Brian Salomon1a217eb2019-10-24 10:50:36 -040091 proxy->dimensions(), params.filter(), &copyParams,
92 scaleAdjust)) {
Robert Phillips9338c602019-02-19 12:52:29 -050093 needsCopyForMipsOnly = GrGpu::IsACopyNeededForMips(this->context()->priv().caps(),
Greg Daniel8f5bbda2018-06-08 17:22:23 -040094 proxy.get(), params.filter(),
95 &copyParams);
96 if (!needsCopyForMipsOnly) {
97 return proxy;
98 }
Brian Osmane8e54582016-11-28 10:06:27 -050099 }
100
Greg Daniel8f5bbda2018-06-08 17:22:23 -0400101 sk_sp<GrTextureProxy> result = this->refTextureProxyCopy(copyParams, willBeMipped);
102 if (!result && needsCopyForMipsOnly) {
103 // If we were unable to make a copy and we only needed a copy for mips, then we will return
104 // the source texture here and require that the GPU backend is able to fall back to using
105 // bilerp if mips are required.
106 return this->originalProxyRef();
107 }
108 return result;
Brian Osmane8e54582016-11-28 10:06:27 -0500109}
110
Brian Salomonaff329b2017-08-11 09:40:37 -0400111std::unique_ptr<GrFragmentProcessor> GrTextureAdjuster::createFragmentProcessor(
112 const SkMatrix& origTextureMatrix,
Greg Danielc77085d2017-11-01 16:38:48 -0400113 const SkRect& constraintRect,
Brian Salomonaff329b2017-08-11 09:40:37 -0400114 FilterConstraint filterConstraint,
115 bool coordsLimitedToConstraintRect,
Brian Osman05c8f462018-10-22 17:13:36 -0400116 const GrSamplerState::Filter* filterOrNullForBicubic) {
Brian Osmane8e54582016-11-28 10:06:27 -0500117 SkMatrix textureMatrix = origTextureMatrix;
Brian Osmane8e54582016-11-28 10:06:27 -0500118
Robert Phillips67c18d62017-01-20 12:44:06 -0500119 SkScalar scaleAdjust[2] = { 1.0f, 1.0f };
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400120 sk_sp<GrTextureProxy> proxy(
Michael Ludwigddeed372019-02-20 16:50:10 -0500121 this->refTextureProxyForParams(filterOrNullForBicubic, scaleAdjust));
Robert Phillips3798c862017-03-27 11:08:16 -0400122 if (!proxy) {
Brian Osmane8e54582016-11-28 10:06:27 -0500123 return nullptr;
124 }
125 // If we made a copy then we only copied the contentArea, in which case the new texture is all
126 // content.
Robert Phillips3798c862017-03-27 11:08:16 -0400127 if (proxy.get() != this->originalProxy()) {
Robert Phillips67c18d62017-01-20 12:44:06 -0500128 textureMatrix.postScale(scaleAdjust[0], scaleAdjust[1]);
Brian Osmane8e54582016-11-28 10:06:27 -0500129 }
130
Michael Ludwigddeed372019-02-20 16:50:10 -0500131 SkRect domain;
Brian Osmane8e54582016-11-28 10:06:27 -0500132 DomainMode domainMode =
Greg Danielc77085d2017-11-01 16:38:48 -0400133 DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect,
134 proxy.get(), filterOrNullForBicubic, &domain);
Brian Osmane8e54582016-11-28 10:06:27 -0500135 if (kTightCopy_DomainMode == domainMode) {
136 // TODO: Copy the texture and adjust the texture matrix (both parts need to consider
137 // non-int constraint rect)
138 // For now: treat as bilerp and ignore what goes on above level 0.
139
140 // We only expect MIP maps to require a tight copy.
141 SkASSERT(filterOrNullForBicubic &&
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400142 GrSamplerState::Filter::kMipMap == *filterOrNullForBicubic);
143 static const GrSamplerState::Filter kBilerp = GrSamplerState::Filter::kBilerp;
Brian Osmane8e54582016-11-28 10:06:27 -0500144 domainMode =
Greg Danielc77085d2017-11-01 16:38:48 -0400145 DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect,
146 proxy.get(), &kBilerp, &domain);
Brian Osmane8e54582016-11-28 10:06:27 -0500147 SkASSERT(kTightCopy_DomainMode != domainMode);
148 }
149 SkASSERT(kNoDomain_DomainMode == domainMode ||
150 (domain.fLeft <= domain.fRight && domain.fTop <= domain.fBottom));
Michael Ludwigddeed372019-02-20 16:50:10 -0500151 return this->createFragmentProcessorForDomainAndFilter(
152 std::move(proxy), textureMatrix, domainMode, domain, filterOrNullForBicubic);
Brian Osmane8e54582016-11-28 10:06:27 -0500153}