aboutsummaryrefslogtreecommitdiffhomepage
path: root/include
diff options
context:
space:
mode:
authorGravatar djsollen@google.com <djsollen@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2012-04-12 13:24:04 +0000
committerGravatar djsollen@google.com <djsollen@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2012-04-12 13:24:04 +0000
commit2b2ede3e713065e1bac461787b0aafb03eaf871f (patch)
tree180a113560b219569ef06556982ec99fcac5ca36 /include
parent7463f190972f45157242d2f1f4368de78e5863d7 (diff)
Decouple the SkFlattenable from SkReader32/SkWriter32.
The current impl for SkFlattenable read/write buffers is that they extend from SkReader32 and SkWriter32, but that dependency must be abstract if we are to add any other serialization format. Review URL: https://codereview.appspot.com/5999045 git-svn-id: http://skia.googlecode.com/svn/trunk@3654 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'include')
-rw-r--r--include/core/SkFlattenable.h106
-rw-r--r--include/core/SkOrderedReadBuffer.h61
-rw-r--r--include/core/SkOrderedWriteBuffer.h59
-rw-r--r--include/core/SkReader32.h14
-rw-r--r--include/core/SkWriter32.h14
5 files changed, 229 insertions, 25 deletions
diff --git a/include/core/SkFlattenable.h b/include/core/SkFlattenable.h
index ba1495ff66..5714f9d542 100644
--- a/include/core/SkFlattenable.h
+++ b/include/core/SkFlattenable.h
@@ -12,6 +12,8 @@
#include "SkRefCnt.h"
#include "SkBitmap.h"
+#include "SkPath.h"
+#include "SkPoint.h"
#include "SkReader32.h"
#include "SkTDArray.h"
#include "SkWriter32.h"
@@ -119,12 +121,38 @@ extern void SkWriteRegion(SkWriter32*, const SkRegion&);
class SkTypeface;
-class SkFlattenableReadBuffer : public SkReader32 {
+class SkFlattenableReadBuffer {
public:
SkFlattenableReadBuffer();
- explicit SkFlattenableReadBuffer(const void* data);
- SkFlattenableReadBuffer(const void* data, size_t size);
-
+ virtual ~SkFlattenableReadBuffer() {}
+
+
+ virtual uint8_t readU8() = 0;
+ virtual uint16_t readU16() = 0;
+ virtual uint32_t readU32() = 0;
+ virtual void read(void* dst, size_t size) = 0;
+ virtual bool readBool() = 0;
+ virtual int32_t readInt() = 0;
+ virtual SkScalar readScalar() = 0;
+ virtual const void* skip(size_t size) = 0;
+
+ virtual int32_t readS32() { return readInt(); }
+ template <typename T> const T& skipT() {
+ SkASSERT(SkAlign4(sizeof(T)) == sizeof(T));
+ return *(const T*)this->skip(sizeof(T));
+ }
+
+ virtual void readMatrix(SkMatrix*) = 0;
+ virtual void readPath(SkPath*) = 0;
+ virtual void readPoint(SkPoint*) = 0;
+
+ // helper function for classes with const SkPoint members
+ SkPoint readPoint() {
+ SkPoint point;
+ this->readPoint(&point);
+ return point;
+ }
+
void setRefCntArray(SkRefCnt* array[], int count) {
fRCArray = array;
fRCCount = count;
@@ -156,12 +184,12 @@ public:
fFactoryCount = 0;
}
- SkTypeface* readTypeface();
- SkRefCnt* readRefCnt();
- void* readFunctionPtr();
- SkFlattenable* readFlattenable();
+ virtual SkTypeface* readTypeface() = 0;
+ virtual SkRefCnt* readRefCnt() = 0;
+ virtual void* readFunctionPtr() = 0;
+ virtual SkFlattenable* readFlattenable() = 0;
-private:
+protected:
SkRefCnt** fRCArray;
int fRCCount;
@@ -171,8 +199,6 @@ private:
SkTDArray<SkFlattenable::Factory>* fFactoryTDArray;
SkFlattenable::Factory* fFactoryArray;
int fFactoryCount;
-
- typedef SkReader32 INHERITED;
};
///////////////////////////////////////////////////////////////////////////////
@@ -187,7 +213,7 @@ private:
class SkRefCntSet : public SkTPtrSet<SkRefCnt*> {
public:
virtual ~SkRefCntSet();
-
+
protected:
// overrides
virtual void incPtr(void*);
@@ -196,22 +222,47 @@ protected:
class SkFactorySet : public SkTPtrSet<SkFlattenable::Factory> {};
-class SkFlattenableWriteBuffer : public SkWriter32 {
+class SkFlattenableWriteBuffer {
public:
- SkFlattenableWriteBuffer(size_t minSize);
+ SkFlattenableWriteBuffer();
virtual ~SkFlattenableWriteBuffer();
+ // deprecated naming convention that will be removed after callers are updated
+ virtual bool writeBool(bool value) = 0;
+ virtual void writeInt(int32_t value) = 0;
+ virtual void write8(int32_t value) = 0;
+ virtual void write16(int32_t value) = 0;
+ virtual void write32(int32_t value) = 0;
+ virtual void writeScalar(SkScalar value) = 0;
+ virtual void writeMul4(const void* values, size_t size) = 0;
+
+ virtual void writePad(const void* src, size_t size) = 0;
+ virtual void writeString(const char* str, size_t len = (size_t)-1) = 0;
+ virtual uint32_t* reserve(size_t size) = 0;
+ virtual void flatten(void* dst) = 0;
+ virtual uint32_t size() = 0;
+ virtual void write(const void* values, size_t size) = 0;
+ virtual void writeRect(const SkRect& rect) = 0;
+ virtual size_t readFromStream(SkStream*, size_t length) = 0;
+
+ virtual void writeMatrix(const SkMatrix& matrix) = 0;
+ virtual void writePath(const SkPath& path) = 0;
+ virtual void writePoint(const SkPoint& point) = 0;
+
+ virtual bool writeToStream(SkWStream*) = 0;
+
+ virtual void writeFunctionPtr(void*)= 0;
+ virtual void writeFlattenable(SkFlattenable* flattenable)= 0;
+
void writeTypeface(SkTypeface*);
- void writeRefCnt(SkRefCnt*);
- void writeFunctionPtr(void*);
- void writeFlattenable(SkFlattenable* flattenable);
-
+ void writeRefCnt(SkRefCnt* obj);
+
SkRefCntSet* getTypefaceRecorder() const { return fTFSet; }
SkRefCntSet* setTypefaceRecorder(SkRefCntSet*);
-
+
SkRefCntSet* getRefCntRecorder() const { return fRCSet; }
SkRefCntSet* setRefCntRecorder(SkRefCntSet*);
-
+
SkFactorySet* getFactoryRecorder() const { return fFactorySet; }
SkFactorySet* setFactoryRecorder(SkFactorySet*);
@@ -225,7 +276,7 @@ public:
};
Flags getFlags() const { return (Flags)fFlags; }
void setFlags(Flags flags) { fFlags = flags; }
-
+
bool isCrossProcess() const {
return SkToBool(fFlags & kCrossProcess_Flag);
}
@@ -236,16 +287,21 @@ public:
bool persistBitmapPixels() const {
return (fFlags & kCrossProcess_Flag) != 0;
}
-
+
bool persistTypeface() const { return (fFlags & kCrossProcess_Flag) != 0; }
-private:
+protected:
+
+ // A helper function so that each subclass does not have to be a friend of
+ // SkFlattenable.
+ void flattenObject(SkFlattenable* obj, SkFlattenableWriteBuffer& buffer) {
+ obj->flatten(buffer);
+ }
+
uint32_t fFlags;
SkRefCntSet* fTFSet;
SkRefCntSet* fRCSet;
SkFactorySet* fFactorySet;
-
- typedef SkWriter32 INHERITED;
};
#endif
diff --git a/include/core/SkOrderedReadBuffer.h b/include/core/SkOrderedReadBuffer.h
new file mode 100644
index 0000000000..c1dbc7e251
--- /dev/null
+++ b/include/core/SkOrderedReadBuffer.h
@@ -0,0 +1,61 @@
+
+/*
+ * Copyright 2011 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef SkOrderedReadBuffer_DEFINED
+#define SkOrderedReadBuffer_DEFINED
+
+#include "SkRefCnt.h"
+#include "SkBitmap.h"
+#include "SkFlattenable.h"
+#include "SkWriter32.h"
+#include "SkPath.h"
+
+class SkOrderedReadBuffer : public SkFlattenableReadBuffer {
+public:
+ SkOrderedReadBuffer() : INHERITED() {}
+ SkOrderedReadBuffer(const void* data, size_t size);
+
+ void setMemory(const void* data, size_t size) { fReader.setMemory(data, size); }
+ uint32_t size() { return fReader.size(); }
+ const void* base() { return fReader.base(); }
+ uint32_t offset() { return fReader.offset(); }
+ bool eof() { return fReader.eof(); }
+ void rewind() { fReader.rewind(); }
+ void setOffset(size_t offset) { fReader.setOffset(offset); }
+
+ SkReader32* getReader32() { return &fReader; }
+
+ virtual uint8_t readU8() { return fReader.readU8(); }
+ virtual uint16_t readU16() { return fReader.readU16(); }
+ virtual uint32_t readU32() { return fReader.readU32(); }
+ virtual void read(void* dst, size_t size) { return fReader.read(dst, size); }
+ virtual bool readBool() { return fReader.readBool(); }
+ virtual int32_t readInt() { return fReader.readInt(); }
+ virtual SkScalar readScalar() { return fReader.readScalar(); }
+ virtual const void* skip(size_t size) { return fReader.skip(size); }
+
+ virtual void readMatrix(SkMatrix* m) { fReader.readMatrix(m); }
+ virtual void readPath(SkPath* p) { p->unflatten(fReader); }
+ virtual void readPoint(SkPoint* p) {
+ p->fX = fReader.readScalar();
+ p->fY = fReader.readScalar();
+ }
+
+ virtual SkTypeface* readTypeface();
+ virtual SkRefCnt* readRefCnt();
+ virtual void* readFunctionPtr();
+ virtual SkFlattenable* readFlattenable();
+
+private:
+ SkReader32 fReader;
+
+ typedef SkFlattenableReadBuffer INHERITED;
+};
+
+#endif // SkOrderedReadBuffer_DEFINED
+
diff --git a/include/core/SkOrderedWriteBuffer.h b/include/core/SkOrderedWriteBuffer.h
new file mode 100644
index 0000000000..6183218af3
--- /dev/null
+++ b/include/core/SkOrderedWriteBuffer.h
@@ -0,0 +1,59 @@
+
+/*
+ * Copyright 2011 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef SkOrderedWriteBuffer_DEFINED
+#define SkOrderedWriteBuffer_DEFINED
+
+#include "SkRefCnt.h"
+#include "SkBitmap.h"
+#include "SkFlattenable.h"
+#include "SkWriter32.h"
+#include "SkPath.h"
+
+class SkOrderedWriteBuffer : public SkFlattenableWriteBuffer {
+public:
+ SkOrderedWriteBuffer(size_t minSize);
+ virtual ~SkOrderedWriteBuffer() {}
+
+ // deprecated naming convention that will be removed after callers are updated
+ virtual bool writeBool(bool value) { return fWriter.writeBool(value); }
+ virtual void writeInt(int32_t value) { fWriter.writeInt(value); }
+ virtual void write8(int32_t value) { fWriter.write8(value); }
+ virtual void write16(int32_t value) { fWriter.write16(value); }
+ virtual void write32(int32_t value) { fWriter.write32(value); }
+ virtual void writeScalar(SkScalar value) { fWriter.writeScalar(value); }
+ virtual void writeMul4(const void* values, size_t size) { fWriter.writeMul4(values, size); }
+
+ virtual void writePad(const void* src, size_t size) { fWriter.writePad(src, size); }
+ virtual void writeString(const char* str, size_t len = (size_t)-1) { fWriter.writeString(str, len); }
+ virtual bool writeToStream(SkWStream* stream) { return fWriter.writeToStream(stream); }
+ virtual void write(const void* values, size_t size) { fWriter.write(values, size); }
+ virtual void writeRect(const SkRect& rect) { fWriter.writeRect(rect); }
+ virtual size_t readFromStream(SkStream* s, size_t length) { return fWriter.readFromStream(s, length); }
+
+ virtual void writeMatrix(const SkMatrix& matrix) { fWriter.writeMatrix(matrix); }
+ virtual void writePath(const SkPath& path) { path.flatten(fWriter); };
+ virtual void writePoint(const SkPoint& point) {
+ fWriter.writeScalar(point.fX);
+ fWriter.writeScalar(point.fY);
+ }
+
+ virtual uint32_t* reserve(size_t size) { return fWriter.reserve(size); }
+ virtual void flatten(void* dst) { fWriter.flatten(dst); }
+ virtual uint32_t size() { return fWriter.size(); }
+
+ virtual void writeFunctionPtr(void*);
+ virtual void writeFlattenable(SkFlattenable* flattenable);
+
+private:
+ SkWriter32 fWriter;
+ typedef SkFlattenableWriteBuffer INHERITED;
+};
+
+#endif // SkOrderedWriteBuffer_DEFINED
+
diff --git a/include/core/SkReader32.h b/include/core/SkReader32.h
index 37ace5c5d6..98385e430a 100644
--- a/include/core/SkReader32.h
+++ b/include/core/SkReader32.h
@@ -10,6 +10,8 @@
#ifndef SkReader32_DEFINED
#define SkReader32_DEFINED
+#include "SkMatrix.h"
+#include "SkRegion.h"
#include "SkScalar.h"
class SkString;
@@ -90,6 +92,18 @@ public:
int32_t readS32() { return this->readInt(); }
uint32_t readU32() { return this->readInt(); }
+ void readMatrix(SkMatrix* matrix) {
+ size_t size = matrix->unflatten(this->peek());
+ SkASSERT(SkAlign4(size) == size);
+ (void)this->skip(size);
+ }
+
+ void readRegion(SkRegion* rgn) {
+ size_t size = rgn->unflatten(this->peek());
+ SkASSERT(SkAlign4(size) == size);
+ (void)this->skip(size);
+ }
+
/**
* Read the length of a string (written by SkWriter32::writeString) into
* len (if len is not NULL) and return the null-ternimated address of the
diff --git a/include/core/SkWriter32.h b/include/core/SkWriter32.h
index d7159ff290..1b475958fb 100644
--- a/include/core/SkWriter32.h
+++ b/include/core/SkWriter32.h
@@ -15,6 +15,8 @@
#include "SkScalar.h"
#include "SkPoint.h"
#include "SkRect.h"
+#include "SkMatrix.h"
+#include "SkRegion.h"
class SkStream;
class SkWStream;
@@ -76,7 +78,19 @@ public:
void writeRect(const SkRect& rect) {
*(SkRect*)this->reserve(sizeof(rect)) = rect;
}
+
+ void writeMatrix(const SkMatrix& matrix) {
+ size_t size = matrix.flatten(NULL);
+ SkASSERT(SkAlign4(size) == size);
+ matrix.flatten(this->reserve(size));
+ }
+ void writeRegion(const SkRegion& rgn) {
+ size_t size = rgn.flatten(NULL);
+ SkASSERT(SkAlign4(size) == size);
+ rgn.flatten(this->reserve(size));
+ }
+
// write count bytes (must be a multiple of 4)
void writeMul4(const void* values, size_t size) {
this->write(values, size);