2 * Copyright 2016 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "tools/debugger/JsonWriteBuffer.h"
10 #include "include/core/SkFlattenable.h"
11 #include "include/core/SkPoint.h"
12 #include "include/core/SkSamplingOptions.h"
13 #include "include/core/SkString.h"
14 #include "src/utils/SkJSONWriter.h"
15 #include "tools/debugger/DrawCommand.h"
27 void JsonWriteBuffer::append(const char* type) {
28 SkString fullName = SkStringPrintf("%02d_%s", fCount++, type);
29 fWriter->appendName(fullName.c_str());
32 void JsonWriteBuffer::writePad32(const void* data, size_t size) {
33 this->append("rawBytes");
34 fWriter->beginArray();
35 const uint8_t* bytes = reinterpret_cast<const uint8_t*>(data);
36 for (size_t i = 0; i < size; ++i) {
37 SkString hexByte = SkStringPrintf("%02x", bytes[i]);
38 fWriter->appendString(hexByte.c_str());
43 void JsonWriteBuffer::writeByteArray(const void* data, size_t size) {
44 this->append("byteArray");
45 fWriter->beginArray();
46 const uint8_t* bytes = reinterpret_cast<const uint8_t*>(data);
47 for (size_t i = 0; i < size; ++i) {
48 SkString hexByte = SkStringPrintf("%02x", bytes[i]);
49 fWriter->appendString(hexByte.c_str());
54 void JsonWriteBuffer::writeBool(bool value) {
56 fWriter->appendBool(value);
59 void JsonWriteBuffer::writeScalar(SkScalar value) {
60 this->append("scalar");
61 fWriter->appendFloat(value);
64 void JsonWriteBuffer::writeScalarArray(const SkScalar* value, uint32_t count) {
65 this->append("scalarArray");
66 fWriter->beginArray();
67 for (uint32_t i = 0; i < count; ++i) {
68 fWriter->appendFloat(value[i]);
73 void JsonWriteBuffer::writeInt(int32_t value) {
75 fWriter->appendS32(value);
78 void JsonWriteBuffer::writeIntArray(const int32_t* value, uint32_t count) {
79 this->append("intArray");
80 fWriter->beginArray();
81 for (uint32_t i = 0; i < count; ++i) {
82 fWriter->appendS32(value[i]);
87 void JsonWriteBuffer::writeUInt(uint32_t value) {
89 fWriter->appendU32(value);
92 void JsonWriteBuffer::writeString(const char* value) {
93 this->append("string");
94 fWriter->appendString(value);
97 void JsonWriteBuffer::writeFlattenable(const SkFlattenable* flattenable) {
99 this->append(flattenable->getTypeName());
100 fWriter->beginObject();
101 JsonWriteBuffer flattenableBuffer(fWriter, fUrlDataManager);
102 flattenable->flatten(flattenableBuffer);
103 fWriter->endObject();
105 this->append("flattenable");
106 fWriter->appendPointer(nullptr);
110 void JsonWriteBuffer::writeColor(SkColor color) {
111 this->append("color");
112 DrawCommand::MakeJsonColor(*fWriter, color);
115 void JsonWriteBuffer::writeColorArray(const SkColor* color, uint32_t count) {
116 this->append("colorArray");
117 fWriter->beginArray();
118 for (uint32_t i = 0; i < count; ++i) {
119 DrawCommand::MakeJsonColor(*fWriter, color[i]);
124 void JsonWriteBuffer::writeColor4f(const SkColor4f& color) {
125 this->append("color");
126 DrawCommand::MakeJsonColor4f(*fWriter, color);
129 void JsonWriteBuffer::writeColor4fArray(const SkColor4f* color, uint32_t count) {
130 this->append("colorArray");
131 fWriter->beginArray();
132 for (uint32_t i = 0; i < count; ++i) {
133 DrawCommand::MakeJsonColor4f(*fWriter, color[i]);
138 void JsonWriteBuffer::writePoint(const SkPoint& point) {
139 this->append("point");
140 DrawCommand::MakeJsonPoint(*fWriter, point);
143 void JsonWriteBuffer::writePoint3(const SkPoint3& point) {
144 this->append("point3");
145 DrawCommand::MakeJsonPoint3(*fWriter, point);
148 void JsonWriteBuffer::writePointArray(const SkPoint* point, uint32_t count) {
149 this->append("pointArray");
150 fWriter->beginArray();
151 for (uint32_t i = 0; i < count; ++i) {
152 DrawCommand::MakeJsonPoint(*fWriter, point[i]);
157 void JsonWriteBuffer::write(const SkM44& matrix) {
158 this->append("matrix");
159 fWriter->beginArray();
160 for (int r = 0; r < 4; ++r) {
161 fWriter->beginArray(nullptr, false);
162 SkV4 v = matrix.row(r);
163 for (int c = 0; c < 4; ++c) {
164 fWriter->appendFloat(v[c]);
171 void JsonWriteBuffer::writeMatrix(const SkMatrix& matrix) {
172 this->append("matrix");
173 DrawCommand::MakeJsonMatrix(*fWriter, matrix);
176 void JsonWriteBuffer::writeIRect(const SkIRect& rect) {
177 this->append("irect");
178 DrawCommand::MakeJsonIRect(*fWriter, rect);
181 void JsonWriteBuffer::writeRect(const SkRect& rect) {
182 this->append("rect");
183 DrawCommand::MakeJsonRect(*fWriter, rect);
186 void JsonWriteBuffer::writeRegion(const SkRegion& region) {
187 this->append("region");
188 DrawCommand::MakeJsonRegion(*fWriter, region);
191 void JsonWriteBuffer::writePath(const SkPath& path) {
192 this->append("path");
193 DrawCommand::MakeJsonPath(*fWriter, path);
196 void JsonWriteBuffer::writeSampling(const SkSamplingOptions& sampling) {
197 this->append("sampling");
198 DrawCommand::MakeJsonSampling(*fWriter, sampling);
201 size_t JsonWriteBuffer::writeStream(SkStream* stream, size_t length) {
202 // Contents not supported
203 this->append("stream");
204 fWriter->appendU64(static_cast<uint64_t>(length));
208 void JsonWriteBuffer::writeImage(const SkImage* image) {
209 this->append("image");
210 fWriter->beginObject();
211 DrawCommand::flatten(*image, *fWriter, *fUrlDataManager);
212 fWriter->endObject();
215 void JsonWriteBuffer::writeTypeface(SkTypeface* typeface) {
217 this->append("typeface");
218 fWriter->appendPointer(typeface);
221 void JsonWriteBuffer::writePaint(const SkPaint& paint) {
222 this->append("paint");
223 DrawCommand::MakeJsonPaint(*fWriter, paint, *fUrlDataManager);