blob: 186c935c703fc905dc8f92bbbce21968a041fd7d [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
bsalomon08d14152016-06-30 12:45:18 -0700427inline static void append_to_batch(NonAAFillRectBatch* batch, GrColor color,
joshualittaa37a962015-09-18 13:03:25 -0700428 const SkMatrix& viewMatrix, const SkRect& rect,
429 const SkRect* localRect, const SkMatrix* localMatrix) {
joshualitt3566d442015-09-18 07:12:55 -0700430 SkASSERT(!viewMatrix.hasPerspective() && (!localMatrix || !localMatrix->hasPerspective()));
bsalomon08d14152016-06-30 12:45:18 -0700431 NonAAFillRectBatch::Geometry& geo = batch->geoData()->push_back();
joshualitt8cce8f12015-08-26 06:23:39 -0700432
joshualitt3566d442015-09-18 07:12:55 -0700433 geo.fColor = color;
434 geo.fViewMatrix = viewMatrix;
435 geo.fRect = rect;
joshualitt8cce8f12015-08-26 06:23:39 -0700436
joshualitt3566d442015-09-18 07:12:55 -0700437 if (localRect && localMatrix) {
438 geo.fLocalQuad.setFromMappedRect(*localRect, *localMatrix);
439 } else if (localRect) {
440 geo.fLocalQuad.set(*localRect);
441 } else if (localMatrix) {
442 geo.fLocalQuad.setFromMappedRect(rect, *localMatrix);
joshualitt9c80b5f2015-08-13 10:05:51 -0700443 } else {
joshualitt3566d442015-09-18 07:12:55 -0700444 geo.fLocalQuad.set(rect);
joshualitt9c80b5f2015-08-13 10:05:51 -0700445 }
joshualitt9c80b5f2015-08-13 10:05:51 -0700446}
joshualitt3566d442015-09-18 07:12:55 -0700447
bsalomon08d14152016-06-30 12:45:18 -0700448inline static void append_to_batch(NonAAFillRectPerspectiveBatch* batch, GrColor color,
joshualittaa37a962015-09-18 13:03:25 -0700449 const SkMatrix& viewMatrix, const SkRect& rect,
450 const SkRect* localRect, const SkMatrix* localMatrix) {
joshualitt3566d442015-09-18 07:12:55 -0700451 SkASSERT(viewMatrix.hasPerspective() || (localMatrix && localMatrix->hasPerspective()));
bsalomon08d14152016-06-30 12:45:18 -0700452 NonAAFillRectPerspectiveBatch::Geometry& geo = batch->geoData()->push_back();
joshualitt3566d442015-09-18 07:12:55 -0700453
454 geo.fColor = color;
455 geo.fViewMatrix = viewMatrix;
456 geo.fRect = rect;
457 geo.fHasLocalRect = SkToBool(localRect);
458 geo.fHasLocalMatrix = SkToBool(localMatrix);
459 if (localMatrix) {
460 geo.fLocalMatrix = *localMatrix;
461 }
462 if (localRect) {
463 geo.fLocalRect = *localRect;
464 }
465
joshualittaa37a962015-09-18 13:03:25 -0700466}
467
468namespace GrNonAAFillRectBatch {
469
470GrDrawBatch* Create(GrColor color,
471 const SkMatrix& viewMatrix,
472 const SkRect& rect,
473 const SkRect* localRect,
474 const SkMatrix* localMatrix) {
bsalomon08d14152016-06-30 12:45:18 -0700475 NonAAFillRectBatch* batch = NonAAFillRectBatch::Create();
joshualittaa37a962015-09-18 13:03:25 -0700476 append_to_batch(batch, color, viewMatrix, rect, localRect, localMatrix);
joshualitt3566d442015-09-18 07:12:55 -0700477 batch->init();
478 return batch;
479}
480
joshualittaa37a962015-09-18 13:03:25 -0700481GrDrawBatch* CreateWithPerspective(GrColor color,
482 const SkMatrix& viewMatrix,
483 const SkRect& rect,
484 const SkRect* localRect,
485 const SkMatrix* localMatrix) {
bsalomon08d14152016-06-30 12:45:18 -0700486 NonAAFillRectPerspectiveBatch* batch = NonAAFillRectPerspectiveBatch::Create();
joshualittaa37a962015-09-18 13:03:25 -0700487 append_to_batch(batch, color, viewMatrix, rect, localRect, localMatrix);
488 batch->init();
489 return batch;
490}
491
492bool Append(GrBatch* origBatch,
493 GrColor color,
494 const SkMatrix& viewMatrix,
495 const SkRect& rect,
496 const SkRect* localRect,
497 const SkMatrix* localMatrix) {
498 bool usePerspective = viewMatrix.hasPerspective() ||
499 (localMatrix && localMatrix->hasPerspective());
500
bsalomon08d14152016-06-30 12:45:18 -0700501 if (usePerspective && origBatch->classID() != NonAAFillRectPerspectiveBatch::ClassID()) {
joshualittaa37a962015-09-18 13:03:25 -0700502 return false;
503 }
504
505 if (!usePerspective) {
bsalomon08d14152016-06-30 12:45:18 -0700506 NonAAFillRectBatch* batch = origBatch->cast<NonAAFillRectBatch>();
joshualittaa37a962015-09-18 13:03:25 -0700507 append_to_batch(batch, color, viewMatrix, rect, localRect, localMatrix);
508 batch->updateBoundsAfterAppend();
509 } else {
bsalomon08d14152016-06-30 12:45:18 -0700510 NonAAFillRectPerspectiveBatch* batch = origBatch->cast<NonAAFillRectPerspectiveBatch>();
511 const NonAAFillRectPerspectiveBatch::Geometry& geo = batch->geoData()->back();
joshualittaa37a962015-09-18 13:03:25 -0700512
513 if (!geo.fViewMatrix.cheapEqualTo(viewMatrix) ||
514 geo.fHasLocalRect != SkToBool(localRect) ||
515 geo.fHasLocalMatrix != SkToBool(localMatrix) ||
516 (geo.fHasLocalMatrix && !geo.fLocalMatrix.cheapEqualTo(*localMatrix))) {
517 return false;
518 }
519
520 append_to_batch(batch, color, viewMatrix, rect, localRect, localMatrix);
521 batch->updateBoundsAfterAppend();
522 }
523
524 return true;
525}
526
joshualitt9c80b5f2015-08-13 10:05:51 -0700527};
528
529///////////////////////////////////////////////////////////////////////////////////////////////////
530
531#ifdef GR_TEST_UTILS
532
533#include "GrBatchTest.h"
534
bsalomonabd30f52015-08-13 13:34:48 -0700535DRAW_BATCH_TEST_DEFINE(RectBatch) {
joshualitt2244c272015-08-21 10:33:15 -0700536 GrColor color = GrRandomColor(random);
537 SkRect rect = GrTest::TestRect(random);
538 SkRect localRect = GrTest::TestRect(random);
539 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
540 SkMatrix localMatrix = GrTest::TestMatrix(random);
joshualitt9c80b5f2015-08-13 10:05:51 -0700541
joshualitt2244c272015-08-21 10:33:15 -0700542 bool hasLocalRect = random->nextBool();
543 bool hasLocalMatrix = random->nextBool();
joshualittbcf33d52015-08-26 08:10:35 -0700544 return GrNonAAFillRectBatch::Create(color, viewMatrix, rect,
545 hasLocalRect ? &localRect : nullptr,
546 hasLocalMatrix ? &localMatrix : nullptr);
joshualitt9c80b5f2015-08-13 10:05:51 -0700547}
548
549#endif