OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 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 "GrStrokeRectBatch.h" | 8 #include "GrStrokeRectBatch.h" |
9 #include "GrBatchTest.h" | 9 #include "GrBatchTest.h" |
10 #include "SkRandom.h" | 10 #include "SkRandom.h" |
11 | 11 |
| 12 GrStrokeRectBatch::GrStrokeRectBatch(const Geometry& geometry, bool snapToPixelC
enters) { |
| 13 this->initClassID<GrStrokeRectBatch>(); |
| 14 |
| 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 GrPipelineInfo& init) { |
| 32 // Handle any color overrides |
| 33 if (!init.readsColor()) { |
| 34 fGeoData[0].fColor = GrColor_ILLEGAL; |
| 35 } |
| 36 init.getOverrideColorIfSet(&fGeoData[0].fColor); |
| 37 |
| 38 // setup batch properties |
| 39 fBatch.fColorIgnored = !init.readsColor(); |
| 40 fBatch.fColor = fGeoData[0].fColor; |
| 41 fBatch.fUsesLocalCoords = init.readsLocalCoords(); |
| 42 fBatch.fCoverageIgnored = !init.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 |
| 69 void GrStrokeRectBatch::generateGeometry(GrBatchTarget* batchTarget, const GrPip
eline* pipeline) { |
| 70 SkAutoTUnref<const GrGeometryProcessor> gp; |
| 71 { |
| 72 using namespace GrDefaultGeoProcFactory; |
| 73 Color color(this->color()); |
| 74 Coverage coverage(this->coverageIgnored() ? Coverage::kSolid_Type : |
| 75 Coverage::kNone_Type); |
| 76 LocalCoords localCoords(this->usesLocalCoords() ? LocalCoords::kUsePosit
ion_Type : |
| 77 LocalCoords::kUnused
_Type); |
| 78 gp.reset(GrDefaultGeoProcFactory::Create(color, coverage, localCoords, |
| 79 this->viewMatrix())); |
| 80 } |
| 81 |
| 82 batchTarget->initDraw(gp, pipeline); |
| 83 |
| 84 size_t vertexStride = gp->getVertexStride(); |
| 85 |
| 86 SkASSERT(vertexStride == sizeof(GrDefaultGeoProcFactory::PositionAttr)); |
| 87 |
| 88 Geometry& args = fGeoData[0]; |
| 89 |
| 90 int vertexCount = kVertsPerHairlineRect; |
| 91 if (args.fStrokeWidth > 0) { |
| 92 vertexCount = kVertsPerStrokeRect; |
| 93 } |
| 94 |
| 95 const GrVertexBuffer* vertexBuffer; |
| 96 int firstVertex; |
| 97 |
| 98 void* verts = batchTarget->makeVertSpace(vertexStride, vertexCount, |
| 99 &vertexBuffer, &firstVertex); |
| 100 |
| 101 if (!verts) { |
| 102 SkDebugf("Could not allocate vertices\n"); |
| 103 return; |
| 104 } |
| 105 |
| 106 SkPoint* vertex = reinterpret_cast<SkPoint*>(verts); |
| 107 |
| 108 GrPrimitiveType primType; |
| 109 |
| 110 if (args.fStrokeWidth > 0) {; |
| 111 primType = kTriangleStrip_GrPrimitiveType; |
| 112 args.fRect.sort(); |
| 113 init_stroke_rect_strip(vertex, args.fRect, args.fStrokeWidth); |
| 114 } else { |
| 115 // hairline |
| 116 primType = kLineStrip_GrPrimitiveType; |
| 117 vertex[0].set(args.fRect.fLeft, args.fRect.fTop); |
| 118 vertex[1].set(args.fRect.fRight, args.fRect.fTop); |
| 119 vertex[2].set(args.fRect.fRight, args.fRect.fBottom); |
| 120 vertex[3].set(args.fRect.fLeft, args.fRect.fBottom); |
| 121 vertex[4].set(args.fRect.fLeft, args.fRect.fTop); |
| 122 } |
| 123 |
| 124 GrVertices vertices; |
| 125 vertices.init(primType, vertexBuffer, firstVertex, vertexCount); |
| 126 batchTarget->draw(vertices); |
| 127 } |
| 128 |
12 #ifdef GR_TEST_UTILS | 129 #ifdef GR_TEST_UTILS |
13 | 130 |
14 BATCH_TEST_DEFINE(GrStrokeRectBatch) { | 131 BATCH_TEST_DEFINE(GrStrokeRectBatch) { |
15 GrStrokeRectBatch::Geometry geometry; | 132 GrStrokeRectBatch::Geometry geometry; |
16 geometry.fViewMatrix = GrTest::TestMatrix(random); | 133 geometry.fViewMatrix = GrTest::TestMatrix(random); |
17 geometry.fColor = GrRandomColor(random); | 134 geometry.fColor = GrRandomColor(random); |
18 geometry.fRect = GrTest::TestRect(random); | 135 geometry.fRect = GrTest::TestRect(random); |
19 geometry.fStrokeWidth = random->nextBool() ? 0.0f : 1.0f; | 136 geometry.fStrokeWidth = random->nextBool() ? 0.0f : 1.0f; |
20 | 137 |
21 return GrStrokeRectBatch::Create(geometry, random->nextBool()); | 138 return GrStrokeRectBatch::Create(geometry, random->nextBool()); |
22 } | 139 } |
23 | 140 |
24 #endif | 141 #endif |
OLD | NEW |