Home | History | Annotate | Download | only in core
      1 #ifndef SkPicturePlayback_DEFINED
      2 #define SkPicturePlayback_DEFINED
      3 
      4 #include "SkPicture.h"
      5 #include "SkReader32.h"
      6 
      7 #include "SkBitmap.h"
      8 #include "SkMatrix.h"
      9 #include "SkPaint.h"
     10 #include "SkPath.h"
     11 #include "SkPathHeap.h"
     12 #include "SkRegion.h"
     13 #include "SkPictureFlat.h"
     14 #include "SkShape.h"
     15 
     16 #ifdef ANDROID
     17 #include "SkThread.h"
     18 #endif
     19 
     20 class SkPictureRecord;
     21 class SkStream;
     22 class SkWStream;
     23 
     24 class SkPicturePlayback {
     25 public:
     26     SkPicturePlayback();
     27     SkPicturePlayback(const SkPicturePlayback& src);
     28     explicit SkPicturePlayback(const SkPictureRecord& record);
     29     explicit SkPicturePlayback(SkStream*);
     30 
     31     virtual ~SkPicturePlayback();
     32 
     33     void draw(SkCanvas& canvas);
     34 
     35     void serialize(SkWStream*) const;
     36 
     37     void dumpSize() const;
     38 
     39     // Can be called in the middle of playback (the draw() call). WIll abort the
     40     // drawing and return from draw() after the "current" op code is done
     41     void abort();
     42 
     43 private:
     44 
     45     class TextContainer {
     46     public:
     47         size_t length() { return fByteLength; }
     48         const void* text() { return (const void*) fText; }
     49         size_t fByteLength;
     50         const char* fText;
     51     };
     52 
     53     const SkBitmap& getBitmap() {
     54         int index = getInt();
     55         SkASSERT(index > 0);
     56         return fBitmaps[index - 1];
     57     }
     58 
     59     int getIndex() { return fReader.readInt(); }
     60     int getInt() { return fReader.readInt(); }
     61 
     62     const SkMatrix* getMatrix() {
     63         int index = getInt();
     64         if (index == 0) {
     65             return NULL;
     66         }
     67         SkASSERT(index > 0 && index <= fMatrixCount);
     68         return &fMatrices[index - 1];
     69     }
     70 
     71     const SkPath& getPath() {
     72         return (*fPathHeap)[getInt() - 1];
     73     }
     74 
     75     SkPicture& getPicture() {
     76         int index = getInt();
     77         SkASSERT(index > 0 && index <= fPictureCount);
     78         return *fPictureRefs[index - 1];
     79     }
     80 
     81     SkShape* getShape() {
     82         int index = getInt();
     83         SkASSERT(index > 0 && index <= fShapeCount);
     84         return fShapes[index - 1];
     85     }
     86 
     87     const SkPaint* getPaint() {
     88         int index = getInt();
     89         if (index == 0) {
     90             return NULL;
     91         }
     92         SkASSERT(index > 0 && index <= fPaintCount);
     93         return &fPaints[index - 1];
     94     }
     95 
     96     const SkRect* getRectPtr() {
     97         if (fReader.readBool()) {
     98             return fReader.skipRect();
     99         } else {
    100             return NULL;
    101         }
    102     }
    103 
    104     const SkIRect* getIRectPtr() {
    105         if (fReader.readBool()) {
    106             return (const SkIRect*)fReader.skip(sizeof(SkIRect));
    107         } else {
    108             return NULL;
    109         }
    110     }
    111 
    112     const SkRegion& getRegion() {
    113         int index = getInt();
    114         SkASSERT(index > 0);
    115         return fRegions[index - 1];
    116     }
    117 
    118     SkScalar getScalar() { return fReader.readScalar(); }
    119 
    120     void getText(TextContainer* text) {
    121         size_t length = text->fByteLength = getInt();
    122         text->fText = (const char*)fReader.skip(length);
    123     }
    124 
    125     void init();
    126 
    127 #ifdef SK_DEBUG_SIZE
    128 public:
    129     int size(size_t* sizePtr);
    130     int bitmaps(size_t* size);
    131     int paints(size_t* size);
    132     int paths(size_t* size);
    133     int regions(size_t* size);
    134 #endif
    135 
    136 #ifdef SK_DEBUG_DUMP
    137 private:
    138     void dumpBitmap(const SkBitmap& bitmap) const;
    139     void dumpMatrix(const SkMatrix& matrix) const;
    140     void dumpPaint(const SkPaint& paint) const;
    141     void dumpPath(const SkPath& path) const;
    142     void dumpPicture(const SkPicture& picture) const;
    143     void dumpRegion(const SkRegion& region) const;
    144     int dumpDrawType(char* bufferPtr, char* buffer, DrawType drawType);
    145     int dumpInt(char* bufferPtr, char* buffer, char* name);
    146     int dumpRect(char* bufferPtr, char* buffer, char* name);
    147     int dumpPoint(char* bufferPtr, char* buffer, char* name);
    148     void dumpPointArray(char** bufferPtrPtr, char* buffer, int count);
    149     int dumpPtr(char* bufferPtr, char* buffer, char* name, void* ptr);
    150     int dumpRectPtr(char* bufferPtr, char* buffer, char* name);
    151     int dumpScalar(char* bufferPtr, char* buffer, char* name);
    152     void dumpText(char** bufferPtrPtr, char* buffer);
    153     void dumpStream();
    154 
    155 public:
    156     void dump() const;
    157 #endif
    158 
    159 private:
    160     SkPathHeap* fPathHeap;  // reference counted
    161     SkBitmap* fBitmaps;
    162     int fBitmapCount;
    163     SkMatrix* fMatrices;
    164     int fMatrixCount;
    165     SkPaint* fPaints;
    166     int fPaintCount;
    167     SkRegion* fRegions;
    168     int fRegionCount;
    169     mutable SkFlattenableReadBuffer fReader;
    170 
    171     SkPicture** fPictureRefs;
    172     int fPictureCount;
    173     SkShape** fShapes;
    174     int fShapeCount;
    175 
    176     SkRefCntPlayback fRCPlayback;
    177     SkTypefacePlayback fTFPlayback;
    178     SkFactoryPlayback*   fFactoryPlayback;
    179 #ifdef ANDROID
    180     SkMutex fDrawMutex;
    181 #endif
    182 };
    183 
    184 #endif
    185