joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 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 | |
joshualitt | bcf33d5 | 2015-08-26 08:10:35 -0700 | [diff] [blame] | 8 | #include "GrNonAAFillRectBatch.h" |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 9 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 10 | #include "GrBatchFlushState.h" |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 11 | #include "GrColor.h" |
| 12 | #include "GrDefaultGeoProcFactory.h" |
| 13 | #include "GrPrimitiveProcessor.h" |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 14 | #include "GrResourceProvider.h" |
joshualitt | ae5b2c6 | 2015-08-19 08:48:41 -0700 | [diff] [blame] | 15 | #include "GrQuad.h" |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 16 | #include "GrVertexBatch.h" |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 17 | |
reed | a39667c | 2016-08-22 06:39:49 -0700 | [diff] [blame] | 18 | #include "SkMatrixPriv.h" |
| 19 | |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 20 | static const int kVertsPerInstance = 4; |
| 21 | static const int kIndicesPerInstance = 6; |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 22 | |
| 23 | /** We always use per-vertex colors so that rects can be batched across color changes. Sometimes |
| 24 | we have explicit local coords and sometimes not. We *could* always provide explicit local |
| 25 | coords and just duplicate the positions when the caller hasn't provided a local coord rect, |
| 26 | but we haven't seen a use case which frequently switches between local rect and no local |
| 27 | rect draws. |
| 28 | |
| 29 | The vertex attrib order is always pos, color, [local coords]. |
| 30 | */ |
robertphillips | 6abd1d1 | 2016-07-01 09:06:56 -0700 | [diff] [blame] | 31 | static sk_sp<GrGeometryProcessor> make_gp(bool readsCoverage) { |
| 32 | using namespace GrDefaultGeoProcFactory; |
| 33 | Color color(Color::kAttribute_Type); |
| 34 | Coverage coverage(readsCoverage ? Coverage::kSolid_Type : Coverage::kNone_Type); |
| 35 | |
| 36 | LocalCoords localCoords(LocalCoords::kHasExplicit_Type); |
| 37 | return GrDefaultGeoProcFactory::Make(color, coverage, localCoords, SkMatrix::I()); |
| 38 | } |
| 39 | |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 40 | static void tesselate(intptr_t vertices, |
| 41 | size_t vertexStride, |
| 42 | GrColor color, |
robertphillips | 6abd1d1 | 2016-07-01 09:06:56 -0700 | [diff] [blame] | 43 | const SkMatrix* viewMatrix, |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 44 | const SkRect& rect, |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 45 | const GrQuad* localQuad) { |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 46 | SkPoint* positions = reinterpret_cast<SkPoint*>(vertices); |
| 47 | |
| 48 | positions->setRectFan(rect.fLeft, rect.fTop, |
| 49 | rect.fRight, rect.fBottom, vertexStride); |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 50 | |
robertphillips | 6abd1d1 | 2016-07-01 09:06:56 -0700 | [diff] [blame] | 51 | if (viewMatrix) { |
reed | a39667c | 2016-08-22 06:39:49 -0700 | [diff] [blame] | 52 | SkMatrixPriv::MapPointsWithStride(*viewMatrix, positions, vertexStride, kVertsPerInstance); |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | // Setup local coords |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 56 | // TODO we should only do this if local coords are being read |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 57 | if (localQuad) { |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 58 | static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 59 | for (int i = 0; i < kVertsPerInstance; i++) { |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 60 | SkPoint* coords = reinterpret_cast<SkPoint*>(vertices + kLocalOffset + |
| 61 | i * vertexStride); |
| 62 | *coords = localQuad->point(i); |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
| 66 | static const int kColorOffset = sizeof(SkPoint); |
| 67 | GrColor* vertColor = reinterpret_cast<GrColor*>(vertices + kColorOffset); |
| 68 | for (int j = 0; j < 4; ++j) { |
| 69 | *vertColor = color; |
| 70 | vertColor = (GrColor*) ((intptr_t) vertColor + vertexStride); |
| 71 | } |
| 72 | } |
| 73 | |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 74 | class NonAAFillRectBatch : public GrVertexBatch { |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 75 | public: |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 76 | DEFINE_BATCH_CLASS_ID |
| 77 | |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 78 | NonAAFillRectBatch(GrColor color, const SkMatrix& viewMatrix, const SkRect& rect, |
| 79 | const SkRect* localRect, const SkMatrix* localMatrix) |
| 80 | : INHERITED(ClassID()) { |
| 81 | SkASSERT(!viewMatrix.hasPerspective() && (!localMatrix || |
| 82 | !localMatrix->hasPerspective())); |
| 83 | RectInfo& info = fRects.push_back(); |
| 84 | info.fColor = color; |
| 85 | info.fViewMatrix = viewMatrix; |
| 86 | info.fRect = rect; |
| 87 | if (localRect && localMatrix) { |
| 88 | info.fLocalQuad.setFromMappedRect(*localRect, *localMatrix); |
| 89 | } else if (localRect) { |
| 90 | info.fLocalQuad.set(*localRect); |
| 91 | } else if (localMatrix) { |
| 92 | info.fLocalQuad.setFromMappedRect(rect, *localMatrix); |
| 93 | } else { |
| 94 | info.fLocalQuad.set(rect); |
| 95 | } |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 96 | this->setTransformedBounds(fRects[0].fRect, viewMatrix, HasAABloat::kNo, IsZeroArea::kNo); |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 97 | } |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 98 | |
bsalomon | bc9b6a4 | 2016-07-01 05:35:51 -0700 | [diff] [blame] | 99 | const char* name() const override { return "NonAAFillRectBatch"; } |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 100 | |
| 101 | SkString dumpInfo() const override { |
| 102 | SkString str; |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 103 | str.appendf("# batched: %d\n", fRects.count()); |
| 104 | for (int i = 0; i < fRects.count(); ++i) { |
| 105 | const RectInfo& info = fRects[i]; |
bsalomon | bc9b6a4 | 2016-07-01 05:35:51 -0700 | [diff] [blame] | 106 | str.appendf("%d: Color: 0x%08x, Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 107 | i, info.fColor, |
| 108 | info.fRect.fLeft, info.fRect.fTop, info.fRect.fRight, info.fRect.fBottom); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 109 | } |
| 110 | str.append(INHERITED::dumpInfo()); |
| 111 | return str; |
| 112 | } |
| 113 | |
| 114 | void computePipelineOptimizations(GrInitInvariantOutput* color, |
| 115 | GrInitInvariantOutput* coverage, |
| 116 | GrBatchToXPOverrides* overrides) const override { |
| 117 | // When this is called on a batch, there is only one geometry bundle |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 118 | color->setKnownFourComponents(fRects[0].fColor); |
bsalomon | bc9b6a4 | 2016-07-01 05:35:51 -0700 | [diff] [blame] | 119 | coverage->setKnownSingleComponent(0xff); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void initBatchTracker(const GrXPOverridesForBatch& overrides) override { |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 123 | overrides.getOverrideColorIfSet(&fRects[0].fColor); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 124 | fOverrides = overrides; |
| 125 | } |
| 126 | |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 127 | private: |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 128 | NonAAFillRectBatch() : INHERITED(ClassID()) {} |
| 129 | |
| 130 | void onPrepareDraws(Target* target) const override { |
robertphillips | 6abd1d1 | 2016-07-01 09:06:56 -0700 | [diff] [blame] | 131 | sk_sp<GrGeometryProcessor> gp = make_gp(fOverrides.readsCoverage()); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 132 | if (!gp) { |
| 133 | SkDebugf("Couldn't create GrGeometryProcessor\n"); |
| 134 | return; |
| 135 | } |
bsalomon | bc9b6a4 | 2016-07-01 05:35:51 -0700 | [diff] [blame] | 136 | SkASSERT(gp->getVertexStride() == |
| 137 | sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr)); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 138 | |
| 139 | size_t vertexStride = gp->getVertexStride(); |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 140 | int instanceCount = fRects.count(); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 141 | |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 142 | sk_sp<const GrBuffer> indexBuffer(target->resourceProvider()->refQuadIndexBuffer()); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 143 | InstancedHelper helper; |
| 144 | void* vertices = helper.init(target, kTriangles_GrPrimitiveType, vertexStride, |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 145 | indexBuffer.get(), kVertsPerInstance, |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 146 | kIndicesPerInstance, instanceCount); |
| 147 | if (!vertices || !indexBuffer) { |
| 148 | SkDebugf("Could not allocate vertices\n"); |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | for (int i = 0; i < instanceCount; i++) { |
| 153 | intptr_t verts = reinterpret_cast<intptr_t>(vertices) + |
| 154 | i * kVertsPerInstance * vertexStride; |
robertphillips | 6abd1d1 | 2016-07-01 09:06:56 -0700 | [diff] [blame] | 155 | tesselate(verts, vertexStride, fRects[i].fColor, &fRects[i].fViewMatrix, |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 156 | fRects[i].fRect, &fRects[i].fLocalQuad); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 157 | } |
| 158 | helper.recordDraw(target, gp.get()); |
| 159 | } |
| 160 | |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 161 | bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { |
| 162 | NonAAFillRectBatch* that = t->cast<NonAAFillRectBatch>(); |
| 163 | if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(), |
| 164 | that->bounds(), caps)) { |
| 165 | return false; |
| 166 | } |
| 167 | |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 168 | // In the event of two batches, one who can tweak, one who cannot, we just fall back to |
| 169 | // not tweaking |
| 170 | if (fOverrides.canTweakAlphaForCoverage() && !that->fOverrides.canTweakAlphaForCoverage()) { |
| 171 | fOverrides = that->fOverrides; |
| 172 | } |
| 173 | |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 174 | fRects.push_back_n(that->fRects.count(), that->fRects.begin()); |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 175 | this->joinBounds(*that); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 176 | return true; |
| 177 | } |
| 178 | |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 179 | struct RectInfo { |
| 180 | GrColor fColor; |
| 181 | SkMatrix fViewMatrix; |
| 182 | SkRect fRect; |
| 183 | GrQuad fLocalQuad; |
| 184 | }; |
| 185 | |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 186 | GrXPOverridesForBatch fOverrides; |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 187 | SkSTArray<1, RectInfo, true> fRects; |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 188 | |
| 189 | typedef GrVertexBatch INHERITED; |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 190 | }; |
| 191 | |
bsalomon | b8cbd20 | 2016-06-30 13:09:48 -0700 | [diff] [blame] | 192 | namespace GrNonAAFillRectBatch { |
| 193 | |
| 194 | GrDrawBatch* Create(GrColor color, |
| 195 | const SkMatrix& viewMatrix, |
| 196 | const SkRect& rect, |
| 197 | const SkRect* localRect, |
| 198 | const SkMatrix* localMatrix) { |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 199 | return new NonAAFillRectBatch(color, viewMatrix, rect, localRect, localMatrix); |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 200 | } |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 201 | |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 202 | }; |
| 203 | |
| 204 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 205 | |
| 206 | #ifdef GR_TEST_UTILS |
| 207 | |
| 208 | #include "GrBatchTest.h" |
| 209 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 210 | DRAW_BATCH_TEST_DEFINE(RectBatch) { |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 211 | GrColor color = GrRandomColor(random); |
| 212 | SkRect rect = GrTest::TestRect(random); |
| 213 | SkRect localRect = GrTest::TestRect(random); |
| 214 | SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random); |
| 215 | SkMatrix localMatrix = GrTest::TestMatrix(random); |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 216 | |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 217 | bool hasLocalRect = random->nextBool(); |
| 218 | bool hasLocalMatrix = random->nextBool(); |
joshualitt | bcf33d5 | 2015-08-26 08:10:35 -0700 | [diff] [blame] | 219 | return GrNonAAFillRectBatch::Create(color, viewMatrix, rect, |
| 220 | hasLocalRect ? &localRect : nullptr, |
| 221 | hasLocalMatrix ? &localMatrix : nullptr); |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | #endif |