OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2015 Google Inc. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license that can be | |
5 * found in the LICENSE file. | |
6 */ | |
7 | |
8 #include "GrStrokeRectBatch.h" | |
9 #include "GrBatchTest.h" | |
10 #include "GrBatchFlushState.h" | |
11 #include "SkRandom.h" | |
12 | |
13 GrStrokeRectBatch::GrStrokeRectBatch(const Geometry& geometry, bool snapToPixelC
enters) | |
14 : INHERITED(ClassID()) { | |
15 fBatch.fHairline = geometry.fStrokeWidth == 0; | |
16 | |
17 fGeoData.push_back(geometry); | |
18 | |
19 // setup bounds | |
20 fBounds = geometry.fRect; | |
21 SkScalar rad = SkScalarHalf(geometry.fStrokeWidth); | |
22 fBounds.outset(rad, rad); | |
23 geometry.fViewMatrix.mapRect(&fBounds); | |
24 | |
25 // If our caller snaps to pixel centers then we have to round out the bounds | |
26 if (snapToPixelCenters) { | |
27 fBounds.roundOut(); | |
28 } | |
29 } | |
30 | |
31 void GrStrokeRectBatch::initBatchTracker(const GrPipelineOptimizations& opt) { | |
32 // Handle any color overrides | |
33 if (!opt.readsColor()) { | |
34 fGeoData[0].fColor = GrColor_ILLEGAL; | |
35 } | |
36 opt.getOverrideColorIfSet(&fGeoData[0].fColor); | |
37 | |
38 // setup batch properties | |
39 fBatch.fColorIgnored = !opt.readsColor(); | |
40 fBatch.fColor = fGeoData[0].fColor; | |
41 fBatch.fUsesLocalCoords = opt.readsLocalCoords(); | |
42 fBatch.fCoverageIgnored = !opt.readsCoverage(); | |
43 } | |
44 | |
45 /* create a triangle strip that strokes the specified rect. There are 8 | |
46 unique vertices, but we repeat the last 2 to close up. Alternatively we | |
47 could use an indices array, and then only send 8 verts, but not sure that | |
48 would be faster. | |
49 */ | |
50 static void init_stroke_rect_strip(SkPoint verts[10], const SkRect& rect, SkScal
ar width) { | |
51 const SkScalar rad = SkScalarHalf(width); | |
52 // TODO we should be able to enable this assert, but we'd have to filter the
se draws | |
53 // this is a bug | |
54 //SkASSERT(rad < rect.width() / 2 && rad < rect.height() / 2); | |
55 | |
56 verts[0].set(rect.fLeft + rad, rect.fTop + rad); | |
57 verts[1].set(rect.fLeft - rad, rect.fTop - rad); | |
58 verts[2].set(rect.fRight - rad, rect.fTop + rad); | |
59 verts[3].set(rect.fRight + rad, rect.fTop - rad); | |
60 verts[4].set(rect.fRight - rad, rect.fBottom - rad); | |
61 verts[5].set(rect.fRight + rad, rect.fBottom + rad); | |
62 verts[6].set(rect.fLeft + rad, rect.fBottom - rad); | |
63 verts[7].set(rect.fLeft - rad, rect.fBottom + rad); | |
64 verts[8] = verts[0]; | |
65 verts[9] = verts[1]; | |
66 } | |
67 | |
68 void GrStrokeRectBatch::onPrepareDraws(Target* target) { | |
69 SkAutoTUnref<const GrGeometryProcessor> gp; | |
70 { | |
71 using namespace GrDefaultGeoProcFactory; | |
72 Color color(this->color()); | |
73 Coverage coverage(this->coverageIgnored() ? Coverage::kSolid_Type : | |
74 Coverage::kNone_Type); | |
75 LocalCoords localCoords(this->usesLocalCoords() ? LocalCoords::kUsePosit
ion_Type : | |
76 LocalCoords::kUnused
_Type); | |
77 gp.reset(GrDefaultGeoProcFactory::Create(color, coverage, localCoords, | |
78 this->viewMatrix())); | |
79 } | |
80 | |
81 target->initDraw(gp, this->pipeline()); | |
82 | |
83 size_t vertexStride = gp->getVertexStride(); | |
84 | |
85 SkASSERT(vertexStride == sizeof(GrDefaultGeoProcFactory::PositionAttr)); | |
86 | |
87 Geometry& args = fGeoData[0]; | |
88 | |
89 int vertexCount = kVertsPerHairlineRect; | |
90 if (args.fStrokeWidth > 0) { | |
91 vertexCount = kVertsPerStrokeRect; | |
92 } | |
93 | |
94 const GrVertexBuffer* vertexBuffer; | |
95 int firstVertex; | |
96 | |
97 void* verts = target->makeVertexSpace(vertexStride, vertexCount, &vertexBuff
er, &firstVertex); | |
98 | |
99 if (!verts) { | |
100 SkDebugf("Could not allocate vertices\n"); | |
101 return; | |
102 } | |
103 | |
104 SkPoint* vertex = reinterpret_cast<SkPoint*>(verts); | |
105 | |
106 GrPrimitiveType primType; | |
107 | |
108 if (args.fStrokeWidth > 0) {; | |
109 primType = kTriangleStrip_GrPrimitiveType; | |
110 args.fRect.sort(); | |
111 init_stroke_rect_strip(vertex, args.fRect, args.fStrokeWidth); | |
112 } else { | |
113 // hairline | |
114 primType = kLineStrip_GrPrimitiveType; | |
115 vertex[0].set(args.fRect.fLeft, args.fRect.fTop); | |
116 vertex[1].set(args.fRect.fRight, args.fRect.fTop); | |
117 vertex[2].set(args.fRect.fRight, args.fRect.fBottom); | |
118 vertex[3].set(args.fRect.fLeft, args.fRect.fBottom); | |
119 vertex[4].set(args.fRect.fLeft, args.fRect.fTop); | |
120 } | |
121 | |
122 GrVertices vertices; | |
123 vertices.init(primType, vertexBuffer, firstVertex, vertexCount); | |
124 target->draw(vertices); | |
125 } | |
126 | |
127 #ifdef GR_TEST_UTILS | |
128 | |
129 DRAW_BATCH_TEST_DEFINE(GrStrokeRectBatch) { | |
130 GrStrokeRectBatch::Geometry geometry; | |
131 geometry.fViewMatrix = GrTest::TestMatrix(random); | |
132 geometry.fColor = GrRandomColor(random); | |
133 geometry.fRect = GrTest::TestRect(random); | |
134 geometry.fStrokeWidth = random->nextBool() ? 0.0f : 1.0f; | |
135 | |
136 return GrStrokeRectBatch::Create(geometry, random->nextBool()); | |
137 } | |
138 | |
139 #endif | |
OLD | NEW |