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