OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "GrRRectEffect.h" | 8 #include "GrRRectEffect.h" |
9 | 9 |
10 #include "gl/GrGLEffect.h" | 10 #include "gl/GrGLEffect.h" |
11 #include "gl/GrGLSL.h" | 11 #include "gl/GrGLSL.h" |
12 #include "GrTBackendEffectFactory.h" | 12 #include "GrTBackendEffectFactory.h" |
13 | 13 |
14 #include "SkPath.h" | 14 #include "SkRRect.h" |
15 | 15 |
16 // This effect only supports circular corner rrects where all corners have the s
ame radius | 16 class GLRRectEffect; |
17 // which must be <= kRadiusMin. | 17 |
18 static const SkScalar kRadiusMin = 0.5f; | 18 class RRectEffect : public GrEffect { |
| 19 public: |
| 20 // This effect only supports circular corner rrects where all corners have t
he same radius |
| 21 // which must be <= kRadiusMin. |
| 22 static const SkScalar kRadiusMin; |
| 23 |
| 24 static GrEffectRef* Create(const SkRRect&); |
| 25 |
| 26 virtual ~RRectEffect() {}; |
| 27 static const char* Name() { return "RRect"; } |
| 28 |
| 29 const SkRRect& getRRect() const { return fRRect; } |
| 30 |
| 31 typedef GLRRectEffect GLEffect; |
| 32 |
| 33 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags
) const SK_OVERRIDE; |
| 34 |
| 35 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; |
| 36 |
| 37 private: |
| 38 RRectEffect(const SkRRect&); |
| 39 |
| 40 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE; |
| 41 |
| 42 SkRRect fRRect; |
| 43 |
| 44 GR_DECLARE_EFFECT_TEST; |
| 45 |
| 46 typedef GrEffect INHERITED; |
| 47 }; |
| 48 |
| 49 const SkScalar RRectEffect::kRadiusMin = 0.5f; |
| 50 |
| 51 GrEffectRef* RRectEffect::Create(const SkRRect& rrect) { |
| 52 return CreateEffectRef(AutoEffectUnref(SkNEW_ARGS(RRectEffect, (rrect)))); |
| 53 } |
| 54 |
| 55 void RRectEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlag
s) const { |
| 56 *validFlags = 0; |
| 57 } |
| 58 |
| 59 const GrBackendEffectFactory& RRectEffect::getFactory() const { |
| 60 return GrTBackendEffectFactory<RRectEffect>::getInstance(); |
| 61 } |
| 62 |
| 63 RRectEffect::RRectEffect(const SkRRect& rrect) |
| 64 : fRRect(rrect) { |
| 65 this->setWillReadFragmentPosition(); |
| 66 } |
| 67 |
| 68 bool RRectEffect::onIsEqual(const GrEffect& other) const { |
| 69 const RRectEffect& rre = CastEffect<RRectEffect>(other); |
| 70 return fRRect == rre.fRRect; |
| 71 } |
19 | 72 |
20 ////////////////////////////////////////////////////////////////////////////// | 73 ////////////////////////////////////////////////////////////////////////////// |
21 class GrGLRRectEffect : public GrGLEffect { | 74 |
| 75 GR_DEFINE_EFFECT_TEST(RRectEffect); |
| 76 |
| 77 GrEffectRef* RRectEffect::TestCreate(SkRandom* random, |
| 78 GrContext*, |
| 79 const GrDrawTargetCaps& caps, |
| 80 GrTexture*[]) { |
| 81 SkScalar w = random->nextRangeScalar(20.f, 1000.f); |
| 82 SkScalar h = random->nextRangeScalar(20.f, 1000.f); |
| 83 SkScalar r = random->nextRangeF(kRadiusMin, 9.f); |
| 84 SkRRect rrect; |
| 85 rrect.setRectXY(SkRect::MakeWH(w, h), r, r); |
| 86 |
| 87 return GrRRectEffect::Create(rrect); |
| 88 } |
| 89 |
| 90 ////////////////////////////////////////////////////////////////////////////// |
| 91 |
| 92 class GLRRectEffect : public GrGLEffect { |
22 public: | 93 public: |
23 GrGLRRectEffect(const GrBackendEffectFactory&, const GrDrawEffect&); | 94 GLRRectEffect(const GrBackendEffectFactory&, const GrDrawEffect&); |
24 | 95 |
25 virtual void emitCode(GrGLShaderBuilder* builder, | 96 virtual void emitCode(GrGLShaderBuilder* builder, |
26 const GrDrawEffect& drawEffect, | 97 const GrDrawEffect& drawEffect, |
27 EffectKey key, | 98 EffectKey key, |
28 const char* outputColor, | 99 const char* outputColor, |
29 const char* inputColor, | 100 const char* inputColor, |
30 const TransformedCoordsArray&, | 101 const TransformedCoordsArray&, |
31 const TextureSamplerArray&) SK_OVERRIDE; | 102 const TextureSamplerArray&) SK_OVERRIDE; |
32 | 103 |
33 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&) { retur
n 0; } | 104 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&) { retur
n 0; } |
34 | 105 |
35 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER
RIDE; | 106 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER
RIDE; |
36 | 107 |
37 private: | 108 private: |
38 GrGLUniformManager::UniformHandle fInnerRectUniform; | 109 GrGLUniformManager::UniformHandle fInnerRectUniform; |
39 GrGLUniformManager::UniformHandle fRadiusPlusHalfUniform; | 110 GrGLUniformManager::UniformHandle fRadiusPlusHalfUniform; |
40 SkRRect fPrevRRect; | 111 SkRRect fPrevRRect; |
41 typedef GrGLEffect INHERITED; | 112 typedef GrGLEffect INHERITED; |
42 }; | 113 }; |
43 | 114 |
44 GrGLRRectEffect::GrGLRRectEffect(const GrBackendEffectFactory& factory, | 115 GLRRectEffect::GLRRectEffect(const GrBackendEffectFactory& factory, |
45 const GrDrawEffect& drawEffect) | 116 const GrDrawEffect& drawEffect) |
46 : INHERITED (factory) { | 117 : INHERITED (factory) { |
47 fPrevRRect.setEmpty(); | 118 fPrevRRect.setEmpty(); |
48 } | 119 } |
49 | 120 |
50 void GrGLRRectEffect::emitCode(GrGLShaderBuilder* builder, | 121 void GLRRectEffect::emitCode(GrGLShaderBuilder* builder, |
51 const GrDrawEffect& drawEffect, | 122 const GrDrawEffect& drawEffect, |
52 EffectKey key, | 123 EffectKey key, |
53 const char* outputColor, | 124 const char* outputColor, |
54 const char* inputColor, | 125 const char* inputColor, |
55 const TransformedCoordsArray&, | 126 const TransformedCoordsArray&, |
56 const TextureSamplerArray& samplers) { | 127 const TextureSamplerArray& samplers) { |
57 const char *rectName; | 128 const char *rectName; |
58 const char *radiusPlusHalfName; | 129 const char *radiusPlusHalfName; |
59 // The inner rect is the rrect bounds inset by the radius. Its top, left, ri
ght, and bottom | 130 // The inner rect is the rrect bounds inset by the radius. Its top, left, ri
ght, and bottom |
60 // edges correspond to components x, y, z, and w, respectively. | 131 // edges correspond to components x, y, z, and w, respectively. |
61 fInnerRectUniform = builder->addUniform(GrGLShaderBuilder::kFragment_Visibil
ity, | 132 fInnerRectUniform = builder->addUniform(GrGLShaderBuilder::kFragment_Visibil
ity, |
62 kVec4f_GrSLType, | 133 kVec4f_GrSLType, |
63 "innerRect", | 134 "innerRect", |
64 &rectName); | 135 &rectName); |
65 fRadiusPlusHalfUniform = builder->addUniform(GrGLShaderBuilder::kFragment_Vi
sibility, | 136 fRadiusPlusHalfUniform = builder->addUniform(GrGLShaderBuilder::kFragment_Vi
sibility, |
66 kFloat_GrSLType, | 137 kFloat_GrSLType, |
(...skipping 14 matching lines...) Expand all Loading... |
81 builder->fsCodeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmen
tPos); | 152 builder->fsCodeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmen
tPos); |
82 builder->fsCodeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rect
Name); | 153 builder->fsCodeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rect
Name); |
83 builder->fsCodeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n"); | 154 builder->fsCodeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n"); |
84 builder->fsCodeAppendf("\t\tfloat alpha = clamp(%s - length(dxy), 0.0, 1.0);
\n", | 155 builder->fsCodeAppendf("\t\tfloat alpha = clamp(%s - length(dxy), 0.0, 1.0);
\n", |
85 radiusPlusHalfName); | 156 radiusPlusHalfName); |
86 | 157 |
87 builder->fsCodeAppendf("\t\t%s = %s;\n", outputColor, | 158 builder->fsCodeAppendf("\t\t%s = %s;\n", outputColor, |
88 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_st
r()); | 159 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_st
r()); |
89 } | 160 } |
90 | 161 |
91 void GrGLRRectEffect::setData(const GrGLUniformManager& uman, const GrDrawEffect
& drawEffect) { | 162 void GLRRectEffect::setData(const GrGLUniformManager& uman, const GrDrawEffect&
drawEffect) { |
92 const GrRRectEffect& rre = drawEffect.castEffect<GrRRectEffect>(); | 163 const RRectEffect& rre = drawEffect.castEffect<RRectEffect>(); |
93 const SkRRect& rrect = rre.getRRect(); | 164 const SkRRect& rrect = rre.getRRect(); |
94 if (rrect != fPrevRRect) { | 165 if (rrect != fPrevRRect) { |
95 SkASSERT(rrect.isSimpleCircular()); | 166 SkASSERT(rrect.isSimpleCircular()); |
96 SkRect rect = rrect.getBounds(); | 167 SkRect rect = rrect.getBounds(); |
97 SkScalar radius = rrect.getSimpleRadii().fX; | 168 SkScalar radius = rrect.getSimpleRadii().fX; |
98 SkASSERT(radius >= kRadiusMin); | 169 SkASSERT(radius >= RRectEffect::kRadiusMin); |
99 rect.inset(radius, radius); | 170 rect.inset(radius, radius); |
100 uman.set4f(fInnerRectUniform, rect.fLeft, rect.fTop, rect.fRight, rect.f
Bottom); | 171 uman.set4f(fInnerRectUniform, rect.fLeft, rect.fTop, rect.fRight, rect.f
Bottom); |
101 uman.set1f(fRadiusPlusHalfUniform, radius + 0.5f); | 172 uman.set1f(fRadiusPlusHalfUniform, radius + 0.5f); |
102 fPrevRRect = rrect; | 173 fPrevRRect = rrect; |
103 } | 174 } |
104 } | 175 } |
105 | 176 |
106 ////////////////////////////////////////////////////////////////////////////// | 177 ////////////////////////////////////////////////////////////////////////////// |
107 | 178 |
108 GrEffectRef* GrRRectEffect::Create(const SkRRect& rrect) { | 179 GrEffectRef* GrRRectEffect::Create(const SkRRect& rrect) { |
109 if (!rrect.isSimpleCircular()) { | 180 if (!rrect.isSimpleCircular()) { |
110 return NULL; | 181 return NULL; |
111 } | 182 } |
112 if (rrect.getSimpleRadii().fX < kRadiusMin) { | 183 |
| 184 if (rrect.getSimpleRadii().fX < RRectEffect::kRadiusMin) { |
113 return NULL; | 185 return NULL; |
114 } | 186 } |
115 return CreateEffectRef(AutoEffectUnref(SkNEW_ARGS(GrRRectEffect, (rrect)))); | 187 |
| 188 return RRectEffect::Create(rrect); |
116 } | 189 } |
117 | |
118 GrRRectEffect::~GrRRectEffect() {} | |
119 | |
120 void GrRRectEffect::getConstantColorComponents(GrColor* color, uint32_t* validFl
ags) const { | |
121 *validFlags = 0; | |
122 } | |
123 | |
124 const GrBackendEffectFactory& GrRRectEffect::getFactory() const { | |
125 return GrTBackendEffectFactory<GrRRectEffect>::getInstance(); | |
126 } | |
127 | |
128 GrRRectEffect::GrRRectEffect(const SkRRect& rrect) | |
129 : fRRect(rrect) { | |
130 SkASSERT(rrect.isSimpleCircular()); | |
131 SkASSERT(rrect.getSimpleRadii().fX >= kRadiusMin); | |
132 this->setWillReadFragmentPosition(); | |
133 } | |
134 | |
135 bool GrRRectEffect::onIsEqual(const GrEffect& other) const { | |
136 const GrRRectEffect& rre = CastEffect<GrRRectEffect>(other); | |
137 return fRRect == rre.fRRect; | |
138 } | |
139 | |
140 ////////////////////////////////////////////////////////////////////////////// | |
141 | |
142 GR_DEFINE_EFFECT_TEST(GrRRectEffect); | |
143 | |
144 GrEffectRef* GrRRectEffect::TestCreate(SkRandom* random, | |
145 GrContext*, | |
146 const GrDrawTargetCaps& caps, | |
147 GrTexture*[]) { | |
148 SkScalar w = random->nextRangeScalar(20.f, 1000.f); | |
149 SkScalar h = random->nextRangeScalar(20.f, 1000.f); | |
150 SkScalar r = random->nextRangeF(kRadiusMin, 9.f); | |
151 SkRRect rrect; | |
152 rrect.setRectXY(SkRect::MakeWH(w, h), r, r); | |
153 | |
154 return GrRRectEffect::Create(rrect); | |
155 } | |
OLD | NEW |