blob: ba902ec6e1f260fcda4a76e1557a107877d14a1c [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
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);
65 }
66
Brian Salomonfc527d22016-12-14 21:07:01 -050067 const char* name() const override { return "GrRegionOp"; }
msarettcc319b92016-08-25 18:07:18 -070068
Chris Dalton1706cbf2019-05-21 19:35:29 -060069 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -040070 fHelper.visitProxies(func);
71 }
72
Brian Osman9a390ac2018-11-12 09:47:48 -050073#ifdef SK_DEBUG
msarettcc319b92016-08-25 18:07:18 -070074 SkString dumpInfo() const override {
75 SkString str;
Brian Salomon53e4c3c2016-12-21 11:38:53 -050076 str.appendf("# combined: %d\n", fRegions.count());
msarettcc319b92016-08-25 18:07:18 -070077 for (int i = 0; i < fRegions.count(); ++i) {
78 const RegionInfo& info = fRegions[i];
Brian Osmancf860852018-10-31 14:04:39 -040079 str.appendf("%d: Color: 0x%08x, Region with %d rects\n", i, info.fColor.toBytes_RGBA(),
Brian Salomonfc527d22016-12-14 21:07:01 -050080 info.fRegion.computeRegionComplexity());
msarettcc319b92016-08-25 18:07:18 -070081 }
Brian Salomonf0366322017-07-11 15:53:05 -040082 str += fHelper.dumpInfo();
83 str += INHERITED::dumpInfo();
msarettcc319b92016-08-25 18:07:18 -070084 return str;
85 }
Brian Osman9a390ac2018-11-12 09:47:48 -050086#endif
msarettcc319b92016-08-25 18:07:18 -070087
Brian Salomonf0366322017-07-11 15:53:05 -040088 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
89
Chris Dalton6ce447a2019-06-23 18:07:38 -060090 GrProcessorSet::Analysis finalize(
91 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
92 GrClampType clampType) override {
93 return fHelper.finalizeProcessors(
94 caps, clip, hasMixedSampledCoverage, clampType, GrProcessorAnalysisCoverage::kNone,
95 &fRegions[0].fColor, &fWideColor);
Brian Salomonf0366322017-07-11 15:53:05 -040096 }
97
msarettcc319b92016-08-25 18:07:18 -070098private:
Brian Salomon91326c32017-08-09 16:02:19 -040099 void onPrepareDraws(Target* target) override {
Brian Osman7561dba2018-12-27 10:16:18 -0500100 sk_sp<GrGeometryProcessor> gp = make_gp(target->caps().shaderCaps(), fViewMatrix,
101 fWideColor);
msarettcc319b92016-08-25 18:07:18 -0700102 if (!gp) {
103 SkDebugf("Couldn't create GrGeometryProcessor\n");
104 return;
105 }
msarettcc319b92016-08-25 18:07:18 -0700106
107 int numRegions = fRegions.count();
108 int numRects = 0;
109 for (int i = 0; i < numRegions; i++) {
110 numRects += fRegions[i].fRegion.computeRegionComplexity();
111 }
112
Brian Salomonf0366322017-07-11 15:53:05 -0400113 if (!numRects) {
114 return;
115 }
Brian Salomondbf70722019-02-07 11:31:24 -0500116 sk_sp<const GrGpuBuffer> indexBuffer = target->resourceProvider()->refQuadIndexBuffer();
Brian Salomon12d22642019-01-29 14:38:50 -0500117 if (!indexBuffer) {
118 SkDebugf("Could not allocate indices\n");
119 return;
120 }
Brian Osman4486d982018-11-15 15:56:04 -0500121 PatternHelper helper(target, GrPrimitiveType::kTriangles, gp->vertexStride(),
Brian Salomon12d22642019-01-29 14:38:50 -0500122 std::move(indexBuffer), kVertsPerInstance, kIndicesPerInstance,
123 numRects);
Brian Osman4486d982018-11-15 15:56:04 -0500124 GrVertexWriter vertices{helper.vertices()};
Brian Salomon12d22642019-01-29 14:38:50 -0500125 if (!vertices.fPtr) {
msarettcc319b92016-08-25 18:07:18 -0700126 SkDebugf("Could not allocate vertices\n");
127 return;
128 }
129
msarettcc319b92016-08-25 18:07:18 -0700130 for (int i = 0; i < numRegions; i++) {
Brian Osman7561dba2018-12-27 10:16:18 -0500131 GrVertexColor color(fRegions[i].fColor, fWideColor);
Brian Osman4486d982018-11-15 15:56:04 -0500132 SkRegion::Iterator iter(fRegions[i].fRegion);
133 while (!iter.done()) {
134 SkRect rect = SkRect::Make(iter.rect());
Brian Osman0dd43022018-11-16 15:53:26 -0500135 vertices.writeQuad(GrVertexWriter::TriStripFromRect(rect), color);
Brian Osman4486d982018-11-15 15:56:04 -0500136 iter.next();
137 }
msarettcc319b92016-08-25 18:07:18 -0700138 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700139 helper.recordDraw(target, std::move(gp));
140 }
141
142 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
143 fHelper.executeDrawsAndUploads(this, flushState, chainBounds);
msarettcc319b92016-08-25 18:07:18 -0700144 }
145
Brian Salomon7eae3e02018-08-07 14:02:38 +0000146 CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Brian Salomonfc527d22016-12-14 21:07:01 -0500147 RegionOp* that = t->cast<RegionOp>();
Brian Salomonf0366322017-07-11 15:53:05 -0400148 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000149 return CombineResult::kCannotCombine;
msarettcc319b92016-08-25 18:07:18 -0700150 }
151
msarettfebb2242016-08-26 12:49:27 -0700152 if (fViewMatrix != that->fViewMatrix) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000153 return CombineResult::kCannotCombine;
msarettfebb2242016-08-26 12:49:27 -0700154 }
155
msarettcc319b92016-08-25 18:07:18 -0700156 fRegions.push_back_n(that->fRegions.count(), that->fRegions.begin());
Brian Osman7561dba2018-12-27 10:16:18 -0500157 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000158 return CombineResult::kMerged;
msarettcc319b92016-08-25 18:07:18 -0700159 }
160
161 struct RegionInfo {
Brian Osmancf860852018-10-31 14:04:39 -0400162 SkPMColor4f fColor;
msarettcc319b92016-08-25 18:07:18 -0700163 SkRegion fRegion;
164 };
165
Brian Salomonf0366322017-07-11 15:53:05 -0400166 Helper fHelper;
msarettfebb2242016-08-26 12:49:27 -0700167 SkMatrix fViewMatrix;
msarettcc319b92016-08-25 18:07:18 -0700168 SkSTArray<1, RegionInfo, true> fRegions;
Brian Osman7561dba2018-12-27 10:16:18 -0500169 bool fWideColor;
msarettcc319b92016-08-25 18:07:18 -0700170
Brian Salomonf0366322017-07-11 15:53:05 -0400171 typedef GrMeshDrawOp INHERITED;
msarettcc319b92016-08-25 18:07:18 -0700172};
173
Brian Salomonf0366322017-07-11 15:53:05 -0400174} // anonymous namespace
175
Brian Salomonfc527d22016-12-14 21:07:01 -0500176namespace GrRegionOp {
msarettcc319b92016-08-25 18:07:18 -0700177
Robert Phillipsb97da532019-02-12 15:24:12 -0500178std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400179 GrPaint&& paint,
180 const SkMatrix& viewMatrix,
181 const SkRegion& region,
182 GrAAType aaType,
183 const GrUserStencilSettings* stencilSettings) {
Brian Salomonf0366322017-07-11 15:53:05 -0400184 if (aaType != GrAAType::kNone && aaType != GrAAType::kMSAA) {
185 return nullptr;
186 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400187 return RegionOp::Make(context, std::move(paint), viewMatrix, region, aaType, stencilSettings);
msarettcc319b92016-08-25 18:07:18 -0700188}
Brian Salomonfc527d22016-12-14 21:07:01 -0500189}
Brian Salomonf0366322017-07-11 15:53:05 -0400190
191#if GR_TEST_UTILS
192
193GR_DRAW_OP_TEST_DEFINE(RegionOp) {
194 SkRegion region;
195 int n = random->nextULessThan(200);
196 for (int i = 0; i < n; ++i) {
197 SkIPoint center;
198 center.fX = random->nextULessThan(1000);
199 center.fY = random->nextULessThan(1000);
200 int w = random->nextRangeU(10, 1000);
201 int h = random->nextRangeU(10, 1000);
202 SkIRect rect = {center.fX - w / 2, center.fY - h / 2, center.fX + w / 2, center.fY + h / 2};
203 SkRegion::Op op;
204 if (i == 0) {
205 op = SkRegion::kReplace_Op;
206 } else {
207 // Pick an other than replace.
208 GR_STATIC_ASSERT(SkRegion::kLastOp == SkRegion::kReplace_Op);
209 op = (SkRegion::Op)random->nextULessThan(SkRegion::kLastOp);
210 }
211 region.op(rect, op);
212 }
213 SkMatrix viewMatrix = GrTest::TestMatrix(random);
214 GrAAType aaType = GrAAType::kNone;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600215 if (numSamples > 1 && random->nextBool()) {
Brian Salomonf0366322017-07-11 15:53:05 -0400216 aaType = GrAAType::kMSAA;
217 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400218 return RegionOp::Make(context, std::move(paint), viewMatrix, region, aaType,
Brian Salomon8514ebf2017-08-01 11:09:09 -0400219 GrGetRandomStencil(random, context));
Brian Salomonf0366322017-07-11 15:53:05 -0400220}
221
222#endif