robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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" |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 9 | #include "GrAARectRenderer.h" |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 10 | #include "GrGpu.h" |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 11 | #include "gl/GrGLEffect.h" |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 12 | #include "gl/GrGLVertexEffect.h" |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 13 | #include "GrTBackendEffectFactory.h" |
robertphillips@google.com | 908aed8 | 2013-05-28 13:16:20 +0000 | [diff] [blame] | 14 | #include "SkColorPriv.h" |
commit-bot@chromium.org | 234d4fb | 2013-09-30 19:55:49 +0000 | [diff] [blame] | 15 | #include "effects/GrVertexEffect.h" |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 16 | |
robertphillips@google.com | b19cb7f | 2013-05-02 15:37:20 +0000 | [diff] [blame] | 17 | /////////////////////////////////////////////////////////////////////////////// |
| 18 | class GrGLAlignedRectEffect; |
| 19 | |
| 20 | // Axis Aligned special case |
commit-bot@chromium.org | 234d4fb | 2013-09-30 19:55:49 +0000 | [diff] [blame] | 21 | class GrAlignedRectEffect : public GrVertexEffect { |
robertphillips@google.com | b19cb7f | 2013-05-02 15:37:20 +0000 | [diff] [blame] | 22 | public: |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 23 | static GrEffect* Create() { |
robertphillips@google.com | b19cb7f | 2013-05-02 15:37:20 +0000 | [diff] [blame] | 24 | GR_CREATE_STATIC_EFFECT(gAlignedRectEffect, GrAlignedRectEffect, ()); |
| 25 | gAlignedRectEffect->ref(); |
| 26 | return gAlignedRectEffect; |
| 27 | } |
| 28 | |
| 29 | virtual ~GrAlignedRectEffect() {} |
| 30 | |
| 31 | static const char* Name() { return "AlignedRectEdge"; } |
| 32 | |
| 33 | virtual void getConstantColorComponents(GrColor* color, |
| 34 | uint32_t* validFlags) const SK_OVERRIDE { |
| 35 | *validFlags = 0; |
| 36 | } |
| 37 | |
| 38 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
| 39 | return GrTBackendEffectFactory<GrAlignedRectEffect>::getInstance(); |
| 40 | } |
| 41 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 42 | class GLEffect : public GrGLVertexEffect { |
robertphillips@google.com | b19cb7f | 2013-05-02 15:37:20 +0000 | [diff] [blame] | 43 | public: |
| 44 | GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) |
| 45 | : INHERITED (factory) {} |
| 46 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 47 | virtual void emitCode(GrGLFullProgramBuilder* builder, |
robertphillips@google.com | b19cb7f | 2013-05-02 15:37:20 +0000 | [diff] [blame] | 48 | const GrDrawEffect& drawEffect, |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 49 | const GrEffectKey& key, |
robertphillips@google.com | b19cb7f | 2013-05-02 15:37:20 +0000 | [diff] [blame] | 50 | const char* outputColor, |
| 51 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 52 | const TransformedCoordsArray&, |
robertphillips@google.com | b19cb7f | 2013-05-02 15:37:20 +0000 | [diff] [blame] | 53 | const TextureSamplerArray& samplers) SK_OVERRIDE { |
| 54 | // setup the varying for the Axis aligned rect effect |
| 55 | // xy -> interpolated offset |
| 56 | // zw -> w/2+0.5, h/2+0.5 |
| 57 | const char *vsRectName, *fsRectName; |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 58 | builder->addVarying(kVec4f_GrSLType, "Rect", &vsRectName, &fsRectName); |
robertphillips@google.com | b19cb7f | 2013-05-02 15:37:20 +0000 | [diff] [blame] | 59 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 60 | GrGLVertexShaderBuilder* vsBuilder = builder->getVertexShaderBuilder(); |
| 61 | const SkString* attr0Name = |
| 62 | vsBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]); |
| 63 | vsBuilder->codeAppendf("\t%s = %s;\n", vsRectName, attr0Name->c_str()); |
| 64 | |
| 65 | GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
commit-bot@chromium.org | 99e0d08 | 2013-06-14 14:58:50 +0000 | [diff] [blame] | 66 | // TODO: compute all these offsets, spans, and scales in the VS |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 67 | fsBuilder->codeAppendf("\tfloat insetW = min(1.0, %s.z) - 0.5;\n", fsRectName); |
| 68 | fsBuilder->codeAppendf("\tfloat insetH = min(1.0, %s.w) - 0.5;\n", fsRectName); |
| 69 | fsBuilder->codeAppend("\tfloat outset = 0.5;\n"); |
commit-bot@chromium.org | 99e0d08 | 2013-06-14 14:58:50 +0000 | [diff] [blame] | 70 | // For rects > 1 pixel wide and tall the span's are noops (i.e., 1.0). For rects |
| 71 | // < 1 pixel wide or tall they serve to normalize the < 1 ramp to a 0 .. 1 range. |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 72 | fsBuilder->codeAppend("\tfloat spanW = insetW + outset;\n"); |
| 73 | fsBuilder->codeAppend("\tfloat spanH = insetH + outset;\n"); |
commit-bot@chromium.org | 99e0d08 | 2013-06-14 14:58:50 +0000 | [diff] [blame] | 74 | // For rects < 1 pixel wide or tall, these scale factors are used to cap the maximum |
| 75 | // value of coverage that is used. In other words it is the coverage that is |
| 76 | // used in the interior of the rect after the ramp. |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 77 | fsBuilder->codeAppend("\tfloat scaleW = min(1.0, 2.0*insetW/spanW);\n"); |
| 78 | fsBuilder->codeAppend("\tfloat scaleH = min(1.0, 2.0*insetH/spanH);\n"); |
robertphillips@google.com | b19cb7f | 2013-05-02 15:37:20 +0000 | [diff] [blame] | 79 | |
| 80 | // Compute the coverage for the rect's width |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 81 | fsBuilder->codeAppendf( |
commit-bot@chromium.org | 99e0d08 | 2013-06-14 14:58:50 +0000 | [diff] [blame] | 82 | "\tfloat coverage = scaleW*clamp((%s.z-abs(%s.x))/spanW, 0.0, 1.0);\n", fsRectName, |
| 83 | fsRectName); |
| 84 | // Compute the coverage for the rect's height and merge with the width |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 85 | fsBuilder->codeAppendf( |
egdaniel@google.com | f1d7de7 | 2013-06-14 19:25:53 +0000 | [diff] [blame] | 86 | "\tcoverage = coverage*scaleH*clamp((%s.w-abs(%s.y))/spanH, 0.0, 1.0);\n", |
| 87 | fsRectName, fsRectName); |
robertphillips@google.com | b19cb7f | 2013-05-02 15:37:20 +0000 | [diff] [blame] | 88 | |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 89 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 90 | fsBuilder->codeAppendf("\t%s = %s;\n", outputColor, |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 91 | (GrGLSLExpr4(inputColor) * GrGLSLExpr1("coverage")).c_str()); |
robertphillips@google.com | b19cb7f | 2013-05-02 15:37:20 +0000 | [diff] [blame] | 92 | } |
| 93 | |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 94 | static void GenKey(const GrDrawEffect&, const GrGLCaps&, GrEffectKeyBuilder*) {} |
robertphillips@google.com | b19cb7f | 2013-05-02 15:37:20 +0000 | [diff] [blame] | 95 | |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 96 | virtual void setData(const GrGLProgramDataManager& pdman, const GrDrawEffect&) SK_OVERRIDE {} |
robertphillips@google.com | b19cb7f | 2013-05-02 15:37:20 +0000 | [diff] [blame] | 97 | |
| 98 | private: |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 99 | typedef GrGLVertexEffect INHERITED; |
robertphillips@google.com | b19cb7f | 2013-05-02 15:37:20 +0000 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | |
| 103 | private: |
commit-bot@chromium.org | 234d4fb | 2013-09-30 19:55:49 +0000 | [diff] [blame] | 104 | GrAlignedRectEffect() : GrVertexEffect() { |
robertphillips@google.com | b19cb7f | 2013-05-02 15:37:20 +0000 | [diff] [blame] | 105 | this->addVertexAttrib(kVec4f_GrSLType); |
| 106 | } |
| 107 | |
| 108 | virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE { return true; } |
| 109 | |
| 110 | GR_DECLARE_EFFECT_TEST; |
| 111 | |
commit-bot@chromium.org | 234d4fb | 2013-09-30 19:55:49 +0000 | [diff] [blame] | 112 | typedef GrVertexEffect INHERITED; |
robertphillips@google.com | b19cb7f | 2013-05-02 15:37:20 +0000 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | |
| 116 | GR_DEFINE_EFFECT_TEST(GrAlignedRectEffect); |
| 117 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 118 | GrEffect* GrAlignedRectEffect::TestCreate(SkRandom* random, |
| 119 | GrContext* context, |
| 120 | const GrDrawTargetCaps&, |
| 121 | GrTexture* textures[]) { |
robertphillips@google.com | b19cb7f | 2013-05-02 15:37:20 +0000 | [diff] [blame] | 122 | return GrAlignedRectEffect::Create(); |
| 123 | } |
| 124 | |
| 125 | /////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 126 | class GrGLRectEffect; |
| 127 | |
| 128 | /** |
skia.committer@gmail.com | 07d3a65 | 2013-04-10 07:01:15 +0000 | [diff] [blame] | 129 | * The output of this effect is a modulation of the input color and coverage |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 130 | * for an arbitrarily oriented rect. The rect is specified as: |
| 131 | * Center of the rect |
| 132 | * Unit vector point down the height of the rect |
| 133 | * Half width + 0.5 |
| 134 | * Half height + 0.5 |
| 135 | * The center and vector are stored in a vec4 varying ("RectEdge") with the |
| 136 | * center in the xy components and the vector in the zw components. |
| 137 | * The munged width and height are stored in a vec2 varying ("WidthHeight") |
| 138 | * with the width in x and the height in y. |
| 139 | */ |
commit-bot@chromium.org | 234d4fb | 2013-09-30 19:55:49 +0000 | [diff] [blame] | 140 | class GrRectEffect : public GrVertexEffect { |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 141 | public: |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 142 | static GrEffect* Create() { |
bsalomon@google.com | d42aca3 | 2013-04-23 15:37:27 +0000 | [diff] [blame] | 143 | GR_CREATE_STATIC_EFFECT(gRectEffect, GrRectEffect, ()); |
| 144 | gRectEffect->ref(); |
| 145 | return gRectEffect; |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | virtual ~GrRectEffect() {} |
| 149 | |
| 150 | static const char* Name() { return "RectEdge"; } |
| 151 | |
skia.committer@gmail.com | 07d3a65 | 2013-04-10 07:01:15 +0000 | [diff] [blame] | 152 | virtual void getConstantColorComponents(GrColor* color, |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 153 | uint32_t* validFlags) const SK_OVERRIDE { |
| 154 | *validFlags = 0; |
| 155 | } |
| 156 | |
| 157 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
| 158 | return GrTBackendEffectFactory<GrRectEffect>::getInstance(); |
| 159 | } |
| 160 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 161 | class GLEffect : public GrGLVertexEffect { |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 162 | public: |
| 163 | GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) |
| 164 | : INHERITED (factory) {} |
| 165 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 166 | virtual void emitCode(GrGLFullProgramBuilder* builder, |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 167 | const GrDrawEffect& drawEffect, |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 168 | const GrEffectKey& key, |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 169 | const char* outputColor, |
| 170 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 171 | const TransformedCoordsArray&, |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 172 | const TextureSamplerArray& samplers) SK_OVERRIDE { |
| 173 | // setup the varying for the center point and the unit vector |
| 174 | // that points down the height of the rect |
| 175 | const char *vsRectEdgeName, *fsRectEdgeName; |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 176 | builder->addVarying(kVec4f_GrSLType, "RectEdge", |
| 177 | &vsRectEdgeName, &fsRectEdgeName); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 178 | |
| 179 | GrGLVertexShaderBuilder* vsBuilder = builder->getVertexShaderBuilder(); |
skia.committer@gmail.com | 07d3a65 | 2013-04-10 07:01:15 +0000 | [diff] [blame] | 180 | const SkString* attr0Name = |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 181 | vsBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]); |
| 182 | vsBuilder->codeAppendf("\t%s = %s;\n", vsRectEdgeName, attr0Name->c_str()); |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 183 | |
| 184 | // setup the varying for width/2+.5 and height/2+.5 |
| 185 | const char *vsWidthHeightName, *fsWidthHeightName; |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 186 | builder->addVarying(kVec2f_GrSLType, "WidthHeight", |
| 187 | &vsWidthHeightName, &fsWidthHeightName); |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 188 | const SkString* attr1Name = |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 189 | vsBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[1]); |
| 190 | vsBuilder->codeAppendf("\t%s = %s;\n", vsWidthHeightName, attr1Name->c_str()); |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 191 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 192 | GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
egdaniel@google.com | f1d7de7 | 2013-06-14 19:25:53 +0000 | [diff] [blame] | 193 | // TODO: compute all these offsets, spans, and scales in the VS |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 194 | fsBuilder->codeAppendf("\tfloat insetW = min(1.0, %s.x) - 0.5;\n", fsWidthHeightName); |
| 195 | fsBuilder->codeAppendf("\tfloat insetH = min(1.0, %s.y) - 0.5;\n", fsWidthHeightName); |
| 196 | fsBuilder->codeAppend("\tfloat outset = 0.5;\n"); |
egdaniel@google.com | f1d7de7 | 2013-06-14 19:25:53 +0000 | [diff] [blame] | 197 | // For rects > 1 pixel wide and tall the span's are noops (i.e., 1.0). For rects |
| 198 | // < 1 pixel wide or tall they serve to normalize the < 1 ramp to a 0 .. 1 range. |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 199 | fsBuilder->codeAppend("\tfloat spanW = insetW + outset;\n"); |
| 200 | fsBuilder->codeAppend("\tfloat spanH = insetH + outset;\n"); |
egdaniel@google.com | f1d7de7 | 2013-06-14 19:25:53 +0000 | [diff] [blame] | 201 | // For rects < 1 pixel wide or tall, these scale factors are used to cap the maximum |
| 202 | // value of coverage that is used. In other words it is the coverage that is |
| 203 | // used in the interior of the rect after the ramp. |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 204 | fsBuilder->codeAppend("\tfloat scaleW = min(1.0, 2.0*insetW/spanW);\n"); |
| 205 | fsBuilder->codeAppend("\tfloat scaleH = min(1.0, 2.0*insetH/spanH);\n"); |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 206 | |
| 207 | // Compute the coverage for the rect's width |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 208 | fsBuilder->codeAppendf("\tvec2 offset = %s.xy - %s.xy;\n", |
| 209 | fsBuilder->fragmentPosition(), fsRectEdgeName); |
| 210 | fsBuilder->codeAppendf("\tfloat perpDot = abs(offset.x * %s.w - offset.y * %s.z);\n", |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 211 | fsRectEdgeName, fsRectEdgeName); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 212 | fsBuilder->codeAppendf( |
egdaniel@google.com | f1d7de7 | 2013-06-14 19:25:53 +0000 | [diff] [blame] | 213 | "\tfloat coverage = scaleW*clamp((%s.x-perpDot)/spanW, 0.0, 1.0);\n", |
| 214 | fsWidthHeightName); |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 215 | |
| 216 | // Compute the coverage for the rect's height and merge with the width |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 217 | fsBuilder->codeAppendf("\tperpDot = abs(dot(offset, %s.zw));\n", |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 218 | fsRectEdgeName); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 219 | fsBuilder->codeAppendf( |
egdaniel@google.com | f1d7de7 | 2013-06-14 19:25:53 +0000 | [diff] [blame] | 220 | "\tcoverage = coverage*scaleH*clamp((%s.y-perpDot)/spanH, 0.0, 1.0);\n", |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 221 | fsWidthHeightName); |
| 222 | |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 223 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 224 | fsBuilder->codeAppendf("\t%s = %s;\n", outputColor, |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 225 | (GrGLSLExpr4(inputColor) * GrGLSLExpr1("coverage")).c_str()); |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 226 | } |
| 227 | |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 228 | static void GenKey(const GrDrawEffect&, const GrGLCaps&, GrEffectKeyBuilder*) {} |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 229 | |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 230 | virtual void setData(const GrGLProgramDataManager& pdman, const GrDrawEffect&) SK_OVERRIDE {} |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 231 | |
| 232 | private: |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 233 | typedef GrGLVertexEffect INHERITED; |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 234 | }; |
| 235 | |
| 236 | |
| 237 | private: |
commit-bot@chromium.org | 234d4fb | 2013-09-30 19:55:49 +0000 | [diff] [blame] | 238 | GrRectEffect() : GrVertexEffect() { |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 239 | this->addVertexAttrib(kVec4f_GrSLType); |
| 240 | this->addVertexAttrib(kVec2f_GrSLType); |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 241 | this->setWillReadFragmentPosition(); |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE { return true; } |
| 245 | |
| 246 | GR_DECLARE_EFFECT_TEST; |
| 247 | |
commit-bot@chromium.org | 234d4fb | 2013-09-30 19:55:49 +0000 | [diff] [blame] | 248 | typedef GrVertexEffect INHERITED; |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 249 | }; |
| 250 | |
| 251 | |
| 252 | GR_DEFINE_EFFECT_TEST(GrRectEffect); |
| 253 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 254 | GrEffect* GrRectEffect::TestCreate(SkRandom* random, |
| 255 | GrContext* context, |
| 256 | const GrDrawTargetCaps&, |
| 257 | GrTexture* textures[]) { |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 258 | return GrRectEffect::Create(); |
| 259 | } |
| 260 | |
| 261 | /////////////////////////////////////////////////////////////////////////////// |
| 262 | |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 263 | namespace { |
bsalomon | 9c0822a | 2014-08-11 11:07:48 -0700 | [diff] [blame] | 264 | extern const GrVertexAttrib gAARectAttribs[] = { |
| 265 | {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, |
| 266 | {kVec4ub_GrVertexAttribType, sizeof(SkPoint), kColor_GrVertexAttribBinding}, |
| 267 | {kVec4ub_GrVertexAttribType, sizeof(SkPoint) + sizeof(SkColor), kCoverage_GrVertexAttribBinding}, |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 268 | }; |
| 269 | |
bsalomon | c30aaa0 | 2014-08-13 07:15:29 -0700 | [diff] [blame] | 270 | // Should the coverage be multiplied into the color attrib or use a separate attrib. |
| 271 | enum CoverageAttribType { |
| 272 | kUseColor_CoverageAttribType, |
| 273 | kUseCoverage_CoverageAttribType, |
| 274 | }; |
| 275 | } |
| 276 | |
| 277 | static CoverageAttribType set_rect_attribs(GrDrawState* drawState) { |
| 278 | if (drawState->canTweakAlphaForCoverage()) { |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 279 | drawState->setVertexAttribs<gAARectAttribs>(2); |
bsalomon | c30aaa0 | 2014-08-13 07:15:29 -0700 | [diff] [blame] | 280 | return kUseColor_CoverageAttribType; |
| 281 | } else { |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 282 | drawState->setVertexAttribs<gAARectAttribs>(3); |
bsalomon | c30aaa0 | 2014-08-13 07:15:29 -0700 | [diff] [blame] | 283 | return kUseCoverage_CoverageAttribType; |
| 284 | } |
| 285 | } |
| 286 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 287 | static void set_inset_fan(SkPoint* pts, size_t stride, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 288 | const SkRect& r, SkScalar dx, SkScalar dy) { |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 289 | pts->setRectFan(r.fLeft + dx, r.fTop + dy, |
| 290 | r.fRight - dx, r.fBottom - dy, stride); |
| 291 | } |
| 292 | |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 293 | void GrAARectRenderer::reset() { |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 294 | SkSafeSetNull(fAAFillRectIndexBuffer); |
commit-bot@chromium.org | 6006d0f | 2013-11-06 10:08:21 +0000 | [diff] [blame] | 295 | SkSafeSetNull(fAAMiterStrokeRectIndexBuffer); |
| 296 | SkSafeSetNull(fAABevelStrokeRectIndexBuffer); |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 297 | } |
| 298 | |
robertphillips@google.com | 6d06730 | 2012-12-18 21:47:47 +0000 | [diff] [blame] | 299 | static const uint16_t gFillAARectIdx[] = { |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 300 | 0, 1, 5, 5, 4, 0, |
| 301 | 1, 2, 6, 6, 5, 1, |
| 302 | 2, 3, 7, 7, 6, 2, |
| 303 | 3, 0, 4, 4, 7, 3, |
| 304 | 4, 5, 6, 6, 7, 4, |
| 305 | }; |
| 306 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 307 | static const int kIndicesPerAAFillRect = SK_ARRAY_COUNT(gFillAARectIdx); |
robertphillips@google.com | 6d06730 | 2012-12-18 21:47:47 +0000 | [diff] [blame] | 308 | static const int kVertsPerAAFillRect = 8; |
| 309 | static const int kNumAAFillRectsInIndexBuffer = 256; |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 310 | |
| 311 | GrIndexBuffer* GrAARectRenderer::aaFillRectIndexBuffer(GrGpu* gpu) { |
robertphillips@google.com | 6d06730 | 2012-12-18 21:47:47 +0000 | [diff] [blame] | 312 | static const size_t kAAFillRectIndexBufferSize = kIndicesPerAAFillRect * |
| 313 | sizeof(uint16_t) * |
| 314 | kNumAAFillRectsInIndexBuffer; |
| 315 | |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 316 | if (NULL == fAAFillRectIndexBuffer) { |
robertphillips@google.com | 6d06730 | 2012-12-18 21:47:47 +0000 | [diff] [blame] | 317 | fAAFillRectIndexBuffer = gpu->createIndexBuffer(kAAFillRectIndexBufferSize, false); |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 318 | if (NULL != fAAFillRectIndexBuffer) { |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 319 | uint16_t* data = (uint16_t*) fAAFillRectIndexBuffer->map(); |
robertphillips@google.com | 6d06730 | 2012-12-18 21:47:47 +0000 | [diff] [blame] | 320 | bool useTempData = (NULL == data); |
| 321 | if (useTempData) { |
| 322 | data = SkNEW_ARRAY(uint16_t, kNumAAFillRectsInIndexBuffer * kIndicesPerAAFillRect); |
| 323 | } |
| 324 | for (int i = 0; i < kNumAAFillRectsInIndexBuffer; ++i) { |
| 325 | // Each AA filled rect is drawn with 8 vertices and 10 triangles (8 around |
| 326 | // the inner rect (for AA) and 2 for the inner rect. |
| 327 | int baseIdx = i * kIndicesPerAAFillRect; |
| 328 | uint16_t baseVert = (uint16_t)(i * kVertsPerAAFillRect); |
| 329 | for (int j = 0; j < kIndicesPerAAFillRect; ++j) { |
| 330 | data[baseIdx+j] = baseVert + gFillAARectIdx[j]; |
| 331 | } |
| 332 | } |
| 333 | if (useTempData) { |
| 334 | if (!fAAFillRectIndexBuffer->updateData(data, kAAFillRectIndexBufferSize)) { |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 335 | SkFAIL("Can't get AA Fill Rect indices into buffer!"); |
robertphillips@google.com | 6d06730 | 2012-12-18 21:47:47 +0000 | [diff] [blame] | 336 | } |
| 337 | SkDELETE_ARRAY(data); |
| 338 | } else { |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 339 | fAAFillRectIndexBuffer->unmap(); |
robertphillips@google.com | 6d06730 | 2012-12-18 21:47:47 +0000 | [diff] [blame] | 340 | } |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 341 | } |
| 342 | } |
robertphillips@google.com | 6d06730 | 2012-12-18 21:47:47 +0000 | [diff] [blame] | 343 | |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 344 | return fAAFillRectIndexBuffer; |
| 345 | } |
| 346 | |
commit-bot@chromium.org | 6006d0f | 2013-11-06 10:08:21 +0000 | [diff] [blame] | 347 | static const uint16_t gMiterStrokeAARectIdx[] = { |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 348 | 0 + 0, 1 + 0, 5 + 0, 5 + 0, 4 + 0, 0 + 0, |
| 349 | 1 + 0, 2 + 0, 6 + 0, 6 + 0, 5 + 0, 1 + 0, |
| 350 | 2 + 0, 3 + 0, 7 + 0, 7 + 0, 6 + 0, 2 + 0, |
| 351 | 3 + 0, 0 + 0, 4 + 0, 4 + 0, 7 + 0, 3 + 0, |
| 352 | |
| 353 | 0 + 4, 1 + 4, 5 + 4, 5 + 4, 4 + 4, 0 + 4, |
| 354 | 1 + 4, 2 + 4, 6 + 4, 6 + 4, 5 + 4, 1 + 4, |
| 355 | 2 + 4, 3 + 4, 7 + 4, 7 + 4, 6 + 4, 2 + 4, |
| 356 | 3 + 4, 0 + 4, 4 + 4, 4 + 4, 7 + 4, 3 + 4, |
| 357 | |
| 358 | 0 + 8, 1 + 8, 5 + 8, 5 + 8, 4 + 8, 0 + 8, |
| 359 | 1 + 8, 2 + 8, 6 + 8, 6 + 8, 5 + 8, 1 + 8, |
| 360 | 2 + 8, 3 + 8, 7 + 8, 7 + 8, 6 + 8, 2 + 8, |
| 361 | 3 + 8, 0 + 8, 4 + 8, 4 + 8, 7 + 8, 3 + 8, |
| 362 | }; |
| 363 | |
commit-bot@chromium.org | 6006d0f | 2013-11-06 10:08:21 +0000 | [diff] [blame] | 364 | /** |
| 365 | * As in miter-stroke, index = a + b, and a is the current index, b is the shift |
| 366 | * from the first index. The index layout: |
| 367 | * outer AA line: 0~3, 4~7 |
| 368 | * outer edge: 8~11, 12~15 |
| 369 | * inner edge: 16~19 |
| 370 | * inner AA line: 20~23 |
| 371 | * Following comes a bevel-stroke rect and its indices: |
| 372 | * |
| 373 | * 4 7 |
skia.committer@gmail.com | 2614418 | 2013-11-07 07:02:19 +0000 | [diff] [blame] | 374 | * ********************************* |
commit-bot@chromium.org | 6006d0f | 2013-11-06 10:08:21 +0000 | [diff] [blame] | 375 | * * ______________________________ * |
| 376 | * * / 12 15 \ * |
| 377 | * * / \ * |
| 378 | * 0 * |8 16_____________________19 11 | * 3 |
| 379 | * * | | | | * |
| 380 | * * | | **************** | | * |
| 381 | * * | | * 20 23 * | | * |
| 382 | * * | | * * | | * |
| 383 | * * | | * 21 22 * | | * |
| 384 | * * | | **************** | | * |
| 385 | * * | |____________________| | * |
| 386 | * 1 * |9 17 18 10| * 2 |
skia.committer@gmail.com | 2614418 | 2013-11-07 07:02:19 +0000 | [diff] [blame] | 387 | * * \ / * |
commit-bot@chromium.org | 6006d0f | 2013-11-06 10:08:21 +0000 | [diff] [blame] | 388 | * * \13 __________________________14/ * |
| 389 | * * * |
skia.committer@gmail.com | 2614418 | 2013-11-07 07:02:19 +0000 | [diff] [blame] | 390 | * ********************************** |
commit-bot@chromium.org | 6006d0f | 2013-11-06 10:08:21 +0000 | [diff] [blame] | 391 | * 5 6 |
| 392 | */ |
| 393 | static const uint16_t gBevelStrokeAARectIdx[] = { |
| 394 | // Draw outer AA, from outer AA line to outer edge, shift is 0. |
| 395 | 0 + 0, 1 + 0, 9 + 0, 9 + 0, 8 + 0, 0 + 0, |
| 396 | 1 + 0, 5 + 0, 13 + 0, 13 + 0, 9 + 0, 1 + 0, |
| 397 | 5 + 0, 6 + 0, 14 + 0, 14 + 0, 13 + 0, 5 + 0, |
| 398 | 6 + 0, 2 + 0, 10 + 0, 10 + 0, 14 + 0, 6 + 0, |
| 399 | 2 + 0, 3 + 0, 11 + 0, 11 + 0, 10 + 0, 2 + 0, |
| 400 | 3 + 0, 7 + 0, 15 + 0, 15 + 0, 11 + 0, 3 + 0, |
| 401 | 7 + 0, 4 + 0, 12 + 0, 12 + 0, 15 + 0, 7 + 0, |
| 402 | 4 + 0, 0 + 0, 8 + 0, 8 + 0, 12 + 0, 4 + 0, |
| 403 | |
| 404 | // Draw the stroke, from outer edge to inner edge, shift is 8. |
| 405 | 0 + 8, 1 + 8, 9 + 8, 9 + 8, 8 + 8, 0 + 8, |
| 406 | 1 + 8, 5 + 8, 9 + 8, |
| 407 | 5 + 8, 6 + 8, 10 + 8, 10 + 8, 9 + 8, 5 + 8, |
| 408 | 6 + 8, 2 + 8, 10 + 8, |
| 409 | 2 + 8, 3 + 8, 11 + 8, 11 + 8, 10 + 8, 2 + 8, |
| 410 | 3 + 8, 7 + 8, 11 + 8, |
| 411 | 7 + 8, 4 + 8, 8 + 8, 8 + 8, 11 + 8, 7 + 8, |
| 412 | 4 + 8, 0 + 8, 8 + 8, |
| 413 | |
| 414 | // Draw the inner AA, from inner edge to inner AA line, shift is 16. |
| 415 | 0 + 16, 1 + 16, 5 + 16, 5 + 16, 4 + 16, 0 + 16, |
| 416 | 1 + 16, 2 + 16, 6 + 16, 6 + 16, 5 + 16, 1 + 16, |
| 417 | 2 + 16, 3 + 16, 7 + 16, 7 + 16, 6 + 16, 2 + 16, |
| 418 | 3 + 16, 0 + 16, 4 + 16, 4 + 16, 7 + 16, 3 + 16, |
| 419 | }; |
| 420 | |
| 421 | int GrAARectRenderer::aaStrokeRectIndexCount(bool miterStroke) { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 422 | return miterStroke ? SK_ARRAY_COUNT(gMiterStrokeAARectIdx) : |
| 423 | SK_ARRAY_COUNT(gBevelStrokeAARectIdx); |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 424 | } |
| 425 | |
commit-bot@chromium.org | 6006d0f | 2013-11-06 10:08:21 +0000 | [diff] [blame] | 426 | GrIndexBuffer* GrAARectRenderer::aaStrokeRectIndexBuffer(GrGpu* gpu, bool miterStroke) { |
| 427 | if (miterStroke) { |
| 428 | if (NULL == fAAMiterStrokeRectIndexBuffer) { |
| 429 | fAAMiterStrokeRectIndexBuffer = |
| 430 | gpu->createIndexBuffer(sizeof(gMiterStrokeAARectIdx), false); |
| 431 | if (NULL != fAAMiterStrokeRectIndexBuffer) { |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 432 | #ifdef SK_DEBUG |
commit-bot@chromium.org | 6006d0f | 2013-11-06 10:08:21 +0000 | [diff] [blame] | 433 | bool updated = |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 434 | #endif |
commit-bot@chromium.org | 6006d0f | 2013-11-06 10:08:21 +0000 | [diff] [blame] | 435 | fAAMiterStrokeRectIndexBuffer->updateData(gMiterStrokeAARectIdx, |
| 436 | sizeof(gMiterStrokeAARectIdx)); |
| 437 | GR_DEBUGASSERT(updated); |
| 438 | } |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 439 | } |
commit-bot@chromium.org | 6006d0f | 2013-11-06 10:08:21 +0000 | [diff] [blame] | 440 | return fAAMiterStrokeRectIndexBuffer; |
| 441 | } else { |
| 442 | if (NULL == fAABevelStrokeRectIndexBuffer) { |
| 443 | fAABevelStrokeRectIndexBuffer = |
| 444 | gpu->createIndexBuffer(sizeof(gBevelStrokeAARectIdx), false); |
| 445 | if (NULL != fAABevelStrokeRectIndexBuffer) { |
| 446 | #ifdef SK_DEBUG |
| 447 | bool updated = |
| 448 | #endif |
| 449 | fAABevelStrokeRectIndexBuffer->updateData(gBevelStrokeAARectIdx, |
| 450 | sizeof(gBevelStrokeAARectIdx)); |
| 451 | GR_DEBUGASSERT(updated); |
| 452 | } |
| 453 | } |
| 454 | return fAABevelStrokeRectIndexBuffer; |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 455 | } |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 456 | } |
| 457 | |
robertphillips@google.com | b19cb7f | 2013-05-02 15:37:20 +0000 | [diff] [blame] | 458 | void GrAARectRenderer::geometryFillAARect(GrGpu* gpu, |
| 459 | GrDrawTarget* target, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 460 | const SkRect& rect, |
robertphillips@google.com | 4b140b5 | 2013-05-02 17:13:13 +0000 | [diff] [blame] | 461 | const SkMatrix& combinedMatrix, |
bsalomon | 9c0822a | 2014-08-11 11:07:48 -0700 | [diff] [blame] | 462 | const SkRect& devRect) { |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 463 | GrDrawState* drawState = target->drawState(); |
| 464 | |
bsalomon | 9c0822a | 2014-08-11 11:07:48 -0700 | [diff] [blame] | 465 | GrColor color = drawState->getColor(); |
| 466 | |
bsalomon | c30aaa0 | 2014-08-13 07:15:29 -0700 | [diff] [blame] | 467 | CoverageAttribType covAttribType = set_rect_attribs(drawState); |
| 468 | if (kUseCoverage_CoverageAttribType == covAttribType && GrColorIsOpaque(color)) { |
bsalomon | 9c0822a | 2014-08-11 11:07:48 -0700 | [diff] [blame] | 469 | drawState->setHint(GrDrawState::kVertexColorsAreOpaque_Hint, true); |
| 470 | } |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 471 | |
jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 472 | GrDrawTarget::AutoReleaseGeometry geo(target, 8, 0); |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 473 | if (!geo.succeeded()) { |
| 474 | GrPrintf("Failed to get space for vertices!\n"); |
| 475 | return; |
| 476 | } |
robertphillips@google.com | 6d06730 | 2012-12-18 21:47:47 +0000 | [diff] [blame] | 477 | |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 478 | GrIndexBuffer* indexBuffer = this->aaFillRectIndexBuffer(gpu); |
| 479 | if (NULL == indexBuffer) { |
| 480 | GrPrintf("Failed to create index buffer!\n"); |
| 481 | return; |
| 482 | } |
| 483 | |
| 484 | intptr_t verts = reinterpret_cast<intptr_t>(geo.vertices()); |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 485 | size_t vsize = drawState->getVertexSize(); |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 486 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 487 | SkPoint* fan0Pos = reinterpret_cast<SkPoint*>(verts); |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 488 | SkPoint* fan1Pos = reinterpret_cast<SkPoint*>(verts + 4 * vsize); |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 489 | |
robertphillips@google.com | 908aed8 | 2013-05-28 13:16:20 +0000 | [diff] [blame] | 490 | SkScalar inset = SkMinScalar(devRect.width(), SK_Scalar1); |
| 491 | inset = SK_ScalarHalf * SkMinScalar(inset, devRect.height()); |
| 492 | |
robertphillips@google.com | 4b140b5 | 2013-05-02 17:13:13 +0000 | [diff] [blame] | 493 | if (combinedMatrix.rectStaysRect()) { |
robertphillips@google.com | afd1cba | 2013-05-14 19:47:47 +0000 | [diff] [blame] | 494 | // Temporarily #if'ed out. We don't want to pass in the devRect but |
| 495 | // right now it is computed in GrContext::apply_aa_to_rect and we don't |
| 496 | // want to throw away the work |
| 497 | #if 0 |
robertphillips@google.com | 91b7116 | 2013-05-10 14:09:54 +0000 | [diff] [blame] | 498 | SkRect devRect; |
| 499 | combinedMatrix.mapRect(&devRect, rect); |
robertphillips@google.com | afd1cba | 2013-05-14 19:47:47 +0000 | [diff] [blame] | 500 | #endif |
robertphillips@google.com | 91b7116 | 2013-05-10 14:09:54 +0000 | [diff] [blame] | 501 | |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 502 | set_inset_fan(fan0Pos, vsize, devRect, -SK_ScalarHalf, -SK_ScalarHalf); |
| 503 | set_inset_fan(fan1Pos, vsize, devRect, inset, inset); |
robertphillips@google.com | 4b140b5 | 2013-05-02 17:13:13 +0000 | [diff] [blame] | 504 | } else { |
| 505 | // compute transformed (1, 0) and (0, 1) vectors |
| 506 | SkVector vec[2] = { |
| 507 | { combinedMatrix[SkMatrix::kMScaleX], combinedMatrix[SkMatrix::kMSkewY] }, |
| 508 | { combinedMatrix[SkMatrix::kMSkewX], combinedMatrix[SkMatrix::kMScaleY] } |
| 509 | }; |
| 510 | |
| 511 | vec[0].normalize(); |
| 512 | vec[0].scale(SK_ScalarHalf); |
| 513 | vec[1].normalize(); |
| 514 | vec[1].scale(SK_ScalarHalf); |
| 515 | |
robertphillips@google.com | 91b7116 | 2013-05-10 14:09:54 +0000 | [diff] [blame] | 516 | // create the rotated rect |
robertphillips@google.com | 4b140b5 | 2013-05-02 17:13:13 +0000 | [diff] [blame] | 517 | fan0Pos->setRectFan(rect.fLeft, rect.fTop, |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 518 | rect.fRight, rect.fBottom, vsize); |
| 519 | combinedMatrix.mapPointsWithStride(fan0Pos, vsize, 4); |
robertphillips@google.com | 4b140b5 | 2013-05-02 17:13:13 +0000 | [diff] [blame] | 520 | |
robertphillips@google.com | 91b7116 | 2013-05-10 14:09:54 +0000 | [diff] [blame] | 521 | // Now create the inset points and then outset the original |
| 522 | // rotated points |
| 523 | |
robertphillips@google.com | 4b140b5 | 2013-05-02 17:13:13 +0000 | [diff] [blame] | 524 | // TL |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 525 | *((SkPoint*)((intptr_t)fan1Pos + 0 * vsize)) = |
| 526 | *((SkPoint*)((intptr_t)fan0Pos + 0 * vsize)) + vec[0] + vec[1]; |
| 527 | *((SkPoint*)((intptr_t)fan0Pos + 0 * vsize)) -= vec[0] + vec[1]; |
robertphillips@google.com | 4b140b5 | 2013-05-02 17:13:13 +0000 | [diff] [blame] | 528 | // BL |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 529 | *((SkPoint*)((intptr_t)fan1Pos + 1 * vsize)) = |
| 530 | *((SkPoint*)((intptr_t)fan0Pos + 1 * vsize)) + vec[0] - vec[1]; |
| 531 | *((SkPoint*)((intptr_t)fan0Pos + 1 * vsize)) -= vec[0] - vec[1]; |
robertphillips@google.com | 4b140b5 | 2013-05-02 17:13:13 +0000 | [diff] [blame] | 532 | // BR |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 533 | *((SkPoint*)((intptr_t)fan1Pos + 2 * vsize)) = |
| 534 | *((SkPoint*)((intptr_t)fan0Pos + 2 * vsize)) - vec[0] - vec[1]; |
| 535 | *((SkPoint*)((intptr_t)fan0Pos + 2 * vsize)) += vec[0] + vec[1]; |
robertphillips@google.com | 4b140b5 | 2013-05-02 17:13:13 +0000 | [diff] [blame] | 536 | // TR |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 537 | *((SkPoint*)((intptr_t)fan1Pos + 3 * vsize)) = |
| 538 | *((SkPoint*)((intptr_t)fan0Pos + 3 * vsize)) - vec[0] + vec[1]; |
| 539 | *((SkPoint*)((intptr_t)fan0Pos + 3 * vsize)) += vec[0] - vec[1]; |
robertphillips@google.com | 4b140b5 | 2013-05-02 17:13:13 +0000 | [diff] [blame] | 540 | } |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 541 | |
bsalomon | 9c0822a | 2014-08-11 11:07:48 -0700 | [diff] [blame] | 542 | // Make verts point to vertex color and then set all the color and coverage vertex attrs values. |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 543 | verts += sizeof(SkPoint); |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 544 | for (int i = 0; i < 4; ++i) { |
bsalomon | c30aaa0 | 2014-08-13 07:15:29 -0700 | [diff] [blame] | 545 | if (kUseCoverage_CoverageAttribType == covAttribType) { |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 546 | *reinterpret_cast<GrColor*>(verts + i * vsize) = color; |
| 547 | *reinterpret_cast<GrColor*>(verts + i * vsize + sizeof(GrColor)) = 0; |
bsalomon | c30aaa0 | 2014-08-13 07:15:29 -0700 | [diff] [blame] | 548 | } else { |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 549 | *reinterpret_cast<GrColor*>(verts + i * vsize) = 0; |
bsalomon | c30aaa0 | 2014-08-13 07:15:29 -0700 | [diff] [blame] | 550 | } |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 551 | } |
| 552 | |
robertphillips@google.com | 908aed8 | 2013-05-28 13:16:20 +0000 | [diff] [blame] | 553 | int scale; |
| 554 | if (inset < SK_ScalarHalf) { |
| 555 | scale = SkScalarFloorToInt(512.0f * inset / (inset + SK_ScalarHalf)); |
| 556 | SkASSERT(scale >= 0 && scale <= 255); |
| 557 | } else { |
| 558 | scale = 0xff; |
| 559 | } |
| 560 | |
bsalomon | 9c0822a | 2014-08-11 11:07:48 -0700 | [diff] [blame] | 561 | GrColor innerCoverage; |
bsalomon | c30aaa0 | 2014-08-13 07:15:29 -0700 | [diff] [blame] | 562 | if (kUseCoverage_CoverageAttribType == covAttribType) { |
| 563 | innerCoverage = GrColorPackRGBA(scale, scale, scale, scale); |
| 564 | } else { |
| 565 | innerCoverage = (0xff == scale) ? color : SkAlphaMulQ(color, scale); |
| 566 | } |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 567 | verts += 4 * vsize; |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 568 | for (int i = 0; i < 4; ++i) { |
bsalomon | c30aaa0 | 2014-08-13 07:15:29 -0700 | [diff] [blame] | 569 | if (kUseCoverage_CoverageAttribType == covAttribType) { |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 570 | *reinterpret_cast<GrColor*>(verts + i * vsize) = color; |
| 571 | *reinterpret_cast<GrColor*>(verts + i * vsize + sizeof(GrColor)) = innerCoverage; |
bsalomon | c30aaa0 | 2014-08-13 07:15:29 -0700 | [diff] [blame] | 572 | } else { |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 573 | *reinterpret_cast<GrColor*>(verts + i * vsize) = innerCoverage; |
bsalomon | c30aaa0 | 2014-08-13 07:15:29 -0700 | [diff] [blame] | 574 | } |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | target->setIndexSourceToBuffer(indexBuffer); |
robertphillips@google.com | 6d06730 | 2012-12-18 21:47:47 +0000 | [diff] [blame] | 578 | target->drawIndexedInstances(kTriangles_GrPrimitiveType, 1, |
| 579 | kVertsPerAAFillRect, |
| 580 | kIndicesPerAAFillRect); |
bsalomon@google.com | 0406b9e | 2013-04-02 21:00:15 +0000 | [diff] [blame] | 581 | target->resetIndexSource(); |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 582 | } |
| 583 | |
robertphillips@google.com | b19cb7f | 2013-05-02 15:37:20 +0000 | [diff] [blame] | 584 | namespace { |
| 585 | |
| 586 | // Rotated |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 587 | struct RectVertex { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 588 | SkPoint fPos; |
| 589 | SkPoint fCenter; |
| 590 | SkPoint fDir; |
| 591 | SkPoint fWidthHeight; |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 592 | }; |
| 593 | |
robertphillips@google.com | b19cb7f | 2013-05-02 15:37:20 +0000 | [diff] [blame] | 594 | // Rotated |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 595 | extern const GrVertexAttrib gAARectVertexAttribs[] = { |
| 596 | { kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding }, |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 597 | { kVec4f_GrVertexAttribType, sizeof(SkPoint), kEffect_GrVertexAttribBinding }, |
| 598 | { kVec2f_GrVertexAttribType, 3*sizeof(SkPoint), kEffect_GrVertexAttribBinding } |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 599 | }; |
| 600 | |
robertphillips@google.com | b19cb7f | 2013-05-02 15:37:20 +0000 | [diff] [blame] | 601 | // Axis Aligned |
| 602 | struct AARectVertex { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 603 | SkPoint fPos; |
| 604 | SkPoint fOffset; |
| 605 | SkPoint fWidthHeight; |
robertphillips@google.com | b19cb7f | 2013-05-02 15:37:20 +0000 | [diff] [blame] | 606 | }; |
| 607 | |
| 608 | // Axis Aligned |
| 609 | extern const GrVertexAttrib gAAAARectVertexAttribs[] = { |
| 610 | { kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding }, |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 611 | { kVec4f_GrVertexAttribType, sizeof(SkPoint), kEffect_GrVertexAttribBinding }, |
robertphillips@google.com | b19cb7f | 2013-05-02 15:37:20 +0000 | [diff] [blame] | 612 | }; |
| 613 | |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 614 | }; |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 615 | |
| 616 | void GrAARectRenderer::shaderFillAARect(GrGpu* gpu, |
| 617 | GrDrawTarget* target, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 618 | const SkRect& rect, |
robertphillips@google.com | 114eb9e | 2013-05-10 13:16:13 +0000 | [diff] [blame] | 619 | const SkMatrix& combinedMatrix) { |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 620 | GrDrawState* drawState = target->drawState(); |
| 621 | |
| 622 | SkPoint center = SkPoint::Make(rect.centerX(), rect.centerY()); |
| 623 | combinedMatrix.mapPoints(¢er, 1); |
| 624 | |
| 625 | // compute transformed (0, 1) vector |
| 626 | SkVector dir = { combinedMatrix[SkMatrix::kMSkewX], combinedMatrix[SkMatrix::kMScaleY] }; |
| 627 | dir.normalize(); |
| 628 | |
| 629 | // compute transformed (width, 0) and (0, height) vectors |
| 630 | SkVector vec[2] = { |
robertphillips@google.com | b19cb7f | 2013-05-02 15:37:20 +0000 | [diff] [blame] | 631 | { combinedMatrix[SkMatrix::kMScaleX], combinedMatrix[SkMatrix::kMSkewY] }, |
| 632 | { combinedMatrix[SkMatrix::kMSkewX], combinedMatrix[SkMatrix::kMScaleY] } |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 633 | }; |
| 634 | |
robertphillips@google.com | b19cb7f | 2013-05-02 15:37:20 +0000 | [diff] [blame] | 635 | SkScalar newWidth = SkScalarHalf(rect.width() * vec[0].length()) + SK_ScalarHalf; |
| 636 | SkScalar newHeight = SkScalarHalf(rect.height() * vec[1].length()) + SK_ScalarHalf; |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 637 | drawState->setVertexAttribs<gAARectVertexAttribs>(SK_ARRAY_COUNT(gAARectVertexAttribs)); |
| 638 | SkASSERT(sizeof(RectVertex) == drawState->getVertexSize()); |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 639 | |
| 640 | GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0); |
| 641 | if (!geo.succeeded()) { |
| 642 | GrPrintf("Failed to get space for vertices!\n"); |
| 643 | return; |
| 644 | } |
| 645 | |
| 646 | RectVertex* verts = reinterpret_cast<RectVertex*>(geo.vertices()); |
| 647 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 648 | GrEffect* effect = GrRectEffect::Create(); |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 649 | static const int kRectAttrIndex = 1; |
| 650 | static const int kWidthIndex = 2; |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 651 | drawState->addCoverageEffect(effect, kRectAttrIndex, kWidthIndex)->unref(); |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 652 | |
| 653 | for (int i = 0; i < 4; ++i) { |
| 654 | verts[i].fCenter = center; |
| 655 | verts[i].fDir = dir; |
| 656 | verts[i].fWidthHeight.fX = newWidth; |
| 657 | verts[i].fWidthHeight.fY = newHeight; |
| 658 | } |
| 659 | |
robertphillips@google.com | 114eb9e | 2013-05-10 13:16:13 +0000 | [diff] [blame] | 660 | SkRect devRect; |
| 661 | combinedMatrix.mapRect(&devRect, rect); |
| 662 | |
robertphillips@google.com | df3695e | 2013-04-09 14:01:44 +0000 | [diff] [blame] | 663 | SkRect devBounds = { |
| 664 | devRect.fLeft - SK_ScalarHalf, |
| 665 | devRect.fTop - SK_ScalarHalf, |
| 666 | devRect.fRight + SK_ScalarHalf, |
| 667 | devRect.fBottom + SK_ScalarHalf |
| 668 | }; |
| 669 | |
| 670 | verts[0].fPos = SkPoint::Make(devBounds.fLeft, devBounds.fTop); |
| 671 | verts[1].fPos = SkPoint::Make(devBounds.fLeft, devBounds.fBottom); |
| 672 | verts[2].fPos = SkPoint::Make(devBounds.fRight, devBounds.fBottom); |
| 673 | verts[3].fPos = SkPoint::Make(devBounds.fRight, devBounds.fTop); |
| 674 | |
| 675 | target->setIndexSourceToBuffer(gpu->getContext()->getQuadIndexBuffer()); |
| 676 | target->drawIndexedInstances(kTriangles_GrPrimitiveType, 1, 4, 6); |
| 677 | target->resetIndexSource(); |
| 678 | } |
| 679 | |
robertphillips@google.com | b19cb7f | 2013-05-02 15:37:20 +0000 | [diff] [blame] | 680 | void GrAARectRenderer::shaderFillAlignedAARect(GrGpu* gpu, |
| 681 | GrDrawTarget* target, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 682 | const SkRect& rect, |
robertphillips@google.com | 114eb9e | 2013-05-10 13:16:13 +0000 | [diff] [blame] | 683 | const SkMatrix& combinedMatrix) { |
robertphillips@google.com | b19cb7f | 2013-05-02 15:37:20 +0000 | [diff] [blame] | 684 | GrDrawState* drawState = target->drawState(); |
| 685 | SkASSERT(combinedMatrix.rectStaysRect()); |
| 686 | |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 687 | drawState->setVertexAttribs<gAAAARectVertexAttribs>(SK_ARRAY_COUNT(gAAAARectVertexAttribs)); |
| 688 | SkASSERT(sizeof(AARectVertex) == drawState->getVertexSize()); |
robertphillips@google.com | b19cb7f | 2013-05-02 15:37:20 +0000 | [diff] [blame] | 689 | |
| 690 | GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0); |
| 691 | if (!geo.succeeded()) { |
| 692 | GrPrintf("Failed to get space for vertices!\n"); |
| 693 | return; |
| 694 | } |
| 695 | |
| 696 | AARectVertex* verts = reinterpret_cast<AARectVertex*>(geo.vertices()); |
| 697 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 698 | GrEffect* effect = GrAlignedRectEffect::Create(); |
robertphillips@google.com | b19cb7f | 2013-05-02 15:37:20 +0000 | [diff] [blame] | 699 | static const int kOffsetIndex = 1; |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 700 | drawState->addCoverageEffect(effect, kOffsetIndex)->unref(); |
robertphillips@google.com | b19cb7f | 2013-05-02 15:37:20 +0000 | [diff] [blame] | 701 | |
robertphillips@google.com | 114eb9e | 2013-05-10 13:16:13 +0000 | [diff] [blame] | 702 | SkRect devRect; |
| 703 | combinedMatrix.mapRect(&devRect, rect); |
| 704 | |
robertphillips@google.com | b19cb7f | 2013-05-02 15:37:20 +0000 | [diff] [blame] | 705 | SkRect devBounds = { |
| 706 | devRect.fLeft - SK_ScalarHalf, |
| 707 | devRect.fTop - SK_ScalarHalf, |
| 708 | devRect.fRight + SK_ScalarHalf, |
| 709 | devRect.fBottom + SK_ScalarHalf |
| 710 | }; |
| 711 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 712 | SkPoint widthHeight = { |
robertphillips@google.com | b19cb7f | 2013-05-02 15:37:20 +0000 | [diff] [blame] | 713 | SkScalarHalf(devRect.width()) + SK_ScalarHalf, |
| 714 | SkScalarHalf(devRect.height()) + SK_ScalarHalf |
| 715 | }; |
| 716 | |
| 717 | verts[0].fPos = SkPoint::Make(devBounds.fLeft, devBounds.fTop); |
| 718 | verts[0].fOffset = SkPoint::Make(-widthHeight.fX, -widthHeight.fY); |
| 719 | verts[0].fWidthHeight = widthHeight; |
| 720 | |
| 721 | verts[1].fPos = SkPoint::Make(devBounds.fLeft, devBounds.fBottom); |
| 722 | verts[1].fOffset = SkPoint::Make(-widthHeight.fX, widthHeight.fY); |
| 723 | verts[1].fWidthHeight = widthHeight; |
| 724 | |
| 725 | verts[2].fPos = SkPoint::Make(devBounds.fRight, devBounds.fBottom); |
| 726 | verts[2].fOffset = widthHeight; |
| 727 | verts[2].fWidthHeight = widthHeight; |
| 728 | |
| 729 | verts[3].fPos = SkPoint::Make(devBounds.fRight, devBounds.fTop); |
| 730 | verts[3].fOffset = SkPoint::Make(widthHeight.fX, -widthHeight.fY); |
| 731 | verts[3].fWidthHeight = widthHeight; |
| 732 | |
| 733 | target->setIndexSourceToBuffer(gpu->getContext()->getQuadIndexBuffer()); |
| 734 | target->drawIndexedInstances(kTriangles_GrPrimitiveType, 1, 4, 6); |
| 735 | target->resetIndexSource(); |
| 736 | } |
| 737 | |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 738 | void GrAARectRenderer::strokeAARect(GrGpu* gpu, |
bsalomon@google.com | e7249bd | 2012-08-16 15:28:54 +0000 | [diff] [blame] | 739 | GrDrawTarget* target, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 740 | const SkRect& rect, |
robertphillips@google.com | 18136d1 | 2013-05-10 11:05:58 +0000 | [diff] [blame] | 741 | const SkMatrix& combinedMatrix, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 742 | const SkRect& devRect, |
bsalomon | 9c0822a | 2014-08-11 11:07:48 -0700 | [diff] [blame] | 743 | const SkStrokeRec& stroke) { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 744 | SkVector devStrokeSize; |
egdaniel | d58a0ba | 2014-06-11 10:30:05 -0700 | [diff] [blame] | 745 | SkScalar width = stroke.getWidth(); |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 746 | if (width > 0) { |
| 747 | devStrokeSize.set(width, width); |
| 748 | combinedMatrix.mapVectors(&devStrokeSize, 1); |
| 749 | devStrokeSize.setAbs(devStrokeSize); |
| 750 | } else { |
| 751 | devStrokeSize.set(SK_Scalar1, SK_Scalar1); |
| 752 | } |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 753 | |
robertphillips@google.com | 18136d1 | 2013-05-10 11:05:58 +0000 | [diff] [blame] | 754 | const SkScalar dx = devStrokeSize.fX; |
| 755 | const SkScalar dy = devStrokeSize.fY; |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 756 | const SkScalar rx = SkScalarMul(dx, SK_ScalarHalf); |
| 757 | const SkScalar ry = SkScalarMul(dy, SK_ScalarHalf); |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 758 | |
robertphillips@google.com | afd1cba | 2013-05-14 19:47:47 +0000 | [diff] [blame] | 759 | // Temporarily #if'ed out. We don't want to pass in the devRect but |
| 760 | // right now it is computed in GrContext::apply_aa_to_rect and we don't |
| 761 | // want to throw away the work |
| 762 | #if 0 |
robertphillips@google.com | 18136d1 | 2013-05-10 11:05:58 +0000 | [diff] [blame] | 763 | SkRect devRect; |
| 764 | combinedMatrix.mapRect(&devRect, rect); |
robertphillips@google.com | afd1cba | 2013-05-14 19:47:47 +0000 | [diff] [blame] | 765 | #endif |
robertphillips@google.com | 18136d1 | 2013-05-10 11:05:58 +0000 | [diff] [blame] | 766 | |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 767 | SkScalar spare; |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 768 | { |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 769 | SkScalar w = devRect.width() - dx; |
| 770 | SkScalar h = devRect.height() - dy; |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 771 | spare = SkTMin(w, h); |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 772 | } |
| 773 | |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 774 | SkRect devOutside(devRect); |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 775 | devOutside.outset(rx, ry); |
| 776 | |
commit-bot@chromium.org | 6006d0f | 2013-11-06 10:08:21 +0000 | [diff] [blame] | 777 | bool miterStroke = true; |
yunchao.he | 2bff230 | 2014-07-28 19:18:49 -0700 | [diff] [blame] | 778 | // For hairlines, make bevel and round joins appear the same as mitered ones. |
commit-bot@chromium.org | 6006d0f | 2013-11-06 10:08:21 +0000 | [diff] [blame] | 779 | // small miter limit means right angles show bevel... |
yunchao.he | 2bff230 | 2014-07-28 19:18:49 -0700 | [diff] [blame] | 780 | if ((width > 0) && (stroke.getJoin() != SkPaint::kMiter_Join || |
| 781 | stroke.getMiter() < SK_ScalarSqrt2)) { |
commit-bot@chromium.org | 6006d0f | 2013-11-06 10:08:21 +0000 | [diff] [blame] | 782 | miterStroke = false; |
| 783 | } |
| 784 | |
| 785 | if (spare <= 0 && miterStroke) { |
bsalomon | 9c0822a | 2014-08-11 11:07:48 -0700 | [diff] [blame] | 786 | this->fillAARect(gpu, target, devOutside, SkMatrix::I(), devOutside); |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 787 | return; |
| 788 | } |
skia.committer@gmail.com | f140f18 | 2013-03-02 07:01:56 +0000 | [diff] [blame] | 789 | |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 790 | SkRect devInside(devRect); |
| 791 | devInside.inset(rx, ry); |
| 792 | |
commit-bot@chromium.org | 6006d0f | 2013-11-06 10:08:21 +0000 | [diff] [blame] | 793 | SkRect devOutsideAssist(devRect); |
| 794 | |
| 795 | // For bevel-stroke, use 2 SkRect instances(devOutside and devOutsideAssist) |
| 796 | // to draw the outer of the rect. Because there are 8 vertices on the outer |
skia.committer@gmail.com | 2614418 | 2013-11-07 07:02:19 +0000 | [diff] [blame] | 797 | // edge, while vertex number of inner edge is 4, the same as miter-stroke. |
commit-bot@chromium.org | 6006d0f | 2013-11-06 10:08:21 +0000 | [diff] [blame] | 798 | if (!miterStroke) { |
| 799 | devOutside.inset(0, ry); |
| 800 | devOutsideAssist.outset(0, ry); |
| 801 | } |
| 802 | |
bsalomon | 9c0822a | 2014-08-11 11:07:48 -0700 | [diff] [blame] | 803 | this->geometryStrokeAARect(gpu, target, devOutside, devOutsideAssist, devInside, miterStroke); |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | void GrAARectRenderer::geometryStrokeAARect(GrGpu* gpu, |
| 807 | GrDrawTarget* target, |
| 808 | const SkRect& devOutside, |
commit-bot@chromium.org | 6006d0f | 2013-11-06 10:08:21 +0000 | [diff] [blame] | 809 | const SkRect& devOutsideAssist, |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 810 | const SkRect& devInside, |
commit-bot@chromium.org | 6006d0f | 2013-11-06 10:08:21 +0000 | [diff] [blame] | 811 | bool miterStroke) { |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 812 | GrDrawState* drawState = target->drawState(); |
| 813 | |
bsalomon | c30aaa0 | 2014-08-13 07:15:29 -0700 | [diff] [blame] | 814 | CoverageAttribType covAttribType = set_rect_attribs(drawState); |
bsalomon | 9c0822a | 2014-08-11 11:07:48 -0700 | [diff] [blame] | 815 | |
| 816 | GrColor color = drawState->getColor(); |
bsalomon | c30aaa0 | 2014-08-13 07:15:29 -0700 | [diff] [blame] | 817 | if (kUseCoverage_CoverageAttribType == covAttribType && GrColorIsOpaque(color)) { |
bsalomon | 9c0822a | 2014-08-11 11:07:48 -0700 | [diff] [blame] | 818 | drawState->setHint(GrDrawState::kVertexColorsAreOpaque_Hint, true); |
| 819 | } |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 820 | |
commit-bot@chromium.org | 6006d0f | 2013-11-06 10:08:21 +0000 | [diff] [blame] | 821 | int innerVertexNum = 4; |
| 822 | int outerVertexNum = miterStroke ? 4 : 8; |
| 823 | int totalVertexNum = (outerVertexNum + innerVertexNum) * 2; |
| 824 | |
| 825 | GrDrawTarget::AutoReleaseGeometry geo(target, totalVertexNum, 0); |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 826 | if (!geo.succeeded()) { |
| 827 | GrPrintf("Failed to get space for vertices!\n"); |
| 828 | return; |
| 829 | } |
commit-bot@chromium.org | 6006d0f | 2013-11-06 10:08:21 +0000 | [diff] [blame] | 830 | GrIndexBuffer* indexBuffer = this->aaStrokeRectIndexBuffer(gpu, miterStroke); |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 831 | if (NULL == indexBuffer) { |
| 832 | GrPrintf("Failed to create index buffer!\n"); |
| 833 | return; |
| 834 | } |
| 835 | |
| 836 | intptr_t verts = reinterpret_cast<intptr_t>(geo.vertices()); |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 837 | size_t vsize = drawState->getVertexSize(); |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 838 | |
bsalomon@google.com | e7249bd | 2012-08-16 15:28:54 +0000 | [diff] [blame] | 839 | // We create vertices for four nested rectangles. There are two ramps from 0 to full |
| 840 | // coverage, one on the exterior of the stroke and the other on the interior. |
| 841 | // The following pointers refer to the four rects, from outermost to innermost. |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 842 | SkPoint* fan0Pos = reinterpret_cast<SkPoint*>(verts); |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 843 | SkPoint* fan1Pos = reinterpret_cast<SkPoint*>(verts + outerVertexNum * vsize); |
| 844 | SkPoint* fan2Pos = reinterpret_cast<SkPoint*>(verts + 2 * outerVertexNum * vsize); |
| 845 | SkPoint* fan3Pos = reinterpret_cast<SkPoint*>(verts + (2 * outerVertexNum + innerVertexNum) * vsize); |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 846 | |
robertphillips@google.com | c111ce2 | 2013-07-01 13:10:10 +0000 | [diff] [blame] | 847 | #ifndef SK_IGNORE_THIN_STROKED_RECT_FIX |
robertphillips@google.com | 353f097 | 2013-06-28 17:57:06 +0000 | [diff] [blame] | 848 | // TODO: this only really works if the X & Y margins are the same all around |
| 849 | // the rect |
| 850 | SkScalar inset = SkMinScalar(SK_Scalar1, devOutside.fRight - devInside.fRight); |
| 851 | inset = SkMinScalar(inset, devInside.fLeft - devOutside.fLeft); |
| 852 | inset = SkMinScalar(inset, devInside.fTop - devOutside.fTop); |
commit-bot@chromium.org | 6006d0f | 2013-11-06 10:08:21 +0000 | [diff] [blame] | 853 | if (miterStroke) { |
| 854 | inset = SK_ScalarHalf * SkMinScalar(inset, devOutside.fBottom - devInside.fBottom); |
| 855 | } else { |
| 856 | inset = SK_ScalarHalf * SkMinScalar(inset, devOutsideAssist.fBottom - devInside.fBottom); |
| 857 | } |
robertphillips@google.com | 353f097 | 2013-06-28 17:57:06 +0000 | [diff] [blame] | 858 | SkASSERT(inset >= 0); |
robertphillips@google.com | c111ce2 | 2013-07-01 13:10:10 +0000 | [diff] [blame] | 859 | #else |
| 860 | SkScalar inset = SK_ScalarHalf; |
| 861 | #endif |
robertphillips@google.com | 353f097 | 2013-06-28 17:57:06 +0000 | [diff] [blame] | 862 | |
commit-bot@chromium.org | 6006d0f | 2013-11-06 10:08:21 +0000 | [diff] [blame] | 863 | if (miterStroke) { |
| 864 | // outermost |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 865 | set_inset_fan(fan0Pos, vsize, devOutside, -SK_ScalarHalf, -SK_ScalarHalf); |
commit-bot@chromium.org | 6006d0f | 2013-11-06 10:08:21 +0000 | [diff] [blame] | 866 | // inner two |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 867 | set_inset_fan(fan1Pos, vsize, devOutside, inset, inset); |
| 868 | set_inset_fan(fan2Pos, vsize, devInside, -inset, -inset); |
commit-bot@chromium.org | 6006d0f | 2013-11-06 10:08:21 +0000 | [diff] [blame] | 869 | // innermost |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 870 | set_inset_fan(fan3Pos, vsize, devInside, SK_ScalarHalf, SK_ScalarHalf); |
commit-bot@chromium.org | 6006d0f | 2013-11-06 10:08:21 +0000 | [diff] [blame] | 871 | } else { |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 872 | SkPoint* fan0AssistPos = reinterpret_cast<SkPoint*>(verts + 4 * vsize); |
| 873 | SkPoint* fan1AssistPos = reinterpret_cast<SkPoint*>(verts + (outerVertexNum + 4) * vsize); |
commit-bot@chromium.org | 6006d0f | 2013-11-06 10:08:21 +0000 | [diff] [blame] | 874 | // outermost |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 875 | set_inset_fan(fan0Pos, vsize, devOutside, -SK_ScalarHalf, -SK_ScalarHalf); |
| 876 | set_inset_fan(fan0AssistPos, vsize, devOutsideAssist, -SK_ScalarHalf, -SK_ScalarHalf); |
commit-bot@chromium.org | 6006d0f | 2013-11-06 10:08:21 +0000 | [diff] [blame] | 877 | // outer one of the inner two |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 878 | set_inset_fan(fan1Pos, vsize, devOutside, inset, inset); |
| 879 | set_inset_fan(fan1AssistPos, vsize, devOutsideAssist, inset, inset); |
commit-bot@chromium.org | 6006d0f | 2013-11-06 10:08:21 +0000 | [diff] [blame] | 880 | // inner one of the inner two |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 881 | set_inset_fan(fan2Pos, vsize, devInside, -inset, -inset); |
commit-bot@chromium.org | 6006d0f | 2013-11-06 10:08:21 +0000 | [diff] [blame] | 882 | // innermost |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 883 | set_inset_fan(fan3Pos, vsize, devInside, SK_ScalarHalf, SK_ScalarHalf); |
commit-bot@chromium.org | 6006d0f | 2013-11-06 10:08:21 +0000 | [diff] [blame] | 884 | } |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 885 | |
bsalomon | 9c0822a | 2014-08-11 11:07:48 -0700 | [diff] [blame] | 886 | // Make verts point to vertex color and then set all the color and coverage vertex attrs values. |
bsalomon@google.com | e7249bd | 2012-08-16 15:28:54 +0000 | [diff] [blame] | 887 | // The outermost rect has 0 coverage |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 888 | verts += sizeof(SkPoint); |
commit-bot@chromium.org | 6006d0f | 2013-11-06 10:08:21 +0000 | [diff] [blame] | 889 | for (int i = 0; i < outerVertexNum; ++i) { |
bsalomon | c30aaa0 | 2014-08-13 07:15:29 -0700 | [diff] [blame] | 890 | if (kUseCoverage_CoverageAttribType == covAttribType) { |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 891 | *reinterpret_cast<GrColor*>(verts + i * vsize) = color; |
| 892 | *reinterpret_cast<GrColor*>(verts + i * vsize + sizeof(GrColor)) = 0; |
bsalomon | c30aaa0 | 2014-08-13 07:15:29 -0700 | [diff] [blame] | 893 | } else { |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 894 | *reinterpret_cast<GrColor*>(verts + i * vsize) = 0; |
bsalomon | c30aaa0 | 2014-08-13 07:15:29 -0700 | [diff] [blame] | 895 | } |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 896 | } |
| 897 | |
bsalomon | 9c0822a | 2014-08-11 11:07:48 -0700 | [diff] [blame] | 898 | // scale is the coverage for the the inner two rects. |
robertphillips@google.com | 353f097 | 2013-06-28 17:57:06 +0000 | [diff] [blame] | 899 | int scale; |
| 900 | if (inset < SK_ScalarHalf) { |
| 901 | scale = SkScalarFloorToInt(512.0f * inset / (inset + SK_ScalarHalf)); |
| 902 | SkASSERT(scale >= 0 && scale <= 255); |
| 903 | } else { |
| 904 | scale = 0xff; |
| 905 | } |
| 906 | |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 907 | verts += outerVertexNum * vsize; |
bsalomon | c30aaa0 | 2014-08-13 07:15:29 -0700 | [diff] [blame] | 908 | GrColor innerCoverage; |
| 909 | if (kUseCoverage_CoverageAttribType == covAttribType) { |
| 910 | innerCoverage = GrColorPackRGBA(scale, scale, scale, scale); |
| 911 | } else { |
| 912 | innerCoverage = (0xff == scale) ? color : SkAlphaMulQ(color, scale); |
| 913 | } |
| 914 | |
commit-bot@chromium.org | 6006d0f | 2013-11-06 10:08:21 +0000 | [diff] [blame] | 915 | for (int i = 0; i < outerVertexNum + innerVertexNum; ++i) { |
bsalomon | c30aaa0 | 2014-08-13 07:15:29 -0700 | [diff] [blame] | 916 | if (kUseCoverage_CoverageAttribType == covAttribType) { |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 917 | *reinterpret_cast<GrColor*>(verts + i * vsize) = color; |
| 918 | *reinterpret_cast<GrColor*>(verts + i * vsize + sizeof(GrColor)) = innerCoverage; |
bsalomon | c30aaa0 | 2014-08-13 07:15:29 -0700 | [diff] [blame] | 919 | } else { |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 920 | *reinterpret_cast<GrColor*>(verts + i * vsize) = innerCoverage; |
bsalomon | c30aaa0 | 2014-08-13 07:15:29 -0700 | [diff] [blame] | 921 | } |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 922 | } |
| 923 | |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 924 | // The innermost rect has 0 coverage |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 925 | verts += (outerVertexNum + innerVertexNum) * vsize; |
commit-bot@chromium.org | 6006d0f | 2013-11-06 10:08:21 +0000 | [diff] [blame] | 926 | for (int i = 0; i < innerVertexNum; ++i) { |
bsalomon | c30aaa0 | 2014-08-13 07:15:29 -0700 | [diff] [blame] | 927 | if (kUseCoverage_CoverageAttribType == covAttribType) { |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 928 | *reinterpret_cast<GrColor*>(verts + i * vsize) = color; |
| 929 | *reinterpret_cast<GrColor*>(verts + i * vsize + sizeof(GrColor)) = 0; |
bsalomon | c30aaa0 | 2014-08-13 07:15:29 -0700 | [diff] [blame] | 930 | } else { |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 931 | *reinterpret_cast<GrColor*>(verts + i * vsize) = 0; |
bsalomon | c30aaa0 | 2014-08-13 07:15:29 -0700 | [diff] [blame] | 932 | } |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | target->setIndexSourceToBuffer(indexBuffer); |
commit-bot@chromium.org | 6006d0f | 2013-11-06 10:08:21 +0000 | [diff] [blame] | 936 | target->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, |
| 937 | totalVertexNum, aaStrokeRectIndexCount(miterStroke)); |
robertphillips@google.com | f6747b0 | 2012-06-12 00:32:28 +0000 | [diff] [blame] | 938 | } |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 939 | |
| 940 | void GrAARectRenderer::fillAANestedRects(GrGpu* gpu, |
| 941 | GrDrawTarget* target, |
| 942 | const SkRect rects[2], |
bsalomon | 9c0822a | 2014-08-11 11:07:48 -0700 | [diff] [blame] | 943 | const SkMatrix& combinedMatrix) { |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 944 | SkASSERT(combinedMatrix.rectStaysRect()); |
| 945 | SkASSERT(!rects[1].isEmpty()); |
| 946 | |
commit-bot@chromium.org | 6006d0f | 2013-11-06 10:08:21 +0000 | [diff] [blame] | 947 | SkRect devOutside, devOutsideAssist, devInside; |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 948 | combinedMatrix.mapRect(&devOutside, rects[0]); |
| 949 | // can't call mapRect for devInside since it calls sort |
| 950 | combinedMatrix.mapPoints((SkPoint*)&devInside, (const SkPoint*)&rects[1], 2); |
| 951 | |
| 952 | if (devInside.isEmpty()) { |
bsalomon | 9c0822a | 2014-08-11 11:07:48 -0700 | [diff] [blame] | 953 | this->fillAARect(gpu, target, devOutside, SkMatrix::I(), devOutside); |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 954 | return; |
| 955 | } |
| 956 | |
bsalomon | 9c0822a | 2014-08-11 11:07:48 -0700 | [diff] [blame] | 957 | this->geometryStrokeAARect(gpu, target, devOutside, devOutsideAssist, devInside, true); |
robertphillips@google.com | 83d1a68 | 2013-05-17 12:50:27 +0000 | [diff] [blame] | 958 | } |