blob: 172d219fd8d34378d34bbe07f59ee8ac6ba4eadd [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,
23 const GrShaderCaps* shaderCaps,
24 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;
29 return GrDefaultGeoProcFactory::Make(arena, shaderCaps, colorType, Coverage::kSolid_Type,
Brian Osman7561dba2018-12-27 10:16:18 -050030 LocalCoords::kUsePosition_Type, viewMatrix);
msarettcc319b92016-08-25 18:07:18 -070031}
32
Brian Salomonf0366322017-07-11 15:53:05 -040033namespace {
34
35class RegionOp final : public GrMeshDrawOp {
36private:
Brian Salomon8514ebf2017-08-01 11:09:09 -040037 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
Brian Salomonf0366322017-07-11 15:53:05 -040038
msarettcc319b92016-08-25 18:07:18 -070039public:
Brian Salomon25a88092016-12-01 09:36:50 -050040 DEFINE_OP_CLASS_ID
msarettcc319b92016-08-25 18:07:18 -070041
Robert Phillipsb97da532019-02-12 15:24:12 -050042 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -040043 GrPaint&& paint,
44 const SkMatrix& viewMatrix,
45 const SkRegion& region,
46 GrAAType aaType,
Brian Salomon8514ebf2017-08-01 11:09:09 -040047 const GrUserStencilSettings* stencilSettings = nullptr) {
Robert Phillips7c525e62018-06-12 10:11:12 -040048 return Helper::FactoryHelper<RegionOp>(context, std::move(paint), viewMatrix, region,
49 aaType, stencilSettings);
Brian Salomonf0366322017-07-11 15:53:05 -040050 }
51
Brian Osmancf860852018-10-31 14:04:39 -040052 RegionOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color,
53 const SkMatrix& viewMatrix, const SkRegion& region, GrAAType aaType,
54 const GrUserStencilSettings* stencilSettings)
Brian Salomon8514ebf2017-08-01 11:09:09 -040055 : INHERITED(ClassID())
56 , fHelper(helperArgs, aaType, stencilSettings)
57 , fViewMatrix(viewMatrix) {
msarettcc319b92016-08-25 18:07:18 -070058 RegionInfo& info = fRegions.push_back();
59 info.fColor = color;
msarettcc319b92016-08-25 18:07:18 -070060 info.fRegion = region;
61
62 SkRect bounds = SkRect::Make(region.getBounds());
Greg Daniel5faf4742019-10-01 15:14:44 -040063 this->setTransformedBounds(bounds, viewMatrix, HasAABloat::kNo, IsHairline::kNo);
msarettcc319b92016-08-25 18:07:18 -070064 }
65
Brian Salomonfc527d22016-12-14 21:07:01 -050066 const char* name() const override { return "GrRegionOp"; }
msarettcc319b92016-08-25 18:07:18 -070067
Chris Dalton1706cbf2019-05-21 19:35:29 -060068 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsd1a8af62020-03-10 12:50:49 -040069 if (fProgramInfo) {
70 fProgramInfo->visitProxies(func);
71 } else {
72 fHelper.visitProxies(func);
73 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -040074 }
75
Brian Osman9a390ac2018-11-12 09:47:48 -050076#ifdef SK_DEBUG
msarettcc319b92016-08-25 18:07:18 -070077 SkString dumpInfo() const override {
78 SkString str;
Brian Salomon53e4c3c2016-12-21 11:38:53 -050079 str.appendf("# combined: %d\n", fRegions.count());
msarettcc319b92016-08-25 18:07:18 -070080 for (int i = 0; i < fRegions.count(); ++i) {
81 const RegionInfo& info = fRegions[i];
Brian Osmancf860852018-10-31 14:04:39 -040082 str.appendf("%d: Color: 0x%08x, Region with %d rects\n", i, info.fColor.toBytes_RGBA(),
Brian Salomonfc527d22016-12-14 21:07:01 -050083 info.fRegion.computeRegionComplexity());
msarettcc319b92016-08-25 18:07:18 -070084 }
Brian Salomonf0366322017-07-11 15:53:05 -040085 str += fHelper.dumpInfo();
86 str += INHERITED::dumpInfo();
msarettcc319b92016-08-25 18:07:18 -070087 return str;
88 }
Brian Osman9a390ac2018-11-12 09:47:48 -050089#endif
msarettcc319b92016-08-25 18:07:18 -070090
Brian Salomonf0366322017-07-11 15:53:05 -040091 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
92
Chris Dalton6ce447a2019-06-23 18:07:38 -060093 GrProcessorSet::Analysis finalize(
94 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
95 GrClampType clampType) override {
96 return fHelper.finalizeProcessors(
97 caps, clip, hasMixedSampledCoverage, clampType, GrProcessorAnalysisCoverage::kNone,
98 &fRegions[0].fColor, &fWideColor);
Brian Salomonf0366322017-07-11 15:53:05 -040099 }
100
msarettcc319b92016-08-25 18:07:18 -0700101private:
Robert Phillips4133dc42020-03-11 15:55:55 -0400102 void onCreateProgramInfo(const GrCaps* caps,
103 SkArenaAlloc* arena,
104 const GrSurfaceProxyView* outputView,
105 GrAppliedClip&& appliedClip,
106 const GrXferProcessor::DstProxyView& dstProxyView) override {
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400107 GrGeometryProcessor* gp = make_gp(arena, caps->shaderCaps(), fViewMatrix, fWideColor);
msarettcc319b92016-08-25 18:07:18 -0700108 if (!gp) {
109 SkDebugf("Couldn't create GrGeometryProcessor\n");
Robert Phillips4133dc42020-03-11 15:55:55 -0400110 return;
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400111 }
112
Robert Phillips4133dc42020-03-11 15:55:55 -0400113 fProgramInfo = fHelper.createProgramInfoWithStencil(caps, arena, outputView,
114 std::move(appliedClip), dstProxyView,
115 gp, GrPrimitiveType::kTriangles);
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400116 }
117
118 void onPrePrepareDraws(GrRecordingContext* context,
119 const GrSurfaceProxyView* outputView,
120 GrAppliedClip* clip,
121 const GrXferProcessor::DstProxyView& dstProxyView) override {
122 SkArenaAlloc* arena = context->priv().recordTimeAllocator();
123
124 // This is equivalent to a GrOpFlushState::detachAppliedClip
125 GrAppliedClip appliedClip = clip ? std::move(*clip) : GrAppliedClip();
126
Robert Phillips4133dc42020-03-11 15:55:55 -0400127 this->createProgramInfo(context->priv().caps(), arena, outputView,
128 std::move(appliedClip), dstProxyView);
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400129
130 context->priv().recordProgramInfo(fProgramInfo);
131 }
132
133 void onPrepareDraws(Target* target) override {
134 if (!fProgramInfo) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400135 this->createProgramInfo(target);
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400136 if (!fProgramInfo) {
137 return;
138 }
msarettcc319b92016-08-25 18:07:18 -0700139 }
msarettcc319b92016-08-25 18:07:18 -0700140
141 int numRegions = fRegions.count();
142 int numRects = 0;
143 for (int i = 0; i < numRegions; i++) {
144 numRects += fRegions[i].fRegion.computeRegionComplexity();
145 }
146
Brian Salomonf0366322017-07-11 15:53:05 -0400147 if (!numRects) {
148 return;
149 }
Robert Phillipse94cdd22019-11-04 14:15:58 -0500150
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400151 QuadHelper helper(target, fProgramInfo->primProc().vertexStride(), numRects);
Robert Phillipse94cdd22019-11-04 14:15:58 -0500152
Brian Osman4486d982018-11-15 15:56:04 -0500153 GrVertexWriter vertices{helper.vertices()};
Brian Salomon12d22642019-01-29 14:38:50 -0500154 if (!vertices.fPtr) {
msarettcc319b92016-08-25 18:07:18 -0700155 SkDebugf("Could not allocate vertices\n");
156 return;
157 }
158
msarettcc319b92016-08-25 18:07:18 -0700159 for (int i = 0; i < numRegions; i++) {
Brian Osman7561dba2018-12-27 10:16:18 -0500160 GrVertexColor color(fRegions[i].fColor, fWideColor);
Brian Osman4486d982018-11-15 15:56:04 -0500161 SkRegion::Iterator iter(fRegions[i].fRegion);
162 while (!iter.done()) {
163 SkRect rect = SkRect::Make(iter.rect());
Brian Osman0dd43022018-11-16 15:53:26 -0500164 vertices.writeQuad(GrVertexWriter::TriStripFromRect(rect), color);
Brian Osman4486d982018-11-15 15:56:04 -0500165 iter.next();
166 }
msarettcc319b92016-08-25 18:07:18 -0700167 }
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400168
169 fMesh = helper.mesh();
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700170 }
171
172 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400173 if (!fProgramInfo || !fMesh) {
174 return;
175 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500176
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400177 flushState->opsRenderPass()->bindPipeline(*fProgramInfo, chainBounds);
178 flushState->opsRenderPass()->drawMeshes(*fProgramInfo, fMesh, 1);
msarettcc319b92016-08-25 18:07:18 -0700179 }
180
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500181 CombineResult onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*,
182 const GrCaps& caps) override {
Brian Salomonfc527d22016-12-14 21:07:01 -0500183 RegionOp* that = t->cast<RegionOp>();
Brian Salomonf0366322017-07-11 15:53:05 -0400184 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000185 return CombineResult::kCannotCombine;
msarettcc319b92016-08-25 18:07:18 -0700186 }
187
msarettfebb2242016-08-26 12:49:27 -0700188 if (fViewMatrix != that->fViewMatrix) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000189 return CombineResult::kCannotCombine;
msarettfebb2242016-08-26 12:49:27 -0700190 }
191
msarettcc319b92016-08-25 18:07:18 -0700192 fRegions.push_back_n(that->fRegions.count(), that->fRegions.begin());
Brian Osman7561dba2018-12-27 10:16:18 -0500193 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000194 return CombineResult::kMerged;
msarettcc319b92016-08-25 18:07:18 -0700195 }
196
197 struct RegionInfo {
Brian Osmancf860852018-10-31 14:04:39 -0400198 SkPMColor4f fColor;
msarettcc319b92016-08-25 18:07:18 -0700199 SkRegion fRegion;
200 };
201
Brian Salomonf0366322017-07-11 15:53:05 -0400202 Helper fHelper;
msarettfebb2242016-08-26 12:49:27 -0700203 SkMatrix fViewMatrix;
msarettcc319b92016-08-25 18:07:18 -0700204 SkSTArray<1, RegionInfo, true> fRegions;
Brian Osman7561dba2018-12-27 10:16:18 -0500205 bool fWideColor;
msarettcc319b92016-08-25 18:07:18 -0700206
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400207 GrMesh* fMesh = nullptr;
208 GrProgramInfo* fProgramInfo = nullptr;
209
Brian Salomonf0366322017-07-11 15:53:05 -0400210 typedef GrMeshDrawOp INHERITED;
msarettcc319b92016-08-25 18:07:18 -0700211};
212
Brian Salomonf0366322017-07-11 15:53:05 -0400213} // anonymous namespace
214
Brian Salomonfc527d22016-12-14 21:07:01 -0500215namespace GrRegionOp {
msarettcc319b92016-08-25 18:07:18 -0700216
Robert Phillipsb97da532019-02-12 15:24:12 -0500217std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400218 GrPaint&& paint,
219 const SkMatrix& viewMatrix,
220 const SkRegion& region,
221 GrAAType aaType,
222 const GrUserStencilSettings* stencilSettings) {
Brian Salomonf0366322017-07-11 15:53:05 -0400223 if (aaType != GrAAType::kNone && aaType != GrAAType::kMSAA) {
224 return nullptr;
225 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400226 return RegionOp::Make(context, std::move(paint), viewMatrix, region, aaType, stencilSettings);
msarettcc319b92016-08-25 18:07:18 -0700227}
Brian Salomonfc527d22016-12-14 21:07:01 -0500228}
Brian Salomonf0366322017-07-11 15:53:05 -0400229
230#if GR_TEST_UTILS
231
232GR_DRAW_OP_TEST_DEFINE(RegionOp) {
233 SkRegion region;
234 int n = random->nextULessThan(200);
235 for (int i = 0; i < n; ++i) {
236 SkIPoint center;
237 center.fX = random->nextULessThan(1000);
238 center.fY = random->nextULessThan(1000);
239 int w = random->nextRangeU(10, 1000);
240 int h = random->nextRangeU(10, 1000);
241 SkIRect rect = {center.fX - w / 2, center.fY - h / 2, center.fX + w / 2, center.fY + h / 2};
242 SkRegion::Op op;
243 if (i == 0) {
244 op = SkRegion::kReplace_Op;
245 } else {
246 // Pick an other than replace.
Brian Salomon4dea72a2019-12-18 10:43:10 -0500247 static_assert(SkRegion::kLastOp == SkRegion::kReplace_Op);
Brian Salomonf0366322017-07-11 15:53:05 -0400248 op = (SkRegion::Op)random->nextULessThan(SkRegion::kLastOp);
249 }
250 region.op(rect, op);
251 }
252 SkMatrix viewMatrix = GrTest::TestMatrix(random);
253 GrAAType aaType = GrAAType::kNone;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600254 if (numSamples > 1 && random->nextBool()) {
Brian Salomonf0366322017-07-11 15:53:05 -0400255 aaType = GrAAType::kMSAA;
256 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400257 return RegionOp::Make(context, std::move(paint), viewMatrix, region, aaType,
Brian Salomon8514ebf2017-08-01 11:09:09 -0400258 GrGetRandomStencil(random, context));
Brian Salomonf0366322017-07-11 15:53:05 -0400259}
260
261#endif