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 "GrNonAAFillRectBatch.h" | 8 #include "GrRegionBatch.h" |
9 | 9 |
10 #include "GrBatchFlushState.h" | 10 #include "GrBatchFlushState.h" |
11 #include "GrColor.h" | 11 #include "GrColor.h" |
12 #include "GrDefaultGeoProcFactory.h" | 12 #include "GrDefaultGeoProcFactory.h" |
13 #include "GrPrimitiveProcessor.h" | 13 #include "GrPrimitiveProcessor.h" |
14 #include "GrResourceProvider.h" | 14 #include "GrResourceProvider.h" |
15 #include "GrQuad.h" | 15 #include "GrQuad.h" |
16 #include "GrVertexBatch.h" | 16 #include "GrVertexBatch.h" |
17 | 17 |
18 #include "SkMatrixPriv.h" | 18 #include "SkMatrixPriv.h" |
19 #include "SkRegion.h" | |
19 | 20 |
20 static const int kVertsPerInstance = 4; | 21 static const int kVertsPerInstance = 4; |
21 static const int kIndicesPerInstance = 6; | 22 static const int kIndicesPerInstance = 6; |
22 | 23 |
23 /** We always use per-vertex colors so that rects can be batched across color ch anges. Sometimes | 24 /** We always use per-vertex colors so that rects can be batched across color ch anges. Sometimes |
24 we have explicit local coords and sometimes not. We *could* always provide explicit local | 25 we have explicit local coords and sometimes not. We *could* always provide explicit local |
25 coords and just duplicate the positions when the caller hasn't provided a lo cal coord rect, | 26 coords and just duplicate the positions when the caller hasn't provided a lo cal coord rect, |
26 but we haven't seen a use case which frequently switches between local rect and no local | 27 but we haven't seen a use case which frequently switches between local rect and no local |
27 rect draws. | 28 rect draws. |
28 | 29 |
29 The vertex attrib order is always pos, color, [local coords]. | 30 The vertex attrib order is always pos, color, [local coords]. |
30 */ | 31 */ |
31 static sk_sp<GrGeometryProcessor> make_gp(bool readsCoverage) { | 32 static sk_sp<GrGeometryProcessor> make_gp(bool readsCoverage) { |
32 using namespace GrDefaultGeoProcFactory; | 33 using namespace GrDefaultGeoProcFactory; |
33 Color color(Color::kAttribute_Type); | 34 Color color(Color::kAttribute_Type); |
34 Coverage coverage(readsCoverage ? Coverage::kSolid_Type : Coverage::kNone_Ty pe); | 35 Coverage coverage(readsCoverage ? Coverage::kSolid_Type : Coverage::kNone_Ty pe); |
35 | 36 |
36 LocalCoords localCoords(LocalCoords::kHasExplicit_Type); | 37 LocalCoords localCoords(LocalCoords::kHasExplicit_Type); |
37 return GrDefaultGeoProcFactory::Make(color, coverage, localCoords, SkMatrix: :I()); | 38 return GrDefaultGeoProcFactory::Make(color, coverage, localCoords, SkMatrix: :I()); |
38 } | 39 } |
39 | 40 |
40 static void tesselate(intptr_t vertices, | 41 static void tesselate(intptr_t vertices, |
41 size_t vertexStride, | 42 size_t vertexStride, |
42 GrColor color, | 43 GrColor color, |
43 const SkMatrix* viewMatrix, | 44 const SkMatrix* viewMatrix, |
44 const SkRect& rect, | 45 const SkRect& rect) { |
45 const GrQuad* localQuad) { | |
46 SkPoint* positions = reinterpret_cast<SkPoint*>(vertices); | 46 SkPoint* positions = reinterpret_cast<SkPoint*>(vertices); |
47 | 47 |
48 positions->setRectFan(rect.fLeft, rect.fTop, | 48 positions->setRectFan(rect.fLeft, rect.fTop, |
49 rect.fRight, rect.fBottom, vertexStride); | 49 rect.fRight, rect.fBottom, vertexStride); |
50 | 50 |
51 if (viewMatrix) { | 51 if (viewMatrix) { |
52 SkMatrixPriv::MapPointsWithStride(*viewMatrix, positions, vertexStride, kVertsPerInstance); | 52 SkMatrixPriv::MapPointsWithStride(*viewMatrix, positions, vertexStride, kVertsPerInstance); |
53 } | 53 } |
54 | 54 |
55 // Setup local coords | |
56 // TODO we should only do this if local coords are being read | |
57 if (localQuad) { | |
58 static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor); | |
59 for (int i = 0; i < kVertsPerInstance; i++) { | |
60 SkPoint* coords = reinterpret_cast<SkPoint*>(vertices + kLocalOffset + | |
61 i * vertexStride); | |
62 *coords = localQuad->point(i); | |
63 } | |
64 } | |
65 | |
66 static const int kColorOffset = sizeof(SkPoint); | 55 static const int kColorOffset = sizeof(SkPoint); |
67 GrColor* vertColor = reinterpret_cast<GrColor*>(vertices + kColorOffset); | 56 GrColor* vertColor = reinterpret_cast<GrColor*>(vertices + kColorOffset); |
68 for (int j = 0; j < 4; ++j) { | 57 for (int j = 0; j < 4; ++j) { |
69 *vertColor = color; | 58 *vertColor = color; |
70 vertColor = (GrColor*) ((intptr_t) vertColor + vertexStride); | 59 vertColor = (GrColor*) ((intptr_t) vertColor + vertexStride); |
71 } | 60 } |
72 } | 61 } |
73 | 62 |
74 class NonAAFillRectBatch : public GrVertexBatch { | 63 class RegionBatch : public GrVertexBatch { |
msarett
2016/08/25 16:36:17
Could/should this extend NonAAFillRectBatch?
bsalomon
2016/08/25 17:49:02
I don't think it should inherit from it. It's poss
msarett
2016/08/25 20:51:42
Alright cool, thanks.
| |
75 public: | 64 public: |
76 DEFINE_BATCH_CLASS_ID | 65 DEFINE_BATCH_CLASS_ID |
77 | 66 |
78 NonAAFillRectBatch(GrColor color, const SkMatrix& viewMatrix, const SkRect& rect, | 67 RegionBatch(GrColor color, const SkMatrix& viewMatrix, const SkRegion& regio n) |
79 const SkRect* localRect, const SkMatrix* localMatrix) | |
msarett
2016/08/25 16:36:17
Do I need to worry about localRect and localMatrix
bsalomon
2016/08/25 17:49:02
no
msarett
2016/08/25 20:51:42
Acknowledged.
| |
80 : INHERITED(ClassID()) { | 68 : INHERITED(ClassID()) { |
81 SkASSERT(!viewMatrix.hasPerspective() && (!localMatrix || | 69 SkRegion::Iterator iter(region); |
82 !localMatrix->hasPerspective() )); | 70 |
83 RectInfo& info = fRects.push_back(); | 71 while (!iter.done()) { |
msarett
2016/08/25 16:36:17
Any way to use push_back_n() here?
bsalomon
2016/08/25 17:49:02
I think we should keep it in region form until gen
msarett
2016/08/25 20:51:42
Done.
| |
84 info.fColor = color; | 72 RectInfo& info = fRects.push_back(); |
85 info.fViewMatrix = viewMatrix; | 73 info.fColor = color; |
86 info.fRect = rect; | 74 info.fViewMatrix = viewMatrix; |
87 if (localRect && localMatrix) { | 75 SkRect rect = SkRect::Make(iter.rect()); |
88 info.fLocalQuad.setFromMappedRect(*localRect, *localMatrix); | 76 info.fRect = rect; |
89 } else if (localRect) { | 77 iter.next(); |
90 info.fLocalQuad.set(*localRect); | |
91 } else if (localMatrix) { | |
92 info.fLocalQuad.setFromMappedRect(rect, *localMatrix); | |
93 } else { | |
94 info.fLocalQuad.set(rect); | |
95 } | 78 } |
96 this->setTransformedBounds(fRects[0].fRect, viewMatrix, HasAABloat::kNo, IsZeroArea::kNo); | 79 |
80 SkRect bounds = SkRect::Make(region.getBounds()); | |
81 this->setTransformedBounds(bounds, viewMatrix, HasAABloat::kNo, IsZeroAr ea::kNo); | |
97 } | 82 } |
98 | 83 |
99 const char* name() const override { return "NonAAFillRectBatch"; } | 84 const char* name() const override { return "GrRegionBatch"; } |
100 | 85 |
101 SkString dumpInfo() const override { | 86 SkString dumpInfo() const override { |
102 SkString str; | 87 SkString str; |
103 str.appendf("# batched: %d\n", fRects.count()); | 88 str.appendf("# batched: %d\n", fRects.count()); |
104 for (int i = 0; i < fRects.count(); ++i) { | 89 for (int i = 0; i < fRects.count(); ++i) { |
105 const RectInfo& info = fRects[i]; | 90 const RectInfo& info = fRects[i]; |
106 str.appendf("%d: Color: 0x%08x, Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", | 91 str.appendf("%d: Color: 0x%08x, Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", |
107 i, info.fColor, | 92 i, info.fColor, |
108 info.fRect.fLeft, info.fRect.fTop, info.fRect.fRight, in fo.fRect.fBottom); | 93 info.fRect.fLeft, info.fRect.fTop, info.fRect.fRight, in fo.fRect.fBottom); |
109 } | 94 } |
110 str.append(INHERITED::dumpInfo()); | 95 str.append(INHERITED::dumpInfo()); |
111 return str; | 96 return str; |
112 } | 97 } |
113 | 98 |
114 void computePipelineOptimizations(GrInitInvariantOutput* color, | 99 void computePipelineOptimizations(GrInitInvariantOutput* color, |
115 GrInitInvariantOutput* coverage, | 100 GrInitInvariantOutput* coverage, |
116 GrBatchToXPOverrides* overrides) const ove rride { | 101 GrBatchToXPOverrides* overrides) const ove rride { |
117 // When this is called on a batch, there is only one geometry bundle | 102 // When this is called on a batch, there is only one geometry bundle |
118 color->setKnownFourComponents(fRects[0].fColor); | 103 color->setKnownFourComponents(fRects[0].fColor); |
119 coverage->setKnownSingleComponent(0xff); | 104 coverage->setKnownSingleComponent(0xff); |
120 } | 105 } |
121 | 106 |
122 void initBatchTracker(const GrXPOverridesForBatch& overrides) override { | 107 void initBatchTracker(const GrXPOverridesForBatch& overrides) override { |
123 overrides.getOverrideColorIfSet(&fRects[0].fColor); | 108 overrides.getOverrideColorIfSet(&fRects[0].fColor); |
124 fOverrides = overrides; | 109 fOverrides = overrides; |
125 } | 110 } |
126 | 111 |
127 private: | 112 private: |
128 NonAAFillRectBatch() : INHERITED(ClassID()) {} | |
129 | 113 |
130 void onPrepareDraws(Target* target) const override { | 114 void onPrepareDraws(Target* target) const override { |
131 sk_sp<GrGeometryProcessor> gp = make_gp(fOverrides.readsCoverage()); | 115 sk_sp<GrGeometryProcessor> gp = make_gp(fOverrides.readsCoverage()); |
132 if (!gp) { | 116 if (!gp) { |
133 SkDebugf("Couldn't create GrGeometryProcessor\n"); | 117 SkDebugf("Couldn't create GrGeometryProcessor\n"); |
134 return; | 118 return; |
135 } | 119 } |
136 SkASSERT(gp->getVertexStride() == | 120 SkASSERT(gp->getVertexStride() == |
137 sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr)); | 121 sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr)); |
138 | 122 |
139 size_t vertexStride = gp->getVertexStride(); | 123 size_t vertexStride = gp->getVertexStride(); |
140 int instanceCount = fRects.count(); | 124 int instanceCount = fRects.count(); |
141 | 125 |
142 SkAutoTUnref<const GrBuffer> indexBuffer(target->resourceProvider()->ref QuadIndexBuffer()); | 126 SkAutoTUnref<const GrBuffer> indexBuffer(target->resourceProvider()->ref QuadIndexBuffer()); |
143 InstancedHelper helper; | 127 InstancedHelper helper; |
144 void* vertices = helper.init(target, kTriangles_GrPrimitiveType, vertexS tride, | 128 void* vertices = helper.init(target, kTriangles_GrPrimitiveType, vertexS tride, |
145 indexBuffer, kVertsPerInstance, | 129 indexBuffer, kVertsPerInstance, |
146 kIndicesPerInstance, instanceCount); | 130 kIndicesPerInstance, instanceCount); |
147 if (!vertices || !indexBuffer) { | 131 if (!vertices || !indexBuffer) { |
148 SkDebugf("Could not allocate vertices\n"); | 132 SkDebugf("Could not allocate vertices\n"); |
149 return; | 133 return; |
150 } | 134 } |
151 | 135 |
152 for (int i = 0; i < instanceCount; i++) { | 136 for (int i = 0; i < instanceCount; i++) { |
153 intptr_t verts = reinterpret_cast<intptr_t>(vertices) + | 137 intptr_t verts = reinterpret_cast<intptr_t>(vertices) + |
154 i * kVertsPerInstance * vertexStride; | 138 i * kVertsPerInstance * vertexStride; |
155 tesselate(verts, vertexStride, fRects[i].fColor, &fRects[i].fViewMat rix, | 139 tesselate(verts, vertexStride, fRects[i].fColor, &fRects[i].fViewMat rix, |
156 fRects[i].fRect, &fRects[i].fLocalQuad); | 140 fRects[i].fRect); |
157 } | 141 } |
158 helper.recordDraw(target, gp.get()); | 142 helper.recordDraw(target, gp.get()); |
159 } | 143 } |
160 | 144 |
161 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { | 145 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { |
162 NonAAFillRectBatch* that = t->cast<NonAAFillRectBatch>(); | 146 RegionBatch* that = t->cast<RegionBatch>(); |
163 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pi peline(), | 147 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pi peline(), |
164 that->bounds(), caps)) { | 148 that->bounds(), caps)) { |
165 return false; | 149 return false; |
166 } | 150 } |
167 | 151 |
168 // In the event of two batches, one who can tweak, one who cannot, we ju st fall back to | |
169 // not tweaking | |
170 if (fOverrides.canTweakAlphaForCoverage() && !that->fOverrides.canTweakA lphaForCoverage()) { | 152 if (fOverrides.canTweakAlphaForCoverage() && !that->fOverrides.canTweakA lphaForCoverage()) { |
171 fOverrides = that->fOverrides; | 153 fOverrides = that->fOverrides; |
172 } | 154 } |
173 | 155 |
174 fRects.push_back_n(that->fRects.count(), that->fRects.begin()); | 156 fRects.push_back_n(that->fRects.count(), that->fRects.begin()); |
175 this->joinBounds(*that); | 157 this->joinBounds(*that); |
176 return true; | 158 return true; |
177 } | 159 } |
178 | 160 |
179 struct RectInfo { | 161 struct RectInfo { |
180 GrColor fColor; | 162 GrColor fColor; |
181 SkMatrix fViewMatrix; | 163 SkMatrix fViewMatrix; |
182 SkRect fRect; | 164 SkRect fRect; |
183 GrQuad fLocalQuad; | |
184 }; | 165 }; |
185 | 166 |
186 GrXPOverridesForBatch fOverrides; | 167 GrXPOverridesForBatch fOverrides; |
187 SkSTArray<1, RectInfo, true> fRects; | 168 SkSTArray<1, RectInfo, true> fRects; |
188 | 169 |
189 typedef GrVertexBatch INHERITED; | 170 typedef GrVertexBatch INHERITED; |
190 }; | 171 }; |
191 | 172 |
192 namespace GrNonAAFillRectBatch { | 173 namespace GrRegionBatch { |
193 | 174 |
194 GrDrawBatch* Create(GrColor color, | 175 GrDrawBatch* Create(GrColor color, |
195 const SkMatrix& viewMatrix, | 176 const SkMatrix& viewMatrix, |
196 const SkRect& rect, | 177 const SkRegion& region) { |
197 const SkRect* localRect, | 178 return new RegionBatch(color, viewMatrix, region); |
198 const SkMatrix* localMatrix) { | |
199 return new NonAAFillRectBatch(color, viewMatrix, rect, localRect, localMatri x); | |
200 } | 179 } |
201 | 180 |
202 }; | 181 }; |
203 | |
204 //////////////////////////////////////////////////////////////////////////////// /////////////////// | |
205 | |
206 #ifdef GR_TEST_UTILS | |
207 | |
208 #include "GrBatchTest.h" | |
209 | |
210 DRAW_BATCH_TEST_DEFINE(RectBatch) { | |
211 GrColor color = GrRandomColor(random); | |
212 SkRect rect = GrTest::TestRect(random); | |
213 SkRect localRect = GrTest::TestRect(random); | |
214 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random); | |
215 SkMatrix localMatrix = GrTest::TestMatrix(random); | |
216 | |
217 bool hasLocalRect = random->nextBool(); | |
218 bool hasLocalMatrix = random->nextBool(); | |
219 return GrNonAAFillRectBatch::Create(color, viewMatrix, rect, | |
220 hasLocalRect ? &localRect : nullptr, | |
221 hasLocalMatrix ? &localMatrix : nullptr) ; | |
222 } | |
223 | |
224 #endif | |
OLD | NEW |