blob: 4a097a571052ca7082316a589363ed40dc1d7884 [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
Robert Phillipsb97da532019-02-12 15:24:12 -050041 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -040042 GrPaint&& paint,
43 const SkMatrix& viewMatrix,
44 const SkRegion& region,
45 GrAAType aaType,
Brian Salomon8514ebf2017-08-01 11:09:09 -040046 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
Brian Osmancf860852018-10-31 14:04:39 -040051 RegionOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color,
52 const SkMatrix& viewMatrix, const SkRegion& region, GrAAType aaType,
53 const GrUserStencilSettings* stencilSettings)
Brian Salomon8514ebf2017-08-01 11:09:09 -040054 : INHERITED(ClassID())
55 , fHelper(helperArgs, aaType, stencilSettings)
56 , 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) {
69 fProgramInfo->visitProxies(func);
70 } else {
71 fHelper.visitProxies(func);
72 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -040073 }
74
Brian Osman9a390ac2018-11-12 09:47:48 -050075#ifdef SK_DEBUG
msarettcc319b92016-08-25 18:07:18 -070076 SkString dumpInfo() const override {
77 SkString str;
Brian Salomon53e4c3c2016-12-21 11:38:53 -050078 str.appendf("# combined: %d\n", fRegions.count());
msarettcc319b92016-08-25 18:07:18 -070079 for (int i = 0; i < fRegions.count(); ++i) {
80 const RegionInfo& info = fRegions[i];
Brian Osmancf860852018-10-31 14:04:39 -040081 str.appendf("%d: Color: 0x%08x, Region with %d rects\n", i, info.fColor.toBytes_RGBA(),
Brian Salomonfc527d22016-12-14 21:07:01 -050082 info.fRegion.computeRegionComplexity());
msarettcc319b92016-08-25 18:07:18 -070083 }
Brian Salomonf0366322017-07-11 15:53:05 -040084 str += fHelper.dumpInfo();
85 str += INHERITED::dumpInfo();
msarettcc319b92016-08-25 18:07:18 -070086 return str;
87 }
Brian Osman9a390ac2018-11-12 09:47:48 -050088#endif
msarettcc319b92016-08-25 18:07:18 -070089
Brian Salomonf0366322017-07-11 15:53:05 -040090 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
91
Chris Dalton6ce447a2019-06-23 18:07:38 -060092 GrProcessorSet::Analysis finalize(
93 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
94 GrClampType clampType) override {
95 return fHelper.finalizeProcessors(
96 caps, clip, hasMixedSampledCoverage, clampType, GrProcessorAnalysisCoverage::kNone,
97 &fRegions[0].fColor, &fWideColor);
Brian Salomonf0366322017-07-11 15:53:05 -040098 }
99
msarettcc319b92016-08-25 18:07:18 -0700100private:
Robert Phillips2669a7b2020-03-12 12:07:19 -0400101 GrProgramInfo* programInfo() override { return fProgramInfo; }
102
Robert Phillips4133dc42020-03-11 15:55:55 -0400103 void onCreateProgramInfo(const GrCaps* caps,
104 SkArenaAlloc* arena,
105 const GrSurfaceProxyView* outputView,
106 GrAppliedClip&& appliedClip,
107 const GrXferProcessor::DstProxyView& dstProxyView) override {
Brian Osmanf0aee742020-03-12 09:28:44 -0400108 GrGeometryProcessor* gp = make_gp(arena, fViewMatrix, fWideColor);
msarettcc319b92016-08-25 18:07:18 -0700109 if (!gp) {
110 SkDebugf("Couldn't create GrGeometryProcessor\n");
Robert Phillips4133dc42020-03-11 15:55:55 -0400111 return;
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400112 }
113
Robert Phillips4133dc42020-03-11 15:55:55 -0400114 fProgramInfo = fHelper.createProgramInfoWithStencil(caps, arena, outputView,
115 std::move(appliedClip), dstProxyView,
116 gp, GrPrimitiveType::kTriangles);
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400117 }
118
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400119 void onPrepareDraws(Target* target) override {
120 if (!fProgramInfo) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400121 this->createProgramInfo(target);
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400122 if (!fProgramInfo) {
123 return;
124 }
msarettcc319b92016-08-25 18:07:18 -0700125 }
msarettcc319b92016-08-25 18:07:18 -0700126
127 int numRegions = fRegions.count();
128 int numRects = 0;
129 for (int i = 0; i < numRegions; i++) {
130 numRects += fRegions[i].fRegion.computeRegionComplexity();
131 }
132
Brian Salomonf0366322017-07-11 15:53:05 -0400133 if (!numRects) {
134 return;
135 }
Robert Phillipse94cdd22019-11-04 14:15:58 -0500136
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400137 QuadHelper helper(target, fProgramInfo->primProc().vertexStride(), numRects);
Robert Phillipse94cdd22019-11-04 14:15:58 -0500138
Brian Osman4486d982018-11-15 15:56:04 -0500139 GrVertexWriter vertices{helper.vertices()};
Brian Salomon12d22642019-01-29 14:38:50 -0500140 if (!vertices.fPtr) {
msarettcc319b92016-08-25 18:07:18 -0700141 SkDebugf("Could not allocate vertices\n");
142 return;
143 }
144
msarettcc319b92016-08-25 18:07:18 -0700145 for (int i = 0; i < numRegions; i++) {
Brian Osman7561dba2018-12-27 10:16:18 -0500146 GrVertexColor color(fRegions[i].fColor, fWideColor);
Brian Osman4486d982018-11-15 15:56:04 -0500147 SkRegion::Iterator iter(fRegions[i].fRegion);
148 while (!iter.done()) {
149 SkRect rect = SkRect::Make(iter.rect());
Brian Osman0dd43022018-11-16 15:53:26 -0500150 vertices.writeQuad(GrVertexWriter::TriStripFromRect(rect), color);
Brian Osman4486d982018-11-15 15:56:04 -0500151 iter.next();
152 }
msarettcc319b92016-08-25 18:07:18 -0700153 }
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400154
155 fMesh = helper.mesh();
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700156 }
157
158 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400159 if (!fProgramInfo || !fMesh) {
160 return;
161 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500162
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400163 flushState->opsRenderPass()->bindPipeline(*fProgramInfo, chainBounds);
164 flushState->opsRenderPass()->drawMeshes(*fProgramInfo, fMesh, 1);
msarettcc319b92016-08-25 18:07:18 -0700165 }
166
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500167 CombineResult onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*,
168 const GrCaps& caps) override {
Brian Salomonfc527d22016-12-14 21:07:01 -0500169 RegionOp* that = t->cast<RegionOp>();
Brian Salomonf0366322017-07-11 15:53:05 -0400170 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000171 return CombineResult::kCannotCombine;
msarettcc319b92016-08-25 18:07:18 -0700172 }
173
msarettfebb2242016-08-26 12:49:27 -0700174 if (fViewMatrix != that->fViewMatrix) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000175 return CombineResult::kCannotCombine;
msarettfebb2242016-08-26 12:49:27 -0700176 }
177
msarettcc319b92016-08-25 18:07:18 -0700178 fRegions.push_back_n(that->fRegions.count(), that->fRegions.begin());
Brian Osman7561dba2018-12-27 10:16:18 -0500179 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000180 return CombineResult::kMerged;
msarettcc319b92016-08-25 18:07:18 -0700181 }
182
183 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
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400193 GrMesh* fMesh = nullptr;
194 GrProgramInfo* fProgramInfo = nullptr;
195
Brian Salomonf0366322017-07-11 15:53:05 -0400196 typedef GrMeshDrawOp INHERITED;
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
Robert Phillipsb97da532019-02-12 15:24:12 -0500203std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400204 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}
Brian Salomonfc527d22016-12-14 21:07:01 -0500214}
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