blob: 175b5564fb1bcd6c6c1c56fc9e6390a936800c19 [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"
Brian Salomondad29232016-12-01 16:40:24 -050013#include "GrMeshDrawOp.h"
joshualitt9c80b5f2015-08-13 10:05:51 -070014#include "GrPrimitiveProcessor.h"
joshualittae5b2c62015-08-19 08:48:41 -070015#include "GrQuad.h"
Brian Salomondad29232016-12-01 16:40:24 -050016#include "GrResourceProvider.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
Brian Salomondad29232016-12-01 16:40:24 -050074class NonAAFillRectBatch : public GrMeshDrawOp {
joshualitt2244c272015-08-21 10:33:15 -070075public:
Brian Salomon25a88092016-12-01 09:36:50 -050076 DEFINE_OP_CLASS_ID
bsalomon08d14152016-06-30 12:45:18 -070077
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 }
Brian Salomon7c3e7182016-12-01 09:35:30 -0500110 str.append(DumpPipelineInfo(*this->pipeline()));
bsalomon08d14152016-06-30 12:45:18 -0700111 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
bsalomon9d7f1842016-07-01 08:01:36 -0700119 color->setKnownFourComponents(fRects[0].fColor);
bsalomonbc9b6a42016-07-01 05:35:51 -0700120 coverage->setKnownSingleComponent(0xff);
bsalomon08d14152016-06-30 12:45:18 -0700121 }
122
123 void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
bsalomon9d7f1842016-07-01 08:01:36 -0700124 overrides.getOverrideColorIfSet(&fRects[0].fColor);
bsalomon08d14152016-06-30 12:45:18 -0700125 fOverrides = overrides;
126 }
127
bsalomon08d14152016-06-30 12:45:18 -0700128private:
bsalomon08d14152016-06-30 12:45:18 -0700129 NonAAFillRectBatch() : INHERITED(ClassID()) {}
130
131 void onPrepareDraws(Target* target) const override {
robertphillips6abd1d12016-07-01 09:06:56 -0700132 sk_sp<GrGeometryProcessor> gp = make_gp(fOverrides.readsCoverage());
bsalomon08d14152016-06-30 12:45:18 -0700133 if (!gp) {
134 SkDebugf("Couldn't create GrGeometryProcessor\n");
135 return;
136 }
bsalomonbc9b6a42016-07-01 05:35:51 -0700137 SkASSERT(gp->getVertexStride() ==
138 sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr));
bsalomon08d14152016-06-30 12:45:18 -0700139
140 size_t vertexStride = gp->getVertexStride();
bsalomon9d7f1842016-07-01 08:01:36 -0700141 int instanceCount = fRects.count();
bsalomon08d14152016-06-30 12:45:18 -0700142
Hal Canary144caf52016-11-07 17:57:18 -0500143 sk_sp<const GrBuffer> indexBuffer(target->resourceProvider()->refQuadIndexBuffer());
bsalomon08d14152016-06-30 12:45:18 -0700144 InstancedHelper helper;
145 void* vertices = helper.init(target, kTriangles_GrPrimitiveType, vertexStride,
Hal Canary144caf52016-11-07 17:57:18 -0500146 indexBuffer.get(), kVertsPerInstance,
bsalomon08d14152016-06-30 12:45:18 -0700147 kIndicesPerInstance, instanceCount);
148 if (!vertices || !indexBuffer) {
149 SkDebugf("Could not allocate vertices\n");
150 return;
151 }
152
153 for (int i = 0; i < instanceCount; i++) {
154 intptr_t verts = reinterpret_cast<intptr_t>(vertices) +
155 i * kVertsPerInstance * vertexStride;
robertphillips6abd1d12016-07-01 09:06:56 -0700156 tesselate(verts, vertexStride, fRects[i].fColor, &fRects[i].fViewMatrix,
bsalomon9d7f1842016-07-01 08:01:36 -0700157 fRects[i].fRect, &fRects[i].fLocalQuad);
bsalomon08d14152016-06-30 12:45:18 -0700158 }
159 helper.recordDraw(target, gp.get());
160 }
161
Brian Salomon25a88092016-12-01 09:36:50 -0500162 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
bsalomon08d14152016-06-30 12:45:18 -0700163 NonAAFillRectBatch* that = t->cast<NonAAFillRectBatch>();
164 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
165 that->bounds(), caps)) {
166 return false;
167 }
168
bsalomon08d14152016-06-30 12:45:18 -0700169 // In the event of two batches, one who can tweak, one who cannot, we just fall back to
170 // not tweaking
171 if (fOverrides.canTweakAlphaForCoverage() && !that->fOverrides.canTweakAlphaForCoverage()) {
172 fOverrides = that->fOverrides;
173 }
174
bsalomon9d7f1842016-07-01 08:01:36 -0700175 fRects.push_back_n(that->fRects.count(), that->fRects.begin());
bsalomon88cf17d2016-07-08 06:40:56 -0700176 this->joinBounds(*that);
bsalomon08d14152016-06-30 12:45:18 -0700177 return true;
178 }
179
bsalomon9d7f1842016-07-01 08:01:36 -0700180 struct RectInfo {
181 GrColor fColor;
182 SkMatrix fViewMatrix;
183 SkRect fRect;
184 GrQuad fLocalQuad;
185 };
186
bsalomon08d14152016-06-30 12:45:18 -0700187 GrXPOverridesForBatch fOverrides;
bsalomon9d7f1842016-07-01 08:01:36 -0700188 SkSTArray<1, RectInfo, true> fRects;
bsalomon08d14152016-06-30 12:45:18 -0700189
Brian Salomondad29232016-12-01 16:40:24 -0500190 typedef GrMeshDrawOp INHERITED;
joshualitt2244c272015-08-21 10:33:15 -0700191};
192
bsalomonb8cbd202016-06-30 13:09:48 -0700193namespace GrNonAAFillRectBatch {
194
Brian Salomon9afd3712016-12-01 10:59:09 -0500195GrDrawOp* Create(GrColor color,
196 const SkMatrix& viewMatrix,
197 const SkRect& rect,
198 const SkRect* localRect,
199 const SkMatrix* localMatrix) {
bsalomon9d7f1842016-07-01 08:01:36 -0700200 return new NonAAFillRectBatch(color, viewMatrix, rect, localRect, localMatrix);
joshualitt9c80b5f2015-08-13 10:05:51 -0700201}
joshualitt3566d442015-09-18 07:12:55 -0700202
joshualitt9c80b5f2015-08-13 10:05:51 -0700203};
204
205///////////////////////////////////////////////////////////////////////////////////////////////////
206
207#ifdef GR_TEST_UTILS
208
209#include "GrBatchTest.h"
210
bsalomonabd30f52015-08-13 13:34:48 -0700211DRAW_BATCH_TEST_DEFINE(RectBatch) {
joshualitt2244c272015-08-21 10:33:15 -0700212 GrColor color = GrRandomColor(random);
213 SkRect rect = GrTest::TestRect(random);
214 SkRect localRect = GrTest::TestRect(random);
215 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
216 SkMatrix localMatrix = GrTest::TestMatrix(random);
joshualitt9c80b5f2015-08-13 10:05:51 -0700217
joshualitt2244c272015-08-21 10:33:15 -0700218 bool hasLocalRect = random->nextBool();
219 bool hasLocalMatrix = random->nextBool();
joshualittbcf33d52015-08-26 08:10:35 -0700220 return GrNonAAFillRectBatch::Create(color, viewMatrix, rect,
221 hasLocalRect ? &localRect : nullptr,
222 hasLocalMatrix ? &localMatrix : nullptr);
joshualitt9c80b5f2015-08-13 10:05:51 -0700223}
224
225#endif