tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +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 | #ifndef GrTextureDomainEffect_DEFINED |
| 9 | #define GrTextureDomainEffect_DEFINED |
| 10 | |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 11 | #include "GrSingleTextureEffect.h" |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 12 | |
| 13 | class GrGLTextureDomainEffect; |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 14 | struct SkRect; |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 15 | |
| 16 | /** |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 17 | * Limits a texture's lookup coordinates to a domain. Samples outside the domain are either clamped |
| 18 | * the edge of the domain or result in a vec4 of zeros. The domain is clipped to normalized texture |
| 19 | * coords ([0,1]x[0,1] square). Bilinear filtering can cause texels outside the domain to affect the |
| 20 | * read value unless the caller considers this when calculating the domain. TODO: This should be a |
| 21 | * helper that can assist an effect rather than effect unto itself. |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 22 | */ |
| 23 | class GrTextureDomainEffect : public GrSingleTextureEffect { |
| 24 | |
| 25 | public: |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 26 | /** |
| 27 | * If SkShader::kDecal_TileMode sticks then this enum could be replaced by SkShader::TileMode. |
| 28 | * We could also consider replacing/augmenting Decal mode with Border mode where the color |
| 29 | * outside of the domain is user-specifiable. Decal mode currently has a hard (non-lerped) |
| 30 | * transition between the border and the interior. |
| 31 | */ |
| 32 | enum WrapMode { |
| 33 | kClamp_WrapMode, |
| 34 | kDecal_WrapMode, |
| 35 | }; |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 36 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 37 | static GrEffectRef* Create(GrTexture*, |
| 38 | const SkMatrix&, |
| 39 | const SkRect& domain, |
| 40 | WrapMode, |
humper@google.com | b86add1 | 2013-07-25 18:49:07 +0000 | [diff] [blame] | 41 | GrTextureParams::FilterMode filterMode, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 42 | GrCoordSet = kLocal_GrCoordSet); |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 43 | |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 44 | virtual ~GrTextureDomainEffect(); |
| 45 | |
| 46 | static const char* Name() { return "TextureDomain"; } |
| 47 | |
bsalomon@google.com | 422e81a | 2012-10-25 14:11:03 +0000 | [diff] [blame] | 48 | typedef GrGLTextureDomainEffect GLEffect; |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 49 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 50 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 51 | virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE; |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 52 | |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 53 | const SkRect& domain() const { return fTextureDomain; } |
| 54 | WrapMode wrapMode() const { return fWrapMode; } |
| 55 | |
| 56 | /* Computes a domain that bounds all the texels in texelRect. Note that with bilerp enabled |
| 57 | texels neighboring the domain may be read. */ |
| 58 | static const SkRect MakeTexelDomain(const GrTexture* texture, const SkIRect& texelRect) { |
| 59 | SkScalar wInv = SK_Scalar1 / texture->width(); |
| 60 | SkScalar hInv = SK_Scalar1 / texture->height(); |
| 61 | SkRect result = { |
| 62 | texelRect.fLeft * wInv, |
| 63 | texelRect.fTop * hInv, |
| 64 | texelRect.fRight * wInv, |
| 65 | texelRect.fBottom * hInv |
| 66 | }; |
| 67 | return result; |
| 68 | } |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 69 | |
| 70 | protected: |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 71 | WrapMode fWrapMode; |
| 72 | SkRect fTextureDomain; |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 73 | |
| 74 | private: |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 75 | GrTextureDomainEffect(GrTexture*, |
| 76 | const SkMatrix&, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 77 | const SkRect& domain, |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 78 | WrapMode, |
humper@google.com | b86add1 | 2013-07-25 18:49:07 +0000 | [diff] [blame] | 79 | GrTextureParams::FilterMode filterMode, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 80 | GrCoordSet); |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 81 | |
bsalomon@google.com | 8a252f7 | 2013-01-22 20:35:13 +0000 | [diff] [blame] | 82 | virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 83 | |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 84 | GR_DECLARE_EFFECT_TEST; |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 85 | |
| 86 | typedef GrSingleTextureEffect INHERITED; |
| 87 | }; |
| 88 | |
| 89 | #endif |