blob: 6709f6347efd7c744133357ada85541726488bd5 [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
99 GrSamplerState samplerState = GrSamplerState(GrSamplerState::WrapMode::kClamp,
100 filter);
101 uint32_t extraSamplerKey = gpu->getExtraSamplerKeyForProgram(samplerState,
102 proxy->backendFormat());
103
Brian Salomon67529b22019-08-13 15:31:04 -0400104 fSampler.reset(proxy->textureType(), samplerState, proxy->textureSwizzle(),
Greg Daniel7a82edf2018-12-04 10:54:34 -0500105 extraSamplerKey);
Brian Salomonf7dcd762018-07-30 14:48:15 -0400106 this->setTextureSamplerCnt(1);
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500107 fInPosition = {"position", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
108 fInTextureCoords = {"textureCoords", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
109 fInTextureDomain = {"textureDomain", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
Brian Osman0b537032018-12-26 12:16:44 -0500110 fInColor = MakeColorAttribute("color", wideColor);
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500111 this->setVertexAttributes(&fInPosition, 4);
Brian Salomon92be2f72018-06-19 14:33:47 -0400112 }
113
Brian Salomonf7dcd762018-07-30 14:48:15 -0400114 const TextureSampler& onTextureSampler(int) const override { return fSampler; }
115
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500116 Attribute fInPosition;
117 Attribute fInTextureCoords;
118 Attribute fInTextureDomain;
119 Attribute fInColor;
Brian Salomon92be2f72018-06-19 14:33:47 -0400120
Brian Salomon2a943df2018-05-04 13:43:19 -0400121 sk_sp<GrColorSpaceXform> fColorSpaceXform;
122 TextureSampler fSampler;
123
124 typedef GrGeometryProcessor INHERITED;
125};
126
Brian Salomon815486c2017-07-11 08:52:13 -0400127class NonAALatticeOp final : public GrMeshDrawOp {
128private:
129 using Helper = GrSimpleMeshDrawOpHelper;
130
joshualitt33a5fce2015-11-18 13:28:51 -0800131public:
Brian Salomon25a88092016-12-01 09:36:50 -0500132 DEFINE_OP_CLASS_ID
joshualitt33a5fce2015-11-18 13:28:51 -0800133
134 static const int kVertsPerRect = 4;
135 static const int kIndicesPerRect = 6;
joshualitt33a5fce2015-11-18 13:28:51 -0800136
Robert Phillipsb97da532019-02-12 15:24:12 -0500137 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400138 GrPaint&& paint,
139 const SkMatrix& viewMatrix,
Brian Salomon2a943df2018-05-04 13:43:19 -0400140 sk_sp<GrTextureProxy> proxy,
141 sk_sp<GrColorSpaceXform> colorSpaceXForm,
142 GrSamplerState::Filter filter,
Robert Phillips7c525e62018-06-12 10:11:12 -0400143 std::unique_ptr<SkLatticeIter> iter,
144 const SkRect& dst) {
Brian Salomond3cee172018-05-07 16:40:48 -0400145 SkASSERT(proxy);
Robert Phillips7c525e62018-06-12 10:11:12 -0400146 return Helper::FactoryHelper<NonAALatticeOp>(context, std::move(paint), viewMatrix,
147 std::move(proxy),
Brian Salomon2a943df2018-05-04 13:43:19 -0400148 std::move(colorSpaceXForm), filter,
149 std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -0400150 }
151
Brian Osmancf860852018-10-31 14:04:39 -0400152 NonAALatticeOp(Helper::MakeArgs& helperArgs, const SkPMColor4f& color,
153 const SkMatrix& viewMatrix, sk_sp<GrTextureProxy> proxy,
154 sk_sp<GrColorSpaceXform> colorSpaceXform, GrSamplerState::Filter filter,
155 std::unique_ptr<SkLatticeIter> iter, const SkRect& dst)
Brian Salomon2a943df2018-05-04 13:43:19 -0400156 : INHERITED(ClassID())
157 , fHelper(helperArgs, GrAAType::kNone)
158 , fProxy(std::move(proxy))
159 , fColorSpaceXform(std::move(colorSpaceXform))
160 , fFilter(filter) {
bsalomona71b8982016-06-30 12:13:52 -0700161 Patch& patch = fPatches.push_back();
162 patch.fViewMatrix = viewMatrix;
163 patch.fColor = color;
msarett10e3d9b2016-08-18 15:46:03 -0700164 patch.fIter = std::move(iter);
bsalomona71b8982016-06-30 12:13:52 -0700165 patch.fDst = dst;
joshualitt33a5fce2015-11-18 13:28:51 -0800166
joshualitt33a5fce2015-11-18 13:28:51 -0800167 // setup bounds
bsalomon88cf17d2016-07-08 06:40:56 -0700168 this->setTransformedBounds(patch.fDst, viewMatrix, HasAABloat::kNo, IsZeroArea::kNo);
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
Chris Dalton1706cbf2019-05-21 19:35:29 -0600173 void visitProxies(const VisitProxyFunc& func) const override {
Chris Dalton7eb5c0f2019-05-23 15:15:47 -0600174 bool mipped = (GrSamplerState::Filter::kMipMap == fFilter);
175 func(fProxy.get(), GrMipMapped(mipped));
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400176 fHelper.visitProxies(func);
177 }
178
Brian Osman9a390ac2018-11-12 09:47:48 -0500179#ifdef SK_DEBUG
robertphillips783a4da2015-11-19 14:00:02 -0800180 SkString dumpInfo() const override {
181 SkString str;
182
bsalomona71b8982016-06-30 12:13:52 -0700183 for (int i = 0; i < fPatches.count(); ++i) {
Brian Salomonfc527d22016-12-14 21:07:01 -0500184 str.appendf("%d: Color: 0x%08x Dst [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", i,
Brian Osmancf860852018-10-31 14:04:39 -0400185 fPatches[i].fColor.toBytes_RGBA(), fPatches[i].fDst.fLeft,
Brian Osman1be2b7c2018-10-29 16:07:15 -0400186 fPatches[i].fDst.fTop, fPatches[i].fDst.fRight, fPatches[i].fDst.fBottom);
robertphillips783a4da2015-11-19 14:00:02 -0800187 }
188
Brian Salomon815486c2017-07-11 08:52:13 -0400189 str += fHelper.dumpInfo();
190 str += INHERITED::dumpInfo();
robertphillips783a4da2015-11-19 14:00:02 -0800191 return str;
192 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500193#endif
joshualitt33a5fce2015-11-18 13:28:51 -0800194
Brian Salomon815486c2017-07-11 08:52:13 -0400195 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
196
Chris Dalton6ce447a2019-06-23 18:07:38 -0600197 GrProcessorSet::Analysis finalize(
198 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
199 GrClampType clampType) override {
Brian Osman1be2b7c2018-10-29 16:07:15 -0400200 auto opaque = fPatches[0].fColor.isOpaque() && GrPixelConfigIsOpaque(fProxy->config())
Brian Salomon2a943df2018-05-04 13:43:19 -0400201 ? GrProcessorAnalysisColor::Opaque::kYes
202 : GrProcessorAnalysisColor::Opaque::kNo;
203 auto analysisColor = GrProcessorAnalysisColor(opaque);
Chris Dalton6ce447a2019-06-23 18:07:38 -0600204 auto result = fHelper.finalizeProcessors(
205 caps, clip, hasMixedSampledCoverage, clampType, GrProcessorAnalysisCoverage::kNone,
206 &analysisColor);
Brian Salomon2a943df2018-05-04 13:43:19 -0400207 analysisColor.isConstant(&fPatches[0].fColor);
Brian Osman8fa7ab42019-03-18 10:22:42 -0400208 fWideColor = SkPMColor4fNeedsWideColor(fPatches[0].fColor, clampType, caps);
Brian Salomon2a943df2018-05-04 13:43:19 -0400209 return result;
Brian Salomon815486c2017-07-11 08:52:13 -0400210 }
211
Brian Salomon92aee3d2016-12-21 09:20:25 -0500212private:
Brian Salomon91326c32017-08-09 16:02:19 -0400213 void onPrepareDraws(Target* target) override {
Greg Daniel7a82edf2018-12-04 10:54:34 -0500214 GrGpu* gpu = target->resourceProvider()->priv().gpu();
Brian Osman0b537032018-12-26 12:16:44 -0500215 auto gp = LatticeGP::Make(gpu, fProxy.get(), fColorSpaceXform, fFilter, fWideColor);
joshualitt33a5fce2015-11-18 13:28:51 -0800216 if (!gp) {
217 SkDebugf("Couldn't create GrGeometryProcessor\n");
218 return;
219 }
220
bsalomona71b8982016-06-30 12:13:52 -0700221 int patchCnt = fPatches.count();
msarett10e3d9b2016-08-18 15:46:03 -0700222 int numRects = 0;
223 for (int i = 0; i < patchCnt; i++) {
msarett0764efe2016-09-02 11:24:30 -0700224 numRects += fPatches[i].fIter->numRectsToDraw();
msarett10e3d9b2016-08-18 15:46:03 -0700225 }
joshualitt33a5fce2015-11-18 13:28:51 -0800226
Brian Salomon0db1b532017-07-12 15:21:43 -0400227 if (!numRects) {
228 return;
229 }
230
Brian Osmanc3064e72018-11-15 08:59:22 -0500231 const size_t kVertexStride = gp->vertexStride();
Brian Salomond28a79d2017-10-16 13:01:07 -0400232 sk_sp<const GrBuffer> indexBuffer = target->resourceProvider()->refQuadIndexBuffer();
Brian Salomon12d22642019-01-29 14:38:50 -0500233 if (!indexBuffer) {
234 SkDebugf("Could not allocate indices\n");
235 return;
236 }
237 PatternHelper helper(target, GrPrimitiveType::kTriangles, kVertexStride,
238 std::move(indexBuffer), kVertsPerRect, kIndicesPerRect, numRects);
Brian Osmanc3064e72018-11-15 08:59:22 -0500239 GrVertexWriter vertices{helper.vertices()};
Brian Salomon12d22642019-01-29 14:38:50 -0500240 if (!vertices.fPtr) {
joshualitt33a5fce2015-11-18 13:28:51 -0800241 SkDebugf("Could not allocate vertices\n");
242 return;
243 }
244
bsalomona71b8982016-06-30 12:13:52 -0700245 for (int i = 0; i < patchCnt; i++) {
msarett7fc08582016-08-18 14:29:22 -0700246 const Patch& patch = fPatches[i];
Brian Osmanc3064e72018-11-15 08:59:22 -0500247
Brian Osman0b537032018-12-26 12:16:44 -0500248 GrVertexColor patchColor(patch.fColor, fWideColor);
msarett10e3d9b2016-08-18 15:46:03 -0700249
250 // Apply the view matrix here if it is scale-translate. Otherwise, we need to
251 // wait until we've created the dst rects.
252 bool isScaleTranslate = patch.fViewMatrix.isScaleTranslate();
253 if (isScaleTranslate) {
254 patch.fIter->mapDstScaleTranslate(patch.fViewMatrix);
255 }
joshualitt33a5fce2015-11-18 13:28:51 -0800256
Brian Salomon2a943df2018-05-04 13:43:19 -0400257 SkIRect srcR;
258 SkRect dstR;
Brian Osmanc3064e72018-11-15 08:59:22 -0500259 SkPoint* patchPositions = reinterpret_cast<SkPoint*>(vertices.fPtr);
Brian Salomon2a943df2018-05-04 13:43:19 -0400260 Sk4f scales(1.f / fProxy->width(), 1.f / fProxy->height(),
261 1.f / fProxy->width(), 1.f / fProxy->height());
262 static const Sk4f kDomainOffsets(0.5f, 0.5f, -0.5f, -0.5f);
Brian Osmanc3064e72018-11-15 08:59:22 -0500263 static const Sk4f kFlipOffsets(0.f, 1.f, 0.f, 1.f);
Brian Salomon2a943df2018-05-04 13:43:19 -0400264 static const Sk4f kFlipMuls(1.f, -1.f, 1.f, -1.f);
msarett10e3d9b2016-08-18 15:46:03 -0700265 while (patch.fIter->next(&srcR, &dstR)) {
Brian Salomon2a943df2018-05-04 13:43:19 -0400266 Sk4f coords(SkIntToScalar(srcR.fLeft), SkIntToScalar(srcR.fTop),
267 SkIntToScalar(srcR.fRight), SkIntToScalar(srcR.fBottom));
268 Sk4f domain = coords + kDomainOffsets;
269 coords *= scales;
270 domain *= scales;
271 if (fProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
272 coords = kFlipMuls * coords + kFlipOffsets;
273 domain = SkNx_shuffle<0, 3, 2, 1>(kFlipMuls * domain + kFlipOffsets);
274 }
Brian Osman4486d982018-11-15 15:56:04 -0500275 SkRect texDomain;
276 SkRect texCoords;
277 domain.store(&texDomain);
278 coords.store(&texCoords);
joshualitt33a5fce2015-11-18 13:28:51 -0800279
Brian Osman0dd43022018-11-16 15:53:26 -0500280 vertices.writeQuad(GrVertexWriter::TriStripFromRect(dstR),
281 GrVertexWriter::TriStripFromRect(texCoords),
Brian Osmanc3064e72018-11-15 08:59:22 -0500282 texDomain,
283 patchColor);
joshualitt33a5fce2015-11-18 13:28:51 -0800284 }
msarett10e3d9b2016-08-18 15:46:03 -0700285
286 // If we didn't handle it above, apply the matrix here.
287 if (!isScaleTranslate) {
Brian Osmanc3064e72018-11-15 08:59:22 -0500288 SkMatrixPriv::MapPointsWithStride(patch.fViewMatrix, patchPositions, kVertexStride,
Brian Salomonfa3783f2018-01-05 13:49:07 -0500289 kVertsPerRect * patch.fIter->numRectsToDraw());
msarett10e3d9b2016-08-18 15:46:03 -0700290 }
joshualitt33a5fce2015-11-18 13:28:51 -0800291 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700292 auto fixedDynamicState = target->makeFixedDynamicState(1);
293 fixedDynamicState->fPrimitiveProcessorTextures[0] = fProxy.get();
294 helper.recordDraw(target, std::move(gp), fixedDynamicState);
295 }
296
297 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
298 fHelper.executeDrawsAndUploads(this, flushState, chainBounds);
joshualitt33a5fce2015-11-18 13:28:51 -0800299 }
300
Brian Salomon7eae3e02018-08-07 14:02:38 +0000301 CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Brian Salomonfc527d22016-12-14 21:07:01 -0500302 NonAALatticeOp* that = t->cast<NonAALatticeOp>();
Brian Salomon2a943df2018-05-04 13:43:19 -0400303 if (fProxy != that->fProxy) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000304 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400305 }
306 if (fFilter != that->fFilter) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000307 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400308 }
309 if (GrColorSpaceXform::Equals(fColorSpaceXform.get(), that->fColorSpaceXform.get())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000310 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400311 }
Brian Salomon815486c2017-07-11 08:52:13 -0400312 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000313 return CombineResult::kCannotCombine;
joshualitt33a5fce2015-11-18 13:28:51 -0800314 }
315
msarett10e3d9b2016-08-18 15:46:03 -0700316 fPatches.move_back_n(that->fPatches.count(), that->fPatches.begin());
Brian Osman0b537032018-12-26 12:16:44 -0500317 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000318 return CombineResult::kMerged;
joshualitt33a5fce2015-11-18 13:28:51 -0800319 }
320
bsalomona71b8982016-06-30 12:13:52 -0700321 struct Patch {
322 SkMatrix fViewMatrix;
msarett10e3d9b2016-08-18 15:46:03 -0700323 std::unique_ptr<SkLatticeIter> fIter;
bsalomona71b8982016-06-30 12:13:52 -0700324 SkRect fDst;
Brian Osmancf860852018-10-31 14:04:39 -0400325 SkPMColor4f fColor;
bsalomona71b8982016-06-30 12:13:52 -0700326 };
327
Brian Salomon815486c2017-07-11 08:52:13 -0400328 Helper fHelper;
329 SkSTArray<1, Patch, true> fPatches;
Brian Salomon2a943df2018-05-04 13:43:19 -0400330 sk_sp<GrTextureProxy> fProxy;
331 sk_sp<GrColorSpaceXform> fColorSpaceXform;
332 GrSamplerState::Filter fFilter;
Brian Osman0b537032018-12-26 12:16:44 -0500333 bool fWideColor;
joshualitt33a5fce2015-11-18 13:28:51 -0800334
Brian Salomon815486c2017-07-11 08:52:13 -0400335 typedef GrMeshDrawOp INHERITED;
joshualitt33a5fce2015-11-18 13:28:51 -0800336};
337
Brian Salomon815486c2017-07-11 08:52:13 -0400338} // anonymous namespace
339
Brian Salomonfc527d22016-12-14 21:07:01 -0500340namespace GrLatticeOp {
Robert Phillipsb97da532019-02-12 15:24:12 -0500341std::unique_ptr<GrDrawOp> MakeNonAA(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400342 GrPaint&& paint,
343 const SkMatrix& viewMatrix,
Brian Salomon2a943df2018-05-04 13:43:19 -0400344 sk_sp<GrTextureProxy> proxy,
345 sk_sp<GrColorSpaceXform> colorSpaceXform,
346 GrSamplerState::Filter filter,
Robert Phillips7c525e62018-06-12 10:11:12 -0400347 std::unique_ptr<SkLatticeIter> iter,
348 const SkRect& dst) {
349 return NonAALatticeOp::Make(context, std::move(paint), viewMatrix, std::move(proxy),
Brian Salomon2a943df2018-05-04 13:43:19 -0400350 std::move(colorSpaceXform), filter, std::move(iter), dst);
joshualitt33a5fce2015-11-18 13:28:51 -0800351}
352};
Brian Salomon815486c2017-07-11 08:52:13 -0400353
354#if GR_TEST_UTILS
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500355#include "src/gpu/GrProxyProvider.h"
356#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon815486c2017-07-11 08:52:13 -0400357
358/** Randomly divides subset into count divs. */
359static void init_random_divs(int divs[], int count, int subsetStart, int subsetStop,
360 SkRandom* random) {
361 // Rules for lattice divs: Must be strictly increasing and in the range
362 // [subsetStart, subsetStop).
363 // Not terribly efficient alg for generating random divs:
364 // 1) Start with minimum legal pixels between each div.
365 // 2) Randomly assign the remaining pixels of the subset to divs.
366 // 3) Convert from pixel counts to div offsets.
367
368 // 1) Initially each divs[i] represents the number of pixels between
369 // div i-1 and i. The initial div is allowed to be at subsetStart. There
370 // must be one pixel spacing between subsequent divs.
371 divs[0] = 0;
372 for (int i = 1; i < count; ++i) {
373 divs[i] = 1;
374 }
375 // 2) Assign the remaining subset pixels to fall
376 int subsetLength = subsetStop - subsetStart;
377 for (int i = 0; i < subsetLength - count; ++i) {
378 // +1 because count divs means count+1 intervals.
379 int entry = random->nextULessThan(count + 1);
380 // We don't have an entry to to store the count after the last div
381 if (entry < count) {
382 divs[entry]++;
383 }
384 }
385 // 3) Now convert the counts between divs to pixel indices, incorporating the subset's offset.
386 int offset = subsetStart;
387 for (int i = 0; i < count; ++i) {
388 divs[i] += offset;
389 offset = divs[i];
390 }
391}
392
393GR_DRAW_OP_TEST_DEFINE(NonAALatticeOp) {
Brian Salomon815486c2017-07-11 08:52:13 -0400394 SkCanvas::Lattice lattice;
Brian Salomon18df7632017-07-11 14:32:18 -0400395 // We loop because our random lattice code can produce an invalid lattice in the case where
396 // there is a single div separator in both x and y and both are aligned with the left and top
397 // edge of the image subset, respectively.
398 std::unique_ptr<int[]> xdivs;
399 std::unique_ptr<int[]> ydivs;
Stan Ilievca8c0952017-12-11 13:01:58 -0500400 std::unique_ptr<SkCanvas::Lattice::RectType[]> flags;
401 std::unique_ptr<SkColor[]> colors;
Brian Salomon18df7632017-07-11 14:32:18 -0400402 SkIRect subset;
Brian Salomon2a943df2018-05-04 13:43:19 -0400403 GrSurfaceDesc desc;
404 desc.fConfig = kRGBA_8888_GrPixelConfig;
405 desc.fWidth = random->nextRangeU(1, 1000);
406 desc.fHeight = random->nextRangeU(1, 1000);
Robert Phillips0a15cc62019-07-30 12:49:10 -0400407 GrSurfaceOrigin origin = random->nextBool() ? kTopLeft_GrSurfaceOrigin
408 : kBottomLeft_GrSurfaceOrigin;
Greg Daniel4065d452018-11-16 15:43:41 -0500409 const GrBackendFormat format =
Robert Phillips0a15cc62019-07-30 12:49:10 -0400410 context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
411 GrRenderable::kNo);
Brian Salomonbeb7f522019-08-30 16:19:42 -0400412 auto proxy = context->priv().proxyProvider()->createProxy(format,
413 desc,
414 GrRenderable::kNo,
415 1,
416 origin,
417 GrMipMapped::kNo,
418 SkBackingFit::kExact,
419 SkBudgeted::kYes,
420 GrProtected::kNo);
Brian Salomon2a943df2018-05-04 13:43:19 -0400421
Brian Salomon18df7632017-07-11 14:32:18 -0400422 do {
Brian Salomon18df7632017-07-11 14:32:18 -0400423 if (random->nextBool()) {
Brian Salomon2a943df2018-05-04 13:43:19 -0400424 subset.fLeft = random->nextULessThan(desc.fWidth);
425 subset.fRight = random->nextRangeU(subset.fLeft + 1, desc.fWidth);
426 subset.fTop = random->nextULessThan(desc.fHeight);
427 subset.fBottom = random->nextRangeU(subset.fTop + 1, desc.fHeight);
Brian Salomon18df7632017-07-11 14:32:18 -0400428 } else {
Brian Salomon2a943df2018-05-04 13:43:19 -0400429 subset.setXYWH(0, 0, desc.fWidth, desc.fHeight);
Brian Salomon815486c2017-07-11 08:52:13 -0400430 }
Brian Salomon2a943df2018-05-04 13:43:19 -0400431 // SkCanvas::Lattice allows bounds to be null. However, SkCanvas creates a temp Lattice with
432 // a non-null bounds before creating a SkLatticeIter since SkLatticeIter requires a bounds.
Brian Salomon18df7632017-07-11 14:32:18 -0400433 lattice.fBounds = &subset;
434 lattice.fXCount = random->nextRangeU(1, subset.width());
435 lattice.fYCount = random->nextRangeU(1, subset.height());
436 xdivs.reset(new int[lattice.fXCount]);
437 ydivs.reset(new int[lattice.fYCount]);
438 init_random_divs(xdivs.get(), lattice.fXCount, subset.fLeft, subset.fRight, random);
439 init_random_divs(ydivs.get(), lattice.fYCount, subset.fTop, subset.fBottom, random);
440 lattice.fXDivs = xdivs.get();
441 lattice.fYDivs = ydivs.get();
442 bool hasFlags = random->nextBool();
443 if (hasFlags) {
444 int n = (lattice.fXCount + 1) * (lattice.fYCount + 1);
Stan Ilievca8c0952017-12-11 13:01:58 -0500445 flags.reset(new SkCanvas::Lattice::RectType[n]);
446 colors.reset(new SkColor[n]);
Brian Salomon18df7632017-07-11 14:32:18 -0400447 for (int i = 0; i < n; ++i) {
Stan Ilievca8c0952017-12-11 13:01:58 -0500448 flags[i] = random->nextBool() ? SkCanvas::Lattice::kTransparent
449 : SkCanvas::Lattice::kDefault;
Brian Salomon18df7632017-07-11 14:32:18 -0400450 }
Stan Ilievca8c0952017-12-11 13:01:58 -0500451 lattice.fRectTypes = flags.get();
452 lattice.fColors = colors.get();
Brian Salomon18df7632017-07-11 14:32:18 -0400453 } else {
Stan Ilievca8c0952017-12-11 13:01:58 -0500454 lattice.fRectTypes = nullptr;
455 lattice.fColors = nullptr;
Brian Salomon18df7632017-07-11 14:32:18 -0400456 }
Brian Salomon2a943df2018-05-04 13:43:19 -0400457 } while (!SkLatticeIter::Valid(desc.fWidth, desc.fHeight, lattice));
Brian Salomon815486c2017-07-11 08:52:13 -0400458 SkRect dst;
459 dst.fLeft = random->nextRangeScalar(-2000.5f, 1000.f);
460 dst.fTop = random->nextRangeScalar(-2000.5f, 1000.f);
461 dst.fRight = dst.fLeft + random->nextRangeScalar(0.5f, 1000.f);
462 dst.fBottom = dst.fTop + random->nextRangeScalar(0.5f, 1000.f);
463 std::unique_ptr<SkLatticeIter> iter(new SkLatticeIter(lattice, dst));
464 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
Brian Salomon2a943df2018-05-04 13:43:19 -0400465 auto csxf = GrTest::TestColorXform(random);
466 GrSamplerState::Filter filter =
467 random->nextBool() ? GrSamplerState::Filter::kNearest : GrSamplerState::Filter::kBilerp;
Robert Phillips7c525e62018-06-12 10:11:12 -0400468 return NonAALatticeOp::Make(context, std::move(paint), viewMatrix, std::move(proxy),
469 std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -0400470}
471
472#endif