xref: /aosp_15_r20/external/skia/tests/SkSLCross.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 "include/core/SkAlphaType.h"
9 #include "include/core/SkBlendMode.h"
10 #include "include/core/SkColorSpace.h"
11 #include "include/core/SkColorType.h"
12 #include "include/core/SkImageInfo.h"
13 #include "include/core/SkMatrix.h"
14 #include "include/core/SkPoint.h"
15 #include "include/core/SkRect.h"
16 #include "include/core/SkSurfaceProps.h"
17 #include "include/core/SkTypes.h"
18 #include "include/gpu/ganesh/GrDirectContext.h"
19 #include "include/private/SkColorData.h"
20 #include "include/private/gpu/ganesh/GrTypesPriv.h"
21 #include "src/core/SkSLTypeShared.h"
22 #include "src/gpu/SkBackingFit.h"
23 #include "src/gpu/ganesh/GrColor.h"
24 #include "src/gpu/ganesh/GrFragmentProcessor.h"
25 #include "src/gpu/ganesh/GrImageInfo.h"
26 #include "src/gpu/ganesh/GrPaint.h"
27 #include "src/gpu/ganesh/GrPixmap.h"
28 #include "src/gpu/ganesh/SurfaceDrawContext.h"
29 #include "src/gpu/ganesh/glsl/GrGLSLFragmentShaderBuilder.h"
30 #include "src/gpu/ganesh/glsl/GrGLSLProgramDataManager.h"
31 #include "src/gpu/ganesh/glsl/GrGLSLUniformHandler.h"
32 #include "tests/CtsEnforcement.h"
33 #include "tests/Test.h"
34 
35 #include <memory>
36 #include <utility>
37 
38 namespace skgpu { class KeyBuilder; }
39 struct GrContextOptions;
40 struct GrShaderCaps;
41 
42 static void run_test(skiatest::Reporter*,
43                      GrDirectContext*,
44                      skgpu::ganesh::SurfaceDrawContext*,
45                      SkVector a,
46                      SkVector b,
47                      float expectedCrossProduct);
48 
49 // This is a GPU test that ensures the SkSL 2d cross() intrinsic returns the correct sign (negative,
50 // positive, or zero).
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(SkSLCross,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)51 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(SkSLCross, reporter, ctxInfo, CtsEnforcement::kApiLevel_T) {
52     GrDirectContext* dContext = ctxInfo.directContext();
53     auto sdc = skgpu::ganesh::SurfaceDrawContext::Make(dContext,
54                                                        GrColorType::kRGBA_8888,
55                                                        nullptr,
56                                                        SkBackingFit::kExact,
57                                                        {1, 1},
58                                                        SkSurfaceProps(),
59                                                        /*label=*/"SkSLCross_Test");
60     if (!sdc) {
61         ERRORF(reporter, "could not create render target context.");
62         return;
63     }
64     run_test(reporter, dContext, sdc.get(), {3,4}, {5,6}, -2);  // Negative.
65     run_test(reporter, dContext, sdc.get(), {3,4}, {-5,-6}, 2);  // Positive.
66     run_test(reporter, dContext, sdc.get(), {0, 2.287f}, {0, -7.741f}, 0);  // Zero.
67     run_test(reporter, dContext, sdc.get(), {62.17f, 0}, {-43.49f, 0}, 0);  // Zero.
68 }
69 
70 namespace {
71 
72 // Outputs:
73 //     Green if cross(a,b) > 0
74 //     Red if cross(a,b) < 0
75 //     Black if cross(a,b) == 0
76 class VisualizeCrossProductSignFP : public GrFragmentProcessor {
77 public:
VisualizeCrossProductSignFP(SkVector a,SkVector b)78     VisualizeCrossProductSignFP(SkVector a, SkVector b)
79             : GrFragmentProcessor(kTestFP_ClassID, kPreservesOpaqueInput_OptimizationFlag)
80             , fA(a), fB(b) {
81     }
82 
name() const83     const char* name() const override { return "VisualizeCrossProductSignFP"; }
84 
clone() const85     std::unique_ptr<GrFragmentProcessor> clone() const override {
86         return std::unique_ptr<GrFragmentProcessor>(new VisualizeCrossProductSignFP(fA, fB));
87     }
88 
89 private:
onAddToKey(const GrShaderCaps &,skgpu::KeyBuilder *) const90     void onAddToKey(const GrShaderCaps&, skgpu::KeyBuilder*) const override {}
onIsEqual(const GrFragmentProcessor &) const91     bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
92 
onMakeProgramImpl() const93     std::unique_ptr<ProgramImpl> onMakeProgramImpl() const override {
94         class Impl : public ProgramImpl {
95         public:
96             void emitCode(EmitArgs& args) override {
97                 auto& fp = args.fFp.cast<VisualizeCrossProductSignFP>();
98                 const char *a, *b;
99                 fAUniform = args.fUniformHandler->addUniform(&fp, kFragment_GrShaderFlag,
100                                                              SkSLType::kFloat2, "a", &a);
101                 fBUniform = args.fUniformHandler->addUniform(&fp, kFragment_GrShaderFlag,
102                                                              SkSLType::kFloat2, "b", &b);
103                 args.fFragBuilder->codeAppendf(R"(
104                     float crossProduct = cross_length_2d(%s, %s);
105                     float2 visualization = clamp(float2(-sign(crossProduct), sign(crossProduct)),
106                                                  float2(0), float2(1));
107                 return half2(visualization).xy01;)", a, b);
108             }
109 
110         private:
111             void onSetData(const GrGLSLProgramDataManager& pdman,
112                            const GrFragmentProcessor& processor) override {
113                 const auto& fp = processor.cast<VisualizeCrossProductSignFP>();
114                 pdman.set2f(fAUniform, fp.fA.x(), fp.fA.y());
115                 pdman.set2f(fBUniform, fp.fB.x(), fp.fB.y());
116             }
117             GrGLSLUniformHandler::UniformHandle fAUniform;
118             GrGLSLUniformHandler::UniformHandle fBUniform;
119         };
120 
121         return std::make_unique<Impl>();
122     }
123     const SkVector fA, fB;
124 };
125 
126 }  // namespace
127 
run_test(skiatest::Reporter * reporter,GrDirectContext * directContext,skgpu::ganesh::SurfaceDrawContext * sdc,SkVector a,SkVector b,float expectedCrossProduct)128 static void run_test(skiatest::Reporter* reporter,
129                      GrDirectContext* directContext,
130                      skgpu::ganesh::SurfaceDrawContext* sdc,
131                      SkVector a,
132                      SkVector b,
133                      float expectedCrossProduct) {
134     SkASSERT(sdc->width() == 1);
135     SkASSERT(sdc->height() == 1);
136 
137     sdc->clear(SkPMColor4f::FromBytes_RGBA(0xbaaaaaad));
138 
139     GrPaint crossPaint;
140     crossPaint.setColor4f(SK_PMColor4fWHITE);
141     crossPaint.setPorterDuffXPFactory(SkBlendMode::kSrcOver);
142     crossPaint.setColorFragmentProcessor(std::make_unique<VisualizeCrossProductSignFP>(a, b));
143     sdc->drawRect(/*clip=*/nullptr, std::move(crossPaint), GrAA::kNo, SkMatrix::I(),
144                   SkRect::MakeWH(1,1));
145 
146     GrColor result;
147     GrPixmap resultPM(SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kPremul_SkAlphaType),
148                       &result,
149                       sizeof(GrColor));
150     sdc->readPixels(directContext, resultPM, {0, 0});
151 
152     SkASSERT(expectedCrossProduct == a.cross(b));
153     if (expectedCrossProduct > 0) {
154         REPORTER_ASSERT(reporter, result == GrColorPackRGBA(0, 255, 0, 255));  // Green.
155     } else if (expectedCrossProduct < 0) {
156         REPORTER_ASSERT(reporter, result == GrColorPackRGBA(255, 0, 0, 255));  // Red.
157     } else {
158         REPORTER_ASSERT(reporter, result == GrColorPackRGBA(0, 0, 0, 255));  // Black.
159     }
160 }
161