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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/ops/GrRegionOp.h" |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkRegion.h" |
| 11 | #include "src/core/SkMatrixPriv.h" |
| 12 | #include "src/gpu/GrCaps.h" |
| 13 | #include "src/gpu/GrDefaultGeoProcFactory.h" |
| 14 | #include "src/gpu/GrDrawOpTest.h" |
| 15 | #include "src/gpu/GrOpFlushState.h" |
| 16 | #include "src/gpu/GrResourceProvider.h" |
| 17 | #include "src/gpu/GrVertexWriter.h" |
| 18 | #include "src/gpu/ops/GrMeshDrawOp.h" |
| 19 | #include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h" |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 20 | |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 21 | static GrGeometryProcessor* make_gp(SkArenaAlloc* arena, |
| 22 | const GrShaderCaps* shaderCaps, |
| 23 | const SkMatrix& viewMatrix, |
| 24 | bool wideColor) { |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 25 | using namespace GrDefaultGeoProcFactory; |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 26 | Color::Type colorType = wideColor ? Color::kPremulWideColorAttribute_Type |
| 27 | : Color::kPremulGrColorAttribute_Type; |
| 28 | return GrDefaultGeoProcFactory::Make(arena, shaderCaps, colorType, Coverage::kSolid_Type, |
Brian Osman | 7561dba | 2018-12-27 10:16:18 -0500 | [diff] [blame] | 29 | LocalCoords::kUsePosition_Type, viewMatrix); |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 30 | } |
| 31 | |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 32 | namespace { |
| 33 | |
| 34 | class RegionOp final : public GrMeshDrawOp { |
| 35 | private: |
Brian Salomon | 8514ebf | 2017-08-01 11:09:09 -0400 | [diff] [blame] | 36 | using Helper = GrSimpleMeshDrawOpHelperWithStencil; |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 37 | |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 38 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 39 | DEFINE_OP_CLASS_ID |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 40 | |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 41 | static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 42 | GrPaint&& paint, |
| 43 | const SkMatrix& viewMatrix, |
| 44 | const SkRegion& region, |
| 45 | GrAAType aaType, |
Brian Salomon | 8514ebf | 2017-08-01 11:09:09 -0400 | [diff] [blame] | 46 | const GrUserStencilSettings* stencilSettings = nullptr) { |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 47 | return Helper::FactoryHelper<RegionOp>(context, std::move(paint), viewMatrix, region, |
| 48 | aaType, stencilSettings); |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 49 | } |
| 50 | |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 51 | RegionOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color, |
| 52 | const SkMatrix& viewMatrix, const SkRegion& region, GrAAType aaType, |
| 53 | const GrUserStencilSettings* stencilSettings) |
Brian Salomon | 8514ebf | 2017-08-01 11:09:09 -0400 | [diff] [blame] | 54 | : INHERITED(ClassID()) |
| 55 | , fHelper(helperArgs, aaType, stencilSettings) |
| 56 | , fViewMatrix(viewMatrix) { |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 57 | RegionInfo& info = fRegions.push_back(); |
| 58 | info.fColor = color; |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 59 | info.fRegion = region; |
| 60 | |
| 61 | SkRect bounds = SkRect::Make(region.getBounds()); |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 62 | this->setTransformedBounds(bounds, viewMatrix, HasAABloat::kNo, IsHairline::kNo); |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 65 | const char* name() const override { return "GrRegionOp"; } |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 66 | |
Chris Dalton | 1706cbf | 2019-05-21 19:35:29 -0600 | [diff] [blame] | 67 | void visitProxies(const VisitProxyFunc& func) const override { |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 68 | fHelper.visitProxies(func); |
| 69 | } |
| 70 | |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 71 | #ifdef SK_DEBUG |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 72 | SkString dumpInfo() const override { |
| 73 | SkString str; |
Brian Salomon | 53e4c3c | 2016-12-21 11:38:53 -0500 | [diff] [blame] | 74 | str.appendf("# combined: %d\n", fRegions.count()); |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 75 | for (int i = 0; i < fRegions.count(); ++i) { |
| 76 | const RegionInfo& info = fRegions[i]; |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 77 | str.appendf("%d: Color: 0x%08x, Region with %d rects\n", i, info.fColor.toBytes_RGBA(), |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 78 | info.fRegion.computeRegionComplexity()); |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 79 | } |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 80 | str += fHelper.dumpInfo(); |
| 81 | str += INHERITED::dumpInfo(); |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 82 | return str; |
| 83 | } |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 84 | #endif |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 85 | |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 86 | FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); } |
| 87 | |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 88 | GrProcessorSet::Analysis finalize( |
| 89 | const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage, |
| 90 | GrClampType clampType) override { |
| 91 | return fHelper.finalizeProcessors( |
| 92 | caps, clip, hasMixedSampledCoverage, clampType, GrProcessorAnalysisCoverage::kNone, |
| 93 | &fRegions[0].fColor, &fWideColor); |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 94 | } |
| 95 | |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 96 | private: |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 97 | void onPrepareDraws(Target* target) override { |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 98 | GrGeometryProcessor* gp = make_gp(target->allocator(), target->caps().shaderCaps(), |
| 99 | fViewMatrix, fWideColor); |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 100 | if (!gp) { |
| 101 | SkDebugf("Couldn't create GrGeometryProcessor\n"); |
| 102 | return; |
| 103 | } |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 104 | |
| 105 | int numRegions = fRegions.count(); |
| 106 | int numRects = 0; |
| 107 | for (int i = 0; i < numRegions; i++) { |
| 108 | numRects += fRegions[i].fRegion.computeRegionComplexity(); |
| 109 | } |
| 110 | |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 111 | if (!numRects) { |
| 112 | return; |
| 113 | } |
Robert Phillips | e94cdd2 | 2019-11-04 14:15:58 -0500 | [diff] [blame] | 114 | |
| 115 | QuadHelper helper(target, gp->vertexStride(), numRects); |
| 116 | |
Brian Osman | 4486d98 | 2018-11-15 15:56:04 -0500 | [diff] [blame] | 117 | GrVertexWriter vertices{helper.vertices()}; |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 118 | if (!vertices.fPtr) { |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 119 | SkDebugf("Could not allocate vertices\n"); |
| 120 | return; |
| 121 | } |
| 122 | |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 123 | for (int i = 0; i < numRegions; i++) { |
Brian Osman | 7561dba | 2018-12-27 10:16:18 -0500 | [diff] [blame] | 124 | GrVertexColor color(fRegions[i].fColor, fWideColor); |
Brian Osman | 4486d98 | 2018-11-15 15:56:04 -0500 | [diff] [blame] | 125 | SkRegion::Iterator iter(fRegions[i].fRegion); |
| 126 | while (!iter.done()) { |
| 127 | SkRect rect = SkRect::Make(iter.rect()); |
Brian Osman | 0dd4302 | 2018-11-16 15:53:26 -0500 | [diff] [blame] | 128 | vertices.writeQuad(GrVertexWriter::TriStripFromRect(rect), color); |
Brian Osman | 4486d98 | 2018-11-15 15:56:04 -0500 | [diff] [blame] | 129 | iter.next(); |
| 130 | } |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 131 | } |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 132 | helper.recordDraw(target, gp); |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override { |
Robert Phillips | 3968fcb | 2019-12-05 16:40:31 -0500 | [diff] [blame^] | 136 | auto pipeline = GrSimpleMeshDrawOpHelper::CreatePipeline(flushState, |
| 137 | fHelper.detachProcessorSet(), |
| 138 | fHelper.pipelineFlags(), |
| 139 | fHelper.stencilSettings()); |
| 140 | |
| 141 | flushState->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds, pipeline); |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 144 | CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override { |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 145 | RegionOp* that = t->cast<RegionOp>(); |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 146 | if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 147 | return CombineResult::kCannotCombine; |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 148 | } |
| 149 | |
msarett | febb224 | 2016-08-26 12:49:27 -0700 | [diff] [blame] | 150 | if (fViewMatrix != that->fViewMatrix) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 151 | return CombineResult::kCannotCombine; |
msarett | febb224 | 2016-08-26 12:49:27 -0700 | [diff] [blame] | 152 | } |
| 153 | |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 154 | fRegions.push_back_n(that->fRegions.count(), that->fRegions.begin()); |
Brian Osman | 7561dba | 2018-12-27 10:16:18 -0500 | [diff] [blame] | 155 | fWideColor |= that->fWideColor; |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 156 | return CombineResult::kMerged; |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | struct RegionInfo { |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 160 | SkPMColor4f fColor; |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 161 | SkRegion fRegion; |
| 162 | }; |
| 163 | |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 164 | Helper fHelper; |
msarett | febb224 | 2016-08-26 12:49:27 -0700 | [diff] [blame] | 165 | SkMatrix fViewMatrix; |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 166 | SkSTArray<1, RegionInfo, true> fRegions; |
Brian Osman | 7561dba | 2018-12-27 10:16:18 -0500 | [diff] [blame] | 167 | bool fWideColor; |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 168 | |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 169 | typedef GrMeshDrawOp INHERITED; |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 170 | }; |
| 171 | |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 172 | } // anonymous namespace |
| 173 | |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 174 | namespace GrRegionOp { |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 175 | |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 176 | std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 177 | GrPaint&& paint, |
| 178 | const SkMatrix& viewMatrix, |
| 179 | const SkRegion& region, |
| 180 | GrAAType aaType, |
| 181 | const GrUserStencilSettings* stencilSettings) { |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 182 | if (aaType != GrAAType::kNone && aaType != GrAAType::kMSAA) { |
| 183 | return nullptr; |
| 184 | } |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 185 | return RegionOp::Make(context, std::move(paint), viewMatrix, region, aaType, stencilSettings); |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 186 | } |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 187 | } |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 188 | |
| 189 | #if GR_TEST_UTILS |
| 190 | |
| 191 | GR_DRAW_OP_TEST_DEFINE(RegionOp) { |
| 192 | SkRegion region; |
| 193 | int n = random->nextULessThan(200); |
| 194 | for (int i = 0; i < n; ++i) { |
| 195 | SkIPoint center; |
| 196 | center.fX = random->nextULessThan(1000); |
| 197 | center.fY = random->nextULessThan(1000); |
| 198 | int w = random->nextRangeU(10, 1000); |
| 199 | int h = random->nextRangeU(10, 1000); |
| 200 | SkIRect rect = {center.fX - w / 2, center.fY - h / 2, center.fX + w / 2, center.fY + h / 2}; |
| 201 | SkRegion::Op op; |
| 202 | if (i == 0) { |
| 203 | op = SkRegion::kReplace_Op; |
| 204 | } else { |
| 205 | // Pick an other than replace. |
Brian Salomon | b0047b5 | 2019-12-05 19:52:25 +0000 | [diff] [blame] | 206 | GR_STATIC_ASSERT(SkRegion::kLastOp == SkRegion::kReplace_Op); |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 207 | op = (SkRegion::Op)random->nextULessThan(SkRegion::kLastOp); |
| 208 | } |
| 209 | region.op(rect, op); |
| 210 | } |
| 211 | SkMatrix viewMatrix = GrTest::TestMatrix(random); |
| 212 | GrAAType aaType = GrAAType::kNone; |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 213 | if (numSamples > 1 && random->nextBool()) { |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 214 | aaType = GrAAType::kMSAA; |
| 215 | } |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 216 | return RegionOp::Make(context, std::move(paint), viewMatrix, region, aaType, |
Brian Salomon | 8514ebf | 2017-08-01 11:09:09 -0400 | [diff] [blame] | 217 | GrGetRandomStencil(random, context)); |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | #endif |