read/write m44

Inspired by https://skia-review.googlesource.com/c/skia/+/303271

Change-Id: I64816f8db1742d263c8f668a7b44a08f20d82f5e
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/304065
Reviewed-by: Mike Reed <reed@google.com>
Commit-Queue: Mike Reed <reed@google.com>
This commit is contained in:
Mike Reed 2020-07-20 16:07:01 -04:00 committed by Skia Commit-Bot
parent 0927dd65c4
commit 6d0ab954e9
7 changed files with 45 additions and 0 deletions

View File

@ -111,6 +111,15 @@ struct SkV4 {
const float* ptr() const { return &x; }
float* ptr() { return &x; }
float operator[](int i) const {
SkASSERT(i >= 0 && i < 4);
return this->ptr()[i];
}
float& operator[](int i) {
SkASSERT(i >= 0 && i < 4);
return this->ptr()[i];
}
};
/**

View File

@ -165,6 +165,17 @@ void SkReadBuffer::readPoint3(SkPoint3* point) {
this->readPad32(point, sizeof(SkPoint3));
}
void SkReadBuffer::read(SkM44* matrix) {
if (this->isValid()) {
if (const float* m = (const float*)this->skip(sizeof(float) * 16)) {
*matrix = SkM44::ColMajor(m);
}
}
if (!this->isValid()) {
*matrix = SkM44();
}
}
void SkReadBuffer::readMatrix(SkMatrix* matrix) {
size_t size = 0;
if (this->isValid()) {

View File

@ -92,6 +92,7 @@ public:
void readPoint(SkPoint* point);
SkPoint readPoint() { SkPoint p; this->readPoint(&p); return p; }
void readPoint3(SkPoint3* point);
void read(SkM44*);
void readMatrix(SkMatrix* matrix);
void readIRect(SkIRect* rect);
void readRect(SkRect* rect);

View File

@ -9,10 +9,12 @@
#include "include/core/SkBitmap.h"
#include "include/core/SkData.h"
#include "include/core/SkM44.h"
#include "include/core/SkStream.h"
#include "include/core/SkTypeface.h"
#include "include/private/SkTo.h"
#include "src/core/SkImagePriv.h"
#include "src/core/SkMatrixPriv.h"
#include "src/core/SkPaintPriv.h"
#include "src/core/SkPtrRecorder.h"
@ -102,6 +104,10 @@ void SkBinaryWriteBuffer::writePointArray(const SkPoint* point, uint32_t count)
fWriter.write(point, count * sizeof(SkPoint));
}
void SkBinaryWriteBuffer::write(const SkM44& matrix) {
fWriter.write(SkMatrixPriv::M44ColMajor(matrix), sizeof(float) * 16);
}
void SkBinaryWriteBuffer::writeMatrix(const SkMatrix& matrix) {
fWriter.writeMatrix(matrix);
}

View File

@ -16,6 +16,7 @@
class SkFactorySet;
class SkImage;
class SkM44;
class SkPath;
class SkRefCntSet;
@ -54,6 +55,7 @@ public:
virtual void writePoint(const SkPoint& point) = 0;
virtual void writePointArray(const SkPoint* point, uint32_t count) = 0;
virtual void writePoint3(const SkPoint3& point) = 0;
virtual void write(const SkM44&) = 0;
virtual void writeMatrix(const SkMatrix& matrix) = 0;
virtual void writeIRect(const SkIRect& rect) = 0;
virtual void writeRect(const SkRect& rect) = 0;
@ -115,6 +117,7 @@ public:
void writePoint(const SkPoint& point) override;
void writePointArray(const SkPoint* point, uint32_t count) override;
void writePoint3(const SkPoint3& point) override;
void write(const SkM44&) override;
void writeMatrix(const SkMatrix& matrix) override;
void writeIRect(const SkIRect& rect) override;
void writeRect(const SkRect& rect) override;

View File

@ -139,6 +139,20 @@ void JsonWriteBuffer::writePointArray(const SkPoint* point, uint32_t count) {
fWriter->endArray();
}
void JsonWriteBuffer::write(const SkM44& matrix) {
this->append("matrix");
fWriter->beginArray();
for (int r = 0; r < 4; ++r) {
fWriter->beginArray(nullptr, false);
SkV4 v = matrix.row(r);
for (int c = 0; c < 4; ++c) {
fWriter->appendFloat(v[c]);
}
fWriter->endArray();
}
fWriter->endArray();
}
void JsonWriteBuffer::writeMatrix(const SkMatrix& matrix) {
this->append("matrix");
DrawCommand::MakeJsonMatrix(*fWriter, matrix);

View File

@ -37,6 +37,7 @@ public:
void writePoint(const SkPoint& point) override;
void writePointArray(const SkPoint* point, uint32_t count) override;
void writePoint3(const SkPoint3& point) override;
void write(const SkM44&) override;
void writeMatrix(const SkMatrix& matrix) override;
void writeIRect(const SkIRect& rect) override;
void writeRect(const SkRect& rect) override;