diff options
author | 2011-06-24 19:12:12 +0000 | |
---|---|---|
committer | 2011-06-24 19:12:12 +0000 | |
commit | 8a85d0c4938173476d037d7af0ee3b9436a1234e (patch) | |
tree | 3eac1645f722282933e2900f21cfc7c15ab81206 /include | |
parent | dcab3a15c98dc56960cd0a4dc84ae69089e89be3 (diff) |
replace detach/getStream apis on dynamicwstream with SkData
http://codereview.appspot.com/4657046/
git-svn-id: http://skia.googlecode.com/svn/trunk@1714 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'include')
-rw-r--r-- | include/core/SkData.h | 47 | ||||
-rw-r--r-- | include/core/SkStream.h | 41 | ||||
-rw-r--r-- | include/svg/SkSVGParser.h | 2 |
3 files changed, 74 insertions, 16 deletions
diff --git a/include/core/SkData.h b/include/core/SkData.h index 8bb3a2968c..07e7f4bb4f 100644 --- a/include/core/SkData.h +++ b/include/core/SkData.h @@ -72,6 +72,12 @@ public: ReleaseProc proc, void* context); /** + * Create a new dataref, reference the data ptr as is, and calling + * sk_free to delete it. + */ + static SkData* NewFromMalloc(const void* data, size_t length); + + /** * Create a new dataref using a subset of the data in the specified * src dataref. */ @@ -94,4 +100,45 @@ private: ~SkData(); }; +/** + * Specialized version of SkAutoTUnref<SkData> for automatically unref-ing a + * SkData. If the SkData is null, data(), bytes() and size() will return 0. + */ +class SkAutoDataUnref : SkNoncopyable { +public: + SkAutoDataUnref(SkData* data) : fRef(data) { + if (data) { + fData = data->data(); + fSize = data->size(); + } else { + fData = NULL; + fSize = 0; + } + } + ~SkAutoDataUnref() { + SkSafeUnref(fRef); + } + + const void* data() const { return fData; } + const uint8_t* bytes() const { + return reinterpret_cast<const uint8_t*> (fData); + } + size_t size() const { return fSize; } + SkData* get() const { return fRef; } + + void release() { + if (fRef) { + fRef->unref(); + fRef = NULL; + fData = NULL; + fSize = 0; + } + } + +private: + SkData* fRef; + const void* fData; + size_t fSize; +}; + #endif diff --git a/include/core/SkStream.h b/include/core/SkStream.h index 915afdd672..9b26787f31 100644 --- a/include/core/SkStream.h +++ b/include/core/SkStream.h @@ -102,6 +102,8 @@ public: bool writePackedUInt(size_t); bool writeStream(SkStream* input, size_t length); + + bool writeData(const SkData*); }; //////////////////////////////////////////////////////////////////////////////////////// @@ -184,6 +186,20 @@ public: will be freed with sk_free. */ void setMemoryOwned(const void* data, size_t length); + + /** + * Return the stream's data in a SkData. The caller must call unref() when + * it is finished using the data. + */ + SkData* copyToData() const; + + /** + * Use the specified data as the memory for this stream. The stream will + * call ref() on the data (assuming it is not null). The function returns + * the data parameter as a convenience. + */ + SkData* setData(SkData*); + void skipToAlign4(); virtual bool rewind(); virtual size_t read(void* buffer, size_t size); @@ -193,9 +209,8 @@ public: size_t peek() const { return fOffset; } private: - const void* fSrc; - size_t fSize, fOffset; - SkBool8 fWeOwnTheData; + SkData* fData; + size_t fOffset; }; /** \class SkBufferStream @@ -275,27 +290,21 @@ class SK_API SkDynamicMemoryWStream : public SkWStream { public: SkDynamicMemoryWStream(); virtual ~SkDynamicMemoryWStream(); + virtual bool write(const void* buffer, size_t size); // random access write // modifies stream and returns true if offset + size is less than or equal to getOffset() bool write(const void* buffer, size_t offset, size_t size); bool read(void* buffer, size_t offset, size_t size); size_t getOffset() const { return fBytesWritten; } + size_t bytesWritten() const { return fBytesWritten; } // copy what has been written to the stream into dst - void copyTo(void* dst) const; - /* return a cache of the flattened data returned by copyTo(). - This copy is only valid until the next call to write(). - The memory is managed by the stream class. - */ - const char* getStream() const; - - // same as getStream, but additionally detach the flattened datat - // DEPRECATED : use copyToData() instead - const char* detach() const; + void copyTo(void* dst) const; /** - * Return a copy of the data written so far + * Return a copy of the data written so far. This call is responsible for + * calling unref() when they are finished with the data. */ SkData* copyToData() const; @@ -307,7 +316,9 @@ private: Block* fHead; Block* fTail; size_t fBytesWritten; - mutable char* fCopyToCache; + mutable SkData* fCopy; // is invalidated if we write after it is created + + void invalidateCopy(); }; diff --git a/include/svg/SkSVGParser.h b/include/svg/SkSVGParser.h index 83b120dd64..dd116bdd1f 100644 --- a/include/svg/SkSVGParser.h +++ b/include/svg/SkSVGParser.h @@ -42,7 +42,7 @@ public: fXMLWriter.addAttributeLen(attrName, attrValue, len); } void _endElement() { fXMLWriter.endElement(); } int findAttribute(SkSVGBase* , const char* attrValue, size_t len, bool isPaint); - const char* getFinal(); +// const char* getFinal(); SkTDict<SkSVGElement*>& getIDs() { return fIDs; } SkString& getPaintLast(SkSVGPaint::Field field); void _startElement(const char name[]) { fXMLWriter.startElement(name); } |