blob: 726a9d269da8ce2ff105560d9b0053b79c5da188 [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:
Brian Osman609f1592020-07-01 15:14:39 -040049 void setData(const GrGLSLProgramDataManager& pdman,
50 const GrPrimitiveProcessor& proc) override {
Brian Salomon2a943df2018-05-04 13:43:19 -040051 const auto& latticeGP = proc.cast<LatticeGP>();
Brian Osmanc891b102018-06-14 14:50:17 -040052 fColorSpaceXformHelper.setData(pdman, latticeGP.fColorSpaceXform.get());
Brian Salomon2a943df2018-05-04 13:43:19 -040053 }
54
55 private:
56 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
57 using Interpolation = GrGLSLVaryingHandler::Interpolation;
58 const auto& latticeGP = args.fGP.cast<LatticeGP>();
59 fColorSpaceXformHelper.emitCode(args.fUniformHandler,
60 latticeGP.fColorSpaceXform.get());
61
62 args.fVaryingHandler->emitAttributes(latticeGP);
Brian Osmanf04fb3c2018-11-12 15:34:00 -050063 this->writeOutputPosition(args.fVertBuilder, gpArgs, latticeGP.fInPosition.name());
Michael Ludwig553db622020-06-19 10:47:30 -040064 gpArgs->fLocalCoordVar = latticeGP.fInTextureCoords.asShaderVar();
65
Brian Salomon2a943df2018-05-04 13:43:19 -040066 args.fFragBuilder->codeAppend("float2 textureCoords;");
Brian Osmanf04fb3c2018-11-12 15:34:00 -050067 args.fVaryingHandler->addPassThroughAttribute(latticeGP.fInTextureCoords,
Brian Salomon2a943df2018-05-04 13:43:19 -040068 "textureCoords");
69 args.fFragBuilder->codeAppend("float4 textureDomain;");
70 args.fVaryingHandler->addPassThroughAttribute(
Brian Osmanf04fb3c2018-11-12 15:34:00 -050071 latticeGP.fInTextureDomain, "textureDomain", Interpolation::kCanBeFlat);
72 args.fVaryingHandler->addPassThroughAttribute(latticeGP.fInColor,
73 args.fOutputColor,
Brian Salomon2a943df2018-05-04 13:43:19 -040074 Interpolation::kCanBeFlat);
75 args.fFragBuilder->codeAppendf("%s = ", args.fOutputColor);
Brian Salomon87e9ddb2019-12-19 14:50:22 -050076 args.fFragBuilder->appendTextureLookupAndBlend(
Brian Salomon2a943df2018-05-04 13:43:19 -040077 args.fOutputColor,
Brian Salomon87e9ddb2019-12-19 14:50:22 -050078 SkBlendMode::kModulate,
Brian Salomon2a943df2018-05-04 13:43:19 -040079 args.fTexSamplers[0],
80 "clamp(textureCoords, textureDomain.xy, textureDomain.zw)",
Brian Salomon2a943df2018-05-04 13:43:19 -040081 &fColorSpaceXformHelper);
82 args.fFragBuilder->codeAppend(";");
83 args.fFragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage);
84 }
85 GrGLSLColorSpaceXformHelper fColorSpaceXformHelper;
86 };
87 return new GLSLProcessor;
88 }
89
90private:
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050091 friend class ::SkArenaAlloc; // for access to ctor
92
Greg Danieled96bca2019-12-05 15:05:54 -050093 LatticeGP(const GrSurfaceProxyView& view, sk_sp<GrColorSpaceXform> csxf,
Brian Osman0b537032018-12-26 12:16:44 -050094 GrSamplerState::Filter filter, bool wideColor)
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050095 : INHERITED(kLatticeGP_ClassID)
96 , fColorSpaceXform(std::move(csxf)) {
Greg Daniel7a82edf2018-12-04 10:54:34 -050097
Robert Phillips323471e2019-11-11 11:33:37 -050098 fSampler.reset(GrSamplerState(GrSamplerState::WrapMode::kClamp, filter),
Greg Danieled96bca2019-12-05 15:05:54 -050099 view.proxy()->backendFormat(), view.swizzle());
Brian Salomonf7dcd762018-07-30 14:48:15 -0400100 this->setTextureSamplerCnt(1);
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500101 fInPosition = {"position", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
102 fInTextureCoords = {"textureCoords", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
103 fInTextureDomain = {"textureDomain", kFloat4_GrVertexAttribType, kFloat4_GrSLType};
Brian Osman0b537032018-12-26 12:16:44 -0500104 fInColor = MakeColorAttribute("color", wideColor);
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500105 this->setVertexAttributes(&fInPosition, 4);
Brian Salomon92be2f72018-06-19 14:33:47 -0400106 }
107
Brian Salomonf7dcd762018-07-30 14:48:15 -0400108 const TextureSampler& onTextureSampler(int) const override { return fSampler; }
109
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500110 Attribute fInPosition;
111 Attribute fInTextureCoords;
112 Attribute fInTextureDomain;
113 Attribute fInColor;
Brian Salomon92be2f72018-06-19 14:33:47 -0400114
Brian Salomon2a943df2018-05-04 13:43:19 -0400115 sk_sp<GrColorSpaceXform> fColorSpaceXform;
116 TextureSampler fSampler;
117
118 typedef GrGeometryProcessor INHERITED;
119};
120
Brian Salomon815486c2017-07-11 08:52:13 -0400121class NonAALatticeOp final : public GrMeshDrawOp {
122private:
123 using Helper = GrSimpleMeshDrawOpHelper;
124
joshualitt33a5fce2015-11-18 13:28:51 -0800125public:
Brian Salomon25a88092016-12-01 09:36:50 -0500126 DEFINE_OP_CLASS_ID
joshualitt33a5fce2015-11-18 13:28:51 -0800127
Robert Phillipsb97da532019-02-12 15:24:12 -0500128 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400129 GrPaint&& paint,
130 const SkMatrix& viewMatrix,
Greg Danieled96bca2019-12-05 15:05:54 -0500131 GrSurfaceProxyView view,
Greg Daniel82c6b102020-01-21 10:33:22 -0500132 SkAlphaType alphaType,
Brian Salomon2a943df2018-05-04 13:43:19 -0400133 sk_sp<GrColorSpaceXform> colorSpaceXForm,
134 GrSamplerState::Filter filter,
Robert Phillips7c525e62018-06-12 10:11:12 -0400135 std::unique_ptr<SkLatticeIter> iter,
136 const SkRect& dst) {
Greg Danieled96bca2019-12-05 15:05:54 -0500137 SkASSERT(view.proxy());
Robert Phillips7c525e62018-06-12 10:11:12 -0400138 return Helper::FactoryHelper<NonAALatticeOp>(context, std::move(paint), viewMatrix,
Greg Daniel82c6b102020-01-21 10:33:22 -0500139 std::move(view), alphaType,
Brian Salomon2a943df2018-05-04 13:43:19 -0400140 std::move(colorSpaceXForm), filter,
141 std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -0400142 }
143
Brian Osmancf860852018-10-31 14:04:39 -0400144 NonAALatticeOp(Helper::MakeArgs& helperArgs, const SkPMColor4f& color,
Greg Danieled96bca2019-12-05 15:05:54 -0500145 const SkMatrix& viewMatrix, GrSurfaceProxyView view,
Greg Daniel82c6b102020-01-21 10:33:22 -0500146 SkAlphaType alphaType, sk_sp<GrColorSpaceXform> colorSpaceXform,
Greg Danielc594e622019-10-15 14:01:49 -0400147 GrSamplerState::Filter filter, std::unique_ptr<SkLatticeIter> iter,
148 const SkRect& dst)
Brian Salomon2a943df2018-05-04 13:43:19 -0400149 : INHERITED(ClassID())
150 , fHelper(helperArgs, GrAAType::kNone)
Greg Danieled96bca2019-12-05 15:05:54 -0500151 , fView(std::move(view))
Greg Daniel82c6b102020-01-21 10:33:22 -0500152 , fAlphaType(alphaType)
Brian Salomon2a943df2018-05-04 13:43:19 -0400153 , fColorSpaceXform(std::move(colorSpaceXform))
154 , fFilter(filter) {
bsalomona71b8982016-06-30 12:13:52 -0700155 Patch& patch = fPatches.push_back();
156 patch.fViewMatrix = viewMatrix;
157 patch.fColor = color;
msarett10e3d9b2016-08-18 15:46:03 -0700158 patch.fIter = std::move(iter);
bsalomona71b8982016-06-30 12:13:52 -0700159 patch.fDst = dst;
joshualitt33a5fce2015-11-18 13:28:51 -0800160
joshualitt33a5fce2015-11-18 13:28:51 -0800161 // setup bounds
Greg Daniel5faf4742019-10-01 15:14:44 -0400162 this->setTransformedBounds(patch.fDst, viewMatrix, HasAABloat::kNo, IsHairline::kNo);
joshualitt33a5fce2015-11-18 13:28:51 -0800163 }
164
Brian Salomonfc527d22016-12-14 21:07:01 -0500165 const char* name() const override { return "NonAALatticeOp"; }
robertphillips783a4da2015-11-19 14:00:02 -0800166
Chris Dalton1706cbf2019-05-21 19:35:29 -0600167 void visitProxies(const VisitProxyFunc& func) const override {
Brian Salomone69b9ef2020-07-22 11:18:06 -0400168 func(fView.proxy(), GrMipmapped::kNo);
Robert Phillipse37f1c42020-03-09 16:10:18 -0400169 if (fProgramInfo) {
Chris Daltonbe457422020-03-16 18:05:03 -0600170 fProgramInfo->visitFPProxies(func);
Robert Phillipse37f1c42020-03-09 16:10:18 -0400171 } else {
Robert Phillipse37f1c42020-03-09 16:10:18 -0400172 fHelper.visitProxies(func);
173 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400174 }
175
Brian Salomon815486c2017-07-11 08:52:13 -0400176 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
177
Chris Dalton6ce447a2019-06-23 18:07:38 -0600178 GrProcessorSet::Analysis finalize(
179 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
180 GrClampType clampType) override {
Greg Daniel82c6b102020-01-21 10:33:22 -0500181 auto opaque = fPatches[0].fColor.isOpaque() && fAlphaType == kOpaque_SkAlphaType
Brian Salomon2a943df2018-05-04 13:43:19 -0400182 ? GrProcessorAnalysisColor::Opaque::kYes
183 : GrProcessorAnalysisColor::Opaque::kNo;
184 auto analysisColor = GrProcessorAnalysisColor(opaque);
Chris Dalton6ce447a2019-06-23 18:07:38 -0600185 auto result = fHelper.finalizeProcessors(
186 caps, clip, hasMixedSampledCoverage, clampType, GrProcessorAnalysisCoverage::kNone,
187 &analysisColor);
Brian Salomon2a943df2018-05-04 13:43:19 -0400188 analysisColor.isConstant(&fPatches[0].fColor);
Brian Osman2715bf52019-12-06 14:38:47 -0500189 fWideColor = !fPatches[0].fColor.fitsInBytes();
Brian Salomon2a943df2018-05-04 13:43:19 -0400190 return result;
Brian Salomon815486c2017-07-11 08:52:13 -0400191 }
192
Brian Salomon92aee3d2016-12-21 09:20:25 -0500193private:
Robert Phillips2669a7b2020-03-12 12:07:19 -0400194 GrProgramInfo* programInfo() override { return fProgramInfo; }
195
Robert Phillips4133dc42020-03-11 15:55:55 -0400196 void onCreateProgramInfo(const GrCaps* caps,
197 SkArenaAlloc* arena,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400198 const GrSurfaceProxyView* writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400199 GrAppliedClip&& appliedClip,
200 const GrXferProcessor::DstProxyView& dstProxyView) override {
Robert Phillipse37f1c42020-03-09 16:10:18 -0400201
202 auto gp = LatticeGP::Make(arena, fView, fColorSpaceXform, fFilter, fWideColor);
joshualitt33a5fce2015-11-18 13:28:51 -0800203 if (!gp) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400204 return;
Robert Phillipse37f1c42020-03-09 16:10:18 -0400205 }
206
Brian Salomon8afde5f2020-04-01 16:22:00 -0400207 fProgramInfo = GrSimpleMeshDrawOpHelper::CreateProgramInfo(caps, arena, writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400208 std::move(appliedClip),
209 dstProxyView, gp,
210 fHelper.detachProcessorSet(),
211 GrPrimitiveType::kTriangles,
212 fHelper.pipelineFlags(),
Chris Dalton765ed362020-03-16 17:34:44 -0600213 &GrUserStencilSettings::kUnused);
Robert Phillipse37f1c42020-03-09 16:10:18 -0400214 }
215
Robert Phillipse37f1c42020-03-09 16:10:18 -0400216 void onPrepareDraws(Target* target) override {
217 if (!fProgramInfo) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400218 this->createProgramInfo(target);
Robert Phillipse37f1c42020-03-09 16:10:18 -0400219 if (!fProgramInfo) {
220 return;
221 }
joshualitt33a5fce2015-11-18 13:28:51 -0800222 }
223
bsalomona71b8982016-06-30 12:13:52 -0700224 int patchCnt = fPatches.count();
msarett10e3d9b2016-08-18 15:46:03 -0700225 int numRects = 0;
226 for (int i = 0; i < patchCnt; i++) {
msarett0764efe2016-09-02 11:24:30 -0700227 numRects += fPatches[i].fIter->numRectsToDraw();
msarett10e3d9b2016-08-18 15:46:03 -0700228 }
joshualitt33a5fce2015-11-18 13:28:51 -0800229
Brian Salomon0db1b532017-07-12 15:21:43 -0400230 if (!numRects) {
231 return;
232 }
233
Robert Phillipse37f1c42020-03-09 16:10:18 -0400234 const size_t kVertexStride = fProgramInfo->primProc().vertexStride();
Robert Phillipse94cdd22019-11-04 14:15:58 -0500235
236 QuadHelper helper(target, kVertexStride, numRects);
237
Brian Osmanc3064e72018-11-15 08:59:22 -0500238 GrVertexWriter vertices{helper.vertices()};
Brian Salomon12d22642019-01-29 14:38:50 -0500239 if (!vertices.fPtr) {
joshualitt33a5fce2015-11-18 13:28:51 -0800240 SkDebugf("Could not allocate vertices\n");
241 return;
242 }
243
bsalomona71b8982016-06-30 12:13:52 -0700244 for (int i = 0; i < patchCnt; i++) {
msarett7fc08582016-08-18 14:29:22 -0700245 const Patch& patch = fPatches[i];
Brian Osmanc3064e72018-11-15 08:59:22 -0500246
Brian Osman0b537032018-12-26 12:16:44 -0500247 GrVertexColor patchColor(patch.fColor, fWideColor);
msarett10e3d9b2016-08-18 15:46:03 -0700248
249 // Apply the view matrix here if it is scale-translate. Otherwise, we need to
250 // wait until we've created the dst rects.
251 bool isScaleTranslate = patch.fViewMatrix.isScaleTranslate();
252 if (isScaleTranslate) {
253 patch.fIter->mapDstScaleTranslate(patch.fViewMatrix);
254 }
joshualitt33a5fce2015-11-18 13:28:51 -0800255
Brian Salomon2a943df2018-05-04 13:43:19 -0400256 SkIRect srcR;
257 SkRect dstR;
Brian Osmanc3064e72018-11-15 08:59:22 -0500258 SkPoint* patchPositions = reinterpret_cast<SkPoint*>(vertices.fPtr);
Greg Danieled96bca2019-12-05 15:05:54 -0500259 Sk4f scales(1.f / fView.proxy()->width(), 1.f / fView.proxy()->height(),
260 1.f / fView.proxy()->width(), 1.f / fView.proxy()->height());
Brian Salomon2a943df2018-05-04 13:43:19 -0400261 static const Sk4f kDomainOffsets(0.5f, 0.5f, -0.5f, -0.5f);
Brian Osmanc3064e72018-11-15 08:59:22 -0500262 static const Sk4f kFlipOffsets(0.f, 1.f, 0.f, 1.f);
Brian Salomon2a943df2018-05-04 13:43:19 -0400263 static const Sk4f kFlipMuls(1.f, -1.f, 1.f, -1.f);
msarett10e3d9b2016-08-18 15:46:03 -0700264 while (patch.fIter->next(&srcR, &dstR)) {
Brian Salomon2a943df2018-05-04 13:43:19 -0400265 Sk4f coords(SkIntToScalar(srcR.fLeft), SkIntToScalar(srcR.fTop),
266 SkIntToScalar(srcR.fRight), SkIntToScalar(srcR.fBottom));
267 Sk4f domain = coords + kDomainOffsets;
268 coords *= scales;
269 domain *= scales;
Greg Danieled96bca2019-12-05 15:05:54 -0500270 if (fView.origin() == kBottomLeft_GrSurfaceOrigin) {
Brian Salomon2a943df2018-05-04 13:43:19 -0400271 coords = kFlipMuls * coords + kFlipOffsets;
272 domain = SkNx_shuffle<0, 3, 2, 1>(kFlipMuls * domain + kFlipOffsets);
273 }
Brian Osman4486d982018-11-15 15:56:04 -0500274 SkRect texDomain;
275 SkRect texCoords;
276 domain.store(&texDomain);
277 coords.store(&texCoords);
joshualitt33a5fce2015-11-18 13:28:51 -0800278
Brian Osman0dd43022018-11-16 15:53:26 -0500279 vertices.writeQuad(GrVertexWriter::TriStripFromRect(dstR),
280 GrVertexWriter::TriStripFromRect(texCoords),
Brian Osmanc3064e72018-11-15 08:59:22 -0500281 texDomain,
282 patchColor);
joshualitt33a5fce2015-11-18 13:28:51 -0800283 }
msarett10e3d9b2016-08-18 15:46:03 -0700284
285 // If we didn't handle it above, apply the matrix here.
286 if (!isScaleTranslate) {
Robert Phillipsee08d522019-10-28 16:34:44 -0400287 SkMatrixPriv::MapPointsWithStride(
288 patch.fViewMatrix, patchPositions, kVertexStride,
289 GrResourceProvider::NumVertsPerNonAAQuad() * patch.fIter->numRectsToDraw());
msarett10e3d9b2016-08-18 15:46:03 -0700290 }
joshualitt33a5fce2015-11-18 13:28:51 -0800291 }
Robert Phillipse37f1c42020-03-09 16:10:18 -0400292
293 fMesh = helper.mesh();
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700294 }
295
296 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillipse37f1c42020-03-09 16:10:18 -0400297 if (!fProgramInfo || !fMesh) {
298 return;
299 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500300
Chris Dalton765ed362020-03-16 17:34:44 -0600301 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
302 flushState->bindTextures(fProgramInfo->primProc(), *fView.proxy(),
303 fProgramInfo->pipeline());
304 flushState->drawMesh(*fMesh);
joshualitt33a5fce2015-11-18 13:28:51 -0800305 }
306
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500307 CombineResult onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*,
308 const GrCaps& caps) override {
Brian Salomonfc527d22016-12-14 21:07:01 -0500309 NonAALatticeOp* that = t->cast<NonAALatticeOp>();
Greg Danieled96bca2019-12-05 15:05:54 -0500310 if (fView != that->fView) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000311 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400312 }
313 if (fFilter != that->fFilter) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000314 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400315 }
316 if (GrColorSpaceXform::Equals(fColorSpaceXform.get(), that->fColorSpaceXform.get())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000317 return CombineResult::kCannotCombine;
Brian Salomon2a943df2018-05-04 13:43:19 -0400318 }
Brian Salomon815486c2017-07-11 08:52:13 -0400319 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000320 return CombineResult::kCannotCombine;
joshualitt33a5fce2015-11-18 13:28:51 -0800321 }
322
msarett10e3d9b2016-08-18 15:46:03 -0700323 fPatches.move_back_n(that->fPatches.count(), that->fPatches.begin());
Brian Osman0b537032018-12-26 12:16:44 -0500324 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000325 return CombineResult::kMerged;
joshualitt33a5fce2015-11-18 13:28:51 -0800326 }
327
John Stilesaf366522020-08-13 09:57:34 -0400328#if GR_TEST_UTILS
329 SkString onDumpInfo() const override {
330 SkString str;
331
332 for (int i = 0; i < fPatches.count(); ++i) {
333 str.appendf("%d: Color: 0x%08x Dst [L: %.2f, T: %.2f, R: %.2f, B: %.2f]\n", i,
334 fPatches[i].fColor.toBytes_RGBA(), fPatches[i].fDst.fLeft,
335 fPatches[i].fDst.fTop, fPatches[i].fDst.fRight, fPatches[i].fDst.fBottom);
336 }
337
338 str += fHelper.dumpInfo();
339 return str;
340 }
341#endif
342
bsalomona71b8982016-06-30 12:13:52 -0700343 struct Patch {
344 SkMatrix fViewMatrix;
msarett10e3d9b2016-08-18 15:46:03 -0700345 std::unique_ptr<SkLatticeIter> fIter;
bsalomona71b8982016-06-30 12:13:52 -0700346 SkRect fDst;
Brian Osmancf860852018-10-31 14:04:39 -0400347 SkPMColor4f fColor;
bsalomona71b8982016-06-30 12:13:52 -0700348 };
349
Brian Salomon815486c2017-07-11 08:52:13 -0400350 Helper fHelper;
351 SkSTArray<1, Patch, true> fPatches;
Greg Danieled96bca2019-12-05 15:05:54 -0500352 GrSurfaceProxyView fView;
Greg Daniel82c6b102020-01-21 10:33:22 -0500353 SkAlphaType fAlphaType;
Brian Salomon2a943df2018-05-04 13:43:19 -0400354 sk_sp<GrColorSpaceXform> fColorSpaceXform;
355 GrSamplerState::Filter fFilter;
Brian Osman0b537032018-12-26 12:16:44 -0500356 bool fWideColor;
joshualitt33a5fce2015-11-18 13:28:51 -0800357
Chris Daltoneb694b72020-03-16 09:25:50 -0600358 GrSimpleMesh* fMesh = nullptr;
Robert Phillipse37f1c42020-03-09 16:10:18 -0400359 GrProgramInfo* fProgramInfo = nullptr;
360
Brian Salomon815486c2017-07-11 08:52:13 -0400361 typedef GrMeshDrawOp INHERITED;
joshualitt33a5fce2015-11-18 13:28:51 -0800362};
363
Brian Salomon815486c2017-07-11 08:52:13 -0400364} // anonymous namespace
365
Brian Salomonfc527d22016-12-14 21:07:01 -0500366namespace GrLatticeOp {
Robert Phillipsb97da532019-02-12 15:24:12 -0500367std::unique_ptr<GrDrawOp> MakeNonAA(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400368 GrPaint&& paint,
369 const SkMatrix& viewMatrix,
Greg Danieled96bca2019-12-05 15:05:54 -0500370 GrSurfaceProxyView view,
Greg Daniel82c6b102020-01-21 10:33:22 -0500371 SkAlphaType alphaType,
Brian Salomon2a943df2018-05-04 13:43:19 -0400372 sk_sp<GrColorSpaceXform> colorSpaceXform,
373 GrSamplerState::Filter filter,
Robert Phillips7c525e62018-06-12 10:11:12 -0400374 std::unique_ptr<SkLatticeIter> iter,
375 const SkRect& dst) {
Greg Daniel82c6b102020-01-21 10:33:22 -0500376 return NonAALatticeOp::Make(context, std::move(paint), viewMatrix, std::move(view), alphaType,
377 std::move(colorSpaceXform), filter, std::move(iter), dst);
joshualitt33a5fce2015-11-18 13:28:51 -0800378}
John Stilesa6841be2020-08-06 14:11:56 -0400379} // namespace GrLatticeOp
Brian Salomon815486c2017-07-11 08:52:13 -0400380
381#if GR_TEST_UTILS
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500382#include "src/gpu/GrProxyProvider.h"
383#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon815486c2017-07-11 08:52:13 -0400384
385/** Randomly divides subset into count divs. */
386static void init_random_divs(int divs[], int count, int subsetStart, int subsetStop,
387 SkRandom* random) {
388 // Rules for lattice divs: Must be strictly increasing and in the range
389 // [subsetStart, subsetStop).
390 // Not terribly efficient alg for generating random divs:
391 // 1) Start with minimum legal pixels between each div.
392 // 2) Randomly assign the remaining pixels of the subset to divs.
393 // 3) Convert from pixel counts to div offsets.
394
395 // 1) Initially each divs[i] represents the number of pixels between
396 // div i-1 and i. The initial div is allowed to be at subsetStart. There
397 // must be one pixel spacing between subsequent divs.
398 divs[0] = 0;
399 for (int i = 1; i < count; ++i) {
400 divs[i] = 1;
401 }
402 // 2) Assign the remaining subset pixels to fall
403 int subsetLength = subsetStop - subsetStart;
404 for (int i = 0; i < subsetLength - count; ++i) {
405 // +1 because count divs means count+1 intervals.
406 int entry = random->nextULessThan(count + 1);
407 // We don't have an entry to to store the count after the last div
408 if (entry < count) {
409 divs[entry]++;
410 }
411 }
412 // 3) Now convert the counts between divs to pixel indices, incorporating the subset's offset.
413 int offset = subsetStart;
414 for (int i = 0; i < count; ++i) {
415 divs[i] += offset;
416 offset = divs[i];
417 }
418}
419
420GR_DRAW_OP_TEST_DEFINE(NonAALatticeOp) {
Brian Salomon815486c2017-07-11 08:52:13 -0400421 SkCanvas::Lattice lattice;
Brian Salomon18df7632017-07-11 14:32:18 -0400422 // We loop because our random lattice code can produce an invalid lattice in the case where
423 // there is a single div separator in both x and y and both are aligned with the left and top
424 // edge of the image subset, respectively.
425 std::unique_ptr<int[]> xdivs;
426 std::unique_ptr<int[]> ydivs;
Stan Ilievca8c0952017-12-11 13:01:58 -0500427 std::unique_ptr<SkCanvas::Lattice::RectType[]> flags;
428 std::unique_ptr<SkColor[]> colors;
Brian Salomon18df7632017-07-11 14:32:18 -0400429 SkIRect subset;
Brian Salomona56a7462020-02-07 14:17:25 -0500430 SkISize dims;
431 dims.fWidth = random->nextRangeU(1, 1000);
432 dims.fHeight = random->nextRangeU(1, 1000);
Robert Phillips0a15cc62019-07-30 12:49:10 -0400433 GrSurfaceOrigin origin = random->nextBool() ? kTopLeft_GrSurfaceOrigin
434 : kBottomLeft_GrSurfaceOrigin;
Greg Daniel4065d452018-11-16 15:43:41 -0500435 const GrBackendFormat format =
Robert Phillips0a15cc62019-07-30 12:49:10 -0400436 context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
437 GrRenderable::kNo);
Greg Daniel47c20e82020-01-21 14:29:57 -0500438
Brian Salomonbeb7f522019-08-30 16:19:42 -0400439 auto proxy = context->priv().proxyProvider()->createProxy(format,
Brian Salomona56a7462020-02-07 14:17:25 -0500440 dims,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400441 GrRenderable::kNo,
442 1,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400443 GrMipmapped::kNo,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400444 SkBackingFit::kExact,
445 SkBudgeted::kYes,
446 GrProtected::kNo);
Brian Salomon2a943df2018-05-04 13:43:19 -0400447
Brian Salomon18df7632017-07-11 14:32:18 -0400448 do {
Brian Salomon18df7632017-07-11 14:32:18 -0400449 if (random->nextBool()) {
Brian Salomona56a7462020-02-07 14:17:25 -0500450 subset.fLeft = random->nextULessThan(dims.fWidth);
451 subset.fRight = random->nextRangeU(subset.fLeft + 1, dims.fWidth);
452 subset.fTop = random->nextULessThan(dims.fHeight);
453 subset.fBottom = random->nextRangeU(subset.fTop + 1, dims.fHeight);
Brian Salomon18df7632017-07-11 14:32:18 -0400454 } else {
Brian Salomona56a7462020-02-07 14:17:25 -0500455 subset.setXYWH(0, 0, dims.fWidth, dims.fHeight);
Brian Salomon815486c2017-07-11 08:52:13 -0400456 }
Brian Salomon2a943df2018-05-04 13:43:19 -0400457 // SkCanvas::Lattice allows bounds to be null. However, SkCanvas creates a temp Lattice with
458 // a non-null bounds before creating a SkLatticeIter since SkLatticeIter requires a bounds.
Brian Salomon18df7632017-07-11 14:32:18 -0400459 lattice.fBounds = &subset;
460 lattice.fXCount = random->nextRangeU(1, subset.width());
461 lattice.fYCount = random->nextRangeU(1, subset.height());
462 xdivs.reset(new int[lattice.fXCount]);
463 ydivs.reset(new int[lattice.fYCount]);
464 init_random_divs(xdivs.get(), lattice.fXCount, subset.fLeft, subset.fRight, random);
465 init_random_divs(ydivs.get(), lattice.fYCount, subset.fTop, subset.fBottom, random);
466 lattice.fXDivs = xdivs.get();
467 lattice.fYDivs = ydivs.get();
468 bool hasFlags = random->nextBool();
469 if (hasFlags) {
470 int n = (lattice.fXCount + 1) * (lattice.fYCount + 1);
Stan Ilievca8c0952017-12-11 13:01:58 -0500471 flags.reset(new SkCanvas::Lattice::RectType[n]);
472 colors.reset(new SkColor[n]);
Brian Salomon18df7632017-07-11 14:32:18 -0400473 for (int i = 0; i < n; ++i) {
Stan Ilievca8c0952017-12-11 13:01:58 -0500474 flags[i] = random->nextBool() ? SkCanvas::Lattice::kTransparent
475 : SkCanvas::Lattice::kDefault;
Brian Salomon18df7632017-07-11 14:32:18 -0400476 }
Stan Ilievca8c0952017-12-11 13:01:58 -0500477 lattice.fRectTypes = flags.get();
478 lattice.fColors = colors.get();
Brian Salomon18df7632017-07-11 14:32:18 -0400479 } else {
Stan Ilievca8c0952017-12-11 13:01:58 -0500480 lattice.fRectTypes = nullptr;
481 lattice.fColors = nullptr;
Brian Salomon18df7632017-07-11 14:32:18 -0400482 }
Brian Salomona56a7462020-02-07 14:17:25 -0500483 } while (!SkLatticeIter::Valid(dims.fWidth, dims.fHeight, lattice));
Brian Salomon815486c2017-07-11 08:52:13 -0400484 SkRect dst;
485 dst.fLeft = random->nextRangeScalar(-2000.5f, 1000.f);
486 dst.fTop = random->nextRangeScalar(-2000.5f, 1000.f);
487 dst.fRight = dst.fLeft + random->nextRangeScalar(0.5f, 1000.f);
488 dst.fBottom = dst.fTop + random->nextRangeScalar(0.5f, 1000.f);
489 std::unique_ptr<SkLatticeIter> iter(new SkLatticeIter(lattice, dst));
490 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
Brian Salomon2a943df2018-05-04 13:43:19 -0400491 auto csxf = GrTest::TestColorXform(random);
492 GrSamplerState::Filter filter =
Brian Salomona3b02f52020-07-15 16:02:01 -0400493 random->nextBool() ? GrSamplerState::Filter::kNearest : GrSamplerState::Filter::kLinear;
Greg Danieled96bca2019-12-05 15:05:54 -0500494
495 GrSurfaceProxyView view(
496 std::move(proxy), origin,
Greg Daniel14b57212019-12-17 16:18:06 -0500497 context->priv().caps()->getReadSwizzle(format, GrColorType::kRGBA_8888));
Greg Danieled96bca2019-12-05 15:05:54 -0500498
499 return NonAALatticeOp::Make(context, std::move(paint), viewMatrix, std::move(view),
Greg Daniel82c6b102020-01-21 10:33:22 -0500500 kPremul_SkAlphaType, std::move(csxf), filter, std::move(iter), dst);
Brian Salomon815486c2017-07-11 08:52:13 -0400501}
502
503#endif