• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/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*, GrDirectContext*,
43                      skgpu::v1::SurfaceDrawContext*, SkVector a,
44                      SkVector b, float expectedCrossProduct);
45 
46 // This is a GPU test that ensures the SkSL 2d cross() intrinsic returns the correct sign (negative,
47 // positive, or zero).
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(SkSLCross,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)48 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(SkSLCross, reporter, ctxInfo, CtsEnforcement::kApiLevel_T) {
49     GrDirectContext* dContext = ctxInfo.directContext();
50     auto sdc = skgpu::v1::SurfaceDrawContext::Make(dContext, GrColorType::kRGBA_8888, nullptr,
51                                                    SkBackingFit::kExact, {1, 1}, SkSurfaceProps(),
52                                                    /*label=*/"SkSLCross_Test");
53     if (!sdc) {
54         ERRORF(reporter, "could not create render target context.");
55         return;
56     }
57     run_test(reporter, dContext, sdc.get(), {3,4}, {5,6}, -2);  // Negative.
58     run_test(reporter, dContext, sdc.get(), {3,4}, {-5,-6}, 2);  // Positive.
59     run_test(reporter, dContext, sdc.get(), {0, 2.287f}, {0, -7.741f}, 0);  // Zero.
60     run_test(reporter, dContext, sdc.get(), {62.17f, 0}, {-43.49f, 0}, 0);  // Zero.
61 }
62 
63 namespace {
64 
65 // Outputs:
66 //     Green if cross(a,b) > 0
67 //     Red if cross(a,b) < 0
68 //     Black if cross(a,b) == 0
69 class VisualizeCrossProductSignFP : public GrFragmentProcessor {
70 public:
VisualizeCrossProductSignFP(SkVector a,SkVector b)71     VisualizeCrossProductSignFP(SkVector a, SkVector b)
72             : GrFragmentProcessor(kTestFP_ClassID, kPreservesOpaqueInput_OptimizationFlag)
73             , fA(a), fB(b) {
74     }
75 
name() const76     const char* name() const override { return "VisualizeCrossProductSignFP"; }
77 
clone() const78     std::unique_ptr<GrFragmentProcessor> clone() const override {
79         return std::unique_ptr<GrFragmentProcessor>(new VisualizeCrossProductSignFP(fA, fB));
80     }
81 
82 private:
onAddToKey(const GrShaderCaps &,skgpu::KeyBuilder *) const83     void onAddToKey(const GrShaderCaps&, skgpu::KeyBuilder*) const override {}
onIsEqual(const GrFragmentProcessor &) const84     bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
85 
onMakeProgramImpl() const86     std::unique_ptr<ProgramImpl> onMakeProgramImpl() const override {
87         class Impl : public ProgramImpl {
88         public:
89             void emitCode(EmitArgs& args) override {
90                 auto& fp = args.fFp.cast<VisualizeCrossProductSignFP>();
91                 const char *a, *b;
92                 fAUniform = args.fUniformHandler->addUniform(&fp, kFragment_GrShaderFlag,
93                                                              SkSLType::kFloat2, "a", &a);
94                 fBUniform = args.fUniformHandler->addUniform(&fp, kFragment_GrShaderFlag,
95                                                              SkSLType::kFloat2, "b", &b);
96                 args.fFragBuilder->codeAppendf(R"(
97                     float crossProduct = cross_length_2d(%s, %s);
98                     float2 visualization = clamp(float2(-sign(crossProduct), sign(crossProduct)),
99                                                  float2(0), float2(1));
100                 return half2(visualization).xy01;)", a, b);
101             }
102 
103         private:
104             void onSetData(const GrGLSLProgramDataManager& pdman,
105                            const GrFragmentProcessor& processor) override {
106                 const auto& fp = processor.cast<VisualizeCrossProductSignFP>();
107                 pdman.set2f(fAUniform, fp.fA.x(), fp.fA.y());
108                 pdman.set2f(fBUniform, fp.fB.x(), fp.fB.y());
109             }
110             GrGLSLUniformHandler::UniformHandle fAUniform;
111             GrGLSLUniformHandler::UniformHandle fBUniform;
112         };
113 
114         return std::make_unique<Impl>();
115     }
116     const SkVector fA, fB;
117 };
118 
119 }  // namespace
120 
run_test(skiatest::Reporter * reporter,GrDirectContext * directContext,skgpu::v1::SurfaceDrawContext * sdc,SkVector a,SkVector b,float expectedCrossProduct)121 static void run_test(skiatest::Reporter* reporter, GrDirectContext* directContext,
122                      skgpu::v1::SurfaceDrawContext* sdc, SkVector a, SkVector b,
123                      float expectedCrossProduct) {
124     SkASSERT(sdc->width() == 1);
125     SkASSERT(sdc->height() == 1);
126 
127     sdc->clear(SkPMColor4f::FromBytes_RGBA(0xbaaaaaad));
128 
129     GrPaint crossPaint;
130     crossPaint.setColor4f(SK_PMColor4fWHITE);
131     crossPaint.setPorterDuffXPFactory(SkBlendMode::kSrcOver);
132     crossPaint.setColorFragmentProcessor(std::make_unique<VisualizeCrossProductSignFP>(a, b));
133     sdc->drawRect(/*clip=*/nullptr, std::move(crossPaint), GrAA::kNo, SkMatrix::I(),
134                   SkRect::MakeWH(1,1));
135 
136     GrColor result;
137     GrPixmap resultPM(SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kPremul_SkAlphaType),
138                       &result,
139                       sizeof(GrColor));
140     sdc->readPixels(directContext, resultPM, {0, 0});
141 
142     SkASSERT(expectedCrossProduct == a.cross(b));
143     if (expectedCrossProduct > 0) {
144         REPORTER_ASSERT(reporter, result == GrColorPackRGBA(0, 255, 0, 255));  // Green.
145     } else if (expectedCrossProduct < 0) {
146         REPORTER_ASSERT(reporter, result == GrColorPackRGBA(255, 0, 0, 255));  // Red.
147     } else {
148         REPORTER_ASSERT(reporter, result == GrColorPackRGBA(0, 0, 0, 255));  // Black.
149     }
150 }
151