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, |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 17 | GrSurfaceProxyView original, |
Greg Daniel | a4828a1 | 2019-10-11 13:51:02 -0400 | [diff] [blame] | 18 | const GrColorInfo& colorInfo, |
| 19 | uint32_t uniqueID, |
| 20 | bool useDecal) |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 21 | : INHERITED(context, {colorInfo, original.proxy()->dimensions()}, useDecal) |
Greg Daniel | a4828a1 | 2019-10-11 13:51:02 -0400 | [diff] [blame] | 22 | , fOriginal(std::move(original)) |
| 23 | , fUniqueID(uniqueID) {} |
| 24 | |
Brian Salomon | c8d092a | 2020-02-24 10:14:21 -0500 | [diff] [blame^] | 25 | void GrTextureAdjuster::makeMipMappedKey(GrUniqueKey* mipMappedKey) { |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 26 | // 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] | 27 | GrUniqueKey baseKey; |
Brian Salomon | 1a217eb | 2019-10-24 10:50:36 -0400 | [diff] [blame] | 28 | GrMakeKeyFromImageID(&baseKey, fUniqueID, SkIRect::MakeSize(this->dimensions())); |
Brian Salomon | c8d092a | 2020-02-24 10:14:21 -0500 | [diff] [blame^] | 29 | MakeMipMappedKeyFromOriginalKey(baseKey, mipMappedKey); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 30 | } |
| 31 | |
Brian Salomon | c8d092a | 2020-02-24 10:14:21 -0500 | [diff] [blame^] | 32 | void GrTextureAdjuster::didCacheMipMappedCopy(const GrUniqueKey& mipMappedKey, |
| 33 | uint32_t contextUniqueID) { |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 34 | // We don't currently have a mechanism for notifications on Images! |
| 35 | } |
| 36 | |
Brian Salomon | c8d092a | 2020-02-24 10:14:21 -0500 | [diff] [blame^] | 37 | GrSurfaceProxyView GrTextureAdjuster::makeMippedCopy() { |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 38 | GrProxyProvider* proxyProvider = this->context()->priv().proxyProvider(); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 39 | |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 40 | GrUniqueKey key; |
Brian Salomon | c8d092a | 2020-02-24 10:14:21 -0500 | [diff] [blame^] | 41 | this->makeMipMappedKey(&key); |
Greg Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 42 | sk_sp<GrTextureProxy> cachedCopy; |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 43 | const GrSurfaceProxyView& originalView = this->originalProxyView(); |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 44 | if (key.isValid()) { |
Greg Daniel | 3a36511 | 2020-02-14 10:47:18 -0500 | [diff] [blame] | 45 | cachedCopy = proxyProvider->findOrCreateProxyByUniqueKey(key, this->colorType()); |
Brian Salomon | c8d092a | 2020-02-24 10:14:21 -0500 | [diff] [blame^] | 46 | if (cachedCopy) { |
| 47 | return {std::move(cachedCopy), originalView.origin(), originalView.swizzle()}; |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
Brian Salomon | c8d092a | 2020-02-24 10:14:21 -0500 | [diff] [blame^] | 51 | GrSurfaceProxyView copyView = GrCopyBaseMipMapToTextureProxy( |
| 52 | this->context(), originalView.proxy(), originalView.origin(), this->colorType()); |
| 53 | if (!copyView) { |
| 54 | return {}; |
Greg Daniel | cce6500 | 2020-01-23 11:16:49 -0500 | [diff] [blame] | 55 | } |
Brian Salomon | c8d092a | 2020-02-24 10:14:21 -0500 | [diff] [blame^] | 56 | if (key.isValid()) { |
| 57 | SkASSERT(copyView.origin() == originalView.origin()); |
| 58 | proxyProvider->assignUniqueKeyToProxy(key, copyView.asTextureProxy()); |
| 59 | this->didCacheMipMappedCopy(key, proxyProvider->contextID()); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 60 | } |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 61 | return copyView; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 62 | } |
| 63 | |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 64 | GrSurfaceProxyView GrTextureAdjuster::onRefTextureProxyViewForParams(GrSamplerState params, |
Brian Salomon | c8d092a | 2020-02-24 10:14:21 -0500 | [diff] [blame^] | 65 | bool willBeMipped) { |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 66 | if (this->context()->priv().abandoned()) { |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 67 | // The texture was abandoned. |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 68 | return {}; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 69 | } |
Brian Salomon | 4df0092 | 2017-09-07 16:34:11 +0000 | [diff] [blame] | 70 | |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 71 | SkASSERT(this->width() <= this->context()->priv().caps()->maxTextureSize() && |
| 72 | this->height() <= this->context()->priv().caps()->maxTextureSize()); |
Brian Osman | 875f785 | 2018-04-12 13:29:08 -0400 | [diff] [blame] | 73 | |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 74 | GrSurfaceProxyView view = this->originalProxyViewRef(); |
| 75 | GrTextureProxy* texProxy = view.asTextureProxy(); |
| 76 | SkASSERT(texProxy); |
Brian Salomon | c8d092a | 2020-02-24 10:14:21 -0500 | [diff] [blame^] | 77 | if (!GrGpu::IsACopyNeededForMips(this->context()->priv().caps(), texProxy, params.filter())) { |
| 78 | return view; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 79 | } |
| 80 | |
Brian Salomon | c8d092a | 2020-02-24 10:14:21 -0500 | [diff] [blame^] | 81 | GrSurfaceProxyView copy = this->makeMippedCopy(); |
| 82 | if (!copy) { |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 83 | // If we were unable to make a copy and we only needed a copy for mips, then we will return |
| 84 | // the source texture here and require that the GPU backend is able to fall back to using |
| 85 | // bilerp if mips are required. |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 86 | return view; |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 87 | } |
Brian Salomon | c8d092a | 2020-02-24 10:14:21 -0500 | [diff] [blame^] | 88 | SkASSERT(copy.asTextureProxy()); |
| 89 | return copy; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 90 | } |
| 91 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 92 | std::unique_ptr<GrFragmentProcessor> GrTextureAdjuster::createFragmentProcessor( |
Brian Salomon | c8d092a | 2020-02-24 10:14:21 -0500 | [diff] [blame^] | 93 | const SkMatrix& textureMatrix, |
Greg Daniel | c77085d | 2017-11-01 16:38:48 -0400 | [diff] [blame] | 94 | const SkRect& constraintRect, |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 95 | FilterConstraint filterConstraint, |
| 96 | bool coordsLimitedToConstraintRect, |
Brian Osman | 05c8f46 | 2018-10-22 17:13:36 -0400 | [diff] [blame] | 97 | const GrSamplerState::Filter* filterOrNullForBicubic) { |
Brian Salomon | c8d092a | 2020-02-24 10:14:21 -0500 | [diff] [blame^] | 98 | GrSurfaceProxyView view = this->viewForParams(filterOrNullForBicubic); |
| 99 | if (!view) { |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 100 | return nullptr; |
| 101 | } |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 102 | SkASSERT(view.asTextureProxy()); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 103 | |
Michael Ludwig | ddeed37 | 2019-02-20 16:50:10 -0500 | [diff] [blame] | 104 | SkRect domain; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 105 | DomainMode domainMode = |
Greg Daniel | c77085d | 2017-11-01 16:38:48 -0400 | [diff] [blame] | 106 | DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect, |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 107 | view.proxy(), filterOrNullForBicubic, &domain); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 108 | if (kTightCopy_DomainMode == domainMode) { |
| 109 | // TODO: Copy the texture and adjust the texture matrix (both parts need to consider |
| 110 | // non-int constraint rect) |
| 111 | // For now: treat as bilerp and ignore what goes on above level 0. |
| 112 | |
| 113 | // We only expect MIP maps to require a tight copy. |
| 114 | SkASSERT(filterOrNullForBicubic && |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 115 | GrSamplerState::Filter::kMipMap == *filterOrNullForBicubic); |
| 116 | static const GrSamplerState::Filter kBilerp = GrSamplerState::Filter::kBilerp; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 117 | domainMode = |
Greg Daniel | c77085d | 2017-11-01 16:38:48 -0400 | [diff] [blame] | 118 | DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect, |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 119 | view.proxy(), &kBilerp, &domain); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 120 | SkASSERT(kTightCopy_DomainMode != domainMode); |
| 121 | } |
| 122 | SkASSERT(kNoDomain_DomainMode == domainMode || |
| 123 | (domain.fLeft <= domain.fRight && domain.fTop <= domain.fBottom)); |
Michael Ludwig | ddeed37 | 2019-02-20 16:50:10 -0500 | [diff] [blame] | 124 | return this->createFragmentProcessorForDomainAndFilter( |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 125 | std::move(view), textureMatrix, domainMode, domain, filterOrNullForBicubic); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 126 | } |