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 Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 15 | |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 16 | GrSurfaceProxyView GrTextureMaker::onRefTextureProxyViewForParams(GrSamplerState params, |
| 17 | bool willBeMipped, |
| 18 | SkScalar scaleAdjust[2]) { |
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 | |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 24 | CopyParams copyParams; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 25 | |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 26 | GrSurfaceProxyView original = this->refOriginalTextureProxyView(willBeMipped, |
| 27 | AllowedTexGenType::kCheap); |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 28 | bool needsCopyForMipsOnly = false; |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 29 | if (original.proxy()) { |
| 30 | GrTextureProxy* texProxy = original.asTextureProxy(); |
| 31 | SkASSERT(texProxy); |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 32 | if (!params.isRepeated() || |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 33 | !GrGpu::IsACopyNeededForRepeatWrapMode(this->context()->priv().caps(), texProxy, |
| 34 | texProxy->dimensions(), params.filter(), |
Brian Salomon | 1a217eb | 2019-10-24 10:50:36 -0400 | [diff] [blame] | 35 | ©Params, scaleAdjust)) { |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 36 | needsCopyForMipsOnly = GrGpu::IsACopyNeededForMips(this->context()->priv().caps(), |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 37 | texProxy, params.filter(), |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 38 | ©Params); |
| 39 | if (!needsCopyForMipsOnly) { |
| 40 | return original; |
| 41 | } |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 42 | } |
| 43 | } else { |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 44 | if (!params.isRepeated() || |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 45 | !GrGpu::IsACopyNeededForRepeatWrapMode(this->context()->priv().caps(), nullptr, |
Brian Salomon | 1a217eb | 2019-10-24 10:50:36 -0400 | [diff] [blame] | 46 | this->dimensions(), params.filter(), ©Params, |
| 47 | scaleAdjust)) { |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 48 | return this->refOriginalTextureProxyView(willBeMipped, AllowedTexGenType::kAny); |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 49 | } |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 50 | } |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 51 | |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 52 | GrProxyProvider* proxyProvider = this->context()->priv().proxyProvider(); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 53 | |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 54 | GrSurfaceOrigin origOrigin = original.proxy() ? original.origin() : kTopLeft_GrSurfaceOrigin; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 55 | GrUniqueKey copyKey; |
Brian Osman | b3f3830 | 2018-09-07 15:24:44 -0400 | [diff] [blame] | 56 | this->makeCopyKey(copyParams, ©Key); |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 57 | GrSurfaceProxyView cachedView; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 58 | if (copyKey.isValid()) { |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 59 | auto cachedProxy = |
Brian Salomon | 2af3e70 | 2019-08-11 19:10:31 -0400 | [diff] [blame] | 60 | proxyProvider->findOrCreateProxyByUniqueKey(copyKey, this->colorType(), origOrigin); |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 61 | if (cachedProxy) { |
| 62 | GrMipMapped mipped = cachedProxy->mipMapped(); |
| 63 | // TODO: Once we no longer use CopyOnGpu which can fallback to arbitrary formats and |
| 64 | // colorTypes, we can use the swizzle of the originalView. |
Greg Daniel | 87506ab | 2020-02-12 13:05:42 -0500 | [diff] [blame^] | 65 | GrSwizzle swizzle = cachedProxy->textureSwizzleDoNotUse(); |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 66 | cachedView = GrSurfaceProxyView(std::move(cachedProxy), origOrigin, swizzle); |
| 67 | if (!willBeMipped || GrMipMapped::kYes == mipped) { |
| 68 | return cachedView; |
| 69 | } |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 73 | GrSurfaceProxyView source; |
| 74 | if (original.proxy()) { |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 75 | source = std::move(original); |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 76 | } else if (cachedView.proxy()) { |
| 77 | source = cachedView; |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 78 | } else { |
Greg Daniel | 2d59d2f | 2017-10-31 15:25:14 -0400 | [diff] [blame] | 79 | // 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] | 80 | source = this->refOriginalTextureProxyView(false, AllowedTexGenType::kAny); |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 81 | } |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 82 | |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 83 | if (!source.proxy()) { |
| 84 | return {}; |
Greg Daniel | 2d59d2f | 2017-10-31 15:25:14 -0400 | [diff] [blame] | 85 | } |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 86 | SkASSERT(source.asTextureProxy()); |
Greg Daniel | 2d59d2f | 2017-10-31 15:25:14 -0400 | [diff] [blame] | 87 | |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 88 | GrSurfaceProxyView result = |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 89 | CopyOnGpu(this->context(), source, this->colorType(), copyParams, willBeMipped); |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 90 | |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 91 | if (!result.proxy()) { |
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 | } |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 98 | return {}; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | if (copyKey.isValid()) { |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 102 | SkASSERT(result.origin() == origOrigin); |
| 103 | if (cachedView.proxy()) { |
| 104 | SkASSERT(GrMipMapped::kYes == result.asTextureProxy()->mipMapped() && |
| 105 | GrMipMapped::kNo == cachedView.asTextureProxy()->mipMapped()); |
Greg Daniel | 2d59d2f | 2017-10-31 15:25:14 -0400 | [diff] [blame] | 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. |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 109 | SkASSERT(cachedView.asTextureProxy()->getUniqueKey() == copyKey); |
| 110 | proxyProvider->removeUniqueKeyFromProxy(cachedView.asTextureProxy()); |
Greg Daniel | 2d59d2f | 2017-10-31 15:25:14 -0400 | [diff] [blame] | 111 | } |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 112 | proxyProvider->assignUniqueKeyToProxy(copyKey, result.asTextureProxy()); |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 113 | this->didCacheCopy(copyKey, proxyProvider->contextID()); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 114 | } |
| 115 | return result; |
| 116 | } |
| 117 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 118 | std::unique_ptr<GrFragmentProcessor> GrTextureMaker::createFragmentProcessor( |
| 119 | const SkMatrix& textureMatrix, |
| 120 | const SkRect& constraintRect, |
| 121 | FilterConstraint filterConstraint, |
| 122 | bool coordsLimitedToConstraintRect, |
Brian Osman | 05c8f46 | 2018-10-22 17:13:36 -0400 | [diff] [blame] | 123 | const GrSamplerState::Filter* filterOrNullForBicubic) { |
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) { |
Michael Ludwig | ddeed37 | 2019-02-20 16:50:10 -0500 | [diff] [blame] | 127 | // 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] | 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 | |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 136 | SkScalar scaleAdjust[2] = { 1.0f, 1.0f }; |
Greg Daniel | c61d7e3 | 2020-02-04 14:27:45 -0500 | [diff] [blame] | 137 | GrSurfaceProxyView view = this->viewForParams(filterOrNullForBicubic, scaleAdjust); |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 138 | if (!view.proxy()) { |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 139 | return nullptr; |
| 140 | } |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 141 | SkMatrix adjustedMatrix = textureMatrix; |
| 142 | adjustedMatrix.postScale(scaleAdjust[0], scaleAdjust[1]); |
Michael Ludwig | ddeed37 | 2019-02-20 16:50:10 -0500 | [diff] [blame] | 143 | |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 144 | SkRect domain; |
| 145 | DomainMode domainMode = |
Brian Salomon | 4df0092 | 2017-09-07 16:34:11 +0000 | [diff] [blame] | 146 | DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect, |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 147 | view.proxy(), fmForDetermineDomain, &domain); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 148 | SkASSERT(kTightCopy_DomainMode != domainMode); |
Michael Ludwig | ddeed37 | 2019-02-20 16:50:10 -0500 | [diff] [blame] | 149 | return this->createFragmentProcessorForDomainAndFilter( |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 150 | std::move(view), adjustedMatrix, domainMode, domain, filterOrNullForBicubic); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 151 | } |