blob: 2fa5241060c59ec5da643506166e5be3a42c7295 [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,
Robert Phillips67c18d62017-01-20 12:44:06 -050067 SkIPoint* outOffset,
68 SkScalar scaleAdjust[2]) {
Brian Osmane8e54582016-11-28 10:06:27 -050069 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 Phillips67c18d62017-01-20 12:44:06 -050085 } else if (!context->getGpu()->makeCopyForTextureParams(texture, params, &copyParams,
86 scaleAdjust)) {
Brian Osmane8e54582016-11-28 10:06:27 -050087 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
104sk_sp<GrFragmentProcessor> GrTextureAdjuster::createFragmentProcessor(
105 const SkMatrix& origTextureMatrix,
106 const SkRect& origConstraintRect,
107 FilterConstraint filterConstraint,
108 bool coordsLimitedToConstraintRect,
109 const GrSamplerParams::FilterMode* filterOrNullForBicubic,
Brian Osman61624f02016-12-09 14:51:59 -0500110 SkColorSpace* dstColorSpace) {
Brian Osmane8e54582016-11-28 10:06:27 -0500111
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 Phillips67c18d62017-01-20 12:44:06 -0500129 SkScalar scaleAdjust[2] = { 1.0f, 1.0f };
130 sk_sp<GrTexture> texture(this->refTextureSafeForParams(params, nullptr, scaleAdjust));
Brian Osmane8e54582016-11-28 10:06:27 -0500131 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 Phillips67c18d62017-01-20 12:44:06 -0500138 textureMatrix.postScale(scaleAdjust[0], scaleAdjust[1]);
Brian Osmane8e54582016-11-28 10:06:27 -0500139 }
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 Osmane8e54582016-11-28 10:06:27 -0500163 sk_sp<GrColorSpaceXform> colorSpaceXform = GrColorSpaceXform::Make(fColorSpace,
164 dstColorSpace);
165 return CreateFragmentProcessorForDomainAndFilter(texture.get(), std::move(colorSpaceXform),
166 textureMatrix, domainMode, domain,
167 filterOrNullForBicubic);
168}