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