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