xref: /aosp_15_r20/external/skia/tests/SkSLES2ConformanceTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 "include/core/SkBitmap.h"
16 #include "include/core/SkCanvas.h"
17 #include "include/core/SkColor.h"
18 #include "include/core/SkData.h"
19 #include "include/core/SkImageInfo.h"
20 #include "include/core/SkPaint.h"
21 #include "include/core/SkRect.h"
22 #include "include/core/SkRefCnt.h"
23 #include "include/core/SkShader.h"
24 #include "include/core/SkString.h"
25 #include "include/core/SkSurface.h"
26 #include "include/core/SkTypes.h"
27 #include "include/effects/SkRuntimeEffect.h"
28 #include "include/gpu/GpuTypes.h"
29 #include "include/gpu/ganesh/GrDirectContext.h"
30 #include "include/gpu/ganesh/SkSurfaceGanesh.h"
31 #include "src/core/SkOSFile.h"
32 #include "src/utils/SkOSPath.h"
33 #include "tests/CtsEnforcement.h"
34 #include "tests/Test.h"
35 #include "tools/Resources.h"
36 
37 #include <functional>
38 
39 struct GrContextOptions;
40 
test_expect_fail(skiatest::Reporter * r,const char * testFile)41 static void test_expect_fail(skiatest::Reporter* r, const char* testFile) {
42     SkRuntimeEffect::Options options{};
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()};
50     SkRuntimeEffect::Result result = SkRuntimeEffect::MakeForShader(shaderString, options);
51     if (result.effect) {
52         ERRORF(r, "%s: Expected failure, but compiled successfully",
53                   SkOSPath::Basename(testFile).c_str());
54         return;
55     }
56 }
57 
test_expect_pass(skiatest::Reporter * r,SkSurface * surface,const char * testFile)58 static void test_expect_pass(skiatest::Reporter* r, SkSurface* surface, const char* testFile) {
59     SkRuntimeEffect::Options options{};
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()};
67     SkRuntimeEffect::Result result = SkRuntimeEffect::MakeForShader(shaderString, options);
68     if (!result.effect) {
69         ERRORF(r, "%s: %s", testFile, result.errorText.c_str());
70         return;
71     }
72 
73     SkRuntimeShaderBuilder builder(result.effect);
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 
84     SkBitmap bitmap;
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,
95                   SkColorGetR(color), SkColorGetG(color), SkColorGetB(color), SkColorGetA(color));
96     }
97 }
98 
iterate_dir(const char * directory,const std::function<void (const char *)> & run)99 static 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");
102     SkString name;
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 
DEF_TEST(SkSL_ES2Conformance_Pass_CPU,r)110 DEF_TEST(SkSL_ES2Conformance_Pass_CPU, r) {
111     const SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
112     sk_sp<SkSurface> surface(SkSurfaces::Raster(info));
113 
114     iterate_dir("sksl/es2_conformance/pass/", [&](const char* path) {
115         test_expect_pass(r, surface.get(), path);
116     });
117 }
118 
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(SkSL_ES2Conformance_Pass_GPU,r,ctxInfo,CtsEnforcement::kApiLevel_T)119 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(SkSL_ES2Conformance_Pass_GPU,
120                                        r,
121                                        ctxInfo,
122                                        CtsEnforcement::kApiLevel_T) {
123     const SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
124     sk_sp<SkSurface> surface(
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 
DEF_TEST(SkSL_ES2Conformance_Fail,r)131 DEF_TEST(SkSL_ES2Conformance_Fail, r) {
132     iterate_dir("sksl/es2_conformance/fail/", [&](const char* path) {
133         test_expect_fail(r, path);
134     });
135 }
136