• 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/sksl/SkSLDebugTrace.h"
12 
13 #include "include/core/SkPoint.h"
14 #include "src/core/SkVM.h"
15 #include "src/sksl/tracing/SkSLDebugInfo.h"
16 
17 #include <cstdint>
18 #include <memory>
19 #include <string>
20 #include <vector>
21 
22 class SkStream;
23 class SkWStream;
24 
25 namespace SkSL {
26 
27 struct SkVMTraceInfo {
28     enum class Op {
29         kLine,  /** data: line number, (unused) */
30         kVar,   /** data: slot, value */
31         kEnter, /** data: function index, (unused) */
32         kExit,  /** data: function index, (unused) */
33         kScope, /** data: scope delta, (unused) */
34     };
35     Op op;
36     int32_t data[2];
37 };
38 
39 class SkVMDebugTrace : public DebugTrace {
40 public:
41     /**
42      * Sets the device-coordinate pixel to trace. If it's not set, the point at (0, 0) will be used.
43      */
44     void setTraceCoord(const SkIPoint& coord);
45 
46     /** Attaches the SkSL source to be debugged. */
47     void setSource(std::string source);
48 
49     /** Serializes a debug trace to JSON which can be parsed by our debugger. */
50     bool readTrace(SkStream* r);
51     void writeTrace(SkWStream* w) const override;
52 
53     /** Generates a human-readable dump of the debug trace. */
54     void dump(SkWStream* o) const override;
55 
56     /** Returns a slot's component as a variable-name suffix, e.g. ".x" or "[2][2]". */
57     std::string getSlotComponentSuffix(int slotIndex) const;
58 
59     /** Bit-casts a slot's value, then converts to text, e.g. "3.14" or "true" or "12345". */
60     std::string getSlotValue(int slotIndex, int32_t value) const;
61 
62     /** Bit-casts a value for a given slot into a double, honoring the slot's NumberKind. */
63     double interpretValueBits(int slotIndex, int32_t valueBits) const;
64 
65     /** Converts a numeric value into text, based on the slot's NumberKind. */
66     std::string slotValueToString(int slotIndex, double value) const;
67 
68     /** The device-coordinate pixel to trace (controlled by setTraceCoord) */
69     SkIPoint fTraceCoord = {};
70 
71     /** A 1:1 mapping of slot numbers to debug information. */
72     std::vector<SlotDebugInfo> fSlotInfo;
73     std::vector<FunctionDebugInfo> fFuncInfo;
74 
75     /** The SkSL debug trace. */
76     std::vector<SkVMTraceInfo> fTraceInfo;
77 
78     /** The SkSL code, split line-by-line. */
79     std::vector<std::string> fSource;
80 
81     /**
82      * A trace hook which populates fTraceInfo during SkVM program evaluation. This will be created
83      * automatically by the SkSLVMCodeGenerator.
84      */
85     std::unique_ptr<skvm::TraceHook> fTraceHook;
86 };
87 
88 }  // namespace SkSL
89 
90 #endif
91