blob: 765d0dafa89885dae2527eeb0e2bd4709ea7252c [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"
15#include "GrTInstanceBatch.h"
joshualittae5b2c62015-08-19 08:48:41 -070016#include "GrQuad.h"
bsalomon16b99132015-08-13 14:55:50 -070017#include "GrVertexBatch.h"
joshualitt9c80b5f2015-08-13 10:05:51 -070018
joshualitt2244c272015-08-21 10:33:15 -070019// Common functions
joshualittbcf33d52015-08-26 08:10:35 -070020class NonAAFillRectBatchBase {
joshualitt2244c272015-08-21 10:33:15 -070021public:
22 static const int kVertsPerInstance = 4;
23 static const int kIndicesPerInstance = 6;
joshualitt87e47522015-08-20 07:22:42 -070024
joshualitt2244c272015-08-21 10:33:15 -070025 static void InitInvariantOutputCoverage(GrInitInvariantOutput* out) {
26 out->setKnownSingleComponent(0xff);
27 }
28
29 static const GrIndexBuffer* GetIndexBuffer(GrResourceProvider* rp) {
30 return rp->refQuadIndexBuffer();
31 }
32
33 template <typename Geometry>
34 static void SetBounds(const Geometry& geo, SkRect* outBounds) {
35 geo.fViewMatrix.mapRect(outBounds, geo.fRect);
36 }
37};
38
39/** We always use per-vertex colors so that rects can be batched across color changes. Sometimes
40 we have explicit local coords and sometimes not. We *could* always provide explicit local
41 coords and just duplicate the positions when the caller hasn't provided a local coord rect,
42 but we haven't seen a use case which frequently switches between local rect and no local
43 rect draws.
44
45 The vertex attrib order is always pos, color, [local coords].
46 */
47static const GrGeometryProcessor* create_gp(const SkMatrix& viewMatrix,
48 bool readsCoverage,
49 bool hasExplicitLocalCoords,
50 const SkMatrix* localMatrix) {
51 using namespace GrDefaultGeoProcFactory;
52 Color color(Color::kAttribute_Type);
53 Coverage coverage(readsCoverage ? Coverage::kSolid_Type : Coverage::kNone_Type);
54
joshualitt8cce8f12015-08-26 06:23:39 -070055 // If we have perspective on the viewMatrix then we won't map on the CPU, nor will we map
56 // the local rect on the cpu (in case the localMatrix also has perspective).
57 // Otherwise, if we have a local rect, then we apply the localMatrix directly to the localRect
58 // to generate vertex local coords
59 if (viewMatrix.hasPerspective()) {
60 LocalCoords localCoords(hasExplicitLocalCoords ? LocalCoords::kHasExplicit_Type :
61 LocalCoords::kUsePosition_Type,
62 localMatrix);
63 return GrDefaultGeoProcFactory::Create(color, coverage, localCoords, viewMatrix);
64 } else if (hasExplicitLocalCoords) {
joshualitt2244c272015-08-21 10:33:15 -070065 LocalCoords localCoords(LocalCoords::kHasExplicit_Type);
66 return GrDefaultGeoProcFactory::Create(color, coverage, localCoords, SkMatrix::I());
67 } else {
68 LocalCoords localCoords(LocalCoords::kUsePosition_Type, localMatrix);
69 return GrDefaultGeoProcFactory::CreateForDeviceSpace(color, coverage, localCoords,
70 viewMatrix);
71 }
72}
73
74static void tesselate(intptr_t vertices,
75 size_t vertexStride,
76 GrColor color,
77 const SkMatrix& viewMatrix,
78 const SkRect& rect,
joshualitt8cce8f12015-08-26 06:23:39 -070079 const GrQuad* localQuad) {
joshualitt2244c272015-08-21 10:33:15 -070080 SkPoint* positions = reinterpret_cast<SkPoint*>(vertices);
81
82 positions->setRectFan(rect.fLeft, rect.fTop,
83 rect.fRight, rect.fBottom, vertexStride);
joshualitt2244c272015-08-21 10:33:15 -070084
joshualitt8cce8f12015-08-26 06:23:39 -070085 if (!viewMatrix.hasPerspective()) {
86 viewMatrix.mapPointsWithStride(positions, vertexStride,
joshualittbcf33d52015-08-26 08:10:35 -070087 NonAAFillRectBatchBase::kVertsPerInstance);
joshualitt8cce8f12015-08-26 06:23:39 -070088 }
89
90 // Setup local coords
joshualitt2244c272015-08-21 10:33:15 -070091 // TODO we should only do this if local coords are being read
joshualitt8cce8f12015-08-26 06:23:39 -070092 if (localQuad) {
joshualitt2244c272015-08-21 10:33:15 -070093 static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor);
joshualittbcf33d52015-08-26 08:10:35 -070094 for (int i = 0; i < NonAAFillRectBatchBase::kVertsPerInstance; i++) {
joshualitt8cce8f12015-08-26 06:23:39 -070095 SkPoint* coords = reinterpret_cast<SkPoint*>(vertices + kLocalOffset +
96 i * vertexStride);
97 *coords = localQuad->point(i);
joshualitt2244c272015-08-21 10:33:15 -070098 }
99 }
100
101 static const int kColorOffset = sizeof(SkPoint);
102 GrColor* vertColor = reinterpret_cast<GrColor*>(vertices + kColorOffset);
103 for (int j = 0; j < 4; ++j) {
104 *vertColor = color;
105 vertColor = (GrColor*) ((intptr_t) vertColor + vertexStride);
106 }
107}
108
joshualittbcf33d52015-08-26 08:10:35 -0700109class NonAAFillRectBatchImp : public NonAAFillRectBatchBase {
joshualitt2244c272015-08-21 10:33:15 -0700110public:
111 struct Geometry {
112 SkMatrix fViewMatrix;
113 SkRect fRect;
joshualitt8cce8f12015-08-26 06:23:39 -0700114 GrQuad fLocalQuad;
joshualitt2244c272015-08-21 10:33:15 -0700115 GrColor fColor;
116 };
117
joshualittbcf33d52015-08-26 08:10:35 -0700118 static const char* Name() { return "NonAAFillRectBatch"; }
joshualitt2244c272015-08-21 10:33:15 -0700119
120 static bool CanCombine(const Geometry& mine, const Geometry& theirs,
121 const GrPipelineOptimizations& opts) {
joshualitt87e47522015-08-20 07:22:42 -0700122 return true;
123 }
124
joshualitt2244c272015-08-21 10:33:15 -0700125 static const GrGeometryProcessor* CreateGP(const Geometry& geo,
126 const GrPipelineOptimizations& opts) {
127 const GrGeometryProcessor* gp = create_gp(geo.fViewMatrix, opts.readsCoverage(), true,
joshualitt8cce8f12015-08-26 06:23:39 -0700128 nullptr);
joshualitt87e47522015-08-20 07:22:42 -0700129
joshualitt2244c272015-08-21 10:33:15 -0700130 SkASSERT(gp->getVertexStride() ==
131 sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr));
132 return gp;
joshualittae41b382015-08-19 06:54:08 -0700133 }
134
joshualitt2244c272015-08-21 10:33:15 -0700135 static void Tesselate(intptr_t vertices, size_t vertexStride, const Geometry& geo,
136 const GrPipelineOptimizations& opts) {
joshualitt8cce8f12015-08-26 06:23:39 -0700137 tesselate(vertices, vertexStride, geo.fColor, geo.fViewMatrix, geo.fRect, &geo.fLocalQuad);
joshualitt2244c272015-08-21 10:33:15 -0700138 }
139};
140
joshualitt8cce8f12015-08-26 06:23:39 -0700141// We handle perspective in the local matrix or viewmatrix with special batches
joshualittbcf33d52015-08-26 08:10:35 -0700142class NonAAFillRectBatchPerspectiveImp : public NonAAFillRectBatchBase {
joshualitt2244c272015-08-21 10:33:15 -0700143public:
144 struct Geometry {
145 SkMatrix fViewMatrix;
146 SkMatrix fLocalMatrix;
147 SkRect fRect;
148 SkRect fLocalRect;
joshualittae41b382015-08-19 06:54:08 -0700149 GrColor fColor;
joshualitt8cce8f12015-08-26 06:23:39 -0700150 bool fHasLocalMatrix;
151 bool fHasLocalRect;
joshualittae41b382015-08-19 06:54:08 -0700152 };
153
joshualittbcf33d52015-08-26 08:10:35 -0700154 static const char* Name() { return "NonAAFillRectBatchPerspective"; }
joshualitt2244c272015-08-21 10:33:15 -0700155
156 static bool CanCombine(const Geometry& mine, const Geometry& theirs,
157 const GrPipelineOptimizations& opts) {
joshualitt8cce8f12015-08-26 06:23:39 -0700158 // We could batch across perspective vm changes if we really wanted to
159 return mine.fViewMatrix.cheapEqualTo(theirs.fViewMatrix) &&
joshualitt2120b6f2015-09-18 12:56:41 -0700160 mine.fHasLocalRect == theirs.fHasLocalRect &&
joshualitt8cce8f12015-08-26 06:23:39 -0700161 (!mine.fHasLocalMatrix || mine.fLocalMatrix.cheapEqualTo(theirs.fLocalMatrix));
joshualitt2244c272015-08-21 10:33:15 -0700162 }
163
164 static const GrGeometryProcessor* CreateGP(const Geometry& geo,
165 const GrPipelineOptimizations& opts) {
joshualitt8cce8f12015-08-26 06:23:39 -0700166 const GrGeometryProcessor* gp = create_gp(geo.fViewMatrix, opts.readsCoverage(),
167 geo.fHasLocalRect,
168 geo.fHasLocalMatrix ? &geo.fLocalMatrix :
169 nullptr);
joshualitt2244c272015-08-21 10:33:15 -0700170
joshualitt8cce8f12015-08-26 06:23:39 -0700171 SkASSERT(geo.fHasLocalRect ?
172 gp->getVertexStride() == sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr) :
173 gp->getVertexStride() == sizeof(GrDefaultGeoProcFactory::PositionColorAttr));
joshualitt2244c272015-08-21 10:33:15 -0700174 return gp;
175 }
176
177 static void Tesselate(intptr_t vertices, size_t vertexStride, const Geometry& geo,
178 const GrPipelineOptimizations& opts) {
joshualitt8cce8f12015-08-26 06:23:39 -0700179 if (geo.fHasLocalRect) {
180 GrQuad quad(geo.fLocalRect);
181 tesselate(vertices, vertexStride, geo.fColor, geo.fViewMatrix, geo.fRect, &quad);
182 } else {
183 tesselate(vertices, vertexStride, geo.fColor, geo.fViewMatrix, geo.fRect, nullptr);
184 }
joshualitt2244c272015-08-21 10:33:15 -0700185 }
joshualittae41b382015-08-19 06:54:08 -0700186};
187
joshualittbcf33d52015-08-26 08:10:35 -0700188typedef GrTInstanceBatch<NonAAFillRectBatchImp> NonAAFillRectBatchSimple;
189typedef GrTInstanceBatch<NonAAFillRectBatchPerspectiveImp> NonAAFillRectBatchPerspective;
joshualitt2244c272015-08-21 10:33:15 -0700190
joshualittbcf33d52015-08-26 08:10:35 -0700191namespace GrNonAAFillRectBatch {
joshualitt3566d442015-09-18 07:12:55 -0700192
bsalomonabd30f52015-08-13 13:34:48 -0700193GrDrawBatch* Create(GrColor color,
194 const SkMatrix& viewMatrix,
195 const SkRect& rect,
196 const SkRect* localRect,
197 const SkMatrix* localMatrix) {
joshualitt3566d442015-09-18 07:12:55 -0700198 SkASSERT(!viewMatrix.hasPerspective() && (!localMatrix || !localMatrix->hasPerspective()));
199 NonAAFillRectBatchSimple* batch = NonAAFillRectBatchSimple::Create();
200 NonAAFillRectBatchSimple::Geometry& geo = *batch->geometry();
joshualitt8cce8f12015-08-26 06:23:39 -0700201
joshualitt3566d442015-09-18 07:12:55 -0700202 geo.fColor = color;
203 geo.fViewMatrix = viewMatrix;
204 geo.fRect = rect;
joshualitt8cce8f12015-08-26 06:23:39 -0700205
joshualitt3566d442015-09-18 07:12:55 -0700206 if (localRect && localMatrix) {
207 geo.fLocalQuad.setFromMappedRect(*localRect, *localMatrix);
208 } else if (localRect) {
209 geo.fLocalQuad.set(*localRect);
210 } else if (localMatrix) {
211 geo.fLocalQuad.setFromMappedRect(rect, *localMatrix);
joshualitt9c80b5f2015-08-13 10:05:51 -0700212 } else {
joshualitt3566d442015-09-18 07:12:55 -0700213 geo.fLocalQuad.set(rect);
joshualitt9c80b5f2015-08-13 10:05:51 -0700214 }
joshualitt3566d442015-09-18 07:12:55 -0700215
216 batch->init();
217 return batch;
joshualitt9c80b5f2015-08-13 10:05:51 -0700218}
joshualitt3566d442015-09-18 07:12:55 -0700219
220GrDrawBatch* CreateWithPerspective(GrColor color,
221 const SkMatrix& viewMatrix,
222 const SkRect& rect,
223 const SkRect* localRect,
224 const SkMatrix* localMatrix) {
225 SkASSERT(viewMatrix.hasPerspective() || (localMatrix && localMatrix->hasPerspective()));
226 NonAAFillRectBatchPerspective* batch = NonAAFillRectBatchPerspective::Create();
227 NonAAFillRectBatchPerspective::Geometry& geo = *batch->geometry();
228
229 geo.fColor = color;
230 geo.fViewMatrix = viewMatrix;
231 geo.fRect = rect;
232 geo.fHasLocalRect = SkToBool(localRect);
233 geo.fHasLocalMatrix = SkToBool(localMatrix);
234 if (localMatrix) {
235 geo.fLocalMatrix = *localMatrix;
236 }
237 if (localRect) {
238 geo.fLocalRect = *localRect;
239 }
240
241 batch->init();
242 return batch;
243}
244
joshualitt9c80b5f2015-08-13 10:05:51 -0700245};
246
247///////////////////////////////////////////////////////////////////////////////////////////////////
248
249#ifdef GR_TEST_UTILS
250
251#include "GrBatchTest.h"
252
bsalomonabd30f52015-08-13 13:34:48 -0700253DRAW_BATCH_TEST_DEFINE(RectBatch) {
joshualitt2244c272015-08-21 10:33:15 -0700254 GrColor color = GrRandomColor(random);
255 SkRect rect = GrTest::TestRect(random);
256 SkRect localRect = GrTest::TestRect(random);
257 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
258 SkMatrix localMatrix = GrTest::TestMatrix(random);
joshualitt9c80b5f2015-08-13 10:05:51 -0700259
joshualitt2244c272015-08-21 10:33:15 -0700260 bool hasLocalRect = random->nextBool();
261 bool hasLocalMatrix = random->nextBool();
joshualittbcf33d52015-08-26 08:10:35 -0700262 return GrNonAAFillRectBatch::Create(color, viewMatrix, rect,
263 hasLocalRect ? &localRect : nullptr,
264 hasLocalMatrix ? &localMatrix : nullptr);
joshualitt9c80b5f2015-08-13 10:05:51 -0700265}
266
267#endif