blob: 4babb823869a134c2b3a0dffbc5b8c8a1b360d84 [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"
12#include "src/gpu/GrDefaultGeoProcFactory.h"
13#include "src/gpu/GrDrawOpTest.h"
14#include "src/gpu/GrGpu.h"
15#include "src/gpu/GrOpFlushState.h"
Robert Phillipse37f1c42020-03-09 16:10:18 -040016#include "src/gpu/GrProgramInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/GrResourceProvider.h"
18#include "src/gpu/GrResourceProviderPriv.h"
19#include "src/gpu/GrVertexWriter.h"
20#include "src/gpu/SkGr.h"
21#include "src/gpu/glsl/GrGLSLColorSpaceXformHelper.h"
Robert Phillips03e4c952019-11-26 16:20:22 -050022#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
24#include "src/gpu/glsl/GrGLSLVarying.h"
25#include "src/gpu/ops/GrLatticeOp.h"
26#include "src/gpu/ops/GrMeshDrawOp.h"
27#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
joshualitt33a5fce2015-11-18 13:28:51 -080028
Brian Salomon815486c2017-07-11 08:52:13 -040029namespace {
30
Brian Salomon2a943df2018-05-04 13:43:19 -040031class LatticeGP : public GrGeometryProcessor {
32public:
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050033 static GrGeometryProcessor* Make(SkArenaAlloc* arena,
Greg Danieled96bca2019-12-05 15:05:54 -050034 const GrSurfaceProxyView& view,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050035 sk_sp<GrColorSpaceXform> csxf,
36 GrSamplerState::Filter filter,
37 bool wideColor) {
Greg Danieled96bca2019-12-05 15:05:54 -050038 return arena->make<LatticeGP>(view, std::move(csxf), filter, wideColor);
Brian Salomon2a943df2018-05-04 13:43:19 -040039 }
40
41 const char* name() const override { return "LatticeGP"; }
42
43 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
44 b->add32(GrColorSpaceXform::XformKey(fColorSpaceXform.get()));
45 }
46
47 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps& caps) const override {
48 class GLSLProcessor : public GrGLSLGeometryProcessor {
49 public:
50 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc,
Brian Salomonc241b582019-11-27 08:57:17 -050051 const CoordTransformRange& transformRange) override {
Brian Salomon2a943df2018-05-04 13:43:19 -040052 const auto& latticeGP = proc.cast<LatticeGP>();
Brian Salomonc241b582019-11-27 08:57:17 -050053 this->setTransformDataHelper(SkMatrix::I(), pdman, transformRange);
Brian Osmanc891b102018-06-14 14:50:17 -040054 fColorSpaceXformHelper.setData(pdman, latticeGP.fColorSpaceXform.get());
Brian Salomon2a943df2018-05-04 13:43:19 -040055 }
56
57 private:
58 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
59 using Interpolation = GrGLSLVaryingHandler::Interpolation;
60 const auto& latticeGP = args.fGP.cast<LatticeGP>();
61 fColorSpaceXformHelper.emitCode(args.fUniformHandler,
62 latticeGP.fColorSpaceXform.get());
63
64 args.fVaryingHandler->emitAttributes(latticeGP);
Brian Osmanf04fb3c2018-11-12 15:34:00 -050065 this->writeOutputPosition(args.fVertBuilder, gpArgs, latticeGP.fInPosition.name());
Brian Salomon2a943df2018-05-04 13:43:19 -040066 this->emitTransforms(args.fVertBuilder,
67 args.fVaryingHandler,
68 args.fUniformHandler,
Brian Osmanf04fb3c2018-11-12 15:34:00 -050069 latticeGP.fInTextureCoords.asShaderVar(),
Brian Salomon2a943df2018-05-04 13:43:19 -040070 args.fFPCoordTransformHandler);
71 args.fFragBuilder->codeAppend("float2 textureCoords;");
Brian Osmanf04fb3c2018-11-12 15:34:00 -050072 args.fVaryingHandler->addPassThroughAttribute(latticeGP.fInTextureCoords,
Brian Salomon2a943df2018-05-04 13:43:19 -040073 "textureCoords");
74 args.fFragBuilder->codeAppend("float4 textureDomain;");
75 args.fVaryingHandler->addPassThroughAttribute(
Brian Osmanf04fb3c2018-11-12 15:34:00 -050076 latticeGP.fInTextureDomain, "textureDomain", Interpolation::kCanBeFlat);
77 args.fVaryingHandler->addPassThroughAttribute(latticeGP.fInColor,
78 args.fOutputColor,
Brian Salomon2a943df2018-05-04 13:43:19 -040079 Interpolation::kCanBeFlat);
80 args.fFragBuilder->codeAppendf("%s = ", args.fOutputColor);
Brian Salomon87e9ddb2019-12-19 14:50:22 -050081 args.fFragBuilder->appendTextureLookupAndBlend(
Brian Salomon2a943df2018-05-04 13:43:19 -040082 args.fOutputColor,
Brian Salomon87e9ddb2019-12-19 14:50:22 -050083 SkBlendMode::kModulate,
Brian Salomon2a943df2018-05-04 13:43:19 -040084 args.fTexSamplers[0],
85 "clamp(textureCoords, textureDomain.xy, textureDomain.zw)",
Brian Salomon2a943df2018-05-04 13:43:19 -040086 &fColorSpaceXformHelper);
87 args.fFragBuilder->codeAppend(";");
88 args.fFragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage);
89 }
90 GrGLSLColorSpaceXformHelper fColorSpaceXformHelper;
91 };
92 return new GLSLProcessor;
93 }
94
95private:
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050096 friend class ::SkArenaAlloc; // for access to ctor
97
Greg Danieled96bca2019-12-05 15:05:54 -050098 LatticeGP(const GrSurfaceProxyView& view, sk_sp<GrColorSpaceXform> csxf,
Brian Osman0b537032018-12-26 12:16:44 -050099 GrSamplerState::Filter filter, bool wideColor)
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500100 : INHERITED(kLatticeGP_ClassID)
101 , fColorSpaceXform(std::move(csxf)) {
Greg Daniel7a82edf2018-12-04 10:54:34 -0500102
Robert Phillips323471e2019-11-11 11:33:37 -0500103 fSampler.reset(GrSamplerState(GrSamplerState::WrapMode::kClamp, filter),
Greg Danieled96bca2019-12-05 15:05:54 -0500104 view.proxy()->backendFormat(), view.swizzle());
Brian Salomonf7dcd762018-07-30 14:48:15 -0400105 this->setTextureSamplerCnt(1);
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500106 fInPosition = {"position", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
107 fInTextureCoords = {"textureCoords", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
108 fInTextureDomain = {"textureDomain", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
Brian Osman0b537032018-12-26 12:16:44 -0500109 fInColor = MakeColorAttribute("color", wideColor);
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500110 this->setVertexAttributes(&fInPosition, 4);
Brian Salomon92be2f72018-06-19 14:33:47 -0400111 }
112
Brian Salomonf7dcd762018-07-30 14:48:15 -0400113 const TextureSampler& onTextureSampler(int) const override { return fSampler; }
114
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500115 Attribute fInPosition;
116 Attribute fInTextureCoords;
117 Attribute fInTextureDomain;
118 Attribute fInColor;
Brian Salomon92be2f72018-06-19 14:33:47 -0400119
Brian Salomon2a943df2018-05-04 13:43:19 -0400120 sk_sp<GrColorSpaceXform> fColorSpaceXform;
121 TextureSampler fSampler;
122
123 typedef GrGeometryProcessor INHERITED;
124};
125
Brian Salomon815486c2017-07-11 08:52:13 -0400126class NonAALatticeOp final : public GrMeshDrawOp {
127private:
128 using Helper = GrSimpleMeshDrawOpHelper;
129
joshualitt33a5fce2015-11-18 13:28:51 -0800130public:
Brian Salomon25a88092016-12-01 09:36:50 -0500131 DEFINE_OP_CLASS_ID
joshualitt33a5fce2015-11-18 13:28:51 -0800132
Robert Phillipsb97da532019-02-12 15:24:12 -0500133 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400134 GrPaint&& paint,
135 const SkMatrix& viewMatrix,
Greg Danieled96bca2019-12-05 15:05:54 -0500136 GrSurfaceProxyView view,
Greg Daniel82c6b102020-01-21 10:33:22 -0500137 SkAlphaType alphaType,
Brian Salomon2a943df2018-05-04 13:43:19 -0400138 sk_sp<GrColorSpaceXform> colorSpaceXForm,
139 GrSamplerState::Filter filter,
Robert Phillips7c525e62018-06-12 10:11:12 -0400140 std::unique_ptr<SkLatticeIter> iter,
141 const SkRect& dst) {
Greg Danieled96bca2019-12-05 15:05:54 -0500142 SkASSERT(view.proxy());
Robert Phillips7c525e62018-06-12 10:11:12 -0400143 return Helper::FactoryHelper<NonAALatticeOp>(context, std::move(paint), viewMatrix,
Greg Daniel82c6b102020-01-21 10:33:22 -0500144 std::move(view), alphaType,
Brian Salomon2a943df2018-05-04 13:43:19 -0400145 std::move(colorSpaceXForm), filter,
146 std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -0400147 }
148
Brian Osmancf860852018-10-31 14:04:39 -0400149 NonAALatticeOp(Helper::MakeArgs& helperArgs, const SkPMColor4f& color,
Greg Danieled96bca2019-12-05 15:05:54 -0500150 const SkMatrix& viewMatrix, GrSurfaceProxyView view,
Greg Daniel82c6b102020-01-21 10:33:22 -0500151 SkAlphaType alphaType, sk_sp<GrColorSpaceXform> colorSpaceXform,
Greg Danielc594e622019-10-15 14:01:49 -0400152 GrSamplerState::Filter filter, std::unique_ptr<SkLatticeIter> iter,
153 const SkRect& dst)
Brian Salomon2a943df2018-05-04 13:43:19 -0400154 : INHERITED(ClassID())
155 , fHelper(helperArgs, GrAAType::kNone)
Greg Danieled96bca2019-12-05 15:05:54 -0500156 , fView(std::move(view))
Greg Daniel82c6b102020-01-21 10:33:22 -0500157 , fAlphaType(alphaType)
Brian Salomon2a943df2018-05-04 13:43:19 -0400158 , fColorSpaceXform(std::move(colorSpaceXform))
159 , fFilter(filter) {
bsalomona71b8982016-06-30 12:13:52 -0700160 Patch& patch = fPatches.push_back();
161 patch.fViewMatrix = viewMatrix;
162 patch.fColor = color;
msarett10e3d9b2016-08-18 15:46:03 -0700163 patch.fIter = std::move(iter);
bsalomona71b8982016-06-30 12:13:52 -0700164 patch.fDst = dst;
joshualitt33a5fce2015-11-18 13:28:51 -0800165
joshualitt33a5fce2015-11-18 13:28:51 -0800166 // setup bounds
Greg Daniel5faf4742019-10-01 15:14:44 -0400167 this->setTransformedBounds(patch.fDst, viewMatrix, HasAABloat::kNo, IsHairline::kNo);
joshualitt33a5fce2015-11-18 13:28:51 -0800168 }
169
Brian Salomonfc527d22016-12-14 21:07:01 -0500170 const char* name() const override { return "NonAALatticeOp"; }
robertphillips783a4da2015-11-19 14:00:02 -0800171
Chris Dalton1706cbf2019-05-21 19:35:29 -0600172 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipse37f1c42020-03-09 16:10:18 -0400173 if (fProgramInfo) {
174 fProgramInfo->visitProxies(func);
175 } else {
176 bool mipped = (GrSamplerState::Filter::kMipMap == fFilter);
177 func(fView.proxy(), GrMipMapped(mipped));
178 fHelper.visitProxies(func);
179 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400180 }
181
Brian Osman9a390ac2018-11-12 09:47:48 -0500182#ifdef SK_DEBUG
robertphillips783a4da2015-11-19 14:00:02 -0800183 SkString dumpInfo() const override {
184 SkString str;
185
bsalomona71b8982016-06-30 12:13:52 -0700186 for (int i = 0; i < fPatches.count(); ++i) {
Brian Salomonfc527d22016-12-14 21:07:01 -0500187 str.appendf("%d: Color: 0x%08x Dst [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", i,
Brian Osmancf860852018-10-31 14:04:39 -0400188 fPatches[i].fColor.toBytes_RGBA(), fPatches[i].fDst.fLeft,
Brian Osman1be2b7c2018-10-29 16:07:15 -0400189 fPatches[i].fDst.fTop, fPatches[i].fDst.fRight, fPatches[i].fDst.fBottom);
robertphillips783a4da2015-11-19 14:00:02 -0800190 }
191
Brian Salomon815486c2017-07-11 08:52:13 -0400192 str += fHelper.dumpInfo();
193 str += INHERITED::dumpInfo();
robertphillips783a4da2015-11-19 14:00:02 -0800194 return str;
195 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500196#endif
joshualitt33a5fce2015-11-18 13:28:51 -0800197
Brian Salomon815486c2017-07-11 08:52:13 -0400198 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
199
Chris Dalton6ce447a2019-06-23 18:07:38 -0600200 GrProcessorSet::Analysis finalize(
201 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
202 GrClampType clampType) override {
Greg Daniel82c6b102020-01-21 10:33:22 -0500203 auto opaque = fPatches[0].fColor.isOpaque() && fAlphaType == kOpaque_SkAlphaType
Brian Salomon2a943df2018-05-04 13:43:19 -0400204 ? GrProcessorAnalysisColor::Opaque::kYes
205 : GrProcessorAnalysisColor::Opaque::kNo;
206 auto analysisColor = GrProcessorAnalysisColor(opaque);
Chris Dalton6ce447a2019-06-23 18:07:38 -0600207 auto result = fHelper.finalizeProcessors(
208 caps, clip, hasMixedSampledCoverage, clampType, GrProcessorAnalysisCoverage::kNone,
209 &analysisColor);
Brian Salomon2a943df2018-05-04 13:43:19 -0400210 analysisColor.isConstant(&fPatches[0].fColor);
Brian Osman2715bf52019-12-06 14:38:47 -0500211 fWideColor = !fPatches[0].fColor.fitsInBytes();
Brian Salomon2a943df2018-05-04 13:43:19 -0400212 return result;
Brian Salomon815486c2017-07-11 08:52:13 -0400213 }
214
Brian Salomon92aee3d2016-12-21 09:20:25 -0500215private:
Robert Phillips4133dc42020-03-11 15:55:55 -0400216 void onCreateProgramInfo(const GrCaps* caps,
217 SkArenaAlloc* arena,
218 const GrSurfaceProxyView* outputView,
219 GrAppliedClip&& appliedClip,
220 const GrXferProcessor::DstProxyView& dstProxyView) override {
Robert Phillipse37f1c42020-03-09 16:10:18 -0400221
222 auto gp = LatticeGP::Make(arena, fView, fColorSpaceXform, fFilter, fWideColor);
joshualitt33a5fce2015-11-18 13:28:51 -0800223 if (!gp) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400224 return;
Robert Phillipse37f1c42020-03-09 16:10:18 -0400225 }
226
227 static constexpr int kOnePrimProcTexture = 1;
228 auto fixedDynamicState = GrMeshDrawOp::Target::MakeFixedDynamicState(arena, &appliedClip,
229 kOnePrimProcTexture);
230 fixedDynamicState->fPrimitiveProcessorTextures[0] = fView.proxy();
231
Robert Phillips4133dc42020-03-11 15:55:55 -0400232 fProgramInfo = GrSimpleMeshDrawOpHelper::CreateProgramInfo(caps, arena, outputView,
233 std::move(appliedClip),
234 dstProxyView, gp,
235 fHelper.detachProcessorSet(),
236 GrPrimitiveType::kTriangles,
237 fHelper.pipelineFlags(),
238 &GrUserStencilSettings::kUnused,
239 fixedDynamicState);
Robert Phillipse37f1c42020-03-09 16:10:18 -0400240 }
241
242 void onPrePrepareDraws(GrRecordingContext* context,
243 const GrSurfaceProxyView* outputView,
244 GrAppliedClip* clip,
245 const GrXferProcessor::DstProxyView& dstProxyView) override {
246 SkArenaAlloc* arena = context->priv().recordTimeAllocator();
247
248 // This is equivalent to a GrOpFlushState::detachAppliedClip
249 GrAppliedClip appliedClip = clip ? std::move(*clip) : GrAppliedClip();
250
Robert Phillips4133dc42020-03-11 15:55:55 -0400251 this->createProgramInfo(context->priv().caps(), arena, outputView,
252 std::move(appliedClip), dstProxyView);
Robert Phillipse37f1c42020-03-09 16:10:18 -0400253
254 context->priv().recordProgramInfo(fProgramInfo);
255 }
256
257 void onPrepareDraws(Target* target) override {
258 if (!fProgramInfo) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400259 this->createProgramInfo(target);
Robert Phillipse37f1c42020-03-09 16:10:18 -0400260 if (!fProgramInfo) {
261 return;
262 }
joshualitt33a5fce2015-11-18 13:28:51 -0800263 }
264
bsalomona71b8982016-06-30 12:13:52 -0700265 int patchCnt = fPatches.count();
msarett10e3d9b2016-08-18 15:46:03 -0700266 int numRects = 0;
267 for (int i = 0; i < patchCnt; i++) {
msarett0764efe2016-09-02 11:24:30 -0700268 numRects += fPatches[i].fIter->numRectsToDraw();
msarett10e3d9b2016-08-18 15:46:03 -0700269 }
joshualitt33a5fce2015-11-18 13:28:51 -0800270
Brian Salomon0db1b532017-07-12 15:21:43 -0400271 if (!numRects) {
272 return;
273 }
274
Robert Phillipse37f1c42020-03-09 16:10:18 -0400275 const size_t kVertexStride = fProgramInfo->primProc().vertexStride();
Robert Phillipse94cdd22019-11-04 14:15:58 -0500276
277 QuadHelper helper(target, kVertexStride, numRects);
278
Brian Osmanc3064e72018-11-15 08:59:22 -0500279 GrVertexWriter vertices{helper.vertices()};
Brian Salomon12d22642019-01-29 14:38:50 -0500280 if (!vertices.fPtr) {
joshualitt33a5fce2015-11-18 13:28:51 -0800281 SkDebugf("Could not allocate vertices\n");
282 return;
283 }
284
bsalomona71b8982016-06-30 12:13:52 -0700285 for (int i = 0; i < patchCnt; i++) {
msarett7fc08582016-08-18 14:29:22 -0700286 const Patch& patch = fPatches[i];
Brian Osmanc3064e72018-11-15 08:59:22 -0500287
Brian Osman0b537032018-12-26 12:16:44 -0500288 GrVertexColor patchColor(patch.fColor, fWideColor);
msarett10e3d9b2016-08-18 15:46:03 -0700289
290 // Apply the view matrix here if it is scale-translate. Otherwise, we need to
291 // wait until we've created the dst rects.
292 bool isScaleTranslate = patch.fViewMatrix.isScaleTranslate();
293 if (isScaleTranslate) {
294 patch.fIter->mapDstScaleTranslate(patch.fViewMatrix);
295 }
joshualitt33a5fce2015-11-18 13:28:51 -0800296
Brian Salomon2a943df2018-05-04 13:43:19 -0400297 SkIRect srcR;
298 SkRect dstR;
Brian Osmanc3064e72018-11-15 08:59:22 -0500299 SkPoint* patchPositions = reinterpret_cast<SkPoint*>(vertices.fPtr);
Greg Danieled96bca2019-12-05 15:05:54 -0500300 Sk4f scales(1.f / fView.proxy()->width(), 1.f / fView.proxy()->height(),
301 1.f / fView.proxy()->width(), 1.f / fView.proxy()->height());
Brian Salomon2a943df2018-05-04 13:43:19 -0400302 static const Sk4f kDomainOffsets(0.5f, 0.5f, -0.5f, -0.5f);
Brian Osmanc3064e72018-11-15 08:59:22 -0500303 static const Sk4f kFlipOffsets(0.f, 1.f, 0.f, 1.f);
Brian Salomon2a943df2018-05-04 13:43:19 -0400304 static const Sk4f kFlipMuls(1.f, -1.f, 1.f, -1.f);
msarett10e3d9b2016-08-18 15:46:03 -0700305 while (patch.fIter->next(&srcR, &dstR)) {
Brian Salomon2a943df2018-05-04 13:43:19 -0400306 Sk4f coords(SkIntToScalar(srcR.fLeft), SkIntToScalar(srcR.fTop),
307 SkIntToScalar(srcR.fRight), SkIntToScalar(srcR.fBottom));
308 Sk4f domain = coords + kDomainOffsets;
309 coords *= scales;
310 domain *= scales;
Greg Danieled96bca2019-12-05 15:05:54 -0500311 if (fView.origin() == kBottomLeft_GrSurfaceOrigin) {
Brian Salomon2a943df2018-05-04 13:43:19 -0400312 coords = kFlipMuls * coords + kFlipOffsets;
313 domain = SkNx_shuffle<0, 3, 2, 1>(kFlipMuls * domain + kFlipOffsets);
314 }
Brian Osman4486d982018-11-15 15:56:04 -0500315 SkRect texDomain;
316 SkRect texCoords;
317 domain.store(&texDomain);
318 coords.store(&texCoords);
joshualitt33a5fce2015-11-18 13:28:51 -0800319
Brian Osman0dd43022018-11-16 15:53:26 -0500320 vertices.writeQuad(GrVertexWriter::TriStripFromRect(dstR),
321 GrVertexWriter::TriStripFromRect(texCoords),
Brian Osmanc3064e72018-11-15 08:59:22 -0500322 texDomain,
323 patchColor);
joshualitt33a5fce2015-11-18 13:28:51 -0800324 }
msarett10e3d9b2016-08-18 15:46:03 -0700325
326 // If we didn't handle it above, apply the matrix here.
327 if (!isScaleTranslate) {
Robert Phillipsee08d522019-10-28 16:34:44 -0400328 SkMatrixPriv::MapPointsWithStride(
329 patch.fViewMatrix, patchPositions, kVertexStride,
330 GrResourceProvider::NumVertsPerNonAAQuad() * patch.fIter->numRectsToDraw());
msarett10e3d9b2016-08-18 15:46:03 -0700331 }
joshualitt33a5fce2015-11-18 13:28:51 -0800332 }
Robert Phillipse37f1c42020-03-09 16:10:18 -0400333
334 fMesh = helper.mesh();
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700335 }
336
337 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillipse37f1c42020-03-09 16:10:18 -0400338 if (!fProgramInfo || !fMesh) {
339 return;
340 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500341
Robert Phillipse37f1c42020-03-09 16:10:18 -0400342 flushState->opsRenderPass()->bindPipeline(*fProgramInfo, chainBounds);
343 flushState->opsRenderPass()->drawMeshes(*fProgramInfo, fMesh, 1);
joshualitt33a5fce2015-11-18 13:28:51 -0800344 }
345
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500346 CombineResult onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*,
347 const GrCaps& caps) override {
Brian Salomonfc527d22016-12-14 21:07:01 -0500348 NonAALatticeOp* that = t->cast<NonAALatticeOp>();
Greg Danieled96bca2019-12-05 15:05:54 -0500349 if (fView != that->fView) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000350 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400351 }
352 if (fFilter != that->fFilter) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000353 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400354 }
355 if (GrColorSpaceXform::Equals(fColorSpaceXform.get(), that->fColorSpaceXform.get())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000356 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400357 }
Brian Salomon815486c2017-07-11 08:52:13 -0400358 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000359 return CombineResult::kCannotCombine;
joshualitt33a5fce2015-11-18 13:28:51 -0800360 }
361
msarett10e3d9b2016-08-18 15:46:03 -0700362 fPatches.move_back_n(that->fPatches.count(), that->fPatches.begin());
Brian Osman0b537032018-12-26 12:16:44 -0500363 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000364 return CombineResult::kMerged;
joshualitt33a5fce2015-11-18 13:28:51 -0800365 }
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
Robert Phillipse37f1c42020-03-09 16:10:18 -0400382 GrMesh* fMesh = nullptr;
383 GrProgramInfo* fProgramInfo = nullptr;
384
Brian Salomon815486c2017-07-11 08:52:13 -0400385 typedef GrMeshDrawOp INHERITED;
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 {
Robert Phillipsb97da532019-02-12 15:24:12 -0500391std::unique_ptr<GrDrawOp> MakeNonAA(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400392 GrPaint&& paint,
393 const SkMatrix& viewMatrix,
Greg Danieled96bca2019-12-05 15:05:54 -0500394 GrSurfaceProxyView view,
Greg Daniel82c6b102020-01-21 10:33:22 -0500395 SkAlphaType alphaType,
Brian Salomon2a943df2018-05-04 13:43:19 -0400396 sk_sp<GrColorSpaceXform> colorSpaceXform,
397 GrSamplerState::Filter filter,
Robert Phillips7c525e62018-06-12 10:11:12 -0400398 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}
403};
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 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(format, GrColorType::kRGBA_8888);
463
Brian Salomonbeb7f522019-08-30 16:19:42 -0400464 auto proxy = context->priv().proxyProvider()->createProxy(format,
Brian Salomona56a7462020-02-07 14:17:25 -0500465 dims,
Greg Daniel47c20e82020-01-21 14:29:57 -0500466 swizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400467 GrRenderable::kNo,
468 1,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400469 GrMipMapped::kNo,
470 SkBackingFit::kExact,
471 SkBudgeted::kYes,
472 GrProtected::kNo);
Brian Salomon2a943df2018-05-04 13:43:19 -0400473
Brian Salomon18df7632017-07-11 14:32:18 -0400474 do {
Brian Salomon18df7632017-07-11 14:32:18 -0400475 if (random->nextBool()) {
Brian Salomona56a7462020-02-07 14:17:25 -0500476 subset.fLeft = random->nextULessThan(dims.fWidth);
477 subset.fRight = random->nextRangeU(subset.fLeft + 1, dims.fWidth);
478 subset.fTop = random->nextULessThan(dims.fHeight);
479 subset.fBottom = random->nextRangeU(subset.fTop + 1, dims.fHeight);
Brian Salomon18df7632017-07-11 14:32:18 -0400480 } else {
Brian Salomona56a7462020-02-07 14:17:25 -0500481 subset.setXYWH(0, 0, dims.fWidth, dims.fHeight);
Brian Salomon815486c2017-07-11 08:52:13 -0400482 }
Brian Salomon2a943df2018-05-04 13:43:19 -0400483 // SkCanvas::Lattice allows bounds to be null. However, SkCanvas creates a temp Lattice with
484 // a non-null bounds before creating a SkLatticeIter since SkLatticeIter requires a bounds.
Brian Salomon18df7632017-07-11 14:32:18 -0400485 lattice.fBounds = &subset;
486 lattice.fXCount = random->nextRangeU(1, subset.width());
487 lattice.fYCount = random->nextRangeU(1, subset.height());
488 xdivs.reset(new int[lattice.fXCount]);
489 ydivs.reset(new int[lattice.fYCount]);
490 init_random_divs(xdivs.get(), lattice.fXCount, subset.fLeft, subset.fRight, random);
491 init_random_divs(ydivs.get(), lattice.fYCount, subset.fTop, subset.fBottom, random);
492 lattice.fXDivs = xdivs.get();
493 lattice.fYDivs = ydivs.get();
494 bool hasFlags = random->nextBool();
495 if (hasFlags) {
496 int n = (lattice.fXCount + 1) * (lattice.fYCount + 1);
Stan Ilievca8c0952017-12-11 13:01:58 -0500497 flags.reset(new SkCanvas::Lattice::RectType[n]);
498 colors.reset(new SkColor[n]);
Brian Salomon18df7632017-07-11 14:32:18 -0400499 for (int i = 0; i < n; ++i) {
Stan Ilievca8c0952017-12-11 13:01:58 -0500500 flags[i] = random->nextBool() ? SkCanvas::Lattice::kTransparent
501 : SkCanvas::Lattice::kDefault;
Brian Salomon18df7632017-07-11 14:32:18 -0400502 }
Stan Ilievca8c0952017-12-11 13:01:58 -0500503 lattice.fRectTypes = flags.get();
504 lattice.fColors = colors.get();
Brian Salomon18df7632017-07-11 14:32:18 -0400505 } else {
Stan Ilievca8c0952017-12-11 13:01:58 -0500506 lattice.fRectTypes = nullptr;
507 lattice.fColors = nullptr;
Brian Salomon18df7632017-07-11 14:32:18 -0400508 }
Brian Salomona56a7462020-02-07 14:17:25 -0500509 } while (!SkLatticeIter::Valid(dims.fWidth, dims.fHeight, lattice));
Brian Salomon815486c2017-07-11 08:52:13 -0400510 SkRect dst;
511 dst.fLeft = random->nextRangeScalar(-2000.5f, 1000.f);
512 dst.fTop = random->nextRangeScalar(-2000.5f, 1000.f);
513 dst.fRight = dst.fLeft + random->nextRangeScalar(0.5f, 1000.f);
514 dst.fBottom = dst.fTop + random->nextRangeScalar(0.5f, 1000.f);
515 std::unique_ptr<SkLatticeIter> iter(new SkLatticeIter(lattice, dst));
516 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
Brian Salomon2a943df2018-05-04 13:43:19 -0400517 auto csxf = GrTest::TestColorXform(random);
518 GrSamplerState::Filter filter =
519 random->nextBool() ? GrSamplerState::Filter::kNearest : GrSamplerState::Filter::kBilerp;
Greg Danieled96bca2019-12-05 15:05:54 -0500520
521 GrSurfaceProxyView view(
522 std::move(proxy), origin,
Greg Daniel14b57212019-12-17 16:18:06 -0500523 context->priv().caps()->getReadSwizzle(format, GrColorType::kRGBA_8888));
Greg Danieled96bca2019-12-05 15:05:54 -0500524
525 return NonAALatticeOp::Make(context, std::move(paint), viewMatrix, std::move(view),
Greg Daniel82c6b102020-01-21 10:33:22 -0500526 kPremul_SkAlphaType, std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -0400527}
528
529#endif