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