blob: fd05bbe79d5614b5802c6c649951e72beb58958b [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"
16#include "src/gpu/GrResourceProvider.h"
17#include "src/gpu/GrVertexWriter.h"
18#include "src/gpu/ops/GrMeshDrawOp.h"
19#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
msarettcc319b92016-08-25 18:07:18 -070020
Ruiqi Maob609e6d2018-07-17 10:19:38 -040021static sk_sp<GrGeometryProcessor> make_gp(const GrShaderCaps* shaderCaps,
Brian Osman7561dba2018-12-27 10:16:18 -050022 const SkMatrix& viewMatrix,
23 bool wideColor) {
msarettcc319b92016-08-25 18:07:18 -070024 using namespace GrDefaultGeoProcFactory;
Brian Osman7561dba2018-12-27 10:16:18 -050025 Color::Type colorType =
26 wideColor ? Color::kPremulWideColorAttribute_Type : Color::kPremulGrColorAttribute_Type;
27 return GrDefaultGeoProcFactory::Make(shaderCaps, colorType, Coverage::kSolid_Type,
28 LocalCoords::kUsePosition_Type, viewMatrix);
msarettcc319b92016-08-25 18:07:18 -070029}
30
Brian Salomonf0366322017-07-11 15:53:05 -040031namespace {
32
33class RegionOp final : public GrMeshDrawOp {
34private:
Brian Salomon8514ebf2017-08-01 11:09:09 -040035 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
Brian Salomonf0366322017-07-11 15:53:05 -040036
msarettcc319b92016-08-25 18:07:18 -070037public:
Brian Salomon25a88092016-12-01 09:36:50 -050038 DEFINE_OP_CLASS_ID
msarettcc319b92016-08-25 18:07:18 -070039
Robert Phillipsb97da532019-02-12 15:24:12 -050040 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -040041 GrPaint&& paint,
42 const SkMatrix& viewMatrix,
43 const SkRegion& region,
44 GrAAType aaType,
Brian Salomon8514ebf2017-08-01 11:09:09 -040045 const GrUserStencilSettings* stencilSettings = nullptr) {
Robert Phillips7c525e62018-06-12 10:11:12 -040046 return Helper::FactoryHelper<RegionOp>(context, std::move(paint), viewMatrix, region,
47 aaType, stencilSettings);
Brian Salomonf0366322017-07-11 15:53:05 -040048 }
49
Brian Osmancf860852018-10-31 14:04:39 -040050 RegionOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color,
51 const SkMatrix& viewMatrix, const SkRegion& region, GrAAType aaType,
52 const GrUserStencilSettings* stencilSettings)
Brian Salomon8514ebf2017-08-01 11:09:09 -040053 : INHERITED(ClassID())
54 , fHelper(helperArgs, aaType, stencilSettings)
55 , fViewMatrix(viewMatrix) {
msarettcc319b92016-08-25 18:07:18 -070056 RegionInfo& info = fRegions.push_back();
57 info.fColor = color;
msarettcc319b92016-08-25 18:07:18 -070058 info.fRegion = region;
59
60 SkRect bounds = SkRect::Make(region.getBounds());
Greg Daniel5faf4742019-10-01 15:14:44 -040061 this->setTransformedBounds(bounds, viewMatrix, HasAABloat::kNo, IsHairline::kNo);
msarettcc319b92016-08-25 18:07:18 -070062 }
63
Brian Salomonfc527d22016-12-14 21:07:01 -050064 const char* name() const override { return "GrRegionOp"; }
msarettcc319b92016-08-25 18:07:18 -070065
Chris Dalton1706cbf2019-05-21 19:35:29 -060066 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -040067 fHelper.visitProxies(func);
68 }
69
Brian Osman9a390ac2018-11-12 09:47:48 -050070#ifdef SK_DEBUG
msarettcc319b92016-08-25 18:07:18 -070071 SkString dumpInfo() const override {
72 SkString str;
Brian Salomon53e4c3c2016-12-21 11:38:53 -050073 str.appendf("# combined: %d\n", fRegions.count());
msarettcc319b92016-08-25 18:07:18 -070074 for (int i = 0; i < fRegions.count(); ++i) {
75 const RegionInfo& info = fRegions[i];
Brian Osmancf860852018-10-31 14:04:39 -040076 str.appendf("%d: Color: 0x%08x, Region with %d rects\n", i, info.fColor.toBytes_RGBA(),
Brian Salomonfc527d22016-12-14 21:07:01 -050077 info.fRegion.computeRegionComplexity());
msarettcc319b92016-08-25 18:07:18 -070078 }
Brian Salomonf0366322017-07-11 15:53:05 -040079 str += fHelper.dumpInfo();
80 str += INHERITED::dumpInfo();
msarettcc319b92016-08-25 18:07:18 -070081 return str;
82 }
Brian Osman9a390ac2018-11-12 09:47:48 -050083#endif
msarettcc319b92016-08-25 18:07:18 -070084
Brian Salomonf0366322017-07-11 15:53:05 -040085 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
86
Chris Dalton6ce447a2019-06-23 18:07:38 -060087 GrProcessorSet::Analysis finalize(
88 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
89 GrClampType clampType) override {
90 return fHelper.finalizeProcessors(
91 caps, clip, hasMixedSampledCoverage, clampType, GrProcessorAnalysisCoverage::kNone,
92 &fRegions[0].fColor, &fWideColor);
Brian Salomonf0366322017-07-11 15:53:05 -040093 }
94
msarettcc319b92016-08-25 18:07:18 -070095private:
Brian Salomon91326c32017-08-09 16:02:19 -040096 void onPrepareDraws(Target* target) override {
Brian Osman7561dba2018-12-27 10:16:18 -050097 sk_sp<GrGeometryProcessor> gp = make_gp(target->caps().shaderCaps(), fViewMatrix,
98 fWideColor);
msarettcc319b92016-08-25 18:07:18 -070099 if (!gp) {
100 SkDebugf("Couldn't create GrGeometryProcessor\n");
101 return;
102 }
msarettcc319b92016-08-25 18:07:18 -0700103
104 int numRegions = fRegions.count();
105 int numRects = 0;
106 for (int i = 0; i < numRegions; i++) {
107 numRects += fRegions[i].fRegion.computeRegionComplexity();
108 }
109
Brian Salomonf0366322017-07-11 15:53:05 -0400110 if (!numRects) {
111 return;
112 }
Robert Phillipse94cdd22019-11-04 14:15:58 -0500113
114 QuadHelper helper(target, gp->vertexStride(), numRects);
115
Brian Osman4486d982018-11-15 15:56:04 -0500116 GrVertexWriter vertices{helper.vertices()};
Brian Salomon12d22642019-01-29 14:38:50 -0500117 if (!vertices.fPtr) {
msarettcc319b92016-08-25 18:07:18 -0700118 SkDebugf("Could not allocate vertices\n");
119 return;
120 }
121
msarettcc319b92016-08-25 18:07:18 -0700122 for (int i = 0; i < numRegions; i++) {
Brian Osman7561dba2018-12-27 10:16:18 -0500123 GrVertexColor color(fRegions[i].fColor, fWideColor);
Brian Osman4486d982018-11-15 15:56:04 -0500124 SkRegion::Iterator iter(fRegions[i].fRegion);
125 while (!iter.done()) {
126 SkRect rect = SkRect::Make(iter.rect());
Brian Osman0dd43022018-11-16 15:53:26 -0500127 vertices.writeQuad(GrVertexWriter::TriStripFromRect(rect), color);
Brian Osman4486d982018-11-15 15:56:04 -0500128 iter.next();
129 }
msarettcc319b92016-08-25 18:07:18 -0700130 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700131 helper.recordDraw(target, std::move(gp));
132 }
133
134 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
135 fHelper.executeDrawsAndUploads(this, flushState, chainBounds);
msarettcc319b92016-08-25 18:07:18 -0700136 }
137
Brian Salomon7eae3e02018-08-07 14:02:38 +0000138 CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Brian Salomonfc527d22016-12-14 21:07:01 -0500139 RegionOp* that = t->cast<RegionOp>();
Brian Salomonf0366322017-07-11 15:53:05 -0400140 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000141 return CombineResult::kCannotCombine;
msarettcc319b92016-08-25 18:07:18 -0700142 }
143
msarettfebb2242016-08-26 12:49:27 -0700144 if (fViewMatrix != that->fViewMatrix) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000145 return CombineResult::kCannotCombine;
msarettfebb2242016-08-26 12:49:27 -0700146 }
147
msarettcc319b92016-08-25 18:07:18 -0700148 fRegions.push_back_n(that->fRegions.count(), that->fRegions.begin());
Brian Osman7561dba2018-12-27 10:16:18 -0500149 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000150 return CombineResult::kMerged;
msarettcc319b92016-08-25 18:07:18 -0700151 }
152
153 struct RegionInfo {
Brian Osmancf860852018-10-31 14:04:39 -0400154 SkPMColor4f fColor;
msarettcc319b92016-08-25 18:07:18 -0700155 SkRegion fRegion;
156 };
157
Brian Salomonf0366322017-07-11 15:53:05 -0400158 Helper fHelper;
msarettfebb2242016-08-26 12:49:27 -0700159 SkMatrix fViewMatrix;
msarettcc319b92016-08-25 18:07:18 -0700160 SkSTArray<1, RegionInfo, true> fRegions;
Brian Osman7561dba2018-12-27 10:16:18 -0500161 bool fWideColor;
msarettcc319b92016-08-25 18:07:18 -0700162
Brian Salomonf0366322017-07-11 15:53:05 -0400163 typedef GrMeshDrawOp INHERITED;
msarettcc319b92016-08-25 18:07:18 -0700164};
165
Brian Salomonf0366322017-07-11 15:53:05 -0400166} // anonymous namespace
167
Brian Salomonfc527d22016-12-14 21:07:01 -0500168namespace GrRegionOp {
msarettcc319b92016-08-25 18:07:18 -0700169
Robert Phillipsb97da532019-02-12 15:24:12 -0500170std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400171 GrPaint&& paint,
172 const SkMatrix& viewMatrix,
173 const SkRegion& region,
174 GrAAType aaType,
175 const GrUserStencilSettings* stencilSettings) {
Brian Salomonf0366322017-07-11 15:53:05 -0400176 if (aaType != GrAAType::kNone && aaType != GrAAType::kMSAA) {
177 return nullptr;
178 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400179 return RegionOp::Make(context, std::move(paint), viewMatrix, region, aaType, stencilSettings);
msarettcc319b92016-08-25 18:07:18 -0700180}
Brian Salomonfc527d22016-12-14 21:07:01 -0500181}
Brian Salomonf0366322017-07-11 15:53:05 -0400182
183#if GR_TEST_UTILS
184
185GR_DRAW_OP_TEST_DEFINE(RegionOp) {
186 SkRegion region;
187 int n = random->nextULessThan(200);
188 for (int i = 0; i < n; ++i) {
189 SkIPoint center;
190 center.fX = random->nextULessThan(1000);
191 center.fY = random->nextULessThan(1000);
192 int w = random->nextRangeU(10, 1000);
193 int h = random->nextRangeU(10, 1000);
194 SkIRect rect = {center.fX - w / 2, center.fY - h / 2, center.fX + w / 2, center.fY + h / 2};
195 SkRegion::Op op;
196 if (i == 0) {
197 op = SkRegion::kReplace_Op;
198 } else {
199 // Pick an other than replace.
200 GR_STATIC_ASSERT(SkRegion::kLastOp == SkRegion::kReplace_Op);
201 op = (SkRegion::Op)random->nextULessThan(SkRegion::kLastOp);
202 }
203 region.op(rect, op);
204 }
205 SkMatrix viewMatrix = GrTest::TestMatrix(random);
206 GrAAType aaType = GrAAType::kNone;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600207 if (numSamples > 1 && random->nextBool()) {
Brian Salomonf0366322017-07-11 15:53:05 -0400208 aaType = GrAAType::kMSAA;
209 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400210 return RegionOp::Make(context, std::move(paint), viewMatrix, region, aaType,
Brian Salomon8514ebf2017-08-01 11:09:09 -0400211 GrGetRandomStencil(random, context));
Brian Salomonf0366322017-07-11 15:53:05 -0400212}
213
214#endif