xref: /aosp_15_r20/external/skia/tests/SkSLDebugTraceTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
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 "include/core/SkData.h"
9 #include "include/core/SkRefCnt.h"
10 #include "include/core/SkStream.h"
11 #include "src/sksl/SkSLPosition.h"
12 #include "src/sksl/ir/SkSLType.h"
13 #include "src/sksl/tracing/SkSLDebugTracePriv.h"
14 #include "tests/Test.h"
15 
16 #include <cstddef>
17 #include <cstdint>
18 #include <string>
19 #include <string_view>
20 #include <vector>
21 
DEF_TEST(DebugTracePrivSetSource,r)22 DEF_TEST(DebugTracePrivSetSource, r) {
23     SkSL::DebugTracePriv i;
24     i.setSource("DebugTracePriv::setSource unit test\n"
25                 "\t// first line\n"
26                 "\t// second line\n"
27                 "\t// third line");
28 
29     REPORTER_ASSERT(r, i.fSource.size() == 4);
30     REPORTER_ASSERT(r, i.fSource[0] == "DebugTracePriv::setSource unit test");
31     REPORTER_ASSERT(r, i.fSource[1] == "\t// first line");
32     REPORTER_ASSERT(r, i.fSource[2] == "\t// second line");
33     REPORTER_ASSERT(r, i.fSource[3] == "\t// third line");
34 }
35 
DEF_TEST(DebugTracePrivSetSourceReplacesExistingText,r)36 DEF_TEST(DebugTracePrivSetSourceReplacesExistingText, r) {
37     SkSL::DebugTracePriv i;
38     i.setSource("One");
39     i.setSource("Two");
40     i.setSource("Three");
41 
42     REPORTER_ASSERT(r, i.fSource.size() == 1);
43     REPORTER_ASSERT(r, i.fSource[0] == "Three");
44 }
45 
DEF_TEST(DebugTracePrivWrite,r)46 DEF_TEST(DebugTracePrivWrite, r) {
47     SkSL::DebugTracePriv i;
48     i.fSource = {
49         "\t// first line",
50         "// \"second line\"",
51         "//\\\\//\\\\ third line",
52     };
53     i.fSlotInfo = {
54         {"SkSL_DebugTrace", 1, 2, 3, 4, (SkSL::Type::NumberKind)5,  6,  SkSL::Position{}, -1},
55         {"Unit_Test",       6, 7, 8, 8, (SkSL::Type::NumberKind)10, 11, SkSL::Position{}, 12},
56     };
57     i.fFuncInfo = {
58         {"void testFunc();"},
59     };
60     i.fTraceInfo = {
61         {SkSL::TraceInfo::Op::kEnter, {0, 0}},
62         {SkSL::TraceInfo::Op::kLine,  {5, 0}},
63         {SkSL::TraceInfo::Op::kVar,   {10, 15}},
64         {SkSL::TraceInfo::Op::kExit,  {20, 0}},
65     };
66     SkDynamicMemoryWStream wstream;
67     i.writeTrace(&wstream);
68     sk_sp<SkData> trace = wstream.detachAsData();
69 
70     static constexpr char kExpected[] =
71             R"({"version":"20220209","source":["\t// first line","// \"second line\"","//\\\\//\\)"
72             R"(\\ third line"],"slots":[{"name":"SkSL_DebugTrace","columns":1,"rows":2,"index":3,)"
73             R"("groupIdx":4,"kind":5,"line":6},{"name":"Unit_Test","columns":6,"rows":7,"index":8)"
74             R"(,"kind":10,"line":11,"retval":12}],"functions":[{"name":"void testFunc();"}],"trac)"
75             R"(e":[[2],[0,5],[1,10,15],[3,20]]})";
76 
77     std::string_view actual{reinterpret_cast<const char*>(trace->bytes()), trace->size()};
78 
79     REPORTER_ASSERT(r, actual == kExpected,
80                     "Expected:\n    %s\n\n  Actual:\n    %.*s\n",
81                     kExpected, (int)actual.size(), actual.data());
82 }
83 
DEF_TEST(DebugTracePrivRead,r)84 DEF_TEST(DebugTracePrivRead, r) {
85     const std::string_view kJSONTrace =
86             R"({"version":"20220209","source":["\t// first line","// \"second line\"","//\\\\//\\)"
87             R"(\\ third line"],"slots":[{"name":"SkSL_DebugTrace","columns":1,"rows":2,"index":3,)"
88             R"("groupIdx":4,"kind":5,"line":6},{"name":"Unit_Test","columns":6,"rows":7,"index":8)"
89             R"(,"kind":10,"line":11,"retval":12}],"functions":[{"name":"void testFunc();"}],"trac)"
90             R"(e":[[2],[0,5],[1,10,15],[3,20]]})";
91 
92     SkMemoryStream stream(kJSONTrace.data(), kJSONTrace.size(), /*copyData=*/false);
93     SkSL::DebugTracePriv i;
94     REPORTER_ASSERT(r, i.readTrace(&stream));
95 
96     REPORTER_ASSERT(r, i.fSource.size() == 3);
97     REPORTER_ASSERT(r, i.fSlotInfo.size() == 2);
98     REPORTER_ASSERT(r, i.fFuncInfo.size() == 1);
99     REPORTER_ASSERT(r, i.fTraceInfo.size() == 4);
100 
101     REPORTER_ASSERT(r, i.fSource[0] == "\t// first line");
102     REPORTER_ASSERT(r, i.fSource[1] == "// \"second line\"");
103     REPORTER_ASSERT(r, i.fSource[2] == "//\\\\//\\\\ third line");
104 
105     REPORTER_ASSERT(r, i.fSlotInfo[0].name == "SkSL_DebugTrace");
106     REPORTER_ASSERT(r, i.fSlotInfo[0].columns == 1);
107     REPORTER_ASSERT(r, i.fSlotInfo[0].rows == 2);
108     REPORTER_ASSERT(r, i.fSlotInfo[0].componentIndex == 3);
109     REPORTER_ASSERT(r, i.fSlotInfo[0].groupIndex == 4);
110     REPORTER_ASSERT(r, i.fSlotInfo[0].numberKind == (SkSL::Type::NumberKind)5);
111     REPORTER_ASSERT(r, i.fSlotInfo[0].line == 6);
112     REPORTER_ASSERT(r, i.fSlotInfo[0].fnReturnValue == -1);
113 
114     REPORTER_ASSERT(r, i.fSlotInfo[1].name == "Unit_Test");
115     REPORTER_ASSERT(r, i.fSlotInfo[1].columns == 6);
116     REPORTER_ASSERT(r, i.fSlotInfo[1].rows == 7);
117     REPORTER_ASSERT(r, i.fSlotInfo[1].componentIndex == 8);
118     REPORTER_ASSERT(r, i.fSlotInfo[1].groupIndex == 8);
119     REPORTER_ASSERT(r, i.fSlotInfo[1].numberKind == (SkSL::Type::NumberKind)10);
120     REPORTER_ASSERT(r, i.fSlotInfo[1].line == 11);
121     REPORTER_ASSERT(r, i.fSlotInfo[1].fnReturnValue == 12);
122 
123     REPORTER_ASSERT(r, i.fFuncInfo[0].name == "void testFunc();");
124 
125     REPORTER_ASSERT(r, i.fTraceInfo[0].op == SkSL::TraceInfo::Op::kEnter);
126     REPORTER_ASSERT(r, i.fTraceInfo[0].data[0] == 0);
127     REPORTER_ASSERT(r, i.fTraceInfo[0].data[1] == 0);
128 
129     REPORTER_ASSERT(r, i.fTraceInfo[1].op == SkSL::TraceInfo::Op::kLine);
130     REPORTER_ASSERT(r, i.fTraceInfo[1].data[0] == 5);
131     REPORTER_ASSERT(r, i.fTraceInfo[1].data[1] == 0);
132 
133     REPORTER_ASSERT(r, i.fTraceInfo[2].op == SkSL::TraceInfo::Op::kVar);
134     REPORTER_ASSERT(r, i.fTraceInfo[2].data[0] == 10);
135     REPORTER_ASSERT(r, i.fTraceInfo[2].data[1] == 15);
136 
137     REPORTER_ASSERT(r, i.fTraceInfo[3].op == SkSL::TraceInfo::Op::kExit);
138     REPORTER_ASSERT(r, i.fTraceInfo[3].data[0] == 20);
139     REPORTER_ASSERT(r, i.fTraceInfo[3].data[1] == 0);
140 }
141 
DEF_TEST(DebugTracePrivGetSlotComponentSuffix,r)142 DEF_TEST(DebugTracePrivGetSlotComponentSuffix, r) {
143     // SlotDebugInfo fields:
144     // - name
145     // - columns
146     // - rows
147     // - componentIndex
148     // - numberKind
149     // - line
150     // - fnReturnValue
151 
152     SkSL::DebugTracePriv i;
153     i.fSlotInfo = {{"s", 1, 1, 0,  0,  SkSL::Type::NumberKind::kFloat, 0, SkSL::Position{}, -1},
154                    {"v", 4, 1, 0,  0,  SkSL::Type::NumberKind::kFloat, 0, SkSL::Position{}, -1},
155                    {"v", 4, 1, 1,  1,  SkSL::Type::NumberKind::kFloat, 0, SkSL::Position{}, -1},
156                    {"v", 4, 1, 2,  2,  SkSL::Type::NumberKind::kFloat, 0, SkSL::Position{}, -1},
157                    {"v", 4, 1, 3,  3,  SkSL::Type::NumberKind::kFloat, 0, SkSL::Position{}, -1},
158                    {"m", 4, 4, 0,  0,  SkSL::Type::NumberKind::kFloat, 0, SkSL::Position{}, -1},
159                    {"m", 4, 4, 1,  1,  SkSL::Type::NumberKind::kFloat, 0, SkSL::Position{}, -1},
160                    {"m", 4, 4, 2,  2,  SkSL::Type::NumberKind::kFloat, 0, SkSL::Position{}, -1},
161                    {"m", 4, 4, 3,  3,  SkSL::Type::NumberKind::kFloat, 0, SkSL::Position{}, -1},
162                    {"m", 4, 4, 4,  4,  SkSL::Type::NumberKind::kFloat, 0, SkSL::Position{}, -1},
163                    {"m", 4, 4, 5,  5,  SkSL::Type::NumberKind::kFloat, 0, SkSL::Position{}, -1},
164                    {"m", 4, 4, 6,  6,  SkSL::Type::NumberKind::kFloat, 0, SkSL::Position{}, -1},
165                    {"m", 4, 4, 7,  7,  SkSL::Type::NumberKind::kFloat, 0, SkSL::Position{}, -1},
166                    {"m", 4, 4, 8,  8,  SkSL::Type::NumberKind::kFloat, 0, SkSL::Position{}, -1},
167                    {"m", 4, 4, 9,  9,  SkSL::Type::NumberKind::kFloat, 0, SkSL::Position{}, -1},
168                    {"m", 4, 4, 10, 10, SkSL::Type::NumberKind::kFloat, 0, SkSL::Position{}, -1},
169                    {"m", 4, 4, 11, 11, SkSL::Type::NumberKind::kFloat, 0, SkSL::Position{}, -1},
170                    {"m", 4, 4, 12, 12, SkSL::Type::NumberKind::kFloat, 0, SkSL::Position{}, -1},
171                    {"m", 4, 4, 13, 13, SkSL::Type::NumberKind::kFloat, 0, SkSL::Position{}, -1},
172                    {"m", 4, 4, 14, 14, SkSL::Type::NumberKind::kFloat, 0, SkSL::Position{}, -1},
173                    {"m", 4, 4, 15, 15, SkSL::Type::NumberKind::kFloat, 0, SkSL::Position{}, -1}};
174 
175     const std::string kExpected[] = {"",
176                                      ".x",     ".y",     ".z",     ".w",
177                                      "[0][0]", "[0][1]", "[0][2]", "[0][3]",
178                                      "[1][0]", "[1][1]", "[1][2]", "[1][3]",
179                                      "[2][0]", "[2][1]", "[2][2]", "[2][3]",
180                                      "[3][0]", "[3][1]", "[3][2]", "[3][3]"};
181 
182     REPORTER_ASSERT(r, i.fSlotInfo.size() == std::size(kExpected));
183     for (size_t index = 0; index < std::size(kExpected); ++index) {
184         REPORTER_ASSERT(r, kExpected[index] == i.getSlotComponentSuffix(index));
185     }
186 }
187