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

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

Issue 171413004: Analytic rrect clip for cicular corners, radius >= .5 (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: fix windows warning in rrects.cpp 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
« no previous file with comments | « src/gpu/effects/GrRRectEffect.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright 2014 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 #include "GrRRectEffect.h"
9
10 #include "gl/GrGLEffect.h"
11 #include "gl/GrGLSL.h"
12 #include "GrTBackendEffectFactory.h"
13
14 #include "SkPath.h"
15
16 // This effect only supports circular corner rrects where all corners have the s ame radius
17 // which must be <= kRadiusMin.
18 static const SkScalar kRadiusMin = 0.5;
19
20 //////////////////////////////////////////////////////////////////////////////
21 class GrGLRRectEffect : public GrGLEffect {
22 public:
23 GrGLRRectEffect(const GrBackendEffectFactory&, const GrDrawEffect&);
24
25 virtual void emitCode(GrGLShaderBuilder* builder,
26 const GrDrawEffect& drawEffect,
27 EffectKey key,
28 const char* outputColor,
29 const char* inputColor,
30 const TransformedCoordsArray&,
31 const TextureSamplerArray&) SK_OVERRIDE;
32
33 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&) { retur n 0; }
34
35 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER RIDE;
36
37 private:
38 GrGLUniformManager::UniformHandle fInnerRectUniform;
39 GrGLUniformManager::UniformHandle fRadiusPlusHalfUniform;
40 SkRRect fPrevRRect;
41 typedef GrGLEffect INHERITED;
42 };
43
44 GrGLRRectEffect::GrGLRRectEffect(const GrBackendEffectFactory& factory,
45 const GrDrawEffect& drawEffect)
46 : INHERITED (factory) {
47 fPrevRRect.setEmpty();
48 }
49
50 void GrGLRRectEffect::emitCode(GrGLShaderBuilder* builder,
51 const GrDrawEffect& drawEffect,
52 EffectKey key,
53 const char* outputColor,
54 const char* inputColor,
55 const TransformedCoordsArray&,
56 const TextureSamplerArray& samplers) {
57 const char *rectName;
58 const char *radiusPlusHalfName;
59 // 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.
61 fInnerRectUniform = builder->addUniform(GrGLShaderBuilder::kFragment_Visibil ity,
62 kVec4f_GrSLType,
63 "innerRect",
64 &rectName);
65 fRadiusPlusHalfUniform = builder->addUniform(GrGLShaderBuilder::kFragment_Vi sibility,
66 kFloat_GrSLType,
67 "radiusPlusHalf",
68 &radiusPlusHalfName);
69 const char* fragmentPos = builder->fragmentPosition();
70 // At each quarter-circle corner we compute a vector that is the offset of t he fragment position
71 // from the circle center. The vector is pinned in x and y to be in the quar ter-plane relevant
72 // to that corner. This means that points near the interior near the rrect t op edge will have
73 // a vector that points straight up for both the TL left and TR corners. Com puting an
74 // alpha from this vector at either the TR or TL corner will give the correc t result. Similarly,
75 // fragments near the other three edges will get the correct AA. Fragments i n the interior of
76 // the rrect will have a (0,0) vector at all four corners. So long as the ra dius > 0.5 they will
77 // correctly produce an alpha value of 1 at all four corners. We take the mi n of all the alphas.
78 // The code below is a simplified version of the above that performs maxs on the vector
79 // components before computing distances and alpha values so that only one d istance computation
80 // need be computed to determine the min alpha.
81 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);
83 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",
85 radiusPlusHalfName);
86
87 builder->fsCodeAppendf("\t\t%s = %s;\n", outputColor,
88 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_st r());
89 }
90
91 void GrGLRRectEffect::setData(const GrGLUniformManager& uman, const GrDrawEffect & drawEffect) {
92 const GrRRectEffect& rre = drawEffect.castEffect<GrRRectEffect>();
93 const SkRRect& rrect = rre.getRRect();
94 if (rrect != fPrevRRect) {
95 SkASSERT(rrect.isSimpleCircular());
96 SkRect rect = rrect.getBounds();
97 SkScalar radius = rrect.getSimpleRadii().fX;
98 SkASSERT(radius >= kRadiusMin);
99 rect.inset(radius, radius);
100 uman.set4f(fInnerRectUniform, rect.fLeft, rect.fTop, rect.fRight, rect.f Bottom);
101 uman.set1f(fRadiusPlusHalfUniform, radius + 0.5f);
102 fPrevRRect = rrect;
103 }
104 }
105
106 //////////////////////////////////////////////////////////////////////////////
107
108 GrEffectRef* GrRRectEffect::Create(const SkRRect& rrect) {
109 if (!rrect.isSimpleCircular()) {
110 return NULL;
111 }
112 if (rrect.getSimpleRadii().fX < kRadiusMin) {
113 return NULL;
114 }
115 return CreateEffectRef(AutoEffectUnref(SkNEW_ARGS(GrRRectEffect, (rrect))));
116 }
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
« no previous file with comments | « src/gpu/effects/GrRRectEffect.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698