Flutter Engine
The Flutter Engine
AtlasInstancedHelper.h
Go to the documentation of this file.
1/*
2 * Copyright 2021 Google LLC.
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#ifndef AtlasInstancedHelper_DEFINED
9#define AtlasInstancedHelper_DEFINED
10
11#include "src/core/SkIPoint16.h"
15
16namespace skgpu::ganesh {
17
18// This class encapsulates all the necessary steps for an instanced GrGeometryProcessor to clip
19// against a path mask from an atlas.
21public:
22 enum class ShaderFlags {
23 kNone = 0,
24 kInvertCoverage = 1 << 0,
25 kCheckBounds = 1 << 1
26 };
27
29
30 constexpr static int kNumShaderFlags = 2;
31
33 : fAtlasProxy(atlasView.detachProxy())
34 , fAtlasSwizzle(atlasView.swizzle())
35 , fShaderFlags(shaderFlags) {
36 // Bottom left origin is not supported.
38 }
39
40 GrSurfaceProxy* proxy() const { return fAtlasProxy.get(); }
41 const skgpu::Swizzle& atlasSwizzle() const { return fAtlasSwizzle; }
42
43 // Returns whether the two helpers can be batched together in a single draw.
44 bool isCompatible(const AtlasInstancedHelper& helper) {
45 // TODO: We may want to consider two helpers compatible if they only differ in the
46 // kCheckBounds flag -- we can always promote one to checking its bounds.
47 SkASSERT(fAtlasProxy != helper.fAtlasProxy || fAtlasSwizzle == helper.fAtlasSwizzle);
48 return fAtlasProxy == helper.fAtlasProxy && fShaderFlags == helper.fShaderFlags;
49 }
50
51 // Adds bits to the shader key that uniquely identify this specific helper's shader code.
52 void getKeyBits(KeyBuilder* b) const;
53
54 // Appends the instanced input attribs to the back of the array that we will need in order to
55 // locate our path in the atlas.
58
59 struct Instance {
60 Instance(SkIPoint16 locationInAtlas, const SkIRect& pathDevIBounds, bool transposedInAtlas)
61 : fLocationInAtlas(locationInAtlas)
62 , fPathDevIBounds(pathDevIBounds)
63 , fTransposedInAtlas(transposedInAtlas) {
66 }
70 };
71
72 // Writes out the given instance data, formatted for the specific attribs that we added during
73 // appendInstanceAttribs().
74 void writeInstanceData(VertexWriter* instanceWriter, const Instance*) const;
75
76 // Injects vertex code, fragment code, varyings, and uniforms to ultimately multiply
77 // "args.fOutputCoverage" in the fragment shader by the atlas coverage.
78 //
79 // The caller is responsible to store "atlasAdjustUniformHandle" and pass it to
80 // setUniformData().
82 const GrShaderVar& devCoord,
83 GrGLSLUniformHandler::UniformHandle* atlasAdjustUniformHandle) const;
84
85 // The atlas clip requires one uniform value -- "atlasAdjustUniform". The caller should have
86 // stored this handle after its call to injectShaderCode(). This method sets its value prior to
87 // drawing.
89 const GrGLSLUniformHandler::UniformHandle& atlasAdjustUniformHandle) const;
90
91private:
92 const sk_sp<GrSurfaceProxy> fAtlasProxy;
93 const skgpu::Swizzle fAtlasSwizzle;
94 const ShaderFlags fShaderFlags;
95};
96
98
99} // namespace skgpu::ganesh
100
101#endif // AtlasInstancedHelper_DEFINED
#define GR_MAKE_BITFIELD_CLASS_OPS(X)
Definition: GrTypes.h:42
@ kTopLeft_GrSurfaceOrigin
Definition: GrTypes.h:148
#define SkASSERT(cond)
Definition: SkAssert.h:116
GrGLSLProgramDataManager::UniformHandle UniformHandle
GrSurfaceOrigin origin() const
T * get() const
Definition: SkRefCnt.h:303
const skgpu::Swizzle & atlasSwizzle() const
void writeInstanceData(VertexWriter *instanceWriter, const Instance *) const
void appendInstanceAttribs(skia_private::TArray< GrGeometryProcessor::Attribute > *instanceAttribs) const
AtlasInstancedHelper(GrSurfaceProxyView atlasView, ShaderFlags shaderFlags)
void injectShaderCode(const GrGeometryProcessor::ProgramImpl::EmitArgs &, const GrShaderVar &devCoord, GrGLSLUniformHandler::UniformHandle *atlasAdjustUniformHandle) const
void setUniformData(const GrGLSLProgramDataManager &, const GrGLSLUniformHandler::UniformHandle &atlasAdjustUniformHandle) const
bool isCompatible(const AtlasInstancedHelper &helper)
static bool b
int16_t y() const
Definition: SkIPoint16.h:43
int16_t x() const
Definition: SkIPoint16.h:37
Definition: SkRect.h:32
Instance(SkIPoint16 locationInAtlas, const SkIRect &pathDevIBounds, bool transposedInAtlas)