blob: 828a1b2d1b5da42245a64ddabcc014b6ccc5f1dd [file] [log] [blame]
msarettcc319b92016-08-25 18:07:18 -07001/*
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
Robert Phillipscadd5db2021-08-30 11:37:18 -04008#include "src/gpu/ops/RegionOp.h"
Robert Phillipsb97da532019-02-12 15:24:12 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkRegion.h"
11#include "src/core/SkMatrixPriv.h"
12#include "src/gpu/GrCaps.h"
13#include "src/gpu/GrDefaultGeoProcFactory.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrOpFlushState.h"
Robert Phillipsd1a8af62020-03-10 12:50:49 -040015#include "src/gpu/GrProgramInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrResourceProvider.h"
17#include "src/gpu/GrVertexWriter.h"
18#include "src/gpu/ops/GrMeshDrawOp.h"
Robert Phillips55f681f2020-02-28 08:58:15 -050019#include "src/gpu/ops/GrSimpleMeshDrawOpHelperWithStencil.h"
msarettcc319b92016-08-25 18:07:18 -070020
Robert Phillipscadd5db2021-08-30 11:37:18 -040021namespace {
22
23GrGeometryProcessor* make_gp(SkArenaAlloc* arena,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050024 const SkMatrix& viewMatrix,
25 bool wideColor) {
msarettcc319b92016-08-25 18:07:18 -070026 using namespace GrDefaultGeoProcFactory;
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050027 Color::Type colorType = wideColor ? Color::kPremulWideColorAttribute_Type
28 : Color::kPremulGrColorAttribute_Type;
Brian Osmanf0aee742020-03-12 09:28:44 -040029 return GrDefaultGeoProcFactory::Make(arena, colorType, Coverage::kSolid_Type,
Brian Osman7561dba2018-12-27 10:16:18 -050030 LocalCoords::kUsePosition_Type, viewMatrix);
msarettcc319b92016-08-25 18:07:18 -070031}
32
Robert Phillipscadd5db2021-08-30 11:37:18 -040033class RegionOpImpl final : public GrMeshDrawOp {
Brian Salomonf0366322017-07-11 15:53:05 -040034private:
Brian Salomon8514ebf2017-08-01 11:09:09 -040035 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
Brian Salomonf0366322017-07-11 15:53:05 -040036
msarettcc319b92016-08-25 18:07:18 -070037public:
Brian Salomon25a88092016-12-01 09:36:50 -050038 DEFINE_OP_CLASS_ID
msarettcc319b92016-08-25 18:07:18 -070039
Herb Derbyc76d4092020-10-07 16:46:15 -040040 static GrOp::Owner Make(GrRecordingContext* context,
41 GrPaint&& paint,
42 const SkMatrix& viewMatrix,
43 const SkRegion& region,
44 GrAAType aaType,
45 const GrUserStencilSettings* stencilSettings = nullptr) {
Robert Phillipscadd5db2021-08-30 11:37:18 -040046 return Helper::FactoryHelper<RegionOpImpl>(context, std::move(paint), viewMatrix, region,
47 aaType, stencilSettings);
Brian Salomonf0366322017-07-11 15:53:05 -040048 }
49
Robert Phillipscadd5db2021-08-30 11:37:18 -040050 RegionOpImpl(GrProcessorSet* processorSet, const SkPMColor4f& color,
51 const SkMatrix& viewMatrix, const SkRegion& region, GrAAType aaType,
52 const GrUserStencilSettings* stencilSettings)
Brian Salomon8514ebf2017-08-01 11:09:09 -040053 : INHERITED(ClassID())
Herb Derbyc76d4092020-10-07 16:46:15 -040054 , fHelper(processorSet, aaType, stencilSettings)
Brian Salomon8514ebf2017-08-01 11:09:09 -040055 , fViewMatrix(viewMatrix) {
msarettcc319b92016-08-25 18:07:18 -070056 RegionInfo& info = fRegions.push_back();
57 info.fColor = color;
msarettcc319b92016-08-25 18:07:18 -070058 info.fRegion = region;
59
60 SkRect bounds = SkRect::Make(region.getBounds());
Greg Daniel5faf4742019-10-01 15:14:44 -040061 this->setTransformedBounds(bounds, viewMatrix, HasAABloat::kNo, IsHairline::kNo);
msarettcc319b92016-08-25 18:07:18 -070062 }
63
Brian Salomonfc527d22016-12-14 21:07:01 -050064 const char* name() const override { return "GrRegionOp"; }
msarettcc319b92016-08-25 18:07:18 -070065
Robert Phillips294723d2021-06-17 09:23:58 -040066 void visitProxies(const GrVisitProxyFunc& func) const override {
Robert Phillipsd1a8af62020-03-10 12:50:49 -040067 if (fProgramInfo) {
Chris Daltonbe457422020-03-16 18:05:03 -060068 fProgramInfo->visitFPProxies(func);
Robert Phillipsd1a8af62020-03-10 12:50:49 -040069 } else {
70 fHelper.visitProxies(func);
71 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -040072 }
73
Brian Salomonf0366322017-07-11 15:53:05 -040074 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
75
Chris Dalton57ab06c2021-04-22 12:57:28 -060076 GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip,
77 GrClampType clampType) override {
78 return fHelper.finalizeProcessors(caps, clip, clampType, GrProcessorAnalysisCoverage::kNone,
79 &fRegions[0].fColor, &fWideColor);
Brian Salomonf0366322017-07-11 15:53:05 -040080 }
81
msarettcc319b92016-08-25 18:07:18 -070082private:
Robert Phillips2669a7b2020-03-12 12:07:19 -040083 GrProgramInfo* programInfo() override { return fProgramInfo; }
84
Robert Phillips4133dc42020-03-11 15:55:55 -040085 void onCreateProgramInfo(const GrCaps* caps,
86 SkArenaAlloc* arena,
Adlai Hollere2296f72020-11-19 13:41:26 -050087 const GrSurfaceProxyView& writeView,
Chris Dalton6aaf00f2021-07-13 13:26:39 -060088 bool usesMSAASurface,
Robert Phillips4133dc42020-03-11 15:55:55 -040089 GrAppliedClip&& appliedClip,
John Stiles52cb1d02021-06-02 11:58:05 -040090 const GrDstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -050091 GrXferBarrierFlags renderPassXferBarriers,
92 GrLoadOp colorLoadOp) override {
Brian Osmanf0aee742020-03-12 09:28:44 -040093 GrGeometryProcessor* gp = make_gp(arena, fViewMatrix, fWideColor);
msarettcc319b92016-08-25 18:07:18 -070094 if (!gp) {
95 SkDebugf("Couldn't create GrGeometryProcessor\n");
Robert Phillips4133dc42020-03-11 15:55:55 -040096 return;
Robert Phillipsd1a8af62020-03-10 12:50:49 -040097 }
98
Chris Dalton2a26c502021-08-26 10:05:11 -060099 fProgramInfo = fHelper.createProgramInfoWithStencil(caps, arena, writeView, usesMSAASurface,
Robert Phillips4133dc42020-03-11 15:55:55 -0400100 std::move(appliedClip), dstProxyView,
Greg Danield358cbe2020-09-11 09:33:54 -0400101 gp, GrPrimitiveType::kTriangles,
Greg Daniel42dbca52020-11-20 10:22:43 -0500102 renderPassXferBarriers, colorLoadOp);
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400103 }
104
Robert Phillips71143952021-06-17 14:55:07 -0400105 void onPrepareDraws(GrMeshDrawTarget* target) override {
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400106 if (!fProgramInfo) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400107 this->createProgramInfo(target);
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400108 if (!fProgramInfo) {
109 return;
110 }
msarettcc319b92016-08-25 18:07:18 -0700111 }
msarettcc319b92016-08-25 18:07:18 -0700112
113 int numRegions = fRegions.count();
114 int numRects = 0;
115 for (int i = 0; i < numRegions; i++) {
116 numRects += fRegions[i].fRegion.computeRegionComplexity();
117 }
118
Brian Salomonf0366322017-07-11 15:53:05 -0400119 if (!numRects) {
120 return;
121 }
Robert Phillipse94cdd22019-11-04 14:15:58 -0500122
Robert Phillips787fd9d2021-03-22 14:48:09 -0400123 QuadHelper helper(target, fProgramInfo->geomProc().vertexStride(), numRects);
Robert Phillipse94cdd22019-11-04 14:15:58 -0500124
Brian Osman4486d982018-11-15 15:56:04 -0500125 GrVertexWriter vertices{helper.vertices()};
Brian Salomon12d22642019-01-29 14:38:50 -0500126 if (!vertices.fPtr) {
msarettcc319b92016-08-25 18:07:18 -0700127 SkDebugf("Could not allocate vertices\n");
128 return;
129 }
130
msarettcc319b92016-08-25 18:07:18 -0700131 for (int i = 0; i < numRegions; i++) {
Brian Osman7561dba2018-12-27 10:16:18 -0500132 GrVertexColor color(fRegions[i].fColor, fWideColor);
Brian Osman4486d982018-11-15 15:56:04 -0500133 SkRegion::Iterator iter(fRegions[i].fRegion);
134 while (!iter.done()) {
135 SkRect rect = SkRect::Make(iter.rect());
Brian Osman0dd43022018-11-16 15:53:26 -0500136 vertices.writeQuad(GrVertexWriter::TriStripFromRect(rect), color);
Brian Osman4486d982018-11-15 15:56:04 -0500137 iter.next();
138 }
msarettcc319b92016-08-25 18:07:18 -0700139 }
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400140
141 fMesh = helper.mesh();
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700142 }
143
144 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400145 if (!fProgramInfo || !fMesh) {
146 return;
147 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500148
Chris Dalton765ed362020-03-16 17:34:44 -0600149 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
Robert Phillips787fd9d2021-03-22 14:48:09 -0400150 flushState->bindTextures(fProgramInfo->geomProc(), nullptr, fProgramInfo->pipeline());
Chris Dalton765ed362020-03-16 17:34:44 -0600151 flushState->drawMesh(*fMesh);
msarettcc319b92016-08-25 18:07:18 -0700152 }
153
Herb Derbye25c3002020-10-27 15:57:27 -0400154 CombineResult onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) override {
Robert Phillipscadd5db2021-08-30 11:37:18 -0400155 auto that = t->cast<RegionOpImpl>();
Brian Salomonf0366322017-07-11 15:53:05 -0400156 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000157 return CombineResult::kCannotCombine;
msarettcc319b92016-08-25 18:07:18 -0700158 }
159
msarettfebb2242016-08-26 12:49:27 -0700160 if (fViewMatrix != that->fViewMatrix) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000161 return CombineResult::kCannotCombine;
msarettfebb2242016-08-26 12:49:27 -0700162 }
163
msarettcc319b92016-08-25 18:07:18 -0700164 fRegions.push_back_n(that->fRegions.count(), that->fRegions.begin());
Brian Osman7561dba2018-12-27 10:16:18 -0500165 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000166 return CombineResult::kMerged;
msarettcc319b92016-08-25 18:07:18 -0700167 }
168
John Stilesaf366522020-08-13 09:57:34 -0400169#if GR_TEST_UTILS
170 SkString onDumpInfo() const override {
171 SkString str = SkStringPrintf("# combined: %d\n", fRegions.count());
172 for (int i = 0; i < fRegions.count(); ++i) {
173 const RegionInfo& info = fRegions[i];
174 str.appendf("%d: Color: 0x%08x, Region with %d rects\n", i, info.fColor.toBytes_RGBA(),
175 info.fRegion.computeRegionComplexity());
176 }
177 str += fHelper.dumpInfo();
178 return str;
179 }
180#endif
181
msarettcc319b92016-08-25 18:07:18 -0700182 struct RegionInfo {
Brian Osmancf860852018-10-31 14:04:39 -0400183 SkPMColor4f fColor;
msarettcc319b92016-08-25 18:07:18 -0700184 SkRegion fRegion;
185 };
186
Brian Salomonf0366322017-07-11 15:53:05 -0400187 Helper fHelper;
msarettfebb2242016-08-26 12:49:27 -0700188 SkMatrix fViewMatrix;
msarettcc319b92016-08-25 18:07:18 -0700189 SkSTArray<1, RegionInfo, true> fRegions;
Brian Osman7561dba2018-12-27 10:16:18 -0500190 bool fWideColor;
msarettcc319b92016-08-25 18:07:18 -0700191
Chris Daltoneb694b72020-03-16 09:25:50 -0600192 GrSimpleMesh* fMesh = nullptr;
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400193 GrProgramInfo* fProgramInfo = nullptr;
194
John Stiles7571f9e2020-09-02 22:42:33 -0400195 using INHERITED = GrMeshDrawOp;
msarettcc319b92016-08-25 18:07:18 -0700196};
197
Brian Salomonf0366322017-07-11 15:53:05 -0400198} // anonymous namespace
199
Robert Phillipscadd5db2021-08-30 11:37:18 -0400200namespace skgpu::v1::RegionOp {
msarettcc319b92016-08-25 18:07:18 -0700201
Herb Derbyc76d4092020-10-07 16:46:15 -0400202GrOp::Owner Make(GrRecordingContext* context,
203 GrPaint&& paint,
204 const SkMatrix& viewMatrix,
205 const SkRegion& region,
206 GrAAType aaType,
207 const GrUserStencilSettings* stencilSettings) {
Brian Salomonf0366322017-07-11 15:53:05 -0400208 if (aaType != GrAAType::kNone && aaType != GrAAType::kMSAA) {
209 return nullptr;
210 }
Robert Phillipscadd5db2021-08-30 11:37:18 -0400211 return RegionOpImpl::Make(context, std::move(paint), viewMatrix, region, aaType,
212 stencilSettings);
msarettcc319b92016-08-25 18:07:18 -0700213}
Robert Phillipscadd5db2021-08-30 11:37:18 -0400214
215} // namespace skgpu::v1::RegionOp
Brian Salomonf0366322017-07-11 15:53:05 -0400216
217#if GR_TEST_UTILS
218
Robert Phillipscadd5db2021-08-30 11:37:18 -0400219#include "src/gpu/GrDrawOpTest.h"
220
Brian Salomonf0366322017-07-11 15:53:05 -0400221GR_DRAW_OP_TEST_DEFINE(RegionOp) {
222 SkRegion region;
223 int n = random->nextULessThan(200);
224 for (int i = 0; i < n; ++i) {
225 SkIPoint center;
226 center.fX = random->nextULessThan(1000);
227 center.fY = random->nextULessThan(1000);
228 int w = random->nextRangeU(10, 1000);
229 int h = random->nextRangeU(10, 1000);
230 SkIRect rect = {center.fX - w / 2, center.fY - h / 2, center.fX + w / 2, center.fY + h / 2};
231 SkRegion::Op op;
232 if (i == 0) {
233 op = SkRegion::kReplace_Op;
234 } else {
235 // Pick an other than replace.
Brian Salomon4dea72a2019-12-18 10:43:10 -0500236 static_assert(SkRegion::kLastOp == SkRegion::kReplace_Op);
Brian Salomonf0366322017-07-11 15:53:05 -0400237 op = (SkRegion::Op)random->nextULessThan(SkRegion::kLastOp);
238 }
239 region.op(rect, op);
240 }
241 SkMatrix viewMatrix = GrTest::TestMatrix(random);
242 GrAAType aaType = GrAAType::kNone;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600243 if (numSamples > 1 && random->nextBool()) {
Brian Salomonf0366322017-07-11 15:53:05 -0400244 aaType = GrAAType::kMSAA;
245 }
Robert Phillipscadd5db2021-08-30 11:37:18 -0400246 return RegionOpImpl::Make(context, std::move(paint), viewMatrix, region, aaType,
247 GrGetRandomStencil(random, context));
Brian Salomonf0366322017-07-11 15:53:05 -0400248}
249
Robert Phillipscadd5db2021-08-30 11:37:18 -0400250#endif // GR_TEST_UTILS