Home | History | Annotate | Download | only in core
      1 
      2 /*
      3  * Copyright 2012 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 SkFlattenableBuffers_DEFINED
     10 #define SkFlattenableBuffers_DEFINED
     11 
     12 #include "SkColor.h"
     13 #include "SkPaint.h"
     14 #include "SkPoint.h"
     15 
     16 class SkBitmap;
     17 class SkFlattenable;
     18 struct SkIRect;
     19 class SkMatrix;
     20 class SkOrderedReadBuffer;
     21 class SkOrderedWriteBuffer;
     22 class SkPath;
     23 class SkPixelRef;
     24 struct SkRect;
     25 class SkRefCnt;
     26 class SkRegion;
     27 class SkStream;
     28 class SkString;
     29 class SkTypeface;
     30 class SkWStream;
     31 
     32 class SkFlattenableReadBuffer {
     33 public:
     34     SkFlattenableReadBuffer();
     35     virtual ~SkFlattenableReadBuffer();
     36 
     37     bool isOrderedBinaryBuffer() { return NULL != getOrderedBinaryBuffer(); }
     38     virtual SkOrderedReadBuffer* getOrderedBinaryBuffer() { return NULL; }
     39 
     40     enum Flags {
     41         kCrossProcess_Flag      = 1 << 0,
     42         kScalarIsFloat_Flag     = 1 << 1,
     43         kPtrIs64Bit_Flag        = 1 << 2,
     44     };
     45 
     46     void setFlags(uint32_t flags) { fFlags = flags; }
     47     uint32_t getFlags() const { return fFlags; }
     48 
     49     bool isCrossProcess() const { return SkToBool(fFlags & kCrossProcess_Flag); }
     50     bool isScalarFloat() const { return SkToBool(fFlags & kScalarIsFloat_Flag); }
     51     bool isPtr64Bit() const { return SkToBool(fFlags & kPtrIs64Bit_Flag); }
     52 
     53     // primitives
     54     virtual bool readBool() = 0;
     55     virtual SkColor readColor() = 0;
     56     virtual SkFixed readFixed() = 0;
     57     virtual int32_t readInt() = 0;
     58     virtual SkScalar readScalar() = 0;
     59     virtual uint32_t readUInt() = 0;
     60     virtual int32_t read32() = 0;
     61 
     62     // strings -- the caller is responsible for freeing the string contents
     63     virtual void readString(SkString* string) = 0;
     64     virtual void* readEncodedString(size_t* length, SkPaint::TextEncoding encoding) = 0;
     65 
     66     // common data structures
     67     virtual SkFlattenable* readFlattenable() = 0;
     68     virtual void readPoint(SkPoint* point) = 0;
     69     virtual void readMatrix(SkMatrix* matrix) = 0;
     70     virtual void readIRect(SkIRect* rect) = 0;
     71     virtual void readRect(SkRect* rect) = 0;
     72     virtual void readRegion(SkRegion* region) = 0;
     73     virtual void readPath(SkPath* path) = 0;
     74 
     75     // binary data and arrays
     76     virtual uint32_t readByteArray(void* value) = 0;
     77     virtual uint32_t readColorArray(SkColor* colors) = 0;
     78     virtual uint32_t readIntArray(int32_t* values) = 0;
     79     virtual uint32_t readPointArray(SkPoint* points) = 0;
     80     virtual uint32_t readScalarArray(SkScalar* values) = 0;
     81 
     82     /** This helper peeks into the buffer and reports back the length of the next array in
     83      *  the buffer but does not change the state of the buffer.
     84      */
     85     virtual uint32_t getArrayCount() = 0;
     86 
     87     // helper functions
     88     virtual void* readFunctionPtr();
     89     virtual void readPaint(SkPaint* paint);
     90 
     91     virtual void readBitmap(SkBitmap* bitmap) = 0;
     92     virtual SkTypeface* readTypeface() = 0;
     93 
     94     // helper function for classes with const SkPoint members
     95     SkPoint readPoint() {
     96         SkPoint point;
     97         this->readPoint(&point);
     98         return point;
     99     }
    100 
    101     template <typename T> T* readFlattenableT() {
    102         return static_cast<T*>(this->readFlattenable());
    103     }
    104 
    105 private:
    106     uint32_t fFlags;
    107 };
    108 
    109 ///////////////////////////////////////////////////////////////////////////////
    110 
    111 class SkFlattenableWriteBuffer {
    112 public:
    113     SkFlattenableWriteBuffer();
    114     virtual ~SkFlattenableWriteBuffer();
    115 
    116     virtual bool isOrderedBinaryBuffer() { return false; }
    117     virtual SkOrderedWriteBuffer* getOrderedBinaryBuffer() { sk_throw(); return NULL; }
    118 
    119     // primitives
    120     virtual void writeByteArray(const void* data, size_t size) = 0;
    121     virtual void writeBool(bool value) = 0;
    122     virtual void writeFixed(SkFixed value) = 0;
    123     virtual void writeScalar(SkScalar value) = 0;
    124     virtual void writeScalarArray(const SkScalar* value, uint32_t count) = 0;
    125     virtual void writeInt(int32_t value) = 0;
    126     virtual void writeIntArray(const int32_t* value, uint32_t count) = 0;
    127     virtual void writeUInt(uint32_t value) = 0;
    128     virtual void write32(int32_t value) = 0; // printf in hex
    129     virtual void writeString(const char* value) = 0;
    130     virtual void writeEncodedString(const void* value, size_t byteLength,
    131                                     SkPaint::TextEncoding encoding) = 0;
    132 
    133     // common data structures
    134     virtual void writeFlattenable(SkFlattenable* flattenable) = 0;
    135     virtual void writeColor(const SkColor& color) = 0;
    136     virtual void writeColorArray(const SkColor* color, uint32_t count) = 0;
    137     virtual void writePoint(const SkPoint& point) = 0;
    138     virtual void writePointArray(const SkPoint* points, uint32_t count) = 0;
    139     virtual void writeMatrix(const SkMatrix& matrix) = 0;
    140     virtual void writeIRect(const SkIRect& rect) = 0;
    141     virtual void writeRect(const SkRect& rect) = 0;
    142     virtual void writeRegion(const SkRegion& region) = 0;
    143     virtual void writePath(const SkPath& path) = 0;
    144     virtual size_t writeStream(SkStream* stream, size_t length) = 0;
    145 
    146     // helper functions
    147     virtual void writeFunctionPtr(void* ptr);
    148     virtual void writePaint(const SkPaint& paint);
    149 
    150     virtual void writeBitmap(const SkBitmap& bitmap) = 0;
    151     virtual void writeTypeface(SkTypeface* typeface) = 0;
    152 
    153     virtual bool writeToStream(SkWStream*) = 0;
    154 
    155     enum Flags {
    156         kCrossProcess_Flag               = 0x01,
    157     };
    158 
    159     uint32_t getFlags() const { return fFlags; }
    160     void setFlags(uint32_t flags) { fFlags = flags; }
    161 
    162     bool isCrossProcess() const {
    163         return SkToBool(fFlags & kCrossProcess_Flag);
    164     }
    165 
    166     bool persistTypeface() const { return (fFlags & kCrossProcess_Flag) != 0; }
    167 
    168 protected:
    169     // A helper function so that each subclass does not have to be a friend of SkFlattenable
    170     void flattenObject(SkFlattenable* obj, SkFlattenableWriteBuffer& buffer);
    171 
    172     uint32_t fFlags;
    173 };
    174 
    175 #endif
    176