Flutter Engine
The Flutter Engine
GrDefaultGeoProcFactory.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2014 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
9
14#include "src/base/SkRandom.h"
16#include "src/gpu/KeyBuilder.h"
27
28#include <memory>
29
30struct GrShaderCaps;
31
32/*
33 * The default Geometry Processor simply takes position and multiplies it by the uniform view
34 * matrix. It also leaves coverage untouched. Behind the scenes, we may add per vertex color or
35 * local coords.
36 */
37
38enum GPFlag {
45};
46
48public:
50 uint32_t gpTypeFlags,
51 const SkPMColor4f& color,
52 const SkMatrix& viewMatrix,
53 const SkMatrix& localMatrix,
54 bool localCoordsWillBeRead,
55 uint8_t coverage) {
56 return arena->make([&](void* ptr) {
57 return new (ptr) DefaultGeoProc(gpTypeFlags, color, viewMatrix, localMatrix, coverage,
58 localCoordsWillBeRead);
59 });
60 }
61
62 const char* name() const override { return "DefaultGeometryProcessor"; }
63
64 void addToKey(const GrShaderCaps& caps, skgpu::KeyBuilder* b) const override {
65 uint32_t key = fFlags;
66 key |= fCoverage == 0xff ? 0x80 : 0;
67 key |= fLocalCoordsWillBeRead ? 0x100 : 0;
68
69 bool usesLocalMatrix = fLocalCoordsWillBeRead && !fInLocalCoords.isInitialized();
71 key,
72 fViewMatrix,
73 usesLocalMatrix ? fLocalMatrix : SkMatrix::I());
74 b->add32(key);
75 }
76
77 std::unique_ptr<ProgramImpl> makeProgramImpl(const GrShaderCaps&) const override {
78 return std::make_unique<Impl>();
79 }
80
81private:
82 class Impl : public ProgramImpl {
83 public:
84 void setData(const GrGLSLProgramDataManager& pdman,
85 const GrShaderCaps& shaderCaps,
86 const GrGeometryProcessor& geomProc) override {
87 const DefaultGeoProc& dgp = geomProc.cast<DefaultGeoProc>();
88
89 SetTransform(pdman, shaderCaps, fViewMatrixUniform, dgp.fViewMatrix, &fViewMatrixPrev);
90 SetTransform(pdman,
91 shaderCaps,
92 fLocalMatrixUniform,
93 dgp.fLocalMatrix,
94 &fLocalMatrixPrev);
95
96 if (!dgp.hasVertexColor() && dgp.fColor != fColor) {
97 pdman.set4fv(fColorUniform, 1, dgp.fColor.vec());
98 fColor = dgp.fColor;
99 }
100
101 if (dgp.fCoverage != fCoverage && !dgp.hasVertexCoverage()) {
102 pdman.set1f(fCoverageUniform, GrNormalizeByteToFloat(dgp.fCoverage));
103 fCoverage = dgp.fCoverage;
104 }
105 }
106
107 private:
108 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
109 const DefaultGeoProc& gp = args.fGeomProc.cast<DefaultGeoProc>();
110 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
111 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
112 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
113 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
114
115 // emit attributes
116 varyingHandler->emitAttributes(gp);
117
118 bool tweakAlpha = SkToBool(gp.fFlags & kCoverageAttributeTweak_GPFlag);
119 bool coverageNeedsSaturate = SkToBool(gp.fFlags & kCoverageAttributeUnclamped_GPFlag);
120 SkASSERT(!tweakAlpha || gp.hasVertexCoverage());
121 SkASSERT(!tweakAlpha || !coverageNeedsSaturate);
122
123 // Setup pass through color
124 fragBuilder->codeAppendf("half4 %s;", args.fOutputColor);
125 if (gp.hasVertexColor() || tweakAlpha) {
127 varyingHandler->addVarying("color", &varying);
128
129 // Start with the attribute or with uniform color
130 if (gp.hasVertexColor()) {
131 vertBuilder->codeAppendf("half4 color = %s;", gp.fInColor.name());
132 } else {
133 const char* colorUniformName;
134 fColorUniform = uniformHandler->addUniform(nullptr,
137 "Color",
138 &colorUniformName);
139 vertBuilder->codeAppendf("half4 color = %s;", colorUniformName);
140 }
141
142 // Optionally fold coverage into alpha (color).
143 if (tweakAlpha) {
144 vertBuilder->codeAppendf("color = color * %s;", gp.fInCoverage.name());
145 }
146 vertBuilder->codeAppendf("%s = color;\n", varying.vsOut());
147 fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, varying.fsIn());
148 } else {
149 this->setupUniformColor(fragBuilder, uniformHandler, args.fOutputColor,
150 &fColorUniform);
151 }
152
153 // Setup position
154 WriteOutputPosition(vertBuilder,
155 uniformHandler,
156 *args.fShaderCaps,
157 gpArgs,
158 gp.fInPosition.name(),
159 gp.fViewMatrix,
160 &fViewMatrixUniform);
161
162 // emit transforms using either explicit local coords or positions
163 if (gp.fInLocalCoords.isInitialized()) {
164 SkASSERT(gp.fLocalMatrix.isIdentity());
165 gpArgs->fLocalCoordVar = gp.fInLocalCoords.asShaderVar();
166 } else if (gp.fLocalCoordsWillBeRead) {
167 WriteLocalCoord(vertBuilder,
168 uniformHandler,
169 *args.fShaderCaps,
170 gpArgs,
171 gp.fInPosition.asShaderVar(),
172 gp.fLocalMatrix,
173 &fLocalMatrixUniform);
174 }
175
176 // Setup coverage as pass through
177 if (gp.hasVertexCoverage() && !tweakAlpha) {
178 fragBuilder->codeAppendf("half alpha = 1.0;");
179 varyingHandler->addPassThroughAttribute(gp.fInCoverage.asShaderVar(), "alpha");
180 if (coverageNeedsSaturate) {
181 fragBuilder->codeAppendf("half4 %s = half4(saturate(alpha));",
182 args.fOutputCoverage);
183 } else {
184 fragBuilder->codeAppendf("half4 %s = half4(alpha);", args.fOutputCoverage);
185 }
186 } else if (gp.fCoverage == 0xff) {
187 fragBuilder->codeAppendf("const half4 %s = half4(1);", args.fOutputCoverage);
188 } else {
189 const char* fragCoverage;
190 fCoverageUniform = uniformHandler->addUniform(nullptr,
193 "Coverage",
194 &fragCoverage);
195 fragBuilder->codeAppendf("half4 %s = half4(%s);",
196 args.fOutputCoverage, fragCoverage);
197 }
198 }
199
200 SkMatrix fViewMatrixPrev = SkMatrix::InvalidMatrix();
201 SkMatrix fLocalMatrixPrev = SkMatrix::InvalidMatrix();
203 uint8_t fCoverage = 0xFF;
204
205 UniformHandle fViewMatrixUniform;
206 UniformHandle fLocalMatrixUniform;
207 UniformHandle fColorUniform;
208 UniformHandle fCoverageUniform;
209 };
210
211 bool hasVertexColor() const { return fInColor.isInitialized(); }
212 bool hasVertexCoverage() const { return fInCoverage.isInitialized(); }
213
214 DefaultGeoProc(uint32_t gpTypeFlags,
215 const SkPMColor4f& color,
216 const SkMatrix& viewMatrix,
217 const SkMatrix& localMatrix,
218 uint8_t coverage,
219 bool localCoordsWillBeRead)
220 : INHERITED(kDefaultGeoProc_ClassID)
221 , fColor(color)
222 , fViewMatrix(viewMatrix)
223 , fLocalMatrix(localMatrix)
224 , fCoverage(coverage)
225 , fFlags(gpTypeFlags)
226 , fLocalCoordsWillBeRead(localCoordsWillBeRead) {
227 fInPosition = {"inPosition", kFloat2_GrVertexAttribType, SkSLType::kFloat2};
228 if (fFlags & kColorAttribute_GPFlag) {
229 fInColor = MakeColorAttribute("inColor",
231 }
232 if (fFlags & kLocalCoordAttribute_GPFlag) {
233 fInLocalCoords = {"inLocalCoord", kFloat2_GrVertexAttribType,
235 }
236 if (fFlags & kCoverageAttribute_GPFlag) {
237 fInCoverage = {"inCoverage", kFloat_GrVertexAttribType, SkSLType::kHalf};
238 }
239 this->setVertexAttributesWithImplicitOffsets(&fInPosition, 4);
240 }
241
242 Attribute fInPosition;
243 Attribute fInColor;
244 Attribute fInLocalCoords;
245 Attribute fInCoverage;
246 SkPMColor4f fColor;
247 SkMatrix fViewMatrix;
248 SkMatrix fLocalMatrix;
249 uint8_t fCoverage;
250 uint32_t fFlags;
251 bool fLocalCoordsWillBeRead;
252
254
255 using INHERITED = GrGeometryProcessor;
256};
257
259
260#if defined(GR_TEST_UTILS)
261GrGeometryProcessor* DefaultGeoProc::TestCreate(GrProcessorTestData* d) {
262 uint32_t flags = 0;
263 if (d->fRandom->nextBool()) {
265 }
266 if (d->fRandom->nextBool()) {
268 }
269 if (d->fRandom->nextBool()) {
271 if (d->fRandom->nextBool()) {
272 flags |= (d->fRandom->nextBool()) ? kCoverageAttributeTweak_GPFlag
274 }
275 }
276 if (d->fRandom->nextBool()) {
278 }
279
280 GrColor color = GrTest::RandomColor(d->fRandom);
281 SkMatrix viewMtx = GrTest::TestMatrix(d->fRandom);
282 SkMatrix localMtx = GrTest::TestMatrix(d->fRandom);
283 bool readsLocalCoords = d->fRandom->nextBool();
284 uint8_t coverage = GrTest::RandomCoverage(d->fRandom);
285 return DefaultGeoProc::Make(d->allocator(),
286 flags,
288 viewMtx,
289 localMtx,
290 readsLocalCoords,
291 coverage);
292}
293#endif
294
296 const Color& color,
297 const Coverage& coverage,
298 const LocalCoords& localCoords,
299 const SkMatrix& viewMatrix) {
300 uint32_t flags = 0;
301 if (Color::kPremulGrColorAttribute_Type == color.fType) {
303 } else if (Color::kPremulWideColorAttribute_Type == color.fType) {
305 }
306 if (Coverage::kAttribute_Type == coverage.fType) {
308 } else if (Coverage::kAttributeTweakAlpha_Type == coverage.fType) {
310 } else if (Coverage::kAttributeUnclamped_Type == coverage.fType) {
312 }
313 flags |= localCoords.fType == LocalCoords::kHasExplicit_Type ? kLocalCoordAttribute_GPFlag : 0;
314
315 uint8_t inCoverage = coverage.fCoverage;
316 bool localCoordsWillBeRead = localCoords.fType != LocalCoords::kUnused_Type;
317
318 return DefaultGeoProc::Make(arena,
319 flags,
320 color.fColor,
321 viewMatrix,
322 localCoords.fMatrix ? *localCoords.fMatrix : SkMatrix::I(),
323 localCoordsWillBeRead,
324 inCoverage);
325}
326
328 const Color& color,
329 const Coverage& coverage,
330 const LocalCoords& localCoords,
331 const SkMatrix& viewMatrix) {
333 if (LocalCoords::kUnused_Type != localCoords.fType) {
334 SkASSERT(LocalCoords::kUsePosition_Type == localCoords.fType);
335 if (!viewMatrix.isIdentity() && !viewMatrix.invert(&invert)) {
336 return nullptr;
337 }
338
339 if (localCoords.hasLocalMatrix()) {
340 invert.postConcat(*localCoords.fMatrix);
341 }
342 }
343
344 LocalCoords inverted(LocalCoords::kUsePosition_Type, &invert);
345 return Make(arena, color, coverage, inverted, SkMatrix::I());
346}
float fCoverage
uint32_t GrColor
Definition: GrColor.h:25
static float GrNormalizeByteToFloat(uint8_t value)
Definition: GrColor.h:71
@ kCoverageAttributeUnclamped_GPFlag
@ kCoverageAttribute_GPFlag
@ kColorAttribute_GPFlag
@ kCoverageAttributeTweak_GPFlag
@ kLocalCoordAttribute_GPFlag
@ kColorAttributeIsWide_GPFlag
#define GR_DECLARE_GEOMETRY_PROCESSOR_TEST
#define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(...)
@ kVertex_GrShaderFlag
Definition: GrTypesPriv.h:286
@ kFragment_GrShaderFlag
Definition: GrTypesPriv.h:287
@ kFloat2_GrVertexAttribType
Definition: GrTypesPriv.h:314
@ kFloat_GrVertexAttribType
Definition: GrTypesPriv.h:313
#define SkASSERT(cond)
Definition: SkAssert.h:116
constexpr SkPMColor4f SK_PMColor4fILLEGAL
Definition: SkColorData.h:381
static constexpr bool SkToBool(const T &x)
Definition: SkTo.h:35
const char * name() const override
void addToKey(const GrShaderCaps &caps, skgpu::KeyBuilder *b) const override
std::unique_ptr< ProgramImpl > makeProgramImpl(const GrShaderCaps &) const override
static GrGeometryProcessor * Make(SkArenaAlloc *arena, uint32_t gpTypeFlags, const SkPMColor4f &color, const SkMatrix &viewMatrix, const SkMatrix &localMatrix, bool localCoordsWillBeRead, uint8_t coverage)
virtual void set4fv(UniformHandle, int arrayCount, const float v[]) const =0
virtual void set1f(UniformHandle, float v0) const =0
void codeAppendf(const char format[],...) SK_PRINTF_LIKE(2
UniformHandle addUniform(const GrProcessor *owner, uint32_t visibility, SkSLType type, const char *name, const char **outName=nullptr)
void emitAttributes(const GrGeometryProcessor &)
void addPassThroughAttribute(const GrShaderVar &vsVar, const char *output, Interpolation=Interpolation::kInterpolated)
void addVarying(const char *name, GrGLSLVarying *varying, Interpolation=Interpolation::kInterpolated)
constexpr bool isInitialized() const
constexpr const char * name() const
void setupUniformColor(GrGLSLFPFragmentBuilder *fragBuilder, GrGLSLUniformHandler *uniformHandler, const char *outputName, UniformHandle *colorUniform)
GrGLSLProgramDataManager::UniformHandle UniformHandle
static uint32_t AddMatrixKeys(const GrShaderCaps &shaderCaps, uint32_t flags, const SkMatrix &viewMatrix, const SkMatrix &localMatrix)
static void WriteOutputPosition(GrGLSLVertexBuilder *, GrGPArgs *, const char *posName)
static void WriteLocalCoord(GrGLSLVertexBuilder *, GrGLSLUniformHandler *, const GrShaderCaps &, GrGPArgs *, GrShaderVar localVar, const SkMatrix &localMatrix, UniformHandle *localMatrixUniform)
static Attribute MakeColorAttribute(const char *name, bool wideColor)
void setVertexAttributesWithImplicitOffsets(const Attribute *attrs, int attrCount)
const T & cast() const
Definition: GrProcessor.h:127
@ kDefaultGeoProc_ClassID
Definition: GrProcessor.h:40
auto make(Ctor &&ctor) -> decltype(ctor(nullptr))
Definition: SkArenaAlloc.h:120
bool invert(SkMatrix *inverse) const
Definition: SkMatrix.h:1206
static const SkMatrix & I()
Definition: SkMatrix.cpp:1544
bool isIdentity() const
Definition: SkMatrix.h:223
static const SkMatrix & InvalidMatrix()
Definition: SkMatrix.cpp:1550
DlColor color
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE auto & d
Definition: main.cc:19
static bool b
FlutterSemanticsFlag flags
gboolean invert
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
GrGeometryProcessor * Make(SkArenaAlloc *, const Color &, const Coverage &, const LocalCoords &, const SkMatrix &viewMatrix)
GrGeometryProcessor * MakeForDeviceSpace(SkArenaAlloc *, const Color &, const Coverage &, const LocalCoords &, const SkMatrix &viewMatrix)
SK_API sk_sp< SkDocument > Make(SkWStream *dst, const SkSerialProcs *=nullptr, std::function< void(const SkPicture *)> onEndPage=nullptr)
const float * vec() const
Definition: SkColor.h:308
static SkRGBA4f FromBytes_RGBA(uint32_t color)