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