OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2015 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 "GrRectBatch.h" | |
9 | |
10 #include "GrBatchTarget.h" | |
11 #include "GrDefaultGeoProcFactory.h" | |
12 #include "GrPrimitiveProcessor.h" | |
13 | |
14 void GrRectBatch::initBatchTracker(const GrPipelineOptimizations& init) { | |
15 // Handle any color overrides | |
16 if (!init.readsColor()) { | |
17 fGeoData[0].fColor = GrColor_ILLEGAL; | |
18 } | |
19 init.getOverrideColorIfSet(&fGeoData[0].fColor); | |
20 | |
21 // setup batch properties | |
22 fBatch.fColorIgnored = !init.readsColor(); | |
23 fBatch.fColor = fGeoData[0].fColor; | |
24 fBatch.fUsesLocalCoords = init.readsLocalCoords(); | |
25 fBatch.fCoverageIgnored = !init.readsCoverage(); | |
26 } | |
27 | |
28 void GrRectBatch::generateGeometry(GrBatchTarget* batchTarget) { | |
29 SkAutoTUnref<const GrGeometryProcessor> gp(this->createRectGP()); | |
30 if (!gp) { | |
31 SkDebugf("Could not create GrGeometryProcessor\n"); | |
32 return; | |
33 } | |
34 | |
35 batchTarget->initDraw(gp, this->pipeline()); | |
36 | |
37 int instanceCount = fGeoData.count(); | |
38 size_t vertexStride = gp->getVertexStride(); | |
39 SkASSERT(this->hasLocalRect() ? | |
40 vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorLocalC
oordAttr) : | |
41 vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorAttr))
; | |
42 QuadHelper helper; | |
43 void* vertices = helper.init(batchTarget, vertexStride, instanceCount); | |
44 | |
45 if (!vertices) { | |
46 return; | |
47 } | |
48 | |
49 for (int i = 0; i < instanceCount; i++) { | |
50 const Geometry& geom = fGeoData[i]; | |
51 | |
52 intptr_t offset = reinterpret_cast<intptr_t>(vertices) + | |
53 kVerticesPerQuad * i * vertexStride; | |
54 SkPoint* positions = reinterpret_cast<SkPoint*>(offset); | |
55 | |
56 positions->setRectFan(geom.fRect.fLeft, geom.fRect.fTop, | |
57 geom.fRect.fRight, geom.fRect.fBottom, vertexStrid
e); | |
58 geom.fViewMatrix.mapPointsWithStride(positions, vertexStride, kVerticesP
erQuad); | |
59 | |
60 // TODO we should only do this if local coords are being read | |
61 if (geom.fHasLocalRect) { | |
62 static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor); | |
63 SkPoint* coords = reinterpret_cast<SkPoint*>(offset + kLocalOffset); | |
64 coords->setRectFan(geom.fLocalRect.fLeft, geom.fLocalRect.fTop, | |
65 geom.fLocalRect.fRight, geom.fLocalRect.fBottom, | |
66 vertexStride); | |
67 if (geom.fHasLocalMatrix) { | |
68 geom.fLocalMatrix.mapPointsWithStride(coords, vertexStride, kVer
ticesPerQuad); | |
69 } | |
70 } | |
71 | |
72 static const int kColorOffset = sizeof(SkPoint); | |
73 GrColor* vertColor = reinterpret_cast<GrColor*>(offset + kColorOffset); | |
74 for (int j = 0; j < 4; ++j) { | |
75 *vertColor = geom.fColor; | |
76 vertColor = (GrColor*) ((intptr_t) vertColor + vertexStride); | |
77 } | |
78 } | |
79 | |
80 helper.issueDraw(batchTarget); | |
81 } | |
82 | |
83 bool GrRectBatch::onCombineIfPossible(GrBatch* t, const GrCaps& caps) { | |
84 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *t->pipeline(
), t->bounds(), | |
85 caps)) { | |
86 return false; | |
87 } | |
88 | |
89 GrRectBatch* that = t->cast<GrRectBatch>(); | |
90 | |
91 if (this->hasLocalRect() != that->hasLocalRect()) { | |
92 return false; | |
93 } | |
94 | |
95 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords()); | |
96 if (!this->hasLocalRect() && this->usesLocalCoords()) { | |
97 if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) { | |
98 return false; | |
99 } | |
100 | |
101 if (this->hasLocalMatrix() != that->hasLocalMatrix()) { | |
102 return false; | |
103 } | |
104 | |
105 if (this->hasLocalMatrix() && !this->localMatrix().cheapEqualTo(that->lo
calMatrix())) { | |
106 return false; | |
107 } | |
108 } | |
109 | |
110 if (this->color() != that->color()) { | |
111 fBatch.fColor = GrColor_ILLEGAL; | |
112 } | |
113 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()); | |
114 this->joinBounds(that->bounds()); | |
115 return true; | |
116 } | |
117 | |
118 /** We always use per-vertex colors so that rects can be batched across color ch
anges. Sometimes | |
119 we have explicit local coords and sometimes not. We *could* always provide
explicit local | |
120 coords and just duplicate the positions when the caller hasn't provided a lo
cal coord rect, | |
121 but we haven't seen a use case which frequently switches between local rect
and no local | |
122 rect draws. | |
123 | |
124 The color param is used to determine whether the opaque hint can be set on t
he draw state. | |
125 The caller must populate the vertex colors itself. | |
126 | |
127 The vertex attrib order is always pos, color, [local coords]. | |
128 */ | |
129 const GrGeometryProcessor* GrRectBatch::createRectGP() { | |
130 using namespace GrDefaultGeoProcFactory; | |
131 Color color(Color::kAttribute_Type); | |
132 Coverage coverage(this->coverageIgnored() ? Coverage::kNone_Type : Coverage:
:kSolid_Type); | |
133 | |
134 // if we have a local rect, then we apply the localMatrix directly to the lo
calRect to | |
135 // generate vertex local coords | |
136 if (this->hasLocalRect()) { | |
137 LocalCoords localCoords(LocalCoords::kHasExplicit_Type); | |
138 return GrDefaultGeoProcFactory::Create(color, coverage, localCoords, SkM
atrix::I()); | |
139 } else { | |
140 LocalCoords localCoords(LocalCoords::kUsePosition_Type, | |
141 this->hasLocalMatrix() ? &this->localMatrix() :
NULL); | |
142 return GrDefaultGeoProcFactory::CreateForDeviceSpace(color, coverage, lo
calCoords, | |
143 this->viewMatrix())
; | |
144 } | |
145 } | |
146 | |
147 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | |
148 | |
149 #ifdef GR_TEST_UTILS | |
150 | |
151 #include "GrBatchTest.h" | |
152 | |
153 BATCH_TEST_DEFINE(RectBatch) { | |
154 GrRectBatch::Geometry geometry; | |
155 geometry.fColor = GrRandomColor(random); | |
156 | |
157 geometry.fRect = GrTest::TestRect(random); | |
158 geometry.fHasLocalRect = random->nextBool(); | |
159 | |
160 if (geometry.fHasLocalRect) { | |
161 geometry.fViewMatrix = GrTest::TestMatrixInvertible(random); | |
162 geometry.fLocalRect = GrTest::TestRect(random); | |
163 } else { | |
164 geometry.fViewMatrix = GrTest::TestMatrix(random); | |
165 } | |
166 | |
167 geometry.fHasLocalMatrix = random->nextBool(); | |
168 if (geometry.fHasLocalMatrix) { | |
169 geometry.fLocalMatrix = GrTest::TestMatrix(random); | |
170 } | |
171 | |
172 return GrRectBatch::Create(geometry); | |
173 } | |
174 | |
175 #endif | |
OLD | NEW |