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 "GrTextureMaker.h" |
| 9 | |
Brian Osman | 1cb4171 | 2017-10-19 12:54:52 -0400 | [diff] [blame] | 10 | #include "GrColorSpaceXform.h" |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 11 | #include "GrContext.h" |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 12 | #include "GrContextPriv.h" |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 13 | #include "GrGpu.h" |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 14 | #include "GrProxyProvider.h" |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 15 | |
Brian Salomon | 2a943df | 2018-05-04 13:43:19 -0400 | [diff] [blame] | 16 | sk_sp<GrTextureProxy> GrTextureMaker::onRefTextureProxyForParams(const GrSamplerState& params, |
| 17 | SkColorSpace* dstColorSpace, |
| 18 | sk_sp<SkColorSpace>* texColorSpace, |
| 19 | SkScalar scaleAdjust[2]) { |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 20 | if (this->width() > fContext->contextPriv().caps()->maxTextureSize() || |
| 21 | this->height() > fContext->contextPriv().caps()->maxTextureSize()) { |
Brian Osman | 875f785 | 2018-04-12 13:29:08 -0400 | [diff] [blame] | 22 | return nullptr; |
| 23 | } |
| 24 | |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 25 | CopyParams copyParams; |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 26 | bool willBeMipped = params.filter() == GrSamplerState::Filter::kMipMap; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 27 | |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 28 | if (!fContext->contextPriv().caps()->mipMapSupport()) { |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 29 | willBeMipped = false; |
| 30 | } |
| 31 | |
| 32 | if (texColorSpace) { |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 33 | *texColorSpace = this->getColorSpace(dstColorSpace); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 34 | } |
| 35 | |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 36 | sk_sp<GrTextureProxy> original(this->refOriginalTextureProxy(willBeMipped, dstColorSpace, |
| 37 | AllowedTexGenType::kCheap)); |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 38 | bool needsCopyForMipsOnly = false; |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 39 | if (original) { |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 40 | if (!params.isRepeated() || |
| 41 | !GrGpu::IsACopyNeededForRepeatWrapMode(fContext->contextPriv().caps(), original.get(), |
| 42 | original->width(), original->height(), |
| 43 | params.filter(), ©Params, scaleAdjust)) { |
| 44 | needsCopyForMipsOnly = GrGpu::IsACopyNeededForMips(fContext->contextPriv().caps(), |
| 45 | original.get(), params.filter(), |
| 46 | ©Params); |
| 47 | if (!needsCopyForMipsOnly) { |
| 48 | return original; |
| 49 | } |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 50 | } |
| 51 | } else { |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 52 | if (!params.isRepeated() || |
| 53 | !GrGpu::IsACopyNeededForRepeatWrapMode(fContext->contextPriv().caps(), nullptr, |
| 54 | this->width(), this->height(), |
| 55 | params.filter(), ©Params, scaleAdjust)) { |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 56 | return this->refOriginalTextureProxy(willBeMipped, dstColorSpace, |
| 57 | AllowedTexGenType::kAny); |
| 58 | } |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 59 | } |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 60 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 61 | GrProxyProvider* proxyProvider = fContext->contextPriv().proxyProvider(); |
| 62 | |
| 63 | GrSurfaceOrigin origOrigin = original ? original->origin() : kTopLeft_GrSurfaceOrigin; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 64 | GrUniqueKey copyKey; |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 65 | this->makeCopyKey(copyParams, ©Key, dstColorSpace); |
Greg Daniel | 2d59d2f | 2017-10-31 15:25:14 -0400 | [diff] [blame] | 66 | sk_sp<GrTextureProxy> cachedProxy; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 67 | if (copyKey.isValid()) { |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 68 | cachedProxy = proxyProvider->findOrCreateProxyByUniqueKey(copyKey, origOrigin); |
Greg Daniel | 2d59d2f | 2017-10-31 15:25:14 -0400 | [diff] [blame] | 69 | if (cachedProxy && (!willBeMipped || GrMipMapped::kYes == cachedProxy->mipMapped())) { |
| 70 | return cachedProxy; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 74 | sk_sp<GrTextureProxy> source; |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 75 | if (original) { |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 76 | source = std::move(original); |
Greg Daniel | 2d59d2f | 2017-10-31 15:25:14 -0400 | [diff] [blame] | 77 | } else if (cachedProxy) { |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 78 | source = cachedProxy; |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 79 | } else { |
Greg Daniel | 2d59d2f | 2017-10-31 15:25:14 -0400 | [diff] [blame] | 80 | // Since we will be copying this texture there is no reason to make it mipped |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 81 | source = this->refOriginalTextureProxy(false, dstColorSpace, |
Greg Daniel | 2d59d2f | 2017-10-31 15:25:14 -0400 | [diff] [blame] | 82 | AllowedTexGenType::kAny); |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 83 | } |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 84 | |
| 85 | if (!source) { |
Greg Daniel | 2d59d2f | 2017-10-31 15:25:14 -0400 | [diff] [blame] | 86 | return nullptr; |
| 87 | } |
| 88 | |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 89 | sk_sp<GrTextureProxy> result = CopyOnGpu(fContext, source, copyParams, willBeMipped); |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 90 | |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 91 | if (!result) { |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 92 | // If we were unable to make a copy and we only needed a copy for mips, then we will return |
| 93 | // the source texture here and require that the GPU backend is able to fall back to using |
| 94 | // bilerp if mips are required. |
| 95 | if (needsCopyForMipsOnly) { |
| 96 | return source; |
| 97 | } |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 98 | return nullptr; |
| 99 | } |
| 100 | |
| 101 | if (copyKey.isValid()) { |
Robert Phillips | d3e247f | 2017-07-25 08:00:17 -0400 | [diff] [blame] | 102 | SkASSERT(result->origin() == origOrigin); |
Greg Daniel | 2d59d2f | 2017-10-31 15:25:14 -0400 | [diff] [blame] | 103 | if (cachedProxy) { |
| 104 | SkASSERT(GrMipMapped::kYes == result->mipMapped() && |
| 105 | GrMipMapped::kNo == cachedProxy->mipMapped()); |
| 106 | // If we had a cachedProxy, that means there already is a proxy in the cache which |
| 107 | // matches the key, but it does not have mip levels and we require them. Thus we must |
| 108 | // remove the unique key from that proxy. |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 109 | proxyProvider->removeUniqueKeyFromProxy(copyKey, cachedProxy.get()); |
Greg Daniel | 2d59d2f | 2017-10-31 15:25:14 -0400 | [diff] [blame] | 110 | } |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 111 | proxyProvider->assignUniqueKeyToProxy(copyKey, result.get()); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 112 | this->didCacheCopy(copyKey); |
| 113 | } |
| 114 | return result; |
| 115 | } |
| 116 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 117 | std::unique_ptr<GrFragmentProcessor> GrTextureMaker::createFragmentProcessor( |
| 118 | const SkMatrix& textureMatrix, |
| 119 | const SkRect& constraintRect, |
| 120 | FilterConstraint filterConstraint, |
| 121 | bool coordsLimitedToConstraintRect, |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 122 | const GrSamplerState::Filter* filterOrNullForBicubic, |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 123 | SkColorSpace* dstColorSpace) { |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 124 | const GrSamplerState::Filter* fmForDetermineDomain = filterOrNullForBicubic; |
| 125 | if (filterOrNullForBicubic && GrSamplerState::Filter::kMipMap == *filterOrNullForBicubic && |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 126 | kYes_FilterConstraint == filterConstraint) { |
| 127 | // TODo: Here we should force a copy restricted to the constraintRect since MIP maps will |
| 128 | // read outside the constraint rect. However, as in the adjuster case, we aren't currently |
| 129 | // doing that. |
| 130 | // We instead we compute the domain as though were bilerping which is only correct if we |
| 131 | // only sample level 0. |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 132 | static const GrSamplerState::Filter kBilerp = GrSamplerState::Filter::kBilerp; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 133 | fmForDetermineDomain = &kBilerp; |
| 134 | } |
| 135 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 136 | GrSamplerState samplerState; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 137 | if (filterOrNullForBicubic) { |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 138 | samplerState = GrSamplerState(GrSamplerState::WrapMode::kClamp, *filterOrNullForBicubic); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 139 | } else { |
| 140 | // Bicubic doesn't use filtering for it's texture accesses. |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 141 | samplerState = GrSamplerState::ClampNearest(); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 142 | } |
| 143 | sk_sp<SkColorSpace> texColorSpace; |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 144 | SkScalar scaleAdjust[2] = { 1.0f, 1.0f }; |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 145 | sk_sp<GrTextureProxy> proxy(this->refTextureProxyForParams(samplerState, dstColorSpace, |
| 146 | &texColorSpace, scaleAdjust)); |
Robert Phillips | 3798c86 | 2017-03-27 11:08:16 -0400 | [diff] [blame] | 147 | if (!proxy) { |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 148 | return nullptr; |
| 149 | } |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 150 | SkMatrix adjustedMatrix = textureMatrix; |
| 151 | adjustedMatrix.postScale(scaleAdjust[0], scaleAdjust[1]); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 152 | SkRect domain; |
| 153 | DomainMode domainMode = |
Brian Salomon | 4df0092 | 2017-09-07 16:34:11 +0000 | [diff] [blame] | 154 | DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect, |
Greg Daniel | c77085d | 2017-11-01 16:38:48 -0400 | [diff] [blame] | 155 | proxy.get(), fmForDetermineDomain, &domain); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 156 | SkASSERT(kTightCopy_DomainMode != domainMode); |
Brian Osman | 5e34167 | 2017-10-18 10:23:18 -0400 | [diff] [blame] | 157 | auto fp = CreateFragmentProcessorForDomainAndFilter(std::move(proxy), adjustedMatrix, |
| 158 | domainMode, domain, filterOrNullForBicubic); |
Brian Osman | 653f34d | 2018-06-14 11:44:02 -0400 | [diff] [blame] | 159 | return GrColorSpaceXformEffect::Make(std::move(fp), texColorSpace.get(), dstColorSpace); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 160 | } |