commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 1 | /* |
| 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" |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 9 | #include "GrInvariantOutput.h" |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 10 | #include "GrSimpleTextureEffect.h" |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 11 | #include "SkFloatingPoint.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 12 | #include "glsl/GrGLSLFragmentProcessor.h" |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 13 | #include "glsl/GrGLSLProgramBuilder.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 14 | #include "glsl/GrGLSLProgramDataManager.h" |
egdaniel | 7dc4bd0 | 2015-10-29 07:57:01 -0700 | [diff] [blame] | 15 | #include "glsl/GrGLSLTextureSampler.h" |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 16 | |
| 17 | GrTextureDomain::GrTextureDomain(const SkRect& domain, Mode mode, int index) |
| 18 | : fIndex(index) { |
| 19 | |
| 20 | static const SkRect kFullRect = {0, 0, SK_Scalar1, SK_Scalar1}; |
commit-bot@chromium.org | 2663263 | 2014-03-25 15:13:18 +0000 | [diff] [blame] | 21 | if (domain.contains(kFullRect) && kClamp_Mode == mode) { |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 22 | fMode = kIgnore_Mode; |
| 23 | } else { |
| 24 | fMode = mode; |
| 25 | } |
| 26 | |
| 27 | if (fMode != kIgnore_Mode) { |
| 28 | // We don't currently handle domains that are empty or don't intersect the texture. |
| 29 | // It is OK if the domain rect is a line or point, but it should not be inverted. We do not |
| 30 | // handle rects that do not intersect the [0..1]x[0..1] rect. |
| 31 | SkASSERT(domain.fLeft <= domain.fRight); |
| 32 | SkASSERT(domain.fTop <= domain.fBottom); |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 33 | fDomain.fLeft = SkScalarPin(domain.fLeft, kFullRect.fLeft, kFullRect.fRight); |
| 34 | fDomain.fRight = SkScalarPin(domain.fRight, kFullRect.fLeft, kFullRect.fRight); |
| 35 | fDomain.fTop = SkScalarPin(domain.fTop, kFullRect.fTop, kFullRect.fBottom); |
| 36 | fDomain.fBottom = SkScalarPin(domain.fBottom, kFullRect.fTop, kFullRect.fBottom); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 37 | SkASSERT(fDomain.fLeft <= fDomain.fRight); |
| 38 | SkASSERT(fDomain.fTop <= fDomain.fBottom); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | ////////////////////////////////////////////////////////////////////////////// |
| 43 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 44 | void GrTextureDomain::GLDomain::sampleTexture(GrGLSLShaderBuilder* builder, |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 45 | const GrTextureDomain& textureDomain, |
| 46 | const char* outColor, |
| 47 | const SkString& inCoords, |
egdaniel | 7dc4bd0 | 2015-10-29 07:57:01 -0700 | [diff] [blame] | 48 | const GrGLSLTextureSampler& sampler, |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 49 | const char* inModulateColor) { |
reed@google.com | d7b1af6 | 2013-12-09 20:31:50 +0000 | [diff] [blame] | 50 | SkASSERT((Mode)-1 == fMode || textureDomain.mode() == fMode); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 51 | SkDEBUGCODE(fMode = textureDomain.mode();) |
| 52 | |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 53 | GrGLSLProgramBuilder* program = builder->getProgramBuilder(); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 54 | |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 55 | if (textureDomain.mode() != kIgnore_Mode && !fDomainUni.isValid()) { |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 56 | const char* name; |
| 57 | SkString uniName("TexDom"); |
| 58 | if (textureDomain.fIndex >= 0) { |
| 59 | uniName.appendS32(textureDomain.fIndex); |
| 60 | } |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 61 | fDomainUni = program->addUniform(GrGLSLProgramBuilder::kFragment_Visibility, |
bsalomon | 422f56f | 2014-12-09 10:18:12 -0800 | [diff] [blame] | 62 | kVec4f_GrSLType, kDefault_GrSLPrecision, |
| 63 | uniName.c_str(), &name); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 64 | fDomainName = name; |
| 65 | } |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 66 | |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 67 | switch (textureDomain.mode()) { |
| 68 | case kIgnore_Mode: { |
jvanverth | 3fc6560 | 2015-07-22 08:41:51 -0700 | [diff] [blame] | 69 | builder->codeAppendf("%s = ", outColor); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 70 | builder->appendTextureLookupAndModulate(inModulateColor, sampler, |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 71 | inCoords.c_str()); |
jvanverth | 3fc6560 | 2015-07-22 08:41:51 -0700 | [diff] [blame] | 72 | builder->codeAppend(";"); |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 73 | break; |
| 74 | } |
| 75 | case kClamp_Mode: { |
| 76 | SkString clampedCoords; |
jvanverth | 3fc6560 | 2015-07-22 08:41:51 -0700 | [diff] [blame] | 77 | clampedCoords.appendf("clamp(%s, %s.xy, %s.zw)", |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 78 | inCoords.c_str(), fDomainName.c_str(), fDomainName.c_str()); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 79 | |
jvanverth | 3fc6560 | 2015-07-22 08:41:51 -0700 | [diff] [blame] | 80 | builder->codeAppendf("%s = ", outColor); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 81 | builder->appendTextureLookupAndModulate(inModulateColor, sampler, |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 82 | clampedCoords.c_str()); |
jvanverth | 3fc6560 | 2015-07-22 08:41:51 -0700 | [diff] [blame] | 83 | builder->codeAppend(";"); |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 84 | break; |
| 85 | } |
| 86 | case kDecal_Mode: { |
| 87 | // Add a block since we're going to declare variables. |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 88 | GrGLSLShaderBuilder::ShaderBlock block(builder); |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 89 | |
| 90 | const char* domain = fDomainName.c_str(); |
egdaniel | 472d44e | 2015-10-22 08:20:00 -0700 | [diff] [blame] | 91 | if (!program->glslCaps()->canUseAnyFunctionInShader()) { |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 92 | // On the NexusS and GalaxyNexus, the other path (with the 'any' |
| 93 | // call) causes the compilation error "Calls to any function that |
| 94 | // may require a gradient calculation inside a conditional block |
| 95 | // may return undefined results". This appears to be an issue with |
| 96 | // the 'any' call since even the simple "result=black; if (any()) |
| 97 | // result=white;" code fails to compile. |
jvanverth | 3fc6560 | 2015-07-22 08:41:51 -0700 | [diff] [blame] | 98 | builder->codeAppend("vec4 outside = vec4(0.0, 0.0, 0.0, 0.0);"); |
| 99 | builder->codeAppend("vec4 inside = "); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 100 | builder->appendTextureLookupAndModulate(inModulateColor, sampler, |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 101 | inCoords.c_str()); |
jvanverth | 3fc6560 | 2015-07-22 08:41:51 -0700 | [diff] [blame] | 102 | builder->codeAppend(";"); |
| 103 | |
egdaniel | 0d3f061 | 2015-10-21 10:45:48 -0700 | [diff] [blame] | 104 | builder->codeAppend(GrGLSLShaderVar::PrecisionString(program->glslCaps(), |
| 105 | kHigh_GrSLPrecision)); |
jvanverth | 3fc6560 | 2015-07-22 08:41:51 -0700 | [diff] [blame] | 106 | builder->codeAppendf("float x = (%s).x;", inCoords.c_str()); |
egdaniel | 0d3f061 | 2015-10-21 10:45:48 -0700 | [diff] [blame] | 107 | builder->codeAppend(GrGLSLShaderVar::PrecisionString(program->glslCaps(), |
| 108 | kHigh_GrSLPrecision)); |
jvanverth | 3fc6560 | 2015-07-22 08:41:51 -0700 | [diff] [blame] | 109 | builder->codeAppendf("float y = (%s).y;", inCoords.c_str()); |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 110 | |
jvanverth | 3fc6560 | 2015-07-22 08:41:51 -0700 | [diff] [blame] | 111 | 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); |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 117 | } else { |
jvanverth | 3fc6560 | 2015-07-22 08:41:51 -0700 | [diff] [blame] | 118 | builder->codeAppend("bvec4 outside;\n"); |
| 119 | builder->codeAppendf("outside.xy = lessThan(%s, %s.xy);", inCoords.c_str(), |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 120 | domain); |
jvanverth | 3fc6560 | 2015-07-22 08:41:51 -0700 | [diff] [blame] | 121 | builder->codeAppendf("outside.zw = greaterThan(%s, %s.zw);", inCoords.c_str(), |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 122 | domain); |
jvanverth | 3fc6560 | 2015-07-22 08:41:51 -0700 | [diff] [blame] | 123 | builder->codeAppendf("%s = any(outside) ? vec4(0.0, 0.0, 0.0, 0.0) : ", |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 124 | outColor); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 125 | builder->appendTextureLookupAndModulate(inModulateColor, sampler, |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 126 | inCoords.c_str()); |
jvanverth | 3fc6560 | 2015-07-22 08:41:51 -0700 | [diff] [blame] | 127 | builder->codeAppend(";"); |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 128 | } |
| 129 | break; |
| 130 | } |
| 131 | case kRepeat_Mode: { |
| 132 | SkString clampedCoords; |
jvanverth | 3fc6560 | 2015-07-22 08:41:51 -0700 | [diff] [blame] | 133 | clampedCoords.printf("mod(%s - %s.xy, %s.zw - %s.xy) + %s.xy", |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 134 | inCoords.c_str(), fDomainName.c_str(), fDomainName.c_str(), |
| 135 | fDomainName.c_str(), fDomainName.c_str()); |
| 136 | |
jvanverth | 3fc6560 | 2015-07-22 08:41:51 -0700 | [diff] [blame] | 137 | builder->codeAppendf("%s = ", outColor); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 138 | builder->appendTextureLookupAndModulate(inModulateColor, sampler, |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 139 | clampedCoords.c_str()); |
jvanverth | 3fc6560 | 2015-07-22 08:41:51 -0700 | [diff] [blame] | 140 | builder->codeAppend(";"); |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 141 | break; |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 146 | void GrTextureDomain::GLDomain::setData(const GrGLSLProgramDataManager& pdman, |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 147 | const GrTextureDomain& textureDomain, |
| 148 | GrSurfaceOrigin textureOrigin) { |
| 149 | SkASSERT(textureDomain.mode() == fMode); |
| 150 | if (kIgnore_Mode != textureDomain.mode()) { |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 151 | float values[kPrevDomainCount] = { |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 152 | 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 | } |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 165 | if (0 != memcmp(values, fPrevDomain, kPrevDomainCount * sizeof(float))) { |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 166 | pdman.set4fv(fDomainUni, 1, values); |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 167 | memcpy(fPrevDomain, values, kPrevDomainCount * sizeof(float)); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | |
| 173 | ////////////////////////////////////////////////////////////////////////////// |
| 174 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 175 | class GrGLTextureDomainEffect : public GrGLSLFragmentProcessor { |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 176 | public: |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 177 | GrGLTextureDomainEffect(const GrProcessor&); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 178 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 179 | virtual void emitCode(EmitArgs&) override; |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 180 | |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 181 | static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 182 | |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 183 | protected: |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 184 | void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 185 | |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 186 | private: |
| 187 | GrTextureDomain::GLDomain fGLDomain; |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 188 | typedef GrGLSLFragmentProcessor INHERITED; |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 189 | }; |
| 190 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 191 | GrGLTextureDomainEffect::GrGLTextureDomainEffect(const GrProcessor&) { |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 192 | } |
| 193 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 194 | void GrGLTextureDomainEffect::emitCode(EmitArgs& args) { |
| 195 | const GrTextureDomainEffect& textureDomainEffect = args.fFp.cast<GrTextureDomainEffect>(); |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 196 | const GrTextureDomain& domain = textureDomainEffect.textureDomain(); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 197 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame^] | 198 | GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 199 | SkString coords2D = fragBuilder->ensureFSCoords2D(args.fCoords, 0); |
| 200 | fGLDomain.sampleTexture(fragBuilder, domain, args.fOutputColor, coords2D, args.fSamplers[0], |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 201 | args.fInputColor); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 202 | } |
| 203 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 204 | void GrGLTextureDomainEffect::onSetData(const GrGLSLProgramDataManager& pdman, |
| 205 | const GrProcessor& processor) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 206 | const GrTextureDomainEffect& textureDomainEffect = processor.cast<GrTextureDomainEffect>(); |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 207 | const GrTextureDomain& domain = textureDomainEffect.textureDomain(); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 208 | fGLDomain.setData(pdman, domain, processor.texture(0)->origin()); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 209 | } |
| 210 | |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 211 | void GrGLTextureDomainEffect::GenKey(const GrProcessor& processor, const GrGLSLCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 212 | GrProcessorKeyBuilder* b) { |
| 213 | const GrTextureDomain& domain = processor.cast<GrTextureDomainEffect>().textureDomain(); |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 214 | b->add32(GrTextureDomain::GLDomain::DomainKey(domain)); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | |
| 218 | /////////////////////////////////////////////////////////////////////////////// |
| 219 | |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 220 | const GrFragmentProcessor* GrTextureDomainEffect::Create(GrTexture* texture, |
| 221 | const SkMatrix& matrix, |
| 222 | const SkRect& domain, |
| 223 | GrTextureDomain::Mode mode, |
| 224 | GrTextureParams::FilterMode filterMode, |
| 225 | GrCoordSet coordSet) { |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 226 | static const SkRect kFullRect = {0, 0, SK_Scalar1, SK_Scalar1}; |
| 227 | if (GrTextureDomain::kIgnore_Mode == mode || |
| 228 | (GrTextureDomain::kClamp_Mode == mode && domain.contains(kFullRect))) { |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 229 | return GrSimpleTextureEffect::Create(texture, matrix, filterMode); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 230 | } else { |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 231 | return new GrTextureDomainEffect(texture, matrix, domain, mode, filterMode, coordSet); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 235 | GrTextureDomainEffect::GrTextureDomainEffect(GrTexture* texture, |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 236 | const SkMatrix& matrix, |
| 237 | const SkRect& domain, |
| 238 | GrTextureDomain::Mode mode, |
| 239 | GrTextureParams::FilterMode filterMode, |
| 240 | GrCoordSet coordSet) |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 241 | : GrSingleTextureEffect(texture, matrix, filterMode, coordSet) |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 242 | , fTextureDomain(domain, mode) { |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 243 | SkASSERT(mode != GrTextureDomain::kRepeat_Mode || |
| 244 | filterMode == GrTextureParams::kNone_FilterMode); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 245 | this->initClassID<GrTextureDomainEffect>(); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 246 | } |
| 247 | |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 248 | GrTextureDomainEffect::~GrTextureDomainEffect() {} |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 249 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 250 | void GrTextureDomainEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps, |
| 251 | GrProcessorKeyBuilder* b) const { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 252 | GrGLTextureDomainEffect::GenKey(*this, caps, b); |
| 253 | } |
| 254 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 255 | GrGLSLFragmentProcessor* GrTextureDomainEffect::onCreateGLSLInstance() const { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 256 | return new GrGLTextureDomainEffect(*this); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 257 | } |
| 258 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 259 | bool GrTextureDomainEffect::onIsEqual(const GrFragmentProcessor& sBase) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 260 | const GrTextureDomainEffect& s = sBase.cast<GrTextureDomainEffect>(); |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 261 | return this->fTextureDomain == s.fTextureDomain; |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 262 | } |
| 263 | |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 264 | void GrTextureDomainEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const { |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 265 | if (GrTextureDomain::kDecal_Mode == fTextureDomain.mode()) { // TODO: helper |
egdaniel | f8449ba | 2014-11-25 10:24:56 -0800 | [diff] [blame] | 266 | if (GrPixelConfigIsAlphaOnly(this->texture(0)->config())) { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 267 | inout->mulByUnknownSingleComponent(); |
egdaniel | f8449ba | 2014-11-25 10:24:56 -0800 | [diff] [blame] | 268 | } else { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 269 | inout->mulByUnknownFourComponents(); |
egdaniel | f8449ba | 2014-11-25 10:24:56 -0800 | [diff] [blame] | 270 | } |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 271 | } else { |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 272 | this->updateInvariantOutputForModulation(inout); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
| 276 | /////////////////////////////////////////////////////////////////////////////// |
| 277 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 278 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrTextureDomainEffect); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 279 | |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 280 | const GrFragmentProcessor* GrTextureDomainEffect::TestCreate(GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 281 | int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx : |
| 282 | GrProcessorUnitTest::kAlphaTextureIdx; |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 283 | SkRect domain; |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 284 | domain.fLeft = d->fRandom->nextUScalar1(); |
| 285 | domain.fRight = d->fRandom->nextRangeScalar(domain.fLeft, SK_Scalar1); |
| 286 | domain.fTop = d->fRandom->nextUScalar1(); |
| 287 | domain.fBottom = d->fRandom->nextRangeScalar(domain.fTop, SK_Scalar1); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 288 | GrTextureDomain::Mode mode = |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 289 | (GrTextureDomain::Mode) d->fRandom->nextULessThan(GrTextureDomain::kModeCount); |
| 290 | const SkMatrix& matrix = GrTest::TestMatrix(d->fRandom); |
| 291 | bool bilerp = mode != GrTextureDomain::kRepeat_Mode ? d->fRandom->nextBool() : false; |
| 292 | GrCoordSet coords = d->fRandom->nextBool() ? kLocal_GrCoordSet : kDevice_GrCoordSet; |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 293 | return GrTextureDomainEffect::Create( |
| 294 | d->fTextures[texIdx], |
| 295 | matrix, |
| 296 | domain, |
| 297 | mode, |
| 298 | bilerp ? GrTextureParams::kBilerp_FilterMode : GrTextureParams::kNone_FilterMode, |
| 299 | coords); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 300 | } |