blob: a5c2d7019834beddefb4c8f28507694e0abc46e3 [file] [log] [blame]
tomhudson@google.com2f68e762012-07-17 18:43:21 +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
tomhudson@google.com2f68e762012-07-17 18:43:21 +000011#include "GrSingleTextureEffect.h"
12#include "GrRect.h"
13
14class GrGLTextureDomainEffect;
15
16/**
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000017 * 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.com2f68e762012-07-17 18:43:21 +000022 */
23class GrTextureDomainEffect : public GrSingleTextureEffect {
24
25public:
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000026 /**
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.com2f68e762012-07-17 18:43:21 +000036
bsalomon@google.com0ac6af42013-01-16 15:16:18 +000037 static GrEffectRef* Create(GrTexture*,
38 const SkMatrix&,
39 const SkRect& domain,
40 WrapMode,
41 bool bilerp = false);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000042
tomhudson@google.com2f68e762012-07-17 18:43:21 +000043 virtual ~GrTextureDomainEffect();
44
45 static const char* Name() { return "TextureDomain"; }
46
bsalomon@google.com422e81a2012-10-25 14:11:03 +000047 typedef GrGLTextureDomainEffect GLEffect;
tomhudson@google.com2f68e762012-07-17 18:43:21 +000048
bsalomon@google.com396e61f2012-10-25 19:00:29 +000049 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
bsalomon@google.coma469c282012-10-24 18:28:34 +000050 virtual bool isEqual(const GrEffect&) const SK_OVERRIDE;
tomhudson@google.com2f68e762012-07-17 18:43:21 +000051
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000052 const SkRect& domain() const { return fTextureDomain; }
53 WrapMode wrapMode() const { return fWrapMode; }
54
55 /* Computes a domain that bounds all the texels in texelRect. Note that with bilerp enabled
56 texels neighboring the domain may be read. */
57 static const SkRect MakeTexelDomain(const GrTexture* texture, const SkIRect& texelRect) {
58 SkScalar wInv = SK_Scalar1 / texture->width();
59 SkScalar hInv = SK_Scalar1 / texture->height();
60 SkRect result = {
61 texelRect.fLeft * wInv,
62 texelRect.fTop * hInv,
63 texelRect.fRight * wInv,
64 texelRect.fBottom * hInv
65 };
66 return result;
67 }
tomhudson@google.com2f68e762012-07-17 18:43:21 +000068
69protected:
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000070 WrapMode fWrapMode;
71 SkRect fTextureDomain;
tomhudson@google.com2f68e762012-07-17 18:43:21 +000072
73private:
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000074 GrTextureDomainEffect(GrTexture*,
75 const SkMatrix&,
76 const GrRect& domain,
77 WrapMode,
78 bool bilerp);
79
bsalomon@google.comf271cc72012-10-24 19:35:13 +000080 GR_DECLARE_EFFECT_TEST;
tomhudson@google.com2f68e762012-07-17 18:43:21 +000081
82 typedef GrSingleTextureEffect INHERITED;
83};
84
85#endif