blob: 9471e1677e228e7cf8597197cb2ecc90e036d8ca [file] [log] [blame]
joshualitt9c80b5f2015-08-13 10:05:51 -07001/*
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
bsalomon75398562015-08-17 12:55:38 -070010#include "GrBatchFlushState.h"
joshualitt9c80b5f2015-08-13 10:05:51 -070011#include "GrColor.h"
12#include "GrDefaultGeoProcFactory.h"
13#include "GrPrimitiveProcessor.h"
joshualitt2244c272015-08-21 10:33:15 -070014#include "GrResourceProvider.h"
15#include "GrTInstanceBatch.h"
joshualittae5b2c62015-08-19 08:48:41 -070016#include "GrQuad.h"
bsalomon16b99132015-08-13 14:55:50 -070017#include "GrVertexBatch.h"
joshualitt9c80b5f2015-08-13 10:05:51 -070018
joshualitt2244c272015-08-21 10:33:15 -070019// Common functions
20class BWFillRectBatchBase {
21public:
22 static const int kVertsPerInstance = 4;
23 static const int kIndicesPerInstance = 6;
joshualitt87e47522015-08-20 07:22:42 -070024
joshualitt2244c272015-08-21 10:33:15 -070025 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 */
47static 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
joshualitt26db32b2015-08-24 10:26:01 -070055 // if we have a local rect, then we apply the localMatrix directly to the localRect to
56 // generate vertex local coords
57 if (hasExplicitLocalCoords) {
joshualitt2244c272015-08-21 10:33:15 -070058 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
67static void tesselate(intptr_t vertices,
68 size_t vertexStride,
69 GrColor color,
70 const SkMatrix& viewMatrix,
71 const SkRect& rect,
joshualitt26db32b2015-08-24 10:26:01 -070072 const SkRect* localRect,
73 const SkMatrix* localMatrix) {
joshualitt2244c272015-08-21 10:33:15 -070074 SkPoint* positions = reinterpret_cast<SkPoint*>(vertices);
75
76 positions->setRectFan(rect.fLeft, rect.fTop,
77 rect.fRight, rect.fBottom, vertexStride);
joshualitt26db32b2015-08-24 10:26:01 -070078 viewMatrix.mapPointsWithStride(positions, vertexStride, BWFillRectBatchBase::kVertsPerInstance);
joshualitt2244c272015-08-21 10:33:15 -070079
80 // TODO we should only do this if local coords are being read
joshualitt26db32b2015-08-24 10:26:01 -070081 if (localRect) {
joshualitt2244c272015-08-21 10:33:15 -070082 static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor);
joshualitt26db32b2015-08-24 10:26:01 -070083 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);
joshualitt2244c272015-08-21 10:33:15 -070090 }
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
101class BWFillRectBatchNoLocalMatrixImp : public BWFillRectBatchBase {
102public:
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) {
joshualitt26db32b2015-08-24 10:26:01 -0700113 // 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
134class BWFillRectBatchLocalMatrixImp : public BWFillRectBatchBase {
135public:
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
169class BWFillRectBatchLocalRectImp : public BWFillRectBatchBase {
170public:
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) {
joshualitt87e47522015-08-20 07:22:42 -0700182 return true;
183 }
184
joshualitt2244c272015-08-21 10:33:15 -0700185 static const GrGeometryProcessor* CreateGP(const Geometry& geo,
186 const GrPipelineOptimizations& opts) {
187 const GrGeometryProcessor* gp = create_gp(geo.fViewMatrix, opts.readsCoverage(), true,
joshualitt26db32b2015-08-24 10:26:01 -0700188 NULL);
joshualitt87e47522015-08-20 07:22:42 -0700189
joshualitt2244c272015-08-21 10:33:15 -0700190 SkASSERT(gp->getVertexStride() ==
191 sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr));
192 return gp;
joshualittae41b382015-08-19 06:54:08 -0700193 }
194
joshualitt2244c272015-08-21 10:33:15 -0700195 static void Tesselate(intptr_t vertices, size_t vertexStride, const Geometry& geo,
196 const GrPipelineOptimizations& opts) {
joshualitt26db32b2015-08-24 10:26:01 -0700197 tesselate(vertices, vertexStride, geo.fColor, geo.fViewMatrix, geo.fRect, &geo.fLocalRect,
198 NULL);
joshualitt2244c272015-08-21 10:33:15 -0700199 }
200};
201
joshualitt26db32b2015-08-24 10:26:01 -0700202class BWFillRectBatchLocalMatrixLocalRectImp : public BWFillRectBatchBase {
joshualitt2244c272015-08-21 10:33:15 -0700203public:
204 struct Geometry {
205 SkMatrix fViewMatrix;
206 SkMatrix fLocalMatrix;
207 SkRect fRect;
208 SkRect fLocalRect;
joshualittae41b382015-08-19 06:54:08 -0700209 GrColor fColor;
210 };
211
joshualitt26db32b2015-08-24 10:26:01 -0700212 static const char* Name() { return "BWFillRectBatchLocalMatrixLocalRect"; }
joshualitt2244c272015-08-21 10:33:15 -0700213
214 static bool CanCombine(const Geometry& mine, const Geometry& theirs,
215 const GrPipelineOptimizations& opts) {
joshualitt26db32b2015-08-24 10:26:01 -0700216 return true;
joshualitt2244c272015-08-21 10:33:15 -0700217 }
218
219 static const GrGeometryProcessor* CreateGP(const Geometry& geo,
220 const GrPipelineOptimizations& opts) {
joshualitt26db32b2015-08-24 10:26:01 -0700221 const GrGeometryProcessor* gp = create_gp(geo.fViewMatrix, opts.readsCoverage(), true,
222 NULL);
joshualitt2244c272015-08-21 10:33:15 -0700223
joshualitt26db32b2015-08-24 10:26:01 -0700224 SkASSERT(gp->getVertexStride() ==
225 sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr));
joshualitt2244c272015-08-21 10:33:15 -0700226 return gp;
227 }
228
229 static void Tesselate(intptr_t vertices, size_t vertexStride, const Geometry& geo,
230 const GrPipelineOptimizations& opts) {
joshualitt26db32b2015-08-24 10:26:01 -0700231 tesselate(vertices, vertexStride, geo.fColor, geo.fViewMatrix, geo.fRect, &geo.fLocalRect,
232 &geo.fLocalMatrix);
joshualitt2244c272015-08-21 10:33:15 -0700233 }
joshualittae41b382015-08-19 06:54:08 -0700234};
235
joshualitt2244c272015-08-21 10:33:15 -0700236typedef GrTInstanceBatch<BWFillRectBatchNoLocalMatrixImp> BWFillRectBatchSimple;
joshualitt26db32b2015-08-24 10:26:01 -0700237typedef GrTInstanceBatch<BWFillRectBatchLocalMatrixImp> BWFillRectBatchLocalMatrix;
238typedef GrTInstanceBatch<BWFillRectBatchLocalRectImp> BWFillRectBatchLocalRect;
239typedef GrTInstanceBatch<BWFillRectBatchLocalMatrixLocalRectImp> BWFillRectBatchLocalMatrixLocalRect;
joshualitt2244c272015-08-21 10:33:15 -0700240
joshualitt9c80b5f2015-08-13 10:05:51 -0700241namespace GrBWFillRectBatch {
bsalomonabd30f52015-08-13 13:34:48 -0700242GrDrawBatch* Create(GrColor color,
243 const SkMatrix& viewMatrix,
244 const SkRect& rect,
245 const SkRect* localRect,
246 const SkMatrix* localMatrix) {
joshualitt26db32b2015-08-24 10:26:01 -0700247 // 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();
joshualitt2244c272015-08-21 10:33:15 -0700261 geo.fColor = color;
262 geo.fViewMatrix = viewMatrix;
263 geo.fRect = rect;
joshualitt26db32b2015-08-24 10:26:01 -0700264 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;
joshualitt2244c272015-08-21 10:33:15 -0700274 batch->init();
275 return batch;
joshualitt9c80b5f2015-08-13 10:05:51 -0700276 } else {
joshualitt2244c272015-08-21 10:33:15 -0700277 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;
joshualitt9c80b5f2015-08-13 10:05:51 -0700284 }
joshualitt9c80b5f2015-08-13 10:05:51 -0700285}
286};
287
288///////////////////////////////////////////////////////////////////////////////////////////////////
289
290#ifdef GR_TEST_UTILS
291
292#include "GrBatchTest.h"
293
bsalomonabd30f52015-08-13 13:34:48 -0700294DRAW_BATCH_TEST_DEFINE(RectBatch) {
joshualitt2244c272015-08-21 10:33:15 -0700295 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);
joshualitt9c80b5f2015-08-13 10:05:51 -0700300
joshualitt2244c272015-08-21 10:33:15 -0700301 bool hasLocalRect = random->nextBool();
302 bool hasLocalMatrix = random->nextBool();
303 return GrBWFillRectBatch::Create(color, viewMatrix, rect, hasLocalRect ? &localRect : nullptr,
304 hasLocalMatrix ? &localMatrix : nullptr);
joshualitt9c80b5f2015-08-13 10:05:51 -0700305}
306
307#endif