Home | History | Annotate | Download | only in core
      1 
      2 /*
      3  * Copyright 2011 Google Inc.
      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 #ifndef SkWriteBuffer_DEFINED
     10 #define SkWriteBuffer_DEFINED
     11 
     12 #include "SkBitmapHeap.h"
     13 #include "SkData.h"
     14 #include "SkPath.h"
     15 #include "SkPicture.h"
     16 #include "SkRefCnt.h"
     17 #include "SkWriter32.h"
     18 
     19 class SkBitmap;
     20 class SkFactorySet;
     21 class SkFlattenable;
     22 class SkNamedFactorySet;
     23 class SkRefCntSet;
     24 
     25 class SkWriteBuffer {
     26 public:
     27     enum Flags {
     28         kCrossProcess_Flag  = 1 << 0,
     29         kValidation_Flag    = 1 << 1,
     30     };
     31 
     32     SkWriteBuffer(uint32_t flags = 0);
     33     SkWriteBuffer(void* initialStorage, size_t storageSize, uint32_t flags = 0);
     34     ~SkWriteBuffer();
     35 
     36     bool isCrossProcess() const {
     37         return this->isValidating() || SkToBool(fFlags & kCrossProcess_Flag);
     38     }
     39 
     40     SkWriter32* getWriter32() { return &fWriter; }
     41     void reset(void* storage = NULL, size_t storageSize = 0) {
     42         fWriter.reset(storage, storageSize);
     43     }
     44 
     45     uint32_t* reserve(size_t size) { return fWriter.reserve(size); }
     46 
     47     size_t bytesWritten() const { return fWriter.bytesWritten(); }
     48 
     49     void writeByteArray(const void* data, size_t size);
     50     void writeDataAsByteArray(SkData* data) { this->writeByteArray(data->data(), data->size()); }
     51     void writeBool(bool value);
     52     void writeFixed(SkFixed value);
     53     void writeScalar(SkScalar value);
     54     void writeScalarArray(const SkScalar* value, uint32_t count);
     55     void writeInt(int32_t value);
     56     void writeIntArray(const int32_t* value, uint32_t count);
     57     void writeUInt(uint32_t value);
     58     void write32(int32_t value);
     59     void writeString(const char* value);
     60     void writeEncodedString(const void* value, size_t byteLength, SkPaint::TextEncoding encoding);
     61     void writeFunctionPtr(void* ptr) { fWriter.writePtr(ptr); }
     62 
     63     void writeFlattenable(const SkFlattenable* flattenable);
     64     void writeColor(const SkColor& color);
     65     void writeColorArray(const SkColor* color, uint32_t count);
     66     void writePoint(const SkPoint& point);
     67     void writePointArray(const SkPoint* point, uint32_t count);
     68     void writeMatrix(const SkMatrix& matrix);
     69     void writeIRect(const SkIRect& rect);
     70     void writeRect(const SkRect& rect);
     71     void writeRegion(const SkRegion& region);
     72     void writePath(const SkPath& path);
     73     size_t writeStream(SkStream* stream, size_t length);
     74     void writeBitmap(const SkBitmap& bitmap);
     75     void writeTypeface(SkTypeface* typeface);
     76     void writePaint(const SkPaint& paint) { paint.flatten(*this); }
     77 
     78     bool writeToStream(SkWStream*);
     79     void writeToMemory(void* dst) { fWriter.flatten(dst); }
     80 
     81     SkFactorySet* setFactoryRecorder(SkFactorySet*);
     82     SkNamedFactorySet* setNamedFactoryRecorder(SkNamedFactorySet*);
     83 
     84     SkRefCntSet* getTypefaceRecorder() const { return fTFSet; }
     85     SkRefCntSet* setTypefaceRecorder(SkRefCntSet*);
     86 
     87     /**
     88      * Set an SkBitmapHeap to store bitmaps rather than flattening.
     89      *
     90      * Incompatible with an EncodeBitmap function. If an EncodeBitmap function is set, setting an
     91      * SkBitmapHeap will set the function to NULL in release mode and crash in debug.
     92      */
     93     void setBitmapHeap(SkBitmapHeap*);
     94 
     95     /**
     96      * Provide a function to encode an SkBitmap to an SkData. writeBitmap will attempt to use
     97      * bitmapEncoder to store the SkBitmap. If the reader does not provide a function to decode, it
     98      * will not be able to restore SkBitmaps, but will still be able to read the rest of the stream.
     99      * bitmapEncoder will never be called with a NULL pixelRefOffset.
    100      *
    101      * Incompatible with the SkBitmapHeap. If an encoder is set fBitmapHeap will be set to NULL in
    102      * release and crash in debug.
    103      */
    104     void setBitmapEncoder(SkPicture::EncodeBitmap bitmapEncoder);
    105 
    106 private:
    107     bool isValidating() const { return SkToBool(fFlags & kValidation_Flag); }
    108 
    109     const uint32_t fFlags;
    110     SkFactorySet* fFactorySet;
    111     SkNamedFactorySet* fNamedFactorySet;
    112     SkWriter32 fWriter;
    113 
    114     SkBitmapHeap* fBitmapHeap;
    115     SkRefCntSet* fTFSet;
    116 
    117     SkPicture::EncodeBitmap fBitmapEncoder;
    118 };
    119 
    120 #endif // SkWriteBuffer_DEFINED
    121