Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #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 Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 15 | |
Brian Salomon | e7499c7 | 2019-06-24 12:12:36 -0400 | [diff] [blame] | 16 | GrTextureAdjuster::GrTextureAdjuster(GrRecordingContext* context, |
| 17 | sk_sp<GrTextureProxy> original, |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 18 | GrColorType colorType, |
Brian Salomon | 4df0092 | 2017-09-07 16:34:11 +0000 | [diff] [blame] | 19 | SkAlphaType alphaType, |
Greg Daniel | c77085d | 2017-11-01 16:38:48 -0400 | [diff] [blame] | 20 | uint32_t uniqueID, |
Michael Ludwig | ddeed37 | 2019-02-20 16:50:10 -0500 | [diff] [blame] | 21 | SkColorSpace* cs, |
| 22 | bool useDecal) |
Brian Salomon | e7499c7 | 2019-06-24 12:12:36 -0400 | [diff] [blame] | 23 | : INHERITED(context, original->width(), original->height(), |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 24 | GrColorSpaceInfo(colorType, |
| 25 | alphaType, |
| 26 | sk_ref_sp(cs), |
| 27 | original->config()), |
| 28 | useDecal) |
Brian Salomon | e7499c7 | 2019-06-24 12:12:36 -0400 | [diff] [blame] | 29 | , fOriginal(std::move(original)) |
| 30 | , fUniqueID(uniqueID) {} |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 31 | |
Brian Osman | b3f3830 | 2018-09-07 15:24:44 -0400 | [diff] [blame] | 32 | void GrTextureAdjuster::makeCopyKey(const CopyParams& params, GrUniqueKey* copyKey) { |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 33 | // Destination color space is irrelevant - we already have a texture so we're just sub-setting |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 34 | GrUniqueKey baseKey; |
| 35 | GrMakeKeyFromImageID(&baseKey, fUniqueID, SkIRect::MakeWH(this->width(), this->height())); |
| 36 | MakeCopyKeyFromOrigKey(baseKey, params, copyKey); |
| 37 | } |
| 38 | |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame] | 39 | void GrTextureAdjuster::didCacheCopy(const GrUniqueKey& copyKey, uint32_t contextUniqueID) { |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 40 | // We don't currently have a mechanism for notifications on Images! |
| 41 | } |
| 42 | |
Greg Daniel | e1da1d9 | 2017-10-06 15:59:27 -0400 | [diff] [blame] | 43 | sk_sp<GrTextureProxy> GrTextureAdjuster::refTextureProxyCopy(const CopyParams& copyParams, |
| 44 | bool willBeMipped) { |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 45 | GrProxyProvider* proxyProvider = this->context()->priv().proxyProvider(); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 46 | |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 47 | GrUniqueKey key; |
Brian Osman | b3f3830 | 2018-09-07 15:24:44 -0400 | [diff] [blame] | 48 | this->makeCopyKey(copyParams, &key); |
Greg Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 49 | sk_sp<GrTextureProxy> cachedCopy; |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 50 | if (key.isValid()) { |
Greg Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 51 | cachedCopy = proxyProvider->findOrCreateProxyByUniqueKey(key, |
| 52 | this->originalProxy()->origin()); |
| 53 | if (cachedCopy && (!willBeMipped || GrMipMapped::kYes == cachedCopy->mipMapped())) { |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 54 | return cachedCopy; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | sk_sp<GrTextureProxy> proxy = this->originalProxyRef(); |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 59 | |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 60 | sk_sp<GrTextureProxy> copy = CopyOnGpu(this->context(), std::move(proxy), this->colorType(), |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 61 | copyParams, willBeMipped); |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 62 | if (copy) { |
| 63 | if (key.isValid()) { |
Robert Phillips | 7881409 | 2017-07-24 15:26:17 -0400 | [diff] [blame] | 64 | SkASSERT(copy->origin() == this->originalProxy()->origin()); |
Greg Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 65 | 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 Dalton | 2de13dd | 2019-01-03 15:11:59 -0700 | [diff] [blame] | 71 | SkASSERT(cachedCopy->getUniqueKey() == key); |
| 72 | proxyProvider->removeUniqueKeyFromProxy(cachedCopy.get()); |
Greg Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 73 | } |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 74 | proxyProvider->assignUniqueKeyToProxy(key, copy.get()); |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 75 | this->didCacheCopy(key, proxyProvider->contextID()); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | return copy; |
| 79 | } |
| 80 | |
Brian Salomon | 2a943df | 2018-05-04 13:43:19 -0400 | [diff] [blame] | 81 | sk_sp<GrTextureProxy> GrTextureAdjuster::onRefTextureProxyForParams( |
| 82 | const GrSamplerState& params, |
Greg Daniel | 8e9b4c4 | 2018-07-20 10:30:48 -0400 | [diff] [blame] | 83 | bool willBeMipped, |
Brian Salomon | 2a943df | 2018-05-04 13:43:19 -0400 | [diff] [blame] | 84 | SkScalar scaleAdjust[2]) { |
Robert Phillips | 3798c86 | 2017-03-27 11:08:16 -0400 | [diff] [blame] | 85 | sk_sp<GrTextureProxy> proxy = this->originalProxyRef(); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 86 | CopyParams copyParams; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 87 | |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 88 | if (this->context()->priv().abandoned()) { |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 89 | // The texture was abandoned. |
| 90 | return nullptr; |
| 91 | } |
Brian Salomon | 4df0092 | 2017-09-07 16:34:11 +0000 | [diff] [blame] | 92 | |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 93 | SkASSERT(this->width() <= this->context()->priv().caps()->maxTextureSize() && |
| 94 | this->height() <= this->context()->priv().caps()->maxTextureSize()); |
Brian Osman | 875f785 | 2018-04-12 13:29:08 -0400 | [diff] [blame] | 95 | |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 96 | bool needsCopyForMipsOnly = false; |
| 97 | if (!params.isRepeated() || |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 98 | !GrGpu::IsACopyNeededForRepeatWrapMode(this->context()->priv().caps(), proxy.get(), |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 99 | proxy->width(), proxy->height(), params.filter(), |
| 100 | ©Params, scaleAdjust)) { |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 101 | needsCopyForMipsOnly = GrGpu::IsACopyNeededForMips(this->context()->priv().caps(), |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 102 | proxy.get(), params.filter(), |
| 103 | ©Params); |
| 104 | if (!needsCopyForMipsOnly) { |
| 105 | return proxy; |
| 106 | } |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 107 | } |
| 108 | |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 109 | sk_sp<GrTextureProxy> result = this->refTextureProxyCopy(copyParams, willBeMipped); |
| 110 | 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 Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 117 | } |
| 118 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 119 | std::unique_ptr<GrFragmentProcessor> GrTextureAdjuster::createFragmentProcessor( |
| 120 | const SkMatrix& origTextureMatrix, |
Greg Daniel | c77085d | 2017-11-01 16:38:48 -0400 | [diff] [blame] | 121 | const SkRect& constraintRect, |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 122 | FilterConstraint filterConstraint, |
| 123 | bool coordsLimitedToConstraintRect, |
Brian Osman | 05c8f46 | 2018-10-22 17:13:36 -0400 | [diff] [blame] | 124 | const GrSamplerState::Filter* filterOrNullForBicubic) { |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 125 | SkMatrix textureMatrix = origTextureMatrix; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 126 | |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 127 | SkScalar scaleAdjust[2] = { 1.0f, 1.0f }; |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 128 | sk_sp<GrTextureProxy> proxy( |
Michael Ludwig | ddeed37 | 2019-02-20 16:50:10 -0500 | [diff] [blame] | 129 | this->refTextureProxyForParams(filterOrNullForBicubic, scaleAdjust)); |
Robert Phillips | 3798c86 | 2017-03-27 11:08:16 -0400 | [diff] [blame] | 130 | if (!proxy) { |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 131 | 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 Phillips | 3798c86 | 2017-03-27 11:08:16 -0400 | [diff] [blame] | 135 | if (proxy.get() != this->originalProxy()) { |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 136 | textureMatrix.postScale(scaleAdjust[0], scaleAdjust[1]); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 137 | } |
| 138 | |
Michael Ludwig | ddeed37 | 2019-02-20 16:50:10 -0500 | [diff] [blame] | 139 | SkRect domain; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 140 | DomainMode domainMode = |
Greg Daniel | c77085d | 2017-11-01 16:38:48 -0400 | [diff] [blame] | 141 | DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect, |
| 142 | proxy.get(), filterOrNullForBicubic, &domain); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 143 | 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 Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 150 | GrSamplerState::Filter::kMipMap == *filterOrNullForBicubic); |
| 151 | static const GrSamplerState::Filter kBilerp = GrSamplerState::Filter::kBilerp; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 152 | domainMode = |
Greg Daniel | c77085d | 2017-11-01 16:38:48 -0400 | [diff] [blame] | 153 | DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect, |
| 154 | proxy.get(), &kBilerp, &domain); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 155 | SkASSERT(kTightCopy_DomainMode != domainMode); |
| 156 | } |
| 157 | SkASSERT(kNoDomain_DomainMode == domainMode || |
| 158 | (domain.fLeft <= domain.fRight && domain.fTop <= domain.fBottom)); |
Michael Ludwig | ddeed37 | 2019-02-20 16:50:10 -0500 | [diff] [blame] | 159 | return this->createFragmentProcessorForDomainAndFilter( |
| 160 | std::move(proxy), textureMatrix, domainMode, domain, filterOrNullForBicubic); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 161 | } |