Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright 2015 Google Inc.
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 #ifndef SkRWBuffer_DEFINED
      9 #define SkRWBuffer_DEFINED
     10 
     11 #include "SkRefCnt.h"
     12 
     13 struct SkBufferBlock;
     14 struct SkBufferHead;
     15 class SkRWBuffer;
     16 class SkStreamAsset;
     17 
     18 /**
     19  *  Contains a read-only, thread-sharable block of memory. To access the memory, the caller must
     20  *  instantiate a local iterator, as the memory is stored in 1 or more contiguous blocks.
     21  */
     22 class SkROBuffer : public SkRefCnt {
     23 public:
     24     /**
     25      *  Return the logical length of the data owned/shared by this buffer. It may be stored in
     26      *  multiple contiguous blocks, accessible via the iterator.
     27      */
     28     size_t size() const { return fUsed; }
     29 
     30     class Iter {
     31     public:
     32         Iter(const SkROBuffer*);
     33 
     34         void reset(const SkROBuffer*);
     35 
     36         /**
     37          *  Return the current continuous block of memory, or NULL if the iterator is exhausted
     38          */
     39         const void* data() const;
     40 
     41         /**
     42          *  Returns the number of bytes in the current continguous block of memory, or 0 if the
     43          *  iterator is exhausted.
     44          */
     45         size_t size() const;
     46 
     47         /**
     48          *  Advance to the next contiguous block of memory, returning true if there is another
     49          *  block, or false if the iterator is exhausted.
     50          */
     51         bool next();
     52 
     53     private:
     54         const SkBufferBlock* fBlock;
     55         size_t               fRemaining;
     56     };
     57 
     58 private:
     59     SkROBuffer(const SkBufferHead* head, size_t used);
     60     virtual ~SkROBuffer();
     61 
     62     const SkBufferHead* fHead;
     63     const size_t        fUsed;
     64 
     65     friend class SkRWBuffer;
     66 };
     67 
     68 /**
     69  *  Accumulates bytes of memory that are "appended" to it, growing internal storage as needed.
     70  *  The growth is done such that at any time, a RBuffer or StreamAsset can be snapped off, which
     71  *  can see the previously stored bytes, but which will be unaware of any future writes.
     72  */
     73 class SkRWBuffer {
     74 public:
     75     SkRWBuffer(size_t initialCapacity = 0);
     76     ~SkRWBuffer();
     77 
     78     size_t size() const { return fTotalUsed; }
     79     void append(const void* buffer, size_t length);
     80     void* append(size_t length);
     81 
     82     SkROBuffer* newRBufferSnapshot() const;
     83     SkStreamAsset* newStreamSnapshot() const;
     84 
     85 #ifdef SK_DEBUG
     86     void validate() const;
     87 #else
     88     void validate() const {}
     89 #endif
     90 
     91 private:
     92     SkBufferHead*   fHead;
     93     SkBufferBlock*  fTail;
     94     size_t          fTotalUsed;
     95 };
     96 
     97 #endif
     98