Flutter Engine
The Flutter Engine
attributes.cpp
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#include "gm/gm.h"
10#include "include/core/SkRect.h"
13#include "src/gpu/KeyBuilder.h"
31
32#include <memory>
33#include <vector>
34
35class GrAppliedClip;
37
38namespace {
39
40enum class AttrMode {
41 kAuto,
42 kManual,
43 kWacky
44};
45
46class AttributeTestProcessor : public GrGeometryProcessor {
47public:
48 static GrGeometryProcessor* Make(SkArenaAlloc* arena, AttrMode mode) {
49 return arena->make([&](void* ptr) { return new (ptr) AttributeTestProcessor(mode); });
50 }
51
52 const char* name() const final { return "AttributeTestProcessor"; }
53
54 void addToKey(const GrShaderCaps&, skgpu::KeyBuilder* b) const final {
55 b->add32(static_cast<uint32_t>(fMode));
56 }
57
58 std::unique_ptr<ProgramImpl> makeProgramImpl(const GrShaderCaps&) const final;
59
60private:
61 AttributeTestProcessor(AttrMode mode)
62 : GrGeometryProcessor(kAttributeTestProcessor_ClassID), fMode(mode) {
63 switch (fMode) {
64 case AttrMode::kAuto:
65 fAttributes.emplace_back("pos", kFloat2_GrVertexAttribType, SkSLType::kFloat2);
66 fAttributes.emplace_back("color", kUByte4_norm_GrVertexAttribType,
68 this->setVertexAttributesWithImplicitOffsets(fAttributes.data(),
69 fAttributes.size());
70 break;
71 case AttrMode::kManual:
72 // Same result as kAuto but with explicitly specified offsets and stride.
73 fAttributes.emplace_back("pos", kFloat2_GrVertexAttribType, SkSLType::kFloat2, 0);
74 fAttributes.emplace_back("color", kUByte4_norm_GrVertexAttribType,
76 this->setVertexAttributes(fAttributes.data(), fAttributes.size(), 12);
77 break;
78 case AttrMode::kWacky:
79 // 0 thru 7 : float2 aliased to "pos0" and "pos1"
80 // 8 thru 11: pad
81 // 12 thru 15: unorm4 "color"
82 // 16 thru 19: pad
83 fAttributes.emplace_back("pos0", kFloat2_GrVertexAttribType, SkSLType::kFloat2, 0);
84 fAttributes.emplace_back("pos1", kFloat2_GrVertexAttribType, SkSLType::kFloat2, 0);
85 fAttributes.emplace_back("color", kUByte4_norm_GrVertexAttribType,
87 this->setVertexAttributes(fAttributes.data(), fAttributes.size(), 20);
88 break;
89 }
90 }
91
92 const AttrMode fMode;
93
94 std::vector<Attribute> fAttributes;
95
97};
98
99std::unique_ptr<GrGeometryProcessor::ProgramImpl> AttributeTestProcessor::makeProgramImpl(
100 const GrShaderCaps&) const {
101 class Impl : public ProgramImpl {
102 public:
103 void setData(const GrGLSLProgramDataManager&,
104 const GrShaderCaps&,
105 const GrGeometryProcessor&) override {}
106
107 private:
108 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
109 const AttributeTestProcessor& proc = args.fGeomProc.cast<AttributeTestProcessor>();
110 args.fVaryingHandler->emitAttributes(proc);
111 if (proc.fMode == AttrMode::kWacky) {
112 args.fVertBuilder->codeAppend("float2 pos = pos0 + pos1;");
113 }
114 args.fFragBuilder->codeAppendf("half4 %s;", args.fOutputColor);
115 args.fVaryingHandler->addPassThroughAttribute(GrShaderVar("color", SkSLType::kHalf4),
116 args.fOutputColor);
117 gpArgs->fPositionVar.set(SkSLType::kFloat2, "pos");
118 args.fFragBuilder->codeAppendf("const half4 %s = half4(1);", args.fOutputCoverage);
119 }
120 };
121
122 return std::make_unique<Impl>();
123}
124
125class AttributeTestOp : public GrDrawOp {
126public:
128
129 static GrOp::Owner Make(GrRecordingContext* context, AttrMode mode, const SkRect& r) {
130 return GrOp::Make<AttributeTestOp>(context, mode, r);
131 }
132
133private:
134 AttributeTestOp(AttrMode mode, SkRect rect) : GrDrawOp(ClassID()), fMode(mode), fRect(rect) {
135 this->setBounds(fRect, HasAABloat::kNo, IsHairline::kNo);
136 }
137
138 const char* name() const override { return "AttributeTestOp"; }
139 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
140 GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*, GrClampType) override {
142 }
143
144 GrProgramInfo* createProgramInfo(const GrCaps* caps,
145 SkArenaAlloc* arena,
146 const GrSurfaceProxyView& writeView,
147 bool usesMSAASurface,
148 GrAppliedClip&& appliedClip,
149 const GrDstProxyView& dstProxyView,
150 GrXferBarrierFlags renderPassXferBarriers,
151 GrLoadOp colorLoadOp) const {
153
154 return sk_gpu_test::CreateProgramInfo(caps,
155 arena,
156 writeView,
157 usesMSAASurface,
158 std::move(appliedClip),
159 dstProxyView,
160 geomProc,
163 renderPassXferBarriers,
164 colorLoadOp);
165 }
166
167 GrProgramInfo* createProgramInfo(GrOpFlushState* flushState) const {
168 return this->createProgramInfo(&flushState->caps(),
169 flushState->allocator(),
170 flushState->writeView(),
171 flushState->usesMSAASurface(),
172 flushState->detachAppliedClip(),
173 flushState->dstProxyView(),
174 flushState->renderPassBarriers(),
175 flushState->colorLoadOp());
176 }
177
178 void onPrePrepare(GrRecordingContext* context,
179 const GrSurfaceProxyView& writeView,
181 const GrDstProxyView& dstProxyView,
182 GrXferBarrierFlags renderPassXferBarriers,
183 GrLoadOp colorLoadOp) final {
184 SkArenaAlloc* arena = context->priv().recordTimeAllocator();
185
186 // DMSAA is not supported on DDL.
187 bool usesMSAASurface = writeView.asRenderTargetProxy()->numSamples() > 1;
188
189 // This is equivalent to a GrOpFlushState::detachAppliedClip
190 GrAppliedClip appliedClip = clip ? std::move(*clip) : GrAppliedClip::Disabled();
191
192 fProgramInfo = this->createProgramInfo(context->priv().caps(),
193 arena,
194 writeView,
195 usesMSAASurface,
196 std::move(appliedClip),
197 dstProxyView,
198 renderPassXferBarriers,
199 colorLoadOp);
200
201 context->priv().recordProgramInfo(fProgramInfo);
202 }
203
204 template <typename V> void makeVB(GrOpFlushState* flushState, const SkRect rect) {
205 V v[4];
206 v[0].p = {rect.left() , rect.top() };
207 v[1].p = {rect.right(), rect.top() };
208 v[2].p = {rect.left() , rect.bottom()};
209 v[3].p = {rect.right(), rect.bottom()};
210 v[0].color = SK_ColorRED;
211 v[1].color = SK_ColorGREEN;
212 v[2].color = SK_ColorYELLOW;
213 v[3].color = SK_ColorMAGENTA;
215 sizeof(v),
218 }
219
220 void onPrepare(GrOpFlushState* flushState) override {
221 if (fMode == AttrMode::kWacky) {
222 struct V {
223 SkPoint p;
224 uint32_t pad0;
225 uint32_t color;
226 uint32_t pad1;
227 };
228 SkRect rect {fRect.fLeft/2.f, fRect.fTop/2.f, fRect.fRight/2.f, fRect.fBottom/2.f};
229 this->makeVB<V>(flushState, rect);
230 } else {
231 struct V {
232 SkPoint p;
233 uint32_t color;
234 };
235 this->makeVB<V>(flushState, fRect);
236 }
237 }
238
239 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
240 if (!fVertexBuffer) {
241 return;
242 }
243
244 if (!fProgramInfo) {
245 fProgramInfo = this->createProgramInfo(flushState);
246 }
247
248 flushState->bindPipeline(*fProgramInfo, fRect);
249 flushState->bindBuffers(nullptr, nullptr, std::move(fVertexBuffer));
250 flushState->draw(4, 0);
251 }
252
254 const AttrMode fMode;
255 const SkRect fRect;
256
257 // The program info (and both the GrPipeline and GrGeometryProcessor it relies on), when
258 // allocated, are allocated in either the ddl-record-time or flush-time arena. It is the
259 // arena's job to free up their memory so we just have a bare programInfo pointer here. We
260 // don't even store the GrPipeline and GrGeometryProcessor pointers here bc they are
261 // guaranteed to have the same lifetime as the program info.
262 GrProgramInfo* fProgramInfo = nullptr;
263
264 friend class ::GrOp; // for ctor
265
266 using INHERITED = GrDrawOp;
267};
268
269} // namespace
270
271namespace skiagm {
272
273/**
274 * This is a GPU-backend specific test that exercises explicit and implicit attribute offsets and
275 * strides.
276 */
277class AttributesGM : public GpuGM {
278 SkString getName() const override { return SkString("attributes"); }
279 SkISize getISize() override { return {120, 340}; }
280 DrawResult onDraw(GrRecordingContext*, SkCanvas*, SkString* errorMsg) override;
281};
282
283DrawResult AttributesGM::onDraw(GrRecordingContext* rc, SkCanvas* canvas, SkString* errorMsg) {
285 if (!sdc) {
286 *errorMsg = kErrorMsg_DrawSkippedGpuOnly;
287 return DrawResult::kSkip;
288 }
289
290 sdc->clear(SK_PMColor4fBLACK);
291
292 // Draw the test directly to the frame buffer.
293 auto r = SkRect::MakeXYWH(10, 10, 100, 100);
294 for (AttrMode m : {AttrMode::kAuto, AttrMode::kManual, AttrMode::kWacky}) {
295 sdc->addDrawOp(AttributeTestOp::Make(rc, m, r));
296 r.offset(0, 110);
297 }
298
299 return DrawResult::kOk;
300}
301
302////////////////////////////////////////////////////////////////////////////////////////////////////
303
304DEF_GM( return new AttributesGM(); )
305
306} // namespace skiagm
SkRect fRect
Definition: FillRRectOp.cpp:73
#define DEFINE_OP_CLASS_ID
Definition: GrOp.h:64
GrClampType
Definition: GrTypesPriv.h:228
@ kStatic_GrAccessPattern
Definition: GrTypesPriv.h:428
GrLoadOp
Definition: GrTypesPriv.h:155
@ kFloat2_GrVertexAttribType
Definition: GrTypesPriv.h:314
@ kUByte4_norm_GrVertexAttribType
Definition: GrTypesPriv.h:334
GrXferBarrierFlags
@ kSrcOver
r = s + (1-sa)*d
constexpr SkPMColor4f SK_PMColor4fBLACK
Definition: SkColorData.h:379
constexpr SkColor SK_ColorYELLOW
Definition: SkColor.h:139
constexpr SkColor SK_ColorMAGENTA
Definition: SkColor.h:147
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
constexpr SkColor SK_ColorGREEN
Definition: SkColor.h:131
static SkPath clip(const SkPath &path, const SkHalfPlane &plane)
Definition: SkPath.cpp:3892
#define INHERITED(method,...)
Definition: SkRecorder.cpp:128
sk_sp< const GrBuffer > fVertexBuffer
Definition: GrCaps.h:57
GrLoadOp colorLoadOp() const final
const GrDstProxyView & dstProxyView() const final
GrXferBarrierFlags renderPassBarriers() const final
SkArenaAlloc * allocator() override
void bindBuffers(sk_sp< const GrBuffer > indexBuffer, sk_sp< const GrBuffer > instanceBuffer, sk_sp< const GrBuffer > vertexBuffer, GrPrimitiveRestart primitiveRestart=GrPrimitiveRestart::kNo)
const GrSurfaceProxyView & writeView() const final
GrResourceProvider * resourceProvider() const final
GrAppliedClip detachAppliedClip() final
const GrCaps & caps() const final
void bindPipeline(const GrProgramInfo &programInfo, const SkRect &drawBounds)
void draw(int vertexCount, int baseVertex)
bool usesMSAASurface() const final
std::unique_ptr< GrOp > Owner
Definition: GrOp.h:72
static constexpr Analysis EmptySetAnalysis()
sk_sp< GrGpuBuffer > createBuffer(size_t size, GrGpuBufferType, GrAccessPattern, ZeroInit)
GrRenderTargetProxy * asRenderTargetProxy() const
auto make(Ctor &&ctor) -> decltype(ctor(nullptr))
Definition: SkArenaAlloc.h:120
DlColor color
static bool b
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
T __attribute__((ext_vector_type(N))) V
SK_API sk_sp< SkDocument > Make(SkWStream *dst, const SkSerialProcs *=nullptr, std::function< void(const SkPicture *)> onEndPage=nullptr)
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
@ kNone
Definition: layer.h:53
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive mode
Definition: switches.h:228
SurfaceDrawContext * TopDeviceSurfaceDrawContext(const SkCanvas *canvas)
Definition: GrCanvas.cpp:20
DEF_GM(return F(C(clipbox), 0.0f, 0.0f, {})) DEF_GM(return F(C(clipbox)
DrawResult
Definition: gm.h:104
Definition: SkSize.h:16
SkScalar fBottom
larger y-axis bounds
Definition: extension.cpp:17
SkScalar fLeft
smaller x-axis bounds
Definition: extension.cpp:14
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659
SkScalar fRight
larger x-axis bounds
Definition: extension.cpp:16
SkScalar fTop
smaller y-axis bounds
Definition: extension.cpp:15
SkBlendMode fMode
Definition: xfermodes.cpp:52