Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(172)

Side by Side Diff: src/gpu/GrAARectRenderer.cpp

Issue 845103005: GrBatchPrototype (Closed) Base URL: https://skia.googlesource.com/skia.git@lc2
Patch Set: update devrect Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « gyp/gpu.gypi ('k') | src/gpu/GrBatch.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 SkMatrix fLocalMatrix;
62 SkRect fRect;
63 SkRect fDevRect;
64 const GrIndexBuffer* fIndexBuffer;
65 };
66
67 static GrBatch* Create(const Geometry& geometry) {
68 return SkNEW_ARGS(AAFillRectBatch, (geometry));
69 }
70
71 const char* name() const SK_OVERRIDE { return "AAFillRectBatch"; }
72
73 void getInvariantOutputColor(GrInitInvariantOutput* out,
74 const GrBatchOpt& batchOpt) const SK_OVERRIDE {
75 // When this is called on a batch, there is only one geometry bundle
76 if (!batchOpt.fCanTweakAlphaForCoverage && GrColorIsOpaque(fGeoData[0].f Color)) {
77 out->setUnknownOpaqueFourComponents();
78 } else {
79 out->setUnknownFourComponents();
80 }
81 }
82 void getInvariantOutputCoverage(GrInitInvariantOutput* out,
83 const GrBatchOpt& batchOpt) const SK_OVERRID E {
84 if (batchOpt.fCanTweakAlphaForCoverage) {
85 // uniform coverage
86 out->setKnownSingleComponent(0xff);
87 } else {
88 out->setUnknownSingleComponent();
89 }
90 }
91
92 void initBatchOpt(const GrBatchOpt& batchOpt) {
93 fBatchOpt = batchOpt;
94 }
95
96 void initBatchTracker(const GrGeometryProcessor::InitBT& init) SK_OVERRIDE {
97 // Handle any color overrides
98 if (init.fColorIgnored) {
99 fGeoData[0].fColor = GrColor_ILLEGAL;
100 } else if (GrColor_ILLEGAL != init.fOverrideColor) {
101 fGeoData[0].fColor = init.fOverrideColor;
102 }
103
104 // setup batch properties
105 fBatch.fLocalMatrix = fGeoData[0].fLocalMatrix;
106 fBatch.fColorIgnored = init.fColorIgnored;
107 fBatch.fColor = fGeoData[0].fColor;
108 fBatch.fUsesLocalCoords = init.fUsesLocalCoords;
109 fBatch.fCoverageIgnored = init.fCoverageIgnored;
110 fBatch.fDevRect = fGeoData[0].fDevRect;
111 }
112
113 bool onCombineIfPossible(GrBatch* t) SK_OVERRIDE {
114 AAFillRectBatch* that = t->cast<AAFillRectBatch>();
115 if (this->canTweakAlphaForCoverage() != that->canTweakAlphaForCoverage() ) {
116 return false;
117 }
118
119 if (this->colorIgnored() != that->colorIgnored()) {
120 return false;
121 }
122
123 if (!this->devRect().contains(that->devRect())) {
124 return false;
125 }
126
127 if (this->usesLocalCoords() != that->usesLocalCoords()) {
128 return false;
129 }
130
131 if (this->usesLocalCoords() && !this->localMatrix().cheapEqualTo(that->l ocalMatrix())) {
132 return false;
133 }
134
135 if (this->color() != that->color()) {
136 fBatch.fColor = GrColor_ILLEGAL;
137 }
138 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()) ;
139 return true;
140 }
141
142 GrColor color() const { return fBatch.fColor; }
143 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
144 bool canTweakAlphaForCoverage() const { return fBatchOpt.fCanTweakAlphaForCo verage; }
145 bool colorIgnored() const { return fBatch.fColorIgnored; }
146 const SkMatrix& localMatrix() const { return fBatch.fLocalMatrix; }
147 const SkRect& devRect() const { return fBatch.fDevRect; }
148
149 void generateGeometry(GrBatchBuffer* batchBuffer, const GrOptDrawState* optS tate) SK_OVERRIDE {
150 bool canTweakAlphaForCoverage = this->canTweakAlphaForCoverage();
151 const GrGeometryProcessor* gp = create_fill_rect_gp(canTweakAlphaForCove rage,
152 this->localMatrix()) ;
153
154 batchBuffer->initDraw(gp, optState);
155 gp->unref();
156
157 // TODO this is hacky, but the only way we have to initialize the GP is to use the initBT
158 // struct so we can generate the correct shader. Once we have GrBatch e verywhere we can
159 // remove this nastiness
160 GrGeometryProcessor::InitBT init;
161 init.fColorIgnored = fBatch.fColorIgnored;
162 init.fOverrideColor = GrColor_ILLEGAL;
163 init.fCoverageIgnored = fBatch.fCoverageIgnored;
164 init.fUsesLocalCoords = this->usesLocalCoords();
165 gp->initBatchTracker(batchBuffer->currentBatchTracker(), init);
166
167 size_t vertexStride = gp->getVertexStride();
168
169 SkASSERT(canTweakAlphaForCoverage ?
170 vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorAt tr) :
171 vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorCo verageAttr));
172
173 int instanceCount = fGeoData.count();
174 int vertexCount = kVertsPerAAFillRect * instanceCount;
175
176 const GrVertexBuffer* vertexBuffer;
177 int firstVertex;
178
179 void *vertices = batchBuffer->vertexPool()->makeSpace(vertexStride,
180 vertexCount,
181 &vertexBuffer,
182 &firstVertex);
183
184 const GrIndexBuffer* indexBuffer = NULL;
185 for (int i = 0; i < instanceCount; i++) {
186 const Geometry& args = fGeoData[i];
187 indexBuffer = args.fIndexBuffer;
188 this->generateGeometry(vertices,
189 i * kVertsPerAAFillRect * vertexStride,
190 vertexStride,
191 args.fColor,
192 args.fViewMatrix,
193 args.fLocalMatrix,
194 args.fRect,
195 args.fDevRect,
196 canTweakAlphaForCoverage);
197 }
198
199 batchBuffer->vertexPool()->unmap();
200
201 SkASSERT(indexBuffer);
202
203 GrDrawTarget::DrawInfo drawInfo;
204 drawInfo.setPrimitiveType(kTriangles_GrPrimitiveType);
205 drawInfo.setStartVertex(0);
206 drawInfo.setStartIndex(0);
207 drawInfo.setVerticesPerInstance(kVertsPerAAFillRect);
208 drawInfo.setIndicesPerInstance(kIndicesPerAAFillRect);
209 drawInfo.adjustStartVertex(firstVertex);
210 drawInfo.setVertexBuffer(vertexBuffer);
211 drawInfo.setIndexBuffer(indexBuffer);
212
213 int maxInstancesPerDraw = kNumAAFillRectsInIndexBuffer;
214
215
216 while (instanceCount) {
217 drawInfo.setInstanceCount(SkTMin(instanceCount, maxInstancesPerDraw) );
218 drawInfo.setVertexCount(drawInfo.instanceCount() * drawInfo.vertices PerInstance());
219 drawInfo.setIndexCount(drawInfo.instanceCount() * drawInfo.indicesPe rInstance());
220
221 batchBuffer->draw(drawInfo);
222
223 drawInfo.setStartVertex(drawInfo.startVertex() + drawInfo.vertexCoun t());
224 instanceCount -= drawInfo.instanceCount();
225 }
226 }
227
228 SkSTArray<32, Geometry, true>* geoData() { return &fGeoData; }
229
230 private:
231 AAFillRectBatch(const Geometry& geometry) {
232 this->initClassID<AAFillRectBatch>();
233 fGeoData.push_back(geometry);
234 }
235
236 void generateGeometry(void* vertices,
237 uint32_t offset,
238 uint32_t vertexStride,
239 GrColor color,
240 const SkMatrix& viewMatrix,
241 const SkMatrix& localMatrix,
242 const SkRect& rect,
243 const SkRect& devRect,
244 bool tweakAlphaForCoverage) const {
245 intptr_t verts = reinterpret_cast<intptr_t>(vertices) + offset;
246
247 SkPoint* fan0Pos = reinterpret_cast<SkPoint*>(verts);
248 SkPoint* fan1Pos = reinterpret_cast<SkPoint*>(verts + 4 * vertexStride);
249
250 SkScalar inset = SkMinScalar(devRect.width(), SK_Scalar1);
251 inset = SK_ScalarHalf * SkMinScalar(inset, devRect.height());
252
253 if (viewMatrix.rectStaysRect()) {
254 set_inset_fan(fan0Pos, vertexStride, devRect, -SK_ScalarHalf, -SK_Sc alarHalf);
255 set_inset_fan(fan1Pos, vertexStride, devRect, inset, inset);
256 } else {
257 // compute transformed (1, 0) and (0, 1) vectors
258 SkVector vec[2] = {
259 { viewMatrix[SkMatrix::kMScaleX], viewMatrix[SkMatrix::kMSkewY] },
260 { viewMatrix[SkMatrix::kMSkewX], viewMatrix[SkMatrix::kMScaleY] }
261 };
262
263 vec[0].normalize();
264 vec[0].scale(SK_ScalarHalf);
265 vec[1].normalize();
266 vec[1].scale(SK_ScalarHalf);
267
268 // create the rotated rect
269 fan0Pos->setRectFan(rect.fLeft, rect.fTop,
270 rect.fRight, rect.fBottom, vertexStride);
271 viewMatrix.mapPointsWithStride(fan0Pos, vertexStride, 4);
272
273 // Now create the inset points and then outset the original
274 // rotated points
275
276 // TL
277 *((SkPoint*)((intptr_t)fan1Pos + 0 * vertexStride)) =
278 *((SkPoint*)((intptr_t)fan0Pos + 0 * vertexStride)) + vec[0] + v ec[1];
279 *((SkPoint*)((intptr_t)fan0Pos + 0 * vertexStride)) -= vec[0] + vec[ 1];
280 // BL
281 *((SkPoint*)((intptr_t)fan1Pos + 1 * vertexStride)) =
282 *((SkPoint*)((intptr_t)fan0Pos + 1 * vertexStride)) + vec[0] - v ec[1];
283 *((SkPoint*)((intptr_t)fan0Pos + 1 * vertexStride)) -= vec[0] - vec[ 1];
284 // BR
285 *((SkPoint*)((intptr_t)fan1Pos + 2 * vertexStride)) =
286 *((SkPoint*)((intptr_t)fan0Pos + 2 * vertexStride)) - vec[0] - v ec[1];
287 *((SkPoint*)((intptr_t)fan0Pos + 2 * vertexStride)) += vec[0] + vec[ 1];
288 // TR
289 *((SkPoint*)((intptr_t)fan1Pos + 3 * vertexStride)) =
290 *((SkPoint*)((intptr_t)fan0Pos + 3 * vertexStride)) - vec[0] + v ec[1];
291 *((SkPoint*)((intptr_t)fan0Pos + 3 * vertexStride)) += vec[0] - vec[ 1];
292 }
293
294 // Make verts point to vertex color and then set all the color and cover age vertex attrs values.
295 verts += sizeof(SkPoint);
296 for (int i = 0; i < 4; ++i) {
297 if (tweakAlphaForCoverage) {
298 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = 0;
299 } else {
300 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color;
301 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrCo lor)) = 0;
302 }
303 }
304
305 int scale;
306 if (inset < SK_ScalarHalf) {
307 scale = SkScalarFloorToInt(512.0f * inset / (inset + SK_ScalarHalf)) ;
308 SkASSERT(scale >= 0 && scale <= 255);
309 } else {
310 scale = 0xff;
311 }
312
313 verts += 4 * vertexStride;
314
315 float innerCoverage = GrNormalizeByteToFloat(scale);
316 GrColor scaledColor = (0xff == scale) ? color : SkAlphaMulQ(color, scale );
317
318 for (int i = 0; i < 4; ++i) {
319 if (tweakAlphaForCoverage) {
320 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledCo lor;
321 } else {
322 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color;
323 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrCo lor)) = innerCoverage;
324 }
325 }
326 }
327
328 struct BatchTracker {
329 GrColor fColor;
330 SkMatrix fLocalMatrix;
331 SkRect fDevRect;
332 bool fUsesLocalCoords;
333 bool fColorIgnored;
334 bool fCoverageIgnored;
335 };
336
337 GrBatchOpt fBatchOpt;
338 BatchTracker fBatch;
339 SkSTArray<32, Geometry, true> fGeoData;
340 };
341
342 namespace {
343 // Should the coverage be multiplied into the color attrib or use a separate att rib.
344 enum CoverageAttribType {
345 kUseColor_CoverageAttribType,
346 kUseCoverage_CoverageAttribType,
347 };
348 }
349
350 void GrAARectRenderer::reset() {
351 SkSafeSetNull(fAAFillRectIndexBuffer);
352 SkSafeSetNull(fAAMiterStrokeRectIndexBuffer);
353 SkSafeSetNull(fAABevelStrokeRectIndexBuffer);
354 }
355
72 static const uint16_t gMiterStrokeAARectIdx[] = { 356 static const uint16_t gMiterStrokeAARectIdx[] = {
73 0 + 0, 1 + 0, 5 + 0, 5 + 0, 4 + 0, 0 + 0, 357 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, 358 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, 359 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, 360 3 + 0, 0 + 0, 4 + 0, 4 + 0, 7 + 0, 3 + 0,
77 361
78 0 + 4, 1 + 4, 5 + 4, 5 + 4, 4 + 4, 0 + 4, 362 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, 363 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, 364 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, 365 3 + 4, 0 + 4, 4 + 4, 4 + 4, 7 + 4, 3 + 4,
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 } 462 }
179 } 463 }
180 464
181 void GrAARectRenderer::geometryFillAARect(GrDrawTarget* target, 465 void GrAARectRenderer::geometryFillAARect(GrDrawTarget* target,
182 GrDrawState* drawState, 466 GrDrawState* drawState,
183 GrColor color, 467 GrColor color,
184 const SkMatrix& viewMatrix, 468 const SkMatrix& viewMatrix,
185 const SkRect& rect, 469 const SkRect& rect,
186 const SkRect& devRect) { 470 const SkRect& devRect) {
187 GrDrawState::AutoRestoreEffects are(drawState); 471 GrDrawState::AutoRestoreEffects are(drawState);
472 if (NULL == fAAFillRectIndexBuffer) {
473 fAAFillRectIndexBuffer = fGpu->createInstancedIndexBuffer(gFillAARectIdx ,
474 kIndicesPerAAF illRect,
475 kNumAAFillRect sInIndexBuffer,
476 kVertsPerAAFil lRect);
477 }
188 478
189 SkMatrix localMatrix; 479 SkMatrix localMatrix;
190 if (!viewMatrix.invert(&localMatrix)) { 480 if (!viewMatrix.invert(&localMatrix)) {
191 SkDebugf("Cannot invert\n"); 481 SkDebugf("Cannot invert\n");
192 return; 482 return;
193 } 483 }
194 484
195 CoverageAttribType type; 485 AAFillRectBatch::Geometry geometry;
196 SkAutoTUnref<const GrGeometryProcessor> gp(create_rect_gp(*drawState, color, &type, 486 geometry.fRect = rect;
197 localMatrix)); 487 geometry.fViewMatrix = viewMatrix;
488 geometry.fLocalMatrix = localMatrix;
489 geometry.fDevRect = devRect;
490 geometry.fColor = color;
491 geometry.fIndexBuffer = fAAFillRectIndexBuffer;
198 492
199 size_t vertexStride = gp->getVertexStride(); 493 GrBatch* batch = AAFillRectBatch::Create(geometry);
bsalomon 2015/01/20 20:49:58 I do kind of thing=k the batches should be ref cou
200 GrDrawTarget::AutoReleaseGeometry geo(target, 8, vertexStride, 0); 494 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 } 495 }
317 496
318 void GrAARectRenderer::strokeAARect(GrDrawTarget* target, 497 void GrAARectRenderer::strokeAARect(GrDrawTarget* target,
319 GrDrawState* drawState, 498 GrDrawState* drawState,
320 GrColor color, 499 GrColor color,
321 const SkMatrix& viewMatrix, 500 const SkMatrix& viewMatrix,
322 const SkRect& rect, 501 const SkRect& rect,
323 const SkRect& devRect, 502 const SkRect& devRect,
324 const SkStrokeRec& stroke) { 503 const SkStrokeRec& stroke) {
325 SkVector devStrokeSize; 504 SkVector devStrokeSize;
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
379 // edge, while vertex number of inner edge is 4, the same as miter-stroke. 558 // edge, while vertex number of inner edge is 4, the same as miter-stroke.
380 if (!miterStroke) { 559 if (!miterStroke) {
381 devOutside.inset(0, ry); 560 devOutside.inset(0, ry);
382 devOutsideAssist.outset(0, ry); 561 devOutsideAssist.outset(0, ry);
383 } 562 }
384 563
385 this->geometryStrokeAARect(target, drawState, color, viewMatrix, devOutside, devOutsideAssist, 564 this->geometryStrokeAARect(target, drawState, color, viewMatrix, devOutside, devOutsideAssist,
386 devInside, miterStroke); 565 devInside, miterStroke);
387 } 566 }
388 567
568 static const GrGeometryProcessor* create_rect_gp(const GrDrawState& drawState,
569 GrColor color,
570 CoverageAttribType* type,
571 const SkMatrix& localMatrix) {
572 uint32_t flags = GrDefaultGeoProcFactory::kColor_GPType;
573 const GrGeometryProcessor* gp;
574 if (drawState.canTweakAlphaForCoverage()) {
575 gp = GrDefaultGeoProcFactory::Create(flags, color, SkMatrix::I(), localM atrix);
576 SkASSERT(gp->getVertexStride() == sizeof(GrDefaultGeoProcFactory::Positi onColorAttr));
577 *type = kUseColor_CoverageAttribType;
578 } else {
579 flags |= GrDefaultGeoProcFactory::kCoverage_GPType;
580 gp = GrDefaultGeoProcFactory::Create(flags, color, SkMatrix::I(), localM atrix,
581 GrColorIsOpaque(color));
582 SkASSERT(gp->getVertexStride()==sizeof(GrDefaultGeoProcFactory::Position ColorCoverageAttr));
583 *type = kUseCoverage_CoverageAttribType;
584 }
585 return gp;
586 }
587
588
389 void GrAARectRenderer::geometryStrokeAARect(GrDrawTarget* target, 589 void GrAARectRenderer::geometryStrokeAARect(GrDrawTarget* target,
390 GrDrawState* drawState, 590 GrDrawState* drawState,
391 GrColor color, 591 GrColor color,
392 const SkMatrix& viewMatrix, 592 const SkMatrix& viewMatrix,
393 const SkRect& devOutside, 593 const SkRect& devOutside,
394 const SkRect& devOutsideAssist, 594 const SkRect& devOutsideAssist,
395 const SkRect& devInside, 595 const SkRect& devInside,
396 bool miterStroke) { 596 bool miterStroke) {
397 GrDrawState::AutoRestoreEffects are(drawState); 597 GrDrawState::AutoRestoreEffects are(drawState);
398 598
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
541 741
542 if (devInside.isEmpty()) { 742 if (devInside.isEmpty()) {
543 this->fillAARect(target, drawState, color, viewMatrix, devOutside, 743 this->fillAARect(target, drawState, color, viewMatrix, devOutside,
544 devOutside); 744 devOutside);
545 return; 745 return;
546 } 746 }
547 747
548 this->geometryStrokeAARect(target, drawState, color, viewMatrix, devOutside, devOutsideAssist, 748 this->geometryStrokeAARect(target, drawState, color, viewMatrix, devOutside, devOutsideAssist,
549 devInside, true); 749 devInside, true);
550 } 750 }
OLDNEW
« no previous file with comments | « gyp/gpu.gypi ('k') | src/gpu/GrBatch.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698