blob: 8806172f949eeb96ab35d915f493f2800dc182fe [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
Brian Salomonfc527d22016-12-14 21:07:01 -05008#include "GrLatticeOp.h"
joshualitt33a5fce2015-11-18 13:28:51 -08009#include "GrDefaultGeoProcFactory.h"
Brian Salomon815486c2017-07-11 08:52:13 -040010#include "GrDrawOpTest.h"
Greg Daniel7a82edf2018-12-04 10:54:34 -050011#include "GrGpu.h"
Brian Salomondad29232016-12-01 16:40:24 -050012#include "GrMeshDrawOp.h"
Brian Salomon742e31d2016-12-07 17:06:19 -050013#include "GrOpFlushState.h"
joshualitt33a5fce2015-11-18 13:28:51 -080014#include "GrResourceProvider.h"
Greg Daniel7a82edf2018-12-04 10:54:34 -050015#include "GrResourceProviderPriv.h"
Brian Salomon815486c2017-07-11 08:52:13 -040016#include "GrSimpleMeshDrawOpHelper.h"
Brian Osmanc3064e72018-11-15 08:59:22 -050017#include "GrVertexWriter.h"
joshualitt33a5fce2015-11-18 13:28:51 -080018#include "SkBitmap.h"
msarettc573a402016-08-02 08:05:56 -070019#include "SkLatticeIter.h"
Brian Salomonfa3783f2018-01-05 13:49:07 -050020#include "SkMatrixPriv.h"
joshualitt33a5fce2015-11-18 13:28:51 -080021#include "SkRect.h"
Brian Salomon2a943df2018-05-04 13:43:19 -040022#include "glsl/GrGLSLColorSpaceXformHelper.h"
23#include "glsl/GrGLSLGeometryProcessor.h"
24#include "glsl/GrGLSLVarying.h"
joshualitt33a5fce2015-11-18 13:28:51 -080025
Brian Salomon815486c2017-07-11 08:52:13 -040026namespace {
27
Brian Salomon2a943df2018-05-04 13:43:19 -040028class LatticeGP : public GrGeometryProcessor {
29public:
Greg Daniel7a82edf2018-12-04 10:54:34 -050030 static sk_sp<GrGeometryProcessor> Make(GrGpu* gpu,
31 const GrTextureProxy* proxy,
Brian Salomon2a943df2018-05-04 13:43:19 -040032 sk_sp<GrColorSpaceXform> csxf,
Brian Osman0b537032018-12-26 12:16:44 -050033 GrSamplerState::Filter filter,
34 bool wideColor) {
35 return sk_sp<GrGeometryProcessor>(
36 new LatticeGP(gpu, proxy, std::move(csxf), filter, wideColor));
Brian Salomon2a943df2018-05-04 13:43:19 -040037 }
38
39 const char* name() const override { return "LatticeGP"; }
40
41 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
42 b->add32(GrColorSpaceXform::XformKey(fColorSpaceXform.get()));
43 }
44
45 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps& caps) const override {
46 class GLSLProcessor : public GrGLSLGeometryProcessor {
47 public:
48 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc,
49 FPCoordTransformIter&& transformIter) override {
50 const auto& latticeGP = proc.cast<LatticeGP>();
51 this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter);
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());
Brian Salomon2a943df2018-05-04 13:43:19 -040064 this->emitTransforms(args.fVertBuilder,
65 args.fVaryingHandler,
66 args.fUniformHandler,
Brian Osmanf04fb3c2018-11-12 15:34:00 -050067 latticeGP.fInTextureCoords.asShaderVar(),
Brian Salomon2a943df2018-05-04 13:43:19 -040068 args.fFPCoordTransformHandler);
69 args.fFragBuilder->codeAppend("float2 textureCoords;");
Brian Osmanf04fb3c2018-11-12 15:34:00 -050070 args.fVaryingHandler->addPassThroughAttribute(latticeGP.fInTextureCoords,
Brian Salomon2a943df2018-05-04 13:43:19 -040071 "textureCoords");
72 args.fFragBuilder->codeAppend("float4 textureDomain;");
73 args.fVaryingHandler->addPassThroughAttribute(
Brian Osmanf04fb3c2018-11-12 15:34:00 -050074 latticeGP.fInTextureDomain, "textureDomain", Interpolation::kCanBeFlat);
75 args.fVaryingHandler->addPassThroughAttribute(latticeGP.fInColor,
76 args.fOutputColor,
Brian Salomon2a943df2018-05-04 13:43:19 -040077 Interpolation::kCanBeFlat);
78 args.fFragBuilder->codeAppendf("%s = ", args.fOutputColor);
79 args.fFragBuilder->appendTextureLookupAndModulate(
80 args.fOutputColor,
81 args.fTexSamplers[0],
82 "clamp(textureCoords, textureDomain.xy, textureDomain.zw)",
83 kFloat2_GrSLType,
84 &fColorSpaceXformHelper);
85 args.fFragBuilder->codeAppend(";");
86 args.fFragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage);
87 }
88 GrGLSLColorSpaceXformHelper fColorSpaceXformHelper;
89 };
90 return new GLSLProcessor;
91 }
92
93private:
Greg Daniel7a82edf2018-12-04 10:54:34 -050094 LatticeGP(GrGpu* gpu, const GrTextureProxy* proxy, sk_sp<GrColorSpaceXform> csxf,
Brian Osman0b537032018-12-26 12:16:44 -050095 GrSamplerState::Filter filter, bool wideColor)
Brian Salomon2a943df2018-05-04 13:43:19 -040096 : INHERITED(kLatticeGP_ClassID), fColorSpaceXform(std::move(csxf)) {
Greg Daniel7a82edf2018-12-04 10:54:34 -050097
98 GrSamplerState samplerState = GrSamplerState(GrSamplerState::WrapMode::kClamp,
99 filter);
100 uint32_t extraSamplerKey = gpu->getExtraSamplerKeyForProgram(samplerState,
101 proxy->backendFormat());
102
103 fSampler.reset(proxy->textureType(), proxy->config(), samplerState,
104 extraSamplerKey);
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
133 static const int kVertsPerRect = 4;
134 static const int kIndicesPerRect = 6;
joshualitt33a5fce2015-11-18 13:28:51 -0800135
Robert Phillipsb97da532019-02-12 15:24:12 -0500136 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400137 GrPaint&& paint,
138 const SkMatrix& viewMatrix,
Brian Salomon2a943df2018-05-04 13:43:19 -0400139 sk_sp<GrTextureProxy> proxy,
140 sk_sp<GrColorSpaceXform> colorSpaceXForm,
141 GrSamplerState::Filter filter,
Robert Phillips7c525e62018-06-12 10:11:12 -0400142 std::unique_ptr<SkLatticeIter> iter,
143 const SkRect& dst) {
Brian Salomond3cee172018-05-07 16:40:48 -0400144 SkASSERT(proxy);
Robert Phillips7c525e62018-06-12 10:11:12 -0400145 return Helper::FactoryHelper<NonAALatticeOp>(context, std::move(paint), viewMatrix,
146 std::move(proxy),
Brian Salomon2a943df2018-05-04 13:43:19 -0400147 std::move(colorSpaceXForm), filter,
148 std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -0400149 }
150
Brian Osmancf860852018-10-31 14:04:39 -0400151 NonAALatticeOp(Helper::MakeArgs& helperArgs, const SkPMColor4f& color,
152 const SkMatrix& viewMatrix, sk_sp<GrTextureProxy> proxy,
153 sk_sp<GrColorSpaceXform> colorSpaceXform, GrSamplerState::Filter filter,
154 std::unique_ptr<SkLatticeIter> iter, const SkRect& dst)
Brian Salomon2a943df2018-05-04 13:43:19 -0400155 : INHERITED(ClassID())
156 , fHelper(helperArgs, GrAAType::kNone)
157 , fProxy(std::move(proxy))
158 , 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
bsalomon88cf17d2016-07-08 06:40:56 -0700167 this->setTransformedBounds(patch.fDst, viewMatrix, HasAABloat::kNo, IsZeroArea::kNo);
Brian Osman0b537032018-12-26 12:16:44 -0500168 fWideColor = !SkPMColor4fFitsInBytes(color);
joshualitt33a5fce2015-11-18 13:28:51 -0800169 }
170
Brian Salomonfc527d22016-12-14 21:07:01 -0500171 const char* name() const override { return "NonAALatticeOp"; }
robertphillips783a4da2015-11-19 14:00:02 -0800172
Brian Salomon7d94bb52018-10-12 14:37:19 -0400173 void visitProxies(const VisitProxyFunc& func, VisitorType) const override {
Brian Salomond3cee172018-05-07 16:40:48 -0400174 func(fProxy.get());
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400175 fHelper.visitProxies(func);
176 }
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 Daltonb8fff0d2019-03-05 10:11:58 -0700196 GrProcessorSet::Analysis finalize(
197 const GrCaps& caps, const GrAppliedClip* clip, GrFSAAType fsaaType) override {
Brian Osman1be2b7c2018-10-29 16:07:15 -0400198 auto opaque = fPatches[0].fColor.isOpaque() && GrPixelConfigIsOpaque(fProxy->config())
Brian Salomon2a943df2018-05-04 13:43:19 -0400199 ? GrProcessorAnalysisColor::Opaque::kYes
200 : GrProcessorAnalysisColor::Opaque::kNo;
201 auto analysisColor = GrProcessorAnalysisColor(opaque);
Chris Dalton4b62aed2019-01-15 11:53:00 -0700202 auto result = fHelper.finalizeProcessors(
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700203 caps, clip, fsaaType, GrProcessorAnalysisCoverage::kNone, &analysisColor);
Brian Salomon2a943df2018-05-04 13:43:19 -0400204 analysisColor.isConstant(&fPatches[0].fColor);
205 return result;
Brian Salomon815486c2017-07-11 08:52:13 -0400206 }
207
Brian Salomon92aee3d2016-12-21 09:20:25 -0500208private:
Brian Salomon91326c32017-08-09 16:02:19 -0400209 void onPrepareDraws(Target* target) override {
Greg Daniel7a82edf2018-12-04 10:54:34 -0500210 GrGpu* gpu = target->resourceProvider()->priv().gpu();
Brian Osman0b537032018-12-26 12:16:44 -0500211 auto gp = LatticeGP::Make(gpu, fProxy.get(), fColorSpaceXform, fFilter, fWideColor);
joshualitt33a5fce2015-11-18 13:28:51 -0800212 if (!gp) {
213 SkDebugf("Couldn't create GrGeometryProcessor\n");
214 return;
215 }
216
bsalomona71b8982016-06-30 12:13:52 -0700217 int patchCnt = fPatches.count();
msarett10e3d9b2016-08-18 15:46:03 -0700218 int numRects = 0;
219 for (int i = 0; i < patchCnt; i++) {
msarett0764efe2016-09-02 11:24:30 -0700220 numRects += fPatches[i].fIter->numRectsToDraw();
msarett10e3d9b2016-08-18 15:46:03 -0700221 }
joshualitt33a5fce2015-11-18 13:28:51 -0800222
Brian Salomon0db1b532017-07-12 15:21:43 -0400223 if (!numRects) {
224 return;
225 }
226
Brian Osmanc3064e72018-11-15 08:59:22 -0500227 const size_t kVertexStride = gp->vertexStride();
Brian Salomond28a79d2017-10-16 13:01:07 -0400228 sk_sp<const GrBuffer> indexBuffer = target->resourceProvider()->refQuadIndexBuffer();
Brian Salomon12d22642019-01-29 14:38:50 -0500229 if (!indexBuffer) {
230 SkDebugf("Could not allocate indices\n");
231 return;
232 }
233 PatternHelper helper(target, GrPrimitiveType::kTriangles, kVertexStride,
234 std::move(indexBuffer), kVertsPerRect, kIndicesPerRect, numRects);
Brian Osmanc3064e72018-11-15 08:59:22 -0500235 GrVertexWriter vertices{helper.vertices()};
Brian Salomon12d22642019-01-29 14:38:50 -0500236 if (!vertices.fPtr) {
joshualitt33a5fce2015-11-18 13:28:51 -0800237 SkDebugf("Could not allocate vertices\n");
238 return;
239 }
240
bsalomona71b8982016-06-30 12:13:52 -0700241 for (int i = 0; i < patchCnt; i++) {
msarett7fc08582016-08-18 14:29:22 -0700242 const Patch& patch = fPatches[i];
Brian Osmanc3064e72018-11-15 08:59:22 -0500243
Brian Osman0b537032018-12-26 12:16:44 -0500244 GrVertexColor patchColor(patch.fColor, fWideColor);
msarett10e3d9b2016-08-18 15:46:03 -0700245
246 // Apply the view matrix here if it is scale-translate. Otherwise, we need to
247 // wait until we've created the dst rects.
248 bool isScaleTranslate = patch.fViewMatrix.isScaleTranslate();
249 if (isScaleTranslate) {
250 patch.fIter->mapDstScaleTranslate(patch.fViewMatrix);
251 }
joshualitt33a5fce2015-11-18 13:28:51 -0800252
Brian Salomon2a943df2018-05-04 13:43:19 -0400253 SkIRect srcR;
254 SkRect dstR;
Brian Osmanc3064e72018-11-15 08:59:22 -0500255 SkPoint* patchPositions = reinterpret_cast<SkPoint*>(vertices.fPtr);
Brian Salomon2a943df2018-05-04 13:43:19 -0400256 Sk4f scales(1.f / fProxy->width(), 1.f / fProxy->height(),
257 1.f / fProxy->width(), 1.f / fProxy->height());
258 static const Sk4f kDomainOffsets(0.5f, 0.5f, -0.5f, -0.5f);
Brian Osmanc3064e72018-11-15 08:59:22 -0500259 static const Sk4f kFlipOffsets(0.f, 1.f, 0.f, 1.f);
Brian Salomon2a943df2018-05-04 13:43:19 -0400260 static const Sk4f kFlipMuls(1.f, -1.f, 1.f, -1.f);
msarett10e3d9b2016-08-18 15:46:03 -0700261 while (patch.fIter->next(&srcR, &dstR)) {
Brian Salomon2a943df2018-05-04 13:43:19 -0400262 Sk4f coords(SkIntToScalar(srcR.fLeft), SkIntToScalar(srcR.fTop),
263 SkIntToScalar(srcR.fRight), SkIntToScalar(srcR.fBottom));
264 Sk4f domain = coords + kDomainOffsets;
265 coords *= scales;
266 domain *= scales;
267 if (fProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
268 coords = kFlipMuls * coords + kFlipOffsets;
269 domain = SkNx_shuffle<0, 3, 2, 1>(kFlipMuls * domain + kFlipOffsets);
270 }
Brian Osman4486d982018-11-15 15:56:04 -0500271 SkRect texDomain;
272 SkRect texCoords;
273 domain.store(&texDomain);
274 coords.store(&texCoords);
joshualitt33a5fce2015-11-18 13:28:51 -0800275
Brian Osman0dd43022018-11-16 15:53:26 -0500276 vertices.writeQuad(GrVertexWriter::TriStripFromRect(dstR),
277 GrVertexWriter::TriStripFromRect(texCoords),
Brian Osmanc3064e72018-11-15 08:59:22 -0500278 texDomain,
279 patchColor);
joshualitt33a5fce2015-11-18 13:28:51 -0800280 }
msarett10e3d9b2016-08-18 15:46:03 -0700281
282 // If we didn't handle it above, apply the matrix here.
283 if (!isScaleTranslate) {
Brian Osmanc3064e72018-11-15 08:59:22 -0500284 SkMatrixPriv::MapPointsWithStride(patch.fViewMatrix, patchPositions, kVertexStride,
Brian Salomonfa3783f2018-01-05 13:49:07 -0500285 kVertsPerRect * patch.fIter->numRectsToDraw());
msarett10e3d9b2016-08-18 15:46:03 -0700286 }
joshualitt33a5fce2015-11-18 13:28:51 -0800287 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700288 auto fixedDynamicState = target->makeFixedDynamicState(1);
289 fixedDynamicState->fPrimitiveProcessorTextures[0] = fProxy.get();
290 helper.recordDraw(target, std::move(gp), fixedDynamicState);
291 }
292
293 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
294 fHelper.executeDrawsAndUploads(this, flushState, chainBounds);
joshualitt33a5fce2015-11-18 13:28:51 -0800295 }
296
Brian Salomon7eae3e02018-08-07 14:02:38 +0000297 CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Brian Salomonfc527d22016-12-14 21:07:01 -0500298 NonAALatticeOp* that = t->cast<NonAALatticeOp>();
Brian Salomon2a943df2018-05-04 13:43:19 -0400299 if (fProxy != that->fProxy) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000300 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400301 }
302 if (fFilter != that->fFilter) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000303 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400304 }
305 if (GrColorSpaceXform::Equals(fColorSpaceXform.get(), that->fColorSpaceXform.get())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000306 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400307 }
Brian Salomon815486c2017-07-11 08:52:13 -0400308 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000309 return CombineResult::kCannotCombine;
joshualitt33a5fce2015-11-18 13:28:51 -0800310 }
311
msarett10e3d9b2016-08-18 15:46:03 -0700312 fPatches.move_back_n(that->fPatches.count(), that->fPatches.begin());
Brian Osman0b537032018-12-26 12:16:44 -0500313 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000314 return CombineResult::kMerged;
joshualitt33a5fce2015-11-18 13:28:51 -0800315 }
316
bsalomona71b8982016-06-30 12:13:52 -0700317 struct Patch {
318 SkMatrix fViewMatrix;
msarett10e3d9b2016-08-18 15:46:03 -0700319 std::unique_ptr<SkLatticeIter> fIter;
bsalomona71b8982016-06-30 12:13:52 -0700320 SkRect fDst;
Brian Osmancf860852018-10-31 14:04:39 -0400321 SkPMColor4f fColor;
bsalomona71b8982016-06-30 12:13:52 -0700322 };
323
Brian Salomon815486c2017-07-11 08:52:13 -0400324 Helper fHelper;
325 SkSTArray<1, Patch, true> fPatches;
Brian Salomon2a943df2018-05-04 13:43:19 -0400326 sk_sp<GrTextureProxy> fProxy;
327 sk_sp<GrColorSpaceXform> fColorSpaceXform;
328 GrSamplerState::Filter fFilter;
Brian Osman0b537032018-12-26 12:16:44 -0500329 bool fWideColor;
joshualitt33a5fce2015-11-18 13:28:51 -0800330
Brian Salomon815486c2017-07-11 08:52:13 -0400331 typedef GrMeshDrawOp INHERITED;
joshualitt33a5fce2015-11-18 13:28:51 -0800332};
333
Brian Salomon815486c2017-07-11 08:52:13 -0400334} // anonymous namespace
335
Brian Salomonfc527d22016-12-14 21:07:01 -0500336namespace GrLatticeOp {
Robert Phillipsb97da532019-02-12 15:24:12 -0500337std::unique_ptr<GrDrawOp> MakeNonAA(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400338 GrPaint&& paint,
339 const SkMatrix& viewMatrix,
Brian Salomon2a943df2018-05-04 13:43:19 -0400340 sk_sp<GrTextureProxy> proxy,
341 sk_sp<GrColorSpaceXform> colorSpaceXform,
342 GrSamplerState::Filter filter,
Robert Phillips7c525e62018-06-12 10:11:12 -0400343 std::unique_ptr<SkLatticeIter> iter,
344 const SkRect& dst) {
345 return NonAALatticeOp::Make(context, std::move(paint), viewMatrix, std::move(proxy),
Brian Salomon2a943df2018-05-04 13:43:19 -0400346 std::move(colorSpaceXform), filter, std::move(iter), dst);
joshualitt33a5fce2015-11-18 13:28:51 -0800347}
348};
Brian Salomon815486c2017-07-11 08:52:13 -0400349
350#if GR_TEST_UTILS
Brian Salomon2a943df2018-05-04 13:43:19 -0400351#include "GrProxyProvider.h"
Robert Phillipsb97da532019-02-12 15:24:12 -0500352#include "GrRecordingContextPriv.h"
Brian Salomon815486c2017-07-11 08:52:13 -0400353
354/** Randomly divides subset into count divs. */
355static void init_random_divs(int divs[], int count, int subsetStart, int subsetStop,
356 SkRandom* random) {
357 // Rules for lattice divs: Must be strictly increasing and in the range
358 // [subsetStart, subsetStop).
359 // Not terribly efficient alg for generating random divs:
360 // 1) Start with minimum legal pixels between each div.
361 // 2) Randomly assign the remaining pixels of the subset to divs.
362 // 3) Convert from pixel counts to div offsets.
363
364 // 1) Initially each divs[i] represents the number of pixels between
365 // div i-1 and i. The initial div is allowed to be at subsetStart. There
366 // must be one pixel spacing between subsequent divs.
367 divs[0] = 0;
368 for (int i = 1; i < count; ++i) {
369 divs[i] = 1;
370 }
371 // 2) Assign the remaining subset pixels to fall
372 int subsetLength = subsetStop - subsetStart;
373 for (int i = 0; i < subsetLength - count; ++i) {
374 // +1 because count divs means count+1 intervals.
375 int entry = random->nextULessThan(count + 1);
376 // We don't have an entry to to store the count after the last div
377 if (entry < count) {
378 divs[entry]++;
379 }
380 }
381 // 3) Now convert the counts between divs to pixel indices, incorporating the subset's offset.
382 int offset = subsetStart;
383 for (int i = 0; i < count; ++i) {
384 divs[i] += offset;
385 offset = divs[i];
386 }
387}
388
389GR_DRAW_OP_TEST_DEFINE(NonAALatticeOp) {
Brian Salomon815486c2017-07-11 08:52:13 -0400390 SkCanvas::Lattice lattice;
Brian Salomon18df7632017-07-11 14:32:18 -0400391 // We loop because our random lattice code can produce an invalid lattice in the case where
392 // there is a single div separator in both x and y and both are aligned with the left and top
393 // edge of the image subset, respectively.
394 std::unique_ptr<int[]> xdivs;
395 std::unique_ptr<int[]> ydivs;
Stan Ilievca8c0952017-12-11 13:01:58 -0500396 std::unique_ptr<SkCanvas::Lattice::RectType[]> flags;
397 std::unique_ptr<SkColor[]> colors;
Brian Salomon18df7632017-07-11 14:32:18 -0400398 SkIRect subset;
Brian Salomon2a943df2018-05-04 13:43:19 -0400399 GrSurfaceDesc desc;
400 desc.fConfig = kRGBA_8888_GrPixelConfig;
401 desc.fWidth = random->nextRangeU(1, 1000);
402 desc.fHeight = random->nextRangeU(1, 1000);
403 GrSurfaceOrigin origin =
404 random->nextBool() ? kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
Greg Daniel4065d452018-11-16 15:43:41 -0500405 const GrBackendFormat format =
Robert Phillips9da87e02019-02-04 13:26:26 -0500406 context->priv().caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
407 auto proxy = context->priv().proxyProvider()->createProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500408 format, desc, origin, SkBackingFit::kExact, SkBudgeted::kYes);
Brian Salomon2a943df2018-05-04 13:43:19 -0400409
Brian Salomon18df7632017-07-11 14:32:18 -0400410 do {
Brian Salomon18df7632017-07-11 14:32:18 -0400411 if (random->nextBool()) {
Brian Salomon2a943df2018-05-04 13:43:19 -0400412 subset.fLeft = random->nextULessThan(desc.fWidth);
413 subset.fRight = random->nextRangeU(subset.fLeft + 1, desc.fWidth);
414 subset.fTop = random->nextULessThan(desc.fHeight);
415 subset.fBottom = random->nextRangeU(subset.fTop + 1, desc.fHeight);
Brian Salomon18df7632017-07-11 14:32:18 -0400416 } else {
Brian Salomon2a943df2018-05-04 13:43:19 -0400417 subset.setXYWH(0, 0, desc.fWidth, desc.fHeight);
Brian Salomon815486c2017-07-11 08:52:13 -0400418 }
Brian Salomon2a943df2018-05-04 13:43:19 -0400419 // SkCanvas::Lattice allows bounds to be null. However, SkCanvas creates a temp Lattice with
420 // a non-null bounds before creating a SkLatticeIter since SkLatticeIter requires a bounds.
Brian Salomon18df7632017-07-11 14:32:18 -0400421 lattice.fBounds = &subset;
422 lattice.fXCount = random->nextRangeU(1, subset.width());
423 lattice.fYCount = random->nextRangeU(1, subset.height());
424 xdivs.reset(new int[lattice.fXCount]);
425 ydivs.reset(new int[lattice.fYCount]);
426 init_random_divs(xdivs.get(), lattice.fXCount, subset.fLeft, subset.fRight, random);
427 init_random_divs(ydivs.get(), lattice.fYCount, subset.fTop, subset.fBottom, random);
428 lattice.fXDivs = xdivs.get();
429 lattice.fYDivs = ydivs.get();
430 bool hasFlags = random->nextBool();
431 if (hasFlags) {
432 int n = (lattice.fXCount + 1) * (lattice.fYCount + 1);
Stan Ilievca8c0952017-12-11 13:01:58 -0500433 flags.reset(new SkCanvas::Lattice::RectType[n]);
434 colors.reset(new SkColor[n]);
Brian Salomon18df7632017-07-11 14:32:18 -0400435 for (int i = 0; i < n; ++i) {
Stan Ilievca8c0952017-12-11 13:01:58 -0500436 flags[i] = random->nextBool() ? SkCanvas::Lattice::kTransparent
437 : SkCanvas::Lattice::kDefault;
Brian Salomon18df7632017-07-11 14:32:18 -0400438 }
Stan Ilievca8c0952017-12-11 13:01:58 -0500439 lattice.fRectTypes = flags.get();
440 lattice.fColors = colors.get();
Brian Salomon18df7632017-07-11 14:32:18 -0400441 } else {
Stan Ilievca8c0952017-12-11 13:01:58 -0500442 lattice.fRectTypes = nullptr;
443 lattice.fColors = nullptr;
Brian Salomon18df7632017-07-11 14:32:18 -0400444 }
Brian Salomon2a943df2018-05-04 13:43:19 -0400445 } while (!SkLatticeIter::Valid(desc.fWidth, desc.fHeight, lattice));
Brian Salomon815486c2017-07-11 08:52:13 -0400446 SkRect dst;
447 dst.fLeft = random->nextRangeScalar(-2000.5f, 1000.f);
448 dst.fTop = random->nextRangeScalar(-2000.5f, 1000.f);
449 dst.fRight = dst.fLeft + random->nextRangeScalar(0.5f, 1000.f);
450 dst.fBottom = dst.fTop + random->nextRangeScalar(0.5f, 1000.f);
451 std::unique_ptr<SkLatticeIter> iter(new SkLatticeIter(lattice, dst));
452 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
Brian Salomon2a943df2018-05-04 13:43:19 -0400453 auto csxf = GrTest::TestColorXform(random);
454 GrSamplerState::Filter filter =
455 random->nextBool() ? GrSamplerState::Filter::kNearest : GrSamplerState::Filter::kBilerp;
Robert Phillips7c525e62018-06-12 10:11:12 -0400456 return NonAALatticeOp::Make(context, std::move(paint), viewMatrix, std::move(proxy),
457 std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -0400458}
459
460#endif