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