blob: 0975025f777905b4668eeef67eef9a2679b7d277 [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,
33 GrSamplerState::Filter filter) {
Greg Daniel7a82edf2018-12-04 10:54:34 -050034 return sk_sp<GrGeometryProcessor>(new LatticeGP(gpu, proxy, std::move(csxf), filter));
Brian Salomon2a943df2018-05-04 13:43:19 -040035 }
36
37 const char* name() const override { return "LatticeGP"; }
38
39 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
40 b->add32(GrColorSpaceXform::XformKey(fColorSpaceXform.get()));
41 }
42
43 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps& caps) const override {
44 class GLSLProcessor : public GrGLSLGeometryProcessor {
45 public:
46 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc,
47 FPCoordTransformIter&& transformIter) override {
48 const auto& latticeGP = proc.cast<LatticeGP>();
49 this->setTransformDataHelper(SkMatrix::I(), pdman, &transformIter);
Brian Osmanc891b102018-06-14 14:50:17 -040050 fColorSpaceXformHelper.setData(pdman, latticeGP.fColorSpaceXform.get());
Brian Salomon2a943df2018-05-04 13:43:19 -040051 }
52
53 private:
54 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
55 using Interpolation = GrGLSLVaryingHandler::Interpolation;
56 const auto& latticeGP = args.fGP.cast<LatticeGP>();
57 fColorSpaceXformHelper.emitCode(args.fUniformHandler,
58 latticeGP.fColorSpaceXform.get());
59
60 args.fVaryingHandler->emitAttributes(latticeGP);
Brian Osmanf04fb3c2018-11-12 15:34:00 -050061 this->writeOutputPosition(args.fVertBuilder, gpArgs, latticeGP.fInPosition.name());
Brian Salomon2a943df2018-05-04 13:43:19 -040062 this->emitTransforms(args.fVertBuilder,
63 args.fVaryingHandler,
64 args.fUniformHandler,
Brian Osmanf04fb3c2018-11-12 15:34:00 -050065 latticeGP.fInTextureCoords.asShaderVar(),
Brian Salomon2a943df2018-05-04 13:43:19 -040066 args.fFPCoordTransformHandler);
67 args.fFragBuilder->codeAppend("float2 textureCoords;");
Brian Osmanf04fb3c2018-11-12 15:34:00 -050068 args.fVaryingHandler->addPassThroughAttribute(latticeGP.fInTextureCoords,
Brian Salomon2a943df2018-05-04 13:43:19 -040069 "textureCoords");
70 args.fFragBuilder->codeAppend("float4 textureDomain;");
71 args.fVaryingHandler->addPassThroughAttribute(
Brian Osmanf04fb3c2018-11-12 15:34:00 -050072 latticeGP.fInTextureDomain, "textureDomain", Interpolation::kCanBeFlat);
73 args.fVaryingHandler->addPassThroughAttribute(latticeGP.fInColor,
74 args.fOutputColor,
Brian Salomon2a943df2018-05-04 13:43:19 -040075 Interpolation::kCanBeFlat);
76 args.fFragBuilder->codeAppendf("%s = ", args.fOutputColor);
77 args.fFragBuilder->appendTextureLookupAndModulate(
78 args.fOutputColor,
79 args.fTexSamplers[0],
80 "clamp(textureCoords, textureDomain.xy, textureDomain.zw)",
81 kFloat2_GrSLType,
82 &fColorSpaceXformHelper);
83 args.fFragBuilder->codeAppend(";");
84 args.fFragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage);
85 }
86 GrGLSLColorSpaceXformHelper fColorSpaceXformHelper;
87 };
88 return new GLSLProcessor;
89 }
90
91private:
Greg Daniel7a82edf2018-12-04 10:54:34 -050092 LatticeGP(GrGpu* gpu, const GrTextureProxy* proxy, sk_sp<GrColorSpaceXform> csxf,
Brian Salomon2a943df2018-05-04 13:43:19 -040093 GrSamplerState::Filter filter)
94 : INHERITED(kLatticeGP_ClassID), fColorSpaceXform(std::move(csxf)) {
Greg Daniel7a82edf2018-12-04 10:54:34 -050095
96 GrSamplerState samplerState = GrSamplerState(GrSamplerState::WrapMode::kClamp,
97 filter);
98 uint32_t extraSamplerKey = gpu->getExtraSamplerKeyForProgram(samplerState,
99 proxy->backendFormat());
100
101 fSampler.reset(proxy->textureType(), proxy->config(), samplerState,
102 extraSamplerKey);
Brian Salomonf7dcd762018-07-30 14:48:15 -0400103 this->setTextureSamplerCnt(1);
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500104 fInPosition = {"position", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
105 fInTextureCoords = {"textureCoords", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
106 fInTextureDomain = {"textureDomain", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
107 fInColor = {"color", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType};
108 this->setVertexAttributes(&fInPosition, 4);
Brian Salomon92be2f72018-06-19 14:33:47 -0400109 }
110
Brian Salomonf7dcd762018-07-30 14:48:15 -0400111 const TextureSampler& onTextureSampler(int) const override { return fSampler; }
112
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500113 Attribute fInPosition;
114 Attribute fInTextureCoords;
115 Attribute fInTextureDomain;
116 Attribute fInColor;
Brian Salomon92be2f72018-06-19 14:33:47 -0400117
Brian Salomon2a943df2018-05-04 13:43:19 -0400118 sk_sp<GrColorSpaceXform> fColorSpaceXform;
119 TextureSampler fSampler;
120
121 typedef GrGeometryProcessor INHERITED;
122};
123
Brian Salomon815486c2017-07-11 08:52:13 -0400124class NonAALatticeOp final : public GrMeshDrawOp {
125private:
126 using Helper = GrSimpleMeshDrawOpHelper;
127
joshualitt33a5fce2015-11-18 13:28:51 -0800128public:
Brian Salomon25a88092016-12-01 09:36:50 -0500129 DEFINE_OP_CLASS_ID
joshualitt33a5fce2015-11-18 13:28:51 -0800130
131 static const int kVertsPerRect = 4;
132 static const int kIndicesPerRect = 6;
joshualitt33a5fce2015-11-18 13:28:51 -0800133
Robert Phillips7c525e62018-06-12 10:11:12 -0400134 static std::unique_ptr<GrDrawOp> Make(GrContext* context,
135 GrPaint&& paint,
136 const SkMatrix& viewMatrix,
Brian Salomon2a943df2018-05-04 13:43:19 -0400137 sk_sp<GrTextureProxy> proxy,
138 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,
144 std::move(proxy),
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,
151 sk_sp<GrColorSpaceXform> colorSpaceXform, GrSamplerState::Filter filter,
152 std::unique_ptr<SkLatticeIter> iter, const SkRect& dst)
Brian Salomon2a943df2018-05-04 13:43:19 -0400153 : INHERITED(ClassID())
154 , fHelper(helperArgs, GrAAType::kNone)
155 , fProxy(std::move(proxy))
156 , fColorSpaceXform(std::move(colorSpaceXform))
157 , fFilter(filter) {
bsalomona71b8982016-06-30 12:13:52 -0700158 Patch& patch = fPatches.push_back();
159 patch.fViewMatrix = viewMatrix;
160 patch.fColor = color;
msarett10e3d9b2016-08-18 15:46:03 -0700161 patch.fIter = std::move(iter);
bsalomona71b8982016-06-30 12:13:52 -0700162 patch.fDst = dst;
joshualitt33a5fce2015-11-18 13:28:51 -0800163
joshualitt33a5fce2015-11-18 13:28:51 -0800164 // setup bounds
bsalomon88cf17d2016-07-08 06:40:56 -0700165 this->setTransformedBounds(patch.fDst, viewMatrix, HasAABloat::kNo, IsZeroArea::kNo);
joshualitt33a5fce2015-11-18 13:28:51 -0800166 }
167
Brian Salomonfc527d22016-12-14 21:07:01 -0500168 const char* name() const override { return "NonAALatticeOp"; }
robertphillips783a4da2015-11-19 14:00:02 -0800169
Brian Salomon7d94bb52018-10-12 14:37:19 -0400170 void visitProxies(const VisitProxyFunc& func, VisitorType) const override {
Brian Salomond3cee172018-05-07 16:40:48 -0400171 func(fProxy.get());
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400172 fHelper.visitProxies(func);
173 }
174
Brian Osman9a390ac2018-11-12 09:47:48 -0500175#ifdef SK_DEBUG
robertphillips783a4da2015-11-19 14:00:02 -0800176 SkString dumpInfo() const override {
177 SkString str;
178
bsalomona71b8982016-06-30 12:13:52 -0700179 for (int i = 0; i < fPatches.count(); ++i) {
Brian Salomonfc527d22016-12-14 21:07:01 -0500180 str.appendf("%d: Color: 0x%08x Dst [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", i,
Brian Osmancf860852018-10-31 14:04:39 -0400181 fPatches[i].fColor.toBytes_RGBA(), fPatches[i].fDst.fLeft,
Brian Osman1be2b7c2018-10-29 16:07:15 -0400182 fPatches[i].fDst.fTop, fPatches[i].fDst.fRight, fPatches[i].fDst.fBottom);
robertphillips783a4da2015-11-19 14:00:02 -0800183 }
184
Brian Salomon815486c2017-07-11 08:52:13 -0400185 str += fHelper.dumpInfo();
186 str += INHERITED::dumpInfo();
robertphillips783a4da2015-11-19 14:00:02 -0800187 return str;
188 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500189#endif
joshualitt33a5fce2015-11-18 13:28:51 -0800190
Brian Salomon815486c2017-07-11 08:52:13 -0400191 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
192
Brian Osman532b3f92018-07-11 10:02:07 -0400193 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip) override {
Brian Osman1be2b7c2018-10-29 16:07:15 -0400194 auto opaque = fPatches[0].fColor.isOpaque() && GrPixelConfigIsOpaque(fProxy->config())
Brian Salomon2a943df2018-05-04 13:43:19 -0400195 ? GrProcessorAnalysisColor::Opaque::kYes
196 : GrProcessorAnalysisColor::Opaque::kNo;
197 auto analysisColor = GrProcessorAnalysisColor(opaque);
198 auto result = fHelper.xpRequiresDstTexture(
Brian Osman532b3f92018-07-11 10:02:07 -0400199 caps, clip, GrProcessorAnalysisCoverage::kNone, &analysisColor);
Brian Salomon2a943df2018-05-04 13:43:19 -0400200 analysisColor.isConstant(&fPatches[0].fColor);
201 return result;
Brian Salomon815486c2017-07-11 08:52:13 -0400202 }
203
Brian Salomon92aee3d2016-12-21 09:20:25 -0500204private:
Brian Salomon91326c32017-08-09 16:02:19 -0400205 void onPrepareDraws(Target* target) override {
Greg Daniel7a82edf2018-12-04 10:54:34 -0500206 GrGpu* gpu = target->resourceProvider()->priv().gpu();
207 auto gp = LatticeGP::Make(gpu, fProxy.get(), fColorSpaceXform, fFilter);
joshualitt33a5fce2015-11-18 13:28:51 -0800208 if (!gp) {
209 SkDebugf("Couldn't create GrGeometryProcessor\n");
210 return;
211 }
212
bsalomona71b8982016-06-30 12:13:52 -0700213 int patchCnt = fPatches.count();
msarett10e3d9b2016-08-18 15:46:03 -0700214 int numRects = 0;
215 for (int i = 0; i < patchCnt; i++) {
msarett0764efe2016-09-02 11:24:30 -0700216 numRects += fPatches[i].fIter->numRectsToDraw();
msarett10e3d9b2016-08-18 15:46:03 -0700217 }
joshualitt33a5fce2015-11-18 13:28:51 -0800218
Brian Salomon0db1b532017-07-12 15:21:43 -0400219 if (!numRects) {
220 return;
221 }
222
Brian Osmanc3064e72018-11-15 08:59:22 -0500223 const size_t kVertexStride = gp->vertexStride();
Brian Salomond28a79d2017-10-16 13:01:07 -0400224 sk_sp<const GrBuffer> indexBuffer = target->resourceProvider()->refQuadIndexBuffer();
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500225 PatternHelper helper(target, GrPrimitiveType::kTriangles, kVertexStride, indexBuffer.get(),
Brian Salomon7eae3e02018-08-07 14:02:38 +0000226 kVertsPerRect, kIndicesPerRect, numRects);
Brian Osmanc3064e72018-11-15 08:59:22 -0500227 GrVertexWriter vertices{helper.vertices()};
228 if (!vertices.fPtr || !indexBuffer) {
joshualitt33a5fce2015-11-18 13:28:51 -0800229 SkDebugf("Could not allocate vertices\n");
230 return;
231 }
232
bsalomona71b8982016-06-30 12:13:52 -0700233 for (int i = 0; i < patchCnt; i++) {
msarett7fc08582016-08-18 14:29:22 -0700234 const Patch& patch = fPatches[i];
Brian Osmanc3064e72018-11-15 08:59:22 -0500235
Brian Osman1be2b7c2018-10-29 16:07:15 -0400236 // TODO4F: Preserve float colors
Brian Osmancf860852018-10-31 14:04:39 -0400237 GrColor patchColor = patch.fColor.toBytes_RGBA();
msarett10e3d9b2016-08-18 15:46:03 -0700238
239 // Apply the view matrix here if it is scale-translate. Otherwise, we need to
240 // wait until we've created the dst rects.
241 bool isScaleTranslate = patch.fViewMatrix.isScaleTranslate();
242 if (isScaleTranslate) {
243 patch.fIter->mapDstScaleTranslate(patch.fViewMatrix);
244 }
joshualitt33a5fce2015-11-18 13:28:51 -0800245
Brian Salomon2a943df2018-05-04 13:43:19 -0400246 SkIRect srcR;
247 SkRect dstR;
Brian Osmanc3064e72018-11-15 08:59:22 -0500248 SkPoint* patchPositions = reinterpret_cast<SkPoint*>(vertices.fPtr);
Brian Salomon2a943df2018-05-04 13:43:19 -0400249 Sk4f scales(1.f / fProxy->width(), 1.f / fProxy->height(),
250 1.f / fProxy->width(), 1.f / fProxy->height());
251 static const Sk4f kDomainOffsets(0.5f, 0.5f, -0.5f, -0.5f);
Brian Osmanc3064e72018-11-15 08:59:22 -0500252 static const Sk4f kFlipOffsets(0.f, 1.f, 0.f, 1.f);
Brian Salomon2a943df2018-05-04 13:43:19 -0400253 static const Sk4f kFlipMuls(1.f, -1.f, 1.f, -1.f);
msarett10e3d9b2016-08-18 15:46:03 -0700254 while (patch.fIter->next(&srcR, &dstR)) {
Brian Salomon2a943df2018-05-04 13:43:19 -0400255 Sk4f coords(SkIntToScalar(srcR.fLeft), SkIntToScalar(srcR.fTop),
256 SkIntToScalar(srcR.fRight), SkIntToScalar(srcR.fBottom));
257 Sk4f domain = coords + kDomainOffsets;
258 coords *= scales;
259 domain *= scales;
260 if (fProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
261 coords = kFlipMuls * coords + kFlipOffsets;
262 domain = SkNx_shuffle<0, 3, 2, 1>(kFlipMuls * domain + kFlipOffsets);
263 }
Brian Osman4486d982018-11-15 15:56:04 -0500264 SkRect texDomain;
265 SkRect texCoords;
266 domain.store(&texDomain);
267 coords.store(&texCoords);
joshualitt33a5fce2015-11-18 13:28:51 -0800268
Brian Osman0dd43022018-11-16 15:53:26 -0500269 vertices.writeQuad(GrVertexWriter::TriStripFromRect(dstR),
270 GrVertexWriter::TriStripFromRect(texCoords),
Brian Osmanc3064e72018-11-15 08:59:22 -0500271 texDomain,
272 patchColor);
joshualitt33a5fce2015-11-18 13:28:51 -0800273 }
msarett10e3d9b2016-08-18 15:46:03 -0700274
275 // If we didn't handle it above, apply the matrix here.
276 if (!isScaleTranslate) {
Brian Osmanc3064e72018-11-15 08:59:22 -0500277 SkMatrixPriv::MapPointsWithStride(patch.fViewMatrix, patchPositions, kVertexStride,
Brian Salomonfa3783f2018-01-05 13:49:07 -0500278 kVertsPerRect * patch.fIter->numRectsToDraw());
msarett10e3d9b2016-08-18 15:46:03 -0700279 }
joshualitt33a5fce2015-11-18 13:28:51 -0800280 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000281 auto pipe = fHelper.makePipeline(target, 1);
282 pipe.fFixedDynamicState->fPrimitiveProcessorTextures[0] = fProxy.get();
283 helper.recordDraw(target, std::move(gp), pipe.fPipeline, pipe.fFixedDynamicState);
joshualitt33a5fce2015-11-18 13:28:51 -0800284 }
285
Brian Salomon7eae3e02018-08-07 14:02:38 +0000286 CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Brian Salomonfc527d22016-12-14 21:07:01 -0500287 NonAALatticeOp* that = t->cast<NonAALatticeOp>();
Brian Salomon2a943df2018-05-04 13:43:19 -0400288 if (fProxy != that->fProxy) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000289 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400290 }
291 if (fFilter != that->fFilter) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000292 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400293 }
294 if (GrColorSpaceXform::Equals(fColorSpaceXform.get(), that->fColorSpaceXform.get())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000295 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400296 }
Brian Salomon815486c2017-07-11 08:52:13 -0400297 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000298 return CombineResult::kCannotCombine;
joshualitt33a5fce2015-11-18 13:28:51 -0800299 }
300
msarett10e3d9b2016-08-18 15:46:03 -0700301 fPatches.move_back_n(that->fPatches.count(), that->fPatches.begin());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000302 return CombineResult::kMerged;
joshualitt33a5fce2015-11-18 13:28:51 -0800303 }
304
bsalomona71b8982016-06-30 12:13:52 -0700305 struct Patch {
306 SkMatrix fViewMatrix;
msarett10e3d9b2016-08-18 15:46:03 -0700307 std::unique_ptr<SkLatticeIter> fIter;
bsalomona71b8982016-06-30 12:13:52 -0700308 SkRect fDst;
Brian Osmancf860852018-10-31 14:04:39 -0400309 SkPMColor4f fColor;
bsalomona71b8982016-06-30 12:13:52 -0700310 };
311
Brian Salomon815486c2017-07-11 08:52:13 -0400312 Helper fHelper;
313 SkSTArray<1, Patch, true> fPatches;
Brian Salomon2a943df2018-05-04 13:43:19 -0400314 sk_sp<GrTextureProxy> fProxy;
315 sk_sp<GrColorSpaceXform> fColorSpaceXform;
316 GrSamplerState::Filter fFilter;
joshualitt33a5fce2015-11-18 13:28:51 -0800317
Brian Salomon815486c2017-07-11 08:52:13 -0400318 typedef GrMeshDrawOp INHERITED;
joshualitt33a5fce2015-11-18 13:28:51 -0800319};
320
Brian Salomon815486c2017-07-11 08:52:13 -0400321} // anonymous namespace
322
Brian Salomonfc527d22016-12-14 21:07:01 -0500323namespace GrLatticeOp {
Robert Phillips7c525e62018-06-12 10:11:12 -0400324std::unique_ptr<GrDrawOp> MakeNonAA(GrContext* context,
325 GrPaint&& paint,
326 const SkMatrix& viewMatrix,
Brian Salomon2a943df2018-05-04 13:43:19 -0400327 sk_sp<GrTextureProxy> proxy,
328 sk_sp<GrColorSpaceXform> colorSpaceXform,
329 GrSamplerState::Filter filter,
Robert Phillips7c525e62018-06-12 10:11:12 -0400330 std::unique_ptr<SkLatticeIter> iter,
331 const SkRect& dst) {
332 return NonAALatticeOp::Make(context, std::move(paint), viewMatrix, std::move(proxy),
Brian Salomon2a943df2018-05-04 13:43:19 -0400333 std::move(colorSpaceXform), filter, std::move(iter), dst);
joshualitt33a5fce2015-11-18 13:28:51 -0800334}
335};
Brian Salomon815486c2017-07-11 08:52:13 -0400336
337#if GR_TEST_UTILS
Brian Salomon2a943df2018-05-04 13:43:19 -0400338#include "GrContextPriv.h"
339#include "GrProxyProvider.h"
Brian Salomon815486c2017-07-11 08:52:13 -0400340
341/** Randomly divides subset into count divs. */
342static void init_random_divs(int divs[], int count, int subsetStart, int subsetStop,
343 SkRandom* random) {
344 // Rules for lattice divs: Must be strictly increasing and in the range
345 // [subsetStart, subsetStop).
346 // Not terribly efficient alg for generating random divs:
347 // 1) Start with minimum legal pixels between each div.
348 // 2) Randomly assign the remaining pixels of the subset to divs.
349 // 3) Convert from pixel counts to div offsets.
350
351 // 1) Initially each divs[i] represents the number of pixels between
352 // div i-1 and i. The initial div is allowed to be at subsetStart. There
353 // must be one pixel spacing between subsequent divs.
354 divs[0] = 0;
355 for (int i = 1; i < count; ++i) {
356 divs[i] = 1;
357 }
358 // 2) Assign the remaining subset pixels to fall
359 int subsetLength = subsetStop - subsetStart;
360 for (int i = 0; i < subsetLength - count; ++i) {
361 // +1 because count divs means count+1 intervals.
362 int entry = random->nextULessThan(count + 1);
363 // We don't have an entry to to store the count after the last div
364 if (entry < count) {
365 divs[entry]++;
366 }
367 }
368 // 3) Now convert the counts between divs to pixel indices, incorporating the subset's offset.
369 int offset = subsetStart;
370 for (int i = 0; i < count; ++i) {
371 divs[i] += offset;
372 offset = divs[i];
373 }
374}
375
376GR_DRAW_OP_TEST_DEFINE(NonAALatticeOp) {
Brian Salomon815486c2017-07-11 08:52:13 -0400377 SkCanvas::Lattice lattice;
Brian Salomon18df7632017-07-11 14:32:18 -0400378 // We loop because our random lattice code can produce an invalid lattice in the case where
379 // there is a single div separator in both x and y and both are aligned with the left and top
380 // edge of the image subset, respectively.
381 std::unique_ptr<int[]> xdivs;
382 std::unique_ptr<int[]> ydivs;
Stan Ilievca8c0952017-12-11 13:01:58 -0500383 std::unique_ptr<SkCanvas::Lattice::RectType[]> flags;
384 std::unique_ptr<SkColor[]> colors;
Brian Salomon18df7632017-07-11 14:32:18 -0400385 SkIRect subset;
Brian Salomon2a943df2018-05-04 13:43:19 -0400386 GrSurfaceDesc desc;
387 desc.fConfig = kRGBA_8888_GrPixelConfig;
388 desc.fWidth = random->nextRangeU(1, 1000);
389 desc.fHeight = random->nextRangeU(1, 1000);
390 GrSurfaceOrigin origin =
391 random->nextBool() ? kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin;
Greg Daniel4065d452018-11-16 15:43:41 -0500392 const GrBackendFormat format =
393 context->contextPriv().caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
Brian Salomon2a943df2018-05-04 13:43:19 -0400394 auto proxy = context->contextPriv().proxyProvider()->createProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500395 format, desc, origin, SkBackingFit::kExact, SkBudgeted::kYes);
Brian Salomon2a943df2018-05-04 13:43:19 -0400396
Brian Salomon18df7632017-07-11 14:32:18 -0400397 do {
Brian Salomon18df7632017-07-11 14:32:18 -0400398 if (random->nextBool()) {
Brian Salomon2a943df2018-05-04 13:43:19 -0400399 subset.fLeft = random->nextULessThan(desc.fWidth);
400 subset.fRight = random->nextRangeU(subset.fLeft + 1, desc.fWidth);
401 subset.fTop = random->nextULessThan(desc.fHeight);
402 subset.fBottom = random->nextRangeU(subset.fTop + 1, desc.fHeight);
Brian Salomon18df7632017-07-11 14:32:18 -0400403 } else {
Brian Salomon2a943df2018-05-04 13:43:19 -0400404 subset.setXYWH(0, 0, desc.fWidth, desc.fHeight);
Brian Salomon815486c2017-07-11 08:52:13 -0400405 }
Brian Salomon2a943df2018-05-04 13:43:19 -0400406 // SkCanvas::Lattice allows bounds to be null. However, SkCanvas creates a temp Lattice with
407 // a non-null bounds before creating a SkLatticeIter since SkLatticeIter requires a bounds.
Brian Salomon18df7632017-07-11 14:32:18 -0400408 lattice.fBounds = &subset;
409 lattice.fXCount = random->nextRangeU(1, subset.width());
410 lattice.fYCount = random->nextRangeU(1, subset.height());
411 xdivs.reset(new int[lattice.fXCount]);
412 ydivs.reset(new int[lattice.fYCount]);
413 init_random_divs(xdivs.get(), lattice.fXCount, subset.fLeft, subset.fRight, random);
414 init_random_divs(ydivs.get(), lattice.fYCount, subset.fTop, subset.fBottom, random);
415 lattice.fXDivs = xdivs.get();
416 lattice.fYDivs = ydivs.get();
417 bool hasFlags = random->nextBool();
418 if (hasFlags) {
419 int n = (lattice.fXCount + 1) * (lattice.fYCount + 1);
Stan Ilievca8c0952017-12-11 13:01:58 -0500420 flags.reset(new SkCanvas::Lattice::RectType[n]);
421 colors.reset(new SkColor[n]);
Brian Salomon18df7632017-07-11 14:32:18 -0400422 for (int i = 0; i < n; ++i) {
Stan Ilievca8c0952017-12-11 13:01:58 -0500423 flags[i] = random->nextBool() ? SkCanvas::Lattice::kTransparent
424 : SkCanvas::Lattice::kDefault;
Brian Salomon18df7632017-07-11 14:32:18 -0400425 }
Stan Ilievca8c0952017-12-11 13:01:58 -0500426 lattice.fRectTypes = flags.get();
427 lattice.fColors = colors.get();
Brian Salomon18df7632017-07-11 14:32:18 -0400428 } else {
Stan Ilievca8c0952017-12-11 13:01:58 -0500429 lattice.fRectTypes = nullptr;
430 lattice.fColors = nullptr;
Brian Salomon18df7632017-07-11 14:32:18 -0400431 }
Brian Salomon2a943df2018-05-04 13:43:19 -0400432 } while (!SkLatticeIter::Valid(desc.fWidth, desc.fHeight, lattice));
Brian Salomon815486c2017-07-11 08:52:13 -0400433 SkRect dst;
434 dst.fLeft = random->nextRangeScalar(-2000.5f, 1000.f);
435 dst.fTop = random->nextRangeScalar(-2000.5f, 1000.f);
436 dst.fRight = dst.fLeft + random->nextRangeScalar(0.5f, 1000.f);
437 dst.fBottom = dst.fTop + random->nextRangeScalar(0.5f, 1000.f);
438 std::unique_ptr<SkLatticeIter> iter(new SkLatticeIter(lattice, dst));
439 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
Brian Salomon2a943df2018-05-04 13:43:19 -0400440 auto csxf = GrTest::TestColorXform(random);
441 GrSamplerState::Filter filter =
442 random->nextBool() ? GrSamplerState::Filter::kNearest : GrSamplerState::Filter::kBilerp;
Robert Phillips7c525e62018-06-12 10:11:12 -0400443 return NonAALatticeOp::Make(context, std::move(paint), viewMatrix, std::move(proxy),
444 std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -0400445}
446
447#endif