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