bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 8 | #include "GrDrawPathBatch.h" |
| 9 | |
| 10 | SkString GrDrawPathBatch::dumpInfo() const { |
| 11 | SkString string; |
| 12 | string.printf("PATH: 0x%p", fPath.get()); |
| 13 | return string; |
| 14 | } |
| 15 | |
| 16 | void GrDrawPathBatch::onDraw(GrBatchFlushState* state) { |
| 17 | GrProgramDesc desc; |
joshualitt | f238469 | 2015-09-10 11:00:51 -0700 | [diff] [blame] | 18 | |
| 19 | SkAutoTUnref<GrPathProcessor> pathProc(GrPathProcessor::Create(this->color(), |
| 20 | this->opts(), |
| 21 | this->viewMatrix())); |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 22 | state->gpu()->buildProgramDesc(&desc, *pathProc, *this->pipeline()); |
joshualitt | f238469 | 2015-09-10 11:00:51 -0700 | [diff] [blame] | 23 | GrPathRendering::DrawPathArgs args(pathProc, this->pipeline(), |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 24 | &desc, &this->stencilSettings()); |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 25 | state->gpu()->pathRendering()->drawPath(args, fPath.get()); |
| 26 | } |
| 27 | |
| 28 | GrDrawPathRangeBatch::~GrDrawPathRangeBatch() { |
| 29 | for (DrawList::Iter iter(fDraws); iter.get(); iter.next()) { |
| 30 | (*iter.get())->unref(); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | SkString GrDrawPathRangeBatch::dumpInfo() const { |
| 35 | SkString string; |
| 36 | string.printf("RANGE: 0x%p COUNTS: [", *fDraws.head()); |
| 37 | for (DrawList::Iter iter(fDraws); iter.get(); iter.next()) { |
| 38 | string.appendf("%d ,", (*iter.get())->count()); |
| 39 | } |
| 40 | string.remove(string.size() - 2, 2); |
| 41 | string.append("]"); |
| 42 | return string; |
| 43 | } |
| 44 | |
| 45 | bool GrDrawPathRangeBatch::isWinding() const { |
| 46 | static const GrStencilSettings::Face pathFace = GrStencilSettings::kFront_Face; |
| 47 | bool isWinding = kInvert_StencilOp != this->stencilSettings().passOp(pathFace); |
| 48 | if (isWinding) { |
| 49 | // Double check that it is in fact winding. |
| 50 | SkASSERT(kIncClamp_StencilOp == this->stencilSettings().passOp(pathFace)); |
| 51 | SkASSERT(kIncClamp_StencilOp == this->stencilSettings().failOp(pathFace)); |
| 52 | SkASSERT(0x1 != this->stencilSettings().writeMask(pathFace)); |
| 53 | SkASSERT(!this->stencilSettings().isTwoSided()); |
| 54 | } |
| 55 | return isWinding; |
| 56 | } |
| 57 | |
joshualitt | f238469 | 2015-09-10 11:00:51 -0700 | [diff] [blame] | 58 | GrDrawPathRangeBatch::GrDrawPathRangeBatch(const SkMatrix& viewMatrix, const SkMatrix& localMatrix, |
bsalomon | bf07455 | 2015-11-23 14:25:19 -0800 | [diff] [blame^] | 59 | GrColor color, GrPathRange* range, GrPathRangeDraw* draw, |
| 60 | const SkRect& bounds) |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 61 | : INHERITED(ClassID(), viewMatrix, color) |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 62 | , fPathRange(range) |
joshualitt | f238469 | 2015-09-10 11:00:51 -0700 | [diff] [blame] | 63 | , fLocalMatrix(localMatrix) { |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 64 | SkDEBUGCODE(draw->fUsedInBatch = true;) |
| 65 | fDraws.addToHead(SkRef(draw)); |
| 66 | fTotalPathCount = draw->count(); |
bsalomon | bf07455 | 2015-11-23 14:25:19 -0800 | [diff] [blame^] | 67 | fBounds = bounds; |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | bool GrDrawPathRangeBatch::onCombineIfPossible(GrBatch* t, const GrCaps& caps) { |
| 71 | GrDrawPathRangeBatch* that = t->cast<GrDrawPathRangeBatch>(); |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 72 | if (this->fPathRange.get() != that->fPathRange.get()) { |
| 73 | return false; |
| 74 | } |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 75 | if (!GrPathRangeDraw::CanMerge(**this->fDraws.head(), **that->fDraws.head())) { |
| 76 | return false; |
| 77 | } |
| 78 | if (!GrPipeline::AreEqual(*this->pipeline(), *that->pipeline(), false)) { |
| 79 | return false; |
| 80 | } |
joshualitt | f238469 | 2015-09-10 11:00:51 -0700 | [diff] [blame] | 81 | if (this->color() != that->color() || |
| 82 | !this->viewMatrix().cheapEqualTo(that->viewMatrix()) || |
| 83 | !fLocalMatrix.cheapEqualTo(that->fLocalMatrix)) { |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 84 | return false; |
| 85 | } |
| 86 | // TODO: Check some other things here. (winding, opaque, pathProc color, vm, ...) |
| 87 | // Try to combine this call with the previous DrawPaths. We do this by stenciling all the |
| 88 | // paths together and then covering them in a single pass. This is not equivalent to two |
| 89 | // separate draw calls, so we can only do it if there is no blending (no overlap would also |
| 90 | // work). Note that it's also possible for overlapping paths to cancel each other's winding |
| 91 | // numbers, and we only partially account for this by not allowing even/odd paths to be |
| 92 | // combined. (Glyphs in the same font tend to wind the same direction so it works out OK.) |
| 93 | if (!this->isWinding() || |
| 94 | this->stencilSettings() != that->stencilSettings() || |
| 95 | this->opts().willColorBlendWithDst()) { |
| 96 | return false; |
| 97 | } |
| 98 | SkASSERT(!that->opts().willColorBlendWithDst()); |
| 99 | fTotalPathCount += that->fTotalPathCount; |
| 100 | while (GrPathRangeDraw** head = that->fDraws.head()) { |
| 101 | fDraws.addToTail(*head); |
| 102 | // We're stealing that's refs, so pop without unreffing. |
| 103 | that->fDraws.popHead(); |
| 104 | } |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | void GrDrawPathRangeBatch::onDraw(GrBatchFlushState* state) { |
| 109 | GrProgramDesc desc; |
joshualitt | f238469 | 2015-09-10 11:00:51 -0700 | [diff] [blame] | 110 | SkAutoTUnref<GrPathProcessor> pathProc(GrPathProcessor::Create(this->color(), |
| 111 | this->opts(), |
| 112 | this->viewMatrix(), |
| 113 | fLocalMatrix)); |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 114 | state->gpu()->buildProgramDesc(&desc, *pathProc, *this->pipeline()); |
joshualitt | f238469 | 2015-09-10 11:00:51 -0700 | [diff] [blame] | 115 | GrPathRendering::DrawPathArgs args(pathProc, this->pipeline(), |
joshualitt | 465283c | 2015-09-11 08:19:35 -0700 | [diff] [blame] | 116 | &desc, &this->stencilSettings()); |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 117 | if (fDraws.count() == 1) { |
| 118 | const GrPathRangeDraw& draw = **fDraws.head(); |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 119 | state->gpu()->pathRendering()->drawPaths(args, fPathRange.get(), draw.indices(), |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 120 | GrPathRange::kU16_PathIndexType, draw.transforms(), draw.transformType(), |
| 121 | draw.count()); |
| 122 | return; |
| 123 | } |
| 124 | |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 125 | GrPathRendering::PathTransformType transformType = (*fDraws.head())->transformType(); |
| 126 | int floatsPerTransform = GrPathRendering::PathTransformSize(transformType); |
| 127 | SkAutoSTMalloc<512, float> transformStorage(floatsPerTransform * fTotalPathCount); |
| 128 | SkAutoSTMalloc<256, uint16_t> indexStorage(fTotalPathCount); |
| 129 | uint16_t* indices = indexStorage.get(); |
| 130 | float* transforms = transformStorage.get(); |
| 131 | for (DrawList::Iter iter(fDraws); iter.get(); iter.next()) { |
| 132 | SkASSERT((*iter.get())->transformType() == transformType); |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 133 | int cnt = (*iter.get())->count(); |
| 134 | memcpy(indices, (*iter.get())->indices(), cnt * sizeof(uint16_t)); |
| 135 | indices += cnt; |
| 136 | memcpy(transforms, (*iter.get())->transforms(), cnt * floatsPerTransform * sizeof(float)); |
| 137 | transforms += cnt * floatsPerTransform; |
| 138 | } |
| 139 | SkASSERT(indices - indexStorage.get() == fTotalPathCount); |
cdalton | 8585dd2 | 2015-10-08 08:04:09 -0700 | [diff] [blame] | 140 | state->gpu()->pathRendering()->drawPaths(args, fPathRange.get(), indexStorage.get(), |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 141 | GrPathRange::kU16_PathIndexType, transformStorage.get(), transformType, |
| 142 | fTotalPathCount); |
| 143 | } |