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 #ifndef GrStrokeRectBatch_DEFINED |
| 9 #define GrStrokeRectBatch_DEFINED |
| 10 |
| 11 #include "GrBatch.h" |
| 12 #include "GrColor.h" |
| 13 #include "GrDefaultGeoProcFactory.h" |
| 14 |
| 15 class GrStrokeRectBatch : public GrBatch { |
| 16 public: |
| 17 struct Geometry { |
| 18 GrColor fColor; |
| 19 SkMatrix fViewMatrix; |
| 20 SkRect fRect; |
| 21 SkScalar fStrokeWidth; |
| 22 }; |
| 23 |
| 24 static GrBatch* Create(const Geometry& geometry, bool snapToPixelCenters) { |
| 25 return SkNEW_ARGS(GrStrokeRectBatch, (geometry, snapToPixelCenters)); |
| 26 } |
| 27 |
| 28 const char* name() const override { return "GrStrokeRectBatch"; } |
| 29 |
| 30 void getInvariantOutputColor(GrInitInvariantOutput* out) const override { |
| 31 // When this is called on a batch, there is only one geometry bundle |
| 32 out->setKnownFourComponents(fGeoData[0].fColor); |
| 33 } |
| 34 |
| 35 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override { |
| 36 out->setKnownSingleComponent(0xff); |
| 37 } |
| 38 |
| 39 void initBatchTracker(const GrPipelineInfo& init) override { |
| 40 // Handle any color overrides |
| 41 if (!init.readsColor()) { |
| 42 fGeoData[0].fColor = GrColor_ILLEGAL; |
| 43 } |
| 44 init.getOverrideColorIfSet(&fGeoData[0].fColor); |
| 45 |
| 46 // setup batch properties |
| 47 fBatch.fColorIgnored = !init.readsColor(); |
| 48 fBatch.fColor = fGeoData[0].fColor; |
| 49 fBatch.fUsesLocalCoords = init.readsLocalCoords(); |
| 50 fBatch.fCoverageIgnored = !init.readsCoverage(); |
| 51 } |
| 52 |
| 53 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline
) override { |
| 54 SkAutoTUnref<const GrGeometryProcessor> gp; |
| 55 { |
| 56 using namespace GrDefaultGeoProcFactory; |
| 57 Color color(this->color()); |
| 58 Coverage coverage(this->coverageIgnored() ? Coverage::kSolid_Type : |
| 59 Coverage::kNone_Type); |
| 60 LocalCoords localCoords(this->usesLocalCoords() ? LocalCoords::kUseP
osition_Type : |
| 61 LocalCoords::kUnus
ed_Type); |
| 62 gp.reset(GrDefaultGeoProcFactory::Create(color, coverage, localCoord
s, |
| 63 this->viewMatrix())); |
| 64 } |
| 65 |
| 66 batchTarget->initDraw(gp, pipeline); |
| 67 |
| 68 size_t vertexStride = gp->getVertexStride(); |
| 69 |
| 70 SkASSERT(vertexStride == sizeof(GrDefaultGeoProcFactory::PositionAttr)); |
| 71 |
| 72 Geometry& args = fGeoData[0]; |
| 73 |
| 74 int vertexCount = kVertsPerHairlineRect; |
| 75 if (args.fStrokeWidth > 0) { |
| 76 vertexCount = kVertsPerStrokeRect; |
| 77 } |
| 78 |
| 79 const GrVertexBuffer* vertexBuffer; |
| 80 int firstVertex; |
| 81 |
| 82 void* verts = batchTarget->makeVertSpace(vertexStride, vertexCount, |
| 83 &vertexBuffer, &firstVertex); |
| 84 |
| 85 if (!verts) { |
| 86 SkDebugf("Could not allocate vertices\n"); |
| 87 return; |
| 88 } |
| 89 |
| 90 SkPoint* vertex = reinterpret_cast<SkPoint*>(verts); |
| 91 |
| 92 GrPrimitiveType primType; |
| 93 |
| 94 if (args.fStrokeWidth > 0) {; |
| 95 primType = kTriangleStrip_GrPrimitiveType; |
| 96 args.fRect.sort(); |
| 97 this->setStrokeRectStrip(vertex, args.fRect, args.fStrokeWidth); |
| 98 } else { |
| 99 // hairline |
| 100 primType = kLineStrip_GrPrimitiveType; |
| 101 vertex[0].set(args.fRect.fLeft, args.fRect.fTop); |
| 102 vertex[1].set(args.fRect.fRight, args.fRect.fTop); |
| 103 vertex[2].set(args.fRect.fRight, args.fRect.fBottom); |
| 104 vertex[3].set(args.fRect.fLeft, args.fRect.fBottom); |
| 105 vertex[4].set(args.fRect.fLeft, args.fRect.fTop); |
| 106 } |
| 107 |
| 108 GrVertices vertices; |
| 109 vertices.init(primType, vertexBuffer, firstVertex, vertexCount); |
| 110 batchTarget->draw(vertices); |
| 111 } |
| 112 |
| 113 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } |
| 114 |
| 115 private: |
| 116 GrStrokeRectBatch(const Geometry& geometry, bool snapToPixelCenters) { |
| 117 this->initClassID<GrStrokeRectBatch>(); |
| 118 |
| 119 fBatch.fHairline = geometry.fStrokeWidth == 0; |
| 120 |
| 121 fGeoData.push_back(geometry); |
| 122 |
| 123 // setup bounds |
| 124 fBounds = geometry.fRect; |
| 125 SkScalar rad = SkScalarHalf(geometry.fStrokeWidth); |
| 126 fBounds.outset(rad, rad); |
| 127 geometry.fViewMatrix.mapRect(&fBounds); |
| 128 |
| 129 // If our caller snaps to pixel centers then we have to round out the bo
unds |
| 130 if (snapToPixelCenters) { |
| 131 fBounds.roundOut(); |
| 132 } |
| 133 } |
| 134 |
| 135 /* create a triangle strip that strokes the specified rect. There are 8 |
| 136 unique vertices, but we repeat the last 2 to close up. Alternatively we |
| 137 could use an indices array, and then only send 8 verts, but not sure that |
| 138 would be faster. |
| 139 */ |
| 140 void setStrokeRectStrip(SkPoint verts[10], const SkRect& rect, SkScalar widt
h) { |
| 141 const SkScalar rad = SkScalarHalf(width); |
| 142 // TODO we should be able to enable this assert, but we'd have to filter
these draws |
| 143 // this is a bug |
| 144 //SkASSERT(rad < rect.width() / 2 && rad < rect.height() / 2); |
| 145 |
| 146 verts[0].set(rect.fLeft + rad, rect.fTop + rad); |
| 147 verts[1].set(rect.fLeft - rad, rect.fTop - rad); |
| 148 verts[2].set(rect.fRight - rad, rect.fTop + rad); |
| 149 verts[3].set(rect.fRight + rad, rect.fTop - rad); |
| 150 verts[4].set(rect.fRight - rad, rect.fBottom - rad); |
| 151 verts[5].set(rect.fRight + rad, rect.fBottom + rad); |
| 152 verts[6].set(rect.fLeft + rad, rect.fBottom - rad); |
| 153 verts[7].set(rect.fLeft - rad, rect.fBottom + rad); |
| 154 verts[8] = verts[0]; |
| 155 verts[9] = verts[1]; |
| 156 } |
| 157 |
| 158 |
| 159 GrColor color() const { return fBatch.fColor; } |
| 160 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } |
| 161 bool colorIgnored() const { return fBatch.fColorIgnored; } |
| 162 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; } |
| 163 bool hairline() const { return fBatch.fHairline; } |
| 164 bool coverageIgnored() const { return fBatch.fCoverageIgnored; } |
| 165 |
| 166 bool onCombineIfPossible(GrBatch* t) override { |
| 167 //if (!this->pipeline()->isEqual(*t->pipeline())) { |
| 168 // return false; |
| 169 //} |
| 170 // GrStrokeRectBatch* that = t->cast<StrokeRectBatch>(); |
| 171 |
| 172 // NonAA stroke rects cannot batch right now |
| 173 // TODO make these batchable |
| 174 return false; |
| 175 } |
| 176 |
| 177 struct BatchTracker { |
| 178 GrColor fColor; |
| 179 bool fUsesLocalCoords; |
| 180 bool fColorIgnored; |
| 181 bool fCoverageIgnored; |
| 182 bool fHairline; |
| 183 }; |
| 184 |
| 185 const static int kVertsPerHairlineRect = 5; |
| 186 const static int kVertsPerStrokeRect = 10; |
| 187 |
| 188 BatchTracker fBatch; |
| 189 SkSTArray<1, Geometry, true> fGeoData; |
| 190 }; |
| 191 |
| 192 #endif |
OLD | NEW |