msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 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 | |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 8 | #include "GrRegionOp.h" |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 9 | #include <GrDrawOpTest.h> |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame] | 10 | #include "GrDefaultGeoProcFactory.h" |
| 11 | #include "GrMeshDrawOp.h" |
Brian Salomon | 742e31d | 2016-12-07 17:06:19 -0500 | [diff] [blame] | 12 | #include "GrOpFlushState.h" |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 13 | #include "GrResourceProvider.h" |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 14 | #include "GrSimpleMeshDrawOpHelper.h" |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 15 | #include "SkMatrixPriv.h" |
Cary Clark | 74f623d | 2017-11-06 20:02:02 -0500 | [diff] [blame] | 16 | #include "SkPointPriv.h" |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 17 | #include "SkRegion.h" |
| 18 | |
| 19 | static const int kVertsPerInstance = 4; |
| 20 | static const int kIndicesPerInstance = 6; |
| 21 | |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame^] | 22 | static sk_sp<GrGeometryProcessor> make_gp(const GrShaderCaps* shaderCaps, |
| 23 | const SkMatrix& viewMatrix) { |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 24 | using namespace GrDefaultGeoProcFactory; |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame^] | 25 | return GrDefaultGeoProcFactory::Make(shaderCaps, Color::kPremulGrColorAttribute_Type, |
| 26 | Coverage::kSolid_Type, LocalCoords::kUsePosition_Type, |
| 27 | viewMatrix); |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 28 | } |
| 29 | |
msarett | febb224 | 2016-08-26 12:49:27 -0700 | [diff] [blame] | 30 | static void tesselate_region(intptr_t vertices, |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 31 | size_t vertexStride, |
| 32 | GrColor color, |
| 33 | const SkRegion& region) { |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 34 | SkRegion::Iterator iter(region); |
| 35 | |
| 36 | intptr_t verts = vertices; |
| 37 | while (!iter.done()) { |
msarett | febb224 | 2016-08-26 12:49:27 -0700 | [diff] [blame] | 38 | SkRect rect = SkRect::Make(iter.rect()); |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 39 | SkPoint* position = (SkPoint*)verts; |
Brian Salomon | ec42e15 | 2018-05-18 12:52:22 -0400 | [diff] [blame] | 40 | SkPointPriv::SetRectTriStrip(position, rect, vertexStride); |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 41 | |
| 42 | static const int kColorOffset = sizeof(SkPoint); |
| 43 | GrColor* vertColor = reinterpret_cast<GrColor*>(verts + kColorOffset); |
| 44 | for (int i = 0; i < kVertsPerInstance; i++) { |
| 45 | *vertColor = color; |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 46 | vertColor = (GrColor*)((intptr_t)vertColor + vertexStride); |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | verts += vertexStride * kVertsPerInstance; |
| 50 | iter.next(); |
| 51 | } |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 54 | namespace { |
| 55 | |
| 56 | class RegionOp final : public GrMeshDrawOp { |
| 57 | private: |
Brian Salomon | 8514ebf | 2017-08-01 11:09:09 -0400 | [diff] [blame] | 58 | using Helper = GrSimpleMeshDrawOpHelperWithStencil; |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 59 | |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 60 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 61 | DEFINE_OP_CLASS_ID |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 62 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 63 | static std::unique_ptr<GrDrawOp> Make(GrContext* context, |
| 64 | GrPaint&& paint, |
| 65 | const SkMatrix& viewMatrix, |
| 66 | const SkRegion& region, |
| 67 | GrAAType aaType, |
Brian Salomon | 8514ebf | 2017-08-01 11:09:09 -0400 | [diff] [blame] | 68 | const GrUserStencilSettings* stencilSettings = nullptr) { |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 69 | return Helper::FactoryHelper<RegionOp>(context, std::move(paint), viewMatrix, region, |
| 70 | aaType, stencilSettings); |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | RegionOp(const Helper::MakeArgs& helperArgs, GrColor color, const SkMatrix& viewMatrix, |
Brian Salomon | 8514ebf | 2017-08-01 11:09:09 -0400 | [diff] [blame] | 74 | const SkRegion& region, GrAAType aaType, const GrUserStencilSettings* stencilSettings) |
| 75 | : INHERITED(ClassID()) |
| 76 | , fHelper(helperArgs, aaType, stencilSettings) |
| 77 | , fViewMatrix(viewMatrix) { |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 78 | RegionInfo& info = fRegions.push_back(); |
| 79 | info.fColor = color; |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 80 | info.fRegion = region; |
| 81 | |
| 82 | SkRect bounds = SkRect::Make(region.getBounds()); |
| 83 | this->setTransformedBounds(bounds, viewMatrix, HasAABloat::kNo, IsZeroArea::kNo); |
| 84 | } |
| 85 | |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 86 | const char* name() const override { return "GrRegionOp"; } |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 87 | |
Robert Phillips | f1748f5 | 2017-09-14 14:11:24 -0400 | [diff] [blame] | 88 | void visitProxies(const VisitProxyFunc& func) const override { |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 89 | fHelper.visitProxies(func); |
| 90 | } |
| 91 | |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 92 | SkString dumpInfo() const override { |
| 93 | SkString str; |
Brian Salomon | 53e4c3c | 2016-12-21 11:38:53 -0500 | [diff] [blame] | 94 | str.appendf("# combined: %d\n", fRegions.count()); |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 95 | for (int i = 0; i < fRegions.count(); ++i) { |
| 96 | const RegionInfo& info = fRegions[i]; |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 97 | str.appendf("%d: Color: 0x%08x, Region with %d rects\n", i, info.fColor, |
| 98 | info.fRegion.computeRegionComplexity()); |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 99 | } |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 100 | str += fHelper.dumpInfo(); |
| 101 | str += INHERITED::dumpInfo(); |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 102 | return str; |
| 103 | } |
| 104 | |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 105 | FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); } |
| 106 | |
Brian Osman | 532b3f9 | 2018-07-11 10:02:07 -0400 | [diff] [blame] | 107 | RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip) override { |
| 108 | return fHelper.xpRequiresDstTexture(caps, clip, GrProcessorAnalysisCoverage::kNone, |
| 109 | &fRegions[0].fColor); |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 110 | } |
| 111 | |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 112 | private: |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 113 | void onPrepareDraws(Target* target) override { |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame^] | 114 | sk_sp<GrGeometryProcessor> gp = make_gp(target->caps().shaderCaps(), fViewMatrix); |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 115 | if (!gp) { |
| 116 | SkDebugf("Couldn't create GrGeometryProcessor\n"); |
| 117 | return; |
| 118 | } |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 119 | static constexpr size_t kVertexStride = sizeof(GrDefaultGeoProcFactory::PositionColorAttr); |
| 120 | SkASSERT(kVertexStride == gp->debugOnly_vertexStride()); |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 121 | |
| 122 | int numRegions = fRegions.count(); |
| 123 | int numRects = 0; |
| 124 | for (int i = 0; i < numRegions; i++) { |
| 125 | numRects += fRegions[i].fRegion.computeRegionComplexity(); |
| 126 | } |
| 127 | |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 128 | if (!numRects) { |
| 129 | return; |
| 130 | } |
Brian Salomon | d28a79d | 2017-10-16 13:01:07 -0400 | [diff] [blame] | 131 | sk_sp<const GrBuffer> indexBuffer = target->resourceProvider()->refQuadIndexBuffer(); |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 132 | PatternHelper helper(GrPrimitiveType::kTriangles); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 133 | void* vertices = helper.init(target, kVertexStride, indexBuffer.get(), kVertsPerInstance, |
| 134 | kIndicesPerInstance, numRects); |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 135 | if (!vertices || !indexBuffer) { |
| 136 | SkDebugf("Could not allocate vertices\n"); |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | intptr_t verts = reinterpret_cast<intptr_t>(vertices); |
| 141 | for (int i = 0; i < numRegions; i++) { |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 142 | tesselate_region(verts, kVertexStride, fRegions[i].fColor, fRegions[i].fRegion); |
msarett | febb224 | 2016-08-26 12:49:27 -0700 | [diff] [blame] | 143 | int numRectsInRegion = fRegions[i].fRegion.computeRegionComplexity(); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 144 | verts += numRectsInRegion * kVertsPerInstance * kVertexStride; |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 145 | } |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 146 | auto pipe = fHelper.makePipeline(target); |
| 147 | helper.recordDraw(target, gp.get(), pipe.fPipeline, pipe.fFixedDynamicState); |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 150 | bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override { |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 151 | RegionOp* that = t->cast<RegionOp>(); |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 152 | if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) { |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 153 | return false; |
| 154 | } |
| 155 | |
msarett | febb224 | 2016-08-26 12:49:27 -0700 | [diff] [blame] | 156 | if (fViewMatrix != that->fViewMatrix) { |
| 157 | return false; |
| 158 | } |
| 159 | |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 160 | fRegions.push_back_n(that->fRegions.count(), that->fRegions.begin()); |
| 161 | this->joinBounds(*that); |
| 162 | return true; |
| 163 | } |
| 164 | |
| 165 | struct RegionInfo { |
| 166 | GrColor fColor; |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 167 | SkRegion fRegion; |
| 168 | }; |
| 169 | |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 170 | Helper fHelper; |
msarett | febb224 | 2016-08-26 12:49:27 -0700 | [diff] [blame] | 171 | SkMatrix fViewMatrix; |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 172 | SkSTArray<1, RegionInfo, true> fRegions; |
| 173 | |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 174 | typedef GrMeshDrawOp INHERITED; |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 175 | }; |
| 176 | |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 177 | } // anonymous namespace |
| 178 | |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 179 | namespace GrRegionOp { |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 180 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 181 | std::unique_ptr<GrDrawOp> Make(GrContext* context, |
| 182 | GrPaint&& paint, |
| 183 | const SkMatrix& viewMatrix, |
| 184 | const SkRegion& region, |
| 185 | GrAAType aaType, |
| 186 | const GrUserStencilSettings* stencilSettings) { |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 187 | if (aaType != GrAAType::kNone && aaType != GrAAType::kMSAA) { |
| 188 | return nullptr; |
| 189 | } |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 190 | return RegionOp::Make(context, std::move(paint), viewMatrix, region, aaType, stencilSettings); |
msarett | cc319b9 | 2016-08-25 18:07:18 -0700 | [diff] [blame] | 191 | } |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 192 | } |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 193 | |
| 194 | #if GR_TEST_UTILS |
| 195 | |
| 196 | GR_DRAW_OP_TEST_DEFINE(RegionOp) { |
| 197 | SkRegion region; |
| 198 | int n = random->nextULessThan(200); |
| 199 | for (int i = 0; i < n; ++i) { |
| 200 | SkIPoint center; |
| 201 | center.fX = random->nextULessThan(1000); |
| 202 | center.fY = random->nextULessThan(1000); |
| 203 | int w = random->nextRangeU(10, 1000); |
| 204 | int h = random->nextRangeU(10, 1000); |
| 205 | SkIRect rect = {center.fX - w / 2, center.fY - h / 2, center.fX + w / 2, center.fY + h / 2}; |
| 206 | SkRegion::Op op; |
| 207 | if (i == 0) { |
| 208 | op = SkRegion::kReplace_Op; |
| 209 | } else { |
| 210 | // Pick an other than replace. |
| 211 | GR_STATIC_ASSERT(SkRegion::kLastOp == SkRegion::kReplace_Op); |
| 212 | op = (SkRegion::Op)random->nextULessThan(SkRegion::kLastOp); |
| 213 | } |
| 214 | region.op(rect, op); |
| 215 | } |
| 216 | SkMatrix viewMatrix = GrTest::TestMatrix(random); |
| 217 | GrAAType aaType = GrAAType::kNone; |
| 218 | if (GrFSAAType::kUnifiedMSAA == fsaaType && random->nextBool()) { |
| 219 | aaType = GrAAType::kMSAA; |
| 220 | } |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 221 | return RegionOp::Make(context, std::move(paint), viewMatrix, region, aaType, |
Brian Salomon | 8514ebf | 2017-08-01 11:09:09 -0400 | [diff] [blame] | 222 | GrGetRandomStencil(random, context)); |
Brian Salomon | f036632 | 2017-07-11 15:53:05 -0400 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | #endif |