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 Osman | b3f3830 | 2018-09-07 15:24:44 -0400 | [diff] [blame] | 25 | void GrTextureAdjuster::makeCopyKey(const CopyParams& params, GrUniqueKey* copyKey) { |
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 Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 29 | MakeCopyKeyFromOrigKey(baseKey, params, copyKey); |
| 30 | } |
| 31 | |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame] | 32 | void GrTextureAdjuster::didCacheCopy(const GrUniqueKey& copyKey, uint32_t contextUniqueID) { |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 33 | // We don't currently have a mechanism for notifications on Images! |
| 34 | } |
| 35 | |
Greg Daniel | c61d7e3 | 2020-02-04 14:27:45 -0500 | [diff] [blame] | 36 | GrSurfaceProxyView GrTextureAdjuster::copy(const CopyParams& copyParams, bool willBeMipped, |
| 37 | bool copyForMipsOnly) { |
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 Osman | b3f3830 | 2018-09-07 15:24:44 -0400 | [diff] [blame] | 41 | this->makeCopyKey(copyParams, &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()) { |
Brian Salomon | 2af3e70 | 2019-08-11 19:10:31 -0400 | [diff] [blame] | 45 | cachedCopy = proxyProvider->findOrCreateProxyByUniqueKey(key, this->colorType(), |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 46 | originalView.origin()); |
Greg Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 47 | if (cachedCopy && (!willBeMipped || GrMipMapped::kYes == cachedCopy->mipMapped())) { |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 48 | // TODO: Once we no longer use CopyOnGpu which can fallback to arbitrary formats and |
| 49 | // colorTypes, we can use the swizzle of the originalView. |
Greg Daniel | 87506ab | 2020-02-12 13:05:42 -0500 | [diff] [blame] | 50 | GrSwizzle swizzle = cachedCopy->textureSwizzleDoNotUse(); |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 51 | return GrSurfaceProxyView(std::move(cachedCopy), originalView.origin(), swizzle); |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 55 | GrSurfaceProxyView copyView; |
Greg Daniel | cce6500 | 2020-01-23 11:16:49 -0500 | [diff] [blame] | 56 | if (copyForMipsOnly) { |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 57 | copyView = GrCopyBaseMipMapToTextureProxy(this->context(), originalView.proxy(), |
| 58 | originalView.origin(), this->colorType()); |
Greg Daniel | cce6500 | 2020-01-23 11:16:49 -0500 | [diff] [blame] | 59 | } else { |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 60 | copyView = CopyOnGpu(this->context(), this->originalProxyViewRef(), this->colorType(), |
| 61 | copyParams, willBeMipped); |
Greg Daniel | cce6500 | 2020-01-23 11:16:49 -0500 | [diff] [blame] | 62 | } |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 63 | if (copyView.proxy()) { |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 64 | if (key.isValid()) { |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 65 | SkASSERT(copyView.origin() == originalView.origin()); |
Greg Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 66 | if (cachedCopy) { |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 67 | SkASSERT(GrMipMapped::kYes == copyView.asTextureProxy()->mipMapped() && |
Greg Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 68 | GrMipMapped::kNo == cachedCopy->mipMapped()); |
| 69 | // If we had a cachedProxy, that means there already is a proxy in the cache which |
| 70 | // matches the key, but it does not have mip levels and we require them. Thus we |
| 71 | // must remove the unique key from that proxy. |
Chris Dalton | 2de13dd | 2019-01-03 15:11:59 -0700 | [diff] [blame] | 72 | SkASSERT(cachedCopy->getUniqueKey() == key); |
| 73 | proxyProvider->removeUniqueKeyFromProxy(cachedCopy.get()); |
Greg Daniel | 09c9400 | 2018-06-08 22:11:51 +0000 | [diff] [blame] | 74 | } |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 75 | proxyProvider->assignUniqueKeyToProxy(key, copyView.asTextureProxy()); |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 76 | this->didCacheCopy(key, proxyProvider->contextID()); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 77 | } |
| 78 | } |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 79 | return copyView; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 80 | } |
| 81 | |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 82 | GrSurfaceProxyView GrTextureAdjuster::onRefTextureProxyViewForParams(GrSamplerState params, |
| 83 | bool willBeMipped, |
| 84 | SkScalar scaleAdjust[2]) { |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 85 | if (this->context()->priv().abandoned()) { |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 86 | // The texture was abandoned. |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 87 | return {}; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 88 | } |
Brian Salomon | 4df0092 | 2017-09-07 16:34:11 +0000 | [diff] [blame] | 89 | |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 90 | SkASSERT(this->width() <= this->context()->priv().caps()->maxTextureSize() && |
| 91 | this->height() <= this->context()->priv().caps()->maxTextureSize()); |
Brian Osman | 875f785 | 2018-04-12 13:29:08 -0400 | [diff] [blame] | 92 | |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 93 | GrSurfaceProxyView view = this->originalProxyViewRef(); |
| 94 | GrTextureProxy* texProxy = view.asTextureProxy(); |
| 95 | SkASSERT(texProxy); |
| 96 | CopyParams copyParams; |
| 97 | |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 98 | bool needsCopyForMipsOnly = false; |
| 99 | if (!params.isRepeated() || |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 100 | !GrGpu::IsACopyNeededForRepeatWrapMode(this->context()->priv().caps(), texProxy, |
| 101 | texProxy->dimensions(), params.filter(), ©Params, |
Brian Salomon | 1a217eb | 2019-10-24 10:50:36 -0400 | [diff] [blame] | 102 | scaleAdjust)) { |
Robert Phillips | 9338c60 | 2019-02-19 12:52:29 -0500 | [diff] [blame] | 103 | needsCopyForMipsOnly = GrGpu::IsACopyNeededForMips(this->context()->priv().caps(), |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 104 | texProxy, params.filter(), |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 105 | ©Params); |
| 106 | if (!needsCopyForMipsOnly) { |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 107 | return view; |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 108 | } |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 109 | } |
| 110 | |
Greg Daniel | c61d7e3 | 2020-02-04 14:27:45 -0500 | [diff] [blame] | 111 | GrSurfaceProxyView result = this->copy(copyParams, willBeMipped, needsCopyForMipsOnly); |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 112 | if (!result.proxy() && needsCopyForMipsOnly) { |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 113 | // If we were unable to make a copy and we only needed a copy for mips, then we will return |
| 114 | // the source texture here and require that the GPU backend is able to fall back to using |
| 115 | // bilerp if mips are required. |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 116 | return view; |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 117 | } |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 118 | SkASSERT(result.asTextureProxy()); |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 119 | return result; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 120 | } |
| 121 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 122 | std::unique_ptr<GrFragmentProcessor> GrTextureAdjuster::createFragmentProcessor( |
| 123 | const SkMatrix& origTextureMatrix, |
Greg Daniel | c77085d | 2017-11-01 16:38:48 -0400 | [diff] [blame] | 124 | const SkRect& constraintRect, |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 125 | FilterConstraint filterConstraint, |
| 126 | bool coordsLimitedToConstraintRect, |
Brian Osman | 05c8f46 | 2018-10-22 17:13:36 -0400 | [diff] [blame] | 127 | const GrSamplerState::Filter* filterOrNullForBicubic) { |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 128 | SkMatrix textureMatrix = origTextureMatrix; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 129 | |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 130 | SkScalar scaleAdjust[2] = { 1.0f, 1.0f }; |
Greg Daniel | c61d7e3 | 2020-02-04 14:27:45 -0500 | [diff] [blame] | 131 | GrSurfaceProxyView view = this->viewForParams(filterOrNullForBicubic, scaleAdjust); |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 132 | if (!view.proxy()) { |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 133 | return nullptr; |
| 134 | } |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 135 | SkASSERT(view.asTextureProxy()); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 136 | // If we made a copy then we only copied the contentArea, in which case the new texture is all |
| 137 | // content. |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 138 | if (view.proxy() != this->originalProxyView().proxy()) { |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 139 | textureMatrix.postScale(scaleAdjust[0], scaleAdjust[1]); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 140 | } |
| 141 | |
Michael Ludwig | ddeed37 | 2019-02-20 16:50:10 -0500 | [diff] [blame] | 142 | SkRect domain; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 143 | DomainMode domainMode = |
Greg Daniel | c77085d | 2017-11-01 16:38:48 -0400 | [diff] [blame] | 144 | DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect, |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 145 | view.proxy(), filterOrNullForBicubic, &domain); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 146 | if (kTightCopy_DomainMode == domainMode) { |
| 147 | // TODO: Copy the texture and adjust the texture matrix (both parts need to consider |
| 148 | // non-int constraint rect) |
| 149 | // For now: treat as bilerp and ignore what goes on above level 0. |
| 150 | |
| 151 | // We only expect MIP maps to require a tight copy. |
| 152 | SkASSERT(filterOrNullForBicubic && |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 153 | GrSamplerState::Filter::kMipMap == *filterOrNullForBicubic); |
| 154 | static const GrSamplerState::Filter kBilerp = GrSamplerState::Filter::kBilerp; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 155 | domainMode = |
Greg Daniel | c77085d | 2017-11-01 16:38:48 -0400 | [diff] [blame] | 156 | DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect, |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 157 | view.proxy(), &kBilerp, &domain); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 158 | SkASSERT(kTightCopy_DomainMode != domainMode); |
| 159 | } |
| 160 | SkASSERT(kNoDomain_DomainMode == domainMode || |
| 161 | (domain.fLeft <= domain.fRight && domain.fTop <= domain.fBottom)); |
Michael Ludwig | ddeed37 | 2019-02-20 16:50:10 -0500 | [diff] [blame] | 162 | return this->createFragmentProcessorForDomainAndFilter( |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 163 | std::move(view), textureMatrix, domainMode, domain, filterOrNullForBicubic); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 164 | } |