blob: 472797d5e01eef1934f8a32723d52e3259bb38e1 [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
27void GrTextureAdjuster::makeCopyKey(const CopyParams& params, GrUniqueKey* copyKey,
Brian Osman61624f02016-12-09 14:51:59 -050028 SkColorSpace* dstColorSpace) {
29 // Destination color space is irrelevant - we already have a texture so we're just sub-setting
Brian Osmane8e54582016-11-28 10:06:27 -050030 GrUniqueKey baseKey;
31 GrMakeKeyFromImageID(&baseKey, fUniqueID, SkIRect::MakeWH(this->width(), this->height()));
32 MakeCopyKeyFromOrigKey(baseKey, params, copyKey);
33}
34
Brian Salomon238069b2018-07-11 15:58:57 -040035void GrTextureAdjuster::didCacheCopy(const GrUniqueKey& copyKey, uint32_t contextUniqueID) {
Brian Osmane8e54582016-11-28 10:06:27 -050036 // We don't currently have a mechanism for notifications on Images!
37}
38
Greg Daniele1da1d92017-10-06 15:59:27 -040039sk_sp<GrTextureProxy> GrTextureAdjuster::refTextureProxyCopy(const CopyParams& copyParams,
40 bool willBeMipped) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -050041 GrProxyProvider* proxyProvider = fContext->contextPriv().proxyProvider();
42
Robert Phillips0c984a02017-03-16 07:51:56 -040043 GrUniqueKey key;
44 this->makeCopyKey(copyParams, &key, nullptr);
Greg Daniel09c94002018-06-08 22:11:51 +000045 sk_sp<GrTextureProxy> cachedCopy;
Robert Phillips0c984a02017-03-16 07:51:56 -040046 if (key.isValid()) {
Greg Daniel09c94002018-06-08 22:11:51 +000047 cachedCopy = proxyProvider->findOrCreateProxyByUniqueKey(key,
48 this->originalProxy()->origin());
49 if (cachedCopy && (!willBeMipped || GrMipMapped::kYes == cachedCopy->mipMapped())) {
Robert Phillips0c984a02017-03-16 07:51:56 -040050 return cachedCopy;
51 }
52 }
53
54 sk_sp<GrTextureProxy> proxy = this->originalProxyRef();
Robert Phillips0c984a02017-03-16 07:51:56 -040055
Greg Danielc77085d2017-11-01 16:38:48 -040056 sk_sp<GrTextureProxy> copy = CopyOnGpu(fContext, std::move(proxy), copyParams, willBeMipped);
Robert Phillips0c984a02017-03-16 07:51:56 -040057 if (copy) {
58 if (key.isValid()) {
Robert Phillips78814092017-07-24 15:26:17 -040059 SkASSERT(copy->origin() == this->originalProxy()->origin());
Greg Daniel09c94002018-06-08 22:11:51 +000060 if (cachedCopy) {
61 SkASSERT(GrMipMapped::kYes == copy->mipMapped() &&
62 GrMipMapped::kNo == cachedCopy->mipMapped());
63 // If we had a cachedProxy, that means there already is a proxy in the cache which
64 // matches the key, but it does not have mip levels and we require them. Thus we
65 // must remove the unique key from that proxy.
66 proxyProvider->removeUniqueKeyFromProxy(key, cachedCopy.get());
67 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050068 proxyProvider->assignUniqueKeyToProxy(key, copy.get());
Robert Phillipsdf65b832018-08-30 09:11:37 -040069 if (!proxyProvider->recordingDDL()) {
70 // If we're recording a DDL we cannot add genID change listeners because
71 // that process isn't thread safe
72 this->didCacheCopy(key, proxyProvider->contextUniqueID());
73 }
Brian Osmane8e54582016-11-28 10:06:27 -050074 }
75 }
76 return copy;
77}
78
Brian Salomon2a943df2018-05-04 13:43:19 -040079sk_sp<GrTextureProxy> GrTextureAdjuster::onRefTextureProxyForParams(
80 const GrSamplerState& params,
81 SkColorSpace* dstColorSpace,
82 sk_sp<SkColorSpace>* texColorSpace,
Greg Daniel8e9b4c42018-07-20 10:30:48 -040083 bool willBeMipped,
Brian Salomon2a943df2018-05-04 13:43:19 -040084 SkScalar scaleAdjust[2]) {
Robert Phillips3798c862017-03-27 11:08:16 -040085 sk_sp<GrTextureProxy> proxy = this->originalProxyRef();
Brian Osmane8e54582016-11-28 10:06:27 -050086 CopyParams copyParams;
Brian Osmane8e54582016-11-28 10:06:27 -050087
Robert Phillips0c984a02017-03-16 07:51:56 -040088 if (!fContext) {
Brian Osmane8e54582016-11-28 10:06:27 -050089 // The texture was abandoned.
90 return nullptr;
91 }
Brian Salomon4df00922017-09-07 16:34:11 +000092
Brian Salomon2a943df2018-05-04 13:43:19 -040093 if (texColorSpace) {
94 *texColorSpace = sk_ref_sp(fColorSpace);
95 }
Brian Salomonc7fe0f72018-05-11 10:14:21 -040096 SkASSERT(this->width() <= fContext->contextPriv().caps()->maxTextureSize() &&
97 this->height() <= fContext->contextPriv().caps()->maxTextureSize());
Brian Osman875f7852018-04-12 13:29:08 -040098
Greg Daniel8f5bbda2018-06-08 17:22:23 -040099 bool needsCopyForMipsOnly = false;
100 if (!params.isRepeated() ||
101 !GrGpu::IsACopyNeededForRepeatWrapMode(fContext->contextPriv().caps(), proxy.get(),
102 proxy->width(), proxy->height(), params.filter(),
103 &copyParams, scaleAdjust)) {
104 needsCopyForMipsOnly = GrGpu::IsACopyNeededForMips(fContext->contextPriv().caps(),
105 proxy.get(), params.filter(),
106 &copyParams);
107 if (!needsCopyForMipsOnly) {
108 return proxy;
109 }
Brian Osmane8e54582016-11-28 10:06:27 -0500110 }
111
Greg Daniel8f5bbda2018-06-08 17:22:23 -0400112 sk_sp<GrTextureProxy> result = this->refTextureProxyCopy(copyParams, willBeMipped);
113 if (!result && needsCopyForMipsOnly) {
114 // If we were unable to make a copy and we only needed a copy for mips, then we will return
115 // the source texture here and require that the GPU backend is able to fall back to using
116 // bilerp if mips are required.
117 return this->originalProxyRef();
118 }
119 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 Salomon2bbdcc42017-09-07 12:36:34 -0400127 const GrSamplerState::Filter* filterOrNullForBicubic,
Brian Salomonaff329b2017-08-11 09:40:37 -0400128 SkColorSpace* dstColorSpace) {
Brian Osmane8e54582016-11-28 10:06:27 -0500129 SkMatrix textureMatrix = origTextureMatrix;
Brian Osmane8e54582016-11-28 10:06:27 -0500130
131 SkRect domain;
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400132 GrSamplerState samplerState;
Brian Osmane8e54582016-11-28 10:06:27 -0500133 if (filterOrNullForBicubic) {
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400134 samplerState.setFilterMode(*filterOrNullForBicubic);
Brian Osmane8e54582016-11-28 10:06:27 -0500135 }
Robert Phillips67c18d62017-01-20 12:44:06 -0500136 SkScalar scaleAdjust[2] = { 1.0f, 1.0f };
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400137 sk_sp<GrTextureProxy> proxy(
Brian Salomon2a943df2018-05-04 13:43:19 -0400138 this->refTextureProxyForParams(samplerState, nullptr, nullptr, scaleAdjust));
Robert Phillips3798c862017-03-27 11:08:16 -0400139 if (!proxy) {
Brian Osmane8e54582016-11-28 10:06:27 -0500140 return nullptr;
141 }
142 // If we made a copy then we only copied the contentArea, in which case the new texture is all
143 // content.
Robert Phillips3798c862017-03-27 11:08:16 -0400144 if (proxy.get() != this->originalProxy()) {
Robert Phillips67c18d62017-01-20 12:44:06 -0500145 textureMatrix.postScale(scaleAdjust[0], scaleAdjust[1]);
Brian Osmane8e54582016-11-28 10:06:27 -0500146 }
147
148 DomainMode domainMode =
Greg Danielc77085d2017-11-01 16:38:48 -0400149 DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect,
150 proxy.get(), filterOrNullForBicubic, &domain);
Brian Osmane8e54582016-11-28 10:06:27 -0500151 if (kTightCopy_DomainMode == domainMode) {
152 // TODO: Copy the texture and adjust the texture matrix (both parts need to consider
153 // non-int constraint rect)
154 // For now: treat as bilerp and ignore what goes on above level 0.
155
156 // We only expect MIP maps to require a tight copy.
157 SkASSERT(filterOrNullForBicubic &&
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400158 GrSamplerState::Filter::kMipMap == *filterOrNullForBicubic);
159 static const GrSamplerState::Filter kBilerp = GrSamplerState::Filter::kBilerp;
Brian Osmane8e54582016-11-28 10:06:27 -0500160 domainMode =
Greg Danielc77085d2017-11-01 16:38:48 -0400161 DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect,
162 proxy.get(), &kBilerp, &domain);
Brian Osmane8e54582016-11-28 10:06:27 -0500163 SkASSERT(kTightCopy_DomainMode != domainMode);
164 }
165 SkASSERT(kNoDomain_DomainMode == domainMode ||
166 (domain.fLeft <= domain.fRight && domain.fTop <= domain.fBottom));
Brian Osman5e341672017-10-18 10:23:18 -0400167 auto fp = CreateFragmentProcessorForDomainAndFilter(std::move(proxy), textureMatrix,
168 domainMode, domain, filterOrNullForBicubic);
Brian Osman21fc5ce2018-08-27 20:36:19 +0000169 return GrColorSpaceXformEffect::Make(std::move(fp), fColorSpace, fAlphaType, dstColorSpace);
Brian Osmane8e54582016-11-28 10:06:27 -0500170}