OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 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 "GrInOrderDrawBuffer.h" | 8 #include "GrInOrderDrawBuffer.h" |
9 | 9 |
10 #include "GrDefaultGeoProcFactory.h" | |
11 #include "GrTemplates.h" | |
12 | |
13 GrInOrderDrawBuffer::GrInOrderDrawBuffer(GrContext* context, | 10 GrInOrderDrawBuffer::GrInOrderDrawBuffer(GrContext* context, |
14 GrVertexBufferAllocPool* vertexPool, | 11 GrVertexBufferAllocPool* vertexPool, |
15 GrIndexBufferAllocPool* indexPool) | 12 GrIndexBufferAllocPool* indexPool) |
16 : INHERITED(context, vertexPool, indexPool) | 13 : INHERITED(context, vertexPool, indexPool) |
17 , fCommands(SkNEW_ARGS(GrCommandBuilder, (context->getGpu(), vertexPool, ind
exPool))) | 14 , fCommands(SkNEW_ARGS(GrCommandBuilder, (context->getGpu(), vertexPool, ind
exPool))) |
18 , fPathIndexBuffer(kPathIdxBufferMinReserve * sizeof(char)/4) | 15 , fPathIndexBuffer(kPathIdxBufferMinReserve * sizeof(char)/4) |
19 , fPathTransformBuffer(kPathXformBufferMinReserve * sizeof(float)/4) | 16 , fPathTransformBuffer(kPathXformBufferMinReserve * sizeof(float)/4) |
20 , fPipelineBuffer(kPipelineBufferMinReserve) | 17 , fPipelineBuffer(kPipelineBufferMinReserve) |
21 , fDrawID(0) { | 18 , fDrawID(0) { |
22 | 19 |
23 SkASSERT(vertexPool); | 20 SkASSERT(vertexPool); |
24 SkASSERT(indexPool); | 21 SkASSERT(indexPool); |
25 } | 22 } |
26 | 23 |
27 GrInOrderDrawBuffer::~GrInOrderDrawBuffer() { | 24 GrInOrderDrawBuffer::~GrInOrderDrawBuffer() { |
28 this->reset(); | 25 this->reset(); |
29 } | 26 } |
30 | 27 |
31 //////////////////////////////////////////////////////////////////////////////// | |
32 | |
33 /** We always use per-vertex colors so that rects can be batched across color ch
anges. Sometimes we | |
34 have explicit local coords and sometimes not. We *could* always provide expl
icit local coords | |
35 and just duplicate the positions when the caller hasn't provided a local coo
rd rect, but we | |
36 haven't seen a use case which frequently switches between local rect and no
local rect draws. | |
37 | |
38 The color param is used to determine whether the opaque hint can be set on t
he draw state. | |
39 The caller must populate the vertex colors itself. | |
40 | |
41 The vertex attrib order is always pos, color, [local coords]. | |
42 */ | |
43 static const GrGeometryProcessor* create_rect_gp(bool hasExplicitLocalCoords, | |
44 GrColor color, | |
45 const SkMatrix* localMatrix) { | |
46 uint32_t flags = GrDefaultGeoProcFactory::kPosition_GPType | | |
47 GrDefaultGeoProcFactory::kColor_GPType; | |
48 flags |= hasExplicitLocalCoords ? GrDefaultGeoProcFactory::kLocalCoord_GPTyp
e : 0; | |
49 if (localMatrix) { | |
50 return GrDefaultGeoProcFactory::Create(flags, color, SkMatrix::I(), *loc
alMatrix, | |
51 GrColorIsOpaque(color)); | |
52 } else { | |
53 return GrDefaultGeoProcFactory::Create(flags, color, SkMatrix::I(), SkMa
trix::I(), | |
54 GrColorIsOpaque(color)); | |
55 } | |
56 } | |
57 | |
58 class RectBatch : public GrBatch { | |
59 public: | |
60 struct Geometry { | |
61 GrColor fColor; | |
62 SkMatrix fViewMatrix; | |
63 SkRect fRect; | |
64 bool fHasLocalRect; | |
65 bool fHasLocalMatrix; | |
66 SkRect fLocalRect; | |
67 SkMatrix fLocalMatrix; | |
68 }; | |
69 | |
70 static GrBatch* Create(const Geometry& geometry) { | |
71 return SkNEW_ARGS(RectBatch, (geometry)); | |
72 } | |
73 | |
74 const char* name() const override { return "RectBatch"; } | |
75 | |
76 void getInvariantOutputColor(GrInitInvariantOutput* out) const override { | |
77 // When this is called on a batch, there is only one geometry bundle | |
78 out->setKnownFourComponents(fGeoData[0].fColor); | |
79 } | |
80 | |
81 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override { | |
82 out->setKnownSingleComponent(0xff); | |
83 } | |
84 | |
85 void initBatchTracker(const GrPipelineInfo& init) override { | |
86 // Handle any color overrides | |
87 if (init.fColorIgnored) { | |
88 fGeoData[0].fColor = GrColor_ILLEGAL; | |
89 } else if (GrColor_ILLEGAL != init.fOverrideColor) { | |
90 fGeoData[0].fColor = init.fOverrideColor; | |
91 } | |
92 | |
93 // setup batch properties | |
94 fBatch.fColorIgnored = init.fColorIgnored; | |
95 fBatch.fColor = fGeoData[0].fColor; | |
96 fBatch.fUsesLocalCoords = init.fUsesLocalCoords; | |
97 fBatch.fCoverageIgnored = init.fCoverageIgnored; | |
98 } | |
99 | |
100 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline
) override { | |
101 // Go to device coords to allow batching across matrix changes | |
102 SkMatrix invert = SkMatrix::I(); | |
103 | |
104 // if we have a local rect, then we apply the localMatrix directly to th
e localRect to | |
105 // generate vertex local coords | |
106 bool hasExplicitLocalCoords = this->hasLocalRect(); | |
107 if (!hasExplicitLocalCoords) { | |
108 if (!this->viewMatrix().isIdentity() && !this->viewMatrix().invert(&
invert)) { | |
109 SkDebugf("Could not invert\n"); | |
110 return; | |
111 } | |
112 | |
113 if (this->hasLocalMatrix()) { | |
114 invert.preConcat(this->localMatrix()); | |
115 } | |
116 } | |
117 | |
118 SkAutoTUnref<const GrGeometryProcessor> gp(create_rect_gp(hasExplicitLoc
alCoords, | |
119 this->color(), | |
120 &invert)); | |
121 | |
122 batchTarget->initDraw(gp, pipeline); | |
123 | |
124 // TODO this is hacky, but the only way we have to initialize the GP is
to use the | |
125 // GrPipelineInfo struct so we can generate the correct shader. Once we
have GrBatch | |
126 // everywhere we can remove this nastiness | |
127 GrPipelineInfo init; | |
128 init.fColorIgnored = fBatch.fColorIgnored; | |
129 init.fOverrideColor = GrColor_ILLEGAL; | |
130 init.fCoverageIgnored = fBatch.fCoverageIgnored; | |
131 init.fUsesLocalCoords = this->usesLocalCoords(); | |
132 gp->initBatchTracker(batchTarget->currentBatchTracker(), init); | |
133 | |
134 int instanceCount = fGeoData.count(); | |
135 size_t vertexStride = gp->getVertexStride(); | |
136 SkASSERT(hasExplicitLocalCoords ? | |
137 vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorLo
calCoordAttr) : | |
138 vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorAt
tr)); | |
139 QuadHelper helper; | |
140 void* vertices = helper.init(batchTarget, vertexStride, instanceCount); | |
141 | |
142 if (!vertices) { | |
143 return; | |
144 } | |
145 | |
146 | |
147 for (int i = 0; i < instanceCount; i++) { | |
148 const Geometry& geom = fGeoData[i]; | |
149 | |
150 intptr_t offset = GrTCast<intptr_t>(vertices) + kVerticesPerQuad * i
* vertexStride; | |
151 SkPoint* positions = GrTCast<SkPoint*>(offset); | |
152 | |
153 positions->setRectFan(geom.fRect.fLeft, geom.fRect.fTop, | |
154 geom.fRect.fRight, geom.fRect.fBottom, vertexS
tride); | |
155 geom.fViewMatrix.mapPointsWithStride(positions, vertexStride, kVerti
cesPerQuad); | |
156 | |
157 if (geom.fHasLocalRect) { | |
158 static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor
); | |
159 SkPoint* coords = GrTCast<SkPoint*>(offset + kLocalOffset); | |
160 coords->setRectFan(geom.fLocalRect.fLeft, geom.fLocalRect.fTop, | |
161 geom.fLocalRect.fRight, geom.fLocalRect.fBott
om, | |
162 vertexStride); | |
163 if (geom.fHasLocalMatrix) { | |
164 geom.fLocalMatrix.mapPointsWithStride(coords, vertexStride,
kVerticesPerQuad); | |
165 } | |
166 } | |
167 | |
168 static const int kColorOffset = sizeof(SkPoint); | |
169 GrColor* vertColor = GrTCast<GrColor*>(offset + kColorOffset); | |
170 for (int j = 0; j < 4; ++j) { | |
171 *vertColor = geom.fColor; | |
172 vertColor = (GrColor*) ((intptr_t) vertColor + vertexStride); | |
173 } | |
174 } | |
175 | |
176 helper.issueDraws(batchTarget); | |
177 } | |
178 | |
179 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } | |
180 | |
181 private: | |
182 RectBatch(const Geometry& geometry) { | |
183 this->initClassID<RectBatch>(); | |
184 fGeoData.push_back(geometry); | |
185 | |
186 fBounds = geometry.fRect; | |
187 geometry.fViewMatrix.mapRect(&fBounds); | |
188 } | |
189 | |
190 GrColor color() const { return fBatch.fColor; } | |
191 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } | |
192 bool colorIgnored() const { return fBatch.fColorIgnored; } | |
193 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; } | |
194 const SkMatrix& localMatrix() const { return fGeoData[0].fLocalMatrix; } | |
195 bool hasLocalRect() const { return fGeoData[0].fHasLocalRect; } | |
196 bool hasLocalMatrix() const { return fGeoData[0].fHasLocalMatrix; } | |
197 | |
198 bool onCombineIfPossible(GrBatch* t) override { | |
199 RectBatch* that = t->cast<RectBatch>(); | |
200 | |
201 if (this->hasLocalRect() != that->hasLocalRect()) { | |
202 return false; | |
203 } | |
204 | |
205 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords()); | |
206 if (!this->hasLocalRect() && this->usesLocalCoords()) { | |
207 if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) { | |
208 return false; | |
209 } | |
210 | |
211 if (this->hasLocalMatrix() != that->hasLocalMatrix()) { | |
212 return false; | |
213 } | |
214 | |
215 if (this->hasLocalMatrix() && !this->localMatrix().cheapEqualTo(that
->localMatrix())) { | |
216 return false; | |
217 } | |
218 } | |
219 | |
220 if (this->color() != that->color()) { | |
221 fBatch.fColor = GrColor_ILLEGAL; | |
222 } | |
223 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin())
; | |
224 this->joinBounds(that->bounds()); | |
225 return true; | |
226 } | |
227 | |
228 struct BatchTracker { | |
229 GrColor fColor; | |
230 bool fUsesLocalCoords; | |
231 bool fColorIgnored; | |
232 bool fCoverageIgnored; | |
233 }; | |
234 | |
235 BatchTracker fBatch; | |
236 SkSTArray<1, Geometry, true> fGeoData; | |
237 }; | |
238 | |
239 void GrInOrderDrawBuffer::onDrawRect(GrPipelineBuilder* pipelineBuilder, | |
240 GrColor color, | |
241 const SkMatrix& viewMatrix, | |
242 const SkRect& rect, | |
243 const SkRect* localRect, | |
244 const SkMatrix* localMatrix) { | |
245 RectBatch::Geometry geometry; | |
246 geometry.fColor = color; | |
247 geometry.fViewMatrix = viewMatrix; | |
248 geometry.fRect = rect; | |
249 | |
250 if (localRect) { | |
251 geometry.fHasLocalRect = true; | |
252 geometry.fLocalRect = *localRect; | |
253 } else { | |
254 geometry.fHasLocalRect = false; | |
255 } | |
256 | |
257 if (localMatrix) { | |
258 geometry.fHasLocalMatrix = true; | |
259 geometry.fLocalMatrix = *localMatrix; | |
260 } else { | |
261 geometry.fHasLocalMatrix = false; | |
262 } | |
263 | |
264 SkAutoTUnref<GrBatch> batch(RectBatch::Create(geometry)); | |
265 this->drawBatch(pipelineBuilder, batch); | |
266 } | |
267 | |
268 void GrInOrderDrawBuffer::onDrawBatch(GrBatch* batch, | 28 void GrInOrderDrawBuffer::onDrawBatch(GrBatch* batch, |
269 const PipelineInfo& pipelineInfo) { | 29 const PipelineInfo& pipelineInfo) { |
270 State* state = this->setupPipelineAndShouldDraw(batch, pipelineInfo); | 30 State* state = this->setupPipelineAndShouldDraw(batch, pipelineInfo); |
271 if (!state) { | 31 if (!state) { |
272 return; | 32 return; |
273 } | 33 } |
274 | 34 |
275 GrTargetCommands::Cmd* cmd = fCommands->recordDrawBatch(state, batch); | 35 GrTargetCommands::Cmd* cmd = fCommands->recordDrawBatch(state, batch); |
276 this->recordTraceMarkersIfNecessary(cmd); | 36 this->recordTraceMarkersIfNecessary(cmd); |
277 } | 37 } |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
432 fPrevState->getPipeline()->isEqual(*state->getPipeline())) { | 192 fPrevState->getPipeline()->isEqual(*state->getPipeline())) { |
433 this->unallocState(state); | 193 this->unallocState(state); |
434 } else { | 194 } else { |
435 fPrevState.reset(state); | 195 fPrevState.reset(state); |
436 } | 196 } |
437 | 197 |
438 this->recordTraceMarkersIfNecessary( | 198 this->recordTraceMarkersIfNecessary( |
439 fCommands->recordXferBarrierIfNecessary(*fPrevState->getPipeline(),
*this->caps())); | 199 fCommands->recordXferBarrierIfNecessary(*fPrevState->getPipeline(),
*this->caps())); |
440 return fPrevState; | 200 return fPrevState; |
441 } | 201 } |
OLD | NEW |