Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Protected Types | Protected Member Functions | List of all members
skiagm::MeshWithShadersGM Class Reference
Inheritance diagram for skiagm::MeshWithShadersGM:
skiagm::GM

Public Types

enum class  Type { kMeshWithImage , kMeshWithPaintColor , kMeshWithPaintImage , kMeshWithEffects }
 
- Public Types inherited from skiagm::GM
enum  Mode { kGM_Mode , kSample_Mode , kBench_Mode }
 
using DrawResult = skiagm::DrawResult
 
using GraphiteTestContext = skiatest::graphite::GraphiteTestContext
 

Public Member Functions

 MeshWithShadersGM (Type type)
 
- Public Member Functions inherited from skiagm::GM
 GM (SkColor backgroundColor=SK_ColorWHITE)
 
virtual ~GM ()
 
void setMode (Mode mode)
 
Mode getMode () const
 
DrawResult gpuSetup (SkCanvas *, SkString *errorMsg, GraphiteTestContext *=nullptr)
 
void gpuTeardown ()
 
void onceBeforeDraw ()
 
DrawResult draw (SkCanvas *canvas)
 
DrawResult draw (SkCanvas *, SkString *errorMsg)
 
void drawBackground (SkCanvas *)
 
DrawResult drawContent (SkCanvas *canvas)
 
DrawResult drawContent (SkCanvas *, SkString *errorMsg)
 
virtual bool runAsBench () const
 
SkScalar width ()
 
SkScalar height ()
 
SkColor getBGColor () const
 
void setBGColor (SkColor)
 
void drawSizeBounds (SkCanvas *, SkColor)
 
bool animate (double)
 
virtual bool onChar (SkUnichar)
 
bool getControls (SkMetaData *controls)
 
void setControls (const SkMetaData &controls)
 
virtual void modifyGrContextOptions (GrContextOptions *)
 
virtual void modifyGraphiteContextOptions (skgpu::graphite::ContextOptions *) const
 
virtual bool isBazelOnly () const
 
virtual std::map< std::string, std::string > getGoldKeys () const
 

Protected Types

using Attribute = SkMeshSpecification::Attribute
 
using Varying = SkMeshSpecification::Varying
 

Protected Member Functions

SkISize getISize () override
 
void onOnceBeforeDraw () override
 
DrawResult onGpuSetup (SkCanvas *canvas, SkString *string, GraphiteTestContext *) override
 
void onGpuTeardown () override
 
SkString getName () const override
 
bool onAnimate (double nanos) override
 
DrawResult onDraw (SkCanvas *canvas, SkString *) override
 
- Protected Member Functions inherited from skiagm::GM
virtual void onDraw (SkCanvas *)
 
virtual bool onGetControls (SkMetaData *)
 
virtual void onSetControls (const SkMetaData &)
 
GraphiteTestContextgraphiteTestContext () const
 

Additional Inherited Members

- Static Public Attributes inherited from skiagm::GM
static constexpr char kErrorMsg_DrawSkippedGpuOnly []
 

Detailed Description

Definition at line 1232 of file mesh.cpp.

Member Typedef Documentation

◆ Attribute

Definition at line 1267 of file mesh.cpp.

◆ Varying

Definition at line 1268 of file mesh.cpp.

Member Enumeration Documentation

◆ Type

Enumerator
kMeshWithImage 
kMeshWithPaintColor 
kMeshWithPaintImage 
kMeshWithEffects 

Definition at line 1234 of file mesh.cpp.

Constructor & Destructor Documentation

◆ MeshWithShadersGM()

skiagm::MeshWithShadersGM::MeshWithShadersGM ( Type  type)
inline

Definition at line 1241 of file mesh.cpp.

1241 : fType(type) {
1242 // Create a grid of evenly spaced points for our mesh
1243 this->onAnimate(0.0);
1244
1245 // Create an index buffer of triangles over our point mesh.
1246 for (int y = 0; y < kMeshSize - 1; ++y) {
1247 for (int x = 0; x < kMeshSize - 1; ++x) {
1248 SkASSERT(((y + 1) * kMeshSize + x + 1) < fVerts.size());
1249
1250 uint16_t TL = y * kMeshSize + x;
1251 uint16_t TR = y * kMeshSize + x + 1;
1252 uint16_t BL = (y + 1) * kMeshSize + x;
1253 uint16_t BR = (y + 1) * kMeshSize + x + 1;
1254
1255 fIndices.push_back(TL);
1256 fIndices.push_back(TR);
1257 fIndices.push_back(BL);
1258
1259 fIndices.push_back(BR);
1260 fIndices.push_back(BL);
1261 fIndices.push_back(TR);
1262 }
1263 }
1264 }
#define SkASSERT(cond)
Definition SkAssert.h:116
int size() const
Definition SkTArray.h:416
bool onAnimate(double nanos) override
Definition mesh.cpp:1405
double y
double x

Member Function Documentation

◆ getISize()

SkISize skiagm::MeshWithShadersGM::getISize ( )
inlineoverrideprotectedvirtual

Implements skiagm::GM.

Definition at line 1270 of file mesh.cpp.

1270{ return {320, 320}; }

◆ getName()

SkString skiagm::MeshWithShadersGM::getName ( ) const
inlineoverrideprotectedvirtual

Implements skiagm::GM.

Definition at line 1395 of file mesh.cpp.

1395 {
1396 switch (fType) {
1397 case Type::kMeshWithImage: return SkString("mesh_with_image");
1398 case Type::kMeshWithEffects: return SkString("mesh_with_effects");
1399 case Type::kMeshWithPaintColor: return SkString("mesh_with_paint_color");
1400 case Type::kMeshWithPaintImage: return SkString("mesh_with_paint_image");
1401 default: SkUNREACHABLE;
1402 }
1403 }
#define SkUNREACHABLE
Definition SkAssert.h:135

◆ onAnimate()

bool skiagm::MeshWithShadersGM::onAnimate ( double  nanos)
inlineoverrideprotectedvirtual

Reimplemented from skiagm::GM.

Definition at line 1405 of file mesh.cpp.

1405 {
1406 // `periodic` goes from zero to 2π every four seconds, then wraps around.
1407 double periodic = nanos / 4'000'000'000.;
1408 periodic -= std::floor(periodic);
1409 periodic *= 2 * SK_DoublePI;
1410
1411 double xOff[kMeshSize], yOff[kMeshSize];
1412 for (int index = 0; index < kMeshSize; ++index) {
1413 xOff[index] = std::sin(periodic) * kRippleSize;
1414 yOff[index] = std::sin(periodic + 10.0) * kRippleSize;
1415 periodic += 0.8;
1416 }
1417
1418 fVerts.clear();
1419 for (int y = 0; y < kMeshSize; ++y) {
1420 float yf = (float)y / (kMeshSize - 1); // yf = 0 .. 1
1421 for (int x = 0; x < kMeshSize; ++x) {
1422 float xf = (float)x / (kMeshSize - 1); // xf = 0 .. 1
1423
1424 Vertex* vert = &fVerts.push_back();
1425 vert->pos[0] = kRect.left() + xf * kRect.width() + xOff[y];
1426 vert->pos[1] = kRect.top() + yf * kRect.height() + yOff[x];
1427 vert->uv[0] = kUV.left() + xf * kUV.width();
1428 vert->uv[1] = kUV.top() + yf * kUV.height();
1429 }
1430 }
1431
1432 return true;
1433 }
constexpr double SK_DoublePI

◆ onDraw()

DrawResult skiagm::MeshWithShadersGM::onDraw ( SkCanvas canvas,
SkString  
)
inlineoverrideprotectedvirtual

Reimplemented from skiagm::GM.

Definition at line 1435 of file mesh.cpp.

1435 {
1436 SkRuntimeEffect::ChildPtr child[4] = {fShader1, fShader2, fColorFilter, fBlender};
1437
1440 fVB->update(dc, fVerts.data(), /*offset=*/0, fVerts.size_bytes());
1441
1444 fVB,
1445 fVerts.size(),
1446 /*vertexOffset=*/0,
1447 fIB,
1448 fIndices.size(),
1449 /*indexOffset=*/0,
1450 /*uniforms=*/nullptr,
1451 /*children=*/child,
1452 kRect.makeOutset(kRippleSize, kRippleSize));
1453 if (!result.mesh.isValid()) {
1454 SkDebugf("Mesh creation failed: %s\n", result.error.c_str());
1455 return DrawResult::kFail;
1456 }
1457
1458 SkPaint paint;
1459 paint.setShader(fPaintShader);
1461
1462 return DrawResult::kOk;
1463 }
static GrDirectContext * GrAsDirectContext(GrContext_Base *base)
@ kDstOver
r = d + (1-da)*s
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
static sk_sp< SkBlender > Mode(SkBlendMode mode)
virtual GrRecordingContext * recordingContext() const
void drawMesh(const SkMesh &mesh, sk_sp< SkBlender > blender, const SkPaint &paint)
bool update(GrDirectContext *, const void *data, size_t offset, size_t size)
Definition SkMesh.cpp:881
static Result MakeIndexed(sk_sp< SkMeshSpecification >, Mode, sk_sp< VertexBuffer >, size_t vertexCount, size_t vertexOffset, sk_sp< IndexBuffer >, size_t indexCount, size_t indexOffset, sk_sp< const SkData > uniforms, SkSpan< ChildPtr > children, const SkRect &bounds)
Definition SkMesh.cpp:718
size_t size_bytes() const
Definition SkTArray.h:417
const Paint & paint
GAsyncResult * result

◆ onGpuSetup()

DrawResult skiagm::MeshWithShadersGM::onGpuSetup ( SkCanvas canvas,
SkString string,
GraphiteTestContext  
)
inlineoverrideprotectedvirtual

Reimplemented from skiagm::GM.

Definition at line 1375 of file mesh.cpp.

1375 {
1376 auto dc = GrAsDirectContext(canvas->recordingContext());
1377 this->ensureBuffers();
1378 if (!dc || dc->abandoned()) {
1379 return DrawResult::kOk;
1380 }
1381
1382 fVB = SkMeshes::CopyVertexBuffer(dc, fVB);
1383 fIB = SkMeshes::CopyIndexBuffer (dc, fIB);
1384 return (!fVB || !fIB) ? DrawResult::kFail
1386 }
SK_API sk_sp< SkMesh::IndexBuffer > CopyIndexBuffer(const sk_sp< SkMesh::IndexBuffer > &)
Definition SkMesh.cpp:893
SK_API sk_sp< SkMesh::VertexBuffer > CopyVertexBuffer(const sk_sp< SkMesh::VertexBuffer > &)
Definition SkMesh.cpp:909

◆ onGpuTeardown()

void skiagm::MeshWithShadersGM::onGpuTeardown ( )
inlineoverrideprotectedvirtual

Reimplemented from skiagm::GM.

Definition at line 1388 of file mesh.cpp.

1388 {
1389 // Destroy the GPU buffers and recreate on CPU
1390 fVB = nullptr;
1391 fIB = nullptr;
1392 this->ensureBuffers();
1393 }

◆ onOnceBeforeDraw()

void skiagm::MeshWithShadersGM::onOnceBeforeDraw ( )
inlineoverrideprotectedvirtual

Reimplemented from skiagm::GM.

Definition at line 1272 of file mesh.cpp.

1272 {
1273 {
1274 static const Attribute kAttributes[] = {
1275 {Attribute::Type::kFloat2, 0, SkString{"position"}},
1277 };
1278 static const Varying kVaryings[] = {
1280 };
1281 static constexpr char kVS[] = R"(
1282 Varyings main(const in Attributes attributes) {
1283 Varyings varyings;
1284 varyings.uv = attributes.uv;
1285 varyings.position = attributes.position;
1286 return varyings;
1287 }
1288 )";
1289 static constexpr char kFS[] = R"(
1290 uniform shader myShader1;
1291 uniform shader myShader2;
1292 uniform colorFilter myColorFilter;
1293 uniform blender myBlend;
1294
1295 float2 main(const in Varyings varyings, out half4 color) {
1296 half4 color1 = myShader1.eval(varyings.uv);
1297 half4 color2 = myShader2.eval(varyings.uv);
1298
1299 // Apply a inverse color filter to the first image.
1300 color1 = myColorFilter.eval(color1);
1301
1302 // Fade in the second image horizontally, leveraging the UVs.
1303 color2 *= varyings.uv.x / 128.0;
1304
1305 // Combine the two images by using a blender (set to dst-over).
1306 color = myBlend.eval(color1, color2);
1307
1308 return varyings.uv;
1309 }
1310 )";
1311 auto [spec, error] = SkMeshSpecification::Make(kAttributes,
1312 sizeof(Vertex),
1313 kVaryings,
1314 SkString(kVS),
1315 SkString(kFS));
1316 if (!spec) {
1317 SkDebugf("%s\n", error.c_str());
1318 }
1319 fSpec = std::move(spec);
1320 }
1321
1322
1323 switch (fType) {
1324 case Type::kMeshWithImage: {
1325 fShader1 = ToolUtils::GetResourceAsImage("images/mandrill_128.png")
1327 fShader2 = nullptr;
1328 fColorFilter = nullptr;
1329 fBlender = nullptr;
1330 fPaintShader = nullptr;
1331 break;
1332 }
1334 uint8_t inverseTable[256];
1335 for (int index = 0; index < 256; ++index) {
1336 inverseTable[index] = 255 - index;
1337 }
1338
1339 fShader1 = ToolUtils::GetResourceAsImage("images/mandrill_128.png")
1341 fShader2 = ToolUtils::GetResourceAsImage("images/color_wheel.png")
1343 fColorFilter = SkColorFilters::TableARGB(/*tableA=*/nullptr,
1344 inverseTable,
1345 inverseTable,
1346 inverseTable);
1348 fPaintShader = nullptr;
1349 break;
1350 }
1352 fShader1 = nullptr;
1353 fShader2 = ToolUtils::GetResourceAsImage("images/mandrill_128.png")
1355 fColorFilter = nullptr;
1357 fPaintShader = SkShaders::Color(SK_ColorGREEN);
1358 break;
1359 }
1361 fShader1 = ToolUtils::GetResourceAsImage("images/color_wheel.png")
1363 fShader2 = nullptr;
1364 fColorFilter = nullptr;
1365 fBlender = nullptr;
1366 fPaintShader = ToolUtils::GetResourceAsImage("images/mandrill_128.png")
1368 break;
1369 }
1370 default:
1372 }
1373 }
constexpr SkColor SK_ColorGREEN
Definition SkColor.h:131
static sk_sp< SkColorFilter > TableARGB(const uint8_t tableA[256], const uint8_t tableR[256], const uint8_t tableG[256], const uint8_t tableB[256])
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Definition SkImage.cpp:179
static Result Make(SkSpan< const Attribute > attributes, size_t vertexStride, SkSpan< const Varying > varyings, const SkString &vs, const SkString &fs)
Definition SkMesh.cpp:389
const uint8_t uint32_t uint32_t GError ** error
sk_sp< SkImage > GetResourceAsImage(const char *resource)
Definition DecodeUtils.h:25

The documentation for this class was generated from the following file: