blob: cc89d03870fb2a8689b6cad32151f1a132f70d14 [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
reeda39667c2016-08-22 06:39:49 -070018#include "SkMatrixPriv.h"
19
bsalomon08d14152016-06-30 12:45:18 -070020static const int kVertsPerInstance = 4;
21static const int kIndicesPerInstance = 6;
joshualitt2244c272015-08-21 10:33:15 -070022
23/** We always use per-vertex colors so that rects can be batched across color changes. Sometimes
24 we have explicit local coords and sometimes not. We *could* always provide explicit local
25 coords and just duplicate the positions when the caller hasn't provided a local coord rect,
26 but we haven't seen a use case which frequently switches between local rect and no local
27 rect draws.
28
29 The vertex attrib order is always pos, color, [local coords].
30 */
robertphillips6abd1d12016-07-01 09:06:56 -070031static sk_sp<GrGeometryProcessor> make_gp(bool readsCoverage) {
32 using namespace GrDefaultGeoProcFactory;
33 Color color(Color::kAttribute_Type);
34 Coverage coverage(readsCoverage ? Coverage::kSolid_Type : Coverage::kNone_Type);
35
36 LocalCoords localCoords(LocalCoords::kHasExplicit_Type);
37 return GrDefaultGeoProcFactory::Make(color, coverage, localCoords, SkMatrix::I());
38}
39
joshualitt2244c272015-08-21 10:33:15 -070040static void tesselate(intptr_t vertices,
41 size_t vertexStride,
42 GrColor color,
robertphillips6abd1d12016-07-01 09:06:56 -070043 const SkMatrix* viewMatrix,
joshualitt2244c272015-08-21 10:33:15 -070044 const SkRect& rect,
joshualitt8cce8f12015-08-26 06:23:39 -070045 const GrQuad* localQuad) {
joshualitt2244c272015-08-21 10:33:15 -070046 SkPoint* positions = reinterpret_cast<SkPoint*>(vertices);
47
48 positions->setRectFan(rect.fLeft, rect.fTop,
49 rect.fRight, rect.fBottom, vertexStride);
joshualitt2244c272015-08-21 10:33:15 -070050
robertphillips6abd1d12016-07-01 09:06:56 -070051 if (viewMatrix) {
reeda39667c2016-08-22 06:39:49 -070052 SkMatrixPriv::MapPointsWithStride(*viewMatrix, positions, vertexStride, kVertsPerInstance);
joshualitt8cce8f12015-08-26 06:23:39 -070053 }
54
55 // Setup local coords
joshualitt2244c272015-08-21 10:33:15 -070056 // TODO we should only do this if local coords are being read
joshualitt8cce8f12015-08-26 06:23:39 -070057 if (localQuad) {
joshualitt2244c272015-08-21 10:33:15 -070058 static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor);
bsalomon08d14152016-06-30 12:45:18 -070059 for (int i = 0; i < kVertsPerInstance; i++) {
joshualitt8cce8f12015-08-26 06:23:39 -070060 SkPoint* coords = reinterpret_cast<SkPoint*>(vertices + kLocalOffset +
61 i * vertexStride);
62 *coords = localQuad->point(i);
joshualitt2244c272015-08-21 10:33:15 -070063 }
64 }
65
66 static const int kColorOffset = sizeof(SkPoint);
67 GrColor* vertColor = reinterpret_cast<GrColor*>(vertices + kColorOffset);
68 for (int j = 0; j < 4; ++j) {
69 *vertColor = color;
70 vertColor = (GrColor*) ((intptr_t) vertColor + vertexStride);
71 }
72}
73
bsalomon08d14152016-06-30 12:45:18 -070074class NonAAFillRectBatch : public GrVertexBatch {
joshualitt2244c272015-08-21 10:33:15 -070075public:
bsalomon08d14152016-06-30 12:45:18 -070076 DEFINE_BATCH_CLASS_ID
77
bsalomon9d7f1842016-07-01 08:01:36 -070078 NonAAFillRectBatch(GrColor color, const SkMatrix& viewMatrix, const SkRect& rect,
79 const SkRect* localRect, const SkMatrix* localMatrix)
80 : INHERITED(ClassID()) {
81 SkASSERT(!viewMatrix.hasPerspective() && (!localMatrix ||
82 !localMatrix->hasPerspective()));
83 RectInfo& info = fRects.push_back();
84 info.fColor = color;
85 info.fViewMatrix = viewMatrix;
86 info.fRect = rect;
87 if (localRect && localMatrix) {
88 info.fLocalQuad.setFromMappedRect(*localRect, *localMatrix);
89 } else if (localRect) {
90 info.fLocalQuad.set(*localRect);
91 } else if (localMatrix) {
92 info.fLocalQuad.setFromMappedRect(rect, *localMatrix);
93 } else {
94 info.fLocalQuad.set(rect);
95 }
bsalomon88cf17d2016-07-08 06:40:56 -070096 this->setTransformedBounds(fRects[0].fRect, viewMatrix, HasAABloat::kNo, IsZeroArea::kNo);
bsalomon9d7f1842016-07-01 08:01:36 -070097 }
bsalomon08d14152016-06-30 12:45:18 -070098
bsalomonbc9b6a42016-07-01 05:35:51 -070099 const char* name() const override { return "NonAAFillRectBatch"; }
bsalomon08d14152016-06-30 12:45:18 -0700100
101 SkString dumpInfo() const override {
102 SkString str;
bsalomon9d7f1842016-07-01 08:01:36 -0700103 str.appendf("# batched: %d\n", fRects.count());
104 for (int i = 0; i < fRects.count(); ++i) {
105 const RectInfo& info = fRects[i];
bsalomonbc9b6a42016-07-01 05:35:51 -0700106 str.appendf("%d: Color: 0x%08x, Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n",
bsalomon9d7f1842016-07-01 08:01:36 -0700107 i, info.fColor,
108 info.fRect.fLeft, info.fRect.fTop, info.fRect.fRight, info.fRect.fBottom);
bsalomon08d14152016-06-30 12:45:18 -0700109 }
110 str.append(INHERITED::dumpInfo());
111 return str;
112 }
113
114 void computePipelineOptimizations(GrInitInvariantOutput* color,
115 GrInitInvariantOutput* coverage,
116 GrBatchToXPOverrides* overrides) const override {
117 // When this is called on a batch, there is only one geometry bundle
bsalomon9d7f1842016-07-01 08:01:36 -0700118 color->setKnownFourComponents(fRects[0].fColor);
bsalomonbc9b6a42016-07-01 05:35:51 -0700119 coverage->setKnownSingleComponent(0xff);
bsalomon08d14152016-06-30 12:45:18 -0700120 }
121
122 void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
bsalomon9d7f1842016-07-01 08:01:36 -0700123 overrides.getOverrideColorIfSet(&fRects[0].fColor);
bsalomon08d14152016-06-30 12:45:18 -0700124 fOverrides = overrides;
125 }
126
bsalomon08d14152016-06-30 12:45:18 -0700127private:
bsalomon08d14152016-06-30 12:45:18 -0700128 NonAAFillRectBatch() : INHERITED(ClassID()) {}
129
130 void onPrepareDraws(Target* target) const override {
robertphillips6abd1d12016-07-01 09:06:56 -0700131 sk_sp<GrGeometryProcessor> gp = make_gp(fOverrides.readsCoverage());
bsalomon08d14152016-06-30 12:45:18 -0700132 if (!gp) {
133 SkDebugf("Couldn't create GrGeometryProcessor\n");
134 return;
135 }
bsalomonbc9b6a42016-07-01 05:35:51 -0700136 SkASSERT(gp->getVertexStride() ==
137 sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr));
bsalomon08d14152016-06-30 12:45:18 -0700138
139 size_t vertexStride = gp->getVertexStride();
bsalomon9d7f1842016-07-01 08:01:36 -0700140 int instanceCount = fRects.count();
bsalomon08d14152016-06-30 12:45:18 -0700141
Hal Canary144caf52016-11-07 17:57:18 -0500142 sk_sp<const GrBuffer> indexBuffer(target->resourceProvider()->refQuadIndexBuffer());
bsalomon08d14152016-06-30 12:45:18 -0700143 InstancedHelper helper;
144 void* vertices = helper.init(target, kTriangles_GrPrimitiveType, vertexStride,
Hal Canary144caf52016-11-07 17:57:18 -0500145 indexBuffer.get(), kVertsPerInstance,
bsalomon08d14152016-06-30 12:45:18 -0700146 kIndicesPerInstance, instanceCount);
147 if (!vertices || !indexBuffer) {
148 SkDebugf("Could not allocate vertices\n");
149 return;
150 }
151
152 for (int i = 0; i < instanceCount; i++) {
153 intptr_t verts = reinterpret_cast<intptr_t>(vertices) +
154 i * kVertsPerInstance * vertexStride;
robertphillips6abd1d12016-07-01 09:06:56 -0700155 tesselate(verts, vertexStride, fRects[i].fColor, &fRects[i].fViewMatrix,
bsalomon9d7f1842016-07-01 08:01:36 -0700156 fRects[i].fRect, &fRects[i].fLocalQuad);
bsalomon08d14152016-06-30 12:45:18 -0700157 }
158 helper.recordDraw(target, gp.get());
159 }
160
bsalomon08d14152016-06-30 12:45:18 -0700161 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
162 NonAAFillRectBatch* that = t->cast<NonAAFillRectBatch>();
163 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
164 that->bounds(), caps)) {
165 return false;
166 }
167
bsalomon08d14152016-06-30 12:45:18 -0700168 // In the event of two batches, one who can tweak, one who cannot, we just fall back to
169 // not tweaking
170 if (fOverrides.canTweakAlphaForCoverage() && !that->fOverrides.canTweakAlphaForCoverage()) {
171 fOverrides = that->fOverrides;
172 }
173
bsalomon9d7f1842016-07-01 08:01:36 -0700174 fRects.push_back_n(that->fRects.count(), that->fRects.begin());
bsalomon88cf17d2016-07-08 06:40:56 -0700175 this->joinBounds(*that);
bsalomon08d14152016-06-30 12:45:18 -0700176 return true;
177 }
178
bsalomon9d7f1842016-07-01 08:01:36 -0700179 struct RectInfo {
180 GrColor fColor;
181 SkMatrix fViewMatrix;
182 SkRect fRect;
183 GrQuad fLocalQuad;
184 };
185
bsalomon08d14152016-06-30 12:45:18 -0700186 GrXPOverridesForBatch fOverrides;
bsalomon9d7f1842016-07-01 08:01:36 -0700187 SkSTArray<1, RectInfo, true> fRects;
bsalomon08d14152016-06-30 12:45:18 -0700188
189 typedef GrVertexBatch INHERITED;
joshualitt2244c272015-08-21 10:33:15 -0700190};
191
bsalomonb8cbd202016-06-30 13:09:48 -0700192namespace GrNonAAFillRectBatch {
193
194GrDrawBatch* Create(GrColor color,
195 const SkMatrix& viewMatrix,
196 const SkRect& rect,
197 const SkRect* localRect,
198 const SkMatrix* localMatrix) {
bsalomon9d7f1842016-07-01 08:01:36 -0700199 return new NonAAFillRectBatch(color, viewMatrix, rect, localRect, localMatrix);
joshualitt9c80b5f2015-08-13 10:05:51 -0700200}
joshualitt3566d442015-09-18 07:12:55 -0700201
joshualitt9c80b5f2015-08-13 10:05:51 -0700202};
203
204///////////////////////////////////////////////////////////////////////////////////////////////////
205
206#ifdef GR_TEST_UTILS
207
208#include "GrBatchTest.h"
209
bsalomonabd30f52015-08-13 13:34:48 -0700210DRAW_BATCH_TEST_DEFINE(RectBatch) {
joshualitt2244c272015-08-21 10:33:15 -0700211 GrColor color = GrRandomColor(random);
212 SkRect rect = GrTest::TestRect(random);
213 SkRect localRect = GrTest::TestRect(random);
214 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
215 SkMatrix localMatrix = GrTest::TestMatrix(random);
joshualitt9c80b5f2015-08-13 10:05:51 -0700216
joshualitt2244c272015-08-21 10:33:15 -0700217 bool hasLocalRect = random->nextBool();
218 bool hasLocalMatrix = random->nextBool();
joshualittbcf33d52015-08-26 08:10:35 -0700219 return GrNonAAFillRectBatch::Create(color, viewMatrix, rect,
220 hasLocalRect ? &localRect : nullptr,
221 hasLocalMatrix ? &localMatrix : nullptr);
joshualitt9c80b5f2015-08-13 10:05:51 -0700222}
223
224#endif