| 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 | d3e247f | 2017-07-25 08:00:17 -0400 | [diff] [blame^] | 44 | SkDEBUGCODE(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 | #ifdef SK_DEBUG | 
|  | 49 | if (original) { | 
|  | 50 | origOrigin = original->origin(); | 
|  | 51 | } else { | 
|  | 52 | origOrigin = kTopLeft_GrSurfaceOrigin; | 
|  | 53 | } | 
|  | 54 | #endif | 
| Robert Phillips | 96be9df | 2017-07-21 14:17:45 +0000 | [diff] [blame] | 55 | sk_sp<GrTextureProxy> result(fContext->resourceProvider()->findProxyByUniqueKey(copyKey)); | 
| Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 56 | if (result) { | 
|  | 57 | return result; | 
|  | 58 | } | 
|  | 59 | } | 
|  | 60 |  | 
| Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 61 | sk_sp<GrTextureProxy> result; | 
|  | 62 | if (original) { | 
|  | 63 | result = CopyOnGpu(fContext, std::move(original), nullptr, copyParams); | 
|  | 64 | } else { | 
|  | 65 | result = this->generateTextureProxyForParams(copyParams, willBeMipped, dstColorSpace); | 
|  | 66 | } | 
|  | 67 |  | 
| Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 68 | if (!result) { | 
|  | 69 | return nullptr; | 
|  | 70 | } | 
|  | 71 |  | 
|  | 72 | if (copyKey.isValid()) { | 
| Robert Phillips | d3e247f | 2017-07-25 08:00:17 -0400 | [diff] [blame^] | 73 | SkASSERT(result->origin() == origOrigin); | 
| Robert Phillips | 3798c86 | 2017-03-27 11:08:16 -0400 | [diff] [blame] | 74 | fContext->resourceProvider()->assignUniqueKeyToProxy(copyKey, result.get()); | 
| Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 75 | this->didCacheCopy(copyKey); | 
|  | 76 | } | 
|  | 77 | return result; | 
|  | 78 | } | 
|  | 79 |  | 
|  | 80 | sk_sp<GrFragmentProcessor> GrTextureMaker::createFragmentProcessor( | 
|  | 81 | const SkMatrix& textureMatrix, | 
|  | 82 | const SkRect& constraintRect, | 
|  | 83 | FilterConstraint filterConstraint, | 
|  | 84 | bool coordsLimitedToConstraintRect, | 
|  | 85 | const GrSamplerParams::FilterMode* filterOrNullForBicubic, | 
| Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 86 | SkColorSpace* dstColorSpace) { | 
| Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 87 |  | 
|  | 88 | const GrSamplerParams::FilterMode* fmForDetermineDomain = filterOrNullForBicubic; | 
|  | 89 | if (filterOrNullForBicubic && GrSamplerParams::kMipMap_FilterMode == *filterOrNullForBicubic && | 
|  | 90 | kYes_FilterConstraint == filterConstraint) { | 
|  | 91 | // TODo: Here we should force a copy restricted to the constraintRect since MIP maps will | 
|  | 92 | // read outside the constraint rect. However, as in the adjuster case, we aren't currently | 
|  | 93 | // doing that. | 
|  | 94 | // We instead we compute the domain as though were bilerping which is only correct if we | 
|  | 95 | // only sample level 0. | 
|  | 96 | static const GrSamplerParams::FilterMode kBilerp = GrSamplerParams::kBilerp_FilterMode; | 
|  | 97 | fmForDetermineDomain = &kBilerp; | 
|  | 98 | } | 
|  | 99 |  | 
|  | 100 | GrSamplerParams params; | 
|  | 101 | if (filterOrNullForBicubic) { | 
|  | 102 | params.reset(SkShader::kClamp_TileMode, *filterOrNullForBicubic); | 
|  | 103 | } else { | 
|  | 104 | // Bicubic doesn't use filtering for it's texture accesses. | 
|  | 105 | params.reset(SkShader::kClamp_TileMode, GrSamplerParams::kNone_FilterMode); | 
|  | 106 | } | 
|  | 107 | sk_sp<SkColorSpace> texColorSpace; | 
| Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 108 | SkScalar scaleAdjust[2] = { 1.0f, 1.0f }; | 
| Robert Phillips | 3798c86 | 2017-03-27 11:08:16 -0400 | [diff] [blame] | 109 | sk_sp<GrTextureProxy> proxy(this->refTextureProxyForParams(params, dstColorSpace, | 
|  | 110 | &texColorSpace, | 
|  | 111 | scaleAdjust)); | 
|  | 112 | if (!proxy) { | 
| Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 113 | return nullptr; | 
|  | 114 | } | 
| Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 115 | SkMatrix adjustedMatrix = textureMatrix; | 
|  | 116 | adjustedMatrix.postScale(scaleAdjust[0], scaleAdjust[1]); | 
| Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 117 | SkRect domain; | 
|  | 118 | DomainMode domainMode = | 
|  | 119 | DetermineDomainMode(constraintRect, filterConstraint, coordsLimitedToConstraintRect, | 
| Robert Phillips | 3798c86 | 2017-03-27 11:08:16 -0400 | [diff] [blame] | 120 | proxy.get(), | 
| Robert Phillips | e98234f | 2017-01-09 14:23:59 -0500 | [diff] [blame] | 121 | nullptr, fmForDetermineDomain, &domain); | 
| Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 122 | SkASSERT(kTightCopy_DomainMode != domainMode); | 
| Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 123 | sk_sp<GrColorSpaceXform> colorSpaceXform = GrColorSpaceXform::Make(texColorSpace.get(), | 
|  | 124 | dstColorSpace); | 
| Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 125 | return CreateFragmentProcessorForDomainAndFilter(std::move(proxy), | 
| Robert Phillips | 3798c86 | 2017-03-27 11:08:16 -0400 | [diff] [blame] | 126 | std::move(colorSpaceXform), | 
| Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 127 | adjustedMatrix, domainMode, domain, | 
| Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 128 | filterOrNullForBicubic); | 
|  | 129 | } | 
|  | 130 |  | 
| Robert Phillips | 3798c86 | 2017-03-27 11:08:16 -0400 | [diff] [blame] | 131 | sk_sp<GrTextureProxy> GrTextureMaker::generateTextureProxyForParams(const CopyParams& copyParams, | 
|  | 132 | bool willBeMipped, | 
|  | 133 | SkColorSpace* dstColorSpace) { | 
| Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 134 | sk_sp<GrTextureProxy> original(this->refOriginalTextureProxy(willBeMipped, dstColorSpace, | 
|  | 135 | AllowedTexGenType::kAny)); | 
| Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 136 | if (!original) { | 
|  | 137 | return nullptr; | 
|  | 138 | } | 
| Robert Phillips | 4f358be | 2017-03-23 08:21:00 -0400 | [diff] [blame] | 139 |  | 
| Robert Phillips | 3798c86 | 2017-03-27 11:08:16 -0400 | [diff] [blame] | 140 | return CopyOnGpu(fContext, std::move(original), nullptr, copyParams); | 
| Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 141 | } |