blob: 41feaa34ab55a00fd599c0e9fedd08d015ff4dce [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
Robert Phillips769b4882021-09-07 16:48:46 -04008#include "src/gpu/ops/LatticeOp.h"
Brian Salomon48959462021-08-11 13:01:06 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkBitmap.h"
11#include "include/core/SkRect.h"
12#include "src/core/SkLatticeIter.h"
13#include "src/core/SkMatrixPriv.h"
Brian Salomon48959462021-08-11 13:01:06 -040014#include "src/gpu/GrGeometryProcessor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrGpu.h"
16#include "src/gpu/GrOpFlushState.h"
Robert Phillipse37f1c42020-03-09 16:10:18 -040017#include "src/gpu/GrProgramInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrResourceProvider.h"
19#include "src/gpu/GrResourceProviderPriv.h"
20#include "src/gpu/GrVertexWriter.h"
21#include "src/gpu/SkGr.h"
22#include "src/gpu/glsl/GrGLSLColorSpaceXformHelper.h"
Robert Phillips03e4c952019-11-26 16:20:22 -050023#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/glsl/GrGLSLVarying.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/ops/GrMeshDrawOp.h"
26#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
joshualitt33a5fce2015-11-18 13:28:51 -080027
Brian Salomon815486c2017-07-11 08:52:13 -040028namespace {
29
Brian Salomon2a943df2018-05-04 13:43:19 -040030class LatticeGP : public GrGeometryProcessor {
31public:
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050032 static GrGeometryProcessor* Make(SkArenaAlloc* arena,
Greg Danieled96bca2019-12-05 15:05:54 -050033 const GrSurfaceProxyView& view,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050034 sk_sp<GrColorSpaceXform> csxf,
35 GrSamplerState::Filter filter,
36 bool wideColor) {
Mike Kleinf1241082020-12-14 15:59:09 -060037 return arena->make([&](void* ptr) {
38 return new (ptr) LatticeGP(view, std::move(csxf), filter, wideColor);
39 });
Brian Salomon2a943df2018-05-04 13:43:19 -040040 }
41
42 const char* name() const override { return "LatticeGP"; }
43
Brian Salomon13b28732021-08-06 15:33:58 -040044 void addToKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
Brian Salomon2a943df2018-05-04 13:43:19 -040045 b->add32(GrColorSpaceXform::XformKey(fColorSpaceXform.get()));
46 }
47
Brian Salomonf95940b2021-08-09 15:56:24 -040048 std::unique_ptr<ProgramImpl> makeProgramImpl(const GrShaderCaps&) const override {
Brian Salomonbab2d112021-08-11 09:59:56 -040049 class Impl : public ProgramImpl {
Brian Salomon2a943df2018-05-04 13:43:19 -040050 public:
Brian Osman609f1592020-07-01 15:14:39 -040051 void setData(const GrGLSLProgramDataManager& pdman,
Brian Salomon5a328282021-04-14 10:32:25 -040052 const GrShaderCaps&,
Robert Phillips787fd9d2021-03-22 14:48:09 -040053 const GrGeometryProcessor& geomProc) override {
54 const auto& latticeGP = geomProc.cast<LatticeGP>();
Brian Osmanc891b102018-06-14 14:50:17 -040055 fColorSpaceXformHelper.setData(pdman, latticeGP.fColorSpaceXform.get());
Brian Salomon2a943df2018-05-04 13:43:19 -040056 }
57
58 private:
59 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
60 using Interpolation = GrGLSLVaryingHandler::Interpolation;
Robert Phillips787fd9d2021-03-22 14:48:09 -040061 const auto& latticeGP = args.fGeomProc.cast<LatticeGP>();
Brian Salomon2a943df2018-05-04 13:43:19 -040062 fColorSpaceXformHelper.emitCode(args.fUniformHandler,
63 latticeGP.fColorSpaceXform.get());
64
65 args.fVaryingHandler->emitAttributes(latticeGP);
Brian Salomon5a328282021-04-14 10:32:25 -040066 WriteOutputPosition(args.fVertBuilder, gpArgs, latticeGP.fInPosition.name());
Michael Ludwig553db622020-06-19 10:47:30 -040067 gpArgs->fLocalCoordVar = latticeGP.fInTextureCoords.asShaderVar();
68
Brian Salomon2a943df2018-05-04 13:43:19 -040069 args.fFragBuilder->codeAppend("float2 textureCoords;");
Brian Salomon48959462021-08-11 13:01:06 -040070 args.fVaryingHandler->addPassThroughAttribute(
71 latticeGP.fInTextureCoords.asShaderVar(),
72 "textureCoords");
Brian Salomon2a943df2018-05-04 13:43:19 -040073 args.fFragBuilder->codeAppend("float4 textureDomain;");
74 args.fVaryingHandler->addPassThroughAttribute(
Brian Salomon48959462021-08-11 13:01:06 -040075 latticeGP.fInTextureDomain.asShaderVar(),
76 "textureDomain",
77 Interpolation::kCanBeFlat);
John Stiles4d7ac492021-03-09 20:16:43 -050078 args.fFragBuilder->codeAppendf("half4 %s;", args.fOutputColor);
Brian Salomon48959462021-08-11 13:01:06 -040079 args.fVaryingHandler->addPassThroughAttribute(latticeGP.fInColor.asShaderVar(),
Brian Osmanf04fb3c2018-11-12 15:34:00 -050080 args.fOutputColor,
Brian Salomon2a943df2018-05-04 13:43:19 -040081 Interpolation::kCanBeFlat);
82 args.fFragBuilder->codeAppendf("%s = ", args.fOutputColor);
Brian Salomon87e9ddb2019-12-19 14:50:22 -050083 args.fFragBuilder->appendTextureLookupAndBlend(
Brian Salomon2a943df2018-05-04 13:43:19 -040084 args.fOutputColor,
Brian Salomon87e9ddb2019-12-19 14:50:22 -050085 SkBlendMode::kModulate,
Brian Salomon2a943df2018-05-04 13:43:19 -040086 args.fTexSamplers[0],
87 "clamp(textureCoords, textureDomain.xy, textureDomain.zw)",
Brian Salomon2a943df2018-05-04 13:43:19 -040088 &fColorSpaceXformHelper);
89 args.fFragBuilder->codeAppend(";");
John Stiles4d7ac492021-03-09 20:16:43 -050090 args.fFragBuilder->codeAppendf("const half4 %s = half4(1);", args.fOutputCoverage);
Brian Salomon2a943df2018-05-04 13:43:19 -040091 }
Brian Salomonbab2d112021-08-11 09:59:56 -040092
Brian Salomon2a943df2018-05-04 13:43:19 -040093 GrGLSLColorSpaceXformHelper fColorSpaceXformHelper;
94 };
Brian Salomonbab2d112021-08-11 09:59:56 -040095
96 return std::make_unique<Impl>();
Brian Salomon2a943df2018-05-04 13:43:19 -040097 }
98
99private:
Greg Danieled96bca2019-12-05 15:05:54 -0500100 LatticeGP(const GrSurfaceProxyView& view, sk_sp<GrColorSpaceXform> csxf,
Brian Osman0b537032018-12-26 12:16:44 -0500101 GrSamplerState::Filter filter, bool wideColor)
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500102 : INHERITED(kLatticeGP_ClassID)
103 , fColorSpaceXform(std::move(csxf)) {
Greg Daniel7a82edf2018-12-04 10:54:34 -0500104
Robert Phillips323471e2019-11-11 11:33:37 -0500105 fSampler.reset(GrSamplerState(GrSamplerState::WrapMode::kClamp, filter),
Greg Danieled96bca2019-12-05 15:05:54 -0500106 view.proxy()->backendFormat(), view.swizzle());
Brian Salomonf7dcd762018-07-30 14:48:15 -0400107 this->setTextureSamplerCnt(1);
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500108 fInPosition = {"position", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
109 fInTextureCoords = {"textureCoords", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
110 fInTextureDomain = {"textureDomain", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
Brian Osman0b537032018-12-26 12:16:44 -0500111 fInColor = MakeColorAttribute("color", wideColor);
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500112 this->setVertexAttributes(&fInPosition, 4);
Brian Salomon92be2f72018-06-19 14:33:47 -0400113 }
114
Brian Salomonf7dcd762018-07-30 14:48:15 -0400115 const TextureSampler& onTextureSampler(int) const override { return fSampler; }
116
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500117 Attribute fInPosition;
118 Attribute fInTextureCoords;
119 Attribute fInTextureDomain;
120 Attribute fInColor;
Brian Salomon92be2f72018-06-19 14:33:47 -0400121
Brian Salomon2a943df2018-05-04 13:43:19 -0400122 sk_sp<GrColorSpaceXform> fColorSpaceXform;
123 TextureSampler fSampler;
124
John Stiles7571f9e2020-09-02 22:42:33 -0400125 using INHERITED = GrGeometryProcessor;
Brian Salomon2a943df2018-05-04 13:43:19 -0400126};
127
Brian Salomon815486c2017-07-11 08:52:13 -0400128class NonAALatticeOp final : public GrMeshDrawOp {
129private:
130 using Helper = GrSimpleMeshDrawOpHelper;
131
joshualitt33a5fce2015-11-18 13:28:51 -0800132public:
Brian Salomon25a88092016-12-01 09:36:50 -0500133 DEFINE_OP_CLASS_ID
joshualitt33a5fce2015-11-18 13:28:51 -0800134
Herb Derbyc76d4092020-10-07 16:46:15 -0400135 static GrOp::Owner Make(GrRecordingContext* context,
136 GrPaint&& paint,
137 const SkMatrix& viewMatrix,
138 GrSurfaceProxyView view,
139 SkAlphaType alphaType,
140 sk_sp<GrColorSpaceXform> colorSpaceXForm,
141 GrSamplerState::Filter filter,
142 std::unique_ptr<SkLatticeIter> iter,
143 const SkRect& dst) {
Greg Danieled96bca2019-12-05 15:05:54 -0500144 SkASSERT(view.proxy());
Robert Phillips7c525e62018-06-12 10:11:12 -0400145 return Helper::FactoryHelper<NonAALatticeOp>(context, std::move(paint), viewMatrix,
Greg Daniel82c6b102020-01-21 10:33:22 -0500146 std::move(view), alphaType,
Brian Salomon2a943df2018-05-04 13:43:19 -0400147 std::move(colorSpaceXForm), filter,
148 std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -0400149 }
150
Herb Derbyc76d4092020-10-07 16:46:15 -0400151 NonAALatticeOp(GrProcessorSet* processorSet, const SkPMColor4f& color,
Greg Danieled96bca2019-12-05 15:05:54 -0500152 const SkMatrix& viewMatrix, GrSurfaceProxyView view,
Greg Daniel82c6b102020-01-21 10:33:22 -0500153 SkAlphaType alphaType, sk_sp<GrColorSpaceXform> colorSpaceXform,
Greg Danielc594e622019-10-15 14:01:49 -0400154 GrSamplerState::Filter filter, std::unique_ptr<SkLatticeIter> iter,
155 const SkRect& dst)
Brian Salomon2a943df2018-05-04 13:43:19 -0400156 : INHERITED(ClassID())
Herb Derbyc76d4092020-10-07 16:46:15 -0400157 , fHelper(processorSet, GrAAType::kNone)
Greg Danieled96bca2019-12-05 15:05:54 -0500158 , fView(std::move(view))
Greg Daniel82c6b102020-01-21 10:33:22 -0500159 , fAlphaType(alphaType)
Brian Salomon2a943df2018-05-04 13:43:19 -0400160 , fColorSpaceXform(std::move(colorSpaceXform))
161 , fFilter(filter) {
bsalomona71b8982016-06-30 12:13:52 -0700162 Patch& patch = fPatches.push_back();
163 patch.fViewMatrix = viewMatrix;
164 patch.fColor = color;
msarett10e3d9b2016-08-18 15:46:03 -0700165 patch.fIter = std::move(iter);
bsalomona71b8982016-06-30 12:13:52 -0700166 patch.fDst = dst;
joshualitt33a5fce2015-11-18 13:28:51 -0800167
joshualitt33a5fce2015-11-18 13:28:51 -0800168 // setup bounds
Greg Daniel5faf4742019-10-01 15:14:44 -0400169 this->setTransformedBounds(patch.fDst, viewMatrix, HasAABloat::kNo, IsHairline::kNo);
joshualitt33a5fce2015-11-18 13:28:51 -0800170 }
171
Brian Salomonfc527d22016-12-14 21:07:01 -0500172 const char* name() const override { return "NonAALatticeOp"; }
robertphillips783a4da2015-11-19 14:00:02 -0800173
Robert Phillips294723d2021-06-17 09:23:58 -0400174 void visitProxies(const GrVisitProxyFunc& func) const override {
Brian Salomone69b9ef2020-07-22 11:18:06 -0400175 func(fView.proxy(), GrMipmapped::kNo);
Robert Phillipse37f1c42020-03-09 16:10:18 -0400176 if (fProgramInfo) {
Chris Daltonbe457422020-03-16 18:05:03 -0600177 fProgramInfo->visitFPProxies(func);
Robert Phillipse37f1c42020-03-09 16:10:18 -0400178 } else {
Robert Phillipse37f1c42020-03-09 16:10:18 -0400179 fHelper.visitProxies(func);
180 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400181 }
182
Brian Salomon815486c2017-07-11 08:52:13 -0400183 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
184
Chris Dalton57ab06c2021-04-22 12:57:28 -0600185 GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip,
186 GrClampType clampType) override {
Greg Daniel82c6b102020-01-21 10:33:22 -0500187 auto opaque = fPatches[0].fColor.isOpaque() && fAlphaType == kOpaque_SkAlphaType
Brian Salomon2a943df2018-05-04 13:43:19 -0400188 ? GrProcessorAnalysisColor::Opaque::kYes
189 : GrProcessorAnalysisColor::Opaque::kNo;
190 auto analysisColor = GrProcessorAnalysisColor(opaque);
Chris Dalton57ab06c2021-04-22 12:57:28 -0600191 auto result = fHelper.finalizeProcessors(caps, clip, clampType,
192 GrProcessorAnalysisCoverage::kNone,
193 &analysisColor);
Brian Salomon2a943df2018-05-04 13:43:19 -0400194 analysisColor.isConstant(&fPatches[0].fColor);
Brian Osman2715bf52019-12-06 14:38:47 -0500195 fWideColor = !fPatches[0].fColor.fitsInBytes();
Brian Salomon2a943df2018-05-04 13:43:19 -0400196 return result;
Brian Salomon815486c2017-07-11 08:52:13 -0400197 }
198
Brian Salomon92aee3d2016-12-21 09:20:25 -0500199private:
Robert Phillips2669a7b2020-03-12 12:07:19 -0400200 GrProgramInfo* programInfo() override { return fProgramInfo; }
201
Robert Phillips4133dc42020-03-11 15:55:55 -0400202 void onCreateProgramInfo(const GrCaps* caps,
203 SkArenaAlloc* arena,
Adlai Hollere2296f72020-11-19 13:41:26 -0500204 const GrSurfaceProxyView& writeView,
Chris Dalton6aaf00f2021-07-13 13:26:39 -0600205 bool usesMSAASurface,
Robert Phillips4133dc42020-03-11 15:55:55 -0400206 GrAppliedClip&& appliedClip,
John Stiles52cb1d02021-06-02 11:58:05 -0400207 const GrDstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500208 GrXferBarrierFlags renderPassXferBarriers,
209 GrLoadOp colorLoadOp) override {
Robert Phillipse37f1c42020-03-09 16:10:18 -0400210
211 auto gp = LatticeGP::Make(arena, fView, fColorSpaceXform, fFilter, fWideColor);
joshualitt33a5fce2015-11-18 13:28:51 -0800212 if (!gp) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400213 return;
Robert Phillipse37f1c42020-03-09 16:10:18 -0400214 }
215
Brian Salomon8afde5f2020-04-01 16:22:00 -0400216 fProgramInfo = GrSimpleMeshDrawOpHelper::CreateProgramInfo(caps, arena, writeView,
Chris Dalton2a26c502021-08-26 10:05:11 -0600217 usesMSAASurface,
Robert Phillips4133dc42020-03-11 15:55:55 -0400218 std::move(appliedClip),
219 dstProxyView, gp,
220 fHelper.detachProcessorSet(),
221 GrPrimitiveType::kTriangles,
Greg Danield358cbe2020-09-11 09:33:54 -0400222 renderPassXferBarriers,
Greg Daniel42dbca52020-11-20 10:22:43 -0500223 colorLoadOp,
Robert Phillips4133dc42020-03-11 15:55:55 -0400224 fHelper.pipelineFlags(),
Chris Dalton765ed362020-03-16 17:34:44 -0600225 &GrUserStencilSettings::kUnused);
Robert Phillipse37f1c42020-03-09 16:10:18 -0400226 }
227
Robert Phillips71143952021-06-17 14:55:07 -0400228 void onPrepareDraws(GrMeshDrawTarget* target) override {
Robert Phillipse37f1c42020-03-09 16:10:18 -0400229 if (!fProgramInfo) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400230 this->createProgramInfo(target);
Robert Phillipse37f1c42020-03-09 16:10:18 -0400231 if (!fProgramInfo) {
232 return;
233 }
joshualitt33a5fce2015-11-18 13:28:51 -0800234 }
235
bsalomona71b8982016-06-30 12:13:52 -0700236 int patchCnt = fPatches.count();
msarett10e3d9b2016-08-18 15:46:03 -0700237 int numRects = 0;
238 for (int i = 0; i < patchCnt; i++) {
msarett0764efe2016-09-02 11:24:30 -0700239 numRects += fPatches[i].fIter->numRectsToDraw();
msarett10e3d9b2016-08-18 15:46:03 -0700240 }
joshualitt33a5fce2015-11-18 13:28:51 -0800241
Brian Salomon0db1b532017-07-12 15:21:43 -0400242 if (!numRects) {
243 return;
244 }
245
Robert Phillips787fd9d2021-03-22 14:48:09 -0400246 const size_t kVertexStride = fProgramInfo->geomProc().vertexStride();
Robert Phillipse94cdd22019-11-04 14:15:58 -0500247
248 QuadHelper helper(target, kVertexStride, numRects);
249
Brian Osmanc3064e72018-11-15 08:59:22 -0500250 GrVertexWriter vertices{helper.vertices()};
Brian Salomon12d22642019-01-29 14:38:50 -0500251 if (!vertices.fPtr) {
joshualitt33a5fce2015-11-18 13:28:51 -0800252 SkDebugf("Could not allocate vertices\n");
253 return;
254 }
255
bsalomona71b8982016-06-30 12:13:52 -0700256 for (int i = 0; i < patchCnt; i++) {
msarett7fc08582016-08-18 14:29:22 -0700257 const Patch& patch = fPatches[i];
Brian Osmanc3064e72018-11-15 08:59:22 -0500258
Brian Osman0b537032018-12-26 12:16:44 -0500259 GrVertexColor patchColor(patch.fColor, fWideColor);
msarett10e3d9b2016-08-18 15:46:03 -0700260
261 // Apply the view matrix here if it is scale-translate. Otherwise, we need to
262 // wait until we've created the dst rects.
263 bool isScaleTranslate = patch.fViewMatrix.isScaleTranslate();
264 if (isScaleTranslate) {
265 patch.fIter->mapDstScaleTranslate(patch.fViewMatrix);
266 }
joshualitt33a5fce2015-11-18 13:28:51 -0800267
Brian Salomon2a943df2018-05-04 13:43:19 -0400268 SkIRect srcR;
269 SkRect dstR;
Greg Danieled96bca2019-12-05 15:05:54 -0500270 Sk4f scales(1.f / fView.proxy()->width(), 1.f / fView.proxy()->height(),
271 1.f / fView.proxy()->width(), 1.f / fView.proxy()->height());
Brian Salomon2a943df2018-05-04 13:43:19 -0400272 static const Sk4f kDomainOffsets(0.5f, 0.5f, -0.5f, -0.5f);
Brian Osmanc3064e72018-11-15 08:59:22 -0500273 static const Sk4f kFlipOffsets(0.f, 1.f, 0.f, 1.f);
Brian Salomon2a943df2018-05-04 13:43:19 -0400274 static const Sk4f kFlipMuls(1.f, -1.f, 1.f, -1.f);
msarett10e3d9b2016-08-18 15:46:03 -0700275 while (patch.fIter->next(&srcR, &dstR)) {
Brian Salomon2a943df2018-05-04 13:43:19 -0400276 Sk4f coords(SkIntToScalar(srcR.fLeft), SkIntToScalar(srcR.fTop),
277 SkIntToScalar(srcR.fRight), SkIntToScalar(srcR.fBottom));
278 Sk4f domain = coords + kDomainOffsets;
279 coords *= scales;
280 domain *= scales;
Greg Danieled96bca2019-12-05 15:05:54 -0500281 if (fView.origin() == kBottomLeft_GrSurfaceOrigin) {
Brian Salomon2a943df2018-05-04 13:43:19 -0400282 coords = kFlipMuls * coords + kFlipOffsets;
283 domain = SkNx_shuffle<0, 3, 2, 1>(kFlipMuls * domain + kFlipOffsets);
284 }
Brian Osman4486d982018-11-15 15:56:04 -0500285 SkRect texDomain;
286 SkRect texCoords;
287 domain.store(&texDomain);
288 coords.store(&texCoords);
joshualitt33a5fce2015-11-18 13:28:51 -0800289
Greg Danield49b1282021-02-02 17:11:37 -0500290 if (isScaleTranslate) {
291 vertices.writeQuad(GrVertexWriter::TriStripFromRect(dstR),
292 GrVertexWriter::TriStripFromRect(texCoords),
293 texDomain,
294 patchColor);
295 } else {
296 SkPoint mappedPts[4];
297 patch.fViewMatrix.mapRectToQuad(mappedPts, dstR);
298 // In the above if statement, writeQuad writes the corners as:
299 // left-top, left-bottom, right-top, right-bottom.
300 // However, mapRectToQuad returns them in the order:
301 // left-top, right-top, right-bottom, left-bottom
302 // Thus we write out the vertices to match the writeQuad path.
303 vertices.write(mappedPts[0],
304 SkPoint::Make(texCoords.fLeft, texCoords.fTop),
Brian Osmanc3064e72018-11-15 08:59:22 -0500305 texDomain,
306 patchColor);
Greg Danield49b1282021-02-02 17:11:37 -0500307 vertices.write(mappedPts[3],
308 SkPoint::Make(texCoords.fLeft, texCoords.fBottom),
309 texDomain,
310 patchColor);
311 vertices.write(mappedPts[1],
312 SkPoint::Make(texCoords.fRight, texCoords.fTop),
313 texDomain,
314 patchColor);
315 vertices.write(mappedPts[2],
316 SkPoint::Make(texCoords.fRight, texCoords.fBottom),
317 texDomain,
318 patchColor);
319 }
msarett10e3d9b2016-08-18 15:46:03 -0700320 }
joshualitt33a5fce2015-11-18 13:28:51 -0800321 }
Robert Phillipse37f1c42020-03-09 16:10:18 -0400322
323 fMesh = helper.mesh();
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700324 }
325
326 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillipse37f1c42020-03-09 16:10:18 -0400327 if (!fProgramInfo || !fMesh) {
328 return;
329 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500330
Chris Dalton765ed362020-03-16 17:34:44 -0600331 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
Robert Phillips787fd9d2021-03-22 14:48:09 -0400332 flushState->bindTextures(fProgramInfo->geomProc(),
333 *fView.proxy(),
Chris Dalton765ed362020-03-16 17:34:44 -0600334 fProgramInfo->pipeline());
335 flushState->drawMesh(*fMesh);
joshualitt33a5fce2015-11-18 13:28:51 -0800336 }
337
Herb Derbye25c3002020-10-27 15:57:27 -0400338 CombineResult onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) override {
Brian Salomonfc527d22016-12-14 21:07:01 -0500339 NonAALatticeOp* that = t->cast<NonAALatticeOp>();
Greg Danieled96bca2019-12-05 15:05:54 -0500340 if (fView != that->fView) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000341 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400342 }
343 if (fFilter != that->fFilter) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000344 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400345 }
346 if (GrColorSpaceXform::Equals(fColorSpaceXform.get(), that->fColorSpaceXform.get())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000347 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400348 }
Brian Salomon815486c2017-07-11 08:52:13 -0400349 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000350 return CombineResult::kCannotCombine;
joshualitt33a5fce2015-11-18 13:28:51 -0800351 }
352
msarett10e3d9b2016-08-18 15:46:03 -0700353 fPatches.move_back_n(that->fPatches.count(), that->fPatches.begin());
Brian Osman0b537032018-12-26 12:16:44 -0500354 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000355 return CombineResult::kMerged;
joshualitt33a5fce2015-11-18 13:28:51 -0800356 }
357
John Stilesaf366522020-08-13 09:57:34 -0400358#if GR_TEST_UTILS
359 SkString onDumpInfo() const override {
360 SkString str;
361
362 for (int i = 0; i < fPatches.count(); ++i) {
363 str.appendf("%d: Color: 0x%08x Dst [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", i,
364 fPatches[i].fColor.toBytes_RGBA(), fPatches[i].fDst.fLeft,
365 fPatches[i].fDst.fTop, fPatches[i].fDst.fRight, fPatches[i].fDst.fBottom);
366 }
367
368 str += fHelper.dumpInfo();
369 return str;
370 }
371#endif
372
bsalomona71b8982016-06-30 12:13:52 -0700373 struct Patch {
374 SkMatrix fViewMatrix;
msarett10e3d9b2016-08-18 15:46:03 -0700375 std::unique_ptr<SkLatticeIter> fIter;
bsalomona71b8982016-06-30 12:13:52 -0700376 SkRect fDst;
Brian Osmancf860852018-10-31 14:04:39 -0400377 SkPMColor4f fColor;
bsalomona71b8982016-06-30 12:13:52 -0700378 };
379
Brian Salomon815486c2017-07-11 08:52:13 -0400380 Helper fHelper;
381 SkSTArray<1, Patch, true> fPatches;
Greg Danieled96bca2019-12-05 15:05:54 -0500382 GrSurfaceProxyView fView;
Greg Daniel82c6b102020-01-21 10:33:22 -0500383 SkAlphaType fAlphaType;
Brian Salomon2a943df2018-05-04 13:43:19 -0400384 sk_sp<GrColorSpaceXform> fColorSpaceXform;
385 GrSamplerState::Filter fFilter;
Brian Osman0b537032018-12-26 12:16:44 -0500386 bool fWideColor;
joshualitt33a5fce2015-11-18 13:28:51 -0800387
Chris Daltoneb694b72020-03-16 09:25:50 -0600388 GrSimpleMesh* fMesh = nullptr;
Robert Phillipse37f1c42020-03-09 16:10:18 -0400389 GrProgramInfo* fProgramInfo = nullptr;
390
John Stiles7571f9e2020-09-02 22:42:33 -0400391 using INHERITED = GrMeshDrawOp;
joshualitt33a5fce2015-11-18 13:28:51 -0800392};
393
Brian Salomon815486c2017-07-11 08:52:13 -0400394} // anonymous namespace
395
Robert Phillips769b4882021-09-07 16:48:46 -0400396namespace skgpu::v1::LatticeOp {
397
Herb Derbyc76d4092020-10-07 16:46:15 -0400398GrOp::Owner MakeNonAA(GrRecordingContext* context,
399 GrPaint&& paint,
400 const SkMatrix& viewMatrix,
401 GrSurfaceProxyView view,
402 SkAlphaType alphaType,
403 sk_sp<GrColorSpaceXform> colorSpaceXform,
404 GrSamplerState::Filter filter,
405 std::unique_ptr<SkLatticeIter> iter,
406 const SkRect& dst) {
Greg Daniel82c6b102020-01-21 10:33:22 -0500407 return NonAALatticeOp::Make(context, std::move(paint), viewMatrix, std::move(view), alphaType,
408 std::move(colorSpaceXform), filter, std::move(iter), dst);
joshualitt33a5fce2015-11-18 13:28:51 -0800409}
Robert Phillips769b4882021-09-07 16:48:46 -0400410
411} // namespace skgpu::v1::LatticeOp
Brian Salomon815486c2017-07-11 08:52:13 -0400412
413#if GR_TEST_UTILS
Robert Phillips769b4882021-09-07 16:48:46 -0400414#include "src/gpu/GrDrawOpTest.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500415#include "src/gpu/GrProxyProvider.h"
416#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon815486c2017-07-11 08:52:13 -0400417
418/** Randomly divides subset into count divs. */
419static void init_random_divs(int divs[], int count, int subsetStart, int subsetStop,
420 SkRandom* random) {
421 // Rules for lattice divs: Must be strictly increasing and in the range
422 // [subsetStart, subsetStop).
423 // Not terribly efficient alg for generating random divs:
424 // 1) Start with minimum legal pixels between each div.
425 // 2) Randomly assign the remaining pixels of the subset to divs.
426 // 3) Convert from pixel counts to div offsets.
427
428 // 1) Initially each divs[i] represents the number of pixels between
429 // div i-1 and i. The initial div is allowed to be at subsetStart. There
430 // must be one pixel spacing between subsequent divs.
431 divs[0] = 0;
432 for (int i = 1; i < count; ++i) {
433 divs[i] = 1;
434 }
435 // 2) Assign the remaining subset pixels to fall
436 int subsetLength = subsetStop - subsetStart;
437 for (int i = 0; i < subsetLength - count; ++i) {
438 // +1 because count divs means count+1 intervals.
439 int entry = random->nextULessThan(count + 1);
440 // We don't have an entry to to store the count after the last div
441 if (entry < count) {
442 divs[entry]++;
443 }
444 }
445 // 3) Now convert the counts between divs to pixel indices, incorporating the subset's offset.
446 int offset = subsetStart;
447 for (int i = 0; i < count; ++i) {
448 divs[i] += offset;
449 offset = divs[i];
450 }
451}
452
453GR_DRAW_OP_TEST_DEFINE(NonAALatticeOp) {
Brian Salomon815486c2017-07-11 08:52:13 -0400454 SkCanvas::Lattice lattice;
Brian Salomon18df7632017-07-11 14:32:18 -0400455 // We loop because our random lattice code can produce an invalid lattice in the case where
456 // there is a single div separator in both x and y and both are aligned with the left and top
457 // edge of the image subset, respectively.
458 std::unique_ptr<int[]> xdivs;
459 std::unique_ptr<int[]> ydivs;
Stan Ilievca8c0952017-12-11 13:01:58 -0500460 std::unique_ptr<SkCanvas::Lattice::RectType[]> flags;
461 std::unique_ptr<SkColor[]> colors;
Brian Salomon18df7632017-07-11 14:32:18 -0400462 SkIRect subset;
Brian Salomona56a7462020-02-07 14:17:25 -0500463 SkISize dims;
464 dims.fWidth = random->nextRangeU(1, 1000);
465 dims.fHeight = random->nextRangeU(1, 1000);
Robert Phillips0a15cc62019-07-30 12:49:10 -0400466 GrSurfaceOrigin origin = random->nextBool() ? kTopLeft_GrSurfaceOrigin
467 : kBottomLeft_GrSurfaceOrigin;
Greg Daniel4065d452018-11-16 15:43:41 -0500468 const GrBackendFormat format =
Robert Phillips0a15cc62019-07-30 12:49:10 -0400469 context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
470 GrRenderable::kNo);
Greg Daniel47c20e82020-01-21 14:29:57 -0500471
Brian Salomonbeb7f522019-08-30 16:19:42 -0400472 auto proxy = context->priv().proxyProvider()->createProxy(format,
Brian Salomona56a7462020-02-07 14:17:25 -0500473 dims,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400474 GrRenderable::kNo,
475 1,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400476 GrMipmapped::kNo,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400477 SkBackingFit::kExact,
478 SkBudgeted::kYes,
479 GrProtected::kNo);
Brian Salomon2a943df2018-05-04 13:43:19 -0400480
Brian Salomon18df7632017-07-11 14:32:18 -0400481 do {
Brian Salomon18df7632017-07-11 14:32:18 -0400482 if (random->nextBool()) {
Brian Salomona56a7462020-02-07 14:17:25 -0500483 subset.fLeft = random->nextULessThan(dims.fWidth);
484 subset.fRight = random->nextRangeU(subset.fLeft + 1, dims.fWidth);
485 subset.fTop = random->nextULessThan(dims.fHeight);
486 subset.fBottom = random->nextRangeU(subset.fTop + 1, dims.fHeight);
Brian Salomon18df7632017-07-11 14:32:18 -0400487 } else {
Brian Salomona56a7462020-02-07 14:17:25 -0500488 subset.setXYWH(0, 0, dims.fWidth, dims.fHeight);
Brian Salomon815486c2017-07-11 08:52:13 -0400489 }
Brian Salomon2a943df2018-05-04 13:43:19 -0400490 // SkCanvas::Lattice allows bounds to be null. However, SkCanvas creates a temp Lattice with
491 // a non-null bounds before creating a SkLatticeIter since SkLatticeIter requires a bounds.
Brian Salomon18df7632017-07-11 14:32:18 -0400492 lattice.fBounds = &subset;
493 lattice.fXCount = random->nextRangeU(1, subset.width());
494 lattice.fYCount = random->nextRangeU(1, subset.height());
495 xdivs.reset(new int[lattice.fXCount]);
496 ydivs.reset(new int[lattice.fYCount]);
497 init_random_divs(xdivs.get(), lattice.fXCount, subset.fLeft, subset.fRight, random);
498 init_random_divs(ydivs.get(), lattice.fYCount, subset.fTop, subset.fBottom, random);
499 lattice.fXDivs = xdivs.get();
500 lattice.fYDivs = ydivs.get();
501 bool hasFlags = random->nextBool();
502 if (hasFlags) {
503 int n = (lattice.fXCount + 1) * (lattice.fYCount + 1);
Stan Ilievca8c0952017-12-11 13:01:58 -0500504 flags.reset(new SkCanvas::Lattice::RectType[n]);
505 colors.reset(new SkColor[n]);
Brian Salomon18df7632017-07-11 14:32:18 -0400506 for (int i = 0; i < n; ++i) {
Stan Ilievca8c0952017-12-11 13:01:58 -0500507 flags[i] = random->nextBool() ? SkCanvas::Lattice::kTransparent
508 : SkCanvas::Lattice::kDefault;
Brian Salomon18df7632017-07-11 14:32:18 -0400509 }
Stan Ilievca8c0952017-12-11 13:01:58 -0500510 lattice.fRectTypes = flags.get();
511 lattice.fColors = colors.get();
Brian Salomon18df7632017-07-11 14:32:18 -0400512 } else {
Stan Ilievca8c0952017-12-11 13:01:58 -0500513 lattice.fRectTypes = nullptr;
514 lattice.fColors = nullptr;
Brian Salomon18df7632017-07-11 14:32:18 -0400515 }
Brian Salomona56a7462020-02-07 14:17:25 -0500516 } while (!SkLatticeIter::Valid(dims.fWidth, dims.fHeight, lattice));
Brian Salomon815486c2017-07-11 08:52:13 -0400517 SkRect dst;
518 dst.fLeft = random->nextRangeScalar(-2000.5f, 1000.f);
519 dst.fTop = random->nextRangeScalar(-2000.5f, 1000.f);
520 dst.fRight = dst.fLeft + random->nextRangeScalar(0.5f, 1000.f);
521 dst.fBottom = dst.fTop + random->nextRangeScalar(0.5f, 1000.f);
522 std::unique_ptr<SkLatticeIter> iter(new SkLatticeIter(lattice, dst));
523 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
Brian Salomon2a943df2018-05-04 13:43:19 -0400524 auto csxf = GrTest::TestColorXform(random);
525 GrSamplerState::Filter filter =
Brian Salomona3b02f52020-07-15 16:02:01 -0400526 random->nextBool() ? GrSamplerState::Filter::kNearest : GrSamplerState::Filter::kLinear;
Greg Danieled96bca2019-12-05 15:05:54 -0500527
528 GrSurfaceProxyView view(
529 std::move(proxy), origin,
Greg Daniel14b57212019-12-17 16:18:06 -0500530 context->priv().caps()->getReadSwizzle(format, GrColorType::kRGBA_8888));
Greg Danieled96bca2019-12-05 15:05:54 -0500531
532 return NonAALatticeOp::Make(context, std::move(paint), viewMatrix, std::move(view),
Greg Daniel82c6b102020-01-21 10:33:22 -0500533 kPremul_SkAlphaType, std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -0400534}
535
536#endif