Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / skia / tools / debugger / JsonWriteBuffer.cpp
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/SkSamplingOptions.h"
13 #include "include/core/SkString.h"
14 #include "src/utils/SkJSONWriter.h"
15 #include "tools/debugger/DrawCommand.h"
16
17 class SkImage;
18 class SkMatrix;
19 class SkPaint;
20 class SkRegion;
21 class SkStream;
22 class SkTypeface;
23 struct SkIRect;
24 struct SkPoint3;
25 struct SkRect;
26
27 void JsonWriteBuffer::append(const char* type) {
28     SkString fullName = SkStringPrintf("%02d_%s", fCount++, type);
29     fWriter->appendName(fullName.c_str());
30 }
31
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());
39     }
40     fWriter->endArray();
41 }
42
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());
50     }
51     fWriter->endArray();
52 }
53
54 void JsonWriteBuffer::writeBool(bool value) {
55     this->append("bool");
56     fWriter->appendBool(value);
57 }
58
59 void JsonWriteBuffer::writeScalar(SkScalar value) {
60     this->append("scalar");
61     fWriter->appendFloat(value);
62 }
63
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]);
69     }
70     fWriter->endArray();
71 }
72
73 void JsonWriteBuffer::writeInt(int32_t value) {
74     this->append("int");
75     fWriter->appendS32(value);
76 }
77
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]);
83     }
84     fWriter->endArray();
85 }
86
87 void JsonWriteBuffer::writeUInt(uint32_t value) {
88     this->append("uint");
89     fWriter->appendU32(value);
90 }
91
92 void JsonWriteBuffer::writeString(const char* value) {
93     this->append("string");
94     fWriter->appendString(value);
95 }
96
97 void JsonWriteBuffer::writeFlattenable(const SkFlattenable* flattenable) {
98     if (flattenable) {
99         this->append(flattenable->getTypeName());
100         fWriter->beginObject();
101         JsonWriteBuffer flattenableBuffer(fWriter, fUrlDataManager);
102         flattenable->flatten(flattenableBuffer);
103         fWriter->endObject();
104     } else {
105         this->append("flattenable");
106         fWriter->appendPointer(nullptr);
107     }
108 }
109
110 void JsonWriteBuffer::writeColor(SkColor color) {
111     this->append("color");
112     DrawCommand::MakeJsonColor(*fWriter, color);
113 }
114
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]);
120     }
121     fWriter->endArray();
122 }
123
124 void JsonWriteBuffer::writeColor4f(const SkColor4f& color) {
125     this->append("color");
126     DrawCommand::MakeJsonColor4f(*fWriter, color);
127 }
128
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]);
134     }
135     fWriter->endArray();
136 }
137
138 void JsonWriteBuffer::writePoint(const SkPoint& point) {
139     this->append("point");
140     DrawCommand::MakeJsonPoint(*fWriter, point);
141 }
142
143 void JsonWriteBuffer::writePoint3(const SkPoint3& point) {
144     this->append("point3");
145     DrawCommand::MakeJsonPoint3(*fWriter, point);
146 }
147
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]);
153     }
154     fWriter->endArray();
155 }
156
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]);
165         }
166         fWriter->endArray();
167     }
168     fWriter->endArray();
169 }
170
171 void JsonWriteBuffer::writeMatrix(const SkMatrix& matrix) {
172     this->append("matrix");
173     DrawCommand::MakeJsonMatrix(*fWriter, matrix);
174 }
175
176 void JsonWriteBuffer::writeIRect(const SkIRect& rect) {
177     this->append("irect");
178     DrawCommand::MakeJsonIRect(*fWriter, rect);
179 }
180
181 void JsonWriteBuffer::writeRect(const SkRect& rect) {
182     this->append("rect");
183     DrawCommand::MakeJsonRect(*fWriter, rect);
184 }
185
186 void JsonWriteBuffer::writeRegion(const SkRegion& region) {
187     this->append("region");
188     DrawCommand::MakeJsonRegion(*fWriter, region);
189 }
190
191 void JsonWriteBuffer::writePath(const SkPath& path) {
192     this->append("path");
193     DrawCommand::MakeJsonPath(*fWriter, path);
194 }
195
196 void JsonWriteBuffer::writeSampling(const SkSamplingOptions& sampling) {
197     this->append("sampling");
198     DrawCommand::MakeJsonSampling(*fWriter, sampling);
199 }
200
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));
205     return 0;
206 }
207
208 void JsonWriteBuffer::writeImage(const SkImage* image) {
209     this->append("image");
210     fWriter->beginObject();
211     DrawCommand::flatten(*image, *fWriter, *fUrlDataManager);
212     fWriter->endObject();
213 }
214
215 void JsonWriteBuffer::writeTypeface(SkTypeface* typeface) {
216     // Unsupported
217     this->append("typeface");
218     fWriter->appendPointer(typeface);
219 }
220
221 void JsonWriteBuffer::writePaint(const SkPaint& paint) {
222     this->append("paint");
223     DrawCommand::MakeJsonPaint(*fWriter, paint, *fUrlDataManager);
224 }