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 | |
Robert Phillips | 3798c86 | 2017-03-27 11:08:16 -0400 | [diff] [blame] | 14 | sk_sp<GrTextureProxy> GrTextureMaker::refTextureProxyForParams(const GrSamplerParams& params, |
| 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; |
| 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 | |
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) { |
| 62 | result = CopyOnGpu(fContext, std::move(original), nullptr, copyParams); |
| 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 | |
| 79 | sk_sp<GrFragmentProcessor> GrTextureMaker::createFragmentProcessor( |
| 80 | const SkMatrix& textureMatrix, |
| 81 | const SkRect& constraintRect, |
| 82 | FilterConstraint filterConstraint, |
| 83 | bool coordsLimitedToConstraintRect, |
| 84 | const GrSamplerParams::FilterMode* filterOrNullForBicubic, |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 85 | SkColorSpace* dstColorSpace) { |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 86 | |
| 87 | const GrSamplerParams::FilterMode* fmForDetermineDomain = filterOrNullForBicubic; |
| 88 | if (filterOrNullForBicubic && GrSamplerParams::kMipMap_FilterMode == *filterOrNullForBicubic && |
| 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. |
| 95 | static const GrSamplerParams::FilterMode kBilerp = GrSamplerParams::kBilerp_FilterMode; |
| 96 | fmForDetermineDomain = &kBilerp; |
| 97 | } |
| 98 | |
| 99 | GrSamplerParams params; |
| 100 | if (filterOrNullForBicubic) { |
| 101 | params.reset(SkShader::kClamp_TileMode, *filterOrNullForBicubic); |
| 102 | } else { |
| 103 | // Bicubic doesn't use filtering for it's texture accesses. |
| 104 | params.reset(SkShader::kClamp_TileMode, GrSamplerParams::kNone_FilterMode); |
| 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 }; |
Robert Phillips | 3798c86 | 2017-03-27 11:08:16 -0400 | [diff] [blame] | 108 | sk_sp<GrTextureProxy> proxy(this->refTextureProxyForParams(params, dstColorSpace, |
| 109 | &texColorSpace, |
| 110 | scaleAdjust)); |
| 111 | if (!proxy) { |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 112 | return nullptr; |
| 113 | } |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 114 | SkMatrix adjustedMatrix = textureMatrix; |
| 115 | adjustedMatrix.postScale(scaleAdjust[0], scaleAdjust[1]); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 116 | SkRect domain; |
| 117 | DomainMode domainMode = |
| 118 | DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect, |
Robert Phillips | 3798c86 | 2017-03-27 11:08:16 -0400 | [diff] [blame] | 119 | proxy.get(), |
Robert Phillips | e98234f | 2017-01-09 14:23:59 -0500 | [diff] [blame] | 120 | nullptr, fmForDetermineDomain, &domain); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 121 | SkASSERT(kTightCopy_DomainMode != domainMode); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 122 | sk_sp<GrColorSpaceXform> colorSpaceXform = GrColorSpaceXform::Make(texColorSpace.get(), |
| 123 | dstColorSpace); |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 124 | return CreateFragmentProcessorForDomainAndFilter(std::move(proxy), |
Robert Phillips | 3798c86 | 2017-03-27 11:08:16 -0400 | [diff] [blame] | 125 | std::move(colorSpaceXform), |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 126 | adjustedMatrix, domainMode, domain, |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 127 | filterOrNullForBicubic); |
| 128 | } |
| 129 | |
Robert Phillips | 3798c86 | 2017-03-27 11:08:16 -0400 | [diff] [blame] | 130 | sk_sp<GrTextureProxy> GrTextureMaker::generateTextureProxyForParams(const CopyParams& copyParams, |
| 131 | bool willBeMipped, |
| 132 | SkColorSpace* dstColorSpace) { |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 133 | sk_sp<GrTextureProxy> original(this->refOriginalTextureProxy(willBeMipped, dstColorSpace, |
| 134 | AllowedTexGenType::kAny)); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 135 | if (!original) { |
| 136 | return nullptr; |
| 137 | } |
Robert Phillips | 4f358be | 2017-03-23 08:21:00 -0400 | [diff] [blame] | 138 | |
Robert Phillips | 3798c86 | 2017-03-27 11:08:16 -0400 | [diff] [blame] | 139 | return CopyOnGpu(fContext, std::move(original), nullptr, copyParams); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 140 | } |