blob: 6cb5ad408e237b18c5c24f21a6a7922b31c26a8c [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#ifndef GrTextureDomainEffect_DEFINED
9#define GrTextureDomainEffect_DEFINED
10
11#include "GrSingleTextureEffect.h"
12#include "gl/GrGLEffect.h"
13
joshualitt30ba4362014-08-21 20:18:45 -070014class GrGLProgramBuilder;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000015class GrGLShaderBuilder;
16struct SkRect;
17
18/**
19 * Limits a texture's lookup coordinates to a domain. Samples outside the domain are either clamped
20 * the edge of the domain or result in a vec4 of zeros (decal mode). The domain is clipped to
21 * normalized texture coords ([0,1]x[0,1] square). Bilinear filtering can cause texels outside the
22 * domain to affect the read value unless the caller considers this when calculating the domain.
23 */
24class GrTextureDomain {
25public:
26 enum Mode {
joshualitt5ae5fc52014-07-29 12:59:27 -070027 // Ignore the texture domain rectangle.
28 kIgnore_Mode,
29 // Clamp texture coords to the domain rectangle.
30 kClamp_Mode,
31 // Treat the area outside the domain rectangle as fully transparent.
32 kDecal_Mode,
33 // Wrap texture coordinates. NOTE: filtering may not work as expected because Bilerp will
34 // read texels outside of the domain. We could perform additional texture reads and filter
35 // in the shader, but are not currently doing this for performance reasons
36 kRepeat_Mode,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000037
joshualitt5ae5fc52014-07-29 12:59:27 -070038 kLastMode = kRepeat_Mode
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000039 };
40 static const int kModeCount = kLastMode + 1;
41
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000042 static const GrTextureDomain& IgnoredDomain() {
43 static const SkRect gDummyRect = {0, 0, 0, 0};
44 static const GrTextureDomain gDomain(gDummyRect, kIgnore_Mode);
45 return gDomain;
46 }
47
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000048 /**
49 * @param index Pass a value >= 0 if using multiple texture domains in the same effect.
50 * It is used to keep inserted variables from causing name collisions.
51 */
52 GrTextureDomain(const SkRect& domain, Mode, int index = -1);
53
54 const SkRect& domain() const { return fDomain; }
55 Mode mode() const { return fMode; }
56
57 /* Computes a domain that bounds all the texels in texelRect. Note that with bilerp enabled
58 texels neighboring the domain may be read. */
59 static const SkRect MakeTexelDomain(const GrTexture* texture, const SkIRect& texelRect) {
60 SkScalar wInv = SK_Scalar1 / texture->width();
61 SkScalar hInv = SK_Scalar1 / texture->height();
62 SkRect result = {
63 texelRect.fLeft * wInv,
64 texelRect.fTop * hInv,
65 texelRect.fRight * wInv,
66 texelRect.fBottom * hInv
67 };
68 return result;
69 }
70
71 bool operator== (const GrTextureDomain& that) const {
egdaniel01a492f2014-08-21 06:47:50 -070072 return fMode == that.fMode && (kIgnore_Mode == fMode || fDomain == that.fDomain);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000073 }
74
75 /**
76 * A GrGLEffect subclass that corresponds to a GrEffect subclass that uses GrTextureDomain
77 * should include this helper. It generates the texture domain GLSL, produces the part of the
78 * effect key that reflects the texture domain code, and performs the uniform uploads necessary
79 * for texture domains.
80 */
81 class GLDomain {
82 public:
83 GLDomain() {
84 fPrevDomain[0] = SK_FloatNaN;
85 SkDEBUGCODE(fMode = (Mode) -1;)
86 }
87
88 /**
89 * Call this from GrGLEffect::emitCode() to sample the texture W.R.T. the domain and mode.
90 *
91 * @param outcolor name of vec4 variable to hold the sampled color.
92 * @param inCoords name of vec2 variable containing the coords to be used with the domain.
93 * It is assumed that this is a variable and not an expression.
94 * @param inModulateColor if non-NULL the sampled color will be modulated with this
95 * expression before being written to outColor.
96 */
97 void sampleTexture(GrGLShaderBuilder* builder,
98 const GrTextureDomain& textureDomain,
99 const char* outColor,
100 const SkString& inCoords,
101 const GrGLEffect::TextureSampler sampler,
102 const char* inModulateColor = NULL);
103
104 /**
105 * Call this from GrGLEffect::setData() to upload uniforms necessary for the texture domain.
106 * The rectangle is automatically adjusted to account for the texture's origin.
107 */
kkinnunen7510b222014-07-30 00:04:16 -0700108 void setData(const GrGLProgramDataManager& pdman, const GrTextureDomain& textureDomain,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000109 GrSurfaceOrigin textureOrigin);
110
111 enum {
112 kDomainKeyBits = 2, // See DomainKey().
113 };
114
115 /**
116 * GrGLEffect::GenKey() must call this and include the returned value in it's computed key.
117 * The returned will be limited to the lower kDomainKeyBits bits.
118 */
bsalomon63e99f72014-07-21 08:03:14 -0700119 static uint32_t DomainKey(const GrTextureDomain& domain) {
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000120 GR_STATIC_ASSERT(kModeCount <= 4);
121 return domain.mode();
122 }
123
124 private:
kkinnunen7510b222014-07-30 00:04:16 -0700125 SkDEBUGCODE(Mode fMode;)
126 GrGLProgramDataManager::UniformHandle fDomainUni;
127 SkString fDomainName;
128 GrGLfloat fPrevDomain[4];
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000129 };
130
131protected:
132 Mode fMode;
133 SkRect fDomain;
134 int fIndex;
135
136 typedef GrSingleTextureEffect INHERITED;
137};
138
139class GrGLTextureDomainEffect;
140
141/**
142 * A basic texture effect that uses GrTextureDomain.
143 */
144class GrTextureDomainEffect : public GrSingleTextureEffect {
145
146public:
bsalomon83d081a2014-07-08 09:56:10 -0700147 static GrEffect* Create(GrTexture*,
148 const SkMatrix&,
149 const SkRect& domain,
150 GrTextureDomain::Mode,
151 GrTextureParams::FilterMode filterMode,
152 GrCoordSet = kLocal_GrCoordSet);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000153
154 virtual ~GrTextureDomainEffect();
155
156 static const char* Name() { return "TextureDomain"; }
157
158 typedef GrGLTextureDomainEffect GLEffect;
159
160 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
161 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE;
162
163 const GrTextureDomain& textureDomain() const { return fTextureDomain; }
164
165protected:
166 GrTextureDomain fTextureDomain;
167
168private:
169 GrTextureDomainEffect(GrTexture*,
170 const SkMatrix&,
171 const SkRect& domain,
172 GrTextureDomain::Mode,
173 GrTextureParams::FilterMode,
174 GrCoordSet);
175
176 virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE;
177
178 GR_DECLARE_EFFECT_TEST;
179
180 typedef GrSingleTextureEffect INHERITED;
181};
182
183#endif