Add SkColor4f serialization

Adjusted usage in color shader, and will also be using this
in gradients, soon.

BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2334123003

Review-Url: https://codereview.chromium.org/2334123003
This commit is contained in:
brianosman 2016-09-25 13:15:58 -07:00 committed by Commit bot
parent b82ac17757
commit 97bbf8211f
12 changed files with 83 additions and 9 deletions

View File

@ -49,6 +49,8 @@ public:
virtual void writeFlattenable(const SkFlattenable* flattenable) = 0;
virtual void writeColor(SkColor color) = 0;
virtual void writeColorArray(const SkColor* color, uint32_t count) = 0;
virtual void writeColor4f(const SkColor4f& color) = 0;
virtual void writeColor4fArray(const SkColor4f* color, uint32_t count) = 0;
virtual void writePoint(const SkPoint& point) = 0;
virtual void writePointArray(const SkPoint* point, uint32_t count) = 0;
virtual void writeMatrix(const SkMatrix& matrix) = 0;
@ -107,6 +109,8 @@ public:
void writeFlattenable(const SkFlattenable* flattenable) override;
void writeColor(SkColor color) override;
void writeColorArray(const SkColor* color, uint32_t count) override;
void writeColor4f(const SkColor4f& color) override;
void writeColor4fArray(const SkColor4f* color, uint32_t count) override;
void writePoint(const SkPoint& point) override;
void writePointArray(const SkPoint* point, uint32_t count) override;
void writeMatrix(const SkMatrix& matrix) override;

View File

@ -130,10 +130,7 @@ SkColor4Shader::SkColor4Shader(const SkColor4f& color, sk_sp<SkColorSpace> space
sk_sp<SkFlattenable> SkColor4Shader::CreateProc(SkReadBuffer& buffer) {
SkColor4f color;
color.fR = buffer.readScalar(); // readFloat()
color.fG = buffer.readScalar();
color.fB = buffer.readScalar();
color.fA = buffer.readScalar();
buffer.readColor4f(&color);
if (buffer.readBool()) {
// TODO how do we unflatten colorspaces
}
@ -141,10 +138,7 @@ sk_sp<SkFlattenable> SkColor4Shader::CreateProc(SkReadBuffer& buffer) {
}
void SkColor4Shader::flatten(SkWriteBuffer& buffer) const {
buffer.writeScalar(fColor4.fR); // writeFloat()
buffer.writeScalar(fColor4.fG);
buffer.writeScalar(fColor4.fB);
buffer.writeScalar(fColor4.fA);
buffer.writeColor4f(fColor4);
buffer.writeBool(false); // TODO how do we flatten colorspaces?
}

View File

@ -142,6 +142,10 @@ void SkReadBuffer::readString(SkString* string) {
string->set(strContents, len);
}
void SkReadBuffer::readColor4f(SkColor4f* color) {
memcpy(color, fReader.skip(sizeof(SkColor4f)), sizeof(SkColor4f));
}
void SkReadBuffer::readPoint(SkPoint* point) {
point->fX = fReader.readScalar();
point->fY = fReader.readScalar();
@ -192,6 +196,10 @@ bool SkReadBuffer::readColorArray(SkColor* colors, size_t size) {
return readArray(colors, size, sizeof(SkColor));
}
bool SkReadBuffer::readColor4fArray(SkColor4f* colors, size_t size) {
return readArray(colors, size, sizeof(SkColor4f));
}
bool SkReadBuffer::readIntArray(int32_t* values, size_t size) {
return readArray(values, size, sizeof(int32_t));
}

View File

@ -123,6 +123,7 @@ public:
virtual void readString(SkString* string);
// common data structures
virtual void readColor4f(SkColor4f* color);
virtual void readPoint(SkPoint* point);
SkPoint readPoint() { SkPoint p; this->readPoint(&p); return p; }
virtual void readMatrix(SkMatrix* matrix);
@ -150,6 +151,7 @@ public:
// binary data and arrays
virtual bool readByteArray(void* value, size_t size);
virtual bool readColorArray(SkColor* colors, size_t size);
virtual bool readColor4fArray(SkColor4f* colors, size_t size);
virtual bool readIntArray(int32_t* values, size_t size);
virtual bool readPointArray(SkPoint* points, size_t size);
virtual bool readScalarArray(SkScalar* values, size_t size);

View File

@ -110,6 +110,13 @@ void SkValidatingReadBuffer::readString(SkString* string) {
}
}
void SkValidatingReadBuffer::readColor4f(SkColor4f* color) {
const void* ptr = this->skip(sizeof(SkColor4f));
if (!fError) {
memcpy(color, ptr, sizeof(SkColor4f));
}
}
void SkValidatingReadBuffer::readPoint(SkPoint* point) {
point->fX = this->readScalar();
point->fY = this->readScalar();
@ -197,6 +204,10 @@ bool SkValidatingReadBuffer::readColorArray(SkColor* colors, size_t size) {
return readArray(colors, size, sizeof(SkColor));
}
bool SkValidatingReadBuffer::readColor4fArray(SkColor4f* colors, size_t size) {
return readArray(colors, size, sizeof(SkColor4f));
}
bool SkValidatingReadBuffer::readIntArray(int32_t* values, size_t size) {
return readArray(values, size, sizeof(int32_t));
}

View File

@ -44,6 +44,7 @@ public:
// common data structures
SkFlattenable* readFlattenable(SkFlattenable::Type type) override;
void readColor4f(SkColor4f* color) override;
void readPoint(SkPoint* point) override;
void readMatrix(SkMatrix* matrix) override;
void readIRect(SkIRect* rect) override;
@ -55,6 +56,7 @@ public:
// binary data and arrays
bool readByteArray(void* value, size_t size) override;
bool readColorArray(SkColor* colors, size_t size) override;
bool readColor4fArray(SkColor4f* colors, size_t size) override;
bool readIntArray(int32_t* values, size_t size) override;
bool readPointArray(SkPoint* points, size_t size) override;
bool readScalarArray(SkScalar* values, size_t size) override;

View File

@ -78,6 +78,15 @@ void SkBinaryWriteBuffer::writeColorArray(const SkColor* color, uint32_t count)
fWriter.write(color, count * sizeof(SkColor));
}
void SkBinaryWriteBuffer::writeColor4f(const SkColor4f& color) {
fWriter.write(&color, sizeof(SkColor4f));
}
void SkBinaryWriteBuffer::writeColor4fArray(const SkColor4f* color, uint32_t count) {
fWriter.write32(count);
fWriter.write(color, count * sizeof(SkColor4f));
}
void SkBinaryWriteBuffer::writePoint(const SkPoint& point) {
fWriter.writeScalar(point.fX);
fWriter.writeScalar(point.fY);

View File

@ -104,6 +104,15 @@ template<> struct SerializationUtils<SkColor> {
}
};
template<> struct SerializationUtils<SkColor4f> {
static void Write(SkWriteBuffer& writer, SkColor4f* data, uint32_t arraySize) {
writer.writeColor4fArray(data, arraySize);
}
static bool Read(SkValidatingReadBuffer& reader, SkColor4f* data, uint32_t arraySize) {
return reader.readColor4fArray(data, arraySize);
}
};
template<> struct SerializationUtils<int32_t> {
static void Write(SkWriteBuffer& writer, int32_t* data, uint32_t arraySize) {
writer.writeIntArray(data, arraySize);
@ -225,7 +234,7 @@ static void TestArraySerialization(T* data, skiatest::Reporter* reporter) {
// This should write the length (in 4 bytes) and the array
REPORTER_ASSERT(reporter, (4 + kArraySize * sizeof(T)) == bytesWritten);
unsigned char dataWritten[1024];
unsigned char dataWritten[2048];
writer.writeToMemory(dataWritten);
// Make sure this fails when it should
@ -494,6 +503,17 @@ DEF_TEST(Serialization, reporter) {
TestArraySerialization(data, reporter);
}
// Test readColor4fArray
{
SkColor4f data[kArraySize] = {
SkColor4f::FromColor(SK_ColorBLACK),
SkColor4f::FromColor(SK_ColorWHITE),
SkColor4f::FromColor(SK_ColorRED),
{ 1.f, 2.f, 4.f, 8.f }
};
TestArraySerialization(data, reporter);
}
// Test readIntArray
{
int32_t data[kArraySize] = { 1, 2, 4, 8 };

View File

@ -428,6 +428,15 @@ Json::Value SkDrawCommand::MakeJsonColor(const SkColor color) {
return result;
}
Json::Value SkDrawCommand::MakeJsonColor4f(const SkColor4f& color) {
Json::Value result(Json::arrayValue);
result.append(Json::Value(color.fA));
result.append(Json::Value(color.fR));
result.append(Json::Value(color.fG));
result.append(Json::Value(color.fB));
return result;
}
Json::Value SkDrawCommand::MakeJsonPoint(const SkPoint& point) {
Json::Value result(Json::arrayValue);
result.append(Json::Value(point.x()));

View File

@ -123,6 +123,7 @@ public:
// Helper methods for converting things to JSON
static Json::Value MakeJsonColor(const SkColor color);
static Json::Value MakeJsonColor4f(const SkColor4f& color);
static Json::Value MakeJsonPoint(const SkPoint& point);
static Json::Value MakeJsonPoint(SkScalar x, SkScalar y);
static Json::Value MakeJsonRect(const SkRect& rect);

View File

@ -83,6 +83,18 @@ void SkJsonWriteBuffer::writeColorArray(const SkColor* color, uint32_t count) {
this->append("colorArray", jsonArray);
}
void SkJsonWriteBuffer::writeColor4f(const SkColor4f& color) {
this->append("color", SkDrawCommand::MakeJsonColor4f(color));
}
void SkJsonWriteBuffer::writeColor4fArray(const SkColor4f* color, uint32_t count) {
Json::Value jsonArray(Json::arrayValue);
for (uint32_t i = 0; i < count; ++i) {
jsonArray.append(SkDrawCommand::MakeJsonColor4f(color[i]));
}
this->append("colorArray", jsonArray);
}
void SkJsonWriteBuffer::writePoint(const SkPoint& point) {
this->append("point", SkDrawCommand::MakeJsonPoint(point));
}

View File

@ -35,6 +35,8 @@ public:
void writeFlattenable(const SkFlattenable* flattenable) override;
void writeColor(SkColor color) override;
void writeColorArray(const SkColor* color, uint32_t count) override;
void writeColor4f(const SkColor4f& color) override;
void writeColor4fArray(const SkColor4f* color, uint32_t count) override;
void writePoint(const SkPoint& point) override;
void writePointArray(const SkPoint* point, uint32_t count) override;
void writeMatrix(const SkMatrix& matrix) override;