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 "src/gpu/GrTextureMaker.h" |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/private/GrRecordingContext.h" |
| 11 | #include "src/gpu/GrColorSpaceXform.h" |
| 12 | #include "src/gpu/GrGpu.h" |
| 13 | #include "src/gpu/GrProxyProvider.h" |
| 14 | #include "src/gpu/GrRecordingContextPriv.h" |
Brian Salomon | adc9bbb | 2020-02-20 20:10:31 -0500 | [diff] [blame^] | 15 | #include "src/gpu/SkGr.h" |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 16 | |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 17 | GrSurfaceProxyView GrTextureMaker::onRefTextureProxyViewForParams(GrSamplerState params, |
Brian Salomon | c8d092a | 2020-02-24 10:14:21 -0500 | [diff] [blame] | 18 | bool willBeMipped) { |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 19 | if (this->width() > this->context()->priv().caps()->maxTextureSize() || |
| 20 | this->height() > this->context()->priv().caps()->maxTextureSize()) { |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 21 | return {}; |
Brian Osman | 875f785 | 2018-04-12 13:29:08 -0400 | [diff] [blame] | 22 | } |
| 23 | |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 24 | GrSurfaceProxyView original = this->refOriginalTextureProxyView(willBeMipped, |
| 25 | AllowedTexGenType::kCheap); |
Brian Salomon | c8d092a | 2020-02-24 10:14:21 -0500 | [diff] [blame] | 26 | if (!original) { |
| 27 | return this->refOriginalTextureProxyView(willBeMipped, AllowedTexGenType::kAny); |
| 28 | } |
| 29 | |
| 30 | GrTextureProxy* texProxy = original.asTextureProxy(); |
| 31 | if (!GrGpu::IsACopyNeededForMips(this->context()->priv().caps(), texProxy, params.filter())) { |
| 32 | return original; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 33 | } |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 34 | |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 35 | GrProxyProvider* proxyProvider = this->context()->priv().proxyProvider(); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 36 | |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 37 | GrSurfaceOrigin origOrigin = original.proxy() ? original.origin() : kTopLeft_GrSurfaceOrigin; |
Brian Salomon | c8d092a | 2020-02-24 10:14:21 -0500 | [diff] [blame] | 38 | GrUniqueKey mipMappedKey; |
| 39 | this->makeMipMappedKey(&mipMappedKey); |
| 40 | if (mipMappedKey.isValid()) { |
| 41 | auto cachedProxy = |
| 42 | proxyProvider->findOrCreateProxyByUniqueKey(mipMappedKey, this->colorType()); |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 43 | if (cachedProxy) { |
Brian Salomon | c8d092a | 2020-02-24 10:14:21 -0500 | [diff] [blame] | 44 | SkASSERT(cachedProxy->mipMapped() == GrMipMapped::kYes); |
Brian Salomon | adc9bbb | 2020-02-20 20:10:31 -0500 | [diff] [blame^] | 45 | return GrSurfaceProxyView(std::move(cachedProxy), origOrigin, original.swizzle()); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 49 | GrSurfaceProxyView source; |
Brian Salomon | c8d092a | 2020-02-24 10:14:21 -0500 | [diff] [blame] | 50 | if (original) { |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 51 | source = std::move(original); |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 52 | } else { |
Greg Daniel | 2d59d2f | 2017-10-31 15:25:14 -0400 | [diff] [blame] | 53 | // Since we will be copying this texture there is no reason to make it mipped |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 54 | source = this->refOriginalTextureProxyView(false, AllowedTexGenType::kAny); |
Brian Salomon | c8d092a | 2020-02-24 10:14:21 -0500 | [diff] [blame] | 55 | if (!source) { |
| 56 | return {}; |
| 57 | } |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 58 | } |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 59 | |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 60 | SkASSERT(source.asTextureProxy()); |
Greg Daniel | 2d59d2f | 2017-10-31 15:25:14 -0400 | [diff] [blame] | 61 | |
Brian Salomon | adc9bbb | 2020-02-20 20:10:31 -0500 | [diff] [blame^] | 62 | GrSurfaceProxyView result = GrCopyBaseMipMapToTextureProxy(this->context(), source.proxy(), |
| 63 | source.origin(), this->colorType()); |
Brian Salomon | c8d092a | 2020-02-24 10:14:21 -0500 | [diff] [blame] | 64 | if (!result) { |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 65 | // If we were unable to make a copy and we only needed a copy for mips, then we will return |
| 66 | // the source texture here and require that the GPU backend is able to fall back to using |
| 67 | // bilerp if mips are required. |
Brian Salomon | c8d092a | 2020-02-24 10:14:21 -0500 | [diff] [blame] | 68 | return source; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 69 | } |
| 70 | |
Brian Salomon | c8d092a | 2020-02-24 10:14:21 -0500 | [diff] [blame] | 71 | if (mipMappedKey.isValid()) { |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 72 | SkASSERT(result.origin() == origOrigin); |
Brian Salomon | c8d092a | 2020-02-24 10:14:21 -0500 | [diff] [blame] | 73 | proxyProvider->assignUniqueKeyToProxy(mipMappedKey, result.asTextureProxy()); |
| 74 | this->didCacheMipMappedCopy(mipMappedKey, proxyProvider->contextID()); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 75 | } |
| 76 | return result; |
| 77 | } |
| 78 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 79 | std::unique_ptr<GrFragmentProcessor> GrTextureMaker::createFragmentProcessor( |
| 80 | const SkMatrix& textureMatrix, |
| 81 | const SkRect& constraintRect, |
| 82 | FilterConstraint filterConstraint, |
| 83 | bool coordsLimitedToConstraintRect, |
Brian Osman | 05c8f46 | 2018-10-22 17:13:36 -0400 | [diff] [blame] | 84 | const GrSamplerState::Filter* filterOrNullForBicubic) { |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 85 | const GrSamplerState::Filter* fmForDetermineDomain = filterOrNullForBicubic; |
| 86 | if (filterOrNullForBicubic && GrSamplerState::Filter::kMipMap == *filterOrNullForBicubic && |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 87 | kYes_FilterConstraint == filterConstraint) { |
Michael Ludwig | ddeed37 | 2019-02-20 16:50:10 -0500 | [diff] [blame] | 88 | // TODO: Here we should force a copy restricted to the constraintRect since MIP maps will |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 89 | // read outside the constraint rect. However, as in the adjuster case, we aren't currently |
| 90 | // doing that. |
| 91 | // We instead we compute the domain as though were bilerping which is only correct if we |
| 92 | // only sample level 0. |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 93 | static const GrSamplerState::Filter kBilerp = GrSamplerState::Filter::kBilerp; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 94 | fmForDetermineDomain = &kBilerp; |
| 95 | } |
| 96 | |
Brian Salomon | c8d092a | 2020-02-24 10:14:21 -0500 | [diff] [blame] | 97 | GrSurfaceProxyView view = this->viewForParams(filterOrNullForBicubic); |
| 98 | if (!view) { |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 99 | return nullptr; |
| 100 | } |
Michael Ludwig | ddeed37 | 2019-02-20 16:50:10 -0500 | [diff] [blame] | 101 | |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 102 | SkRect domain; |
| 103 | DomainMode domainMode = |
Brian Salomon | 4df0092 | 2017-09-07 16:34:11 +0000 | [diff] [blame] | 104 | DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect, |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 105 | view.proxy(), fmForDetermineDomain, &domain); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 106 | SkASSERT(kTightCopy_DomainMode != domainMode); |
Michael Ludwig | ddeed37 | 2019-02-20 16:50:10 -0500 | [diff] [blame] | 107 | return this->createFragmentProcessorForDomainAndFilter( |
Brian Salomon | c8d092a | 2020-02-24 10:14:21 -0500 | [diff] [blame] | 108 | std::move(view), textureMatrix, domainMode, domain, filterOrNullForBicubic); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 109 | } |