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