blob: 9b90934c2e402445fa69960353dc94a3d608254e [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrDrawOpTest.h"
13#include "src/gpu/GrGpu.h"
14#include "src/gpu/GrOpFlushState.h"
Robert Phillipse37f1c42020-03-09 16:10:18 -040015#include "src/gpu/GrProgramInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#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 Daniel82c6b102020-01-21 10:33:22 -0500136 SkAlphaType alphaType,
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 Daniel82c6b102020-01-21 10:33:22 -0500143 std::move(view), alphaType,
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 Daniel82c6b102020-01-21 10:33:22 -0500150 SkAlphaType alphaType, sk_sp<GrColorSpaceXform> colorSpaceXform,
Greg Danielc594e622019-10-15 14:01:49 -0400151 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 Daniel82c6b102020-01-21 10:33:22 -0500156 , fAlphaType(alphaType)
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 {
Robert Phillipse37f1c42020-03-09 16:10:18 -0400172 if (fProgramInfo) {
173 fProgramInfo->visitProxies(func);
174 } else {
175 bool mipped = (GrSamplerState::Filter::kMipMap == fFilter);
176 func(fView.proxy(), GrMipMapped(mipped));
177 fHelper.visitProxies(func);
178 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400179 }
180
Brian Osman9a390ac2018-11-12 09:47:48 -0500181#ifdef SK_DEBUG
robertphillips783a4da2015-11-19 14:00:02 -0800182 SkString dumpInfo() const override {
183 SkString str;
184
bsalomona71b8982016-06-30 12:13:52 -0700185 for (int i = 0; i < fPatches.count(); ++i) {
Brian Salomonfc527d22016-12-14 21:07:01 -0500186 str.appendf("%d: Color: 0x%08x Dst [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", i,
Brian Osmancf860852018-10-31 14:04:39 -0400187 fPatches[i].fColor.toBytes_RGBA(), fPatches[i].fDst.fLeft,
Brian Osman1be2b7c2018-10-29 16:07:15 -0400188 fPatches[i].fDst.fTop, fPatches[i].fDst.fRight, fPatches[i].fDst.fBottom);
robertphillips783a4da2015-11-19 14:00:02 -0800189 }
190
Brian Salomon815486c2017-07-11 08:52:13 -0400191 str += fHelper.dumpInfo();
192 str += INHERITED::dumpInfo();
robertphillips783a4da2015-11-19 14:00:02 -0800193 return str;
194 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500195#endif
joshualitt33a5fce2015-11-18 13:28:51 -0800196
Brian Salomon815486c2017-07-11 08:52:13 -0400197 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
198
Chris Dalton6ce447a2019-06-23 18:07:38 -0600199 GrProcessorSet::Analysis finalize(
200 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
201 GrClampType clampType) override {
Greg Daniel82c6b102020-01-21 10:33:22 -0500202 auto opaque = fPatches[0].fColor.isOpaque() && fAlphaType == kOpaque_SkAlphaType
Brian Salomon2a943df2018-05-04 13:43:19 -0400203 ? GrProcessorAnalysisColor::Opaque::kYes
204 : GrProcessorAnalysisColor::Opaque::kNo;
205 auto analysisColor = GrProcessorAnalysisColor(opaque);
Chris Dalton6ce447a2019-06-23 18:07:38 -0600206 auto result = fHelper.finalizeProcessors(
207 caps, clip, hasMixedSampledCoverage, clampType, GrProcessorAnalysisCoverage::kNone,
208 &analysisColor);
Brian Salomon2a943df2018-05-04 13:43:19 -0400209 analysisColor.isConstant(&fPatches[0].fColor);
Brian Osman2715bf52019-12-06 14:38:47 -0500210 fWideColor = !fPatches[0].fColor.fitsInBytes();
Brian Salomon2a943df2018-05-04 13:43:19 -0400211 return result;
Brian Salomon815486c2017-07-11 08:52:13 -0400212 }
213
Brian Salomon92aee3d2016-12-21 09:20:25 -0500214private:
Robert Phillips2669a7b2020-03-12 12:07:19 -0400215 GrProgramInfo* programInfo() override { return fProgramInfo; }
216
Robert Phillips4133dc42020-03-11 15:55:55 -0400217 void onCreateProgramInfo(const GrCaps* caps,
218 SkArenaAlloc* arena,
219 const GrSurfaceProxyView* outputView,
220 GrAppliedClip&& appliedClip,
221 const GrXferProcessor::DstProxyView& dstProxyView) override {
Robert Phillipse37f1c42020-03-09 16:10:18 -0400222
223 auto gp = LatticeGP::Make(arena, fView, fColorSpaceXform, fFilter, fWideColor);
joshualitt33a5fce2015-11-18 13:28:51 -0800224 if (!gp) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400225 return;
Robert Phillipse37f1c42020-03-09 16:10:18 -0400226 }
227
228 static constexpr int kOnePrimProcTexture = 1;
229 auto fixedDynamicState = GrMeshDrawOp::Target::MakeFixedDynamicState(arena, &appliedClip,
230 kOnePrimProcTexture);
231 fixedDynamicState->fPrimitiveProcessorTextures[0] = fView.proxy();
232
Robert Phillips4133dc42020-03-11 15:55:55 -0400233 fProgramInfo = GrSimpleMeshDrawOpHelper::CreateProgramInfo(caps, arena, outputView,
234 std::move(appliedClip),
235 dstProxyView, gp,
236 fHelper.detachProcessorSet(),
237 GrPrimitiveType::kTriangles,
238 fHelper.pipelineFlags(),
239 &GrUserStencilSettings::kUnused,
240 fixedDynamicState);
Robert Phillipse37f1c42020-03-09 16:10:18 -0400241 }
242
Robert Phillipse37f1c42020-03-09 16:10:18 -0400243 void onPrepareDraws(Target* target) override {
244 if (!fProgramInfo) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400245 this->createProgramInfo(target);
Robert Phillipse37f1c42020-03-09 16:10:18 -0400246 if (!fProgramInfo) {
247 return;
248 }
joshualitt33a5fce2015-11-18 13:28:51 -0800249 }
250
bsalomona71b8982016-06-30 12:13:52 -0700251 int patchCnt = fPatches.count();
msarett10e3d9b2016-08-18 15:46:03 -0700252 int numRects = 0;
253 for (int i = 0; i < patchCnt; i++) {
msarett0764efe2016-09-02 11:24:30 -0700254 numRects += fPatches[i].fIter->numRectsToDraw();
msarett10e3d9b2016-08-18 15:46:03 -0700255 }
joshualitt33a5fce2015-11-18 13:28:51 -0800256
Brian Salomon0db1b532017-07-12 15:21:43 -0400257 if (!numRects) {
258 return;
259 }
260
Robert Phillipse37f1c42020-03-09 16:10:18 -0400261 const size_t kVertexStride = fProgramInfo->primProc().vertexStride();
Robert Phillipse94cdd22019-11-04 14:15:58 -0500262
263 QuadHelper helper(target, kVertexStride, numRects);
264
Brian Osmanc3064e72018-11-15 08:59:22 -0500265 GrVertexWriter vertices{helper.vertices()};
Brian Salomon12d22642019-01-29 14:38:50 -0500266 if (!vertices.fPtr) {
joshualitt33a5fce2015-11-18 13:28:51 -0800267 SkDebugf("Could not allocate vertices\n");
268 return;
269 }
270
bsalomona71b8982016-06-30 12:13:52 -0700271 for (int i = 0; i < patchCnt; i++) {
msarett7fc08582016-08-18 14:29:22 -0700272 const Patch& patch = fPatches[i];
Brian Osmanc3064e72018-11-15 08:59:22 -0500273
Brian Osman0b537032018-12-26 12:16:44 -0500274 GrVertexColor patchColor(patch.fColor, fWideColor);
msarett10e3d9b2016-08-18 15:46:03 -0700275
276 // Apply the view matrix here if it is scale-translate. Otherwise, we need to
277 // wait until we've created the dst rects.
278 bool isScaleTranslate = patch.fViewMatrix.isScaleTranslate();
279 if (isScaleTranslate) {
280 patch.fIter->mapDstScaleTranslate(patch.fViewMatrix);
281 }
joshualitt33a5fce2015-11-18 13:28:51 -0800282
Brian Salomon2a943df2018-05-04 13:43:19 -0400283 SkIRect srcR;
284 SkRect dstR;
Brian Osmanc3064e72018-11-15 08:59:22 -0500285 SkPoint* patchPositions = reinterpret_cast<SkPoint*>(vertices.fPtr);
Greg Danieled96bca2019-12-05 15:05:54 -0500286 Sk4f scales(1.f / fView.proxy()->width(), 1.f / fView.proxy()->height(),
287 1.f / fView.proxy()->width(), 1.f / fView.proxy()->height());
Brian Salomon2a943df2018-05-04 13:43:19 -0400288 static const Sk4f kDomainOffsets(0.5f, 0.5f, -0.5f, -0.5f);
Brian Osmanc3064e72018-11-15 08:59:22 -0500289 static const Sk4f kFlipOffsets(0.f, 1.f, 0.f, 1.f);
Brian Salomon2a943df2018-05-04 13:43:19 -0400290 static const Sk4f kFlipMuls(1.f, -1.f, 1.f, -1.f);
msarett10e3d9b2016-08-18 15:46:03 -0700291 while (patch.fIter->next(&srcR, &dstR)) {
Brian Salomon2a943df2018-05-04 13:43:19 -0400292 Sk4f coords(SkIntToScalar(srcR.fLeft), SkIntToScalar(srcR.fTop),
293 SkIntToScalar(srcR.fRight), SkIntToScalar(srcR.fBottom));
294 Sk4f domain = coords + kDomainOffsets;
295 coords *= scales;
296 domain *= scales;
Greg Danieled96bca2019-12-05 15:05:54 -0500297 if (fView.origin() == kBottomLeft_GrSurfaceOrigin) {
Brian Salomon2a943df2018-05-04 13:43:19 -0400298 coords = kFlipMuls * coords + kFlipOffsets;
299 domain = SkNx_shuffle<0, 3, 2, 1>(kFlipMuls * domain + kFlipOffsets);
300 }
Brian Osman4486d982018-11-15 15:56:04 -0500301 SkRect texDomain;
302 SkRect texCoords;
303 domain.store(&texDomain);
304 coords.store(&texCoords);
joshualitt33a5fce2015-11-18 13:28:51 -0800305
Brian Osman0dd43022018-11-16 15:53:26 -0500306 vertices.writeQuad(GrVertexWriter::TriStripFromRect(dstR),
307 GrVertexWriter::TriStripFromRect(texCoords),
Brian Osmanc3064e72018-11-15 08:59:22 -0500308 texDomain,
309 patchColor);
joshualitt33a5fce2015-11-18 13:28:51 -0800310 }
msarett10e3d9b2016-08-18 15:46:03 -0700311
312 // If we didn't handle it above, apply the matrix here.
313 if (!isScaleTranslate) {
Robert Phillipsee08d522019-10-28 16:34:44 -0400314 SkMatrixPriv::MapPointsWithStride(
315 patch.fViewMatrix, patchPositions, kVertexStride,
316 GrResourceProvider::NumVertsPerNonAAQuad() * patch.fIter->numRectsToDraw());
msarett10e3d9b2016-08-18 15:46:03 -0700317 }
joshualitt33a5fce2015-11-18 13:28:51 -0800318 }
Robert Phillipse37f1c42020-03-09 16:10:18 -0400319
320 fMesh = helper.mesh();
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700321 }
322
323 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillipse37f1c42020-03-09 16:10:18 -0400324 if (!fProgramInfo || !fMesh) {
325 return;
326 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500327
Robert Phillipse37f1c42020-03-09 16:10:18 -0400328 flushState->opsRenderPass()->bindPipeline(*fProgramInfo, chainBounds);
329 flushState->opsRenderPass()->drawMeshes(*fProgramInfo, fMesh, 1);
joshualitt33a5fce2015-11-18 13:28:51 -0800330 }
331
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500332 CombineResult onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*,
333 const GrCaps& caps) override {
Brian Salomonfc527d22016-12-14 21:07:01 -0500334 NonAALatticeOp* that = t->cast<NonAALatticeOp>();
Greg Danieled96bca2019-12-05 15:05:54 -0500335 if (fView != that->fView) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000336 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400337 }
338 if (fFilter != that->fFilter) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000339 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400340 }
341 if (GrColorSpaceXform::Equals(fColorSpaceXform.get(), that->fColorSpaceXform.get())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000342 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400343 }
Brian Salomon815486c2017-07-11 08:52:13 -0400344 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000345 return CombineResult::kCannotCombine;
joshualitt33a5fce2015-11-18 13:28:51 -0800346 }
347
msarett10e3d9b2016-08-18 15:46:03 -0700348 fPatches.move_back_n(that->fPatches.count(), that->fPatches.begin());
Brian Osman0b537032018-12-26 12:16:44 -0500349 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000350 return CombineResult::kMerged;
joshualitt33a5fce2015-11-18 13:28:51 -0800351 }
352
bsalomona71b8982016-06-30 12:13:52 -0700353 struct Patch {
354 SkMatrix fViewMatrix;
msarett10e3d9b2016-08-18 15:46:03 -0700355 std::unique_ptr<SkLatticeIter> fIter;
bsalomona71b8982016-06-30 12:13:52 -0700356 SkRect fDst;
Brian Osmancf860852018-10-31 14:04:39 -0400357 SkPMColor4f fColor;
bsalomona71b8982016-06-30 12:13:52 -0700358 };
359
Brian Salomon815486c2017-07-11 08:52:13 -0400360 Helper fHelper;
361 SkSTArray<1, Patch, true> fPatches;
Greg Danieled96bca2019-12-05 15:05:54 -0500362 GrSurfaceProxyView fView;
Greg Daniel82c6b102020-01-21 10:33:22 -0500363 SkAlphaType fAlphaType;
Brian Salomon2a943df2018-05-04 13:43:19 -0400364 sk_sp<GrColorSpaceXform> fColorSpaceXform;
365 GrSamplerState::Filter fFilter;
Brian Osman0b537032018-12-26 12:16:44 -0500366 bool fWideColor;
joshualitt33a5fce2015-11-18 13:28:51 -0800367
Robert Phillipse37f1c42020-03-09 16:10:18 -0400368 GrMesh* fMesh = nullptr;
369 GrProgramInfo* fProgramInfo = nullptr;
370
Brian Salomon815486c2017-07-11 08:52:13 -0400371 typedef GrMeshDrawOp INHERITED;
joshualitt33a5fce2015-11-18 13:28:51 -0800372};
373
Brian Salomon815486c2017-07-11 08:52:13 -0400374} // anonymous namespace
375
Brian Salomonfc527d22016-12-14 21:07:01 -0500376namespace GrLatticeOp {
Robert Phillipsb97da532019-02-12 15:24:12 -0500377std::unique_ptr<GrDrawOp> MakeNonAA(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400378 GrPaint&& paint,
379 const SkMatrix& viewMatrix,
Greg Danieled96bca2019-12-05 15:05:54 -0500380 GrSurfaceProxyView view,
Greg Daniel82c6b102020-01-21 10:33:22 -0500381 SkAlphaType alphaType,
Brian Salomon2a943df2018-05-04 13:43:19 -0400382 sk_sp<GrColorSpaceXform> colorSpaceXform,
383 GrSamplerState::Filter filter,
Robert Phillips7c525e62018-06-12 10:11:12 -0400384 std::unique_ptr<SkLatticeIter> iter,
385 const SkRect& dst) {
Greg Daniel82c6b102020-01-21 10:33:22 -0500386 return NonAALatticeOp::Make(context, std::move(paint), viewMatrix, std::move(view), alphaType,
387 std::move(colorSpaceXform), filter, std::move(iter), dst);
joshualitt33a5fce2015-11-18 13:28:51 -0800388}
389};
Brian Salomon815486c2017-07-11 08:52:13 -0400390
391#if GR_TEST_UTILS
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500392#include "src/gpu/GrProxyProvider.h"
393#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon815486c2017-07-11 08:52:13 -0400394
395/** Randomly divides subset into count divs. */
396static void init_random_divs(int divs[], int count, int subsetStart, int subsetStop,
397 SkRandom* random) {
398 // Rules for lattice divs: Must be strictly increasing and in the range
399 // [subsetStart, subsetStop).
400 // Not terribly efficient alg for generating random divs:
401 // 1) Start with minimum legal pixels between each div.
402 // 2) Randomly assign the remaining pixels of the subset to divs.
403 // 3) Convert from pixel counts to div offsets.
404
405 // 1) Initially each divs[i] represents the number of pixels between
406 // div i-1 and i. The initial div is allowed to be at subsetStart. There
407 // must be one pixel spacing between subsequent divs.
408 divs[0] = 0;
409 for (int i = 1; i < count; ++i) {
410 divs[i] = 1;
411 }
412 // 2) Assign the remaining subset pixels to fall
413 int subsetLength = subsetStop - subsetStart;
414 for (int i = 0; i < subsetLength - count; ++i) {
415 // +1 because count divs means count+1 intervals.
416 int entry = random->nextULessThan(count + 1);
417 // We don't have an entry to to store the count after the last div
418 if (entry < count) {
419 divs[entry]++;
420 }
421 }
422 // 3) Now convert the counts between divs to pixel indices, incorporating the subset's offset.
423 int offset = subsetStart;
424 for (int i = 0; i < count; ++i) {
425 divs[i] += offset;
426 offset = divs[i];
427 }
428}
429
430GR_DRAW_OP_TEST_DEFINE(NonAALatticeOp) {
Brian Salomon815486c2017-07-11 08:52:13 -0400431 SkCanvas::Lattice lattice;
Brian Salomon18df7632017-07-11 14:32:18 -0400432 // We loop because our random lattice code can produce an invalid lattice in the case where
433 // there is a single div separator in both x and y and both are aligned with the left and top
434 // edge of the image subset, respectively.
435 std::unique_ptr<int[]> xdivs;
436 std::unique_ptr<int[]> ydivs;
Stan Ilievca8c0952017-12-11 13:01:58 -0500437 std::unique_ptr<SkCanvas::Lattice::RectType[]> flags;
438 std::unique_ptr<SkColor[]> colors;
Brian Salomon18df7632017-07-11 14:32:18 -0400439 SkIRect subset;
Brian Salomona56a7462020-02-07 14:17:25 -0500440 SkISize dims;
441 dims.fWidth = random->nextRangeU(1, 1000);
442 dims.fHeight = random->nextRangeU(1, 1000);
Robert Phillips0a15cc62019-07-30 12:49:10 -0400443 GrSurfaceOrigin origin = random->nextBool() ? kTopLeft_GrSurfaceOrigin
444 : kBottomLeft_GrSurfaceOrigin;
Greg Daniel4065d452018-11-16 15:43:41 -0500445 const GrBackendFormat format =
Robert Phillips0a15cc62019-07-30 12:49:10 -0400446 context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
447 GrRenderable::kNo);
Greg Daniel47c20e82020-01-21 14:29:57 -0500448 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(format, GrColorType::kRGBA_8888);
449
Brian Salomonbeb7f522019-08-30 16:19:42 -0400450 auto proxy = context->priv().proxyProvider()->createProxy(format,
Brian Salomona56a7462020-02-07 14:17:25 -0500451 dims,
Greg Daniel47c20e82020-01-21 14:29:57 -0500452 swizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400453 GrRenderable::kNo,
454 1,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400455 GrMipMapped::kNo,
456 SkBackingFit::kExact,
457 SkBudgeted::kYes,
458 GrProtected::kNo);
Brian Salomon2a943df2018-05-04 13:43:19 -0400459
Brian Salomon18df7632017-07-11 14:32:18 -0400460 do {
Brian Salomon18df7632017-07-11 14:32:18 -0400461 if (random->nextBool()) {
Brian Salomona56a7462020-02-07 14:17:25 -0500462 subset.fLeft = random->nextULessThan(dims.fWidth);
463 subset.fRight = random->nextRangeU(subset.fLeft + 1, dims.fWidth);
464 subset.fTop = random->nextULessThan(dims.fHeight);
465 subset.fBottom = random->nextRangeU(subset.fTop + 1, dims.fHeight);
Brian Salomon18df7632017-07-11 14:32:18 -0400466 } else {
Brian Salomona56a7462020-02-07 14:17:25 -0500467 subset.setXYWH(0, 0, dims.fWidth, dims.fHeight);
Brian Salomon815486c2017-07-11 08:52:13 -0400468 }
Brian Salomon2a943df2018-05-04 13:43:19 -0400469 // SkCanvas::Lattice allows bounds to be null. However, SkCanvas creates a temp Lattice with
470 // a non-null bounds before creating a SkLatticeIter since SkLatticeIter requires a bounds.
Brian Salomon18df7632017-07-11 14:32:18 -0400471 lattice.fBounds = &subset;
472 lattice.fXCount = random->nextRangeU(1, subset.width());
473 lattice.fYCount = random->nextRangeU(1, subset.height());
474 xdivs.reset(new int[lattice.fXCount]);
475 ydivs.reset(new int[lattice.fYCount]);
476 init_random_divs(xdivs.get(), lattice.fXCount, subset.fLeft, subset.fRight, random);
477 init_random_divs(ydivs.get(), lattice.fYCount, subset.fTop, subset.fBottom, random);
478 lattice.fXDivs = xdivs.get();
479 lattice.fYDivs = ydivs.get();
480 bool hasFlags = random->nextBool();
481 if (hasFlags) {
482 int n = (lattice.fXCount + 1) * (lattice.fYCount + 1);
Stan Ilievca8c0952017-12-11 13:01:58 -0500483 flags.reset(new SkCanvas::Lattice::RectType[n]);
484 colors.reset(new SkColor[n]);
Brian Salomon18df7632017-07-11 14:32:18 -0400485 for (int i = 0; i < n; ++i) {
Stan Ilievca8c0952017-12-11 13:01:58 -0500486 flags[i] = random->nextBool() ? SkCanvas::Lattice::kTransparent
487 : SkCanvas::Lattice::kDefault;
Brian Salomon18df7632017-07-11 14:32:18 -0400488 }
Stan Ilievca8c0952017-12-11 13:01:58 -0500489 lattice.fRectTypes = flags.get();
490 lattice.fColors = colors.get();
Brian Salomon18df7632017-07-11 14:32:18 -0400491 } else {
Stan Ilievca8c0952017-12-11 13:01:58 -0500492 lattice.fRectTypes = nullptr;
493 lattice.fColors = nullptr;
Brian Salomon18df7632017-07-11 14:32:18 -0400494 }
Brian Salomona56a7462020-02-07 14:17:25 -0500495 } while (!SkLatticeIter::Valid(dims.fWidth, dims.fHeight, lattice));
Brian Salomon815486c2017-07-11 08:52:13 -0400496 SkRect dst;
497 dst.fLeft = random->nextRangeScalar(-2000.5f, 1000.f);
498 dst.fTop = random->nextRangeScalar(-2000.5f, 1000.f);
499 dst.fRight = dst.fLeft + random->nextRangeScalar(0.5f, 1000.f);
500 dst.fBottom = dst.fTop + random->nextRangeScalar(0.5f, 1000.f);
501 std::unique_ptr<SkLatticeIter> iter(new SkLatticeIter(lattice, dst));
502 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
Brian Salomon2a943df2018-05-04 13:43:19 -0400503 auto csxf = GrTest::TestColorXform(random);
504 GrSamplerState::Filter filter =
505 random->nextBool() ? GrSamplerState::Filter::kNearest : GrSamplerState::Filter::kBilerp;
Greg Danieled96bca2019-12-05 15:05:54 -0500506
507 GrSurfaceProxyView view(
508 std::move(proxy), origin,
Greg Daniel14b57212019-12-17 16:18:06 -0500509 context->priv().caps()->getReadSwizzle(format, GrColorType::kRGBA_8888));
Greg Danieled96bca2019-12-05 15:05:54 -0500510
511 return NonAALatticeOp::Make(context, std::move(paint), viewMatrix, std::move(view),
Greg Daniel82c6b102020-01-21 10:33:22 -0500512 kPremul_SkAlphaType, std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -0400513}
514
515#endif