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