blob: 74b264fbf97e17a12a0181ad6647476fa908647f [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 Phillips4133dc42020-03-11 15:55:55 -0400101 void onCreateProgramInfo(const GrCaps* caps,
102 SkArenaAlloc* arena,
103 const GrSurfaceProxyView* outputView,
104 GrAppliedClip&& appliedClip,
105 const GrXferProcessor::DstProxyView& dstProxyView) override {
Brian Osmanf0aee742020-03-12 09:28:44 -0400106 GrGeometryProcessor* gp = make_gp(arena, fViewMatrix, fWideColor);
msarettcc319b92016-08-25 18:07:18 -0700107 if (!gp) {
108 SkDebugf("Couldn't create GrGeometryProcessor\n");
Robert Phillips4133dc42020-03-11 15:55:55 -0400109 return;
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400110 }
111
Robert Phillips4133dc42020-03-11 15:55:55 -0400112 fProgramInfo = fHelper.createProgramInfoWithStencil(caps, arena, outputView,
113 std::move(appliedClip), dstProxyView,
114 gp, GrPrimitiveType::kTriangles);
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400115 }
116
117 void onPrePrepareDraws(GrRecordingContext* context,
118 const GrSurfaceProxyView* outputView,
119 GrAppliedClip* clip,
120 const GrXferProcessor::DstProxyView& dstProxyView) override {
121 SkArenaAlloc* arena = context->priv().recordTimeAllocator();
122
123 // This is equivalent to a GrOpFlushState::detachAppliedClip
124 GrAppliedClip appliedClip = clip ? std::move(*clip) : GrAppliedClip();
125
Robert Phillips4133dc42020-03-11 15:55:55 -0400126 this->createProgramInfo(context->priv().caps(), arena, outputView,
127 std::move(appliedClip), dstProxyView);
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400128
129 context->priv().recordProgramInfo(fProgramInfo);
130 }
131
132 void onPrepareDraws(Target* target) override {
133 if (!fProgramInfo) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400134 this->createProgramInfo(target);
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400135 if (!fProgramInfo) {
136 return;
137 }
msarettcc319b92016-08-25 18:07:18 -0700138 }
msarettcc319b92016-08-25 18:07:18 -0700139
140 int numRegions = fRegions.count();
141 int numRects = 0;
142 for (int i = 0; i < numRegions; i++) {
143 numRects += fRegions[i].fRegion.computeRegionComplexity();
144 }
145
Brian Salomonf0366322017-07-11 15:53:05 -0400146 if (!numRects) {
147 return;
148 }
Robert Phillipse94cdd22019-11-04 14:15:58 -0500149
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400150 QuadHelper helper(target, fProgramInfo->primProc().vertexStride(), numRects);
Robert Phillipse94cdd22019-11-04 14:15:58 -0500151
Brian Osman4486d982018-11-15 15:56:04 -0500152 GrVertexWriter vertices{helper.vertices()};
Brian Salomon12d22642019-01-29 14:38:50 -0500153 if (!vertices.fPtr) {
msarettcc319b92016-08-25 18:07:18 -0700154 SkDebugf("Could not allocate vertices\n");
155 return;
156 }
157
msarettcc319b92016-08-25 18:07:18 -0700158 for (int i = 0; i < numRegions; i++) {
Brian Osman7561dba2018-12-27 10:16:18 -0500159 GrVertexColor color(fRegions[i].fColor, fWideColor);
Brian Osman4486d982018-11-15 15:56:04 -0500160 SkRegion::Iterator iter(fRegions[i].fRegion);
161 while (!iter.done()) {
162 SkRect rect = SkRect::Make(iter.rect());
Brian Osman0dd43022018-11-16 15:53:26 -0500163 vertices.writeQuad(GrVertexWriter::TriStripFromRect(rect), color);
Brian Osman4486d982018-11-15 15:56:04 -0500164 iter.next();
165 }
msarettcc319b92016-08-25 18:07:18 -0700166 }
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400167
168 fMesh = helper.mesh();
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700169 }
170
171 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400172 if (!fProgramInfo || !fMesh) {
173 return;
174 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500175
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400176 flushState->opsRenderPass()->bindPipeline(*fProgramInfo, chainBounds);
177 flushState->opsRenderPass()->drawMeshes(*fProgramInfo, fMesh, 1);
msarettcc319b92016-08-25 18:07:18 -0700178 }
179
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500180 CombineResult onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*,
181 const GrCaps& caps) override {
Brian Salomonfc527d22016-12-14 21:07:01 -0500182 RegionOp* that = t->cast<RegionOp>();
Brian Salomonf0366322017-07-11 15:53:05 -0400183 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000184 return CombineResult::kCannotCombine;
msarettcc319b92016-08-25 18:07:18 -0700185 }
186
msarettfebb2242016-08-26 12:49:27 -0700187 if (fViewMatrix != that->fViewMatrix) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000188 return CombineResult::kCannotCombine;
msarettfebb2242016-08-26 12:49:27 -0700189 }
190
msarettcc319b92016-08-25 18:07:18 -0700191 fRegions.push_back_n(that->fRegions.count(), that->fRegions.begin());
Brian Osman7561dba2018-12-27 10:16:18 -0500192 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000193 return CombineResult::kMerged;
msarettcc319b92016-08-25 18:07:18 -0700194 }
195
196 struct RegionInfo {
Brian Osmancf860852018-10-31 14:04:39 -0400197 SkPMColor4f fColor;
msarettcc319b92016-08-25 18:07:18 -0700198 SkRegion fRegion;
199 };
200
Brian Salomonf0366322017-07-11 15:53:05 -0400201 Helper fHelper;
msarettfebb2242016-08-26 12:49:27 -0700202 SkMatrix fViewMatrix;
msarettcc319b92016-08-25 18:07:18 -0700203 SkSTArray<1, RegionInfo, true> fRegions;
Brian Osman7561dba2018-12-27 10:16:18 -0500204 bool fWideColor;
msarettcc319b92016-08-25 18:07:18 -0700205
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400206 GrMesh* fMesh = nullptr;
207 GrProgramInfo* fProgramInfo = nullptr;
208
Brian Salomonf0366322017-07-11 15:53:05 -0400209 typedef GrMeshDrawOp INHERITED;
msarettcc319b92016-08-25 18:07:18 -0700210};
211
Brian Salomonf0366322017-07-11 15:53:05 -0400212} // anonymous namespace
213
Brian Salomonfc527d22016-12-14 21:07:01 -0500214namespace GrRegionOp {
msarettcc319b92016-08-25 18:07:18 -0700215
Robert Phillipsb97da532019-02-12 15:24:12 -0500216std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400217 GrPaint&& paint,
218 const SkMatrix& viewMatrix,
219 const SkRegion& region,
220 GrAAType aaType,
221 const GrUserStencilSettings* stencilSettings) {
Brian Salomonf0366322017-07-11 15:53:05 -0400222 if (aaType != GrAAType::kNone && aaType != GrAAType::kMSAA) {
223 return nullptr;
224 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400225 return RegionOp::Make(context, std::move(paint), viewMatrix, region, aaType, stencilSettings);
msarettcc319b92016-08-25 18:07:18 -0700226}
Brian Salomonfc527d22016-12-14 21:07:01 -0500227}
Brian Salomonf0366322017-07-11 15:53:05 -0400228
229#if GR_TEST_UTILS
230
231GR_DRAW_OP_TEST_DEFINE(RegionOp) {
232 SkRegion region;
233 int n = random->nextULessThan(200);
234 for (int i = 0; i < n; ++i) {
235 SkIPoint center;
236 center.fX = random->nextULessThan(1000);
237 center.fY = random->nextULessThan(1000);
238 int w = random->nextRangeU(10, 1000);
239 int h = random->nextRangeU(10, 1000);
240 SkIRect rect = {center.fX - w / 2, center.fY - h / 2, center.fX + w / 2, center.fY + h / 2};
241 SkRegion::Op op;
242 if (i == 0) {
243 op = SkRegion::kReplace_Op;
244 } else {
245 // Pick an other than replace.
Brian Salomon4dea72a2019-12-18 10:43:10 -0500246 static_assert(SkRegion::kLastOp == SkRegion::kReplace_Op);
Brian Salomonf0366322017-07-11 15:53:05 -0400247 op = (SkRegion::Op)random->nextULessThan(SkRegion::kLastOp);
248 }
249 region.op(rect, op);
250 }
251 SkMatrix viewMatrix = GrTest::TestMatrix(random);
252 GrAAType aaType = GrAAType::kNone;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600253 if (numSamples > 1 && random->nextBool()) {
Brian Salomonf0366322017-07-11 15:53:05 -0400254 aaType = GrAAType::kMSAA;
255 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400256 return RegionOp::Make(context, std::move(paint), viewMatrix, region, aaType,
Brian Salomon8514ebf2017-08-01 11:09:09 -0400257 GrGetRandomStencil(random, context));
Brian Salomonf0366322017-07-11 15:53:05 -0400258}
259
260#endif