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 "GrBatch.h" |
| 10 #include "GrBatchBuffer.h" |
| 11 #include "GrBufferAllocPool.h" |
9 #include "GrDefaultGeoProcFactory.h" | 12 #include "GrDefaultGeoProcFactory.h" |
10 #include "GrGeometryProcessor.h" | 13 #include "GrGeometryProcessor.h" |
11 #include "GrGpu.h" | 14 #include "GrGpu.h" |
12 #include "GrInvariantOutput.h" | 15 #include "GrInvariantOutput.h" |
13 #include "SkColorPriv.h" | 16 #include "SkColorPriv.h" |
14 #include "gl/GrGLProcessor.h" | 17 #include "gl/GrGLProcessor.h" |
15 #include "gl/GrGLGeometryProcessor.h" | 18 #include "gl/GrGLGeometryProcessor.h" |
16 #include "gl/builders/GrGLProgramBuilder.h" | 19 #include "gl/builders/GrGLProgramBuilder.h" |
17 | 20 |
18 /////////////////////////////////////////////////////////////////////////////// | 21 /////////////////////////////////////////////////////////////////////////////// |
19 | 22 |
20 namespace { | |
21 // Should the coverage be multiplied into the color attrib or use a separate att
rib. | |
22 enum CoverageAttribType { | |
23 kUseColor_CoverageAttribType, | |
24 kUseCoverage_CoverageAttribType, | |
25 }; | |
26 } | |
27 | |
28 static const GrGeometryProcessor* create_rect_gp(const GrDrawState& drawState, | |
29 GrColor color, | |
30 CoverageAttribType* type, | |
31 const SkMatrix& localMatrix) { | |
32 uint32_t flags = GrDefaultGeoProcFactory::kColor_GPType; | |
33 const GrGeometryProcessor* gp; | |
34 if (drawState.canTweakAlphaForCoverage()) { | |
35 gp = GrDefaultGeoProcFactory::Create(flags, color, SkMatrix::I(), localM
atrix); | |
36 SkASSERT(gp->getVertexStride() == sizeof(GrDefaultGeoProcFactory::Positi
onColorAttr)); | |
37 *type = kUseColor_CoverageAttribType; | |
38 } else { | |
39 flags |= GrDefaultGeoProcFactory::kCoverage_GPType; | |
40 gp = GrDefaultGeoProcFactory::Create(flags, color, SkMatrix::I(), localM
atrix, | |
41 GrColorIsOpaque(color)); | |
42 SkASSERT(gp->getVertexStride()==sizeof(GrDefaultGeoProcFactory::Position
ColorCoverageAttr)); | |
43 *type = kUseCoverage_CoverageAttribType; | |
44 } | |
45 return gp; | |
46 } | |
47 | |
48 static void set_inset_fan(SkPoint* pts, size_t stride, | 23 static void set_inset_fan(SkPoint* pts, size_t stride, |
49 const SkRect& r, SkScalar dx, SkScalar dy) { | 24 const SkRect& r, SkScalar dx, SkScalar dy) { |
50 pts->setRectFan(r.fLeft + dx, r.fTop + dy, | 25 pts->setRectFan(r.fLeft + dx, r.fTop + dy, |
51 r.fRight - dx, r.fBottom - dy, stride); | 26 r.fRight - dx, r.fBottom - dy, stride); |
52 } | 27 } |
53 | 28 |
54 void GrAARectRenderer::reset() { | |
55 SkSafeSetNull(fAAFillRectIndexBuffer); | |
56 SkSafeSetNull(fAAMiterStrokeRectIndexBuffer); | |
57 SkSafeSetNull(fAABevelStrokeRectIndexBuffer); | |
58 } | |
59 | |
60 static const uint16_t gFillAARectIdx[] = { | 29 static const uint16_t gFillAARectIdx[] = { |
61 0, 1, 5, 5, 4, 0, | 30 0, 1, 5, 5, 4, 0, |
62 1, 2, 6, 6, 5, 1, | 31 1, 2, 6, 6, 5, 1, |
63 2, 3, 7, 7, 6, 2, | 32 2, 3, 7, 7, 6, 2, |
64 3, 0, 4, 4, 7, 3, | 33 3, 0, 4, 4, 7, 3, |
65 4, 5, 6, 6, 7, 4, | 34 4, 5, 6, 6, 7, 4, |
66 }; | 35 }; |
67 | 36 |
68 static const int kIndicesPerAAFillRect = SK_ARRAY_COUNT(gFillAARectIdx); | 37 static const int kIndicesPerAAFillRect = SK_ARRAY_COUNT(gFillAARectIdx); |
69 static const int kVertsPerAAFillRect = 8; | 38 static const int kVertsPerAAFillRect = 8; |
70 static const int kNumAAFillRectsInIndexBuffer = 256; | 39 static const int kNumAAFillRectsInIndexBuffer = 256; |
71 | 40 |
| 41 static const GrGeometryProcessor* create_fill_rect_gp(bool tweakAlphaForCoverage
, |
| 42 const SkMatrix& localMatri
x) { |
| 43 uint32_t flags = GrDefaultGeoProcFactory::kColor_GPType; |
| 44 const GrGeometryProcessor* gp; |
| 45 if (tweakAlphaForCoverage) { |
| 46 gp = GrDefaultGeoProcFactory::Create(flags, GrColor_WHITE, SkMatrix::I()
, localMatrix, |
| 47 false, 0xff); |
| 48 } else { |
| 49 flags |= GrDefaultGeoProcFactory::kCoverage_GPType; |
| 50 gp = GrDefaultGeoProcFactory::Create(flags, GrColor_WHITE, SkMatrix::I()
, localMatrix, |
| 51 false, 0xff); |
| 52 } |
| 53 return gp; |
| 54 } |
| 55 |
| 56 class AAFillRectBatch : public GrBatch { |
| 57 public: |
| 58 struct Geometry { |
| 59 GrColor fColor; |
| 60 SkMatrix fViewMatrix; |
| 61 SkRect fRect; |
| 62 SkRect fDevRect; |
| 63 }; |
| 64 |
| 65 static GrBatch* Create(const Geometry& geometry, const GrIndexBuffer* indexB
uffer) { |
| 66 return SkNEW_ARGS(AAFillRectBatch, (geometry, indexBuffer)); |
| 67 } |
| 68 |
| 69 const char* name() const SK_OVERRIDE { return "AAFillRectBatch"; } |
| 70 |
| 71 void getInvariantOutputColor(GrInitInvariantOutput* out, |
| 72 const GrBatchOpt& batchOpt) const SK_OVERRIDE { |
| 73 // When this is called on a batch, there is only one geometry bundle |
| 74 if (!batchOpt.fCanTweakAlphaForCoverage && GrColorIsOpaque(fGeoData[0].f
Color)) { |
| 75 out->setUnknownOpaqueFourComponents(); |
| 76 } else { |
| 77 out->setUnknownFourComponents(); |
| 78 } |
| 79 } |
| 80 |
| 81 void getInvariantOutputCoverage(GrInitInvariantOutput* out, |
| 82 const GrBatchOpt& batchOpt) const SK_OVERRID
E { |
| 83 if (batchOpt.fCanTweakAlphaForCoverage) { |
| 84 // uniform coverage |
| 85 out->setKnownSingleComponent(0xff); |
| 86 } else { |
| 87 out->setUnknownSingleComponent(); |
| 88 } |
| 89 } |
| 90 |
| 91 void initBatchOpt(const GrBatchOpt& batchOpt) { |
| 92 fBatchOpt = batchOpt; |
| 93 } |
| 94 |
| 95 void initBatchTracker(const GrGeometryProcessor::InitBT& init) SK_OVERRIDE { |
| 96 // Handle any color overrides |
| 97 if (init.fColorIgnored) { |
| 98 fGeoData[0].fColor = GrColor_ILLEGAL; |
| 99 } else if (GrColor_ILLEGAL != init.fOverrideColor) { |
| 100 fGeoData[0].fColor = init.fOverrideColor; |
| 101 } |
| 102 |
| 103 // setup batch properties |
| 104 fBatch.fColorIgnored = init.fColorIgnored; |
| 105 fBatch.fColor = fGeoData[0].fColor; |
| 106 fBatch.fUsesLocalCoords = init.fUsesLocalCoords; |
| 107 fBatch.fCoverageIgnored = init.fCoverageIgnored; |
| 108 } |
| 109 |
| 110 void generateGeometry(GrBatchTarget* batchTarget, const GrOptDrawState* optS
tate) SK_OVERRIDE { |
| 111 bool canTweakAlphaForCoverage = this->canTweakAlphaForCoverage(); |
| 112 |
| 113 SkMatrix localMatrix; |
| 114 if (!this->viewMatrix().invert(&localMatrix)) { |
| 115 SkDebugf("Cannot invert\n"); |
| 116 return; |
| 117 } |
| 118 |
| 119 const GrGeometryProcessor* gp = create_fill_rect_gp(canTweakAlphaForCove
rage, |
| 120 localMatrix); |
| 121 |
| 122 batchTarget->initDraw(gp, optState); |
| 123 gp->unref(); |
| 124 |
| 125 // TODO this is hacky, but the only way we have to initialize the GP is
to use the initBT |
| 126 // struct so we can generate the correct shader. Once we have GrBatch e
verywhere we can |
| 127 // remove this nastiness |
| 128 GrGeometryProcessor::InitBT init; |
| 129 init.fColorIgnored = fBatch.fColorIgnored; |
| 130 init.fOverrideColor = GrColor_ILLEGAL; |
| 131 init.fCoverageIgnored = fBatch.fCoverageIgnored; |
| 132 init.fUsesLocalCoords = this->usesLocalCoords(); |
| 133 gp->initBatchTracker(batchTarget->currentBatchTracker(), init); |
| 134 |
| 135 size_t vertexStride = gp->getVertexStride(); |
| 136 |
| 137 SkASSERT(canTweakAlphaForCoverage ? |
| 138 vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorAt
tr) : |
| 139 vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorCo
verageAttr)); |
| 140 |
| 141 int instanceCount = fGeoData.count(); |
| 142 int vertexCount = kVertsPerAAFillRect * instanceCount; |
| 143 |
| 144 const GrVertexBuffer* vertexBuffer; |
| 145 int firstVertex; |
| 146 |
| 147 void *vertices = batchTarget->vertexPool()->makeSpace(vertexStride, |
| 148 vertexCount, |
| 149 &vertexBuffer, |
| 150 &firstVertex); |
| 151 |
| 152 for (int i = 0; i < instanceCount; i++) { |
| 153 const Geometry& args = fGeoData[i]; |
| 154 this->generateAAFillRectGeometry(vertices, |
| 155 i * kVertsPerAAFillRect * vertexStride, |
| 156 vertexStride, |
| 157 args.fColor, |
| 158 args.fViewMatrix, |
| 159 args.fRect, |
| 160 args.fDevRect, |
| 161 canTweakAlphaForCoverage); |
| 162 } |
| 163 |
| 164 GrDrawTarget::DrawInfo drawInfo; |
| 165 drawInfo.setPrimitiveType(kTriangles_GrPrimitiveType); |
| 166 drawInfo.setStartVertex(0); |
| 167 drawInfo.setStartIndex(0); |
| 168 drawInfo.setVerticesPerInstance(kVertsPerAAFillRect); |
| 169 drawInfo.setIndicesPerInstance(kIndicesPerAAFillRect); |
| 170 drawInfo.adjustStartVertex(firstVertex); |
| 171 drawInfo.setVertexBuffer(vertexBuffer); |
| 172 drawInfo.setIndexBuffer(fIndexBuffer); |
| 173 |
| 174 int maxInstancesPerDraw = kNumAAFillRectsInIndexBuffer; |
| 175 |
| 176 while (instanceCount) { |
| 177 drawInfo.setInstanceCount(SkTMin(instanceCount, maxInstancesPerDraw)
); |
| 178 drawInfo.setVertexCount(drawInfo.instanceCount() * drawInfo.vertices
PerInstance()); |
| 179 drawInfo.setIndexCount(drawInfo.instanceCount() * drawInfo.indicesPe
rInstance()); |
| 180 |
| 181 batchTarget->draw(drawInfo); |
| 182 |
| 183 drawInfo.setStartVertex(drawInfo.startVertex() + drawInfo.vertexCoun
t()); |
| 184 instanceCount -= drawInfo.instanceCount(); |
| 185 } |
| 186 } |
| 187 |
| 188 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } |
| 189 |
| 190 private: |
| 191 AAFillRectBatch(const Geometry& geometry, const GrIndexBuffer* indexBuffer) |
| 192 : fIndexBuffer(indexBuffer) { |
| 193 this->initClassID<AAFillRectBatch>(); |
| 194 fGeoData.push_back(geometry); |
| 195 } |
| 196 |
| 197 GrColor color() const { return fBatch.fColor; } |
| 198 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } |
| 199 bool canTweakAlphaForCoverage() const { return fBatchOpt.fCanTweakAlphaForCo
verage; } |
| 200 bool colorIgnored() const { return fBatch.fColorIgnored; } |
| 201 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; } |
| 202 |
| 203 bool onCombineIfPossible(GrBatch* t) SK_OVERRIDE { |
| 204 AAFillRectBatch* that = t->cast<AAFillRectBatch>(); |
| 205 if (this->canTweakAlphaForCoverage() != that->canTweakAlphaForCoverage()
) { |
| 206 return false; |
| 207 } |
| 208 |
| 209 if (this->colorIgnored() != that->colorIgnored()) { |
| 210 return false; |
| 211 } |
| 212 |
| 213 if (this->usesLocalCoords() != that->usesLocalCoords()) { |
| 214 return false; |
| 215 } |
| 216 |
| 217 // We apply the viewmatrix to the rect points on the cpu. However, if t
he pipeline uses |
| 218 // local coords then we won't be able to batch. We could actually uploa
d the viewmatrix |
| 219 // using vertex attributes in these cases, but haven't investigated that |
| 220 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->vi
ewMatrix())) { |
| 221 return false; |
| 222 } |
| 223 |
| 224 if (this->color() != that->color()) { |
| 225 fBatch.fColor = GrColor_ILLEGAL; |
| 226 } |
| 227 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin())
; |
| 228 return true; |
| 229 } |
| 230 |
| 231 void generateAAFillRectGeometry(void* vertices, |
| 232 uint32_t offset, |
| 233 uint32_t vertexStride, |
| 234 GrColor color, |
| 235 const SkMatrix& viewMatrix, |
| 236 const SkRect& rect, |
| 237 const SkRect& devRect, |
| 238 bool tweakAlphaForCoverage) const { |
| 239 intptr_t verts = reinterpret_cast<intptr_t>(vertices) + offset; |
| 240 |
| 241 SkPoint* fan0Pos = reinterpret_cast<SkPoint*>(verts); |
| 242 SkPoint* fan1Pos = reinterpret_cast<SkPoint*>(verts + 4 * vertexStride); |
| 243 |
| 244 SkScalar inset = SkMinScalar(devRect.width(), SK_Scalar1); |
| 245 inset = SK_ScalarHalf * SkMinScalar(inset, devRect.height()); |
| 246 |
| 247 if (viewMatrix.rectStaysRect()) { |
| 248 set_inset_fan(fan0Pos, vertexStride, devRect, -SK_ScalarHalf, -SK_Sc
alarHalf); |
| 249 set_inset_fan(fan1Pos, vertexStride, devRect, inset, inset); |
| 250 } else { |
| 251 // compute transformed (1, 0) and (0, 1) vectors |
| 252 SkVector vec[2] = { |
| 253 { viewMatrix[SkMatrix::kMScaleX], viewMatrix[SkMatrix::kMSkewY] }, |
| 254 { viewMatrix[SkMatrix::kMSkewX], viewMatrix[SkMatrix::kMScaleY] } |
| 255 }; |
| 256 |
| 257 vec[0].normalize(); |
| 258 vec[0].scale(SK_ScalarHalf); |
| 259 vec[1].normalize(); |
| 260 vec[1].scale(SK_ScalarHalf); |
| 261 |
| 262 // create the rotated rect |
| 263 fan0Pos->setRectFan(rect.fLeft, rect.fTop, |
| 264 rect.fRight, rect.fBottom, vertexStride); |
| 265 viewMatrix.mapPointsWithStride(fan0Pos, vertexStride, 4); |
| 266 |
| 267 // Now create the inset points and then outset the original |
| 268 // rotated points |
| 269 |
| 270 // TL |
| 271 *((SkPoint*)((intptr_t)fan1Pos + 0 * vertexStride)) = |
| 272 *((SkPoint*)((intptr_t)fan0Pos + 0 * vertexStride)) + vec[0] + v
ec[1]; |
| 273 *((SkPoint*)((intptr_t)fan0Pos + 0 * vertexStride)) -= vec[0] + vec[
1]; |
| 274 // BL |
| 275 *((SkPoint*)((intptr_t)fan1Pos + 1 * vertexStride)) = |
| 276 *((SkPoint*)((intptr_t)fan0Pos + 1 * vertexStride)) + vec[0] - v
ec[1]; |
| 277 *((SkPoint*)((intptr_t)fan0Pos + 1 * vertexStride)) -= vec[0] - vec[
1]; |
| 278 // BR |
| 279 *((SkPoint*)((intptr_t)fan1Pos + 2 * vertexStride)) = |
| 280 *((SkPoint*)((intptr_t)fan0Pos + 2 * vertexStride)) - vec[0] - v
ec[1]; |
| 281 *((SkPoint*)((intptr_t)fan0Pos + 2 * vertexStride)) += vec[0] + vec[
1]; |
| 282 // TR |
| 283 *((SkPoint*)((intptr_t)fan1Pos + 3 * vertexStride)) = |
| 284 *((SkPoint*)((intptr_t)fan0Pos + 3 * vertexStride)) - vec[0] + v
ec[1]; |
| 285 *((SkPoint*)((intptr_t)fan0Pos + 3 * vertexStride)) += vec[0] - vec[
1]; |
| 286 } |
| 287 |
| 288 // Make verts point to vertex color and then set all the color and cover
age vertex attrs |
| 289 // values. |
| 290 verts += sizeof(SkPoint); |
| 291 for (int i = 0; i < 4; ++i) { |
| 292 if (tweakAlphaForCoverage) { |
| 293 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = 0; |
| 294 } else { |
| 295 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color; |
| 296 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrCo
lor)) = 0; |
| 297 } |
| 298 } |
| 299 |
| 300 int scale; |
| 301 if (inset < SK_ScalarHalf) { |
| 302 scale = SkScalarFloorToInt(512.0f * inset / (inset + SK_ScalarHalf))
; |
| 303 SkASSERT(scale >= 0 && scale <= 255); |
| 304 } else { |
| 305 scale = 0xff; |
| 306 } |
| 307 |
| 308 verts += 4 * vertexStride; |
| 309 |
| 310 float innerCoverage = GrNormalizeByteToFloat(scale); |
| 311 GrColor scaledColor = (0xff == scale) ? color : SkAlphaMulQ(color, scale
); |
| 312 |
| 313 for (int i = 0; i < 4; ++i) { |
| 314 if (tweakAlphaForCoverage) { |
| 315 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledCo
lor; |
| 316 } else { |
| 317 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color; |
| 318 *reinterpret_cast<float*>(verts + i * vertexStride + |
| 319 sizeof(GrColor)) = innerCoverage; |
| 320 } |
| 321 } |
| 322 } |
| 323 |
| 324 struct BatchTracker { |
| 325 GrColor fColor; |
| 326 bool fUsesLocalCoords; |
| 327 bool fColorIgnored; |
| 328 bool fCoverageIgnored; |
| 329 }; |
| 330 |
| 331 GrBatchOpt fBatchOpt; |
| 332 BatchTracker fBatch; |
| 333 const GrIndexBuffer* fIndexBuffer; |
| 334 SkSTArray<1, Geometry, true> fGeoData; |
| 335 }; |
| 336 |
| 337 namespace { |
| 338 // Should the coverage be multiplied into the color attrib or use a separate att
rib. |
| 339 enum CoverageAttribType { |
| 340 kUseColor_CoverageAttribType, |
| 341 kUseCoverage_CoverageAttribType, |
| 342 }; |
| 343 } |
| 344 |
| 345 void GrAARectRenderer::reset() { |
| 346 SkSafeSetNull(fAAFillRectIndexBuffer); |
| 347 SkSafeSetNull(fAAMiterStrokeRectIndexBuffer); |
| 348 SkSafeSetNull(fAABevelStrokeRectIndexBuffer); |
| 349 } |
| 350 |
72 static const uint16_t gMiterStrokeAARectIdx[] = { | 351 static const uint16_t gMiterStrokeAARectIdx[] = { |
73 0 + 0, 1 + 0, 5 + 0, 5 + 0, 4 + 0, 0 + 0, | 352 0 + 0, 1 + 0, 5 + 0, 5 + 0, 4 + 0, 0 + 0, |
74 1 + 0, 2 + 0, 6 + 0, 6 + 0, 5 + 0, 1 + 0, | 353 1 + 0, 2 + 0, 6 + 0, 6 + 0, 5 + 0, 1 + 0, |
75 2 + 0, 3 + 0, 7 + 0, 7 + 0, 6 + 0, 2 + 0, | 354 2 + 0, 3 + 0, 7 + 0, 7 + 0, 6 + 0, 2 + 0, |
76 3 + 0, 0 + 0, 4 + 0, 4 + 0, 7 + 0, 3 + 0, | 355 3 + 0, 0 + 0, 4 + 0, 4 + 0, 7 + 0, 3 + 0, |
77 | 356 |
78 0 + 4, 1 + 4, 5 + 4, 5 + 4, 4 + 4, 0 + 4, | 357 0 + 4, 1 + 4, 5 + 4, 5 + 4, 4 + 4, 0 + 4, |
79 1 + 4, 2 + 4, 6 + 4, 6 + 4, 5 + 4, 1 + 4, | 358 1 + 4, 2 + 4, 6 + 4, 6 + 4, 5 + 4, 1 + 4, |
80 2 + 4, 3 + 4, 7 + 4, 7 + 4, 6 + 4, 2 + 4, | 359 2 + 4, 3 + 4, 7 + 4, 7 + 4, 6 + 4, 2 + 4, |
81 3 + 4, 0 + 4, 4 + 4, 4 + 4, 7 + 4, 3 + 4, | 360 3 + 4, 0 + 4, 4 + 4, 4 + 4, 7 + 4, 3 + 4, |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 return fAABevelStrokeRectIndexBuffer; | 456 return fAABevelStrokeRectIndexBuffer; |
178 } | 457 } |
179 } | 458 } |
180 | 459 |
181 void GrAARectRenderer::geometryFillAARect(GrDrawTarget* target, | 460 void GrAARectRenderer::geometryFillAARect(GrDrawTarget* target, |
182 GrDrawState* drawState, | 461 GrDrawState* drawState, |
183 GrColor color, | 462 GrColor color, |
184 const SkMatrix& viewMatrix, | 463 const SkMatrix& viewMatrix, |
185 const SkRect& rect, | 464 const SkRect& rect, |
186 const SkRect& devRect) { | 465 const SkRect& devRect) { |
187 GrDrawState::AutoRestoreEffects are(drawState); | |
188 | |
189 SkMatrix localMatrix; | |
190 if (!viewMatrix.invert(&localMatrix)) { | |
191 SkDebugf("Cannot invert\n"); | |
192 return; | |
193 } | |
194 | |
195 CoverageAttribType type; | |
196 SkAutoTUnref<const GrGeometryProcessor> gp(create_rect_gp(*drawState, color,
&type, | |
197 localMatrix)); | |
198 | |
199 size_t vertexStride = gp->getVertexStride(); | |
200 GrDrawTarget::AutoReleaseGeometry geo(target, 8, vertexStride, 0); | |
201 if (!geo.succeeded()) { | |
202 SkDebugf("Failed to get space for vertices!\n"); | |
203 return; | |
204 } | |
205 | |
206 if (NULL == fAAFillRectIndexBuffer) { | 466 if (NULL == fAAFillRectIndexBuffer) { |
207 fAAFillRectIndexBuffer = fGpu->createInstancedIndexBuffer(gFillAARectIdx
, | 467 fAAFillRectIndexBuffer = fGpu->createInstancedIndexBuffer(gFillAARectIdx
, |
208 kIndicesPerAAF
illRect, | 468 kIndicesPerAAF
illRect, |
209 kNumAAFillRect
sInIndexBuffer, | 469 kNumAAFillRect
sInIndexBuffer, |
210 kVertsPerAAFil
lRect); | 470 kVertsPerAAFil
lRect); |
211 } | 471 } |
212 GrIndexBuffer* indexBuffer = fAAFillRectIndexBuffer; | |
213 if (NULL == indexBuffer) { | |
214 SkDebugf("Failed to create index buffer!\n"); | |
215 return; | |
216 } | |
217 | 472 |
218 intptr_t verts = reinterpret_cast<intptr_t>(geo.vertices()); | 473 AAFillRectBatch::Geometry geometry; |
| 474 geometry.fRect = rect; |
| 475 geometry.fViewMatrix = viewMatrix; |
| 476 geometry.fDevRect = devRect; |
| 477 geometry.fColor = color; |
219 | 478 |
220 SkPoint* fan0Pos = reinterpret_cast<SkPoint*>(verts); | 479 SkAutoTUnref<GrBatch> batch(AAFillRectBatch::Create(geometry, fAAFillRectInd
exBuffer)); |
221 SkPoint* fan1Pos = reinterpret_cast<SkPoint*>(verts + 4 * vertexStride); | 480 target->drawBatch(drawState, batch, kTriangles_GrPrimitiveType, &devRect); |
222 | |
223 SkScalar inset = SkMinScalar(devRect.width(), SK_Scalar1); | |
224 inset = SK_ScalarHalf * SkMinScalar(inset, devRect.height()); | |
225 | |
226 if (viewMatrix.rectStaysRect()) { | |
227 // Temporarily #if'ed out. We don't want to pass in the devRect but | |
228 // right now it is computed in GrContext::apply_aa_to_rect and we don't | |
229 // want to throw away the work | |
230 #if 0 | |
231 SkRect devRect; | |
232 combinedMatrix.mapRect(&devRect, rect); | |
233 #endif | |
234 | |
235 set_inset_fan(fan0Pos, vertexStride, devRect, -SK_ScalarHalf, -SK_Scalar
Half); | |
236 set_inset_fan(fan1Pos, vertexStride, devRect, inset, inset); | |
237 } else { | |
238 // compute transformed (1, 0) and (0, 1) vectors | |
239 SkVector vec[2] = { | |
240 { viewMatrix[SkMatrix::kMScaleX], viewMatrix[SkMatrix::kMSkewY] }, | |
241 { viewMatrix[SkMatrix::kMSkewX], viewMatrix[SkMatrix::kMScaleY] } | |
242 }; | |
243 | |
244 vec[0].normalize(); | |
245 vec[0].scale(SK_ScalarHalf); | |
246 vec[1].normalize(); | |
247 vec[1].scale(SK_ScalarHalf); | |
248 | |
249 // create the rotated rect | |
250 fan0Pos->setRectFan(rect.fLeft, rect.fTop, | |
251 rect.fRight, rect.fBottom, vertexStride); | |
252 viewMatrix.mapPointsWithStride(fan0Pos, vertexStride, 4); | |
253 | |
254 // Now create the inset points and then outset the original | |
255 // rotated points | |
256 | |
257 // TL | |
258 *((SkPoint*)((intptr_t)fan1Pos + 0 * vertexStride)) = | |
259 *((SkPoint*)((intptr_t)fan0Pos + 0 * vertexStride)) + vec[0] + vec[1
]; | |
260 *((SkPoint*)((intptr_t)fan0Pos + 0 * vertexStride)) -= vec[0] + vec[1]; | |
261 // BL | |
262 *((SkPoint*)((intptr_t)fan1Pos + 1 * vertexStride)) = | |
263 *((SkPoint*)((intptr_t)fan0Pos + 1 * vertexStride)) + vec[0] - vec[1
]; | |
264 *((SkPoint*)((intptr_t)fan0Pos + 1 * vertexStride)) -= vec[0] - vec[1]; | |
265 // BR | |
266 *((SkPoint*)((intptr_t)fan1Pos + 2 * vertexStride)) = | |
267 *((SkPoint*)((intptr_t)fan0Pos + 2 * vertexStride)) - vec[0] - vec[1
]; | |
268 *((SkPoint*)((intptr_t)fan0Pos + 2 * vertexStride)) += vec[0] + vec[1]; | |
269 // TR | |
270 *((SkPoint*)((intptr_t)fan1Pos + 3 * vertexStride)) = | |
271 *((SkPoint*)((intptr_t)fan0Pos + 3 * vertexStride)) - vec[0] + vec[1
]; | |
272 *((SkPoint*)((intptr_t)fan0Pos + 3 * vertexStride)) += vec[0] - vec[1]; | |
273 } | |
274 | |
275 // Make verts point to vertex color and then set all the color and coverage
vertex attrs values. | |
276 verts += sizeof(SkPoint); | |
277 for (int i = 0; i < 4; ++i) { | |
278 if (kUseCoverage_CoverageAttribType == type) { | |
279 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color; | |
280 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)
) = 0; | |
281 } else { | |
282 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = 0; | |
283 } | |
284 } | |
285 | |
286 int scale; | |
287 if (inset < SK_ScalarHalf) { | |
288 scale = SkScalarFloorToInt(512.0f * inset / (inset + SK_ScalarHalf)); | |
289 SkASSERT(scale >= 0 && scale <= 255); | |
290 } else { | |
291 scale = 0xff; | |
292 } | |
293 | |
294 verts += 4 * vertexStride; | |
295 | |
296 float innerCoverage = GrNormalizeByteToFloat(scale); | |
297 GrColor scaledColor = (0xff == scale) ? color : SkAlphaMulQ(color, scale); | |
298 | |
299 for (int i = 0; i < 4; ++i) { | |
300 if (kUseCoverage_CoverageAttribType == type) { | |
301 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color; | |
302 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)
) = innerCoverage; | |
303 } else { | |
304 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor; | |
305 } | |
306 } | |
307 | |
308 target->setIndexSourceToBuffer(indexBuffer); | |
309 target->drawIndexedInstances(drawState, | |
310 gp, | |
311 kTriangles_GrPrimitiveType, | |
312 1, | |
313 kVertsPerAAFillRect, | |
314 kIndicesPerAAFillRect); | |
315 target->resetIndexSource(); | |
316 } | 481 } |
317 | 482 |
318 void GrAARectRenderer::strokeAARect(GrDrawTarget* target, | 483 void GrAARectRenderer::strokeAARect(GrDrawTarget* target, |
319 GrDrawState* drawState, | 484 GrDrawState* drawState, |
320 GrColor color, | 485 GrColor color, |
321 const SkMatrix& viewMatrix, | 486 const SkMatrix& viewMatrix, |
322 const SkRect& rect, | 487 const SkRect& rect, |
323 const SkRect& devRect, | 488 const SkRect& devRect, |
324 const SkStrokeRec& stroke) { | 489 const SkStrokeRec& stroke) { |
325 SkVector devStrokeSize; | 490 SkVector devStrokeSize; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
379 // edge, while vertex number of inner edge is 4, the same as miter-stroke. | 544 // edge, while vertex number of inner edge is 4, the same as miter-stroke. |
380 if (!miterStroke) { | 545 if (!miterStroke) { |
381 devOutside.inset(0, ry); | 546 devOutside.inset(0, ry); |
382 devOutsideAssist.outset(0, ry); | 547 devOutsideAssist.outset(0, ry); |
383 } | 548 } |
384 | 549 |
385 this->geometryStrokeAARect(target, drawState, color, viewMatrix, devOutside,
devOutsideAssist, | 550 this->geometryStrokeAARect(target, drawState, color, viewMatrix, devOutside,
devOutsideAssist, |
386 devInside, miterStroke); | 551 devInside, miterStroke); |
387 } | 552 } |
388 | 553 |
| 554 static const GrGeometryProcessor* create_rect_gp(const GrDrawState& drawState, |
| 555 GrColor color, |
| 556 CoverageAttribType* type, |
| 557 const SkMatrix& localMatrix) { |
| 558 uint32_t flags = GrDefaultGeoProcFactory::kColor_GPType; |
| 559 const GrGeometryProcessor* gp; |
| 560 if (drawState.canTweakAlphaForCoverage()) { |
| 561 gp = GrDefaultGeoProcFactory::Create(flags, color, SkMatrix::I(), localM
atrix); |
| 562 SkASSERT(gp->getVertexStride() == sizeof(GrDefaultGeoProcFactory::Positi
onColorAttr)); |
| 563 *type = kUseColor_CoverageAttribType; |
| 564 } else { |
| 565 flags |= GrDefaultGeoProcFactory::kCoverage_GPType; |
| 566 gp = GrDefaultGeoProcFactory::Create(flags, color, SkMatrix::I(), localM
atrix, |
| 567 GrColorIsOpaque(color)); |
| 568 SkASSERT(gp->getVertexStride()==sizeof(GrDefaultGeoProcFactory::Position
ColorCoverageAttr)); |
| 569 *type = kUseCoverage_CoverageAttribType; |
| 570 } |
| 571 return gp; |
| 572 } |
| 573 |
| 574 |
389 void GrAARectRenderer::geometryStrokeAARect(GrDrawTarget* target, | 575 void GrAARectRenderer::geometryStrokeAARect(GrDrawTarget* target, |
390 GrDrawState* drawState, | 576 GrDrawState* drawState, |
391 GrColor color, | 577 GrColor color, |
392 const SkMatrix& viewMatrix, | 578 const SkMatrix& viewMatrix, |
393 const SkRect& devOutside, | 579 const SkRect& devOutside, |
394 const SkRect& devOutsideAssist, | 580 const SkRect& devOutsideAssist, |
395 const SkRect& devInside, | 581 const SkRect& devInside, |
396 bool miterStroke) { | 582 bool miterStroke) { |
397 GrDrawState::AutoRestoreEffects are(drawState); | |
398 | |
399 SkMatrix localMatrix; | 583 SkMatrix localMatrix; |
400 if (!viewMatrix.invert(&localMatrix)) { | 584 if (!viewMatrix.invert(&localMatrix)) { |
401 SkDebugf("Cannot invert\n"); | 585 SkDebugf("Cannot invert\n"); |
402 return; | 586 return; |
403 } | 587 } |
404 | 588 |
405 CoverageAttribType type; | 589 CoverageAttribType type; |
406 SkAutoTUnref<const GrGeometryProcessor> gp(create_rect_gp(*drawState, color,
&type, | 590 SkAutoTUnref<const GrGeometryProcessor> gp(create_rect_gp(*drawState, color,
&type, |
407 localMatrix)); | 591 localMatrix)); |
408 | 592 |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
541 | 725 |
542 if (devInside.isEmpty()) { | 726 if (devInside.isEmpty()) { |
543 this->fillAARect(target, drawState, color, viewMatrix, devOutside, | 727 this->fillAARect(target, drawState, color, viewMatrix, devOutside, |
544 devOutside); | 728 devOutside); |
545 return; | 729 return; |
546 } | 730 } |
547 | 731 |
548 this->geometryStrokeAARect(target, drawState, color, viewMatrix, devOutside,
devOutsideAssist, | 732 this->geometryStrokeAARect(target, drawState, color, viewMatrix, devOutside,
devOutsideAssist, |
549 devInside, true); | 733 devInside, true); |
550 } | 734 } |
OLD | NEW |