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 | |
Robert Phillips | 066f020 | 2017-07-25 10:16:35 -0400 | [diff] [blame] | 45 | GrSurfaceOrigin origOrigin; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 46 | GrUniqueKey copyKey; |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 47 | this->makeCopyKey(copyParams, ©Key, dstColorSpace); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 48 | if (copyKey.isValid()) { |
Robert Phillips | d3e247f | 2017-07-25 08:00:17 -0400 | [diff] [blame] | 49 | if (original) { |
| 50 | origOrigin = original->origin(); |
| 51 | } else { |
| 52 | origOrigin = kTopLeft_GrSurfaceOrigin; |
| 53 | } |
Greg Daniel | cd87140 | 2017-09-26 12:49:26 -0400 | [diff] [blame] | 54 | sk_sp<GrTextureProxy> result(fContext->resourceProvider()->findOrCreateProxyByUniqueKey( |
Robert Phillips | 066f020 | 2017-07-25 10:16:35 -0400 | [diff] [blame] | 55 | copyKey, origOrigin)); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 56 | if (result) { |
| 57 | return result; |
| 58 | } |
| 59 | } |
| 60 | |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 61 | sk_sp<GrTextureProxy> result; |
| 62 | if (original) { |
Greg Daniel | e1da1d9 | 2017-10-06 15:59:27 -0400 | [diff] [blame] | 63 | result = CopyOnGpu(fContext, std::move(original), nullptr, copyParams, willBeMipped); |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 64 | } else { |
| 65 | result = this->generateTextureProxyForParams(copyParams, willBeMipped, dstColorSpace); |
| 66 | } |
| 67 | |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 68 | if (!result) { |
| 69 | return nullptr; |
| 70 | } |
| 71 | |
| 72 | if (copyKey.isValid()) { |
Robert Phillips | d3e247f | 2017-07-25 08:00:17 -0400 | [diff] [blame] | 73 | SkASSERT(result->origin() == origOrigin); |
Robert Phillips | 3798c86 | 2017-03-27 11:08:16 -0400 | [diff] [blame] | 74 | fContext->resourceProvider()->assignUniqueKeyToProxy(copyKey, result.get()); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 75 | this->didCacheCopy(copyKey); |
| 76 | } |
| 77 | return result; |
| 78 | } |
| 79 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 80 | std::unique_ptr<GrFragmentProcessor> GrTextureMaker::createFragmentProcessor( |
| 81 | const SkMatrix& textureMatrix, |
| 82 | const SkRect& constraintRect, |
| 83 | FilterConstraint filterConstraint, |
| 84 | bool coordsLimitedToConstraintRect, |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 85 | const GrSamplerState::Filter* filterOrNullForBicubic, |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 86 | SkColorSpace* dstColorSpace) { |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 87 | const GrSamplerState::Filter* fmForDetermineDomain = filterOrNullForBicubic; |
| 88 | if (filterOrNullForBicubic && GrSamplerState::Filter::kMipMap == *filterOrNullForBicubic && |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 89 | kYes_FilterConstraint == filterConstraint) { |
| 90 | // TODo: Here we should force a copy restricted to the constraintRect since MIP maps will |
| 91 | // read outside the constraint rect. However, as in the adjuster case, we aren't currently |
| 92 | // doing that. |
| 93 | // We instead we compute the domain as though were bilerping which is only correct if we |
| 94 | // only sample level 0. |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 95 | static const GrSamplerState::Filter kBilerp = GrSamplerState::Filter::kBilerp; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 96 | fmForDetermineDomain = &kBilerp; |
| 97 | } |
| 98 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 99 | GrSamplerState samplerState; |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 100 | if (filterOrNullForBicubic) { |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 101 | samplerState = GrSamplerState(GrSamplerState::WrapMode::kClamp, *filterOrNullForBicubic); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 102 | } else { |
| 103 | // Bicubic doesn't use filtering for it's texture accesses. |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 104 | samplerState = GrSamplerState::ClampNearest(); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 105 | } |
| 106 | sk_sp<SkColorSpace> texColorSpace; |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 107 | SkScalar scaleAdjust[2] = { 1.0f, 1.0f }; |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 108 | sk_sp<GrTextureProxy> proxy(this->refTextureProxyForParams(samplerState, dstColorSpace, |
| 109 | &texColorSpace, scaleAdjust)); |
Robert Phillips | 3798c86 | 2017-03-27 11:08:16 -0400 | [diff] [blame] | 110 | if (!proxy) { |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 111 | return nullptr; |
| 112 | } |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 113 | SkMatrix adjustedMatrix = textureMatrix; |
| 114 | adjustedMatrix.postScale(scaleAdjust[0], scaleAdjust[1]); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 115 | SkRect domain; |
| 116 | DomainMode domainMode = |
Brian Salomon | 4df0092 | 2017-09-07 16:34:11 +0000 | [diff] [blame] | 117 | DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect, |
| 118 | proxy.get(), |
| 119 | nullptr, fmForDetermineDomain, &domain); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 120 | SkASSERT(kTightCopy_DomainMode != domainMode); |
Brian Osman | 5e34167 | 2017-10-18 10:23:18 -0400 | [diff] [blame] | 121 | auto fp = CreateFragmentProcessorForDomainAndFilter(std::move(proxy), adjustedMatrix, |
| 122 | domainMode, domain, filterOrNullForBicubic); |
| 123 | return GrColorSpaceXformEffect::Make(std::move(fp), texColorSpace.get(), dstColorSpace); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 124 | } |
| 125 | |
Robert Phillips | 3798c86 | 2017-03-27 11:08:16 -0400 | [diff] [blame] | 126 | sk_sp<GrTextureProxy> GrTextureMaker::generateTextureProxyForParams(const CopyParams& copyParams, |
| 127 | bool willBeMipped, |
| 128 | SkColorSpace* dstColorSpace) { |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 129 | sk_sp<GrTextureProxy> original(this->refOriginalTextureProxy(willBeMipped, dstColorSpace, |
| 130 | AllowedTexGenType::kAny)); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 131 | if (!original) { |
| 132 | return nullptr; |
| 133 | } |
Robert Phillips | 4f358be | 2017-03-23 08:21:00 -0400 | [diff] [blame] | 134 | |
Greg Daniel | e1da1d9 | 2017-10-06 15:59:27 -0400 | [diff] [blame] | 135 | return CopyOnGpu(fContext, std::move(original), nullptr, copyParams, willBeMipped); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 136 | } |