blob: 0d600ed30535c85f9166f1344a215fe5eb1dd69a [file] [log] [blame]
joshualitt33a5fce2015-11-18 13:28:51 -08001/*
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
Brian Salomonfc527d22016-12-14 21:07:01 -05008#include "GrLatticeOp.h"
joshualitt33a5fce2015-11-18 13:28:51 -08009#include "GrDefaultGeoProcFactory.h"
Brian Salomon815486c2017-07-11 08:52:13 -040010#include "GrDrawOpTest.h"
Brian Salomondad29232016-12-01 16:40:24 -050011#include "GrMeshDrawOp.h"
Brian Salomon742e31d2016-12-07 17:06:19 -050012#include "GrOpFlushState.h"
joshualitt33a5fce2015-11-18 13:28:51 -080013#include "GrResourceProvider.h"
Brian Salomon815486c2017-07-11 08:52:13 -040014#include "GrSimpleMeshDrawOpHelper.h"
joshualitt33a5fce2015-11-18 13:28:51 -080015#include "SkBitmap.h"
msarettc573a402016-08-02 08:05:56 -070016#include "SkLatticeIter.h"
Brian Salomonfa3783f2018-01-05 13:49:07 -050017#include "SkMatrixPriv.h"
Cary Clark74f623d2017-11-06 20:02:02 -050018#include "SkPointPriv.h"
joshualitt33a5fce2015-11-18 13:28:51 -080019#include "SkRect.h"
Brian Salomon2a943df2018-05-04 13:43:19 -040020#include "glsl/GrGLSLColorSpaceXformHelper.h"
21#include "glsl/GrGLSLGeometryProcessor.h"
22#include "glsl/GrGLSLVarying.h"
joshualitt33a5fce2015-11-18 13:28:51 -080023
Brian Salomon815486c2017-07-11 08:52:13 -040024namespace {
25
Brian Salomon2a943df2018-05-04 13:43:19 -040026class LatticeGP : public GrGeometryProcessor {
27public:
28 struct Vertex {
29 SkPoint fPosition;
30 SkPoint fTextureCoords;
31 SkRect fTextureDomain;
32 GrColor fColor;
33 };
34
35 static sk_sp<GrGeometryProcessor> Make(sk_sp<GrTextureProxy> proxy,
36 sk_sp<GrColorSpaceXform> csxf,
37 GrSamplerState::Filter filter) {
38 return sk_sp<GrGeometryProcessor>(new LatticeGP(std::move(proxy), std::move(csxf), filter));
39 }
40
41 const char* name() const override { return "LatticeGP"; }
42
43 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
44 b->add32(GrColorSpaceXform::XformKey(fColorSpaceXform.get()));
45 }
46
47 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps& caps) const override {
48 class GLSLProcessor : public GrGLSLGeometryProcessor {
49 public:
50 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc,
51 FPCoordTransformIter&& transformIter) override {
52 const auto& latticeGP = proc.cast<LatticeGP>();
53 this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter);
54 if (fColorSpaceXformHelper.isValid()) {
55 fColorSpaceXformHelper.setData(pdman, latticeGP.fColorSpaceXform.get());
56 }
57 }
58
59 private:
60 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
61 using Interpolation = GrGLSLVaryingHandler::Interpolation;
62 const auto& latticeGP = args.fGP.cast<LatticeGP>();
63 fColorSpaceXformHelper.emitCode(args.fUniformHandler,
64 latticeGP.fColorSpaceXform.get());
65
66 args.fVaryingHandler->emitAttributes(latticeGP);
67 this->writeOutputPosition(args.fVertBuilder, gpArgs, latticeGP.fPositions.fName);
68 this->emitTransforms(args.fVertBuilder,
69 args.fVaryingHandler,
70 args.fUniformHandler,
71 latticeGP.fTextureCoords.asShaderVar(),
72 args.fFPCoordTransformHandler);
73 args.fFragBuilder->codeAppend("float2 textureCoords;");
74 args.fVaryingHandler->addPassThroughAttribute(&latticeGP.fTextureCoords,
75 "textureCoords");
76 args.fFragBuilder->codeAppend("float4 textureDomain;");
77 args.fVaryingHandler->addPassThroughAttribute(
78 &latticeGP.fTextureDomain, "textureDomain", Interpolation::kCanBeFlat);
79 args.fVaryingHandler->addPassThroughAttribute(&latticeGP.fColors, args.fOutputColor,
80 Interpolation::kCanBeFlat);
81 args.fFragBuilder->codeAppendf("%s = ", args.fOutputColor);
82 args.fFragBuilder->appendTextureLookupAndModulate(
83 args.fOutputColor,
84 args.fTexSamplers[0],
85 "clamp(textureCoords, textureDomain.xy, textureDomain.zw)",
86 kFloat2_GrSLType,
87 &fColorSpaceXformHelper);
88 args.fFragBuilder->codeAppend(";");
89 args.fFragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage);
90 }
91 GrGLSLColorSpaceXformHelper fColorSpaceXformHelper;
92 };
93 return new GLSLProcessor;
94 }
95
96private:
97 LatticeGP(sk_sp<GrTextureProxy> proxy, sk_sp<GrColorSpaceXform> csxf,
98 GrSamplerState::Filter filter)
99 : INHERITED(kLatticeGP_ClassID), fColorSpaceXform(std::move(csxf)) {
100 fPositions = this->addVertexAttrib("position", kFloat2_GrVertexAttribType);
101 fSampler.reset(std::move(proxy), filter);
102 this->addTextureSampler(&fSampler);
103 fTextureCoords = this->addVertexAttrib("textureCoords", kFloat2_GrVertexAttribType);
104 fTextureDomain = this->addVertexAttrib("textureDomain", kFloat4_GrVertexAttribType);
105 fColors = this->addVertexAttrib("color", kUByte4_norm_GrVertexAttribType);
106 }
107
108 Attribute fPositions;
109 Attribute fTextureCoords;
110 Attribute fTextureDomain;
111 Attribute fColors;
112 sk_sp<GrColorSpaceXform> fColorSpaceXform;
113 TextureSampler fSampler;
114
115 typedef GrGeometryProcessor INHERITED;
116};
117
Brian Salomon815486c2017-07-11 08:52:13 -0400118class NonAALatticeOp final : public GrMeshDrawOp {
119private:
120 using Helper = GrSimpleMeshDrawOpHelper;
121
joshualitt33a5fce2015-11-18 13:28:51 -0800122public:
Brian Salomon25a88092016-12-01 09:36:50 -0500123 DEFINE_OP_CLASS_ID
joshualitt33a5fce2015-11-18 13:28:51 -0800124
125 static const int kVertsPerRect = 4;
126 static const int kIndicesPerRect = 6;
joshualitt33a5fce2015-11-18 13:28:51 -0800127
Brian Salomon815486c2017-07-11 08:52:13 -0400128 static std::unique_ptr<GrDrawOp> Make(GrPaint&& paint, const SkMatrix& viewMatrix,
Brian Salomon2a943df2018-05-04 13:43:19 -0400129 sk_sp<GrTextureProxy> proxy,
130 sk_sp<GrColorSpaceXform> colorSpaceXForm,
131 GrSamplerState::Filter filter,
Brian Salomon815486c2017-07-11 08:52:13 -0400132 std::unique_ptr<SkLatticeIter> iter, const SkRect& dst) {
Brian Salomond3cee172018-05-07 16:40:48 -0400133 SkASSERT(proxy);
Brian Salomon2a943df2018-05-04 13:43:19 -0400134 return Helper::FactoryHelper<NonAALatticeOp>(std::move(paint), viewMatrix, std::move(proxy),
135 std::move(colorSpaceXForm), filter,
136 std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -0400137 }
138
139 NonAALatticeOp(Helper::MakeArgs& helperArgs, GrColor color, const SkMatrix& viewMatrix,
Brian Salomon2a943df2018-05-04 13:43:19 -0400140 sk_sp<GrTextureProxy> proxy, sk_sp<GrColorSpaceXform> colorSpaceXform,
141 GrSamplerState::Filter filter, std::unique_ptr<SkLatticeIter> iter,
Brian Salomon815486c2017-07-11 08:52:13 -0400142 const SkRect& dst)
Brian Salomon2a943df2018-05-04 13:43:19 -0400143 : INHERITED(ClassID())
144 , fHelper(helperArgs, GrAAType::kNone)
145 , fProxy(std::move(proxy))
146 , fColorSpaceXform(std::move(colorSpaceXform))
147 , fFilter(filter) {
bsalomona71b8982016-06-30 12:13:52 -0700148 Patch& patch = fPatches.push_back();
149 patch.fViewMatrix = viewMatrix;
150 patch.fColor = color;
msarett10e3d9b2016-08-18 15:46:03 -0700151 patch.fIter = std::move(iter);
bsalomona71b8982016-06-30 12:13:52 -0700152 patch.fDst = dst;
joshualitt33a5fce2015-11-18 13:28:51 -0800153
joshualitt33a5fce2015-11-18 13:28:51 -0800154 // setup bounds
bsalomon88cf17d2016-07-08 06:40:56 -0700155 this->setTransformedBounds(patch.fDst, viewMatrix, HasAABloat::kNo, IsZeroArea::kNo);
joshualitt33a5fce2015-11-18 13:28:51 -0800156 }
157
Brian Salomonfc527d22016-12-14 21:07:01 -0500158 const char* name() const override { return "NonAALatticeOp"; }
robertphillips783a4da2015-11-19 14:00:02 -0800159
Robert Phillipsf1748f52017-09-14 14:11:24 -0400160 void visitProxies(const VisitProxyFunc& func) const override {
Brian Salomond3cee172018-05-07 16:40:48 -0400161 func(fProxy.get());
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400162 fHelper.visitProxies(func);
163 }
164
robertphillips783a4da2015-11-19 14:00:02 -0800165 SkString dumpInfo() const override {
166 SkString str;
167
bsalomona71b8982016-06-30 12:13:52 -0700168 for (int i = 0; i < fPatches.count(); ++i) {
Brian Salomonfc527d22016-12-14 21:07:01 -0500169 str.appendf("%d: Color: 0x%08x Dst [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", i,
170 fPatches[i].fColor, fPatches[i].fDst.fLeft, fPatches[i].fDst.fTop,
bsalomona71b8982016-06-30 12:13:52 -0700171 fPatches[i].fDst.fRight, fPatches[i].fDst.fBottom);
robertphillips783a4da2015-11-19 14:00:02 -0800172 }
173
Brian Salomon815486c2017-07-11 08:52:13 -0400174 str += fHelper.dumpInfo();
175 str += INHERITED::dumpInfo();
robertphillips783a4da2015-11-19 14:00:02 -0800176 return str;
177 }
joshualitt33a5fce2015-11-18 13:28:51 -0800178
Brian Salomon815486c2017-07-11 08:52:13 -0400179 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
180
Brian Osman9a725dd2017-09-20 09:53:22 -0400181 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip,
182 GrPixelConfigIsClamped dstIsClamped) override {
Brian Salomon2a943df2018-05-04 13:43:19 -0400183 auto opaque = GrColorIsOpaque(fPatches[0].fColor) && GrPixelConfigIsOpaque(fProxy->config())
184 ? GrProcessorAnalysisColor::Opaque::kYes
185 : GrProcessorAnalysisColor::Opaque::kNo;
186 auto analysisColor = GrProcessorAnalysisColor(opaque);
187 auto result = fHelper.xpRequiresDstTexture(
188 caps, clip, dstIsClamped, GrProcessorAnalysisCoverage::kNone, &analysisColor);
189 analysisColor.isConstant(&fPatches[0].fColor);
190 return result;
Brian Salomon815486c2017-07-11 08:52:13 -0400191 }
192
Brian Salomon92aee3d2016-12-21 09:20:25 -0500193private:
Brian Salomon91326c32017-08-09 16:02:19 -0400194 void onPrepareDraws(Target* target) override {
Brian Salomon2a943df2018-05-04 13:43:19 -0400195 auto gp = LatticeGP::Make(fProxy, fColorSpaceXform, fFilter);
joshualitt33a5fce2015-11-18 13:28:51 -0800196 if (!gp) {
197 SkDebugf("Couldn't create GrGeometryProcessor\n");
198 return;
199 }
200
joshualitt33a5fce2015-11-18 13:28:51 -0800201 size_t vertexStride = gp->getVertexStride();
bsalomona71b8982016-06-30 12:13:52 -0700202 int patchCnt = fPatches.count();
msarett10e3d9b2016-08-18 15:46:03 -0700203 int numRects = 0;
204 for (int i = 0; i < patchCnt; i++) {
msarett0764efe2016-09-02 11:24:30 -0700205 numRects += fPatches[i].fIter->numRectsToDraw();
msarett10e3d9b2016-08-18 15:46:03 -0700206 }
joshualitt33a5fce2015-11-18 13:28:51 -0800207
Brian Salomon0db1b532017-07-12 15:21:43 -0400208 if (!numRects) {
209 return;
210 }
211
Brian Salomond28a79d2017-10-16 13:01:07 -0400212 sk_sp<const GrBuffer> indexBuffer = target->resourceProvider()->refQuadIndexBuffer();
Chris Dalton3809bab2017-06-13 10:55:06 -0600213 PatternHelper helper(GrPrimitiveType::kTriangles);
Chris Daltonbca46e22017-05-15 11:03:26 -0600214 void* vertices = helper.init(target, vertexStride, indexBuffer.get(), kVertsPerRect,
215 kIndicesPerRect, numRects);
joshualitt33a5fce2015-11-18 13:28:51 -0800216 if (!vertices || !indexBuffer) {
217 SkDebugf("Could not allocate vertices\n");
218 return;
219 }
220
msarett10e3d9b2016-08-18 15:46:03 -0700221 intptr_t verts = reinterpret_cast<intptr_t>(vertices);
bsalomona71b8982016-06-30 12:13:52 -0700222 for (int i = 0; i < patchCnt; i++) {
msarett7fc08582016-08-18 14:29:22 -0700223 const Patch& patch = fPatches[i];
msarett10e3d9b2016-08-18 15:46:03 -0700224
225 // Apply the view matrix here if it is scale-translate. Otherwise, we need to
226 // wait until we've created the dst rects.
227 bool isScaleTranslate = patch.fViewMatrix.isScaleTranslate();
228 if (isScaleTranslate) {
229 patch.fIter->mapDstScaleTranslate(patch.fViewMatrix);
230 }
joshualitt33a5fce2015-11-18 13:28:51 -0800231
Brian Salomon2a943df2018-05-04 13:43:19 -0400232 SkIRect srcR;
233 SkRect dstR;
msarett10e3d9b2016-08-18 15:46:03 -0700234 intptr_t patchVerts = verts;
Brian Salomon2a943df2018-05-04 13:43:19 -0400235 Sk4f scales(1.f / fProxy->width(), 1.f / fProxy->height(),
236 1.f / fProxy->width(), 1.f / fProxy->height());
237 static const Sk4f kDomainOffsets(0.5f, 0.5f, -0.5f, -0.5f);
238 static const Sk4f kFlipOffsets(0.f, 1, 0.f, 1.f);
239 static const Sk4f kFlipMuls(1.f, -1.f, 1.f, -1.f);
msarett10e3d9b2016-08-18 15:46:03 -0700240 while (patch.fIter->next(&srcR, &dstR)) {
Brian Salomon2a943df2018-05-04 13:43:19 -0400241 auto vertices = reinterpret_cast<LatticeGP::Vertex*>(verts);
Brian Salomonec42e152018-05-18 12:52:22 -0400242 SkPointPriv::SetRectTriStrip(&vertices->fPosition, dstR, vertexStride);
Brian Salomon2a943df2018-05-04 13:43:19 -0400243 Sk4f coords(SkIntToScalar(srcR.fLeft), SkIntToScalar(srcR.fTop),
244 SkIntToScalar(srcR.fRight), SkIntToScalar(srcR.fBottom));
245 Sk4f domain = coords + kDomainOffsets;
246 coords *= scales;
247 domain *= scales;
248 if (fProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
249 coords = kFlipMuls * coords + kFlipOffsets;
250 domain = SkNx_shuffle<0, 3, 2, 1>(kFlipMuls * domain + kFlipOffsets);
251 }
252 SkPointPriv::SetRectTriStrip(&vertices->fTextureCoords, coords[0], coords[1],
253 coords[2], coords[3], vertexStride);
254 for (int j = 0; j < kVertsPerRect; ++j) {
255 vertices[j].fTextureDomain = {domain[0], domain[1], domain[2], domain[3]};
256 }
joshualitt33a5fce2015-11-18 13:28:51 -0800257
Brian Salomon2a943df2018-05-04 13:43:19 -0400258 for (int j = 0; j < kVertsPerRect; ++j) {
259 vertices[j].fColor = patch.fColor;
joshualitt33a5fce2015-11-18 13:28:51 -0800260 }
261 verts += kVertsPerRect * vertexStride;
262 }
msarett10e3d9b2016-08-18 15:46:03 -0700263
264 // If we didn't handle it above, apply the matrix here.
265 if (!isScaleTranslate) {
266 SkPoint* positions = reinterpret_cast<SkPoint*>(patchVerts);
Brian Salomonfa3783f2018-01-05 13:49:07 -0500267 SkMatrixPriv::MapPointsWithStride(patch.fViewMatrix, positions, vertexStride,
268 kVertsPerRect * patch.fIter->numRectsToDraw());
msarett10e3d9b2016-08-18 15:46:03 -0700269 }
joshualitt33a5fce2015-11-18 13:28:51 -0800270 }
Brian Salomon815486c2017-07-11 08:52:13 -0400271 helper.recordDraw(target, gp.get(), fHelper.makePipeline(target));
joshualitt33a5fce2015-11-18 13:28:51 -0800272 }
273
Brian Salomon25a88092016-12-01 09:36:50 -0500274 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Brian Salomonfc527d22016-12-14 21:07:01 -0500275 NonAALatticeOp* that = t->cast<NonAALatticeOp>();
Brian Salomon2a943df2018-05-04 13:43:19 -0400276 if (fProxy != that->fProxy) {
277 return false;
278 }
279 if (fFilter != that->fFilter) {
280 return false;
281 }
282 if (GrColorSpaceXform::Equals(fColorSpaceXform.get(), that->fColorSpaceXform.get())) {
283 return false;
284 }
Brian Salomon815486c2017-07-11 08:52:13 -0400285 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
joshualitt33a5fce2015-11-18 13:28:51 -0800286 return false;
287 }
288
msarett10e3d9b2016-08-18 15:46:03 -0700289 fPatches.move_back_n(that->fPatches.count(), that->fPatches.begin());
bsalomon88cf17d2016-07-08 06:40:56 -0700290 this->joinBounds(*that);
joshualitt33a5fce2015-11-18 13:28:51 -0800291 return true;
292 }
293
bsalomona71b8982016-06-30 12:13:52 -0700294 struct Patch {
295 SkMatrix fViewMatrix;
msarett10e3d9b2016-08-18 15:46:03 -0700296 std::unique_ptr<SkLatticeIter> fIter;
bsalomona71b8982016-06-30 12:13:52 -0700297 SkRect fDst;
298 GrColor fColor;
299 };
300
Brian Salomon815486c2017-07-11 08:52:13 -0400301 Helper fHelper;
302 SkSTArray<1, Patch, true> fPatches;
Brian Salomon2a943df2018-05-04 13:43:19 -0400303 sk_sp<GrTextureProxy> fProxy;
304 sk_sp<GrColorSpaceXform> fColorSpaceXform;
305 GrSamplerState::Filter fFilter;
joshualitt33a5fce2015-11-18 13:28:51 -0800306
Brian Salomon815486c2017-07-11 08:52:13 -0400307 typedef GrMeshDrawOp INHERITED;
joshualitt33a5fce2015-11-18 13:28:51 -0800308};
309
Brian Salomon815486c2017-07-11 08:52:13 -0400310} // anonymous namespace
311
Brian Salomonfc527d22016-12-14 21:07:01 -0500312namespace GrLatticeOp {
Brian Salomon2a943df2018-05-04 13:43:19 -0400313std::unique_ptr<GrDrawOp> MakeNonAA(GrPaint&& paint, const SkMatrix& viewMatrix,
314 sk_sp<GrTextureProxy> proxy,
315 sk_sp<GrColorSpaceXform> colorSpaceXform,
316 GrSamplerState::Filter filter,
317 std::unique_ptr<SkLatticeIter> iter, const SkRect& dst) {
318 return NonAALatticeOp::Make(std::move(paint), viewMatrix, std::move(proxy),
319 std::move(colorSpaceXform), filter, std::move(iter), dst);
joshualitt33a5fce2015-11-18 13:28:51 -0800320}
321};
Brian Salomon815486c2017-07-11 08:52:13 -0400322
323#if GR_TEST_UTILS
Brian Salomon2a943df2018-05-04 13:43:19 -0400324#include "GrContextPriv.h"
325#include "GrProxyProvider.h"
Brian Salomon815486c2017-07-11 08:52:13 -0400326
327/** Randomly divides subset into count divs. */
328static void init_random_divs(int divs[], int count, int subsetStart, int subsetStop,
329 SkRandom* random) {
330 // Rules for lattice divs: Must be strictly increasing and in the range
331 // [subsetStart, subsetStop).
332 // Not terribly efficient alg for generating random divs:
333 // 1) Start with minimum legal pixels between each div.
334 // 2) Randomly assign the remaining pixels of the subset to divs.
335 // 3) Convert from pixel counts to div offsets.
336
337 // 1) Initially each divs[i] represents the number of pixels between
338 // div i-1 and i. The initial div is allowed to be at subsetStart. There
339 // must be one pixel spacing between subsequent divs.
340 divs[0] = 0;
341 for (int i = 1; i < count; ++i) {
342 divs[i] = 1;
343 }
344 // 2) Assign the remaining subset pixels to fall
345 int subsetLength = subsetStop - subsetStart;
346 for (int i = 0; i < subsetLength - count; ++i) {
347 // +1 because count divs means count+1 intervals.
348 int entry = random->nextULessThan(count + 1);
349 // We don't have an entry to to store the count after the last div
350 if (entry < count) {
351 divs[entry]++;
352 }
353 }
354 // 3) Now convert the counts between divs to pixel indices, incorporating the subset's offset.
355 int offset = subsetStart;
356 for (int i = 0; i < count; ++i) {
357 divs[i] += offset;
358 offset = divs[i];
359 }
360}
361
362GR_DRAW_OP_TEST_DEFINE(NonAALatticeOp) {
Brian Salomon815486c2017-07-11 08:52:13 -0400363 SkCanvas::Lattice lattice;
Brian Salomon18df7632017-07-11 14:32:18 -0400364 // We loop because our random lattice code can produce an invalid lattice in the case where
365 // there is a single div separator in both x and y and both are aligned with the left and top
366 // edge of the image subset, respectively.
367 std::unique_ptr<int[]> xdivs;
368 std::unique_ptr<int[]> ydivs;
Stan Ilievca8c0952017-12-11 13:01:58 -0500369 std::unique_ptr<SkCanvas::Lattice::RectType[]> flags;
370 std::unique_ptr<SkColor[]> colors;
Brian Salomon18df7632017-07-11 14:32:18 -0400371 SkIRect subset;
Brian Salomon2a943df2018-05-04 13:43:19 -0400372 GrSurfaceDesc desc;
373 desc.fConfig = kRGBA_8888_GrPixelConfig;
374 desc.fWidth = random->nextRangeU(1, 1000);
375 desc.fHeight = random->nextRangeU(1, 1000);
376 GrSurfaceOrigin origin =
377 random->nextBool() ? kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
378 auto proxy = context->contextPriv().proxyProvider()->createProxy(
379 desc, origin, SkBackingFit::kExact, SkBudgeted::kYes);
380
Brian Salomon18df7632017-07-11 14:32:18 -0400381 do {
Brian Salomon18df7632017-07-11 14:32:18 -0400382 if (random->nextBool()) {
Brian Salomon2a943df2018-05-04 13:43:19 -0400383 subset.fLeft = random->nextULessThan(desc.fWidth);
384 subset.fRight = random->nextRangeU(subset.fLeft + 1, desc.fWidth);
385 subset.fTop = random->nextULessThan(desc.fHeight);
386 subset.fBottom = random->nextRangeU(subset.fTop + 1, desc.fHeight);
Brian Salomon18df7632017-07-11 14:32:18 -0400387 } else {
Brian Salomon2a943df2018-05-04 13:43:19 -0400388 subset.setXYWH(0, 0, desc.fWidth, desc.fHeight);
Brian Salomon815486c2017-07-11 08:52:13 -0400389 }
Brian Salomon2a943df2018-05-04 13:43:19 -0400390 // SkCanvas::Lattice allows bounds to be null. However, SkCanvas creates a temp Lattice with
391 // a non-null bounds before creating a SkLatticeIter since SkLatticeIter requires a bounds.
Brian Salomon18df7632017-07-11 14:32:18 -0400392 lattice.fBounds = &subset;
393 lattice.fXCount = random->nextRangeU(1, subset.width());
394 lattice.fYCount = random->nextRangeU(1, subset.height());
395 xdivs.reset(new int[lattice.fXCount]);
396 ydivs.reset(new int[lattice.fYCount]);
397 init_random_divs(xdivs.get(), lattice.fXCount, subset.fLeft, subset.fRight, random);
398 init_random_divs(ydivs.get(), lattice.fYCount, subset.fTop, subset.fBottom, random);
399 lattice.fXDivs = xdivs.get();
400 lattice.fYDivs = ydivs.get();
401 bool hasFlags = random->nextBool();
402 if (hasFlags) {
403 int n = (lattice.fXCount + 1) * (lattice.fYCount + 1);
Stan Ilievca8c0952017-12-11 13:01:58 -0500404 flags.reset(new SkCanvas::Lattice::RectType[n]);
405 colors.reset(new SkColor[n]);
Brian Salomon18df7632017-07-11 14:32:18 -0400406 for (int i = 0; i < n; ++i) {
Stan Ilievca8c0952017-12-11 13:01:58 -0500407 flags[i] = random->nextBool() ? SkCanvas::Lattice::kTransparent
408 : SkCanvas::Lattice::kDefault;
Brian Salomon18df7632017-07-11 14:32:18 -0400409 }
Stan Ilievca8c0952017-12-11 13:01:58 -0500410 lattice.fRectTypes = flags.get();
411 lattice.fColors = colors.get();
Brian Salomon18df7632017-07-11 14:32:18 -0400412 } else {
Stan Ilievca8c0952017-12-11 13:01:58 -0500413 lattice.fRectTypes = nullptr;
414 lattice.fColors = nullptr;
Brian Salomon18df7632017-07-11 14:32:18 -0400415 }
Brian Salomon2a943df2018-05-04 13:43:19 -0400416 } while (!SkLatticeIter::Valid(desc.fWidth, desc.fHeight, lattice));
Brian Salomon815486c2017-07-11 08:52:13 -0400417 SkRect dst;
418 dst.fLeft = random->nextRangeScalar(-2000.5f, 1000.f);
419 dst.fTop = random->nextRangeScalar(-2000.5f, 1000.f);
420 dst.fRight = dst.fLeft + random->nextRangeScalar(0.5f, 1000.f);
421 dst.fBottom = dst.fTop + random->nextRangeScalar(0.5f, 1000.f);
422 std::unique_ptr<SkLatticeIter> iter(new SkLatticeIter(lattice, dst));
423 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
Brian Salomon2a943df2018-05-04 13:43:19 -0400424 auto csxf = GrTest::TestColorXform(random);
425 GrSamplerState::Filter filter =
426 random->nextBool() ? GrSamplerState::Filter::kNearest : GrSamplerState::Filter::kBilerp;
427 return NonAALatticeOp::Make(std::move(paint), viewMatrix, std::move(proxy), std::move(csxf),
428 filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -0400429}
430
431#endif