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 "GrNonAAStrokeRectBatch.h" |
| 9 |
| 10 #include "GrBatchTest.h" |
| 11 #include "GrBatchFlushState.h" |
| 12 #include "GrColor.h" |
| 13 #include "GrDefaultGeoProcFactory.h" |
| 14 #include "GrVertexBatch.h" |
| 15 #include "SkRandom.h" |
| 16 |
| 17 /* create a triangle strip that strokes the specified rect. There are 8 |
| 18 unique vertices, but we repeat the last 2 to close up. Alternatively we |
| 19 could use an indices array, and then only send 8 verts, but not sure that |
| 20 would be faster. |
| 21 */ |
| 22 static void init_stroke_rect_strip(SkPoint verts[10], const SkRect& rect, SkScal
ar width) { |
| 23 const SkScalar rad = SkScalarHalf(width); |
| 24 // TODO we should be able to enable this assert, but we'd have to filter the
se draws |
| 25 // this is a bug |
| 26 //SkASSERT(rad < rect.width() / 2 && rad < rect.height() / 2); |
| 27 |
| 28 verts[0].set(rect.fLeft + rad, rect.fTop + rad); |
| 29 verts[1].set(rect.fLeft - rad, rect.fTop - rad); |
| 30 verts[2].set(rect.fRight - rad, rect.fTop + rad); |
| 31 verts[3].set(rect.fRight + rad, rect.fTop - rad); |
| 32 verts[4].set(rect.fRight - rad, rect.fBottom - rad); |
| 33 verts[5].set(rect.fRight + rad, rect.fBottom + rad); |
| 34 verts[6].set(rect.fLeft + rad, rect.fBottom - rad); |
| 35 verts[7].set(rect.fLeft - rad, rect.fBottom + rad); |
| 36 verts[8] = verts[0]; |
| 37 verts[9] = verts[1]; |
| 38 } |
| 39 |
| 40 class NonAAStrokeRectBatch : public GrVertexBatch { |
| 41 public: |
| 42 DEFINE_BATCH_CLASS_ID |
| 43 |
| 44 struct Geometry { |
| 45 SkMatrix fViewMatrix; |
| 46 SkRect fRect; |
| 47 SkScalar fStrokeWidth; |
| 48 GrColor fColor; |
| 49 }; |
| 50 |
| 51 static GrDrawBatch* Create(GrColor color, const SkMatrix& viewMatrix, const
SkRect& rect, |
| 52 SkScalar strokeWidth, bool snapToPixelCenters) { |
| 53 return new NonAAStrokeRectBatch(color, viewMatrix, rect, strokeWidth, sn
apToPixelCenters); |
| 54 } |
| 55 |
| 56 const char* name() const override { return "GrStrokeRectBatch"; } |
| 57 |
| 58 void getInvariantOutputColor(GrInitInvariantOutput* out) const override { |
| 59 // When this is called on a batch, there is only one geometry bundle |
| 60 out->setKnownFourComponents(fGeoData[0].fColor); |
| 61 } |
| 62 |
| 63 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override { |
| 64 out->setKnownSingleComponent(0xff); |
| 65 } |
| 66 |
| 67 private: |
| 68 void onPrepareDraws(Target* target) override { |
| 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::kUseP
osition_Type : |
| 76 LocalCoords::kUnus
ed_Type); |
| 77 gp.reset(GrDefaultGeoProcFactory::Create(color, coverage, localCoord
s, |
| 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, &vertex
Buffer, |
| 98 &firstVertex); |
| 99 |
| 100 if (!verts) { |
| 101 SkDebugf("Could not allocate vertices\n"); |
| 102 return; |
| 103 } |
| 104 |
| 105 SkPoint* vertex = reinterpret_cast<SkPoint*>(verts); |
| 106 |
| 107 GrPrimitiveType primType; |
| 108 |
| 109 if (args.fStrokeWidth > 0) {; |
| 110 primType = kTriangleStrip_GrPrimitiveType; |
| 111 args.fRect.sort(); |
| 112 init_stroke_rect_strip(vertex, args.fRect, args.fStrokeWidth); |
| 113 } else { |
| 114 // hairline |
| 115 primType = kLineStrip_GrPrimitiveType; |
| 116 vertex[0].set(args.fRect.fLeft, args.fRect.fTop); |
| 117 vertex[1].set(args.fRect.fRight, args.fRect.fTop); |
| 118 vertex[2].set(args.fRect.fRight, args.fRect.fBottom); |
| 119 vertex[3].set(args.fRect.fLeft, args.fRect.fBottom); |
| 120 vertex[4].set(args.fRect.fLeft, args.fRect.fTop); |
| 121 } |
| 122 |
| 123 GrVertices vertices; |
| 124 vertices.init(primType, vertexBuffer, firstVertex, vertexCount); |
| 125 target->draw(vertices); |
| 126 } |
| 127 |
| 128 void initBatchTracker(const GrPipelineOptimizations& opt) override { |
| 129 // Handle any color overrides |
| 130 if (!opt.readsColor()) { |
| 131 fGeoData[0].fColor = GrColor_ILLEGAL; |
| 132 } |
| 133 opt.getOverrideColorIfSet(&fGeoData[0].fColor); |
| 134 |
| 135 // setup batch properties |
| 136 fBatch.fColorIgnored = !opt.readsColor(); |
| 137 fBatch.fColor = fGeoData[0].fColor; |
| 138 fBatch.fUsesLocalCoords = opt.readsLocalCoords(); |
| 139 fBatch.fCoverageIgnored = !opt.readsCoverage(); |
| 140 } |
| 141 |
| 142 NonAAStrokeRectBatch(GrColor color, const SkMatrix& viewMatrix, const SkRect
& rect, |
| 143 SkScalar strokeWidth, bool snapToPixelCenters) |
| 144 : INHERITED(ClassID()) { |
| 145 Geometry& geometry = fGeoData.push_back(); |
| 146 geometry.fViewMatrix = viewMatrix; |
| 147 geometry.fRect = rect; |
| 148 geometry.fStrokeWidth = strokeWidth; |
| 149 geometry.fColor = color; |
| 150 |
| 151 fBatch.fHairline = geometry.fStrokeWidth == 0; |
| 152 |
| 153 fGeoData.push_back(geometry); |
| 154 |
| 155 // setup bounds |
| 156 fBounds = geometry.fRect; |
| 157 SkScalar rad = SkScalarHalf(geometry.fStrokeWidth); |
| 158 fBounds.outset(rad, rad); |
| 159 geometry.fViewMatrix.mapRect(&fBounds); |
| 160 |
| 161 // If our caller snaps to pixel centers then we have to round out the bo
unds |
| 162 if (snapToPixelCenters) { |
| 163 fBounds.roundOut(); |
| 164 } |
| 165 } |
| 166 |
| 167 GrColor color() const { return fBatch.fColor; } |
| 168 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } |
| 169 bool colorIgnored() const { return fBatch.fColorIgnored; } |
| 170 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; } |
| 171 bool hairline() const { return fBatch.fHairline; } |
| 172 bool coverageIgnored() const { return fBatch.fCoverageIgnored; } |
| 173 |
| 174 bool onCombineIfPossible(GrBatch* t, const GrCaps&) override { |
| 175 // if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *t->pi
peline(), |
| 176 // t->bounds(), caps)) { |
| 177 // return false; |
| 178 // } |
| 179 // GrStrokeRectBatch* that = t->cast<StrokeRectBatch>(); |
| 180 |
| 181 // NonAA stroke rects cannot batch right now |
| 182 // TODO make these batchable |
| 183 return false; |
| 184 } |
| 185 |
| 186 struct BatchTracker { |
| 187 GrColor fColor; |
| 188 bool fUsesLocalCoords; |
| 189 bool fColorIgnored; |
| 190 bool fCoverageIgnored; |
| 191 bool fHairline; |
| 192 }; |
| 193 |
| 194 const static int kVertsPerHairlineRect = 5; |
| 195 const static int kVertsPerStrokeRect = 10; |
| 196 |
| 197 BatchTracker fBatch; |
| 198 SkSTArray<1, Geometry, true> fGeoData; |
| 199 |
| 200 typedef GrVertexBatch INHERITED; |
| 201 }; |
| 202 |
| 203 namespace GrNonAAStrokeRectBatch { |
| 204 |
| 205 GrDrawBatch* Create(GrColor color, |
| 206 const SkMatrix& viewMatrix, |
| 207 const SkRect& rect, |
| 208 SkScalar strokeWidth, |
| 209 bool snapToPixelCenters) { |
| 210 return NonAAStrokeRectBatch::Create(color, viewMatrix, rect, strokeWidth, sn
apToPixelCenters); |
| 211 } |
| 212 |
| 213 }; |
| 214 |
| 215 #ifdef GR_TEST_UTILS |
| 216 |
| 217 DRAW_BATCH_TEST_DEFINE(NonAAStrokeRectBatch) { |
| 218 SkMatrix viewMatrix = GrTest::TestMatrix(random); |
| 219 GrColor color = GrRandomColor(random); |
| 220 SkRect rect = GrTest::TestRect(random); |
| 221 SkScalar strokeWidth = random->nextBool() ? 0.0f : 1.0f; |
| 222 |
| 223 return NonAAStrokeRectBatch::Create(color, viewMatrix, rect, strokeWidth, ra
ndom->nextBool()); |
| 224 } |
| 225 |
| 226 #endif |
OLD | NEW |