blob: 05f839690bd318ab1081ed4b67ba1d9732a30238 [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,
Brian Salomond6287472019-06-24 15:50:07 -040018 GrColorType colorType,
Brian Salomon4df00922017-09-07 16:34:11 +000019 SkAlphaType alphaType,
Greg Danielc77085d2017-11-01 16:38:48 -040020 uint32_t uniqueID,
Michael Ludwigddeed372019-02-20 16:50:10 -050021 SkColorSpace* cs,
22 bool useDecal)
Brian Salomone7499c72019-06-24 12:12:36 -040023 : INHERITED(context, original->width(), original->height(),
Brian Salomonbd3d8d32019-07-02 09:16:28 -040024 GrColorSpaceInfo(colorType, alphaType, sk_ref_sp(cs)), useDecal)
Brian Salomone7499c72019-06-24 12:12:36 -040025 , fOriginal(std::move(original))
26 , fUniqueID(uniqueID) {}
Brian Osmane8e54582016-11-28 10:06:27 -050027
Brian Osmanb3f38302018-09-07 15:24:44 -040028void GrTextureAdjuster::makeCopyKey(const CopyParams& params, GrUniqueKey* copyKey) {
Brian Osman61624f02016-12-09 14:51:59 -050029 // 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 Phillips9338c602019-02-19 12:52:29 -050041 GrProxyProvider* proxyProvider = this->context()->priv().proxyProvider();
Robert Phillips1afd4cd2018-01-08 13:40:32 -050042
Robert Phillips0c984a02017-03-16 07:51:56 -040043 GrUniqueKey key;
Brian Osmanb3f38302018-09-07 15:24:44 -040044 this->makeCopyKey(copyParams, &key);
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
Brian Salomond6287472019-06-24 15:50:07 -040056 sk_sp<GrTextureProxy> copy = CopyOnGpu(this->context(), std::move(proxy), this->colorType(),
Robert Phillips9338c602019-02-19 12:52:29 -050057 copyParams, willBeMipped);
Robert Phillips0c984a02017-03-16 07:51:56 -040058 if (copy) {
59 if (key.isValid()) {
Robert Phillips78814092017-07-24 15:26:17 -040060 SkASSERT(copy->origin() == this->originalProxy()->origin());
Greg Daniel09c94002018-06-08 22:11:51 +000061 if (cachedCopy) {
62 SkASSERT(GrMipMapped::kYes == copy->mipMapped() &&
63 GrMipMapped::kNo == cachedCopy->mipMapped());
64 // If we had a cachedProxy, that means there already is a proxy in the cache which
65 // matches the key, but it does not have mip levels and we require them. Thus we
66 // must remove the unique key from that proxy.
Chris Dalton2de13dd2019-01-03 15:11:59 -070067 SkASSERT(cachedCopy->getUniqueKey() == key);
68 proxyProvider->removeUniqueKeyFromProxy(cachedCopy.get());
Greg Daniel09c94002018-06-08 22:11:51 +000069 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050070 proxyProvider->assignUniqueKeyToProxy(key, copy.get());
Robert Phillipsa41c6852019-02-07 10:44:10 -050071 this->didCacheCopy(key, proxyProvider->contextID());
Brian Osmane8e54582016-11-28 10:06:27 -050072 }
73 }
74 return copy;
75}
76
Brian Salomon2a943df2018-05-04 13:43:19 -040077sk_sp<GrTextureProxy> GrTextureAdjuster::onRefTextureProxyForParams(
78 const GrSamplerState& params,
Greg Daniel8e9b4c42018-07-20 10:30:48 -040079 bool willBeMipped,
Brian Salomon2a943df2018-05-04 13:43:19 -040080 SkScalar scaleAdjust[2]) {
Robert Phillips3798c862017-03-27 11:08:16 -040081 sk_sp<GrTextureProxy> proxy = this->originalProxyRef();
Brian Osmane8e54582016-11-28 10:06:27 -050082 CopyParams copyParams;
Brian Osmane8e54582016-11-28 10:06:27 -050083
Robert Phillips9338c602019-02-19 12:52:29 -050084 if (this->context()->priv().abandoned()) {
Brian Osmane8e54582016-11-28 10:06:27 -050085 // The texture was abandoned.
86 return nullptr;
87 }
Brian Salomon4df00922017-09-07 16:34:11 +000088
Robert Phillips9338c602019-02-19 12:52:29 -050089 SkASSERT(this->width() <= this->context()->priv().caps()->maxTextureSize() &&
90 this->height() <= this->context()->priv().caps()->maxTextureSize());
Brian Osman875f7852018-04-12 13:29:08 -040091
Greg Daniel8f5bbda2018-06-08 17:22:23 -040092 bool needsCopyForMipsOnly = false;
93 if (!params.isRepeated() ||
Robert Phillips9338c602019-02-19 12:52:29 -050094 !GrGpu::IsACopyNeededForRepeatWrapMode(this->context()->priv().caps(), proxy.get(),
Greg Daniel8f5bbda2018-06-08 17:22:23 -040095 proxy->width(), proxy->height(), params.filter(),
96 &copyParams, scaleAdjust)) {
Robert Phillips9338c602019-02-19 12:52:29 -050097 needsCopyForMipsOnly = GrGpu::IsACopyNeededForMips(this->context()->priv().caps(),
Greg Daniel8f5bbda2018-06-08 17:22:23 -040098 proxy.get(), params.filter(),
99 &copyParams);
100 if (!needsCopyForMipsOnly) {
101 return proxy;
102 }
Brian Osmane8e54582016-11-28 10:06:27 -0500103 }
104
Greg Daniel8f5bbda2018-06-08 17:22:23 -0400105 sk_sp<GrTextureProxy> result = this->refTextureProxyCopy(copyParams, willBeMipped);
106 if (!result && needsCopyForMipsOnly) {
107 // If we were unable to make a copy and we only needed a copy for mips, then we will return
108 // the source texture here and require that the GPU backend is able to fall back to using
109 // bilerp if mips are required.
110 return this->originalProxyRef();
111 }
112 return result;
Brian Osmane8e54582016-11-28 10:06:27 -0500113}
114
Brian Salomonaff329b2017-08-11 09:40:37 -0400115std::unique_ptr<GrFragmentProcessor> GrTextureAdjuster::createFragmentProcessor(
116 const SkMatrix& origTextureMatrix,
Greg Danielc77085d2017-11-01 16:38:48 -0400117 const SkRect& constraintRect,
Brian Salomonaff329b2017-08-11 09:40:37 -0400118 FilterConstraint filterConstraint,
119 bool coordsLimitedToConstraintRect,
Brian Osman05c8f462018-10-22 17:13:36 -0400120 const GrSamplerState::Filter* filterOrNullForBicubic) {
Brian Osmane8e54582016-11-28 10:06:27 -0500121 SkMatrix textureMatrix = origTextureMatrix;
Brian Osmane8e54582016-11-28 10:06:27 -0500122
Robert Phillips67c18d62017-01-20 12:44:06 -0500123 SkScalar scaleAdjust[2] = { 1.0f, 1.0f };
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400124 sk_sp<GrTextureProxy> proxy(
Michael Ludwigddeed372019-02-20 16:50:10 -0500125 this->refTextureProxyForParams(filterOrNullForBicubic, scaleAdjust));
Robert Phillips3798c862017-03-27 11:08:16 -0400126 if (!proxy) {
Brian Osmane8e54582016-11-28 10:06:27 -0500127 return nullptr;
128 }
129 // If we made a copy then we only copied the contentArea, in which case the new texture is all
130 // content.
Robert Phillips3798c862017-03-27 11:08:16 -0400131 if (proxy.get() != this->originalProxy()) {
Robert Phillips67c18d62017-01-20 12:44:06 -0500132 textureMatrix.postScale(scaleAdjust[0], scaleAdjust[1]);
Brian Osmane8e54582016-11-28 10:06:27 -0500133 }
134
Michael Ludwigddeed372019-02-20 16:50:10 -0500135 SkRect domain;
Brian Osmane8e54582016-11-28 10:06:27 -0500136 DomainMode domainMode =
Greg Danielc77085d2017-11-01 16:38:48 -0400137 DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect,
138 proxy.get(), filterOrNullForBicubic, &domain);
Brian Osmane8e54582016-11-28 10:06:27 -0500139 if (kTightCopy_DomainMode == domainMode) {
140 // TODO: Copy the texture and adjust the texture matrix (both parts need to consider
141 // non-int constraint rect)
142 // For now: treat as bilerp and ignore what goes on above level 0.
143
144 // We only expect MIP maps to require a tight copy.
145 SkASSERT(filterOrNullForBicubic &&
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400146 GrSamplerState::Filter::kMipMap == *filterOrNullForBicubic);
147 static const GrSamplerState::Filter kBilerp = GrSamplerState::Filter::kBilerp;
Brian Osmane8e54582016-11-28 10:06:27 -0500148 domainMode =
Greg Danielc77085d2017-11-01 16:38:48 -0400149 DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect,
150 proxy.get(), &kBilerp, &domain);
Brian Osmane8e54582016-11-28 10:06:27 -0500151 SkASSERT(kTightCopy_DomainMode != domainMode);
152 }
153 SkASSERT(kNoDomain_DomainMode == domainMode ||
154 (domain.fLeft <= domain.fRight && domain.fTop <= domain.fBottom));
Michael Ludwigddeed372019-02-20 16:50:10 -0500155 return this->createFragmentProcessorForDomainAndFilter(
156 std::move(proxy), textureMatrix, domainMode, domain, filterOrNullForBicubic);
Brian Osmane8e54582016-11-28 10:06:27 -0500157}