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

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

Issue 140093004: Specialize GrConvexPolyEffect for AA rects, use for AA clip rects. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Address comments, rebase 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/GrConvexPolyEffect.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
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 "GrConvexPolyEffect.h" 8 #include "GrConvexPolyEffect.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 "gl/GrGLVertexEffect.h" 12 #include "gl/GrGLVertexEffect.h"
13 #include "GrTBackendEffectFactory.h" 13 #include "GrTBackendEffectFactory.h"
14 14
15 #include "SkPath.h" 15 #include "SkPath.h"
16 16
17 //////////////////////////////////////////////////////////////////////////////
18 class GLAARectEffect;
19
20 class AARectEffect : public GrEffect {
21 public:
22 typedef GLAARectEffect GLEffect;
23
24 const SkRect& getRect() const { return fRect; }
25
26 static const char* Name() { return "AARect"; }
27
28 static GrEffectRef* Create(const SkRect& rect) {
29 return CreateEffectRef(AutoEffectUnref(SkNEW_ARGS(AARectEffect, (rect))) );
30 }
31
32 virtual void getConstantColorComponents(GrColor* color,
33 uint32_t* validFlags) const SK_OVERR IDE {
34 if (fRect.isEmpty()) {
35 // An empty rect will have no coverage anywhere.
36 *color = 0x00000000;
37 *validFlags = kRGBA_GrColorComponentFlags;
38 } else {
39 *validFlags = 0;
40 }
41 }
42
43 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
44
45 private:
46 AARectEffect(const SkRect& rect) : fRect(rect) {
47 this->setWillReadFragmentPosition();
48 }
49
50 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
51 const AARectEffect& aare = CastEffect<AARectEffect>(other);
52 return fRect == aare.fRect;
53 }
54
55 SkRect fRect;
56 typedef GrEffect INHERITED;
57
58 GR_DECLARE_EFFECT_TEST;
59
60 };
61
62 GR_DEFINE_EFFECT_TEST(AARectEffect);
63
64 GrEffectRef* AARectEffect::TestCreate(SkRandom* random,
65 GrContext*,
66 const GrDrawTargetCaps& caps,
67 GrTexture*[]) {
68 SkRect rect = SkRect::MakeLTRB(random->nextSScalar1(),
69 random->nextSScalar1(),
70 random->nextSScalar1(),
71 random->nextSScalar1());
72 return AARectEffect::Create(rect);
73 }
74
75 //////////////////////////////////////////////////////////////////////////////
76
77 class GLAARectEffect : public GrGLEffect {
78 public:
79 GLAARectEffect(const GrBackendEffectFactory&, const GrDrawEffect&);
80
81 virtual void emitCode(GrGLShaderBuilder* builder,
82 const GrDrawEffect& drawEffect,
83 EffectKey key,
84 const char* outputColor,
85 const char* inputColor,
86 const TransformedCoordsArray&,
87 const TextureSamplerArray&) SK_OVERRIDE;
88
89 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&) { retur n 0; }
90
91 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER RIDE;
92
93 private:
94 GrGLUniformManager::UniformHandle fRectUniform;
95 SkRect fPrevRect;
96 typedef GrGLEffect INHERITED;
97 };
98
99 GLAARectEffect::GLAARectEffect(const GrBackendEffectFactory& factory,
100 const GrDrawEffect& drawEffect)
101 : INHERITED (factory) {
102 fPrevRect.fLeft = SK_ScalarNaN;
103 }
104
105 void GLAARectEffect::emitCode(GrGLShaderBuilder* builder,
106 const GrDrawEffect& drawEffect,
107 EffectKey key,
108 const char* outputColor,
109 const char* inputColor,
110 const TransformedCoordsArray&,
111 const TextureSamplerArray& samplers) {
112 const char *rectName;
113 fRectUniform = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
114 kVec4f_GrSLType,
115 "rect",
116 &rectName);
117 const char* fragmentPos = builder->fragmentPosition();
118 // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bot tom - 0.5),
119 // respectively. The amount of coverage removed in x and y by the edges is c omputed as a pair of
120 // negative numbers, xSub and ySub.
121 builder->fsCodeAppend("\t\tfloat xSub, ySub;\n");
122 builder->fsCodeAppendf("\t\txSub = min(%s.x - %s.x, 0.0);\n", fragmentPos, r ectName);
123 builder->fsCodeAppendf("\t\txSub += min(%s.z - %s.x, 0.0);\n", rectName, fra gmentPos);
124 builder->fsCodeAppendf("\t\tySub = min(%s.y - %s.y, 0.0);\n", fragmentPos, r ectName);
125 builder->fsCodeAppendf("\t\tySub += min(%s.w - %s.y, 0.0);\n", rectName, fra gmentPos);
126 // Now compute coverage in x and y and multiply them to get the fraction of the pixel covered.
127 builder->fsCodeAppendf("\t\tfloat alpha = (1.0 + max(xSub, -1.0)) * (1.0 + m ax(ySub, -1.0));\n");
128
129 builder->fsCodeAppendf("\t\t%s = %s;\n", outputColor,
130 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_st r());
131 }
132
133 void GLAARectEffect::setData(const GrGLUniformManager& uman, const GrDrawEffect& drawEffect) {
134 const AARectEffect& aare = drawEffect.castEffect<AARectEffect>();
135 const SkRect& rect = aare.getRect();
136 if (rect != fPrevRect) {
137 uman.set4f(fRectUniform, rect.fLeft + 0.5f, rect.fTop + 0.5f,
138 rect.fRight - 0.5f, rect.fBottom - 0.5f);
139 fPrevRect = rect;
140 }
141 }
142
143 const GrBackendEffectFactory& AARectEffect::getFactory() const {
144 return GrTBackendEffectFactory<AARectEffect>::getInstance();
145 }
146
147 //////////////////////////////////////////////////////////////////////////////
148
17 class GrGLConvexPolyEffect : public GrGLEffect { 149 class GrGLConvexPolyEffect : public GrGLEffect {
18 public: 150 public:
19 GrGLConvexPolyEffect(const GrBackendEffectFactory&, const GrDrawEffect&); 151 GrGLConvexPolyEffect(const GrBackendEffectFactory&, const GrDrawEffect&);
20 152
21 virtual void emitCode(GrGLShaderBuilder* builder, 153 virtual void emitCode(GrGLShaderBuilder* builder,
22 const GrDrawEffect& drawEffect, 154 const GrDrawEffect& drawEffect,
23 EffectKey key, 155 EffectKey key,
24 const char* outputColor, 156 const char* outputColor,
25 const char* inputColor, 157 const char* inputColor,
26 const TransformedCoordsArray&, 158 const TransformedCoordsArray&,
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 edges[3 * n + 1] = v.fX; 271 edges[3 * n + 1] = v.fX;
140 } 272 }
141 SkPoint p = pts[i] + t; 273 SkPoint p = pts[i] + t;
142 edges[3 * n + 2] = -(edges[3 * n] * p.fX + edges[3 * n + 1] * p.fY); 274 edges[3 * n + 2] = -(edges[3 * n] * p.fX + edges[3 * n + 1] * p.fY);
143 ++n; 275 ++n;
144 } 276 }
145 } 277 }
146 return Create(type, n, edges); 278 return Create(type, n, edges);
147 } 279 }
148 280
281 GrEffectRef* GrConvexPolyEffect::CreateForAAFillRect(const SkRect& rect) {
282 return AARectEffect::Create(rect);
283 }
284
149 GrConvexPolyEffect::~GrConvexPolyEffect() {} 285 GrConvexPolyEffect::~GrConvexPolyEffect() {}
150 286
151 void GrConvexPolyEffect::getConstantColorComponents(GrColor* color, uint32_t* va lidFlags) const { 287 void GrConvexPolyEffect::getConstantColorComponents(GrColor* color, uint32_t* va lidFlags) const {
152 *validFlags = 0; 288 *validFlags = 0;
153 } 289 }
154 290
155 const GrBackendEffectFactory& GrConvexPolyEffect::getFactory() const { 291 const GrBackendEffectFactory& GrConvexPolyEffect::getFactory() const {
156 return GrTBackendEffectFactory<GrConvexPolyEffect>::getInstance(); 292 return GrTBackendEffectFactory<GrConvexPolyEffect>::getInstance();
157 } 293 }
158 294
(...skipping 28 matching lines...) Expand all
187 GrTexture*[]) { 323 GrTexture*[]) {
188 EdgeType edgeType = static_cast<EdgeType>(random->nextULessThan(kEdgeTypeCnt )); 324 EdgeType edgeType = static_cast<EdgeType>(random->nextULessThan(kEdgeTypeCnt ));
189 int count = random->nextULessThan(kMaxEdges) + 1; 325 int count = random->nextULessThan(kMaxEdges) + 1;
190 SkScalar edges[kMaxEdges * 3]; 326 SkScalar edges[kMaxEdges * 3];
191 for (int i = 0; i < 3 * count; ++i) { 327 for (int i = 0; i < 3 * count; ++i) {
192 edges[i] = random->nextSScalar1(); 328 edges[i] = random->nextSScalar1();
193 } 329 }
194 330
195 return GrConvexPolyEffect::Create(edgeType, count, edges); 331 return GrConvexPolyEffect::Create(edgeType, count, edges);
196 } 332 }
OLDNEW
« no previous file with comments | « src/gpu/effects/GrConvexPolyEffect.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698