2 * Copyright 2021 Google LLC
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
11 #include "include/sksl/SkSLDebugTrace.h"
13 #include "include/core/SkPoint.h"
14 #include "src/core/SkVM.h"
15 #include "src/sksl/ir/SkSLType.h"
28 /** The full name of this variable (without component): (e.g. `myArray[3].myStruct.myVector`) */
30 /** The dimensions of this variable: 1x1 is a scalar, Nx1 is a vector, NxM is a matrix. */
31 uint8_t columns = 1, rows = 1;
32 /** Which component of the variable is this slot? (e.g. `vec4.z` is component 2) */
33 uint8_t componentIndex = 0;
34 /** Complex types (arrays/structs) can be tracked as a "group" of adjacent slots. */
36 /** What kind of numbers belong in this slot? */
37 SkSL::Type::NumberKind numberKind = SkSL::Type::NumberKind::kNonnumeric;
38 /** Where is this variable located in the program? */
40 /** If this slot holds a function's return value, its FunctionInfo index; if not, -1. */
44 struct SkVMFunctionInfo {
45 /** Full function declaration: `float myFunction(half4 color)`) */
49 struct SkVMTraceInfo {
51 kLine, /** data: line number, (unused) */
52 kVar, /** data: slot, value */
53 kEnter, /** data: function index, (unused) */
54 kExit, /** data: function index, (unused) */
55 kScope, /** data: scope delta, (unused) */
61 class SkVMDebugTrace : public DebugTrace {
64 * Sets the device-coordinate pixel to trace. If it's not set, the point at (0, 0) will be used.
66 void setTraceCoord(const SkIPoint& coord);
68 /** Attaches the SkSL source to be debugged. */
69 void setSource(std::string source);
71 /** Serializes a debug trace to JSON which can be parsed by our debugger. */
72 bool readTrace(SkStream* r);
73 void writeTrace(SkWStream* w) const override;
75 /** Generates a human-readable dump of the debug trace. */
76 void dump(SkWStream* o) const override;
78 /** Returns a slot's component as a variable-name suffix, e.g. ".x" or "[2][2]". */
79 std::string getSlotComponentSuffix(int slotIndex) const;
81 /** Bit-casts a slot's value, then converts to text, e.g. "3.14" or "true" or "12345". */
82 std::string getSlotValue(int slotIndex, int32_t value) const;
84 /** Bit-casts a value for a given slot into a double, honoring the slot's NumberKind. */
85 double interpretValueBits(int slotIndex, int32_t valueBits) const;
87 /** Converts a numeric value into text, based on the slot's NumberKind. */
88 std::string slotValueToString(int slotIndex, double value) const;
90 /** The device-coordinate pixel to trace (controlled by setTraceCoord) */
91 SkIPoint fTraceCoord = {};
93 /** A 1:1 mapping of slot numbers to debug information. */
94 std::vector<SkVMSlotInfo> fSlotInfo;
95 std::vector<SkVMFunctionInfo> fFuncInfo;
97 /** The SkSL debug trace. */
98 std::vector<SkVMTraceInfo> fTraceInfo;
100 /** The SkSL code, split line-by-line. */
101 std::vector<std::string> fSource;
104 * A trace hook which populates fTraceInfo during SkVM program evaluation. This will be created
105 * automatically by the SkSLVMCodeGenerator.
107 std::unique_ptr<skvm::TraceHook> fTraceHook;