blob: aaeabdaeb7f9f10513147df44695f5ba923931e0 [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 */
robertphillips6abd1d12016-07-01 09:06:56 -070029static sk_sp<GrGeometryProcessor> make_gp(bool readsCoverage) {
30 using namespace GrDefaultGeoProcFactory;
31 Color color(Color::kAttribute_Type);
32 Coverage coverage(readsCoverage ? Coverage::kSolid_Type : Coverage::kNone_Type);
33
34 LocalCoords localCoords(LocalCoords::kHasExplicit_Type);
35 return GrDefaultGeoProcFactory::Make(color, coverage, localCoords, SkMatrix::I());
36}
37
joshualitt2244c272015-08-21 10:33:15 -070038static void tesselate(intptr_t vertices,
39 size_t vertexStride,
40 GrColor color,
robertphillips6abd1d12016-07-01 09:06:56 -070041 const SkMatrix* viewMatrix,
joshualitt2244c272015-08-21 10:33:15 -070042 const SkRect& rect,
joshualitt8cce8f12015-08-26 06:23:39 -070043 const GrQuad* localQuad) {
joshualitt2244c272015-08-21 10:33:15 -070044 SkPoint* positions = reinterpret_cast<SkPoint*>(vertices);
45
46 positions->setRectFan(rect.fLeft, rect.fTop,
47 rect.fRight, rect.fBottom, vertexStride);
joshualitt2244c272015-08-21 10:33:15 -070048
robertphillips6abd1d12016-07-01 09:06:56 -070049 if (viewMatrix) {
50 viewMatrix->mapPointsWithStride(positions, vertexStride, kVertsPerInstance);
joshualitt8cce8f12015-08-26 06:23:39 -070051 }
52
53 // Setup local coords
joshualitt2244c272015-08-21 10:33:15 -070054 // TODO we should only do this if local coords are being read
joshualitt8cce8f12015-08-26 06:23:39 -070055 if (localQuad) {
joshualitt2244c272015-08-21 10:33:15 -070056 static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor);
bsalomon08d14152016-06-30 12:45:18 -070057 for (int i = 0; i < kVertsPerInstance; i++) {
joshualitt8cce8f12015-08-26 06:23:39 -070058 SkPoint* coords = reinterpret_cast<SkPoint*>(vertices + kLocalOffset +
59 i * vertexStride);
60 *coords = localQuad->point(i);
joshualitt2244c272015-08-21 10:33:15 -070061 }
62 }
63
64 static const int kColorOffset = sizeof(SkPoint);
65 GrColor* vertColor = reinterpret_cast<GrColor*>(vertices + kColorOffset);
66 for (int j = 0; j < 4; ++j) {
67 *vertColor = color;
68 vertColor = (GrColor*) ((intptr_t) vertColor + vertexStride);
69 }
70}
71
bsalomon08d14152016-06-30 12:45:18 -070072class NonAAFillRectBatch : public GrVertexBatch {
joshualitt2244c272015-08-21 10:33:15 -070073public:
bsalomon08d14152016-06-30 12:45:18 -070074 DEFINE_BATCH_CLASS_ID
75
bsalomon9d7f1842016-07-01 08:01:36 -070076 NonAAFillRectBatch(GrColor color, const SkMatrix& viewMatrix, const SkRect& rect,
77 const SkRect* localRect, const SkMatrix* localMatrix)
78 : INHERITED(ClassID()) {
79 SkASSERT(!viewMatrix.hasPerspective() && (!localMatrix ||
80 !localMatrix->hasPerspective()));
81 RectInfo& info = fRects.push_back();
82 info.fColor = color;
83 info.fViewMatrix = viewMatrix;
84 info.fRect = rect;
85 if (localRect && localMatrix) {
86 info.fLocalQuad.setFromMappedRect(*localRect, *localMatrix);
87 } else if (localRect) {
88 info.fLocalQuad.set(*localRect);
89 } else if (localMatrix) {
90 info.fLocalQuad.setFromMappedRect(rect, *localMatrix);
91 } else {
92 info.fLocalQuad.set(rect);
93 }
94 viewMatrix.mapRect(&fBounds, fRects[0].fRect);
95 }
bsalomon08d14152016-06-30 12:45:18 -070096
bsalomonbc9b6a42016-07-01 05:35:51 -070097 const char* name() const override { return "NonAAFillRectBatch"; }
bsalomon08d14152016-06-30 12:45:18 -070098
99 SkString dumpInfo() const override {
100 SkString str;
bsalomon9d7f1842016-07-01 08:01:36 -0700101 str.appendf("# batched: %d\n", fRects.count());
102 for (int i = 0; i < fRects.count(); ++i) {
103 const RectInfo& info = fRects[i];
bsalomonbc9b6a42016-07-01 05:35:51 -0700104 str.appendf("%d: Color: 0x%08x, Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n",
bsalomon9d7f1842016-07-01 08:01:36 -0700105 i, info.fColor,
106 info.fRect.fLeft, info.fRect.fTop, info.fRect.fRight, info.fRect.fBottom);
bsalomon08d14152016-06-30 12:45:18 -0700107 }
108 str.append(INHERITED::dumpInfo());
109 return str;
110 }
111
112 void computePipelineOptimizations(GrInitInvariantOutput* color,
113 GrInitInvariantOutput* coverage,
114 GrBatchToXPOverrides* overrides) const override {
115 // When this is called on a batch, there is only one geometry bundle
bsalomon9d7f1842016-07-01 08:01:36 -0700116 color->setKnownFourComponents(fRects[0].fColor);
bsalomonbc9b6a42016-07-01 05:35:51 -0700117 coverage->setKnownSingleComponent(0xff);
bsalomon08d14152016-06-30 12:45:18 -0700118 }
119
120 void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
bsalomon9d7f1842016-07-01 08:01:36 -0700121 overrides.getOverrideColorIfSet(&fRects[0].fColor);
bsalomon08d14152016-06-30 12:45:18 -0700122 fOverrides = overrides;
123 }
124
bsalomon08d14152016-06-30 12:45:18 -0700125private:
bsalomon08d14152016-06-30 12:45:18 -0700126 NonAAFillRectBatch() : INHERITED(ClassID()) {}
127
128 void onPrepareDraws(Target* target) const override {
robertphillips6abd1d12016-07-01 09:06:56 -0700129 sk_sp<GrGeometryProcessor> gp = make_gp(fOverrides.readsCoverage());
bsalomon08d14152016-06-30 12:45:18 -0700130 if (!gp) {
131 SkDebugf("Couldn't create GrGeometryProcessor\n");
132 return;
133 }
bsalomonbc9b6a42016-07-01 05:35:51 -0700134 SkASSERT(gp->getVertexStride() ==
135 sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr));
bsalomon08d14152016-06-30 12:45:18 -0700136
137 size_t vertexStride = gp->getVertexStride();
bsalomon9d7f1842016-07-01 08:01:36 -0700138 int instanceCount = fRects.count();
bsalomon08d14152016-06-30 12:45:18 -0700139
bsalomonbc9b6a42016-07-01 05:35:51 -0700140 SkAutoTUnref<const GrBuffer> indexBuffer(target->resourceProvider()->refQuadIndexBuffer());
bsalomon08d14152016-06-30 12:45:18 -0700141 InstancedHelper helper;
142 void* vertices = helper.init(target, kTriangles_GrPrimitiveType, vertexStride,
143 indexBuffer, kVertsPerInstance,
144 kIndicesPerInstance, instanceCount);
145 if (!vertices || !indexBuffer) {
146 SkDebugf("Could not allocate vertices\n");
147 return;
148 }
149
150 for (int i = 0; i < instanceCount; i++) {
151 intptr_t verts = reinterpret_cast<intptr_t>(vertices) +
152 i * kVertsPerInstance * vertexStride;
robertphillips6abd1d12016-07-01 09:06:56 -0700153 tesselate(verts, vertexStride, fRects[i].fColor, &fRects[i].fViewMatrix,
bsalomon9d7f1842016-07-01 08:01:36 -0700154 fRects[i].fRect, &fRects[i].fLocalQuad);
bsalomon08d14152016-06-30 12:45:18 -0700155 }
156 helper.recordDraw(target, gp.get());
157 }
158
bsalomon08d14152016-06-30 12:45:18 -0700159 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
160 NonAAFillRectBatch* that = t->cast<NonAAFillRectBatch>();
161 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
162 that->bounds(), caps)) {
163 return false;
164 }
165
bsalomon08d14152016-06-30 12:45:18 -0700166 // In the event of two batches, one who can tweak, one who cannot, we just fall back to
167 // not tweaking
168 if (fOverrides.canTweakAlphaForCoverage() && !that->fOverrides.canTweakAlphaForCoverage()) {
169 fOverrides = that->fOverrides;
170 }
171
bsalomon9d7f1842016-07-01 08:01:36 -0700172 fRects.push_back_n(that->fRects.count(), that->fRects.begin());
bsalomon08d14152016-06-30 12:45:18 -0700173 this->joinBounds(that->bounds());
174 return true;
175 }
176
bsalomon9d7f1842016-07-01 08:01:36 -0700177 struct RectInfo {
178 GrColor fColor;
179 SkMatrix fViewMatrix;
180 SkRect fRect;
181 GrQuad fLocalQuad;
182 };
183
bsalomon08d14152016-06-30 12:45:18 -0700184 GrXPOverridesForBatch fOverrides;
bsalomon9d7f1842016-07-01 08:01:36 -0700185 SkSTArray<1, RectInfo, true> fRects;
bsalomon08d14152016-06-30 12:45:18 -0700186
187 typedef GrVertexBatch INHERITED;
joshualitt2244c272015-08-21 10:33:15 -0700188};
189
bsalomonb8cbd202016-06-30 13:09:48 -0700190namespace GrNonAAFillRectBatch {
191
192GrDrawBatch* Create(GrColor color,
193 const SkMatrix& viewMatrix,
194 const SkRect& rect,
195 const SkRect* localRect,
196 const SkMatrix* localMatrix) {
bsalomon9d7f1842016-07-01 08:01:36 -0700197 return new NonAAFillRectBatch(color, viewMatrix, rect, localRect, localMatrix);
joshualitt9c80b5f2015-08-13 10:05:51 -0700198}
joshualitt3566d442015-09-18 07:12:55 -0700199
joshualitt9c80b5f2015-08-13 10:05:51 -0700200};
201
202///////////////////////////////////////////////////////////////////////////////////////////////////
203
204#ifdef GR_TEST_UTILS
205
206#include "GrBatchTest.h"
207
bsalomonabd30f52015-08-13 13:34:48 -0700208DRAW_BATCH_TEST_DEFINE(RectBatch) {
joshualitt2244c272015-08-21 10:33:15 -0700209 GrColor color = GrRandomColor(random);
210 SkRect rect = GrTest::TestRect(random);
211 SkRect localRect = GrTest::TestRect(random);
212 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
213 SkMatrix localMatrix = GrTest::TestMatrix(random);
joshualitt9c80b5f2015-08-13 10:05:51 -0700214
joshualitt2244c272015-08-21 10:33:15 -0700215 bool hasLocalRect = random->nextBool();
216 bool hasLocalMatrix = random->nextBool();
joshualittbcf33d52015-08-26 08:10:35 -0700217 return GrNonAAFillRectBatch::Create(color, viewMatrix, rect,
218 hasLocalRect ? &localRect : nullptr,
219 hasLocalMatrix ? &localMatrix : nullptr);
joshualitt9c80b5f2015-08-13 10:05:51 -0700220}
221
222#endif