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 | */ |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 29 | static sk_sp<GrGeometryProcessor> make_gp(const SkMatrix& viewMatrix, |
| 30 | bool readsCoverage, |
| 31 | bool hasExplicitLocalCoords, |
| 32 | const SkMatrix* localMatrix) { |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 33 | using namespace GrDefaultGeoProcFactory; |
| 34 | Color color(Color::kAttribute_Type); |
| 35 | Coverage coverage(readsCoverage ? Coverage::kSolid_Type : Coverage::kNone_Type); |
| 36 | |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 37 | // If we have perspective on the viewMatrix then we won't map on the CPU, nor will we map |
| 38 | // the local rect on the cpu (in case the localMatrix also has perspective). |
| 39 | // Otherwise, if we have a local rect, then we apply the localMatrix directly to the localRect |
| 40 | // to generate vertex local coords |
| 41 | if (viewMatrix.hasPerspective()) { |
| 42 | LocalCoords localCoords(hasExplicitLocalCoords ? LocalCoords::kHasExplicit_Type : |
| 43 | LocalCoords::kUsePosition_Type, |
| 44 | localMatrix); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 45 | return GrDefaultGeoProcFactory::Make(color, coverage, localCoords, viewMatrix); |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 46 | } else if (hasExplicitLocalCoords) { |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 47 | LocalCoords localCoords(LocalCoords::kHasExplicit_Type); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 48 | return GrDefaultGeoProcFactory::Make(color, coverage, localCoords, SkMatrix::I()); |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 49 | } else { |
| 50 | LocalCoords localCoords(LocalCoords::kUsePosition_Type, localMatrix); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 51 | return GrDefaultGeoProcFactory::MakeForDeviceSpace(color, coverage, localCoords, |
| 52 | viewMatrix); |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
| 56 | static void tesselate(intptr_t vertices, |
| 57 | size_t vertexStride, |
| 58 | GrColor color, |
| 59 | const SkMatrix& viewMatrix, |
| 60 | const SkRect& rect, |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 61 | const GrQuad* localQuad) { |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 62 | SkPoint* positions = reinterpret_cast<SkPoint*>(vertices); |
| 63 | |
| 64 | positions->setRectFan(rect.fLeft, rect.fTop, |
| 65 | rect.fRight, rect.fBottom, vertexStride); |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 66 | |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 67 | if (!viewMatrix.hasPerspective()) { |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 68 | viewMatrix.mapPointsWithStride(positions, vertexStride, kVertsPerInstance); |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | // Setup local coords |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 72 | // TODO we should only do this if local coords are being read |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 73 | if (localQuad) { |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 74 | static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 75 | for (int i = 0; i < kVertsPerInstance; i++) { |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 76 | SkPoint* coords = reinterpret_cast<SkPoint*>(vertices + kLocalOffset + |
| 77 | i * vertexStride); |
| 78 | *coords = localQuad->point(i); |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
| 82 | static const int kColorOffset = sizeof(SkPoint); |
| 83 | GrColor* vertColor = reinterpret_cast<GrColor*>(vertices + kColorOffset); |
| 84 | for (int j = 0; j < 4; ++j) { |
| 85 | *vertColor = color; |
| 86 | vertColor = (GrColor*) ((intptr_t) vertColor + vertexStride); |
| 87 | } |
| 88 | } |
| 89 | |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 90 | class NonAAFillRectBatch : public GrVertexBatch { |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 91 | public: |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 92 | DEFINE_BATCH_CLASS_ID |
| 93 | |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 94 | NonAAFillRectBatch(GrColor color, const SkMatrix& viewMatrix, const SkRect& rect, |
| 95 | const SkRect* localRect, const SkMatrix* localMatrix) |
| 96 | : INHERITED(ClassID()) { |
| 97 | SkASSERT(!viewMatrix.hasPerspective() && (!localMatrix || |
| 98 | !localMatrix->hasPerspective())); |
| 99 | RectInfo& info = fRects.push_back(); |
| 100 | info.fColor = color; |
| 101 | info.fViewMatrix = viewMatrix; |
| 102 | info.fRect = rect; |
| 103 | if (localRect && localMatrix) { |
| 104 | info.fLocalQuad.setFromMappedRect(*localRect, *localMatrix); |
| 105 | } else if (localRect) { |
| 106 | info.fLocalQuad.set(*localRect); |
| 107 | } else if (localMatrix) { |
| 108 | info.fLocalQuad.setFromMappedRect(rect, *localMatrix); |
| 109 | } else { |
| 110 | info.fLocalQuad.set(rect); |
| 111 | } |
| 112 | viewMatrix.mapRect(&fBounds, fRects[0].fRect); |
| 113 | } |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 114 | |
bsalomon | bc9b6a4 | 2016-07-01 05:35:51 -0700 | [diff] [blame] | 115 | const char* name() const override { return "NonAAFillRectBatch"; } |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 116 | |
| 117 | SkString dumpInfo() const override { |
| 118 | SkString str; |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 119 | str.appendf("# batched: %d\n", fRects.count()); |
| 120 | for (int i = 0; i < fRects.count(); ++i) { |
| 121 | const RectInfo& info = fRects[i]; |
bsalomon | bc9b6a4 | 2016-07-01 05:35:51 -0700 | [diff] [blame] | 122 | 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] | 123 | i, info.fColor, |
| 124 | info.fRect.fLeft, info.fRect.fTop, info.fRect.fRight, info.fRect.fBottom); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 125 | } |
| 126 | str.append(INHERITED::dumpInfo()); |
| 127 | return str; |
| 128 | } |
| 129 | |
| 130 | void computePipelineOptimizations(GrInitInvariantOutput* color, |
| 131 | GrInitInvariantOutput* coverage, |
| 132 | GrBatchToXPOverrides* overrides) const override { |
| 133 | // When this is called on a batch, there is only one geometry bundle |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 134 | color->setKnownFourComponents(fRects[0].fColor); |
bsalomon | bc9b6a4 | 2016-07-01 05:35:51 -0700 | [diff] [blame] | 135 | coverage->setKnownSingleComponent(0xff); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | void initBatchTracker(const GrXPOverridesForBatch& overrides) override { |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 139 | overrides.getOverrideColorIfSet(&fRects[0].fColor); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 140 | fOverrides = overrides; |
| 141 | } |
| 142 | |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 143 | private: |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 144 | NonAAFillRectBatch() : INHERITED(ClassID()) {} |
| 145 | |
| 146 | void onPrepareDraws(Target* target) const override { |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 147 | sk_sp<GrGeometryProcessor> gp = make_gp(fRects[0].fViewMatrix, fOverrides.readsCoverage(), |
bsalomon | bc9b6a4 | 2016-07-01 05:35:51 -0700 | [diff] [blame] | 148 | true, nullptr); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 149 | if (!gp) { |
| 150 | SkDebugf("Couldn't create GrGeometryProcessor\n"); |
| 151 | return; |
| 152 | } |
bsalomon | bc9b6a4 | 2016-07-01 05:35:51 -0700 | [diff] [blame] | 153 | SkASSERT(gp->getVertexStride() == |
| 154 | sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr)); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 155 | |
| 156 | size_t vertexStride = gp->getVertexStride(); |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 157 | int instanceCount = fRects.count(); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 158 | |
bsalomon | bc9b6a4 | 2016-07-01 05:35:51 -0700 | [diff] [blame] | 159 | SkAutoTUnref<const GrBuffer> indexBuffer(target->resourceProvider()->refQuadIndexBuffer()); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 160 | InstancedHelper helper; |
| 161 | void* vertices = helper.init(target, kTriangles_GrPrimitiveType, vertexStride, |
| 162 | indexBuffer, kVertsPerInstance, |
| 163 | kIndicesPerInstance, instanceCount); |
| 164 | if (!vertices || !indexBuffer) { |
| 165 | SkDebugf("Could not allocate vertices\n"); |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | for (int i = 0; i < instanceCount; i++) { |
| 170 | intptr_t verts = reinterpret_cast<intptr_t>(vertices) + |
| 171 | i * kVertsPerInstance * vertexStride; |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 172 | tesselate(verts, vertexStride, fRects[i].fColor, fRects[i].fViewMatrix, |
| 173 | fRects[i].fRect, &fRects[i].fLocalQuad); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 174 | } |
| 175 | helper.recordDraw(target, gp.get()); |
| 176 | } |
| 177 | |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 178 | bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { |
| 179 | NonAAFillRectBatch* that = t->cast<NonAAFillRectBatch>(); |
| 180 | if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(), |
| 181 | that->bounds(), caps)) { |
| 182 | return false; |
| 183 | } |
| 184 | |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 185 | // In the event of two batches, one who can tweak, one who cannot, we just fall back to |
| 186 | // not tweaking |
| 187 | if (fOverrides.canTweakAlphaForCoverage() && !that->fOverrides.canTweakAlphaForCoverage()) { |
| 188 | fOverrides = that->fOverrides; |
| 189 | } |
| 190 | |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 191 | fRects.push_back_n(that->fRects.count(), that->fRects.begin()); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 192 | this->joinBounds(that->bounds()); |
| 193 | return true; |
| 194 | } |
| 195 | |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 196 | struct RectInfo { |
| 197 | GrColor fColor; |
| 198 | SkMatrix fViewMatrix; |
| 199 | SkRect fRect; |
| 200 | GrQuad fLocalQuad; |
| 201 | }; |
| 202 | |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 203 | GrXPOverridesForBatch fOverrides; |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 204 | SkSTArray<1, RectInfo, true> fRects; |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 205 | |
| 206 | typedef GrVertexBatch INHERITED; |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 207 | }; |
| 208 | |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 209 | // We handle perspective in the local matrix or viewmatrix with special batches |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 210 | class NonAAFillRectPerspectiveBatch : public GrVertexBatch { |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 211 | public: |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 212 | DEFINE_BATCH_CLASS_ID |
| 213 | |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 214 | NonAAFillRectPerspectiveBatch(GrColor color, const SkMatrix& viewMatrix, const SkRect& rect, |
| 215 | const SkRect* localRect, const SkMatrix* localMatrix) |
| 216 | : INHERITED(ClassID()) |
| 217 | , fViewMatrix(viewMatrix) { |
| 218 | SkASSERT(viewMatrix.hasPerspective() || (localMatrix && |
| 219 | localMatrix->hasPerspective())); |
| 220 | RectInfo& info = fRects.push_back(); |
| 221 | info.fColor = color; |
| 222 | info.fRect = rect; |
| 223 | fHasLocalRect = SkToBool(localRect); |
| 224 | fHasLocalMatrix = SkToBool(localMatrix); |
| 225 | if (fHasLocalMatrix) { |
| 226 | fLocalMatrix = *localMatrix; |
| 227 | } |
| 228 | if (fHasLocalRect) { |
| 229 | info.fLocalRect = *localRect; |
| 230 | } |
| 231 | viewMatrix.mapRect(&fBounds, rect); |
| 232 | } |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 233 | |
bsalomon | bc9b6a4 | 2016-07-01 05:35:51 -0700 | [diff] [blame] | 234 | const char* name() const override { return "NonAAFillRectPerspectiveBatch"; } |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 235 | |
| 236 | SkString dumpInfo() const override { |
| 237 | SkString str; |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 238 | str.appendf("# batched: %d\n", fRects.count()); |
| 239 | for (int i = 0; i < fRects.count(); ++i) { |
| 240 | const RectInfo& geo = fRects[0]; |
bsalomon | bc9b6a4 | 2016-07-01 05:35:51 -0700 | [diff] [blame] | 241 | str.appendf("%d: Color: 0x%08x, Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", |
| 242 | i, geo.fColor, |
| 243 | geo.fRect.fLeft, geo.fRect.fTop, geo.fRect.fRight, geo.fRect.fBottom); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 244 | } |
| 245 | str.append(INHERITED::dumpInfo()); |
| 246 | return str; |
| 247 | } |
| 248 | |
| 249 | void computePipelineOptimizations(GrInitInvariantOutput* color, |
| 250 | GrInitInvariantOutput* coverage, |
| 251 | GrBatchToXPOverrides* overrides) const override { |
| 252 | // When this is called on a batch, there is only one geometry bundle |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 253 | color->setKnownFourComponents(fRects[0].fColor); |
bsalomon | bc9b6a4 | 2016-07-01 05:35:51 -0700 | [diff] [blame] | 254 | coverage->setKnownSingleComponent(0xff); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | void initBatchTracker(const GrXPOverridesForBatch& overrides) override { |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 258 | overrides.getOverrideColorIfSet(&fRects[0].fColor); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 259 | fOverrides = overrides; |
| 260 | } |
| 261 | |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 262 | private: |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 263 | NonAAFillRectPerspectiveBatch() : INHERITED(ClassID()) {} |
| 264 | |
| 265 | void onPrepareDraws(Target* target) const override { |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 266 | sk_sp<GrGeometryProcessor> gp = make_gp(fViewMatrix, fOverrides.readsCoverage(), |
| 267 | fHasLocalRect, |
| 268 | fHasLocalMatrix ? &fLocalMatrix : nullptr); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 269 | if (!gp) { |
| 270 | SkDebugf("Couldn't create GrGeometryProcessor\n"); |
| 271 | return; |
| 272 | } |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 273 | SkASSERT(fHasLocalRect |
bsalomon | bc9b6a4 | 2016-07-01 05:35:51 -0700 | [diff] [blame] | 274 | ? gp->getVertexStride() == |
| 275 | sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr) |
| 276 | : gp->getVertexStride() == sizeof(GrDefaultGeoProcFactory::PositionColorAttr)); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 277 | |
| 278 | size_t vertexStride = gp->getVertexStride(); |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 279 | int instanceCount = fRects.count(); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 280 | |
bsalomon | bc9b6a4 | 2016-07-01 05:35:51 -0700 | [diff] [blame] | 281 | SkAutoTUnref<const GrBuffer> indexBuffer(target->resourceProvider()->refQuadIndexBuffer()); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 282 | InstancedHelper helper; |
| 283 | void* vertices = helper.init(target, kTriangles_GrPrimitiveType, vertexStride, |
| 284 | indexBuffer, kVertsPerInstance, |
| 285 | kIndicesPerInstance, instanceCount); |
| 286 | if (!vertices || !indexBuffer) { |
| 287 | SkDebugf("Could not allocate vertices\n"); |
| 288 | return; |
| 289 | } |
| 290 | |
| 291 | for (int i = 0; i < instanceCount; i++) { |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 292 | const RectInfo& info = fRects[i]; |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 293 | intptr_t verts = reinterpret_cast<intptr_t>(vertices) + |
| 294 | i * kVertsPerInstance * vertexStride; |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 295 | if (fHasLocalRect) { |
| 296 | GrQuad quad(info.fLocalRect); |
| 297 | tesselate(verts, vertexStride, info.fColor, fViewMatrix, info.fRect, &quad); |
bsalomon | bc9b6a4 | 2016-07-01 05:35:51 -0700 | [diff] [blame] | 298 | } else { |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 299 | tesselate(verts, vertexStride, info.fColor, fViewMatrix, info.fRect, nullptr); |
bsalomon | bc9b6a4 | 2016-07-01 05:35:51 -0700 | [diff] [blame] | 300 | } |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 301 | } |
| 302 | helper.recordDraw(target, gp.get()); |
| 303 | } |
| 304 | |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 305 | bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { |
| 306 | NonAAFillRectPerspectiveBatch* that = t->cast<NonAAFillRectPerspectiveBatch>(); |
| 307 | if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(), |
| 308 | that->bounds(), caps)) { |
| 309 | return false; |
| 310 | } |
| 311 | |
bsalomon | bc9b6a4 | 2016-07-01 05:35:51 -0700 | [diff] [blame] | 312 | // We could batch across perspective vm changes if we really wanted to |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 313 | if (!fViewMatrix.cheapEqualTo(that->fViewMatrix)) { |
bsalomon | bc9b6a4 | 2016-07-01 05:35:51 -0700 | [diff] [blame] | 314 | return false; |
| 315 | } |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 316 | if (fHasLocalRect != that->fHasLocalRect) { |
bsalomon | bc9b6a4 | 2016-07-01 05:35:51 -0700 | [diff] [blame] | 317 | return false; |
| 318 | } |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 319 | if (fHasLocalMatrix && !fLocalMatrix.cheapEqualTo(that->fLocalMatrix)) { |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 320 | return false; |
| 321 | } |
| 322 | |
| 323 | // In the event of two batches, one who can tweak, one who cannot, we just fall back to |
| 324 | // not tweaking |
| 325 | if (fOverrides.canTweakAlphaForCoverage() && !that->fOverrides.canTweakAlphaForCoverage()) { |
| 326 | fOverrides = that->fOverrides; |
| 327 | } |
| 328 | |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 329 | fRects.push_back_n(that->fRects.count(), that->fRects.begin()); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 330 | this->joinBounds(that->bounds()); |
| 331 | return true; |
| 332 | } |
| 333 | |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 334 | struct RectInfo { |
| 335 | SkRect fRect; |
| 336 | GrColor fColor; |
| 337 | SkRect fLocalRect; |
| 338 | }; |
| 339 | |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 340 | GrXPOverridesForBatch fOverrides; |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 341 | SkSTArray<1, RectInfo, true> fRects; |
| 342 | bool fHasLocalMatrix; |
| 343 | bool fHasLocalRect; |
| 344 | SkMatrix fLocalMatrix; |
| 345 | SkMatrix fViewMatrix; |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 346 | |
| 347 | typedef GrVertexBatch INHERITED; |
joshualitt | ae41b38 | 2015-08-19 06:54:08 -0700 | [diff] [blame] | 348 | }; |
| 349 | |
bsalomon | b8cbd20 | 2016-06-30 13:09:48 -0700 | [diff] [blame] | 350 | namespace GrNonAAFillRectBatch { |
| 351 | |
| 352 | GrDrawBatch* Create(GrColor color, |
| 353 | const SkMatrix& viewMatrix, |
| 354 | const SkRect& rect, |
| 355 | const SkRect* localRect, |
| 356 | const SkMatrix* localMatrix) { |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 357 | return new NonAAFillRectBatch(color, viewMatrix, rect, localRect, localMatrix); |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 358 | } |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 359 | |
bsalomon | b8cbd20 | 2016-06-30 13:09:48 -0700 | [diff] [blame] | 360 | GrDrawBatch* CreateWithPerspective(GrColor color, |
| 361 | const SkMatrix& viewMatrix, |
| 362 | const SkRect& rect, |
| 363 | const SkRect* localRect, |
| 364 | const SkMatrix* localMatrix) { |
bsalomon | 9d7f184 | 2016-07-01 08:01:36 -0700 | [diff] [blame] | 365 | return new NonAAFillRectPerspectiveBatch(color, viewMatrix, rect, localRect, localMatrix); |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 366 | } |
| 367 | |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 368 | }; |
| 369 | |
| 370 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 371 | |
| 372 | #ifdef GR_TEST_UTILS |
| 373 | |
| 374 | #include "GrBatchTest.h" |
| 375 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 376 | DRAW_BATCH_TEST_DEFINE(RectBatch) { |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 377 | GrColor color = GrRandomColor(random); |
| 378 | SkRect rect = GrTest::TestRect(random); |
| 379 | SkRect localRect = GrTest::TestRect(random); |
| 380 | SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random); |
| 381 | SkMatrix localMatrix = GrTest::TestMatrix(random); |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 382 | |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 383 | bool hasLocalRect = random->nextBool(); |
| 384 | bool hasLocalMatrix = random->nextBool(); |
joshualitt | bcf33d5 | 2015-08-26 08:10:35 -0700 | [diff] [blame] | 385 | return GrNonAAFillRectBatch::Create(color, viewMatrix, rect, |
| 386 | hasLocalRect ? &localRect : nullptr, |
| 387 | hasLocalMatrix ? &localMatrix : nullptr); |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | #endif |