blob: a378b226a28cda7ecea53fd12ce2e6a8e1168aef [file] [log] [blame]
John Stiles232dd2b2021-01-25 10:57:47 -05001/*
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#include "gm/gm.h"
9#include "include/core/SkBitmap.h"
10#include "include/core/SkCanvas.h"
11#include "include/core/SkData.h"
12#include "include/core/SkFont.h"
13#include "include/core/SkPaint.h"
14#include "include/core/SkSize.h"
15#include "include/core/SkString.h"
16#include "include/core/SkSurface.h"
17#include "include/effects/SkGradientShader.h"
18#include "include/effects/SkImageFilters.h"
19#include "include/effects/SkRuntimeEffect.h"
20#include "include/utils/SkRandom.h"
21#include "tests/Test.h"
22#include "tools/Resources.h"
23#include "tools/ToolUtils.h"
24
25static const SkRect kRect = SkRect::MakeWH(1, 1);
26
27static void test(skiatest::Reporter* r, SkSurface* surface, const char* testFile) {
28 SkString resourcePath = SkStringPrintf("sksl/%s", testFile);
29 sk_sp<SkData> shaderData = GetResourceAsData(resourcePath.c_str());
30 if (!shaderData) {
31 ERRORF(r, "%s: Unable to load file", testFile);
32 return;
33 }
34
35 SkString shaderString{reinterpret_cast<const char*>(shaderData->bytes()), shaderData->size()};
36 auto [effect, error] = SkRuntimeEffect::Make(shaderString);
37 if (!effect) {
38 ERRORF(r, "%s: %s", testFile, error.c_str());
39 return;
40 }
41
42 SkRuntimeShaderBuilder builder(effect);
43 sk_sp<SkShader> shader = builder.makeShader(/*localMatrix=*/nullptr, /*isOpaque=*/true);
44 if (!shader) {
45 ERRORF(r, "%s: Unable to build shader", testFile);
46 return;
47 }
48
49 SkPaint paintShader;
50 paintShader.setShader(shader);
51 surface->getCanvas()->drawRect(kRect, paintShader);
52
53 SkBitmap bitmap;
54 REPORTER_ASSERT(r, bitmap.tryAllocPixels(surface->imageInfo()));
55 REPORTER_ASSERT(r, surface->readPixels(bitmap.info(), bitmap.getPixels(), bitmap.rowBytes(),
56 /*srcX=*/0, /*srcY=*/0));
57
58 SkColor color = bitmap.getColor(0, 0);
59 REPORTER_ASSERT(r, color == SkColorSetARGB(0xFF, 0x00, 0xFF, 0x00),
60 "Expected: solid green. Actual: A=%02X R=%02X G=%02X B=%02X.",
61 SkColorGetA(color), SkColorGetR(color), SkColorGetG(color), SkColorGetB(color));
62}
63
64static void test_cpu(skiatest::Reporter* r, const char* testFile) {
65 const SkImageInfo info = SkImageInfo::MakeN32Premul(kRect.width(), kRect.height());
66 sk_sp<SkSurface> surface(SkSurface::MakeRaster(info));
67
68 test(r, surface.get(), testFile);
69}
70
71static void test_gpu(skiatest::Reporter* r, GrDirectContext* ctx, const char* testFile) {
72 const SkImageInfo info = SkImageInfo::MakeN32Premul(kRect.width(), kRect.height());
73 sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info));
74
75 test(r, surface.get(), testFile);
76}
77
78#define SKSL_TEST(name, path) \
79 DEF_TEST(name ## _CPU, r) { \
80 test_cpu(r, path); \
81 } \
82 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(name ## _GPU, r, ctxInfo) { \
83 test_gpu(r, ctxInfo.directContext(), path); \
84 }
85
86SKSL_TEST(SkSLBoolFolding, "folding/BoolFolding.sksl")
87SKSL_TEST(SkSLIntFoldingES2, "folding/IntFoldingES2.sksl")
John Stiles053f7852021-01-25 13:04:12 -050088SKSL_TEST(SkSLFloatFolding, "folding/FloatFolding.sksl")
John Stiles232dd2b2021-01-25 10:57:47 -050089SKSL_TEST(SkSLMatrixFoldingES2, "folding/MatrixFoldingES2.sksl")
90SKSL_TEST(SkSLShortCircuitBoolFolding, "folding/ShortCircuitBoolFolding.sksl")
91SKSL_TEST(SkSLVectorScalarFolding, "folding/VectorScalarFolding.sksl")
92SKSL_TEST(SkSLVectorVectorFolding, "folding/VectorVectorFolding.sksl")
93
94/*
John Stiles232dd2b2021-01-25 10:57:47 -050095TODO(skia:11209): enable these tests when Runtime Effects have support for ES3
96
97SKSL_TEST(SkSLIntFoldingES3, "folding/IntFoldingES3.sksl")
98SKSL_TEST(SkSLMatrixFoldingES3, "folding/MatrixFoldingES3.sksl")
99*/