blob: 4d163b88d3b5ff7dbbcd32548dae7a9b5aa97ccd [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"
egdaniel605dd0f2014-11-12 08:35:25 -08009#include "GrInvariantOutput.h"
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000010#include "GrSimpleTextureEffect.h"
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000011#include "SkFloatingPoint.h"
egdaniel64c47282015-11-13 06:54:19 -080012#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel7ea439b2015-12-03 09:20:44 -080013#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -070014#include "glsl/GrGLSLProgramDataManager.h"
cdalton3f6f76f2016-04-11 12:18:09 -070015#include "glsl/GrGLSLSampler.h"
egdaniel7ea439b2015-12-03 09:20:44 -080016#include "glsl/GrGLSLShaderBuilder.h"
egdaniel7ea439b2015-12-03 09:20:44 -080017#include "glsl/GrGLSLUniformHandler.h"
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000018
19GrTextureDomain::GrTextureDomain(const SkRect& domain, Mode mode, int index)
20 : fIndex(index) {
21
22 static const SkRect kFullRect = {0, 0, SK_Scalar1, SK_Scalar1};
commit-bot@chromium.org26632632014-03-25 15:13:18 +000023 if (domain.contains(kFullRect) && kClamp_Mode == mode) {
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000024 fMode = kIgnore_Mode;
25 } else {
26 fMode = mode;
27 }
28
29 if (fMode != kIgnore_Mode) {
30 // We don't currently handle domains that are empty or don't intersect the texture.
31 // It is OK if the domain rect is a line or point, but it should not be inverted. We do not
32 // handle rects that do not intersect the [0..1]x[0..1] rect.
33 SkASSERT(domain.fLeft <= domain.fRight);
34 SkASSERT(domain.fTop <= domain.fBottom);
senorblancod0d37ca2015-04-02 04:54:56 -070035 fDomain.fLeft = SkScalarPin(domain.fLeft, kFullRect.fLeft, kFullRect.fRight);
36 fDomain.fRight = SkScalarPin(domain.fRight, kFullRect.fLeft, kFullRect.fRight);
37 fDomain.fTop = SkScalarPin(domain.fTop, kFullRect.fTop, kFullRect.fBottom);
38 fDomain.fBottom = SkScalarPin(domain.fBottom, kFullRect.fTop, kFullRect.fBottom);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000039 SkASSERT(fDomain.fLeft <= fDomain.fRight);
40 SkASSERT(fDomain.fTop <= fDomain.fBottom);
41 }
42}
43
44//////////////////////////////////////////////////////////////////////////////
45
egdaniel2d721d32015-11-11 13:06:05 -080046void GrTextureDomain::GLDomain::sampleTexture(GrGLSLShaderBuilder* builder,
egdaniel7ea439b2015-12-03 09:20:44 -080047 GrGLSLUniformHandler* uniformHandler,
egdaniela2e3e0f2015-11-19 07:23:45 -080048 const GrGLSLCaps* glslCaps,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000049 const GrTextureDomain& textureDomain,
50 const char* outColor,
51 const SkString& inCoords,
egdaniel09aa1fc2016-04-20 07:09:46 -070052 GrGLSLFragmentProcessor::SamplerHandle sampler,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000053 const char* inModulateColor) {
reed@google.comd7b1af62013-12-09 20:31:50 +000054 SkASSERT((Mode)-1 == fMode || textureDomain.mode() == fMode);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000055 SkDEBUGCODE(fMode = textureDomain.mode();)
56
joshualitt5ae5fc52014-07-29 12:59:27 -070057 if (textureDomain.mode() != kIgnore_Mode && !fDomainUni.isValid()) {
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000058 const char* name;
59 SkString uniName("TexDom");
60 if (textureDomain.fIndex >= 0) {
61 uniName.appendS32(textureDomain.fIndex);
62 }
cdalton5e58cee2016-02-11 12:49:47 -080063 fDomainUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -080064 kVec4f_GrSLType, kDefault_GrSLPrecision,
65 uniName.c_str(), &name);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000066 fDomainName = name;
67 }
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000068
joshualitt5ae5fc52014-07-29 12:59:27 -070069 switch (textureDomain.mode()) {
70 case kIgnore_Mode: {
jvanverth3fc65602015-07-22 08:41:51 -070071 builder->codeAppendf("%s = ", outColor);
joshualitt30ba4362014-08-21 20:18:45 -070072 builder->appendTextureLookupAndModulate(inModulateColor, sampler,
joshualitt5ae5fc52014-07-29 12:59:27 -070073 inCoords.c_str());
jvanverth3fc65602015-07-22 08:41:51 -070074 builder->codeAppend(";");
joshualitt5ae5fc52014-07-29 12:59:27 -070075 break;
76 }
77 case kClamp_Mode: {
78 SkString clampedCoords;
jvanverth3fc65602015-07-22 08:41:51 -070079 clampedCoords.appendf("clamp(%s, %s.xy, %s.zw)",
joshualitt5ae5fc52014-07-29 12:59:27 -070080 inCoords.c_str(), fDomainName.c_str(), fDomainName.c_str());
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000081
jvanverth3fc65602015-07-22 08:41:51 -070082 builder->codeAppendf("%s = ", outColor);
joshualitt30ba4362014-08-21 20:18:45 -070083 builder->appendTextureLookupAndModulate(inModulateColor, sampler,
joshualitt5ae5fc52014-07-29 12:59:27 -070084 clampedCoords.c_str());
jvanverth3fc65602015-07-22 08:41:51 -070085 builder->codeAppend(";");
joshualitt5ae5fc52014-07-29 12:59:27 -070086 break;
87 }
88 case kDecal_Mode: {
89 // Add a block since we're going to declare variables.
egdaniel2d721d32015-11-11 13:06:05 -080090 GrGLSLShaderBuilder::ShaderBlock block(builder);
joshualitt5ae5fc52014-07-29 12:59:27 -070091
92 const char* domain = fDomainName.c_str();
egdaniela2e3e0f2015-11-19 07:23:45 -080093 if (!glslCaps->canUseAnyFunctionInShader()) {
joshualitt5ae5fc52014-07-29 12:59:27 -070094 // On the NexusS and GalaxyNexus, the other path (with the 'any'
95 // call) causes the compilation error "Calls to any function that
96 // may require a gradient calculation inside a conditional block
97 // may return undefined results". This appears to be an issue with
98 // the 'any' call since even the simple "result=black; if (any())
99 // result=white;" code fails to compile.
jvanverth3fc65602015-07-22 08:41:51 -0700100 builder->codeAppend("vec4 outside = vec4(0.0, 0.0, 0.0, 0.0);");
101 builder->codeAppend("vec4 inside = ");
joshualitt30ba4362014-08-21 20:18:45 -0700102 builder->appendTextureLookupAndModulate(inModulateColor, sampler,
joshualitt5ae5fc52014-07-29 12:59:27 -0700103 inCoords.c_str());
jvanverth3fc65602015-07-22 08:41:51 -0700104 builder->codeAppend(";");
halcanary9d524f22016-03-29 09:03:52 -0700105
cdalton1f50acf2016-04-11 11:30:50 -0700106 builder->appendPrecisionModifier(kHigh_GrSLPrecision);
jvanverth3fc65602015-07-22 08:41:51 -0700107 builder->codeAppendf("float x = (%s).x;", inCoords.c_str());
cdalton1f50acf2016-04-11 11:30:50 -0700108 builder->appendPrecisionModifier(kHigh_GrSLPrecision);
jvanverth3fc65602015-07-22 08:41:51 -0700109 builder->codeAppendf("float y = (%s).y;", inCoords.c_str());
joshualitt5ae5fc52014-07-29 12:59:27 -0700110
jvanverth3fc65602015-07-22 08:41:51 -0700111 builder->codeAppendf("x = abs(2.0*(x - %s.x)/(%s.z - %s.x) - 1.0);",
112 domain, domain, domain);
113 builder->codeAppendf("y = abs(2.0*(y - %s.y)/(%s.w - %s.y) - 1.0);",
114 domain, domain, domain);
115 builder->codeAppend("float blend = step(1.0, max(x, y));");
116 builder->codeAppendf("%s = mix(inside, outside, blend);", outColor);
joshualitt5ae5fc52014-07-29 12:59:27 -0700117 } else {
jvanverth3fc65602015-07-22 08:41:51 -0700118 builder->codeAppend("bvec4 outside;\n");
119 builder->codeAppendf("outside.xy = lessThan(%s, %s.xy);", inCoords.c_str(),
joshualitt5ae5fc52014-07-29 12:59:27 -0700120 domain);
jvanverth3fc65602015-07-22 08:41:51 -0700121 builder->codeAppendf("outside.zw = greaterThan(%s, %s.zw);", inCoords.c_str(),
joshualitt5ae5fc52014-07-29 12:59:27 -0700122 domain);
jvanverth3fc65602015-07-22 08:41:51 -0700123 builder->codeAppendf("%s = any(outside) ? vec4(0.0, 0.0, 0.0, 0.0) : ",
joshualitt5ae5fc52014-07-29 12:59:27 -0700124 outColor);
joshualitt30ba4362014-08-21 20:18:45 -0700125 builder->appendTextureLookupAndModulate(inModulateColor, sampler,
joshualitt5ae5fc52014-07-29 12:59:27 -0700126 inCoords.c_str());
jvanverth3fc65602015-07-22 08:41:51 -0700127 builder->codeAppend(";");
joshualitt5ae5fc52014-07-29 12:59:27 -0700128 }
129 break;
130 }
131 case kRepeat_Mode: {
132 SkString clampedCoords;
jvanverth3fc65602015-07-22 08:41:51 -0700133 clampedCoords.printf("mod(%s - %s.xy, %s.zw - %s.xy) + %s.xy",
joshualitt5ae5fc52014-07-29 12:59:27 -0700134 inCoords.c_str(), fDomainName.c_str(), fDomainName.c_str(),
135 fDomainName.c_str(), fDomainName.c_str());
136
jvanverth3fc65602015-07-22 08:41:51 -0700137 builder->codeAppendf("%s = ", outColor);
joshualitt30ba4362014-08-21 20:18:45 -0700138 builder->appendTextureLookupAndModulate(inModulateColor, sampler,
joshualitt5ae5fc52014-07-29 12:59:27 -0700139 clampedCoords.c_str());
jvanverth3fc65602015-07-22 08:41:51 -0700140 builder->codeAppend(";");
joshualitt5ae5fc52014-07-29 12:59:27 -0700141 break;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000142 }
143 }
144}
145
egdaniel018fb622015-10-28 07:26:40 -0700146void GrTextureDomain::GLDomain::setData(const GrGLSLProgramDataManager& pdman,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000147 const GrTextureDomain& textureDomain,
148 GrSurfaceOrigin textureOrigin) {
149 SkASSERT(textureDomain.mode() == fMode);
150 if (kIgnore_Mode != textureDomain.mode()) {
egdaniel018fb622015-10-28 07:26:40 -0700151 float values[kPrevDomainCount] = {
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000152 SkScalarToFloat(textureDomain.domain().left()),
153 SkScalarToFloat(textureDomain.domain().top()),
154 SkScalarToFloat(textureDomain.domain().right()),
155 SkScalarToFloat(textureDomain.domain().bottom())
156 };
157 // vertical flip if necessary
158 if (kBottomLeft_GrSurfaceOrigin == textureOrigin) {
159 values[1] = 1.0f - values[1];
160 values[3] = 1.0f - values[3];
161 // The top and bottom were just flipped, so correct the ordering
162 // of elements so that values = (l, t, r, b).
163 SkTSwap(values[1], values[3]);
164 }
egdaniel018fb622015-10-28 07:26:40 -0700165 if (0 != memcmp(values, fPrevDomain, kPrevDomainCount * sizeof(float))) {
kkinnunen7510b222014-07-30 00:04:16 -0700166 pdman.set4fv(fDomainUni, 1, values);
egdaniel018fb622015-10-28 07:26:40 -0700167 memcpy(fPrevDomain, values, kPrevDomainCount * sizeof(float));
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000168 }
169 }
170}
171
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000172///////////////////////////////////////////////////////////////////////////////
173
bungeman06ca8ec2016-06-09 08:01:03 -0700174sk_sp<GrFragmentProcessor> GrTextureDomainEffect::Make(GrTexture* texture,
brianosman54f30c12016-07-18 10:53:52 -0700175 sk_sp<GrColorSpaceXform> colorSpaceXform,
bungeman06ca8ec2016-06-09 08:01:03 -0700176 const SkMatrix& matrix,
177 const SkRect& domain,
178 GrTextureDomain::Mode mode,
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400179 GrTextureParams::FilterMode filterMode) {
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000180 static const SkRect kFullRect = {0, 0, SK_Scalar1, SK_Scalar1};
181 if (GrTextureDomain::kIgnore_Mode == mode ||
182 (GrTextureDomain::kClamp_Mode == mode && domain.contains(kFullRect))) {
brianosman54f30c12016-07-18 10:53:52 -0700183 return GrSimpleTextureEffect::Make(texture, std::move(colorSpaceXform), matrix, filterMode);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000184 } else {
bungeman06ca8ec2016-06-09 08:01:03 -0700185 return sk_sp<GrFragmentProcessor>(
brianosman54f30c12016-07-18 10:53:52 -0700186 new GrTextureDomainEffect(texture, std::move(colorSpaceXform), matrix, domain, mode,
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400187 filterMode));
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000188 }
189}
190
bsalomon4a339522015-10-06 08:40:50 -0700191GrTextureDomainEffect::GrTextureDomainEffect(GrTexture* texture,
brianosman54f30c12016-07-18 10:53:52 -0700192 sk_sp<GrColorSpaceXform> colorSpaceXform,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000193 const SkMatrix& matrix,
194 const SkRect& domain,
195 GrTextureDomain::Mode mode,
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400196 GrTextureParams::FilterMode filterMode)
197 : GrSingleTextureEffect(texture, std::move(colorSpaceXform), matrix, filterMode)
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000198 , fTextureDomain(domain, mode) {
joshualitt5ae5fc52014-07-29 12:59:27 -0700199 SkASSERT(mode != GrTextureDomain::kRepeat_Mode ||
200 filterMode == GrTextureParams::kNone_FilterMode);
joshualitteb2a6762014-12-04 11:35:33 -0800201 this->initClassID<GrTextureDomainEffect>();
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000202}
203
egdaniel57d3b032015-11-13 11:57:27 -0800204void GrTextureDomainEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps,
205 GrProcessorKeyBuilder* b) const {
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400206 b->add32(GrTextureDomain::GLDomain::DomainKey(fTextureDomain));
joshualitteb2a6762014-12-04 11:35:33 -0800207}
208
egdaniel57d3b032015-11-13 11:57:27 -0800209GrGLSLFragmentProcessor* GrTextureDomainEffect::onCreateGLSLInstance() const {
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400210 class GLSLProcessor : public GrGLSLFragmentProcessor {
211 public:
212 void emitCode(EmitArgs& args) override {
213 const GrTextureDomainEffect& tde = args.fFp.cast<GrTextureDomainEffect>();
214 const GrTextureDomain& domain = tde.fTextureDomain;
215
216 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
217 SkString coords2D = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
218 fGLDomain.sampleTexture(fragBuilder,
219 args.fUniformHandler,
220 args.fGLSLCaps,
221 domain,
222 args.fOutputColor,
223 coords2D,
224 args.fTexSamplers[0],
225 args.fInputColor);
226 }
227
228 protected:
229 void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& fp) override {
230 const GrTextureDomainEffect& tde = fp.cast<GrTextureDomainEffect>();
231 const GrTextureDomain& domain = tde.fTextureDomain;
Brian Salomon0bbecb22016-11-17 11:38:22 -0500232 fGLDomain.setData(pdman, domain, tde.textureSampler(0).getTexture()->origin());
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400233 }
234
235 private:
236 GrTextureDomain::GLDomain fGLDomain;
237
238 };
239
240 return new GLSLProcessor;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000241}
242
bsalomon0e08fc12014-10-15 08:19:04 -0700243bool GrTextureDomainEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
joshualitt49586be2014-09-16 08:21:41 -0700244 const GrTextureDomainEffect& s = sBase.cast<GrTextureDomainEffect>();
Brian Salomona7c4c292016-11-17 12:47:06 -0500245 return this->fTextureDomain == s.fTextureDomain;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000246}
247
egdaniel605dd0f2014-11-12 08:35:25 -0800248void GrTextureDomainEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400249 if (GrTextureDomain::kDecal_Mode == fTextureDomain.mode()) {
Brian Salomon0bbecb22016-11-17 11:38:22 -0500250 if (GrPixelConfigIsAlphaOnly(this->textureSampler(0).getTexture()->config())) {
joshualitt56995b52014-12-11 15:44:02 -0800251 inout->mulByUnknownSingleComponent();
egdanielf8449ba2014-11-25 10:24:56 -0800252 } else {
joshualitt56995b52014-12-11 15:44:02 -0800253 inout->mulByUnknownFourComponents();
egdanielf8449ba2014-11-25 10:24:56 -0800254 }
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000255 } else {
egdaniel1a8ecdf2014-10-03 06:24:12 -0700256 this->updateInvariantOutputForModulation(inout);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000257 }
258}
259
260///////////////////////////////////////////////////////////////////////////////
261
joshualittb0a8a372014-09-23 09:50:21 -0700262GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrTextureDomainEffect);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000263
bungeman06ca8ec2016-06-09 08:01:03 -0700264sk_sp<GrFragmentProcessor> GrTextureDomainEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700265 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
266 GrProcessorUnitTest::kAlphaTextureIdx;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000267 SkRect domain;
joshualitt0067ff52015-07-08 14:26:19 -0700268 domain.fLeft = d->fRandom->nextUScalar1();
269 domain.fRight = d->fRandom->nextRangeScalar(domain.fLeft, SK_Scalar1);
270 domain.fTop = d->fRandom->nextUScalar1();
271 domain.fBottom = d->fRandom->nextRangeScalar(domain.fTop, SK_Scalar1);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000272 GrTextureDomain::Mode mode =
joshualitt0067ff52015-07-08 14:26:19 -0700273 (GrTextureDomain::Mode) d->fRandom->nextULessThan(GrTextureDomain::kModeCount);
274 const SkMatrix& matrix = GrTest::TestMatrix(d->fRandom);
275 bool bilerp = mode != GrTextureDomain::kRepeat_Mode ? d->fRandom->nextBool() : false;
Brian Osmane2f732f2016-10-03 14:23:50 -0400276 auto colorSpaceXform = GrTest::TestColorXform(d->fRandom);
bungeman06ca8ec2016-06-09 08:01:03 -0700277 return GrTextureDomainEffect::Make(
bsalomon0ba8c242015-10-07 09:20:28 -0700278 d->fTextures[texIdx],
Brian Osmane2f732f2016-10-03 14:23:50 -0400279 colorSpaceXform,
bsalomon0ba8c242015-10-07 09:20:28 -0700280 matrix,
281 domain,
282 mode,
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400283 bilerp ? GrTextureParams::kBilerp_FilterMode : GrTextureParams::kNone_FilterMode);
284}
285
286///////////////////////////////////////////////////////////////////////////////
287
288sk_sp<GrFragmentProcessor> GrDeviceSpaceTextureDecalFragmentProcessor::Make(GrTexture* texture,
289 const SkIRect& subset, const SkIPoint& deviceSpaceOffset) {
290 return sk_sp<GrFragmentProcessor>(new GrDeviceSpaceTextureDecalFragmentProcessor(
291 texture, subset, deviceSpaceOffset));
292}
293
294GrDeviceSpaceTextureDecalFragmentProcessor::GrDeviceSpaceTextureDecalFragmentProcessor(
295 GrTexture* texture, const SkIRect& subset, const SkIPoint& deviceSpaceOffset)
Brian Salomon0bbecb22016-11-17 11:38:22 -0500296 : fTextureSampler(texture, GrTextureParams::ClampNoFilter())
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400297 , fTextureDomain(GrTextureDomain::MakeTexelDomain(texture, subset),
298 GrTextureDomain::kDecal_Mode) {
Brian Salomon0bbecb22016-11-17 11:38:22 -0500299 this->addTextureSampler(&fTextureSampler);
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400300 fDeviceSpaceOffset.fX = deviceSpaceOffset.fX - subset.fLeft;
301 fDeviceSpaceOffset.fY = deviceSpaceOffset.fY - subset.fTop;
302 this->initClassID<GrDeviceSpaceTextureDecalFragmentProcessor>();
303 this->setWillReadFragmentPosition();
304}
305
306GrGLSLFragmentProcessor* GrDeviceSpaceTextureDecalFragmentProcessor::onCreateGLSLInstance() const {
307 class GLSLProcessor : public GrGLSLFragmentProcessor {
308 public:
309 void emitCode(EmitArgs& args) override {
310 const GrDeviceSpaceTextureDecalFragmentProcessor& dstdfp =
311 args.fFp.cast<GrDeviceSpaceTextureDecalFragmentProcessor>();
312 const char* scaleAndTranslateName;
313 fScaleAndTranslateUni = args.fUniformHandler->addUniform(kFragment_GrShaderFlag,
314 kVec4f_GrSLType,
315 kDefault_GrSLPrecision,
316 "scaleAndTranslate",
317 &scaleAndTranslateName);
318 args.fFragBuilder->codeAppendf("vec2 coords = %s.xy * %s.xy + %s.zw;",
319 args.fFragBuilder->fragmentPosition(),
320 scaleAndTranslateName, scaleAndTranslateName);
321 fGLDomain.sampleTexture(args.fFragBuilder,
322 args.fUniformHandler,
323 args.fGLSLCaps,
324 dstdfp.fTextureDomain,
325 args.fOutputColor,
326 SkString("coords"),
327 args.fTexSamplers[0],
328 args.fInputColor);
329 }
330
331 protected:
332 void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& fp) override {
333 const GrDeviceSpaceTextureDecalFragmentProcessor& dstdfp =
334 fp.cast<GrDeviceSpaceTextureDecalFragmentProcessor>();
Brian Salomon0bbecb22016-11-17 11:38:22 -0500335 GrTexture* texture = dstdfp.textureSampler(0).getTexture();
336 fGLDomain.setData(pdman, dstdfp.fTextureDomain, texture->origin());
337 float iw = 1.f / texture->width();
338 float ih = 1.f / texture->height();
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400339 float scaleAndTransData[4] = {
340 iw, ih,
341 -dstdfp.fDeviceSpaceOffset.fX * iw, -dstdfp.fDeviceSpaceOffset.fY * ih
342 };
Brian Salomon0bbecb22016-11-17 11:38:22 -0500343 if (texture->origin() == kBottomLeft_GrSurfaceOrigin) {
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400344 scaleAndTransData[1] = -scaleAndTransData[1];
345 scaleAndTransData[3] = 1 - scaleAndTransData[3];
346 }
347 pdman.set4fv(fScaleAndTranslateUni, 1, scaleAndTransData);
348 }
349
350 private:
351 GrTextureDomain::GLDomain fGLDomain;
352 UniformHandle fScaleAndTranslateUni;
353 };
354
355 return new GLSLProcessor;
356}
357
358bool GrDeviceSpaceTextureDecalFragmentProcessor::onIsEqual(const GrFragmentProcessor& fp) const {
359 const GrDeviceSpaceTextureDecalFragmentProcessor& dstdfp =
360 fp.cast<GrDeviceSpaceTextureDecalFragmentProcessor>();
Brian Salomon0bbecb22016-11-17 11:38:22 -0500361 return dstdfp.fTextureSampler.getTexture() == fTextureSampler.getTexture() &&
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400362 dstdfp.fDeviceSpaceOffset == fDeviceSpaceOffset &&
363 dstdfp.fTextureDomain == fTextureDomain;
364}
365
366void GrDeviceSpaceTextureDecalFragmentProcessor::onComputeInvariantOutput(
367 GrInvariantOutput* inout) const {
Brian Salomon0bbecb22016-11-17 11:38:22 -0500368 if (GrPixelConfigIsAlphaOnly(this->textureSampler(0).getTexture()->config())) {
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400369 inout->mulByUnknownSingleComponent();
370 } else {
371 inout->mulByUnknownFourComponents();
372 }
373}
374
375///////////////////////////////////////////////////////////////////////////////
376
377GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrDeviceSpaceTextureDecalFragmentProcessor);
378
379sk_sp<GrFragmentProcessor> GrDeviceSpaceTextureDecalFragmentProcessor::TestCreate(
380 GrProcessorTestData* d) {
381 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
382 : GrProcessorUnitTest::kAlphaTextureIdx;
383 SkIRect subset;
384 subset.fLeft = d->fRandom->nextULessThan(d->fTextures[texIdx]->width() - 1);
385 subset.fRight = d->fRandom->nextRangeU(subset.fLeft, d->fTextures[texIdx]->width());
386 subset.fTop = d->fRandom->nextULessThan(d->fTextures[texIdx]->height() - 1);
387 subset.fBottom = d->fRandom->nextRangeU(subset.fTop, d->fTextures[texIdx]->height());
388 SkIPoint pt;
389 pt.fX = d->fRandom->nextULessThan(2048);
390 pt.fY = d->fRandom->nextULessThan(2048);
391 return GrDeviceSpaceTextureDecalFragmentProcessor::Make(d->fTextures[texIdx], subset, pt);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000392}