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 | ae41b38 | 2015-08-19 06:54:08 -0700 | [diff] [blame^] | 14 | #include "GrResourceProvider.h" |
| 15 | #include "GrTInstanceBatch.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 | |
joshualitt | ae41b38 | 2015-08-19 06:54:08 -0700 | [diff] [blame^] | 18 | // Common functions |
| 19 | class BWFillRectBatchBase { |
| 20 | public: |
| 21 | static const int kVertsPerInstance = 4; |
| 22 | static const int kIndicesPerInstance = 6; |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 23 | |
joshualitt | ae41b38 | 2015-08-19 06:54:08 -0700 | [diff] [blame^] | 24 | static const GrIndexBuffer* GetIndexBuffer(GrResourceProvider* rp) { |
| 25 | return rp->refQuadIndexBuffer(); |
| 26 | } |
| 27 | |
| 28 | template <typename Geometry> |
| 29 | static void SetBounds(const Geometry& geo, SkRect* outBounds) { |
| 30 | geo.fViewMatrix.mapRect(outBounds, geo.fRect); |
| 31 | } |
| 32 | }; |
| 33 | |
| 34 | /** We always use per-vertex colors so that rects can be batched across color changes. Sometimes |
| 35 | we have explicit local coords and sometimes not. We *could* always provide explicit local |
| 36 | coords and just duplicate the positions when the caller hasn't provided a local coord rect, |
| 37 | but we haven't seen a use case which frequently switches between local rect and no local |
| 38 | rect draws. |
| 39 | |
| 40 | The color param is used to determine whether the opaque hint can be set on the draw state. |
| 41 | The caller must populate the vertex colors itself. |
| 42 | |
| 43 | The vertex attrib order is always pos, color, [local coords]. |
| 44 | */ |
| 45 | static const GrGeometryProcessor* create_gp(const SkMatrix& viewMatrix, |
| 46 | bool readsCoverage, |
| 47 | bool hasExplicitLocalCoords, |
| 48 | const SkMatrix* localMatrix) { |
| 49 | using namespace GrDefaultGeoProcFactory; |
| 50 | Color color(Color::kAttribute_Type); |
| 51 | Coverage coverage(readsCoverage ? Coverage::kSolid_Type : Coverage::kNone_Type); |
| 52 | |
| 53 | // if we have a local rect, then we apply the localMatrix directly to the localRect to |
| 54 | // generate vertex local coords |
| 55 | if (hasExplicitLocalCoords) { |
| 56 | LocalCoords localCoords(LocalCoords::kHasExplicit_Type); |
| 57 | return GrDefaultGeoProcFactory::Create(color, coverage, localCoords, SkMatrix::I()); |
| 58 | } else { |
| 59 | LocalCoords localCoords(LocalCoords::kUsePosition_Type, localMatrix ? localMatrix : NULL); |
| 60 | return GrDefaultGeoProcFactory::CreateForDeviceSpace(color, coverage, localCoords, |
| 61 | viewMatrix); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | static void tesselate(intptr_t vertices, |
| 66 | size_t vertexStride, |
| 67 | GrColor color, |
| 68 | const SkMatrix& viewMatrix, |
| 69 | const SkRect& rect, |
| 70 | const SkRect* localRect, |
| 71 | const SkMatrix* localMatrix) { |
| 72 | SkPoint* positions = reinterpret_cast<SkPoint*>(vertices); |
| 73 | |
| 74 | positions->setRectFan(rect.fLeft, rect.fTop, |
| 75 | rect.fRight, rect.fBottom, vertexStride); |
| 76 | viewMatrix.mapPointsWithStride(positions, vertexStride, BWFillRectBatchBase::kVertsPerInstance); |
| 77 | |
| 78 | // TODO we should only do this if local coords are being read |
| 79 | if (localRect) { |
| 80 | static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor); |
| 81 | SkPoint* coords = reinterpret_cast<SkPoint*>(vertices + kLocalOffset); |
| 82 | coords->setRectFan(localRect->fLeft, localRect->fTop, |
| 83 | localRect->fRight, localRect->fBottom, |
| 84 | vertexStride); |
| 85 | if (localMatrix) { |
| 86 | localMatrix->mapPointsWithStride(coords, vertexStride, |
| 87 | BWFillRectBatchBase::kVertsPerInstance); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | static const int kColorOffset = sizeof(SkPoint); |
| 92 | GrColor* vertColor = reinterpret_cast<GrColor*>(vertices + kColorOffset); |
| 93 | for (int j = 0; j < 4; ++j) { |
| 94 | *vertColor = color; |
| 95 | vertColor = (GrColor*) ((intptr_t) vertColor + vertexStride); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | class BWFillRectBatchNoLocalMatrixImp : public BWFillRectBatchBase { |
| 100 | public: |
| 101 | struct Geometry { |
| 102 | SkMatrix fViewMatrix; |
| 103 | SkRect fRect; |
| 104 | GrColor fColor; |
| 105 | }; |
| 106 | |
| 107 | static const char* Name() { return "BWFillRectBatchNoLocalMatrix"; } |
| 108 | |
| 109 | static bool CanCombine(const Geometry& mine, const Geometry& theirs, |
| 110 | const GrPipelineOptimizations& opts) { |
| 111 | // We apply the viewmatrix to the rect points on the cpu. However, if the pipeline uses |
| 112 | // local coords then we won't be able to batch. We could actually upload the viewmatrix |
| 113 | // using vertex attributes in these cases, but haven't investigated that |
| 114 | return !opts.readsLocalCoords() || mine.fViewMatrix.cheapEqualTo(theirs.fViewMatrix); |
| 115 | } |
| 116 | |
| 117 | static const GrGeometryProcessor* CreateGP(const Geometry& geo, |
| 118 | const GrPipelineOptimizations& opts) { |
| 119 | const GrGeometryProcessor* gp = create_gp(geo.fViewMatrix, opts.readsCoverage(), false, |
| 120 | NULL); |
| 121 | |
| 122 | SkASSERT(gp->getVertexStride() == sizeof(GrDefaultGeoProcFactory::PositionColorAttr)); |
| 123 | return gp; |
| 124 | } |
| 125 | |
| 126 | static void Tesselate(intptr_t vertices, size_t vertexStride, const Geometry& geo, |
| 127 | const GrPipelineOptimizations& opts) { |
| 128 | tesselate(vertices, vertexStride, geo.fColor, geo.fViewMatrix, geo.fRect, NULL, NULL); |
| 129 | } |
| 130 | }; |
| 131 | |
| 132 | class BWFillRectBatchLocalMatrixImp : public BWFillRectBatchBase { |
| 133 | public: |
| 134 | struct Geometry { |
| 135 | SkMatrix fViewMatrix; |
| 136 | SkMatrix fLocalMatrix; |
| 137 | SkRect fRect; |
| 138 | GrColor fColor; |
| 139 | }; |
| 140 | |
| 141 | static const char* Name() { return "BWFillRectBatchLocalMatrix"; } |
| 142 | |
| 143 | static bool CanCombine(const Geometry& mine, const Geometry& theirs, |
| 144 | const GrPipelineOptimizations& opts) { |
| 145 | // We apply the viewmatrix to the rect points on the cpu. However, if the pipeline uses |
| 146 | // local coords then we won't be able to batch. We could actually upload the viewmatrix |
| 147 | // using vertex attributes in these cases, but haven't investigated that |
| 148 | return !opts.readsLocalCoords() || mine.fViewMatrix.cheapEqualTo(theirs.fViewMatrix); |
| 149 | } |
| 150 | |
| 151 | static const GrGeometryProcessor* CreateGP(const Geometry& geo, |
| 152 | const GrPipelineOptimizations& opts) { |
| 153 | const GrGeometryProcessor* gp = create_gp(geo.fViewMatrix, opts.readsCoverage(), false, |
| 154 | &geo.fLocalMatrix); |
| 155 | |
| 156 | SkASSERT(gp->getVertexStride() == sizeof(GrDefaultGeoProcFactory::PositionColorAttr)); |
| 157 | return gp; |
| 158 | } |
| 159 | |
| 160 | static void Tesselate(intptr_t vertices, size_t vertexStride, const Geometry& geo, |
| 161 | const GrPipelineOptimizations& opts) { |
| 162 | tesselate(vertices, vertexStride, geo.fColor, geo.fViewMatrix, geo.fRect, NULL, |
| 163 | &geo.fLocalMatrix); |
| 164 | } |
| 165 | }; |
| 166 | |
| 167 | class BWFillRectBatchLocalRectImp : public BWFillRectBatchBase { |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 168 | public: |
| 169 | struct Geometry { |
| 170 | SkMatrix fViewMatrix; |
| 171 | SkRect fRect; |
| 172 | SkRect fLocalRect; |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 173 | GrColor fColor; |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 174 | }; |
| 175 | |
joshualitt | ae41b38 | 2015-08-19 06:54:08 -0700 | [diff] [blame^] | 176 | static const char* Name() { return "BWFillRectBatchLocalRect"; } |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 177 | |
joshualitt | ae41b38 | 2015-08-19 06:54:08 -0700 | [diff] [blame^] | 178 | static bool CanCombine(const Geometry& mine, const Geometry& theirs, |
| 179 | const GrPipelineOptimizations& opts) { |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 180 | return true; |
| 181 | } |
| 182 | |
joshualitt | ae41b38 | 2015-08-19 06:54:08 -0700 | [diff] [blame^] | 183 | static const GrGeometryProcessor* CreateGP(const Geometry& geo, |
| 184 | const GrPipelineOptimizations& opts) { |
| 185 | const GrGeometryProcessor* gp = create_gp(geo.fViewMatrix, opts.readsCoverage(), true, |
| 186 | NULL); |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 187 | |
joshualitt | ae41b38 | 2015-08-19 06:54:08 -0700 | [diff] [blame^] | 188 | SkASSERT(gp->getVertexStride() == |
| 189 | sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr)); |
| 190 | return gp; |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 191 | } |
| 192 | |
joshualitt | ae41b38 | 2015-08-19 06:54:08 -0700 | [diff] [blame^] | 193 | static void Tesselate(intptr_t vertices, size_t vertexStride, const Geometry& geo, |
| 194 | const GrPipelineOptimizations& opts) { |
| 195 | tesselate(vertices, vertexStride, geo.fColor, geo.fViewMatrix, geo.fRect, &geo.fLocalRect, |
| 196 | NULL); |
| 197 | } |
| 198 | }; |
| 199 | |
| 200 | class BWFillRectBatchLocalMatrixLocalRectImp : public BWFillRectBatchBase { |
| 201 | public: |
| 202 | struct Geometry { |
| 203 | SkMatrix fViewMatrix; |
| 204 | SkMatrix fLocalMatrix; |
| 205 | SkRect fRect; |
| 206 | SkRect fLocalRect; |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 207 | GrColor fColor; |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 208 | }; |
| 209 | |
joshualitt | ae41b38 | 2015-08-19 06:54:08 -0700 | [diff] [blame^] | 210 | static const char* Name() { return "BWFillRectBatchLocalMatrixLocalRect"; } |
| 211 | |
| 212 | static bool CanCombine(const Geometry& mine, const Geometry& theirs, |
| 213 | const GrPipelineOptimizations& opts) { |
| 214 | return true; |
| 215 | } |
| 216 | |
| 217 | static const GrGeometryProcessor* CreateGP(const Geometry& geo, |
| 218 | const GrPipelineOptimizations& opts) { |
| 219 | const GrGeometryProcessor* gp = create_gp(geo.fViewMatrix, opts.readsCoverage(), true, |
| 220 | NULL); |
| 221 | |
| 222 | SkASSERT(gp->getVertexStride() == |
| 223 | sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr)); |
| 224 | return gp; |
| 225 | } |
| 226 | |
| 227 | static void Tesselate(intptr_t vertices, size_t vertexStride, const Geometry& geo, |
| 228 | const GrPipelineOptimizations& opts) { |
| 229 | tesselate(vertices, vertexStride, geo.fColor, geo.fViewMatrix, geo.fRect, &geo.fLocalRect, |
| 230 | &geo.fLocalMatrix); |
| 231 | } |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 232 | }; |
| 233 | |
joshualitt | ae41b38 | 2015-08-19 06:54:08 -0700 | [diff] [blame^] | 234 | typedef GrTInstanceBatch<BWFillRectBatchNoLocalMatrixImp> BWFillRectBatchSimple; |
| 235 | typedef GrTInstanceBatch<BWFillRectBatchLocalMatrixImp> BWFillRectBatchLocalMatrix; |
| 236 | typedef GrTInstanceBatch<BWFillRectBatchLocalRectImp> BWFillRectBatchLocalRect; |
| 237 | typedef GrTInstanceBatch<BWFillRectBatchLocalMatrixLocalRectImp> BWFillRectBatchLocalMatrixLocalRect; |
| 238 | |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 239 | namespace GrBWFillRectBatch { |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 240 | GrDrawBatch* Create(GrColor color, |
| 241 | const SkMatrix& viewMatrix, |
| 242 | const SkRect& rect, |
| 243 | const SkRect* localRect, |
| 244 | const SkMatrix* localMatrix) { |
joshualitt | ae41b38 | 2015-08-19 06:54:08 -0700 | [diff] [blame^] | 245 | // TODO bubble these up as separate calls |
| 246 | if (localRect && localMatrix) { |
| 247 | BWFillRectBatchLocalMatrixLocalRect* batch = BWFillRectBatchLocalMatrixLocalRect::Create(); |
| 248 | BWFillRectBatchLocalMatrixLocalRect::Geometry& geo = *batch->geometry(); |
| 249 | geo.fColor = color; |
| 250 | geo.fViewMatrix = viewMatrix; |
| 251 | geo.fLocalMatrix = *localMatrix; |
| 252 | geo.fRect = rect; |
| 253 | geo.fLocalRect = *localRect; |
| 254 | batch->init(); |
| 255 | return batch; |
| 256 | } else if (localRect) { |
| 257 | BWFillRectBatchLocalRect* batch = BWFillRectBatchLocalRect::Create(); |
| 258 | BWFillRectBatchLocalRect::Geometry& geo = *batch->geometry(); |
| 259 | geo.fColor = color; |
| 260 | geo.fViewMatrix = viewMatrix; |
| 261 | geo.fRect = rect; |
| 262 | geo.fLocalRect = *localRect; |
| 263 | batch->init(); |
| 264 | return batch; |
| 265 | } else if (localMatrix) { |
| 266 | BWFillRectBatchLocalMatrix* batch = BWFillRectBatchLocalMatrix::Create(); |
| 267 | BWFillRectBatchLocalMatrix::Geometry& geo = *batch->geometry(); |
| 268 | geo.fColor = color; |
| 269 | geo.fViewMatrix = viewMatrix; |
| 270 | geo.fLocalMatrix = *localMatrix; |
| 271 | geo.fRect = rect; |
| 272 | batch->init(); |
| 273 | return batch; |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 274 | } else { |
joshualitt | ae41b38 | 2015-08-19 06:54:08 -0700 | [diff] [blame^] | 275 | BWFillRectBatchSimple* batch = BWFillRectBatchSimple::Create(); |
| 276 | BWFillRectBatchSimple::Geometry& geo = *batch->geometry(); |
| 277 | geo.fColor = color; |
| 278 | geo.fViewMatrix = viewMatrix; |
| 279 | geo.fRect = rect; |
| 280 | batch->init(); |
| 281 | return batch; |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 282 | } |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 283 | } |
| 284 | }; |
| 285 | |
| 286 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 287 | |
| 288 | #ifdef GR_TEST_UTILS |
| 289 | |
| 290 | #include "GrBatchTest.h" |
| 291 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 292 | DRAW_BATCH_TEST_DEFINE(RectBatch) { |
joshualitt | ae41b38 | 2015-08-19 06:54:08 -0700 | [diff] [blame^] | 293 | GrColor color = GrRandomColor(random); |
| 294 | SkRect rect = GrTest::TestRect(random); |
| 295 | SkRect localRect = GrTest::TestRect(random); |
| 296 | SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random); |
| 297 | SkMatrix localMatrix = GrTest::TestMatrix(random); |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 298 | |
joshualitt | ae41b38 | 2015-08-19 06:54:08 -0700 | [diff] [blame^] | 299 | bool hasLocalRect = random->nextBool(); |
| 300 | bool hasLocalMatrix = random->nextBool(); |
| 301 | return GrBWFillRectBatch::Create(color, viewMatrix, rect, hasLocalRect ? &localRect : nullptr, |
| 302 | hasLocalMatrix ? &localMatrix : nullptr); |
joshualitt | 9c80b5f | 2015-08-13 10:05:51 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | #endif |