blob: 72b4d1db906771f0a5a091449e2677f1a70cf2bb [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 Phillipsd1a8af62020-03-10 12:50:49 -0400102 GrProgramInfo* createProgramInfo(const GrCaps* caps,
103 SkArenaAlloc* arena,
104 const GrSurfaceProxyView* outputView,
105 GrAppliedClip&& appliedClip,
106 const GrXferProcessor::DstProxyView& dstProxyView) {
107
108 GrGeometryProcessor* gp = make_gp(arena, caps->shaderCaps(), fViewMatrix, fWideColor);
msarettcc319b92016-08-25 18:07:18 -0700109 if (!gp) {
110 SkDebugf("Couldn't create GrGeometryProcessor\n");
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400111 return nullptr;
112 }
113
114 return fHelper.createProgramInfoWithStencil(caps, arena, outputView,
115 std::move(appliedClip), dstProxyView,
116 gp, GrPrimitiveType::kTriangles);
117 }
118
119 GrProgramInfo* createProgramInfo(Target* target) {
120 return this->createProgramInfo(&target->caps(),
121 target->allocator(),
122 target->outputView(),
123 target->detachAppliedClip(),
124 target->dstProxyView());
125 }
126
127 void onPrePrepareDraws(GrRecordingContext* context,
128 const GrSurfaceProxyView* outputView,
129 GrAppliedClip* clip,
130 const GrXferProcessor::DstProxyView& dstProxyView) override {
131 SkArenaAlloc* arena = context->priv().recordTimeAllocator();
132
133 // This is equivalent to a GrOpFlushState::detachAppliedClip
134 GrAppliedClip appliedClip = clip ? std::move(*clip) : GrAppliedClip();
135
136 fProgramInfo = this->createProgramInfo(context->priv().caps(), arena, outputView,
137 std::move(appliedClip), dstProxyView);
138
139 context->priv().recordProgramInfo(fProgramInfo);
140 }
141
142 void onPrepareDraws(Target* target) override {
143 if (!fProgramInfo) {
144 fProgramInfo = this->createProgramInfo(target);
145 if (!fProgramInfo) {
146 return;
147 }
msarettcc319b92016-08-25 18:07:18 -0700148 }
msarettcc319b92016-08-25 18:07:18 -0700149
150 int numRegions = fRegions.count();
151 int numRects = 0;
152 for (int i = 0; i < numRegions; i++) {
153 numRects += fRegions[i].fRegion.computeRegionComplexity();
154 }
155
Brian Salomonf0366322017-07-11 15:53:05 -0400156 if (!numRects) {
157 return;
158 }
Robert Phillipse94cdd22019-11-04 14:15:58 -0500159
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400160 QuadHelper helper(target, fProgramInfo->primProc().vertexStride(), numRects);
Robert Phillipse94cdd22019-11-04 14:15:58 -0500161
Brian Osman4486d982018-11-15 15:56:04 -0500162 GrVertexWriter vertices{helper.vertices()};
Brian Salomon12d22642019-01-29 14:38:50 -0500163 if (!vertices.fPtr) {
msarettcc319b92016-08-25 18:07:18 -0700164 SkDebugf("Could not allocate vertices\n");
165 return;
166 }
167
msarettcc319b92016-08-25 18:07:18 -0700168 for (int i = 0; i < numRegions; i++) {
Brian Osman7561dba2018-12-27 10:16:18 -0500169 GrVertexColor color(fRegions[i].fColor, fWideColor);
Brian Osman4486d982018-11-15 15:56:04 -0500170 SkRegion::Iterator iter(fRegions[i].fRegion);
171 while (!iter.done()) {
172 SkRect rect = SkRect::Make(iter.rect());
Brian Osman0dd43022018-11-16 15:53:26 -0500173 vertices.writeQuad(GrVertexWriter::TriStripFromRect(rect), color);
Brian Osman4486d982018-11-15 15:56:04 -0500174 iter.next();
175 }
msarettcc319b92016-08-25 18:07:18 -0700176 }
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400177
178 fMesh = helper.mesh();
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700179 }
180
181 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400182 if (!fProgramInfo || !fMesh) {
183 return;
184 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500185
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400186 flushState->opsRenderPass()->bindPipeline(*fProgramInfo, chainBounds);
187 flushState->opsRenderPass()->drawMeshes(*fProgramInfo, fMesh, 1);
msarettcc319b92016-08-25 18:07:18 -0700188 }
189
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500190 CombineResult onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*,
191 const GrCaps& caps) override {
Brian Salomonfc527d22016-12-14 21:07:01 -0500192 RegionOp* that = t->cast<RegionOp>();
Brian Salomonf0366322017-07-11 15:53:05 -0400193 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000194 return CombineResult::kCannotCombine;
msarettcc319b92016-08-25 18:07:18 -0700195 }
196
msarettfebb2242016-08-26 12:49:27 -0700197 if (fViewMatrix != that->fViewMatrix) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000198 return CombineResult::kCannotCombine;
msarettfebb2242016-08-26 12:49:27 -0700199 }
200
msarettcc319b92016-08-25 18:07:18 -0700201 fRegions.push_back_n(that->fRegions.count(), that->fRegions.begin());
Brian Osman7561dba2018-12-27 10:16:18 -0500202 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000203 return CombineResult::kMerged;
msarettcc319b92016-08-25 18:07:18 -0700204 }
205
206 struct RegionInfo {
Brian Osmancf860852018-10-31 14:04:39 -0400207 SkPMColor4f fColor;
msarettcc319b92016-08-25 18:07:18 -0700208 SkRegion fRegion;
209 };
210
Brian Salomonf0366322017-07-11 15:53:05 -0400211 Helper fHelper;
msarettfebb2242016-08-26 12:49:27 -0700212 SkMatrix fViewMatrix;
msarettcc319b92016-08-25 18:07:18 -0700213 SkSTArray<1, RegionInfo, true> fRegions;
Brian Osman7561dba2018-12-27 10:16:18 -0500214 bool fWideColor;
msarettcc319b92016-08-25 18:07:18 -0700215
Robert Phillipsd1a8af62020-03-10 12:50:49 -0400216 GrMesh* fMesh = nullptr;
217 GrProgramInfo* fProgramInfo = nullptr;
218
Brian Salomonf0366322017-07-11 15:53:05 -0400219 typedef GrMeshDrawOp INHERITED;
msarettcc319b92016-08-25 18:07:18 -0700220};
221
Brian Salomonf0366322017-07-11 15:53:05 -0400222} // anonymous namespace
223
Brian Salomonfc527d22016-12-14 21:07:01 -0500224namespace GrRegionOp {
msarettcc319b92016-08-25 18:07:18 -0700225
Robert Phillipsb97da532019-02-12 15:24:12 -0500226std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400227 GrPaint&& paint,
228 const SkMatrix& viewMatrix,
229 const SkRegion& region,
230 GrAAType aaType,
231 const GrUserStencilSettings* stencilSettings) {
Brian Salomonf0366322017-07-11 15:53:05 -0400232 if (aaType != GrAAType::kNone && aaType != GrAAType::kMSAA) {
233 return nullptr;
234 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400235 return RegionOp::Make(context, std::move(paint), viewMatrix, region, aaType, stencilSettings);
msarettcc319b92016-08-25 18:07:18 -0700236}
Brian Salomonfc527d22016-12-14 21:07:01 -0500237}
Brian Salomonf0366322017-07-11 15:53:05 -0400238
239#if GR_TEST_UTILS
240
241GR_DRAW_OP_TEST_DEFINE(RegionOp) {
242 SkRegion region;
243 int n = random->nextULessThan(200);
244 for (int i = 0; i < n; ++i) {
245 SkIPoint center;
246 center.fX = random->nextULessThan(1000);
247 center.fY = random->nextULessThan(1000);
248 int w = random->nextRangeU(10, 1000);
249 int h = random->nextRangeU(10, 1000);
250 SkIRect rect = {center.fX - w / 2, center.fY - h / 2, center.fX + w / 2, center.fY + h / 2};
251 SkRegion::Op op;
252 if (i == 0) {
253 op = SkRegion::kReplace_Op;
254 } else {
255 // Pick an other than replace.
Brian Salomon4dea72a2019-12-18 10:43:10 -0500256 static_assert(SkRegion::kLastOp == SkRegion::kReplace_Op);
Brian Salomonf0366322017-07-11 15:53:05 -0400257 op = (SkRegion::Op)random->nextULessThan(SkRegion::kLastOp);
258 }
259 region.op(rect, op);
260 }
261 SkMatrix viewMatrix = GrTest::TestMatrix(random);
262 GrAAType aaType = GrAAType::kNone;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600263 if (numSamples > 1 && random->nextBool()) {
Brian Salomonf0366322017-07-11 15:53:05 -0400264 aaType = GrAAType::kMSAA;
265 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400266 return RegionOp::Make(context, std::move(paint), viewMatrix, region, aaType,
Brian Salomon8514ebf2017-08-01 11:09:09 -0400267 GrGetRandomStencil(random, context));
Brian Salomonf0366322017-07-11 15:53:05 -0400268}
269
270#endif