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