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 #ifndef SkPicturePlayback_DEFINED
      9 #define SkPicturePlayback_DEFINED
     10 
     11 #include "SkPicture.h"
     12 #include "SkReader32.h"
     13 
     14 #include "SkBitmap.h"
     15 #include "SkMatrix.h"
     16 #include "SkPaint.h"
     17 #include "SkPath.h"
     18 #include "SkPathHeap.h"
     19 #include "SkRegion.h"
     20 #include "SkPictureFlat.h"
     21 
     22 #ifdef SK_BUILD_FOR_ANDROID
     23 #include "SkThread.h"
     24 #endif
     25 
     26 class SkPictureRecord;
     27 class SkStream;
     28 class SkWStream;
     29 
     30 class SkPicturePlayback {
     31 public:
     32     SkPicturePlayback();
     33     SkPicturePlayback(const SkPicturePlayback& src);
     34     explicit SkPicturePlayback(const SkPictureRecord& record);
     35     explicit SkPicturePlayback(SkStream*, uint32_t pictureVersion = PICTURE_VERSION_JB);
     36 
     37     virtual ~SkPicturePlayback();
     38 
     39     void draw(SkCanvas& canvas);
     40 
     41     void serialize(SkWStream*) const;
     42 
     43     void dumpSize() const;
     44 
     45     // Can be called in the middle of playback (the draw() call). WIll abort the
     46     // drawing and return from draw() after the "current" op code is done
     47     void abort();
     48 
     49 private:
     50 
     51     class TextContainer {
     52     public:
     53         size_t length() { return fByteLength; }
     54         const void* text() { return (const void*) fText; }
     55         size_t fByteLength;
     56         const char* fText;
     57     };
     58 
     59     const SkBitmap& getBitmap() {
     60         int index = getInt();
     61         SkASSERT(index > 0);
     62         return fBitmaps[index - 1];
     63     }
     64 
     65     int getIndex() { return fReader.readInt(); }
     66     int getInt() { return fReader.readInt(); }
     67 
     68     const SkMatrix* getMatrix() {
     69         int index = getInt();
     70         if (index == 0) {
     71             return NULL;
     72         }
     73         SkASSERT(index > 0 && index <= fMatrixCount);
     74         return &fMatrices[index - 1];
     75     }
     76 
     77     const SkPath& getPath() {
     78         return (*fPathHeap)[getInt() - 1];
     79     }
     80 
     81     SkPicture& getPicture() {
     82         int index = getInt();
     83         SkASSERT(index > 0 && index <= fPictureCount);
     84         return *fPictureRefs[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.skipT<SkRect>();
     99         } else {
    100             return NULL;
    101         }
    102     }
    103 
    104     const SkIRect* getIRectPtr() {
    105         if (fReader.readBool()) {
    106             return &fReader.skipT<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 
    174     SkRefCntPlayback fRCPlayback;
    175     SkTypefacePlayback fTFPlayback;
    176     SkFactoryPlayback*   fFactoryPlayback;
    177 #ifdef SK_BUILD_FOR_ANDROID
    178     SkMutex fDrawMutex;
    179 #endif
    180 };
    181 
    182 #endif
    183