blob: 3ad869bc9ac67c872856a916301be6aca09c9e32 [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 }
joshualittaa37a962015-09-18 13:03:25 -070037
38 template <typename Geometry>
39 static void UpdateBoundsAfterAppend(const Geometry& geo, SkRect* outBounds) {
40 SkRect bounds = geo.fRect;
41 geo.fViewMatrix.mapRect(&bounds);
42 outBounds->join(bounds);
43 }
joshualitt2244c272015-08-21 10:33:15 -070044};
45
46/** We always use per-vertex colors so that rects can be batched across color changes. Sometimes
47 we have explicit local coords and sometimes not. We *could* always provide explicit local
48 coords and just duplicate the positions when the caller hasn't provided a local coord rect,
49 but we haven't seen a use case which frequently switches between local rect and no local
50 rect draws.
51
52 The vertex attrib order is always pos, color, [local coords].
53 */
54static const GrGeometryProcessor* create_gp(const SkMatrix& viewMatrix,
55 bool readsCoverage,
56 bool hasExplicitLocalCoords,
57 const SkMatrix* localMatrix) {
58 using namespace GrDefaultGeoProcFactory;
59 Color color(Color::kAttribute_Type);
60 Coverage coverage(readsCoverage ? Coverage::kSolid_Type : Coverage::kNone_Type);
61
joshualitt8cce8f12015-08-26 06:23:39 -070062 // If we have perspective on the viewMatrix then we won't map on the CPU, nor will we map
63 // the local rect on the cpu (in case the localMatrix also has perspective).
64 // Otherwise, if we have a local rect, then we apply the localMatrix directly to the localRect
65 // to generate vertex local coords
66 if (viewMatrix.hasPerspective()) {
67 LocalCoords localCoords(hasExplicitLocalCoords ? LocalCoords::kHasExplicit_Type :
68 LocalCoords::kUsePosition_Type,
69 localMatrix);
70 return GrDefaultGeoProcFactory::Create(color, coverage, localCoords, viewMatrix);
71 } else if (hasExplicitLocalCoords) {
joshualitt2244c272015-08-21 10:33:15 -070072 LocalCoords localCoords(LocalCoords::kHasExplicit_Type);
73 return GrDefaultGeoProcFactory::Create(color, coverage, localCoords, SkMatrix::I());
74 } else {
75 LocalCoords localCoords(LocalCoords::kUsePosition_Type, localMatrix);
76 return GrDefaultGeoProcFactory::CreateForDeviceSpace(color, coverage, localCoords,
77 viewMatrix);
78 }
79}
80
81static void tesselate(intptr_t vertices,
82 size_t vertexStride,
83 GrColor color,
84 const SkMatrix& viewMatrix,
85 const SkRect& rect,
joshualitt8cce8f12015-08-26 06:23:39 -070086 const GrQuad* localQuad) {
joshualitt2244c272015-08-21 10:33:15 -070087 SkPoint* positions = reinterpret_cast<SkPoint*>(vertices);
88
89 positions->setRectFan(rect.fLeft, rect.fTop,
90 rect.fRight, rect.fBottom, vertexStride);
joshualitt2244c272015-08-21 10:33:15 -070091
joshualitt8cce8f12015-08-26 06:23:39 -070092 if (!viewMatrix.hasPerspective()) {
93 viewMatrix.mapPointsWithStride(positions, vertexStride,
joshualittbcf33d52015-08-26 08:10:35 -070094 NonAAFillRectBatchBase::kVertsPerInstance);
joshualitt8cce8f12015-08-26 06:23:39 -070095 }
96
97 // Setup local coords
joshualitt2244c272015-08-21 10:33:15 -070098 // TODO we should only do this if local coords are being read
joshualitt8cce8f12015-08-26 06:23:39 -070099 if (localQuad) {
joshualitt2244c272015-08-21 10:33:15 -0700100 static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor);
joshualittbcf33d52015-08-26 08:10:35 -0700101 for (int i = 0; i < NonAAFillRectBatchBase::kVertsPerInstance; i++) {
joshualitt8cce8f12015-08-26 06:23:39 -0700102 SkPoint* coords = reinterpret_cast<SkPoint*>(vertices + kLocalOffset +
103 i * vertexStride);
104 *coords = localQuad->point(i);
joshualitt2244c272015-08-21 10:33:15 -0700105 }
106 }
107
108 static const int kColorOffset = sizeof(SkPoint);
109 GrColor* vertColor = reinterpret_cast<GrColor*>(vertices + kColorOffset);
110 for (int j = 0; j < 4; ++j) {
111 *vertColor = color;
112 vertColor = (GrColor*) ((intptr_t) vertColor + vertexStride);
113 }
114}
115
joshualittbcf33d52015-08-26 08:10:35 -0700116class NonAAFillRectBatchImp : public NonAAFillRectBatchBase {
joshualitt2244c272015-08-21 10:33:15 -0700117public:
118 struct Geometry {
119 SkMatrix fViewMatrix;
120 SkRect fRect;
joshualitt8cce8f12015-08-26 06:23:39 -0700121 GrQuad fLocalQuad;
joshualitt2244c272015-08-21 10:33:15 -0700122 GrColor fColor;
123 };
124
joshualittbcf33d52015-08-26 08:10:35 -0700125 static const char* Name() { return "NonAAFillRectBatch"; }
joshualitt2244c272015-08-21 10:33:15 -0700126
robertphillipse004bfc2015-11-16 09:06:59 -0800127 static SkString DumpInfo(const Geometry& geo) {
128 SkString str;
129 str.appendf("Color: 0x%08x, Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n",
130 geo.fColor,
131 geo.fRect.fLeft, geo.fRect.fTop, geo.fRect.fRight, geo.fRect.fBottom);
132 return str;
133 }
134
joshualitt2244c272015-08-21 10:33:15 -0700135 static bool CanCombine(const Geometry& mine, const Geometry& theirs,
136 const GrPipelineOptimizations& opts) {
joshualitt87e47522015-08-20 07:22:42 -0700137 return true;
138 }
139
joshualitt2244c272015-08-21 10:33:15 -0700140 static const GrGeometryProcessor* CreateGP(const Geometry& geo,
141 const GrPipelineOptimizations& opts) {
142 const GrGeometryProcessor* gp = create_gp(geo.fViewMatrix, opts.readsCoverage(), true,
joshualitt8cce8f12015-08-26 06:23:39 -0700143 nullptr);
joshualitt87e47522015-08-20 07:22:42 -0700144
joshualitt2244c272015-08-21 10:33:15 -0700145 SkASSERT(gp->getVertexStride() ==
146 sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr));
147 return gp;
joshualittae41b382015-08-19 06:54:08 -0700148 }
149
joshualitt2244c272015-08-21 10:33:15 -0700150 static void Tesselate(intptr_t vertices, size_t vertexStride, const Geometry& geo,
151 const GrPipelineOptimizations& opts) {
joshualitt8cce8f12015-08-26 06:23:39 -0700152 tesselate(vertices, vertexStride, geo.fColor, geo.fViewMatrix, geo.fRect, &geo.fLocalQuad);
joshualitt2244c272015-08-21 10:33:15 -0700153 }
154};
155
joshualitt8cce8f12015-08-26 06:23:39 -0700156// We handle perspective in the local matrix or viewmatrix with special batches
joshualittbcf33d52015-08-26 08:10:35 -0700157class NonAAFillRectBatchPerspectiveImp : public NonAAFillRectBatchBase {
joshualitt2244c272015-08-21 10:33:15 -0700158public:
159 struct Geometry {
160 SkMatrix fViewMatrix;
161 SkMatrix fLocalMatrix;
162 SkRect fRect;
163 SkRect fLocalRect;
joshualittae41b382015-08-19 06:54:08 -0700164 GrColor fColor;
joshualitt8cce8f12015-08-26 06:23:39 -0700165 bool fHasLocalMatrix;
166 bool fHasLocalRect;
joshualittae41b382015-08-19 06:54:08 -0700167 };
168
joshualittbcf33d52015-08-26 08:10:35 -0700169 static const char* Name() { return "NonAAFillRectBatchPerspective"; }
joshualitt2244c272015-08-21 10:33:15 -0700170
robertphillipse004bfc2015-11-16 09:06:59 -0800171 static SkString DumpInfo(const Geometry& geo) {
172 SkString str;
173 str.appendf("Color: 0x%08x, Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n",
174 geo.fColor,
175 geo.fRect.fLeft, geo.fRect.fTop, geo.fRect.fRight, geo.fRect.fBottom);
176 return str;
177 }
178
joshualitt2244c272015-08-21 10:33:15 -0700179 static bool CanCombine(const Geometry& mine, const Geometry& theirs,
180 const GrPipelineOptimizations& opts) {
joshualitt8cce8f12015-08-26 06:23:39 -0700181 // We could batch across perspective vm changes if we really wanted to
182 return mine.fViewMatrix.cheapEqualTo(theirs.fViewMatrix) &&
joshualitt2120b6f2015-09-18 12:56:41 -0700183 mine.fHasLocalRect == theirs.fHasLocalRect &&
joshualitt8cce8f12015-08-26 06:23:39 -0700184 (!mine.fHasLocalMatrix || mine.fLocalMatrix.cheapEqualTo(theirs.fLocalMatrix));
joshualitt2244c272015-08-21 10:33:15 -0700185 }
186
187 static const GrGeometryProcessor* CreateGP(const Geometry& geo,
188 const GrPipelineOptimizations& opts) {
joshualitt8cce8f12015-08-26 06:23:39 -0700189 const GrGeometryProcessor* gp = create_gp(geo.fViewMatrix, opts.readsCoverage(),
190 geo.fHasLocalRect,
191 geo.fHasLocalMatrix ? &geo.fLocalMatrix :
192 nullptr);
joshualitt2244c272015-08-21 10:33:15 -0700193
joshualitt8cce8f12015-08-26 06:23:39 -0700194 SkASSERT(geo.fHasLocalRect ?
195 gp->getVertexStride() == sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr) :
196 gp->getVertexStride() == sizeof(GrDefaultGeoProcFactory::PositionColorAttr));
joshualitt2244c272015-08-21 10:33:15 -0700197 return gp;
198 }
199
200 static void Tesselate(intptr_t vertices, size_t vertexStride, const Geometry& geo,
201 const GrPipelineOptimizations& opts) {
joshualitt8cce8f12015-08-26 06:23:39 -0700202 if (geo.fHasLocalRect) {
203 GrQuad quad(geo.fLocalRect);
204 tesselate(vertices, vertexStride, geo.fColor, geo.fViewMatrix, geo.fRect, &quad);
205 } else {
206 tesselate(vertices, vertexStride, geo.fColor, geo.fViewMatrix, geo.fRect, nullptr);
207 }
joshualitt2244c272015-08-21 10:33:15 -0700208 }
joshualittae41b382015-08-19 06:54:08 -0700209};
210
joshualittbcf33d52015-08-26 08:10:35 -0700211typedef GrTInstanceBatch<NonAAFillRectBatchImp> NonAAFillRectBatchSimple;
212typedef GrTInstanceBatch<NonAAFillRectBatchPerspectiveImp> NonAAFillRectBatchPerspective;
joshualitt2244c272015-08-21 10:33:15 -0700213
joshualittaa37a962015-09-18 13:03:25 -0700214inline static void append_to_batch(NonAAFillRectBatchSimple* batch, GrColor color,
215 const SkMatrix& viewMatrix, const SkRect& rect,
216 const SkRect* localRect, const SkMatrix* localMatrix) {
joshualitt3566d442015-09-18 07:12:55 -0700217 SkASSERT(!viewMatrix.hasPerspective() && (!localMatrix || !localMatrix->hasPerspective()));
joshualittaa37a962015-09-18 13:03:25 -0700218 NonAAFillRectBatchSimple::Geometry& geo = batch->geoData()->push_back();
joshualitt8cce8f12015-08-26 06:23:39 -0700219
joshualitt3566d442015-09-18 07:12:55 -0700220 geo.fColor = color;
221 geo.fViewMatrix = viewMatrix;
222 geo.fRect = rect;
joshualitt8cce8f12015-08-26 06:23:39 -0700223
joshualitt3566d442015-09-18 07:12:55 -0700224 if (localRect && localMatrix) {
225 geo.fLocalQuad.setFromMappedRect(*localRect, *localMatrix);
226 } else if (localRect) {
227 geo.fLocalQuad.set(*localRect);
228 } else if (localMatrix) {
229 geo.fLocalQuad.setFromMappedRect(rect, *localMatrix);
joshualitt9c80b5f2015-08-13 10:05:51 -0700230 } else {
joshualitt3566d442015-09-18 07:12:55 -0700231 geo.fLocalQuad.set(rect);
joshualitt9c80b5f2015-08-13 10:05:51 -0700232 }
joshualitt9c80b5f2015-08-13 10:05:51 -0700233}
joshualitt3566d442015-09-18 07:12:55 -0700234
joshualittaa37a962015-09-18 13:03:25 -0700235inline static void append_to_batch(NonAAFillRectBatchPerspective* batch, GrColor color,
236 const SkMatrix& viewMatrix, const SkRect& rect,
237 const SkRect* localRect, const SkMatrix* localMatrix) {
joshualitt3566d442015-09-18 07:12:55 -0700238 SkASSERT(viewMatrix.hasPerspective() || (localMatrix && localMatrix->hasPerspective()));
joshualittaa37a962015-09-18 13:03:25 -0700239 NonAAFillRectBatchPerspective::Geometry& geo = batch->geoData()->push_back();
joshualitt3566d442015-09-18 07:12:55 -0700240
241 geo.fColor = color;
242 geo.fViewMatrix = viewMatrix;
243 geo.fRect = rect;
244 geo.fHasLocalRect = SkToBool(localRect);
245 geo.fHasLocalMatrix = SkToBool(localMatrix);
246 if (localMatrix) {
247 geo.fLocalMatrix = *localMatrix;
248 }
249 if (localRect) {
250 geo.fLocalRect = *localRect;
251 }
252
joshualittaa37a962015-09-18 13:03:25 -0700253}
254
255namespace GrNonAAFillRectBatch {
256
257GrDrawBatch* Create(GrColor color,
258 const SkMatrix& viewMatrix,
259 const SkRect& rect,
260 const SkRect* localRect,
261 const SkMatrix* localMatrix) {
262 NonAAFillRectBatchSimple* batch = NonAAFillRectBatchSimple::Create();
263 append_to_batch(batch, color, viewMatrix, rect, localRect, localMatrix);
joshualitt3566d442015-09-18 07:12:55 -0700264 batch->init();
265 return batch;
266}
267
joshualittaa37a962015-09-18 13:03:25 -0700268GrDrawBatch* CreateWithPerspective(GrColor color,
269 const SkMatrix& viewMatrix,
270 const SkRect& rect,
271 const SkRect* localRect,
272 const SkMatrix* localMatrix) {
273 NonAAFillRectBatchPerspective* batch = NonAAFillRectBatchPerspective::Create();
274 append_to_batch(batch, color, viewMatrix, rect, localRect, localMatrix);
275 batch->init();
276 return batch;
277}
278
279bool Append(GrBatch* origBatch,
280 GrColor color,
281 const SkMatrix& viewMatrix,
282 const SkRect& rect,
283 const SkRect* localRect,
284 const SkMatrix* localMatrix) {
285 bool usePerspective = viewMatrix.hasPerspective() ||
286 (localMatrix && localMatrix->hasPerspective());
287
288 if (usePerspective && origBatch->classID() != NonAAFillRectBatchPerspective::ClassID()) {
289 return false;
290 }
291
292 if (!usePerspective) {
293 NonAAFillRectBatchSimple* batch = origBatch->cast<NonAAFillRectBatchSimple>();
294 append_to_batch(batch, color, viewMatrix, rect, localRect, localMatrix);
295 batch->updateBoundsAfterAppend();
296 } else {
297 NonAAFillRectBatchPerspective* batch = origBatch->cast<NonAAFillRectBatchPerspective>();
298 const NonAAFillRectBatchPerspective::Geometry& geo = batch->geoData()->back();
299
300 if (!geo.fViewMatrix.cheapEqualTo(viewMatrix) ||
301 geo.fHasLocalRect != SkToBool(localRect) ||
302 geo.fHasLocalMatrix != SkToBool(localMatrix) ||
303 (geo.fHasLocalMatrix && !geo.fLocalMatrix.cheapEqualTo(*localMatrix))) {
304 return false;
305 }
306
307 append_to_batch(batch, color, viewMatrix, rect, localRect, localMatrix);
308 batch->updateBoundsAfterAppend();
309 }
310
311 return true;
312}
313
joshualitt9c80b5f2015-08-13 10:05:51 -0700314};
315
316///////////////////////////////////////////////////////////////////////////////////////////////////
317
318#ifdef GR_TEST_UTILS
319
320#include "GrBatchTest.h"
321
bsalomonabd30f52015-08-13 13:34:48 -0700322DRAW_BATCH_TEST_DEFINE(RectBatch) {
joshualitt2244c272015-08-21 10:33:15 -0700323 GrColor color = GrRandomColor(random);
324 SkRect rect = GrTest::TestRect(random);
325 SkRect localRect = GrTest::TestRect(random);
326 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
327 SkMatrix localMatrix = GrTest::TestMatrix(random);
joshualitt9c80b5f2015-08-13 10:05:51 -0700328
joshualitt2244c272015-08-21 10:33:15 -0700329 bool hasLocalRect = random->nextBool();
330 bool hasLocalMatrix = random->nextBool();
joshualittbcf33d52015-08-26 08:10:35 -0700331 return GrNonAAFillRectBatch::Create(color, viewMatrix, rect,
332 hasLocalRect ? &localRect : nullptr,
333 hasLocalMatrix ? &localMatrix : nullptr);
joshualitt9c80b5f2015-08-13 10:05:51 -0700334}
335
336#endif