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 | |
Brian Salomon | 82c263f | 2016-12-15 09:54:06 -0500 | [diff] [blame] | 8 | #include "GrDrawPathOp.h" |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 9 | |
cdalton | 193d9cf | 2016-05-12 11:52:02 -0700 | [diff] [blame] | 10 | #include "GrRenderTargetPriv.h" |
| 11 | |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 12 | static void pre_translate_transform_values(const float* xforms, |
| 13 | GrPathRendering::PathTransformType type, int count, |
| 14 | SkScalar x, SkScalar y, float* dst); |
| 15 | |
Brian Salomon | 82c263f | 2016-12-15 09:54:06 -0500 | [diff] [blame] | 16 | void GrDrawPathOpBase::onPrepare(GrOpFlushState*) { |
cdalton | 193d9cf | 2016-05-12 11:52:02 -0700 | [diff] [blame] | 17 | const GrRenderTargetPriv& rtPriv = this->pipeline()->getRenderTarget()->renderTargetPriv(); |
| 18 | fStencilPassSettings.reset(GrPathRendering::GetStencilPassSettings(fFillType), |
| 19 | this->pipeline()->hasStencilClip(), rtPriv.numStencilBits()); |
| 20 | } |
| 21 | |
Brian Salomon | 82c263f | 2016-12-15 09:54:06 -0500 | [diff] [blame] | 22 | SkString GrDrawPathOp::dumpInfo() const { |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 23 | SkString string; |
stephana | 1dc1721 | 2016-04-25 07:01:22 -0700 | [diff] [blame] | 24 | string.printf("PATH: 0x%p", fPath.get()); |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 25 | string.append(DumpPipelineInfo(*this->pipeline())); |
robertphillips | 44fbc79 | 2016-06-29 06:56:12 -0700 | [diff] [blame] | 26 | string.append(INHERITED::dumpInfo()); |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 27 | return string; |
| 28 | } |
| 29 | |
Brian Salomon | bde4285 | 2016-12-21 11:37:49 -0500 | [diff] [blame] | 30 | void GrDrawPathOp::onExecute(GrOpFlushState* state, const SkRect& bounds) { |
Brian Salomon | 82c263f | 2016-12-15 09:54:06 -0500 | [diff] [blame] | 31 | GrProgramDesc desc; |
stephana | 1dc1721 | 2016-04-25 07:01:22 -0700 | [diff] [blame] | 32 | |
Brian Salomon | 82c263f | 2016-12-15 09:54:06 -0500 | [diff] [blame] | 33 | sk_sp<GrPathProcessor> pathProc( |
Brian Salomon | ecea86a | 2017-01-04 13:25:17 -0500 | [diff] [blame] | 34 | GrPathProcessor::Create(this->color(), this->viewMatrix())); |
cdalton | 193d9cf | 2016-05-12 11:52:02 -0700 | [diff] [blame] | 35 | state->gpu()->pathRendering()->drawPath(*this->pipeline(), *pathProc, |
| 36 | this->stencilPassSettings(), fPath.get()); |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Brian Salomon | 82c263f | 2016-12-15 09:54:06 -0500 | [diff] [blame] | 39 | SkString GrDrawPathRangeOp::dumpInfo() const { |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 40 | SkString string; |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 41 | string.printf("RANGE: 0x%p COUNTS: [", fPathRange.get()); |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 42 | for (DrawList::Iter iter(fDraws); iter.get(); iter.next()) { |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 43 | string.appendf("%d, ", iter.get()->fInstanceData->count()); |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 44 | } |
| 45 | string.remove(string.size() - 2, 2); |
| 46 | string.append("]"); |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 47 | string.append(DumpPipelineInfo(*this->pipeline())); |
robertphillips | 44fbc79 | 2016-06-29 06:56:12 -0700 | [diff] [blame] | 48 | string.append(INHERITED::dumpInfo()); |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 49 | return string; |
| 50 | } |
| 51 | |
Brian Salomon | 82c263f | 2016-12-15 09:54:06 -0500 | [diff] [blame] | 52 | GrDrawPathRangeOp::GrDrawPathRangeOp(const SkMatrix& viewMatrix, SkScalar scale, SkScalar x, |
| 53 | SkScalar y, GrColor color, GrPathRendering::FillType fill, |
| 54 | GrPathRange* range, const InstanceData* instanceData, |
| 55 | const SkRect& bounds) |
| 56 | : INHERITED(ClassID(), viewMatrix, color, fill) |
| 57 | , fPathRange(range) |
| 58 | , fTotalPathCount(instanceData->count()) |
| 59 | , fScale(scale) { |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 60 | fDraws.addToHead()->set(instanceData, x, y); |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 61 | this->setBounds(bounds, HasAABloat::kNo, IsZeroArea::kNo); |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Brian Salomon | 82c263f | 2016-12-15 09:54:06 -0500 | [diff] [blame] | 64 | bool GrDrawPathRangeOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) { |
| 65 | GrDrawPathRangeOp* that = t->cast<GrDrawPathRangeOp>(); |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 66 | if (this->fPathRange.get() != that->fPathRange.get() || |
Brian Salomon | 82c263f | 2016-12-15 09:54:06 -0500 | [diff] [blame] | 67 | this->transformType() != that->transformType() || this->fScale != that->fScale || |
| 68 | this->color() != that->color() || !this->viewMatrix().cheapEqualTo(that->viewMatrix())) { |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 69 | return false; |
| 70 | } |
bsalomon | 7312ff8 | 2016-09-12 08:55:38 -0700 | [diff] [blame] | 71 | if (!GrPipeline::AreEqual(*this->pipeline(), *that->pipeline())) { |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 72 | return false; |
| 73 | } |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 74 | switch (fDraws.head()->fInstanceData->transformType()) { |
| 75 | case GrPathRendering::kNone_PathTransformType: |
| 76 | if (this->fDraws.head()->fX != that->fDraws.head()->fX || |
| 77 | this->fDraws.head()->fY != that->fDraws.head()->fY) { |
| 78 | return false; |
| 79 | } |
| 80 | break; |
| 81 | case GrPathRendering::kTranslateX_PathTransformType: |
| 82 | if (this->fDraws.head()->fY != that->fDraws.head()->fY) { |
| 83 | return false; |
| 84 | } |
| 85 | break; |
| 86 | case GrPathRendering::kTranslateY_PathTransformType: |
| 87 | if (this->fDraws.head()->fX != that->fDraws.head()->fX) { |
| 88 | return false; |
| 89 | } |
| 90 | break; |
Brian Salomon | 82c263f | 2016-12-15 09:54:06 -0500 | [diff] [blame] | 91 | default: |
| 92 | break; |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 93 | } |
| 94 | // TODO: Check some other things here. (winding, opaque, pathProc color, vm, ...) |
| 95 | // Try to combine this call with the previous DrawPaths. We do this by stenciling all the |
| 96 | // paths together and then covering them in a single pass. This is not equivalent to two |
| 97 | // separate draw calls, so we can only do it if there is no blending (no overlap would also |
| 98 | // work). Note that it's also possible for overlapping paths to cancel each other's winding |
| 99 | // numbers, and we only partially account for this by not allowing even/odd paths to be |
| 100 | // combined. (Glyphs in the same font tend to wind the same direction so it works out OK.) |
cdalton | 8ff8d24 | 2015-12-08 10:20:32 -0800 | [diff] [blame] | 101 | if (GrPathRendering::kWinding_FillType != this->fillType() || |
cdalton | 193d9cf | 2016-05-12 11:52:02 -0700 | [diff] [blame] | 102 | GrPathRendering::kWinding_FillType != that->fillType() || |
Brian Salomon | ecea86a | 2017-01-04 13:25:17 -0500 | [diff] [blame] | 103 | this->blendsWithDst()) { |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 104 | return false; |
| 105 | } |
Brian Salomon | ecea86a | 2017-01-04 13:25:17 -0500 | [diff] [blame] | 106 | SkASSERT(!that->blendsWithDst()); |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 107 | fTotalPathCount += that->fTotalPathCount; |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 108 | while (Draw* head = that->fDraws.head()) { |
| 109 | Draw* draw = fDraws.addToTail(); |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 110 | draw->fInstanceData.reset(head->fInstanceData.release()); |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 111 | draw->fX = head->fX; |
| 112 | draw->fY = head->fY; |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 113 | that->fDraws.popHead(); |
| 114 | } |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 115 | this->joinBounds(*that); |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 116 | return true; |
| 117 | } |
| 118 | |
Brian Salomon | bde4285 | 2016-12-21 11:37:49 -0500 | [diff] [blame] | 119 | void GrDrawPathRangeOp::onExecute(GrOpFlushState* state, const SkRect& bounds) { |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 120 | const Draw& head = *fDraws.head(); |
| 121 | |
| 122 | SkMatrix drawMatrix(this->viewMatrix()); |
| 123 | drawMatrix.preScale(fScale, fScale); |
| 124 | drawMatrix.preTranslate(head.fX, head.fY); |
| 125 | |
| 126 | SkMatrix localMatrix; |
| 127 | localMatrix.setScale(fScale, fScale); |
| 128 | localMatrix.preTranslate(head.fX, head.fY); |
| 129 | |
Brian Salomon | 82c263f | 2016-12-15 09:54:06 -0500 | [diff] [blame] | 130 | sk_sp<GrPathProcessor> pathProc( |
Brian Salomon | ecea86a | 2017-01-04 13:25:17 -0500 | [diff] [blame] | 131 | GrPathProcessor::Create(this->color(), drawMatrix, localMatrix)); |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 132 | |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 133 | if (fDraws.count() == 1) { |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 134 | const InstanceData& instances = *head.fInstanceData; |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 135 | state->gpu()->pathRendering()->drawPaths(*this->pipeline(), |
| 136 | *pathProc, |
cdalton | 193d9cf | 2016-05-12 11:52:02 -0700 | [diff] [blame] | 137 | this->stencilPassSettings(), |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 138 | fPathRange.get(), |
| 139 | instances.indices(), |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 140 | GrPathRange::kU16_PathIndexType, |
| 141 | instances.transformValues(), |
| 142 | instances.transformType(), |
| 143 | instances.count()); |
| 144 | } else { |
| 145 | int floatsPerTransform = GrPathRendering::PathTransformSize(this->transformType()); |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 146 | SkAutoSTMalloc<4096, float> transformStorage(floatsPerTransform * fTotalPathCount); |
| 147 | SkAutoSTMalloc<2048, uint16_t> indexStorage(fTotalPathCount); |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 148 | int idx = 0; |
| 149 | for (DrawList::Iter iter(fDraws); iter.get(); iter.next()) { |
| 150 | const Draw& draw = *iter.get(); |
| 151 | const InstanceData& instances = *draw.fInstanceData; |
| 152 | memcpy(&indexStorage[idx], instances.indices(), instances.count() * sizeof(uint16_t)); |
| 153 | pre_translate_transform_values(instances.transformValues(), this->transformType(), |
Brian Salomon | 82c263f | 2016-12-15 09:54:06 -0500 | [diff] [blame] | 154 | instances.count(), draw.fX - head.fX, draw.fY - head.fY, |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 155 | &transformStorage[floatsPerTransform * idx]); |
| 156 | idx += instances.count(); |
| 157 | |
| 158 | // TODO: Support mismatched transform types if we start using more types other than 2D. |
| 159 | SkASSERT(instances.transformType() == this->transformType()); |
| 160 | } |
| 161 | SkASSERT(idx == fTotalPathCount); |
| 162 | |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 163 | state->gpu()->pathRendering()->drawPaths(*this->pipeline(), |
| 164 | *pathProc, |
cdalton | 193d9cf | 2016-05-12 11:52:02 -0700 | [diff] [blame] | 165 | this->stencilPassSettings(), |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 166 | fPathRange.get(), |
| 167 | indexStorage, |
| 168 | GrPathRange::kU16_PathIndexType, |
| 169 | transformStorage, |
| 170 | this->transformType(), |
| 171 | fTotalPathCount); |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
| 175 | inline void pre_translate_transform_values(const float* xforms, |
| 176 | GrPathRendering::PathTransformType type, int count, |
| 177 | SkScalar x, SkScalar y, float* dst) { |
| 178 | if (0 == x && 0 == y) { |
| 179 | memcpy(dst, xforms, count * GrPathRendering::PathTransformSize(type) * sizeof(float)); |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 180 | return; |
| 181 | } |
cdalton | cdd4682 | 2015-12-08 10:48:31 -0800 | [diff] [blame] | 182 | switch (type) { |
| 183 | case GrPathRendering::kNone_PathTransformType: |
| 184 | SkFAIL("Cannot pre-translate kNone_PathTransformType."); |
| 185 | break; |
| 186 | case GrPathRendering::kTranslateX_PathTransformType: |
| 187 | SkASSERT(0 == y); |
| 188 | for (int i = 0; i < count; i++) { |
| 189 | dst[i] = xforms[i] + x; |
| 190 | } |
| 191 | break; |
| 192 | case GrPathRendering::kTranslateY_PathTransformType: |
| 193 | SkASSERT(0 == x); |
| 194 | for (int i = 0; i < count; i++) { |
| 195 | dst[i] = xforms[i] + y; |
| 196 | } |
| 197 | break; |
| 198 | case GrPathRendering::kTranslate_PathTransformType: |
| 199 | for (int i = 0; i < 2 * count; i += 2) { |
| 200 | dst[i] = xforms[i] + x; |
| 201 | dst[i + 1] = xforms[i + 1] + y; |
| 202 | } |
| 203 | break; |
| 204 | case GrPathRendering::kAffine_PathTransformType: |
| 205 | for (int i = 0; i < 6 * count; i += 6) { |
| 206 | dst[i] = xforms[i]; |
| 207 | dst[i + 1] = xforms[i + 1]; |
| 208 | dst[i + 2] = xforms[i] * x + xforms[i + 1] * y + xforms[i + 2]; |
| 209 | dst[i + 3] = xforms[i + 3]; |
| 210 | dst[i + 4] = xforms[i + 4]; |
| 211 | dst[i + 5] = xforms[i + 3] * x + xforms[i + 4] * y + xforms[i + 5]; |
| 212 | } |
| 213 | break; |
| 214 | default: |
| 215 | SkFAIL("Unknown transform type."); |
| 216 | break; |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 217 | } |
bsalomon | 1fcc01c | 2015-09-09 09:48:06 -0700 | [diff] [blame] | 218 | } |