Home | History | Annotate | Download | only in core
      1 
      2 /*
      3  * Copyright 2008 The Android Open Source Project
      4  *
      5  * Use of this source code is governed by a BSD-style license that can be
      6  * found in the LICENSE file.
      7  */
      8 
      9 
     10 #ifndef SkWriter32_DEFINED
     11 #define SkWriter32_DEFINED
     12 
     13 #include "../private/SkTemplates.h"
     14 #include "SkData.h"
     15 #include "SkMatrix.h"
     16 #include "SkPath.h"
     17 #include "SkPoint.h"
     18 #include "SkPoint3.h"
     19 #include "SkRRect.h"
     20 #include "SkRect.h"
     21 #include "SkRegion.h"
     22 #include "SkScalar.h"
     23 #include "SkStream.h"
     24 #include "SkTypes.h"
     25 
     26 class SK_API SkWriter32 : SkNoncopyable {
     27 public:
     28     /**
     29      *  The caller can specify an initial block of storage, which the caller manages.
     30      *
     31      *  SkWriter32 will try to back reserve and write calls with this external storage until the
     32      *  first time an allocation doesn't fit.  From then it will use dynamically allocated storage.
     33      *  This used to be optional behavior, but pipe now relies on it.
     34      */
     35     SkWriter32(void* external = nullptr, size_t externalBytes = 0) {
     36         this->reset(external, externalBytes);
     37     }
     38 
     39     // return the current offset (will always be a multiple of 4)
     40     size_t bytesWritten() const { return fUsed; }
     41 
     42     SK_ATTR_DEPRECATED("use bytesWritten")
     43     size_t size() const { return this->bytesWritten(); }
     44 
     45     void reset(void* external = nullptr, size_t externalBytes = 0) {
     46         SkASSERT(SkIsAlign4((uintptr_t)external));
     47         SkASSERT(SkIsAlign4(externalBytes));
     48 
     49         fData = (uint8_t*)external;
     50         fCapacity = externalBytes;
     51         fUsed = 0;
     52         fExternal = external;
     53     }
     54 
     55     // size MUST be multiple of 4
     56     uint32_t* reserve(size_t size) {
     57         SkASSERT(SkAlign4(size) == size);
     58         size_t offset = fUsed;
     59         size_t totalRequired = fUsed + size;
     60         if (totalRequired > fCapacity) {
     61             this->growToAtLeast(totalRequired);
     62         }
     63         fUsed = totalRequired;
     64         return (uint32_t*)(fData + offset);
     65     }
     66 
     67     /**
     68      *  Read a T record at offset, which must be a multiple of 4. Only legal if the record
     69      *  was written atomically using the write methods below.
     70      */
     71     template<typename T>
     72     const T& readTAt(size_t offset) const {
     73         SkASSERT(SkAlign4(offset) == offset);
     74         SkASSERT(offset < fUsed);
     75         return *(T*)(fData + offset);
     76     }
     77 
     78     /**
     79      *  Overwrite a T record at offset, which must be a multiple of 4. Only legal if the record
     80      *  was written atomically using the write methods below.
     81      */
     82     template<typename T>
     83     void overwriteTAt(size_t offset, const T& value) {
     84         SkASSERT(SkAlign4(offset) == offset);
     85         SkASSERT(offset < fUsed);
     86         *(T*)(fData + offset) = value;
     87     }
     88 
     89     bool writeBool(bool value) {
     90         this->write32(value);
     91         return value;
     92     }
     93 
     94     void writeInt(int32_t value) {
     95         this->write32(value);
     96     }
     97 
     98     void write8(int32_t value) {
     99         *(int32_t*)this->reserve(sizeof(value)) = value & 0xFF;
    100     }
    101 
    102     void write16(int32_t value) {
    103         *(int32_t*)this->reserve(sizeof(value)) = value & 0xFFFF;
    104     }
    105 
    106     void write32(int32_t value) {
    107         *(int32_t*)this->reserve(sizeof(value)) = value;
    108     }
    109 
    110     void writePtr(void* value) {
    111         *(void**)this->reserve(sizeof(value)) = value;
    112     }
    113 
    114     void writeScalar(SkScalar value) {
    115         *(SkScalar*)this->reserve(sizeof(value)) = value;
    116     }
    117 
    118     void writePoint(const SkPoint& pt) {
    119         *(SkPoint*)this->reserve(sizeof(pt)) = pt;
    120     }
    121 
    122     void writePoint3(const SkPoint3& pt) {
    123         *(SkPoint3*)this->reserve(sizeof(pt)) = pt;
    124     }
    125 
    126     void writeRect(const SkRect& rect) {
    127         *(SkRect*)this->reserve(sizeof(rect)) = rect;
    128     }
    129 
    130     void writeIRect(const SkIRect& rect) {
    131         *(SkIRect*)this->reserve(sizeof(rect)) = rect;
    132     }
    133 
    134     void writeRRect(const SkRRect& rrect) {
    135         rrect.writeToMemory(this->reserve(SkRRect::kSizeInMemory));
    136     }
    137 
    138     void writePath(const SkPath& path) {
    139         size_t size = path.writeToMemory(nullptr);
    140         SkASSERT(SkAlign4(size) == size);
    141         path.writeToMemory(this->reserve(size));
    142     }
    143 
    144     void writeMatrix(const SkMatrix& matrix);
    145 
    146     void writeRegion(const SkRegion& rgn) {
    147         size_t size = rgn.writeToMemory(nullptr);
    148         SkASSERT(SkAlign4(size) == size);
    149         rgn.writeToMemory(this->reserve(size));
    150     }
    151 
    152     // write count bytes (must be a multiple of 4)
    153     void writeMul4(const void* values, size_t size) {
    154         this->write(values, size);
    155     }
    156 
    157     /**
    158      *  Write size bytes from values. size must be a multiple of 4, though
    159      *  values need not be 4-byte aligned.
    160      */
    161     void write(const void* values, size_t size) {
    162         SkASSERT(SkAlign4(size) == size);
    163         sk_careful_memcpy(this->reserve(size), values, size);
    164     }
    165 
    166     /**
    167      *  Reserve size bytes. Does not need to be 4 byte aligned. The remaining space (if any) will be
    168      *  filled in with zeroes.
    169      */
    170     uint32_t* reservePad(size_t size) {
    171         size_t alignedSize = SkAlign4(size);
    172         uint32_t* p = this->reserve(alignedSize);
    173         if (alignedSize != size) {
    174             SkASSERT(alignedSize >= 4);
    175             p[alignedSize / 4 - 1] = 0;
    176         }
    177         return p;
    178     }
    179 
    180     /**
    181      *  Write size bytes from src, and pad to 4 byte alignment with zeroes.
    182      */
    183     void writePad(const void* src, size_t size) {
    184         sk_careful_memcpy(this->reservePad(size), src, size);
    185     }
    186 
    187     /**
    188      *  Writes a string to the writer, which can be retrieved with
    189      *  SkReader32::readString().
    190      *  The length can be specified, or if -1 is passed, it will be computed by
    191      *  calling strlen(). The length must be < max size_t.
    192      *
    193      *  If you write NULL, it will be read as "".
    194      */
    195     void writeString(const char* str, size_t len = (size_t)-1);
    196 
    197     /**
    198      *  Computes the size (aligned to multiple of 4) need to write the string
    199      *  in a call to writeString(). If the length is not specified, it will be
    200      *  computed by calling strlen().
    201      */
    202     static size_t WriteStringSize(const char* str, size_t len = (size_t)-1);
    203 
    204     void writeData(const SkData* data) {
    205         uint32_t len = data ? SkToU32(data->size()) : 0;
    206         this->write32(len);
    207         if (data) {
    208             this->writePad(data->data(), len);
    209         }
    210     }
    211 
    212     static size_t WriteDataSize(const SkData* data) {
    213         return 4 + SkAlign4(data ? data->size() : 0);
    214     }
    215 
    216     /**
    217      *  Move the cursor back to offset bytes from the beginning.
    218      *  offset must be a multiple of 4 no greater than size().
    219      */
    220     void rewindToOffset(size_t offset) {
    221         SkASSERT(SkAlign4(offset) == offset);
    222         SkASSERT(offset <= bytesWritten());
    223         fUsed = offset;
    224     }
    225 
    226     // copy into a single buffer (allocated by caller). Must be at least size()
    227     void flatten(void* dst) const {
    228         memcpy(dst, fData, fUsed);
    229     }
    230 
    231     bool writeToStream(SkWStream* stream) const {
    232         return stream->write(fData, fUsed);
    233     }
    234 
    235     // read from the stream, and write up to length bytes. Return the actual
    236     // number of bytes written.
    237     size_t readFromStream(SkStream* stream, size_t length) {
    238         return stream->read(this->reservePad(length), length);
    239     }
    240 
    241     /**
    242      *  Captures a snapshot of the data as it is right now, and return it.
    243      */
    244     sk_sp<SkData> snapshotAsData() const;
    245 private:
    246     void growToAtLeast(size_t size);
    247 
    248     uint8_t* fData;                    // Points to either fInternal or fExternal.
    249     size_t fCapacity;                  // Number of bytes we can write to fData.
    250     size_t fUsed;                      // Number of bytes written.
    251     void* fExternal;                   // Unmanaged memory block.
    252     SkAutoTMalloc<uint8_t> fInternal;  // Managed memory block.
    253 };
    254 
    255 /**
    256  *  Helper class to allocated SIZE bytes as part of the writer, and to provide
    257  *  that storage to the constructor as its initial storage buffer.
    258  *
    259  *  This wrapper ensures proper alignment rules are met for the storage.
    260  */
    261 template <size_t SIZE> class SkSWriter32 : public SkWriter32 {
    262 public:
    263     SkSWriter32() { this->reset(); }
    264 
    265     void reset() {this->INHERITED::reset(fData.fStorage, SIZE); }
    266 
    267 private:
    268     union {
    269         void*   fPtrAlignment;
    270         double  fDoubleAlignment;
    271         char    fStorage[SIZE];
    272     } fData;
    273 
    274     typedef SkWriter32 INHERITED;
    275 };
    276 
    277 #endif
    278