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 | |
| 103 | const char* name() const override { return Name(); } |
| 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) { |
| 109 | str.append(DumpInfo(fGeoData[i], i)); |
| 110 | } |
| 111 | str.append(INHERITED::dumpInfo()); |
| 112 | return str; |
| 113 | } |
| 114 | |
| 115 | void computePipelineOptimizations(GrInitInvariantOutput* color, |
| 116 | GrInitInvariantOutput* coverage, |
| 117 | GrBatchToXPOverrides* overrides) const override { |
| 118 | // When this is called on a batch, there is only one geometry bundle |
| 119 | color->setKnownFourComponents(fGeoData[0].fColor); |
| 120 | InitInvariantOutputCoverage(coverage); |
| 121 | } |
| 122 | |
| 123 | void initBatchTracker(const GrXPOverridesForBatch& overrides) override { |
| 124 | overrides.getOverrideColorIfSet(&fGeoData[0].fColor); |
| 125 | fOverrides = overrides; |
| 126 | } |
| 127 | |
| 128 | SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } |
| 129 | |
| 130 | // After seeding, the client should call init() so the Batch can initialize itself |
| 131 | void init() { |
| 132 | const Geometry& geo = fGeoData[0]; |
| 133 | SetBounds(geo, &fBounds); |
| 134 | } |
| 135 | |
| 136 | void updateBoundsAfterAppend() { |
| 137 | const Geometry& geo = fGeoData.back(); |
| 138 | UpdateBoundsAfterAppend(geo, &fBounds); |
| 139 | } |
| 140 | |
| 141 | private: |
joshualitt | bcf33d5 | 2015-08-26 08:10:35 -0700 | [diff] [blame] | 142 | static const char* Name() { return "NonAAFillRectBatch"; } |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 143 | |
robertphillips | 783a4da | 2015-11-19 14:00:02 -0800 | [diff] [blame] | 144 | static SkString DumpInfo(const Geometry& geo, int index) { |
robertphillips | e004bfc | 2015-11-16 09:06:59 -0800 | [diff] [blame] | 145 | SkString str; |
robertphillips | 783a4da | 2015-11-19 14:00:02 -0800 | [diff] [blame] | 146 | str.appendf("%d: Color: 0x%08x, Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", |
| 147 | index, |
robertphillips | e004bfc | 2015-11-16 09:06:59 -0800 | [diff] [blame] | 148 | geo.fColor, |
| 149 | geo.fRect.fLeft, geo.fRect.fTop, geo.fRect.fRight, geo.fRect.fBottom); |
| 150 | return str; |
| 151 | } |
| 152 | |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 153 | static bool CanCombine(const Geometry& mine, const Geometry& theirs, |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 154 | const GrXPOverridesForBatch& overrides) { |
joshualitt | 87e4752 | 2015-08-20 07:22:42 -0700 | [diff] [blame] | 155 | return true; |
| 156 | } |
| 157 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 158 | static sk_sp<GrGeometryProcessor> MakeGP(const Geometry& geo, |
| 159 | const GrXPOverridesForBatch& overrides) { |
| 160 | sk_sp<GrGeometryProcessor> gp = make_gp(geo.fViewMatrix, overrides.readsCoverage(), true, |
| 161 | nullptr); |
joshualitt | 87e4752 | 2015-08-20 07:22:42 -0700 | [diff] [blame] | 162 | |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 163 | SkASSERT(gp->getVertexStride() == |
| 164 | sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr)); |
| 165 | return gp; |
joshualitt | ae41b38 | 2015-08-19 06:54:08 -0700 | [diff] [blame] | 166 | } |
| 167 | |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 168 | static void Tesselate(intptr_t vertices, size_t vertexStride, const Geometry& geo, |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 169 | const GrXPOverridesForBatch& overrides) { |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 170 | tesselate(vertices, vertexStride, geo.fColor, geo.fViewMatrix, geo.fRect, &geo.fLocalQuad); |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 171 | } |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 172 | |
| 173 | static void InitInvariantOutputCoverage(GrInitInvariantOutput* out) { |
| 174 | out->setKnownSingleComponent(0xff); |
| 175 | } |
| 176 | |
| 177 | static const GrBuffer* GetIndexBuffer(GrResourceProvider* rp) { |
| 178 | return rp->refQuadIndexBuffer(); |
| 179 | } |
| 180 | |
| 181 | static void SetBounds(const Geometry& geo, SkRect* outBounds) { |
| 182 | geo.fViewMatrix.mapRect(outBounds, geo.fRect); |
| 183 | } |
| 184 | |
| 185 | static void UpdateBoundsAfterAppend(const Geometry& geo, SkRect* outBounds) { |
| 186 | SkRect bounds = geo.fRect; |
| 187 | geo.fViewMatrix.mapRect(&bounds); |
| 188 | outBounds->join(bounds); |
| 189 | } |
| 190 | |
| 191 | NonAAFillRectBatch() : INHERITED(ClassID()) {} |
| 192 | |
| 193 | void onPrepareDraws(Target* target) const override { |
| 194 | sk_sp<GrGeometryProcessor> gp(MakeGP(this->seedGeometry(), fOverrides)); |
| 195 | if (!gp) { |
| 196 | SkDebugf("Couldn't create GrGeometryProcessor\n"); |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | size_t vertexStride = gp->getVertexStride(); |
| 201 | int instanceCount = fGeoData.count(); |
| 202 | |
| 203 | SkAutoTUnref<const GrBuffer> indexBuffer(GetIndexBuffer(target->resourceProvider())); |
| 204 | InstancedHelper helper; |
| 205 | void* vertices = helper.init(target, kTriangles_GrPrimitiveType, vertexStride, |
| 206 | indexBuffer, kVertsPerInstance, |
| 207 | kIndicesPerInstance, instanceCount); |
| 208 | if (!vertices || !indexBuffer) { |
| 209 | SkDebugf("Could not allocate vertices\n"); |
| 210 | return; |
| 211 | } |
| 212 | |
| 213 | for (int i = 0; i < instanceCount; i++) { |
| 214 | intptr_t verts = reinterpret_cast<intptr_t>(vertices) + |
| 215 | i * kVertsPerInstance * vertexStride; |
| 216 | Tesselate(verts, vertexStride, fGeoData[i], fOverrides); |
| 217 | } |
| 218 | helper.recordDraw(target, gp.get()); |
| 219 | } |
| 220 | |
| 221 | const Geometry& seedGeometry() const { return fGeoData[0]; } |
| 222 | |
| 223 | bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { |
| 224 | NonAAFillRectBatch* that = t->cast<NonAAFillRectBatch>(); |
| 225 | if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(), |
| 226 | that->bounds(), caps)) { |
| 227 | return false; |
| 228 | } |
| 229 | |
| 230 | if (!CanCombine(this->seedGeometry(), that->seedGeometry(), fOverrides)) { |
| 231 | return false; |
| 232 | } |
| 233 | |
| 234 | // In the event of two batches, one who can tweak, one who cannot, we just fall back to |
| 235 | // not tweaking |
| 236 | if (fOverrides.canTweakAlphaForCoverage() && !that->fOverrides.canTweakAlphaForCoverage()) { |
| 237 | fOverrides = that->fOverrides; |
| 238 | } |
| 239 | |
| 240 | fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()); |
| 241 | this->joinBounds(that->bounds()); |
| 242 | return true; |
| 243 | } |
| 244 | |
| 245 | GrXPOverridesForBatch fOverrides; |
| 246 | SkSTArray<1, Geometry, true> fGeoData; |
| 247 | |
| 248 | typedef GrVertexBatch INHERITED; |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 249 | }; |
| 250 | |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 251 | // We handle perspective in the local matrix or viewmatrix with special batches |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 252 | class NonAAFillRectPerspectiveBatch : public GrVertexBatch { |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 253 | public: |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 254 | DEFINE_BATCH_CLASS_ID |
| 255 | |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 256 | struct Geometry { |
| 257 | SkMatrix fViewMatrix; |
| 258 | SkMatrix fLocalMatrix; |
| 259 | SkRect fRect; |
| 260 | SkRect fLocalRect; |
joshualitt | ae41b38 | 2015-08-19 06:54:08 -0700 | [diff] [blame] | 261 | GrColor fColor; |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 262 | bool fHasLocalMatrix; |
| 263 | bool fHasLocalRect; |
joshualitt | ae41b38 | 2015-08-19 06:54:08 -0700 | [diff] [blame] | 264 | }; |
| 265 | |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 266 | static NonAAFillRectPerspectiveBatch* Create() { return new NonAAFillRectPerspectiveBatch; } |
| 267 | |
| 268 | const char* name() const override { return Name(); } |
| 269 | |
| 270 | SkString dumpInfo() const override { |
| 271 | SkString str; |
| 272 | str.appendf("# batched: %d\n", fGeoData.count()); |
| 273 | for (int i = 0; i < fGeoData.count(); ++i) { |
| 274 | str.append(DumpInfo(fGeoData[i], i)); |
| 275 | } |
| 276 | str.append(INHERITED::dumpInfo()); |
| 277 | return str; |
| 278 | } |
| 279 | |
| 280 | void computePipelineOptimizations(GrInitInvariantOutput* color, |
| 281 | GrInitInvariantOutput* coverage, |
| 282 | GrBatchToXPOverrides* overrides) const override { |
| 283 | // When this is called on a batch, there is only one geometry bundle |
| 284 | color->setKnownFourComponents(fGeoData[0].fColor); |
| 285 | InitInvariantOutputCoverage(coverage); |
| 286 | } |
| 287 | |
| 288 | void initBatchTracker(const GrXPOverridesForBatch& overrides) override { |
| 289 | overrides.getOverrideColorIfSet(&fGeoData[0].fColor); |
| 290 | fOverrides = overrides; |
| 291 | } |
| 292 | |
| 293 | SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } |
| 294 | |
| 295 | // After seeding, the client should call init() so the Batch can initialize itself |
| 296 | void init() { |
| 297 | const Geometry& geo = fGeoData[0]; |
| 298 | SetBounds(geo, &fBounds); |
| 299 | } |
| 300 | |
| 301 | void updateBoundsAfterAppend() { |
| 302 | const Geometry& geo = fGeoData.back(); |
| 303 | UpdateBoundsAfterAppend(geo, &fBounds); |
| 304 | } |
| 305 | |
| 306 | private: |
| 307 | static const char* Name() { return "NonAAFillRectPerspectiveBatch"; } |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 308 | |
robertphillips | 783a4da | 2015-11-19 14:00:02 -0800 | [diff] [blame] | 309 | static SkString DumpInfo(const Geometry& geo, int index) { |
robertphillips | e004bfc | 2015-11-16 09:06:59 -0800 | [diff] [blame] | 310 | SkString str; |
robertphillips | 783a4da | 2015-11-19 14:00:02 -0800 | [diff] [blame] | 311 | str.appendf("%d: Color: 0x%08x, Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", |
| 312 | index, |
robertphillips | e004bfc | 2015-11-16 09:06:59 -0800 | [diff] [blame] | 313 | geo.fColor, |
| 314 | geo.fRect.fLeft, geo.fRect.fTop, geo.fRect.fRight, geo.fRect.fBottom); |
| 315 | return str; |
| 316 | } |
| 317 | |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 318 | static bool CanCombine(const Geometry& mine, const Geometry& theirs, |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 319 | const GrXPOverridesForBatch& overrides) { |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 320 | // We could batch across perspective vm changes if we really wanted to |
| 321 | return mine.fViewMatrix.cheapEqualTo(theirs.fViewMatrix) && |
joshualitt | 2120b6f | 2015-09-18 12:56:41 -0700 | [diff] [blame] | 322 | mine.fHasLocalRect == theirs.fHasLocalRect && |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 323 | (!mine.fHasLocalMatrix || mine.fLocalMatrix.cheapEqualTo(theirs.fLocalMatrix)); |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 324 | } |
| 325 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 326 | static sk_sp<GrGeometryProcessor> MakeGP(const Geometry& geo, |
| 327 | const GrXPOverridesForBatch& overrides) { |
| 328 | sk_sp<GrGeometryProcessor> gp = make_gp(geo.fViewMatrix, overrides.readsCoverage(), |
| 329 | geo.fHasLocalRect, |
| 330 | geo.fHasLocalMatrix ? &geo.fLocalMatrix |
| 331 | : nullptr); |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 332 | |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 333 | SkASSERT(geo.fHasLocalRect ? |
| 334 | gp->getVertexStride() == sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr) : |
| 335 | gp->getVertexStride() == sizeof(GrDefaultGeoProcFactory::PositionColorAttr)); |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 336 | return gp; |
| 337 | } |
| 338 | |
| 339 | static void Tesselate(intptr_t vertices, size_t vertexStride, const Geometry& geo, |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 340 | const GrXPOverridesForBatch& overrides) { |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 341 | if (geo.fHasLocalRect) { |
| 342 | GrQuad quad(geo.fLocalRect); |
| 343 | tesselate(vertices, vertexStride, geo.fColor, geo.fViewMatrix, geo.fRect, &quad); |
| 344 | } else { |
| 345 | tesselate(vertices, vertexStride, geo.fColor, geo.fViewMatrix, geo.fRect, nullptr); |
| 346 | } |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 347 | } |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 348 | |
| 349 | static void InitInvariantOutputCoverage(GrInitInvariantOutput* out) { |
| 350 | out->setKnownSingleComponent(0xff); |
| 351 | } |
| 352 | |
| 353 | static const GrBuffer* GetIndexBuffer(GrResourceProvider* rp) { |
| 354 | return rp->refQuadIndexBuffer(); |
| 355 | } |
| 356 | |
| 357 | static void SetBounds(const Geometry& geo, SkRect* outBounds) { |
| 358 | geo.fViewMatrix.mapRect(outBounds, geo.fRect); |
| 359 | } |
| 360 | |
| 361 | static void UpdateBoundsAfterAppend(const Geometry& geo, SkRect* outBounds) { |
| 362 | SkRect bounds = geo.fRect; |
| 363 | geo.fViewMatrix.mapRect(&bounds); |
| 364 | outBounds->join(bounds); |
| 365 | } |
| 366 | |
| 367 | NonAAFillRectPerspectiveBatch() : INHERITED(ClassID()) {} |
| 368 | |
| 369 | void onPrepareDraws(Target* target) const override { |
| 370 | sk_sp<GrGeometryProcessor> gp(MakeGP(this->seedGeometry(), fOverrides)); |
| 371 | if (!gp) { |
| 372 | SkDebugf("Couldn't create GrGeometryProcessor\n"); |
| 373 | return; |
| 374 | } |
| 375 | |
| 376 | size_t vertexStride = gp->getVertexStride(); |
| 377 | int instanceCount = fGeoData.count(); |
| 378 | |
| 379 | SkAutoTUnref<const GrBuffer> indexBuffer(GetIndexBuffer(target->resourceProvider())); |
| 380 | InstancedHelper helper; |
| 381 | void* vertices = helper.init(target, kTriangles_GrPrimitiveType, vertexStride, |
| 382 | indexBuffer, kVertsPerInstance, |
| 383 | kIndicesPerInstance, instanceCount); |
| 384 | if (!vertices || !indexBuffer) { |
| 385 | SkDebugf("Could not allocate vertices\n"); |
| 386 | return; |
| 387 | } |
| 388 | |
| 389 | for (int i = 0; i < instanceCount; i++) { |
| 390 | intptr_t verts = reinterpret_cast<intptr_t>(vertices) + |
| 391 | i * kVertsPerInstance * vertexStride; |
| 392 | Tesselate(verts, vertexStride, fGeoData[i], fOverrides); |
| 393 | } |
| 394 | helper.recordDraw(target, gp.get()); |
| 395 | } |
| 396 | |
| 397 | const Geometry& seedGeometry() const { return fGeoData[0]; } |
| 398 | |
| 399 | bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { |
| 400 | NonAAFillRectPerspectiveBatch* that = t->cast<NonAAFillRectPerspectiveBatch>(); |
| 401 | if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(), |
| 402 | that->bounds(), caps)) { |
| 403 | return false; |
| 404 | } |
| 405 | |
| 406 | if (!CanCombine(this->seedGeometry(), that->seedGeometry(), fOverrides)) { |
| 407 | return false; |
| 408 | } |
| 409 | |
| 410 | // In the event of two batches, one who can tweak, one who cannot, we just fall back to |
| 411 | // not tweaking |
| 412 | if (fOverrides.canTweakAlphaForCoverage() && !that->fOverrides.canTweakAlphaForCoverage()) { |
| 413 | fOverrides = that->fOverrides; |
| 414 | } |
| 415 | |
| 416 | fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()); |
| 417 | this->joinBounds(that->bounds()); |
| 418 | return true; |
| 419 | } |
| 420 | |
| 421 | GrXPOverridesForBatch fOverrides; |
| 422 | SkSTArray<1, Geometry, true> fGeoData; |
| 423 | |
| 424 | typedef GrVertexBatch INHERITED; |
joshualitt | ae41b38 | 2015-08-19 06:54:08 -0700 | [diff] [blame] | 425 | }; |
| 426 | |
bsalomon | b8cbd20 | 2016-06-30 13:09:48 -0700 | [diff] [blame] | 427 | namespace GrNonAAFillRectBatch { |
| 428 | |
| 429 | GrDrawBatch* Create(GrColor color, |
| 430 | const SkMatrix& viewMatrix, |
| 431 | const SkRect& rect, |
| 432 | const SkRect* localRect, |
| 433 | const SkMatrix* localMatrix) { |
| 434 | NonAAFillRectBatch* batch = NonAAFillRectBatch::Create(); |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 435 | SkASSERT(!viewMatrix.hasPerspective() && (!localMatrix || !localMatrix->hasPerspective())); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 436 | NonAAFillRectBatch::Geometry& geo = batch->geoData()->push_back(); |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 437 | |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 438 | geo.fColor = color; |
| 439 | geo.fViewMatrix = viewMatrix; |
| 440 | geo.fRect = rect; |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 441 | |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 442 | if (localRect && localMatrix) { |
| 443 | geo.fLocalQuad.setFromMappedRect(*localRect, *localMatrix); |
| 444 | } else if (localRect) { |
| 445 | geo.fLocalQuad.set(*localRect); |
| 446 | } else if (localMatrix) { |
| 447 | geo.fLocalQuad.setFromMappedRect(rect, *localMatrix); |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 448 | } else { |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 449 | geo.fLocalQuad.set(rect); |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 450 | } |
bsalomon | b8cbd20 | 2016-06-30 13:09:48 -0700 | [diff] [blame] | 451 | batch->init(); |
| 452 | return batch; |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 453 | } |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 454 | |
bsalomon | b8cbd20 | 2016-06-30 13:09:48 -0700 | [diff] [blame] | 455 | GrDrawBatch* CreateWithPerspective(GrColor color, |
| 456 | const SkMatrix& viewMatrix, |
| 457 | const SkRect& rect, |
| 458 | const SkRect* localRect, |
| 459 | const SkMatrix* localMatrix) { |
| 460 | NonAAFillRectPerspectiveBatch* batch = NonAAFillRectPerspectiveBatch::Create(); |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 461 | SkASSERT(viewMatrix.hasPerspective() || (localMatrix && localMatrix->hasPerspective())); |
bsalomon | 08d1415 | 2016-06-30 12:45:18 -0700 | [diff] [blame] | 462 | NonAAFillRectPerspectiveBatch::Geometry& geo = batch->geoData()->push_back(); |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 463 | |
| 464 | geo.fColor = color; |
| 465 | geo.fViewMatrix = viewMatrix; |
| 466 | geo.fRect = rect; |
| 467 | geo.fHasLocalRect = SkToBool(localRect); |
| 468 | geo.fHasLocalMatrix = SkToBool(localMatrix); |
| 469 | if (localMatrix) { |
| 470 | geo.fLocalMatrix = *localMatrix; |
| 471 | } |
| 472 | if (localRect) { |
| 473 | geo.fLocalRect = *localRect; |
| 474 | } |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 475 | batch->init(); |
| 476 | return batch; |
| 477 | } |
| 478 | |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 479 | }; |
| 480 | |
| 481 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 482 | |
| 483 | #ifdef GR_TEST_UTILS |
| 484 | |
| 485 | #include "GrBatchTest.h" |
| 486 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 487 | DRAW_BATCH_TEST_DEFINE(RectBatch) { |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 488 | GrColor color = GrRandomColor(random); |
| 489 | SkRect rect = GrTest::TestRect(random); |
| 490 | SkRect localRect = GrTest::TestRect(random); |
| 491 | SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random); |
| 492 | SkMatrix localMatrix = GrTest::TestMatrix(random); |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 493 | |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 494 | bool hasLocalRect = random->nextBool(); |
| 495 | bool hasLocalMatrix = random->nextBool(); |
joshualitt | bcf33d5 | 2015-08-26 08:10:35 -0700 | [diff] [blame] | 496 | return GrNonAAFillRectBatch::Create(color, viewMatrix, rect, |
| 497 | hasLocalRect ? &localRect : nullptr, |
| 498 | hasLocalMatrix ? &localMatrix : nullptr); |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | #endif |