skia2/tests/SkSLES2ConformanceTest.cpp
Kevin Lubick 08ece0c9a0 [includes] Enforce IWYU on sksl code
PS1 regenerates the Bazel files. Use it as the base change when
    comparing patchsets.

IWYU seems to do a good job of working with MyFile.cpp and
MyFile.h, but if there is just a MyHeader.h, it doesn't always
seem to throw errors if the includes aren't correct. This was
observed with include/sksl/DSL.h This might be due to the fact
that headers are not compiled on their own, so they are never
sent directly to the IWYU binary.

This change sets enforce_iwyu_on_package() on the all sksl
packages and then fixes the includes until all those checks
are happy. There were a few files that needed fixes outside
of the sksl folder. Examples include:
 - src/gpu/effects/GrConvexPolyEffect.cpp
 - tests/SkSLDSLTest.cpp

To really enforce this, we need to add a CI/CQ job that runs
bazel build //example:hello_world_gl --config=clang \
  --sandbox_base=/dev/shm --features skia_enforce_iwyu

If that failed, a dev could make the changes described in
the logs and/or run the command locally to see those
prescribed fixes.

I had to add several entries to toolchain/IWYU_mapping.imp
in order to fix some private includes and other atypical
choices. I tried adding a rule there to allow inclusion of
SkTypes.h to make sure defines like SK_SUPPORT_GPU, but
could not get it to work for all cases, so I deferred to
using the IWYU pragma: keep (e.g. SkSLPipelineStageCodeGenerator.h)

Change-Id: I4c3e536d8e69ff7ff2d26fe61a525a6c2e80db06
Bug: skia:13052
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/522256
Reviewed-by: John Stiles <johnstiles@google.com>
Reviewed-by: Greg Daniel <egdaniel@google.com>
2022-03-21 12:43:02 +00:00

129 lines
4.5 KiB
C++

/*
* Copyright 2021 Google LLC
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/**
* This test relies on GLSL ES2 conformance test files, which are not included in Skia.
*
* To run the test suite, open `resources/sksl/es2_conformance/import_conformance_tests.py` and
* follow the instructions at the top to download and import the test suite.
*/
#include "include/core/SkBitmap.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkColor.h"
#include "include/core/SkData.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkPaint.h"
#include "include/core/SkRect.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkShader.h"
#include "include/core/SkString.h"
#include "include/core/SkSurface.h"
#include "include/core/SkTypes.h"
#include "include/effects/SkRuntimeEffect.h"
#include "include/gpu/GrDirectContext.h"
#include "src/core/SkOSFile.h"
#include "src/utils/SkOSPath.h"
#include "tests/Test.h"
#include "tools/Resources.h"
#include "tools/gpu/GrContextFactory.h"
#include <functional>
static void test_expect_fail(skiatest::Reporter* r, const char* testFile) {
SkRuntimeEffect::Options options{};
sk_sp<SkData> shaderData = GetResourceAsData(testFile);
if (!shaderData) {
ERRORF(r, "%s: Unable to load file", SkOSPath::Basename(testFile).c_str());
return;
}
SkString shaderString{reinterpret_cast<const char*>(shaderData->bytes()), shaderData->size()};
SkRuntimeEffect::Result result = SkRuntimeEffect::MakeForShader(shaderString, options);
if (result.effect) {
ERRORF(r, "%s: Expected failure, but compiled successfully",
SkOSPath::Basename(testFile).c_str());
return;
}
}
static void test_expect_pass(skiatest::Reporter* r, SkSurface* surface, const char* testFile) {
SkRuntimeEffect::Options options{};
sk_sp<SkData> shaderData = GetResourceAsData(testFile);
if (!shaderData) {
ERRORF(r, "%s: Unable to load file", testFile);
return;
}
SkString shaderString{reinterpret_cast<const char*>(shaderData->bytes()), shaderData->size()};
SkRuntimeEffect::Result result = SkRuntimeEffect::MakeForShader(shaderString, options);
if (!result.effect) {
ERRORF(r, "%s: %s", testFile, result.errorText.c_str());
return;
}
SkRuntimeShaderBuilder builder(result.effect);
sk_sp<SkShader> shader = builder.makeShader();
if (!shader) {
ERRORF(r, "%s: Unable to build shader", testFile);
return;
}
SkPaint paintShader;
paintShader.setShader(shader);
surface->getCanvas()->drawRect(SkRect::MakeWH(1, 1), paintShader);
SkBitmap bitmap;
REPORTER_ASSERT(r, bitmap.tryAllocPixels(surface->imageInfo()));
REPORTER_ASSERT(r, surface->readPixels(bitmap.info(), bitmap.getPixels(), bitmap.rowBytes(),
/*srcX=*/0, /*srcY=*/0));
SkColor color = bitmap.getColor(0, 0);
if (color != SkColorSetARGB(0xFF, 0x00, 0xFF, 0x00)) {
ERRORF(r, "%s: Expected solid green. Actual:\n"
"RRGGBBAA\n"
"%02X%02X%02X%02X",
testFile,
SkColorGetR(color), SkColorGetG(color), SkColorGetB(color), SkColorGetA(color));
}
}
static void iterate_dir(const char* directory, const std::function<void(const char*)>& run) {
SkString resourceDirectory = GetResourcePath(directory);
SkOSFile::Iter iter(resourceDirectory.c_str(), ".rts");
SkString name;
while (iter.next(&name, /*getDir=*/false)) {
SkString path(SkOSPath::Join(directory, name.c_str()));
run(path.c_str());
}
}
DEF_TEST(SkSL_ES2Conformance_Pass_CPU, r) {
const SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
sk_sp<SkSurface> surface(SkSurface::MakeRaster(info));
iterate_dir("sksl/es2_conformance/pass/", [&](const char* path) {
test_expect_pass(r, surface.get(), path);
});
}
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkSL_ES2Conformance_Pass_GPU, r, ctxInfo) {
const SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(ctxInfo.directContext(),
SkBudgeted::kNo, info));
iterate_dir("sksl/es2_conformance/pass/", [&](const char* path) {
test_expect_pass(r, surface.get(), path);
});
}
DEF_TEST(SkSL_ES2Conformance_Fail, r) {
iterate_dir("sksl/es2_conformance/fail/", [&](const char* path) {
test_expect_fail(r, path);
});
}