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" |
| 15 | #include "GrTInstanceBatch.h" |
joshualitt | ae5b2c6 | 2015-08-19 08:48:41 -0700 | [diff] [blame] | 16 | #include "GrQuad.h" |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 17 | #include "GrVertexBatch.h" |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 18 | |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 19 | // Common functions |
joshualitt | bcf33d5 | 2015-08-26 08:10:35 -0700 | [diff] [blame^] | 20 | class NonAAFillRectBatchBase { |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 21 | public: |
| 22 | static const int kVertsPerInstance = 4; |
| 23 | static const int kIndicesPerInstance = 6; |
joshualitt | 87e4752 | 2015-08-20 07:22:42 -0700 | [diff] [blame] | 24 | |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 25 | static void InitInvariantOutputCoverage(GrInitInvariantOutput* out) { |
| 26 | out->setKnownSingleComponent(0xff); |
| 27 | } |
| 28 | |
| 29 | static const GrIndexBuffer* GetIndexBuffer(GrResourceProvider* rp) { |
| 30 | return rp->refQuadIndexBuffer(); |
| 31 | } |
| 32 | |
| 33 | template <typename Geometry> |
| 34 | static void SetBounds(const Geometry& geo, SkRect* outBounds) { |
| 35 | geo.fViewMatrix.mapRect(outBounds, geo.fRect); |
| 36 | } |
| 37 | }; |
| 38 | |
| 39 | /** We always use per-vertex colors so that rects can be batched across color changes. Sometimes |
| 40 | we have explicit local coords and sometimes not. We *could* always provide explicit local |
| 41 | coords and just duplicate the positions when the caller hasn't provided a local coord rect, |
| 42 | but we haven't seen a use case which frequently switches between local rect and no local |
| 43 | rect draws. |
| 44 | |
| 45 | The vertex attrib order is always pos, color, [local coords]. |
| 46 | */ |
| 47 | static const GrGeometryProcessor* create_gp(const SkMatrix& viewMatrix, |
| 48 | bool readsCoverage, |
| 49 | bool hasExplicitLocalCoords, |
| 50 | const SkMatrix* localMatrix) { |
| 51 | using namespace GrDefaultGeoProcFactory; |
| 52 | Color color(Color::kAttribute_Type); |
| 53 | Coverage coverage(readsCoverage ? Coverage::kSolid_Type : Coverage::kNone_Type); |
| 54 | |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 55 | // If we have perspective on the viewMatrix then we won't map on the CPU, nor will we map |
| 56 | // the local rect on the cpu (in case the localMatrix also has perspective). |
| 57 | // Otherwise, if we have a local rect, then we apply the localMatrix directly to the localRect |
| 58 | // to generate vertex local coords |
| 59 | if (viewMatrix.hasPerspective()) { |
| 60 | LocalCoords localCoords(hasExplicitLocalCoords ? LocalCoords::kHasExplicit_Type : |
| 61 | LocalCoords::kUsePosition_Type, |
| 62 | localMatrix); |
| 63 | return GrDefaultGeoProcFactory::Create(color, coverage, localCoords, viewMatrix); |
| 64 | } else if (hasExplicitLocalCoords) { |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 65 | LocalCoords localCoords(LocalCoords::kHasExplicit_Type); |
| 66 | return GrDefaultGeoProcFactory::Create(color, coverage, localCoords, SkMatrix::I()); |
| 67 | } else { |
| 68 | LocalCoords localCoords(LocalCoords::kUsePosition_Type, localMatrix); |
| 69 | return GrDefaultGeoProcFactory::CreateForDeviceSpace(color, coverage, localCoords, |
| 70 | viewMatrix); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | static void tesselate(intptr_t vertices, |
| 75 | size_t vertexStride, |
| 76 | GrColor color, |
| 77 | const SkMatrix& viewMatrix, |
| 78 | const SkRect& rect, |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 79 | const GrQuad* localQuad) { |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 80 | SkPoint* positions = reinterpret_cast<SkPoint*>(vertices); |
| 81 | |
| 82 | positions->setRectFan(rect.fLeft, rect.fTop, |
| 83 | rect.fRight, rect.fBottom, vertexStride); |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 84 | |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 85 | if (!viewMatrix.hasPerspective()) { |
| 86 | viewMatrix.mapPointsWithStride(positions, vertexStride, |
joshualitt | bcf33d5 | 2015-08-26 08:10:35 -0700 | [diff] [blame^] | 87 | NonAAFillRectBatchBase::kVertsPerInstance); |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | // Setup local coords |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 91 | // TODO we should only do this if local coords are being read |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 92 | if (localQuad) { |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 93 | static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor); |
joshualitt | bcf33d5 | 2015-08-26 08:10:35 -0700 | [diff] [blame^] | 94 | for (int i = 0; i < NonAAFillRectBatchBase::kVertsPerInstance; i++) { |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 95 | SkPoint* coords = reinterpret_cast<SkPoint*>(vertices + kLocalOffset + |
| 96 | i * vertexStride); |
| 97 | *coords = localQuad->point(i); |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
| 101 | static const int kColorOffset = sizeof(SkPoint); |
| 102 | GrColor* vertColor = reinterpret_cast<GrColor*>(vertices + kColorOffset); |
| 103 | for (int j = 0; j < 4; ++j) { |
| 104 | *vertColor = color; |
| 105 | vertColor = (GrColor*) ((intptr_t) vertColor + vertexStride); |
| 106 | } |
| 107 | } |
| 108 | |
joshualitt | bcf33d5 | 2015-08-26 08:10:35 -0700 | [diff] [blame^] | 109 | class NonAAFillRectBatchImp : public NonAAFillRectBatchBase { |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 110 | public: |
| 111 | struct Geometry { |
| 112 | SkMatrix fViewMatrix; |
| 113 | SkRect fRect; |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 114 | GrQuad fLocalQuad; |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 115 | GrColor fColor; |
| 116 | }; |
| 117 | |
joshualitt | bcf33d5 | 2015-08-26 08:10:35 -0700 | [diff] [blame^] | 118 | static const char* Name() { return "NonAAFillRectBatch"; } |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 119 | |
| 120 | static bool CanCombine(const Geometry& mine, const Geometry& theirs, |
| 121 | const GrPipelineOptimizations& opts) { |
joshualitt | 87e4752 | 2015-08-20 07:22:42 -0700 | [diff] [blame] | 122 | return true; |
| 123 | } |
| 124 | |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 125 | static const GrGeometryProcessor* CreateGP(const Geometry& geo, |
| 126 | const GrPipelineOptimizations& opts) { |
| 127 | const GrGeometryProcessor* gp = create_gp(geo.fViewMatrix, opts.readsCoverage(), true, |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 128 | nullptr); |
joshualitt | 87e4752 | 2015-08-20 07:22:42 -0700 | [diff] [blame] | 129 | |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 130 | SkASSERT(gp->getVertexStride() == |
| 131 | sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr)); |
| 132 | return gp; |
joshualitt | ae41b38 | 2015-08-19 06:54:08 -0700 | [diff] [blame] | 133 | } |
| 134 | |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 135 | static void Tesselate(intptr_t vertices, size_t vertexStride, const Geometry& geo, |
| 136 | const GrPipelineOptimizations& opts) { |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 137 | tesselate(vertices, vertexStride, geo.fColor, geo.fViewMatrix, geo.fRect, &geo.fLocalQuad); |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 138 | } |
| 139 | }; |
| 140 | |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 141 | // We handle perspective in the local matrix or viewmatrix with special batches |
joshualitt | bcf33d5 | 2015-08-26 08:10:35 -0700 | [diff] [blame^] | 142 | class NonAAFillRectBatchPerspectiveImp : public NonAAFillRectBatchBase { |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 143 | public: |
| 144 | struct Geometry { |
| 145 | SkMatrix fViewMatrix; |
| 146 | SkMatrix fLocalMatrix; |
| 147 | SkRect fRect; |
| 148 | SkRect fLocalRect; |
joshualitt | ae41b38 | 2015-08-19 06:54:08 -0700 | [diff] [blame] | 149 | GrColor fColor; |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 150 | bool fHasLocalMatrix; |
| 151 | bool fHasLocalRect; |
joshualitt | ae41b38 | 2015-08-19 06:54:08 -0700 | [diff] [blame] | 152 | }; |
| 153 | |
joshualitt | bcf33d5 | 2015-08-26 08:10:35 -0700 | [diff] [blame^] | 154 | static const char* Name() { return "NonAAFillRectBatchPerspective"; } |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 155 | |
| 156 | static bool CanCombine(const Geometry& mine, const Geometry& theirs, |
| 157 | const GrPipelineOptimizations& opts) { |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 158 | // We could batch across perspective vm changes if we really wanted to |
| 159 | return mine.fViewMatrix.cheapEqualTo(theirs.fViewMatrix) && |
| 160 | (!mine.fHasLocalMatrix || mine.fLocalMatrix.cheapEqualTo(theirs.fLocalMatrix)); |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | static const GrGeometryProcessor* CreateGP(const Geometry& geo, |
| 164 | const GrPipelineOptimizations& opts) { |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 165 | const GrGeometryProcessor* gp = create_gp(geo.fViewMatrix, opts.readsCoverage(), |
| 166 | geo.fHasLocalRect, |
| 167 | geo.fHasLocalMatrix ? &geo.fLocalMatrix : |
| 168 | nullptr); |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 169 | |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 170 | SkASSERT(geo.fHasLocalRect ? |
| 171 | gp->getVertexStride() == sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr) : |
| 172 | gp->getVertexStride() == sizeof(GrDefaultGeoProcFactory::PositionColorAttr)); |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 173 | return gp; |
| 174 | } |
| 175 | |
| 176 | static void Tesselate(intptr_t vertices, size_t vertexStride, const Geometry& geo, |
| 177 | const GrPipelineOptimizations& opts) { |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 178 | if (geo.fHasLocalRect) { |
| 179 | GrQuad quad(geo.fLocalRect); |
| 180 | tesselate(vertices, vertexStride, geo.fColor, geo.fViewMatrix, geo.fRect, &quad); |
| 181 | } else { |
| 182 | tesselate(vertices, vertexStride, geo.fColor, geo.fViewMatrix, geo.fRect, nullptr); |
| 183 | } |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 184 | } |
joshualitt | ae41b38 | 2015-08-19 06:54:08 -0700 | [diff] [blame] | 185 | }; |
| 186 | |
joshualitt | bcf33d5 | 2015-08-26 08:10:35 -0700 | [diff] [blame^] | 187 | typedef GrTInstanceBatch<NonAAFillRectBatchImp> NonAAFillRectBatchSimple; |
| 188 | typedef GrTInstanceBatch<NonAAFillRectBatchPerspectiveImp> NonAAFillRectBatchPerspective; |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 189 | |
joshualitt | bcf33d5 | 2015-08-26 08:10:35 -0700 | [diff] [blame^] | 190 | namespace GrNonAAFillRectBatch { |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 191 | GrDrawBatch* Create(GrColor color, |
| 192 | const SkMatrix& viewMatrix, |
| 193 | const SkRect& rect, |
| 194 | const SkRect* localRect, |
| 195 | const SkMatrix* localMatrix) { |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 196 | |
| 197 | /* Perspective has to be handled in a slow path for now */ |
| 198 | if (viewMatrix.hasPerspective() || (localMatrix && localMatrix->hasPerspective())) { |
joshualitt | bcf33d5 | 2015-08-26 08:10:35 -0700 | [diff] [blame^] | 199 | NonAAFillRectBatchPerspective* batch = NonAAFillRectBatchPerspective::Create(); |
| 200 | NonAAFillRectBatchPerspective::Geometry& geo = *batch->geometry(); |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 201 | |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 202 | geo.fColor = color; |
| 203 | geo.fViewMatrix = viewMatrix; |
| 204 | geo.fRect = rect; |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 205 | geo.fHasLocalRect = SkToBool(localRect); |
| 206 | geo.fHasLocalMatrix = SkToBool(localMatrix); |
| 207 | if (localMatrix) { |
| 208 | geo.fLocalMatrix = *localMatrix; |
| 209 | } |
| 210 | if (localRect) { |
| 211 | geo.fLocalRect = *localRect; |
| 212 | } |
| 213 | |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 214 | batch->init(); |
| 215 | return batch; |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 216 | } else { |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 217 | // TODO bubble these up as separate calls |
joshualitt | bcf33d5 | 2015-08-26 08:10:35 -0700 | [diff] [blame^] | 218 | NonAAFillRectBatchSimple* batch = NonAAFillRectBatchSimple::Create(); |
| 219 | NonAAFillRectBatchSimple::Geometry& geo = *batch->geometry(); |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 220 | |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 221 | geo.fColor = color; |
| 222 | geo.fViewMatrix = viewMatrix; |
| 223 | geo.fRect = rect; |
joshualitt | 8cce8f1 | 2015-08-26 06:23:39 -0700 | [diff] [blame] | 224 | |
| 225 | if (localRect && localMatrix) { |
| 226 | geo.fLocalQuad.setFromMappedRect(*localRect, *localMatrix); |
| 227 | } else if (localRect) { |
| 228 | geo.fLocalQuad.set(*localRect); |
| 229 | } else if (localMatrix) { |
| 230 | geo.fLocalQuad.setFromMappedRect(rect, *localMatrix); |
| 231 | } else { |
| 232 | geo.fLocalQuad.set(rect); |
| 233 | } |
| 234 | |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 235 | batch->init(); |
| 236 | return batch; |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 237 | } |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 238 | } |
| 239 | }; |
| 240 | |
| 241 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 242 | |
| 243 | #ifdef GR_TEST_UTILS |
| 244 | |
| 245 | #include "GrBatchTest.h" |
| 246 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 247 | DRAW_BATCH_TEST_DEFINE(RectBatch) { |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 248 | GrColor color = GrRandomColor(random); |
| 249 | SkRect rect = GrTest::TestRect(random); |
| 250 | SkRect localRect = GrTest::TestRect(random); |
| 251 | SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random); |
| 252 | SkMatrix localMatrix = GrTest::TestMatrix(random); |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 253 | |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 254 | bool hasLocalRect = random->nextBool(); |
| 255 | bool hasLocalMatrix = random->nextBool(); |
joshualitt | bcf33d5 | 2015-08-26 08:10:35 -0700 | [diff] [blame^] | 256 | return GrNonAAFillRectBatch::Create(color, viewMatrix, rect, |
| 257 | hasLocalRect ? &localRect : nullptr, |
| 258 | hasLocalMatrix ? &localMatrix : nullptr); |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | #endif |