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 SkPictureFlat_DEFINED
      9 #define SkPictureFlat_DEFINED
     10 
     11 #include "SkChunkAlloc.h"
     12 #include "SkBitmap.h"
     13 #include "SkPicture.h"
     14 #include "SkMatrix.h"
     15 #include "SkPaint.h"
     16 #include "SkPath.h"
     17 #include "SkRegion.h"
     18 
     19 enum DrawType {
     20     UNUSED,
     21     CLIP_PATH,
     22     CLIP_REGION,
     23     CLIP_RECT,
     24     CONCAT,
     25     DRAW_BITMAP,
     26     DRAW_BITMAP_MATRIX,
     27     DRAW_BITMAP_RECT,
     28     DRAW_CLEAR,
     29     DRAW_DATA,
     30     DRAW_PAINT,
     31     DRAW_PATH,
     32     DRAW_PICTURE,
     33     DRAW_POINTS,
     34     DRAW_POS_TEXT,
     35     DRAW_POS_TEXT_TOP_BOTTOM, // fast variant of DRAW_POS_TEXT
     36     DRAW_POS_TEXT_H,
     37     DRAW_POS_TEXT_H_TOP_BOTTOM, // fast variant of DRAW_POS_TEXT_H
     38     DRAW_RECT,
     39     DRAW_SHAPE,
     40     DRAW_SPRITE,
     41     DRAW_TEXT,
     42     DRAW_TEXT_ON_PATH,
     43     DRAW_TEXT_TOP_BOTTOM,   // fast variant of DRAW_TEXT
     44     DRAW_VERTICES,
     45     RESTORE,
     46     ROTATE,
     47     SAVE,
     48     SAVE_LAYER,
     49     SCALE,
     50     SET_MATRIX,
     51     SKEW,
     52     TRANSLATE,
     53     DRAW_BITMAP_NINE
     54 };
     55 
     56 enum DrawVertexFlags {
     57     DRAW_VERTICES_HAS_TEXS    = 0x01,
     58     DRAW_VERTICES_HAS_COLORS  = 0x02,
     59     DRAW_VERTICES_HAS_INDICES = 0x04
     60 };
     61 
     62 ///////////////////////////////////////////////////////////////////////////////
     63 // clipparams are packed in 5 bits
     64 //  doAA:1 | regionOp:4
     65 
     66 static inline uint32_t ClipParams_pack(SkRegion::Op op, bool doAA) {
     67     unsigned doAABit = doAA ? 1 : 0;
     68     return (doAABit << 4) | op;
     69 }
     70 
     71 static inline SkRegion::Op ClipParams_unpackRegionOp(uint32_t packed) {
     72     return (SkRegion::Op)(packed & 0xF);
     73 }
     74 
     75 static inline bool ClipParams_unpackDoAA(uint32_t packed) {
     76     return SkToBool((packed >> 4) & 1);
     77 }
     78 
     79 ///////////////////////////////////////////////////////////////////////////////
     80 
     81 class SkRefCntPlayback {
     82 public:
     83     SkRefCntPlayback();
     84     virtual ~SkRefCntPlayback();
     85 
     86     int count() const { return fCount; }
     87 
     88     void reset(const SkRefCntSet*);
     89 
     90     void setCount(int count);
     91     SkRefCnt* set(int index, SkRefCnt*);
     92 
     93     virtual void setupBuffer(SkFlattenableReadBuffer& buffer) const {
     94         buffer.setRefCntArray(fArray, fCount);
     95     }
     96 
     97 protected:
     98     int fCount;
     99     SkRefCnt** fArray;
    100 };
    101 
    102 class SkTypefacePlayback : public SkRefCntPlayback {
    103 public:
    104     virtual void setupBuffer(SkFlattenableReadBuffer& buffer) const {
    105         buffer.setTypefaceArray((SkTypeface**)fArray, fCount);
    106     }
    107 };
    108 
    109 class SkFactoryPlayback {
    110 public:
    111     SkFactoryPlayback(int count) : fCount(count) {
    112         fArray = SkNEW_ARRAY(SkFlattenable::Factory, count);
    113     }
    114 
    115     ~SkFactoryPlayback() {
    116         SkDELETE_ARRAY(fArray);
    117     }
    118 
    119     SkFlattenable::Factory* base() const { return fArray; }
    120 
    121     void setupBuffer(SkFlattenableReadBuffer& buffer) const {
    122         buffer.setFactoryPlayback(fArray, fCount);
    123     }
    124 
    125 private:
    126     int fCount;
    127     SkFlattenable::Factory* fArray;
    128 };
    129 
    130 class SkFlatData {
    131 public:
    132     static int Compare(const SkFlatData* a, const SkFlatData* b) {
    133         return memcmp(&a->fAllocSize, &b->fAllocSize, a->fAllocSize);
    134     }
    135 
    136     int index() const { return fIndex; }
    137 
    138 #ifdef SK_DEBUG_SIZE
    139     size_t size() const { return sizeof(fIndex) + fAllocSize; }
    140 #endif
    141 
    142 protected:
    143     static SkFlatData* Alloc(SkChunkAlloc* heap, int32_t size, int index);
    144 
    145     int fIndex;
    146     int32_t fAllocSize;
    147 };
    148 
    149 class SkFlatBitmap : public SkFlatData {
    150 public:
    151     static SkFlatBitmap* Flatten(SkChunkAlloc*, const SkBitmap&, int index,
    152                                  SkRefCntSet*);
    153 
    154     void unflatten(SkBitmap* bitmap, SkRefCntPlayback* rcp) const {
    155         SkFlattenableReadBuffer buffer(fBitmapData);
    156         if (rcp) {
    157             rcp->setupBuffer(buffer);
    158         }
    159         bitmap->unflatten(buffer);
    160     }
    161 
    162 #ifdef SK_DEBUG_VALIDATE
    163     void validate() const {
    164         // to be written
    165     }
    166 #endif
    167 
    168 private:
    169     char fBitmapData[1];
    170     typedef SkFlatData INHERITED;
    171 };
    172 
    173 class SkFlatMatrix : public SkFlatData {
    174 public:
    175     static SkFlatMatrix* Flatten(SkChunkAlloc* heap, const SkMatrix& matrix, int index);
    176 
    177     void unflatten(SkMatrix* result) const {
    178         result->unflatten(fMatrixData);
    179     }
    180 
    181 #ifdef SK_DEBUG_DUMP
    182     void dump() const;
    183 #endif
    184 
    185 #ifdef SK_DEBUG_VALIDATE
    186     void validate() const {
    187         // to be written
    188     }
    189 #endif
    190 
    191 private:
    192     char fMatrixData[1];
    193     typedef SkFlatData INHERITED;
    194 };
    195 
    196 class SkFlatPaint : public SkFlatData {
    197 public:
    198     static SkFlatPaint* Flatten(SkChunkAlloc* heap, const SkPaint& paint,
    199                                 int index, SkRefCntSet*,
    200                                 SkRefCntSet* faceRecorder);
    201 
    202     void unflatten(SkPaint* result, SkRefCntPlayback* rcp,
    203                    SkTypefacePlayback* facePlayback) const {
    204         Read(fPaintData, result, rcp, facePlayback);
    205     }
    206 
    207     static void Read(const void* storage, SkPaint* paint, SkRefCntPlayback*,
    208                      SkTypefacePlayback* facePlayback);
    209 
    210 #ifdef SK_DEBUG_DUMP
    211     void dump() const;
    212 #endif
    213 
    214 private:
    215     char fPaintData[1];
    216     typedef SkFlatData INHERITED;
    217 };
    218 
    219 class SkFlatRegion : public SkFlatData {
    220 public:
    221     static SkFlatRegion* Flatten(SkChunkAlloc* heap, const SkRegion& region, int index);
    222 
    223     void unflatten(SkRegion* result) const {
    224         result->unflatten(fRegionData);
    225     }
    226 
    227 #ifdef SK_DEBUG_VALIDATE
    228     void validate() const {
    229         // to be written
    230     }
    231 #endif
    232 
    233 private:
    234     char fRegionData[1];
    235     typedef SkFlatData INHERITED;
    236 };
    237 
    238 #endif
    239