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 "GrDrawAtlasBatch.h" | |
9 #include "GrBatchTest.h" | |
10 #include "SkRandom.h" | |
11 | |
12 void GrDrawAtlasBatch::initBatchTracker(const GrPipelineInfo& init) { | |
13 // Handle any color overrides | |
14 if (!init.readsColor()) { | |
15 fGeoData[0].fColor = GrColor_ILLEGAL; | |
16 } | |
17 init.getOverrideColorIfSet(&fGeoData[0].fColor); | |
18 | |
19 // setup batch properties | |
20 fColorIgnored = !init.readsColor(); | |
21 fColor = fGeoData[0].fColor; | |
22 SkASSERT(init.readsLocalCoords()); | |
23 fCoverageIgnored = !init.readsCoverage(); | |
24 } | |
25 | |
26 static const GrGeometryProcessor* set_vertex_attributes(bool hasLocalCoords, | |
27 bool hasColors, | |
28 int* colorOffset, | |
29 int* texOffset, | |
30 GrColor color, | |
31 const SkMatrix& viewMatr
ix, | |
32 bool coverageIgnored) { | |
33 using namespace GrDefaultGeoProcFactory; | |
34 *texOffset = -1; | |
35 *colorOffset = -1; | |
36 Color gpColor(color); | |
37 if (hasColors) { | |
38 gpColor.fType = Color::kAttribute_Type; | |
39 } | |
40 | |
41 Coverage coverage(coverageIgnored ? Coverage::kNone_Type : Coverage::kSolid_
Type); | |
42 LocalCoords localCoords(hasLocalCoords ? LocalCoords::kHasExplicit_Type : | |
43 LocalCoords::kUsePosition_Type); | |
44 if (hasLocalCoords && hasColors) { | |
45 *colorOffset = sizeof(SkPoint); | |
46 *texOffset = sizeof(SkPoint) + sizeof(GrColor); | |
47 } else if (hasLocalCoords) { | |
48 *texOffset = sizeof(SkPoint); | |
49 } else if (hasColors) { | |
50 *colorOffset = sizeof(SkPoint); | |
51 } | |
52 return GrDefaultGeoProcFactory::Create(gpColor, coverage, localCoords, viewM
atrix); | |
53 } | |
54 | |
55 void GrDrawAtlasBatch::generateGeometry(GrBatchTarget* batchTarget) { | |
56 int colorOffset = -1, texOffset = -1; | |
57 // Setup geometry processor | |
58 SkAutoTUnref<const GrGeometryProcessor> gp( | |
59 set_vertex_attributes(true, this->hasColors(), &
colorOffset, | |
60 &texOffset, this->color(),
this->viewMatrix(), | |
61 this->coverageIgnored())); | |
62 | |
63 batchTarget->initDraw(gp, this->pipeline()); | |
64 | |
65 int instanceCount = fGeoData.count(); | |
66 size_t vertexStride = gp->getVertexStride(); | |
67 SkASSERT(vertexStride == sizeof(SkPoint) + sizeof(SkPoint) | |
68 + (this->hasColors() ? sizeof(GrColor) : 0)); | |
69 | |
70 QuadHelper helper; | |
71 int numQuads = this->vertexCount()/4; | |
72 void* verts = helper.init(batchTarget, vertexStride, numQuads); | |
73 if (!verts) { | |
74 SkDebugf("Could not allocate vertices\n"); | |
75 return; | |
76 } | |
77 | |
78 int vertexOffset = 0; | |
79 for (int i = 0; i < instanceCount; i++) { | |
80 const Geometry& args = fGeoData[i]; | |
81 | |
82 for (int j = 0; j < args.fPositions.count(); ++j) { | |
83 *((SkPoint*)verts) = args.fPositions[j]; | |
84 if (this->hasColors()) { | |
85 *(GrColor*)((intptr_t)verts + colorOffset) = args.fColors[j]; | |
86 } | |
87 *(SkPoint*)((intptr_t)verts + texOffset) = args.fLocalCoords[j]; | |
88 verts = (void*)((intptr_t)verts + vertexStride); | |
89 vertexOffset++; | |
90 } | |
91 } | |
92 helper.issueDraw(batchTarget); | |
93 } | |
94 | |
95 GrDrawAtlasBatch::GrDrawAtlasBatch(const Geometry& geometry, const SkMatrix& vie
wMatrix, | |
96 const SkPoint* positions, int vertexCount, | |
97 const GrColor* colors, const SkPoint* localCo
ords, | |
98 const SkRect& bounds) { | |
99 this->initClassID<GrDrawAtlasBatch>(); | |
100 SkASSERT(positions); | |
101 SkASSERT(localCoords); | |
102 | |
103 fViewMatrix = viewMatrix; | |
104 Geometry& installedGeo = fGeoData.push_back(geometry); | |
105 | |
106 installedGeo.fPositions.append(vertexCount, positions); | |
107 | |
108 if (colors) { | |
109 installedGeo.fColors.append(vertexCount, colors); | |
110 fHasColors = true; | |
111 } else { | |
112 fHasColors = false; | |
113 } | |
114 | |
115 installedGeo.fLocalCoords.append(vertexCount, localCoords); | |
116 fVertexCount = vertexCount; | |
117 | |
118 this->setBounds(bounds); | |
119 } | |
120 | |
121 bool GrDrawAtlasBatch::onCombineIfPossible(GrBatch* t) { | |
122 if (!this->pipeline()->isEqual(*t->pipeline())) { | |
123 return false; | |
124 } | |
125 | |
126 GrDrawAtlasBatch* that = t->cast<GrDrawAtlasBatch>(); | |
127 | |
128 // We currently use a uniform viewmatrix for this batch | |
129 if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) { | |
130 return false; | |
131 } | |
132 | |
133 if (this->hasColors() != that->hasColors()) { | |
134 return false; | |
135 } | |
136 | |
137 if (!this->hasColors() && this->color() != that->color()) { | |
138 return false; | |
139 } | |
140 | |
141 if (this->color() != that->color()) { | |
142 fColor = GrColor_ILLEGAL; | |
143 } | |
144 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()); | |
145 fVertexCount += that->vertexCount(); | |
146 | |
147 this->joinBounds(that->bounds()); | |
148 return true; | |
149 } | |
150 | |
151 #ifdef GR_TEST_UTILS | |
152 | |
153 static SkPoint random_point(SkRandom* random, SkScalar min, SkScalar max) { | |
154 SkPoint p; | |
155 p.fX = random->nextRangeScalar(min, max); | |
156 p.fY = random->nextRangeScalar(min, max); | |
157 return p; | |
158 } | |
159 | |
160 static void randomize_params(size_t count, SkScalar min, SkScalar max, | |
161 SkRandom* random, | |
162 SkTArray<SkPoint>* positions, | |
163 SkTArray<SkPoint>* texCoords, | |
164 SkTArray<GrColor>* colors, bool hasColors) { | |
165 for (uint32_t v = 0; v < count; v++) { | |
166 positions->push_back(random_point(random, min, max)); | |
167 texCoords->push_back(random_point(random, min, max)); | |
168 if (hasColors) { | |
169 colors->push_back(GrRandomColor(random)); | |
170 } | |
171 } | |
172 } | |
173 | |
174 | |
175 BATCH_TEST_DEFINE(GrDrawAtlasBatch) { | |
176 uint32_t spriteCount = random->nextRangeU(1, 100); | |
177 | |
178 // TODO make 'sensible' indexbuffers | |
179 SkTArray<SkPoint> positions; | |
180 SkTArray<SkPoint> texCoords; | |
181 SkTArray<GrColor> colors; | |
182 | |
183 bool hasColors = random->nextBool(); | |
184 | |
185 uint32_t vertexCount = 4*spriteCount; | |
186 | |
187 static const SkScalar kMinVertExtent = -100.f; | |
188 static const SkScalar kMaxVertExtent = 100.f; | |
189 randomize_params(vertexCount, kMinVertExtent, kMaxVertExtent, | |
190 random, | |
191 &positions, | |
192 &texCoords, | |
193 &colors, hasColors); | |
194 | |
195 SkMatrix viewMatrix = GrTest::TestMatrix(random); | |
196 SkRect bounds; | |
197 SkDEBUGCODE(bool result = ) bounds.setBoundsCheck(positions.begin(), vertexC
ount); | |
198 SkASSERT(result); | |
199 | |
200 viewMatrix.mapRect(&bounds); | |
201 | |
202 GrDrawAtlasBatch::Geometry geometry; | |
203 geometry.fColor = GrRandomColor(random); | |
204 return GrDrawAtlasBatch::Create(geometry, viewMatrix, | |
205 positions.begin(), vertexCount, | |
206 colors.begin(), | |
207 texCoords.begin(), | |
208 bounds); | |
209 } | |
210 | |
211 #endif | |
OLD | NEW |