1/*
2 * Copyright 2020 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
8#include "src/gpu/tessellate/GrDrawAtlasPathOp.h"
9
10#include "src/gpu/GrOpFlushState.h"
11#include "src/gpu/GrOpsRenderPass.h"
12#include "src/gpu/GrProgramInfo.h"
13#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
14#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
15#include "src/gpu/glsl/GrGLSLVarying.h"
16#include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
17
18namespace {
19
20constexpr static GrGeometryProcessor::Attribute kInstanceAttribs[] = {
21 {"dev_xywh", kInt4_GrVertexAttribType, kInt4_GrSLType},
22 {"atlas_xy", kInt2_GrVertexAttribType, kInt2_GrSLType},
23 {"color", kFloat4_GrVertexAttribType, kHalf4_GrSLType},
24 {"viewmatrix_scaleskew", kFloat4_GrVertexAttribType, kFloat4_GrSLType},
25 {"viewmatrix_trans", kFloat2_GrVertexAttribType, kFloat2_GrSLType}};
26
27class DrawAtlasPathShader : public GrGeometryProcessor {
28public:
29 DrawAtlasPathShader(const GrTextureProxy* atlasProxy, GrSwizzle swizzle, bool usesLocalCoords)
30 : GrGeometryProcessor(kDrawAtlasPathShader_ClassID)
31 , fAtlasAccess(GrSamplerState::Filter::kNearest, atlasProxy->backendFormat(), swizzle)
32 , fAtlasDimensions(atlasProxy->backingStoreDimensions())
33 , fUsesLocalCoords(usesLocalCoords) {
34 int numInstanceAttribs = SK_ARRAY_COUNT(kInstanceAttribs);
35 if (!fUsesLocalCoords) {
36 numInstanceAttribs -= 2;
37 }
38 this->setInstanceAttributes(kInstanceAttribs, numInstanceAttribs);
39 this->setTextureSamplerCnt(1);
40 }
41
42private:
43 const char* name() const override { return "DrawAtlasPathShader"; }
44 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
45 b->add32(fUsesLocalCoords);
46 }
47 const TextureSampler& onTextureSampler(int) const override { return fAtlasAccess; }
48 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override;
49
50 const TextureSampler fAtlasAccess;
51 const SkISize fAtlasDimensions;
52 const bool fUsesLocalCoords;
53
54 class Impl;
55};
56
57class DrawAtlasPathShader::Impl : public GrGLSLGeometryProcessor {
58 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
59 const auto& shader = args.fGP.cast<DrawAtlasPathShader>();
60 args.fVaryingHandler->emitAttributes(shader);
61
62 GrGLSLVarying atlasCoord(kFloat2_GrSLType);
63 args.fVaryingHandler->addVarying("atlascoord", &atlasCoord);
64
65 GrGLSLVarying color(kHalf4_GrSLType);
66 args.fVaryingHandler->addPassThroughAttribute(
67 kInstanceAttribs[2], args.fOutputColor,
68 GrGLSLVaryingHandler::Interpolation::kCanBeFlat);
69
70 const char* atlasAdjust;
71 fAtlasAdjustUniform = args.fUniformHandler->addUniform(
72 nullptr, kVertex_GrShaderFlag, kFloat2_GrSLType, "atlas_adjust", &atlasAdjust);
73
74 args.fVertBuilder->codeAppendf(R"(
75 float2 T = float2(sk_VertexID & 1, sk_VertexID >> 1);
76 float2 devtopleft = float2(dev_xywh.xy);
77 float2 devcoord = abs(float2(dev_xywh.zw)) * T + devtopleft;
78 float2 atlascoord = devcoord - devtopleft;
79 if (dev_xywh.w < 0) { // Negative height indicates that the path is transposed.
80 atlascoord = atlascoord.yx;
81 }
82 atlascoord += atlas_xy;
83 %s = atlascoord * %s;)",
84 atlasCoord.vsOut(), atlasAdjust);
85
86 gpArgs->fPositionVar.set(kFloat2_GrSLType, "devcoord");
87
88 if (shader.fUsesLocalCoords) {
89 args.fVertBuilder->codeAppendf(R"(
90 float2x2 M = float2x2(viewmatrix_scaleskew);
91 float2 localcoord = inverse(M) * (devcoord - viewmatrix_trans);)");
92 gpArgs->fLocalCoordVar.set(kFloat2_GrSLType, "localcoord");
93 }
94
95 args.fFragBuilder->codeAppendf("%s = ", args.fOutputCoverage);
96 args.fFragBuilder->appendTextureLookup(args.fTexSamplers[0], atlasCoord.fsIn());
97 args.fFragBuilder->codeAppendf(".aaaa;");
98 }
99
100 void setData(const GrGLSLProgramDataManager& pdman,
101 const GrPrimitiveProcessor& primProc) override {
102 const SkISize& dimensions = primProc.cast<DrawAtlasPathShader>().fAtlasDimensions;
103 pdman.set2f(fAtlasAdjustUniform, 1.f / dimensions.width(), 1.f / dimensions.height());
104 }
105
106 GrGLSLUniformHandler::UniformHandle fAtlasAdjustUniform;
107};
108
109GrGLSLPrimitiveProcessor* DrawAtlasPathShader::createGLSLInstance(const GrShaderCaps&) const {
110 return new Impl();
111}
112
113} // namespace
114
115GrProcessorSet::Analysis GrDrawAtlasPathOp::finalize(const GrCaps& caps, const GrAppliedClip* clip,
116 bool hasMixedSampledCoverage,
117 GrClampType clampType) {
118 const GrProcessorSet::Analysis& analysis = fProcessors.finalize(
119 fInstanceList.fInstance.fColor, GrProcessorAnalysisCoverage::kSingleChannel, clip,
120 &GrUserStencilSettings::kUnused, hasMixedSampledCoverage, caps, clampType,
121 &fInstanceList.fInstance.fColor);
122 fUsesLocalCoords = analysis.usesLocalCoords();
123 return analysis;
124}
125
126GrOp::CombineResult GrDrawAtlasPathOp::onCombineIfPossible(
127 GrOp* op, GrRecordingContext::Arenas* arenas, const GrCaps&) {
128 auto* that = op->cast<GrDrawAtlasPathOp>();
129 SkASSERT(fAtlasProxy == that->fAtlasProxy);
130 SkASSERT(fEnableHWAA == that->fEnableHWAA);
131
132 if (fProcessors != that->fProcessors) {
133 return CombineResult::kCannotCombine;
134 }
135
136 SkASSERT(fUsesLocalCoords == that->fUsesLocalCoords);
137 auto* copy = arenas->recordTimeAllocator()->make<InstanceList>(that->fInstanceList);
138 *fInstanceTail = copy;
139 fInstanceTail = (!copy->fNext) ? &copy->fNext : that->fInstanceTail;
140 fInstanceCount += that->fInstanceCount;
141 return CombineResult::kMerged;
142}
143
144void GrDrawAtlasPathOp::onPrePrepare(GrRecordingContext*,
145 const GrSurfaceProxyView* writeView,
146 GrAppliedClip*,
147 const GrXferProcessor::DstProxyView&) {
148}
149
150void GrDrawAtlasPathOp::onPrepare(GrOpFlushState* state) {
151 size_t instanceStride = Instance::Stride(fUsesLocalCoords);
152 if (char* instanceData = (char*)state->makeVertexSpace(
153 instanceStride, fInstanceCount, &fInstanceBuffer, &fBaseInstance)) {
154 SkDEBUGCODE(char* end = instanceData + fInstanceCount * instanceStride);
155 for (const InstanceList* list = &fInstanceList; list; list = list->fNext) {
156 memcpy(instanceData, &list->fInstance, instanceStride);
157 instanceData += instanceStride;
158 }
159 SkASSERT(instanceData == end);
160 }
161}
162
163void GrDrawAtlasPathOp::onExecute(GrOpFlushState* state, const SkRect& chainBounds) {
164 SkASSERT(fAtlasProxy->isInstantiated());
165
166 GrPipeline::InitArgs initArgs;
167 if (fEnableHWAA) {
168 initArgs.fInputFlags |= GrPipeline::InputFlags::kHWAntialias;
169 }
170 initArgs.fCaps = &state->caps();
171 initArgs.fDstProxyView = state->drawOpArgs().dstProxyView();
172 initArgs.fWriteSwizzle = state->drawOpArgs().writeSwizzle();
173 GrPipeline pipeline(initArgs, std::move(fProcessors), state->detachAppliedClip());
174
175 GrSwizzle swizzle = state->caps().getReadSwizzle(fAtlasProxy->backendFormat(),
176 GrColorType::kAlpha_8);
177
178 DrawAtlasPathShader shader(fAtlasProxy.get(), swizzle, fUsesLocalCoords);
179 SkASSERT(shader.instanceStride() == Instance::Stride(fUsesLocalCoords));
180
181 GrProgramInfo programInfo(state->proxy()->numSamples(), state->proxy()->numStencilSamples(),
182 state->proxy()->backendFormat(), state->writeView()->origin(),
183 &pipeline, &shader, GrPrimitiveType::kTriangleStrip);
184
185 state->bindPipelineAndScissorClip(programInfo, this->bounds());
186 state->bindTextures(shader, *fAtlasProxy, pipeline);
187 state->bindBuffers(nullptr, std::move(fInstanceBuffer), nullptr);
188 state->drawInstanced(fInstanceCount, fBaseInstance, 4, 0);
189}
190