| 1 |
/* |
| 2 |
* Copyright 2016 Google Inc. |
| 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 "tools/debugger/JsonWriteBuffer.h" |
| 9 |
|
| 10 |
#include "include/core/SkFlattenable.h" |
| 11 |
#include "include/core/SkPoint.h" |
| 12 |
#include "include/core/SkString.h" |
| 13 |
#include "src/utils/SkJSONWriter.h" |
| 14 |
#include "tools/debugger/DrawCommand.h" |
| 15 |
|
| 16 |
class SkImage; |
| 17 |
class SkMatrix; |
| 18 |
class SkPaint; |
| 19 |
class SkRegion; |
| 20 |
class SkStream; |
| 21 |
class SkTypeface; |
| 22 |
struct SkIRect; |
| 23 |
struct SkPoint3; |
| 24 |
struct SkRect; |
| 25 |
|
| 26 |
void JsonWriteBuffer::append(const char* type) { |
| 27 |
SkString fullName = SkStringPrintf("%02d_%s", fCount++, type); |
| 28 |
fWriter->appendName(fullName.c_str()); |
| 29 |
} |
| 30 |
|
| 31 |
void JsonWriteBuffer::writePad32(const void* data, size_t size) { |
| 32 |
this->append("rawBytes"); |
| 33 |
fWriter->beginArray(); |
| 34 |
const uint8_t* bytes = reinterpret_cast<const uint8_t*>(data); |
| 35 |
for (size_t i = 0; i < size; ++i) { |
| 36 |
SkString hexByte = SkStringPrintf("%02x", bytes[i]); |
| 37 |
fWriter->appendString(hexByte); |
| 38 |
} |
| 39 |
fWriter->endArray(); |
| 40 |
} |
| 41 |
|
| 42 |
void JsonWriteBuffer::writeByteArray(const void* data, size_t size) { |
| 43 |
this->append("byteArray"); |
| 44 |
fWriter->beginArray(); |
| 45 |
const uint8_t* bytes = reinterpret_cast<const uint8_t*>(data); |
| 46 |
for (size_t i = 0; i < size; ++i) { |
| 47 |
SkString hexByte = SkStringPrintf("%02x", bytes[i]); |
| 48 |
fWriter->appendString(hexByte); |
| 49 |
} |
| 50 |
fWriter->endArray(); |
| 51 |
} |
| 52 |
|
| 53 |
void JsonWriteBuffer::writeBool(bool value) { |
| 54 |
this->append("bool"); |
| 55 |
fWriter->appendBool(value); |
| 56 |
} |
| 57 |
|
| 58 |
void JsonWriteBuffer::writeScalar(SkScalar value) { |
| 59 |
this->append("scalar"); |
| 60 |
fWriter->appendFloat(value); |
| 61 |
} |
| 62 |
|
| 63 |
void JsonWriteBuffer::writeScalarArray(SkSpan<const SkScalar> values) { |
| 64 |
this->append("scalarArray"); |
| 65 |
fWriter->beginArray(); |
| 66 |
for (auto value : values) { |
| 67 |
fWriter->appendFloat(value); |
| 68 |
} |
| 69 |
fWriter->endArray(); |
| 70 |
} |
| 71 |
|
| 72 |
void JsonWriteBuffer::writeInt(int32_t value) { |
| 73 |
this->append("int"); |
| 74 |
fWriter->appendS32(value); |
| 75 |
} |
| 76 |
|
| 77 |
void JsonWriteBuffer::writeIntArray(SkSpan<const int32_t> values) { |
| 78 |
this->append("intArray"); |
| 79 |
fWriter->beginArray(); |
| 80 |
for (auto value : values) { |
| 81 |
fWriter->appendS32(value); |
| 82 |
} |
| 83 |
fWriter->endArray(); |
| 84 |
} |
| 85 |
|
| 86 |
void JsonWriteBuffer::writeUInt(uint32_t value) { |
| 87 |
this->append("uint"); |
| 88 |
fWriter->appendU32(value); |
| 89 |
} |
| 90 |
|
| 91 |
void JsonWriteBuffer::writeString(std::string_view value) { |
| 92 |
this->append("string"); |
| 93 |
fWriter->appendString(value.data(), value.size()); |
| 94 |
} |
| 95 |
|
| 96 |
void JsonWriteBuffer::writeFlattenable(const SkFlattenable* flattenable) { |
| 97 |
if (flattenable) { |
| 98 |
this->append(flattenable->getTypeName()); |
| 99 |
fWriter->beginObject(); |
| 100 |
JsonWriteBuffer flattenableBuffer(fWriter, fUrlDataManager); |
| 101 |
flattenable->flatten(flattenableBuffer); |
| 102 |
fWriter->endObject(); |
| 103 |
} else { |
| 104 |
this->append("flattenable"); |
| 105 |
fWriter->appendPointer(nullptr); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
void JsonWriteBuffer::writeColor(SkColor color) { |
| 110 |
this->append("color"); |
| 111 |
DrawCommand::MakeJsonColor(*fWriter, color); |
| 112 |
} |
| 113 |
|
| 114 |
void JsonWriteBuffer::writeColorArray(SkSpan<const SkColor> colors) { |
| 115 |
this->append("colorArray"); |
| 116 |
fWriter->beginArray(); |
| 117 |
for (auto color : colors) { |
| 118 |
DrawCommand::MakeJsonColor(*fWriter, color); |
| 119 |
} |
| 120 |
fWriter->endArray(); |
| 121 |
} |
| 122 |
|
| 123 |
void JsonWriteBuffer::writeColor4f(const SkColor4f& color) { |
| 124 |
this->append("color"); |
| 125 |
DrawCommand::MakeJsonColor4f(*fWriter, color); |
| 126 |
} |
| 127 |
|
| 128 |
void JsonWriteBuffer::writeColor4fArray(SkSpan<const SkColor4f> colors) { |
| 129 |
this->append("colorArray"); |
| 130 |
fWriter->beginArray(); |
| 131 |
for (auto color : colors) { |
| 132 |
DrawCommand::MakeJsonColor4f(*fWriter, color); |
| 133 |
} |
| 134 |
fWriter->endArray(); |
| 135 |
} |
| 136 |
|
| 137 |
void JsonWriteBuffer::writePoint(const SkPoint& point) { |
| 138 |
this->append("point"); |
| 139 |
DrawCommand::MakeJsonPoint(*fWriter, point); |
| 140 |
} |
| 141 |
|
| 142 |
void JsonWriteBuffer::writePoint3(const SkPoint3& point) { |
| 143 |
this->append("point3"); |
| 144 |
DrawCommand::MakeJsonPoint3(*fWriter, point); |
| 145 |
} |
| 146 |
|
| 147 |
void JsonWriteBuffer::writePointArray(SkSpan<const SkPoint> points) { |
| 148 |
this->append("pointArray"); |
| 149 |
fWriter->beginArray(); |
| 150 |
for (auto point : points) { |
| 151 |
DrawCommand::MakeJsonPoint(*fWriter, point); |
| 152 |
} |
| 153 |
fWriter->endArray(); |
| 154 |
} |
| 155 |
|
| 156 |
void JsonWriteBuffer::write(const SkM44& matrix) { |
| 157 |
this->append("matrix"); |
| 158 |
fWriter->beginArray(); |
| 159 |
for (int r = 0; r < 4; ++r) { |
| 160 |
fWriter->beginArray(nullptr, false); |
| 161 |
SkV4 v = matrix.row(r); |
| 162 |
for (int c = 0; c < 4; ++c) { |
| 163 |
fWriter->appendFloat(v[c]); |
| 164 |
} |
| 165 |
fWriter->endArray(); |
| 166 |
} |
| 167 |
fWriter->endArray(); |
| 168 |
} |
| 169 |
|
| 170 |
void JsonWriteBuffer::writeMatrix(const SkMatrix& matrix) { |
| 171 |
this->append("matrix"); |
| 172 |
DrawCommand::MakeJsonMatrix(*fWriter, matrix); |
| 173 |
} |
| 174 |
|
| 175 |
void JsonWriteBuffer::writeIRect(const SkIRect& rect) { |
| 176 |
this->append("irect"); |
| 177 |
DrawCommand::MakeJsonIRect(*fWriter, rect); |
| 178 |
} |
| 179 |
|
| 180 |
void JsonWriteBuffer::writeRect(const SkRect& rect) { |
| 181 |
this->append("rect"); |
| 182 |
DrawCommand::MakeJsonRect(*fWriter, rect); |
| 183 |
} |
| 184 |
|
| 185 |
void JsonWriteBuffer::writeRegion(const SkRegion& region) { |
| 186 |
this->append("region"); |
| 187 |
DrawCommand::MakeJsonRegion(*fWriter, region); |
| 188 |
} |
| 189 |
|
| 190 |
void JsonWriteBuffer::writePath(const SkPath& path) { |
| 191 |
this->append("path"); |
| 192 |
DrawCommand::MakeJsonPath(*fWriter, path); |
| 193 |
} |
| 194 |
|
| 195 |
void JsonWriteBuffer::writeSampling(const SkSamplingOptions& sampling) { |
| 196 |
this->append("sampling"); |
| 197 |
DrawCommand::MakeJsonSampling(*fWriter, sampling); |
| 198 |
} |
| 199 |
|
| 200 |
size_t JsonWriteBuffer::writeStream(SkStream* stream, size_t length) { |
| 201 |
// Contents not supported |
| 202 |
this->append("stream"); |
| 203 |
fWriter->appendU64(static_cast<uint64_t>(length)); |
| 204 |
return 0; |
| 205 |
} |
| 206 |
|
| 207 |
void JsonWriteBuffer::writeImage(const SkImage* image) { |
| 208 |
this->append("image"); |
| 209 |
fWriter->beginObject(); |
| 210 |
DrawCommand::flatten(*image, *fWriter, *fUrlDataManager); |
| 211 |
fWriter->endObject(); |
| 212 |
} |
| 213 |
|
| 214 |
void JsonWriteBuffer::writeTypeface(SkTypeface* typeface) { |
| 215 |
// Unsupported |
| 216 |
this->append("typeface"); |
| 217 |
fWriter->appendPointer(typeface); |
| 218 |
} |
| 219 |
|
| 220 |
void JsonWriteBuffer::writePaint(const SkPaint& paint) { |
| 221 |
this->append("paint"); |
| 222 |
DrawCommand::MakeJsonPaint(*fWriter, paint, *fUrlDataManager); |
| 223 |
} |