OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 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 "GrAARectRenderer.h" | 8 #include "GrAARectRenderer.h" |
9 #include "GrBatch.h" | |
10 #include "GrBatchBuffer.h" | |
11 #include "GrBufferAllocPool.h" | |
9 #include "GrDefaultGeoProcFactory.h" | 12 #include "GrDefaultGeoProcFactory.h" |
10 #include "GrGeometryProcessor.h" | 13 #include "GrGeometryProcessor.h" |
11 #include "GrGpu.h" | 14 #include "GrGpu.h" |
12 #include "GrInvariantOutput.h" | 15 #include "GrInvariantOutput.h" |
13 #include "SkColorPriv.h" | 16 #include "SkColorPriv.h" |
14 #include "gl/GrGLProcessor.h" | 17 #include "gl/GrGLProcessor.h" |
15 #include "gl/GrGLGeometryProcessor.h" | 18 #include "gl/GrGLGeometryProcessor.h" |
16 #include "gl/builders/GrGLProgramBuilder.h" | 19 #include "gl/builders/GrGLProgramBuilder.h" |
17 | 20 |
18 /////////////////////////////////////////////////////////////////////////////// | 21 /////////////////////////////////////////////////////////////////////////////// |
19 | 22 |
20 namespace { | |
21 // Should the coverage be multiplied into the color attrib or use a separate att rib. | |
22 enum CoverageAttribType { | |
23 kUseColor_CoverageAttribType, | |
24 kUseCoverage_CoverageAttribType, | |
25 }; | |
26 } | |
27 | |
28 static const GrGeometryProcessor* create_rect_gp(const GrDrawState& drawState, | |
29 GrColor color, | |
30 CoverageAttribType* type, | |
31 const SkMatrix& localMatrix) { | |
32 uint32_t flags = GrDefaultGeoProcFactory::kColor_GPType; | |
33 const GrGeometryProcessor* gp; | |
34 if (drawState.canTweakAlphaForCoverage()) { | |
35 gp = GrDefaultGeoProcFactory::Create(flags, color, SkMatrix::I(), localM atrix); | |
36 SkASSERT(gp->getVertexStride() == sizeof(GrDefaultGeoProcFactory::Positi onColorAttr)); | |
37 *type = kUseColor_CoverageAttribType; | |
38 } else { | |
39 flags |= GrDefaultGeoProcFactory::kCoverage_GPType; | |
40 gp = GrDefaultGeoProcFactory::Create(flags, color, SkMatrix::I(), localM atrix, | |
41 GrColorIsOpaque(color)); | |
42 SkASSERT(gp->getVertexStride()==sizeof(GrDefaultGeoProcFactory::Position ColorCoverageAttr)); | |
43 *type = kUseCoverage_CoverageAttribType; | |
44 } | |
45 return gp; | |
46 } | |
47 | |
48 static void set_inset_fan(SkPoint* pts, size_t stride, | 23 static void set_inset_fan(SkPoint* pts, size_t stride, |
49 const SkRect& r, SkScalar dx, SkScalar dy) { | 24 const SkRect& r, SkScalar dx, SkScalar dy) { |
50 pts->setRectFan(r.fLeft + dx, r.fTop + dy, | 25 pts->setRectFan(r.fLeft + dx, r.fTop + dy, |
51 r.fRight - dx, r.fBottom - dy, stride); | 26 r.fRight - dx, r.fBottom - dy, stride); |
52 } | 27 } |
53 | 28 |
54 void GrAARectRenderer::reset() { | |
55 SkSafeSetNull(fAAFillRectIndexBuffer); | |
56 SkSafeSetNull(fAAMiterStrokeRectIndexBuffer); | |
57 SkSafeSetNull(fAABevelStrokeRectIndexBuffer); | |
58 } | |
59 | |
60 static const uint16_t gFillAARectIdx[] = { | 29 static const uint16_t gFillAARectIdx[] = { |
61 0, 1, 5, 5, 4, 0, | 30 0, 1, 5, 5, 4, 0, |
62 1, 2, 6, 6, 5, 1, | 31 1, 2, 6, 6, 5, 1, |
63 2, 3, 7, 7, 6, 2, | 32 2, 3, 7, 7, 6, 2, |
64 3, 0, 4, 4, 7, 3, | 33 3, 0, 4, 4, 7, 3, |
65 4, 5, 6, 6, 7, 4, | 34 4, 5, 6, 6, 7, 4, |
66 }; | 35 }; |
67 | 36 |
68 static const int kIndicesPerAAFillRect = SK_ARRAY_COUNT(gFillAARectIdx); | 37 static const int kIndicesPerAAFillRect = SK_ARRAY_COUNT(gFillAARectIdx); |
69 static const int kVertsPerAAFillRect = 8; | 38 static const int kVertsPerAAFillRect = 8; |
70 static const int kNumAAFillRectsInIndexBuffer = 256; | 39 static const int kNumAAFillRectsInIndexBuffer = 256; |
71 | 40 |
41 static const GrGeometryProcessor* create_fill_rect_gp(bool tweakAlphaForCoverage , | |
42 const SkMatrix& localMatri x) { | |
43 uint32_t flags = GrDefaultGeoProcFactory::kColor_GPType; | |
44 const GrGeometryProcessor* gp; | |
45 if (tweakAlphaForCoverage) { | |
46 gp = GrDefaultGeoProcFactory::Create(flags, GrColor_WHITE, SkMatrix::I() , localMatrix, | |
47 false, 0xff); | |
48 } else { | |
49 flags |= GrDefaultGeoProcFactory::kCoverage_GPType; | |
50 gp = GrDefaultGeoProcFactory::Create(flags, GrColor_WHITE, SkMatrix::I() , localMatrix, | |
51 false, 0xff); | |
52 } | |
53 return gp; | |
54 } | |
55 | |
56 class AAFillRectBatch : public GrBatch { | |
57 public: | |
58 struct Geometry { | |
59 GrColor fColor; | |
60 SkMatrix fViewMatrix; | |
61 SkRect fRect; | |
62 SkRect fDevRect; | |
63 }; | |
64 | |
65 static GrBatch* Create(const Geometry& geometry, const GrIndexBuffer* indexB uffer) { | |
66 return SkNEW_ARGS(AAFillRectBatch, (geometry, indexBuffer)); | |
67 } | |
68 | |
69 const char* name() const SK_OVERRIDE { return "AAFillRectBatch"; } | |
bsalomon
2015/01/22 14:05:02
What are batch names used for? not opposed to it,
joshualitt
2015/01/22 15:47:45
Debugging, I can remove if desired
| |
70 | |
71 void getInvariantOutputColor(GrInitInvariantOutput* out, | |
72 const GrBatchOpt& batchOpt) const SK_OVERRIDE { | |
73 // When this is called on a batch, there is only one geometry bundle | |
74 if (!batchOpt.fCanTweakAlphaForCoverage && GrColorIsOpaque(fGeoData[0].f Color)) { | |
75 out->setUnknownOpaqueFourComponents(); | |
76 } else { | |
77 out->setUnknownFourComponents(); | |
78 } | |
79 } | |
80 void getInvariantOutputCoverage(GrInitInvariantOutput* out, | |
bsalomon
2015/01/22 14:05:02
need a \n here
joshualitt
2015/01/22 15:47:46
I'm confused, isn't there a newline here?
bsalomon
2015/01/22 16:00:59
before this line, after the preceeding }
| |
81 const GrBatchOpt& batchOpt) const SK_OVERRID E { | |
82 if (batchOpt.fCanTweakAlphaForCoverage) { | |
83 // uniform coverage | |
84 out->setKnownSingleComponent(0xff); | |
85 } else { | |
86 out->setUnknownSingleComponent(); | |
87 } | |
88 } | |
89 | |
90 void initBatchOpt(const GrBatchOpt& batchOpt) { | |
91 fBatchOpt = batchOpt; | |
92 } | |
93 | |
94 void initBatchTracker(const GrGeometryProcessor::InitBT& init) SK_OVERRIDE { | |
bsalomon
2015/01/22 14:05:02
Why do we need a BT? Just to fit into the GP schem
joshualitt
2015/01/22 15:47:46
There really isn't a 'batch tracker object' in bat
bsalomon
2015/01/22 16:00:59
By single init do you mean combining initBatchTrac
| |
95 // Handle any color overrides | |
96 if (init.fColorIgnored) { | |
97 fGeoData[0].fColor = GrColor_ILLEGAL; | |
98 } else if (GrColor_ILLEGAL != init.fOverrideColor) { | |
99 fGeoData[0].fColor = init.fOverrideColor; | |
100 } | |
101 | |
102 // setup batch properties | |
103 fBatch.fColorIgnored = init.fColorIgnored; | |
104 fBatch.fColor = fGeoData[0].fColor; | |
105 fBatch.fUsesLocalCoords = init.fUsesLocalCoords; | |
106 fBatch.fCoverageIgnored = init.fCoverageIgnored; | |
107 } | |
108 | |
109 bool onCombineIfPossible(GrBatch* t) SK_OVERRIDE { | |
bsalomon
2015/01/22 14:05:02
private?
| |
110 AAFillRectBatch* that = t->cast<AAFillRectBatch>(); | |
111 if (this->canTweakAlphaForCoverage() != that->canTweakAlphaForCoverage() ) { | |
112 return false; | |
113 } | |
114 | |
115 if (this->colorIgnored() != that->colorIgnored()) { | |
116 return false; | |
117 } | |
118 | |
119 if (this->usesLocalCoords() != that->usesLocalCoords()) { | |
120 return false; | |
121 } | |
122 | |
123 // We apply the viewmatrix to the rect points on the cpu. However, if t he pipeline uses | |
124 // local coords then we won't be able to batch. We could actually uploa d the viewmatrix | |
125 // using vertex attributes in these cases, but haven't investigated that | |
126 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->vi ewMatrix())) { | |
127 return false; | |
128 } | |
129 | |
130 if (this->color() != that->color()) { | |
131 fBatch.fColor = GrColor_ILLEGAL; | |
132 } | |
133 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()) ; | |
134 return true; | |
135 } | |
136 | |
137 GrColor color() const { return fBatch.fColor; } | |
bsalomon
2015/01/22 14:05:02
do all these need to be public or have getters at
joshualitt
2015/01/22 15:47:46
Well, its basically for the merge and generate geo
bsalomon
2015/01/22 16:00:59
Ok, but can we make them private?
| |
138 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } | |
139 bool canTweakAlphaForCoverage() const { return fBatchOpt.fCanTweakAlphaForCo verage; } | |
140 bool colorIgnored() const { return fBatch.fColorIgnored; } | |
141 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; } | |
142 | |
143 void generateGeometry(GrBatchBuffer* batchBuffer, const GrOptDrawState* optS tate) SK_OVERRIDE { | |
144 bool canTweakAlphaForCoverage = this->canTweakAlphaForCoverage(); | |
145 | |
146 SkMatrix localMatrix; | |
147 if (!fGeoData[0].fViewMatrix.invert(&localMatrix)) { | |
148 SkDebugf("Cannot invert\n"); | |
149 return; | |
150 } | |
151 | |
152 const GrGeometryProcessor* gp = create_fill_rect_gp(canTweakAlphaForCove rage, | |
153 localMatrix); | |
154 | |
155 batchBuffer->initDraw(gp, optState); | |
156 gp->unref(); | |
157 | |
158 // TODO this is hacky, but the only way we have to initialize the GP is to use the initBT | |
159 // struct so we can generate the correct shader. Once we have GrBatch e verywhere we can | |
160 // remove this nastiness | |
161 GrGeometryProcessor::InitBT init; | |
162 init.fColorIgnored = fBatch.fColorIgnored; | |
163 init.fOverrideColor = GrColor_ILLEGAL; | |
164 init.fCoverageIgnored = fBatch.fCoverageIgnored; | |
165 init.fUsesLocalCoords = this->usesLocalCoords(); | |
166 gp->initBatchTracker(batchBuffer->currentBatchTracker(), init); | |
167 | |
168 size_t vertexStride = gp->getVertexStride(); | |
169 | |
170 SkASSERT(canTweakAlphaForCoverage ? | |
171 vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorAt tr) : | |
172 vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorCo verageAttr)); | |
173 | |
174 int instanceCount = fGeoData.count(); | |
175 int vertexCount = kVertsPerAAFillRect * instanceCount; | |
176 | |
177 const GrVertexBuffer* vertexBuffer; | |
178 int firstVertex; | |
179 | |
180 void *vertices = batchBuffer->vertexPool()->makeSpace(vertexStride, | |
181 vertexCount, | |
182 &vertexBuffer, | |
183 &firstVertex); | |
184 | |
185 for (int i = 0; i < instanceCount; i++) { | |
186 const Geometry& args = fGeoData[i]; | |
187 this->generateGeometry(vertices, | |
188 i * kVertsPerAAFillRect * vertexStride, | |
189 vertexStride, | |
190 args.fColor, | |
191 args.fViewMatrix, | |
192 args.fRect, | |
193 args.fDevRect, | |
194 canTweakAlphaForCoverage); | |
195 } | |
196 | |
197 GrDrawTarget::DrawInfo drawInfo; | |
198 drawInfo.setPrimitiveType(kTriangles_GrPrimitiveType); | |
199 drawInfo.setStartVertex(0); | |
200 drawInfo.setStartIndex(0); | |
201 drawInfo.setVerticesPerInstance(kVertsPerAAFillRect); | |
202 drawInfo.setIndicesPerInstance(kIndicesPerAAFillRect); | |
203 drawInfo.adjustStartVertex(firstVertex); | |
204 drawInfo.setVertexBuffer(vertexBuffer); | |
205 drawInfo.setIndexBuffer(fIndexBuffer); | |
206 | |
207 int maxInstancesPerDraw = kNumAAFillRectsInIndexBuffer; | |
208 | |
209 while (instanceCount) { | |
210 drawInfo.setInstanceCount(SkTMin(instanceCount, maxInstancesPerDraw) ); | |
211 drawInfo.setVertexCount(drawInfo.instanceCount() * drawInfo.vertices PerInstance()); | |
212 drawInfo.setIndexCount(drawInfo.instanceCount() * drawInfo.indicesPe rInstance()); | |
213 | |
214 batchBuffer->draw(drawInfo); | |
215 | |
216 drawInfo.setStartVertex(drawInfo.startVertex() + drawInfo.vertexCoun t()); | |
217 instanceCount -= drawInfo.instanceCount(); | |
218 } | |
219 } | |
220 | |
221 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } | |
222 | |
223 private: | |
224 AAFillRectBatch(const Geometry& geometry, const GrIndexBuffer* indexBuffer) | |
225 : fIndexBuffer(indexBuffer) { | |
226 this->initClassID<AAFillRectBatch>(); | |
227 fGeoData.push_back(geometry); | |
228 } | |
229 | |
230 void generateGeometry(void* vertices, | |
bsalomon
2015/01/22 14:05:02
this has the same name as a virtual override... co
| |
231 uint32_t offset, | |
232 uint32_t vertexStride, | |
233 GrColor color, | |
234 const SkMatrix& viewMatrix, | |
235 const SkRect& rect, | |
236 const SkRect& devRect, | |
237 bool tweakAlphaForCoverage) const { | |
238 intptr_t verts = reinterpret_cast<intptr_t>(vertices) + offset; | |
239 | |
240 SkPoint* fan0Pos = reinterpret_cast<SkPoint*>(verts); | |
241 SkPoint* fan1Pos = reinterpret_cast<SkPoint*>(verts + 4 * vertexStride); | |
242 | |
243 SkScalar inset = SkMinScalar(devRect.width(), SK_Scalar1); | |
244 inset = SK_ScalarHalf * SkMinScalar(inset, devRect.height()); | |
245 | |
246 if (viewMatrix.rectStaysRect()) { | |
247 set_inset_fan(fan0Pos, vertexStride, devRect, -SK_ScalarHalf, -SK_Sc alarHalf); | |
248 set_inset_fan(fan1Pos, vertexStride, devRect, inset, inset); | |
249 } else { | |
250 // compute transformed (1, 0) and (0, 1) vectors | |
251 SkVector vec[2] = { | |
252 { viewMatrix[SkMatrix::kMScaleX], viewMatrix[SkMatrix::kMSkewY] }, | |
253 { viewMatrix[SkMatrix::kMSkewX], viewMatrix[SkMatrix::kMScaleY] } | |
254 }; | |
255 | |
256 vec[0].normalize(); | |
257 vec[0].scale(SK_ScalarHalf); | |
258 vec[1].normalize(); | |
259 vec[1].scale(SK_ScalarHalf); | |
260 | |
261 // create the rotated rect | |
262 fan0Pos->setRectFan(rect.fLeft, rect.fTop, | |
263 rect.fRight, rect.fBottom, vertexStride); | |
264 viewMatrix.mapPointsWithStride(fan0Pos, vertexStride, 4); | |
265 | |
266 // Now create the inset points and then outset the original | |
267 // rotated points | |
268 | |
269 // TL | |
270 *((SkPoint*)((intptr_t)fan1Pos + 0 * vertexStride)) = | |
271 *((SkPoint*)((intptr_t)fan0Pos + 0 * vertexStride)) + vec[0] + v ec[1]; | |
272 *((SkPoint*)((intptr_t)fan0Pos + 0 * vertexStride)) -= vec[0] + vec[ 1]; | |
273 // BL | |
274 *((SkPoint*)((intptr_t)fan1Pos + 1 * vertexStride)) = | |
275 *((SkPoint*)((intptr_t)fan0Pos + 1 * vertexStride)) + vec[0] - v ec[1]; | |
276 *((SkPoint*)((intptr_t)fan0Pos + 1 * vertexStride)) -= vec[0] - vec[ 1]; | |
277 // BR | |
278 *((SkPoint*)((intptr_t)fan1Pos + 2 * vertexStride)) = | |
279 *((SkPoint*)((intptr_t)fan0Pos + 2 * vertexStride)) - vec[0] - v ec[1]; | |
280 *((SkPoint*)((intptr_t)fan0Pos + 2 * vertexStride)) += vec[0] + vec[ 1]; | |
281 // TR | |
282 *((SkPoint*)((intptr_t)fan1Pos + 3 * vertexStride)) = | |
283 *((SkPoint*)((intptr_t)fan0Pos + 3 * vertexStride)) - vec[0] + v ec[1]; | |
284 *((SkPoint*)((intptr_t)fan0Pos + 3 * vertexStride)) += vec[0] - vec[ 1]; | |
285 } | |
286 | |
287 // Make verts point to vertex color and then set all the color and cover age vertex attrs values. | |
bsalomon
2015/01/22 14:05:02
wrap
| |
288 verts += sizeof(SkPoint); | |
289 for (int i = 0; i < 4; ++i) { | |
290 if (tweakAlphaForCoverage) { | |
291 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = 0; | |
292 } else { | |
293 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color; | |
294 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrCo lor)) = 0; | |
295 } | |
296 } | |
297 | |
298 int scale; | |
299 if (inset < SK_ScalarHalf) { | |
300 scale = SkScalarFloorToInt(512.0f * inset / (inset + SK_ScalarHalf)) ; | |
301 SkASSERT(scale >= 0 && scale <= 255); | |
302 } else { | |
303 scale = 0xff; | |
304 } | |
305 | |
306 verts += 4 * vertexStride; | |
307 | |
308 float innerCoverage = GrNormalizeByteToFloat(scale); | |
309 GrColor scaledColor = (0xff == scale) ? color : SkAlphaMulQ(color, scale ); | |
310 | |
311 for (int i = 0; i < 4; ++i) { | |
312 if (tweakAlphaForCoverage) { | |
313 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledCo lor; | |
314 } else { | |
315 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color; | |
316 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrCo lor)) = innerCoverage; | |
bsalomon
2015/01/22 14:05:02
wrap
| |
317 } | |
318 } | |
319 } | |
320 | |
321 struct BatchTracker { | |
322 GrColor fColor; | |
323 bool fUsesLocalCoords; | |
324 bool fColorIgnored; | |
325 bool fCoverageIgnored; | |
326 }; | |
327 | |
328 GrBatchOpt fBatchOpt; | |
329 BatchTracker fBatch; | |
330 const GrIndexBuffer* fIndexBuffer; | |
331 SkSTArray<1, Geometry, true> fGeoData; | |
332 }; | |
333 | |
334 namespace { | |
335 // Should the coverage be multiplied into the color attrib or use a separate att rib. | |
336 enum CoverageAttribType { | |
337 kUseColor_CoverageAttribType, | |
338 kUseCoverage_CoverageAttribType, | |
339 }; | |
340 } | |
341 | |
342 void GrAARectRenderer::reset() { | |
343 SkSafeSetNull(fAAFillRectIndexBuffer); | |
344 SkSafeSetNull(fAAMiterStrokeRectIndexBuffer); | |
345 SkSafeSetNull(fAABevelStrokeRectIndexBuffer); | |
346 } | |
347 | |
72 static const uint16_t gMiterStrokeAARectIdx[] = { | 348 static const uint16_t gMiterStrokeAARectIdx[] = { |
73 0 + 0, 1 + 0, 5 + 0, 5 + 0, 4 + 0, 0 + 0, | 349 0 + 0, 1 + 0, 5 + 0, 5 + 0, 4 + 0, 0 + 0, |
74 1 + 0, 2 + 0, 6 + 0, 6 + 0, 5 + 0, 1 + 0, | 350 1 + 0, 2 + 0, 6 + 0, 6 + 0, 5 + 0, 1 + 0, |
75 2 + 0, 3 + 0, 7 + 0, 7 + 0, 6 + 0, 2 + 0, | 351 2 + 0, 3 + 0, 7 + 0, 7 + 0, 6 + 0, 2 + 0, |
76 3 + 0, 0 + 0, 4 + 0, 4 + 0, 7 + 0, 3 + 0, | 352 3 + 0, 0 + 0, 4 + 0, 4 + 0, 7 + 0, 3 + 0, |
77 | 353 |
78 0 + 4, 1 + 4, 5 + 4, 5 + 4, 4 + 4, 0 + 4, | 354 0 + 4, 1 + 4, 5 + 4, 5 + 4, 4 + 4, 0 + 4, |
79 1 + 4, 2 + 4, 6 + 4, 6 + 4, 5 + 4, 1 + 4, | 355 1 + 4, 2 + 4, 6 + 4, 6 + 4, 5 + 4, 1 + 4, |
80 2 + 4, 3 + 4, 7 + 4, 7 + 4, 6 + 4, 2 + 4, | 356 2 + 4, 3 + 4, 7 + 4, 7 + 4, 6 + 4, 2 + 4, |
81 3 + 4, 0 + 4, 4 + 4, 4 + 4, 7 + 4, 3 + 4, | 357 3 + 4, 0 + 4, 4 + 4, 4 + 4, 7 + 4, 3 + 4, |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
177 return fAABevelStrokeRectIndexBuffer; | 453 return fAABevelStrokeRectIndexBuffer; |
178 } | 454 } |
179 } | 455 } |
180 | 456 |
181 void GrAARectRenderer::geometryFillAARect(GrDrawTarget* target, | 457 void GrAARectRenderer::geometryFillAARect(GrDrawTarget* target, |
182 GrDrawState* drawState, | 458 GrDrawState* drawState, |
183 GrColor color, | 459 GrColor color, |
184 const SkMatrix& viewMatrix, | 460 const SkMatrix& viewMatrix, |
185 const SkRect& rect, | 461 const SkRect& rect, |
186 const SkRect& devRect) { | 462 const SkRect& devRect) { |
187 GrDrawState::AutoRestoreEffects are(drawState); | |
188 | |
189 SkMatrix localMatrix; | |
190 if (!viewMatrix.invert(&localMatrix)) { | |
191 SkDebugf("Cannot invert\n"); | |
192 return; | |
193 } | |
194 | |
195 CoverageAttribType type; | |
196 SkAutoTUnref<const GrGeometryProcessor> gp(create_rect_gp(*drawState, color, &type, | |
197 localMatrix)); | |
198 | |
199 size_t vertexStride = gp->getVertexStride(); | |
200 GrDrawTarget::AutoReleaseGeometry geo(target, 8, vertexStride, 0); | |
201 if (!geo.succeeded()) { | |
202 SkDebugf("Failed to get space for vertices!\n"); | |
203 return; | |
204 } | |
205 | |
206 if (NULL == fAAFillRectIndexBuffer) { | 463 if (NULL == fAAFillRectIndexBuffer) { |
207 fAAFillRectIndexBuffer = fGpu->createInstancedIndexBuffer(gFillAARectIdx , | 464 fAAFillRectIndexBuffer = fGpu->createInstancedIndexBuffer(gFillAARectIdx , |
208 kIndicesPerAAF illRect, | 465 kIndicesPerAAF illRect, |
209 kNumAAFillRect sInIndexBuffer, | 466 kNumAAFillRect sInIndexBuffer, |
210 kVertsPerAAFil lRect); | 467 kVertsPerAAFil lRect); |
211 } | 468 } |
212 GrIndexBuffer* indexBuffer = fAAFillRectIndexBuffer; | |
213 if (NULL == indexBuffer) { | |
214 SkDebugf("Failed to create index buffer!\n"); | |
215 return; | |
216 } | |
217 | 469 |
218 intptr_t verts = reinterpret_cast<intptr_t>(geo.vertices()); | 470 AAFillRectBatch::Geometry geometry; |
471 geometry.fRect = rect; | |
472 geometry.fViewMatrix = viewMatrix; | |
473 geometry.fDevRect = devRect; | |
474 geometry.fColor = color; | |
219 | 475 |
220 SkPoint* fan0Pos = reinterpret_cast<SkPoint*>(verts); | 476 SkAutoTUnref<GrBatch> batch(AAFillRectBatch::Create(geometry, fAAFillRectInd exBuffer)); |
221 SkPoint* fan1Pos = reinterpret_cast<SkPoint*>(verts + 4 * vertexStride); | 477 target->batchDraw(drawState, batch, kTriangles_GrPrimitiveType, &devRect); |
222 | |
223 SkScalar inset = SkMinScalar(devRect.width(), SK_Scalar1); | |
224 inset = SK_ScalarHalf * SkMinScalar(inset, devRect.height()); | |
225 | |
226 if (viewMatrix.rectStaysRect()) { | |
227 // Temporarily #if'ed out. We don't want to pass in the devRect but | |
228 // right now it is computed in GrContext::apply_aa_to_rect and we don't | |
229 // want to throw away the work | |
230 #if 0 | |
231 SkRect devRect; | |
232 combinedMatrix.mapRect(&devRect, rect); | |
233 #endif | |
234 | |
235 set_inset_fan(fan0Pos, vertexStride, devRect, -SK_ScalarHalf, -SK_Scalar Half); | |
236 set_inset_fan(fan1Pos, vertexStride, devRect, inset, inset); | |
237 } else { | |
238 // compute transformed (1, 0) and (0, 1) vectors | |
239 SkVector vec[2] = { | |
240 { viewMatrix[SkMatrix::kMScaleX], viewMatrix[SkMatrix::kMSkewY] }, | |
241 { viewMatrix[SkMatrix::kMSkewX], viewMatrix[SkMatrix::kMScaleY] } | |
242 }; | |
243 | |
244 vec[0].normalize(); | |
245 vec[0].scale(SK_ScalarHalf); | |
246 vec[1].normalize(); | |
247 vec[1].scale(SK_ScalarHalf); | |
248 | |
249 // create the rotated rect | |
250 fan0Pos->setRectFan(rect.fLeft, rect.fTop, | |
251 rect.fRight, rect.fBottom, vertexStride); | |
252 viewMatrix.mapPointsWithStride(fan0Pos, vertexStride, 4); | |
253 | |
254 // Now create the inset points and then outset the original | |
255 // rotated points | |
256 | |
257 // TL | |
258 *((SkPoint*)((intptr_t)fan1Pos + 0 * vertexStride)) = | |
259 *((SkPoint*)((intptr_t)fan0Pos + 0 * vertexStride)) + vec[0] + vec[1 ]; | |
260 *((SkPoint*)((intptr_t)fan0Pos + 0 * vertexStride)) -= vec[0] + vec[1]; | |
261 // BL | |
262 *((SkPoint*)((intptr_t)fan1Pos + 1 * vertexStride)) = | |
263 *((SkPoint*)((intptr_t)fan0Pos + 1 * vertexStride)) + vec[0] - vec[1 ]; | |
264 *((SkPoint*)((intptr_t)fan0Pos + 1 * vertexStride)) -= vec[0] - vec[1]; | |
265 // BR | |
266 *((SkPoint*)((intptr_t)fan1Pos + 2 * vertexStride)) = | |
267 *((SkPoint*)((intptr_t)fan0Pos + 2 * vertexStride)) - vec[0] - vec[1 ]; | |
268 *((SkPoint*)((intptr_t)fan0Pos + 2 * vertexStride)) += vec[0] + vec[1]; | |
269 // TR | |
270 *((SkPoint*)((intptr_t)fan1Pos + 3 * vertexStride)) = | |
271 *((SkPoint*)((intptr_t)fan0Pos + 3 * vertexStride)) - vec[0] + vec[1 ]; | |
272 *((SkPoint*)((intptr_t)fan0Pos + 3 * vertexStride)) += vec[0] - vec[1]; | |
273 } | |
274 | |
275 // Make verts point to vertex color and then set all the color and coverage vertex attrs values. | |
276 verts += sizeof(SkPoint); | |
277 for (int i = 0; i < 4; ++i) { | |
278 if (kUseCoverage_CoverageAttribType == type) { | |
279 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color; | |
280 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor) ) = 0; | |
281 } else { | |
282 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = 0; | |
283 } | |
284 } | |
285 | |
286 int scale; | |
287 if (inset < SK_ScalarHalf) { | |
288 scale = SkScalarFloorToInt(512.0f * inset / (inset + SK_ScalarHalf)); | |
289 SkASSERT(scale >= 0 && scale <= 255); | |
290 } else { | |
291 scale = 0xff; | |
292 } | |
293 | |
294 verts += 4 * vertexStride; | |
295 | |
296 float innerCoverage = GrNormalizeByteToFloat(scale); | |
297 GrColor scaledColor = (0xff == scale) ? color : SkAlphaMulQ(color, scale); | |
298 | |
299 for (int i = 0; i < 4; ++i) { | |
300 if (kUseCoverage_CoverageAttribType == type) { | |
301 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color; | |
302 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor) ) = innerCoverage; | |
303 } else { | |
304 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor; | |
305 } | |
306 } | |
307 | |
308 target->setIndexSourceToBuffer(indexBuffer); | |
309 target->drawIndexedInstances(drawState, | |
310 gp, | |
311 kTriangles_GrPrimitiveType, | |
312 1, | |
313 kVertsPerAAFillRect, | |
314 kIndicesPerAAFillRect); | |
315 target->resetIndexSource(); | |
316 } | 478 } |
317 | 479 |
318 void GrAARectRenderer::strokeAARect(GrDrawTarget* target, | 480 void GrAARectRenderer::strokeAARect(GrDrawTarget* target, |
319 GrDrawState* drawState, | 481 GrDrawState* drawState, |
320 GrColor color, | 482 GrColor color, |
321 const SkMatrix& viewMatrix, | 483 const SkMatrix& viewMatrix, |
322 const SkRect& rect, | 484 const SkRect& rect, |
323 const SkRect& devRect, | 485 const SkRect& devRect, |
324 const SkStrokeRec& stroke) { | 486 const SkStrokeRec& stroke) { |
325 SkVector devStrokeSize; | 487 SkVector devStrokeSize; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
379 // edge, while vertex number of inner edge is 4, the same as miter-stroke. | 541 // edge, while vertex number of inner edge is 4, the same as miter-stroke. |
380 if (!miterStroke) { | 542 if (!miterStroke) { |
381 devOutside.inset(0, ry); | 543 devOutside.inset(0, ry); |
382 devOutsideAssist.outset(0, ry); | 544 devOutsideAssist.outset(0, ry); |
383 } | 545 } |
384 | 546 |
385 this->geometryStrokeAARect(target, drawState, color, viewMatrix, devOutside, devOutsideAssist, | 547 this->geometryStrokeAARect(target, drawState, color, viewMatrix, devOutside, devOutsideAssist, |
386 devInside, miterStroke); | 548 devInside, miterStroke); |
387 } | 549 } |
388 | 550 |
551 static const GrGeometryProcessor* create_rect_gp(const GrDrawState& drawState, | |
552 GrColor color, | |
553 CoverageAttribType* type, | |
554 const SkMatrix& localMatrix) { | |
555 uint32_t flags = GrDefaultGeoProcFactory::kColor_GPType; | |
556 const GrGeometryProcessor* gp; | |
557 if (drawState.canTweakAlphaForCoverage()) { | |
558 gp = GrDefaultGeoProcFactory::Create(flags, color, SkMatrix::I(), localM atrix); | |
559 SkASSERT(gp->getVertexStride() == sizeof(GrDefaultGeoProcFactory::Positi onColorAttr)); | |
560 *type = kUseColor_CoverageAttribType; | |
561 } else { | |
562 flags |= GrDefaultGeoProcFactory::kCoverage_GPType; | |
563 gp = GrDefaultGeoProcFactory::Create(flags, color, SkMatrix::I(), localM atrix, | |
564 GrColorIsOpaque(color)); | |
565 SkASSERT(gp->getVertexStride()==sizeof(GrDefaultGeoProcFactory::Position ColorCoverageAttr)); | |
566 *type = kUseCoverage_CoverageAttribType; | |
567 } | |
568 return gp; | |
569 } | |
570 | |
571 | |
389 void GrAARectRenderer::geometryStrokeAARect(GrDrawTarget* target, | 572 void GrAARectRenderer::geometryStrokeAARect(GrDrawTarget* target, |
390 GrDrawState* drawState, | 573 GrDrawState* drawState, |
391 GrColor color, | 574 GrColor color, |
392 const SkMatrix& viewMatrix, | 575 const SkMatrix& viewMatrix, |
393 const SkRect& devOutside, | 576 const SkRect& devOutside, |
394 const SkRect& devOutsideAssist, | 577 const SkRect& devOutsideAssist, |
395 const SkRect& devInside, | 578 const SkRect& devInside, |
396 bool miterStroke) { | 579 bool miterStroke) { |
397 GrDrawState::AutoRestoreEffects are(drawState); | |
398 | |
399 SkMatrix localMatrix; | 580 SkMatrix localMatrix; |
400 if (!viewMatrix.invert(&localMatrix)) { | 581 if (!viewMatrix.invert(&localMatrix)) { |
401 SkDebugf("Cannot invert\n"); | 582 SkDebugf("Cannot invert\n"); |
402 return; | 583 return; |
403 } | 584 } |
404 | 585 |
405 CoverageAttribType type; | 586 CoverageAttribType type; |
406 SkAutoTUnref<const GrGeometryProcessor> gp(create_rect_gp(*drawState, color, &type, | 587 SkAutoTUnref<const GrGeometryProcessor> gp(create_rect_gp(*drawState, color, &type, |
407 localMatrix)); | 588 localMatrix)); |
408 | 589 |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
541 | 722 |
542 if (devInside.isEmpty()) { | 723 if (devInside.isEmpty()) { |
543 this->fillAARect(target, drawState, color, viewMatrix, devOutside, | 724 this->fillAARect(target, drawState, color, viewMatrix, devOutside, |
544 devOutside); | 725 devOutside); |
545 return; | 726 return; |
546 } | 727 } |
547 | 728 |
548 this->geometryStrokeAARect(target, drawState, color, viewMatrix, devOutside, devOutsideAssist, | 729 this->geometryStrokeAARect(target, drawState, color, viewMatrix, devOutside, devOutsideAssist, |
549 devInside, true); | 730 devInside, true); |
550 } | 731 } |
OLD | NEW |