1 /*
2  * Copyright 2020 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 "tests/Test.h"
9 
10 #include "src/gpu/GrSurfaceDrawContext.h"
11 #include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
12 
13 static void run_test(skiatest::Reporter*, GrDirectContext*,
14                      GrSurfaceDrawContext*, SkVector a,
15                      SkVector b, float expectedCrossProduct);
16 
17 // This is a GPU test that ensures the SkSL 2d cross() intrinsic returns the correct sign (negative,
18 // positive, or zero).
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkSLCross,reporter,ctxInfo)19 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkSLCross, reporter, ctxInfo) {
20     GrDirectContext* directContext = ctxInfo.directContext();
21     auto rtc = GrSurfaceDrawContext::Make(directContext, GrColorType::kRGBA_8888, nullptr,
22                                           SkBackingFit::kExact, {1, 1}, SkSurfaceProps());
23     if (!rtc) {
24         ERRORF(reporter, "could not create render target context.");
25         return;
26     }
27     run_test(reporter, directContext, rtc.get(), {3,4}, {5,6}, -2);  // Negative.
28     run_test(reporter, directContext, rtc.get(), {3,4}, {-5,-6}, 2);  // Positive.
29     run_test(reporter, directContext, rtc.get(), {0, 2.287f}, {0, -7.741f}, 0);  // Zero.
30     run_test(reporter, directContext, rtc.get(), {62.17f, 0}, {-43.49f, 0}, 0);  // Zero.
31 }
32 
33 namespace {
34 
35 // Outputs:
36 //     Green if cross(a,b) > 0
37 //     Red if cross(a,b) < 0
38 //     Black if cross(a,b) == 0
39 class VisualizeCrossProductSignFP : public GrFragmentProcessor {
40 public:
VisualizeCrossProductSignFP(SkVector a,SkVector b)41     VisualizeCrossProductSignFP(SkVector a, SkVector b)
42             : GrFragmentProcessor(kTestFP_ClassID, kPreservesOpaqueInput_OptimizationFlag)
43             , fA(a), fB(b) {
44     }
45 
46 private:
name() const47     const char* name() const override { return "VisualizeCrossProductSignFP"; }
clone() const48     std::unique_ptr<GrFragmentProcessor> clone() const override {
49         return std::unique_ptr<GrFragmentProcessor>(new VisualizeCrossProductSignFP(fA, fB));
50     }
onGetGLSLProcessorKey(const GrShaderCaps &,GrProcessorKeyBuilder *) const51     void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
onIsEqual(const GrFragmentProcessor &) const52     bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
53 
54     class Impl : public GrGLSLFragmentProcessor {
emitCode(EmitArgs & args)55         void emitCode(EmitArgs& args) override {
56             auto& fp = args.fFp.cast<VisualizeCrossProductSignFP>();
57             const char* a, *b;
58             fAUniform = args.fUniformHandler->addUniform(&fp, kFragment_GrShaderFlag,
59                                                          GrSLType::kFloat2_GrSLType, "a", &a);
60             fBUniform = args.fUniformHandler->addUniform(&fp, kFragment_GrShaderFlag,
61                                                          GrSLType::kFloat2_GrSLType, "b", &b);
62             args.fFragBuilder->codeAppendf(R"(
63                     float crossProduct = cross(%s, %s);
64                     float2 visualization = clamp(float2(-sign(crossProduct), sign(crossProduct)),
65                                                  float2(0), float2(1));
66                     return half2(visualization).xy01;)", a, b);
67         }
onSetData(const GrGLSLProgramDataManager & pdman,const GrFragmentProcessor & processor)68         void onSetData(const GrGLSLProgramDataManager& pdman,
69                        const GrFragmentProcessor& processor) override {
70             const auto& fp = processor.cast<VisualizeCrossProductSignFP>();
71             pdman.set2f(fAUniform, fp.fA.x(), fp.fA.y());
72             pdman.set2f(fBUniform, fp.fB.x(), fp.fB.y());
73         }
74         GrGLSLUniformHandler::UniformHandle fAUniform;
75         GrGLSLUniformHandler::UniformHandle fBUniform;
76     };
77 
onMakeProgramImpl() const78     std::unique_ptr<GrGLSLFragmentProcessor> onMakeProgramImpl() const override {
79         return std::make_unique<Impl>();
80     }
81     const SkVector fA, fB;
82 };
83 
84 }  // namespace
85 
run_test(skiatest::Reporter * reporter,GrDirectContext * directContext,GrSurfaceDrawContext * rtc,SkVector a,SkVector b,float expectedCrossProduct)86 static void run_test(skiatest::Reporter* reporter, GrDirectContext* directContext,
87                      GrSurfaceDrawContext* rtc, SkVector a, SkVector b,
88                      float expectedCrossProduct) {
89     SkASSERT(rtc->width() == 1);
90     SkASSERT(rtc->height() == 1);
91 
92     rtc->clear(SkPMColor4f::FromBytes_RGBA(0xbaaaaaad));
93 
94     GrPaint crossPaint;
95     crossPaint.setColor4f(SK_PMColor4fWHITE);
96     crossPaint.setPorterDuffXPFactory(SkBlendMode::kSrcOver);
97     crossPaint.setColorFragmentProcessor(std::make_unique<VisualizeCrossProductSignFP>(a, b));
98     rtc->drawRect(/*clip=*/nullptr, std::move(crossPaint), GrAA::kNo, SkMatrix::I(),
99                   SkRect::MakeWH(1,1));
100 
101     GrColor result;
102     GrPixmap resultPM(SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kPremul_SkAlphaType),
103                       &result,
104                       sizeof(GrColor));
105     rtc->readPixels(directContext, resultPM, {0, 0});
106 
107     SkASSERT(expectedCrossProduct == a.cross(b));
108     if (expectedCrossProduct > 0) {
109         REPORTER_ASSERT(reporter, result == GrColorPackRGBA(0, 255, 0, 255));  // Green.
110     } else if (expectedCrossProduct < 0) {
111         REPORTER_ASSERT(reporter, result == GrColorPackRGBA(255, 0, 0, 255));  // Red.
112     } else {
113         REPORTER_ASSERT(reporter, result == GrColorPackRGBA(0, 0, 0, 255));  // Black.
114     }
115 }
116