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 "GrBufferAllocPool.h" | |
9 #include "GrDefaultGeoProcFactory.h" | 11 #include "GrDefaultGeoProcFactory.h" |
10 #include "GrGeometryProcessor.h" | 12 #include "GrGeometryProcessor.h" |
11 #include "GrGpu.h" | 13 #include "GrGpu.h" |
12 #include "GrInvariantOutput.h" | 14 #include "GrInvariantOutput.h" |
13 #include "SkColorPriv.h" | 15 #include "SkColorPriv.h" |
14 #include "gl/GrGLProcessor.h" | 16 #include "gl/GrGLProcessor.h" |
15 #include "gl/GrGLGeometryProcessor.h" | 17 #include "gl/GrGLGeometryProcessor.h" |
16 #include "gl/builders/GrGLProgramBuilder.h" | 18 #include "gl/builders/GrGLProgramBuilder.h" |
17 | 19 |
18 /////////////////////////////////////////////////////////////////////////////// | 20 /////////////////////////////////////////////////////////////////////////////// |
19 | 21 |
20 namespace { | 22 // TODO this doesn't build. The next step is to figure out how to further threa d GrBatch through |
bsalomon
2015/01/20 16:14:02
still true?
| |
21 // Should the coverage be multiplied into the color attrib or use a separate att rib. | 23 // the existing Skia pipline. Push it through, clean it up. |
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 | 24 |
48 static void set_inset_fan(SkPoint* pts, size_t stride, | 25 static void set_inset_fan(SkPoint* pts, size_t stride, |
49 const SkRect& r, SkScalar dx, SkScalar dy) { | 26 const SkRect& r, SkScalar dx, SkScalar dy) { |
50 pts->setRectFan(r.fLeft + dx, r.fTop + dy, | 27 pts->setRectFan(r.fLeft + dx, r.fTop + dy, |
51 r.fRight - dx, r.fBottom - dy, stride); | 28 r.fRight - dx, r.fBottom - dy, stride); |
52 } | 29 } |
53 | 30 |
54 void GrAARectRenderer::reset() { | |
55 SkSafeSetNull(fAAFillRectIndexBuffer); | |
56 SkSafeSetNull(fAAMiterStrokeRectIndexBuffer); | |
57 SkSafeSetNull(fAABevelStrokeRectIndexBuffer); | |
58 } | |
59 | |
60 static const uint16_t gFillAARectIdx[] = { | 31 static const uint16_t gFillAARectIdx[] = { |
61 0, 1, 5, 5, 4, 0, | 32 0, 1, 5, 5, 4, 0, |
62 1, 2, 6, 6, 5, 1, | 33 1, 2, 6, 6, 5, 1, |
63 2, 3, 7, 7, 6, 2, | 34 2, 3, 7, 7, 6, 2, |
64 3, 0, 4, 4, 7, 3, | 35 3, 0, 4, 4, 7, 3, |
65 4, 5, 6, 6, 7, 4, | 36 4, 5, 6, 6, 7, 4, |
66 }; | 37 }; |
67 | 38 |
68 static const int kIndicesPerAAFillRect = SK_ARRAY_COUNT(gFillAARectIdx); | 39 static const int kIndicesPerAAFillRect = SK_ARRAY_COUNT(gFillAARectIdx); |
69 static const int kVertsPerAAFillRect = 8; | 40 static const int kVertsPerAAFillRect = 8; |
70 static const int kNumAAFillRectsInIndexBuffer = 256; | 41 static const int kNumAAFillRectsInIndexBuffer = 256; |
71 | 42 |
43 static const GrGeometryProcessor* create_fill_rect_gp(bool tweakAlphaForCoverage , | |
44 const SkMatrix& localMatri x) { | |
45 uint32_t flags = GrDefaultGeoProcFactory::kColor_GPType; | |
46 const GrGeometryProcessor* gp; | |
47 if (tweakAlphaForCoverage) { | |
48 gp = GrDefaultGeoProcFactory::Create(flags, GrColor_WHITE, SkMatrix::I() , localMatrix, | |
49 false, 0xff); | |
50 } else { | |
51 flags |= GrDefaultGeoProcFactory::kCoverage_GPType; | |
52 gp = GrDefaultGeoProcFactory::Create(flags, GrColor_WHITE, SkMatrix::I() , localMatrix, | |
53 false, 0xff); | |
54 } | |
55 return gp; | |
56 } | |
57 | |
58 class AAFillRectBatch : public GrBatch { | |
59 public: | |
60 struct Geometry { | |
61 GrColor fColor; | |
62 SkMatrix fViewMatrix; | |
63 SkMatrix fLocalMatrix; | |
64 SkRect fRect; | |
65 SkRect fDevRect; | |
66 const GrIndexBuffer* fIndexBuffer; | |
67 }; | |
68 | |
69 static GrBatch* Create(const Geometry& geometry) { | |
70 return SkNEW_ARGS(AAFillRectBatch, (geometry)); | |
71 } | |
72 | |
73 const char* name() const SK_OVERRIDE { return "AAFillRectBatch"; } | |
74 | |
75 void getInvariantOutputColor(GrInitInvariantOutput* out, | |
76 const GrBatchOpt& batchOpt) const SK_OVERRIDE { | |
77 // When this is called on a batch, there is only one geometry bundle | |
78 if (!batchOpt.fCanTweakAlphaForCoverage && GrColorIsOpaque(fGeoData[0].f Color)) { | |
79 out->setUnknownOpaqueFourComponents(); | |
80 } else { | |
81 out->setUnknownFourComponents(); | |
82 } | |
83 } | |
84 void getInvariantOutputCoverage(GrInitInvariantOutput* out, | |
85 const GrBatchOpt& batchOpt) const SK_OVERRID E { | |
86 if (batchOpt.fCanTweakAlphaForCoverage) { | |
87 // uniform coverage | |
88 out->setKnownSingleComponent(0xff); | |
89 } else { | |
90 out->setUnknownSingleComponent(); | |
91 } | |
92 } | |
93 | |
94 void initBatchOpt(const GrBatchOpt& batchOpt) { | |
95 fBatchOpt = batchOpt; | |
96 } | |
97 | |
98 void initBatchTracker(const GrGeometryProcessor::InitBT& init) SK_OVERRIDE { | |
99 // Handle any color overrides | |
100 if (init.fColorIgnored) { | |
101 fGeoData[0].fColor = GrColor_ILLEGAL; | |
102 } else if (GrColor_ILLEGAL != init.fOverrideColor) { | |
103 fGeoData[0].fColor = init.fOverrideColor; | |
104 } | |
105 | |
106 // setup batch properties | |
107 fBatch.fLocalMatrix = fGeoData[0].fLocalMatrix; | |
108 fBatch.fColorIgnored = init.fColorIgnored; | |
109 fBatch.fColor = fGeoData[0].fColor; | |
110 fBatch.fUsesLocalCoords = init.fUsesLocalCoords; | |
111 fBatch.fCoverageIgnored = init.fCoverageIgnored; | |
112 } | |
113 | |
114 bool onCanMakeEqual(const GrBatch& t) const SK_OVERRIDE { | |
115 const AAFillRectBatch& that = t.cast<AAFillRectBatch>(); | |
116 if (this->canTweakAlphaForCoverage() != that.canTweakAlphaForCoverage()) { | |
117 return false; | |
118 } | |
119 | |
120 if (this->colorIgnored() != that.colorIgnored()) { | |
121 return false; | |
122 } | |
123 | |
124 if (this->usesLocalCoords() != that.usesLocalCoords()) { | |
125 return false; | |
126 } | |
127 | |
128 if (this->usesLocalCoords() && !this->localMatrix().cheapEqualTo(that.lo calMatrix())) { | |
129 return false; | |
130 } | |
131 return true; | |
132 } | |
133 | |
134 void makeEqual(GrBatch* t) SK_OVERRIDE { | |
135 AAFillRectBatch* that = t->cast<AAFillRectBatch>(); | |
136 | |
137 if (this->color() != that->color()) { | |
138 fBatch.fColor = GrColor_ILLEGAL; | |
139 } | |
140 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()) ; | |
141 } | |
142 | |
143 GrColor color() const { return fBatch.fColor; } | |
144 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } | |
145 bool canTweakAlphaForCoverage() const { return fBatchOpt.fCanTweakAlphaForCo verage; } | |
146 bool colorIgnored() const { return fBatch.fColorIgnored; } | |
147 const SkMatrix& localMatrix() const { return fBatch.fLocalMatrix; } | |
148 | |
149 void generateGeometry(GrGpu* gpu, | |
150 GrVertexBufferAllocPool* vpool, | |
151 GrIndexBufferAllocPool* ipool, | |
152 GrOptDrawState* optState) SK_OVERRIDE { | |
153 SkASSERT(!fGeometryGenerated); | |
154 bool canTweakAlphaForCoverage = this->canTweakAlphaForCoverage(); | |
155 const GrGeometryProcessor* gp = create_fill_rect_gp(canTweakAlphaForCove rage, | |
156 this->localMatrix()) ; | |
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(optState->batchTracker(), 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 = vpool->makeSpace(vertexStride, | |
181 vertexCount, | |
182 &vertexBuffer, | |
183 &firstVertex); | |
184 | |
185 const GrIndexBuffer* indexBuffer = NULL; | |
186 for (int i = 0; i < instanceCount; i++) { | |
187 const Geometry& args = fGeoData[i]; | |
188 indexBuffer = args.fIndexBuffer; | |
189 this->generateGeometry(vertices, | |
190 i * kVertsPerAAFillRect * vertexStride, | |
191 vertexStride, | |
192 args.fColor, | |
193 args.fViewMatrix, | |
194 args.fLocalMatrix, | |
195 args.fRect, | |
196 args.fDevRect, | |
197 canTweakAlphaForCoverage); | |
198 } | |
199 | |
200 vpool->unmap(); | |
201 | |
202 SkASSERT(indexBuffer); | |
203 | |
204 fDrawInfo.setPrimitiveType(kTriangles_GrPrimitiveType); | |
205 fDrawInfo.setStartVertex(0); | |
206 fDrawInfo.setStartIndex(0); | |
207 fDrawInfo.setVerticesPerInstance(kVertsPerAAFillRect); | |
208 fDrawInfo.setIndicesPerInstance(kIndicesPerAAFillRect); | |
209 fDrawInfo.adjustStartVertex(firstVertex); | |
210 fDrawInfo.setVertexBuffer(vertexBuffer); | |
211 fDrawInfo.setIndexBuffer(indexBuffer); | |
212 | |
213 // TODO this goes away when I pull GP of optstate | |
214 optState->setPrimitiveProcessor(gp); | |
215 gp->unref(); | |
216 optState->finalize(gpu); | |
217 fGeometryGenerated = true; | |
218 } | |
219 | |
220 void draw(GrGpu* gpu, const GrOptDrawState* optState) SK_OVERRIDE { | |
221 SkASSERT(fGeometryGenerated); | |
222 int maxInstancesPerDraw = kNumAAFillRectsInIndexBuffer; | |
223 int instanceCount = fGeoData.count(); | |
224 while (instanceCount) { | |
225 fDrawInfo.setInstanceCount(SkTMin(instanceCount, maxInstancesPerDraw )); | |
226 fDrawInfo.setVertexCount(fDrawInfo.instanceCount() * fDrawInfo.verti cesPerInstance()); | |
227 fDrawInfo.setIndexCount(fDrawInfo.instanceCount() * fDrawInfo.indice sPerInstance()); | |
228 | |
229 gpu->draw(*optState, fDrawInfo); | |
230 | |
231 fDrawInfo.setStartVertex(fDrawInfo.startVertex() + fDrawInfo.vertexC ount()); | |
232 instanceCount -= fDrawInfo.instanceCount(); | |
233 } | |
234 } | |
235 | |
236 bool fGeometryGenerated; | |
237 GrDrawTarget::DrawInfo fDrawInfo; | |
238 SkSTArray<32, Geometry, true>* geoData() { return &fGeoData; } | |
239 | |
240 private: | |
241 AAFillRectBatch(const Geometry& geometry) | |
242 : fGeometryGenerated(false) { | |
243 this->initClassID<AAFillRectBatch>(); | |
244 fGeoData.push_back(geometry); | |
245 } | |
246 | |
247 void generateGeometry(void* vertices, | |
248 uint32_t offset, | |
249 uint32_t vertexStride, | |
250 GrColor color, | |
251 const SkMatrix& viewMatrix, | |
252 const SkMatrix& localMatrix, | |
253 const SkRect& rect, | |
254 const SkRect& devRect, | |
255 bool tweakAlphaForCoverage) const { | |
256 intptr_t verts = reinterpret_cast<intptr_t>(vertices) + offset; | |
257 | |
258 SkPoint* fan0Pos = reinterpret_cast<SkPoint*>(verts); | |
259 SkPoint* fan1Pos = reinterpret_cast<SkPoint*>(verts + 4 * vertexStride); | |
260 | |
261 SkScalar inset = SkMinScalar(devRect.width(), SK_Scalar1); | |
262 inset = SK_ScalarHalf * SkMinScalar(inset, devRect.height()); | |
263 | |
264 if (viewMatrix.rectStaysRect()) { | |
265 // Temporarily #if'ed out. We don't want to pass in the devRect but | |
266 // right now it is computed in GrContext::apply_aa_to_rect and we do n't | |
267 // want to throw away the work | |
268 #if 0 | |
269 SkRect devRect; | |
270 combinedMatrix.mapRect(&devRect, rect); | |
271 #endif | |
272 | |
273 set_inset_fan(fan0Pos, vertexStride, devRect, -SK_ScalarHalf, -SK_Sc alarHalf); | |
274 set_inset_fan(fan1Pos, vertexStride, devRect, inset, inset); | |
275 } else { | |
276 // compute transformed (1, 0) and (0, 1) vectors | |
277 SkVector vec[2] = { | |
278 { viewMatrix[SkMatrix::kMScaleX], viewMatrix[SkMatrix::kMSkewY] }, | |
279 { viewMatrix[SkMatrix::kMSkewX], viewMatrix[SkMatrix::kMScaleY] } | |
280 }; | |
281 | |
282 vec[0].normalize(); | |
283 vec[0].scale(SK_ScalarHalf); | |
284 vec[1].normalize(); | |
285 vec[1].scale(SK_ScalarHalf); | |
286 | |
287 // create the rotated rect | |
288 fan0Pos->setRectFan(rect.fLeft, rect.fTop, | |
289 rect.fRight, rect.fBottom, vertexStride); | |
290 viewMatrix.mapPointsWithStride(fan0Pos, vertexStride, 4); | |
291 | |
292 // Now create the inset points and then outset the original | |
293 // rotated points | |
294 | |
295 // TL | |
296 *((SkPoint*)((intptr_t)fan1Pos + 0 * vertexStride)) = | |
297 *((SkPoint*)((intptr_t)fan0Pos + 0 * vertexStride)) + vec[0] + v ec[1]; | |
298 *((SkPoint*)((intptr_t)fan0Pos + 0 * vertexStride)) -= vec[0] + vec[ 1]; | |
299 // BL | |
300 *((SkPoint*)((intptr_t)fan1Pos + 1 * vertexStride)) = | |
301 *((SkPoint*)((intptr_t)fan0Pos + 1 * vertexStride)) + vec[0] - v ec[1]; | |
302 *((SkPoint*)((intptr_t)fan0Pos + 1 * vertexStride)) -= vec[0] - vec[ 1]; | |
303 // BR | |
304 *((SkPoint*)((intptr_t)fan1Pos + 2 * vertexStride)) = | |
305 *((SkPoint*)((intptr_t)fan0Pos + 2 * vertexStride)) - vec[0] - v ec[1]; | |
306 *((SkPoint*)((intptr_t)fan0Pos + 2 * vertexStride)) += vec[0] + vec[ 1]; | |
307 // TR | |
308 *((SkPoint*)((intptr_t)fan1Pos + 3 * vertexStride)) = | |
309 *((SkPoint*)((intptr_t)fan0Pos + 3 * vertexStride)) - vec[0] + v ec[1]; | |
310 *((SkPoint*)((intptr_t)fan0Pos + 3 * vertexStride)) += vec[0] - vec[ 1]; | |
311 } | |
312 | |
313 // Make verts point to vertex color and then set all the color and cover age vertex attrs values. | |
314 verts += sizeof(SkPoint); | |
315 for (int i = 0; i < 4; ++i) { | |
316 if (tweakAlphaForCoverage) { | |
317 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = 0; | |
318 } else { | |
319 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color; | |
320 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrCo lor)) = 0; | |
321 } | |
322 } | |
323 | |
324 int scale; | |
325 if (inset < SK_ScalarHalf) { | |
326 scale = SkScalarFloorToInt(512.0f * inset / (inset + SK_ScalarHalf)) ; | |
327 SkASSERT(scale >= 0 && scale <= 255); | |
328 } else { | |
329 scale = 0xff; | |
330 } | |
331 | |
332 verts += 4 * vertexStride; | |
333 | |
334 float innerCoverage = GrNormalizeByteToFloat(scale); | |
335 GrColor scaledColor = (0xff == scale) ? color : SkAlphaMulQ(color, scale ); | |
336 | |
337 for (int i = 0; i < 4; ++i) { | |
338 if (tweakAlphaForCoverage) { | |
339 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledCo lor; | |
340 } else { | |
341 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color; | |
342 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrCo lor)) = innerCoverage; | |
343 } | |
344 } | |
345 } | |
346 | |
347 struct BatchTracker { | |
348 GrColor fColor; | |
349 SkMatrix fLocalMatrix; | |
350 bool fUsesLocalCoords; | |
351 bool fColorIgnored; | |
352 bool fCoverageIgnored; | |
353 }; | |
354 | |
355 GrBatchOpt fBatchOpt; | |
356 BatchTracker fBatch; | |
357 SkSTArray<32, Geometry, true> fGeoData; | |
358 }; | |
359 | |
360 namespace { | |
361 // Should the coverage be multiplied into the color attrib or use a separate att rib. | |
362 enum CoverageAttribType { | |
363 kUseColor_CoverageAttribType, | |
364 kUseCoverage_CoverageAttribType, | |
365 }; | |
366 } | |
367 | |
368 void GrAARectRenderer::reset() { | |
369 SkSafeSetNull(fAAFillRectIndexBuffer); | |
370 SkSafeSetNull(fAAMiterStrokeRectIndexBuffer); | |
371 SkSafeSetNull(fAABevelStrokeRectIndexBuffer); | |
372 } | |
373 | |
72 static const uint16_t gMiterStrokeAARectIdx[] = { | 374 static const uint16_t gMiterStrokeAARectIdx[] = { |
73 0 + 0, 1 + 0, 5 + 0, 5 + 0, 4 + 0, 0 + 0, | 375 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, | 376 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, | 377 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, | 378 3 + 0, 0 + 0, 4 + 0, 4 + 0, 7 + 0, 3 + 0, |
77 | 379 |
78 0 + 4, 1 + 4, 5 + 4, 5 + 4, 4 + 4, 0 + 4, | 380 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, | 381 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, | 382 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, | 383 3 + 4, 0 + 4, 4 + 4, 4 + 4, 7 + 4, 3 + 4, |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
178 } | 480 } |
179 } | 481 } |
180 | 482 |
181 void GrAARectRenderer::geometryFillAARect(GrDrawTarget* target, | 483 void GrAARectRenderer::geometryFillAARect(GrDrawTarget* target, |
182 GrDrawState* drawState, | 484 GrDrawState* drawState, |
183 GrColor color, | 485 GrColor color, |
184 const SkMatrix& viewMatrix, | 486 const SkMatrix& viewMatrix, |
185 const SkRect& rect, | 487 const SkRect& rect, |
186 const SkRect& devRect) { | 488 const SkRect& devRect) { |
187 GrDrawState::AutoRestoreEffects are(drawState); | 489 GrDrawState::AutoRestoreEffects are(drawState); |
490 if (NULL == fAAFillRectIndexBuffer) { | |
491 fAAFillRectIndexBuffer = fGpu->createInstancedIndexBuffer(gFillAARectIdx , | |
492 kIndicesPerAAF illRect, | |
493 kNumAAFillRect sInIndexBuffer, | |
494 kVertsPerAAFil lRect); | |
495 } | |
188 | 496 |
189 SkMatrix localMatrix; | 497 SkMatrix localMatrix; |
190 if (!viewMatrix.invert(&localMatrix)) { | 498 if (!viewMatrix.invert(&localMatrix)) { |
191 SkDebugf("Cannot invert\n"); | 499 SkDebugf("Cannot invert\n"); |
192 return; | 500 return; |
193 } | 501 } |
194 | 502 |
195 CoverageAttribType type; | 503 AAFillRectBatch::Geometry geometry; |
196 SkAutoTUnref<const GrGeometryProcessor> gp(create_rect_gp(*drawState, color, &type, | 504 geometry.fRect = rect; |
197 localMatrix)); | 505 geometry.fViewMatrix = viewMatrix; |
506 geometry.fLocalMatrix = localMatrix; | |
507 geometry.fDevRect = devRect; | |
508 geometry.fColor = color; | |
509 geometry.fIndexBuffer = fAAFillRectIndexBuffer; | |
198 | 510 |
199 size_t vertexStride = gp->getVertexStride(); | 511 GrBatch* batch = AAFillRectBatch::Create(geometry); |
200 GrDrawTarget::AutoReleaseGeometry geo(target, 8, vertexStride, 0); | 512 target->batchDraw(drawState, batch, kTriangles_GrPrimitiveType, &devRect); |
201 if (!geo.succeeded()) { | |
202 SkDebugf("Failed to get space for vertices!\n"); | |
203 return; | |
204 } | |
205 | |
206 if (NULL == fAAFillRectIndexBuffer) { | |
207 fAAFillRectIndexBuffer = fGpu->createInstancedIndexBuffer(gFillAARectIdx , | |
208 kIndicesPerAAF illRect, | |
209 kNumAAFillRect sInIndexBuffer, | |
210 kVertsPerAAFil lRect); | |
211 } | |
212 GrIndexBuffer* indexBuffer = fAAFillRectIndexBuffer; | |
213 if (NULL == indexBuffer) { | |
214 SkDebugf("Failed to create index buffer!\n"); | |
215 return; | |
216 } | |
217 | |
218 intptr_t verts = reinterpret_cast<intptr_t>(geo.vertices()); | |
219 | |
220 SkPoint* fan0Pos = reinterpret_cast<SkPoint*>(verts); | |
221 SkPoint* fan1Pos = reinterpret_cast<SkPoint*>(verts + 4 * vertexStride); | |
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 } | 513 } |
317 | 514 |
318 void GrAARectRenderer::strokeAARect(GrDrawTarget* target, | 515 void GrAARectRenderer::strokeAARect(GrDrawTarget* target, |
319 GrDrawState* drawState, | 516 GrDrawState* drawState, |
320 GrColor color, | 517 GrColor color, |
321 const SkMatrix& viewMatrix, | 518 const SkMatrix& viewMatrix, |
322 const SkRect& rect, | 519 const SkRect& rect, |
323 const SkRect& devRect, | 520 const SkRect& devRect, |
324 const SkStrokeRec& stroke) { | 521 const SkStrokeRec& stroke) { |
325 SkVector devStrokeSize; | 522 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. | 576 // edge, while vertex number of inner edge is 4, the same as miter-stroke. |
380 if (!miterStroke) { | 577 if (!miterStroke) { |
381 devOutside.inset(0, ry); | 578 devOutside.inset(0, ry); |
382 devOutsideAssist.outset(0, ry); | 579 devOutsideAssist.outset(0, ry); |
383 } | 580 } |
384 | 581 |
385 this->geometryStrokeAARect(target, drawState, color, viewMatrix, devOutside, devOutsideAssist, | 582 this->geometryStrokeAARect(target, drawState, color, viewMatrix, devOutside, devOutsideAssist, |
386 devInside, miterStroke); | 583 devInside, miterStroke); |
387 } | 584 } |
388 | 585 |
586 static const GrGeometryProcessor* create_rect_gp(const GrDrawState& drawState, | |
587 GrColor color, | |
588 CoverageAttribType* type, | |
589 const SkMatrix& localMatrix) { | |
590 uint32_t flags = GrDefaultGeoProcFactory::kColor_GPType; | |
591 const GrGeometryProcessor* gp; | |
592 if (drawState.canTweakAlphaForCoverage()) { | |
593 gp = GrDefaultGeoProcFactory::Create(flags, color, SkMatrix::I(), localM atrix); | |
594 SkASSERT(gp->getVertexStride() == sizeof(GrDefaultGeoProcFactory::Positi onColorAttr)); | |
595 *type = kUseColor_CoverageAttribType; | |
596 } else { | |
597 flags |= GrDefaultGeoProcFactory::kCoverage_GPType; | |
598 gp = GrDefaultGeoProcFactory::Create(flags, color, SkMatrix::I(), localM atrix, | |
599 GrColorIsOpaque(color)); | |
600 SkASSERT(gp->getVertexStride()==sizeof(GrDefaultGeoProcFactory::Position ColorCoverageAttr)); | |
601 *type = kUseCoverage_CoverageAttribType; | |
602 } | |
603 return gp; | |
604 } | |
605 | |
606 | |
389 void GrAARectRenderer::geometryStrokeAARect(GrDrawTarget* target, | 607 void GrAARectRenderer::geometryStrokeAARect(GrDrawTarget* target, |
390 GrDrawState* drawState, | 608 GrDrawState* drawState, |
391 GrColor color, | 609 GrColor color, |
392 const SkMatrix& viewMatrix, | 610 const SkMatrix& viewMatrix, |
393 const SkRect& devOutside, | 611 const SkRect& devOutside, |
394 const SkRect& devOutsideAssist, | 612 const SkRect& devOutsideAssist, |
395 const SkRect& devInside, | 613 const SkRect& devInside, |
396 bool miterStroke) { | 614 bool miterStroke) { |
397 GrDrawState::AutoRestoreEffects are(drawState); | 615 GrDrawState::AutoRestoreEffects are(drawState); |
398 | 616 |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
541 | 759 |
542 if (devInside.isEmpty()) { | 760 if (devInside.isEmpty()) { |
543 this->fillAARect(target, drawState, color, viewMatrix, devOutside, | 761 this->fillAARect(target, drawState, color, viewMatrix, devOutside, |
544 devOutside); | 762 devOutside); |
545 return; | 763 return; |
546 } | 764 } |
547 | 765 |
548 this->geometryStrokeAARect(target, drawState, color, viewMatrix, devOutside, devOutsideAssist, | 766 this->geometryStrokeAARect(target, drawState, color, viewMatrix, devOutside, devOutsideAssist, |
549 devInside, true); | 767 devInside, true); |
550 } | 768 } |
OLD | NEW |