blob: f86533990d0fc784ad6a841c7fcdeaa02ad05132 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/ops/GrRegionOp.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"
14#include "src/gpu/GrDrawOpTest.h"
15#include "src/gpu/GrOpFlushState.h"
Robert Phillipsd1a8af62020-03-10 12:50:49 -040016#include "src/gpu/GrProgramInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/GrResourceProvider.h"
18#include "src/gpu/GrVertexWriter.h"
19#include "src/gpu/ops/GrMeshDrawOp.h"
Robert Phillips55f681f2020-02-28 08:58:15 -050020#include "src/gpu/ops/GrSimpleMeshDrawOpHelperWithStencil.h"
msarettcc319b92016-08-25 18:07:18 -070021
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050022static GrGeometryProcessor* make_gp(SkArenaAlloc* arena,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050023 const SkMatrix& viewMatrix,
24 bool wideColor) {
msarettcc319b92016-08-25 18:07:18 -070025 using namespace GrDefaultGeoProcFactory;
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050026 Color::Type colorType = wideColor ? Color::kPremulWideColorAttribute_Type
27 : Color::kPremulGrColorAttribute_Type;
Brian Osmanf0aee742020-03-12 09:28:44 -040028 return GrDefaultGeoProcFactory::Make(arena, colorType, Coverage::kSolid_Type,
Brian Osman7561dba2018-12-27 10:16:18 -050029 LocalCoords::kUsePosition_Type, viewMatrix);
msarettcc319b92016-08-25 18:07:18 -070030}
31
Brian Salomonf0366322017-07-11 15:53:05 -040032namespace {
33
34class RegionOp final : public GrMeshDrawOp {
35private:
Brian Salomon8514ebf2017-08-01 11:09:09 -040036 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
Brian Salomonf0366322017-07-11 15:53:05 -040037
msarettcc319b92016-08-25 18:07:18 -070038public:
Brian Salomon25a88092016-12-01 09:36:50 -050039 DEFINE_OP_CLASS_ID
msarettcc319b92016-08-25 18:07:18 -070040
Herb Derbyc76d4092020-10-07 16:46:15 -040041 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 Phillips7c525e62018-06-12 10:11:12 -040047 return Helper::FactoryHelper<RegionOp>(context, std::move(paint), viewMatrix, region,
48 aaType, stencilSettings);
Brian Salomonf0366322017-07-11 15:53:05 -040049 }
50
Herb Derbyc76d4092020-10-07 16:46:15 -040051 RegionOp(GrProcessorSet* processorSet, const SkPMColor4f& color,
Brian Osmancf860852018-10-31 14:04:39 -040052 const SkMatrix& viewMatrix, const SkRegion& region, GrAAType aaType,
53 const GrUserStencilSettings* stencilSettings)
Brian Salomon8514ebf2017-08-01 11:09:09 -040054 : INHERITED(ClassID())
Herb Derbyc76d4092020-10-07 16:46:15 -040055 , fHelper(processorSet, aaType, stencilSettings)
Brian Salomon8514ebf2017-08-01 11:09:09 -040056 , fViewMatrix(viewMatrix) {
msarettcc319b92016-08-25 18:07:18 -070057 RegionInfo& info = fRegions.push_back();
58 info.fColor = color;
msarettcc319b92016-08-25 18:07:18 -070059 info.fRegion = region;
60
61 SkRect bounds = SkRect::Make(region.getBounds());
Greg Daniel5faf4742019-10-01 15:14:44 -040062 this->setTransformedBounds(bounds, viewMatrix, HasAABloat::kNo, IsHairline::kNo);
msarettcc319b92016-08-25 18:07:18 -070063 }
64
Brian Salomonfc527d22016-12-14 21:07:01 -050065 const char* name() const override { return "GrRegionOp"; }
msarettcc319b92016-08-25 18:07:18 -070066
Chris Dalton1706cbf2019-05-21 19:35:29 -060067 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsd1a8af62020-03-10 12:50:49 -040068 if (fProgramInfo) {
Chris Daltonbe457422020-03-16 18:05:03 -060069 fProgramInfo->visitFPProxies(func);
Robert Phillipsd1a8af62020-03-10 12:50:49 -040070 } else {
71 fHelper.visitProxies(func);
72 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -040073 }
74
Brian Salomonf0366322017-07-11 15:53:05 -040075 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
76
Chris Dalton6ce447a2019-06-23 18:07:38 -060077 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 Salomonf0366322017-07-11 15:53:05 -040083 }
84
msarettcc319b92016-08-25 18:07:18 -070085private:
Robert Phillips2669a7b2020-03-12 12:07:19 -040086 GrProgramInfo* programInfo() override { return fProgramInfo; }
87
Robert Phillips4133dc42020-03-11 15:55:55 -040088 void onCreateProgramInfo(const GrCaps* caps,
89 SkArenaAlloc* arena,
Brian Salomon8afde5f2020-04-01 16:22:00 -040090 const GrSurfaceProxyView* writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -040091 GrAppliedClip&& appliedClip,
Greg Danield358cbe2020-09-11 09:33:54 -040092 const GrXferProcessor::DstProxyView& dstProxyView,
93 GrXferBarrierFlags renderPassXferBarriers) override {
Brian Osmanf0aee742020-03-12 09:28:44 -040094 GrGeometryProcessor* gp = make_gp(arena, fViewMatrix, fWideColor);
msarettcc319b92016-08-25 18:07:18 -070095 if (!gp) {
96 SkDebugf("Couldn't create GrGeometryProcessor\n");
Robert Phillips4133dc42020-03-11 15:55:55 -040097 return;
Robert Phillipsd1a8af62020-03-10 12:50:49 -040098 }
99
Brian Salomon8afde5f2020-04-01 16:22:00 -0400100 fProgramInfo = fHelper.createProgramInfoWithStencil(caps, arena, writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400101 std::move(appliedClip), dstProxyView,
Greg Danield358cbe2020-09-11 09:33:54 -0400102 gp, GrPrimitiveType::kTriangles,
103 renderPassXferBarriers);
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400104 }
105
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400106 void onPrepareDraws(Target* target) override {
107 if (!fProgramInfo) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400108 this->createProgramInfo(target);
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400109 if (!fProgramInfo) {
110 return;
111 }
msarettcc319b92016-08-25 18:07:18 -0700112 }
msarettcc319b92016-08-25 18:07:18 -0700113
114 int numRegions = fRegions.count();
115 int numRects = 0;
116 for (int i = 0; i < numRegions; i++) {
117 numRects += fRegions[i].fRegion.computeRegionComplexity();
118 }
119
Brian Salomonf0366322017-07-11 15:53:05 -0400120 if (!numRects) {
121 return;
122 }
Robert Phillipse94cdd22019-11-04 14:15:58 -0500123
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400124 QuadHelper helper(target, fProgramInfo->primProc().vertexStride(), numRects);
Robert Phillipse94cdd22019-11-04 14:15:58 -0500125
Brian Osman4486d982018-11-15 15:56:04 -0500126 GrVertexWriter vertices{helper.vertices()};
Brian Salomon12d22642019-01-29 14:38:50 -0500127 if (!vertices.fPtr) {
msarettcc319b92016-08-25 18:07:18 -0700128 SkDebugf("Could not allocate vertices\n");
129 return;
130 }
131
msarettcc319b92016-08-25 18:07:18 -0700132 for (int i = 0; i < numRegions; i++) {
Brian Osman7561dba2018-12-27 10:16:18 -0500133 GrVertexColor color(fRegions[i].fColor, fWideColor);
Brian Osman4486d982018-11-15 15:56:04 -0500134 SkRegion::Iterator iter(fRegions[i].fRegion);
135 while (!iter.done()) {
136 SkRect rect = SkRect::Make(iter.rect());
Brian Osman0dd43022018-11-16 15:53:26 -0500137 vertices.writeQuad(GrVertexWriter::TriStripFromRect(rect), color);
Brian Osman4486d982018-11-15 15:56:04 -0500138 iter.next();
139 }
msarettcc319b92016-08-25 18:07:18 -0700140 }
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400141
142 fMesh = helper.mesh();
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700143 }
144
145 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400146 if (!fProgramInfo || !fMesh) {
147 return;
148 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500149
Chris Dalton765ed362020-03-16 17:34:44 -0600150 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
151 flushState->bindTextures(fProgramInfo->primProc(), nullptr, fProgramInfo->pipeline());
152 flushState->drawMesh(*fMesh);
msarettcc319b92016-08-25 18:07:18 -0700153 }
154
Herb Derbye25c3002020-10-27 15:57:27 -0400155 CombineResult onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) override {
Brian Salomonfc527d22016-12-14 21:07:01 -0500156 RegionOp* that = t->cast<RegionOp>();
Brian Salomonf0366322017-07-11 15:53:05 -0400157 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000158 return CombineResult::kCannotCombine;
msarettcc319b92016-08-25 18:07:18 -0700159 }
160
msarettfebb2242016-08-26 12:49:27 -0700161 if (fViewMatrix != that->fViewMatrix) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000162 return CombineResult::kCannotCombine;
msarettfebb2242016-08-26 12:49:27 -0700163 }
164
msarettcc319b92016-08-25 18:07:18 -0700165 fRegions.push_back_n(that->fRegions.count(), that->fRegions.begin());
Brian Osman7561dba2018-12-27 10:16:18 -0500166 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000167 return CombineResult::kMerged;
msarettcc319b92016-08-25 18:07:18 -0700168 }
169
John Stilesaf366522020-08-13 09:57:34 -0400170#if GR_TEST_UTILS
171 SkString onDumpInfo() const override {
172 SkString str = SkStringPrintf("# combined: %d\n", fRegions.count());
173 for (int i = 0; i < fRegions.count(); ++i) {
174 const RegionInfo& info = fRegions[i];
175 str.appendf("%d: Color: 0x%08x, Region with %d rects\n", i, info.fColor.toBytes_RGBA(),
176 info.fRegion.computeRegionComplexity());
177 }
178 str += fHelper.dumpInfo();
179 return str;
180 }
181#endif
182
msarettcc319b92016-08-25 18:07:18 -0700183 struct RegionInfo {
Brian Osmancf860852018-10-31 14:04:39 -0400184 SkPMColor4f fColor;
msarettcc319b92016-08-25 18:07:18 -0700185 SkRegion fRegion;
186 };
187
Brian Salomonf0366322017-07-11 15:53:05 -0400188 Helper fHelper;
msarettfebb2242016-08-26 12:49:27 -0700189 SkMatrix fViewMatrix;
msarettcc319b92016-08-25 18:07:18 -0700190 SkSTArray<1, RegionInfo, true> fRegions;
Brian Osman7561dba2018-12-27 10:16:18 -0500191 bool fWideColor;
msarettcc319b92016-08-25 18:07:18 -0700192
Chris Daltoneb694b72020-03-16 09:25:50 -0600193 GrSimpleMesh* fMesh = nullptr;
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400194 GrProgramInfo* fProgramInfo = nullptr;
195
John Stiles7571f9e2020-09-02 22:42:33 -0400196 using INHERITED = GrMeshDrawOp;
msarettcc319b92016-08-25 18:07:18 -0700197};
198
Brian Salomonf0366322017-07-11 15:53:05 -0400199} // anonymous namespace
200
Brian Salomonfc527d22016-12-14 21:07:01 -0500201namespace GrRegionOp {
msarettcc319b92016-08-25 18:07:18 -0700202
Herb Derbyc76d4092020-10-07 16:46:15 -0400203GrOp::Owner Make(GrRecordingContext* context,
204 GrPaint&& paint,
205 const SkMatrix& viewMatrix,
206 const SkRegion& region,
207 GrAAType aaType,
208 const GrUserStencilSettings* stencilSettings) {
Brian Salomonf0366322017-07-11 15:53:05 -0400209 if (aaType != GrAAType::kNone && aaType != GrAAType::kMSAA) {
210 return nullptr;
211 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400212 return RegionOp::Make(context, std::move(paint), viewMatrix, region, aaType, stencilSettings);
msarettcc319b92016-08-25 18:07:18 -0700213}
John Stilesa6841be2020-08-06 14:11:56 -0400214} // namespace GrRegionOp
Brian Salomonf0366322017-07-11 15:53:05 -0400215
216#if GR_TEST_UTILS
217
218GR_DRAW_OP_TEST_DEFINE(RegionOp) {
219 SkRegion region;
220 int n = random->nextULessThan(200);
221 for (int i = 0; i < n; ++i) {
222 SkIPoint center;
223 center.fX = random->nextULessThan(1000);
224 center.fY = random->nextULessThan(1000);
225 int w = random->nextRangeU(10, 1000);
226 int h = random->nextRangeU(10, 1000);
227 SkIRect rect = {center.fX - w / 2, center.fY - h / 2, center.fX + w / 2, center.fY + h / 2};
228 SkRegion::Op op;
229 if (i == 0) {
230 op = SkRegion::kReplace_Op;
231 } else {
232 // Pick an other than replace.
Brian Salomon4dea72a2019-12-18 10:43:10 -0500233 static_assert(SkRegion::kLastOp == SkRegion::kReplace_Op);
Brian Salomonf0366322017-07-11 15:53:05 -0400234 op = (SkRegion::Op)random->nextULessThan(SkRegion::kLastOp);
235 }
236 region.op(rect, op);
237 }
238 SkMatrix viewMatrix = GrTest::TestMatrix(random);
239 GrAAType aaType = GrAAType::kNone;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600240 if (numSamples > 1 && random->nextBool()) {
Brian Salomonf0366322017-07-11 15:53:05 -0400241 aaType = GrAAType::kMSAA;
242 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400243 return RegionOp::Make(context, std::move(paint), viewMatrix, region, aaType,
Brian Salomon8514ebf2017-08-01 11:09:09 -0400244 GrGetRandomStencil(random, context));
Brian Salomonf0366322017-07-11 15:53:05 -0400245}
246
247#endif