Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkSLCross.cpp
Go to the documentation of this file.
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
15#include "include/core/SkRect.h"
33#include "tests/Test.h"
34
35#include <memory>
36#include <utility>
37
38namespace skgpu { class KeyBuilder; }
39struct GrContextOptions;
40struct GrShaderCaps;
41
42static void run_test(skiatest::Reporter*,
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).
52 GrDirectContext* dContext = ctxInfo.directContext();
55 nullptr,
57 {1, 1},
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
70namespace {
71
72// Outputs:
73// Green if cross(a,b) > 0
74// Red if cross(a,b) < 0
75// Black if cross(a,b) == 0
76class VisualizeCrossProductSignFP : public GrFragmentProcessor {
77public:
78 VisualizeCrossProductSignFP(SkVector a, SkVector b)
79 : GrFragmentProcessor(kTestFP_ClassID, kPreservesOpaqueInput_OptimizationFlag)
80 , fA(a), fB(b) {
81 }
82
83 const char* name() const override { return "VisualizeCrossProductSignFP"; }
84
85 std::unique_ptr<GrFragmentProcessor> clone() const override {
86 return std::unique_ptr<GrFragmentProcessor>(new VisualizeCrossProductSignFP(fA, fB));
87 }
88
89private:
90 void onAddToKey(const GrShaderCaps&, skgpu::KeyBuilder*) const override {}
91 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
92
93 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 }
119 };
120
121 return std::make_unique<Impl>();
122 }
123 const SkVector fA, fB;
124};
125
126} // namespace
127
128static void run_test(skiatest::Reporter* reporter,
129 GrDirectContext* directContext,
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);
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
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}
reporter
static GrColor GrColorPackRGBA(unsigned r, unsigned g, unsigned b, unsigned a)
Definition GrColor.h:46
uint32_t GrColor
Definition GrColor.h:25
@ kFragment_GrShaderFlag
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
#define SkASSERT(cond)
Definition SkAssert.h:116
@ kSrcOver
r = s + (1-sa)*d
constexpr SkPMColor4f SK_PMColor4fWHITE
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition SkColorType.h:24
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
#define ERRORF(r,...)
Definition Test.h:293
#define DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(name, reporter, context_info, ctsEnforcement)
Definition Test.h:434
virtual void set2f(UniformHandle, float, float) const =0
GrGLSLProgramDataManager::UniformHandle UniformHandle
void setColorFragmentProcessor(std::unique_ptr< GrFragmentProcessor > fp)
Definition GrPaint.h:65
void setColor4f(const SkPMColor4f &color)
Definition GrPaint.h:50
void setPorterDuffXPFactory(SkBlendMode mode)
Definition GrPaint.cpp:28
const T & cast() const
static const SkMatrix & I()
bool readPixels(GrDirectContext *dContext, GrPixmap dst, SkIPoint srcPt)
static std::unique_ptr< SurfaceDrawContext > Make(GrRecordingContext *, GrColorType, sk_sp< GrSurfaceProxy >, sk_sp< SkColorSpace >, GrSurfaceOrigin, const SkSurfaceProps &)
void drawRect(const GrClip *, GrPaint &&paint, GrAA, const SkMatrix &viewMatrix, const SkRect &, const GrStyle *style=nullptr)
void clear(const SkIRect &rect, const SkRGBA4f< AlphaType > &color)
static bool b
struct MyStruct a[10]
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
GAsyncResult * result
const char * name
Definition fuchsia.cc:50
const uint32_t fp
void run_test(skiatest::Reporter *reporter, Context *context, SkISize surfaceSize, SkISize recordingSize, SkISize replayOffset, DrawCallback draw, const std::vector< Expectation > &expectations)
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
static SkRGBA4f FromBytes_RGBA(uint32_t color)
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609