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 "GrTextureMaker.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" |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 13 | #include "GrGpu.h" |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 14 | #include "GrProxyProvider.h" |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 15 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 16 | sk_sp<GrTextureProxy> GrTextureMaker::refTextureProxyForParams(const GrSamplerState& params, |
Robert Phillips | 3798c86 | 2017-03-27 11:08:16 -0400 | [diff] [blame] | 17 | SkColorSpace* dstColorSpace, |
| 18 | sk_sp<SkColorSpace>* texColorSpace, |
| 19 | SkScalar scaleAdjust[2]) { |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 20 | CopyParams copyParams; |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 21 | bool willBeMipped = params.filter() == GrSamplerState::Filter::kMipMap; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 22 | |
| 23 | if (!fContext->caps()->mipMapSupport()) { |
| 24 | willBeMipped = false; |
| 25 | } |
| 26 | |
| 27 | if (texColorSpace) { |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 28 | *texColorSpace = this->getColorSpace(dstColorSpace); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 29 | } |
| 30 | |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 31 | GrGpu* gpu = fContext->contextPriv().getGpu(); |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 32 | sk_sp<GrTextureProxy> original(this->refOriginalTextureProxy(willBeMipped, dstColorSpace, |
| 33 | AllowedTexGenType::kCheap)); |
| 34 | if (original) { |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 35 | if (!gpu->isACopyNeededForTextureParams(original.get(), params, ©Params, scaleAdjust)) { |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 36 | return original; |
| 37 | } |
| 38 | } else { |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 39 | if (!gpu->isACopyNeededForTextureParams(this->width(), this->height(), |
| 40 | params, ©Params, scaleAdjust)) { |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 41 | return this->refOriginalTextureProxy(willBeMipped, dstColorSpace, |
| 42 | AllowedTexGenType::kAny); |
| 43 | } |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 44 | } |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 45 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 46 | GrProxyProvider* proxyProvider = fContext->contextPriv().proxyProvider(); |
| 47 | |
| 48 | GrSurfaceOrigin origOrigin = original ? original->origin() : kTopLeft_GrSurfaceOrigin; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 49 | GrUniqueKey copyKey; |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 50 | this->makeCopyKey(copyParams, ©Key, dstColorSpace); |
Greg Daniel | 2d59d2f | 2017-10-31 15:25:14 -0400 | [diff] [blame] | 51 | sk_sp<GrTextureProxy> cachedProxy; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 52 | if (copyKey.isValid()) { |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 53 | cachedProxy = proxyProvider->findOrCreateProxyByUniqueKey(copyKey, origOrigin); |
Greg Daniel | 2d59d2f | 2017-10-31 15:25:14 -0400 | [diff] [blame] | 54 | if (cachedProxy && (!willBeMipped || GrMipMapped::kYes == cachedProxy->mipMapped())) { |
| 55 | return cachedProxy; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 59 | sk_sp<GrTextureProxy> result; |
| 60 | if (original) { |
Greg Daniel | 2d59d2f | 2017-10-31 15:25:14 -0400 | [diff] [blame] | 61 | result = std::move(original); |
| 62 | } else if (cachedProxy) { |
| 63 | result = cachedProxy; |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 64 | } else { |
Greg Daniel | 2d59d2f | 2017-10-31 15:25:14 -0400 | [diff] [blame] | 65 | // Since we will be copying this texture there is no reason to make it mipped |
| 66 | result = this->refOriginalTextureProxy(false, dstColorSpace, |
| 67 | AllowedTexGenType::kAny); |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 68 | } |
Greg Daniel | 2d59d2f | 2017-10-31 15:25:14 -0400 | [diff] [blame] | 69 | if (!result) { |
| 70 | return nullptr; |
| 71 | } |
| 72 | |
Greg Daniel | c77085d | 2017-11-01 16:38:48 -0400 | [diff] [blame] | 73 | result = CopyOnGpu(fContext, std::move(result), copyParams, willBeMipped); |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 74 | |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 75 | if (!result) { |
| 76 | return nullptr; |
| 77 | } |
| 78 | |
| 79 | if (copyKey.isValid()) { |
Robert Phillips | d3e247f | 2017-07-25 08:00:17 -0400 | [diff] [blame] | 80 | SkASSERT(result->origin() == origOrigin); |
Greg Daniel | 2d59d2f | 2017-10-31 15:25:14 -0400 | [diff] [blame] | 81 | if (cachedProxy) { |
| 82 | SkASSERT(GrMipMapped::kYes == result->mipMapped() && |
| 83 | GrMipMapped::kNo == cachedProxy->mipMapped()); |
| 84 | // If we had a cachedProxy, that means there already is a proxy in the cache which |
| 85 | // matches the key, but it does not have mip levels and we require them. Thus we must |
| 86 | // remove the unique key from that proxy. |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 87 | proxyProvider->removeUniqueKeyFromProxy(copyKey, cachedProxy.get()); |
Greg Daniel | 2d59d2f | 2017-10-31 15:25:14 -0400 | [diff] [blame] | 88 | } |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 89 | proxyProvider->assignUniqueKeyToProxy(copyKey, result.get()); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 90 | this->didCacheCopy(copyKey); |
| 91 | } |
| 92 | return result; |
| 93 | } |
| 94 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 95 | std::unique_ptr<GrFragmentProcessor> GrTextureMaker::createFragmentProcessor( |
| 96 | const SkMatrix& textureMatrix, |
| 97 | const SkRect& constraintRect, |
| 98 | FilterConstraint filterConstraint, |
| 99 | bool coordsLimitedToConstraintRect, |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 100 | const GrSamplerState::Filter* filterOrNullForBicubic, |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 101 | SkColorSpace* dstColorSpace) { |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 102 | const GrSamplerState::Filter* fmForDetermineDomain = filterOrNullForBicubic; |
| 103 | if (filterOrNullForBicubic && GrSamplerState::Filter::kMipMap == *filterOrNullForBicubic && |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 104 | kYes_FilterConstraint == filterConstraint) { |
| 105 | // TODo: Here we should force a copy restricted to the constraintRect since MIP maps will |
| 106 | // read outside the constraint rect. However, as in the adjuster case, we aren't currently |
| 107 | // doing that. |
| 108 | // We instead we compute the domain as though were bilerping which is only correct if we |
| 109 | // only sample level 0. |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 110 | static const GrSamplerState::Filter kBilerp = GrSamplerState::Filter::kBilerp; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 111 | fmForDetermineDomain = &kBilerp; |
| 112 | } |
| 113 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 114 | GrSamplerState samplerState; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 115 | if (filterOrNullForBicubic) { |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 116 | samplerState = GrSamplerState(GrSamplerState::WrapMode::kClamp, *filterOrNullForBicubic); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 117 | } else { |
| 118 | // Bicubic doesn't use filtering for it's texture accesses. |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 119 | samplerState = GrSamplerState::ClampNearest(); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 120 | } |
| 121 | sk_sp<SkColorSpace> texColorSpace; |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 122 | SkScalar scaleAdjust[2] = { 1.0f, 1.0f }; |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 123 | sk_sp<GrTextureProxy> proxy(this->refTextureProxyForParams(samplerState, dstColorSpace, |
| 124 | &texColorSpace, scaleAdjust)); |
Robert Phillips | 3798c86 | 2017-03-27 11:08:16 -0400 | [diff] [blame] | 125 | if (!proxy) { |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 126 | return nullptr; |
| 127 | } |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 128 | SkMatrix adjustedMatrix = textureMatrix; |
| 129 | adjustedMatrix.postScale(scaleAdjust[0], scaleAdjust[1]); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 130 | SkRect domain; |
| 131 | DomainMode domainMode = |
Brian Salomon | 4df0092 | 2017-09-07 16:34:11 +0000 | [diff] [blame] | 132 | DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect, |
Greg Daniel | c77085d | 2017-11-01 16:38:48 -0400 | [diff] [blame] | 133 | proxy.get(), fmForDetermineDomain, &domain); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 134 | SkASSERT(kTightCopy_DomainMode != domainMode); |
Brian Osman | f06ead9 | 2017-10-30 13:47:41 -0400 | [diff] [blame] | 135 | GrPixelConfig config = proxy->config(); |
Brian Osman | 5e34167 | 2017-10-18 10:23:18 -0400 | [diff] [blame] | 136 | auto fp = CreateFragmentProcessorForDomainAndFilter(std::move(proxy), adjustedMatrix, |
| 137 | domainMode, domain, filterOrNullForBicubic); |
Brian Osman | f06ead9 | 2017-10-30 13:47:41 -0400 | [diff] [blame] | 138 | return GrColorSpaceXformEffect::Make(std::move(fp), texColorSpace.get(), config, dstColorSpace); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 139 | } |
| 140 | |
Robert Phillips | 3798c86 | 2017-03-27 11:08:16 -0400 | [diff] [blame] | 141 | sk_sp<GrTextureProxy> GrTextureMaker::generateTextureProxyForParams(const CopyParams& copyParams, |
| 142 | bool willBeMipped, |
| 143 | SkColorSpace* dstColorSpace) { |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 144 | sk_sp<GrTextureProxy> original(this->refOriginalTextureProxy(willBeMipped, dstColorSpace, |
| 145 | AllowedTexGenType::kAny)); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 146 | if (!original) { |
| 147 | return nullptr; |
| 148 | } |
Robert Phillips | 4f358be | 2017-03-23 08:21:00 -0400 | [diff] [blame] | 149 | |
Greg Daniel | c77085d | 2017-11-01 16:38:48 -0400 | [diff] [blame] | 150 | return CopyOnGpu(fContext, std::move(original), copyParams, willBeMipped); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 151 | } |