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