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