blob: 99e4477689ee1dd4fd511dafc7603a8795572f77 [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"
16#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"
21#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
22#include "src/gpu/glsl/GrGLSLVarying.h"
23#include "src/gpu/ops/GrLatticeOp.h"
24#include "src/gpu/ops/GrMeshDrawOp.h"
25#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
joshualitt33a5fce2015-11-18 13:28:51 -080026
Brian Salomon815486c2017-07-11 08:52:13 -040027namespace {
28
Brian Salomon2a943df2018-05-04 13:43:19 -040029class LatticeGP : public GrGeometryProcessor {
30public:
Greg Daniel7a82edf2018-12-04 10:54:34 -050031 static sk_sp<GrGeometryProcessor> Make(GrGpu* gpu,
32 const GrTextureProxy* proxy,
Brian Salomon2a943df2018-05-04 13:43:19 -040033 sk_sp<GrColorSpaceXform> csxf,
Brian Osman0b537032018-12-26 12:16:44 -050034 GrSamplerState::Filter filter,
35 bool wideColor) {
36 return sk_sp<GrGeometryProcessor>(
37 new LatticeGP(gpu, proxy, 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,
50 FPCoordTransformIter&& transformIter) override {
51 const auto& latticeGP = proc.cast<LatticeGP>();
52 this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter);
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);
80 args.fFragBuilder->appendTextureLookupAndModulate(
81 args.fOutputColor,
82 args.fTexSamplers[0],
83 "clamp(textureCoords, textureDomain.xy, textureDomain.zw)",
84 kFloat2_GrSLType,
85 &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:
Greg Daniel7a82edf2018-12-04 10:54:34 -050095 LatticeGP(GrGpu* gpu, const GrTextureProxy* proxy, sk_sp<GrColorSpaceXform> csxf,
Brian Osman0b537032018-12-26 12:16:44 -050096 GrSamplerState::Filter filter, bool wideColor)
Brian Salomon2a943df2018-05-04 13:43:19 -040097 : INHERITED(kLatticeGP_ClassID), 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),
100 proxy->backendFormat(), proxy->textureSwizzle());
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,
Brian Salomon2a943df2018-05-04 13:43:19 -0400132 sk_sp<GrTextureProxy> proxy,
Greg Danielc594e622019-10-15 14:01:49 -0400133 GrColorType srcColorType,
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) {
Brian Salomond3cee172018-05-07 16:40:48 -0400138 SkASSERT(proxy);
Robert Phillips7c525e62018-06-12 10:11:12 -0400139 return Helper::FactoryHelper<NonAALatticeOp>(context, std::move(paint), viewMatrix,
Greg Danielc594e622019-10-15 14:01:49 -0400140 std::move(proxy), srcColorType,
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,
146 const SkMatrix& viewMatrix, sk_sp<GrTextureProxy> proxy,
Greg Danielc594e622019-10-15 14:01:49 -0400147 GrColorType srcColorType, sk_sp<GrColorSpaceXform> colorSpaceXform,
148 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)
152 , fProxy(std::move(proxy))
Greg Danielc594e622019-10-15 14:01:49 -0400153 , fSrcColorType(srcColorType)
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 Dalton7eb5c0f2019-05-23 15:15:47 -0600169 bool mipped = (GrSamplerState::Filter::kMipMap == fFilter);
170 func(fProxy.get(), GrMipMapped(mipped));
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400171 fHelper.visitProxies(func);
172 }
173
Brian Osman9a390ac2018-11-12 09:47:48 -0500174#ifdef SK_DEBUG
robertphillips783a4da2015-11-19 14:00:02 -0800175 SkString dumpInfo() const override {
176 SkString str;
177
bsalomona71b8982016-06-30 12:13:52 -0700178 for (int i = 0; i < fPatches.count(); ++i) {
Brian Salomonfc527d22016-12-14 21:07:01 -0500179 str.appendf("%d: Color: 0x%08x Dst [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", i,
Brian Osmancf860852018-10-31 14:04:39 -0400180 fPatches[i].fColor.toBytes_RGBA(), fPatches[i].fDst.fLeft,
Brian Osman1be2b7c2018-10-29 16:07:15 -0400181 fPatches[i].fDst.fTop, fPatches[i].fDst.fRight, fPatches[i].fDst.fBottom);
robertphillips783a4da2015-11-19 14:00:02 -0800182 }
183
Brian Salomon815486c2017-07-11 08:52:13 -0400184 str += fHelper.dumpInfo();
185 str += INHERITED::dumpInfo();
robertphillips783a4da2015-11-19 14:00:02 -0800186 return str;
187 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500188#endif
joshualitt33a5fce2015-11-18 13:28:51 -0800189
Brian Salomon815486c2017-07-11 08:52:13 -0400190 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
191
Chris Dalton6ce447a2019-06-23 18:07:38 -0600192 GrProcessorSet::Analysis finalize(
193 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
194 GrClampType clampType) override {
Greg Danielc594e622019-10-15 14:01:49 -0400195 auto opaque = fPatches[0].fColor.isOpaque() && !GrColorTypeHasAlpha(fSrcColorType)
Brian Salomon2a943df2018-05-04 13:43:19 -0400196 ? GrProcessorAnalysisColor::Opaque::kYes
197 : GrProcessorAnalysisColor::Opaque::kNo;
198 auto analysisColor = GrProcessorAnalysisColor(opaque);
Chris Dalton6ce447a2019-06-23 18:07:38 -0600199 auto result = fHelper.finalizeProcessors(
200 caps, clip, hasMixedSampledCoverage, clampType, GrProcessorAnalysisCoverage::kNone,
201 &analysisColor);
Brian Salomon2a943df2018-05-04 13:43:19 -0400202 analysisColor.isConstant(&fPatches[0].fColor);
Brian Osman8fa7ab42019-03-18 10:22:42 -0400203 fWideColor = SkPMColor4fNeedsWideColor(fPatches[0].fColor, clampType, caps);
Brian Salomon2a943df2018-05-04 13:43:19 -0400204 return result;
Brian Salomon815486c2017-07-11 08:52:13 -0400205 }
206
Brian Salomon92aee3d2016-12-21 09:20:25 -0500207private:
Brian Salomon91326c32017-08-09 16:02:19 -0400208 void onPrepareDraws(Target* target) override {
Greg Daniel7a82edf2018-12-04 10:54:34 -0500209 GrGpu* gpu = target->resourceProvider()->priv().gpu();
Brian Osman0b537032018-12-26 12:16:44 -0500210 auto gp = LatticeGP::Make(gpu, fProxy.get(), fColorSpaceXform, fFilter, fWideColor);
joshualitt33a5fce2015-11-18 13:28:51 -0800211 if (!gp) {
212 SkDebugf("Couldn't create GrGeometryProcessor\n");
213 return;
214 }
215
bsalomona71b8982016-06-30 12:13:52 -0700216 int patchCnt = fPatches.count();
msarett10e3d9b2016-08-18 15:46:03 -0700217 int numRects = 0;
218 for (int i = 0; i < patchCnt; i++) {
msarett0764efe2016-09-02 11:24:30 -0700219 numRects += fPatches[i].fIter->numRectsToDraw();
msarett10e3d9b2016-08-18 15:46:03 -0700220 }
joshualitt33a5fce2015-11-18 13:28:51 -0800221
Brian Salomon0db1b532017-07-12 15:21:43 -0400222 if (!numRects) {
223 return;
224 }
225
Brian Osmanc3064e72018-11-15 08:59:22 -0500226 const size_t kVertexStride = gp->vertexStride();
Robert Phillipse94cdd22019-11-04 14:15:58 -0500227
228 QuadHelper helper(target, kVertexStride, numRects);
229
Brian Osmanc3064e72018-11-15 08:59:22 -0500230 GrVertexWriter vertices{helper.vertices()};
Brian Salomon12d22642019-01-29 14:38:50 -0500231 if (!vertices.fPtr) {
joshualitt33a5fce2015-11-18 13:28:51 -0800232 SkDebugf("Could not allocate vertices\n");
233 return;
234 }
235
bsalomona71b8982016-06-30 12:13:52 -0700236 for (int i = 0; i < patchCnt; i++) {
msarett7fc08582016-08-18 14:29:22 -0700237 const Patch& patch = fPatches[i];
Brian Osmanc3064e72018-11-15 08:59:22 -0500238
Brian Osman0b537032018-12-26 12:16:44 -0500239 GrVertexColor patchColor(patch.fColor, fWideColor);
msarett10e3d9b2016-08-18 15:46:03 -0700240
241 // Apply the view matrix here if it is scale-translate. Otherwise, we need to
242 // wait until we've created the dst rects.
243 bool isScaleTranslate = patch.fViewMatrix.isScaleTranslate();
244 if (isScaleTranslate) {
245 patch.fIter->mapDstScaleTranslate(patch.fViewMatrix);
246 }
joshualitt33a5fce2015-11-18 13:28:51 -0800247
Brian Salomon2a943df2018-05-04 13:43:19 -0400248 SkIRect srcR;
249 SkRect dstR;
Brian Osmanc3064e72018-11-15 08:59:22 -0500250 SkPoint* patchPositions = reinterpret_cast<SkPoint*>(vertices.fPtr);
Brian Salomon2a943df2018-05-04 13:43:19 -0400251 Sk4f scales(1.f / fProxy->width(), 1.f / fProxy->height(),
252 1.f / fProxy->width(), 1.f / fProxy->height());
253 static const Sk4f kDomainOffsets(0.5f, 0.5f, -0.5f, -0.5f);
Brian Osmanc3064e72018-11-15 08:59:22 -0500254 static const Sk4f kFlipOffsets(0.f, 1.f, 0.f, 1.f);
Brian Salomon2a943df2018-05-04 13:43:19 -0400255 static const Sk4f kFlipMuls(1.f, -1.f, 1.f, -1.f);
msarett10e3d9b2016-08-18 15:46:03 -0700256 while (patch.fIter->next(&srcR, &dstR)) {
Brian Salomon2a943df2018-05-04 13:43:19 -0400257 Sk4f coords(SkIntToScalar(srcR.fLeft), SkIntToScalar(srcR.fTop),
258 SkIntToScalar(srcR.fRight), SkIntToScalar(srcR.fBottom));
259 Sk4f domain = coords + kDomainOffsets;
260 coords *= scales;
261 domain *= scales;
262 if (fProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
263 coords = kFlipMuls * coords + kFlipOffsets;
264 domain = SkNx_shuffle<0, 3, 2, 1>(kFlipMuls * domain + kFlipOffsets);
265 }
Brian Osman4486d982018-11-15 15:56:04 -0500266 SkRect texDomain;
267 SkRect texCoords;
268 domain.store(&texDomain);
269 coords.store(&texCoords);
joshualitt33a5fce2015-11-18 13:28:51 -0800270
Brian Osman0dd43022018-11-16 15:53:26 -0500271 vertices.writeQuad(GrVertexWriter::TriStripFromRect(dstR),
272 GrVertexWriter::TriStripFromRect(texCoords),
Brian Osmanc3064e72018-11-15 08:59:22 -0500273 texDomain,
274 patchColor);
joshualitt33a5fce2015-11-18 13:28:51 -0800275 }
msarett10e3d9b2016-08-18 15:46:03 -0700276
277 // If we didn't handle it above, apply the matrix here.
278 if (!isScaleTranslate) {
Robert Phillipsee08d522019-10-28 16:34:44 -0400279 SkMatrixPriv::MapPointsWithStride(
280 patch.fViewMatrix, patchPositions, kVertexStride,
281 GrResourceProvider::NumVertsPerNonAAQuad() * patch.fIter->numRectsToDraw());
msarett10e3d9b2016-08-18 15:46:03 -0700282 }
joshualitt33a5fce2015-11-18 13:28:51 -0800283 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700284 auto fixedDynamicState = target->makeFixedDynamicState(1);
285 fixedDynamicState->fPrimitiveProcessorTextures[0] = fProxy.get();
286 helper.recordDraw(target, std::move(gp), fixedDynamicState);
287 }
288
289 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
290 fHelper.executeDrawsAndUploads(this, flushState, chainBounds);
joshualitt33a5fce2015-11-18 13:28:51 -0800291 }
292
Brian Salomon7eae3e02018-08-07 14:02:38 +0000293 CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Brian Salomonfc527d22016-12-14 21:07:01 -0500294 NonAALatticeOp* that = t->cast<NonAALatticeOp>();
Brian Salomon2a943df2018-05-04 13:43:19 -0400295 if (fProxy != that->fProxy) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000296 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400297 }
298 if (fFilter != that->fFilter) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000299 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400300 }
301 if (GrColorSpaceXform::Equals(fColorSpaceXform.get(), that->fColorSpaceXform.get())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000302 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400303 }
Brian Salomon815486c2017-07-11 08:52:13 -0400304 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000305 return CombineResult::kCannotCombine;
joshualitt33a5fce2015-11-18 13:28:51 -0800306 }
307
msarett10e3d9b2016-08-18 15:46:03 -0700308 fPatches.move_back_n(that->fPatches.count(), that->fPatches.begin());
Brian Osman0b537032018-12-26 12:16:44 -0500309 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000310 return CombineResult::kMerged;
joshualitt33a5fce2015-11-18 13:28:51 -0800311 }
312
bsalomona71b8982016-06-30 12:13:52 -0700313 struct Patch {
314 SkMatrix fViewMatrix;
msarett10e3d9b2016-08-18 15:46:03 -0700315 std::unique_ptr<SkLatticeIter> fIter;
bsalomona71b8982016-06-30 12:13:52 -0700316 SkRect fDst;
Brian Osmancf860852018-10-31 14:04:39 -0400317 SkPMColor4f fColor;
bsalomona71b8982016-06-30 12:13:52 -0700318 };
319
Brian Salomon815486c2017-07-11 08:52:13 -0400320 Helper fHelper;
321 SkSTArray<1, Patch, true> fPatches;
Brian Salomon2a943df2018-05-04 13:43:19 -0400322 sk_sp<GrTextureProxy> fProxy;
Greg Danielc594e622019-10-15 14:01:49 -0400323 GrColorType fSrcColorType;
Brian Salomon2a943df2018-05-04 13:43:19 -0400324 sk_sp<GrColorSpaceXform> fColorSpaceXform;
325 GrSamplerState::Filter fFilter;
Brian Osman0b537032018-12-26 12:16:44 -0500326 bool fWideColor;
joshualitt33a5fce2015-11-18 13:28:51 -0800327
Brian Salomon815486c2017-07-11 08:52:13 -0400328 typedef GrMeshDrawOp INHERITED;
joshualitt33a5fce2015-11-18 13:28:51 -0800329};
330
Brian Salomon815486c2017-07-11 08:52:13 -0400331} // anonymous namespace
332
Brian Salomonfc527d22016-12-14 21:07:01 -0500333namespace GrLatticeOp {
Robert Phillipsb97da532019-02-12 15:24:12 -0500334std::unique_ptr<GrDrawOp> MakeNonAA(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400335 GrPaint&& paint,
336 const SkMatrix& viewMatrix,
Brian Salomon2a943df2018-05-04 13:43:19 -0400337 sk_sp<GrTextureProxy> proxy,
Greg Danielc594e622019-10-15 14:01:49 -0400338 GrColorType srcColorType,
Brian Salomon2a943df2018-05-04 13:43:19 -0400339 sk_sp<GrColorSpaceXform> colorSpaceXform,
340 GrSamplerState::Filter filter,
Robert Phillips7c525e62018-06-12 10:11:12 -0400341 std::unique_ptr<SkLatticeIter> iter,
342 const SkRect& dst) {
343 return NonAALatticeOp::Make(context, std::move(paint), viewMatrix, std::move(proxy),
Greg Danielc594e622019-10-15 14:01:49 -0400344 srcColorType, std::move(colorSpaceXform), filter, std::move(iter),
345 dst);
joshualitt33a5fce2015-11-18 13:28:51 -0800346}
347};
Brian Salomon815486c2017-07-11 08:52:13 -0400348
349#if GR_TEST_UTILS
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500350#include "src/gpu/GrProxyProvider.h"
351#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon815486c2017-07-11 08:52:13 -0400352
353/** Randomly divides subset into count divs. */
354static void init_random_divs(int divs[], int count, int subsetStart, int subsetStop,
355 SkRandom* random) {
356 // Rules for lattice divs: Must be strictly increasing and in the range
357 // [subsetStart, subsetStop).
358 // Not terribly efficient alg for generating random divs:
359 // 1) Start with minimum legal pixels between each div.
360 // 2) Randomly assign the remaining pixels of the subset to divs.
361 // 3) Convert from pixel counts to div offsets.
362
363 // 1) Initially each divs[i] represents the number of pixels between
364 // div i-1 and i. The initial div is allowed to be at subsetStart. There
365 // must be one pixel spacing between subsequent divs.
366 divs[0] = 0;
367 for (int i = 1; i < count; ++i) {
368 divs[i] = 1;
369 }
370 // 2) Assign the remaining subset pixels to fall
371 int subsetLength = subsetStop - subsetStart;
372 for (int i = 0; i < subsetLength - count; ++i) {
373 // +1 because count divs means count+1 intervals.
374 int entry = random->nextULessThan(count + 1);
375 // We don't have an entry to to store the count after the last div
376 if (entry < count) {
377 divs[entry]++;
378 }
379 }
380 // 3) Now convert the counts between divs to pixel indices, incorporating the subset's offset.
381 int offset = subsetStart;
382 for (int i = 0; i < count; ++i) {
383 divs[i] += offset;
384 offset = divs[i];
385 }
386}
387
388GR_DRAW_OP_TEST_DEFINE(NonAALatticeOp) {
Brian Salomon815486c2017-07-11 08:52:13 -0400389 SkCanvas::Lattice lattice;
Brian Salomon18df7632017-07-11 14:32:18 -0400390 // We loop because our random lattice code can produce an invalid lattice in the case where
391 // there is a single div separator in both x and y and both are aligned with the left and top
392 // edge of the image subset, respectively.
393 std::unique_ptr<int[]> xdivs;
394 std::unique_ptr<int[]> ydivs;
Stan Ilievca8c0952017-12-11 13:01:58 -0500395 std::unique_ptr<SkCanvas::Lattice::RectType[]> flags;
396 std::unique_ptr<SkColor[]> colors;
Brian Salomon18df7632017-07-11 14:32:18 -0400397 SkIRect subset;
Brian Salomon2a943df2018-05-04 13:43:19 -0400398 GrSurfaceDesc desc;
399 desc.fConfig = kRGBA_8888_GrPixelConfig;
400 desc.fWidth = random->nextRangeU(1, 1000);
401 desc.fHeight = random->nextRangeU(1, 1000);
Robert Phillips0a15cc62019-07-30 12:49:10 -0400402 GrSurfaceOrigin origin = random->nextBool() ? kTopLeft_GrSurfaceOrigin
403 : kBottomLeft_GrSurfaceOrigin;
Greg Daniel4065d452018-11-16 15:43:41 -0500404 const GrBackendFormat format =
Robert Phillips0a15cc62019-07-30 12:49:10 -0400405 context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
406 GrRenderable::kNo);
Brian Salomonbeb7f522019-08-30 16:19:42 -0400407 auto proxy = context->priv().proxyProvider()->createProxy(format,
408 desc,
409 GrRenderable::kNo,
410 1,
411 origin,
412 GrMipMapped::kNo,
413 SkBackingFit::kExact,
414 SkBudgeted::kYes,
415 GrProtected::kNo);
Brian Salomon2a943df2018-05-04 13:43:19 -0400416
Brian Salomon18df7632017-07-11 14:32:18 -0400417 do {
Brian Salomon18df7632017-07-11 14:32:18 -0400418 if (random->nextBool()) {
Brian Salomon2a943df2018-05-04 13:43:19 -0400419 subset.fLeft = random->nextULessThan(desc.fWidth);
420 subset.fRight = random->nextRangeU(subset.fLeft + 1, desc.fWidth);
421 subset.fTop = random->nextULessThan(desc.fHeight);
422 subset.fBottom = random->nextRangeU(subset.fTop + 1, desc.fHeight);
Brian Salomon18df7632017-07-11 14:32:18 -0400423 } else {
Brian Salomon2a943df2018-05-04 13:43:19 -0400424 subset.setXYWH(0, 0, desc.fWidth, desc.fHeight);
Brian Salomon815486c2017-07-11 08:52:13 -0400425 }
Brian Salomon2a943df2018-05-04 13:43:19 -0400426 // SkCanvas::Lattice allows bounds to be null. However, SkCanvas creates a temp Lattice with
427 // a non-null bounds before creating a SkLatticeIter since SkLatticeIter requires a bounds.
Brian Salomon18df7632017-07-11 14:32:18 -0400428 lattice.fBounds = &subset;
429 lattice.fXCount = random->nextRangeU(1, subset.width());
430 lattice.fYCount = random->nextRangeU(1, subset.height());
431 xdivs.reset(new int[lattice.fXCount]);
432 ydivs.reset(new int[lattice.fYCount]);
433 init_random_divs(xdivs.get(), lattice.fXCount, subset.fLeft, subset.fRight, random);
434 init_random_divs(ydivs.get(), lattice.fYCount, subset.fTop, subset.fBottom, random);
435 lattice.fXDivs = xdivs.get();
436 lattice.fYDivs = ydivs.get();
437 bool hasFlags = random->nextBool();
438 if (hasFlags) {
439 int n = (lattice.fXCount + 1) * (lattice.fYCount + 1);
Stan Ilievca8c0952017-12-11 13:01:58 -0500440 flags.reset(new SkCanvas::Lattice::RectType[n]);
441 colors.reset(new SkColor[n]);
Brian Salomon18df7632017-07-11 14:32:18 -0400442 for (int i = 0; i < n; ++i) {
Stan Ilievca8c0952017-12-11 13:01:58 -0500443 flags[i] = random->nextBool() ? SkCanvas::Lattice::kTransparent
444 : SkCanvas::Lattice::kDefault;
Brian Salomon18df7632017-07-11 14:32:18 -0400445 }
Stan Ilievca8c0952017-12-11 13:01:58 -0500446 lattice.fRectTypes = flags.get();
447 lattice.fColors = colors.get();
Brian Salomon18df7632017-07-11 14:32:18 -0400448 } else {
Stan Ilievca8c0952017-12-11 13:01:58 -0500449 lattice.fRectTypes = nullptr;
450 lattice.fColors = nullptr;
Brian Salomon18df7632017-07-11 14:32:18 -0400451 }
Brian Salomon2a943df2018-05-04 13:43:19 -0400452 } while (!SkLatticeIter::Valid(desc.fWidth, desc.fHeight, lattice));
Brian Salomon815486c2017-07-11 08:52:13 -0400453 SkRect dst;
454 dst.fLeft = random->nextRangeScalar(-2000.5f, 1000.f);
455 dst.fTop = random->nextRangeScalar(-2000.5f, 1000.f);
456 dst.fRight = dst.fLeft + random->nextRangeScalar(0.5f, 1000.f);
457 dst.fBottom = dst.fTop + random->nextRangeScalar(0.5f, 1000.f);
458 std::unique_ptr<SkLatticeIter> iter(new SkLatticeIter(lattice, dst));
459 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
Brian Salomon2a943df2018-05-04 13:43:19 -0400460 auto csxf = GrTest::TestColorXform(random);
461 GrSamplerState::Filter filter =
462 random->nextBool() ? GrSamplerState::Filter::kNearest : GrSamplerState::Filter::kBilerp;
Robert Phillips7c525e62018-06-12 10:11:12 -0400463 return NonAALatticeOp::Make(context, std::move(paint), viewMatrix, std::move(proxy),
Greg Danielc594e622019-10-15 14:01:49 -0400464 GrColorType::kRGBA_8888, std::move(csxf), filter, std::move(iter),
465 dst);
Brian Salomon815486c2017-07-11 08:52:13 -0400466}
467
468#endif