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