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