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