blob: d9198b27331dfe2ceab3bab46310d28dcd4686eb [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"
Robert Phillipse37f1c42020-03-09 16:10:18 -040016#include "src/gpu/GrProgramInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/GrResourceProvider.h"
18#include "src/gpu/GrResourceProviderPriv.h"
19#include "src/gpu/GrVertexWriter.h"
20#include "src/gpu/SkGr.h"
21#include "src/gpu/glsl/GrGLSLColorSpaceXformHelper.h"
Robert Phillips03e4c952019-11-26 16:20:22 -050022#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
24#include "src/gpu/glsl/GrGLSLVarying.h"
25#include "src/gpu/ops/GrLatticeOp.h"
26#include "src/gpu/ops/GrMeshDrawOp.h"
27#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
joshualitt33a5fce2015-11-18 13:28:51 -080028
Brian Salomon815486c2017-07-11 08:52:13 -040029namespace {
30
Brian Salomon2a943df2018-05-04 13:43:19 -040031class LatticeGP : public GrGeometryProcessor {
32public:
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050033 static GrGeometryProcessor* Make(SkArenaAlloc* arena,
Greg Danieled96bca2019-12-05 15:05:54 -050034 const GrSurfaceProxyView& view,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050035 sk_sp<GrColorSpaceXform> csxf,
36 GrSamplerState::Filter filter,
37 bool wideColor) {
Greg Danieled96bca2019-12-05 15:05:54 -050038 return arena->make<LatticeGP>(view, std::move(csxf), filter, wideColor);
Brian Salomon2a943df2018-05-04 13:43:19 -040039 }
40
41 const char* name() const override { return "LatticeGP"; }
42
43 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
44 b->add32(GrColorSpaceXform::XformKey(fColorSpaceXform.get()));
45 }
46
47 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps& caps) const override {
48 class GLSLProcessor : public GrGLSLGeometryProcessor {
49 public:
50 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc,
Brian Salomonc241b582019-11-27 08:57:17 -050051 const CoordTransformRange& transformRange) override {
Brian Salomon2a943df2018-05-04 13:43:19 -040052 const auto& latticeGP = proc.cast<LatticeGP>();
Brian Salomonc241b582019-11-27 08:57:17 -050053 this->setTransformDataHelper(SkMatrix::I(), pdman, transformRange);
Brian Osmanc891b102018-06-14 14:50:17 -040054 fColorSpaceXformHelper.setData(pdman, latticeGP.fColorSpaceXform.get());
Brian Salomon2a943df2018-05-04 13:43:19 -040055 }
56
57 private:
58 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
59 using Interpolation = GrGLSLVaryingHandler::Interpolation;
60 const auto& latticeGP = args.fGP.cast<LatticeGP>();
61 fColorSpaceXformHelper.emitCode(args.fUniformHandler,
62 latticeGP.fColorSpaceXform.get());
63
64 args.fVaryingHandler->emitAttributes(latticeGP);
Brian Osmanf04fb3c2018-11-12 15:34:00 -050065 this->writeOutputPosition(args.fVertBuilder, gpArgs, latticeGP.fInPosition.name());
Brian Salomon2a943df2018-05-04 13:43:19 -040066 this->emitTransforms(args.fVertBuilder,
67 args.fVaryingHandler,
68 args.fUniformHandler,
Brian Osmanf04fb3c2018-11-12 15:34:00 -050069 latticeGP.fInTextureCoords.asShaderVar(),
Brian Salomon2a943df2018-05-04 13:43:19 -040070 args.fFPCoordTransformHandler);
71 args.fFragBuilder->codeAppend("float2 textureCoords;");
Brian Osmanf04fb3c2018-11-12 15:34:00 -050072 args.fVaryingHandler->addPassThroughAttribute(latticeGP.fInTextureCoords,
Brian Salomon2a943df2018-05-04 13:43:19 -040073 "textureCoords");
74 args.fFragBuilder->codeAppend("float4 textureDomain;");
75 args.fVaryingHandler->addPassThroughAttribute(
Brian Osmanf04fb3c2018-11-12 15:34:00 -050076 latticeGP.fInTextureDomain, "textureDomain", Interpolation::kCanBeFlat);
77 args.fVaryingHandler->addPassThroughAttribute(latticeGP.fInColor,
78 args.fOutputColor,
Brian Salomon2a943df2018-05-04 13:43:19 -040079 Interpolation::kCanBeFlat);
80 args.fFragBuilder->codeAppendf("%s = ", args.fOutputColor);
Brian Salomon87e9ddb2019-12-19 14:50:22 -050081 args.fFragBuilder->appendTextureLookupAndBlend(
Brian Salomon2a943df2018-05-04 13:43:19 -040082 args.fOutputColor,
Brian Salomon87e9ddb2019-12-19 14:50:22 -050083 SkBlendMode::kModulate,
Brian Salomon2a943df2018-05-04 13:43:19 -040084 args.fTexSamplers[0],
85 "clamp(textureCoords, textureDomain.xy, textureDomain.zw)",
Brian Salomon2a943df2018-05-04 13:43:19 -040086 &fColorSpaceXformHelper);
87 args.fFragBuilder->codeAppend(";");
88 args.fFragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage);
89 }
90 GrGLSLColorSpaceXformHelper fColorSpaceXformHelper;
91 };
92 return new GLSLProcessor;
93 }
94
95private:
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050096 friend class ::SkArenaAlloc; // for access to ctor
97
Greg Danieled96bca2019-12-05 15:05:54 -050098 LatticeGP(const GrSurfaceProxyView& view, sk_sp<GrColorSpaceXform> csxf,
Brian Osman0b537032018-12-26 12:16:44 -050099 GrSamplerState::Filter filter, bool wideColor)
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500100 : INHERITED(kLatticeGP_ClassID)
101 , fColorSpaceXform(std::move(csxf)) {
Greg Daniel7a82edf2018-12-04 10:54:34 -0500102
Robert Phillips323471e2019-11-11 11:33:37 -0500103 fSampler.reset(GrSamplerState(GrSamplerState::WrapMode::kClamp, filter),
Greg Danieled96bca2019-12-05 15:05:54 -0500104 view.proxy()->backendFormat(), view.swizzle());
Brian Salomonf7dcd762018-07-30 14:48:15 -0400105 this->setTextureSamplerCnt(1);
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500106 fInPosition = {"position", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
107 fInTextureCoords = {"textureCoords", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
108 fInTextureDomain = {"textureDomain", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
Brian Osman0b537032018-12-26 12:16:44 -0500109 fInColor = MakeColorAttribute("color", wideColor);
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500110 this->setVertexAttributes(&fInPosition, 4);
Brian Salomon92be2f72018-06-19 14:33:47 -0400111 }
112
Brian Salomonf7dcd762018-07-30 14:48:15 -0400113 const TextureSampler& onTextureSampler(int) const override { return fSampler; }
114
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500115 Attribute fInPosition;
116 Attribute fInTextureCoords;
117 Attribute fInTextureDomain;
118 Attribute fInColor;
Brian Salomon92be2f72018-06-19 14:33:47 -0400119
Brian Salomon2a943df2018-05-04 13:43:19 -0400120 sk_sp<GrColorSpaceXform> fColorSpaceXform;
121 TextureSampler fSampler;
122
123 typedef GrGeometryProcessor INHERITED;
124};
125
Brian Salomon815486c2017-07-11 08:52:13 -0400126class NonAALatticeOp final : public GrMeshDrawOp {
127private:
128 using Helper = GrSimpleMeshDrawOpHelper;
129
joshualitt33a5fce2015-11-18 13:28:51 -0800130public:
Brian Salomon25a88092016-12-01 09:36:50 -0500131 DEFINE_OP_CLASS_ID
joshualitt33a5fce2015-11-18 13:28:51 -0800132
Robert Phillipsb97da532019-02-12 15:24:12 -0500133 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400134 GrPaint&& paint,
135 const SkMatrix& viewMatrix,
Greg Danieled96bca2019-12-05 15:05:54 -0500136 GrSurfaceProxyView view,
Greg Daniel82c6b102020-01-21 10:33:22 -0500137 SkAlphaType alphaType,
Brian Salomon2a943df2018-05-04 13:43:19 -0400138 sk_sp<GrColorSpaceXform> colorSpaceXForm,
139 GrSamplerState::Filter filter,
Robert Phillips7c525e62018-06-12 10:11:12 -0400140 std::unique_ptr<SkLatticeIter> iter,
141 const SkRect& dst) {
Greg Danieled96bca2019-12-05 15:05:54 -0500142 SkASSERT(view.proxy());
Robert Phillips7c525e62018-06-12 10:11:12 -0400143 return Helper::FactoryHelper<NonAALatticeOp>(context, std::move(paint), viewMatrix,
Greg Daniel82c6b102020-01-21 10:33:22 -0500144 std::move(view), alphaType,
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,
Greg Danieled96bca2019-12-05 15:05:54 -0500150 const SkMatrix& viewMatrix, GrSurfaceProxyView view,
Greg Daniel82c6b102020-01-21 10:33:22 -0500151 SkAlphaType alphaType, sk_sp<GrColorSpaceXform> colorSpaceXform,
Greg Danielc594e622019-10-15 14:01:49 -0400152 GrSamplerState::Filter filter, std::unique_ptr<SkLatticeIter> iter,
153 const SkRect& dst)
Brian Salomon2a943df2018-05-04 13:43:19 -0400154 : INHERITED(ClassID())
155 , fHelper(helperArgs, GrAAType::kNone)
Greg Danieled96bca2019-12-05 15:05:54 -0500156 , fView(std::move(view))
Greg Daniel82c6b102020-01-21 10:33:22 -0500157 , fAlphaType(alphaType)
Brian Salomon2a943df2018-05-04 13:43:19 -0400158 , fColorSpaceXform(std::move(colorSpaceXform))
159 , fFilter(filter) {
bsalomona71b8982016-06-30 12:13:52 -0700160 Patch& patch = fPatches.push_back();
161 patch.fViewMatrix = viewMatrix;
162 patch.fColor = color;
msarett10e3d9b2016-08-18 15:46:03 -0700163 patch.fIter = std::move(iter);
bsalomona71b8982016-06-30 12:13:52 -0700164 patch.fDst = dst;
joshualitt33a5fce2015-11-18 13:28:51 -0800165
joshualitt33a5fce2015-11-18 13:28:51 -0800166 // setup bounds
Greg Daniel5faf4742019-10-01 15:14:44 -0400167 this->setTransformedBounds(patch.fDst, viewMatrix, HasAABloat::kNo, IsHairline::kNo);
joshualitt33a5fce2015-11-18 13:28:51 -0800168 }
169
Brian Salomonfc527d22016-12-14 21:07:01 -0500170 const char* name() const override { return "NonAALatticeOp"; }
robertphillips783a4da2015-11-19 14:00:02 -0800171
Chris Dalton1706cbf2019-05-21 19:35:29 -0600172 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipse37f1c42020-03-09 16:10:18 -0400173 if (fProgramInfo) {
174 fProgramInfo->visitProxies(func);
175 } else {
176 bool mipped = (GrSamplerState::Filter::kMipMap == fFilter);
177 func(fView.proxy(), GrMipMapped(mipped));
178 fHelper.visitProxies(func);
179 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400180 }
181
Brian Osman9a390ac2018-11-12 09:47:48 -0500182#ifdef SK_DEBUG
robertphillips783a4da2015-11-19 14:00:02 -0800183 SkString dumpInfo() const override {
184 SkString str;
185
bsalomona71b8982016-06-30 12:13:52 -0700186 for (int i = 0; i < fPatches.count(); ++i) {
Brian Salomonfc527d22016-12-14 21:07:01 -0500187 str.appendf("%d: Color: 0x%08x Dst [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", i,
Brian Osmancf860852018-10-31 14:04:39 -0400188 fPatches[i].fColor.toBytes_RGBA(), fPatches[i].fDst.fLeft,
Brian Osman1be2b7c2018-10-29 16:07:15 -0400189 fPatches[i].fDst.fTop, fPatches[i].fDst.fRight, fPatches[i].fDst.fBottom);
robertphillips783a4da2015-11-19 14:00:02 -0800190 }
191
Brian Salomon815486c2017-07-11 08:52:13 -0400192 str += fHelper.dumpInfo();
193 str += INHERITED::dumpInfo();
robertphillips783a4da2015-11-19 14:00:02 -0800194 return str;
195 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500196#endif
joshualitt33a5fce2015-11-18 13:28:51 -0800197
Brian Salomon815486c2017-07-11 08:52:13 -0400198 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
199
Chris Dalton6ce447a2019-06-23 18:07:38 -0600200 GrProcessorSet::Analysis finalize(
201 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
202 GrClampType clampType) override {
Greg Daniel82c6b102020-01-21 10:33:22 -0500203 auto opaque = fPatches[0].fColor.isOpaque() && fAlphaType == kOpaque_SkAlphaType
Brian Salomon2a943df2018-05-04 13:43:19 -0400204 ? GrProcessorAnalysisColor::Opaque::kYes
205 : GrProcessorAnalysisColor::Opaque::kNo;
206 auto analysisColor = GrProcessorAnalysisColor(opaque);
Chris Dalton6ce447a2019-06-23 18:07:38 -0600207 auto result = fHelper.finalizeProcessors(
208 caps, clip, hasMixedSampledCoverage, clampType, GrProcessorAnalysisCoverage::kNone,
209 &analysisColor);
Brian Salomon2a943df2018-05-04 13:43:19 -0400210 analysisColor.isConstant(&fPatches[0].fColor);
Brian Osman2715bf52019-12-06 14:38:47 -0500211 fWideColor = !fPatches[0].fColor.fitsInBytes();
Brian Salomon2a943df2018-05-04 13:43:19 -0400212 return result;
Brian Salomon815486c2017-07-11 08:52:13 -0400213 }
214
Brian Salomon92aee3d2016-12-21 09:20:25 -0500215private:
Robert Phillipse37f1c42020-03-09 16:10:18 -0400216 GrProgramInfo* createProgramInfo(const GrCaps* caps,
217 SkArenaAlloc* arena,
218 const GrSurfaceProxyView* outputView,
219 GrAppliedClip&& appliedClip,
220 const GrXferProcessor::DstProxyView& dstProxyView) {
221
222 auto gp = LatticeGP::Make(arena, fView, fColorSpaceXform, fFilter, fWideColor);
joshualitt33a5fce2015-11-18 13:28:51 -0800223 if (!gp) {
Robert Phillipse37f1c42020-03-09 16:10:18 -0400224 return nullptr;
225 }
226
227 static constexpr int kOnePrimProcTexture = 1;
228 auto fixedDynamicState = GrMeshDrawOp::Target::MakeFixedDynamicState(arena, &appliedClip,
229 kOnePrimProcTexture);
230 fixedDynamicState->fPrimitiveProcessorTextures[0] = fView.proxy();
231
232 return GrSimpleMeshDrawOpHelper::CreateProgramInfo(caps, arena, outputView,
233 std::move(appliedClip), dstProxyView,
234 gp, fHelper.detachProcessorSet(),
235 GrPrimitiveType::kTriangles,
236 fHelper.pipelineFlags(),
237 &GrUserStencilSettings::kUnused,
238 fixedDynamicState);
239 }
240
241 GrProgramInfo* createProgramInfo(Target* target) {
242 return this->createProgramInfo(&target->caps(),
243 target->allocator(),
244 target->outputView(),
245 target->detachAppliedClip(),
246 target->dstProxyView());
247 }
248
249 void onPrePrepareDraws(GrRecordingContext* context,
250 const GrSurfaceProxyView* outputView,
251 GrAppliedClip* clip,
252 const GrXferProcessor::DstProxyView& dstProxyView) override {
253 SkArenaAlloc* arena = context->priv().recordTimeAllocator();
254
255 // This is equivalent to a GrOpFlushState::detachAppliedClip
256 GrAppliedClip appliedClip = clip ? std::move(*clip) : GrAppliedClip();
257
258 fProgramInfo = this->createProgramInfo(context->priv().caps(), arena, outputView,
259 std::move(appliedClip), dstProxyView);
260
261 context->priv().recordProgramInfo(fProgramInfo);
262 }
263
264 void onPrepareDraws(Target* target) override {
265 if (!fProgramInfo) {
266 fProgramInfo = this->createProgramInfo(target);
267 if (!fProgramInfo) {
268 return;
269 }
joshualitt33a5fce2015-11-18 13:28:51 -0800270 }
271
bsalomona71b8982016-06-30 12:13:52 -0700272 int patchCnt = fPatches.count();
msarett10e3d9b2016-08-18 15:46:03 -0700273 int numRects = 0;
274 for (int i = 0; i < patchCnt; i++) {
msarett0764efe2016-09-02 11:24:30 -0700275 numRects += fPatches[i].fIter->numRectsToDraw();
msarett10e3d9b2016-08-18 15:46:03 -0700276 }
joshualitt33a5fce2015-11-18 13:28:51 -0800277
Brian Salomon0db1b532017-07-12 15:21:43 -0400278 if (!numRects) {
279 return;
280 }
281
Robert Phillipse37f1c42020-03-09 16:10:18 -0400282 const size_t kVertexStride = fProgramInfo->primProc().vertexStride();
Robert Phillipse94cdd22019-11-04 14:15:58 -0500283
284 QuadHelper helper(target, kVertexStride, numRects);
285
Brian Osmanc3064e72018-11-15 08:59:22 -0500286 GrVertexWriter vertices{helper.vertices()};
Brian Salomon12d22642019-01-29 14:38:50 -0500287 if (!vertices.fPtr) {
joshualitt33a5fce2015-11-18 13:28:51 -0800288 SkDebugf("Could not allocate vertices\n");
289 return;
290 }
291
bsalomona71b8982016-06-30 12:13:52 -0700292 for (int i = 0; i < patchCnt; i++) {
msarett7fc08582016-08-18 14:29:22 -0700293 const Patch& patch = fPatches[i];
Brian Osmanc3064e72018-11-15 08:59:22 -0500294
Brian Osman0b537032018-12-26 12:16:44 -0500295 GrVertexColor patchColor(patch.fColor, fWideColor);
msarett10e3d9b2016-08-18 15:46:03 -0700296
297 // Apply the view matrix here if it is scale-translate. Otherwise, we need to
298 // wait until we've created the dst rects.
299 bool isScaleTranslate = patch.fViewMatrix.isScaleTranslate();
300 if (isScaleTranslate) {
301 patch.fIter->mapDstScaleTranslate(patch.fViewMatrix);
302 }
joshualitt33a5fce2015-11-18 13:28:51 -0800303
Brian Salomon2a943df2018-05-04 13:43:19 -0400304 SkIRect srcR;
305 SkRect dstR;
Brian Osmanc3064e72018-11-15 08:59:22 -0500306 SkPoint* patchPositions = reinterpret_cast<SkPoint*>(vertices.fPtr);
Greg Danieled96bca2019-12-05 15:05:54 -0500307 Sk4f scales(1.f / fView.proxy()->width(), 1.f / fView.proxy()->height(),
308 1.f / fView.proxy()->width(), 1.f / fView.proxy()->height());
Brian Salomon2a943df2018-05-04 13:43:19 -0400309 static const Sk4f kDomainOffsets(0.5f, 0.5f, -0.5f, -0.5f);
Brian Osmanc3064e72018-11-15 08:59:22 -0500310 static const Sk4f kFlipOffsets(0.f, 1.f, 0.f, 1.f);
Brian Salomon2a943df2018-05-04 13:43:19 -0400311 static const Sk4f kFlipMuls(1.f, -1.f, 1.f, -1.f);
msarett10e3d9b2016-08-18 15:46:03 -0700312 while (patch.fIter->next(&srcR, &dstR)) {
Brian Salomon2a943df2018-05-04 13:43:19 -0400313 Sk4f coords(SkIntToScalar(srcR.fLeft), SkIntToScalar(srcR.fTop),
314 SkIntToScalar(srcR.fRight), SkIntToScalar(srcR.fBottom));
315 Sk4f domain = coords + kDomainOffsets;
316 coords *= scales;
317 domain *= scales;
Greg Danieled96bca2019-12-05 15:05:54 -0500318 if (fView.origin() == kBottomLeft_GrSurfaceOrigin) {
Brian Salomon2a943df2018-05-04 13:43:19 -0400319 coords = kFlipMuls * coords + kFlipOffsets;
320 domain = SkNx_shuffle<0, 3, 2, 1>(kFlipMuls * domain + kFlipOffsets);
321 }
Brian Osman4486d982018-11-15 15:56:04 -0500322 SkRect texDomain;
323 SkRect texCoords;
324 domain.store(&texDomain);
325 coords.store(&texCoords);
joshualitt33a5fce2015-11-18 13:28:51 -0800326
Brian Osman0dd43022018-11-16 15:53:26 -0500327 vertices.writeQuad(GrVertexWriter::TriStripFromRect(dstR),
328 GrVertexWriter::TriStripFromRect(texCoords),
Brian Osmanc3064e72018-11-15 08:59:22 -0500329 texDomain,
330 patchColor);
joshualitt33a5fce2015-11-18 13:28:51 -0800331 }
msarett10e3d9b2016-08-18 15:46:03 -0700332
333 // If we didn't handle it above, apply the matrix here.
334 if (!isScaleTranslate) {
Robert Phillipsee08d522019-10-28 16:34:44 -0400335 SkMatrixPriv::MapPointsWithStride(
336 patch.fViewMatrix, patchPositions, kVertexStride,
337 GrResourceProvider::NumVertsPerNonAAQuad() * patch.fIter->numRectsToDraw());
msarett10e3d9b2016-08-18 15:46:03 -0700338 }
joshualitt33a5fce2015-11-18 13:28:51 -0800339 }
Robert Phillipse37f1c42020-03-09 16:10:18 -0400340
341 fMesh = helper.mesh();
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700342 }
343
344 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillipse37f1c42020-03-09 16:10:18 -0400345 if (!fProgramInfo || !fMesh) {
346 return;
347 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500348
Robert Phillipse37f1c42020-03-09 16:10:18 -0400349 flushState->opsRenderPass()->bindPipeline(*fProgramInfo, chainBounds);
350 flushState->opsRenderPass()->drawMeshes(*fProgramInfo, fMesh, 1);
joshualitt33a5fce2015-11-18 13:28:51 -0800351 }
352
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500353 CombineResult onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*,
354 const GrCaps& caps) override {
Brian Salomonfc527d22016-12-14 21:07:01 -0500355 NonAALatticeOp* that = t->cast<NonAALatticeOp>();
Greg Danieled96bca2019-12-05 15:05:54 -0500356 if (fView != that->fView) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000357 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400358 }
359 if (fFilter != that->fFilter) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000360 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400361 }
362 if (GrColorSpaceXform::Equals(fColorSpaceXform.get(), that->fColorSpaceXform.get())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000363 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400364 }
Brian Salomon815486c2017-07-11 08:52:13 -0400365 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000366 return CombineResult::kCannotCombine;
joshualitt33a5fce2015-11-18 13:28:51 -0800367 }
368
msarett10e3d9b2016-08-18 15:46:03 -0700369 fPatches.move_back_n(that->fPatches.count(), that->fPatches.begin());
Brian Osman0b537032018-12-26 12:16:44 -0500370 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000371 return CombineResult::kMerged;
joshualitt33a5fce2015-11-18 13:28:51 -0800372 }
373
bsalomona71b8982016-06-30 12:13:52 -0700374 struct Patch {
375 SkMatrix fViewMatrix;
msarett10e3d9b2016-08-18 15:46:03 -0700376 std::unique_ptr<SkLatticeIter> fIter;
bsalomona71b8982016-06-30 12:13:52 -0700377 SkRect fDst;
Brian Osmancf860852018-10-31 14:04:39 -0400378 SkPMColor4f fColor;
bsalomona71b8982016-06-30 12:13:52 -0700379 };
380
Brian Salomon815486c2017-07-11 08:52:13 -0400381 Helper fHelper;
382 SkSTArray<1, Patch, true> fPatches;
Greg Danieled96bca2019-12-05 15:05:54 -0500383 GrSurfaceProxyView fView;
Greg Daniel82c6b102020-01-21 10:33:22 -0500384 SkAlphaType fAlphaType;
Brian Salomon2a943df2018-05-04 13:43:19 -0400385 sk_sp<GrColorSpaceXform> fColorSpaceXform;
386 GrSamplerState::Filter fFilter;
Brian Osman0b537032018-12-26 12:16:44 -0500387 bool fWideColor;
joshualitt33a5fce2015-11-18 13:28:51 -0800388
Robert Phillipse37f1c42020-03-09 16:10:18 -0400389 GrMesh* fMesh = nullptr;
390 GrProgramInfo* fProgramInfo = nullptr;
391
Brian Salomon815486c2017-07-11 08:52:13 -0400392 typedef GrMeshDrawOp INHERITED;
joshualitt33a5fce2015-11-18 13:28:51 -0800393};
394
Brian Salomon815486c2017-07-11 08:52:13 -0400395} // anonymous namespace
396
Brian Salomonfc527d22016-12-14 21:07:01 -0500397namespace GrLatticeOp {
Robert Phillipsb97da532019-02-12 15:24:12 -0500398std::unique_ptr<GrDrawOp> MakeNonAA(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400399 GrPaint&& paint,
400 const SkMatrix& viewMatrix,
Greg Danieled96bca2019-12-05 15:05:54 -0500401 GrSurfaceProxyView view,
Greg Daniel82c6b102020-01-21 10:33:22 -0500402 SkAlphaType alphaType,
Brian Salomon2a943df2018-05-04 13:43:19 -0400403 sk_sp<GrColorSpaceXform> colorSpaceXform,
404 GrSamplerState::Filter filter,
Robert Phillips7c525e62018-06-12 10:11:12 -0400405 std::unique_ptr<SkLatticeIter> iter,
406 const SkRect& dst) {
Greg Daniel82c6b102020-01-21 10:33:22 -0500407 return NonAALatticeOp::Make(context, std::move(paint), viewMatrix, std::move(view), alphaType,
408 std::move(colorSpaceXform), filter, std::move(iter), dst);
joshualitt33a5fce2015-11-18 13:28:51 -0800409}
410};
Brian Salomon815486c2017-07-11 08:52:13 -0400411
412#if GR_TEST_UTILS
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500413#include "src/gpu/GrProxyProvider.h"
414#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon815486c2017-07-11 08:52:13 -0400415
416/** Randomly divides subset into count divs. */
417static void init_random_divs(int divs[], int count, int subsetStart, int subsetStop,
418 SkRandom* random) {
419 // Rules for lattice divs: Must be strictly increasing and in the range
420 // [subsetStart, subsetStop).
421 // Not terribly efficient alg for generating random divs:
422 // 1) Start with minimum legal pixels between each div.
423 // 2) Randomly assign the remaining pixels of the subset to divs.
424 // 3) Convert from pixel counts to div offsets.
425
426 // 1) Initially each divs[i] represents the number of pixels between
427 // div i-1 and i. The initial div is allowed to be at subsetStart. There
428 // must be one pixel spacing between subsequent divs.
429 divs[0] = 0;
430 for (int i = 1; i < count; ++i) {
431 divs[i] = 1;
432 }
433 // 2) Assign the remaining subset pixels to fall
434 int subsetLength = subsetStop - subsetStart;
435 for (int i = 0; i < subsetLength - count; ++i) {
436 // +1 because count divs means count+1 intervals.
437 int entry = random->nextULessThan(count + 1);
438 // We don't have an entry to to store the count after the last div
439 if (entry < count) {
440 divs[entry]++;
441 }
442 }
443 // 3) Now convert the counts between divs to pixel indices, incorporating the subset's offset.
444 int offset = subsetStart;
445 for (int i = 0; i < count; ++i) {
446 divs[i] += offset;
447 offset = divs[i];
448 }
449}
450
451GR_DRAW_OP_TEST_DEFINE(NonAALatticeOp) {
Brian Salomon815486c2017-07-11 08:52:13 -0400452 SkCanvas::Lattice lattice;
Brian Salomon18df7632017-07-11 14:32:18 -0400453 // We loop because our random lattice code can produce an invalid lattice in the case where
454 // there is a single div separator in both x and y and both are aligned with the left and top
455 // edge of the image subset, respectively.
456 std::unique_ptr<int[]> xdivs;
457 std::unique_ptr<int[]> ydivs;
Stan Ilievca8c0952017-12-11 13:01:58 -0500458 std::unique_ptr<SkCanvas::Lattice::RectType[]> flags;
459 std::unique_ptr<SkColor[]> colors;
Brian Salomon18df7632017-07-11 14:32:18 -0400460 SkIRect subset;
Brian Salomona56a7462020-02-07 14:17:25 -0500461 SkISize dims;
462 dims.fWidth = random->nextRangeU(1, 1000);
463 dims.fHeight = random->nextRangeU(1, 1000);
Robert Phillips0a15cc62019-07-30 12:49:10 -0400464 GrSurfaceOrigin origin = random->nextBool() ? kTopLeft_GrSurfaceOrigin
465 : kBottomLeft_GrSurfaceOrigin;
Greg Daniel4065d452018-11-16 15:43:41 -0500466 const GrBackendFormat format =
Robert Phillips0a15cc62019-07-30 12:49:10 -0400467 context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
468 GrRenderable::kNo);
Greg Daniel47c20e82020-01-21 14:29:57 -0500469 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(format, GrColorType::kRGBA_8888);
470
Brian Salomonbeb7f522019-08-30 16:19:42 -0400471 auto proxy = context->priv().proxyProvider()->createProxy(format,
Brian Salomona56a7462020-02-07 14:17:25 -0500472 dims,
Greg Daniel47c20e82020-01-21 14:29:57 -0500473 swizzle,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400474 GrRenderable::kNo,
475 1,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400476 GrMipMapped::kNo,
477 SkBackingFit::kExact,
478 SkBudgeted::kYes,
479 GrProtected::kNo);
Brian Salomon2a943df2018-05-04 13:43:19 -0400480
Brian Salomon18df7632017-07-11 14:32:18 -0400481 do {
Brian Salomon18df7632017-07-11 14:32:18 -0400482 if (random->nextBool()) {
Brian Salomona56a7462020-02-07 14:17:25 -0500483 subset.fLeft = random->nextULessThan(dims.fWidth);
484 subset.fRight = random->nextRangeU(subset.fLeft + 1, dims.fWidth);
485 subset.fTop = random->nextULessThan(dims.fHeight);
486 subset.fBottom = random->nextRangeU(subset.fTop + 1, dims.fHeight);
Brian Salomon18df7632017-07-11 14:32:18 -0400487 } else {
Brian Salomona56a7462020-02-07 14:17:25 -0500488 subset.setXYWH(0, 0, dims.fWidth, dims.fHeight);
Brian Salomon815486c2017-07-11 08:52:13 -0400489 }
Brian Salomon2a943df2018-05-04 13:43:19 -0400490 // SkCanvas::Lattice allows bounds to be null. However, SkCanvas creates a temp Lattice with
491 // a non-null bounds before creating a SkLatticeIter since SkLatticeIter requires a bounds.
Brian Salomon18df7632017-07-11 14:32:18 -0400492 lattice.fBounds = &subset;
493 lattice.fXCount = random->nextRangeU(1, subset.width());
494 lattice.fYCount = random->nextRangeU(1, subset.height());
495 xdivs.reset(new int[lattice.fXCount]);
496 ydivs.reset(new int[lattice.fYCount]);
497 init_random_divs(xdivs.get(), lattice.fXCount, subset.fLeft, subset.fRight, random);
498 init_random_divs(ydivs.get(), lattice.fYCount, subset.fTop, subset.fBottom, random);
499 lattice.fXDivs = xdivs.get();
500 lattice.fYDivs = ydivs.get();
501 bool hasFlags = random->nextBool();
502 if (hasFlags) {
503 int n = (lattice.fXCount + 1) * (lattice.fYCount + 1);
Stan Ilievca8c0952017-12-11 13:01:58 -0500504 flags.reset(new SkCanvas::Lattice::RectType[n]);
505 colors.reset(new SkColor[n]);
Brian Salomon18df7632017-07-11 14:32:18 -0400506 for (int i = 0; i < n; ++i) {
Stan Ilievca8c0952017-12-11 13:01:58 -0500507 flags[i] = random->nextBool() ? SkCanvas::Lattice::kTransparent
508 : SkCanvas::Lattice::kDefault;
Brian Salomon18df7632017-07-11 14:32:18 -0400509 }
Stan Ilievca8c0952017-12-11 13:01:58 -0500510 lattice.fRectTypes = flags.get();
511 lattice.fColors = colors.get();
Brian Salomon18df7632017-07-11 14:32:18 -0400512 } else {
Stan Ilievca8c0952017-12-11 13:01:58 -0500513 lattice.fRectTypes = nullptr;
514 lattice.fColors = nullptr;
Brian Salomon18df7632017-07-11 14:32:18 -0400515 }
Brian Salomona56a7462020-02-07 14:17:25 -0500516 } while (!SkLatticeIter::Valid(dims.fWidth, dims.fHeight, lattice));
Brian Salomon815486c2017-07-11 08:52:13 -0400517 SkRect dst;
518 dst.fLeft = random->nextRangeScalar(-2000.5f, 1000.f);
519 dst.fTop = random->nextRangeScalar(-2000.5f, 1000.f);
520 dst.fRight = dst.fLeft + random->nextRangeScalar(0.5f, 1000.f);
521 dst.fBottom = dst.fTop + random->nextRangeScalar(0.5f, 1000.f);
522 std::unique_ptr<SkLatticeIter> iter(new SkLatticeIter(lattice, dst));
523 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
Brian Salomon2a943df2018-05-04 13:43:19 -0400524 auto csxf = GrTest::TestColorXform(random);
525 GrSamplerState::Filter filter =
526 random->nextBool() ? GrSamplerState::Filter::kNearest : GrSamplerState::Filter::kBilerp;
Greg Danieled96bca2019-12-05 15:05:54 -0500527
528 GrSurfaceProxyView view(
529 std::move(proxy), origin,
Greg Daniel14b57212019-12-17 16:18:06 -0500530 context->priv().caps()->getReadSwizzle(format, GrColorType::kRGBA_8888));
Greg Danieled96bca2019-12-05 15:05:54 -0500531
532 return NonAALatticeOp::Make(context, std::move(paint), viewMatrix, std::move(view),
Greg Daniel82c6b102020-01-21 10:33:22 -0500533 kPremul_SkAlphaType, std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -0400534}
535
536#endif