3 * Copyright 2011 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
11 #ifndef SkGPipePriv_DEFINED
12 #define SkGPipePriv_DEFINED
18 // these must be contiguous, 0...N-1
20 kColorFilter_PaintFlat,
21 kDrawLooper_PaintFlat,
22 kImageFilter_PaintFlat,
23 kMaskFilter_PaintFlat,
24 kPathEffect_PaintFlat,
25 kRasterizer_PaintFlat,
29 kLast_PaintFlat = kXfermode_PaintFlat
31 #define kCount_PaintFlats (kLast_PaintFlat + 1)
34 kSkip_DrawOp, // skip an addition N bytes (N == data)
36 // these match Canvas apis
43 kDrawBitmapMatrix_DrawOp,
44 kDrawBitmapNine_DrawOp,
45 kDrawBitmapRectToRect_DrawOp,
62 kDrawTextOnPath_DrawOp,
75 kSetAnnotation_DrawOp,
78 kDef_Flattenable_DrawOp,
82 // these are signals to playback, not drawing verbs
84 kShareBitmapHeap_DrawOp,
89 * DrawOp packs into a 32bit int as follows
91 * DrawOp:8 - Flags:4 - Data:20
93 * Flags and Data are called out separately, so we can reuse Data between
94 * different Ops that might have different Flags. e.g. Data might be a Paint
95 * index for both drawRect (no flags) and saveLayer (does have flags).
97 * All Ops that take a SkPaint use their Data field to store the index to
98 * the paint (previously defined with kPaintOp_DrawOp).
101 #define DRAWOPS_OP_BITS 8
102 #define DRAWOPS_FLAG_BITS 4
103 #define DRAWOPS_DATA_BITS 20
105 #define DRAWOPS_OP_MASK ((1 << DRAWOPS_OP_BITS) - 1)
106 #define DRAWOPS_FLAG_MASK ((1 << DRAWOPS_FLAG_BITS) - 1)
107 #define DRAWOPS_DATA_MASK ((1 << DRAWOPS_DATA_BITS) - 1)
109 static inline unsigned DrawOp_unpackOp(uint32_t op32) {
110 return (op32 >> (DRAWOPS_FLAG_BITS + DRAWOPS_DATA_BITS));
113 static inline unsigned DrawOp_unpackFlags(uint32_t op32) {
114 return (op32 >> DRAWOPS_DATA_BITS) & DRAWOPS_FLAG_MASK;
117 static inline unsigned DrawOp_unpackData(uint32_t op32) {
118 return op32 & DRAWOPS_DATA_MASK;
121 static inline uint32_t DrawOp_packOpFlagData(DrawOps op, unsigned flags, unsigned data) {
122 SkASSERT(0 == (op & ~DRAWOPS_OP_MASK));
123 SkASSERT(0 == (flags & ~DRAWOPS_FLAG_MASK));
124 SkASSERT(0 == (data & ~DRAWOPS_DATA_MASK));
126 return (op << (DRAWOPS_FLAG_BITS + DRAWOPS_DATA_BITS)) |
127 (flags << DRAWOPS_DATA_BITS) |
131 /** DrawOp specific flag bits
135 kSaveLayer_HasBounds_DrawOpFlag = 1 << 0,
136 kSaveLayer_HasPaint_DrawOpFlag = 1 << 1,
139 kClear_HasColor_DrawOpFlag = 1 << 0
142 kDrawTextOnPath_HasMatrix_DrawOpFlag = 1 << 0
145 kDrawVertices_HasTexs_DrawOpFlag = 1 << 0,
146 kDrawVertices_HasColors_DrawOpFlag = 1 << 1,
147 kDrawVertices_HasIndices_DrawOpFlag = 1 << 2,
148 kDrawVertices_HasXfermode_DrawOpFlag = 1 << 3,
151 kDrawBitmap_HasPaint_DrawOpFlag = 1 << 0,
152 // Specific to drawBitmapRect, but needs to be different from HasPaint,
153 // which is used for all drawBitmap calls, so include it here.
154 kDrawBitmap_HasSrcRect_DrawOpFlag = 1 << 1,
155 // SkCanvas::DrawBitmapRectFlags::kBleed_DrawBitmapRectFlag is
156 // converted into and out of this flag to save space
157 kDrawBitmap_Bleed_DrawOpFlag = 1 << 2,
160 kClip_HasAntiAlias_DrawOpFlag = 1 << 0,
162 ///////////////////////////////////////////////////////////////////////////////
164 class BitmapInfo : SkNoncopyable {
166 BitmapInfo(SkBitmap* bitmap, uint32_t genID, int toBeDrawnCount)
170 , fMoreRecentlyUsed(NULL)
171 , fLessRecentlyUsed(NULL)
172 , fToBeDrawnCount(toBeDrawnCount)
176 SkASSERT(0 == fToBeDrawnCount);
180 void addDraws(int drawsToAdd) {
181 if (0 == fToBeDrawnCount) {
182 // The readers will only ever decrement the count, so once the
183 // count is zero, the writer will be the only one modifying it,
184 // so it does not need to be an atomic operation.
185 fToBeDrawnCount = drawsToAdd;
187 sk_atomic_add(&fToBeDrawnCount, drawsToAdd);
192 sk_atomic_dec(&fToBeDrawnCount);
195 int drawCount() const {
196 return fToBeDrawnCount;
200 // Store the generation ID of the original bitmap, since copying does
201 // not copy this field, so fBitmap's generation ID will not be useful
203 // FIXME: Is it reasonable to make copying a bitmap/pixelref copy the
206 // Keep track of the bytes allocated for this bitmap. When replacing the
207 // bitmap or removing this BitmapInfo we know how much memory has been
209 size_t fBytesAllocated;
210 // TODO: Generalize the LRU caching mechanism
211 BitmapInfo* fMoreRecentlyUsed;
212 BitmapInfo* fLessRecentlyUsed;
217 static inline bool shouldFlattenBitmaps(uint32_t flags) {
218 return SkToBool(flags & SkGPipeWriter::kCrossProcess_Flag
219 && !(flags & SkGPipeWriter::kSharedAddressSpace_Flag));
222 ///////////////////////////////////////////////////////////////////////////////
225 kReset_PaintOp, // no arg
227 kFlags_PaintOp, // arg inline
228 kColor_PaintOp, // arg 32
229 kFilterLevel_PaintOp, // arg inline
230 kStyle_PaintOp, // arg inline
231 kJoin_PaintOp, // arg inline
232 kCap_PaintOp, // arg inline
233 kWidth_PaintOp, // arg scalar
234 kMiter_PaintOp, // arg scalar
236 kEncoding_PaintOp, // arg inline - text
237 kHinting_PaintOp, // arg inline - text
238 kAlign_PaintOp, // arg inline - text
239 kTextSize_PaintOp, // arg scalar - text
240 kTextScaleX_PaintOp,// arg scalar - text
241 kTextSkewX_PaintOp, // arg scalar - text
242 kTypeface_PaintOp, // arg inline (index) - text
244 kFlatIndex_PaintOp, // flags=paintflat, data=index
247 #define PAINTOPS_OP_BITS 8
248 #define PAINTOPS_FLAG_BITS 4
249 #define PAINTOPS_DATA_BITS 20
251 #define PAINTOPS_OP_MASK ((1 << PAINTOPS_OP_BITS) - 1)
252 #define PAINTOPS_FLAG_MASK ((1 << PAINTOPS_FLAG_BITS) - 1)
253 #define PAINTOPS_DATA_MASK ((1 << PAINTOPS_DATA_BITS) - 1)
255 static inline unsigned PaintOp_unpackOp(uint32_t op32) {
256 return (op32 >> (PAINTOPS_FLAG_BITS + PAINTOPS_DATA_BITS));
259 static inline unsigned PaintOp_unpackFlags(uint32_t op32) {
260 return (op32 >> PAINTOPS_DATA_BITS) & PAINTOPS_FLAG_MASK;
263 static inline unsigned PaintOp_unpackData(uint32_t op32) {
264 return op32 & PAINTOPS_DATA_MASK;
267 static inline uint32_t PaintOp_packOp(PaintOps op) {
268 SkASSERT(0 == (op & ~PAINTOPS_OP_MASK));
270 return op << (PAINTOPS_FLAG_BITS + PAINTOPS_DATA_BITS);
273 static inline uint32_t PaintOp_packOpData(PaintOps op, unsigned data) {
274 SkASSERT(0 == (op & ~PAINTOPS_OP_MASK));
275 SkASSERT(0 == (data & ~PAINTOPS_DATA_MASK));
277 return (op << (PAINTOPS_FLAG_BITS + PAINTOPS_DATA_BITS)) | data;
280 static inline uint32_t PaintOp_packOpFlagData(PaintOps op, unsigned flags, unsigned data) {
281 SkASSERT(0 == (op & ~PAINTOPS_OP_MASK));
282 SkASSERT(0 == (flags & ~PAINTOPS_FLAG_MASK));
283 SkASSERT(0 == (data & ~PAINTOPS_DATA_MASK));
285 return (op << (PAINTOPS_FLAG_BITS + PAINTOPS_DATA_BITS)) |
286 (flags << PAINTOPS_DATA_BITS) |