blob: 6eabba68728b42990fed73f79e355ec0c82b7a50 [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) &&
160 (!mine.fHasLocalMatrix || mine.fLocalMatrix.cheapEqualTo(theirs.fLocalMatrix));
joshualitt2244c272015-08-21 10:33:15 -0700161 }
162
163 static const GrGeometryProcessor* CreateGP(const Geometry& geo,
164 const GrPipelineOptimizations& opts) {
joshualitt8cce8f12015-08-26 06:23:39 -0700165 const GrGeometryProcessor* gp = create_gp(geo.fViewMatrix, opts.readsCoverage(),
166 geo.fHasLocalRect,
167 geo.fHasLocalMatrix ? &geo.fLocalMatrix :
168 nullptr);
joshualitt2244c272015-08-21 10:33:15 -0700169
joshualitt8cce8f12015-08-26 06:23:39 -0700170 SkASSERT(geo.fHasLocalRect ?
171 gp->getVertexStride() == sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr) :
172 gp->getVertexStride() == sizeof(GrDefaultGeoProcFactory::PositionColorAttr));
joshualitt2244c272015-08-21 10:33:15 -0700173 return gp;
174 }
175
176 static void Tesselate(intptr_t vertices, size_t vertexStride, const Geometry& geo,
177 const GrPipelineOptimizations& opts) {
joshualitt8cce8f12015-08-26 06:23:39 -0700178 if (geo.fHasLocalRect) {
179 GrQuad quad(geo.fLocalRect);
180 tesselate(vertices, vertexStride, geo.fColor, geo.fViewMatrix, geo.fRect, &quad);
181 } else {
182 tesselate(vertices, vertexStride, geo.fColor, geo.fViewMatrix, geo.fRect, nullptr);
183 }
joshualitt2244c272015-08-21 10:33:15 -0700184 }
joshualittae41b382015-08-19 06:54:08 -0700185};
186
joshualittbcf33d52015-08-26 08:10:35 -0700187typedef GrTInstanceBatch<NonAAFillRectBatchImp> NonAAFillRectBatchSimple;
188typedef GrTInstanceBatch<NonAAFillRectBatchPerspectiveImp> NonAAFillRectBatchPerspective;
joshualitt2244c272015-08-21 10:33:15 -0700189
joshualittbcf33d52015-08-26 08:10:35 -0700190namespace GrNonAAFillRectBatch {
bsalomonabd30f52015-08-13 13:34:48 -0700191GrDrawBatch* Create(GrColor color,
192 const SkMatrix& viewMatrix,
193 const SkRect& rect,
194 const SkRect* localRect,
195 const SkMatrix* localMatrix) {
joshualitt8cce8f12015-08-26 06:23:39 -0700196
197 /* Perspective has to be handled in a slow path for now */
198 if (viewMatrix.hasPerspective() || (localMatrix && localMatrix->hasPerspective())) {
joshualittbcf33d52015-08-26 08:10:35 -0700199 NonAAFillRectBatchPerspective* batch = NonAAFillRectBatchPerspective::Create();
200 NonAAFillRectBatchPerspective::Geometry& geo = *batch->geometry();
joshualitt8cce8f12015-08-26 06:23:39 -0700201
joshualitt2244c272015-08-21 10:33:15 -0700202 geo.fColor = color;
203 geo.fViewMatrix = viewMatrix;
204 geo.fRect = rect;
joshualitt8cce8f12015-08-26 06:23:39 -0700205 geo.fHasLocalRect = SkToBool(localRect);
206 geo.fHasLocalMatrix = SkToBool(localMatrix);
207 if (localMatrix) {
208 geo.fLocalMatrix = *localMatrix;
209 }
210 if (localRect) {
211 geo.fLocalRect = *localRect;
212 }
213
joshualitt2244c272015-08-21 10:33:15 -0700214 batch->init();
215 return batch;
joshualitt9c80b5f2015-08-13 10:05:51 -0700216 } else {
joshualitt8cce8f12015-08-26 06:23:39 -0700217 // TODO bubble these up as separate calls
joshualittbcf33d52015-08-26 08:10:35 -0700218 NonAAFillRectBatchSimple* batch = NonAAFillRectBatchSimple::Create();
219 NonAAFillRectBatchSimple::Geometry& geo = *batch->geometry();
joshualitt8cce8f12015-08-26 06:23:39 -0700220
joshualitt2244c272015-08-21 10:33:15 -0700221 geo.fColor = color;
222 geo.fViewMatrix = viewMatrix;
223 geo.fRect = rect;
joshualitt8cce8f12015-08-26 06:23:39 -0700224
225 if (localRect && localMatrix) {
226 geo.fLocalQuad.setFromMappedRect(*localRect, *localMatrix);
227 } else if (localRect) {
228 geo.fLocalQuad.set(*localRect);
229 } else if (localMatrix) {
230 geo.fLocalQuad.setFromMappedRect(rect, *localMatrix);
231 } else {
232 geo.fLocalQuad.set(rect);
233 }
234
joshualitt2244c272015-08-21 10:33:15 -0700235 batch->init();
236 return batch;
joshualitt9c80b5f2015-08-13 10:05:51 -0700237 }
joshualitt9c80b5f2015-08-13 10:05:51 -0700238}
239};
240
241///////////////////////////////////////////////////////////////////////////////////////////////////
242
243#ifdef GR_TEST_UTILS
244
245#include "GrBatchTest.h"
246
bsalomonabd30f52015-08-13 13:34:48 -0700247DRAW_BATCH_TEST_DEFINE(RectBatch) {
joshualitt2244c272015-08-21 10:33:15 -0700248 GrColor color = GrRandomColor(random);
249 SkRect rect = GrTest::TestRect(random);
250 SkRect localRect = GrTest::TestRect(random);
251 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
252 SkMatrix localMatrix = GrTest::TestMatrix(random);
joshualitt9c80b5f2015-08-13 10:05:51 -0700253
joshualitt2244c272015-08-21 10:33:15 -0700254 bool hasLocalRect = random->nextBool();
255 bool hasLocalMatrix = random->nextBool();
joshualittbcf33d52015-08-26 08:10:35 -0700256 return GrNonAAFillRectBatch::Create(color, viewMatrix, rect,
257 hasLocalRect ? &localRect : nullptr,
258 hasLocalMatrix ? &localMatrix : nullptr);
joshualitt9c80b5f2015-08-13 10:05:51 -0700259}
260
261#endif