2015-04-29 00:50:31 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2016-03-25 19:00:14 +00:00
|
|
|
class SK_API SkROBuffer : public SkRefCnt {
|
2015-04-29 00:50:31 +00:00
|
|
|
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.
|
|
|
|
*/
|
2016-04-08 19:47:14 +00:00
|
|
|
size_t size() const { return fAvailable; }
|
2016-04-04 17:05:23 +00:00
|
|
|
|
|
|
|
class SK_API Iter {
|
2015-04-29 00:50:31 +00:00
|
|
|
public:
|
|
|
|
Iter(const SkROBuffer*);
|
2017-06-01 13:43:01 +00:00
|
|
|
Iter(const sk_sp<SkROBuffer>&);
|
2015-04-29 00:50:31 +00:00
|
|
|
|
|
|
|
void reset(const SkROBuffer*);
|
|
|
|
|
|
|
|
/**
|
2015-08-27 14:41:13 +00:00
|
|
|
* Return the current continuous block of memory, or nullptr if the iterator is exhausted
|
2015-04-29 00:50:31 +00:00
|
|
|
*/
|
|
|
|
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();
|
2017-10-09 19:45:33 +00:00
|
|
|
|
2015-04-29 00:50:31 +00:00
|
|
|
private:
|
|
|
|
const SkBufferBlock* fBlock;
|
|
|
|
size_t fRemaining;
|
2016-04-22 13:59:01 +00:00
|
|
|
const SkROBuffer* fBuffer;
|
2015-04-29 00:50:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2016-04-22 13:59:01 +00:00
|
|
|
SkROBuffer(const SkBufferHead* head, size_t available, const SkBufferBlock* fTail);
|
2015-04-29 00:50:31 +00:00
|
|
|
virtual ~SkROBuffer();
|
2016-04-04 17:05:23 +00:00
|
|
|
|
2016-04-22 13:59:01 +00:00
|
|
|
const SkBufferHead* fHead;
|
|
|
|
const size_t fAvailable;
|
|
|
|
const SkBufferBlock* fTail;
|
2016-04-04 17:05:23 +00:00
|
|
|
|
2015-04-29 00:50:31 +00:00
|
|
|
friend class SkRWBuffer;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Accumulates bytes of memory that are "appended" to it, growing internal storage as needed.
|
2016-04-22 13:59:01 +00:00
|
|
|
* The growth is done such that at any time in the writer's thread, an RBuffer or StreamAsset
|
|
|
|
* can be snapped off (and safely passed to another thread). The RBuffer/StreamAsset snapshot
|
|
|
|
* can see the previously stored bytes, but will be unaware of any future writes.
|
2015-04-29 00:50:31 +00:00
|
|
|
*/
|
2016-03-25 19:00:14 +00:00
|
|
|
class SK_API SkRWBuffer {
|
2015-04-29 00:50:31 +00:00
|
|
|
public:
|
|
|
|
SkRWBuffer(size_t initialCapacity = 0);
|
|
|
|
~SkRWBuffer();
|
2016-04-04 17:05:23 +00:00
|
|
|
|
2015-04-29 00:50:31 +00:00
|
|
|
size_t size() const { return fTotalUsed; }
|
2016-09-30 20:34:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Append |length| bytes from |buffer|.
|
|
|
|
*
|
|
|
|
* If the caller knows in advance how much more data they are going to append, they can
|
|
|
|
* pass a |reserve| hint (representing the number of upcoming bytes *in addition* to the
|
|
|
|
* current append), to minimize the number of internal allocations.
|
|
|
|
*/
|
|
|
|
void append(const void* buffer, size_t length, size_t reserve = 0);
|
2015-04-29 00:50:31 +00:00
|
|
|
|
2017-06-01 13:43:01 +00:00
|
|
|
sk_sp<SkROBuffer> makeROBufferSnapshot() const {
|
|
|
|
return sk_sp<SkROBuffer>(new SkROBuffer(fHead, fTotalUsed, fTail));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<SkStreamAsset> makeStreamSnapshot() const;
|
|
|
|
|
2015-04-29 00:50:31 +00:00
|
|
|
#ifdef SK_DEBUG
|
|
|
|
void validate() const;
|
|
|
|
#else
|
|
|
|
void validate() const {}
|
|
|
|
#endif
|
2017-10-09 19:45:33 +00:00
|
|
|
|
2015-04-29 00:50:31 +00:00
|
|
|
private:
|
|
|
|
SkBufferHead* fHead;
|
|
|
|
SkBufferBlock* fTail;
|
|
|
|
size_t fTotalUsed;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|