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 | |
| 8 | #include "GrBWFillRectBatch.h" |
| 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 |
| 20 | class BWFillRectBatchBase { |
| 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 | 26db32b | 2015-08-24 10:26:01 -0700 | [diff] [blame] | 55 | // if we have a local rect, then we apply the localMatrix directly to the localRect to |
| 56 | // generate vertex local coords |
| 57 | if (hasExplicitLocalCoords) { |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 58 | LocalCoords localCoords(LocalCoords::kHasExplicit_Type); |
| 59 | return GrDefaultGeoProcFactory::Create(color, coverage, localCoords, SkMatrix::I()); |
| 60 | } else { |
| 61 | LocalCoords localCoords(LocalCoords::kUsePosition_Type, localMatrix); |
| 62 | return GrDefaultGeoProcFactory::CreateForDeviceSpace(color, coverage, localCoords, |
| 63 | viewMatrix); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | static void tesselate(intptr_t vertices, |
| 68 | size_t vertexStride, |
| 69 | GrColor color, |
| 70 | const SkMatrix& viewMatrix, |
| 71 | const SkRect& rect, |
joshualitt | 26db32b | 2015-08-24 10:26:01 -0700 | [diff] [blame] | 72 | const SkRect* localRect, |
| 73 | const SkMatrix* localMatrix) { |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 74 | SkPoint* positions = reinterpret_cast<SkPoint*>(vertices); |
| 75 | |
| 76 | positions->setRectFan(rect.fLeft, rect.fTop, |
| 77 | rect.fRight, rect.fBottom, vertexStride); |
joshualitt | 26db32b | 2015-08-24 10:26:01 -0700 | [diff] [blame] | 78 | viewMatrix.mapPointsWithStride(positions, vertexStride, BWFillRectBatchBase::kVertsPerInstance); |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 79 | |
| 80 | // TODO we should only do this if local coords are being read |
joshualitt | 26db32b | 2015-08-24 10:26:01 -0700 | [diff] [blame] | 81 | if (localRect) { |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 82 | static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor); |
joshualitt | 26db32b | 2015-08-24 10:26:01 -0700 | [diff] [blame] | 83 | SkPoint* coords = reinterpret_cast<SkPoint*>(vertices + kLocalOffset); |
| 84 | coords->setRectFan(localRect->fLeft, localRect->fTop, |
| 85 | localRect->fRight, localRect->fBottom, |
| 86 | vertexStride); |
| 87 | if (localMatrix) { |
| 88 | localMatrix->mapPointsWithStride(coords, vertexStride, |
| 89 | BWFillRectBatchBase::kVertsPerInstance); |
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 | |
| 101 | class BWFillRectBatchNoLocalMatrixImp : public BWFillRectBatchBase { |
| 102 | public: |
| 103 | struct Geometry { |
| 104 | SkMatrix fViewMatrix; |
| 105 | SkRect fRect; |
| 106 | GrColor fColor; |
| 107 | }; |
| 108 | |
| 109 | static const char* Name() { return "BWFillRectBatchNoLocalMatrix"; } |
| 110 | |
| 111 | static bool CanCombine(const Geometry& mine, const Geometry& theirs, |
| 112 | const GrPipelineOptimizations& opts) { |
joshualitt | 26db32b | 2015-08-24 10:26:01 -0700 | [diff] [blame] | 113 | // We apply the viewmatrix to the rect points on the cpu. However, if the pipeline uses |
| 114 | // local coords then we won't be able to batch. We could actually upload the viewmatrix |
| 115 | // using vertex attributes in these cases, but haven't investigated that |
| 116 | return !opts.readsLocalCoords() || mine.fViewMatrix.cheapEqualTo(theirs.fViewMatrix); |
| 117 | } |
| 118 | |
| 119 | static const GrGeometryProcessor* CreateGP(const Geometry& geo, |
| 120 | const GrPipelineOptimizations& opts) { |
| 121 | const GrGeometryProcessor* gp = create_gp(geo.fViewMatrix, opts.readsCoverage(), false, |
| 122 | NULL); |
| 123 | |
| 124 | SkASSERT(gp->getVertexStride() == sizeof(GrDefaultGeoProcFactory::PositionColorAttr)); |
| 125 | return gp; |
| 126 | } |
| 127 | |
| 128 | static void Tesselate(intptr_t vertices, size_t vertexStride, const Geometry& geo, |
| 129 | const GrPipelineOptimizations& opts) { |
| 130 | tesselate(vertices, vertexStride, geo.fColor, geo.fViewMatrix, geo.fRect, NULL, NULL); |
| 131 | } |
| 132 | }; |
| 133 | |
| 134 | class BWFillRectBatchLocalMatrixImp : public BWFillRectBatchBase { |
| 135 | public: |
| 136 | struct Geometry { |
| 137 | SkMatrix fViewMatrix; |
| 138 | SkMatrix fLocalMatrix; |
| 139 | SkRect fRect; |
| 140 | GrColor fColor; |
| 141 | }; |
| 142 | |
| 143 | static const char* Name() { return "BWFillRectBatchLocalMatrix"; } |
| 144 | |
| 145 | static bool CanCombine(const Geometry& mine, const Geometry& theirs, |
| 146 | const GrPipelineOptimizations& opts) { |
| 147 | // if we read local coords then we have to have the same viewmatrix and localmatrix |
| 148 | return !opts.readsLocalCoords() || |
| 149 | (mine.fViewMatrix.cheapEqualTo(theirs.fViewMatrix) && |
| 150 | mine.fLocalMatrix.cheapEqualTo(theirs.fLocalMatrix)); |
| 151 | } |
| 152 | |
| 153 | static const GrGeometryProcessor* CreateGP(const Geometry& geo, |
| 154 | const GrPipelineOptimizations& opts) { |
| 155 | const GrGeometryProcessor* gp = create_gp(geo.fViewMatrix, opts.readsCoverage(), false, |
| 156 | &geo.fLocalMatrix); |
| 157 | |
| 158 | SkASSERT(gp->getVertexStride() == sizeof(GrDefaultGeoProcFactory::PositionColorAttr)); |
| 159 | return gp; |
| 160 | } |
| 161 | |
| 162 | static void Tesselate(intptr_t vertices, size_t vertexStride, const Geometry& geo, |
| 163 | const GrPipelineOptimizations& opts) { |
| 164 | tesselate(vertices, vertexStride, geo.fColor, geo.fViewMatrix, geo.fRect, NULL, |
| 165 | &geo.fLocalMatrix); |
| 166 | } |
| 167 | }; |
| 168 | |
| 169 | class BWFillRectBatchLocalRectImp : public BWFillRectBatchBase { |
| 170 | public: |
| 171 | struct Geometry { |
| 172 | SkMatrix fViewMatrix; |
| 173 | SkRect fRect; |
| 174 | SkRect fLocalRect; |
| 175 | GrColor fColor; |
| 176 | }; |
| 177 | |
| 178 | static const char* Name() { return "BWFillRectBatchLocalRect"; } |
| 179 | |
| 180 | static bool CanCombine(const Geometry& mine, const Geometry& theirs, |
| 181 | const GrPipelineOptimizations& opts) { |
joshualitt | 87e4752 | 2015-08-20 07:22:42 -0700 | [diff] [blame] | 182 | return true; |
| 183 | } |
| 184 | |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 185 | static const GrGeometryProcessor* CreateGP(const Geometry& geo, |
| 186 | const GrPipelineOptimizations& opts) { |
| 187 | const GrGeometryProcessor* gp = create_gp(geo.fViewMatrix, opts.readsCoverage(), true, |
joshualitt | 26db32b | 2015-08-24 10:26:01 -0700 | [diff] [blame] | 188 | NULL); |
joshualitt | 87e4752 | 2015-08-20 07:22:42 -0700 | [diff] [blame] | 189 | |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 190 | SkASSERT(gp->getVertexStride() == |
| 191 | sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr)); |
| 192 | return gp; |
joshualitt | ae41b38 | 2015-08-19 06:54:08 -0700 | [diff] [blame] | 193 | } |
| 194 | |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 195 | static void Tesselate(intptr_t vertices, size_t vertexStride, const Geometry& geo, |
| 196 | const GrPipelineOptimizations& opts) { |
joshualitt | 26db32b | 2015-08-24 10:26:01 -0700 | [diff] [blame] | 197 | tesselate(vertices, vertexStride, geo.fColor, geo.fViewMatrix, geo.fRect, &geo.fLocalRect, |
| 198 | NULL); |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 199 | } |
| 200 | }; |
| 201 | |
joshualitt | 26db32b | 2015-08-24 10:26:01 -0700 | [diff] [blame] | 202 | class BWFillRectBatchLocalMatrixLocalRectImp : public BWFillRectBatchBase { |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 203 | public: |
| 204 | struct Geometry { |
| 205 | SkMatrix fViewMatrix; |
| 206 | SkMatrix fLocalMatrix; |
| 207 | SkRect fRect; |
| 208 | SkRect fLocalRect; |
joshualitt | ae41b38 | 2015-08-19 06:54:08 -0700 | [diff] [blame] | 209 | GrColor fColor; |
| 210 | }; |
| 211 | |
joshualitt | 26db32b | 2015-08-24 10:26:01 -0700 | [diff] [blame] | 212 | static const char* Name() { return "BWFillRectBatchLocalMatrixLocalRect"; } |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 213 | |
| 214 | static bool CanCombine(const Geometry& mine, const Geometry& theirs, |
| 215 | const GrPipelineOptimizations& opts) { |
joshualitt | 26db32b | 2015-08-24 10:26:01 -0700 | [diff] [blame] | 216 | return true; |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | static const GrGeometryProcessor* CreateGP(const Geometry& geo, |
| 220 | const GrPipelineOptimizations& opts) { |
joshualitt | 26db32b | 2015-08-24 10:26:01 -0700 | [diff] [blame] | 221 | const GrGeometryProcessor* gp = create_gp(geo.fViewMatrix, opts.readsCoverage(), true, |
| 222 | NULL); |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 223 | |
joshualitt | 26db32b | 2015-08-24 10:26:01 -0700 | [diff] [blame] | 224 | SkASSERT(gp->getVertexStride() == |
| 225 | sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr)); |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 226 | return gp; |
| 227 | } |
| 228 | |
| 229 | static void Tesselate(intptr_t vertices, size_t vertexStride, const Geometry& geo, |
| 230 | const GrPipelineOptimizations& opts) { |
joshualitt | 26db32b | 2015-08-24 10:26:01 -0700 | [diff] [blame] | 231 | tesselate(vertices, vertexStride, geo.fColor, geo.fViewMatrix, geo.fRect, &geo.fLocalRect, |
| 232 | &geo.fLocalMatrix); |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 233 | } |
joshualitt | ae41b38 | 2015-08-19 06:54:08 -0700 | [diff] [blame] | 234 | }; |
| 235 | |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 236 | typedef GrTInstanceBatch<BWFillRectBatchNoLocalMatrixImp> BWFillRectBatchSimple; |
joshualitt | 26db32b | 2015-08-24 10:26:01 -0700 | [diff] [blame] | 237 | typedef GrTInstanceBatch<BWFillRectBatchLocalMatrixImp> BWFillRectBatchLocalMatrix; |
| 238 | typedef GrTInstanceBatch<BWFillRectBatchLocalRectImp> BWFillRectBatchLocalRect; |
| 239 | typedef GrTInstanceBatch<BWFillRectBatchLocalMatrixLocalRectImp> BWFillRectBatchLocalMatrixLocalRect; |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 240 | |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 241 | namespace GrBWFillRectBatch { |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 242 | GrDrawBatch* Create(GrColor color, |
| 243 | const SkMatrix& viewMatrix, |
| 244 | const SkRect& rect, |
| 245 | const SkRect* localRect, |
| 246 | const SkMatrix* localMatrix) { |
joshualitt | 26db32b | 2015-08-24 10:26:01 -0700 | [diff] [blame] | 247 | // TODO bubble these up as separate calls |
| 248 | if (localRect && localMatrix) { |
| 249 | BWFillRectBatchLocalMatrixLocalRect* batch = BWFillRectBatchLocalMatrixLocalRect::Create(); |
| 250 | BWFillRectBatchLocalMatrixLocalRect::Geometry& geo = *batch->geometry(); |
| 251 | geo.fColor = color; |
| 252 | geo.fViewMatrix = viewMatrix; |
| 253 | geo.fLocalMatrix = *localMatrix; |
| 254 | geo.fRect = rect; |
| 255 | geo.fLocalRect = *localRect; |
| 256 | batch->init(); |
| 257 | return batch; |
| 258 | } else if (localRect) { |
| 259 | BWFillRectBatchLocalRect* batch = BWFillRectBatchLocalRect::Create(); |
| 260 | BWFillRectBatchLocalRect::Geometry& geo = *batch->geometry(); |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 261 | geo.fColor = color; |
| 262 | geo.fViewMatrix = viewMatrix; |
| 263 | geo.fRect = rect; |
joshualitt | 26db32b | 2015-08-24 10:26:01 -0700 | [diff] [blame] | 264 | geo.fLocalRect = *localRect; |
| 265 | batch->init(); |
| 266 | return batch; |
| 267 | } else if (localMatrix) { |
| 268 | BWFillRectBatchLocalMatrix* batch = BWFillRectBatchLocalMatrix::Create(); |
| 269 | BWFillRectBatchLocalMatrix::Geometry& geo = *batch->geometry(); |
| 270 | geo.fColor = color; |
| 271 | geo.fViewMatrix = viewMatrix; |
| 272 | geo.fLocalMatrix = *localMatrix; |
| 273 | geo.fRect = rect; |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 274 | batch->init(); |
| 275 | return batch; |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 276 | } else { |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 277 | BWFillRectBatchSimple* batch = BWFillRectBatchSimple::Create(); |
| 278 | BWFillRectBatchSimple::Geometry& geo = *batch->geometry(); |
| 279 | geo.fColor = color; |
| 280 | geo.fViewMatrix = viewMatrix; |
| 281 | geo.fRect = rect; |
| 282 | batch->init(); |
| 283 | return batch; |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 284 | } |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 285 | } |
| 286 | }; |
| 287 | |
| 288 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 289 | |
| 290 | #ifdef GR_TEST_UTILS |
| 291 | |
| 292 | #include "GrBatchTest.h" |
| 293 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 294 | DRAW_BATCH_TEST_DEFINE(RectBatch) { |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 295 | GrColor color = GrRandomColor(random); |
| 296 | SkRect rect = GrTest::TestRect(random); |
| 297 | SkRect localRect = GrTest::TestRect(random); |
| 298 | SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random); |
| 299 | SkMatrix localMatrix = GrTest::TestMatrix(random); |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 300 | |
joshualitt | 2244c27 | 2015-08-21 10:33:15 -0700 | [diff] [blame] | 301 | bool hasLocalRect = random->nextBool(); |
| 302 | bool hasLocalMatrix = random->nextBool(); |
| 303 | return GrBWFillRectBatch::Create(color, viewMatrix, rect, hasLocalRect ? &localRect : nullptr, |
| 304 | hasLocalMatrix ? &localMatrix : nullptr); |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | #endif |