2 * Copyright 2016 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "GrTextureAdjuster.h"
10 #include "GrContext.h"
12 #include "GrGpuResourcePriv.h"
13 #include "GrTexture.h"
16 GrTextureAdjuster::GrTextureAdjuster(GrTexture* original, SkAlphaType alphaType,
17 const SkIRect& contentArea, uint32_t uniqueID,
19 : INHERITED(contentArea.width(), contentArea.height(),
20 GrPixelConfigIsAlphaOnly(original->config()))
22 , fAlphaType(alphaType)
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);
32 void GrTextureAdjuster::makeCopyKey(const CopyParams& params, GrUniqueKey* copyKey,
33 SkColorSpace* dstColorSpace) {
34 // Destination color space is irrelevant - we already have a texture so we're just sub-setting
36 GrMakeKeyFromImageID(&baseKey, fUniqueID, SkIRect::MakeWH(this->width(), this->height()));
37 MakeCopyKeyFromOrigKey(baseKey, params, copyKey);
40 void GrTextureAdjuster::didCacheCopy(const GrUniqueKey& copyKey) {
41 // We don't currently have a mechanism for notifications on Images!
44 GrTexture* GrTextureAdjuster::refCopy(const CopyParams& copyParams) {
45 GrTexture* texture = this->originalTexture();
46 GrContext* context = texture->getContext();
47 const SkIRect* contentArea = this->contentAreaOrNull();
49 this->makeCopyKey(copyParams, &key, nullptr);
51 GrTexture* cachedCopy = context->textureProvider()->findAndRefTextureByUniqueKey(key);
56 GrTexture* copy = CopyOnGpu(texture, contentArea, copyParams);
59 copy->resourcePriv().setUniqueKey(key);
60 this->didCacheCopy(key);
66 GrTexture* GrTextureAdjuster::refTextureSafeForParams(const GrSamplerParams& params,
67 SkIPoint* outOffset) {
68 GrTexture* texture = this->originalTexture();
69 GrContext* context = texture->getContext();
70 CopyParams copyParams;
71 const SkIRect* contentArea = this->contentAreaOrNull();
74 // The texture was abandoned.
78 if (contentArea && GrSamplerParams::kMipMap_FilterMode == params.filterMode()) {
79 // If we generate a MIP chain for texture it will read pixel values from outside the content
81 copyParams.fWidth = contentArea->width();
82 copyParams.fHeight = contentArea->height();
83 copyParams.fFilter = GrSamplerParams::kBilerp_FilterMode;
84 } else if (!context->getGpu()->makeCopyForTextureParams(texture, params, ©Params)) {
87 outOffset->set(contentArea->fLeft, contentArea->fRight);
92 return SkRef(texture);
95 GrTexture* copy = this->refCopy(copyParams);
96 if (copy && outOffset) {
102 sk_sp<GrFragmentProcessor> GrTextureAdjuster::createFragmentProcessor(
103 const SkMatrix& origTextureMatrix,
104 const SkRect& origConstraintRect,
105 FilterConstraint filterConstraint,
106 bool coordsLimitedToConstraintRect,
107 const GrSamplerParams::FilterMode* filterOrNullForBicubic,
108 SkColorSpace* dstColorSpace) {
110 SkMatrix textureMatrix = origTextureMatrix;
111 const SkIRect* contentArea = this->contentAreaOrNull();
112 // Convert the constraintRect to be relative to the texture rather than the content area so
113 // that both rects are in the same coordinate system.
114 SkTCopyOnFirstWrite<SkRect> constraintRect(origConstraintRect);
116 SkScalar l = SkIntToScalar(contentArea->fLeft);
117 SkScalar t = SkIntToScalar(contentArea->fTop);
118 constraintRect.writable()->offset(l, t);
119 textureMatrix.postTranslate(l, t);
123 GrSamplerParams params;
124 if (filterOrNullForBicubic) {
125 params.setFilterMode(*filterOrNullForBicubic);
127 sk_sp<GrTexture> texture(this->refTextureSafeForParams(params, nullptr));
131 // If we made a copy then we only copied the contentArea, in which case the new texture is all
133 if (texture.get() != this->originalTexture()) {
134 contentArea = nullptr;
137 DomainMode domainMode =
138 DetermineDomainMode(*constraintRect, filterConstraint, coordsLimitedToConstraintRect,
139 texture->width(), texture->height(),
140 contentArea, filterOrNullForBicubic,
142 if (kTightCopy_DomainMode == domainMode) {
143 // TODO: Copy the texture and adjust the texture matrix (both parts need to consider
144 // non-int constraint rect)
145 // For now: treat as bilerp and ignore what goes on above level 0.
147 // We only expect MIP maps to require a tight copy.
148 SkASSERT(filterOrNullForBicubic &&
149 GrSamplerParams::kMipMap_FilterMode == *filterOrNullForBicubic);
150 static const GrSamplerParams::FilterMode kBilerp = GrSamplerParams::kBilerp_FilterMode;
152 DetermineDomainMode(*constraintRect, filterConstraint, coordsLimitedToConstraintRect,
153 texture->width(), texture->height(),
154 contentArea, &kBilerp, &domain);
155 SkASSERT(kTightCopy_DomainMode != domainMode);
157 SkASSERT(kNoDomain_DomainMode == domainMode ||
158 (domain.fLeft <= domain.fRight && domain.fTop <= domain.fBottom));
159 textureMatrix.postIDiv(texture->width(), texture->height());
160 sk_sp<GrColorSpaceXform> colorSpaceXform = GrColorSpaceXform::Make(fColorSpace,
162 return CreateFragmentProcessorForDomainAndFilter(texture.get(), std::move(colorSpaceXform),
163 textureMatrix, domainMode, domain,
164 filterOrNullForBicubic);