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 | |
| 14 | GrTexture* GrTextureMaker::refTextureForParams(const GrSamplerParams& params, |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 15 | SkColorSpace* dstColorSpace, |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 16 | sk_sp<SkColorSpace>* texColorSpace, |
| 17 | SkScalar scaleAdjust[2]) { |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 18 | CopyParams copyParams; |
| 19 | bool willBeMipped = params.filterMode() == GrSamplerParams::kMipMap_FilterMode; |
| 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 | |
| 29 | if (!fContext->getGpu()->makeCopyForTextureParams(this->width(), this->height(), params, |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 30 | ©Params, scaleAdjust)) { |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 31 | return this->refOriginalTexture(willBeMipped, dstColorSpace); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 32 | } |
| 33 | GrUniqueKey copyKey; |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 34 | this->makeCopyKey(copyParams, ©Key, dstColorSpace); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 35 | if (copyKey.isValid()) { |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame^] | 36 | GrTexture* result = fContext->resourceProvider()->findAndRefTextureByUniqueKey(copyKey); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 37 | if (result) { |
| 38 | return result; |
| 39 | } |
| 40 | } |
| 41 | |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 42 | GrTexture* result = this->generateTextureForParams(copyParams, willBeMipped, dstColorSpace); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 43 | if (!result) { |
| 44 | return nullptr; |
| 45 | } |
| 46 | |
| 47 | if (copyKey.isValid()) { |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame^] | 48 | fContext->resourceProvider()->assignUniqueKeyToTexture(copyKey, result); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 49 | this->didCacheCopy(copyKey); |
| 50 | } |
| 51 | return result; |
| 52 | } |
| 53 | |
| 54 | sk_sp<GrFragmentProcessor> GrTextureMaker::createFragmentProcessor( |
| 55 | const SkMatrix& textureMatrix, |
| 56 | const SkRect& constraintRect, |
| 57 | FilterConstraint filterConstraint, |
| 58 | bool coordsLimitedToConstraintRect, |
| 59 | const GrSamplerParams::FilterMode* filterOrNullForBicubic, |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 60 | SkColorSpace* dstColorSpace) { |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 61 | |
| 62 | const GrSamplerParams::FilterMode* fmForDetermineDomain = filterOrNullForBicubic; |
| 63 | if (filterOrNullForBicubic && GrSamplerParams::kMipMap_FilterMode == *filterOrNullForBicubic && |
| 64 | kYes_FilterConstraint == filterConstraint) { |
| 65 | // TODo: Here we should force a copy restricted to the constraintRect since MIP maps will |
| 66 | // read outside the constraint rect. However, as in the adjuster case, we aren't currently |
| 67 | // doing that. |
| 68 | // We instead we compute the domain as though were bilerping which is only correct if we |
| 69 | // only sample level 0. |
| 70 | static const GrSamplerParams::FilterMode kBilerp = GrSamplerParams::kBilerp_FilterMode; |
| 71 | fmForDetermineDomain = &kBilerp; |
| 72 | } |
| 73 | |
| 74 | GrSamplerParams params; |
| 75 | if (filterOrNullForBicubic) { |
| 76 | params.reset(SkShader::kClamp_TileMode, *filterOrNullForBicubic); |
| 77 | } else { |
| 78 | // Bicubic doesn't use filtering for it's texture accesses. |
| 79 | params.reset(SkShader::kClamp_TileMode, GrSamplerParams::kNone_FilterMode); |
| 80 | } |
| 81 | sk_sp<SkColorSpace> texColorSpace; |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 82 | SkScalar scaleAdjust[2] = { 1.0f, 1.0f }; |
| 83 | sk_sp<GrTexture> texture(this->refTextureForParams(params, dstColorSpace, &texColorSpace, |
| 84 | scaleAdjust)); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 85 | if (!texture) { |
| 86 | return nullptr; |
| 87 | } |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 88 | SkMatrix adjustedMatrix = textureMatrix; |
| 89 | adjustedMatrix.postScale(scaleAdjust[0], scaleAdjust[1]); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 90 | SkRect domain; |
| 91 | DomainMode domainMode = |
| 92 | DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect, |
Robert Phillips | e98234f | 2017-01-09 14:23:59 -0500 | [diff] [blame] | 93 | texture->width(), texture->height(), |
| 94 | nullptr, fmForDetermineDomain, &domain); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 95 | SkASSERT(kTightCopy_DomainMode != domainMode); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 96 | sk_sp<GrColorSpaceXform> colorSpaceXform = GrColorSpaceXform::Make(texColorSpace.get(), |
| 97 | dstColorSpace); |
| 98 | return CreateFragmentProcessorForDomainAndFilter(texture.get(), std::move(colorSpaceXform), |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 99 | adjustedMatrix, domainMode, domain, |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 100 | filterOrNullForBicubic); |
| 101 | } |
| 102 | |
| 103 | GrTexture* GrTextureMaker::generateTextureForParams(const CopyParams& copyParams, bool willBeMipped, |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 104 | SkColorSpace* dstColorSpace) { |
| 105 | sk_sp<GrTexture> original(this->refOriginalTexture(willBeMipped, dstColorSpace)); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 106 | if (!original) { |
| 107 | return nullptr; |
| 108 | } |
| 109 | return CopyOnGpu(original.get(), nullptr, copyParams); |
| 110 | } |