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