blob: 7142ab9baca18aa95058d7d5b188dfea03d582dc [file] [log] [blame]
Brian Osmane8e54582016-11-28 10:06:27 -05001/*
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
16GrTextureAdjuster::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
32void GrTextureAdjuster::makeCopyKey(const CopyParams& params, GrUniqueKey* copyKey,
Brian Osman61624f02016-12-09 14:51:59 -050033 SkColorSpace* dstColorSpace) {
34 // Destination color space is irrelevant - we already have a texture so we're just sub-setting
Brian Osmane8e54582016-11-28 10:06:27 -050035 GrUniqueKey baseKey;
36 GrMakeKeyFromImageID(&baseKey, fUniqueID, SkIRect::MakeWH(this->width(), this->height()));
37 MakeCopyKeyFromOrigKey(baseKey, params, copyKey);
38}
39
40void GrTextureAdjuster::didCacheCopy(const GrUniqueKey& copyKey) {
41 // We don't currently have a mechanism for notifications on Images!
42}
43
44GrTexture* 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 Osman61624f02016-12-09 14:51:59 -050049 this->makeCopyKey(copyParams, &key, nullptr);
Brian Osmane8e54582016-11-28 10:06:27 -050050 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
66GrTexture* GrTextureAdjuster::refTextureSafeForParams(const GrSamplerParams& params,
Brian Osmane8e54582016-11-28 10:06:27 -050067 SkIPoint* outOffset) {
68 GrTexture* texture = this->originalTexture();
69 GrContext* context = texture->getContext();
70 CopyParams copyParams;
71 const SkIRect* contentArea = this->contentAreaOrNull();
72
73 if (!context) {
74 // The texture was abandoned.
75 return nullptr;
76 }
77
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
80 // area.
81 copyParams.fWidth = contentArea->width();
82 copyParams.fHeight = contentArea->height();
83 copyParams.fFilter = GrSamplerParams::kBilerp_FilterMode;
84 } else if (!context->getGpu()->makeCopyForTextureParams(texture, params, &copyParams)) {
85 if (outOffset) {
86 if (contentArea) {
87 outOffset->set(contentArea->fLeft, contentArea->fRight);
88 } else {
89 outOffset->set(0, 0);
90 }
91 }
92 return SkRef(texture);
93 }
94
95 GrTexture* copy = this->refCopy(copyParams);
96 if (copy && outOffset) {
97 outOffset->set(0, 0);
98 }
99 return copy;
100}
101
102sk_sp<GrFragmentProcessor> GrTextureAdjuster::createFragmentProcessor(
103 const SkMatrix& origTextureMatrix,
104 const SkRect& origConstraintRect,
105 FilterConstraint filterConstraint,
106 bool coordsLimitedToConstraintRect,
107 const GrSamplerParams::FilterMode* filterOrNullForBicubic,
Brian Osman61624f02016-12-09 14:51:59 -0500108 SkColorSpace* dstColorSpace) {
Brian Osmane8e54582016-11-28 10:06:27 -0500109
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);
115 if (contentArea) {
116 SkScalar l = SkIntToScalar(contentArea->fLeft);
117 SkScalar t = SkIntToScalar(contentArea->fTop);
118 constraintRect.writable()->offset(l, t);
119 textureMatrix.postTranslate(l, t);
120 }
121
122 SkRect domain;
123 GrSamplerParams params;
124 if (filterOrNullForBicubic) {
125 params.setFilterMode(*filterOrNullForBicubic);
126 }
Brian Osman61624f02016-12-09 14:51:59 -0500127 sk_sp<GrTexture> texture(this->refTextureSafeForParams(params, nullptr));
Brian Osmane8e54582016-11-28 10:06:27 -0500128 if (!texture) {
129 return nullptr;
130 }
131 // If we made a copy then we only copied the contentArea, in which case the new texture is all
132 // content.
133 if (texture.get() != this->originalTexture()) {
134 contentArea = nullptr;
135 }
136
137 DomainMode domainMode =
138 DetermineDomainMode(*constraintRect, filterConstraint, coordsLimitedToConstraintRect,
139 texture->width(), texture->height(),
140 contentArea, filterOrNullForBicubic,
141 &domain);
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.
146
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;
151 domainMode =
152 DetermineDomainMode(*constraintRect, filterConstraint, coordsLimitedToConstraintRect,
153 texture->width(), texture->height(),
154 contentArea, &kBilerp, &domain);
155 SkASSERT(kTightCopy_DomainMode != domainMode);
156 }
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,
161 dstColorSpace);
162 return CreateFragmentProcessorForDomainAndFilter(texture.get(), std::move(colorSpaceXform),
163 textureMatrix, domainMode, domain,
164 filterOrNullForBicubic);
165}