blob: 2a248a55af83a0da3f9ac9cf44753829dda2b233 [file] [log] [blame]
csmartdaltona7f29642016-07-07 08:49:11 -07001/*
2 * Copyright 2016 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 "InstancedRendering.h"
9
robertphillips5fa7f302016-07-21 09:21:04 -070010#include "GrCaps.h"
Brian Salomon742e31d2016-12-07 17:06:19 -050011#include "GrOpFlushState.h"
csmartdaltona7f29642016-07-07 08:49:11 -070012#include "GrPipeline.h"
13#include "GrResourceProvider.h"
14#include "instanced/InstanceProcessor.h"
15
16namespace gr_instanced {
17
csmartdaltone0d36292016-07-29 08:14:20 -070018InstancedRendering::InstancedRendering(GrGpu* gpu)
csmartdaltona7f29642016-07-07 08:49:11 -070019 : fGpu(SkRef(gpu)),
csmartdaltona7f29642016-07-07 08:49:11 -070020 fState(State::kRecordingDraws),
dskibae4cd0062016-11-29 06:50:35 -080021 fDrawPool(1024, 1024) {
csmartdaltona7f29642016-07-07 08:49:11 -070022}
23
Brian Salomon99ad1642016-12-16 09:50:45 -050024sk_sp<GrDrawOp> InstancedRendering::recordRect(const SkRect& rect, const SkMatrix& viewMatrix,
25 GrColor color, GrAA aa,
26 const GrInstancedPipelineInfo& info,
27 GrAAType* aaType) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050028 return this->recordShape(ShapeType::kRect, rect, viewMatrix, color, rect, aa, info, aaType);
csmartdaltona7f29642016-07-07 08:49:11 -070029}
30
Brian Salomon99ad1642016-12-16 09:50:45 -050031sk_sp<GrDrawOp> InstancedRendering::recordRect(const SkRect& rect, const SkMatrix& viewMatrix,
32 GrColor color, const SkRect& localRect, GrAA aa,
33 const GrInstancedPipelineInfo& info,
34 GrAAType* aaType) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050035 return this->recordShape(ShapeType::kRect, rect, viewMatrix, color, localRect, aa, info,
36 aaType);
csmartdaltona7f29642016-07-07 08:49:11 -070037}
38
Brian Salomon99ad1642016-12-16 09:50:45 -050039sk_sp<GrDrawOp> InstancedRendering::recordRect(const SkRect& rect, const SkMatrix& viewMatrix,
40 GrColor color, const SkMatrix& localMatrix, GrAA aa,
41 const GrInstancedPipelineInfo& info,
42 GrAAType* aaType) {
csmartdaltona7f29642016-07-07 08:49:11 -070043 if (localMatrix.hasPerspective()) {
44 return nullptr; // Perspective is not yet supported in the local matrix.
45 }
Brian Salomon99ad1642016-12-16 09:50:45 -050046 if (sk_sp<Op> op = this->recordShape(ShapeType::kRect, rect, viewMatrix, color, rect, aa, info,
47 aaType)) {
48 op->getSingleInstance().fInfo |= kLocalMatrix_InfoFlag;
49 op->appendParamsTexel(localMatrix.getScaleX(), localMatrix.getSkewX(),
50 localMatrix.getTranslateX());
51 op->appendParamsTexel(localMatrix.getSkewY(), localMatrix.getScaleY(),
52 localMatrix.getTranslateY());
53 op->fInfo.fHasLocalMatrix = true;
54 return std::move(op);
csmartdaltona7f29642016-07-07 08:49:11 -070055 }
56 return nullptr;
57}
58
Brian Salomon99ad1642016-12-16 09:50:45 -050059sk_sp<GrDrawOp> InstancedRendering::recordOval(const SkRect& oval, const SkMatrix& viewMatrix,
60 GrColor color, GrAA aa,
61 const GrInstancedPipelineInfo& info,
62 GrAAType* aaType) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050063 return this->recordShape(ShapeType::kOval, oval, viewMatrix, color, oval, aa, info, aaType);
csmartdaltona7f29642016-07-07 08:49:11 -070064}
65
Brian Salomon99ad1642016-12-16 09:50:45 -050066sk_sp<GrDrawOp> InstancedRendering::recordRRect(const SkRRect& rrect, const SkMatrix& viewMatrix,
67 GrColor color, GrAA aa,
68 const GrInstancedPipelineInfo& info,
69 GrAAType* aaType) {
70 if (sk_sp<Op> op = this->recordShape(GetRRectShapeType(rrect), rrect.rect(), viewMatrix, color,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050071 rrect.rect(), aa, info, aaType)) {
Brian Salomon99ad1642016-12-16 09:50:45 -050072 op->appendRRectParams(rrect);
73 return std::move(op);
csmartdaltona7f29642016-07-07 08:49:11 -070074 }
75 return nullptr;
76}
77
Brian Salomon99ad1642016-12-16 09:50:45 -050078sk_sp<GrDrawOp> InstancedRendering::recordDRRect(const SkRRect& outer, const SkRRect& inner,
79 const SkMatrix& viewMatrix, GrColor color, GrAA aa,
80 const GrInstancedPipelineInfo& info,
81 GrAAType* aaType) {
csmartdaltona7f29642016-07-07 08:49:11 -070082 if (inner.getType() > SkRRect::kSimple_Type) {
83 return nullptr; // Complex inner round rects are not yet supported.
84 }
85 if (SkRRect::kEmpty_Type == inner.getType()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050086 return this->recordRRect(outer, viewMatrix, color, aa, info, aaType);
csmartdaltona7f29642016-07-07 08:49:11 -070087 }
Brian Salomon99ad1642016-12-16 09:50:45 -050088 if (sk_sp<Op> op = this->recordShape(GetRRectShapeType(outer), outer.rect(), viewMatrix, color,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050089 outer.rect(), aa, info, aaType)) {
Brian Salomon99ad1642016-12-16 09:50:45 -050090 op->appendRRectParams(outer);
csmartdaltona7f29642016-07-07 08:49:11 -070091 ShapeType innerShapeType = GetRRectShapeType(inner);
Brian Salomon99ad1642016-12-16 09:50:45 -050092 op->fInfo.fInnerShapeTypes |= GetShapeFlag(innerShapeType);
93 op->getSingleInstance().fInfo |= ((int)innerShapeType << kInnerShapeType_InfoBit);
94 op->appendParamsTexel(inner.rect().asScalars(), 4);
95 op->appendRRectParams(inner);
96 return std::move(op);
csmartdaltona7f29642016-07-07 08:49:11 -070097 }
98 return nullptr;
99}
100
Brian Salomon99ad1642016-12-16 09:50:45 -0500101sk_sp<InstancedRendering::Op> InstancedRendering::recordShape(
102 ShapeType type, const SkRect& bounds, const SkMatrix& viewMatrix, GrColor color,
103 const SkRect& localRect, GrAA aa, const GrInstancedPipelineInfo& info, GrAAType* aaType) {
csmartdaltona7f29642016-07-07 08:49:11 -0700104 SkASSERT(State::kRecordingDraws == fState);
105
csmartdaltone0d36292016-07-29 08:14:20 -0700106 if (info.fIsRenderingToFloat && fGpu->caps()->avoidInstancedDrawsToFPTargets()) {
csmartdaltona7f29642016-07-07 08:49:11 -0700107 return nullptr;
108 }
109
110 AntialiasMode antialiasMode;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500111 if (!this->selectAntialiasMode(viewMatrix, aa, info, aaType, &antialiasMode)) {
csmartdaltona7f29642016-07-07 08:49:11 -0700112 return nullptr;
113 }
114
Brian Salomon99ad1642016-12-16 09:50:45 -0500115 sk_sp<Op> op = this->makeOp();
116 op->fInfo.fAntialiasMode = antialiasMode;
117 op->fInfo.fShapeTypes = GetShapeFlag(type);
118 op->fInfo.fCannotDiscard = !info.fCanDiscard;
csmartdaltona7f29642016-07-07 08:49:11 -0700119
Brian Salomon99ad1642016-12-16 09:50:45 -0500120 Instance& instance = op->getSingleInstance();
csmartdaltona7f29642016-07-07 08:49:11 -0700121 instance.fInfo = (int)type << kShapeType_InfoBit;
122
Brian Salomon99ad1642016-12-16 09:50:45 -0500123 Op::HasAABloat aaBloat = (antialiasMode == AntialiasMode::kCoverage) ? Op::HasAABloat::kYes
124 : Op::HasAABloat::kNo;
125 Op::IsZeroArea zeroArea = (bounds.isEmpty()) ? Op::IsZeroArea::kYes : Op::IsZeroArea::kNo;
bsalomon88cf17d2016-07-08 06:40:56 -0700126
csmartdaltona7f29642016-07-07 08:49:11 -0700127 // The instanced shape renderer draws rectangles of [-1, -1, +1, +1], so we find the matrix that
128 // will map this rectangle to the same device coordinates as "viewMatrix * bounds".
129 float sx = 0.5f * bounds.width();
130 float sy = 0.5f * bounds.height();
131 float tx = sx + bounds.fLeft;
132 float ty = sy + bounds.fTop;
133 if (!viewMatrix.hasPerspective()) {
134 float* m = instance.fShapeMatrix2x3;
135 m[0] = viewMatrix.getScaleX() * sx;
136 m[1] = viewMatrix.getSkewX() * sy;
137 m[2] = viewMatrix.getTranslateX() +
138 viewMatrix.getScaleX() * tx + viewMatrix.getSkewX() * ty;
139
140 m[3] = viewMatrix.getSkewY() * sx;
141 m[4] = viewMatrix.getScaleY() * sy;
142 m[5] = viewMatrix.getTranslateY() +
143 viewMatrix.getSkewY() * tx + viewMatrix.getScaleY() * ty;
144
145 // Since 'm' is a 2x3 matrix that maps the rect [-1, +1] into the shape's device-space quad,
146 // it's quite simple to find the bounding rectangle:
147 float devBoundsHalfWidth = fabsf(m[0]) + fabsf(m[1]);
148 float devBoundsHalfHeight = fabsf(m[3]) + fabsf(m[4]);
Brian Salomon99ad1642016-12-16 09:50:45 -0500149 SkRect opBounds;
150 opBounds.fLeft = m[2] - devBoundsHalfWidth;
151 opBounds.fRight = m[2] + devBoundsHalfWidth;
152 opBounds.fTop = m[5] - devBoundsHalfHeight;
153 opBounds.fBottom = m[5] + devBoundsHalfHeight;
154 op->setBounds(opBounds, aaBloat, zeroArea);
csmartdaltona7f29642016-07-07 08:49:11 -0700155
156 // TODO: Is this worth the CPU overhead?
Brian Salomon99ad1642016-12-16 09:50:45 -0500157 op->fInfo.fNonSquare =
158 fabsf(devBoundsHalfHeight - devBoundsHalfWidth) > 0.5f || // Early out.
159 fabs(m[0] * m[3] + m[1] * m[4]) > 1e-3f || // Skew?
160 fabs(m[0] * m[0] + m[1] * m[1] - m[3] * m[3] - m[4] * m[4]) >
161 1e-2f; // Diff. lengths?
csmartdaltona7f29642016-07-07 08:49:11 -0700162 } else {
163 SkMatrix shapeMatrix(viewMatrix);
164 shapeMatrix.preTranslate(tx, ty);
165 shapeMatrix.preScale(sx, sy);
166 instance.fInfo |= kPerspective_InfoFlag;
167
168 float* m = instance.fShapeMatrix2x3;
169 m[0] = SkScalarToFloat(shapeMatrix.getScaleX());
170 m[1] = SkScalarToFloat(shapeMatrix.getSkewX());
171 m[2] = SkScalarToFloat(shapeMatrix.getTranslateX());
172 m[3] = SkScalarToFloat(shapeMatrix.getSkewY());
173 m[4] = SkScalarToFloat(shapeMatrix.getScaleY());
174 m[5] = SkScalarToFloat(shapeMatrix.getTranslateY());
175
176 // Send the perspective column as a param.
Brian Salomon99ad1642016-12-16 09:50:45 -0500177 op->appendParamsTexel(shapeMatrix[SkMatrix::kMPersp0], shapeMatrix[SkMatrix::kMPersp1],
178 shapeMatrix[SkMatrix::kMPersp2]);
179 op->fInfo.fHasPerspective = true;
csmartdaltona7f29642016-07-07 08:49:11 -0700180
Brian Salomon99ad1642016-12-16 09:50:45 -0500181 op->setBounds(bounds, aaBloat, zeroArea);
182 op->fInfo.fNonSquare = true;
csmartdaltona7f29642016-07-07 08:49:11 -0700183 }
184
185 instance.fColor = color;
186
187 const float* rectAsFloats = localRect.asScalars(); // Ensure SkScalar == float.
188 memcpy(&instance.fLocalRect, rectAsFloats, 4 * sizeof(float));
189
Brian Salomon99ad1642016-12-16 09:50:45 -0500190 op->fPixelLoad = op->bounds().height() * op->bounds().width();
191 return op;
csmartdaltona7f29642016-07-07 08:49:11 -0700192}
193
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500194inline bool InstancedRendering::selectAntialiasMode(const SkMatrix& viewMatrix, GrAA aa,
csmartdaltona7f29642016-07-07 08:49:11 -0700195 const GrInstancedPipelineInfo& info,
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500196 GrAAType* aaType,
197 AntialiasMode* antialiasMode) {
csmartdaltona7f29642016-07-07 08:49:11 -0700198 SkASSERT(!info.fColorDisabled || info.fDrawingShapeToStencil);
199 SkASSERT(!info.fIsMixedSampled || info.fIsMultisampled);
csmartdaltone0d36292016-07-29 08:14:20 -0700200 SkASSERT(GrCaps::InstancedSupport::kNone != fGpu->caps()->instancedSupport());
csmartdaltona7f29642016-07-07 08:49:11 -0700201
202 if (!info.fIsMultisampled || fGpu->caps()->multisampleDisableSupport()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500203 if (GrAA::kNo == aa) {
csmartdaltona7f29642016-07-07 08:49:11 -0700204 if (info.fDrawingShapeToStencil && !info.fCanDiscard) {
205 // We can't draw to the stencil buffer without discard (or sample mask if MSAA).
206 return false;
207 }
208 *antialiasMode = AntialiasMode::kNone;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500209 *aaType = GrAAType::kNone;
csmartdaltona7f29642016-07-07 08:49:11 -0700210 return true;
211 }
212
213 if (info.canUseCoverageAA() && viewMatrix.preservesRightAngles()) {
214 *antialiasMode = AntialiasMode::kCoverage;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500215 *aaType = GrAAType::kCoverage;
csmartdaltona7f29642016-07-07 08:49:11 -0700216 return true;
217 }
218 }
219
csmartdaltone0d36292016-07-29 08:14:20 -0700220 if (info.fIsMultisampled &&
221 fGpu->caps()->instancedSupport() >= GrCaps::InstancedSupport::kMultisampled) {
csmartdaltona7f29642016-07-07 08:49:11 -0700222 if (!info.fIsMixedSampled || info.fColorDisabled) {
223 *antialiasMode = AntialiasMode::kMSAA;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500224 *aaType = GrAAType::kMSAA;
csmartdaltona7f29642016-07-07 08:49:11 -0700225 return true;
226 }
csmartdaltone0d36292016-07-29 08:14:20 -0700227 if (fGpu->caps()->instancedSupport() >= GrCaps::InstancedSupport::kMixedSampled) {
csmartdaltona7f29642016-07-07 08:49:11 -0700228 *antialiasMode = AntialiasMode::kMixedSamples;
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500229 *aaType = GrAAType::kMixedSamples;
csmartdaltona7f29642016-07-07 08:49:11 -0700230 return true;
231 }
232 }
233
234 return false;
235}
236
Brian Salomon99ad1642016-12-16 09:50:45 -0500237InstancedRendering::Op::Op(uint32_t classID, InstancedRendering* ir)
238 : INHERITED(classID)
239 , fInstancedRendering(ir)
240 , fIsTracked(false)
241 , fNumDraws(1)
242 , fNumChangesInGeometry(0) {
dskibae4cd0062016-11-29 06:50:35 -0800243 fHeadDraw = fTailDraw = fInstancedRendering->fDrawPool.allocate();
csmartdaltona7f29642016-07-07 08:49:11 -0700244#ifdef SK_DEBUG
245 fHeadDraw->fGeometry = {-1, 0};
246#endif
247 fHeadDraw->fNext = nullptr;
248}
249
Brian Salomon99ad1642016-12-16 09:50:45 -0500250InstancedRendering::Op::~Op() {
csmartdaltona7f29642016-07-07 08:49:11 -0700251 if (fIsTracked) {
Brian Salomon99ad1642016-12-16 09:50:45 -0500252 fInstancedRendering->fTrackedOps.remove(this);
csmartdaltona7f29642016-07-07 08:49:11 -0700253 }
254
255 Draw* draw = fHeadDraw;
256 while (draw) {
257 Draw* next = draw->fNext;
258 fInstancedRendering->fDrawPool.release(draw);
259 draw = next;
260 }
261}
262
Brian Salomon99ad1642016-12-16 09:50:45 -0500263void InstancedRendering::Op::appendRRectParams(const SkRRect& rrect) {
csmartdaltona7f29642016-07-07 08:49:11 -0700264 SkASSERT(!fIsTracked);
265 switch (rrect.getType()) {
266 case SkRRect::kSimple_Type: {
267 const SkVector& radii = rrect.getSimpleRadii();
268 this->appendParamsTexel(radii.x(), radii.y(), rrect.width(), rrect.height());
269 return;
270 }
271 case SkRRect::kNinePatch_Type: {
272 float twoOverW = 2 / rrect.width();
273 float twoOverH = 2 / rrect.height();
274 const SkVector& radiiTL = rrect.radii(SkRRect::kUpperLeft_Corner);
275 const SkVector& radiiBR = rrect.radii(SkRRect::kLowerRight_Corner);
276 this->appendParamsTexel(radiiTL.x() * twoOverW, radiiBR.x() * twoOverW,
277 radiiTL.y() * twoOverH, radiiBR.y() * twoOverH);
278 return;
279 }
280 case SkRRect::kComplex_Type: {
281 /**
282 * The x and y radii of each arc are stored in separate vectors,
283 * in the following order:
284 *
285 * __x1 _ _ _ x3__
286 * y1 | | y2
287 *
288 * | |
289 *
290 * y3 |__ _ _ _ __| y4
291 * x2 x4
292 *
293 */
294 float twoOverW = 2 / rrect.width();
295 float twoOverH = 2 / rrect.height();
296 const SkVector& radiiTL = rrect.radii(SkRRect::kUpperLeft_Corner);
297 const SkVector& radiiTR = rrect.radii(SkRRect::kUpperRight_Corner);
298 const SkVector& radiiBR = rrect.radii(SkRRect::kLowerRight_Corner);
299 const SkVector& radiiBL = rrect.radii(SkRRect::kLowerLeft_Corner);
300 this->appendParamsTexel(radiiTL.x() * twoOverW, radiiBL.x() * twoOverW,
301 radiiTR.x() * twoOverW, radiiBR.x() * twoOverW);
302 this->appendParamsTexel(radiiTL.y() * twoOverH, radiiTR.y() * twoOverH,
303 radiiBL.y() * twoOverH, radiiBR.y() * twoOverH);
304 return;
305 }
306 default: return;
307 }
308}
309
Brian Salomon99ad1642016-12-16 09:50:45 -0500310void InstancedRendering::Op::appendParamsTexel(const SkScalar* vals, int count) {
csmartdaltona7f29642016-07-07 08:49:11 -0700311 SkASSERT(!fIsTracked);
312 SkASSERT(count <= 4 && count >= 0);
313 const float* valsAsFloats = vals; // Ensure SkScalar == float.
314 memcpy(&fParams.push_back(), valsAsFloats, count * sizeof(float));
315 fInfo.fHasParams = true;
316}
317
Brian Salomon99ad1642016-12-16 09:50:45 -0500318void InstancedRendering::Op::appendParamsTexel(SkScalar x, SkScalar y, SkScalar z, SkScalar w) {
csmartdaltona7f29642016-07-07 08:49:11 -0700319 SkASSERT(!fIsTracked);
320 ParamsTexel& texel = fParams.push_back();
321 texel.fX = SkScalarToFloat(x);
322 texel.fY = SkScalarToFloat(y);
323 texel.fZ = SkScalarToFloat(z);
324 texel.fW = SkScalarToFloat(w);
325 fInfo.fHasParams = true;
326}
327
Brian Salomon99ad1642016-12-16 09:50:45 -0500328void InstancedRendering::Op::appendParamsTexel(SkScalar x, SkScalar y, SkScalar z) {
csmartdaltona7f29642016-07-07 08:49:11 -0700329 SkASSERT(!fIsTracked);
330 ParamsTexel& texel = fParams.push_back();
331 texel.fX = SkScalarToFloat(x);
332 texel.fY = SkScalarToFloat(y);
333 texel.fZ = SkScalarToFloat(z);
334 fInfo.fHasParams = true;
335}
336
Brian Salomon99ad1642016-12-16 09:50:45 -0500337void InstancedRendering::Op::computePipelineOptimizations(GrInitInvariantOutput* color,
338 GrInitInvariantOutput* coverage,
339 GrBatchToXPOverrides* overrides) const {
csmartdaltona7f29642016-07-07 08:49:11 -0700340 color->setKnownFourComponents(this->getSingleInstance().fColor);
341
342 if (AntialiasMode::kCoverage == fInfo.fAntialiasMode ||
343 (AntialiasMode::kNone == fInfo.fAntialiasMode &&
344 !fInfo.isSimpleRects() && fInfo.fCannotDiscard)) {
345 coverage->setUnknownSingleComponent();
346 } else {
347 coverage->setKnownSingleComponent(255);
348 }
349}
350
Brian Salomon99ad1642016-12-16 09:50:45 -0500351void InstancedRendering::Op::initBatchTracker(const GrXPOverridesForBatch& overrides) {
csmartdaltona7f29642016-07-07 08:49:11 -0700352 Draw& draw = this->getSingleDraw(); // This will assert if we have > 1 command.
353 SkASSERT(draw.fGeometry.isEmpty());
354 SkASSERT(SkIsPow2(fInfo.fShapeTypes));
355 SkASSERT(!fIsTracked);
356
357 if (kRect_ShapeFlag == fInfo.fShapeTypes) {
358 draw.fGeometry = InstanceProcessor::GetIndexRangeForRect(fInfo.fAntialiasMode);
359 } else if (kOval_ShapeFlag == fInfo.fShapeTypes) {
bsalomon88cf17d2016-07-08 06:40:56 -0700360 draw.fGeometry = InstanceProcessor::GetIndexRangeForOval(fInfo.fAntialiasMode,
361 this->bounds());
csmartdaltona7f29642016-07-07 08:49:11 -0700362 } else {
363 draw.fGeometry = InstanceProcessor::GetIndexRangeForRRect(fInfo.fAntialiasMode);
364 }
365
366 if (!fParams.empty()) {
367 SkASSERT(fInstancedRendering->fParams.count() < (int)kParamsIdx_InfoMask); // TODO: cleaner.
368 this->getSingleInstance().fInfo |= fInstancedRendering->fParams.count();
369 fInstancedRendering->fParams.push_back_n(fParams.count(), fParams.begin());
370 }
371
372 GrColor overrideColor;
373 if (overrides.getOverrideColorIfSet(&overrideColor)) {
374 SkASSERT(State::kRecordingDraws == fInstancedRendering->fState);
375 this->getSingleInstance().fColor = overrideColor;
376 }
377 fInfo.fUsesLocalCoords = overrides.readsLocalCoords();
378 fInfo.fCannotTweakAlphaForCoverage = !overrides.canTweakAlphaForCoverage();
379
Brian Salomon99ad1642016-12-16 09:50:45 -0500380 fInstancedRendering->fTrackedOps.addToTail(this);
csmartdaltona7f29642016-07-07 08:49:11 -0700381 fIsTracked = true;
382}
383
Brian Salomon99ad1642016-12-16 09:50:45 -0500384bool InstancedRendering::Op::onCombineIfPossible(GrOp* other, const GrCaps& caps) {
385 Op* that = static_cast<Op*>(other);
csmartdaltona7f29642016-07-07 08:49:11 -0700386 SkASSERT(fInstancedRendering == that->fInstancedRendering);
387 SkASSERT(fTailDraw);
388 SkASSERT(that->fTailDraw);
389
Brian Salomon99ad1642016-12-16 09:50:45 -0500390 if (!OpInfo::CanCombine(fInfo, that->fInfo) ||
391 !GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
392 that->bounds(), caps)) {
csmartdaltona7f29642016-07-07 08:49:11 -0700393 return false;
394 }
395
Brian Salomon99ad1642016-12-16 09:50:45 -0500396 OpInfo combinedInfo = fInfo | that->fInfo;
csmartdaltona7f29642016-07-07 08:49:11 -0700397 if (!combinedInfo.isSimpleRects()) {
398 // This threshold was chosen with the "shapes_mixed" bench on a MacBook with Intel graphics.
399 // There seems to be a wide range where it doesn't matter if we combine or not. What matters
400 // is that the itty bitty rects combine with other shapes and the giant ones don't.
401 constexpr SkScalar kMaxPixelsToGeneralizeRects = 256 * 256;
402 if (fInfo.isSimpleRects() && fPixelLoad > kMaxPixelsToGeneralizeRects) {
403 return false;
404 }
405 if (that->fInfo.isSimpleRects() && that->fPixelLoad > kMaxPixelsToGeneralizeRects) {
406 return false;
407 }
408 }
409
bsalomon88cf17d2016-07-08 06:40:56 -0700410 this->joinBounds(*that);
csmartdaltona7f29642016-07-07 08:49:11 -0700411 fInfo = combinedInfo;
412 fPixelLoad += that->fPixelLoad;
413
Brian Salomon99ad1642016-12-16 09:50:45 -0500414 // Adopt the other op's draws.
csmartdaltona7f29642016-07-07 08:49:11 -0700415 fNumDraws += that->fNumDraws;
416 fNumChangesInGeometry += that->fNumChangesInGeometry;
417 if (fTailDraw->fGeometry != that->fHeadDraw->fGeometry) {
418 ++fNumChangesInGeometry;
419 }
420 fTailDraw->fNext = that->fHeadDraw;
421 fTailDraw = that->fTailDraw;
422
423 that->fHeadDraw = that->fTailDraw = nullptr;
424
425 return true;
426}
427
428void InstancedRendering::beginFlush(GrResourceProvider* rp) {
429 SkASSERT(State::kRecordingDraws == fState);
430 fState = State::kFlushing;
431
Brian Salomon99ad1642016-12-16 09:50:45 -0500432 if (fTrackedOps.isEmpty()) {
csmartdaltona7f29642016-07-07 08:49:11 -0700433 return;
434 }
435
436 if (!fVertexBuffer) {
Hal Canary144caf52016-11-07 17:57:18 -0500437 fVertexBuffer.reset(InstanceProcessor::FindOrCreateVertexBuffer(fGpu.get()));
csmartdaltona7f29642016-07-07 08:49:11 -0700438 if (!fVertexBuffer) {
439 return;
440 }
441 }
442
443 if (!fIndexBuffer) {
Hal Canary144caf52016-11-07 17:57:18 -0500444 fIndexBuffer.reset(InstanceProcessor::FindOrCreateIndex8Buffer(fGpu.get()));
csmartdaltona7f29642016-07-07 08:49:11 -0700445 if (!fIndexBuffer) {
446 return;
447 }
448 }
449
450 if (!fParams.empty()) {
451 fParamsBuffer.reset(rp->createBuffer(fParams.count() * sizeof(ParamsTexel),
452 kTexel_GrBufferType, kDynamic_GrAccessPattern,
csmartdalton485a1202016-07-13 10:16:32 -0700453 GrResourceProvider::kNoPendingIO_Flag |
454 GrResourceProvider::kRequireGpuMemory_Flag,
csmartdaltona7f29642016-07-07 08:49:11 -0700455 fParams.begin()));
456 if (!fParamsBuffer) {
457 return;
458 }
459 }
460
461 this->onBeginFlush(rp);
462}
463
Brian Salomon99ad1642016-12-16 09:50:45 -0500464void InstancedRendering::Op::onDraw(GrOpFlushState* state, const SkRect& bounds) {
csmartdaltona7f29642016-07-07 08:49:11 -0700465 SkASSERT(State::kFlushing == fInstancedRendering->fState);
466 SkASSERT(state->gpu() == fInstancedRendering->gpu());
467
468 state->gpu()->handleDirtyContext();
469 if (GrXferBarrierType barrierType = this->pipeline()->xferBarrierType(*state->gpu()->caps())) {
470 state->gpu()->xferBarrier(this->pipeline()->getRenderTarget(), barrierType);
471 }
472
Hal Canary144caf52016-11-07 17:57:18 -0500473 InstanceProcessor instProc(fInfo, fInstancedRendering->fParamsBuffer.get());
csmartdaltona7f29642016-07-07 08:49:11 -0700474 fInstancedRendering->onDraw(*this->pipeline(), instProc, this);
475}
476
477void InstancedRendering::endFlush() {
Brian Salomon99ad1642016-12-16 09:50:45 -0500478 // The caller is expected to delete all tracked ops (i.e. ops whose initBatchTracker
csmartdaltona7f29642016-07-07 08:49:11 -0700479 // method has been called) before ending the flush.
Brian Salomon99ad1642016-12-16 09:50:45 -0500480 SkASSERT(fTrackedOps.isEmpty());
csmartdaltona7f29642016-07-07 08:49:11 -0700481 fParams.reset();
482 fParamsBuffer.reset();
483 this->onEndFlush();
484 fState = State::kRecordingDraws;
485 // Hold on to the shape coords and index buffers.
486}
487
488void InstancedRendering::resetGpuResources(ResetType resetType) {
489 fVertexBuffer.reset();
490 fIndexBuffer.reset();
491 fParamsBuffer.reset();
492 this->onResetGpuResources(resetType);
493}
494
495}