msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 8 | #include "GrRegionOp.h" |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 9 | |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame] | 10 | #include "GrDefaultGeoProcFactory.h" |
| 11 | #include "GrMeshDrawOp.h" |
Brian Salomon | 742e31d | 2016-12-07 17:06:19 -0500 | [diff] [blame] | 12 | #include "GrOpFlushState.h" |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 13 | #include "GrResourceProvider.h" |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 14 | #include "SkMatrixPriv.h" |
| 15 | #include "SkRegion.h" |
| 16 | |
| 17 | static const int kVertsPerInstance = 4; |
| 18 | static const int kIndicesPerInstance = 6; |
| 19 | |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame] | 20 | static sk_sp<GrGeometryProcessor> make_gp(const SkMatrix& viewMatrix) { |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 21 | using namespace GrDefaultGeoProcFactory; |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 22 | return GrDefaultGeoProcFactory::Make(Color::kPremulGrColorAttribute_Type, Coverage::kSolid_Type, |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame] | 23 | LocalCoords::kUsePosition_Type, viewMatrix); |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 24 | } |
| 25 | |
msarett | febb224 | 2016-08-26 12:49:27 -0700 | [diff] [blame] | 26 | static void tesselate_region(intptr_t vertices, |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 27 | size_t vertexStride, |
| 28 | GrColor color, |
| 29 | const SkRegion& region) { |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 30 | SkRegion::Iterator iter(region); |
| 31 | |
| 32 | intptr_t verts = vertices; |
| 33 | while (!iter.done()) { |
msarett | febb224 | 2016-08-26 12:49:27 -0700 | [diff] [blame] | 34 | SkRect rect = SkRect::Make(iter.rect()); |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 35 | SkPoint* position = (SkPoint*)verts; |
msarett | febb224 | 2016-08-26 12:49:27 -0700 | [diff] [blame] | 36 | position->setRectFan(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, vertexStride); |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 37 | |
| 38 | static const int kColorOffset = sizeof(SkPoint); |
| 39 | GrColor* vertColor = reinterpret_cast<GrColor*>(verts + kColorOffset); |
| 40 | for (int i = 0; i < kVertsPerInstance; i++) { |
| 41 | *vertColor = color; |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 42 | vertColor = (GrColor*)((intptr_t)vertColor + vertexStride); |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | verts += vertexStride * kVertsPerInstance; |
| 46 | iter.next(); |
| 47 | } |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 50 | class RegionOp final : public GrLegacyMeshDrawOp { |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 51 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 52 | DEFINE_OP_CLASS_ID |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 53 | |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 54 | RegionOp(GrColor color, const SkMatrix& viewMatrix, const SkRegion& region) |
| 55 | : INHERITED(ClassID()), fViewMatrix(viewMatrix) { |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 56 | RegionInfo& info = fRegions.push_back(); |
| 57 | info.fColor = color; |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 58 | info.fRegion = region; |
| 59 | |
| 60 | SkRect bounds = SkRect::Make(region.getBounds()); |
| 61 | this->setTransformedBounds(bounds, viewMatrix, HasAABloat::kNo, IsZeroArea::kNo); |
| 62 | } |
| 63 | |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 64 | const char* name() const override { return "GrRegionOp"; } |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 65 | |
| 66 | SkString dumpInfo() const override { |
| 67 | SkString str; |
Brian Salomon | 53e4c3c | 2016-12-21 11:38:53 -0500 | [diff] [blame] | 68 | str.appendf("# combined: %d\n", fRegions.count()); |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 69 | for (int i = 0; i < fRegions.count(); ++i) { |
| 70 | const RegionInfo& info = fRegions[i]; |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 71 | str.appendf("%d: Color: 0x%08x, Region with %d rects\n", i, info.fColor, |
| 72 | info.fRegion.computeRegionComplexity()); |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 73 | } |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 74 | str.append(DumpPipelineInfo(*this->pipeline())); |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 75 | str.append(INHERITED::dumpInfo()); |
| 76 | return str; |
| 77 | } |
| 78 | |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 79 | private: |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 80 | void getProcessorAnalysisInputs(GrProcessorAnalysisColor* color, |
| 81 | GrProcessorAnalysisCoverage* coverage) const override { |
Brian Salomon | c0b642c | 2017-03-27 13:09:36 -0400 | [diff] [blame] | 82 | color->setToConstant(fRegions[0].fColor); |
Brian Salomon | a811b12 | 2017-03-30 08:21:32 -0400 | [diff] [blame] | 83 | *coverage = GrProcessorAnalysisCoverage::kNone; |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 84 | } |
| 85 | |
Brian Salomon | e7d3048 | 2017-03-29 12:09:15 -0400 | [diff] [blame] | 86 | void applyPipelineOptimizations(const PipelineOptimizations& optimizations) override { |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 87 | optimizations.getOverrideColorIfSet(&fRegions[0].fColor); |
Brian Salomon | 92aee3d | 2016-12-21 09:20:25 -0500 | [diff] [blame] | 88 | } |
| 89 | |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 90 | void onPrepareDraws(Target* target) const override { |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame] | 91 | sk_sp<GrGeometryProcessor> gp = make_gp(fViewMatrix); |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 92 | if (!gp) { |
| 93 | SkDebugf("Couldn't create GrGeometryProcessor\n"); |
| 94 | return; |
| 95 | } |
msarett | febb224 | 2016-08-26 12:49:27 -0700 | [diff] [blame] | 96 | SkASSERT(gp->getVertexStride() == sizeof(GrDefaultGeoProcFactory::PositionColorAttr)); |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 97 | |
| 98 | int numRegions = fRegions.count(); |
| 99 | int numRects = 0; |
| 100 | for (int i = 0; i < numRegions; i++) { |
| 101 | numRects += fRegions[i].fRegion.computeRegionComplexity(); |
| 102 | } |
| 103 | |
| 104 | size_t vertexStride = gp->getVertexStride(); |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 105 | sk_sp<const GrBuffer> indexBuffer(target->resourceProvider()->refQuadIndexBuffer()); |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 106 | PatternHelper helper(kTriangles_GrPrimitiveType); |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 107 | void* vertices = |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 108 | helper.init(target, vertexStride, indexBuffer.get(), kVertsPerInstance, |
| 109 | kIndicesPerInstance, numRects); |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 110 | if (!vertices || !indexBuffer) { |
| 111 | SkDebugf("Could not allocate vertices\n"); |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | intptr_t verts = reinterpret_cast<intptr_t>(vertices); |
| 116 | for (int i = 0; i < numRegions; i++) { |
msarett | febb224 | 2016-08-26 12:49:27 -0700 | [diff] [blame] | 117 | tesselate_region(verts, vertexStride, fRegions[i].fColor, fRegions[i].fRegion); |
| 118 | int numRectsInRegion = fRegions[i].fRegion.computeRegionComplexity(); |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 119 | verts += numRectsInRegion * kVertsPerInstance * vertexStride; |
| 120 | } |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 121 | helper.recordDraw(target, gp.get(), this->pipeline()); |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 124 | bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override { |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 125 | RegionOp* that = t->cast<RegionOp>(); |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 126 | if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(), |
| 127 | that->bounds(), caps)) { |
| 128 | return false; |
| 129 | } |
| 130 | |
msarett | febb224 | 2016-08-26 12:49:27 -0700 | [diff] [blame] | 131 | if (fViewMatrix != that->fViewMatrix) { |
| 132 | return false; |
| 133 | } |
| 134 | |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 135 | fRegions.push_back_n(that->fRegions.count(), that->fRegions.begin()); |
| 136 | this->joinBounds(*that); |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | struct RegionInfo { |
| 141 | GrColor fColor; |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 142 | SkRegion fRegion; |
| 143 | }; |
| 144 | |
msarett | febb224 | 2016-08-26 12:49:27 -0700 | [diff] [blame] | 145 | SkMatrix fViewMatrix; |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 146 | SkSTArray<1, RegionInfo, true> fRegions; |
| 147 | |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 148 | typedef GrLegacyMeshDrawOp INHERITED; |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 149 | }; |
| 150 | |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 151 | namespace GrRegionOp { |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 152 | |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 153 | std::unique_ptr<GrLegacyMeshDrawOp> Make(GrColor color, const SkMatrix& viewMatrix, |
| 154 | const SkRegion& region) { |
| 155 | return std::unique_ptr<GrLegacyMeshDrawOp>(new RegionOp(color, viewMatrix, region)); |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 156 | } |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 157 | } |