blob: fe9983fb51dd2ba2ff34edbb10f38ccd6cebe69a [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:
49 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc,
Brian Salomonc241b582019-11-27 08:57:17 -050050 const CoordTransformRange& transformRange) override {
Brian Salomon2a943df2018-05-04 13:43:19 -040051 const auto& latticeGP = proc.cast<LatticeGP>();
Michael Ludwig553db622020-06-19 10:47:30 -040052 this->setTransformDataHelper(pdman, transformRange);
Brian Osmanc891b102018-06-14 14:50:17 -040053 fColorSpaceXformHelper.setData(pdman, latticeGP.fColorSpaceXform.get());
Brian Salomon2a943df2018-05-04 13:43:19 -040054 }
55
56 private:
57 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
58 using Interpolation = GrGLSLVaryingHandler::Interpolation;
59 const auto& latticeGP = args.fGP.cast<LatticeGP>();
60 fColorSpaceXformHelper.emitCode(args.fUniformHandler,
61 latticeGP.fColorSpaceXform.get());
62
63 args.fVaryingHandler->emitAttributes(latticeGP);
Brian Osmanf04fb3c2018-11-12 15:34:00 -050064 this->writeOutputPosition(args.fVertBuilder, gpArgs, latticeGP.fInPosition.name());
Michael Ludwig553db622020-06-19 10:47:30 -040065 gpArgs->fLocalCoordVar = latticeGP.fInTextureCoords.asShaderVar();
66
Brian Salomon2a943df2018-05-04 13:43:19 -040067 args.fFragBuilder->codeAppend("float2 textureCoords;");
Brian Osmanf04fb3c2018-11-12 15:34:00 -050068 args.fVaryingHandler->addPassThroughAttribute(latticeGP.fInTextureCoords,
Brian Salomon2a943df2018-05-04 13:43:19 -040069 "textureCoords");
70 args.fFragBuilder->codeAppend("float4 textureDomain;");
71 args.fVaryingHandler->addPassThroughAttribute(
Brian Osmanf04fb3c2018-11-12 15:34:00 -050072 latticeGP.fInTextureDomain, "textureDomain", Interpolation::kCanBeFlat);
73 args.fVaryingHandler->addPassThroughAttribute(latticeGP.fInColor,
74 args.fOutputColor,
Brian Salomon2a943df2018-05-04 13:43:19 -040075 Interpolation::kCanBeFlat);
76 args.fFragBuilder->codeAppendf("%s = ", args.fOutputColor);
Brian Salomon87e9ddb2019-12-19 14:50:22 -050077 args.fFragBuilder->appendTextureLookupAndBlend(
Brian Salomon2a943df2018-05-04 13:43:19 -040078 args.fOutputColor,
Brian Salomon87e9ddb2019-12-19 14:50:22 -050079 SkBlendMode::kModulate,
Brian Salomon2a943df2018-05-04 13:43:19 -040080 args.fTexSamplers[0],
81 "clamp(textureCoords, textureDomain.xy, textureDomain.zw)",
Brian Salomon2a943df2018-05-04 13:43:19 -040082 &fColorSpaceXformHelper);
83 args.fFragBuilder->codeAppend(";");
84 args.fFragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage);
85 }
86 GrGLSLColorSpaceXformHelper fColorSpaceXformHelper;
87 };
88 return new GLSLProcessor;
89 }
90
91private:
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050092 friend class ::SkArenaAlloc; // for access to ctor
93
Greg Danieled96bca2019-12-05 15:05:54 -050094 LatticeGP(const GrSurfaceProxyView& view, sk_sp<GrColorSpaceXform> csxf,
Brian Osman0b537032018-12-26 12:16:44 -050095 GrSamplerState::Filter filter, bool wideColor)
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050096 : INHERITED(kLatticeGP_ClassID)
97 , fColorSpaceXform(std::move(csxf)) {
Greg Daniel7a82edf2018-12-04 10:54:34 -050098
Robert Phillips323471e2019-11-11 11:33:37 -050099 fSampler.reset(GrSamplerState(GrSamplerState::WrapMode::kClamp, filter),
Greg Danieled96bca2019-12-05 15:05:54 -0500100 view.proxy()->backendFormat(), view.swizzle());
Brian Salomonf7dcd762018-07-30 14:48:15 -0400101 this->setTextureSamplerCnt(1);
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500102 fInPosition = {"position", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
103 fInTextureCoords = {"textureCoords", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
104 fInTextureDomain = {"textureDomain", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
Brian Osman0b537032018-12-26 12:16:44 -0500105 fInColor = MakeColorAttribute("color", wideColor);
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500106 this->setVertexAttributes(&fInPosition, 4);
Brian Salomon92be2f72018-06-19 14:33:47 -0400107 }
108
Brian Salomonf7dcd762018-07-30 14:48:15 -0400109 const TextureSampler& onTextureSampler(int) const override { return fSampler; }
110
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500111 Attribute fInPosition;
112 Attribute fInTextureCoords;
113 Attribute fInTextureDomain;
114 Attribute fInColor;
Brian Salomon92be2f72018-06-19 14:33:47 -0400115
Brian Salomon2a943df2018-05-04 13:43:19 -0400116 sk_sp<GrColorSpaceXform> fColorSpaceXform;
117 TextureSampler fSampler;
118
119 typedef GrGeometryProcessor INHERITED;
120};
121
Brian Salomon815486c2017-07-11 08:52:13 -0400122class NonAALatticeOp final : public GrMeshDrawOp {
123private:
124 using Helper = GrSimpleMeshDrawOpHelper;
125
joshualitt33a5fce2015-11-18 13:28:51 -0800126public:
Brian Salomon25a88092016-12-01 09:36:50 -0500127 DEFINE_OP_CLASS_ID
joshualitt33a5fce2015-11-18 13:28:51 -0800128
Robert Phillipsb97da532019-02-12 15:24:12 -0500129 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400130 GrPaint&& paint,
131 const SkMatrix& viewMatrix,
Greg Danieled96bca2019-12-05 15:05:54 -0500132 GrSurfaceProxyView view,
Greg Daniel82c6b102020-01-21 10:33:22 -0500133 SkAlphaType alphaType,
Brian Salomon2a943df2018-05-04 13:43:19 -0400134 sk_sp<GrColorSpaceXform> colorSpaceXForm,
135 GrSamplerState::Filter filter,
Robert Phillips7c525e62018-06-12 10:11:12 -0400136 std::unique_ptr<SkLatticeIter> iter,
137 const SkRect& dst) {
Greg Danieled96bca2019-12-05 15:05:54 -0500138 SkASSERT(view.proxy());
Robert Phillips7c525e62018-06-12 10:11:12 -0400139 return Helper::FactoryHelper<NonAALatticeOp>(context, std::move(paint), viewMatrix,
Greg Daniel82c6b102020-01-21 10:33:22 -0500140 std::move(view), alphaType,
Brian Salomon2a943df2018-05-04 13:43:19 -0400141 std::move(colorSpaceXForm), filter,
142 std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -0400143 }
144
Brian Osmancf860852018-10-31 14:04:39 -0400145 NonAALatticeOp(Helper::MakeArgs& helperArgs, const SkPMColor4f& color,
Greg Danieled96bca2019-12-05 15:05:54 -0500146 const SkMatrix& viewMatrix, GrSurfaceProxyView view,
Greg Daniel82c6b102020-01-21 10:33:22 -0500147 SkAlphaType alphaType, sk_sp<GrColorSpaceXform> colorSpaceXform,
Greg Danielc594e622019-10-15 14:01:49 -0400148 GrSamplerState::Filter filter, std::unique_ptr<SkLatticeIter> iter,
149 const SkRect& dst)
Brian Salomon2a943df2018-05-04 13:43:19 -0400150 : INHERITED(ClassID())
151 , fHelper(helperArgs, GrAAType::kNone)
Greg Danieled96bca2019-12-05 15:05:54 -0500152 , fView(std::move(view))
Greg Daniel82c6b102020-01-21 10:33:22 -0500153 , fAlphaType(alphaType)
Brian Salomon2a943df2018-05-04 13:43:19 -0400154 , fColorSpaceXform(std::move(colorSpaceXform))
155 , fFilter(filter) {
bsalomona71b8982016-06-30 12:13:52 -0700156 Patch& patch = fPatches.push_back();
157 patch.fViewMatrix = viewMatrix;
158 patch.fColor = color;
msarett10e3d9b2016-08-18 15:46:03 -0700159 patch.fIter = std::move(iter);
bsalomona71b8982016-06-30 12:13:52 -0700160 patch.fDst = dst;
joshualitt33a5fce2015-11-18 13:28:51 -0800161
joshualitt33a5fce2015-11-18 13:28:51 -0800162 // setup bounds
Greg Daniel5faf4742019-10-01 15:14:44 -0400163 this->setTransformedBounds(patch.fDst, viewMatrix, HasAABloat::kNo, IsHairline::kNo);
joshualitt33a5fce2015-11-18 13:28:51 -0800164 }
165
Brian Salomonfc527d22016-12-14 21:07:01 -0500166 const char* name() const override { return "NonAALatticeOp"; }
robertphillips783a4da2015-11-19 14:00:02 -0800167
Chris Dalton1706cbf2019-05-21 19:35:29 -0600168 void visitProxies(const VisitProxyFunc& func) const override {
Chris Daltonbe457422020-03-16 18:05:03 -0600169 bool mipped = (GrSamplerState::Filter::kMipMap == fFilter);
170 func(fView.proxy(), GrMipMapped(mipped));
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 Osman9a390ac2018-11-12 09:47:48 -0500178#ifdef SK_DEBUG
robertphillips783a4da2015-11-19 14:00:02 -0800179 SkString dumpInfo() const override {
180 SkString str;
181
bsalomona71b8982016-06-30 12:13:52 -0700182 for (int i = 0; i < fPatches.count(); ++i) {
Brian Salomonfc527d22016-12-14 21:07:01 -0500183 str.appendf("%d: Color: 0x%08x Dst [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", i,
Brian Osmancf860852018-10-31 14:04:39 -0400184 fPatches[i].fColor.toBytes_RGBA(), fPatches[i].fDst.fLeft,
Brian Osman1be2b7c2018-10-29 16:07:15 -0400185 fPatches[i].fDst.fTop, fPatches[i].fDst.fRight, fPatches[i].fDst.fBottom);
robertphillips783a4da2015-11-19 14:00:02 -0800186 }
187
Brian Salomon815486c2017-07-11 08:52:13 -0400188 str += fHelper.dumpInfo();
189 str += INHERITED::dumpInfo();
robertphillips783a4da2015-11-19 14:00:02 -0800190 return str;
191 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500192#endif
joshualitt33a5fce2015-11-18 13:28:51 -0800193
Brian Salomon815486c2017-07-11 08:52:13 -0400194 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
195
Chris Dalton6ce447a2019-06-23 18:07:38 -0600196 GrProcessorSet::Analysis finalize(
197 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
198 GrClampType clampType) override {
Greg Daniel82c6b102020-01-21 10:33:22 -0500199 auto opaque = fPatches[0].fColor.isOpaque() && fAlphaType == kOpaque_SkAlphaType
Brian Salomon2a943df2018-05-04 13:43:19 -0400200 ? GrProcessorAnalysisColor::Opaque::kYes
201 : GrProcessorAnalysisColor::Opaque::kNo;
202 auto analysisColor = GrProcessorAnalysisColor(opaque);
Chris Dalton6ce447a2019-06-23 18:07:38 -0600203 auto result = fHelper.finalizeProcessors(
204 caps, clip, hasMixedSampledCoverage, clampType, GrProcessorAnalysisCoverage::kNone,
205 &analysisColor);
Brian Salomon2a943df2018-05-04 13:43:19 -0400206 analysisColor.isConstant(&fPatches[0].fColor);
Brian Osman2715bf52019-12-06 14:38:47 -0500207 fWideColor = !fPatches[0].fColor.fitsInBytes();
Brian Salomon2a943df2018-05-04 13:43:19 -0400208 return result;
Brian Salomon815486c2017-07-11 08:52:13 -0400209 }
210
Brian Salomon92aee3d2016-12-21 09:20:25 -0500211private:
Robert Phillips2669a7b2020-03-12 12:07:19 -0400212 GrProgramInfo* programInfo() override { return fProgramInfo; }
213
Robert Phillips4133dc42020-03-11 15:55:55 -0400214 void onCreateProgramInfo(const GrCaps* caps,
215 SkArenaAlloc* arena,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400216 const GrSurfaceProxyView* writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400217 GrAppliedClip&& appliedClip,
218 const GrXferProcessor::DstProxyView& dstProxyView) override {
Robert Phillipse37f1c42020-03-09 16:10:18 -0400219
220 auto gp = LatticeGP::Make(arena, fView, fColorSpaceXform, fFilter, fWideColor);
joshualitt33a5fce2015-11-18 13:28:51 -0800221 if (!gp) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400222 return;
Robert Phillipse37f1c42020-03-09 16:10:18 -0400223 }
224
Brian Salomon8afde5f2020-04-01 16:22:00 -0400225 fProgramInfo = GrSimpleMeshDrawOpHelper::CreateProgramInfo(caps, arena, writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400226 std::move(appliedClip),
227 dstProxyView, gp,
228 fHelper.detachProcessorSet(),
229 GrPrimitiveType::kTriangles,
230 fHelper.pipelineFlags(),
Chris Dalton765ed362020-03-16 17:34:44 -0600231 &GrUserStencilSettings::kUnused);
Robert Phillipse37f1c42020-03-09 16:10:18 -0400232 }
233
Robert Phillipse37f1c42020-03-09 16:10:18 -0400234 void onPrepareDraws(Target* target) override {
235 if (!fProgramInfo) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400236 this->createProgramInfo(target);
Robert Phillipse37f1c42020-03-09 16:10:18 -0400237 if (!fProgramInfo) {
238 return;
239 }
joshualitt33a5fce2015-11-18 13:28:51 -0800240 }
241
bsalomona71b8982016-06-30 12:13:52 -0700242 int patchCnt = fPatches.count();
msarett10e3d9b2016-08-18 15:46:03 -0700243 int numRects = 0;
244 for (int i = 0; i < patchCnt; i++) {
msarett0764efe2016-09-02 11:24:30 -0700245 numRects += fPatches[i].fIter->numRectsToDraw();
msarett10e3d9b2016-08-18 15:46:03 -0700246 }
joshualitt33a5fce2015-11-18 13:28:51 -0800247
Brian Salomon0db1b532017-07-12 15:21:43 -0400248 if (!numRects) {
249 return;
250 }
251
Robert Phillipse37f1c42020-03-09 16:10:18 -0400252 const size_t kVertexStride = fProgramInfo->primProc().vertexStride();
Robert Phillipse94cdd22019-11-04 14:15:58 -0500253
254 QuadHelper helper(target, kVertexStride, numRects);
255
Brian Osmanc3064e72018-11-15 08:59:22 -0500256 GrVertexWriter vertices{helper.vertices()};
Brian Salomon12d22642019-01-29 14:38:50 -0500257 if (!vertices.fPtr) {
joshualitt33a5fce2015-11-18 13:28:51 -0800258 SkDebugf("Could not allocate vertices\n");
259 return;
260 }
261
bsalomona71b8982016-06-30 12:13:52 -0700262 for (int i = 0; i < patchCnt; i++) {
msarett7fc08582016-08-18 14:29:22 -0700263 const Patch& patch = fPatches[i];
Brian Osmanc3064e72018-11-15 08:59:22 -0500264
Brian Osman0b537032018-12-26 12:16:44 -0500265 GrVertexColor patchColor(patch.fColor, fWideColor);
msarett10e3d9b2016-08-18 15:46:03 -0700266
267 // Apply the view matrix here if it is scale-translate. Otherwise, we need to
268 // wait until we've created the dst rects.
269 bool isScaleTranslate = patch.fViewMatrix.isScaleTranslate();
270 if (isScaleTranslate) {
271 patch.fIter->mapDstScaleTranslate(patch.fViewMatrix);
272 }
joshualitt33a5fce2015-11-18 13:28:51 -0800273
Brian Salomon2a943df2018-05-04 13:43:19 -0400274 SkIRect srcR;
275 SkRect dstR;
Brian Osmanc3064e72018-11-15 08:59:22 -0500276 SkPoint* patchPositions = reinterpret_cast<SkPoint*>(vertices.fPtr);
Greg Danieled96bca2019-12-05 15:05:54 -0500277 Sk4f scales(1.f / fView.proxy()->width(), 1.f / fView.proxy()->height(),
278 1.f / fView.proxy()->width(), 1.f / fView.proxy()->height());
Brian Salomon2a943df2018-05-04 13:43:19 -0400279 static const Sk4f kDomainOffsets(0.5f, 0.5f, -0.5f, -0.5f);
Brian Osmanc3064e72018-11-15 08:59:22 -0500280 static const Sk4f kFlipOffsets(0.f, 1.f, 0.f, 1.f);
Brian Salomon2a943df2018-05-04 13:43:19 -0400281 static const Sk4f kFlipMuls(1.f, -1.f, 1.f, -1.f);
msarett10e3d9b2016-08-18 15:46:03 -0700282 while (patch.fIter->next(&srcR, &dstR)) {
Brian Salomon2a943df2018-05-04 13:43:19 -0400283 Sk4f coords(SkIntToScalar(srcR.fLeft), SkIntToScalar(srcR.fTop),
284 SkIntToScalar(srcR.fRight), SkIntToScalar(srcR.fBottom));
285 Sk4f domain = coords + kDomainOffsets;
286 coords *= scales;
287 domain *= scales;
Greg Danieled96bca2019-12-05 15:05:54 -0500288 if (fView.origin() == kBottomLeft_GrSurfaceOrigin) {
Brian Salomon2a943df2018-05-04 13:43:19 -0400289 coords = kFlipMuls * coords + kFlipOffsets;
290 domain = SkNx_shuffle<0, 3, 2, 1>(kFlipMuls * domain + kFlipOffsets);
291 }
Brian Osman4486d982018-11-15 15:56:04 -0500292 SkRect texDomain;
293 SkRect texCoords;
294 domain.store(&texDomain);
295 coords.store(&texCoords);
joshualitt33a5fce2015-11-18 13:28:51 -0800296
Brian Osman0dd43022018-11-16 15:53:26 -0500297 vertices.writeQuad(GrVertexWriter::TriStripFromRect(dstR),
298 GrVertexWriter::TriStripFromRect(texCoords),
Brian Osmanc3064e72018-11-15 08:59:22 -0500299 texDomain,
300 patchColor);
joshualitt33a5fce2015-11-18 13:28:51 -0800301 }
msarett10e3d9b2016-08-18 15:46:03 -0700302
303 // If we didn't handle it above, apply the matrix here.
304 if (!isScaleTranslate) {
Robert Phillipsee08d522019-10-28 16:34:44 -0400305 SkMatrixPriv::MapPointsWithStride(
306 patch.fViewMatrix, patchPositions, kVertexStride,
307 GrResourceProvider::NumVertsPerNonAAQuad() * patch.fIter->numRectsToDraw());
msarett10e3d9b2016-08-18 15:46:03 -0700308 }
joshualitt33a5fce2015-11-18 13:28:51 -0800309 }
Robert Phillipse37f1c42020-03-09 16:10:18 -0400310
311 fMesh = helper.mesh();
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700312 }
313
314 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillipse37f1c42020-03-09 16:10:18 -0400315 if (!fProgramInfo || !fMesh) {
316 return;
317 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500318
Chris Dalton765ed362020-03-16 17:34:44 -0600319 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
320 flushState->bindTextures(fProgramInfo->primProc(), *fView.proxy(),
321 fProgramInfo->pipeline());
322 flushState->drawMesh(*fMesh);
joshualitt33a5fce2015-11-18 13:28:51 -0800323 }
324
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500325 CombineResult onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*,
326 const GrCaps& caps) override {
Brian Salomonfc527d22016-12-14 21:07:01 -0500327 NonAALatticeOp* that = t->cast<NonAALatticeOp>();
Greg Danieled96bca2019-12-05 15:05:54 -0500328 if (fView != that->fView) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000329 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400330 }
331 if (fFilter != that->fFilter) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000332 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400333 }
334 if (GrColorSpaceXform::Equals(fColorSpaceXform.get(), that->fColorSpaceXform.get())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000335 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400336 }
Brian Salomon815486c2017-07-11 08:52:13 -0400337 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000338 return CombineResult::kCannotCombine;
joshualitt33a5fce2015-11-18 13:28:51 -0800339 }
340
msarett10e3d9b2016-08-18 15:46:03 -0700341 fPatches.move_back_n(that->fPatches.count(), that->fPatches.begin());
Brian Osman0b537032018-12-26 12:16:44 -0500342 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000343 return CombineResult::kMerged;
joshualitt33a5fce2015-11-18 13:28:51 -0800344 }
345
bsalomona71b8982016-06-30 12:13:52 -0700346 struct Patch {
347 SkMatrix fViewMatrix;
msarett10e3d9b2016-08-18 15:46:03 -0700348 std::unique_ptr<SkLatticeIter> fIter;
bsalomona71b8982016-06-30 12:13:52 -0700349 SkRect fDst;
Brian Osmancf860852018-10-31 14:04:39 -0400350 SkPMColor4f fColor;
bsalomona71b8982016-06-30 12:13:52 -0700351 };
352
Brian Salomon815486c2017-07-11 08:52:13 -0400353 Helper fHelper;
354 SkSTArray<1, Patch, true> fPatches;
Greg Danieled96bca2019-12-05 15:05:54 -0500355 GrSurfaceProxyView fView;
Greg Daniel82c6b102020-01-21 10:33:22 -0500356 SkAlphaType fAlphaType;
Brian Salomon2a943df2018-05-04 13:43:19 -0400357 sk_sp<GrColorSpaceXform> fColorSpaceXform;
358 GrSamplerState::Filter fFilter;
Brian Osman0b537032018-12-26 12:16:44 -0500359 bool fWideColor;
joshualitt33a5fce2015-11-18 13:28:51 -0800360
Chris Daltoneb694b72020-03-16 09:25:50 -0600361 GrSimpleMesh* fMesh = nullptr;
Robert Phillipse37f1c42020-03-09 16:10:18 -0400362 GrProgramInfo* fProgramInfo = nullptr;
363
Brian Salomon815486c2017-07-11 08:52:13 -0400364 typedef GrMeshDrawOp INHERITED;
joshualitt33a5fce2015-11-18 13:28:51 -0800365};
366
Brian Salomon815486c2017-07-11 08:52:13 -0400367} // anonymous namespace
368
Brian Salomonfc527d22016-12-14 21:07:01 -0500369namespace GrLatticeOp {
Robert Phillipsb97da532019-02-12 15:24:12 -0500370std::unique_ptr<GrDrawOp> MakeNonAA(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400371 GrPaint&& paint,
372 const SkMatrix& viewMatrix,
Greg Danieled96bca2019-12-05 15:05:54 -0500373 GrSurfaceProxyView view,
Greg Daniel82c6b102020-01-21 10:33:22 -0500374 SkAlphaType alphaType,
Brian Salomon2a943df2018-05-04 13:43:19 -0400375 sk_sp<GrColorSpaceXform> colorSpaceXform,
376 GrSamplerState::Filter filter,
Robert Phillips7c525e62018-06-12 10:11:12 -0400377 std::unique_ptr<SkLatticeIter> iter,
378 const SkRect& dst) {
Greg Daniel82c6b102020-01-21 10:33:22 -0500379 return NonAALatticeOp::Make(context, std::move(paint), viewMatrix, std::move(view), alphaType,
380 std::move(colorSpaceXform), filter, std::move(iter), dst);
joshualitt33a5fce2015-11-18 13:28:51 -0800381}
382};
Brian Salomon815486c2017-07-11 08:52:13 -0400383
384#if GR_TEST_UTILS
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500385#include "src/gpu/GrProxyProvider.h"
386#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon815486c2017-07-11 08:52:13 -0400387
388/** Randomly divides subset into count divs. */
389static void init_random_divs(int divs[], int count, int subsetStart, int subsetStop,
390 SkRandom* random) {
391 // Rules for lattice divs: Must be strictly increasing and in the range
392 // [subsetStart, subsetStop).
393 // Not terribly efficient alg for generating random divs:
394 // 1) Start with minimum legal pixels between each div.
395 // 2) Randomly assign the remaining pixels of the subset to divs.
396 // 3) Convert from pixel counts to div offsets.
397
398 // 1) Initially each divs[i] represents the number of pixels between
399 // div i-1 and i. The initial div is allowed to be at subsetStart. There
400 // must be one pixel spacing between subsequent divs.
401 divs[0] = 0;
402 for (int i = 1; i < count; ++i) {
403 divs[i] = 1;
404 }
405 // 2) Assign the remaining subset pixels to fall
406 int subsetLength = subsetStop - subsetStart;
407 for (int i = 0; i < subsetLength - count; ++i) {
408 // +1 because count divs means count+1 intervals.
409 int entry = random->nextULessThan(count + 1);
410 // We don't have an entry to to store the count after the last div
411 if (entry < count) {
412 divs[entry]++;
413 }
414 }
415 // 3) Now convert the counts between divs to pixel indices, incorporating the subset's offset.
416 int offset = subsetStart;
417 for (int i = 0; i < count; ++i) {
418 divs[i] += offset;
419 offset = divs[i];
420 }
421}
422
423GR_DRAW_OP_TEST_DEFINE(NonAALatticeOp) {
Brian Salomon815486c2017-07-11 08:52:13 -0400424 SkCanvas::Lattice lattice;
Brian Salomon18df7632017-07-11 14:32:18 -0400425 // We loop because our random lattice code can produce an invalid lattice in the case where
426 // there is a single div separator in both x and y and both are aligned with the left and top
427 // edge of the image subset, respectively.
428 std::unique_ptr<int[]> xdivs;
429 std::unique_ptr<int[]> ydivs;
Stan Ilievca8c0952017-12-11 13:01:58 -0500430 std::unique_ptr<SkCanvas::Lattice::RectType[]> flags;
431 std::unique_ptr<SkColor[]> colors;
Brian Salomon18df7632017-07-11 14:32:18 -0400432 SkIRect subset;
Brian Salomona56a7462020-02-07 14:17:25 -0500433 SkISize dims;
434 dims.fWidth = random->nextRangeU(1, 1000);
435 dims.fHeight = random->nextRangeU(1, 1000);
Robert Phillips0a15cc62019-07-30 12:49:10 -0400436 GrSurfaceOrigin origin = random->nextBool() ? kTopLeft_GrSurfaceOrigin
437 : kBottomLeft_GrSurfaceOrigin;
Greg Daniel4065d452018-11-16 15:43:41 -0500438 const GrBackendFormat format =
Robert Phillips0a15cc62019-07-30 12:49:10 -0400439 context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
440 GrRenderable::kNo);
Greg Daniel47c20e82020-01-21 14:29:57 -0500441
Brian Salomonbeb7f522019-08-30 16:19:42 -0400442 auto proxy = context->priv().proxyProvider()->createProxy(format,
Brian Salomona56a7462020-02-07 14:17:25 -0500443 dims,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400444 GrRenderable::kNo,
445 1,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400446 GrMipMapped::kNo,
447 SkBackingFit::kExact,
448 SkBudgeted::kYes,
449 GrProtected::kNo);
Brian Salomon2a943df2018-05-04 13:43:19 -0400450
Brian Salomon18df7632017-07-11 14:32:18 -0400451 do {
Brian Salomon18df7632017-07-11 14:32:18 -0400452 if (random->nextBool()) {
Brian Salomona56a7462020-02-07 14:17:25 -0500453 subset.fLeft = random->nextULessThan(dims.fWidth);
454 subset.fRight = random->nextRangeU(subset.fLeft + 1, dims.fWidth);
455 subset.fTop = random->nextULessThan(dims.fHeight);
456 subset.fBottom = random->nextRangeU(subset.fTop + 1, dims.fHeight);
Brian Salomon18df7632017-07-11 14:32:18 -0400457 } else {
Brian Salomona56a7462020-02-07 14:17:25 -0500458 subset.setXYWH(0, 0, dims.fWidth, dims.fHeight);
Brian Salomon815486c2017-07-11 08:52:13 -0400459 }
Brian Salomon2a943df2018-05-04 13:43:19 -0400460 // SkCanvas::Lattice allows bounds to be null. However, SkCanvas creates a temp Lattice with
461 // a non-null bounds before creating a SkLatticeIter since SkLatticeIter requires a bounds.
Brian Salomon18df7632017-07-11 14:32:18 -0400462 lattice.fBounds = &subset;
463 lattice.fXCount = random->nextRangeU(1, subset.width());
464 lattice.fYCount = random->nextRangeU(1, subset.height());
465 xdivs.reset(new int[lattice.fXCount]);
466 ydivs.reset(new int[lattice.fYCount]);
467 init_random_divs(xdivs.get(), lattice.fXCount, subset.fLeft, subset.fRight, random);
468 init_random_divs(ydivs.get(), lattice.fYCount, subset.fTop, subset.fBottom, random);
469 lattice.fXDivs = xdivs.get();
470 lattice.fYDivs = ydivs.get();
471 bool hasFlags = random->nextBool();
472 if (hasFlags) {
473 int n = (lattice.fXCount + 1) * (lattice.fYCount + 1);
Stan Ilievca8c0952017-12-11 13:01:58 -0500474 flags.reset(new SkCanvas::Lattice::RectType[n]);
475 colors.reset(new SkColor[n]);
Brian Salomon18df7632017-07-11 14:32:18 -0400476 for (int i = 0; i < n; ++i) {
Stan Ilievca8c0952017-12-11 13:01:58 -0500477 flags[i] = random->nextBool() ? SkCanvas::Lattice::kTransparent
478 : SkCanvas::Lattice::kDefault;
Brian Salomon18df7632017-07-11 14:32:18 -0400479 }
Stan Ilievca8c0952017-12-11 13:01:58 -0500480 lattice.fRectTypes = flags.get();
481 lattice.fColors = colors.get();
Brian Salomon18df7632017-07-11 14:32:18 -0400482 } else {
Stan Ilievca8c0952017-12-11 13:01:58 -0500483 lattice.fRectTypes = nullptr;
484 lattice.fColors = nullptr;
Brian Salomon18df7632017-07-11 14:32:18 -0400485 }
Brian Salomona56a7462020-02-07 14:17:25 -0500486 } while (!SkLatticeIter::Valid(dims.fWidth, dims.fHeight, lattice));
Brian Salomon815486c2017-07-11 08:52:13 -0400487 SkRect dst;
488 dst.fLeft = random->nextRangeScalar(-2000.5f, 1000.f);
489 dst.fTop = random->nextRangeScalar(-2000.5f, 1000.f);
490 dst.fRight = dst.fLeft + random->nextRangeScalar(0.5f, 1000.f);
491 dst.fBottom = dst.fTop + random->nextRangeScalar(0.5f, 1000.f);
492 std::unique_ptr<SkLatticeIter> iter(new SkLatticeIter(lattice, dst));
493 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
Brian Salomon2a943df2018-05-04 13:43:19 -0400494 auto csxf = GrTest::TestColorXform(random);
495 GrSamplerState::Filter filter =
496 random->nextBool() ? GrSamplerState::Filter::kNearest : GrSamplerState::Filter::kBilerp;
Greg Danieled96bca2019-12-05 15:05:54 -0500497
498 GrSurfaceProxyView view(
499 std::move(proxy), origin,
Greg Daniel14b57212019-12-17 16:18:06 -0500500 context->priv().caps()->getReadSwizzle(format, GrColorType::kRGBA_8888));
Greg Danieled96bca2019-12-05 15:05:54 -0500501
502 return NonAALatticeOp::Make(context, std::move(paint), viewMatrix, std::move(view),
Greg Daniel82c6b102020-01-21 10:33:22 -0500503 kPremul_SkAlphaType, std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -0400504}
505
506#endif