Home | History | Annotate | Download | only in debugger
      1 /*
      2  * Copyright 2016 Google Inc.
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 #include "SkJsonWriteBuffer.h"
      9 
     10 #include "SkDrawCommand.h"
     11 #include "SkObjectParser.h"
     12 
     13 void SkJsonWriteBuffer::append(const char* type, const Json::Value& value) {
     14     SkString fullName = SkStringPrintf("%02d_%s", fJson.size(), type);
     15     fJson[fullName.c_str()] = value;
     16 }
     17 
     18 void SkJsonWriteBuffer::writePad32(const void* data, size_t size) {
     19     Json::Value jsonArray(Json::arrayValue);
     20     const uint8_t* bytes = reinterpret_cast<const uint8_t*>(data);
     21     for (size_t i = 0; i < size; ++i) {
     22         SkString hexByte = SkStringPrintf("%02x", bytes[i]);
     23         jsonArray.append(hexByte.c_str());
     24     }
     25     this->append("rawBytes", jsonArray);
     26 }
     27 
     28 void SkJsonWriteBuffer::writeByteArray(const void* data, size_t size) {
     29     Json::Value jsonArray(Json::arrayValue);
     30     const uint8_t* bytes = reinterpret_cast<const uint8_t*>(data);
     31     for (size_t i = 0; i < size; ++i) {
     32         SkString hexByte = SkStringPrintf("%02x", bytes[i]);
     33         jsonArray.append(hexByte.c_str());
     34     }
     35     this->append("byteArray", jsonArray);
     36 }
     37 
     38 void SkJsonWriteBuffer::writeBool(bool value) {
     39     this->append("bool", value);
     40 }
     41 
     42 void SkJsonWriteBuffer::writeScalar(SkScalar value) {
     43     this->append("scalar", value);
     44 }
     45 
     46 void SkJsonWriteBuffer::writeScalarArray(const SkScalar* value, uint32_t count) {
     47     Json::Value jsonArray(Json::arrayValue);
     48     for (uint32_t i = 0; i < count; ++i) {
     49         jsonArray.append(value[i]);
     50     }
     51     this->append("scalarArray", jsonArray);
     52 }
     53 
     54 void SkJsonWriteBuffer::writeInt(int32_t value) {
     55     this->append("int", value);
     56 }
     57 
     58 void SkJsonWriteBuffer::writeIntArray(const int32_t* value, uint32_t count) {
     59     Json::Value jsonArray(Json::arrayValue);
     60     for (uint32_t i = 0; i < count; ++i) {
     61         jsonArray.append(value[i]);
     62     }
     63     this->append("intArray", jsonArray);
     64 }
     65 
     66 void SkJsonWriteBuffer::writeUInt(uint32_t value) {
     67     this->append("uint", value);
     68 }
     69 
     70 void SkJsonWriteBuffer::writeString(const char* value) {
     71     this->append("string", value);
     72 }
     73 
     74 void SkJsonWriteBuffer::writeFlattenable(const SkFlattenable* flattenable) {
     75     if (flattenable) {
     76         SkJsonWriteBuffer flattenableBuffer(fUrlDataManager);
     77         flattenable->flatten(flattenableBuffer);
     78         this->append(flattenable->getTypeName(), flattenableBuffer.getValue());
     79     } else {
     80         this->append("flattenable", Json::Value());
     81     }
     82 }
     83 
     84 void SkJsonWriteBuffer::writeColor(SkColor color) {
     85     this->append("color", SkDrawCommand::MakeJsonColor(color));
     86 }
     87 
     88 void SkJsonWriteBuffer::writeColorArray(const SkColor* color, uint32_t count) {
     89     Json::Value jsonArray(Json::arrayValue);
     90     for (uint32_t i = 0; i < count; ++i) {
     91         jsonArray.append(SkDrawCommand::MakeJsonColor(color[i]));
     92     }
     93     this->append("colorArray", jsonArray);
     94 }
     95 
     96 void SkJsonWriteBuffer::writeColor4f(const SkColor4f& color) {
     97     this->append("color", SkDrawCommand::MakeJsonColor4f(color));
     98 }
     99 
    100 void SkJsonWriteBuffer::writeColor4fArray(const SkColor4f* color, uint32_t count) {
    101     Json::Value jsonArray(Json::arrayValue);
    102     for (uint32_t i = 0; i < count; ++i) {
    103         jsonArray.append(SkDrawCommand::MakeJsonColor4f(color[i]));
    104     }
    105     this->append("colorArray", jsonArray);
    106 }
    107 
    108 void SkJsonWriteBuffer::writePoint(const SkPoint& point) {
    109     this->append("point", SkDrawCommand::MakeJsonPoint(point));
    110 }
    111 
    112 void SkJsonWriteBuffer::writePointArray(const SkPoint* point, uint32_t count) {
    113     Json::Value jsonArray(Json::arrayValue);
    114     for (uint32_t i = 0; i < count; ++i) {
    115         jsonArray.append(SkDrawCommand::MakeJsonPoint(point[i]));
    116     }
    117     this->append("pointArray", jsonArray);
    118 }
    119 
    120 void SkJsonWriteBuffer::writeMatrix(const SkMatrix& matrix) {
    121     this->append("matrix", SkDrawCommand::MakeJsonMatrix(matrix));
    122 }
    123 
    124 void SkJsonWriteBuffer::writeIRect(const SkIRect& rect) {
    125     this->append("irect", SkDrawCommand::MakeJsonIRect(rect));
    126 }
    127 
    128 void SkJsonWriteBuffer::writeRect(const SkRect& rect) {
    129     this->append("rect", SkDrawCommand::MakeJsonRect(rect));
    130 }
    131 
    132 void SkJsonWriteBuffer::writeRegion(const SkRegion& region) {
    133     this->append("region", SkDrawCommand::MakeJsonRegion(region));
    134 }
    135 
    136 void SkJsonWriteBuffer::writePath(const SkPath& path) {
    137     this->append("path", SkDrawCommand::MakeJsonPath(path));
    138 }
    139 
    140 size_t SkJsonWriteBuffer::writeStream(SkStream* stream, size_t length) {
    141     // Contents not supported
    142     SkASSERT(length < Json::Value::maxUInt);
    143     this->append("stream", static_cast<Json::UInt>(length));
    144     return 0;
    145 }
    146 
    147 void SkJsonWriteBuffer::writeImage(const SkImage* image) {
    148     Json::Value jsonImage;
    149     SkDrawCommand::flatten(*image, &jsonImage, *fUrlDataManager);
    150     this->append("image", jsonImage);
    151 }
    152 
    153 void SkJsonWriteBuffer::writeTypeface(SkTypeface* typeface) {
    154     // Unsupported
    155     this->append("typeface", Json::Value());
    156 }
    157 
    158 void SkJsonWriteBuffer::writePaint(const SkPaint& paint) {
    159     this->append("paint", SkDrawCommand::MakeJsonPaint(paint, *fUrlDataManager));
    160 }
    161