blob: 6430cf2022d6c8a59244e3ce20999f2604ccee6a [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
8#include "GrBWFillRectBatch.h"
9
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"
bsalomon16b99132015-08-13 14:55:50 -070014#include "GrVertexBatch.h"
joshualitt9c80b5f2015-08-13 10:05:51 -070015
bsalomon75398562015-08-17 12:55:38 -070016class GrBatchFlushState;
joshualitt9c80b5f2015-08-13 10:05:51 -070017class SkMatrix;
18struct SkRect;
19
bsalomonabd30f52015-08-13 13:34:48 -070020class BWFillRectBatch : public GrVertexBatch {
joshualitt9c80b5f2015-08-13 10:05:51 -070021public:
22 struct Geometry {
23 SkMatrix fViewMatrix;
24 SkRect fRect;
25 SkRect fLocalRect;
26 SkMatrix fLocalMatrix;
27 GrColor fColor;
28 bool fHasLocalRect;
29 bool fHasLocalMatrix;
30 };
31
bsalomonabd30f52015-08-13 13:34:48 -070032 static GrDrawBatch* Create(const Geometry& geometry) {
joshualitt9c80b5f2015-08-13 10:05:51 -070033 return SkNEW_ARGS(BWFillRectBatch, (geometry));
34 }
35
36 const char* name() const override { return "RectBatch"; }
37
38 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
39 // When this is called on a batch, there is only one geometry bundle
40 out->setKnownFourComponents(fGeoData[0].fColor);
41 }
42
43 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
44 out->setKnownSingleComponent(0xff);
45 }
46
bsalomone46f9fe2015-08-18 06:05:14 -070047 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
48
49private:
50 BWFillRectBatch(const Geometry& geometry) {
51 this->initClassID<BWFillRectBatch>();
52 fGeoData.push_back(geometry);
53
54 fBounds = geometry.fRect;
55 geometry.fViewMatrix.mapRect(&fBounds);
56 }
57
58 GrColor color() const { return fBatch.fColor; }
59 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
60 bool colorIgnored() const { return fBatch.fColorIgnored; }
61 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
62 const SkMatrix& localMatrix() const { return fGeoData[0].fLocalMatrix; }
63 bool hasLocalRect() const { return fGeoData[0].fHasLocalRect; }
64 bool hasLocalMatrix() const { return fGeoData[0].fHasLocalMatrix; }
65 bool coverageIgnored() const { return fBatch.fCoverageIgnored; }
66
joshualitt9c80b5f2015-08-13 10:05:51 -070067 void initBatchTracker(const GrPipelineOptimizations& init) override {
68 // Handle any color overrides
69 if (!init.readsColor()) {
70 fGeoData[0].fColor = GrColor_ILLEGAL;
71 }
72 init.getOverrideColorIfSet(&fGeoData[0].fColor);
73
74 // setup batch properties
75 fBatch.fColorIgnored = !init.readsColor();
76 fBatch.fColor = fGeoData[0].fColor;
77 fBatch.fUsesLocalCoords = init.readsLocalCoords();
78 fBatch.fCoverageIgnored = !init.readsCoverage();
79 }
80
bsalomon75398562015-08-17 12:55:38 -070081 void onPrepareDraws(Target* target) override {
joshualitt9c80b5f2015-08-13 10:05:51 -070082 SkAutoTUnref<const GrGeometryProcessor> gp(this->createRectGP());
83 if (!gp) {
84 SkDebugf("Could not create GrGeometryProcessor\n");
85 return;
86 }
87
bsalomon75398562015-08-17 12:55:38 -070088 target->initDraw(gp, this->pipeline());
joshualitt9c80b5f2015-08-13 10:05:51 -070089
90 int instanceCount = fGeoData.count();
91 size_t vertexStride = gp->getVertexStride();
92 SkASSERT(this->hasLocalRect() ?
93 vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr) :
94 vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorAttr));
95 QuadHelper helper;
bsalomon75398562015-08-17 12:55:38 -070096 void* vertices = helper.init(target, vertexStride, instanceCount);
joshualitt9c80b5f2015-08-13 10:05:51 -070097
98 if (!vertices) {
99 return;
100 }
101
102 for (int i = 0; i < instanceCount; i++) {
103 const Geometry& geom = fGeoData[i];
104
105 intptr_t offset = reinterpret_cast<intptr_t>(vertices) +
106 kVerticesPerQuad * i * vertexStride;
107 SkPoint* positions = reinterpret_cast<SkPoint*>(offset);
108
109 positions->setRectFan(geom.fRect.fLeft, geom.fRect.fTop,
110 geom.fRect.fRight, geom.fRect.fBottom, vertexStride);
111 geom.fViewMatrix.mapPointsWithStride(positions, vertexStride, kVerticesPerQuad);
112
113 // TODO we should only do this if local coords are being read
114 if (geom.fHasLocalRect) {
115 static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor);
116 SkPoint* coords = reinterpret_cast<SkPoint*>(offset + kLocalOffset);
117 coords->setRectFan(geom.fLocalRect.fLeft, geom.fLocalRect.fTop,
118 geom.fLocalRect.fRight, geom.fLocalRect.fBottom,
119 vertexStride);
120 if (geom.fHasLocalMatrix) {
121 geom.fLocalMatrix.mapPointsWithStride(coords, vertexStride, kVerticesPerQuad);
122 }
123 }
124
125 static const int kColorOffset = sizeof(SkPoint);
126 GrColor* vertColor = reinterpret_cast<GrColor*>(offset + kColorOffset);
127 for (int j = 0; j < 4; ++j) {
128 *vertColor = geom.fColor;
129 vertColor = (GrColor*) ((intptr_t) vertColor + vertexStride);
130 }
131 }
132
bsalomon75398562015-08-17 12:55:38 -0700133 helper.recordDraw(target);
joshualitt9c80b5f2015-08-13 10:05:51 -0700134 }
135
joshualitt9c80b5f2015-08-13 10:05:51 -0700136 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
bsalomonabd30f52015-08-13 13:34:48 -0700137 BWFillRectBatch* that = t->cast<BWFillRectBatch>();
138 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
139 that->bounds(), caps)) {
joshualitt9c80b5f2015-08-13 10:05:51 -0700140 return false;
141 }
142
joshualitt9c80b5f2015-08-13 10:05:51 -0700143 if (this->hasLocalRect() != that->hasLocalRect()) {
144 return false;
145 }
146
147 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords());
148 if (!this->hasLocalRect() && this->usesLocalCoords()) {
149 if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
150 return false;
151 }
152
153 if (this->hasLocalMatrix() != that->hasLocalMatrix()) {
154 return false;
155 }
156
157 if (this->hasLocalMatrix() && !this->localMatrix().cheapEqualTo(that->localMatrix())) {
158 return false;
159 }
160 }
161
162 if (this->color() != that->color()) {
163 fBatch.fColor = GrColor_ILLEGAL;
164 }
165 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
166 this->joinBounds(that->bounds());
167 return true;
168 }
169
170
171 /** We always use per-vertex colors so that rects can be batched across color changes. Sometimes
172 we have explicit local coords and sometimes not. We *could* always provide explicit local
173 coords and just duplicate the positions when the caller hasn't provided a local coord rect,
174 but we haven't seen a use case which frequently switches between local rect and no local
175 rect draws.
176
177 The color param is used to determine whether the opaque hint can be set on the draw state.
178 The caller must populate the vertex colors itself.
179
180 The vertex attrib order is always pos, color, [local coords].
181 */
182 const GrGeometryProcessor* createRectGP() const {
183 using namespace GrDefaultGeoProcFactory;
184 Color color(Color::kAttribute_Type);
185 Coverage coverage(this->coverageIgnored() ? Coverage::kNone_Type : Coverage::kSolid_Type);
186
187 // if we have a local rect, then we apply the localMatrix directly to the localRect to
188 // generate vertex local coords
189 if (this->hasLocalRect()) {
190 LocalCoords localCoords(LocalCoords::kHasExplicit_Type);
191 return GrDefaultGeoProcFactory::Create(color, coverage, localCoords, SkMatrix::I());
192 } else {
193 LocalCoords localCoords(LocalCoords::kUsePosition_Type,
194 this->hasLocalMatrix() ? &this->localMatrix() : NULL);
195 return GrDefaultGeoProcFactory::CreateForDeviceSpace(color, coverage, localCoords,
196 this->viewMatrix());
197 }
198 }
199
200 struct BatchTracker {
201 GrColor fColor;
202 bool fUsesLocalCoords;
203 bool fColorIgnored;
204 bool fCoverageIgnored;
205 };
206
207 BatchTracker fBatch;
208 SkSTArray<1, Geometry, true> fGeoData;
209};
210
211namespace GrBWFillRectBatch {
bsalomonabd30f52015-08-13 13:34:48 -0700212GrDrawBatch* Create(GrColor color,
213 const SkMatrix& viewMatrix,
214 const SkRect& rect,
215 const SkRect* localRect,
216 const SkMatrix* localMatrix) {
joshualitt9c80b5f2015-08-13 10:05:51 -0700217 BWFillRectBatch::Geometry geometry;
218 geometry.fColor = color;
219 geometry.fViewMatrix = viewMatrix;
220 geometry.fRect = rect;
221
222 if (localRect) {
223 geometry.fHasLocalRect = true;
224 geometry.fLocalRect = *localRect;
225 } else {
226 geometry.fHasLocalRect = false;
227 }
228
229 if (localMatrix) {
230 geometry.fHasLocalMatrix = true;
231 geometry.fLocalMatrix = *localMatrix;
232 } else {
233 geometry.fHasLocalMatrix = false;
234 }
235
236 return BWFillRectBatch::Create(geometry);
237}
238};
239
240///////////////////////////////////////////////////////////////////////////////////////////////////
241
242#ifdef GR_TEST_UTILS
243
244#include "GrBatchTest.h"
245
bsalomonabd30f52015-08-13 13:34:48 -0700246DRAW_BATCH_TEST_DEFINE(RectBatch) {
joshualitt9c80b5f2015-08-13 10:05:51 -0700247 BWFillRectBatch::Geometry geometry;
248 geometry.fColor = GrRandomColor(random);
249
250 geometry.fRect = GrTest::TestRect(random);
251 geometry.fHasLocalRect = random->nextBool();
252
253 if (geometry.fHasLocalRect) {
254 geometry.fViewMatrix = GrTest::TestMatrixInvertible(random);
255 geometry.fLocalRect = GrTest::TestRect(random);
256 } else {
257 geometry.fViewMatrix = GrTest::TestMatrix(random);
258 }
259
260 geometry.fHasLocalMatrix = random->nextBool();
261 if (geometry.fHasLocalMatrix) {
262 geometry.fLocalMatrix = GrTest::TestMatrix(random);
263 }
264
265 return BWFillRectBatch::Create(geometry);
266}
267
268#endif