blob: 172814957fac35394e868aa122d532945d561f77 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkBitmap.h"
9#include "include/core/SkRect.h"
10#include "src/core/SkLatticeIter.h"
11#include "src/core/SkMatrixPriv.h"
12#include "src/gpu/GrDefaultGeoProcFactory.h"
13#include "src/gpu/GrDrawOpTest.h"
14#include "src/gpu/GrGpu.h"
15#include "src/gpu/GrOpFlushState.h"
16#include "src/gpu/GrResourceProvider.h"
17#include "src/gpu/GrResourceProviderPriv.h"
18#include "src/gpu/GrVertexWriter.h"
19#include "src/gpu/SkGr.h"
20#include "src/gpu/glsl/GrGLSLColorSpaceXformHelper.h"
21#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
22#include "src/gpu/glsl/GrGLSLVarying.h"
23#include "src/gpu/ops/GrLatticeOp.h"
24#include "src/gpu/ops/GrMeshDrawOp.h"
25#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
joshualitt33a5fce2015-11-18 13:28:51 -080026
Brian Salomon815486c2017-07-11 08:52:13 -040027namespace {
28
Brian Salomon2a943df2018-05-04 13:43:19 -040029class LatticeGP : public GrGeometryProcessor {
30public:
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050031 static GrGeometryProcessor* Make(SkArenaAlloc* arena,
32 const GrTextureProxy* proxy,
33 sk_sp<GrColorSpaceXform> csxf,
34 GrSamplerState::Filter filter,
35 bool wideColor) {
36 return arena->make<LatticeGP>(proxy, std::move(csxf), filter, wideColor);
Brian Salomon2a943df2018-05-04 13:43:19 -040037 }
38
39 const char* name() const override { return "LatticeGP"; }
40
41 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
42 b->add32(GrColorSpaceXform::XformKey(fColorSpaceXform.get()));
43 }
44
45 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps& caps) const override {
46 class GLSLProcessor : public GrGLSLGeometryProcessor {
47 public:
48 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc,
49 FPCoordTransformIter&& transformIter) override {
50 const auto& latticeGP = proc.cast<LatticeGP>();
51 this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter);
Brian Osmanc891b102018-06-14 14:50:17 -040052 fColorSpaceXformHelper.setData(pdman, latticeGP.fColorSpaceXform.get());
Brian Salomon2a943df2018-05-04 13:43:19 -040053 }
54
55 private:
56 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
57 using Interpolation = GrGLSLVaryingHandler::Interpolation;
58 const auto& latticeGP = args.fGP.cast<LatticeGP>();
59 fColorSpaceXformHelper.emitCode(args.fUniformHandler,
60 latticeGP.fColorSpaceXform.get());
61
62 args.fVaryingHandler->emitAttributes(latticeGP);
Brian Osmanf04fb3c2018-11-12 15:34:00 -050063 this->writeOutputPosition(args.fVertBuilder, gpArgs, latticeGP.fInPosition.name());
Brian Salomon2a943df2018-05-04 13:43:19 -040064 this->emitTransforms(args.fVertBuilder,
65 args.fVaryingHandler,
66 args.fUniformHandler,
Brian Osmanf04fb3c2018-11-12 15:34:00 -050067 latticeGP.fInTextureCoords.asShaderVar(),
Brian Salomon2a943df2018-05-04 13:43:19 -040068 args.fFPCoordTransformHandler);
69 args.fFragBuilder->codeAppend("float2 textureCoords;");
Brian Osmanf04fb3c2018-11-12 15:34:00 -050070 args.fVaryingHandler->addPassThroughAttribute(latticeGP.fInTextureCoords,
Brian Salomon2a943df2018-05-04 13:43:19 -040071 "textureCoords");
72 args.fFragBuilder->codeAppend("float4 textureDomain;");
73 args.fVaryingHandler->addPassThroughAttribute(
Brian Osmanf04fb3c2018-11-12 15:34:00 -050074 latticeGP.fInTextureDomain, "textureDomain", Interpolation::kCanBeFlat);
75 args.fVaryingHandler->addPassThroughAttribute(latticeGP.fInColor,
76 args.fOutputColor,
Brian Salomon2a943df2018-05-04 13:43:19 -040077 Interpolation::kCanBeFlat);
78 args.fFragBuilder->codeAppendf("%s = ", args.fOutputColor);
79 args.fFragBuilder->appendTextureLookupAndModulate(
80 args.fOutputColor,
81 args.fTexSamplers[0],
82 "clamp(textureCoords, textureDomain.xy, textureDomain.zw)",
83 kFloat2_GrSLType,
84 &fColorSpaceXformHelper);
85 args.fFragBuilder->codeAppend(";");
86 args.fFragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage);
87 }
88 GrGLSLColorSpaceXformHelper fColorSpaceXformHelper;
89 };
90 return new GLSLProcessor;
91 }
92
93private:
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050094 friend class ::SkArenaAlloc; // for access to ctor
95
96 LatticeGP(const GrTextureProxy* proxy, sk_sp<GrColorSpaceXform> csxf,
Brian Osman0b537032018-12-26 12:16:44 -050097 GrSamplerState::Filter filter, bool wideColor)
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050098 : INHERITED(kLatticeGP_ClassID)
99 , fColorSpaceXform(std::move(csxf)) {
Greg Daniel7a82edf2018-12-04 10:54:34 -0500100
Robert Phillips323471e2019-11-11 11:33:37 -0500101 fSampler.reset(GrSamplerState(GrSamplerState::WrapMode::kClamp, filter),
102 proxy->backendFormat(), proxy->textureSwizzle());
Brian Salomonf7dcd762018-07-30 14:48:15 -0400103 this->setTextureSamplerCnt(1);
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500104 fInPosition = {"position", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
105 fInTextureCoords = {"textureCoords", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
106 fInTextureDomain = {"textureDomain", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
Brian Osman0b537032018-12-26 12:16:44 -0500107 fInColor = MakeColorAttribute("color", wideColor);
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500108 this->setVertexAttributes(&fInPosition, 4);
Brian Salomon92be2f72018-06-19 14:33:47 -0400109 }
110
Brian Salomonf7dcd762018-07-30 14:48:15 -0400111 const TextureSampler& onTextureSampler(int) const override { return fSampler; }
112
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500113 Attribute fInPosition;
114 Attribute fInTextureCoords;
115 Attribute fInTextureDomain;
116 Attribute fInColor;
Brian Salomon92be2f72018-06-19 14:33:47 -0400117
Brian Salomon2a943df2018-05-04 13:43:19 -0400118 sk_sp<GrColorSpaceXform> fColorSpaceXform;
119 TextureSampler fSampler;
120
121 typedef GrGeometryProcessor INHERITED;
122};
123
Brian Salomon815486c2017-07-11 08:52:13 -0400124class NonAALatticeOp final : public GrMeshDrawOp {
125private:
126 using Helper = GrSimpleMeshDrawOpHelper;
127
joshualitt33a5fce2015-11-18 13:28:51 -0800128public:
Brian Salomon25a88092016-12-01 09:36:50 -0500129 DEFINE_OP_CLASS_ID
joshualitt33a5fce2015-11-18 13:28:51 -0800130
Robert Phillipsb97da532019-02-12 15:24:12 -0500131 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400132 GrPaint&& paint,
133 const SkMatrix& viewMatrix,
Brian Salomon2a943df2018-05-04 13:43:19 -0400134 sk_sp<GrTextureProxy> proxy,
Greg Danielc594e622019-10-15 14:01:49 -0400135 GrColorType srcColorType,
Brian Salomon2a943df2018-05-04 13:43:19 -0400136 sk_sp<GrColorSpaceXform> colorSpaceXForm,
137 GrSamplerState::Filter filter,
Robert Phillips7c525e62018-06-12 10:11:12 -0400138 std::unique_ptr<SkLatticeIter> iter,
139 const SkRect& dst) {
Brian Salomond3cee172018-05-07 16:40:48 -0400140 SkASSERT(proxy);
Robert Phillips7c525e62018-06-12 10:11:12 -0400141 return Helper::FactoryHelper<NonAALatticeOp>(context, std::move(paint), viewMatrix,
Greg Danielc594e622019-10-15 14:01:49 -0400142 std::move(proxy), srcColorType,
Brian Salomon2a943df2018-05-04 13:43:19 -0400143 std::move(colorSpaceXForm), filter,
144 std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -0400145 }
146
Brian Osmancf860852018-10-31 14:04:39 -0400147 NonAALatticeOp(Helper::MakeArgs& helperArgs, const SkPMColor4f& color,
148 const SkMatrix& viewMatrix, sk_sp<GrTextureProxy> proxy,
Greg Danielc594e622019-10-15 14:01:49 -0400149 GrColorType srcColorType, sk_sp<GrColorSpaceXform> colorSpaceXform,
150 GrSamplerState::Filter filter, std::unique_ptr<SkLatticeIter> iter,
151 const SkRect& dst)
Brian Salomon2a943df2018-05-04 13:43:19 -0400152 : INHERITED(ClassID())
153 , fHelper(helperArgs, GrAAType::kNone)
154 , fProxy(std::move(proxy))
Greg Danielc594e622019-10-15 14:01:49 -0400155 , fSrcColorType(srcColorType)
Brian Salomon2a943df2018-05-04 13:43:19 -0400156 , fColorSpaceXform(std::move(colorSpaceXform))
157 , fFilter(filter) {
bsalomona71b8982016-06-30 12:13:52 -0700158 Patch& patch = fPatches.push_back();
159 patch.fViewMatrix = viewMatrix;
160 patch.fColor = color;
msarett10e3d9b2016-08-18 15:46:03 -0700161 patch.fIter = std::move(iter);
bsalomona71b8982016-06-30 12:13:52 -0700162 patch.fDst = dst;
joshualitt33a5fce2015-11-18 13:28:51 -0800163
joshualitt33a5fce2015-11-18 13:28:51 -0800164 // setup bounds
Greg Daniel5faf4742019-10-01 15:14:44 -0400165 this->setTransformedBounds(patch.fDst, viewMatrix, HasAABloat::kNo, IsHairline::kNo);
joshualitt33a5fce2015-11-18 13:28:51 -0800166 }
167
Brian Salomonfc527d22016-12-14 21:07:01 -0500168 const char* name() const override { return "NonAALatticeOp"; }
robertphillips783a4da2015-11-19 14:00:02 -0800169
Chris Dalton1706cbf2019-05-21 19:35:29 -0600170 void visitProxies(const VisitProxyFunc& func) const override {
Chris Dalton7eb5c0f2019-05-23 15:15:47 -0600171 bool mipped = (GrSamplerState::Filter::kMipMap == fFilter);
172 func(fProxy.get(), GrMipMapped(mipped));
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400173 fHelper.visitProxies(func);
174 }
175
Brian Osman9a390ac2018-11-12 09:47:48 -0500176#ifdef SK_DEBUG
robertphillips783a4da2015-11-19 14:00:02 -0800177 SkString dumpInfo() const override {
178 SkString str;
179
bsalomona71b8982016-06-30 12:13:52 -0700180 for (int i = 0; i < fPatches.count(); ++i) {
Brian Salomonfc527d22016-12-14 21:07:01 -0500181 str.appendf("%d: Color: 0x%08x Dst [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", i,
Brian Osmancf860852018-10-31 14:04:39 -0400182 fPatches[i].fColor.toBytes_RGBA(), fPatches[i].fDst.fLeft,
Brian Osman1be2b7c2018-10-29 16:07:15 -0400183 fPatches[i].fDst.fTop, fPatches[i].fDst.fRight, fPatches[i].fDst.fBottom);
robertphillips783a4da2015-11-19 14:00:02 -0800184 }
185
Brian Salomon815486c2017-07-11 08:52:13 -0400186 str += fHelper.dumpInfo();
187 str += INHERITED::dumpInfo();
robertphillips783a4da2015-11-19 14:00:02 -0800188 return str;
189 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500190#endif
joshualitt33a5fce2015-11-18 13:28:51 -0800191
Brian Salomon815486c2017-07-11 08:52:13 -0400192 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
193
Chris Dalton6ce447a2019-06-23 18:07:38 -0600194 GrProcessorSet::Analysis finalize(
195 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
196 GrClampType clampType) override {
Greg Danielc594e622019-10-15 14:01:49 -0400197 auto opaque = fPatches[0].fColor.isOpaque() && !GrColorTypeHasAlpha(fSrcColorType)
Brian Salomon2a943df2018-05-04 13:43:19 -0400198 ? GrProcessorAnalysisColor::Opaque::kYes
199 : GrProcessorAnalysisColor::Opaque::kNo;
200 auto analysisColor = GrProcessorAnalysisColor(opaque);
Chris Dalton6ce447a2019-06-23 18:07:38 -0600201 auto result = fHelper.finalizeProcessors(
202 caps, clip, hasMixedSampledCoverage, clampType, GrProcessorAnalysisCoverage::kNone,
203 &analysisColor);
Brian Salomon2a943df2018-05-04 13:43:19 -0400204 analysisColor.isConstant(&fPatches[0].fColor);
Brian Osman8fa7ab42019-03-18 10:22:42 -0400205 fWideColor = SkPMColor4fNeedsWideColor(fPatches[0].fColor, clampType, caps);
Brian Salomon2a943df2018-05-04 13:43:19 -0400206 return result;
Brian Salomon815486c2017-07-11 08:52:13 -0400207 }
208
Brian Salomon92aee3d2016-12-21 09:20:25 -0500209private:
Brian Salomon91326c32017-08-09 16:02:19 -0400210 void onPrepareDraws(Target* target) override {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500211 auto gp = LatticeGP::Make(target->allocator(), fProxy.get(), fColorSpaceXform,
212 fFilter, fWideColor);
joshualitt33a5fce2015-11-18 13:28:51 -0800213 if (!gp) {
214 SkDebugf("Couldn't create GrGeometryProcessor\n");
215 return;
216 }
217
bsalomona71b8982016-06-30 12:13:52 -0700218 int patchCnt = fPatches.count();
msarett10e3d9b2016-08-18 15:46:03 -0700219 int numRects = 0;
220 for (int i = 0; i < patchCnt; i++) {
msarett0764efe2016-09-02 11:24:30 -0700221 numRects += fPatches[i].fIter->numRectsToDraw();
msarett10e3d9b2016-08-18 15:46:03 -0700222 }
joshualitt33a5fce2015-11-18 13:28:51 -0800223
Brian Salomon0db1b532017-07-12 15:21:43 -0400224 if (!numRects) {
225 return;
226 }
227
Brian Osmanc3064e72018-11-15 08:59:22 -0500228 const size_t kVertexStride = gp->vertexStride();
Robert Phillipse94cdd22019-11-04 14:15:58 -0500229
230 QuadHelper helper(target, kVertexStride, numRects);
231
Brian Osmanc3064e72018-11-15 08:59:22 -0500232 GrVertexWriter vertices{helper.vertices()};
Brian Salomon12d22642019-01-29 14:38:50 -0500233 if (!vertices.fPtr) {
joshualitt33a5fce2015-11-18 13:28:51 -0800234 SkDebugf("Could not allocate vertices\n");
235 return;
236 }
237
bsalomona71b8982016-06-30 12:13:52 -0700238 for (int i = 0; i < patchCnt; i++) {
msarett7fc08582016-08-18 14:29:22 -0700239 const Patch& patch = fPatches[i];
Brian Osmanc3064e72018-11-15 08:59:22 -0500240
Brian Osman0b537032018-12-26 12:16:44 -0500241 GrVertexColor patchColor(patch.fColor, fWideColor);
msarett10e3d9b2016-08-18 15:46:03 -0700242
243 // Apply the view matrix here if it is scale-translate. Otherwise, we need to
244 // wait until we've created the dst rects.
245 bool isScaleTranslate = patch.fViewMatrix.isScaleTranslate();
246 if (isScaleTranslate) {
247 patch.fIter->mapDstScaleTranslate(patch.fViewMatrix);
248 }
joshualitt33a5fce2015-11-18 13:28:51 -0800249
Brian Salomon2a943df2018-05-04 13:43:19 -0400250 SkIRect srcR;
251 SkRect dstR;
Brian Osmanc3064e72018-11-15 08:59:22 -0500252 SkPoint* patchPositions = reinterpret_cast<SkPoint*>(vertices.fPtr);
Brian Salomon2a943df2018-05-04 13:43:19 -0400253 Sk4f scales(1.f / fProxy->width(), 1.f / fProxy->height(),
254 1.f / fProxy->width(), 1.f / fProxy->height());
255 static const Sk4f kDomainOffsets(0.5f, 0.5f, -0.5f, -0.5f);
Brian Osmanc3064e72018-11-15 08:59:22 -0500256 static const Sk4f kFlipOffsets(0.f, 1.f, 0.f, 1.f);
Brian Salomon2a943df2018-05-04 13:43:19 -0400257 static const Sk4f kFlipMuls(1.f, -1.f, 1.f, -1.f);
msarett10e3d9b2016-08-18 15:46:03 -0700258 while (patch.fIter->next(&srcR, &dstR)) {
Brian Salomon2a943df2018-05-04 13:43:19 -0400259 Sk4f coords(SkIntToScalar(srcR.fLeft), SkIntToScalar(srcR.fTop),
260 SkIntToScalar(srcR.fRight), SkIntToScalar(srcR.fBottom));
261 Sk4f domain = coords + kDomainOffsets;
262 coords *= scales;
263 domain *= scales;
264 if (fProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
265 coords = kFlipMuls * coords + kFlipOffsets;
266 domain = SkNx_shuffle<0, 3, 2, 1>(kFlipMuls * domain + kFlipOffsets);
267 }
Brian Osman4486d982018-11-15 15:56:04 -0500268 SkRect texDomain;
269 SkRect texCoords;
270 domain.store(&texDomain);
271 coords.store(&texCoords);
joshualitt33a5fce2015-11-18 13:28:51 -0800272
Brian Osman0dd43022018-11-16 15:53:26 -0500273 vertices.writeQuad(GrVertexWriter::TriStripFromRect(dstR),
274 GrVertexWriter::TriStripFromRect(texCoords),
Brian Osmanc3064e72018-11-15 08:59:22 -0500275 texDomain,
276 patchColor);
joshualitt33a5fce2015-11-18 13:28:51 -0800277 }
msarett10e3d9b2016-08-18 15:46:03 -0700278
279 // If we didn't handle it above, apply the matrix here.
280 if (!isScaleTranslate) {
Robert Phillipsee08d522019-10-28 16:34:44 -0400281 SkMatrixPriv::MapPointsWithStride(
282 patch.fViewMatrix, patchPositions, kVertexStride,
283 GrResourceProvider::NumVertsPerNonAAQuad() * patch.fIter->numRectsToDraw());
msarett10e3d9b2016-08-18 15:46:03 -0700284 }
joshualitt33a5fce2015-11-18 13:28:51 -0800285 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700286 auto fixedDynamicState = target->makeFixedDynamicState(1);
287 fixedDynamicState->fPrimitiveProcessorTextures[0] = fProxy.get();
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500288 helper.recordDraw(target, gp, fixedDynamicState);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700289 }
290
291 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
292 fHelper.executeDrawsAndUploads(this, flushState, chainBounds);
joshualitt33a5fce2015-11-18 13:28:51 -0800293 }
294
Brian Salomon7eae3e02018-08-07 14:02:38 +0000295 CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Brian Salomonfc527d22016-12-14 21:07:01 -0500296 NonAALatticeOp* that = t->cast<NonAALatticeOp>();
Brian Salomon2a943df2018-05-04 13:43:19 -0400297 if (fProxy != that->fProxy) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000298 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400299 }
300 if (fFilter != that->fFilter) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000301 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400302 }
303 if (GrColorSpaceXform::Equals(fColorSpaceXform.get(), that->fColorSpaceXform.get())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000304 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400305 }
Brian Salomon815486c2017-07-11 08:52:13 -0400306 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000307 return CombineResult::kCannotCombine;
joshualitt33a5fce2015-11-18 13:28:51 -0800308 }
309
msarett10e3d9b2016-08-18 15:46:03 -0700310 fPatches.move_back_n(that->fPatches.count(), that->fPatches.begin());
Brian Osman0b537032018-12-26 12:16:44 -0500311 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000312 return CombineResult::kMerged;
joshualitt33a5fce2015-11-18 13:28:51 -0800313 }
314
bsalomona71b8982016-06-30 12:13:52 -0700315 struct Patch {
316 SkMatrix fViewMatrix;
msarett10e3d9b2016-08-18 15:46:03 -0700317 std::unique_ptr<SkLatticeIter> fIter;
bsalomona71b8982016-06-30 12:13:52 -0700318 SkRect fDst;
Brian Osmancf860852018-10-31 14:04:39 -0400319 SkPMColor4f fColor;
bsalomona71b8982016-06-30 12:13:52 -0700320 };
321
Brian Salomon815486c2017-07-11 08:52:13 -0400322 Helper fHelper;
323 SkSTArray<1, Patch, true> fPatches;
Brian Salomon2a943df2018-05-04 13:43:19 -0400324 sk_sp<GrTextureProxy> fProxy;
Greg Danielc594e622019-10-15 14:01:49 -0400325 GrColorType fSrcColorType;
Brian Salomon2a943df2018-05-04 13:43:19 -0400326 sk_sp<GrColorSpaceXform> fColorSpaceXform;
327 GrSamplerState::Filter fFilter;
Brian Osman0b537032018-12-26 12:16:44 -0500328 bool fWideColor;
joshualitt33a5fce2015-11-18 13:28:51 -0800329
Brian Salomon815486c2017-07-11 08:52:13 -0400330 typedef GrMeshDrawOp INHERITED;
joshualitt33a5fce2015-11-18 13:28:51 -0800331};
332
Brian Salomon815486c2017-07-11 08:52:13 -0400333} // anonymous namespace
334
Brian Salomonfc527d22016-12-14 21:07:01 -0500335namespace GrLatticeOp {
Robert Phillipsb97da532019-02-12 15:24:12 -0500336std::unique_ptr<GrDrawOp> MakeNonAA(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400337 GrPaint&& paint,
338 const SkMatrix& viewMatrix,
Brian Salomon2a943df2018-05-04 13:43:19 -0400339 sk_sp<GrTextureProxy> proxy,
Greg Danielc594e622019-10-15 14:01:49 -0400340 GrColorType srcColorType,
Brian Salomon2a943df2018-05-04 13:43:19 -0400341 sk_sp<GrColorSpaceXform> colorSpaceXform,
342 GrSamplerState::Filter filter,
Robert Phillips7c525e62018-06-12 10:11:12 -0400343 std::unique_ptr<SkLatticeIter> iter,
344 const SkRect& dst) {
345 return NonAALatticeOp::Make(context, std::move(paint), viewMatrix, std::move(proxy),
Greg Danielc594e622019-10-15 14:01:49 -0400346 srcColorType, std::move(colorSpaceXform), filter, std::move(iter),
347 dst);
joshualitt33a5fce2015-11-18 13:28:51 -0800348}
349};
Brian Salomon815486c2017-07-11 08:52:13 -0400350
351#if GR_TEST_UTILS
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500352#include "src/gpu/GrProxyProvider.h"
353#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon815486c2017-07-11 08:52:13 -0400354
355/** Randomly divides subset into count divs. */
356static void init_random_divs(int divs[], int count, int subsetStart, int subsetStop,
357 SkRandom* random) {
358 // Rules for lattice divs: Must be strictly increasing and in the range
359 // [subsetStart, subsetStop).
360 // Not terribly efficient alg for generating random divs:
361 // 1) Start with minimum legal pixels between each div.
362 // 2) Randomly assign the remaining pixels of the subset to divs.
363 // 3) Convert from pixel counts to div offsets.
364
365 // 1) Initially each divs[i] represents the number of pixels between
366 // div i-1 and i. The initial div is allowed to be at subsetStart. There
367 // must be one pixel spacing between subsequent divs.
368 divs[0] = 0;
369 for (int i = 1; i < count; ++i) {
370 divs[i] = 1;
371 }
372 // 2) Assign the remaining subset pixels to fall
373 int subsetLength = subsetStop - subsetStart;
374 for (int i = 0; i < subsetLength - count; ++i) {
375 // +1 because count divs means count+1 intervals.
376 int entry = random->nextULessThan(count + 1);
377 // We don't have an entry to to store the count after the last div
378 if (entry < count) {
379 divs[entry]++;
380 }
381 }
382 // 3) Now convert the counts between divs to pixel indices, incorporating the subset's offset.
383 int offset = subsetStart;
384 for (int i = 0; i < count; ++i) {
385 divs[i] += offset;
386 offset = divs[i];
387 }
388}
389
390GR_DRAW_OP_TEST_DEFINE(NonAALatticeOp) {
Brian Salomon815486c2017-07-11 08:52:13 -0400391 SkCanvas::Lattice lattice;
Brian Salomon18df7632017-07-11 14:32:18 -0400392 // We loop because our random lattice code can produce an invalid lattice in the case where
393 // there is a single div separator in both x and y and both are aligned with the left and top
394 // edge of the image subset, respectively.
395 std::unique_ptr<int[]> xdivs;
396 std::unique_ptr<int[]> ydivs;
Stan Ilievca8c0952017-12-11 13:01:58 -0500397 std::unique_ptr<SkCanvas::Lattice::RectType[]> flags;
398 std::unique_ptr<SkColor[]> colors;
Brian Salomon18df7632017-07-11 14:32:18 -0400399 SkIRect subset;
Brian Salomon2a943df2018-05-04 13:43:19 -0400400 GrSurfaceDesc desc;
401 desc.fConfig = kRGBA_8888_GrPixelConfig;
402 desc.fWidth = random->nextRangeU(1, 1000);
403 desc.fHeight = random->nextRangeU(1, 1000);
Robert Phillips0a15cc62019-07-30 12:49:10 -0400404 GrSurfaceOrigin origin = random->nextBool() ? kTopLeft_GrSurfaceOrigin
405 : kBottomLeft_GrSurfaceOrigin;
Greg Daniel4065d452018-11-16 15:43:41 -0500406 const GrBackendFormat format =
Robert Phillips0a15cc62019-07-30 12:49:10 -0400407 context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
408 GrRenderable::kNo);
Brian Salomonbeb7f522019-08-30 16:19:42 -0400409 auto proxy = context->priv().proxyProvider()->createProxy(format,
410 desc,
411 GrRenderable::kNo,
412 1,
413 origin,
414 GrMipMapped::kNo,
415 SkBackingFit::kExact,
416 SkBudgeted::kYes,
417 GrProtected::kNo);
Brian Salomon2a943df2018-05-04 13:43:19 -0400418
Brian Salomon18df7632017-07-11 14:32:18 -0400419 do {
Brian Salomon18df7632017-07-11 14:32:18 -0400420 if (random->nextBool()) {
Brian Salomon2a943df2018-05-04 13:43:19 -0400421 subset.fLeft = random->nextULessThan(desc.fWidth);
422 subset.fRight = random->nextRangeU(subset.fLeft + 1, desc.fWidth);
423 subset.fTop = random->nextULessThan(desc.fHeight);
424 subset.fBottom = random->nextRangeU(subset.fTop + 1, desc.fHeight);
Brian Salomon18df7632017-07-11 14:32:18 -0400425 } else {
Brian Salomon2a943df2018-05-04 13:43:19 -0400426 subset.setXYWH(0, 0, desc.fWidth, desc.fHeight);
Brian Salomon815486c2017-07-11 08:52:13 -0400427 }
Brian Salomon2a943df2018-05-04 13:43:19 -0400428 // SkCanvas::Lattice allows bounds to be null. However, SkCanvas creates a temp Lattice with
429 // a non-null bounds before creating a SkLatticeIter since SkLatticeIter requires a bounds.
Brian Salomon18df7632017-07-11 14:32:18 -0400430 lattice.fBounds = &subset;
431 lattice.fXCount = random->nextRangeU(1, subset.width());
432 lattice.fYCount = random->nextRangeU(1, subset.height());
433 xdivs.reset(new int[lattice.fXCount]);
434 ydivs.reset(new int[lattice.fYCount]);
435 init_random_divs(xdivs.get(), lattice.fXCount, subset.fLeft, subset.fRight, random);
436 init_random_divs(ydivs.get(), lattice.fYCount, subset.fTop, subset.fBottom, random);
437 lattice.fXDivs = xdivs.get();
438 lattice.fYDivs = ydivs.get();
439 bool hasFlags = random->nextBool();
440 if (hasFlags) {
441 int n = (lattice.fXCount + 1) * (lattice.fYCount + 1);
Stan Ilievca8c0952017-12-11 13:01:58 -0500442 flags.reset(new SkCanvas::Lattice::RectType[n]);
443 colors.reset(new SkColor[n]);
Brian Salomon18df7632017-07-11 14:32:18 -0400444 for (int i = 0; i < n; ++i) {
Stan Ilievca8c0952017-12-11 13:01:58 -0500445 flags[i] = random->nextBool() ? SkCanvas::Lattice::kTransparent
446 : SkCanvas::Lattice::kDefault;
Brian Salomon18df7632017-07-11 14:32:18 -0400447 }
Stan Ilievca8c0952017-12-11 13:01:58 -0500448 lattice.fRectTypes = flags.get();
449 lattice.fColors = colors.get();
Brian Salomon18df7632017-07-11 14:32:18 -0400450 } else {
Stan Ilievca8c0952017-12-11 13:01:58 -0500451 lattice.fRectTypes = nullptr;
452 lattice.fColors = nullptr;
Brian Salomon18df7632017-07-11 14:32:18 -0400453 }
Brian Salomon2a943df2018-05-04 13:43:19 -0400454 } while (!SkLatticeIter::Valid(desc.fWidth, desc.fHeight, lattice));
Brian Salomon815486c2017-07-11 08:52:13 -0400455 SkRect dst;
456 dst.fLeft = random->nextRangeScalar(-2000.5f, 1000.f);
457 dst.fTop = random->nextRangeScalar(-2000.5f, 1000.f);
458 dst.fRight = dst.fLeft + random->nextRangeScalar(0.5f, 1000.f);
459 dst.fBottom = dst.fTop + random->nextRangeScalar(0.5f, 1000.f);
460 std::unique_ptr<SkLatticeIter> iter(new SkLatticeIter(lattice, dst));
461 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
Brian Salomon2a943df2018-05-04 13:43:19 -0400462 auto csxf = GrTest::TestColorXform(random);
463 GrSamplerState::Filter filter =
464 random->nextBool() ? GrSamplerState::Filter::kNearest : GrSamplerState::Filter::kBilerp;
Robert Phillips7c525e62018-06-12 10:11:12 -0400465 return NonAALatticeOp::Make(context, std::move(paint), viewMatrix, std::move(proxy),
Greg Danielc594e622019-10-15 14:01:49 -0400466 GrColorType::kRGBA_8888, std::move(csxf), filter, std::move(iter),
467 dst);
Brian Salomon815486c2017-07-11 08:52:13 -0400468}
469
470#endif