blob: dce16a868e7a48c77666f12ff9284b8ca93c8032 [file] [log] [blame]
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +00001/*
2 * Copyright 2012 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 "GrTextureDomain.h"
Robert Phillips40fd7c92017-01-30 08:06:27 -05009
Robert Phillipsb66b42f2017-03-14 08:53:02 -040010#include "GrResourceProvider.h"
Brian Salomon94efbf52016-11-29 13:43:05 -050011#include "GrShaderCaps.h"
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000012#include "GrSimpleTextureEffect.h"
Robert Phillips40fd7c92017-01-30 08:06:27 -050013#include "GrSurfaceProxyPriv.h"
Robert Phillips646e4292017-06-13 12:44:56 -040014#include "GrTexture.h"
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000015#include "SkFloatingPoint.h"
Brian Osmanc4689632016-12-19 17:04:59 -050016#include "glsl/GrGLSLColorSpaceXformHelper.h"
egdaniel64c47282015-11-13 06:54:19 -080017#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel7ea439b2015-12-03 09:20:44 -080018#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -070019#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080020#include "glsl/GrGLSLShaderBuilder.h"
egdaniel7ea439b2015-12-03 09:20:44 -080021#include "glsl/GrGLSLUniformHandler.h"
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000022
Robert Phillipsb66b42f2017-03-14 08:53:02 -040023static bool can_ignore_rect(GrTextureProxy* proxy, const SkRect& domain) {
24 if (GrResourceProvider::IsFunctionallyExact(proxy)) {
Robert Phillips40fd7c92017-01-30 08:06:27 -050025 const SkIRect kFullRect = SkIRect::MakeWH(proxy->width(), proxy->height());
26
27 return domain.contains(kFullRect);
28 }
29
30 return false;
31}
32
Robert Phillipse98234f2017-01-09 14:23:59 -050033static bool can_ignore_rect(GrTexture* tex, const SkRect& domain) {
34 // This logic is relying on the instantiated size of 'tex'. In the deferred world it
35 // will have to change so this logic only fires for kExact texture proxies. This shouldn't
36 // change the actual behavior of Ganesh since shaders shouldn't be accessing pixels outside
37 // of the content rectangle.
38 const SkIRect kFullRect = SkIRect::MakeWH(tex->width(), tex->height());
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000039
Robert Phillipse98234f2017-01-09 14:23:59 -050040 return domain.contains(kFullRect);
41}
42
43GrTextureDomain::GrTextureDomain(GrTexture* tex, const SkRect& domain, Mode mode, int index)
Robert Phillips40fd7c92017-01-30 08:06:27 -050044 : fMode(mode)
45 , fIndex(index) {
Robert Phillipse98234f2017-01-09 14:23:59 -050046
47 if (kIgnore_Mode == fMode) {
48 return;
49 }
50
51 if (kClamp_Mode == mode && can_ignore_rect(tex, domain)) {
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000052 fMode = kIgnore_Mode;
Robert Phillipse98234f2017-01-09 14:23:59 -050053 return;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000054 }
55
Robert Phillipse98234f2017-01-09 14:23:59 -050056 const SkRect kFullRect = SkRect::MakeIWH(tex->width(), tex->height());
57
58 // We don't currently handle domains that are empty or don't intersect the texture.
59 // It is OK if the domain rect is a line or point, but it should not be inverted. We do not
60 // handle rects that do not intersect the [0..1]x[0..1] rect.
61 SkASSERT(domain.fLeft <= domain.fRight);
62 SkASSERT(domain.fTop <= domain.fBottom);
63 fDomain.fLeft = SkScalarPin(domain.fLeft, 0.0f, kFullRect.fRight);
64 fDomain.fRight = SkScalarPin(domain.fRight, fDomain.fLeft, kFullRect.fRight);
65 fDomain.fTop = SkScalarPin(domain.fTop, 0.0f, kFullRect.fBottom);
66 fDomain.fBottom = SkScalarPin(domain.fBottom, fDomain.fTop, kFullRect.fBottom);
67 SkASSERT(fDomain.fLeft <= fDomain.fRight);
68 SkASSERT(fDomain.fTop <= fDomain.fBottom);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000069}
70
Robert Phillips40fd7c92017-01-30 08:06:27 -050071GrTextureDomain::GrTextureDomain(GrTextureProxy* proxy, const SkRect& domain, Mode mode, int index)
72 : fMode(mode)
73 , fIndex(index) {
74
75 if (kIgnore_Mode == fMode) {
76 return;
77 }
78
79 if (kClamp_Mode == mode && can_ignore_rect(proxy, domain)) {
80 fMode = kIgnore_Mode;
81 return;
82 }
83
84 const SkRect kFullRect = SkRect::MakeIWH(proxy->width(), proxy->height());
85
86 // We don't currently handle domains that are empty or don't intersect the texture.
87 // It is OK if the domain rect is a line or point, but it should not be inverted. We do not
88 // handle rects that do not intersect the [0..1]x[0..1] rect.
89 SkASSERT(domain.fLeft <= domain.fRight);
90 SkASSERT(domain.fTop <= domain.fBottom);
91 fDomain.fLeft = SkScalarPin(domain.fLeft, 0.0f, kFullRect.fRight);
92 fDomain.fRight = SkScalarPin(domain.fRight, fDomain.fLeft, kFullRect.fRight);
93 fDomain.fTop = SkScalarPin(domain.fTop, 0.0f, kFullRect.fBottom);
94 fDomain.fBottom = SkScalarPin(domain.fBottom, fDomain.fTop, kFullRect.fBottom);
95 SkASSERT(fDomain.fLeft <= fDomain.fRight);
96 SkASSERT(fDomain.fTop <= fDomain.fBottom);
97}
98
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000099//////////////////////////////////////////////////////////////////////////////
100
egdaniel2d721d32015-11-11 13:06:05 -0800101void GrTextureDomain::GLDomain::sampleTexture(GrGLSLShaderBuilder* builder,
egdaniel7ea439b2015-12-03 09:20:44 -0800102 GrGLSLUniformHandler* uniformHandler,
Brian Salomon1edc5b92016-11-29 13:43:46 -0500103 const GrShaderCaps* shaderCaps,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000104 const GrTextureDomain& textureDomain,
105 const char* outColor,
106 const SkString& inCoords,
egdaniel09aa1fc2016-04-20 07:09:46 -0700107 GrGLSLFragmentProcessor::SamplerHandle sampler,
Brian Osmanc4689632016-12-19 17:04:59 -0500108 const char* inModulateColor,
109 GrGLSLColorSpaceXformHelper* colorXformHelper) {
reed@google.comd7b1af62013-12-09 20:31:50 +0000110 SkASSERT((Mode)-1 == fMode || textureDomain.mode() == fMode);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000111 SkDEBUGCODE(fMode = textureDomain.mode();)
112
joshualitt5ae5fc52014-07-29 12:59:27 -0700113 if (textureDomain.mode() != kIgnore_Mode && !fDomainUni.isValid()) {
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000114 const char* name;
115 SkString uniName("TexDom");
116 if (textureDomain.fIndex >= 0) {
117 uniName.appendS32(textureDomain.fIndex);
118 }
cdalton5e58cee2016-02-11 12:49:47 -0800119 fDomainUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -0800120 kVec4f_GrSLType, kDefault_GrSLPrecision,
121 uniName.c_str(), &name);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000122 fDomainName = name;
123 }
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000124
joshualitt5ae5fc52014-07-29 12:59:27 -0700125 switch (textureDomain.mode()) {
126 case kIgnore_Mode: {
jvanverth3fc65602015-07-22 08:41:51 -0700127 builder->codeAppendf("%s = ", outColor);
Brian Osmanc4689632016-12-19 17:04:59 -0500128 builder->appendTextureLookupAndModulate(inModulateColor, sampler, inCoords.c_str(),
129 kVec2f_GrSLType, colorXformHelper);
jvanverth3fc65602015-07-22 08:41:51 -0700130 builder->codeAppend(";");
joshualitt5ae5fc52014-07-29 12:59:27 -0700131 break;
132 }
133 case kClamp_Mode: {
134 SkString clampedCoords;
jvanverth3fc65602015-07-22 08:41:51 -0700135 clampedCoords.appendf("clamp(%s, %s.xy, %s.zw)",
joshualitt5ae5fc52014-07-29 12:59:27 -0700136 inCoords.c_str(), fDomainName.c_str(), fDomainName.c_str());
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000137
jvanverth3fc65602015-07-22 08:41:51 -0700138 builder->codeAppendf("%s = ", outColor);
Brian Osmanc4689632016-12-19 17:04:59 -0500139 builder->appendTextureLookupAndModulate(inModulateColor, sampler, clampedCoords.c_str(),
140 kVec2f_GrSLType, colorXformHelper);
jvanverth3fc65602015-07-22 08:41:51 -0700141 builder->codeAppend(";");
joshualitt5ae5fc52014-07-29 12:59:27 -0700142 break;
143 }
144 case kDecal_Mode: {
145 // Add a block since we're going to declare variables.
egdaniel2d721d32015-11-11 13:06:05 -0800146 GrGLSLShaderBuilder::ShaderBlock block(builder);
joshualitt5ae5fc52014-07-29 12:59:27 -0700147
148 const char* domain = fDomainName.c_str();
Brian Salomon1edc5b92016-11-29 13:43:46 -0500149 if (!shaderCaps->canUseAnyFunctionInShader()) {
joshualitt5ae5fc52014-07-29 12:59:27 -0700150 // On the NexusS and GalaxyNexus, the other path (with the 'any'
151 // call) causes the compilation error "Calls to any function that
152 // may require a gradient calculation inside a conditional block
153 // may return undefined results". This appears to be an issue with
154 // the 'any' call since even the simple "result=black; if (any())
155 // result=white;" code fails to compile.
jvanverth3fc65602015-07-22 08:41:51 -0700156 builder->codeAppend("vec4 outside = vec4(0.0, 0.0, 0.0, 0.0);");
157 builder->codeAppend("vec4 inside = ");
Brian Osmanc4689632016-12-19 17:04:59 -0500158 builder->appendTextureLookupAndModulate(inModulateColor, sampler, inCoords.c_str(),
159 kVec2f_GrSLType, colorXformHelper);
jvanverth3fc65602015-07-22 08:41:51 -0700160 builder->codeAppend(";");
halcanary9d524f22016-03-29 09:03:52 -0700161
Ethan Nicholas1fc83b12016-11-22 09:31:35 -0500162 builder->codeAppendf("highp float x = (%s).x;", inCoords.c_str());
163 builder->codeAppendf("highp float y = (%s).y;", inCoords.c_str());
joshualitt5ae5fc52014-07-29 12:59:27 -0700164
jvanverth3fc65602015-07-22 08:41:51 -0700165 builder->codeAppendf("x = abs(2.0*(x - %s.x)/(%s.z - %s.x) - 1.0);",
166 domain, domain, domain);
167 builder->codeAppendf("y = abs(2.0*(y - %s.y)/(%s.w - %s.y) - 1.0);",
168 domain, domain, domain);
169 builder->codeAppend("float blend = step(1.0, max(x, y));");
170 builder->codeAppendf("%s = mix(inside, outside, blend);", outColor);
joshualitt5ae5fc52014-07-29 12:59:27 -0700171 } else {
jvanverth3fc65602015-07-22 08:41:51 -0700172 builder->codeAppend("bvec4 outside;\n");
173 builder->codeAppendf("outside.xy = lessThan(%s, %s.xy);", inCoords.c_str(),
joshualitt5ae5fc52014-07-29 12:59:27 -0700174 domain);
jvanverth3fc65602015-07-22 08:41:51 -0700175 builder->codeAppendf("outside.zw = greaterThan(%s, %s.zw);", inCoords.c_str(),
joshualitt5ae5fc52014-07-29 12:59:27 -0700176 domain);
jvanverth3fc65602015-07-22 08:41:51 -0700177 builder->codeAppendf("%s = any(outside) ? vec4(0.0, 0.0, 0.0, 0.0) : ",
joshualitt5ae5fc52014-07-29 12:59:27 -0700178 outColor);
Brian Osmanc4689632016-12-19 17:04:59 -0500179 builder->appendTextureLookupAndModulate(inModulateColor, sampler, inCoords.c_str(),
180 kVec2f_GrSLType, colorXformHelper);
jvanverth3fc65602015-07-22 08:41:51 -0700181 builder->codeAppend(";");
joshualitt5ae5fc52014-07-29 12:59:27 -0700182 }
183 break;
184 }
185 case kRepeat_Mode: {
186 SkString clampedCoords;
jvanverth3fc65602015-07-22 08:41:51 -0700187 clampedCoords.printf("mod(%s - %s.xy, %s.zw - %s.xy) + %s.xy",
joshualitt5ae5fc52014-07-29 12:59:27 -0700188 inCoords.c_str(), fDomainName.c_str(), fDomainName.c_str(),
189 fDomainName.c_str(), fDomainName.c_str());
190
jvanverth3fc65602015-07-22 08:41:51 -0700191 builder->codeAppendf("%s = ", outColor);
Brian Osmanc4689632016-12-19 17:04:59 -0500192 builder->appendTextureLookupAndModulate(inModulateColor, sampler, clampedCoords.c_str(),
193 kVec2f_GrSLType, colorXformHelper);
jvanverth3fc65602015-07-22 08:41:51 -0700194 builder->codeAppend(";");
joshualitt5ae5fc52014-07-29 12:59:27 -0700195 break;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000196 }
197 }
198}
199
egdaniel018fb622015-10-28 07:26:40 -0700200void GrTextureDomain::GLDomain::setData(const GrGLSLProgramDataManager& pdman,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000201 const GrTextureDomain& textureDomain,
Robert Phillipse98234f2017-01-09 14:23:59 -0500202 GrTexture* tex) {
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000203 SkASSERT(textureDomain.mode() == fMode);
204 if (kIgnore_Mode != textureDomain.mode()) {
Robert Phillipse98234f2017-01-09 14:23:59 -0500205 SkScalar wInv = SK_Scalar1 / tex->width();
206 SkScalar hInv = SK_Scalar1 / tex->height();
207
egdaniel018fb622015-10-28 07:26:40 -0700208 float values[kPrevDomainCount] = {
Robert Phillipse98234f2017-01-09 14:23:59 -0500209 SkScalarToFloat(textureDomain.domain().fLeft * wInv),
210 SkScalarToFloat(textureDomain.domain().fTop * hInv),
211 SkScalarToFloat(textureDomain.domain().fRight * wInv),
212 SkScalarToFloat(textureDomain.domain().fBottom * hInv)
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000213 };
Robert Phillipse98234f2017-01-09 14:23:59 -0500214
215 SkASSERT(values[0] >= 0.0f && values[0] <= 1.0f);
216 SkASSERT(values[1] >= 0.0f && values[1] <= 1.0f);
217 SkASSERT(values[2] >= 0.0f && values[2] <= 1.0f);
218 SkASSERT(values[3] >= 0.0f && values[3] <= 1.0f);
219
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000220 // vertical flip if necessary
Robert Phillipse98234f2017-01-09 14:23:59 -0500221 if (kBottomLeft_GrSurfaceOrigin == tex->origin()) {
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000222 values[1] = 1.0f - values[1];
223 values[3] = 1.0f - values[3];
224 // The top and bottom were just flipped, so correct the ordering
225 // of elements so that values = (l, t, r, b).
226 SkTSwap(values[1], values[3]);
227 }
egdaniel018fb622015-10-28 07:26:40 -0700228 if (0 != memcmp(values, fPrevDomain, kPrevDomainCount * sizeof(float))) {
kkinnunen7510b222014-07-30 00:04:16 -0700229 pdman.set4fv(fDomainUni, 1, values);
egdaniel018fb622015-10-28 07:26:40 -0700230 memcpy(fPrevDomain, values, kPrevDomainCount * sizeof(float));
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000231 }
232 }
233}
234
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000235///////////////////////////////////////////////////////////////////////////////
Brian Salomon587e08f2017-01-27 10:59:27 -0500236inline GrFragmentProcessor::OptimizationFlags GrTextureDomainEffect::OptFlags(
Robert Phillips40fd7c92017-01-30 08:06:27 -0500237 GrPixelConfig config, GrTextureDomain::Mode mode) {
238 if (mode == GrTextureDomain::kDecal_Mode || !GrPixelConfigIsOpaque(config)) {
Brian Salomonf3b995b2017-02-15 10:22:23 -0500239 return GrFragmentProcessor::kCompatibleWithCoverageAsAlpha_OptimizationFlag;
Brian Salomon587e08f2017-01-27 10:59:27 -0500240 } else {
Brian Salomonf3b995b2017-02-15 10:22:23 -0500241 return GrFragmentProcessor::kCompatibleWithCoverageAsAlpha_OptimizationFlag |
Brian Salomon587e08f2017-01-27 10:59:27 -0500242 GrFragmentProcessor::kPreservesOpaqueInput_OptimizationFlag;
243 }
244}
245
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400246sk_sp<GrFragmentProcessor> GrTextureDomainEffect::Make(sk_sp<GrTextureProxy> proxy,
Robert Phillips40fd7c92017-01-30 08:06:27 -0500247 sk_sp<GrColorSpaceXform> colorSpaceXform,
248 const SkMatrix& matrix,
249 const SkRect& domain,
250 GrTextureDomain::Mode mode,
251 GrSamplerParams::FilterMode filterMode) {
252 if (GrTextureDomain::kIgnore_Mode == mode ||
253 (GrTextureDomain::kClamp_Mode == mode && can_ignore_rect(proxy.get(), domain))) {
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400254 return GrSimpleTextureEffect::Make(std::move(proxy),
Robert Phillips296b1cc2017-03-15 10:42:12 -0400255 std::move(colorSpaceXform), matrix, filterMode);
Robert Phillips40fd7c92017-01-30 08:06:27 -0500256 } else {
257 return sk_sp<GrFragmentProcessor>(
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400258 new GrTextureDomainEffect(std::move(proxy),
Robert Phillips296b1cc2017-03-15 10:42:12 -0400259 std::move(colorSpaceXform),
Robert Phillips40fd7c92017-01-30 08:06:27 -0500260 matrix, domain, mode, filterMode));
261 }
262}
263
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400264GrTextureDomainEffect::GrTextureDomainEffect(sk_sp<GrTextureProxy> proxy,
Robert Phillips40fd7c92017-01-30 08:06:27 -0500265 sk_sp<GrColorSpaceXform> colorSpaceXform,
266 const SkMatrix& matrix,
267 const SkRect& domain,
268 GrTextureDomain::Mode mode,
269 GrSamplerParams::FilterMode filterMode)
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400270 : GrSingleTextureEffect(OptFlags(proxy->config(), mode), proxy,
Robert Phillips40fd7c92017-01-30 08:06:27 -0500271 std::move(colorSpaceXform), matrix, filterMode)
272 , fTextureDomain(proxy.get(), domain, mode) {
273 SkASSERT(mode != GrTextureDomain::kRepeat_Mode ||
274 filterMode == GrSamplerParams::kNone_FilterMode);
275 this->initClassID<GrTextureDomainEffect>();
276}
277
Brian Salomon94efbf52016-11-29 13:43:05 -0500278void GrTextureDomainEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800279 GrProcessorKeyBuilder* b) const {
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400280 b->add32(GrTextureDomain::GLDomain::DomainKey(fTextureDomain));
Brian Osman70826d72016-12-20 15:06:18 -0500281 b->add32(GrColorSpaceXform::XformKey(this->colorSpaceXform()));
joshualitteb2a6762014-12-04 11:35:33 -0800282}
283
egdaniel57d3b032015-11-13 11:57:27 -0800284GrGLSLFragmentProcessor* GrTextureDomainEffect::onCreateGLSLInstance() const {
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400285 class GLSLProcessor : public GrGLSLFragmentProcessor {
286 public:
287 void emitCode(EmitArgs& args) override {
288 const GrTextureDomainEffect& tde = args.fFp.cast<GrTextureDomainEffect>();
289 const GrTextureDomain& domain = tde.fTextureDomain;
290
291 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
292 SkString coords2D = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
Brian Osmanc4689632016-12-19 17:04:59 -0500293
Brian Osmanc624d9d2017-03-08 11:42:02 -0500294 fColorSpaceHelper.emitCode(args.fUniformHandler, tde.colorSpaceXform());
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400295 fGLDomain.sampleTexture(fragBuilder,
296 args.fUniformHandler,
Brian Salomon1edc5b92016-11-29 13:43:46 -0500297 args.fShaderCaps,
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400298 domain,
299 args.fOutputColor,
300 coords2D,
301 args.fTexSamplers[0],
Brian Osmanc4689632016-12-19 17:04:59 -0500302 args.fInputColor,
Brian Osmanc624d9d2017-03-08 11:42:02 -0500303 &fColorSpaceHelper);
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400304 }
305
306 protected:
Brian Salomonab015ef2017-04-04 10:15:51 -0400307 void onSetData(const GrGLSLProgramDataManager& pdman,
308 const GrFragmentProcessor& fp) override {
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400309 const GrTextureDomainEffect& tde = fp.cast<GrTextureDomainEffect>();
310 const GrTextureDomain& domain = tde.fTextureDomain;
Robert Phillips9bee2e52017-05-29 12:37:20 -0400311 GrTexture* texture = tde.textureSampler(0).peekTexture();
312
313 fGLDomain.setData(pdman, domain, texture);
Brian Osmanc4689632016-12-19 17:04:59 -0500314 if (SkToBool(tde.colorSpaceXform())) {
Brian Osmanc624d9d2017-03-08 11:42:02 -0500315 fColorSpaceHelper.setData(pdman, tde.colorSpaceXform());
Brian Osmanc4689632016-12-19 17:04:59 -0500316 }
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400317 }
318
319 private:
320 GrTextureDomain::GLDomain fGLDomain;
Brian Osmanc624d9d2017-03-08 11:42:02 -0500321 GrGLSLColorSpaceXformHelper fColorSpaceHelper;
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400322 };
323
324 return new GLSLProcessor;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000325}
326
bsalomon0e08fc12014-10-15 08:19:04 -0700327bool GrTextureDomainEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
joshualitt49586be2014-09-16 08:21:41 -0700328 const GrTextureDomainEffect& s = sBase.cast<GrTextureDomainEffect>();
Brian Salomona7c4c292016-11-17 12:47:06 -0500329 return this->fTextureDomain == s.fTextureDomain;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000330}
331
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000332///////////////////////////////////////////////////////////////////////////////
333
joshualittb0a8a372014-09-23 09:50:21 -0700334GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrTextureDomainEffect);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000335
Hal Canary6f6961e2017-01-31 13:50:44 -0500336#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -0700337sk_sp<GrFragmentProcessor> GrTextureDomainEffect::TestCreate(GrProcessorTestData* d) {
Robert Phillips40fd7c92017-01-30 08:06:27 -0500338 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
339 : GrProcessorUnitTest::kAlphaTextureIdx;
340 sk_sp<GrTextureProxy> proxy = d->textureProxy(texIdx);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000341 SkRect domain;
Robert Phillips40fd7c92017-01-30 08:06:27 -0500342 domain.fLeft = d->fRandom->nextRangeScalar(0, proxy->width());
343 domain.fRight = d->fRandom->nextRangeScalar(domain.fLeft, proxy->width());
344 domain.fTop = d->fRandom->nextRangeScalar(0, proxy->height());
345 domain.fBottom = d->fRandom->nextRangeScalar(domain.fTop, proxy->height());
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000346 GrTextureDomain::Mode mode =
joshualitt0067ff52015-07-08 14:26:19 -0700347 (GrTextureDomain::Mode) d->fRandom->nextULessThan(GrTextureDomain::kModeCount);
348 const SkMatrix& matrix = GrTest::TestMatrix(d->fRandom);
349 bool bilerp = mode != GrTextureDomain::kRepeat_Mode ? d->fRandom->nextBool() : false;
Robert Phillips40fd7c92017-01-30 08:06:27 -0500350 sk_sp<GrColorSpaceXform> colorSpaceXform = GrTest::TestColorXform(d->fRandom);
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400351 return GrTextureDomainEffect::Make(std::move(proxy),
Robert Phillips40fd7c92017-01-30 08:06:27 -0500352 std::move(colorSpaceXform),
353 matrix,
354 domain,
355 mode,
356 bilerp ? GrSamplerParams::kBilerp_FilterMode
357 : GrSamplerParams::kNone_FilterMode);
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400358}
Hal Canary6f6961e2017-01-31 13:50:44 -0500359#endif
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400360
361///////////////////////////////////////////////////////////////////////////////
Robert Phillips40fd7c92017-01-30 08:06:27 -0500362sk_sp<GrFragmentProcessor> GrDeviceSpaceTextureDecalFragmentProcessor::Make(
Robert Phillips40fd7c92017-01-30 08:06:27 -0500363 sk_sp<GrTextureProxy> proxy,
364 const SkIRect& subset,
365 const SkIPoint& deviceSpaceOffset) {
366 return sk_sp<GrFragmentProcessor>(new GrDeviceSpaceTextureDecalFragmentProcessor(
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400367 std::move(proxy), subset, deviceSpaceOffset));
Robert Phillips40fd7c92017-01-30 08:06:27 -0500368}
369
370GrDeviceSpaceTextureDecalFragmentProcessor::GrDeviceSpaceTextureDecalFragmentProcessor(
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400371 sk_sp<GrTextureProxy> proxy,
372 const SkIRect& subset,
373 const SkIPoint& deviceSpaceOffset)
Brian Salomonf3b995b2017-02-15 10:22:23 -0500374 : INHERITED(kCompatibleWithCoverageAsAlpha_OptimizationFlag)
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400375 , fTextureSampler(proxy, GrSamplerParams::ClampNoFilter())
Robert Phillips40fd7c92017-01-30 08:06:27 -0500376 , fTextureDomain(proxy.get(), GrTextureDomain::MakeTexelDomain(subset),
377 GrTextureDomain::kDecal_Mode) {
378 this->addTextureSampler(&fTextureSampler);
379 fDeviceSpaceOffset.fX = deviceSpaceOffset.fX - subset.fLeft;
380 fDeviceSpaceOffset.fY = deviceSpaceOffset.fY - subset.fTop;
381 this->initClassID<GrDeviceSpaceTextureDecalFragmentProcessor>();
382}
383
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400384GrGLSLFragmentProcessor* GrDeviceSpaceTextureDecalFragmentProcessor::onCreateGLSLInstance() const {
385 class GLSLProcessor : public GrGLSLFragmentProcessor {
386 public:
387 void emitCode(EmitArgs& args) override {
388 const GrDeviceSpaceTextureDecalFragmentProcessor& dstdfp =
389 args.fFp.cast<GrDeviceSpaceTextureDecalFragmentProcessor>();
390 const char* scaleAndTranslateName;
391 fScaleAndTranslateUni = args.fUniformHandler->addUniform(kFragment_GrShaderFlag,
392 kVec4f_GrSLType,
393 kDefault_GrSLPrecision,
394 "scaleAndTranslate",
395 &scaleAndTranslateName);
Ethan Nicholas38657112017-02-09 17:01:22 -0500396 args.fFragBuilder->codeAppendf("vec2 coords = sk_FragCoord.xy * %s.xy + %s.zw;",
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400397 scaleAndTranslateName, scaleAndTranslateName);
398 fGLDomain.sampleTexture(args.fFragBuilder,
399 args.fUniformHandler,
Brian Salomon1edc5b92016-11-29 13:43:46 -0500400 args.fShaderCaps,
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400401 dstdfp.fTextureDomain,
402 args.fOutputColor,
403 SkString("coords"),
404 args.fTexSamplers[0],
405 args.fInputColor);
406 }
407
408 protected:
Brian Salomonab015ef2017-04-04 10:15:51 -0400409 void onSetData(const GrGLSLProgramDataManager& pdman,
410 const GrFragmentProcessor& fp) override {
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400411 const GrDeviceSpaceTextureDecalFragmentProcessor& dstdfp =
412 fp.cast<GrDeviceSpaceTextureDecalFragmentProcessor>();
Robert Phillips9bee2e52017-05-29 12:37:20 -0400413 GrTexture* texture = dstdfp.textureSampler(0).peekTexture();
414
Robert Phillipse98234f2017-01-09 14:23:59 -0500415 fGLDomain.setData(pdman, dstdfp.fTextureDomain, texture);
Brian Salomon0bbecb22016-11-17 11:38:22 -0500416 float iw = 1.f / texture->width();
417 float ih = 1.f / texture->height();
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400418 float scaleAndTransData[4] = {
419 iw, ih,
420 -dstdfp.fDeviceSpaceOffset.fX * iw, -dstdfp.fDeviceSpaceOffset.fY * ih
421 };
Brian Salomon0bbecb22016-11-17 11:38:22 -0500422 if (texture->origin() == kBottomLeft_GrSurfaceOrigin) {
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400423 scaleAndTransData[1] = -scaleAndTransData[1];
424 scaleAndTransData[3] = 1 - scaleAndTransData[3];
425 }
426 pdman.set4fv(fScaleAndTranslateUni, 1, scaleAndTransData);
427 }
428
429 private:
430 GrTextureDomain::GLDomain fGLDomain;
431 UniformHandle fScaleAndTranslateUni;
432 };
433
434 return new GLSLProcessor;
435}
436
437bool GrDeviceSpaceTextureDecalFragmentProcessor::onIsEqual(const GrFragmentProcessor& fp) const {
438 const GrDeviceSpaceTextureDecalFragmentProcessor& dstdfp =
439 fp.cast<GrDeviceSpaceTextureDecalFragmentProcessor>();
Robert Phillips18166ee2017-06-01 12:55:44 -0400440 return dstdfp.fTextureSampler.proxy() == fTextureSampler.proxy() &&
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400441 dstdfp.fDeviceSpaceOffset == fDeviceSpaceOffset &&
442 dstdfp.fTextureDomain == fTextureDomain;
443}
444
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400445///////////////////////////////////////////////////////////////////////////////
446
447GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrDeviceSpaceTextureDecalFragmentProcessor);
448
Hal Canary6f6961e2017-01-31 13:50:44 -0500449#if GR_TEST_UTILS
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400450sk_sp<GrFragmentProcessor> GrDeviceSpaceTextureDecalFragmentProcessor::TestCreate(
451 GrProcessorTestData* d) {
452 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
453 : GrProcessorUnitTest::kAlphaTextureIdx;
Robert Phillips40fd7c92017-01-30 08:06:27 -0500454 sk_sp<GrTextureProxy> proxy = d->textureProxy(texIdx);
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400455 SkIRect subset;
Robert Phillips40fd7c92017-01-30 08:06:27 -0500456 subset.fLeft = d->fRandom->nextULessThan(proxy->width() - 1);
457 subset.fRight = d->fRandom->nextRangeU(subset.fLeft, proxy->width());
458 subset.fTop = d->fRandom->nextULessThan(proxy->height() - 1);
459 subset.fBottom = d->fRandom->nextRangeU(subset.fTop, proxy->height());
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400460 SkIPoint pt;
461 pt.fX = d->fRandom->nextULessThan(2048);
462 pt.fY = d->fRandom->nextULessThan(2048);
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400463 return GrDeviceSpaceTextureDecalFragmentProcessor::Make(std::move(proxy), subset, pt);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000464}
Hal Canary6f6961e2017-01-31 13:50:44 -0500465#endif