1 /*
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
8 #include "src/gpu/ops/GrRegionOp.h"
9
10 #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"
20
21 static const int kVertsPerInstance = 4;
22 static const int kIndicesPerInstance = 6;
23
make_gp(const GrShaderCaps * shaderCaps,const SkMatrix & viewMatrix,bool wideColor)24 static sk_sp<GrGeometryProcessor> make_gp(const GrShaderCaps* shaderCaps,
25 const SkMatrix& viewMatrix,
26 bool wideColor) {
27 using namespace GrDefaultGeoProcFactory;
28 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);
32 }
33
34 namespace {
35
36 class RegionOp final : public GrMeshDrawOp {
37 private:
38 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
39
40 public:
41 DEFINE_OP_CLASS_ID
42
Make(GrRecordingContext * context,GrPaint && paint,const SkMatrix & viewMatrix,const SkRegion & region,GrAAType aaType,const GrUserStencilSettings * stencilSettings=nullptr)43 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
44 GrPaint&& paint,
45 const SkMatrix& viewMatrix,
46 const SkRegion& region,
47 GrAAType aaType,
48 const GrUserStencilSettings* stencilSettings = nullptr) {
49 return Helper::FactoryHelper<RegionOp>(context, std::move(paint), viewMatrix, region,
50 aaType, stencilSettings);
51 }
52
RegionOp(const Helper::MakeArgs & helperArgs,const SkPMColor4f & color,const SkMatrix & viewMatrix,const SkRegion & region,GrAAType aaType,const GrUserStencilSettings * stencilSettings)53 RegionOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color,
54 const SkMatrix& viewMatrix, const SkRegion& region, GrAAType aaType,
55 const GrUserStencilSettings* stencilSettings)
56 : INHERITED(ClassID())
57 , fHelper(helperArgs, aaType, stencilSettings)
58 , fViewMatrix(viewMatrix) {
59 RegionInfo& info = fRegions.push_back();
60 info.fColor = color;
61 info.fRegion = region;
62
63 SkRect bounds = SkRect::Make(region.getBounds());
64 this->setTransformedBounds(bounds, viewMatrix, HasAABloat::kNo, IsZeroArea::kNo);
65 }
66
name() const67 const char* name() const override { return "GrRegionOp"; }
68
visitProxies(const VisitProxyFunc & func) const69 void visitProxies(const VisitProxyFunc& func) const override {
70 fHelper.visitProxies(func);
71 }
72
73 #ifdef SK_DEBUG
dumpInfo() const74 SkString dumpInfo() const override {
75 SkString str;
76 str.appendf("# combined: %d\n", fRegions.count());
77 for (int i = 0; i < fRegions.count(); ++i) {
78 const RegionInfo& info = fRegions[i];
79 str.appendf("%d: Color: 0x%08x, Region with %d rects\n", i, info.fColor.toBytes_RGBA(),
80 info.fRegion.computeRegionComplexity());
81 }
82 str += fHelper.dumpInfo();
83 str += INHERITED::dumpInfo();
84 return str;
85 }
86 #endif
87
fixedFunctionFlags() const88 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
89
finalize(const GrCaps & caps,const GrAppliedClip * clip,bool hasMixedSampledCoverage,GrClampType clampType)90 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);
96 }
97
98 private:
onPrepareDraws(Target * target)99 void onPrepareDraws(Target* target) override {
100 sk_sp<GrGeometryProcessor> gp = make_gp(target->caps().shaderCaps(), fViewMatrix,
101 fWideColor);
102 if (!gp) {
103 SkDebugf("Couldn't create GrGeometryProcessor\n");
104 return;
105 }
106
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
113 if (!numRects) {
114 return;
115 }
116 sk_sp<const GrGpuBuffer> indexBuffer = target->resourceProvider()->refQuadIndexBuffer();
117 if (!indexBuffer) {
118 SkDebugf("Could not allocate indices\n");
119 return;
120 }
121 PatternHelper helper(target, GrPrimitiveType::kTriangles, gp->vertexStride(),
122 std::move(indexBuffer), kVertsPerInstance, kIndicesPerInstance,
123 numRects);
124 GrVertexWriter vertices{helper.vertices()};
125 if (!vertices.fPtr) {
126 SkDebugf("Could not allocate vertices\n");
127 return;
128 }
129
130 for (int i = 0; i < numRegions; i++) {
131 GrVertexColor color(fRegions[i].fColor, fWideColor);
132 SkRegion::Iterator iter(fRegions[i].fRegion);
133 while (!iter.done()) {
134 SkRect rect = SkRect::Make(iter.rect());
135 vertices.writeQuad(GrVertexWriter::TriStripFromRect(rect), color);
136 iter.next();
137 }
138 }
139 helper.recordDraw(target, std::move(gp));
140 }
141
onExecute(GrOpFlushState * flushState,const SkRect & chainBounds)142 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
143 fHelper.executeDrawsAndUploads(this, flushState, chainBounds);
144 }
145
onCombineIfPossible(GrOp * t,const GrCaps & caps)146 CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
147 RegionOp* that = t->cast<RegionOp>();
148 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
149 return CombineResult::kCannotCombine;
150 }
151
152 if (fViewMatrix != that->fViewMatrix) {
153 return CombineResult::kCannotCombine;
154 }
155
156 fRegions.push_back_n(that->fRegions.count(), that->fRegions.begin());
157 fWideColor |= that->fWideColor;
158 return CombineResult::kMerged;
159 }
160
161 struct RegionInfo {
162 SkPMColor4f fColor;
163 SkRegion fRegion;
164 };
165
166 Helper fHelper;
167 SkMatrix fViewMatrix;
168 SkSTArray<1, RegionInfo, true> fRegions;
169 bool fWideColor;
170
171 typedef GrMeshDrawOp INHERITED;
172 };
173
174 } // anonymous namespace
175
176 namespace GrRegionOp {
177
Make(GrRecordingContext * context,GrPaint && paint,const SkMatrix & viewMatrix,const SkRegion & region,GrAAType aaType,const GrUserStencilSettings * stencilSettings)178 std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
179 GrPaint&& paint,
180 const SkMatrix& viewMatrix,
181 const SkRegion& region,
182 GrAAType aaType,
183 const GrUserStencilSettings* stencilSettings) {
184 if (aaType != GrAAType::kNone && aaType != GrAAType::kMSAA) {
185 return nullptr;
186 }
187 return RegionOp::Make(context, std::move(paint), viewMatrix, region, aaType, stencilSettings);
188 }
189 }
190
191 #if GR_TEST_UTILS
192
GR_DRAW_OP_TEST_DEFINE(RegionOp)193 GR_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;
215 if (numSamples > 1 && random->nextBool()) {
216 aaType = GrAAType::kMSAA;
217 }
218 return RegionOp::Make(context, std::move(paint), viewMatrix, region, aaType,
219 GrGetRandomStencil(random, context));
220 }
221
222 #endif
223