| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 |  | 
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 2 | /* | 
| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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. | 
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 7 |  */ | 
 | 8 |  | 
 | 9 |  | 
| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 |  | 
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 11 | #ifndef SkGPipePriv_DEFINED | 
 | 12 | #define SkGPipePriv_DEFINED | 
 | 13 |  | 
 | 14 | #include "SkTypes.h" | 
 | 15 |  | 
 | 16 | #define UNIMPLEMENTED | 
 | 17 |  | 
| reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 18 | // these must be contiguous, 0...N-1 | 
 | 19 | enum PaintFlats { | 
 | 20 |     kColorFilter_PaintFlat, | 
| reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 21 |     kDrawLooper_PaintFlat, | 
| reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 22 |     kMaskFilter_PaintFlat, | 
 | 23 |     kPathEffect_PaintFlat, | 
 | 24 |     kRasterizer_PaintFlat, | 
 | 25 |     kShader_PaintFlat, | 
| scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 26 |     kImageFilter_PaintFlat, | 
| reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 27 |     kXfermode_PaintFlat, | 
 | 28 |  | 
 | 29 |     kLast_PaintFlat = kXfermode_PaintFlat | 
 | 30 | }; | 
 | 31 | #define kCount_PaintFlats   (kLast_PaintFlat + 1) | 
 | 32 |  | 
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 33 | enum DrawOps { | 
| reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 34 |     kSkip_DrawOp,   // skip an addition N bytes (N == data) | 
 | 35 |  | 
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 36 |     // these match Canvas apis | 
 | 37 |     kClipPath_DrawOp, | 
 | 38 |     kClipRegion_DrawOp, | 
 | 39 |     kClipRect_DrawOp, | 
 | 40 |     kConcat_DrawOp, | 
 | 41 |     kDrawBitmap_DrawOp, | 
 | 42 |     kDrawBitmapMatrix_DrawOp, | 
| scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 43 |     kDrawBitmapNine_DrawOp, | 
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 44 |     kDrawBitmapRect_DrawOp, | 
 | 45 |     kDrawClear_DrawOp, | 
 | 46 |     kDrawData_DrawOp, | 
 | 47 |     kDrawPaint_DrawOp, | 
 | 48 |     kDrawPath_DrawOp, | 
 | 49 |     kDrawPicture_DrawOp, | 
 | 50 |     kDrawPoints_DrawOp, | 
 | 51 |     kDrawPosText_DrawOp, | 
 | 52 |     kDrawPosTextH_DrawOp, | 
 | 53 |     kDrawRect_DrawOp, | 
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 54 |     kDrawSprite_DrawOp, | 
 | 55 |     kDrawText_DrawOp, | 
 | 56 |     kDrawTextOnPath_DrawOp, | 
 | 57 |     kDrawVertices_DrawOp, | 
 | 58 |     kRestore_DrawOp, | 
 | 59 |     kRotate_DrawOp, | 
 | 60 |     kSave_DrawOp, | 
 | 61 |     kSaveLayer_DrawOp, | 
 | 62 |     kScale_DrawOp, | 
 | 63 |     kSetMatrix_DrawOp, | 
 | 64 |     kSkew_DrawOp, | 
 | 65 |     kTranslate_DrawOp, | 
 | 66 |  | 
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 67 |     kPaintOp_DrawOp, | 
| reed@google.com | bb6793b | 2011-05-05 15:18:15 +0000 | [diff] [blame] | 68 |  | 
| reed@google.com | bb6793b | 2011-05-05 15:18:15 +0000 | [diff] [blame] | 69 |     kDef_Typeface_DrawOp, | 
| reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 70 |     kDef_Flattenable_DrawOp, | 
| scroggo@google.com | 16d1d0b | 2012-05-02 19:09:40 +0000 | [diff] [blame] | 71 |     kDef_Bitmap_DrawOp, | 
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 72 |  | 
 | 73 |     // these are signals to playback, not drawing verbs | 
| scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 74 |     kReportFlags_DrawOp, | 
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 75 |     kDone_DrawOp, | 
 | 76 | }; | 
 | 77 |  | 
 | 78 | /** | 
 | 79 |  *  DrawOp packs into a 32bit int as follows | 
 | 80 |  * | 
 | 81 |  *  DrawOp:8 - Flags:4 - Data:20 | 
 | 82 |  * | 
 | 83 |  *  Flags and Data are called out separately, so we can reuse Data between | 
 | 84 |  *  different Ops that might have different Flags. e.g. Data might be a Paint | 
 | 85 |  *  index for both drawRect (no flags) and saveLayer (does have flags). | 
 | 86 |  * | 
 | 87 |  *  All Ops that take a SkPaint use their Data field to store the index to | 
 | 88 |  *  the paint (previously defined with kPaintOp_DrawOp). | 
 | 89 |  */ | 
 | 90 |  | 
 | 91 | #define DRAWOPS_OP_BITS     8 | 
 | 92 | #define DRAWOPS_FLAG_BITS   4 | 
 | 93 | #define DRAWOPS_DATA_BITS   20 | 
 | 94 |  | 
 | 95 | #define DRAWOPS_OP_MASK     ((1 << DRAWOPS_OP_BITS) - 1) | 
 | 96 | #define DRAWOPS_FLAG_MASK   ((1 << DRAWOPS_FLAG_BITS) - 1) | 
 | 97 | #define DRAWOPS_DATA_MASK   ((1 << DRAWOPS_DATA_BITS) - 1) | 
 | 98 |  | 
| caryclark@google.com | 920901d | 2012-06-06 12:04:11 +0000 | [diff] [blame] | 99 | static inline unsigned DrawOp_unpackOp(uint32_t op32) { | 
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 100 |     return (op32 >> (DRAWOPS_FLAG_BITS + DRAWOPS_DATA_BITS)); | 
 | 101 | } | 
 | 102 |  | 
| caryclark@google.com | 920901d | 2012-06-06 12:04:11 +0000 | [diff] [blame] | 103 | static inline unsigned DrawOp_unpackFlags(uint32_t op32) { | 
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 104 |     return (op32 >> DRAWOPS_DATA_BITS) & DRAWOPS_FLAG_MASK; | 
 | 105 | } | 
 | 106 |  | 
| caryclark@google.com | 920901d | 2012-06-06 12:04:11 +0000 | [diff] [blame] | 107 | static inline unsigned DrawOp_unpackData(uint32_t op32) { | 
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 108 |     return op32 & DRAWOPS_DATA_MASK; | 
 | 109 | } | 
 | 110 |  | 
| caryclark@google.com | 920901d | 2012-06-06 12:04:11 +0000 | [diff] [blame] | 111 | static inline uint32_t DrawOp_packOpFlagData(DrawOps op, unsigned flags, unsigned data) { | 
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 112 |     SkASSERT(0 == (op & ~DRAWOPS_OP_MASK)); | 
 | 113 |     SkASSERT(0 == (flags & ~DRAWOPS_FLAG_MASK)); | 
 | 114 |     SkASSERT(0 == (data & ~DRAWOPS_DATA_MASK)); | 
 | 115 |  | 
| reed@google.com | 67908f2 | 2011-06-27 14:47:50 +0000 | [diff] [blame] | 116 |     return (op << (DRAWOPS_FLAG_BITS + DRAWOPS_DATA_BITS)) | | 
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 117 |            (flags << DRAWOPS_DATA_BITS) | | 
 | 118 |             data; | 
 | 119 | } | 
 | 120 |  | 
 | 121 | /** DrawOp specific flag bits | 
 | 122 |  */ | 
 | 123 |  | 
 | 124 | enum { | 
 | 125 |     kSaveLayer_HasBounds_DrawOpFlag = 1 << 0, | 
 | 126 |     kSaveLayer_HasPaint_DrawOpFlag = 1 << 1, | 
 | 127 | }; | 
 | 128 | enum { | 
 | 129 |     kClear_HasColor_DrawOpFlag  = 1 << 0 | 
 | 130 | }; | 
 | 131 | enum { | 
 | 132 |     kDrawTextOnPath_HasMatrix_DrawOpFlag = 1 << 0 | 
 | 133 | }; | 
 | 134 | enum { | 
 | 135 |     kDrawVertices_HasTexs_DrawOpFlag     = 1 << 0, | 
 | 136 |     kDrawVertices_HasColors_DrawOpFlag   = 1 << 1, | 
 | 137 |     kDrawVertices_HasIndices_DrawOpFlag  = 1 << 2, | 
 | 138 | }; | 
 | 139 |  | 
 | 140 | /////////////////////////////////////////////////////////////////////////////// | 
 | 141 |  | 
| scroggo@google.com | 284bf50 | 2012-07-17 16:10:34 +0000 | [diff] [blame^] | 142 | class BitmapInfo : SkNoncopyable { | 
 | 143 | public: | 
 | 144 |     BitmapInfo(SkBitmap* bitmap, uint32_t genID, int toBeDrawnCount) | 
 | 145 |         : fBitmap(bitmap) | 
 | 146 |         , fGenID(genID) | 
 | 147 |         , fMoreRecentlyUsed(NULL) | 
 | 148 |         , fLessRecentlyUsed(NULL) | 
 | 149 |         , fToBeDrawnCount(toBeDrawnCount) | 
 | 150 |     {} | 
 | 151 |  | 
 | 152 |     ~BitmapInfo() { | 
 | 153 |         SkASSERT(0 == fToBeDrawnCount); | 
 | 154 |         SkDELETE(fBitmap); | 
 | 155 |     } | 
 | 156 |  | 
 | 157 |     void addDraws(int drawsToAdd) { | 
 | 158 |         if (0 == fToBeDrawnCount) { | 
 | 159 |             // The readers will only ever decrement the count, so once the | 
 | 160 |             // count is zero, the writer will be the only one modifying it, | 
 | 161 |             // so it does not need to be an atomic operation. | 
 | 162 |             fToBeDrawnCount = drawsToAdd; | 
 | 163 |         } else { | 
 | 164 |             sk_atomic_add(&fToBeDrawnCount, drawsToAdd); | 
 | 165 |         } | 
 | 166 |     } | 
 | 167 |  | 
 | 168 |     void decDraws() { | 
 | 169 |         sk_atomic_dec(&fToBeDrawnCount); | 
 | 170 |     } | 
 | 171 |  | 
 | 172 |     int drawCount() const { | 
 | 173 |         return fToBeDrawnCount; | 
 | 174 |     } | 
 | 175 |  | 
 | 176 |     SkBitmap* fBitmap; | 
 | 177 |     // Store the generation ID of the original bitmap, since copying does | 
 | 178 |     // not copy this field, so fBitmap's generation ID will not be useful | 
 | 179 |     // for comparing. | 
 | 180 |     uint32_t fGenID; | 
 | 181 |     // TODO: Generalize the LRU caching mechanism | 
 | 182 |     BitmapInfo* fMoreRecentlyUsed; | 
 | 183 |     BitmapInfo* fLessRecentlyUsed; | 
 | 184 | private: | 
 | 185 |     int      fToBeDrawnCount; | 
 | 186 | }; | 
 | 187 |  | 
| scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 188 | static inline bool shouldFlattenBitmaps(uint32_t flags) { | 
 | 189 |     return flags & SkGPipeWriter::kCrossProcess_Flag | 
 | 190 |             && !(flags & SkGPipeWriter::kSharedAddressSpace_SkGPipeFlag); | 
 | 191 | } | 
 | 192 |  | 
 | 193 | /////////////////////////////////////////////////////////////////////////////// | 
 | 194 |  | 
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 195 | enum PaintOps { | 
 | 196 |     kReset_PaintOp,     // no arg | 
 | 197 |      | 
 | 198 |     kFlags_PaintOp,     // arg inline | 
 | 199 |     kColor_PaintOp,     // arg 32 | 
 | 200 |     kStyle_PaintOp,     // arg inline | 
 | 201 |     kJoin_PaintOp,      // arg inline | 
 | 202 |     kCap_PaintOp,       // arg inline | 
 | 203 |     kWidth_PaintOp,     // arg scalar | 
 | 204 |     kMiter_PaintOp,// arg scalar | 
 | 205 |      | 
 | 206 |     kEncoding_PaintOp,  // arg inline - text | 
 | 207 |     kHinting_PaintOp,   // arg inline - text | 
 | 208 |     kAlign_PaintOp,     // arg inline - text | 
 | 209 |     kTextSize_PaintOp,  // arg scalar - text | 
 | 210 |     kTextScaleX_PaintOp,// arg scalar - text | 
 | 211 |     kTextSkewX_PaintOp, // arg scalar - text | 
| reed@google.com | f5842f7 | 2011-05-04 18:30:04 +0000 | [diff] [blame] | 212 |     kTypeface_PaintOp,  // arg inline (index) - text | 
 | 213 |  | 
| reed@google.com | b55d118 | 2011-05-11 00:42:04 +0000 | [diff] [blame] | 214 |     kFlatIndex_PaintOp, // flags=paintflat, data=index | 
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 215 | }; | 
 | 216 |  | 
 | 217 | #define PAINTOPS_OP_BITS     8 | 
 | 218 | #define PAINTOPS_FLAG_BITS   4 | 
 | 219 | #define PAINTOPS_DATA_BITS   20 | 
 | 220 |  | 
 | 221 | #define PAINTOPS_OP_MASK     ((1 << PAINTOPS_OP_BITS) - 1) | 
 | 222 | #define PAINTOPS_FLAG_MASK   ((1 << PAINTOPS_FLAG_BITS) - 1) | 
 | 223 | #define PAINTOPS_DATA_MASK   ((1 << PAINTOPS_DATA_BITS) - 1) | 
 | 224 |  | 
| caryclark@google.com | 920901d | 2012-06-06 12:04:11 +0000 | [diff] [blame] | 225 | static inline unsigned PaintOp_unpackOp(uint32_t op32) { | 
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 226 |     return (op32 >> (PAINTOPS_FLAG_BITS + PAINTOPS_DATA_BITS)); | 
 | 227 | } | 
 | 228 |  | 
| caryclark@google.com | 920901d | 2012-06-06 12:04:11 +0000 | [diff] [blame] | 229 | static inline unsigned PaintOp_unpackFlags(uint32_t op32) { | 
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 230 |     return (op32 >> PAINTOPS_DATA_BITS) & PAINTOPS_FLAG_MASK; | 
 | 231 | } | 
 | 232 |  | 
| caryclark@google.com | 920901d | 2012-06-06 12:04:11 +0000 | [diff] [blame] | 233 | static inline unsigned PaintOp_unpackData(uint32_t op32) { | 
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 234 |     return op32 & PAINTOPS_DATA_MASK; | 
 | 235 | } | 
 | 236 |  | 
| caryclark@google.com | 920901d | 2012-06-06 12:04:11 +0000 | [diff] [blame] | 237 | static inline uint32_t PaintOp_packOp(PaintOps op) { | 
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 238 |     SkASSERT(0 == (op & ~PAINTOPS_OP_MASK)); | 
 | 239 |      | 
| reed@google.com | 67908f2 | 2011-06-27 14:47:50 +0000 | [diff] [blame] | 240 |     return op << (PAINTOPS_FLAG_BITS + PAINTOPS_DATA_BITS); | 
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 241 | } | 
 | 242 |  | 
| caryclark@google.com | 920901d | 2012-06-06 12:04:11 +0000 | [diff] [blame] | 243 | static inline uint32_t PaintOp_packOpData(PaintOps op, unsigned data) { | 
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 244 |     SkASSERT(0 == (op & ~PAINTOPS_OP_MASK)); | 
 | 245 |     SkASSERT(0 == (data & ~PAINTOPS_DATA_MASK)); | 
 | 246 |      | 
| reed@google.com | 67908f2 | 2011-06-27 14:47:50 +0000 | [diff] [blame] | 247 |     return (op << (PAINTOPS_FLAG_BITS + PAINTOPS_DATA_BITS)) | data; | 
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 248 | } | 
 | 249 |  | 
| caryclark@google.com | 920901d | 2012-06-06 12:04:11 +0000 | [diff] [blame] | 250 | static inline uint32_t PaintOp_packOpFlagData(PaintOps op, unsigned flags, unsigned data) { | 
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 251 |     SkASSERT(0 == (op & ~PAINTOPS_OP_MASK)); | 
 | 252 |     SkASSERT(0 == (flags & ~PAINTOPS_FLAG_MASK)); | 
 | 253 |     SkASSERT(0 == (data & ~PAINTOPS_DATA_MASK)); | 
 | 254 |      | 
| reed@google.com | 67908f2 | 2011-06-27 14:47:50 +0000 | [diff] [blame] | 255 |     return (op << (PAINTOPS_FLAG_BITS + PAINTOPS_DATA_BITS)) | | 
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 256 |     (flags << PAINTOPS_DATA_BITS) | | 
 | 257 |     data; | 
 | 258 | } | 
 | 259 |  | 
| reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 260 | #endif |