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 kImageFilter_PaintFlat, 23 kMaskFilter_PaintFlat, 24 kPathEffect_PaintFlat, 25 kRasterizer_PaintFlat, 26 kShader_PaintFlat, 27 kXfermode_PaintFlat, 28 29 kLast_PaintFlat = kXfermode_PaintFlat 30 }; 31 #define kCount_PaintFlats (kLast_PaintFlat + 1) 32 33 enum DrawOps { 34 kSkip_DrawOp, // skip an addition N bytes (N == data) 35 36 // these match Canvas apis 37 kClipPath_DrawOp, 38 kClipRegion_DrawOp, 39 kClipRect_DrawOp, 40 kClipRRect_DrawOp, 41 kConcat_DrawOp, 42 kDrawBitmap_DrawOp, 43 kDrawBitmapMatrix_DrawOp, 44 kDrawBitmapNine_DrawOp, 45 kDrawBitmapRectToRect_DrawOp, 46 kDrawClear_DrawOp, 47 kDrawData_DrawOp, 48 kDrawOval_DrawOp, 49 kDrawPaint_DrawOp, 50 kDrawPath_DrawOp, 51 kDrawPicture_DrawOp, 52 kDrawPoints_DrawOp, 53 kDrawPosText_DrawOp, 54 kDrawPosTextH_DrawOp, 55 kDrawRect_DrawOp, 56 kDrawRRect_DrawOp, 57 kDrawSprite_DrawOp, 58 kDrawText_DrawOp, 59 kDrawTextOnPath_DrawOp, 60 kDrawVertices_DrawOp, 61 kRestore_DrawOp, 62 kRotate_DrawOp, 63 kSave_DrawOp, 64 kSaveLayer_DrawOp, 65 kScale_DrawOp, 66 kSetMatrix_DrawOp, 67 kSkew_DrawOp, 68 kTranslate_DrawOp, 69 70 kPaintOp_DrawOp, 71 kSetTypeface_DrawOp, 72 kSetAnnotation_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 // SkCanvas::DrawBitmapRectFlags::kBleed_DrawBitmapRectFlag is 152 // converted into and out of this flag to save space 153 kDrawBitmap_Bleed_DrawOpFlag = 1 << 2, 154 }; 155 enum { 156 kClip_HasAntiAlias_DrawOpFlag = 1 << 0, 157 }; 158 /////////////////////////////////////////////////////////////////////////////// 159 160 class BitmapInfo : SkNoncopyable { 161 public: 162 BitmapInfo(SkBitmap* bitmap, uint32_t genID, int toBeDrawnCount) 163 : fBitmap(bitmap) 164 , fGenID(genID) 165 , fBytesAllocated(0) 166 , fMoreRecentlyUsed(NULL) 167 , fLessRecentlyUsed(NULL) 168 , fToBeDrawnCount(toBeDrawnCount) 169 {} 170 171 ~BitmapInfo() { 172 SkASSERT(0 == fToBeDrawnCount); 173 SkDELETE(fBitmap); 174 } 175 176 void addDraws(int drawsToAdd) { 177 if (0 == fToBeDrawnCount) { 178 // The readers will only ever decrement the count, so once the 179 // count is zero, the writer will be the only one modifying it, 180 // so it does not need to be an atomic operation. 181 fToBeDrawnCount = drawsToAdd; 182 } else { 183 sk_atomic_add(&fToBeDrawnCount, drawsToAdd); 184 } 185 } 186 187 void decDraws() { 188 sk_atomic_dec(&fToBeDrawnCount); 189 } 190 191 int drawCount() const { 192 return fToBeDrawnCount; 193 } 194 195 SkBitmap* fBitmap; 196 // Store the generation ID of the original bitmap, since copying does 197 // not copy this field, so fBitmap's generation ID will not be useful 198 // for comparing. 199 // FIXME: Is it reasonable to make copying a bitmap/pixelref copy the 200 // generation ID? 201 uint32_t fGenID; 202 // Keep track of the bytes allocated for this bitmap. When replacing the 203 // bitmap or removing this BitmapInfo we know how much memory has been 204 // reclaimed. 205 size_t fBytesAllocated; 206 // TODO: Generalize the LRU caching mechanism 207 BitmapInfo* fMoreRecentlyUsed; 208 BitmapInfo* fLessRecentlyUsed; 209 private: 210 int fToBeDrawnCount; 211 }; 212 213 static inline bool shouldFlattenBitmaps(uint32_t flags) { 214 return SkToBool(flags & SkGPipeWriter::kCrossProcess_Flag 215 && !(flags & SkGPipeWriter::kSharedAddressSpace_Flag)); 216 } 217 218 /////////////////////////////////////////////////////////////////////////////// 219 220 enum PaintOps { 221 kReset_PaintOp, // no arg 222 223 kFlags_PaintOp, // arg inline 224 kColor_PaintOp, // arg 32 225 kStyle_PaintOp, // arg inline 226 kJoin_PaintOp, // arg inline 227 kCap_PaintOp, // arg inline 228 kWidth_PaintOp, // arg scalar 229 kMiter_PaintOp, // arg scalar 230 231 kEncoding_PaintOp, // arg inline - text 232 kHinting_PaintOp, // arg inline - text 233 kAlign_PaintOp, // arg inline - text 234 kTextSize_PaintOp, // arg scalar - text 235 kTextScaleX_PaintOp,// arg scalar - text 236 kTextSkewX_PaintOp, // arg scalar - text 237 kTypeface_PaintOp, // arg inline (index) - text 238 239 kFlatIndex_PaintOp, // flags=paintflat, data=index 240 }; 241 242 #define PAINTOPS_OP_BITS 8 243 #define PAINTOPS_FLAG_BITS 4 244 #define PAINTOPS_DATA_BITS 20 245 246 #define PAINTOPS_OP_MASK ((1 << PAINTOPS_OP_BITS) - 1) 247 #define PAINTOPS_FLAG_MASK ((1 << PAINTOPS_FLAG_BITS) - 1) 248 #define PAINTOPS_DATA_MASK ((1 << PAINTOPS_DATA_BITS) - 1) 249 250 static inline unsigned PaintOp_unpackOp(uint32_t op32) { 251 return (op32 >> (PAINTOPS_FLAG_BITS + PAINTOPS_DATA_BITS)); 252 } 253 254 static inline unsigned PaintOp_unpackFlags(uint32_t op32) { 255 return (op32 >> PAINTOPS_DATA_BITS) & PAINTOPS_FLAG_MASK; 256 } 257 258 static inline unsigned PaintOp_unpackData(uint32_t op32) { 259 return op32 & PAINTOPS_DATA_MASK; 260 } 261 262 static inline uint32_t PaintOp_packOp(PaintOps op) { 263 SkASSERT(0 == (op & ~PAINTOPS_OP_MASK)); 264 265 return op << (PAINTOPS_FLAG_BITS + PAINTOPS_DATA_BITS); 266 } 267 268 static inline uint32_t PaintOp_packOpData(PaintOps op, unsigned data) { 269 SkASSERT(0 == (op & ~PAINTOPS_OP_MASK)); 270 SkASSERT(0 == (data & ~PAINTOPS_DATA_MASK)); 271 272 return (op << (PAINTOPS_FLAG_BITS + PAINTOPS_DATA_BITS)) | data; 273 } 274 275 static inline uint32_t PaintOp_packOpFlagData(PaintOps op, unsigned flags, unsigned data) { 276 SkASSERT(0 == (op & ~PAINTOPS_OP_MASK)); 277 SkASSERT(0 == (flags & ~PAINTOPS_FLAG_MASK)); 278 SkASSERT(0 == (data & ~PAINTOPS_DATA_MASK)); 279 280 return (op << (PAINTOPS_FLAG_BITS + PAINTOPS_DATA_BITS)) | 281 (flags << PAINTOPS_DATA_BITS) | 282 data; 283 } 284 285 #endif 286