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 kColorOffset = sizeof(SkPoint); | |
bsalomon
2016/08/25 21:19:50
blah blah local coords
msarett
2016/08/25 21:26:35
Done, thanks.
| |
43 GrColor* vertColor = reinterpret_cast<GrColor*>(verts + kColorOffset); | |
44 for (int i = 0; i < kVertsPerInstance; i++) { | |
45 *vertColor = color; | |
46 vertColor = (GrColor*) ((intptr_t) vertColor + vertexStride); | |
47 } | |
48 | |
49 verts += vertexStride * kVertsPerInstance; | |
50 iter.next(); | |
51 } | |
52 | |
53 SkPoint* positions = reinterpret_cast<SkPoint*>(vertices); | |
54 int numRects = region.computeRegionComplexity(); | |
55 SkMatrixPriv::MapPointsWithStride(viewMatrix, positions, vertexStride, | |
56 numRects * kVertsPerInstance); | |
57 | |
58 return numRects; | |
59 } | |
60 | |
61 class RegionBatch : public GrVertexBatch { | |
62 public: | |
63 DEFINE_BATCH_CLASS_ID | |
64 | |
65 RegionBatch(GrColor color, const SkMatrix& viewMatrix, const SkRegion& regio n) | |
66 : INHERITED(ClassID()) { | |
67 | |
68 RegionInfo& info = fRegions.push_back(); | |
69 info.fColor = color; | |
70 info.fViewMatrix = viewMatrix; | |
71 info.fRegion = region; | |
72 | |
73 SkRect bounds = SkRect::Make(region.getBounds()); | |
74 this->setTransformedBounds(bounds, viewMatrix, HasAABloat::kNo, IsZeroAr ea::kNo); | |
75 } | |
76 | |
77 const char* name() const override { return "GrRegionBatch"; } | |
78 | |
79 SkString dumpInfo() const override { | |
80 SkString str; | |
81 str.appendf("# batched: %d\n", fRegions.count()); | |
82 for (int i = 0; i < fRegions.count(); ++i) { | |
83 const RegionInfo& info = fRegions[i]; | |
84 str.appendf("%d: Color: 0x%08x, Region with %d rects\n", | |
85 i, info.fColor, info.fRegion.computeRegionComplexity()); | |
86 } | |
87 str.append(INHERITED::dumpInfo()); | |
88 return str; | |
89 } | |
90 | |
91 void computePipelineOptimizations(GrInitInvariantOutput* color, | |
92 GrInitInvariantOutput* coverage, | |
93 GrBatchToXPOverrides* overrides) const ove rride { | |
94 // When this is called on a batch, there is only one geometry bundle | |
bsalomon
2016/08/25 21:19:50
Can you change "geometry bundle" to "region"?
msarett
2016/08/25 21:26:35
Done.
| |
95 color->setKnownFourComponents(fRegions[0].fColor); | |
96 coverage->setKnownSingleComponent(0xff); | |
97 } | |
98 | |
99 void initBatchTracker(const GrXPOverridesForBatch& overrides) override { | |
100 overrides.getOverrideColorIfSet(&fRegions[0].fColor); | |
101 fOverrides = overrides; | |
102 } | |
103 | |
104 private: | |
105 | |
106 void onPrepareDraws(Target* target) const override { | |
107 sk_sp<GrGeometryProcessor> gp = make_gp(fOverrides.readsCoverage()); | |
108 if (!gp) { | |
109 SkDebugf("Couldn't create GrGeometryProcessor\n"); | |
110 return; | |
111 } | |
112 SkASSERT(gp->getVertexStride() == | |
113 sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr)); | |
114 | |
115 int numRegions = fRegions.count(); | |
116 int numRects = 0; | |
117 for (int i = 0; i < numRegions; i++) { | |
118 numRects += fRegions[i].fRegion.computeRegionComplexity(); | |
119 } | |
120 | |
121 size_t vertexStride = gp->getVertexStride(); | |
122 SkAutoTUnref<const GrBuffer> indexBuffer(target->resourceProvider()->ref QuadIndexBuffer()); | |
123 InstancedHelper helper; | |
124 void* vertices = helper.init(target, kTriangles_GrPrimitiveType, vertexS tride, | |
125 indexBuffer, kVertsPerInstance, kIndicesPer Instance, numRects); | |
126 if (!vertices || !indexBuffer) { | |
127 SkDebugf("Could not allocate vertices\n"); | |
128 return; | |
129 } | |
130 | |
131 intptr_t verts = reinterpret_cast<intptr_t>(vertices); | |
132 for (int i = 0; i < numRegions; i++) { | |
133 int numRectsInRegion = tesselate_region(verts, vertexStride, fRegion s[i].fColor, | |
134 fRegions[i].fViewMatrix, fRe gions[i].fRegion); | |
135 verts += numRectsInRegion * kVertsPerInstance * vertexStride; | |
136 } | |
137 helper.recordDraw(target, gp.get()); | |
138 } | |
139 | |
140 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { | |
141 RegionBatch* that = t->cast<RegionBatch>(); | |
142 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pi peline(), | |
143 that->bounds(), caps)) { | |
144 return false; | |
145 } | |
146 | |
147 if (fOverrides.canTweakAlphaForCoverage() && !that->fOverrides.canTweakA lphaForCoverage()) { | |
bsalomon
2016/08/25 21:19:50
This can be removed.
msarett
2016/08/25 21:26:35
Done.
| |
148 fOverrides = that->fOverrides; | |
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 |