blob: 7bad753287ffb4c473527d612b45c6561f842a9a [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"
Robert Phillips03e4c952019-11-26 16:20:22 -050021#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
23#include "src/gpu/glsl/GrGLSLVarying.h"
24#include "src/gpu/ops/GrLatticeOp.h"
25#include "src/gpu/ops/GrMeshDrawOp.h"
26#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
joshualitt33a5fce2015-11-18 13:28:51 -080027
Brian Salomon815486c2017-07-11 08:52:13 -040028namespace {
29
Brian Salomon2a943df2018-05-04 13:43:19 -040030class LatticeGP : public GrGeometryProcessor {
31public:
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050032 static GrGeometryProcessor* Make(SkArenaAlloc* arena,
Greg Danieled96bca2019-12-05 15:05:54 -050033 const GrSurfaceProxyView& view,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050034 sk_sp<GrColorSpaceXform> csxf,
35 GrSamplerState::Filter filter,
36 bool wideColor) {
Greg Danieled96bca2019-12-05 15:05:54 -050037 return arena->make<LatticeGP>(view, 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,
Brian Salomonc241b582019-11-27 08:57:17 -050050 const CoordTransformRange& transformRange) override {
Brian Salomon2a943df2018-05-04 13:43:19 -040051 const auto& latticeGP = proc.cast<LatticeGP>();
Brian Salomonc241b582019-11-27 08:57:17 -050052 this->setTransformDataHelper(SkMatrix::I(), pdman, transformRange);
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);
Brian Salomon87e9ddb2019-12-19 14:50:22 -050080 args.fFragBuilder->appendTextureLookupAndBlend(
Brian Salomon2a943df2018-05-04 13:43:19 -040081 args.fOutputColor,
Brian Salomon87e9ddb2019-12-19 14:50:22 -050082 SkBlendMode::kModulate,
Brian Salomon2a943df2018-05-04 13:43:19 -040083 args.fTexSamplers[0],
84 "clamp(textureCoords, textureDomain.xy, textureDomain.zw)",
Brian Salomon2a943df2018-05-04 13:43:19 -040085 &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:
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050095 friend class ::SkArenaAlloc; // for access to ctor
96
Greg Danieled96bca2019-12-05 15:05:54 -050097 LatticeGP(const GrSurfaceProxyView& view, sk_sp<GrColorSpaceXform> csxf,
Brian Osman0b537032018-12-26 12:16:44 -050098 GrSamplerState::Filter filter, bool wideColor)
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050099 : INHERITED(kLatticeGP_ClassID)
100 , fColorSpaceXform(std::move(csxf)) {
Greg Daniel7a82edf2018-12-04 10:54:34 -0500101
Robert Phillips323471e2019-11-11 11:33:37 -0500102 fSampler.reset(GrSamplerState(GrSamplerState::WrapMode::kClamp, filter),
Greg Danieled96bca2019-12-05 15:05:54 -0500103 view.proxy()->backendFormat(), view.swizzle());
Brian Salomonf7dcd762018-07-30 14:48:15 -0400104 this->setTextureSamplerCnt(1);
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500105 fInPosition = {"position", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
106 fInTextureCoords = {"textureCoords", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
107 fInTextureDomain = {"textureDomain", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
Brian Osman0b537032018-12-26 12:16:44 -0500108 fInColor = MakeColorAttribute("color", wideColor);
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500109 this->setVertexAttributes(&fInPosition, 4);
Brian Salomon92be2f72018-06-19 14:33:47 -0400110 }
111
Brian Salomonf7dcd762018-07-30 14:48:15 -0400112 const TextureSampler& onTextureSampler(int) const override { return fSampler; }
113
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500114 Attribute fInPosition;
115 Attribute fInTextureCoords;
116 Attribute fInTextureDomain;
117 Attribute fInColor;
Brian Salomon92be2f72018-06-19 14:33:47 -0400118
Brian Salomon2a943df2018-05-04 13:43:19 -0400119 sk_sp<GrColorSpaceXform> fColorSpaceXform;
120 TextureSampler fSampler;
121
122 typedef GrGeometryProcessor INHERITED;
123};
124
Brian Salomon815486c2017-07-11 08:52:13 -0400125class NonAALatticeOp final : public GrMeshDrawOp {
126private:
127 using Helper = GrSimpleMeshDrawOpHelper;
128
joshualitt33a5fce2015-11-18 13:28:51 -0800129public:
Brian Salomon25a88092016-12-01 09:36:50 -0500130 DEFINE_OP_CLASS_ID
joshualitt33a5fce2015-11-18 13:28:51 -0800131
Robert Phillipsb97da532019-02-12 15:24:12 -0500132 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400133 GrPaint&& paint,
134 const SkMatrix& viewMatrix,
Greg Danieled96bca2019-12-05 15:05:54 -0500135 GrSurfaceProxyView view,
Greg Danielc594e622019-10-15 14:01:49 -0400136 GrColorType srcColorType,
Brian Salomon2a943df2018-05-04 13:43:19 -0400137 sk_sp<GrColorSpaceXform> colorSpaceXForm,
138 GrSamplerState::Filter filter,
Robert Phillips7c525e62018-06-12 10:11:12 -0400139 std::unique_ptr<SkLatticeIter> iter,
140 const SkRect& dst) {
Greg Danieled96bca2019-12-05 15:05:54 -0500141 SkASSERT(view.proxy());
Robert Phillips7c525e62018-06-12 10:11:12 -0400142 return Helper::FactoryHelper<NonAALatticeOp>(context, std::move(paint), viewMatrix,
Greg Danieled96bca2019-12-05 15:05:54 -0500143 std::move(view), srcColorType,
Brian Salomon2a943df2018-05-04 13:43:19 -0400144 std::move(colorSpaceXForm), filter,
145 std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -0400146 }
147
Brian Osmancf860852018-10-31 14:04:39 -0400148 NonAALatticeOp(Helper::MakeArgs& helperArgs, const SkPMColor4f& color,
Greg Danieled96bca2019-12-05 15:05:54 -0500149 const SkMatrix& viewMatrix, GrSurfaceProxyView view,
Greg Danielc594e622019-10-15 14:01:49 -0400150 GrColorType srcColorType, sk_sp<GrColorSpaceXform> colorSpaceXform,
151 GrSamplerState::Filter filter, std::unique_ptr<SkLatticeIter> iter,
152 const SkRect& dst)
Brian Salomon2a943df2018-05-04 13:43:19 -0400153 : INHERITED(ClassID())
154 , fHelper(helperArgs, GrAAType::kNone)
Greg Danieled96bca2019-12-05 15:05:54 -0500155 , fView(std::move(view))
Greg Danielc594e622019-10-15 14:01:49 -0400156 , fSrcColorType(srcColorType)
Brian Salomon2a943df2018-05-04 13:43:19 -0400157 , fColorSpaceXform(std::move(colorSpaceXform))
158 , fFilter(filter) {
bsalomona71b8982016-06-30 12:13:52 -0700159 Patch& patch = fPatches.push_back();
160 patch.fViewMatrix = viewMatrix;
161 patch.fColor = color;
msarett10e3d9b2016-08-18 15:46:03 -0700162 patch.fIter = std::move(iter);
bsalomona71b8982016-06-30 12:13:52 -0700163 patch.fDst = dst;
joshualitt33a5fce2015-11-18 13:28:51 -0800164
joshualitt33a5fce2015-11-18 13:28:51 -0800165 // setup bounds
Greg Daniel5faf4742019-10-01 15:14:44 -0400166 this->setTransformedBounds(patch.fDst, viewMatrix, HasAABloat::kNo, IsHairline::kNo);
joshualitt33a5fce2015-11-18 13:28:51 -0800167 }
168
Brian Salomonfc527d22016-12-14 21:07:01 -0500169 const char* name() const override { return "NonAALatticeOp"; }
robertphillips783a4da2015-11-19 14:00:02 -0800170
Chris Dalton1706cbf2019-05-21 19:35:29 -0600171 void visitProxies(const VisitProxyFunc& func) const override {
Chris Dalton7eb5c0f2019-05-23 15:15:47 -0600172 bool mipped = (GrSamplerState::Filter::kMipMap == fFilter);
Greg Danieled96bca2019-12-05 15:05:54 -0500173 func(fView.proxy(), GrMipMapped(mipped));
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400174 fHelper.visitProxies(func);
175 }
176
Brian Osman9a390ac2018-11-12 09:47:48 -0500177#ifdef SK_DEBUG
robertphillips783a4da2015-11-19 14:00:02 -0800178 SkString dumpInfo() const override {
179 SkString str;
180
bsalomona71b8982016-06-30 12:13:52 -0700181 for (int i = 0; i < fPatches.count(); ++i) {
Brian Salomonfc527d22016-12-14 21:07:01 -0500182 str.appendf("%d: Color: 0x%08x Dst [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", i,
Brian Osmancf860852018-10-31 14:04:39 -0400183 fPatches[i].fColor.toBytes_RGBA(), fPatches[i].fDst.fLeft,
Brian Osman1be2b7c2018-10-29 16:07:15 -0400184 fPatches[i].fDst.fTop, fPatches[i].fDst.fRight, fPatches[i].fDst.fBottom);
robertphillips783a4da2015-11-19 14:00:02 -0800185 }
186
Brian Salomon815486c2017-07-11 08:52:13 -0400187 str += fHelper.dumpInfo();
188 str += INHERITED::dumpInfo();
robertphillips783a4da2015-11-19 14:00:02 -0800189 return str;
190 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500191#endif
joshualitt33a5fce2015-11-18 13:28:51 -0800192
Brian Salomon815486c2017-07-11 08:52:13 -0400193 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
194
Chris Dalton6ce447a2019-06-23 18:07:38 -0600195 GrProcessorSet::Analysis finalize(
196 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
197 GrClampType clampType) override {
Greg Danielc594e622019-10-15 14:01:49 -0400198 auto opaque = fPatches[0].fColor.isOpaque() && !GrColorTypeHasAlpha(fSrcColorType)
Brian Salomon2a943df2018-05-04 13:43:19 -0400199 ? GrProcessorAnalysisColor::Opaque::kYes
200 : GrProcessorAnalysisColor::Opaque::kNo;
201 auto analysisColor = GrProcessorAnalysisColor(opaque);
Chris Dalton6ce447a2019-06-23 18:07:38 -0600202 auto result = fHelper.finalizeProcessors(
203 caps, clip, hasMixedSampledCoverage, clampType, GrProcessorAnalysisCoverage::kNone,
204 &analysisColor);
Brian Salomon2a943df2018-05-04 13:43:19 -0400205 analysisColor.isConstant(&fPatches[0].fColor);
Brian Osman2715bf52019-12-06 14:38:47 -0500206 fWideColor = !fPatches[0].fColor.fitsInBytes();
Brian Salomon2a943df2018-05-04 13:43:19 -0400207 return result;
Brian Salomon815486c2017-07-11 08:52:13 -0400208 }
209
Brian Salomon92aee3d2016-12-21 09:20:25 -0500210private:
Brian Salomon91326c32017-08-09 16:02:19 -0400211 void onPrepareDraws(Target* target) override {
Greg Danieled96bca2019-12-05 15:05:54 -0500212 auto gp = LatticeGP::Make(target->allocator(), fView, fColorSpaceXform,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500213 fFilter, fWideColor);
joshualitt33a5fce2015-11-18 13:28:51 -0800214 if (!gp) {
215 SkDebugf("Couldn't create GrGeometryProcessor\n");
216 return;
217 }
218
bsalomona71b8982016-06-30 12:13:52 -0700219 int patchCnt = fPatches.count();
msarett10e3d9b2016-08-18 15:46:03 -0700220 int numRects = 0;
221 for (int i = 0; i < patchCnt; i++) {
msarett0764efe2016-09-02 11:24:30 -0700222 numRects += fPatches[i].fIter->numRectsToDraw();
msarett10e3d9b2016-08-18 15:46:03 -0700223 }
joshualitt33a5fce2015-11-18 13:28:51 -0800224
Brian Salomon0db1b532017-07-12 15:21:43 -0400225 if (!numRects) {
226 return;
227 }
228
Brian Osmanc3064e72018-11-15 08:59:22 -0500229 const size_t kVertexStride = gp->vertexStride();
Robert Phillipse94cdd22019-11-04 14:15:58 -0500230
231 QuadHelper helper(target, kVertexStride, numRects);
232
Brian Osmanc3064e72018-11-15 08:59:22 -0500233 GrVertexWriter vertices{helper.vertices()};
Brian Salomon12d22642019-01-29 14:38:50 -0500234 if (!vertices.fPtr) {
joshualitt33a5fce2015-11-18 13:28:51 -0800235 SkDebugf("Could not allocate vertices\n");
236 return;
237 }
238
bsalomona71b8982016-06-30 12:13:52 -0700239 for (int i = 0; i < patchCnt; i++) {
msarett7fc08582016-08-18 14:29:22 -0700240 const Patch& patch = fPatches[i];
Brian Osmanc3064e72018-11-15 08:59:22 -0500241
Brian Osman0b537032018-12-26 12:16:44 -0500242 GrVertexColor patchColor(patch.fColor, fWideColor);
msarett10e3d9b2016-08-18 15:46:03 -0700243
244 // Apply the view matrix here if it is scale-translate. Otherwise, we need to
245 // wait until we've created the dst rects.
246 bool isScaleTranslate = patch.fViewMatrix.isScaleTranslate();
247 if (isScaleTranslate) {
248 patch.fIter->mapDstScaleTranslate(patch.fViewMatrix);
249 }
joshualitt33a5fce2015-11-18 13:28:51 -0800250
Brian Salomon2a943df2018-05-04 13:43:19 -0400251 SkIRect srcR;
252 SkRect dstR;
Brian Osmanc3064e72018-11-15 08:59:22 -0500253 SkPoint* patchPositions = reinterpret_cast<SkPoint*>(vertices.fPtr);
Greg Danieled96bca2019-12-05 15:05:54 -0500254 Sk4f scales(1.f / fView.proxy()->width(), 1.f / fView.proxy()->height(),
255 1.f / fView.proxy()->width(), 1.f / fView.proxy()->height());
Brian Salomon2a943df2018-05-04 13:43:19 -0400256 static const Sk4f kDomainOffsets(0.5f, 0.5f, -0.5f, -0.5f);
Brian Osmanc3064e72018-11-15 08:59:22 -0500257 static const Sk4f kFlipOffsets(0.f, 1.f, 0.f, 1.f);
Brian Salomon2a943df2018-05-04 13:43:19 -0400258 static const Sk4f kFlipMuls(1.f, -1.f, 1.f, -1.f);
msarett10e3d9b2016-08-18 15:46:03 -0700259 while (patch.fIter->next(&srcR, &dstR)) {
Brian Salomon2a943df2018-05-04 13:43:19 -0400260 Sk4f coords(SkIntToScalar(srcR.fLeft), SkIntToScalar(srcR.fTop),
261 SkIntToScalar(srcR.fRight), SkIntToScalar(srcR.fBottom));
262 Sk4f domain = coords + kDomainOffsets;
263 coords *= scales;
264 domain *= scales;
Greg Danieled96bca2019-12-05 15:05:54 -0500265 if (fView.origin() == kBottomLeft_GrSurfaceOrigin) {
Brian Salomon2a943df2018-05-04 13:43:19 -0400266 coords = kFlipMuls * coords + kFlipOffsets;
267 domain = SkNx_shuffle<0, 3, 2, 1>(kFlipMuls * domain + kFlipOffsets);
268 }
Brian Osman4486d982018-11-15 15:56:04 -0500269 SkRect texDomain;
270 SkRect texCoords;
271 domain.store(&texDomain);
272 coords.store(&texCoords);
joshualitt33a5fce2015-11-18 13:28:51 -0800273
Brian Osman0dd43022018-11-16 15:53:26 -0500274 vertices.writeQuad(GrVertexWriter::TriStripFromRect(dstR),
275 GrVertexWriter::TriStripFromRect(texCoords),
Brian Osmanc3064e72018-11-15 08:59:22 -0500276 texDomain,
277 patchColor);
joshualitt33a5fce2015-11-18 13:28:51 -0800278 }
msarett10e3d9b2016-08-18 15:46:03 -0700279
280 // If we didn't handle it above, apply the matrix here.
281 if (!isScaleTranslate) {
Robert Phillipsee08d522019-10-28 16:34:44 -0400282 SkMatrixPriv::MapPointsWithStride(
283 patch.fViewMatrix, patchPositions, kVertexStride,
284 GrResourceProvider::NumVertsPerNonAAQuad() * patch.fIter->numRectsToDraw());
msarett10e3d9b2016-08-18 15:46:03 -0700285 }
joshualitt33a5fce2015-11-18 13:28:51 -0800286 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700287 auto fixedDynamicState = target->makeFixedDynamicState(1);
Greg Danieled96bca2019-12-05 15:05:54 -0500288 fixedDynamicState->fPrimitiveProcessorTextures[0] = fView.proxy();
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500289 helper.recordDraw(target, gp, fixedDynamicState);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700290 }
291
292 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillips3968fcb2019-12-05 16:40:31 -0500293 auto pipeline = GrSimpleMeshDrawOpHelper::CreatePipeline(flushState,
294 fHelper.detachProcessorSet(),
295 fHelper.pipelineFlags());
296
297 flushState->executeDrawsAndUploadsForMeshDrawOp(this, chainBounds, pipeline);
joshualitt33a5fce2015-11-18 13:28:51 -0800298 }
299
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500300 CombineResult onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*,
301 const GrCaps& caps) override {
Brian Salomonfc527d22016-12-14 21:07:01 -0500302 NonAALatticeOp* that = t->cast<NonAALatticeOp>();
Greg Danieled96bca2019-12-05 15:05:54 -0500303 if (fView != that->fView) {
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;
Greg Danieled96bca2019-12-05 15:05:54 -0500330 GrSurfaceProxyView fView;
Greg Danielc594e622019-10-15 14:01:49 -0400331 GrColorType fSrcColorType;
Brian Salomon2a943df2018-05-04 13:43:19 -0400332 sk_sp<GrColorSpaceXform> fColorSpaceXform;
333 GrSamplerState::Filter fFilter;
Brian Osman0b537032018-12-26 12:16:44 -0500334 bool fWideColor;
joshualitt33a5fce2015-11-18 13:28:51 -0800335
Brian Salomon815486c2017-07-11 08:52:13 -0400336 typedef GrMeshDrawOp INHERITED;
joshualitt33a5fce2015-11-18 13:28:51 -0800337};
338
Brian Salomon815486c2017-07-11 08:52:13 -0400339} // anonymous namespace
340
Brian Salomonfc527d22016-12-14 21:07:01 -0500341namespace GrLatticeOp {
Robert Phillipsb97da532019-02-12 15:24:12 -0500342std::unique_ptr<GrDrawOp> MakeNonAA(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400343 GrPaint&& paint,
344 const SkMatrix& viewMatrix,
Greg Danieled96bca2019-12-05 15:05:54 -0500345 GrSurfaceProxyView view,
Greg Danielc594e622019-10-15 14:01:49 -0400346 GrColorType srcColorType,
Brian Salomon2a943df2018-05-04 13:43:19 -0400347 sk_sp<GrColorSpaceXform> colorSpaceXform,
348 GrSamplerState::Filter filter,
Robert Phillips7c525e62018-06-12 10:11:12 -0400349 std::unique_ptr<SkLatticeIter> iter,
350 const SkRect& dst) {
Greg Danieled96bca2019-12-05 15:05:54 -0500351 return NonAALatticeOp::Make(context, std::move(paint), viewMatrix, std::move(view),
Greg Danielc594e622019-10-15 14:01:49 -0400352 srcColorType, std::move(colorSpaceXform), filter, std::move(iter),
353 dst);
joshualitt33a5fce2015-11-18 13:28:51 -0800354}
355};
Brian Salomon815486c2017-07-11 08:52:13 -0400356
357#if GR_TEST_UTILS
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500358#include "src/gpu/GrProxyProvider.h"
359#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon815486c2017-07-11 08:52:13 -0400360
361/** Randomly divides subset into count divs. */
362static void init_random_divs(int divs[], int count, int subsetStart, int subsetStop,
363 SkRandom* random) {
364 // Rules for lattice divs: Must be strictly increasing and in the range
365 // [subsetStart, subsetStop).
366 // Not terribly efficient alg for generating random divs:
367 // 1) Start with minimum legal pixels between each div.
368 // 2) Randomly assign the remaining pixels of the subset to divs.
369 // 3) Convert from pixel counts to div offsets.
370
371 // 1) Initially each divs[i] represents the number of pixels between
372 // div i-1 and i. The initial div is allowed to be at subsetStart. There
373 // must be one pixel spacing between subsequent divs.
374 divs[0] = 0;
375 for (int i = 1; i < count; ++i) {
376 divs[i] = 1;
377 }
378 // 2) Assign the remaining subset pixels to fall
379 int subsetLength = subsetStop - subsetStart;
380 for (int i = 0; i < subsetLength - count; ++i) {
381 // +1 because count divs means count+1 intervals.
382 int entry = random->nextULessThan(count + 1);
383 // We don't have an entry to to store the count after the last div
384 if (entry < count) {
385 divs[entry]++;
386 }
387 }
388 // 3) Now convert the counts between divs to pixel indices, incorporating the subset's offset.
389 int offset = subsetStart;
390 for (int i = 0; i < count; ++i) {
391 divs[i] += offset;
392 offset = divs[i];
393 }
394}
395
396GR_DRAW_OP_TEST_DEFINE(NonAALatticeOp) {
Brian Salomon815486c2017-07-11 08:52:13 -0400397 SkCanvas::Lattice lattice;
Brian Salomon18df7632017-07-11 14:32:18 -0400398 // We loop because our random lattice code can produce an invalid lattice in the case where
399 // there is a single div separator in both x and y and both are aligned with the left and top
400 // edge of the image subset, respectively.
401 std::unique_ptr<int[]> xdivs;
402 std::unique_ptr<int[]> ydivs;
Stan Ilievca8c0952017-12-11 13:01:58 -0500403 std::unique_ptr<SkCanvas::Lattice::RectType[]> flags;
404 std::unique_ptr<SkColor[]> colors;
Brian Salomon18df7632017-07-11 14:32:18 -0400405 SkIRect subset;
Brian Salomon2a943df2018-05-04 13:43:19 -0400406 GrSurfaceDesc desc;
407 desc.fConfig = kRGBA_8888_GrPixelConfig;
408 desc.fWidth = random->nextRangeU(1, 1000);
409 desc.fHeight = random->nextRangeU(1, 1000);
Robert Phillips0a15cc62019-07-30 12:49:10 -0400410 GrSurfaceOrigin origin = random->nextBool() ? kTopLeft_GrSurfaceOrigin
411 : kBottomLeft_GrSurfaceOrigin;
Greg Daniel4065d452018-11-16 15:43:41 -0500412 const GrBackendFormat format =
Robert Phillips0a15cc62019-07-30 12:49:10 -0400413 context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
414 GrRenderable::kNo);
Brian Salomonbeb7f522019-08-30 16:19:42 -0400415 auto proxy = context->priv().proxyProvider()->createProxy(format,
416 desc,
417 GrRenderable::kNo,
418 1,
419 origin,
420 GrMipMapped::kNo,
421 SkBackingFit::kExact,
422 SkBudgeted::kYes,
423 GrProtected::kNo);
Brian Salomon2a943df2018-05-04 13:43:19 -0400424
Brian Salomon18df7632017-07-11 14:32:18 -0400425 do {
Brian Salomon18df7632017-07-11 14:32:18 -0400426 if (random->nextBool()) {
Brian Salomon2a943df2018-05-04 13:43:19 -0400427 subset.fLeft = random->nextULessThan(desc.fWidth);
428 subset.fRight = random->nextRangeU(subset.fLeft + 1, desc.fWidth);
429 subset.fTop = random->nextULessThan(desc.fHeight);
430 subset.fBottom = random->nextRangeU(subset.fTop + 1, desc.fHeight);
Brian Salomon18df7632017-07-11 14:32:18 -0400431 } else {
Brian Salomon2a943df2018-05-04 13:43:19 -0400432 subset.setXYWH(0, 0, desc.fWidth, desc.fHeight);
Brian Salomon815486c2017-07-11 08:52:13 -0400433 }
Brian Salomon2a943df2018-05-04 13:43:19 -0400434 // SkCanvas::Lattice allows bounds to be null. However, SkCanvas creates a temp Lattice with
435 // a non-null bounds before creating a SkLatticeIter since SkLatticeIter requires a bounds.
Brian Salomon18df7632017-07-11 14:32:18 -0400436 lattice.fBounds = &subset;
437 lattice.fXCount = random->nextRangeU(1, subset.width());
438 lattice.fYCount = random->nextRangeU(1, subset.height());
439 xdivs.reset(new int[lattice.fXCount]);
440 ydivs.reset(new int[lattice.fYCount]);
441 init_random_divs(xdivs.get(), lattice.fXCount, subset.fLeft, subset.fRight, random);
442 init_random_divs(ydivs.get(), lattice.fYCount, subset.fTop, subset.fBottom, random);
443 lattice.fXDivs = xdivs.get();
444 lattice.fYDivs = ydivs.get();
445 bool hasFlags = random->nextBool();
446 if (hasFlags) {
447 int n = (lattice.fXCount + 1) * (lattice.fYCount + 1);
Stan Ilievca8c0952017-12-11 13:01:58 -0500448 flags.reset(new SkCanvas::Lattice::RectType[n]);
449 colors.reset(new SkColor[n]);
Brian Salomon18df7632017-07-11 14:32:18 -0400450 for (int i = 0; i < n; ++i) {
Stan Ilievca8c0952017-12-11 13:01:58 -0500451 flags[i] = random->nextBool() ? SkCanvas::Lattice::kTransparent
452 : SkCanvas::Lattice::kDefault;
Brian Salomon18df7632017-07-11 14:32:18 -0400453 }
Stan Ilievca8c0952017-12-11 13:01:58 -0500454 lattice.fRectTypes = flags.get();
455 lattice.fColors = colors.get();
Brian Salomon18df7632017-07-11 14:32:18 -0400456 } else {
Stan Ilievca8c0952017-12-11 13:01:58 -0500457 lattice.fRectTypes = nullptr;
458 lattice.fColors = nullptr;
Brian Salomon18df7632017-07-11 14:32:18 -0400459 }
Brian Salomon2a943df2018-05-04 13:43:19 -0400460 } while (!SkLatticeIter::Valid(desc.fWidth, desc.fHeight, lattice));
Brian Salomon815486c2017-07-11 08:52:13 -0400461 SkRect dst;
462 dst.fLeft = random->nextRangeScalar(-2000.5f, 1000.f);
463 dst.fTop = random->nextRangeScalar(-2000.5f, 1000.f);
464 dst.fRight = dst.fLeft + random->nextRangeScalar(0.5f, 1000.f);
465 dst.fBottom = dst.fTop + random->nextRangeScalar(0.5f, 1000.f);
466 std::unique_ptr<SkLatticeIter> iter(new SkLatticeIter(lattice, dst));
467 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
Brian Salomon2a943df2018-05-04 13:43:19 -0400468 auto csxf = GrTest::TestColorXform(random);
469 GrSamplerState::Filter filter =
470 random->nextBool() ? GrSamplerState::Filter::kNearest : GrSamplerState::Filter::kBilerp;
Greg Danieled96bca2019-12-05 15:05:54 -0500471
472 GrSurfaceProxyView view(
473 std::move(proxy), origin,
Greg Daniel14b57212019-12-17 16:18:06 -0500474 context->priv().caps()->getReadSwizzle(format, GrColorType::kRGBA_8888));
Greg Danieled96bca2019-12-05 15:05:54 -0500475
476 return NonAALatticeOp::Make(context, std::move(paint), viewMatrix, std::move(view),
Greg Danielc594e622019-10-15 14:01:49 -0400477 GrColorType::kRGBA_8888, std::move(csxf), filter, std::move(iter),
478 dst);
Brian Salomon815486c2017-07-11 08:52:13 -0400479}
480
481#endif