blob: 78fc2a3c6ccaed0a38baf065ae8294e5b5a1b3fe [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>();
Brian Salomonc241b582019-11-27 08:57:17 -050052 this->setTransformDataHelper(SkMatrix::I(), 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());
Brian Salomon2a943df2018-05-04 13:43:19 -040065 this->emitTransforms(args.fVertBuilder,
66 args.fVaryingHandler,
67 args.fUniformHandler,
Brian Osmanf04fb3c2018-11-12 15:34:00 -050068 latticeGP.fInTextureCoords.asShaderVar(),
Brian Salomon2a943df2018-05-04 13:43:19 -040069 args.fFPCoordTransformHandler);
70 args.fFragBuilder->codeAppend("float2 textureCoords;");
Brian Osmanf04fb3c2018-11-12 15:34:00 -050071 args.fVaryingHandler->addPassThroughAttribute(latticeGP.fInTextureCoords,
Brian Salomon2a943df2018-05-04 13:43:19 -040072 "textureCoords");
73 args.fFragBuilder->codeAppend("float4 textureDomain;");
74 args.fVaryingHandler->addPassThroughAttribute(
Brian Osmanf04fb3c2018-11-12 15:34:00 -050075 latticeGP.fInTextureDomain, "textureDomain", Interpolation::kCanBeFlat);
76 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(";");
87 args.fFragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage);
88 }
89 GrGLSLColorSpaceXformHelper fColorSpaceXformHelper;
90 };
91 return new GLSLProcessor;
92 }
93
94private:
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050095 friend class ::SkArenaAlloc; // for access to ctor
96
Greg Danieled96bca2019-12-05 15:05:54 -050097 LatticeGP(const GrSurfaceProxyView& view, sk_sp<GrColorSpaceXform> csxf,
Brian Osman0b537032018-12-26 12:16:44 -050098 GrSamplerState::Filter filter, bool wideColor)
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050099 : INHERITED(kLatticeGP_ClassID)
100 , fColorSpaceXform(std::move(csxf)) {
Greg Daniel7a82edf2018-12-04 10:54:34 -0500101
Robert Phillips323471e2019-11-11 11:33:37 -0500102 fSampler.reset(GrSamplerState(GrSamplerState::WrapMode::kClamp, filter),
Greg Danieled96bca2019-12-05 15:05:54 -0500103 view.proxy()->backendFormat(), view.swizzle());
Brian Salomonf7dcd762018-07-30 14:48:15 -0400104 this->setTextureSamplerCnt(1);
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500105 fInPosition = {"position", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
106 fInTextureCoords = {"textureCoords", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
107 fInTextureDomain = {"textureDomain", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
Brian Osman0b537032018-12-26 12:16:44 -0500108 fInColor = MakeColorAttribute("color", wideColor);
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500109 this->setVertexAttributes(&fInPosition, 4);
Brian Salomon92be2f72018-06-19 14:33:47 -0400110 }
111
Brian Salomonf7dcd762018-07-30 14:48:15 -0400112 const TextureSampler& onTextureSampler(int) const override { return fSampler; }
113
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500114 Attribute fInPosition;
115 Attribute fInTextureCoords;
116 Attribute fInTextureDomain;
117 Attribute fInColor;
Brian Salomon92be2f72018-06-19 14:33:47 -0400118
Brian Salomon2a943df2018-05-04 13:43:19 -0400119 sk_sp<GrColorSpaceXform> fColorSpaceXform;
120 TextureSampler fSampler;
121
122 typedef GrGeometryProcessor INHERITED;
123};
124
Brian Salomon815486c2017-07-11 08:52:13 -0400125class NonAALatticeOp final : public GrMeshDrawOp {
126private:
127 using Helper = GrSimpleMeshDrawOpHelper;
128
joshualitt33a5fce2015-11-18 13:28:51 -0800129public:
Brian Salomon25a88092016-12-01 09:36:50 -0500130 DEFINE_OP_CLASS_ID
joshualitt33a5fce2015-11-18 13:28:51 -0800131
Robert Phillipsb97da532019-02-12 15:24:12 -0500132 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400133 GrPaint&& paint,
134 const SkMatrix& viewMatrix,
Greg Danieled96bca2019-12-05 15:05:54 -0500135 GrSurfaceProxyView view,
Greg Daniel82c6b102020-01-21 10:33:22 -0500136 SkAlphaType alphaType,
Brian Salomon2a943df2018-05-04 13:43:19 -0400137 sk_sp<GrColorSpaceXform> colorSpaceXForm,
138 GrSamplerState::Filter filter,
Robert Phillips7c525e62018-06-12 10:11:12 -0400139 std::unique_ptr<SkLatticeIter> iter,
140 const SkRect& dst) {
Greg Danieled96bca2019-12-05 15:05:54 -0500141 SkASSERT(view.proxy());
Robert Phillips7c525e62018-06-12 10:11:12 -0400142 return Helper::FactoryHelper<NonAALatticeOp>(context, std::move(paint), viewMatrix,
Greg Daniel82c6b102020-01-21 10:33:22 -0500143 std::move(view), alphaType,
Brian Salomon2a943df2018-05-04 13:43:19 -0400144 std::move(colorSpaceXForm), filter,
145 std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -0400146 }
147
Brian Osmancf860852018-10-31 14:04:39 -0400148 NonAALatticeOp(Helper::MakeArgs& helperArgs, const SkPMColor4f& color,
Greg Danieled96bca2019-12-05 15:05:54 -0500149 const SkMatrix& viewMatrix, GrSurfaceProxyView view,
Greg Daniel82c6b102020-01-21 10:33:22 -0500150 SkAlphaType alphaType, sk_sp<GrColorSpaceXform> colorSpaceXform,
Greg Danielc594e622019-10-15 14:01:49 -0400151 GrSamplerState::Filter filter, std::unique_ptr<SkLatticeIter> iter,
152 const SkRect& dst)
Brian Salomon2a943df2018-05-04 13:43:19 -0400153 : INHERITED(ClassID())
154 , fHelper(helperArgs, GrAAType::kNone)
Greg Danieled96bca2019-12-05 15:05:54 -0500155 , fView(std::move(view))
Greg Daniel82c6b102020-01-21 10:33:22 -0500156 , fAlphaType(alphaType)
Brian Salomon2a943df2018-05-04 13:43:19 -0400157 , fColorSpaceXform(std::move(colorSpaceXform))
158 , fFilter(filter) {
bsalomona71b8982016-06-30 12:13:52 -0700159 Patch& patch = fPatches.push_back();
160 patch.fViewMatrix = viewMatrix;
161 patch.fColor = color;
msarett10e3d9b2016-08-18 15:46:03 -0700162 patch.fIter = std::move(iter);
bsalomona71b8982016-06-30 12:13:52 -0700163 patch.fDst = dst;
joshualitt33a5fce2015-11-18 13:28:51 -0800164
joshualitt33a5fce2015-11-18 13:28:51 -0800165 // setup bounds
Greg Daniel5faf4742019-10-01 15:14:44 -0400166 this->setTransformedBounds(patch.fDst, viewMatrix, HasAABloat::kNo, IsHairline::kNo);
joshualitt33a5fce2015-11-18 13:28:51 -0800167 }
168
Brian Salomonfc527d22016-12-14 21:07:01 -0500169 const char* name() const override { return "NonAALatticeOp"; }
robertphillips783a4da2015-11-19 14:00:02 -0800170
Chris Dalton1706cbf2019-05-21 19:35:29 -0600171 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipse37f1c42020-03-09 16:10:18 -0400172 if (fProgramInfo) {
173 fProgramInfo->visitProxies(func);
174 } else {
175 bool mipped = (GrSamplerState::Filter::kMipMap == fFilter);
176 func(fView.proxy(), GrMipMapped(mipped));
177 fHelper.visitProxies(func);
178 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400179 }
180
Brian Osman9a390ac2018-11-12 09:47:48 -0500181#ifdef SK_DEBUG
robertphillips783a4da2015-11-19 14:00:02 -0800182 SkString dumpInfo() const override {
183 SkString str;
184
bsalomona71b8982016-06-30 12:13:52 -0700185 for (int i = 0; i < fPatches.count(); ++i) {
Brian Salomonfc527d22016-12-14 21:07:01 -0500186 str.appendf("%d: Color: 0x%08x Dst [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", i,
Brian Osmancf860852018-10-31 14:04:39 -0400187 fPatches[i].fColor.toBytes_RGBA(), fPatches[i].fDst.fLeft,
Brian Osman1be2b7c2018-10-29 16:07:15 -0400188 fPatches[i].fDst.fTop, fPatches[i].fDst.fRight, fPatches[i].fDst.fBottom);
robertphillips783a4da2015-11-19 14:00:02 -0800189 }
190
Brian Salomon815486c2017-07-11 08:52:13 -0400191 str += fHelper.dumpInfo();
192 str += INHERITED::dumpInfo();
robertphillips783a4da2015-11-19 14:00:02 -0800193 return str;
194 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500195#endif
joshualitt33a5fce2015-11-18 13:28:51 -0800196
Brian Salomon815486c2017-07-11 08:52:13 -0400197 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
198
Chris Dalton6ce447a2019-06-23 18:07:38 -0600199 GrProcessorSet::Analysis finalize(
200 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
201 GrClampType clampType) override {
Greg Daniel82c6b102020-01-21 10:33:22 -0500202 auto opaque = fPatches[0].fColor.isOpaque() && fAlphaType == kOpaque_SkAlphaType
Brian Salomon2a943df2018-05-04 13:43:19 -0400203 ? GrProcessorAnalysisColor::Opaque::kYes
204 : GrProcessorAnalysisColor::Opaque::kNo;
205 auto analysisColor = GrProcessorAnalysisColor(opaque);
Chris Dalton6ce447a2019-06-23 18:07:38 -0600206 auto result = fHelper.finalizeProcessors(
207 caps, clip, hasMixedSampledCoverage, clampType, GrProcessorAnalysisCoverage::kNone,
208 &analysisColor);
Brian Salomon2a943df2018-05-04 13:43:19 -0400209 analysisColor.isConstant(&fPatches[0].fColor);
Brian Osman2715bf52019-12-06 14:38:47 -0500210 fWideColor = !fPatches[0].fColor.fitsInBytes();
Brian Salomon2a943df2018-05-04 13:43:19 -0400211 return result;
Brian Salomon815486c2017-07-11 08:52:13 -0400212 }
213
Brian Salomon92aee3d2016-12-21 09:20:25 -0500214private:
Robert Phillips4133dc42020-03-11 15:55:55 -0400215 void onCreateProgramInfo(const GrCaps* caps,
216 SkArenaAlloc* arena,
217 const GrSurfaceProxyView* outputView,
218 GrAppliedClip&& appliedClip,
219 const GrXferProcessor::DstProxyView& dstProxyView) override {
Robert Phillipse37f1c42020-03-09 16:10:18 -0400220
221 auto gp = LatticeGP::Make(arena, fView, fColorSpaceXform, fFilter, fWideColor);
joshualitt33a5fce2015-11-18 13:28:51 -0800222 if (!gp) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400223 return;
Robert Phillipse37f1c42020-03-09 16:10:18 -0400224 }
225
226 static constexpr int kOnePrimProcTexture = 1;
227 auto fixedDynamicState = GrMeshDrawOp::Target::MakeFixedDynamicState(arena, &appliedClip,
228 kOnePrimProcTexture);
229 fixedDynamicState->fPrimitiveProcessorTextures[0] = fView.proxy();
230
Robert Phillips4133dc42020-03-11 15:55:55 -0400231 fProgramInfo = GrSimpleMeshDrawOpHelper::CreateProgramInfo(caps, arena, outputView,
232 std::move(appliedClip),
233 dstProxyView, gp,
234 fHelper.detachProcessorSet(),
235 GrPrimitiveType::kTriangles,
236 fHelper.pipelineFlags(),
237 &GrUserStencilSettings::kUnused,
238 fixedDynamicState);
Robert Phillipse37f1c42020-03-09 16:10:18 -0400239 }
240
241 void onPrePrepareDraws(GrRecordingContext* context,
242 const GrSurfaceProxyView* outputView,
243 GrAppliedClip* clip,
244 const GrXferProcessor::DstProxyView& dstProxyView) override {
245 SkArenaAlloc* arena = context->priv().recordTimeAllocator();
246
247 // This is equivalent to a GrOpFlushState::detachAppliedClip
248 GrAppliedClip appliedClip = clip ? std::move(*clip) : GrAppliedClip();
249
Robert Phillips4133dc42020-03-11 15:55:55 -0400250 this->createProgramInfo(context->priv().caps(), arena, outputView,
251 std::move(appliedClip), dstProxyView);
Robert Phillipse37f1c42020-03-09 16:10:18 -0400252
253 context->priv().recordProgramInfo(fProgramInfo);
254 }
255
256 void onPrepareDraws(Target* target) override {
257 if (!fProgramInfo) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400258 this->createProgramInfo(target);
Robert Phillipse37f1c42020-03-09 16:10:18 -0400259 if (!fProgramInfo) {
260 return;
261 }
joshualitt33a5fce2015-11-18 13:28:51 -0800262 }
263
bsalomona71b8982016-06-30 12:13:52 -0700264 int patchCnt = fPatches.count();
msarett10e3d9b2016-08-18 15:46:03 -0700265 int numRects = 0;
266 for (int i = 0; i < patchCnt; i++) {
msarett0764efe2016-09-02 11:24:30 -0700267 numRects += fPatches[i].fIter->numRectsToDraw();
msarett10e3d9b2016-08-18 15:46:03 -0700268 }
joshualitt33a5fce2015-11-18 13:28:51 -0800269
Brian Salomon0db1b532017-07-12 15:21:43 -0400270 if (!numRects) {
271 return;
272 }
273
Robert Phillipse37f1c42020-03-09 16:10:18 -0400274 const size_t kVertexStride = fProgramInfo->primProc().vertexStride();
Robert Phillipse94cdd22019-11-04 14:15:58 -0500275
276 QuadHelper helper(target, kVertexStride, numRects);
277
Brian Osmanc3064e72018-11-15 08:59:22 -0500278 GrVertexWriter vertices{helper.vertices()};
Brian Salomon12d22642019-01-29 14:38:50 -0500279 if (!vertices.fPtr) {
joshualitt33a5fce2015-11-18 13:28:51 -0800280 SkDebugf("Could not allocate vertices\n");
281 return;
282 }
283
bsalomona71b8982016-06-30 12:13:52 -0700284 for (int i = 0; i < patchCnt; i++) {
msarett7fc08582016-08-18 14:29:22 -0700285 const Patch& patch = fPatches[i];
Brian Osmanc3064e72018-11-15 08:59:22 -0500286
Brian Osman0b537032018-12-26 12:16:44 -0500287 GrVertexColor patchColor(patch.fColor, fWideColor);
msarett10e3d9b2016-08-18 15:46:03 -0700288
289 // Apply the view matrix here if it is scale-translate. Otherwise, we need to
290 // wait until we've created the dst rects.
291 bool isScaleTranslate = patch.fViewMatrix.isScaleTranslate();
292 if (isScaleTranslate) {
293 patch.fIter->mapDstScaleTranslate(patch.fViewMatrix);
294 }
joshualitt33a5fce2015-11-18 13:28:51 -0800295
Brian Salomon2a943df2018-05-04 13:43:19 -0400296 SkIRect srcR;
297 SkRect dstR;
Brian Osmanc3064e72018-11-15 08:59:22 -0500298 SkPoint* patchPositions = reinterpret_cast<SkPoint*>(vertices.fPtr);
Greg Danieled96bca2019-12-05 15:05:54 -0500299 Sk4f scales(1.f / fView.proxy()->width(), 1.f / fView.proxy()->height(),
300 1.f / fView.proxy()->width(), 1.f / fView.proxy()->height());
Brian Salomon2a943df2018-05-04 13:43:19 -0400301 static const Sk4f kDomainOffsets(0.5f, 0.5f, -0.5f, -0.5f);
Brian Osmanc3064e72018-11-15 08:59:22 -0500302 static const Sk4f kFlipOffsets(0.f, 1.f, 0.f, 1.f);
Brian Salomon2a943df2018-05-04 13:43:19 -0400303 static const Sk4f kFlipMuls(1.f, -1.f, 1.f, -1.f);
msarett10e3d9b2016-08-18 15:46:03 -0700304 while (patch.fIter->next(&srcR, &dstR)) {
Brian Salomon2a943df2018-05-04 13:43:19 -0400305 Sk4f coords(SkIntToScalar(srcR.fLeft), SkIntToScalar(srcR.fTop),
306 SkIntToScalar(srcR.fRight), SkIntToScalar(srcR.fBottom));
307 Sk4f domain = coords + kDomainOffsets;
308 coords *= scales;
309 domain *= scales;
Greg Danieled96bca2019-12-05 15:05:54 -0500310 if (fView.origin() == kBottomLeft_GrSurfaceOrigin) {
Brian Salomon2a943df2018-05-04 13:43:19 -0400311 coords = kFlipMuls * coords + kFlipOffsets;
312 domain = SkNx_shuffle<0, 3, 2, 1>(kFlipMuls * domain + kFlipOffsets);
313 }
Brian Osman4486d982018-11-15 15:56:04 -0500314 SkRect texDomain;
315 SkRect texCoords;
316 domain.store(&texDomain);
317 coords.store(&texCoords);
joshualitt33a5fce2015-11-18 13:28:51 -0800318
Brian Osman0dd43022018-11-16 15:53:26 -0500319 vertices.writeQuad(GrVertexWriter::TriStripFromRect(dstR),
320 GrVertexWriter::TriStripFromRect(texCoords),
Brian Osmanc3064e72018-11-15 08:59:22 -0500321 texDomain,
322 patchColor);
joshualitt33a5fce2015-11-18 13:28:51 -0800323 }
msarett10e3d9b2016-08-18 15:46:03 -0700324
325 // If we didn't handle it above, apply the matrix here.
326 if (!isScaleTranslate) {
Robert Phillipsee08d522019-10-28 16:34:44 -0400327 SkMatrixPriv::MapPointsWithStride(
328 patch.fViewMatrix, patchPositions, kVertexStride,
329 GrResourceProvider::NumVertsPerNonAAQuad() * patch.fIter->numRectsToDraw());
msarett10e3d9b2016-08-18 15:46:03 -0700330 }
joshualitt33a5fce2015-11-18 13:28:51 -0800331 }
Robert Phillipse37f1c42020-03-09 16:10:18 -0400332
333 fMesh = helper.mesh();
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700334 }
335
336 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillipse37f1c42020-03-09 16:10:18 -0400337 if (!fProgramInfo || !fMesh) {
338 return;
339 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500340
Robert Phillipse37f1c42020-03-09 16:10:18 -0400341 flushState->opsRenderPass()->bindPipeline(*fProgramInfo, chainBounds);
342 flushState->opsRenderPass()->drawMeshes(*fProgramInfo, fMesh, 1);
joshualitt33a5fce2015-11-18 13:28:51 -0800343 }
344
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500345 CombineResult onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*,
346 const GrCaps& caps) override {
Brian Salomonfc527d22016-12-14 21:07:01 -0500347 NonAALatticeOp* that = t->cast<NonAALatticeOp>();
Greg Danieled96bca2019-12-05 15:05:54 -0500348 if (fView != that->fView) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000349 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400350 }
351 if (fFilter != that->fFilter) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000352 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400353 }
354 if (GrColorSpaceXform::Equals(fColorSpaceXform.get(), that->fColorSpaceXform.get())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000355 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400356 }
Brian Salomon815486c2017-07-11 08:52:13 -0400357 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000358 return CombineResult::kCannotCombine;
joshualitt33a5fce2015-11-18 13:28:51 -0800359 }
360
msarett10e3d9b2016-08-18 15:46:03 -0700361 fPatches.move_back_n(that->fPatches.count(), that->fPatches.begin());
Brian Osman0b537032018-12-26 12:16:44 -0500362 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000363 return CombineResult::kMerged;
joshualitt33a5fce2015-11-18 13:28:51 -0800364 }
365
bsalomona71b8982016-06-30 12:13:52 -0700366 struct Patch {
367 SkMatrix fViewMatrix;
msarett10e3d9b2016-08-18 15:46:03 -0700368 std::unique_ptr<SkLatticeIter> fIter;
bsalomona71b8982016-06-30 12:13:52 -0700369 SkRect fDst;
Brian Osmancf860852018-10-31 14:04:39 -0400370 SkPMColor4f fColor;
bsalomona71b8982016-06-30 12:13:52 -0700371 };
372
Brian Salomon815486c2017-07-11 08:52:13 -0400373 Helper fHelper;
374 SkSTArray<1, Patch, true> fPatches;
Greg Danieled96bca2019-12-05 15:05:54 -0500375 GrSurfaceProxyView fView;
Greg Daniel82c6b102020-01-21 10:33:22 -0500376 SkAlphaType fAlphaType;
Brian Salomon2a943df2018-05-04 13:43:19 -0400377 sk_sp<GrColorSpaceXform> fColorSpaceXform;
378 GrSamplerState::Filter fFilter;
Brian Osman0b537032018-12-26 12:16:44 -0500379 bool fWideColor;
joshualitt33a5fce2015-11-18 13:28:51 -0800380
Robert Phillipse37f1c42020-03-09 16:10:18 -0400381 GrMesh* fMesh = nullptr;
382 GrProgramInfo* fProgramInfo = nullptr;
383
Brian Salomon815486c2017-07-11 08:52:13 -0400384 typedef GrMeshDrawOp INHERITED;
joshualitt33a5fce2015-11-18 13:28:51 -0800385};
386
Brian Salomon815486c2017-07-11 08:52:13 -0400387} // anonymous namespace
388
Brian Salomonfc527d22016-12-14 21:07:01 -0500389namespace GrLatticeOp {
Robert Phillipsb97da532019-02-12 15:24:12 -0500390std::unique_ptr<GrDrawOp> MakeNonAA(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400391 GrPaint&& paint,
392 const SkMatrix& viewMatrix,
Greg Danieled96bca2019-12-05 15:05:54 -0500393 GrSurfaceProxyView view,
Greg Daniel82c6b102020-01-21 10:33:22 -0500394 SkAlphaType alphaType,
Brian Salomon2a943df2018-05-04 13:43:19 -0400395 sk_sp<GrColorSpaceXform> colorSpaceXform,
396 GrSamplerState::Filter filter,
Robert Phillips7c525e62018-06-12 10:11:12 -0400397 std::unique_ptr<SkLatticeIter> iter,
398 const SkRect& dst) {
Greg Daniel82c6b102020-01-21 10:33:22 -0500399 return NonAALatticeOp::Make(context, std::move(paint), viewMatrix, std::move(view), alphaType,
400 std::move(colorSpaceXform), filter, std::move(iter), dst);
joshualitt33a5fce2015-11-18 13:28:51 -0800401}
402};
Brian Salomon815486c2017-07-11 08:52:13 -0400403
404#if GR_TEST_UTILS
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500405#include "src/gpu/GrProxyProvider.h"
406#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon815486c2017-07-11 08:52:13 -0400407
408/** Randomly divides subset into count divs. */
409static void init_random_divs(int divs[], int count, int subsetStart, int subsetStop,
410 SkRandom* random) {
411 // Rules for lattice divs: Must be strictly increasing and in the range
412 // [subsetStart, subsetStop).
413 // Not terribly efficient alg for generating random divs:
414 // 1) Start with minimum legal pixels between each div.
415 // 2) Randomly assign the remaining pixels of the subset to divs.
416 // 3) Convert from pixel counts to div offsets.
417
418 // 1) Initially each divs[i] represents the number of pixels between
419 // div i-1 and i. The initial div is allowed to be at subsetStart. There
420 // must be one pixel spacing between subsequent divs.
421 divs[0] = 0;
422 for (int i = 1; i < count; ++i) {
423 divs[i] = 1;
424 }
425 // 2) Assign the remaining subset pixels to fall
426 int subsetLength = subsetStop - subsetStart;
427 for (int i = 0; i < subsetLength - count; ++i) {
428 // +1 because count divs means count+1 intervals.
429 int entry = random->nextULessThan(count + 1);
430 // We don't have an entry to to store the count after the last div
431 if (entry < count) {
432 divs[entry]++;
433 }
434 }
435 // 3) Now convert the counts between divs to pixel indices, incorporating the subset's offset.
436 int offset = subsetStart;
437 for (int i = 0; i < count; ++i) {
438 divs[i] += offset;
439 offset = divs[i];
440 }
441}
442
443GR_DRAW_OP_TEST_DEFINE(NonAALatticeOp) {
Brian Salomon815486c2017-07-11 08:52:13 -0400444 SkCanvas::Lattice lattice;
Brian Salomon18df7632017-07-11 14:32:18 -0400445 // We loop because our random lattice code can produce an invalid lattice in the case where
446 // there is a single div separator in both x and y and both are aligned with the left and top
447 // edge of the image subset, respectively.
448 std::unique_ptr<int[]> xdivs;
449 std::unique_ptr<int[]> ydivs;
Stan Ilievca8c0952017-12-11 13:01:58 -0500450 std::unique_ptr<SkCanvas::Lattice::RectType[]> flags;
451 std::unique_ptr<SkColor[]> colors;
Brian Salomon18df7632017-07-11 14:32:18 -0400452 SkIRect subset;
Brian Salomona56a7462020-02-07 14:17:25 -0500453 SkISize dims;
454 dims.fWidth = random->nextRangeU(1, 1000);
455 dims.fHeight = random->nextRangeU(1, 1000);
Robert Phillips0a15cc62019-07-30 12:49:10 -0400456 GrSurfaceOrigin origin = random->nextBool() ? kTopLeft_GrSurfaceOrigin
457 : kBottomLeft_GrSurfaceOrigin;
Greg Daniel4065d452018-11-16 15:43:41 -0500458 const GrBackendFormat format =
Robert Phillips0a15cc62019-07-30 12:49:10 -0400459 context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
460 GrRenderable::kNo);
Greg Daniel47c20e82020-01-21 14:29:57 -0500461 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(format, GrColorType::kRGBA_8888);
462
Brian Salomonbeb7f522019-08-30 16:19:42 -0400463 auto proxy = context->priv().proxyProvider()->createProxy(format,
Brian Salomona56a7462020-02-07 14:17:25 -0500464 dims,
Greg Daniel47c20e82020-01-21 14:29:57 -0500465 swizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400466 GrRenderable::kNo,
467 1,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400468 GrMipMapped::kNo,
469 SkBackingFit::kExact,
470 SkBudgeted::kYes,
471 GrProtected::kNo);
Brian Salomon2a943df2018-05-04 13:43:19 -0400472
Brian Salomon18df7632017-07-11 14:32:18 -0400473 do {
Brian Salomon18df7632017-07-11 14:32:18 -0400474 if (random->nextBool()) {
Brian Salomona56a7462020-02-07 14:17:25 -0500475 subset.fLeft = random->nextULessThan(dims.fWidth);
476 subset.fRight = random->nextRangeU(subset.fLeft + 1, dims.fWidth);
477 subset.fTop = random->nextULessThan(dims.fHeight);
478 subset.fBottom = random->nextRangeU(subset.fTop + 1, dims.fHeight);
Brian Salomon18df7632017-07-11 14:32:18 -0400479 } else {
Brian Salomona56a7462020-02-07 14:17:25 -0500480 subset.setXYWH(0, 0, dims.fWidth, dims.fHeight);
Brian Salomon815486c2017-07-11 08:52:13 -0400481 }
Brian Salomon2a943df2018-05-04 13:43:19 -0400482 // SkCanvas::Lattice allows bounds to be null. However, SkCanvas creates a temp Lattice with
483 // a non-null bounds before creating a SkLatticeIter since SkLatticeIter requires a bounds.
Brian Salomon18df7632017-07-11 14:32:18 -0400484 lattice.fBounds = &subset;
485 lattice.fXCount = random->nextRangeU(1, subset.width());
486 lattice.fYCount = random->nextRangeU(1, subset.height());
487 xdivs.reset(new int[lattice.fXCount]);
488 ydivs.reset(new int[lattice.fYCount]);
489 init_random_divs(xdivs.get(), lattice.fXCount, subset.fLeft, subset.fRight, random);
490 init_random_divs(ydivs.get(), lattice.fYCount, subset.fTop, subset.fBottom, random);
491 lattice.fXDivs = xdivs.get();
492 lattice.fYDivs = ydivs.get();
493 bool hasFlags = random->nextBool();
494 if (hasFlags) {
495 int n = (lattice.fXCount + 1) * (lattice.fYCount + 1);
Stan Ilievca8c0952017-12-11 13:01:58 -0500496 flags.reset(new SkCanvas::Lattice::RectType[n]);
497 colors.reset(new SkColor[n]);
Brian Salomon18df7632017-07-11 14:32:18 -0400498 for (int i = 0; i < n; ++i) {
Stan Ilievca8c0952017-12-11 13:01:58 -0500499 flags[i] = random->nextBool() ? SkCanvas::Lattice::kTransparent
500 : SkCanvas::Lattice::kDefault;
Brian Salomon18df7632017-07-11 14:32:18 -0400501 }
Stan Ilievca8c0952017-12-11 13:01:58 -0500502 lattice.fRectTypes = flags.get();
503 lattice.fColors = colors.get();
Brian Salomon18df7632017-07-11 14:32:18 -0400504 } else {
Stan Ilievca8c0952017-12-11 13:01:58 -0500505 lattice.fRectTypes = nullptr;
506 lattice.fColors = nullptr;
Brian Salomon18df7632017-07-11 14:32:18 -0400507 }
Brian Salomona56a7462020-02-07 14:17:25 -0500508 } while (!SkLatticeIter::Valid(dims.fWidth, dims.fHeight, lattice));
Brian Salomon815486c2017-07-11 08:52:13 -0400509 SkRect dst;
510 dst.fLeft = random->nextRangeScalar(-2000.5f, 1000.f);
511 dst.fTop = random->nextRangeScalar(-2000.5f, 1000.f);
512 dst.fRight = dst.fLeft + random->nextRangeScalar(0.5f, 1000.f);
513 dst.fBottom = dst.fTop + random->nextRangeScalar(0.5f, 1000.f);
514 std::unique_ptr<SkLatticeIter> iter(new SkLatticeIter(lattice, dst));
515 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
Brian Salomon2a943df2018-05-04 13:43:19 -0400516 auto csxf = GrTest::TestColorXform(random);
517 GrSamplerState::Filter filter =
518 random->nextBool() ? GrSamplerState::Filter::kNearest : GrSamplerState::Filter::kBilerp;
Greg Danieled96bca2019-12-05 15:05:54 -0500519
520 GrSurfaceProxyView view(
521 std::move(proxy), origin,
Greg Daniel14b57212019-12-17 16:18:06 -0500522 context->priv().caps()->getReadSwizzle(format, GrColorType::kRGBA_8888));
Greg Danieled96bca2019-12-05 15:05:54 -0500523
524 return NonAALatticeOp::Make(context, std::move(paint), viewMatrix, std::move(view),
Greg Daniel82c6b102020-01-21 10:33:22 -0500525 kPremul_SkAlphaType, std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -0400526}
527
528#endif