OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "GrBWFillRectBatch.h" | 8 #include "GrBWFillRectBatch.h" |
9 | 9 |
10 #include "GrBatchFlushState.h" | 10 #include "GrBatchFlushState.h" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 The vertex attrib order is always pos, color, [local coords]. | 45 The vertex attrib order is always pos, color, [local coords]. |
46 */ | 46 */ |
47 static const GrGeometryProcessor* create_gp(const SkMatrix& viewMatrix, | 47 static const GrGeometryProcessor* create_gp(const SkMatrix& viewMatrix, |
48 bool readsCoverage, | 48 bool readsCoverage, |
49 bool hasExplicitLocalCoords, | 49 bool hasExplicitLocalCoords, |
50 const SkMatrix* localMatrix) { | 50 const SkMatrix* localMatrix) { |
51 using namespace GrDefaultGeoProcFactory; | 51 using namespace GrDefaultGeoProcFactory; |
52 Color color(Color::kAttribute_Type); | 52 Color color(Color::kAttribute_Type); |
53 Coverage coverage(readsCoverage ? Coverage::kSolid_Type : Coverage::kNone_Ty
pe); | 53 Coverage coverage(readsCoverage ? Coverage::kSolid_Type : Coverage::kNone_Ty
pe); |
54 | 54 |
55 // if we have a local rect, then we apply the localMatrix directly to the lo
calRect to | 55 // If we have perspective on the viewMatrix then we won't map on the CPU, no
r will we map |
56 // generate vertex local coords | 56 // the local rect on the cpu (in case the localMatrix also has perspective). |
57 if (hasExplicitLocalCoords) { | 57 // Otherwise, if we have a local rect, then we apply the localMatrix directl
y to the localRect |
| 58 // to generate vertex local coords |
| 59 if (viewMatrix.hasPerspective()) { |
| 60 LocalCoords localCoords(hasExplicitLocalCoords ? LocalCoords::kHasExplic
it_Type : |
| 61 LocalCoords::kUsePositi
on_Type, |
| 62 localMatrix); |
| 63 return GrDefaultGeoProcFactory::Create(color, coverage, localCoords, vie
wMatrix); |
| 64 } else if (hasExplicitLocalCoords) { |
58 LocalCoords localCoords(LocalCoords::kHasExplicit_Type); | 65 LocalCoords localCoords(LocalCoords::kHasExplicit_Type); |
59 return GrDefaultGeoProcFactory::Create(color, coverage, localCoords, SkM
atrix::I()); | 66 return GrDefaultGeoProcFactory::Create(color, coverage, localCoords, SkM
atrix::I()); |
60 } else { | 67 } else { |
61 LocalCoords localCoords(LocalCoords::kUsePosition_Type, localMatrix); | 68 LocalCoords localCoords(LocalCoords::kUsePosition_Type, localMatrix); |
62 return GrDefaultGeoProcFactory::CreateForDeviceSpace(color, coverage, lo
calCoords, | 69 return GrDefaultGeoProcFactory::CreateForDeviceSpace(color, coverage, lo
calCoords, |
63 viewMatrix); | 70 viewMatrix); |
64 } | 71 } |
65 } | 72 } |
66 | 73 |
67 static void tesselate(intptr_t vertices, | 74 static void tesselate(intptr_t vertices, |
68 size_t vertexStride, | 75 size_t vertexStride, |
69 GrColor color, | 76 GrColor color, |
70 const SkMatrix& viewMatrix, | 77 const SkMatrix& viewMatrix, |
71 const SkRect& rect, | 78 const SkRect& rect, |
72 const SkRect* localRect, | 79 const GrQuad* localQuad) { |
73 const SkMatrix* localMatrix) { | |
74 SkPoint* positions = reinterpret_cast<SkPoint*>(vertices); | 80 SkPoint* positions = reinterpret_cast<SkPoint*>(vertices); |
75 | 81 |
76 positions->setRectFan(rect.fLeft, rect.fTop, | 82 positions->setRectFan(rect.fLeft, rect.fTop, |
77 rect.fRight, rect.fBottom, vertexStride); | 83 rect.fRight, rect.fBottom, vertexStride); |
78 viewMatrix.mapPointsWithStride(positions, vertexStride, BWFillRectBatchBase:
:kVertsPerInstance); | |
79 | 84 |
| 85 if (!viewMatrix.hasPerspective()) { |
| 86 viewMatrix.mapPointsWithStride(positions, vertexStride, |
| 87 BWFillRectBatchBase::kVertsPerInstance); |
| 88 } |
| 89 |
| 90 // Setup local coords |
80 // TODO we should only do this if local coords are being read | 91 // TODO we should only do this if local coords are being read |
81 if (localRect) { | 92 if (localQuad) { |
82 static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor); | 93 static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor); |
83 SkPoint* coords = reinterpret_cast<SkPoint*>(vertices + kLocalOffset); | 94 for (int i = 0; i < BWFillRectBatchBase::kVertsPerInstance; i++) { |
84 coords->setRectFan(localRect->fLeft, localRect->fTop, | 95 SkPoint* coords = reinterpret_cast<SkPoint*>(vertices + kLocalOffset
+ |
85 localRect->fRight, localRect->fBottom, | 96 i * vertexStride); |
86 vertexStride); | 97 *coords = localQuad->point(i); |
87 if (localMatrix) { | |
88 localMatrix->mapPointsWithStride(coords, vertexStride, | |
89 BWFillRectBatchBase::kVertsPerInsta
nce); | |
90 } | 98 } |
91 } | 99 } |
92 | 100 |
93 static const int kColorOffset = sizeof(SkPoint); | 101 static const int kColorOffset = sizeof(SkPoint); |
94 GrColor* vertColor = reinterpret_cast<GrColor*>(vertices + kColorOffset); | 102 GrColor* vertColor = reinterpret_cast<GrColor*>(vertices + kColorOffset); |
95 for (int j = 0; j < 4; ++j) { | 103 for (int j = 0; j < 4; ++j) { |
96 *vertColor = color; | 104 *vertColor = color; |
97 vertColor = (GrColor*) ((intptr_t) vertColor + vertexStride); | 105 vertColor = (GrColor*) ((intptr_t) vertColor + vertexStride); |
98 } | 106 } |
99 } | 107 } |
100 | 108 |
101 class BWFillRectBatchNoLocalMatrixImp : public BWFillRectBatchBase { | 109 class BWFillRectBatchNoLocalMatrixImp : public BWFillRectBatchBase { |
102 public: | 110 public: |
103 struct Geometry { | 111 struct Geometry { |
104 SkMatrix fViewMatrix; | 112 SkMatrix fViewMatrix; |
105 SkRect fRect; | 113 SkRect fRect; |
| 114 GrQuad fLocalQuad; |
106 GrColor fColor; | 115 GrColor fColor; |
107 }; | 116 }; |
108 | 117 |
109 static const char* Name() { return "BWFillRectBatchNoLocalMatrix"; } | 118 static const char* Name() { return "BWFillRectBatchNoLocalMatrix"; } |
110 | 119 |
111 static bool CanCombine(const Geometry& mine, const Geometry& theirs, | 120 static bool CanCombine(const Geometry& mine, const Geometry& theirs, |
112 const GrPipelineOptimizations& opts) { | 121 const GrPipelineOptimizations& opts) { |
113 // We apply the viewmatrix to the rect points on the cpu. However, if t
he pipeline uses | |
114 // local coords then we won't be able to batch. We could actually uploa
d 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& op
ts) { | |
121 const GrGeometryProcessor* gp = create_gp(geo.fViewMatrix, opts.readsCov
erage(), false, | |
122 NULL); | |
123 | |
124 SkASSERT(gp->getVertexStride() == sizeof(GrDefaultGeoProcFactory::Positi
onColorAttr)); | |
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& op
ts) { | |
155 const GrGeometryProcessor* gp = create_gp(geo.fViewMatrix, opts.readsCov
erage(), false, | |
156 &geo.fLocalMatrix); | |
157 | |
158 SkASSERT(gp->getVertexStride() == sizeof(GrDefaultGeoProcFactory::Positi
onColorAttr)); | |
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) { | |
182 return true; | 122 return true; |
183 } | 123 } |
184 | 124 |
185 static const GrGeometryProcessor* CreateGP(const Geometry& geo, | 125 static const GrGeometryProcessor* CreateGP(const Geometry& geo, |
186 const GrPipelineOptimizations& op
ts) { | 126 const GrPipelineOptimizations& op
ts) { |
187 const GrGeometryProcessor* gp = create_gp(geo.fViewMatrix, opts.readsCov
erage(), true, | 127 const GrGeometryProcessor* gp = create_gp(geo.fViewMatrix, opts.readsCov
erage(), true, |
188 NULL); | 128 nullptr); |
189 | 129 |
190 SkASSERT(gp->getVertexStride() == | 130 SkASSERT(gp->getVertexStride() == |
191 sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr)); | 131 sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr)); |
192 return gp; | 132 return gp; |
193 } | 133 } |
194 | 134 |
195 static void Tesselate(intptr_t vertices, size_t vertexStride, const Geometry
& geo, | 135 static void Tesselate(intptr_t vertices, size_t vertexStride, const Geometry
& geo, |
196 const GrPipelineOptimizations& opts) { | 136 const GrPipelineOptimizations& opts) { |
197 tesselate(vertices, vertexStride, geo.fColor, geo.fViewMatrix, geo.fRect
, &geo.fLocalRect, | 137 tesselate(vertices, vertexStride, geo.fColor, geo.fViewMatrix, geo.fRect
, &geo.fLocalQuad); |
198 NULL); | |
199 } | 138 } |
200 }; | 139 }; |
201 | 140 |
202 class BWFillRectBatchLocalMatrixLocalRectImp : public BWFillRectBatchBase { | 141 // We handle perspective in the local matrix or viewmatrix with special batches |
| 142 class BWFillRectBatchPerspectiveImp : public BWFillRectBatchBase { |
203 public: | 143 public: |
204 struct Geometry { | 144 struct Geometry { |
205 SkMatrix fViewMatrix; | 145 SkMatrix fViewMatrix; |
206 SkMatrix fLocalMatrix; | 146 SkMatrix fLocalMatrix; |
207 SkRect fRect; | 147 SkRect fRect; |
208 SkRect fLocalRect; | 148 SkRect fLocalRect; |
209 GrColor fColor; | 149 GrColor fColor; |
| 150 bool fHasLocalMatrix; |
| 151 bool fHasLocalRect; |
210 }; | 152 }; |
211 | 153 |
212 static const char* Name() { return "BWFillRectBatchLocalMatrixLocalRect"; } | 154 static const char* Name() { return "BWFillRectBatchPerspective"; } |
213 | 155 |
214 static bool CanCombine(const Geometry& mine, const Geometry& theirs, | 156 static bool CanCombine(const Geometry& mine, const Geometry& theirs, |
215 const GrPipelineOptimizations& opts) { | 157 const GrPipelineOptimizations& opts) { |
216 return true; | 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.f
LocalMatrix)); |
217 } | 161 } |
218 | 162 |
219 static const GrGeometryProcessor* CreateGP(const Geometry& geo, | 163 static const GrGeometryProcessor* CreateGP(const Geometry& geo, |
220 const GrPipelineOptimizations& op
ts) { | 164 const GrPipelineOptimizations& op
ts) { |
221 const GrGeometryProcessor* gp = create_gp(geo.fViewMatrix, opts.readsCov
erage(), true, | 165 const GrGeometryProcessor* gp = create_gp(geo.fViewMatrix, opts.readsCov
erage(), |
222 NULL); | 166 geo.fHasLocalRect, |
| 167 geo.fHasLocalMatrix ? &geo.fLo
calMatrix : |
| 168 nullptr)
; |
223 | 169 |
224 SkASSERT(gp->getVertexStride() == | 170 SkASSERT(geo.fHasLocalRect ? |
225 sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr)); | 171 gp->getVertexStride() == sizeof(GrDefaultGeoProcFactory::PositionCo
lorLocalCoordAttr) : |
| 172 gp->getVertexStride() == sizeof(GrDefaultGeoProcFactory::PositionCo
lorAttr)); |
226 return gp; | 173 return gp; |
227 } | 174 } |
228 | 175 |
229 static void Tesselate(intptr_t vertices, size_t vertexStride, const Geometry
& geo, | 176 static void Tesselate(intptr_t vertices, size_t vertexStride, const Geometry
& geo, |
230 const GrPipelineOptimizations& opts) { | 177 const GrPipelineOptimizations& opts) { |
231 tesselate(vertices, vertexStride, geo.fColor, geo.fViewMatrix, geo.fRect
, &geo.fLocalRect, | 178 if (geo.fHasLocalRect) { |
232 &geo.fLocalMatrix); | 179 GrQuad quad(geo.fLocalRect); |
| 180 tesselate(vertices, vertexStride, geo.fColor, geo.fViewMatrix, geo.f
Rect, &quad); |
| 181 } else { |
| 182 tesselate(vertices, vertexStride, geo.fColor, geo.fViewMatrix, geo.f
Rect, nullptr); |
| 183 } |
233 } | 184 } |
234 }; | 185 }; |
235 | 186 |
236 typedef GrTInstanceBatch<BWFillRectBatchNoLocalMatrixImp> BWFillRectBatchSimple; | 187 typedef GrTInstanceBatch<BWFillRectBatchNoLocalMatrixImp> BWFillRectBatchSimple; |
237 typedef GrTInstanceBatch<BWFillRectBatchLocalMatrixImp> BWFillRectBatchLocalMatr
ix; | 188 typedef GrTInstanceBatch<BWFillRectBatchPerspectiveImp> BWFillRectBatchPerspecti
ve; |
238 typedef GrTInstanceBatch<BWFillRectBatchLocalRectImp> BWFillRectBatchLocalRect; | |
239 typedef GrTInstanceBatch<BWFillRectBatchLocalMatrixLocalRectImp> BWFillRectBatch
LocalMatrixLocalRect; | |
240 | 189 |
241 namespace GrBWFillRectBatch { | 190 namespace GrBWFillRectBatch { |
242 GrDrawBatch* Create(GrColor color, | 191 GrDrawBatch* Create(GrColor color, |
243 const SkMatrix& viewMatrix, | 192 const SkMatrix& viewMatrix, |
244 const SkRect& rect, | 193 const SkRect& rect, |
245 const SkRect* localRect, | 194 const SkRect* localRect, |
246 const SkMatrix* localMatrix) { | 195 const SkMatrix* localMatrix) { |
247 // TODO bubble these up as separate calls | 196 |
248 if (localRect && localMatrix) { | 197 /* Perspective has to be handled in a slow path for now */ |
249 BWFillRectBatchLocalMatrixLocalRect* batch = BWFillRectBatchLocalMatrixL
ocalRect::Create(); | 198 if (viewMatrix.hasPerspective() || (localMatrix && localMatrix->hasPerspecti
ve())) { |
250 BWFillRectBatchLocalMatrixLocalRect::Geometry& geo = *batch->geometry(); | 199 BWFillRectBatchPerspective* batch = BWFillRectBatchPerspective::Create()
; |
251 geo.fColor = color; | 200 BWFillRectBatchPerspective::Geometry& geo = *batch->geometry(); |
252 geo.fViewMatrix = viewMatrix; | 201 |
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(); | |
261 geo.fColor = color; | 202 geo.fColor = color; |
262 geo.fViewMatrix = viewMatrix; | 203 geo.fViewMatrix = viewMatrix; |
263 geo.fRect = rect; | 204 geo.fRect = rect; |
264 geo.fLocalRect = *localRect; | 205 geo.fHasLocalRect = SkToBool(localRect); |
265 batch->init(); | 206 geo.fHasLocalMatrix = SkToBool(localMatrix); |
266 return batch; | 207 if (localMatrix) { |
267 } else if (localMatrix) { | 208 geo.fLocalMatrix = *localMatrix; |
268 BWFillRectBatchLocalMatrix* batch = BWFillRectBatchLocalMatrix::Create()
; | 209 } |
269 BWFillRectBatchLocalMatrix::Geometry& geo = *batch->geometry(); | 210 if (localRect) { |
270 geo.fColor = color; | 211 geo.fLocalRect = *localRect; |
271 geo.fViewMatrix = viewMatrix; | 212 } |
272 geo.fLocalMatrix = *localMatrix; | 213 |
273 geo.fRect = rect; | |
274 batch->init(); | 214 batch->init(); |
275 return batch; | 215 return batch; |
276 } else { | 216 } else { |
| 217 // TODO bubble these up as separate calls |
277 BWFillRectBatchSimple* batch = BWFillRectBatchSimple::Create(); | 218 BWFillRectBatchSimple* batch = BWFillRectBatchSimple::Create(); |
278 BWFillRectBatchSimple::Geometry& geo = *batch->geometry(); | 219 BWFillRectBatchSimple::Geometry& geo = *batch->geometry(); |
| 220 |
279 geo.fColor = color; | 221 geo.fColor = color; |
280 geo.fViewMatrix = viewMatrix; | 222 geo.fViewMatrix = viewMatrix; |
281 geo.fRect = rect; | 223 geo.fRect = rect; |
| 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 |
282 batch->init(); | 235 batch->init(); |
283 return batch; | 236 return batch; |
284 } | 237 } |
285 } | 238 } |
286 }; | 239 }; |
287 | 240 |
288 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 241 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
289 | 242 |
290 #ifdef GR_TEST_UTILS | 243 #ifdef GR_TEST_UTILS |
291 | 244 |
292 #include "GrBatchTest.h" | 245 #include "GrBatchTest.h" |
293 | 246 |
294 DRAW_BATCH_TEST_DEFINE(RectBatch) { | 247 DRAW_BATCH_TEST_DEFINE(RectBatch) { |
295 GrColor color = GrRandomColor(random); | 248 GrColor color = GrRandomColor(random); |
296 SkRect rect = GrTest::TestRect(random); | 249 SkRect rect = GrTest::TestRect(random); |
297 SkRect localRect = GrTest::TestRect(random); | 250 SkRect localRect = GrTest::TestRect(random); |
298 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random); | 251 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random); |
299 SkMatrix localMatrix = GrTest::TestMatrix(random); | 252 SkMatrix localMatrix = GrTest::TestMatrix(random); |
300 | 253 |
301 bool hasLocalRect = random->nextBool(); | 254 bool hasLocalRect = random->nextBool(); |
302 bool hasLocalMatrix = random->nextBool(); | 255 bool hasLocalMatrix = random->nextBool(); |
303 return GrBWFillRectBatch::Create(color, viewMatrix, rect, hasLocalRect ? &lo
calRect : nullptr, | 256 return GrBWFillRectBatch::Create(color, viewMatrix, rect, hasLocalRect ? &lo
calRect : nullptr, |
304 hasLocalMatrix ? &localMatrix : nullptr); | 257 hasLocalMatrix ? &localMatrix : nullptr); |
305 } | 258 } |
306 | 259 |
307 #endif | 260 #endif |
OLD | NEW |