• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef SKVMDEBUGTRACE
9 #define SKVMDEBUGTRACE
10 
11 #include "include/core/SkPoint.h"
12 #include "include/sksl/SkSLDebugTrace.h"
13 #include "src/core/SkVM.h"
14 #include "src/sksl/ir/SkSLType.h"
15 
16 #include <string>
17 #include <vector>
18 
19 class SkStream;
20 class SkWStream;
21 
22 namespace SkSL {
23 
24 struct SkVMSlotInfo {
25     /** The full name of this variable (without component): (e.g. `myArray[3].myStruct.myVector`) */
26     std::string             name;
27     /** The dimensions of this variable: 1x1 is a scalar, Nx1 is a vector, NxM is a matrix. */
28     uint8_t                 columns = 1, rows = 1;
29     /** Which component of the variable is this slot? (e.g. `vec4.z` is component 2) */
30     uint8_t                 componentIndex = 0;
31     /** Complex types (arrays/structs) can be tracked as a "group" of adjacent slots. */
32     int                     groupIndex = 0;
33     /** What kind of numbers belong in this slot? */
34     SkSL::Type::NumberKind  numberKind = SkSL::Type::NumberKind::kNonnumeric;
35     /** Where is this variable located in the program? */
36     int                     line;
37     /** If this slot holds a function's return value, its FunctionInfo index; if not, -1. */
38     int                     fnReturnValue;
39 };
40 
41 struct SkVMFunctionInfo {
42     /** Full function declaration: `float myFunction(half4 color)`) */
43     std::string             name;
44 };
45 
46 struct SkVMTraceInfo {
47     enum class Op {
48         kLine,  /** data: line number, (unused) */
49         kVar,   /** data: slot, value */
50         kEnter, /** data: function index, (unused) */
51         kExit,  /** data: function index, (unused) */
52         kScope, /** data: scope delta, (unused) */
53     };
54     Op op;
55     int32_t data[2];
56 };
57 
58 class SkVMDebugTrace : public DebugTrace {
59 public:
60     /**
61      * Sets the device-coordinate pixel to trace. If it's not set, the point at (0, 0) will be used.
62      */
63     void setTraceCoord(const SkIPoint& coord);
64 
65     /** Attaches the SkSL source to be debugged. */
66     void setSource(std::string source);
67 
68     /** Serializes a debug trace to JSON which can be parsed by our debugger. */
69     bool readTrace(SkStream* r);
70     void writeTrace(SkWStream* w) const override;
71 
72     /** Generates a human-readable dump of the debug trace. */
73     void dump(SkWStream* o) const override;
74 
75     /** Returns a slot's component as a variable-name suffix, e.g. ".x" or "[2][2]". */
76     std::string getSlotComponentSuffix(int slotIndex) const;
77 
78     /** Bit-casts a slot's value, then converts to text, e.g. "3.14" or "true" or "12345". */
79     std::string getSlotValue(int slotIndex, int32_t value) const;
80 
81     /** Bit-casts a value for a given slot into a double, honoring the slot's NumberKind. */
82     double interpretValueBits(int slotIndex, int32_t valueBits) const;
83 
84     /** Converts a numeric value into text, based on the slot's NumberKind. */
85     std::string slotValueToString(int slotIndex, double value) const;
86 
87     /** The device-coordinate pixel to trace (controlled by setTraceCoord) */
88     SkIPoint fTraceCoord = {};
89 
90     /** A 1:1 mapping of slot numbers to debug information. */
91     std::vector<SkVMSlotInfo> fSlotInfo;
92     std::vector<SkVMFunctionInfo> fFuncInfo;
93 
94     /** The SkSL debug trace. */
95     std::vector<SkVMTraceInfo> fTraceInfo;
96 
97     /** The SkSL code, split line-by-line. */
98     std::vector<std::string> fSource;
99 
100     /**
101      * A trace hook which populates fTraceInfo during SkVM program evaluation. This will be created
102      * automatically by the SkSLVMCodeGenerator.
103      */
104     std::unique_ptr<skvm::TraceHook> fTraceHook;
105 };
106 
107 }  // namespace SkSL
108 
109 #endif
110