blob: 9db1feca66d8015609b91a1af0282b87bca09746 [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
Chris Dalton4b62aed2019-01-15 11:53:00 -070089 GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip) override {
90 return fHelper.finalizeProcessors(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 Salomondbf70722019-02-07 11:31:24 -0500112 sk_sp<const GrGpuBuffer> indexBuffer = target->resourceProvider()->refQuadIndexBuffer();
Brian Salomon12d22642019-01-29 14:38:50 -0500113 if (!indexBuffer) {
114 SkDebugf("Could not allocate indices\n");
115 return;
116 }
Brian Osman4486d982018-11-15 15:56:04 -0500117 PatternHelper helper(target, GrPrimitiveType::kTriangles, gp->vertexStride(),
Brian Salomon12d22642019-01-29 14:38:50 -0500118 std::move(indexBuffer), kVertsPerInstance, kIndicesPerInstance,
119 numRects);
Brian Osman4486d982018-11-15 15:56:04 -0500120 GrVertexWriter vertices{helper.vertices()};
Brian Salomon12d22642019-01-29 14:38:50 -0500121 if (!vertices.fPtr) {
msarettcc319b92016-08-25 18:07:18 -0700122 SkDebugf("Could not allocate vertices\n");
123 return;
124 }
125
msarettcc319b92016-08-25 18:07:18 -0700126 for (int i = 0; i < numRegions; i++) {
Brian Osman7561dba2018-12-27 10:16:18 -0500127 GrVertexColor color(fRegions[i].fColor, fWideColor);
Brian Osman4486d982018-11-15 15:56:04 -0500128 SkRegion::Iterator iter(fRegions[i].fRegion);
129 while (!iter.done()) {
130 SkRect rect = SkRect::Make(iter.rect());
Brian Osman0dd43022018-11-16 15:53:26 -0500131 vertices.writeQuad(GrVertexWriter::TriStripFromRect(rect), color);
Brian Osman4486d982018-11-15 15:56:04 -0500132 iter.next();
133 }
msarettcc319b92016-08-25 18:07:18 -0700134 }
Brian Salomon49348902018-06-26 09:12:38 -0400135 auto pipe = fHelper.makePipeline(target);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000136 helper.recordDraw(target, std::move(gp), pipe.fPipeline, pipe.fFixedDynamicState);
msarettcc319b92016-08-25 18:07:18 -0700137 }
138
Brian Salomon7eae3e02018-08-07 14:02:38 +0000139 CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Brian Salomonfc527d22016-12-14 21:07:01 -0500140 RegionOp* that = t->cast<RegionOp>();
Brian Salomonf0366322017-07-11 15:53:05 -0400141 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000142 return CombineResult::kCannotCombine;
msarettcc319b92016-08-25 18:07:18 -0700143 }
144
msarettfebb2242016-08-26 12:49:27 -0700145 if (fViewMatrix != that->fViewMatrix) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000146 return CombineResult::kCannotCombine;
msarettfebb2242016-08-26 12:49:27 -0700147 }
148
msarettcc319b92016-08-25 18:07:18 -0700149 fRegions.push_back_n(that->fRegions.count(), that->fRegions.begin());
Brian Osman7561dba2018-12-27 10:16:18 -0500150 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000151 return CombineResult::kMerged;
msarettcc319b92016-08-25 18:07:18 -0700152 }
153
154 struct RegionInfo {
Brian Osmancf860852018-10-31 14:04:39 -0400155 SkPMColor4f fColor;
msarettcc319b92016-08-25 18:07:18 -0700156 SkRegion fRegion;
157 };
158
Brian Salomonf0366322017-07-11 15:53:05 -0400159 Helper fHelper;
msarettfebb2242016-08-26 12:49:27 -0700160 SkMatrix fViewMatrix;
msarettcc319b92016-08-25 18:07:18 -0700161 SkSTArray<1, RegionInfo, true> fRegions;
Brian Osman7561dba2018-12-27 10:16:18 -0500162 bool fWideColor;
msarettcc319b92016-08-25 18:07:18 -0700163
Brian Salomonf0366322017-07-11 15:53:05 -0400164 typedef GrMeshDrawOp INHERITED;
msarettcc319b92016-08-25 18:07:18 -0700165};
166
Brian Salomonf0366322017-07-11 15:53:05 -0400167} // anonymous namespace
168
Brian Salomonfc527d22016-12-14 21:07:01 -0500169namespace GrRegionOp {
msarettcc319b92016-08-25 18:07:18 -0700170
Robert Phillips7c525e62018-06-12 10:11:12 -0400171std::unique_ptr<GrDrawOp> Make(GrContext* context,
172 GrPaint&& paint,
173 const SkMatrix& viewMatrix,
174 const SkRegion& region,
175 GrAAType aaType,
176 const GrUserStencilSettings* stencilSettings) {
Brian Salomonf0366322017-07-11 15:53:05 -0400177 if (aaType != GrAAType::kNone && aaType != GrAAType::kMSAA) {
178 return nullptr;
179 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400180 return RegionOp::Make(context, std::move(paint), viewMatrix, region, aaType, stencilSettings);
msarettcc319b92016-08-25 18:07:18 -0700181}
Brian Salomonfc527d22016-12-14 21:07:01 -0500182}
Brian Salomonf0366322017-07-11 15:53:05 -0400183
184#if GR_TEST_UTILS
185
186GR_DRAW_OP_TEST_DEFINE(RegionOp) {
187 SkRegion region;
188 int n = random->nextULessThan(200);
189 for (int i = 0; i < n; ++i) {
190 SkIPoint center;
191 center.fX = random->nextULessThan(1000);
192 center.fY = random->nextULessThan(1000);
193 int w = random->nextRangeU(10, 1000);
194 int h = random->nextRangeU(10, 1000);
195 SkIRect rect = {center.fX - w / 2, center.fY - h / 2, center.fX + w / 2, center.fY + h / 2};
196 SkRegion::Op op;
197 if (i == 0) {
198 op = SkRegion::kReplace_Op;
199 } else {
200 // Pick an other than replace.
201 GR_STATIC_ASSERT(SkRegion::kLastOp == SkRegion::kReplace_Op);
202 op = (SkRegion::Op)random->nextULessThan(SkRegion::kLastOp);
203 }
204 region.op(rect, op);
205 }
206 SkMatrix viewMatrix = GrTest::TestMatrix(random);
207 GrAAType aaType = GrAAType::kNone;
208 if (GrFSAAType::kUnifiedMSAA == fsaaType && random->nextBool()) {
209 aaType = GrAAType::kMSAA;
210 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400211 return RegionOp::Make(context, std::move(paint), viewMatrix, region, aaType,
Brian Salomon8514ebf2017-08-01 11:09:09 -0400212 GrGetRandomStencil(random, context));
Brian Salomonf0366322017-07-11 15:53:05 -0400213}
214
215#endif