diff options
author | scroggo <scroggo@chromium.org> | 2016-03-25 12:00:14 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-03-25 12:00:15 -0700 |
commit | 77899d122083644380c833a32bd4ab5212cb0ec2 (patch) | |
tree | 75c68d74fdcf52b5b45dbfa35c4696c0772a943b /include/core | |
parent | a50151dcb5a0b8bfdef383e363e519c91d2d2c7a (diff) |
Move SkRWBuffer.h to include/ and add SK_API
Needed for use in Blink in crrev.com/1812273003
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1838463003
Review URL: https://codereview.chromium.org/1838463003
Diffstat (limited to 'include/core')
-rw-r--r-- | include/core/SkRWBuffer.h | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/include/core/SkRWBuffer.h b/include/core/SkRWBuffer.h new file mode 100644 index 0000000000..452e182d94 --- /dev/null +++ b/include/core/SkRWBuffer.h @@ -0,0 +1,97 @@ +/* + * Copyright 2015 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef SkRWBuffer_DEFINED +#define SkRWBuffer_DEFINED + +#include "SkRefCnt.h" + +struct SkBufferBlock; +struct SkBufferHead; +class SkRWBuffer; +class SkStreamAsset; + +/** + * Contains a read-only, thread-sharable block of memory. To access the memory, the caller must + * instantiate a local iterator, as the memory is stored in 1 or more contiguous blocks. + */ +class SK_API SkROBuffer : public SkRefCnt { +public: + /** + * Return the logical length of the data owned/shared by this buffer. It may be stored in + * multiple contiguous blocks, accessible via the iterator. + */ + size_t size() const { return fUsed; } + + class Iter { + public: + Iter(const SkROBuffer*); + + void reset(const SkROBuffer*); + + /** + * Return the current continuous block of memory, or nullptr if the iterator is exhausted + */ + const void* data() const; + + /** + * Returns the number of bytes in the current continguous block of memory, or 0 if the + * iterator is exhausted. + */ + size_t size() const; + + /** + * Advance to the next contiguous block of memory, returning true if there is another + * block, or false if the iterator is exhausted. + */ + bool next(); + + private: + const SkBufferBlock* fBlock; + size_t fRemaining; + }; + +private: + SkROBuffer(const SkBufferHead* head, size_t used); + virtual ~SkROBuffer(); + + const SkBufferHead* fHead; + const size_t fUsed; + + friend class SkRWBuffer; +}; + +/** + * Accumulates bytes of memory that are "appended" to it, growing internal storage as needed. + * The growth is done such that at any time, a RBuffer or StreamAsset can be snapped off, which + * can see the previously stored bytes, but which will be unaware of any future writes. + */ +class SK_API SkRWBuffer { +public: + SkRWBuffer(size_t initialCapacity = 0); + ~SkRWBuffer(); + + size_t size() const { return fTotalUsed; } + void append(const void* buffer, size_t length); + void* append(size_t length); + + SkROBuffer* newRBufferSnapshot() const; + SkStreamAsset* newStreamSnapshot() const; + +#ifdef SK_DEBUG + void validate() const; +#else + void validate() const {} +#endif + +private: + SkBufferHead* fHead; + SkBufferBlock* fTail; + size_t fTotalUsed; +}; + +#endif |