blob: aeb1095a228bac68e089526d62a58bb13560a2d2 [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) {
Greg Danieled96bca2019-12-05 15:05:54 -050037 return arena->make<LatticeGP>(view, std::move(csxf), filter, wideColor);
Brian Salomon2a943df2018-05-04 13:43:19 -040038 }
39
40 const char* name() const override { return "LatticeGP"; }
41
42 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
43 b->add32(GrColorSpaceXform::XformKey(fColorSpaceXform.get()));
44 }
45
46 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps& caps) const override {
47 class GLSLProcessor : public GrGLSLGeometryProcessor {
48 public:
Brian Osman609f1592020-07-01 15:14:39 -040049 void setData(const GrGLSLProgramDataManager& pdman,
50 const GrPrimitiveProcessor& proc) override {
Brian Salomon2a943df2018-05-04 13:43:19 -040051 const auto& latticeGP = proc.cast<LatticeGP>();
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());
Michael Ludwig553db622020-06-19 10:47:30 -040064 gpArgs->fLocalCoordVar = latticeGP.fInTextureCoords.asShaderVar();
65
Brian Salomon2a943df2018-05-04 13:43:19 -040066 args.fFragBuilder->codeAppend("float2 textureCoords;");
Brian Osmanf04fb3c2018-11-12 15:34:00 -050067 args.fVaryingHandler->addPassThroughAttribute(latticeGP.fInTextureCoords,
Brian Salomon2a943df2018-05-04 13:43:19 -040068 "textureCoords");
69 args.fFragBuilder->codeAppend("float4 textureDomain;");
70 args.fVaryingHandler->addPassThroughAttribute(
Brian Osmanf04fb3c2018-11-12 15:34:00 -050071 latticeGP.fInTextureDomain, "textureDomain", Interpolation::kCanBeFlat);
72 args.fVaryingHandler->addPassThroughAttribute(latticeGP.fInColor,
73 args.fOutputColor,
Brian Salomon2a943df2018-05-04 13:43:19 -040074 Interpolation::kCanBeFlat);
75 args.fFragBuilder->codeAppendf("%s = ", args.fOutputColor);
Brian Salomon87e9ddb2019-12-19 14:50:22 -050076 args.fFragBuilder->appendTextureLookupAndBlend(
Brian Salomon2a943df2018-05-04 13:43:19 -040077 args.fOutputColor,
Brian Salomon87e9ddb2019-12-19 14:50:22 -050078 SkBlendMode::kModulate,
Brian Salomon2a943df2018-05-04 13:43:19 -040079 args.fTexSamplers[0],
80 "clamp(textureCoords, textureDomain.xy, textureDomain.zw)",
Brian Salomon2a943df2018-05-04 13:43:19 -040081 &fColorSpaceXformHelper);
82 args.fFragBuilder->codeAppend(";");
83 args.fFragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage);
84 }
85 GrGLSLColorSpaceXformHelper fColorSpaceXformHelper;
86 };
87 return new GLSLProcessor;
88 }
89
90private:
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050091 friend class ::SkArenaAlloc; // for access to ctor
92
Greg Danieled96bca2019-12-05 15:05:54 -050093 LatticeGP(const GrSurfaceProxyView& view, sk_sp<GrColorSpaceXform> csxf,
Brian Osman0b537032018-12-26 12:16:44 -050094 GrSamplerState::Filter filter, bool wideColor)
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050095 : INHERITED(kLatticeGP_ClassID)
96 , fColorSpaceXform(std::move(csxf)) {
Greg Daniel7a82edf2018-12-04 10:54:34 -050097
Robert Phillips323471e2019-11-11 11:33:37 -050098 fSampler.reset(GrSamplerState(GrSamplerState::WrapMode::kClamp, filter),
Greg Danieled96bca2019-12-05 15:05:54 -050099 view.proxy()->backendFormat(), view.swizzle());
Brian Salomonf7dcd762018-07-30 14:48:15 -0400100 this->setTextureSamplerCnt(1);
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500101 fInPosition = {"position", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
102 fInTextureCoords = {"textureCoords", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
103 fInTextureDomain = {"textureDomain", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
Brian Osman0b537032018-12-26 12:16:44 -0500104 fInColor = MakeColorAttribute("color", wideColor);
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500105 this->setVertexAttributes(&fInPosition, 4);
Brian Salomon92be2f72018-06-19 14:33:47 -0400106 }
107
Brian Salomonf7dcd762018-07-30 14:48:15 -0400108 const TextureSampler& onTextureSampler(int) const override { return fSampler; }
109
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500110 Attribute fInPosition;
111 Attribute fInTextureCoords;
112 Attribute fInTextureDomain;
113 Attribute fInColor;
Brian Salomon92be2f72018-06-19 14:33:47 -0400114
Brian Salomon2a943df2018-05-04 13:43:19 -0400115 sk_sp<GrColorSpaceXform> fColorSpaceXform;
116 TextureSampler fSampler;
117
118 typedef GrGeometryProcessor INHERITED;
119};
120
Brian Salomon815486c2017-07-11 08:52:13 -0400121class NonAALatticeOp final : public GrMeshDrawOp {
122private:
123 using Helper = GrSimpleMeshDrawOpHelper;
124
joshualitt33a5fce2015-11-18 13:28:51 -0800125public:
Brian Salomon25a88092016-12-01 09:36:50 -0500126 DEFINE_OP_CLASS_ID
joshualitt33a5fce2015-11-18 13:28:51 -0800127
Robert Phillipsb97da532019-02-12 15:24:12 -0500128 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400129 GrPaint&& paint,
130 const SkMatrix& viewMatrix,
Greg Danieled96bca2019-12-05 15:05:54 -0500131 GrSurfaceProxyView view,
Greg Daniel82c6b102020-01-21 10:33:22 -0500132 SkAlphaType alphaType,
Brian Salomon2a943df2018-05-04 13:43:19 -0400133 sk_sp<GrColorSpaceXform> colorSpaceXForm,
134 GrSamplerState::Filter filter,
Robert Phillips7c525e62018-06-12 10:11:12 -0400135 std::unique_ptr<SkLatticeIter> iter,
136 const SkRect& dst) {
Greg Danieled96bca2019-12-05 15:05:54 -0500137 SkASSERT(view.proxy());
Robert Phillips7c525e62018-06-12 10:11:12 -0400138 return Helper::FactoryHelper<NonAALatticeOp>(context, std::move(paint), viewMatrix,
Greg Daniel82c6b102020-01-21 10:33:22 -0500139 std::move(view), alphaType,
Brian Salomon2a943df2018-05-04 13:43:19 -0400140 std::move(colorSpaceXForm), filter,
141 std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -0400142 }
143
Brian Osmancf860852018-10-31 14:04:39 -0400144 NonAALatticeOp(Helper::MakeArgs& helperArgs, const SkPMColor4f& color,
Greg Danieled96bca2019-12-05 15:05:54 -0500145 const SkMatrix& viewMatrix, GrSurfaceProxyView view,
Greg Daniel82c6b102020-01-21 10:33:22 -0500146 SkAlphaType alphaType, sk_sp<GrColorSpaceXform> colorSpaceXform,
Greg Danielc594e622019-10-15 14:01:49 -0400147 GrSamplerState::Filter filter, std::unique_ptr<SkLatticeIter> iter,
148 const SkRect& dst)
Brian Salomon2a943df2018-05-04 13:43:19 -0400149 : INHERITED(ClassID())
150 , fHelper(helperArgs, GrAAType::kNone)
Greg Danieled96bca2019-12-05 15:05:54 -0500151 , fView(std::move(view))
Greg Daniel82c6b102020-01-21 10:33:22 -0500152 , fAlphaType(alphaType)
Brian Salomon2a943df2018-05-04 13:43:19 -0400153 , fColorSpaceXform(std::move(colorSpaceXform))
154 , fFilter(filter) {
bsalomona71b8982016-06-30 12:13:52 -0700155 Patch& patch = fPatches.push_back();
156 patch.fViewMatrix = viewMatrix;
157 patch.fColor = color;
msarett10e3d9b2016-08-18 15:46:03 -0700158 patch.fIter = std::move(iter);
bsalomona71b8982016-06-30 12:13:52 -0700159 patch.fDst = dst;
joshualitt33a5fce2015-11-18 13:28:51 -0800160
joshualitt33a5fce2015-11-18 13:28:51 -0800161 // setup bounds
Greg Daniel5faf4742019-10-01 15:14:44 -0400162 this->setTransformedBounds(patch.fDst, viewMatrix, HasAABloat::kNo, IsHairline::kNo);
joshualitt33a5fce2015-11-18 13:28:51 -0800163 }
164
Brian Salomonfc527d22016-12-14 21:07:01 -0500165 const char* name() const override { return "NonAALatticeOp"; }
robertphillips783a4da2015-11-19 14:00:02 -0800166
Chris Dalton1706cbf2019-05-21 19:35:29 -0600167 void visitProxies(const VisitProxyFunc& func) const override {
Brian Salomone69b9ef2020-07-22 11:18:06 -0400168 func(fView.proxy(), GrMipmapped::kNo);
Robert Phillipse37f1c42020-03-09 16:10:18 -0400169 if (fProgramInfo) {
Chris Daltonbe457422020-03-16 18:05:03 -0600170 fProgramInfo->visitFPProxies(func);
Robert Phillipse37f1c42020-03-09 16:10:18 -0400171 } else {
Robert Phillipse37f1c42020-03-09 16:10:18 -0400172 fHelper.visitProxies(func);
173 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400174 }
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 Daniel82c6b102020-01-21 10:33:22 -0500197 auto opaque = fPatches[0].fColor.isOpaque() && fAlphaType == kOpaque_SkAlphaType
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 Osman2715bf52019-12-06 14:38:47 -0500205 fWideColor = !fPatches[0].fColor.fitsInBytes();
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:
Robert Phillips2669a7b2020-03-12 12:07:19 -0400210 GrProgramInfo* programInfo() override { return fProgramInfo; }
211
Robert Phillips4133dc42020-03-11 15:55:55 -0400212 void onCreateProgramInfo(const GrCaps* caps,
213 SkArenaAlloc* arena,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400214 const GrSurfaceProxyView* writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400215 GrAppliedClip&& appliedClip,
216 const GrXferProcessor::DstProxyView& dstProxyView) override {
Robert Phillipse37f1c42020-03-09 16:10:18 -0400217
218 auto gp = LatticeGP::Make(arena, fView, fColorSpaceXform, fFilter, fWideColor);
joshualitt33a5fce2015-11-18 13:28:51 -0800219 if (!gp) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400220 return;
Robert Phillipse37f1c42020-03-09 16:10:18 -0400221 }
222
Brian Salomon8afde5f2020-04-01 16:22:00 -0400223 fProgramInfo = GrSimpleMeshDrawOpHelper::CreateProgramInfo(caps, arena, writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400224 std::move(appliedClip),
225 dstProxyView, gp,
226 fHelper.detachProcessorSet(),
227 GrPrimitiveType::kTriangles,
228 fHelper.pipelineFlags(),
Chris Dalton765ed362020-03-16 17:34:44 -0600229 &GrUserStencilSettings::kUnused);
Robert Phillipse37f1c42020-03-09 16:10:18 -0400230 }
231
Robert Phillipse37f1c42020-03-09 16:10:18 -0400232 void onPrepareDraws(Target* target) override {
233 if (!fProgramInfo) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400234 this->createProgramInfo(target);
Robert Phillipse37f1c42020-03-09 16:10:18 -0400235 if (!fProgramInfo) {
236 return;
237 }
joshualitt33a5fce2015-11-18 13:28:51 -0800238 }
239
bsalomona71b8982016-06-30 12:13:52 -0700240 int patchCnt = fPatches.count();
msarett10e3d9b2016-08-18 15:46:03 -0700241 int numRects = 0;
242 for (int i = 0; i < patchCnt; i++) {
msarett0764efe2016-09-02 11:24:30 -0700243 numRects += fPatches[i].fIter->numRectsToDraw();
msarett10e3d9b2016-08-18 15:46:03 -0700244 }
joshualitt33a5fce2015-11-18 13:28:51 -0800245
Brian Salomon0db1b532017-07-12 15:21:43 -0400246 if (!numRects) {
247 return;
248 }
249
Robert Phillipse37f1c42020-03-09 16:10:18 -0400250 const size_t kVertexStride = fProgramInfo->primProc().vertexStride();
Robert Phillipse94cdd22019-11-04 14:15:58 -0500251
252 QuadHelper helper(target, kVertexStride, numRects);
253
Brian Osmanc3064e72018-11-15 08:59:22 -0500254 GrVertexWriter vertices{helper.vertices()};
Brian Salomon12d22642019-01-29 14:38:50 -0500255 if (!vertices.fPtr) {
joshualitt33a5fce2015-11-18 13:28:51 -0800256 SkDebugf("Could not allocate vertices\n");
257 return;
258 }
259
bsalomona71b8982016-06-30 12:13:52 -0700260 for (int i = 0; i < patchCnt; i++) {
msarett7fc08582016-08-18 14:29:22 -0700261 const Patch& patch = fPatches[i];
Brian Osmanc3064e72018-11-15 08:59:22 -0500262
Brian Osman0b537032018-12-26 12:16:44 -0500263 GrVertexColor patchColor(patch.fColor, fWideColor);
msarett10e3d9b2016-08-18 15:46:03 -0700264
265 // Apply the view matrix here if it is scale-translate. Otherwise, we need to
266 // wait until we've created the dst rects.
267 bool isScaleTranslate = patch.fViewMatrix.isScaleTranslate();
268 if (isScaleTranslate) {
269 patch.fIter->mapDstScaleTranslate(patch.fViewMatrix);
270 }
joshualitt33a5fce2015-11-18 13:28:51 -0800271
Brian Salomon2a943df2018-05-04 13:43:19 -0400272 SkIRect srcR;
273 SkRect dstR;
Brian Osmanc3064e72018-11-15 08:59:22 -0500274 SkPoint* patchPositions = reinterpret_cast<SkPoint*>(vertices.fPtr);
Greg Danieled96bca2019-12-05 15:05:54 -0500275 Sk4f scales(1.f / fView.proxy()->width(), 1.f / fView.proxy()->height(),
276 1.f / fView.proxy()->width(), 1.f / fView.proxy()->height());
Brian Salomon2a943df2018-05-04 13:43:19 -0400277 static const Sk4f kDomainOffsets(0.5f, 0.5f, -0.5f, -0.5f);
Brian Osmanc3064e72018-11-15 08:59:22 -0500278 static const Sk4f kFlipOffsets(0.f, 1.f, 0.f, 1.f);
Brian Salomon2a943df2018-05-04 13:43:19 -0400279 static const Sk4f kFlipMuls(1.f, -1.f, 1.f, -1.f);
msarett10e3d9b2016-08-18 15:46:03 -0700280 while (patch.fIter->next(&srcR, &dstR)) {
Brian Salomon2a943df2018-05-04 13:43:19 -0400281 Sk4f coords(SkIntToScalar(srcR.fLeft), SkIntToScalar(srcR.fTop),
282 SkIntToScalar(srcR.fRight), SkIntToScalar(srcR.fBottom));
283 Sk4f domain = coords + kDomainOffsets;
284 coords *= scales;
285 domain *= scales;
Greg Danieled96bca2019-12-05 15:05:54 -0500286 if (fView.origin() == kBottomLeft_GrSurfaceOrigin) {
Brian Salomon2a943df2018-05-04 13:43:19 -0400287 coords = kFlipMuls * coords + kFlipOffsets;
288 domain = SkNx_shuffle<0, 3, 2, 1>(kFlipMuls * domain + kFlipOffsets);
289 }
Brian Osman4486d982018-11-15 15:56:04 -0500290 SkRect texDomain;
291 SkRect texCoords;
292 domain.store(&texDomain);
293 coords.store(&texCoords);
joshualitt33a5fce2015-11-18 13:28:51 -0800294
Brian Osman0dd43022018-11-16 15:53:26 -0500295 vertices.writeQuad(GrVertexWriter::TriStripFromRect(dstR),
296 GrVertexWriter::TriStripFromRect(texCoords),
Brian Osmanc3064e72018-11-15 08:59:22 -0500297 texDomain,
298 patchColor);
joshualitt33a5fce2015-11-18 13:28:51 -0800299 }
msarett10e3d9b2016-08-18 15:46:03 -0700300
301 // If we didn't handle it above, apply the matrix here.
302 if (!isScaleTranslate) {
Robert Phillipsee08d522019-10-28 16:34:44 -0400303 SkMatrixPriv::MapPointsWithStride(
304 patch.fViewMatrix, patchPositions, kVertexStride,
305 GrResourceProvider::NumVertsPerNonAAQuad() * patch.fIter->numRectsToDraw());
msarett10e3d9b2016-08-18 15:46:03 -0700306 }
joshualitt33a5fce2015-11-18 13:28:51 -0800307 }
Robert Phillipse37f1c42020-03-09 16:10:18 -0400308
309 fMesh = helper.mesh();
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700310 }
311
312 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillipse37f1c42020-03-09 16:10:18 -0400313 if (!fProgramInfo || !fMesh) {
314 return;
315 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500316
Chris Dalton765ed362020-03-16 17:34:44 -0600317 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
318 flushState->bindTextures(fProgramInfo->primProc(), *fView.proxy(),
319 fProgramInfo->pipeline());
320 flushState->drawMesh(*fMesh);
joshualitt33a5fce2015-11-18 13:28:51 -0800321 }
322
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500323 CombineResult onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*,
324 const GrCaps& caps) override {
Brian Salomonfc527d22016-12-14 21:07:01 -0500325 NonAALatticeOp* that = t->cast<NonAALatticeOp>();
Greg Danieled96bca2019-12-05 15:05:54 -0500326 if (fView != that->fView) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000327 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400328 }
329 if (fFilter != that->fFilter) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000330 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400331 }
332 if (GrColorSpaceXform::Equals(fColorSpaceXform.get(), that->fColorSpaceXform.get())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000333 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400334 }
Brian Salomon815486c2017-07-11 08:52:13 -0400335 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000336 return CombineResult::kCannotCombine;
joshualitt33a5fce2015-11-18 13:28:51 -0800337 }
338
msarett10e3d9b2016-08-18 15:46:03 -0700339 fPatches.move_back_n(that->fPatches.count(), that->fPatches.begin());
Brian Osman0b537032018-12-26 12:16:44 -0500340 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000341 return CombineResult::kMerged;
joshualitt33a5fce2015-11-18 13:28:51 -0800342 }
343
bsalomona71b8982016-06-30 12:13:52 -0700344 struct Patch {
345 SkMatrix fViewMatrix;
msarett10e3d9b2016-08-18 15:46:03 -0700346 std::unique_ptr<SkLatticeIter> fIter;
bsalomona71b8982016-06-30 12:13:52 -0700347 SkRect fDst;
Brian Osmancf860852018-10-31 14:04:39 -0400348 SkPMColor4f fColor;
bsalomona71b8982016-06-30 12:13:52 -0700349 };
350
Brian Salomon815486c2017-07-11 08:52:13 -0400351 Helper fHelper;
352 SkSTArray<1, Patch, true> fPatches;
Greg Danieled96bca2019-12-05 15:05:54 -0500353 GrSurfaceProxyView fView;
Greg Daniel82c6b102020-01-21 10:33:22 -0500354 SkAlphaType fAlphaType;
Brian Salomon2a943df2018-05-04 13:43:19 -0400355 sk_sp<GrColorSpaceXform> fColorSpaceXform;
356 GrSamplerState::Filter fFilter;
Brian Osman0b537032018-12-26 12:16:44 -0500357 bool fWideColor;
joshualitt33a5fce2015-11-18 13:28:51 -0800358
Chris Daltoneb694b72020-03-16 09:25:50 -0600359 GrSimpleMesh* fMesh = nullptr;
Robert Phillipse37f1c42020-03-09 16:10:18 -0400360 GrProgramInfo* fProgramInfo = nullptr;
361
Brian Salomon815486c2017-07-11 08:52:13 -0400362 typedef GrMeshDrawOp INHERITED;
joshualitt33a5fce2015-11-18 13:28:51 -0800363};
364
Brian Salomon815486c2017-07-11 08:52:13 -0400365} // anonymous namespace
366
Brian Salomonfc527d22016-12-14 21:07:01 -0500367namespace GrLatticeOp {
Robert Phillipsb97da532019-02-12 15:24:12 -0500368std::unique_ptr<GrDrawOp> MakeNonAA(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400369 GrPaint&& paint,
370 const SkMatrix& viewMatrix,
Greg Danieled96bca2019-12-05 15:05:54 -0500371 GrSurfaceProxyView view,
Greg Daniel82c6b102020-01-21 10:33:22 -0500372 SkAlphaType alphaType,
Brian Salomon2a943df2018-05-04 13:43:19 -0400373 sk_sp<GrColorSpaceXform> colorSpaceXform,
374 GrSamplerState::Filter filter,
Robert Phillips7c525e62018-06-12 10:11:12 -0400375 std::unique_ptr<SkLatticeIter> iter,
376 const SkRect& dst) {
Greg Daniel82c6b102020-01-21 10:33:22 -0500377 return NonAALatticeOp::Make(context, std::move(paint), viewMatrix, std::move(view), alphaType,
378 std::move(colorSpaceXform), filter, std::move(iter), dst);
joshualitt33a5fce2015-11-18 13:28:51 -0800379}
380};
Brian Salomon815486c2017-07-11 08:52:13 -0400381
382#if GR_TEST_UTILS
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500383#include "src/gpu/GrProxyProvider.h"
384#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon815486c2017-07-11 08:52:13 -0400385
386/** Randomly divides subset into count divs. */
387static void init_random_divs(int divs[], int count, int subsetStart, int subsetStop,
388 SkRandom* random) {
389 // Rules for lattice divs: Must be strictly increasing and in the range
390 // [subsetStart, subsetStop).
391 // Not terribly efficient alg for generating random divs:
392 // 1) Start with minimum legal pixels between each div.
393 // 2) Randomly assign the remaining pixels of the subset to divs.
394 // 3) Convert from pixel counts to div offsets.
395
396 // 1) Initially each divs[i] represents the number of pixels between
397 // div i-1 and i. The initial div is allowed to be at subsetStart. There
398 // must be one pixel spacing between subsequent divs.
399 divs[0] = 0;
400 for (int i = 1; i < count; ++i) {
401 divs[i] = 1;
402 }
403 // 2) Assign the remaining subset pixels to fall
404 int subsetLength = subsetStop - subsetStart;
405 for (int i = 0; i < subsetLength - count; ++i) {
406 // +1 because count divs means count+1 intervals.
407 int entry = random->nextULessThan(count + 1);
408 // We don't have an entry to to store the count after the last div
409 if (entry < count) {
410 divs[entry]++;
411 }
412 }
413 // 3) Now convert the counts between divs to pixel indices, incorporating the subset's offset.
414 int offset = subsetStart;
415 for (int i = 0; i < count; ++i) {
416 divs[i] += offset;
417 offset = divs[i];
418 }
419}
420
421GR_DRAW_OP_TEST_DEFINE(NonAALatticeOp) {
Brian Salomon815486c2017-07-11 08:52:13 -0400422 SkCanvas::Lattice lattice;
Brian Salomon18df7632017-07-11 14:32:18 -0400423 // We loop because our random lattice code can produce an invalid lattice in the case where
424 // there is a single div separator in both x and y and both are aligned with the left and top
425 // edge of the image subset, respectively.
426 std::unique_ptr<int[]> xdivs;
427 std::unique_ptr<int[]> ydivs;
Stan Ilievca8c0952017-12-11 13:01:58 -0500428 std::unique_ptr<SkCanvas::Lattice::RectType[]> flags;
429 std::unique_ptr<SkColor[]> colors;
Brian Salomon18df7632017-07-11 14:32:18 -0400430 SkIRect subset;
Brian Salomona56a7462020-02-07 14:17:25 -0500431 SkISize dims;
432 dims.fWidth = random->nextRangeU(1, 1000);
433 dims.fHeight = random->nextRangeU(1, 1000);
Robert Phillips0a15cc62019-07-30 12:49:10 -0400434 GrSurfaceOrigin origin = random->nextBool() ? kTopLeft_GrSurfaceOrigin
435 : kBottomLeft_GrSurfaceOrigin;
Greg Daniel4065d452018-11-16 15:43:41 -0500436 const GrBackendFormat format =
Robert Phillips0a15cc62019-07-30 12:49:10 -0400437 context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
438 GrRenderable::kNo);
Greg Daniel47c20e82020-01-21 14:29:57 -0500439
Brian Salomonbeb7f522019-08-30 16:19:42 -0400440 auto proxy = context->priv().proxyProvider()->createProxy(format,
Brian Salomona56a7462020-02-07 14:17:25 -0500441 dims,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400442 GrRenderable::kNo,
443 1,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400444 GrMipmapped::kNo,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400445 SkBackingFit::kExact,
446 SkBudgeted::kYes,
447 GrProtected::kNo);
Brian Salomon2a943df2018-05-04 13:43:19 -0400448
Brian Salomon18df7632017-07-11 14:32:18 -0400449 do {
Brian Salomon18df7632017-07-11 14:32:18 -0400450 if (random->nextBool()) {
Brian Salomona56a7462020-02-07 14:17:25 -0500451 subset.fLeft = random->nextULessThan(dims.fWidth);
452 subset.fRight = random->nextRangeU(subset.fLeft + 1, dims.fWidth);
453 subset.fTop = random->nextULessThan(dims.fHeight);
454 subset.fBottom = random->nextRangeU(subset.fTop + 1, dims.fHeight);
Brian Salomon18df7632017-07-11 14:32:18 -0400455 } else {
Brian Salomona56a7462020-02-07 14:17:25 -0500456 subset.setXYWH(0, 0, dims.fWidth, dims.fHeight);
Brian Salomon815486c2017-07-11 08:52:13 -0400457 }
Brian Salomon2a943df2018-05-04 13:43:19 -0400458 // SkCanvas::Lattice allows bounds to be null. However, SkCanvas creates a temp Lattice with
459 // a non-null bounds before creating a SkLatticeIter since SkLatticeIter requires a bounds.
Brian Salomon18df7632017-07-11 14:32:18 -0400460 lattice.fBounds = &subset;
461 lattice.fXCount = random->nextRangeU(1, subset.width());
462 lattice.fYCount = random->nextRangeU(1, subset.height());
463 xdivs.reset(new int[lattice.fXCount]);
464 ydivs.reset(new int[lattice.fYCount]);
465 init_random_divs(xdivs.get(), lattice.fXCount, subset.fLeft, subset.fRight, random);
466 init_random_divs(ydivs.get(), lattice.fYCount, subset.fTop, subset.fBottom, random);
467 lattice.fXDivs = xdivs.get();
468 lattice.fYDivs = ydivs.get();
469 bool hasFlags = random->nextBool();
470 if (hasFlags) {
471 int n = (lattice.fXCount + 1) * (lattice.fYCount + 1);
Stan Ilievca8c0952017-12-11 13:01:58 -0500472 flags.reset(new SkCanvas::Lattice::RectType[n]);
473 colors.reset(new SkColor[n]);
Brian Salomon18df7632017-07-11 14:32:18 -0400474 for (int i = 0; i < n; ++i) {
Stan Ilievca8c0952017-12-11 13:01:58 -0500475 flags[i] = random->nextBool() ? SkCanvas::Lattice::kTransparent
476 : SkCanvas::Lattice::kDefault;
Brian Salomon18df7632017-07-11 14:32:18 -0400477 }
Stan Ilievca8c0952017-12-11 13:01:58 -0500478 lattice.fRectTypes = flags.get();
479 lattice.fColors = colors.get();
Brian Salomon18df7632017-07-11 14:32:18 -0400480 } else {
Stan Ilievca8c0952017-12-11 13:01:58 -0500481 lattice.fRectTypes = nullptr;
482 lattice.fColors = nullptr;
Brian Salomon18df7632017-07-11 14:32:18 -0400483 }
Brian Salomona56a7462020-02-07 14:17:25 -0500484 } while (!SkLatticeIter::Valid(dims.fWidth, dims.fHeight, lattice));
Brian Salomon815486c2017-07-11 08:52:13 -0400485 SkRect dst;
486 dst.fLeft = random->nextRangeScalar(-2000.5f, 1000.f);
487 dst.fTop = random->nextRangeScalar(-2000.5f, 1000.f);
488 dst.fRight = dst.fLeft + random->nextRangeScalar(0.5f, 1000.f);
489 dst.fBottom = dst.fTop + random->nextRangeScalar(0.5f, 1000.f);
490 std::unique_ptr<SkLatticeIter> iter(new SkLatticeIter(lattice, dst));
491 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
Brian Salomon2a943df2018-05-04 13:43:19 -0400492 auto csxf = GrTest::TestColorXform(random);
493 GrSamplerState::Filter filter =
Brian Salomona3b02f52020-07-15 16:02:01 -0400494 random->nextBool() ? GrSamplerState::Filter::kNearest : GrSamplerState::Filter::kLinear;
Greg Danieled96bca2019-12-05 15:05:54 -0500495
496 GrSurfaceProxyView view(
497 std::move(proxy), origin,
Greg Daniel14b57212019-12-17 16:18:06 -0500498 context->priv().caps()->getReadSwizzle(format, GrColorType::kRGBA_8888));
Greg Danieled96bca2019-12-05 15:05:54 -0500499
500 return NonAALatticeOp::Make(context, std::move(paint), viewMatrix, std::move(view),
Greg Daniel82c6b102020-01-21 10:33:22 -0500501 kPremul_SkAlphaType, std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -0400502}
503
504#endif