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