Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright 2011 Google Inc.
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 #ifndef SkPictureRecord_DEFINED
      9 #define SkPictureRecord_DEFINED
     10 
     11 #include "SkCanvas.h"
     12 #include "SkFlattenable.h"
     13 #include "SkPicture.h"
     14 #include "SkPictureData.h"
     15 #include "SkTArray.h"
     16 #include "SkTDArray.h"
     17 #include "SkTHash.h"
     18 #include "SkWriter32.h"
     19 
     20 // These macros help with packing and unpacking a single byte value and
     21 // a 3 byte value into/out of a uint32_t
     22 #define MASK_24 0x00FFFFFF
     23 #define UNPACK_8_24(combined, small, large)             \
     24     small = (combined >> 24) & 0xFF;                    \
     25     large = combined & MASK_24;
     26 #define PACK_8_24(small, large) ((small << 24) | large)
     27 
     28 
     29 class SkPictureRecord : public SkCanvas {
     30 public:
     31     SkPictureRecord(const SkISize& dimensions, uint32_t recordFlags);
     32     virtual ~SkPictureRecord();
     33 
     34     const SkTDArray<const SkPicture* >& getPictureRefs() const {
     35         return fPictureRefs;
     36     }
     37 
     38     const SkTDArray<const SkTextBlob* >& getTextBlobRefs() const {
     39         return fTextBlobRefs;
     40     }
     41 
     42     const SkTDArray<const SkImage* >& getImageRefs() const {
     43         return fImageRefs;
     44     }
     45 
     46     SkData* opData(bool deepCopy) const {
     47         this->validate(fWriter.bytesWritten(), 0);
     48 
     49         if (fWriter.bytesWritten() == 0) {
     50             return SkData::NewEmpty();
     51         }
     52 
     53         if (deepCopy) {
     54             return SkData::NewWithCopy(fWriter.contiguousArray(), fWriter.bytesWritten());
     55         }
     56 
     57         return fWriter.snapshotAsData();
     58     }
     59 
     60     const SkPictureContentInfo& contentInfo() const {
     61         return fContentInfo;
     62     }
     63 
     64     void setFlags(uint32_t recordFlags) {
     65         fRecordFlags = recordFlags;
     66     }
     67 
     68     const SkWriter32& writeStream() const {
     69         return fWriter;
     70     }
     71 
     72     void beginRecording();
     73     void endRecording();
     74 
     75 protected:
     76     void addNoOp();
     77 
     78 private:
     79     void handleOptimization(int opt);
     80     size_t recordRestoreOffsetPlaceholder(SkRegion::Op);
     81     void fillRestoreOffsetPlaceholdersForCurrentStackLevel(uint32_t restoreOffset);
     82 
     83     SkTDArray<int32_t> fRestoreOffsetStack;
     84 
     85     SkTDArray<uint32_t> fCullOffsetStack;
     86 
     87     /*
     88      * Write the 'drawType' operation and chunk size to the skp. 'size'
     89      * can potentially be increased if the chunk size needs its own storage
     90      * location (i.e., it overflows 24 bits).
     91      * Returns the start offset of the chunk. This is the location at which
     92      * the opcode & size are stored.
     93      * TODO: since we are handing the size into here we could call reserve
     94      * and then return a pointer to the memory storage. This could decrease
     95      * allocation overhead but could lead to more wasted space (the tail
     96      * end of blocks could go unused). Possibly add a second addDraw that
     97      * operates in this manner.
     98      */
     99     size_t addDraw(DrawType drawType, size_t* size) {
    100         size_t offset = fWriter.bytesWritten();
    101 
    102         this->predrawNotify();
    103         fContentInfo.addOperation();
    104 
    105         SkASSERT(0 != *size);
    106         SkASSERT(((uint8_t) drawType) == drawType);
    107 
    108         if (0 != (*size & ~MASK_24) || *size == MASK_24) {
    109             fWriter.writeInt(PACK_8_24(drawType, MASK_24));
    110             *size += 1;
    111             fWriter.writeInt(SkToU32(*size));
    112         } else {
    113             fWriter.writeInt(PACK_8_24(drawType, SkToU32(*size)));
    114         }
    115 
    116         return offset;
    117     }
    118 
    119     void addInt(int value) {
    120         fWriter.writeInt(value);
    121     }
    122     void addScalar(SkScalar scalar) {
    123         fWriter.writeScalar(scalar);
    124     }
    125 
    126     void addBitmap(const SkBitmap& bitmap);
    127     void addImage(const SkImage*);
    128     void addMatrix(const SkMatrix& matrix);
    129     void addPaint(const SkPaint& paint) { this->addPaintPtr(&paint); }
    130     void addPaintPtr(const SkPaint* paint);
    131     void addPatch(const SkPoint cubics[12]);
    132     void addPath(const SkPath& path);
    133     void addPicture(const SkPicture* picture);
    134     void addPoint(const SkPoint& point);
    135     void addPoints(const SkPoint pts[], int count);
    136     void addRect(const SkRect& rect);
    137     void addRectPtr(const SkRect* rect);
    138     void addIRect(const SkIRect& rect);
    139     void addIRectPtr(const SkIRect* rect);
    140     void addRRect(const SkRRect&);
    141     void addRegion(const SkRegion& region);
    142     void addText(const void* text, size_t byteLength);
    143     void addTextBlob(const SkTextBlob* blob);
    144 
    145     int find(const SkBitmap& bitmap);
    146 
    147 protected:
    148     void validate(size_t initialOffset, size_t size) const {
    149         SkASSERT(fWriter.bytesWritten() == initialOffset + size);
    150     }
    151 
    152     SkSurface* onNewSurface(const SkImageInfo&, const SkSurfaceProps&) override;
    153     bool onPeekPixels(SkPixmap*) override { return false; }
    154 
    155     void willSave() override;
    156     SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec&) override;
    157     void willRestore() override;
    158 
    159     void didConcat(const SkMatrix&) override;
    160     void didSetMatrix(const SkMatrix&) override;
    161 
    162     void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
    163 
    164     void onDrawText(const void* text, size_t, SkScalar x, SkScalar y, const SkPaint&) override;
    165     void onDrawPosText(const void* text, size_t, const SkPoint pos[], const SkPaint&) override;
    166     void onDrawPosTextH(const void* text, size_t, const SkScalar xpos[], SkScalar constY,
    167                         const SkPaint&) override;
    168     void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
    169                                   const SkMatrix* matrix, const SkPaint&) override;
    170     void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
    171                                 const SkPaint& paint) override;
    172 
    173     void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
    174                              const SkPoint texCoords[4], SkXfermode* xmode,
    175                              const SkPaint& paint) override;
    176     void onDrawAtlas(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[], int,
    177                      SkXfermode::Mode, const SkRect*, const SkPaint*) override;
    178 
    179     void onDrawPaint(const SkPaint&) override;
    180     void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
    181     void onDrawRect(const SkRect&, const SkPaint&) override;
    182     void onDrawOval(const SkRect&, const SkPaint&) override;
    183     void onDrawRRect(const SkRRect&, const SkPaint&) override;
    184     void onDrawPath(const SkPath&, const SkPaint&) override;
    185     void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override;
    186     void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*,
    187                           SrcRectConstraint) override;
    188     void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override;
    189     void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
    190                          const SkPaint*, SrcRectConstraint) override;
    191     void onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst,
    192                          const SkPaint*) override;
    193     void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
    194                           const SkPaint*) override;
    195     void onDrawVertices(VertexMode vmode, int vertexCount,
    196                         const SkPoint vertices[], const SkPoint texs[],
    197                         const SkColor colors[], SkXfermode* xmode,
    198                         const uint16_t indices[], int indexCount,
    199                         const SkPaint&) override;
    200 
    201     void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) override;
    202     void onClipRRect(const SkRRect&, SkRegion::Op, ClipEdgeStyle) override;
    203     void onClipPath(const SkPath&, SkRegion::Op, ClipEdgeStyle) override;
    204     void onClipRegion(const SkRegion&, SkRegion::Op) override;
    205 
    206     void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
    207 
    208     int addPathToHeap(const SkPath& path);  // does not write to ops stream
    209 
    210     // These entry points allow the writing of matrices, clips, saves &
    211     // restores to be deferred (e.g., if the MC state is being collapsed and
    212     // only written out as needed).
    213     void recordConcat(const SkMatrix& matrix);
    214     void recordTranslate(const SkMatrix& matrix);
    215     void recordScale(const SkMatrix& matrix);
    216     size_t recordClipRect(const SkRect& rect, SkRegion::Op op, bool doAA);
    217     size_t recordClipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA);
    218     size_t recordClipPath(int pathID, SkRegion::Op op, bool doAA);
    219     size_t recordClipRegion(const SkRegion& region, SkRegion::Op op);
    220     void recordSave();
    221     void recordSaveLayer(const SaveLayerRec&);
    222     void recordRestore(bool fillInSkips = true);
    223 
    224 private:
    225     SkPictureContentInfo fContentInfo;
    226 
    227     SkTArray<SkBitmap> fBitmaps;
    228     SkTArray<SkPaint>  fPaints;
    229 
    230     struct PathHash {
    231         uint32_t operator()(const SkPath& p) { return p.getGenerationID(); }
    232     };
    233     SkTHashMap<SkPath, int, PathHash> fPaths;
    234 
    235     SkWriter32 fWriter;
    236 
    237     // we ref each item in these arrays
    238     SkTDArray<const SkImage*>    fImageRefs;
    239     SkTDArray<const SkPicture*>  fPictureRefs;
    240     SkTDArray<const SkTextBlob*> fTextBlobRefs;
    241 
    242     uint32_t fRecordFlags;
    243     int      fInitialSaveCount;
    244 
    245     friend class SkPictureData;   // for SkPictureData's SkPictureRecord-based constructor
    246 
    247     typedef SkCanvas INHERITED;
    248 };
    249 
    250 #endif
    251