Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(71)

Unified Diff: tools/debugger/SkJsonWriteBuffer.cpp

Issue 1920423002: Prototype code that turns any/every flattenable into JSON (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Fix compile errors Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« tools/debugger/SkJsonWriteBuffer.h ('K') | « tools/debugger/SkJsonWriteBuffer.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/debugger/SkJsonWriteBuffer.cpp
diff --git a/tools/debugger/SkJsonWriteBuffer.cpp b/tools/debugger/SkJsonWriteBuffer.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..beefdf934b68835f286b8ed9b06bb9855880c230
--- /dev/null
+++ b/tools/debugger/SkJsonWriteBuffer.cpp
@@ -0,0 +1,210 @@
+/*
+ * Copyright 2012 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkJsonWriteBuffer.h"
+
+#include "SkObjectParser.h"
+
+uint32_t* SkJsonWriteBuffer::reserve(size_t size) {
+ // This code path should never be encountered! reserve() is part of the SkWriteBuffer
+ // interface, but that's only because SkPaint uses it during flatten() to save a bit
+ // of time & space. It's a leaky abstraction, though - assuming that data can be written
+ // through a raw pointer to the buffer.
+ SkASSERT(false);
+
+ // Regardless - we make a best effort to not crash if we somehow get here, by giving
+ // the caller a valid pointer to memory that we then drop on the floor:
+ SkASSERT(SkAlign4(size) == size);
+ if (size > fReserveDataSize) {
+ fReserveData.realloc(size / sizeof(uint32_t));
+ }
+
+ return fReserveData.get();
+}
+
+void SkJsonWriteBuffer::append(const char* type, const Json::Value& value) {
+ SkString fullName = SkStringPrintf("%02d_%s", fFieldCount++, type);
+ fJson[fullName.c_str()] = value;
+}
+
+void SkJsonWriteBuffer::writeByteArray(const void* data, size_t size) {
+ Json::Value jsonArray;
+ const uint8_t* bytes = reinterpret_cast<const uint8_t*>(data);
+ for (size_t i = 0; i < size; ++i) {
+ SkString hexByte = SkStringPrintf("%02x", bytes[i]);
+ jsonArray.append(hexByte.c_str());
+ }
+ this->append("byteArray", jsonArray);
+}
+
+void SkJsonWriteBuffer::writeDataAsByteArray(SkData* data) {
+ Json::Value jsonArray;
+ const uint8_t* bytes = data->bytes();
+ for (size_t i = 0; i < data->size(); ++i) {
+ SkString hexByte = SkStringPrintf("%02x", bytes[i]);
+ jsonArray.append(hexByte.c_str());
+ }
+ this->append("data", jsonArray);
+}
+
+void SkJsonWriteBuffer::writeBool(bool value) {
+ this->append("bool", value);
+}
+
+void SkJsonWriteBuffer::writeScalar(SkScalar value) {
+ this->append("scalar", value);
+}
+
+void SkJsonWriteBuffer::writeScalarArray(const SkScalar* value, uint32_t count) {
+ Json::Value jsonArray;
+ for (uint32_t i = 0; i < count; ++i) {
+ jsonArray.append(value[i]);
+ }
+ this->append("scalarArray", jsonArray);
+}
+
+void SkJsonWriteBuffer::writeInt(int32_t value) {
+ this->append("int", value);
+}
+
+void SkJsonWriteBuffer::writeIntArray(const int32_t* value, uint32_t count) {
+ Json::Value jsonArray;
+ for (uint32_t i = 0; i < count; ++i) {
+ jsonArray.append(value[i]);
+ }
+ this->append("intArray", jsonArray);
+}
+
+void SkJsonWriteBuffer::writeUInt(uint32_t value) {
+ this->append("uint", value);
+}
+
+void SkJsonWriteBuffer::write32(int32_t value) {
+ this->append("int32", value);
+}
+
+void SkJsonWriteBuffer::writeString(const char* value) {
+ this->append("string", value);
+}
+
+void SkJsonWriteBuffer::writeEncodedString(const void* value, size_t byteLength,
+ SkPaint::TextEncoding encoding) {
+ SkAutoTDelete<SkString> string = SkObjectParser::TextToString(value, byteLength, encoding);
+ this->append("encodedString", string->c_str());
+}
+
+void SkJsonWriteBuffer::writeFunctionPtr(void* ptr) {
+ SkString ptrString = SkStringPrintf("%p", ptr);
+ this->append("functionPtr", ptrString.c_str());
+}
+
+void SkJsonWriteBuffer::writeFlattenable(const SkFlattenable* flattenable) {
+ if (flattenable) {
+ SkJsonWriteBuffer flattenableBuffer(fUrlDataManager);
+ flattenable->flatten(flattenableBuffer);
+ this->append(flattenable->getTypeName(), flattenableBuffer.getValue());
+ } else {
+ 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.
+ }
+}
+
+void SkJsonWriteBuffer::writeColor(const SkColor& color) {
+ 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
+ this->append("color", hexColor.c_str());
+}
+
+void SkJsonWriteBuffer::writeColorArray(const SkColor* color, uint32_t count) {
+ Json::Value jsonArray;
+ for (uint32_t i = 0; i < count; ++i) {
+ SkString hexColor = SkStringPrintf("#%08x", color[i]);
+ jsonArray.append(hexColor.c_str());
+ }
+ this->append("colorArray", jsonArray);
+}
+
+void SkJsonWriteBuffer::writePoint(const SkPoint& point) {
+ Json::Value jsonPoint;
+ jsonPoint.append(point.fX);
+ jsonPoint.append(point.fY);
+ this->append("point", jsonPoint);
+}
+
+void SkJsonWriteBuffer::writePointArray(const SkPoint* point, uint32_t count) {
+ Json::Value jsonArray;
+ for (uint32_t i = 0; i < count; ++i) {
+ Json::Value jsonPoint;
+ jsonPoint.append(point[i].fX);
+ jsonPoint.append(point[i].fY);
+ jsonArray.append(jsonPoint);
+ }
+ this->append("pointArray", jsonArray);
+}
+
+void SkJsonWriteBuffer::writeMatrix(const SkMatrix& matrix) {
+ SkString matrixString;
+#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
+ matrix.toString(&matrixString);
+#endif
+ this->append("matrix", matrixString.c_str());
+}
+
+void SkJsonWriteBuffer::writeIRect(const SkIRect& rect) {
+ Json::Value jsonRect;
+ jsonRect.append(rect.fLeft);
+ jsonRect.append(rect.fTop);
+ jsonRect.append(rect.fRight);
+ jsonRect.append(rect.fBottom);
+ this->append("irect", jsonRect);
+}
+
+void SkJsonWriteBuffer::writeRect(const SkRect& rect) {
+ Json::Value jsonRect;
+ jsonRect.append(rect.fLeft);
+ jsonRect.append(rect.fTop);
+ jsonRect.append(rect.fRight);
+ jsonRect.append(rect.fBottom);
+ this->append("rect", jsonRect);
+}
+
+void SkJsonWriteBuffer::writeRegion(const SkRegion& region) {
+ // Unsupported
+ this->append("region", Json::Value());
+}
+
+void SkJsonWriteBuffer::writePath(const SkPath& path) {
+ SkAutoTDelete<SkString> pathString = SkObjectParser::PathToString(path);
+ this->append("path", pathString->c_str());
+}
+
+size_t SkJsonWriteBuffer::writeStream(SkStream* stream, size_t length) {
+ // Contents not supported
+ SkASSERT(length < Json::Value::maxUInt);
+ this->append("stream", static_cast<Json::UInt>(length));
+ return 0;
+}
+
+void SkJsonWriteBuffer::writeBitmap(const SkBitmap& bitmap) {
+ SkAutoTDelete<SkString> bitmapString = SkObjectParser::BitmapToString(bitmap);
+ this->append("bitmap", bitmapString->c_str());
+}
+
+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
+ SkAutoTDelete<SkString> imageString = SkObjectParser::ImageToString(image);
+ this->append("image", imageString->c_str());
+}
+
+void SkJsonWriteBuffer::writeTypeface(SkTypeface* typeface) {
+ // Unsupported
+ this->append("typeface", Json::Value());
mtklein 2016/04/29 17:55:42 Might be nice to surface up some basics: - getF
+}
+
+void SkJsonWriteBuffer::writePaint(const SkPaint& paint) {
+ // TODO: Store a reference to the UrlDataManager in this class, then call back into
mtklein 2016/04/29 17:55:42 Haven't you done this here?
Brian Osman 2016/04/29 19:59:48 Acknowledged.
+ // make_json_paint in this situation, to get the richer decoding?
+ extern Json::Value make_json_paint(const SkPaint& paint, UrlDataManager& urlDataManager);
+ this->append("paint", make_json_paint(paint, *fUrlDataManager));
+}
« tools/debugger/SkJsonWriteBuffer.h ('K') | « tools/debugger/SkJsonWriteBuffer.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698