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/GrProgramInfo.h"
17#include "src/gpu/GrResourceProvider.h"
18#include "src/gpu/GrVertexWriter.h"
19#include "src/gpu/ops/GrMeshDrawOp.h"
20#include "src/gpu/ops/GrSimpleMeshDrawOpHelperWithStencil.h"
21
22static GrGeometryProcessor* make_gp(SkArenaAlloc* arena,
23 const SkMatrix& viewMatrix,
24 bool wideColor) {
25 using namespace GrDefaultGeoProcFactory;
26 Color::Type colorType = wideColor ? Color::kPremulWideColorAttribute_Type
27 : Color::kPremulGrColorAttribute_Type;
28 return GrDefaultGeoProcFactory::Make(arena, colorType, Coverage::kSolid_Type,
29 LocalCoords::kUsePosition_Type, viewMatrix);
30}
31
32namespace {
33
34class RegionOp final : public GrMeshDrawOp {
35private:
36 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
37
38public:
39 DEFINE_OP_CLASS_ID
40
41 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* 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
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, IsHairline::kNo);
63 }
64
65 const char* name() const override { return "GrRegionOp"; }
66
67 void visitProxies(const VisitProxyFunc& func) const override {
68 if (fProgramInfo) {
69 fProgramInfo->visitFPProxies(func);
70 } else {
71 fHelper.visitProxies(func);
72 }
73 }
74
75#ifdef SK_DEBUG
76 SkString dumpInfo() const override {
77 SkString str;
78 str.appendf("# combined: %d\n", fRegions.count());
79 for (int i = 0; i < fRegions.count(); ++i) {
80 const RegionInfo& info = fRegions[i];
81 str.appendf("%d: Color: 0x%08x, Region with %d rects\n", i, info.fColor.toBytes_RGBA(),
82 info.fRegion.computeRegionComplexity());
83 }
84 str += fHelper.dumpInfo();
85 str += INHERITED::dumpInfo();
86 return str;
87 }
88#endif
89
90 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
91
92 GrProcessorSet::Analysis finalize(
93 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
94 GrClampType clampType) override {
95 return fHelper.finalizeProcessors(
96 caps, clip, hasMixedSampledCoverage, clampType, GrProcessorAnalysisCoverage::kNone,
97 &fRegions[0].fColor, &fWideColor);
98 }
99
100private:
101 GrProgramInfo* programInfo() override { return fProgramInfo; }
102
103 void onCreateProgramInfo(const GrCaps* caps,
104 SkArenaAlloc* arena,
105 const GrSurfaceProxyView* writeView,
106 GrAppliedClip&& appliedClip,
107 const GrXferProcessor::DstProxyView& dstProxyView) override {
108 GrGeometryProcessor* gp = make_gp(arena, fViewMatrix, fWideColor);
109 if (!gp) {
110 SkDebugf("Couldn't create GrGeometryProcessor\n");
111 return;
112 }
113
114 fProgramInfo = fHelper.createProgramInfoWithStencil(caps, arena, writeView,
115 std::move(appliedClip), dstProxyView,
116 gp, GrPrimitiveType::kTriangles);
117 }
118
119 void onPrepareDraws(Target* target) override {
120 if (!fProgramInfo) {
121 this->createProgramInfo(target);
122 if (!fProgramInfo) {
123 return;
124 }
125 }
126
127 int numRegions = fRegions.count();
128 int numRects = 0;
129 for (int i = 0; i < numRegions; i++) {
130 numRects += fRegions[i].fRegion.computeRegionComplexity();
131 }
132
133 if (!numRects) {
134 return;
135 }
136
137 QuadHelper helper(target, fProgramInfo->primProc().vertexStride(), numRects);
138
139 GrVertexWriter vertices{helper.vertices()};
140 if (!vertices.fPtr) {
141 SkDebugf("Could not allocate vertices\n");
142 return;
143 }
144
145 for (int i = 0; i < numRegions; i++) {
146 GrVertexColor color(fRegions[i].fColor, fWideColor);
147 SkRegion::Iterator iter(fRegions[i].fRegion);
148 while (!iter.done()) {
149 SkRect rect = SkRect::Make(iter.rect());
150 vertices.writeQuad(GrVertexWriter::TriStripFromRect(rect), color);
151 iter.next();
152 }
153 }
154
155 fMesh = helper.mesh();
156 }
157
158 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
159 if (!fProgramInfo || !fMesh) {
160 return;
161 }
162
163 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
164 flushState->bindTextures(fProgramInfo->primProc(), nullptr, fProgramInfo->pipeline());
165 flushState->drawMesh(*fMesh);
166 }
167
168 CombineResult onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*,
169 const GrCaps& caps) override {
170 RegionOp* that = t->cast<RegionOp>();
171 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
172 return CombineResult::kCannotCombine;
173 }
174
175 if (fViewMatrix != that->fViewMatrix) {
176 return CombineResult::kCannotCombine;
177 }
178
179 fRegions.push_back_n(that->fRegions.count(), that->fRegions.begin());
180 fWideColor |= that->fWideColor;
181 return CombineResult::kMerged;
182 }
183
184 struct RegionInfo {
185 SkPMColor4f fColor;
186 SkRegion fRegion;
187 };
188
189 Helper fHelper;
190 SkMatrix fViewMatrix;
191 SkSTArray<1, RegionInfo, true> fRegions;
192 bool fWideColor;
193
194 GrSimpleMesh* fMesh = nullptr;
195 GrProgramInfo* fProgramInfo = nullptr;
196
197 typedef GrMeshDrawOp INHERITED;
198};
199
200} // anonymous namespace
201
202namespace GrRegionOp {
203
204std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
205 GrPaint&& paint,
206 const SkMatrix& viewMatrix,
207 const SkRegion& region,
208 GrAAType aaType,
209 const GrUserStencilSettings* stencilSettings) {
210 if (aaType != GrAAType::kNone && aaType != GrAAType::kMSAA) {
211 return nullptr;
212 }
213 return RegionOp::Make(context, std::move(paint), viewMatrix, region, aaType, stencilSettings);
214}
215}
216
217#if GR_TEST_UTILS
218
219GR_DRAW_OP_TEST_DEFINE(RegionOp) {
220 SkRegion region;
221 int n = random->nextULessThan(200);
222 for (int i = 0; i < n; ++i) {
223 SkIPoint center;
224 center.fX = random->nextULessThan(1000);
225 center.fY = random->nextULessThan(1000);
226 int w = random->nextRangeU(10, 1000);
227 int h = random->nextRangeU(10, 1000);
228 SkIRect rect = {center.fX - w / 2, center.fY - h / 2, center.fX + w / 2, center.fY + h / 2};
229 SkRegion::Op op;
230 if (i == 0) {
231 op = SkRegion::kReplace_Op;
232 } else {
233 // Pick an other than replace.
234 static_assert(SkRegion::kLastOp == SkRegion::kReplace_Op);
235 op = (SkRegion::Op)random->nextULessThan(SkRegion::kLastOp);
236 }
237 region.op(rect, op);
238 }
239 SkMatrix viewMatrix = GrTest::TestMatrix(random);
240 GrAAType aaType = GrAAType::kNone;
241 if (numSamples > 1 && random->nextBool()) {
242 aaType = GrAAType::kMSAA;
243 }
244 return RegionOp::Make(context, std::move(paint), viewMatrix, region, aaType,
245 GrGetRandomStencil(random, context));
246}
247
248#endif
249