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