| 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 "GrRectBatch.h" | |
| 9 | |
| 10 #include "GrBatch.h" | |
| 11 #include "GrBatchTarget.h" | |
| 12 #include "GrBatchTest.h" | |
| 13 #include "GrDefaultGeoProcFactory.h" | |
| 14 #include "GrPrimitiveProcessor.h" | |
| 15 | |
| 16 class RectBatch : public GrBatch { | |
| 17 public: | |
| 18 struct Geometry { | |
| 19 SkMatrix fViewMatrix; | |
| 20 SkRect fRect; | |
| 21 SkRect fLocalRect; | |
| 22 SkMatrix fLocalMatrix; | |
| 23 GrColor fColor; | |
| 24 bool fHasLocalRect; | |
| 25 bool fHasLocalMatrix; | |
| 26 }; | |
| 27 | |
| 28 static GrBatch* Create(const Geometry& geometry) { | |
| 29 return SkNEW_ARGS(RectBatch, (geometry)); | |
| 30 } | |
| 31 | |
| 32 const char* name() const override { return "RectBatch"; } | |
| 33 | |
| 34 void getInvariantOutputColor(GrInitInvariantOutput* out) const override { | |
| 35 // When this is called on a batch, there is only one geometry bundle | |
| 36 out->setKnownFourComponents(fGeoData[0].fColor); | |
| 37 } | |
| 38 | |
| 39 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override { | |
| 40 out->setKnownSingleComponent(0xff); | |
| 41 } | |
| 42 | |
| 43 void initBatchTracker(const GrPipelineInfo& init) override { | |
| 44 // Handle any color overrides | |
| 45 if (!init.readsColor()) { | |
| 46 fGeoData[0].fColor = GrColor_ILLEGAL; | |
| 47 } | |
| 48 init.getOverrideColorIfSet(&fGeoData[0].fColor); | |
| 49 | |
| 50 // setup batch properties | |
| 51 fBatch.fColorIgnored = !init.readsColor(); | |
| 52 fBatch.fColor = fGeoData[0].fColor; | |
| 53 fBatch.fUsesLocalCoords = init.readsLocalCoords(); | |
| 54 fBatch.fCoverageIgnored = !init.readsCoverage(); | |
| 55 } | |
| 56 | |
| 57 void generateGeometry(GrBatchTarget* batchTarget) override { | |
| 58 SkAutoTUnref<const GrGeometryProcessor> gp(this->createRectGP()); | |
| 59 if (!gp) { | |
| 60 SkDebugf("Could not create GrGeometryProcessor\n"); | |
| 61 return; | |
| 62 } | |
| 63 | |
| 64 batchTarget->initDraw(gp, this->pipeline()); | |
| 65 | |
| 66 int instanceCount = fGeoData.count(); | |
| 67 size_t vertexStride = gp->getVertexStride(); | |
| 68 SkASSERT(this->hasLocalRect() ? | |
| 69 vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorLo
calCoordAttr) : | |
| 70 vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorAt
tr)); | |
| 71 QuadHelper helper; | |
| 72 void* vertices = helper.init(batchTarget, vertexStride, instanceCount); | |
| 73 | |
| 74 if (!vertices) { | |
| 75 return; | |
| 76 } | |
| 77 | |
| 78 for (int i = 0; i < instanceCount; i++) { | |
| 79 const Geometry& geom = fGeoData[i]; | |
| 80 | |
| 81 intptr_t offset = reinterpret_cast<intptr_t>(vertices) + | |
| 82 kVerticesPerQuad * i * vertexStride; | |
| 83 SkPoint* positions = reinterpret_cast<SkPoint*>(offset); | |
| 84 | |
| 85 positions->setRectFan(geom.fRect.fLeft, geom.fRect.fTop, | |
| 86 geom.fRect.fRight, geom.fRect.fBottom, vertexS
tride); | |
| 87 geom.fViewMatrix.mapPointsWithStride(positions, vertexStride, kVerti
cesPerQuad); | |
| 88 | |
| 89 // TODO we should only do this if local coords are being read | |
| 90 if (geom.fHasLocalRect) { | |
| 91 static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor
); | |
| 92 SkPoint* coords = reinterpret_cast<SkPoint*>(offset + kLocalOffs
et); | |
| 93 coords->setRectFan(geom.fLocalRect.fLeft, geom.fLocalRect.fTop, | |
| 94 geom.fLocalRect.fRight, geom.fLocalRect.fBott
om, | |
| 95 vertexStride); | |
| 96 if (geom.fHasLocalMatrix) { | |
| 97 geom.fLocalMatrix.mapPointsWithStride(coords, vertexStride,
kVerticesPerQuad); | |
| 98 } | |
| 99 } | |
| 100 | |
| 101 static const int kColorOffset = sizeof(SkPoint); | |
| 102 GrColor* vertColor = reinterpret_cast<GrColor*>(offset + kColorOffse
t); | |
| 103 for (int j = 0; j < 4; ++j) { | |
| 104 *vertColor = geom.fColor; | |
| 105 vertColor = (GrColor*) ((intptr_t) vertColor + vertexStride); | |
| 106 } | |
| 107 } | |
| 108 | |
| 109 helper.issueDraw(batchTarget); | |
| 110 } | |
| 111 | |
| 112 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } | |
| 113 | |
| 114 private: | |
| 115 RectBatch(const Geometry& geometry) { | |
| 116 this->initClassID<RectBatch>(); | |
| 117 fGeoData.push_back(geometry); | |
| 118 | |
| 119 fBounds = geometry.fRect; | |
| 120 geometry.fViewMatrix.mapRect(&fBounds); | |
| 121 } | |
| 122 | |
| 123 GrColor color() const { return fBatch.fColor; } | |
| 124 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } | |
| 125 bool colorIgnored() const { return fBatch.fColorIgnored; } | |
| 126 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; } | |
| 127 const SkMatrix& localMatrix() const { return fGeoData[0].fLocalMatrix; } | |
| 128 bool hasLocalRect() const { return fGeoData[0].fHasLocalRect; } | |
| 129 bool hasLocalMatrix() const { return fGeoData[0].fHasLocalMatrix; } | |
| 130 bool coverageIgnored() const { return fBatch.fCoverageIgnored; } | |
| 131 | |
| 132 bool onCombineIfPossible(GrBatch* t) override { | |
| 133 if (!this->pipeline()->isEqual(*t->pipeline())) { | |
| 134 return false; | |
| 135 } | |
| 136 | |
| 137 RectBatch* that = t->cast<RectBatch>(); | |
| 138 | |
| 139 if (this->hasLocalRect() != that->hasLocalRect()) { | |
| 140 return false; | |
| 141 } | |
| 142 | |
| 143 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords()); | |
| 144 if (!this->hasLocalRect() && this->usesLocalCoords()) { | |
| 145 if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) { | |
| 146 return false; | |
| 147 } | |
| 148 | |
| 149 if (this->hasLocalMatrix() != that->hasLocalMatrix()) { | |
| 150 return false; | |
| 151 } | |
| 152 | |
| 153 if (this->hasLocalMatrix() && !this->localMatrix().cheapEqualTo(that
->localMatrix())) { | |
| 154 return false; | |
| 155 } | |
| 156 } | |
| 157 | |
| 158 if (this->color() != that->color()) { | |
| 159 fBatch.fColor = GrColor_ILLEGAL; | |
| 160 } | |
| 161 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin())
; | |
| 162 this->joinBounds(that->bounds()); | |
| 163 return true; | |
| 164 } | |
| 165 | |
| 166 /** We always use per-vertex colors so that rects can be batched across colo
r changes. Sometimes | |
| 167 we have explicit local coords and sometimes not. We *could* always prov
ide explicit local | |
| 168 coords and just duplicate the positions when the caller hasn't provided
a local coord rect, | |
| 169 but we haven't seen a use case which frequently switches between local r
ect and no local | |
| 170 rect draws. | |
| 171 | |
| 172 The color param is used to determine whether the opaque hint can be set
on the draw state. | |
| 173 The caller must populate the vertex colors itself. | |
| 174 | |
| 175 The vertex attrib order is always pos, color, [local coords]. | |
| 176 */ | |
| 177 const GrGeometryProcessor* createRectGP() { | |
| 178 using namespace GrDefaultGeoProcFactory; | |
| 179 Color color(Color::kAttribute_Type); | |
| 180 Coverage coverage(this->coverageIgnored() ? Coverage::kNone_Type : Cover
age::kSolid_Type); | |
| 181 | |
| 182 // if we have a local rect, then we apply the localMatrix directly to th
e localRect to | |
| 183 // generate vertex local coords | |
| 184 if (this->hasLocalRect()) { | |
| 185 LocalCoords localCoords(LocalCoords::kHasExplicit_Type); | |
| 186 return GrDefaultGeoProcFactory::Create(color, coverage, localCoords,
SkMatrix::I()); | |
| 187 } else { | |
| 188 LocalCoords localCoords(LocalCoords::kUsePosition_Type, | |
| 189 this->hasLocalMatrix() ? &this->localMatrix(
) : NULL); | |
| 190 return GrDefaultGeoProcFactory::CreateForDeviceSpace(color, coverage
, localCoords, | |
| 191 this->viewMatri
x()); | |
| 192 } | |
| 193 } | |
| 194 | |
| 195 | |
| 196 struct BatchTracker { | |
| 197 GrColor fColor; | |
| 198 bool fUsesLocalCoords; | |
| 199 bool fColorIgnored; | |
| 200 bool fCoverageIgnored; | |
| 201 }; | |
| 202 | |
| 203 BatchTracker fBatch; | |
| 204 SkSTArray<1, Geometry, true> fGeoData; | |
| 205 }; | |
| 206 | |
| 207 namespace GrRectBatch { | |
| 208 | |
| 209 GrBatch* Create(GrColor color, | |
| 210 const SkMatrix& viewMatrix, | |
| 211 const SkRect& rect, | |
| 212 const SkRect* localRect, | |
| 213 const SkMatrix* localMatrix) { | |
| 214 RectBatch::Geometry geometry; | |
| 215 geometry.fColor = color; | |
| 216 geometry.fViewMatrix = viewMatrix; | |
| 217 geometry.fRect = rect; | |
| 218 | |
| 219 if (localRect) { | |
| 220 geometry.fHasLocalRect = true; | |
| 221 geometry.fLocalRect = *localRect; | |
| 222 } else { | |
| 223 geometry.fHasLocalRect = false; | |
| 224 } | |
| 225 | |
| 226 if (localMatrix) { | |
| 227 geometry.fHasLocalMatrix = true; | |
| 228 geometry.fLocalMatrix = *localMatrix; | |
| 229 } else { | |
| 230 geometry.fHasLocalMatrix = false; | |
| 231 } | |
| 232 | |
| 233 return RectBatch::Create(geometry); | |
| 234 } | |
| 235 | |
| 236 }; | |
| 237 | |
| 238 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | |
| 239 | |
| 240 #ifdef GR_TEST_UTILS | |
| 241 | |
| 242 BATCH_TEST_DEFINE(RectBatch) { | |
| 243 GrColor color = GrRandomColor(random); | |
| 244 | |
| 245 SkRect rect = GrTest::TestRect(random); | |
| 246 SkRect localRect; | |
| 247 bool hasLocalRect = random->nextBool(); | |
| 248 bool hasLocalMatrix = random->nextBool(); | |
| 249 | |
| 250 SkMatrix viewMatrix; | |
| 251 SkMatrix localMatrix; | |
| 252 if (hasLocalRect) { | |
| 253 viewMatrix = GrTest::TestMatrixInvertible(random); | |
| 254 localRect = GrTest::TestRect(random); | |
| 255 } else { | |
| 256 viewMatrix = GrTest::TestMatrix(random); | |
| 257 } | |
| 258 | |
| 259 if (hasLocalMatrix) { | |
| 260 localMatrix = GrTest::TestMatrix(random); | |
| 261 } | |
| 262 | |
| 263 return GrRectBatch::Create(color, viewMatrix, rect, | |
| 264 hasLocalRect ? &localRect : NULL, | |
| 265 hasLocalMatrix ? &localMatrix : NULL); | |
| 266 } | |
| 267 | |
| 268 #endif | |
| OLD | NEW |