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 "GrNonAAFillRectBatch.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 static const int kVertsPerInstance = 4; | 18 static const int kVertsPerInstance = 4; |
19 static const int kIndicesPerInstance = 6; | 19 static const int kIndicesPerInstance = 6; |
20 | 20 |
21 /** We always use per-vertex colors so that rects can be batched across color ch
anges. Sometimes | 21 /** We always use per-vertex colors so that rects can be batched across color ch
anges. Sometimes |
22 we have explicit local coords and sometimes not. We *could* always provide
explicit local | 22 we have explicit local coords and sometimes not. We *could* always provide
explicit local |
23 coords and just duplicate the positions when the caller hasn't provided a lo
cal coord rect, | 23 coords and just duplicate the positions when the caller hasn't provided a lo
cal coord rect, |
24 but we haven't seen a use case which frequently switches between local rect
and no local | 24 but we haven't seen a use case which frequently switches between local rect
and no local |
25 rect draws. | 25 rect draws. |
26 | 26 |
27 The vertex attrib order is always pos, color, [local coords]. | 27 The vertex attrib order is always pos, color, [local coords]. |
28 */ | 28 */ |
29 static sk_sp<GrGeometryProcessor> make_gp(const SkMatrix& viewMatrix, | 29 static sk_sp<GrGeometryProcessor> make_persp_gp(const SkMatrix& viewMatrix, |
30 bool readsCoverage, | 30 bool readsCoverage, |
31 bool hasExplicitLocalCoords, | 31 bool hasExplicitLocalCoords, |
32 const SkMatrix* localMatrix) { | 32 const SkMatrix* localMatrix) { |
| 33 SkASSERT(viewMatrix.hasPerspective() || (localMatrix && localMatrix->hasPers
pective())); |
| 34 |
33 using namespace GrDefaultGeoProcFactory; | 35 using namespace GrDefaultGeoProcFactory; |
34 Color color(Color::kAttribute_Type); | 36 Color color(Color::kAttribute_Type); |
35 Coverage coverage(readsCoverage ? Coverage::kSolid_Type : Coverage::kNone_Ty
pe); | 37 Coverage coverage(readsCoverage ? Coverage::kSolid_Type : Coverage::kNone_Ty
pe); |
36 | 38 |
37 // If we have perspective on the viewMatrix then we won't map on the CPU, no
r will we map | 39 // If we have perspective on the viewMatrix then we won't map on the CPU, no
r will we map |
38 // the local rect on the cpu (in case the localMatrix also has perspective). | 40 // the local rect on the cpu (in case the localMatrix also has perspective). |
39 // Otherwise, if we have a local rect, then we apply the localMatrix directl
y to the localRect | 41 // Otherwise, if we have a local rect, then we apply the localMatrix directl
y to the localRect |
40 // to generate vertex local coords | 42 // to generate vertex local coords |
41 if (viewMatrix.hasPerspective()) { | 43 if (viewMatrix.hasPerspective()) { |
42 LocalCoords localCoords(hasExplicitLocalCoords ? LocalCoords::kHasExplic
it_Type : | 44 LocalCoords localCoords(hasExplicitLocalCoords ? LocalCoords::kHasExplic
it_Type : |
43 LocalCoords::kUsePositi
on_Type, | 45 LocalCoords::kUsePositi
on_Type, |
44 localMatrix); | 46 localMatrix); |
45 return GrDefaultGeoProcFactory::Make(color, coverage, localCoords, viewM
atrix); | 47 return GrDefaultGeoProcFactory::Make(color, coverage, localCoords, viewM
atrix); |
46 } else if (hasExplicitLocalCoords) { | 48 } else if (hasExplicitLocalCoords) { |
47 LocalCoords localCoords(LocalCoords::kHasExplicit_Type); | 49 LocalCoords localCoords(LocalCoords::kHasExplicit_Type, localMatrix); |
48 return GrDefaultGeoProcFactory::Make(color, coverage, localCoords, SkMat
rix::I()); | 50 return GrDefaultGeoProcFactory::Make(color, coverage, localCoords, SkMat
rix::I()); |
49 } else { | 51 } else { |
50 LocalCoords localCoords(LocalCoords::kUsePosition_Type, localMatrix); | 52 LocalCoords localCoords(LocalCoords::kUsePosition_Type, localMatrix); |
51 return GrDefaultGeoProcFactory::MakeForDeviceSpace(color, coverage, loca
lCoords, | 53 return GrDefaultGeoProcFactory::MakeForDeviceSpace(color, coverage, loca
lCoords, |
52 viewMatrix); | 54 viewMatrix); |
53 } | 55 } |
54 } | 56 } |
55 | 57 |
| 58 static sk_sp<GrGeometryProcessor> make_gp(bool readsCoverage) { |
| 59 using namespace GrDefaultGeoProcFactory; |
| 60 Color color(Color::kAttribute_Type); |
| 61 Coverage coverage(readsCoverage ? Coverage::kSolid_Type : Coverage::kNone_Ty
pe); |
| 62 |
| 63 LocalCoords localCoords(LocalCoords::kHasExplicit_Type); |
| 64 return GrDefaultGeoProcFactory::Make(color, coverage, localCoords, SkMatrix:
:I()); |
| 65 } |
| 66 |
56 static void tesselate(intptr_t vertices, | 67 static void tesselate(intptr_t vertices, |
57 size_t vertexStride, | 68 size_t vertexStride, |
58 GrColor color, | 69 GrColor color, |
59 const SkMatrix& viewMatrix, | 70 const SkMatrix* viewMatrix, |
60 const SkRect& rect, | 71 const SkRect& rect, |
61 const GrQuad* localQuad) { | 72 const GrQuad* localQuad) { |
62 SkPoint* positions = reinterpret_cast<SkPoint*>(vertices); | 73 SkPoint* positions = reinterpret_cast<SkPoint*>(vertices); |
63 | 74 |
64 positions->setRectFan(rect.fLeft, rect.fTop, | 75 positions->setRectFan(rect.fLeft, rect.fTop, |
65 rect.fRight, rect.fBottom, vertexStride); | 76 rect.fRight, rect.fBottom, vertexStride); |
66 | 77 |
67 if (!viewMatrix.hasPerspective()) { | 78 if (viewMatrix) { |
68 viewMatrix.mapPointsWithStride(positions, vertexStride, kVertsPerInstanc
e); | 79 viewMatrix->mapPointsWithStride(positions, vertexStride, kVertsPerInstan
ce); |
69 } | 80 } |
70 | 81 |
71 // Setup local coords | 82 // Setup local coords |
72 // TODO we should only do this if local coords are being read | 83 // TODO we should only do this if local coords are being read |
73 if (localQuad) { | 84 if (localQuad) { |
74 static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor); | 85 static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor); |
75 for (int i = 0; i < kVertsPerInstance; i++) { | 86 for (int i = 0; i < kVertsPerInstance; i++) { |
76 SkPoint* coords = reinterpret_cast<SkPoint*>(vertices + kLocalOffset
+ | 87 SkPoint* coords = reinterpret_cast<SkPoint*>(vertices + kLocalOffset
+ |
77 i * vertexStride); | 88 i * vertexStride); |
78 *coords = localQuad->point(i); | 89 *coords = localQuad->point(i); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 | 148 |
138 void initBatchTracker(const GrXPOverridesForBatch& overrides) override { | 149 void initBatchTracker(const GrXPOverridesForBatch& overrides) override { |
139 overrides.getOverrideColorIfSet(&fRects[0].fColor); | 150 overrides.getOverrideColorIfSet(&fRects[0].fColor); |
140 fOverrides = overrides; | 151 fOverrides = overrides; |
141 } | 152 } |
142 | 153 |
143 private: | 154 private: |
144 NonAAFillRectBatch() : INHERITED(ClassID()) {} | 155 NonAAFillRectBatch() : INHERITED(ClassID()) {} |
145 | 156 |
146 void onPrepareDraws(Target* target) const override { | 157 void onPrepareDraws(Target* target) const override { |
147 sk_sp<GrGeometryProcessor> gp = make_gp(fRects[0].fViewMatrix, fOverride
s.readsCoverage(), | 158 sk_sp<GrGeometryProcessor> gp = make_gp(fOverrides.readsCoverage()); |
148 true, nullptr); | |
149 if (!gp) { | 159 if (!gp) { |
150 SkDebugf("Couldn't create GrGeometryProcessor\n"); | 160 SkDebugf("Couldn't create GrGeometryProcessor\n"); |
151 return; | 161 return; |
152 } | 162 } |
153 SkASSERT(gp->getVertexStride() == | 163 SkASSERT(gp->getVertexStride() == |
154 sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr)); | 164 sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr)); |
155 | 165 |
156 size_t vertexStride = gp->getVertexStride(); | 166 size_t vertexStride = gp->getVertexStride(); |
157 int instanceCount = fRects.count(); | 167 int instanceCount = fRects.count(); |
158 | 168 |
159 SkAutoTUnref<const GrBuffer> indexBuffer(target->resourceProvider()->ref
QuadIndexBuffer()); | 169 SkAutoTUnref<const GrBuffer> indexBuffer(target->resourceProvider()->ref
QuadIndexBuffer()); |
160 InstancedHelper helper; | 170 InstancedHelper helper; |
161 void* vertices = helper.init(target, kTriangles_GrPrimitiveType, vertexS
tride, | 171 void* vertices = helper.init(target, kTriangles_GrPrimitiveType, vertexS
tride, |
162 indexBuffer, kVertsPerInstance, | 172 indexBuffer, kVertsPerInstance, |
163 kIndicesPerInstance, instanceCount); | 173 kIndicesPerInstance, instanceCount); |
164 if (!vertices || !indexBuffer) { | 174 if (!vertices || !indexBuffer) { |
165 SkDebugf("Could not allocate vertices\n"); | 175 SkDebugf("Could not allocate vertices\n"); |
166 return; | 176 return; |
167 } | 177 } |
168 | 178 |
169 for (int i = 0; i < instanceCount; i++) { | 179 for (int i = 0; i < instanceCount; i++) { |
170 intptr_t verts = reinterpret_cast<intptr_t>(vertices) + | 180 intptr_t verts = reinterpret_cast<intptr_t>(vertices) + |
171 i * kVertsPerInstance * vertexStride; | 181 i * kVertsPerInstance * vertexStride; |
172 tesselate(verts, vertexStride, fRects[i].fColor, fRects[i].fViewMatr
ix, | 182 tesselate(verts, vertexStride, fRects[i].fColor, &fRects[i].fViewMat
rix, |
173 fRects[i].fRect, &fRects[i].fLocalQuad); | 183 fRects[i].fRect, &fRects[i].fLocalQuad); |
174 } | 184 } |
175 helper.recordDraw(target, gp.get()); | 185 helper.recordDraw(target, gp.get()); |
176 } | 186 } |
177 | 187 |
178 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { | 188 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { |
179 NonAAFillRectBatch* that = t->cast<NonAAFillRectBatch>(); | 189 NonAAFillRectBatch* that = t->cast<NonAAFillRectBatch>(); |
180 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pi
peline(), | 190 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pi
peline(), |
181 that->bounds(), caps)) { | 191 that->bounds(), caps)) { |
182 return false; | 192 return false; |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 | 266 |
257 void initBatchTracker(const GrXPOverridesForBatch& overrides) override { | 267 void initBatchTracker(const GrXPOverridesForBatch& overrides) override { |
258 overrides.getOverrideColorIfSet(&fRects[0].fColor); | 268 overrides.getOverrideColorIfSet(&fRects[0].fColor); |
259 fOverrides = overrides; | 269 fOverrides = overrides; |
260 } | 270 } |
261 | 271 |
262 private: | 272 private: |
263 NonAAFillRectPerspectiveBatch() : INHERITED(ClassID()) {} | 273 NonAAFillRectPerspectiveBatch() : INHERITED(ClassID()) {} |
264 | 274 |
265 void onPrepareDraws(Target* target) const override { | 275 void onPrepareDraws(Target* target) const override { |
266 sk_sp<GrGeometryProcessor> gp = make_gp(fViewMatrix, fOverrides.readsCov
erage(), | 276 sk_sp<GrGeometryProcessor> gp = make_persp_gp(fViewMatrix, |
267 fHasLocalRect, | 277 fOverrides.readsCoverage()
, |
268 fHasLocalMatrix ? &fLocalMatrix
: nullptr); | 278 fHasLocalRect, |
| 279 fHasLocalMatrix ? &fLocalM
atrix : nullptr); |
269 if (!gp) { | 280 if (!gp) { |
270 SkDebugf("Couldn't create GrGeometryProcessor\n"); | 281 SkDebugf("Couldn't create GrGeometryProcessor\n"); |
271 return; | 282 return; |
272 } | 283 } |
273 SkASSERT(fHasLocalRect | 284 SkASSERT(fHasLocalRect |
274 ? gp->getVertexStride() == | 285 ? gp->getVertexStride() == |
275 sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoord
Attr) | 286 sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoord
Attr) |
276 : gp->getVertexStride() == sizeof(GrDefaultGeoProcFactory::
PositionColorAttr)); | 287 : gp->getVertexStride() == sizeof(GrDefaultGeoProcFactory::
PositionColorAttr)); |
277 | 288 |
278 size_t vertexStride = gp->getVertexStride(); | 289 size_t vertexStride = gp->getVertexStride(); |
279 int instanceCount = fRects.count(); | 290 int instanceCount = fRects.count(); |
280 | 291 |
281 SkAutoTUnref<const GrBuffer> indexBuffer(target->resourceProvider()->ref
QuadIndexBuffer()); | 292 SkAutoTUnref<const GrBuffer> indexBuffer(target->resourceProvider()->ref
QuadIndexBuffer()); |
282 InstancedHelper helper; | 293 InstancedHelper helper; |
283 void* vertices = helper.init(target, kTriangles_GrPrimitiveType, vertexS
tride, | 294 void* vertices = helper.init(target, kTriangles_GrPrimitiveType, vertexS
tride, |
284 indexBuffer, kVertsPerInstance, | 295 indexBuffer, kVertsPerInstance, |
285 kIndicesPerInstance, instanceCount); | 296 kIndicesPerInstance, instanceCount); |
286 if (!vertices || !indexBuffer) { | 297 if (!vertices || !indexBuffer) { |
287 SkDebugf("Could not allocate vertices\n"); | 298 SkDebugf("Could not allocate vertices\n"); |
288 return; | 299 return; |
289 } | 300 } |
290 | 301 |
291 for (int i = 0; i < instanceCount; i++) { | 302 for (int i = 0; i < instanceCount; i++) { |
292 const RectInfo& info = fRects[i]; | 303 const RectInfo& info = fRects[i]; |
293 intptr_t verts = reinterpret_cast<intptr_t>(vertices) + | 304 intptr_t verts = reinterpret_cast<intptr_t>(vertices) + |
294 i * kVertsPerInstance * vertexStride; | 305 i * kVertsPerInstance * vertexStride; |
295 if (fHasLocalRect) { | 306 if (fHasLocalRect) { |
296 GrQuad quad(info.fLocalRect); | 307 GrQuad quad(info.fLocalRect); |
297 tesselate(verts, vertexStride, info.fColor, fViewMatrix, info.fR
ect, &quad); | 308 tesselate(verts, vertexStride, info.fColor, nullptr, info.fRect,
&quad); |
298 } else { | 309 } else { |
299 tesselate(verts, vertexStride, info.fColor, fViewMatrix, info.fR
ect, nullptr); | 310 tesselate(verts, vertexStride, info.fColor, nullptr, info.fRect,
nullptr); |
300 } | 311 } |
301 } | 312 } |
302 helper.recordDraw(target, gp.get()); | 313 helper.recordDraw(target, gp.get()); |
303 } | 314 } |
304 | 315 |
305 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { | 316 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { |
306 NonAAFillRectPerspectiveBatch* that = t->cast<NonAAFillRectPerspectiveBa
tch>(); | 317 NonAAFillRectPerspectiveBatch* that = t->cast<NonAAFillRectPerspectiveBa
tch>(); |
307 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pi
peline(), | 318 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pi
peline(), |
308 that->bounds(), caps)) { | 319 that->bounds(), caps)) { |
309 return false; | 320 return false; |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 SkMatrix localMatrix = GrTest::TestMatrix(random); | 392 SkMatrix localMatrix = GrTest::TestMatrix(random); |
382 | 393 |
383 bool hasLocalRect = random->nextBool(); | 394 bool hasLocalRect = random->nextBool(); |
384 bool hasLocalMatrix = random->nextBool(); | 395 bool hasLocalMatrix = random->nextBool(); |
385 return GrNonAAFillRectBatch::Create(color, viewMatrix, rect, | 396 return GrNonAAFillRectBatch::Create(color, viewMatrix, rect, |
386 hasLocalRect ? &localRect : nullptr, | 397 hasLocalRect ? &localRect : nullptr, |
387 hasLocalMatrix ? &localMatrix : nullptr)
; | 398 hasLocalMatrix ? &localMatrix : nullptr)
; |
388 } | 399 } |
389 | 400 |
390 #endif | 401 #endif |
OLD | NEW |