Line data Source code
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 :
10 : #include "GrDefaultGeoProcFactory.h"
11 : #include "GrMeshDrawOp.h"
12 : #include "GrOpFlushState.h"
13 : #include "GrResourceProvider.h"
14 : #include "SkMatrixPriv.h"
15 : #include "SkRegion.h"
16 :
17 : static const int kVertsPerInstance = 4;
18 : static const int kIndicesPerInstance = 6;
19 :
20 0 : static sk_sp<GrGeometryProcessor> make_gp(const SkMatrix& viewMatrix) {
21 : using namespace GrDefaultGeoProcFactory;
22 : return GrDefaultGeoProcFactory::Make(Color::kPremulGrColorAttribute_Type, Coverage::kSolid_Type,
23 0 : LocalCoords::kUsePosition_Type, viewMatrix);
24 : }
25 :
26 0 : static void tesselate_region(intptr_t vertices,
27 : size_t vertexStride,
28 : GrColor color,
29 : const SkRegion& region) {
30 0 : SkRegion::Iterator iter(region);
31 :
32 0 : intptr_t verts = vertices;
33 0 : while (!iter.done()) {
34 0 : SkRect rect = SkRect::Make(iter.rect());
35 0 : SkPoint* position = (SkPoint*)verts;
36 0 : position->setRectFan(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, vertexStride);
37 :
38 : static const int kColorOffset = sizeof(SkPoint);
39 0 : GrColor* vertColor = reinterpret_cast<GrColor*>(verts + kColorOffset);
40 0 : for (int i = 0; i < kVertsPerInstance; i++) {
41 0 : *vertColor = color;
42 0 : vertColor = (GrColor*)((intptr_t)vertColor + vertexStride);
43 : }
44 :
45 0 : verts += vertexStride * kVertsPerInstance;
46 0 : iter.next();
47 : }
48 0 : }
49 :
50 0 : class RegionOp final : public GrLegacyMeshDrawOp {
51 : public:
52 0 : DEFINE_OP_CLASS_ID
53 :
54 0 : RegionOp(GrColor color, const SkMatrix& viewMatrix, const SkRegion& region)
55 0 : : INHERITED(ClassID()), fViewMatrix(viewMatrix) {
56 0 : RegionInfo& info = fRegions.push_back();
57 0 : info.fColor = color;
58 0 : info.fRegion = region;
59 :
60 0 : SkRect bounds = SkRect::Make(region.getBounds());
61 0 : this->setTransformedBounds(bounds, viewMatrix, HasAABloat::kNo, IsZeroArea::kNo);
62 0 : }
63 :
64 0 : const char* name() const override { return "GrRegionOp"; }
65 :
66 0 : SkString dumpInfo() const override {
67 0 : SkString str;
68 0 : str.appendf("# combined: %d\n", fRegions.count());
69 0 : for (int i = 0; i < fRegions.count(); ++i) {
70 0 : const RegionInfo& info = fRegions[i];
71 0 : str.appendf("%d: Color: 0x%08x, Region with %d rects\n", i, info.fColor,
72 0 : info.fRegion.computeRegionComplexity());
73 : }
74 0 : str.append(DumpPipelineInfo(*this->pipeline()));
75 0 : str.append(INHERITED::dumpInfo());
76 0 : return str;
77 : }
78 :
79 : private:
80 0 : void getProcessorAnalysisInputs(GrProcessorAnalysisColor* color,
81 : GrProcessorAnalysisCoverage* coverage) const override {
82 0 : color->setToConstant(fRegions[0].fColor);
83 0 : *coverage = GrProcessorAnalysisCoverage::kNone;
84 0 : }
85 :
86 0 : void applyPipelineOptimizations(const PipelineOptimizations& optimizations) override {
87 0 : optimizations.getOverrideColorIfSet(&fRegions[0].fColor);
88 0 : }
89 :
90 0 : void onPrepareDraws(Target* target) const override {
91 0 : sk_sp<GrGeometryProcessor> gp = make_gp(fViewMatrix);
92 0 : if (!gp) {
93 0 : SkDebugf("Couldn't create GrGeometryProcessor\n");
94 0 : return;
95 : }
96 0 : SkASSERT(gp->getVertexStride() == sizeof(GrDefaultGeoProcFactory::PositionColorAttr));
97 :
98 0 : int numRegions = fRegions.count();
99 0 : int numRects = 0;
100 0 : for (int i = 0; i < numRegions; i++) {
101 0 : numRects += fRegions[i].fRegion.computeRegionComplexity();
102 : }
103 :
104 0 : size_t vertexStride = gp->getVertexStride();
105 0 : sk_sp<const GrBuffer> indexBuffer(target->resourceProvider()->refQuadIndexBuffer());
106 0 : InstancedHelper helper;
107 : void* vertices =
108 0 : helper.init(target, kTriangles_GrPrimitiveType, vertexStride, indexBuffer.get(),
109 0 : kVertsPerInstance, kIndicesPerInstance, numRects);
110 0 : if (!vertices || !indexBuffer) {
111 0 : SkDebugf("Could not allocate vertices\n");
112 0 : return;
113 : }
114 :
115 0 : intptr_t verts = reinterpret_cast<intptr_t>(vertices);
116 0 : for (int i = 0; i < numRegions; i++) {
117 0 : tesselate_region(verts, vertexStride, fRegions[i].fColor, fRegions[i].fRegion);
118 0 : int numRectsInRegion = fRegions[i].fRegion.computeRegionComplexity();
119 0 : verts += numRectsInRegion * kVertsPerInstance * vertexStride;
120 : }
121 0 : helper.recordDraw(target, gp.get(), this->pipeline());
122 : }
123 :
124 0 : bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
125 0 : RegionOp* that = t->cast<RegionOp>();
126 0 : if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
127 : that->bounds(), caps)) {
128 0 : return false;
129 : }
130 :
131 0 : if (fViewMatrix != that->fViewMatrix) {
132 0 : return false;
133 : }
134 :
135 0 : fRegions.push_back_n(that->fRegions.count(), that->fRegions.begin());
136 0 : this->joinBounds(*that);
137 0 : return true;
138 : }
139 :
140 0 : struct RegionInfo {
141 : GrColor fColor;
142 : SkRegion fRegion;
143 : };
144 :
145 : SkMatrix fViewMatrix;
146 : SkSTArray<1, RegionInfo, true> fRegions;
147 :
148 : typedef GrLegacyMeshDrawOp INHERITED;
149 : };
150 :
151 : namespace GrRegionOp {
152 :
153 0 : std::unique_ptr<GrLegacyMeshDrawOp> Make(GrColor color, const SkMatrix& viewMatrix,
154 : const SkRegion& region) {
155 0 : return std::unique_ptr<GrLegacyMeshDrawOp>(new RegionOp(color, viewMatrix, region));
156 : }
157 : }
|