blob: 23a5020f6b0b99e399e5498aefaf7a1509dd6840 [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"
msarettcc319b92016-08-25 18:07:18 -070015#include "SkMatrixPriv.h"
16#include "SkRegion.h"
17
18static const int kVertsPerInstance = 4;
19static const int kIndicesPerInstance = 6;
20
Brian Salomon8c852be2017-01-04 10:44:42 -050021static sk_sp<GrGeometryProcessor> make_gp(const SkMatrix& viewMatrix) {
msarettcc319b92016-08-25 18:07:18 -070022 using namespace GrDefaultGeoProcFactory;
Brian Salomon3de0aee2017-01-29 09:34:17 -050023 return GrDefaultGeoProcFactory::Make(Color::kPremulGrColorAttribute_Type, Coverage::kSolid_Type,
Brian Salomon8c852be2017-01-04 10:44:42 -050024 LocalCoords::kUsePosition_Type, viewMatrix);
msarettcc319b92016-08-25 18:07:18 -070025}
26
msarettfebb2242016-08-26 12:49:27 -070027static void tesselate_region(intptr_t vertices,
Brian Salomonfc527d22016-12-14 21:07:01 -050028 size_t vertexStride,
29 GrColor color,
30 const SkRegion& region) {
msarettcc319b92016-08-25 18:07:18 -070031 SkRegion::Iterator iter(region);
32
33 intptr_t verts = vertices;
34 while (!iter.done()) {
msarettfebb2242016-08-26 12:49:27 -070035 SkRect rect = SkRect::Make(iter.rect());
Brian Salomonfc527d22016-12-14 21:07:01 -050036 SkPoint* position = (SkPoint*)verts;
msarettfebb2242016-08-26 12:49:27 -070037 position->setRectFan(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, vertexStride);
msarettcc319b92016-08-25 18:07:18 -070038
39 static const int kColorOffset = sizeof(SkPoint);
40 GrColor* vertColor = reinterpret_cast<GrColor*>(verts + kColorOffset);
41 for (int i = 0; i < kVertsPerInstance; i++) {
42 *vertColor = color;
Brian Salomonfc527d22016-12-14 21:07:01 -050043 vertColor = (GrColor*)((intptr_t)vertColor + vertexStride);
msarettcc319b92016-08-25 18:07:18 -070044 }
45
46 verts += vertexStride * kVertsPerInstance;
47 iter.next();
48 }
msarettcc319b92016-08-25 18:07:18 -070049}
50
Brian Salomonf0366322017-07-11 15:53:05 -040051namespace {
52
53class RegionOp final : public GrMeshDrawOp {
54private:
Brian Salomon8514ebf2017-08-01 11:09:09 -040055 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
Brian Salomonf0366322017-07-11 15:53:05 -040056
msarettcc319b92016-08-25 18:07:18 -070057public:
Brian Salomon25a88092016-12-01 09:36:50 -050058 DEFINE_OP_CLASS_ID
msarettcc319b92016-08-25 18:07:18 -070059
Brian Salomonf0366322017-07-11 15:53:05 -040060 static std::unique_ptr<GrDrawOp> Make(GrPaint&& paint, const SkMatrix& viewMatrix,
Brian Salomon8514ebf2017-08-01 11:09:09 -040061 const SkRegion& region, GrAAType aaType,
62 const GrUserStencilSettings* stencilSettings = nullptr) {
63 return Helper::FactoryHelper<RegionOp>(std::move(paint), viewMatrix, region, aaType,
64 stencilSettings);
Brian Salomonf0366322017-07-11 15:53:05 -040065 }
66
67 RegionOp(const Helper::MakeArgs& helperArgs, GrColor color, const SkMatrix& viewMatrix,
Brian Salomon8514ebf2017-08-01 11:09:09 -040068 const SkRegion& region, GrAAType aaType, const GrUserStencilSettings* stencilSettings)
69 : INHERITED(ClassID())
70 , fHelper(helperArgs, aaType, stencilSettings)
71 , fViewMatrix(viewMatrix) {
msarettcc319b92016-08-25 18:07:18 -070072 RegionInfo& info = fRegions.push_back();
73 info.fColor = color;
msarettcc319b92016-08-25 18:07:18 -070074 info.fRegion = region;
75
76 SkRect bounds = SkRect::Make(region.getBounds());
77 this->setTransformedBounds(bounds, viewMatrix, HasAABloat::kNo, IsZeroArea::kNo);
78 }
79
Brian Salomonfc527d22016-12-14 21:07:01 -050080 const char* name() const override { return "GrRegionOp"; }
msarettcc319b92016-08-25 18:07:18 -070081
Robert Phillipsf1748f52017-09-14 14:11:24 -040082 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -040083 fHelper.visitProxies(func);
84 }
85
msarettcc319b92016-08-25 18:07:18 -070086 SkString dumpInfo() const override {
87 SkString str;
Brian Salomon53e4c3c2016-12-21 11:38:53 -050088 str.appendf("# combined: %d\n", fRegions.count());
msarettcc319b92016-08-25 18:07:18 -070089 for (int i = 0; i < fRegions.count(); ++i) {
90 const RegionInfo& info = fRegions[i];
Brian Salomonfc527d22016-12-14 21:07:01 -050091 str.appendf("%d: Color: 0x%08x, Region with %d rects\n", i, info.fColor,
92 info.fRegion.computeRegionComplexity());
msarettcc319b92016-08-25 18:07:18 -070093 }
Brian Salomonf0366322017-07-11 15:53:05 -040094 str += fHelper.dumpInfo();
95 str += INHERITED::dumpInfo();
msarettcc319b92016-08-25 18:07:18 -070096 return str;
97 }
98
Brian Salomonf0366322017-07-11 15:53:05 -040099 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
100
101 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip) override {
102 return fHelper.xpRequiresDstTexture(caps, clip, GrProcessorAnalysisCoverage::kNone,
103 &fRegions[0].fColor);
104 }
105
msarettcc319b92016-08-25 18:07:18 -0700106private:
Brian Salomon91326c32017-08-09 16:02:19 -0400107 void onPrepareDraws(Target* target) override {
Brian Salomon8c852be2017-01-04 10:44:42 -0500108 sk_sp<GrGeometryProcessor> gp = make_gp(fViewMatrix);
msarettcc319b92016-08-25 18:07:18 -0700109 if (!gp) {
110 SkDebugf("Couldn't create GrGeometryProcessor\n");
111 return;
112 }
msarettfebb2242016-08-26 12:49:27 -0700113 SkASSERT(gp->getVertexStride() == sizeof(GrDefaultGeoProcFactory::PositionColorAttr));
msarettcc319b92016-08-25 18:07:18 -0700114
115 int numRegions = fRegions.count();
116 int numRects = 0;
117 for (int i = 0; i < numRegions; i++) {
118 numRects += fRegions[i].fRegion.computeRegionComplexity();
119 }
120
Brian Salomonf0366322017-07-11 15:53:05 -0400121 if (!numRects) {
122 return;
123 }
msarettcc319b92016-08-25 18:07:18 -0700124 size_t vertexStride = gp->getVertexStride();
Hal Canary144caf52016-11-07 17:57:18 -0500125 sk_sp<const GrBuffer> indexBuffer(target->resourceProvider()->refQuadIndexBuffer());
Chris Dalton3809bab2017-06-13 10:55:06 -0600126 PatternHelper helper(GrPrimitiveType::kTriangles);
Brian Salomonfc527d22016-12-14 21:07:01 -0500127 void* vertices =
Chris Daltonbca46e22017-05-15 11:03:26 -0600128 helper.init(target, vertexStride, indexBuffer.get(), kVertsPerInstance,
129 kIndicesPerInstance, numRects);
msarettcc319b92016-08-25 18:07:18 -0700130 if (!vertices || !indexBuffer) {
131 SkDebugf("Could not allocate vertices\n");
132 return;
133 }
134
135 intptr_t verts = reinterpret_cast<intptr_t>(vertices);
136 for (int i = 0; i < numRegions; i++) {
msarettfebb2242016-08-26 12:49:27 -0700137 tesselate_region(verts, vertexStride, fRegions[i].fColor, fRegions[i].fRegion);
138 int numRectsInRegion = fRegions[i].fRegion.computeRegionComplexity();
msarettcc319b92016-08-25 18:07:18 -0700139 verts += numRectsInRegion * kVertsPerInstance * vertexStride;
140 }
Brian Salomonf0366322017-07-11 15:53:05 -0400141 helper.recordDraw(target, gp.get(), fHelper.makePipeline(target));
msarettcc319b92016-08-25 18:07:18 -0700142 }
143
Brian Salomon25a88092016-12-01 09:36:50 -0500144 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Brian Salomonfc527d22016-12-14 21:07:01 -0500145 RegionOp* that = t->cast<RegionOp>();
Brian Salomonf0366322017-07-11 15:53:05 -0400146 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
msarettcc319b92016-08-25 18:07:18 -0700147 return false;
148 }
149
msarettfebb2242016-08-26 12:49:27 -0700150 if (fViewMatrix != that->fViewMatrix) {
151 return false;
152 }
153
msarettcc319b92016-08-25 18:07:18 -0700154 fRegions.push_back_n(that->fRegions.count(), that->fRegions.begin());
155 this->joinBounds(*that);
156 return true;
157 }
158
159 struct RegionInfo {
160 GrColor fColor;
msarettcc319b92016-08-25 18:07:18 -0700161 SkRegion fRegion;
162 };
163
Brian Salomonf0366322017-07-11 15:53:05 -0400164 Helper fHelper;
msarettfebb2242016-08-26 12:49:27 -0700165 SkMatrix fViewMatrix;
msarettcc319b92016-08-25 18:07:18 -0700166 SkSTArray<1, RegionInfo, true> fRegions;
167
Brian Salomonf0366322017-07-11 15:53:05 -0400168 typedef GrMeshDrawOp INHERITED;
msarettcc319b92016-08-25 18:07:18 -0700169};
170
Brian Salomonf0366322017-07-11 15:53:05 -0400171} // anonymous namespace
172
Brian Salomonfc527d22016-12-14 21:07:01 -0500173namespace GrRegionOp {
msarettcc319b92016-08-25 18:07:18 -0700174
Brian Salomonf0366322017-07-11 15:53:05 -0400175std::unique_ptr<GrDrawOp> Make(GrPaint&& paint, const SkMatrix& viewMatrix, const SkRegion& region,
Stan Iliev73d8fd92017-08-02 15:36:24 -0400176 GrAAType aaType, const GrUserStencilSettings* stencilSettings) {
Brian Salomonf0366322017-07-11 15:53:05 -0400177 if (aaType != GrAAType::kNone && aaType != GrAAType::kMSAA) {
178 return nullptr;
179 }
Stan Iliev73d8fd92017-08-02 15:36:24 -0400180 return RegionOp::Make(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 }
Brian Salomon8514ebf2017-08-01 11:09:09 -0400211 return RegionOp::Make(std::move(paint), viewMatrix, region, aaType,
212 GrGetRandomStencil(random, context));
Brian Salomonf0366322017-07-11 15:53:05 -0400213}
214
215#endif