OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "GrAARectRenderer.h" | 8 #include "GrAARectRenderer.h" |
9 #include "GrRefCnt.h" | 9 #include "GrRefCnt.h" |
10 #include "GrGpu.h" | 10 #include "GrGpu.h" |
| 11 #include "effects/GrRectEffect.h" |
11 | 12 |
12 SK_DEFINE_INST_COUNT(GrAARectRenderer) | 13 SK_DEFINE_INST_COUNT(GrAARectRenderer) |
13 | 14 |
14 namespace { | 15 namespace { |
15 | 16 |
16 static void aa_rect_attributes(bool useCoverage, const GrVertexAttrib** attribs,
int* count) { | 17 static void aa_rect_attributes(bool useCoverage, const GrVertexAttrib** attribs,
int* count) { |
17 static const GrVertexAttrib kCoverageAttribs[] = { | 18 static const GrVertexAttrib kCoverageAttribs[] = { |
18 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, | 19 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, |
19 {kVec4ub_GrVertexAttribType, sizeof(GrPoint), kCoverage_GrVertexAttribBi
nding}, | 20 {kVec4ub_GrVertexAttribType, sizeof(GrPoint), kCoverage_GrVertexAttribBi
nding}, |
20 }; | 21 }; |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 *reinterpret_cast<GrColor*>(verts + i * vsize) = innerColor; | 175 *reinterpret_cast<GrColor*>(verts + i * vsize) = innerColor; |
175 } | 176 } |
176 | 177 |
177 target->setIndexSourceToBuffer(indexBuffer); | 178 target->setIndexSourceToBuffer(indexBuffer); |
178 target->drawIndexedInstances(kTriangles_GrPrimitiveType, 1, | 179 target->drawIndexedInstances(kTriangles_GrPrimitiveType, 1, |
179 kVertsPerAAFillRect, | 180 kVertsPerAAFillRect, |
180 kIndicesPerAAFillRect); | 181 kIndicesPerAAFillRect); |
181 target->resetIndexSource(); | 182 target->resetIndexSource(); |
182 } | 183 } |
183 | 184 |
| 185 struct RectVertex { |
| 186 GrPoint fPos; |
| 187 GrPoint fCenter; |
| 188 GrPoint fDir; |
| 189 GrPoint fWidthHeight; |
| 190 }; |
| 191 |
| 192 |
| 193 void GrAARectRenderer::shaderFillAARect(GrGpu* gpu, |
| 194 GrDrawTarget* target, |
| 195 const GrRect& rect, |
| 196 const SkMatrix& combinedMatrix, |
| 197 const GrRect& devRect, |
| 198 bool useVertexCoverage) { |
| 199 GrDrawState* drawState = target->drawState(); |
| 200 |
| 201 SkPoint center = SkPoint::Make(rect.centerX(), rect.centerY()); |
| 202 combinedMatrix.mapPoints(¢er, 1); |
| 203 |
| 204 // compute transformed (0, 1) vector |
| 205 SkVector dir = { combinedMatrix[SkMatrix::kMSkewX], combinedMatrix[SkMatrix:
:kMScaleY] }; |
| 206 dir.normalize(); |
| 207 |
| 208 // compute transformed (width, 0) and (0, height) vectors |
| 209 SkVector vec[2] = { |
| 210 { combinedMatrix[SkMatrix::kMScaleX] * rect.width(), |
| 211 combinedMatrix[SkMatrix::kMSkewY] * rect.width() }, |
| 212 { combinedMatrix[SkMatrix::kMSkewX] * rect.height(), |
| 213 combinedMatrix[SkMatrix::kMScaleY] * rect.height() } |
| 214 }; |
| 215 |
| 216 SkScalar newWidth = vec[0].length() / 2.0f + 0.5f; |
| 217 SkScalar newHeight = vec[1].length() / 2.0f + 0.5f; |
| 218 |
| 219 static const GrVertexAttrib kVertexAttribs[] = { |
| 220 { kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding }, |
| 221 { kVec4f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBind
ing }, |
| 222 { kVec2f_GrVertexAttribType, 3*sizeof(GrPoint), kEffect_GrVertexAttribBi
nding } |
| 223 }; |
| 224 drawState->setVertexAttribs(kVertexAttribs, SK_ARRAY_COUNT(kVertexAttribs)); |
| 225 GrAssert(sizeof(RectVertex) == drawState->getVertexSize()); |
| 226 |
| 227 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0); |
| 228 if (!geo.succeeded()) { |
| 229 GrPrintf("Failed to get space for vertices!\n"); |
| 230 return; |
| 231 } |
| 232 |
| 233 RectVertex* verts = reinterpret_cast<RectVertex*>(geo.vertices()); |
| 234 |
| 235 enum { |
| 236 // the edge effects share this stage with glyph rendering |
| 237 // (kGlyphMaskStage in GrTextContext) && SW path rendering |
| 238 // (kPathMaskStage in GrSWMaskHelper) |
| 239 kEdgeEffectStage = GrPaint::kTotalStages, |
| 240 }; |
| 241 |
| 242 GrEffectRef* effect = GrRectEffect::Create(); |
| 243 static const int kRectAttrIndex = 1; |
| 244 static const int kWidthIndex = 2; |
| 245 drawState->setEffect(kEdgeEffectStage, effect, kRectAttrIndex, kWidthIndex)-
>unref(); |
| 246 |
| 247 for (int i = 0; i < 4; ++i) { |
| 248 verts[i].fCenter = center; |
| 249 verts[i].fDir = dir; |
| 250 verts[i].fWidthHeight.fX = newWidth; |
| 251 verts[i].fWidthHeight.fY = newHeight; |
| 252 } |
| 253 |
| 254 SkRect devBounds = { |
| 255 devRect.fLeft - SK_ScalarHalf, |
| 256 devRect.fTop - SK_ScalarHalf, |
| 257 devRect.fRight + SK_ScalarHalf, |
| 258 devRect.fBottom + SK_ScalarHalf |
| 259 }; |
| 260 |
| 261 verts[0].fPos = SkPoint::Make(devBounds.fLeft, devBounds.fTop); |
| 262 verts[1].fPos = SkPoint::Make(devBounds.fLeft, devBounds.fBottom); |
| 263 verts[2].fPos = SkPoint::Make(devBounds.fRight, devBounds.fBottom); |
| 264 verts[3].fPos = SkPoint::Make(devBounds.fRight, devBounds.fTop); |
| 265 |
| 266 target->setIndexSourceToBuffer(gpu->getContext()->getQuadIndexBuffer()); |
| 267 target->drawIndexedInstances(kTriangles_GrPrimitiveType, 1, 4, 6); |
| 268 target->resetIndexSource(); |
| 269 } |
| 270 |
184 void GrAARectRenderer::strokeAARect(GrGpu* gpu, | 271 void GrAARectRenderer::strokeAARect(GrGpu* gpu, |
185 GrDrawTarget* target, | 272 GrDrawTarget* target, |
186 const GrRect& devRect, | 273 const GrRect& devRect, |
187 const GrVec& devStrokeSize, | 274 const GrVec& devStrokeSize, |
188 bool useVertexCoverage) { | 275 bool useVertexCoverage) { |
189 GrDrawState* drawState = target->drawState(); | 276 GrDrawState* drawState = target->drawState(); |
190 | 277 |
191 const SkScalar& dx = devStrokeSize.fX; | 278 const SkScalar& dx = devStrokeSize.fX; |
192 const SkScalar& dy = devStrokeSize.fY; | 279 const SkScalar& dy = devStrokeSize.fY; |
193 const SkScalar rx = SkScalarMul(dx, SK_ScalarHalf); | 280 const SkScalar rx = SkScalarMul(dx, SK_ScalarHalf); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 // The innermost rect has full coverage | 352 // The innermost rect has full coverage |
266 verts += 8 * vsize; | 353 verts += 8 * vsize; |
267 for (int i = 0; i < 4; ++i) { | 354 for (int i = 0; i < 4; ++i) { |
268 *reinterpret_cast<GrColor*>(verts + i * vsize) = 0; | 355 *reinterpret_cast<GrColor*>(verts + i * vsize) = 0; |
269 } | 356 } |
270 | 357 |
271 target->setIndexSourceToBuffer(indexBuffer); | 358 target->setIndexSourceToBuffer(indexBuffer); |
272 target->drawIndexed(kTriangles_GrPrimitiveType, | 359 target->drawIndexed(kTriangles_GrPrimitiveType, |
273 0, 0, 16, aaStrokeRectIndexCount()); | 360 0, 0, 16, aaStrokeRectIndexCount()); |
274 } | 361 } |
OLD | NEW |