blob: b1556fd7f63eeae7d9cc6712328b0b8fecf34cd2 [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
Brian Osman5ced0bf2019-03-15 10:15:29 -040090 GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip,
91 GrFSAAType fsaaType, GrClampType clampType) override {
92 return fHelper.finalizeProcessors(caps, clip, fsaaType, clampType,
Brian Osman8fa7ab42019-03-18 10:22:42 -040093 GrProcessorAnalysisCoverage::kNone, &fRegions[0].fColor,
94 &fWideColor);
Brian Salomonf0366322017-07-11 15:53:05 -040095 }
96
msarettcc319b92016-08-25 18:07:18 -070097private:
Brian Salomon91326c32017-08-09 16:02:19 -040098 void onPrepareDraws(Target* target) override {
Brian Osman7561dba2018-12-27 10:16:18 -050099 sk_sp<GrGeometryProcessor> gp = make_gp(target->caps().shaderCaps(), fViewMatrix,
100 fWideColor);
msarettcc319b92016-08-25 18:07:18 -0700101 if (!gp) {
102 SkDebugf("Couldn't create GrGeometryProcessor\n");
103 return;
104 }
msarettcc319b92016-08-25 18:07:18 -0700105
106 int numRegions = fRegions.count();
107 int numRects = 0;
108 for (int i = 0; i < numRegions; i++) {
109 numRects += fRegions[i].fRegion.computeRegionComplexity();
110 }
111
Brian Salomonf0366322017-07-11 15:53:05 -0400112 if (!numRects) {
113 return;
114 }
Brian Salomondbf70722019-02-07 11:31:24 -0500115 sk_sp<const GrGpuBuffer> indexBuffer = target->resourceProvider()->refQuadIndexBuffer();
Brian Salomon12d22642019-01-29 14:38:50 -0500116 if (!indexBuffer) {
117 SkDebugf("Could not allocate indices\n");
118 return;
119 }
Brian Osman4486d982018-11-15 15:56:04 -0500120 PatternHelper helper(target, GrPrimitiveType::kTriangles, gp->vertexStride(),
Brian Salomon12d22642019-01-29 14:38:50 -0500121 std::move(indexBuffer), kVertsPerInstance, kIndicesPerInstance,
122 numRects);
Brian Osman4486d982018-11-15 15:56:04 -0500123 GrVertexWriter vertices{helper.vertices()};
Brian Salomon12d22642019-01-29 14:38:50 -0500124 if (!vertices.fPtr) {
msarettcc319b92016-08-25 18:07:18 -0700125 SkDebugf("Could not allocate vertices\n");
126 return;
127 }
128
msarettcc319b92016-08-25 18:07:18 -0700129 for (int i = 0; i < numRegions; i++) {
Brian Osman7561dba2018-12-27 10:16:18 -0500130 GrVertexColor color(fRegions[i].fColor, fWideColor);
Brian Osman4486d982018-11-15 15:56:04 -0500131 SkRegion::Iterator iter(fRegions[i].fRegion);
132 while (!iter.done()) {
133 SkRect rect = SkRect::Make(iter.rect());
Brian Osman0dd43022018-11-16 15:53:26 -0500134 vertices.writeQuad(GrVertexWriter::TriStripFromRect(rect), color);
Brian Osman4486d982018-11-15 15:56:04 -0500135 iter.next();
136 }
msarettcc319b92016-08-25 18:07:18 -0700137 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700138 helper.recordDraw(target, std::move(gp));
139 }
140
141 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
142 fHelper.executeDrawsAndUploads(this, flushState, chainBounds);
msarettcc319b92016-08-25 18:07:18 -0700143 }
144
Brian Salomon7eae3e02018-08-07 14:02:38 +0000145 CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Brian Salomonfc527d22016-12-14 21:07:01 -0500146 RegionOp* that = t->cast<RegionOp>();
Brian Salomonf0366322017-07-11 15:53:05 -0400147 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000148 return CombineResult::kCannotCombine;
msarettcc319b92016-08-25 18:07:18 -0700149 }
150
msarettfebb2242016-08-26 12:49:27 -0700151 if (fViewMatrix != that->fViewMatrix) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000152 return CombineResult::kCannotCombine;
msarettfebb2242016-08-26 12:49:27 -0700153 }
154
msarettcc319b92016-08-25 18:07:18 -0700155 fRegions.push_back_n(that->fRegions.count(), that->fRegions.begin());
Brian Osman7561dba2018-12-27 10:16:18 -0500156 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000157 return CombineResult::kMerged;
msarettcc319b92016-08-25 18:07:18 -0700158 }
159
160 struct RegionInfo {
Brian Osmancf860852018-10-31 14:04:39 -0400161 SkPMColor4f fColor;
msarettcc319b92016-08-25 18:07:18 -0700162 SkRegion fRegion;
163 };
164
Brian Salomonf0366322017-07-11 15:53:05 -0400165 Helper fHelper;
msarettfebb2242016-08-26 12:49:27 -0700166 SkMatrix fViewMatrix;
msarettcc319b92016-08-25 18:07:18 -0700167 SkSTArray<1, RegionInfo, true> fRegions;
Brian Osman7561dba2018-12-27 10:16:18 -0500168 bool fWideColor;
msarettcc319b92016-08-25 18:07:18 -0700169
Brian Salomonf0366322017-07-11 15:53:05 -0400170 typedef GrMeshDrawOp INHERITED;
msarettcc319b92016-08-25 18:07:18 -0700171};
172
Brian Salomonf0366322017-07-11 15:53:05 -0400173} // anonymous namespace
174
Brian Salomonfc527d22016-12-14 21:07:01 -0500175namespace GrRegionOp {
msarettcc319b92016-08-25 18:07:18 -0700176
Robert Phillipsb97da532019-02-12 15:24:12 -0500177std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400178 GrPaint&& paint,
179 const SkMatrix& viewMatrix,
180 const SkRegion& region,
181 GrAAType aaType,
182 const GrUserStencilSettings* stencilSettings) {
Brian Salomonf0366322017-07-11 15:53:05 -0400183 if (aaType != GrAAType::kNone && aaType != GrAAType::kMSAA) {
184 return nullptr;
185 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400186 return RegionOp::Make(context, std::move(paint), viewMatrix, region, aaType, stencilSettings);
msarettcc319b92016-08-25 18:07:18 -0700187}
Brian Salomonfc527d22016-12-14 21:07:01 -0500188}
Brian Salomonf0366322017-07-11 15:53:05 -0400189
190#if GR_TEST_UTILS
191
192GR_DRAW_OP_TEST_DEFINE(RegionOp) {
193 SkRegion region;
194 int n = random->nextULessThan(200);
195 for (int i = 0; i < n; ++i) {
196 SkIPoint center;
197 center.fX = random->nextULessThan(1000);
198 center.fY = random->nextULessThan(1000);
199 int w = random->nextRangeU(10, 1000);
200 int h = random->nextRangeU(10, 1000);
201 SkIRect rect = {center.fX - w / 2, center.fY - h / 2, center.fX + w / 2, center.fY + h / 2};
202 SkRegion::Op op;
203 if (i == 0) {
204 op = SkRegion::kReplace_Op;
205 } else {
206 // Pick an other than replace.
207 GR_STATIC_ASSERT(SkRegion::kLastOp == SkRegion::kReplace_Op);
208 op = (SkRegion::Op)random->nextULessThan(SkRegion::kLastOp);
209 }
210 region.op(rect, op);
211 }
212 SkMatrix viewMatrix = GrTest::TestMatrix(random);
213 GrAAType aaType = GrAAType::kNone;
214 if (GrFSAAType::kUnifiedMSAA == fsaaType && random->nextBool()) {
215 aaType = GrAAType::kMSAA;
216 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400217 return RegionOp::Make(context, std::move(paint), viewMatrix, region, aaType,
Brian Salomon8514ebf2017-08-01 11:09:09 -0400218 GrGetRandomStencil(random, context));
Brian Salomonf0366322017-07-11 15:53:05 -0400219}
220
221#endif