blob: bb98f355fc9fce352ccc03f08124cb304d4402a0 [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 Phillipsf272bea2019-10-17 08:56:16 -040099 GrSamplerState samplerState = GrSamplerState(GrSamplerState::WrapMode::kClamp, filter);
Greg Daniel7a82edf2018-12-04 10:54:34 -0500100 uint32_t extraSamplerKey = gpu->getExtraSamplerKeyForProgram(samplerState,
101 proxy->backendFormat());
102
Robert Phillipsf272bea2019-10-17 08:56:16 -0400103 fSampler.reset(samplerState, proxy->backendFormat(), proxy->textureSwizzle(),
Greg Daniel7a82edf2018-12-04 10:54:34 -0500104 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
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,
Brian Salomon2a943df2018-05-04 13:43:19 -0400136 sk_sp<GrTextureProxy> proxy,
Greg Danielc594e622019-10-15 14:01:49 -0400137 GrColorType srcColorType,
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) {
Brian Salomond3cee172018-05-07 16:40:48 -0400142 SkASSERT(proxy);
Robert Phillips7c525e62018-06-12 10:11:12 -0400143 return Helper::FactoryHelper<NonAALatticeOp>(context, std::move(paint), viewMatrix,
Greg Danielc594e622019-10-15 14:01:49 -0400144 std::move(proxy), srcColorType,
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,
150 const SkMatrix& viewMatrix, sk_sp<GrTextureProxy> proxy,
Greg Danielc594e622019-10-15 14:01:49 -0400151 GrColorType srcColorType, sk_sp<GrColorSpaceXform> colorSpaceXform,
152 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)
156 , fProxy(std::move(proxy))
Greg Danielc594e622019-10-15 14:01:49 -0400157 , fSrcColorType(srcColorType)
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 {
Chris Dalton7eb5c0f2019-05-23 15:15:47 -0600173 bool mipped = (GrSamplerState::Filter::kMipMap == fFilter);
174 func(fProxy.get(), GrMipMapped(mipped));
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 Dalton6ce447a2019-06-23 18:07:38 -0600196 GrProcessorSet::Analysis finalize(
197 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
198 GrClampType clampType) override {
Greg Danielc594e622019-10-15 14:01:49 -0400199 auto opaque = fPatches[0].fColor.isOpaque() && !GrColorTypeHasAlpha(fSrcColorType)
Brian Salomon2a943df2018-05-04 13:43:19 -0400200 ? GrProcessorAnalysisColor::Opaque::kYes
201 : GrProcessorAnalysisColor::Opaque::kNo;
202 auto analysisColor = GrProcessorAnalysisColor(opaque);
Chris Dalton6ce447a2019-06-23 18:07:38 -0600203 auto result = fHelper.finalizeProcessors(
204 caps, clip, hasMixedSampledCoverage, clampType, GrProcessorAnalysisCoverage::kNone,
205 &analysisColor);
Brian Salomon2a943df2018-05-04 13:43:19 -0400206 analysisColor.isConstant(&fPatches[0].fColor);
Brian Osman8fa7ab42019-03-18 10:22:42 -0400207 fWideColor = SkPMColor4fNeedsWideColor(fPatches[0].fColor, clampType, caps);
Brian Salomon2a943df2018-05-04 13:43:19 -0400208 return result;
Brian Salomon815486c2017-07-11 08:52:13 -0400209 }
210
Brian Salomon92aee3d2016-12-21 09:20:25 -0500211private:
Brian Salomon91326c32017-08-09 16:02:19 -0400212 void onPrepareDraws(Target* target) override {
Greg Daniel7a82edf2018-12-04 10:54:34 -0500213 GrGpu* gpu = target->resourceProvider()->priv().gpu();
Brian Osman0b537032018-12-26 12:16:44 -0500214 auto gp = LatticeGP::Make(gpu, fProxy.get(), fColorSpaceXform, fFilter, fWideColor);
joshualitt33a5fce2015-11-18 13:28:51 -0800215 if (!gp) {
216 SkDebugf("Couldn't create GrGeometryProcessor\n");
217 return;
218 }
219
bsalomona71b8982016-06-30 12:13:52 -0700220 int patchCnt = fPatches.count();
msarett10e3d9b2016-08-18 15:46:03 -0700221 int numRects = 0;
222 for (int i = 0; i < patchCnt; i++) {
msarett0764efe2016-09-02 11:24:30 -0700223 numRects += fPatches[i].fIter->numRectsToDraw();
msarett10e3d9b2016-08-18 15:46:03 -0700224 }
joshualitt33a5fce2015-11-18 13:28:51 -0800225
Brian Salomon0db1b532017-07-12 15:21:43 -0400226 if (!numRects) {
227 return;
228 }
229
Brian Osmanc3064e72018-11-15 08:59:22 -0500230 const size_t kVertexStride = gp->vertexStride();
Robert Phillipse94cdd22019-11-04 14:15:58 -0500231
232 QuadHelper helper(target, kVertexStride, numRects);
233
Brian Osmanc3064e72018-11-15 08:59:22 -0500234 GrVertexWriter vertices{helper.vertices()};
Brian Salomon12d22642019-01-29 14:38:50 -0500235 if (!vertices.fPtr) {
joshualitt33a5fce2015-11-18 13:28:51 -0800236 SkDebugf("Could not allocate vertices\n");
237 return;
238 }
239
bsalomona71b8982016-06-30 12:13:52 -0700240 for (int i = 0; i < patchCnt; i++) {
msarett7fc08582016-08-18 14:29:22 -0700241 const Patch& patch = fPatches[i];
Brian Osmanc3064e72018-11-15 08:59:22 -0500242
Brian Osman0b537032018-12-26 12:16:44 -0500243 GrVertexColor patchColor(patch.fColor, fWideColor);
msarett10e3d9b2016-08-18 15:46:03 -0700244
245 // Apply the view matrix here if it is scale-translate. Otherwise, we need to
246 // wait until we've created the dst rects.
247 bool isScaleTranslate = patch.fViewMatrix.isScaleTranslate();
248 if (isScaleTranslate) {
249 patch.fIter->mapDstScaleTranslate(patch.fViewMatrix);
250 }
joshualitt33a5fce2015-11-18 13:28:51 -0800251
Brian Salomon2a943df2018-05-04 13:43:19 -0400252 SkIRect srcR;
253 SkRect dstR;
Brian Osmanc3064e72018-11-15 08:59:22 -0500254 SkPoint* patchPositions = reinterpret_cast<SkPoint*>(vertices.fPtr);
Brian Salomon2a943df2018-05-04 13:43:19 -0400255 Sk4f scales(1.f / fProxy->width(), 1.f / fProxy->height(),
256 1.f / fProxy->width(), 1.f / fProxy->height());
257 static const Sk4f kDomainOffsets(0.5f, 0.5f, -0.5f, -0.5f);
Brian Osmanc3064e72018-11-15 08:59:22 -0500258 static const Sk4f kFlipOffsets(0.f, 1.f, 0.f, 1.f);
Brian Salomon2a943df2018-05-04 13:43:19 -0400259 static const Sk4f kFlipMuls(1.f, -1.f, 1.f, -1.f);
msarett10e3d9b2016-08-18 15:46:03 -0700260 while (patch.fIter->next(&srcR, &dstR)) {
Brian Salomon2a943df2018-05-04 13:43:19 -0400261 Sk4f coords(SkIntToScalar(srcR.fLeft), SkIntToScalar(srcR.fTop),
262 SkIntToScalar(srcR.fRight), SkIntToScalar(srcR.fBottom));
263 Sk4f domain = coords + kDomainOffsets;
264 coords *= scales;
265 domain *= scales;
266 if (fProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
267 coords = kFlipMuls * coords + kFlipOffsets;
268 domain = SkNx_shuffle<0, 3, 2, 1>(kFlipMuls * domain + kFlipOffsets);
269 }
Brian Osman4486d982018-11-15 15:56:04 -0500270 SkRect texDomain;
271 SkRect texCoords;
272 domain.store(&texDomain);
273 coords.store(&texCoords);
joshualitt33a5fce2015-11-18 13:28:51 -0800274
Brian Osman0dd43022018-11-16 15:53:26 -0500275 vertices.writeQuad(GrVertexWriter::TriStripFromRect(dstR),
276 GrVertexWriter::TriStripFromRect(texCoords),
Brian Osmanc3064e72018-11-15 08:59:22 -0500277 texDomain,
278 patchColor);
joshualitt33a5fce2015-11-18 13:28:51 -0800279 }
msarett10e3d9b2016-08-18 15:46:03 -0700280
281 // If we didn't handle it above, apply the matrix here.
282 if (!isScaleTranslate) {
Robert Phillipsee08d522019-10-28 16:34:44 -0400283 SkMatrixPriv::MapPointsWithStride(
284 patch.fViewMatrix, patchPositions, kVertexStride,
285 GrResourceProvider::NumVertsPerNonAAQuad() * 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;
Greg Danielc594e622019-10-15 14:01:49 -0400327 GrColorType fSrcColorType;
Brian Salomon2a943df2018-05-04 13:43:19 -0400328 sk_sp<GrColorSpaceXform> fColorSpaceXform;
329 GrSamplerState::Filter fFilter;
Brian Osman0b537032018-12-26 12:16:44 -0500330 bool fWideColor;
joshualitt33a5fce2015-11-18 13:28:51 -0800331
Brian Salomon815486c2017-07-11 08:52:13 -0400332 typedef GrMeshDrawOp INHERITED;
joshualitt33a5fce2015-11-18 13:28:51 -0800333};
334
Brian Salomon815486c2017-07-11 08:52:13 -0400335} // anonymous namespace
336
Brian Salomonfc527d22016-12-14 21:07:01 -0500337namespace GrLatticeOp {
Robert Phillipsb97da532019-02-12 15:24:12 -0500338std::unique_ptr<GrDrawOp> MakeNonAA(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400339 GrPaint&& paint,
340 const SkMatrix& viewMatrix,
Brian Salomon2a943df2018-05-04 13:43:19 -0400341 sk_sp<GrTextureProxy> proxy,
Greg Danielc594e622019-10-15 14:01:49 -0400342 GrColorType srcColorType,
Brian Salomon2a943df2018-05-04 13:43:19 -0400343 sk_sp<GrColorSpaceXform> colorSpaceXform,
344 GrSamplerState::Filter filter,
Robert Phillips7c525e62018-06-12 10:11:12 -0400345 std::unique_ptr<SkLatticeIter> iter,
346 const SkRect& dst) {
347 return NonAALatticeOp::Make(context, std::move(paint), viewMatrix, std::move(proxy),
Greg Danielc594e622019-10-15 14:01:49 -0400348 srcColorType, std::move(colorSpaceXform), filter, std::move(iter),
349 dst);
joshualitt33a5fce2015-11-18 13:28:51 -0800350}
351};
Brian Salomon815486c2017-07-11 08:52:13 -0400352
353#if GR_TEST_UTILS
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500354#include "src/gpu/GrProxyProvider.h"
355#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon815486c2017-07-11 08:52:13 -0400356
357/** Randomly divides subset into count divs. */
358static void init_random_divs(int divs[], int count, int subsetStart, int subsetStop,
359 SkRandom* random) {
360 // Rules for lattice divs: Must be strictly increasing and in the range
361 // [subsetStart, subsetStop).
362 // Not terribly efficient alg for generating random divs:
363 // 1) Start with minimum legal pixels between each div.
364 // 2) Randomly assign the remaining pixels of the subset to divs.
365 // 3) Convert from pixel counts to div offsets.
366
367 // 1) Initially each divs[i] represents the number of pixels between
368 // div i-1 and i. The initial div is allowed to be at subsetStart. There
369 // must be one pixel spacing between subsequent divs.
370 divs[0] = 0;
371 for (int i = 1; i < count; ++i) {
372 divs[i] = 1;
373 }
374 // 2) Assign the remaining subset pixels to fall
375 int subsetLength = subsetStop - subsetStart;
376 for (int i = 0; i < subsetLength - count; ++i) {
377 // +1 because count divs means count+1 intervals.
378 int entry = random->nextULessThan(count + 1);
379 // We don't have an entry to to store the count after the last div
380 if (entry < count) {
381 divs[entry]++;
382 }
383 }
384 // 3) Now convert the counts between divs to pixel indices, incorporating the subset's offset.
385 int offset = subsetStart;
386 for (int i = 0; i < count; ++i) {
387 divs[i] += offset;
388 offset = divs[i];
389 }
390}
391
392GR_DRAW_OP_TEST_DEFINE(NonAALatticeOp) {
Brian Salomon815486c2017-07-11 08:52:13 -0400393 SkCanvas::Lattice lattice;
Brian Salomon18df7632017-07-11 14:32:18 -0400394 // We loop because our random lattice code can produce an invalid lattice in the case where
395 // there is a single div separator in both x and y and both are aligned with the left and top
396 // edge of the image subset, respectively.
397 std::unique_ptr<int[]> xdivs;
398 std::unique_ptr<int[]> ydivs;
Stan Ilievca8c0952017-12-11 13:01:58 -0500399 std::unique_ptr<SkCanvas::Lattice::RectType[]> flags;
400 std::unique_ptr<SkColor[]> colors;
Brian Salomon18df7632017-07-11 14:32:18 -0400401 SkIRect subset;
Brian Salomon2a943df2018-05-04 13:43:19 -0400402 GrSurfaceDesc desc;
403 desc.fConfig = kRGBA_8888_GrPixelConfig;
404 desc.fWidth = random->nextRangeU(1, 1000);
405 desc.fHeight = random->nextRangeU(1, 1000);
Robert Phillips0a15cc62019-07-30 12:49:10 -0400406 GrSurfaceOrigin origin = random->nextBool() ? kTopLeft_GrSurfaceOrigin
407 : kBottomLeft_GrSurfaceOrigin;
Greg Daniel4065d452018-11-16 15:43:41 -0500408 const GrBackendFormat format =
Robert Phillips0a15cc62019-07-30 12:49:10 -0400409 context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
410 GrRenderable::kNo);
Brian Salomonbeb7f522019-08-30 16:19:42 -0400411 auto proxy = context->priv().proxyProvider()->createProxy(format,
412 desc,
413 GrRenderable::kNo,
414 1,
415 origin,
416 GrMipMapped::kNo,
417 SkBackingFit::kExact,
418 SkBudgeted::kYes,
419 GrProtected::kNo);
Brian Salomon2a943df2018-05-04 13:43:19 -0400420
Brian Salomon18df7632017-07-11 14:32:18 -0400421 do {
Brian Salomon18df7632017-07-11 14:32:18 -0400422 if (random->nextBool()) {
Brian Salomon2a943df2018-05-04 13:43:19 -0400423 subset.fLeft = random->nextULessThan(desc.fWidth);
424 subset.fRight = random->nextRangeU(subset.fLeft + 1, desc.fWidth);
425 subset.fTop = random->nextULessThan(desc.fHeight);
426 subset.fBottom = random->nextRangeU(subset.fTop + 1, desc.fHeight);
Brian Salomon18df7632017-07-11 14:32:18 -0400427 } else {
Brian Salomon2a943df2018-05-04 13:43:19 -0400428 subset.setXYWH(0, 0, desc.fWidth, desc.fHeight);
Brian Salomon815486c2017-07-11 08:52:13 -0400429 }
Brian Salomon2a943df2018-05-04 13:43:19 -0400430 // SkCanvas::Lattice allows bounds to be null. However, SkCanvas creates a temp Lattice with
431 // a non-null bounds before creating a SkLatticeIter since SkLatticeIter requires a bounds.
Brian Salomon18df7632017-07-11 14:32:18 -0400432 lattice.fBounds = &subset;
433 lattice.fXCount = random->nextRangeU(1, subset.width());
434 lattice.fYCount = random->nextRangeU(1, subset.height());
435 xdivs.reset(new int[lattice.fXCount]);
436 ydivs.reset(new int[lattice.fYCount]);
437 init_random_divs(xdivs.get(), lattice.fXCount, subset.fLeft, subset.fRight, random);
438 init_random_divs(ydivs.get(), lattice.fYCount, subset.fTop, subset.fBottom, random);
439 lattice.fXDivs = xdivs.get();
440 lattice.fYDivs = ydivs.get();
441 bool hasFlags = random->nextBool();
442 if (hasFlags) {
443 int n = (lattice.fXCount + 1) * (lattice.fYCount + 1);
Stan Ilievca8c0952017-12-11 13:01:58 -0500444 flags.reset(new SkCanvas::Lattice::RectType[n]);
445 colors.reset(new SkColor[n]);
Brian Salomon18df7632017-07-11 14:32:18 -0400446 for (int i = 0; i < n; ++i) {
Stan Ilievca8c0952017-12-11 13:01:58 -0500447 flags[i] = random->nextBool() ? SkCanvas::Lattice::kTransparent
448 : SkCanvas::Lattice::kDefault;
Brian Salomon18df7632017-07-11 14:32:18 -0400449 }
Stan Ilievca8c0952017-12-11 13:01:58 -0500450 lattice.fRectTypes = flags.get();
451 lattice.fColors = colors.get();
Brian Salomon18df7632017-07-11 14:32:18 -0400452 } else {
Stan Ilievca8c0952017-12-11 13:01:58 -0500453 lattice.fRectTypes = nullptr;
454 lattice.fColors = nullptr;
Brian Salomon18df7632017-07-11 14:32:18 -0400455 }
Brian Salomon2a943df2018-05-04 13:43:19 -0400456 } while (!SkLatticeIter::Valid(desc.fWidth, desc.fHeight, lattice));
Brian Salomon815486c2017-07-11 08:52:13 -0400457 SkRect dst;
458 dst.fLeft = random->nextRangeScalar(-2000.5f, 1000.f);
459 dst.fTop = random->nextRangeScalar(-2000.5f, 1000.f);
460 dst.fRight = dst.fLeft + random->nextRangeScalar(0.5f, 1000.f);
461 dst.fBottom = dst.fTop + random->nextRangeScalar(0.5f, 1000.f);
462 std::unique_ptr<SkLatticeIter> iter(new SkLatticeIter(lattice, dst));
463 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
Brian Salomon2a943df2018-05-04 13:43:19 -0400464 auto csxf = GrTest::TestColorXform(random);
465 GrSamplerState::Filter filter =
466 random->nextBool() ? GrSamplerState::Filter::kNearest : GrSamplerState::Filter::kBilerp;
Robert Phillips7c525e62018-06-12 10:11:12 -0400467 return NonAALatticeOp::Make(context, std::move(paint), viewMatrix, std::move(proxy),
Greg Danielc594e622019-10-15 14:01:49 -0400468 GrColorType::kRGBA_8888, std::move(csxf), filter, std::move(iter),
469 dst);
Brian Salomon815486c2017-07-11 08:52:13 -0400470}
471
472#endif