• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015 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 "GrLatticeOp.h"
9 #include "GrDefaultGeoProcFactory.h"
10 #include "GrDrawOpTest.h"
11 #include "GrMeshDrawOp.h"
12 #include "GrOpFlushState.h"
13 #include "GrResourceProvider.h"
14 #include "GrSimpleMeshDrawOpHelper.h"
15 #include "SkBitmap.h"
16 #include "SkLatticeIter.h"
17 #include "SkRect.h"
18 
create_gp()19 static sk_sp<GrGeometryProcessor> create_gp() {
20     using namespace GrDefaultGeoProcFactory;
21     return GrDefaultGeoProcFactory::Make(Color::kPremulGrColorAttribute_Type, Coverage::kSolid_Type,
22                                          LocalCoords::kHasExplicit_Type, SkMatrix::I());
23 }
24 
25 namespace {
26 
27 class NonAALatticeOp final : public GrMeshDrawOp {
28 private:
29     using Helper = GrSimpleMeshDrawOpHelper;
30 
31 public:
32     DEFINE_OP_CLASS_ID
33 
34     static const int kVertsPerRect = 4;
35     static const int kIndicesPerRect = 6;
36 
Make(GrPaint && paint,const SkMatrix & viewMatrix,int imageWidth,int imageHeight,std::unique_ptr<SkLatticeIter> iter,const SkRect & dst)37     static std::unique_ptr<GrDrawOp> Make(GrPaint&& paint, const SkMatrix& viewMatrix,
38                                           int imageWidth, int imageHeight,
39                                           std::unique_ptr<SkLatticeIter> iter, const SkRect& dst) {
40         return Helper::FactoryHelper<NonAALatticeOp>(std::move(paint), viewMatrix, imageWidth,
41                                                      imageHeight, std::move(iter), dst);
42     }
43 
NonAALatticeOp(Helper::MakeArgs & helperArgs,GrColor color,const SkMatrix & viewMatrix,int imageWidth,int imageHeight,std::unique_ptr<SkLatticeIter> iter,const SkRect & dst)44     NonAALatticeOp(Helper::MakeArgs& helperArgs, GrColor color, const SkMatrix& viewMatrix,
45                    int imageWidth, int imageHeight, std::unique_ptr<SkLatticeIter> iter,
46                    const SkRect& dst)
47             : INHERITED(ClassID()), fHelper(helperArgs, GrAAType::kNone) {
48         Patch& patch = fPatches.push_back();
49         patch.fViewMatrix = viewMatrix;
50         patch.fColor = color;
51         patch.fIter = std::move(iter);
52         patch.fDst = dst;
53 
54         fImageWidth = imageWidth;
55         fImageHeight = imageHeight;
56 
57         // setup bounds
58         this->setTransformedBounds(patch.fDst, viewMatrix, HasAABloat::kNo, IsZeroArea::kNo);
59     }
60 
name() const61     const char* name() const override { return "NonAALatticeOp"; }
62 
dumpInfo() const63     SkString dumpInfo() const override {
64         SkString str;
65 
66         for (int i = 0; i < fPatches.count(); ++i) {
67             str.appendf("%d: Color: 0x%08x Dst [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", i,
68                         fPatches[i].fColor, fPatches[i].fDst.fLeft, fPatches[i].fDst.fTop,
69                         fPatches[i].fDst.fRight, fPatches[i].fDst.fBottom);
70         }
71 
72         str += fHelper.dumpInfo();
73         str += INHERITED::dumpInfo();
74         return str;
75     }
76 
fixedFunctionFlags() const77     FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
78 
finalize(const GrCaps & caps,const GrAppliedClip * clip)79     RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip) override {
80         return fHelper.xpRequiresDstTexture(caps, clip, GrProcessorAnalysisCoverage::kNone,
81                                             &fPatches.front().fColor);
82     }
83 
84 private:
onPrepareDraws(Target * target) const85     void onPrepareDraws(Target* target) const override {
86         sk_sp<GrGeometryProcessor> gp(create_gp());
87         if (!gp) {
88             SkDebugf("Couldn't create GrGeometryProcessor\n");
89             return;
90         }
91 
92         size_t vertexStride = gp->getVertexStride();
93         int patchCnt = fPatches.count();
94         int numRects = 0;
95         for (int i = 0; i < patchCnt; i++) {
96             numRects += fPatches[i].fIter->numRectsToDraw();
97         }
98 
99         if (!numRects) {
100             return;
101         }
102 
103         sk_sp<const GrBuffer> indexBuffer(target->resourceProvider()->refQuadIndexBuffer());
104         PatternHelper helper(GrPrimitiveType::kTriangles);
105         void* vertices = helper.init(target, vertexStride, indexBuffer.get(), kVertsPerRect,
106                                      kIndicesPerRect, numRects);
107         if (!vertices || !indexBuffer) {
108             SkDebugf("Could not allocate vertices\n");
109             return;
110         }
111 
112         intptr_t verts = reinterpret_cast<intptr_t>(vertices);
113         for (int i = 0; i < patchCnt; i++) {
114             const Patch& patch = fPatches[i];
115 
116             // Apply the view matrix here if it is scale-translate.  Otherwise, we need to
117             // wait until we've created the dst rects.
118             bool isScaleTranslate = patch.fViewMatrix.isScaleTranslate();
119             if (isScaleTranslate) {
120                 patch.fIter->mapDstScaleTranslate(patch.fViewMatrix);
121             }
122 
123             SkRect srcR, dstR;
124             intptr_t patchVerts = verts;
125             while (patch.fIter->next(&srcR, &dstR)) {
126                 SkPoint* positions = reinterpret_cast<SkPoint*>(verts);
127                 positions->setRectFan(dstR.fLeft, dstR.fTop, dstR.fRight, dstR.fBottom,
128                                       vertexStride);
129 
130                 // Setup local coords
131                 static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor);
132                 SkPoint* coords = reinterpret_cast<SkPoint*>(verts + kLocalOffset);
133                 coords->setRectFan(srcR.fLeft, srcR.fTop, srcR.fRight, srcR.fBottom, vertexStride);
134 
135                 static const int kColorOffset = sizeof(SkPoint);
136                 GrColor* vertColor = reinterpret_cast<GrColor*>(verts + kColorOffset);
137                 for (int j = 0; j < 4; ++j) {
138                     *vertColor = patch.fColor;
139                     vertColor = (GrColor*)((intptr_t)vertColor + vertexStride);
140                 }
141                 verts += kVertsPerRect * vertexStride;
142             }
143 
144             // If we didn't handle it above, apply the matrix here.
145             if (!isScaleTranslate) {
146                 SkPoint* positions = reinterpret_cast<SkPoint*>(patchVerts);
147                 patch.fViewMatrix.mapPointsWithStride(
148                         positions, vertexStride, kVertsPerRect * patch.fIter->numRectsToDraw());
149             }
150         }
151         helper.recordDraw(target, gp.get(), fHelper.makePipeline(target));
152     }
153 
onCombineIfPossible(GrOp * t,const GrCaps & caps)154     bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
155         NonAALatticeOp* that = t->cast<NonAALatticeOp>();
156         if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
157             return false;
158         }
159 
160         SkASSERT(this->fImageWidth == that->fImageWidth &&
161                  this->fImageHeight == that->fImageHeight);
162 
163         fPatches.move_back_n(that->fPatches.count(), that->fPatches.begin());
164         this->joinBounds(*that);
165         return true;
166     }
167 
168     struct Patch {
169         SkMatrix fViewMatrix;
170         std::unique_ptr<SkLatticeIter> fIter;
171         SkRect fDst;
172         GrColor fColor;
173     };
174 
175     Helper fHelper;
176     SkSTArray<1, Patch, true> fPatches;
177     int fImageWidth;
178     int fImageHeight;
179 
180     typedef GrMeshDrawOp INHERITED;
181 };
182 
183 }  // anonymous namespace
184 
185 namespace GrLatticeOp {
MakeNonAA(GrPaint && paint,const SkMatrix & viewMatrix,int imageWidth,int imageHeight,std::unique_ptr<SkLatticeIter> iter,const SkRect & dst)186 std::unique_ptr<GrDrawOp> MakeNonAA(GrPaint&& paint, const SkMatrix& viewMatrix, int imageWidth,
187                                     int imageHeight, std::unique_ptr<SkLatticeIter> iter,
188                                     const SkRect& dst) {
189     return NonAALatticeOp::Make(std::move(paint), viewMatrix, imageWidth, imageHeight,
190                                 std::move(iter), dst);
191 }
192 };
193 
194 #if GR_TEST_UTILS
195 
196 /** Randomly divides subset into count divs. */
init_random_divs(int divs[],int count,int subsetStart,int subsetStop,SkRandom * random)197 static void init_random_divs(int divs[], int count, int subsetStart, int subsetStop,
198                              SkRandom* random) {
199     // Rules for lattice divs: Must be strictly increasing and in the range
200     // [subsetStart, subsetStop).
201     // Not terribly efficient alg for generating random divs:
202     // 1) Start with minimum legal pixels between each div.
203     // 2) Randomly assign the remaining pixels of the subset to divs.
204     // 3) Convert from pixel counts to div offsets.
205 
206     // 1) Initially each divs[i] represents the number of pixels between
207     // div i-1 and i. The initial div is allowed to be at subsetStart. There
208     // must be one pixel spacing between subsequent divs.
209     divs[0] = 0;
210     for (int i = 1; i < count; ++i) {
211         divs[i] = 1;
212     }
213     // 2) Assign the remaining subset pixels to fall
214     int subsetLength = subsetStop - subsetStart;
215     for (int i = 0; i < subsetLength - count; ++i) {
216         // +1 because count divs means count+1 intervals.
217         int entry = random->nextULessThan(count + 1);
218         // We don't have an entry to  to store the count after the last div
219         if (entry < count) {
220             divs[entry]++;
221         }
222     }
223     // 3) Now convert the counts between divs to pixel indices, incorporating the subset's offset.
224     int offset = subsetStart;
225     for (int i = 0; i < count; ++i) {
226         divs[i] += offset;
227         offset = divs[i];
228     }
229 }
230 
GR_DRAW_OP_TEST_DEFINE(NonAALatticeOp)231 GR_DRAW_OP_TEST_DEFINE(NonAALatticeOp) {
232     SkCanvas::Lattice lattice;
233     int imgW, imgH;
234     // We loop because our random lattice code can produce an invalid lattice in the case where
235     // there is a single div separator in both x and y and both are aligned with the left and top
236     // edge of the image subset, respectively.
237     std::unique_ptr<int[]> xdivs;
238     std::unique_ptr<int[]> ydivs;
239     std::unique_ptr<SkCanvas::Lattice::Flags[]> flags;
240     SkIRect subset;
241     do {
242         imgW = random->nextRangeU(1, 1000);
243         imgH = random->nextRangeU(1, 1000);
244         if (random->nextBool()) {
245             subset.fLeft = random->nextULessThan(imgW);
246             subset.fRight = random->nextRangeU(subset.fLeft + 1, imgW);
247             subset.fTop = random->nextULessThan(imgH);
248             subset.fBottom = random->nextRangeU(subset.fTop + 1, imgH);
249         } else {
250             subset.setXYWH(0, 0, imgW, imgH);
251         }
252         // SkCanvas::Lattice allows bounds to be null. However, SkCanvas creates a temp Lattice with a
253         // non-null bounds before creating a SkLatticeIter since SkLatticeIter requires a bounds.
254         lattice.fBounds = &subset;
255         lattice.fXCount = random->nextRangeU(1, subset.width());
256         lattice.fYCount = random->nextRangeU(1, subset.height());
257         xdivs.reset(new int[lattice.fXCount]);
258         ydivs.reset(new int[lattice.fYCount]);
259         init_random_divs(xdivs.get(), lattice.fXCount, subset.fLeft, subset.fRight, random);
260         init_random_divs(ydivs.get(), lattice.fYCount, subset.fTop, subset.fBottom, random);
261         lattice.fXDivs = xdivs.get();
262         lattice.fYDivs = ydivs.get();
263         bool hasFlags = random->nextBool();
264         if (hasFlags) {
265             int n = (lattice.fXCount + 1) * (lattice.fYCount + 1);
266             flags.reset(new SkCanvas::Lattice::Flags[n]);
267             for (int i = 0; i < n; ++i) {
268                 flags[i] = random->nextBool() ? SkCanvas::Lattice::kTransparent_Flags
269                                               : (SkCanvas::Lattice::Flags)0;
270             }
271             lattice.fFlags = flags.get();
272         } else {
273             lattice.fFlags = nullptr;
274         }
275     } while (!SkLatticeIter::Valid(imgW, imgH, lattice));
276 
277     SkRect dst;
278     dst.fLeft = random->nextRangeScalar(-2000.5f, 1000.f);
279     dst.fTop = random->nextRangeScalar(-2000.5f, 1000.f);
280     dst.fRight = dst.fLeft + random->nextRangeScalar(0.5f, 1000.f);
281     dst.fBottom = dst.fTop + random->nextRangeScalar(0.5f, 1000.f);
282     std::unique_ptr<SkLatticeIter> iter(new SkLatticeIter(lattice, dst));
283     SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
284     return NonAALatticeOp::Make(std::move(paint), viewMatrix, imgW, imgH, std::move(iter), dst);
285 }
286 
287 #endif
288