| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 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 | |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 8 | #include "gl/builders/GrGLProgramBuilder.h" |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 9 | #include "GrRRectEffect.h" |
| 10 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 11 | #include "gl/GrGLProcessor.h" |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 12 | #include "gl/GrGLSL.h" |
| commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 13 | #include "GrConvexPolyEffect.h" |
| commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 14 | #include "GrOvalEffect.h" |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 15 | #include "GrTBackendProcessorFactory.h" |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 16 | |
| commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 17 | #include "SkRRect.h" |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 18 | |
| commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 19 | // The effects defined here only handle rrect radii >= kRadiusMin. |
| 20 | static const SkScalar kRadiusMin = SK_ScalarHalf; |
| 21 | |
| 22 | ////////////////////////////////////////////////////////////////////////////// |
| 23 | |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 24 | class GLCircularRRectEffect; |
| commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 25 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 26 | class CircularRRectEffect : public GrFragmentProcessor { |
| commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 27 | public: |
| skia.committer@gmail.com | f1f66c0 | 2014-03-05 03:02:06 +0000 | [diff] [blame] | 28 | |
| commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 29 | enum CornerFlags { |
| 30 | kTopLeft_CornerFlag = (1 << SkRRect::kUpperLeft_Corner), |
| 31 | kTopRight_CornerFlag = (1 << SkRRect::kUpperRight_Corner), |
| 32 | kBottomRight_CornerFlag = (1 << SkRRect::kLowerRight_Corner), |
| 33 | kBottomLeft_CornerFlag = (1 << SkRRect::kLowerLeft_Corner), |
| 34 | |
| 35 | kLeft_CornerFlags = kTopLeft_CornerFlag | kBottomLeft_CornerFlag, |
| 36 | kTop_CornerFlags = kTopLeft_CornerFlag | kTopRight_CornerFlag, |
| 37 | kRight_CornerFlags = kTopRight_CornerFlag | kBottomRight_CornerFlag, |
| 38 | kBottom_CornerFlags = kBottomLeft_CornerFlag | kBottomRight_CornerFlag, |
| 39 | |
| 40 | kAll_CornerFlags = kTopLeft_CornerFlag | kTopRight_CornerFlag | |
| 41 | kBottomLeft_CornerFlag | kBottomRight_CornerFlag, |
| 42 | |
| commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 43 | kNone_CornerFlags = 0 |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
| commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 46 | // The flags are used to indicate which corners are circluar (unflagged corners are assumed to |
| 47 | // be square). |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 48 | static GrFragmentProcessor* Create(GrPrimitiveEdgeType, uint32_t circularCornerFlags, |
| 49 | const SkRRect&); |
| commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 50 | |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 51 | virtual ~CircularRRectEffect() {}; |
| 52 | static const char* Name() { return "CircularRRect"; } |
| commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 53 | |
| 54 | const SkRRect& getRRect() const { return fRRect; } |
| 55 | |
| commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 56 | uint32_t getCircularCornerFlags() const { return fCircularCornerFlags; } |
| skia.committer@gmail.com | 06acb58 | 2014-03-06 03:02:32 +0000 | [diff] [blame] | 57 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 58 | GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; } |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 59 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 60 | typedef GLCircularRRectEffect GLProcessor; |
| commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 61 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 62 | virtual const GrBackendFragmentProcessorFactory& getFactory() const SK_OVERRIDE; |
| commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 63 | |
| 64 | private: |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 65 | CircularRRectEffect(GrPrimitiveEdgeType, uint32_t circularCornerFlags, const SkRRect&); |
| commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 66 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 67 | virtual bool onIsEqual(const GrProcessor& other) const SK_OVERRIDE; |
| commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 68 | |
| egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 69 | virtual void onComputeInvariantOutput(InvariantOutput* inout) const SK_OVERRIDE; |
| 70 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 71 | SkRRect fRRect; |
| 72 | GrPrimitiveEdgeType fEdgeType; |
| 73 | uint32_t fCircularCornerFlags; |
| commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 74 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 75 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
| commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 76 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 77 | typedef GrFragmentProcessor INHERITED; |
| commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 78 | }; |
| 79 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 80 | GrFragmentProcessor* CircularRRectEffect::Create(GrPrimitiveEdgeType edgeType, |
| 81 | uint32_t circularCornerFlags, |
| 82 | const SkRRect& rrect) { |
| 83 | if (kFillAA_GrProcessorEdgeType != edgeType && kInverseFillAA_GrProcessorEdgeType != edgeType) { |
| commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 84 | return NULL; |
| 85 | } |
| bsalomon | 55fad7a | 2014-07-08 07:34:20 -0700 | [diff] [blame] | 86 | return SkNEW_ARGS(CircularRRectEffect, (edgeType, circularCornerFlags, rrect)); |
| commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 89 | void CircularRRectEffect::onComputeInvariantOutput(InvariantOutput* inout) const { |
| 90 | inout->fValidFlags = 0; |
| 91 | inout->fIsSingleComponent = false; |
| commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 94 | const GrBackendFragmentProcessorFactory& CircularRRectEffect::getFactory() const { |
| 95 | return GrTBackendFragmentProcessorFactory<CircularRRectEffect>::getInstance(); |
| commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 98 | CircularRRectEffect::CircularRRectEffect(GrPrimitiveEdgeType edgeType, uint32_t circularCornerFlags, |
| 99 | const SkRRect& rrect) |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 100 | : fRRect(rrect) |
| commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 101 | , fEdgeType(edgeType) |
| commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 102 | , fCircularCornerFlags(circularCornerFlags) { |
| commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 103 | this->setWillReadFragmentPosition(); |
| 104 | } |
| 105 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 106 | bool CircularRRectEffect::onIsEqual(const GrProcessor& other) const { |
| joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 107 | const CircularRRectEffect& crre = other.cast<CircularRRectEffect>(); |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 108 | // The corner flags are derived from fRRect, so no need to check them. |
| 109 | return fEdgeType == crre.fEdgeType && fRRect == crre.fRRect; |
| commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 110 | } |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 111 | |
| 112 | ////////////////////////////////////////////////////////////////////////////// |
| commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 113 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 114 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(CircularRRectEffect); |
| commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 115 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 116 | GrFragmentProcessor* CircularRRectEffect::TestCreate(SkRandom* random, |
| 117 | GrContext*, |
| 118 | const GrDrawTargetCaps& caps, |
| 119 | GrTexture*[]) { |
| commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 120 | SkScalar w = random->nextRangeScalar(20.f, 1000.f); |
| 121 | SkScalar h = random->nextRangeScalar(20.f, 1000.f); |
| 122 | SkScalar r = random->nextRangeF(kRadiusMin, 9.f); |
| 123 | SkRRect rrect; |
| 124 | rrect.setRectXY(SkRect::MakeWH(w, h), r, r); |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 125 | GrFragmentProcessor* fp; |
| commit-bot@chromium.org | cabf4b2 | 2014-03-05 18:27:43 +0000 | [diff] [blame] | 126 | do { |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 127 | GrPrimitiveEdgeType et = |
| 128 | (GrPrimitiveEdgeType)random->nextULessThan(kGrProcessorEdgeTypeCnt); |
| 129 | fp = GrRRectEffect::Create(et, rrect); |
| 130 | } while (NULL == fp); |
| 131 | return fp; |
| commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | ////////////////////////////////////////////////////////////////////////////// |
| 135 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 136 | class GLCircularRRectEffect : public GrGLFragmentProcessor { |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 137 | public: |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 138 | GLCircularRRectEffect(const GrBackendProcessorFactory&, const GrProcessor&); |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 139 | |
| joshualitt | 1598899 | 2014-10-09 15:04:05 -0700 | [diff] [blame^] | 140 | virtual void emitCode(GrGLFPBuilder* builder, |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 141 | const GrFragmentProcessor& fp, |
| 142 | const GrProcessorKey& key, |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 143 | const char* outputColor, |
| 144 | const char* inputColor, |
| 145 | const TransformedCoordsArray&, |
| 146 | const TextureSamplerArray&) SK_OVERRIDE; |
| 147 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 148 | static inline void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKeyBuilder*); |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 149 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 150 | virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE; |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 151 | |
| 152 | private: |
| kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 153 | GrGLProgramDataManager::UniformHandle fInnerRectUniform; |
| 154 | GrGLProgramDataManager::UniformHandle fRadiusPlusHalfUniform; |
| 155 | SkRRect fPrevRRect; |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 156 | typedef GrGLFragmentProcessor INHERITED; |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 157 | }; |
| 158 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 159 | GLCircularRRectEffect::GLCircularRRectEffect(const GrBackendProcessorFactory& factory, |
| 160 | const GrProcessor& ) |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 161 | : INHERITED (factory) { |
| 162 | fPrevRRect.setEmpty(); |
| 163 | } |
| 164 | |
| joshualitt | 1598899 | 2014-10-09 15:04:05 -0700 | [diff] [blame^] | 165 | void GLCircularRRectEffect::emitCode(GrGLFPBuilder* builder, |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 166 | const GrFragmentProcessor& fp, |
| 167 | const GrProcessorKey& key, |
| commit-bot@chromium.org | e528089 | 2014-02-21 17:52:29 +0000 | [diff] [blame] | 168 | const char* outputColor, |
| 169 | const char* inputColor, |
| 170 | const TransformedCoordsArray&, |
| 171 | const TextureSamplerArray& samplers) { |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 172 | const CircularRRectEffect& crre = fp.cast<CircularRRectEffect>(); |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 173 | const char *rectName; |
| 174 | const char *radiusPlusHalfName; |
| commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 175 | // The inner rect is the rrect bounds inset by the radius. Its left, top, right, and bottom |
| 176 | // edges correspond to components x, y, z, and w, respectively. When a side of the rrect has |
| 177 | // only rectangular corners, that side's value corresponds to the rect edge's value outset by |
| 178 | // half a pixel. |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 179 | fInnerRectUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 180 | kVec4f_GrSLType, |
| 181 | "innerRect", |
| 182 | &rectName); |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 183 | fRadiusPlusHalfUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 184 | kFloat_GrSLType, |
| 185 | "radiusPlusHalf", |
| 186 | &radiusPlusHalfName); |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 187 | |
| joshualitt | 1598899 | 2014-10-09 15:04:05 -0700 | [diff] [blame^] | 188 | GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 189 | const char* fragmentPos = fsBuilder->fragmentPosition(); |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 190 | // At each quarter-circle corner we compute a vector that is the offset of the fragment position |
| 191 | // from the circle center. The vector is pinned in x and y to be in the quarter-plane relevant |
| 192 | // to that corner. This means that points near the interior near the rrect top edge will have |
| 193 | // a vector that points straight up for both the TL left and TR corners. Computing an |
| 194 | // alpha from this vector at either the TR or TL corner will give the correct result. Similarly, |
| 195 | // fragments near the other three edges will get the correct AA. Fragments in the interior of |
| 196 | // the rrect will have a (0,0) vector at all four corners. So long as the radius > 0.5 they will |
| 197 | // correctly produce an alpha value of 1 at all four corners. We take the min of all the alphas. |
| 198 | // The code below is a simplified version of the above that performs maxs on the vector |
| 199 | // components before computing distances and alpha values so that only one distance computation |
| 200 | // need be computed to determine the min alpha. |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 201 | // |
| 202 | // For the cases where one half of the rrect is rectangular we drop one of the x or y |
| 203 | // computations, compute a separate rect edge alpha for the rect side, and mul the two computed |
| 204 | // alphas together. |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 205 | switch (crre.getCircularCornerFlags()) { |
| 206 | case CircularRRectEffect::kAll_CornerFlags: |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 207 | fsBuilder->codeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos); |
| 208 | fsBuilder->codeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName); |
| 209 | fsBuilder->codeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n"); |
| 210 | fsBuilder->codeAppendf("\t\tfloat alpha = clamp(%s - length(dxy), 0.0, 1.0);\n", |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 211 | radiusPlusHalfName); |
| 212 | break; |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 213 | case CircularRRectEffect::kTopLeft_CornerFlag: |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 214 | fsBuilder->codeAppendf("\t\tvec2 dxy = max(%s.xy - %s.xy, 0.0);\n", |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 215 | rectName, fragmentPos); |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 216 | fsBuilder->codeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x, 0.0, 1.0);\n", |
| commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 217 | rectName, fragmentPos); |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 218 | fsBuilder->codeAppendf("\t\tfloat bottomAlpha = clamp(%s.w - %s.y, 0.0, 1.0);\n", |
| commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 219 | rectName, fragmentPos); |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 220 | fsBuilder->codeAppendf("\t\tfloat alpha = bottomAlpha * rightAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n", |
| commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 221 | radiusPlusHalfName); |
| 222 | break; |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 223 | case CircularRRectEffect::kTopRight_CornerFlag: |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 224 | fsBuilder->codeAppendf("\t\tvec2 dxy = max(vec2(%s.x - %s.z, %s.y - %s.y), 0.0);\n", |
| commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 225 | fragmentPos, rectName, rectName, fragmentPos); |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 226 | fsBuilder->codeAppendf("\t\tfloat leftAlpha = clamp(%s.x - %s.x, 0.0, 1.0);\n", |
| commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 227 | fragmentPos, rectName); |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 228 | fsBuilder->codeAppendf("\t\tfloat bottomAlpha = clamp(%s.w - %s.y, 0.0, 1.0);\n", |
| commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 229 | rectName, fragmentPos); |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 230 | fsBuilder->codeAppendf("\t\tfloat alpha = bottomAlpha * leftAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n", |
| commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 231 | radiusPlusHalfName); |
| 232 | break; |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 233 | case CircularRRectEffect::kBottomRight_CornerFlag: |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 234 | fsBuilder->codeAppendf("\t\tvec2 dxy = max(%s.xy - %s.zw, 0.0);\n", |
| commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 235 | fragmentPos, rectName); |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 236 | fsBuilder->codeAppendf("\t\tfloat leftAlpha = clamp(%s.x - %s.x, 0.0, 1.0);\n", |
| commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 237 | fragmentPos, rectName); |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 238 | fsBuilder->codeAppendf("\t\tfloat topAlpha = clamp(%s.y - %s.y, 0.0, 1.0);\n", |
| commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 239 | fragmentPos, rectName); |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 240 | fsBuilder->codeAppendf("\t\tfloat alpha = topAlpha * leftAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n", |
| commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 241 | radiusPlusHalfName); |
| 242 | break; |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 243 | case CircularRRectEffect::kBottomLeft_CornerFlag: |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 244 | fsBuilder->codeAppendf("\t\tvec2 dxy = max(vec2(%s.x - %s.x, %s.y - %s.w), 0.0);\n", |
| commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 245 | rectName, fragmentPos, fragmentPos, rectName); |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 246 | fsBuilder->codeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x, 0.0, 1.0);\n", |
| commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 247 | rectName, fragmentPos); |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 248 | fsBuilder->codeAppendf("\t\tfloat topAlpha = clamp(%s.y - %s.y, 0.0, 1.0);\n", |
| commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 249 | fragmentPos, rectName); |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 250 | fsBuilder->codeAppendf("\t\tfloat alpha = topAlpha * rightAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n", |
| commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 251 | radiusPlusHalfName); |
| 252 | break; |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 253 | case CircularRRectEffect::kLeft_CornerFlags: |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 254 | fsBuilder->codeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos); |
| 255 | fsBuilder->codeAppendf("\t\tfloat dy1 = %s.y - %s.w;\n", fragmentPos, rectName); |
| 256 | fsBuilder->codeAppend("\t\tvec2 dxy = max(vec2(dxy0.x, max(dxy0.y, dy1)), 0.0);\n"); |
| 257 | fsBuilder->codeAppendf("\t\tfloat rightAlpha = clamp(%s.z - %s.x, 0.0, 1.0);\n", |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 258 | rectName, fragmentPos); |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 259 | fsBuilder->codeAppendf("\t\tfloat alpha = rightAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n", |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 260 | radiusPlusHalfName); |
| 261 | break; |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 262 | case CircularRRectEffect::kTop_CornerFlags: |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 263 | fsBuilder->codeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos); |
| 264 | fsBuilder->codeAppendf("\t\tfloat dx1 = %s.x - %s.z;\n", fragmentPos, rectName); |
| 265 | fsBuilder->codeAppend("\t\tvec2 dxy = max(vec2(max(dxy0.x, dx1), dxy0.y), 0.0);\n"); |
| 266 | fsBuilder->codeAppendf("\t\tfloat bottomAlpha = clamp(%s.w - %s.y, 0.0, 1.0);\n", |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 267 | rectName, fragmentPos); |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 268 | fsBuilder->codeAppendf("\t\tfloat alpha = bottomAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n", |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 269 | radiusPlusHalfName); |
| 270 | break; |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 271 | case CircularRRectEffect::kRight_CornerFlags: |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 272 | fsBuilder->codeAppendf("\t\tfloat dy0 = %s.y - %s.y;\n", rectName, fragmentPos); |
| 273 | fsBuilder->codeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName); |
| 274 | fsBuilder->codeAppend("\t\tvec2 dxy = max(vec2(dxy1.x, max(dy0, dxy1.y)), 0.0);\n"); |
| 275 | fsBuilder->codeAppendf("\t\tfloat leftAlpha = clamp(%s.x - %s.x, 0.0, 1.0);\n", |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 276 | fragmentPos, rectName); |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 277 | fsBuilder->codeAppendf("\t\tfloat alpha = leftAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n", |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 278 | radiusPlusHalfName); |
| 279 | break; |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 280 | case CircularRRectEffect::kBottom_CornerFlags: |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 281 | fsBuilder->codeAppendf("\t\tfloat dx0 = %s.x - %s.x;\n", rectName, fragmentPos); |
| 282 | fsBuilder->codeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName); |
| 283 | fsBuilder->codeAppend("\t\tvec2 dxy = max(vec2(max(dx0, dxy1.x), dxy1.y), 0.0);\n"); |
| 284 | fsBuilder->codeAppendf("\t\tfloat topAlpha = clamp(%s.y - %s.y, 0.0, 1.0);\n", |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 285 | fragmentPos, rectName); |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 286 | fsBuilder->codeAppendf("\t\tfloat alpha = topAlpha * clamp(%s - length(dxy), 0.0, 1.0);\n", |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 287 | radiusPlusHalfName); |
| 288 | break; |
| 289 | } |
| skia.committer@gmail.com | 06acb58 | 2014-03-06 03:02:32 +0000 | [diff] [blame] | 290 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 291 | if (kInverseFillAA_GrProcessorEdgeType == crre.getEdgeType()) { |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 292 | fsBuilder->codeAppend("\t\talpha = 1.0 - alpha;\n"); |
| commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 293 | } |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 294 | |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 295 | fsBuilder->codeAppendf("\t\t%s = %s;\n", outputColor, |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 296 | (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str()); |
| 297 | } |
| 298 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 299 | void GLCircularRRectEffect::GenKey(const GrProcessor& processor, const GrGLCaps&, |
| 300 | GrProcessorKeyBuilder* b) { |
| 301 | const CircularRRectEffect& crre = processor.cast<CircularRRectEffect>(); |
| 302 | GR_STATIC_ASSERT(kGrProcessorEdgeTypeCnt <= 8); |
| bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 303 | b->add32((crre.getCircularCornerFlags() << 3) | crre.getEdgeType()); |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 306 | void GLCircularRRectEffect::setData(const GrGLProgramDataManager& pdman, |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 307 | const GrProcessor& processor) { |
| 308 | const CircularRRectEffect& crre = processor.cast<CircularRRectEffect>(); |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 309 | const SkRRect& rrect = crre.getRRect(); |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 310 | if (rrect != fPrevRRect) { |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 311 | SkRect rect = rrect.getBounds(); |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 312 | SkScalar radius = 0; |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 313 | switch (crre.getCircularCornerFlags()) { |
| 314 | case CircularRRectEffect::kAll_CornerFlags: |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 315 | SkASSERT(rrect.isSimpleCircular()); |
| 316 | radius = rrect.getSimpleRadii().fX; |
| commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 317 | SkASSERT(radius >= kRadiusMin); |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 318 | rect.inset(radius, radius); |
| 319 | break; |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 320 | case CircularRRectEffect::kTopLeft_CornerFlag: |
| commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 321 | radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX; |
| 322 | rect.fLeft += radius; |
| 323 | rect.fTop += radius; |
| 324 | rect.fRight += 0.5f; |
| 325 | rect.fBottom += 0.5f; |
| 326 | break; |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 327 | case CircularRRectEffect::kTopRight_CornerFlag: |
| commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 328 | radius = rrect.radii(SkRRect::kUpperRight_Corner).fX; |
| bsalomon@google.com | de9f251 | 2014-03-11 17:09:17 +0000 | [diff] [blame] | 329 | rect.fLeft -= 0.5f; |
| commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 330 | rect.fTop += radius; |
| 331 | rect.fRight -= radius; |
| 332 | rect.fBottom += 0.5f; |
| 333 | break; |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 334 | case CircularRRectEffect::kBottomRight_CornerFlag: |
| commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 335 | radius = rrect.radii(SkRRect::kLowerRight_Corner).fX; |
| bsalomon@google.com | de9f251 | 2014-03-11 17:09:17 +0000 | [diff] [blame] | 336 | rect.fLeft -= 0.5f; |
| 337 | rect.fTop -= 0.5f; |
| commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 338 | rect.fRight -= radius; |
| 339 | rect.fBottom -= radius; |
| 340 | break; |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 341 | case CircularRRectEffect::kBottomLeft_CornerFlag: |
| commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 342 | radius = rrect.radii(SkRRect::kLowerLeft_Corner).fX; |
| 343 | rect.fLeft += radius; |
| bsalomon@google.com | de9f251 | 2014-03-11 17:09:17 +0000 | [diff] [blame] | 344 | rect.fTop -= 0.5f; |
| 345 | rect.fRight += 0.5f; |
| commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 346 | rect.fBottom -= radius; |
| 347 | break; |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 348 | case CircularRRectEffect::kLeft_CornerFlags: |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 349 | radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX; |
| 350 | rect.fLeft += radius; |
| 351 | rect.fTop += radius; |
| 352 | rect.fRight += 0.5f; |
| 353 | rect.fBottom -= radius; |
| 354 | break; |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 355 | case CircularRRectEffect::kTop_CornerFlags: |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 356 | radius = rrect.radii(SkRRect::kUpperLeft_Corner).fX; |
| 357 | rect.fLeft += radius; |
| 358 | rect.fTop += radius; |
| 359 | rect.fRight -= radius; |
| 360 | rect.fBottom += 0.5f; |
| 361 | break; |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 362 | case CircularRRectEffect::kRight_CornerFlags: |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 363 | radius = rrect.radii(SkRRect::kUpperRight_Corner).fX; |
| 364 | rect.fLeft -= 0.5f; |
| 365 | rect.fTop += radius; |
| 366 | rect.fRight -= radius; |
| 367 | rect.fBottom -= radius; |
| 368 | break; |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 369 | case CircularRRectEffect::kBottom_CornerFlags: |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 370 | radius = rrect.radii(SkRRect::kLowerLeft_Corner).fX; |
| 371 | rect.fLeft += radius; |
| 372 | rect.fTop -= 0.5f; |
| 373 | rect.fRight -= radius; |
| 374 | rect.fBottom -= radius; |
| 375 | break; |
| commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 376 | default: |
| commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 377 | SkFAIL("Should have been one of the above cases."); |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 378 | } |
| kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 379 | pdman.set4f(fInnerRectUniform, rect.fLeft, rect.fTop, rect.fRight, rect.fBottom); |
| 380 | pdman.set1f(fRadiusPlusHalfUniform, radius + 0.5f); |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 381 | fPrevRRect = rrect; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | ////////////////////////////////////////////////////////////////////////////// |
| 386 | |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 387 | class GLEllipticalRRectEffect; |
| 388 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 389 | class EllipticalRRectEffect : public GrFragmentProcessor { |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 390 | public: |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 391 | static GrFragmentProcessor* Create(GrPrimitiveEdgeType, const SkRRect&); |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 392 | |
| 393 | virtual ~EllipticalRRectEffect() {}; |
| 394 | static const char* Name() { return "EllipticalRRect"; } |
| 395 | |
| 396 | const SkRRect& getRRect() const { return fRRect; } |
| 397 | |
| commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 398 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 399 | GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; } |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 400 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 401 | typedef GLEllipticalRRectEffect GLProcessor; |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 402 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 403 | virtual const GrBackendFragmentProcessorFactory& getFactory() const SK_OVERRIDE; |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 404 | |
| 405 | private: |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 406 | EllipticalRRectEffect(GrPrimitiveEdgeType, const SkRRect&); |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 407 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 408 | virtual bool onIsEqual(const GrProcessor& other) const SK_OVERRIDE; |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 409 | |
| egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 410 | virtual void onComputeInvariantOutput(InvariantOutput* inout) const SK_OVERRIDE; |
| 411 | |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 412 | SkRRect fRRect; |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 413 | GrPrimitiveEdgeType fEdgeType; |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 414 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 415 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 416 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 417 | typedef GrFragmentProcessor INHERITED; |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 418 | }; |
| 419 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 420 | GrFragmentProcessor* |
| 421 | EllipticalRRectEffect::Create(GrPrimitiveEdgeType edgeType, const SkRRect& rrect) { |
| 422 | if (kFillAA_GrProcessorEdgeType != edgeType && kInverseFillAA_GrProcessorEdgeType != edgeType) { |
| commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 423 | return NULL; |
| 424 | } |
| bsalomon | 55fad7a | 2014-07-08 07:34:20 -0700 | [diff] [blame] | 425 | return SkNEW_ARGS(EllipticalRRectEffect, (edgeType, rrect)); |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 428 | void EllipticalRRectEffect::onComputeInvariantOutput(InvariantOutput* inout) const { |
| 429 | inout->fValidFlags = 0; |
| 430 | inout->fIsSingleComponent = false; |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 433 | const GrBackendFragmentProcessorFactory& EllipticalRRectEffect::getFactory() const { |
| 434 | return GrTBackendFragmentProcessorFactory<EllipticalRRectEffect>::getInstance(); |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 435 | } |
| 436 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 437 | EllipticalRRectEffect::EllipticalRRectEffect(GrPrimitiveEdgeType edgeType, const SkRRect& rrect) |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 438 | : fRRect(rrect) |
| 439 | , fEdgeType(edgeType){ |
| 440 | this->setWillReadFragmentPosition(); |
| 441 | } |
| 442 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 443 | bool EllipticalRRectEffect::onIsEqual(const GrProcessor& other) const { |
| joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 444 | const EllipticalRRectEffect& erre = other.cast<EllipticalRRectEffect>(); |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 445 | return fEdgeType == erre.fEdgeType && fRRect == erre.fRRect; |
| 446 | } |
| 447 | |
| 448 | ////////////////////////////////////////////////////////////////////////////// |
| 449 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 450 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(EllipticalRRectEffect); |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 451 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 452 | GrFragmentProcessor* EllipticalRRectEffect::TestCreate(SkRandom* random, |
| 453 | GrContext*, |
| 454 | const GrDrawTargetCaps& caps, |
| 455 | GrTexture*[]) { |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 456 | SkScalar w = random->nextRangeScalar(20.f, 1000.f); |
| 457 | SkScalar h = random->nextRangeScalar(20.f, 1000.f); |
| commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 458 | SkVector r[4]; |
| 459 | r[SkRRect::kUpperLeft_Corner].fX = random->nextRangeF(kRadiusMin, 9.f); |
| 460 | // ensure at least one corner really is elliptical |
| 461 | do { |
| 462 | r[SkRRect::kUpperLeft_Corner].fY = random->nextRangeF(kRadiusMin, 9.f); |
| 463 | } while (r[SkRRect::kUpperLeft_Corner].fY == r[SkRRect::kUpperLeft_Corner].fX); |
| 464 | |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 465 | SkRRect rrect; |
| commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 466 | if (random->nextBool()) { |
| 467 | // half the time create a four-radii rrect. |
| 468 | r[SkRRect::kLowerRight_Corner].fX = random->nextRangeF(kRadiusMin, 9.f); |
| 469 | r[SkRRect::kLowerRight_Corner].fY = random->nextRangeF(kRadiusMin, 9.f); |
| 470 | |
| 471 | r[SkRRect::kUpperRight_Corner].fX = r[SkRRect::kLowerRight_Corner].fX; |
| 472 | r[SkRRect::kUpperRight_Corner].fY = r[SkRRect::kUpperLeft_Corner].fY; |
| 473 | |
| 474 | r[SkRRect::kLowerLeft_Corner].fX = r[SkRRect::kUpperLeft_Corner].fX; |
| 475 | r[SkRRect::kLowerLeft_Corner].fY = r[SkRRect::kLowerRight_Corner].fY; |
| 476 | |
| 477 | rrect.setRectRadii(SkRect::MakeWH(w, h), r); |
| 478 | } else { |
| 479 | rrect.setRectXY(SkRect::MakeWH(w, h), r[SkRRect::kUpperLeft_Corner].fX, |
| 480 | r[SkRRect::kUpperLeft_Corner].fY); |
| 481 | } |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 482 | GrFragmentProcessor* fp; |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 483 | do { |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 484 | GrPrimitiveEdgeType et = (GrPrimitiveEdgeType)random->nextULessThan(kGrProcessorEdgeTypeCnt); |
| 485 | fp = GrRRectEffect::Create(et, rrect); |
| 486 | } while (NULL == fp); |
| 487 | return fp; |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | ////////////////////////////////////////////////////////////////////////////// |
| 491 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 492 | class GLEllipticalRRectEffect : public GrGLFragmentProcessor { |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 493 | public: |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 494 | GLEllipticalRRectEffect(const GrBackendProcessorFactory&, const GrProcessor&); |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 495 | |
| joshualitt | 1598899 | 2014-10-09 15:04:05 -0700 | [diff] [blame^] | 496 | virtual void emitCode(GrGLFPBuilder* builder, |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 497 | const GrFragmentProcessor& effect, |
| 498 | const GrProcessorKey& key, |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 499 | const char* outputColor, |
| 500 | const char* inputColor, |
| 501 | const TransformedCoordsArray&, |
| 502 | const TextureSamplerArray&) SK_OVERRIDE; |
| 503 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 504 | static inline void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKeyBuilder*); |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 505 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 506 | virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE; |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 507 | |
| 508 | private: |
| kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 509 | GrGLProgramDataManager::UniformHandle fInnerRectUniform; |
| 510 | GrGLProgramDataManager::UniformHandle fInvRadiiSqdUniform; |
| 511 | SkRRect fPrevRRect; |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 512 | typedef GrGLFragmentProcessor INHERITED; |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 513 | }; |
| 514 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 515 | GLEllipticalRRectEffect::GLEllipticalRRectEffect(const GrBackendProcessorFactory& factory, |
| 516 | const GrProcessor& effect) |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 517 | : INHERITED (factory) { |
| 518 | fPrevRRect.setEmpty(); |
| 519 | } |
| 520 | |
| joshualitt | 1598899 | 2014-10-09 15:04:05 -0700 | [diff] [blame^] | 521 | void GLEllipticalRRectEffect::emitCode(GrGLFPBuilder* builder, |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 522 | const GrFragmentProcessor& effect, |
| 523 | const GrProcessorKey& key, |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 524 | const char* outputColor, |
| 525 | const char* inputColor, |
| 526 | const TransformedCoordsArray&, |
| 527 | const TextureSamplerArray& samplers) { |
| joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 528 | const EllipticalRRectEffect& erre = effect.cast<EllipticalRRectEffect>(); |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 529 | const char *rectName; |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 530 | // The inner rect is the rrect bounds inset by the x/y radii |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 531 | fInnerRectUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 532 | kVec4f_GrSLType, |
| 533 | "innerRect", |
| 534 | &rectName); |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 535 | |
| joshualitt | 1598899 | 2014-10-09 15:04:05 -0700 | [diff] [blame^] | 536 | GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 537 | const char* fragmentPos = fsBuilder->fragmentPosition(); |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 538 | // At each quarter-ellipse corner we compute a vector that is the offset of the fragment pos |
| 539 | // to the ellipse center. The vector is pinned in x and y to be in the quarter-plane relevant |
| 540 | // to that corner. This means that points near the interior near the rrect top edge will have |
| 541 | // a vector that points straight up for both the TL left and TR corners. Computing an |
| 542 | // alpha from this vector at either the TR or TL corner will give the correct result. Similarly, |
| 543 | // fragments near the other three edges will get the correct AA. Fragments in the interior of |
| 544 | // the rrect will have a (0,0) vector at all four corners. So long as the radii > 0.5 they will |
| 545 | // correctly produce an alpha value of 1 at all four corners. We take the min of all the alphas. |
| 546 | // The code below is a simplified version of the above that performs maxs on the vector |
| 547 | // components before computing distances and alpha values so that only one distance computation |
| 548 | // need be computed to determine the min alpha. |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 549 | fsBuilder->codeAppendf("\t\tvec2 dxy0 = %s.xy - %s.xy;\n", rectName, fragmentPos); |
| 550 | fsBuilder->codeAppendf("\t\tvec2 dxy1 = %s.xy - %s.zw;\n", fragmentPos, rectName); |
| commit-bot@chromium.org | 9615d5f | 2014-03-20 21:39:03 +0000 | [diff] [blame] | 551 | switch (erre.getRRect().getType()) { |
| 552 | case SkRRect::kSimple_Type: { |
| commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 553 | const char *invRadiiXYSqdName; |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 554 | fInvRadiiSqdUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
| commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 555 | kVec2f_GrSLType, |
| 556 | "invRadiiXY", |
| 557 | &invRadiiXYSqdName); |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 558 | fsBuilder->codeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n"); |
| commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 559 | // Z is the x/y offsets divided by squared radii. |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 560 | fsBuilder->codeAppendf("\t\tvec2 Z = dxy * %s;\n", invRadiiXYSqdName); |
| commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 561 | break; |
| 562 | } |
| commit-bot@chromium.org | 9615d5f | 2014-03-20 21:39:03 +0000 | [diff] [blame] | 563 | case SkRRect::kNinePatch_Type: { |
| commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 564 | const char *invRadiiLTRBSqdName; |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 565 | fInvRadiiSqdUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
| commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 566 | kVec4f_GrSLType, |
| 567 | "invRadiiLTRB", |
| 568 | &invRadiiLTRBSqdName); |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 569 | fsBuilder->codeAppend("\t\tvec2 dxy = max(max(dxy0, dxy1), 0.0);\n"); |
| commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 570 | // Z is the x/y offsets divided by squared radii. We only care about the (at most) one |
| 571 | // corner where both the x and y offsets are positive, hence the maxes. (The inverse |
| 572 | // squared radii will always be positive.) |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 573 | fsBuilder->codeAppendf("\t\tvec2 Z = max(max(dxy0 * %s.xy, dxy1 * %s.zw), 0.0);\n", |
| commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 574 | invRadiiLTRBSqdName, invRadiiLTRBSqdName); |
| 575 | break; |
| commit-bot@chromium.org | 9615d5f | 2014-03-20 21:39:03 +0000 | [diff] [blame] | 576 | } |
| 577 | default: |
| commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 578 | SkFAIL("RRect should always be simple or nine-patch."); |
| commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 579 | } |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 580 | // implicit is the evaluation of (x/a)^2 + (y/b)^2 - 1. |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 581 | fsBuilder->codeAppend("\t\tfloat implicit = dot(Z, dxy) - 1.0;\n"); |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 582 | // grad_dot is the squared length of the gradient of the implicit. |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 583 | fsBuilder->codeAppendf("\t\tfloat grad_dot = 4.0 * dot(Z, Z);\n"); |
| commit-bot@chromium.org | 1b035d8 | 2014-04-09 17:11:09 +0000 | [diff] [blame] | 584 | // avoid calling inversesqrt on zero. |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 585 | fsBuilder->codeAppend("\t\tgrad_dot = max(grad_dot, 1.0e-4);\n"); |
| 586 | fsBuilder->codeAppendf("\t\tfloat approx_dist = implicit * inversesqrt(grad_dot);\n"); |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 587 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 588 | if (kFillAA_GrProcessorEdgeType == erre.getEdgeType()) { |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 589 | fsBuilder->codeAppend("\t\tfloat alpha = clamp(0.5 - approx_dist, 0.0, 1.0);\n"); |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 590 | } else { |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 591 | fsBuilder->codeAppend("\t\tfloat alpha = clamp(0.5 + approx_dist, 0.0, 1.0);\n"); |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 592 | } |
| 593 | |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 594 | fsBuilder->codeAppendf("\t\t%s = %s;\n", outputColor, |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 595 | (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str()); |
| 596 | } |
| 597 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 598 | void GLEllipticalRRectEffect::GenKey(const GrProcessor& effect, const GrGLCaps&, |
| 599 | GrProcessorKeyBuilder* b) { |
| joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 600 | const EllipticalRRectEffect& erre = effect.cast<EllipticalRRectEffect>(); |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 601 | GR_STATIC_ASSERT(kLast_GrProcessorEdgeType < (1 << 3)); |
| bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 602 | b->add32(erre.getRRect().getType() | erre.getEdgeType() << 3); |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 605 | void GLEllipticalRRectEffect::setData(const GrGLProgramDataManager& pdman, |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 606 | const GrProcessor& effect) { |
| joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 607 | const EllipticalRRectEffect& erre = effect.cast<EllipticalRRectEffect>(); |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 608 | const SkRRect& rrect = erre.getRRect(); |
| 609 | if (rrect != fPrevRRect) { |
| 610 | SkRect rect = rrect.getBounds(); |
| commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 611 | const SkVector& r0 = rrect.radii(SkRRect::kUpperLeft_Corner); |
| commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 612 | SkASSERT(r0.fX >= kRadiusMin); |
| 613 | SkASSERT(r0.fY >= kRadiusMin); |
| commit-bot@chromium.org | 9615d5f | 2014-03-20 21:39:03 +0000 | [diff] [blame] | 614 | switch (erre.getRRect().getType()) { |
| 615 | case SkRRect::kSimple_Type: |
| commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 616 | rect.inset(r0.fX, r0.fY); |
| kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 617 | pdman.set2f(fInvRadiiSqdUniform, 1.f / (r0.fX * r0.fX), |
| commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 618 | 1.f / (r0.fY * r0.fY)); |
| 619 | break; |
| commit-bot@chromium.org | 9615d5f | 2014-03-20 21:39:03 +0000 | [diff] [blame] | 620 | case SkRRect::kNinePatch_Type: { |
| commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 621 | const SkVector& r1 = rrect.radii(SkRRect::kLowerRight_Corner); |
| commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 622 | SkASSERT(r1.fX >= kRadiusMin); |
| 623 | SkASSERT(r1.fY >= kRadiusMin); |
| commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 624 | rect.fLeft += r0.fX; |
| 625 | rect.fTop += r0.fY; |
| 626 | rect.fRight -= r1.fX; |
| 627 | rect.fBottom -= r1.fY; |
| kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 628 | pdman.set4f(fInvRadiiSqdUniform, 1.f / (r0.fX * r0.fX), |
| commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 629 | 1.f / (r0.fY * r0.fY), |
| 630 | 1.f / (r1.fX * r1.fX), |
| 631 | 1.f / (r1.fY * r1.fY)); |
| 632 | break; |
| 633 | } |
| commit-bot@chromium.org | 9615d5f | 2014-03-20 21:39:03 +0000 | [diff] [blame] | 634 | default: |
| commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 635 | SkFAIL("RRect should always be simple or nine-patch."); |
| commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 636 | } |
| kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 637 | pdman.set4f(fInnerRectUniform, rect.fLeft, rect.fTop, rect.fRight, rect.fBottom); |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 638 | fPrevRRect = rrect; |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | ////////////////////////////////////////////////////////////////////////////// |
| 643 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 644 | GrFragmentProcessor* GrRRectEffect::Create(GrPrimitiveEdgeType edgeType, const SkRRect& rrect) { |
| commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 645 | if (rrect.isRect()) { |
| 646 | return GrConvexPolyEffect::Create(edgeType, rrect.getBounds()); |
| 647 | } |
| 648 | |
| commit-bot@chromium.org | 3eedb80 | 2014-03-28 15:58:31 +0000 | [diff] [blame] | 649 | if (rrect.isOval()) { |
| 650 | return GrOvalEffect::Create(edgeType, rrect.getBounds()); |
| 651 | } |
| 652 | |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 653 | if (rrect.isSimple()) { |
| commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 654 | if (rrect.getSimpleRadii().fX < kRadiusMin || rrect.getSimpleRadii().fY < kRadiusMin) { |
| 655 | // In this case the corners are extremely close to rectangular and we collapse the |
| 656 | // clip to a rectangular clip. |
| 657 | return GrConvexPolyEffect::Create(edgeType, rrect.getBounds()); |
| 658 | } |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 659 | if (rrect.getSimpleRadii().fX == rrect.getSimpleRadii().fY) { |
| skia.committer@gmail.com | 6e4eb21 | 2014-03-25 03:02:32 +0000 | [diff] [blame] | 660 | return CircularRRectEffect::Create(edgeType, CircularRRectEffect::kAll_CornerFlags, |
| commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 661 | rrect); |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 662 | } else { |
| commit-bot@chromium.org | 9615d5f | 2014-03-20 21:39:03 +0000 | [diff] [blame] | 663 | return EllipticalRRectEffect::Create(edgeType, rrect); |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 664 | } |
| commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | if (rrect.isComplex() || rrect.isNinePatch()) { |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 668 | // Check for the "tab" cases - two adjacent circular corners and two square corners. |
| commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 669 | SkScalar circularRadius = 0; |
| 670 | uint32_t cornerFlags = 0; |
| 671 | |
| 672 | SkVector radii[4]; |
| 673 | bool squashedRadii = false; |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 674 | for (int c = 0; c < 4; ++c) { |
| commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 675 | radii[c] = rrect.radii((SkRRect::Corner)c); |
| 676 | SkASSERT((0 == radii[c].fX) == (0 == radii[c].fY)); |
| 677 | if (0 == radii[c].fX) { |
| 678 | // The corner is square, so no need to squash or flag as circular. |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 679 | continue; |
| 680 | } |
| commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 681 | if (radii[c].fX < kRadiusMin || radii[c].fY < kRadiusMin) { |
| 682 | radii[c].set(0, 0); |
| 683 | squashedRadii = true; |
| 684 | continue; |
| 685 | } |
| 686 | if (radii[c].fX != radii[c].fY) { |
| bsalomon@google.com | 44a435b | 2014-03-13 19:20:32 +0000 | [diff] [blame] | 687 | cornerFlags = ~0U; |
| commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 688 | break; |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 689 | } |
| commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 690 | if (!cornerFlags) { |
| commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 691 | circularRadius = radii[c].fX; |
| commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 692 | cornerFlags = 1 << c; |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 693 | } else { |
| commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 694 | if (radii[c].fX != circularRadius) { |
| bsalomon@google.com | 44a435b | 2014-03-13 19:20:32 +0000 | [diff] [blame] | 695 | cornerFlags = ~0U; |
| commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 696 | break; |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 697 | } |
| commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 698 | cornerFlags |= 1 << c; |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 699 | } |
| 700 | } |
| 701 | |
| commit-bot@chromium.org | dd58422 | 2014-03-10 16:08:22 +0000 | [diff] [blame] | 702 | switch (cornerFlags) { |
| commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 703 | case CircularRRectEffect::kAll_CornerFlags: |
| 704 | // This rrect should have been caught in the simple case above. Though, it would |
| 705 | // be correctly handled in the fallthrough code. |
| 706 | SkASSERT(false); |
| commit-bot@chromium.org | 4355f21 | 2014-03-12 15:32:50 +0000 | [diff] [blame] | 707 | case CircularRRectEffect::kTopLeft_CornerFlag: |
| 708 | case CircularRRectEffect::kTopRight_CornerFlag: |
| 709 | case CircularRRectEffect::kBottomRight_CornerFlag: |
| 710 | case CircularRRectEffect::kBottomLeft_CornerFlag: |
| 711 | case CircularRRectEffect::kLeft_CornerFlags: |
| 712 | case CircularRRectEffect::kTop_CornerFlags: |
| 713 | case CircularRRectEffect::kRight_CornerFlags: |
| commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 714 | case CircularRRectEffect::kBottom_CornerFlags: { |
| 715 | SkTCopyOnFirstWrite<SkRRect> rr(rrect); |
| 716 | if (squashedRadii) { |
| 717 | rr.writable()->setRectRadii(rrect.getBounds(), radii); |
| 718 | } |
| 719 | return CircularRRectEffect::Create(edgeType, cornerFlags, *rr); |
| 720 | } |
| 721 | case CircularRRectEffect::kNone_CornerFlags: |
| 722 | return GrConvexPolyEffect::Create(edgeType, rrect.getBounds()); |
| 723 | default: { |
| 724 | if (squashedRadii) { |
| 725 | // If we got here then we squashed some but not all the radii to zero. (If all |
| 726 | // had been squashed cornerFlags would be 0.) The elliptical effect doesn't |
| 727 | // support some rounded and some square corners. |
| 728 | return NULL; |
| 729 | } |
| commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 730 | if (rrect.isNinePatch()) { |
| commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 731 | return EllipticalRRectEffect::Create(edgeType, rrect); |
| commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 732 | } |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 733 | return NULL; |
| commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 734 | } |
| commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 735 | } |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 736 | } |
| commit-bot@chromium.org | 2a8be90 | 2014-03-24 19:24:59 +0000 | [diff] [blame] | 737 | |
| 738 | return NULL; |
| commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 739 | } |