Mike Klein | 8f4e224 | 2019-03-20 11:59:00 -0500 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "tools/debugger/JsonWriteBuffer.h" |
Mike Klein | 8f4e224 | 2019-03-20 11:59:00 -0500 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "tools/debugger/DrawCommand.h" |
Mike Klein | 8f4e224 | 2019-03-20 11:59:00 -0500 | [diff] [blame] | 11 | |
| 12 | void JsonWriteBuffer::append(const char* type) { |
| 13 | SkString fullName = SkStringPrintf("%02d_%s", fCount++, type); |
| 14 | fWriter->appendName(fullName.c_str()); |
| 15 | } |
| 16 | |
| 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 | |
| 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 | |
| 39 | void JsonWriteBuffer::writeBool(bool value) { |
| 40 | this->append("bool"); |
| 41 | fWriter->appendBool(value); |
| 42 | } |
| 43 | |
| 44 | void JsonWriteBuffer::writeScalar(SkScalar value) { |
| 45 | this->append("scalar"); |
| 46 | fWriter->appendFloat(value); |
| 47 | } |
| 48 | |
| 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 | |
| 58 | void JsonWriteBuffer::writeInt(int32_t value) { |
| 59 | this->append("int"); |
| 60 | fWriter->appendS32(value); |
| 61 | } |
| 62 | |
| 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 | |
| 72 | void JsonWriteBuffer::writeUInt(uint32_t value) { |
| 73 | this->append("uint"); |
| 74 | fWriter->appendU32(value); |
| 75 | } |
| 76 | |
| 77 | void JsonWriteBuffer::writeString(const char* value) { |
| 78 | this->append("string"); |
| 79 | fWriter->appendString(value); |
| 80 | } |
| 81 | |
| 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 | |
| 95 | void JsonWriteBuffer::writeColor(SkColor color) { |
| 96 | this->append("color"); |
| 97 | DrawCommand::MakeJsonColor(*fWriter, color); |
| 98 | } |
| 99 | |
| 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 | |
| 109 | void JsonWriteBuffer::writeColor4f(const SkColor4f& color) { |
| 110 | this->append("color"); |
| 111 | DrawCommand::MakeJsonColor4f(*fWriter, color); |
| 112 | } |
| 113 | |
| 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 | |
| 123 | void JsonWriteBuffer::writePoint(const SkPoint& point) { |
| 124 | this->append("point"); |
| 125 | DrawCommand::MakeJsonPoint(*fWriter, point); |
| 126 | } |
| 127 | |
| 128 | void JsonWriteBuffer::writePoint3(const SkPoint3& point) { |
| 129 | this->append("point3"); |
| 130 | DrawCommand::MakeJsonPoint3(*fWriter, point); |
| 131 | } |
| 132 | |
| 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 | |
Mike Reed | 6d0ab95 | 2020-07-20 16:07:01 -0400 | [diff] [blame] | 142 | void JsonWriteBuffer::write(const SkM44& matrix) { |
| 143 | this->append("matrix"); |
| 144 | fWriter->beginArray(); |
| 145 | for (int r = 0; r < 4; ++r) { |
| 146 | fWriter->beginArray(nullptr, false); |
| 147 | SkV4 v = matrix.row(r); |
| 148 | for (int c = 0; c < 4; ++c) { |
| 149 | fWriter->appendFloat(v[c]); |
| 150 | } |
| 151 | fWriter->endArray(); |
| 152 | } |
| 153 | fWriter->endArray(); |
| 154 | } |
| 155 | |
Mike Klein | 8f4e224 | 2019-03-20 11:59:00 -0500 | [diff] [blame] | 156 | void JsonWriteBuffer::writeMatrix(const SkMatrix& matrix) { |
| 157 | this->append("matrix"); |
| 158 | DrawCommand::MakeJsonMatrix(*fWriter, matrix); |
| 159 | } |
| 160 | |
| 161 | void JsonWriteBuffer::writeIRect(const SkIRect& rect) { |
| 162 | this->append("irect"); |
| 163 | DrawCommand::MakeJsonIRect(*fWriter, rect); |
| 164 | } |
| 165 | |
| 166 | void JsonWriteBuffer::writeRect(const SkRect& rect) { |
| 167 | this->append("rect"); |
| 168 | DrawCommand::MakeJsonRect(*fWriter, rect); |
| 169 | } |
| 170 | |
| 171 | void JsonWriteBuffer::writeRegion(const SkRegion& region) { |
| 172 | this->append("region"); |
| 173 | DrawCommand::MakeJsonRegion(*fWriter, region); |
| 174 | } |
| 175 | |
| 176 | void JsonWriteBuffer::writePath(const SkPath& path) { |
| 177 | this->append("path"); |
| 178 | DrawCommand::MakeJsonPath(*fWriter, path); |
| 179 | } |
| 180 | |
| 181 | size_t JsonWriteBuffer::writeStream(SkStream* stream, size_t length) { |
| 182 | // Contents not supported |
| 183 | this->append("stream"); |
| 184 | fWriter->appendU64(static_cast<uint64_t>(length)); |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | void JsonWriteBuffer::writeImage(const SkImage* image) { |
| 189 | this->append("image"); |
| 190 | fWriter->beginObject(); |
| 191 | DrawCommand::flatten(*image, *fWriter, *fUrlDataManager); |
| 192 | fWriter->endObject(); |
| 193 | } |
| 194 | |
| 195 | void JsonWriteBuffer::writeTypeface(SkTypeface* typeface) { |
| 196 | // Unsupported |
| 197 | this->append("typeface"); |
| 198 | fWriter->appendPointer(typeface); |
| 199 | } |
| 200 | |
| 201 | void JsonWriteBuffer::writePaint(const SkPaint& paint) { |
| 202 | this->append("paint"); |
| 203 | DrawCommand::MakeJsonPaint(*fWriter, paint, *fUrlDataManager); |
| 204 | } |