OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright 2012 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 "SkJsonWriteBuffer.h" | |
9 | |
10 #include "SkObjectParser.h" | |
11 | |
12 uint32_t* SkJsonWriteBuffer::reserve(size_t size) { | |
13 // This code path should never be encountered! reserve() is part of the SkWr iteBuffer | |
14 // interface, but that's only because SkPaint uses it during flatten() to sa ve a bit | |
15 // of time & space. It's a leaky abstraction, though - assuming that data ca n be written | |
16 // through a raw pointer to the buffer. | |
17 SkASSERT(false); | |
18 | |
19 // Regardless - we make a best effort to not crash if we somehow get here, b y giving | |
20 // the caller a valid pointer to memory that we then drop on the floor: | |
21 SkASSERT(SkAlign4(size) == size); | |
22 if (size > fReserveDataSize) { | |
23 fReserveData.realloc(size / sizeof(uint32_t)); | |
24 } | |
25 | |
26 return fReserveData.get(); | |
27 } | |
28 | |
29 void SkJsonWriteBuffer::append(const char* type, const Json::Value& value) { | |
30 SkString fullName = SkStringPrintf("%02d_%s", fFieldCount++, type); | |
31 fJson[fullName.c_str()] = value; | |
32 } | |
33 | |
34 void SkJsonWriteBuffer::writeByteArray(const void* data, size_t size) { | |
35 Json::Value jsonArray; | |
36 const uint8_t* bytes = reinterpret_cast<const uint8_t*>(data); | |
37 for (size_t i = 0; i < size; ++i) { | |
38 SkString hexByte = SkStringPrintf("%02x", bytes[i]); | |
39 jsonArray.append(hexByte.c_str()); | |
40 } | |
41 this->append("byteArray", jsonArray); | |
42 } | |
43 | |
44 void SkJsonWriteBuffer::writeDataAsByteArray(SkData* data) { | |
45 Json::Value jsonArray; | |
46 const uint8_t* bytes = data->bytes(); | |
47 for (size_t i = 0; i < data->size(); ++i) { | |
48 SkString hexByte = SkStringPrintf("%02x", bytes[i]); | |
49 jsonArray.append(hexByte.c_str()); | |
50 } | |
51 this->append("data", jsonArray); | |
52 } | |
53 | |
54 void SkJsonWriteBuffer::writeBool(bool value) { | |
55 this->append("bool", value); | |
56 } | |
57 | |
58 void SkJsonWriteBuffer::writeScalar(SkScalar value) { | |
59 this->append("scalar", value); | |
60 } | |
61 | |
62 void SkJsonWriteBuffer::writeScalarArray(const SkScalar* value, uint32_t count) { | |
63 Json::Value jsonArray; | |
64 for (uint32_t i = 0; i < count; ++i) { | |
65 jsonArray.append(value[i]); | |
66 } | |
67 this->append("scalarArray", jsonArray); | |
68 } | |
69 | |
70 void SkJsonWriteBuffer::writeInt(int32_t value) { | |
71 this->append("int", value); | |
72 } | |
73 | |
74 void SkJsonWriteBuffer::writeIntArray(const int32_t* value, uint32_t count) { | |
75 Json::Value jsonArray; | |
76 for (uint32_t i = 0; i < count; ++i) { | |
77 jsonArray.append(value[i]); | |
78 } | |
79 this->append("intArray", jsonArray); | |
80 } | |
81 | |
82 void SkJsonWriteBuffer::writeUInt(uint32_t value) { | |
83 this->append("uint", value); | |
84 } | |
85 | |
86 void SkJsonWriteBuffer::write32(int32_t value) { | |
87 this->append("int32", value); | |
88 } | |
89 | |
90 void SkJsonWriteBuffer::writeString(const char* value) { | |
91 this->append("string", value); | |
92 } | |
93 | |
94 void SkJsonWriteBuffer::writeEncodedString(const void* value, size_t byteLength, | |
95 SkPaint::TextEncoding encoding) { | |
96 SkAutoTDelete<SkString> string = SkObjectParser::TextToString(value, byteLen gth, encoding); | |
97 this->append("encodedString", string->c_str()); | |
98 } | |
99 | |
100 void SkJsonWriteBuffer::writeFunctionPtr(void* ptr) { | |
101 SkString ptrString = SkStringPrintf("%p", ptr); | |
102 this->append("functionPtr", ptrString.c_str()); | |
103 } | |
104 | |
105 void SkJsonWriteBuffer::writeFlattenable(const SkFlattenable* flattenable) { | |
106 if (flattenable) { | |
107 SkJsonWriteBuffer flattenableBuffer(fUrlDataManager); | |
108 flattenable->flatten(flattenableBuffer); | |
109 this->append(flattenable->getTypeName(), flattenableBuffer.getValue()); | |
110 } else { | |
111 this->append("flattenable", "<null>"); | |
mtklein
2016/04/29 17:55:42
Does this happen? Might wanna SkASSERT(false)?
I
Brian Osman
2016/04/29 19:59:48
1) Until I switched to using make_json_paint, yes.
| |
112 } | |
113 } | |
114 | |
115 void SkJsonWriteBuffer::writeColor(const SkColor& color) { | |
116 SkString hexColor = SkStringPrintf("#%08x", color); | |
mtklein
2016/04/29 17:55:42
This leading # tempts me to parse this as a CSS co
Brian Osman
2016/04/29 19:59:48
Yeah - for some reason I was thinking this was leg
| |
117 this->append("color", hexColor.c_str()); | |
118 } | |
119 | |
120 void SkJsonWriteBuffer::writeColorArray(const SkColor* color, uint32_t count) { | |
121 Json::Value jsonArray; | |
122 for (uint32_t i = 0; i < count; ++i) { | |
123 SkString hexColor = SkStringPrintf("#%08x", color[i]); | |
124 jsonArray.append(hexColor.c_str()); | |
125 } | |
126 this->append("colorArray", jsonArray); | |
127 } | |
128 | |
129 void SkJsonWriteBuffer::writePoint(const SkPoint& point) { | |
130 Json::Value jsonPoint; | |
131 jsonPoint.append(point.fX); | |
132 jsonPoint.append(point.fY); | |
133 this->append("point", jsonPoint); | |
134 } | |
135 | |
136 void SkJsonWriteBuffer::writePointArray(const SkPoint* point, uint32_t count) { | |
137 Json::Value jsonArray; | |
138 for (uint32_t i = 0; i < count; ++i) { | |
139 Json::Value jsonPoint; | |
140 jsonPoint.append(point[i].fX); | |
141 jsonPoint.append(point[i].fY); | |
142 jsonArray.append(jsonPoint); | |
143 } | |
144 this->append("pointArray", jsonArray); | |
145 } | |
146 | |
147 void SkJsonWriteBuffer::writeMatrix(const SkMatrix& matrix) { | |
148 SkString matrixString; | |
149 #ifndef SK_IGNORE_TO_STRING | |
mtklein
2016/04/29 17:55:42
As far as I can tell, no one defines this. Is tha
Brian Osman
2016/04/29 19:59:48
I was just basing this on every other call-site (w
| |
150 matrix.toString(&matrixString); | |
151 #endif | |
152 this->append("matrix", matrixString.c_str()); | |
153 } | |
154 | |
155 void SkJsonWriteBuffer::writeIRect(const SkIRect& rect) { | |
156 Json::Value jsonRect; | |
157 jsonRect.append(rect.fLeft); | |
158 jsonRect.append(rect.fTop); | |
159 jsonRect.append(rect.fRight); | |
160 jsonRect.append(rect.fBottom); | |
161 this->append("irect", jsonRect); | |
162 } | |
163 | |
164 void SkJsonWriteBuffer::writeRect(const SkRect& rect) { | |
165 Json::Value jsonRect; | |
166 jsonRect.append(rect.fLeft); | |
167 jsonRect.append(rect.fTop); | |
168 jsonRect.append(rect.fRight); | |
169 jsonRect.append(rect.fBottom); | |
170 this->append("rect", jsonRect); | |
171 } | |
172 | |
173 void SkJsonWriteBuffer::writeRegion(const SkRegion& region) { | |
174 // Unsupported | |
175 this->append("region", Json::Value()); | |
176 } | |
177 | |
178 void SkJsonWriteBuffer::writePath(const SkPath& path) { | |
179 SkAutoTDelete<SkString> pathString = SkObjectParser::PathToString(path); | |
180 this->append("path", pathString->c_str()); | |
181 } | |
182 | |
183 size_t SkJsonWriteBuffer::writeStream(SkStream* stream, size_t length) { | |
184 // Contents not supported | |
185 SkASSERT(length < Json::Value::maxUInt); | |
186 this->append("stream", static_cast<Json::UInt>(length)); | |
187 return 0; | |
188 } | |
189 | |
190 void SkJsonWriteBuffer::writeBitmap(const SkBitmap& bitmap) { | |
191 SkAutoTDelete<SkString> bitmapString = SkObjectParser::BitmapToString(bitmap ); | |
192 this->append("bitmap", bitmapString->c_str()); | |
193 } | |
194 | |
195 void SkJsonWriteBuffer::writeImage(const SkImage* image) { | |
mtklein
2016/04/29 17:55:42
Do we have a way to side-channel the original bitm
Brian Osman
2016/04/29 19:59:48
Actually, the code that's in SkDrawCommand does th
| |
196 SkAutoTDelete<SkString> imageString = SkObjectParser::ImageToString(image); | |
197 this->append("image", imageString->c_str()); | |
198 } | |
199 | |
200 void SkJsonWriteBuffer::writeTypeface(SkTypeface* typeface) { | |
201 // Unsupported | |
202 this->append("typeface", Json::Value()); | |
mtklein
2016/04/29 17:55:42
Might be nice to surface up some basics:
- getF
| |
203 } | |
204 | |
205 void SkJsonWriteBuffer::writePaint(const SkPaint& paint) { | |
206 // TODO: Store a reference to the UrlDataManager in this class, then call ba ck into | |
mtklein
2016/04/29 17:55:42
Haven't you done this here?
Brian Osman
2016/04/29 19:59:48
Acknowledged.
| |
207 // make_json_paint in this situation, to get the richer decoding? | |
208 extern Json::Value make_json_paint(const SkPaint& paint, UrlDataManager& url DataManager); | |
209 this->append("paint", make_json_paint(paint, *fUrlDataManager)); | |
210 } | |
OLD | NEW |