blob: 1ffaaa44d4ae2151590fea9c2fe9a95f5d0a4749 [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 Salomonf8334782017-01-03 09:42:58 -050024std::unique_ptr<GrDrawOp> InstancedRendering::recordRect(const SkRect& rect,
25 const SkMatrix& viewMatrix, GrColor color,
26 GrAA aa,
27 const GrInstancedPipelineInfo& info,
28 GrAAType* aaType) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050029 return this->recordShape(ShapeType::kRect, rect, viewMatrix, color, rect, aa, info, aaType);
csmartdaltona7f29642016-07-07 08:49:11 -070030}
31
Brian Salomonf8334782017-01-03 09:42:58 -050032std::unique_ptr<GrDrawOp> InstancedRendering::recordRect(const SkRect& rect,
33 const SkMatrix& viewMatrix, GrColor color,
34 const SkRect& localRect, GrAA aa,
35 const GrInstancedPipelineInfo& info,
36 GrAAType* aaType) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050037 return this->recordShape(ShapeType::kRect, rect, viewMatrix, color, localRect, aa, info,
38 aaType);
csmartdaltona7f29642016-07-07 08:49:11 -070039}
40
Brian Salomonf8334782017-01-03 09:42:58 -050041std::unique_ptr<GrDrawOp> InstancedRendering::recordRect(const SkRect& rect,
42 const SkMatrix& viewMatrix, GrColor color,
43 const SkMatrix& localMatrix, GrAA aa,
44 const GrInstancedPipelineInfo& info,
45 GrAAType* aaType) {
csmartdaltona7f29642016-07-07 08:49:11 -070046 if (localMatrix.hasPerspective()) {
47 return nullptr; // Perspective is not yet supported in the local matrix.
48 }
Brian Salomonf8334782017-01-03 09:42:58 -050049 if (std::unique_ptr<Op> op = this->recordShape(ShapeType::kRect, rect, viewMatrix, color, rect,
50 aa, info, aaType)) {
Brian Salomon99ad1642016-12-16 09:50:45 -050051 op->getSingleInstance().fInfo |= kLocalMatrix_InfoFlag;
52 op->appendParamsTexel(localMatrix.getScaleX(), localMatrix.getSkewX(),
53 localMatrix.getTranslateX());
54 op->appendParamsTexel(localMatrix.getSkewY(), localMatrix.getScaleY(),
55 localMatrix.getTranslateY());
56 op->fInfo.fHasLocalMatrix = true;
57 return std::move(op);
csmartdaltona7f29642016-07-07 08:49:11 -070058 }
59 return nullptr;
60}
61
Brian Salomonf8334782017-01-03 09:42:58 -050062std::unique_ptr<GrDrawOp> InstancedRendering::recordOval(const SkRect& oval,
63 const SkMatrix& viewMatrix, GrColor color,
64 GrAA aa,
65 const GrInstancedPipelineInfo& info,
66 GrAAType* aaType) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050067 return this->recordShape(ShapeType::kOval, oval, viewMatrix, color, oval, aa, info, aaType);
csmartdaltona7f29642016-07-07 08:49:11 -070068}
69
Brian Salomonf8334782017-01-03 09:42:58 -050070std::unique_ptr<GrDrawOp> InstancedRendering::recordRRect(const SkRRect& rrect,
71 const SkMatrix& viewMatrix, GrColor color,
72 GrAA aa,
73 const GrInstancedPipelineInfo& info,
74 GrAAType* aaType) {
75 if (std::unique_ptr<Op> op =
76 this->recordShape(GetRRectShapeType(rrect), rrect.rect(), viewMatrix, color,
77 rrect.rect(), aa, info, aaType)) {
Brian Salomon99ad1642016-12-16 09:50:45 -050078 op->appendRRectParams(rrect);
79 return std::move(op);
csmartdaltona7f29642016-07-07 08:49:11 -070080 }
81 return nullptr;
82}
83
Brian Salomonf8334782017-01-03 09:42:58 -050084std::unique_ptr<GrDrawOp> InstancedRendering::recordDRRect(
85 const SkRRect& outer, const SkRRect& inner, const SkMatrix& viewMatrix, GrColor color,
86 GrAA aa, const GrInstancedPipelineInfo& info, GrAAType* aaType) {
csmartdaltona7f29642016-07-07 08:49:11 -070087 if (inner.getType() > SkRRect::kSimple_Type) {
88 return nullptr; // Complex inner round rects are not yet supported.
89 }
90 if (SkRRect::kEmpty_Type == inner.getType()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -050091 return this->recordRRect(outer, viewMatrix, color, aa, info, aaType);
csmartdaltona7f29642016-07-07 08:49:11 -070092 }
Brian Salomonf8334782017-01-03 09:42:58 -050093 if (std::unique_ptr<Op> op =
94 this->recordShape(GetRRectShapeType(outer), outer.rect(), viewMatrix, color,
95 outer.rect(), aa, info, aaType)) {
Brian Salomon99ad1642016-12-16 09:50:45 -050096 op->appendRRectParams(outer);
csmartdaltona7f29642016-07-07 08:49:11 -070097 ShapeType innerShapeType = GetRRectShapeType(inner);
Brian Salomon99ad1642016-12-16 09:50:45 -050098 op->fInfo.fInnerShapeTypes |= GetShapeFlag(innerShapeType);
99 op->getSingleInstance().fInfo |= ((int)innerShapeType << kInnerShapeType_InfoBit);
100 op->appendParamsTexel(inner.rect().asScalars(), 4);
101 op->appendRRectParams(inner);
102 return std::move(op);
csmartdaltona7f29642016-07-07 08:49:11 -0700103 }
104 return nullptr;
105}
106
Brian Salomonf8334782017-01-03 09:42:58 -0500107std::unique_ptr<InstancedRendering::Op> InstancedRendering::recordShape(
Brian Salomon99ad1642016-12-16 09:50:45 -0500108 ShapeType type, const SkRect& bounds, const SkMatrix& viewMatrix, GrColor color,
109 const SkRect& localRect, GrAA aa, const GrInstancedPipelineInfo& info, GrAAType* aaType) {
csmartdaltona7f29642016-07-07 08:49:11 -0700110 SkASSERT(State::kRecordingDraws == fState);
111
csmartdaltone0d36292016-07-29 08:14:20 -0700112 if (info.fIsRenderingToFloat && fGpu->caps()->avoidInstancedDrawsToFPTargets()) {
csmartdaltona7f29642016-07-07 08:49:11 -0700113 return nullptr;
114 }
115
Brian Salomonaf9847e2017-03-01 11:28:27 -0500116 if (!this->selectAntialiasMode(viewMatrix, aa, info, aaType)) {
csmartdaltona7f29642016-07-07 08:49:11 -0700117 return nullptr;
118 }
119
Brian Salomonf8334782017-01-03 09:42:58 -0500120 std::unique_ptr<Op> op = this->makeOp();
Brian Salomonaf9847e2017-03-01 11:28:27 -0500121 op->fInfo.setAAType(*aaType);
Brian Salomon99ad1642016-12-16 09:50:45 -0500122 op->fInfo.fShapeTypes = GetShapeFlag(type);
Brian Salomon5ff3a5c2017-03-01 14:34:41 -0500123 op->fInfo.fCannotDiscard = true;
csmartdaltona7f29642016-07-07 08:49:11 -0700124
Brian Salomon99ad1642016-12-16 09:50:45 -0500125 Instance& instance = op->getSingleInstance();
csmartdaltona7f29642016-07-07 08:49:11 -0700126 instance.fInfo = (int)type << kShapeType_InfoBit;
127
Brian Salomonaf9847e2017-03-01 11:28:27 -0500128 Op::HasAABloat aaBloat =
129 (*aaType == GrAAType::kCoverage) ? Op::HasAABloat::kYes : Op::HasAABloat::kNo;
Brian Salomon99ad1642016-12-16 09:50:45 -0500130 Op::IsZeroArea zeroArea = (bounds.isEmpty()) ? Op::IsZeroArea::kYes : Op::IsZeroArea::kNo;
bsalomon88cf17d2016-07-08 06:40:56 -0700131
csmartdaltona7f29642016-07-07 08:49:11 -0700132 // The instanced shape renderer draws rectangles of [-1, -1, +1, +1], so we find the matrix that
133 // will map this rectangle to the same device coordinates as "viewMatrix * bounds".
134 float sx = 0.5f * bounds.width();
135 float sy = 0.5f * bounds.height();
136 float tx = sx + bounds.fLeft;
137 float ty = sy + bounds.fTop;
138 if (!viewMatrix.hasPerspective()) {
139 float* m = instance.fShapeMatrix2x3;
140 m[0] = viewMatrix.getScaleX() * sx;
141 m[1] = viewMatrix.getSkewX() * sy;
142 m[2] = viewMatrix.getTranslateX() +
143 viewMatrix.getScaleX() * tx + viewMatrix.getSkewX() * ty;
144
145 m[3] = viewMatrix.getSkewY() * sx;
146 m[4] = viewMatrix.getScaleY() * sy;
147 m[5] = viewMatrix.getTranslateY() +
148 viewMatrix.getSkewY() * tx + viewMatrix.getScaleY() * ty;
149
150 // Since 'm' is a 2x3 matrix that maps the rect [-1, +1] into the shape's device-space quad,
151 // it's quite simple to find the bounding rectangle:
152 float devBoundsHalfWidth = fabsf(m[0]) + fabsf(m[1]);
153 float devBoundsHalfHeight = fabsf(m[3]) + fabsf(m[4]);
Brian Salomon99ad1642016-12-16 09:50:45 -0500154 SkRect opBounds;
155 opBounds.fLeft = m[2] - devBoundsHalfWidth;
156 opBounds.fRight = m[2] + devBoundsHalfWidth;
157 opBounds.fTop = m[5] - devBoundsHalfHeight;
158 opBounds.fBottom = m[5] + devBoundsHalfHeight;
159 op->setBounds(opBounds, aaBloat, zeroArea);
csmartdaltona7f29642016-07-07 08:49:11 -0700160
161 // TODO: Is this worth the CPU overhead?
Brian Salomon99ad1642016-12-16 09:50:45 -0500162 op->fInfo.fNonSquare =
163 fabsf(devBoundsHalfHeight - devBoundsHalfWidth) > 0.5f || // Early out.
164 fabs(m[0] * m[3] + m[1] * m[4]) > 1e-3f || // Skew?
165 fabs(m[0] * m[0] + m[1] * m[1] - m[3] * m[3] - m[4] * m[4]) >
166 1e-2f; // Diff. lengths?
csmartdaltona7f29642016-07-07 08:49:11 -0700167 } else {
168 SkMatrix shapeMatrix(viewMatrix);
169 shapeMatrix.preTranslate(tx, ty);
170 shapeMatrix.preScale(sx, sy);
171 instance.fInfo |= kPerspective_InfoFlag;
172
173 float* m = instance.fShapeMatrix2x3;
174 m[0] = SkScalarToFloat(shapeMatrix.getScaleX());
175 m[1] = SkScalarToFloat(shapeMatrix.getSkewX());
176 m[2] = SkScalarToFloat(shapeMatrix.getTranslateX());
177 m[3] = SkScalarToFloat(shapeMatrix.getSkewY());
178 m[4] = SkScalarToFloat(shapeMatrix.getScaleY());
179 m[5] = SkScalarToFloat(shapeMatrix.getTranslateY());
180
181 // Send the perspective column as a param.
Brian Salomon99ad1642016-12-16 09:50:45 -0500182 op->appendParamsTexel(shapeMatrix[SkMatrix::kMPersp0], shapeMatrix[SkMatrix::kMPersp1],
183 shapeMatrix[SkMatrix::kMPersp2]);
184 op->fInfo.fHasPerspective = true;
csmartdaltona7f29642016-07-07 08:49:11 -0700185
Brian Salomon99ad1642016-12-16 09:50:45 -0500186 op->setBounds(bounds, aaBloat, zeroArea);
187 op->fInfo.fNonSquare = true;
csmartdaltona7f29642016-07-07 08:49:11 -0700188 }
189
190 instance.fColor = color;
191
192 const float* rectAsFloats = localRect.asScalars(); // Ensure SkScalar == float.
193 memcpy(&instance.fLocalRect, rectAsFloats, 4 * sizeof(float));
194
Brian Salomon99ad1642016-12-16 09:50:45 -0500195 op->fPixelLoad = op->bounds().height() * op->bounds().width();
196 return op;
csmartdaltona7f29642016-07-07 08:49:11 -0700197}
198
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500199inline bool InstancedRendering::selectAntialiasMode(const SkMatrix& viewMatrix, GrAA aa,
csmartdaltona7f29642016-07-07 08:49:11 -0700200 const GrInstancedPipelineInfo& info,
Brian Salomonaf9847e2017-03-01 11:28:27 -0500201 GrAAType* aaType) {
csmartdaltona7f29642016-07-07 08:49:11 -0700202 SkASSERT(!info.fIsMixedSampled || info.fIsMultisampled);
csmartdaltone0d36292016-07-29 08:14:20 -0700203 SkASSERT(GrCaps::InstancedSupport::kNone != fGpu->caps()->instancedSupport());
csmartdaltona7f29642016-07-07 08:49:11 -0700204
205 if (!info.fIsMultisampled || fGpu->caps()->multisampleDisableSupport()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500206 if (GrAA::kNo == aa) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500207 *aaType = GrAAType::kNone;
csmartdaltona7f29642016-07-07 08:49:11 -0700208 return true;
209 }
210
211 if (info.canUseCoverageAA() && viewMatrix.preservesRightAngles()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500212 *aaType = GrAAType::kCoverage;
csmartdaltona7f29642016-07-07 08:49:11 -0700213 return true;
214 }
215 }
216
csmartdaltone0d36292016-07-29 08:14:20 -0700217 if (info.fIsMultisampled &&
218 fGpu->caps()->instancedSupport() >= GrCaps::InstancedSupport::kMultisampled) {
Brian Salomon5ff3a5c2017-03-01 14:34:41 -0500219 if (!info.fIsMixedSampled) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500220 *aaType = GrAAType::kMSAA;
csmartdaltona7f29642016-07-07 08:49:11 -0700221 return true;
222 }
csmartdaltone0d36292016-07-29 08:14:20 -0700223 if (fGpu->caps()->instancedSupport() >= GrCaps::InstancedSupport::kMixedSampled) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500224 *aaType = GrAAType::kMixedSamples;
csmartdaltona7f29642016-07-07 08:49:11 -0700225 return true;
226 }
227 }
228
229 return false;
230}
231
Brian Salomon99ad1642016-12-16 09:50:45 -0500232InstancedRendering::Op::Op(uint32_t classID, InstancedRendering* ir)
233 : INHERITED(classID)
234 , fInstancedRendering(ir)
235 , fIsTracked(false)
236 , fNumDraws(1)
237 , fNumChangesInGeometry(0) {
dskibae4cd0062016-11-29 06:50:35 -0800238 fHeadDraw = fTailDraw = fInstancedRendering->fDrawPool.allocate();
csmartdaltona7f29642016-07-07 08:49:11 -0700239#ifdef SK_DEBUG
240 fHeadDraw->fGeometry = {-1, 0};
241#endif
242 fHeadDraw->fNext = nullptr;
243}
244
Brian Salomon99ad1642016-12-16 09:50:45 -0500245InstancedRendering::Op::~Op() {
csmartdaltona7f29642016-07-07 08:49:11 -0700246 if (fIsTracked) {
Brian Salomon99ad1642016-12-16 09:50:45 -0500247 fInstancedRendering->fTrackedOps.remove(this);
csmartdaltona7f29642016-07-07 08:49:11 -0700248 }
249
250 Draw* draw = fHeadDraw;
251 while (draw) {
252 Draw* next = draw->fNext;
253 fInstancedRendering->fDrawPool.release(draw);
254 draw = next;
255 }
256}
257
Brian Salomon99ad1642016-12-16 09:50:45 -0500258void InstancedRendering::Op::appendRRectParams(const SkRRect& rrect) {
csmartdaltona7f29642016-07-07 08:49:11 -0700259 SkASSERT(!fIsTracked);
260 switch (rrect.getType()) {
261 case SkRRect::kSimple_Type: {
262 const SkVector& radii = rrect.getSimpleRadii();
263 this->appendParamsTexel(radii.x(), radii.y(), rrect.width(), rrect.height());
264 return;
265 }
266 case SkRRect::kNinePatch_Type: {
267 float twoOverW = 2 / rrect.width();
268 float twoOverH = 2 / rrect.height();
269 const SkVector& radiiTL = rrect.radii(SkRRect::kUpperLeft_Corner);
270 const SkVector& radiiBR = rrect.radii(SkRRect::kLowerRight_Corner);
271 this->appendParamsTexel(radiiTL.x() * twoOverW, radiiBR.x() * twoOverW,
272 radiiTL.y() * twoOverH, radiiBR.y() * twoOverH);
273 return;
274 }
275 case SkRRect::kComplex_Type: {
276 /**
277 * The x and y radii of each arc are stored in separate vectors,
278 * in the following order:
279 *
280 * __x1 _ _ _ x3__
281 * y1 | | y2
282 *
283 * | |
284 *
285 * y3 |__ _ _ _ __| y4
286 * x2 x4
287 *
288 */
289 float twoOverW = 2 / rrect.width();
290 float twoOverH = 2 / rrect.height();
291 const SkVector& radiiTL = rrect.radii(SkRRect::kUpperLeft_Corner);
292 const SkVector& radiiTR = rrect.radii(SkRRect::kUpperRight_Corner);
293 const SkVector& radiiBR = rrect.radii(SkRRect::kLowerRight_Corner);
294 const SkVector& radiiBL = rrect.radii(SkRRect::kLowerLeft_Corner);
295 this->appendParamsTexel(radiiTL.x() * twoOverW, radiiBL.x() * twoOverW,
296 radiiTR.x() * twoOverW, radiiBR.x() * twoOverW);
297 this->appendParamsTexel(radiiTL.y() * twoOverH, radiiTR.y() * twoOverH,
298 radiiBL.y() * twoOverH, radiiBR.y() * twoOverH);
299 return;
300 }
301 default: return;
302 }
303}
304
Brian Salomon99ad1642016-12-16 09:50:45 -0500305void InstancedRendering::Op::appendParamsTexel(const SkScalar* vals, int count) {
csmartdaltona7f29642016-07-07 08:49:11 -0700306 SkASSERT(!fIsTracked);
307 SkASSERT(count <= 4 && count >= 0);
308 const float* valsAsFloats = vals; // Ensure SkScalar == float.
309 memcpy(&fParams.push_back(), valsAsFloats, count * sizeof(float));
310 fInfo.fHasParams = true;
311}
312
Brian Salomon99ad1642016-12-16 09:50:45 -0500313void InstancedRendering::Op::appendParamsTexel(SkScalar x, SkScalar y, SkScalar z, SkScalar w) {
csmartdaltona7f29642016-07-07 08:49:11 -0700314 SkASSERT(!fIsTracked);
315 ParamsTexel& texel = fParams.push_back();
316 texel.fX = SkScalarToFloat(x);
317 texel.fY = SkScalarToFloat(y);
318 texel.fZ = SkScalarToFloat(z);
319 texel.fW = SkScalarToFloat(w);
320 fInfo.fHasParams = true;
321}
322
Brian Salomon99ad1642016-12-16 09:50:45 -0500323void InstancedRendering::Op::appendParamsTexel(SkScalar x, SkScalar y, SkScalar z) {
csmartdaltona7f29642016-07-07 08:49:11 -0700324 SkASSERT(!fIsTracked);
325 ParamsTexel& texel = fParams.push_back();
326 texel.fX = SkScalarToFloat(x);
327 texel.fY = SkScalarToFloat(y);
328 texel.fZ = SkScalarToFloat(z);
329 fInfo.fHasParams = true;
330}
331
Brian Salomon5298dc82017-02-22 11:52:03 -0500332void InstancedRendering::Op::getFragmentProcessorAnalysisInputs(
333 FragmentProcessorAnalysisInputs* input) const {
334 input->colorInput()->setToConstant(this->getSingleInstance().fColor);
csmartdaltona7f29642016-07-07 08:49:11 -0700335
Brian Salomonaf9847e2017-03-01 11:28:27 -0500336 if (GrAAType::kCoverage == fInfo.aaType() ||
337 (GrAAType::kNone == fInfo.aaType() && !fInfo.isSimpleRects() && fInfo.fCannotDiscard)) {
Brian Salomon5298dc82017-02-22 11:52:03 -0500338 input->coverageInput()->setToUnknown();
csmartdaltona7f29642016-07-07 08:49:11 -0700339 } else {
Brian Salomon5298dc82017-02-22 11:52:03 -0500340 input->coverageInput()->setToSolidCoverage();
csmartdaltona7f29642016-07-07 08:49:11 -0700341 }
342}
343
Brian Salomon92aee3d2016-12-21 09:20:25 -0500344void InstancedRendering::Op::applyPipelineOptimizations(
345 const GrPipelineOptimizations& optimizations) {
csmartdaltona7f29642016-07-07 08:49:11 -0700346 Draw& draw = this->getSingleDraw(); // This will assert if we have > 1 command.
347 SkASSERT(draw.fGeometry.isEmpty());
348 SkASSERT(SkIsPow2(fInfo.fShapeTypes));
349 SkASSERT(!fIsTracked);
350
351 if (kRect_ShapeFlag == fInfo.fShapeTypes) {
Brian Salomonaf9847e2017-03-01 11:28:27 -0500352 draw.fGeometry = InstanceProcessor::GetIndexRangeForRect(fInfo.aaType());
csmartdaltona7f29642016-07-07 08:49:11 -0700353 } else if (kOval_ShapeFlag == fInfo.fShapeTypes) {
Brian Salomonaf9847e2017-03-01 11:28:27 -0500354 draw.fGeometry = InstanceProcessor::GetIndexRangeForOval(fInfo.aaType(), this->bounds());
csmartdaltona7f29642016-07-07 08:49:11 -0700355 } else {
Brian Salomonaf9847e2017-03-01 11:28:27 -0500356 draw.fGeometry = InstanceProcessor::GetIndexRangeForRRect(fInfo.aaType());
csmartdaltona7f29642016-07-07 08:49:11 -0700357 }
358
359 if (!fParams.empty()) {
360 SkASSERT(fInstancedRendering->fParams.count() < (int)kParamsIdx_InfoMask); // TODO: cleaner.
361 this->getSingleInstance().fInfo |= fInstancedRendering->fParams.count();
362 fInstancedRendering->fParams.push_back_n(fParams.count(), fParams.begin());
363 }
364
365 GrColor overrideColor;
Brian Salomon92aee3d2016-12-21 09:20:25 -0500366 if (optimizations.getOverrideColorIfSet(&overrideColor)) {
csmartdaltona7f29642016-07-07 08:49:11 -0700367 SkASSERT(State::kRecordingDraws == fInstancedRendering->fState);
368 this->getSingleInstance().fColor = overrideColor;
369 }
Brian Salomon92aee3d2016-12-21 09:20:25 -0500370 fInfo.fUsesLocalCoords = optimizations.readsLocalCoords();
371 fInfo.fCannotTweakAlphaForCoverage = !optimizations.canTweakAlphaForCoverage();
csmartdaltona7f29642016-07-07 08:49:11 -0700372
Brian Salomon99ad1642016-12-16 09:50:45 -0500373 fInstancedRendering->fTrackedOps.addToTail(this);
csmartdaltona7f29642016-07-07 08:49:11 -0700374 fIsTracked = true;
375}
376
Brian Salomon99ad1642016-12-16 09:50:45 -0500377bool InstancedRendering::Op::onCombineIfPossible(GrOp* other, const GrCaps& caps) {
378 Op* that = static_cast<Op*>(other);
csmartdaltona7f29642016-07-07 08:49:11 -0700379 SkASSERT(fInstancedRendering == that->fInstancedRendering);
380 SkASSERT(fTailDraw);
381 SkASSERT(that->fTailDraw);
382
Brian Salomon99ad1642016-12-16 09:50:45 -0500383 if (!OpInfo::CanCombine(fInfo, that->fInfo) ||
384 !GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
385 that->bounds(), caps)) {
csmartdaltona7f29642016-07-07 08:49:11 -0700386 return false;
387 }
388
Brian Salomon99ad1642016-12-16 09:50:45 -0500389 OpInfo combinedInfo = fInfo | that->fInfo;
csmartdaltona7f29642016-07-07 08:49:11 -0700390 if (!combinedInfo.isSimpleRects()) {
391 // This threshold was chosen with the "shapes_mixed" bench on a MacBook with Intel graphics.
392 // There seems to be a wide range where it doesn't matter if we combine or not. What matters
393 // is that the itty bitty rects combine with other shapes and the giant ones don't.
394 constexpr SkScalar kMaxPixelsToGeneralizeRects = 256 * 256;
395 if (fInfo.isSimpleRects() && fPixelLoad > kMaxPixelsToGeneralizeRects) {
396 return false;
397 }
398 if (that->fInfo.isSimpleRects() && that->fPixelLoad > kMaxPixelsToGeneralizeRects) {
399 return false;
400 }
401 }
402
bsalomon88cf17d2016-07-08 06:40:56 -0700403 this->joinBounds(*that);
csmartdaltona7f29642016-07-07 08:49:11 -0700404 fInfo = combinedInfo;
405 fPixelLoad += that->fPixelLoad;
406
Brian Salomon99ad1642016-12-16 09:50:45 -0500407 // Adopt the other op's draws.
csmartdaltona7f29642016-07-07 08:49:11 -0700408 fNumDraws += that->fNumDraws;
409 fNumChangesInGeometry += that->fNumChangesInGeometry;
410 if (fTailDraw->fGeometry != that->fHeadDraw->fGeometry) {
411 ++fNumChangesInGeometry;
412 }
413 fTailDraw->fNext = that->fHeadDraw;
414 fTailDraw = that->fTailDraw;
415
416 that->fHeadDraw = that->fTailDraw = nullptr;
417
418 return true;
419}
420
421void InstancedRendering::beginFlush(GrResourceProvider* rp) {
422 SkASSERT(State::kRecordingDraws == fState);
423 fState = State::kFlushing;
424
Brian Salomon99ad1642016-12-16 09:50:45 -0500425 if (fTrackedOps.isEmpty()) {
csmartdaltona7f29642016-07-07 08:49:11 -0700426 return;
427 }
428
429 if (!fVertexBuffer) {
Hal Canary144caf52016-11-07 17:57:18 -0500430 fVertexBuffer.reset(InstanceProcessor::FindOrCreateVertexBuffer(fGpu.get()));
csmartdaltona7f29642016-07-07 08:49:11 -0700431 if (!fVertexBuffer) {
432 return;
433 }
434 }
435
436 if (!fIndexBuffer) {
Hal Canary144caf52016-11-07 17:57:18 -0500437 fIndexBuffer.reset(InstanceProcessor::FindOrCreateIndex8Buffer(fGpu.get()));
csmartdaltona7f29642016-07-07 08:49:11 -0700438 if (!fIndexBuffer) {
439 return;
440 }
441 }
442
443 if (!fParams.empty()) {
444 fParamsBuffer.reset(rp->createBuffer(fParams.count() * sizeof(ParamsTexel),
445 kTexel_GrBufferType, kDynamic_GrAccessPattern,
csmartdalton485a1202016-07-13 10:16:32 -0700446 GrResourceProvider::kNoPendingIO_Flag |
447 GrResourceProvider::kRequireGpuMemory_Flag,
csmartdaltona7f29642016-07-07 08:49:11 -0700448 fParams.begin()));
449 if (!fParamsBuffer) {
450 return;
451 }
452 }
453
454 this->onBeginFlush(rp);
455}
456
Brian Salomon9e50f7b2017-03-06 12:02:34 -0500457void InstancedRendering::Op::onExecute(GrOpFlushState* state) {
csmartdaltona7f29642016-07-07 08:49:11 -0700458 SkASSERT(State::kFlushing == fInstancedRendering->fState);
459 SkASSERT(state->gpu() == fInstancedRendering->gpu());
460
461 state->gpu()->handleDirtyContext();
462 if (GrXferBarrierType barrierType = this->pipeline()->xferBarrierType(*state->gpu()->caps())) {
463 state->gpu()->xferBarrier(this->pipeline()->getRenderTarget(), barrierType);
464 }
465
Hal Canary144caf52016-11-07 17:57:18 -0500466 InstanceProcessor instProc(fInfo, fInstancedRendering->fParamsBuffer.get());
csmartdaltona7f29642016-07-07 08:49:11 -0700467 fInstancedRendering->onDraw(*this->pipeline(), instProc, this);
468}
469
470void InstancedRendering::endFlush() {
Brian Salomon92aee3d2016-12-21 09:20:25 -0500471 // The caller is expected to delete all tracked ops (i.e. ops whose applyPipelineOptimizations
csmartdaltona7f29642016-07-07 08:49:11 -0700472 // method has been called) before ending the flush.
Brian Salomon99ad1642016-12-16 09:50:45 -0500473 SkASSERT(fTrackedOps.isEmpty());
csmartdaltona7f29642016-07-07 08:49:11 -0700474 fParams.reset();
475 fParamsBuffer.reset();
476 this->onEndFlush();
477 fState = State::kRecordingDraws;
478 // Hold on to the shape coords and index buffers.
479}
480
481void InstancedRendering::resetGpuResources(ResetType resetType) {
482 fVertexBuffer.reset();
483 fIndexBuffer.reset();
484 fParamsBuffer.reset();
485 this->onResetGpuResources(resetType);
486}
487
488}