Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 | |
|
robertphillips
2014/02/19 13:39:56
// This effect can't handle radii smaller then kRa
bsalomon
2014/02/19 14:39:41
Done.
| |
| 16 static const SkScalar kRadiusMin = 0.5; | |
| 17 | |
| 18 ////////////////////////////////////////////////////////////////////////////// | |
| 19 class GrGLRRectEffect : public GrGLEffect { | |
| 20 public: | |
| 21 GrGLRRectEffect(const GrBackendEffectFactory&, const GrDrawEffect&); | |
| 22 | |
| 23 virtual void emitCode(GrGLShaderBuilder* builder, | |
| 24 const GrDrawEffect& drawEffect, | |
| 25 EffectKey key, | |
| 26 const char* outputColor, | |
| 27 const char* inputColor, | |
| 28 const TransformedCoordsArray&, | |
| 29 const TextureSamplerArray&) SK_OVERRIDE; | |
| 30 | |
| 31 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&) { retur n 0; } | |
| 32 | |
| 33 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER RIDE; | |
| 34 | |
| 35 private: | |
| 36 GrGLUniformManager::UniformHandle fInnerRectUniform; | |
| 37 GrGLUniformManager::UniformHandle fRadiusPlusHalfUniform; | |
| 38 SkRRect fPrevRRect; | |
| 39 typedef GrGLEffect INHERITED; | |
| 40 }; | |
| 41 | |
| 42 GrGLRRectEffect::GrGLRRectEffect(const GrBackendEffectFactory& factory, | |
| 43 const GrDrawEffect& drawEffect) | |
| 44 : INHERITED (factory) { | |
| 45 fPrevRRect.setEmpty(); | |
| 46 } | |
| 47 | |
| 48 void GrGLRRectEffect::emitCode(GrGLShaderBuilder* builder, | |
| 49 const GrDrawEffect& drawEffect, | |
| 50 EffectKey key, | |
| 51 const char* outputColor, | |
| 52 const char* inputColor, | |
| 53 const TransformedCoordsArray&, | |
| 54 const TextureSamplerArray& samplers) { | |
| 55 const char *rectName; | |
| 56 const char *radiusPlusHalfName; | |
| 57 // The inner rect is the rrect bounds inset by the radius. Its top, left, ri ght, and bottom | |
| 58 // edges correspond to components x, y, z, and w, respectively. | |
| 59 fInnerRectUniform = builder->addUniform(GrGLShaderBuilder::kFragment_Visibil ity, | |
| 60 kVec4f_GrSLType, | |
| 61 "innerRect", | |
| 62 &rectName); | |
| 63 fRadiusPlusHalfUniform = builder->addUniform(GrGLShaderBuilder::kFragment_Vi sibility, | |
| 64 kFloat_GrSLType, | |
| 65 "radiusPlusHalf", | |
| 66 &radiusPlusHalfName); | |
| 67 const char* fragmentPos = builder->fragmentPosition(); | |
| 68 // At each quarter-circle corner we compute a vector that is the offset of t he fragment position | |
| 69 // from the circle center. The vector is pinned in x and y to be in the quar ter-plane relevant | |
|
robertphillips
2014/02/19 13:39:56
near on -> on?
bsalomon
2014/02/19 14:39:41
Done.
| |
| 70 // to that corner. This means that points near on the interior near the rrec t top edge will have | |
| 71 // a vector that points straight up for both the TL left and TR corners. Com puting an | |
| 72 // alpha from this vector at either the TR or TL corner will give the correc t result. Similarly, | |
| 73 // fragments near the other three edges will get the correct AA. Fragments i n the interior of | |
| 74 // the rrect will have a (0,0) vector at all four corners. So long as the ra dius > 0.5 they will | |
| 75 // correctly produce an alpha value of 1 at all four corners. We take the mi n of all the alphas. | |
| 76 // The code below is a simplified version of the above that performs maxs on the vector | |
| 77 // components before computing distances and alpha values so that only one d istance computation | |
| 78 // need be computed to determine the min alpha. | |
| 79 builder->fsCodeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmen tPos); | |
| 80 builder->fsCodeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rect Name); | |
| 81 builder->fsCodeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n"); | |
| 82 builder->fsCodeAppendf("\t\tfloat alpha = clamp(%s - length(dxy), 0.0, 1.0); \n", | |
| 83 radiusPlusHalfName); | |
| 84 | |
| 85 builder->fsCodeAppendf("\t\t%s = %s;\n", outputColor, | |
| 86 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_st r()); | |
| 87 } | |
| 88 | |
| 89 void GrGLRRectEffect::setData(const GrGLUniformManager& uman, const GrDrawEffect & drawEffect) { | |
| 90 const GrRRectEffect& rre = drawEffect.castEffect<GrRRectEffect>(); | |
| 91 const SkRRect& rrect = rre.getRRect(); | |
| 92 if (rrect != fPrevRRect) { | |
| 93 SkASSERT(rrect.isSimpleCircular()); | |
| 94 SkRect rect = rrect.getBounds(); | |
| 95 SkScalar radius = rrect.getSimpleRadii().fX; | |
| 96 SkASSERT(radius >= kRadiusMin); | |
| 97 rect.inset(radius, radius); | |
| 98 uman.set4f(fInnerRectUniform, rect.fLeft, rect.fTop, rect.fRight, rect.f Bottom); | |
| 99 uman.set1f(fRadiusPlusHalfUniform, radius + 0.5f); | |
| 100 fPrevRRect = rrect; | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 ////////////////////////////////////////////////////////////////////////////// | |
| 105 | |
| 106 GrEffectRef* GrRRectEffect::Create(const SkRRect& rrect) { | |
| 107 if (!rrect.isSimpleCircular()) { | |
| 108 return NULL; | |
| 109 } | |
| 110 if (rrect.getSimpleRadii().fX < kRadiusMin) { | |
| 111 return NULL; | |
| 112 } | |
| 113 return CreateEffectRef(AutoEffectUnref(SkNEW_ARGS(GrRRectEffect, (rrect)))); | |
| 114 } | |
| 115 | |
| 116 GrRRectEffect::~GrRRectEffect() {} | |
| 117 | |
| 118 void GrRRectEffect::getConstantColorComponents(GrColor* color, uint32_t* validFl ags) const { | |
| 119 *validFlags = 0; | |
| 120 } | |
| 121 | |
| 122 const GrBackendEffectFactory& GrRRectEffect::getFactory() const { | |
| 123 return GrTBackendEffectFactory<GrRRectEffect>::getInstance(); | |
| 124 } | |
| 125 | |
| 126 GrRRectEffect::GrRRectEffect(const SkRRect& rrect) | |
| 127 : fRRect(rrect) { | |
| 128 SkASSERT(rrect.isSimpleCircular()); | |
| 129 SkASSERT(rrect.getSimpleRadii().fX >= kRadiusMin); | |
| 130 this->setWillReadFragmentPosition(); | |
| 131 } | |
| 132 | |
| 133 bool GrRRectEffect::onIsEqual(const GrEffect& other) const { | |
| 134 const GrRRectEffect& rre = CastEffect<GrRRectEffect>(other); | |
| 135 return fRRect == rre.fRRect; | |
| 136 } | |
| 137 | |
| 138 ////////////////////////////////////////////////////////////////////////////// | |
| 139 | |
| 140 GR_DEFINE_EFFECT_TEST(GrRRectEffect); | |
| 141 | |
| 142 GrEffectRef* GrRRectEffect::TestCreate(SkRandom* random, | |
| 143 GrContext*, | |
| 144 const GrDrawTargetCaps& caps, | |
| 145 GrTexture*[]) { | |
| 146 SkScalar w = random->nextRangeScalar(20.f, 1000.f); | |
| 147 SkScalar h = random->nextRangeScalar(20.f, 1000.f); | |
| 148 SkScalar r = random->nextRangeF(kRadiusMin, 9.f); | |
| 149 SkRRect rrect; | |
| 150 rrect.setRectXY(SkRect::MakeWH(w, h), r, r); | |
| 151 | |
| 152 return GrRRectEffect::Create(rrect); | |
| 153 } | |
| OLD | NEW |