blob: 2b8f44cee97228f5f6c396e0fdca11bc7b89f42d [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
8#include "GrTextureAdjuster.h"
Brian Osman1cb41712017-10-19 12:54:52 -04009#include "GrColorSpaceXform.h"
Brian Osmane8e54582016-11-28 10:06:27 -050010#include "GrContext.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050011#include "GrContextPriv.h"
Robert Phillips009e9af2017-06-15 14:01:04 -040012#include "GrGpu.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050013#include "GrProxyProvider.h"
Brian Osman3b655982017-03-07 16:58:08 -050014#include "SkGr.h"
Brian Osmane8e54582016-11-28 10:06:27 -050015
Robert Phillips3798c862017-03-27 11:08:16 -040016GrTextureAdjuster::GrTextureAdjuster(GrContext* context, sk_sp<GrTextureProxy> original,
Brian Salomon4df00922017-09-07 16:34:11 +000017 SkAlphaType alphaType,
Greg Danielc77085d2017-11-01 16:38:48 -040018 uint32_t uniqueID,
Brian Salomon4df00922017-09-07 16:34:11 +000019 SkColorSpace* cs)
Greg Daniel8e9b4c42018-07-20 10:30:48 -040020 : INHERITED(context, original->width(), original->height(),
Brian Salomon4df00922017-09-07 16:34:11 +000021 GrPixelConfigIsAlphaOnly(original->config()))
Brian Salomon4df00922017-09-07 16:34:11 +000022 , fOriginal(std::move(original))
23 , fAlphaType(alphaType)
24 , fColorSpace(cs)
Greg Danielc77085d2017-11-01 16:38:48 -040025 , fUniqueID(uniqueID) {}
Brian Osmane8e54582016-11-28 10:06:27 -050026
Brian Osmanb3f38302018-09-07 15:24:44 -040027void GrTextureAdjuster::makeCopyKey(const CopyParams& params, GrUniqueKey* copyKey) {
Brian Osman61624f02016-12-09 14:51:59 -050028 // Destination color space is irrelevant - we already have a texture so we're just sub-setting
Brian Osmane8e54582016-11-28 10:06:27 -050029 GrUniqueKey baseKey;
30 GrMakeKeyFromImageID(&baseKey, fUniqueID, SkIRect::MakeWH(this->width(), this->height()));
31 MakeCopyKeyFromOrigKey(baseKey, params, copyKey);
32}
33
Brian Salomon238069b2018-07-11 15:58:57 -040034void GrTextureAdjuster::didCacheCopy(const GrUniqueKey& copyKey, uint32_t contextUniqueID) {
Brian Osmane8e54582016-11-28 10:06:27 -050035 // We don't currently have a mechanism for notifications on Images!
36}
37
Greg Daniele1da1d92017-10-06 15:59:27 -040038sk_sp<GrTextureProxy> GrTextureAdjuster::refTextureProxyCopy(const CopyParams& copyParams,
39 bool willBeMipped) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050040 GrProxyProvider* proxyProvider = fContext->contextPriv().proxyProvider();
41
Robert Phillips0c984a02017-03-16 07:51:56 -040042 GrUniqueKey key;
Brian Osmanb3f38302018-09-07 15:24:44 -040043 this->makeCopyKey(copyParams, &key);
Greg Daniel09c94002018-06-08 22:11:51 +000044 sk_sp<GrTextureProxy> cachedCopy;
Robert Phillips0c984a02017-03-16 07:51:56 -040045 if (key.isValid()) {
Greg Daniel09c94002018-06-08 22:11:51 +000046 cachedCopy = proxyProvider->findOrCreateProxyByUniqueKey(key,
47 this->originalProxy()->origin());
48 if (cachedCopy && (!willBeMipped || GrMipMapped::kYes == cachedCopy->mipMapped())) {
Robert Phillips0c984a02017-03-16 07:51:56 -040049 return cachedCopy;
50 }
51 }
52
53 sk_sp<GrTextureProxy> proxy = this->originalProxyRef();
Robert Phillips0c984a02017-03-16 07:51:56 -040054
Greg Danielc77085d2017-11-01 16:38:48 -040055 sk_sp<GrTextureProxy> copy = CopyOnGpu(fContext, std::move(proxy), copyParams, willBeMipped);
Robert Phillips0c984a02017-03-16 07:51:56 -040056 if (copy) {
57 if (key.isValid()) {
Robert Phillips78814092017-07-24 15:26:17 -040058 SkASSERT(copy->origin() == this->originalProxy()->origin());
Greg Daniel09c94002018-06-08 22:11:51 +000059 if (cachedCopy) {
60 SkASSERT(GrMipMapped::kYes == copy->mipMapped() &&
61 GrMipMapped::kNo == cachedCopy->mipMapped());
62 // If we had a cachedProxy, that means there already is a proxy in the cache which
63 // matches the key, but it does not have mip levels and we require them. Thus we
64 // must remove the unique key from that proxy.
Chris Dalton2de13dd2019-01-03 15:11:59 -070065 SkASSERT(cachedCopy->getUniqueKey() == key);
66 proxyProvider->removeUniqueKeyFromProxy(cachedCopy.get());
Greg Daniel09c94002018-06-08 22:11:51 +000067 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050068 proxyProvider->assignUniqueKeyToProxy(key, copy.get());
Robert Phillips869fe562018-09-17 14:55:49 -040069 this->didCacheCopy(key, proxyProvider->contextUniqueID());
Brian Osmane8e54582016-11-28 10:06:27 -050070 }
71 }
72 return copy;
73}
74
Brian Salomon2a943df2018-05-04 13:43:19 -040075sk_sp<GrTextureProxy> GrTextureAdjuster::onRefTextureProxyForParams(
76 const GrSamplerState& params,
Greg Daniel8e9b4c42018-07-20 10:30:48 -040077 bool willBeMipped,
Brian Salomon2a943df2018-05-04 13:43:19 -040078 SkScalar scaleAdjust[2]) {
Robert Phillips3798c862017-03-27 11:08:16 -040079 sk_sp<GrTextureProxy> proxy = this->originalProxyRef();
Brian Osmane8e54582016-11-28 10:06:27 -050080 CopyParams copyParams;
Brian Osmane8e54582016-11-28 10:06:27 -050081
Robert Phillips0c984a02017-03-16 07:51:56 -040082 if (!fContext) {
Brian Osmane8e54582016-11-28 10:06:27 -050083 // The texture was abandoned.
84 return nullptr;
85 }
Brian Salomon4df00922017-09-07 16:34:11 +000086
Brian Salomonc7fe0f72018-05-11 10:14:21 -040087 SkASSERT(this->width() <= fContext->contextPriv().caps()->maxTextureSize() &&
88 this->height() <= fContext->contextPriv().caps()->maxTextureSize());
Brian Osman875f7852018-04-12 13:29:08 -040089
Greg Daniel8f5bbda2018-06-08 17:22:23 -040090 bool needsCopyForMipsOnly = false;
91 if (!params.isRepeated() ||
92 !GrGpu::IsACopyNeededForRepeatWrapMode(fContext->contextPriv().caps(), proxy.get(),
93 proxy->width(), proxy->height(), params.filter(),
94 &copyParams, scaleAdjust)) {
95 needsCopyForMipsOnly = GrGpu::IsACopyNeededForMips(fContext->contextPriv().caps(),
96 proxy.get(), params.filter(),
97 &copyParams);
98 if (!needsCopyForMipsOnly) {
99 return proxy;
100 }
Brian Osmane8e54582016-11-28 10:06:27 -0500101 }
102
Greg Daniel8f5bbda2018-06-08 17:22:23 -0400103 sk_sp<GrTextureProxy> result = this->refTextureProxyCopy(copyParams, willBeMipped);
104 if (!result && needsCopyForMipsOnly) {
105 // If we were unable to make a copy and we only needed a copy for mips, then we will return
106 // the source texture here and require that the GPU backend is able to fall back to using
107 // bilerp if mips are required.
108 return this->originalProxyRef();
109 }
110 return result;
Brian Osmane8e54582016-11-28 10:06:27 -0500111}
112
Brian Salomonaff329b2017-08-11 09:40:37 -0400113std::unique_ptr<GrFragmentProcessor> GrTextureAdjuster::createFragmentProcessor(
114 const SkMatrix& origTextureMatrix,
Greg Danielc77085d2017-11-01 16:38:48 -0400115 const SkRect& constraintRect,
Brian Salomonaff329b2017-08-11 09:40:37 -0400116 FilterConstraint filterConstraint,
117 bool coordsLimitedToConstraintRect,
Brian Osman05c8f462018-10-22 17:13:36 -0400118 const GrSamplerState::Filter* filterOrNullForBicubic) {
Brian Osmane8e54582016-11-28 10:06:27 -0500119 SkMatrix textureMatrix = origTextureMatrix;
Brian Osmane8e54582016-11-28 10:06:27 -0500120
121 SkRect domain;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400122 GrSamplerState samplerState;
Brian Osmane8e54582016-11-28 10:06:27 -0500123 if (filterOrNullForBicubic) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400124 samplerState.setFilterMode(*filterOrNullForBicubic);
Brian Osmane8e54582016-11-28 10:06:27 -0500125 }
Robert Phillips67c18d62017-01-20 12:44:06 -0500126 SkScalar scaleAdjust[2] = { 1.0f, 1.0f };
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400127 sk_sp<GrTextureProxy> proxy(
Brian Osman6064e1c2018-10-19 14:27:54 -0400128 this->refTextureProxyForParams(samplerState, scaleAdjust));
Robert Phillips3798c862017-03-27 11:08:16 -0400129 if (!proxy) {
Brian Osmane8e54582016-11-28 10:06:27 -0500130 return nullptr;
131 }
132 // If we made a copy then we only copied the contentArea, in which case the new texture is all
133 // content.
Robert Phillips3798c862017-03-27 11:08:16 -0400134 if (proxy.get() != this->originalProxy()) {
Robert Phillips67c18d62017-01-20 12:44:06 -0500135 textureMatrix.postScale(scaleAdjust[0], scaleAdjust[1]);
Brian Osmane8e54582016-11-28 10:06:27 -0500136 }
137
138 DomainMode domainMode =
Greg Danielc77085d2017-11-01 16:38:48 -0400139 DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect,
140 proxy.get(), filterOrNullForBicubic, &domain);
Brian Osmane8e54582016-11-28 10:06:27 -0500141 if (kTightCopy_DomainMode == domainMode) {
142 // TODO: Copy the texture and adjust the texture matrix (both parts need to consider
143 // non-int constraint rect)
144 // For now: treat as bilerp and ignore what goes on above level 0.
145
146 // We only expect MIP maps to require a tight copy.
147 SkASSERT(filterOrNullForBicubic &&
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400148 GrSamplerState::Filter::kMipMap == *filterOrNullForBicubic);
149 static const GrSamplerState::Filter kBilerp = GrSamplerState::Filter::kBilerp;
Brian Osmane8e54582016-11-28 10:06:27 -0500150 domainMode =
Greg Danielc77085d2017-11-01 16:38:48 -0400151 DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect,
152 proxy.get(), &kBilerp, &domain);
Brian Osmane8e54582016-11-28 10:06:27 -0500153 SkASSERT(kTightCopy_DomainMode != domainMode);
154 }
155 SkASSERT(kNoDomain_DomainMode == domainMode ||
156 (domain.fLeft <= domain.fRight && domain.fTop <= domain.fBottom));
Brian Osman05c8f462018-10-22 17:13:36 -0400157 return CreateFragmentProcessorForDomainAndFilter(std::move(proxy), textureMatrix, domainMode,
158 domain, filterOrNullForBicubic);
Brian Osmane8e54582016-11-28 10:06:27 -0500159}