JsonWriteBuffer.cpp 5.3 KB

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