blob: 00ea878025d9c28b24b98bacb91291003f6829ec [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrDrawOpTest.h"
13#include "src/gpu/GrGpu.h"
14#include "src/gpu/GrOpFlushState.h"
Robert Phillipse37f1c42020-03-09 16:10:18 -040015#include "src/gpu/GrProgramInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#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"
Robert Phillips03e4c952019-11-26 16:20:22 -050021#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
23#include "src/gpu/glsl/GrGLSLVarying.h"
24#include "src/gpu/ops/GrLatticeOp.h"
25#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
44 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
45 b->add32(GrColorSpaceXform::XformKey(fColorSpaceXform.get()));
46 }
47
Robert Phillipsf10535f2021-03-23 09:30:45 -040048 GrGLSLGeometryProcessor* createGLSLInstance(const GrShaderCaps& caps) const override {
Brian Salomon2a943df2018-05-04 13:43:19 -040049 class GLSLProcessor : public GrGLSLGeometryProcessor {
50 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 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);
John Stiles4d7ac492021-03-09 20:16:43 -050075 args.fFragBuilder->codeAppendf("half4 %s;", args.fOutputColor);
Brian Osmanf04fb3c2018-11-12 15:34:00 -050076 args.fVaryingHandler->addPassThroughAttribute(latticeGP.fInColor,
77 args.fOutputColor,
Brian Salomon2a943df2018-05-04 13:43:19 -040078 Interpolation::kCanBeFlat);
79 args.fFragBuilder->codeAppendf("%s = ", args.fOutputColor);
Brian Salomon87e9ddb2019-12-19 14:50:22 -050080 args.fFragBuilder->appendTextureLookupAndBlend(
Brian Salomon2a943df2018-05-04 13:43:19 -040081 args.fOutputColor,
Brian Salomon87e9ddb2019-12-19 14:50:22 -050082 SkBlendMode::kModulate,
Brian Salomon2a943df2018-05-04 13:43:19 -040083 args.fTexSamplers[0],
84 "clamp(textureCoords, textureDomain.xy, textureDomain.zw)",
Brian Salomon2a943df2018-05-04 13:43:19 -040085 &fColorSpaceXformHelper);
86 args.fFragBuilder->codeAppend(";");
John Stiles4d7ac492021-03-09 20:16:43 -050087 args.fFragBuilder->codeAppendf("const half4 %s = half4(1);", args.fOutputCoverage);
Brian Salomon2a943df2018-05-04 13:43:19 -040088 }
89 GrGLSLColorSpaceXformHelper fColorSpaceXformHelper;
90 };
91 return new GLSLProcessor;
92 }
93
94private:
Greg Danieled96bca2019-12-05 15:05:54 -050095 LatticeGP(const GrSurfaceProxyView& view, sk_sp<GrColorSpaceXform> csxf,
Brian Osman0b537032018-12-26 12:16:44 -050096 GrSamplerState::Filter filter, bool wideColor)
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050097 : INHERITED(kLatticeGP_ClassID)
98 , fColorSpaceXform(std::move(csxf)) {
Greg Daniel7a82edf2018-12-04 10:54:34 -050099
Robert Phillips323471e2019-11-11 11:33:37 -0500100 fSampler.reset(GrSamplerState(GrSamplerState::WrapMode::kClamp, filter),
Greg Danieled96bca2019-12-05 15:05:54 -0500101 view.proxy()->backendFormat(), view.swizzle());
Brian Salomonf7dcd762018-07-30 14:48:15 -0400102 this->setTextureSamplerCnt(1);
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500103 fInPosition = {"position", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
104 fInTextureCoords = {"textureCoords", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
105 fInTextureDomain = {"textureDomain", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
Brian Osman0b537032018-12-26 12:16:44 -0500106 fInColor = MakeColorAttribute("color", wideColor);
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500107 this->setVertexAttributes(&fInPosition, 4);
Brian Salomon92be2f72018-06-19 14:33:47 -0400108 }
109
Brian Salomonf7dcd762018-07-30 14:48:15 -0400110 const TextureSampler& onTextureSampler(int) const override { return fSampler; }
111
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500112 Attribute fInPosition;
113 Attribute fInTextureCoords;
114 Attribute fInTextureDomain;
115 Attribute fInColor;
Brian Salomon92be2f72018-06-19 14:33:47 -0400116
Brian Salomon2a943df2018-05-04 13:43:19 -0400117 sk_sp<GrColorSpaceXform> fColorSpaceXform;
118 TextureSampler fSampler;
119
John Stiles7571f9e2020-09-02 22:42:33 -0400120 using INHERITED = GrGeometryProcessor;
Brian Salomon2a943df2018-05-04 13:43:19 -0400121};
122
Brian Salomon815486c2017-07-11 08:52:13 -0400123class NonAALatticeOp final : public GrMeshDrawOp {
124private:
125 using Helper = GrSimpleMeshDrawOpHelper;
126
joshualitt33a5fce2015-11-18 13:28:51 -0800127public:
Brian Salomon25a88092016-12-01 09:36:50 -0500128 DEFINE_OP_CLASS_ID
joshualitt33a5fce2015-11-18 13:28:51 -0800129
Herb Derbyc76d4092020-10-07 16:46:15 -0400130 static GrOp::Owner Make(GrRecordingContext* context,
131 GrPaint&& paint,
132 const SkMatrix& viewMatrix,
133 GrSurfaceProxyView view,
134 SkAlphaType alphaType,
135 sk_sp<GrColorSpaceXform> colorSpaceXForm,
136 GrSamplerState::Filter filter,
137 std::unique_ptr<SkLatticeIter> iter,
138 const SkRect& dst) {
Greg Danieled96bca2019-12-05 15:05:54 -0500139 SkASSERT(view.proxy());
Robert Phillips7c525e62018-06-12 10:11:12 -0400140 return Helper::FactoryHelper<NonAALatticeOp>(context, std::move(paint), viewMatrix,
Greg Daniel82c6b102020-01-21 10:33:22 -0500141 std::move(view), alphaType,
Brian Salomon2a943df2018-05-04 13:43:19 -0400142 std::move(colorSpaceXForm), filter,
143 std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -0400144 }
145
Herb Derbyc76d4092020-10-07 16:46:15 -0400146 NonAALatticeOp(GrProcessorSet* processorSet, const SkPMColor4f& color,
Greg Danieled96bca2019-12-05 15:05:54 -0500147 const SkMatrix& viewMatrix, GrSurfaceProxyView view,
Greg Daniel82c6b102020-01-21 10:33:22 -0500148 SkAlphaType alphaType, sk_sp<GrColorSpaceXform> colorSpaceXform,
Greg Danielc594e622019-10-15 14:01:49 -0400149 GrSamplerState::Filter filter, std::unique_ptr<SkLatticeIter> iter,
150 const SkRect& dst)
Brian Salomon2a943df2018-05-04 13:43:19 -0400151 : INHERITED(ClassID())
Herb Derbyc76d4092020-10-07 16:46:15 -0400152 , fHelper(processorSet, GrAAType::kNone)
Greg Danieled96bca2019-12-05 15:05:54 -0500153 , fView(std::move(view))
Greg Daniel82c6b102020-01-21 10:33:22 -0500154 , fAlphaType(alphaType)
Brian Salomon2a943df2018-05-04 13:43:19 -0400155 , fColorSpaceXform(std::move(colorSpaceXform))
156 , fFilter(filter) {
bsalomona71b8982016-06-30 12:13:52 -0700157 Patch& patch = fPatches.push_back();
158 patch.fViewMatrix = viewMatrix;
159 patch.fColor = color;
msarett10e3d9b2016-08-18 15:46:03 -0700160 patch.fIter = std::move(iter);
bsalomona71b8982016-06-30 12:13:52 -0700161 patch.fDst = dst;
joshualitt33a5fce2015-11-18 13:28:51 -0800162
joshualitt33a5fce2015-11-18 13:28:51 -0800163 // setup bounds
Greg Daniel5faf4742019-10-01 15:14:44 -0400164 this->setTransformedBounds(patch.fDst, viewMatrix, HasAABloat::kNo, IsHairline::kNo);
joshualitt33a5fce2015-11-18 13:28:51 -0800165 }
166
Brian Salomonfc527d22016-12-14 21:07:01 -0500167 const char* name() const override { return "NonAALatticeOp"; }
robertphillips783a4da2015-11-19 14:00:02 -0800168
Chris Dalton1706cbf2019-05-21 19:35:29 -0600169 void visitProxies(const VisitProxyFunc& func) const override {
Brian Salomone69b9ef2020-07-22 11:18:06 -0400170 func(fView.proxy(), GrMipmapped::kNo);
Robert Phillipse37f1c42020-03-09 16:10:18 -0400171 if (fProgramInfo) {
Chris Daltonbe457422020-03-16 18:05:03 -0600172 fProgramInfo->visitFPProxies(func);
Robert Phillipse37f1c42020-03-09 16:10:18 -0400173 } else {
Robert Phillipse37f1c42020-03-09 16:10:18 -0400174 fHelper.visitProxies(func);
175 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400176 }
177
Brian Salomon815486c2017-07-11 08:52:13 -0400178 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
179
Chris Dalton6ce447a2019-06-23 18:07:38 -0600180 GrProcessorSet::Analysis finalize(
181 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
182 GrClampType clampType) override {
Greg Daniel82c6b102020-01-21 10:33:22 -0500183 auto opaque = fPatches[0].fColor.isOpaque() && fAlphaType == kOpaque_SkAlphaType
Brian Salomon2a943df2018-05-04 13:43:19 -0400184 ? GrProcessorAnalysisColor::Opaque::kYes
185 : GrProcessorAnalysisColor::Opaque::kNo;
186 auto analysisColor = GrProcessorAnalysisColor(opaque);
Chris Dalton6ce447a2019-06-23 18:07:38 -0600187 auto result = fHelper.finalizeProcessors(
188 caps, clip, hasMixedSampledCoverage, clampType, GrProcessorAnalysisCoverage::kNone,
189 &analysisColor);
Brian Salomon2a943df2018-05-04 13:43:19 -0400190 analysisColor.isConstant(&fPatches[0].fColor);
Brian Osman2715bf52019-12-06 14:38:47 -0500191 fWideColor = !fPatches[0].fColor.fitsInBytes();
Brian Salomon2a943df2018-05-04 13:43:19 -0400192 return result;
Brian Salomon815486c2017-07-11 08:52:13 -0400193 }
194
Brian Salomon92aee3d2016-12-21 09:20:25 -0500195private:
Robert Phillips2669a7b2020-03-12 12:07:19 -0400196 GrProgramInfo* programInfo() override { return fProgramInfo; }
197
Robert Phillips4133dc42020-03-11 15:55:55 -0400198 void onCreateProgramInfo(const GrCaps* caps,
199 SkArenaAlloc* arena,
Adlai Hollere2296f72020-11-19 13:41:26 -0500200 const GrSurfaceProxyView& writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400201 GrAppliedClip&& appliedClip,
Greg Danield358cbe2020-09-11 09:33:54 -0400202 const GrXferProcessor::DstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500203 GrXferBarrierFlags renderPassXferBarriers,
204 GrLoadOp colorLoadOp) override {
Robert Phillipse37f1c42020-03-09 16:10:18 -0400205
206 auto gp = LatticeGP::Make(arena, fView, fColorSpaceXform, fFilter, fWideColor);
joshualitt33a5fce2015-11-18 13:28:51 -0800207 if (!gp) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400208 return;
Robert Phillipse37f1c42020-03-09 16:10:18 -0400209 }
210
Brian Salomon8afde5f2020-04-01 16:22:00 -0400211 fProgramInfo = GrSimpleMeshDrawOpHelper::CreateProgramInfo(caps, arena, writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400212 std::move(appliedClip),
213 dstProxyView, gp,
214 fHelper.detachProcessorSet(),
215 GrPrimitiveType::kTriangles,
Greg Danield358cbe2020-09-11 09:33:54 -0400216 renderPassXferBarriers,
Greg Daniel42dbca52020-11-20 10:22:43 -0500217 colorLoadOp,
Robert Phillips4133dc42020-03-11 15:55:55 -0400218 fHelper.pipelineFlags(),
Chris Dalton765ed362020-03-16 17:34:44 -0600219 &GrUserStencilSettings::kUnused);
Robert Phillipse37f1c42020-03-09 16:10:18 -0400220 }
221
Robert Phillipse37f1c42020-03-09 16:10:18 -0400222 void onPrepareDraws(Target* target) override {
223 if (!fProgramInfo) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400224 this->createProgramInfo(target);
Robert Phillipse37f1c42020-03-09 16:10:18 -0400225 if (!fProgramInfo) {
226 return;
227 }
joshualitt33a5fce2015-11-18 13:28:51 -0800228 }
229
bsalomona71b8982016-06-30 12:13:52 -0700230 int patchCnt = fPatches.count();
msarett10e3d9b2016-08-18 15:46:03 -0700231 int numRects = 0;
232 for (int i = 0; i < patchCnt; i++) {
msarett0764efe2016-09-02 11:24:30 -0700233 numRects += fPatches[i].fIter->numRectsToDraw();
msarett10e3d9b2016-08-18 15:46:03 -0700234 }
joshualitt33a5fce2015-11-18 13:28:51 -0800235
Brian Salomon0db1b532017-07-12 15:21:43 -0400236 if (!numRects) {
237 return;
238 }
239
Robert Phillips787fd9d2021-03-22 14:48:09 -0400240 const size_t kVertexStride = fProgramInfo->geomProc().vertexStride();
Robert Phillipse94cdd22019-11-04 14:15:58 -0500241
242 QuadHelper helper(target, kVertexStride, numRects);
243
Brian Osmanc3064e72018-11-15 08:59:22 -0500244 GrVertexWriter vertices{helper.vertices()};
Brian Salomon12d22642019-01-29 14:38:50 -0500245 if (!vertices.fPtr) {
joshualitt33a5fce2015-11-18 13:28:51 -0800246 SkDebugf("Could not allocate vertices\n");
247 return;
248 }
249
bsalomona71b8982016-06-30 12:13:52 -0700250 for (int i = 0; i < patchCnt; i++) {
msarett7fc08582016-08-18 14:29:22 -0700251 const Patch& patch = fPatches[i];
Brian Osmanc3064e72018-11-15 08:59:22 -0500252
Brian Osman0b537032018-12-26 12:16:44 -0500253 GrVertexColor patchColor(patch.fColor, fWideColor);
msarett10e3d9b2016-08-18 15:46:03 -0700254
255 // Apply the view matrix here if it is scale-translate. Otherwise, we need to
256 // wait until we've created the dst rects.
257 bool isScaleTranslate = patch.fViewMatrix.isScaleTranslate();
258 if (isScaleTranslate) {
259 patch.fIter->mapDstScaleTranslate(patch.fViewMatrix);
260 }
joshualitt33a5fce2015-11-18 13:28:51 -0800261
Brian Salomon2a943df2018-05-04 13:43:19 -0400262 SkIRect srcR;
263 SkRect dstR;
Greg Danieled96bca2019-12-05 15:05:54 -0500264 Sk4f scales(1.f / fView.proxy()->width(), 1.f / fView.proxy()->height(),
265 1.f / fView.proxy()->width(), 1.f / fView.proxy()->height());
Brian Salomon2a943df2018-05-04 13:43:19 -0400266 static const Sk4f kDomainOffsets(0.5f, 0.5f, -0.5f, -0.5f);
Brian Osmanc3064e72018-11-15 08:59:22 -0500267 static const Sk4f kFlipOffsets(0.f, 1.f, 0.f, 1.f);
Brian Salomon2a943df2018-05-04 13:43:19 -0400268 static const Sk4f kFlipMuls(1.f, -1.f, 1.f, -1.f);
msarett10e3d9b2016-08-18 15:46:03 -0700269 while (patch.fIter->next(&srcR, &dstR)) {
Brian Salomon2a943df2018-05-04 13:43:19 -0400270 Sk4f coords(SkIntToScalar(srcR.fLeft), SkIntToScalar(srcR.fTop),
271 SkIntToScalar(srcR.fRight), SkIntToScalar(srcR.fBottom));
272 Sk4f domain = coords + kDomainOffsets;
273 coords *= scales;
274 domain *= scales;
Greg Danieled96bca2019-12-05 15:05:54 -0500275 if (fView.origin() == kBottomLeft_GrSurfaceOrigin) {
Brian Salomon2a943df2018-05-04 13:43:19 -0400276 coords = kFlipMuls * coords + kFlipOffsets;
277 domain = SkNx_shuffle<0, 3, 2, 1>(kFlipMuls * domain + kFlipOffsets);
278 }
Brian Osman4486d982018-11-15 15:56:04 -0500279 SkRect texDomain;
280 SkRect texCoords;
281 domain.store(&texDomain);
282 coords.store(&texCoords);
joshualitt33a5fce2015-11-18 13:28:51 -0800283
Greg Danield49b1282021-02-02 17:11:37 -0500284 if (isScaleTranslate) {
285 vertices.writeQuad(GrVertexWriter::TriStripFromRect(dstR),
286 GrVertexWriter::TriStripFromRect(texCoords),
287 texDomain,
288 patchColor);
289 } else {
290 SkPoint mappedPts[4];
291 patch.fViewMatrix.mapRectToQuad(mappedPts, dstR);
292 // In the above if statement, writeQuad writes the corners as:
293 // left-top, left-bottom, right-top, right-bottom.
294 // However, mapRectToQuad returns them in the order:
295 // left-top, right-top, right-bottom, left-bottom
296 // Thus we write out the vertices to match the writeQuad path.
297 vertices.write(mappedPts[0],
298 SkPoint::Make(texCoords.fLeft, texCoords.fTop),
Brian Osmanc3064e72018-11-15 08:59:22 -0500299 texDomain,
300 patchColor);
Greg Danield49b1282021-02-02 17:11:37 -0500301 vertices.write(mappedPts[3],
302 SkPoint::Make(texCoords.fLeft, texCoords.fBottom),
303 texDomain,
304 patchColor);
305 vertices.write(mappedPts[1],
306 SkPoint::Make(texCoords.fRight, texCoords.fTop),
307 texDomain,
308 patchColor);
309 vertices.write(mappedPts[2],
310 SkPoint::Make(texCoords.fRight, texCoords.fBottom),
311 texDomain,
312 patchColor);
313 }
msarett10e3d9b2016-08-18 15:46:03 -0700314 }
joshualitt33a5fce2015-11-18 13:28:51 -0800315 }
Robert Phillipse37f1c42020-03-09 16:10:18 -0400316
317 fMesh = helper.mesh();
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700318 }
319
320 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillipse37f1c42020-03-09 16:10:18 -0400321 if (!fProgramInfo || !fMesh) {
322 return;
323 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500324
Chris Dalton765ed362020-03-16 17:34:44 -0600325 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
Robert Phillips787fd9d2021-03-22 14:48:09 -0400326 flushState->bindTextures(fProgramInfo->geomProc(),
327 *fView.proxy(),
Chris Dalton765ed362020-03-16 17:34:44 -0600328 fProgramInfo->pipeline());
329 flushState->drawMesh(*fMesh);
joshualitt33a5fce2015-11-18 13:28:51 -0800330 }
331
Herb Derbye25c3002020-10-27 15:57:27 -0400332 CombineResult onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) override {
Brian Salomonfc527d22016-12-14 21:07:01 -0500333 NonAALatticeOp* that = t->cast<NonAALatticeOp>();
Greg Danieled96bca2019-12-05 15:05:54 -0500334 if (fView != that->fView) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000335 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400336 }
337 if (fFilter != that->fFilter) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000338 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400339 }
340 if (GrColorSpaceXform::Equals(fColorSpaceXform.get(), that->fColorSpaceXform.get())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000341 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400342 }
Brian Salomon815486c2017-07-11 08:52:13 -0400343 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000344 return CombineResult::kCannotCombine;
joshualitt33a5fce2015-11-18 13:28:51 -0800345 }
346
msarett10e3d9b2016-08-18 15:46:03 -0700347 fPatches.move_back_n(that->fPatches.count(), that->fPatches.begin());
Brian Osman0b537032018-12-26 12:16:44 -0500348 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000349 return CombineResult::kMerged;
joshualitt33a5fce2015-11-18 13:28:51 -0800350 }
351
John Stilesaf366522020-08-13 09:57:34 -0400352#if GR_TEST_UTILS
353 SkString onDumpInfo() const override {
354 SkString str;
355
356 for (int i = 0; i < fPatches.count(); ++i) {
357 str.appendf("%d: Color: 0x%08x Dst [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", i,
358 fPatches[i].fColor.toBytes_RGBA(), fPatches[i].fDst.fLeft,
359 fPatches[i].fDst.fTop, fPatches[i].fDst.fRight, fPatches[i].fDst.fBottom);
360 }
361
362 str += fHelper.dumpInfo();
363 return str;
364 }
365#endif
366
bsalomona71b8982016-06-30 12:13:52 -0700367 struct Patch {
368 SkMatrix fViewMatrix;
msarett10e3d9b2016-08-18 15:46:03 -0700369 std::unique_ptr<SkLatticeIter> fIter;
bsalomona71b8982016-06-30 12:13:52 -0700370 SkRect fDst;
Brian Osmancf860852018-10-31 14:04:39 -0400371 SkPMColor4f fColor;
bsalomona71b8982016-06-30 12:13:52 -0700372 };
373
Brian Salomon815486c2017-07-11 08:52:13 -0400374 Helper fHelper;
375 SkSTArray<1, Patch, true> fPatches;
Greg Danieled96bca2019-12-05 15:05:54 -0500376 GrSurfaceProxyView fView;
Greg Daniel82c6b102020-01-21 10:33:22 -0500377 SkAlphaType fAlphaType;
Brian Salomon2a943df2018-05-04 13:43:19 -0400378 sk_sp<GrColorSpaceXform> fColorSpaceXform;
379 GrSamplerState::Filter fFilter;
Brian Osman0b537032018-12-26 12:16:44 -0500380 bool fWideColor;
joshualitt33a5fce2015-11-18 13:28:51 -0800381
Chris Daltoneb694b72020-03-16 09:25:50 -0600382 GrSimpleMesh* fMesh = nullptr;
Robert Phillipse37f1c42020-03-09 16:10:18 -0400383 GrProgramInfo* fProgramInfo = nullptr;
384
John Stiles7571f9e2020-09-02 22:42:33 -0400385 using INHERITED = GrMeshDrawOp;
joshualitt33a5fce2015-11-18 13:28:51 -0800386};
387
Brian Salomon815486c2017-07-11 08:52:13 -0400388} // anonymous namespace
389
Brian Salomonfc527d22016-12-14 21:07:01 -0500390namespace GrLatticeOp {
Herb Derbyc76d4092020-10-07 16:46:15 -0400391GrOp::Owner MakeNonAA(GrRecordingContext* context,
392 GrPaint&& paint,
393 const SkMatrix& viewMatrix,
394 GrSurfaceProxyView view,
395 SkAlphaType alphaType,
396 sk_sp<GrColorSpaceXform> colorSpaceXform,
397 GrSamplerState::Filter filter,
398 std::unique_ptr<SkLatticeIter> iter,
399 const SkRect& dst) {
Greg Daniel82c6b102020-01-21 10:33:22 -0500400 return NonAALatticeOp::Make(context, std::move(paint), viewMatrix, std::move(view), alphaType,
401 std::move(colorSpaceXform), filter, std::move(iter), dst);
joshualitt33a5fce2015-11-18 13:28:51 -0800402}
John Stilesa6841be2020-08-06 14:11:56 -0400403} // namespace GrLatticeOp
Brian Salomon815486c2017-07-11 08:52:13 -0400404
405#if GR_TEST_UTILS
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500406#include "src/gpu/GrProxyProvider.h"
407#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon815486c2017-07-11 08:52:13 -0400408
409/** Randomly divides subset into count divs. */
410static void init_random_divs(int divs[], int count, int subsetStart, int subsetStop,
411 SkRandom* random) {
412 // Rules for lattice divs: Must be strictly increasing and in the range
413 // [subsetStart, subsetStop).
414 // Not terribly efficient alg for generating random divs:
415 // 1) Start with minimum legal pixels between each div.
416 // 2) Randomly assign the remaining pixels of the subset to divs.
417 // 3) Convert from pixel counts to div offsets.
418
419 // 1) Initially each divs[i] represents the number of pixels between
420 // div i-1 and i. The initial div is allowed to be at subsetStart. There
421 // must be one pixel spacing between subsequent divs.
422 divs[0] = 0;
423 for (int i = 1; i < count; ++i) {
424 divs[i] = 1;
425 }
426 // 2) Assign the remaining subset pixels to fall
427 int subsetLength = subsetStop - subsetStart;
428 for (int i = 0; i < subsetLength - count; ++i) {
429 // +1 because count divs means count+1 intervals.
430 int entry = random->nextULessThan(count + 1);
431 // We don't have an entry to to store the count after the last div
432 if (entry < count) {
433 divs[entry]++;
434 }
435 }
436 // 3) Now convert the counts between divs to pixel indices, incorporating the subset's offset.
437 int offset = subsetStart;
438 for (int i = 0; i < count; ++i) {
439 divs[i] += offset;
440 offset = divs[i];
441 }
442}
443
444GR_DRAW_OP_TEST_DEFINE(NonAALatticeOp) {
Brian Salomon815486c2017-07-11 08:52:13 -0400445 SkCanvas::Lattice lattice;
Brian Salomon18df7632017-07-11 14:32:18 -0400446 // We loop because our random lattice code can produce an invalid lattice in the case where
447 // there is a single div separator in both x and y and both are aligned with the left and top
448 // edge of the image subset, respectively.
449 std::unique_ptr<int[]> xdivs;
450 std::unique_ptr<int[]> ydivs;
Stan Ilievca8c0952017-12-11 13:01:58 -0500451 std::unique_ptr<SkCanvas::Lattice::RectType[]> flags;
452 std::unique_ptr<SkColor[]> colors;
Brian Salomon18df7632017-07-11 14:32:18 -0400453 SkIRect subset;
Brian Salomona56a7462020-02-07 14:17:25 -0500454 SkISize dims;
455 dims.fWidth = random->nextRangeU(1, 1000);
456 dims.fHeight = random->nextRangeU(1, 1000);
Robert Phillips0a15cc62019-07-30 12:49:10 -0400457 GrSurfaceOrigin origin = random->nextBool() ? kTopLeft_GrSurfaceOrigin
458 : kBottomLeft_GrSurfaceOrigin;
Greg Daniel4065d452018-11-16 15:43:41 -0500459 const GrBackendFormat format =
Robert Phillips0a15cc62019-07-30 12:49:10 -0400460 context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
461 GrRenderable::kNo);
Greg Daniel47c20e82020-01-21 14:29:57 -0500462
Brian Salomonbeb7f522019-08-30 16:19:42 -0400463 auto proxy = context->priv().proxyProvider()->createProxy(format,
Brian Salomona56a7462020-02-07 14:17:25 -0500464 dims,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400465 GrRenderable::kNo,
466 1,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400467 GrMipmapped::kNo,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400468 SkBackingFit::kExact,
469 SkBudgeted::kYes,
470 GrProtected::kNo);
Brian Salomon2a943df2018-05-04 13:43:19 -0400471
Brian Salomon18df7632017-07-11 14:32:18 -0400472 do {
Brian Salomon18df7632017-07-11 14:32:18 -0400473 if (random->nextBool()) {
Brian Salomona56a7462020-02-07 14:17:25 -0500474 subset.fLeft = random->nextULessThan(dims.fWidth);
475 subset.fRight = random->nextRangeU(subset.fLeft + 1, dims.fWidth);
476 subset.fTop = random->nextULessThan(dims.fHeight);
477 subset.fBottom = random->nextRangeU(subset.fTop + 1, dims.fHeight);
Brian Salomon18df7632017-07-11 14:32:18 -0400478 } else {
Brian Salomona56a7462020-02-07 14:17:25 -0500479 subset.setXYWH(0, 0, dims.fWidth, dims.fHeight);
Brian Salomon815486c2017-07-11 08:52:13 -0400480 }
Brian Salomon2a943df2018-05-04 13:43:19 -0400481 // SkCanvas::Lattice allows bounds to be null. However, SkCanvas creates a temp Lattice with
482 // a non-null bounds before creating a SkLatticeIter since SkLatticeIter requires a bounds.
Brian Salomon18df7632017-07-11 14:32:18 -0400483 lattice.fBounds = &subset;
484 lattice.fXCount = random->nextRangeU(1, subset.width());
485 lattice.fYCount = random->nextRangeU(1, subset.height());
486 xdivs.reset(new int[lattice.fXCount]);
487 ydivs.reset(new int[lattice.fYCount]);
488 init_random_divs(xdivs.get(), lattice.fXCount, subset.fLeft, subset.fRight, random);
489 init_random_divs(ydivs.get(), lattice.fYCount, subset.fTop, subset.fBottom, random);
490 lattice.fXDivs = xdivs.get();
491 lattice.fYDivs = ydivs.get();
492 bool hasFlags = random->nextBool();
493 if (hasFlags) {
494 int n = (lattice.fXCount + 1) * (lattice.fYCount + 1);
Stan Ilievca8c0952017-12-11 13:01:58 -0500495 flags.reset(new SkCanvas::Lattice::RectType[n]);
496 colors.reset(new SkColor[n]);
Brian Salomon18df7632017-07-11 14:32:18 -0400497 for (int i = 0; i < n; ++i) {
Stan Ilievca8c0952017-12-11 13:01:58 -0500498 flags[i] = random->nextBool() ? SkCanvas::Lattice::kTransparent
499 : SkCanvas::Lattice::kDefault;
Brian Salomon18df7632017-07-11 14:32:18 -0400500 }
Stan Ilievca8c0952017-12-11 13:01:58 -0500501 lattice.fRectTypes = flags.get();
502 lattice.fColors = colors.get();
Brian Salomon18df7632017-07-11 14:32:18 -0400503 } else {
Stan Ilievca8c0952017-12-11 13:01:58 -0500504 lattice.fRectTypes = nullptr;
505 lattice.fColors = nullptr;
Brian Salomon18df7632017-07-11 14:32:18 -0400506 }
Brian Salomona56a7462020-02-07 14:17:25 -0500507 } while (!SkLatticeIter::Valid(dims.fWidth, dims.fHeight, lattice));
Brian Salomon815486c2017-07-11 08:52:13 -0400508 SkRect dst;
509 dst.fLeft = random->nextRangeScalar(-2000.5f, 1000.f);
510 dst.fTop = random->nextRangeScalar(-2000.5f, 1000.f);
511 dst.fRight = dst.fLeft + random->nextRangeScalar(0.5f, 1000.f);
512 dst.fBottom = dst.fTop + random->nextRangeScalar(0.5f, 1000.f);
513 std::unique_ptr<SkLatticeIter> iter(new SkLatticeIter(lattice, dst));
514 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
Brian Salomon2a943df2018-05-04 13:43:19 -0400515 auto csxf = GrTest::TestColorXform(random);
516 GrSamplerState::Filter filter =
Brian Salomona3b02f52020-07-15 16:02:01 -0400517 random->nextBool() ? GrSamplerState::Filter::kNearest : GrSamplerState::Filter::kLinear;
Greg Danieled96bca2019-12-05 15:05:54 -0500518
519 GrSurfaceProxyView view(
520 std::move(proxy), origin,
Greg Daniel14b57212019-12-17 16:18:06 -0500521 context->priv().caps()->getReadSwizzle(format, GrColorType::kRGBA_8888));
Greg Danieled96bca2019-12-05 15:05:54 -0500522
523 return NonAALatticeOp::Make(context, std::move(paint), viewMatrix, std::move(view),
Greg Daniel82c6b102020-01-21 10:33:22 -0500524 kPremul_SkAlphaType, std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -0400525}
526
527#endif