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
15 #include "gm/gm.h"
16 #include "include/core/SkBitmap.h"
17 #include "include/core/SkCanvas.h"
18 #include "include/core/SkData.h"
19 #include "include/core/SkFont.h"
20 #include "include/core/SkPaint.h"
21 #include "include/core/SkSize.h"
22 #include "include/core/SkString.h"
23 #include "include/core/SkSurface.h"
24 #include "include/effects/SkGradientShader.h"
25 #include "include/effects/SkImageFilters.h"
26 #include "include/effects/SkRuntimeEffect.h"
27 #include "include/utils/SkRandom.h"
28 #include "src/core/SkOSFile.h"
29 #include "src/core/SkRuntimeEffectPriv.h"
30 #include "src/gpu/GrCaps.h"
31 #include "src/gpu/GrDirectContextPriv.h"
32 #include "src/utils/SkOSPath.h"
33 #include "tests/Test.h"
34 #include "tools/Resources.h"
35 #include "tools/ToolUtils.h"
36
37 #include <string_view>
38
test_expect_fail(skiatest::Reporter * r,const char * testFile)39 static void test_expect_fail(skiatest::Reporter* r, const char* testFile) {
40 SkRuntimeEffect::Options options{};
41 sk_sp<SkData> shaderData = GetResourceAsData(testFile);
42 if (!shaderData) {
43 ERRORF(r, "%s: Unable to load file", SkOSPath::Basename(testFile).c_str());
44 return;
45 }
46
47 SkString shaderString{reinterpret_cast<const char*>(shaderData->bytes()), shaderData->size()};
48 SkRuntimeEffect::Result result = SkRuntimeEffect::MakeForShader(shaderString, options);
49 if (result.effect) {
50 ERRORF(r, "%s: Expected failure, but compiled successfully",
51 SkOSPath::Basename(testFile).c_str());
52 return;
53 }
54 }
55
test_expect_pass(skiatest::Reporter * r,SkSurface * surface,const char * testFile)56 static void test_expect_pass(skiatest::Reporter* r, SkSurface* surface, const char* testFile) {
57 SkRuntimeEffect::Options options{};
58 sk_sp<SkData> shaderData = GetResourceAsData(testFile);
59 if (!shaderData) {
60 ERRORF(r, "%s: Unable to load file", testFile);
61 return;
62 }
63
64 SkString shaderString{reinterpret_cast<const char*>(shaderData->bytes()), shaderData->size()};
65 SkRuntimeEffect::Result result = SkRuntimeEffect::MakeForShader(shaderString, options);
66 if (!result.effect) {
67 ERRORF(r, "%s: %s", testFile, result.errorText.c_str());
68 return;
69 }
70
71 SkRuntimeShaderBuilder builder(result.effect);
72 sk_sp<SkShader> shader = builder.makeShader();
73 if (!shader) {
74 ERRORF(r, "%s: Unable to build shader", testFile);
75 return;
76 }
77
78 SkPaint paintShader;
79 paintShader.setShader(shader);
80 surface->getCanvas()->drawRect(SkRect::MakeWH(1, 1), paintShader);
81
82 SkBitmap bitmap;
83 REPORTER_ASSERT(r, bitmap.tryAllocPixels(surface->imageInfo()));
84 REPORTER_ASSERT(r, surface->readPixels(bitmap.info(), bitmap.getPixels(), bitmap.rowBytes(),
85 /*srcX=*/0, /*srcY=*/0));
86
87 SkColor color = bitmap.getColor(0, 0);
88 if (color != SkColorSetARGB(0xFF, 0x00, 0xFF, 0x00)) {
89 ERRORF(r, "%s: Expected solid green. Actual:\n"
90 "RRGGBBAA\n"
91 "%02X%02X%02X%02X",
92 testFile,
93 SkColorGetR(color), SkColorGetG(color), SkColorGetB(color), SkColorGetA(color));
94 }
95 }
96
iterate_dir(const char * directory,const std::function<void (const char *)> & run)97 static void iterate_dir(const char* directory, const std::function<void(const char*)>& run) {
98 SkString resourceDirectory = GetResourcePath(directory);
99 SkOSFile::Iter iter(resourceDirectory.c_str(), ".rts");
100 SkString name;
101
102 while (iter.next(&name, /*getDir=*/false)) {
103 SkString path(SkOSPath::Join(directory, name.c_str()));
104 run(path.c_str());
105 }
106 }
107
DEF_TEST(SkSL_ES2Conformance_Pass_CPU,r)108 DEF_TEST(SkSL_ES2Conformance_Pass_CPU, r) {
109 const SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
110 sk_sp<SkSurface> surface(SkSurface::MakeRaster(info));
111
112 iterate_dir("sksl/es2_conformance/pass/", [&](const char* path) {
113 test_expect_pass(r, surface.get(), path);
114 });
115 }
116
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkSL_ES2Conformance_Pass_GPU,r,ctxInfo)117 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkSL_ES2Conformance_Pass_GPU, r, ctxInfo) {
118 const SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
119 sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(ctxInfo.directContext(),
120 SkBudgeted::kNo, info));
121 iterate_dir("sksl/es2_conformance/pass/", [&](const char* path) {
122 test_expect_pass(r, surface.get(), path);
123 });
124 }
125
DEF_TEST(SkSL_ES2Conformance_Fail,r)126 DEF_TEST(SkSL_ES2Conformance_Fail, r) {
127 iterate_dir("sksl/es2_conformance/fail/", [&](const char* path) {
128 test_expect_fail(r, path);
129 });
130 }
131