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