blob: faccdf6798db74907176dfd9479b5a008bac4957 [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
bsalomonbc9b6a42016-07-01 05:35:51 -0700103 const char* name() const override { return "NonAAFillRectBatch"; }
bsalomon08d14152016-06-30 12:45:18 -0700104
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) {
bsalomonbc9b6a42016-07-01 05:35:51 -0700109 const Geometry& geo = fGeoData[i];
110 str.appendf("%d: Color: 0x%08x, Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n",
111 i, geo.fColor,
112 geo.fRect.fLeft, geo.fRect.fTop, geo.fRect.fRight, geo.fRect.fBottom);
bsalomon08d14152016-06-30 12:45:18 -0700113 }
114 str.append(INHERITED::dumpInfo());
115 return str;
116 }
117
118 void computePipelineOptimizations(GrInitInvariantOutput* color,
119 GrInitInvariantOutput* coverage,
120 GrBatchToXPOverrides* overrides) const override {
121 // When this is called on a batch, there is only one geometry bundle
122 color->setKnownFourComponents(fGeoData[0].fColor);
bsalomonbc9b6a42016-07-01 05:35:51 -0700123 coverage->setKnownSingleComponent(0xff);
bsalomon08d14152016-06-30 12:45:18 -0700124 }
125
126 void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
127 overrides.getOverrideColorIfSet(&fGeoData[0].fColor);
128 fOverrides = overrides;
129 }
130
131 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
132
133 // After seeding, the client should call init() so the Batch can initialize itself
bsalomonbc9b6a42016-07-01 05:35:51 -0700134 void init() { fGeoData[0].fViewMatrix.mapRect(&fBounds, fGeoData[0].fRect); }
bsalomon08d14152016-06-30 12:45:18 -0700135
136private:
bsalomon08d14152016-06-30 12:45:18 -0700137 NonAAFillRectBatch() : INHERITED(ClassID()) {}
138
139 void onPrepareDraws(Target* target) const override {
bsalomonbc9b6a42016-07-01 05:35:51 -0700140 sk_sp<GrGeometryProcessor> gp = make_gp(fGeoData[0].fViewMatrix, fOverrides.readsCoverage(),
141 true, nullptr);
bsalomon08d14152016-06-30 12:45:18 -0700142 if (!gp) {
143 SkDebugf("Couldn't create GrGeometryProcessor\n");
144 return;
145 }
bsalomonbc9b6a42016-07-01 05:35:51 -0700146 SkASSERT(gp->getVertexStride() ==
147 sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr));
bsalomon08d14152016-06-30 12:45:18 -0700148
149 size_t vertexStride = gp->getVertexStride();
150 int instanceCount = fGeoData.count();
151
bsalomonbc9b6a42016-07-01 05:35:51 -0700152 SkAutoTUnref<const GrBuffer> indexBuffer(target->resourceProvider()->refQuadIndexBuffer());
bsalomon08d14152016-06-30 12:45:18 -0700153 InstancedHelper helper;
154 void* vertices = helper.init(target, kTriangles_GrPrimitiveType, vertexStride,
155 indexBuffer, kVertsPerInstance,
156 kIndicesPerInstance, instanceCount);
157 if (!vertices || !indexBuffer) {
158 SkDebugf("Could not allocate vertices\n");
159 return;
160 }
161
162 for (int i = 0; i < instanceCount; i++) {
163 intptr_t verts = reinterpret_cast<intptr_t>(vertices) +
164 i * kVertsPerInstance * vertexStride;
bsalomonbc9b6a42016-07-01 05:35:51 -0700165 tesselate(verts, vertexStride, fGeoData[i].fColor, fGeoData[i].fViewMatrix,
166 fGeoData[i].fRect, &fGeoData[i].fLocalQuad);
bsalomon08d14152016-06-30 12:45:18 -0700167 }
168 helper.recordDraw(target, gp.get());
169 }
170
bsalomon08d14152016-06-30 12:45:18 -0700171 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
172 NonAAFillRectBatch* that = t->cast<NonAAFillRectBatch>();
173 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
174 that->bounds(), caps)) {
175 return false;
176 }
177
bsalomon08d14152016-06-30 12:45:18 -0700178 // In the event of two batches, one who can tweak, one who cannot, we just fall back to
179 // not tweaking
180 if (fOverrides.canTweakAlphaForCoverage() && !that->fOverrides.canTweakAlphaForCoverage()) {
181 fOverrides = that->fOverrides;
182 }
183
184 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
185 this->joinBounds(that->bounds());
186 return true;
187 }
188
189 GrXPOverridesForBatch fOverrides;
190 SkSTArray<1, Geometry, true> fGeoData;
191
192 typedef GrVertexBatch INHERITED;
joshualitt2244c272015-08-21 10:33:15 -0700193};
194
joshualitt8cce8f12015-08-26 06:23:39 -0700195// We handle perspective in the local matrix or viewmatrix with special batches
bsalomon08d14152016-06-30 12:45:18 -0700196class NonAAFillRectPerspectiveBatch : public GrVertexBatch {
joshualitt2244c272015-08-21 10:33:15 -0700197public:
bsalomon08d14152016-06-30 12:45:18 -0700198 DEFINE_BATCH_CLASS_ID
199
joshualitt2244c272015-08-21 10:33:15 -0700200 struct Geometry {
201 SkMatrix fViewMatrix;
202 SkMatrix fLocalMatrix;
203 SkRect fRect;
204 SkRect fLocalRect;
joshualittae41b382015-08-19 06:54:08 -0700205 GrColor fColor;
joshualitt8cce8f12015-08-26 06:23:39 -0700206 bool fHasLocalMatrix;
207 bool fHasLocalRect;
joshualittae41b382015-08-19 06:54:08 -0700208 };
209
bsalomon08d14152016-06-30 12:45:18 -0700210 static NonAAFillRectPerspectiveBatch* Create() { return new NonAAFillRectPerspectiveBatch; }
211
bsalomonbc9b6a42016-07-01 05:35:51 -0700212 const char* name() const override { return "NonAAFillRectPerspectiveBatch"; }
bsalomon08d14152016-06-30 12:45:18 -0700213
214 SkString dumpInfo() const override {
215 SkString str;
216 str.appendf("# batched: %d\n", fGeoData.count());
217 for (int i = 0; i < fGeoData.count(); ++i) {
bsalomonbc9b6a42016-07-01 05:35:51 -0700218 const Geometry& geo = fGeoData[0];
219 str.appendf("%d: Color: 0x%08x, Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n",
220 i, geo.fColor,
221 geo.fRect.fLeft, geo.fRect.fTop, geo.fRect.fRight, geo.fRect.fBottom);
bsalomon08d14152016-06-30 12:45:18 -0700222 }
223 str.append(INHERITED::dumpInfo());
224 return str;
225 }
226
227 void computePipelineOptimizations(GrInitInvariantOutput* color,
228 GrInitInvariantOutput* coverage,
229 GrBatchToXPOverrides* overrides) const override {
230 // When this is called on a batch, there is only one geometry bundle
231 color->setKnownFourComponents(fGeoData[0].fColor);
bsalomonbc9b6a42016-07-01 05:35:51 -0700232 coverage->setKnownSingleComponent(0xff);
bsalomon08d14152016-06-30 12:45:18 -0700233 }
234
235 void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
236 overrides.getOverrideColorIfSet(&fGeoData[0].fColor);
237 fOverrides = overrides;
238 }
239
240 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
241
242 // After seeding, the client should call init() so the Batch can initialize itself
bsalomonbc9b6a42016-07-01 05:35:51 -0700243 void init() { fGeoData[0].fViewMatrix.mapRect(&fBounds, fGeoData[0].fRect); }
bsalomon08d14152016-06-30 12:45:18 -0700244
245private:
bsalomon08d14152016-06-30 12:45:18 -0700246 NonAAFillRectPerspectiveBatch() : INHERITED(ClassID()) {}
247
248 void onPrepareDraws(Target* target) const override {
bsalomonbc9b6a42016-07-01 05:35:51 -0700249 sk_sp<GrGeometryProcessor> gp = make_gp(fGeoData[0].fViewMatrix, fOverrides.readsCoverage(),
250 fGeoData[0].fHasLocalRect,
251 fGeoData[0].fHasLocalMatrix
252 ? &fGeoData[0].fLocalMatrix
253 : nullptr);
bsalomon08d14152016-06-30 12:45:18 -0700254 if (!gp) {
255 SkDebugf("Couldn't create GrGeometryProcessor\n");
256 return;
257 }
bsalomonbc9b6a42016-07-01 05:35:51 -0700258 SkASSERT(fGeoData[0].fHasLocalRect
259 ? gp->getVertexStride() ==
260 sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr)
261 : gp->getVertexStride() == sizeof(GrDefaultGeoProcFactory::PositionColorAttr));
bsalomon08d14152016-06-30 12:45:18 -0700262
263 size_t vertexStride = gp->getVertexStride();
264 int instanceCount = fGeoData.count();
265
bsalomonbc9b6a42016-07-01 05:35:51 -0700266 SkAutoTUnref<const GrBuffer> indexBuffer(target->resourceProvider()->refQuadIndexBuffer());
bsalomon08d14152016-06-30 12:45:18 -0700267 InstancedHelper helper;
268 void* vertices = helper.init(target, kTriangles_GrPrimitiveType, vertexStride,
269 indexBuffer, kVertsPerInstance,
270 kIndicesPerInstance, instanceCount);
271 if (!vertices || !indexBuffer) {
272 SkDebugf("Could not allocate vertices\n");
273 return;
274 }
275
276 for (int i = 0; i < instanceCount; i++) {
bsalomonbc9b6a42016-07-01 05:35:51 -0700277 const Geometry& geo = fGeoData[i];
bsalomon08d14152016-06-30 12:45:18 -0700278 intptr_t verts = reinterpret_cast<intptr_t>(vertices) +
279 i * kVertsPerInstance * vertexStride;
bsalomonbc9b6a42016-07-01 05:35:51 -0700280 if (geo.fHasLocalRect) {
281 GrQuad quad(geo.fLocalRect);
282 tesselate(verts, vertexStride, geo.fColor, geo.fViewMatrix, geo.fRect, &quad);
283 } else {
284 tesselate(verts, vertexStride, geo.fColor, geo.fViewMatrix, geo.fRect, nullptr);
285 }
bsalomon08d14152016-06-30 12:45:18 -0700286 }
287 helper.recordDraw(target, gp.get());
288 }
289
290 const Geometry& seedGeometry() const { return fGeoData[0]; }
291
292 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
293 NonAAFillRectPerspectiveBatch* that = t->cast<NonAAFillRectPerspectiveBatch>();
294 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
295 that->bounds(), caps)) {
296 return false;
297 }
298
bsalomonbc9b6a42016-07-01 05:35:51 -0700299 // We could batch across perspective vm changes if we really wanted to
300 if (!fGeoData[0].fViewMatrix.cheapEqualTo(that->fGeoData[0].fViewMatrix)) {
301 return false;
302 }
303 if (fGeoData[0].fHasLocalRect != that->fGeoData[0].fHasLocalRect) {
304 return false;
305 }
306 if (fGeoData[0].fHasLocalMatrix &&
307 !fGeoData[0].fLocalMatrix.cheapEqualTo(that->fGeoData[0].fLocalMatrix)) {
bsalomon08d14152016-06-30 12:45:18 -0700308 return false;
309 }
310
311 // In the event of two batches, one who can tweak, one who cannot, we just fall back to
312 // not tweaking
313 if (fOverrides.canTweakAlphaForCoverage() && !that->fOverrides.canTweakAlphaForCoverage()) {
314 fOverrides = that->fOverrides;
315 }
316
317 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
318 this->joinBounds(that->bounds());
319 return true;
320 }
321
322 GrXPOverridesForBatch fOverrides;
323 SkSTArray<1, Geometry, true> fGeoData;
324
325 typedef GrVertexBatch INHERITED;
joshualittae41b382015-08-19 06:54:08 -0700326};
327
bsalomonb8cbd202016-06-30 13:09:48 -0700328namespace GrNonAAFillRectBatch {
329
330GrDrawBatch* Create(GrColor color,
331 const SkMatrix& viewMatrix,
332 const SkRect& rect,
333 const SkRect* localRect,
334 const SkMatrix* localMatrix) {
335 NonAAFillRectBatch* batch = NonAAFillRectBatch::Create();
joshualitt3566d442015-09-18 07:12:55 -0700336 SkASSERT(!viewMatrix.hasPerspective() && (!localMatrix || !localMatrix->hasPerspective()));
bsalomon08d14152016-06-30 12:45:18 -0700337 NonAAFillRectBatch::Geometry& geo = batch->geoData()->push_back();
joshualitt8cce8f12015-08-26 06:23:39 -0700338
joshualitt3566d442015-09-18 07:12:55 -0700339 geo.fColor = color;
340 geo.fViewMatrix = viewMatrix;
341 geo.fRect = rect;
joshualitt8cce8f12015-08-26 06:23:39 -0700342
joshualitt3566d442015-09-18 07:12:55 -0700343 if (localRect && localMatrix) {
344 geo.fLocalQuad.setFromMappedRect(*localRect, *localMatrix);
345 } else if (localRect) {
346 geo.fLocalQuad.set(*localRect);
347 } else if (localMatrix) {
348 geo.fLocalQuad.setFromMappedRect(rect, *localMatrix);
joshualitt9c80b5f2015-08-13 10:05:51 -0700349 } else {
joshualitt3566d442015-09-18 07:12:55 -0700350 geo.fLocalQuad.set(rect);
joshualitt9c80b5f2015-08-13 10:05:51 -0700351 }
bsalomonb8cbd202016-06-30 13:09:48 -0700352 batch->init();
353 return batch;
joshualitt9c80b5f2015-08-13 10:05:51 -0700354}
joshualitt3566d442015-09-18 07:12:55 -0700355
bsalomonb8cbd202016-06-30 13:09:48 -0700356GrDrawBatch* CreateWithPerspective(GrColor color,
357 const SkMatrix& viewMatrix,
358 const SkRect& rect,
359 const SkRect* localRect,
360 const SkMatrix* localMatrix) {
361 NonAAFillRectPerspectiveBatch* batch = NonAAFillRectPerspectiveBatch::Create();
joshualitt3566d442015-09-18 07:12:55 -0700362 SkASSERT(viewMatrix.hasPerspective() || (localMatrix && localMatrix->hasPerspective()));
bsalomon08d14152016-06-30 12:45:18 -0700363 NonAAFillRectPerspectiveBatch::Geometry& geo = batch->geoData()->push_back();
joshualitt3566d442015-09-18 07:12:55 -0700364
365 geo.fColor = color;
366 geo.fViewMatrix = viewMatrix;
367 geo.fRect = rect;
368 geo.fHasLocalRect = SkToBool(localRect);
369 geo.fHasLocalMatrix = SkToBool(localMatrix);
370 if (localMatrix) {
371 geo.fLocalMatrix = *localMatrix;
372 }
373 if (localRect) {
374 geo.fLocalRect = *localRect;
375 }
joshualitt3566d442015-09-18 07:12:55 -0700376 batch->init();
377 return batch;
378}
379
joshualitt9c80b5f2015-08-13 10:05:51 -0700380};
381
382///////////////////////////////////////////////////////////////////////////////////////////////////
383
384#ifdef GR_TEST_UTILS
385
386#include "GrBatchTest.h"
387
bsalomonabd30f52015-08-13 13:34:48 -0700388DRAW_BATCH_TEST_DEFINE(RectBatch) {
joshualitt2244c272015-08-21 10:33:15 -0700389 GrColor color = GrRandomColor(random);
390 SkRect rect = GrTest::TestRect(random);
391 SkRect localRect = GrTest::TestRect(random);
392 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
393 SkMatrix localMatrix = GrTest::TestMatrix(random);
joshualitt9c80b5f2015-08-13 10:05:51 -0700394
joshualitt2244c272015-08-21 10:33:15 -0700395 bool hasLocalRect = random->nextBool();
396 bool hasLocalMatrix = random->nextBool();
joshualittbcf33d52015-08-26 08:10:35 -0700397 return GrNonAAFillRectBatch::Create(color, viewMatrix, rect,
398 hasLocalRect ? &localRect : nullptr,
399 hasLocalMatrix ? &localMatrix : nullptr);
joshualitt9c80b5f2015-08-13 10:05:51 -0700400}
401
402#endif