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" |
Robert Phillips | d1a8af6 | 2020-03-10 12:50:49 -0400 | [diff] [blame] | 16 | #include "src/gpu/GrProgramInfo.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/gpu/GrResourceProvider.h" |
| 18 | #include "src/gpu/GrVertexWriter.h" |
| 19 | #include "src/gpu/ops/GrMeshDrawOp.h" |
Robert Phillips | 55f681f | 2020-02-28 08:58:15 -0500 | [diff] [blame] | 20 | #include "src/gpu/ops/GrSimpleMeshDrawOpHelperWithStencil.h" |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 21 | |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 22 | static GrGeometryProcessor* make_gp(SkArenaAlloc* arena, |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 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; |
Brian Osman | f0aee74 | 2020-03-12 09:28:44 -0400 | [diff] [blame] | 28 | return GrDefaultGeoProcFactory::Make(arena, 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 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 41 | static GrOp::Owner Make(GrRecordingContext* context, |
| 42 | GrPaint&& paint, |
| 43 | const SkMatrix& viewMatrix, |
| 44 | const SkRegion& region, |
| 45 | GrAAType aaType, |
| 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 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 51 | RegionOp(GrProcessorSet* processorSet, const SkPMColor4f& color, |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 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()) |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 55 | , fHelper(processorSet, aaType, stencilSettings) |
Brian Salomon | 8514ebf | 2017-08-01 11:09:09 -0400 | [diff] [blame] | 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 | d1a8af6 | 2020-03-10 12:50:49 -0400 | [diff] [blame] | 68 | if (fProgramInfo) { |
Chris Dalton | be45742 | 2020-03-16 18:05:03 -0600 | [diff] [blame] | 69 | fProgramInfo->visitFPProxies(func); |
Robert Phillips | d1a8af6 | 2020-03-10 12:50:49 -0400 | [diff] [blame] | 70 | } else { |
| 71 | fHelper.visitProxies(func); |
| 72 | } |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 73 | } |
| 74 | |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 75 | FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); } |
| 76 | |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 77 | GrProcessorSet::Analysis finalize( |
| 78 | const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage, |
| 79 | GrClampType clampType) override { |
| 80 | return fHelper.finalizeProcessors( |
| 81 | caps, clip, hasMixedSampledCoverage, clampType, GrProcessorAnalysisCoverage::kNone, |
| 82 | &fRegions[0].fColor, &fWideColor); |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 83 | } |
| 84 | |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 85 | private: |
Robert Phillips | 2669a7b | 2020-03-12 12:07:19 -0400 | [diff] [blame] | 86 | GrProgramInfo* programInfo() override { return fProgramInfo; } |
| 87 | |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 88 | void onCreateProgramInfo(const GrCaps* caps, |
| 89 | SkArenaAlloc* arena, |
Adlai Holler | e2296f7 | 2020-11-19 13:41:26 -0500 | [diff] [blame] | 90 | const GrSurfaceProxyView& writeView, |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 91 | GrAppliedClip&& appliedClip, |
Greg Daniel | d358cbe | 2020-09-11 09:33:54 -0400 | [diff] [blame] | 92 | const GrXferProcessor::DstProxyView& dstProxyView, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame^] | 93 | GrXferBarrierFlags renderPassXferBarriers, |
| 94 | GrLoadOp colorLoadOp) override { |
Brian Osman | f0aee74 | 2020-03-12 09:28:44 -0400 | [diff] [blame] | 95 | GrGeometryProcessor* gp = make_gp(arena, fViewMatrix, fWideColor); |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 96 | if (!gp) { |
| 97 | SkDebugf("Couldn't create GrGeometryProcessor\n"); |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 98 | return; |
Robert Phillips | d1a8af6 | 2020-03-10 12:50:49 -0400 | [diff] [blame] | 99 | } |
| 100 | |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 101 | fProgramInfo = fHelper.createProgramInfoWithStencil(caps, arena, writeView, |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 102 | std::move(appliedClip), dstProxyView, |
Greg Daniel | d358cbe | 2020-09-11 09:33:54 -0400 | [diff] [blame] | 103 | gp, GrPrimitiveType::kTriangles, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame^] | 104 | renderPassXferBarriers, colorLoadOp); |
Robert Phillips | d1a8af6 | 2020-03-10 12:50:49 -0400 | [diff] [blame] | 105 | } |
| 106 | |
Robert Phillips | d1a8af6 | 2020-03-10 12:50:49 -0400 | [diff] [blame] | 107 | void onPrepareDraws(Target* target) override { |
| 108 | if (!fProgramInfo) { |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 109 | this->createProgramInfo(target); |
Robert Phillips | d1a8af6 | 2020-03-10 12:50:49 -0400 | [diff] [blame] | 110 | if (!fProgramInfo) { |
| 111 | return; |
| 112 | } |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 113 | } |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 114 | |
| 115 | int numRegions = fRegions.count(); |
| 116 | int numRects = 0; |
| 117 | for (int i = 0; i < numRegions; i++) { |
| 118 | numRects += fRegions[i].fRegion.computeRegionComplexity(); |
| 119 | } |
| 120 | |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 121 | if (!numRects) { |
| 122 | return; |
| 123 | } |
Robert Phillips | e94cdd2 | 2019-11-04 14:15:58 -0500 | [diff] [blame] | 124 | |
Robert Phillips | d1a8af6 | 2020-03-10 12:50:49 -0400 | [diff] [blame] | 125 | QuadHelper helper(target, fProgramInfo->primProc().vertexStride(), numRects); |
Robert Phillips | e94cdd2 | 2019-11-04 14:15:58 -0500 | [diff] [blame] | 126 | |
Brian Osman | 4486d98 | 2018-11-15 15:56:04 -0500 | [diff] [blame] | 127 | GrVertexWriter vertices{helper.vertices()}; |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 128 | if (!vertices.fPtr) { |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 129 | SkDebugf("Could not allocate vertices\n"); |
| 130 | return; |
| 131 | } |
| 132 | |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 133 | for (int i = 0; i < numRegions; i++) { |
Brian Osman | 7561dba | 2018-12-27 10:16:18 -0500 | [diff] [blame] | 134 | GrVertexColor color(fRegions[i].fColor, fWideColor); |
Brian Osman | 4486d98 | 2018-11-15 15:56:04 -0500 | [diff] [blame] | 135 | SkRegion::Iterator iter(fRegions[i].fRegion); |
| 136 | while (!iter.done()) { |
| 137 | SkRect rect = SkRect::Make(iter.rect()); |
Brian Osman | 0dd4302 | 2018-11-16 15:53:26 -0500 | [diff] [blame] | 138 | vertices.writeQuad(GrVertexWriter::TriStripFromRect(rect), color); |
Brian Osman | 4486d98 | 2018-11-15 15:56:04 -0500 | [diff] [blame] | 139 | iter.next(); |
| 140 | } |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 141 | } |
Robert Phillips | d1a8af6 | 2020-03-10 12:50:49 -0400 | [diff] [blame] | 142 | |
| 143 | fMesh = helper.mesh(); |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override { |
Robert Phillips | d1a8af6 | 2020-03-10 12:50:49 -0400 | [diff] [blame] | 147 | if (!fProgramInfo || !fMesh) { |
| 148 | return; |
| 149 | } |
Robert Phillips | 3968fcb | 2019-12-05 16:40:31 -0500 | [diff] [blame] | 150 | |
Chris Dalton | 765ed36 | 2020-03-16 17:34:44 -0600 | [diff] [blame] | 151 | flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds); |
| 152 | flushState->bindTextures(fProgramInfo->primProc(), nullptr, fProgramInfo->pipeline()); |
| 153 | flushState->drawMesh(*fMesh); |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Herb Derby | e25c300 | 2020-10-27 15:57:27 -0400 | [diff] [blame] | 156 | CombineResult onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) override { |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 157 | RegionOp* that = t->cast<RegionOp>(); |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 158 | if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 159 | return CombineResult::kCannotCombine; |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 160 | } |
| 161 | |
msarett | febb224 | 2016-08-26 12:49:27 -0700 | [diff] [blame] | 162 | if (fViewMatrix != that->fViewMatrix) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 163 | return CombineResult::kCannotCombine; |
msarett | febb224 | 2016-08-26 12:49:27 -0700 | [diff] [blame] | 164 | } |
| 165 | |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 166 | fRegions.push_back_n(that->fRegions.count(), that->fRegions.begin()); |
Brian Osman | 7561dba | 2018-12-27 10:16:18 -0500 | [diff] [blame] | 167 | fWideColor |= that->fWideColor; |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 168 | return CombineResult::kMerged; |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 169 | } |
| 170 | |
John Stiles | af36652 | 2020-08-13 09:57:34 -0400 | [diff] [blame] | 171 | #if GR_TEST_UTILS |
| 172 | SkString onDumpInfo() const override { |
| 173 | SkString str = SkStringPrintf("# combined: %d\n", fRegions.count()); |
| 174 | for (int i = 0; i < fRegions.count(); ++i) { |
| 175 | const RegionInfo& info = fRegions[i]; |
| 176 | str.appendf("%d: Color: 0x%08x, Region with %d rects\n", i, info.fColor.toBytes_RGBA(), |
| 177 | info.fRegion.computeRegionComplexity()); |
| 178 | } |
| 179 | str += fHelper.dumpInfo(); |
| 180 | return str; |
| 181 | } |
| 182 | #endif |
| 183 | |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 184 | struct RegionInfo { |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 185 | SkPMColor4f fColor; |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 186 | SkRegion fRegion; |
| 187 | }; |
| 188 | |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 189 | Helper fHelper; |
msarett | febb224 | 2016-08-26 12:49:27 -0700 | [diff] [blame] | 190 | SkMatrix fViewMatrix; |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 191 | SkSTArray<1, RegionInfo, true> fRegions; |
Brian Osman | 7561dba | 2018-12-27 10:16:18 -0500 | [diff] [blame] | 192 | bool fWideColor; |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 193 | |
Chris Dalton | eb694b7 | 2020-03-16 09:25:50 -0600 | [diff] [blame] | 194 | GrSimpleMesh* fMesh = nullptr; |
Robert Phillips | d1a8af6 | 2020-03-10 12:50:49 -0400 | [diff] [blame] | 195 | GrProgramInfo* fProgramInfo = nullptr; |
| 196 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 197 | using INHERITED = GrMeshDrawOp; |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 198 | }; |
| 199 | |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 200 | } // anonymous namespace |
| 201 | |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 202 | namespace GrRegionOp { |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 203 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 204 | GrOp::Owner Make(GrRecordingContext* context, |
| 205 | GrPaint&& paint, |
| 206 | const SkMatrix& viewMatrix, |
| 207 | const SkRegion& region, |
| 208 | GrAAType aaType, |
| 209 | const GrUserStencilSettings* stencilSettings) { |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 210 | if (aaType != GrAAType::kNone && aaType != GrAAType::kMSAA) { |
| 211 | return nullptr; |
| 212 | } |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 213 | return RegionOp::Make(context, std::move(paint), viewMatrix, region, aaType, stencilSettings); |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 214 | } |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 215 | } // namespace GrRegionOp |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 216 | |
| 217 | #if GR_TEST_UTILS |
| 218 | |
| 219 | GR_DRAW_OP_TEST_DEFINE(RegionOp) { |
| 220 | SkRegion region; |
| 221 | int n = random->nextULessThan(200); |
| 222 | for (int i = 0; i < n; ++i) { |
| 223 | SkIPoint center; |
| 224 | center.fX = random->nextULessThan(1000); |
| 225 | center.fY = random->nextULessThan(1000); |
| 226 | int w = random->nextRangeU(10, 1000); |
| 227 | int h = random->nextRangeU(10, 1000); |
| 228 | SkIRect rect = {center.fX - w / 2, center.fY - h / 2, center.fX + w / 2, center.fY + h / 2}; |
| 229 | SkRegion::Op op; |
| 230 | if (i == 0) { |
| 231 | op = SkRegion::kReplace_Op; |
| 232 | } else { |
| 233 | // Pick an other than replace. |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 234 | static_assert(SkRegion::kLastOp == SkRegion::kReplace_Op); |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 235 | op = (SkRegion::Op)random->nextULessThan(SkRegion::kLastOp); |
| 236 | } |
| 237 | region.op(rect, op); |
| 238 | } |
| 239 | SkMatrix viewMatrix = GrTest::TestMatrix(random); |
| 240 | GrAAType aaType = GrAAType::kNone; |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 241 | if (numSamples > 1 && random->nextBool()) { |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 242 | aaType = GrAAType::kMSAA; |
| 243 | } |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 244 | return RegionOp::Make(context, std::move(paint), viewMatrix, region, aaType, |
Brian Salomon | 8514ebf | 2017-08-01 11:09:09 -0400 | [diff] [blame] | 245 | GrGetRandomStencil(random, context)); |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | #endif |