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 | |
| 8 | #include "GrTextureAdjuster.h" |
Brian Osman | 1cb4171 | 2017-10-19 12:54:52 -0400 | [diff] [blame] | 9 | #include "GrColorSpaceXform.h" |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 10 | #include "GrContext.h" |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 11 | #include "GrContextPriv.h" |
Robert Phillips | 009e9af | 2017-06-15 14:01:04 -0400 | [diff] [blame] | 12 | #include "GrGpu.h" |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 13 | #include "GrProxyProvider.h" |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 14 | #include "SkGr.h" |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 15 | |
Robert Phillips | 3798c86 | 2017-03-27 11:08:16 -0400 | [diff] [blame] | 16 | GrTextureAdjuster::GrTextureAdjuster(GrContext* context, sk_sp<GrTextureProxy> original, |
Brian Salomon | 4df0092 | 2017-09-07 16:34:11 +0000 | [diff] [blame] | 17 | SkAlphaType alphaType, |
Greg Daniel | c77085d | 2017-11-01 16:38:48 -0400 | [diff] [blame] | 18 | uint32_t uniqueID, |
Brian Salomon | 4df0092 | 2017-09-07 16:34:11 +0000 | [diff] [blame] | 19 | SkColorSpace* cs) |
Greg Daniel | 8e9b4c4 | 2018-07-20 10:30:48 -0400 | [diff] [blame] | 20 | : INHERITED(context, original->width(), original->height(), |
Brian Salomon | 4df0092 | 2017-09-07 16:34:11 +0000 | [diff] [blame] | 21 | GrPixelConfigIsAlphaOnly(original->config())) |
Brian Salomon | 4df0092 | 2017-09-07 16:34:11 +0000 | [diff] [blame] | 22 | , fOriginal(std::move(original)) |
| 23 | , fAlphaType(alphaType) |
| 24 | , fColorSpace(cs) |
Greg Daniel | c77085d | 2017-11-01 16:38:48 -0400 | [diff] [blame] | 25 | , fUniqueID(uniqueID) {} |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 26 | |
Brian Osman | b3f3830 | 2018-09-07 15:24:44 -0400 | [diff] [blame] | 27 | void GrTextureAdjuster::makeCopyKey(const CopyParams& params, GrUniqueKey* copyKey) { |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 28 | // 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] | 29 | GrUniqueKey baseKey; |
| 30 | GrMakeKeyFromImageID(&baseKey, fUniqueID, SkIRect::MakeWH(this->width(), this->height())); |
| 31 | MakeCopyKeyFromOrigKey(baseKey, params, copyKey); |
| 32 | } |
| 33 | |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame] | 34 | void GrTextureAdjuster::didCacheCopy(const GrUniqueKey& copyKey, uint32_t contextUniqueID) { |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 35 | // We don't currently have a mechanism for notifications on Images! |
| 36 | } |
| 37 | |
Greg Daniel | e1da1d9 | 2017-10-06 15:59:27 -0400 | [diff] [blame] | 38 | sk_sp<GrTextureProxy> GrTextureAdjuster::refTextureProxyCopy(const CopyParams& copyParams, |
| 39 | bool willBeMipped) { |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 40 | GrProxyProvider* proxyProvider = fContext->contextPriv().proxyProvider(); |
| 41 | |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 42 | GrUniqueKey key; |
Brian Osman | b3f3830 | 2018-09-07 15:24:44 -0400 | [diff] [blame] | 43 | this->makeCopyKey(copyParams, &key); |
Greg Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 44 | sk_sp<GrTextureProxy> cachedCopy; |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 45 | if (key.isValid()) { |
Greg Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 46 | cachedCopy = proxyProvider->findOrCreateProxyByUniqueKey(key, |
| 47 | this->originalProxy()->origin()); |
| 48 | if (cachedCopy && (!willBeMipped || GrMipMapped::kYes == cachedCopy->mipMapped())) { |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 49 | return cachedCopy; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | sk_sp<GrTextureProxy> proxy = this->originalProxyRef(); |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 54 | |
Greg Daniel | c77085d | 2017-11-01 16:38:48 -0400 | [diff] [blame] | 55 | sk_sp<GrTextureProxy> copy = CopyOnGpu(fContext, std::move(proxy), copyParams, willBeMipped); |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 56 | if (copy) { |
| 57 | if (key.isValid()) { |
Robert Phillips | 7881409 | 2017-07-24 15:26:17 -0400 | [diff] [blame] | 58 | SkASSERT(copy->origin() == this->originalProxy()->origin()); |
Greg Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 59 | 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. |
| 65 | proxyProvider->removeUniqueKeyFromProxy(key, cachedCopy.get()); |
| 66 | } |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 67 | proxyProvider->assignUniqueKeyToProxy(key, copy.get()); |
Robert Phillips | 869fe56 | 2018-09-17 14:55:49 -0400 | [diff] [blame] | 68 | this->didCacheCopy(key, proxyProvider->contextUniqueID()); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | return copy; |
| 72 | } |
| 73 | |
Brian Salomon | 2a943df | 2018-05-04 13:43:19 -0400 | [diff] [blame] | 74 | sk_sp<GrTextureProxy> GrTextureAdjuster::onRefTextureProxyForParams( |
| 75 | const GrSamplerState& params, |
Brian Salomon | 2a943df | 2018-05-04 13:43:19 -0400 | [diff] [blame] | 76 | sk_sp<SkColorSpace>* texColorSpace, |
Greg Daniel | 8e9b4c4 | 2018-07-20 10:30:48 -0400 | [diff] [blame] | 77 | bool willBeMipped, |
Brian Salomon | 2a943df | 2018-05-04 13:43:19 -0400 | [diff] [blame] | 78 | SkScalar scaleAdjust[2]) { |
Robert Phillips | 3798c86 | 2017-03-27 11:08:16 -0400 | [diff] [blame] | 79 | sk_sp<GrTextureProxy> proxy = this->originalProxyRef(); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 80 | CopyParams copyParams; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 81 | |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 82 | if (!fContext) { |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 83 | // The texture was abandoned. |
| 84 | return nullptr; |
| 85 | } |
Brian Salomon | 4df0092 | 2017-09-07 16:34:11 +0000 | [diff] [blame] | 86 | |
Brian Salomon | 2a943df | 2018-05-04 13:43:19 -0400 | [diff] [blame] | 87 | if (texColorSpace) { |
| 88 | *texColorSpace = sk_ref_sp(fColorSpace); |
| 89 | } |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 90 | SkASSERT(this->width() <= fContext->contextPriv().caps()->maxTextureSize() && |
| 91 | this->height() <= fContext->contextPriv().caps()->maxTextureSize()); |
Brian Osman | 875f785 | 2018-04-12 13:29:08 -0400 | [diff] [blame] | 92 | |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 93 | bool needsCopyForMipsOnly = false; |
| 94 | if (!params.isRepeated() || |
| 95 | !GrGpu::IsACopyNeededForRepeatWrapMode(fContext->contextPriv().caps(), proxy.get(), |
| 96 | proxy->width(), proxy->height(), params.filter(), |
| 97 | ©Params, scaleAdjust)) { |
| 98 | needsCopyForMipsOnly = GrGpu::IsACopyNeededForMips(fContext->contextPriv().caps(), |
| 99 | proxy.get(), params.filter(), |
| 100 | ©Params); |
| 101 | if (!needsCopyForMipsOnly) { |
| 102 | return proxy; |
| 103 | } |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 104 | } |
| 105 | |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 106 | sk_sp<GrTextureProxy> result = this->refTextureProxyCopy(copyParams, willBeMipped); |
| 107 | if (!result && needsCopyForMipsOnly) { |
| 108 | // If we were unable to make a copy and we only needed a copy for mips, then we will return |
| 109 | // the source texture here and require that the GPU backend is able to fall back to using |
| 110 | // bilerp if mips are required. |
| 111 | return this->originalProxyRef(); |
| 112 | } |
| 113 | return result; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 114 | } |
| 115 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 116 | std::unique_ptr<GrFragmentProcessor> GrTextureAdjuster::createFragmentProcessor( |
| 117 | const SkMatrix& origTextureMatrix, |
Greg Daniel | c77085d | 2017-11-01 16:38:48 -0400 | [diff] [blame] | 118 | const SkRect& constraintRect, |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 119 | FilterConstraint filterConstraint, |
| 120 | bool coordsLimitedToConstraintRect, |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 121 | const GrSamplerState::Filter* filterOrNullForBicubic, |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 122 | SkColorSpace* dstColorSpace) { |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 123 | SkMatrix textureMatrix = origTextureMatrix; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 124 | |
| 125 | SkRect domain; |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 126 | GrSamplerState samplerState; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 127 | if (filterOrNullForBicubic) { |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 128 | samplerState.setFilterMode(*filterOrNullForBicubic); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 129 | } |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 130 | SkScalar scaleAdjust[2] = { 1.0f, 1.0f }; |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 131 | sk_sp<GrTextureProxy> proxy( |
Brian Osman | e7fd8c3 | 2018-10-19 13:30:39 -0400 | [diff] [blame] | 132 | this->refTextureProxyForParams(samplerState, nullptr, scaleAdjust)); |
Robert Phillips | 3798c86 | 2017-03-27 11:08:16 -0400 | [diff] [blame] | 133 | if (!proxy) { |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 134 | return nullptr; |
| 135 | } |
| 136 | // If we made a copy then we only copied the contentArea, in which case the new texture is all |
| 137 | // content. |
Robert Phillips | 3798c86 | 2017-03-27 11:08:16 -0400 | [diff] [blame] | 138 | if (proxy.get() != this->originalProxy()) { |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 139 | textureMatrix.postScale(scaleAdjust[0], scaleAdjust[1]); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | DomainMode domainMode = |
Greg Daniel | c77085d | 2017-11-01 16:38:48 -0400 | [diff] [blame] | 143 | DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect, |
| 144 | proxy.get(), filterOrNullForBicubic, &domain); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 145 | if (kTightCopy_DomainMode == domainMode) { |
| 146 | // TODO: Copy the texture and adjust the texture matrix (both parts need to consider |
| 147 | // non-int constraint rect) |
| 148 | // For now: treat as bilerp and ignore what goes on above level 0. |
| 149 | |
| 150 | // We only expect MIP maps to require a tight copy. |
| 151 | SkASSERT(filterOrNullForBicubic && |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 152 | GrSamplerState::Filter::kMipMap == *filterOrNullForBicubic); |
| 153 | static const GrSamplerState::Filter kBilerp = GrSamplerState::Filter::kBilerp; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 154 | domainMode = |
Greg Daniel | c77085d | 2017-11-01 16:38:48 -0400 | [diff] [blame] | 155 | DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect, |
| 156 | proxy.get(), &kBilerp, &domain); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 157 | SkASSERT(kTightCopy_DomainMode != domainMode); |
| 158 | } |
| 159 | SkASSERT(kNoDomain_DomainMode == domainMode || |
| 160 | (domain.fLeft <= domain.fRight && domain.fTop <= domain.fBottom)); |
Brian Osman | 5e34167 | 2017-10-18 10:23:18 -0400 | [diff] [blame] | 161 | auto fp = CreateFragmentProcessorForDomainAndFilter(std::move(proxy), textureMatrix, |
| 162 | domainMode, domain, filterOrNullForBicubic); |
Brian Osman | 21fc5ce | 2018-08-27 20:36:19 +0000 | [diff] [blame] | 163 | return GrColorSpaceXformEffect::Make(std::move(fp), fColorSpace, fAlphaType, dstColorSpace); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 164 | } |