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 "GrTextureAdjuster.h" |
| 9 | |
| 10 | #include "GrContext.h" |
| 11 | #include "GrGpu.h" |
| 12 | #include "GrGpuResourcePriv.h" |
| 13 | #include "GrTexture.h" |
| 14 | #include "SkGrPriv.h" |
| 15 | |
| 16 | GrTextureAdjuster::GrTextureAdjuster(GrTexture* original, SkAlphaType alphaType, |
| 17 | const SkIRect& contentArea, uint32_t uniqueID, |
| 18 | SkColorSpace* cs) |
| 19 | : INHERITED(contentArea.width(), contentArea.height(), |
| 20 | GrPixelConfigIsAlphaOnly(original->config())) |
| 21 | , fOriginal(original) |
| 22 | , fAlphaType(alphaType) |
| 23 | , fColorSpace(cs) |
| 24 | , fUniqueID(uniqueID) { |
| 25 | SkASSERT(SkIRect::MakeWH(original->width(), original->height()).contains(contentArea)); |
| 26 | if (contentArea.fLeft > 0 || contentArea.fTop > 0 || |
| 27 | contentArea.fRight < original->width() || contentArea.fBottom < original->height()) { |
| 28 | fContentArea.set(contentArea); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | void GrTextureAdjuster::makeCopyKey(const CopyParams& params, GrUniqueKey* copyKey, |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 33 | SkColorSpace* dstColorSpace) { |
| 34 | // Destination color space is irrelevant - we already have a texture so we're just sub-setting |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 35 | GrUniqueKey baseKey; |
| 36 | GrMakeKeyFromImageID(&baseKey, fUniqueID, SkIRect::MakeWH(this->width(), this->height())); |
| 37 | MakeCopyKeyFromOrigKey(baseKey, params, copyKey); |
| 38 | } |
| 39 | |
| 40 | void GrTextureAdjuster::didCacheCopy(const GrUniqueKey& copyKey) { |
| 41 | // We don't currently have a mechanism for notifications on Images! |
| 42 | } |
| 43 | |
| 44 | GrTexture* GrTextureAdjuster::refCopy(const CopyParams& copyParams) { |
| 45 | GrTexture* texture = this->originalTexture(); |
| 46 | GrContext* context = texture->getContext(); |
| 47 | const SkIRect* contentArea = this->contentAreaOrNull(); |
| 48 | GrUniqueKey key; |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 49 | this->makeCopyKey(copyParams, &key, nullptr); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 50 | if (key.isValid()) { |
| 51 | GrTexture* cachedCopy = context->textureProvider()->findAndRefTextureByUniqueKey(key); |
| 52 | if (cachedCopy) { |
| 53 | return cachedCopy; |
| 54 | } |
| 55 | } |
| 56 | GrTexture* copy = CopyOnGpu(texture, contentArea, copyParams); |
| 57 | if (copy) { |
| 58 | if (key.isValid()) { |
| 59 | copy->resourcePriv().setUniqueKey(key); |
| 60 | this->didCacheCopy(key); |
| 61 | } |
| 62 | } |
| 63 | return copy; |
| 64 | } |
| 65 | |
| 66 | GrTexture* GrTextureAdjuster::refTextureSafeForParams(const GrSamplerParams& params, |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 67 | SkIPoint* outOffset, |
| 68 | SkScalar scaleAdjust[2]) { |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 69 | GrTexture* texture = this->originalTexture(); |
| 70 | GrContext* context = texture->getContext(); |
| 71 | CopyParams copyParams; |
| 72 | const SkIRect* contentArea = this->contentAreaOrNull(); |
| 73 | |
| 74 | if (!context) { |
| 75 | // The texture was abandoned. |
| 76 | return nullptr; |
| 77 | } |
| 78 | |
| 79 | if (contentArea && GrSamplerParams::kMipMap_FilterMode == params.filterMode()) { |
| 80 | // If we generate a MIP chain for texture it will read pixel values from outside the content |
| 81 | // area. |
| 82 | copyParams.fWidth = contentArea->width(); |
| 83 | copyParams.fHeight = contentArea->height(); |
| 84 | copyParams.fFilter = GrSamplerParams::kBilerp_FilterMode; |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 85 | } else if (!context->getGpu()->makeCopyForTextureParams(texture, params, ©Params, |
| 86 | scaleAdjust)) { |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 87 | if (outOffset) { |
| 88 | if (contentArea) { |
| 89 | outOffset->set(contentArea->fLeft, contentArea->fRight); |
| 90 | } else { |
| 91 | outOffset->set(0, 0); |
| 92 | } |
| 93 | } |
| 94 | return SkRef(texture); |
| 95 | } |
| 96 | |
| 97 | GrTexture* copy = this->refCopy(copyParams); |
| 98 | if (copy && outOffset) { |
| 99 | outOffset->set(0, 0); |
| 100 | } |
| 101 | return copy; |
| 102 | } |
| 103 | |
| 104 | sk_sp<GrFragmentProcessor> GrTextureAdjuster::createFragmentProcessor( |
| 105 | const SkMatrix& origTextureMatrix, |
| 106 | const SkRect& origConstraintRect, |
| 107 | FilterConstraint filterConstraint, |
| 108 | bool coordsLimitedToConstraintRect, |
| 109 | const GrSamplerParams::FilterMode* filterOrNullForBicubic, |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 110 | SkColorSpace* dstColorSpace) { |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 111 | |
| 112 | SkMatrix textureMatrix = origTextureMatrix; |
| 113 | const SkIRect* contentArea = this->contentAreaOrNull(); |
| 114 | // Convert the constraintRect to be relative to the texture rather than the content area so |
| 115 | // that both rects are in the same coordinate system. |
| 116 | SkTCopyOnFirstWrite<SkRect> constraintRect(origConstraintRect); |
| 117 | if (contentArea) { |
| 118 | SkScalar l = SkIntToScalar(contentArea->fLeft); |
| 119 | SkScalar t = SkIntToScalar(contentArea->fTop); |
| 120 | constraintRect.writable()->offset(l, t); |
| 121 | textureMatrix.postTranslate(l, t); |
| 122 | } |
| 123 | |
| 124 | SkRect domain; |
| 125 | GrSamplerParams params; |
| 126 | if (filterOrNullForBicubic) { |
| 127 | params.setFilterMode(*filterOrNullForBicubic); |
| 128 | } |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 129 | SkScalar scaleAdjust[2] = { 1.0f, 1.0f }; |
| 130 | sk_sp<GrTexture> texture(this->refTextureSafeForParams(params, nullptr, scaleAdjust)); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 131 | if (!texture) { |
| 132 | return nullptr; |
| 133 | } |
| 134 | // If we made a copy then we only copied the contentArea, in which case the new texture is all |
| 135 | // content. |
| 136 | if (texture.get() != this->originalTexture()) { |
| 137 | contentArea = nullptr; |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 138 | textureMatrix.postScale(scaleAdjust[0], scaleAdjust[1]); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | DomainMode domainMode = |
| 142 | DetermineDomainMode(*constraintRect, filterConstraint, coordsLimitedToConstraintRect, |
| 143 | texture->width(), texture->height(), |
| 144 | contentArea, filterOrNullForBicubic, |
| 145 | &domain); |
| 146 | if (kTightCopy_DomainMode == domainMode) { |
| 147 | // TODO: Copy the texture and adjust the texture matrix (both parts need to consider |
| 148 | // non-int constraint rect) |
| 149 | // For now: treat as bilerp and ignore what goes on above level 0. |
| 150 | |
| 151 | // We only expect MIP maps to require a tight copy. |
| 152 | SkASSERT(filterOrNullForBicubic && |
| 153 | GrSamplerParams::kMipMap_FilterMode == *filterOrNullForBicubic); |
| 154 | static const GrSamplerParams::FilterMode kBilerp = GrSamplerParams::kBilerp_FilterMode; |
| 155 | domainMode = |
| 156 | DetermineDomainMode(*constraintRect, filterConstraint, coordsLimitedToConstraintRect, |
| 157 | texture->width(), texture->height(), |
| 158 | contentArea, &kBilerp, &domain); |
| 159 | SkASSERT(kTightCopy_DomainMode != domainMode); |
| 160 | } |
| 161 | SkASSERT(kNoDomain_DomainMode == domainMode || |
| 162 | (domain.fLeft <= domain.fRight && domain.fTop <= domain.fBottom)); |
Brian Osman | e8e5458 | 2016-11-28 10:06:27 -0500 | [diff] [blame] | 163 | sk_sp<GrColorSpaceXform> colorSpaceXform = GrColorSpaceXform::Make(fColorSpace, |
| 164 | dstColorSpace); |
| 165 | return CreateFragmentProcessorForDomainAndFilter(texture.get(), std::move(colorSpaceXform), |
| 166 | textureMatrix, domainMode, domain, |
| 167 | filterOrNullForBicubic); |
| 168 | } |