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