Moved Canvas->JSON and JSON->Canvas functionality into SkDebugCanvas.
authorethannicholas <ethannicholas@google.com>
Wed, 10 Feb 2016 13:40:46 +0000 (05:40 -0800)
committerCommit bot <commit-bot@chromium.org>
Wed, 10 Feb 2016 13:40:46 +0000 (05:40 -0800)
SkJSONCanvas and SkJSONRenderer will be deleted once the debug server
has been updated.
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1681643002

Committed: https://skia.googlesource.com/skia/+/32fff1ba0c759d97ab78320b7ae46dd7696a4e6e

Committed: https://skia.googlesource.com/skia/+/46cb6d6b82b0ab71033aef91419e8beae25bb8a4

Review URL: https://codereview.chromium.org/1681643002

gyp/SampleApp.gyp
gyp/debugger.gyp
tools/debugger/SkDrawCommand.cpp
tools/debugger/SkDrawCommand.h
tools/debugger/SkObjectParser.cpp

index bb48aff..c22eaa2 100644 (file)
         'views.gyp:views',
         'views_animated.gyp:views_animated',
         'xml.gyp:xml',
+        'jsoncpp.gyp:jsoncpp',
       ],
       'msvs_settings': {
         'VCLinkerTool': {
index e7e8275..c86538d 100644 (file)
       'dependencies': [
         'debugger_qt_mocs',
         'skia_lib.gyp:skia_lib',
+        'jsoncpp.gyp:jsoncpp',
       ],
       'cflags': [
         # Clang gets confused by QWeakPointer, see http://llvm.org/bugs/show_bug.cgi?id=13127
index 17c59e6..e9cdd32 100644 (file)
 
 
 #include "SkDrawCommand.h"
+#include "SkBlurMaskFilter.h"
+#include "SkColorFilter.h"
+#include "SkDashPathEffect.h"
+#include "SkImageFilter.h"
+#include "SkMaskFilter.h"
 #include "SkObjectParser.h"
+#include "SkPaintDefaults.h"
+#include "SkPathEffect.h"
 #include "SkPicture.h"
 #include "SkTextBlob.h"
 #include "SkTextBlobRunIterator.h"
+#include "SkTHash.h"
+#include "SkTypeface.h"
+#include "SkValidatingReadBuffer.h"
+#include "SkWriteBuffer.h"
+
+#define SKDEBUGCANVAS_SEND_BINARIES               false
+
+#define SKDEBUGCANVAS_ATTRIBUTE_COMMAND           "command"
+#define SKDEBUGCANVAS_ATTRIBUTE_MATRIX            "matrix"
+#define SKDEBUGCANVAS_ATTRIBUTE_COORDS            "coords"
+#define SKDEBUGCANVAS_ATTRIBUTE_BOUNDS            "bounds"
+#define SKDEBUGCANVAS_ATTRIBUTE_PAINT             "paint"
+#define SKDEBUGCANVAS_ATTRIBUTE_OUTER             "outer"
+#define SKDEBUGCANVAS_ATTRIBUTE_INNER             "inner"
+#define SKDEBUGCANVAS_ATTRIBUTE_MODE              "mode"
+#define SKDEBUGCANVAS_ATTRIBUTE_POINTS            "points"
+#define SKDEBUGCANVAS_ATTRIBUTE_PATH              "path"
+#define SKDEBUGCANVAS_ATTRIBUTE_TEXT              "text"
+#define SKDEBUGCANVAS_ATTRIBUTE_COLOR             "color"
+#define SKDEBUGCANVAS_ATTRIBUTE_ALPHA             "alpha"
+#define SKDEBUGCANVAS_ATTRIBUTE_STYLE             "style"
+#define SKDEBUGCANVAS_ATTRIBUTE_STROKEWIDTH       "strokeWidth"
+#define SKDEBUGCANVAS_ATTRIBUTE_STROKEMITER       "strokeMiter"
+#define SKDEBUGCANVAS_ATTRIBUTE_CAP               "cap"
+#define SKDEBUGCANVAS_ATTRIBUTE_ANTIALIAS         "antiAlias"
+#define SKDEBUGCANVAS_ATTRIBUTE_REGION            "region"
+#define SKDEBUGCANVAS_ATTRIBUTE_REGIONOP          "op"
+#define SKDEBUGCANVAS_ATTRIBUTE_EDGESTYLE         "edgeStyle"
+#define SKDEBUGCANVAS_ATTRIBUTE_DEVICEREGION      "deviceRegion"
+#define SKDEBUGCANVAS_ATTRIBUTE_BLUR              "blur"
+#define SKDEBUGCANVAS_ATTRIBUTE_SIGMA             "sigma"
+#define SKDEBUGCANVAS_ATTRIBUTE_QUALITY           "quality"
+#define SKDEBUGCANVAS_ATTRIBUTE_TEXTALIGN         "textAlign"
+#define SKDEBUGCANVAS_ATTRIBUTE_TEXTSIZE          "textSize"
+#define SKDEBUGCANVAS_ATTRIBUTE_TEXTSCALEX        "textScaleX"
+#define SKDEBUGCANVAS_ATTRIBUTE_TEXTSKEWX         "textSkewX"
+#define SKDEBUGCANVAS_ATTRIBUTE_DASHING           "dashing"
+#define SKDEBUGCANVAS_ATTRIBUTE_INTERVALS         "intervals"
+#define SKDEBUGCANVAS_ATTRIBUTE_PHASE             "phase"
+#define SKDEBUGCANVAS_ATTRIBUTE_FILLTYPE          "fillType"
+#define SKDEBUGCANVAS_ATTRIBUTE_VERBS             "verbs"
+#define SKDEBUGCANVAS_ATTRIBUTE_NAME              "name"
+#define SKDEBUGCANVAS_ATTRIBUTE_BYTES             "bytes"
+#define SKDEBUGCANVAS_ATTRIBUTE_SHADER            "shader"
+#define SKDEBUGCANVAS_ATTRIBUTE_PATHEFFECT        "pathEffect"
+#define SKDEBUGCANVAS_ATTRIBUTE_MASKFILTER        "maskFilter"
+#define SKDEBUGCANVAS_ATTRIBUTE_XFERMODE          "xfermode"
+#define SKDEBUGCANVAS_ATTRIBUTE_BACKDROP          "backdrop"
+#define SKDEBUGCANVAS_ATTRIBUTE_COLORFILTER       "colorfilter"
+#define SKDEBUGCANVAS_ATTRIBUTE_IMAGEFILTER       "imagefilter"
+#define SKDEBUGCANVAS_ATTRIBUTE_IMAGE             "image"
+#define SKDEBUGCANVAS_ATTRIBUTE_BITMAP            "bitmap"
+#define SKDEBUGCANVAS_ATTRIBUTE_SRC               "src"
+#define SKDEBUGCANVAS_ATTRIBUTE_DST               "dst"
+#define SKDEBUGCANVAS_ATTRIBUTE_CENTER            "center"
+#define SKDEBUGCANVAS_ATTRIBUTE_STRICT            "strict"
+#define SKDEBUGCANVAS_ATTRIBUTE_DESCRIPTION       "description"
+#define SKDEBUGCANVAS_ATTRIBUTE_X                 "x"
+#define SKDEBUGCANVAS_ATTRIBUTE_Y                 "y"
+#define SKDEBUGCANVAS_ATTRIBUTE_RUNS              "runs"
+#define SKDEBUGCANVAS_ATTRIBUTE_POSITIONS         "positions"
+#define SKDEBUGCANVAS_ATTRIBUTE_GLYPHS            "glyphs"
+#define SKDEBUGCANVAS_ATTRIBUTE_FONT              "font"
+#define SKDEBUGCANVAS_ATTRIBUTE_TYPEFACE          "typeface"
+
+#define SKDEBUGCANVAS_VERB_MOVE                   "move"
+#define SKDEBUGCANVAS_VERB_LINE                   "line"
+#define SKDEBUGCANVAS_VERB_QUAD                   "quad"
+#define SKDEBUGCANVAS_VERB_CUBIC                  "cubic"
+#define SKDEBUGCANVAS_VERB_CONIC                  "conic"
+#define SKDEBUGCANVAS_VERB_CLOSE                  "close"
+
+#define SKDEBUGCANVAS_STYLE_FILL                  "fill"
+#define SKDEBUGCANVAS_STYLE_STROKE                "stroke"
+#define SKDEBUGCANVAS_STYLE_STROKEANDFILL         "strokeAndFill"
+
+#define SKDEBUGCANVAS_POINTMODE_POINTS            "points"
+#define SKDEBUGCANVAS_POINTMODE_LINES             "lines"
+#define SKDEBUGCANVAS_POINTMODE_POLYGON           "polygon"
+
+#define SKDEBUGCANVAS_REGIONOP_DIFFERENCE         "difference"
+#define SKDEBUGCANVAS_REGIONOP_INTERSECT          "intersect"
+#define SKDEBUGCANVAS_REGIONOP_UNION              "union"
+#define SKDEBUGCANVAS_REGIONOP_XOR                "xor"
+#define SKDEBUGCANVAS_REGIONOP_REVERSE_DIFFERENCE "reverseDifference"
+#define SKDEBUGCANVAS_REGIONOP_REPLACE            "replace"
+
+#define SKDEBUGCANVAS_BLURSTYLE_NORMAL            "normal"
+#define SKDEBUGCANVAS_BLURSTYLE_SOLID             "solid"
+#define SKDEBUGCANVAS_BLURSTYLE_OUTER             "outer"
+#define SKDEBUGCANVAS_BLURSTYLE_INNER             "inner"
+
+#define SKDEBUGCANVAS_BLURQUALITY_LOW             "low"
+#define SKDEBUGCANVAS_BLURQUALITY_HIGH            "high"
+
+#define SKDEBUGCANVAS_ALIGN_LEFT                  "left"
+#define SKDEBUGCANVAS_ALIGN_CENTER                "center"
+#define SKDEBUGCANVAS_ALIGN_RIGHT                 "right"
+
+#define SKDEBUGCANVAS_FILLTYPE_WINDING            "winding"
+#define SKDEBUGCANVAS_FILLTYPE_EVENODD            "evenOdd"
+#define SKDEBUGCANVAS_FILLTYPE_INVERSEWINDING     "inverseWinding"
+#define SKDEBUGCANVAS_FILLTYPE_INVERSEEVENODD     "inverseEvenOdd"
+
+#define SKDEBUGCANVAS_CAP_BUTT                    "butt"
+#define SKDEBUGCANVAS_CAP_ROUND                   "round"
+#define SKDEBUGCANVAS_CAP_SQUARE                  "square"
+
+#define SKDEBUGCANVAS_COLORTYPE_ARGB4444          "ARGB4444"
+#define SKDEBUGCANVAS_COLORTYPE_RGBA8888          "RGBA8888"
+#define SKDEBUGCANVAS_COLORTYPE_BGRA8888          "BGRA8888"
+#define SKDEBUGCANVAS_COLORTYPE_565               "565"
+#define SKDEBUGCANVAS_COLORTYPE_GRAY8             "Gray8"
+#define SKDEBUGCANVAS_COLORTYPE_INDEX8            "Index8"
+#define SKDEBUGCANVAS_COLORTYPE_ALPHA8            "Alpha8"
+
+#define SKDEBUGCANVAS_ALPHATYPE_OPAQUE            "opaque"
+#define SKDEBUGCANVAS_ALPHATYPE_PREMUL            "premul"
+#define SKDEBUGCANVAS_ALPHATYPE_UNPREMUL          "unpremul"
+
+typedef SkDrawCommand* (*FROM_JSON)(Json::Value);
 
 // TODO(chudy): Refactor into non subclass model.
 
@@ -70,6 +198,55 @@ SkString SkDrawCommand::toString() const {
     return SkString(GetCommandString(fOpType));
 }
 
+Json::Value SkDrawCommand::toJSON() const {
+    Json::Value result;
+    result[SKDEBUGCANVAS_ATTRIBUTE_COMMAND] = this->GetCommandString(fOpType);
+    return result;
+}
+
+#define INSTALL_FACTORY(name) factories.set(SkString(GetCommandString(k ## name ##_OpType)), \
+                                            (FROM_JSON) Sk ## name ## Command::fromJSON)
+SkDrawCommand* SkDrawCommand::fromJSON(Json::Value& command) {
+    static SkTHashMap<SkString, FROM_JSON> factories;
+    static bool initialized = false;
+    if (!initialized) {
+        initialized = true;
+        INSTALL_FACTORY(Restore);
+        INSTALL_FACTORY(ClipPath);
+        INSTALL_FACTORY(ClipRegion);
+        INSTALL_FACTORY(ClipRect);
+        INSTALL_FACTORY(ClipRRect);
+        INSTALL_FACTORY(Concat);
+        INSTALL_FACTORY(DrawBitmap);
+        INSTALL_FACTORY(DrawBitmapRect);
+        INSTALL_FACTORY(DrawBitmapNine);
+        INSTALL_FACTORY(DrawImage);
+        INSTALL_FACTORY(DrawImageRect);
+        INSTALL_FACTORY(DrawOval);
+        INSTALL_FACTORY(DrawPaint);
+        INSTALL_FACTORY(DrawPath);
+        INSTALL_FACTORY(DrawPoints);
+        INSTALL_FACTORY(DrawText);
+        INSTALL_FACTORY(DrawPosText);
+        INSTALL_FACTORY(DrawTextOnPath);
+        INSTALL_FACTORY(DrawTextBlob);
+
+        INSTALL_FACTORY(DrawRect);
+        INSTALL_FACTORY(DrawRRect);
+        INSTALL_FACTORY(DrawDRRect);
+        INSTALL_FACTORY(Save);
+        INSTALL_FACTORY(SaveLayer);
+        INSTALL_FACTORY(SetMatrix);
+    }
+    SkString name = SkString(command[SKDEBUGCANVAS_ATTRIBUTE_COMMAND].asCString());
+    FROM_JSON* factory = factories.find(name);
+    if (factory == nullptr) {
+        SkDebugf("no JSON factory for '%s'\n", name.c_str());
+        return nullptr;
+    }
+    return (*factory)(command);
+}
+
 SkClearCommand::SkClearCommand(SkColor color) : INHERITED(kDrawClear_OpType) {
     fColor = color;
     fInfo.push(SkObjectParser::CustomTextToString("No Parameters"));
@@ -79,6 +256,23 @@ void SkClearCommand::execute(SkCanvas* canvas) const {
     canvas->clear(fColor);
 }
 
+Json::Value SkClearCommand::toJSON() const {
+    Json::Value result = INHERITED::toJSON();
+    Json::Value colorValue(Json::arrayValue);
+    colorValue.append(Json::Value(SkColorGetA(fColor)));
+    colorValue.append(Json::Value(SkColorGetR(fColor)));
+    colorValue.append(Json::Value(SkColorGetG(fColor)));
+    colorValue.append(Json::Value(SkColorGetB(fColor)));
+    result[SKDEBUGCANVAS_ATTRIBUTE_COLOR] = colorValue;;
+    return result;
+}
+
+ SkClearCommand* SkClearCommand::fromJSON(Json::Value& command) {
+    Json::Value color = command[SKDEBUGCANVAS_ATTRIBUTE_COLOR];
+    return new SkClearCommand(SkColorSetARGB(color[0].asInt(), color[1].asInt(), color[2].asInt(),
+                                             color[3].asInt()));
+}
+
 namespace {
 
 void xlate_and_scale_to_bounds(SkCanvas* canvas, const SkRect& bounds) {
@@ -200,6 +394,986 @@ void render_drrect(SkCanvas* canvas, const SkRRect& outer, const SkRRect& inner)
 
 };
 
+static Json::Value make_json_point(const SkPoint& point) {
+    Json::Value result(Json::arrayValue);
+    result.append(Json::Value(point.x()));
+    result.append(Json::Value(point.y()));
+    return result;
+}
+
+static Json::Value make_json_point(SkScalar x, SkScalar y) {
+    Json::Value result(Json::arrayValue);
+    result.append(Json::Value(x));
+    result.append(Json::Value(y));
+    return result;
+}
+
+static Json::Value make_json_rect(const SkRect& rect) {
+    Json::Value result(Json::arrayValue);
+    result.append(Json::Value(rect.left()));
+    result.append(Json::Value(rect.top()));
+    result.append(Json::Value(rect.right()));
+    result.append(Json::Value(rect.bottom()));
+    return result;
+}
+
+static Json::Value make_json_irect(const SkIRect& rect) {
+    Json::Value result(Json::arrayValue);
+    result.append(Json::Value(rect.left()));
+    result.append(Json::Value(rect.top()));
+    result.append(Json::Value(rect.right()));
+    result.append(Json::Value(rect.bottom()));
+    return result;
+}
+
+static Json::Value make_json_rrect(const SkRRect& rrect) {
+    Json::Value result(Json::arrayValue);
+    result.append(make_json_rect(rrect.rect()));
+    result.append(make_json_point(rrect.radii(SkRRect::kUpperLeft_Corner)));
+    result.append(make_json_point(rrect.radii(SkRRect::kUpperRight_Corner)));
+    result.append(make_json_point(rrect.radii(SkRRect::kLowerRight_Corner)));
+    result.append(make_json_point(rrect.radii(SkRRect::kLowerLeft_Corner)));
+    return result;
+}
+
+static Json::Value make_json_matrix(const SkMatrix& matrix) {
+    Json::Value result(Json::arrayValue);
+    Json::Value row1(Json::arrayValue);
+    row1.append(Json::Value(matrix[0]));
+    row1.append(Json::Value(matrix[1]));
+    row1.append(Json::Value(matrix[2]));
+    result.append(row1);
+    Json::Value row2(Json::arrayValue);
+    row2.append(Json::Value(matrix[3]));
+    row2.append(Json::Value(matrix[4]));
+    row2.append(Json::Value(matrix[5]));
+    result.append(row2);
+    Json::Value row3(Json::arrayValue);
+    row3.append(Json::Value(matrix[6]));
+    row3.append(Json::Value(matrix[7]));
+    row3.append(Json::Value(matrix[8]));
+    result.append(row3);
+    return result;
+}
+static Json::Value make_json_path(const SkPath& path) {
+    Json::Value result(Json::objectValue);
+    switch (path.getFillType()) {
+        case SkPath::kWinding_FillType:
+            result[SKDEBUGCANVAS_ATTRIBUTE_FILLTYPE] = SKDEBUGCANVAS_FILLTYPE_WINDING;
+            break;
+        case SkPath::kEvenOdd_FillType:
+            result[SKDEBUGCANVAS_ATTRIBUTE_FILLTYPE] = SKDEBUGCANVAS_FILLTYPE_EVENODD;
+            break;
+        case SkPath::kInverseWinding_FillType:
+            result[SKDEBUGCANVAS_ATTRIBUTE_FILLTYPE] = SKDEBUGCANVAS_FILLTYPE_INVERSEWINDING;
+            break;
+        case SkPath::kInverseEvenOdd_FillType:
+            result[SKDEBUGCANVAS_ATTRIBUTE_FILLTYPE] = SKDEBUGCANVAS_FILLTYPE_INVERSEEVENODD;
+            break;
+    }    
+    Json::Value verbs(Json::arrayValue);
+    SkPath::Iter iter(path, false);
+    SkPoint pts[4];
+    SkPath::Verb verb;
+    while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
+        switch (verb) {
+            case SkPath::kLine_Verb: {
+                Json::Value line(Json::objectValue);
+                line[SKDEBUGCANVAS_VERB_LINE] = make_json_point(pts[1]);
+                verbs.append(line);
+                break;
+            }
+            case SkPath::kQuad_Verb: {
+                Json::Value quad(Json::objectValue);
+                Json::Value coords(Json::arrayValue);
+                coords.append(make_json_point(pts[1]));
+                coords.append(make_json_point(pts[2]));
+                quad[SKDEBUGCANVAS_VERB_QUAD] = coords;
+                verbs.append(quad);
+                break;
+            }
+            case SkPath::kCubic_Verb: {
+                Json::Value cubic(Json::objectValue);
+                Json::Value coords(Json::arrayValue);
+                coords.append(make_json_point(pts[1]));
+                coords.append(make_json_point(pts[2]));
+                coords.append(make_json_point(pts[3]));
+                cubic[SKDEBUGCANVAS_VERB_CUBIC] = coords;
+                verbs.append(cubic);
+                break;
+            }
+            case SkPath::kConic_Verb: {
+                Json::Value conic(Json::objectValue);
+                Json::Value coords(Json::arrayValue);
+                coords.append(make_json_point(pts[1]));
+                coords.append(make_json_point(pts[2]));
+                coords.append(Json::Value(iter.conicWeight()));
+                conic[SKDEBUGCANVAS_VERB_CONIC] = coords;
+                verbs.append(conic);
+                break;
+            }
+            case SkPath::kMove_Verb: {
+                Json::Value move(Json::objectValue);
+                move[SKDEBUGCANVAS_VERB_MOVE] = make_json_point(pts[0]);
+                verbs.append(move);
+                break;
+            }
+            case SkPath::kClose_Verb:
+                verbs.append(Json::Value(SKDEBUGCANVAS_VERB_CLOSE));
+                break;
+            case SkPath::kDone_Verb:
+                break;
+        }
+    }
+    result[SKDEBUGCANVAS_ATTRIBUTE_VERBS] = verbs;
+    return result;
+}
+
+static Json::Value make_json_region(const SkRegion& region) {
+    return Json::Value("<unimplemented>");
+}
+
+static Json::Value make_json_regionop(SkRegion::Op op) {
+    switch (op) {
+        case SkRegion::kDifference_Op:
+            return Json::Value(SKDEBUGCANVAS_REGIONOP_DIFFERENCE);
+        case SkRegion::kIntersect_Op:
+            return Json::Value(SKDEBUGCANVAS_REGIONOP_INTERSECT);
+        case SkRegion::kUnion_Op:
+            return Json::Value(SKDEBUGCANVAS_REGIONOP_UNION);
+        case SkRegion::kXOR_Op:
+            return Json::Value(SKDEBUGCANVAS_REGIONOP_XOR);
+        case SkRegion::kReverseDifference_Op:
+            return Json::Value(SKDEBUGCANVAS_REGIONOP_REVERSE_DIFFERENCE);
+        case SkRegion::kReplace_Op:
+            return Json::Value(SKDEBUGCANVAS_REGIONOP_REPLACE);
+        default:
+            SkASSERT(false);
+            return Json::Value("<invalid region op>");
+    };
+}
+
+static Json::Value make_json_pointmode(SkCanvas::PointMode mode) {
+    switch (mode) {
+        case SkCanvas::kPoints_PointMode:
+            return Json::Value(SKDEBUGCANVAS_POINTMODE_POINTS);
+        case SkCanvas::kLines_PointMode:
+            return Json::Value(SKDEBUGCANVAS_POINTMODE_LINES);
+        case SkCanvas::kPolygon_PointMode: 
+            return Json::Value(SKDEBUGCANVAS_POINTMODE_POLYGON);
+        default:
+            SkASSERT(false);
+            return Json::Value("<invalid point mode>");
+    };
+}
+
+void store_scalar(Json::Value* target, const char* key, SkScalar value, SkScalar defaultValue) {
+    if (value != defaultValue) {
+        (*target)[key] = Json::Value(value);
+    }
+}
+
+void store_bool(Json::Value* target, const char* key, bool value, bool defaultValue) {
+    if (value != defaultValue) {
+        (*target)[key] = Json::Value(value);
+    }
+}
+
+static void encode_data(const void* data, size_t count, Json::Value* target) {
+    // just use a brain-dead JSON array for now, switch to base64 or something else smarter down the
+    // road
+    for (size_t i = 0; i < count; i++) {
+        target->append(((const uint8_t*)data)[i]);
+    }
+}
+
+static void flatten(const SkFlattenable* flattenable, Json::Value* target, bool sendBinaries) {
+    if (sendBinaries) {
+        SkWriteBuffer buffer;
+        flattenable->flatten(buffer);
+        void* data = sk_malloc_throw(buffer.bytesWritten());
+        buffer.writeToMemory(data);
+        Json::Value bytes;
+        encode_data(data, buffer.bytesWritten(), &bytes);
+        Json::Value jsonFlattenable;
+        jsonFlattenable[SKDEBUGCANVAS_ATTRIBUTE_NAME] = Json::Value(flattenable->getTypeName());
+        jsonFlattenable[SKDEBUGCANVAS_ATTRIBUTE_BYTES] = bytes;
+        (*target) = jsonFlattenable;
+        sk_free(data);
+    } else {
+        (*target)[SKDEBUGCANVAS_ATTRIBUTE_DESCRIPTION] = Json::Value(flattenable->getTypeName());
+    }
+}
+
+static bool SK_WARN_UNUSED_RESULT flatten(const SkImage& image, Json::Value* target, 
+                                          bool sendBinaries) {
+    if (sendBinaries) {
+        SkData* encoded = image.encode(SkImageEncoder::kPNG_Type, 100);
+        if (encoded == nullptr) {
+            // PNG encode doesn't necessarily support all color formats, convert to a different
+            // format
+            size_t rowBytes = 4 * image.width();
+            void* buffer = sk_malloc_throw(rowBytes * image.height());
+            SkImageInfo dstInfo = SkImageInfo::Make(image.width(), image.height(), 
+                                                    kN32_SkColorType, kPremul_SkAlphaType);
+            if (!image.readPixels(dstInfo, buffer, rowBytes, 0, 0)) {
+                SkDebugf("readPixels failed\n");
+                return false;
+            }
+            SkImage* converted = SkImage::NewRasterCopy(dstInfo, buffer, rowBytes);
+            encoded = converted->encode(SkImageEncoder::kPNG_Type, 100);
+            if (encoded == nullptr) {
+                SkDebugf("image encode failed\n");
+                return false;
+            }
+            sk_free(converted);
+            sk_free(buffer);
+        }
+        Json::Value bytes;
+        encode_data(encoded->data(), encoded->size(), &bytes);
+        (*target)[SKDEBUGCANVAS_ATTRIBUTE_BYTES] = bytes;
+        encoded->unref();
+    } else {
+        SkString description = SkStringPrintf("%dx%d pixel image", image.width(), image.height());
+        (*target)[SKDEBUGCANVAS_ATTRIBUTE_DESCRIPTION] = Json::Value(description.c_str());
+    }
+    return true;
+}
+
+static const char* color_type_name(SkColorType colorType) {
+    switch (colorType) {
+        case kARGB_4444_SkColorType:
+            return SKDEBUGCANVAS_COLORTYPE_ARGB4444;
+        case kRGBA_8888_SkColorType:
+            return SKDEBUGCANVAS_COLORTYPE_RGBA8888;
+        case kBGRA_8888_SkColorType:
+            return SKDEBUGCANVAS_COLORTYPE_BGRA8888;
+        case kRGB_565_SkColorType:
+            return SKDEBUGCANVAS_COLORTYPE_565;
+        case kGray_8_SkColorType:
+            return SKDEBUGCANVAS_COLORTYPE_GRAY8;
+        case kIndex_8_SkColorType:
+            return SKDEBUGCANVAS_COLORTYPE_INDEX8;
+        case kAlpha_8_SkColorType:
+            return SKDEBUGCANVAS_COLORTYPE_ALPHA8;
+        default:
+            SkASSERT(false);
+            return SKDEBUGCANVAS_COLORTYPE_RGBA8888;
+    }
+}
+
+static const char* alpha_type_name(SkAlphaType alphaType) {
+    switch (alphaType) {
+        case kOpaque_SkAlphaType:
+            return SKDEBUGCANVAS_ALPHATYPE_OPAQUE;
+        case kPremul_SkAlphaType:
+            return SKDEBUGCANVAS_ALPHATYPE_PREMUL;
+        case kUnpremul_SkAlphaType:
+            return SKDEBUGCANVAS_ALPHATYPE_UNPREMUL;
+        default:
+            SkASSERT(false);
+            return SKDEBUGCANVAS_ALPHATYPE_OPAQUE;
+    }
+}
+
+// note that the caller is responsible for freeing the pointer
+static Json::ArrayIndex decode_data(Json::Value bytes, void** target) {
+    Json::ArrayIndex size = bytes.size();
+    *target = sk_malloc_throw(size);
+    for (Json::ArrayIndex i = 0; i < size; i++) {
+        ((uint8_t*) *target)[i] = bytes[i].asInt();
+    }
+    return size;
+}
+
+static SkFlattenable* load_flattenable(Json::Value jsonFlattenable) {
+    if (!jsonFlattenable.isMember(SKDEBUGCANVAS_ATTRIBUTE_NAME)) {
+        return nullptr;
+    }
+    const char* name = jsonFlattenable[SKDEBUGCANVAS_ATTRIBUTE_NAME].asCString();
+    SkFlattenable::Factory factory = SkFlattenable::NameToFactory(name);
+    if (factory == nullptr) {
+        SkDebugf("no factory for loading '%s'\n", name);
+        return nullptr;
+    }
+    void* data;
+    int size = decode_data(jsonFlattenable[SKDEBUGCANVAS_ATTRIBUTE_BYTES], &data);
+    SkValidatingReadBuffer buffer(data, size);
+    SkFlattenable* result = factory(buffer);
+    sk_free(data);
+    if (!buffer.isValid()) {
+        SkDebugf("invalid buffer loading flattenable\n");
+        return nullptr;
+    }
+    return result;
+}
+
+static SkColorType colortype_from_name(const char* name) {
+    if (!strcmp(name, SKDEBUGCANVAS_COLORTYPE_ARGB4444)) {
+        return kARGB_4444_SkColorType;
+    }
+    else if (!strcmp(name, SKDEBUGCANVAS_COLORTYPE_RGBA8888)) {
+        return kRGBA_8888_SkColorType;
+    }
+    else if (!strcmp(name, SKDEBUGCANVAS_COLORTYPE_BGRA8888)) {
+        return kBGRA_8888_SkColorType;
+    }
+    else if (!strcmp(name, SKDEBUGCANVAS_COLORTYPE_565)) {
+        return kRGB_565_SkColorType;
+    }
+    else if (!strcmp(name, SKDEBUGCANVAS_COLORTYPE_GRAY8)) {
+        return kGray_8_SkColorType;
+    }
+    else if (!strcmp(name, SKDEBUGCANVAS_COLORTYPE_INDEX8)) {
+        return kIndex_8_SkColorType;
+    }
+    else if (!strcmp(name, SKDEBUGCANVAS_COLORTYPE_ALPHA8)) {
+        return kAlpha_8_SkColorType;
+    }
+    SkASSERT(false);
+    return kN32_SkColorType;
+}
+
+static SkBitmap* convert_colortype(SkBitmap* bitmap, SkColorType colorType) {
+    if (bitmap->colorType() == colorType  ) {
+        return bitmap;
+    }
+    SkBitmap* dst = new SkBitmap();
+    if (bitmap->copyTo(dst, colorType)) {
+        delete bitmap;
+        return dst;
+    }
+    SkASSERT(false);
+    delete dst;
+    return bitmap;
+}
+
+// caller is responsible for freeing return value
+static SkBitmap* load_bitmap(const Json::Value& jsonBitmap) {
+    if (!jsonBitmap.isMember(SKDEBUGCANVAS_ATTRIBUTE_BYTES)) {
+        SkDebugf("invalid bitmap\n");
+        return nullptr;
+    }
+    void* data;
+    int size = decode_data(jsonBitmap[SKDEBUGCANVAS_ATTRIBUTE_BYTES], &data);
+    SkMemoryStream stream(data, size);
+    SkImageDecoder* decoder = SkImageDecoder::Factory(&stream);
+    SkBitmap* bitmap = new SkBitmap();
+    SkImageDecoder::Result result = decoder->decode(&stream, bitmap, 
+                                                    SkImageDecoder::kDecodePixels_Mode);
+    sk_free(decoder);
+    if (result != SkImageDecoder::kFailure) {
+        sk_free(data);
+        if (jsonBitmap.isMember(SKDEBUGCANVAS_ATTRIBUTE_COLOR)) {
+            const char* ctName = jsonBitmap[SKDEBUGCANVAS_ATTRIBUTE_COLOR].asCString();
+            SkColorType ct = colortype_from_name(ctName);
+            if (ct != kIndex_8_SkColorType) {
+                bitmap = convert_colortype(bitmap, ct);
+            }
+        }
+        return bitmap;
+    }
+    SkDebugf("image decode failed\n");
+    sk_free(data);
+    return nullptr;
+}
+
+static SkImage* load_image(const Json::Value& jsonImage) {
+    SkBitmap* bitmap = load_bitmap(jsonImage);
+    if (bitmap == nullptr) {
+        return nullptr;
+    }
+    SkImage* result = SkImage::NewFromBitmap(*bitmap);
+    delete bitmap;
+    return result;
+}
+
+static bool SK_WARN_UNUSED_RESULT flatten(const SkBitmap& bitmap, Json::Value* target, 
+                                          bool sendBinaries) {
+    bitmap.lockPixels();
+    SkAutoTUnref<SkImage> image(SkImage::NewFromBitmap(bitmap));
+    bitmap.unlockPixels();
+    (*target)[SKDEBUGCANVAS_ATTRIBUTE_COLOR] = Json::Value(color_type_name(bitmap.colorType()));
+    (*target)[SKDEBUGCANVAS_ATTRIBUTE_ALPHA] = Json::Value(alpha_type_name(bitmap.alphaType()));
+    bool success = flatten(*image, target, sendBinaries);
+    return success;
+}
+
+static void apply_paint_color(const SkPaint& paint, Json::Value* target) {
+    SkColor color = paint.getColor();
+    if (color != SK_ColorBLACK) {
+        Json::Value colorValue(Json::arrayValue);
+        colorValue.append(Json::Value(SkColorGetA(color)));
+        colorValue.append(Json::Value(SkColorGetR(color)));
+        colorValue.append(Json::Value(SkColorGetG(color)));
+        colorValue.append(Json::Value(SkColorGetB(color)));
+        (*target)[SKDEBUGCANVAS_ATTRIBUTE_COLOR] = colorValue;;
+    }
+}
+
+static void apply_paint_style(const SkPaint& paint, Json::Value* target) {
+    SkPaint::Style style = paint.getStyle();
+    if (style != SkPaint::kFill_Style) {
+        switch (style) {
+            case SkPaint::kStroke_Style: {
+                Json::Value stroke(SKDEBUGCANVAS_STYLE_STROKE);
+                (*target)[SKDEBUGCANVAS_ATTRIBUTE_STYLE] = stroke;
+                break;
+            }
+            case SkPaint::kStrokeAndFill_Style: {
+                Json::Value strokeAndFill(SKDEBUGCANVAS_STYLE_STROKEANDFILL);
+                (*target)[SKDEBUGCANVAS_ATTRIBUTE_STYLE] = strokeAndFill;
+                break;
+            }
+            default: SkASSERT(false);
+        }
+    }
+}
+
+static void apply_paint_cap(const SkPaint& paint, Json::Value* target) {
+    SkPaint::Cap cap = paint.getStrokeCap();
+    if (cap != SkPaint::kDefault_Cap) {
+        switch (cap) {
+            case SkPaint::kButt_Cap: {
+                (*target)[SKDEBUGCANVAS_ATTRIBUTE_CAP] = Json::Value(SKDEBUGCANVAS_CAP_BUTT);
+                break;
+            }
+            case SkPaint::kRound_Cap: {
+                (*target)[SKDEBUGCANVAS_ATTRIBUTE_CAP] = Json::Value(SKDEBUGCANVAS_CAP_ROUND);
+                break;
+            }
+            case SkPaint::kSquare_Cap: {
+                (*target)[SKDEBUGCANVAS_ATTRIBUTE_CAP] = Json::Value(SKDEBUGCANVAS_CAP_SQUARE);
+                break;
+            }
+            default: SkASSERT(false);
+        }
+    }
+}
+static void apply_paint_maskfilter(const SkPaint& paint, Json::Value* target, bool sendBinaries) {
+    SkMaskFilter* maskFilter = paint.getMaskFilter();
+    if (maskFilter != nullptr) {
+        SkMaskFilter::BlurRec blurRec;
+        if (maskFilter->asABlur(&blurRec)) {
+            Json::Value blur(Json::objectValue);
+            blur[SKDEBUGCANVAS_ATTRIBUTE_SIGMA] = Json::Value(blurRec.fSigma);
+            switch (blurRec.fStyle) {
+                case SkBlurStyle::kNormal_SkBlurStyle:
+                    blur[SKDEBUGCANVAS_ATTRIBUTE_STYLE] = Json::Value(SKDEBUGCANVAS_BLURSTYLE_NORMAL);
+                    break;
+                case SkBlurStyle::kSolid_SkBlurStyle:
+                    blur[SKDEBUGCANVAS_ATTRIBUTE_STYLE] = Json::Value(SKDEBUGCANVAS_BLURSTYLE_SOLID);
+                    break;
+                case SkBlurStyle::kOuter_SkBlurStyle:
+                    blur[SKDEBUGCANVAS_ATTRIBUTE_STYLE] = Json::Value(SKDEBUGCANVAS_BLURSTYLE_OUTER);
+                    break;
+                case SkBlurStyle::kInner_SkBlurStyle:
+                    blur[SKDEBUGCANVAS_ATTRIBUTE_STYLE] = Json::Value(SKDEBUGCANVAS_BLURSTYLE_INNER);
+                    break;
+                default:
+                    SkASSERT(false);
+            }
+            switch (blurRec.fQuality) {
+                case SkBlurQuality::kLow_SkBlurQuality:
+                    blur[SKDEBUGCANVAS_ATTRIBUTE_QUALITY] = Json::Value(SKDEBUGCANVAS_BLURQUALITY_LOW);
+                    break;
+                case SkBlurQuality::kHigh_SkBlurQuality:
+                    blur[SKDEBUGCANVAS_ATTRIBUTE_QUALITY] = Json::Value(SKDEBUGCANVAS_BLURQUALITY_HIGH);
+                    break;
+                default:
+                    SkASSERT(false);
+            }
+            (*target)[SKDEBUGCANVAS_ATTRIBUTE_BLUR] = blur;
+        } else {
+            Json::Value jsonMaskFilter;
+            flatten(maskFilter, &jsonMaskFilter, sendBinaries);
+            (*target)[SKDEBUGCANVAS_ATTRIBUTE_MASKFILTER] = jsonMaskFilter;
+        }
+    }
+}
+
+static void apply_paint_patheffect(const SkPaint& paint, Json::Value* target, bool sendBinaries) {
+    SkPathEffect* pathEffect = paint.getPathEffect();
+    if (pathEffect != nullptr) {
+        SkPathEffect::DashInfo dashInfo;
+        SkPathEffect::DashType dashType = pathEffect->asADash(&dashInfo);
+        if (dashType == SkPathEffect::kDash_DashType) {
+            dashInfo.fIntervals = (SkScalar*) sk_malloc_throw(dashInfo.fCount * sizeof(SkScalar));
+            pathEffect->asADash(&dashInfo);
+            Json::Value dashing(Json::objectValue);
+            Json::Value intervals(Json::arrayValue);
+            for (int32_t i = 0; i < dashInfo.fCount; i++) {
+                intervals.append(Json::Value(dashInfo.fIntervals[i]));
+            }
+            sk_free(dashInfo.fIntervals);
+            dashing[SKDEBUGCANVAS_ATTRIBUTE_INTERVALS] = intervals;
+            dashing[SKDEBUGCANVAS_ATTRIBUTE_PHASE] = dashInfo.fPhase;
+            (*target)[SKDEBUGCANVAS_ATTRIBUTE_DASHING] = dashing;
+        } else {
+            Json::Value jsonPathEffect;
+            flatten(pathEffect, &jsonPathEffect, sendBinaries);
+            (*target)[SKDEBUGCANVAS_ATTRIBUTE_PATHEFFECT] = jsonPathEffect;
+        }
+    }
+}
+    
+static void apply_paint_textalign(const SkPaint& paint, Json::Value* target) {
+    SkPaint::Align textAlign = paint.getTextAlign();
+    if (textAlign != SkPaint::kLeft_Align) {
+        switch (textAlign) {
+            case SkPaint::kCenter_Align: {
+                (*target)[SKDEBUGCANVAS_ATTRIBUTE_TEXTALIGN] = SKDEBUGCANVAS_ALIGN_CENTER;
+                break;
+            }
+            case SkPaint::kRight_Align: {
+                (*target)[SKDEBUGCANVAS_ATTRIBUTE_TEXTALIGN] = SKDEBUGCANVAS_ALIGN_RIGHT;
+                break;
+            }
+            default: SkASSERT(false);
+        }
+    }
+}
+
+static void apply_paint_typeface(const SkPaint& paint, Json::Value* target, 
+                                 bool sendBinaries) {
+    SkTypeface* typeface = paint.getTypeface();
+    if (typeface != nullptr) {
+        if (sendBinaries) {
+            Json::Value jsonTypeface;
+            SkDynamicMemoryWStream buffer;
+            typeface->serialize(&buffer);
+            void* data = sk_malloc_throw(buffer.bytesWritten());
+            buffer.copyTo(data);
+            Json::Value bytes;
+            encode_data(data, buffer.bytesWritten(), &bytes);
+            jsonTypeface[SKDEBUGCANVAS_ATTRIBUTE_BYTES] = bytes;
+            sk_free(data);
+            (*target)[SKDEBUGCANVAS_ATTRIBUTE_TYPEFACE] = jsonTypeface;
+        }
+    }
+}
+
+static void apply_paint_shader(const SkPaint& paint, Json::Value* target, bool sendBinaries) {
+    SkFlattenable* shader = paint.getShader();
+    if (shader != nullptr) {
+        Json::Value jsonShader;
+        flatten(shader, &jsonShader, sendBinaries);
+        (*target)[SKDEBUGCANVAS_ATTRIBUTE_SHADER] = jsonShader;
+    }
+}
+
+static void apply_paint_xfermode(const SkPaint& paint, Json::Value* target, bool sendBinaries) {
+    SkFlattenable* xfermode = paint.getXfermode();
+    if (xfermode != nullptr) {
+        Json::Value jsonXfermode;
+        flatten(xfermode, &jsonXfermode, sendBinaries);
+        (*target)[SKDEBUGCANVAS_ATTRIBUTE_XFERMODE] = jsonXfermode;
+    }
+}
+
+static void apply_paint_imagefilter(const SkPaint& paint, Json::Value* target, bool sendBinaries) {
+    SkFlattenable* imageFilter = paint.getImageFilter();
+    if (imageFilter != nullptr) {
+        Json::Value jsonImageFilter;
+        flatten(imageFilter, &jsonImageFilter, sendBinaries);
+        (*target)[SKDEBUGCANVAS_ATTRIBUTE_IMAGEFILTER] = jsonImageFilter;
+    }
+}
+
+static void apply_paint_colorfilter(const SkPaint& paint, Json::Value* target, bool sendBinaries) {
+    SkFlattenable* colorFilter = paint.getColorFilter();
+    if (colorFilter != nullptr) {
+        Json::Value jsonColorFilter;
+        flatten(colorFilter, &jsonColorFilter, sendBinaries);
+        (*target)[SKDEBUGCANVAS_ATTRIBUTE_COLORFILTER] = jsonColorFilter;
+    }
+}
+
+Json::Value make_json_paint(const SkPaint& paint, bool sendBinaries) {
+    Json::Value result(Json::objectValue);
+    store_scalar(&result, SKDEBUGCANVAS_ATTRIBUTE_STROKEWIDTH, paint.getStrokeWidth(), 0.0f);
+    store_scalar(&result, SKDEBUGCANVAS_ATTRIBUTE_STROKEMITER, paint.getStrokeMiter(), 
+                 SkPaintDefaults_MiterLimit);
+    store_bool(&result, SKDEBUGCANVAS_ATTRIBUTE_ANTIALIAS, paint.isAntiAlias(), false);
+    store_scalar(&result, SKDEBUGCANVAS_ATTRIBUTE_TEXTSIZE, paint.getTextSize(), 
+                 SkPaintDefaults_TextSize);
+    store_scalar(&result, SKDEBUGCANVAS_ATTRIBUTE_TEXTSCALEX, paint.getTextScaleX(), SK_Scalar1);
+    store_scalar(&result, SKDEBUGCANVAS_ATTRIBUTE_TEXTSCALEX, paint.getTextSkewX(), 0.0f);
+    apply_paint_color(paint, &result);
+    apply_paint_style(paint, &result);
+    apply_paint_cap(paint, &result);
+    apply_paint_textalign(paint, &result);
+    apply_paint_patheffect(paint, &result, sendBinaries);
+    apply_paint_maskfilter(paint, &result, sendBinaries);
+    apply_paint_shader(paint, &result, sendBinaries);
+    apply_paint_xfermode(paint, &result, sendBinaries);
+    apply_paint_imagefilter(paint, &result, sendBinaries);
+    apply_paint_colorfilter(paint, &result, sendBinaries);
+    apply_paint_typeface(paint, &result, sendBinaries);
+    return result;
+}
+
+static void extract_json_paint_color(Json::Value& jsonPaint, SkPaint* target) {
+    if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_COLOR)) {
+        Json::Value color = jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_COLOR];
+        target->setColor(SkColorSetARGB(color[0].asInt(), color[1].asInt(), color[2].asInt(),
+                         color[3].asInt()));
+    }
+}
+
+static void extract_json_paint_shader(Json::Value& jsonPaint, SkPaint* target) {
+    if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_SHADER)) {
+        Json::Value jsonShader = jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_SHADER];
+        SkShader* shader = (SkShader*) load_flattenable(jsonShader);
+        if (shader != nullptr) {
+            target->setShader(shader);
+            shader->unref();
+        }
+    }
+}
+
+static void extract_json_paint_patheffect(Json::Value& jsonPaint, SkPaint* target) {
+    if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_PATHEFFECT)) {
+        Json::Value jsonPathEffect = jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_PATHEFFECT];
+        SkPathEffect* pathEffect = (SkPathEffect*) load_flattenable(jsonPathEffect);
+        if (pathEffect != nullptr) {
+            target->setPathEffect(pathEffect);
+            pathEffect->unref();
+        }
+    }
+}
+
+static void extract_json_paint_maskfilter(Json::Value& jsonPaint, SkPaint* target) {
+    if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_MASKFILTER)) {
+        Json::Value jsonMaskFilter = jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_MASKFILTER];
+        SkMaskFilter* maskFilter = (SkMaskFilter*) load_flattenable(jsonMaskFilter);
+        if (maskFilter != nullptr) {
+            target->setMaskFilter(maskFilter);
+            maskFilter->unref();
+        }
+    }
+}
+
+static void extract_json_paint_colorfilter(Json::Value& jsonPaint, SkPaint* target) {
+    if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_COLORFILTER)) {
+        Json::Value jsonColorFilter = jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_COLORFILTER];
+        SkColorFilter* colorFilter = (SkColorFilter*) load_flattenable(jsonColorFilter);
+        if (colorFilter != nullptr) {
+            target->setColorFilter(colorFilter);
+            colorFilter->unref();
+        }
+    }
+}
+
+static void extract_json_paint_xfermode(Json::Value& jsonPaint, SkPaint* target) {
+    if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_XFERMODE)) {
+        Json::Value jsonXfermode = jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_XFERMODE];
+        SkXfermode* xfermode = (SkXfermode*) load_flattenable(jsonXfermode);
+        if (xfermode != nullptr) {
+            target->setXfermode(xfermode);
+            xfermode->unref();
+        }
+    }
+}
+
+static void extract_json_paint_imagefilter(Json::Value& jsonPaint, SkPaint* target) {
+    if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_IMAGEFILTER)) {
+        Json::Value jsonImageFilter = jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_IMAGEFILTER];
+        SkImageFilter* imageFilter = (SkImageFilter*) load_flattenable(jsonImageFilter);
+        if (imageFilter != nullptr) {
+            target->setImageFilter(imageFilter);
+            imageFilter->unref();
+        }
+    }
+}
+
+static void extract_json_paint_style(Json::Value& jsonPaint, SkPaint* target) {
+    if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_STYLE)) {
+        const char* style = jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_STYLE].asCString();
+        if (!strcmp(style, SKDEBUGCANVAS_STYLE_FILL)) {
+            target->setStyle(SkPaint::kFill_Style);
+        }
+        else if (!strcmp(style, SKDEBUGCANVAS_STYLE_STROKE)) {
+            target->setStyle(SkPaint::kStroke_Style);
+        }
+        else if (!strcmp(style, SKDEBUGCANVAS_STYLE_STROKEANDFILL)) {
+            target->setStyle(SkPaint::kStrokeAndFill_Style);
+        }
+    }
+}
+
+static void extract_json_paint_strokewidth(Json::Value& jsonPaint, SkPaint* target) {
+    if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_STROKEWIDTH)) {
+        float strokeWidth = jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_STROKEWIDTH].asFloat();
+        target->setStrokeWidth(strokeWidth);
+    }    
+}
+
+static void extract_json_paint_strokemiter(Json::Value& jsonPaint, SkPaint* target) {
+    if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_STROKEMITER)) {
+        float strokeMiter = jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_STROKEMITER].asFloat();
+        target->setStrokeMiter(strokeMiter);
+    }    
+}
+
+static void extract_json_paint_cap(Json::Value& jsonPaint, SkPaint* target) {
+    if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_CAP)) {
+        const char* cap = jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_CAP].asCString();
+        if (!strcmp(cap, SKDEBUGCANVAS_CAP_BUTT)) {
+            target->setStrokeCap(SkPaint::kButt_Cap);
+        }
+        else if (!strcmp(cap, SKDEBUGCANVAS_CAP_ROUND)) {
+            target->setStrokeCap(SkPaint::kRound_Cap);
+        }
+        else if (!strcmp(cap, SKDEBUGCANVAS_CAP_SQUARE)) {
+            target->setStrokeCap(SkPaint::kSquare_Cap);
+        }
+    }
+}
+
+static void extract_json_paint_antialias(Json::Value& jsonPaint, SkPaint* target) {
+    if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_ANTIALIAS)) {
+        target->setAntiAlias(jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_ANTIALIAS].asBool());
+    }
+}
+
+static void extract_json_paint_blur(Json::Value& jsonPaint, SkPaint* target) {
+    if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_BLUR)) {
+        Json::Value blur = jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_BLUR];
+        SkScalar sigma = blur[SKDEBUGCANVAS_ATTRIBUTE_SIGMA].asFloat();
+        SkBlurStyle style;
+        const char* jsonStyle = blur[SKDEBUGCANVAS_ATTRIBUTE_STYLE].asCString();
+        if (!strcmp(jsonStyle, SKDEBUGCANVAS_BLURSTYLE_NORMAL)) {
+            style = SkBlurStyle::kNormal_SkBlurStyle;
+        }
+        else if (!strcmp(jsonStyle, SKDEBUGCANVAS_BLURSTYLE_SOLID)) {
+            style = SkBlurStyle::kSolid_SkBlurStyle;
+        }
+        else if (!strcmp(jsonStyle, SKDEBUGCANVAS_BLURSTYLE_OUTER)) {
+            style = SkBlurStyle::kOuter_SkBlurStyle;
+        }
+        else if (!strcmp(jsonStyle, SKDEBUGCANVAS_BLURSTYLE_INNER)) {
+            style = SkBlurStyle::kInner_SkBlurStyle;
+        }
+        else {
+            SkASSERT(false);
+            style = SkBlurStyle::kNormal_SkBlurStyle;
+        }
+        SkBlurMaskFilter::BlurFlags flags;
+        const char* jsonQuality = blur[SKDEBUGCANVAS_ATTRIBUTE_QUALITY].asCString();
+        if (!strcmp(jsonQuality, SKDEBUGCANVAS_BLURQUALITY_LOW)) {
+            flags = SkBlurMaskFilter::BlurFlags::kNone_BlurFlag;
+        }
+        else if (!strcmp(jsonQuality, SKDEBUGCANVAS_BLURQUALITY_HIGH)) {
+            flags = SkBlurMaskFilter::BlurFlags::kHighQuality_BlurFlag;
+        }
+        else {
+            SkASSERT(false);
+            flags = SkBlurMaskFilter::BlurFlags::kNone_BlurFlag;
+        }
+        target->setMaskFilter(SkBlurMaskFilter::Create(style, sigma, flags));
+    }
+}
+
+static void extract_json_paint_dashing(Json::Value& jsonPaint, SkPaint* target) {
+    if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_DASHING)) {
+        Json::Value dash = jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_DASHING];
+        Json::Value jsonIntervals = dash[SKDEBUGCANVAS_ATTRIBUTE_INTERVALS];
+        Json::ArrayIndex count = jsonIntervals.size();
+        SkScalar* intervals = (SkScalar*) sk_malloc_throw(count * sizeof(SkScalar));
+        for (Json::ArrayIndex i = 0; i < count; i++) {
+            intervals[i] = jsonIntervals[i].asFloat();
+        }
+        SkScalar phase = dash[SKDEBUGCANVAS_ATTRIBUTE_PHASE].asFloat();
+        target->setPathEffect(SkDashPathEffect::Create(intervals, count, phase));
+        sk_free(intervals);
+    }
+}
+
+static void extract_json_paint_textalign(Json::Value& jsonPaint, SkPaint* target) {
+    if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_TEXTALIGN)) {
+        SkPaint::Align textAlign;
+        const char* jsonAlign = jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_TEXTALIGN].asCString();
+        if (!strcmp(jsonAlign, SKDEBUGCANVAS_ALIGN_LEFT)) {
+            textAlign = SkPaint::kLeft_Align;
+        }
+        else if (!strcmp(jsonAlign, SKDEBUGCANVAS_ALIGN_CENTER)) {
+            textAlign = SkPaint::kCenter_Align;
+        }
+        else if (!strcmp(jsonAlign, SKDEBUGCANVAS_ALIGN_RIGHT)) {
+            textAlign = SkPaint::kRight_Align;
+        }
+        else {
+            SkASSERT(false);
+            textAlign = SkPaint::kLeft_Align;
+        }
+        target->setTextAlign(textAlign);
+    }
+}
+
+static void extract_json_paint_textsize(Json::Value& jsonPaint, SkPaint* target) {
+    if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_TEXTSIZE)) {
+        float textSize = jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_TEXTSIZE].asFloat();
+        target->setTextSize(textSize);
+    }
+}
+
+static void extract_json_paint_textscalex(Json::Value& jsonPaint, SkPaint* target) {
+    if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_TEXTSCALEX)) {
+        float textScaleX = jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_TEXTSCALEX].asFloat();
+        target->setTextScaleX(textScaleX);
+    }
+}
+
+static void extract_json_paint_textskewx(Json::Value& jsonPaint, SkPaint* target) {
+    if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_TEXTSKEWX)) {
+        float textSkewX = jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_TEXTSKEWX].asFloat();
+        target->setTextSkewX(textSkewX);
+    }
+}
+
+static void extract_json_paint_typeface(Json::Value& jsonPaint, SkPaint* target) {
+    if (jsonPaint.isMember(SKDEBUGCANVAS_ATTRIBUTE_TYPEFACE)) {
+        Json::Value jsonTypeface = jsonPaint[SKDEBUGCANVAS_ATTRIBUTE_TYPEFACE];
+        Json::Value bytes = jsonTypeface[SKDEBUGCANVAS_ATTRIBUTE_BYTES];
+        void* data;
+        Json::ArrayIndex length = decode_data(bytes, &data);
+        SkMemoryStream buffer(data, length);
+        SkTypeface* typeface = SkTypeface::Deserialize(&buffer);
+        sk_free(data);
+        target->setTypeface(typeface);
+    }
+}
+
+static void extract_json_paint(Json::Value& paint, SkPaint* result) {
+    extract_json_paint_color(paint, result);
+    extract_json_paint_shader(paint, result);
+    extract_json_paint_patheffect(paint, result);
+    extract_json_paint_maskfilter(paint, result);
+    extract_json_paint_colorfilter(paint, result);
+    extract_json_paint_xfermode(paint, result);
+    extract_json_paint_imagefilter(paint, result);
+    extract_json_paint_style(paint, result);
+    extract_json_paint_strokewidth(paint, result);
+    extract_json_paint_strokemiter(paint, result);
+    extract_json_paint_cap(paint, result);
+    extract_json_paint_antialias(paint, result);
+    extract_json_paint_blur(paint, result);
+    extract_json_paint_dashing(paint, result);
+    extract_json_paint_textalign(paint, result);
+    extract_json_paint_textsize(paint, result);
+    extract_json_paint_textscalex(paint, result);
+    extract_json_paint_textskewx(paint, result);
+    extract_json_paint_typeface(paint, result);
+}
+
+static void extract_json_rect(Json::Value& rect, SkRect* result) {
+    result->set(rect[0].asFloat(), rect[1].asFloat(), rect[2].asFloat(), rect[3].asFloat());
+}
+
+static void extract_json_irect(Json::Value& rect, SkIRect* result) {
+    result->set(rect[0].asInt(), rect[1].asInt(), rect[2].asInt(), rect[3].asInt());
+}
+
+static void extract_json_rrect(Json::Value& rrect, SkRRect* result) {
+    SkVector radii[4] = {
+                            { rrect[1][0].asFloat(), rrect[1][1].asFloat() }, 
+                            { rrect[2][0].asFloat(), rrect[2][1].asFloat() }, 
+                            { rrect[3][0].asFloat(), rrect[3][1].asFloat() }, 
+                            { rrect[4][0].asFloat(), rrect[4][1].asFloat() }
+                        };
+    result->setRectRadii(SkRect::MakeLTRB(rrect[0][0].asFloat(), rrect[0][1].asFloat(), 
+                                          rrect[0][2].asFloat(), rrect[0][3].asFloat()), 
+                                          radii);
+}
+
+static void extract_json_matrix(Json::Value& matrix, SkMatrix* result) {
+    SkScalar values[] = { 
+        matrix[0][0].asFloat(), matrix[0][1].asFloat(), matrix[0][2].asFloat(),
+        matrix[1][0].asFloat(), matrix[1][1].asFloat(), matrix[1][2].asFloat(),
+        matrix[2][0].asFloat(), matrix[2][1].asFloat(), matrix[2][2].asFloat() 
+    };
+    result->set9(values);
+}
+
+static void extract_json_path(Json::Value& path, SkPath* result) {
+    const char* fillType = path[SKDEBUGCANVAS_ATTRIBUTE_FILLTYPE].asCString();
+    if (!strcmp(fillType, SKDEBUGCANVAS_FILLTYPE_WINDING)) {
+        result->setFillType(SkPath::kWinding_FillType);
+    }
+    else if (!strcmp(fillType, SKDEBUGCANVAS_FILLTYPE_EVENODD)) {
+        result->setFillType(SkPath::kEvenOdd_FillType);
+    }
+    else if (!strcmp(fillType, SKDEBUGCANVAS_FILLTYPE_INVERSEWINDING)) {
+        result->setFillType(SkPath::kInverseWinding_FillType);
+    }
+    else if (!strcmp(fillType, SKDEBUGCANVAS_FILLTYPE_INVERSEEVENODD)) {
+        result->setFillType(SkPath::kInverseEvenOdd_FillType);
+    }
+    Json::Value verbs = path[SKDEBUGCANVAS_ATTRIBUTE_VERBS];
+    for (Json::ArrayIndex i = 0; i < verbs.size(); i++) {
+        Json::Value verb = verbs[i];
+        if (verb.isString()) {
+            SkASSERT(!strcmp(verb.asCString(), SKDEBUGCANVAS_VERB_CLOSE));
+            result->close();
+        }
+        else {
+            if (verb.isMember(SKDEBUGCANVAS_VERB_MOVE)) {
+                Json::Value move = verb[SKDEBUGCANVAS_VERB_MOVE];
+                result->moveTo(move[0].asFloat(), move[1].asFloat());
+            }
+            else if (verb.isMember(SKDEBUGCANVAS_VERB_LINE)) {
+                Json::Value line = verb[SKDEBUGCANVAS_VERB_LINE];
+                result->lineTo(line[0].asFloat(), line[1].asFloat());
+            }
+            else if (verb.isMember(SKDEBUGCANVAS_VERB_QUAD)) {
+                Json::Value quad = verb[SKDEBUGCANVAS_VERB_QUAD];
+                result->quadTo(quad[0][0].asFloat(), quad[0][1].asFloat(),
+                               quad[1][0].asFloat(), quad[1][1].asFloat());
+            }
+            else if (verb.isMember(SKDEBUGCANVAS_VERB_CUBIC)) {
+                Json::Value cubic = verb[SKDEBUGCANVAS_VERB_CUBIC];
+                result->cubicTo(cubic[0][0].asFloat(), cubic[0][1].asFloat(),
+                                cubic[1][0].asFloat(), cubic[1][1].asFloat(),
+                                cubic[2][0].asFloat(), cubic[2][1].asFloat());
+            }
+            else if (verb.isMember(SKDEBUGCANVAS_VERB_CONIC)) {
+                Json::Value conic = verb[SKDEBUGCANVAS_VERB_CONIC];
+                result->conicTo(conic[0][0].asFloat(), conic[0][1].asFloat(),
+                                conic[1][0].asFloat(), conic[1][1].asFloat(),
+                                conic[2].asFloat());
+            }
+            else {
+                SkASSERT(false);
+            }
+        }
+    }
+}
+
+SkRegion::Op get_json_regionop(Json::Value& jsonOp) {
+    const char* op = jsonOp.asCString();
+    if (!strcmp(op, SKDEBUGCANVAS_REGIONOP_DIFFERENCE)) {
+        return SkRegion::kDifference_Op;
+    }
+    else if (!strcmp(op, SKDEBUGCANVAS_REGIONOP_INTERSECT)) {
+        return SkRegion::kIntersect_Op;
+    }
+    else if (!strcmp(op, SKDEBUGCANVAS_REGIONOP_UNION)) {
+        return SkRegion::kUnion_Op;
+    }
+    else if (!strcmp(op, SKDEBUGCANVAS_REGIONOP_XOR)) {
+        return SkRegion::kXOR_Op;
+    }
+    else if (!strcmp(op, SKDEBUGCANVAS_REGIONOP_REVERSE_DIFFERENCE)) {
+        return SkRegion::kReverseDifference_Op;
+    }
+    else if (!strcmp(op, SKDEBUGCANVAS_REGIONOP_REPLACE)) {
+        return SkRegion::kReplace_Op;
+    }
+    SkASSERT(false);
+    return SkRegion::kIntersect_Op;
+}
+
 
 SkClipPathCommand::SkClipPathCommand(const SkPath& path, SkRegion::Op op, bool doAA)
     : INHERITED(kClipPath_OpType) {
@@ -221,6 +1395,21 @@ bool SkClipPathCommand::render(SkCanvas* canvas) const {
     return true;
 }
 
+Json::Value SkClipPathCommand::toJSON() const {
+    Json::Value result = INHERITED::toJSON();
+    result[SKDEBUGCANVAS_ATTRIBUTE_PATH] = make_json_path(fPath);
+    result[SKDEBUGCANVAS_ATTRIBUTE_REGIONOP] = make_json_regionop(fOp);
+    result[SKDEBUGCANVAS_ATTRIBUTE_ANTIALIAS] = fDoAA;
+    return result;
+}
+
+SkClipPathCommand* SkClipPathCommand::fromJSON(Json::Value& command) {
+    SkPath path;
+    extract_json_path(command[SKDEBUGCANVAS_ATTRIBUTE_PATH], &path);
+    return new SkClipPathCommand(path, get_json_regionop(command[SKDEBUGCANVAS_ATTRIBUTE_REGIONOP]), 
+                                 command[SKDEBUGCANVAS_ATTRIBUTE_ANTIALIAS].asBool());
+}
+
 SkClipRegionCommand::SkClipRegionCommand(const SkRegion& region, SkRegion::Op op)
     : INHERITED(kClipRegion_OpType) {
     fRegion = region;
@@ -234,6 +1423,18 @@ void SkClipRegionCommand::execute(SkCanvas* canvas) const {
     canvas->clipRegion(fRegion, fOp);
 }
 
+Json::Value SkClipRegionCommand::toJSON() const {
+    Json::Value result = INHERITED::toJSON();
+    result[SKDEBUGCANVAS_ATTRIBUTE_REGION] = make_json_region(fRegion);
+    result[SKDEBUGCANVAS_ATTRIBUTE_REGIONOP] = make_json_regionop(fOp);
+    return result;
+}
+
+SkClipRegionCommand* SkClipRegionCommand::fromJSON(Json::Value& command) {
+    SkASSERT(false);
+    return nullptr;
+}
+
 SkClipRectCommand::SkClipRectCommand(const SkRect& rect, SkRegion::Op op, bool doAA)
     : INHERITED(kClipRect_OpType) {
     fRect = rect;
@@ -249,6 +1450,21 @@ void SkClipRectCommand::execute(SkCanvas* canvas) const {
     canvas->clipRect(fRect, fOp, fDoAA);
 }
 
+Json::Value SkClipRectCommand::toJSON() const {
+    Json::Value result = INHERITED::toJSON();
+    result[SKDEBUGCANVAS_ATTRIBUTE_COORDS] = make_json_rect(fRect);
+    result[SKDEBUGCANVAS_ATTRIBUTE_REGIONOP] = make_json_regionop(fOp);
+    result[SKDEBUGCANVAS_ATTRIBUTE_ANTIALIAS] = Json::Value(fDoAA);
+    return result;
+}
+
+SkClipRectCommand* SkClipRectCommand::fromJSON(Json::Value& command) {
+    SkRect rect;
+    extract_json_rect(command[SKDEBUGCANVAS_ATTRIBUTE_COORDS], &rect);
+    return new SkClipRectCommand(rect, get_json_regionop(command[SKDEBUGCANVAS_ATTRIBUTE_REGIONOP]), 
+                                 command[SKDEBUGCANVAS_ATTRIBUTE_ANTIALIAS].asBool());
+}
+
 SkClipRRectCommand::SkClipRRectCommand(const SkRRect& rrect, SkRegion::Op op, bool doAA)
     : INHERITED(kClipRRect_OpType) {
     fRRect = rrect;
@@ -269,6 +1485,22 @@ bool SkClipRRectCommand::render(SkCanvas* canvas) const {
     return true;
 }
 
+Json::Value SkClipRRectCommand::toJSON() const {
+    Json::Value result = INHERITED::toJSON();
+    result[SKDEBUGCANVAS_ATTRIBUTE_COORDS] = make_json_rrect(fRRect);
+    result[SKDEBUGCANVAS_ATTRIBUTE_REGIONOP] = make_json_regionop(fOp);
+    result[SKDEBUGCANVAS_ATTRIBUTE_ANTIALIAS] = Json::Value(fDoAA);
+    return result;
+}
+
+SkClipRRectCommand* SkClipRRectCommand::fromJSON(Json::Value& command) {
+    SkRRect rrect;
+    extract_json_rrect(command[SKDEBUGCANVAS_ATTRIBUTE_COORDS], &rrect);
+    return new SkClipRRectCommand(rrect, 
+                                  get_json_regionop(command[SKDEBUGCANVAS_ATTRIBUTE_REGIONOP]), 
+                                  command[SKDEBUGCANVAS_ATTRIBUTE_ANTIALIAS].asBool());
+}
+
 SkConcatCommand::SkConcatCommand(const SkMatrix& matrix)
     : INHERITED(kConcat_OpType) {
     fMatrix = matrix;
@@ -280,6 +1512,18 @@ void SkConcatCommand::execute(SkCanvas* canvas) const {
     canvas->concat(fMatrix);
 }
 
+Json::Value SkConcatCommand::toJSON() const {
+    Json::Value result = INHERITED::toJSON();
+    result[SKDEBUGCANVAS_ATTRIBUTE_MATRIX] = make_json_matrix(fMatrix);
+    return result;
+}
+
+SkConcatCommand* SkConcatCommand::fromJSON(Json::Value& command) {
+    SkMatrix matrix;
+    extract_json_matrix(command[SKDEBUGCANVAS_ATTRIBUTE_MATRIX], &matrix);
+    return new SkConcatCommand(matrix);
+}
+
 SkDrawBitmapCommand::SkDrawBitmapCommand(const SkBitmap& bitmap, SkScalar left, SkScalar top,
                                          const SkPaint* paint)
     : INHERITED(kDrawBitmap_OpType) {
@@ -310,6 +1554,41 @@ bool SkDrawBitmapCommand::render(SkCanvas* canvas) const {
     return true;
 }
 
+Json::Value SkDrawBitmapCommand::toJSON() const {
+    Json::Value result = INHERITED::toJSON();
+    Json::Value encoded;
+    if (flatten(fBitmap, &encoded, SKDEBUGCANVAS_SEND_BINARIES)) {
+        Json::Value command(Json::objectValue);
+        result[SKDEBUGCANVAS_ATTRIBUTE_BITMAP] = encoded;
+        result[SKDEBUGCANVAS_ATTRIBUTE_COORDS] = make_json_point(fLeft, fTop);
+        if (fPaintPtr != nullptr) {
+            result[SKDEBUGCANVAS_ATTRIBUTE_PAINT] = make_json_paint(*fPaintPtr, SKDEBUGCANVAS_SEND_BINARIES);
+        }
+    }
+    return result;
+}
+
+SkDrawBitmapCommand* SkDrawBitmapCommand::fromJSON(Json::Value& command) {
+    SkBitmap* bitmap = load_bitmap(command[SKDEBUGCANVAS_ATTRIBUTE_BITMAP]);
+    if (bitmap == nullptr) {
+        return nullptr;
+    }
+    Json::Value point = command[SKDEBUGCANVAS_ATTRIBUTE_COORDS];
+    SkPaint* paintPtr;
+    SkPaint paint;
+    if (command.isMember(SKDEBUGCANVAS_ATTRIBUTE_PAINT)) {
+        extract_json_paint(command[SKDEBUGCANVAS_ATTRIBUTE_PAINT], &paint);
+        paintPtr = &paint;
+    }
+    else {
+        paintPtr = nullptr;
+    }
+    SkDrawBitmapCommand* result = new SkDrawBitmapCommand(*bitmap, point[0].asFloat(), 
+                                                          point[1].asFloat(), paintPtr);
+    delete bitmap;
+    return result;
+}
+
 SkDrawBitmapNineCommand::SkDrawBitmapNineCommand(const SkBitmap& bitmap, const SkIRect& center,
                                                  const SkRect& dst, const SkPaint* paint)
     : INHERITED(kDrawBitmapNine_OpType) {
@@ -341,6 +1620,44 @@ bool SkDrawBitmapNineCommand::render(SkCanvas* canvas) const {
     return true;
 }
 
+Json::Value SkDrawBitmapNineCommand::toJSON() const {
+    Json::Value result = INHERITED::toJSON();
+    Json::Value encoded;
+    if (flatten(fBitmap, &encoded, SKDEBUGCANVAS_SEND_BINARIES)) {
+        result[SKDEBUGCANVAS_ATTRIBUTE_BITMAP] = encoded;
+        result[SKDEBUGCANVAS_ATTRIBUTE_CENTER] = make_json_irect(fCenter);
+        result[SKDEBUGCANVAS_ATTRIBUTE_DST] = make_json_rect(fDst);
+        if (fPaintPtr != nullptr) {
+            result[SKDEBUGCANVAS_ATTRIBUTE_PAINT] = make_json_paint(*fPaintPtr,
+                                                                    SKDEBUGCANVAS_SEND_BINARIES);
+        }
+    }
+    return result;
+}
+
+SkDrawBitmapNineCommand* SkDrawBitmapNineCommand::fromJSON(Json::Value& command) {
+    SkBitmap* bitmap = load_bitmap(command[SKDEBUGCANVAS_ATTRIBUTE_BITMAP]);
+    if (bitmap == nullptr) {
+        return nullptr;
+    }
+    SkIRect center;
+    extract_json_irect(command[SKDEBUGCANVAS_ATTRIBUTE_CENTER], &center);
+    SkRect dst;
+    extract_json_rect(command[SKDEBUGCANVAS_ATTRIBUTE_DST], &dst);
+    SkPaint* paintPtr;
+    SkPaint paint;
+    if (command.isMember(SKDEBUGCANVAS_ATTRIBUTE_PAINT)) {
+        extract_json_paint(command[SKDEBUGCANVAS_ATTRIBUTE_PAINT], &paint);
+        paintPtr = &paint;
+    }
+    else {
+        paintPtr = nullptr;
+    }
+    SkDrawBitmapNineCommand* result = new SkDrawBitmapNineCommand(*bitmap, center, dst, paintPtr);
+    delete bitmap;
+    return result;
+}
+
 SkDrawBitmapRectCommand::SkDrawBitmapRectCommand(const SkBitmap& bitmap, const SkRect* src,
                                                  const SkRect& dst, const SkPaint* paint,
                                                  SkCanvas::SrcRectConstraint constraint)
@@ -381,6 +1698,65 @@ bool SkDrawBitmapRectCommand::render(SkCanvas* canvas) const {
     return true;
 }
 
+Json::Value SkDrawBitmapRectCommand::toJSON() const {
+    Json::Value result = INHERITED::toJSON();
+    Json::Value encoded;
+    if (flatten(fBitmap, &encoded, SKDEBUGCANVAS_SEND_BINARIES)) {
+        result[SKDEBUGCANVAS_ATTRIBUTE_BITMAP] = encoded;
+        if (!fSrc.isEmpty()) {
+            result[SKDEBUGCANVAS_ATTRIBUTE_SRC] = make_json_rect(fSrc);
+        }
+        result[SKDEBUGCANVAS_ATTRIBUTE_DST] = make_json_rect(fDst);
+        if (fPaintPtr != nullptr) {
+            result[SKDEBUGCANVAS_ATTRIBUTE_PAINT] = make_json_paint(*fPaintPtr,
+                                                                    SKDEBUGCANVAS_SEND_BINARIES);
+        }
+        if (fConstraint == SkCanvas::kStrict_SrcRectConstraint) {
+            result[SKDEBUGCANVAS_ATTRIBUTE_STRICT] = Json::Value(true);
+        }
+    }
+    return result;
+}
+
+SkDrawBitmapRectCommand* SkDrawBitmapRectCommand::fromJSON(Json::Value& command) {
+    SkBitmap* bitmap = load_bitmap(command[SKDEBUGCANVAS_ATTRIBUTE_BITMAP]);
+    if (bitmap == nullptr) {
+        return nullptr;
+    }
+    SkRect dst;
+    extract_json_rect(command[SKDEBUGCANVAS_ATTRIBUTE_DST], &dst);
+    SkPaint* paintPtr;
+    SkPaint paint;
+    if (command.isMember(SKDEBUGCANVAS_ATTRIBUTE_PAINT)) {
+        extract_json_paint(command[SKDEBUGCANVAS_ATTRIBUTE_PAINT], &paint);
+        paintPtr = &paint;
+    }
+    else {
+        paintPtr = nullptr;
+    }
+    SkCanvas::SrcRectConstraint constraint;
+    if (command.isMember(SKDEBUGCANVAS_ATTRIBUTE_STRICT) && 
+        command[SKDEBUGCANVAS_ATTRIBUTE_STRICT].asBool()) {
+        constraint = SkCanvas::kStrict_SrcRectConstraint;
+    }
+    else {
+        constraint = SkCanvas::kFast_SrcRectConstraint;
+    }
+    SkRect* srcPtr;
+    SkRect src;
+    if (command.isMember(SKDEBUGCANVAS_ATTRIBUTE_SRC)) {
+        extract_json_rect(command[SKDEBUGCANVAS_ATTRIBUTE_SRC], &src);
+        srcPtr = &src;
+    }
+    else {
+        srcPtr = nullptr;
+    }
+    SkDrawBitmapRectCommand* result = new SkDrawBitmapRectCommand(*bitmap, srcPtr, dst, paintPtr,
+                                                                  constraint);
+    delete bitmap;
+    return result;
+}
+
 SkDrawImageCommand::SkDrawImageCommand(const SkImage* image, SkScalar left, SkScalar top,
                                        const SkPaint* paint)
     : INHERITED(kDrawImage_OpType)
@@ -413,6 +1789,41 @@ bool SkDrawImageCommand::render(SkCanvas* canvas) const {
     return true;
 }
 
+Json::Value SkDrawImageCommand::toJSON() const {
+    Json::Value result = INHERITED::toJSON();
+    Json::Value encoded;
+    if (flatten(*fImage, &encoded, SKDEBUGCANVAS_SEND_BINARIES)) {
+        result[SKDEBUGCANVAS_ATTRIBUTE_IMAGE] = encoded;
+        result[SKDEBUGCANVAS_ATTRIBUTE_COORDS] = make_json_point(fLeft, fTop);
+        if (fPaint.isValid()) {
+            result[SKDEBUGCANVAS_ATTRIBUTE_PAINT] = make_json_paint(*fPaint.get(),
+                                                                    SKDEBUGCANVAS_SEND_BINARIES);
+        }
+    }
+    return result;
+}
+
+SkDrawImageCommand* SkDrawImageCommand::fromJSON(Json::Value& command) {
+    SkImage* image = load_image(command[SKDEBUGCANVAS_ATTRIBUTE_IMAGE]);
+    if (image == nullptr) {
+        return nullptr;
+    }
+    Json::Value point = command[SKDEBUGCANVAS_ATTRIBUTE_COORDS];
+    SkPaint* paintPtr;
+    SkPaint paint;
+    if (command.isMember(SKDEBUGCANVAS_ATTRIBUTE_PAINT)) {
+        extract_json_paint(command[SKDEBUGCANVAS_ATTRIBUTE_PAINT], &paint);
+        paintPtr = &paint;
+    }
+    else {
+        paintPtr = nullptr;
+    }
+    SkDrawImageCommand* result = new SkDrawImageCommand(image, point[0].asFloat(), 
+                                                        point[1].asFloat(), paintPtr);
+    image->unref();
+    return result;
+}
+
 SkDrawImageRectCommand::SkDrawImageRectCommand(const SkImage* image, const SkRect* src,
                                                const SkRect& dst, const SkPaint* paint,
                                                SkCanvas::SrcRectConstraint constraint)
@@ -454,6 +1865,65 @@ bool SkDrawImageRectCommand::render(SkCanvas* canvas) const {
     return true;
 }
 
+Json::Value SkDrawImageRectCommand::toJSON() const {
+    Json::Value result = INHERITED::toJSON();
+    Json::Value encoded;
+    if (flatten(*fImage.get(), &encoded, SKDEBUGCANVAS_SEND_BINARIES)) {
+        result[SKDEBUGCANVAS_ATTRIBUTE_BITMAP] = encoded;
+        if (fSrc.isValid()) {
+            result[SKDEBUGCANVAS_ATTRIBUTE_SRC] = make_json_rect(*fSrc.get());
+        }
+        result[SKDEBUGCANVAS_ATTRIBUTE_DST] = make_json_rect(fDst);
+        if (fPaint.isValid()) {
+            result[SKDEBUGCANVAS_ATTRIBUTE_PAINT] = make_json_paint(*fPaint.get(),
+                                                                    SKDEBUGCANVAS_SEND_BINARIES);
+        }
+        if (fConstraint == SkCanvas::kStrict_SrcRectConstraint) {
+            result[SKDEBUGCANVAS_ATTRIBUTE_STRICT] = Json::Value(true);
+        }
+    }
+    return result;
+}
+
+SkDrawImageRectCommand* SkDrawImageRectCommand::fromJSON(Json::Value& command) {
+    SkImage* image = load_image(command[SKDEBUGCANVAS_ATTRIBUTE_IMAGE]);
+    if (image == nullptr) {
+        return nullptr;
+    }
+    SkRect dst;
+    extract_json_rect(command[SKDEBUGCANVAS_ATTRIBUTE_DST], &dst);
+    SkPaint* paintPtr;
+    SkPaint paint;
+    if (command.isMember(SKDEBUGCANVAS_ATTRIBUTE_PAINT)) {
+        extract_json_paint(command[SKDEBUGCANVAS_ATTRIBUTE_PAINT], &paint);
+        paintPtr = &paint;
+    }
+    else {
+        paintPtr = nullptr;
+    }
+    SkCanvas::SrcRectConstraint constraint;
+    if (command.isMember(SKDEBUGCANVAS_ATTRIBUTE_STRICT) && 
+        command[SKDEBUGCANVAS_ATTRIBUTE_STRICT].asBool()) {
+        constraint = SkCanvas::kStrict_SrcRectConstraint;
+    }
+    else {
+        constraint = SkCanvas::kFast_SrcRectConstraint;
+    }
+    SkRect* srcPtr;
+    SkRect src;
+    if (command.isMember(SKDEBUGCANVAS_ATTRIBUTE_SRC)) {
+        extract_json_rect(command[SKDEBUGCANVAS_ATTRIBUTE_SRC], &src);
+        srcPtr = &src;
+    }
+    else {
+        srcPtr = nullptr;
+    }
+    SkDrawImageRectCommand* result = new SkDrawImageRectCommand(image, srcPtr, dst, paintPtr, 
+                                                                constraint);
+    image->unref();
+    return result;
+}
+
 SkDrawOvalCommand::SkDrawOvalCommand(const SkRect& oval, const SkPaint& paint)
     : INHERITED(kDrawOval_OpType) {
     fOval = oval;
@@ -483,6 +1953,21 @@ bool SkDrawOvalCommand::render(SkCanvas* canvas) const {
     return true;
 }
 
+Json::Value SkDrawOvalCommand::toJSON() const {
+    Json::Value result = INHERITED::toJSON();
+    result[SKDEBUGCANVAS_ATTRIBUTE_COORDS] = make_json_rect(fOval);
+    result[SKDEBUGCANVAS_ATTRIBUTE_PAINT] = make_json_paint(fPaint, SKDEBUGCANVAS_SEND_BINARIES);
+    return result;
+}
+
+SkDrawOvalCommand* SkDrawOvalCommand::fromJSON(Json::Value& command) {
+    SkRect coords;
+    extract_json_rect(command[SKDEBUGCANVAS_ATTRIBUTE_COORDS], &coords);
+    SkPaint paint;
+    extract_json_paint(command[SKDEBUGCANVAS_ATTRIBUTE_PAINT], &paint);
+    return new SkDrawOvalCommand(coords, paint);
+}
+
 SkDrawPaintCommand::SkDrawPaintCommand(const SkPaint& paint)
     : INHERITED(kDrawPaint_OpType) {
     fPaint = paint;
@@ -500,6 +1985,18 @@ bool SkDrawPaintCommand::render(SkCanvas* canvas) const {
     return true;
 }
 
+Json::Value SkDrawPaintCommand::toJSON() const {
+    Json::Value result = INHERITED::toJSON();
+    result[SKDEBUGCANVAS_ATTRIBUTE_PAINT] = make_json_paint(fPaint, SKDEBUGCANVAS_SEND_BINARIES);
+    return result;
+}
+
+SkDrawPaintCommand* SkDrawPaintCommand::fromJSON(Json::Value& command) {
+    SkPaint paint;
+    extract_json_paint(command[SKDEBUGCANVAS_ATTRIBUTE_PAINT], &paint);
+    return new SkDrawPaintCommand(paint);
+}
+
 SkDrawPathCommand::SkDrawPathCommand(const SkPath& path, const SkPaint& paint)
     : INHERITED(kDrawPath_OpType) {
     fPath = path;
@@ -518,6 +2015,21 @@ bool SkDrawPathCommand::render(SkCanvas* canvas) const {
     return true;
 }
 
+Json::Value SkDrawPathCommand::toJSON() const {
+    Json::Value result = INHERITED::toJSON();
+    result[SKDEBUGCANVAS_ATTRIBUTE_PATH] = make_json_path(fPath);
+    result[SKDEBUGCANVAS_ATTRIBUTE_PAINT] = make_json_paint(fPaint, SKDEBUGCANVAS_SEND_BINARIES);
+    return result;
+}
+
+SkDrawPathCommand* SkDrawPathCommand::fromJSON(Json::Value& command) {
+    SkPath path;
+    extract_json_path(command[SKDEBUGCANVAS_ATTRIBUTE_PATH], &path);
+    SkPaint paint;
+    extract_json_paint(command[SKDEBUGCANVAS_ATTRIBUTE_PAINT], &paint);
+    return new SkDrawPathCommand(path, paint);
+}
+
 SkBeginDrawPictureCommand::SkBeginDrawPictureCommand(const SkPicture* picture,
                                                      const SkMatrix* matrix,
                                                      const SkPaint* paint)
@@ -624,6 +2136,47 @@ bool SkDrawPointsCommand::render(SkCanvas* canvas) const {
     return true;
 }
 
+Json::Value SkDrawPointsCommand::toJSON() const {
+    Json::Value result = INHERITED::toJSON();
+    result[SKDEBUGCANVAS_ATTRIBUTE_MODE] = make_json_pointmode(fMode);
+    Json::Value points(Json::arrayValue);
+    for (size_t i = 0; i < fCount; i++) {
+        points.append(make_json_point(fPts[i]));
+    }
+    result[SKDEBUGCANVAS_ATTRIBUTE_POINTS] = points;
+    result[SKDEBUGCANVAS_ATTRIBUTE_PAINT] = make_json_paint(fPaint, SKDEBUGCANVAS_SEND_BINARIES);
+    return result;
+}
+
+SkDrawPointsCommand* SkDrawPointsCommand::fromJSON(Json::Value& command) {
+    SkCanvas::PointMode mode;
+    const char* jsonMode = command[SKDEBUGCANVAS_ATTRIBUTE_MODE].asCString();
+    if (!strcmp(jsonMode, SKDEBUGCANVAS_POINTMODE_POINTS)) {
+        mode = SkCanvas::kPoints_PointMode;
+    }
+    else if (!strcmp(jsonMode, SKDEBUGCANVAS_POINTMODE_LINES)) {
+        mode = SkCanvas::kLines_PointMode;
+    }
+    else if (!strcmp(jsonMode, SKDEBUGCANVAS_POINTMODE_POLYGON)) {
+        mode = SkCanvas::kPolygon_PointMode;
+    }
+    else {
+        SkASSERT(false);
+        return nullptr;
+    }
+    Json::Value jsonPoints = command[SKDEBUGCANVAS_ATTRIBUTE_POINTS];
+    int count = (int) jsonPoints.size();
+    SkPoint* points = (SkPoint*) sk_malloc_throw(count * sizeof(SkPoint));
+    for (int i = 0; i < count; i++) {
+        points[i] = SkPoint::Make(jsonPoints[i][0].asFloat(), jsonPoints[i][1].asFloat());
+    }
+    SkPaint paint;
+    extract_json_paint(command[SKDEBUGCANVAS_ATTRIBUTE_PAINT], &paint);
+    SkDrawPointsCommand* result = new SkDrawPointsCommand(mode, count, points, paint);
+    sk_free(points);
+    return result;
+}
+
 SkDrawPosTextCommand::SkDrawPosTextCommand(const void* text, size_t byteLength,
                                            const SkPoint pos[], const SkPaint& paint)
     : INHERITED(kDrawPosText_OpType) {
@@ -648,6 +2201,31 @@ void SkDrawPosTextCommand::execute(SkCanvas* canvas) const {
     canvas->drawPosText(fText, fByteLength, fPos, fPaint);
 }
 
+Json::Value SkDrawPosTextCommand::toJSON() const {
+    Json::Value result = INHERITED::toJSON();
+    result[SKDEBUGCANVAS_ATTRIBUTE_TEXT] = Json::Value((const char*) fText, 
+                                                       ((const char*) fText) + fByteLength);
+    Json::Value coords(Json::arrayValue);
+    for (size_t i = 0; i < fByteLength; i++) {
+        coords.append(make_json_point(fPos[i]));
+    }
+    result[SKDEBUGCANVAS_ATTRIBUTE_COORDS] = coords;
+    result[SKDEBUGCANVAS_ATTRIBUTE_PAINT] = make_json_paint(fPaint, SKDEBUGCANVAS_SEND_BINARIES);
+    return result;
+}
+
+SkDrawPosTextCommand* SkDrawPosTextCommand::fromJSON(Json::Value& command) {
+    const char* text = command[SKDEBUGCANVAS_ATTRIBUTE_TEXT].asCString();
+    SkPaint paint;
+    extract_json_paint(command[SKDEBUGCANVAS_ATTRIBUTE_PAINT], &paint);
+    Json::Value coords = command[SKDEBUGCANVAS_ATTRIBUTE_COORDS];
+    int count = (int) coords.size();
+    SkPoint* points = (SkPoint*) sk_malloc_throw(count * sizeof(SkPoint));
+    for (int i = 0; i < count; i++) {
+        points[i] = SkPoint::Make(coords[i][0].asFloat(), coords[i][1].asFloat());
+    }
+    return new SkDrawPosTextCommand(text, strlen(text), points, paint);
+}
 
 SkDrawPosTextHCommand::SkDrawPosTextHCommand(const void* text, size_t byteLength,
                                              const SkScalar xpos[], SkScalar constY,
@@ -738,6 +2316,93 @@ bool SkDrawTextBlobCommand::render(SkCanvas* canvas) const {
     return true;
 }
 
+Json::Value SkDrawTextBlobCommand::toJSON() const {
+    Json::Value result = INHERITED::toJSON();
+    Json::Value runs(Json::arrayValue);
+    SkTextBlobRunIterator iter(fBlob.get());
+    while (!iter.done()) {
+        Json::Value run(Json::objectValue);
+        Json::Value jsonPositions(Json::arrayValue);
+        Json::Value jsonGlyphs(Json::arrayValue);
+        const SkScalar* iterPositions = iter.pos();
+        const uint16_t* iterGlyphs = iter.glyphs();
+        for (uint32_t i = 0; i < iter.glyphCount(); i++) {
+            switch (iter.positioning()) {
+                case SkTextBlob::kFull_Positioning:
+                    jsonPositions.append(make_json_point(iterPositions[i * 2],
+                                                         iterPositions[i * 2 + 1]));
+                    break;
+                case SkTextBlob::kHorizontal_Positioning:
+                    jsonPositions.append(Json::Value(iterPositions[i]));
+                    break;
+                case SkTextBlob::kDefault_Positioning:
+                    break;
+            }
+            jsonGlyphs.append(Json::Value(iterGlyphs[i]));
+        }
+        if (iter.positioning() != SkTextBlob::kDefault_Positioning) {
+            run[SKDEBUGCANVAS_ATTRIBUTE_POSITIONS] = jsonPositions;
+        }
+        run[SKDEBUGCANVAS_ATTRIBUTE_GLYPHS] = jsonGlyphs;
+        SkPaint fontPaint;
+        iter.applyFontToPaint(&fontPaint);
+        run[SKDEBUGCANVAS_ATTRIBUTE_FONT] = make_json_paint(fontPaint, SKDEBUGCANVAS_SEND_BINARIES);
+        run[SKDEBUGCANVAS_ATTRIBUTE_COORDS] = make_json_point(iter.offset());
+        runs.append(run);
+        iter.next();
+    }
+    result[SKDEBUGCANVAS_ATTRIBUTE_RUNS] = runs;
+    result[SKDEBUGCANVAS_ATTRIBUTE_X] = Json::Value(fXPos);
+    result[SKDEBUGCANVAS_ATTRIBUTE_Y] = Json::Value(fYPos);
+    result[SKDEBUGCANVAS_ATTRIBUTE_PAINT] = make_json_paint(fPaint, SKDEBUGCANVAS_SEND_BINARIES);
+    return result;
+}
+
+SkDrawTextBlobCommand* SkDrawTextBlobCommand::fromJSON(Json::Value& command) {
+    SkTextBlobBuilder builder;
+    Json::Value runs = command[SKDEBUGCANVAS_ATTRIBUTE_RUNS];
+    for (Json::ArrayIndex i = 0 ; i < runs.size(); i++) {
+        Json::Value run = runs[i];
+        SkPaint font;
+        font.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
+        extract_json_paint(run[SKDEBUGCANVAS_ATTRIBUTE_FONT], &font);
+        Json::Value glyphs = run[SKDEBUGCANVAS_ATTRIBUTE_GLYPHS];
+        int count = glyphs.size();
+        Json::Value coords = run[SKDEBUGCANVAS_ATTRIBUTE_COORDS];
+        SkScalar x = coords[0].asFloat();
+        SkScalar y = coords[1].asFloat();
+        if (run.isMember(SKDEBUGCANVAS_ATTRIBUTE_POSITIONS)) {
+            Json::Value positions = run[SKDEBUGCANVAS_ATTRIBUTE_POSITIONS];
+            if (positions.size() > 0 && positions[0].isNumeric()) {
+                SkTextBlobBuilder::RunBuffer buffer = builder.allocRunPosH(font, count, y);
+                for (int j = 0; j < count; j++) {
+                    buffer.glyphs[j] = glyphs[j].asUInt();
+                    buffer.pos[j] = positions[j].asFloat();
+                }
+            }
+            else {
+                SkTextBlobBuilder::RunBuffer buffer = builder.allocRunPos(font, count);
+                for (int j = 0; j < count; j++) {
+                    buffer.glyphs[j] = glyphs[j].asUInt();
+                    buffer.pos[j * 2] = positions[j][0].asFloat();
+                    buffer.pos[j * 2 + 1] = positions[j][1].asFloat();
+                }
+            }
+        }
+        else {
+            SkTextBlobBuilder::RunBuffer buffer = builder.allocRun(font, count, x, y);
+            for (int j = 0; j < count; j++) {
+                buffer.glyphs[j] = glyphs[j].asUInt();
+            }
+        }
+    }
+    SkScalar x = command[SKDEBUGCANVAS_ATTRIBUTE_X].asFloat();
+    SkScalar y = command[SKDEBUGCANVAS_ATTRIBUTE_Y].asFloat();
+    SkPaint paint;
+    extract_json_paint(command[SKDEBUGCANVAS_ATTRIBUTE_PAINT], &paint);
+    return new SkDrawTextBlobCommand(builder.build(), x, y, paint);
+}
+
 SkDrawPatchCommand::SkDrawPatchCommand(const SkPoint cubics[12], const SkColor colors[4],
                                        const SkPoint texCoords[4], SkXfermode* xfermode,
                                        const SkPaint& paint)
@@ -768,6 +2433,21 @@ void SkDrawRectCommand::execute(SkCanvas* canvas) const {
     canvas->drawRect(fRect, fPaint);
 }
 
+Json::Value SkDrawRectCommand::toJSON() const {
+    Json::Value result = INHERITED::toJSON();
+    result[SKDEBUGCANVAS_ATTRIBUTE_COORDS] = make_json_rect(fRect);
+    result[SKDEBUGCANVAS_ATTRIBUTE_PAINT] = make_json_paint(fPaint, SKDEBUGCANVAS_SEND_BINARIES);
+    return result;
+}
+
+SkDrawRectCommand* SkDrawRectCommand::fromJSON(Json::Value& command) {
+    SkRect coords;
+    extract_json_rect(command[SKDEBUGCANVAS_ATTRIBUTE_COORDS], &coords);
+    SkPaint paint;
+    extract_json_paint(command[SKDEBUGCANVAS_ATTRIBUTE_PAINT], &paint);
+    return new SkDrawRectCommand(coords, paint);
+}
+
 SkDrawRRectCommand::SkDrawRRectCommand(const SkRRect& rrect, const SkPaint& paint)
     : INHERITED(kDrawRRect_OpType) {
     fRRect = rrect;
@@ -786,6 +2466,21 @@ bool SkDrawRRectCommand::render(SkCanvas* canvas) const {
     return true;
 }
 
+Json::Value SkDrawRRectCommand::toJSON() const {
+    Json::Value result = INHERITED::toJSON();
+    result[SKDEBUGCANVAS_ATTRIBUTE_COORDS] = make_json_rrect(fRRect);
+    result[SKDEBUGCANVAS_ATTRIBUTE_PAINT] = make_json_paint(fPaint, SKDEBUGCANVAS_SEND_BINARIES);
+    return result;
+}
+
+SkDrawRRectCommand* SkDrawRRectCommand::fromJSON(Json::Value& command) {
+    SkRRect coords;
+    extract_json_rrect(command[SKDEBUGCANVAS_ATTRIBUTE_COORDS], &coords);
+    SkPaint paint;
+    extract_json_paint(command[SKDEBUGCANVAS_ATTRIBUTE_PAINT], &paint);
+    return new SkDrawRRectCommand(coords, paint);
+}
+
 SkDrawDRRectCommand::SkDrawDRRectCommand(const SkRRect& outer,
                                          const SkRRect& inner,
                                          const SkPaint& paint)
@@ -808,6 +2503,24 @@ bool SkDrawDRRectCommand::render(SkCanvas* canvas) const {
     return true;
 }
 
+Json::Value SkDrawDRRectCommand::toJSON() const {
+    Json::Value result = INHERITED::toJSON();
+    result[SKDEBUGCANVAS_ATTRIBUTE_OUTER] = make_json_rrect(fOuter);
+    result[SKDEBUGCANVAS_ATTRIBUTE_INNER] = make_json_rrect(fInner);
+    result[SKDEBUGCANVAS_ATTRIBUTE_PAINT] = make_json_paint(fPaint, SKDEBUGCANVAS_SEND_BINARIES);
+    return result;
+}
+
+SkDrawDRRectCommand* SkDrawDRRectCommand::fromJSON(Json::Value& command) {
+    SkRRect outer;
+    extract_json_rrect(command[SKDEBUGCANVAS_ATTRIBUTE_INNER], &outer);
+    SkRRect inner;
+    extract_json_rrect(command[SKDEBUGCANVAS_ATTRIBUTE_INNER], &inner);
+    SkPaint paint;
+    extract_json_paint(command[SKDEBUGCANVAS_ATTRIBUTE_PAINT], &paint);
+    return new SkDrawDRRectCommand(outer, inner, paint);
+}
+
 SkDrawTextCommand::SkDrawTextCommand(const void* text, size_t byteLength, SkScalar x, SkScalar y,
                                      const SkPaint& paint)
     : INHERITED(kDrawText_OpType) {
@@ -828,6 +2541,25 @@ void SkDrawTextCommand::execute(SkCanvas* canvas) const {
     canvas->drawText(fText, fByteLength, fX, fY, fPaint);
 }
 
+Json::Value SkDrawTextCommand::toJSON() const {
+    Json::Value result = INHERITED::toJSON();
+    result[SKDEBUGCANVAS_ATTRIBUTE_TEXT] = Json::Value((const char*) fText, 
+                                                       ((const char*) fText) + fByteLength);
+    Json::Value coords(Json::arrayValue);
+    result[SKDEBUGCANVAS_ATTRIBUTE_COORDS] = make_json_point(fX, fY);
+    result[SKDEBUGCANVAS_ATTRIBUTE_PAINT] = make_json_paint(fPaint, SKDEBUGCANVAS_SEND_BINARIES);
+    return result;
+}
+
+SkDrawTextCommand* SkDrawTextCommand::fromJSON(Json::Value& command) {
+    const char* text = command[SKDEBUGCANVAS_ATTRIBUTE_TEXT].asCString();
+    SkPaint paint;
+    extract_json_paint(command[SKDEBUGCANVAS_ATTRIBUTE_PAINT], &paint);
+    Json::Value coords = command[SKDEBUGCANVAS_ATTRIBUTE_COORDS];
+    return new SkDrawTextCommand(text, strlen(text), coords[0].asFloat(), coords[1].asFloat(), 
+                                 paint);
+}
+
 SkDrawTextOnPathCommand::SkDrawTextOnPathCommand(const void* text, size_t byteLength,
                                                  const SkPath& path, const SkMatrix* matrix,
                                                  const SkPaint& paint)
@@ -857,6 +2589,37 @@ void SkDrawTextOnPathCommand::execute(SkCanvas* canvas) const {
                            fPaint);
 }
 
+Json::Value SkDrawTextOnPathCommand::toJSON() const {
+    Json::Value result = INHERITED::toJSON();
+    result[SKDEBUGCANVAS_ATTRIBUTE_TEXT] = Json::Value((const char*) fText, 
+                                                       ((const char*) fText) + fByteLength);
+    Json::Value coords(Json::arrayValue);
+    result[SKDEBUGCANVAS_ATTRIBUTE_PATH] = make_json_path(fPath);
+    if (!fMatrix.isIdentity()) {
+        result[SKDEBUGCANVAS_ATTRIBUTE_MATRIX] = make_json_matrix(fMatrix);
+    }
+    result[SKDEBUGCANVAS_ATTRIBUTE_PAINT] = make_json_paint(fPaint, SKDEBUGCANVAS_SEND_BINARIES);
+    return result;
+}
+
+SkDrawTextOnPathCommand* SkDrawTextOnPathCommand::fromJSON(Json::Value& command) {
+    const char* text = command[SKDEBUGCANVAS_ATTRIBUTE_TEXT].asCString();
+    SkPaint paint;
+    extract_json_paint(command[SKDEBUGCANVAS_ATTRIBUTE_PAINT], &paint);
+    SkPath path;
+    extract_json_path(command[SKDEBUGCANVAS_ATTRIBUTE_PATH], &path);
+    SkMatrix* matrixPtr;
+    SkMatrix matrix;
+    if (command.isMember(SKDEBUGCANVAS_ATTRIBUTE_MATRIX)) {
+        extract_json_matrix(command[SKDEBUGCANVAS_ATTRIBUTE_MATRIX], &matrix);
+        matrixPtr = &matrix;
+    }
+    else {
+        matrixPtr = nullptr;
+    }
+    return new SkDrawTextOnPathCommand(text, strlen(text), path, matrixPtr, paint);
+}
+
 SkDrawVerticesCommand::SkDrawVerticesCommand(SkCanvas::VertexMode vmode, int vertexCount,
                                              const SkPoint vertices[], const SkPoint texs[],
                                              const SkColor colors[], SkXfermode* xfermode,
@@ -927,6 +2690,10 @@ void SkRestoreCommand::execute(SkCanvas* canvas) const {
     canvas->restore();
 }
 
+SkRestoreCommand* SkRestoreCommand::fromJSON(Json::Value& command) {
+    return new SkRestoreCommand();
+}
+
 SkSaveCommand::SkSaveCommand()
     : INHERITED(kSave_OpType) {
 }
@@ -935,6 +2702,10 @@ void SkSaveCommand::execute(SkCanvas* canvas) const {
     canvas->save();
 }
 
+SkSaveCommand* SkSaveCommand::fromJSON(Json::Value& command) {
+    return new SkSaveCommand();
+}
+
 SkSaveLayerCommand::SkSaveLayerCommand(const SkCanvas::SaveLayerRec& rec)
     : INHERITED(kSaveLayer_OpType) {
     if (rec.fBounds) {
@@ -951,6 +2722,13 @@ SkSaveLayerCommand::SkSaveLayerCommand(const SkCanvas::SaveLayerRec& rec)
     }
     fSaveLayerFlags = rec.fSaveLayerFlags;
 
+    if (rec.fBackdrop) {
+        fBackdrop = rec.fBackdrop;
+        fBackdrop->ref();
+    } else {
+        fBackdrop = nullptr;
+    }
+
     if (rec.fBounds) {
         fInfo.push(SkObjectParser::RectToString(*rec.fBounds, "Bounds: "));
     }
@@ -960,6 +2738,12 @@ SkSaveLayerCommand::SkSaveLayerCommand(const SkCanvas::SaveLayerRec& rec)
     fInfo.push(SkObjectParser::SaveLayerFlagsToString(fSaveLayerFlags));
 }
 
+SkSaveLayerCommand::~SkSaveLayerCommand() {
+    if (fBackdrop != nullptr) {
+        fBackdrop->unref();
+    }
+}
+
 void SkSaveLayerCommand::execute(SkCanvas* canvas) const {
     canvas->saveLayer(SkCanvas::SaveLayerRec(fBounds.isEmpty() ? nullptr : &fBounds,
                                              fPaintPtr,
@@ -970,6 +2754,50 @@ void SkSaveLayerCommand::vizExecute(SkCanvas* canvas) const {
     canvas->save();
 }
 
+Json::Value SkSaveLayerCommand::toJSON() const {
+    Json::Value result = INHERITED::toJSON();
+    if (!fBounds.isEmpty()) {
+        result[SKDEBUGCANVAS_ATTRIBUTE_BOUNDS] = make_json_rect(fBounds);
+    }
+    if (fPaintPtr != nullptr) {
+        result[SKDEBUGCANVAS_ATTRIBUTE_PAINT] = make_json_paint(*fPaintPtr, 
+                                                                SKDEBUGCANVAS_SEND_BINARIES);
+    }
+    if (fBackdrop != nullptr) {
+        Json::Value jsonBackdrop;
+        flatten(fBackdrop, &jsonBackdrop, SKDEBUGCANVAS_SEND_BINARIES);
+        result[SKDEBUGCANVAS_ATTRIBUTE_BACKDROP] = jsonBackdrop;
+    }
+    if (fSaveLayerFlags != 0) {
+        SkDebugf("unsupported: saveLayer flags\n");
+        SkASSERT(false);
+    }
+    return result;
+}
+
+SkSaveLayerCommand* SkSaveLayerCommand::fromJSON(Json::Value& command) {
+    SkCanvas::SaveLayerRec rec;
+    SkRect bounds;
+    if (command.isMember(SKDEBUGCANVAS_ATTRIBUTE_BOUNDS)) {
+        extract_json_rect(command[SKDEBUGCANVAS_ATTRIBUTE_BOUNDS], &bounds);
+        rec.fBounds = &bounds;
+    }
+    SkPaint paint;
+    if (command.isMember(SKDEBUGCANVAS_ATTRIBUTE_PAINT)) {
+        extract_json_paint(command[SKDEBUGCANVAS_ATTRIBUTE_PAINT], &paint);
+        rec.fPaint = &paint;
+    }
+    if (command.isMember(SKDEBUGCANVAS_ATTRIBUTE_BACKDROP)) {
+        Json::Value backdrop = command[SKDEBUGCANVAS_ATTRIBUTE_BACKDROP];
+        rec.fBackdrop = (SkImageFilter*) load_flattenable(backdrop);
+    }
+    SkSaveLayerCommand* result = new SkSaveLayerCommand(rec);
+    if (rec.fBackdrop != nullptr) {
+        rec.fBackdrop->unref();
+    }
+    return result;
+}
+
 SkSetMatrixCommand::SkSetMatrixCommand(const SkMatrix& matrix)
     : INHERITED(kSetMatrix_OpType) {
     fUserMatrix.reset();
@@ -987,3 +2815,14 @@ void SkSetMatrixCommand::execute(SkCanvas* canvas) const {
     canvas->setMatrix(temp);
 }
 
+Json::Value SkSetMatrixCommand::toJSON() const {
+    Json::Value result = INHERITED::toJSON();
+    result[SKDEBUGCANVAS_ATTRIBUTE_MATRIX] = make_json_matrix(fMatrix);
+    return result;
+}
+
+SkSetMatrixCommand* SkSetMatrixCommand::fromJSON(Json::Value& command) {
+    SkMatrix matrix;
+    extract_json_matrix(command[SKDEBUGCANVAS_ATTRIBUTE_MATRIX], &matrix);
+    return new SkSetMatrixCommand(matrix);
+}
index f67df92..a93461a 100644 (file)
@@ -15,6 +15,7 @@
 #include "SkRRect.h"
 #include "SkString.h"
 #include "SkTDArray.h"
+#include "SkJSONCPP.h"
 
 class SK_API SkDrawCommand {
 public:
@@ -99,6 +100,14 @@ public:
 
     virtual bool render(SkCanvas* canvas) const { return false; }
 
+    virtual Json::Value toJSON() const;
+
+    /* Converts a JSON representation of a command into a newly-allocated SkDrawCommand object. It
+     * is the caller's responsibility to delete this object. This method may return null if an error
+     * occurs.
+     */
+    static SkDrawCommand* fromJSON(Json::Value& command);
+
     static const char* GetCommandString(OpType type);
 
 protected:
@@ -114,6 +123,7 @@ public:
     SkRestoreCommand();
     void execute(SkCanvas* canvas) const override;
     Action action() const override { return kPopLayer_Action; }
+    static SkRestoreCommand* fromJSON(Json::Value& command);
 
 private:
     typedef SkDrawCommand INHERITED;
@@ -123,6 +133,9 @@ class SkClearCommand : public SkDrawCommand {
 public:
     SkClearCommand(SkColor color);
     void execute(SkCanvas* canvas) const override;
+    Json::Value toJSON() const override;
+    static SkClearCommand* fromJSON(Json::Value& command);
+
 private:
     SkColor fColor;
 
@@ -134,6 +147,9 @@ public:
     SkClipPathCommand(const SkPath& path, SkRegion::Op op, bool doAA);
     void execute(SkCanvas* canvas) const override;
     bool render(SkCanvas* canvas) const override;
+    Json::Value toJSON() const override;
+    static SkClipPathCommand* fromJSON(Json::Value& command);
+
 private:
     SkPath       fPath;
     SkRegion::Op fOp;
@@ -146,6 +162,9 @@ class SkClipRegionCommand : public SkDrawCommand {
 public:
     SkClipRegionCommand(const SkRegion& region, SkRegion::Op op);
     void execute(SkCanvas* canvas) const override;
+    Json::Value toJSON() const override;
+    static SkClipRegionCommand* fromJSON(Json::Value& command);
+
 private:
     SkRegion     fRegion;
     SkRegion::Op fOp;
@@ -157,6 +176,8 @@ class SkClipRectCommand : public SkDrawCommand {
 public:
     SkClipRectCommand(const SkRect& rect, SkRegion::Op op, bool doAA);
     void execute(SkCanvas* canvas) const override;
+    Json::Value toJSON() const override;
+    static SkClipRectCommand* fromJSON(Json::Value& command);
 
     const SkRect& rect() const { return fRect; }
     SkRegion::Op op() const { return fOp; }
@@ -175,6 +196,8 @@ public:
     SkClipRRectCommand(const SkRRect& rrect, SkRegion::Op op, bool doAA);
     void execute(SkCanvas* canvas) const override;
     bool render(SkCanvas* canvas) const override;
+    Json::Value toJSON() const override;
+    static SkClipRRectCommand* fromJSON(Json::Value& command);
 
     const SkRRect& rrect() const { return fRRect; }
     SkRegion::Op op() const { return fOp; }
@@ -192,6 +215,9 @@ class SkConcatCommand : public SkDrawCommand {
 public:
     SkConcatCommand(const SkMatrix& matrix);
     void execute(SkCanvas* canvas) const override;
+    Json::Value toJSON() const override;
+    static SkConcatCommand* fromJSON(Json::Value& command);
+
 private:
     SkMatrix fMatrix;
 
@@ -204,6 +230,9 @@ public:
                         const SkPaint* paint);
     void execute(SkCanvas* canvas) const override;
     bool render(SkCanvas* canvas) const override;
+    Json::Value toJSON() const override;
+    static SkDrawBitmapCommand* fromJSON(Json::Value& command);
+
 private:
     SkBitmap fBitmap;
     SkScalar fLeft;
@@ -220,6 +249,9 @@ public:
                             const SkRect& dst, const SkPaint* paint);
     void execute(SkCanvas* canvas) const override;
     bool render(SkCanvas* canvas) const override;
+    Json::Value toJSON() const override;
+    static SkDrawBitmapNineCommand* fromJSON(Json::Value& command);
+
 private:
     SkBitmap fBitmap;
     SkIRect  fCenter;
@@ -237,6 +269,8 @@ public:
                             SkCanvas::SrcRectConstraint);
     void execute(SkCanvas* canvas) const override;
     bool render(SkCanvas* canvas) const override;
+    Json::Value toJSON() const override;
+    static SkDrawBitmapRectCommand* fromJSON(Json::Value& command);
 
     const SkBitmap& bitmap() const { return fBitmap; }
 
@@ -274,6 +308,9 @@ public:
     SkDrawImageCommand(const SkImage* image, SkScalar left, SkScalar top, const SkPaint* paint);
     void execute(SkCanvas* canvas) const override;
     bool render(SkCanvas* canvas) const override;
+    Json::Value toJSON() const override;
+    static SkDrawImageCommand* fromJSON(Json::Value& command);
+
 private:
     SkAutoTUnref<const SkImage> fImage;
     SkScalar                    fLeft;
@@ -289,6 +326,9 @@ public:
                            const SkPaint* paint, SkCanvas::SrcRectConstraint constraint);
     void execute(SkCanvas* canvas) const override;
     bool render(SkCanvas* canvas) const override;
+    Json::Value toJSON() const override;
+    static SkDrawImageRectCommand* fromJSON(Json::Value& command);
+
 private:
     SkAutoTUnref<const SkImage> fImage;
     SkTLazy<SkRect>             fSrc;
@@ -304,6 +344,9 @@ public:
     SkDrawOvalCommand(const SkRect& oval, const SkPaint& paint);
     void execute(SkCanvas* canvas) const override;
     bool render(SkCanvas* canvas) const override;
+    Json::Value toJSON() const override;
+    static SkDrawOvalCommand* fromJSON(Json::Value& command);
+
 private:
     SkRect  fOval;
     SkPaint fPaint;
@@ -316,6 +359,9 @@ public:
     SkDrawPaintCommand(const SkPaint& paint);
     void execute(SkCanvas* canvas) const override;
     bool render(SkCanvas* canvas) const override;
+    Json::Value toJSON() const override;
+    static SkDrawPaintCommand* fromJSON(Json::Value& command);
+
 private:
     SkPaint fPaint;
 
@@ -327,6 +373,8 @@ public:
     SkDrawPathCommand(const SkPath& path, const SkPaint& paint);
     void execute(SkCanvas* canvas) const override;
     bool render(SkCanvas* canvas) const override;
+    Json::Value toJSON() const override;
+    static SkDrawPathCommand* fromJSON(Json::Value& command);
 
 private:
     SkPath   fPath;
@@ -371,6 +419,9 @@ public:
     virtual ~SkDrawPointsCommand() { delete [] fPts; }
     void execute(SkCanvas* canvas) const override;
     bool render(SkCanvas* canvas) const override;
+    Json::Value toJSON() const override;
+    static SkDrawPointsCommand* fromJSON(Json::Value& command);
+
 private:
     SkCanvas::PointMode fMode;
     size_t              fCount;
@@ -386,6 +437,9 @@ public:
                       const SkPaint& paint);
     virtual ~SkDrawTextCommand() { delete [] fText; }
     void execute(SkCanvas* canvas) const override;
+    Json::Value toJSON() const override;
+    static SkDrawTextCommand* fromJSON(Json::Value& command);
+
 private:
     char*    fText;
     size_t   fByteLength;
@@ -402,6 +456,9 @@ public:
                          const SkPaint& paint);
     virtual ~SkDrawPosTextCommand() { delete [] fPos; delete [] fText; }
     void execute(SkCanvas* canvas) const override;
+    Json::Value toJSON() const override;
+    static SkDrawPosTextCommand* fromJSON(Json::Value& command);
+
 private:
     char*    fText;
     size_t   fByteLength;
@@ -417,6 +474,9 @@ public:
                             const SkMatrix* matrix, const SkPaint& paint);
     virtual ~SkDrawTextOnPathCommand() { delete [] fText; }
     void execute(SkCanvas* canvas) const override;
+    Json::Value toJSON() const override;
+    static SkDrawTextOnPathCommand* fromJSON(Json::Value& command);
+
 private:
     char*    fText;
     size_t   fByteLength;
@@ -433,6 +493,7 @@ public:
                           SkScalar constY, const SkPaint& paint);
     virtual ~SkDrawPosTextHCommand() { delete [] fXpos; delete [] fText; }
     void execute(SkCanvas* canvas) const override;
+
 private:
     SkScalar* fXpos;
     char*     fText;
@@ -449,6 +510,8 @@ public:
 
     void execute(SkCanvas* canvas) const override;
     bool render(SkCanvas* canvas) const override;
+    Json::Value toJSON() const override;
+    static SkDrawTextBlobCommand* fromJSON(Json::Value& command);
 
 private:
     SkAutoTUnref<const SkTextBlob> fBlob;
@@ -481,6 +544,8 @@ class SkDrawRectCommand : public SkDrawCommand {
 public:
     SkDrawRectCommand(const SkRect& rect, const SkPaint& paint);
     void execute(SkCanvas* canvas) const override;
+    Json::Value toJSON() const override;
+    static SkDrawRectCommand* fromJSON(Json::Value& command);
 
     const SkRect& rect() const   { return fRect; }
     const SkPaint& paint() const { return fPaint; }
@@ -496,6 +561,9 @@ public:
     SkDrawRRectCommand(const SkRRect& rrect, const SkPaint& paint);
     void execute(SkCanvas* canvas) const override;
     bool render(SkCanvas* canvas) const override;
+    Json::Value toJSON() const override;
+    static SkDrawRRectCommand* fromJSON(Json::Value& command);
+
 private:
     SkRRect fRRect;
     SkPaint fPaint;
@@ -509,6 +577,9 @@ public:
                         const SkPaint& paint);
     void execute(SkCanvas* canvas) const override;
     bool render(SkCanvas* canvas) const override;
+    Json::Value toJSON() const override;
+    static SkDrawDRRectCommand* fromJSON(Json::Value& command);
+
 private:
     SkRRect fOuter;
     SkRRect fInner;
@@ -526,6 +597,7 @@ public:
                           const SkPaint& paint);
     virtual ~SkDrawVerticesCommand();
     void execute(SkCanvas* canvas) const override;
+
 private:
     SkCanvas::VertexMode fVmode;
     int         fVertexCount;
@@ -545,6 +617,8 @@ public:
     SkSaveCommand();
     void execute(SkCanvas* canvas) const override;
     Action action() const override { return kPushLayer_Action; }
+    static SkSaveCommand* fromJSON(Json::Value& command);
+
 private:
     typedef SkDrawCommand INHERITED;
 };
@@ -552,7 +626,10 @@ private:
 class SkSaveLayerCommand : public SkDrawCommand {
 public:
     SkSaveLayerCommand(const SkCanvas::SaveLayerRec&);
+    virtual ~SkSaveLayerCommand();
     void execute(SkCanvas* canvas) const override;
+    Json::Value toJSON() const override;
+    static SkSaveLayerCommand* fromJSON(Json::Value& command);
     void vizExecute(SkCanvas* canvas) const override;
     Action action() const override{ return kPushLayer_Action; }
     void setActive(bool active) override { fActive = active; }
@@ -561,10 +638,11 @@ public:
     const SkPaint* paint() const { return fPaintPtr; }
 
 private:
-    SkRect      fBounds;
-    SkPaint     fPaint;
-    SkPaint*    fPaintPtr;
-    uint32_t    fSaveLayerFlags;
+    SkRect               fBounds;
+    SkPaint              fPaint;
+    SkPaint*             fPaintPtr;
+    const SkImageFilter* fBackdrop;
+    uint32_t       fSaveLayerFlags;
 
     bool        fActive;
 
@@ -576,6 +654,9 @@ public:
     SkSetMatrixCommand(const SkMatrix& matrix);
     void setUserMatrix(const SkMatrix&) override;
     void execute(SkCanvas* canvas) const override;
+    Json::Value toJSON() const override;
+    static SkSetMatrixCommand* fromJSON(Json::Value& command);
+
 private:
     SkMatrix fUserMatrix;
     SkMatrix fMatrix;
index 6d71a38..5f3fa44 100644 (file)
@@ -26,11 +26,12 @@ SkString* SkObjectParser::BitmapToString(const SkBitmap& bitmap) {
     mBitmap->appendS32(bitmap.width());
     mBitmap->append(" H: ");
     mBitmap->appendS32(bitmap.height());
-
+    
     const char* gColorTypeStrings[] = {
-        "None", "A8", "565", "4444", "RGBA", "BGRA", "Index8", "G8"
+        "None", "A8", "565", "4444", "RGBA", "BGRA", "Index8", "G8", "RGBAf16"
     };
-    SkASSERT(kLastEnum_SkColorType + 1 == SK_ARRAY_COUNT(gColorTypeStrings));
+    static_assert(kLastEnum_SkColorType + 1 == SK_ARRAY_COUNT(gColorTypeStrings),
+                  "colortype names do not match colortype enum");
 
     mBitmap->append(" ColorType: ");
     mBitmap->append(gColorTypeStrings[bitmap.colorType()]);