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 "GrRegionOp.h"
9 #include <GrDrawOpTest.h>
10 #include "GrDefaultGeoProcFactory.h"
11 #include "GrMeshDrawOp.h"
12 #include "GrOpFlushState.h"
13 #include "GrResourceProvider.h"
14 #include "GrSimpleMeshDrawOpHelper.h"
15 #include "GrVertexWriter.h"
16 #include "SkMatrixPriv.h"
17 #include "SkRegion.h"
18 
19 static const int kVertsPerInstance = 4;
20 static const int kIndicesPerInstance = 6;
21 
make_gp(const GrShaderCaps * shaderCaps,const SkMatrix & viewMatrix,bool wideColor)22 static sk_sp<GrGeometryProcessor> make_gp(const GrShaderCaps* shaderCaps,
23                                           const SkMatrix& viewMatrix,
24                                           bool wideColor) {
25     using namespace GrDefaultGeoProcFactory;
26     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);
30 }
31 
32 namespace {
33 
34 class RegionOp final : public GrMeshDrawOp {
35 private:
36     using Helper = GrSimpleMeshDrawOpHelperWithStencil;
37 
38 public:
39     DEFINE_OP_CLASS_ID
40 
Make(GrContext * context,GrPaint && paint,const SkMatrix & viewMatrix,const SkRegion & region,GrAAType aaType,const GrUserStencilSettings * stencilSettings=nullptr)41     static std::unique_ptr<GrDrawOp> Make(GrContext* context,
42                                           GrPaint&& paint,
43                                           const SkMatrix& viewMatrix,
44                                           const SkRegion& region,
45                                           GrAAType aaType,
46                                           const GrUserStencilSettings* stencilSettings = nullptr) {
47         return Helper::FactoryHelper<RegionOp>(context, std::move(paint), viewMatrix, region,
48                                                aaType, stencilSettings);
49     }
50 
RegionOp(const Helper::MakeArgs & helperArgs,const SkPMColor4f & color,const SkMatrix & viewMatrix,const SkRegion & region,GrAAType aaType,const GrUserStencilSettings * stencilSettings)51     RegionOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color,
52              const SkMatrix& viewMatrix, const SkRegion& region, GrAAType aaType,
53              const GrUserStencilSettings* stencilSettings)
54             : INHERITED(ClassID())
55             , fHelper(helperArgs, aaType, stencilSettings)
56             , fViewMatrix(viewMatrix) {
57         RegionInfo& info = fRegions.push_back();
58         info.fColor = color;
59         info.fRegion = region;
60 
61         SkRect bounds = SkRect::Make(region.getBounds());
62         this->setTransformedBounds(bounds, viewMatrix, HasAABloat::kNo, IsZeroArea::kNo);
63         fWideColor = !SkPMColor4fFitsInBytes(color);
64     }
65 
name() const66     const char* name() const override { return "GrRegionOp"; }
67 
visitProxies(const VisitProxyFunc & func,VisitorType) const68     void visitProxies(const VisitProxyFunc& func, VisitorType) const override {
69         fHelper.visitProxies(func);
70     }
71 
72 #ifdef SK_DEBUG
dumpInfo() const73     SkString dumpInfo() const override {
74         SkString str;
75         str.appendf("# combined: %d\n", fRegions.count());
76         for (int i = 0; i < fRegions.count(); ++i) {
77             const RegionInfo& info = fRegions[i];
78             str.appendf("%d: Color: 0x%08x, Region with %d rects\n", i, info.fColor.toBytes_RGBA(),
79                         info.fRegion.computeRegionComplexity());
80         }
81         str += fHelper.dumpInfo();
82         str += INHERITED::dumpInfo();
83         return str;
84     }
85 #endif
86 
fixedFunctionFlags() const87     FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
88 
finalize(const GrCaps & caps,const GrAppliedClip * clip)89     GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip) override {
90         return fHelper.finalizeProcessors(caps, clip, GrProcessorAnalysisCoverage::kNone,
91                                           &fRegions[0].fColor);
92     }
93 
94 private:
onPrepareDraws(Target * target)95     void onPrepareDraws(Target* target) override {
96         sk_sp<GrGeometryProcessor> gp = make_gp(target->caps().shaderCaps(), fViewMatrix,
97                                                 fWideColor);
98         if (!gp) {
99             SkDebugf("Couldn't create GrGeometryProcessor\n");
100             return;
101         }
102 
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 
109         if (!numRects) {
110             return;
111         }
112         sk_sp<const GrBuffer> indexBuffer = target->resourceProvider()->refQuadIndexBuffer();
113         if (!indexBuffer) {
114             SkDebugf("Could not allocate indices\n");
115             return;
116         }
117         PatternHelper helper(target, GrPrimitiveType::kTriangles, gp->vertexStride(),
118                              std::move(indexBuffer), kVertsPerInstance, kIndicesPerInstance,
119                              numRects);
120         GrVertexWriter vertices{helper.vertices()};
121         if (!vertices.fPtr) {
122             SkDebugf("Could not allocate vertices\n");
123             return;
124         }
125 
126         for (int i = 0; i < numRegions; i++) {
127             GrVertexColor color(fRegions[i].fColor, fWideColor);
128             SkRegion::Iterator iter(fRegions[i].fRegion);
129             while (!iter.done()) {
130                 SkRect rect = SkRect::Make(iter.rect());
131                 vertices.writeQuad(GrVertexWriter::TriStripFromRect(rect), color);
132                 iter.next();
133             }
134         }
135         auto pipe = fHelper.makePipeline(target);
136         helper.recordDraw(target, std::move(gp), pipe.fPipeline, pipe.fFixedDynamicState);
137     }
138 
onCombineIfPossible(GrOp * t,const GrCaps & caps)139     CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
140         RegionOp* that = t->cast<RegionOp>();
141         if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
142             return CombineResult::kCannotCombine;
143         }
144 
145         if (fViewMatrix != that->fViewMatrix) {
146             return CombineResult::kCannotCombine;
147         }
148 
149         fRegions.push_back_n(that->fRegions.count(), that->fRegions.begin());
150         fWideColor |= that->fWideColor;
151         return CombineResult::kMerged;
152     }
153 
154     struct RegionInfo {
155         SkPMColor4f fColor;
156         SkRegion fRegion;
157     };
158 
159     Helper fHelper;
160     SkMatrix fViewMatrix;
161     SkSTArray<1, RegionInfo, true> fRegions;
162     bool fWideColor;
163 
164     typedef GrMeshDrawOp INHERITED;
165 };
166 
167 }  // anonymous namespace
168 
169 namespace GrRegionOp {
170 
Make(GrContext * context,GrPaint && paint,const SkMatrix & viewMatrix,const SkRegion & region,GrAAType aaType,const GrUserStencilSettings * stencilSettings)171 std::unique_ptr<GrDrawOp> Make(GrContext* context,
172                                GrPaint&& paint,
173                                const SkMatrix& viewMatrix,
174                                const SkRegion& region,
175                                GrAAType aaType,
176                                const GrUserStencilSettings* stencilSettings) {
177     if (aaType != GrAAType::kNone && aaType != GrAAType::kMSAA) {
178         return nullptr;
179     }
180     return RegionOp::Make(context, std::move(paint), viewMatrix, region, aaType, stencilSettings);
181 }
182 }
183 
184 #if GR_TEST_UTILS
185 
GR_DRAW_OP_TEST_DEFINE(RegionOp)186 GR_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     }
211     return RegionOp::Make(context, std::move(paint), viewMatrix, region, aaType,
212                           GrGetRandomStencil(random, context));
213 }
214 
215 #endif
216