| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 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 "GrAAFillRectBatch.h" | 8 #include "GrAAFillRectBatch.h" |
| 9 | 9 |
| 10 #include "GrBatch.h" | 10 #include "GrBatch.h" |
| 11 #include "GrColor.h" | 11 #include "GrColor.h" |
| 12 #include "GrDefaultGeoProcFactory.h" | 12 #include "GrDefaultGeoProcFactory.h" |
| 13 #include "GrResourceKey.h" | 13 #include "GrResourceKey.h" |
| 14 #include "GrResourceProvider.h" | 14 #include "GrResourceProvider.h" |
| 15 #include "GrTypes.h" | 15 #include "GrTypes.h" |
| 16 #include "SkMatrix.h" | 16 #include "SkMatrix.h" |
| 17 #include "SkRect.h" | 17 #include "SkRect.h" |
| 18 | 18 |
| 19 GR_DECLARE_STATIC_UNIQUE_KEY(gAAFillRectIndexBufferKey); | 19 GR_DECLARE_STATIC_UNIQUE_KEY(gAAFillRectIndexBufferKey); |
| 20 | 20 |
| 21 static void set_inset_fan(SkPoint* pts, size_t stride, | 21 static void set_inset_fan(SkPoint* pts, size_t stride, |
| 22 const SkRect& r, SkScalar dx, SkScalar dy) { | 22 const SkRect& r, SkScalar dx, SkScalar dy) { |
| 23 pts->setRectFan(r.fLeft + dx, r.fTop + dy, | 23 pts->setRectFan(r.fLeft + dx, r.fTop + dy, |
| 24 r.fRight - dx, r.fBottom - dy, stride); | 24 r.fRight - dx, r.fBottom - dy, stride); |
| 25 } | 25 } |
| 26 | 26 |
| 27 static const int kNumAAFillRectsInIndexBuffer = 256; |
| 28 static const int kVertsPerAAFillRect = 8; |
| 29 static const int kIndicesPerAAFillRect = 30; |
| 30 |
| 31 const GrIndexBuffer* get_index_buffer(GrResourceProvider* resourceProvider) { |
| 32 GR_DEFINE_STATIC_UNIQUE_KEY(gAAFillRectIndexBufferKey); |
| 33 |
| 34 static const uint16_t gFillAARectIdx[] = { |
| 35 0, 1, 5, 5, 4, 0, |
| 36 1, 2, 6, 6, 5, 1, |
| 37 2, 3, 7, 7, 6, 2, |
| 38 3, 0, 4, 4, 7, 3, |
| 39 4, 5, 6, 6, 7, 4, |
| 40 }; |
| 41 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gFillAARectIdx) == kIndicesPerAAFillRect); |
| 42 return resourceProvider->findOrCreateInstancedIndexBuffer(gFillAARectIdx, |
| 43 kIndicesPerAAFillRect, kNumAAFillRectsInIndexBuffer, kVertsPerAAFillRect
, |
| 44 gAAFillRectIndexBufferKey); |
| 45 } |
| 46 |
| 27 /* | 47 /* |
| 28 * AAFillRectBatch is templated to optionally allow the insertion of an addition
al | 48 * AAFillRectBatch is templated to optionally allow the insertion of an addition
al |
| 29 * attribute for explicit local coordinates. | 49 * attribute for explicit local coordinates. |
| 30 * To use this template, an implementation must define the following static func
tions: | 50 * To use this template, an implementation must define the following static func
tions: |
| 31 * A Geometry struct | 51 * A Geometry struct |
| 32 * | 52 * |
| 33 * bool CanCombineLocalCoords(const SkMatrix& mine, const SkMatrix& theirs, | 53 * bool CanCombineLocalCoords(const SkMatrix& mine, const SkMatrix& theirs, |
| 34 * bool usesLocalCoords) | 54 * bool usesLocalCoords) |
| 35 * | 55 * |
| 36 * GrDefaultGeoProcFactory::LocalCoords::Type LocalCoordsType() | 56 * GrDefaultGeoProcFactory::LocalCoords::Type LocalCoordsType() |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 return; | 109 return; |
| 90 } | 110 } |
| 91 | 111 |
| 92 batchTarget->initDraw(gp, this->pipeline()); | 112 batchTarget->initDraw(gp, this->pipeline()); |
| 93 | 113 |
| 94 size_t vertexStride = gp->getVertexStride(); | 114 size_t vertexStride = gp->getVertexStride(); |
| 95 SkASSERT(Base::StrideCheck(vertexStride, canTweakAlphaForCoverage, | 115 SkASSERT(Base::StrideCheck(vertexStride, canTweakAlphaForCoverage, |
| 96 this->usesLocalCoords())); | 116 this->usesLocalCoords())); |
| 97 int instanceCount = fGeoData.count(); | 117 int instanceCount = fGeoData.count(); |
| 98 | 118 |
| 99 SkAutoTUnref<const GrIndexBuffer> indexBuffer(this->getIndexBuffer( | 119 SkAutoTUnref<const GrIndexBuffer> indexBuffer(get_index_buffer( |
| 100 batchTarget->resourceProvider())); | 120 batchTarget->resourceProvider())); |
| 101 InstancedHelper helper; | 121 InstancedHelper helper; |
| 102 void* vertices = helper.init(batchTarget, kTriangles_GrPrimitiveType, ve
rtexStride, | 122 void* vertices = helper.init(batchTarget, kTriangles_GrPrimitiveType, ve
rtexStride, |
| 103 indexBuffer, kVertsPerAAFillRect, kIndicesP
erAAFillRect, | 123 indexBuffer, kVertsPerAAFillRect, kIndicesP
erAAFillRect, |
| 104 instanceCount); | 124 instanceCount); |
| 105 if (!vertices || !indexBuffer) { | 125 if (!vertices || !indexBuffer) { |
| 106 SkDebugf("Could not allocate vertices\n"); | 126 SkDebugf("Could not allocate vertices\n"); |
| 107 return; | 127 return; |
| 108 } | 128 } |
| 109 | 129 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 130 | 150 |
| 131 | 151 |
| 132 private: | 152 private: |
| 133 AAFillRectBatch() { | 153 AAFillRectBatch() { |
| 134 this->initClassID<AAFillRectBatch<Base>>(); | 154 this->initClassID<AAFillRectBatch<Base>>(); |
| 135 | 155 |
| 136 // Push back an initial geometry | 156 // Push back an initial geometry |
| 137 fGeoData.push_back(); | 157 fGeoData.push_back(); |
| 138 } | 158 } |
| 139 | 159 |
| 140 static const int kNumAAFillRectsInIndexBuffer = 256; | |
| 141 static const int kVertsPerAAFillRect = 8; | |
| 142 static const int kIndicesPerAAFillRect = 30; | |
| 143 | |
| 144 const GrIndexBuffer* getIndexBuffer(GrResourceProvider* resourceProvider) { | |
| 145 GR_DEFINE_STATIC_UNIQUE_KEY(gAAFillRectIndexBufferKey); | |
| 146 | |
| 147 static const uint16_t gFillAARectIdx[] = { | |
| 148 0, 1, 5, 5, 4, 0, | |
| 149 1, 2, 6, 6, 5, 1, | |
| 150 2, 3, 7, 7, 6, 2, | |
| 151 3, 0, 4, 4, 7, 3, | |
| 152 4, 5, 6, 6, 7, 4, | |
| 153 }; | |
| 154 GR_STATIC_ASSERT(SK_ARRAY_COUNT(gFillAARectIdx) == kIndicesPerAAFillRect
); | |
| 155 return resourceProvider->findOrCreateInstancedIndexBuffer(gFillAARectIdx
, | |
| 156 kIndicesPerAAFillRect, kNumAAFillRectsInIndexBuffer, kVertsPerAAFill
Rect, | |
| 157 gAAFillRectIndexBufferKey); | |
| 158 } | |
| 159 | |
| 160 GrColor color() const { return fBatch.fColor; } | 160 GrColor color() const { return fBatch.fColor; } |
| 161 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } | 161 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } |
| 162 bool canTweakAlphaForCoverage() const { return fBatch.fCanTweakAlphaForCover
age; } | 162 bool canTweakAlphaForCoverage() const { return fBatch.fCanTweakAlphaForCover
age; } |
| 163 bool colorIgnored() const { return fBatch.fColorIgnored; } | 163 bool colorIgnored() const { return fBatch.fColorIgnored; } |
| 164 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; } | 164 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; } |
| 165 bool coverageIgnored() const { return fBatch.fCoverageIgnored; } | 165 bool coverageIgnored() const { return fBatch.fCoverageIgnored; } |
| 166 | 166 |
| 167 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { | 167 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { |
| 168 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *t->pipel
ine(), t->bounds(), | 168 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *t->pipel
ine(), t->bounds(), |
| 169 caps)) { | 169 caps)) { |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 /*if (coverageIgnored) { | 297 /*if (coverageIgnored) { |
| 298 coverageType = Coverage::kNone_Type; | 298 coverageType = Coverage::kNone_Type; |
| 299 } else*/ if (tweakAlphaForCoverage) { | 299 } else*/ if (tweakAlphaForCoverage) { |
| 300 coverageType = Coverage::kSolid_Type; | 300 coverageType = Coverage::kSolid_Type; |
| 301 } else { | 301 } else { |
| 302 coverageType = Coverage::kAttribute_Type; | 302 coverageType = Coverage::kAttribute_Type; |
| 303 } | 303 } |
| 304 Coverage coverage(coverageType); | 304 Coverage coverage(coverageType); |
| 305 | 305 |
| 306 // We assume the caller has inverted the viewmatrix | 306 // We assume the caller has inverted the viewmatrix |
| 307 LocalCoords localCoords(usesLocalCoords ? localCoordsType : LocalCoords:
:kUnused_Type); | |
| 308 if (LocalCoords::kHasExplicit_Type == localCoordsType) { | 307 if (LocalCoords::kHasExplicit_Type == localCoordsType) { |
| 308 LocalCoords localCoords(localCoordsType); |
| 309 return GrDefaultGeoProcFactory::Create(color, coverage, localCoords,
SkMatrix::I()); | 309 return GrDefaultGeoProcFactory::Create(color, coverage, localCoords,
SkMatrix::I()); |
| 310 } else { | 310 } else { |
| 311 LocalCoords localCoords(usesLocalCoords ? localCoordsType : LocalCoo
rds::kUnused_Type); |
| 311 return CreateForDeviceSpace(color, coverage, localCoords, viewMatrix
); | 312 return CreateForDeviceSpace(color, coverage, localCoords, viewMatrix
); |
| 312 } | 313 } |
| 313 } | 314 } |
| 314 | 315 |
| 315 struct BatchTracker { | 316 struct BatchTracker { |
| 316 GrColor fColor; | 317 GrColor fColor; |
| 317 bool fUsesLocalCoords; | 318 bool fUsesLocalCoords; |
| 318 bool fColorIgnored; | 319 bool fColorIgnored; |
| 319 bool fCoverageIgnored; | 320 bool fCoverageIgnored; |
| 320 bool fCanTweakAlphaForCoverage; | 321 bool fCanTweakAlphaForCoverage; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 370 return true; | 371 return true; |
| 371 } | 372 } |
| 372 | 373 |
| 373 inline static GrDefaultGeoProcFactory::LocalCoords::Type LocalCoordsType() { | 374 inline static GrDefaultGeoProcFactory::LocalCoords::Type LocalCoordsType() { |
| 374 return GrDefaultGeoProcFactory::LocalCoords::kHasExplicit_Type; | 375 return GrDefaultGeoProcFactory::LocalCoords::kHasExplicit_Type; |
| 375 } | 376 } |
| 376 | 377 |
| 377 inline static bool StrideCheck(size_t vertexStride, bool canTweakAlphaForCov
erage, | 378 inline static bool StrideCheck(size_t vertexStride, bool canTweakAlphaForCov
erage, |
| 378 bool usesLocalCoords) { | 379 bool usesLocalCoords) { |
| 379 // Whomever created us should not have done so if there are no local coo
rds | 380 // Whomever created us should not have done so if there are no local coo
rds |
| 380 SkASSERT(usesLocalCoords); | |
| 381 return canTweakAlphaForCoverage ? | 381 return canTweakAlphaForCoverage ? |
| 382 vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorLo
calCoordAttr) : | 382 vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorLo
calCoordAttr) : |
| 383 vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorLo
calCoordCoverage); | 383 vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorLo
calCoordCoverage); |
| 384 } | 384 } |
| 385 | 385 |
| 386 inline static void FillInAttributes(intptr_t vertices, size_t vertexStride, | 386 inline static void FillInAttributes(intptr_t vertices, size_t vertexStride, |
| 387 SkPoint* fan0Pos, const Geometry& args)
{ | 387 SkPoint* fan0Pos, const Geometry& args)
{ |
| 388 SkMatrix invViewMatrix; | 388 SkMatrix invViewMatrix; |
| 389 if (!args.fViewMatrix.invert(&invViewMatrix)) { | 389 if (!args.fViewMatrix.invert(&invViewMatrix)) { |
| 390 SkASSERT(false); | 390 SkASSERT(false); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 457 geo.fColor = GrRandomColor(random); | 457 geo.fColor = GrRandomColor(random); |
| 458 geo.fViewMatrix = GrTest::TestMatrix(random); | 458 geo.fViewMatrix = GrTest::TestMatrix(random); |
| 459 geo.fLocalMatrix = GrTest::TestMatrix(random); | 459 geo.fLocalMatrix = GrTest::TestMatrix(random); |
| 460 geo.fRect = GrTest::TestRect(random); | 460 geo.fRect = GrTest::TestRect(random); |
| 461 geo.fDevRect = GrTest::TestRect(random); | 461 geo.fDevRect = GrTest::TestRect(random); |
| 462 batch->init(); | 462 batch->init(); |
| 463 return batch; | 463 return batch; |
| 464 } | 464 } |
| 465 | 465 |
| 466 #endif | 466 #endif |
| OLD | NEW |