Flutter Engine
The Flutter Engine
SkSLES2ConformanceTest.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/**
9 * This test relies on GLSL ES2 conformance test files, which are not included in Skia.
10 *
11 * To run the test suite, open `resources/sksl/es2_conformance/import_conformance_tests.py` and
12 * follow the instructions at the top to download and import the test suite.
13 */
14
18#include "include/core/SkData.h"
21#include "include/core/SkRect.h"
31#include "src/core/SkOSFile.h"
32#include "src/utils/SkOSPath.h"
34#include "tests/Test.h"
35#include "tools/Resources.h"
36
37#include <functional>
38
39struct GrContextOptions;
40
41static void test_expect_fail(skiatest::Reporter* r, const char* testFile) {
43 sk_sp<SkData> shaderData = GetResourceAsData(testFile);
44 if (!shaderData) {
45 ERRORF(r, "%s: Unable to load file", SkOSPath::Basename(testFile).c_str());
46 return;
47 }
48
49 SkString shaderString{reinterpret_cast<const char*>(shaderData->bytes()), shaderData->size()};
51 if (result.effect) {
52 ERRORF(r, "%s: Expected failure, but compiled successfully",
53 SkOSPath::Basename(testFile).c_str());
54 return;
55 }
56}
57
58static void test_expect_pass(skiatest::Reporter* r, SkSurface* surface, const char* testFile) {
60 sk_sp<SkData> shaderData = GetResourceAsData(testFile);
61 if (!shaderData) {
62 ERRORF(r, "%s: Unable to load file", testFile);
63 return;
64 }
65
66 SkString shaderString{reinterpret_cast<const char*>(shaderData->bytes()), shaderData->size()};
68 if (!result.effect) {
69 ERRORF(r, "%s: %s", testFile, result.errorText.c_str());
70 return;
71 }
72
74 sk_sp<SkShader> shader = builder.makeShader();
75 if (!shader) {
76 ERRORF(r, "%s: Unable to build shader", testFile);
77 return;
78 }
79
80 SkPaint paintShader;
81 paintShader.setShader(shader);
82 surface->getCanvas()->drawRect(SkRect::MakeWH(1, 1), paintShader);
83
85 REPORTER_ASSERT(r, bitmap.tryAllocPixels(surface->imageInfo()));
86 REPORTER_ASSERT(r, surface->readPixels(bitmap.info(), bitmap.getPixels(), bitmap.rowBytes(),
87 /*srcX=*/0, /*srcY=*/0));
88
89 SkColor color = bitmap.getColor(0, 0);
90 if (color != SkColorSetARGB(0xFF, 0x00, 0xFF, 0x00)) {
91 ERRORF(r, "%s: Expected solid green. Actual:\n"
92 "RRGGBBAA\n"
93 "%02X%02X%02X%02X",
94 testFile,
96 }
97}
98
99static void iterate_dir(const char* directory, const std::function<void(const char*)>& run) {
100 SkString resourceDirectory = GetResourcePath(directory);
101 SkOSFile::Iter iter(resourceDirectory.c_str(), ".rts");
103
104 while (iter.next(&name, /*getDir=*/false)) {
105 SkString path(SkOSPath::Join(directory, name.c_str()));
106 run(path.c_str());
107 }
108}
109
110DEF_TEST(SkSL_ES2Conformance_Pass_CPU, r) {
113
114 iterate_dir("sksl/es2_conformance/pass/", [&](const char* path) {
115 test_expect_pass(r, surface.get(), path);
116 });
117}
118
119DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(SkSL_ES2Conformance_Pass_GPU,
120 r,
121 ctxInfo,
125 SkSurfaces::RenderTarget(ctxInfo.directContext(), skgpu::Budgeted::kNo, info));
126 iterate_dir("sksl/es2_conformance/pass/", [&](const char* path) {
127 test_expect_pass(r, surface.get(), path);
128 });
129}
130
131DEF_TEST(SkSL_ES2Conformance_Fail, r) {
132 iterate_dir("sksl/es2_conformance/fail/", [&](const char* path) {
134 });
135}
const char * options
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
sk_sp< SkData > GetResourceAsData(const char *resource)
Definition: Resources.cpp:42
SkString GetResourcePath(const char *resource)
Definition: Resources.cpp:23
#define SkColorGetR(color)
Definition: SkColor.h:65
#define SkColorGetG(color)
Definition: SkColor.h:69
uint32_t SkColor
Definition: SkColor.h:37
static constexpr SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition: SkColor.h:49
#define SkColorGetA(color)
Definition: SkColor.h:61
#define SkColorGetB(color)
Definition: SkColor.h:73
static void test_expect_pass(skiatest::Reporter *r, SkSurface *surface, const char *testFile)
static void test_expect_fail(skiatest::Reporter *r, const char *testFile)
DEF_TEST(SkSL_ES2Conformance_Pass_CPU, r)
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(SkSL_ES2Conformance_Pass_GPU, r, ctxInfo, CtsEnforcement::kApiLevel_T)
static void iterate_dir(const char *directory, const std::function< void(const char *)> &run)
#define REPORTER_ASSERT(r, cond,...)
Definition: Test.h:286
#define ERRORF(r,...)
Definition: Test.h:293
const uint8_t * bytes() const
Definition: SkData.h:43
size_t size() const
Definition: SkData.h:30
SK_SPI bool next(SkString *name, bool getDir=false)
static SkString Join(const char *rootPath, const char *relativePath)
Definition: SkOSPath.cpp:14
static SkString Basename(const char *fullPath)
Definition: SkOSPath.cpp:23
void setShader(sk_sp< SkShader > shader)
static Result MakeForShader(SkString sksl, const Options &)
const char * c_str() const
Definition: SkString.h:133
DlColor color
VkSurfaceKHR surface
Definition: main.cc:49
GAsyncResult * result
Dart_NativeFunction function
Definition: fuchsia.cc:51
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
SK_API sk_sp< SkSurface > RenderTarget(GrRecordingContext *context, skgpu::Budgeted budgeted, const SkImageInfo &imageInfo, int sampleCount, GrSurfaceOrigin surfaceOrigin, const SkSurfaceProps *surfaceProps, bool shouldCreateWithMips=false, bool isProtected=false)
Definition: bitmap.py:1
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
Definition: run.py:1
def run(cmd)
Definition: run.py:14
static SkImageInfo MakeN32Premul(int width, int height)
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609