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