| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 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 "GrRegionBatch.h" | 8 #include "GrRegionBatch.h" |
| 9 | 9 |
| 10 #include "GrDefaultGeoProcFactory.h" | 10 #include "GrDefaultGeoProcFactory.h" |
| 11 #include "GrBatchFlushState.h" | 11 #include "GrBatchFlushState.h" |
| 12 #include "GrResourceProvider.h" | 12 #include "GrResourceProvider.h" |
| 13 #include "GrVertexBatch.h" | 13 #include "GrVertexBatch.h" |
| 14 #include "SkMatrixPriv.h" | 14 #include "SkMatrixPriv.h" |
| 15 #include "SkRegion.h" | 15 #include "SkRegion.h" |
| 16 | 16 |
| 17 static const int kVertsPerInstance = 4; | 17 static const int kVertsPerInstance = 4; |
| 18 static const int kIndicesPerInstance = 6; | 18 static const int kIndicesPerInstance = 6; |
| 19 | 19 |
| 20 static sk_sp<GrGeometryProcessor> make_gp(bool readsCoverage) { | 20 static sk_sp<GrGeometryProcessor> make_gp(bool readsCoverage, const SkMatrix& vi
ewMatrix) { |
| 21 using namespace GrDefaultGeoProcFactory; | 21 using namespace GrDefaultGeoProcFactory; |
| 22 Color color(Color::kAttribute_Type); | 22 Color color(Color::kAttribute_Type); |
| 23 Coverage coverage(readsCoverage ? Coverage::kSolid_Type : Coverage::kNone_Ty
pe); | 23 Coverage coverage(readsCoverage ? Coverage::kSolid_Type : Coverage::kNone_Ty
pe); |
| 24 | 24 |
| 25 LocalCoords localCoords(LocalCoords::kHasExplicit_Type); | 25 LocalCoords localCoords(LocalCoords::kUsePosition_Type); |
| 26 return GrDefaultGeoProcFactory::Make(color, coverage, localCoords, SkMatrix:
:I()); | 26 return GrDefaultGeoProcFactory::Make(color, coverage, localCoords, viewMatri
x); |
| 27 } | 27 } |
| 28 | 28 |
| 29 static int tesselate_region(intptr_t vertices, | 29 static void tesselate_region(intptr_t vertices, |
| 30 size_t vertexStride, | 30 size_t vertexStride, |
| 31 GrColor color, | 31 GrColor color, |
| 32 const SkMatrix& viewMatrix, | |
| 33 const SkRegion& region) { | 32 const SkRegion& region) { |
| 34 SkRegion::Iterator iter(region); | 33 SkRegion::Iterator iter(region); |
| 35 | 34 |
| 36 intptr_t verts = vertices; | 35 intptr_t verts = vertices; |
| 37 while (!iter.done()) { | 36 while (!iter.done()) { |
| 38 SkIRect rect = iter.rect(); | 37 SkRect rect = SkRect::Make(iter.rect()); |
| 39 SkPoint* position = (SkPoint*) verts; | 38 SkPoint* position = (SkPoint*) verts; |
| 40 position->setIRectFan(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom,
vertexStride); | 39 position->setRectFan(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, v
ertexStride); |
| 41 | |
| 42 static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor); | |
| 43 SkPoint* localPosition = (SkPoint*) (verts + kLocalOffset); | |
| 44 localPosition->setIRectFan(rect.fLeft, rect.fTop, rect.fRight, rect.fBot
tom, vertexStride); | |
| 45 | 40 |
| 46 static const int kColorOffset = sizeof(SkPoint); | 41 static const int kColorOffset = sizeof(SkPoint); |
| 47 GrColor* vertColor = reinterpret_cast<GrColor*>(verts + kColorOffset); | 42 GrColor* vertColor = reinterpret_cast<GrColor*>(verts + kColorOffset); |
| 48 for (int i = 0; i < kVertsPerInstance; i++) { | 43 for (int i = 0; i < kVertsPerInstance; i++) { |
| 49 *vertColor = color; | 44 *vertColor = color; |
| 50 vertColor = (GrColor*) ((intptr_t) vertColor + vertexStride); | 45 vertColor = (GrColor*) ((intptr_t) vertColor + vertexStride); |
| 51 } | 46 } |
| 52 | 47 |
| 53 verts += vertexStride * kVertsPerInstance; | 48 verts += vertexStride * kVertsPerInstance; |
| 54 iter.next(); | 49 iter.next(); |
| 55 } | 50 } |
| 56 | |
| 57 SkPoint* positions = reinterpret_cast<SkPoint*>(vertices); | |
| 58 int numRects = region.computeRegionComplexity(); | |
| 59 SkMatrixPriv::MapPointsWithStride(viewMatrix, positions, vertexStride, | |
| 60 numRects * kVertsPerInstance); | |
| 61 | |
| 62 return numRects; | |
| 63 } | 51 } |
| 64 | 52 |
| 65 class RegionBatch : public GrVertexBatch { | 53 class RegionBatch : public GrVertexBatch { |
| 66 public: | 54 public: |
| 67 DEFINE_BATCH_CLASS_ID | 55 DEFINE_BATCH_CLASS_ID |
| 68 | 56 |
| 69 RegionBatch(GrColor color, const SkMatrix& viewMatrix, const SkRegion& regio
n) | 57 RegionBatch(GrColor color, const SkMatrix& viewMatrix, const SkRegion& regio
n) |
| 70 : INHERITED(ClassID()) { | 58 : INHERITED(ClassID()) |
| 71 | 59 , fViewMatrix(viewMatrix) |
| 60 { |
| 72 RegionInfo& info = fRegions.push_back(); | 61 RegionInfo& info = fRegions.push_back(); |
| 73 info.fColor = color; | 62 info.fColor = color; |
| 74 info.fViewMatrix = viewMatrix; | |
| 75 info.fRegion = region; | 63 info.fRegion = region; |
| 76 | 64 |
| 77 SkRect bounds = SkRect::Make(region.getBounds()); | 65 SkRect bounds = SkRect::Make(region.getBounds()); |
| 78 this->setTransformedBounds(bounds, viewMatrix, HasAABloat::kNo, IsZeroAr
ea::kNo); | 66 this->setTransformedBounds(bounds, viewMatrix, HasAABloat::kNo, IsZeroAr
ea::kNo); |
| 79 } | 67 } |
| 80 | 68 |
| 81 const char* name() const override { return "GrRegionBatch"; } | 69 const char* name() const override { return "GrRegionBatch"; } |
| 82 | 70 |
| 83 SkString dumpInfo() const override { | 71 SkString dumpInfo() const override { |
| 84 SkString str; | 72 SkString str; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 101 } | 89 } |
| 102 | 90 |
| 103 void initBatchTracker(const GrXPOverridesForBatch& overrides) override { | 91 void initBatchTracker(const GrXPOverridesForBatch& overrides) override { |
| 104 overrides.getOverrideColorIfSet(&fRegions[0].fColor); | 92 overrides.getOverrideColorIfSet(&fRegions[0].fColor); |
| 105 fOverrides = overrides; | 93 fOverrides = overrides; |
| 106 } | 94 } |
| 107 | 95 |
| 108 private: | 96 private: |
| 109 | 97 |
| 110 void onPrepareDraws(Target* target) const override { | 98 void onPrepareDraws(Target* target) const override { |
| 111 sk_sp<GrGeometryProcessor> gp = make_gp(fOverrides.readsCoverage()); | 99 sk_sp<GrGeometryProcessor> gp = make_gp(fOverrides.readsCoverage(), fVie
wMatrix); |
| 112 if (!gp) { | 100 if (!gp) { |
| 113 SkDebugf("Couldn't create GrGeometryProcessor\n"); | 101 SkDebugf("Couldn't create GrGeometryProcessor\n"); |
| 114 return; | 102 return; |
| 115 } | 103 } |
| 116 SkASSERT(gp->getVertexStride() == | 104 SkASSERT(gp->getVertexStride() == sizeof(GrDefaultGeoProcFactory::Positi
onColorAttr)); |
| 117 sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr)); | |
| 118 | 105 |
| 119 int numRegions = fRegions.count(); | 106 int numRegions = fRegions.count(); |
| 120 int numRects = 0; | 107 int numRects = 0; |
| 121 for (int i = 0; i < numRegions; i++) { | 108 for (int i = 0; i < numRegions; i++) { |
| 122 numRects += fRegions[i].fRegion.computeRegionComplexity(); | 109 numRects += fRegions[i].fRegion.computeRegionComplexity(); |
| 123 } | 110 } |
| 124 | 111 |
| 125 size_t vertexStride = gp->getVertexStride(); | 112 size_t vertexStride = gp->getVertexStride(); |
| 126 SkAutoTUnref<const GrBuffer> indexBuffer(target->resourceProvider()->ref
QuadIndexBuffer()); | 113 SkAutoTUnref<const GrBuffer> indexBuffer(target->resourceProvider()->ref
QuadIndexBuffer()); |
| 127 InstancedHelper helper; | 114 InstancedHelper helper; |
| 128 void* vertices = helper.init(target, kTriangles_GrPrimitiveType, vertexS
tride, | 115 void* vertices = helper.init(target, kTriangles_GrPrimitiveType, vertexS
tride, |
| 129 indexBuffer, kVertsPerInstance, kIndicesPer
Instance, numRects); | 116 indexBuffer, kVertsPerInstance, kIndicesPer
Instance, numRects); |
| 130 if (!vertices || !indexBuffer) { | 117 if (!vertices || !indexBuffer) { |
| 131 SkDebugf("Could not allocate vertices\n"); | 118 SkDebugf("Could not allocate vertices\n"); |
| 132 return; | 119 return; |
| 133 } | 120 } |
| 134 | 121 |
| 135 intptr_t verts = reinterpret_cast<intptr_t>(vertices); | 122 intptr_t verts = reinterpret_cast<intptr_t>(vertices); |
| 136 for (int i = 0; i < numRegions; i++) { | 123 for (int i = 0; i < numRegions; i++) { |
| 137 int numRectsInRegion = tesselate_region(verts, vertexStride, fRegion
s[i].fColor, | 124 tesselate_region(verts, vertexStride, fRegions[i].fColor, fRegions[i
].fRegion); |
| 138 fRegions[i].fViewMatrix, fRe
gions[i].fRegion); | 125 int numRectsInRegion = fRegions[i].fRegion.computeRegionComplexity()
; |
| 139 verts += numRectsInRegion * kVertsPerInstance * vertexStride; | 126 verts += numRectsInRegion * kVertsPerInstance * vertexStride; |
| 140 } | 127 } |
| 141 helper.recordDraw(target, gp.get()); | 128 helper.recordDraw(target, gp.get()); |
| 142 } | 129 } |
| 143 | 130 |
| 144 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { | 131 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { |
| 145 RegionBatch* that = t->cast<RegionBatch>(); | 132 RegionBatch* that = t->cast<RegionBatch>(); |
| 146 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pi
peline(), | 133 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pi
peline(), |
| 147 that->bounds(), caps)) { | 134 that->bounds(), caps)) { |
| 148 return false; | 135 return false; |
| 149 } | 136 } |
| 150 | 137 |
| 138 if (fViewMatrix != that->fViewMatrix) { |
| 139 return false; |
| 140 } |
| 141 |
| 151 fRegions.push_back_n(that->fRegions.count(), that->fRegions.begin()); | 142 fRegions.push_back_n(that->fRegions.count(), that->fRegions.begin()); |
| 152 this->joinBounds(*that); | 143 this->joinBounds(*that); |
| 153 return true; | 144 return true; |
| 154 } | 145 } |
| 155 | 146 |
| 156 struct RegionInfo { | 147 struct RegionInfo { |
| 157 GrColor fColor; | 148 GrColor fColor; |
| 158 SkMatrix fViewMatrix; | |
| 159 SkRegion fRegion; | 149 SkRegion fRegion; |
| 160 }; | 150 }; |
| 161 | 151 |
| 152 SkMatrix fViewMatrix; |
| 162 GrXPOverridesForBatch fOverrides; | 153 GrXPOverridesForBatch fOverrides; |
| 163 SkSTArray<1, RegionInfo, true> fRegions; | 154 SkSTArray<1, RegionInfo, true> fRegions; |
| 164 | 155 |
| 165 typedef GrVertexBatch INHERITED; | 156 typedef GrVertexBatch INHERITED; |
| 166 }; | 157 }; |
| 167 | 158 |
| 168 namespace GrRegionBatch { | 159 namespace GrRegionBatch { |
| 169 | 160 |
| 170 GrDrawBatch* Create(GrColor color, | 161 GrDrawBatch* Create(GrColor color, |
| 171 const SkMatrix& viewMatrix, | 162 const SkMatrix& viewMatrix, |
| 172 const SkRegion& region) { | 163 const SkRegion& region) { |
| 173 return new RegionBatch(color, viewMatrix, region); | 164 return new RegionBatch(color, viewMatrix, region); |
| 174 } | 165 } |
| 175 | 166 |
| 176 }; | 167 }; |
| OLD | NEW |