Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(181)

Side by Side Diff: src/gpu/effects/GrRRectEffect.cpp

Issue 175233002: Hide GrRRectEffect class from header (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: improve comment Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
robertphillips 2014/02/21 14:39:58 Move into the class now?
bsalomon 2014/02/21 15:36:21 Done.
55 RRectEffect::~RRectEffect() {}
56
57 void RRectEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlag s) const {
58 *validFlags = 0;
59 }
60
61 const GrBackendEffectFactory& RRectEffect::getFactory() const {
62 return GrTBackendEffectFactory<RRectEffect>::getInstance();
63 }
64
65 RRectEffect::RRectEffect(const SkRRect& rrect)
66 : fRRect(rrect) {
67 this->setWillReadFragmentPosition();
68 }
69
70 bool RRectEffect::onIsEqual(const GrEffect& other) const {
71 const RRectEffect& rre = CastEffect<RRectEffect>(other);
72 return fRRect == rre.fRRect;
73 }
19 74
20 ////////////////////////////////////////////////////////////////////////////// 75 //////////////////////////////////////////////////////////////////////////////
21 class GrGLRRectEffect : public GrGLEffect { 76
77 GR_DEFINE_EFFECT_TEST(RRectEffect);
78
79 GrEffectRef* RRectEffect::TestCreate(SkRandom* random,
80 GrContext*,
81 const GrDrawTargetCaps& caps,
82 GrTexture*[]) {
83 SkScalar w = random->nextRangeScalar(20.f, 1000.f);
84 SkScalar h = random->nextRangeScalar(20.f, 1000.f);
85 SkScalar r = random->nextRangeF(kRadiusMin, 9.f);
86 SkRRect rrect;
87 rrect.setRectXY(SkRect::MakeWH(w, h), r, r);
88
89 return GrRRectEffect::Create(rrect);
90 }
91
92 //////////////////////////////////////////////////////////////////////////////
93
94 class GLRRectEffect : public GrGLEffect {
22 public: 95 public:
23 GrGLRRectEffect(const GrBackendEffectFactory&, const GrDrawEffect&); 96 GLRRectEffect(const GrBackendEffectFactory&, const GrDrawEffect&);
24 97
25 virtual void emitCode(GrGLShaderBuilder* builder, 98 virtual void emitCode(GrGLShaderBuilder* builder,
26 const GrDrawEffect& drawEffect, 99 const GrDrawEffect& drawEffect,
27 EffectKey key, 100 EffectKey key,
28 const char* outputColor, 101 const char* outputColor,
29 const char* inputColor, 102 const char* inputColor,
30 const TransformedCoordsArray&, 103 const TransformedCoordsArray&,
31 const TextureSamplerArray&) SK_OVERRIDE; 104 const TextureSamplerArray&) SK_OVERRIDE;
32 105
33 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&) { retur n 0; } 106 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&) { retur n 0; }
34 107
35 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER RIDE; 108 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER RIDE;
36 109
37 private: 110 private:
38 GrGLUniformManager::UniformHandle fInnerRectUniform; 111 GrGLUniformManager::UniformHandle fInnerRectUniform;
39 GrGLUniformManager::UniformHandle fRadiusPlusHalfUniform; 112 GrGLUniformManager::UniformHandle fRadiusPlusHalfUniform;
40 SkRRect fPrevRRect; 113 SkRRect fPrevRRect;
41 typedef GrGLEffect INHERITED; 114 typedef GrGLEffect INHERITED;
42 }; 115 };
43 116
44 GrGLRRectEffect::GrGLRRectEffect(const GrBackendEffectFactory& factory, 117 GLRRectEffect::GLRRectEffect(const GrBackendEffectFactory& factory,
45 const GrDrawEffect& drawEffect) 118 const GrDrawEffect& drawEffect)
46 : INHERITED (factory) { 119 : INHERITED (factory) {
47 fPrevRRect.setEmpty(); 120 fPrevRRect.setEmpty();
48 } 121 }
49 122
50 void GrGLRRectEffect::emitCode(GrGLShaderBuilder* builder, 123 void GLRRectEffect::emitCode(GrGLShaderBuilder* builder,
51 const GrDrawEffect& drawEffect, 124 const GrDrawEffect& drawEffect,
52 EffectKey key, 125 EffectKey key,
53 const char* outputColor, 126 const char* outputColor,
54 const char* inputColor, 127 const char* inputColor,
55 const TransformedCoordsArray&, 128 const TransformedCoordsArray&,
56 const TextureSamplerArray& samplers) { 129 const TextureSamplerArray& samplers) {
57 const char *rectName; 130 const char *rectName;
58 const char *radiusPlusHalfName; 131 const char *radiusPlusHalfName;
59 // The inner rect is the rrect bounds inset by the radius. Its top, left, ri ght, and bottom 132 // 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. 133 // edges correspond to components x, y, z, and w, respectively.
61 fInnerRectUniform = builder->addUniform(GrGLShaderBuilder::kFragment_Visibil ity, 134 fInnerRectUniform = builder->addUniform(GrGLShaderBuilder::kFragment_Visibil ity,
62 kVec4f_GrSLType, 135 kVec4f_GrSLType,
63 "innerRect", 136 "innerRect",
64 &rectName); 137 &rectName);
65 fRadiusPlusHalfUniform = builder->addUniform(GrGLShaderBuilder::kFragment_Vi sibility, 138 fRadiusPlusHalfUniform = builder->addUniform(GrGLShaderBuilder::kFragment_Vi sibility,
66 kFloat_GrSLType, 139 kFloat_GrSLType,
(...skipping 14 matching lines...) Expand all
81 builder->fsCodeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmen tPos); 154 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); 155 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"); 156 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", 157 builder->fsCodeAppendf("\t\tfloat alpha = clamp(%s - length(dxy), 0.0, 1.0); \n",
85 radiusPlusHalfName); 158 radiusPlusHalfName);
86 159
87 builder->fsCodeAppendf("\t\t%s = %s;\n", outputColor, 160 builder->fsCodeAppendf("\t\t%s = %s;\n", outputColor,
88 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_st r()); 161 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_st r());
89 } 162 }
90 163
91 void GrGLRRectEffect::setData(const GrGLUniformManager& uman, const GrDrawEffect & drawEffect) { 164 void GLRRectEffect::setData(const GrGLUniformManager& uman, const GrDrawEffect& drawEffect) {
92 const GrRRectEffect& rre = drawEffect.castEffect<GrRRectEffect>(); 165 const RRectEffect& rre = drawEffect.castEffect<RRectEffect>();
93 const SkRRect& rrect = rre.getRRect(); 166 const SkRRect& rrect = rre.getRRect();
94 if (rrect != fPrevRRect) { 167 if (rrect != fPrevRRect) {
95 SkASSERT(rrect.isSimpleCircular()); 168 SkASSERT(rrect.isSimpleCircular());
96 SkRect rect = rrect.getBounds(); 169 SkRect rect = rrect.getBounds();
97 SkScalar radius = rrect.getSimpleRadii().fX; 170 SkScalar radius = rrect.getSimpleRadii().fX;
98 SkASSERT(radius >= kRadiusMin); 171 SkASSERT(radius >= RRectEffect::kRadiusMin);
99 rect.inset(radius, radius); 172 rect.inset(radius, radius);
100 uman.set4f(fInnerRectUniform, rect.fLeft, rect.fTop, rect.fRight, rect.f Bottom); 173 uman.set4f(fInnerRectUniform, rect.fLeft, rect.fTop, rect.fRight, rect.f Bottom);
101 uman.set1f(fRadiusPlusHalfUniform, radius + 0.5f); 174 uman.set1f(fRadiusPlusHalfUniform, radius + 0.5f);
102 fPrevRRect = rrect; 175 fPrevRRect = rrect;
103 } 176 }
104 } 177 }
105 178
106 ////////////////////////////////////////////////////////////////////////////// 179 //////////////////////////////////////////////////////////////////////////////
107 180
108 GrEffectRef* GrRRectEffect::Create(const SkRRect& rrect) { 181 GrEffectRef* GrRRectEffect::Create(const SkRRect& rrect) {
109 if (!rrect.isSimpleCircular()) { 182 if (!rrect.isSimpleCircular()) {
110 return NULL; 183 return NULL;
111 } 184 }
112 if (rrect.getSimpleRadii().fX < kRadiusMin) { 185
186 if (rrect.getSimpleRadii().fX < RRectEffect::kRadiusMin) {
113 return NULL; 187 return NULL;
114 } 188 }
115 return CreateEffectRef(AutoEffectUnref(SkNEW_ARGS(GrRRectEffect, (rrect)))); 189
190 return RRectEffect::Create(rrect);
116 } 191 }
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 }
OLDNEW
« src/gpu/effects/GrRRectEffect.h ('K') | « src/gpu/effects/GrRRectEffect.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698