blob: b9865740a2a2ec0aee0b443b2b3f15481b2c7b80 [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
joshualittbcf33d52015-08-26 08:10:35 -07008#include "GrNonAAFillRectBatch.h"
joshualitt9c80b5f2015-08-13 10:05:51 -07009
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"
joshualittae5b2c62015-08-19 08:48:41 -070015#include "GrQuad.h"
bsalomon16b99132015-08-13 14:55:50 -070016#include "GrVertexBatch.h"
joshualitt9c80b5f2015-08-13 10:05:51 -070017
bsalomon08d14152016-06-30 12:45:18 -070018static const int kVertsPerInstance = 4;
19static const int kIndicesPerInstance = 6;
joshualitt2244c272015-08-21 10:33:15 -070020
21/** We always use per-vertex colors so that rects can be batched across color changes. Sometimes
22 we have explicit local coords and sometimes not. We *could* always provide explicit local
23 coords and just duplicate the positions when the caller hasn't provided a local coord rect,
24 but we haven't seen a use case which frequently switches between local rect and no local
25 rect draws.
26
27 The vertex attrib order is always pos, color, [local coords].
28 */
bungeman06ca8ec2016-06-09 08:01:03 -070029static sk_sp<GrGeometryProcessor> make_gp(const SkMatrix& viewMatrix,
30 bool readsCoverage,
31 bool hasExplicitLocalCoords,
32 const SkMatrix* localMatrix) {
joshualitt2244c272015-08-21 10:33:15 -070033 using namespace GrDefaultGeoProcFactory;
34 Color color(Color::kAttribute_Type);
35 Coverage coverage(readsCoverage ? Coverage::kSolid_Type : Coverage::kNone_Type);
36
joshualitt8cce8f12015-08-26 06:23:39 -070037 // If we have perspective on the viewMatrix then we won't map on the CPU, nor will we map
38 // the local rect on the cpu (in case the localMatrix also has perspective).
39 // Otherwise, if we have a local rect, then we apply the localMatrix directly to the localRect
40 // to generate vertex local coords
41 if (viewMatrix.hasPerspective()) {
42 LocalCoords localCoords(hasExplicitLocalCoords ? LocalCoords::kHasExplicit_Type :
43 LocalCoords::kUsePosition_Type,
44 localMatrix);
bungeman06ca8ec2016-06-09 08:01:03 -070045 return GrDefaultGeoProcFactory::Make(color, coverage, localCoords, viewMatrix);
joshualitt8cce8f12015-08-26 06:23:39 -070046 } else if (hasExplicitLocalCoords) {
joshualitt2244c272015-08-21 10:33:15 -070047 LocalCoords localCoords(LocalCoords::kHasExplicit_Type);
bungeman06ca8ec2016-06-09 08:01:03 -070048 return GrDefaultGeoProcFactory::Make(color, coverage, localCoords, SkMatrix::I());
joshualitt2244c272015-08-21 10:33:15 -070049 } else {
50 LocalCoords localCoords(LocalCoords::kUsePosition_Type, localMatrix);
bungeman06ca8ec2016-06-09 08:01:03 -070051 return GrDefaultGeoProcFactory::MakeForDeviceSpace(color, coverage, localCoords,
52 viewMatrix);
joshualitt2244c272015-08-21 10:33:15 -070053 }
54}
55
56static void tesselate(intptr_t vertices,
57 size_t vertexStride,
58 GrColor color,
59 const SkMatrix& viewMatrix,
60 const SkRect& rect,
joshualitt8cce8f12015-08-26 06:23:39 -070061 const GrQuad* localQuad) {
joshualitt2244c272015-08-21 10:33:15 -070062 SkPoint* positions = reinterpret_cast<SkPoint*>(vertices);
63
64 positions->setRectFan(rect.fLeft, rect.fTop,
65 rect.fRight, rect.fBottom, vertexStride);
joshualitt2244c272015-08-21 10:33:15 -070066
joshualitt8cce8f12015-08-26 06:23:39 -070067 if (!viewMatrix.hasPerspective()) {
bsalomon08d14152016-06-30 12:45:18 -070068 viewMatrix.mapPointsWithStride(positions, vertexStride, kVertsPerInstance);
joshualitt8cce8f12015-08-26 06:23:39 -070069 }
70
71 // Setup local coords
joshualitt2244c272015-08-21 10:33:15 -070072 // TODO we should only do this if local coords are being read
joshualitt8cce8f12015-08-26 06:23:39 -070073 if (localQuad) {
joshualitt2244c272015-08-21 10:33:15 -070074 static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor);
bsalomon08d14152016-06-30 12:45:18 -070075 for (int i = 0; i < kVertsPerInstance; i++) {
joshualitt8cce8f12015-08-26 06:23:39 -070076 SkPoint* coords = reinterpret_cast<SkPoint*>(vertices + kLocalOffset +
77 i * vertexStride);
78 *coords = localQuad->point(i);
joshualitt2244c272015-08-21 10:33:15 -070079 }
80 }
81
82 static const int kColorOffset = sizeof(SkPoint);
83 GrColor* vertColor = reinterpret_cast<GrColor*>(vertices + kColorOffset);
84 for (int j = 0; j < 4; ++j) {
85 *vertColor = color;
86 vertColor = (GrColor*) ((intptr_t) vertColor + vertexStride);
87 }
88}
89
bsalomon08d14152016-06-30 12:45:18 -070090class NonAAFillRectBatch : public GrVertexBatch {
joshualitt2244c272015-08-21 10:33:15 -070091public:
bsalomon08d14152016-06-30 12:45:18 -070092 DEFINE_BATCH_CLASS_ID
93
joshualitt2244c272015-08-21 10:33:15 -070094 struct Geometry {
95 SkMatrix fViewMatrix;
96 SkRect fRect;
joshualitt8cce8f12015-08-26 06:23:39 -070097 GrQuad fLocalQuad;
joshualitt2244c272015-08-21 10:33:15 -070098 GrColor fColor;
99 };
100
bsalomon08d14152016-06-30 12:45:18 -0700101 static NonAAFillRectBatch* Create() { return new NonAAFillRectBatch; }
102
103 const char* name() const override { return Name(); }
104
105 SkString dumpInfo() const override {
106 SkString str;
107 str.appendf("# batched: %d\n", fGeoData.count());
108 for (int i = 0; i < fGeoData.count(); ++i) {
109 str.append(DumpInfo(fGeoData[i], i));
110 }
111 str.append(INHERITED::dumpInfo());
112 return str;
113 }
114
115 void computePipelineOptimizations(GrInitInvariantOutput* color,
116 GrInitInvariantOutput* coverage,
117 GrBatchToXPOverrides* overrides) const override {
118 // When this is called on a batch, there is only one geometry bundle
119 color->setKnownFourComponents(fGeoData[0].fColor);
120 InitInvariantOutputCoverage(coverage);
121 }
122
123 void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
124 overrides.getOverrideColorIfSet(&fGeoData[0].fColor);
125 fOverrides = overrides;
126 }
127
128 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
129
130 // After seeding, the client should call init() so the Batch can initialize itself
131 void init() {
132 const Geometry& geo = fGeoData[0];
133 SetBounds(geo, &fBounds);
134 }
135
136 void updateBoundsAfterAppend() {
137 const Geometry& geo = fGeoData.back();
138 UpdateBoundsAfterAppend(geo, &fBounds);
139 }
140
141private:
joshualittbcf33d52015-08-26 08:10:35 -0700142 static const char* Name() { return "NonAAFillRectBatch"; }
joshualitt2244c272015-08-21 10:33:15 -0700143
robertphillips783a4da2015-11-19 14:00:02 -0800144 static SkString DumpInfo(const Geometry& geo, int index) {
robertphillipse004bfc2015-11-16 09:06:59 -0800145 SkString str;
robertphillips783a4da2015-11-19 14:00:02 -0800146 str.appendf("%d: Color: 0x%08x, Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n",
147 index,
robertphillipse004bfc2015-11-16 09:06:59 -0800148 geo.fColor,
149 geo.fRect.fLeft, geo.fRect.fTop, geo.fRect.fRight, geo.fRect.fBottom);
150 return str;
151 }
152
joshualitt2244c272015-08-21 10:33:15 -0700153 static bool CanCombine(const Geometry& mine, const Geometry& theirs,
ethannicholasff210322015-11-24 12:10:10 -0800154 const GrXPOverridesForBatch& overrides) {
joshualitt87e47522015-08-20 07:22:42 -0700155 return true;
156 }
157
bungeman06ca8ec2016-06-09 08:01:03 -0700158 static sk_sp<GrGeometryProcessor> MakeGP(const Geometry& geo,
159 const GrXPOverridesForBatch& overrides) {
160 sk_sp<GrGeometryProcessor> gp = make_gp(geo.fViewMatrix, overrides.readsCoverage(), true,
161 nullptr);
joshualitt87e47522015-08-20 07:22:42 -0700162
joshualitt2244c272015-08-21 10:33:15 -0700163 SkASSERT(gp->getVertexStride() ==
164 sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr));
165 return gp;
joshualittae41b382015-08-19 06:54:08 -0700166 }
167
joshualitt2244c272015-08-21 10:33:15 -0700168 static void Tesselate(intptr_t vertices, size_t vertexStride, const Geometry& geo,
ethannicholasff210322015-11-24 12:10:10 -0800169 const GrXPOverridesForBatch& overrides) {
joshualitt8cce8f12015-08-26 06:23:39 -0700170 tesselate(vertices, vertexStride, geo.fColor, geo.fViewMatrix, geo.fRect, &geo.fLocalQuad);
joshualitt2244c272015-08-21 10:33:15 -0700171 }
bsalomon08d14152016-06-30 12:45:18 -0700172
173 static void InitInvariantOutputCoverage(GrInitInvariantOutput* out) {
174 out->setKnownSingleComponent(0xff);
175 }
176
177 static const GrBuffer* GetIndexBuffer(GrResourceProvider* rp) {
178 return rp->refQuadIndexBuffer();
179 }
180
181 static void SetBounds(const Geometry& geo, SkRect* outBounds) {
182 geo.fViewMatrix.mapRect(outBounds, geo.fRect);
183 }
184
185 static void UpdateBoundsAfterAppend(const Geometry& geo, SkRect* outBounds) {
186 SkRect bounds = geo.fRect;
187 geo.fViewMatrix.mapRect(&bounds);
188 outBounds->join(bounds);
189 }
190
191 NonAAFillRectBatch() : INHERITED(ClassID()) {}
192
193 void onPrepareDraws(Target* target) const override {
194 sk_sp<GrGeometryProcessor> gp(MakeGP(this->seedGeometry(), fOverrides));
195 if (!gp) {
196 SkDebugf("Couldn't create GrGeometryProcessor\n");
197 return;
198 }
199
200 size_t vertexStride = gp->getVertexStride();
201 int instanceCount = fGeoData.count();
202
203 SkAutoTUnref<const GrBuffer> indexBuffer(GetIndexBuffer(target->resourceProvider()));
204 InstancedHelper helper;
205 void* vertices = helper.init(target, kTriangles_GrPrimitiveType, vertexStride,
206 indexBuffer, kVertsPerInstance,
207 kIndicesPerInstance, instanceCount);
208 if (!vertices || !indexBuffer) {
209 SkDebugf("Could not allocate vertices\n");
210 return;
211 }
212
213 for (int i = 0; i < instanceCount; i++) {
214 intptr_t verts = reinterpret_cast<intptr_t>(vertices) +
215 i * kVertsPerInstance * vertexStride;
216 Tesselate(verts, vertexStride, fGeoData[i], fOverrides);
217 }
218 helper.recordDraw(target, gp.get());
219 }
220
221 const Geometry& seedGeometry() const { return fGeoData[0]; }
222
223 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
224 NonAAFillRectBatch* that = t->cast<NonAAFillRectBatch>();
225 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
226 that->bounds(), caps)) {
227 return false;
228 }
229
230 if (!CanCombine(this->seedGeometry(), that->seedGeometry(), fOverrides)) {
231 return false;
232 }
233
234 // In the event of two batches, one who can tweak, one who cannot, we just fall back to
235 // not tweaking
236 if (fOverrides.canTweakAlphaForCoverage() && !that->fOverrides.canTweakAlphaForCoverage()) {
237 fOverrides = that->fOverrides;
238 }
239
240 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
241 this->joinBounds(that->bounds());
242 return true;
243 }
244
245 GrXPOverridesForBatch fOverrides;
246 SkSTArray<1, Geometry, true> fGeoData;
247
248 typedef GrVertexBatch INHERITED;
joshualitt2244c272015-08-21 10:33:15 -0700249};
250
joshualitt8cce8f12015-08-26 06:23:39 -0700251// We handle perspective in the local matrix or viewmatrix with special batches
bsalomon08d14152016-06-30 12:45:18 -0700252class NonAAFillRectPerspectiveBatch : public GrVertexBatch {
joshualitt2244c272015-08-21 10:33:15 -0700253public:
bsalomon08d14152016-06-30 12:45:18 -0700254 DEFINE_BATCH_CLASS_ID
255
joshualitt2244c272015-08-21 10:33:15 -0700256 struct Geometry {
257 SkMatrix fViewMatrix;
258 SkMatrix fLocalMatrix;
259 SkRect fRect;
260 SkRect fLocalRect;
joshualittae41b382015-08-19 06:54:08 -0700261 GrColor fColor;
joshualitt8cce8f12015-08-26 06:23:39 -0700262 bool fHasLocalMatrix;
263 bool fHasLocalRect;
joshualittae41b382015-08-19 06:54:08 -0700264 };
265
bsalomon08d14152016-06-30 12:45:18 -0700266 static NonAAFillRectPerspectiveBatch* Create() { return new NonAAFillRectPerspectiveBatch; }
267
268 const char* name() const override { return Name(); }
269
270 SkString dumpInfo() const override {
271 SkString str;
272 str.appendf("# batched: %d\n", fGeoData.count());
273 for (int i = 0; i < fGeoData.count(); ++i) {
274 str.append(DumpInfo(fGeoData[i], i));
275 }
276 str.append(INHERITED::dumpInfo());
277 return str;
278 }
279
280 void computePipelineOptimizations(GrInitInvariantOutput* color,
281 GrInitInvariantOutput* coverage,
282 GrBatchToXPOverrides* overrides) const override {
283 // When this is called on a batch, there is only one geometry bundle
284 color->setKnownFourComponents(fGeoData[0].fColor);
285 InitInvariantOutputCoverage(coverage);
286 }
287
288 void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
289 overrides.getOverrideColorIfSet(&fGeoData[0].fColor);
290 fOverrides = overrides;
291 }
292
293 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
294
295 // After seeding, the client should call init() so the Batch can initialize itself
296 void init() {
297 const Geometry& geo = fGeoData[0];
298 SetBounds(geo, &fBounds);
299 }
300
301 void updateBoundsAfterAppend() {
302 const Geometry& geo = fGeoData.back();
303 UpdateBoundsAfterAppend(geo, &fBounds);
304 }
305
306private:
307 static const char* Name() { return "NonAAFillRectPerspectiveBatch"; }
joshualitt2244c272015-08-21 10:33:15 -0700308
robertphillips783a4da2015-11-19 14:00:02 -0800309 static SkString DumpInfo(const Geometry& geo, int index) {
robertphillipse004bfc2015-11-16 09:06:59 -0800310 SkString str;
robertphillips783a4da2015-11-19 14:00:02 -0800311 str.appendf("%d: Color: 0x%08x, Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n",
312 index,
robertphillipse004bfc2015-11-16 09:06:59 -0800313 geo.fColor,
314 geo.fRect.fLeft, geo.fRect.fTop, geo.fRect.fRight, geo.fRect.fBottom);
315 return str;
316 }
317
joshualitt2244c272015-08-21 10:33:15 -0700318 static bool CanCombine(const Geometry& mine, const Geometry& theirs,
ethannicholasff210322015-11-24 12:10:10 -0800319 const GrXPOverridesForBatch& overrides) {
joshualitt8cce8f12015-08-26 06:23:39 -0700320 // We could batch across perspective vm changes if we really wanted to
321 return mine.fViewMatrix.cheapEqualTo(theirs.fViewMatrix) &&
joshualitt2120b6f2015-09-18 12:56:41 -0700322 mine.fHasLocalRect == theirs.fHasLocalRect &&
joshualitt8cce8f12015-08-26 06:23:39 -0700323 (!mine.fHasLocalMatrix || mine.fLocalMatrix.cheapEqualTo(theirs.fLocalMatrix));
joshualitt2244c272015-08-21 10:33:15 -0700324 }
325
bungeman06ca8ec2016-06-09 08:01:03 -0700326 static sk_sp<GrGeometryProcessor> MakeGP(const Geometry& geo,
327 const GrXPOverridesForBatch& overrides) {
328 sk_sp<GrGeometryProcessor> gp = make_gp(geo.fViewMatrix, overrides.readsCoverage(),
329 geo.fHasLocalRect,
330 geo.fHasLocalMatrix ? &geo.fLocalMatrix
331 : nullptr);
joshualitt2244c272015-08-21 10:33:15 -0700332
joshualitt8cce8f12015-08-26 06:23:39 -0700333 SkASSERT(geo.fHasLocalRect ?
334 gp->getVertexStride() == sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr) :
335 gp->getVertexStride() == sizeof(GrDefaultGeoProcFactory::PositionColorAttr));
joshualitt2244c272015-08-21 10:33:15 -0700336 return gp;
337 }
338
339 static void Tesselate(intptr_t vertices, size_t vertexStride, const Geometry& geo,
ethannicholasff210322015-11-24 12:10:10 -0800340 const GrXPOverridesForBatch& overrides) {
joshualitt8cce8f12015-08-26 06:23:39 -0700341 if (geo.fHasLocalRect) {
342 GrQuad quad(geo.fLocalRect);
343 tesselate(vertices, vertexStride, geo.fColor, geo.fViewMatrix, geo.fRect, &quad);
344 } else {
345 tesselate(vertices, vertexStride, geo.fColor, geo.fViewMatrix, geo.fRect, nullptr);
346 }
joshualitt2244c272015-08-21 10:33:15 -0700347 }
bsalomon08d14152016-06-30 12:45:18 -0700348
349 static void InitInvariantOutputCoverage(GrInitInvariantOutput* out) {
350 out->setKnownSingleComponent(0xff);
351 }
352
353 static const GrBuffer* GetIndexBuffer(GrResourceProvider* rp) {
354 return rp->refQuadIndexBuffer();
355 }
356
357 static void SetBounds(const Geometry& geo, SkRect* outBounds) {
358 geo.fViewMatrix.mapRect(outBounds, geo.fRect);
359 }
360
361 static void UpdateBoundsAfterAppend(const Geometry& geo, SkRect* outBounds) {
362 SkRect bounds = geo.fRect;
363 geo.fViewMatrix.mapRect(&bounds);
364 outBounds->join(bounds);
365 }
366
367 NonAAFillRectPerspectiveBatch() : INHERITED(ClassID()) {}
368
369 void onPrepareDraws(Target* target) const override {
370 sk_sp<GrGeometryProcessor> gp(MakeGP(this->seedGeometry(), fOverrides));
371 if (!gp) {
372 SkDebugf("Couldn't create GrGeometryProcessor\n");
373 return;
374 }
375
376 size_t vertexStride = gp->getVertexStride();
377 int instanceCount = fGeoData.count();
378
379 SkAutoTUnref<const GrBuffer> indexBuffer(GetIndexBuffer(target->resourceProvider()));
380 InstancedHelper helper;
381 void* vertices = helper.init(target, kTriangles_GrPrimitiveType, vertexStride,
382 indexBuffer, kVertsPerInstance,
383 kIndicesPerInstance, instanceCount);
384 if (!vertices || !indexBuffer) {
385 SkDebugf("Could not allocate vertices\n");
386 return;
387 }
388
389 for (int i = 0; i < instanceCount; i++) {
390 intptr_t verts = reinterpret_cast<intptr_t>(vertices) +
391 i * kVertsPerInstance * vertexStride;
392 Tesselate(verts, vertexStride, fGeoData[i], fOverrides);
393 }
394 helper.recordDraw(target, gp.get());
395 }
396
397 const Geometry& seedGeometry() const { return fGeoData[0]; }
398
399 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
400 NonAAFillRectPerspectiveBatch* that = t->cast<NonAAFillRectPerspectiveBatch>();
401 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
402 that->bounds(), caps)) {
403 return false;
404 }
405
406 if (!CanCombine(this->seedGeometry(), that->seedGeometry(), fOverrides)) {
407 return false;
408 }
409
410 // In the event of two batches, one who can tweak, one who cannot, we just fall back to
411 // not tweaking
412 if (fOverrides.canTweakAlphaForCoverage() && !that->fOverrides.canTweakAlphaForCoverage()) {
413 fOverrides = that->fOverrides;
414 }
415
416 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
417 this->joinBounds(that->bounds());
418 return true;
419 }
420
421 GrXPOverridesForBatch fOverrides;
422 SkSTArray<1, Geometry, true> fGeoData;
423
424 typedef GrVertexBatch INHERITED;
joshualittae41b382015-08-19 06:54:08 -0700425};
426
bsalomonb8cbd202016-06-30 13:09:48 -0700427namespace GrNonAAFillRectBatch {
428
429GrDrawBatch* Create(GrColor color,
430 const SkMatrix& viewMatrix,
431 const SkRect& rect,
432 const SkRect* localRect,
433 const SkMatrix* localMatrix) {
434 NonAAFillRectBatch* batch = NonAAFillRectBatch::Create();
joshualitt3566d442015-09-18 07:12:55 -0700435 SkASSERT(!viewMatrix.hasPerspective() && (!localMatrix || !localMatrix->hasPerspective()));
bsalomon08d14152016-06-30 12:45:18 -0700436 NonAAFillRectBatch::Geometry& geo = batch->geoData()->push_back();
joshualitt8cce8f12015-08-26 06:23:39 -0700437
joshualitt3566d442015-09-18 07:12:55 -0700438 geo.fColor = color;
439 geo.fViewMatrix = viewMatrix;
440 geo.fRect = rect;
joshualitt8cce8f12015-08-26 06:23:39 -0700441
joshualitt3566d442015-09-18 07:12:55 -0700442 if (localRect && localMatrix) {
443 geo.fLocalQuad.setFromMappedRect(*localRect, *localMatrix);
444 } else if (localRect) {
445 geo.fLocalQuad.set(*localRect);
446 } else if (localMatrix) {
447 geo.fLocalQuad.setFromMappedRect(rect, *localMatrix);
joshualitt9c80b5f2015-08-13 10:05:51 -0700448 } else {
joshualitt3566d442015-09-18 07:12:55 -0700449 geo.fLocalQuad.set(rect);
joshualitt9c80b5f2015-08-13 10:05:51 -0700450 }
bsalomonb8cbd202016-06-30 13:09:48 -0700451 batch->init();
452 return batch;
joshualitt9c80b5f2015-08-13 10:05:51 -0700453}
joshualitt3566d442015-09-18 07:12:55 -0700454
bsalomonb8cbd202016-06-30 13:09:48 -0700455GrDrawBatch* CreateWithPerspective(GrColor color,
456 const SkMatrix& viewMatrix,
457 const SkRect& rect,
458 const SkRect* localRect,
459 const SkMatrix* localMatrix) {
460 NonAAFillRectPerspectiveBatch* batch = NonAAFillRectPerspectiveBatch::Create();
joshualitt3566d442015-09-18 07:12:55 -0700461 SkASSERT(viewMatrix.hasPerspective() || (localMatrix && localMatrix->hasPerspective()));
bsalomon08d14152016-06-30 12:45:18 -0700462 NonAAFillRectPerspectiveBatch::Geometry& geo = batch->geoData()->push_back();
joshualitt3566d442015-09-18 07:12:55 -0700463
464 geo.fColor = color;
465 geo.fViewMatrix = viewMatrix;
466 geo.fRect = rect;
467 geo.fHasLocalRect = SkToBool(localRect);
468 geo.fHasLocalMatrix = SkToBool(localMatrix);
469 if (localMatrix) {
470 geo.fLocalMatrix = *localMatrix;
471 }
472 if (localRect) {
473 geo.fLocalRect = *localRect;
474 }
joshualitt3566d442015-09-18 07:12:55 -0700475 batch->init();
476 return batch;
477}
478
joshualitt9c80b5f2015-08-13 10:05:51 -0700479};
480
481///////////////////////////////////////////////////////////////////////////////////////////////////
482
483#ifdef GR_TEST_UTILS
484
485#include "GrBatchTest.h"
486
bsalomonabd30f52015-08-13 13:34:48 -0700487DRAW_BATCH_TEST_DEFINE(RectBatch) {
joshualitt2244c272015-08-21 10:33:15 -0700488 GrColor color = GrRandomColor(random);
489 SkRect rect = GrTest::TestRect(random);
490 SkRect localRect = GrTest::TestRect(random);
491 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
492 SkMatrix localMatrix = GrTest::TestMatrix(random);
joshualitt9c80b5f2015-08-13 10:05:51 -0700493
joshualitt2244c272015-08-21 10:33:15 -0700494 bool hasLocalRect = random->nextBool();
495 bool hasLocalMatrix = random->nextBool();
joshualittbcf33d52015-08-26 08:10:35 -0700496 return GrNonAAFillRectBatch::Create(color, viewMatrix, rect,
497 hasLocalRect ? &localRect : nullptr,
498 hasLocalMatrix ? &localMatrix : nullptr);
joshualitt9c80b5f2015-08-13 10:05:51 -0700499}
500
501#endif