blob: c050142cdfed5abe9fd9bcb57437651cac10da35 [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
Brian Salomonfc527d22016-12-14 21:07:01 -05008#include "GrRegionOp.h"
Robert Phillipsb97da532019-02-12 15:24:12 -05009
10#include "GrCaps.h"
Brian Salomondad29232016-12-01 16:40:24 -050011#include "GrDefaultGeoProcFactory.h"
Robert Phillipsb97da532019-02-12 15:24:12 -050012#include "GrDrawOpTest.h"
Brian Salomondad29232016-12-01 16:40:24 -050013#include "GrMeshDrawOp.h"
Brian Salomon742e31d2016-12-07 17:06:19 -050014#include "GrOpFlushState.h"
msarettcc319b92016-08-25 18:07:18 -070015#include "GrResourceProvider.h"
Brian Salomonf0366322017-07-11 15:53:05 -040016#include "GrSimpleMeshDrawOpHelper.h"
Brian Osman4486d982018-11-15 15:56:04 -050017#include "GrVertexWriter.h"
msarettcc319b92016-08-25 18:07:18 -070018#include "SkMatrixPriv.h"
19#include "SkRegion.h"
20
21static const int kVertsPerInstance = 4;
22static const int kIndicesPerInstance = 6;
23
Ruiqi Maob609e6d2018-07-17 10:19:38 -040024static sk_sp<GrGeometryProcessor> make_gp(const GrShaderCaps* shaderCaps,
Brian Osman7561dba2018-12-27 10:16:18 -050025 const SkMatrix& viewMatrix,
26 bool wideColor) {
msarettcc319b92016-08-25 18:07:18 -070027 using namespace GrDefaultGeoProcFactory;
Brian Osman7561dba2018-12-27 10:16:18 -050028 Color::Type colorType =
29 wideColor ? Color::kPremulWideColorAttribute_Type : Color::kPremulGrColorAttribute_Type;
30 return GrDefaultGeoProcFactory::Make(shaderCaps, colorType, Coverage::kSolid_Type,
31 LocalCoords::kUsePosition_Type, viewMatrix);
msarettcc319b92016-08-25 18:07:18 -070032}
33
Brian Salomonf0366322017-07-11 15:53:05 -040034namespace {
35
36class RegionOp final : public GrMeshDrawOp {
37private:
Brian Salomon8514ebf2017-08-01 11:09:09 -040038 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
Brian Salomonf0366322017-07-11 15:53:05 -040039
msarettcc319b92016-08-25 18:07:18 -070040public:
Brian Salomon25a88092016-12-01 09:36:50 -050041 DEFINE_OP_CLASS_ID
msarettcc319b92016-08-25 18:07:18 -070042
Robert Phillipsb97da532019-02-12 15:24:12 -050043 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -040044 GrPaint&& paint,
45 const SkMatrix& viewMatrix,
46 const SkRegion& region,
47 GrAAType aaType,
Brian Salomon8514ebf2017-08-01 11:09:09 -040048 const GrUserStencilSettings* stencilSettings = nullptr) {
Robert Phillips7c525e62018-06-12 10:11:12 -040049 return Helper::FactoryHelper<RegionOp>(context, std::move(paint), viewMatrix, region,
50 aaType, stencilSettings);
Brian Salomonf0366322017-07-11 15:53:05 -040051 }
52
Brian Osmancf860852018-10-31 14:04:39 -040053 RegionOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color,
54 const SkMatrix& viewMatrix, const SkRegion& region, GrAAType aaType,
55 const GrUserStencilSettings* stencilSettings)
Brian Salomon8514ebf2017-08-01 11:09:09 -040056 : INHERITED(ClassID())
57 , fHelper(helperArgs, aaType, stencilSettings)
58 , fViewMatrix(viewMatrix) {
msarettcc319b92016-08-25 18:07:18 -070059 RegionInfo& info = fRegions.push_back();
60 info.fColor = color;
msarettcc319b92016-08-25 18:07:18 -070061 info.fRegion = region;
62
63 SkRect bounds = SkRect::Make(region.getBounds());
64 this->setTransformedBounds(bounds, viewMatrix, HasAABloat::kNo, IsZeroArea::kNo);
Brian Osman7561dba2018-12-27 10:16:18 -050065 fWideColor = !SkPMColor4fFitsInBytes(color);
msarettcc319b92016-08-25 18:07:18 -070066 }
67
Brian Salomonfc527d22016-12-14 21:07:01 -050068 const char* name() const override { return "GrRegionOp"; }
msarettcc319b92016-08-25 18:07:18 -070069
Brian Salomon7d94bb52018-10-12 14:37:19 -040070 void visitProxies(const VisitProxyFunc& func, VisitorType) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -040071 fHelper.visitProxies(func);
72 }
73
Brian Osman9a390ac2018-11-12 09:47:48 -050074#ifdef SK_DEBUG
msarettcc319b92016-08-25 18:07:18 -070075 SkString dumpInfo() const override {
76 SkString str;
Brian Salomon53e4c3c2016-12-21 11:38:53 -050077 str.appendf("# combined: %d\n", fRegions.count());
msarettcc319b92016-08-25 18:07:18 -070078 for (int i = 0; i < fRegions.count(); ++i) {
79 const RegionInfo& info = fRegions[i];
Brian Osmancf860852018-10-31 14:04:39 -040080 str.appendf("%d: Color: 0x%08x, Region with %d rects\n", i, info.fColor.toBytes_RGBA(),
Brian Salomonfc527d22016-12-14 21:07:01 -050081 info.fRegion.computeRegionComplexity());
msarettcc319b92016-08-25 18:07:18 -070082 }
Brian Salomonf0366322017-07-11 15:53:05 -040083 str += fHelper.dumpInfo();
84 str += INHERITED::dumpInfo();
msarettcc319b92016-08-25 18:07:18 -070085 return str;
86 }
Brian Osman9a390ac2018-11-12 09:47:48 -050087#endif
msarettcc319b92016-08-25 18:07:18 -070088
Brian Salomonf0366322017-07-11 15:53:05 -040089 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
90
Chris Dalton4b62aed2019-01-15 11:53:00 -070091 GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip) override {
92 return fHelper.finalizeProcessors(caps, clip, GrProcessorAnalysisCoverage::kNone,
93 &fRegions[0].fColor);
Brian Salomonf0366322017-07-11 15:53:05 -040094 }
95
msarettcc319b92016-08-25 18:07:18 -070096private:
Brian Salomon91326c32017-08-09 16:02:19 -040097 void onPrepareDraws(Target* target) override {
Brian Osman7561dba2018-12-27 10:16:18 -050098 sk_sp<GrGeometryProcessor> gp = make_gp(target->caps().shaderCaps(), fViewMatrix,
99 fWideColor);
msarettcc319b92016-08-25 18:07:18 -0700100 if (!gp) {
101 SkDebugf("Couldn't create GrGeometryProcessor\n");
102 return;
103 }
msarettcc319b92016-08-25 18:07:18 -0700104
105 int numRegions = fRegions.count();
106 int numRects = 0;
107 for (int i = 0; i < numRegions; i++) {
108 numRects += fRegions[i].fRegion.computeRegionComplexity();
109 }
110
Brian Salomonf0366322017-07-11 15:53:05 -0400111 if (!numRects) {
112 return;
113 }
Brian Salomondbf70722019-02-07 11:31:24 -0500114 sk_sp<const GrGpuBuffer> indexBuffer = target->resourceProvider()->refQuadIndexBuffer();
Brian Salomon12d22642019-01-29 14:38:50 -0500115 if (!indexBuffer) {
116 SkDebugf("Could not allocate indices\n");
117 return;
118 }
Brian Osman4486d982018-11-15 15:56:04 -0500119 PatternHelper helper(target, GrPrimitiveType::kTriangles, gp->vertexStride(),
Brian Salomon12d22642019-01-29 14:38:50 -0500120 std::move(indexBuffer), kVertsPerInstance, kIndicesPerInstance,
121 numRects);
Brian Osman4486d982018-11-15 15:56:04 -0500122 GrVertexWriter vertices{helper.vertices()};
Brian Salomon12d22642019-01-29 14:38:50 -0500123 if (!vertices.fPtr) {
msarettcc319b92016-08-25 18:07:18 -0700124 SkDebugf("Could not allocate vertices\n");
125 return;
126 }
127
msarettcc319b92016-08-25 18:07:18 -0700128 for (int i = 0; i < numRegions; i++) {
Brian Osman7561dba2018-12-27 10:16:18 -0500129 GrVertexColor color(fRegions[i].fColor, fWideColor);
Brian Osman4486d982018-11-15 15:56:04 -0500130 SkRegion::Iterator iter(fRegions[i].fRegion);
131 while (!iter.done()) {
132 SkRect rect = SkRect::Make(iter.rect());
Brian Osman0dd43022018-11-16 15:53:26 -0500133 vertices.writeQuad(GrVertexWriter::TriStripFromRect(rect), color);
Brian Osman4486d982018-11-15 15:56:04 -0500134 iter.next();
135 }
msarettcc319b92016-08-25 18:07:18 -0700136 }
Brian Salomon49348902018-06-26 09:12:38 -0400137 auto pipe = fHelper.makePipeline(target);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000138 helper.recordDraw(target, std::move(gp), pipe.fPipeline, pipe.fFixedDynamicState);
msarettcc319b92016-08-25 18:07:18 -0700139 }
140
Brian Salomon7eae3e02018-08-07 14:02:38 +0000141 CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Brian Salomonfc527d22016-12-14 21:07:01 -0500142 RegionOp* that = t->cast<RegionOp>();
Brian Salomonf0366322017-07-11 15:53:05 -0400143 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000144 return CombineResult::kCannotCombine;
msarettcc319b92016-08-25 18:07:18 -0700145 }
146
msarettfebb2242016-08-26 12:49:27 -0700147 if (fViewMatrix != that->fViewMatrix) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000148 return CombineResult::kCannotCombine;
msarettfebb2242016-08-26 12:49:27 -0700149 }
150
msarettcc319b92016-08-25 18:07:18 -0700151 fRegions.push_back_n(that->fRegions.count(), that->fRegions.begin());
Brian Osman7561dba2018-12-27 10:16:18 -0500152 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000153 return CombineResult::kMerged;
msarettcc319b92016-08-25 18:07:18 -0700154 }
155
156 struct RegionInfo {
Brian Osmancf860852018-10-31 14:04:39 -0400157 SkPMColor4f fColor;
msarettcc319b92016-08-25 18:07:18 -0700158 SkRegion fRegion;
159 };
160
Brian Salomonf0366322017-07-11 15:53:05 -0400161 Helper fHelper;
msarettfebb2242016-08-26 12:49:27 -0700162 SkMatrix fViewMatrix;
msarettcc319b92016-08-25 18:07:18 -0700163 SkSTArray<1, RegionInfo, true> fRegions;
Brian Osman7561dba2018-12-27 10:16:18 -0500164 bool fWideColor;
msarettcc319b92016-08-25 18:07:18 -0700165
Brian Salomonf0366322017-07-11 15:53:05 -0400166 typedef GrMeshDrawOp INHERITED;
msarettcc319b92016-08-25 18:07:18 -0700167};
168
Brian Salomonf0366322017-07-11 15:53:05 -0400169} // anonymous namespace
170
Brian Salomonfc527d22016-12-14 21:07:01 -0500171namespace GrRegionOp {
msarettcc319b92016-08-25 18:07:18 -0700172
Robert Phillipsb97da532019-02-12 15:24:12 -0500173std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400174 GrPaint&& paint,
175 const SkMatrix& viewMatrix,
176 const SkRegion& region,
177 GrAAType aaType,
178 const GrUserStencilSettings* stencilSettings) {
Brian Salomonf0366322017-07-11 15:53:05 -0400179 if (aaType != GrAAType::kNone && aaType != GrAAType::kMSAA) {
180 return nullptr;
181 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400182 return RegionOp::Make(context, std::move(paint), viewMatrix, region, aaType, stencilSettings);
msarettcc319b92016-08-25 18:07:18 -0700183}
Brian Salomonfc527d22016-12-14 21:07:01 -0500184}
Brian Salomonf0366322017-07-11 15:53:05 -0400185
186#if GR_TEST_UTILS
187
188GR_DRAW_OP_TEST_DEFINE(RegionOp) {
189 SkRegion region;
190 int n = random->nextULessThan(200);
191 for (int i = 0; i < n; ++i) {
192 SkIPoint center;
193 center.fX = random->nextULessThan(1000);
194 center.fY = random->nextULessThan(1000);
195 int w = random->nextRangeU(10, 1000);
196 int h = random->nextRangeU(10, 1000);
197 SkIRect rect = {center.fX - w / 2, center.fY - h / 2, center.fX + w / 2, center.fY + h / 2};
198 SkRegion::Op op;
199 if (i == 0) {
200 op = SkRegion::kReplace_Op;
201 } else {
202 // Pick an other than replace.
203 GR_STATIC_ASSERT(SkRegion::kLastOp == SkRegion::kReplace_Op);
204 op = (SkRegion::Op)random->nextULessThan(SkRegion::kLastOp);
205 }
206 region.op(rect, op);
207 }
208 SkMatrix viewMatrix = GrTest::TestMatrix(random);
209 GrAAType aaType = GrAAType::kNone;
210 if (GrFSAAType::kUnifiedMSAA == fsaaType && random->nextBool()) {
211 aaType = GrAAType::kMSAA;
212 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400213 return RegionOp::Make(context, std::move(paint), viewMatrix, region, aaType,
Brian Salomon8514ebf2017-08-01 11:09:09 -0400214 GrGetRandomStencil(random, context));
Brian Salomonf0366322017-07-11 15:53:05 -0400215}
216
217#endif