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 | |
| 8 | #include "GrTextureAdjuster.h" |
| 9 | |
Brian Osman | 1cb4171 | 2017-10-19 12:54:52 -0400 | [diff] [blame] | 10 | #include "GrColorSpaceXform.h" |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 11 | #include "GrContext.h" |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 12 | #include "GrContextPriv.h" |
Robert Phillips | 009e9af | 2017-06-15 14:01:04 -0400 | [diff] [blame] | 13 | #include "GrGpu.h" |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 14 | #include "GrProxyProvider.h" |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 15 | #include "SkGr.h" |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 16 | |
Robert Phillips | 3798c86 | 2017-03-27 11:08:16 -0400 | [diff] [blame] | 17 | GrTextureAdjuster::GrTextureAdjuster(GrContext* context, sk_sp<GrTextureProxy> original, |
Brian Salomon | 4df0092 | 2017-09-07 16:34:11 +0000 | [diff] [blame] | 18 | SkAlphaType alphaType, |
Greg Daniel | c77085d | 2017-11-01 16:38:48 -0400 | [diff] [blame] | 19 | uint32_t uniqueID, |
Brian Salomon | 4df0092 | 2017-09-07 16:34:11 +0000 | [diff] [blame] | 20 | SkColorSpace* cs) |
Greg Daniel | c77085d | 2017-11-01 16:38:48 -0400 | [diff] [blame] | 21 | : INHERITED(original->width(), original->height(), |
Brian Salomon | 4df0092 | 2017-09-07 16:34:11 +0000 | [diff] [blame] | 22 | GrPixelConfigIsAlphaOnly(original->config())) |
| 23 | , fContext(context) |
| 24 | , fOriginal(std::move(original)) |
| 25 | , fAlphaType(alphaType) |
| 26 | , fColorSpace(cs) |
Greg Daniel | c77085d | 2017-11-01 16:38:48 -0400 | [diff] [blame] | 27 | , fUniqueID(uniqueID) {} |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 28 | |
| 29 | void GrTextureAdjuster::makeCopyKey(const CopyParams& params, GrUniqueKey* copyKey, |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 30 | SkColorSpace* dstColorSpace) { |
| 31 | // 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] | 32 | GrUniqueKey baseKey; |
| 33 | GrMakeKeyFromImageID(&baseKey, fUniqueID, SkIRect::MakeWH(this->width(), this->height())); |
| 34 | MakeCopyKeyFromOrigKey(baseKey, params, copyKey); |
| 35 | } |
| 36 | |
| 37 | void GrTextureAdjuster::didCacheCopy(const GrUniqueKey& copyKey) { |
| 38 | // We don't currently have a mechanism for notifications on Images! |
| 39 | } |
| 40 | |
Greg Daniel | e1da1d9 | 2017-10-06 15:59:27 -0400 | [diff] [blame] | 41 | sk_sp<GrTextureProxy> GrTextureAdjuster::refTextureProxyCopy(const CopyParams& copyParams, |
| 42 | bool willBeMipped) { |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 43 | GrProxyProvider* proxyProvider = fContext->contextPriv().proxyProvider(); |
| 44 | |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 45 | GrUniqueKey key; |
| 46 | this->makeCopyKey(copyParams, &key, nullptr); |
| 47 | if (key.isValid()) { |
Greg Daniel | cd87140 | 2017-09-26 12:49:26 -0400 | [diff] [blame] | 48 | sk_sp<GrTextureProxy> cachedCopy = |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 49 | proxyProvider->findOrCreateProxyByUniqueKey(key, this->originalProxy()->origin()); |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 50 | if (cachedCopy) { |
| 51 | return cachedCopy; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | sk_sp<GrTextureProxy> proxy = this->originalProxyRef(); |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 56 | |
Greg Daniel | c77085d | 2017-11-01 16:38:48 -0400 | [diff] [blame] | 57 | sk_sp<GrTextureProxy> copy = CopyOnGpu(fContext, std::move(proxy), copyParams, willBeMipped); |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 58 | if (copy) { |
| 59 | if (key.isValid()) { |
Robert Phillips | 7881409 | 2017-07-24 15:26:17 -0400 | [diff] [blame] | 60 | SkASSERT(copy->origin() == this->originalProxy()->origin()); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 61 | proxyProvider->assignUniqueKeyToProxy(key, copy.get()); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 62 | this->didCacheCopy(key); |
| 63 | } |
| 64 | } |
| 65 | return copy; |
| 66 | } |
| 67 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 68 | sk_sp<GrTextureProxy> GrTextureAdjuster::refTextureProxySafeForParams(const GrSamplerState& params, |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 69 | SkScalar scaleAdjust[2]) { |
Robert Phillips | 3798c86 | 2017-03-27 11:08:16 -0400 | [diff] [blame] | 70 | sk_sp<GrTextureProxy> proxy = this->originalProxyRef(); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 71 | CopyParams copyParams; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 72 | |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 73 | if (!fContext) { |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 74 | // The texture was abandoned. |
| 75 | return nullptr; |
| 76 | } |
Brian Salomon | 4df0092 | 2017-09-07 16:34:11 +0000 | [diff] [blame] | 77 | |
Brian Osman | 875f785 | 2018-04-12 13:29:08 -0400 | [diff] [blame^] | 78 | SkASSERT(this->width() <= fContext->caps()->maxTextureSize() && |
| 79 | this->height() <= fContext->caps()->maxTextureSize()); |
| 80 | |
Robert Phillips | abf7b76 | 2018-03-21 12:13:37 -0400 | [diff] [blame] | 81 | if (!GrGpu::IsACopyNeededForTextureParams(fContext->caps(), |
| 82 | proxy.get(), proxy->width(), proxy->height(), |
| 83 | params, ©Params, scaleAdjust)) { |
Robert Phillips | 3798c86 | 2017-03-27 11:08:16 -0400 | [diff] [blame] | 84 | return proxy; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 85 | } |
| 86 | |
Greg Daniel | c77085d | 2017-11-01 16:38:48 -0400 | [diff] [blame] | 87 | bool willBeMipped = GrSamplerState::Filter::kMipMap == params.filter(); |
Greg Daniel | e1da1d9 | 2017-10-06 15:59:27 -0400 | [diff] [blame] | 88 | return this->refTextureProxyCopy(copyParams, willBeMipped); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 89 | } |
| 90 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 91 | std::unique_ptr<GrFragmentProcessor> GrTextureAdjuster::createFragmentProcessor( |
| 92 | const SkMatrix& origTextureMatrix, |
Greg Daniel | c77085d | 2017-11-01 16:38:48 -0400 | [diff] [blame] | 93 | const SkRect& constraintRect, |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 94 | FilterConstraint filterConstraint, |
| 95 | bool coordsLimitedToConstraintRect, |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 96 | const GrSamplerState::Filter* filterOrNullForBicubic, |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 97 | SkColorSpace* dstColorSpace) { |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 98 | SkMatrix textureMatrix = origTextureMatrix; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 99 | |
| 100 | SkRect domain; |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 101 | GrSamplerState samplerState; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 102 | if (filterOrNullForBicubic) { |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 103 | samplerState.setFilterMode(*filterOrNullForBicubic); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 104 | } |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 105 | SkScalar scaleAdjust[2] = { 1.0f, 1.0f }; |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 106 | sk_sp<GrTextureProxy> proxy( |
Greg Daniel | 4faa040 | 2017-09-29 15:21:28 -0400 | [diff] [blame] | 107 | this->refTextureProxySafeForParams(samplerState, scaleAdjust)); |
Robert Phillips | 3798c86 | 2017-03-27 11:08:16 -0400 | [diff] [blame] | 108 | if (!proxy) { |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 109 | return nullptr; |
| 110 | } |
| 111 | // If we made a copy then we only copied the contentArea, in which case the new texture is all |
| 112 | // content. |
Robert Phillips | 3798c86 | 2017-03-27 11:08:16 -0400 | [diff] [blame] | 113 | if (proxy.get() != this->originalProxy()) { |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 114 | textureMatrix.postScale(scaleAdjust[0], scaleAdjust[1]); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | DomainMode domainMode = |
Greg Daniel | c77085d | 2017-11-01 16:38:48 -0400 | [diff] [blame] | 118 | DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect, |
| 119 | proxy.get(), filterOrNullForBicubic, &domain); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 120 | if (kTightCopy_DomainMode == domainMode) { |
| 121 | // TODO: Copy the texture and adjust the texture matrix (both parts need to consider |
| 122 | // non-int constraint rect) |
| 123 | // For now: treat as bilerp and ignore what goes on above level 0. |
| 124 | |
| 125 | // We only expect MIP maps to require a tight copy. |
| 126 | SkASSERT(filterOrNullForBicubic && |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 127 | GrSamplerState::Filter::kMipMap == *filterOrNullForBicubic); |
| 128 | static const GrSamplerState::Filter kBilerp = GrSamplerState::Filter::kBilerp; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 129 | domainMode = |
Greg Daniel | c77085d | 2017-11-01 16:38:48 -0400 | [diff] [blame] | 130 | DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect, |
| 131 | proxy.get(), &kBilerp, &domain); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 132 | SkASSERT(kTightCopy_DomainMode != domainMode); |
| 133 | } |
| 134 | SkASSERT(kNoDomain_DomainMode == domainMode || |
| 135 | (domain.fLeft <= domain.fRight && domain.fTop <= domain.fBottom)); |
Brian Osman | f06ead9 | 2017-10-30 13:47:41 -0400 | [diff] [blame] | 136 | GrPixelConfig config = proxy->config(); |
Brian Osman | 5e34167 | 2017-10-18 10:23:18 -0400 | [diff] [blame] | 137 | auto fp = CreateFragmentProcessorForDomainAndFilter(std::move(proxy), textureMatrix, |
| 138 | domainMode, domain, filterOrNullForBicubic); |
Brian Osman | f06ead9 | 2017-10-30 13:47:41 -0400 | [diff] [blame] | 139 | return GrColorSpaceXformEffect::Make(std::move(fp), fColorSpace, config, dstColorSpace); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 140 | } |