blob: 356f3a7fefdfaa6f1f69fb5b632c37e4b44cddde [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"
Brian Salomonf0366322017-07-11 15:53:05 -04009#include <GrDrawOpTest.h>
Brian Salomondad29232016-12-01 16:40:24 -050010#include "GrDefaultGeoProcFactory.h"
11#include "GrMeshDrawOp.h"
Brian Salomon742e31d2016-12-07 17:06:19 -050012#include "GrOpFlushState.h"
msarettcc319b92016-08-25 18:07:18 -070013#include "GrResourceProvider.h"
Brian Salomonf0366322017-07-11 15:53:05 -040014#include "GrSimpleMeshDrawOpHelper.h"
Brian Osman4486d982018-11-15 15:56:04 -050015#include "GrVertexWriter.h"
msarettcc319b92016-08-25 18:07:18 -070016#include "SkMatrixPriv.h"
17#include "SkRegion.h"
18
19static const int kVertsPerInstance = 4;
20static const int kIndicesPerInstance = 6;
21
Ruiqi Maob609e6d2018-07-17 10:19:38 -040022static sk_sp<GrGeometryProcessor> make_gp(const GrShaderCaps* shaderCaps,
Brian Osman7561dba2018-12-27 10:16:18 -050023 const SkMatrix& viewMatrix,
24 bool wideColor) {
msarettcc319b92016-08-25 18:07:18 -070025 using namespace GrDefaultGeoProcFactory;
Brian Osman7561dba2018-12-27 10:16:18 -050026 Color::Type colorType =
27 wideColor ? Color::kPremulWideColorAttribute_Type : Color::kPremulGrColorAttribute_Type;
28 return GrDefaultGeoProcFactory::Make(shaderCaps, colorType, Coverage::kSolid_Type,
29 LocalCoords::kUsePosition_Type, viewMatrix);
msarettcc319b92016-08-25 18:07:18 -070030}
31
Brian Salomonf0366322017-07-11 15:53:05 -040032namespace {
33
34class RegionOp final : public GrMeshDrawOp {
35private:
Brian Salomon8514ebf2017-08-01 11:09:09 -040036 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
Brian Salomonf0366322017-07-11 15:53:05 -040037
msarettcc319b92016-08-25 18:07:18 -070038public:
Brian Salomon25a88092016-12-01 09:36:50 -050039 DEFINE_OP_CLASS_ID
msarettcc319b92016-08-25 18:07:18 -070040
Robert Phillips7c525e62018-06-12 10:11:12 -040041 static std::unique_ptr<GrDrawOp> Make(GrContext* context,
42 GrPaint&& paint,
43 const SkMatrix& viewMatrix,
44 const SkRegion& region,
45 GrAAType aaType,
Brian Salomon8514ebf2017-08-01 11:09:09 -040046 const GrUserStencilSettings* stencilSettings = nullptr) {
Robert Phillips7c525e62018-06-12 10:11:12 -040047 return Helper::FactoryHelper<RegionOp>(context, std::move(paint), viewMatrix, region,
48 aaType, stencilSettings);
Brian Salomonf0366322017-07-11 15:53:05 -040049 }
50
Brian Osmancf860852018-10-31 14:04:39 -040051 RegionOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color,
52 const SkMatrix& viewMatrix, const SkRegion& region, GrAAType aaType,
53 const GrUserStencilSettings* stencilSettings)
Brian Salomon8514ebf2017-08-01 11:09:09 -040054 : INHERITED(ClassID())
55 , fHelper(helperArgs, aaType, stencilSettings)
56 , fViewMatrix(viewMatrix) {
msarettcc319b92016-08-25 18:07:18 -070057 RegionInfo& info = fRegions.push_back();
58 info.fColor = color;
msarettcc319b92016-08-25 18:07:18 -070059 info.fRegion = region;
60
61 SkRect bounds = SkRect::Make(region.getBounds());
62 this->setTransformedBounds(bounds, viewMatrix, HasAABloat::kNo, IsZeroArea::kNo);
Brian Osman7561dba2018-12-27 10:16:18 -050063 fWideColor = !SkPMColor4fFitsInBytes(color);
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
Brian Salomon7d94bb52018-10-12 14:37:19 -040068 void visitProxies(const VisitProxyFunc& func, VisitorType) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -040069 fHelper.visitProxies(func);
70 }
71
Brian Osman9a390ac2018-11-12 09:47:48 -050072#ifdef SK_DEBUG
msarettcc319b92016-08-25 18:07:18 -070073 SkString dumpInfo() const override {
74 SkString str;
Brian Salomon53e4c3c2016-12-21 11:38:53 -050075 str.appendf("# combined: %d\n", fRegions.count());
msarettcc319b92016-08-25 18:07:18 -070076 for (int i = 0; i < fRegions.count(); ++i) {
77 const RegionInfo& info = fRegions[i];
Brian Osmancf860852018-10-31 14:04:39 -040078 str.appendf("%d: Color: 0x%08x, Region with %d rects\n", i, info.fColor.toBytes_RGBA(),
Brian Salomonfc527d22016-12-14 21:07:01 -050079 info.fRegion.computeRegionComplexity());
msarettcc319b92016-08-25 18:07:18 -070080 }
Brian Salomonf0366322017-07-11 15:53:05 -040081 str += fHelper.dumpInfo();
82 str += INHERITED::dumpInfo();
msarettcc319b92016-08-25 18:07:18 -070083 return str;
84 }
Brian Osman9a390ac2018-11-12 09:47:48 -050085#endif
msarettcc319b92016-08-25 18:07:18 -070086
Brian Salomonf0366322017-07-11 15:53:05 -040087 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
88
Brian Osman532b3f92018-07-11 10:02:07 -040089 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip) override {
90 return fHelper.xpRequiresDstTexture(caps, clip, GrProcessorAnalysisCoverage::kNone,
91 &fRegions[0].fColor);
Brian Salomonf0366322017-07-11 15:53:05 -040092 }
93
msarettcc319b92016-08-25 18:07:18 -070094private:
Brian Salomon91326c32017-08-09 16:02:19 -040095 void onPrepareDraws(Target* target) override {
Brian Osman7561dba2018-12-27 10:16:18 -050096 sk_sp<GrGeometryProcessor> gp = make_gp(target->caps().shaderCaps(), fViewMatrix,
97 fWideColor);
msarettcc319b92016-08-25 18:07:18 -070098 if (!gp) {
99 SkDebugf("Couldn't create GrGeometryProcessor\n");
100 return;
101 }
msarettcc319b92016-08-25 18:07:18 -0700102
103 int numRegions = fRegions.count();
104 int numRects = 0;
105 for (int i = 0; i < numRegions; i++) {
106 numRects += fRegions[i].fRegion.computeRegionComplexity();
107 }
108
Brian Salomonf0366322017-07-11 15:53:05 -0400109 if (!numRects) {
110 return;
111 }
Brian Salomond28a79d2017-10-16 13:01:07 -0400112 sk_sp<const GrBuffer> indexBuffer = target->resourceProvider()->refQuadIndexBuffer();
Brian Osman4486d982018-11-15 15:56:04 -0500113 PatternHelper helper(target, GrPrimitiveType::kTriangles, gp->vertexStride(),
114 indexBuffer.get(), kVertsPerInstance, kIndicesPerInstance, numRects);
115 GrVertexWriter vertices{helper.vertices()};
116 if (!vertices.fPtr || !indexBuffer) {
msarettcc319b92016-08-25 18:07:18 -0700117 SkDebugf("Could not allocate vertices\n");
118 return;
119 }
120
msarettcc319b92016-08-25 18:07:18 -0700121 for (int i = 0; i < numRegions; i++) {
Brian Osman7561dba2018-12-27 10:16:18 -0500122 GrVertexColor color(fRegions[i].fColor, fWideColor);
Brian Osman4486d982018-11-15 15:56:04 -0500123 SkRegion::Iterator iter(fRegions[i].fRegion);
124 while (!iter.done()) {
125 SkRect rect = SkRect::Make(iter.rect());
Brian Osman0dd43022018-11-16 15:53:26 -0500126 vertices.writeQuad(GrVertexWriter::TriStripFromRect(rect), color);
Brian Osman4486d982018-11-15 15:56:04 -0500127 iter.next();
128 }
msarettcc319b92016-08-25 18:07:18 -0700129 }
Brian Salomon49348902018-06-26 09:12:38 -0400130 auto pipe = fHelper.makePipeline(target);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000131 helper.recordDraw(target, std::move(gp), pipe.fPipeline, pipe.fFixedDynamicState);
msarettcc319b92016-08-25 18:07:18 -0700132 }
133
Brian Salomon7eae3e02018-08-07 14:02:38 +0000134 CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Brian Salomonfc527d22016-12-14 21:07:01 -0500135 RegionOp* that = t->cast<RegionOp>();
Brian Salomonf0366322017-07-11 15:53:05 -0400136 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000137 return CombineResult::kCannotCombine;
msarettcc319b92016-08-25 18:07:18 -0700138 }
139
msarettfebb2242016-08-26 12:49:27 -0700140 if (fViewMatrix != that->fViewMatrix) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000141 return CombineResult::kCannotCombine;
msarettfebb2242016-08-26 12:49:27 -0700142 }
143
msarettcc319b92016-08-25 18:07:18 -0700144 fRegions.push_back_n(that->fRegions.count(), that->fRegions.begin());
Brian Osman7561dba2018-12-27 10:16:18 -0500145 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000146 return CombineResult::kMerged;
msarettcc319b92016-08-25 18:07:18 -0700147 }
148
149 struct RegionInfo {
Brian Osmancf860852018-10-31 14:04:39 -0400150 SkPMColor4f fColor;
msarettcc319b92016-08-25 18:07:18 -0700151 SkRegion fRegion;
152 };
153
Brian Salomonf0366322017-07-11 15:53:05 -0400154 Helper fHelper;
msarettfebb2242016-08-26 12:49:27 -0700155 SkMatrix fViewMatrix;
msarettcc319b92016-08-25 18:07:18 -0700156 SkSTArray<1, RegionInfo, true> fRegions;
Brian Osman7561dba2018-12-27 10:16:18 -0500157 bool fWideColor;
msarettcc319b92016-08-25 18:07:18 -0700158
Brian Salomonf0366322017-07-11 15:53:05 -0400159 typedef GrMeshDrawOp INHERITED;
msarettcc319b92016-08-25 18:07:18 -0700160};
161
Brian Salomonf0366322017-07-11 15:53:05 -0400162} // anonymous namespace
163
Brian Salomonfc527d22016-12-14 21:07:01 -0500164namespace GrRegionOp {
msarettcc319b92016-08-25 18:07:18 -0700165
Robert Phillips7c525e62018-06-12 10:11:12 -0400166std::unique_ptr<GrDrawOp> Make(GrContext* context,
167 GrPaint&& paint,
168 const SkMatrix& viewMatrix,
169 const SkRegion& region,
170 GrAAType aaType,
171 const GrUserStencilSettings* stencilSettings) {
Brian Salomonf0366322017-07-11 15:53:05 -0400172 if (aaType != GrAAType::kNone && aaType != GrAAType::kMSAA) {
173 return nullptr;
174 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400175 return RegionOp::Make(context, std::move(paint), viewMatrix, region, aaType, stencilSettings);
msarettcc319b92016-08-25 18:07:18 -0700176}
Brian Salomonfc527d22016-12-14 21:07:01 -0500177}
Brian Salomonf0366322017-07-11 15:53:05 -0400178
179#if GR_TEST_UTILS
180
181GR_DRAW_OP_TEST_DEFINE(RegionOp) {
182 SkRegion region;
183 int n = random->nextULessThan(200);
184 for (int i = 0; i < n; ++i) {
185 SkIPoint center;
186 center.fX = random->nextULessThan(1000);
187 center.fY = random->nextULessThan(1000);
188 int w = random->nextRangeU(10, 1000);
189 int h = random->nextRangeU(10, 1000);
190 SkIRect rect = {center.fX - w / 2, center.fY - h / 2, center.fX + w / 2, center.fY + h / 2};
191 SkRegion::Op op;
192 if (i == 0) {
193 op = SkRegion::kReplace_Op;
194 } else {
195 // Pick an other than replace.
196 GR_STATIC_ASSERT(SkRegion::kLastOp == SkRegion::kReplace_Op);
197 op = (SkRegion::Op)random->nextULessThan(SkRegion::kLastOp);
198 }
199 region.op(rect, op);
200 }
201 SkMatrix viewMatrix = GrTest::TestMatrix(random);
202 GrAAType aaType = GrAAType::kNone;
203 if (GrFSAAType::kUnifiedMSAA == fsaaType && random->nextBool()) {
204 aaType = GrAAType::kMSAA;
205 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400206 return RegionOp::Make(context, std::move(paint), viewMatrix, region, aaType,
Brian Salomon8514ebf2017-08-01 11:09:09 -0400207 GrGetRandomStencil(random, context));
Brian Salomonf0366322017-07-11 15:53:05 -0400208}
209
210#endif