joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [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 | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 8 | #include "GrLatticeOp.h" |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 9 | |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 10 | #include "GrDefaultGeoProcFactory.h" |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame] | 11 | #include "GrMeshDrawOp.h" |
Brian Salomon | 742e31d | 2016-12-07 17:06:19 -0500 | [diff] [blame] | 12 | #include "GrOpFlushState.h" |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 13 | #include "GrResourceProvider.h" |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 14 | #include "SkBitmap.h" |
msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 15 | #include "SkLatticeIter.h" |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 16 | #include "SkRect.h" |
| 17 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 18 | static sk_sp<GrGeometryProcessor> create_gp(bool readsCoverage) { |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 19 | using namespace GrDefaultGeoProcFactory; |
| 20 | Color color(Color::kAttribute_Type); |
| 21 | Coverage coverage(readsCoverage ? Coverage::kSolid_Type : Coverage::kNone_Type); |
| 22 | LocalCoords localCoords(LocalCoords::kHasExplicit_Type); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 23 | return GrDefaultGeoProcFactory::Make(color, coverage, localCoords, SkMatrix::I()); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 24 | } |
| 25 | |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 26 | class NonAALatticeOp final : public GrMeshDrawOp { |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 27 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 28 | DEFINE_OP_CLASS_ID |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 29 | |
| 30 | static const int kVertsPerRect = 4; |
| 31 | static const int kIndicesPerRect = 6; |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 32 | |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 33 | NonAALatticeOp(GrColor color, const SkMatrix& viewMatrix, int imageWidth, int imageHeight, |
| 34 | std::unique_ptr<SkLatticeIter> iter, const SkRect& dst) |
| 35 | : INHERITED(ClassID()) { |
bsalomon | a71b898 | 2016-06-30 12:13:52 -0700 | [diff] [blame] | 36 | Patch& patch = fPatches.push_back(); |
| 37 | patch.fViewMatrix = viewMatrix; |
| 38 | patch.fColor = color; |
msarett | 10e3d9b | 2016-08-18 15:46:03 -0700 | [diff] [blame] | 39 | patch.fIter = std::move(iter); |
bsalomon | a71b898 | 2016-06-30 12:13:52 -0700 | [diff] [blame] | 40 | patch.fDst = dst; |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 41 | |
| 42 | fImageWidth = imageWidth; |
| 43 | fImageHeight = imageHeight; |
| 44 | |
| 45 | // setup bounds |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 46 | this->setTransformedBounds(patch.fDst, viewMatrix, HasAABloat::kNo, IsZeroArea::kNo); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 49 | const char* name() const override { return "NonAALatticeOp"; } |
robertphillips | 783a4da | 2015-11-19 14:00:02 -0800 | [diff] [blame] | 50 | |
| 51 | SkString dumpInfo() const override { |
| 52 | SkString str; |
| 53 | |
bsalomon | a71b898 | 2016-06-30 12:13:52 -0700 | [diff] [blame] | 54 | for (int i = 0; i < fPatches.count(); ++i) { |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 55 | str.appendf("%d: Color: 0x%08x Dst [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", i, |
| 56 | fPatches[i].fColor, fPatches[i].fDst.fLeft, fPatches[i].fDst.fTop, |
bsalomon | a71b898 | 2016-06-30 12:13:52 -0700 | [diff] [blame] | 57 | fPatches[i].fDst.fRight, fPatches[i].fDst.fBottom); |
robertphillips | 783a4da | 2015-11-19 14:00:02 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 60 | str.append(DumpPipelineInfo(*this->pipeline())); |
robertphillips | 783a4da | 2015-11-19 14:00:02 -0800 | [diff] [blame] | 61 | str.append(INHERITED::dumpInfo()); |
| 62 | return str; |
| 63 | } |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 64 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 65 | private: |
| 66 | void getPipelineAnalysisInput(GrPipelineAnalysisDrawOpInput* input) const override { |
| 67 | input->pipelineColorInput()->setUnknownFourComponents(); |
| 68 | input->pipelineCoverageInput()->setKnownSingleComponent(0xff); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 71 | void applyPipelineOptimizations(const GrPipelineOptimizations& analysioptimizations) override { |
| 72 | analysioptimizations.getOverrideColorIfSet(&fPatches[0].fColor); |
| 73 | fOptimizations = analysioptimizations; |
| 74 | } |
| 75 | |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 76 | void onPrepareDraws(Target* target) const override { |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 77 | sk_sp<GrGeometryProcessor> gp(create_gp(fOptimizations.readsCoverage())); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 78 | if (!gp) { |
| 79 | SkDebugf("Couldn't create GrGeometryProcessor\n"); |
| 80 | return; |
| 81 | } |
| 82 | |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 83 | size_t vertexStride = gp->getVertexStride(); |
bsalomon | a71b898 | 2016-06-30 12:13:52 -0700 | [diff] [blame] | 84 | int patchCnt = fPatches.count(); |
msarett | 10e3d9b | 2016-08-18 15:46:03 -0700 | [diff] [blame] | 85 | int numRects = 0; |
| 86 | for (int i = 0; i < patchCnt; i++) { |
msarett | 0764efe | 2016-09-02 11:24:30 -0700 | [diff] [blame] | 87 | numRects += fPatches[i].fIter->numRectsToDraw(); |
msarett | 10e3d9b | 2016-08-18 15:46:03 -0700 | [diff] [blame] | 88 | } |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 89 | |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 90 | sk_sp<const GrBuffer> indexBuffer(target->resourceProvider()->refQuadIndexBuffer()); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 91 | InstancedHelper helper; |
| 92 | void* vertices = helper.init(target, kTriangles_GrPrimitiveType, vertexStride, |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 93 | indexBuffer.get(), kVertsPerRect, kIndicesPerRect, numRects); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 94 | if (!vertices || !indexBuffer) { |
| 95 | SkDebugf("Could not allocate vertices\n"); |
| 96 | return; |
| 97 | } |
| 98 | |
msarett | 10e3d9b | 2016-08-18 15:46:03 -0700 | [diff] [blame] | 99 | intptr_t verts = reinterpret_cast<intptr_t>(vertices); |
bsalomon | a71b898 | 2016-06-30 12:13:52 -0700 | [diff] [blame] | 100 | for (int i = 0; i < patchCnt; i++) { |
msarett | 7fc0858 | 2016-08-18 14:29:22 -0700 | [diff] [blame] | 101 | const Patch& patch = fPatches[i]; |
msarett | 10e3d9b | 2016-08-18 15:46:03 -0700 | [diff] [blame] | 102 | |
| 103 | // Apply the view matrix here if it is scale-translate. Otherwise, we need to |
| 104 | // wait until we've created the dst rects. |
| 105 | bool isScaleTranslate = patch.fViewMatrix.isScaleTranslate(); |
| 106 | if (isScaleTranslate) { |
| 107 | patch.fIter->mapDstScaleTranslate(patch.fViewMatrix); |
| 108 | } |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 109 | |
| 110 | SkRect srcR, dstR; |
msarett | 10e3d9b | 2016-08-18 15:46:03 -0700 | [diff] [blame] | 111 | intptr_t patchVerts = verts; |
| 112 | while (patch.fIter->next(&srcR, &dstR)) { |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 113 | SkPoint* positions = reinterpret_cast<SkPoint*>(verts); |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 114 | positions->setRectFan(dstR.fLeft, dstR.fTop, dstR.fRight, dstR.fBottom, |
| 115 | vertexStride); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 116 | |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 117 | // Setup local coords |
| 118 | static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor); |
| 119 | SkPoint* coords = reinterpret_cast<SkPoint*>(verts + kLocalOffset); |
| 120 | coords->setRectFan(srcR.fLeft, srcR.fTop, srcR.fRight, srcR.fBottom, vertexStride); |
| 121 | |
| 122 | static const int kColorOffset = sizeof(SkPoint); |
| 123 | GrColor* vertColor = reinterpret_cast<GrColor*>(verts + kColorOffset); |
| 124 | for (int j = 0; j < 4; ++j) { |
bsalomon | a71b898 | 2016-06-30 12:13:52 -0700 | [diff] [blame] | 125 | *vertColor = patch.fColor; |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 126 | vertColor = (GrColor*)((intptr_t)vertColor + vertexStride); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 127 | } |
| 128 | verts += kVertsPerRect * vertexStride; |
| 129 | } |
msarett | 10e3d9b | 2016-08-18 15:46:03 -0700 | [diff] [blame] | 130 | |
| 131 | // If we didn't handle it above, apply the matrix here. |
| 132 | if (!isScaleTranslate) { |
| 133 | SkPoint* positions = reinterpret_cast<SkPoint*>(patchVerts); |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 134 | patch.fViewMatrix.mapPointsWithStride( |
| 135 | positions, vertexStride, kVertsPerRect * patch.fIter->numRectsToDraw()); |
msarett | 10e3d9b | 2016-08-18 15:46:03 -0700 | [diff] [blame] | 136 | } |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 137 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 138 | helper.recordDraw(target, gp.get()); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 141 | bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override { |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 142 | NonAALatticeOp* that = t->cast<NonAALatticeOp>(); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 143 | if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(), |
| 144 | that->bounds(), caps)) { |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | SkASSERT(this->fImageWidth == that->fImageWidth && |
| 149 | this->fImageHeight == that->fImageHeight); |
| 150 | |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 151 | // In the event of two ops, one who can tweak, one who cannot, we just fall back to not |
| 152 | // tweaking. |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 153 | if (fOptimizations.canTweakAlphaForCoverage() && |
| 154 | !that->fOptimizations.canTweakAlphaForCoverage()) { |
| 155 | fOptimizations = that->fOptimizations; |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 156 | } |
| 157 | |
msarett | 10e3d9b | 2016-08-18 15:46:03 -0700 | [diff] [blame] | 158 | fPatches.move_back_n(that->fPatches.count(), that->fPatches.begin()); |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 159 | this->joinBounds(*that); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 160 | return true; |
| 161 | } |
| 162 | |
bsalomon | a71b898 | 2016-06-30 12:13:52 -0700 | [diff] [blame] | 163 | struct Patch { |
| 164 | SkMatrix fViewMatrix; |
msarett | 10e3d9b | 2016-08-18 15:46:03 -0700 | [diff] [blame] | 165 | std::unique_ptr<SkLatticeIter> fIter; |
bsalomon | a71b898 | 2016-06-30 12:13:52 -0700 | [diff] [blame] | 166 | SkRect fDst; |
| 167 | GrColor fColor; |
| 168 | }; |
| 169 | |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 170 | GrPipelineOptimizations fOptimizations; |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 171 | int fImageWidth; |
| 172 | int fImageHeight; |
bsalomon | a71b898 | 2016-06-30 12:13:52 -0700 | [diff] [blame] | 173 | SkSTArray<1, Patch, true> fPatches; |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 174 | |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame] | 175 | typedef GrMeshDrawOp INHERITED; |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 176 | }; |
| 177 | |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 178 | namespace GrLatticeOp { |
Brian Salomon | f833478 | 2017-01-03 09:42:58 -0500 | [diff] [blame^] | 179 | std::unique_ptr<GrDrawOp> MakeNonAA(GrColor color, const SkMatrix& viewMatrix, int imageWidth, |
| 180 | int imageHeight, std::unique_ptr<SkLatticeIter> iter, |
| 181 | const SkRect& dst) { |
| 182 | return std::unique_ptr<GrDrawOp>( |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 183 | new NonAALatticeOp(color, viewMatrix, imageWidth, imageHeight, std::move(iter), dst)); |
joshualitt | 33a5fce | 2015-11-18 13:28:51 -0800 | [diff] [blame] | 184 | } |
| 185 | }; |