| 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 "GrNonAAFillRectBatch.h" | 8 #include "GrNonAAFillRectBatch.h" |
| 9 | 9 |
| 10 #include "GrBatchFlushState.h" | 10 #include "GrBatchFlushState.h" |
| 11 #include "GrColor.h" | 11 #include "GrColor.h" |
| 12 #include "GrDefaultGeoProcFactory.h" | 12 #include "GrDefaultGeoProcFactory.h" |
| 13 #include "GrPrimitiveProcessor.h" | 13 #include "GrPrimitiveProcessor.h" |
| 14 #include "GrResourceProvider.h" | 14 #include "GrResourceProvider.h" |
| 15 #include "GrQuad.h" | 15 #include "GrQuad.h" |
| 16 #include "GrVertexBatch.h" | 16 #include "GrVertexBatch.h" |
| 17 | 17 |
| 18 static const int kVertsPerInstance = 4; | 18 static const int kVertsPerInstance = 4; |
| 19 static const int kIndicesPerInstance = 6; | 19 static const int kIndicesPerInstance = 6; |
| 20 | 20 |
| 21 /** We always use per-vertex colors so that rects can be batched across color ch
anges. Sometimes | 21 /** We always use per-vertex colors so that rects can be batched across color ch
anges. Sometimes |
| 22 we have explicit local coords and sometimes not. We *could* always provide
explicit local | 22 we have explicit local coords and sometimes not. We *could* always provide
explicit local |
| 23 coords and just duplicate the positions when the caller hasn't provided a lo
cal coord rect, | 23 coords and just duplicate the positions when the caller hasn't provided a lo
cal coord rect, |
| 24 but we haven't seen a use case which frequently switches between local rect
and no local | 24 but we haven't seen a use case which frequently switches between local rect
and no local |
| 25 rect draws. | 25 rect draws. |
| 26 | 26 |
| 27 The vertex attrib order is always pos, color, [local coords]. | 27 The vertex attrib order is always pos, color, [local coords]. |
| 28 */ | 28 */ |
| 29 static sk_sp<GrGeometryProcessor> make_persp_gp(const SkMatrix& viewMatrix, | |
| 30 bool readsCoverage, | |
| 31 bool hasExplicitLocalCoords, | |
| 32 const SkMatrix* localMatrix) { | |
| 33 SkASSERT(viewMatrix.hasPerspective() || (localMatrix && localMatrix->hasPers
pective())); | |
| 34 | |
| 35 using namespace GrDefaultGeoProcFactory; | |
| 36 Color color(Color::kAttribute_Type); | |
| 37 Coverage coverage(readsCoverage ? Coverage::kSolid_Type : Coverage::kNone_Ty
pe); | |
| 38 | |
| 39 // If we have perspective on the viewMatrix then we won't map on the CPU, no
r will we map | |
| 40 // the local rect on the cpu (in case the localMatrix also has perspective). | |
| 41 // Otherwise, if we have a local rect, then we apply the localMatrix directl
y to the localRect | |
| 42 // to generate vertex local coords | |
| 43 if (viewMatrix.hasPerspective()) { | |
| 44 LocalCoords localCoords(hasExplicitLocalCoords ? LocalCoords::kHasExplic
it_Type : | |
| 45 LocalCoords::kUsePositi
on_Type, | |
| 46 localMatrix); | |
| 47 return GrDefaultGeoProcFactory::Make(color, coverage, localCoords, viewM
atrix); | |
| 48 } else if (hasExplicitLocalCoords) { | |
| 49 LocalCoords localCoords(LocalCoords::kHasExplicit_Type, localMatrix); | |
| 50 return GrDefaultGeoProcFactory::Make(color, coverage, localCoords, SkMat
rix::I()); | |
| 51 } else { | |
| 52 LocalCoords localCoords(LocalCoords::kUsePosition_Type, localMatrix); | |
| 53 return GrDefaultGeoProcFactory::MakeForDeviceSpace(color, coverage, loca
lCoords, | |
| 54 viewMatrix); | |
| 55 } | |
| 56 } | |
| 57 | |
| 58 static sk_sp<GrGeometryProcessor> make_gp(bool readsCoverage) { | 29 static sk_sp<GrGeometryProcessor> make_gp(bool readsCoverage) { |
| 59 using namespace GrDefaultGeoProcFactory; | 30 using namespace GrDefaultGeoProcFactory; |
| 60 Color color(Color::kAttribute_Type); | 31 Color color(Color::kAttribute_Type); |
| 61 Coverage coverage(readsCoverage ? Coverage::kSolid_Type : Coverage::kNone_Ty
pe); | 32 Coverage coverage(readsCoverage ? Coverage::kSolid_Type : Coverage::kNone_Ty
pe); |
| 62 | 33 |
| 63 LocalCoords localCoords(LocalCoords::kHasExplicit_Type); | 34 LocalCoords localCoords(LocalCoords::kHasExplicit_Type); |
| 64 return GrDefaultGeoProcFactory::Make(color, coverage, localCoords, SkMatrix:
:I()); | 35 return GrDefaultGeoProcFactory::Make(color, coverage, localCoords, SkMatrix:
:I()); |
| 65 } | 36 } |
| 66 | 37 |
| 67 static void tesselate(intptr_t vertices, | 38 static void tesselate(intptr_t vertices, |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 SkRect fRect; | 180 SkRect fRect; |
| 210 GrQuad fLocalQuad; | 181 GrQuad fLocalQuad; |
| 211 }; | 182 }; |
| 212 | 183 |
| 213 GrXPOverridesForBatch fOverrides; | 184 GrXPOverridesForBatch fOverrides; |
| 214 SkSTArray<1, RectInfo, true> fRects; | 185 SkSTArray<1, RectInfo, true> fRects; |
| 215 | 186 |
| 216 typedef GrVertexBatch INHERITED; | 187 typedef GrVertexBatch INHERITED; |
| 217 }; | 188 }; |
| 218 | 189 |
| 219 // We handle perspective in the local matrix or viewmatrix with special batches | |
| 220 class NonAAFillRectPerspectiveBatch : public GrVertexBatch { | |
| 221 public: | |
| 222 DEFINE_BATCH_CLASS_ID | |
| 223 | |
| 224 NonAAFillRectPerspectiveBatch(GrColor color, const SkMatrix& viewMatrix, con
st SkRect& rect, | |
| 225 const SkRect* localRect, const SkMatrix* local
Matrix) | |
| 226 : INHERITED(ClassID()) | |
| 227 , fViewMatrix(viewMatrix) { | |
| 228 SkASSERT(viewMatrix.hasPerspective() || (localMatrix && | |
| 229 localMatrix->hasPerspective()))
; | |
| 230 RectInfo& info = fRects.push_back(); | |
| 231 info.fColor = color; | |
| 232 info.fRect = rect; | |
| 233 fHasLocalRect = SkToBool(localRect); | |
| 234 fHasLocalMatrix = SkToBool(localMatrix); | |
| 235 if (fHasLocalMatrix) { | |
| 236 fLocalMatrix = *localMatrix; | |
| 237 } | |
| 238 if (fHasLocalRect) { | |
| 239 info.fLocalRect = *localRect; | |
| 240 } | |
| 241 viewMatrix.mapRect(&fBounds, rect); | |
| 242 } | |
| 243 | |
| 244 const char* name() const override { return "NonAAFillRectPerspectiveBatch";
} | |
| 245 | |
| 246 SkString dumpInfo() const override { | |
| 247 SkString str; | |
| 248 str.appendf("# batched: %d\n", fRects.count()); | |
| 249 for (int i = 0; i < fRects.count(); ++i) { | |
| 250 const RectInfo& geo = fRects[0]; | |
| 251 str.appendf("%d: Color: 0x%08x, Rect [L: %.2f, T: %.2f, R: %.2f, B:
%.2f]\n", | |
| 252 i, geo.fColor, | |
| 253 geo.fRect.fLeft, geo.fRect.fTop, geo.fRect.fRight, geo.f
Rect.fBottom); | |
| 254 } | |
| 255 str.append(INHERITED::dumpInfo()); | |
| 256 return str; | |
| 257 } | |
| 258 | |
| 259 void computePipelineOptimizations(GrInitInvariantOutput* color, | |
| 260 GrInitInvariantOutput* coverage, | |
| 261 GrBatchToXPOverrides* overrides) const ove
rride { | |
| 262 // When this is called on a batch, there is only one geometry bundle | |
| 263 color->setKnownFourComponents(fRects[0].fColor); | |
| 264 coverage->setKnownSingleComponent(0xff); | |
| 265 } | |
| 266 | |
| 267 void initBatchTracker(const GrXPOverridesForBatch& overrides) override { | |
| 268 overrides.getOverrideColorIfSet(&fRects[0].fColor); | |
| 269 fOverrides = overrides; | |
| 270 } | |
| 271 | |
| 272 private: | |
| 273 NonAAFillRectPerspectiveBatch() : INHERITED(ClassID()) {} | |
| 274 | |
| 275 void onPrepareDraws(Target* target) const override { | |
| 276 sk_sp<GrGeometryProcessor> gp = make_persp_gp(fViewMatrix, | |
| 277 fOverrides.readsCoverage()
, | |
| 278 fHasLocalRect, | |
| 279 fHasLocalMatrix ? &fLocalM
atrix : nullptr); | |
| 280 if (!gp) { | |
| 281 SkDebugf("Couldn't create GrGeometryProcessor\n"); | |
| 282 return; | |
| 283 } | |
| 284 SkASSERT(fHasLocalRect | |
| 285 ? gp->getVertexStride() == | |
| 286 sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoord
Attr) | |
| 287 : gp->getVertexStride() == sizeof(GrDefaultGeoProcFactory::
PositionColorAttr)); | |
| 288 | |
| 289 size_t vertexStride = gp->getVertexStride(); | |
| 290 int instanceCount = fRects.count(); | |
| 291 | |
| 292 SkAutoTUnref<const GrBuffer> indexBuffer(target->resourceProvider()->ref
QuadIndexBuffer()); | |
| 293 InstancedHelper helper; | |
| 294 void* vertices = helper.init(target, kTriangles_GrPrimitiveType, vertexS
tride, | |
| 295 indexBuffer, kVertsPerInstance, | |
| 296 kIndicesPerInstance, instanceCount); | |
| 297 if (!vertices || !indexBuffer) { | |
| 298 SkDebugf("Could not allocate vertices\n"); | |
| 299 return; | |
| 300 } | |
| 301 | |
| 302 for (int i = 0; i < instanceCount; i++) { | |
| 303 const RectInfo& info = fRects[i]; | |
| 304 intptr_t verts = reinterpret_cast<intptr_t>(vertices) + | |
| 305 i * kVertsPerInstance * vertexStride; | |
| 306 if (fHasLocalRect) { | |
| 307 GrQuad quad(info.fLocalRect); | |
| 308 tesselate(verts, vertexStride, info.fColor, nullptr, info.fRect,
&quad); | |
| 309 } else { | |
| 310 tesselate(verts, vertexStride, info.fColor, nullptr, info.fRect,
nullptr); | |
| 311 } | |
| 312 } | |
| 313 helper.recordDraw(target, gp.get()); | |
| 314 } | |
| 315 | |
| 316 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { | |
| 317 NonAAFillRectPerspectiveBatch* that = t->cast<NonAAFillRectPerspectiveBa
tch>(); | |
| 318 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pi
peline(), | |
| 319 that->bounds(), caps)) { | |
| 320 return false; | |
| 321 } | |
| 322 | |
| 323 // We could batch across perspective vm changes if we really wanted to | |
| 324 if (!fViewMatrix.cheapEqualTo(that->fViewMatrix)) { | |
| 325 return false; | |
| 326 } | |
| 327 if (fHasLocalRect != that->fHasLocalRect) { | |
| 328 return false; | |
| 329 } | |
| 330 if (fHasLocalMatrix && !fLocalMatrix.cheapEqualTo(that->fLocalMatrix)) { | |
| 331 return false; | |
| 332 } | |
| 333 | |
| 334 // In the event of two batches, one who can tweak, one who cannot, we ju
st fall back to | |
| 335 // not tweaking | |
| 336 if (fOverrides.canTweakAlphaForCoverage() && !that->fOverrides.canTweakA
lphaForCoverage()) { | |
| 337 fOverrides = that->fOverrides; | |
| 338 } | |
| 339 | |
| 340 fRects.push_back_n(that->fRects.count(), that->fRects.begin()); | |
| 341 this->joinBounds(that->bounds()); | |
| 342 return true; | |
| 343 } | |
| 344 | |
| 345 struct RectInfo { | |
| 346 SkRect fRect; | |
| 347 GrColor fColor; | |
| 348 SkRect fLocalRect; | |
| 349 }; | |
| 350 | |
| 351 GrXPOverridesForBatch fOverrides; | |
| 352 SkSTArray<1, RectInfo, true> fRects; | |
| 353 bool fHasLocalMatrix; | |
| 354 bool fHasLocalRect; | |
| 355 SkMatrix fLocalMatrix; | |
| 356 SkMatrix fViewMatrix; | |
| 357 | |
| 358 typedef GrVertexBatch INHERITED; | |
| 359 }; | |
| 360 | |
| 361 namespace GrNonAAFillRectBatch { | 190 namespace GrNonAAFillRectBatch { |
| 362 | 191 |
| 363 GrDrawBatch* Create(GrColor color, | 192 GrDrawBatch* Create(GrColor color, |
| 364 const SkMatrix& viewMatrix, | 193 const SkMatrix& viewMatrix, |
| 365 const SkRect& rect, | 194 const SkRect& rect, |
| 366 const SkRect* localRect, | 195 const SkRect* localRect, |
| 367 const SkMatrix* localMatrix) { | 196 const SkMatrix* localMatrix) { |
| 368 return new NonAAFillRectBatch(color, viewMatrix, rect, localRect, localMatri
x); | 197 return new NonAAFillRectBatch(color, viewMatrix, rect, localRect, localMatri
x); |
| 369 } | 198 } |
| 370 | 199 |
| 371 GrDrawBatch* CreateWithPerspective(GrColor color, | |
| 372 const SkMatrix& viewMatrix, | |
| 373 const SkRect& rect, | |
| 374 const SkRect* localRect, | |
| 375 const SkMatrix* localMatrix) { | |
| 376 return new NonAAFillRectPerspectiveBatch(color, viewMatrix, rect, localRect,
localMatrix); | |
| 377 } | |
| 378 | |
| 379 }; | 200 }; |
| 380 | 201 |
| 381 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 202 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
| 382 | 203 |
| 383 #ifdef GR_TEST_UTILS | 204 #ifdef GR_TEST_UTILS |
| 384 | 205 |
| 385 #include "GrBatchTest.h" | 206 #include "GrBatchTest.h" |
| 386 | 207 |
| 387 DRAW_BATCH_TEST_DEFINE(RectBatch) { | 208 DRAW_BATCH_TEST_DEFINE(RectBatch) { |
| 388 GrColor color = GrRandomColor(random); | 209 GrColor color = GrRandomColor(random); |
| 389 SkRect rect = GrTest::TestRect(random); | 210 SkRect rect = GrTest::TestRect(random); |
| 390 SkRect localRect = GrTest::TestRect(random); | 211 SkRect localRect = GrTest::TestRect(random); |
| 391 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random); | 212 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random); |
| 392 SkMatrix localMatrix = GrTest::TestMatrix(random); | 213 SkMatrix localMatrix = GrTest::TestMatrix(random); |
| 393 | 214 |
| 394 bool hasLocalRect = random->nextBool(); | 215 bool hasLocalRect = random->nextBool(); |
| 395 bool hasLocalMatrix = random->nextBool(); | 216 bool hasLocalMatrix = random->nextBool(); |
| 396 return GrNonAAFillRectBatch::Create(color, viewMatrix, rect, | 217 return GrNonAAFillRectBatch::Create(color, viewMatrix, rect, |
| 397 hasLocalRect ? &localRect : nullptr, | 218 hasLocalRect ? &localRect : nullptr, |
| 398 hasLocalMatrix ? &localMatrix : nullptr)
; | 219 hasLocalMatrix ? &localMatrix : nullptr)
; |
| 399 } | 220 } |
| 400 | 221 |
| 401 #endif | 222 #endif |
| OLD | NEW |