Flutter Engine
The Flutter Engine
GrBitmapTextGeoProc.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2013 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/AtlasTypes.h"
17#include "src/gpu/KeyBuilder.h"
31
32#include <algorithm>
33
35
37
39public:
41 const GrShaderCaps& shaderCaps,
42 const GrGeometryProcessor& geomProc) override {
43 const GrBitmapTextGeoProc& btgp = geomProc.cast<GrBitmapTextGeoProc>();
44 if (btgp.fColor != fColor && !btgp.hasVertexColor()) {
45 pdman.set4fv(fColorUniform, 1, btgp.fColor.vec());
46 fColor = btgp.fColor;
47 }
48
49 const SkISize& atlasDimensions = btgp.fAtlasDimensions;
50 SkASSERT(SkIsPow2(atlasDimensions.fWidth) && SkIsPow2(atlasDimensions.fHeight));
51
52 if (fAtlasDimensions != atlasDimensions) {
53 pdman.set2f(fAtlasDimensionsInvUniform,
54 1.0f / atlasDimensions.fWidth,
55 1.0f / atlasDimensions.fHeight);
56 fAtlasDimensions = atlasDimensions;
57 }
58
59 SetTransform(pdman, shaderCaps, fLocalMatrixUniform, btgp.fLocalMatrix, &fLocalMatrix);
60 fColorSpaceXformHelper.setData(pdman, btgp.fColorSpaceXform.get());
61 }
62
63private:
64 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
65 const GrBitmapTextGeoProc& btgp = args.fGeomProc.cast<GrBitmapTextGeoProc>();
66
67 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
68 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
69 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
70
71 fColorSpaceXformHelper.emitCode(uniformHandler,
72 btgp.fColorSpaceXform.get());
73
74 // emit attributes
75 varyingHandler->emitAttributes(btgp);
76
77 const char* atlasDimensionsInvName;
78 fAtlasDimensionsInvUniform = uniformHandler->addUniform(nullptr, kVertex_GrShaderFlag,
79 SkSLType::kFloat2, "AtlasSizeInv", &atlasDimensionsInvName);
80
81 GrGLSLVarying uv, texIdx;
83 btgp.numTextureSamplers(),
84 btgp.fInTextureCoords.name(),
85 atlasDimensionsInvName,
86 &uv,
87 &texIdx,
88 nullptr);
89
90 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
91 // Setup pass through color
92 fragBuilder->codeAppendf("half4 %s;", args.fOutputColor);
93 if (btgp.hasVertexColor()) {
94 varyingHandler->addPassThroughAttribute(btgp.fInColor.asShaderVar(), args.fOutputColor);
95 } else {
96 this->setupUniformColor(fragBuilder, uniformHandler, args.fOutputColor,
97 &fColorUniform);
98 }
99
100 // Setup position
101 gpArgs->fPositionVar = btgp.fInPosition.asShaderVar();
102 WriteLocalCoord(vertBuilder,
103 uniformHandler,
104 *args.fShaderCaps,
105 gpArgs,
106 btgp.fInPosition.asShaderVar(),
107 btgp.fLocalMatrix,
108 &fLocalMatrixUniform);
109
110 fragBuilder->codeAppend("half4 texColor;");
112 texIdx, uv.fsIn(), "texColor");
113 if (!fColorSpaceXformHelper.isNoop()) {
114 fragBuilder->codeAppend("texColor = ");
115 fragBuilder->appendColorGamutXform("texColor", &fColorSpaceXformHelper);
116 fragBuilder->codeAppend(";");
117 }
118
119 if (btgp.fMaskFormat == MaskFormat::kARGB) {
120 // modulate by color
121 fragBuilder->codeAppendf("%s = %s * texColor;", args.fOutputColor, args.fOutputColor);
122 fragBuilder->codeAppendf("const half4 %s = half4(1);", args.fOutputCoverage);
123 } else {
124 fragBuilder->codeAppendf("half4 %s = texColor;", args.fOutputCoverage);
125 }
126 }
127
128private:
130 SkISize fAtlasDimensions = {-1, -1};
131 SkMatrix fLocalMatrix = SkMatrix::InvalidMatrix();
132
133 UniformHandle fColorUniform;
134 UniformHandle fAtlasDimensionsInvUniform;
135 UniformHandle fLocalMatrixUniform;
136
137 GrGLSLColorSpaceXformHelper fColorSpaceXformHelper;
138};
139
140///////////////////////////////////////////////////////////////////////////////
141
142GrBitmapTextGeoProc::GrBitmapTextGeoProc(const GrShaderCaps& caps,
143 const SkPMColor4f& color,
144 bool wideColor,
145 sk_sp<GrColorSpaceXform> colorSpaceXform,
146 const GrSurfaceProxyView* views,
147 int numActiveViews,
150 const SkMatrix& localMatrix,
151 bool usesW)
152 : INHERITED(kGrBitmapTextGeoProc_ClassID)
153 , fColor(color)
154 , fColorSpaceXform(std::move(colorSpaceXform))
155 , fLocalMatrix(localMatrix)
156 , fUsesW(usesW)
157 , fMaskFormat(format) {
158 SkASSERT(numActiveViews <= kMaxTextures);
159
160 if (usesW) {
161 fInPosition = {"inPosition", kFloat3_GrVertexAttribType, SkSLType::kFloat3};
162 } else {
163 fInPosition = {"inPosition", kFloat2_GrVertexAttribType, SkSLType::kFloat2};
164 }
165
166 bool hasVertexColor = MaskFormat::kA8 == fMaskFormat || MaskFormat::kA565 == fMaskFormat;
167 if (hasVertexColor) {
168 fInColor = MakeColorAttribute("inColor", wideColor);
169 }
170
171 fInTextureCoords = {"inTextureCoords", kUShort2_GrVertexAttribType,
173 this->setVertexAttributesWithImplicitOffsets(&fInPosition, 3);
174
175 if (numActiveViews) {
176 fAtlasDimensions = views[0].proxy()->dimensions();
177 }
178 for (int i = 0; i < numActiveViews; ++i) {
179 const GrSurfaceProxy* proxy = views[i].proxy();
180 SkASSERT(proxy);
181 SkASSERT(proxy->dimensions() == fAtlasDimensions);
182 fTextureSamplers[i].reset(params, proxy->backendFormat(), views[i].swizzle());
183 }
184 this->setTextureSamplerCnt(numActiveViews);
185}
186
188 int numActiveViews,
190 SkASSERT(numActiveViews <= kMaxTextures);
191 // Just to make sure we don't try to add too many proxies
192 numActiveViews = std::min(numActiveViews, kMaxTextures);
193
194 if (!fTextureSamplers[0].isInitialized()) {
195 fAtlasDimensions = views[0].proxy()->dimensions();
196 }
197
198 for (int i = 0; i < numActiveViews; ++i) {
199 const GrSurfaceProxy* proxy = views[i].proxy();
200 SkASSERT(proxy);
201 SkASSERT(proxy->dimensions() == fAtlasDimensions);
202
203 if (!fTextureSamplers[i].isInitialized()) {
204 fTextureSamplers[i].reset(params, proxy->backendFormat(), views[i].swizzle());
205 }
206 }
207 this->setTextureSamplerCnt(numActiveViews);
208}
209
211 b->addBool(fUsesW, "usesW");
212 static_assert(static_cast<int>(MaskFormat::kLast) < (1u << 2));
213 b->addBits(2, static_cast<int>(fMaskFormat), "maskFormat");
215 ProgramImpl::ComputeMatrixKey(caps, fLocalMatrix),
216 "localMatrixType");
217 b->add32(this->numTextureSamplers(), "numTextures");
218 b->add32(GrColorSpaceXform::XformKey(fColorSpaceXform.get()), "colorSpaceXform");
219}
220
221std::unique_ptr<GrGeometryProcessor::ProgramImpl> GrBitmapTextGeoProc::makeProgramImpl(
222 const GrShaderCaps& caps) const {
223 return std::make_unique<Impl>();
224}
225
226///////////////////////////////////////////////////////////////////////////////
227
229
230#if defined(GR_TEST_UTILS)
231
232GrGeometryProcessor* GrBitmapTextGeoProc::TestCreate(GrProcessorTestData* d) {
233 auto [view, ct, at] = d->randomView();
234
235 GrSamplerState::WrapMode wrapModes[2];
236 GrTest::TestWrapModes(d->fRandom, wrapModes);
237 GrSamplerState samplerState(wrapModes, d->fRandom->nextBool()
240
242 switch (ct) {
244 format = MaskFormat::kA8;
245 break;
247 format = MaskFormat::kA565;
248 break;
250 default: // It doesn't really matter that color type and mask format agree.
251 format = MaskFormat::kARGB;
252 break;
253 }
254
255 GrColor color = GrTest::RandomColor(d->fRandom);
256 bool wideColor = d->fRandom->nextBool();
257 SkMatrix localMatrix = GrTest::TestMatrix(d->fRandom);
258 bool usesW = d->fRandom->nextBool();
259 return GrBitmapTextGeoProc::Make(d->allocator(), *d->caps()->shaderCaps(),
261 wideColor, /*colorSpaceXform=*/nullptr,
262 &view, 1, samplerState, format,
263 localMatrix, usesW);
264}
265#endif
static void append_index_uv_varyings(GrGeometryProcessor::ProgramImpl::EmitArgs &args, int numTextureSamplers, const char *inTexCoordsName, const char *atlasDimensionsInvName, GrGLSLVarying *uv, GrGLSLVarying *texIdx, GrGLSLVarying *st)
static void append_multitexture_lookup(GrGeometryProcessor::ProgramImpl::EmitArgs &args, int numTextureSamplers, const GrGLSLVarying &texIdx, const char *coordName, const char *colorName)
uint32_t GrColor
Definition: GrColor.h:25
#define GR_DEFINE_GEOMETRY_PROCESSOR_TEST(...)
@ kVertex_GrShaderFlag
Definition: GrTypesPriv.h:286
@ kFloat2_GrVertexAttribType
Definition: GrTypesPriv.h:314
@ kUShort2_GrVertexAttribType
Definition: GrTypesPriv.h:340
@ kFloat3_GrVertexAttribType
Definition: GrTypesPriv.h:315
#define SkASSERT(cond)
Definition: SkAssert.h:116
constexpr SkPMColor4f SK_PMColor4fILLEGAL
Definition: SkColorData.h:381
constexpr bool SkIsPow2(T value)
Definition: SkMath.h:51
#define INHERITED(method,...)
Definition: SkRecorder.cpp:128
void setData(const GrGLSLProgramDataManager &pdman, const GrShaderCaps &shaderCaps, const GrGeometryProcessor &geomProc) override
static GrGeometryProcessor * Make(SkArenaAlloc *arena, const GrShaderCaps &caps, const SkPMColor4f &color, bool wideColor, sk_sp< GrColorSpaceXform > colorSpaceXform, const GrSurfaceProxyView *views, int numActiveViews, GrSamplerState p, skgpu::MaskFormat format, const SkMatrix &localMatrix, bool usesW)
void addToKey(const GrShaderCaps &caps, skgpu::KeyBuilder *b) const override
static constexpr int kMaxTextures
std::unique_ptr< ProgramImpl > makeProgramImpl(const GrShaderCaps &caps) const override
void addNewViews(const GrSurfaceProxyView *, int numActiveViews, GrSamplerState)
static uint32_t XformKey(const GrColorSpaceXform *xform)
void setData(const GrGLSLProgramDataManager &pdman, const GrColorSpaceXform *colorSpaceXform)
void emitCode(GrGLSLUniformHandler *uniformHandler, const GrColorSpaceXform *colorSpaceXform, uint32_t visibility=kFragment_GrShaderFlag)
virtual void set4fv(UniformHandle, int arrayCount, const float v[]) const =0
virtual void set2f(UniformHandle, float, float) const =0
void codeAppend(const char *str)
void appendColorGamutXform(SkString *out, const char *srcColor, GrGLSLColorSpaceXformHelper *colorXformHelper)
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)
const char * fsIn() const
Definition: GrGLSLVarying.h:68
constexpr const char * name() const
void setupUniformColor(GrGLSLFPFragmentBuilder *fragBuilder, GrGLSLUniformHandler *uniformHandler, const char *outputName, UniformHandle *colorUniform)
GrGLSLProgramDataManager::UniformHandle UniformHandle
static void WriteLocalCoord(GrGLSLVertexBuilder *, GrGLSLUniformHandler *, const GrShaderCaps &, GrGPArgs *, GrShaderVar localVar, const SkMatrix &localMatrix, UniformHandle *localMatrixUniform)
static uint32_t ComputeMatrixKey(const GrShaderCaps &caps, const SkMatrix &mat)
static void SetTransform(const GrGLSLProgramDataManager &, const GrShaderCaps &, const UniformHandle &uniform, const SkMatrix &matrix, SkMatrix *state=nullptr)
void reset(GrSamplerState, const GrBackendFormat &, const skgpu::Swizzle &)
void setTextureSamplerCnt(int cnt)
static Attribute MakeColorAttribute(const char *name, bool wideColor)
void setVertexAttributesWithImplicitOffsets(const Attribute *attrs, int attrCount)
const T & cast() const
Definition: GrProcessor.h:127
skgpu::Swizzle swizzle() const
GrSurfaceProxy * proxy() const
const GrBackendFormat & backendFormat() const
SkISize dimensions() const
static const SkMatrix & InvalidMatrix()
Definition: SkMatrix.cpp:1550
T * get() const
Definition: SkRefCnt.h:303
void add32(uint32_t v, std::string_view label="unknown")
Definition: KeyBuilder.h:58
DlColor color
const EmbeddedViewParams * params
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE auto & d
Definition: main.cc:19
static bool b
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
uint32_t uint32_t * format
static float min(float r, float g, float b)
Definition: hsl.cpp:48
MaskFormat
Definition: AtlasTypes.h:98
Definition: ref_ptr.h:256
Definition: SkSize.h:16
int32_t fHeight
Definition: SkSize.h:18
int32_t fWidth
Definition: SkSize.h:17
const float * vec() const
Definition: SkColor.h:308
static SkRGBA4f FromBytes_RGBA(uint32_t color)
bool fIntegerSupport
Definition: SkSLUtil.h:89